Commit 4710e16d authored by Matthew Dempsky's avatar Matthew Dempsky

cmd/compile: cleanup addmethod

Change-Id: Icb1671187d70edd962e2bda2cc45771b17a8e770
Reviewed-on: https://go-review.googlesource.com/28175
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 4eb2fa17
...@@ -1167,52 +1167,37 @@ func addmethod(msym *Sym, t *Type, tpkg *Pkg, local, nointerface bool) { ...@@ -1167,52 +1167,37 @@ func addmethod(msym *Sym, t *Type, tpkg *Pkg, local, nointerface bool) {
return return
} }
pa := rf.Type // base type mt := methtype(rf.Type)
mt := methtype(pa)
if mt == nil || mt.Sym == nil { if mt == nil || mt.Sym == nil {
t = pa pa := rf.Type
if t == nil { // rely on typecheck having complained before t := pa
return if t != nil && t.IsPtr() {
} if t.Sym != nil {
if t != nil {
if t.IsPtr() {
if t.Sym != nil {
Yyerror("invalid receiver type %v (%v is a pointer type)", pa, t)
return
}
t = t.Elem()
}
if t.Broke { // rely on typecheck having complained before
return
}
if t.Sym == nil {
Yyerror("invalid receiver type %v (%v is an unnamed type)", pa, t)
return
}
if t.IsPtr() {
Yyerror("invalid receiver type %v (%v is a pointer type)", pa, t) Yyerror("invalid receiver type %v (%v is a pointer type)", pa, t)
return return
} }
t = t.Elem()
if t.IsInterface() {
Yyerror("invalid receiver type %v (%v is an interface type)", pa, t)
return
}
} }
// Should have picked off all the reasons above, switch {
// but just in case, fall back to generic error. case t == nil || t.Broke:
Yyerror("invalid receiver type %v (%v / %v)", pa, Tconv(pa, FmtLong), Tconv(t, FmtLong)) // rely on typecheck having complained before
case t.Sym == nil:
Yyerror("invalid receiver type %v (%v is an unnamed type)", pa, t)
case t.IsPtr():
Yyerror("invalid receiver type %v (%v is a pointer type)", pa, t)
case t.IsInterface():
Yyerror("invalid receiver type %v (%v is an interface type)", pa, t)
default:
// Should have picked off all the reasons above,
// but just in case, fall back to generic error.
Yyerror("invalid receiver type %v (%v / %v)", pa, Tconv(pa, FmtLong), Tconv(t, FmtLong))
}
return return
} }
pa = mt if local && !mt.Local {
if local && !pa.Local { Yyerror("cannot define new methods on non-local type %v", mt)
Yyerror("cannot define new methods on non-local type %v", pa)
return return
} }
...@@ -1220,10 +1205,10 @@ func addmethod(msym *Sym, t *Type, tpkg *Pkg, local, nointerface bool) { ...@@ -1220,10 +1205,10 @@ func addmethod(msym *Sym, t *Type, tpkg *Pkg, local, nointerface bool) {
return return
} }
if pa.IsStruct() { if mt.IsStruct() {
for _, f := range pa.Fields().Slice() { for _, f := range mt.Fields().Slice() {
if f.Sym == msym { if f.Sym == msym {
Yyerror("type %v has both field and method named %v", pa, msym) Yyerror("type %v has both field and method named %v", mt, msym)
return return
} }
} }
...@@ -1232,14 +1217,14 @@ func addmethod(msym *Sym, t *Type, tpkg *Pkg, local, nointerface bool) { ...@@ -1232,14 +1217,14 @@ func addmethod(msym *Sym, t *Type, tpkg *Pkg, local, nointerface bool) {
n := Nod(ODCLFIELD, newname(msym), nil) n := Nod(ODCLFIELD, newname(msym), nil)
n.Type = t n.Type = t
for _, f := range pa.Methods().Slice() { for _, f := range mt.Methods().Slice() {
if msym.Name != f.Sym.Name { if msym.Name != f.Sym.Name {
continue continue
} }
// Eqtype only checks that incoming and result parameters match, // Eqtype only checks that incoming and result parameters match,
// so explicitly check that the receiver parameters match too. // so explicitly check that the receiver parameters match too.
if !Eqtype(t, f.Type) || !Eqtype(t.Recv().Type, f.Type.Recv().Type) { if !Eqtype(t, f.Type) || !Eqtype(t.Recv().Type, f.Type.Recv().Type) {
Yyerror("method redeclared: %v.%v\n\t%v\n\t%v", pa, msym, f.Type, t) Yyerror("method redeclared: %v.%v\n\t%v\n\t%v", mt, msym, f.Type, t)
} }
return return
} }
...@@ -1252,7 +1237,7 @@ func addmethod(msym *Sym, t *Type, tpkg *Pkg, local, nointerface bool) { ...@@ -1252,7 +1237,7 @@ func addmethod(msym *Sym, t *Type, tpkg *Pkg, local, nointerface bool) {
Fatalf("imported method name %v in wrong package %s\n", sconv(f.Sym, FmtSign), tpkg.Name) Fatalf("imported method name %v in wrong package %s\n", sconv(f.Sym, FmtSign), tpkg.Name)
} }
pa.Methods().Append(f) mt.Methods().Append(f)
} }
func funccompile(n *Node) { func funccompile(n *Node) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment