Skip to content

Commit 1b19a3e

Browse files
committed
QL: introduce FieldDecl in the ast
1 parent f54f70d commit 1b19a3e

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

ql/src/codeql_ql/ast/Ast.qll

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,10 @@ class VarDecl extends TVarDecl, VarDef, Declaration {
563563
)
564564
}
565565

566-
/** If this is a field, returns the class type that declares it. */
567-
ClassType getDeclaringType() { result.getDeclaration().getAField() = this }
566+
/** If this is declared in a field, returns the class type that declares it. */
567+
ClassType getDeclaringType() {
568+
exists(FieldDecl f | f.getVarDecl() = this and result = f.getParent().(Class).getType())
569+
}
568570

569571
/**
570572
* Holds if this is a class field that overrides the field `other`.
@@ -580,6 +582,32 @@ class VarDecl extends TVarDecl, VarDef, Declaration {
580582
override string toString() { result = this.getName() }
581583
}
582584

585+
/**
586+
* A field declaration;
587+
*/
588+
class FieldDecl extends TFieldDecl, AstNode {
589+
QL::Field f;
590+
591+
FieldDecl() { this = TFieldDecl(f) }
592+
593+
VarDecl getVarDecl() { toQL(result) = f.getChild() }
594+
595+
override AstNode getAChild(string pred) {
596+
result = super.getAChild(pred)
597+
or
598+
pred = directMember("getVarDecl") and result = this.getVarDecl()
599+
}
600+
601+
override string getAPrimaryQlClass() { result = "FieldDecl" }
602+
603+
/** Holds if this field is annotated as overriding another field. */
604+
predicate isOverride() { this.hasAnnotation("override") }
605+
606+
string getName() { result = getVarDecl().getName() }
607+
608+
override QLDoc getQLDoc() { result = any(Class c).getQLDocFor(this) }
609+
}
610+
583611
/**
584612
* A type reference, such as `DataFlow::Node`.
585613
*/
@@ -716,10 +744,7 @@ class Class extends TClass, TypeDeclaration, ModuleDeclaration {
716744
*/
717745
CharPred getCharPred() { toQL(result) = cls.getChild(_).(QL::ClassMember).getChild(_) }
718746

719-
AstNode getMember(int i) {
720-
toQL(result) = cls.getChild(i).(QL::ClassMember).getChild(_) or
721-
toQL(result) = cls.getChild(i).(QL::ClassMember).getChild(_).(QL::Field).getChild()
722-
}
747+
AstNode getMember(int i) { toQL(result) = cls.getChild(i).(QL::ClassMember).getChild(_) }
723748

724749
QLDoc getQLDocFor(AstNode m) {
725750
exists(int i | result = this.getMember(i) and m = this.getMember(i + 1))
@@ -743,9 +768,7 @@ class Class extends TClass, TypeDeclaration, ModuleDeclaration {
743768
/**
744769
* Gets a field in this class.
745770
*/
746-
VarDecl getAField() {
747-
toQL(result) = cls.getChild(_).(QL::ClassMember).getChild(_).(QL::Field).getChild()
748-
}
771+
FieldDecl getAField() { result = getMember(_) }
749772

750773
/**
751774
* Gets a super-type referenced in the `extends` part of the class declaration.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ newtype TAstNode =
88
TQLDoc(QL::Qldoc qldoc) or
99
TClasslessPredicate(QL::ClasslessPredicate pred) or
1010
TVarDecl(QL::VarDecl decl) or
11+
TFieldDecl(QL::Field field) or
1112
TClass(QL::Dataclass dc) or
1213
TCharPred(QL::Charpred pred) or
1314
TClassPredicate(QL::MemberPredicate pred) or
@@ -150,6 +151,8 @@ QL::AstNode toQL(AST::AstNode n) {
150151
or
151152
n = TVarDecl(result)
152153
or
154+
n = TFieldDecl(result)
155+
or
153156
n = TClass(result)
154157
or
155158
n = TCharPred(result)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ predicate predOverrides(ClassPredicate sub, ClassPredicate sup) {
134134
}
135135

136136
private VarDecl declaredField(ClassType ty, string name) {
137-
result = ty.getDeclaration().getAField() and
137+
result = ty.getDeclaration().getAField().getVarDecl() and
138138
result.getName() = name
139139
}
140140

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class VariableScope extends TScope, AstNode {
3737
predicate containsField(VarDef decl, string name) {
3838
name = decl.getName() and
3939
(
40-
decl = this.(Class).getAField()
40+
decl = this.(Class).getAField().getVarDecl()
4141
or
4242
this.getOuterScope().containsField(decl, name) and
4343
not exists(this.getADefinition(name))

0 commit comments

Comments
 (0)