comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/generator/TypeSystemNodeFactory.java @ 20938:18c0f02fa4d2

Truffle-DSL: make type systems optional.
author Christian Humer <christian.humer@gmail.com>
date Tue, 14 Apr 2015 15:12:48 +0200
parents c0fb70634640
children 476374f3fe9a
comparison
equal deleted inserted replaced
20937:37ea76052733 20938:18c0f02fa4d2
48 this.typeSystem = typeSystem; 48 this.typeSystem = typeSystem;
49 this.options = typeSystem.getOptions(); 49 this.options = typeSystem.getOptions();
50 } 50 }
51 51
52 public static TypeMirror nodeType(TypeSystemData typeSystem) { 52 public static TypeMirror nodeType(TypeSystemData typeSystem) {
53 TypeMirror parentType = TypeSystemCodeGenerator.createTypeSystemGen(typeSystem); 53 if (typeSystem.isDefault()) {
54 return new GeneratedTypeMirror(getQualifiedName(parentType), typeName(typeSystem)); 54 return typeSystem.getContext().getType(SpecializationNode.class);
55 } else {
56 TypeMirror parentType = TypeSystemCodeGenerator.createTypeSystemGen(typeSystem);
57 return new GeneratedTypeMirror(getQualifiedName(parentType), typeName(typeSystem));
58 }
55 } 59 }
56 60
57 public static String typeName(TypeSystemData typeSystem) { 61 public static String typeName(TypeSystemData typeSystem) {
58 return getTypeId(typeSystem.getTemplateType().asType()) + "Node"; 62 return getTypeId(typeSystem.getTemplateType().asType()) + "Node";
59 } 63 }
60 64
61 public static String acceptAndExecuteName() { 65 public static String acceptAndExecuteName() {
62 return "acceptAndExecute"; 66 return "acceptAndExecute";
63 } 67 }
64 68
65 public static String executeName(TypeData type) { 69 public static String executeName(TypeMirror type) {
66 if (type == null) { 70 if (type == null) {
67 return acceptAndExecuteName(); 71 return acceptAndExecuteName();
68 } else if (type.isGeneric()) { 72 } else if (ElementUtils.isObject(type)) {
69 return "executeGeneric"; 73 return "executeGeneric";
70 } else { 74 } else {
71 return "execute" + getTypeId(type.getBoxedType()); 75 return "execute" + getTypeId(type);
72 } 76 }
73 } 77 }
74 78
75 public static String voidBoxingExecuteName(TypeData type) { 79 public static String voidBoxingExecuteName(TypeMirror type) {
76 return executeName(type) + "Void"; 80 return executeName(type) + "Void";
77 } 81 }
78 82
79 public CodeTypeElement create() { 83 public CodeTypeElement create() {
80 String typeName = typeName(typeSystem); 84 String typeName = typeName(typeSystem);
83 87
84 for (ExecutableElement constructor : ElementFilter.constructorsIn(ElementUtils.fromTypeMirror(baseType).getEnclosedElements())) { 88 for (ExecutableElement constructor : ElementFilter.constructorsIn(ElementUtils.fromTypeMirror(baseType).getEnclosedElements())) {
85 clazz.add(GeneratorUtils.createSuperConstructor(context, clazz, constructor)); 89 clazz.add(GeneratorUtils.createSuperConstructor(context, clazz, constructor));
86 } 90 }
87 91
88 for (TypeData type : typeSystem.getTypes()) { 92 for (TypeMirror type : typeSystem.getLegacyTypes()) {
89 clazz.add(createExecuteMethod(type)); 93 clazz.add(createExecuteMethod(type));
90 if (GeneratorUtils.isTypeBoxingOptimized(options.voidBoxingOptimization(), type)) { 94 if (GeneratorUtils.isTypeBoxingOptimized(options.voidBoxingOptimization(), type)) {
91 clazz.add(createVoidBoxingExecuteMethod(type)); 95 clazz.add(createVoidBoxingExecuteMethod(type));
92 } 96 }
93 } 97 }
94 return clazz; 98 return clazz;
95 } 99 }
96 100
97 private Element createVoidBoxingExecuteMethod(TypeData type) { 101 private Element createVoidBoxingExecuteMethod(TypeMirror type) {
98 TypeData voidType = typeSystem.getVoidType(); 102 TypeMirror voidType = context.getType(void.class);
99 String methodName = voidBoxingExecuteName(type); 103 String methodName = voidBoxingExecuteName(type);
100 CodeExecutableElement method = new CodeExecutableElement(modifiers(PROTECTED, FINAL), voidType.getPrimitiveType(), methodName); 104 CodeExecutableElement method = new CodeExecutableElement(modifiers(PROTECTED, FINAL), voidType, methodName);
101 method.addParameter(new CodeVariableElement(context.getType(Frame.class), "frame")); 105 method.addParameter(new CodeVariableElement(context.getType(Frame.class), "frame"));
102 106
103 CodeTreeBuilder builder = method.createBuilder(); 107 CodeTreeBuilder builder = method.createBuilder();
104 builder.startTryBlock(); 108 builder.startTryBlock();
105 builder.startStatement().startCall(executeName(type)).string("frame").end().end(); 109 builder.startStatement().startCall(executeName(type)).string("frame").end().end();
108 builder.end(); 112 builder.end();
109 113
110 return method; 114 return method;
111 } 115 }
112 116
113 private Element createExecuteMethod(TypeData type) { 117 private Element createExecuteMethod(TypeMirror type) {
114 TypeData genericType = typeSystem.getGenericTypeData(); 118 TypeMirror genericType = context.getType(Object.class);
115 String methodName = executeName(type); 119 String methodName = executeName(type);
116 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), type.getPrimitiveType(), methodName); 120 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), type, methodName);
117 method.addParameter(new CodeVariableElement(context.getType(Frame.class), "frame")); 121 method.addParameter(new CodeVariableElement(context.getType(Frame.class), "frame"));
118 122
119 if (type.isGeneric()) { 123 if (ElementUtils.isObject(type)) {
120 method.getModifiers().add(ABSTRACT); 124 method.getModifiers().add(ABSTRACT);
121 } else { 125 } else {
122 CodeTreeBuilder builder = method.createBuilder(); 126 CodeTreeBuilder builder = method.createBuilder();
123 CodeTree executeGeneric = builder.create().startCall(executeName(genericType)).string("frame").end().build(); 127 CodeTree executeGeneric = builder.create().startCall(executeName(genericType)).string("frame").end().build();
124 if (!type.isVoid()) { 128 if (!ElementUtils.isVoid(type)) {
125 method.getThrownTypes().add(context.getType(UnexpectedResultException.class)); 129 method.getThrownTypes().add(context.getType(UnexpectedResultException.class));
126 } 130 }
127 builder.startReturn().tree(TypeSystemCodeGenerator.expect(type, executeGeneric)).end(); 131 builder.startReturn().tree(TypeSystemCodeGenerator.expect(typeSystem, type, executeGeneric)).end();
128 } 132 }
129 133
130 return method; 134 return method;
131 } 135 }
132 } 136 }