forked from JetBrains/kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
track assignement for declaration from CFA
- Loading branch information
1 parent
2b820fb
commit 6d0e46e
Showing
5 changed files
with
139 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
*/ | ||
|
||
package org.jetbrains.kotlin.formver | ||
|
||
import org.jetbrains.kotlin.fir.FirElement | ||
import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction | ||
import org.jetbrains.kotlin.fir.expressions.FirBlock | ||
import org.jetbrains.kotlin.fir.expressions.FirFunctionCall | ||
import org.jetbrains.kotlin.fir.expressions.arguments | ||
import org.jetbrains.kotlin.fir.expressions.toResolvedCallableSymbol | ||
import org.jetbrains.kotlin.fir.symbols.SymbolInternals | ||
import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol | ||
import org.jetbrains.kotlin.fir.visitors.FirVisitor | ||
import org.jetbrains.kotlin.text | ||
|
||
/** | ||
* The UniqueCheckVisitor class is responsible for visiting expression and return its unique level | ||
* | ||
* @see FirVisitor | ||
* @see UniqueLevel | ||
* | ||
* @property uniqueLevel The uniqueness level to be of the expression | ||
* @property uniqueCheckerContext The context object that contains information about the uniqueness annotations and resolution. | ||
*/ | ||
object UniqueCheckVisitor : FirVisitor<UniqueLevel, UniqueCheckerContext>() { | ||
override fun visitElement(element: FirElement, data: UniqueCheckerContext): UniqueLevel = UniqueLevel.Shared | ||
|
||
override fun visitSimpleFunction(simpleFunction: FirSimpleFunction, data: UniqueCheckerContext): UniqueLevel { | ||
simpleFunction.body?.accept(this, data) | ||
// Function definition don't have to return a unique level | ||
return UniqueLevel.Shared | ||
} | ||
|
||
override fun visitBlock(block: FirBlock, data: UniqueCheckerContext): UniqueLevel { | ||
block.statements.forEach { statement -> | ||
when (statement) { | ||
is FirFunctionCall -> { | ||
statement.accept(this, data) | ||
} | ||
} | ||
} | ||
return UniqueLevel.Shared | ||
} | ||
|
||
@OptIn(SymbolInternals::class) | ||
override fun visitFunctionCall(functionCall: FirFunctionCall, data: UniqueCheckerContext): UniqueLevel { | ||
// To keep is simple, assume a functionCall always return Shared for now | ||
val symbol = functionCall.toResolvedCallableSymbol() | ||
val params = (symbol as FirFunctionSymbol<*>).fir.valueParameters | ||
val requiredUniqueLevels = params.map { data.resolveUniqueAnnotation(it) } | ||
// Skip merge of context for now | ||
val arguments = functionCall.arguments | ||
arguments.forEachIndexed { index, argument -> | ||
val requiredUnique = requiredUniqueLevels[index] | ||
if (requiredUnique == UniqueLevel.Unique && visitExpression(argument, data) == UniqueLevel.Shared) { | ||
throw IllegalArgumentException("uniqueness level not match ${argument.source.text}") | ||
} | ||
} | ||
|
||
val callee = functionCall.toResolvedCallableSymbol()?.fir as FirSimpleFunction | ||
return data.resolveUniqueAnnotation(callee) | ||
} | ||
} | ||
|
||
object UniquenessCheckExceptionWrapper : FirVisitor<UniqueLevel, UniqueCheckerContext>() { | ||
override fun visitElement(element: FirElement, data: UniqueCheckerContext): UniqueLevel { | ||
try { | ||
return element.accept(UniqueCheckVisitor, data) | ||
} catch (e: Exception) { | ||
data.errorCollector.addErrorInfo("... while checking uniqueness level for ${element.source.text}") | ||
throw e | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters