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.asm4.Type;
023 import org.jetbrains.jet.codegen.state.GenerationState;
024 import org.jetbrains.jet.lang.descriptors.ScriptDescriptor;
025 import org.jetbrains.jet.lang.psi.JetFile;
026 import org.jetbrains.jet.lang.psi.JetPsiUtil;
027 import org.jetbrains.jet.lang.psi.JetScript;
028 import org.jetbrains.jet.lang.resolve.ScriptNameUtil;
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, Type.getObjectType(name));
048 }
049 }
050
051 state.beforeCompile();
052
053 MultiMap<FqName, JetFile> namespaceGrouping = new MultiMap<FqName, JetFile>();
054 for (JetFile file : state.getFiles()) {
055 if (file == null) throw new IllegalArgumentException("A null file given for compilation");
056 namespaceGrouping.putValue(JetPsiUtil.getFQName(file), file);
057 }
058
059 for (Map.Entry<FqName, Collection<JetFile>> entry : namespaceGrouping.entrySet()) {
060 generateNamespace(state, entry.getKey(), entry.getValue(), errorHandler);
061 }
062 }
063
064 public static void generateNamespace(
065 @NotNull GenerationState state,
066 @NotNull FqName fqName,
067 @NotNull Collection<JetFile> jetFiles,
068 @NotNull CompilationErrorHandler errorHandler
069 ) {
070 NamespaceCodegen codegen = state.getFactory().forNamespace(fqName, jetFiles);
071 codegen.generate(errorHandler);
072 }
073
074 private KotlinCodegenFacade() {}
075 }