Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit f11b956

Browse files
authored
Add a superclass for literals (#172)
1 parent 40ffa22 commit f11b956

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

docs/language/learn-ql/go/ast-class-reference.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ All classes in this section are subclasses of
287287
Literals
288288
~~~~~~~~
289289

290+
All classes in this subsection are subclasses of
291+
`Literal <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$Literal.html>`__.
292+
290293
+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+
291294
| Expression syntax example | CodeQL class | Superclass |
292295
+=========================================+==============================================================================================+====================================================================================================+

ql/src/semmle/go/Expr.qll

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,23 @@ class Ellipsis extends @ellipsis, Expr {
212212
override string toString() { result = "..." }
213213
}
214214

215+
/**
216+
* A literal expression.
217+
*
218+
* Examples:
219+
*
220+
* ```go
221+
* "hello"
222+
* func(x, y int) int { return x + y }
223+
* map[string]int{"A": 1, "B": 2}
224+
* ```
225+
*/
226+
class Literal extends Expr {
227+
Literal() {
228+
this instanceof @basiclit or this instanceof @funclit or this instanceof @compositelit
229+
}
230+
}
231+
215232
/**
216233
* A literal expression of basic type.
217234
*
@@ -222,7 +239,7 @@ class Ellipsis extends @ellipsis, Expr {
222239
* "hello"
223240
* ```
224241
*/
225-
class BasicLit extends @basiclit, Expr {
242+
class BasicLit extends @basiclit, Literal {
226243
/** Gets the value of this literal expressed as a string. */
227244
string getValue() { literals(this, result, _) }
228245

@@ -319,10 +336,10 @@ class StringLit extends @stringlit, BasicLit { }
319336
* func(x, y int) int { return x + y }
320337
* ```
321338
*/
322-
class FuncLit extends @funclit, Expr, StmtParent, FuncDef {
339+
class FuncLit extends @funclit, Literal, StmtParent, FuncDef {
323340
override FuncTypeExpr getTypeExpr() { result = getChildExpr(0) }
324341

325-
override SignatureType getType() { result = Expr.super.getType() }
342+
override SignatureType getType() { result = Literal.super.getType() }
326343

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

0 commit comments

Comments
 (0)