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