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.load.kotlin.PackageClassUtils; 025 import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus; 026 import org.jetbrains.kotlin.name.FqName; 027 import org.jetbrains.kotlin.psi.JetFile; 028 import org.jetbrains.kotlin.psi.JetScript; 029 import org.jetbrains.kotlin.resolve.ScriptNameUtil; 030 import org.jetbrains.org.objectweb.asm.Type; 031 032 import java.util.Collection; 033 import java.util.HashSet; 034 import java.util.Set; 035 036 import static org.jetbrains.kotlin.codegen.binding.CodegenBinding.registerClassNameForScript; 037 038 public class KotlinCodegenFacade { 039 040 public static void prepareForCompilation(@NotNull GenerationState state) { 041 for (JetFile file : state.getFiles()) { 042 if (file.isScript()) { 043 // SCRIPT: register class name for scripting from this file, move outside of this function 044 JetScript script = file.getScript(); 045 assert script != null; 046 047 FqName name = ScriptNameUtil.classNameForScript(script); 048 Type type = AsmUtil.asmTypeByFqNameWithoutInnerClasses(name); 049 registerClassNameForScript(state.getBindingTrace(), script, type, state.getFileClassesProvider()); 050 } 051 } 052 053 state.beforeCompile(); 054 } 055 056 public static void compileCorrectFiles( 057 @NotNull GenerationState state, 058 @NotNull CompilationErrorHandler errorHandler 059 ) { 060 ProgressIndicatorAndCompilationCanceledStatus.checkCanceled(); 061 062 prepareForCompilation(state); 063 064 ProgressIndicatorAndCompilationCanceledStatus.checkCanceled(); 065 066 MultiMap<FqName, JetFile> packageFqNameToFiles = new MultiMap<FqName, JetFile>(); 067 MultiMap<FqName, JetFile> multifileClassFqNameToFiles = new MultiMap<FqName, JetFile>(); 068 069 for (JetFile file : state.getFiles()) { 070 if (file == null) throw new IllegalArgumentException("A null file given for compilation"); 071 JvmFileClassInfo fileClassInfo = state.getFileClassesProvider().getFileClassInfo(file); 072 if (fileClassInfo.getIsMultifileClass()) { 073 multifileClassFqNameToFiles.putValue(fileClassInfo.getFacadeClassFqName(), file); 074 } 075 else if (state.getPackageFacadesAsMultifileClasses()) { 076 multifileClassFqNameToFiles.putValue(PackageClassUtils.getPackageClassFqName(file.getPackageFqName()), file); 077 } 078 else { 079 packageFqNameToFiles.putValue(file.getPackageFqName(), file); 080 } 081 } 082 083 Set<FqName> packagesWithObsoleteParts = new HashSet<FqName>(state.getPackagesWithObsoleteParts()); 084 for (FqName packageFqName : Sets.union(packagesWithObsoleteParts, packageFqNameToFiles.keySet())) { 085 ProgressIndicatorAndCompilationCanceledStatus.checkCanceled(); 086 generatePackage(state, packageFqName, packageFqNameToFiles.get(packageFqName), errorHandler); 087 } 088 089 for (FqName multifileClassFqName : multifileClassFqNameToFiles.keySet()) { 090 ProgressIndicatorAndCompilationCanceledStatus.checkCanceled(); 091 generateMultifileClass(state, multifileClassFqName, multifileClassFqNameToFiles.get(multifileClassFqName), errorHandler); 092 } 093 094 ProgressIndicatorAndCompilationCanceledStatus.checkCanceled(); 095 state.getFactory().done(); 096 } 097 098 public static void generatePackage( 099 @NotNull GenerationState state, 100 @NotNull FqName packageFqName, 101 @NotNull Collection<JetFile> jetFiles, 102 @NotNull CompilationErrorHandler errorHandler 103 ) { 104 PackageCodegen codegen = state.getFactory().forPackage(packageFqName, jetFiles); 105 codegen.generate(errorHandler); 106 } 107 108 private static void generateMultifileClass( 109 @NotNull GenerationState state, 110 @NotNull FqName multifileClassFqName, 111 @NotNull Collection<JetFile> files, 112 @NotNull CompilationErrorHandler handler 113 ) { 114 MultifileClassCodegen codegen = state.getFactory().forMultifileClass(multifileClassFqName, files); 115 codegen.generate(handler); 116 } 117 118 private KotlinCodegenFacade() {} 119 }