@@ -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/**
0 commit comments