@@ -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.
@@ -363,11 +363,10 @@ class TypeSpec extends @typespec, Spec {
363
363
}
364
364
365
365
/**
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).
367
368
*/
368
- class FieldDecl extends @field, Documentable , ExprParent {
369
- FieldDecl ( ) { fields ( this , any ( StructTypeExpr st ) , _) }
370
-
369
+ class FieldBase extends @field, ExprParent {
371
370
/**
372
371
* Gets the expression representing the type of the fields declared in this declaration.
373
372
*/
@@ -377,6 +376,15 @@ class FieldDecl extends @field, Documentable, ExprParent {
377
376
* Gets the type of the fields declared in this declaration.
378
377
*/
379
378
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 ( _) }
380
388
381
389
/**
382
390
* 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 {
391
399
Expr getTag ( ) { result = getChildExpr ( - 1 ) }
392
400
393
401
/** Gets the struct type expression to which this field declaration belongs. */
394
- StructTypeExpr getDeclaringStructTypeExpr ( ) { fields ( this , result , _ ) }
402
+ StructTypeExpr getDeclaringStructTypeExpr ( ) { result = st }
395
403
396
404
/** Gets the struct type to which this field declaration belongs. */
397
405
StructType getDeclaringType ( ) { result = getDeclaringStructTypeExpr ( ) .getType ( ) }
@@ -407,74 +415,66 @@ class EmbeddedFieldDecl extends FieldDecl {
407
415
}
408
416
409
417
/**
410
- * A parameter declaration.
418
+ * A function parameter or result variable declaration.
411
419
*/
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 ) }
419
425
420
426
/**
421
- * Gets the function type expression to which this parameter declaration belongs.
427
+ * Gets the function type expression to which this declaration belongs.
422
428
*/
423
- FuncTypeExpr getFunctionTypeExpr ( ) { fields ( this , result , _ ) }
429
+ FuncTypeExpr getFunctionTypeExpr ( ) { result = ft }
424
430
425
431
/**
426
- * Gets the function to which this parameter declaration belongs.
432
+ * Gets the function to which this declaration belongs.
427
433
*/
428
434
FuncDef getFunction ( ) { result .getTypeExpr ( ) = getFunctionTypeExpr ( ) }
429
435
430
436
/**
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) .
433
439
*/
434
- int getIndex ( ) { fields ( this , _, result ) }
440
+ Expr getNameExpr ( int i ) {
441
+ i >= 0 and
442
+ result = getChildExpr ( i + 1 )
443
+ }
435
444
436
445
/**
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.
438
447
*/
439
- Expr getTypeExpr ( ) { result = getChildExpr ( 0 ) }
448
+ Expr getANameExpr ( ) { result = getNameExpr ( _) }
449
+ }
440
450
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 }
445
456
446
457
/**
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 .
449
460
*/
450
- Expr getNameExpr ( int i ) {
451
- i >= 0 and
452
- result = getChildExpr ( i + 1 )
453
- }
461
+ int getIndex ( ) { result = rawIndex }
454
462
455
463
override string toString ( ) { result = "parameter declaration" }
456
464
}
457
465
458
466
/**
459
467
* A receiver declaration in a function declaration.
460
468
*/
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 ;
463
471
464
- /**
465
- * Gets the function declaration to which this receiver belongs.
466
- */
467
- FuncDecl getFunction ( ) { fields ( this , result , _) }
472
+ ReceiverDecl ( ) { fd .getField ( - 1 ) = this }
468
473
469
474
/**
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.
476
476
*/
477
- Type getType ( ) { result = getTypeExpr ( ) . getType ( ) }
477
+ FuncDecl getFunction ( ) { result = fd }
478
478
479
479
/**
480
480
* Gets the expression representing the name of the receiver declared in this declaration.
@@ -487,60 +487,26 @@ class ReceiverDecl extends @field, Documentable, ExprParent {
487
487
/**
488
488
* A result variable declaration.
489
489
*/
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 }
526
492
527
493
/**
528
494
* Gets the index of this result variable declaration among all result variable declarations of
529
495
* its associated function type.
530
496
*/
531
- int getIndex ( ) { fields ( this , _ , - ( result + 1 ) ) }
497
+ int getIndex ( ) { result = - ( rawIndex + 1 ) }
532
498
533
499
override string toString ( ) { result = "result variable declaration" }
534
500
}
535
501
536
502
/**
537
503
* A method or embedding specification in an interface type expression.
538
504
*/
539
- class InterfaceMemberSpec extends @field , Documentable , ExprParent {
505
+ class InterfaceMemberSpec extends FieldBase , Documentable , ExprParent {
540
506
InterfaceTypeExpr ite ;
541
507
int idx ;
542
508
543
- InterfaceMemberSpec ( ) { fields ( this , ite , idx ) }
509
+ InterfaceMemberSpec ( ) { this = ite . getField ( idx ) }
544
510
545
511
/**
546
512
* Gets the interface type expression to which this member specification belongs.
@@ -552,17 +518,6 @@ class InterfaceMemberSpec extends @field, Documentable, ExprParent {
552
518
* its associated interface type expression.
553
519
*/
554
520
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 ( ) }
566
521
}
567
522
568
523
/**
0 commit comments