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