Skip to content

Commit 9a500ee

Browse files
authored
QL: Merge pull request #80 from github/aschackmull/calltarget-astnode
Refactor Call.getTarget to be an AstNode
2 parents 09d727f + dfa6f28 commit 9a500ee

8 files changed

Lines changed: 193 additions & 160 deletions

File tree

ql/src/codeql_ql/ast/Ast.qll

Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ private import codeql_ql.ast.internal.Module
44
private import codeql_ql.ast.internal.Predicate
55
import codeql_ql.ast.internal.Type
66
private import codeql_ql.ast.internal.Variable
7+
private import codeql_ql.ast.internal.Builtins
78

89
bindingset[name]
910
private string directMember(string name) { result = name + "()" }
@@ -31,6 +32,20 @@ class AstNode extends TAstNode {
3132
)
3233
}
3334

35+
predicate hasLocationInfo(
36+
string filepath, int startline, int startcolumn, int endline, int endcolumn
37+
) {
38+
if exists(getLocation())
39+
then getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
40+
else (
41+
filepath = "" and
42+
startline = 0 and
43+
startcolumn = 0 and
44+
endline = 0 and
45+
endcolumn = 0
46+
)
47+
}
48+
3449
/**
3550
* Gets the parent in the AST for this node.
3651
*/
@@ -177,11 +192,62 @@ class Select extends TSelect, AstNode {
177192
override string getAPrimaryQlClass() { result = "Select" }
178193
}
179194

