summaryrefslogtreecommitdiff
path: root/src/cmd/cgo
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2011-01-17 12:40:45 +0100
committerOndřej Surý <ondrej@sury.org>2011-01-17 12:40:45 +0100
commit3e45412327a2654a77944249962b3652e6142299 (patch)
treebc3bf69452afa055423cbe0c5cfa8ca357df6ccf /src/cmd/cgo
parentc533680039762cacbc37db8dc7eed074c3e497be (diff)
downloadgolang-3e45412327a2654a77944249962b3652e6142299.tar.gz
Imported Upstream version 2011.01.12upstream/2011.01.12
Diffstat (limited to 'src/cmd/cgo')
-rw-r--r--src/cmd/cgo/Makefile2
-rw-r--r--src/cmd/cgo/ast.go465
-rw-r--r--src/cmd/cgo/doc.go25
-rw-r--r--src/cmd/cgo/gcc.go543
-rw-r--r--src/cmd/cgo/main.go256
-rw-r--r--src/cmd/cgo/out.go524
-rw-r--r--src/cmd/cgo/util.go51
7 files changed, 1183 insertions, 683 deletions
diff --git a/src/cmd/cgo/Makefile b/src/cmd/cgo/Makefile
index 34ca3dd46..5458c3e4f 100644
--- a/src/cmd/cgo/Makefile
+++ b/src/cmd/cgo/Makefile
@@ -2,7 +2,7 @@
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
-include ../../Make.$(GOARCH)
+include ../../Make.inc
TARG=cgo
GOFILES=\
diff --git a/src/cmd/cgo/ast.go b/src/cmd/cgo/ast.go
index 580a72a95..8689ac3da 100644
--- a/src/cmd/cgo/ast.go
+++ b/src/cmd/cgo/ast.go
@@ -12,63 +12,13 @@ import (
"go/doc"
"go/parser"
"go/scanner"
+ "go/token"
"os"
"strings"
)
-// A Cref refers to an expression of the form C.xxx in the AST.
-type Cref struct {
- Name string
- Expr *ast.Expr
- Context string // "type", "expr", "const", or "call"
- TypeName bool // whether xxx is a C type name
- Type *Type // the type of xxx
- FuncType *FuncType
-}
-
-// A ExpFunc is an exported function, callable from C.
-type ExpFunc struct {
- Func *ast.FuncDecl
- ExpName string // name to use from C
-}
-
-// A Prog collects information about a cgo program.
-type Prog struct {
- AST *ast.File // parsed AST
- Preamble string // C preamble (doc comment on import "C")
- PackagePath string
- Package string
- Crefs []*Cref
- Typedef map[string]ast.Expr
- Vardef map[string]*Type
- Funcdef map[string]*FuncType
- Enumdef map[string]int64
- Constdef map[string]string
- ExpFuncs []*ExpFunc
- PtrSize int64
- GccOptions []string
- OutDefs map[string]bool
-}
-
-// A Type collects information about a type in both the C and Go worlds.
-type Type struct {
- Size int64
- Align int64
- C string
- Go ast.Expr
- EnumValues map[string]int64
-}
-
-// A FuncType collects information about a function type in both the C and Go worlds.
-type FuncType struct {
- Params []*Type
- Result *Type
- Go *ast.FuncType
-}
-
-func openProg(name string, p *Prog) {
- var err os.Error
- p.AST, err = parser.ParseFile(name, nil, nil, parser.ParseComments)
+func parse(name string, flags uint) *ast.File {
+ ast1, err := parser.ParseFile(fset, name, nil, flags)
if err != nil {
if list, ok := err.(scanner.ErrorList); ok {
// If err is a scanner.ErrorList, its String will print just
@@ -82,25 +32,37 @@ func openProg(name string, p *Prog) {
}
fatal("parsing %s: %s", name, err)
}
- p.Package = p.AST.Name.Name()
+ return ast1
+}
- // Find the import "C" line and get any extra C preamble.
- // Delete the import "C" line along the way.
+// ReadGo populates f with information learned from reading the
+// Go source file with the given file name. It gathers the C preamble
+// attached to the import "C" comment, a list of references to C.xxx,
+// a list of exported functions, and the actual AST, to be rewritten and
+// printed.
+func (f *File) ReadGo(name string) {
+ // Two different parses: once with comments, once without.
+ // The printer is not good enough at printing comments in the
+ // right place when we start editing the AST behind its back,
+ // so we use ast1 to look for the doc comments on import "C"
+ // and on exported functions, and we use ast2 for translating
+ // and reprinting.
+ ast1 := parse(name, parser.ParseComments)
+ ast2 := parse(name, 0)
+
+ f.Package = ast1.Name.Name
+ f.Name = make(map[string]*Name)
+
+ // In ast1, find the import "C" line and get any extra C preamble.
sawC := false
- w := 0
- for _, decl := range p.AST.Decls {
+ for _, decl := range ast1.Decls {
d, ok := decl.(*ast.GenDecl)
if !ok {
- p.AST.Decls[w] = decl
- w++
continue
}
- ws := 0
for _, spec := range d.Specs {
s, ok := spec.(*ast.ImportSpec)
if !ok || string(s.Path.Value) != `"C"` {
- d.Specs[ws] = spec
- ws++
continue
}
sawC = true
@@ -108,269 +70,330 @@ func openProg(name string, p *Prog) {
error(s.Path.Pos(), `cannot rename import "C"`)
}
if s.Doc != nil {
- p.Preamble += doc.CommentText(s.Doc) + "\n"
+ f.Preamble += doc.CommentText(s.Doc) + "\n"
} else if len(d.Specs) == 1 && d.Doc != nil {
- p.Preamble += doc.CommentText(d.Doc) + "\n"
+ f.Preamble += doc.CommentText(d.Doc) + "\n"
+ }
+ }
+ }
+ if !sawC {
+ error(token.NoPos, `cannot find import "C"`)
+ }
+
+ // In ast2, strip the import "C" line.
+ w := 0
+ for _, decl := range ast2.Decls {
+ d, ok := decl.(*ast.GenDecl)
+ if !ok {
+ ast2.Decls[w] = decl
+ w++
+ continue
+ }
+ ws := 0
+ for _, spec := range d.Specs {
+ s, ok := spec.(*ast.ImportSpec)
+ if !ok || string(s.Path.Value) != `"C"` {
+ d.Specs[ws] = spec
+ ws++
}
}
if ws == 0 {
continue
}
d.Specs = d.Specs[0:ws]
- p.AST.Decls[w] = d
+ ast2.Decls[w] = d
w++
}
- p.AST.Decls = p.AST.Decls[0:w]
-
- if !sawC {
- error(noPos, `cannot find import "C"`)
- }
+ ast2.Decls = ast2.Decls[0:w]
// Accumulate pointers to uses of C.x.
- if p.Crefs == nil {
- p.Crefs = make([]*Cref, 0, 8)
+ if f.Ref == nil {
+ f.Ref = make([]*Ref, 0, 8)
}
- walk(p.AST, p, "prog")
+ f.walk(ast2, "prog", (*File).saveRef)
+
+ // Accumulate exported functions.
+ // The comments are only on ast1 but we need to
+ // save the function bodies from ast2.
+ // The first walk fills in ExpFunc, and the
+ // second walk changes the entries to
+ // refer to ast2 instead.
+ f.walk(ast1, "prog", (*File).saveExport)
+ f.walk(ast2, "prog", (*File).saveExport2)
+
+ f.AST = ast2
}
-func walk(x interface{}, p *Prog, context string) {
- switch n := x.(type) {
- case *ast.Expr:
- if sel, ok := (*n).(*ast.SelectorExpr); ok {
- // For now, assume that the only instance of capital C is
- // when used as the imported package identifier.
- // The parser should take care of scoping in the future,
- // so that we will be able to distinguish a "top-level C"
- // from a local C.
- if l, ok := sel.X.(*ast.Ident); ok && l.Name() == "C" {
- i := len(p.Crefs)
- if i >= cap(p.Crefs) {
- new := make([]*Cref, 2*i)
- for j, v := range p.Crefs {
- new[j] = v
- }
- p.Crefs = new
- }
- p.Crefs = p.Crefs[0 : i+1]
- p.Crefs[i] = &Cref{
- Name: sel.Sel.Name(),
- Expr: n,
- Context: context,
+// Save references to C.xxx for later processing.
+func (f *File) saveRef(x interface{}, context string) {
+ n, ok := x.(*ast.Expr)
+ if !ok {
+ return
+ }
+ if sel, ok := (*n).(*ast.SelectorExpr); ok {
+ // For now, assume that the only instance of capital C is
+ // when used as the imported package identifier.
+ // The parser should take care of scoping in the future,
+ // so that we will be able to distinguish a "top-level C"
+ // from a local C.
+ if l, ok := sel.X.(*ast.Ident); ok && l.Name == "C" {
+ if context == "as2" {
+ context = "expr"
+ }
+ goname := sel.Sel.Name
+ if goname == "errno" {
+ error(sel.Pos(), "cannot refer to errno directly; see documentation")
+ return
+ }
+ name := f.Name[goname]
+ if name == nil {
+ name = &Name{
+ Go: goname,
}
- break
+ f.Name[goname] = name
}
+ f.Ref = append(f.Ref, &Ref{
+ Name: name,
+ Expr: n,
+ Context: context,
+ })
+ return
}
- walk(*n, p, context)
+ }
+}
+
+// If a function should be exported add it to ExpFunc.
+func (f *File) saveExport(x interface{}, context string) {
+ n, ok := x.(*ast.FuncDecl)
+ if !ok {
+ return
+ }
+
+ if n.Doc == nil {
+ return
+ }
+ for _, c := range n.Doc.List {
+ if string(c.Text[0:9]) != "//export " {
+ continue
+ }
+
+ name := strings.TrimSpace(string(c.Text[9:]))
+ if name == "" {
+ error(c.Pos(), "export missing name")
+ }
+
+ f.ExpFunc = append(f.ExpFunc, &ExpFunc{
+ Func: n,
+ ExpName: name,
+ })
+ break
+ }
+}
+
+// Make f.ExpFunc[i] point at the Func from this AST instead of the other one.
+func (f *File) saveExport2(x interface{}, context string) {
+ n, ok := x.(*ast.FuncDecl)
+ if !ok {
+ return
+ }
+
+ for _, exp := range f.ExpFunc {
+ if exp.Func.Name.Name == n.Name.Name {
+ exp.Func = n
+ break
+ }
+ }
+}
+
+// walk walks the AST x, calling visit(f, x, context) for each node.
+func (f *File) walk(x interface{}, context string, visit func(*File, interface{}, string)) {
+ visit(f, x, context)
+ switch n := x.(type) {
+ case *ast.Expr:
+ f.walk(*n, context, visit)
// everything else just recurs
default:
- error(noPos, "unexpected type %T in walk", x)
+ error(token.NoPos, "unexpected type %T in walk", x, visit)
panic("unexpected type")
case nil:
// These are ordered and grouped to match ../../pkg/go/ast/ast.go
case *ast.Field:
- walk(&n.Type, p, "type")
+ f.walk(&n.Type, "type", visit)
case *ast.FieldList:
- for _, f := range n.List {
- walk(f, p, context)
+ for _, field := range n.List {
+ f.walk(field, context, visit)
}
case *ast.BadExpr:
case *ast.Ident:
case *ast.Ellipsis:
case *ast.BasicLit:
case *ast.FuncLit:
- walk(n.Type, p, "type")
- walk(n.Body, p, "stmt")
+ f.walk(n.Type, "type", visit)
+ f.walk(n.Body, "stmt", visit)
case *ast.CompositeLit:
- walk(&n.Type, p, "type")
- walk(n.Elts, p, "expr")
+ f.walk(&n.Type, "type", visit)
+ f.walk(n.Elts, "expr", visit)
case *ast.ParenExpr:
- walk(&n.X, p, context)
+ f.walk(&n.X, context, visit)
case *ast.SelectorExpr:
- walk(&n.X, p, "selector")
+ f.walk(&n.X, "selector", visit)
case *ast.IndexExpr:
- walk(&n.X, p, "expr")
- walk(&n.Index, p, "expr")
+ f.walk(&n.X, "expr", visit)
+ f.walk(&n.Index, "expr", visit)
case *ast.SliceExpr:
- walk(&n.X, p, "expr")
- walk(&n.Index, p, "expr")
- if n.End != nil {
- walk(&n.End, p, "expr")
+ f.walk(&n.X, "expr", visit)
+ if n.Low != nil {
+ f.walk(&n.Low, "expr", visit)
+ }
+ if n.High != nil {
+ f.walk(&n.High, "expr", visit)
}
case *ast.TypeAssertExpr:
- walk(&n.X, p, "expr")
- walk(&n.Type, p, "type")
+ f.walk(&n.X, "expr", visit)
+ f.walk(&n.Type, "type", visit)
case *ast.CallExpr:
- walk(&n.Fun, p, "call")
- walk(n.Args, p, "expr")
+ if context == "as2" {
+ f.walk(&n.Fun, "call2", visit)
+ } else {
+ f.walk(&n.Fun, "call", visit)
+ }
+ f.walk(n.Args, "expr", visit)
case *ast.StarExpr:
- walk(&n.X, p, context)
+ f.walk(&n.X, context, visit)
case *ast.UnaryExpr:
- walk(&n.X, p, "expr")
+ f.walk(&n.X, "expr", visit)
case *ast.BinaryExpr:
- walk(&n.X, p, "expr")
- walk(&n.Y, p, "expr")
+ f.walk(&n.X, "expr", visit)
+ f.walk(&n.Y, "expr", visit)
case *ast.KeyValueExpr:
- walk(&n.Key, p, "expr")
- walk(&n.Value, p, "expr")
+ f.walk(&n.Key, "expr", visit)
+ f.walk(&n.Value, "expr", visit)
case *ast.ArrayType:
- walk(&n.Len, p, "expr")
- walk(&n.Elt, p, "type")
+ f.walk(&n.Len, "expr", visit)
+ f.walk(&n.Elt, "type", visit)
case *ast.StructType:
- walk(n.Fields, p, "field")
+ f.walk(n.Fields, "field", visit)
case *ast.FuncType:
- walk(n.Params, p, "field")
+ f.walk(n.Params, "field", visit)
if n.Results != nil {
- walk(n.Results, p, "field")
+ f.walk(n.Results, "field", visit)
}
case *ast.InterfaceType:
- walk(n.Methods, p, "field")
+ f.walk(n.Methods, "field", visit)
case *ast.MapType:
- walk(&n.Key, p, "type")
- walk(&n.Value, p, "type")
+ f.walk(&n.Key, "type", visit)
+ f.walk(&n.Value, "type", visit)
case *ast.ChanType:
- walk(&n.Value, p, "type")
+ f.walk(&n.Value, "type", visit)
case *ast.BadStmt:
case *ast.DeclStmt:
- walk(n.Decl, p, "decl")
+ f.walk(n.Decl, "decl", visit)
case *ast.EmptyStmt:
case *ast.LabeledStmt:
- walk(n.Stmt, p, "stmt")
+ f.walk(n.Stmt, "stmt", visit)
case *ast.ExprStmt:
- walk(&n.X, p, "expr")
+ f.walk(&n.X, "expr", visit)
case *ast.IncDecStmt:
- walk(&n.X, p, "expr")
+ f.walk(&n.X, "expr", visit)
case *ast.AssignStmt:
- walk(n.Lhs, p, "expr")
- walk(n.Rhs, p, "expr")
+ f.walk(n.Lhs, "expr", visit)
+ if len(n.Lhs) == 2 && len(n.Rhs) == 1 {
+ f.walk(n.Rhs, "as2", visit)
+ } else {
+ f.walk(n.Rhs, "expr", visit)
+ }
case *ast.GoStmt:
- walk(n.Call, p, "expr")
+ f.walk(n.Call, "expr", visit)
case *ast.DeferStmt:
- walk(n.Call, p, "expr")
+ f.walk(n.Call, "expr", visit)
case *ast.ReturnStmt:
- walk(n.Results, p, "expr")
+ f.walk(n.Results, "expr", visit)
case *ast.BranchStmt:
case *ast.BlockStmt:
- walk(n.List, p, "stmt")
+ f.walk(n.List, "stmt", visit)
case *ast.IfStmt:
- walk(n.Init, p, "stmt")
- walk(&n.Cond, p, "expr")
- walk(n.Body, p, "stmt")
- walk(n.Else, p, "stmt")
+ f.walk(n.Init, "stmt", visit)
+ f.walk(&n.Cond, "expr", visit)
+ f.walk(n.Body, "stmt", visit)
+ f.walk(n.Else, "stmt", visit)
case *ast.CaseClause:
- walk(n.Values, p, "expr")
- walk(n.Body, p, "stmt")
+ f.walk(n.Values, "expr", visit)
+ f.walk(n.Body, "stmt", visit)
case *ast.SwitchStmt:
- walk(n.Init, p, "stmt")
- walk(&n.Tag, p, "expr")
- walk(n.Body, p, "stmt")
+ f.walk(n.Init, "stmt", visit)
+ f.walk(&n.Tag, "expr", visit)
+ f.walk(n.Body, "stmt", visit)
case *ast.TypeCaseClause:
- walk(n.Types, p, "type")
- walk(n.Body, p, "stmt")
+ f.walk(n.Types, "type", visit)
+ f.walk(n.Body, "stmt", visit)
case *ast.TypeSwitchStmt:
- walk(n.Init, p, "stmt")
- walk(n.Assign, p, "stmt")
- walk(n.Body, p, "stmt")
+ f.walk(n.Init, "stmt", visit)
+ f.walk(n.Assign, "stmt", visit)
+ f.walk(n.Body, "stmt", visit)
case *ast.CommClause:
- walk(n.Lhs, p, "expr")
- walk(n.Rhs, p, "expr")
- walk(n.Body, p, "stmt")
+ f.walk(n.Lhs, "expr", visit)
+ f.walk(n.Rhs, "expr", visit)
+ f.walk(n.Body, "stmt", visit)
case *ast.SelectStmt:
- walk(n.Body, p, "stmt")
+ f.walk(n.Body, "stmt", visit)
case *ast.ForStmt:
- walk(n.Init, p, "stmt")
- walk(&n.Cond, p, "expr")
- walk(n.Post, p, "stmt")
- walk(n.Body, p, "stmt")
+ f.walk(n.Init, "stmt", visit)
+ f.walk(&n.Cond, "expr", visit)
+ f.walk(n.Post, "stmt", visit)
+ f.walk(n.Body, "stmt", visit)
case *ast.RangeStmt:
- walk(&n.Key, p, "expr")
- walk(&n.Value, p, "expr")
- walk(&n.X, p, "expr")
- walk(n.Body, p, "stmt")
+ f.walk(&n.Key, "expr", visit)
+ f.walk(&n.Value, "expr", visit)
+ f.walk(&n.X, "expr", visit)
+ f.walk(n.Body, "stmt", visit)
case *ast.ImportSpec:
case *ast.ValueSpec:
- walk(&n.Type, p, "type")
- walk(n.Values, p, "expr")
+ f.walk(&n.Type, "type", visit)
+ f.walk(n.Values, "expr", visit)
case *ast.TypeSpec:
- walk(&n.Type, p, "type")
+ f.walk(&n.Type, "type", visit)
case *ast.BadDecl:
case *ast.GenDecl:
- walk(n.Specs, p, "spec")
+ f.walk(n.Specs, "spec", visit)
case *ast.FuncDecl:
if n.Recv != nil {
- walk(n.Recv, p, "field")
+ f.walk(n.Recv, "field", visit)
}
- walk(n.Type, p, "type")
+ f.walk(n.Type, "type", visit)
if n.Body != nil {
- walk(n.Body, p, "stmt")
+ f.walk(n.Body, "stmt", visit)
}
- checkExpFunc(n, p)
-
case *ast.File:
- walk(n.Decls, p, "decl")
+ f.walk(n.Decls, "decl", visit)
case *ast.Package:
- for _, f := range n.Files {
- walk(f, p, "file")
+ for _, file := range n.Files {
+ f.walk(file, "file", visit)
}
case []ast.Decl:
for _, d := range n {
- walk(d, p, context)
+ f.walk(d, context, visit)
}
case []ast.Expr:
for i := range n {
- walk(&n[i], p, context)
+ f.walk(&n[i], context, visit)
}
case []ast.Stmt:
for _, s := range n {
- walk(s, p, context)
+ f.walk(s, context, visit)
}
case []ast.Spec:
for _, s := range n {
- walk(s, p, context)
- }
- }
-}
-
-// If a function should be exported add it to ExpFuncs.
-func checkExpFunc(n *ast.FuncDecl, p *Prog) {
- if n.Doc == nil {
- return
- }
- for _, c := range n.Doc.List {
- if string(c.Text[0:9]) != "//export " {
- continue
- }
-
- name := strings.TrimSpace(string(c.Text[9:]))
- if name == "" {
- error(c.Position, "export missing name")
- }
-
- if p.ExpFuncs == nil {
- p.ExpFuncs = make([]*ExpFunc, 0, 8)
+ f.walk(s, context, visit)
}
- i := len(p.ExpFuncs)
- if i >= cap(p.ExpFuncs) {
- new := make([]*ExpFunc, 2*i)
- for j, v := range p.ExpFuncs {
- new[j] = v
- }
- p.ExpFuncs = new
- }
- p.ExpFuncs = p.ExpFuncs[0 : i+1]
- p.ExpFuncs[i] = &ExpFunc{
- Func: n,
- ExpName: name,
- }
- break
}
}
diff --git a/src/cmd/cgo/doc.go b/src/cmd/cgo/doc.go
index 022a87c15..0f9204d7f 100644
--- a/src/cmd/cgo/doc.go
+++ b/src/cmd/cgo/doc.go
@@ -23,6 +23,31 @@ the package. For example:
// #include <errno.h>
import "C"
+C identifiers or field names that are keywords in Go can be
+accessed by prefixing them with an underscore: if x points at
+a C struct with a field named "type", x._type accesses the field.
+
+The standard C numeric types are available under the names
+C.char, C.schar (signed char), C.uchar (unsigned char),
+C.short, C.ushort (unsigned short), C.int, C.uint (unsigned int),
+C.long, C.ulong (unsigned long), C.longlong (long long),
+C.ulonglong (unsigned long long), C.float, C.double.
+
+To access a struct, union, or enum type directly, prefix it with
+struct_, union_, or enum_, as in C.struct_stat.
+
+Any C function that returns a value may be called in a multiple
+assignment context to retrieve both the return value and the
+C errno variable as an os.Error. For example:
+
+ n, err := C.atoi("abc")
+
+In C, a function argument written as a fixed size array
+actually requires a pointer to the first element of the array.
+C compilers are aware of this calling convention and adjust
+the call accordingly, but Go cannot. In Go, you must pass
+the pointer to the first element explicitly: C.f(&x[0]).
+
Cgo transforms the input file into four output files: two Go source
files, a C file for 6c (or 8c or 5c), and a C file for gcc.
diff --git a/src/cmd/cgo/gcc.go b/src/cmd/cgo/gcc.go
index 5e12a6687..be3b8fe64 100644
--- a/src/cmd/cgo/gcc.go
+++ b/src/cmd/cgo/gcc.go
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-// Annotate Crefs in Prog with C types by parsing gcc debug output.
+// Annotate Ref in Prog with C types by parsing gcc debug output.
// Conversion of debug output to Go types.
package main
@@ -12,6 +12,8 @@ import (
"debug/dwarf"
"debug/elf"
"debug/macho"
+ "debug/pe"
+ "flag"
"fmt"
"go/ast"
"go/parser"
@@ -21,12 +23,64 @@ import (
"strings"
)
-func (p *Prog) loadDebugInfo() {
+var debugDefine = flag.Bool("debug-define", false, "print relevant #defines")
+var debugGcc = flag.Bool("debug-gcc", false, "print gcc invocations")
+
+var nameToC = map[string]string{
+ "schar": "signed char",
+ "uchar": "unsigned char",
+ "ushort": "unsigned short",
+ "uint": "unsigned int",
+ "ulong": "unsigned long",
+ "longlong": "long long",
+ "ulonglong": "unsigned long long",
+}
+
+// cname returns the C name to use for C.s.
+// The expansions are listed in nameToC and also
+// struct_foo becomes "struct foo", and similarly for
+// union and enum.
+func cname(s string) string {
+ if t, ok := nameToC[s]; ok {
+ return t
+ }
+
+ if strings.HasPrefix(s, "struct_") {
+ return "struct " + s[len("struct_"):]
+ }
+ if strings.HasPrefix(s, "union_") {
+ return "union " + s[len("union_"):]
+ }
+ if strings.HasPrefix(s, "enum_") {
+ return "enum " + s[len("enum_"):]
+ }
+ return s
+}
+
+// Translate rewrites f.AST, the original Go input, to remove
+// references to the imported package C, replacing them with
+// references to the equivalent Go types, functions, and variables.
+func (p *Package) Translate(f *File) {
+ for _, cref := range f.Ref {
+ // Convert C.ulong to C.unsigned long, etc.
+ cref.Name.C = cname(cref.Name.Go)
+ }
+ p.loadDefines(f)
+ needType := p.guessKinds(f)
+ if len(needType) > 0 {
+ p.loadDWARF(f, needType)
+ }
+ p.rewriteRef(f)
+}
+
+// loadDefines coerces gcc into spitting out the #defines in use
+// in the file f and saves relevant renamings in f.Name[name].Define.
+func (p *Package) loadDefines(f *File) {
var b bytes.Buffer
+ b.WriteString(builtinProlog)
+ b.WriteString(f.Preamble)
+ stdout := p.gccDefines(b.Bytes())
- b.WriteString(p.Preamble)
- stdout := p.gccPostProc(b.Bytes())
- defines := make(map[string]string)
for _, line := range strings.Split(stdout, "\n", -1) {
if len(line) < 9 || line[0:7] != "#define" {
continue
@@ -48,70 +102,112 @@ func (p *Prog) loadDebugInfo() {
val = strings.TrimSpace(line[tabIndex:])
}
- // Only allow string, character, and numeric constants. Ignoring #defines for
- // symbols allows those symbols to be referenced in Go, as they will be
- // translated by gcc later.
- _, err := strconv.Atoi(string(val[0]))
- if err == nil || val[0] == '\'' || val[0] == '"' {
- defines[key] = val
- }
- }
-
- // Construct a slice of unique names from p.Crefs.
- m := make(map[string]int)
- for _, c := range p.Crefs {
- // If we've already found this name as a define, it is not a Cref.
- if val, ok := defines[c.Name]; ok {
- _, err := parser.ParseExpr("", val, nil)
- if err != nil {
- fmt.Fprintf(os.Stderr, "The value in C.%s does not parse as a Go expression; cannot use.\n", c.Name)
- os.Exit(2)
+ if n := f.Name[key]; n != nil {
+ if *debugDefine {
+ fmt.Fprintf(os.Stderr, "#define %s %s\n", key, val)
}
-
- c.Context = "const"
- c.TypeName = false
- p.Constdef[c.Name] = val
- continue
+ n.Define = val
}
- m[c.Name] = -1
- }
- names := make([]string, 0, len(m))
- for name, _ := range m {
- i := len(names)
- names = names[0 : i+1]
- names[i] = name
- m[name] = i
}
+}
+// guessKinds tricks gcc into revealing the kind of each
+// name xxx for the references C.xxx in the Go input.
+// The kind is either a constant, type, or variable.
+func (p *Package) guessKinds(f *File) []*Name {
// Coerce gcc into telling us whether each name is
// a type, a value, or undeclared. We compile a function
// containing the line:
// name;
// If name is a type, gcc will print:
- // x.c:2: warning: useless type name in empty declaration
+ // cgo-test:2: warning: useless type name in empty declaration
// If name is a value, gcc will print
- // x.c:2: warning: statement with no effect
+ // cgo-test:2: warning: statement with no effect
// If name is undeclared, gcc will print
- // x.c:2: error: 'name' undeclared (first use in this function)
+ // cgo-test:2: error: 'name' undeclared (first use in this function)
// A line number directive causes the line number to
// correspond to the index in the names array.
- b.Reset()
- b.WriteString(p.Preamble)
+ //
+ // The line also has an enum declaration:
+ // name; enum { _cgo_enum_1 = name };
+ // If name is not a constant, gcc will print:
+ // cgo-test:4: error: enumerator value for '_cgo_enum_4' is not an integer constant
+ // we assume lines without that error are constants.
+
+ // Make list of names that need sniffing, type lookup.
+ toSniff := make([]*Name, 0, len(f.Name))
+ needType := make([]*Name, 0, len(f.Name))
+
+ for _, n := range f.Name {
+ // If we've already found this name as a #define
+ // and we can translate it as a constant value, do so.
+ if n.Define != "" {
+ ok := false
+ if _, err := strconv.Atoi(n.Define); err == nil {
+ ok = true
+ } else if n.Define[0] == '"' || n.Define[0] == '\'' {
+ _, err := parser.ParseExpr(fset, "", n.Define)
+ if err == nil {
+ ok = true
+ }
+ }
+ if ok {
+ n.Kind = "const"
+ n.Const = n.Define
+ continue
+ }
+
+ if isName(n.Define) {
+ n.C = n.Define
+ }
+ }
+
+ // If this is a struct, union, or enum type name,
+ // record the kind but also that we need type information.
+ if strings.HasPrefix(n.C, "struct ") || strings.HasPrefix(n.C, "union ") || strings.HasPrefix(n.C, "enum ") {
+ n.Kind = "type"
+ i := len(needType)
+ needType = needType[0 : i+1]
+ needType[i] = n
+ continue
+ }
+
+ i := len(toSniff)
+ toSniff = toSniff[0 : i+1]
+ toSniff[i] = n
+ }
+
+ if len(toSniff) == 0 {
+ return needType
+ }
+
+ var b bytes.Buffer
+ b.WriteString(builtinProlog)
+ b.WriteString(f.Preamble)
b.WriteString("void f(void) {\n")
b.WriteString("#line 0 \"cgo-test\"\n")
- for _, n := range names {
- b.WriteString(n)
- b.WriteString(";\n")
+ for i, n := range toSniff {
+ fmt.Fprintf(&b, "%s; enum { _cgo_enum_%d = %s }; /* cgo-test:%d */\n", n.C, i, n.C, i)
}
b.WriteString("}\n")
-
- kind := make(map[string]string)
- _, stderr := p.gccDebug(b.Bytes())
+ stderr := p.gccErrors(b.Bytes())
if stderr == "" {
- fatal("gcc produced no output")
+ fatal("gcc produced no output\non input:\n%s", b.Bytes())
}
+
+ names := make([]*Name, len(toSniff))
+ copy(names, toSniff)
+
+ isConst := make([]bool, len(toSniff))
+ for i := range isConst {
+ isConst[i] = true // until proven otherwise
+ }
+
for _, line := range strings.Split(stderr, "\n", -1) {
if len(line) < 9 || line[0:9] != "cgo-test:" {
+ if len(line) > 8 && line[0:8] == "<stdin>:" {
+ fatal("gcc produced unexpected output:\n%s\non input:\n%s", line, b.Bytes())
+ }
continue
}
line = line[9:]
@@ -127,28 +223,52 @@ func (p *Prog) loadDebugInfo() {
switch {
default:
continue
- case strings.Index(line, ": useless type name in empty declaration") >= 0:
+ case strings.Contains(line, ": useless type name in empty declaration"):
what = "type"
- case strings.Index(line, ": statement with no effect") >= 0:
- what = "value"
- case strings.Index(line, "undeclared") >= 0:
- what = "error"
+ isConst[i] = false
+ case strings.Contains(line, ": statement with no effect"):
+ what = "not-type" // const or func or var
+ case strings.Contains(line, "undeclared"):
+ error(token.NoPos, "%s", strings.TrimSpace(line[colon+1:]))
+ case strings.Contains(line, "is not an integer constant"):
+ isConst[i] = false
+ continue
}
- if old, ok := kind[names[i]]; ok && old != what {
- error(noPos, "inconsistent gcc output about C.%s", names[i])
+ n := toSniff[i]
+ if n == nil {
+ continue
}
- kind[names[i]] = what
+ toSniff[i] = nil
+ n.Kind = what
+
+ j := len(needType)
+ needType = needType[0 : j+1]
+ needType[j] = n
}
- for _, n := range names {
- if _, ok := kind[n]; !ok {
- error(noPos, "could not determine kind of name for C.%s", n)
+ for i, b := range isConst {
+ if b {
+ names[i].Kind = "const"
}
}
-
+ for _, n := range toSniff {
+ if n == nil {
+ continue
+ }
+ if n.Kind != "" {
+ continue
+ }
+ error(token.NoPos, "could not determine kind of name for C.%s", n.Go)
+ }
if nerrors > 0 {
- fatal("failed to interpret gcc output:\n%s", stderr)
+ fatal("unresolved names")
}
+ return needType
+}
+// loadDWARF parses the DWARF debug information generated
+// by gcc to learn the details of the constants, variables, and types
+// being referred to as C.xxx.
+func (p *Package) loadDWARF(f *File, names []*Name) {
// Extract the types from the DWARF section of an object
// from a well-formed C program. Gcc only generates DWARF info
// for symbols in the object file, so it is not enough to print the
@@ -157,19 +277,24 @@ func (p *Prog) loadDebugInfo() {
// typeof(names[i]) *__cgo__i;
// for each entry in names and then dereference the type we
// learn for __cgo__i.
- b.Reset()
- b.WriteString(p.Preamble)
+ var b bytes.Buffer
+ b.WriteString(builtinProlog)
+ b.WriteString(f.Preamble)
for i, n := range names {
- fmt.Fprintf(&b, "typeof(%s) *__cgo__%d;\n", n, i)
- }
- d, stderr := p.gccDebug(b.Bytes())
- if d == nil {
- fatal("gcc failed:\n%s\non input:\n%s", stderr, b.Bytes())
+ fmt.Fprintf(&b, "typeof(%s) *__cgo__%d;\n", n.C, i)
+ if n.Kind == "const" {
+ fmt.Fprintf(&b, "enum { __cgo_enum__%d = %s };\n", i, n.C)
+ }
}
+ d := p.gccDebug(b.Bytes())
// Scan DWARF info for top-level TagVariable entries with AttrName __cgo__i.
types := make([]dwarf.Type, len(names))
enums := make([]dwarf.Offset, len(names))
+ nameToIndex := make(map[*Name]int)
+ for i, n := range names {
+ nameToIndex[n] = i
+ }
r := d.Reader()
for {
e, err := r.Next()
@@ -192,9 +317,11 @@ func (p *Prog) loadDebugInfo() {
}
if e.Tag == dwarf.TagEnumerator {
entryName := e.Val(dwarf.AttrName).(string)
- i, ok := m[entryName]
- if ok {
- enums[i] = offset
+ if strings.HasPrefix(entryName, "__cgo_enum__") {
+ n, _ := strconv.Atoi(entryName[len("__cgo_enum__"):])
+ if 0 <= n && n < len(names) {
+ enums[n] = offset
+ }
}
}
}
@@ -234,97 +361,221 @@ func (p *Prog) loadDebugInfo() {
}
}
- // Record types and typedef information in Crefs.
+ // Record types and typedef information.
var conv typeConv
conv.Init(p.PtrSize)
- for _, c := range p.Crefs {
- i, ok := m[c.Name]
- if !ok {
- if _, ok := p.Constdef[c.Name]; !ok {
- fatal("Cref %s is no longer around", c.Name)
- }
- continue
- }
- c.TypeName = kind[c.Name] == "type"
+ for i, n := range names {
f, fok := types[i].(*dwarf.FuncType)
- if c.Context == "call" && !c.TypeName && fok {
- c.FuncType = conv.FuncType(f)
+ if n.Kind != "type" && fok {
+ n.Kind = "func"
+ n.FuncType = conv.FuncType(f)
} else {
- c.Type = conv.Type(types[i])
+ n.Type = conv.Type(types[i])
+ if enums[i] != 0 && n.Type.EnumValues != nil {
+ k := fmt.Sprintf("__cgo_enum__%d", i)
+ n.Kind = "const"
+ n.Const = strconv.Itoa64(n.Type.EnumValues[k])
+ // Remove injected enum to ensure the value will deep-compare
+ // equally in future loads of the same constant.
+ n.Type.EnumValues[k] = 0, false
+ }
}
}
- p.Typedef = conv.typedef
}
-func concat(a, b []string) []string {
- c := make([]string, len(a)+len(b))
- for i, s := range a {
- c[i] = s
+// rewriteRef rewrites all the C.xxx references in f.AST to refer to the
+// Go equivalents, now that we have figured out the meaning of all
+// the xxx.
+func (p *Package) rewriteRef(f *File) {
+ // Assign mangled names.
+ for _, n := range f.Name {
+ if n.Kind == "not-type" {
+ n.Kind = "var"
+ }
+ if n.Mangle == "" {
+ n.Mangle = "_C" + n.Kind + "_" + n.Go
+ }
}
- for i, s := range b {
- c[i+len(a)] = s
+
+ // Now that we have all the name types filled in,
+ // scan through the Refs to identify the ones that
+ // are trying to do a ,err call. Also check that
+ // functions are only used in calls.
+ for _, r := range f.Ref {
+ var expr ast.Expr = ast.NewIdent(r.Name.Mangle) // default
+ switch r.Context {
+ case "call", "call2":
+ if r.Name.Kind != "func" {
+ if r.Name.Kind == "type" {
+ r.Context = "type"
+ expr = r.Name.Type.Go
+ break
+ }
+ error(r.Pos(), "call of non-function C.%s", r.Name.Go)
+ break
+ }
+ if r.Context == "call2" {
+ if r.Name.FuncType.Result == nil {
+ error(r.Pos(), "assignment count mismatch: 2 = 0")
+ }
+ // Invent new Name for the two-result function.
+ n := f.Name["2"+r.Name.Go]
+ if n == nil {
+ n = new(Name)
+ *n = *r.Name
+ n.AddError = true
+ n.Mangle = "_C2func_" + n.Go
+ f.Name["2"+r.Name.Go] = n
+ }
+ expr = ast.NewIdent(n.Mangle)
+ r.Name = n
+ break
+ }
+ case "expr":
+ if r.Name.Kind == "func" {
+ error(r.Pos(), "must call C.%s", r.Name.Go)
+ }
+ if r.Name.Kind == "type" {
+ // Okay - might be new(T)
+ expr = r.Name.Type.Go
+ }
+ if r.Name.Kind == "var" {
+ expr = &ast.StarExpr{X: expr}
+ }
+
+ case "type":
+ if r.Name.Kind != "type" {
+ error(r.Pos(), "expression C.%s used as type", r.Name.Go)
+ } else {
+ expr = r.Name.Type.Go
+ }
+ default:
+ if r.Name.Kind == "func" {
+ error(r.Pos(), "must call C.%s", r.Name.Go)
+ }
+ }
+ *r.Expr = expr
}
- return c
}
-// gccDebug runs gcc -gdwarf-2 over the C program stdin and
-// returns the corresponding DWARF data and any messages
-// printed to standard error.
-func (p *Prog) gccDebug(stdin []byte) (*dwarf.Data, string) {
- machine := "-m32"
+// gccName returns the name of the compiler to run. Use $GCC if set in
+// the environment, otherwise just "gcc".
+
+func (p *Package) gccName() (ret string) {
+ if ret = os.Getenv("GCC"); ret == "" {
+ ret = "gcc"
+ }
+ return
+}
+
+// gccMachine returns the gcc -m flag to use, either "-m32" or "-m64".
+func (p *Package) gccMachine() string {
if p.PtrSize == 8 {
- machine = "-m64"
+ return "-m64"
}
+ return "-m32"
+}
- tmp := "_cgo_.o"
- base := []string{
- "gcc",
- machine,
+const gccTmp = "_cgo_.o"
+
+// gccCmd returns the gcc command line to use for compiling
+// the input.
+func (p *Package) gccCmd() []string {
+ return []string{
+ p.gccName(),
+ p.gccMachine(),
"-Wall", // many warnings
"-Werror", // warnings are errors
- "-o" + tmp, // write object to tmp
+ "-o" + gccTmp, // write object to tmp
"-gdwarf-2", // generate DWARF v2 debugging symbols
"-fno-eliminate-unused-debug-types", // gets rid of e.g. untyped enum otherwise
"-c", // do not link
"-xc", // input language is C
"-", // read input from standard input
}
- _, stderr, ok := run(stdin, concat(base, p.GccOptions))
- if !ok {
- return nil, string(stderr)
- }
+}
+
+// gccDebug runs gcc -gdwarf-2 over the C program stdin and
+// returns the corresponding DWARF data and any messages
+// printed to standard error.
+func (p *Package) gccDebug(stdin []byte) *dwarf.Data {
+ runGcc(stdin, append(p.gccCmd(), p.GccOptions...))
// Try to parse f as ELF and Mach-O and hope one works.
var f interface {
DWARF() (*dwarf.Data, os.Error)
}
var err os.Error
- if f, err = elf.Open(tmp); err != nil {
- if f, err = macho.Open(tmp); err != nil {
- fatal("cannot parse gcc output %s as ELF or Mach-O object", tmp)
+ if f, err = elf.Open(gccTmp); err != nil {
+ if f, err = macho.Open(gccTmp); err != nil {
+ if f, err = pe.Open(gccTmp); err != nil {
+ fatal("cannot parse gcc output %s as ELF or Mach-O or PE object", gccTmp)
+ }
}
}
d, err := f.DWARF()
if err != nil {
- fatal("cannot load DWARF debug information from %s: %s", tmp, err)
+ fatal("cannot load DWARF debug information from %s: %s", gccTmp, err)
}
- return d, ""
+ return d
}
-func (p *Prog) gccPostProc(stdin []byte) string {
- machine := "-m32"
- if p.PtrSize == 8 {
- machine = "-m64"
+// gccDefines runs gcc -E -dM -xc - over the C program stdin
+// and returns the corresponding standard output, which is the
+// #defines that gcc encountered while processing the input
+// and its included files.
+func (p *Package) gccDefines(stdin []byte) string {
+ base := []string{p.gccName(), p.gccMachine(), "-E", "-dM", "-xc", "-"}
+ stdout, _ := runGcc(stdin, append(base, p.GccOptions...))
+ return stdout
+}
+
+// gccErrors runs gcc over the C program stdin and returns
+// the errors that gcc prints. That is, this function expects
+// gcc to fail.
+func (p *Package) gccErrors(stdin []byte) string {
+ // TODO(rsc): require failure
+ args := append(p.gccCmd(), p.GccOptions...)
+ if *debugGcc {
+ fmt.Fprintf(os.Stderr, "$ %s <<EOF\n", strings.Join(args, " "))
+ os.Stderr.Write(stdin)
+ fmt.Fprint(os.Stderr, "EOF\n")
+ }
+ stdout, stderr, _ := run(stdin, args)
+ if *debugGcc {
+ os.Stderr.Write(stdout)
+ os.Stderr.Write(stderr)
}
+ return string(stderr)
+}
- base := []string{"gcc", machine, "-E", "-dM", "-xc", "-"}
- stdout, stderr, ok := run(stdin, concat(base, p.GccOptions))
+// runGcc runs the gcc command line args with stdin on standard input.
+// If the command exits with a non-zero exit status, runGcc prints
+// details about what was run and exits.
+// Otherwise runGcc returns the data written to standard output and standard error.
+// Note that for some of the uses we expect useful data back
+// on standard error, but for those uses gcc must still exit 0.
+func runGcc(stdin []byte, args []string) (string, string) {
+ if *debugGcc {
+ fmt.Fprintf(os.Stderr, "$ %s <<EOF\n", strings.Join(args, " "))
+ os.Stderr.Write(stdin)
+ fmt.Fprint(os.Stderr, "EOF\n")
+ }
+ stdout, stderr, ok := run(stdin, args)
+ if *debugGcc {
+ os.Stderr.Write(stdout)
+ os.Stderr.Write(stderr)
+ }
if !ok {
- return string(stderr)
+ fmt.Fprint(os.Stderr, "Error running gcc:\n")
+ fmt.Fprintf(os.Stderr, "$ %s <<EOF\n", strings.Join(args, " "))
+ os.Stderr.Write(stdin)
+ fmt.Fprint(os.Stderr, "EOF\n")
+ os.Stderr.Write(stderr)
+ os.Exit(2)
}
-
- return string(stdout)
+ return string(stdout), string(stderr)
}
// A typeConv is a translator from dwarf types to Go types
@@ -345,14 +596,14 @@ type typeConv struct {
string ast.Expr
ptrSize int64
-
- tagGen int
}
+var tagGen int
+var typedef = make(map[string]ast.Expr)
+
func (c *typeConv) Init(ptrSize int64) {
c.ptrSize = ptrSize
c.m = make(map[dwarf.Type]*Type)
- c.typedef = make(map[string]ast.Expr)
c.bool = c.Ident("bool")
c.byte = c.Ident("byte")
c.int8 = c.Ident("int8")
@@ -388,7 +639,7 @@ func base(dt dwarf.Type) dwarf.Type {
}
// Map from dwarf text names to aliases we use in package "C".
-var cnameMap = map[string]string{
+var dwarfToName = map[string]string{
"long int": "long",
"long unsigned int": "ulong",
"unsigned int": "uint",
@@ -415,6 +666,7 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type {
t.C = dtype.Common().Name
t.EnumValues = nil
c.m[dtype] = t
+
if t.Size < 0 {
// Unsized types are [0]byte
t.Size = 0
@@ -550,16 +802,16 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type {
// Have to give it a name to simulate C "struct foo" references.
tag := dt.StructName
if tag == "" {
- tag = "__" + strconv.Itoa(c.tagGen)
- c.tagGen++
+ tag = "__" + strconv.Itoa(tagGen)
+ tagGen++
} else if t.C == "" {
t.C = dt.Kind + " " + tag
}
- name := c.Ident("_C" + dt.Kind + "_" + tag)
+ name := c.Ident("_Ctype_" + dt.Kind + "_" + tag)
t.Go = name // publish before recursive calls
switch dt.Kind {
case "union", "class":
- c.typedef[name.Name()] = c.Opaque(t.Size)
+ typedef[name.Name] = c.Opaque(t.Size)
if t.C == "" {
t.C = fmt.Sprintf("typeof(unsigned char[%d])", t.Size)
}
@@ -569,7 +821,7 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type {
t.C = csyntax
}
t.Align = align
- c.typedef[name.Name()] = g
+ typedef[name.Name] = g
}
case *dwarf.TypedefType:
@@ -583,13 +835,13 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type {
t.Align = c.ptrSize
break
}
- name := c.Ident("_C_" + dt.Name)
+ name := c.Ident("_Ctypedef_" + dt.Name)
t.Go = name // publish before recursive call
sub := c.Type(dt.Type)
t.Size = sub.Size
t.Align = sub.Align
- if _, ok := c.typedef[name.Name()]; !ok {
- c.typedef[name.Name()] = sub.Go
+ if _, ok := typedef[name.Name]; !ok {
+ typedef[name.Name] = sub.Go
}
case *dwarf.UcharType:
@@ -628,12 +880,12 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type {
case *dwarf.AddrType, *dwarf.BoolType, *dwarf.CharType, *dwarf.IntType, *dwarf.FloatType, *dwarf.UcharType, *dwarf.UintType:
s := dtype.Common().Name
if s != "" {
- if ss, ok := cnameMap[s]; ok {
+ if ss, ok := dwarfToName[s]; ok {
s = ss
}
s = strings.Join(strings.Split(s, " ", -1), "") // strip spaces
- name := c.Ident("_C_" + s)
- c.typedef[name.Name()] = t.Go
+ name := c.Ident("_Ctype_" + s)
+ typedef[name.Name] = t.Go
t.Go = name
}
}
@@ -710,7 +962,9 @@ func (c *typeConv) FuncType(dtype *dwarf.FuncType) *FuncType {
}
// Identifier
-func (c *typeConv) Ident(s string) *ast.Ident { return ast.NewIdent(s) }
+func (c *typeConv) Ident(s string) *ast.Ident {
+ return ast.NewIdent(s)
+}
// Opaque type of n bytes.
func (c *typeConv) Opaque(n int64) ast.Expr {
@@ -736,13 +990,14 @@ func (c *typeConv) pad(fld []*ast.Field, size int64) []*ast.Field {
return fld
}
-// Struct conversion
+// Struct conversion: return Go and (6g) C syntax for type.
func (c *typeConv) Struct(dt *dwarf.StructType) (expr *ast.StructType, csyntax string, align int64) {
- csyntax = "struct { "
+ var buf bytes.Buffer
+ buf.WriteString("struct {")
fld := make([]*ast.Field, 0, 2*len(dt.Field)+1) // enough for padding around every field
off := int64(0)
- // Mangle struct fields that happen to be named Go keywords into
+ // Rename struct fields that happen to be named Go keywords into
// _{keyword}. Create a map from C ident -> Go ident. The Go ident will
// be mangled. Any existing identifier that already has the same name on
// the C-side will cause the Go-mangled version to be prefixed with _.
@@ -783,7 +1038,10 @@ func (c *typeConv) Struct(dt *dwarf.StructType) (expr *ast.StructType, csyntax s
fld[n] = &ast.Field{Names: []*ast.Ident{c.Ident(ident[f.Name])}, Type: t.Go}
off += t.Size
- csyntax += t.C + " " + f.Name + "; "
+ buf.WriteString(t.C)
+ buf.WriteString(" ")
+ buf.WriteString(f.Name)
+ buf.WriteString("; ")
if t.Align > align {
align = t.Align
}
@@ -795,7 +1053,8 @@ func (c *typeConv) Struct(dt *dwarf.StructType) (expr *ast.StructType, csyntax s
if off != dt.ByteSize {
fatal("struct size calculation error")
}
- csyntax += "}"
+ buf.WriteString("}")
+ csyntax = buf.String()
expr = &ast.StructType{Fields: &ast.FieldList{List: fld}}
return
}
diff --git a/src/cmd/cgo/main.go b/src/cmd/cgo/main.go
index 070146c9a..942bda5f4 100644
--- a/src/cmd/cgo/main.go
+++ b/src/cmd/cgo/main.go
@@ -11,13 +11,95 @@
package main
import (
+ "crypto/md5"
+ "flag"
"fmt"
"go/ast"
+ "go/token"
+ "io"
"os"
+ "reflect"
"strings"
)
-func usage() { fmt.Fprint(os.Stderr, "usage: cgo [compiler options] file.go ...\n") }
+// A Package collects information about the package we're going to write.
+type Package struct {
+ PackageName string // name of package
+ PackagePath string
+ PtrSize int64
+ GccOptions []string
+ Written map[string]bool
+ Name map[string]*Name // accumulated Name from Files
+ Typedef map[string]ast.Expr // accumulated Typedef from Files
+ ExpFunc []*ExpFunc // accumulated ExpFunc from Files
+ Decl []ast.Decl
+ GoFiles []string // list of Go files
+ GccFiles []string // list of gcc output files
+}
+
+// A File collects information about a single Go input file.
+type File struct {
+ AST *ast.File // parsed AST
+ Package string // Package name
+ Preamble string // C preamble (doc comment on import "C")
+ Ref []*Ref // all references to C.xxx in AST
+ ExpFunc []*ExpFunc // exported functions for this file
+ Name map[string]*Name // map from Go name to Name
+ Typedef map[string]ast.Expr // translations of all necessary types from C
+}
+
+// A Ref refers to an expression of the form C.xxx in the AST.
+type Ref struct {
+ Name *Name
+ Expr *ast.Expr
+ Context string // "type", "expr", "call", or "call2"
+}
+
+func (r *Ref) Pos() token.Pos {
+ return (*r.Expr).Pos()
+}
+
+// A Name collects information about C.xxx.
+type Name struct {
+ Go string // name used in Go referring to package C
+ Mangle string // name used in generated Go
+ C string // name used in C
+ Define string // #define expansion
+ Kind string // "const", "type", "var", "func", "not-type"
+ Type *Type // the type of xxx
+ FuncType *FuncType
+ AddError bool
+ Const string // constant definition
+}
+
+// A ExpFunc is an exported function, callable from C.
+// Such functions are identified in the Go input file
+// by doc comments containing the line //export ExpName
+type ExpFunc struct {
+ Func *ast.FuncDecl
+ ExpName string // name to use from C
+}
+
+// A Type collects information about a type in both the C and Go worlds.
+type Type struct {
+ Size int64
+ Align int64
+ C string
+ Go ast.Expr
+ EnumValues map[string]int64
+}
+
+// A FuncType collects information about a function type in both the C and Go worlds.
+type FuncType struct {
+ Params []*Type
+ Result *Type
+ Go *ast.FuncType
+}
+
+func usage() {
+ fmt.Fprint(os.Stderr, "usage: cgo [compiler options] file.go ...\n")
+ os.Exit(2)
+}
var ptrSizeMap = map[string]int64{
"386": 4,
@@ -25,43 +107,60 @@ var ptrSizeMap = map[string]int64{
"arm": 4,
}
-var expandName = map[string]string{
- "schar": "signed char",
- "uchar": "unsigned char",
- "ushort": "unsigned short",
- "uint": "unsigned int",
- "ulong": "unsigned long",
- "longlong": "long long",
- "ulonglong": "unsigned long long",
-}
+var cPrefix string
+
+var fset = token.NewFileSet()
+
+var dynobj = flag.String("dynimport", "", "if non-empty, print dynamic import data for that file")
func main() {
- args := os.Args
- if len(args) < 2 {
+ flag.Usage = usage
+ flag.Parse()
+
+ if *dynobj != "" {
+ // cgo -dynimport is essentially a separate helper command
+ // built into the cgo binary. It scans a gcc-produced executable
+ // and dumps information about the imported symbols and the
+ // imported libraries. The Make.pkg rules for cgo prepare an
+ // appropriate executable and then use its import information
+ // instead of needing to make the linkers duplicate all the
+ // specialized knowledge gcc has about where to look for imported
+ // symbols and which ones to use.
+ syms, imports := dynimport(*dynobj)
+ for _, sym := range syms {
+ fmt.Printf("#pragma dynimport %s %s %q\n", sym, sym, "")
+ }
+ for _, p := range imports {
+ fmt.Printf("#pragma dynimport %s %s %q\n", "_", "_", p)
+ }
+ return
+ }
+
+ args := flag.Args()
+ if len(args) < 1 {
usage()
- os.Exit(2)
}
// Find first arg that looks like a go file and assume everything before
// that are options to pass to gcc.
var i int
- for i = len(args) - 1; i > 0; i-- {
- if !strings.HasSuffix(args[i], ".go") {
+ for i = len(args); i > 0; i-- {
+ if !strings.HasSuffix(args[i-1], ".go") {
break
}
}
-
- i += 1
-
- gccOptions, goFiles := args[1:i], args[i:]
+ if i == len(args) {
+ usage()
+ }
+ gccOptions, goFiles := args[0:i], args[i:]
arch := os.Getenv("GOARCH")
if arch == "" {
fatal("$GOARCH is not set")
}
- ptrSize, ok := ptrSizeMap[arch]
- if !ok {
- fatal("unknown architecture %s", arch)
+ ptrSize := ptrSizeMap[arch]
+ if ptrSize == 0 {
+ fatal("unknown $GOARCH %q", arch)
}
// Clear locale variables so gcc emits English errors [sic].
@@ -69,75 +168,82 @@ func main() {
os.Setenv("LC_ALL", "C")
os.Setenv("LC_CTYPE", "C")
- p := new(Prog)
-
- p.PtrSize = ptrSize
- p.GccOptions = gccOptions
- p.Vardef = make(map[string]*Type)
- p.Funcdef = make(map[string]*FuncType)
- p.Enumdef = make(map[string]int64)
- p.Constdef = make(map[string]string)
- p.OutDefs = make(map[string]bool)
+ p := &Package{
+ PtrSize: ptrSize,
+ GccOptions: gccOptions,
+ Written: make(map[string]bool),
+ }
+ // Need a unique prefix for the global C symbols that
+ // we use to coordinate between gcc and ourselves.
+ // We already put _cgo_ at the beginning, so the main
+ // concern is other cgo wrappers for the same functions.
+ // Use the beginning of the md5 of the input to disambiguate.
+ h := md5.New()
for _, input := range goFiles {
- // Reset p.Preamble so that we don't end up with conflicting headers / defines
- p.Preamble = builtinProlog
- openProg(input, p)
- for _, cref := range p.Crefs {
- // Convert C.ulong to C.unsigned long, etc.
- if expand, ok := expandName[cref.Name]; ok {
- cref.Name = expand
- }
+ f, err := os.Open(input, os.O_RDONLY, 0)
+ if err != nil {
+ fatal("%s", err)
}
- p.loadDebugInfo()
- for _, cref := range p.Crefs {
+ io.Copy(h, f)
+ f.Close()
+ }
+ cPrefix = fmt.Sprintf("_%x", h.Sum()[0:6])
+
+ for _, input := range goFiles {
+ f := new(File)
+ // Reset f.Preamble so that we don't end up with conflicting headers / defines
+ f.Preamble = ""
+ f.ReadGo(input)
+ p.Translate(f)
+ for _, cref := range f.Ref {
switch cref.Context {
- case "const":
- // This came from a #define and we'll output it later.
- *cref.Expr = ast.NewIdent(cref.Name)
- break
- case "call":
- if !cref.TypeName {
- // Is an actual function call.
- pos := (*cref.Expr).Pos()
- *cref.Expr = &ast.Ident{Position: pos, Obj: ast.NewObj(ast.Err, pos, "_C_"+cref.Name)}
- p.Funcdef[cref.Name] = cref.FuncType
- break
- }
- *cref.Expr = cref.Type.Go
- case "expr":
- if cref.TypeName {
- error((*cref.Expr).Pos(), "type C.%s used as expression", cref.Name)
- }
- // If the expression refers to an enumerated value, then
- // place the identifier for the value and add it to Enumdef so
- // it will be declared as a constant in the later stage.
- if cref.Type.EnumValues != nil {
- *cref.Expr = ast.NewIdent(cref.Name)
- p.Enumdef[cref.Name] = cref.Type.EnumValues[cref.Name]
+ case "call", "call2":
+ if cref.Name.Kind != "type" {
break
}
- // Reference to C variable.
- // We declare a pointer and arrange to have it filled in.
- *cref.Expr = &ast.StarExpr{X: ast.NewIdent("_C_" + cref.Name)}
- p.Vardef[cref.Name] = cref.Type
- case "type":
- if !cref.TypeName {
- error((*cref.Expr).Pos(), "expression C.%s used as type", cref.Name)
- }
- *cref.Expr = cref.Type.Go
+ *cref.Expr = cref.Name.Type.Go
}
}
if nerrors > 0 {
os.Exit(2)
}
- pkg := p.Package
+ pkg := f.Package
if dir := os.Getenv("CGOPKGPATH"); dir != "" {
pkg = dir + "/" + pkg
}
p.PackagePath = pkg
- p.writeOutput(input)
+ p.writeOutput(f, input)
+
+ p.Record(f)
}
p.writeDefs()
+ if nerrors > 0 {
+ os.Exit(2)
+ }
+}
+
+// Record what needs to be recorded about f.
+func (p *Package) Record(f *File) {
+ if p.PackageName == "" {
+ p.PackageName = f.Package
+ } else if p.PackageName != f.Package {
+ error(token.NoPos, "inconsistent package names: %s, %s", p.PackageName, f.Package)
+ }
+
+ if p.Name == nil {
+ p.Name = f.Name
+ } else {
+ for k, v := range f.Name {
+ if p.Name[k] == nil {
+ p.Name[k] = v
+ } else if !reflect.DeepEqual(p.Name[k], v) {
+ error(token.NoPos, "inconsistent definitions for C.%s", k)
+ }
+ }
+ }
+
+ p.ExpFunc = append(p.ExpFunc, f.ExpFunc...)
+ p.Decl = append(p.Decl, f.AST.Decls...)
}
diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go
index 7cdf483f0..c3f9ae60b 100644
--- a/src/cmd/cgo/out.go
+++ b/src/cmd/cgo/out.go
@@ -5,6 +5,9 @@
package main
import (
+ "bytes"
+ "debug/elf"
+ "debug/macho"
"fmt"
"go/ast"
"go/printer"
@@ -13,30 +16,9 @@ import (
"strings"
)
-func creat(name string) *os.File {
- f, err := os.Open(name, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0666)
- if err != nil {
- fatal("%s", err)
- }
- return f
-}
-
-func slashToUnderscore(c int) int {
- if c == '/' {
- c = '_'
- }
- return c
-}
-
// writeDefs creates output files to be compiled by 6g, 6c, and gcc.
// (The comments here say 6g and 6c but the code applies to the 8 and 5 tools too.)
-func (p *Prog) writeDefs() {
- pkgroot := os.Getenv("GOROOT") + "/pkg/" + os.Getenv("GOOS") + "_" + os.Getenv("GOARCH")
- path := p.PackagePath
- if !strings.HasPrefix(path, "/") {
- path = pkgroot + "/" + path
- }
-
+func (p *Package) writeDefs() {
// The path for the shared object is slash-free so that ELF loaders
// will treat it as a relative path. We rewrite slashes to underscores.
sopath := "cgo_" + strings.Map(slashToUnderscore, p.PackagePath)
@@ -48,229 +30,305 @@ func (p *Prog) writeDefs() {
fgo2 := creat("_cgo_gotypes.go")
fc := creat("_cgo_defun.c")
+ fm := creat("_cgo_main.c")
+
+ // Write C main file for using gcc to resolve imports.
+ fmt.Fprintf(fm, "int main() { return 0; }\n")
+ fmt.Fprintf(fm, "int crosscall2;\n\n")
// Write second Go output: definitions of _C_xxx.
// In a separate file so that the import of "unsafe" does not
// pollute the original file.
- fmt.Fprintf(fgo2, "// Created by cgo - DO NOT EDIT\n")
- fmt.Fprintf(fgo2, "package %s\n\n", p.Package)
+ fmt.Fprintf(fgo2, "// Created by cgo - DO NOT EDIT\n\n")
+ fmt.Fprintf(fgo2, "package %s\n\n", p.PackageName)
fmt.Fprintf(fgo2, "import \"unsafe\"\n\n")
+ fmt.Fprintf(fgo2, "import \"os\"\n\n")
+ fmt.Fprintf(fgo2, "import _ \"runtime/cgo\"\n\n")
fmt.Fprintf(fgo2, "type _ unsafe.Pointer\n\n")
+ fmt.Fprintf(fgo2, "func _Cerrno(dst *os.Error, x int) { *dst = os.Errno(x) }\n")
- for name, def := range p.Typedef {
+ for name, def := range typedef {
fmt.Fprintf(fgo2, "type %s ", name)
- printer.Fprint(fgo2, def)
+ printer.Fprint(fgo2, fset, def)
fmt.Fprintf(fgo2, "\n")
}
- fmt.Fprintf(fgo2, "type _C_void [0]byte\n")
+ fmt.Fprintf(fgo2, "type _Ctype_void [0]byte\n")
+
+ fmt.Fprintf(fc, cProlog)
+
+ var cVars []string
+ for _, n := range p.Name {
+ if n.Kind != "var" {
+ continue
+ }
+ cVars = append(cVars, n.C)
- fmt.Fprintf(fc, cProlog, soprefix, soprefix, soprefix, soprefix, soprefix)
+ fmt.Fprintf(fm, "extern char %s[];\n", n.C)
+ fmt.Fprintf(fm, "void *_cgohack_%s = %s;\n\n", n.C, n.C)
+
+ fmt.Fprintf(fc, "extern byte *%s;\n", n.C)
+ fmt.Fprintf(fc, "void *·%s = &%s;\n", n.Mangle, n.C)
+ fmt.Fprintf(fc, "\n")
- for name, def := range p.Vardef {
- fmt.Fprintf(fc, "#pragma dynimport ·_C_%s %s \"%s%s.so\"\n", name, name, soprefix, sopath)
- fmt.Fprintf(fgo2, "var _C_%s ", name)
- printer.Fprint(fgo2, &ast.StarExpr{X: def.Go})
+ fmt.Fprintf(fgo2, "var %s ", n.Mangle)
+ printer.Fprint(fgo2, fset, &ast.StarExpr{X: n.Type.Go})
fmt.Fprintf(fgo2, "\n")
}
fmt.Fprintf(fc, "\n")
- for name, value := range p.Constdef {
- fmt.Fprintf(fgo2, "const %s = %s\n", name, value)
- }
-
- for name, value := range p.Enumdef {
- fmt.Fprintf(fgo2, "const %s = %d\n", name, value)
+ for _, n := range p.Name {
+ if n.Const != "" {
+ fmt.Fprintf(fgo2, "const _Cconst_%s = %s\n", n.Go, n.Const)
+ }
}
fmt.Fprintf(fgo2, "\n")
- for name, def := range p.Funcdef {
- // Go func declaration.
- d := &ast.FuncDecl{
- Name: ast.NewIdent("_C_" + name),
- Type: def.Go,
+ for _, n := range p.Name {
+ if n.FuncType != nil {
+ p.writeDefsFunc(fc, fgo2, n, soprefix, sopath)
}
- printer.Fprint(fgo2, d)
- fmt.Fprintf(fgo2, "\n")
+ }
- if name == "CString" || name == "GoString" {
- // The builtins are already defined in the C prolog.
- continue
+ p.writeExports(fgo2, fc, fm)
+
+ fgo2.Close()
+ fc.Close()
+}
+
+func dynimport(obj string) (syms, imports []string) {
+ var f interface {
+ ImportedLibraries() ([]string, os.Error)
+ ImportedSymbols() ([]string, os.Error)
+ }
+ var isMacho bool
+ var err1, err2 os.Error
+ if f, err1 = elf.Open(obj); err1 != nil {
+ if f, err2 = macho.Open(obj); err2 != nil {
+ fatal("cannot parse %s as ELF (%v) or Mach-O (%v)", obj, err1, err2)
}
+ isMacho = true
+ }
- // Construct a gcc struct matching the 6c argument frame.
- // Assumes that in gcc, char is 1 byte, short 2 bytes, int 4 bytes, long long 8 bytes.
- // These assumptions are checked by the gccProlog.
- // Also assumes that 6c convention is to word-align the
- // input and output parameters.
- structType := "struct {\n"
- off := int64(0)
- npad := 0
- for i, t := range def.Params {
- if off%t.Align != 0 {
- pad := t.Align - off%t.Align
- structType += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad)
- off += pad
- npad++
+ var err os.Error
+ syms, err = f.ImportedSymbols()
+ if err != nil {
+ fatal("cannot load dynamic symbols: %v", err)
+ }
+ if isMacho {
+ // remove leading _ that OS X insists on
+ for i, s := range syms {
+ if len(s) >= 2 && s[0] == '_' {
+ syms[i] = s[1:]
}
- structType += fmt.Sprintf("\t\t%s p%d;\n", t.C, i)
- off += t.Size
}
- if off%p.PtrSize != 0 {
- pad := p.PtrSize - off%p.PtrSize
- structType += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad)
+ }
+
+ imports, err = f.ImportedLibraries()
+ if err != nil {
+ fatal("cannot load dynamic imports: %v", err)
+ }
+
+ return
+}
+
+// Construct a gcc struct matching the 6c argument frame.
+// Assumes that in gcc, char is 1 byte, short 2 bytes, int 4 bytes, long long 8 bytes.
+// These assumptions are checked by the gccProlog.
+// Also assumes that 6c convention is to word-align the
+// input and output parameters.
+func (p *Package) structType(n *Name) (string, int64) {
+ var buf bytes.Buffer
+ fmt.Fprint(&buf, "struct {\n")
+ off := int64(0)
+ for i, t := range n.FuncType.Params {
+ if off%t.Align != 0 {
+ pad := t.Align - off%t.Align
+ fmt.Fprintf(&buf, "\t\tchar __pad%d[%d];\n", off, pad)
off += pad
- npad++
- }
- if t := def.Result; t != nil {
- if off%t.Align != 0 {
- pad := t.Align - off%t.Align
- structType += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad)
- off += pad
- npad++
- }
- structType += fmt.Sprintf("\t\t%s r;\n", t.C)
- off += t.Size
}
- if off%p.PtrSize != 0 {
- pad := p.PtrSize - off%p.PtrSize
- structType += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad)
+ fmt.Fprintf(&buf, "\t\t%s p%d;\n", t.C, i)
+ off += t.Size
+ }
+ if off%p.PtrSize != 0 {
+ pad := p.PtrSize - off%p.PtrSize
+ fmt.Fprintf(&buf, "\t\tchar __pad%d[%d];\n", off, pad)
+ off += pad
+ }
+ if t := n.FuncType.Result; t != nil {
+ if off%t.Align != 0 {
+ pad := t.Align - off%t.Align
+ fmt.Fprintf(&buf, "\t\tchar __pad%d[%d];\n", off, pad)
off += pad
- npad++
}
- if len(def.Params) == 0 && def.Result == nil {
- structType += "\t\tchar unused;\n" // avoid empty struct
- off++
+ qual := ""
+ if t.C[len(t.C)-1] == '*' {
+ qual = "const "
}
- structType += "\t}"
- argSize := off
+ fmt.Fprintf(&buf, "\t\t%s%s r;\n", qual, t.C)
+ off += t.Size
+ }
+ if off%p.PtrSize != 0 {
+ pad := p.PtrSize - off%p.PtrSize
+ fmt.Fprintf(&buf, "\t\tchar __pad%d[%d];\n", off, pad)
+ off += pad
+ }
+ if n.AddError {
+ fmt.Fprint(&buf, "\t\tvoid *e[2]; /* os.Error */\n")
+ off += 2 * p.PtrSize
+ }
+ if off == 0 {
+ fmt.Fprintf(&buf, "\t\tchar unused;\n") // avoid empty struct
+ off++
+ }
+ fmt.Fprintf(&buf, "\t}")
+ return buf.String(), off
+}
- // C wrapper calls into gcc, passing a pointer to the argument frame.
- // Also emit #pragma to get a pointer to the gcc wrapper.
- fmt.Fprintf(fc, "#pragma dynimport _cgo_%s _cgo_%s \"%s%s.so\"\n", name, name, soprefix, sopath)
- fmt.Fprintf(fc, "void (*_cgo_%s)(void*);\n", name)
- fmt.Fprintf(fc, "\n")
- fmt.Fprintf(fc, "void\n")
- fmt.Fprintf(fc, "·_C_%s(struct{uint8 x[%d];}p)\n", name, argSize)
- fmt.Fprintf(fc, "{\n")
- fmt.Fprintf(fc, "\tcgocall(_cgo_%s, &p);\n", name)
- fmt.Fprintf(fc, "}\n")
- fmt.Fprintf(fc, "\n")
+func (p *Package) writeDefsFunc(fc, fgo2 *os.File, n *Name, soprefix, sopath string) {
+ name := n.Go
+ gtype := n.FuncType.Go
+ if n.AddError {
+ // Add "os.Error" to return type list.
+ // Type list is known to be 0 or 1 element - it's a C function.
+ err := &ast.Field{Type: ast.NewIdent("os.Error")}
+ l := gtype.Results.List
+ if len(l) == 0 {
+ l = []*ast.Field{err}
+ } else {
+ l = []*ast.Field{l[0], err}
+ }
+ t := new(ast.FuncType)
+ *t = *gtype
+ t.Results = &ast.FieldList{List: l}
+ gtype = t
}
- p.writeExports(fgo2, fc)
+ // Go func declaration.
+ d := &ast.FuncDecl{
+ Name: ast.NewIdent(n.Mangle),
+ Type: gtype,
+ }
+ printer.Fprint(fgo2, fset, d)
+ fmt.Fprintf(fgo2, "\n")
- fgo2.Close()
- fc.Close()
+ if name == "CString" || name == "GoString" || name == "GoStringN" {
+ // The builtins are already defined in the C prolog.
+ return
+ }
+
+ var argSize int64
+ _, argSize = p.structType(n)
+
+ // C wrapper calls into gcc, passing a pointer to the argument frame.
+ fmt.Fprintf(fc, "void _cgo%s%s(void*);\n", cPrefix, n.Mangle)
+ fmt.Fprintf(fc, "\n")
+ fmt.Fprintf(fc, "void\n")
+ fmt.Fprintf(fc, "·%s(struct{uint8 x[%d];}p)\n", n.Mangle, argSize)
+ fmt.Fprintf(fc, "{\n")
+ fmt.Fprintf(fc, "\truntime·cgocall(_cgo%s%s, &p);\n", cPrefix, n.Mangle)
+ if n.AddError {
+ // gcc leaves errno in first word of interface at end of p.
+ // check whether it is zero; if so, turn interface into nil.
+ // if not, turn interface into errno.
+ // Go init function initializes ·_Cerrno with an os.Errno
+ // for us to copy.
+ fmt.Fprintln(fc, ` {
+ int32 e;
+ void **v;
+ v = (void**)(&p+1) - 2; /* v = final two void* of p */
+ e = *(int32*)v;
+ v[0] = (void*)0xdeadbeef;
+ v[1] = (void*)0xdeadbeef;
+ if(e == 0) {
+ /* nil interface */
+ v[0] = 0;
+ v[1] = 0;
+ } else {
+ ·_Cerrno(v, e); /* fill in v as os.Error for errno e */
+ }
+ }`)
+ }
+ fmt.Fprintf(fc, "}\n")
+ fmt.Fprintf(fc, "\n")
}
// writeOutput creates stubs for a specific source file to be compiled by 6g
// (The comments here say 6g and 6c but the code applies to the 8 and 5 tools too.)
-func (p *Prog) writeOutput(srcfile string) {
+func (p *Package) writeOutput(f *File, srcfile string) {
base := srcfile
if strings.HasSuffix(base, ".go") {
base = base[0 : len(base)-3]
}
+ base = strings.Map(slashToUnderscore, base)
fgo1 := creat(base + ".cgo1.go")
fgcc := creat(base + ".cgo2.c")
+ p.GoFiles = append(p.GoFiles, base+".cgo1.go")
+ p.GccFiles = append(p.GccFiles, base+".cgo2.c")
+
// Write Go output: Go input with rewrites of C.xxx to _C_xxx.
- fmt.Fprintf(fgo1, "// Created by cgo - DO NOT EDIT\n")
+ fmt.Fprintf(fgo1, "// Created by cgo - DO NOT EDIT\n\n")
fmt.Fprintf(fgo1, "//line %s:1\n", srcfile)
- printer.Fprint(fgo1, p.AST)
+ printer.Fprint(fgo1, fset, f.AST)
// While we process the vars and funcs, also write 6c and gcc output.
// Gcc output starts with the preamble.
- fmt.Fprintf(fgcc, "%s\n", p.Preamble)
+ fmt.Fprintf(fgcc, "%s\n", f.Preamble)
fmt.Fprintf(fgcc, "%s\n", gccProlog)
- for name, def := range p.Funcdef {
- _, ok := p.OutDefs[name]
- if name == "CString" || name == "GoString" || ok {
- // The builtins are already defined in the C prolog, and we don't
- // want to duplicate function definitions we've already done.
- continue
- }
- p.OutDefs[name] = true
-
- // Construct a gcc struct matching the 6c argument frame.
- // Assumes that in gcc, char is 1 byte, short 2 bytes, int 4 bytes, long long 8 bytes.
- // These assumptions are checked by the gccProlog.
- // Also assumes that 6c convention is to word-align the
- // input and output parameters.
- structType := "struct {\n"
- off := int64(0)
- npad := 0
- for i, t := range def.Params {
- if off%t.Align != 0 {
- pad := t.Align - off%t.Align
- structType += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad)
- off += pad
- npad++
- }
- structType += fmt.Sprintf("\t\t%s p%d;\n", t.C, i)
- off += t.Size
+ for _, n := range f.Name {
+ if n.FuncType != nil {
+ p.writeOutputFunc(fgcc, n)
}
- if off%p.PtrSize != 0 {
- pad := p.PtrSize - off%p.PtrSize
- structType += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad)
- off += pad
- npad++
- }
- if t := def.Result; t != nil {
- if off%t.Align != 0 {
- pad := t.Align - off%t.Align
- structType += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad)
- off += pad
- npad++
- }
- structType += fmt.Sprintf("\t\t%s r;\n", t.C)
- off += t.Size
- }
- if off%p.PtrSize != 0 {
- pad := p.PtrSize - off%p.PtrSize
- structType += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad)
- off += pad
- npad++
- }
- if len(def.Params) == 0 && def.Result == nil {
- structType += "\t\tchar unused;\n" // avoid empty struct
- off++
- }
- structType += "\t}"
-
- // Gcc wrapper unpacks the C argument struct
- // and calls the actual C function.
- fmt.Fprintf(fgcc, "void\n")
- fmt.Fprintf(fgcc, "_cgo_%s(void *v)\n", name)
- fmt.Fprintf(fgcc, "{\n")
- fmt.Fprintf(fgcc, "\t%s *a = v;\n", structType)
- fmt.Fprintf(fgcc, "\t")
- if def.Result != nil {
- fmt.Fprintf(fgcc, "a->r = ")
- }
- fmt.Fprintf(fgcc, "%s(", name)
- for i := range def.Params {
- if i > 0 {
- fmt.Fprintf(fgcc, ", ")
- }
- fmt.Fprintf(fgcc, "a->p%d", i)
- }
- fmt.Fprintf(fgcc, ");\n")
- fmt.Fprintf(fgcc, "}\n")
- fmt.Fprintf(fgcc, "\n")
}
fgo1.Close()
fgcc.Close()
}
-// Write out the various stubs we need to support functions exported
-// from Go so that they are callable from C.
-func (p *Prog) writeExports(fgo2, fc *os.File) {
- if len(p.ExpFuncs) == 0 {
+func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) {
+ name := n.Mangle
+ if name == "_Cfunc_CString" || name == "_Cfunc_GoString" || name == "_Cfunc_GoStringN" || p.Written[name] {
+ // The builtins are already defined in the C prolog, and we don't
+ // want to duplicate function definitions we've already done.
return
}
+ p.Written[name] = true
+
+ ctype, _ := p.structType(n)
+
+ // Gcc wrapper unpacks the C argument struct
+ // and calls the actual C function.
+ fmt.Fprintf(fgcc, "void\n")
+ fmt.Fprintf(fgcc, "_cgo%s%s(void *v)\n", cPrefix, n.Mangle)
+ fmt.Fprintf(fgcc, "{\n")
+ if n.AddError {
+ fmt.Fprintf(fgcc, "\tint e;\n") // assuming 32 bit (see comment above structType)
+ fmt.Fprintf(fgcc, "\terrno = 0;\n")
+ }
+ fmt.Fprintf(fgcc, "\t%s *a = v;\n", ctype)
+ fmt.Fprintf(fgcc, "\t")
+ if n.FuncType.Result != nil {
+ fmt.Fprintf(fgcc, "a->r = ")
+ }
+ fmt.Fprintf(fgcc, "%s(", n.C)
+ for i := range n.FuncType.Params {
+ if i > 0 {
+ fmt.Fprintf(fgcc, ", ")
+ }
+ fmt.Fprintf(fgcc, "a->p%d", i)
+ }
+ fmt.Fprintf(fgcc, ");\n")
+ if n.AddError {
+ fmt.Fprintf(fgcc, "\t*(int*)(a->e) = errno;\n")
+ }
+ fmt.Fprintf(fgcc, "}\n")
+ fmt.Fprintf(fgcc, "\n")
+}
+// Write out the various stubs we need to support functions exported
+// from Go so that they are callable from C.
+func (p *Package) writeExports(fgo2, fc, fm *os.File) {
fgcc := creat("_cgo_export.c")
fgcch := creat("_cgo_export.h")
@@ -280,17 +338,17 @@ func (p *Prog) writeExports(fgo2, fc *os.File) {
fmt.Fprintf(fgcc, "/* Created by cgo - DO NOT EDIT. */\n")
fmt.Fprintf(fgcc, "#include \"_cgo_export.h\"\n")
- for _, exp := range p.ExpFuncs {
+ for _, exp := range p.ExpFunc {
fn := exp.Func
// Construct a gcc struct matching the 6c argument and
// result frame.
- structType := "struct {\n"
+ ctype := "struct {\n"
off := int64(0)
npad := 0
if fn.Recv != nil {
t := p.cgoType(fn.Recv.List[0].Type)
- structType += fmt.Sprintf("\t\t%s recv;\n", t.C)
+ ctype += fmt.Sprintf("\t\t%s recv;\n", t.C)
off += t.Size
}
fntype := fn.Type
@@ -299,16 +357,16 @@ func (p *Prog) writeExports(fgo2, fc *os.File) {
t := p.cgoType(atype)
if off%t.Align != 0 {
pad := t.Align - off%t.Align
- structType += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad)
+ ctype += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad)
off += pad
npad++
}
- structType += fmt.Sprintf("\t\t%s p%d;\n", t.C, i)
+ ctype += fmt.Sprintf("\t\t%s p%d;\n", t.C, i)
off += t.Size
})
if off%p.PtrSize != 0 {
pad := p.PtrSize - off%p.PtrSize
- structType += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad)
+ ctype += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad)
off += pad
npad++
}
@@ -317,24 +375,24 @@ func (p *Prog) writeExports(fgo2, fc *os.File) {
t := p.cgoType(atype)
if off%t.Align != 0 {
pad := t.Align - off%t.Align
- structType += fmt.Sprintf("\t\tchar __pad%d[%d]\n", npad, pad)
+ ctype += fmt.Sprintf("\t\tchar __pad%d[%d]\n", npad, pad)
off += pad
npad++
}
- structType += fmt.Sprintf("\t\t%s r%d;\n", t.C, i)
+ ctype += fmt.Sprintf("\t\t%s r%d;\n", t.C, i)
off += t.Size
})
if off%p.PtrSize != 0 {
pad := p.PtrSize - off%p.PtrSize
- structType += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad)
+ ctype += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad)
off += pad
npad++
}
- if structType == "struct {\n" {
- structType += "\t\tchar unused;\n" // avoid empty struct
+ if ctype == "struct {\n" {
+ ctype += "\t\tchar unused;\n" // avoid empty struct
off++
}
- structType += "\t}"
+ ctype += "\t}"
// Get the return type of the wrapper function
// compiled by gcc.
@@ -370,10 +428,10 @@ func (p *Prog) writeExports(fgo2, fc *os.File) {
s += ")"
fmt.Fprintf(fgcch, "\nextern %s;\n", s)
- fmt.Fprintf(fgcc, "extern _cgoexp_%s(void *, int);\n", exp.ExpName)
+ fmt.Fprintf(fgcc, "extern _cgoexp%s_%s(void *, int);\n", cPrefix, exp.ExpName)
fmt.Fprintf(fgcc, "\n%s\n", s)
fmt.Fprintf(fgcc, "{\n")
- fmt.Fprintf(fgcc, "\t%s a;\n", structType)
+ fmt.Fprintf(fgcc, "\t%s a;\n", ctype)
if gccResult != "void" && (len(fntype.Results.List) > 1 || len(fntype.Results.List[0].Names) > 1) {
fmt.Fprintf(fgcc, "\t%s r;\n", gccResult)
}
@@ -384,7 +442,7 @@ func (p *Prog) writeExports(fgo2, fc *os.File) {
func(i int, atype ast.Expr) {
fmt.Fprintf(fgcc, "\ta.p%d = p%d;\n", i, i)
})
- fmt.Fprintf(fgcc, "\tcrosscall2(_cgoexp_%s, &a, (int) sizeof a);\n", exp.ExpName)
+ fmt.Fprintf(fgcc, "\tcrosscall2(_cgoexp%s_%s, &a, (int) sizeof a);\n", cPrefix, exp.ExpName)
if gccResult != "void" {
if len(fntype.Results.List) == 1 && len(fntype.Results.List[0].Names) <= 1 {
fmt.Fprintf(fgcc, "\treturn a.r0;\n")
@@ -399,27 +457,28 @@ func (p *Prog) writeExports(fgo2, fc *os.File) {
fmt.Fprintf(fgcc, "}\n")
// Build the wrapper function compiled by 6c/8c
- goname := exp.Func.Name.Name()
+ goname := exp.Func.Name.Name
if fn.Recv != nil {
- goname = "_cgoexpwrap_" + fn.Recv.List[0].Names[0].Name() + "_" + goname
+ goname = "_cgoexpwrap" + cPrefix + "_" + fn.Recv.List[0].Names[0].Name + "_" + goname
}
- fmt.Fprintf(fc, "#pragma dynexport _cgoexp_%s _cgoexp_%s\n", exp.ExpName, exp.ExpName)
fmt.Fprintf(fc, "extern void ·%s();\n", goname)
fmt.Fprintf(fc, "\nvoid\n")
- fmt.Fprintf(fc, "_cgoexp_%s(void *a, int32 n)\n", exp.ExpName)
+ fmt.Fprintf(fc, "_cgoexp%s_%s(void *a, int32 n)\n", cPrefix, exp.ExpName)
fmt.Fprintf(fc, "{\n")
- fmt.Fprintf(fc, "\tcgocallback(·%s, a, n);\n", goname)
+ fmt.Fprintf(fc, "\truntime·cgocallback(·%s, a, n);\n", goname)
fmt.Fprintf(fc, "}\n")
+ fmt.Fprintf(fm, "int _cgoexp%s_%s;\n", cPrefix, exp.ExpName)
+
// Calling a function with a receiver from C requires
// a Go wrapper function.
if fn.Recv != nil {
fmt.Fprintf(fgo2, "func %s(recv ", goname)
- printer.Fprint(fgo2, fn.Recv.List[0].Type)
+ printer.Fprint(fgo2, fset, fn.Recv.List[0].Type)
forFieldList(fntype.Params,
func(i int, atype ast.Expr) {
- fmt.Fprintf(fgo2, ", p%d", i)
- printer.Fprint(fgo2, atype)
+ fmt.Fprintf(fgo2, ", p%d ", i)
+ printer.Fprint(fgo2, fset, atype)
})
fmt.Fprintf(fgo2, ")")
if gccResult != "void" {
@@ -429,7 +488,7 @@ func (p *Prog) writeExports(fgo2, fc *os.File) {
if i > 0 {
fmt.Fprint(fgo2, ", ")
}
- printer.Fprint(fgo2, atype)
+ printer.Fprint(fgo2, fset, atype)
})
fmt.Fprint(fgo2, ")")
}
@@ -493,7 +552,7 @@ var goTypes = map[string]*Type{
}
// Map an ast type to a Type.
-func (p *Prog) cgoType(e ast.Expr) *Type {
+func (p *Package) cgoType(e ast.Expr) *Type {
switch t := e.(type) {
case *ast.StarExpr:
x := p.cgoType(t.X)
@@ -515,7 +574,7 @@ func (p *Prog) cgoType(e ast.Expr) *Type {
case *ast.Ident:
// Look up the type in the top level declarations.
// TODO: Handle types defined within a function.
- for _, d := range p.AST.Decls {
+ for _, d := range p.Decl {
gd, ok := d.(*ast.GenDecl)
if !ok || gd.Tok != token.TYPE {
continue
@@ -525,30 +584,35 @@ func (p *Prog) cgoType(e ast.Expr) *Type {
if !ok {
continue
}
- if ts.Name.Name() == t.Name() {
+ if ts.Name.Name == t.Name {
return p.cgoType(ts.Type)
}
}
}
- for name, def := range p.Typedef {
- if name == t.Name() {
+ for name, def := range typedef {
+ if name == t.Name {
return p.cgoType(def)
}
}
- if t.Name() == "uintptr" {
+ if t.Name == "uintptr" {
return &Type{Size: p.PtrSize, Align: p.PtrSize, C: "uintptr"}
}
- if t.Name() == "string" {
+ if t.Name == "string" {
return &Type{Size: p.PtrSize + 4, Align: p.PtrSize, C: "GoString"}
}
- if r, ok := goTypes[t.Name()]; ok {
+ if r, ok := goTypes[t.Name]; ok {
if r.Align > p.PtrSize {
r.Align = p.PtrSize
}
return r
}
+ case *ast.SelectorExpr:
+ id, ok := t.X.(*ast.Ident)
+ if ok && id.Name == "unsafe" && t.Sel.Name == "Pointer" {
+ return &Type{Size: p.PtrSize, Align: p.PtrSize, C: "void*"}
+ }
}
- error(e.Pos(), "unrecognized Go type %v", e)
+ error(e.Pos(), "unrecognized Go type %T", e)
return &Type{Size: 4, Align: 4, C: "int"}
}
@@ -568,11 +632,15 @@ typedef long long __cgo_long_long;
__cgo_size_assert(__cgo_long_long, 8)
__cgo_size_assert(float, 4)
__cgo_size_assert(double, 8)
+
+#include <errno.h>
+#include <string.h>
`
const builtinProlog = `
typedef struct { char *p; int n; } _GoString_;
_GoString_ GoString(char *p);
+_GoString_ GoStringN(char *p, int l);
char *CString(_GoString_);
`
@@ -580,24 +648,27 @@ const cProlog = `
#include "runtime.h"
#include "cgocall.h"
-#pragma dynimport initcgo initcgo "%slibcgo.so"
-#pragma dynimport libcgo_thread_start libcgo_thread_start "%slibcgo.so"
-#pragma dynimport libcgo_set_scheduler libcgo_set_scheduler "%slibcgo.so"
-#pragma dynimport _cgo_malloc _cgo_malloc "%slibcgo.so"
-#pragma dynimport _cgo_free _cgo_free "%slibcgo.so"
+void ·_Cerrno(void*, int32);
+
+void
+·_Cfunc_GoString(int8 *p, String s)
+{
+ s = runtime·gostring((byte*)p);
+ FLUSH(&s);
+}
void
-·_C_GoString(int8 *p, String s)
+·_Cfunc_GoStringN(int8 *p, int32 l, String s)
{
- s = gostring((byte*)p);
+ s = runtime·gostringn((byte*)p, l);
FLUSH(&s);
}
void
-·_C_CString(String s, int8 *p)
+·_Cfunc_CString(String s, int8 *p)
{
- p = cmalloc(s.len+1);
- mcpy((byte*)p, s.str, s.len);
+ p = runtime·cmalloc(s.len+1);
+ runtime·mcpy((byte*)p, s.str, s.len);
p[s.len] = 0;
FLUSH(&p);
}
@@ -610,6 +681,7 @@ typedef unsigned char uchar;
typedef unsigned short ushort;
typedef long long int64;
typedef unsigned long long uint64;
+typedef __SIZE_TYPE__ uintptr;
typedef struct { char *p; int n; } GoString;
typedef void *GoMap;
diff --git a/src/cmd/cgo/util.go b/src/cmd/cgo/util.go
index 95067039c..a6f509dc4 100644
--- a/src/cmd/cgo/util.go
+++ b/src/cmd/cgo/util.go
@@ -12,16 +12,6 @@ import (
"os"
)
-// A ByteReaderAt implements io.ReadAt using a slice of bytes.
-type ByteReaderAt []byte
-
-func (r ByteReaderAt) ReadAt(p []byte, off int64) (n int, err os.Error) {
- if off >= int64(len(r)) || off < 0 {
- return 0, os.EOF
- }
- return copy(p, r[off:]), nil
-}
-
// run runs the command argv, feeding in stdin on standard input.
// It returns the output to standard output and standard error.
// ok indicates whether the command exited successfully.
@@ -55,9 +45,8 @@ func run(stdin []byte, argv []string) (stdout, stderr []byte, ok bool) {
w0.Close()
c <- true
}()
- var xstdout []byte // TODO(rsc): delete after 6g can take address of out parameter
go func() {
- xstdout, _ = ioutil.ReadAll(r1)
+ stdout, _ = ioutil.ReadAll(r1)
r1.Close()
c <- true
}()
@@ -65,7 +54,6 @@ func run(stdin []byte, argv []string) (stdout, stderr []byte, ok bool) {
r2.Close()
<-c
<-c
- stdout = xstdout
w, err := os.Wait(pid, 0)
if err != nil {
@@ -77,18 +65,45 @@ func run(stdin []byte, argv []string) (stdout, stderr []byte, ok bool) {
// Die with an error message.
func fatal(msg string, args ...interface{}) {
- fmt.Fprintf(os.Stderr, msg+"\n", args)
+ fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(2)
}
var nerrors int
-var noPos token.Position
-func error(pos token.Position, msg string, args ...interface{}) {
+func error(pos token.Pos, msg string, args ...interface{}) {
nerrors++
if pos.IsValid() {
- fmt.Fprintf(os.Stderr, "%s: ", pos)
+ fmt.Fprintf(os.Stderr, "%s: ", fset.Position(pos).String())
}
- fmt.Fprintf(os.Stderr, msg, args)
+ fmt.Fprintf(os.Stderr, msg, args...)
fmt.Fprintf(os.Stderr, "\n")
}
+
+// isName returns true if s is a valid C identifier
+func isName(s string) bool {
+ for i, v := range s {
+ if v != '_' && (v < 'A' || v > 'Z') && (v < 'a' || v > 'z') && (v < '0' || v > '9') {
+ return false
+ }
+ if i == 0 && '0' <= v && v <= '9' {
+ return false
+ }
+ }
+ return s != ""
+}
+
+func creat(name string) *os.File {
+ f, err := os.Open(name, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0666)
+ if err != nil {
+ fatal("%s", err)
+ }
+ return f
+}
+
+func slashToUnderscore(c int) int {
+ if c == '/' {
+ c = '_'
+ }
+ return c
+}