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

Commit 79b0ea8

Browse files
authored
Merge pull request #183 from smowton/smowton/cleanup/field-parent
Clean up @field and @fieldparent usage
2 parents 7c2358c + 3c8153c commit 79b0ea8

File tree

3 files changed

+80
-120
lines changed

3 files changed

+80
-120
lines changed

ql/src/semmle/go/AST.qll

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class AstNode extends @node, Locatable {
2020
result = this.(StmtParent).getChildStmt(i) or
2121
result = this.(DeclParent).getDecl(i) or
2222
result = this.(GenDecl).getSpec(i) or
23-
fields(result, this, i)
23+
result = this.(FieldParent).getField(i)
2424
}
2525

2626
/**
@@ -146,6 +146,29 @@ class DeclParent extends @declparent, AstNode {
146146
int getNumDecl() { result = count(getADecl()) }
147147
}
148148

149+
/**
150+
* An AST node whose children include fields.
151+
*/
152+
class FieldParent extends @fieldparent, AstNode {
153+
/**
154+
* Gets the `i`th field of this node.
155+
*
156+
* Note that the precise indices of fields are considered an implementation detail
157+
* and are subject to change without notice.
158+
*/
159+
FieldBase getField(int i) { fields(result, this, i) }
160+
161+
/**
162+
* Gets a child field of this node in the AST.
163+
*/
164+
FieldBase getAField() { result = getField(_) }
165+
166+
/**
167+
* Gets the number of child fields of this node.
168+
*/
169+
int getNumFields() { result = count(getAField()) }
170+
}
171+
149172
/**
150173
* An AST node which may induce a scope.
151174
*

ql/src/semmle/go/Decls.qll

Lines changed: 50 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import go
77
/**
88
* A declaration.
99
*/
10-
class Decl extends @decl, ExprParent, StmtParent {
10+
class Decl extends @decl, ExprParent, StmtParent, FieldParent {
1111
/**
1212
* Gets the kind of this declaration, which is an integer value representing the declaration's
1313
* node type.
@@ -389,11 +389,10 @@ class TypeSpec extends @typespec, Spec {
389389
}
390390

391391
/**
392-
* A field declaration in a struct type.
392+
* A field declaration, of a struct, a function (in which case this is a parameter or result variable),
393+
* or an interface (in which case this is a method or embedding spec).
393394
*/
394-
class FieldDecl extends @field, Documentable, ExprParent {
395-
FieldDecl() { fields(this, any(StructTypeExpr st), _) }
396-
395+
class FieldBase extends @field, ExprParent {
397396
/**
398397
* Gets the expression representing the type of the fields declared in this declaration.
399398
*/
@@ -403,6 +402,15 @@ class FieldDecl extends @field, Documentable, ExprParent {
403402
* Gets the type of the fields declared in this declaration.
404403
*/
405404
Type getType() { result = getTypeExpr().getType() }
405+
}
406+
407+
/**
408+
* A field declaration in a struct type.
409+
*/
410+
class FieldDecl extends FieldBase, Documentable, ExprParent {
411+
StructTypeExpr st;
412+
413+
FieldDecl() { this = st.getField(_) }
406414

407415
/**
408416
* Gets the expression representing the name of the `i`th field declared in this declaration
@@ -417,7 +425,7 @@ class FieldDecl extends @field, Documentable, ExprParent {
417425
Expr getTag() { result = getChildExpr(-1) }
418426

419427
/** Gets the struct type expression to which this field declaration belongs. */
420-
StructTypeExpr getDeclaringStructTypeExpr() { fields(this, result, _) }
428+
StructTypeExpr getDeclaringStructTypeExpr() { result = st }
421429

422430
/** Gets the struct type to which this field declaration belongs. */
423431
StructType getDeclaringType() { result = getDeclaringStructTypeExpr().getType() }
@@ -437,50 +445,50 @@ class EmbeddedFieldDecl extends FieldDecl {
437445
}
438446

439447
/**
440-
* A parameter declaration.
448+
* A function parameter or result variable declaration.
441449
*/
442-
class ParameterDecl extends @field, Documentable, ExprParent {
443-
ParameterDecl() {
444-
exists(int i |
445-
fields(this, any(FuncTypeExpr ft), i) and
446-
i >= 0
447-
)
448-
}
450+
class ParameterOrResultDecl extends FieldBase, Documentable, ExprParent {
451+
int rawIndex;
452+
FuncTypeExpr ft;
453+
454+
ParameterOrResultDecl() { this = ft.getField(rawIndex) }
449455

450456
/**
451-
* Gets the function type expression to which this parameter declaration belongs.
457+
* Gets the function type expression to which this declaration belongs.
452458
*/
453-
FuncTypeExpr getFunctionTypeExpr() { fields(this, result, _) }
459+
FuncTypeExpr getFunctionTypeExpr() { result = ft }
454460

455461
/**
456-
* Gets the function to which this parameter declaration belongs.
462+
* Gets the function to which this declaration belongs.
457463
*/
458464
FuncDef getFunction() { result.getTypeExpr() = getFunctionTypeExpr() }
459465

460466
/**
461-
* Gets the index of this parameter declarations among all parameter declarations of
462-
* its associated function type.
467+
* Gets the expression representing the name of the `i`th variable declared in this declaration
468+
* (0-based).
463469
*/
464-
int getIndex() { fields(this, _, result) }
470+
Expr getNameExpr(int i) {
471+
i >= 0 and
472+
result = getChildExpr(i + 1)
473+
}
465474

466475
/**
467-
* Gets the expression representing the type of the parameters declared in this declaration.
476+
* Gets an expression representing the name of a variable declared in this declaration.
468477
*/
469-
Expr getTypeExpr() { result = getChildExpr(0) }
478+
Expr getANameExpr() { result = getNameExpr(_) }
479+
}
470480

471-
/**
472-
* Gets the type of the parameters declared in this declaration.
473-
*/
474-
Type getType() { result = getTypeExpr().getType() }
481+
/**
482+
* A parameter declaration.
483+
*/
484+
class ParameterDecl extends ParameterOrResultDecl {
485+
ParameterDecl() { rawIndex >= 0 }
475486

476487
/**
477-
* Gets the expression representing the name of the `i`th parameter declared in this declaration
478-
* (0-based).
488+
* Gets the index of this parameter declarations among all parameter declarations of
489+
* its associated function type.
479490
*/
480-
Expr getNameExpr(int i) {
481-
i >= 0 and
482-
result = getChildExpr(i + 1)
483-
}
491+
int getIndex() { result = rawIndex }
484492

485493
override string toString() { result = "parameter declaration" }
486494

@@ -490,23 +498,15 @@ class ParameterDecl extends @field, Documentable, ExprParent {
490498
/**
491499
* A receiver declaration in a function declaration.
492500
*/
493-
class ReceiverDecl extends @field, Documentable, ExprParent {
494-
ReceiverDecl() { fields(this, any(FuncDecl fd), -1) }
501+
class ReceiverDecl extends FieldBase, Documentable, ExprParent {
502+
FuncDecl fd;
495503

496-
/**
497-
* Gets the function declaration to which this receiver belongs.
498-
*/
499-
FuncDecl getFunction() { fields(this, result, _) }
504+
ReceiverDecl() { fd.getField(-1) = this }
500505

501506
/**
502-
* Gets the expression representing the type of the receiver declared in this declaration.
503-
*/
504-
Expr getTypeExpr() { result = getChildExpr(0) }
505-
506-
/**
507-
* Gets the type of the receiver declared in this declaration.
507+
* Gets the function declaration to which this receiver belongs.
508508
*/
509-
Type getType() { result = getTypeExpr().getType() }
509+
FuncDecl getFunction() { result = fd }
510510

511511
/**
512512
* Gets the expression representing the name of the receiver declared in this declaration.
@@ -521,48 +521,14 @@ class ReceiverDecl extends @field, Documentable, ExprParent {
521521
/**
522522
* A result variable declaration.
523523
*/
524-
class ResultVariableDecl extends @field, Documentable, ExprParent {
525-
ResultVariableDecl() {
526-
exists(int i |
527-
fields(this, any(FuncTypeExpr ft), i) and
528-
i < 0
529-
)
530-
}
531-
532-
/**
533-
* Gets the expression representing the type of the result variables declared in this declaration.
534-
*/
535-
Expr getTypeExpr() { result = getChildExpr(0) }
536-
537-
/**
538-
* Gets the type of the result variables declared in this declaration.
539-
*/
540-
Type getType() { result = getTypeExpr().getType() }
541-
542-
/**
543-
* Gets the expression representing the name of the `i`th result variable declared in this declaration
544-
* (0-based).
545-
*/
546-
Expr getNameExpr(int i) {
547-
i >= 0 and
548-
result = getChildExpr(i + 1)
549-
}
550-
551-
/**
552-
* Gets an expression representing the name of a result variable declared in this declaration.
553-
*/
554-
Expr getANameExpr() { result = getNameExpr(_) }
555-
556-
/**
557-
* Gets the function type expression to which this result variable declaration belongs.
558-
*/
559-
FuncTypeExpr getFunctionTypeExpr() { fields(this, result, _) }
524+
class ResultVariableDecl extends ParameterOrResultDecl {
525+
ResultVariableDecl() { rawIndex < 0 }
560526

561527
/**
562528
* Gets the index of this result variable declaration among all result variable declarations of
563529
* its associated function type.
564530
*/
565-
int getIndex() { fields(this, _, -(result + 1)) }
531+
int getIndex() { result = -(rawIndex + 1) }
566532

567533
override string toString() { result = "result variable declaration" }
568534

@@ -572,11 +538,11 @@ class ResultVariableDecl extends @field, Documentable, ExprParent {
572538
/**
573539
* A method or embedding specification in an interface type expression.
574540
*/
575-
class InterfaceMemberSpec extends @field, Documentable, ExprParent {
541+
class InterfaceMemberSpec extends FieldBase, Documentable, ExprParent {
576542
InterfaceTypeExpr ite;
577543
int idx;
578544

579-
InterfaceMemberSpec() { fields(this, ite, idx) }
545+
InterfaceMemberSpec() { this = ite.getField(idx) }
580546

581547
/**
582548
* Gets the interface type expression to which this member specification belongs.
@@ -588,17 +554,6 @@ class InterfaceMemberSpec extends @field, Documentable, ExprParent {
588554
* its associated interface type expression.
589555
*/
590556
int getIndex() { result = idx }
591-
592-
/**
593-
* Gets the expression representing the type of the method or embedding declared in
594-
* this specification.
595-
*/
596-
Expr getTypeExpr() { result = getChildExpr(0) }
597-
598-
/**
599-
* Gets the type of the method or embedding declared in this specification.
600-
*/
601-
Type getType() { result = getTypeExpr().getType() }
602557
}
603558

604559
/**

ql/src/semmle/go/Expr.qll

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -844,16 +844,7 @@ class ArrayTypeExpr extends @arraytypeexpr, TypeExpr {
844844
* struct {x, y int; z float32}
845845
* ```
846846
*/
847-
class StructTypeExpr extends @structtypeexpr, TypeExpr {
848-
/** Gets the `i`th field declared in this struct type expression (0-based). */
849-
FieldDecl getField(int i) { fields(result, this, i) }
850-
851-
/** Gets a field declared in this struct type expression. */
852-
FieldDecl getAField() { result = getField(_) }
853-
854-
/** Gets the number of fields declared in this struct type expression. */
855-
int getNumField() { result = count(getAField()) }
856-
847+
class StructTypeExpr extends @structtypeexpr, TypeExpr, FieldParent {
857848
override string toString() { result = "struct type" }
858849

859850
override string describeQlClass() { result = "StructTypeExpr" }
@@ -868,12 +859,9 @@ class StructTypeExpr extends @structtypeexpr, TypeExpr {
868859
* func(a, b int, c float32) (float32, bool)
869860
* ```
870861
*/
871-
class FuncTypeExpr extends @functypeexpr, TypeExpr, ScopeNode {
862+
class FuncTypeExpr extends @functypeexpr, TypeExpr, ScopeNode, FieldParent {
872863
/** Gets the `i`th parameter of this function type (0-based). */
873-
ParameterDecl getParameterDecl(int i) {
874-
result.getFunctionTypeExpr() = this and
875-
result.getIndex() = i
876-
}
864+
ParameterDecl getParameterDecl(int i) { result = getField(i) and i >= 0 }
877865

878866
/** Gets a parameter of this function type. */
879867
ParameterDecl getAParameterDecl() { result = getParameterDecl(_) }
@@ -882,10 +870,7 @@ class FuncTypeExpr extends @functypeexpr, TypeExpr, ScopeNode {
882870
int getNumParameter() { result = count(getAParameterDecl()) }
883871

884872
/** Gets the `i`th result of this function type (0-based). */
885-
ResultVariableDecl getResultDecl(int i) {
886-
result.getFunctionTypeExpr() = this and
887-
result.getIndex() = i
888-
}
873+
ResultVariableDecl getResultDecl(int i) { result = getField(-(i + 1)) }
889874

890875
/** Gets a result of this function type. */
891876
ResultVariableDecl getAResultDecl() { result = getResultDecl(_) }
@@ -910,12 +895,9 @@ class FuncTypeExpr extends @functypeexpr, TypeExpr, ScopeNode {
910895
* interface { Read(p []byte) (n int, err error); Close() error}
911896
* ```
912897
*/
913-
class InterfaceTypeExpr extends @interfacetypeexpr, TypeExpr {
898+
class InterfaceTypeExpr extends @interfacetypeexpr, TypeExpr, FieldParent {
914899
/** Gets the `i`th method specification of this interface type. */
915-
MethodSpec getMethod(int i) {
916-
result.getInterfaceTypeExpr() = this and
917-
result.getIndex() = i
918-
}
900+
MethodSpec getMethod(int i) { result = getField(i) }
919901

920902
/** Gets a method of this interface type. */
921903
MethodSpec getAMethod() { result = getMethod(_) }

0 commit comments

Comments
 (0)