|
| 1 | +/** |
| 2 | + * This module provides the public class `CallExpr`. |
| 3 | + */ |
| 4 | + |
| 5 | +private import rust |
| 6 | +private import codeql.rust.elements.internal.CallImpl::Impl as CallImpl |
| 7 | +private import codeql.rust.internal.TypeInference as TypeInference |
| 8 | + |
| 9 | +/** |
| 10 | + * A call expression. For example: |
| 11 | + * ```rust |
| 12 | + * foo(42); |
| 13 | + * foo::<u32, u64>(42); |
| 14 | + * foo[0](42); |
| 15 | + * foo(1) = 4; |
| 16 | + * ``` |
| 17 | + */ |
| 18 | +final class CallExpr extends CallImpl::Call instanceof ParenArgsExpr { |
| 19 | + CallExpr() { |
| 20 | + forall(Addressable target | |
| 21 | + // Cannot use `this.getResolvedTarget()` as that results in non-monotonic recursion |
| 22 | + target = TypeInference::resolveCallTarget(this) |
| 23 | + | |
| 24 | + target instanceof Callable |
| 25 | + ) |
| 26 | + } |
| 27 | + |
| 28 | + private predicate isMethodCall() { this.getResolvedTarget() instanceof Method } |
| 29 | + |
| 30 | + override Expr getArgument(int i) { |
| 31 | + if this.isMethodCall() |
| 32 | + then result = super.getArgList().getArg(i + 1) |
| 33 | + else result = super.getArgList().getArg(i) |
| 34 | + } |
| 35 | + |
| 36 | + override Expr getReceiver() { this.isMethodCall() and result = super.getArgList().getArg(0) } |
| 37 | + |
| 38 | + /** |
| 39 | + * Gets the `index`th attr of this paren arguments expression (0-based). |
| 40 | + */ |
| 41 | + Attr getAttr(int index) { result = super.getAttr(index) } |
| 42 | + |
| 43 | + /** |
| 44 | + * Gets any of the attrs of this paren arguments expression. |
| 45 | + */ |
| 46 | + Attr getAnAttr() { result = this.getAttr(_) } |
| 47 | + |
| 48 | + /** |
| 49 | + * Gets the number of attrs of this paren arguments expression. |
| 50 | + */ |
| 51 | + int getNumberOfAttrs() { result = count(int i | exists(this.getAttr(i))) } |
| 52 | + |
| 53 | + /** |
| 54 | + * Gets the base of this paren arguments expression, if it exists. |
| 55 | + */ |
| 56 | + Expr getBase() { result = super.getBase() } |
| 57 | + |
| 58 | + /** |
| 59 | + * Holds if `getBase()` exists. |
| 60 | + */ |
| 61 | + predicate hasBase() { exists(this.getBase()) } |
| 62 | + |
| 63 | + // todo: remove once internal query has been updated |
| 64 | + Expr getFunction() { result = this.getBase() } |
| 65 | + |
| 66 | + // todo: remove once internal query has been updated |
| 67 | + Expr getArg(int i) { result = this.getArgument(i) } |
| 68 | + |
| 69 | + // todo: remove once internal query has been updated |
| 70 | + int getNumberOfArgs() { result = this.getNumberOfArguments() } |
| 71 | +} |
0 commit comments