mirror of
https://github.com/golang/go
synced 2024-11-23 16:10:05 -07:00
[dev.regabi] cmd/compile: add ir.BasicLit to represent literals
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>
This commit is contained in:
parent
3512cde10a
commit
c8610e4700
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user