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.name.FqName; 024 import org.jetbrains.kotlin.psi.JetFile; 025 import org.jetbrains.kotlin.psi.JetScript; 026 import org.jetbrains.kotlin.resolve.ScriptNameUtil; 027 import org.jetbrains.org.objectweb.asm.Type; 028 029 import java.util.Collection; 030 import java.util.HashSet; 031 import java.util.Set; 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 (JetFile file : state.getFiles()) { 039 if (file.isScript()) { 040 // SCRIPT: register class name for scripting from this file, move outside of this function 041 JetScript 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); 047 } 048 } 049 050 state.beforeCompile(); 051 } 052 053 public static void compileCorrectFiles( 054 @NotNull GenerationState state, 055 @NotNull CompilationErrorHandler errorHandler 056 ) { 057 prepareForCompilation(state); 058 059 MultiMap<FqName, JetFile> packageFqNameToFiles = new MultiMap<FqName, JetFile>(); 060 for (JetFile file : state.getFiles()) { 061 if (file == null) throw new IllegalArgumentException("A null file given for compilation"); 062 packageFqNameToFiles.putValue(file.getPackageFqName(), file); 063 } 064 065 Set<FqName> removedPackageFiles = new HashSet<FqName>(state.getPackagesWithRemovedFiles()); 066 for (FqName fqName : Sets.union(removedPackageFiles, packageFqNameToFiles.keySet())) { 067 generatePackage(state, fqName, packageFqNameToFiles.get(fqName), errorHandler); 068 } 069 070 state.getFactory().done(); 071 } 072 073 public static void generatePackage( 074 @NotNull GenerationState state, 075 @NotNull FqName fqName, 076 @NotNull Collection<JetFile> jetFiles, 077 @NotNull CompilationErrorHandler errorHandler 078 ) { 079 PackageCodegen codegen = state.getFactory().forPackage(fqName, jetFiles); 080 codegen.generate(errorHandler); 081 } 082 083 private KotlinCodegenFacade() {} 084 }