コード例 #1
0
ファイル: MemberCodegen.java プロジェクト: kuity/kotlin
  public static void genClassOrObject(
      @NotNull CodegenContext parentContext,
      @NotNull JetClassOrObject aClass,
      @NotNull GenerationState state,
      @Nullable MemberCodegen<?> parentCodegen) {
    ClassDescriptor descriptor = state.getBindingContext().get(BindingContext.CLASS, aClass);

    if (descriptor == null || ErrorUtils.isError(descriptor)) {
      badDescriptor(descriptor, state.getClassBuilderMode());
      return;
    }

    if (descriptor.getName().equals(SpecialNames.NO_NAME_PROVIDED)) {
      badDescriptor(descriptor, state.getClassBuilderMode());
    }

    ClassBuilder classBuilder =
        state.getFactory().forClassImplementation(descriptor, aClass.getContainingFile());
    ClassContext classContext =
        parentContext.intoClass(descriptor, OwnerKind.IMPLEMENTATION, state);
    new ImplementationBodyCodegen(aClass, classContext, classBuilder, state, parentCodegen)
        .generate();

    if (aClass instanceof JetClass && ((JetClass) aClass).isTrait()) {
      ClassBuilder traitImplBuilder =
          state.getFactory().forTraitImplementation(descriptor, state, aClass.getContainingFile());
      ClassContext traitImplContext =
          parentContext.intoClass(descriptor, OwnerKind.TRAIT_IMPL, state);
      new TraitImplBodyCodegen(aClass, traitImplContext, traitImplBuilder, state, parentCodegen)
          .generate();
    }
  }
コード例 #2
0
 @NotNull
 public static ClassFileFactory generateFiles(
     @NotNull JetCoreEnvironment environment, @NotNull CodegenTestFiles files) {
   AnalyzeExhaust analyzeExhaust =
       AnalyzerFacadeForJVM.analyzeFilesWithJavaIntegrationAndCheckForErrors(
           environment.getProject(),
           files.getPsiFiles(),
           files.getScriptParameterTypes(),
           Predicates.<PsiFile>alwaysTrue());
   analyzeExhaust.throwIfError();
   AnalyzingUtils.throwExceptionOnErrors(analyzeExhaust.getBindingContext());
   CompilerConfiguration configuration = environment.getConfiguration();
   GenerationState state =
       new GenerationState(
           environment.getProject(),
           ClassBuilderFactories.TEST,
           Progress.DEAF,
           analyzeExhaust.getBindingContext(),
           files.getPsiFiles(),
           configuration.get(JVMConfigurationKeys.GENERATE_NOT_NULL_ASSERTIONS, true),
           configuration.get(JVMConfigurationKeys.GENERATE_NOT_NULL_PARAMETER_ASSERTIONS, true),
           /*generateDeclaredClasses = */ true,
           configuration.get(
               JVMConfigurationKeys.ENABLE_INLINE, InlineUtil.DEFAULT_INLINE_FLAG_FOR_TEST));
   KotlinCodegenFacade.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);
   return state.getFactory();
 }
コード例 #3
0
    @NotNull
    @Override
    protected String processRequest(@NotNull Location location) {
      JetFile jetFile = location.getJetFile();
      assert jetFile != null;

      GenerationState state;
      try {
        AnalyzeExhaust exhaust = AnalyzerFacadeWithCache.analyzeFileWithCache(jetFile);
        if (exhaust.isError()) {
          return printStackTraceToString(exhaust.getError());
        }
        state =
            new GenerationState(
                jetFile.getProject(),
                ClassBuilderFactories.TEST,
                Progress.DEAF,
                exhaust.getBindingContext(),
                Collections.singletonList(jetFile),
                true,
                true,
                GenerationState.GenerateClassFilter.GENERATE_ALL,
                enableInline.isSelected());
        KotlinCodegenFacade.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);
      } catch (ProcessCanceledException e) {
        throw e;
      } catch (Exception e) {
        return printStackTraceToString(e);
      }

      StringBuilder answer = new StringBuilder();

      OutputFileCollection outputFiles = state.getFactory();
      for (OutputFile outputFile : outputFiles.asList()) {
        answer.append("// ================");
        answer.append(outputFile.getRelativePath());
        answer.append(" =================\n");
        answer.append(outputFile.asText()).append("\n\n");
      }

      return answer.toString();
    }
コード例 #4
0
  public void testNoAssertionsForKotlinFromBinary() throws Exception {
    CompilerConfiguration configuration =
        JetTestUtils.compilerConfigurationForTests(
            ConfigurationKind.JDK_ONLY, TestJdkKind.MOCK_JDK);
    JetCoreEnvironment tmpEnvironment =
        new JetCoreEnvironment(getTestRootDisposable(), configuration);
    GenerationState state =
        generateCommon(
            ClassBuilderFactories.TEST,
            tmpEnvironment,
            CodegenTestFiles.create(
                tmpEnvironment.getProject(),
                new String[] {"notNullAssertions/noAssertionsForKotlin.kt"}));
    File compiledDirectory = new File(FileUtil.getTempDirectory(), "kotlin-classes");
    CompileEnvironmentUtil.writeToOutputDirectory(state.getFactory(), compiledDirectory);

    setUpEnvironment(true, false, compiledDirectory);

    loadFile("notNullAssertions/noAssertionsForKotlinMain.kt");

    assertNoIntrinsicsMethodIsCalled(PackageClassUtils.getPackageClassName(FqName.ROOT));
  }
