"cmd/compile/internal/ir"
"cmd/compile/internal/types"
"cmd/internal/src"
+ "go/constant"
)
var basicTypes = [...]struct {
}
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
}
}
+// 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 {
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)
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
// 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 {