Skip to content

Commit dd1f91d

Browse files
committed
Implement primitive overload selection if argument types are exact.
1 parent 8ca0fdd commit dd1f91d

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

truffle/src/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/MethodGuardIdempotentTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,33 @@ public String s1(TestObject arg) {
329329

330330
}
331331

332+
@SuppressWarnings("unused")
333+
abstract static class MethodOverloadIdempotentNode extends SlowPathListenerNode {
334+
335+
static final TestObject VALUE = null;
336+
337+
abstract String execute(TestObject arg);
338+
339+
// no warning here: guard(TestObject arg) should get selected by overload.
340+
@Specialization(guards = {"guard(VALUE)"})
341+
public String s0(TestObject arg) {
342+
return "s0";
343+
}
344+
345+
public static boolean guard(Object arg) {
346+
return arg == null;
347+
}
348+
349+
@NonIdempotent
350+
public static boolean guard(TestObject arg) {
351+
return arg == null;
352+
}
353+
354+
@Specialization
355+
public String s1(TestObject arg) {
356+
return "s1";
357+
}
358+
359+
}
360+
332361
}

truffle/src/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/expression/DSLExpressionResolver.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,28 @@ public ExecutableElement lookupMethod(String searchName, List<TypeMirror> search
188188
lazyProcess();
189189
List<ExecutableElement> methodsWithName = this.methods.get(searchName);
190190
ExecutableElement foundWithName = null;
191+
ExecutableElement found = null;
192+
List<ExecutableElement> overloads = null;
191193
if (methodsWithName != null) {
192194
for (ExecutableElement method : methodsWithName) {
193195
if (matchExecutable(searchName, searchParameters, method) && ElementUtils.isVisible(accessType, method)) {
194-
return method;
196+
if (found == null) {
197+
found = method;
198+
} else {
199+
if (overloads == null) {
200+
overloads = new ArrayList<>();
201+
overloads.add(found);
202+
}
203+
overloads.add(method);
204+
}
195205
}
196206
foundWithName = method;
197207
}
208+
if (overloads != null) {
209+
return selectOverload(overloads, searchParameters);
210+
} else if (found != null) {
211+
return found;
212+
}
198213
}
199214
if (parent != null) {
200215
ExecutableElement parentResult = parent.lookupMethod(searchName, searchParameters);
@@ -238,6 +253,20 @@ public static boolean matchExecutable(String name, List<TypeMirror> searchParame
238253
return true;
239254
}
240255

256+
private static ExecutableElement selectOverload(List<ExecutableElement> overloads, List<TypeMirror> searchParameters) {
257+
method: for (ExecutableElement method : overloads) {
258+
for (int i = 0; i < searchParameters.size(); i++) {
259+
TypeMirror searchParameter = searchParameters.get(i);
260+
TypeMirror actualParameter = method.getParameters().get(i).asType();
261+
if (!ElementUtils.typeEquals(searchParameter, actualParameter)) {
262+
continue method;
263+
}
264+
}
265+
return method;
266+
}
267+
return overloads.get(0);
268+
}
269+
241270
private VariableElement resolveVariable(Variable variable) {
242271
final String name = variable.getName();
243272

0 commit comments

Comments
 (0)