github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/pointer/testdata/structs.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  package main
     5  
     6  var unknown bool // defeat dead-code elimination
     7  
     8  var p, q int
     9  
    10  type A struct {
    11  	f *int
    12  	g interface{}
    13  }
    14  
    15  func (a A) m1() {
    16  	print(a.f) // @pointsto command-line-arguments.p
    17  }
    18  
    19  func (a *A) m2() {
    20  	print(a)   // @pointsto complit.A@struct1s:9
    21  	print(a.f) // @pointsto command-line-arguments.p
    22  }
    23  
    24  type B struct {
    25  	h *int
    26  	A
    27  }
    28  
    29  func structs1() {
    30  	b := &B{ // @line struct1s
    31  		h: &q,
    32  	}
    33  	b.f = &p
    34  	b.g = b
    35  
    36  	print(b.h) // @pointsto command-line-arguments.q
    37  	print(b.f) // @pointsto command-line-arguments.p
    38  	print(b.g) // @types *B
    39  
    40  	ptr := &b.f
    41  	print(*ptr) // @pointsto command-line-arguments.p
    42  
    43  	b.m1()
    44  	b.m2()
    45  }
    46  
    47  // @calls command-line-arguments.structs1 -> (command-line-arguments.A).m1
    48  // @calls command-line-arguments.structs1 -> (*command-line-arguments.A).m2
    49  // @calls (*command-line-arguments.B).m1 -> (command-line-arguments.A).m1
    50  // @calls (*command-line-arguments.B).m2 -> (*command-line-arguments.A).m2
    51  
    52  type T struct {
    53  	x int
    54  	y int
    55  }
    56  
    57  type S struct {
    58  	a [3]T
    59  	b *[3]T
    60  	c [3]*T
    61  }
    62  
    63  func structs2() {
    64  	var s S          // @line s2s
    65  	print(&s)        // @pointsto s@s2s:6
    66  	print(&s.a)      // @pointsto s.a@s2s:6
    67  	print(&s.a[0])   // @pointsto s.a[*]@s2s:6
    68  	print(&s.a[0].x) // @pointsto s.a[*].x@s2s:6
    69  	print(&s.a[0].y) // @pointsto s.a[*].y@s2s:6
    70  	print(&s.b)      // @pointsto s.b@s2s:6
    71  	print(&s.b[0])   // @pointsto
    72  	print(&s.b[0].x) // @pointsto
    73  	print(&s.b[0].y) // @pointsto
    74  	print(&s.c)      // @pointsto s.c@s2s:6
    75  	print(&s.c[0])   // @pointsto s.c[*]@s2s:6
    76  	print(&s.c[0].x) // @pointsto
    77  	print(&s.c[0].y) // @pointsto
    78  
    79  	var s2 S          // @line s2s2
    80  	s2.b = new([3]T)  // @line s2s2b
    81  	print(s2.b)       // @pointsto new@s2s2b:12
    82  	print(&s2.b)      // @pointsto s2.b@s2s2:6
    83  	print(&s2.b[0])   // @pointsto new[*]@s2s2b:12
    84  	print(&s2.b[0].x) // @pointsto new[*].x@s2s2b:12
    85  	print(&s2.b[0].y) // @pointsto new[*].y@s2s2b:12
    86  	print(&s2.c[0].x) // @pointsto
    87  	print(&s2.c[0].y) // @pointsto
    88  
    89  	var s3 S          // @line s2s3
    90  	s3.c[2] = new(T)  // @line s2s3c
    91  	print(&s3.c)      // @pointsto s3.c@s2s3:6
    92  	print(s3.c[1])    // @pointsto new@s2s3c:15
    93  	print(&s3.c[1])   // @pointsto s3.c[*]@s2s3:6
    94  	print(&s3.c[1].x) // @pointsto new.x@s2s3c:15
    95  	print(&s3.c[1].y) // @pointsto new.y@s2s3c:15
    96  }
    97  
    98  func main() {
    99  	structs1()
   100  	structs2()
   101  }