Skip to content

Commit 3cfe867

Browse files
committed
feat(ast): Update AST layer
1 parent dece8fb commit 3cfe867

File tree

13 files changed

+203
-66
lines changed

13 files changed

+203
-66
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ private import internal.SubscriptExpression
1717
private import internal.TernaryExpression
1818
private import internal.UnaryExpression
1919

20+
private import Resources
21+
2022
/**
2123
* An expression in the AST.
2224
*/
@@ -55,7 +57,9 @@ final class LambdaExpression extends Expr instanceof LambdaExpressionImpl { }
5557
/**
5658
* A MemberExpression expression in the AST.
5759
*/
58-
final class MemberExpression extends Expr instanceof MemberExpressionImpl { }
60+
class MemberExpression extends Expr instanceof MemberExpressionImpl {
61+
Object getObject() { result = super.getObject() }
62+
}
5963

6064
/**
6165
* An alias for MemberExpression.

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ private import AstNodes
22
private import Expr
33
private import internal.Idents
44
private import internal.Identifier
5-
private import internal.Parameter
5+
private import internal.PropertyIdentifier
66

77
/**
88
* A Idents AST node.
@@ -17,3 +17,10 @@ abstract class Idents extends Expr instanceof IdentsImpl {
1717
class Identifier extends Idents instanceof IdentifierImpl {
1818
override string getName() { result = IdentifierImpl.super.getName() }
1919
}
20+
21+
/**
22+
* A PropertyIdentifier unknown AST node.
23+
*/
24+
class PropertyIdentifier extends Idents instanceof PropertyIdentifierImpl {
25+
override string getName() { result = PropertyIdentifierImpl.super.getName() }
26+
}

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

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
*/
44

55
private import AstNodes
6+
private import Expr
67
private import internal.AstNodes
78
private import internal.TreeSitter
89
private import internal.Literals
10+
private import internal.Array
911
private import internal.Null
1012
private import internal.NullableReturnType
13+
private import internal.Number
1114
private import internal.String
1215
private import internal.StringContent
1316

@@ -17,24 +20,53 @@ private import internal.StringContent
1720
final class Literals extends AstNode instanceof LiteralsImpl { }
1821

