Search in sources :

Example 26 with FqName

use of org.jetbrains.kotlin.name.FqName in project kotlin by JetBrains.

the class KotlinCodegenFacade method doGenerateFiles.

public static void doGenerateFiles(@NotNull Collection<KtFile> files, @NotNull GenerationState state, @NotNull CompilationErrorHandler errorHandler) {
    MultiMap<FqName, KtFile> filesInPackages = new MultiMap<FqName, KtFile>();
    MultiMap<FqName, KtFile> filesInMultifileClasses = new MultiMap<FqName, KtFile>();
    for (KtFile file : files) {
        if (file == null)
            throw new IllegalArgumentException("A null file given for compilation");
        JvmFileClassInfo fileClassInfo = state.getFileClassesProvider().getFileClassInfo(file);
        if (fileClassInfo.getWithJvmMultifileClass()) {
            filesInMultifileClasses.putValue(fileClassInfo.getFacadeClassFqName(), file);
        } else {
            filesInPackages.putValue(file.getPackageFqName(), file);
        }
    }
    Set<FqName> obsoleteMultifileClasses = new HashSet<FqName>(state.getObsoleteMultifileClasses());
    for (FqName multifileClassFqName : Sets.union(filesInMultifileClasses.keySet(), obsoleteMultifileClasses)) {
        doCheckCancelled(state);
        generateMultifileClass(state, multifileClassFqName, filesInMultifileClasses.get(multifileClassFqName), errorHandler);
    }
    Set<FqName> packagesWithObsoleteParts = new HashSet<FqName>(state.getPackagesWithObsoleteParts());
    for (FqName packageFqName : Sets.union(packagesWithObsoleteParts, filesInPackages.keySet())) {
        doCheckCancelled(state);
        generatePackage(state, packageFqName, filesInPackages.get(packageFqName), errorHandler);
    }
    doCheckCancelled(state);
    state.getFactory().done();
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) JvmFileClassInfo(org.jetbrains.kotlin.fileClasses.JvmFileClassInfo) FqName(org.jetbrains.kotlin.name.FqName) KtFile(org.jetbrains.kotlin.psi.KtFile) HashSet(java.util.HashSet)

Example 27 with FqName

use of org.jetbrains.kotlin.name.FqName in project kotlin by JetBrains.

the class ImplementationBodyCodegen method signature.

@NotNull
public static JvmClassSignature signature(@NotNull ClassDescriptor descriptor, @NotNull Type classAsmType, @NotNull SuperClassInfo superClassInfo, @NotNull KotlinTypeMapper typeMapper) {
    JvmSignatureWriter sw = new BothSignatureWriter(BothSignatureWriter.Mode.CLASS);
    typeMapper.writeFormalTypeParameters(descriptor.getDeclaredTypeParameters(), sw);
    sw.writeSuperclass();
    if (superClassInfo.getKotlinType() == null) {
        sw.writeClassBegin(superClassInfo.getType());
        sw.writeClassEnd();
    } else {
        typeMapper.mapSupertype(superClassInfo.getKotlinType(), sw);
    }
    sw.writeSuperclassEnd();
    LinkedHashSet<String> superInterfaces = new LinkedHashSet<String>();
    Set<String> kotlinMarkerInterfaces = new LinkedHashSet<String>();
    for (KotlinType supertype : descriptor.getTypeConstructor().getSupertypes()) {
        if (isJvmInterface(supertype.getConstructor().getDeclarationDescriptor())) {
            sw.writeInterface();
            Type jvmInterfaceType = typeMapper.mapSupertype(supertype, sw);
            sw.writeInterfaceEnd();
            String jvmInterfaceInternalName = jvmInterfaceType.getInternalName();
            superInterfaces.add(jvmInterfaceInternalName);
            FqName kotlinInterfaceName = DescriptorUtils.getFqName(supertype.getConstructor().getDeclarationDescriptor()).toSafe();
            String kotlinMarkerInterfaceInternalName = KOTLIN_MARKER_INTERFACES.get(kotlinInterfaceName);
            if (kotlinMarkerInterfaceInternalName != null) {
                kotlinMarkerInterfaces.add(kotlinMarkerInterfaceInternalName);
            }
        }
    }
    for (String kotlinMarkerInterface : kotlinMarkerInterfaces) {
        sw.writeInterface();
        sw.writeAsmType(getObjectType(kotlinMarkerInterface));
        sw.writeInterfaceEnd();
    }
    superInterfaces.addAll(kotlinMarkerInterfaces);
    return new JvmClassSignature(classAsmType.getInternalName(), superClassInfo.getType().getInternalName(), new ArrayList<String>(superInterfaces), sw.makeJavaGenericSignature());
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) Type.getObjectType(org.jetbrains.org.objectweb.asm.Type.getObjectType) JvmSignatureWriter(org.jetbrains.kotlin.codegen.signature.JvmSignatureWriter) BothSignatureWriter(org.jetbrains.kotlin.codegen.signature.BothSignatureWriter) FqName(org.jetbrains.kotlin.name.FqName) KotlinType(org.jetbrains.kotlin.types.KotlinType) JvmClassSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmClassSignature) NotNull(org.jetbrains.annotations.NotNull) BindingContextUtils.getNotNull(org.jetbrains.kotlin.resolve.BindingContextUtils.getNotNull)

