github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/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, src []byte, flags parser.Mode) *ast.File {
    21  	ast1, err := parser.ParseFile(fset, name, src, 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  // ParseGo populates f with information learned from the Go source code
    43  // which was read from the named file. 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) ParseGo(name string, src []byte) {
    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, src, parser.ParseComments)
    62  	ast2 := parse(name, src, 0)
    63  
    64  	f.Package = ast1.Name.Name
    65  	f.Name = make(map[string]*Name)
    66  	f.NamePos = make(map[*Name]token.Pos)
    67  
    68  	// In ast1, find the import "C" line and get any extra C preamble.
    69  	sawC := false
    70  	for _, decl := range ast1.Decls {
    71  		d, ok := decl.(*ast.GenDecl)
    72  		if !ok {
    73  			continue
    74  		}
    75  		for _, spec := range d.Specs {
    76  			s, ok := spec.(*ast.ImportSpec)
    77  			if !ok || s.Path.Value != `"C"` {
    78  				continue
    79  			}
    80  			sawC = true
    81  			if s.Name != nil {
    82  				error_(s.Path.Pos(), `cannot rename import "C"`)
    83  			}
    84  			cg := s.Doc
    85  			if cg == nil && len(d.Specs) == 1 {
    86  				cg = d.Doc
    87  			}
    88  			if cg != nil {
    89  				f.Preamble += fmt.Sprintf("#line %d %q\n", sourceLine(cg), name)
    90  				f.Preamble += commentText(cg) + "\n"
    91  				f.Preamble += "#line 1 \"cgo-generated-wrapper\"\n"
    92  			}
    93  		}
    94  	}
    95  	if !sawC {
    96  		error_(token.NoPos, `cannot find import "C"`)
    97  	}
    98  
    99  	// In ast2, strip the import "C" line.
   100  	w := 0
   101  	for _, decl := range ast2.Decls {
   102  		d, ok := decl.(*ast.GenDecl)
   103  		if !ok {
   104  			ast2.Decls[w] = decl
   105  			w++
   106  			continue
   107  		}
   108  		ws := 0
   109  		for _, spec := range d.Specs {
   110  			s, ok := spec.(*ast.ImportSpec)
   111  			if !ok || s.Path.Value != `"C"` {
   112  				d.Specs[ws] = spec
   113  				ws++
   114  			}
   115  		}
   116  		if ws == 0 {
   117  			continue
   118  		}
   119  		d.Specs = d.Specs[0:ws]
   120  		ast2.Decls[w] = d
   121  		w++
   122  	}
   123  	ast2.Decls = ast2.Decls[0:w]
   124  
   125  	// Accumulate pointers to uses of C.x.
   126  	if f.Ref == nil {
   127  		f.Ref = make([]*Ref, 0, 8)
   128  	}
   129  	f.walk(ast2, ctxProg, (*File).saveExprs)
   130  
   131  	// Accumulate exported functions.
   132  	// The comments are only on ast1 but we need to
   133  	// save the function bodies from ast2.
   134  	// The first walk fills in ExpFunc, and the
   135  	// second walk changes the entries to
   136  	// refer to ast2 instead.
   137  	f.walk(ast1, ctxProg, (*File).saveExport)
   138  	f.walk(ast2, ctxProg, (*File).saveExport2)
   139  
   140  	f.Comments = ast1.Comments
   141  	f.AST = ast2
   142  }
   143  
   144  // Like ast.CommentGroup's Text method but preserves
   145  // leading blank lines, so that line numbers line up.
   146  func commentText(g *ast.CommentGroup) string {
   147  	var pieces []string
   148  	for _, com := range g.List {
   149  		c := com.Text
   150  		// Remove comment markers.
   151  		// The parser has given us exactly the comment text.
   152  		switch c[1] {
   153  		case '/':
   154  			//-style comment (no newline at the end)
   155  			c = c[2:] + "\n"
   156  		case '*':
   157  			/*-style comment */
   158  			c = c[2 : len(c)-2]
   159  		}
   160  		pieces = append(pieces, c)
   161  	}
   162  	return strings.Join(pieces, "")
   163  }
   164  
   165  // Save various references we are going to need later.
   166  func (f *File) saveExprs(x interface{}, context astContext) {
   167  	switch x := x.(type) {
   168  	case *ast.Expr:
   169  		switch (*x).(type) {
   170  		case *ast.SelectorExpr:
   171  			f.saveRef(x, context)
   172  		}
   173  	case *ast.CallExpr:
   174  		f.saveCall(x, context)
   175  	}
   176  }
   177  
   178  // Save references to C.xxx for later processing.
   179  func (f *File) saveRef(n *ast.Expr, context astContext) {
   180  	sel := (*n).(*ast.SelectorExpr)
   181  	// For now, assume that the only instance of capital C is when
   182  	// used as the imported package identifier.
   183  	// The parser should take care of scoping in the future, so
   184  	// that we will be able to distinguish a "top-level C" from a
   185  	// local C.
   186  	if l, ok := sel.X.(*ast.Ident); !ok || l.Name != "C" {
   187  		return
   188  	}
   189  	if context == ctxAssign2 {
   190  		context = ctxExpr
   191  	}
   192  	if context == ctxEmbedType {
   193  		error_(sel.Pos(), "cannot embed C type")
   194  	}
   195  	goname := sel.Sel.Name
   196  	if goname == "errno" {
   197  		error_(sel.Pos(), "cannot refer to errno directly; see documentation")
   198  		return
   199  	}
   200  	if goname == "_CMalloc" {
   201  		error_(sel.Pos(), "cannot refer to C._CMalloc; use C.malloc")
   202  		return
   203  	}
   204  	if goname == "malloc" {
   205  		goname = "_CMalloc"
   206  	}
   207  	name := f.Name[goname]
   208  	if name == nil {
   209  		name = &Name{
   210  			Go: goname,
   211  		}
   212  		f.Name[goname] = name
   213  		f.NamePos[name] = sel.Pos()
   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, context astContext) {
   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  	c := &Call{Call: call, Deferred: context == ctxDefer}
   232  	f.Calls = append(f.Calls, c)
   233  }
   234  
   235  // If a function should be exported add it to ExpFunc.
   236  func (f *File) saveExport(x interface{}, context astContext) {
   237  	n, ok := x.(*ast.FuncDecl)
   238  	if !ok {
   239  		return
   240  	}
   241  
   242  	if n.Doc == nil {
   243  		return
   244  	}
   245  	for _, c := range n.Doc.List {
   246  		if !strings.HasPrefix(c.Text, "//export ") {
   247  			continue
   248  		}
   249  
   250  		name := strings.TrimSpace(c.Text[9:])
   251  		if name == "" {
   252  			error_(c.Pos(), "export missing name")
   253  		}
   254  
   255  		if name != n.Name.Name {
   256  			error_(c.Pos(), "export comment has wrong name %q, want %q", name, n.Name.Name)
   257  		}
   258  
   259  		doc := ""
   260  		for _, c1 := range n.Doc.List {
   261  			if c1 != c {
   262  				doc += c1.Text + "\n"
   263  			}
   264  		}
   265  
   266  		f.ExpFunc = append(f.ExpFunc, &ExpFunc{
   267  			Func:    n,
   268  			ExpName: name,
   269  			Doc:     doc,
   270  		})
   271  		break
   272  	}
   273  }
   274  
   275  // Make f.ExpFunc[i] point at the Func from this AST instead of the other one.
   276  func (f *File) saveExport2(x interface{}, context astContext) {
   277  	n, ok := x.(*ast.FuncDecl)
   278  	if !ok {
   279  		return
   280  	}
   281  
   282  	for _, exp := range f.ExpFunc {
   283  		if exp.Func.Name.Name == n.Name.Name {
   284  			exp.Func = n
   285  			break
   286  		}
   287  	}
   288  }
   289  
   290  type astContext int
   291  
   292  const (
   293  	ctxProg astContext = iota
   294  	ctxEmbedType
   295  	ctxType
   296  	ctxStmt
   297  	ctxExpr
   298  	ctxField
   299  	ctxParam
   300  	ctxAssign2 // assignment of a single expression to two variables
   301  	ctxSwitch
   302  	ctxTypeSwitch
   303  	ctxFile
   304  	ctxDecl
   305  	ctxSpec
   306  	ctxDefer
   307  	ctxCall  // any function call other than ctxCall2
   308  	ctxCall2 // function call whose result is assigned to two variables
   309  	ctxSelector
   310  )
   311  
   312  // walk walks the AST x, calling visit(f, x, context) for each node.
   313  func (f *File) walk(x interface{}, context astContext, visit func(*File, interface{}, astContext)) {
   314  	visit(f, x, context)
   315  	switch n := x.(type) {
   316  	case *ast.Expr:
   317  		f.walk(*n, context, visit)
   318  
   319  	// everything else just recurs
   320  	default:
   321  		error_(token.NoPos, "unexpected type %T in walk", x)
   322  		panic("unexpected type")
   323  
   324  	case nil:
   325  
   326  	// These are ordered and grouped to match ../../go/ast/ast.go
   327  	case *ast.Field:
   328  		if len(n.Names) == 0 && context == ctxField {
   329  			f.walk(&n.Type, ctxEmbedType, visit)
   330  		} else {
   331  			f.walk(&n.Type, ctxType, visit)
   332  		}
   333  	case *ast.FieldList:
   334  		for _, field := range n.List {
   335  			f.walk(field, context, visit)
   336  		}
   337  	case *ast.BadExpr:
   338  	case *ast.Ident:
   339  	case *ast.Ellipsis:
   340  	case *ast.BasicLit:
   341  	case *ast.FuncLit:
   342  		f.walk(n.Type, ctxType, visit)
   343  		f.walk(n.Body, ctxStmt, visit)
   344  	case *ast.CompositeLit:
   345  		f.walk(&n.Type, ctxType, visit)
   346  		f.walk(n.Elts, ctxExpr, visit)
   347  	case *ast.ParenExpr:
   348  		f.walk(&n.X, context, visit)
   349  	case *ast.SelectorExpr:
   350  		f.walk(&n.X, ctxSelector, visit)
   351  	case *ast.IndexExpr:
   352  		f.walk(&n.X, ctxExpr, visit)
   353  		f.walk(&n.Index, ctxExpr, visit)
   354  	case *ast.SliceExpr:
   355  		f.walk(&n.X, ctxExpr, visit)
   356  		if n.Low != nil {
   357  			f.walk(&n.Low, ctxExpr, visit)
   358  		}
   359  		if n.High != nil {
   360  			f.walk(&n.High, ctxExpr, visit)
   361  		}
   362  		if n.Max != nil {
   363  			f.walk(&n.Max, ctxExpr, visit)
   364  		}
   365  	case *ast.TypeAssertExpr:
   366  		f.walk(&n.X, ctxExpr, visit)
   367  		f.walk(&n.Type, ctxType, visit)
   368  	case *ast.CallExpr:
   369  		if context == ctxAssign2 {
   370  			f.walk(&n.Fun, ctxCall2, visit)
   371  		} else {
   372  			f.walk(&n.Fun, ctxCall, visit)
   373  		}
   374  		f.walk(n.Args, ctxExpr, visit)
   375  	case *ast.StarExpr:
   376  		f.walk(&n.X, context, visit)
   377  	case *ast.UnaryExpr:
   378  		f.walk(&n.X, ctxExpr, visit)
   379  	case *ast.BinaryExpr:
   380  		f.walk(&n.X, ctxExpr, visit)
   381  		f.walk(&n.Y, ctxExpr, visit)
   382  	case *ast.KeyValueExpr:
   383  		f.walk(&n.Key, ctxExpr, visit)
   384  		f.walk(&n.Value, ctxExpr, visit)
   385  
   386  	case *ast.ArrayType:
   387  		f.walk(&n.Len, ctxExpr, visit)
   388  		f.walk(&n.Elt, ctxType, visit)
   389  	case *ast.StructType:
   390  		f.walk(n.Fields, ctxField, visit)
   391  	case *ast.FuncType:
   392  		f.walk(n.Params, ctxParam, visit)
   393  		if n.Results != nil {
   394  			f.walk(n.Results, ctxParam, visit)
   395  		}
   396  	case *ast.InterfaceType:
   397  		f.walk(n.Methods, ctxField, visit)
   398  	case *ast.MapType:
   399  		f.walk(&n.Key, ctxType, visit)
   400  		f.walk(&n.Value, ctxType, visit)
   401  	case *ast.ChanType:
   402  		f.walk(&n.Value, ctxType, visit)
   403  
   404  	case *ast.BadStmt:
   405  	case *ast.DeclStmt:
   406  		f.walk(n.Decl, ctxDecl, visit)
   407  	case *ast.EmptyStmt:
   408  	case *ast.LabeledStmt:
   409  		f.walk(n.Stmt, ctxStmt, visit)
   410  	case *ast.ExprStmt:
   411  		f.walk(&n.X, ctxExpr, visit)
   412  	case *ast.SendStmt:
   413  		f.walk(&n.Chan, ctxExpr, visit)
   414  		f.walk(&n.Value, ctxExpr, visit)
   415  	case *ast.IncDecStmt:
   416  		f.walk(&n.X, ctxExpr, visit)
   417  	case *ast.AssignStmt:
   418  		f.walk(n.Lhs, ctxExpr, visit)
   419  		if len(n.Lhs) == 2 && len(n.Rhs) == 1 {
   420  			f.walk(n.Rhs, ctxAssign2, visit)
   421  		} else {
   422  			f.walk(n.Rhs, ctxExpr, visit)
   423  		}
   424  	case *ast.GoStmt:
   425  		f.walk(n.Call, ctxExpr, visit)
   426  	case *ast.DeferStmt:
   427  		f.walk(n.Call, ctxDefer, visit)
   428  	case *ast.ReturnStmt:
   429  		f.walk(n.Results, ctxExpr, visit)
   430  	case *ast.BranchStmt:
   431  	case *ast.BlockStmt:
   432  		f.walk(n.List, context, visit)
   433  	case *ast.IfStmt:
   434  		f.walk(n.Init, ctxStmt, visit)
   435  		f.walk(&n.Cond, ctxExpr, visit)
   436  		f.walk(n.Body, ctxStmt, visit)
   437  		f.walk(n.Else, ctxStmt, visit)
   438  	case *ast.CaseClause:
   439  		if context == ctxTypeSwitch {
   440  			context = ctxType
   441  		} else {
   442  			context = ctxExpr
   443  		}
   444  		f.walk(n.List, context, visit)
   445  		f.walk(n.Body, ctxStmt, visit)
   446  	case *ast.SwitchStmt:
   447  		f.walk(n.Init, ctxStmt, visit)
   448  		f.walk(&n.Tag, ctxExpr, visit)
   449  		f.walk(n.Body, ctxSwitch, visit)
   450  	case *ast.TypeSwitchStmt:
   451  		f.walk(n.Init, ctxStmt, visit)
   452  		f.walk(n.Assign, ctxStmt, visit)
   453  		f.walk(n.Body, ctxTypeSwitch, visit)
   454  	case *ast.CommClause:
   455  		f.walk(n.Comm, ctxStmt, visit)
   456  		f.walk(n.Body, ctxStmt, visit)
   457  	case *ast.SelectStmt:
   458  		f.walk(n.Body, ctxStmt, visit)
   459  	case *ast.ForStmt:
   460  		f.walk(n.Init, ctxStmt, visit)
   461  		f.walk(&n.Cond, ctxExpr, visit)
   462  		f.walk(n.Post, ctxStmt, visit)
   463  		f.walk(n.Body, ctxStmt, visit)
   464  	case *ast.RangeStmt:
   465  		f.walk(&n.Key, ctxExpr, visit)
   466  		f.walk(&n.Value, ctxExpr, visit)
   467  		f.walk(&n.X, ctxExpr, visit)
   468  		f.walk(n.Body, ctxStmt, visit)
   469  
   470  	case *ast.ImportSpec:
   471  	case *ast.ValueSpec:
   472  		f.walk(&n.Type, ctxType, visit)
   473  		if len(n.Names) == 2 && len(n.Values) == 1 {
   474  			f.walk(&n.Values[0], ctxAssign2, visit)
   475  		} else {
   476  			f.walk(n.Values, ctxExpr, visit)
   477  		}
   478  	case *ast.TypeSpec:
   479  		f.walk(&n.Type, ctxType, visit)
   480  
   481  	case *ast.BadDecl:
   482  	case *ast.GenDecl:
   483  		f.walk(n.Specs, ctxSpec, visit)
   484  	case *ast.FuncDecl:
   485  		if n.Recv != nil {
   486  			f.walk(n.Recv, ctxParam, visit)
   487  		}
   488  		f.walk(n.Type, ctxType, visit)
   489  		if n.Body != nil {
   490  			f.walk(n.Body, ctxStmt, visit)
   491  		}
   492  
   493  	case *ast.File:
   494  		f.walk(n.Decls, ctxDecl, visit)
   495  
   496  	case *ast.Package:
   497  		for _, file := range n.Files {
   498  			f.walk(file, ctxFile, visit)
   499  		}
   500  
   501  	case []ast.Decl:
   502  		for _, d := range n {
   503  			f.walk(d, context, visit)
   504  		}
   505  	case []ast.Expr:
   506  		for i := range n {
   507  			f.walk(&n[i], context, visit)
   508  		}
   509  	case []ast.Stmt:
   510  		for _, s := range n {
   511  			f.walk(s, context, visit)
   512  		}
   513  	case []ast.Spec:
   514  		for _, s := range n {
   515  			f.walk(s, context, visit)
   516  		}
   517  	}
   518  }