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