From 8c0f78db5060f54a09525699260086e0026076ee Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Mon, 23 May 2016 15:02:07 +0300 Subject: [PATCH] Introduce CompilerConfiguration#getNotNull and getBoolean To reduce boilerplate at call sites --- .../org/jetbrains/kotlin/codegen/AsmUtil.java | 4 ++-- .../kotlin/codegen/ExpressionCodegen.java | 4 ++-- .../codegen/inline/InlineCodegenUtil.java | 2 +- .../kotlin/codegen/state/GenerationState.kt | 10 ++++---- .../jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt | 6 ++--- .../cli/jvm/compiler/KotlinCoreEnvironment.kt | 3 +-- .../compiler/KotlinToJVMBytecodeCompiler.kt | 5 ++-- .../kotlin/config/CompilerConfiguration.java | 24 ++++++++++++------- .../config/CompilerConfigurationKey.java | 5 ++++ .../internal/KotlinBytecodeToolWindow.java | 2 +- 10 files changed, 36 insertions(+), 29 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/AsmUtil.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/AsmUtil.java index 99a307481dc..79b0fa5c36e 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/AsmUtil.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/AsmUtil.java @@ -614,7 +614,7 @@ public class AsmUtil { @NotNull FunctionDescriptor descriptor, @NotNull FrameMap frameMap ) { - if (!state.isParamAssertionsEnabled()) return; + if (state.isParamAssertionsDisabled()) return; // Private method is not accessible from other classes, no assertions needed if (getVisibilityAccessFlag(descriptor) == ACC_PRIVATE) return; @@ -655,7 +655,7 @@ public class AsmUtil { @NotNull final StackValue stackValue, @Nullable final RuntimeAssertionInfo runtimeAssertionInfo ) { - if (!state.isCallAssertionsEnabled()) return stackValue; + if (state.isCallAssertionsDisabled()) return stackValue; if (runtimeAssertionInfo == null || !runtimeAssertionInfo.getNeedNotNullAssertion()) return stackValue; return new StackValue(stackValue.type) { diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index baf5eae07d8..635e2950c72 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -1879,7 +1879,7 @@ public class ExpressionCodegen extends KtVisitor impleme CallableMemberDescriptor descriptor = getContext().getContextDescriptor(); NonLocalReturnInfo nonLocalReturn = getNonLocalReturnInfo(descriptor, expression); boolean isNonLocalReturn = nonLocalReturn != null; - if (isNonLocalReturn && !state.isInlineEnabled()) { + if (isNonLocalReturn && state.isInlineDisabled()) { state.getDiagnostics().report(Errors.NON_LOCAL_RETURN_IN_DISABLED_INLINE.on(expression)); genThrow(v, "java/lang/UnsupportedOperationException", "Non-local returns are not allowed with inlining disabled"); @@ -2520,7 +2520,7 @@ public class ExpressionCodegen extends KtVisitor impleme // We should inline callable containing reified type parameters even if inline is disabled // because they may contain something to reify and straight call will probably fail at runtime - boolean isInline = (state.isInlineEnabled() || InlineUtil.containsReifiedTypeParameters(descriptor)) && + boolean isInline = (!state.isInlineDisabled() || InlineUtil.containsReifiedTypeParameters(descriptor)) && (InlineUtil.isInline(descriptor) || InlineUtil.isArrayConstructorWithLambda(descriptor)); if (!isInline) return defaultCallGenerator; diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegenUtil.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegenUtil.java index 2f4f6f12402..8b6e60ec850 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegenUtil.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegenUtil.java @@ -155,7 +155,7 @@ public class InlineCodegenUtil { public static void initDefaultSourceMappingIfNeeded( @NotNull CodegenContext context, @NotNull MemberCodegen codegen, @NotNull GenerationState state ) { - if (!state.isInlineEnabled()) return; + if (state.isInlineDisabled()) return; CodegenContext parentContext = context.getParentContext(); while (parentContext != null) { 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 650b266d18f..4887345912e 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/state/GenerationState.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/state/GenerationState.kt @@ -147,11 +147,11 @@ class GenerationState @JvmOverloads constructor( var hasResult: Boolean = false } - val isCallAssertionsEnabled: Boolean = !configuration.get(JVMConfigurationKeys.DISABLE_CALL_ASSERTIONS, false) - val isParamAssertionsEnabled: Boolean = !configuration.get(JVMConfigurationKeys.DISABLE_PARAM_ASSERTIONS, false) - val isInlineEnabled: Boolean = !configuration.get(JVMConfigurationKeys.DISABLE_INLINE, false) - val useTypeTableInSerializer: Boolean = configuration.get(JVMConfigurationKeys.USE_TYPE_TABLE, false) - val inheritMultifileParts: Boolean = configuration.get(JVMConfigurationKeys.INHERIT_MULTIFILE_PARTS, false) + val isCallAssertionsDisabled: Boolean = configuration.getBoolean(JVMConfigurationKeys.DISABLE_CALL_ASSERTIONS) + val isParamAssertionsDisabled: Boolean = configuration.getBoolean(JVMConfigurationKeys.DISABLE_PARAM_ASSERTIONS) + val isInlineDisabled: Boolean = configuration.getBoolean(JVMConfigurationKeys.DISABLE_INLINE) + val useTypeTableInSerializer: Boolean = configuration.getBoolean(JVMConfigurationKeys.USE_TYPE_TABLE) + val inheritMultifileParts: Boolean = configuration.getBoolean(JVMConfigurationKeys.INHERIT_MULTIFILE_PARTS) val rootContext: CodegenContext<*> = RootContext(this) diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt index c3ecebdf062..0d0145f36bd 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt @@ -248,11 +248,9 @@ open class K2JVMCompiler : CLICompiler() { } fun reportPerf(configuration: CompilerConfiguration, message: String) { - if (!configuration.get(CLIConfigurationKeys.REPORT_PERF, false)) { - return - } + if (!configuration.getBoolean(CLIConfigurationKeys.REPORT_PERF)) return - val collector = configuration[CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY]!! + val collector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY) collector.report(CompilerMessageSeverity.INFO, "PERF: " + message, CompilerMessageLocation.NO_LOCATION) } diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinCoreEnvironment.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinCoreEnvironment.kt index fc443ed0781..b7122ab5e17 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinCoreEnvironment.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinCoreEnvironment.kt @@ -244,8 +244,7 @@ class KotlinCoreEnvironment private constructor( fun getSourceFiles(): List = sourceFiles private fun report(severity: CompilerMessageSeverity, message: String) { - val messageCollector = configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY) - ?: throw CompileEnvironmentException(message) + val messageCollector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY) messageCollector.report(severity, message, CompilerMessageLocation.NO_LOCATION) } diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt index b2e00c013a6..57132c8248e 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt @@ -417,7 +417,7 @@ object KotlinToJVMBytecodeCompiler { } private fun checkKotlinPackageUsage(environment: KotlinCoreEnvironment, files: Collection): Boolean { - if (environment.configuration.get(CLIConfigurationKeys.ALLOW_KOTLIN_PACKAGE) == true) { + if (environment.configuration.getBoolean(CLIConfigurationKeys.ALLOW_KOTLIN_PACKAGE)) { return true } val messageCollector = environment.configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE) @@ -434,8 +434,7 @@ object KotlinToJVMBytecodeCompiler { } private val KotlinCoreEnvironment.messageCollector: MessageCollector - get() = configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY) - ?: error("Message collector not specified in compiler configuration") + get() = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY) private fun reportRuntimeConflicts(messageCollector: MessageCollector, jvmClasspathRoots: List) { fun String.removeIdeaVersionSuffix(): String { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/config/CompilerConfiguration.java b/compiler/frontend/src/org/jetbrains/kotlin/config/CompilerConfiguration.java index d9723bf5313..634f1926db8 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/config/CompilerConfiguration.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/config/CompilerConfiguration.java @@ -41,19 +41,25 @@ public class CompilerConfiguration { @NotNull public T get(@NotNull CompilerConfigurationKey key, @NotNull T defaultValue) { - T data = (T) map.get(key.ideaKey); - return data == null ? defaultValue : unmodifiable(data); + T data = get(key); + return data == null ? defaultValue : data; + } + + @NotNull + public T getNotNull(@NotNull CompilerConfigurationKey key) { + T data = get(key); + assert data != null : "No value for configuration key: " + key; + return data; + } + + public boolean getBoolean(@NotNull CompilerConfigurationKey key) { + return get(key, false); } @NotNull public List getList(@NotNull CompilerConfigurationKey> key) { - List data = (List) map.get(key.ideaKey); - if (data == null) { - return Collections.emptyList(); - } - else { - return Collections.unmodifiableList(data); - } + List data = get(key); + return data == null ? Collections.emptyList() : data; } public void put(@NotNull CompilerConfigurationKey key, @Nullable T value) { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/config/CompilerConfigurationKey.java b/compiler/frontend/src/org/jetbrains/kotlin/config/CompilerConfigurationKey.java index 5dfb87366a0..f829fe94933 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/config/CompilerConfigurationKey.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/config/CompilerConfigurationKey.java @@ -30,4 +30,9 @@ public class CompilerConfigurationKey { public static CompilerConfigurationKey create(@NotNull @NonNls String name) { return new CompilerConfigurationKey(name); } + + @Override + public String toString() { + return ideaKey.toString(); + } } diff --git a/idea/src/org/jetbrains/kotlin/idea/internal/KotlinBytecodeToolWindow.java b/idea/src/org/jetbrains/kotlin/idea/internal/KotlinBytecodeToolWindow.java index 408bade68f5..da8ee45ecf5 100644 --- a/idea/src/org/jetbrains/kotlin/idea/internal/KotlinBytecodeToolWindow.java +++ b/idea/src/org/jetbrains/kotlin/idea/internal/KotlinBytecodeToolWindow.java @@ -275,7 +275,7 @@ public class KotlinBytecodeToolWindow extends JPanel implements Disposable { BindingContext bindingContextForFile = resolutionFacade.analyzeFullyAndGetResult(Collections.singletonList(ktFile)).getBindingContext(); kotlin.Pair> result = DebuggerUtils.INSTANCE.analyzeInlinedFunctions( - resolutionFacade, bindingContextForFile, ktFile, configuration.get(JVMConfigurationKeys.DISABLE_INLINE, false) + resolutionFacade, bindingContextForFile, ktFile, configuration.getBoolean(JVMConfigurationKeys.DISABLE_INLINE) ); BindingContext bindingContext = result.getFirst();