Search in sources :

Example 1 with ModuleDescriptor

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

the class TypeSubstitutorTest method getContextScope.

private LexicalScope getContextScope() throws IOException {
    // todo comments
    String text = FileUtil.loadFile(new File("compiler/testData/type-substitutor.kt"), true);
    KtFile ktFile = KtPsiFactoryKt.KtPsiFactory(getProject()).createFile(text);
    AnalysisResult analysisResult = JvmResolveUtil.analyze(ktFile, getEnvironment());
    ModuleDescriptor module = analysisResult.getModuleDescriptor();
    LexicalScope topLevelScope = analysisResult.getBindingContext().get(BindingContext.LEXICAL_SCOPE, ktFile);
    final ClassifierDescriptor contextClass = ScopeUtilsKt.findClassifier(topLevelScope, Name.identifier("___Context"), NoLookupLocation.FROM_TEST);
    assert contextClass instanceof ClassDescriptor;
    LocalRedeclarationChecker redeclarationChecker = new ThrowingLocalRedeclarationChecker(new OverloadChecker(TypeSpecificityComparator.NONE.INSTANCE));
    LexicalScope typeParameters = new LexicalScopeImpl(topLevelScope, module, false, null, LexicalScopeKind.SYNTHETIC, redeclarationChecker, new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {

        @Override
        public Unit invoke(LexicalScopeImpl.InitializeHandler handler) {
            for (TypeParameterDescriptor parameterDescriptor : contextClass.getTypeConstructor().getParameters()) {
                handler.addClassifierDescriptor(parameterDescriptor);
            }
            return Unit.INSTANCE;
        }
    });
    return new LexicalChainedScope(typeParameters, module, false, null, LexicalScopeKind.SYNTHETIC, Arrays.asList(contextClass.getDefaultType().getMemberScope(), module.getBuiltIns().getBuiltInsPackageScope()));
}
Also used : TypeParameterDescriptor(org.jetbrains.kotlin.descriptors.TypeParameterDescriptor) ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) ClassifierDescriptor(org.jetbrains.kotlin.descriptors.ClassifierDescriptor) Unit(kotlin.Unit) AnalysisResult(org.jetbrains.kotlin.analyzer.AnalysisResult) ModuleDescriptor(org.jetbrains.kotlin.descriptors.ModuleDescriptor) KtFile(org.jetbrains.kotlin.psi.KtFile) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File)

Example 2 with ModuleDescriptor

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

the class ResolveDescriptorsFromExternalLibraries method parseLibraryFileChunk.

private static boolean parseLibraryFileChunk(File jar, String libDescription, ZipInputStream zip, int classesPerChunk) throws IOException {
    Disposable junk = new Disposable() {

        @Override
        public void dispose() {
        }
    };
    KotlinCoreEnvironment environment;
    if (jar != null) {
        environment = KotlinCoreEnvironment.createForTests(junk, KotlinTestUtils.newConfiguration(ConfigurationKind.JDK_ONLY, TestJdkKind.MOCK_JDK, KotlinTestUtils.getAnnotationsJar(), jar), EnvironmentConfigFiles.JVM_CONFIG_FILES);
    } else {
        CompilerConfiguration configuration = KotlinTestUtils.newConfiguration(ConfigurationKind.JDK_ONLY, TestJdkKind.FULL_JDK);
        environment = KotlinCoreEnvironment.createForTests(junk, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
        if (!findRtJar().equals(jar)) {
            throw new RuntimeException("rt.jar mismatch: " + jar + ", " + findRtJar());
        }
    }
    ModuleDescriptor module = JvmResolveUtil.analyze(environment).getModuleDescriptor();
    boolean hasErrors;
    try {
        hasErrors = false;
        for (int count = 0; count < classesPerChunk; ) {
            ZipEntry entry = zip.getNextEntry();
            if (entry == null) {
                break;
            }
            if (count == 0) {
                System.err.println("chunk from " + entry.getName());
            }
            System.err.println(entry.getName());
            String entryName = entry.getName();
            if (!entryName.endsWith(".class")) {
                continue;
            }
            if (entryName.matches("(.*/|)package-info\\.class")) {
                continue;
            }
            if (entryName.contains("$")) {
                continue;
            }
            String className = entryName.substring(0, entryName.length() - ".class".length()).replace("/", ".");
            try {
                ClassDescriptor clazz = DescriptorUtilsKt.resolveTopLevelClass(module, new FqName(className), NoLookupLocation.FROM_TEST);
                if (clazz == null) {
                    throw new IllegalStateException("class not found by name " + className + " in " + libDescription);
                }
                DescriptorUtils.getAllDescriptors(clazz.getDefaultType().getMemberScope());
            } catch (Exception e) {
                System.err.println("failed to resolve " + className);
                e.printStackTrace();
                //throw new RuntimeException("failed to resolve " + className + ": " + e, e);
                hasErrors = true;
            }
            ++count;
        }
    } finally {
        Disposer.dispose(junk);
    }
    return hasErrors;
}
Also used : Disposable(com.intellij.openapi.Disposable) ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) FqName(org.jetbrains.kotlin.name.FqName) ZipEntry(java.util.zip.ZipEntry) ModuleDescriptor(org.jetbrains.kotlin.descriptors.ModuleDescriptor) KotlinCoreEnvironment(org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment) CompilerConfiguration(org.jetbrains.kotlin.config.CompilerConfiguration)

