cmd/cgo: annotate named return struct members in comments
If an exported function has named return variables, then show the names
as comments in the return struct we create in the header file.
Example here:
https://groups.google.com/forum/#!topic/golang-nuts/r393ne4zIfY
Change-Id: I21fb4ca2673f6977bec35ccab0cef7d42b311f96
Reviewed-on: https://go-review.googlesource.com/13061
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go
index 4a371fe..aaa105d 100644
--- a/src/cmd/cgo/out.go
+++ b/src/cmd/cgo/out.go
@@ -693,7 +693,7 @@
}
fntype := fn.Type
forFieldList(fntype.Params,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
t := p.cgoType(atype)
if off%t.Align != 0 {
pad := t.Align - off%t.Align
@@ -711,7 +711,7 @@
npad++
}
forFieldList(fntype.Results,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
t := p.cgoType(atype)
if off%t.Align != 0 {
pad := t.Align - off%t.Align
@@ -744,8 +744,12 @@
fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName)
fmt.Fprintf(fgcch, "struct %s_return {\n", exp.ExpName)
forFieldList(fntype.Results,
- func(i int, atype ast.Expr) {
- fmt.Fprintf(fgcch, "\t%s r%d;\n", p.cgoType(atype).C, i)
+ func(i int, aname string, atype ast.Expr) {
+ fmt.Fprintf(fgcch, "\t%s r%d;", p.cgoType(atype).C, i)
+ if len(aname) > 0 {
+ fmt.Fprintf(fgcch, " /* %s */", aname)
+ }
+ fmt.Fprint(fgcch, "\n")
})
fmt.Fprintf(fgcch, "};\n")
gccResult = "struct " + exp.ExpName + "_return"
@@ -758,7 +762,7 @@
s += " recv"
}
forFieldList(fntype.Params,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
if i > 0 || fn.Recv != nil {
s += ", "
}
@@ -783,7 +787,7 @@
fmt.Fprintf(fgcc, "\ta.recv = recv;\n")
}
forFieldList(fntype.Params,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
fmt.Fprintf(fgcc, "\ta.p%d = p%d;\n", i, i)
})
fmt.Fprintf(fgcc, "\tcrosscall2(_cgoexp%s_%s, &a, %d);\n", cPrefix, exp.ExpName, off)
@@ -792,7 +796,7 @@
fmt.Fprintf(fgcc, "\treturn a.r0;\n")
} else {
forFieldList(fntype.Results,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
fmt.Fprintf(fgcc, "\tr.r%d = a.r%d;\n", i, i)
})
fmt.Fprintf(fgcc, "\treturn r;\n")
@@ -824,7 +828,7 @@
fmt.Fprintf(fgo2, "func %s(recv ", goname)
conf.Fprint(fgo2, fset, fn.Recv.List[0].Type)
forFieldList(fntype.Params,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
fmt.Fprintf(fgo2, ", p%d ", i)
conf.Fprint(fgo2, fset, atype)
})
@@ -832,7 +836,7 @@
if gccResult != "void" {
fmt.Fprint(fgo2, " (")
forFieldList(fntype.Results,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
if i > 0 {
fmt.Fprint(fgo2, ", ")
}
@@ -847,7 +851,7 @@
}
fmt.Fprintf(fgo2, "recv.%s(", exp.Func.Name)
forFieldList(fntype.Params,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
if i > 0 {
fmt.Fprint(fgo2, ", ")
}
@@ -879,13 +883,13 @@
cdeclBuf := new(bytes.Buffer)
resultCount := 0
forFieldList(fntype.Results,
- func(i int, atype ast.Expr) { resultCount++ })
+ func(i int, aname string, atype ast.Expr) { resultCount++ })
switch resultCount {
case 0:
fmt.Fprintf(cdeclBuf, "void")
case 1:
forFieldList(fntype.Results,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
t := p.cgoType(atype)
fmt.Fprintf(cdeclBuf, "%s", t.C)
})
@@ -894,9 +898,13 @@
fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName)
fmt.Fprintf(fgcch, "struct %s_result {\n", exp.ExpName)
forFieldList(fntype.Results,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
t := p.cgoType(atype)
- fmt.Fprintf(fgcch, "\t%s r%d;\n", t.C, i)
+ fmt.Fprintf(fgcch, "\t%s r%d;", t.C, i)
+ if len(aname) > 0 {
+ fmt.Fprintf(fgcch, " /* %s */", aname)
+ }
+ fmt.Fprint(fgcch, "\n")
})
fmt.Fprintf(fgcch, "};\n")
fmt.Fprintf(cdeclBuf, "struct %s_result", exp.ExpName)
@@ -911,7 +919,7 @@
}
// Function parameters.
forFieldList(fntype.Params,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
if i > 0 || fn.Recv != nil {
fmt.Fprintf(cdeclBuf, ", ")
}
@@ -956,7 +964,7 @@
fmt.Fprint(fgcc, "recv")
}
forFieldList(fntype.Params,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
if i > 0 || fn.Recv != nil {
fmt.Fprintf(fgcc, ", ")
}
@@ -982,7 +990,7 @@
printer.Fprint(fgo2, fset, fn.Recv.List[0].Type)
}
forFieldList(fntype.Params,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
if i > 0 || fn.Recv != nil {
fmt.Fprintf(fgo2, ", ")
}
@@ -993,7 +1001,7 @@
if resultCount > 0 {
fmt.Fprintf(fgo2, " (")
forFieldList(fntype.Results,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
if i > 0 {
fmt.Fprint(fgo2, ", ")
}
@@ -1013,7 +1021,7 @@
}
fmt.Fprintf(fgo2, "%s(", exp.Func.Name)
forFieldList(fntype.Params,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
if i > 0 {
fmt.Fprint(fgo2, ", ")
}
@@ -1071,19 +1079,19 @@
}
// Call a function for each entry in an ast.FieldList, passing the
-// index into the list and the type.
-func forFieldList(fl *ast.FieldList, fn func(int, ast.Expr)) {
+// index into the list, the name if any, and the type.
+func forFieldList(fl *ast.FieldList, fn func(int, string, ast.Expr)) {
if fl == nil {
return
}
i := 0
for _, r := range fl.List {
if r.Names == nil {
- fn(i, r.Type)
+ fn(i, "", r.Type)
i++
} else {
- for range r.Names {
- fn(i, r.Type)
+ for _, n := range r.Names {
+ fn(i, n.Name, r.Type)
i++
}
}