Search in sources :

Example 1 with GenerationState

use of org.jetbrains.kotlin.codegen.state.GenerationState in project kotlin by JetBrains.

the class ScriptContext method getResultFieldInfo.

@NotNull
public FieldInfo getResultFieldInfo() {
    assert getState().getReplSpecific().getShouldGenerateScriptResultValue() : "Should not be called unless 'scriptResultFieldName' is set";
    GenerationState state = getState();
    String scriptResultFieldName = state.getReplSpecific().getScriptResultFieldName();
    assert scriptResultFieldName != null;
    return FieldInfo.createForHiddenField(state.getTypeMapper().mapClass(scriptDescriptor), AsmTypes.OBJECT_TYPE, scriptResultFieldName);
}
Also used : GenerationState(org.jetbrains.kotlin.codegen.state.GenerationState) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with GenerationState

use of org.jetbrains.kotlin.codegen.state.GenerationState in project kotlin by JetBrains.

the class InlineCodegen method createMethodNode.

@NotNull
static SMAPAndMethodNode createMethodNode(@NotNull final FunctionDescriptor functionDescriptor, @NotNull JvmMethodSignature jvmSignature, @NotNull ExpressionCodegen codegen, @NotNull CodegenContext context, boolean callDefault, @Nullable ResolvedCall<?> resolvedCall) {
    if (InlineCodegenUtil.isSpecialEnumMethod(functionDescriptor)) {
        assert resolvedCall != null : "Resolved call for " + functionDescriptor + " should be not null";
        Map<TypeParameterDescriptor, KotlinType> arguments = resolvedCall.getTypeArguments();
        assert arguments.size() == 1 : "Resolved call for " + functionDescriptor + " should have 1 type argument";
        MethodNode node = InlineCodegenUtil.createSpecialEnumMethodBody(codegen, functionDescriptor.getName().asString(), arguments.keySet().iterator().next().getDefaultType(), codegen.getState().getTypeMapper());
        return new SMAPAndMethodNode(node, SMAPParser.parseOrCreateDefault(null, null, "fake", -1, -1));
    } else if (CoroutineCodegenUtilKt.isBuiltInSuspendCoroutineOrReturnInJvm(functionDescriptor)) {
        return new SMAPAndMethodNode(CoroutineCodegenUtilKt.createMethodNodeForSuspendCoroutineOrReturn(functionDescriptor, codegen.getState().getTypeMapper()), SMAPParser.parseOrCreateDefault(null, null, "fake", -1, -1));
    }
    final GenerationState state = codegen.getState();
    final Method asmMethod = callDefault ? state.getTypeMapper().mapDefaultMethod(functionDescriptor, context.getContextKind()) : jvmSignature.getAsmMethod();
    MethodId methodId = new MethodId(DescriptorUtils.getFqNameSafe(functionDescriptor.getContainingDeclaration()), asmMethod);
    final CallableMemberDescriptor directMember = getDirectMemberAndCallableFromObject(functionDescriptor);
    if (!isBuiltInArrayIntrinsic(functionDescriptor) && !(directMember instanceof DeserializedCallableMemberDescriptor)) {
        return doCreateMethodNodeFromSource(functionDescriptor, jvmSignature, codegen, context, callDefault, state, asmMethod);
    }
    SMAPAndMethodNode resultInCache = InlineCacheKt.getOrPut(state.getInlineCache().getMethodNodeById(), methodId, new Function0<SMAPAndMethodNode>() {

        @Override
        public SMAPAndMethodNode invoke() {
            SMAPAndMethodNode result = doCreateMethodNodeFromCompiled(directMember, state, asmMethod);
            if (result == null) {
                throw new IllegalStateException("Couldn't obtain compiled function body for " + functionDescriptor);
            }
            return result;
        }
    });
    return resultInCache.copyWithNewNode(cloneMethodNode(resultInCache.getNode()));
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) Method(org.jetbrains.org.objectweb.asm.commons.Method) DeserializedCallableMemberDescriptor(org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor) GenerationState(org.jetbrains.kotlin.codegen.state.GenerationState) MethodNode(org.jetbrains.org.objectweb.asm.tree.MethodNode) DeserializedCallableMemberDescriptor(org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GenerationState

use of org.jetbrains.kotlin.codegen.state.GenerationState in project kotlin by JetBrains.

the class FunctionCodegen method generateDefaultImplBody.

public static void generateDefaultImplBody(@NotNull MethodContext methodContext, @NotNull FunctionDescriptor functionDescriptor, @NotNull MethodVisitor mv, @NotNull DefaultParameterValueLoader loadStrategy, @Nullable KtNamedFunction function, @NotNull MemberCodegen<?> parentCodegen, @NotNull Method defaultMethod) {
    GenerationState state = parentCodegen.state;
    JvmMethodSignature signature = state.getTypeMapper().mapSignatureWithGeneric(functionDescriptor, methodContext.getContextKind());
    boolean isStatic = isStaticMethod(methodContext.getContextKind(), functionDescriptor);
    FrameMap frameMap = createFrameMap(state, functionDescriptor, signature, isStatic);
    ExpressionCodegen codegen = new ExpressionCodegen(mv, frameMap, signature.getReturnType(), methodContext, state, parentCodegen);
    CallGenerator generator = codegen.getOrCreateCallGeneratorForDefaultImplBody(functionDescriptor, function);
    InstructionAdapter iv = new InstructionAdapter(mv);
    genDefaultSuperCallCheckIfNeeded(iv, functionDescriptor, defaultMethod);
    List<JvmMethodParameterSignature> mappedParameters = signature.getValueParameters();
    int capturedArgumentsCount = 0;
    while (capturedArgumentsCount < mappedParameters.size() && mappedParameters.get(capturedArgumentsCount).getKind() != JvmMethodParameterKind.VALUE) {
        capturedArgumentsCount++;
    }
    int maskIndex = 0;
    List<ValueParameterDescriptor> valueParameters = functionDescriptor.getValueParameters();
    for (int index = 0; index < valueParameters.size(); index++) {
        if (index % Integer.SIZE == 0) {
            maskIndex = frameMap.enterTemp(Type.INT_TYPE);
        }
        ValueParameterDescriptor parameterDescriptor = valueParameters.get(index);
        Type type = mappedParameters.get(capturedArgumentsCount + index).getAsmType();
        int parameterIndex = frameMap.getIndex(parameterDescriptor);
        if (parameterDescriptor.declaresDefaultValue()) {
            iv.load(maskIndex, Type.INT_TYPE);
            iv.iconst(1 << (index % Integer.SIZE));
            iv.and(Type.INT_TYPE);
            Label loadArg = new Label();
            iv.ifeq(loadArg);
            StackValue.local(parameterIndex, type).store(loadStrategy.genValue(parameterDescriptor, codegen), iv);
            iv.mark(loadArg);
        }
    }
    // load arguments after defaults generation to avoid redundant stack normalization operations
    loadExplicitArgumentsOnStack(OBJECT_TYPE, isStatic, signature, generator);
    for (int index = 0; index < valueParameters.size(); index++) {
        ValueParameterDescriptor parameterDescriptor = valueParameters.get(index);
        Type type = mappedParameters.get(capturedArgumentsCount + index).getAsmType();
        int parameterIndex = frameMap.getIndex(parameterDescriptor);
        generator.putValueIfNeeded(type, StackValue.local(parameterIndex, type));
    }
    CallableMethod method = state.getTypeMapper().mapToCallableMethod(functionDescriptor, false);
    generator.genCall(method, null, false, codegen);
    iv.areturn(signature.getReturnType());
}
Also used : JvmMethodParameterSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature) GenerationState(org.jetbrains.kotlin.codegen.state.GenerationState) JvmMethodSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature) KotlinType(org.jetbrains.kotlin.types.KotlinType) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)

