From: Ian Lance Taylor Date: Wed, 17 Oct 2018 01:18:40 +0000 (-0700) Subject: cmd/cgo: mangle names before rewriting calls X-Git-Tag: go1.12beta1~740 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=19b264e7bfb47526607d68dd5e1adc2b9f7d058f;p=gostls13.git cmd/cgo: mangle names before rewriting calls Move name mangling before rewriting calls rather than after. This is in preparation for later changes. Change-Id: I74bc351f4290dad7ebf6d0d361bb684087786053 Reviewed-on: https://go-review.googlesource.com/c/142881 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- diff --git a/src/cmd/cgo/gcc.go b/src/cmd/cgo/gcc.go index 3058fc5f34..858f539915 100644 --- a/src/cmd/cgo/gcc.go +++ b/src/cmd/cgo/gcc.go @@ -188,6 +188,7 @@ func (p *Package) Translate(f *File) { break } } + p.prepareNames(f) if p.rewriteCalls(f) { // Add `import _cgo_unsafe "unsafe"` after the package statement. f.Edit.Insert(f.offset(f.AST.Name.End()), "; import _cgo_unsafe \"unsafe\"") @@ -679,6 +680,27 @@ func (p *Package) recordTypedefs1(dtype dwarf.Type, visited map[dwarf.Type]bool) } } +// prepareNames finalizes the Kind field of not-type names and sets +// the mangled name of all names. +func (p *Package) prepareNames(f *File) { + for _, n := range f.Name { + if n.Kind == "not-type" { + if n.Define == "" { + n.Kind = "var" + } else { + n.Kind = "macro" + n.FuncType = &FuncType{ + Result: n.Type, + Go: &ast.FuncType{ + Results: &ast.FieldList{List: []*ast.Field{{Type: n.Type.Go}}}, + }, + } + } + } + p.mangleName(n) + } +} + // mangleName does name mangling to translate names // from the original Go source files to the names // used in the final Go files generated by cgo. @@ -1130,24 +1152,7 @@ func (p *Package) rewriteRef(f *File) { // code for them. functions := make(map[string]bool) - // Assign mangled names. for _, n := range f.Name { - if n.Kind == "not-type" { - if n.Define == "" { - n.Kind = "var" - } else { - n.Kind = "macro" - n.FuncType = &FuncType{ - Result: n.Type, - Go: &ast.FuncType{ - Results: &ast.FieldList{List: []*ast.Field{{Type: n.Type.Go}}}, - }, - } - } - } - if n.Mangle == "" { - p.mangleName(n) - } if n.Kind == "func" { functions[n.Go] = false }