Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/generator/GeneratorUtils.java @ 18761:a665483c3881
Truffle-DSL: new node layout implementation.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 29 Dec 2014 23:38:54 +0100 |
parents | f6b8787dc113 |
children | ae81dd154fb6 |
comparison
equal
deleted
inserted
replaced
18760:6fa3999631d8 | 18761:a665483c3881 |
---|---|
29 | 29 |
30 import javax.lang.model.element.*; | 30 import javax.lang.model.element.*; |
31 import javax.lang.model.type.*; | 31 import javax.lang.model.type.*; |
32 import javax.lang.model.util.*; | 32 import javax.lang.model.util.*; |
33 | 33 |
34 import com.oracle.truffle.api.*; | |
34 import com.oracle.truffle.api.dsl.*; | 35 import com.oracle.truffle.api.dsl.*; |
36 import com.oracle.truffle.api.dsl.internal.DSLOptions.*; | |
35 import com.oracle.truffle.dsl.processor.*; | 37 import com.oracle.truffle.dsl.processor.*; |
36 import com.oracle.truffle.dsl.processor.java.model.*; | 38 import com.oracle.truffle.dsl.processor.java.model.*; |
37 import com.oracle.truffle.dsl.processor.model.*; | 39 import com.oracle.truffle.dsl.processor.model.*; |
38 | 40 |
39 public class GeneratorUtils { | 41 public class GeneratorUtils { |
40 | 42 |
41 public static CodeExecutableElement createConstructorUsingFields(ProcessorContext context, Set<Modifier> modifiers, CodeTypeElement clazz) { | 43 public static CodeTree createTransferToInterpreterAndInvalidate() { |
44 ProcessorContext context = ProcessorContext.getInstance(); | |
45 CodeTreeBuilder builder = CodeTreeBuilder.createBuilder(); | |
46 builder.startStatement().startStaticCall(context.getType(CompilerDirectives.class), "transferToInterpreterAndInvalidate").end().end(); | |
47 return builder.build(); | |
48 } | |
49 | |
50 public static CodeExecutableElement createConstructorUsingFields(Set<Modifier> modifiers, CodeTypeElement clazz) { | |
51 TypeElement superClass = fromTypeMirror(clazz.getSuperclass()); | |
52 ExecutableElement constructor = findConstructor(superClass); | |
53 return createConstructorUsingFields(modifiers, clazz, constructor); | |
54 } | |
55 | |
56 public static CodeExecutableElement createConstructorUsingFields(Set<Modifier> modifiers, CodeTypeElement clazz, ExecutableElement constructor) { | |
42 CodeExecutableElement method = new CodeExecutableElement(modifiers, null, clazz.getSimpleName().toString()); | 57 CodeExecutableElement method = new CodeExecutableElement(modifiers, null, clazz.getSimpleName().toString()); |
43 CodeTreeBuilder builder = method.createBuilder(); | 58 CodeTreeBuilder builder = method.createBuilder(); |
44 TypeElement superClass = fromTypeMirror(clazz.getSuperclass()); | |
45 ExecutableElement constructor = findConstructor(superClass); | |
46 if (constructor != null && constructor.getParameters().size() > 0) { | 59 if (constructor != null && constructor.getParameters().size() > 0) { |
47 builder.startStatement(); | 60 builder.startStatement(); |
48 builder.startSuperCall(); | 61 builder.startSuperCall(); |
49 for (VariableElement parameter : constructor.getParameters()) { | 62 for (VariableElement parameter : constructor.getParameters()) { |
50 method.addParameter(new CodeVariableElement(parameter.asType(), parameter.getSimpleName().toString())); | 63 method.addParameter(new CodeVariableElement(parameter.asType(), parameter.getSimpleName().toString())); |
62 method.addParameter(new CodeVariableElement(field.asType(), fieldName)); | 75 method.addParameter(new CodeVariableElement(field.asType(), fieldName)); |
63 builder.startStatement(); | 76 builder.startStatement(); |
64 builder.string("this."); | 77 builder.string("this."); |
65 builder.string(fieldName); | 78 builder.string(fieldName); |
66 builder.string(" = "); | 79 builder.string(" = "); |
67 if (isAssignable(field.asType(), context.getTruffleTypes().getNode())) { | 80 builder.string(fieldName); |
68 builder.string("adoptChild(").string(fieldName).string(")"); | |
69 } else { | |
70 builder.string(fieldName); | |
71 } | |
72 builder.end(); // statement | 81 builder.end(); // statement |
73 } | 82 } |
74 | 83 |
75 return method; | 84 return method; |
85 } | |
86 | |
87 public static boolean isTypeBoxingOptimized(TypeBoxingOptimization boxing, TypeData type) { | |
88 switch (boxing) { | |
89 case NONE: | |
90 return false; | |
91 case ALWAYS: | |
92 return !type.isGeneric() && !type.isVoid(); | |
93 case PRIMITIVE: | |
94 return type.isPrimitive(); | |
95 default: | |
96 throw new AssertionError(); | |
97 } | |
76 } | 98 } |
77 | 99 |
78 private static ExecutableElement findConstructor(TypeElement clazz) { | 100 private static ExecutableElement findConstructor(TypeElement clazz) { |
79 List<ExecutableElement> constructors = ElementFilter.constructorsIn(clazz.getEnclosedElements()); | 101 List<ExecutableElement> constructors = ElementFilter.constructorsIn(clazz.getEnclosedElements()); |
80 if (constructors.isEmpty()) { | 102 if (constructors.isEmpty()) { |
101 b.end(); | 123 b.end(); |
102 | 124 |
103 return executable; | 125 return executable; |
104 } | 126 } |
105 | 127 |
106 public static CodeTypeElement createClass(Template model, Set<Modifier> modifiers, String simpleName, TypeMirror superType, boolean enumType) { | 128 public static CodeTypeElement createClass(Template sourceModel, TemplateMethod sourceMethod, Set<Modifier> modifiers, String simpleName, TypeMirror superType) { |
107 TypeElement templateType = model.getTemplateType(); | 129 TypeElement templateType = sourceModel.getTemplateType(); |
108 | 130 |
109 ProcessorContext context = ProcessorContext.getInstance(); | 131 ProcessorContext context = ProcessorContext.getInstance(); |
110 | 132 |
111 PackageElement pack = context.getEnvironment().getElementUtils().getPackageOf(templateType); | 133 PackageElement pack = context.getEnvironment().getElementUtils().getPackageOf(templateType); |
112 CodeTypeElement clazz = new CodeTypeElement(modifiers, enumType ? ElementKind.ENUM : ElementKind.CLASS, pack, simpleName); | 134 CodeTypeElement clazz = new CodeTypeElement(modifiers, ElementKind.CLASS, pack, simpleName); |
113 TypeMirror resolvedSuperType = superType; | 135 TypeMirror resolvedSuperType = superType; |
114 if (resolvedSuperType == null) { | 136 if (resolvedSuperType == null) { |
115 resolvedSuperType = context.getType(Object.class); | 137 resolvedSuperType = context.getType(Object.class); |
116 } | 138 } |
117 clazz.setSuperClass(resolvedSuperType); | 139 clazz.setSuperClass(resolvedSuperType); |
118 | 140 |
119 CodeAnnotationMirror generatedByAnnotation = new CodeAnnotationMirror((DeclaredType) context.getType(GeneratedBy.class)); | 141 CodeAnnotationMirror generatedByAnnotation = new CodeAnnotationMirror((DeclaredType) context.getType(GeneratedBy.class)); |
120 generatedByAnnotation.setElementValue(generatedByAnnotation.findExecutableElement("value"), new CodeAnnotationValue(templateType.asType())); | 142 generatedByAnnotation.setElementValue(generatedByAnnotation.findExecutableElement("value"), new CodeAnnotationValue(templateType.asType())); |
121 if (model.getTemplateMethodName() != null) { | 143 if (sourceMethod != null && sourceMethod.getMethod() != null) { |
122 generatedByAnnotation.setElementValue(generatedByAnnotation.findExecutableElement("methodName"), new CodeAnnotationValue(model.getTemplateMethodName())); | 144 generatedByAnnotation.setElementValue(generatedByAnnotation.findExecutableElement("methodName"), new CodeAnnotationValue(sourceMethod.createReferenceName())); |
123 } | 145 } |
124 | 146 |
125 clazz.addAnnotationMirror(generatedByAnnotation); | 147 clazz.addAnnotationMirror(generatedByAnnotation); |
126 return clazz; | 148 return clazz; |
127 } | 149 } |