From 970d4f2949305be85b15fa04e58a6a470a8e08dc Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Tue, 7 Feb 2023 19:32:30 +0100 Subject: [PATCH] K2: Extract PersistentCheckerContext::copy --- .../context/PersistentCheckerContext.kt | 149 +++++------------- 1 file changed, 39 insertions(+), 110 deletions(-) diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/context/PersistentCheckerContext.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/context/PersistentCheckerContext.kt index 5ccfa132102..644d308929e 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/context/PersistentCheckerContext.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/context/PersistentCheckerContext.kt @@ -48,99 +48,34 @@ class PersistentCheckerContext private constructor( allErrorsSuppressed = false ) - override fun addImplicitReceiver(name: Name?, value: ImplicitReceiverValue<*>): PersistentCheckerContext { - return PersistentCheckerContext( - implicitReceiverStack.add(name, value), - containingDeclarations, - qualifiedAccessOrAssignmentsOrAnnotationCalls, - getClassCalls, - annotationContainers, - isContractBody, - sessionHolder, - returnTypeCalculator, - suppressedDiagnostics, - allInfosSuppressed, - allWarningsSuppressed, - allErrorsSuppressed - ) - } + override fun addImplicitReceiver(name: Name?, value: ImplicitReceiverValue<*>): PersistentCheckerContext = + copy(implicitReceiverStack = implicitReceiverStack.add(name, value)) - override fun addDeclaration(declaration: FirDeclaration): PersistentCheckerContext { - return PersistentCheckerContext( - implicitReceiverStack, - containingDeclarations.add(declaration), - qualifiedAccessOrAssignmentsOrAnnotationCalls, - getClassCalls, - annotationContainers, - isContractBody, - sessionHolder, - returnTypeCalculator, - suppressedDiagnostics, - allInfosSuppressed, - allWarningsSuppressed, - allErrorsSuppressed - ) - } + override fun addDeclaration(declaration: FirDeclaration): PersistentCheckerContext = + copy(containingDeclarations = containingDeclarations.add(declaration)) override fun dropDeclaration() { } - override fun addQualifiedAccessOrAnnotationCall(qualifiedAccessOrAnnotationCall: FirStatement): PersistentCheckerContext { - return PersistentCheckerContext( - implicitReceiverStack, - containingDeclarations, - this.qualifiedAccessOrAssignmentsOrAnnotationCalls.add(qualifiedAccessOrAnnotationCall), - getClassCalls, - annotationContainers, - isContractBody, - sessionHolder, - returnTypeCalculator, - suppressedDiagnostics, - allInfosSuppressed, - allWarningsSuppressed, - allErrorsSuppressed + override fun addQualifiedAccessOrAnnotationCall(qualifiedAccessOrAnnotationCall: FirStatement): PersistentCheckerContext = + copy( + qualifiedAccessOrAssignmentsOrAnnotationCalls = + qualifiedAccessOrAssignmentsOrAnnotationCalls.add(qualifiedAccessOrAnnotationCall) ) - } override fun dropQualifiedAccessOrAnnotationCall() { } - override fun addGetClassCall(getClassCall: FirGetClassCall): PersistentCheckerContext { - return PersistentCheckerContext( - implicitReceiverStack, - containingDeclarations, - qualifiedAccessOrAssignmentsOrAnnotationCalls, - getClassCalls.add(getClassCall), - annotationContainers, - isContractBody, - sessionHolder, - returnTypeCalculator, - suppressedDiagnostics, - allInfosSuppressed, - allWarningsSuppressed, - allErrorsSuppressed + override fun addGetClassCall(getClassCall: FirGetClassCall): PersistentCheckerContext = + copy( + getClassCalls = getClassCalls.add(getClassCall), ) - } override fun dropGetClassCall() { } - override fun addAnnotationContainer(annotationContainer: FirAnnotationContainer): PersistentCheckerContext { - return PersistentCheckerContext( - implicitReceiverStack, - containingDeclarations, - qualifiedAccessOrAssignmentsOrAnnotationCalls, - getClassCalls, - annotationContainers.add(annotationContainer), - isContractBody, - sessionHolder, - returnTypeCalculator, - suppressedDiagnostics, - allInfosSuppressed, - allWarningsSuppressed, - allErrorsSuppressed - ) - } + override fun addAnnotationContainer(annotationContainer: FirAnnotationContainer): PersistentCheckerContext = + copy(annotationContainers = annotationContainers.add(annotationContainer)) override fun dropAnnotationContainer() { } @@ -152,46 +87,40 @@ class PersistentCheckerContext private constructor( allErrorsSuppressed: Boolean ): CheckerContextForProvider { if (diagnosticNames.isEmpty()) return this + return copy( + suppressedDiagnostics = suppressedDiagnostics.addAll(diagnosticNames), + allInfosSuppressed = this.allInfosSuppressed || allInfosSuppressed, + allWarningsSuppressed = this.allWarningsSuppressed || allWarningsSuppressed, + allErrorsSuppressed = this.allErrorsSuppressed || allErrorsSuppressed + ) + } + + private fun copy( + implicitReceiverStack: PersistentImplicitReceiverStack = this.implicitReceiverStack, + qualifiedAccessOrAssignmentsOrAnnotationCalls: PersistentList = this.qualifiedAccessOrAssignmentsOrAnnotationCalls, + getClassCalls: PersistentList = this.getClassCalls, + annotationContainers: PersistentList = this.annotationContainers, + containingDeclarations: PersistentList = this.containingDeclarations, + isContractBody: Boolean = this.isContractBody, + allInfosSuppressed: Boolean = this.allInfosSuppressed, + allWarningsSuppressed: Boolean = this.allWarningsSuppressed, + allErrorsSuppressed: Boolean = this.allErrorsSuppressed, + suppressedDiagnostics: PersistentSet = this.suppressedDiagnostics, + ): PersistentCheckerContext { return PersistentCheckerContext( - implicitReceiverStack, - containingDeclarations, - qualifiedAccessOrAssignmentsOrAnnotationCalls, - getClassCalls, - annotationContainers, - isContractBody, - sessionHolder, - returnTypeCalculator, - suppressedDiagnostics.addAll(diagnosticNames), - this.allInfosSuppressed || allInfosSuppressed, - this.allWarningsSuppressed || allWarningsSuppressed, - this.allErrorsSuppressed || allErrorsSuppressed + implicitReceiverStack, containingDeclarations, qualifiedAccessOrAssignmentsOrAnnotationCalls, + getClassCalls, annotationContainers, isContractBody, sessionHolder, returnTypeCalculator, suppressedDiagnostics, + allInfosSuppressed, allWarningsSuppressed, allErrorsSuppressed, ) } private fun toggleContractBody(newValue: Boolean): CheckerContextForProvider { check(isContractBody != newValue) - return PersistentCheckerContext( - implicitReceiverStack, - containingDeclarations, - qualifiedAccessOrAssignmentsOrAnnotationCalls, - getClassCalls, - annotationContainers, - isContractBody = newValue, - sessionHolder, - returnTypeCalculator, - suppressedDiagnostics, - allInfosSuppressed, - allWarningsSuppressed, - allErrorsSuppressed - ) + return copy(isContractBody = newValue) } - override fun enterContractBody(): CheckerContextForProvider { - return toggleContractBody(newValue = true) - } + override fun enterContractBody(): CheckerContextForProvider = toggleContractBody(newValue = true) - override fun exitContractBody(): CheckerContextForProvider { - return toggleContractBody(newValue = false) - } + override fun exitContractBody(): CheckerContextForProvider = toggleContractBody(newValue = false) }