func eqFunc(t *types.Type) *ir.Func {
// Autogenerate code for equality of structs and arrays.
-
+ sym := TypeSymPrefix(".eq", t)
+ if sym.Def != nil {
+ return sym.Def.(*ir.Name).Func
+ }
base.Pos = base.AutogeneratedPos // less confusing than end of input
typecheck.DeclContext = ir.PEXTERN
// func sym(p, q *T) bool
- sym := TypeSymPrefix(".eq", t)
fn := typecheck.DeclFunc(sym, nil,
[]*ir.Field{ir.NewField(base.Pos, typecheck.Lookup("p"), types.NewPtr(t)), ir.NewField(base.Pos, typecheck.Lookup("q"), types.NewPtr(t))},
[]*ir.Field{ir.NewField(base.Pos, typecheck.Lookup("r"), types.Types[types.TBOOL])},
)
+ sym.Def = fn.Nname
np := ir.AsNode(fn.Type().Params().Field(0).Nname)
nq := ir.AsNode(fn.Type().Params().Field(1).Nname)
nr := ir.AsNode(fn.Type().Results().Field(0).Nname)
fn.SetDupok(true)
typecheck.Func(fn)
- ir.CurFunc = fn
- typecheck.Stmts(fn.Body)
- ir.CurFunc = nil
+ ir.WithFunc(fn, func() {
+ typecheck.Stmts(fn.Body)
+ })
// Disable checknils while compiling this code.
// We are comparing a struct or an array,
return fn
}
+// EqFor returns ONAME node represents type t's equal function, and a boolean
+// to indicates whether a length needs to be passed when calling the function.
+func EqFor(t *types.Type) (ir.Node, bool) {
+ switch a, _ := types.AlgType(t); a {
+ case types.AMEM:
+ n := typecheck.LookupRuntime("memequal")
+ n = typecheck.SubstArgTypes(n, t, t)
+ return n, true
+ case types.ASPECIAL:
+ fn := eqFunc(t)
+ return fn.Nname, false
+ }
+ base.Fatalf("EqFor %v", t)
+ return nil, false
+}
+
func anyCall(fn *ir.Func) bool {
return ir.Any(fn, func(n ir.Node) bool {
// TODO(rsc): No methods?
base.Fatalf("arguments of comparison must be lvalues - %v %v", cmpl, cmpr)
}
- fn, needsize := eqFor(t)
+ // Should only arrive here with large memory or
+ // a struct/array containing a non-memory field/element.
+ // Small memory is handled inline, and single non-memory
+ // is handled by walkCompare.
+ fn, needsLength := reflectdata.EqFor(t)
call := ir.NewCallExpr(base.Pos, ir.OCALL, fn, nil)
call.Args.Append(typecheck.NodAddr(cmpl))
call.Args.Append(typecheck.NodAddr(cmpr))
- if needsize {
+ if needsLength {
call.Args.Append(ir.NewInt(t.Size()))
}
res := ir.Node(call)