diff options
Diffstat (limited to 'libgo/go/cmd/cgo/ast.go')
-rw-r--r-- | libgo/go/cmd/cgo/ast.go | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/libgo/go/cmd/cgo/ast.go b/libgo/go/cmd/cgo/ast.go index c3a24c2b76..8ce824196d 100644 --- a/libgo/go/cmd/cgo/ast.go +++ b/libgo/go/cmd/cgo/ast.go @@ -1,4 +1,4 @@ -// Copyright 2009 The Go Authors. All rights reserved. +// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. @@ -40,7 +40,7 @@ func sourceLine(n ast.Node) int { } // ReadGo populates f with information learned from reading the -// Go source file with the given file name. It gathers the C preamble +// 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. @@ -73,7 +73,7 @@ func (f *File) ReadGo(name string) { } for _, spec := range d.Specs { s, ok := spec.(*ast.ImportSpec) - if !ok || string(s.Path.Value) != `"C"` { + if !ok || s.Path.Value != `"C"` { continue } sawC = true @@ -87,6 +87,7 @@ func (f *File) ReadGo(name string) { if cg != nil { f.Preamble += fmt.Sprintf("#line %d %q\n", sourceLine(cg), name) f.Preamble += commentText(cg) + "\n" + f.Preamble += "#line 1 \"cgo-generated-wrapper\"\n" } } } @@ -106,7 +107,7 @@ func (f *File) ReadGo(name string) { ws := 0 for _, spec := range d.Specs { s, ok := spec.(*ast.ImportSpec) - if !ok || string(s.Path.Value) != `"C"` { + if !ok || s.Path.Value != `"C"` { d.Specs[ws] = spec ws++ } @@ -147,7 +148,7 @@ func commentText(g *ast.CommentGroup) string { } var pieces []string for _, com := range g.List { - c := string(com.Text) + c := com.Text // Remove comment markers. // The parser has given us exactly the comment text. switch c[1] { @@ -172,7 +173,7 @@ func (f *File) saveExprs(x interface{}, context string) { f.saveRef(x, context) } case *ast.CallExpr: - f.saveCall(x) + f.saveCall(x, context) } } @@ -220,7 +221,7 @@ func (f *File) saveRef(n *ast.Expr, context string) { } // Save calls to C.xxx for later processing. -func (f *File) saveCall(call *ast.CallExpr) { +func (f *File) saveCall(call *ast.CallExpr, context string) { sel, ok := call.Fun.(*ast.SelectorExpr) if !ok { return @@ -228,7 +229,8 @@ func (f *File) saveCall(call *ast.CallExpr) { if l, ok := sel.X.(*ast.Ident); !ok || l.Name != "C" { return } - f.Calls = append(f.Calls, call) + c := &Call{Call: call, Deferred: context == "defer"} + f.Calls = append(f.Calls, c) } // If a function should be exported add it to ExpFunc. @@ -242,11 +244,11 @@ func (f *File) saveExport(x interface{}, context string) { return } for _, c := range n.Doc.List { - if !strings.HasPrefix(string(c.Text), "//export ") { + if !strings.HasPrefix(c.Text, "//export ") { continue } - name := strings.TrimSpace(string(c.Text[9:])) + name := strings.TrimSpace(c.Text[9:]) if name == "" { error_(c.Pos(), "export missing name") } @@ -295,7 +297,7 @@ func (f *File) walk(x interface{}, context string, visit func(*File, interface{} // everything else just recurs default: - error_(token.NoPos, "unexpected type %T in walk", x, visit) + error_(token.NoPos, "unexpected type %T in walk", x) panic("unexpected type") case nil: @@ -401,7 +403,7 @@ func (f *File) walk(x interface{}, context string, visit func(*File, interface{} case *ast.GoStmt: f.walk(n.Call, "expr", visit) case *ast.DeferStmt: - f.walk(n.Call, "expr", visit) + f.walk(n.Call, "defer", visit) case *ast.ReturnStmt: f.walk(n.Results, "expr", visit) case *ast.BranchStmt: @@ -447,7 +449,11 @@ func (f *File) walk(x interface{}, context string, visit func(*File, interface{} case *ast.ImportSpec: case *ast.ValueSpec: f.walk(&n.Type, "type", visit) - f.walk(n.Values, "expr", visit) + if len(n.Names) == 2 && len(n.Values) == 1 { + f.walk(&n.Values[0], "as2", visit) + } else { + f.walk(n.Values, "expr", visit) + } case *ast.TypeSpec: f.walk(&n.Type, "type", visit) |