diff options
| author | Robert Griesemer <gri@golang.org> | 2009-12-15 15:33:31 -0800 |
|---|---|---|
| committer | Robert Griesemer <gri@golang.org> | 2009-12-15 15:33:31 -0800 |
| commit | d9527dd16f72598b54a64550607bf892efa12384 (patch) | |
| tree | 2ad16a7db2d3c484b47426ad2568359ab633820c /src/cmd/cgo | |
| parent | aea97e0bd7da9cef1cc631ddbd3578a0877a4fcc (diff) | |
| download | golang-d9527dd16f72598b54a64550607bf892efa12384.tar.gz | |
1) Change default gofmt default settings for
parsing and printing to new syntax.
Use -oldparser to parse the old syntax,
use -oldprinter to print the old syntax.
2) Change default gofmt formatting settings
to use tabs for indentation only and to use
spaces for alignment. This will make the code
alignment insensitive to an editor's tabwidth.
Use -spaces=false to use tabs for alignment.
3) Manually changed src/exp/parser/parser_test.go
so that it doesn't try to parse the parser's
source files using the old syntax (they have
new syntax now).
4) gofmt -w src misc test/bench
1st set of files.
R=rsc
CC=agl, golang-dev, iant, ken2, r
http://codereview.appspot.com/180047
Diffstat (limited to 'src/cmd/cgo')
| -rw-r--r-- | src/cmd/cgo/ast.go | 230 | ||||
| -rw-r--r-- | src/cmd/cgo/gcc.go | 412 | ||||
| -rw-r--r-- | src/cmd/cgo/main.go | 60 | ||||
| -rw-r--r-- | src/cmd/cgo/out.go | 170 | ||||
| -rw-r--r-- | src/cmd/cgo/util.go | 74 |
5 files changed, 473 insertions, 473 deletions
diff --git a/src/cmd/cgo/ast.go b/src/cmd/cgo/ast.go index ccaef69d1..b309c33d4 100644 --- a/src/cmd/cgo/ast.go +++ b/src/cmd/cgo/ast.go @@ -7,57 +7,57 @@ package main import ( - "fmt"; - "go/ast"; - "go/doc"; - "go/parser"; - "go/scanner"; - "os"; + "fmt" + "go/ast" + "go/doc" + "go/parser" + "go/scanner" + "os" ) // 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", or "call" - TypeName bool; // whether xxx is a C type name - Type *Type; // the type of xxx - FuncType *FuncType; + Name string + Expr *ast.Expr + Context string // "type", "expr", or "call" + TypeName bool // whether xxx is a C type name + Type *Type // the type of xxx + FuncType *FuncType } // 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; - PtrSize int64; - GccOptions []string; + 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 + PtrSize int64 + GccOptions []string } // 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; + Size int64 + Align int64 + C string + Go ast.Expr } // 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; + Params []*Type + Result *Type + Go *ast.FuncType } func openProg(name string) *Prog { - p := new(Prog); - var err os.Error; - p.AST, err = parser.ParsePkgFile("", name, parser.ParseComments); + p := new(Prog) + var err os.Error + p.AST, err = parser.ParsePkgFile("", name, parser.ParseComments) if err != nil { if list, ok := err.(scanner.ErrorList); ok { // If err is a scanner.ErrorList, its String will print just @@ -67,32 +67,32 @@ func openProg(name string) *Prog { for _, e := range list { fmt.Fprintln(os.Stderr, e) } - os.Exit(2); + os.Exit(2) } - fatal("parsing %s: %s", name, err); + fatal("parsing %s: %s", name, err) } - p.Package = p.AST.Name.Value; + p.Package = p.AST.Name.Value // Find the import "C" line and get any extra C preamble. // Delete the import "C" line along the way. - sawC := false; - w := 0; + sawC := false + w := 0 for _, decl := range p.AST.Decls { - d, ok := decl.(*ast.GenDecl); + d, ok := decl.(*ast.GenDecl) if !ok { - p.AST.Decls[w] = decl; - w++; - continue; + p.AST.Decls[w] = decl + w++ + continue } - ws := 0; + ws := 0 for _, spec := range d.Specs { - s, ok := spec.(*ast.ImportSpec); + s, ok := spec.(*ast.ImportSpec) if !ok || len(s.Path) != 1 || string(s.Path[0].Value) != `"C"` { - d.Specs[ws] = spec; - ws++; - continue; + d.Specs[ws] = spec + ws++ + continue } - sawC = true; + sawC = true if s.Name != nil { error(s.Path[0].Pos(), `cannot rename import "C"`) } @@ -105,20 +105,20 @@ func openProg(name string) *Prog { if ws == 0 { continue } - d.Specs = d.Specs[0:ws]; - p.AST.Decls[w] = d; - w++; + d.Specs = d.Specs[0:ws] + p.AST.Decls[w] = d + w++ } - p.AST.Decls = p.AST.Decls[0:w]; + p.AST.Decls = p.AST.Decls[0:w] if !sawC { error(noPos, `cannot find import "C"`) } // Accumulate pointers to uses of C.x. - p.Crefs = make([]*Cref, 0, 8); - walk(p.AST, p, "prog"); - return p; + p.Crefs = make([]*Cref, 0, 8) + walk(p.AST, p, "prog") + return p } func walk(x interface{}, p *Prog, context string) { @@ -131,29 +131,29 @@ func walk(x interface{}, p *Prog, context string) { // 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.Value == "C" { - i := len(p.Crefs); + i := len(p.Crefs) if i >= cap(p.Crefs) { - new := make([]*Cref, 2*i); + new := make([]*Cref, 2*i) for j, v := range p.Crefs { new[j] = v } - p.Crefs = new; + p.Crefs = new } - p.Crefs = p.Crefs[0 : i+1]; + p.Crefs = p.Crefs[0 : i+1] p.Crefs[i] = &Cref{ Name: sel.Sel.Value, Expr: n, Context: context, - }; - break; + } + break } } - walk(*n, p, context); + walk(*n, p, context) // everything else just recurs default: - error(noPos, "unexpected type %T in walk", x); - panic(); + error(noPos, "unexpected type %T in walk", x) + panic() case nil: @@ -166,54 +166,54 @@ func walk(x interface{}, p *Prog, context string) { case *ast.BasicLit: case *ast.StringList: case *ast.FuncLit: - walk(n.Type, p, "type"); - walk(n.Body, p, "stmt"); + walk(n.Type, p, "type") + walk(n.Body, p, "stmt") case *ast.CompositeLit: - walk(&n.Type, p, "type"); - walk(n.Elts, p, "expr"); + walk(&n.Type, p, "type") + walk(n.Elts, p, "expr") case *ast.ParenExpr: walk(&n.X, p, context) case *ast.SelectorExpr: walk(&n.X, p, "selector") case *ast.IndexExpr: - walk(&n.X, p, "expr"); - walk(&n.Index, p, "expr"); + walk(&n.X, p, "expr") + walk(&n.Index, p, "expr") case *ast.SliceExpr: - walk(&n.X, p, "expr"); - walk(&n.Index, p, "expr"); + walk(&n.X, p, "expr") + walk(&n.Index, p, "expr") if n.End != nil { walk(&n.End, p, "expr") } case *ast.TypeAssertExpr: - walk(&n.X, p, "expr"); - walk(&n.Type, p, "type"); + walk(&n.X, p, "expr") + walk(&n.Type, p, "type") case *ast.CallExpr: - walk(&n.Fun, p, "call"); - walk(n.Args, p, "expr"); + walk(&n.Fun, p, "call") + walk(n.Args, p, "expr") case *ast.StarExpr: walk(&n.X, p, context) case *ast.UnaryExpr: walk(&n.X, p, "expr") case *ast.BinaryExpr: - walk(&n.X, p, "expr"); - walk(&n.Y, p, "expr"); + walk(&n.X, p, "expr") + walk(&n.Y, p, "expr") case *ast.KeyValueExpr: - walk(&n.Key, p, "expr"); - walk(&n.Value, p, "expr"); + walk(&n.Key, p, "expr") + walk(&n.Value, p, "expr") case *ast.ArrayType: - walk(&n.Len, p, "expr"); - walk(&n.Elt, p, "type"); + walk(&n.Len, p, "expr") + walk(&n.Elt, p, "type") case *ast.StructType: walk(n.Fields, p, "field") case *ast.FuncType: - walk(n.Params, p, "field"); - walk(n.Results, p, "field"); + walk(n.Params, p, "field") + walk(n.Results, p, "field") case *ast.InterfaceType: walk(n.Methods, p, "field") case *ast.MapType: - walk(&n.Key, p, "type"); - walk(&n.Value, p, "type"); + walk(&n.Key, p, "type") + walk(&n.Value, p, "type") case *ast.ChanType: walk(&n.Value, p, "type") @@ -228,8 +228,8 @@ func walk(x interface{}, p *Prog, context string) { case *ast.IncDecStmt: walk(&n.X, p, "expr") case *ast.AssignStmt: - walk(n.Lhs, p, "expr"); - walk(n.Rhs, p, "expr"); + walk(n.Lhs, p, "expr") + walk(n.Rhs, p, "expr") case *ast.GoStmt: walk(n.Call, p, "expr") case *ast.DeferStmt: @@ -240,45 +240,45 @@ func walk(x interface{}, p *Prog, context string) { case *ast.BlockStmt: walk(n.List, p, "stmt") case *ast.IfStmt: - walk(n.Init, p, "stmt"); - walk(&n.Cond, p, "expr"); - walk(n.Body, p, "stmt"); - walk(n.Else, p, "stmt"); + walk(n.Init, p, "stmt") + walk(&n.Cond, p, "expr") + walk(n.Body, p, "stmt") + walk(n.Else, p, "stmt") case *ast.CaseClause: - walk(n.Values, p, "expr"); - walk(n.Body, p, "stmt"); + walk(n.Values, p, "expr") + walk(n.Body, p, "stmt") case *ast.SwitchStmt: - walk(n.Init, p, "stmt"); - walk(&n.Tag, p, "expr"); - walk(n.Body, p, "stmt"); + walk(n.Init, p, "stmt") + walk(&n.Tag, p, "expr") + walk(n.Body, p, "stmt") case *ast.TypeCaseClause: - walk(n.Types, p, "type"); - walk(n.Body, p, "stmt"); + walk(n.Types, p, "type") + walk(n.Body, p, "stmt") case *ast.TypeSwitchStmt: - walk(n.Init, p, "stmt"); - walk(n.Assign, p, "stmt"); - walk(n.Body, p, "stmt"); + walk(n.Init, p, "stmt") + walk(n.Assign, p, "stmt") + walk(n.Body, p, "stmt") case *ast.CommClause: - walk(n.Lhs, p, "expr"); - walk(n.Rhs, p, "expr"); - walk(n.Body, p, "stmt"); + walk(n.Lhs, p, "expr") + walk(n.Rhs, p, "expr") + walk(n.Body, p, "stmt") case *ast.SelectStmt: walk(n.Body, p, "stmt") case *ast.ForStmt: - walk(n.Init, p, "stmt"); - walk(&n.Cond, p, "expr"); - walk(n.Post, p, "stmt"); - walk(n.Body, p, "stmt"); + walk(n.Init, p, "stmt") + walk(&n.Cond, p, "expr") + walk(n.Post, p, "stmt") + walk(n.Body, p, "stmt") case *ast.RangeStmt: - walk(&n.Key, p, "expr"); - walk(&n.Value, p, "expr"); - walk(&n.X, p, "expr"); - walk(n.Body, p, "stmt"); + walk(&n.Key, p, "expr") + walk(&n.Value, p, "expr") + walk(&n.X, p, "expr") + walk(n.Body, p, "stmt") case *ast.ImportSpec: case *ast.ValueSpec: - walk(&n.Type, p, "type"); - walk(n.Values, p, "expr"); + walk(&n.Type, p, "type") + walk(n.Values, p, "expr") case *ast.TypeSpec: walk(&n.Type, p, "type") @@ -289,7 +289,7 @@ func walk(x interface{}, p *Prog, context string) { if n.Recv != nil { walk(n.Recv, p, "field") } - walk(n.Type, p, "type"); + walk(n.Type, p, "type") if n.Body != nil { walk(n.Body, p, "stmt") } diff --git a/src/cmd/cgo/gcc.go b/src/cmd/cgo/gcc.go index 7d377db54..c42cb7300 100644 --- a/src/cmd/cgo/gcc.go +++ b/src/cmd/cgo/gcc.go @@ -8,30 +8,30 @@ package main import ( - "bytes"; - "debug/dwarf"; - "debug/elf"; - "debug/macho"; - "fmt"; - "go/ast"; - "go/token"; - "os"; - "strconv"; - "strings"; + "bytes" + "debug/dwarf" + "debug/elf" + "debug/macho" + "fmt" + "go/ast" + "go/token" + "os" + "strconv" + "strings" ) func (p *Prog) loadDebugInfo() { // Construct a slice of unique names from p.Crefs. - m := make(map[string]int); + m := make(map[string]int) for _, c := range p.Crefs { m[c.Name] = -1 } - names := make([]string, 0, len(m)); + names := make([]string, 0, len(m)) for name, _ := range m { - i := len(names); - names = names[0 : i+1]; - names[i] = name; - m[name] = i; + i := len(names) + names = names[0 : i+1] + names[i] = name + m[name] = i } // Coerce gcc into telling us whether each name is @@ -46,18 +46,18 @@ func (p *Prog) loadDebugInfo() { // x.c: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. - var b bytes.Buffer; - b.WriteString(p.Preamble); - b.WriteString("void f(void) {\n"); - b.WriteString("#line 0 \"cgo-test\"\n"); + var b bytes.Buffer + b.WriteString(p.Preamble) + b.WriteString("void f(void) {\n") + b.WriteString("#line 0 \"cgo-test\"\n") for _, n := range names { - b.WriteString(n); - b.WriteString(";\n"); + b.WriteString(n) + b.WriteString(";\n") } - b.WriteString("}\n"); + b.WriteString("}\n") - kind := make(map[string]string); - _, stderr := p.gccDebug(b.Bytes()); + kind := make(map[string]string) + _, stderr := p.gccDebug(b.Bytes()) if stderr == "" { fatal("gcc produced no output") } @@ -65,16 +65,16 @@ func (p *Prog) loadDebugInfo() { if len(line) < 9 || line[0:9] != "cgo-test:" { continue } - line = line[9:]; - colon := strings.Index(line, ":"); + line = line[9:] + colon := strings.Index(line, ":") if colon < 0 { continue } - i, err := strconv.Atoi(line[0:colon]); + i, err := strconv.Atoi(line[0:colon]) if err != nil { continue } - what := ""; + what := "" switch { default: continue @@ -88,7 +88,7 @@ func (p *Prog) loadDebugInfo() { if old, ok := kind[names[i]]; ok && old != what { error(noPos, "inconsistent gcc output about C.%s", names[i]) } - kind[names[i]] = what; + kind[names[i]] = what } for _, n := range names { if _, ok := kind[n]; !ok { @@ -108,21 +108,21 @@ 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); + b.Reset() + b.WriteString(p.Preamble) for i, n := range names { fmt.Fprintf(&b, "typeof(%s) *__cgo__%d;\n", n, i) } - d, stderr := p.gccDebug(b.Bytes()); + d, stderr := p.gccDebug(b.Bytes()) if d == nil { fatal("gcc failed:\n%s\non input:\n%s", stderr, b.Bytes()) } // Scan DWARF info for top-level TagVariable entries with AttrName __cgo__i. - types := make([]dwarf.Type, len(names)); - r := d.Reader(); + types := make([]dwarf.Type, len(names)) + r := d.Reader() for { - e, err := r.Next(); + e, err := r.Next() if err != nil { fatal("reading DWARF entry: %s", err) } @@ -132,27 +132,27 @@ func (p *Prog) loadDebugInfo() { if e.Tag != dwarf.TagVariable { goto Continue } - name, _ := e.Val(dwarf.AttrName).(string); - typOff, _ := e.Val(dwarf.AttrType).(dwarf.Offset); + name, _ := e.Val(dwarf.AttrName).(string) + typOff, _ := e.Val(dwarf.AttrType).(dwarf.Offset) if name == "" || typOff == 0 { fatal("malformed DWARF TagVariable entry") } if !strings.HasPrefix(name, "__cgo__") { goto Continue } - typ, err := d.Type(typOff); + typ, err := d.Type(typOff) if err != nil { fatal("loading DWARF type: %s", err) } - t, ok := typ.(*dwarf.PtrType); + t, ok := typ.(*dwarf.PtrType) if !ok || t == nil { fatal("internal error: %s has non-pointer type", name) } - i, err := strconv.Atoi(name[7:]); + i, err := strconv.Atoi(name[7:]) if err != nil { fatal("malformed __cgo__ name: %s", name) } - types[i] = t.Type; + types[i] = t.Type Continue: if e.Tag != dwarf.TagCompileUnit { @@ -161,132 +161,132 @@ func (p *Prog) loadDebugInfo() { } // Record types and typedef information in Crefs. - var conv typeConv; - conv.Init(p.PtrSize); + var conv typeConv + conv.Init(p.PtrSize) for _, c := range p.Crefs { - i := m[c.Name]; - c.TypeName = kind[c.Name] == "type"; - f, fok := types[i].(*dwarf.FuncType); + i := m[c.Name] + c.TypeName = kind[c.Name] == "type" + f, fok := types[i].(*dwarf.FuncType) if c.Context == "call" && !c.TypeName && fok { c.FuncType = conv.FuncType(f) } else { c.Type = conv.Type(types[i]) } } - p.Typedef = conv.typedef; + p.Typedef = conv.typedef } func concat(a, b []string) []string { - c := make([]string, len(a)+len(b)); + c := make([]string, len(a)+len(b)) for i, s := range a { c[i] = s } for i, s := range b { c[i+len(a)] = s } - return c; + 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"; + machine := "-m32" if p.PtrSize == 8 { machine = "-m64" } - tmp := "_cgo_.o"; + tmp := "_cgo_.o" base := []string{ "gcc", machine, - "-Wall", // many warnings - "-Werror", // warnings are errors - "-o" + tmp, // write object to tmp - "-gdwarf-2", // generate DWARF v2 debugging symbols - "-c", // do not link - "-xc", // input language is C - "-", // read input from standard input - }; - _, stderr, ok := run(stdin, concat(base, p.GccOptions)); + "-Wall", // many warnings + "-Werror", // warnings are errors + "-o" + tmp, // write object to tmp + "-gdwarf-2", // generate DWARF v2 debugging symbols + "-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) } // Try to parse f as ELF and Mach-O and hope one works. var f interface { - DWARF() (*dwarf.Data, os.Error); + DWARF() (*dwarf.Data, os.Error) } - var err 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) } } - d, err := f.DWARF(); + d, err := f.DWARF() if err != nil { fatal("cannot load DWARF debug information from %s: %s", tmp, err) } - return d, ""; + return d, "" } // A typeConv is a translator from dwarf types to Go types // with equivalent memory layout. type typeConv struct { // Cache of already-translated or in-progress types. - m map[dwarf.Type]*Type; - typedef map[string]ast.Expr; + m map[dwarf.Type]*Type + typedef map[string]ast.Expr // Predeclared types. - byte ast.Expr; // denotes padding - int8, int16, int32, int64 ast.Expr; - uint8, uint16, uint32, uint64, uintptr ast.Expr; - float32, float64 ast.Expr; - void ast.Expr; - unsafePointer ast.Expr; - string ast.Expr; + byte ast.Expr // denotes padding + int8, int16, int32, int64 ast.Expr + uint8, uint16, uint32, uint64, uintptr ast.Expr + float32, float64 ast.Expr + void ast.Expr + unsafePointer ast.Expr + string ast.Expr - ptrSize int64; + ptrSize int64 - tagGen int; + tagGen int } func (c *typeConv) Init(ptrSize int64) { - c.ptrSize = ptrSize; - c.m = make(map[dwarf.Type]*Type); - c.typedef = make(map[string]ast.Expr); - c.byte = c.Ident("byte"); - c.int8 = c.Ident("int8"); - c.int16 = c.Ident("int16"); - c.int32 = c.Ident("int32"); - c.int64 = c.Ident("int64"); - c.uint8 = c.Ident("uint8"); - c.uint16 = c.Ident("uint16"); - c.uint32 = c.Ident("uint32"); - c.uint64 = c.Ident("uint64"); - c.uintptr = c.Ident("uintptr"); - c.float32 = c.Ident("float32"); - c.float64 = c.Ident("float64"); - c.unsafePointer = c.Ident("unsafe.Pointer"); - c.void = c.Ident("void"); - c.string = c.Ident("string"); + c.ptrSize = ptrSize + c.m = make(map[dwarf.Type]*Type) + c.typedef = make(map[string]ast.Expr) + c.byte = c.Ident("byte") + c.int8 = c.Ident("int8") + c.int16 = c.Ident("int16") + c.int32 = c.Ident("int32") + c.int64 = c.Ident("int64") + c.uint8 = c.Ident("uint8") + c.uint16 = c.Ident("uint16") + c.uint32 = c.Ident("uint32") + c.uint64 = c.Ident("uint64") + c.uintptr = c.Ident("uintptr") + c.float32 = c.Ident("float32") + c.float64 = c.Ident("float64") + c.unsafePointer = c.Ident("unsafe.Pointer") + c.void = c.Ident("void") + c.string = c.Ident("string") } // base strips away qualifiers and typedefs to get the underlying type func base(dt dwarf.Type) dwarf.Type { for { if d, ok := dt.(*dwarf.QualType); ok { - dt = d.Type; - continue; + dt = d.Type + continue } if d, ok := dt.(*dwarf.TypedefType); ok { - dt = d.Type; - continue; + dt = d.Type + continue } - break; + break } - return dt; + return dt } // Map from dwarf text names to aliases we use in package "C". @@ -308,22 +308,22 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type { if t.Go == nil { fatal("type conversion loop at %s", dtype) } - return t; + return t } - t := new(Type); - t.Size = dtype.Size(); - t.Align = -1; - t.C = dtype.Common().Name; - c.m[dtype] = t; + t := new(Type) + t.Size = dtype.Size() + t.Align = -1 + t.C = dtype.Common().Name + c.m[dtype] = t if t.Size < 0 { // Unsized types are [0]byte - t.Size = 0; - t.Go = c.Opaque(0); + t.Size = 0 + t.Go = c.Opaque(0) if t.C == "" { t.C = "void" } - return t; + return t } switch dt := dtype.(type) { @@ -334,30 +334,30 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type { if t.Size != c.ptrSize { fatal("unexpected: %d-byte address type - %s", t.Size, dtype) } - t.Go = c.uintptr; - t.Align = t.Size; + t.Go = c.uintptr + t.Align = t.Size case *dwarf.ArrayType: if dt.StrideBitSize > 0 { // Cannot represent bit-sized elements in Go. - t.Go = c.Opaque(t.Size); - break; + t.Go = c.Opaque(t.Size) + break } gt := &ast.ArrayType{ Len: c.intExpr(dt.Count), - }; - t.Go = gt; // publish before recursive call - sub := c.Type(dt.Type); - t.Align = sub.Align; - gt.Elt = sub.Go; - t.C = fmt.Sprintf("typeof(%s[%d])", sub.C, dt.Count); + } + t.Go = gt // publish before recursive call + sub := c.Type(dt.Type) + t.Align = sub.Align + gt.Elt = sub.Go + t.C = fmt.Sprintf("typeof(%s[%d])", sub.C, dt.Count) case *dwarf.CharType: if t.Size != 1 { fatal("unexpected: %d-byte char type - %s", t.Size, dtype) } - t.Go = c.int8; - t.Align = 1; + t.Go = c.int8 + t.Align = 1 case *dwarf.EnumType: switch t.Size { @@ -375,7 +375,7 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type { if t.Align = t.Size; t.Align >= c.ptrSize { t.Align = c.ptrSize } - t.C = "enum " + dt.EnumName; + t.C = "enum " + dt.EnumName case *dwarf.FloatType: switch t.Size { @@ -393,8 +393,8 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type { case *dwarf.FuncType: // No attempt at translation: would enable calls // directly between worlds, but we need to moderate those. - t.Go = c.uintptr; - t.Align = c.ptrSize; + t.Go = c.uintptr + t.Align = c.ptrSize case *dwarf.IntType: if dt.BitSize > 0 { @@ -417,52 +417,52 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type { } case *dwarf.PtrType: - t.Align = c.ptrSize; + t.Align = c.ptrSize // Translate void* as unsafe.Pointer if _, ok := base(dt.Type).(*dwarf.VoidType); ok { - t.Go = c.unsafePointer; - t.C = "void*"; - break; + t.Go = c.unsafePointer + t.C = "void*" + break } - gt := &ast.StarExpr{}; - t.Go = gt; // publish before recursive call - sub := c.Type(dt.Type); - gt.X = sub.Go; - t.C = sub.C + "*"; + gt := &ast.StarExpr{} + t.Go = gt // publish before recursive call + sub := c.Type(dt.Type) + gt.X = sub.Go + t.C = sub.C + "*" case *dwarf.QualType: // Ignore qualifier. - t = c.Type(dt.Type); - c.m[dtype] = t; - return t; + t = c.Type(dt.Type) + c.m[dtype] = t + return t case *dwarf.StructType: // Convert to Go struct, being careful about alignment. // Have to give it a name to simulate C "struct foo" references. - tag := dt.StructName; + tag := dt.StructName if tag == "" { - tag = "__" + strconv.Itoa(c.tagGen); - c.tagGen++; + tag = "__" + strconv.Itoa(c.tagGen) + c.tagGen++ } else if t.C == "" { t.C = dt.Kind + " " + tag } - name := c.Ident("_C" + dt.Kind + "_" + tag); - t.Go = name; // publish before recursive calls + name := c.Ident("_C" + dt.Kind + "_" + tag) + t.Go = name // publish before recursive calls switch dt.Kind { case "union", "class": - c.typedef[name.Value] = c.Opaque(t.Size); + c.typedef[name.Value] = c.Opaque(t.Size) if t.C == "" { t.C = fmt.Sprintf("typeof(unsigned char[%d])", t.Size) } case "struct": - g, csyntax, align := c.Struct(dt); + g, csyntax, align := c.Struct(dt) if t.C == "" { t.C = csyntax } - t.Align = align; - c.typedef[name.Value] = g; + t.Align = align + c.typedef[name.Value] = g } case *dwarf.TypedefType: @@ -471,16 +471,16 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type { // Special C name for Go string type. // Knows string layout used by compilers: pointer plus length, // which rounds up to 2 pointers after alignment. - t.Go = c.string; - t.Size = c.ptrSize * 2; - t.Align = c.ptrSize; - break; - } - name := c.Ident("_C_" + dt.Name); - t.Go = name; // publish before recursive call - sub := c.Type(dt.Type); - t.Size = sub.Size; - t.Align = sub.Align; + t.Go = c.string + t.Size = c.ptrSize * 2 + t.Align = c.ptrSize + break + } + name := c.Ident("_C_" + 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.Value]; !ok { c.typedef[name.Value] = sub.Go } @@ -489,8 +489,8 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type { if t.Size != 1 { fatal("unexpected: %d-byte uchar type - %s", t.Size, dtype) } - t.Go = c.uint8; - t.Align = 1; + t.Go = c.uint8 + t.Align = 1 case *dwarf.UintType: if dt.BitSize > 0 { @@ -513,21 +513,21 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type { } case *dwarf.VoidType: - t.Go = c.void; - t.C = "void"; + t.Go = c.void + t.C = "void" } switch dtype.(type) { case *dwarf.AddrType, *dwarf.CharType, *dwarf.IntType, *dwarf.FloatType, *dwarf.UcharType, *dwarf.UintType: - s := dtype.Common().Name; + s := dtype.Common().Name if s != "" { if ss, ok := cnameMap[s]; ok { s = ss } - s = strings.Join(strings.Split(s, " ", 0), ""); // strip spaces - name := c.Ident("_C_" + s); - c.typedef[name.Value] = t.Go; - t.Go = name; + s = strings.Join(strings.Split(s, " ", 0), "") // strip spaces + name := c.Ident("_C_" + s) + c.typedef[name.Value] = t.Go + t.Go = name } } @@ -535,13 +535,13 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type { fatal("internal error: did not create C name for %s", dtype) } - return t; + return t } // FuncArg returns a Go type with the same memory layout as // dtype when used as the type of a C function argument. func (c *typeConv) FuncArg(dtype dwarf.Type) *Type { - t := c.Type(dtype); + t := c.Type(dtype) switch dt := dtype.(type) { case *dwarf.ArrayType: // Arrays are passed implicitly as pointers in C. @@ -565,14 +565,14 @@ func (c *typeConv) FuncArg(dtype dwarf.Type) *Type { } } } - return t; + return t } // FuncType returns the Go type analogous to dtype. // There is no guarantee about matching memory layout. func (c *typeConv) FuncType(dtype *dwarf.FuncType) *FuncType { - p := make([]*Type, len(dtype.ParamType)); - gp := make([]*ast.Field, len(dtype.ParamType)); + p := make([]*Type, len(dtype.ParamType)) + gp := make([]*ast.Field, len(dtype.ParamType)) for i, f := range dtype.ParamType { // gcc's DWARF generator outputs a single DotDotDotType parameter for // function pointers that specify no parameters (e.g. void @@ -580,17 +580,17 @@ func (c *typeConv) FuncType(dtype *dwarf.FuncType) *FuncType { // invalid according to ISO C anyway (i.e. void (*__cgo_1)(...) is not // legal). if _, ok := f.(*dwarf.DotDotDotType); ok && i == 0 { - p, gp = nil, nil; - break; + p, gp = nil, nil + break } - p[i] = c.FuncArg(f); - gp[i] = &ast.Field{Type: p[i].Go}; + p[i] = c.FuncArg(f) + gp[i] = &ast.Field{Type: p[i].Go} } - var r *Type; - var gr []*ast.Field; + var r *Type + var gr []*ast.Field if _, ok := dtype.ReturnType.(*dwarf.VoidType); !ok && dtype.ReturnType != nil { - r = c.Type(dtype.ReturnType); - gr = []*ast.Field{&ast.Field{Type: r.Go}}; + r = c.Type(dtype.ReturnType) + gr = []*ast.Field{&ast.Field{Type: r.Go}} } return &FuncType{ Params: p, @@ -599,11 +599,11 @@ func (c *typeConv) FuncType(dtype *dwarf.FuncType) *FuncType { Params: gp, Results: gr, }, - }; + } } // Identifier -func (c *typeConv) Ident(s string) *ast.Ident { return &ast.Ident{Value: s} } +func (c *typeConv) Ident(s string) *ast.Ident { return &ast.Ident{Value: s} } // Opaque type of n bytes. func (c *typeConv) Opaque(n int64) ast.Expr { @@ -623,17 +623,17 @@ func (c *typeConv) intExpr(n int64) ast.Expr { // Add padding of given size to fld. func (c *typeConv) pad(fld []*ast.Field, size int64) []*ast.Field { - n := len(fld); - fld = fld[0 : n+1]; - fld[n] = &ast.Field{Names: []*ast.Ident{c.Ident("_")}, Type: c.Opaque(size)}; - return fld; + n := len(fld) + fld = fld[0 : n+1] + fld[n] = &ast.Field{Names: []*ast.Ident{c.Ident("_")}, Type: c.Opaque(size)} + return fld } // Struct conversion func (c *typeConv) Struct(dt *dwarf.StructType) (expr *ast.StructType, csyntax string, align int64) { - csyntax = "struct { "; - fld := make([]*ast.Field, 0, 2*len(dt.Field)+1); // enough for padding around every field - off := int64(0); + csyntax = "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 // _{keyword}. Create a map from C ident -> Go ident. The Go ident will @@ -641,51 +641,51 @@ func (c *typeConv) Struct(dt *dwarf.StructType) (expr *ast.StructType, csyntax s // the C-side will cause the Go-mangled version to be prefixed with _. // (e.g. in a struct with fields '_type' and 'type', the latter would be // rendered as '__type' in Go). - ident := make(map[string]string); - used := make(map[string]bool); + ident := make(map[string]string) + used := make(map[string]bool) for _, f := range dt.Field { - ident[f.Name] = f.Name; - used[f.Name] = true; + ident[f.Name] = f.Name + used[f.Name] = true } for cid, goid := range ident { if token.Lookup(strings.Bytes(goid)).IsKeyword() { // Avoid keyword - goid = "_" + goid; + goid = "_" + goid // Also avoid existing fields for _, exist := used[goid]; exist; _, exist = used[goid] { goid = "_" + goid } - used[goid] = true; - ident[cid] = goid; + used[goid] = true + ident[cid] = goid } } for _, f := range dt.Field { if f.ByteOffset > off { - fld = c.pad(fld, f.ByteOffset-off); - off = f.ByteOffset; + fld = c.pad(fld, f.ByteOffset-off) + off = f.ByteOffset } - t := c.Type(f.Type); - n := len(fld); - fld = fld[0 : n+1]; + t := c.Type(f.Type) + n := len(fld) + fld = fld[0 : n+1] - fld[n] = &ast.Field{Names: []*ast.Ident{c.Ident(ident[f.Name])}, Type: t.Go}; - off += t.Size; - csyntax += t.C + " " + f.Name + "; "; + fld[n] = &ast.Field{Names: []*ast.Ident{c.Ident(ident[f.Name])}, Type: t.Go} + off += t.Size + csyntax += t.C + " " + f.Name + "; " if t.Align > align { align = t.Align } } if off < dt.ByteSize { - fld = c.pad(fld, dt.ByteSize-off); - off = dt.ByteSize; + fld = c.pad(fld, dt.ByteSize-off) + off = dt.ByteSize } if off != dt.ByteSize { fatal("struct size calculation error") } - csyntax += "}"; - expr = &ast.StructType{Fields: fld}; - return; + csyntax += "}" + expr = &ast.StructType{Fields: fld} + return } diff --git a/src/cmd/cgo/main.go b/src/cmd/cgo/main.go index 8202b8e2b..373df3ba2 100644 --- a/src/cmd/cgo/main.go +++ b/src/cmd/cgo/main.go @@ -11,12 +11,12 @@ package main import ( - "fmt"; - "go/ast"; - "os"; + "fmt" + "go/ast" + "os" ) -func usage() { fmt.Fprint(os.Stderr, "usage: cgo [compiler options] file.go\n") } +func usage() { fmt.Fprint(os.Stderr, "usage: cgo [compiler options] file.go\n") } var ptrSizeMap = map[string]int64{ "386": 4, @@ -35,29 +35,29 @@ var expandName = map[string]string{ } func main() { - args := os.Args; + args := os.Args if len(args) < 2 { - usage(); - os.Exit(2); + usage() + os.Exit(2) } - gccOptions := args[1 : len(args)-1]; - input := args[len(args)-1]; + gccOptions := args[1 : len(args)-1] + input := args[len(args)-1] - arch := os.Getenv("GOARCH"); + arch := os.Getenv("GOARCH") if arch == "" { fatal("$GOARCH is not set") } - ptrSize, ok := ptrSizeMap[arch]; + ptrSize, ok := ptrSizeMap[arch] if !ok { fatal("unknown architecture %s", arch) } // Clear locale variables so gcc emits English errors [sic]. - os.Setenv("LANG", "en_US.UTF-8"); - os.Setenv("LC_ALL", "C"); - os.Setenv("LC_CTYPE", "C"); + os.Setenv("LANG", "en_US.UTF-8") + os.Setenv("LC_ALL", "C") + os.Setenv("LC_CTYPE", "C") - p := openProg(input); + p := openProg(input) for _, cref := range p.Crefs { // Convert C.ulong to C.unsigned long, etc. if expand, ok := expandName[cref.Name]; ok { @@ -65,42 +65,42 @@ func main() { } } - p.PtrSize = ptrSize; - p.Preamble = p.Preamble + "\n" + builtinProlog; - p.GccOptions = gccOptions; - p.loadDebugInfo(); - p.Vardef = make(map[string]*Type); - p.Funcdef = make(map[string]*FuncType); + p.PtrSize = ptrSize + p.Preamble = p.Preamble + "\n" + builtinProlog + p.GccOptions = gccOptions + p.loadDebugInfo() + p.Vardef = make(map[string]*Type) + p.Funcdef = make(map[string]*FuncType) for _, cref := range p.Crefs { switch cref.Context { case "call": if !cref.TypeName { // Is an actual function call. - *cref.Expr = &ast.Ident{Value: "_C_" + cref.Name}; - p.Funcdef[cref.Name] = cref.FuncType; - break; + *cref.Expr = &ast.Ident{Value: "_C_" + cref.Name} + p.Funcdef[cref.Name] = cref.FuncType + break } - *cref.Expr = cref.Type.Go; + *cref.Expr = cref.Type.Go case "expr": if cref.TypeName { error((*cref.Expr).Pos(), "type C.%s used as expression", cref.Name) } // Reference to C variable. // We declare a pointer and arrange to have it filled in. - *cref.Expr = &ast.StarExpr{X: &ast.Ident{Value: "_C_" + cref.Name}}; - p.Vardef[cref.Name] = cref.Type; + *cref.Expr = &ast.StarExpr{X: &ast.Ident{Value: "_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.Type.Go } } if nerrors > 0 { os.Exit(2) } - p.PackagePath = os.Getenv("CGOPKGPATH") + "/" + p.Package; - p.writeOutput(input); + p.PackagePath = os.Getenv("CGOPKGPATH") + "/" + p.Package + p.writeOutput(input) } diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go index 297ffe40a..9c85bc598 100644 --- a/src/cmd/cgo/out.go +++ b/src/cmd/cgo/out.go @@ -5,78 +5,78 @@ package main import ( - "fmt"; - "go/ast"; - "go/printer"; - "os"; - "strings"; + "fmt" + "go/ast" + "go/printer" + "os" + "strings" ) func creat(name string) *os.File { - f, err := os.Open(name, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0666); + f, err := os.Open(name, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0666) if err != nil { fatal("%s", err) } - return f; + return f } // writeOutput 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) writeOutput(srcfile string) { - pkgroot := os.Getenv("GOROOT") + "/pkg/" + os.Getenv("GOOS") + "_" + os.Getenv("GOARCH"); + pkgroot := os.Getenv("GOROOT") + "/pkg/" + os.Getenv("GOOS") + "_" + os.Getenv("GOARCH") - base := srcfile; + base := srcfile if strings.HasSuffix(base, ".go") { base = base[0 : len(base)-3] } - fgo1 := creat(base + ".cgo1.go"); - fgo2 := creat(base + ".cgo2.go"); - fc := creat(base + ".cgo3.c"); - fgcc := creat(base + ".cgo4.c"); + fgo1 := creat(base + ".cgo1.go") + fgo2 := creat(base + ".cgo2.go") + fc := creat(base + ".cgo3.c") + fgcc := creat(base + ".cgo4.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, "//line %s:1\n", srcfile); - printer.Fprint(fgo1, p.AST); + fmt.Fprintf(fgo1, "// Created by cgo - DO NOT EDIT\n") + fmt.Fprintf(fgo1, "//line %s:1\n", srcfile) + printer.Fprint(fgo1, p.AST) // 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, "import \"unsafe\"\n\n"); - fmt.Fprintf(fgo2, "type _ unsafe.Pointer\n\n"); + fmt.Fprintf(fgo2, "// Created by cgo - DO NOT EDIT\n") + fmt.Fprintf(fgo2, "package %s\n\n", p.Package) + fmt.Fprintf(fgo2, "import \"unsafe\"\n\n") + fmt.Fprintf(fgo2, "type _ unsafe.Pointer\n\n") for name, def := range p.Typedef { - fmt.Fprintf(fgo2, "type %s ", name); - printer.Fprint(fgo2, def); - fmt.Fprintf(fgo2, "\n"); + fmt.Fprintf(fgo2, "type %s ", name) + printer.Fprint(fgo2, def) + fmt.Fprintf(fgo2, "\n") } - fmt.Fprintf(fgo2, "type _C_void [0]byte\n"); + fmt.Fprintf(fgo2, "type _C_void [0]byte\n") // 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", gccProlog); + fmt.Fprintf(fgcc, "%s\n", p.Preamble) + fmt.Fprintf(fgcc, "%s\n", gccProlog) - fmt.Fprintf(fc, cProlog, pkgroot, pkgroot, pkgroot, pkgroot, p.Package, p.Package); + fmt.Fprintf(fc, cProlog, pkgroot, pkgroot, pkgroot, pkgroot, p.Package, p.Package) for name, def := range p.Vardef { - fmt.Fprintf(fc, "#pragma dynld %s·_C_%s %s \"%s/%s_%s.so\"\n", p.Package, name, name, pkgroot, p.PackagePath, base); - fmt.Fprintf(fgo2, "var _C_%s ", name); - printer.Fprint(fgo2, &ast.StarExpr{X: def.Go}); - fmt.Fprintf(fgo2, "\n"); + fmt.Fprintf(fc, "#pragma dynld %s·_C_%s %s \"%s/%s_%s.so\"\n", p.Package, name, name, pkgroot, p.PackagePath, base) + fmt.Fprintf(fgo2, "var _C_%s ", name) + printer.Fprint(fgo2, &ast.StarExpr{X: def.Go}) + fmt.Fprintf(fgo2, "\n") } - fmt.Fprintf(fc, "\n"); + fmt.Fprintf(fc, "\n") for name, def := range p.Funcdef { // Go func declaration. d := &ast.FuncDecl{ Name: &ast.Ident{Value: "_C_" + name}, Type: def.Go, - }; - printer.Fprint(fgo2, d); - fmt.Fprintf(fgo2, "\n"); + } + printer.Fprint(fgo2, d) + fmt.Fprintf(fgo2, "\n") if name == "CString" || name == "GoString" { // The builtins are already defined in the C prolog. @@ -88,86 +88,86 @@ func (p *Prog) writeOutput(srcfile string) { // 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; + 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++; + 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; + 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); - off += pad; - npad++; + 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++; + 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; + 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++; + 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\tchar unused;\n" // avoid empty struct + off++ } - structType += "\t}"; - argSize := off; + structType += "\t}" + argSize := 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 dynld _cgo_%s _cgo_%s \"%s/%s_%s.so\"\n", name, name, pkgroot, p.PackagePath, base); - fmt.Fprintf(fc, "void (*_cgo_%s)(void*);\n", name); - fmt.Fprintf(fc, "\n"); - fmt.Fprintf(fc, "void\n"); - fmt.Fprintf(fc, "%s·_C_%s(struct{uint8 x[%d];}p)\n", p.Package, name, argSize); - fmt.Fprintf(fc, "{\n"); - fmt.Fprintf(fc, "\tcgocall(_cgo_%s, &p);\n", name); - fmt.Fprintf(fc, "}\n"); - fmt.Fprintf(fc, "\n"); + fmt.Fprintf(fc, "#pragma dynld _cgo_%s _cgo_%s \"%s/%s_%s.so\"\n", name, name, pkgroot, p.PackagePath, base) + fmt.Fprintf(fc, "void (*_cgo_%s)(void*);\n", name) + fmt.Fprintf(fc, "\n") + fmt.Fprintf(fc, "void\n") + fmt.Fprintf(fc, "%s·_C_%s(struct{uint8 x[%d];}p)\n", p.Package, name, argSize) + fmt.Fprintf(fc, "{\n") + fmt.Fprintf(fc, "\tcgocall(_cgo_%s, &p);\n", name) + fmt.Fprintf(fc, "}\n") + fmt.Fprintf(fc, "\n") // 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"); + 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); + 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, "a->p%d", i) } - fmt.Fprintf(fgcc, ");\n"); - fmt.Fprintf(fgcc, "}\n"); - fmt.Fprintf(fgcc, "\n"); + fmt.Fprintf(fgcc, ");\n") + fmt.Fprintf(fgcc, "}\n") + fmt.Fprintf(fgcc, "\n") } - fgo1.Close(); - fgo2.Close(); - fc.Close(); - fgcc.Close(); + fgo1.Close() + fgo2.Close() + fc.Close() + fgcc.Close() } const gccProlog = ` diff --git a/src/cmd/cgo/util.go b/src/cmd/cgo/util.go index 176e9528e..782efddf4 100644 --- a/src/cmd/cgo/util.go +++ b/src/cmd/cgo/util.go @@ -5,11 +5,11 @@ package main import ( - "exec"; - "fmt"; - "go/token"; - "io/ioutil"; - "os"; + "exec" + "fmt" + "go/token" + "io/ioutil" + "os" ) // A ByteReaderAt implements io.ReadAt using a slice of bytes. @@ -19,76 +19,76 @@ 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; + 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. func run(stdin []byte, argv []string) (stdout, stderr []byte, ok bool) { - cmd, err := exec.LookPath(argv[0]); + cmd, err := exec.LookPath(argv[0]) if err != nil { fatal("exec %s: %s", argv[0], err) } - r0, w0, err := os.Pipe(); + r0, w0, err := os.Pipe() if err != nil { fatal("%s", err) } - r1, w1, err := os.Pipe(); + r1, w1, err := os.Pipe() if err != nil { fatal("%s", err) } - r2, w2, err := os.Pipe(); + r2, w2, err := os.Pipe() if err != nil { fatal("%s", err) } - pid, err := os.ForkExec(cmd, argv, os.Environ(), "", []*os.File{r0, w1, w2}); + pid, err := os.ForkExec(cmd, argv, os.Environ(), "", []*os.File{r0, w1, w2}) if err != nil { fatal("%s", err) } - r0.Close(); - w1.Close(); - w2.Close(); - c := make(chan bool); + r0.Close() + w1.Close() + w2.Close() + c := make(chan bool) go func() { - w0.Write(stdin); - w0.Close(); - c <- true; - }(); - var xstdout []byte; // TODO(rsc): delete after 6g can take address of out parameter + w0.Write(stdin) + w0.Close() + c <- true + }() + var xstdout []byte // TODO(rsc): delete after 6g can take address of out parameter go func() { - xstdout, _ = ioutil.ReadAll(r1); - r1.Close(); - c <- true; - }(); - stderr, _ = ioutil.ReadAll(r2); - r2.Close(); - <-c; - <-c; - stdout = xstdout; + xstdout, _ = ioutil.ReadAll(r1) + r1.Close() + c <- true + }() + stderr, _ = ioutil.ReadAll(r2) + r2.Close() + <-c + <-c + stdout = xstdout - w, err := os.Wait(pid, 0); + w, err := os.Wait(pid, 0) if err != nil { fatal("%s", err) } - ok = w.Exited() && w.ExitStatus() == 0; - return; + ok = w.Exited() && w.ExitStatus() == 0 + return } // Die with an error message. func fatal(msg string, args ...) { - fmt.Fprintf(os.Stderr, msg+"\n", args); - os.Exit(2); + 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 ...) { - nerrors++; + nerrors++ if pos.IsValid() { fmt.Fprintf(os.Stderr, "%s: ", pos) } - fmt.Fprintf(os.Stderr, msg, args); - fmt.Fprintf(os.Stderr, "\n"); + fmt.Fprintf(os.Stderr, msg, args) + fmt.Fprintf(os.Stderr, "\n") } |
