Skip to content

Commit 972fc05

Browse files
mdempskygopherbot
authored andcommitted
cmd/compile/internal/ir: add Type param to NewBasicLit
This CL adds an explicit Type parameter to NewBasicLit so that callers can directly construct typed OLITERAL nodes. Change-Id: I0ab50ac3d7ddb7adcc903633a62ac496921165e9 Reviewed-on: https://go-review.googlesource.com/c/go/+/527096 Auto-Submit: Matthew Dempsky <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]>
1 parent e3ce312 commit 972fc05

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed

src/cmd/compile/internal/ir/const.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,44 @@ import (
1414
"cmd/internal/src"
1515
)
1616

17+
// NewBool returns an OLITERAL representing b as an untyped boolean.
1718
func NewBool(pos src.XPos, b bool) Node {
18-
return NewBasicLit(pos, constant.MakeBool(b))
19+
return NewBasicLit(pos, types.UntypedBool, constant.MakeBool(b))
1920
}
2021

22+
// NewInt returns an OLITERAL representing v as an untyped integer.
2123
func NewInt(pos src.XPos, v int64) Node {
22-
return NewBasicLit(pos, constant.MakeInt64(v))
24+
return NewBasicLit(pos, types.UntypedInt, constant.MakeInt64(v))
2325
}
2426

27+
// NewString returns an OLITERAL representing s as an untyped string.
2528
func NewString(pos src.XPos, s string) Node {
26-
return NewBasicLit(pos, constant.MakeString(s))
29+
return NewBasicLit(pos, types.UntypedString, constant.MakeString(s))
2730
}
2831

32+
// NewOne returns an OLITERAL representing 1 with the given type.
33+
func NewOne(pos src.XPos, typ *types.Type) Node {
34+
var val constant.Value
35+
switch {
36+
case typ.IsInteger():
37+
val = intOne
38+
case typ.IsFloat():
39+
val = floatOne
40+
case typ.IsComplex():
41+
val = complexOne
42+
default:
43+
base.FatalfAt(pos, "%v cannot represent 1", typ)
44+
}
45+
46+
return NewBasicLit(pos, typ, val)
47+
}
48+
49+
var (
50+
intOne = constant.MakeInt64(1)
51+
floatOne = constant.ToFloat(intOne)
52+
complexOne = constant.ToComplex(intOne)
53+
)
54+
2955
const (
3056
// Maximum size in bits for big.Ints before signaling
3157
// overflow and also mantissa precision for big.Floats.

src/cmd/compile/internal/ir/expr.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,14 @@ type BasicLit struct {
132132
val constant.Value
133133
}
134134

135-
func NewBasicLit(pos src.XPos, val constant.Value) Node {
136-
if val == nil || val.Kind() == constant.Unknown {
137-
base.FatalfAt(pos, "bad value: %v", val)
138-
}
135+
// NewBasicLit returns an OLITERAL representing val with the given type.
136+
func NewBasicLit(pos src.XPos, typ *types.Type, val constant.Value) Node {
137+
AssertValidTypeForConst(typ, val)
139138

140139
n := &BasicLit{val: val}
141140
n.op = OLITERAL
142141
n.pos = pos
143-
n.SetType(types.UntypedTypes[val.Kind()])
142+
n.SetType(typ)
144143
n.SetTypecheck(1)
145144
return n
146145
}
@@ -151,10 +150,7 @@ func (n *BasicLit) SetVal(val constant.Value) { n.val = val }
151150
// NewConstExpr returns an OLITERAL representing val, copying the
152151
// position and type from orig.
153152
func NewConstExpr(val constant.Value, orig Node) Node {
154-
n := NewBasicLit(orig.Pos(), val)
155-
n.SetType(orig.Type())
156-
n.SetTypecheck(orig.Typecheck())
157-
return n
153+
return NewBasicLit(orig.Pos(), orig.Type(), val)
158154
}
159155

160156
// A BinaryExpr is a binary expression X Op Y,

src/cmd/compile/internal/noder/helpers.go

-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ func Deref(pos src.XPos, typ *types.Type, x ir.Node) *ir.StarExpr {
7979

8080
// Statements
8181

82-
var one = constant.MakeInt64(1)
83-
8482
func idealType(tv syntax.TypeAndValue) types2.Type {
8583
// The gc backend expects all expressions to have a concrete type, and
8684
// types2 mostly satisfies this expectation already. But there are a few

src/cmd/compile/internal/noder/reader.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1760,7 +1760,7 @@ func (r *reader) stmt1(tag codeStmt, out *ir.Nodes) ir.Node {
17601760
op := r.op()
17611761
lhs := r.expr()
17621762
pos := r.pos()
1763-
n := ir.NewAssignOpStmt(pos, op, lhs, ir.NewBasicLit(pos, one))
1763+
n := ir.NewAssignOpStmt(pos, op, lhs, ir.NewOne(pos, lhs.Type()))
17641764
n.IncDec = true
17651765
return n
17661766

@@ -2176,7 +2176,7 @@ func (r *reader) expr() (res ir.Node) {
21762176
pos := r.pos()
21772177
typ := r.typ()
21782178
val := FixValue(typ, r.Value())
2179-
return typed(typ, ir.NewBasicLit(pos, val))
2179+
return ir.NewBasicLit(pos, typ, val)
21802180

21812181
case exprNil:
21822182
pos := r.pos()
@@ -3152,7 +3152,7 @@ func (r *reader) exprs() []ir.Node {
31523152
// uintptr-typed word from the dictionary parameter.
31533153
func (r *reader) dictWord(pos src.XPos, idx int) ir.Node {
31543154
base.AssertfAt(r.dictParam != nil, pos, "expected dictParam in %v", r.curfn)
3155-
return typecheck.Expr(ir.NewIndexExpr(pos, r.dictParam, ir.NewBasicLit(pos, constant.MakeInt64(int64(idx)))))
3155+
return typecheck.Expr(ir.NewIndexExpr(pos, r.dictParam, ir.NewInt(pos, int64(idx))))
31563156
}
31573157

31583158
// rttiWord is like dictWord, but converts it to *byte (the type used

src/cmd/compile/internal/walk/switch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func (s *exprSwitch) flush() {
233233
s.done.Append(ir.NewBranchStmt(pos, ir.OGOTO, endLabel))
234234

235235
// Add length case to outer switch.
236-
cas := ir.NewBasicLit(pos, constant.MakeInt64(runLen(run)))
236+
cas := ir.NewInt(pos, runLen(run))
237237
jmp := ir.NewBranchStmt(pos, ir.OGOTO, label)
238238
outer.Add(pos, cas, nil, jmp)
239239
}

0 commit comments

Comments
 (0)