]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/noder: avoid ir.Node temps in FixValue
authorMatthew Dempsky <mdempsky@google.com>
Fri, 25 Aug 2023 00:05:50 +0000 (17:05 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 25 Aug 2023 16:39:04 +0000 (16:39 +0000)
Instead of constructing an untyped basic literal IR node, having
typecheck convert it and return a new one, only to extract the
constant.Value; just have typecheck export the underlying value
conversion function, so we can call it directly.

Change-Id: Ie98f5362b3926a728d80262b0274a0b4fd023eaf
Reviewed-on: https://go-review.googlesource.com/c/go/+/522878
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Auto-Submit: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>

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

index 5349db3879b9898c02560d67a85981d877b805a4..8aa93ef5dc6d0674fa393930a503be63f522bb4b 100644 (file)
@@ -58,7 +58,7 @@ func FixValue(typ *types.Type, val constant.Value) constant.Value {
                val = constant.ToComplex(val)
        }
        if !typ.IsUntyped() {
-               val = typecheck.DefaultLit(ir.NewBasicLit(src.NoXPos, val), typ).Val()
+               val = typecheck.ConvertVal(val, typ, false)
        }
        ir.AssertValidTypeForConst(typ, val)
        return val
index 7ef913236e5f8bbd0c67bcf53b2ba9b0426db3d7..2ac489aeefc2d28fa32e165ae1e75faa3b094600 100644 (file)
@@ -113,7 +113,7 @@ func convlit1(n ir.Node, t *types.Type, explicit bool, context func() string) ir
                base.Fatalf("unexpected untyped expression: %v", n)
 
        case ir.OLITERAL:
-               v := convertVal(n.Val(), t, explicit)
+               v := ConvertVal(n.Val(), t, explicit)
                if v.Kind() == constant.Unknown {
                        n = ir.NewConstExpr(n.Val(), n)
                        break
@@ -219,12 +219,13 @@ func operandType(op ir.Op, t *types.Type) *types.Type {
        return nil
 }
 
-// convertVal converts v into a representation appropriate for t. If
-// no such representation exists, it returns Val{} instead.
+// ConvertVal converts v into a representation appropriate for t. If
+// no such representation exists, it returns constant.MakeUnknown()
+// instead.
 //
 // If explicit is true, then conversions from integer to string are
 // also allowed.
-func convertVal(v constant.Value, t *types.Type, explicit bool) constant.Value {
+func ConvertVal(v constant.Value, t *types.Type, explicit bool) constant.Value {
        switch ct := v.Kind(); ct {
        case constant.Bool:
                if t.IsBoolean() {
@@ -344,7 +345,7 @@ var overflowNames = [...]string{
 // OrigConst returns an OLITERAL with orig n and value v.
 func OrigConst(n ir.Node, v constant.Value) ir.Node {
        lno := ir.SetPos(n)
-       v = convertVal(v, n.Type(), false)
+       v = ConvertVal(v, n.Type(), false)
        base.Pos = lno
 
        switch v.Kind() {