001 /* 002 * Copyright 2010-2015 JetBrains s.r.o. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.jetbrains.kotlin.codegen; 018 019 import com.google.common.collect.Sets; 020 import com.intellij.util.containers.MultiMap; 021 import org.jetbrains.annotations.NotNull; 022 import org.jetbrains.kotlin.codegen.state.GenerationState; 023 import org.jetbrains.kotlin.fileClasses.JvmFileClassInfo; 024 import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus; 025 import org.jetbrains.kotlin.name.FqName; 026 import org.jetbrains.kotlin.psi.KtFile; 027 import org.jetbrains.kotlin.psi.KtScript; 028 import org.jetbrains.kotlin.resolve.ScriptNameUtil; 029 import org.jetbrains.org.objectweb.asm.Type; 030 031 import java.util.*; 032 033 import static org.jetbrains.kotlin.codegen.binding.CodegenBinding.registerClassNameForScript; 034 035 public class KotlinCodegenFacade { 036 037 public static void prepareForCompilation(@NotNull GenerationState state) { 038 for (KtFile file : state.getFiles()) { 039 if (file.isScript()) { 040 // SCRIPT: register class name for scripting from this file, move outside of this function 041 KtScript script = file.getScript(); 042 assert script != null; 043 044 FqName name = ScriptNameUtil.classNameForScript(script); 045 Type type = AsmUtil.asmTypeByFqNameWithoutInnerClasses(name); 046 registerClassNameForScript(state.getBindingTrace(), script, type, state.getFileClassesProvider()); 047 } 048 } 049 050 state.beforeCompile(); 051 } 052 053 public static void compileCorrectFiles( 054 @NotNull GenerationState state, 055 @NotNull CompilationErrorHandler errorHandler 056 ) { 057 ProgressIndicatorAndCompilationCanceledStatus.checkCanceled(); 058 059 prepareForCompilation(state); 060 061 ProgressIndicatorAndCompilationCanceledStatus.checkCanceled(); 062 063 doGenerateFiles(state.getFiles(), state, errorHandler); 064 } 065 066 public static void doGenerateFiles( 067 @NotNull Collection<KtFile> files, 068 @NotNull GenerationState state, 069 @NotNull CompilationErrorHandler errorHandler 070 ) { 071 MultiMap<FqName, KtFile> filesInPackages = new MultiMap<FqName, KtFile>(); 072 MultiMap<FqName, KtFile> filesInMultifileClasses = new MultiMap<FqName, KtFile>(); 073 074 for (KtFile file : files) { 075 if (file == null) throw new IllegalArgumentException("A null file given for compilation"); 076 077 JvmFileClassInfo fileClassInfo = state.getFileClassesProvider().getFileClassInfo(file); 078 079 if (fileClassInfo.getWithJvmMultifileClass()) { 080 filesInMultifileClasses.putValue(fileClassInfo.getFacadeClassFqName(), file); 081 } 082 else { 083 filesInPackages.putValue(file.getPackageFqName(), file); 084 } 085 } 086 087 Set<FqName> obsoleteMultifileClasses = new HashSet<FqName>(state.getObsoleteMultifileClasses()); 088 for (FqName multifileClassFqName : Sets.union(filesInMultifileClasses.keySet(), obsoleteMultifileClasses)) { 089 doCheckCancelled(state); 090 generateMultifileClass(state, multifileClassFqName, filesInMultifileClasses.get(multifileClassFqName), errorHandler); 091 } 092 093 Set<FqName> packagesWithObsoleteParts = new HashSet<FqName>(state.getPackagesWithObsoleteParts()); 094 for (FqName packageFqName : Sets.union(packagesWithObsoleteParts, filesInPackages.keySet())) { 095 doCheckCancelled(state); 096 generatePackage(state, packageFqName, filesInPackages.get(packageFqName), errorHandler); 097 } 098 099 doCheckCancelled(state); 100 state.getFactory().done(); 101 } 102 103 private static void doCheckCancelled(GenerationState state) { 104 if (state.getClassBuilderMode() == ClassBuilderMode.FULL) { 105 ProgressIndicatorAndCompilationCanceledStatus.checkCanceled(); 106 } 107 } 108 109 public static void generatePackage( 110 @NotNull GenerationState state, 111 @NotNull FqName packageFqName, 112 @NotNull Collection<KtFile> jetFiles, 113 @NotNull CompilationErrorHandler errorHandler 114 ) { 115 // We do not really generate package class, but use old package fqName to identify package in module-info. 116 //FqName packageClassFqName = PackageClassUtils.getPackageClassFqName(packageFqName); 117 PackageCodegen codegen = state.getFactory().forPackage(packageFqName, jetFiles); 118 codegen.generate(errorHandler); 119 } 120 121 private static void generateMultifileClass( 122 @NotNull GenerationState state, 123 @NotNull FqName multifileClassFqName, 124 @NotNull Collection<KtFile> files, 125 @NotNull CompilationErrorHandler handler 126 ) { 127 MultifileClassCodegen codegen = state.getFactory().forMultifileClass(multifileClassFqName, files); 128 codegen.generate(handler); 129 } 130 131 private KotlinCodegenFacade() {} 132 }