Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/language/learn-ql/go/ast-class-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ All classes in this section are subclasses of
Literals
~~~~~~~~

All classes in this subsection are subclasses of
`Literal <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$Literal.html>`__.

+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+
| Expression syntax example | CodeQL class | Superclass |
+=========================================+==============================================================================================+====================================================================================================+
Expand Down
25 changes: 21 additions & 4 deletions ql/src/semmle/go/Expr.qll
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,23 @@ class Ellipsis extends @ellipsis, Expr {
override string toString() { result = "..." }
}

/**
* A literal expression.
*
* Examples:
*
* ```go
* "hello"
* func(x, y int) int { return x + y }
* map[string]int{"A": 1, "B": 2}
* ```
*/
class Literal extends Expr {
Literal() {
this instanceof @basiclit or this instanceof @funclit or this instanceof @compositelit
}
}

/**
* A literal expression of basic type.
*
Expand All @@ -222,7 +239,7 @@ class Ellipsis extends @ellipsis, Expr {
* "hello"
* ```
*/
class BasicLit extends @basiclit, Expr {
class BasicLit extends @basiclit, Literal {
/** Gets the value of this literal expressed as a string. */
string getValue() { literals(this, result, _) }

Expand Down Expand Up @@ -319,10 +336,10 @@ class StringLit extends @stringlit, BasicLit { }
* func(x, y int) int { return x + y }
* ```
*/
class FuncLit extends @funclit, Expr, StmtParent, FuncDef {
class FuncLit extends @funclit, Literal, StmtParent, FuncDef {
override FuncTypeExpr getTypeExpr() { result = getChildExpr(0) }

override SignatureType getType() { result = Expr.super.getType() }
override SignatureType getType() { result = Literal.super.getType() }

/** Gets the body of this function literal. */
override BlockStmt getBody() { result = getChildStmt(1) }
Expand All @@ -342,7 +359,7 @@ class FuncLit extends @funclit, Expr, StmtParent, FuncDef {
* map[string]int{"A": 1, "B": 2}
* ```
*/
class CompositeLit extends @compositelit, Expr {
class CompositeLit extends @compositelit, Literal {
/** Gets the expression representing the type of this composite literal. */
Expr getTypeExpr() { result = getChildExpr(0) }

Expand Down