Example 4 with GenerationState

use of org.jetbrains.kotlin.codegen.state.GenerationState in project kotlin by JetBrains.

the class LoadDescriptorUtil method compileKotlinToDirAndGetModule.

@NotNull
public static ModuleDescriptor compileKotlinToDirAndGetModule(@NotNull List<File> kotlinFiles, @NotNull File outDir, @NotNull KotlinCoreEnvironment environment) {
    GenerationState state = GenerationUtils.compileFiles(createKtFiles(kotlinFiles, environment), environment);
    OutputUtilsKt.writeAllTo(state.getFactory(), outDir);
    return state.getModule();
}
Also used : GenerationState(org.jetbrains.kotlin.codegen.state.GenerationState) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with GenerationState

use of org.jetbrains.kotlin.codegen.state.GenerationState in project kotlin by JetBrains.

the class KotlinBytecodeToolWindow method compileSingleFile.

@NotNull
public static GenerationState compileSingleFile(@NotNull final KtFile ktFile, @NotNull CompilerConfiguration configuration) {
    ResolutionFacade resolutionFacade = ResolutionUtils.getResolutionFacade(ktFile);
    BindingContext bindingContextForFile = resolutionFacade.analyzeFullyAndGetResult(Collections.singletonList(ktFile)).getBindingContext();
    kotlin.Pair<BindingContext, List<KtFile>> result = DebuggerUtils.INSTANCE.analyzeInlinedFunctions(resolutionFacade, ktFile, configuration.getBoolean(CommonConfigurationKeys.DISABLE_INLINE), bindingContextForFile);
    BindingContext bindingContext = result.getFirst();
    List<KtFile> toProcess = result.getSecond();
    GenerationState.GenerateClassFilter generateClassFilter = new GenerationState.GenerateClassFilter() {

        @Override
        public boolean shouldGeneratePackagePart(@NotNull KtFile file) {
            return file == ktFile;
        }

        @Override
        public boolean shouldAnnotateClass(@NotNull KtClassOrObject processingClassOrObject) {
            return true;
        }

        @Override
        public boolean shouldGenerateClass(@NotNull KtClassOrObject processingClassOrObject) {
            return processingClassOrObject.getContainingKtFile() == ktFile;
        }

        @Override
        public boolean shouldGenerateScript(@NotNull KtScript script) {
            return script.getContainingKtFile() == ktFile;
        }
    };
    GenerationState state = new GenerationState(ktFile.getProject(), ClassBuilderFactories.TEST, resolutionFacade.getModuleDescriptor(), bindingContext, toProcess, configuration, generateClassFilter);
    KotlinCodegenFacade.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);
    return state;
}
Also used : KtClassOrObject(org.jetbrains.kotlin.psi.KtClassOrObject) KtScript(org.jetbrains.kotlin.psi.KtScript) BindingContext(org.jetbrains.kotlin.resolve.BindingContext) NotNull(org.jetbrains.annotations.NotNull) GenerationState(org.jetbrains.kotlin.codegen.state.GenerationState) ResolutionFacade(org.jetbrains.kotlin.idea.resolve.ResolutionFacade) List(java.util.List) KtFile(org.jetbrains.kotlin.psi.KtFile) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GenerationState (org.jetbrains.kotlin.codegen.state.GenerationState)11 NotNull (org.jetbrains.annotations.NotNull)7 CompilerConfiguration (org.jetbrains.kotlin.config.CompilerConfiguration)3 KtFile (org.jetbrains.kotlin.psi.KtFile)3 KotlinType (org.jetbrains.kotlin.types.KotlinType)3 ArrayList (java.util.ArrayList)2 KtScript (org.jetbrains.kotlin.psi.KtScript)2 NoDataException (com.intellij.debugger.NoDataException)1 PositionManager (com.intellij.debugger.PositionManager)1 Disposable (com.intellij.openapi.Disposable)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 Project (com.intellij.openapi.project.Project)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 ReferenceType (com.sun.jdi.ReferenceType)1 File (java.io.File)1 List (java.util.List)1 TestRunner (junit.textui.TestRunner)1 AnalysisResult (org.jetbrains.kotlin.analyzer.AnalysisResult)1 OutputFile (org.jetbrains.kotlin.backend.common.output.OutputFile)1 OutputFileCollection (org.jetbrains.kotlin.backend.common.output.OutputFileCollection)1