001 /* 002 * Copyright 2010-2013 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.jet.codegen; 018 019 import com.intellij.util.containers.MultiMap; 020 import org.jetbrains.annotations.NotNull; 021 import org.jetbrains.asm4.Type; 022 import org.jetbrains.jet.codegen.state.GenerationState; 023 import org.jetbrains.jet.lang.psi.JetFile; 024 import org.jetbrains.jet.lang.psi.JetPsiUtil; 025 import org.jetbrains.jet.lang.psi.JetScript; 026 import org.jetbrains.jet.lang.resolve.ScriptNameUtil; 027 import org.jetbrains.jet.lang.resolve.name.FqName; 028 029 import java.util.Collection; 030 import java.util.Map; 031 032 import static org.jetbrains.jet.codegen.binding.CodegenBinding.registerClassNameForScript; 033 034 public class KotlinCodegenFacade { 035 public static void compileCorrectFiles( 036 @NotNull GenerationState state, 037 @NotNull CompilationErrorHandler errorHandler 038 ) { 039 for (JetFile file : state.getFiles()) { 040 if (file.isScript()) { 041 String name = ScriptNameUtil.classNameForScript(file); 042 JetScript script = file.getScript(); 043 assert script != null; 044 registerClassNameForScript(state.getBindingTrace(), script, Type.getObjectType(name)); 045 } 046 } 047 048 state.beforeCompile(); 049 050 MultiMap<FqName, JetFile> packageFqNameToFiles = new MultiMap<FqName, JetFile>(); 051 for (JetFile file : state.getFiles()) { 052 if (file == null) throw new IllegalArgumentException("A null file given for compilation"); 053 packageFqNameToFiles.putValue(JetPsiUtil.getFQName(file), file); 054 } 055 056 for (Map.Entry<FqName, Collection<JetFile>> entry : packageFqNameToFiles.entrySet()) { 057 generatePackage(state, entry.getKey(), entry.getValue(), errorHandler); 058 } 059 060 state.getFactory().done(); 061 } 062 063 public static void generatePackage( 064 @NotNull GenerationState state, 065 @NotNull FqName fqName, 066 @NotNull Collection<JetFile> jetFiles, 067 @NotNull CompilationErrorHandler errorHandler 068 ) { 069 PackageCodegen codegen = state.getFactory().forPackage(fqName, jetFiles); 070 codegen.generate(errorHandler); 071 } 072 073 private KotlinCodegenFacade() {} 074 }