Skip to content

Commit be3bf99

Browse files
committed
feat(ast): Update AST layer to impove Bicep support
1 parent a40c828 commit be3bf99

File tree

13 files changed

+673
-337
lines changed

13 files changed

+673
-337
lines changed

ql/lib/codeql/bicep/ast/AstNodes.qll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ private import codeql.bicep.ast.internal.TreeSitter
44
private import codeql.bicep.ast.internal.AstNodes
55
private import codeql.bicep.ast.internal.TreeSitter
66
private import codeql.bicep.controlflow.ControlFlowGraph
7+
private import Variables
78

89
/**
910
* An AST node of a Bicep program.
@@ -112,6 +113,20 @@ class AstNode extends TAstNode {
112113
*
113114
* This returns the name of the most specific QL class that describes this node.
114115
* Used primarily for debugging and toString() representations.
116+
* Gets the lexical scope containing this AST node.
117+
*/
118+
cached
119+
Scope getScope() {
120+
result = this.getParent+() and
121+
not exists(AstNode mid |
122+
mid = this.getParent+() and
123+
mid instanceof Scope and
124+
mid.getParent+() = result
125+
)
126+
}
127+
128+
/**
129+
* Gets the primary QL class for the ast node.
115130
*/
116131
string getAPrimaryQlClass() { result = "???" }
117132
}

ql/lib/codeql/bicep/ast/Calls.qll

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
private import AstNodes
22
private import Expr
3+
private import Stmts
34
private import Idents
45
private import Misc
5-
private import internal.Arguments
6+
private import internal.Calls
67
private import internal.CallExpression
78

