|
| 1 | +/** |
| 2 | + * INTERNAL: This module contains the customizable definition of `CallExpr` and should not |
| 3 | + * be referenced directly. |
| 4 | + */ |
| 5 | +module Impl { |
| 6 | + private import rust |
| 7 | + private import codeql.rust.elements.internal.CallImpl::Impl as CallImpl |
| 8 | + private import codeql.rust.elements.internal.ParenArgsExprImpl::Impl as ParenArgsExprImpl |
| 9 | + private import codeql.rust.internal.TypeInference as TypeInference |
| 10 | + |
| 11 | + private predicate isClosureCall(ParenArgsExpr call) { |
| 12 | + exists(Expr receiver | receiver = call.getBase() | |
| 13 | + // All calls to complex expressions and local variable accesses are lambda calls |
| 14 | + receiver instanceof PathExpr implies receiver = any(Variable v).getAnAccess() |
| 15 | + ) |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * A call expression. For example: |
| 20 | + * ```rust |
| 21 | + * foo(42); |
| 22 | + * foo::<u32, u64>(42); |
| 23 | + * foo[0](42); |
| 24 | + * foo(1) = 4; |
| 25 | + * ``` |
| 26 | + */ |
| 27 | + class CallExpr extends CallImpl::Call, ParenArgsExprImpl::ParenArgsExpr { |
| 28 | + CallExpr() { |
| 29 | + forall(Addressable target | |
| 30 | + // Cannot use `this.getResolvedTarget()` as that results in non-monotonic recursion |
| 31 | + target = TypeInference::resolveCallTarget(this) |
| 32 | + | |
| 33 | + target instanceof Callable |
| 34 | + ) |
| 35 | + or |
| 36 | + isClosureCall(this) |
| 37 | + } |
| 38 | + |
| 39 | + private predicate isMethodCall() { this.getResolvedTarget() instanceof Method } |
| 40 | + |
| 41 | + override Expr getArgument(int i) { |
| 42 | + if this.isMethodCall() |
| 43 | + then result = super.getArgList().getArg(i + 1) |
| 44 | + else result = super.getArgList().getArg(i) |
| 45 | + } |
| 46 | + |
| 47 | + override Expr getReceiver() { this.isMethodCall() and result = super.getArgList().getArg(0) } |
| 48 | + |
| 49 | + // todo: remove once internal query has been updated |
| 50 | + Expr getFunction() { result = this.getBase() } |
| 51 | + |
| 52 | + // todo: remove once internal query has been updated |
| 53 | + Expr getArg(int i) { result = this.getArgument(i) } |
| 54 | + |
| 55 | + // todo: remove once internal query has been updated |
| 56 | + int getNumberOfArgs() { result = this.getNumberOfArguments() } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * A call expression that targets a closure. |
| 61 | + * |
| 62 | + * Closure calls never have a static target, and the set of potential |
| 63 | + * run-time targets is only available internally to the data flow library. |
| 64 | + */ |
| 65 | + class ClosureCallExpr extends CallExpr { |
| 66 | + ClosureCallExpr() { isClosureCall(this) } |
| 67 | + |
| 68 | + /** Gets the closure expression being called. */ |
| 69 | + Expr getClosureExpr() { result = super.getBase() } |
| 70 | + } |
| 71 | +} |
0 commit comments