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

Commit 3c8153c

Browse files
committed
Clean up @field and @fieldparent usage
* Centralise use of raw types and database predicates in FieldParent and FieldBase classes * Deduplicate type predicates common to all fields * Deduplicate predicates common to function parameters and results
1 parent 2282def commit 3c8153c

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
/**
@@ -140,6 +140,29 @@ class DeclParent extends @declparent, AstNode {
140140
int getNumDecl() { result = count(getADecl()) }
141141
}
142142

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

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.
@@ -363,11 +363,10 @@ class TypeSpec extends @typespec, Spec {
363363
}
364364

365365
/**
366-
* A field declaration in a struct type.
366+
* A field declaration, of a struct, a function (in which case this is a parameter or result variable),
367+
* or an interface (in which case this is a method or embedding spec).
367368
*/
368-
class FieldDecl extends @field, Documentable, ExprParent {
369-
FieldDecl() { fields(this, any(StructTypeExpr st), _) }
370-
369+
class FieldBase extends @field, ExprParent {
371370
/**
372371
* Gets the expression representing the type of the fields declared in this declaration.
373372
*/
@@ -377,6 +376,15 @@ class FieldDecl extends @field, Documentable, ExprParent {
377376
* Gets the type of the fields declared in this declaration.
378377
*/
379378
Type getType() { result = getTypeExpr().getType() }
379+
}
380+
381+
/**
382+
* A field declaration in a struct type.
383+
*/
384+
class FieldDecl extends FieldBase, Documentable, ExprParent {
385+
StructTypeExpr st;
386+
387+
FieldDecl() { this = st.getField(_) }
380388

381389
/**
382390
* Gets the expression representing the name of the `i`th field declared in this declaration
@@ -391,7 +399,7 @@ class FieldDecl extends @field, Documentable, ExprParent {
391399
Expr getTag() { result = getChildExpr(-1) }
392400

393401
/** Gets the struct type expression to which this field declaration belongs. */
394-
StructTypeExpr getDeclaringStructTypeExpr() { fields(this, result, _) }
402+
StructTypeExpr getDeclaringStructTypeExpr() { result = st }
395403

396404
/** Gets the struct type to which this field declaration belongs. */
397405
StructType getDeclaringType() { result = getDeclaringStructTypeExpr().getType() }
@@ -407,74 +415,66 @@ class EmbeddedFieldDecl extends FieldDecl {
407415
}
408416

409417
/**
410-
* A parameter declaration.
418+
* A function parameter or result variable declaration.
411419
*/
412-
class ParameterDecl extends @field, Documentable, ExprParent {
413-
ParameterDecl() {
414-
exists(int i |
415-
fields(this, any(FuncTypeExpr ft), i) and
416-
i >= 0
417-
)
418-
}
420+
class ParameterOrResultDecl extends FieldBase, Documentable, ExprParent {
421+
int rawIndex;
422+
FuncTypeExpr ft;
423+
424+
ParameterOrResultDecl() { this = ft.getField(rawIndex) }
419425

420426
/**
421-
* Gets the function type expression to which this parameter declaration belongs.
427+
* Gets the function type expression to which this declaration belongs.
422428
*/
423-
FuncTypeExpr getFunctionTypeExpr() { fields(this, result, _) }
429+
FuncTypeExpr getFunctionTypeExpr() { result = ft }
424430

425431
/**
426-
* Gets the function to which this parameter declaration belongs.
432+
* Gets the function to which this declaration belongs.
427433
*/
428434
FuncDef getFunction() { result.getTypeExpr() = getFunctionTypeExpr() }
429435

430436
/**
431-
* Gets the index of this parameter declarations among all parameter declarations of
432-
* its associated function type.
437+
* Gets the expression representing the name of the `i`th variable declared in this declaration
438+
* (0-based).
433439
*/
434-
int getIndex() { fields(this, _, result) }
440+
Expr getNameExpr(int i) {
441+
i >= 0 and
442+
result = getChildExpr(i + 1)
443+
}
435444

436445
/**
437-
* Gets the expression representing the type of the parameters declared in this declaration.
446+
* Gets an expression representing the name of a variable declared in this declaration.
438447
*/
439-
Expr getTypeExpr() { result = getChildExpr(0) }
448+
Expr getANameExpr() { result = getNameExpr(_) }
449+
}
440450

441-
/**
442-
* Gets the type of the parameters declared in this declaration.
443-
*/
444-
Type getType() { result = getTypeExpr().getType() }
451+
/**
452+
* A parameter declaration.
453+
*/
454+
class ParameterDecl extends ParameterOrResultDecl {
455+
ParameterDecl() { rawIndex >= 0 }
445456

446457
/**
447-
* Gets the expression representing the name of the `i`th parameter declared in this declaration
448-
* (0-based).
458+
* Gets the index of this parameter declarations among all parameter declarations of
459+
* its associated function type.
449460
*/
450-
Expr getNameExpr(int i) {
451-
i >= 0 and
452-
result = getChildExpr(i + 1)
453-
}
461+
int getIndex() { result = rawIndex }
454462

455463
override string toString() { result = "parameter declaration" }
456464
}
457465

458466
/**
459467
* A receiver declaration in a function declaration.
460468
*/
461-
class ReceiverDecl extends @field, Documentable, ExprParent {
462-
ReceiverDecl() { fields(this, any(FuncDecl fd), -1) }
469+
class ReceiverDecl extends FieldBase, Documentable, ExprParent {
470+
FuncDecl fd;
463471

464-
/**
465-
* Gets the function declaration to which this receiver belongs.
466-
*/
467-
FuncDecl getFunction() { fields(this, result, _) }
472+
ReceiverDecl() { fd.getField(-1) = this }
468473

469474
/**
470-
* Gets the expression representing the type of the receiver declared in this declaration.
471-
*/
472-
Expr getTypeExpr() { result = getChildExpr(0) }
473-
474-
/**
475-
* Gets the type of the receiver declared in this declaration.
475+
* Gets the function declaration to which this receiver belongs.
476476
*/
477-
Type getType() { result = getTypeExpr().getType() }
477+
FuncDecl getFunction() { result = fd }
478478

479479
/**
480480
* Gets the expression representing the name of the receiver declared in this declaration.
@@ -487,60 +487,26 @@ class ReceiverDecl extends @field, Documentable, ExprParent {
487487
/**
488488
* A result variable declaration.
489489
*/
490-
class ResultVariableDecl extends @field, Documentable, ExprParent {
491-
ResultVariableDecl() {
492-
exists(int i |
493-
fields(this, any(FuncTypeExpr ft), i) and
494-
i < 0
495-
)
496-
}
497-
498-
/**
499-
* Gets the expression representing the type of the result variables declared in this declaration.
500-
*/
501-
Expr getTypeExpr() { result = getChildExpr(0) }
502-
503-
/**
504-
* Gets the type of the result variables declared in this declaration.
505-
*/
506-
Type getType() { result = getTypeExpr().getType() }
507-
508-
/**
509-
* Gets the expression representing the name of the `i`th result variable declared in this declaration
510-
* (0-based).
511-
*/
512-
Expr getNameExpr(int i) {
513-
i >= 0 and
514-
result = getChildExpr(i + 1)
515-
}
516-
517-
/**
518-
* Gets an expression representing the name of a result variable declared in this declaration.
519-
*/
520-
Expr getANameExpr() { result = getNameExpr(_) }
521-
522-
/**
523-
* Gets the function type expression to which this result variable declaration belongs.
524-
*/
525-
FuncTypeExpr getFunctionTypeExpr() { fields(this, result, _) }
490+
class ResultVariableDecl extends ParameterOrResultDecl {
491+
ResultVariableDecl() { rawIndex < 0 }
526492

527493
/**
528494
* Gets the index of this result variable declaration among all result variable declarations of
529495
* its associated function type.
530496
*/
531-
int getIndex() { fields(this, _, -(result + 1)) }
497+
int getIndex() { result = -(rawIndex + 1) }
532498

533499
override string toString() { result = "result variable declaration" }
534500
}
535501

536502
/**
537503
* A method or embedding specification in an interface type expression.
538504
*/
539-
class InterfaceMemberSpec extends @field, Documentable, ExprParent {
505+
class InterfaceMemberSpec extends FieldBase, Documentable, ExprParent {
540506
InterfaceTypeExpr ite;
541507
int idx;
542508

543-
InterfaceMemberSpec() { fields(this, ite, idx) }
509+
InterfaceMemberSpec() { this = ite.getField(idx) }
544510

545511
/**
546512
* Gets the interface type expression to which this member specification belongs.
@@ -552,17 +518,6 @@ class InterfaceMemberSpec extends @field, Documentable, ExprParent {
552518
* its associated interface type expression.
553519
*/
554520
int getIndex() { result = idx }
555-
556-
/**
557-
* Gets the expression representing the type of the method or embedding declared in
558-
* this specification.
559-
*/
560-
Expr getTypeExpr() { result = getChildExpr(0) }
561-
562-
/**
563-
* Gets the type of the method or embedding declared in this specification.
564-
*/
565-
Type getType() { result = getTypeExpr().getType() }
566521
}
567522

568523
/**

ql/src/semmle/go/Expr.qll

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -792,16 +792,7 @@ class ArrayTypeExpr extends @arraytypeexpr, TypeExpr {
792792
* struct {x, y int; z float32}
793793
* ```
794794
*/
795-
class StructTypeExpr extends @structtypeexpr, TypeExpr {
796-
/** Gets the `i`th field declared in this struct type expression (0-based). */
797-
FieldDecl getField(int i) { fields(result, this, i) }
798-
799-
/** Gets a field declared in this struct type expression. */
800-
FieldDecl getAField() { result = getField(_) }
801-
802-
/** Gets the number of fields declared in this struct type expression. */
803-
int getNumField() { result = count(getAField()) }
804-
795+
class StructTypeExpr extends @structtypeexpr, TypeExpr, FieldParent {
805796
override string toString() { result = "struct type" }
806797
}
807798

@@ -814,12 +805,9 @@ class StructTypeExpr extends @structtypeexpr, TypeExpr {
814805
* func(a, b int, c float32) (float32, bool)
815806
* ```
816807
*/
817-
class FuncTypeExpr extends @functypeexpr, TypeExpr, ScopeNode {
808+
class FuncTypeExpr extends @functypeexpr, TypeExpr, ScopeNode, FieldParent {
818809
/** Gets the `i`th parameter of this function type (0-based). */
819-
ParameterDecl getParameterDecl(int i) {
820-
result.getFunctionTypeExpr() = this and
821-
result.getIndex() = i
822-
}
810+
ParameterDecl getParameterDecl(int i) { result = getField(i) and i >= 0 }
823811

824812
/** Gets a parameter of this function type. */
825813
ParameterDecl getAParameterDecl() { result = getParameterDecl(_) }
@@ -828,10 +816,7 @@ class FuncTypeExpr extends @functypeexpr, TypeExpr, ScopeNode {
828816
int getNumParameter() { result = count(getAParameterDecl()) }
829817

830818
/** Gets the `i`th result of this function type (0-based). */
831-
ResultVariableDecl getResultDecl(int i) {
832-
result.getFunctionTypeExpr() = this and
833-
result.getIndex() = i
834-
}
819+
ResultVariableDecl getResultDecl(int i) { result = getField(-(i + 1)) }
835820

836821
/** Gets a result of this function type. */
837822
ResultVariableDecl getAResultDecl() { result = getResultDecl(_) }
@@ -854,12 +839,9 @@ class FuncTypeExpr extends @functypeexpr, TypeExpr, ScopeNode {
854839
* interface { Read(p []byte) (n int, err error); Close() error}
855840
* ```
856841
*/
857-
class InterfaceTypeExpr extends @interfacetypeexpr, TypeExpr {
842+
class InterfaceTypeExpr extends @interfacetypeexpr, TypeExpr, FieldParent {
858843
/** Gets the `i`th method specification of this interface type. */
859-
MethodSpec getMethod(int i) {
860-
result.getInterfaceTypeExpr() = this and
861-
result.getIndex() = i
862-
}
844+
MethodSpec getMethod(int i) { result = getField(i) }
863845

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

0 commit comments

Comments
 (0)