Commit a8eb6d51 authored by Robert Griesemer's avatar Robert Griesemer

cmd/compile: simplify field/method export (internal cleanup)

Towards a fix for #15514.

Change-Id: I62073e9fdcfe5ddda9b0a47fe8554b524191a77c
Reviewed-on: https://go-review.googlesource.com/27638Reviewed-by: default avatarMatthew Dempsky <mdempsky@google.com>
parent ec75230b
...@@ -810,9 +810,6 @@ func (p *exporter) typ(t *Type) { ...@@ -810,9 +810,6 @@ func (p *exporter) typ(t *Type) {
} }
func (p *exporter) qualifiedName(sym *Sym) { func (p *exporter) qualifiedName(sym *Sym) {
if strings.Contains(sym.Name, ".") {
Fatalf("exporter: invalid symbol name: %s", sym.Name)
}
p.string(sym.Name) p.string(sym.Name)
p.pkg(sym.Pkg) p.pkg(sym.Pkg)
} }
...@@ -834,7 +831,7 @@ func (p *exporter) fieldList(t *Type) { ...@@ -834,7 +831,7 @@ func (p *exporter) fieldList(t *Type) {
func (p *exporter) field(f *Field) { func (p *exporter) field(f *Field) {
p.pos(f.Nname) p.pos(f.Nname)
p.fieldName(f.Sym, f) p.fieldName(f)
p.typ(f.Type) p.typ(f.Type)
p.string(f.Note) p.string(f.Note)
} }
...@@ -856,37 +853,27 @@ func (p *exporter) methodList(t *Type) { ...@@ -856,37 +853,27 @@ func (p *exporter) methodList(t *Type) {
func (p *exporter) method(m *Field) { func (p *exporter) method(m *Field) {
p.pos(m.Nname) p.pos(m.Nname)
p.fieldName(m.Sym, m) p.fieldName(m)
p.paramList(m.Type.Params(), false) p.paramList(m.Type.Params(), false)
p.paramList(m.Type.Results(), false) p.paramList(m.Type.Results(), false)
} }
// fieldName is like qualifiedName but it doesn't record the package // fieldName is like qualifiedName but it doesn't record the package
// for blank (_) or exported names. // for blank (_) or exported names.
func (p *exporter) fieldName(sym *Sym, t *Field) { func (p *exporter) fieldName(t *Field) {
if t != nil && sym != t.Sym { name := t.Sym.Name
Fatalf("exporter: invalid fieldName parameters")
}
name := sym.Name
if t != nil {
if t.Embedded == 0 {
name = sym.Name
} else if bname := basetypeName(t.Type); bname != "" && !exportname(bname) {
// anonymous field with unexported base type name: use "?" as field name
// (bname != "" per spec, but we are conservative in case of errors)
name = "?"
} else {
name = ""
}
}
if strings.Contains(name, ".") { if t.Embedded != 0 {
Fatalf("exporter: invalid symbol name: %s", name) name = "" // anonymous field
if bname := basetypeName(t.Type); bname != "" && !exportname(bname) {
// anonymous field with unexported base type name
name = "?" // unexported name to force export of package
}
} }
p.string(name) p.string(name)
if name == "?" || name != "_" && name != "" && !exportname(name) {
p.pkg(sym.Pkg) if name != "_" && name != "" && !exportname(name) {
p.pkg(t.Sym.Pkg)
} }
} }
...@@ -895,10 +882,8 @@ func basetypeName(t *Type) string { ...@@ -895,10 +882,8 @@ func basetypeName(t *Type) string {
if s == nil && t.IsPtr() { if s == nil && t.IsPtr() {
s = t.Elem().Sym // deref s = t.Elem().Sym // deref
} }
// s should exist, but be conservative
if s != nil { if s != nil {
if strings.Contains(s.Name, ".") {
Fatalf("exporter: invalid symbol name: %s", s.Name)
}
return s.Name return s.Name
} }
return "" return ""
......
...@@ -595,7 +595,7 @@ func (p *importer) fieldName() *Sym { ...@@ -595,7 +595,7 @@ func (p *importer) fieldName() *Sym {
// (see parser.go:sym). The binary exporter only exports blank as a non-exported // (see parser.go:sym). The binary exporter only exports blank as a non-exported
// identifier without qualification. // identifier without qualification.
pkg = builtinpkg pkg = builtinpkg
} else if name == "?" || name != "" && !exportname(name) { } else if name != "" && !exportname(name) {
if name == "?" { if name == "?" {
name = "" name = ""
} }
......
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