Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/ast/CodeVariableElement.java @ 10597:79041ab43660
Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 01 Jul 2013 20:58:32 +0200 |
parents | |
children | 0cd0bdedd4ad |
comparison
equal
deleted
inserted
replaced
10596:f43eb2f1bbbc | 10597:79041ab43660 |
---|---|
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.ast; | |
24 | |
25 import java.util.*; | |
26 | |
27 import javax.lang.model.element.*; | |
28 import javax.lang.model.type.*; | |
29 | |
30 import com.oracle.truffle.dsl.processor.*; | |
31 import com.oracle.truffle.dsl.processor.api.element.*; | |
32 | |
33 public final class CodeVariableElement extends CodeElement<Element> implements WritableVariableElement { | |
34 | |
35 private Name name; | |
36 private TypeMirror type; | |
37 private Object constantValue; | |
38 | |
39 private CodeTree init; | |
40 | |
41 public CodeVariableElement(TypeMirror type, String name) { | |
42 super(Utils.modifiers()); | |
43 this.type = type; | |
44 this.name = CodeNames.of(name); | |
45 } | |
46 | |
47 public CodeVariableElement(Set<Modifier> modifiers, TypeMirror type, String name) { | |
48 super(modifiers); | |
49 this.type = type; | |
50 this.name = CodeNames.of(name); | |
51 } | |
52 | |
53 public CodeVariableElement(Set<Modifier> modifiers, TypeMirror type, String name, String init) { | |
54 this(modifiers, type, name); | |
55 if (init != null) { | |
56 this.init = new CodeTree(CodeTreeKind.STRING, null, init); | |
57 } | |
58 } | |
59 | |
60 public CodeTreeBuilder createInitBuilder() { | |
61 CodeTreeBuilder builder = new CodeTreeBuilder(null); | |
62 init = builder.getTree(); | |
63 init.setEnclosingElement(this); | |
64 return builder; | |
65 } | |
66 | |
67 public void setInit(CodeTree init) { | |
68 this.init = init; | |
69 } | |
70 | |
71 public CodeTree getInit() { | |
72 return init; | |
73 } | |
74 | |
75 public Name getSimpleName() { | |
76 return name; | |
77 } | |
78 | |
79 public TypeMirror getType() { | |
80 return type; | |
81 } | |
82 | |
83 @Override | |
84 public TypeMirror asType() { | |
85 return type; | |
86 } | |
87 | |
88 @Override | |
89 public ElementKind getKind() { | |
90 if (getEnclosingElement() instanceof ExecutableElement) { | |
91 return ElementKind.PARAMETER; | |
92 } else if (getEnclosingElement() instanceof TypeElement) { | |
93 return ElementKind.FIELD; | |
94 } else { | |
95 return ElementKind.PARAMETER; | |
96 } | |
97 } | |
98 | |
99 public void setConstantValue(Object constantValue) { | |
100 this.constantValue = constantValue; | |
101 } | |
102 | |
103 @Override | |
104 public Object getConstantValue() { | |
105 return constantValue; | |
106 } | |
107 | |
108 public String getName() { | |
109 return getSimpleName().toString(); | |
110 } | |
111 | |
112 @Override | |
113 public void setSimpleName(Name name) { | |
114 this.name = name; | |
115 } | |
116 | |
117 public void setName(String name) { | |
118 this.name = CodeNames.of(name); | |
119 } | |
120 | |
121 @Override | |
122 public void setType(TypeMirror type) { | |
123 this.type = type; | |
124 } | |
125 | |
126 @Override | |
127 public <R, P> R accept(ElementVisitor<R, P> v, P p) { | |
128 return v.visitVariable(this, p); | |
129 } | |
130 | |
131 public static CodeVariableElement clone(VariableElement var) { | |
132 CodeVariableElement copy = new CodeVariableElement(var.getModifiers(), var.asType(), var.getSimpleName().toString()); | |
133 copy.setConstantValue(var.getConstantValue()); | |
134 for (AnnotationMirror mirror : var.getAnnotationMirrors()) { | |
135 copy.addAnnotationMirror(mirror); | |
136 } | |
137 for (Element element : var.getEnclosedElements()) { | |
138 copy.add(element); | |
139 } | |
140 return copy; | |
141 } | |
142 | |
143 } |