return NewBasicLit(pos, types.Types[types.TUINTPTR], constant.MakeInt64(v))
}
+// NewZero returns a zero value of the given type.
+func NewZero(pos src.XPos, typ *types.Type) Node {
+ switch {
+ case typ.HasNil():
+ return NewNilExpr(pos, typ)
+ case typ.IsInteger():
+ return NewBasicLit(pos, typ, intZero)
+ case typ.IsFloat():
+ return NewBasicLit(pos, typ, floatZero)
+ case typ.IsComplex():
+ return NewBasicLit(pos, typ, complexZero)
+ case typ.IsBoolean():
+ return NewBasicLit(pos, typ, constant.MakeBool(false))
+ case typ.IsString():
+ return NewBasicLit(pos, typ, constant.MakeString(""))
+ case typ.IsArray() || typ.IsStruct():
+ // TODO(mdempsky): Return a typechecked expression instead.
+ return NewCompLitExpr(pos, OCOMPLIT, typ, nil)
+ }
+
+ base.FatalfAt(pos, "unexpected type: %v", typ)
+ panic("unreachable")
+}
+
+var (
+ intZero = constant.MakeInt64(0)
+ floatZero = constant.ToFloat(intZero)
+ complexZero = constant.ToComplex(intZero)
+)
+
// NewOne returns an OLITERAL representing 1 with the given type.
func NewOne(pos src.XPos, typ *types.Type) Node {
var val constant.Value
// For anonymous and blank parameters, we don't have an *ir.Name
// to use as the argument. However, since we know the shaped
// function won't use the value either, we can just pass the
- // zero value. (Also unfortunately, we don't have an easy
- // zero-value IR node; so we use a default-initialized temporary
- // variable.)
+ // zero value.
if arg == nil {
- tmp := typecheck.TempAt(pos, r.curfn, param.Type)
- r.curfn.Body.Append(
- typecheck.Stmt(ir.NewDecl(pos, ir.ODCL, tmp)),
- typecheck.Stmt(ir.NewAssignStmt(pos, tmp, nil)),
- )
- arg = tmp
+ arg = ir.NewZero(pos, param.Type)
}
out.Append(arg)
val := FixValue(typ, r.Value())
return ir.NewBasicLit(pos, typ, val)
- case exprNil:
+ case exprZero:
pos := r.pos()
typ := r.typ()
- return Nil(pos, typ)
+ return ir.NewZero(pos, typ)
case exprCompLit:
return r.compLit()