@@ -7,7 +7,7 @@ import go
7
7
/**
8
8
* A declaration.
9
9
*/
10
- class Decl extends @decl, ExprParent , StmtParent {
10
+ class Decl extends @decl, ExprParent , StmtParent , FieldParent {
11
11
/**
12
12
* Gets the kind of this declaration, which is an integer value representing the declaration's
13
13
* node type.
@@ -389,11 +389,10 @@ class TypeSpec extends @typespec, Spec {
389
389
}
390
390
391
391
/**
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).
393
394
*/
394
- class FieldDecl extends @field, Documentable , ExprParent {
395
- FieldDecl ( ) { fields ( this , any ( StructTypeExpr st ) , _) }
396
-
395
+ class FieldBase extends @field, ExprParent {
397
396
/**
398
397
* Gets the expression representing the type of the fields declared in this declaration.
399
398
*/
@@ -403,6 +402,15 @@ class FieldDecl extends @field, Documentable, ExprParent {
403
402
* Gets the type of the fields declared in this declaration.
404
403
*/
405
404
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 ( _) }
406
414
407
415
/**
408
416
* 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 {
417
425
Expr getTag ( ) { result = getChildExpr ( - 1 ) }
418
426
419
427
/** Gets the struct type expression to which this field declaration belongs. */
420
- StructTypeExpr getDeclaringStructTypeExpr ( ) { fields ( this , result , _ ) }
428
+ StructTypeExpr getDeclaringStructTypeExpr ( ) { result = st }
421
429
422
430
/** Gets the struct type to which this field declaration belongs. */
423
431
StructType getDeclaringType ( ) { result = getDeclaringStructTypeExpr ( ) .getType ( ) }
@@ -437,50 +445,50 @@ class EmbeddedFieldDecl extends FieldDecl {
437
445
}
438
446
439
447
/**
440
- * A parameter declaration.
448
+ * A function parameter or result variable declaration.
441
449
*/
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 ) }
449
455
450
456
/**
451
- * Gets the function type expression to which this parameter declaration belongs.
457
+ * Gets the function type expression to which this declaration belongs.
452
458
*/
453
- FuncTypeExpr getFunctionTypeExpr ( ) { fields ( this , result , _ ) }
459
+ FuncTypeExpr getFunctionTypeExpr ( ) { result = ft }
454
460
455
461
/**
456
- * Gets the function to which this parameter declaration belongs.
462
+ * Gets the function to which this declaration belongs.
457
463
*/
458
464
FuncDef getFunction ( ) { result .getTypeExpr ( ) = getFunctionTypeExpr ( ) }
459
465
460
466
/**
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) .
463
469
*/
464
- int getIndex ( ) { fields ( this , _, result ) }
470
+ Expr getNameExpr ( int i ) {
471
+ i >= 0 and
472
+ result = getChildExpr ( i + 1 )
473
+ }
465
474
466
475
/**
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.
468
477
*/
469
- Expr getTypeExpr ( ) { result = getChildExpr ( 0 ) }
478
+ Expr getANameExpr ( ) { result = getNameExpr ( _) }
479
+ }
470
480
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 }
475
486
476
487
/**
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 .
479
490
*/
480
- Expr getNameExpr ( int i ) {
481
- i >= 0 and
482
- result = getChildExpr ( i + 1 )
483
- }
491
+ int getIndex ( ) { result = rawIndex }
484
492
485
493
override string toString ( ) { result = "parameter declaration" }
486
494
@@ -490,23 +498,15 @@ class ParameterDecl extends @field, Documentable, ExprParent {
490
498
/**
491
499
* A receiver declaration in a function declaration.
492
500
*/
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 ;
495
503
496
- /**
497
- * Gets the function declaration to which this receiver belongs.
498
- */
499
- FuncDecl getFunction ( ) { fields ( this , result , _) }
504
+ ReceiverDecl ( ) { fd .getField ( - 1 ) = this }
500
505
501
506
/**
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.
508
508
*/
509
- Type getType ( ) { result = getTypeExpr ( ) . getType ( ) }
509
+ FuncDecl getFunction ( ) { result = fd }
510
510
511
511
/**
512
512
* Gets the expression representing the name of the receiver declared in this declaration.
@@ -521,48 +521,14 @@ class ReceiverDecl extends @field, Documentable, ExprParent {
521
521
/**
522
522
* A result variable declaration.
523
523
*/
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 }
560
526
561
527
/**
562
528
* Gets the index of this result variable declaration among all result variable declarations of
563
529
* its associated function type.
564
530
*/
565
- int getIndex ( ) { fields ( this , _ , - ( result + 1 ) ) }
531
+ int getIndex ( ) { result = - ( rawIndex + 1 ) }
566
532
567
533
override string toString ( ) { result = "result variable declaration" }
568
534
@@ -572,11 +538,11 @@ class ResultVariableDecl extends @field, Documentable, ExprParent {
572
538
/**
573
539
* A method or embedding specification in an interface type expression.
574
540
*/
575
- class InterfaceMemberSpec extends @field , Documentable , ExprParent {
541
+ class InterfaceMemberSpec extends FieldBase , Documentable , ExprParent {
576
542
InterfaceTypeExpr ite ;
577
543
int idx ;
578
544
579
- InterfaceMemberSpec ( ) { fields ( this , ite , idx ) }
545
+ InterfaceMemberSpec ( ) { this = ite . getField ( idx ) }
580
546
581
547
/**
582
548
* Gets the interface type expression to which this member specification belongs.
@@ -588,17 +554,6 @@ class InterfaceMemberSpec extends @field, Documentable, ExprParent {
588
554
* its associated interface type expression.
589
555
*/
590
556
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 ( ) }
602
557
}
603
558
604
559
/**
0 commit comments