Skip to content

Commit 6d89a1e

Browse files
asgerfCopilot
andcommitted
Unified: Extract setter methods as function declarations
A setter method, such as a Swift subscript setter, is now a plain function_declaration identified by its '#newValue' parameter, rather than an accessor_declaration of kind 'setter_method'. This also drops the type_parameter field from accessor_declaration that was only needed for generic subscript setters. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 745b2c2 commit 6d89a1e

10 files changed

Lines changed: 33 additions & 59 deletions

File tree

‎unified/extractor/ast_types.yml‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,13 @@ named:
342342
type: expr
343343

344344
# A top-level function declaration.
345+
#
346+
# A function with a parameter whose external name is the reserved name
347+
# `#newValue` is a setter method, such as the setter of a Swift subscript. A
348+
# call `x.foo(...)` that is the target of an assignment, or is otherwise
349+
# updated in place, invokes the setter method named `foo` (whereas a call in
350+
# a read position invokes the ordinary method `foo`), passing the new value
351+
# as the `#newValue` argument.
345352
function_declaration:
346353
modifier*: modifier
347354
name_node: identifier
@@ -536,23 +543,15 @@ named:
536543
# downstream. Stored properties with observers are emitted as a
537544
# variable_declaration followed by one accessor_declaration per observer
538545
# (each observer also tagged with `chained_declaration`).
539-
#
540-
# An accessor of kind `setter_method` is a setter invoked via call syntax,
541-
# such as the setter of a Swift subscript. A call `x.foo(...)` that is the
542-
# target of an assignment, or is otherwise updated in place, invokes the
543-
# `setter_method` accessor named `foo` (whereas a call in a read position
544-
# invokes the ordinary method `foo`). The setter takes the new value as an
545-
# extra trailing parameter with the reserved external name `#newValue`.
546546
accessor_declaration:
547547
modifier*: modifier
548548
name_node: identifier
549549
accessor_kind: accessor_kind
550-
type_parameter*: type_parameter
551550
parameter*: parameter
552551
return_type?: expr
553552
body?: block
554553

555-
# "get", "set", "setter_method", or a language-specific kind like "didSet"
554+
# "get", "set", or a language-specific kind like "didSet"
556555
accessor_kind:
557556

558557
# Static or instance initializer block. That is, code that runs at initialization time of either the class or an instance.

‎unified/extractor/src/languages/swift/swift.rs‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,8 @@ struct SubscriptAccessor {
183183
body: Option<yeast::Id>,
184184
}
185185

