github.com/cockroachdb/tools@v0.0.0-20230222021103-a6d27438930d/go/ssa/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  	_ = /*@UnOp*/ (spilled)
    13  	_ = /*@Parameter*/ (unspilled)
    14  	_ = /*@nil*/ (1 + 2) // (constant)
    15  	i := 0
    16  
    17  	f := func() (int, int) { return 0, 0 }
    18  
    19  	/*@Call*/
    20  	(print( /*@BinOp*/ (i + 1)))
    21  	_, _ = /*@Call*/ (f())
    22  	ch := /*@MakeChan*/ (make(chan int))
    23  	/*@UnOp*/ (<-ch)
    24  	x := /*@UnOp*/ (<-ch)
    25  	_ = x
    26  	select {
    27  	case /*@Extract*/ (<-ch):
    28  	case x := /*@Extract*/ (<-ch):
    29  		_ = x
    30  	}
    31  	defer /*@Function*/ (func() {
    32  	})()
    33  	go /*@Function*/ (func() {
    34  	})()
    35  	y := 0
    36  	if true && /*@BinOp*/ (bool(y > 0)) {
    37  		y = 1
    38  	}
    39  	_ = /*@Phi*/ (y)
    40  	map1 := /*@MakeMap*/ (make(map[string]string))
    41  	_ = map1
    42  	_ = /*@Slice*/ (make([]int, 0))
    43  	_ = /*@MakeClosure*/ (func() { print(spilled) })
    44  
    45  	sl := []int{}
    46  	_ = /*@Slice*/ (sl[:0])
    47  
    48  	_ = /*@nil*/ (new(int)) // optimized away
    49  	tmp := /*@Alloc*/ (new(int))
    50  	_ = tmp
    51  	var iface interface{}
    52  	_ = /*@TypeAssert*/ (iface.(int))
    53  	_ = /*@UnOp*/ (sl[0])
    54  	_ = /*@IndexAddr*/ (&sl[0])
    55  	_ = /*@Index*/ ([2]int{}[0])
    56  	var p *int
    57  	_ = /*@UnOp*/ (*p)
    58  
    59  	_ = /*@UnOp*/ (global)
    60  	/*@UnOp*/ (global)[""] = ""
    61  	/*@Global*/ (global) = map[string]string{}
    62  
    63  	var local t
    64  	/*UnOp*/ (local.x) = 1
    65  
    66  	// Exercise corner-cases of lvalues vs rvalues.
    67  	type N *N
    68  	var n N
    69  	/*@UnOp*/ (n) = /*@UnOp*/ (n)
    70  	/*@ChangeType*/ (n) = /*@Alloc*/ (&n)
    71  	/*@UnOp*/ (n) = /*@UnOp*/ (*n)
    72  	/*@UnOp*/ (n) = /*@UnOp*/ (**n)
    73  }
    74  
    75  func complit() {
    76  	// Composite literals.
    77  	// We get different results for
    78  	// - composite literal as value (e.g. operand to print)
    79  	// - composite literal initializer for addressable value
    80  	// - composite literal value assigned to blank var
    81  
    82  	// 1. Slices
    83  	print( /*@Slice*/ ([]int{}))
    84  	print( /*@Alloc*/ (&[]int{}))
    85  	print(& /*@Slice*/ ([]int{}))
    86  
    87  	sl1 := /*@Slice*/ ([]int{})
    88  	sl2 := /*@Alloc*/ (&[]int{})
    89  	sl3 := & /*@Slice*/ ([]int{})
    90  	_, _, _ = sl1, sl2, sl3
    91  
    92  	_ = /*@Slice*/ ([]int{})
    93  	_ = /*@nil*/ (& /*@Slice*/ ([]int{})) // & optimized away
    94  	_ = & /*@Slice*/ ([]int{})
    95  
    96  	// 2. Arrays
    97  	print( /*@UnOp*/ ([1]int{}))
    98  	print( /*@Alloc*/ (&[1]int{}))
    99  	print(& /*@Alloc*/ ([1]int{}))
   100  
   101  	arr1 := /*@Alloc*/ ([1]int{})
   102  	arr2 := /*@Alloc*/ (&[1]int{})
   103  	arr3 := & /*@Alloc*/ ([1]int{})
   104  	_, _, _ = arr1, arr2, arr3
   105  
   106  	_ = /*@UnOp*/ ([1]int{})
   107  	_ = /*@Alloc*/ (& /*@Alloc*/ ([1]int{}))
   108  	_ = & /*@Alloc*/ ([1]int{})
   109  
   110  	// 3. Maps
   111  	type M map[int]int
   112  	print( /*@MakeMap*/ (M{}))
   113  	print( /*@Alloc*/ (&M{}))
   114  	print(& /*@MakeMap*/ (M{}))
   115  
   116  	m1 := /*@MakeMap*/ (M{})
   117  	m2 := /*@Alloc*/ (&M{})
   118  	m3 := & /*@MakeMap*/ (M{})
   119  	_, _, _ = m1, m2, m3
   120  
   121  	_ = /*@MakeMap*/ (M{})
   122  	_ = /*@nil*/ (& /*@MakeMap*/ (M{})) // & optimized away
   123  	_ = & /*@MakeMap*/ (M{})
   124  
   125  	// 4. Structs
   126  	print( /*@UnOp*/ (struct{}{}))
   127  	print( /*@Alloc*/ (&struct{}{}))
   128  	print(& /*@Alloc*/ (struct{}{}))
   129  
   130  	s1 := /*@Alloc*/ (struct{}{})
   131  	s2 := /*@Alloc*/ (&struct{}{})
   132  	s3 := & /*@Alloc*/ (struct{}{})
   133  	_, _, _ = s1, s2, s3
   134  
   135  	_ = /*@UnOp*/ (struct{}{})
   136  	_ = /*@Alloc*/ (& /*@Alloc*/ (struct{}{}))
   137  	_ = & /*@Alloc*/ (struct{}{})
   138  }
   139  
   140  type t struct{ x int }
   141  
   142  // Ensure we can locate methods of named types.
   143  func (t) f(param int) {
   144  	_ = /*@Parameter*/ (param)
   145  }
   146  
   147  // Ensure we can locate init functions.
   148  func init() {
   149  	m := /*@MakeMap*/ (make(map[string]string))
   150  	_ = m
   151  }
   152  
   153  // Ensure we can locate variables in initializer expressions.
   154  var global = /*@MakeMap*/ (make(map[string]string))