GenerationStrategy is now a strategy
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.codegen;
|
||||
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.util.containers.MultiMap;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.codegen.state.GenerationState;
|
||||
import org.jetbrains.jet.codegen.state.GenerationStrategy;
|
||||
import org.jetbrains.jet.lang.descriptors.ScriptDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUtil;
|
||||
import org.jetbrains.jet.lang.psi.JetScript;
|
||||
import org.jetbrains.jet.lang.resolve.ScriptNameUtil;
|
||||
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.jetbrains.jet.codegen.binding.CodegenBinding.registerClassNameForScript;
|
||||
|
||||
public class KotlinCodegenFacade {
|
||||
public static void compileCorrectFiles(
|
||||
@NotNull GenerationState state,
|
||||
@NotNull GenerationStrategy strategy, @NotNull CompilationErrorHandler errorHandler
|
||||
) {
|
||||
for (JetFile file : state.getFiles()) {
|
||||
if (file.isScript()) {
|
||||
String name = ScriptNameUtil.classNameForScript(file);
|
||||
JetScript script = file.getScript();
|
||||
assert script != null;
|
||||
registerClassNameForScript(state.getBindingTrace(), script, JvmClassName.byInternalName(name));
|
||||
}
|
||||
}
|
||||
|
||||
state.getScriptCodegen().registerEarlierScripts(Collections.<Pair<ScriptDescriptor, JvmClassName>>emptyList());
|
||||
|
||||
state.beforeCompile();
|
||||
|
||||
MultiMap<FqName, JetFile> namespaceGrouping = new MultiMap<FqName, JetFile>();
|
||||
for (JetFile file : state.getFiles()) {
|
||||
if (file == null) throw new IllegalArgumentException("A null file given for compilation");
|
||||
namespaceGrouping.putValue(JetPsiUtil.getFQName(file), file);
|
||||
}
|
||||
|
||||
for (Map.Entry<FqName, Collection<JetFile>> entry : namespaceGrouping.entrySet()) {
|
||||
strategy.generateNamespace(state, entry.getKey(), entry.getValue(), errorHandler);
|
||||
}
|
||||
}
|
||||
|
||||
private KotlinCodegenFacade() {}
|
||||
}
|
||||
@@ -27,7 +27,7 @@ import org.jetbrains.jet.codegen.context.CodegenContext;
|
||||
import org.jetbrains.jet.codegen.context.ScriptContext;
|
||||
import org.jetbrains.jet.codegen.signature.JvmMethodSignature;
|
||||
import org.jetbrains.jet.codegen.state.GenerationState;
|
||||
import org.jetbrains.jet.codegen.state.GenerationStrategy;
|
||||
import org.jetbrains.jet.codegen.state.StandardGenerationStrategy;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.ScriptDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
|
||||
@@ -251,7 +251,7 @@ public class ScriptCodegen extends MemberCodegen {
|
||||
registerClassNameForScript(state.getBindingTrace(), script, className);
|
||||
|
||||
state.beforeCompile();
|
||||
GenerationStrategy.STANDARD.generateNamespace(
|
||||
StandardGenerationStrategy.INSTANCE.generateNamespace(
|
||||
state,
|
||||
JetPsiUtil.getFQName((JetFile) script.getContainingFile()),
|
||||
Collections.singleton((JetFile) script.getContainingFile()),
|
||||
|
||||
@@ -16,62 +16,18 @@
|
||||
|
||||
package org.jetbrains.jet.codegen.state;
|
||||
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.util.containers.MultiMap;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.codegen.CompilationErrorHandler;
|
||||
import org.jetbrains.jet.codegen.NamespaceCodegen;
|
||||
import org.jetbrains.jet.lang.descriptors.ScriptDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUtil;
|
||||
import org.jetbrains.jet.lang.psi.JetScript;
|
||||
import org.jetbrains.jet.lang.resolve.ScriptNameUtil;
|
||||
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.jetbrains.jet.codegen.binding.CodegenBinding.registerClassNameForScript;
|
||||
|
||||
public class GenerationStrategy {
|
||||
public static final GenerationStrategy STANDARD = new GenerationStrategy();
|
||||
|
||||
protected GenerationStrategy () {
|
||||
}
|
||||
|
||||
public void generateNamespace(GenerationState state,
|
||||
FqName fqName,
|
||||
Collection<JetFile> jetFiles,
|
||||
CompilationErrorHandler errorHandler
|
||||
) {
|
||||
NamespaceCodegen codegen = state.getFactory().forNamespace(fqName, jetFiles);
|
||||
codegen.generate(errorHandler);
|
||||
}
|
||||
|
||||
public final void compileCorrectFiles(GenerationState state, @NotNull CompilationErrorHandler errorHandler) {
|
||||
for (JetFile file : state.getFiles()) {
|
||||
if (file.isScript()) {
|
||||
String name = ScriptNameUtil.classNameForScript(file);
|
||||
JetScript script = file.getScript();
|
||||
assert script != null;
|
||||
registerClassNameForScript(state.getBindingTrace(), script, JvmClassName.byInternalName(name));
|
||||
}
|
||||
}
|
||||
|
||||
state.getScriptCodegen().registerEarlierScripts(Collections.<Pair<ScriptDescriptor, JvmClassName>>emptyList());
|
||||
|
||||
state.beforeCompile();
|
||||
|
||||
MultiMap<FqName, JetFile> namespaceGrouping = new MultiMap<FqName, JetFile>();
|
||||
for (JetFile file : state.getFiles()) {
|
||||
if (file == null) throw new IllegalArgumentException("A null file given for compilation");
|
||||
namespaceGrouping.putValue(JetPsiUtil.getFQName(file), file);
|
||||
}
|
||||
|
||||
for (Map.Entry<FqName, Collection<JetFile>> entry : namespaceGrouping.entrySet()) {
|
||||
generateNamespace(state, entry.getKey(), entry.getValue(), errorHandler);
|
||||
}
|
||||
}
|
||||
public interface GenerationStrategy {
|
||||
void generateNamespace(
|
||||
@NotNull GenerationState state,
|
||||
@NotNull FqName fqName,
|
||||
@NotNull Collection<JetFile> jetFiles,
|
||||
@NotNull CompilationErrorHandler errorHandler
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.codegen.state;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.codegen.CompilationErrorHandler;
|
||||
import org.jetbrains.jet.codegen.NamespaceCodegen;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class StandardGenerationStrategy implements GenerationStrategy {
|
||||
public static final GenerationStrategy INSTANCE = new StandardGenerationStrategy();
|
||||
|
||||
private StandardGenerationStrategy() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateNamespace(
|
||||
@NotNull GenerationState state,
|
||||
@NotNull FqName fqName,
|
||||
@NotNull Collection<JetFile> jetFiles,
|
||||
@NotNull CompilationErrorHandler errorHandler
|
||||
) {
|
||||
NamespaceCodegen codegen = state.getFactory().forNamespace(fqName, jetFiles);
|
||||
codegen.generate(errorHandler);
|
||||
}
|
||||
|
||||
}
|
||||
+2
-2
@@ -35,8 +35,8 @@ import org.jetbrains.jet.cli.common.messages.*;
|
||||
import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys;
|
||||
import org.jetbrains.jet.codegen.*;
|
||||
import org.jetbrains.jet.codegen.state.GenerationState;
|
||||
import org.jetbrains.jet.codegen.state.GenerationStrategy;
|
||||
import org.jetbrains.jet.codegen.state.Progress;
|
||||
import org.jetbrains.jet.codegen.state.StandardGenerationStrategy;
|
||||
import org.jetbrains.jet.config.CommonConfigurationKeys;
|
||||
import org.jetbrains.jet.config.CompilerConfiguration;
|
||||
import org.jetbrains.jet.lang.parsing.JetScriptDefinition;
|
||||
@@ -355,7 +355,7 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
configuration.get(JVMConfigurationKeys.GENERATE_NOT_NULL_ASSERTIONS, false),
|
||||
configuration.get(JVMConfigurationKeys.GENERATE_NOT_NULL_PARAMETER_ASSERTIONS, false)
|
||||
);
|
||||
GenerationStrategy.STANDARD.compileCorrectFiles(generationState, CompilationErrorHandler.THROW_EXCEPTION);
|
||||
KotlinCodegenFacade.compileCorrectFiles(generationState, StandardGenerationStrategy.INSTANCE, CompilationErrorHandler.THROW_EXCEPTION);
|
||||
|
||||
CompilerPluginContext context = new CompilerPluginContext(project, exhaust.getBindingContext(), environment.getSourceFiles());
|
||||
for (CompilerPlugin plugin : configuration.getList(CLIConfigurationKeys.COMPILER_PLUGINS)) {
|
||||
|
||||
@@ -35,10 +35,7 @@ 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.codegen.ClassBuilder;
|
||||
import org.jetbrains.jet.codegen.ClassBuilderFactory;
|
||||
import org.jetbrains.jet.codegen.ClassBuilderMode;
|
||||
import org.jetbrains.jet.codegen.CompilationErrorHandler;
|
||||
import org.jetbrains.jet.codegen.*;
|
||||
import org.jetbrains.jet.codegen.binding.PsiCodegenPredictor;
|
||||
import org.jetbrains.jet.codegen.state.GenerationState;
|
||||
import org.jetbrains.jet.codegen.state.GenerationStrategy;
|
||||
@@ -213,9 +210,10 @@ public class JetLightClass extends AbstractLightClass implements JetJavaMirrorMa
|
||||
GenerationState state = new GenerationState(project, builderFactory, context.getBindingContext(), Collections.singletonList(file));
|
||||
VirtualFile virtualFile = file.getVirtualFile();
|
||||
assert virtualFile != null;
|
||||
GenerationStrategy strategy = new LightClassGenerationStrategy(virtualFile, stubStack, answer);
|
||||
|
||||
strategy.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);
|
||||
GenerationStrategy strategy = new LightClassGenerationStrategy(virtualFile, stubStack, answer);
|
||||
KotlinCodegenFacade.compileCorrectFiles(state, strategy, CompilationErrorHandler.THROW_EXCEPTION);
|
||||
|
||||
state.getFactory().files();
|
||||
}
|
||||
catch (ProcessCanceledException e) {
|
||||
|
||||
+7
-6
@@ -30,12 +30,13 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.codegen.CompilationErrorHandler;
|
||||
import org.jetbrains.jet.codegen.state.GenerationState;
|
||||
import org.jetbrains.jet.codegen.state.GenerationStrategy;
|
||||
import org.jetbrains.jet.codegen.state.StandardGenerationStrategy;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
class LightClassGenerationStrategy extends GenerationStrategy {
|
||||
class LightClassGenerationStrategy implements GenerationStrategy {
|
||||
private static final Logger LOG = Logger.getInstance("#org.jetbrains.jet.asJava.LightClassGenerationStrategy");
|
||||
|
||||
private final Stack<StubElement> stubStack;
|
||||
@@ -50,10 +51,10 @@ class LightClassGenerationStrategy extends GenerationStrategy {
|
||||
|
||||
@Override
|
||||
public void generateNamespace(
|
||||
GenerationState state,
|
||||
FqName fqName,
|
||||
Collection<JetFile> namespaceFiles,
|
||||
CompilationErrorHandler errorHandler
|
||||
@NotNull GenerationState state,
|
||||
@NotNull FqName fqName,
|
||||
@NotNull Collection<JetFile> namespaceFiles,
|
||||
@NotNull CompilationErrorHandler errorHandler
|
||||
) {
|
||||
PsiManager manager = PsiManager.getInstance(state.getProject());
|
||||
stubStack.push(answer);
|
||||
@@ -71,7 +72,7 @@ class LightClassGenerationStrategy extends GenerationStrategy {
|
||||
fakeFile.setPhysical(false);
|
||||
answer.setPsi(fakeFile);
|
||||
|
||||
super.generateNamespace(state, fqName, namespaceFiles, errorHandler);
|
||||
StandardGenerationStrategy.INSTANCE.generateNamespace(state, fqName, namespaceFiles, errorHandler);
|
||||
final StubElement pop = stubStack.pop();
|
||||
if (pop != answer) {
|
||||
LOG.error("Unbalanced stack operations: " + pop);
|
||||
|
||||
@@ -33,7 +33,7 @@ import org.jetbrains.jet.analyzer.AnalyzeExhaust;
|
||||
import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys;
|
||||
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
|
||||
import org.jetbrains.jet.codegen.state.GenerationState;
|
||||
import org.jetbrains.jet.codegen.state.GenerationStrategy;
|
||||
import org.jetbrains.jet.codegen.state.StandardGenerationStrategy;
|
||||
import org.jetbrains.jet.config.CompilerConfiguration;
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUtil;
|
||||
import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
|
||||
@@ -363,7 +363,7 @@ public abstract class CodegenTestCase extends UsefulTestCase {
|
||||
configuration.get(JVMConfigurationKeys.GENERATE_NOT_NULL_ASSERTIONS, true),
|
||||
configuration.get(JVMConfigurationKeys.GENERATE_NOT_NULL_PARAMETER_ASSERTIONS, true)
|
||||
);
|
||||
GenerationStrategy.STANDARD.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);
|
||||
KotlinCodegenFacade.compileCorrectFiles(state, StandardGenerationStrategy.INSTANCE, CompilationErrorHandler.THROW_EXCEPTION);
|
||||
return state;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ import com.intellij.psi.PsiFile;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.analyzer.AnalyzeExhaust;
|
||||
import org.jetbrains.jet.codegen.state.GenerationState;
|
||||
import org.jetbrains.jet.codegen.state.GenerationStrategy;
|
||||
import org.jetbrains.jet.codegen.state.StandardGenerationStrategy;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter;
|
||||
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM;
|
||||
@@ -59,7 +59,7 @@ public class GenerationUtils {
|
||||
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);
|
||||
GenerationStrategy.STANDARD.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);
|
||||
KotlinCodegenFacade.compileCorrectFiles(state, StandardGenerationStrategy.INSTANCE, CompilationErrorHandler.THROW_EXCEPTION);
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.analyzer.AnalyzeExhaust;
|
||||
import org.jetbrains.jet.codegen.*;
|
||||
import org.jetbrains.jet.codegen.state.GenerationState;
|
||||
import org.jetbrains.jet.codegen.state.GenerationStrategy;
|
||||
import org.jetbrains.jet.codegen.state.StandardGenerationStrategy;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.plugin.internal.Location;
|
||||
import org.jetbrains.jet.plugin.project.WholeProjectAnalyzerFacade;
|
||||
@@ -92,7 +92,7 @@ public class BytecodeToolwindow extends JPanel implements Disposable {
|
||||
return printStackTraceToString(exhaust.getError());
|
||||
}
|
||||
state = new GenerationState(jetFile.getProject(), ClassBuilderFactories.TEXT, exhaust.getBindingContext(), Collections.singletonList(jetFile));
|
||||
GenerationStrategy.STANDARD.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);
|
||||
KotlinCodegenFacade.compileCorrectFiles(state, StandardGenerationStrategy.INSTANCE, CompilationErrorHandler.THROW_EXCEPTION);
|
||||
}
|
||||
catch (ProcessCanceledException e) {
|
||||
throw e;
|
||||
|
||||
Reference in New Issue
Block a user