Skip to content

Commit c17869a

Browse files
committed
Cleanup CoreModuleChecks and add CoreModuleProcessor#isSameType()
1 parent f85244c commit c17869a

File tree

2 files changed

+44
-57
lines changed

2 files changed

+44
-57
lines changed

src/processor/java/org/truffleruby/processor/CoreModuleChecks.java

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
import javax.lang.model.element.VariableElement;
1818
import javax.lang.model.type.TypeKind;
1919
import javax.lang.model.type.TypeMirror;
20-
import javax.lang.model.util.Elements;
21-
import javax.lang.model.util.Types;
2220
import javax.tools.Diagnostic;
2321

2422
import com.oracle.truffle.api.dsl.CachedLanguage;
@@ -31,15 +29,15 @@
3129

3230
public class CoreModuleChecks {
3331
static void checks(
34-
CoreModuleProcessor coreModuleProcessor,
32+
CoreModuleProcessor processor,
3533
int[] lowerFixnum,
3634
CoreMethod coreMethod,
3735
TypeElement klass,
3836
boolean hasZeroArgument) {
3937
byte[] lowerArgs = null;
4038

4139
TypeElement klassIt = klass;
42-
while (!coreModuleProcessor.isNodeBaseType(klassIt)) {
40+
while (!processor.isNodeBaseType(klassIt)) {
4341
for (Element el : klassIt.getEnclosedElements()) {
4442
if (!(el instanceof ExecutableElement)) {
4543
continue; // we are interested only in executable elements
@@ -52,25 +50,25 @@ static void checks(
5250
continue; // we are interested only in Specialization methods
5351
}
5452

55-
lowerArgs = checkLowerFixnumArguments(coreModuleProcessor, specializationMethod, lowerArgs);
53+
lowerArgs = checkLowerFixnumArguments(processor, specializationMethod, lowerArgs);
5654
if (coreMethod != null) {
5755
checkAmbiguousOptionalArguments(
58-
coreModuleProcessor,
56+
processor,
5957
coreMethod,
6058
specializationMethod,
6159
specializationAnnotation);
6260
}
6361

6462
}
6563

66-
klassIt = coreModuleProcessor
64+
klassIt = processor
6765
.getProcessingEnvironment()
6866
.getElementUtils()
6967
.getTypeElement(klassIt.getSuperclass().toString());
7068
}
7169

7270
if (lowerArgs == null) {
73-
coreModuleProcessor.getProcessingEnvironment().getMessager().printMessage(
71+
processor.getProcessingEnvironment().getMessager().printMessage(
7472
Diagnostic.Kind.ERROR,
7573
"could not find specializations (lowerArgs == null)",
7674
klass);
@@ -81,7 +79,7 @@ static void checks(
8179
for (int i = 0; i < lowerArgs.length; i++) {
8280
boolean shouldLower = lowerArgs[i] == 0b01; // int without long
8381
if (shouldLower && !contains(lowerFixnum, hasZeroArgument ? i : i + 1)) {
84-
coreModuleProcessor.getProcessingEnvironment().getMessager().printMessage(
82+
processor.getProcessingEnvironment().getMessager().printMessage(
8583
Diagnostic.Kind.ERROR,
8684
"should use lowerFixnum for argument " + (hasZeroArgument ? i : i + 1),
8785
klass);
@@ -90,16 +88,13 @@ static void checks(
9088
}
9189

9290
private static byte[] checkLowerFixnumArguments(
93-
CoreModuleProcessor coreModuleProcessor,
91+
CoreModuleProcessor processor,
9492
ExecutableElement specializationMethod,
9593
byte[] lowerArgs) {
9694
List<? extends VariableElement> parameters = specializationMethod.getParameters();
9795
int start = 0;
9896

99-
if (parameters.size() > 0 &&
100-
coreModuleProcessor.getProcessingEnvironment().getTypeUtils().isSameType(
101-
parameters.get(0).asType(),
102-
coreModuleProcessor.virtualFrameType)) {
97+
if (parameters.size() > 0 && processor.isSameType(parameters.get(0).asType(), processor.virtualFrameType)) {
10398
start++;
10499
}
105100

@@ -141,7 +136,7 @@ private static boolean contains(int[] array, int value) {
141136
}
142137

143138
private static void checkAmbiguousOptionalArguments(
144-
CoreModuleProcessor coreModuleProcessor,
139+
CoreModuleProcessor processor,
145140
CoreMethod coreMethod,
146141
ExecutableElement specializationMethod,
147142
Specialization specializationAnnotation) {
@@ -158,27 +153,27 @@ private static void checkAmbiguousOptionalArguments(
158153

159154
if (coreMethod.needsBlock()) {
160155
if (n < 0) {
161-
coreModuleProcessor.getProcessingEnvironment().getMessager().printMessage(
156+
processor.getProcessingEnvironment().getMessager().printMessage(
162157
Diagnostic.Kind.ERROR,
163158
"invalid block method parameter position for",
164159
specializationMethod);
165160
return;
166161
}
167-
isParameterBlock(coreModuleProcessor, parameters.get(n));
162+
isParameterBlock(processor, parameters.get(n));
168163
n--; // Ignore block argument.
169164
}
170165

171166
if (coreMethod.rest()) {
172167
if (n < 0) {
173-
coreModuleProcessor.getProcessingEnvironment().getMessager().printMessage(
168+
processor.getProcessingEnvironment().getMessager().printMessage(
174169
Diagnostic.Kind.ERROR,
175170
"missing rest method parameter",
176171
specializationMethod);
177172
return;
178173
}
179174

180175
if (parameters.get(n).asType().getKind() != TypeKind.ARRAY) {
181-
coreModuleProcessor.getProcessingEnvironment().getMessager().printMessage(
176+
processor.getProcessingEnvironment().getMessager().printMessage(
182177
Diagnostic.Kind.ERROR,
183178
"rest method parameter is not array",
184179
parameters.get(n));
@@ -189,18 +184,18 @@ private static void checkAmbiguousOptionalArguments(
189184

190185
for (int i = 0; i < coreMethod.optional(); i++, n--) {
191186
if (n < 0) {
192-
coreModuleProcessor.getProcessingEnvironment().getMessager().printMessage(
187+
processor.getProcessingEnvironment().getMessager().printMessage(
193188
Diagnostic.Kind.ERROR,
194189
"invalid optional parameter count for",
195190
specializationMethod);
196191
continue;
197192
}
198-
isParameterUnguarded(coreModuleProcessor, specializationAnnotation, parameters.get(n));
193+
isParameterUnguarded(processor, specializationAnnotation, parameters.get(n));
199194
}
200195
}
201196

202197
private static void isParameterUnguarded(
203-
CoreModuleProcessor coreModuleProcessor,
198+
CoreModuleProcessor processor,
204199
Specialization specializationAnnotation,
205200
VariableElement parameter) {
206201
String name = parameter.getSimpleName().toString();
@@ -215,13 +210,13 @@ private static void isParameterUnguarded(
215210
// it clear in the parameter name (by using unused or maybe prefix) that it may not have been
216211
// provided or is not used.
217212

218-
if (coreModuleProcessor.getProcessingEnvironment().getTypeUtils().isSameType(
213+
if (processor.isSameType(
219214
parameter.asType(),
220-
coreModuleProcessor.objectType) &&
215+
processor.objectType) &&
221216
!name.startsWith("unused") &&
222217
!name.startsWith("maybe") &&
223218
!isGuarded(name, specializationAnnotation.guards())) {
224-
coreModuleProcessor.getProcessingEnvironment().getMessager().printMessage(
219+
processor.getProcessingEnvironment().getMessager().printMessage(
225220
Diagnostic.Kind.ERROR,
226221
"Since Object is the super type of NotProvided any optional parameter declaration of type Object " +
227222
"must have additional guards to check whether this specialization should be called, " +
@@ -243,20 +238,13 @@ private static boolean isGuarded(String name, String[] guards) {
243238
return false;
244239
}
245240

246-
private static void isParameterBlock(
247-
CoreModuleProcessor coreModuleProcessor,
248-
VariableElement parameter) {
249-
TypeMirror blockType = parameter.asType();
250-
Types typeUtils = coreModuleProcessor.getProcessingEnvironment().getTypeUtils();
251-
Elements elementUtils = coreModuleProcessor.getProcessingEnvironment().getElementUtils();
252-
boolean isNil = typeUtils
253-
.isSameType(blockType, elementUtils.getTypeElement("org.truffleruby.language.Nil").asType());
254-
boolean isRubyProc = typeUtils
255-
.isSameType(blockType, elementUtils.getTypeElement("org.truffleruby.core.proc.RubyProc").asType());
256-
boolean isObject = typeUtils.isSameType(blockType, coreModuleProcessor.objectType);
241+
private static void isParameterBlock(CoreModuleProcessor processor, VariableElement parameter) {
242+
final TypeMirror blockType = parameter.asType();
257243

258-
if (!(isNil || isRubyProc || isObject)) {
259-
coreModuleProcessor.getProcessingEnvironment().getMessager().printMessage(
244+
if (!(processor.isSameType(blockType, processor.nilType) ||
245+
processor.isSameType(blockType, processor.rubyProcType) ||
246+
processor.isSameType(blockType, processor.objectType))) {
247+
processor.getProcessingEnvironment().getMessager().printMessage(
260248
Diagnostic.Kind.ERROR,
261249
"A block parameter must be of type Nil, RubyProc or Object.",
262250
parameter);

src/processor/java/org/truffleruby/processor/CoreModuleProcessor.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import javax.lang.model.element.TypeElement;
3131
import javax.lang.model.element.VariableElement;
3232
import javax.lang.model.type.TypeMirror;
33+
import javax.lang.model.util.Elements;
3334
import javax.tools.Diagnostic.Kind;
3435
import javax.tools.FileObject;
3536
import javax.tools.JavaFileObject;
@@ -92,8 +93,11 @@ ProcessingEnvironment getProcessingEnvironment() {
9293
}
9394

9495
private final Set<String> processed = new HashSet<>();
96+
9597
TypeMirror virtualFrameType;
9698
TypeMirror objectType;
99+
TypeMirror nilType;
100+
TypeMirror rubyProcType;
97101
TypeMirror rubyNodeType;
98102
TypeMirror rubyBaseNodeType;
99103

@@ -104,22 +108,13 @@ public SourceVersion getSupportedSourceVersion() {
104108

105109
@Override
106110
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) {
107-
virtualFrameType = processingEnv
108-
.getElementUtils()
109-
.getTypeElement("com.oracle.truffle.api.frame.VirtualFrame")
110-
.asType();
111-
objectType = processingEnv
112-
.getElementUtils()
113-
.getTypeElement("java.lang.Object")
114-
.asType();
115-
rubyNodeType = processingEnv
116-
.getElementUtils()
117-
.getTypeElement("org.truffleruby.language.RubyNode")
118-
.asType();
119-
rubyBaseNodeType = processingEnv
120-
.getElementUtils()
121-
.getTypeElement("org.truffleruby.language.RubyBaseNode")
122-
.asType();
111+
Elements elementUtils = processingEnv.getElementUtils();
112+
virtualFrameType = elementUtils.getTypeElement("com.oracle.truffle.api.frame.VirtualFrame").asType();
113+
objectType = elementUtils.getTypeElement("java.lang.Object").asType();
114+
nilType = elementUtils.getTypeElement("org.truffleruby.language.Nil").asType();
115+
rubyProcType = elementUtils.getTypeElement("org.truffleruby.core.proc.RubyProc").asType();
116+
rubyNodeType = elementUtils.getTypeElement("org.truffleruby.language.RubyNode").asType();
117+
rubyBaseNodeType = elementUtils.getTypeElement("org.truffleruby.language.RubyBaseNode").asType();
123118

124119
if (!annotations.isEmpty()) {
125120
for (Element element : roundEnvironment.getElementsAnnotatedWith(CoreModule.class)) {
@@ -134,6 +129,10 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
134129
return true;
135130
}
136131

132+
boolean isSameType(TypeMirror type1, TypeMirror type2) {
133+
return processingEnv.getTypeUtils().isSameType(type1, type2);
134+
}
135+
137136
private void processCoreModule(TypeElement coreModuleElement) throws IOException {
138137
final CoreModule coreModule = coreModuleElement.getAnnotation(CoreModule.class);
139138

@@ -430,7 +429,7 @@ private List<String> getArgumentNamesFromSpecializations(TypeElement klass, bool
430429
continue; // we ignore arguments having annotations like @Cached
431430
}
432431

433-
if (processingEnv.getTypeUtils().isSameType(parameter.asType(), virtualFrameType)) {
432+
if (isSameType(parameter.asType(), virtualFrameType)) {
434433
continue;
435434
}
436435

@@ -473,8 +472,8 @@ private List<String> getArgumentNamesFromSpecializations(TypeElement klass, bool
473472
}
474473

475474
public boolean isNodeBaseType(TypeElement typeElement) {
476-
return processingEnv.getTypeUtils().isSameType(typeElement.asType(), rubyNodeType) ||
477-
processingEnv.getTypeUtils().isSameType(typeElement.asType(), rubyBaseNodeType);
475+
return isSameType(typeElement.asType(), rubyNodeType) ||
476+
isSameType(typeElement.asType(), rubyBaseNodeType);
478477
}
479478

480479
private boolean anyCoreMethod(List<? extends Element> enclosedElements) {

0 commit comments

Comments
 (0)