]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: mangle names before rewriting calls
authorIan Lance Taylor <iant@golang.org>
Wed, 17 Oct 2018 01:18:40 +0000 (18:18 -0700)
committerIan Lance Taylor <iant@golang.org>
Wed, 17 Oct 2018 19:47:38 +0000 (19:47 +0000)
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 <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/cmd/cgo/gcc.go

index 3058fc5f3415ab06a0f2b83ed2b22ec3322f09df..858f5399154e2791bcb58997b2858da8b0966283 100644 (file)
@@ -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
                }