@@ -4,6 +4,7 @@ private import codeql_ql.ast.internal.Module
44private import codeql_ql.ast.internal.Predicate
55import codeql_ql.ast.internal.Type
66private import codeql_ql.ast.internal.Variable
7+ private import codeql_ql.ast.internal.Builtins
78
89bindingset [ name]
910private 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 }
0 commit comments