]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/ir: add Type param to NewBasicLit
authorMatthew Dempsky <mdempsky@google.com>
Fri, 8 Sep 2023 22:44:57 +0000 (15:44 -0700)
committerGopher Robot <gobot@golang.org>
Mon, 11 Sep 2023 16:04:33 +0000 (16:04 +0000)
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 <mdempsky@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
src/cmd/compile/internal/ir/const.go
src/cmd/compile/internal/ir/expr.go
src/cmd/compile/internal/noder/helpers.go
src/cmd/compile/internal/noder/reader.go
src/cmd/compile/internal/walk/switch.go

index 751620f26a3b01f3e3f3c6f0c18ab0988fc31761..74e55511e48511c934ee23c8b5e4b0dfc353cbd5 100644 (file)
@@ -14,18 +14,44 @@ import (
        "cmd/internal/src"
 )
 
+// NewBool returns an OLITERAL representing b as an untyped boolean.
 func NewBool(pos src.XPos, b bool) Node {
-       return NewBasicLit(pos, constant.MakeBool(b))
+       return NewBasicLit(pos, types.UntypedBool, constant.MakeBool(b))
 }
 
+// NewInt returns an OLITERAL representing v as an untyped integer.
 func NewInt(pos src.XPos, v int64) Node {
-       return NewBasicLit(pos, constant.MakeInt64(v))
+       return NewBasicLit(pos, types.UntypedInt, constant.MakeInt64(v))
 }
 
+// NewString returns an OLITERAL representing s as an untyped string.
 func NewString(pos src.XPos, s string) Node {
-       return NewBasicLit(pos, constant.MakeString(s))
+       return NewBasicLit(pos, types.UntypedString, constant.MakeString(s))
 }
 
+// NewOne returns an OLITERAL representing 1 with the given type.
+func NewOne(pos src.XPos, typ *types.Type) Node {
+       var val constant.Value
+       switch {
+       case typ.IsInteger():
+               val = intOne
+       case typ.IsFloat():
+               val = floatOne
+       case typ.IsComplex():
+               val = complexOne
+       default:
+               base.FatalfAt(pos, "%v cannot represent 1", typ)
+       }
+
+       return NewBasicLit(pos, typ, val)
+}
+
+var (
+       intOne     = constant.MakeInt64(1)
+       floatOne   = constant.ToFloat(intOne)
+       complexOne = constant.ToComplex(intOne)
+)
+
 const (
        // Maximum size in bits for big.Ints before signaling
        // overflow and also mantissa precision for big.Floats.
index abea185dd3fcec3ecd669ee79c70485592872ee9..676045d27ade4e1ec25c60ef3ebbb8815d5bb553 100644 (file)
@@ -132,15 +132,14 @@ type BasicLit struct {
        val constant.Value
 }
 
-func NewBasicLit(pos src.XPos, val constant.Value) Node {
-       if val == nil || val.Kind() == constant.Unknown {
-               base.FatalfAt(pos, "bad value: %v", val)
-       }
+// NewBasicLit returns an OLITERAL representing val with the given type.
+func NewBasicLit(pos src.XPos, typ *types.Type, val constant.Value) Node {
+       AssertValidTypeForConst(typ, val)
 
        n := &BasicLit{val: val}
        n.op = OLITERAL
        n.pos = pos
-       n.SetType(types.UntypedTypes[val.Kind()])
+       n.SetType(typ)
        n.SetTypecheck(1)
        return n
 }
@@ -151,10 +150,7 @@ func (n *BasicLit) SetVal(val constant.Value) { n.val = val }
 // NewConstExpr returns an OLITERAL representing val, copying the
 // position and type from orig.
 func NewConstExpr(val constant.Value, orig Node) Node {
-       n := NewBasicLit(orig.Pos(), val)
-       n.SetType(orig.Type())
-       n.SetTypecheck(orig.Typecheck())
-       return n
+       return NewBasicLit(orig.Pos(), orig.Type(), val)
 }
 
 // A BinaryExpr is a binary expression X Op Y,
index ae31f8600637e93f2892f44214e7dd8f313c8335..05a57d07f049e061d10af27d34b34b4c0fdff861 100644 (file)
@@ -79,8 +79,6 @@ func Deref(pos src.XPos, typ *types.Type, x ir.Node) *ir.StarExpr {
 
 // Statements
 
-var one = constant.MakeInt64(1)
-
 func idealType(tv syntax.TypeAndValue) types2.Type {
        // The gc backend expects all expressions to have a concrete type, and
        // types2 mostly satisfies this expectation already. But there are a few
index 35dfe3d674c34cb3df440f935efd5cfa72d861fa..4b26eb466880216007ff041ab264d2c6db3f7279 100644 (file)
@@ -1760,7 +1760,7 @@ func (r *reader) stmt1(tag codeStmt, out *ir.Nodes) ir.Node {
                op := r.op()
                lhs := r.expr()
                pos := r.pos()
-               n := ir.NewAssignOpStmt(pos, op, lhs, ir.NewBasicLit(pos, one))
+               n := ir.NewAssignOpStmt(pos, op, lhs, ir.NewOne(pos, lhs.Type()))
                n.IncDec = true
                return n
 
@@ -2176,7 +2176,7 @@ func (r *reader) expr() (res ir.Node) {
                pos := r.pos()
                typ := r.typ()
                val := FixValue(typ, r.Value())
-               return typed(typ, ir.NewBasicLit(pos, val))
+               return ir.NewBasicLit(pos, typ, val)
 
        case exprNil:
                pos := r.pos()
@@ -3152,7 +3152,7 @@ func (r *reader) exprs() []ir.Node {
 // uintptr-typed word from the dictionary parameter.
 func (r *reader) dictWord(pos src.XPos, idx int) ir.Node {
        base.AssertfAt(r.dictParam != nil, pos, "expected dictParam in %v", r.curfn)
-       return typecheck.Expr(ir.NewIndexExpr(pos, r.dictParam, ir.NewBasicLit(pos, constant.MakeInt64(int64(idx)))))
+       return typecheck.Expr(ir.NewIndexExpr(pos, r.dictParam, ir.NewInt(pos, int64(idx))))
 }
 
 // rttiWord is like dictWord, but converts it to *byte (the type used
index 67ccb2e5d166748ed6e8c24b69f6ee2374fbc470..2fc8aefe5fa7e34f8ae90d4ac8633937ddfc9fd4 100644 (file)
@@ -233,7 +233,7 @@ func (s *exprSwitch) flush() {
                        s.done.Append(ir.NewBranchStmt(pos, ir.OGOTO, endLabel))
 
                        // Add length case to outer switch.
-                       cas := ir.NewBasicLit(pos, constant.MakeInt64(runLen(run)))
+                       cas := ir.NewInt(pos, runLen(run))
                        jmp := ir.NewBranchStmt(pos, ir.OGOTO, label)
                        outer.Add(pos, cas, nil, jmp)
                }