Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/model/ParameterSpec.java @ 20938:18c0f02fa4d2
Truffle-DSL: make type systems optional.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Tue, 14 Apr 2015 15:12:48 +0200 |
parents | 62c43fcf5be2 |
children | 476374f3fe9a |
comparison
equal
deleted
inserted
replaced
20937:37ea76052733 | 20938:18c0f02fa4d2 |
---|---|
31 import com.oracle.truffle.dsl.processor.model.MethodSpec.TypeDef; | 31 import com.oracle.truffle.dsl.processor.model.MethodSpec.TypeDef; |
32 | 32 |
33 public class ParameterSpec { | 33 public class ParameterSpec { |
34 | 34 |
35 private final String name; | 35 private final String name; |
36 private final List<TypeMirror> allowedTypes; | 36 private final Collection<TypeMirror> allowedTypes; |
37 private final Set<String> allowedTypesIdentifier; | 37 private final boolean anyType; |
38 | 38 |
39 /** Type is bound to local final variable. */ | 39 /** Type is bound to local final variable. */ |
40 private boolean local; | 40 private boolean local; |
41 private boolean signature; | 41 private boolean signature; |
42 private boolean allowSubclasses = true; | |
42 | 43 |
43 /** Optional bound execution of node. */ | 44 /** Optional bound execution of node. */ |
44 private NodeExecutionData execution; | 45 private NodeExecutionData execution; |
45 private TypeDef typeDefinition; | 46 private TypeDef typeDefinition; |
46 | 47 |
47 public ParameterSpec(String name, List<TypeMirror> allowedTypes, Set<String> typeIdentifiers) { | 48 public ParameterSpec(String name, Collection<TypeMirror> allowedTypes) { |
48 this.name = name; | 49 this.name = name; |
49 this.allowedTypes = allowedTypes; | 50 this.allowedTypes = allowedTypes; |
50 this.allowedTypesIdentifier = typeIdentifiers; | 51 boolean anyTypeTemp = false; |
52 for (TypeMirror type : allowedTypes) { | |
53 if (ElementUtils.isObject(type)) { | |
54 anyTypeTemp = true; | |
55 break; | |
56 } | |
57 } | |
58 this.anyType = anyTypeTemp; | |
51 } | 59 } |
52 | 60 |
53 public ParameterSpec(String name, List<TypeMirror> allowedTypes) { | 61 public ParameterSpec(ParameterSpec original, TypeMirror newType) { |
54 this.name = name; | 62 this(original.name, newType); |
55 this.allowedTypes = allowedTypes; | 63 this.local = original.local; |
56 Set<String> typeIdentifiers = new HashSet<>(); | 64 this.signature = original.signature; |
57 for (TypeMirror type : allowedTypes) { | 65 this.execution = original.execution; |
58 typeIdentifiers.add(ElementUtils.getUniqueIdentifier(type)); | 66 this.typeDefinition = original.typeDefinition; |
59 } | 67 this.allowSubclasses = original.allowSubclasses; |
60 this.allowedTypesIdentifier = typeIdentifiers; | |
61 } | 68 } |
62 | 69 |
63 public ParameterSpec(String name, TypeMirror type) { | 70 public ParameterSpec(String name, TypeMirror type) { |
64 this(name, Arrays.asList(type), new HashSet<>(Arrays.asList(ElementUtils.getUniqueIdentifier(type)))); | 71 this(name, Arrays.asList(type)); |
65 } | 72 } |
66 | 73 |
67 public ParameterSpec(ParameterSpec o, List<TypeMirror> allowedTypes, Set<String> typeIdentifiers) { | 74 public void setAllowSubclasses(boolean allowSubclasses) { |
68 this.name = o.name; | 75 this.allowSubclasses = allowSubclasses; |
69 this.local = o.local; | |
70 this.typeDefinition = o.typeDefinition; | |
71 this.execution = o.execution; | |
72 this.signature = o.signature; | |
73 this.allowedTypes = allowedTypes; | |
74 this.allowedTypesIdentifier = typeIdentifiers; | |
75 } | 76 } |
76 | 77 |
77 public NodeExecutionData getExecution() { | 78 public NodeExecutionData getExecution() { |
78 return execution; | 79 return execution; |
79 } | 80 } |
109 | 110 |
110 public String getName() { | 111 public String getName() { |
111 return name; | 112 return name; |
112 } | 113 } |
113 | 114 |
114 public List<TypeMirror> getAllowedTypes() { | 115 public Collection<TypeMirror> getAllowedTypes() { |
115 return allowedTypes; | 116 return allowedTypes; |
116 } | 117 } |
117 | 118 |
118 public boolean matches(VariableElement variable) { | 119 public boolean matches(VariableElement variable) { |
119 if (allowedTypesIdentifier != null) { | 120 if (anyType) { |
120 return allowedTypesIdentifier.contains(ElementUtils.getUniqueIdentifier(variable.asType())); | 121 return true; |
122 } else { | |
123 for (TypeMirror type : allowedTypes) { | |
124 if (ElementUtils.typeEquals(variable.asType(), type)) { | |
125 return true; | |
126 } | |
127 } | |
128 if (allowSubclasses) { | |
129 for (TypeMirror type : allowedTypes) { | |
130 if (ElementUtils.isAssignable(variable.asType(), type)) { | |
131 return true; | |
132 } | |
133 } | |
134 } | |
121 } | 135 } |
122 return true; | 136 return false; |
123 } | 137 } |
124 | 138 |
125 @Override | 139 @Override |
126 public String toString() { | 140 public String toString() { |
127 return toSignatureString(false); | 141 return toSignatureString(false); |
130 public String toSignatureString(boolean typeOnly) { | 144 public String toSignatureString(boolean typeOnly) { |
131 StringBuilder builder = new StringBuilder(); | 145 StringBuilder builder = new StringBuilder(); |
132 if (typeDefinition != null) { | 146 if (typeDefinition != null) { |
133 builder.append("<" + typeDefinition.getName() + ">"); | 147 builder.append("<" + typeDefinition.getName() + ">"); |
134 } else if (getAllowedTypes().size() >= 1) { | 148 } else if (getAllowedTypes().size() >= 1) { |
135 builder.append(ElementUtils.getSimpleName(getAllowedTypes().get(0))); | 149 builder.append(ElementUtils.getSimpleName(getAllowedTypes().iterator().next())); |
136 } else { | 150 } else { |
137 builder.append("void"); | 151 builder.append("void"); |
138 } | 152 } |
139 if (!typeOnly) { | 153 if (!typeOnly) { |
140 builder.append(" "); | 154 builder.append(" "); |