9
9
*/
10
10
package org .truffleruby .processor ;
11
11
12
+ import java .util .ArrayList ;
12
13
import java .util .List ;
13
14
14
15
import javax .lang .model .element .Element ;
@@ -34,6 +35,7 @@ static void checks(
34
35
TypeElement klass ,
35
36
boolean hasZeroArgument ) {
36
37
byte [] lowerArgs = null ;
38
+ List <ExecutableElement > specializationMethods = new ArrayList <>();
37
39
38
40
TypeElement klassIt = klass ;
39
41
while (!processor .isNodeBaseType (klassIt )) {
@@ -48,6 +50,7 @@ static void checks(
48
50
if (specializationAnnotation == null ) {
49
51
continue ; // we are interested only in Specialization methods
50
52
}
53
+ specializationMethods .add (specializationMethod );
51
54
52
55
lowerArgs = checkLowerFixnumArguments (processor , specializationMethod , lowerArgs );
53
56
if (coreMethod != null ) {
@@ -66,11 +69,15 @@ static void checks(
66
69
.getTypeElement (klassIt .getSuperclass ().toString ());
67
70
}
68
71
69
- if (lowerArgs == null ) {
70
- processor .error ("could not find specializations (lowerArgs == null) " , klass );
72
+ if (specializationMethods . isEmpty () ) {
73
+ processor .error ("could not find specializations" , klass );
71
74
return ;
72
75
}
73
76
77
+ if (coreMethod == null ) { // @Primitive
78
+ checkPrimitiveArguments (processor , specializationMethods );
79
+ }
80
+
74
81
// Verify against the lowerFixnum annotation
75
82
for (int i = 0 ; i < lowerArgs .length ; i ++) {
76
83
boolean shouldLower = lowerArgs [i ] == 0b01; // int without long
@@ -134,15 +141,7 @@ private static void checkAmbiguousOptionalArguments(
134
141
ExecutableElement specializationMethod ,
135
142
Specialization specializationAnnotation ) {
136
143
List <? extends VariableElement > parameters = specializationMethod .getParameters ();
137
- int n = parameters .size () - 1 ;
138
- // Ignore all the @Cached methods from our consideration.
139
- while (n >= 0 &&
140
- (parameters .get (n ).getAnnotation (Cached .class ) != null ||
141
- parameters .get (n ).getAnnotation (CachedLibrary .class ) != null ||
142
- parameters .get (n ).getAnnotation (CachedContext .class ) != null ||
143
- parameters .get (n ).getAnnotation (CachedLanguage .class ) != null )) {
144
- n --;
145
- }
144
+ int n = getLastParameterIndex (parameters );
146
145
147
146
if (coreMethod .needsBlock ()) {
148
147
if (n < 0 ) {
@@ -175,6 +174,46 @@ private static void checkAmbiguousOptionalArguments(
175
174
}
176
175
}
177
176
177
+ private static void checkPrimitiveArguments (
178
+ CoreModuleProcessor processor ,
179
+ List <ExecutableElement > specializationMethods ) {
180
+ boolean hasRubyProcLastArgument = false ;
181
+ for (ExecutableElement specialization : specializationMethods ) {
182
+ List <? extends VariableElement > parameters = specialization .getParameters ();
183
+ int last = getLastParameterIndex (parameters );
184
+ if (last >= 0 && processor .isSameType (parameters .get (last ).asType (), processor .rubyProcType )) {
185
+ hasRubyProcLastArgument = true ;
186
+ break ;
187
+ }
188
+ }
189
+
190
+ if (hasRubyProcLastArgument ) {
191
+ for (ExecutableElement specialization : specializationMethods ) {
192
+ List <? extends VariableElement > parameters = specialization .getParameters ();
193
+ int last = getLastParameterIndex (parameters );
194
+ VariableElement parameter = parameters .get (last );
195
+ if (processor .isSameType (parameter .asType (), processor .notProvidedType )) {
196
+ processor .error (
197
+ "The last primitive parameter should not be NotProvided if it can be a RubyProc" ,
198
+ parameter );
199
+ }
200
+ }
201
+ }
202
+ }
203
+
204
+ private static int getLastParameterIndex (List <? extends VariableElement > parameters ) {
205
+ int n = parameters .size () - 1 ;
206
+ // Ignore all the @Cached methods from our consideration.
207
+ while (n >= 0 &&
208
+ (parameters .get (n ).getAnnotation (Cached .class ) != null ||
209
+ parameters .get (n ).getAnnotation (CachedLibrary .class ) != null ||
210
+ parameters .get (n ).getAnnotation (CachedContext .class ) != null ||
211
+ parameters .get (n ).getAnnotation (CachedLanguage .class ) != null )) {
212
+ n --;
213
+ }
214
+ return n ;
215
+ }
216
+
178
217
private static void isParameterUnguarded (
179
218
CoreModuleProcessor processor ,
180
219
Specialization specializationAnnotation ,
0 commit comments