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.org.objectweb.asm.Type;
022 import org.jetbrains.jet.codegen.state.GenerationState;
023 import org.jetbrains.jet.lang.psi.JetFile;
024 import org.jetbrains.jet.lang.psi.JetScript;
025 import org.jetbrains.jet.lang.resolve.ScriptNameUtil;
026 import org.jetbrains.jet.lang.resolve.name.FqName;
027
028 import java.util.Collection;
029 import java.util.Map;
030
031 import static org.jetbrains.jet.codegen.binding.CodegenBinding.registerClassNameForScript;
032
033 public class KotlinCodegenFacade {
034 public static void compileCorrectFiles(
035 @NotNull GenerationState state,
036 @NotNull CompilationErrorHandler errorHandler
037 ) {
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 MultiMap<FqName, JetFile> packageFqNameToFiles = new MultiMap<FqName, JetFile>();
053 for (JetFile file : state.getFiles()) {
054 if (file == null) throw new IllegalArgumentException("A null file given for compilation");
055 packageFqNameToFiles.putValue(file.getPackageFqName(), file);
056 }
057
058 for (Map.Entry<FqName, Collection<JetFile>> entry : packageFqNameToFiles.entrySet()) {
059 generatePackage(state, entry.getKey(), entry.getValue(), errorHandler);
060 }
061
062 state.getFactory().done();
063 }
064
065 public static void generatePackage(
066 @NotNull GenerationState state,
067 @NotNull FqName fqName,
068 @NotNull Collection<JetFile> jetFiles,
069 @NotNull CompilationErrorHandler errorHandler
070 ) {
071 PackageCodegen codegen = state.getFactory().forPackage(fqName, jetFiles);
072 codegen.generate(errorHandler);
073 }
074
075 private KotlinCodegenFacade() {}
076 }