Uast: Rewrite visitor

This commit is contained in:
Yan Zhulanow
2016-03-31 16:46:32 +03:00
parent 04e8161f1d
commit 41979de71e
66 changed files with 423 additions and 356 deletions
@@ -17,6 +17,7 @@
package org.jetbrains.uast
import org.jetbrains.uast.kinds.UastClassKind
import org.jetbrains.uast.visitor.UastVisitor
tailrec fun UElement?.getContainingClass(): UClass? {
val parent = this?.parent ?: return null
@@ -44,11 +45,6 @@ tailrec fun UElement?.getContainingDeclaration(): UDeclaration? {
return parent.getContainingDeclaration()
}
fun UElement.handleTraverse(callback: UastCallback) {
callback(this)
this.traverse(callback)
}
fun UDeclaration.isTopLevel() = parent is UFile
fun UElement.log(firstLine: String, vararg nested: Any?): String {
@@ -67,10 +63,9 @@ fun UElement.log(firstLine: String, vararg nested: Any?): String {
val String.withMargin: String
get() = lines().joinToString("\n") { " " + it }
fun List<UElement>.handleTraverseList(callback: UastCallback) {
fun List<UElement>.acceptList(visitor: UastVisitor) {
for (element in this) {
callback(element)
element.traverse(callback)
element.accept(visitor)
}
}
@@ -18,108 +18,51 @@ package org.jetbrains.uast.visitor
import org.jetbrains.uast.*
abstract class UastVisitor {
open fun visitFile(node: UFile): Boolean = false
open fun visitElement(node: UElement): Boolean = false
open fun visitFile(node: UFile) = visitElement(node)
open fun visitImportStatement(node: UImportStatement) = visitElement(node)
open fun visitAnnotation(node: UAnnotation) = visitElement(node)
open fun visitCatchClause(node: UCatchClause) = visitElement(node)
open fun visitType(node: UType) = visitElement(node)
// Declarations
open fun visitClass(node: UClass): Boolean = false
open fun visitFunction(node: UFunction): Boolean = false
open fun visitVariable(node: UVariable): Boolean = false
open fun visitImportStatement(node: UImportStatement): Boolean = false
open fun visitAnnotation(node: UAnnotation): Boolean = false
open fun visitClass(node: UClass) = visitElement(node)
open fun visitFunction(node: UFunction) = visitElement(node)
open fun visitVariable(node: UVariable) = visitElement(node)
// Expressions
open fun visitLabeledExpression(node: ULabeledExpression): Boolean = false
open fun visitDeclarationsExpression(node: UDeclarationsExpression): Boolean = false
open fun visitBlockExpression(node: UBlockExpression): Boolean = false
open fun visitQualifiedExpression(node: UQualifiedExpression): Boolean = false
open fun visitSimpleReferenceExpression(node: USimpleReferenceExpression): Boolean = false
open fun visitCallExpression(node: UCallExpression): Boolean = false
open fun visitBinaryExpression(node: UBinaryExpression): Boolean = false
open fun visitBinaryExpressionWithType(node: UBinaryExpressionWithType): Boolean = false
open fun visitParenthesizedExpression(node: UParenthesizedExpression): Boolean = false
open fun visitPrefixExpression(node: UPrefixExpression): Boolean = false
open fun visitPostfixExpression(node: UPostfixExpression): Boolean = false
open fun visitSpecialExpressionList(node: USpecialExpressionList): Boolean = false
open fun visitExpressionList(node: UExpressionList): Boolean = false
open fun visitIfExpression(node: UIfExpression): Boolean = false
open fun visitSwitchExpression(node: USwitchExpression): Boolean = false
open fun visitSwitchClauseExpression(node: USwitchClauseExpression): Boolean = false
open fun visitWhileExpression(node: UWhileExpression): Boolean = false
open fun visitDoWhileExpression(node: UDoWhileExpression): Boolean = false
open fun visitForExpression(node: UForExpression): Boolean = false
open fun visitForEachExpression(node: UForEachExpression): Boolean = false
open fun visitTryExpression(node: UTryExpression): Boolean = false
open fun visitLiteralExpression(node: ULiteralExpression): Boolean = false
open fun visitThisExpression(node: UThisExpression): Boolean = false
open fun visitSuperExpression(node: USuperExpression): Boolean = false
open fun visitArrayAccessExpression(node: UArrayAccessExpression): Boolean = false
open fun visitCallableReferenceExpression(node: UCallableReferenceExpression): Boolean = false
open fun visitClassLiteralExpression(node: UClassLiteralExpression): Boolean = false
open fun visitLambdaExpression(node: ULambdaExpression): Boolean = false
open fun visitObjectLiteralExpression(node: UObjectLiteralExpression): Boolean = false
open fun beforeVisit(node: UElement) {}
open fun visitOther(node: UElement): Boolean = true
open fun afterVisit(node: UElement) {}
fun handle(node: UElement): Boolean {
beforeVisit(node)
val result = when (node) {
is UFile -> visitFile(node)
is UClass -> visitClass(node)
is UFunction -> visitFunction(node)
is UVariable -> visitVariable(node)
is UImportStatement -> visitImportStatement(node)
is UAnnotation -> visitAnnotation(node)
is ULabeledExpression -> visitLabeledExpression(node)
is UDeclarationsExpression -> visitDeclarationsExpression(node)
is UBlockExpression -> visitBlockExpression(node)
is UQualifiedExpression -> visitQualifiedExpression(node)
is USimpleReferenceExpression -> visitSimpleReferenceExpression(node)
is UCallExpression -> visitCallExpression(node)
is UBinaryExpression -> visitBinaryExpression(node)
is UBinaryExpressionWithType -> visitBinaryExpressionWithType(node)
is UParenthesizedExpression -> visitParenthesizedExpression(node)
is UPrefixExpression -> visitPrefixExpression(node)
is UPostfixExpression -> visitPostfixExpression(node)
is USpecialExpressionList -> visitSpecialExpressionList(node)
is UExpressionList -> visitExpressionList(node)
is UIfExpression -> visitIfExpression(node)
is USwitchExpression -> visitSwitchExpression(node)
is USwitchClauseExpression -> visitSwitchClauseExpression(node)
is UWhileExpression -> visitWhileExpression(node)
is UDoWhileExpression -> visitDoWhileExpression(node)
is UForExpression -> visitForExpression(node)
is UForEachExpression -> visitForEachExpression(node)
is UTryExpression -> visitTryExpression(node)
is ULiteralExpression -> visitLiteralExpression(node)
is UThisExpression -> visitThisExpression(node)
is USuperExpression -> visitSuperExpression(node)
is UArrayAccessExpression -> visitArrayAccessExpression(node)
is UCallableReferenceExpression -> visitCallableReferenceExpression(node)
is UClassLiteralExpression -> visitClassLiteralExpression(node)
is ULambdaExpression -> visitLambdaExpression(node)
is UObjectLiteralExpression -> visitObjectLiteralExpression(node)
else -> visitOther(node)
}
afterVisit(node)
return result
}
open fun process(element: UElement) {
if (!handle(element)) {
processChildren(element)
}
}
fun processChildren(element: UElement) {
element.traverse(UastCallback { handle(it) })
}
open fun visitLabeledExpression(node: ULabeledExpression) = visitElement(node)
open fun visitDeclarationsExpression(node: UDeclarationsExpression) = visitElement(node)
open fun visitBlockExpression(node: UBlockExpression) = visitElement(node)
open fun visitQualifiedExpression(node: UQualifiedExpression) = visitElement(node)
open fun visitSimpleReferenceExpression(node: USimpleReferenceExpression) = visitElement(node)
open fun visitCallExpression(node: UCallExpression) = visitElement(node)
open fun visitBinaryExpression(node: UBinaryExpression) = visitElement(node)
open fun visitBinaryExpressionWithType(node: UBinaryExpressionWithType) = visitElement(node)
open fun visitParenthesizedExpression(node: UParenthesizedExpression) = visitElement(node)
open fun visitUnaryExpression(node: UUnaryExpression) = visitElement(node)
open fun visitPrefixExpression(node: UPrefixExpression) = visitElement(node)
open fun visitPostfixExpression(node: UPostfixExpression) = visitElement(node)
open fun visitSpecialExpressionList(node: USpecialExpressionList) = visitElement(node)
open fun visitExpressionList(node: UExpressionList) = visitElement(node)
open fun visitIfExpression(node: UIfExpression) = visitElement(node)
open fun visitSwitchExpression(node: USwitchExpression) = visitElement(node)
open fun visitSwitchClauseExpression(node: USwitchClauseExpression) = visitElement(node)
open fun visitWhileExpression(node: UWhileExpression) = visitElement(node)
open fun visitDoWhileExpression(node: UDoWhileExpression) = visitElement(node)
open fun visitForExpression(node: UForExpression) = visitElement(node)
open fun visitForEachExpression(node: UForEachExpression) = visitElement(node)
open fun visitTryExpression(node: UTryExpression) = visitElement(node)
open fun visitLiteralExpression(node: ULiteralExpression) = visitElement(node)
open fun visitThisExpression(node: UThisExpression) = visitElement(node)
open fun visitSuperExpression(node: USuperExpression) = visitElement(node)
open fun visitArrayAccessExpression(node: UArrayAccessExpression) = visitElement(node)
open fun visitCallableReferenceExpression(node: UCallableReferenceExpression) = visitElement(node)
open fun visitClassLiteralExpression(node: UClassLiteralExpression) = visitElement(node)
open fun visitLambdaExpression(node: ULambdaExpression) = visitElement(node)
open fun visitObjectLiteralExpression(node: UObjectLiteralExpression) = visitElement(node)
}
object EmptyUastVisitor : UastVisitor() {
override fun process(element: UElement) {}
}
object EmptyUastVisitor : UastVisitor()
@@ -15,10 +15,12 @@
*/
package org.jetbrains.uast
interface UAnnotation : UElement, UNamed, UFqNamed, LeafUElement {
fun resolve(context: UastContext): UClass?
import org.jetbrains.uast.visitor.UastVisitor
interface UAnnotation : UElement, UNamed, UFqNamed {
val valueArguments: List<UNamedExpression>
fun resolve(context: UastContext): UClass?
fun getValue(name: String) = valueArguments.firstOrNull { it.name == name }?.expression?.evaluate()
fun getValues() = valueArguments.map { Pair(it.name, it.expression.evaluate()) }
@@ -26,6 +28,11 @@ interface UAnnotation : UElement, UNamed, UFqNamed, LeafUElement {
override fun logString() = log("UAnnotation ($name)")
override fun renderString() = if (valueArguments.isEmpty()) "@$name" else "@$name(" +
valueArguments.joinToString { it.renderString() } + ")"
override fun accept(visitor: UastVisitor) {
if (visitor.visitAnnotation(this)) return
valueArguments.acceptList(visitor)
}
}
interface UAnnotated : UElement {
@@ -15,6 +15,8 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UElement {
val parent: UElement?
@@ -23,7 +25,10 @@ interface UElement {
fun logString(): String
fun renderString(): String = logString()
fun traverse(callback: UastCallback)
fun accept(visitor: UastVisitor) {
visitor.visitElement(this)
}
}
interface UNamed {
@@ -42,10 +47,6 @@ interface UModifierOwner {
fun hasModifier(modifier: UastModifier): Boolean
}
interface LeafUElement : UElement {
override fun traverse(callback: UastCallback) {}
}
interface UResolvable {
fun resolve(context: UastContext): UDeclaration?
fun resolveOrEmpty(context: UastContext): UDeclaration = resolve(context) ?: UDeclarationNotResolved
@@ -15,10 +15,16 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UExpression : UElement {
open fun evaluate(): Any? = null
fun evaluateString(): String? = evaluate() as? String
fun getExpressionType(): UType? = null
override fun accept(visitor: UastVisitor) {
visitor.visitElement(this)
}
}
interface NoAnnotations : UAnnotated {
@@ -30,6 +36,6 @@ interface NoModifiers : UModifierOwner {
override fun hasModifier(modifier: UastModifier) = false
}
class EmptyExpression(override val parent: UElement) : UExpression, LeafUElement {
class EmptyExpression(override val parent: UElement) : UExpression {
override fun logString() = "EmptyExpression"
}
@@ -15,6 +15,8 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UFile: UElement {
val packageFqName: String?
val importStatements: List<UImportStatement>
@@ -26,9 +28,10 @@ interface UFile: UElement {
override val parent: UElement?
get() = null
override fun traverse(callback: UastCallback) {
declarations.handleTraverseList(callback)
importStatements.handleTraverseList(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitFile(this)) return
declarations.acceptList(visitor)
importStatements.acceptList(visitor)
}
override fun logString() = "UFile (package = $packageFqName)\n" + declarations.joinToString("\n") { it.logString().withMargin }
@@ -15,12 +15,15 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UDoWhileExpression : ULoopExpression {
val condition: UExpression
override fun traverse(callback: UastCallback) {
condition.handleTraverse(callback)
body.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitDoWhileExpression(this)) return
condition.accept(visitor)
body.accept(visitor)
}
override fun renderString() = buildString {
@@ -15,14 +15,17 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UForEachExpression : ULoopExpression {
val variable: UVariable
val iteratedValue: UExpression
override fun traverse(callback: UastCallback) {
variable.handleTraverse(callback)
iteratedValue.handleTraverse(callback)
body.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitForEachExpression(this)) return
variable.accept(visitor)
iteratedValue.accept(visitor)
body.accept(visitor)
}
override fun renderString() = buildString {
@@ -15,16 +15,19 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UForExpression : ULoopExpression {
val declaration: UExpression?
val condition: UExpression?
val update: UExpression?
override fun traverse(callback: UastCallback) {
declaration?.handleTraverse(callback)
condition?.handleTraverse(callback)
update?.handleTraverse(callback)
body.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitForExpression(this)) return
declaration?.accept(visitor)
condition?.accept(visitor)
update?.accept(visitor)
body.accept(visitor)
}
override fun renderString() = buildString {
@@ -15,16 +15,19 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UIfExpression : UExpression {
val condition: UExpression
val thenBranch: UExpression?
val elseBranch: UExpression?
val isTernary: Boolean
override fun traverse(callback: UastCallback) {
condition.handleTraverse(callback)
thenBranch?.handleTraverse(callback)
elseBranch?.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitIfExpression(this)) return
condition.accept(visitor)
thenBranch?.accept(visitor)
elseBranch?.accept(visitor)
}
override fun logString() = log("UIfExpression", condition, thenBranch, elseBranch)
@@ -15,13 +15,16 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface USwitchExpression : UExpression {
val expression: UExpression?
val body: UExpression
override fun traverse(callback: UastCallback) {
expression?.handleTraverse(callback)
body.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitSwitchExpression(this)) return
expression?.accept(visitor)
body.accept(visitor)
}
override fun logString() = log("USwitchExpression", expression, body)
@@ -37,8 +40,9 @@ interface USwitchClauseExpression : UExpression
interface UExpressionSwitchClauseExpression : USwitchClauseExpression {
val caseValue: UExpression
override fun traverse(callback: UastCallback) {
caseValue.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitSwitchClauseExpression(this)) return
caseValue.accept(visitor)
}
override fun renderString() = caseValue.renderString() + " -> "
@@ -46,7 +50,6 @@ interface UExpressionSwitchClauseExpression : USwitchClauseExpression {
}
interface UDefaultSwitchClauseExpression : USwitchClauseExpression {
override fun traverse(callback: UastCallback) {}
override fun logString() = "UDefaultSwitchClause"
override fun renderString() = "else -> "
}
@@ -15,17 +15,20 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UTryExpression : UExpression {
val resources: List<UElement>?
val tryClause: UExpression
val catchClauses: List<UCatchClause>
val finallyClause: UExpression?
override fun traverse(callback: UastCallback) {
resources?.handleTraverseList(callback)
tryClause.handleTraverse(callback)
catchClauses.handleTraverseList(callback)
finallyClause?.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitTryExpression(this)) return
resources?.acceptList(visitor)
tryClause.accept(visitor)
catchClauses.acceptList(visitor)
finallyClause?.accept(visitor)
}
override fun renderString() = buildString {
@@ -46,10 +49,11 @@ interface UCatchClause : UElement {
val parameters: List<UVariable>
val types: List<UType>
override fun traverse(callback: UastCallback) {
body.handleTraverse(callback)
parameters.handleTraverseList(callback)
types.handleTraverseList(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitCatchClause(this)) return
body.accept(visitor)
parameters.acceptList(visitor)
types.acceptList(visitor)
}
override fun logString() = log("UCatchClause", body)
@@ -15,12 +15,15 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UWhileExpression : ULoopExpression {
val condition: UExpression
override fun traverse(callback: UastCallback) {
condition.handleTraverse(callback)
body.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitWhileExpression(this)) return
condition.accept(visitor)
body.accept(visitor)
}
override fun renderString() = buildString {
@@ -16,6 +16,7 @@
package org.jetbrains.uast
import org.jetbrains.uast.kinds.UastClassKind
import org.jetbrains.uast.visitor.UastVisitor
interface UClass : UDeclaration, UFqNamed, UModifierOwner, UAnnotated {
/* The simple class name is only for the debug purposes. Do not check against it in the production code */
@@ -51,10 +52,11 @@ interface UClass : UDeclaration, UFqNamed, UModifierOwner, UAnnotated {
fun getSuperClass(context: UastContext): UClass?
override fun traverse(callback: UastCallback) {
nameElement?.handleTraverse(callback)
declarations.handleTraverseList(callback)
annotations.handleTraverseList(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitClass(this)) return
nameElement?.accept(visitor)
declarations.acceptList(visitor)
annotations.acceptList(visitor)
}
override fun renderString(): String {
@@ -15,11 +15,17 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UDeclaration : UElement, UNamed {
val nameElement: UElement?
override fun accept(visitor: UastVisitor) {
visitor.visitElement(this)
}
}
object UDeclarationNotResolved : UDeclaration, LeafUElement {
object UDeclarationNotResolved : UDeclaration {
override val name = "<declaration not resolved>"
override val nameElement = null
override val parent = null
@@ -15,6 +15,8 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UFunction : UDeclaration, UModifierOwner, UAnnotated {
val kind: UastFunctionKind
val valueParameters: List<UVariable>
@@ -30,13 +32,14 @@ interface UFunction : UDeclaration, UModifierOwner, UAnnotated {
fun getSuperFunctions(context: UastContext): List<UFunction>
override fun traverse(callback: UastCallback) {
nameElement?.handleTraverse(callback)
valueParameters.handleTraverseList(callback)
body.handleTraverse(callback)
annotations.handleTraverseList(callback)
typeParameters.handleTraverseList(callback)
returnType?.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitFunction(this)) return
nameElement?.accept(visitor)
valueParameters.acceptList(visitor)
body.accept(visitor)
annotations.acceptList(visitor)
typeParameters.acceptList(visitor)
returnType?.accept(visitor)
}
override fun renderString(): String {
@@ -16,6 +16,7 @@
package org.jetbrains.uast
import org.jetbrains.uast.kinds.UastImportKind
import org.jetbrains.uast.visitor.UastVisitor
interface UImportStatement : UElement {
val fqNameToImport: String?
@@ -25,7 +26,10 @@ interface UImportStatement : UElement {
/* Returns null if isStarImport = true */
fun resolve(context: UastContext): UDeclaration?
override fun traverse(callback: UastCallback) {}
override fun accept(visitor: UastVisitor) {
visitor.visitImportStatement(this)
}
override fun logString() = "UImport ($fqNameToImport)"
override fun renderString() = "import ${fqNameToImport ?: ""}"
}
@@ -15,7 +15,9 @@
*/
package org.jetbrains.uast
interface UType : UElement, UNamed, UFqNamed, UAnnotated, UResolvable, LeafUElement {
import org.jetbrains.uast.visitor.UastVisitor
interface UType : UElement, UNamed, UFqNamed, UAnnotated, UResolvable {
override fun matchesName(name: String) = this.name == name || this.name.endsWith(".$name")
/* The simple type name is only for the debug purposes. Do not check against it in the production code */
@@ -36,12 +38,13 @@ interface UType : UElement, UNamed, UFqNamed, UAnnotated, UResolvable, LeafUElem
override fun resolve(context: UastContext): UClass?
override fun resolveOrEmpty(context: UastContext) = resolve(context) ?: UClassNotResolved
override fun traverse(callback: UastCallback) {
annotations.handleTraverseList(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitType(this)) return
annotations.acceptList(visitor)
}
}
interface UTypeReference : UDeclaration, UResolvable, LeafUElement {
interface UTypeReference : UDeclaration, UResolvable {
override fun renderString() = ""
override fun logString() = log("UTypeReference")
@@ -15,6 +15,8 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UVariable : UDeclaration, UModifierOwner, UAnnotated {
val initializer: UExpression?
val kind: UastVariableKind
@@ -27,11 +29,12 @@ interface UVariable : UDeclaration, UModifierOwner, UAnnotated {
open val setters: List<UFunction>?
get() = null
override fun traverse(callback: UastCallback) {
nameElement?.handleTraverse(callback)
initializer?.handleTraverse(callback)
annotations.handleTraverseList(callback)
type.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitVariable(this)) return
nameElement?.accept(visitor)
initializer?.accept(visitor)
annotations.acceptList(visitor)
type.accept(visitor)
}
override fun renderString(): String {
@@ -15,13 +15,16 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UArrayAccessExpression : UExpression {
val receiver: UExpression
val indices: List<UExpression>
override fun traverse(callback: UastCallback) {
receiver.handleTraverse(callback)
indices.handleTraverseList(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitArrayAccessExpression(this)) return
receiver.accept(visitor)
indices.acceptList(visitor)
}
override fun logString() = log("UArrayAccessExpression", receiver, indices)
@@ -15,14 +15,17 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UBinaryExpression : UExpression {
val leftOperand: UExpression
val operator: UastBinaryOperator
val rightOperand: UExpression
override fun traverse(callback: UastCallback) {
leftOperand.handleTraverse(callback)
rightOperand.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitBinaryExpression(this)) return
leftOperand.accept(visitor)
rightOperand.accept(visitor)
}
override fun logString() =
@@ -15,6 +15,8 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UBinaryExpressionWithType : UExpression {
val operand: UExpression
val operationKind: UastBinaryExpressionWithTypeKind
@@ -23,8 +25,9 @@ interface UBinaryExpressionWithType : UExpression {
override fun logString() = log("UBinaryExpressionWithType (${getExpressionType()?.name}, ${operationKind.name})", operand)
override fun renderString() = "(${operand.renderString()}) ${operationKind.name} ${getExpressionType()?.name}"
override fun traverse(callback: UastCallback) {
operand.handleTraverse(callback)
type.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitBinaryExpressionWithType(this)) return
operand.accept(visitor)
type.accept(visitor)
}
}
@@ -15,10 +15,16 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UBlockExpression : UExpression {
val expressions: List<UExpression>
override fun traverse(callback: UastCallback) = expressions.handleTraverseList(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitBlockExpression(this)) return
expressions.acceptList(visitor)
}
override fun logString() = log("UBlockExpression", expressions)
override fun renderString() = buildString {
@@ -15,6 +15,8 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UCallExpression : UExpression, UResolvable {
val functionReference: USimpleReferenceExpression?
@@ -36,12 +38,13 @@ interface UCallExpression : UExpression, UResolvable {
override fun resolve(context: UastContext): UFunction?
override fun resolveOrEmpty(context: UastContext): UFunction = resolve(context) ?: UFunctionNotResolved
override fun traverse(callback: UastCallback) {
functionReference?.handleTraverse(callback)
classReference?.handleTraverse(callback)
functionNameElement?.handleTraverse(callback)
valueArguments.handleTraverseList(callback)
typeArguments.handleTraverseList(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitCallExpression(this)) return
functionReference?.accept(visitor)
classReference?.accept(visitor)
functionNameElement?.accept(visitor)
valueArguments.acceptList(visitor)
typeArguments.acceptList(visitor)
}
override fun logString() = log("UFunctionCallExpression ($kind, argCount = $valueArgumentCount)", functionReference, valueArguments)
@@ -15,11 +15,14 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UCallableReferenceExpression : UExpression {
val qualifierType: UType
override fun traverse(callback: UastCallback) {
qualifierType.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitCallableReferenceExpression(this)) return
qualifierType.accept(visitor)
}
override fun logString() = "UCallableReferenceExpression"
@@ -15,9 +15,15 @@
*/
package org.jetbrains.uast
interface UClassLiteralExpression : UExpression, LeafUElement {
import org.jetbrains.uast.visitor.UastVisitor
interface UClassLiteralExpression : UExpression {
override fun logString() = "UClassLiteralExpression"
override fun renderString() = getExpressionType()?.name ?: "::class"
val type: UType
override fun accept(visitor: UastVisitor) {
visitor.visitClassLiteralExpression(this)
}
}
@@ -15,14 +15,18 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UDeclarationsExpression : UExpression {
val declarations: List<UElement>
val variables: List<UVariable>
get() = declarations.filterIsInstance<UVariable>()
override fun evaluate() = null
override fun traverse(callback: UastCallback) = declarations.handleTraverseList(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitDeclarationsExpression(this)) return
declarations.acceptList(visitor)
}
override fun renderString() = declarations.joinToString("\n") { it.renderString() }
override fun logString() = log("UDeclarationsExpression", declarations)
@@ -31,6 +35,4 @@ interface UDeclarationsExpression : UExpression {
class SimpleUDeclarationsExpression(
override val parent: UElement,
override val declarations: List<UElement>
) : UDeclarationsExpression {
override fun evaluate() = null
}
) : UDeclarationsExpression
@@ -15,11 +15,15 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UExpressionList : UExpression {
val expressions: List<UExpression>
override fun evaluate(): Any? = null
override fun traverse(callback: UastCallback) = expressions.forEach { it.handleTraverse(callback) }
override fun accept(visitor: UastVisitor) {
if (visitor.visitExpressionList(this)) return
expressions.acceptList(visitor)
}
override fun logString() = log("UExpressionList", expressions)
override fun renderString() = log("", expressions)
@@ -15,12 +15,15 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface ULabeledExpression : UExpression {
val label: String
val expression: UExpression
override fun traverse(callback: UastCallback) {
expression.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitLabeledExpression(this)) return
expression.accept(visitor)
}
override fun evaluate() = expression.evaluate()
@@ -15,13 +15,16 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface ULambdaExpression : UExpression {
val valueParameters: List<UVariable>
val body: UExpression
override fun traverse(callback: UastCallback) {
valueParameters.handleTraverseList(callback)
body.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitLambdaExpression(this)) return
valueParameters.acceptList(visitor)
body.accept(visitor)
}
override fun logString() = log("ULambdaExpression", valueParameters, body)
@@ -15,6 +15,8 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface ULiteralExpression : UExpression {
val value: Any?
@@ -28,7 +30,10 @@ interface ULiteralExpression : UExpression {
fun asString() = value?.toString() ?: ""
override fun traverse(callback: UastCallback) {}
override fun accept(visitor: UastVisitor) {
visitor.visitLiteralExpression(this)
}
override fun logString() = "ULiteralExpression (${asString()})"
override fun renderString() = asString()
}
@@ -15,14 +15,17 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
class UNamedExpression(
override val name: String,
override val parent: UElement
): UExpression, UNamed {
lateinit var expression: UExpression
override fun traverse(callback: UastCallback) {
expression.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitElement(this)) return
expression.accept(visitor)
}
override fun logString() = log("UNamedExpression ($name)", expression)
@@ -15,11 +15,14 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UObjectLiteralExpression : UExpression {
val declaration: UClass
override fun traverse(callback: UastCallback) {
declaration.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitObjectLiteralExpression(this)) return
declaration.accept(visitor)
}
override fun logString() = log("UObjectLiteralExpression", declaration)
@@ -15,11 +15,14 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UParenthesizedExpression : UExpression {
val expression: UExpression
override fun traverse(callback: UastCallback) {
expression.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitParenthesizedExpression(this)) return
expression.accept(visitor)
}
override fun evaluate() = expression.evaluate()
@@ -15,6 +15,8 @@
*/
package org.jetbrains.uast
import org.jetbrains.uast.visitor.UastVisitor
interface UQualifiedExpression : UExpression, UResolvable {
val receiver: UExpression
val selector: UExpression
@@ -24,9 +26,10 @@ interface UQualifiedExpression : UExpression, UResolvable {
override fun renderString() = receiver.renderString() + accessType.name + selector.renderString()
override fun traverse(callback: UastCallback) {
receiver.handleTraverse(callback)
selector.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitQualifiedExpression(this)) return
receiver.accept(visitor)
selector.accept(visitor)
}
override fun logString() = log("UQualifiedExpression", receiver, selector)
@@ -15,9 +15,15 @@
*/
package org.jetbrains.uast
interface USimpleReferenceExpression : UExpression, UResolvable, LeafUElement {
import org.jetbrains.uast.visitor.UastVisitor
interface USimpleReferenceExpression : UExpression, UResolvable {
val identifier: String
override fun accept(visitor: UastVisitor) {
visitor.visitSimpleReferenceExpression(this)
}
override fun logString() = "USimpleReferenceExpression ($identifier)"
override fun renderString() = identifier
}
@@ -15,7 +15,13 @@
*/
package org.jetbrains.uast
interface USuperExpression : UExpression, LeafUElement {
import org.jetbrains.uast.visitor.UastVisitor
interface USuperExpression : UExpression {
override fun logString() = "USuperExpression"
override fun renderString() = "super"
override fun accept(visitor: UastVisitor) {
visitor.visitSuperExpression(this)
}
}
@@ -15,7 +15,13 @@
*/
package org.jetbrains.uast
interface UThisExpression : UExpression, LeafUElement {
import org.jetbrains.uast.visitor.UastVisitor
interface UThisExpression : UExpression {
override fun logString() = "UThisExpression"
override fun renderString() = "this"
override fun accept(visitor: UastVisitor) {
visitor.visitThisExpression(this)
}
}
@@ -16,24 +16,38 @@
package org.jetbrains.uast
import org.jetbrains.uast.kinds.UastOperator
import org.jetbrains.uast.visitor.UastVisitor
interface UUnaryExpression : UExpression {
val operand: UExpression
val operator: UastOperator
override fun traverse(callback: UastCallback) {
operand.handleTraverse(callback)
override fun accept(visitor: UastVisitor) {
if (visitor.visitUnaryExpression(this)) return
operand.accept(visitor)
}
}
interface UPrefixExpression : UUnaryExpression {
override val operator: UastPrefixOperator
override fun accept(visitor: UastVisitor) {
if (visitor.visitPrefixExpression(this)) return
operand.accept(visitor)
}
override fun logString() = log("UPrefixExpression (${operator.text})", operand)
override fun renderString() = operator.text + operand.renderString()
}
interface UPostfixExpression : UUnaryExpression {
override val operator: UastPostfixOperator
override fun accept(visitor: UastVisitor) {
if (visitor.visitPostfixExpression(this)) return
operand.accept(visitor)
}
override fun logString() = log("UPostfixExpression (${operator.text})", operand)
override fun renderString() = operand.renderString() + operator.text
}