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.fileClasses.JvmFileClassInfo;
024    import org.jetbrains.kotlin.load.kotlin.PackageClassUtils;
025    import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus;
026    import org.jetbrains.kotlin.name.FqName;
027    import org.jetbrains.kotlin.psi.JetFile;
028    import org.jetbrains.kotlin.psi.JetScript;
029    import org.jetbrains.kotlin.resolve.ScriptNameUtil;
030    import org.jetbrains.org.objectweb.asm.Type;
031    
032    import java.util.Collection;
033    import java.util.HashSet;
034    import java.util.Set;
035    
036    import static org.jetbrains.kotlin.codegen.binding.CodegenBinding.registerClassNameForScript;
037    
038    public class KotlinCodegenFacade {
039    
040        public static void prepareForCompilation(@NotNull GenerationState state) {
041            for (JetFile file : state.getFiles()) {
042                if (file.isScript()) {
043                    // SCRIPT: register class name for scripting from this file, move outside of this function
044                    JetScript script = file.getScript();
045                    assert script != null;
046    
047                    FqName name = ScriptNameUtil.classNameForScript(script);
048                    Type type = AsmUtil.asmTypeByFqNameWithoutInnerClasses(name);
049                    registerClassNameForScript(state.getBindingTrace(), script, type, state.getFileClassesProvider());
050                }
051            }
052    
053            state.beforeCompile();
054        }
055    
056        public static void compileCorrectFiles(
057                @NotNull GenerationState state,
058                @NotNull CompilationErrorHandler errorHandler
059        ) {
060            ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
061    
062            prepareForCompilation(state);
063    
064            ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
065    
066            MultiMap<FqName, JetFile> filesInPackageClasses = new MultiMap<FqName, JetFile>();
067            MultiMap<FqName, JetFile> filesInMultifileClasses = new MultiMap<FqName, JetFile>();
068    
069            for (JetFile file : state.getFiles()) {
070                if (file == null) throw new IllegalArgumentException("A null file given for compilation");
071    
072                JvmFileClassInfo fileClassInfo = state.getFileClassesProvider().getFileClassInfo(file);
073    
074                if (fileClassInfo.getIsMultifileClass()) {
075                    filesInMultifileClasses.putValue(fileClassInfo.getFacadeClassFqName(), file);
076                }
077    
078                if (state.getPackageFacadesAsMultifileClasses()) {
079                    if (!fileClassInfo.getIsMultifileClass()) {
080                        filesInMultifileClasses.putValue(PackageClassUtils.getPackageClassFqName(file.getPackageFqName()), file);
081                    }
082                }
083                else {
084                    filesInPackageClasses.putValue(file.getPackageFqName(), file);
085                }
086            }
087    
088            Set<FqName> packagesWithObsoleteParts = new HashSet<FqName>(state.getPackagesWithObsoleteParts());
089            for (FqName packageFqName : Sets.union(packagesWithObsoleteParts, filesInPackageClasses.keySet())) {
090                ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
091                generatePackage(state, packageFqName, filesInPackageClasses.get(packageFqName), errorHandler);
092            }
093    
094            for (FqName multifileClassFqName : filesInMultifileClasses.keySet()) {
095                ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
096                generateMultifileClass(state, multifileClassFqName, filesInMultifileClasses.get(multifileClassFqName), errorHandler);
097            }
098    
099            ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
100            state.getFactory().done();
101        }
102    
103        public static void generatePackage(
104                @NotNull GenerationState state,
105                @NotNull FqName packageFqName,
106                @NotNull Collection<JetFile> jetFiles,
107                @NotNull CompilationErrorHandler errorHandler
108        ) {
109            PackageCodegen codegen = state.getFactory().forPackage(packageFqName, jetFiles);
110            codegen.generate(errorHandler);
111        }
112    
113        private static void generateMultifileClass(
114                @NotNull GenerationState state,
115                @NotNull FqName multifileClassFqName,
116                @NotNull Collection<JetFile> files,
117                @NotNull CompilationErrorHandler handler
118        ) {
119            MultifileClassCodegen codegen = state.getFactory().forMultifileClass(multifileClassFqName, files);
120            codegen.generate(handler);
121        }
122    
123        private KotlinCodegenFacade() {}
124    }