diff options
author | Ondřej Surý <ondrej@sury.org> | 2012-06-14 13:23:46 +0200 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2012-06-14 13:23:46 +0200 |
commit | 917c5fb8ec48e22459d77e3849e6d388f93d3260 (patch) | |
tree | 9c23734a6ffd4d2a8ac99502eda3cc812a8b130b /src/cmd/cgo | |
parent | 0003ee229fd33ff46cb5f2fe1e35f5c0284debc4 (diff) | |
download | golang-917c5fb8ec48e22459d77e3849e6d388f93d3260.tar.gz |
Imported Upstream version 1.0.2upstream/1.0.2
Diffstat (limited to 'src/cmd/cgo')
-rw-r--r-- | src/cmd/cgo/main.go | 16 | ||||
-rw-r--r-- | src/cmd/cgo/out.go | 72 |
2 files changed, 53 insertions, 35 deletions
diff --git a/src/cmd/cgo/main.go b/src/cmd/cgo/main.go index 7449f04c4..60165961a 100644 --- a/src/cmd/cgo/main.go +++ b/src/cmd/cgo/main.go @@ -22,6 +22,7 @@ import ( "path/filepath" "reflect" "runtime" + "sort" "strings" ) @@ -33,9 +34,8 @@ type Package struct { GccOptions []string CgoFlags map[string]string // #cgo flags (CFLAGS, LDFLAGS) 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 + Name map[string]*Name // accumulated Name from Files + ExpFunc []*ExpFunc // accumulated ExpFunc from Files Decl []ast.Decl GoFiles []string // list of Go files GccFiles []string // list of gcc output files @@ -51,7 +51,15 @@ type File struct { 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 +} + +func nameKeys(m map[string]*Name) []string { + var ks []string + for k := range m { + ks = append(ks, k) + } + sort.Strings(ks) + return ks } // A Ref refers to an expression of the form C.xxx in the AST. diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go index 814250c2e..5dfc16a02 100644 --- a/src/cmd/cgo/out.go +++ b/src/cmd/cgo/out.go @@ -71,7 +71,8 @@ func (p *Package) writeDefs() { } cVars := make(map[string]bool) - for _, n := range p.Name { + for _, key := range nameKeys(p.Name) { + n := p.Name[key] if n.Kind != "var" { continue } @@ -94,14 +95,16 @@ func (p *Package) writeDefs() { } fmt.Fprintf(fc, "\n") - for _, n := range p.Name { + for _, key := range nameKeys(p.Name) { + n := p.Name[key] if n.Const != "" { fmt.Fprintf(fgo2, "const _Cconst_%s = %s\n", n.Go, n.Const) } } fmt.Fprintf(fgo2, "\n") - for _, n := range p.Name { + for _, key := range nameKeys(p.Name) { + n := p.Name[key] if n.FuncType != nil { p.writeDefsFunc(fc, fgo2, n) } @@ -372,7 +375,8 @@ func (p *Package) writeOutput(f *File, srcfile string) { fmt.Fprintf(fgcc, "%s\n", f.Preamble) fmt.Fprintf(fgcc, "%s\n", gccProlog) - for _, n := range f.Name { + for _, key := range nameKeys(f.Name) { + n := f.Name[key] if n.FuncType != nil { p.writeOutputFunc(fgcc, n) } @@ -736,25 +740,23 @@ func c(repr string, args ...interface{}) *TypeRepr { // Map predeclared Go types to Type. var goTypes = map[string]*Type{ - "bool": {Size: 1, Align: 1, C: c("uchar")}, - "byte": {Size: 1, Align: 1, C: c("uchar")}, - "int": {Size: 4, Align: 4, C: c("int")}, - "uint": {Size: 4, Align: 4, C: c("uint")}, - "rune": {Size: 4, Align: 4, C: c("int")}, - "int8": {Size: 1, Align: 1, C: c("schar")}, - "uint8": {Size: 1, Align: 1, C: c("uchar")}, - "int16": {Size: 2, Align: 2, C: c("short")}, - "uint16": {Size: 2, Align: 2, C: c("ushort")}, - "int32": {Size: 4, Align: 4, C: c("int")}, - "uint32": {Size: 4, Align: 4, C: c("uint")}, - "int64": {Size: 8, Align: 8, C: c("int64")}, - "uint64": {Size: 8, Align: 8, C: c("uint64")}, - "float": {Size: 4, Align: 4, C: c("float")}, - "float32": {Size: 4, Align: 4, C: c("float")}, - "float64": {Size: 8, Align: 8, C: c("double")}, - "complex": {Size: 8, Align: 8, C: c("__complex float")}, - "complex64": {Size: 8, Align: 8, C: c("__complex float")}, - "complex128": {Size: 16, Align: 16, C: c("__complex double")}, + "bool": {Size: 1, Align: 1, C: c("GoUint8")}, + "byte": {Size: 1, Align: 1, C: c("GoUint8")}, + "int": {Size: 4, Align: 4, C: c("GoInt")}, + "uint": {Size: 4, Align: 4, C: c("GoUint")}, + "rune": {Size: 4, Align: 4, C: c("GoInt32")}, + "int8": {Size: 1, Align: 1, C: c("GoInt8")}, + "uint8": {Size: 1, Align: 1, C: c("GoUint8")}, + "int16": {Size: 2, Align: 2, C: c("GoInt16")}, + "uint16": {Size: 2, Align: 2, C: c("GoUint16")}, + "int32": {Size: 4, Align: 4, C: c("GoInt32")}, + "uint32": {Size: 4, Align: 4, C: c("GoUint32")}, + "int64": {Size: 8, Align: 8, C: c("GoInt64")}, + "uint64": {Size: 8, Align: 8, C: c("GoUint64")}, + "float32": {Size: 4, Align: 4, C: c("GoFloat32")}, + "float64": {Size: 8, Align: 8, C: c("GoFloat64")}, + "complex64": {Size: 8, Align: 8, C: c("GoComplex64")}, + "complex128": {Size: 16, Align: 16, C: c("GoComplex128")}, } // Map an ast type to a Type. @@ -799,7 +801,7 @@ func (p *Package) cgoType(e ast.Expr) *Type { return def } if t.Name == "uintptr" { - return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("uintptr")} + return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("GoUintptr")} } if t.Name == "string" { return &Type{Size: p.PtrSize + 4, Align: p.PtrSize, C: c("GoString")} @@ -930,13 +932,21 @@ Slice GoBytes(char *p, int n) { ` const gccExportHeaderProlog = ` -typedef unsigned int uint; -typedef signed char schar; -typedef unsigned char uchar; -typedef unsigned short ushort; -typedef long long int64; -typedef unsigned long long uint64; -typedef __SIZE_TYPE__ uintptr; +typedef int GoInt; +typedef unsigned int GoUint; +typedef signed char GoInt8; +typedef unsigned char GoUint8; +typedef short GoInt16; +typedef unsigned short GoUint16; +typedef int GoInt32; +typedef unsigned int GoUint32; +typedef long long GoInt64; +typedef unsigned long long GoUint64; +typedef __SIZE_TYPE__ GoUintptr; +typedef float GoFloat32; +typedef double GoFloat64; +typedef __complex float GoComplex64; +typedef __complex double GoComplex128; typedef struct { char *p; int n; } GoString; typedef void *GoMap; |