Example 28 with FqName

use of org.jetbrains.kotlin.name.FqName in project kotlin by JetBrains.

the class SamWrapperCodegen method getWrapperName.

@NotNull
private FqName getWrapperName(@NotNull KtFile containingFile) {
    FqName fileClassFqName = JvmFileClassUtil.getFileClassInfoNoResolve(containingFile).getFileClassFqName();
    JavaClassDescriptor descriptor = samType.getJavaClassDescriptor();
    int hash = PackagePartClassUtils.getPathHashCode(containingFile.getVirtualFile()) * 31 + DescriptorUtils.getFqNameSafe(descriptor).hashCode();
    String shortName = String.format("%s$sam$%s%s$%08x", fileClassFqName.shortName().asString(), descriptor.getName().asString(), (isInsideInline ? "$i" : ""), hash);
    return fileClassFqName.parent().child(Name.identifier(shortName));
}
Also used : FqName(org.jetbrains.kotlin.name.FqName) JavaClassDescriptor(org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 29 with FqName

use of org.jetbrains.kotlin.name.FqName in project kotlin by JetBrains.

the class ExpectedResolveData method checkResult.

public final void checkResult(BindingContext bindingContext) {
    Set<PsiElement> unresolvedReferences = Sets.newHashSet();
    for (Diagnostic diagnostic : bindingContext.getDiagnostics()) {
        if (Errors.UNRESOLVED_REFERENCE_DIAGNOSTICS.contains(diagnostic.getFactory())) {
            unresolvedReferences.add(diagnostic.getPsiElement());
        }
    }
    Map<String, PsiElement> nameToDeclaration = Maps.newHashMap();
    Map<PsiElement, String> declarationToName = Maps.newHashMap();
    for (Map.Entry<String, Position> entry : declarationToPosition.entrySet()) {
        String name = entry.getKey();
        Position position = entry.getValue();
        PsiElement element = position.getElement();
        PsiElement ancestorOfType;
        if (name.equals("file")) {
            ancestorOfType = element.getContainingFile();
        } else {
            ancestorOfType = getAncestorOfType(KtDeclaration.class, element);
            if (ancestorOfType == null) {
                KtPackageDirective directive = getAncestorOfType(KtPackageDirective.class, element);
                assert directive != null : "Not a declaration: " + name;
                ancestorOfType = element;
            }
        }
        nameToDeclaration.put(name, ancestorOfType);
        declarationToName.put(ancestorOfType, name);
    }
    for (Map.Entry<Position, String> entry : positionToReference.entrySet()) {
        Position position = entry.getKey();
        String name = entry.getValue();
        PsiElement element = position.getElement();
        KtReferenceExpression referenceExpression = PsiTreeUtil.getParentOfType(element, KtReferenceExpression.class);
        DeclarationDescriptor referenceTarget = bindingContext.get(REFERENCE_TARGET, referenceExpression);
        if ("!".equals(name)) {
            assertTrue("Must have been unresolved: " + renderReferenceInContext(referenceExpression) + " but was resolved to " + renderNullableDescriptor(referenceTarget), unresolvedReferences.contains(referenceExpression));
            assertTrue(String.format("Reference =%s= has a reference target =%s= but expected to be unresolved", renderReferenceInContext(referenceExpression), renderNullableDescriptor(referenceTarget)), referenceTarget == null);
            continue;
        }
        if ("!!".equals(name)) {
            assertTrue("Must have been resolved to multiple descriptors: " + renderReferenceInContext(referenceExpression) + " but was resolved to " + renderNullableDescriptor(referenceTarget), bindingContext.get(AMBIGUOUS_REFERENCE_TARGET, referenceExpression) != null);
            continue;
        } else if ("!null".equals(name)) {
            assertTrue("Must have been resolved to null: " + renderReferenceInContext(referenceExpression) + " but was resolved to " + renderNullableDescriptor(referenceTarget), referenceTarget == null);
            continue;
        } else if ("!error".equals(name)) {
            assertTrue("Must have been resolved to error: " + renderReferenceInContext(referenceExpression) + " but was resolved to " + renderNullableDescriptor(referenceTarget), ErrorUtils.isError(referenceTarget));
            continue;
        }
        PsiElement expected = nameToDeclaration.get(name);
        if (expected == null) {
            expected = nameToPsiElement.get(name);
        }
        KtReferenceExpression reference = getAncestorOfType(KtReferenceExpression.class, element);
        if (expected == null && name.startsWith(STANDARD_PREFIX)) {
            DeclarationDescriptor expectedDescriptor = nameToDescriptor.get(name);
            KtTypeReference typeReference = getAncestorOfType(KtTypeReference.class, element);
            if (expectedDescriptor != null) {
                DeclarationDescriptor actual = bindingContext.get(REFERENCE_TARGET, reference);
                assertDescriptorsEqual("Expected: " + name, expectedDescriptor.getOriginal(), actual == null ? null : actual.getOriginal());
                continue;
            }
            KotlinType actualType = bindingContext.get(BindingContext.TYPE, typeReference);
            assertNotNull("Type " + name + " not resolved for reference " + name, actualType);
            ClassifierDescriptor expectedClass = getBuiltinClass(name.substring(STANDARD_PREFIX.length()));
            assertTypeConstructorEquals("Type resolution mismatch: ", expectedClass.getTypeConstructor(), actualType.getConstructor());
            continue;
        }
        assert expected != null : "No declaration for " + name;
        if (referenceTarget instanceof PackageViewDescriptor) {
            KtPackageDirective expectedDirective = PsiTreeUtil.getParentOfType(expected, KtPackageDirective.class);
            FqName expectedFqName;
            if (expectedDirective != null) {
                expectedFqName = expectedDirective.getFqName();
            } else if (expected instanceof PsiQualifiedNamedElement) {
                String qualifiedName = ((PsiQualifiedNamedElement) expected).getQualifiedName();
                assert qualifiedName != null : "No qualified name for " + name;
                expectedFqName = new FqName(qualifiedName);
            } else {
                throw new IllegalStateException(expected.getClass().getName() + " name=" + name);
            }
            assertEquals(expectedFqName, ((PackageViewDescriptor) referenceTarget).getFqName());
            continue;
        }
        PsiElement actual = referenceTarget == null ? bindingContext.get(BindingContext.LABEL_TARGET, referenceExpression) : DescriptorToSourceUtils.descriptorToDeclaration(referenceTarget);
        if (actual instanceof KtSimpleNameExpression) {
            actual = ((KtSimpleNameExpression) actual).getIdentifier();
        }
        String actualName = null;
        if (actual != null) {
            actualName = declarationToName.get(actual);
            if (actualName == null) {
                actualName = actual.toString();
            }
        }
        assertNotNull(element.getText(), reference);
        assertEquals("Reference `" + name + "`" + renderReferenceInContext(reference) + " is resolved into " + actualName + ".", expected, actual);
    }
    for (Map.Entry<Position, String> entry : positionToType.entrySet()) {
        Position position = entry.getKey();
        String typeName = entry.getValue();
        PsiElement element = position.getElement();
        KtExpression expression = getAncestorOfType(KtExpression.class, element);
        KotlinType expressionType = bindingContext.getType(expression);
        TypeConstructor expectedTypeConstructor;
        if (typeName.startsWith(STANDARD_PREFIX)) {
            String name = typeName.substring(STANDARD_PREFIX.length());
            ClassifierDescriptor expectedClass = getBuiltinClass(name);
            expectedTypeConstructor = expectedClass.getTypeConstructor();
        } else {
            Position declarationPosition = declarationToPosition.get(typeName);
            assertNotNull("Undeclared: " + typeName, declarationPosition);
            PsiElement declElement = declarationPosition.getElement();
            assertNotNull(declarationPosition);
            KtDeclaration declaration = getAncestorOfType(KtDeclaration.class, declElement);
            assertNotNull(declaration);
            if (declaration instanceof KtClass) {
                ClassDescriptor classDescriptor = bindingContext.get(BindingContext.CLASS, declaration);
                expectedTypeConstructor = classDescriptor.getTypeConstructor();
            } else if (declaration instanceof KtTypeParameter) {
                TypeParameterDescriptor typeParameterDescriptor = bindingContext.get(BindingContext.TYPE_PARAMETER, (KtTypeParameter) declaration);
                expectedTypeConstructor = typeParameterDescriptor.getTypeConstructor();
            } else {
                fail("Unsupported declaration: " + declaration);
                return;
            }
        }
        assertNotNull(expression.getText() + " type is null", expressionType);
        assertTypeConstructorEquals("At " + position + ": ", expectedTypeConstructor, expressionType.getConstructor());
    }
}
Also used : FqName(org.jetbrains.kotlin.name.FqName) KotlinType(org.jetbrains.kotlin.types.KotlinType) Diagnostic(org.jetbrains.kotlin.diagnostics.Diagnostic) PsiQualifiedNamedElement(com.intellij.psi.PsiQualifiedNamedElement) PsiElement(com.intellij.psi.PsiElement) TypeConstructor(org.jetbrains.kotlin.types.TypeConstructor) Map(java.util.Map)

Example 30 with FqName

use of org.jetbrains.kotlin.name.FqName in project kotlin by JetBrains.

the class KotlinDebuggerTestCase method createDebugProcess.

@SuppressWarnings("MethodMayBeStatic")
protected void createDebugProcess(@NotNull String path) throws Exception {
    File file = new File(path);
    //noinspection ConstantConditions
    FileBasedIndex.getInstance().requestReindex(VfsUtil.findFileByIoFile(file, true));
    String packageName = file.getName().replace(".kt", "");
    FqName packageFQN = new FqName(packageName);
    String mainClassName = PackagePartClassUtils.getPackagePartFqName(packageFQN, file.getName()).asString();
    createLocalProcess(mainClassName);
}
Also used : FqName(org.jetbrains.kotlin.name.FqName) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File)

Aggregations

FqName (org.jetbrains.kotlin.name.FqName)74 NotNull (org.jetbrains.annotations.NotNull)25 Nullable (org.jetbrains.annotations.Nullable)15 KtFile (org.jetbrains.kotlin.psi.KtFile)10 StringRef (com.intellij.util.io.StringRef)6 File (java.io.File)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)4 ModuleDescriptor (org.jetbrains.kotlin.descriptors.ModuleDescriptor)4 Name (org.jetbrains.kotlin.name.Name)4 PsiClass (com.intellij.psi.PsiClass)3 ClassId (org.jetbrains.kotlin.name.ClassId)3 Disposable (com.intellij.openapi.Disposable)2 ConfigurationException (com.intellij.openapi.options.ConfigurationException)2 SmartList (com.intellij.util.SmartList)2 Function1 (kotlin.jvm.functions.Function1)2 KotlinCoreEnvironment (org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment)2 CompilerConfiguration (org.jetbrains.kotlin.config.CompilerConfiguration)2 JavaClassDescriptor (org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor)2 KtClassOrObject (org.jetbrains.kotlin.psi.KtClassOrObject)2