Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/typesystem/TypeSystemCodeGenerator.java @ 9258:07f8d136a05e
Truffle API changes for the Frame API. Introduction of Assumptions class.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Tue, 23 Apr 2013 15:34:06 +0200 |
parents | 89006c76f737 |
children | 8b2573c8d47f |
comparison
equal
deleted
inserted
replaced
9257:542712a4732a | 9258:07f8d136a05e |
---|---|
77 @Override | 77 @Override |
78 public CodeTypeElement create(TypeSystemData typeSystem) { | 78 public CodeTypeElement create(TypeSystemData typeSystem) { |
79 String name = typeName(typeSystem); | 79 String name = typeName(typeSystem); |
80 CodeTypeElement clazz = createClass(typeSystem, modifiers(PUBLIC), name, typeSystem.getTemplateType().asType(), false); | 80 CodeTypeElement clazz = createClass(typeSystem, modifiers(PUBLIC), name, typeSystem.getTemplateType().asType(), false); |
81 | 81 |
82 clazz.getImplements().add(getContext().getTruffleTypes().getTypeConversion()); | |
83 | |
84 clazz.add(createConstructorUsingFields(modifiers(PROTECTED), clazz)); | 82 clazz.add(createConstructorUsingFields(modifiers(PROTECTED), clazz)); |
85 CodeVariableElement singleton = createSingleton(clazz); | 83 CodeVariableElement singleton = createSingleton(clazz); |
86 clazz.add(singleton); | 84 clazz.add(singleton); |
87 | |
88 clazz.add(createGetTopType(typeSystem)); | |
89 clazz.add(createConvertTo(typeSystem, singleton)); | |
90 | 85 |
91 for (TypeData type : typeSystem.getTypes()) { | 86 for (TypeData type : typeSystem.getTypes()) { |
92 if (!type.isGeneric()) { | 87 if (!type.isGeneric()) { |
93 CodeExecutableElement isType = createIsTypeMethod(type); | 88 CodeExecutableElement isType = createIsTypeMethod(type); |
94 if (isType != null) { | 89 if (isType != null) { |
121 } | 116 } |
122 for (TypeCheckData cast : type.getTypeChecks()) { | 117 for (TypeCheckData cast : type.getTypeChecks()) { |
123 sourceTypes.add(cast.getCheckedType()); | 118 sourceTypes.add(cast.getCheckedType()); |
124 } | 119 } |
125 return new ArrayList<>(sourceTypes); | 120 return new ArrayList<>(sourceTypes); |
126 } | |
127 | |
128 private CodeExecutableElement createConvertTo(TypeSystemData typeSystem, CodeVariableElement singleton) { | |
129 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), getContext().getType(Object.class), "convertTo"); | |
130 method.addParameter(new CodeVariableElement(getContext().getType(Class.class), "targetType")); | |
131 method.addParameter(new CodeVariableElement(getContext().getType(Object.class), "value")); | |
132 | |
133 CodeTreeBuilder builder = method.createBuilder(); | |
134 | |
135 boolean first = true; | |
136 for (TypeData type : typeSystem.getTypes()) { | |
137 if (first) { | |
138 builder.startIf(); | |
139 first = false; | |
140 } else { | |
141 builder.startElseIf(); | |
142 } | |
143 builder.string("targetType").string(" == ").typeLiteral(type.getBoxedType()); | |
144 builder.end(); // if | |
145 builder.startBlock(); | |
146 | |
147 builder.startReturn(); | |
148 | |
149 if (typeEquals(type.getBoxedType(), getContext().getType(Object.class))) { | |
150 builder.string("value"); | |
151 } else { | |
152 builder.string(singleton.getName()).string(".").startCall(asTypeMethodName(type)).string("value").end(); | |
153 } | |
154 | |
155 builder.end(); // return | |
156 | |
157 builder.end(); // block | |
158 } | |
159 | |
160 builder.startThrow().startNew(getContext().getType(IllegalArgumentException.class)).end().end(); | |
161 | |
162 return method; | |
163 } | |
164 | |
165 private CodeExecutableElement createGetTopType(TypeSystemData typeSystem) { | |
166 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), getContext().getType(Class.class), "getTopType"); | |
167 | |
168 CodeTreeBuilder builder = method.createBuilder(); | |
169 builder.startReturn(); | |
170 if (!typeSystem.getTypes().isEmpty()) { | |
171 builder.typeLiteral(typeSystem.getTypes().get(0).getBoxedType()); | |
172 } else { | |
173 builder.null_(); | |
174 } | |
175 builder.end(); // return | |
176 | |
177 return method; | |
178 } | 121 } |
179 | 122 |
180 private static String typeName(TypeSystemData typeSystem) { | 123 private static String typeName(TypeSystemData typeSystem) { |
181 String name = getSimpleName(typeSystem.getTemplateType()); | 124 String name = getSimpleName(typeSystem.getTemplateType()); |
182 return name + "Gen"; | 125 return name + "Gen"; |