]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.regabi] cmd/compile: add ir.BasicLit to represent literals
authorMatthew Dempsky <mdempsky@google.com>
Fri, 18 Dec 2020 04:17:04 +0000 (20:17 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Tue, 22 Dec 2020 17:04:46 +0000 (17:04 +0000)
This CL changes so that all literals are represented with a new,
smaller ir.BasicLit type, so that ir.Name is only used to represent
declared constants.

Passes buildall w/ toolstash -cmp.

Change-Id: I4702b8e3fa945617bd05881d7a2be1205f229633
Reviewed-on: https://go-review.googlesource.com/c/go/+/279153
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
src/cmd/compile/internal/gc/universe.go
src/cmd/compile/internal/ir/expr.go
src/cmd/compile/internal/ir/name.go
src/cmd/compile/internal/ir/node_gen.go
src/cmd/compile/internal/ir/val.go

index c988c575dcd6e004c8c58c79ec4d7a84380146d9..e11c0eb92c7733627b0de306d2547da45bdbec70 100644 (file)
@@ -11,6 +11,7 @@ import (
        "cmd/compile/internal/ir"
        "cmd/compile/internal/types"
        "cmd/internal/src"
+       "go/constant"
 )
 
 var basicTypes = [...]struct {
@@ -163,14 +164,10 @@ func initUniverse() {
        }
 
        s = types.BuiltinPkg.Lookup("true")
-       b := nodbool(true)
-       b.(*ir.Name).SetSym(lookup("true"))
-       s.Def = b
+       s.Def = ir.NewConstAt(src.NoXPos, s, types.UntypedBool, constant.MakeBool(true))
 
        s = types.BuiltinPkg.Lookup("false")
-       b = nodbool(false)
-       b.(*ir.Name).SetSym(lookup("false"))
-       s.Def = b
+       s.Def = ir.NewConstAt(src.NoXPos, s, types.UntypedBool, constant.MakeBool(false))
 
        s = lookup("_")
        types.BlankSym = s
index d74e7f8763d145dfd98051988b7e56bbf1cae09b..5937798bd4be2509ded41feaeb12f371eabd3d62 100644 (file)
@@ -136,6 +136,25 @@ func (n *AddrExpr) SetOp(op Op) {
        }
 }
 
+// A BasicLit is a literal of basic type.
+type BasicLit struct {
+       miniExpr
+       val constant.Value
+}
+
+func NewBasicLit(pos src.XPos, val constant.Value) Node {
+       n := &BasicLit{val: val}
+       n.op = OLITERAL
+       n.pos = pos
+       if k := val.Kind(); k != constant.Unknown {
+               n.SetType(idealType(k))
+       }
+       return n
+}
+
+func (n *BasicLit) Val() constant.Value       { return n.val }
+func (n *BasicLit) SetVal(val constant.Value) { n.val = val }
+
 // A BinaryExpr is a binary expression X Op Y,
 // or Op(X, Y) for builtin functions that do not become calls.
 type BinaryExpr struct {
index 9cf959b23d7924e1e4e2eec26569f5af863d1bed..b0b33cccfaefba5357e97ff8a7976574af3c20fd 100644 (file)
@@ -179,6 +179,17 @@ func NewDeclNameAt(pos src.XPos, op Op, sym *types.Sym) *Name {
        return newNameAt(pos, op, sym)
 }
 
+// NewConstAt returns a new OLITERAL Node associated with symbol s at position pos.
+func NewConstAt(pos src.XPos, sym *types.Sym, typ *types.Type, val constant.Value) *Name {
+       if sym == nil {
+               base.Fatalf("NewConstAt nil")
+       }
+       n := newNameAt(pos, OLITERAL, sym)
+       n.SetType(typ)
+       n.SetVal(val)
+       return n
+}
+
 // newNameAt is like NewNameAt but allows sym == nil.
 func newNameAt(pos src.XPos, op Op, sym *types.Sym) *Name {
        n := new(Name)
index a0fae2b94960451b5ba375902c953b44572c9cd6..a5959ea26f39e8c99852dd52c38d8bb6faf84b4f 100644 (file)
@@ -116,6 +116,21 @@ func (n *AssignStmt) editChildren(edit func(Node) Node) {
        n.Y = maybeEdit(n.Y, edit)
 }
 
+func (n *BasicLit) Format(s fmt.State, verb rune) { FmtNode(n, s, verb) }
+func (n *BasicLit) copy() Node {
+       c := *n
+       c.init = c.init.Copy()
+       return &c
+}
+func (n *BasicLit) doChildren(do func(Node) error) error {
+       var err error
+       err = maybeDoList(n.init, err, do)
+       return err
+}
+func (n *BasicLit) editChildren(edit func(Node) Node) {
+       editList(n.init, edit)
+}
+
 func (n *BinaryExpr) Format(s fmt.State, verb rune) { FmtNode(n, s, verb) }
 func (n *BinaryExpr) copy() Node {
        c := *n
index 5b0506c0d04fcb2d80ac7d2b787180e39e243da0..ff45f31074e1c39b9d1509407edafd4695f68948 100644 (file)
@@ -92,12 +92,7 @@ func ValidTypeForConst(t *types.Type, v constant.Value) bool {
 
 // nodlit returns a new untyped constant with value v.
 func NewLiteral(v constant.Value) Node {
-       n := newNameAt(base.Pos, OLITERAL, nil)
-       if k := v.Kind(); k != constant.Unknown {
-               n.SetType(idealType(k))
-               n.SetVal(v)
-       }
-       return n
+       return NewBasicLit(base.Pos, v)
 }
 
 func idealType(ct constant.Kind) *types.Type {