Search in sources :

Example 1 with ScriptDescriptor

use of org.jetbrains.kotlin.descriptors.ScriptDescriptor in project kotlin by JetBrains.

the class ExpressionTypingServices method getBlockReturnedType.

@NotNull
public KotlinTypeInfo getBlockReturnedType(@NotNull KtBlockExpression expression, @NotNull CoercionStrategy coercionStrategyForLastExpression, @NotNull ExpressionTypingContext context) {
    List<KtExpression> block = StatementFilterKt.filterStatements(statementFilter, expression);
    DeclarationDescriptor containingDescriptor = context.scope.getOwnerDescriptor();
    TraceBasedLocalRedeclarationChecker redeclarationChecker = new TraceBasedLocalRedeclarationChecker(context.trace, expressionTypingComponents.overloadChecker);
    LexicalWritableScope scope = new LexicalWritableScope(context.scope, containingDescriptor, false, redeclarationChecker, LexicalScopeKind.CODE_BLOCK);
    KotlinTypeInfo r;
    if (block.isEmpty()) {
        r = expressionTypingComponents.dataFlowAnalyzer.createCheckedTypeInfo(expressionTypingComponents.builtIns.getUnitType(), context, expression);
    } else {
        r = getBlockReturnedTypeWithWritableScope(scope, block, coercionStrategyForLastExpression, context.replaceStatementFilter(statementFilter));
    }
    scope.freeze();
    if (containingDescriptor instanceof ScriptDescriptor) {
        context.trace.record(BindingContext.SCRIPT_SCOPE, (ScriptDescriptor) containingDescriptor, scope);
    }
    return r;
}
Also used : LexicalWritableScope(org.jetbrains.kotlin.resolve.scopes.LexicalWritableScope) TraceBasedLocalRedeclarationChecker(org.jetbrains.kotlin.resolve.scopes.TraceBasedLocalRedeclarationChecker) DeclarationDescriptor(org.jetbrains.kotlin.descriptors.DeclarationDescriptor) ScriptDescriptor(org.jetbrains.kotlin.descriptors.ScriptDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ScriptDescriptor

use of org.jetbrains.kotlin.descriptors.ScriptDescriptor in project kotlin by JetBrains.

the class ScriptCodegen method createScriptCodegen.

public static ScriptCodegen createScriptCodegen(@NotNull KtScript declaration, @NotNull GenerationState state, @NotNull CodegenContext parentContext) {
    BindingContext bindingContext = state.getBindingContext();
    ScriptDescriptor scriptDescriptor = bindingContext.get(BindingContext.SCRIPT, declaration);
    assert scriptDescriptor != null;
    Type classType = state.getTypeMapper().mapType(scriptDescriptor);
    ClassBuilder builder = state.getFactory().newVisitor(JvmDeclarationOriginKt.OtherOrigin(declaration, scriptDescriptor), classType, declaration.getContainingFile());
    List<ScriptDescriptor> earlierScripts = state.getReplSpecific().getEarlierScriptsForReplInterpreter();
    ScriptContext scriptContext = parentContext.intoScript(scriptDescriptor, earlierScripts == null ? Collections.<ScriptDescriptor>emptyList() : earlierScripts, scriptDescriptor, state.getTypeMapper());
    return new ScriptCodegen(declaration, state, scriptContext, builder);
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) ScriptContext(org.jetbrains.kotlin.codegen.context.ScriptContext) BindingContext(org.jetbrains.kotlin.resolve.BindingContext) ScriptDescriptor(org.jetbrains.kotlin.descriptors.ScriptDescriptor)

Example 3 with ScriptDescriptor

use of org.jetbrains.kotlin.descriptors.ScriptDescriptor in project kotlin by JetBrains.

the class ScriptCodegen method genConstructor.

private void genConstructor(@NotNull ScriptDescriptor scriptDescriptor, @NotNull ClassBuilder classBuilder, @NotNull MethodContext methodContext) {
    JvmMethodSignature jvmSignature = typeMapper.mapScriptSignature(scriptDescriptor, context.getEarlierScripts());
    if (state.getReplSpecific().getShouldGenerateScriptResultValue()) {
        FieldInfo resultFieldInfo = context.getResultFieldInfo();
        classBuilder.newField(JvmDeclarationOrigin.NO_ORIGIN, ACC_PUBLIC | ACC_FINAL, resultFieldInfo.getFieldName(), resultFieldInfo.getFieldType().getDescriptor(), null, null);
    }
    MethodVisitor mv = classBuilder.newMethod(JvmDeclarationOriginKt.OtherOrigin(scriptDeclaration, scriptDescriptor.getUnsubstitutedPrimaryConstructor()), ACC_PUBLIC, jvmSignature.getAsmMethod().getName(), jvmSignature.getAsmMethod().getDescriptor(), null, null);
    if (state.getClassBuilderMode().generateBodies) {
        mv.visitCode();
        InstructionAdapter iv = new InstructionAdapter(mv);
        Type classType = typeMapper.mapType(scriptDescriptor);
        ClassDescriptor superclass = DescriptorUtilsKt.getSuperClassNotAny(scriptDescriptor);
        if (superclass == null) {
            iv.load(0, classType);
            iv.invokespecial("java/lang/Object", "<init>", "()V", false);
        } else {
            ConstructorDescriptor ctorDesc = superclass.getUnsubstitutedPrimaryConstructor();
            if (ctorDesc == null)
                throw new RuntimeException("Primary constructor not found for script template " + superclass.toString());
            iv.load(0, classType);
            int valueParamStart = context.getEarlierScripts().size() + 1;
            List<ValueParameterDescriptor> valueParameters = scriptDescriptor.getUnsubstitutedPrimaryConstructor().getValueParameters();
            for (ValueParameterDescriptor superclassParam : ctorDesc.getValueParameters()) {
                ValueParameterDescriptor valueParam = null;
                for (ValueParameterDescriptor vpd : valueParameters) {
                    if (vpd.getName().equals(superclassParam.getName())) {
                        valueParam = vpd;
                        break;
                    }
                }
                assert valueParam != null;
                iv.load(valueParam.getIndex() + valueParamStart, typeMapper.mapType(valueParam.getType()));
            }
            CallableMethod ctorMethod = typeMapper.mapToCallableMethod(ctorDesc, false);
            String sig = ctorMethod.getAsmMethod().getDescriptor();
            iv.invokespecial(typeMapper.mapSupertype(superclass.getDefaultType(), null).getInternalName(), "<init>", sig, false);
        }
        iv.load(0, classType);
        FrameMap frameMap = new FrameMap();
        frameMap.enterTemp(OBJECT_TYPE);
        for (ScriptDescriptor importedScript : context.getEarlierScripts()) {
            frameMap.enter(importedScript, OBJECT_TYPE);
        }
        int offset = 1;
        for (ScriptDescriptor earlierScript : context.getEarlierScripts()) {
            Type earlierClassType = typeMapper.mapClass(earlierScript);
            iv.load(0, classType);
            iv.load(offset, earlierClassType);
            offset += earlierClassType.getSize();
            iv.putfield(classType.getInternalName(), context.getScriptFieldName(earlierScript), earlierClassType.getDescriptor());
        }
        final ExpressionCodegen codegen = new ExpressionCodegen(mv, frameMap, Type.VOID_TYPE, methodContext, state, this);
        generateInitializers(new Function0<ExpressionCodegen>() {

            @Override
            public ExpressionCodegen invoke() {
                return codegen;
            }
        });
        iv.areturn(Type.VOID_TYPE);
    }
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}
Also used : ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) ConstructorDescriptor(org.jetbrains.kotlin.descriptors.ConstructorDescriptor) ScriptDescriptor(org.jetbrains.kotlin.descriptors.ScriptDescriptor) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor) JvmMethodSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature) Type(org.jetbrains.org.objectweb.asm.Type) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) ValueParameterDescriptor(org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)

