2016-07-22 20:15:38 +02:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
2015-01-07 00:26:02 +01:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Parse input AST and prepare Prog structure.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/parser"
|
|
|
|
"go/scanner"
|
|
|
|
"go/token"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2017-09-14 19:11:35 +02:00
|
|
|
func parse(name string, src []byte, flags parser.Mode) *ast.File {
|
|
|
|
ast1, err := parser.ParseFile(fset, name, src, flags)
|
2015-01-07 00:26:02 +01:00
|
|
|
if err != nil {
|
|
|
|
if list, ok := err.(scanner.ErrorList); ok {
|
|
|
|
// If err is a scanner.ErrorList, its String will print just
|
|
|
|
// the first error and then (+n more errors).
|
|
|
|
// Instead, turn it into a new Error that will return
|
|
|
|
// details for all the errors.
|
|
|
|
for _, e := range list {
|
|
|
|
fmt.Fprintln(os.Stderr, e)
|
|
|
|
}
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
fatalf("parsing %s: %s", name, err)
|
|
|
|
}
|
|
|
|
return ast1
|
|
|
|
}
|
|
|
|
|
|
|
|
func sourceLine(n ast.Node) int {
|
|
|
|
return fset.Position(n.Pos()).Line
|
|
|
|
}
|
|
|
|
|
2017-09-14 19:11:35 +02:00
|
|
|
// ParseGo populates f with information learned from the Go source code
|
|
|
|
// which was read from the named file. It gathers the C preamble
|
2015-01-07 00:26:02 +01:00
|
|
|
// 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.
|
2017-09-14 19:11:35 +02:00
|
|
|
func (f *File) ParseGo(name string, src []byte) {
|
2015-01-07 00:26:02 +01:00
|
|
|
// Create absolute path for file, so that it will be used in error
|
|
|
|
// messages and recorded in debug line number information.
|
|
|
|
// This matches the rest of the toolchain. See golang.org/issue/5122.
|
|
|
|
if aname, err := filepath.Abs(name); err == nil {
|
|
|
|
name = aname
|
|
|
|
}
|
|
|
|
|
|
|
|
// Two different parses: once with comments, once without.
|
|
|
|
// The printer is not good enough at printing comments in the
|
|
|
|
// right place when we start editing the AST behind its back,
|
|
|
|
// so we use ast1 to look for the doc comments on import "C"
|
|
|
|
// and on exported functions, and we use ast2 for translating
|
|
|
|
// and reprinting.
|
2018-01-09 02:23:08 +01:00
|
|
|
// In cgo mode, we ignore ast2 and just apply edits directly
|
|
|
|
// the text behind ast1. In godefs mode we modify and print ast2.
|
2017-09-14 19:11:35 +02:00
|
|
|
ast1 := parse(name, src, parser.ParseComments)
|
|
|
|
ast2 := parse(name, src, 0)
|
2015-01-07 00:26:02 +01:00
|
|
|
|
|
|
|
f.Package = ast1.Name.Name
|
|
|
|
f.Name = make(map[string]*Name)
|
2018-01-09 02:23:08 +01:00
|
|
|
f.NamePos = make(map[*Name]token.Pos)
|
2015-01-07 00:26:02 +01:00
|
|
|
|
|
|
|
// In ast1, find the import "C" line and get any extra C preamble.
|
|
|
|
sawC := false
|
|
|
|
for _, decl := range ast1.Decls {
|
|
|
|
d, ok := decl.(*ast.GenDecl)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, spec := range d.Specs {
|
|
|
|
s, ok := spec.(*ast.ImportSpec)
|
2016-07-22 20:15:38 +02:00
|
|
|
if !ok || s.Path.Value != `"C"` {
|
2015-01-07 00:26:02 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
sawC = true
|
|
|
|
if s.Name != nil {
|
|
|
|
error_(s.Path.Pos(), `cannot rename import "C"`)
|
|
|
|
}
|
|
|
|
cg := s.Doc
|
|
|
|
if cg == nil && len(d.Specs) == 1 {
|
|
|
|
cg = d.Doc
|
|
|
|
}
|
|
|
|
if cg != nil {
|
|
|
|
f.Preamble += fmt.Sprintf("#line %d %q\n", sourceLine(cg), name)
|
|
|
|
f.Preamble += commentText(cg) + "\n"
|
2017-01-14 01:05:42 +01:00
|
|
|
f.Preamble += "#line 1 \"cgo-generated-wrapper\"\n"
|
2015-01-07 00:26:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !sawC {
|
2018-09-24 23:46:21 +02:00
|
|
|
error_(ast1.Package, `cannot find import "C"`)
|
2015-01-07 00:26:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// In ast2, strip the import "C" line.
|
2018-01-09 02:23:08 +01:00
|
|
|
if *godefs {
|
|
|
|
w := 0
|
|
|
|
for _, decl := range ast2.Decls {
|
|
|
|
d, ok := decl.(*ast.GenDecl)
|
|
|
|
if !ok {
|
|
|
|
ast2.Decls[w] = decl
|
|
|
|
w++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ws := 0
|
|
|
|
for _, spec := range d.Specs {
|
|
|
|
s, ok := spec.(*ast.ImportSpec)
|
|
|
|
if !ok || s.Path.Value != `"C"` {
|
|
|
|
d.Specs[ws] = spec
|
|
|
|
ws++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ws == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
d.Specs = d.Specs[0:ws]
|
|
|
|
ast2.Decls[w] = d
|
2015-01-07 00:26:02 +01:00
|
|
|
w++
|
|
|
|
}
|
2018-01-09 02:23:08 +01:00
|
|
|
ast2.Decls = ast2.Decls[0:w]
|
|
|
|
} else {
|
|
|
|
for _, decl := range ast2.Decls {
|
|
|
|
d, ok := decl.(*ast.GenDecl)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, spec := range d.Specs {
|
|
|
|
if s, ok := spec.(*ast.ImportSpec); ok && s.Path.Value == `"C"` {
|
|
|
|
// Replace "C" with _ "unsafe", to keep program valid.
|
|
|
|
// (Deleting import statement or clause is not safe if it is followed
|
|
|
|
// in the source by an explicit semicolon.)
|
|
|
|
f.Edit.Replace(f.offset(s.Path.Pos()), f.offset(s.Path.End()), `_ "unsafe"`)
|
|
|
|
}
|
2015-01-07 00:26:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate pointers to uses of C.x.
|
|
|
|
if f.Ref == nil {
|
|
|
|
f.Ref = make([]*Ref, 0, 8)
|
|
|
|
}
|
2019-01-18 20:04:36 +01:00
|
|
|
f.walk(ast2, ctxProg, (*File).validateIdents)
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(ast2, ctxProg, (*File).saveExprs)
|
2015-01-07 00:26:02 +01:00
|
|
|
|
|
|
|
// Accumulate exported functions.
|
|
|
|
// The comments are only on ast1 but we need to
|
|
|
|
// save the function bodies from ast2.
|
|
|
|
// The first walk fills in ExpFunc, and the
|
|
|
|
// second walk changes the entries to
|
|
|
|
// refer to ast2 instead.
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(ast1, ctxProg, (*File).saveExport)
|
|
|
|
f.walk(ast2, ctxProg, (*File).saveExport2)
|
2015-01-07 00:26:02 +01:00
|
|
|
|
|
|
|
f.Comments = ast1.Comments
|
|
|
|
f.AST = ast2
|
|
|
|
}
|
|
|
|
|
|
|
|
// Like ast.CommentGroup's Text method but preserves
|
|
|
|
// leading blank lines, so that line numbers line up.
|
|
|
|
func commentText(g *ast.CommentGroup) string {
|
|
|
|
var pieces []string
|
|
|
|
for _, com := range g.List {
|
2016-07-22 20:15:38 +02:00
|
|
|
c := com.Text
|
2015-01-07 00:26:02 +01:00
|
|
|
// Remove comment markers.
|
|
|
|
// The parser has given us exactly the comment text.
|
|
|
|
switch c[1] {
|
|
|
|
case '/':
|
|
|
|
//-style comment (no newline at the end)
|
|
|
|
c = c[2:] + "\n"
|
|
|
|
case '*':
|
|
|
|
/*-style comment */
|
|
|
|
c = c[2 : len(c)-2]
|
|
|
|
}
|
|
|
|
pieces = append(pieces, c)
|
|
|
|
}
|
|
|
|
return strings.Join(pieces, "")
|
|
|
|
}
|
|
|
|
|
2019-01-18 20:04:36 +01:00
|
|
|
func (f *File) validateIdents(x interface{}, context astContext) {
|
|
|
|
if x, ok := x.(*ast.Ident); ok {
|
|
|
|
if f.isMangledName(x.Name) {
|
|
|
|
error_(x.Pos(), "identifier %q may conflict with identifiers generated by cgo", x.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 22:58:02 +01:00
|
|
|
// Save various references we are going to need later.
|
2018-01-09 02:23:08 +01:00
|
|
|
func (f *File) saveExprs(x interface{}, context astContext) {
|
2016-02-03 22:58:02 +01:00
|
|
|
switch x := x.(type) {
|
|
|
|
case *ast.Expr:
|
|
|
|
switch (*x).(type) {
|
|
|
|
case *ast.SelectorExpr:
|
|
|
|
f.saveRef(x, context)
|
|
|
|
}
|
|
|
|
case *ast.CallExpr:
|
2016-07-22 20:15:38 +02:00
|
|
|
f.saveCall(x, context)
|
2019-01-18 20:04:36 +01:00
|
|
|
case *ast.GenDecl:
|
|
|
|
if x.Tok == token.CONST {
|
|
|
|
for _, spec := range x.Specs {
|
|
|
|
vs := spec.(*ast.ValueSpec)
|
|
|
|
if vs.Type == nil {
|
|
|
|
for _, name := range spec.(*ast.ValueSpec).Names {
|
|
|
|
consts[name.Name] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 22:58:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 00:26:02 +01:00
|
|
|
// Save references to C.xxx for later processing.
|
2018-01-09 02:23:08 +01:00
|
|
|
func (f *File) saveRef(n *ast.Expr, context astContext) {
|
2016-02-03 22:58:02 +01:00
|
|
|
sel := (*n).(*ast.SelectorExpr)
|
|
|
|
// For now, assume that the only instance of capital C is when
|
|
|
|
// used as the imported package identifier.
|
|
|
|
// The parser should take care of scoping in the future, 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.Name != "C" {
|
2015-01-07 00:26:02 +01:00
|
|
|
return
|
|
|
|
}
|
2018-01-09 02:23:08 +01:00
|
|
|
if context == ctxAssign2 {
|
|
|
|
context = ctxExpr
|
2016-02-03 22:58:02 +01:00
|
|
|
}
|
2018-01-09 02:23:08 +01:00
|
|
|
if context == ctxEmbedType {
|
2016-02-03 22:58:02 +01:00
|
|
|
error_(sel.Pos(), "cannot embed C type")
|
|
|
|
}
|
|
|
|
goname := sel.Sel.Name
|
|
|
|
if goname == "errno" {
|
|
|
|
error_(sel.Pos(), "cannot refer to errno directly; see documentation")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if goname == "_CMalloc" {
|
|
|
|
error_(sel.Pos(), "cannot refer to C._CMalloc; use C.malloc")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if goname == "malloc" {
|
|
|
|
goname = "_CMalloc"
|
|
|
|
}
|
|
|
|
name := f.Name[goname]
|
|
|
|
if name == nil {
|
|
|
|
name = &Name{
|
|
|
|
Go: goname,
|
2015-01-07 00:26:02 +01:00
|
|
|
}
|
2016-02-03 22:58:02 +01:00
|
|
|
f.Name[goname] = name
|
2018-01-09 02:23:08 +01:00
|
|
|
f.NamePos[name] = sel.Pos()
|
2016-02-03 22:58:02 +01:00
|
|
|
}
|
|
|
|
f.Ref = append(f.Ref, &Ref{
|
|
|
|
Name: name,
|
|
|
|
Expr: n,
|
|
|
|
Context: context,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save calls to C.xxx for later processing.
|
2018-01-09 02:23:08 +01:00
|
|
|
func (f *File) saveCall(call *ast.CallExpr, context astContext) {
|
2016-02-03 22:58:02 +01:00
|
|
|
sel, ok := call.Fun.(*ast.SelectorExpr)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if l, ok := sel.X.(*ast.Ident); !ok || l.Name != "C" {
|
|
|
|
return
|
2015-01-07 00:26:02 +01:00
|
|
|
}
|
2018-01-09 02:23:08 +01:00
|
|
|
c := &Call{Call: call, Deferred: context == ctxDefer}
|
2016-07-22 20:15:38 +02:00
|
|
|
f.Calls = append(f.Calls, c)
|
2015-01-07 00:26:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If a function should be exported add it to ExpFunc.
|
2018-01-09 02:23:08 +01:00
|
|
|
func (f *File) saveExport(x interface{}, context astContext) {
|
2015-01-07 00:26:02 +01:00
|
|
|
n, ok := x.(*ast.FuncDecl)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.Doc == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, c := range n.Doc.List {
|
2016-07-22 20:15:38 +02:00
|
|
|
if !strings.HasPrefix(c.Text, "//export ") {
|
2015-01-07 00:26:02 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
name := strings.TrimSpace(c.Text[9:])
|
2015-01-07 00:26:02 +01:00
|
|
|
if name == "" {
|
|
|
|
error_(c.Pos(), "export missing name")
|
|
|
|
}
|
|
|
|
|
|
|
|
if name != n.Name.Name {
|
|
|
|
error_(c.Pos(), "export comment has wrong name %q, want %q", name, n.Name.Name)
|
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
doc := ""
|
|
|
|
for _, c1 := range n.Doc.List {
|
|
|
|
if c1 != c {
|
|
|
|
doc += c1.Text + "\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 00:26:02 +01:00
|
|
|
f.ExpFunc = append(f.ExpFunc, &ExpFunc{
|
|
|
|
Func: n,
|
|
|
|
ExpName: name,
|
2015-10-31 01:59:47 +01:00
|
|
|
Doc: doc,
|
2015-01-07 00:26:02 +01:00
|
|
|
})
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make f.ExpFunc[i] point at the Func from this AST instead of the other one.
|
2018-01-09 02:23:08 +01:00
|
|
|
func (f *File) saveExport2(x interface{}, context astContext) {
|
2015-01-07 00:26:02 +01:00
|
|
|
n, ok := x.(*ast.FuncDecl)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, exp := range f.ExpFunc {
|
|
|
|
if exp.Func.Name.Name == n.Name.Name {
|
|
|
|
exp.Func = n
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
type astContext int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ctxProg astContext = iota
|
|
|
|
ctxEmbedType
|
|
|
|
ctxType
|
|
|
|
ctxStmt
|
|
|
|
ctxExpr
|
|
|
|
ctxField
|
|
|
|
ctxParam
|
|
|
|
ctxAssign2 // assignment of a single expression to two variables
|
|
|
|
ctxSwitch
|
|
|
|
ctxTypeSwitch
|
|
|
|
ctxFile
|
|
|
|
ctxDecl
|
|
|
|
ctxSpec
|
|
|
|
ctxDefer
|
|
|
|
ctxCall // any function call other than ctxCall2
|
|
|
|
ctxCall2 // function call whose result is assigned to two variables
|
|
|
|
ctxSelector
|
|
|
|
)
|
|
|
|
|
2015-01-07 00:26:02 +01:00
|
|
|
// walk walks the AST x, calling visit(f, x, context) for each node.
|
2018-01-09 02:23:08 +01:00
|
|
|
func (f *File) walk(x interface{}, context astContext, visit func(*File, interface{}, astContext)) {
|
2015-01-07 00:26:02 +01:00
|
|
|
visit(f, x, context)
|
|
|
|
switch n := x.(type) {
|
|
|
|
case *ast.Expr:
|
|
|
|
f.walk(*n, context, visit)
|
|
|
|
|
|
|
|
// everything else just recurs
|
|
|
|
default:
|
2017-01-14 01:05:42 +01:00
|
|
|
error_(token.NoPos, "unexpected type %T in walk", x)
|
2015-01-07 00:26:02 +01:00
|
|
|
panic("unexpected type")
|
|
|
|
|
|
|
|
case nil:
|
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
// These are ordered and grouped to match ../../go/ast/ast.go
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.Field:
|
2018-01-09 02:23:08 +01:00
|
|
|
if len(n.Names) == 0 && context == ctxField {
|
|
|
|
f.walk(&n.Type, ctxEmbedType, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
} else {
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.Type, ctxType, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
}
|
|
|
|
case *ast.FieldList:
|
|
|
|
for _, field := range n.List {
|
|
|
|
f.walk(field, context, visit)
|
|
|
|
}
|
|
|
|
case *ast.BadExpr:
|
|
|
|
case *ast.Ident:
|
|
|
|
case *ast.Ellipsis:
|
2018-09-24 23:46:21 +02:00
|
|
|
f.walk(&n.Elt, ctxType, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.BasicLit:
|
|
|
|
case *ast.FuncLit:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Type, ctxType, visit)
|
|
|
|
f.walk(n.Body, ctxStmt, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.CompositeLit:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.Type, ctxType, visit)
|
|
|
|
f.walk(n.Elts, ctxExpr, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.ParenExpr:
|
|
|
|
f.walk(&n.X, context, visit)
|
|
|
|
case *ast.SelectorExpr:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.X, ctxSelector, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.IndexExpr:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.X, ctxExpr, visit)
|
|
|
|
f.walk(&n.Index, ctxExpr, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.SliceExpr:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.X, ctxExpr, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
if n.Low != nil {
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.Low, ctxExpr, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
}
|
|
|
|
if n.High != nil {
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.High, ctxExpr, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
}
|
2015-01-15 01:27:56 +01:00
|
|
|
if n.Max != nil {
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.Max, ctxExpr, visit)
|
2015-01-15 01:27:56 +01:00
|
|
|
}
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.TypeAssertExpr:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.X, ctxExpr, visit)
|
|
|
|
f.walk(&n.Type, ctxType, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.CallExpr:
|
2018-01-09 02:23:08 +01:00
|
|
|
if context == ctxAssign2 {
|
|
|
|
f.walk(&n.Fun, ctxCall2, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
} else {
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.Fun, ctxCall, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
}
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Args, ctxExpr, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.StarExpr:
|
|
|
|
f.walk(&n.X, context, visit)
|
|
|
|
case *ast.UnaryExpr:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.X, ctxExpr, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.BinaryExpr:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.X, ctxExpr, visit)
|
|
|
|
f.walk(&n.Y, ctxExpr, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.KeyValueExpr:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.Key, ctxExpr, visit)
|
|
|
|
f.walk(&n.Value, ctxExpr, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
|
|
|
|
case *ast.ArrayType:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.Len, ctxExpr, visit)
|
|
|
|
f.walk(&n.Elt, ctxType, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.StructType:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Fields, ctxField, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.FuncType:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Params, ctxParam, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
if n.Results != nil {
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Results, ctxParam, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
}
|
|
|
|
case *ast.InterfaceType:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Methods, ctxField, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.MapType:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.Key, ctxType, visit)
|
|
|
|
f.walk(&n.Value, ctxType, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.ChanType:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.Value, ctxType, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
|
|
|
|
case *ast.BadStmt:
|
|
|
|
case *ast.DeclStmt:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Decl, ctxDecl, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.EmptyStmt:
|
|
|
|
case *ast.LabeledStmt:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Stmt, ctxStmt, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.ExprStmt:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.X, ctxExpr, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.SendStmt:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.Chan, ctxExpr, visit)
|
|
|
|
f.walk(&n.Value, ctxExpr, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.IncDecStmt:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.X, ctxExpr, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.AssignStmt:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Lhs, ctxExpr, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
if len(n.Lhs) == 2 && len(n.Rhs) == 1 {
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Rhs, ctxAssign2, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
} else {
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Rhs, ctxExpr, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
}
|
|
|
|
case *ast.GoStmt:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Call, ctxExpr, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.DeferStmt:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Call, ctxDefer, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.ReturnStmt:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Results, ctxExpr, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.BranchStmt:
|
|
|
|
case *ast.BlockStmt:
|
|
|
|
f.walk(n.List, context, visit)
|
|
|
|
case *ast.IfStmt:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Init, ctxStmt, visit)
|
|
|
|
f.walk(&n.Cond, ctxExpr, visit)
|
|
|
|
f.walk(n.Body, ctxStmt, visit)
|
|
|
|
f.walk(n.Else, ctxStmt, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.CaseClause:
|
2018-01-09 02:23:08 +01:00
|
|
|
if context == ctxTypeSwitch {
|
|
|
|
context = ctxType
|
2015-01-07 00:26:02 +01:00
|
|
|
} else {
|
2018-01-09 02:23:08 +01:00
|
|
|
context = ctxExpr
|
2015-01-07 00:26:02 +01:00
|
|
|
}
|
|
|
|
f.walk(n.List, context, visit)
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Body, ctxStmt, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.SwitchStmt:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Init, ctxStmt, visit)
|
|
|
|
f.walk(&n.Tag, ctxExpr, visit)
|
|
|
|
f.walk(n.Body, ctxSwitch, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.TypeSwitchStmt:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Init, ctxStmt, visit)
|
|
|
|
f.walk(n.Assign, ctxStmt, visit)
|
|
|
|
f.walk(n.Body, ctxTypeSwitch, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.CommClause:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Comm, ctxStmt, visit)
|
|
|
|
f.walk(n.Body, ctxStmt, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.SelectStmt:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Body, ctxStmt, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.ForStmt:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Init, ctxStmt, visit)
|
|
|
|
f.walk(&n.Cond, ctxExpr, visit)
|
|
|
|
f.walk(n.Post, ctxStmt, visit)
|
|
|
|
f.walk(n.Body, ctxStmt, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.RangeStmt:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.Key, ctxExpr, visit)
|
|
|
|
f.walk(&n.Value, ctxExpr, visit)
|
|
|
|
f.walk(&n.X, ctxExpr, visit)
|
|
|
|
f.walk(n.Body, ctxStmt, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
|
|
|
|
case *ast.ImportSpec:
|
|
|
|
case *ast.ValueSpec:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.Type, ctxType, visit)
|
2016-07-22 20:15:38 +02:00
|
|
|
if len(n.Names) == 2 && len(n.Values) == 1 {
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.Values[0], ctxAssign2, visit)
|
2016-07-22 20:15:38 +02:00
|
|
|
} else {
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Values, ctxExpr, visit)
|
2016-07-22 20:15:38 +02:00
|
|
|
}
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.TypeSpec:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(&n.Type, ctxType, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
|
|
|
|
case *ast.BadDecl:
|
|
|
|
case *ast.GenDecl:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Specs, ctxSpec, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
case *ast.FuncDecl:
|
|
|
|
if n.Recv != nil {
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Recv, ctxParam, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
}
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Type, ctxType, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
if n.Body != nil {
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Body, ctxStmt, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
case *ast.File:
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(n.Decls, ctxDecl, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
|
|
|
|
case *ast.Package:
|
|
|
|
for _, file := range n.Files {
|
2018-01-09 02:23:08 +01:00
|
|
|
f.walk(file, ctxFile, visit)
|
2015-01-07 00:26:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
case []ast.Decl:
|
|
|
|
for _, d := range n {
|
|
|
|
f.walk(d, context, visit)
|
|
|
|
}
|
|
|
|
case []ast.Expr:
|
|
|
|
for i := range n {
|
|
|
|
f.walk(&n[i], context, visit)
|
|
|
|
}
|
|
|
|
case []ast.Stmt:
|
|
|
|
for _, s := range n {
|
|
|
|
f.walk(s, context, visit)
|
|
|
|
}
|
|
|
|
case []ast.Spec:
|
|
|
|
for _, s := range n {
|
|
|
|
f.walk(s, context, visit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|