Skip to content

Commit 6652021

Browse files
committed
PS: Add user-facing AST classes.
1 parent d79eb01 commit 6652021

File tree

93 files changed

+3518
-85
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+3518
-85
lines changed

powershell/ql/lib/powershell.qll

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1 @@
1-
import semmle.code.powershell.File
2-
import semmle.code.powershell.Location
3-
import semmle.code.powershell.SourceLocation
4-
import semmle.code.powershell.Ast
5-
import semmle.code.powershell.Statement
6-
import semmle.code.powershell.Expression
7-
import semmle.code.powershell.CommandBase
8-
import semmle.code.powershell.AttributeBase
9-
import semmle.code.powershell.PipelineBase
10-
import semmle.code.powershell.PipelineChain
11-
import semmle.code.powershell.BaseConstantExpression
12-
import semmle.code.powershell.ConstantExpression
13-
import semmle.code.powershell.MemberExpressionBase
14-
import semmle.code.powershell.Attribute
15-
import semmle.code.powershell.NamedAttributeArgument
16-
import semmle.code.powershell.TypeConstraint
17-
import semmle.code.powershell.VariableExpression
18-
import semmle.code.powershell.ModuleSpecification
19-
import semmle.code.powershell.ParamBlock
20-
import semmle.code.powershell.NamedBlock
21-
import semmle.code.powershell.ScriptBlock
22-
import semmle.code.powershell.StringLiteral
23-
import semmle.code.powershell.AssignmentStatement
24-
import semmle.code.powershell.BinaryExpression
25-
import semmle.code.powershell.UnaryExpression
26-
import semmle.code.powershell.ScriptBlockExpr
27-
import semmle.code.powershell.TernaryExpression
28-
import semmle.code.powershell.UsingExpression
29-
import semmle.code.powershell.TrapStatement
30-
import semmle.code.powershell.StatementBlock
31-
import semmle.code.powershell.ArrayExpression
32-
import semmle.code.powershell.ArrayLiteral
33-
import semmle.code.powershell.CommandElement
34-
import semmle.code.powershell.Redirection
35-
import semmle.code.powershell.FileRedirection
36-
import semmle.code.powershell.MergingRedirection
37-
import semmle.code.powershell.LoopStmt
38-
import semmle.code.powershell.DoWhileStmt
39-
import semmle.code.powershell.DoUntilStmt
40-
import semmle.code.powershell.WhileStmt
41-
import semmle.code.powershell.ForStmt
42-
import semmle.code.powershell.ForEachStmt
43-
import semmle.code.powershell.GotoStmt
44-
import semmle.code.powershell.ContinueStmt
45-
import semmle.code.powershell.BreakStmt
46-
import semmle.code.powershell.ReturnStmt
47-
import semmle.code.powershell.UsingStmt
48-
import semmle.code.powershell.ThrowStmt
49-
import semmle.code.powershell.ErrorStmt
50-
import semmle.code.powershell.Type
51-
import semmle.code.powershell.Member
52-
import semmle.code.powershell.PropertyMember
53-
import semmle.code.powershell.Function
54-
import semmle.code.powershell.TryStmt
55-
import semmle.code.powershell.IfStmt
56-
import semmle.code.powershell.SwitchStmt
57-
import semmle.code.powershell.ExitStmt
58-
import semmle.code.powershell.LabeledStmt
59-
import semmle.code.powershell.DynamicStmt
60-
import semmle.code.powershell.DataStmt
61-
import semmle.code.powershell.Configuration
62-
import semmle.code.powershell.CatchClause
63-
import semmle.code.powershell.Command
64-
import semmle.code.powershell.CommandExpression
65-
import semmle.code.powershell.CommandParameter
66-
import semmle.code.powershell.ExpandableStringExpression
67-
import semmle.code.powershell.TypeExpression
68-
import semmle.code.powershell.ParenExpression
69-
import semmle.code.powershell.Chainable
70-
import semmle.code.powershell.Pipeline
71-
import semmle.code.powershell.StringConstantExpression
72-
import semmle.code.powershell.MemberExpr
73-
import semmle.code.powershell.InvokeMemberExpression
74-
import semmle.code.powershell.Call
75-
import semmle.code.powershell.ObjectCreation
76-
import semmle.code.powershell.SubExpression
77-
import semmle.code.powershell.ErrorExpr
78-
import semmle.code.powershell.ConvertExpr
79-
import semmle.code.powershell.IndexExpr
80-
import semmle.code.powershell.HashTable
81-
import semmle.code.powershell.SplitExpr
82-
import semmle.code.powershell.CommentEntity
83-
import semmle.code.powershell.Variable
84-
import semmle.code.powershell.internal.Internal::Public
85-
import semmle.code.powershell.ModuleManifest
1+
import semmle.code.powershell.ast.Ast
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import internal.Internal::Public
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
private import AstImport
2+
3+
class ArrayExpr extends Expr, TArrayExpr {
4+
StmtBlock getStmtBlock() {
5+
exists(Raw::Ast r | r = getRawAst(this) |
6+
synthChild(r, arrayExprStmtBlock(), result)
7+
or
8+
not synthChild(r, arrayExprStmtBlock(), result) and
9+
result = getResultAst(r.(Raw::ArrayExpr).getStmtBlock())
10+
)
11+
}
12+
13+
override string toString() { result = "@(...)" }
14+
15+
final override Ast getChild(ChildIndex i) {
16+
result = super.getChild(i)
17+
or
18+
i = arrayExprStmtBlock() and result = this.getStmtBlock()
19+
}
20+
21+
/**
22+
* Gets the i'th element of this `ArrayExpr`, if this can be determined statically.
23+
*
24+
* See `getStmtBlock` when the array elements are not known statically.
25+
*/
26+
Expr getExpr(int i) {
27+
result =
28+
unique( | | this.getStmtBlock().getAStmt()).(ExprStmt).getExpr().(ArrayLiteral).getExpr(i)
29+
}
30+
31+
/**
32+
* Gets an element of this `ArrayExpr`, if this can be determined statically.
33+
*
34+
* See `getStmtBlock` when the array elements are not known statically.
35+
*/
36+
Expr getAnExpr() { result = this.getExpr(_) }
37+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
private import AstImport
2+
3+
class ArrayLiteral extends Expr, TArrayLiteral {
4+
Expr getExpr(int index) {
5+
exists(ChildIndex i, Raw::Ast r | i = arrayLiteralExpr(index) and r = getRawAst(this) |
6+
synthChild(r, i, result)
7+
or
8+
not synthChild(r, i, _) and
9+
result = getResultAst(r.(Raw::ArrayLiteral).getElement(index))
10+
)
11+
}
12+
13+
Expr getAnExpr() { result = this.getExpr(_) }
14+
15+
override string toString() { result = "...,..." }
16+
17+
final override Ast getChild(ChildIndex i) {
18+
result = super.getChild(i)
19+
or
20+
exists(int index |
21+
i = arrayLiteralExpr(index) and
22+
result = this.getExpr(index)
23+
)
24+
}
25+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
private import AstImport
2+
3+
class AssignStmt extends Stmt, TAssignStmt {
4+
Expr getRightHandSide() {
5+
exists(Raw::Ast r | r = getRawAst(this) |
6+
synthChild(r, assignStmtRightHandSide(), result)
7+
or
8+
not synthChild(r, assignStmtRightHandSide(), _) and
9+
result = getResultAst(r.(Raw::AssignStmt).getRightHandSide())
10+
)
11+
}
12+
13+
Expr getLeftHandSide() {
14+
exists(Raw::Ast r | r = getRawAst(this) |
15+
synthChild(r, assignStmtLeftHandSide(), result)
16+
or
17+
not synthChild(r, assignStmtLeftHandSide(), _) and
18+
result = getResultAst(r.(Raw::AssignStmt).getLeftHandSide())
19+
)
20+
}
21+
22+
override Ast getChild(ChildIndex i) {
23+
result = super.getChild(i)
24+
or
25+
i = assignStmtLeftHandSide() and
26+
result = this.getLeftHandSide()
27+
or
28+
i = assignStmtRightHandSide() and
29+
result = this.getRightHandSide()
30+
}
31+
32+
override string toString() { result = "...=..." }
33+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
private import AstImport
2+
3+
class Ast extends TAst {
4+
string toString() { none() }
5+
6+
final Ast getParent() { result.getChild(_) = this }
7+
8+
Location getLocation() {
9+
result = getRawAst(this).getLocation()
10+
or
11+
result = any(Synthesis s).getLocation(this)
12+
}
13+
14+
Ast getChild(ChildIndex i) {
15+
exists(Raw::Ast r | r = getRawAst(this) |
16+
synthChild(r, i, result)
17+
or
18+
exists(string name |
19+
i = RealVar(name) and
20+
result = TVariableReal(r, name, _)
21+
)
22+
)
23+
}
24+
25+
final Ast getAChild() { result = this.getChild(_) }
26+
27+
Scope getEnclosingScope() { result = scopeOf(this) } // TODO: Scope of synth?
28+
29+
Function getEnclosingFunction() {
30+
exists(Scope scope | scope = scopeOf(this) |
31+
result.getBody() = scope
32+
or
33+
not scope instanceof ScriptBlock and
34+
result = scope.getEnclosingFunction()
35+
)
36+
}
37+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import TAst
2+
import Raw.Raw as Raw
3+
import Internal::Private
4+
import Internal::Public
5+
import Synthesis
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
private import AstImport
2+
3+
class Attribute extends AttributeBase, TAttribute {
4+
string getName() { result = getRawAst(this).(Raw::Attribute).getName() }
5+
6+
NamedAttributeArgument getNamedArgument(int i) {
7+
exists(ChildIndex index, Raw::Ast r | index = attributeNamedArg(i) and r = getRawAst(this) |
8+
synthChild(r, attributeNamedArg(i), result)
9+
or
10+
not synthChild(r, index, _) and
11+
result = getResultAst(r.(Raw::Attribute).getNamedArgument(i))
12+
)
13+
}
14+
15+
NamedAttributeArgument getANamedArgument() { result = this.getNamedArgument(_) }
16+
17+
int getNumberOfArguments() { result = count(this.getAPositionalArgument()) }
18+
19+
Expr getPositionalArgument(int i) {
20+
exists(ChildIndex index, Raw::Ast r | index = attributePosArg(i) and r = getRawAst(this) |
21+
synthChild(r, index, result)
22+
or
23+
not synthChild(r, index, _) and
24+
result = getResultAst(r.(Raw::Attribute).getPositionalArgument(i))
25+
)
26+
}
27+
28+
Expr getAPositionalArgument() { result = this.getPositionalArgument(_) }
29+
30+
int getNumberOfPositionalArguments() { result = count(this.getAPositionalArgument()) }
31+
32+
private string toStringSpecific() {
33+
not exists(this.getAPositionalArgument()) and
34+
result = unique( | | this.getANamedArgument()).getName()
35+
or
36+
not exists(this.getANamedArgument()) and
37+
result = unique( | | this.getANamedArgument()).getName()
38+
}
39+
40+
override string toString() {
41+
result = this.toStringSpecific()
42+
or
43+
not exists(this.toStringSpecific()) and
44+
result = this.getName()
45+
}
46+
47+
final override Ast getChild(ChildIndex i) {
48+
result = super.getChild(i)
49+
or
50+
exists(int index |
51+
i = attributeNamedArg(index) and
52+
result = this.getNamedArgument(index)
53+
or
54+
i = attributePosArg(index) and
55+
result = this.getPositionalArgument(index)
56+
)
57+
}
58+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
private import AstImport
2+
3+
class AttributeBase extends Ast, TAttributeBase { }
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
private import AstImport
2+
3+
class AttributedExpr extends AttributedExprBase, TAttributedExpr {
4+
final override string toString() { result = "[...]" + this.getExpr().toString() }
5+
6+
final override Expr getExpr() {
7+
exists(Raw::Ast r | r = getRawAst(this) |
8+
synthChild(r, attributedExprExpr(), result)
9+
or
10+
not synthChild(r, attributedExprExpr(), _) and
11+
result = getResultAst(r.(Raw::AttributedExpr).getExpr())
12+
)
13+
}
14+
15+
final override Attribute getAttribute() {
16+
exists(Raw::Ast r | r = getRawAst(this) |
17+
synthChild(r, attributedExprAttr(), result)
18+
or
19+
not synthChild(r, attributedExprAttr(), _) and
20+
result = getResultAst(r.(Raw::AttributedExpr).getAttribute())
21+
)
22+
}
23+
24+
override Ast getChild(ChildIndex i) {
25+
result = super.getChild(i)
26+
or
27+
i = attributedExprExpr() and result = this.getExpr()
28+
or
29+
i = attributedExprAttr() and
30+
result = this.getAttribute()
31+
}
32+
}

0 commit comments

Comments
 (0)