Skip to content

Commit eec579c

Browse files
committed
Refactoring of ArgumentClinicData creation
1 parent e095571 commit eec579c

File tree

2 files changed

+67
-55
lines changed

2 files changed

+67
-55
lines changed

graalpython/com.oracle.graal.python.processor/src/com/oracle/graal/python/processor/ArgumentClinicModel.java

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -118,59 +118,67 @@ private ArgumentClinicData(ArgumentClinic annotation, int index, Set<PrimitiveTy
118118
this.imports = imports;
119119
}
120120

121-
public static ArgumentClinicData create(ArgumentClinic annotation, TypeElement type, BuiltinAnnotation builtinAnnotation, int index, ConverterFactory factory) throws ProcessingError {
122-
if (annotation == null) {
123-
return new ArgumentClinicData(null, index, new HashSet<>(Arrays.asList(PrimitiveType.values())), null, Collections.emptySet());
124-
}
121+
private static ConverterFactory getFactory(ArgumentClinic annotation, TypeElement type, ConverterFactory factory) throws ProcessingError {
125122
if (factory == null && annotation.args().length != 0) {
126123
throw new ProcessingError(type, "No conversionClass specified but arguments were provided");
127124
}
128-
PrimitiveType[] acceptedPrimitives = new PrimitiveType[0];
129-
String castNodeFactory;
130-
Set<String> imports = new HashSet<>();
131-
if (annotation.customConversion().isEmpty()) {
132-
if (factory == null) {
133-
if (annotation.conversion() == ClinicConversion.None && annotation.defaultValue().isEmpty()) {
134-
throw new ProcessingError(type, "ArgumentClinic annotation must declare either builtin conversion or custom conversion.");
135-
}
136-
factory = ConverterFactory.getBuiltin(annotation);
137-
}
138-
if (annotation.args().length != factory.paramCount) {
139-
throw new ProcessingError(type, "Conversion %s.%s expects %d arguments", factory.fullClassName, factory.methodName, factory.paramCount);
140-
}
141-
String args = Stream.concat(
142-
Arrays.stream(factory.clinicArgs).map(ca -> {
143-
switch (ca) {
144-
case BuiltinName:
145-
return String.format("\"%s\"", builtinAnnotation.name);
146-
case ArgumentIndex:
147-
return String.valueOf(index);
148-
case ArgumentName:
149-
return String.format("\"%s\"", builtinAnnotation.argumentNames[index]);
150-
case DefaultValue:
151-
return annotation.defaultValue();
152-
case UseDefaultForNone:
153-
return String.valueOf(annotation.useDefaultForNone());
154-
default:
155-
throw new IllegalStateException("Unsupported ClinicArgument: " + ca);
156-
}
157-
}),
158-
Arrays.stream(annotation.args())).collect(Collectors.joining(", "));
159-
castNodeFactory = String.format("%s.%s(%s)", factory.className, factory.methodName, args);
160-
imports.add(factory.fullClassName);
161-
acceptedPrimitives = factory.acceptedPrimitiveTypes;
162-
if (annotation.defaultValue().startsWith("PNone.")) {
163-
imports.add("com.oracle.graal.python.builtins.objects.PNone");
164-
}
165-
} else {
125+
if (!annotation.customConversion().isEmpty()) {
166126
if (factory != null) {
167127
throw new ProcessingError(type, "Cannot specify both conversionClass and customConversion");
168128
}
169-
castNodeFactory = type.getQualifiedName().toString() + '.' + annotation.customConversion() + "()";
129+
return ConverterFactory.forCustomConversion(type, annotation.customConversion());
130+
}
131+
if (factory != null) {
132+
return factory;
170133
}
134+
if (annotation.conversion() == ClinicConversion.None && annotation.defaultValue().isEmpty()) {
135+
throw new ProcessingError(type, "ArgumentClinic annotation must declare either builtin conversion or custom conversion.");
136+
}
137+
return ConverterFactory.getBuiltin(annotation);
138+
}
139+
140+
public static ArgumentClinicData create(ArgumentClinic annotation, TypeElement type, BuiltinAnnotation builtinAnnotation, int index, ConverterFactory ofactory) throws ProcessingError {
141+
if (annotation == null) {
142+
return new ArgumentClinicData(null, index, new HashSet<>(Arrays.asList(PrimitiveType.values())), null, Collections.emptySet());
143+
}
144+
ConverterFactory factory = getFactory(annotation, type, ofactory);
145+
if (annotation.args().length != factory.paramCount) {
146+
throw new ProcessingError(type, "Conversion %s.%s expects %d arguments", factory.fullClassName, factory.methodName, factory.paramCount);
147+
}
148+
149+
PrimitiveType[] acceptedPrimitives;
171150
if (annotation.shortCircuitPrimitive().length > 0) {
172151
acceptedPrimitives = annotation.shortCircuitPrimitive();
152+
} else {
153+
acceptedPrimitives = factory.acceptedPrimitiveTypes;
154+
}
155+
156+
String args = Stream.concat(
157+
Arrays.stream(factory.clinicArgs).map(ca -> {
158+
switch (ca) {
159+
case BuiltinName:
160+
return String.format("\"%s\"", builtinAnnotation.name);
161+
case ArgumentIndex:
162+
return String.valueOf(index);
163+
case ArgumentName:
164+
return String.format("\"%s\"", builtinAnnotation.argumentNames[index]);
165+
case DefaultValue:
166+
return annotation.defaultValue();
167+
case UseDefaultForNone:
168+
return String.valueOf(annotation.useDefaultForNone());
169+
default:
170+
throw new IllegalStateException("Unsupported ClinicArgument: " + ca);
171+
}
172+
}),
173+
Arrays.stream(annotation.args())).collect(Collectors.joining(", "));
174+
String castNodeFactory = String.format("%s.%s(%s)", factory.className, factory.methodName, args);
175+
176+
Set<String> imports = new HashSet<>();
177+
imports.add(factory.fullClassName);
178+
if (annotation.defaultValue().startsWith("PNone.")) {
179+
imports.add("com.oracle.graal.python.builtins.objects.PNone");
173180
}
181+
174182
return new ArgumentClinicData(annotation, index, new HashSet<>(Arrays.asList(acceptedPrimitives)), castNodeFactory, imports);
175183
}
176184
}

graalpython/com.oracle.graal.python.processor/src/com/oracle/graal/python/processor/ConverterFactory.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,17 @@ public class ConverterFactory {
6464
public final ClinicArgument[] clinicArgs;
6565
public final PrimitiveType[] acceptedPrimitiveTypes;
6666

67-
private ConverterFactory(ExecutableElement method, ClinicArgument[] clinicArgs, PrimitiveType[] acceptedPrimitiveTypes) {
68-
fullClassName = method.getEnclosingElement().toString();
69-
className = fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
70-
methodName = method.getSimpleName().toString();
71-
paramCount = method.getParameters().size() - clinicArgs.length;
67+
private ConverterFactory(String fullClassName, String className, String methodName, int paramCount, ClinicArgument[] clinicArgs, PrimitiveType[] acceptedPrimitiveTypes) {
68+
this.fullClassName = fullClassName;
69+
this.className = className;
70+
this.methodName = methodName;
71+
this.paramCount = paramCount;
7272
this.clinicArgs = clinicArgs;
7373
this.acceptedPrimitiveTypes = acceptedPrimitiveTypes;
7474
}
7575

7676
private ConverterFactory(String className, ClinicArgument[] clinicArgs, PrimitiveType[] acceptedPrimitiveTypes) {
77-
this.fullClassName = CLINIC_PACKAGE + "." + className;
78-
this.className = className;
79-
this.methodName = "create";
80-
this.paramCount = 0;
81-
this.clinicArgs = clinicArgs;
82-
this.acceptedPrimitiveTypes = acceptedPrimitiveTypes;
77+
this(CLINIC_PACKAGE + "." + className, className, "create", 0, clinicArgs, acceptedPrimitiveTypes);
8378
}
8479

8580
private static final ConverterFactory BuiltinBoolean = new ConverterFactory("JavaBooleanConvertorNodeGen",
@@ -150,7 +145,11 @@ public static ConverterFactory getForClass(TypeElement conversionClass) throws P
150145
if (factory != null) {
151146
throw new ProcessingError(conversionClass, "Multiple ConversionFactory annotations in a single class.");
152147
}
153-
factory = new ConverterFactory((ExecutableElement) e, annot.clinicArgs(), annot.shortCircuitPrimitive());
148+
String fullClassName = conversionClass.toString();
149+
String className = conversionClass.getSimpleName().toString();
150+
String methodName = e.getSimpleName().toString();
151+
int paramCount = ((ExecutableElement) e).getParameters().size() - annot.clinicArgs().length;
152+
factory = new ConverterFactory(fullClassName, className, methodName, paramCount, annot.clinicArgs(), annot.shortCircuitPrimitive());
154153
}
155154
}
156155
if (factory == null) {
@@ -160,4 +159,9 @@ public static ConverterFactory getForClass(TypeElement conversionClass) throws P
160159
return factory;
161160
}
162161

162+
public static ConverterFactory forCustomConversion(TypeElement type, String methodName) {
163+
String fullClassName = type.getQualifiedName().toString();
164+
String className = type.getSimpleName().toString();
165+
return new ConverterFactory(fullClassName, className, methodName, 0, new ClinicArgument[0], new PrimitiveType[0]);
166+
}
163167
}

0 commit comments

Comments
 (0)