diff --git a/compiler/frontend/src/org/jetbrains/jet/analyzer/AnalyzeExhaust.java b/compiler/frontend/src/org/jetbrains/jet/analyzer/AnalyzeExhaust.java deleted file mode 100644 index 1e1f7aa5444..00000000000 --- a/compiler/frontend/src/org/jetbrains/jet/analyzer/AnalyzeExhaust.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright 2010-2013 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.analyzer; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lang.descriptors.ModuleDescriptor; -import org.jetbrains.jet.lang.resolve.BindingContext; -import org.jetbrains.jet.lang.types.ErrorUtils; - -public class AnalyzeExhaust { - public static final AnalyzeExhaust EMPTY = success(BindingContext.EMPTY, ErrorUtils.getErrorModule()); - - @NotNull - public static AnalyzeExhaust success(@NotNull BindingContext bindingContext, @NotNull ModuleDescriptor module) { - return new AnalyzeExhaust(bindingContext, module, null); - } - - @NotNull - public static AnalyzeExhaust error(@NotNull BindingContext bindingContext, @NotNull Throwable error) { - return new AnalyzeExhaust(bindingContext, ErrorUtils.getErrorModule(), error); - } - - private final BindingContext bindingContext; - private final Throwable error; - private final ModuleDescriptor moduleDescriptor; - - private AnalyzeExhaust( - @NotNull BindingContext bindingContext, - @NotNull ModuleDescriptor moduleDescriptor, - @Nullable Throwable error - ) { - this.bindingContext = bindingContext; - this.error = error; - this.moduleDescriptor = moduleDescriptor; - } - - @NotNull - public BindingContext getBindingContext() { - return bindingContext; - } - - @NotNull - public Throwable getError() { - if (error == null) throw new IllegalStateException("Should be called only for error analyze result"); - return error; - } - - public boolean isError() { - return error != null; - } - - public void throwIfError() { - if (isError()) { - throw new IllegalStateException("failed to analyze: " + error, error); - } - } - - @NotNull - public ModuleDescriptor getModuleDescriptor() { - return moduleDescriptor; - } -} diff --git a/compiler/frontend/src/org/jetbrains/jet/analyzer/AnalyzeExhaust.kt b/compiler/frontend/src/org/jetbrains/jet/analyzer/AnalyzeExhaust.kt new file mode 100644 index 00000000000..730508956a0 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/analyzer/AnalyzeExhaust.kt @@ -0,0 +1,53 @@ +/* + * Copyright 2010-2014 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.analyzer + +import org.jetbrains.jet.lang.descriptors.ModuleDescriptor +import org.jetbrains.jet.lang.resolve.BindingContext +import org.jetbrains.jet.lang.types.ErrorUtils +import kotlin.platform.platformStatic + +public data open class AnalyzeExhaust protected ( + public val bindingContext: BindingContext, + public val moduleDescriptor: ModuleDescriptor +) { + + public val error: Throwable + get() = if (this is Error) this.exception else throw IllegalStateException("Should only be called for error analyze exhaust") + + public fun isError(): Boolean = this is Error + + public fun throwIfError() { + if (isError()) { + throw IllegalStateException("failed to analyze: " + error, error) + } + } + + private class Error(bindingContext: BindingContext, val exception: Throwable) : AnalyzeExhaust(bindingContext, ErrorUtils.getErrorModule()) + + class object { + public val EMPTY: AnalyzeExhaust = success(BindingContext.EMPTY, ErrorUtils.getErrorModule()) + + platformStatic public fun success(bindingContext: BindingContext, module: ModuleDescriptor): AnalyzeExhaust { + return AnalyzeExhaust(bindingContext, module) + } + + platformStatic public fun error(bindingContext: BindingContext, error: Throwable): AnalyzeExhaust { + return Error(bindingContext, error) + } + } +} diff --git a/compiler/tests/org/jetbrains/jet/resolve/calls/AbstractResolvedCallsTest.kt b/compiler/tests/org/jetbrains/jet/resolve/calls/AbstractResolvedCallsTest.kt index ba74695202f..3dd485db0d5 100644 --- a/compiler/tests/org/jetbrains/jet/resolve/calls/AbstractResolvedCallsTest.kt +++ b/compiler/tests/org/jetbrains/jet/resolve/calls/AbstractResolvedCallsTest.kt @@ -48,7 +48,7 @@ public abstract class AbstractResolvedCallsTest() : JetLiteFixture() { val text = JetTestUtils.doLoadFile(File(filePath))!! val jetFile = JetPsiFactory(getProject()).createFile(text.replace("", "")) - val bindingContext = JvmResolveUtil.analyzeOneFileWithJavaIntegration(jetFile).getBindingContext() + val bindingContext = JvmResolveUtil.analyzeOneFileWithJavaIntegration(jetFile).bindingContext val element = jetFile.findElementAt(text.indexOf("")) val expression = PsiTreeUtil.getParentOfType(element, javaClass()) diff --git a/compiler/tests/org/jetbrains/jet/resolve/constraintSystem/AbstractConstraintSystemTest.kt b/compiler/tests/org/jetbrains/jet/resolve/constraintSystem/AbstractConstraintSystemTest.kt index 8a314d163f3..7f664ecab47 100644 --- a/compiler/tests/org/jetbrains/jet/resolve/constraintSystem/AbstractConstraintSystemTest.kt +++ b/compiler/tests/org/jetbrains/jet/resolve/constraintSystem/AbstractConstraintSystemTest.kt @@ -74,8 +74,7 @@ abstract public class AbstractConstraintSystemTest() : JetLiteFixture() { val fileName = "declarations/declarations.kt" val psiFile = createPsiFile(null, fileName, loadFile(fileName))!! - val analyzeExhaust = JvmResolveUtil.analyzeOneFileWithJavaIntegrationAndCheckForErrors(psiFile) - val bindingContext = analyzeExhaust.getBindingContext() + val bindingContext = JvmResolveUtil.analyzeOneFileWithJavaIntegrationAndCheckForErrors(psiFile).bindingContext return MyDeclarations(bindingContext, getProject(), typeResolver) } diff --git a/idea/idea-analysis/src/org/jetbrains/jet/plugin/caches/resolve/KotlinCacheService.kt b/idea/idea-analysis/src/org/jetbrains/jet/plugin/caches/resolve/KotlinCacheService.kt index 69b7625dd46..956009209b9 100644 --- a/idea/idea-analysis/src/org/jetbrains/jet/plugin/caches/resolve/KotlinCacheService.kt +++ b/idea/idea-analysis/src/org/jetbrains/jet/plugin/caches/resolve/KotlinCacheService.kt @@ -47,7 +47,7 @@ public fun JetElement.getAnalysisResults(vararg extraFiles: JetFile): AnalyzeExh } public fun JetElement.getBindingContext(): BindingContext { - return getAnalysisResults().getBindingContext() + return getAnalysisResults().bindingContext } public fun getAnalysisResultsForElements(elements: Collection): AnalyzeExhaust { diff --git a/idea/idea-analysis/src/org/jetbrains/jet/plugin/caches/resolve/KotlinResolveCache.kt b/idea/idea-analysis/src/org/jetbrains/jet/plugin/caches/resolve/KotlinResolveCache.kt index 6f46e8fdc07..596d7e54a61 100644 --- a/idea/idea-analysis/src/org/jetbrains/jet/plugin/caches/resolve/KotlinResolveCache.kt +++ b/idea/idea-analysis/src/org/jetbrains/jet/plugin/caches/resolve/KotlinResolveCache.kt @@ -110,10 +110,10 @@ private class KotlinResolveCache( } perFileCache.getAnalysisResults(it) } - val error = results.firstOrNull { it.isError() } - val bindingContext = CompositeBindingContext.create(results.map { it.getBindingContext() }) - return if (error != null) - AnalyzeExhaust.error(bindingContext, error.getError()) + val withError = results.firstOrNull { it.isError() } + val bindingContext = CompositeBindingContext.create(results.map { it.bindingContext }) + return if (withError != null) + AnalyzeExhaust.error(bindingContext, withError.error) else //TODO: (module refactoring) several elements are passed here in debugger AnalyzeExhaust.success(bindingContext, getLazyResolveSession(elements.first()).getModuleDescriptor()) diff --git a/idea/src/org/jetbrains/jet/plugin/debugger/evaluate/KotlinEvaluationBuilder.kt b/idea/src/org/jetbrains/jet/plugin/debugger/evaluate/KotlinEvaluationBuilder.kt index 411d56f37dd..db1dd5f245e 100644 --- a/idea/src/org/jetbrains/jet/plugin/debugger/evaluate/KotlinEvaluationBuilder.kt +++ b/idea/src/org/jetbrains/jet/plugin/debugger/evaluate/KotlinEvaluationBuilder.kt @@ -220,7 +220,7 @@ class KotlinEvaluator(val codeFragment: JetCodeFragment, private fun JetNamedFunction.getParametersForDebugger(): ParametersDescriptor { return runReadAction { val parameters = ParametersDescriptor() - val bindingContext = getAnalysisResults().getBindingContext() + val bindingContext = getAnalysisResults().bindingContext val descriptor = bindingContext[BindingContext.FUNCTION, this] if (descriptor != null) { val receiver = descriptor.getExtensionReceiverParameter() @@ -245,13 +245,13 @@ class KotlinEvaluator(val codeFragment: JetCodeFragment, return runReadAction { val file = createFileForDebugger(codeFragment, extractedFunction) - val analyzeExhaust = file.checkForErrors() + val (bindingContext, moduleDescriptor) = file.checkForErrors() val state = GenerationState( file.getProject(), ClassBuilderFactories.BINARIES, - analyzeExhaust.getModuleDescriptor(), - analyzeExhaust.getBindingContext(), + moduleDescriptor, + bindingContext, listOf(file) ) @@ -282,10 +282,10 @@ class KotlinEvaluator(val codeFragment: JetCodeFragment, val analyzeExhaust = this.getAnalysisResults(createFlexibleTypesFile()) if (analyzeExhaust.isError()) { - throw EvaluateExceptionUtil.createEvaluateException(analyzeExhaust.getError()) + throw EvaluateExceptionUtil.createEvaluateException(analyzeExhaust.error) } - val bindingContext = analyzeExhaust.getBindingContext() + val bindingContext = analyzeExhaust.bindingContext bindingContext.getDiagnostics().firstOrNull { it.getSeverity() == Severity.ERROR }?.let { throw EvaluateExceptionUtil.createEvaluateException(DefaultErrorMessages.RENDERER.render(it)) } diff --git a/idea/src/org/jetbrains/jet/plugin/j2k/J2kPostProcessor.kt b/idea/src/org/jetbrains/jet/plugin/j2k/J2kPostProcessor.kt index 50dbffaac09..027e61c5a8a 100644 --- a/idea/src/org/jetbrains/jet/plugin/j2k/J2kPostProcessor.kt +++ b/idea/src/org/jetbrains/jet/plugin/j2k/J2kPostProcessor.kt @@ -26,7 +26,7 @@ import com.intellij.psi.PsiElement public class J2kPostProcessor(override val contextToAnalyzeIn: PsiElement) : PostProcessor { override fun analyzeFile(file: JetFile): BindingContext { - return file.getAnalysisResults().getBindingContext() + return file.getAnalysisResults().bindingContext } override fun doAdditionalProcessing(file: JetFile) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableBuilder.kt b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableBuilder.kt index 45ce8b13381..75c314be2b1 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableBuilder.kt +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableBuilder.kt @@ -145,8 +145,8 @@ class CallableBuilder(val config: CallableBuilderConfiguration) { { val exhaust = config.currentFile.getAnalysisResults() - currentFileContext = exhaust.getBindingContext() - currentFileModule = exhaust.getModuleDescriptor() + currentFileContext = exhaust.bindingContext + currentFileModule = exhaust.moduleDescriptor } public var placement: CallablePlacement by Delegates.notNull() diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createClass/CreateClassFromConstructorCallActionFactory.kt b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createClass/CreateClassFromConstructorCallActionFactory.kt index c9bc3b3971c..b3aa96feaa7 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createClass/CreateClassFromConstructorCallActionFactory.kt +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createClass/CreateClassFromConstructorCallActionFactory.kt @@ -39,8 +39,7 @@ public object CreateClassFromConstructorCallActionFactory: JetSingleIntentionAct val file = fullCallExpr.getContainingFile() as? JetFile ?: return null - val exhaust = callExpr.getAnalysisResults() - val context = exhaust.getBindingContext() + val (context, moduleDescriptor) = callExpr.getAnalysisResults() val call = callExpr.getCall(context) ?: return null val targetParent = getTargetParentByCall(call, file) ?: return null @@ -58,7 +57,7 @@ public object CreateClassFromConstructorCallActionFactory: JetSingleIntentionAct val classKind = if (inAnnotationEntry) ClassKind.ANNOTATION_CLASS else ClassKind.PLAIN_CLASS - val (expectedTypeInfo, filter) = fullCallExpr.getInheritableTypeInfo(context, exhaust.getModuleDescriptor(), targetParent) + val (expectedTypeInfo, filter) = fullCallExpr.getInheritableTypeInfo(context, moduleDescriptor, targetParent) if (!filter(classKind)) return null val typeArgumentInfos = if (inAnnotationEntry) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createClass/CreateClassFromReferenceExpressionActionFactory.kt b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createClass/CreateClassFromReferenceExpressionActionFactory.kt index 8a9bc93a594..2947400fd94 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createClass/CreateClassFromReferenceExpressionActionFactory.kt +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createClass/CreateClassFromReferenceExpressionActionFactory.kt @@ -38,8 +38,7 @@ public object CreateClassFromReferenceExpressionActionFactory : JetIntentionActi val name = refExpr.getReferencedName() - val exhaust = refExpr.getAnalysisResults() - val context = exhaust.getBindingContext() + val (context, moduleDescriptor) = refExpr.getAnalysisResults() val fullCallExpr = refExpr.getParent()?.let { when { @@ -88,7 +87,7 @@ public object CreateClassFromReferenceExpressionActionFactory : JetIntentionActi val targetParent = getTargetParentByCall(call, file) ?: return Collections.emptyList() if (isInnerClassExpected(call)) return Collections.emptyList() - val (expectedTypeInfo, filter) = fullCallExpr.getInheritableTypeInfo(context, exhaust.getModuleDescriptor(), targetParent) + val (expectedTypeInfo, filter) = fullCallExpr.getInheritableTypeInfo(context, moduleDescriptor, targetParent) return Arrays.asList(ClassKind.OBJECT, ClassKind.ENUM_ENTRY) .filter { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createVariable/CreateParameterActionFactory.kt b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createVariable/CreateParameterActionFactory.kt index 38575f790b6..86cd69dacc5 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createVariable/CreateParameterActionFactory.kt +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createVariable/CreateParameterActionFactory.kt @@ -67,14 +67,14 @@ object CreateParameterActionFactory: JetSingleIntentionActionFactory() { override fun createAction(diagnostic: Diagnostic): IntentionAction? { val exhaust = (diagnostic.getPsiFile() as? JetFile)?.getAnalysisResults() ?: return null - val context = exhaust.getBindingContext() + val context = exhaust.bindingContext val refExpr = QuickFixUtil.getParentElementOfType(diagnostic, javaClass()) ?: return null if (refExpr.getQualifiedElement() != refExpr) return null val varExpected = refExpr.getAssignmentByLHS() != null - val paramType = refExpr.getExpressionForTypeGuess().guessTypes(context, exhaust.getModuleDescriptor()).let { + val paramType = refExpr.getExpressionForTypeGuess().guessTypes(context, exhaust.moduleDescriptor).let { when (it.size) { 0 -> KotlinBuiltIns.getInstance().getAnyType() 1 -> it.first() diff --git a/idea/tests/org/jetbrains/jet/plugin/decompiler/textBuilder/DecompiledTextConsistencyTest.kt b/idea/tests/org/jetbrains/jet/plugin/decompiler/textBuilder/DecompiledTextConsistencyTest.kt index fef557a4637..22082a8a29f 100644 --- a/idea/tests/org/jetbrains/jet/plugin/decompiler/textBuilder/DecompiledTextConsistencyTest.kt +++ b/idea/tests/org/jetbrains/jet/plugin/decompiler/textBuilder/DecompiledTextConsistencyTest.kt @@ -66,7 +66,7 @@ class ProjectBasedResolverForDecompiler(project: Project) : ResolverForDecompile TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration( project, listOf(), BindingTraceContext(), { false }, module, null, null - ).getModuleDescriptor() + ).moduleDescriptor } override fun resolveTopLevelClass(classId: ClassId): ClassDescriptor? { diff --git a/j2k/tests/test/org/jetbrains/jet/j2k/test/AbstractJavaToKotlinConverterTest.kt b/j2k/tests/test/org/jetbrains/jet/j2k/test/AbstractJavaToKotlinConverterTest.kt index 46ec94626fc..e4ddba825c5 100644 --- a/j2k/tests/test/org/jetbrains/jet/j2k/test/AbstractJavaToKotlinConverterTest.kt +++ b/j2k/tests/test/org/jetbrains/jet/j2k/test/AbstractJavaToKotlinConverterTest.kt @@ -74,7 +74,7 @@ public abstract class AbstractJavaToKotlinConverterTest : LightCodeInsightFixtur } protected fun addErrorsDump(jetFile: JetFile): String { - val diagnostics = jetFile.getAnalysisResults().getBindingContext().getDiagnostics() + val diagnostics = jetFile.getAnalysisResults().bindingContext.getDiagnostics() val errors = diagnostics.filter { it.getSeverity() == Severity.ERROR } if (errors.isEmpty()) return jetFile.getText() val header = errors.map { "// ERROR: " + DefaultErrorMessages.RENDERER.render(it).replace('\n', ' ') }.joinToString("\n", postfix = "\n")