"cmd/internal/src"
)
+// NewBool returns an OLITERAL representing b as an untyped boolean.
func NewBool(pos src.XPos, b bool) Node {
- return NewBasicLit(pos, constant.MakeBool(b))
+ return NewBasicLit(pos, types.UntypedBool, constant.MakeBool(b))
}
+// NewInt returns an OLITERAL representing v as an untyped integer.
func NewInt(pos src.XPos, v int64) Node {
- return NewBasicLit(pos, constant.MakeInt64(v))
+ return NewBasicLit(pos, types.UntypedInt, constant.MakeInt64(v))
}
+// NewString returns an OLITERAL representing s as an untyped string.
func NewString(pos src.XPos, s string) Node {
- return NewBasicLit(pos, constant.MakeString(s))
+ return NewBasicLit(pos, types.UntypedString, constant.MakeString(s))
}
+// NewOne returns an OLITERAL representing 1 with the given type.
+func NewOne(pos src.XPos, typ *types.Type) Node {
+ var val constant.Value
+ switch {
+ case typ.IsInteger():
+ val = intOne
+ case typ.IsFloat():
+ val = floatOne
+ case typ.IsComplex():
+ val = complexOne
+ default:
+ base.FatalfAt(pos, "%v cannot represent 1", typ)
+ }
+
+ return NewBasicLit(pos, typ, val)
+}
+
+var (
+ intOne = constant.MakeInt64(1)
+ floatOne = constant.ToFloat(intOne)
+ complexOne = constant.ToComplex(intOne)
+)
+
const (
// Maximum size in bits for big.Ints before signaling
// overflow and also mantissa precision for big.Floats.
val constant.Value
}
-func NewBasicLit(pos src.XPos, val constant.Value) Node {
- if val == nil || val.Kind() == constant.Unknown {
- base.FatalfAt(pos, "bad value: %v", val)
- }
+// NewBasicLit returns an OLITERAL representing val with the given type.
+func NewBasicLit(pos src.XPos, typ *types.Type, val constant.Value) Node {
+ AssertValidTypeForConst(typ, val)
n := &BasicLit{val: val}
n.op = OLITERAL
n.pos = pos
- n.SetType(types.UntypedTypes[val.Kind()])
+ n.SetType(typ)
n.SetTypecheck(1)
return n
}
// NewConstExpr returns an OLITERAL representing val, copying the
// position and type from orig.
func NewConstExpr(val constant.Value, orig Node) Node {
- n := NewBasicLit(orig.Pos(), val)
- n.SetType(orig.Type())
- n.SetTypecheck(orig.Typecheck())
- return n
+ return NewBasicLit(orig.Pos(), orig.Type(), val)
}
// A BinaryExpr is a binary expression X Op Y,
// Statements
-var one = constant.MakeInt64(1)
-
func idealType(tv syntax.TypeAndValue) types2.Type {
// The gc backend expects all expressions to have a concrete type, and
// types2 mostly satisfies this expectation already. But there are a few
op := r.op()
lhs := r.expr()
pos := r.pos()
- n := ir.NewAssignOpStmt(pos, op, lhs, ir.NewBasicLit(pos, one))
+ n := ir.NewAssignOpStmt(pos, op, lhs, ir.NewOne(pos, lhs.Type()))
n.IncDec = true
return n
pos := r.pos()
typ := r.typ()
val := FixValue(typ, r.Value())
- return typed(typ, ir.NewBasicLit(pos, val))
+ return ir.NewBasicLit(pos, typ, val)
case exprNil:
pos := r.pos()
// uintptr-typed word from the dictionary parameter.
func (r *reader) dictWord(pos src.XPos, idx int) ir.Node {
base.AssertfAt(r.dictParam != nil, pos, "expected dictParam in %v", r.curfn)
- return typecheck.Expr(ir.NewIndexExpr(pos, r.dictParam, ir.NewBasicLit(pos, constant.MakeInt64(int64(idx)))))
+ return typecheck.Expr(ir.NewIndexExpr(pos, r.dictParam, ir.NewInt(pos, int64(idx))))
}
// rttiWord is like dictWord, but converts it to *byte (the type used
s.done.Append(ir.NewBranchStmt(pos, ir.OGOTO, endLabel))
// Add length case to outer switch.
- cas := ir.NewBasicLit(pos, constant.MakeInt64(runLen(run)))
+ cas := ir.NewInt(pos, runLen(run))
jmp := ir.NewBranchStmt(pos, ir.OGOTO, label)
outer.Add(pos, cas, nil, jmp)
}