From ae5c95038b3b0085d9a99597bf9d690fb9f04d1f Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Tue, 17 Nov 2015 21:45:40 +0300 Subject: [PATCH] Place repl specific stuff of GenerationState into a separate entity Add even more hacks to tell if the line has useful result to display --- .../jetbrains/kotlin/codegen/ExpressionCodegen.java | 3 ++- .../org/jetbrains/kotlin/codegen/ScriptCodegen.java | 4 ++-- .../kotlin/codegen/context/ScriptContext.java | 4 ++-- .../kotlin/codegen/inline/InlineCodegen.java | 2 +- .../kotlin/codegen/state/GenerationState.kt | 13 ++++++++++--- .../kotlin/cli/jvm/repl/ReplInterpreter.java | 6 +++--- 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index 817f63f3110..d9717128965 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -311,11 +311,12 @@ public class ExpressionCodegen extends KtVisitor impleme public void gen(KtElement expr, Type type) { StackValue value = Type.VOID_TYPE.equals(type) ? genStatement(expr) : gen(expr); // for repl store the result of the last line into special field - if (value.type != Type.VOID_TYPE && state.getShouldGenerateScriptResultValue()) { + if (value.type != Type.VOID_TYPE && state.getReplSpecific().getShouldGenerateScriptResultValue()) { ScriptContext context = getScriptContext(); if (expr == context.getLastStatement()) { StackValue.Field resultValue = StackValue.field(context.getResultFieldInfo(), StackValue.LOCAL_0); resultValue.store(value, v); + state.getReplSpecific().setHasResult(true); return; } } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ScriptCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ScriptCodegen.java index 536471c18f1..b61f694c48f 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ScriptCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ScriptCodegen.java @@ -56,7 +56,7 @@ public class ScriptCodegen extends MemberCodegen { ClassBuilder builder = state.getFactory().newVisitor(JvmDeclarationOriginKt.OtherOrigin(declaration, scriptDescriptor), classType, declaration.getContainingFile()); - List earlierScripts = state.getEarlierScriptsForReplInterpreter(); + List earlierScripts = state.getReplSpecific().getEarlierScriptsForReplInterpreter(); ScriptContext scriptContext = parentContext.intoScript( scriptDescriptor, earlierScripts == null ? Collections.emptyList() : earlierScripts, @@ -115,7 +115,7 @@ public class ScriptCodegen extends MemberCodegen { ) { JvmMethodSignature jvmSignature = typeMapper.mapScriptSignature(scriptDescriptor, context.getEarlierScripts()); - if (state.getShouldGenerateScriptResultValue()) { + if (state.getReplSpecific().getShouldGenerateScriptResultValue()) { FieldInfo resultFieldInfo = context.getResultFieldInfo(); classBuilder.newField( JvmDeclarationOrigin.NO_ORIGIN, diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/context/ScriptContext.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/context/ScriptContext.java index 3ee659f2a7c..bac4fa94f6a 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/context/ScriptContext.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/context/ScriptContext.java @@ -67,9 +67,9 @@ public class ScriptContext extends ClassContext { @NotNull public FieldInfo getResultFieldInfo() { - assert getState().getShouldGenerateScriptResultValue() : "Should not be called unless 'scriptResultFieldName' is set"; + assert getState().getReplSpecific().getShouldGenerateScriptResultValue() : "Should not be called unless 'scriptResultFieldName' is set"; GenerationState state = getState(); - String scriptResultFieldName = state.getScriptResultFieldName(); + String scriptResultFieldName = state.getReplSpecific().getScriptResultFieldName(); assert scriptResultFieldName != null; return FieldInfo.createForHiddenField(state.getTypeMapper().mapClass(scriptDescriptor), AsmTypes.OBJECT_TYPE, scriptResultFieldName); } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.java index 08c0e1b28d0..7aff321879c 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.java @@ -598,7 +598,7 @@ public class InlineCodegen extends CallGenerator { CodegenContext parent = getContext(descriptor.getContainingDeclaration(), state, sourceFile); if (descriptor instanceof ScriptDescriptor) { - List earlierScripts = state.getEarlierScriptsForReplInterpreter(); + List earlierScripts = state.getReplSpecific().getEarlierScriptsForReplInterpreter(); return parent.intoScript((ScriptDescriptor) descriptor, earlierScripts == null ? Collections.emptyList() : earlierScripts, (ClassDescriptor) descriptor, state.getTypeMapper()); diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/state/GenerationState.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/state/GenerationState.kt index 2fdbff3ccb5..6adaf825764 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/state/GenerationState.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/state/GenerationState.kt @@ -100,15 +100,22 @@ public class GenerationState @JvmOverloads constructor( public val samWrapperClasses: SamWrapperClasses = SamWrapperClasses(this) public val inlineCycleReporter: InlineCycleReporter = InlineCycleReporter(diagnostics) public val mappingsClassesForWhenByEnum: MappingsClassesForWhenByEnum = MappingsClassesForWhenByEnum(this) - public var earlierScriptsForReplInterpreter: List? = null - public var scriptResultFieldName: String? = null - public val shouldGenerateScriptResultValue: Boolean get() = scriptResultFieldName != null public val reflectionTypes: ReflectionTypes = ReflectionTypes(module) public val jvmRuntimeTypes: JvmRuntimeTypes = JvmRuntimeTypes() public val factory: ClassFileFactory private val interceptedBuilderFactory: ClassBuilderFactory private var used = false + public val replSpecific = ForRepl() + + //TODO: should be refactored out + public class ForRepl { + public var earlierScriptsForReplInterpreter: List? = null + public var scriptResultFieldName: String? = null + public val shouldGenerateScriptResultValue: Boolean get() = scriptResultFieldName != null + public var hasResult: Boolean = false + } + public val isCallAssertionsEnabled: Boolean = !disableCallAssertions @JvmName("isCallAssertionsEnabled") get diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.java b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.java index 7143923b4a8..9974a6bdf69 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.java @@ -352,7 +352,7 @@ public class ReplInterpreter { earlierLines.add(new EarlierLine(line, scriptDescriptor, scriptClass, scriptInstance, scriptClassType)); - return LineResult.successful(rv, false); + return LineResult.successful(rv, !state.getReplSpecific().getHasResult()); } catch (Throwable e) { @SuppressWarnings("UseOfSystemOutOrSystemErr") @@ -431,7 +431,7 @@ public class ReplInterpreter { ScriptDescriptor earlierDescriptor = pair.first; earlierScriptDescriptors.add(earlierDescriptor); } - state.setEarlierScriptsForReplInterpreter(earlierScriptDescriptors); + state.getReplSpecific().setEarlierScriptsForReplInterpreter(earlierScriptDescriptors); } public static void compileScript( @@ -440,7 +440,7 @@ public class ReplInterpreter { @NotNull GenerationState state, @NotNull CompilationErrorHandler errorHandler ) { - state.setScriptResultFieldName(SCRIPT_RESULT_FIELD_NAME); + state.getReplSpecific().setScriptResultFieldName(SCRIPT_RESULT_FIELD_NAME); registerEarlierScripts(state, earlierScripts); state.beforeCompile();