comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/typesystem/TypeSystemCodeGenerator.java @ 16755:bd28da642eea

Truffle-DSL: Several new features implemented: Implementation of a new code generation layout which shares code between generated nodes. Declaration order of specializations is now used as specialization order. Specializations do no longer perform fallthrough on respecialization, they now always respecialize from the first specialization. Implemented support for contains relations between specializations. Improved reachability error messages. Preliminary support for @Implies.
author Christian Humer <christian.humer@gmail.com>
date Mon, 11 Aug 2014 15:53:05 +0200
parents bd5c996b5d25
children
comparison
equal deleted inserted replaced
16754:55fd5be68a52 16755:bd28da642eea
34 import com.oracle.truffle.dsl.processor.ast.*; 34 import com.oracle.truffle.dsl.processor.ast.*;
35 import com.oracle.truffle.dsl.processor.template.*; 35 import com.oracle.truffle.dsl.processor.template.*;
36 36
37 public class TypeSystemCodeGenerator extends CompilationUnitFactory<TypeSystemData> { 37 public class TypeSystemCodeGenerator extends CompilationUnitFactory<TypeSystemData> {
38 38
39 public TypeSystemCodeGenerator(ProcessorContext context) {
40 super(context);
41 }
42
43 public static String isTypeMethodName(TypeData type) { 39 public static String isTypeMethodName(TypeData type) {
44 return "is" + Utils.getTypeId(type.getBoxedType()); 40 return "is" + Utils.getTypeId(type.getBoxedType());
45 } 41 }
46 42
47 public static String isImplicitTypeMethodName(TypeData type) { 43 public static String isImplicitTypeMethodName(TypeData type) {
60 return "getImplicit" + Utils.getTypeId(type.getBoxedType()) + "Class"; 56 return "getImplicit" + Utils.getTypeId(type.getBoxedType()) + "Class";
61 } 57 }
62 58
63 public static String expectTypeMethodName(TypeData type) { 59 public static String expectTypeMethodName(TypeData type) {
64 return "expect" + Utils.getTypeId(type.getBoxedType()); 60 return "expect" + Utils.getTypeId(type.getBoxedType());
61 }
62
63 public static String typeName(TypeSystemData typeSystem) {
64 String name = getSimpleName(typeSystem.getTemplateType());
65 return name + "Gen";
66 }
67
68 public static String singletonName(TypeSystemData type) {
69 return createConstantName(getSimpleName(type.getTemplateType().asType()));
65 } 70 }
66 71
67 /** 72 /**
68 * Finds the generated singleton field for a TypeSytemData instance. TypeSystemCodeGenerator 73 * Finds the generated singleton field for a TypeSytemData instance. TypeSystemCodeGenerator
69 * must be applied to the TypeSystemData model before use. 74 * must be applied to the TypeSystemData model before use.
70 */ 75 */
71 public static VariableElement findSingleton(ProcessorContext context, TypeSystemData typeSystem) { 76 public static VariableElement findSingleton(ProcessorContext context, TypeSystemData typeSystem) {
72 TypeMirror type = context.findGeneratedClassBySimpleName(TypeClassFactory.typeName(typeSystem), typeSystem); 77 TypeMirror type = context.findGeneratedClassBySimpleName(typeName(typeSystem), typeSystem);
73 return Utils.findDeclaredField(type, TypeClassFactory.singletonName(typeSystem.getTemplateType().asType())); 78 return Utils.findDeclaredField(type, singletonName(typeSystem));
74 } 79 }
75 80
76 @Override 81 @Override
77 protected void createChildren(TypeSystemData m) { 82 protected void createChildren(TypeSystemData m) {
78 add(new TypeClassFactory(context), m); 83 add(new TypeClassFactory(), m);
79 } 84 }
80 85
81 protected static class TypeClassFactory extends ClassElementFactory<TypeSystemData> { 86 protected static class TypeClassFactory extends ClassElementFactory<TypeSystemData> {
82 87
83 private static final String LOCAL_VALUE = "value"; 88 private static final String LOCAL_VALUE = "value";
84
85 public TypeClassFactory(ProcessorContext context) {
86 super(context);
87 }
88 89
89 @Override 90 @Override
90 public CodeTypeElement create(TypeSystemData typeSystem) { 91 public CodeTypeElement create(TypeSystemData typeSystem) {
91 String name = typeName(typeSystem); 92 String name = typeName(typeSystem);
92 CodeTypeElement clazz = createClass(typeSystem, modifiers(PUBLIC, FINAL), name, typeSystem.getTemplateType().asType(), false); 93 CodeTypeElement clazz = createClass(typeSystem, modifiers(PUBLIC, FINAL), name, typeSystem.getTemplateType().asType(), false);
125 sourceTypes.add(cast.getCheckedType()); 126 sourceTypes.add(cast.getCheckedType());
126 } 127 }
127 return new ArrayList<>(sourceTypes); 128 return new ArrayList<>(sourceTypes);
128 } 129 }
129 130
130 private static String typeName(TypeSystemData typeSystem) {
131 String name = getSimpleName(typeSystem.getTemplateType());
132 return name + "Gen";
133 }
134
135 private static String singletonName(TypeMirror type) {
136 return createConstantName(getSimpleName(type));
137 }
138
139 private CodeVariableElement createSingleton(CodeTypeElement clazz) { 131 private CodeVariableElement createSingleton(CodeTypeElement clazz) {
140 CodeVariableElement field = new CodeVariableElement(modifiers(PUBLIC, STATIC, FINAL), clazz.asType(), singletonName(getModel().getTemplateType().asType())); 132 CodeVariableElement field = new CodeVariableElement(modifiers(PUBLIC, STATIC, FINAL), clazz.asType(), singletonName(getModel()));
141 field.createInitBuilder().startNew(clazz.asType()).end(); 133 field.createInitBuilder().startNew(clazz.asType()).end();
142 return field; 134 return field;
143 } 135 }
144 136
145 private CodeExecutableElement createIsImplicitTypeMethod(TypeData type, boolean typed) { 137 private CodeExecutableElement createIsImplicitTypeMethod(TypeData type, boolean typed) {