1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package noder
import (
"go/constant"
"cmd/compile/internal/base"
"cmd/compile/internal/ir"
"cmd/compile/internal/typecheck"
"cmd/compile/internal/types"
"cmd/internal/src"
)
// Helpers for constructing typed IR nodes.
//
// TODO(mdempsky): Move into their own package so they can be easily
// reused by iimport and frontend optimizations.
type ImplicitNode interface {
ir.Node
SetImplicit(x bool)
}
// Implicit returns n after marking it as Implicit.
func Implicit(n ImplicitNode) ImplicitNode {
n.SetImplicit(true)
return n
}
// typed returns n after setting its type to typ.
func typed(typ *types.Type, n ir.Node) ir.Node {
n.SetType(typ)
n.SetTypecheck(1)
return n
}
// Values
func Const(pos src.XPos, typ *types.Type, val constant.Value) ir.Node {
return typed(typ, ir.NewBasicLit(pos, val))
}
func OrigConst(pos src.XPos, typ *types.Type, val constant.Value, op ir.Op, raw string) ir.Node {
orig := ir.NewRawOrigExpr(pos, op, raw)
return ir.NewConstExpr(val, typed(typ, orig))
}
// FixValue returns val after converting and truncating it as
// appropriate for typ.
func FixValue(typ *types.Type, val constant.Value) constant.Value {
assert(typ.Kind() != types.TFORW)
switch {
case typ.IsInteger():
val = constant.ToInt(val)
case typ.IsFloat():
val = constant.ToFloat(val)
case typ.IsComplex():
val = constant.ToComplex(val)
}
if !typ.IsUntyped() {
val = typecheck.DefaultLit(ir.NewBasicLit(src.NoXPos, val), typ).Val()
}
if !typ.IsTypeParam() {
ir.AssertValidTypeForConst(typ, val)
}
return val
}
func Nil(pos src.XPos, typ *types.Type) ir.Node {
return typed(typ, ir.NewNilExpr(pos))
}
// Expressions
func Addr(pos src.XPos, x ir.Node) *ir.AddrExpr {
n := typecheck.NodAddrAt(pos, x)
typed(types.NewPtr(x.Type()), n)
return n
}
func Assert(pos src.XPos, x ir.Node, typ *types.Type) ir.Node {
return typed(typ, ir.NewTypeAssertExpr(pos, x, nil))
}
func Binary(pos src.XPos, op ir.Op, typ *types.Type, x, y ir.Node) *ir.BinaryExpr {
switch op {
case ir.OADD:
n := ir.NewBinaryExpr(pos, op, x, y)
typed(typ, n)
return n
default:
n := ir.NewBinaryExpr(pos, op, x, y)
typed(x.Type(), n)
return n
}
}
func Compare(pos src.XPos, typ *types.Type, op ir.Op, x, y ir.Node) *ir.BinaryExpr {
n := ir.NewBinaryExpr(pos, op, x, y)
typed(typ, n)
return n
}
func Deref(pos src.XPos, typ *types.Type, x ir.Node) *ir.StarExpr {
n := ir.NewStarExpr(pos, x)
typed(typ, n)
return n
}
func DotField(pos src.XPos, x ir.Node, index int) *ir.SelectorExpr {
op, typ := ir.ODOT, x.Type()
if typ.IsPtr() {
op, typ = ir.ODOTPTR, typ.Elem()
}
if !typ.IsStruct() {
base.FatalfAt(pos, "DotField of non-struct: %L", x)
}
// TODO(mdempsky): This is the backend's responsibility.
types.CalcSize(typ)
field := typ.Field(index)
return dot(pos, field.Type, op, x, field)
}
func DotMethod(pos src.XPos, x ir.Node, index int) *ir.SelectorExpr {
method := method(x.Type(), index)
// Method value.
typ := typecheck.NewMethodType(method.Type, nil)
return dot(pos, typ, ir.OMETHVALUE, x, method)
}
// MethodExpr returns a OMETHEXPR node with the indicated index into the methods
// of typ. The receiver type is set from recv, which is different from typ if the
// method was accessed via embedded fields. Similarly, the X value of the
// ir.SelectorExpr is recv, the original OTYPE node before passing through the
// embedded fields.
func MethodExpr(pos src.XPos, recv ir.Node, embed *types.Type, index int) *ir.SelectorExpr {
method := method(embed, index)
typ := typecheck.NewMethodType(method.Type, recv.Type())
// The method expression T.m requires a wrapper when T
// is different from m's declared receiver type. We
// normally generate these wrappers while writing out
// runtime type descriptors, which is always done for
// types declared at package scope. However, we need
// to make sure to generate wrappers for anonymous
// receiver types too.
if recv.Sym() == nil {
typecheck.NeedRuntimeType(recv.Type())
}
return dot(pos, typ, ir.OMETHEXPR, recv, method)
}
func dot(pos src.XPos, typ *types.Type, op ir.Op, x ir.Node, selection *types.Field) *ir.SelectorExpr {
n := ir.NewSelectorExpr(pos, op, x, selection.Sym)
n.Selection = selection
typed(typ, n)
return n
}
// TODO(mdempsky): Move to package types.
func method(typ *types.Type, index int) *types.Field {
if typ.IsInterface() {
return typ.AllMethods().Index(index)
}
return types.ReceiverBaseType(typ).Methods().Index(index)
}
func Index(pos src.XPos, typ *types.Type, x, index ir.Node) *ir.IndexExpr {
n := ir.NewIndexExpr(pos, x, index)
typed(typ, n)
return n
}
func Slice(pos src.XPos, typ *types.Type, x, low, high, max ir.Node) *ir.SliceExpr {
op := ir.OSLICE
if max != nil {
op = ir.OSLICE3
}
n := ir.NewSliceExpr(pos, op, x, low, high, max)
typed(typ, n)
return n
}
func Unary(pos src.XPos, typ *types.Type, op ir.Op, x ir.Node) ir.Node {
switch op {
case ir.OADDR:
return Addr(pos, x)
case ir.ODEREF:
return Deref(pos, typ, x)
}
if op == ir.ORECV {
if typ.IsFuncArgStruct() && typ.NumFields() == 2 {
// Remove the second boolean type (if provided by type2),
// since that works better with the rest of the compiler
// (which will add it back in later).
assert(typ.Field(1).Type.Kind() == types.TBOOL)
typ = typ.Field(0).Type
}
}
return typed(typ, ir.NewUnaryExpr(pos, op, x))
}
// Statements
var one = constant.MakeInt64(1)
func IncDec(pos src.XPos, op ir.Op, x ir.Node) *ir.AssignOpStmt {
assert(x.Type() != nil)
bl := ir.NewBasicLit(pos, one)
if x.Type().HasTParam() {
// If the operand is generic, then types2 will have proved it must be
// a type that fits with increment/decrement, so just set the type of
// "one" to n.Type(). This works even for types that are eventually
// float or complex.
typed(x.Type(), bl)
} else {
bl = typecheck.DefaultLit(bl, x.Type())
}
return ir.NewAssignOpStmt(pos, op, x, bl)
}
|