195+
class PredicateOrBuiltin extends TPredOrBuiltin, AstNode {
196+
string getName() { none() }
197+
198+
Type getDeclaringType() { none() }
199+
200+
Type getParameterType(int i) { none() }
201+
202+
Type getReturnType() { none() }
203+
204+
int getArity() { result = count(getParameterType(_)) }
205+
206+
predicate isPrivate() { none() }
207+
}
208+
209+
class BuiltinPredicate extends PredicateOrBuiltin, TBuiltin {
210+
override string toString() { result = getName() }
211+
212+
override string getAPrimaryQlClass() { result = "BuiltinPredicate" }
213+
}
214+
215+
private class BuiltinClassless extends BuiltinPredicate, TBuiltinClassless {
216+
string name;
217+
string ret;
218+
string args;
219+
220+
BuiltinClassless() { this = TBuiltinClassless(ret, name, args) }
221+
222+
override string getName() { result = name }
223+
224+
override PrimitiveType getReturnType() { result.getName() = ret }
225+
226+
override PrimitiveType getParameterType(int i) { result.getName() = getArgType(args, i) }
227+
}
228+
229+
private class BuiltinMember extends BuiltinPredicate, TBuiltinMember {
230+
string name;
231+
string qual;
232+
string ret;
233+
string args;
234+
235+
BuiltinMember() { this = TBuiltinMember(qual, ret, name, args) }
236+
237+
override string getName() { result = name }
238+
239+
override PrimitiveType getReturnType() { result.getName() = ret }
240+
241+
override PrimitiveType getParameterType(int i) { result.getName() = getArgType(args, i) }
242+
243+
override PrimitiveType getDeclaringType() { result.getName() = qual }
244+
}
245+
180246
/**
181247
* A QL predicate.
182248
* Either a classless predicate, a class predicate, or a characteristic predicate.
183249
*/
184-
class Predicate extends TPredicate, AstNode, Declaration {
250+
class Predicate extends TPredicate, AstNode, PredicateOrBuiltin, Declaration {
185251
/**
186252
* Gets the body of the predicate.
187253
*/
@@ -200,7 +266,7 @@ class Predicate extends TPredicate, AstNode, Declaration {
200266
/**
201267
* Gets the number of parameters.
202268
*/
203-
int getArity() {
269+
override int getArity() {
204270
not this.(ClasslessPredicate).getAlias() instanceof PredicateExpr and
205271
result = count(getParameter(_))
206272
or
@@ -209,12 +275,19 @@ class Predicate extends TPredicate, AstNode, Declaration {
209275
)
210276
}
211277

278+
/**
279+
* Holds if this predicate is private.
280+
*/
281+
override predicate isPrivate() { hasAnnotation("private") }
282+
212283
/**
213284
* Gets the return type (if any) of the predicate.
214285
*/
215286
TypeExpr getReturnTypeExpr() { none() }
216287

217-
Type getReturnType() { result = this.getReturnTypeExpr().getResolvedType() }
288+
override Type getReturnType() { result = this.getReturnTypeExpr().getResolvedType() }
289+
290+
override Type getParameterType(int i) { result = this.getParameter(i).getType() }
218291

219292
override AstNode getAChild(string pred) {
220293
result = super.getAChild(pred)
@@ -376,6 +449,8 @@ class ClasslessPredicate extends TClasslessPredicate, Predicate, ModuleDeclarati
376449
or
377450
pred_name = directMember("getReturnTypeExpr") and result = this.getReturnTypeExpr()
378451
}
452+
453+
override predicate isPrivate() { Predicate.super.isPrivate() }
379454
}
380455

381456
/**
@@ -394,11 +469,6 @@ class ClassPredicate extends TClassPredicate, Predicate {
394469

395470
override Class getParent() { result.getAClassPredicate() = this }
396471

397-
/**
398-
* Holds if this predicate is private.
399-
*/
400-
predicate isPrivate() { hasAnnotation("private") }
401-
402472
/**
403473
* Holds if this predicate is annotated as overriding another predicate.
404474
*/
@@ -416,7 +486,7 @@ class ClassPredicate extends TClassPredicate, Predicate {
416486
/**
417487
* Gets the type representing this class.
418488
*/
419-
ClassType getDeclaringType() { result.getDeclaration() = getParent() }
489+
override ClassType getDeclaringType() { result.getDeclaration() = getParent() }
420490

421491
predicate overrides(ClassPredicate other) { predOverrides(this, other) }
422492

@@ -453,7 +523,7 @@ class CharPred extends TCharPred, Predicate {
453523
pred_name = directMember("getBody") and result = this.getBody()
454524
}
455525

456-
ClassType getDeclaringType() { result.getDeclaration() = getParent() }
526+
override ClassType getDeclaringType() { result.getDeclaration() = getParent() }
457527
}
458528

459529
/**
@@ -764,7 +834,7 @@ class NewType extends TNewType, TypeDeclaration, ModuleDeclaration {
764834
* A branch in a `newtype`.
765835
* E.g. `Bar()` or `Baz()` in `newtype Foo = Bar() or Baz()`.
766836
*/
767-
class NewTypeBranch extends TNewTypeBranch, TypeDeclaration {
837+
class NewTypeBranch extends TNewTypeBranch, PredicateOrBuiltin, TypeDeclaration {
768838
Generated::DatatypeBranch branch;
769839

770840
NewTypeBranch() { this = TNewTypeBranch(branch) }
@@ -786,6 +856,16 @@ class NewTypeBranch extends TNewTypeBranch, TypeDeclaration {
786856
/** Gets the body of this branch. */
787857
Formula getBody() { toGenerated(result) = branch.getChild(_).(Generated::Body).getChild() }
788858

859+
override NewTypeBranchType getReturnType() { result.getDeclaration() = this }
860+
861+
override Type getParameterType(int i) { result = this.getField(i).getType() }
862+
863+
override int getArity() { result = count(this.getField(_)) }
864+
865+
override Type getDeclaringType() { none() }
866+
867+
override predicate isPrivate() { this.getNewType().isPrivate() }
868+
789869
override QLDoc getQLDoc() { toGenerated(result) = branch.getChild(_) }
790870

791871
NewType getNewType() { result.getABranch() = this }

ql/src/codeql_ql/ast/internal/AstNodes.qll

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import codeql_ql.ast.Ast as AST
22
import TreeSitter
3+
private import Builtins
34

45
cached
56
newtype TAstNode =
@@ -65,7 +66,11 @@ newtype TAstNode =
6566
TYamlEntry(Generated::YamlEntry ye) or
6667
TYamlKey(Generated::YamlKey yk) or
6768
TYamlListitem(Generated::YamlListitem yli) or
68-
TYamlValue(Generated::YamlValue yv)
69+
TYamlValue(Generated::YamlValue yv) or
70+
TBuiltinClassless(string ret, string name, string args) { isBuiltinClassless(ret, name, args) } or
71+
TBuiltinMember(string qual, string ret, string name, string args) {
72+
isBuiltinMember(qual, ret, name, args)
73+
}
6974

7075
class TFormula =
7176
TDisjunction or TConjunction or TComparisonFormula or TQuantifier or TNegation or TIfFormula or
@@ -194,6 +199,10 @@ Generated::AstNode toGenerated(AST::AstNode n) {
194199

195200
class TPredicate = TCharPred or TClasslessPredicate or TClassPredicate or TDBRelation;
196201

202+
class TPredOrBuiltin = TPredicate or TNewTypeBranch or TBuiltin;
203+
204+
class TBuiltin = TBuiltinClassless or TBuiltinMember;
205+
197206
class TModuleMember = TModuleDeclaration or TImport or TSelect or TQLDoc;
198207

199208
class TDeclaration = TTypeDeclaration or TModuleDeclaration or TPredicate or TVarDecl;

0 commit comments

Comments
 (0)