コード例 #5
0
ファイル: ReplInterpreter.java プロジェクト: kuity/kotlin
  @NotNull
  public LineResult eval(@NotNull String line) {
    ++lineNumber;

    FqName scriptFqName = new FqName("Line" + lineNumber);
    Type scriptClassType = asmTypeByFqNameWithoutInnerClasses(scriptFqName);

    StringBuilder fullText = new StringBuilder();
    for (String prevLine : previousIncompleteLines) {
      fullText.append(prevLine + "\n");
    }
    fullText.append(line);

    LightVirtualFile virtualFile =
        new LightVirtualFile(
            "line" + lineNumber + JetParserDefinition.STD_SCRIPT_EXT,
            JetLanguage.INSTANCE,
            fullText.toString());
    virtualFile.setCharset(CharsetToolkit.UTF8_CHARSET);
    JetFile psiFile =
        (JetFile)
            ((PsiFileFactoryImpl) PsiFileFactory.getInstance(jetCoreEnvironment.getProject()))
                .trySetupPsiForFile(virtualFile, JetLanguage.INSTANCE, true, false);

    MessageCollectorToString errorCollector = new MessageCollectorToString();

    AnalyzerWithCompilerReport.SyntaxErrorReport syntaxErrorReport =
        AnalyzerWithCompilerReport.reportSyntaxErrors(psiFile, errorCollector);

    if (syntaxErrorReport.isOnlyErrorAtEof()) {
      previousIncompleteLines.add(line);
      return LineResult.incomplete();
    }

    previousIncompleteLines.clear();

    if (syntaxErrorReport.isHasErrors()) {
      return LineResult.error(errorCollector.getString());
    }

    prepareForTheNextReplLine(topDownAnalysisContext);
    trace.clearDiagnostics();

    psiFile.getScript().putUserData(ScriptHeaderResolver.PRIORITY_KEY, lineNumber);

    ScriptDescriptor scriptDescriptor = doAnalyze(psiFile, errorCollector);
    if (scriptDescriptor == null) {
      return LineResult.error(errorCollector.getString());
    }

    List<Pair<ScriptDescriptor, Type>> earlierScripts = Lists.newArrayList();

    for (EarlierLine earlierLine : earlierLines) {
      earlierScripts.add(
          Pair.create(earlierLine.getScriptDescriptor(), earlierLine.getClassType()));
    }

    BindingContext bindingContext =
        AnalyzeExhaust.success(trace.getBindingContext(), module).getBindingContext();
    GenerationState generationState =
        new GenerationState(
            psiFile.getProject(),
            ClassBuilderFactories.BINARIES,
            bindingContext,
            Collections.singletonList(psiFile),
            CompilerArgumentsUtil.DEFAULT_INLINE_FLAG);

    compileScript(
        psiFile.getScript(),
        scriptClassType,
        earlierScripts,
        generationState,
        CompilationErrorHandler.THROW_EXCEPTION);

    for (OutputFile outputFile : generationState.getFactory().asList()) {
      classLoader.addClass(
          JvmClassName.byInternalName(outputFile.getRelativePath().replaceFirst("\\.class$", "")),
          outputFile.asByteArray());
    }

    try {
      Class<?> scriptClass = classLoader.loadClass(scriptFqName.asString());

      Class<?>[] constructorParams = new Class<?>[earlierLines.size()];
      Object[] constructorArgs = new Object[earlierLines.size()];

      for (int i = 0; i < earlierLines.size(); ++i) {
        constructorParams[i] = earlierLines.get(i).getScriptClass();
        constructorArgs[i] = earlierLines.get(i).getScriptInstance();
      }

      Constructor<?> scriptInstanceConstructor = scriptClass.getConstructor(constructorParams);
      Object scriptInstance;
      try {
        scriptInstance = scriptInstanceConstructor.newInstance(constructorArgs);
      } catch (Throwable e) {
        return LineResult.error(Throwables.getStackTraceAsString(e));
      }
      Field rvField = scriptClass.getDeclaredField("rv");
      rvField.setAccessible(true);
      Object rv = rvField.get(scriptInstance);

      earlierLines.add(
          new EarlierLine(line, scriptDescriptor, scriptClass, scriptInstance, scriptClassType));

      return LineResult.successful(
          rv,
          KotlinBuiltIns.getInstance()
              .getUnitType()
              .equals(scriptDescriptor.getScriptCodeDescriptor().getReturnType()));
    } catch (Throwable e) {
      PrintWriter writer = new PrintWriter(System.err);
      classLoader.dumpClasses(writer);
      writer.flush();
      throw UtilsPackage.rethrow(e);
    }
  }