CodeQL documentation

Abstract syntax tree classes for working with Go programs

CodeQL has a large selection of classes for representing the abstract syntax tree of Go programs.

The abstract syntax tree (AST) represents the syntactic structure of a program. Nodes on the AST represent elements such as statements and expressions.

Statement classes

This table lists all subclasses of Stmt.

Statement syntax CodeQL class Superclasses Remarks
; EmptyStmt    
Expr ExprStmt    
{ Stmt ... } BlockStmt    
if Expr BlockStmt IfStmt    
if Expr BlockStmt else Stmt
if Stmt; Expr BlockStmt
for Expr BlockStmt ForStmt LoopStmt  
for Stmt; Expr; Stmt BlockStmt
for Expr ... = range Expr BlockStmt RangeStmt LoopStmt  
switch Expr { CaseClause ... } ExpressionSwitchStmt SwitchStmt  
switch Stmt; Expr { CaseClause ... }
switch Expr.(type) { CaseClause ... } TypeSwitchStmt SwitchStmt  
switch SimpleAssignStmt.(type) { CaseClause ... }
switch Stmt; Expr.(type) { CaseClause ... }
select { CommClause ... } SelectStmt    
return ReturnStmt    
break BreakStmt BranchStmt  
continue ContinueStmt BranchStmt  
goto LabelName GotoStmt BranchStmt  
fallthrough FallthroughStmt BranchStmt can only occur as final non-empty child of a CaseClause in an ExpressionSwitchStmt
LabelName: Stmt LabeledStmt    
var VariableName TypeName DeclStmt    
const VariableName = Expr
type TypeName TypeExpr
type TypeName = TypeExpr
Expr ... = Expr ... AssignStmt SimpleAssignStmt, Assignment  
VariableName ... := Expr ... DefineStmt SimpleAssignStmt, Assignment  
Expr += Expr AddAssignStmt CompoundAssignStmt, Assignment  
Expr -= Expr SubAssignStmt CompoundAssignStmt, Assignment  
Expr *= Expr MulAssignStmt CompoundAssignStmt, Assignment  
Expr /= Expr QuoAssignStmt CompoundAssignStmt, Assignment  
Expr %= Expr RemAssignStmt CompoundAssignStmt, Assignment  
Expr *= Expr MulAssignStmt CompoundAssignStmt, Assignment  
Expr &= Expr AndAssignStmt CompoundAssignStmt, Assignment  
Expr |= Expr OrAssignStmt CompoundAssignStmt, Assignment  
Expr ^= Expr XorAssignStmt CompoundAssignStmt, Assignment  
Expr <<= Expr ShlAssignStmt CompoundAssignStmt, Assignment  
Expr >>= Expr ShrAssignStmt CompoundAssignStmt, Assignment  
Expr &^= Expr AndNotAssignStmt CompoundAssignStmt, Assignment  
Expr ++ IncStmt IncDecStmt  
Expr -- DecStmt IncDecStmt  
go CallExpr GoStmt    
defer CallExpr DeferStmt    
Expr <- Expr SendStmt    
case Expr ...: Stmt ... CaseClause   can only occur as child of a SwitchStmt
case TypeExpr ...: Stmt ...
default: Stmt ...
case SendStmt: Stmt ... CommClause   can only occur as child of a SelectStmt
case RecvStmt: Stmt ...
default: Stmt ...
Expr ... = RecvExpr RecvStmt   can only occur as child of a CommClause
VariableName ... := RecvExpr
(anything unparseable) BadStmt    

Expression classes

There are many expression classes, so we present them by category. All classes in this section are subclasses of Expr.


Expression syntax example CodeQL class Superclass
23 IntLit BasicLit
4.2 FloatLit BasicLit
4.2 + 2.7i ImagLit BasicLit
'a' CharLit BasicLit
"Hello" StringLit BasicLit
func(x, y int) int { return x + y } FuncLit FuncDef
map[string]int{"A": 1, "B": 2} MapLit CompositeLit
Point3D{0.5, -0.5, 0.5} StructLit CompositeLit

Unary expressions

All classes in this subsection are subclasses of UnaryExpr.

Expression syntax CodeQL class Superclasses
+Expr PlusExpr ArithmeticUnaryExpr
-Expr MinusExpr ArithmeticUnaryExpr
!Expr NotExpr LogicalUnaryExpr
^Expr ComplementExpr BitwiseUnaryExpr
&Expr AddressExpr  
<-Expr RecvExpr  

Type expressions

These classes represent different expressions for types. They do not have a common superclass.

Expression syntax CodeQL class Superclasses
[Expr] TypeExpr ArrayTypeExpr  
struct { ... } StructTypeExpr  
func FunctionName(...) (...) FuncTypeExpr  
interface { ... } InterfaceTypeExpr  
map[TypeExpr]TypeExpr MapTypeExpr  
chan<- TypeExpr SendChanTypeExpr ChanTypeExpr
<-chan TypeExpr RecvChanTypeExpr ChanTypeExpr
chan TypeExpr SendRecvChanTypeExpr ChanTypeExpr

Name expressions

All classes in this subsection are subclasses of Name.

The following classes relate to the structure of the name.

Expression syntax CodeQL class Superclasses
Ident SimpleName Ident
Ident.Ident QualifiedName SelectorExpr

The following classes relate to what sort of entity the name refers to.


Expression syntax CodeQL class Superclasses Remarks
foo Ident    
_ BlankIdent    
... Ellipsis    
(Expr) ParenExpr    
Ident.Ident SelectorExpr    
Expr[Expr] IndexExpr    
Expr[Expr:Expr:Expr] SliceExpr    
Expr.(TypeExpr) TypeAssertExpr    
*Expr StarExpr   can be a ValueExpr or TypeExpr depending on context
Expr: Expr KeyValueExpr    
TypeExpr(Expr) ConversionExpr CallOrConversionExpr  
Expr(...) CallExpr CallOrConversionExpr  
(anything unparseable) BadExpr    

The following classes organize expressions by the kind of entity they refer to.

CodeQL class Explanation
TypeExpr an expression that denotes a type
ReferenceExpr an expression that refers to a variable, a constant, a function, a field, or an element of an array or a slice
ValueExpr an expression that can be evaluated to a value (as opposed to expressions that refer to a package, a type, or a statement label). This generalizes ReferenceExpr