186-
/// Emit the declaration for one accessor of a subscript: the getter is a
187-
/// `function_declaration` named `subscript`, and the setter an
188-
/// `accessor_declaration` of kind `setter_method`. The shared parts of the subscript (modifiers, generic and value
186+
/// Emit a `function_declaration` named `subscript` for one accessor of a
187+
/// subscript. The shared parts of the subscript (modifiers, generic and value
189188
/// parameters and the element type) are translated afresh for each accessor,
190189
/// so a getter/setter pair each get their own copy. A setter takes the new
191190
/// value as an extra trailing parameter named `#newValue` and has no return
@@ -211,12 +210,11 @@ fn subscript_method(
211210
match accessor.setter {
212211
Some((param_name, spec)) => {
213212
let implicit_param_name = param_name.is_none().then_some("newValue");
214-
tree!((accessor_declaration
213+
tree!((function_declaration
215214
modifier: {mods}
216215
modifier: {accessor.modifiers}
217216
modifier: [(modifier "chained_declaration") if { is_chained }]
218217
name_node: (identifier #{kw})
219-
accessor_kind: (accessor_kind "setter_method" location: {spec})
220218
type_parameter: {type_params}
221219
parameter: {params}
222220
// The new value is passed as a named parameter with the reserved
@@ -517,8 +515,8 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
517515
),
518516
// ---- Subscripts ----
519517
// A subscript declaration becomes a `function_declaration` named
520-
// `subscript` for its getter and, if it has a setter, an
521-
// `accessor_declaration` of kind `setter_method` named `subscript`.
518+
// `subscript` for its getter and, if it has a setter, another one for
519+
// the setter, which is identified by its `#newValue` parameter.
522520
// A subscript with an explicit accessor block publishes its shared parts
523521
// in `ctx` and lets the `accessorDecl` rule below emit one method per
524522
// accessor; each one after the first is tagged `chained_declaration`.

‎unified/extractor/tests/corpus/swift/subscripts/subscript-get-set.output‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,11 @@ top_level source="⟨body⟩"
259259
argument:
260260
argument source="⟨value⟩"
261261
value: identifier "index" source="index"
262-
accessor_declaration source="⟨name_node⟩(⟨parameter⟩, ⟨parameter⟩) -> Int {\n get { data[index] }\n ⟨modifier⟩ ⟨accessor_kind⟩(⟨parameter⟩) ⟨body⟩"
262+
function_declaration source="⟨name_node⟩(⟨parameter⟩, ⟨parameter⟩) -> Int {\n get { data[index] }\n ⟨modifier⟩ set(⟨parameter⟩) ⟨body⟩"
263263
modifier:
264264
modifier "nonmutating" source="nonmutating"
265265
modifier "chained_declaration" source=""
266266
name_node: identifier "subscript" source="subscript"
267-
accessor_kind: accessor_kind "setter_method" source="set"
268267
parameter:
269268
parameter source="⟨external_name_node⟩ ⟨pattern⟩: ⟨type⟩"
270269
external_name_node: identifier "at" source="at"
@@ -286,10 +285,9 @@ top_level source="⟨body⟩"
286285
argument:
287286
argument source="⟨value⟩"
288287
value: identifier "v" source="v"
289-
accessor_declaration source="⟨modifier⟩ ⟨name_node⟩<⟨type_parameter⟩>(⟨parameter⟩) -> T {\n ⟨accessor_kind⟩⟨parameter⟩ ⟨body⟩"
288+
function_declaration source="⟨modifier⟩ ⟨name_node⟩<⟨type_parameter⟩>(⟨parameter⟩) -> T {\n ⟨parameter⟩ ⟨body⟩"
290289
modifier: modifier "static" source="static"
291290
name_node: identifier "subscript" source="subscript"
292-
accessor_kind: accessor_kind "setter_method" source="set"
293291
type_parameter:
294292
type_parameter source="⟨name_node⟩"
295293
name_node: identifier "T" source="T"

‎unified/extractor/tests/corpus/swift/subscripts/subscript-protocol-requirement.output‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,9 @@ top_level source="⟨body⟩"
7676
pattern: identifier "i" source="i"
7777
return_type: identifier "String" source="String"
7878
body: block source=""
79-
accessor_declaration source="⟨name_node⟩(⟨parameter⟩) -> String { get ⟨accessor_kind⟩⟨parameter⟩"
79+
function_declaration source="⟨name_node⟩(⟨parameter⟩) -> String { get ⟨parameter⟩"
8080
modifier: modifier "chained_declaration" source=""
8181
name_node: identifier "subscript" source="subscript"
82-
accessor_kind: accessor_kind "setter_method" source="set"
8382
parameter:
8483
parameter source="⟨pattern⟩: ⟨type⟩"
8584
type: identifier "Int" source="Int"

‎unified/ql/lib/codeql/unified/internal/Ast.qll‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,14 @@ module Unified {
128128
unified_accessor_declaration_return_type(this, result)
129129
}
130130

131-
/** Gets the node corresponding to the field `type_parameter`. */
132-
final F::TypeParameter getTypeParameter(int i) {
133-
unified_accessor_declaration_type_parameter(this, i, result)
134-
}
135-
136-
/** Gets the node corresponding to the field `type_parameter`. */
137-
final F::TypeParameter getATypeParameter() { result = this.getTypeParameter(_) }
138-
139131
/** Gets a field or child node of this node. */
140132
final override F::AstNode getAFieldOrChild() {
141133
unified_accessor_declaration_def(this, result, _) or
142134
unified_accessor_declaration_body(this, result) or
143135
unified_accessor_declaration_modifier(this, _, result) or
144136
unified_accessor_declaration_def(this, _, result) or
145137
unified_accessor_declaration_parameter(this, _, result) or
146-
unified_accessor_declaration_return_type(this, result) or
147-
unified_accessor_declaration_type_parameter(this, _, result)
138+
unified_accessor_declaration_return_type(this, result)
148139
}
149140
}
150141

@@ -1650,8 +1641,6 @@ module Unified {
16501641
or
16511642
result = node.(AccessorDeclaration).getReturnType() and i = -1 and name = "getReturnType"
16521643
or
1653-
result = node.(AccessorDeclaration).getTypeParameter(i) and name = "getTypeParameter"
1654-
or
16551644
result = node.(Argument).getModifier(i) and name = "getModifier"
16561645
or
16571646
result = node.(Argument).getNameNode() and i = -1 and name = "getNameNode"

‎unified/ql/lib/codeql/unified/internal/FacadeAst.qll‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -476,21 +476,23 @@ module Unified {
476476
}
477477

478478
/**
479-
* An accessor declaration of kind `setter_method`, representing a setter that is invoked via call
480-
* syntax when a call appears as an assignment target or is otherwise updated in place. Such a call
481-
* to `foo` invokes the `setter_method` accessor named `foo`, rather than the ordinary method `foo`.
479+
* A function declaration with a parameter whose external name is the reserved name `#newValue`,
480+
* representing a setter that is invoked via call syntax when a call appears as an assignment
481+
* target or is otherwise updated in place. Such a call to `foo` invokes the setter method named
482+
* `foo`, rather than the ordinary method `foo`, passing the new value as the `#newValue` argument.
482483
*
483-
* The new value is passed as an additional trailing parameter with the reserved external name
484-
* `#newValue`. For example, the setter of a Swift subscript declaration:
484+
* For example, the setter of a Swift subscript declaration:
485485
* ```swift
486486
* subscript(i: Int) -> String {
487487
* get { "42" }
488488
* set { print(newValue) } // setter method declaration, with parameters `i` and `newValue`
489489
* }
490490
* ```
491491
*/
492-
final class SetterMethodDeclaration extends AccessorDeclaration {
493-
SetterMethodDeclaration() { this.getAccessorKind().getValue() = "setter_method" }
492+
final class SetterMethodDeclaration extends FunctionDeclaration {
493+
SetterMethodDeclaration() {
494+
this.getAParameter().getExternalNameNode().getValue() = "#newValue"
495+
}
494496
}
495497

496498
/** An `if` statement or conditional expression. */

‎unified/ql/lib/codeql/unified/internal/mad/LegacyMaD.qll‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ private string getArgLabelsFromCallable(Callable callable) {
128128
pragma[nomagic]
129129
private predicate callableSelector(Callable callable, string type, string name, string argLabels) {
130130
(
131-
name = callable.(FunctionDeclaration).getName()
131+
name = callable.(FunctionDeclaration).getName() and
132+
not callable instanceof SetterMethodDeclaration
132133
or
133134
name = callable.(SetterMethodDeclaration).getName() + ":set"
134135
) and

‎unified/ql/lib/codeql/unified/internal/typeinference/Type.qll‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,6 @@ class TypeParameterType extends TypeParameter, TTypeParameterType {
263263
typeParam = result.(Unified::TypeAliasDeclaration).getATypeParameter()
264264
or
265265
typeParam = result.(Unified::FunctionDeclaration).getATypeParameter()
266-
or
267-
typeParam = result.(Unified::AccessorDeclaration).getATypeParameter()
268266
}
269267

270268
override Member getScope() {

‎unified/ql/lib/codeql/unified/internal/typeinference/TypeInference.qll‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,7 @@ module Input3 implements InputSig3 {
286286
private class AccessorField extends FieldImpl, Callable {
287287
private AccessorDeclaration accessor;
288288

289-
AccessorField() {
290-
accessor = this.getCallable() and
291-
not accessor instanceof SetterMethodDeclaration // invoked via call syntax, not a field
292-
}
289+
AccessorField() { accessor = this.getCallable() }
293290

294291
override TypeMention getType() { result = accessor.getReturnType() }
295292

@@ -467,8 +464,6 @@ module Input3 implements InputSig3 {
467464

468465
TypeParameter getTypeParameter(int i) {
469466
result = TTypeParameterType(c.(FunctionDeclaration).getTypeParameter(i))
470-
or
471-
result = TTypeParameterType(c.(AccessorDeclaration).getTypeParameter(i))
472467
}
473468

474469
TypeMention getAdditionalTypeParameterConstraint(TypeParameter tp) {
@@ -572,7 +567,9 @@ module Input3 implements InputSig3 {
572567
isFunctionExprInvoke = false
573568
) and
574569
// handled as a field access
575-
not result.asCallable() instanceof AccessorDeclaration
570+
not result.asCallable() instanceof AccessorDeclaration and
571+
// only invoked from a setter position; see `resolveSetterCallTarget`
572+
not result.asCallable() instanceof SetterMethodDeclaration
576573
}
577574

578575
Callable getTarget(InvocationResolutionContext c) {
@@ -963,7 +960,7 @@ Callable resolveCallTarget(CallExpr ce) {
963960
* Gets the setter invoked when the given call expression appears as an assignment target,
964961
* or is otherwise updated in place.
965962
*
966-
* For a call `x.foo(...)` this is the `setter_method` accessor named `foo` of the receiver type,
963+
* For a call `x.foo(...)` this is the setter method named `foo` of the receiver type,
967964
* such as the setter of a subscript for a Swift subscript call `x[i]`.
968965
*/
969966
cached

‎unified/ql/lib/unified.dbscheme‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,6 @@ unified_accessor_declaration_return_type(
160160
unique int return_type: @unified_expr ref
161161
);
162162

163-
#keyset[unified_accessor_declaration, index]
164-
unified_accessor_declaration_type_parameter(
165-
int unified_accessor_declaration: @unified_accessor_declaration ref,
166-
int index: int ref,
167-
unique int type_parameter: @unified_type_parameter ref
168-
);
169-
170163
unified_accessor_declaration_def(
171164
unique int id: @unified_accessor_declaration,
172165
int accessor_kind: @unified_token_accessor_kind ref,

0 commit comments

Comments
 (0)