Skip to content

Commit b56e387

Browse files
authored
Merge pull request #46 from GitHubSecurityLab/ast-types
AST Type Improvements
2 parents e132166 + a6fac2e commit b56e387

26 files changed

+351
-160
lines changed

ql/lib/codeql/bicep/AST.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ import ast.Misc
99
import ast.Idents
1010
import ast.Variables
1111
import ast.Resources
12+
import ast.Types

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ private import Expr
33
private import Stmts
44
private import Idents
55
private import Misc
6+
private import Types
67
private import internal.Calls
78
private import internal.CallExpression
89
private import internal.LambdaExpression

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
private import Expr
66
private import Stmts
77
private import internal.Conditionals
8+
private import internal.IfStatement
89

910

1011
class Conditionals extends Stmts instanceof ConditionalsImpl {
@@ -24,4 +25,34 @@ private import internal.Conditionals
2425
* this branch is selected in a switch/match statement.
2526
*/
2627
StmtSequence getBranch() { result = ConditionalsImpl.super.getBranch() }
28+
}
29+
30+
31+
/**
32+
* An if statement in the AST.
33+
*
34+
* Represents a conditional statement in Bicep that executes certain code
35+
* only when a specific condition is true. If statements enable conditional
36+
* resource creation or property setting based on input parameters or other factors.
37+
*/
38+
class IfStatement extends Stmts instanceof IfStatementImpl {
39+
/**
40+
* Gets the condition of the if statement.
41+
*
42+
* This is the expression that is evaluated to determine whether
43+
* the body of the if statement should be executed.
44+
*
45+
* @return The condition expression
46+
*/
47+
Expr getCondition() { result = IfStatementImpl.super.getCondition() }
48+
49+
/**
50+
* Gets the body of the if statement.
51+
*
52+
* This is the expression or block that will be executed if the
53+
* condition evaluates to true.
54+
*
55+
* @return The body expression
56+
*/
57+
Expr getBody() { result = IfStatementImpl.super.getBody() }
2758
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ private import Expr
33
private import internal.Idents
44
private import internal.Identifier
55
private import internal.PropertyIdentifier
6+
private import internal.CompatibleIdentifier
67

78
/**
89
* The base class for all identifiers in the AST.
@@ -42,6 +43,15 @@ class Identifier extends Idents instanceof IdentifierImpl {
4243
* Represents the name part of a property in an object literal. For example,
4344
* in the property `name: 'example'`, `name` is represented by a PropertyIdentifier.
4445
* Property identifiers are used as keys in object literals.
46+
*
47+
* In Bicep, property identifiers appear in object literals and resource declarations:
48+
*
49+
* ```bicep
50+
* var myObject = {
51+
* name: 'value', // 'name' is a PropertyIdentifier
52+
* type: 'string' // 'type' is a PropertyIdentifier
53+
* }
54+
* ```
4555
*/
4656
class PropertyIdentifier extends Idents instanceof PropertyIdentifierImpl {
4757
/**
@@ -51,3 +61,26 @@ class PropertyIdentifier extends Idents instanceof PropertyIdentifierImpl {
5161
*/
5262
override string getName() { result = PropertyIdentifierImpl.super.getName() }
5363
}
64+
65+
/**
66+
* A compatible identifier in the AST.
67+
*
68+
* Represents an identifier that is compatible with certain naming conventions.
69+
* Compatible identifiers are often used when a standard identifier is needed
70+
* in contexts that have specific compatibility requirements.
71+
*/
72+
class CompatibleIdentifier extends Idents instanceof CompatibleIdentifierImpl {
73+
/**
74+
* Gets the name of this compatible identifier as a string.
75+
*
76+
* @return The name of this compatible identifier
77+
*/
78+
override string getName() { result = CompatibleIdentifierImpl.super.getName() }
79+
80+
/**
81+
* Gets the underlying identifier.
82+
*
83+
* @return The underlying identifier
84+
*/
85+
Identifier getIdentifier() { result = CompatibleIdentifierImpl.super.getIdentifier() }
86+
}

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

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,19 @@
11
private import AstNodes
22
private import internal.Array
3-
private import internal.ArrayType
43
private import internal.Boolean
5-
private import internal.CompatibleIdentifier
64
private import internal.Decorator
75
private import internal.Decorators
86
private import internal.DiagnosticComment
97
private import internal.EscapeSequence
108
private import internal.ForLoopParameters
11-
private import internal.Identifier
129
private import internal.ImportFunctionality
1310
private import internal.LoopEnumerator
1411
private import internal.LoopVariable
1512
private import internal.MetadataDeclaration
1613
private import internal.ModuleDeclaration
17-
private import internal.NegatedType
1814
private import internal.ObjectProperty
19-
private import internal.ParameterizedType
20-
private import internal.ParenthesizedType
21-
private import internal.PrimitiveType
22-
private import internal.PropertyIdentifier
2315
private import internal.TargetScopeAssignment
2416
private import internal.TestBlock
25-
private import internal.Type
26-
private import internal.TypeArguments
27-
private import internal.TypeDeclaration
28-
private import internal.UnionType
29-
30-
/**
31-
* A ArrayType unknown AST node.
32-
*/
33-
class ArrayType extends AstNode instanceof ArrayTypeImpl { }
34-
35-
/**
36-
* A CompatibleIdentifier unknown AST node.
37-
*/
38-
class CompatibleIdentifier extends AstNode instanceof CompatibleIdentifierImpl { }
3917

4018
/**
4119
* A Decorator unknown AST node.
@@ -87,26 +65,6 @@ class MetadataDeclaration extends AstNode instanceof MetadataDeclarationImpl { }
8765
*/
8866
class ModuleDeclaration extends AstNode instanceof ModuleDeclarationImpl { }
8967

90-
/**
91-
* A NegatedType unknown AST node.
92-
*/
93-
class NegatedType extends AstNode instanceof NegatedTypeImpl { }
94-
95-
/**
96-
* A ParameterizedType unknown AST node.
97-
*/
98-
class ParameterizedType extends AstNode instanceof ParameterizedTypeImpl { }
99-
100-
/**
101-
* A ParenthesizedType unknown AST node.
102-
*/
103-
class ParenthesizedType extends AstNode instanceof ParenthesizedTypeImpl { }
104-
105-
/**
106-
* A PrimitiveType unknown AST node.
107-
*/
108-
class PrimitiveType extends AstNode instanceof PrimitiveTypeImpl { }
109-
11068
/**
11169
* A TargetScopeAssignment unknown AST node.
11270
*/
@@ -116,38 +74,3 @@ class TargetScopeAssignment extends AstNode instanceof TargetScopeAssignmentImpl
11674
* A TestBlock unknown AST node.
11775
*/
11876
class TestBlock extends AstNode instanceof TestBlockImpl { }
119-
120-
/**
121-
* A type node in the AST.
122-
*
123-
* This class represents all type annotations in Bicep, including primitive types
124-
* (like string, int, bool), complex types (like arrays, objects), and user-defined
125-
* types. Types are used in parameter declarations, variable declarations, function
126-
* return types, and other contexts to specify the kind of values that are expected.
127-
*/
128-
class Type extends AstNode instanceof TypeImpl {
129-
/**
130-
* Gets the name of this type as a string.
131-
*
132-
* For primitive types, this will be the name of the type (e.g., "string", "int").
133-
* For complex types, this will be a representation of the type structure.
134-
*
135-
* @return The type name or representation as a string
136-
*/
137-
string getType() { result = TypeImpl.super.getType() }
138-
}
139-
140-
/**
141-
* A TypeArguments unknown AST node.
142-
*/
143-
class TypeArguments extends AstNode instanceof TypeArgumentsImpl { }
144-
145-
/**
146-
* A TypeDeclaration unknown AST node.
147-
*/
148-
class TypeDeclaration extends AstNode instanceof TypeDeclarationImpl { }
149-
150-
/**
151-
* A UnionType unknown AST node.
152-
*/
153-
class UnionType extends AstNode instanceof UnionTypeImpl { }

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

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ private import Idents
77
private import Expr
88
private import Calls
99
private import Misc
10+
private import Types
1011
private import internal.AstNodes
1112
private import internal.TreeSitter
1213
private import internal.Stmts
@@ -52,6 +53,8 @@ class Stmts extends AstNode instanceof StmtsImpl {
5253
* @return A control-flow entry node for this statement
5354
*/
5455
AstNode getAControlFlowEntryNode() { result = CfgImpl::getAControlFlowEntryNode(this) }
56+
57+
Expr getExpr() { result = StmtsImpl.super.getExpr() }
5558
}
5659

5760
/**
@@ -102,45 +105,8 @@ class StmtSequence extends AstNode instanceof StmtSequenceImpl {
102105
* and raises an error if the condition is false. Assert statements are
103106
* useful for validating assumptions and ensuring inputs meet expected criteria.
104107
*/
105-
final class AssertStatementStmt extends Stmts instanceof AssertStatementImpl { }
106-
107-
/**
108-
* A for statement in the AST.
109-
*
110-
* Represents a for loop in Bicep, which iterates over a collection of items.
111-
* For loops are commonly used for creating multiple instances of resources
112-
* or for processing arrays of configuration data.
113-
*/
114-
final class ForStatementStmt extends Stmts instanceof ForStatementImpl { }
115-
116-
/**
117-
* An if statement in the AST.
118-
*
119-
* Represents a conditional statement in Bicep that executes certain code
120-
* only when a specific condition is true. If statements enable conditional
121-
* resource creation or property setting based on input parameters or other factors.
122-
*/
123-
class IfStatement extends Stmts instanceof IfStatementImpl {
124-
/**
125-
* Gets the condition of the if statement.
126-
*
127-
* This is the expression that is evaluated to determine whether
128-
* the body of the if statement should be executed.
129-
*
130-
* @return The condition expression
131-
*/
132-
Expr getCondition() { result = IfStatementImpl.super.getCondition() }
108+
class AssertStatement extends Stmts instanceof AssertStatementImpl { }
133109

134-
/**
135-
* Gets the body of the if statement.
136-
*
137-
* This is the expression or block that will be executed if the
138-
* condition evaluates to true.
139-
*
140-
* @return The body expression
141-
*/
142-
Expr getBody() { result = IfStatementImpl.super.getBody() }
143-
}
144110

145111
/**
146112
* An import statement in the AST.
@@ -149,7 +115,7 @@ class IfStatement extends Stmts instanceof IfStatementImpl {
149115
* namespaces, or resources into the current scope. Import statements enable
150116
* code reuse and modularization of Bicep templates.
151117
*/
152-
final class ImportStatementStmt extends Stmts instanceof ImportStatementImpl { }
118+
class ImportStatement extends Stmts instanceof ImportStatementImpl { }
153119

154120
/**
155121
* An infrastructure node in the AST.
@@ -322,7 +288,7 @@ class Parameters extends Expr instanceof ParametersImpl {
322288
* Import-with statements allow importing external modules with specific settings
323289
* or transformations applied to the imported items.
324290
*/
325-
final class ImportWithStatementStmt extends Stmts instanceof ImportWithStatementImpl { }
291+
class ImportWithStatement extends Stmts instanceof ImportWithStatementImpl { }
326292

327293
/**
328294
* A using statement in the AST.
@@ -331,4 +297,4 @@ final class ImportWithStatementStmt extends Stmts instanceof ImportWithStatement
331297
* an imported module or namespace. Using statements help improve readability
332298
* by allowing shorter references to external resources.
333299
*/
334-
final class UsingStatementStmt extends Stmts instanceof UsingStatementImpl { }
300+
class UsingStatement extends Stmts instanceof UsingStatementImpl { }

0 commit comments

Comments
 (0)