Example 4 with ScriptDescriptor

use of org.jetbrains.kotlin.descriptors.ScriptDescriptor in project kotlin by JetBrains.

the class ScriptCodegen method genFieldsForParameters.

private void genFieldsForParameters(@NotNull ClassBuilder classBuilder) {
    for (ScriptDescriptor earlierScript : context.getEarlierScripts()) {
        Type earlierClassName = typeMapper.mapType(earlierScript);
        int access = ACC_PUBLIC | ACC_FINAL;
        classBuilder.newField(NO_ORIGIN, access, context.getScriptFieldName(earlierScript), earlierClassName.getDescriptor(), null, null);
    }
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) ScriptDescriptor(org.jetbrains.kotlin.descriptors.ScriptDescriptor)

Aggregations

ScriptDescriptor (org.jetbrains.kotlin.descriptors.ScriptDescriptor)4 Type (org.jetbrains.org.objectweb.asm.Type)3 NotNull (org.jetbrains.annotations.NotNull)1 ScriptContext (org.jetbrains.kotlin.codegen.context.ScriptContext)1 ClassDescriptor (org.jetbrains.kotlin.descriptors.ClassDescriptor)1 ConstructorDescriptor (org.jetbrains.kotlin.descriptors.ConstructorDescriptor)1 DeclarationDescriptor (org.jetbrains.kotlin.descriptors.DeclarationDescriptor)1 ValueParameterDescriptor (org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)1 BindingContext (org.jetbrains.kotlin.resolve.BindingContext)1 JvmMethodSignature (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature)1 LexicalWritableScope (org.jetbrains.kotlin.resolve.scopes.LexicalWritableScope)1 TraceBasedLocalRedeclarationChecker (org.jetbrains.kotlin.resolve.scopes.TraceBasedLocalRedeclarationChecker)1 MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)1 InstructionAdapter (org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)1