Skip to content

Commit df1c696

Browse files
authored
Remove Painless Type From Locals, Variables, Params, and ScriptInfo (#28471)
1 parent 998461c commit df1c696

File tree

16 files changed

+119
-123
lines changed

16 files changed

+119
-123
lines changed

modules/lang-painless/src/main/java/org/elasticsearch/painless/LambdaBootstrap.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
* private $$Lambda0(List arg$0) {
9494
* this.arg$0 = arg$0;
9595
* }
96-
*
96+
*
9797
* public static Consumer create$lambda(List arg$0) {
9898
* return new $$Lambda0(arg$0);
9999
* }
@@ -212,7 +212,7 @@ public static CallSite lambdaBootstrap(
212212
ClassWriter cw = beginLambdaClass(lambdaClassName, factoryMethodType.returnType());
213213
Capture[] captures = generateCaptureFields(cw, factoryMethodType);
214214
generateLambdaConstructor(cw, lambdaClassType, factoryMethodType, captures);
215-
215+
216216
// Handles the special case where a method reference refers to a ctor (we need a static wrapper method):
217217
if (delegateInvokeType == H_NEWINVOKESPECIAL) {
218218
assert CTOR_METHOD_NAME.equals(delegateMethodName);
@@ -226,7 +226,7 @@ public static CallSite lambdaBootstrap(
226226
generateInterfaceMethod(cw, factoryMethodType, lambdaClassType, interfaceMethodName,
227227
interfaceMethodType, delegateClassType, delegateInvokeType,
228228
delegateMethodName, delegateMethodType, captures);
229-
229+
230230
endLambdaClass(cw);
231231

232232
Class<?> lambdaClass = createLambdaClass(loader, cw, lambdaClassType);
@@ -321,7 +321,7 @@ private static void generateLambdaConstructor(
321321

322322
constructor.returnValue();
323323
constructor.endMethod();
324-
324+
325325
// Add a factory method, if lambda takes captures.
326326
// @uschindler says: I talked with Rémi Forax about this. Technically, a plain ctor
327327
// and a MethodHandle to the ctor would be enough - BUT: Hotspot is unable to
@@ -337,10 +337,10 @@ private static void generateLambdaConstructor(
337337
/**
338338
* Generates a factory method to delegate to constructors.
339339
*/
340-
private static void generateStaticCtorDelegator(ClassWriter cw, int access, String delegatorMethodName,
340+
private static void generateStaticCtorDelegator(ClassWriter cw, int access, String delegatorMethodName,
341341
Type delegateClassType, MethodType delegateMethodType) {
342342
Method wrapperMethod = new Method(delegatorMethodName, delegateMethodType.toMethodDescriptorString());
343-
Method constructorMethod =
343+
Method constructorMethod =
344344
new Method(CTOR_METHOD_NAME, delegateMethodType.changeReturnType(void.class).toMethodDescriptorString());
345345
int modifiers = access | ACC_STATIC;
346346

@@ -379,7 +379,7 @@ private static void generateInterfaceMethod(
379379
GeneratorAdapter iface = new GeneratorAdapter(modifiers, lamMeth,
380380
cw.visitMethod(modifiers, interfaceMethodName, lamDesc, null, null));
381381
iface.visitCode();
382-
382+
383383
// Loads any captured variables onto the stack.
384384
for (int captureCount = 0; captureCount < captures.length; ++captureCount) {
385385
iface.loadThis();
@@ -473,7 +473,7 @@ private static Class<?> createLambdaClass(
473473
private static CallSite createNoCaptureCallSite(
474474
MethodType factoryMethodType,
475475
Class<?> lambdaClass) {
476-
476+
477477
try {
478478
return new ConstantCallSite(MethodHandles.constant(
479479
factoryMethodType.returnType(), lambdaClass.getConstructor().newInstance()));
@@ -503,7 +503,7 @@ private static CallSite createCaptureCallSite(
503503
* delegate method will use converted types from the interface method. Using
504504
* invokedynamic to make the delegate method call allows
505505
* {@link MethodHandle#asType} to be used to do the type conversion instead
506-
* of either a lot more code or requiring many {@link Definition.Type}s to be looked
506+
* of either a lot more code or requiring many {@link Class}es to be looked
507507
* up at link-time.
508508
*/
509509
public static CallSite delegateBootstrap(Lookup lookup,

modules/lang-painless/src/main/java/org/elasticsearch/painless/Locals.java

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.elasticsearch.painless.Definition.Method;
2323
import org.elasticsearch.painless.Definition.MethodKey;
24-
import org.elasticsearch.painless.Definition.Type;
2524
import org.elasticsearch.painless.ScriptClassInfo.MethodArgument;
2625

2726
import java.util.Arrays;
@@ -58,7 +57,7 @@ public static Locals newLocalScope(Locals currentScope) {
5857
* <p>
5958
* This is just like {@link #newFunctionScope}, except the captured parameters are made read-only.
6059
*/
61-
public static Locals newLambdaScope(Locals programScope, Type returnType, List<Parameter> parameters,
60+
public static Locals newLambdaScope(Locals programScope, Class<?> returnType, List<Parameter> parameters,
6261
int captureCount, int maxLoopCounter) {
6362
Locals locals = new Locals(programScope, programScope.definition, returnType, KEYWORDS);
6463
for (int i = 0; i < parameters.size(); i++) {
@@ -68,43 +67,43 @@ public static Locals newLambdaScope(Locals programScope, Type returnType, List<P
6867
// currently, this cannot be allowed, as we swap in real types,
6968
// but that can prevent a store of a different type...
7069
boolean isCapture = true;
71-
locals.addVariable(parameter.location, parameter.type, parameter.name, isCapture);
70+
locals.addVariable(parameter.location, parameter.clazz, parameter.name, isCapture);
7271
}
7372
// Loop counter to catch infinite loops. Internal use only.
7473
if (maxLoopCounter > 0) {
75-
locals.defineVariable(null, locals.getDefinition().intType, LOOP, true);
74+
locals.defineVariable(null, int.class, LOOP, true);
7675
}
7776
return locals;
7877
}
7978

8079
/** Creates a new function scope inside the current scope */
81-
public static Locals newFunctionScope(Locals programScope, Type returnType, List<Parameter> parameters, int maxLoopCounter) {
80+
public static Locals newFunctionScope(Locals programScope, Class<?> returnType, List<Parameter> parameters, int maxLoopCounter) {
8281
Locals locals = new Locals(programScope, programScope.definition, returnType, KEYWORDS);
8382
for (Parameter parameter : parameters) {
84-
locals.addVariable(parameter.location, parameter.type, parameter.name, false);
83+
locals.addVariable(parameter.location, parameter.clazz, parameter.name, false);
8584
}
8685
// Loop counter to catch infinite loops. Internal use only.
8786
if (maxLoopCounter > 0) {
88-
locals.defineVariable(null, locals.getDefinition().intType, LOOP, true);
87+
locals.defineVariable(null, int.class, LOOP, true);
8988
}
9089
return locals;
9190
}
9291

9392
/** Creates a new main method scope */
9493
public static Locals newMainMethodScope(ScriptClassInfo scriptClassInfo, Locals programScope, int maxLoopCounter) {
95-
Locals locals = new Locals(programScope, programScope.definition,
96-
scriptClassInfo.getExecuteMethodReturnType(), KEYWORDS);
94+
Locals locals = new Locals(
95+
programScope, programScope.definition, scriptClassInfo.getExecuteMethodReturnType(), KEYWORDS);
9796
// This reference. Internal use only.
98-
locals.defineVariable(null, programScope.definition.getType("Object"), THIS, true);
97+
locals.defineVariable(null, Object.class, THIS, true);
9998

10099
// Method arguments
101100
for (MethodArgument arg : scriptClassInfo.getExecuteArguments()) {
102-
locals.defineVariable(null, arg.getType(), arg.getName(), true);
101+
locals.defineVariable(null, arg.getClazz(), arg.getName(), true);
103102
}
104103

105104
// Loop counter to catch infinite loops. Internal use only.
106105
if (maxLoopCounter > 0) {
107-
locals.defineVariable(null, locals.getDefinition().intType, LOOP, true);
106+
locals.defineVariable(null, int.class, LOOP, true);
108107
}
109108
return locals;
110109
}
@@ -155,18 +154,18 @@ public Method getMethod(MethodKey key) {
155154
}
156155

157156
/** Creates a new variable. Throws IAE if the variable has already been defined (even in a parent) or reserved. */
158-
public Variable addVariable(Location location, Type type, String name, boolean readonly) {
157+
public Variable addVariable(Location location, Class<?> clazz, String name, boolean readonly) {
159158
if (hasVariable(name)) {
160159
throw location.createError(new IllegalArgumentException("Variable [" + name + "] is already defined."));
161160
}
162161
if (keywords.contains(name)) {
163162
throw location.createError(new IllegalArgumentException("Variable [" + name + "] is reserved."));
164163
}
165-
return defineVariable(location, type, name, readonly);
164+
return defineVariable(location, clazz, name, readonly);
166165
}
167166

168167
/** Return type of this scope (e.g. int, if inside a function that returns int) */
169-
public Type getReturnType() {
168+
public Class<?> getReturnType() {
170169
return returnType;
171170
}
172171

@@ -191,7 +190,7 @@ public Definition getDefinition() {
191190
// parent scope
192191
private final Locals parent;
193192
// return type of this scope
194-
private final Type returnType;
193+
private final Class<?> returnType;
195194
// keywords for this scope
196195
private final Set<String> keywords;
197196
// next slot number to assign
@@ -211,7 +210,7 @@ private Locals(Locals parent) {
211210
/**
212211
* Create a new Locals with specified return type
213212
*/
214-
private Locals(Locals parent, Definition definition, Type returnType, Set<String> keywords) {
213+
private Locals(Locals parent, Definition definition, Class<?> returnType, Set<String> keywords) {
215214
this.parent = parent;
216215
this.definition = definition;
217216
this.returnType = returnType;
@@ -246,13 +245,13 @@ private Method lookupMethod(MethodKey key) {
246245

247246

248247
/** Defines a variable at this scope internally. */
249-
private Variable defineVariable(Location location, Type type, String name, boolean readonly) {
248+
private Variable defineVariable(Location location, Class<?> type, String name, boolean readonly) {
250249
if (variables == null) {
251250
variables = new HashMap<>();
252251
}
253252
Variable variable = new Variable(location, name, type, getNextSlot(), readonly);
254253
variables.put(name, variable); // TODO: check result
255-
nextSlotNumber += type.type.getSize();
254+
nextSlotNumber += MethodWriter.getType(type).getSize();
256255
return variable;
257256
}
258257

@@ -272,15 +271,15 @@ private int getNextSlot() {
272271
public static final class Variable {
273272
public final Location location;
274273
public final String name;
275-
public final Type type;
274+
public final Class<?> clazz;
276275
public final boolean readonly;
277276
private final int slot;
278277
private boolean used;
279278

280-
public Variable(Location location, String name, Type type, int slot, boolean readonly) {
279+
public Variable(Location location, String name, Class<?> clazz, int slot, boolean readonly) {
281280
this.location = location;
282281
this.name = name;
283-
this.type = type;
282+
this.clazz = clazz;
284283
this.slot = slot;
285284
this.readonly = readonly;
286285
}
@@ -292,7 +291,7 @@ public int getSlot() {
292291
@Override
293292
public String toString() {
294293
StringBuilder b = new StringBuilder();
295-
b.append("Variable[type=").append(type);
294+
b.append("Variable[type=").append(Definition.ClassToName(clazz));
296295
b.append(",name=").append(name);
297296
b.append(",slot=").append(slot);
298297
if (readonly) {
@@ -306,12 +305,12 @@ public String toString() {
306305
public static final class Parameter {
307306
public final Location location;
308307
public final String name;
309-
public final Type type;
308+
public final Class<?> clazz;
310309

311-
public Parameter(Location location, String name, Type type) {
310+
public Parameter(Location location, String name, Class<?> clazz) {
312311
this.location = location;
313312
this.name = name;
314-
this.type = type;
313+
this.clazz = clazz;
315314
}
316315
}
317316
}

modules/lang-painless/src/main/java/org/elasticsearch/painless/ScriptClassInfo.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ public class ScriptClassInfo {
3838

3939
private final Class<?> baseClass;
4040
private final org.objectweb.asm.commons.Method executeMethod;
41-
private final Definition.Type executeMethodReturnType;
41+
private final Class<?> executeMethodReturnType;
4242
private final List<MethodArgument> executeArguments;
4343
private final List<org.objectweb.asm.commons.Method> needsMethods;
4444
private final List<org.objectweb.asm.commons.Method> getMethods;
45-
private final List<Definition.Type> getReturns;
45+
private final List<Class<?>> getReturns;
4646

4747
public ScriptClassInfo(Definition definition, Class<?> baseClass) {
4848
this.baseClass = baseClass;
@@ -51,7 +51,7 @@ public ScriptClassInfo(Definition definition, Class<?> baseClass) {
5151
java.lang.reflect.Method executeMethod = null;
5252
List<org.objectweb.asm.commons.Method> needsMethods = new ArrayList<>();
5353
List<org.objectweb.asm.commons.Method> getMethods = new ArrayList<>();
54-
List<Definition.Type> getReturns = new ArrayList<>();
54+
List<Class<?>> getReturns = new ArrayList<>();
5555
for (java.lang.reflect.Method m : baseClass.getMethods()) {
5656
if (m.isDefault()) {
5757
continue;
@@ -65,7 +65,7 @@ public ScriptClassInfo(Definition definition, Class<?> baseClass) {
6565
+ "] has more than one.");
6666
}
6767
}
68-
if (m.getName().startsWith("needs") && m.getReturnType().equals(boolean.class) && m.getParameterTypes().length == 0) {
68+
if (m.getName().startsWith("needs") && m.getReturnType() == boolean.class && m.getParameterTypes().length == 0) {
6969
needsMethods.add(new org.objectweb.asm.commons.Method(m.getName(), NEEDS_PARAMETER_METHOD_TYPE.toMethodDescriptorString()));
7070
}
7171
if (m.getName().startsWith("get") && m.getName().equals("getClass") == false && Modifier.isStatic(m.getModifiers()) == false) {
@@ -118,15 +118,15 @@ public org.objectweb.asm.commons.Method getExecuteMethod() {
118118
}
119119

120120
/**
121-
* The Painless {@link Definition.Type} or the return type of the {@code execute} method. This is used to generate the appropriate
121+
* The Painless {@link Class} or the return type of the {@code execute} method. This is used to generate the appropriate
122122
* return bytecode.
123123
*/
124-
public Definition.Type getExecuteMethodReturnType() {
124+
public Class<?> getExecuteMethodReturnType() {
125125
return executeMethodReturnType;
126126
}
127127

128128
/**
129-
* Painless {@link Definition.Type}s and names of the arguments to the {@code execute} method. The names are exposed to the Painless
129+
* Painless {@link Class}s and names of the arguments to the {@code execute} method. The names are exposed to the Painless
130130
* script.
131131
*/
132132
public List<MethodArgument> getExecuteArguments() {
@@ -150,38 +150,38 @@ public List<org.objectweb.asm.commons.Method> getGetMethods() {
150150
/**
151151
* The {@code getVarName} methods return types.
152152
*/
153-
public List<Definition.Type> getGetReturns() {
153+
public List<Class<?>> getGetReturns() {
154154
return getReturns;
155155
}
156156

157157
/**
158-
* Painless {@link Definition.Type}s and name of the argument to the {@code execute} method.
158+
* Painless {@link Class}es and name of the argument to the {@code execute} method.
159159
*/
160160
public static class MethodArgument {
161-
private final Definition.Type type;
161+
private final Class<?> clazz;
162162
private final String name;
163163

164-
public MethodArgument(Definition.Type type, String name) {
165-
this.type = type;
164+
public MethodArgument(Class<?> clazz, String name) {
165+
this.clazz = clazz;
166166
this.name = name;
167167
}
168168

169-
public Definition.Type getType() {
170-
return type;
169+
public Class<?> getClazz() {
170+
return clazz;
171171
}
172172

173173
public String getName() {
174174
return name;
175175
}
176176
}
177177

178-
private MethodArgument methodArgument(Definition definition, Class<?> type, String argName) {
179-
Definition.Type defType = definitionTypeForClass(definition, type, componentType -> "[" + argName + "] is of unknown type ["
178+
private MethodArgument methodArgument(Definition definition, Class<?> clazz, String argName) {
179+
Class<?> defClass = definitionTypeForClass(definition, clazz, componentType -> "[" + argName + "] is of unknown type ["
180180
+ componentType.getName() + ". Painless interfaces can only accept arguments that are of whitelisted types.");
181-
return new MethodArgument(defType, argName);
181+
return new MethodArgument(defClass, argName);
182182
}
183183

184-
private static Definition.Type definitionTypeForClass(Definition definition, Class<?> type,
184+
private static Class<?> definitionTypeForClass(Definition definition, Class<?> type,
185185
Function<Class<?>, String> unknownErrorMessageSource) {
186186
int dimensions = 0;
187187
Class<?> componentType = type;
@@ -190,16 +190,16 @@ private static Definition.Type definitionTypeForClass(Definition definition, Cla
190190
componentType = componentType.getComponentType();
191191
}
192192
Definition.Struct struct;
193-
if (componentType.equals(Object.class)) {
194-
struct = definition.DefType.struct;
193+
if (componentType == Object.class) {
194+
struct = definition.getType("def").struct;
195195
} else {
196196
Definition.RuntimeClass runtimeClass = definition.getRuntimeClass(componentType);
197197
if (runtimeClass == null) {
198198
throw new IllegalArgumentException(unknownErrorMessageSource.apply(componentType));
199199
}
200200
struct = runtimeClass.getStruct();
201201
}
202-
return definition.getType(struct, dimensions);
202+
return Definition.TypeToClass(definition.getType(struct, dimensions));
203203
}
204204

205205
private static String[] readArgumentNamesConstant(Class<?> iface) {

0 commit comments

Comments
 (0)