|
| 1 | +/** |
| 2 | + * Provides logic for synthesizing parameters. |
| 3 | + */ |
| 4 | + |
| 5 | +private import unified |
| 6 | +private import AstPlugin |
| 7 | +private import NameBinding |
| 8 | + |
| 9 | +private newtype TParameterEx = |
| 10 | + TAstParameter(Parameter p) or |
| 11 | + TImplicitReceiverParameter(Callable c) { |
| 12 | + exists(LocalVariable v | v.isImplicitReceiverParameter(c)) |
| 13 | + } or |
| 14 | + TDefaultConstructorParameter(ConstructorDeclaration cd, int i, string name) { |
| 15 | + cd.hasModifier("generated") and |
| 16 | + defaultConstructorParameter(cd, i, name) |
| 17 | + } |
| 18 | + |
| 19 | +final class ParameterEx = ParameterExImpl; |
| 20 | + |
| 21 | +/** |
| 22 | + * A parameter from source code or a synthesized parameter. |
| 23 | + */ |
| 24 | +abstract private class ParameterExImpl extends TParameterEx { |
| 25 | + /** |
| 26 | + * Gets the source code parameter that this entity represents, if any. |
| 27 | + */ |
| 28 | + Parameter asParameter() { this = TAstParameter(result) } |
| 29 | + |
| 30 | + /** |
| 31 | + * Holds if this entity represents a regular parameter from source code. |
| 32 | + */ |
| 33 | + predicate isParameter() { exists(this.asParameter()) } |
| 34 | + |
| 35 | + /** |
| 36 | + * Holds if this entity represents an implicit receiver parameter of `c`. |
| 37 | + */ |
| 38 | + predicate isImplicitReceiverParameter(Callable c) { this = TImplicitReceiverParameter(c) } |
| 39 | + |
| 40 | + /** |
| 41 | + * Holds if this entity represents the `i`th parameter of the default |
| 42 | + * constructor `cd` with name `name`. |
| 43 | + */ |
| 44 | + predicate isDefaultConstructorParameter(ConstructorDeclaration cd, int i, string name) { |
| 45 | + this = TDefaultConstructorParameter(cd, i, name) |
| 46 | + } |
| 47 | + |
| 48 | + /** Gets the callable that this parameter belongs to. */ |
| 49 | + Callable getCallable() { |
| 50 | + this.asParameter() = |
| 51 | + [ |
| 52 | + result.(FunctionDeclaration).getAParameter(), |
| 53 | + result.(ConstructorDeclaration).getAParameter(), |
| 54 | + result.(AccessorDeclaration).getAParameter() |
| 55 | + ] |
| 56 | + or |
| 57 | + this.isImplicitReceiverParameter(result) |
| 58 | + or |
| 59 | + this.isDefaultConstructorParameter(result, _, _) |
| 60 | + } |
| 61 | + |
| 62 | + /** Gets a textual representation of this parameter. */ |
| 63 | + abstract string toString(); |
| 64 | + |
| 65 | + /** Gets the location of this parameter. */ |
| 66 | + abstract Location getLocation(); |
| 67 | +} |
| 68 | + |
| 69 | +private class AstParameterEx extends ParameterExImpl, TAstParameter { |
| 70 | + Parameter p; |
| 71 | + |
| 72 | + AstParameterEx() { this = TAstParameter(p) } |
| 73 | + |
| 74 | + override string toString() { result = p.toString() } |
| 75 | + |
| 76 | + override Location getLocation() { result = p.getLocation() } |
| 77 | +} |
| 78 | + |
| 79 | +private class ImplicitReceiverParameterEx extends ParameterExImpl, TImplicitReceiverParameter { |
| 80 | + Callable c; |
| 81 | + |
| 82 | + ImplicitReceiverParameterEx() { this = TImplicitReceiverParameter(c) } |
| 83 | + |
| 84 | + override string toString() { |
| 85 | + exists(LocalVariable v | |
| 86 | + v.isImplicitReceiverParameter(c) and |
| 87 | + result = v.getName() |
| 88 | + ) |
| 89 | + } |
| 90 | + |
| 91 | + override Location getLocation() { result = c.getLocation() } |
| 92 | +} |
| 93 | + |
| 94 | +private class DefaultConstructorParameterEx extends ParameterExImpl, TDefaultConstructorParameter { |
| 95 | + ConstructorDeclaration cd; |
| 96 | + int i; |
| 97 | + string name; |
| 98 | + |
| 99 | + DefaultConstructorParameterEx() { this = TDefaultConstructorParameter(cd, i, name) } |
| 100 | + |
| 101 | + override string toString() { result = name + " [" + cd.getName() + " default constructor]" } |
| 102 | + |
| 103 | + override Location getLocation() { result = cd.getLocation() } |
| 104 | +} |
0 commit comments