Search in sources :

Example 6 with Unit

use of kotlin.Unit in project kotlin by JetBrains.

the class ExpressionCodegen method visitObjectLiteralExpression.

@Override
public StackValue visitObjectLiteralExpression(@NotNull KtObjectLiteralExpression expression, StackValue receiver) {
    final ObjectLiteralResult objectLiteralResult = generateObjectLiteral(expression);
    final ClassDescriptor classDescriptor = objectLiteralResult.classDescriptor;
    final Type type = typeMapper.mapType(classDescriptor);
    return StackValue.operation(type, new Function1<InstructionAdapter, Unit>() {

        @Override
        public Unit invoke(InstructionAdapter v) {
            if (objectLiteralResult.wereReifiedMarkers) {
                ReifiedTypeInliner.putNeedClassReificationMarker(v);
            }
            v.anew(type);
            v.dup();
            pushClosureOnStack(classDescriptor, true, defaultCallGenerator, /* functionReferenceReceiver = */
            null);
            ConstructorDescriptor primaryConstructor = classDescriptor.getUnsubstitutedPrimaryConstructor();
            assert primaryConstructor != null : "There should be primary constructor for object literal";
            ResolvedCall<ConstructorDescriptor> superCall = getDelegationConstructorCall(bindingContext, primaryConstructor);
            if (superCall != null) {
                // For an anonymous object, we should also generate all non-default arguments that it captures for its super call
                ConstructorDescriptor superConstructor = superCall.getResultingDescriptor();
                ConstructorDescriptor constructorToCall = SamCodegenUtil.resolveSamAdapter(superConstructor);
                List<ValueParameterDescriptor> superValueParameters = superConstructor.getValueParameters();
                int params = superValueParameters.size();
                List<Type> superMappedTypes = typeMapper.mapToCallableMethod(constructorToCall, false).getValueParameterTypes();
                assert superMappedTypes.size() >= params : String.format("Incorrect number of mapped parameters vs arguments: %d < %d for %s", superMappedTypes.size(), params, classDescriptor);
                List<ResolvedValueArgument> valueArguments = new ArrayList<ResolvedValueArgument>(params);
                List<ValueParameterDescriptor> valueParameters = new ArrayList<ValueParameterDescriptor>(params);
                List<Type> mappedTypes = new ArrayList<Type>(params);
                for (ValueParameterDescriptor parameter : superValueParameters) {
                    ResolvedValueArgument argument = superCall.getValueArguments().get(parameter);
                    if (!(argument instanceof DefaultValueArgument)) {
                        valueArguments.add(argument);
                        valueParameters.add(parameter);
                        mappedTypes.add(superMappedTypes.get(parameter.getIndex()));
                    }
                }
                ArgumentGenerator argumentGenerator = new CallBasedArgumentGenerator(ExpressionCodegen.this, defaultCallGenerator, valueParameters, mappedTypes);
                argumentGenerator.generate(valueArguments, valueArguments, null);
            }
            Collection<ClassConstructorDescriptor> constructors = classDescriptor.getConstructors();
            assert constructors.size() == 1 : "Unexpected number of constructors for class: " + classDescriptor + " " + constructors;
            ConstructorDescriptor constructorDescriptor = CollectionsKt.single(constructors);
            Method constructor = typeMapper.mapAsmMethod(SamCodegenUtil.resolveSamAdapter(constructorDescriptor));
            v.invokespecial(type.getInternalName(), "<init>", constructor.getDescriptor(), false);
            return Unit.INSTANCE;
        }
    });
}
Also used : SamConstructorDescriptor(org.jetbrains.kotlin.load.java.descriptors.SamConstructorDescriptor) TypeAliasConstructorDescriptor(org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor) Method(org.jetbrains.org.objectweb.asm.commons.Method) Unit(kotlin.Unit) IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)

Example 7 with Unit

use of kotlin.Unit in project kotlin by JetBrains.

the class ExpressionCodegen method generateWhenExpression.

public StackValue generateWhenExpression(final KtWhenExpression expression, final boolean isStatement) {
    final KtExpression expr = expression.getSubjectExpression();
    final Type subjectType = expressionType(expr);
    final Type resultType = isStatement ? Type.VOID_TYPE : expressionTypeForBranchingOperation(expression);
    return StackValue.operation(resultType, new Function1<InstructionAdapter, Unit>() {

        @Override
        public Unit invoke(InstructionAdapter v) {
            SwitchCodegen switchCodegen = SwitchCodegenUtil.buildAppropriateSwitchCodegenIfPossible(expression, isStatement, CodegenUtil.isExhaustive(bindingContext, expression, isStatement), ExpressionCodegen.this);
            if (switchCodegen != null) {
                switchCodegen.generate();
                return Unit.INSTANCE;
            }
            int subjectLocal = expr != null ? myFrameMap.enterTemp(subjectType) : -1;
            if (subjectLocal != -1) {
                gen(expr, subjectType);
                tempVariables.put(expr, StackValue.local(subjectLocal, subjectType));
                v.store(subjectLocal, subjectType);
            }
            Label end = new Label();
            boolean hasElse = KtPsiUtil.checkWhenExpressionHasSingleElse(expression);
            Label nextCondition = null;
            for (KtWhenEntry whenEntry : expression.getEntries()) {
                if (nextCondition != null) {
                    v.mark(nextCondition);
                }
                nextCondition = new Label();
                FrameMap.Mark mark = myFrameMap.mark();
                Label thisEntry = new Label();
                if (!whenEntry.isElse()) {
                    KtWhenCondition[] conditions = whenEntry.getConditions();
                    for (int i = 0; i < conditions.length; i++) {
                        StackValue conditionValue = generateWhenCondition(expr, subjectType, subjectLocal, conditions[i]);
                        BranchedValue.Companion.condJump(conditionValue, nextCondition, true, v);
                        if (i < conditions.length - 1) {
                            v.goTo(thisEntry);
                            v.mark(nextCondition);
                            nextCondition = new Label();
                        }
                    }
                }
                v.visitLabel(thisEntry);
                gen(whenEntry.getExpression(), resultType);
                mark.dropTo();
                if (!whenEntry.isElse()) {
                    v.goTo(end);
                }
            }
            if (!hasElse && nextCondition != null) {
                v.mark(nextCondition);
                putUnitInstanceOntoStackForNonExhaustiveWhen(expression, isStatement);
            }
            markLineNumber(expression, isStatement);
            v.mark(end);
            myFrameMap.leaveTemp(subjectType);
            tempVariables.remove(expr);
            return null;
        }
    });
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) Label(org.jetbrains.org.objectweb.asm.Label) Unit(kotlin.Unit) SwitchCodegen(org.jetbrains.kotlin.codegen.when.SwitchCodegen)

