diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/binding/PsiCodegenPredictor.java b/compiler/backend/src/org/jetbrains/jet/codegen/binding/PsiCodegenPredictor.java index fd7690d99d7..de4ffae0d69 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/binding/PsiCodegenPredictor.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/binding/PsiCodegenPredictor.java @@ -26,6 +26,7 @@ import org.jetbrains.jet.codegen.PackageCodegen; import org.jetbrains.jet.codegen.state.GenerationState; import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; +import org.jetbrains.jet.lang.descriptors.ModuleDescriptor; import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContextUtils; @@ -144,13 +145,14 @@ public final class PsiCodegenPredictor { @Nullable public static JetFile getFileForCodegenNamedClass( + @NotNull ModuleDescriptor module, @NotNull BindingContext context, @NotNull Collection allPackageFiles, @NotNull String classInternalName ) { Project project = allPackageFiles.iterator().next().getProject(); - GenerationState state = - new GenerationState(project, ClassBuilderFactories.THROW_EXCEPTION, context, new ArrayList(allPackageFiles)); + GenerationState state = new GenerationState(project, ClassBuilderFactories.THROW_EXCEPTION, module, context, + new ArrayList(allPackageFiles)); state.beforeCompile(); BindingTrace trace = state.getBindingTrace(); diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/state/GenerationState.java b/compiler/backend/src/org/jetbrains/jet/codegen/state/GenerationState.java index 1e56e48b771..64f4fe64894 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/state/GenerationState.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/state/GenerationState.java @@ -24,7 +24,6 @@ import org.jetbrains.jet.codegen.binding.CodegenBinding; import org.jetbrains.jet.codegen.inline.InlineCodegenUtil; import org.jetbrains.jet.codegen.intrinsics.IntrinsicMethods; import org.jetbrains.jet.lang.descriptors.ModuleDescriptor; -import org.jetbrains.jet.lang.descriptors.PackageFragmentDescriptor; import org.jetbrains.jet.lang.descriptors.ScriptDescriptor; import org.jetbrains.jet.lang.psi.JetClassOrObject; import org.jetbrains.jet.lang.psi.JetFile; @@ -32,7 +31,6 @@ import org.jetbrains.jet.lang.reflect.ReflectionTypes; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingTrace; import org.jetbrains.jet.lang.resolve.DelegatingBindingTrace; -import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; import java.util.List; @@ -103,10 +101,11 @@ public class GenerationState { public GenerationState( @NotNull Project project, @NotNull ClassBuilderFactory builderFactory, + @NotNull ModuleDescriptor module, @NotNull BindingContext bindingContext, @NotNull List files ) { - this(project, builderFactory, Progress.DEAF, bindingContext, files, true, false, GenerateClassFilter.GENERATE_ALL, + this(project, builderFactory, Progress.DEAF, module, bindingContext, files, true, false, GenerateClassFilter.GENERATE_ALL, InlineCodegenUtil.DEFAULT_INLINE_FLAG); } @@ -114,6 +113,7 @@ public class GenerationState { @NotNull Project project, @NotNull ClassBuilderFactory builderFactory, @NotNull Progress progress, + @NotNull ModuleDescriptor module, @NotNull BindingContext bindingContext, @NotNull List files, boolean generateNotNullAssertions, @@ -139,25 +139,10 @@ public class GenerationState { this.generateNotNullParamAssertions = generateNotNullParamAssertions; this.generateClassFilter = generateClassFilter; - ReflectionTypes reflectionTypes = new ReflectionTypes(getAnyModule()); + ReflectionTypes reflectionTypes = new ReflectionTypes(module); this.functionImplTypes = new JvmFunctionImplTypes(reflectionTypes); } - @NotNull - private ModuleDescriptor getAnyModule() { - // TODO: this shouldn't be happening once we have modules in the compiler (there simply will be a ModuleDescriptor instance here) - - if (files.isEmpty()) { - // This is a hackish workaround for this code not to fail when invoked for an empty file list. Technically it doesn't matter - // which module we return here: if we're not compiling anything, we should never reach a point where we need this module - return KotlinBuiltIns.getInstance().getBuiltInsModule(); - } - - PackageFragmentDescriptor descriptor = bindingContext.get(BindingContext.FILE_TO_PACKAGE_FRAGMENT, files.get(0)); - assert descriptor != null : "File is not under any module: " + files.get(0); - return descriptor.getContainingDeclaration(); - } - @NotNull public ClassFileFactory getFactory() { return classFileFactory; diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/CliLightClassGenerationSupport.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/CliLightClassGenerationSupport.java index daa0d730004..f7335e87db3 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/CliLightClassGenerationSupport.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/CliLightClassGenerationSupport.java @@ -117,7 +117,7 @@ public class CliLightClassGenerationSupport extends LightClassGenerationSupport @NotNull private LightClassConstructionContext getContext() { - return new LightClassConstructionContext(getTrace().getBindingContext(), null); + return new LightClassConstructionContext(getTrace().getBindingContext(), getModule()); } @NotNull diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java index c39e7d81baf..50dd9426a78 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java @@ -304,7 +304,8 @@ public class KotlinToJVMBytecodeCompiler { private static GenerationState generate(@NotNull JetCoreEnvironment environment, @NotNull AnalyzeExhaust exhaust) { CompilerConfiguration configuration = environment.getConfiguration(); GenerationState generationState = new GenerationState( - environment.getProject(), ClassBuilderFactories.BINARIES, Progress.DEAF, exhaust.getBindingContext(), environment.getSourceFiles(), + environment.getProject(), ClassBuilderFactories.BINARIES, Progress.DEAF, + exhaust.getModuleDescriptor(), exhaust.getBindingContext(), environment.getSourceFiles(), configuration.get(JVMConfigurationKeys.GENERATE_NOT_NULL_ASSERTIONS, false), configuration.get(JVMConfigurationKeys.GENERATE_NOT_NULL_PARAMETER_ASSERTIONS, false), GenerationState.GenerateClassFilter.GENERATE_ALL, diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/repl/ReplInterpreter.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/repl/ReplInterpreter.java index 770fedd8c54..ef9644bbbed 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/repl/ReplInterpreter.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/repl/ReplInterpreter.java @@ -249,7 +249,7 @@ public class ReplInterpreter { BindingContext bindingContext = AnalyzeExhaust.success(trace.getBindingContext(), module).getBindingContext(); GenerationState generationState = new GenerationState(psiFile.getProject(), ClassBuilderFactories.BINARIES, - bindingContext, Collections.singletonList(psiFile)); + module, bindingContext, Collections.singletonList(psiFile)); compileScript(psiFile.getScript(), scriptClassType, earlierScripts, generationState, CompilationErrorHandler.THROW_EXCEPTION); diff --git a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinJavaFileStubProvider.java b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinJavaFileStubProvider.java index 0a87a36e399..d2dad0eb3b0 100644 --- a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinJavaFileStubProvider.java +++ b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinJavaFileStubProvider.java @@ -53,7 +53,6 @@ import org.jetbrains.jet.lang.psi.JetPsiUtil; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContextUtils; import org.jetbrains.jet.lang.resolve.name.FqName; -import org.jetbrains.jet.lang.types.lang.InlineUtil; import java.util.Collection; import java.util.Collections; @@ -262,10 +261,6 @@ public class KotlinJavaFileStubProvider implements Cache checkForBuiltIns(packageFqName, files); LightClassConstructionContext context = stubGenerationStrategy.getContext(files); - Throwable error = context.getError(); - if (error != null) { - throw new IllegalStateException("failed to analyze: " + error, error); - } PsiJavaFileStub javaFileStub = createJavaFileStub(packageFqName, getRepresentativeVirtualFile(files)); BindingContext bindingContext; @@ -277,11 +272,13 @@ public class KotlinJavaFileStubProvider implements Cache project, new KotlinLightClassBuilderFactory(stubStack), Progress.DEAF, + context.getModule(), context.getBindingContext(), Lists.newArrayList(files), /*not-null assertions*/false, false, /*generateClassFilter=*/stubGenerationStrategy.getGenerateClassFilter(), - /*to generate inline flag on methods*/true); + /*to generate inline flag on methods*/true + ); state.beforeCompile(); bindingContext = state.getBindingContext(); diff --git a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/LightClassConstructionContext.java b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/LightClassConstructionContext.java index 8ef3070ea7e..f3c866f6bb0 100644 --- a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/LightClassConstructionContext.java +++ b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/LightClassConstructionContext.java @@ -17,16 +17,16 @@ package org.jetbrains.jet.asJava; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.descriptors.ModuleDescriptor; import org.jetbrains.jet.lang.resolve.BindingContext; public class LightClassConstructionContext { private final BindingContext bindingContext; - private final Throwable error; + private final ModuleDescriptor module; - public LightClassConstructionContext(@NotNull BindingContext bindingContext, @Nullable Throwable error) { + public LightClassConstructionContext(@NotNull BindingContext bindingContext, @NotNull ModuleDescriptor module) { this.bindingContext = bindingContext; - this.error = error; + this.module = module; } @NotNull @@ -34,8 +34,8 @@ public class LightClassConstructionContext { return bindingContext; } - @Nullable - public Throwable getError() { - return error; + @NotNull + public ModuleDescriptor getModule() { + return module; } } diff --git a/compiler/tests/org/jetbrains/jet/codegen/CodegenTestUtil.java b/compiler/tests/org/jetbrains/jet/codegen/CodegenTestUtil.java index c6047860e07..0ffe0886f10 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/CodegenTestUtil.java +++ b/compiler/tests/org/jetbrains/jet/codegen/CodegenTestUtil.java @@ -57,7 +57,8 @@ public class CodegenTestUtil { AnalyzingUtils.throwExceptionOnErrors(analyzeExhaust.getBindingContext()); CompilerConfiguration configuration = environment.getConfiguration(); GenerationState state = new GenerationState( - environment.getProject(), ClassBuilderFactories.TEST, Progress.DEAF, analyzeExhaust.getBindingContext(), files.getPsiFiles(), + environment.getProject(), ClassBuilderFactories.TEST, Progress.DEAF, + analyzeExhaust.getModuleDescriptor(), analyzeExhaust.getBindingContext(), files.getPsiFiles(), configuration.get(JVMConfigurationKeys.GENERATE_NOT_NULL_ASSERTIONS, true), configuration.get(JVMConfigurationKeys.GENERATE_NOT_NULL_PARAMETER_ASSERTIONS, true), GenerationState.GenerateClassFilter.GENERATE_ALL, diff --git a/compiler/tests/org/jetbrains/jet/codegen/GenerationUtils.java b/compiler/tests/org/jetbrains/jet/codegen/GenerationUtils.java index 169e22c8cd1..7ae0518a0b7 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/GenerationUtils.java +++ b/compiler/tests/org/jetbrains/jet/codegen/GenerationUtils.java @@ -48,14 +48,18 @@ public class GenerationUtils { public static GenerationState compileManyFilesGetGenerationStateForTest(@NotNull Project project, @NotNull List files) { AnalyzeExhaust analyzeExhaust = JvmResolveUtil.analyzeFilesWithJavaIntegrationAndCheckForErrors( project, files, Predicates.alwaysTrue()); - return compileFilesGetGenerationState(project,analyzeExhaust, files); + return compileFilesGetGenerationState(project, analyzeExhaust, files); } - @NotNull - public static GenerationState compileFilesGetGenerationState(@NotNull Project project, @NotNull AnalyzeExhaust analyzeExhaust, @NotNull List files) { + public static GenerationState compileFilesGetGenerationState( + @NotNull Project project, + @NotNull AnalyzeExhaust analyzeExhaust, + @NotNull List files + ) { analyzeExhaust.throwIfError(); - GenerationState state = new GenerationState(project, ClassBuilderFactories.TEST, analyzeExhaust.getBindingContext(), files); + GenerationState state = new GenerationState(project, ClassBuilderFactories.TEST, analyzeExhaust.getModuleDescriptor(), + analyzeExhaust.getBindingContext(), files); KotlinCodegenFacade.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION); return state; } diff --git a/idea/src/org/jetbrains/jet/plugin/caches/resolve/IDELightClassGenerationSupport.java b/idea/src/org/jetbrains/jet/plugin/caches/resolve/IDELightClassGenerationSupport.java index a1a589a95b3..52e399e17d0 100644 --- a/idea/src/org/jetbrains/jet/plugin/caches/resolve/IDELightClassGenerationSupport.java +++ b/idea/src/org/jetbrains/jet/plugin/caches/resolve/IDELightClassGenerationSupport.java @@ -29,7 +29,10 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.asJava.KotlinLightClassForExplicitDeclaration; import org.jetbrains.jet.asJava.LightClassConstructionContext; import org.jetbrains.jet.asJava.LightClassGenerationSupport; -import org.jetbrains.jet.lang.descriptors.*; +import org.jetbrains.jet.lang.descriptors.ClassDescriptor; +import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; +import org.jetbrains.jet.lang.descriptors.PackageViewDescriptor; +import org.jetbrains.jet.lang.descriptors.VariableDescriptor; import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.java.PackageClassUtils; @@ -79,16 +82,14 @@ public class IDELightClassGenerationSupport extends LightClassGenerationSupport @NotNull @Override public LightClassConstructionContext getContextForPackage(@NotNull Collection files) { - if (files.isEmpty()) { - return new LightClassConstructionContext(BindingContext.EMPTY, null); - } + assert !files.isEmpty() : "No files in package"; List sortedFiles = new ArrayList(files); Collections.sort(sortedFiles, jetFileComparator); ResolveSessionForBodies session = ResolvePackage.getLazyResolveSession(sortedFiles.get(0)); forceResolvePackageDeclarations(files, session); - return new LightClassConstructionContext(session.getBindingContext(), null); + return new LightClassConstructionContext(session.getBindingContext(), session.getModuleDescriptor()); } @NotNull @@ -102,16 +103,16 @@ public class IDELightClassGenerationSupport extends LightClassGenerationSupport if (descriptor == null) { LOG.warn("No class descriptor in context for class: " + JetPsiUtil.getElementTextWithContext(classOrObject)); - return new LightClassConstructionContext(BindingContext.EMPTY, null); + return new LightClassConstructionContext(BindingContext.EMPTY, session.getModuleDescriptor()); } ForceResolveUtil.forceResolveAllContents(descriptor); - return new LightClassConstructionContext(bindingContext, null); + return new LightClassConstructionContext(bindingContext, session.getModuleDescriptor()); } ForceResolveUtil.forceResolveAllContents(session.getClassDescriptor(classOrObject)); - return new LightClassConstructionContext(session.getBindingContext(), null); + return new LightClassConstructionContext(session.getBindingContext(), session.getModuleDescriptor()); } private static void forceResolvePackageDeclarations(@NotNull Collection files, @NotNull KotlinCodeAnalyzer session) { @@ -164,7 +165,7 @@ public class IDELightClassGenerationSupport extends LightClassGenerationSupport @NotNull @Override - public Collection findFilesForPackage(@NotNull final FqName fqName, @NotNull GlobalSearchScope searchScope) { + public Collection findFilesForPackage(@NotNull FqName fqName, @NotNull GlobalSearchScope searchScope) { return PackageIndexUtil.findFilesWithExactPackage(fqName, kotlinSources(searchScope), project); } diff --git a/idea/src/org/jetbrains/jet/plugin/debugger/JetPositionManager.java b/idea/src/org/jetbrains/jet/plugin/debugger/JetPositionManager.java index 8ef0bf6665f..1633d454739 100644 --- a/idea/src/org/jetbrains/jet/plugin/debugger/JetPositionManager.java +++ b/idea/src/org/jetbrains/jet/plugin/debugger/JetPositionManager.java @@ -201,7 +201,8 @@ public class JetPositionManager implements PositionManager { analyzeExhaust.throwIfError(); GenerationState state = new GenerationState(file.getProject(), ClassBuilderFactories.THROW_EXCEPTION, - analyzeExhaust.getBindingContext(), new ArrayList(packageFiles)); + analyzeExhaust.getModuleDescriptor(), analyzeExhaust.getBindingContext(), + new ArrayList(packageFiles)); state.beforeCompile(); return new Result(state.getTypeMapper(), PsiModificationTracker.MODIFICATION_COUNT); } diff --git a/idea/src/org/jetbrains/jet/plugin/debugger/evaluate/KotlinEvaluationBuilder.kt b/idea/src/org/jetbrains/jet/plugin/debugger/evaluate/KotlinEvaluationBuilder.kt index 227c5021a32..7cd9838551f 100644 --- a/idea/src/org/jetbrains/jet/plugin/debugger/evaluate/KotlinEvaluationBuilder.kt +++ b/idea/src/org/jetbrains/jet/plugin/debugger/evaluate/KotlinEvaluationBuilder.kt @@ -187,6 +187,7 @@ class KotlinEvaluator(val codeFragment: JetCodeFragment, val state = GenerationState( file.getProject(), ClassBuilderFactories.BINARIES, + analyzeExhaust.getModuleDescriptor(), bindingContext, listOf(file) ) diff --git a/idea/src/org/jetbrains/jet/plugin/internal/codewindow/BytecodeToolWindow.java b/idea/src/org/jetbrains/jet/plugin/internal/codewindow/BytecodeToolWindow.java index 39825ac6b91..68a1352597c 100644 --- a/idea/src/org/jetbrains/jet/plugin/internal/codewindow/BytecodeToolWindow.java +++ b/idea/src/org/jetbrains/jet/plugin/internal/codewindow/BytecodeToolWindow.java @@ -108,7 +108,8 @@ public class BytecodeToolWindow extends JPanel implements Disposable { if (exhaust.isError()) { return printStackTraceToString(exhaust.getError()); } - state = new GenerationState(jetFile.getProject(), ClassBuilderFactories.TEST, Progress.DEAF, exhaust.getBindingContext(), + state = new GenerationState(jetFile.getProject(), ClassBuilderFactories.TEST, Progress.DEAF, + exhaust.getModuleDescriptor(), exhaust.getBindingContext(), Collections.singletonList(jetFile), true, true, GenerationState.GenerateClassFilter.GENERATE_ALL, enableInline.isSelected()); diff --git a/idea/src/org/jetbrains/jet/plugin/util/DebuggerUtils.java b/idea/src/org/jetbrains/jet/plugin/util/DebuggerUtils.java index c2bf5f6d798..ad3255d4c3c 100644 --- a/idea/src/org/jetbrains/jet/plugin/util/DebuggerUtils.java +++ b/idea/src/org/jetbrains/jet/plugin/util/DebuggerUtils.java @@ -74,7 +74,8 @@ public class DebuggerUtils { // TODO: this code is not entirely correct, because it takes a session for only one file AnalyzeExhaust analyzeExhaust = ResolvePackage.getAnalysisResultsForElements(files); - return PsiCodegenPredictor.getFileForCodegenNamedClass(analyzeExhaust.getBindingContext(), allPackageFiles, className.getInternalName()); + return PsiCodegenPredictor.getFileForCodegenNamedClass(analyzeExhaust.getModuleDescriptor(), analyzeExhaust.getBindingContext(), + allPackageFiles, className.getInternalName()); } @NotNull