89
/**
@@ -12,7 +13,7 @@ private import internal.CallExpression
1213
* a call to a function or method. It provides common functionality for accessing
1314
* the name and identifier of the called entity.
1415
*/
15-
abstract class Callable extends Expr {
16+
class Callable extends Expr, Stmts instanceof CallableImpl {
1617
/**
1718
* Gets the identifier of the callable expression.
1819
*
@@ -42,6 +43,8 @@ abstract class Callable extends Expr {
4243
predicate hasName(string name) { this.getName() = name }
4344
}
4445

46+
class Call extends Expr instanceof CallImpl {}
47+
4548
/**
4649
* Represents a function or method call expression in the AST.
4750
*
@@ -50,7 +53,7 @@ abstract class Callable extends Expr {
5053
* invoke functions defined in the language, user-defined functions, or
5154
* module functions to compute values or perform operations.
5255
*/
53-
class CallExpression extends Expr instanceof CallExpressionImpl {
56+
class CallExpression extends Call instanceof CallExpressionImpl {
5457
/**
5558
* Gets the identifier of the call expression.
5659
*
@@ -93,26 +96,3 @@ class CallExpression extends Expr instanceof CallExpressionImpl {
9396
*/
9497
Arguments getDeclaredArguments() { result = CallExpressionImpl.super.getArguments() }
9598
}
96-
97-
/**
98-
* Represents a collection of arguments in a function call.
99-
*
100-
* This class models the set of arguments passed to a function, allowing
101-
* access to individual arguments by index or to the complete set of arguments.
102-
*/
103-
class Arguments extends AstNode instanceof ArgumentsImpl {
104-
/**
105-
* Gets the argument at the specified index.
106-
*
107-
* @param index The zero-based index of the argument to retrieve
108-
* @return The expression node of the argument at the specified index
109-
*/
110-
Expr getArgument(int index) { result = ArgumentsImpl.super.getArgument(index) }
111-
112-
/**
113-
* Gets all arguments in the collection.
114-
*
115-
* @return All argument expressions in this arguments collection
116-
*/
117-
Expr getArguments() { result = ArgumentsImpl.super.getArguments() }
118-
}

ql/lib/codeql/bicep/ast/Expr.qll

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ private import AstNodes
66
private import internal.AstNodes
77
private import internal.TreeSitter
88
private import internal.Expr
9+
private import internal.Arguments
910
private import internal.AssignmentExpression
1011
private import internal.BinaryExpression
1112
private import internal.Expression
@@ -29,7 +30,31 @@ private import Resources
2930
* Expressions can be nested and can appear in various contexts such as assignments,
3031
* parameter values, and return statements.
3132
*/
32-
final class Expr extends AstNode instanceof ExprImpl { }
33+
class Expr extends AstNode instanceof ExprImpl { }
34+
35+
/**
36+
* Represents a collection of arguments in a function call.
37+
*
38+
* This class models the set of arguments passed to a function, allowing
39+
* access to individual arguments by index or to the complete set of arguments.
40+
* Arguments are expressions that are passed to a function or method call.
41+
*/
42+
class Arguments extends Expr instanceof ArgumentsImpl {
43+
/**
44+
* Gets the argument at the specified index.
45+
*
46+
* @param index The zero-based index of the argument to retrieve
47+
* @return The expression node of the argument at the specified index
48+
*/
49+
Expr getArgument(int index) { result = ArgumentsImpl.super.getArgument(index) }
50+
51+
/**
52+
* Gets all arguments in the collection.
53+
*
54+
* @return All argument expressions in this arguments collection
55+
*/
56+
Expr getArguments() { result = ArgumentsImpl.super.getArguments() }
57+
}
3358

3459
/**
3560
* An assignment expression in the AST.
@@ -94,7 +119,7 @@ class BinaryExpression extends Expr instanceof BinaryExpressionImpl {
94119
* specific expression categories. It serves as a base implementation for
95120
* expressions in the Bicep language.
96121
*/
97-
final class Expression extends Expr instanceof ExpressionImpl { }
122+
class Expression extends Expr instanceof ExpressionImpl { }
98123

99124
/**
100125
* An interpolation expression in the AST.
@@ -103,7 +128,7 @@ final class Expression extends Expr instanceof ExpressionImpl { }
103128
* string literals. Interpolations allow embedding dynamic values or expressions
104129
* within string literals.
105130
*/
106-
final class Interpolation extends Expr instanceof InterpolationImpl {
131+
class Interpolation extends Expr instanceof InterpolationImpl {
107132
/**
108133
* Gets the expression contained within the interpolation.
109134
*
@@ -131,7 +156,7 @@ final class Interpolation extends Expr instanceof InterpolationImpl {
131156
* filters, or other functional programming patterns. A lambda expression
132157
* consists of parameters and a body that defines the computation to be performed.
133158
*/
134-
final class LambdaExpression extends Expr instanceof LambdaExpressionImpl { }
159+
class LambdaExpression extends Expr instanceof LambdaExpressionImpl { }
135160

136161
/**
137162
* A member expression in the AST.
@@ -171,7 +196,7 @@ class MemberExpression extends Expr instanceof MemberExpressionImpl {
171196
*
172197
* This alias provides a shorter name for convenience.
173198
*/
174-
final class MemberExpr = MemberExpression;
199+
class MemberExpr = MemberExpression;
175200

176201
/**
177202
* A nullable type expression in the AST.
@@ -180,7 +205,7 @@ final class MemberExpr = MemberExpression;
180205
* after the type name (e.g., `string?`). Nullable types explicitly allow
181206
* the value to be null in addition to values of the underlying type.
182207
*/
183-
final class NullableType extends Expr instanceof NullableTypeImpl { }
208+
class NullableType extends Expr instanceof NullableTypeImpl { }
184209

185210
/**
186211
* A parenthesized expression in the AST.
@@ -219,7 +244,7 @@ class ParenthesizedExpression extends Expr instanceof ParenthesizedExpressionImp
219244
* identifier, or other fundamental expression type. Primary expressions
220245
* serve as the building blocks for more complex expressions.
221246
*/
222-
final class PrimaryExpression extends Expr instanceof PrimaryExpressionImpl { }
247+
class PrimaryExpression extends Expr instanceof PrimaryExpressionImpl { }
223248

224249
/**
225250
* A resource expression in the AST.
@@ -228,7 +253,7 @@ final class PrimaryExpression extends Expr instanceof PrimaryExpressionImpl { }
228253
* Resource expressions are fundamental to Bicep as they define the
229254
* infrastructure resources to be provisioned.
230255
*/
231-
final class ResourceExpression extends Expr instanceof ResourceExpressionImpl { }
256+
class ResourceExpression extends Expr instanceof ResourceExpressionImpl { }
232257

233258
/**
234259
* A ternary expression in the AST.
@@ -237,7 +262,7 @@ final class ResourceExpression extends Expr instanceof ResourceExpressionImpl {
237262
* The expression evaluates the condition and returns one of two values based on whether
238263
* the condition is true or false.
239264
*/
240-
final class TernaryExpression extends Expr instanceof TernaryExpressionImpl { }
265+
class TernaryExpression extends Expr instanceof TernaryExpressionImpl { }
241266

242267
/**
243268
* A unary expression in the AST.
@@ -246,4 +271,4 @@ final class TernaryExpression extends Expr instanceof TernaryExpressionImpl { }
246271
* Examples include negation (`!expr`), numeric negation (`-expr`),
247272
* and other operations that apply to a single value.
248273
*/
249-
final class UnaryExpression extends Expr instanceof UnaryExpressionImpl { }
274+
class UnaryExpression extends Expr instanceof UnaryExpressionImpl { }

0 commit comments

Comments
 (0)