Search in sources :

Example 11 with Unit

use of kotlin.Unit 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 12 with Unit

use of kotlin.Unit in project kotlin by JetBrains.

the class AbstractBlackBoxCodegenTest method doLightAnalysisModeTest.

private void doLightAnalysisModeTest(@NotNull File wholeFile, @NotNull List<TestFile> files, @Nullable File javaFilesDir) {
    if (SKIP_LIGHT_ANALYSIS_MODE_TESTS)
        return;
    File boxTestsDir = new File("compiler/testData/codegen/box");
    String relativePath = FilesKt.toRelativeString(wholeFile, boxTestsDir);
    // Do nothing if this test is not under codegen/box
    if (relativePath.startsWith(".."))
        return;
    String outDir = new File("compiler/testData/codegen/light-analysis", relativePath).getParent();
    File txtFile = new File(outDir, FilesKt.getNameWithoutExtension(wholeFile) + ".txt");
    AbstractBytecodeListingTest.doTest(getTestRootDisposable(), files, javaFilesDir, txtFile, ClassBuilderFactories.TEST_KAPT3, new Function1<KotlinCoreEnvironment, Unit>() {

        @Override
        public Unit invoke(KotlinCoreEnvironment environment) {
            AnalysisHandlerExtension.Companion.registerExtension(environment.getProject(), new PartialAnalysisHandlerExtension());
            return Unit.INSTANCE;
        }
    });
}
Also used : KotlinCoreEnvironment(org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment) PartialAnalysisHandlerExtension(org.jetbrains.kotlin.resolve.jvm.extensions.PartialAnalysisHandlerExtension) Unit(kotlin.Unit) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File)

Example 13 with Unit

use of kotlin.Unit in project kotlin by JetBrains.

the class CompileEnvironmentUtil method getKtFiles.

@NotNull
public static List<KtFile> getKtFiles(@NotNull final Project project, @NotNull Collection<String> sourceRoots, @NotNull CompilerConfiguration configuration, @NotNull Function1<String, Unit> reportError) throws IOException {
    final VirtualFileSystem localFileSystem = VirtualFileManager.getInstance().getFileSystem(StandardFileSystems.FILE_PROTOCOL);
    final Set<VirtualFile> processedFiles = Sets.newHashSet();
    final List<KtFile> result = Lists.newArrayList();
    for (String sourceRootPath : sourceRoots) {
        if (sourceRootPath == null) {
            continue;
        }
        VirtualFile vFile = localFileSystem.findFileByPath(sourceRootPath);
        if (vFile == null) {
            String message = "Source file or directory not found: " + sourceRootPath;
            File moduleFilePath = configuration.get(JVMConfigurationKeys.MODULE_XML_FILE);
            if (moduleFilePath != null) {
                String moduleFileContent = FileUtil.loadFile(moduleFilePath);
                LOG.warn(message + "\n\nmodule file path: " + moduleFilePath + "\ncontent:\n" + moduleFileContent);
            }
            reportError.invoke(message);
            continue;
        }
        if (!vFile.isDirectory() && vFile.getFileType() != KotlinFileType.INSTANCE) {
            reportError.invoke("Source entry is not a Kotlin file: " + sourceRootPath);
            continue;
        }
        SequencesKt.forEach(FilesKt.walkTopDown(new File(sourceRootPath)), new Function1<File, Unit>() {

            @Override
            public Unit invoke(File file) {
                if (file.isFile()) {
                    VirtualFile virtualFile = localFileSystem.findFileByPath(file.getAbsolutePath());
                    if (virtualFile != null && !processedFiles.contains(virtualFile)) {
                        processedFiles.add(virtualFile);
                        PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
                        if (psiFile instanceof KtFile) {
                            result.add((KtFile) psiFile);
                        }
                    }
                }
                return Unit.INSTANCE;
            }
        });
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFileSystem(com.intellij.openapi.vfs.VirtualFileSystem) KtFile(org.jetbrains.kotlin.psi.KtFile) PsiFile(com.intellij.psi.PsiFile) Unit(kotlin.Unit) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) OutputFile(org.jetbrains.kotlin.backend.common.output.OutputFile) KtFile(org.jetbrains.kotlin.psi.KtFile) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with Unit

use of kotlin.Unit in project kotlin by JetBrains.

the class ExpressionCodegen method visitBinaryWithTypeRHSExpression.

@Override
public StackValue visitBinaryWithTypeRHSExpression(@NotNull KtBinaryExpressionWithTypeRHS expression, StackValue receiver) {
    KtExpression left = expression.getLeft();
    final IElementType opToken = expression.getOperationReference().getReferencedNameElementType();
    final KotlinType rightType = bindingContext.get(TYPE, expression.getRight());
    assert rightType != null;
    final StackValue value = genQualified(receiver, left);
    return StackValue.operation(boxType(asmType(rightType)), new Function1<InstructionAdapter, Unit>() {

        @Override
        public Unit invoke(InstructionAdapter v) {
            value.put(boxType(value.type), v);
            if (value.type == Type.VOID_TYPE) {
                StackValue.putUnitInstance(v);
            }
            boolean safeAs = opToken == KtTokens.AS_SAFE;
            Type type = boxType(asmType(rightType));
            if (TypeUtils.isReifiedTypeParameter(rightType)) {
                putReifiedOperationMarkerIfTypeIsReifiedParameter(rightType, safeAs ? ReifiedTypeInliner.OperationKind.SAFE_AS : ReifiedTypeInliner.OperationKind.AS);
                v.checkcast(type);
                return Unit.INSTANCE;
            }
            CodegenUtilKt.generateAsCast(v, rightType, type, safeAs);
            return Unit.INSTANCE;
        }
    });
}
Also used : IElementType(com.intellij.psi.tree.IElementType) IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) KotlinType(org.jetbrains.kotlin.types.KotlinType) Unit(kotlin.Unit)