Example 8 with Unit

use of kotlin.Unit 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;
            }
        });
    }
}
Also used : Unit(kotlin.Unit) IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)

Example 9 with Unit

use of kotlin.Unit in project kotlin by JetBrains.

the class AsmUtil method genEqualsForExpressionsOnStack.

@NotNull
public static StackValue genEqualsForExpressionsOnStack(@NotNull final IElementType opToken, @NotNull final StackValue left, @NotNull final StackValue right) {
    final Type leftType = left.type;
    final Type rightType = right.type;
    if (isPrimitive(leftType) && leftType == rightType) {
        return StackValue.cmp(opToken, leftType, left, right);
    }
    return StackValue.operation(Type.BOOLEAN_TYPE, new Function1<InstructionAdapter, Unit>() {

        @Override
        public Unit invoke(InstructionAdapter v) {
            left.put(leftType, v);
            right.put(rightType, v);
            genAreEqualCall(v);
            if (opToken == KtTokens.EXCLEQ || opToken == KtTokens.EXCLEQEQEQ) {
                genInvertBoolean(v);
            }
            return Unit.INSTANCE;
        }
    });
}
Also used : IElementType(com.intellij.psi.tree.IElementType) JvmPrimitiveType(org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType) PrimitiveType(org.jetbrains.kotlin.builtins.PrimitiveType) KotlinType(org.jetbrains.kotlin.types.KotlinType) TypeUtils.isNullableType(org.jetbrains.kotlin.types.TypeUtils.isNullableType) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) Unit(kotlin.Unit) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with Unit

use of kotlin.Unit in project kotlin by JetBrains.

the class ClosureCodegen method generateKotlinMetadataAnnotation.

@Override
protected void generateKotlinMetadataAnnotation() {
    FunctionDescriptor frontendFunDescriptor = CodegenUtilKt.unwrapFrontendVersion(funDescriptor);
    FunctionDescriptor freeLambdaDescriptor = createFreeLambdaDescriptor(frontendFunDescriptor);
    Method method = v.getSerializationBindings().get(METHOD_FOR_FUNCTION, frontendFunDescriptor);
    assert method != null : "No method for " + frontendFunDescriptor;
    v.getSerializationBindings().put(METHOD_FOR_FUNCTION, freeLambdaDescriptor, method);
    final DescriptorSerializer serializer = DescriptorSerializer.createForLambda(new JvmSerializerExtension(v.getSerializationBindings(), state));
    final ProtoBuf.Function functionProto = serializer.functionProto(freeLambdaDescriptor).build();
    WriteAnnotationUtilKt.writeKotlinMetadata(v, state, KotlinClassHeader.Kind.SYNTHETIC_CLASS, 0, new Function1<AnnotationVisitor, Unit>() {

        @Override
        public Unit invoke(AnnotationVisitor av) {
            writeAnnotationData(av, serializer, functionProto);
            return Unit.INSTANCE;
        }
    });
}
Also used : JvmSerializerExtension(org.jetbrains.kotlin.codegen.serialization.JvmSerializerExtension) DescriptorSerializer(org.jetbrains.kotlin.serialization.DescriptorSerializer) AnnotationVisitor(org.jetbrains.org.objectweb.asm.AnnotationVisitor) Method(org.jetbrains.org.objectweb.asm.commons.Method) Unit(kotlin.Unit) ProtoBuf(org.jetbrains.kotlin.serialization.ProtoBuf)

Aggregations

Unit (kotlin.Unit)18 KotlinType (org.jetbrains.kotlin.types.KotlinType)10 InstructionAdapter (org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)10 IElementType (com.intellij.psi.tree.IElementType)9 Type (org.jetbrains.org.objectweb.asm.Type)8 Label (org.jetbrains.org.objectweb.asm.Label)6 JvmSerializerExtension (org.jetbrains.kotlin.codegen.serialization.JvmSerializerExtension)3 KtFile (org.jetbrains.kotlin.psi.KtFile)3 DescriptorSerializer (org.jetbrains.kotlin.serialization.DescriptorSerializer)3 ProtoBuf (org.jetbrains.kotlin.serialization.ProtoBuf)3 Method (org.jetbrains.org.objectweb.asm.commons.Method)3 File (java.io.File)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 AnnotationVisitor (org.jetbrains.org.objectweb.asm.AnnotationVisitor)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 VirtualFileSystem (com.intellij.openapi.vfs.VirtualFileSystem)1 PsiFile (com.intellij.psi.PsiFile)1 Function (com.intellij.util.Function)1 ArrayList (java.util.ArrayList)1