Debian Codegraph @

go/ir/testdata/valueforexpr.go

raw
1 package main
2
3 // This file is the input to TestValueForExpr in source_test.go, which
4 // ensures that each expression e immediately following a /*@kind*/(x)
5 // annotation, when passed to Function.ValueForExpr(e), returns a
6 // non-nil Value of the same type as e and of kind 'kind'.
7
8 func f(spilled, unspilled int) {
9 _ = /*@Parameter*/ (spilled)
10 _ = /*@Parameter*/ (unspilled)
11 _ = /*@nil*/ (1 + 2) // (constant)
12 i := 0
13
14 f := func() (int, int) { return 0, 0 }
15
16 /*@Call*/
17 (print( /*@BinOp*/ (i + 1)))
18 _, _ = /*@Call*/ (f())
19 ch := /*@MakeChan*/ (make(chan int))
20 /*@Recv*/ (<-ch)
21 x := /*@Recv*/ (<-ch)
22 _ = x
23 select {
24 case /*@Extract*/ (<-ch):
25 case x := /*@Extract*/ (<-ch):
26 _ = x
27 }
28 defer /*@Function*/ (func() {
29 })()
30 go /*@Function*/ (func() {
31 })()
32 y := 0
33 if true && /*@BinOp*/ (bool(y > 0)) {
34 y = 1
35 }
36 _ = /*@Phi*/ (y)
37 map1 := /*@MakeMap*/ (make(map[string]string))
38 _ = map1
39 _ = /*@Slice*/ (make([]int, 0))
40 _ = /*@MakeClosure*/ (func() { print(spilled) })
41
42 _ = /*@Load*/ (spilled)
43
44 sl := []int{}
45 _ = /*@Slice*/ (sl[:0])
46
47 _ = /*@Alloc*/ (new(int))
48 tmp := /*@Alloc*/ (new(int))
49 _ = tmp
50 var iface interface{}
51 _ = /*@TypeAssert*/ (iface.(int))
52 _ = /*@Load*/ (sl[0])
53 _ = /*@IndexAddr*/ (&sl[0])
54 _ = /*@Index*/ ([2]int{}[0])
55 var p *int
56 _ = /*@Load*/ (*p)
57
58 _ = /*@Load*/ (global)
59 /*@Load*/ (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 /*@Const*/ (n) = /*@Const*/ (n)
69 /*@ChangeType*/ (n) = /*@Alloc*/ (&n)
70 /*@Load*/ (n) = /*@Load*/ n
71 /*@Load*/ (n) = /*@Load*/ (*n)
72 /*@Load*/ (n) = /*@Load*/ (**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 _ = /*@Alloc*/ (& /*@Slice*/ ([]int{}))
94 _ = & /*@Slice*/ ([]int{})
95
96 // 2. Arrays
97 print( /*@Const*/ ([1]int{}))
98 print( /*@Alloc*/ (&[1]int{}))
99 print(& /*@Alloc*/ ([1]int{}))
100
101 arr1 := /*@Const*/ ([1]int{})
102 arr2 := /*@Alloc*/ (&[1]int{})
103 arr3 := & /*@Alloc*/ ([1]int{})
104 _, _, _ = arr1, arr2, arr3
105
106 _ = /*@Const*/ ([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 _ = /*@Alloc*/ (& /*@MakeMap*/ (M{}))
123 _ = & /*@MakeMap*/ (M{})
124
125 // 4. Structs
126 print( /*@Const*/ (struct{}{}))
127 print( /*@Alloc*/ (&struct{}{}))
128 print(& /*@Alloc*/ (struct{}{}))
129
130 s1 := /*@Const*/ (struct{}{})
131 s2 := /*@Alloc*/ (&struct{}{})
132 s3 := & /*@Alloc*/ (struct{}{})
133 _, _, _ = s1, s2, s3
134
135 _ = /*@Const*/ (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))
155
156 type t1 struct {
157 x int
158 }
159 type t2 struct {
160 x int `tag`
161 }
162
163 func main() {
164 var tv1 t1
165 var tv2 t2 = /*@ChangeType*/ (t2(tv1))
166 _ = tv2
167 }