Pass module to GenerationState

- get rid of a hack there which was used to obtain any module, to be used in
  ReflectionTypes
- delete unused getError() in LightClassConstructionContext which was always
  null
- fix some minor warnings
This commit is contained in:
Alexander Udalov
2014-05-12 19:56:50 +04:00
parent b27b8bf8b2
commit 357fe5cb17
14 changed files with 49 additions and 54 deletions
@@ -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<JetFile> allPackageFiles,
@NotNull String classInternalName
) {
Project project = allPackageFiles.iterator().next().getProject();
GenerationState state =
new GenerationState(project, ClassBuilderFactories.THROW_EXCEPTION, context, new ArrayList<JetFile>(allPackageFiles));
GenerationState state = new GenerationState(project, ClassBuilderFactories.THROW_EXCEPTION, module, context,
new ArrayList<JetFile>(allPackageFiles));
state.beforeCompile();
BindingTrace trace = state.getBindingTrace();
@@ -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<JetFile> 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<JetFile> 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;
@@ -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
@@ -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,
@@ -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);
@@ -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<T extends WithFileStub> 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<T extends WithFileStub> 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();
@@ -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;
}
}
@@ -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,
@@ -48,14 +48,18 @@ public class GenerationUtils {
public static GenerationState compileManyFilesGetGenerationStateForTest(@NotNull Project project, @NotNull List<JetFile> files) {
AnalyzeExhaust analyzeExhaust = JvmResolveUtil.analyzeFilesWithJavaIntegrationAndCheckForErrors(
project, files, Predicates.<PsiFile>alwaysTrue());
return compileFilesGetGenerationState(project,analyzeExhaust, files);
return compileFilesGetGenerationState(project, analyzeExhaust, files);
}
@NotNull
public static GenerationState compileFilesGetGenerationState(@NotNull Project project, @NotNull AnalyzeExhaust analyzeExhaust, @NotNull List<JetFile> files) {
public static GenerationState compileFilesGetGenerationState(
@NotNull Project project,
@NotNull AnalyzeExhaust analyzeExhaust,
@NotNull List<JetFile> 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;
}
@@ -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<JetFile> files) {
if (files.isEmpty()) {
return new LightClassConstructionContext(BindingContext.EMPTY, null);
}
assert !files.isEmpty() : "No files in package";
List<JetFile> sortedFiles = new ArrayList<JetFile>(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<JetFile> files, @NotNull KotlinCodeAnalyzer session) {
@@ -164,7 +165,7 @@ public class IDELightClassGenerationSupport extends LightClassGenerationSupport
@NotNull
@Override
public Collection<JetFile> findFilesForPackage(@NotNull final FqName fqName, @NotNull GlobalSearchScope searchScope) {
public Collection<JetFile> findFilesForPackage(@NotNull FqName fqName, @NotNull GlobalSearchScope searchScope) {
return PackageIndexUtil.findFilesWithExactPackage(fqName, kotlinSources(searchScope), project);
}
@@ -201,7 +201,8 @@ public class JetPositionManager implements PositionManager {
analyzeExhaust.throwIfError();
GenerationState state = new GenerationState(file.getProject(), ClassBuilderFactories.THROW_EXCEPTION,
analyzeExhaust.getBindingContext(), new ArrayList<JetFile>(packageFiles));
analyzeExhaust.getModuleDescriptor(), analyzeExhaust.getBindingContext(),
new ArrayList<JetFile>(packageFiles));
state.beforeCompile();
return new Result<JetTypeMapper>(state.getTypeMapper(), PsiModificationTracker.MODIFICATION_COUNT);
}
@@ -187,6 +187,7 @@ class KotlinEvaluator(val codeFragment: JetCodeFragment,
val state = GenerationState(
file.getProject(),
ClassBuilderFactories.BINARIES,
analyzeExhaust.getModuleDescriptor(),
bindingContext,
listOf(file)
)
@@ -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());
@@ -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