Example 15 with Unit

use of kotlin.Unit in project kotlin by JetBrains.

the class ExpressionCodegen method generateElvis.

private StackValue generateElvis(@NotNull final KtBinaryExpression expression) {
    KtExpression left = expression.getLeft();
    final Type exprType = expressionType(expression);
    final Type leftType = expressionType(left);
    final Label ifNull = new Label();
    assert left != null : "left expression in elvis should be not null: " + expression.getText();
    final StackValue value = generateExpressionWithNullFallback(left, ifNull);
    if (isPrimitive(leftType)) {
        return value;
    }
    return StackValue.operation(exprType, new Function1<InstructionAdapter, Unit>() {

        @Override
        public Unit invoke(InstructionAdapter v) {
            value.put(value.type, v);
            v.dup();
            v.ifnull(ifNull);
            StackValue.onStack(leftType).put(exprType, v);
            Label end = new Label();
            v.goTo(end);
            v.mark(ifNull);
            v.pop();
            gen(expression.getRight(), exprType);
            v.mark(end);
            return null;
        }
    });
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) Label(org.jetbrains.org.objectweb.asm.Label) Unit(kotlin.Unit)

Aggregations

Unit (kotlin.Unit)18 KotlinType (org.jetbrains.kotlin.types.KotlinType)10 InstructionAdapter (org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)10 IElementType (com.intellij.psi.tree.IElementType)9 Type (org.jetbrains.org.objectweb.asm.Type)8 Label (org.jetbrains.org.objectweb.asm.Label)6 JvmSerializerExtension (org.jetbrains.kotlin.codegen.serialization.JvmSerializerExtension)3 KtFile (org.jetbrains.kotlin.psi.KtFile)3 DescriptorSerializer (org.jetbrains.kotlin.serialization.DescriptorSerializer)3 ProtoBuf (org.jetbrains.kotlin.serialization.ProtoBuf)3 Method (org.jetbrains.org.objectweb.asm.commons.Method)3 File (java.io.File)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 AnnotationVisitor (org.jetbrains.org.objectweb.asm.AnnotationVisitor)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 VirtualFileSystem (com.intellij.openapi.vfs.VirtualFileSystem)1 PsiFile (com.intellij.psi.PsiFile)1 Function (com.intellij.util.Function)1 ArrayList (java.util.ArrayList)1