KotlinCacheManager and LightClassGenerationSupport services added
This commit is contained in:
+70
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2010-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.cli.jvm.compiler;
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.components.ServiceManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.asJava.LightClassConstructionContext;
|
||||
import org.jetbrains.jet.asJava.LightClassGenerationSupport;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.resolve.BindingTrace;
|
||||
import org.jetbrains.jet.lang.resolve.BindingTraceContext;
|
||||
|
||||
/**
|
||||
* This class solves the problem of interdependency between analyzing Kotlin code and generating JetLightClasses
|
||||
*
|
||||
* Consider the following example:
|
||||
*
|
||||
* KClass.kt refers to JClass.java and vice versa
|
||||
*
|
||||
* To analyze KClass.kt we need to load descriptors from JClass.java, and to do that we need a JetLightClass instance for KClass,
|
||||
* which can only be constructed when the structure of KClass is known.
|
||||
*
|
||||
* To mitigate this, CliLightClassGenerationSupport hold a trace that is shared between the analyzer and JetLightClasses
|
||||
*/
|
||||
public class CliLightClassGenerationSupport extends LightClassGenerationSupport {
|
||||
|
||||
public static CliLightClassGenerationSupport getInstanceForCli(@NotNull Project project) {
|
||||
return ServiceManager.getService(project, CliLightClassGenerationSupport.class);
|
||||
}
|
||||
|
||||
private BindingTrace trace;
|
||||
|
||||
public CliLightClassGenerationSupport() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public BindingTrace getTrace() {
|
||||
if (trace == null) {
|
||||
trace = new BindingTraceContext();
|
||||
}
|
||||
return trace;
|
||||
}
|
||||
|
||||
public void clearBindingTrace() {
|
||||
assert ApplicationManager.getApplication().isUnitTestMode() : "Mutating project service's state shouldn't happen other than in tests";
|
||||
trace = null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public LightClassConstructionContext analyzeRelevantCode(@NotNull JetFile file) {
|
||||
return new LightClassConstructionContext(getTrace().getBindingContext(), null);
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,7 @@ import com.intellij.psi.impl.compiled.ClsCustomNavigationPolicy;
|
||||
import com.intellij.psi.impl.file.impl.JavaFileManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.asJava.JavaElementFinder;
|
||||
import org.jetbrains.jet.asJava.LightClassGenerationSupport;
|
||||
import org.jetbrains.jet.cli.common.CLIConfigurationKeys;
|
||||
import org.jetbrains.jet.cli.common.messages.CompilerMessageLocation;
|
||||
import org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity;
|
||||
@@ -86,6 +87,11 @@ public class JetCoreEnvironment {
|
||||
project.registerService(JetScriptDefinitionProvider.class, new JetScriptDefinitionProvider());
|
||||
project.registerService(JetFilesProvider.class, new CliJetFilesProvider(this));
|
||||
project.registerService(CoreJavaFileManager.class, (CoreJavaFileManager) ServiceManager.getService(project, JavaFileManager.class));
|
||||
|
||||
CliLightClassGenerationSupport cliLightClassGenerationSupport = new CliLightClassGenerationSupport();
|
||||
project.registerService(LightClassGenerationSupport.class, cliLightClassGenerationSupport);
|
||||
project.registerService(CliLightClassGenerationSupport.class, cliLightClassGenerationSupport);
|
||||
|
||||
Extensions.getArea(project)
|
||||
.getExtensionPoint(PsiElementFinder.EP_NAME)
|
||||
.registerExtension(new JavaElementFinder(project));
|
||||
|
||||
+5
-1
@@ -44,6 +44,7 @@ import org.jetbrains.jet.lang.parsing.JetScriptDefinitionProvider;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUtil;
|
||||
import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter;
|
||||
import org.jetbrains.jet.lang.resolve.BindingTrace;
|
||||
import org.jetbrains.jet.lang.resolve.ScriptNameUtil;
|
||||
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM;
|
||||
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
|
||||
@@ -311,11 +312,14 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
@NotNull
|
||||
@Override
|
||||
public AnalyzeExhaust invoke() {
|
||||
BindingTrace sharedTrace = CliLightClassGenerationSupport.getInstanceForCli(environment.getProject()).getTrace();
|
||||
return AnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(
|
||||
environment.getProject(),
|
||||
environment.getSourceFiles(),
|
||||
sharedTrace,
|
||||
scriptParameters,
|
||||
filesToAnalyzeCompletely
|
||||
filesToAnalyzeCompletely,
|
||||
false
|
||||
);
|
||||
}
|
||||
}, environment.getSourceFiles()
|
||||
|
||||
+14
-2
@@ -189,6 +189,18 @@ public enum AnalyzerFacadeForJVM implements AnalyzerFacade {
|
||||
boolean storeContextForBodiesResolve) {
|
||||
BindingTraceContext bindingTraceContext = new BindingTraceContext();
|
||||
|
||||
return analyzeFilesWithJavaIntegration(project, files, bindingTraceContext, scriptParameters, filesToAnalyzeCompletely,
|
||||
storeContextForBodiesResolve);
|
||||
}
|
||||
|
||||
public static AnalyzeExhaust analyzeFilesWithJavaIntegration(
|
||||
Project project,
|
||||
Collection<JetFile> files,
|
||||
BindingTrace trace,
|
||||
List<AnalyzerScriptParameter> scriptParameters,
|
||||
Predicate<PsiFile> filesToAnalyzeCompletely,
|
||||
boolean storeContextForBodiesResolve
|
||||
) {
|
||||
final ModuleDescriptor owner = new ModuleDescriptor(Name.special("<module>"));
|
||||
|
||||
TopDownAnalysisParameters topDownAnalysisParameters = new TopDownAnalysisParameters(
|
||||
@@ -196,13 +208,13 @@ public enum AnalyzerFacadeForJVM implements AnalyzerFacade {
|
||||
|
||||
InjectorForTopDownAnalyzerForJvm injector = new InjectorForTopDownAnalyzerForJvm(
|
||||
project, topDownAnalysisParameters,
|
||||
new ObservableBindingTrace(bindingTraceContext), owner);
|
||||
new ObservableBindingTrace(trace), owner);
|
||||
try {
|
||||
injector.getTopDownAnalyzer().analyzeFiles(files, scriptParameters);
|
||||
BodiesResolveContext bodiesResolveContext = storeContextForBodiesResolve ?
|
||||
new CachedBodiesResolveContext(injector.getTopDownAnalysisContext()) :
|
||||
null;
|
||||
return AnalyzeExhaust.success(bindingTraceContext.getBindingContext(), bodiesResolveContext, injector.getModuleConfiguration());
|
||||
return AnalyzeExhaust.success(trace.getBindingContext(), bodiesResolveContext, injector.getModuleConfiguration());
|
||||
} finally {
|
||||
injector.destroy();
|
||||
}
|
||||
|
||||
@@ -36,7 +36,6 @@ import com.intellij.psi.util.*;
|
||||
import com.intellij.util.containers.Stack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.analyzer.AnalyzeExhaust;
|
||||
import org.jetbrains.jet.codegen.ClassBuilder;
|
||||
import org.jetbrains.jet.codegen.ClassBuilderFactory;
|
||||
import org.jetbrains.jet.codegen.ClassBuilderMode;
|
||||
@@ -45,8 +44,6 @@ import org.jetbrains.jet.codegen.binding.PsiCodegenPredictor;
|
||||
import org.jetbrains.jet.codegen.state.GenerationState;
|
||||
import org.jetbrains.jet.codegen.state.GenerationStrategy;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM;
|
||||
import org.jetbrains.jet.lang.resolve.java.JetFilesProvider;
|
||||
import org.jetbrains.jet.lang.resolve.java.JetJavaMirrorMarker;
|
||||
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
@@ -208,15 +205,15 @@ public class JetLightClass extends AbstractLightClass implements JetJavaMirrorMa
|
||||
// The context must reflect _all files in the module_. not only the current file
|
||||
// Otherwise, the analyzer gets confused and can't, for example, tell which files come as sources and which
|
||||
// must be loaded from .class files
|
||||
AnalyzeExhaust exhaust = AnalyzerFacadeForJVM.shallowAnalyzeFiles(
|
||||
JetFilesProvider.getInstance(project).sampleToAllFilesInModule().fun(file));
|
||||
LightClassConstructionContext context = LightClassGenerationSupport.getInstance(project).analyzeRelevantCode(file);
|
||||
|
||||
if (exhaust.isError()) {
|
||||
throw new IllegalStateException("failed to analyze: " + exhaust.getError(), exhaust.getError());
|
||||
Throwable error = context.getError();
|
||||
if (error != null) {
|
||||
throw new IllegalStateException("failed to analyze: " + error, error);
|
||||
}
|
||||
|
||||
try {
|
||||
GenerationState state = new GenerationState(project, builderFactory, exhaust.getBindingContext(), Collections.singletonList(file));
|
||||
GenerationState state = new GenerationState(project, builderFactory, context.getBindingContext(), Collections.singletonList(file));
|
||||
GenerationStrategy strategy = new LightClassGenerationStrategy(this, stubStack, answer);
|
||||
|
||||
strategy.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2010-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.asJava;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
|
||||
public class LightClassConstructionContext {
|
||||
private final BindingContext bindingContext;
|
||||
private final Throwable error;
|
||||
|
||||
public LightClassConstructionContext(@NotNull BindingContext bindingContext, @Nullable Throwable error) {
|
||||
this.bindingContext = bindingContext;
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public BindingContext getBindingContext() {
|
||||
return bindingContext;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Throwable getError() {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2010-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.asJava;
|
||||
|
||||
import com.intellij.openapi.components.ServiceManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
|
||||
public abstract class LightClassGenerationSupport {
|
||||
|
||||
@NotNull
|
||||
public static LightClassGenerationSupport getInstance(@NotNull Project project) {
|
||||
return ServiceManager.getService(project, LightClassGenerationSupport.class);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public abstract LightClassConstructionContext analyzeRelevantCode(@NotNull JetFile file);
|
||||
}
|
||||
Reference in New Issue
Block a user