Example 3 with ModuleDescriptor

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

the class AbstractResolveByStubTest method performTest.

private void performTest(@NotNull String path, boolean checkPrimaryConstructors, boolean checkPropertyAccessors) {
    KtFile file = (KtFile) getFile();
    ModuleDescriptor module = ResolutionUtils.findModuleDescriptor(file);
    PackageViewDescriptor packageViewDescriptor = module.getPackage(new FqName("test"));
    Assert.assertFalse(packageViewDescriptor.isEmpty());
    File fileToCompareTo = new File(FileUtil.getNameWithoutExtension(path) + ".txt");
    RecursiveDescriptorComparator.validateAndCompareDescriptorWithFile(packageViewDescriptor, RecursiveDescriptorComparator.DONT_INCLUDE_METHODS_OF_OBJECT.filterRecursion(RecursiveDescriptorComparator.SKIP_BUILT_INS_PACKAGES).checkPrimaryConstructors(checkPrimaryConstructors).checkPropertyAccessors(checkPropertyAccessors).withValidationStrategy(errorTypesForbidden()), fileToCompareTo);
}
Also used : ModuleDescriptor(org.jetbrains.kotlin.descriptors.ModuleDescriptor) FqName(org.jetbrains.kotlin.name.FqName) KtFile(org.jetbrains.kotlin.psi.KtFile) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File) PackageViewDescriptor(org.jetbrains.kotlin.descriptors.PackageViewDescriptor)

Example 4 with ModuleDescriptor

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

the class K2JSTranslator method translate.

@NotNull
public TranslationResult translate(@NotNull List<KtFile> files, @NotNull MainCallParameters mainCallParameters, @Nullable JsAnalysisResult analysisResult) throws TranslationException {
    if (analysisResult == null) {
        analysisResult = TopDownAnalyzerFacadeForJS.analyzeFiles(files, config);
        ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
    }
    BindingTrace bindingTrace = analysisResult.getBindingTrace();
    TopDownAnalyzerFacadeForJS.checkForErrors(files, bindingTrace.getBindingContext());
    ModuleDescriptor moduleDescriptor = analysisResult.getModuleDescriptor();
    Diagnostics diagnostics = bindingTrace.getBindingContext().getDiagnostics();
    TranslationContext context = Translation.generateAst(bindingTrace, files, mainCallParameters, moduleDescriptor, config);
    ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
    if (hasError(diagnostics))
        return new TranslationResult.Fail(diagnostics);
    JsProgram program = JsInliner.process(context);
    ResolveTemporaryNamesKt.resolveTemporaryNames(program);
    ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
    if (hasError(diagnostics))
        return new TranslationResult.Fail(diagnostics);
    CoroutineTransformer coroutineTransformer = new CoroutineTransformer(program);
    coroutineTransformer.accept(program);
    RemoveUnusedImportsKt.removeUnusedImports(program);
    ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
    if (hasError(diagnostics))
        return new TranslationResult.Fail(diagnostics);
    expandIsCalls(program, context);
    ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
    List<String> importedModules = new ArrayList<String>();
    for (StaticContext.ImportedModule module : context.getImportedModules()) {
        importedModules.add(module.getExternalName());
    }
    return new TranslationResult.Success(config, files, program, diagnostics, importedModules, moduleDescriptor, bindingTrace.getBindingContext());
}
Also used : BindingTrace(org.jetbrains.kotlin.resolve.BindingTrace) Diagnostics(org.jetbrains.kotlin.resolve.diagnostics.Diagnostics) CoroutineTransformer(org.jetbrains.kotlin.js.coroutine.CoroutineTransformer) ArrayList(java.util.ArrayList) JsProgram(org.jetbrains.kotlin.js.backend.ast.JsProgram) ModuleDescriptor(org.jetbrains.kotlin.descriptors.ModuleDescriptor) TranslationContext(org.jetbrains.kotlin.js.translate.context.TranslationContext) StaticContext(org.jetbrains.kotlin.js.translate.context.StaticContext) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with ModuleDescriptor

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

