github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/src/cmd/cgo/ast.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Parse input AST and prepare Prog structure.
     6  
     7  package main
     8  
     9  import (
    10  	"fmt"
    11  	"go/ast"
    12  	"go/parser"
    13  	"go/scanner"
    14  	"go/token"
    15  	"os"
    16  	"path/filepath"
    17  	"strings"
    18  )
    19  
    20  func parse(name string, flags parser.Mode) *ast.File {
    21  	ast1, err := parser.ParseFile(fset, name, nil, flags)
    22  	if err != nil {
    23  		if list, ok := err.(scanner.ErrorList); ok {
    24  			// If err is a scanner.ErrorList, its String will print just
    25  			// the first error and then (+n more errors).
    26  			// Instead, turn it into a new Error that will return
    27  			// details for all the errors.
    28  			for _, e := range list {
    29  				fmt.Fprintln(os.Stderr, e)
    30  			}
    31  			os.Exit(2)
    32  		}
    33  		fatalf("parsing %s: %s", name, err)
    34  	}
    35  	return ast1
    36  }
    37  
    38  func sourceLine(n ast.Node) int {
    39  	return fset.Position(n.Pos()).Line
    40  }
    41  
    42  // ReadGo populates f with information learned from reading the
    43  // Go source file with the given file name. It gathers the C preamble
    44  // attached to the import "C" comment, a list of references to C.xxx,
    45  // a list of exported functions, and the actual AST, to be rewritten and
    46  // printed.
    47  func (f *File) ReadGo(name string) {
    48  	// Create absolute path for file, so that it will be used in error
    49  	// messages and recorded in debug line number information.
    50  	// This matches the rest of the toolchain. See golang.org/issue/5122.
    51  	if aname, err := filepath.Abs(name); err == nil {
    52  		name = aname
    53  	}
    54  
    55  	// Two different parses: once with comments, once without.
    56  	// The printer is not good enough at printing comments in the
    57  	// right place when we start editing the AST behind its back,
    58  	// so we use ast1 to look for the doc comments on import "C"
    59  	// and on exported functions, and we use ast2 for translating
    60  	// and reprinting.
    61  	ast1 := parse(name, parser.ParseComments)
    62  	ast2 := parse(name, 0)
    63  
    64  	f.Package = ast1.Name.Name
    65  	f.Name = make(map[string]*Name)
    66  
    67  	// In ast1, find the import "C" line and get any extra C preamble.
    68  	sawC := false
    69  	for _, decl := range ast1.Decls {
    70  		d, ok := decl.(*ast.GenDecl)
    71  		if !ok {
    72  			continue
    73  		}
    74  		for _, spec := range d.Specs {
    75  			s, ok := spec.(*ast.ImportSpec)
    76  			if !ok || string(s.Path.Value) != `"C"` {
    77  				continue
    78  			}
    79  			sawC = true
    80  			if s.Name != nil {
    81  				error_(s.Path.Pos(), `cannot rename import "C"`)
    82  			}
    83  			cg := s.Doc
    84  			if cg == nil && len(d.Specs) == 1 {
    85  				cg = d.Doc
    86  			}
    87  			if cg != nil {
    88  				f.Preamble += fmt.Sprintf("#line %d %q\n", sourceLine(cg), name)
    89  				f.Preamble += commentText(cg) + "\n"
    90  			}
    91  		}
    92  	}
    93  	if !sawC {
    94  		error_(token.NoPos, `cannot find import "C"`)
    95  	}
    96  
    97  	// In ast2, strip the import "C" line.
    98  	w := 0
    99  	for _, decl := range ast2.Decls {
   100  		d, ok := decl.(*ast.GenDecl)
   101  		if !ok {
   102  			ast2.Decls[w] = decl
   103  			w++
   104  			continue
   105  		}
   106  		ws := 0
   107  		for _, spec := range d.Specs {
   108  			s, ok := spec.(*ast.ImportSpec)
   109  			if !ok || string(s.Path.Value) != `"C"` {
   110  				d.Specs[ws] = spec
   111  				ws++
   112  			}
   113  		}
   114  		if ws == 0 {
   115  			continue
   116  		}
   117  		d.Specs = d.Specs[0:ws]
   118  		ast2.Decls[w] = d
   119  		w++
   120  	}
   121  	ast2.Decls = ast2.Decls[0:w]
   122  
   123  	// Accumulate pointers to uses of C.x.
   124  	if f.Ref == nil {
   125  		f.Ref = make([]*Ref, 0, 8)
   126  	}
   127  	f.walk(ast2, "prog", (*File).saveExprs)
   128  
   129  	// Accumulate exported functions.
   130  	// The comments are only on ast1 but we need to
   131  	// save the function bodies from ast2.
   132  	// The first walk fills in ExpFunc, and the
   133  	// second walk changes the entries to
   134  	// refer to ast2 instead.
   135  	f.walk(ast1, "prog", (*File).saveExport)
   136  	f.walk(ast2, "prog", (*File).saveExport2)
   137  
   138  	f.Comments = ast1.Comments
   139  	f.AST = ast2
   140  }
   141  
   142  // Like ast.CommentGroup's Text method but preserves
   143  // leading blank lines, so that line numbers line up.
   144  func commentText(g *ast.CommentGroup) string {
   145  	if g == nil {
   146  		return ""
   147  	}
   148  	var pieces []string
   149  	for _, com := range g.List {
   150  		c := string(com.Text)
   151  		// Remove comment markers.
   152  		// The parser has given us exactly the comment text.
   153  		switch c[1] {
   154  		case '/':
   155  			//-style comment (no newline at the end)
   156  			c = c[2:] + "\n"
   157  		case '*':
   158  			/*-style comment */
   159  			c = c[2 : len(c)-2]
   160  		}
   161  		pieces = append(pieces, c)
   162  	}
   163  	return strings.Join(pieces, "")
   164  }
   165  
   166  // Save various references we are going to need later.
   167  func (f *File) saveExprs(x interface{}, context string) {
   168  	switch x := x.(type) {
   169  	case *ast.Expr:
   170  		switch (*x).(type) {
   171  		case *ast.SelectorExpr:
   172  			f.saveRef(x, context)
   173  		}
   174  	case *ast.CallExpr:
   175  		f.saveCall(x)
   176  	}
   177  }
   178  
   179  // Save references to C.xxx for later processing.
   180  func (f *File) saveRef(n *ast.Expr, context string) {
   181  	sel := (*n).(*ast.SelectorExpr)
   182  	// For now, assume that the only instance of capital C is when
   183  	// used as the imported package identifier.
   184  	// The parser should take care of scoping in the future, so
   185  	// that we will be able to distinguish a "top-level C" from a
   186  	// local C.
   187  	if l, ok := sel.X.(*ast.Ident); !ok || l.Name != "C" {
   188  		return
   189  	}
   190  	if context == "as2" {
   191  		context = "expr"
   192  	}
   193  	if context == "embed-type" {
   194  		error_(sel.Pos(), "cannot embed C type")
   195  	}
   196  	goname := sel.Sel.Name
   197  	if goname == "errno" {
   198  		error_(sel.Pos(), "cannot refer to errno directly; see documentation")
   199  		return
   200  	}
   201  	if goname == "_CMalloc" {
   202  		error_(sel.Pos(), "cannot refer to C._CMalloc; use C.malloc")
   203  		return
   204  	}
   205  	if goname == "malloc" {
   206  		goname = "_CMalloc"
   207  	}
   208  	name := f.Name[goname]
   209  	if name == nil {
   210  		name = &Name{
   211  			Go: goname,
   212  		}
   213  		f.Name[goname] = name
   214  	}
   215  	f.Ref = append(f.Ref, &Ref{
   216  		Name:    name,
   217  		Expr:    n,
   218  		Context: context,
   219  	})
   220  }
   221  
   222  // Save calls to C.xxx for later processing.
   223  func (f *File) saveCall(call *ast.CallExpr) {
   224  	sel, ok := call.Fun.(*ast.SelectorExpr)
   225  	if !ok {
   226  		return
   227  	}
   228  	if l, ok := sel.X.(*ast.Ident); !ok || l.Name != "C" {
   229  		return
   230  	}
   231  	f.Calls = append(f.Calls, call)
   232  }
   233  
   234  // If a function should be exported add it to ExpFunc.
   235  func (f *File) saveExport(x interface{}, context string) {
   236  	n, ok := x.(*ast.FuncDecl)
   237  	if !ok {
   238  		return
   239  	}
   240  
   241  	if n.Doc == nil {
   242  		return
   243  	}
   244  	for _, c := range n.Doc.List {
   245  		if !strings.HasPrefix(string(c.Text), "//export ") {
   246  			continue
   247  		}
   248  
   249  		name := strings.TrimSpace(string(c.Text[9:]))
   250  		if name == "" {
   251  			error_(c.Pos(), "export missing name")
   252  		}
   253  
   254  		if name != n.Name.Name {
   255  			error_(c.Pos(), "export comment has wrong name %q, want %q", name, n.Name.Name)
   256  		}
   257  
   258  		doc := ""
   259  		for _, c1 := range n.Doc.List {
   260  			if c1 != c {
   261  				doc += c1.Text + "\n"
   262  			}
   263  		}
   264  
   265  		f.ExpFunc = append(f.ExpFunc, &ExpFunc{
   266  			Func:    n,
   267  			ExpName: name,
   268  			Doc:     doc,
   269  		})
   270  		break
   271  	}
   272  }
   273  
   274  // Make f.ExpFunc[i] point at the Func from this AST instead of the other one.
   275  func (f *File) saveExport2(x interface{}, context string) {
   276  	n, ok := x.(*ast.FuncDecl)
   277  	if !ok {
   278  		return
   279  	}
   280  
   281  	for _, exp := range f.ExpFunc {
   282  		if exp.Func.Name.Name == n.Name.Name {
   283  			exp.Func = n
   284  			break
   285  		}
   286  	}
   287  }
   288  
   289  // walk walks the AST x, calling visit(f, x, context) for each node.
   290  func (f *File) walk(x interface{}, context string, visit func(*File, interface{}, string)) {
   291  	visit(f, x, context)
   292  	switch n := x.(type) {
   293  	case *ast.Expr:
   294  		f.walk(*n, context, visit)
   295  
   296  	// everything else just recurs
   297  	default:
   298  		error_(token.NoPos, "unexpected type %T in walk", x, visit)
   299  		panic("unexpected type")
   300  
   301  	case nil:
   302  
   303  	// These are ordered and grouped to match ../../go/ast/ast.go
   304  	case *ast.Field:
   305  		if len(n.Names) == 0 && context == "field" {
   306  			f.walk(&n.Type, "embed-type", visit)
   307  		} else {
   308  			f.walk(&n.Type, "type", visit)
   309  		}
   310  	case *ast.FieldList:
   311  		for _, field := range n.List {
   312  			f.walk(field, context, visit)
   313  		}
   314  	case *ast.BadExpr:
   315  	case *ast.Ident:
   316  	case *ast.Ellipsis:
   317  	case *ast.BasicLit:
   318  	case *ast.FuncLit:
   319  		f.walk(n.Type, "type", visit)
   320  		f.walk(n.Body, "stmt", visit)
   321  	case *ast.CompositeLit:
   322  		f.walk(&n.Type, "type", visit)
   323  		f.walk(n.Elts, "expr", visit)
   324  	case *ast.ParenExpr:
   325  		f.walk(&n.X, context, visit)
   326  	case *ast.SelectorExpr:
   327  		f.walk(&n.X, "selector", visit)
   328  	case *ast.IndexExpr:
   329  		f.walk(&n.X, "expr", visit)
   330  		f.walk(&n.Index, "expr", visit)
   331  	case *ast.SliceExpr:
   332  		f.walk(&n.X, "expr", visit)
   333  		if n.Low != nil {
   334  			f.walk(&n.Low, "expr", visit)
   335  		}
   336  		if n.High != nil {
   337  			f.walk(&n.High, "expr", visit)
   338  		}
   339  		if n.Max != nil {
   340  			f.walk(&n.Max, "expr", visit)
   341  		}
   342  	case *ast.TypeAssertExpr:
   343  		f.walk(&n.X, "expr", visit)
   344  		f.walk(&n.Type, "type", visit)
   345  	case *ast.CallExpr:
   346  		if context == "as2" {
   347  			f.walk(&n.Fun, "call2", visit)
   348  		} else {
   349  			f.walk(&n.Fun, "call", visit)
   350  		}
   351  		f.walk(n.Args, "expr", visit)
   352  	case *ast.StarExpr:
   353  		f.walk(&n.X, context, visit)
   354  	case *ast.UnaryExpr:
   355  		f.walk(&n.X, "expr", visit)
   356  	case *ast.BinaryExpr:
   357  		f.walk(&n.X, "expr", visit)
   358  		f.walk(&n.Y, "expr", visit)
   359  	case *ast.KeyValueExpr:
   360  		f.walk(&n.Key, "expr", visit)
   361  		f.walk(&n.Value, "expr", visit)
   362  
   363  	case *ast.ArrayType:
   364  		f.walk(&n.Len, "expr", visit)
   365  		f.walk(&n.Elt, "type", visit)
   366  	case *ast.StructType:
   367  		f.walk(n.Fields, "field", visit)
   368  	case *ast.FuncType:
   369  		f.walk(n.Params, "param", visit)
   370  		if n.Results != nil {
   371  			f.walk(n.Results, "param", visit)
   372  		}
   373  	case *ast.InterfaceType:
   374  		f.walk(n.Methods, "field", visit)
   375  	case *ast.MapType:
   376  		f.walk(&n.Key, "type", visit)
   377  		f.walk(&n.Value, "type", visit)
   378  	case *ast.ChanType:
   379  		f.walk(&n.Value, "type", visit)
   380  
   381  	case *ast.BadStmt:
   382  	case *ast.DeclStmt:
   383  		f.walk(n.Decl, "decl", visit)
   384  	case *ast.EmptyStmt:
   385  	case *ast.LabeledStmt:
   386  		f.walk(n.Stmt, "stmt", visit)
   387  	case *ast.ExprStmt:
   388  		f.walk(&n.X, "expr", visit)
   389  	case *ast.SendStmt:
   390  		f.walk(&n.Chan, "expr", visit)
   391  		f.walk(&n.Value, "expr", visit)
   392  	case *ast.IncDecStmt:
   393  		f.walk(&n.X, "expr", visit)
   394  	case *ast.AssignStmt:
   395  		f.walk(n.Lhs, "expr", visit)
   396  		if len(n.Lhs) == 2 && len(n.Rhs) == 1 {
   397  			f.walk(n.Rhs, "as2", visit)
   398  		} else {
   399  			f.walk(n.Rhs, "expr", visit)
   400  		}
   401  	case *ast.GoStmt:
   402  		f.walk(n.Call, "expr", visit)
   403  	case *ast.DeferStmt:
   404  		f.walk(n.Call, "expr", visit)
   405  	case *ast.ReturnStmt:
   406  		f.walk(n.Results, "expr", visit)
   407  	case *ast.BranchStmt:
   408  	case *ast.BlockStmt:
   409  		f.walk(n.List, context, visit)
   410  	case *ast.IfStmt:
   411  		f.walk(n.Init, "stmt", visit)
   412  		f.walk(&n.Cond, "expr", visit)
   413  		f.walk(n.Body, "stmt", visit)
   414  		f.walk(n.Else, "stmt", visit)
   415  	case *ast.CaseClause:
   416  		if context == "typeswitch" {
   417  			context = "type"
   418  		} else {
   419  			context = "expr"
   420  		}
   421  		f.walk(n.List, context, visit)
   422  		f.walk(n.Body, "stmt", visit)
   423  	case *ast.SwitchStmt:
   424  		f.walk(n.Init, "stmt", visit)
   425  		f.walk(&n.Tag, "expr", visit)
   426  		f.walk(n.Body, "switch", visit)
   427  	case *ast.TypeSwitchStmt:
   428  		f.walk(n.Init, "stmt", visit)
   429  		f.walk(n.Assign, "stmt", visit)
   430  		f.walk(n.Body, "typeswitch", visit)
   431  	case *ast.CommClause:
   432  		f.walk(n.Comm, "stmt", visit)
   433  		f.walk(n.Body, "stmt", visit)
   434  	case *ast.SelectStmt:
   435  		f.walk(n.Body, "stmt", visit)
   436  	case *ast.ForStmt:
   437  		f.walk(n.Init, "stmt", visit)
   438  		f.walk(&n.Cond, "expr", visit)
   439  		f.walk(n.Post, "stmt", visit)
   440  		f.walk(n.Body, "stmt", visit)
   441  	case *ast.RangeStmt:
   442  		f.walk(&n.Key, "expr", visit)
   443  		f.walk(&n.Value, "expr", visit)
   444  		f.walk(&n.X, "expr", visit)
   445  		f.walk(n.Body, "stmt", visit)
   446  
   447  	case *ast.ImportSpec:
   448  	case *ast.ValueSpec:
   449  		f.walk(&n.Type, "type", visit)
   450  		if len(n.Names) == 2 && len(n.Values) == 1 {
   451  			f.walk(&n.Values[0], "as2", visit)
   452  		} else {
   453  			f.walk(n.Values, "expr", visit)
   454  		}
   455  	case *ast.TypeSpec:
   456  		f.walk(&n.Type, "type", visit)
   457  
   458  	case *ast.BadDecl:
   459  	case *ast.GenDecl:
   460  		f.walk(n.Specs, "spec", visit)
   461  	case *ast.FuncDecl:
   462  		if n.Recv != nil {
   463  			f.walk(n.Recv, "param", visit)
   464  		}
   465  		f.walk(n.Type, "type", visit)
   466  		if n.Body != nil {
   467  			f.walk(n.Body, "stmt", visit)
   468  		}
   469  
   470  	case *ast.File:
   471  		f.walk(n.Decls, "decl", visit)
   472  
   473  	case *ast.Package:
   474  		for _, file := range n.Files {
   475  			f.walk(file, "file", visit)
   476  		}
   477  
   478  	case []ast.Decl:
   479  		for _, d := range n {
   480  			f.walk(d, context, visit)
   481  		}
   482  	case []ast.Expr:
   483  		for i := range n {
   484  			f.walk(&n[i], context, visit)
   485  		}
   486  	case []ast.Stmt:
   487  		for _, s := range n {
   488  			f.walk(s, context, visit)
   489  		}
   490  	case []ast.Spec:
   491  		for _, s := range n {
   492  			f.walk(s, context, visit)
   493  		}
   494  	}
   495  }