1922
/**
20-
* A Null literal in the AST.
23+
* A Array unknown AST node.
2124
*/
22-
final class NullLiteral extends Literals instanceof NullImpl {
25+
class Array extends Literals instanceof ArrayImpl {
26+
Expr getElements() {
27+
result = ArrayImpl.super.getElements()
28+
}
29+
30+
Expr getElement(int index) {
31+
result = ArrayImpl.super.getElement(index)
32+
}
2333
}
34+
35+
/**
36+
* A Null literal in the AST.
37+
*/
38+
final class NullLiteral extends Literals instanceof NullImpl { }
39+
2440
/**
2541
* A NullableReturnType literal in the AST.
2642
*/
27-
final class NullableReturnTypeLiteral extends Literals instanceof NullableReturnTypeImpl {
43+
final class NullableReturnTypeLiteral extends Literals instanceof NullableReturnTypeImpl { }
44+
45+
/**
46+
* A Number unknown AST node.
47+
*/
48+
class Number extends Literals instanceof NumberImpl {
49+
int getValue() {
50+
result = NumberImpl.super.getValue().toInt()
51+
}
2852
}
2953

54+
3055
/**
3156
* A String literal in the AST.
3257
*/
33-
final class StringLiteral extends Literals instanceof StringImpl {
34-
string getString() { result = super.getValue() }
58+
class StringLiteral extends Literals instanceof StringImpl {
59+
string getValue() {
60+
exists(StringContentLiteral content |
61+
content = this.getAChild() and
62+
result = content.getValue()
63+
)
64+
}
3565
}
66+
3667
/**
3768
* A StringContent literal in the AST.
3869
*/
39-
final class StringContentLiteral extends Literals instanceof StringContentImpl {
40-
}
70+
class StringContentLiteral extends Literals instanceof StringContentImpl {
71+
string getValue() { result = StringContentImpl.super.getValue() }
72+
}

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

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ private import internal.LoopVariable
1818
private import internal.MetadataDeclaration
1919
private import internal.ModuleDeclaration
2020
private import internal.NegatedType
21-
private import internal.Number
22-
private import internal.Object
2321
private import internal.ObjectProperty
2422
private import internal.OutputDeclaration
2523
private import internal.Parameter
@@ -29,7 +27,6 @@ private import internal.Parameters
2927
private import internal.ParenthesizedType
3028
private import internal.PrimitiveType
3129
private import internal.PropertyIdentifier
32-
private import internal.ResourceDeclaration
3330
private import internal.TargetScopeAssignment
3431
private import internal.TestBlock
3532
private import internal.Type
@@ -44,11 +41,6 @@ private import internal.VariableDeclaration
4441
*/
4542
class Arguments extends AstNode instanceof ArgumentsImpl { }
4643

47-
/**
48-
* A Array unknown AST node.
49-
*/
50-
class Array extends AstNode instanceof ArrayImpl { }
51-
5244
/**
5345
* A ArrayType unknown AST node.
5446
*/
@@ -129,21 +121,6 @@ class ModuleDeclaration extends AstNode instanceof ModuleDeclarationImpl { }
129121
*/
130122
class NegatedType extends AstNode instanceof NegatedTypeImpl { }
131123

132-
/**
133-
* A Number unknown AST node.
134-
*/
135-
class Number extends AstNode instanceof NumberImpl { }
136-
137-
/**
138-
* A Object unknown AST node.
139-
*/
140-
class Object extends AstNode instanceof ObjectImpl { }
141-
142-
/**
143-
* A ObjectProperty unknown AST node.
144-
*/
145-
class ObjectProperty extends AstNode instanceof ObjectPropertyImpl { }
146-
147124
/**
148125
* A OutputDeclaration unknown AST node.
149126
*/
@@ -179,16 +156,6 @@ class ParenthesizedType extends AstNode instanceof ParenthesizedTypeImpl { }
179156
*/
180157
class PrimitiveType extends AstNode instanceof PrimitiveTypeImpl { }
181158

182-
/**
183-
* A PropertyIdentifier unknown AST node.
184-
*/
185-
class PropertyIdentifier extends AstNode instanceof PropertyIdentifierImpl { }
186-
187-
/**
188-
* A ResourceDeclaration unknown AST node.
189-
*/
190-
class ResourceDeclaration extends AstNode instanceof ResourceDeclarationImpl { }
191-
192159
/**
193160
* A TargetScopeAssignment unknown AST node.
194161
*/

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

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
private import AstNodes
2+
private import Expr
3+
private import Idents
4+
private import Literals
5+
6+
private import internal.ResourceDeclaration
7+
private import internal.ObjectProperty
8+
private import internal.Object
9+
10+
/**
11+
* A Object unknown AST node.
12+
*/
13+
class Object extends Expr instanceof ObjectImpl {
14+
ObjectProperty getProperties() { result = ObjectImpl.super.getProperties() }
15+
16+
ObjectProperty getProp(int i) { result = ObjectImpl.super.getProperty(i) }
17+
18+
Expr getProperty(string name) {
19+
exists(ObjectProperty property |
20+
property = this.getProperties() and
21+
property.getName().getName() = name
22+
|
23+
result = property.getValue()
24+
)
25+
}
26+
}
27+
28+
/**
29+
* A ObjectProperty unknown AST node.
30+
*/
31+
class ObjectProperty extends Expr instanceof ObjectPropertyImpl {
32+
Idents getName() { result = ObjectPropertyImpl.super.getName() }
33+
34+
Expr getValue() { result = ObjectPropertyImpl.super.getValue() }
35+
}
36+
37+
/**
38+
* A ResourceDeclaration unknown AST node.
39+
*/
40+
class ResourceDeclaration extends AstNode instanceof ResourceDeclarationImpl {
41+
/**
42+
* The name of the resource instance
43+
*/
44+
Idents getIdentifier() { result = ResourceDeclarationImpl.super.getIdentifier() }
45+
46+
/**
47+
* The name of the resource instance.
48+
*/
49+
Literals getName() { result = ResourceDeclarationImpl.super.getName() }
50+
51+
/**
52+
* The object that represents the resource.
53+
*/
54+
Object getBody() { result = ResourceDeclarationImpl.super.getObject() }
55+
56+
ObjectProperty getProperties() { result = this.getBody().getProperties() }
57+
58+
Expr getProperty(string name) { result = this.getBody().getProperty(name) }
59+
}
60+
61+
// Resource resolveResource(Expr expr) {
62+
// exists(Resource resource |
63+
// // Object having an id property needs to be resolved
64+
// // {resource.id}.id
65+
// exists(MemberExpr memexpr |
66+
// memexpr = expr.(Object).getProperty("id") and
67+
// memexpr.getObject().(Identifier).getName() = resource.getIdentifier().(Identifier).getName()
68+
// |
69+
// result = resource
70+
// )
71+
// or
72+
// exists(Identifier ident |
73+
// ident = expr and
74+
// ident.getName() = resource.getIdentifier().(Identifier).getName()
75+
// |
76+
// result = resource
77+
// )
78+
// )
79+
// }
80+
81+
82+
class Resource extends TResource {
83+
private ResourceDeclaration resource;
84+
85+
Resource() { this = TResourceDeclaration(resource) }
86+
87+
string getResourceType() {
88+
exists(StringLiteral sl | sl = resource.getName() | result = sl.getValue())
89+
}
90+
91+
Expr getProperty(string name) {
92+
result = resource.getProperty(name)
93+
}
94+
95+
string toString() { result = "Resource[" + resource.getIdentifier().getName() + "]" }
96+
97+
string getAPrimaryQlClass() { result = "Resource" }
98+
}
99+
100+
cached
101+
private module Cached {
102+
cached
103+
newtype TResource = TResourceDeclaration(ResourceDeclaration rd)
104+
}
105+
106+
private import Cached

ql/lib/codeql/bicep/ast/internal/Array.qll

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
*
44
* WARNING: this file is generated, do not edit manually
55
*/
6+
67
private import AstNodes
78
private import TreeSitter
89
private import codeql.bicep.ast.AstNodes
10+
private import Expr
911

1012
/**
1113
* A Array AST Node.
@@ -19,6 +21,7 @@ class ArrayImpl extends TArray, AstNode {
1921

2022
override string toString() { result = ast.toString() }
2123

24+
ExprImpl getElements() { toTreeSitter(result) = ast.getChild(_) }
2225

23-
24-
}
26+
ExprImpl getElement(int index) { toTreeSitter(result) = ast.getChild(index) }
27+
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,13 @@ newtype TAstNode =
7575
* A literal value in a Bicep program
7676
*/
7777
class TLiterals =
78-
TInterpolation or TNull or TNullableReturnType or TNullableType or TString or TStringContent;
78+
TArray or TInterpolation or TNull or TNullableReturnType or TNullableType or TNumber or TString or
79+
TStringContent;
7980

8081
/**
8182
* A identifier in a SQL program
8283
*/
83-
class TIdents = TIdentifier;
84+
class TIdents = TIdentifier or TPropertyIdentifier;
8485

8586
/**
8687
* A statement in a Bicep program
@@ -93,10 +94,10 @@ class TStmts =
9394
* A expersion value in a Bicep program
9495
*/
9596
class TExpr =
96-
TLiterals or TConditionalExpr or TStmts or TIdents or TAssignmentExpression or
97-
TBinaryExpression or TCallExpression or TExpression or TLambdaExpression or
98-
TMemberExpression or TParenthesizedExpression or TPrimaryExpression or TResourceExpression or
99-
TSubscriptExpression or TTernaryExpression or TUnaryExpression;
97+
TLiterals or TConditionalExpr or TStmts or TIdents or TObject or TObjectProperty or
98+
TAssignmentExpression or TBinaryExpression or TCallExpression or TExpression or
99+
TLambdaExpression or TMemberExpression or TParenthesizedExpression or TPrimaryExpression or
100+
TResourceExpression or TSubscriptExpression or TTernaryExpression or TUnaryExpression;
100101

101102
/**
102103
* A expersion value in a Bicep program

ql/lib/codeql/bicep/ast/internal/MemberExpression.qll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
*
44
* WARNING: this file is generated, do not edit manually
55
*/
6+
67
private import AstNodes
78
private import TreeSitter
89
private import codeql.bicep.ast.AstNodes
910
private import Expr
10-
11+
private import Object
12+
private import PropertyIdentifier
1113

1214
/**
1315
* A MemberExpression AST Node.
@@ -21,6 +23,7 @@ class MemberExpressionImpl extends TMemberExpression, ExprImpl {
2123

2224
override string toString() { result = ast.toString() }
2325

26+
ObjectImpl getObject() { toTreeSitter(result) = ast.getObject() }
2427

25-
26-
}
28+
PropertyIdentifierImpl getProperty() { toTreeSitter(result) = ast.getProperty() }
29+
}

ql/lib/codeql/bicep/ast/internal/Number.qll

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
*
44
* WARNING: this file is generated, do not edit manually
55
*/
6+
67
private import AstNodes
78
private import TreeSitter
89
private import codeql.bicep.ast.AstNodes
10+
private import Literals
911

1012
/**
1113
* A Number AST Node.
1214
*/
13-
class NumberImpl extends TNumber, AstNode {
15+
class NumberImpl extends TNumber, LiteralsImpl {
1416
private BICEP::Number ast;
1517

1618
override string getAPrimaryQlClass() { result = "Number" }
@@ -19,6 +21,5 @@ class NumberImpl extends TNumber, AstNode {
1921

2022
override string toString() { result = ast.toString() }
2123

22-
23-
24-
}
24+
override string getValue() { result = ast.getValue() }
25+
}

0 commit comments

Comments
 (0)