the class LoadBuiltinsTest method testBuiltIns.

public void testBuiltIns() throws Exception {
    RecursiveDescriptorComparator.Configuration configuration = RecursiveDescriptorComparator.RECURSIVE_ALL.includeMethodsOfKotlinAny(false).withRenderer(DescriptorRenderer.Companion.withOptions(new Function1<DescriptorRendererOptions, Unit>() {

        @Override
        public Unit invoke(DescriptorRendererOptions options) {
            options.setWithDefinedIn(false);
            options.setOverrideRenderingPolicy(OverrideRenderingPolicy.RENDER_OPEN_OVERRIDE);
            options.setVerbose(true);
            options.setAnnotationArgumentsRenderingPolicy(AnnotationArgumentsRenderingPolicy.UNLESS_EMPTY);
            options.setModifiers(DescriptorRendererModifier.ALL);
            return Unit.INSTANCE;
        }
    }));
    PackageFragmentProvider packageFragmentProvider = createBuiltInsPackageFragmentProvider();
    List<KtFile> files = KotlinTestUtils.loadToJetFiles(getEnvironment(), ContainerUtil.concat(allFilesUnder("core/builtins/native"), allFilesUnder("core/builtins/src")));
    ModuleDescriptor module = LazyResolveTestUtilsKt.createResolveSessionForFiles(getEnvironment().getProject(), files, false).getModuleDescriptor();
    for (FqName packageFqName : CollectionsKt.listOf(BUILT_INS_PACKAGE_FQ_NAME, COLLECTIONS_PACKAGE_FQ_NAME, RANGES_PACKAGE_FQ_NAME)) {
        PackageFragmentDescriptor fromLazyResolve = CollectionsKt.single(module.getPackage(packageFqName).getFragments());
        if (fromLazyResolve instanceof LazyPackageDescriptor) {
            PackageFragmentDescriptor deserialized = CollectionsKt.single(packageFragmentProvider.getPackageFragments(packageFqName));
            RecursiveDescriptorComparator.validateAndCompareDescriptors(fromLazyResolve, deserialized, configuration, new File("compiler/testData/builtin-classes/default/" + packageFqName.asString().replace('.', '-') + ".txt"));
        }
    }
}
Also used : RecursiveDescriptorComparator(org.jetbrains.kotlin.test.util.RecursiveDescriptorComparator) ModuleDescriptor(org.jetbrains.kotlin.descriptors.ModuleDescriptor) PackageFragmentProvider(org.jetbrains.kotlin.descriptors.PackageFragmentProvider) BuiltInsPackageFragmentProviderKt.createBuiltInPackageFragmentProvider(org.jetbrains.kotlin.builtins.BuiltInsPackageFragmentProviderKt.createBuiltInPackageFragmentProvider) FqName(org.jetbrains.kotlin.name.FqName) Function1(kotlin.jvm.functions.Function1) LazyPackageDescriptor(org.jetbrains.kotlin.resolve.lazy.descriptors.LazyPackageDescriptor) KtFile(org.jetbrains.kotlin.psi.KtFile) PackageFragmentDescriptor(org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File)

Aggregations

ModuleDescriptor (org.jetbrains.kotlin.descriptors.ModuleDescriptor)6 KtFile (org.jetbrains.kotlin.psi.KtFile)4 File (java.io.File)3 FqName (org.jetbrains.kotlin.name.FqName)3 ClassDescriptor (org.jetbrains.kotlin.descriptors.ClassDescriptor)2 Disposable (com.intellij.openapi.Disposable)1 ArrayList (java.util.ArrayList)1 ZipEntry (java.util.zip.ZipEntry)1 Unit (kotlin.Unit)1 Function1 (kotlin.jvm.functions.Function1)1 NotNull (org.jetbrains.annotations.NotNull)1 AnalysisResult (org.jetbrains.kotlin.analyzer.AnalysisResult)1 BuiltInsPackageFragmentProviderKt.createBuiltInPackageFragmentProvider (org.jetbrains.kotlin.builtins.BuiltInsPackageFragmentProviderKt.createBuiltInPackageFragmentProvider)1 KotlinCoreEnvironment (org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment)1 CompilerConfiguration (org.jetbrains.kotlin.config.CompilerConfiguration)1 ClassifierDescriptor (org.jetbrains.kotlin.descriptors.ClassifierDescriptor)1 FunctionDescriptor (org.jetbrains.kotlin.descriptors.FunctionDescriptor)1 PackageFragmentDescriptor (org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor)1 PackageFragmentProvider (org.jetbrains.kotlin.descriptors.PackageFragmentProvider)1 PackageViewDescriptor (org.jetbrains.kotlin.descriptors.PackageViewDescriptor)1