@@ -5,80 +5,82 @@ private import codeql.swift.elements.expr.ClosureExpr
55private import codeql.swift.elements.Callable
66private import codeql.swift.generated.ParentChild
77
8- private module Cached {
9- private Element getEnclosingDeclStep ( Element e ) {
10- not e instanceof Decl and
11- result = getImmediateParent ( e )
12- }
8+ module Impl {
9+ private module Cached {
10+ private Element getEnclosingDeclStep ( Element e ) {
11+ not e instanceof Decl and
12+ result = getImmediateParent ( e )
13+ }
1314
14- cached
15- Decl getEnclosingDecl ( AstNode ast ) { result = getEnclosingDeclStep * ( getImmediateParent ( ast ) ) }
15+ cached
16+ Decl getEnclosingDecl ( AstNode ast ) { result = getEnclosingDeclStep * ( getImmediateParent ( ast ) ) }
1617
17- private Element getEnclosingFunctionStep ( Element e ) {
18- not e instanceof Function and
19- result = getEnclosingDecl ( e )
20- }
18+ private Element getEnclosingFunctionStep ( Element e ) {
19+ not e instanceof Function and
20+ result = getEnclosingDecl ( e )
21+ }
2122
22- cached
23- Function getEnclosingFunction ( AstNode ast ) {
24- result = getEnclosingFunctionStep * ( getEnclosingDecl ( ast ) )
25- }
23+ cached
24+ Function getEnclosingFunction ( AstNode ast ) {
25+ result = getEnclosingFunctionStep * ( getEnclosingDecl ( ast ) )
26+ }
2627
27- private Element getEnclosingClosureStep ( Element e ) {
28- not e instanceof Callable and
29- result = getImmediateParent ( e )
30- }
28+ private Element getEnclosingClosureStep ( Element e ) {
29+ not e instanceof Callable and
30+ result = getImmediateParent ( e )
31+ }
3132
32- cached
33- ClosureExpr getEnclosingClosure ( AstNode ast ) {
34- result = getEnclosingClosureStep * ( getImmediateParent ( ast ) )
33+ cached
34+ ClosureExpr getEnclosingClosure ( AstNode ast ) {
35+ result = getEnclosingClosureStep * ( getImmediateParent ( ast ) )
36+ }
3537 }
36- }
3738
38- /**
39- * A node in the abstract syntax tree.
40- */
41- class AstNode extends Generated:: AstNode {
4239 /**
43- * Gets the nearest function definition that contains this AST node, if any.
44- * This includes functions, methods, (de)initializers, and accessors, but not closures.
45- *
46- * For example, in the following code, the AST node for `n + 1` has `foo` as its
47- * enclosing function (via `getEnclosingFunction`), whereas its enclosing callable is
48- * the closure `{(n : Int) in n + 1 }` (via `getEnclosingCallable`):
49- *
50- * ```swift
51- * func foo() {
52- * var f = { (n : Int) in n + 1 }
53- * }
54- * ```
40+ * A node in the abstract syntax tree.
5541 */
56- final Function getEnclosingFunction ( ) { result = Cached:: getEnclosingFunction ( this ) }
42+ class AstNode extends Generated:: AstNode {
43+ /**
44+ * Gets the nearest function definition that contains this AST node, if any.
45+ * This includes functions, methods, (de)initializers, and accessors, but not closures.
46+ *
47+ * For example, in the following code, the AST node for `n + 1` has `foo` as its
48+ * enclosing function (via `getEnclosingFunction`), whereas its enclosing callable is
49+ * the closure `{(n : Int) in n + 1 }` (via `getEnclosingCallable`):
50+ *
51+ * ```swift
52+ * func foo() {
53+ * var f = { (n : Int) in n + 1 }
54+ * }
55+ * ```
56+ */
57+ final Function getEnclosingFunction ( ) { result = Cached:: getEnclosingFunction ( this ) }
5758
58- /**
59- * Gets the nearest declaration that contains this AST node, if any.
60- *
61- * Note that the nearest declaration may be an extension of a type declaration. If you always
62- * want the type declaration and not the extension, use `getEnclosingDecl().asNominalTypeDecl()`.
63- */
64- final Decl getEnclosingDecl ( ) { result = Cached:: getEnclosingDecl ( this ) }
59+ /**
60+ * Gets the nearest declaration that contains this AST node, if any.
61+ *
62+ * Note that the nearest declaration may be an extension of a type declaration. If you always
63+ * want the type declaration and not the extension, use `getEnclosingDecl().asNominalTypeDecl()`.
64+ */
65+ final Decl getEnclosingDecl ( ) { result = Cached:: getEnclosingDecl ( this ) }
6566
66- /**
67- * Gets the nearest `Callable` that contains this AST node, if any.
68- * This includes (auto)closures, functions, methods, (de)initializers, and accessors.
69- *
70- * For example, in the following code, the AST node for `n + 1` has the closure
71- * `{(n : Int) in n + 1 }` as its enclosing callable.
72- *
73- * ```swift
74- * func foo() {
75- * var f = { (n : Int) in n + 1 }
76- * }
77- * ```
78- */
79- final Callable getEnclosingCallable ( ) {
80- if exists ( Cached:: getEnclosingClosure ( this ) )
81- then result = Cached:: getEnclosingClosure ( this )
82- else result = Cached:: getEnclosingFunction ( this )
67+ /**
68+ * Gets the nearest `Callable` that contains this AST node, if any.
69+ * This includes (auto)closures, functions, methods, (de)initializers, and accessors.
70+ *
71+ * For example, in the following code, the AST node for `n + 1` has the closure
72+ * `{(n : Int) in n + 1 }` as its enclosing callable.
73+ *
74+ * ```swift
75+ * func foo() {
76+ * var f = { (n : Int) in n + 1 }
77+ * }
78+ * ```
79+ */
80+ final Callable getEnclosingCallable ( ) {
81+ if exists ( Cached:: getEnclosingClosure ( this ) )
82+ then result = Cached:: getEnclosingClosure ( this )
83+ else result = Cached:: getEnclosingFunction ( this )
84+ }
8385 }
8486}
0 commit comments