github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/callgraph/vta/testdata/src/callgraph_interfaces.go (about)

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // go:build ignore
     6  
     7  package testdata
     8  
     9  type I interface {
    10  	Foo()
    11  }
    12  
    13  type A struct{}
    14  
    15  func (a A) Foo() {}
    16  
    17  type B struct{}
    18  
    19  func (b B) Foo() {}
    20  
    21  type C struct{}
    22  
    23  func (c C) Foo() {}
    24  
    25  func NewB() B {
    26  	return B{}
    27  }
    28  
    29  func Do(b bool) I {
    30  	if b {
    31  		return A{}
    32  	}
    33  
    34  	c := C{}
    35  	c.Foo()
    36  
    37  	return NewB()
    38  }
    39  
    40  func Baz(b bool) {
    41  	Do(b).Foo()
    42  }
    43  
    44  // Relevant SSA:
    45  // func Baz(b bool):
    46  //   t0 = Do(b)
    47  //   t1 = invoke t0.Foo()
    48  //   return
    49  
    50  // func Do(b bool) I:
    51  //    ...
    52  //   t3 = local C (c)
    53  //   t4 = *t3
    54  //   t5 = (C).Foo(t4)
    55  //   t6 = NewB()
    56  //   t7 = make I <- B (t6)
    57  //   return t7
    58  
    59  // WANT:
    60  // Baz: Do(b) -> Do; invoke t0.Foo() -> A.Foo, B.Foo
    61  // Do: (C).Foo(t4) -> C.Foo; NewB() -> NewB