Skip to content

Commit 7955a8b

Browse files
committed
Refactor
1 parent 486fc45 commit 7955a8b

4 files changed

Lines changed: 58 additions & 106 deletions

File tree

ql/src/codeql_ql/ast/Ast.qll

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ class AstNode extends TAstNode {
3131
)
3232
}
3333

34+
predicate hasLocationInfo(
35+
string filepath, int startline, int startcolumn, int endline, int endcolumn
36+
) {
37+
if exists(getLocation())
38+
then getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
39+
else (
40+
filepath = "" and
41+
startline = 0 and
42+
startcolumn = 0 and
43+
endline = 0 and
44+
endcolumn = 0
45+
)
46+
}
47+
3448
/**
3549
* Gets the parent in the AST for this node.
3650
*/
@@ -181,7 +195,7 @@ class Select extends TSelect, AstNode {
181195
* A QL predicate.
182196
* Either a classless predicate, a class predicate, or a characteristic predicate.
183197
*/
184-
class Predicate extends TPredicate, AstNode, Declaration {
198+
class Predicate extends TPredicate, AstNode, PredicateOrBuiltin, Declaration {
185199
/**
186200
* Gets the body of the predicate.
187201
*/
@@ -200,7 +214,7 @@ class Predicate extends TPredicate, AstNode, Declaration {
200214
/**
201215
* Gets the number of parameters.
202216
*/
203-
int getArity() {
217+
override int getArity() {
204218
not this.(ClasslessPredicate).getAlias() instanceof PredicateExpr and
205219
result = count(getParameter(_))
206220
or
@@ -209,12 +223,19 @@ class Predicate extends TPredicate, AstNode, Declaration {
209223
)
210224
}
211225

226+
/**
227+
* Holds if this predicate is private.
228+
*/
229+
override predicate isPrivate() { hasAnnotation("private") }
230+
212231
/**
213232
* Gets the return type (if any) of the predicate.
214233
*/
215234
TypeExpr getReturnTypeExpr() { none() }
216235

217-
Type getReturnType() { result = this.getReturnTypeExpr().getResolvedType() }
236+
override Type getReturnType() { result = this.getReturnTypeExpr().getResolvedType() }
237+
238+
override Type getParameterType(int i) { result = this.getParameter(i).getType() }
218239

219240
override AstNode getAChild(string pred) {
220241
result = super.getAChild(pred)
@@ -376,6 +397,8 @@ class ClasslessPredicate extends TClasslessPredicate, Predicate, ModuleDeclarati
376397
or
377398
pred_name = directMember("getReturnTypeExpr") and result = this.getReturnTypeExpr()
378399
}
400+
401+
override predicate isPrivate() { Predicate.super.isPrivate() }
379402
}
380403

381404
/**
@@ -394,11 +417,6 @@ class ClassPredicate extends TClassPredicate, Predicate {
394417

395418
override Class getParent() { result.getAClassPredicate() = this }
396419

397-
/**
398-
* Holds if this predicate is private.
399-
*/
400-
predicate isPrivate() { hasAnnotation("private") }
401-
402420
/**
403421
* Holds if this predicate is annotated as overriding another predicate.
404422
*/
@@ -416,7 +434,7 @@ class ClassPredicate extends TClassPredicate, Predicate {
416434
/**
417435
* Gets the type representing this class.
418436
*/
419-
ClassType getDeclaringType() { result.getDeclaration() = getParent() }
437+
override ClassType getDeclaringType() { result.getDeclaration() = getParent() }
420438

421439
predicate overrides(ClassPredicate other) { predOverrides(this, other) }
422440

@@ -453,7 +471,7 @@ class CharPred extends TCharPred, Predicate {
453471
pred_name = directMember("getBody") and result = this.getBody()
454472
}
455473

456-
ClassType getDeclaringType() { result.getDeclaration() = getParent() }
474+
override ClassType getDeclaringType() { result.getDeclaration() = getParent() }
457475
}
458476

459477
/**
@@ -764,7 +782,7 @@ class NewType extends TNewType, TypeDeclaration, ModuleDeclaration {
764782
* A branch in a `newtype`.
765783
* E.g. `Bar()` or `Baz()` in `newtype Foo = Bar() or Baz()`.
766784
*/
767-
class NewTypeBranch extends TNewTypeBranch, TypeDeclaration {
785+
class NewTypeBranch extends TNewTypeBranch, PredicateOrBuiltin, TypeDeclaration {
768786
Generated::DatatypeBranch branch;
769787

770788
NewTypeBranch() { this = TNewTypeBranch(branch) }
@@ -786,6 +804,16 @@ class NewTypeBranch extends TNewTypeBranch, TypeDeclaration {
786804
/** Gets the body of this branch. */
787805
Formula getBody() { toGenerated(result) = branch.getChild(_).(Generated::Body).getChild() }
788806

807+
override NewTypeBranchType getReturnType() { result.getDeclaration() = this }
808+
809+
override Type getParameterType(int i) { result = this.getField(i).getType() }
810+
811+
override int getArity() { result = count(this.getField(_)) }
812+
813+
override Type getDeclaringType() { none() }
814+
815+
predicate isPrivate() { this.getNewType().isPrivate() }
816+
789817
override QLDoc getQLDoc() { toGenerated(result) = branch.getChild(_) }
790818

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

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

Lines changed: 8 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,8 @@ 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 TBuiltinClassless or TBuiltinMember;
203+
197204
class TModuleMember = TModuleDeclaration or TImport or TSelect or TQLDoc;
198205

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

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

Lines changed: 10 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import ql
22
private import Builtins
33
private import codeql_ql.ast.internal.Module
4-
private import codeql_ql.ast.internal.AstNodes as AstNodes
4+
private import codeql_ql.ast.internal.AstNodes
55

6-
private class TClasslessPredicateOrNewTypeBranch =
7-
AstNodes::TClasslessPredicate or AstNodes::TNewTypeBranch;
6+
private class TClasslessPredicateOrNewTypeBranch = TClasslessPredicate or TNewTypeBranch;
87

9-
string getPredicateName(TClasslessPredicateOrNewTypeBranch p) {
8+
private string getPredicateName(TClasslessPredicateOrNewTypeBranch p) {
109
result = p.(ClasslessPredicate).getName() or
1110
result = p.(NewTypeBranch).getName()
1211
}
@@ -69,8 +68,7 @@ private module Cached {
6968
m = pc.getQualifier().getResolvedModule() and
7069
public = true
7170
|
72-
definesPredicate(m, pc.getPredicateName(), pc.getNumberOfArguments(), p.getDeclaration(),
73-
public)
71+
definesPredicate(m, pc.getPredicateName(), pc.getNumberOfArguments(), p, public)
7472
)
7573
}
7674

@@ -120,49 +118,13 @@ private module Cached {
120118
not resolvePredicateCall(c, _) and
121119
resolveDBRelation(c, p)
122120
}
123-
124-
cached
125-
module NewTypeDef {
126-
cached
127-
newtype TPredOrBuiltin =
128-
TPred(Predicate p) or
129-
TNewTypeBranch(NewTypeBranch b) or
130-
TBuiltinClassless(string ret, string name, string args) {
131-
isBuiltinClassless(ret, name, args)
132-
} or
133-
TBuiltinMember(string qual, string ret, string name, string args) {
134-
isBuiltinMember(qual, ret, name, args)
135-
}
136-
}
137121
}
138122

139123
import Cached
140-
private import NewTypeDef
141124

142-
class PredicateOrBuiltin extends TPredOrBuiltin {
125+
class PredicateOrBuiltin extends TPredOrBuiltin, AstNode {
143126
string getName() { none() }
144127

145-
string toString() { result = getName() }
146-
147-
predicate hasLocationInfo(
148-
string filepath, int startline, int startcolumn, int endline, int endcolumn
149-
) {
150-
if exists(getDeclaration())
151-
then
152-
getDeclaration()
153-
.getLocation()
154-
.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
155-
else (
156-
filepath = "" and
157-
startline = 0 and
158-
startcolumn = 0 and
159-
endline = 0 and
160-
endcolumn = 0
161-
)
162-
}
163-
164-
AstNode getDeclaration() { none() }
165-
166128
Type getDeclaringType() { none() }
167129

168130
Type getParameterType(int i) { none() }
@@ -174,58 +136,14 @@ class PredicateOrBuiltin extends TPredOrBuiltin {
174136
predicate isPrivate() { none() }
175137
}
176138

177-
private class DefinedPredicate extends PredicateOrBuiltin, TPred {
178-
Predicate decl;
179-
180-
DefinedPredicate() { this = TPred(decl) }
181-
182-
override Predicate getDeclaration() { result = decl }
183-
184-
override string getName() { result = decl.getName() }
185-
186-
override Type getReturnType() { result = decl.getReturnType() }
187-
188-
override Type getParameterType(int i) { result = decl.getParameter(i).getType() }
189-
190-
// Can be removed when all types can be resolved
191-
override int getArity() { result = decl.getArity() }
192-
193-
override Type getDeclaringType() {
194-
result = decl.(ClassPredicate).getDeclaringType()
195-
or
196-
result = decl.(CharPred).getDeclaringType()
197-
}
198-
199-
override predicate isPrivate() {
200-
decl.(ClassPredicate).isPrivate() or decl.(ClassPredicate).isPrivate()
201-
}
202-
}
203-
204-
private class DefinedNewTypeBranch extends PredicateOrBuiltin, TNewTypeBranch {
205-
NewTypeBranch b;
206-
207-
DefinedNewTypeBranch() { this = TNewTypeBranch(b) }
208-
209-
override NewTypeBranch getDeclaration() { result = b }
210-
211-
override string getName() { result = b.getName() }
212-
213-
override NewTypeBranchType getReturnType() { result.getDeclaration() = b }
214-
215-
override Type getParameterType(int i) { result = b.getField(i).getType() }
216-
217-
// Can be removed when all types can be resolved
218-
override int getArity() { result = count(b.getField(_)) }
139+
private class TBuiltin = TBuiltinClassless or TBuiltinMember;
219140

220-
override Type getDeclaringType() { none() }
141+
class BuiltinPredicate extends PredicateOrBuiltin, TBuiltin {
142+
override string toString() { result = getName() }
221143

222-
override predicate isPrivate() { b.getNewType().isPrivate() }
144+
override string getAPrimaryQlClass() { result = "BuiltinPredicate" }
223145
}
224146

225-
private class TBuiltin = TBuiltinClassless or TBuiltinMember;
226-
227-
class BuiltinPredicate extends PredicateOrBuiltin, TBuiltin { }
228-
229147
private class BuiltinClassless extends BuiltinPredicate, TBuiltinClassless {
230148
string name;
231149
string ret;
@@ -287,7 +205,7 @@ module PredConsistency {
287205
strictcount(PredicateOrBuiltin p0 |
288206
resolveCall(call, p0) and
289207
// aliases are expected to resolve to multiple.
290-
not exists(p0.getDeclaration().(ClasslessPredicate).getAlias())
208+
not exists(p0.(ClasslessPredicate).getAlias())
291209
) and
292210
c > 1 and
293211
resolveCall(call, p)

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ private PredicateOrBuiltin inherClassPredCandidate(Type ty, string name, int ari
127127
}
128128

129129
predicate predOverrides(ClassPredicate sub, ClassPredicate sup) {
130-
sup =
131-
inherClassPredCandidate(sub.getDeclaringType(), sub.getName(), sub.getArity()).getDeclaration()
130+
sup = inherClassPredCandidate(sub.getDeclaringType(), sub.getName(), sub.getArity())
132131
}
133132

134133
private VarDecl declaredField(ClassType ty, string name) {

0 commit comments

Comments
 (0)