KotlinCacheManager and LightClassGenerationSupport services added

This commit is contained in:
Andrey Breslav
2012-12-17 17:31:56 +04:00
parent b5aa44ae49
commit 7c4d1e6b09
12 changed files with 371 additions and 11 deletions
@@ -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));
@@ -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()
@@ -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);
@@ -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;
}
}
@@ -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);
}
+6
View File
@@ -79,6 +79,12 @@
<projectService serviceInterface="org.jetbrains.jet.lang.resolve.java.JetFilesProvider"
serviceImplementation="org.jetbrains.jet.plugin.project.PluginJetFilesProvider"/>
<projectService serviceInterface="org.jetbrains.jet.plugin.caches.resolve.KotlinCacheManager"
serviceImplementation="org.jetbrains.jet.plugin.caches.resolve.KotlinCacheManager"/>
<projectService serviceInterface="org.jetbrains.jet.asJava.LightClassGenerationSupport"
serviceImplementation="org.jetbrains.jet.plugin.caches.resolve.IDELightClassGenerationSupport"/>
<projectService serviceInterface="org.jetbrains.jet.lang.parsing.JetScriptDefinitionProvider"
serviceImplementation="org.jetbrains.jet.lang.parsing.JetScriptDefinitionProvider"/>
@@ -0,0 +1,57 @@
/*
* 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.plugin.caches.resolve;
import com.intellij.openapi.diagnostic.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.analyzer.AnalyzeExhaust;
import org.jetbrains.jet.lang.ModuleConfiguration;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BodiesResolveContext;
public class AnalyzeExhaustAsKotlinDeclarationsCache implements KotlinDeclarationsCache {
private static final Logger LOG = Logger.getInstance(AnalyzeExhaustAsKotlinDeclarationsCache.class);
private final AnalyzeExhaust exhaust;
public AnalyzeExhaustAsKotlinDeclarationsCache(@NotNull AnalyzeExhaust exhaust) {
this.exhaust = exhaust;
if (exhaust.isError()) {
LOG.error(exhaust.getError());
}
}
@Nullable
@Override
public BodiesResolveContext getBodiesResolveContext() {
return exhaust.getBodiesResolveContext();
}
@Override
@NotNull
public BindingContext getBindingContext() {
return exhaust.getBindingContext();
}
@Override
@NotNull
public ModuleConfiguration getModuleConfiguration() {
return exhaust.getModuleConfiguration();
}
}
@@ -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.plugin.caches.resolve;
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;
public class IDELightClassGenerationSupport extends LightClassGenerationSupport {
@NotNull
@Override
public LightClassConstructionContext analyzeRelevantCode(@NotNull JetFile file) {
Project project = file.getProject();
KotlinDeclarationsCache cache = KotlinCacheManager.getInstance(project).getDeclarationsFromProject(project);
return new LightClassConstructionContext(cache.getBindingContext(), null);
}
}
@@ -0,0 +1,83 @@
/*
* 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.plugin.caches.resolve;
import com.google.common.base.Predicates;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.CachedValue;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.psi.util.PsiModificationTracker;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.analyzer.AnalyzeExhaust;
import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter;
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM;
import org.jetbrains.jet.lang.resolve.java.JetFilesProvider;
import java.util.Collections;
public class KotlinCacheManager {
public static KotlinCacheManager getInstance(@NotNull Project project) {
return ServiceManager.getService(project, KotlinCacheManager.class);
}
private final Key<CachedValue<KotlinDeclarationsCache>> KOTLIN_DECLARATIONS_CACHE = Key.create("KOTLIN_DECLARATIONS_CACHE");
private final Project project;
private final Object declarationAnalysisLock = new Object();
public KotlinCacheManager(@NotNull Project project) {
this.project = project;
}
@NotNull
public KotlinDeclarationsCache getDeclarationsFromProject(@NotNull Project project) {
synchronized (declarationAnalysisLock) {
return CachedValuesManager.getManager(project).getCachedValue(
project,
KOTLIN_DECLARATIONS_CACHE,
new KotlinDeclarationsCacheProvider(),
false
);
}
}
private class KotlinDeclarationsCacheProvider implements CachedValueProvider<KotlinDeclarationsCache> {
@Nullable
@Override
public Result<KotlinDeclarationsCache> compute() {
AnalyzeExhaust analyzeExhaust = AnalyzerFacadeForJVM.INSTANCE.analyzeFiles(
project,
JetFilesProvider.getInstance(project).allInScope(GlobalSearchScope.allScope(project)),
Collections.<AnalyzerScriptParameter>emptyList(),
Predicates.<PsiFile>alwaysFalse()
);
return Result.<KotlinDeclarationsCache>create(
new AnalyzeExhaustAsKotlinDeclarationsCache(analyzeExhaust),
PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT
);
}
}
}
@@ -0,0 +1,18 @@
package org.jetbrains.jet.plugin.caches.resolve;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.ModuleConfiguration;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BodiesResolveContext;
public interface KotlinDeclarationsCache {
@NotNull
BindingContext getBindingContext();
@Nullable
BodiesResolveContext getBodiesResolveContext();
@NotNull @Deprecated // ModuleConfiguration must be obtained from the module
ModuleConfiguration getModuleConfiguration();
}