comparison truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/model/ParameterSpec.java @ 21951:9c8c0937da41

Moving all sources into truffle subdirectory
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 17 Jun 2015 10:58:08 +0200
parents graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/model/ParameterSpec.java@6361fa2e3321
children dc83cc1f94f2
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
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.model;
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.java.*;
32 import com.oracle.truffle.dsl.processor.model.MethodSpec.TypeDef;
33
34 public class ParameterSpec {
35
36 private final String name;
37 private final Collection<TypeMirror> allowedTypes;
38 private final boolean anyType;
39
40 /** Type is bound to local final variable. */
41 private boolean local;
42 private boolean signature;
43 private boolean allowSubclasses = true;
44
45 /** Optional bound execution of node. */
46 private NodeExecutionData execution;
47 private TypeDef typeDefinition;
48
49 public ParameterSpec(String name, Collection<TypeMirror> allowedTypes) {
50 this.name = name;
51 this.allowedTypes = allowedTypes;
52 boolean anyTypeTemp = false;
53 for (TypeMirror type : allowedTypes) {
54 if (ElementUtils.isObject(type)) {
55 anyTypeTemp = true;
56 break;
57 }
58 }
59 this.anyType = anyTypeTemp;
60 }
61
62 public boolean isAnnotated() {
63 return false;
64 }
65
66 public ParameterSpec(ParameterSpec original, TypeMirror newType) {
67 this(original.name, newType);
68 this.local = original.local;
69 this.signature = original.signature;
70 this.execution = original.execution;
71 this.typeDefinition = original.typeDefinition;
72 this.allowSubclasses = original.allowSubclasses;
73 }
74
75 public ParameterSpec(String name, TypeMirror type) {
76 this(name, Arrays.asList(type));
77 }
78
79 public void setAllowSubclasses(boolean allowSubclasses) {
80 this.allowSubclasses = allowSubclasses;
81 }
82
83 public NodeExecutionData getExecution() {
84 return execution;
85 }
86
87 public void setExecution(NodeExecutionData executionData) {
88 this.execution = executionData;
89 this.signature = execution != null;
90 }
91
92 public void setSignature(boolean signature) {
93 this.signature = signature;
94 }
95
96 void setTypeDefinition(TypeDef typeDefinition) {
97 this.typeDefinition = typeDefinition;
98 }
99
100 TypeDef getTypeDefinition() {
101 return typeDefinition;
102 }
103
104 public void setLocal(boolean local) {
105 this.local = local;
106 }
107
108 public boolean isSignature() {
109 return signature;
110 }
111
112 public boolean isLocal() {
113 return local;
114 }
115
116 public String getName() {
117 return name;
118 }
119
120 public Collection<TypeMirror> getAllowedTypes() {
121 return allowedTypes;
122 }
123
124 public boolean matches(VariableElement variable) {
125 if (anyType) {
126 return true;
127 } else {
128 for (TypeMirror type : allowedTypes) {
129 if (ElementUtils.typeEquals(variable.asType(), type)) {
130 return true;
131 }
132 }
133 if (allowSubclasses) {
134 for (TypeMirror type : allowedTypes) {
135 if (ElementUtils.isSubtypeBoxed(ProcessorContext.getInstance(), variable.asType(), type)) {
136 return true;
137 }
138 }
139 }
140 }
141 return false;
142 }
143
144 @Override
145 public String toString() {
146 return toSignatureString(false);
147 }
148
149 public String toSignatureString(boolean typeOnly) {
150 StringBuilder builder = new StringBuilder();
151 if (typeDefinition != null) {
152 builder.append("<" + typeDefinition.getName() + ">");
153 } else if (getAllowedTypes().size() >= 1) {
154 builder.append(ElementUtils.getSimpleName(getAllowedTypes().iterator().next()));
155 } else {
156 builder.append("void");
157 }
158 if (!typeOnly) {
159 builder.append(" ");
160 builder.append(getName());
161 }
162 return builder.toString();
163 }
164
165 }