comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/generator/AbstractClassElementFactory.java @ 16759:23415229349b

Truffle-DSL: new package structure.
author Christian Humer <christian.humer@gmail.com>
date Mon, 11 Aug 2014 15:57:14 +0200
parents graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/template/ClassElementFactory.java@bd28da642eea
children 0370880ac9ce
comparison
equal deleted inserted replaced
16758:c5f8eeb3cbc8 16759:23415229349b
1 /*
2 * Copyright (c) 2012, 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.java.model.*;
36 import com.oracle.truffle.dsl.processor.model.*;
37
38 public abstract class AbstractClassElementFactory<M> extends AbstractCodeElementFactory<M> {
39
40 @Override
41 protected abstract CodeTypeElement create(M m);
42
43 @Override
44 public CodeTypeElement getElement() {
45 return (CodeTypeElement) super.getElement();
46 }
47
48 protected CodeExecutableElement createConstructorUsingFields(Set<Modifier> modifiers, CodeTypeElement clazz) {
49 CodeExecutableElement method = new CodeExecutableElement(modifiers, null, clazz.getSimpleName().toString());
50 CodeTreeBuilder builder = method.createBuilder();
51 TypeElement superClass = fromTypeMirror(clazz.getSuperclass());
52 ExecutableElement constructor = findConstructor(superClass);
53 if (constructor != null && constructor.getParameters().size() > 0) {
54 builder.startStatement();
55 builder.startSuperCall();
56 for (VariableElement parameter : constructor.getParameters()) {
57 method.addParameter(new CodeVariableElement(parameter.asType(), parameter.getSimpleName().toString()));
58 builder.string(parameter.getSimpleName().toString());
59 }
60 builder.end(); // super
61 builder.end(); // statement
62 }
63
64 for (VariableElement field : clazz.getFields()) {
65 if (field.getModifiers().contains(STATIC)) {
66 continue;
67 }
68 String fieldName = field.getSimpleName().toString();
69 method.addParameter(new CodeVariableElement(field.asType(), fieldName));
70 builder.startStatement();
71 builder.string("this.");
72 builder.string(fieldName);
73 builder.string(" = ");
74 if (isAssignable(field.asType(), getContext().getTruffleTypes().getNode())) {
75 builder.string("adoptChild(").string(fieldName).string(")");
76 } else {
77 builder.string(fieldName);
78 }
79 builder.end(); // statement
80 }
81
82 return method;
83 }
84
85 private static ExecutableElement findConstructor(TypeElement clazz) {
86 List<ExecutableElement> constructors = ElementFilter.constructorsIn(clazz.getEnclosedElements());
87 if (constructors.isEmpty()) {
88 return null;
89 } else {
90 return constructors.get(0);
91 }
92 }
93
94 protected CodeExecutableElement createSuperConstructor(TypeElement type, ExecutableElement element) {
95 if (element.getModifiers().contains(Modifier.PRIVATE)) {
96 return null;
97 }
98 CodeExecutableElement executable = CodeExecutableElement.clone(getContext().getEnvironment(), element);
99 executable.setReturnType(null);
100 executable.setSimpleName(CodeNames.of(type.getSimpleName().toString()));
101 CodeTreeBuilder b = executable.createBuilder();
102 b.startStatement();
103 b.startSuperCall();
104 for (VariableElement v : element.getParameters()) {
105 b.string(v.getSimpleName().toString());
106 }
107 b.end();
108 b.end();
109
110 return executable;
111 }
112
113 protected CodeTypeElement createClass(Template model, Set<Modifier> modifiers, String simpleName, TypeMirror superType, boolean enumType) {
114 TypeElement templateType = model.getTemplateType();
115
116 PackageElement pack = getContext().getEnvironment().getElementUtils().getPackageOf(templateType);
117 CodeTypeElement clazz = new CodeTypeElement(modifiers, enumType ? ElementKind.ENUM : ElementKind.CLASS, pack, simpleName);
118 TypeMirror resolvedSuperType = superType;
119 if (resolvedSuperType == null) {
120 resolvedSuperType = getContext().getType(Object.class);
121 }
122 clazz.setSuperClass(resolvedSuperType);
123
124 CodeAnnotationMirror generatedByAnnotation = new CodeAnnotationMirror((DeclaredType) getContext().getType(GeneratedBy.class));
125 generatedByAnnotation.setElementValue(generatedByAnnotation.findExecutableElement("value"), new CodeAnnotationValue(templateType.asType()));
126 if (model.getTemplateMethodName() != null) {
127 generatedByAnnotation.setElementValue(generatedByAnnotation.findExecutableElement("methodName"), new CodeAnnotationValue(model.getTemplateMethodName()));
128 }
129
130 clazz.addAnnotationMirror(generatedByAnnotation);
131
132 return clazz;
133 }
134 }