github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/go/ir/testdata/valueforexpr.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  package main
     5  
     6  // This file is the input to TestValueForExpr in source_test.go, which
     7  // ensures that each expression e immediately following a /*@kind*/(x)
     8  // annotation, when passed to Function.ValueForExpr(e), returns a
     9  // non-nil Value of the same type as e and of kind 'kind'.
    10  
    11  func f(spilled, unspilled int) {
    12  	_ = /*@Parameter*/ (spilled)
    13  	_ = /*@Parameter*/ (unspilled)
    14  	_ = /*@nil*/ (1 + 2) // (constant)
    15  	i := 0
    16  
    17  	f := func() (int, int) { return 0, 0 }
    18  
    19  	(print( /*@BinOp*/ (i + 1)))
    20  	_, _ = /*@Call*/ (f())
    21  	ch := /*@MakeChan*/ (make(chan int))
    22  	/*@Recv*/ (<-ch)
    23  	x := /*@Recv*/ (<-ch)
    24  	_ = x
    25  	select {
    26  	case /*@Extract*/ (<-ch):
    27  	case x := /*@Extract*/ (<-ch):
    28  		_ = x
    29  	}
    30  	defer /*@Function*/ (func() {
    31  	})()
    32  	go /*@Function*/ (func() {
    33  	})()
    34  	y := 0
    35  	if true && /*@BinOp*/ (bool(y > 0)) {
    36  		y = 1
    37  	}
    38  	_ = /*@Phi*/ (y)
    39  	map1 := /*@MakeMap*/ (make(map[string]string))
    40  	_ = map1
    41  	_ = /*@Slice*/ (make([]int, 0))
    42  	_ = /*@MakeClosure*/ (func() { print(spilled) })
    43  
    44  	_ = /*@Load*/ (spilled)
    45  
    46  	sl := []int{}
    47  	_ = /*@Slice*/ (sl[:0])
    48  
    49  	_ = /*@Alloc*/ (new(int))
    50  	tmp := /*@Alloc*/ (new(int))
    51  	_ = tmp
    52  	var iface interface{}
    53  	_ = /*@TypeAssert*/ (iface.(int))
    54  	_ = /*@Load*/ (sl[0])
    55  	_ = /*@IndexAddr*/ (&sl[0])
    56  	_ = /*@Index*/ ([2]int{}[0])
    57  	var p *int
    58  	_ = /*@Load*/ (*p)
    59  
    60  	_ = /*@Load*/ (global)
    61  	/*@Load*/ (global)[""] = ""
    62  	/*@Global*/ (global) = map[string]string{}
    63  
    64  	var local t
    65  	/*UnOp*/ (local.x) = 1
    66  
    67  	// Exercise corner-cases of lvalues vs rvalues.
    68  	type N *N
    69  	var n N
    70  	/*@Const*/ (n) = /*@Const*/ (n)
    71  	/*@ChangeType*/ (n) = /*@Alloc*/ (&n)
    72  	/*@Load*/ (n) = /*@Load*/ (n)
    73  	/*@Load*/ (n) = /*@Load*/ (*n)
    74  	/*@Load*/ (n) = /*@Load*/ (**n)
    75  }
    76  
    77  func complit() {
    78  	// Composite literals.
    79  	// We get different results for
    80  	// - composite literal as value (e.g. operand to print)
    81  	// - composite literal initializer for addressable value
    82  	// - composite literal value assigned to blank var
    83  
    84  	// 1. Slices
    85  	print( /*@Slice*/ ([]int{}))
    86  	print( /*@Alloc*/ (&[]int{}))
    87  	print(& /*@Slice*/ ([]int{}))
    88  
    89  	sl1 := /*@Slice*/ ([]int{})
    90  	sl2 := /*@Alloc*/ (&[]int{})
    91  	sl3 := & /*@Slice*/ ([]int{})
    92  	_, _, _ = sl1, sl2, sl3
    93  
    94  	_ = /*@Slice*/ ([]int{})
    95  	_ = /*@Alloc*/ (& /*@Slice*/ ([]int{}))
    96  	_ = & /*@Slice*/ ([]int{})
    97  
    98  	// 2. Arrays
    99  	print( /*@ArrayConst*/ ([1]int{}))
   100  	print( /*@Alloc*/ (&[1]int{}))
   101  	print(& /*@Alloc*/ ([1]int{}))
   102  
   103  	arr1 := /*@ArrayConst*/ ([1]int{})
   104  	arr2 := /*@Alloc*/ (&[1]int{})
   105  	arr3 := & /*@Alloc*/ ([1]int{})
   106  	_, _, _ = arr1, arr2, arr3
   107  
   108  	_ = /*@ArrayConst*/ ([1]int{})
   109  	_ = /*@Alloc*/ (& /*@Alloc*/ ([1]int{}))
   110  	_ = & /*@Alloc*/ ([1]int{})
   111  
   112  	// 3. Maps
   113  	type M map[int]int
   114  	print( /*@MakeMap*/ (M{}))
   115  	print( /*@Alloc*/ (&M{}))
   116  	print(& /*@MakeMap*/ (M{}))
   117  
   118  	m1 := /*@MakeMap*/ (M{})
   119  	m2 := /*@Alloc*/ (&M{})
   120  	m3 := & /*@MakeMap*/ (M{})
   121  	_, _, _ = m1, m2, m3
   122  
   123  	_ = /*@MakeMap*/ (M{})
   124  	_ = /*@Alloc*/ (& /*@MakeMap*/ (M{}))
   125  	_ = & /*@MakeMap*/ (M{})
   126  
   127  	// 4. Structs
   128  	print( /*@AggregateConst*/ (struct{}{}))
   129  	print( /*@Alloc*/ (&struct{}{}))
   130  	print(& /*@Alloc*/ (struct{}{}))
   131  
   132  	s1 := /*@AggregateConst*/ (struct{}{})
   133  	s2 := /*@Alloc*/ (&struct{}{})
   134  	s3 := & /*@Alloc*/ (struct{}{})
   135  	_, _, _ = s1, s2, s3
   136  
   137  	_ = /*@AggregateConst*/ (struct{}{})
   138  	_ = /*@Alloc*/ (& /*@Alloc*/ (struct{}{}))
   139  	_ = & /*@Alloc*/ (struct{}{})
   140  }
   141  
   142  type t struct{ x int }
   143  
   144  // Ensure we can locate methods of named types.
   145  func (t) f(param int) {
   146  	_ = /*@Parameter*/ (param)
   147  }
   148  
   149  // Ensure we can locate init functions.
   150  func init() {
   151  	m := /*@MakeMap*/ (make(map[string]string))
   152  	_ = m
   153  }
   154  
   155  // Ensure we can locate variables in initializer expressions.
   156  var global = /*@MakeMap*/ (make(map[string]string))
   157  
   158  type t1 struct {
   159  	x int
   160  }
   161  type t2 struct {
   162  	x int `tag`
   163  }
   164  
   165  func main() {
   166  	var tv1 t1
   167  	var tv2 t2 = /*@ChangeType*/ (t2(tv1))
   168  	_ = tv2
   169  }