github.com/jd-ly/tools@v0.5.7/go/ssa/testdata/valueforexpr.go (about)

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