use of org.jetbrains.org.objectweb.asm.Type in project kotlin by JetBrains.
the class StackValue method box.
private static void box(Type type, Type toType, InstructionAdapter v) {
if (type == Type.INT_TYPE) {
if (toType.getInternalName().equals(NULLABLE_BYTE_TYPE_NAME)) {
type = Type.BYTE_TYPE;
} else if (toType.getInternalName().equals(NULLABLE_SHORT_TYPE_NAME)) {
type = Type.SHORT_TYPE;
} else if (toType.getInternalName().equals(NULLABLE_LONG_TYPE_NAME)) {
type = Type.LONG_TYPE;
}
v.cast(Type.INT_TYPE, type);
}
Type boxedType = AsmUtil.boxType(type);
if (boxedType == type)
return;
v.invokestatic(boxedType.getInternalName(), "valueOf", Type.getMethodDescriptor(boxedType, type), false);
coerce(boxedType, toType, v);
}
use of org.jetbrains.org.objectweb.asm.Type in project kotlin by JetBrains.
the class TailRecursionCodegen method assignParameterValues.
private void assignParameterValues(CallableDescriptor fd, CallableMethod callableMethod, List<ResolvedValueArgument> valueArguments) {
List<Type> types = callableMethod.getValueParameterTypes();
for (ValueParameterDescriptor parameterDescriptor : Lists.reverse(fd.getValueParameters())) {
ResolvedValueArgument arg = valueArguments.get(parameterDescriptor.getIndex());
Type type = types.get(parameterDescriptor.getIndex());
if (arg instanceof ExpressionValueArgument) {
ExpressionValueArgument ev = (ExpressionValueArgument) arg;
ValueArgument argument = ev.getValueArgument();
KtExpression argumentExpression = argument == null ? null : argument.getArgumentExpression();
if (argumentExpression instanceof KtSimpleNameExpression) {
ResolvedCall<?> resolvedCall = CallUtilKt.getResolvedCall(argumentExpression, state.getBindingContext());
if (resolvedCall != null && resolvedCall.getResultingDescriptor().equals(parameterDescriptor.getOriginal())) {
// do nothing: we shouldn't store argument to itself again
AsmUtil.pop(v, type);
continue;
}
}
//assign the parameter below
} else if (arg instanceof DefaultValueArgument) {
AsmUtil.pop(v, type);
DefaultParameterValueLoader.DEFAULT.genValue(parameterDescriptor, codegen).put(type, v);
} else if (arg instanceof VarargValueArgument) {
// assign the parameter below
} else {
throw new UnsupportedOperationException("Unknown argument type: " + arg + " in " + fd);
}
store(parameterDescriptor, type);
}
}
use of org.jetbrains.org.objectweb.asm.Type in project kotlin by JetBrains.
the class ExpressionCodegen method genVarargs.
public void genVarargs(@NotNull VarargValueArgument valueArgument, @NotNull KotlinType outType) {
Type type = asmType(outType);
assert type.getSort() == Type.ARRAY;
Type elementType = correctElementType(type);
List<ValueArgument> arguments = valueArgument.getArguments();
int size = arguments.size();
boolean hasSpread = false;
for (int i = 0; i != size; ++i) {
if (arguments.get(i).getSpreadElement() != null) {
hasSpread = true;
break;
}
}
if (hasSpread) {
boolean arrayOfReferences = KotlinBuiltIns.isArray(outType);
if (size == 1) {
// Arrays.copyOf(receiverValue, newLength)
ValueArgument argument = arguments.get(0);
Type arrayType = arrayOfReferences ? Type.getType("[Ljava/lang/Object;") : Type.getType("[" + elementType.getDescriptor());
gen(argument.getArgumentExpression(), type);
v.dup();
v.arraylength();
v.invokestatic("java/util/Arrays", "copyOf", Type.getMethodDescriptor(arrayType, arrayType, Type.INT_TYPE), false);
if (arrayOfReferences) {
v.checkcast(type);
}
} else {
String owner;
String addDescriptor;
String toArrayDescriptor;
if (arrayOfReferences) {
owner = "kotlin/jvm/internal/SpreadBuilder";
addDescriptor = "(Ljava/lang/Object;)V";
toArrayDescriptor = "([Ljava/lang/Object;)[Ljava/lang/Object;";
} else {
String spreadBuilderClassName = AsmUtil.asmPrimitiveTypeToLangPrimitiveType(elementType).getTypeName().getIdentifier() + "SpreadBuilder";
owner = "kotlin/jvm/internal/" + spreadBuilderClassName;
addDescriptor = "(" + elementType.getDescriptor() + ")V";
toArrayDescriptor = "()" + type.getDescriptor();
}
v.anew(Type.getObjectType(owner));
v.dup();
v.iconst(size);
v.invokespecial(owner, "<init>", "(I)V", false);
for (int i = 0; i != size; ++i) {
v.dup();
ValueArgument argument = arguments.get(i);
if (argument.getSpreadElement() != null) {
gen(argument.getArgumentExpression(), OBJECT_TYPE);
v.invokevirtual(owner, "addSpread", "(Ljava/lang/Object;)V", false);
} else {
gen(argument.getArgumentExpression(), elementType);
v.invokevirtual(owner, "add", addDescriptor, false);
}
}
if (arrayOfReferences) {
v.dup();
v.invokevirtual(owner, "size", "()I", false);
newArrayInstruction(outType);
v.invokevirtual(owner, "toArray", toArrayDescriptor, false);
v.checkcast(type);
} else {
v.invokevirtual(owner, "toArray", toArrayDescriptor, false);
}
}
} else {
v.iconst(arguments.size());
newArrayInstruction(outType);
for (int i = 0; i != size; ++i) {
v.dup();
StackValue rightSide = gen(arguments.get(i).getArgumentExpression());
StackValue.arrayElement(elementType, StackValue.onStack(type), StackValue.constant(i, Type.INT_TYPE)).store(rightSide, v);
}
}
}
use of org.jetbrains.org.objectweb.asm.Type in project kotlin by JetBrains.
the class ExpressionCodegen method visitArrayAccessExpression.
@Override
public StackValue visitArrayAccessExpression(@NotNull KtArrayAccessExpression expression, StackValue receiver) {
KtExpression array = expression.getArrayExpression();
KotlinType type = array != null ? bindingContext.getType(array) : null;
Type arrayType = expressionType(array);
List<KtExpression> indices = expression.getIndexExpressions();
FunctionDescriptor operationDescriptor = (FunctionDescriptor) bindingContext.get(REFERENCE_TARGET, expression);
assert operationDescriptor != null;
if (arrayType.getSort() == Type.ARRAY && indices.size() == 1 && isInt(operationDescriptor.getValueParameters().get(0).getType())) {
assert type != null;
Type elementType;
if (KotlinBuiltIns.isArray(type)) {
KotlinType jetElementType = type.getArguments().get(0).getType();
elementType = boxType(asmType(jetElementType));
} else {
elementType = correctElementType(arrayType);
}
StackValue arrayValue = genLazy(array, arrayType);
StackValue index = genLazy(indices.get(0), Type.INT_TYPE);
return StackValue.arrayElement(elementType, arrayValue, index);
} else {
ResolvedCall<FunctionDescriptor> resolvedSetCall = bindingContext.get(INDEXED_LVALUE_SET, expression);
ResolvedCall<FunctionDescriptor> resolvedGetCall = bindingContext.get(INDEXED_LVALUE_GET, expression);
boolean isGetter = OperatorNameConventions.GET.equals(operationDescriptor.getName());
Callable callable = resolveToCallable(operationDescriptor, false, isGetter ? resolvedGetCall : resolvedSetCall);
Callable callableMethod = resolveToCallableMethod(operationDescriptor, false);
Type[] argumentTypes = callableMethod.getParameterTypes();
StackValue.CollectionElementReceiver collectionElementReceiver = createCollectionElementReceiver(expression, receiver, operationDescriptor, isGetter, resolvedGetCall, resolvedSetCall, callable);
Type elementType = isGetter ? callableMethod.getReturnType() : ArrayUtil.getLastElement(argumentTypes);
return StackValue.collectionElement(collectionElementReceiver, elementType, resolvedGetCall, resolvedSetCall, this);
}
}
use of org.jetbrains.org.objectweb.asm.Type in project kotlin by JetBrains.
the class ExpressionCodegen method visitStringTemplateExpression.
@Override
public StackValue visitStringTemplateExpression(@NotNull KtStringTemplateExpression expression, StackValue receiver) {
StringBuilder constantValue = new StringBuilder("");
final KtStringTemplateEntry[] entries = expression.getEntries();
if (entries.length == 1 && entries[0] instanceof KtStringTemplateEntryWithExpression) {
KtExpression expr = entries[0].getExpression();
return genToString(gen(expr), expressionType(expr));
}
for (KtStringTemplateEntry entry : entries) {
if (entry instanceof KtLiteralStringTemplateEntry) {
constantValue.append(entry.getText());
} else if (entry instanceof KtEscapeStringTemplateEntry) {
constantValue.append(((KtEscapeStringTemplateEntry) entry).getUnescapedValue());
} else {
constantValue = null;
break;
}
}
if (constantValue != null) {
Type type = expressionType(expression);
return StackValue.constant(constantValue.toString(), type);
} else {
return StackValue.operation(JAVA_STRING_TYPE, new Function1<InstructionAdapter, Unit>() {
@Override
public Unit invoke(InstructionAdapter v) {
genStringBuilderConstructor(v);
for (KtStringTemplateEntry entry : entries) {
if (entry instanceof KtStringTemplateEntryWithExpression) {
invokeAppend(entry.getExpression());
} else {
String text = entry instanceof KtEscapeStringTemplateEntry ? ((KtEscapeStringTemplateEntry) entry).getUnescapedValue() : entry.getText();
v.aconst(text);
genInvokeAppendMethod(v, JAVA_STRING_TYPE);
}
}
v.invokevirtual("java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
return Unit.INSTANCE;
}
});
}
}
Aggregations