Thu, 24 Nov 2011 13:36:20 +0000
7115046: Add AST node for lambda expressions
Summary: Add tree nodes for representing lambda expressions and update relevant visitors interfaces
Reviewed-by: jjg
duke@1 | 1 | /* |
ohair@798 | 2 | * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javah; |
duke@1 | 27 | |
duke@1 | 28 | import java.io.OutputStream; |
duke@1 | 29 | import java.io.PrintWriter; |
jjg@416 | 30 | import java.util.ArrayList; |
jjg@416 | 31 | import java.util.List; |
jjg@416 | 32 | import javax.lang.model.element.Element; |
jjg@416 | 33 | import javax.lang.model.element.ExecutableElement; |
jjg@416 | 34 | import javax.lang.model.element.Modifier; |
jjg@416 | 35 | import javax.lang.model.element.TypeElement; |
jjg@416 | 36 | import javax.lang.model.element.VariableElement; |
jjg@416 | 37 | import javax.lang.model.type.ArrayType; |
jjg@416 | 38 | import javax.lang.model.type.TypeMirror; |
jjg@416 | 39 | import javax.lang.model.util.ElementFilter; |
duke@1 | 40 | |
duke@1 | 41 | |
duke@1 | 42 | /** |
duke@1 | 43 | * Header file generator for JNI. |
duke@1 | 44 | * |
jjg@581 | 45 | * <p><b>This is NOT part of any supported API. |
jjg@416 | 46 | * If you write code that depends on this, you do so at your own |
jjg@416 | 47 | * risk. This code and its internal interfaces are subject to change |
jjg@416 | 48 | * or deletion without notice.</b></p> |
jjg@416 | 49 | * |
duke@1 | 50 | * @author Sucheta Dambalkar(Revised) |
duke@1 | 51 | */ |
duke@1 | 52 | public class JNI extends Gen { |
jjg@416 | 53 | JNI(Util util) { |
jjg@416 | 54 | super(util); |
duke@1 | 55 | } |
duke@1 | 56 | |
duke@1 | 57 | public String getIncludes() { |
duke@1 | 58 | return "#include <jni.h>"; |
duke@1 | 59 | } |
duke@1 | 60 | |
jjg@416 | 61 | public void write(OutputStream o, TypeElement clazz) throws Util.Exit { |
jjg@712 | 62 | try { |
jjg@712 | 63 | String cname = mangler.mangle(clazz.getQualifiedName(), Mangle.Type.CLASS); |
jjg@712 | 64 | PrintWriter pw = wrapWriter(o); |
jjg@712 | 65 | pw.println(guardBegin(cname)); |
jjg@712 | 66 | pw.println(cppGuardBegin()); |
duke@1 | 67 | |
jjg@712 | 68 | /* Write statics. */ |
jjg@712 | 69 | List<VariableElement> classfields = getAllFields(clazz); |
duke@1 | 70 | |
jjg@712 | 71 | for (VariableElement v: classfields) { |
jjg@712 | 72 | if (!v.getModifiers().contains(Modifier.STATIC)) |
jjg@712 | 73 | continue; |
jjg@712 | 74 | String s = null; |
jjg@712 | 75 | s = defineForStatic(clazz, v); |
jjg@712 | 76 | if (s != null) { |
jjg@712 | 77 | pw.println(s); |
jjg@712 | 78 | } |
duke@1 | 79 | } |
jjg@712 | 80 | |
jjg@712 | 81 | /* Write methods. */ |
jjg@712 | 82 | List<ExecutableElement> classmethods = ElementFilter.methodsIn(clazz.getEnclosedElements()); |
jjg@712 | 83 | for (ExecutableElement md: classmethods) { |
jjg@712 | 84 | if(md.getModifiers().contains(Modifier.NATIVE)){ |
jjg@712 | 85 | TypeMirror mtr = types.erasure(md.getReturnType()); |
jjg@712 | 86 | String sig = signature(md); |
jjg@712 | 87 | TypeSignature newtypesig = new TypeSignature(elems); |
jjg@712 | 88 | CharSequence methodName = md.getSimpleName(); |
jjg@712 | 89 | boolean longName = false; |
jjg@712 | 90 | for (ExecutableElement md2: classmethods) { |
jjg@712 | 91 | if ((md2 != md) |
jjg@712 | 92 | && (methodName.equals(md2.getSimpleName())) |
jjg@712 | 93 | && (md2.getModifiers().contains(Modifier.NATIVE))) |
jjg@712 | 94 | longName = true; |
jjg@712 | 95 | |
jjg@712 | 96 | } |
jjg@712 | 97 | pw.println("/*"); |
jjg@712 | 98 | pw.println(" * Class: " + cname); |
jjg@712 | 99 | pw.println(" * Method: " + |
jjg@712 | 100 | mangler.mangle(methodName, Mangle.Type.FIELDSTUB)); |
jjg@712 | 101 | pw.println(" * Signature: " + newtypesig.getTypeSignature(sig, mtr)); |
jjg@712 | 102 | pw.println(" */"); |
jjg@712 | 103 | pw.println("JNIEXPORT " + jniType(mtr) + |
jjg@712 | 104 | " JNICALL " + |
jjg@712 | 105 | mangler.mangleMethod(md, clazz, |
jjg@712 | 106 | (longName) ? |
jjg@712 | 107 | Mangle.Type.METHOD_JNI_LONG : |
jjg@712 | 108 | Mangle.Type.METHOD_JNI_SHORT)); |
jjg@712 | 109 | pw.print(" (JNIEnv *, "); |
jjg@712 | 110 | List<? extends VariableElement> paramargs = md.getParameters(); |
jjg@712 | 111 | List<TypeMirror> args = new ArrayList<TypeMirror>(); |
jjg@712 | 112 | for (VariableElement p: paramargs) { |
jjg@712 | 113 | args.add(types.erasure(p.asType())); |
jjg@712 | 114 | } |
jjg@712 | 115 | if (md.getModifiers().contains(Modifier.STATIC)) |
jjg@712 | 116 | pw.print("jclass"); |
jjg@712 | 117 | else |
jjg@712 | 118 | pw.print("jobject"); |
jjg@712 | 119 | |
jjg@712 | 120 | for (TypeMirror arg: args) { |
jjg@712 | 121 | pw.print(", "); |
jjg@712 | 122 | pw.print(jniType(arg)); |
jjg@712 | 123 | } |
jjg@712 | 124 | pw.println(");" + lineSep); |
jjg@712 | 125 | } |
jjg@712 | 126 | } |
jjg@712 | 127 | pw.println(cppGuardEnd()); |
jjg@712 | 128 | pw.println(guardEnd(cname)); |
jjg@712 | 129 | } catch (TypeSignature.SignatureException e) { |
jjg@712 | 130 | util.error("jni.sigerror", e.getMessage()); |
duke@1 | 131 | } |
duke@1 | 132 | } |
duke@1 | 133 | |
duke@1 | 134 | |
jjg@416 | 135 | protected final String jniType(TypeMirror t) throws Util.Exit { |
jjg@416 | 136 | TypeElement throwable = elems.getTypeElement("java.lang.Throwable"); |
jjg@416 | 137 | TypeElement jClass = elems.getTypeElement("java.lang.Class"); |
jjg@416 | 138 | TypeElement jString = elems.getTypeElement("java.lang.String"); |
jjg@416 | 139 | Element tclassDoc = types.asElement(t); |
duke@1 | 140 | |
duke@1 | 141 | |
jjg@416 | 142 | switch (t.getKind()) { |
jjg@416 | 143 | case ARRAY: { |
jjg@416 | 144 | TypeMirror ct = ((ArrayType) t).getComponentType(); |
jjg@416 | 145 | switch (ct.getKind()) { |
jjg@416 | 146 | case BOOLEAN: return "jbooleanArray"; |
jjg@416 | 147 | case BYTE: return "jbyteArray"; |
jjg@416 | 148 | case CHAR: return "jcharArray"; |
jjg@416 | 149 | case SHORT: return "jshortArray"; |
jjg@416 | 150 | case INT: return "jintArray"; |
jjg@416 | 151 | case LONG: return "jlongArray"; |
jjg@416 | 152 | case FLOAT: return "jfloatArray"; |
jjg@416 | 153 | case DOUBLE: return "jdoubleArray"; |
jjg@416 | 154 | case ARRAY: |
jjg@416 | 155 | case DECLARED: return "jobjectArray"; |
jjg@416 | 156 | default: throw new Error(ct.toString()); |
jjg@416 | 157 | } |
jjg@416 | 158 | } |
jjg@416 | 159 | |
jjg@416 | 160 | case VOID: return "void"; |
jjg@416 | 161 | case BOOLEAN: return "jboolean"; |
jjg@416 | 162 | case BYTE: return "jbyte"; |
jjg@416 | 163 | case CHAR: return "jchar"; |
jjg@416 | 164 | case SHORT: return "jshort"; |
jjg@416 | 165 | case INT: return "jint"; |
jjg@416 | 166 | case LONG: return "jlong"; |
jjg@416 | 167 | case FLOAT: return "jfloat"; |
jjg@416 | 168 | case DOUBLE: return "jdouble"; |
jjg@416 | 169 | |
jjg@416 | 170 | case DECLARED: { |
jjg@416 | 171 | if (tclassDoc.equals(jString)) |
jjg@416 | 172 | return "jstring"; |
jjg@416 | 173 | else if (types.isAssignable(t, throwable.asType())) |
jjg@416 | 174 | return "jthrowable"; |
jjg@416 | 175 | else if (types.isAssignable(t, jClass.asType())) |
jjg@416 | 176 | return "jclass"; |
jjg@416 | 177 | else |
jjg@416 | 178 | return "jobject"; |
duke@1 | 179 | } |
duke@1 | 180 | } |
jjg@416 | 181 | |
jjg@416 | 182 | util.bug("jni.unknown.type"); |
duke@1 | 183 | return null; /* dead code. */ |
duke@1 | 184 | } |
duke@1 | 185 | } |