diff options
author | Matthew Dempsky <mdempsky@google.com> | 2021-05-27 02:50:17 -0700 |
---|---|---|
committer | Matthew Dempsky <mdempsky@google.com> | 2021-05-27 22:13:46 +0000 |
commit | ea522bc546bb9f66285ea00744de8b258368b3ed (patch) | |
tree | f7ad88506a7f8a9e9220bf830061a1f7b295430e /src/cmd/compile/internal/noder/helpers.go | |
parent | de5d1aca5e61e49e0704213961c68bcf14e288b8 (diff) | |
download | go-ea522bc546bb9f66285ea00744de8b258368b3ed.tar.gz go-ea522bc546bb9f66285ea00744de8b258368b3ed.zip |
[dev.typeparams] cmd/compile: add and use ir.RawOrigExpr
This CL adds ir.RawOrigExpr, which can be used to represent arbitrary
constant expressions without needing to build and carry around an
entire IR representation of the original expression. It also allows
distinguishing how the constant was originally written by the
user (e.g., "0xff" vs "255").
This CL then also updates irgen to make use of this functionality for
expressions that were constant folded by types2.
Change-Id: I41e04e228e715ae2735c357b75633a2d08ee7021
Reviewed-on: https://go-review.googlesource.com/c/go/+/323210
Trust: Matthew Dempsky <mdempsky@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
Diffstat (limited to 'src/cmd/compile/internal/noder/helpers.go')
-rw-r--r-- | src/cmd/compile/internal/noder/helpers.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/noder/helpers.go b/src/cmd/compile/internal/noder/helpers.go index 9da0e49300..ea30a3bfa9 100644 --- a/src/cmd/compile/internal/noder/helpers.go +++ b/src/cmd/compile/internal/noder/helpers.go @@ -43,6 +43,32 @@ 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.Kind() != types.TTYPEPARAM { + ir.AssertValidTypeForConst(typ, val) + } + return val +} + func Nil(pos src.XPos, typ *types.Type) ir.Node { return typed(typ, ir.NewNilExpr(pos)) } |