Commit dc110f24 authored by Andy Maloney's avatar Andy Maloney Committed by Ian Lance Taylor

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/13061Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 38519e69
...@@ -693,7 +693,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) { ...@@ -693,7 +693,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
} }
fntype := fn.Type fntype := fn.Type
forFieldList(fntype.Params, forFieldList(fntype.Params,
func(i int, atype ast.Expr) { func(i int, aname string, atype ast.Expr) {
t := p.cgoType(atype) t := p.cgoType(atype)
if off%t.Align != 0 { if off%t.Align != 0 {
pad := t.Align - off%t.Align pad := t.Align - off%t.Align
...@@ -711,7 +711,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) { ...@@ -711,7 +711,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
npad++ npad++
} }
forFieldList(fntype.Results, forFieldList(fntype.Results,
func(i int, atype ast.Expr) { func(i int, aname string, atype ast.Expr) {
t := p.cgoType(atype) t := p.cgoType(atype)
if off%t.Align != 0 { if off%t.Align != 0 {
pad := t.Align - off%t.Align pad := t.Align - off%t.Align
...@@ -744,8 +744,12 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) { ...@@ -744,8 +744,12 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName) fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName)
fmt.Fprintf(fgcch, "struct %s_return {\n", exp.ExpName) fmt.Fprintf(fgcch, "struct %s_return {\n", exp.ExpName)
forFieldList(fntype.Results, forFieldList(fntype.Results,
func(i int, atype ast.Expr) { func(i int, aname string, atype ast.Expr) {
fmt.Fprintf(fgcch, "\t%s r%d;\n", p.cgoType(atype).C, i) 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") fmt.Fprintf(fgcch, "};\n")
gccResult = "struct " + exp.ExpName + "_return" gccResult = "struct " + exp.ExpName + "_return"
...@@ -758,7 +762,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) { ...@@ -758,7 +762,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
s += " recv" s += " recv"
} }
forFieldList(fntype.Params, forFieldList(fntype.Params,
func(i int, atype ast.Expr) { func(i int, aname string, atype ast.Expr) {
if i > 0 || fn.Recv != nil { if i > 0 || fn.Recv != nil {
s += ", " s += ", "
} }
...@@ -783,7 +787,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) { ...@@ -783,7 +787,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
fmt.Fprintf(fgcc, "\ta.recv = recv;\n") fmt.Fprintf(fgcc, "\ta.recv = recv;\n")
} }
forFieldList(fntype.Params, 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, "\ta.p%d = p%d;\n", i, i)
}) })
fmt.Fprintf(fgcc, "\tcrosscall2(_cgoexp%s_%s, &a, %d);\n", cPrefix, exp.ExpName, off) fmt.Fprintf(fgcc, "\tcrosscall2(_cgoexp%s_%s, &a, %d);\n", cPrefix, exp.ExpName, off)
...@@ -792,7 +796,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) { ...@@ -792,7 +796,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
fmt.Fprintf(fgcc, "\treturn a.r0;\n") fmt.Fprintf(fgcc, "\treturn a.r0;\n")
} else { } else {
forFieldList(fntype.Results, 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, "\tr.r%d = a.r%d;\n", i, i)
}) })
fmt.Fprintf(fgcc, "\treturn r;\n") fmt.Fprintf(fgcc, "\treturn r;\n")
...@@ -824,7 +828,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) { ...@@ -824,7 +828,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
fmt.Fprintf(fgo2, "func %s(recv ", goname) fmt.Fprintf(fgo2, "func %s(recv ", goname)
conf.Fprint(fgo2, fset, fn.Recv.List[0].Type) conf.Fprint(fgo2, fset, fn.Recv.List[0].Type)
forFieldList(fntype.Params, forFieldList(fntype.Params,
func(i int, atype ast.Expr) { func(i int, aname string, atype ast.Expr) {
fmt.Fprintf(fgo2, ", p%d ", i) fmt.Fprintf(fgo2, ", p%d ", i)
conf.Fprint(fgo2, fset, atype) conf.Fprint(fgo2, fset, atype)
}) })
...@@ -832,7 +836,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) { ...@@ -832,7 +836,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
if gccResult != "void" { if gccResult != "void" {
fmt.Fprint(fgo2, " (") fmt.Fprint(fgo2, " (")
forFieldList(fntype.Results, forFieldList(fntype.Results,
func(i int, atype ast.Expr) { func(i int, aname string, atype ast.Expr) {
if i > 0 { if i > 0 {
fmt.Fprint(fgo2, ", ") fmt.Fprint(fgo2, ", ")
} }
...@@ -847,7 +851,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) { ...@@ -847,7 +851,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
} }
fmt.Fprintf(fgo2, "recv.%s(", exp.Func.Name) fmt.Fprintf(fgo2, "recv.%s(", exp.Func.Name)
forFieldList(fntype.Params, forFieldList(fntype.Params,
func(i int, atype ast.Expr) { func(i int, aname string, atype ast.Expr) {
if i > 0 { if i > 0 {
fmt.Fprint(fgo2, ", ") fmt.Fprint(fgo2, ", ")
} }
...@@ -879,13 +883,13 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) { ...@@ -879,13 +883,13 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
cdeclBuf := new(bytes.Buffer) cdeclBuf := new(bytes.Buffer)
resultCount := 0 resultCount := 0
forFieldList(fntype.Results, forFieldList(fntype.Results,
func(i int, atype ast.Expr) { resultCount++ }) func(i int, aname string, atype ast.Expr) { resultCount++ })
switch resultCount { switch resultCount {
case 0: case 0:
fmt.Fprintf(cdeclBuf, "void") fmt.Fprintf(cdeclBuf, "void")
case 1: case 1:
forFieldList(fntype.Results, forFieldList(fntype.Results,
func(i int, atype ast.Expr) { func(i int, aname string, atype ast.Expr) {
t := p.cgoType(atype) t := p.cgoType(atype)
fmt.Fprintf(cdeclBuf, "%s", t.C) fmt.Fprintf(cdeclBuf, "%s", t.C)
}) })
...@@ -894,9 +898,13 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) { ...@@ -894,9 +898,13 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName) fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName)
fmt.Fprintf(fgcch, "struct %s_result {\n", exp.ExpName) fmt.Fprintf(fgcch, "struct %s_result {\n", exp.ExpName)
forFieldList(fntype.Results, forFieldList(fntype.Results,
func(i int, atype ast.Expr) { func(i int, aname string, atype ast.Expr) {
t := p.cgoType(atype) 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(fgcch, "};\n")
fmt.Fprintf(cdeclBuf, "struct %s_result", exp.ExpName) fmt.Fprintf(cdeclBuf, "struct %s_result", exp.ExpName)
...@@ -911,7 +919,7 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) { ...@@ -911,7 +919,7 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
} }
// Function parameters. // Function parameters.
forFieldList(fntype.Params, forFieldList(fntype.Params,
func(i int, atype ast.Expr) { func(i int, aname string, atype ast.Expr) {
if i > 0 || fn.Recv != nil { if i > 0 || fn.Recv != nil {
fmt.Fprintf(cdeclBuf, ", ") fmt.Fprintf(cdeclBuf, ", ")
} }
...@@ -956,7 +964,7 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) { ...@@ -956,7 +964,7 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
fmt.Fprint(fgcc, "recv") fmt.Fprint(fgcc, "recv")
} }
forFieldList(fntype.Params, forFieldList(fntype.Params,
func(i int, atype ast.Expr) { func(i int, aname string, atype ast.Expr) {
if i > 0 || fn.Recv != nil { if i > 0 || fn.Recv != nil {
fmt.Fprintf(fgcc, ", ") fmt.Fprintf(fgcc, ", ")
} }
...@@ -982,7 +990,7 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) { ...@@ -982,7 +990,7 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
printer.Fprint(fgo2, fset, fn.Recv.List[0].Type) printer.Fprint(fgo2, fset, fn.Recv.List[0].Type)
} }
forFieldList(fntype.Params, forFieldList(fntype.Params,
func(i int, atype ast.Expr) { func(i int, aname string, atype ast.Expr) {
if i > 0 || fn.Recv != nil { if i > 0 || fn.Recv != nil {
fmt.Fprintf(fgo2, ", ") fmt.Fprintf(fgo2, ", ")
} }
...@@ -993,7 +1001,7 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) { ...@@ -993,7 +1001,7 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
if resultCount > 0 { if resultCount > 0 {
fmt.Fprintf(fgo2, " (") fmt.Fprintf(fgo2, " (")
forFieldList(fntype.Results, forFieldList(fntype.Results,
func(i int, atype ast.Expr) { func(i int, aname string, atype ast.Expr) {
if i > 0 { if i > 0 {
fmt.Fprint(fgo2, ", ") fmt.Fprint(fgo2, ", ")
} }
...@@ -1013,7 +1021,7 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) { ...@@ -1013,7 +1021,7 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
} }
fmt.Fprintf(fgo2, "%s(", exp.Func.Name) fmt.Fprintf(fgo2, "%s(", exp.Func.Name)
forFieldList(fntype.Params, forFieldList(fntype.Params,
func(i int, atype ast.Expr) { func(i int, aname string, atype ast.Expr) {
if i > 0 { if i > 0 {
fmt.Fprint(fgo2, ", ") fmt.Fprint(fgo2, ", ")
} }
...@@ -1071,19 +1079,19 @@ func (p *Package) gccgoSymbolPrefix() string { ...@@ -1071,19 +1079,19 @@ func (p *Package) gccgoSymbolPrefix() string {
} }
// Call a function for each entry in an ast.FieldList, passing the // Call a function for each entry in an ast.FieldList, passing the
// index into the list and the type. // index into the list, the name if any, and the type.
func forFieldList(fl *ast.FieldList, fn func(int, ast.Expr)) { func forFieldList(fl *ast.FieldList, fn func(int, string, ast.Expr)) {
if fl == nil { if fl == nil {
return return
} }
i := 0 i := 0
for _, r := range fl.List { for _, r := range fl.List {
if r.Names == nil { if r.Names == nil {
fn(i, r.Type) fn(i, "", r.Type)
i++ i++
} else { } else {
for range r.Names { for _, n := range r.Names {
fn(i, r.Type) fn(i, n.Name, r.Type)
i++ i++
} }
} }
......
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