Swift: MethodLookupExpr.getMethodRef() [hand-written]

This refactors SelfApplyExpr.getFunction() as MethodLookupExpr().getMethodRef().

This is simpler, because we are not hiding DeclRefExprs or reinventing hidden AST resolution.
This commit is contained in:
Nora Dimitrijević
2022-12-20 23:08:32 -05:00
parent 1c64bf4bbc
commit 4900e4030a
6 changed files with 15 additions and 29 deletions

View File

@@ -4,5 +4,5 @@ private import codeql.swift.elements.expr.MethodLookupExpr
predicate constructArgument(Raw::Argument id) {
// exclude an argument that will be part of a SelfApplyExpr
// that will be transformed into a MethodLookupExpr
not exists(Raw::SelfApplyExpr e | id.getExpr() = e.getBase())
not exists(Raw::SelfApplyExpr e | id = e.getArgument(0))
}

View File

@@ -1,8 +1,4 @@
// generated by codegen/codegen.py, remove this comment if you wish to edit this file
private import codeql.swift.generated.Raw
private import codeql.swift.elements.expr.MethodLookupExprConstructor
predicate constructDeclRefExpr(Raw::DeclRefExpr id) {
// exclude an argument that will be part of a SelfApplyExpr
// that will be transformed into a MethodLookupExpr
not exists(Raw::SelfApplyExpr e | id.getDecl() = extractDeclFromSelfApplyExpr(e))
}
predicate constructDeclRefExpr(Raw::DeclRefExpr id) { any() }

View File

@@ -1,6 +1,8 @@
private import codeql.swift.generated.expr.MethodLookupExpr
private import codeql.swift.elements.expr.MethodLookupExprConstructor
private import codeql.swift.elements.expr.Expr
private import codeql.swift.elements.expr.DeclRefExpr
private import codeql.swift.elements.expr.OtherConstructorDeclRefExpr
private import codeql.swift.elements.decl.Decl
private import codeql.swift.elements.decl.MethodDecl
private import codeql.swift.generated.Raw
@@ -14,7 +16,13 @@ class MethodLookupExpr extends Generated::MethodLookupExpr {
}
override Decl getImmediateMember() {
result = Synth::convertDeclFromRaw(extractDeclFromSelfApplyExpr(this.getUnderlying()))
result = this.getMethodRef().(DeclRefExpr).getDecl()
or
result = this.getMethodRef().(OtherConstructorDeclRefExpr).getConstructorDecl()
}
override Expr getImmediateMethodRef() {
result = Synth::convertExprFromRaw(this.getUnderlying().getFunction())
}
MethodDecl getMethod() { result = this.getMember() }

View File

@@ -1,17 +1,3 @@
private import codeql.swift.generated.Raw
predicate constructMethodLookupExpr(Raw::SelfApplyExpr id) { any() }
Raw::Decl extractDeclFromSelfApplyExpr(Raw::SelfApplyExpr e) {
exists(Raw::Expr unwrappedFunction | unwrappedFunction = unwrapConversion*(e.getFunction()) |
result =
[
unwrappedFunction.(Raw::DeclRefExpr).getDecl(),
unwrappedFunction.(Raw::OtherConstructorDeclRefExpr).getConstructorDecl()
]
)
}
private Raw::Expr unwrapConversion(Raw::Expr e) {
e.(Raw::ImplicitConversionExpr).getSubExpr() = result
}

View File

@@ -1,8 +1,4 @@
// generated by codegen/codegen.py, remove this comment if you wish to edit this file
private import codeql.swift.generated.Raw
private import codeql.swift.elements.expr.MethodLookupExprConstructor
predicate constructOtherConstructorDeclRefExpr(Raw::OtherConstructorDeclRefExpr id) {
// exclude an argument that will be part of a SelfApplyExpr
// that will be transformed into a MethodLookupExpr
not exists(Raw::SelfApplyExpr e | id.getConstructorDecl() = extractDeclFromSelfApplyExpr(e))
}
predicate constructOtherConstructorDeclRefExpr(Raw::OtherConstructorDeclRefExpr id) { any() }

View File

@@ -677,7 +677,7 @@ class DotSyntaxCallExpr(SelfApplyExpr):
@synth.from_class(SelfApplyExpr)
class MethodLookupExpr(LookupExpr):
pass
method_ref: Expr | child | doc("the underlying method declaration reference expression")
class DynamicMemberRefExpr(DynamicLookupExpr):
pass