Search in sources :

Example 1 with BindingContext

use of org.jetbrains.kotlin.resolve.BindingContext in project kotlin by JetBrains.

the class SwitchCodegenUtil method buildAppropriateSwitchCodegenIfPossible.

@Nullable
public static SwitchCodegen buildAppropriateSwitchCodegenIfPossible(@NotNull KtWhenExpression expression, boolean isStatement, boolean isExhaustive, @NotNull ExpressionCodegen codegen) {
    BindingContext bindingContext = codegen.getBindingContext();
    boolean shouldInlineConstVals = codegen.getState().getShouldInlineConstVals();
    if (!isThereConstantEntriesButNulls(expression, bindingContext, shouldInlineConstVals)) {
        return null;
    }
    Type subjectType = codegen.expressionType(expression.getSubjectExpression());
    WhenByEnumsMapping mapping = codegen.getBindingContext().get(CodegenBinding.MAPPING_FOR_WHEN_BY_ENUM, expression);
    if (mapping != null) {
        return new EnumSwitchCodegen(expression, isStatement, isExhaustive, codegen, mapping);
    }
    if (isIntegralConstantsSwitch(expression, subjectType, bindingContext, shouldInlineConstVals)) {
        return new IntegralConstantsSwitchCodegen(expression, isStatement, isExhaustive, codegen);
    }
    if (isStringConstantsSwitch(expression, subjectType, bindingContext, shouldInlineConstVals)) {
        return new StringSwitchCodegen(expression, isStatement, isExhaustive, codegen);
    }
    return null;
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) BindingContext(org.jetbrains.kotlin.resolve.BindingContext) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with BindingContext

use of org.jetbrains.kotlin.resolve.BindingContext 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 BindingContext

use of org.jetbrains.kotlin.resolve.BindingContext in project kotlin by JetBrains.

the class AbstractLoadJavaTest method doTestSourceJava.

protected void doTestSourceJava(@NotNull String javaFileName) throws Exception {
    File originalJavaFile = new File(javaFileName);
    File expectedFile = getTxtFile(javaFileName);
    File testPackageDir = new File(tmpdir, "test");
    assertTrue(testPackageDir.mkdir());
    FileUtil.copy(originalJavaFile, new File(testPackageDir, originalJavaFile.getName()));
    Pair<PackageViewDescriptor, BindingContext> javaPackageAndContext = loadTestPackageAndBindingContextFromJavaRoot(tmpdir, getTestRootDisposable(), getJdkKind(), ConfigurationKind.JDK_ONLY, false);
    checkJavaPackage(expectedFile, javaPackageAndContext.first, javaPackageAndContext.second, COMPARATOR_CONFIGURATION.withValidationStrategy(errorTypesAllowed()));
}
Also used : BindingContext(org.jetbrains.kotlin.resolve.BindingContext) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File)

Example 4 with BindingContext

use of org.jetbrains.kotlin.resolve.BindingContext in project kotlin by JetBrains.

the class AbstractLoadJavaTest method doTestCompiledJavaAndKotlin.

// Java-Kotlin dependencies are not supported in this method for simplicity
protected void doTestCompiledJavaAndKotlin(@NotNull String expectedFileName) throws Exception {
    File expectedFile = new File(expectedFileName);
    File sourcesDir = new File(expectedFileName.replaceFirst("\\.txt$", ""));
    List<File> kotlinSources = FileUtil.findFilesByMask(Pattern.compile(".+\\.kt"), sourcesDir);
    KotlinCoreEnvironment environment = KotlinTestUtils.createEnvironmentWithMockJdkAndIdeaAnnotations(myTestRootDisposable, ConfigurationKind.JDK_ONLY);
    compileKotlinToDirAndGetModule(kotlinSources, tmpdir, environment);
    List<File> javaSources = FileUtil.findFilesByMask(Pattern.compile(".+\\.java"), sourcesDir);
    Pair<PackageViewDescriptor, BindingContext> binaryPackageAndContext = compileJavaAndLoadTestPackageAndBindingContextFromBinary(javaSources, tmpdir, ConfigurationKind.JDK_ONLY);
    checkJavaPackage(expectedFile, binaryPackageAndContext.first, binaryPackageAndContext.second, COMPARATOR_CONFIGURATION);
}
Also used : KotlinCoreEnvironment(org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment) BindingContext(org.jetbrains.kotlin.resolve.BindingContext) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File)

Example 5 with BindingContext

use of org.jetbrains.kotlin.resolve.BindingContext in project kotlin by JetBrains.

the class AbstractPseudocodeTest method doTest.

protected void doTest(String fileName) throws Exception {
    File file = new File(fileName);
    KtFile jetFile = KotlinTestUtils.loadJetFile(getProject(), file);
    SetMultimap<KtElement, Pseudocode> data = LinkedHashMultimap.create();
    AnalysisResult analysisResult = KotlinTestUtils.analyzeFile(jetFile, getEnvironment());
    List<KtDeclaration> declarations = jetFile.getDeclarations();
    BindingContext bindingContext = analysisResult.getBindingContext();
    for (KtDeclaration declaration : declarations) {
        addDeclaration(data, bindingContext, declaration);
        if (declaration instanceof KtDeclarationContainer) {
            for (KtDeclaration member : ((KtDeclarationContainer) declaration).getDeclarations()) {
                // Properties and initializers are processed elsewhere
                if (member instanceof KtNamedFunction || member instanceof KtSecondaryConstructor) {
                    addDeclaration(data, bindingContext, member);
                }
            }
        }
    }
    try {
        processCFData(file, data, bindingContext);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if ("true".equals(System.getProperty("kotlin.control.flow.test.dump.graphs"))) {
            CFGraphToDotFilePrinter.dumpDot(file, data.values());
        }
    }
}
Also used : IOException(java.io.IOException) BindingContext(org.jetbrains.kotlin.resolve.BindingContext) AnalysisResult(org.jetbrains.kotlin.analyzer.AnalysisResult) Pseudocode(org.jetbrains.kotlin.cfg.pseudocode.Pseudocode) File(java.io.File)

Aggregations

BindingContext (org.jetbrains.kotlin.resolve.BindingContext)20 KtFile (org.jetbrains.kotlin.psi.KtFile)6 File (java.io.File)5 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)3 AnalysisResult (org.jetbrains.kotlin.analyzer.AnalysisResult)3 IOException (java.io.IOException)2 KotlinCoreEnvironment (org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment)2 ResolutionFacade (org.jetbrains.kotlin.idea.resolve.ResolutionFacade)2 KtNamedFunction (org.jetbrains.kotlin.psi.KtNamedFunction)2 KotlinType (org.jetbrains.kotlin.types.KotlinType)2 Type (org.jetbrains.org.objectweb.asm.Type)2 HierarchyNodeDescriptor (com.intellij.ide.hierarchy.HierarchyNodeDescriptor)1 Editor (com.intellij.openapi.editor.Editor)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 PsiMethod (com.intellij.psi.PsiMethod)1 SearchScope (com.intellij.psi.search.SearchScope)1 HashMap (com.intellij.util.containers.HashMap)1