本文整理汇总了Golang中go/ast.FuncDecl.Body方法的典型用法代码示例。如果您正苦于以下问题:Golang FuncDecl.Body方法的具体用法?Golang FuncDecl.Body怎么用?Golang FuncDecl.Body使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类go/ast.FuncDecl
的用法示例。
在下文中一共展示了FuncDecl.Body方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: readFunc
// readFunc processes a func or method declaration.
//
func (r *reader) readFunc(fun *ast.FuncDecl) {
// strip function body
fun.Body = nil
// determine if it should be associated with a type
if fun.Recv != nil {
// method
recvTypeName, imp := baseTypeName(fun.Recv.List[0].Type)
if imp {
// should not happen (incorrect AST);
// don't show this method
return
}
var typ *baseType
if r.isVisible(recvTypeName) {
// visible recv type: if not found, add it to r.types
typ = r.lookupType(recvTypeName)
} else {
// invisible recv type: if not found, do not add it
// (invisible embedded types are added before this
// phase, so if the type doesn't exist yet, we don't
// care about this method)
typ = r.types[recvTypeName]
}
if typ != nil {
// associate method with the type
// (if the type is not exported, it may be embedded
// somewhere so we need to collect the method anyway)
typ.methods.set(fun)
}
// otherwise don't show the method
// TODO(gri): There may be exported methods of non-exported types
// that can be called because of exported values (consts, vars, or
// function results) of that type. Could determine if that is the
// case and then show those methods in an appropriate section.
return
}
// perhaps a factory function
// determine result type, if any
if fun.Type.Results.NumFields() >= 1 {
res := fun.Type.Results.List[0]
if len(res.Names) <= 1 {
// exactly one (named or anonymous) result associated
// with the first type in result signature (there may
// be more than one result)
if n, imp := baseTypeName(res.Type); !imp && r.isVisible(n) {
if typ := r.lookupType(n); typ != nil {
// associate Func with typ
typ.funcs.set(fun)
return
}
}
}
}
// just an ordinary function
r.funcs.set(fun)
}
示例2: funcDeclToString
func funcDeclToString(decl *ast.FuncDecl) string {
var buffer bytes.Buffer
var body *ast.BlockStmt
body, decl.Body = decl.Body, nil
printer.Fprint(&buffer, token.NewFileSet(), decl)
decl.Body = body
return buffer.String()
}
示例3: topLevelNodeToDecl
// topLevelNodeToDecl converts an AST node to Go ast.Decl
func topLevelNodeToDecl(node *parser.CallNode) ast.Decl {
if node.Callee.(*parser.IdentNode).Ident != "defn" {
panic("Top level node has to be defn")
}
switch node.Callee.(*parser.IdentNode).Ident {
case "defn":
decl := ast.FuncDecl{
Name: &ast.Ident{
NamePos: 2,
Name: node.Args[0].(*parser.IdentNode).Ident,
},
}
fmt.Printf("SHOW ARGS: %+v\n", node.Args[1:])
// @TODO Fix this to support signature properly
params := &ast.FieldList{
Opening: 1,
Closing: 3,
}
params.List = make([]*ast.Field, 1)
params.List[0] = &ast.Field{
Names: []*ast.Ident{
&ast.Ident{
Name: "lol",
},
},
Type: &ast.Ident{
Name: "interface{}",
},
}
decl.Type = &ast.FuncType{
Func: 1,
Params: params,
Results: nil,
}
//decl.Body = nodeFnBody(node.Args[1:])
// TODO: Check argument lengths
decl.Body = nodeFnBody(node.Args[1])
return &decl
default:
// The rest is normal function call probably
//return nodeFnCall(node)
fmt.Println("got nil %+v", node)
return nil
}
}
示例4: readFunc
// readFunc processes a func or method declaration.
//
func (r *reader) readFunc(fun *ast.FuncDecl) {
// strip function body
fun.Body = nil
// associate methods with the receiver type, if any
if fun.Recv != nil {
// method
if len(fun.Recv.List) == 0 {
// should not happen (incorrect AST); (See issue 17788)
// don't show this method
return
}
recvTypeName, imp := baseTypeName(fun.Recv.List[0].Type)
if imp {
// should not happen (incorrect AST);
// don't show this method
return
}
if typ := r.lookupType(recvTypeName); typ != nil {
typ.methods.set(fun)
}
// otherwise ignore the method
// TODO(gri): There may be exported methods of non-exported types
// that can be called because of exported values (consts, vars, or
// function results) of that type. Could determine if that is the
// case and then show those methods in an appropriate section.
return
}
// associate factory functions with the first visible result type, if any
if fun.Type.Results.NumFields() >= 1 {
res := fun.Type.Results.List[0]
if len(res.Names) <= 1 {
// exactly one (named or anonymous) result associated
// with the first type in result signature (there may
// be more than one result)
if n, imp := baseTypeName(res.Type); !imp && r.isVisible(n) {
if typ := r.lookupType(n); typ != nil {
// associate function with typ
typ.funcs.set(fun)
return
}
}
}
}
// just an ordinary function
r.funcs.set(fun)
}
示例5: newFunc
func newFunc(decl *ast.FuncDecl) *Func {
if !ast.IsExported(decl.Name.Name) {
return nil
}
f := new(Func)
f.Doc = doc.CommentText(decl.Doc)
decl.Doc = nil
f.Name = decl.Name.Name
if decl.Recv != nil {
f.Recv = recvAsString(decl.Recv.List[0].Type)
}
f.Decl = decl
decl.Body = nil // remove body
return f
}
示例6: oneLineFunc
// oneLineFunc prints a function declaration as a single line.
func (pkg *Package) oneLineFunc(decl *ast.FuncDecl) {
decl.Doc = nil
decl.Body = nil
pkg.emit("", decl)
}
示例7: TrackImports
//.........这里部分代码省略.........
fmt.Println("FIXME: I don't handle const yet at all... (ignoring)")
} else if tdecl, ok := d.(*ast.GenDecl); ok && tdecl.Tok == token.TYPE {
for _, spec0 := range tdecl.Specs {
spec := spec0.(*ast.TypeSpec)
if spec.Name.Name == fn {
// fmt.Println("Got type declaration of", spec.Name)
spec := *spec
spec.Name = ast.NewIdent(fn)
spec.Type = sc.MangleExpr(spec.Type)
sc.MangleExpr(spec.Name)
d := &ast.GenDecl{
Tok: token.TYPE,
Specs: []ast.Spec{&spec},
}
main.Decls = append(main.Decls, d)
}
}
} else if vdecl, ok := d.(*ast.GenDecl); ok && vdecl.Tok == token.VAR {
for _, spec0 := range vdecl.Specs {
spec := spec0.(*ast.ValueSpec)
for i, n := range spec.Names {
if n.Name == fn {
// fmt.Println("I got variable", fn)
nnew := *n
sc.MangleExpr(&nnew)
vs := []ast.Expr(nil)
if len(spec.Values) > i {
vs = append(vs, spec.Values[i])
sc.MangleExpr(spec.Values[i])
}
sc.MangleExpr(spec.Type)
d := ast.GenDecl{
Tok: token.VAR,
Specs: []ast.Spec{
&ast.ValueSpec{
Names: []*ast.Ident{&nnew},
Type: spec.Type,
Values: vs,
},
},
}
main.Decls = append(main.Decls, &d)
}
}
}
} else if fdecl, ok := d.(*ast.FuncDecl); ok {
if fdecl.Name.Name == fn {
// first, let's update the name... but in a copy of the
// function declaration
fdecl := *fdecl
fdecl.Name = ast.NewIdent(pkg + "_" + fn)
if fdecl.Type.Params != nil {
for _, f := range fdecl.Type.Params.List {
sc.MangleExpr(f.Type)
}
}
if fdecl.Type.Results != nil {
for _, f := range fdecl.Type.Results.List {
sc.MangleExpr(f.Type)
}
}
sc.MangleStatement(fdecl.Body)
// fmt.Println("Dumping out", pkg, fn)
main.Decls = append(main.Decls, &fdecl)
if fn == "init" && fdecl.Recv == nil {
initstmts = append(initstmts,
&ast.ExprStmt{&ast.CallExpr{Fun: fdecl.Name}})
}
}
}
}
// See what else we need to compile...
for _, x := range sc.ToDo {
if _, done := done[x]; !done {
todo[x] = struct{}{}
}
}
}
delete(todo, pkgfn)
done[pkgfn] = struct{}{}
}
}
// Now we reverse the order, so that the declarations will be in
// C-style order, not requiring forward declarations.
newdecls := make([]ast.Decl, len(main.Decls))
for i := range main.Decls {
newdecls[i] = main.Decls[len(main.Decls)-i-1]
}
main.Decls = newdecls
mainfn := new(ast.FuncDecl)
mainfn.Name = ast.NewIdent("main")
mainfn.Type = &ast.FuncType{Params: &ast.FieldList{}}
initstmts = append(initstmts,
&ast.ExprStmt{&ast.CallExpr{Fun: ast.NewIdent("main_main")}})
mainfn.Body = &ast.BlockStmt{List: initstmts}
main.Decls = append(main.Decls, mainfn)
return main
}