From a692794178acae0eafd6d4ebeff768d4ac8bc29d Mon Sep 17 00:00:00 2001 From: Erik Krogh Kristensen Date: Wed, 26 May 2021 21:10:37 +0000 Subject: [PATCH] add types --- ql/src/codeql_ql/ast/Ast.qll | 38 ++++++++++++++++++++-- ql/src/codeql_ql/ast/internal/AstNodes.qll | 3 ++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/ql/src/codeql_ql/ast/Ast.qll b/ql/src/codeql_ql/ast/Ast.qll index 9237f9e38f1..e5a60b8b14a 100644 --- a/ql/src/codeql_ql/ast/Ast.qll +++ b/ql/src/codeql_ql/ast/Ast.qll @@ -117,7 +117,7 @@ class VarDecl extends TVarDecl, AstNode { /** * Gets the name for this variable declaration. */ - string getName() { result = var.getChild(_).(Generated::VarName).getChild().getValue() } + string getName() { result = var.getChild(1).(Generated::VarName).getChild().getValue() } override string getAPrimaryQlClass() { result = "VarDecl" } @@ -126,7 +126,41 @@ class VarDecl extends TVarDecl, AstNode { or result.(Class).getAField() = this } - // TODO: Getter for the Type. + + Type getType() { toGenerated(result) = var.getChild(0) } +} + +/** + * A type, such as `DataFlow::Node`. + */ +class Type extends TType, AstNode { + Generated::TypeExpr type; + + Type() { this = TType(type) } + + override string getAPrimaryQlClass() { result = "Type" } + + /** + * Gets the class name for the type. + * E.g. `Node` in `DataFlow::Node`. + * Also gets the name for primitive types such as `string` or `int`. + */ + string getClassName() { + result = type.getName().getValue() + or + result = type.getChild().(Generated::PrimitiveType).getValue() + } + + /** + * Holds if this type is a primitive such as `string` or `int`. + */ + predicate isPrimitive() { type.getChild() instanceof Generated::PrimitiveType } + + /** + * Gets the module name of the type, if it exists. + * E.g. `DataFlow` in `DataFlow::Node`. + */ + string getModuleName() { result = type.getChild().(Generated::ModuleExpr).getName().getValue() } } /** diff --git a/ql/src/codeql_ql/ast/internal/AstNodes.qll b/ql/src/codeql_ql/ast/internal/AstNodes.qll index 66edaea97a8..e482d673655 100644 --- a/ql/src/codeql_ql/ast/internal/AstNodes.qll +++ b/ql/src/codeql_ql/ast/internal/AstNodes.qll @@ -15,6 +15,7 @@ newtype TAstNode = TNewType(Generated::Datatype dt) or TNewTypeBranch(Generated::DatatypeBranch branch) or TImport(Generated::ImportDirective imp) or + TType(Generated::TypeExpr type) or TDisjunction(Generated::Disjunction disj) or TConjunction(Generated::Conjunction conj) or TComparisonFormula(Generated::CompTerm comp) or @@ -67,6 +68,8 @@ Generated::AstNode toGenerated(AST::AstNode n) { n = TNewTypeBranch(result) or n = TImport(result) + or + n = TType(result) } class TPredicate = TCharPred or TClasslessPredicate or TClassPredicate;