Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/node/NodeParser.java @ 8248:c4c3f50fa9c2
Fixes for codegen builtins support.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Tue, 12 Mar 2013 11:37:32 +0100 |
parents | 703c09f8640c |
children | cb70ed101b5f |
comparison
equal
deleted
inserted
replaced
8247:5b08b0f4d338 | 8248:c4c3f50fa9c2 |
---|---|
53 protected NodeData parse(Element element, AnnotationMirror mirror) { | 53 protected NodeData parse(Element element, AnnotationMirror mirror) { |
54 assert element instanceof TypeElement; | 54 assert element instanceof TypeElement; |
55 NodeData node = null; | 55 NodeData node = null; |
56 try { | 56 try { |
57 parsedNodes = new HashMap<>(); | 57 parsedNodes = new HashMap<>(); |
58 node = parseInnerClassHierarchy((TypeElement) element); | 58 node = resolveNode((TypeElement) element); |
59 } finally { | 59 } finally { |
60 parsedNodes = null; | 60 parsedNodes = null; |
61 } | 61 } |
62 return node; | 62 return node; |
63 } | 63 } |
65 @Override | 65 @Override |
66 public boolean isDelegateToRootDeclaredType() { | 66 public boolean isDelegateToRootDeclaredType() { |
67 return true; | 67 return true; |
68 } | 68 } |
69 | 69 |
70 private NodeData parseInnerClassHierarchy(TypeElement rootType) { | 70 private NodeData resolveNode(TypeElement rootType) { |
71 String typeName = Utils.getQualifiedName(rootType); | |
72 if (parsedNodes.containsKey(typeName)) { | |
73 return parsedNodes.get(typeName); | |
74 } | |
75 | |
71 List<? extends TypeElement> types = ElementFilter.typesIn(rootType.getEnclosedElements()); | 76 List<? extends TypeElement> types = ElementFilter.typesIn(rootType.getEnclosedElements()); |
77 | |
72 List<NodeData> children = new ArrayList<>(); | 78 List<NodeData> children = new ArrayList<>(); |
73 for (TypeElement childElement : types) { | 79 for (TypeElement childElement : types) { |
74 NodeData childNode = parseInnerClassHierarchy(childElement); | 80 NodeData childNode = resolveNode(childElement); |
75 if (childNode != null) { | 81 if (childNode != null) { |
76 children.add(childNode); | 82 children.add(childNode); |
77 } | 83 } |
78 } | 84 } |
79 NodeData rootNode = resolveNode(rootType); | 85 NodeData rootNode = parseNode(rootType); |
80 if (rootNode == null && children.size() > 0) { | 86 if (rootNode == null && children.size() > 0) { |
81 rootNode = new NodeData(rootType, null, rootType.getSimpleName().toString()); | 87 rootNode = new NodeData(rootType, null, rootType.getSimpleName().toString()); |
82 } | 88 } |
89 | |
90 parsedNodes.put(typeName, rootNode); | |
91 | |
83 if (rootNode != null) { | 92 if (rootNode != null) { |
84 children.addAll(rootNode.getDeclaredChildren()); | 93 children.addAll(rootNode.getDeclaredChildren()); |
85 rootNode.setDeclaredChildren(children); | 94 rootNode.setDeclaredChildren(children); |
86 } | 95 } |
87 | 96 |
97 if (Log.DEBUG) { | |
98 NodeData parsed = parsedNodes.get(typeName); | |
99 if (parsed != null) { | |
100 String dump = parsed.dump(); | |
101 String valid = rootNode != null ? "" : " failed"; | |
102 String msg = String.format("Node parsing %s : %s", valid, dump); | |
103 log.error(msg); | |
104 System.out.println(msg); | |
105 } | |
106 } | |
107 | |
88 return rootNode; | 108 return rootNode; |
89 } | |
90 | |
91 private NodeData resolveNode(TypeElement currentType) { | |
92 String typeName = Utils.getQualifiedName(currentType); | |
93 if (!parsedNodes.containsKey(typeName)) { | |
94 NodeData node = parseNode(currentType); | |
95 if (node != null) { | |
96 parsedNodes.put(typeName, node); | |
97 } | |
98 | |
99 if (Log.DEBUG) { | |
100 NodeData parsed = parsedNodes.get(Utils.getQualifiedName(currentType)); | |
101 if (parsed != null) { | |
102 String dump = parsed.dump(); | |
103 String valid = node != null ? "" : " failed"; | |
104 String msg = String.format("Node parsing %s : %s", valid, dump); | |
105 log.error(msg); | |
106 System.out.println(msg); | |
107 } | |
108 } | |
109 | |
110 return node; | |
111 } | |
112 return parsedNodes.get(typeName); | |
113 } | 109 } |
114 | 110 |
115 private NodeData parseNode(TypeElement type) { | 111 private NodeData parseNode(TypeElement type) { |
116 if (Utils.findAnnotationMirror(processingEnv, type, GeneratedBy.class) != null) { | 112 if (Utils.findAnnotationMirror(processingEnv, type, GeneratedBy.class) != null) { |
117 // generated nodes should not get called again. | 113 // generated nodes should not get called again. |
126 | 122 |
127 TypeElement nodeType; | 123 TypeElement nodeType; |
128 boolean needsSplit; | 124 boolean needsSplit; |
129 if (methodNodes != null) { | 125 if (methodNodes != null) { |
130 needsSplit = methodNodes != null; | 126 needsSplit = methodNodes != null; |
131 nodeType = Utils.fromTypeMirror(Utils.getAnnotationValueType(methodNodes, "value")); | 127 nodeType = Utils.fromTypeMirror(Utils.getAnnotationValue(TypeMirror.class, methodNodes, "value")); |
132 } else { | 128 } else { |
133 needsSplit = false; | 129 needsSplit = false; |
134 nodeType = type; | 130 nodeType = type; |
135 } | 131 } |
136 | 132 |
171 } | 167 } |
172 if (!verifyNode(splittedNode)) { | 168 if (!verifyNode(splittedNode)) { |
173 valid = false; | 169 valid = false; |
174 } | 170 } |
175 } | 171 } |
172 | |
176 if (!valid) { | 173 if (!valid) { |
177 return null; | 174 return null; |
178 } | 175 } |
179 | 176 |
180 if (needsSplit) { | 177 if (needsSplit) { |
211 String nodeId = node.getNodeId(); | 208 String nodeId = node.getNodeId(); |
212 if (nodeId.endsWith("Node") && !nodeId.equals("Node")) { | 209 if (nodeId.endsWith("Node") && !nodeId.equals("Node")) { |
213 nodeId = nodeId.substring(0, nodeId.length() - 4); | 210 nodeId = nodeId.substring(0, nodeId.length() - 4); |
214 } | 211 } |
215 String newNodeId = nodeId + Utils.firstLetterUpperCase(id); | 212 String newNodeId = nodeId + Utils.firstLetterUpperCase(id); |
216 NodeData copy = new NodeData(node, newNodeId); | 213 NodeData copy = new NodeData(node, id, newNodeId); |
217 | 214 |
218 copy.setSpecializations(specializations); | 215 copy.setSpecializations(specializations); |
219 copy.setSpecializationListeners(listeners); | 216 copy.setSpecializationListeners(listeners); |
220 | 217 |
221 splitted.add(copy); | 218 splitted.add(copy); |
222 } | |
223 | |
224 if (splitted.isEmpty()) { | |
225 splitted.add(node); | |
226 } | 219 } |
227 | 220 |
228 node.setSpecializations(new ArrayList<SpecializationData>()); | 221 node.setSpecializations(new ArrayList<SpecializationData>()); |
229 node.setSpecializationListeners(new ArrayList<SpecializationListenerData>()); | 222 node.setSpecializationListeners(new ArrayList<SpecializationListenerData>()); |
230 | 223 |
458 if (typeSystemMirror == null) { | 451 if (typeSystemMirror == null) { |
459 log.error(templateType, "No @%s annotation found in type hierarchy of %s.", TypeSystemReference.class.getSimpleName(), nodeType.getQualifiedName().toString()); | 452 log.error(templateType, "No @%s annotation found in type hierarchy of %s.", TypeSystemReference.class.getSimpleName(), nodeType.getQualifiedName().toString()); |
460 return null; | 453 return null; |
461 } | 454 } |
462 | 455 |
463 TypeMirror typeSytemType = Utils.getAnnotationValueType(typeSystemMirror, "value"); | 456 TypeMirror typeSytemType = Utils.getAnnotationValue(TypeMirror.class, typeSystemMirror, "value"); |
464 final TypeSystemData typeSystem = (TypeSystemData) context.getTemplate(typeSytemType, true); | 457 final TypeSystemData typeSystem = (TypeSystemData) context.getTemplate(typeSytemType, true); |
465 if (typeSystem == null) { | 458 if (typeSystem == null) { |
466 log.error(templateType, "The used type system '%s' is invalid.", Utils.getQualifiedName(typeSytemType)); | 459 log.error(templateType, "The used type system '%s' is invalid.", Utils.getQualifiedName(typeSytemType)); |
467 return null; | 460 return null; |
468 } | 461 } |
469 | 462 |
470 String nodeId = templateType.getSimpleName().toString(); | |
471 if (nodeId.endsWith("Node") && !nodeId.equals("Node")) { | |
472 nodeId = nodeId.substring(0, nodeId.length() - 4); | |
473 } | |
474 | |
475 NodeData nodeData = new NodeData(templateType, typeSystem, templateType.getSimpleName().toString()); | 463 NodeData nodeData = new NodeData(templateType, typeSystem, templateType.getSimpleName().toString()); |
476 nodeData.setNodeType(nodeType.asType()); | 464 nodeData.setNodeType(nodeType.asType()); |
477 | 465 |
478 List<ExecutableTypeData> executableTypes = filterExecutableTypes(new ExecutableTypeMethodParser(context, nodeData).parse(elements)); | 466 List<ExecutableTypeData> executableTypes = filterExecutableTypes(new ExecutableTypeMethodParser(context, nodeData).parse(elements)); |
479 | 467 |
480 nodeData.setExecutableTypes(executableTypes); | 468 nodeData.setExecutableTypes(executableTypes); |
481 | 469 |
482 parsedNodes.put(Utils.getQualifiedName(nodeType), nodeData); | 470 parsedNodes.put(Utils.getQualifiedName(templateType), nodeData); |
483 | 471 |
484 List<NodeFieldData> fields = parseFields(nodeData, elements, typeHierarchy); | 472 List<NodeFieldData> fields = parseFields(nodeData, elements, typeHierarchy); |
485 if (fields == null) { | 473 if (fields == null) { |
486 return null; | 474 return null; |
487 } | 475 } |
488 nodeData.setFields(fields); | 476 nodeData.setFields(fields); |
489 | |
490 if (!Utils.isAssignable(templateType.asType(), nodeType.asType())) { | |
491 // nodeData.setInstanceParameterSpec(new ParameterSpec("instance", templateType.asType(), false, | |
492 // true)); | |
493 } | |
494 | 477 |
495 return nodeData; | 478 return nodeData; |
496 } | 479 } |
497 | 480 |
498 private boolean verifySpecializationParameters(NodeData nodeData) { | 481 private boolean verifySpecializationParameters(NodeData nodeData) { |
624 private List<NodeFieldData> parseFields(NodeData nodeData, List<? extends Element> elements, final List<TypeElement> typeHierarchy) { | 607 private List<NodeFieldData> parseFields(NodeData nodeData, List<? extends Element> elements, final List<TypeElement> typeHierarchy) { |
625 AnnotationMirror executionOrderMirror = findFirstAnnotation(typeHierarchy, ExecuteChildren.class); | 608 AnnotationMirror executionOrderMirror = findFirstAnnotation(typeHierarchy, ExecuteChildren.class); |
626 List<String> executionDefinition = null; | 609 List<String> executionDefinition = null; |
627 if (executionOrderMirror != null) { | 610 if (executionOrderMirror != null) { |
628 executionDefinition = new ArrayList<>(); | 611 executionDefinition = new ArrayList<>(); |
629 for (Object object : Utils.getAnnotationValueList(executionOrderMirror, "value")) { | 612 for (String object : Utils.getAnnotationValueList(String.class, executionOrderMirror, "value")) { |
630 executionDefinition.add((String) object); | 613 executionDefinition.add(object); |
631 } | 614 } |
632 } | 615 } |
633 | 616 |
634 Set<String> shortCircuits = new HashSet<>(); | 617 Set<String> shortCircuits = new HashSet<>(); |
635 for (ExecutableElement method : ElementFilter.methodsIn(elements)) { | 618 for (ExecutableElement method : ElementFilter.methodsIn(elements)) { |
636 AnnotationMirror mirror = Utils.findAnnotationMirror(processingEnv, method, ShortCircuit.class); | 619 AnnotationMirror mirror = Utils.findAnnotationMirror(processingEnv, method, ShortCircuit.class); |
637 if (mirror != null) { | 620 if (mirror != null) { |
638 shortCircuits.add(Utils.getAnnotationValueString(mirror, "value")); | 621 shortCircuits.add(Utils.getAnnotationValue(String.class, mirror, "value")); |
639 } | 622 } |
640 } | 623 } |
641 | 624 |
642 boolean valid = true; | 625 boolean valid = true; |
643 | 626 |