comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/generator/GeneratorUtils.java @ 18753:f6b8787dc113

Truffle-DSL: replace complex factory system with a much simpler version
author Christian Humer <christian.humer@gmail.com>
date Mon, 29 Dec 2014 23:38:21 +0100
parents
children a665483c3881
comparison
equal deleted inserted replaced
18752:1acaa69ff61b 18753:f6b8787dc113
1 /*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.truffle.dsl.processor.generator;
24
25 import static com.oracle.truffle.dsl.processor.java.ElementUtils.*;
26 import static javax.lang.model.element.Modifier.*;
27
28 import java.util.*;
29
30 import javax.lang.model.element.*;
31 import javax.lang.model.type.*;
32 import javax.lang.model.util.*;
33
34 import com.oracle.truffle.api.dsl.*;
35 import com.oracle.truffle.dsl.processor.*;
36 import com.oracle.truffle.dsl.processor.java.model.*;
37 import com.oracle.truffle.dsl.processor.model.*;
38
39 public class GeneratorUtils {
40
41 public static CodeExecutableElement createConstructorUsingFields(ProcessorContext context, Set<Modifier> modifiers, CodeTypeElement clazz) {
42 CodeExecutableElement method = new CodeExecutableElement(modifiers, null, clazz.getSimpleName().toString());
43 CodeTreeBuilder builder = method.createBuilder();
44 TypeElement superClass = fromTypeMirror(clazz.getSuperclass());
45 ExecutableElement constructor = findConstructor(superClass);
46 if (constructor != null && constructor.getParameters().size() > 0) {
47 builder.startStatement();
48 builder.startSuperCall();
49 for (VariableElement parameter : constructor.getParameters()) {
50 method.addParameter(new CodeVariableElement(parameter.asType(), parameter.getSimpleName().toString()));
51 builder.string(parameter.getSimpleName().toString());
52 }
53 builder.end(); // super
54 builder.end(); // statement
55 }
56
57 for (VariableElement field : clazz.getFields()) {
58 if (field.getModifiers().contains(STATIC)) {
59 continue;
60 }
61 String fieldName = field.getSimpleName().toString();
62 method.addParameter(new CodeVariableElement(field.asType(), fieldName));
63 builder.startStatement();
64 builder.string("this.");
65 builder.string(fieldName);
66 builder.string(" = ");
67 if (isAssignable(field.asType(), context.getTruffleTypes().getNode())) {
68 builder.string("adoptChild(").string(fieldName).string(")");
69 } else {
70 builder.string(fieldName);
71 }
72 builder.end(); // statement
73 }
74
75 return method;
76 }
77
78 private static ExecutableElement findConstructor(TypeElement clazz) {
79 List<ExecutableElement> constructors = ElementFilter.constructorsIn(clazz.getEnclosedElements());
80 if (constructors.isEmpty()) {
81 return null;
82 } else {
83 return constructors.get(0);
84 }
85 }
86
87 public static CodeExecutableElement createSuperConstructor(ProcessorContext context, TypeElement type, ExecutableElement element) {
88 if (element.getModifiers().contains(Modifier.PRIVATE)) {
89 return null;
90 }
91 CodeExecutableElement executable = CodeExecutableElement.clone(context.getEnvironment(), element);
92 executable.setReturnType(null);
93 executable.setSimpleName(CodeNames.of(type.getSimpleName().toString()));
94 CodeTreeBuilder b = executable.createBuilder();
95 b.startStatement();
96 b.startSuperCall();
97 for (VariableElement v : element.getParameters()) {
98 b.string(v.getSimpleName().toString());
99 }
100 b.end();
101 b.end();
102
103 return executable;
104 }
105
106 public static CodeTypeElement createClass(Template model, Set<Modifier> modifiers, String simpleName, TypeMirror superType, boolean enumType) {
107 TypeElement templateType = model.getTemplateType();
108
109 ProcessorContext context = ProcessorContext.getInstance();
110
111 PackageElement pack = context.getEnvironment().getElementUtils().getPackageOf(templateType);
112 CodeTypeElement clazz = new CodeTypeElement(modifiers, enumType ? ElementKind.ENUM : ElementKind.CLASS, pack, simpleName);
113 TypeMirror resolvedSuperType = superType;
114 if (resolvedSuperType == null) {
115 resolvedSuperType = context.getType(Object.class);
116 }
117 clazz.setSuperClass(resolvedSuperType);
118
119 CodeAnnotationMirror generatedByAnnotation = new CodeAnnotationMirror((DeclaredType) context.getType(GeneratedBy.class));
120 generatedByAnnotation.setElementValue(generatedByAnnotation.findExecutableElement("value"), new CodeAnnotationValue(templateType.asType()));
121 if (model.getTemplateMethodName() != null) {
122 generatedByAnnotation.setElementValue(generatedByAnnotation.findExecutableElement("methodName"), new CodeAnnotationValue(model.getTemplateMethodName()));
123 }
124
125 clazz.addAnnotationMirror(generatedByAnnotation);
126 return clazz;
127 }
128
129 }