// These are ordered and grouped to match ../../pkg/go/ast/ast.go
case *ast.Field:
walk(&n.Type, p, "type")
+ case *ast.FieldList:
+ for _, f := range n.List {
+ walk(f, p, context)
+ }
case *ast.BadExpr:
case *ast.Ident:
case *ast.Ellipsis:
walk(n.Fields, p, "field")
case *ast.FuncType:
walk(n.Params, p, "field")
- walk(n.Results, p, "field")
+ if n.Results != nil {
+ walk(n.Results, p, "field")
+ }
case *ast.InterfaceType:
walk(n.Methods, p, "field")
case *ast.MapType:
for i := range n {
walk(&n[i], p, context)
}
- case []*ast.Field:
- for _, f := range n {
- walk(f, p, context)
- }
case []ast.Stmt:
for _, s := range n {
walk(s, p, context)
Params: p,
Result: r,
Go: &ast.FuncType{
- Params: gp,
- Results: gr,
+ Params: &ast.FieldList{List: gp},
+ Results: &ast.FieldList{List: gr},
},
}
}
fatal("struct size calculation error")
}
csyntax += "}"
- expr = &ast.StructType{Fields: fld}
+ expr = &ast.StructType{Fields: &ast.FieldList{List: fld}}
return
}
return NewArrayType(l, elem)
}
-func countFields(fs []*ast.Field) int {
- n := 0
- for _, f := range fs {
- if f.Names == nil {
- n++
- } else {
- n += len(f.Names)
- }
- }
- return n
-}
-
-func (a *typeCompiler) compileFields(fs []*ast.Field, allowRec bool) ([]Type, []*ast.Ident, []token.Position, bool) {
- n := countFields(fs)
+func (a *typeCompiler) compileFields(fields *ast.FieldList, allowRec bool) ([]Type, []*ast.Ident, []token.Position, bool) {
+ n := fields.NumFields()
ts := make([]Type, n)
ns := make([]*ast.Ident, n)
ps := make([]token.Position, n)
-
bad := false
- i := 0
- for _, f := range fs {
- t := a.compileType(f.Type, allowRec)
- if t == nil {
- bad = true
- }
- if f.Names == nil {
- ns[i] = nil
- ts[i] = t
- ps[i] = f.Type.Pos()
- i++
- continue
- }
- for _, n := range f.Names {
- ns[i] = n
- ts[i] = t
- ps[i] = n.Pos()
- i++
+
+ if fields != nil {
+ i := 0
+ for _, f := range fields.List {
+ t := a.compileType(f.Type, allowRec)
+ if t == nil {
+ bad = true
+ }
+ if f.Names == nil {
+ ns[i] = nil
+ ts[i] = t
+ ps[i] = f.Type.Pos()
+ i++
+ continue
+ }
+ for _, n := range f.Names {
+ ns[i] = n
+ ts[i] = t
+ ps[i] = n.Pos()
+ i++
+ }
}
}
}
+// A FieldList represents a list of Fields, enclosed by parentheses or braces.
+type FieldList struct {
+ Opening token.Position // position of opening parenthesis/brace
+ List []*Field // field list
+ Closing token.Position // position of closing parenthesis/brace
+}
+
+
+// NumFields returns the number of (named and anonymous fields) in a FieldList.
+func (f *FieldList) NumFields() int {
+ n := 0
+ if f != nil {
+ for _, g := range f.List {
+ m := len(g.Names)
+ if m == 0 {
+ m = 1 // anonymous field
+ }
+ n += m
+ }
+ }
+ return n
+}
+
+
// An expression is represented by a tree consisting of one
// or more of the following concrete expression nodes.
//
// A StructType node represents a struct type.
StructType struct {
- token.Position // position of "struct" keyword
- Lbrace token.Position // position of "{"
- Fields []*Field // list of field declarations
- Rbrace token.Position // position of "}"
- Incomplete bool // true if (source) fields are missing in the Fields list
+ token.Position // position of "struct" keyword
+ Fields *FieldList // list of field declarations
+ Incomplete bool // true if (source) fields are missing in the Fields list
}
// Pointer types are represented via StarExpr nodes.
// A FuncType node represents a function type.
FuncType struct {
- token.Position // position of "func" keyword
- Params []*Field // (incoming) parameters
- Results []*Field // (outgoing) results
+ token.Position // position of "func" keyword
+ Params *FieldList // (incoming) parameters
+ Results *FieldList // (outgoing) results
}
// An InterfaceType node represents an interface type.
InterfaceType struct {
- token.Position // position of "interface" keyword
- Lbrace token.Position // position of "{"
- Methods []*Field // list of methods
- Rbrace token.Position // position of "}"
- Incomplete bool // true if (source) methods are missing in the Methods list
+ token.Position // position of "interface" keyword
+ Methods *FieldList // list of methods
+ Incomplete bool // true if (source) methods are missing in the Methods list
}
// A MapType node represents a map type.
// A FuncDecl node represents a function declaration.
FuncDecl struct {
Doc *CommentGroup // associated documentation; or nil
- Recv *Field // receiver (methods); or nil (functions)
+ Recv *FieldList // receiver (methods); or nil (functions)
Name *Ident // function/method name
Type *FuncType // position of Func keyword, parameters and results
Body *BlockStmt // function body; or nil (forward declaration)
}
-func filterFieldList(list []*Field, incomplete *bool) []*Field {
+func filterFieldList(fields *FieldList, incomplete *bool) {
+ if fields == nil {
+ return
+ }
+ list := fields.List
j := 0
for _, f := range list {
exported := false
if j < len(list) {
*incomplete = true
}
- return list[0:j]
+ fields.List = list[0:j]
}
-func filterParamList(list []*Field) {
- for _, f := range list {
+func filterParamList(fields *FieldList) {
+ if fields == nil {
+ return
+ }
+ for _, f := range fields.List {
filterType(f.Type)
}
}
case *ArrayType:
filterType(t.Elt)
case *StructType:
- t.Fields = filterFieldList(t.Fields, &t.Incomplete)
+ filterFieldList(t.Fields, &t.Incomplete)
case *FuncType:
filterParamList(t.Params)
filterParamList(t.Results)
case *InterfaceType:
- t.Methods = filterFieldList(t.Methods, &t.Incomplete)
+ filterFieldList(t.Methods, &t.Incomplete)
case *MapType:
filterType(t.Key)
filterType(t.Value)
Walk(v, n.Tag)
walkCommentGroup(v, n.Comment)
+ case *FieldList:
+ for _, f := range n.List {
+ Walk(v, f)
+ }
+
// Expressions
case *BadExpr, *Ident, *Ellipsis, *BasicLit:
// nothing to do
case *FuncType:
Walk(v, n.Params)
- Walk(v, n.Results)
+ if n.Results != nil {
+ Walk(v, n.Results)
+ }
case *InterfaceType:
Walk(v, n.Methods)
Walk(v, f)
}
- case []*Field:
- for _, x := range n {
- Walk(v, x)
- }
-
case []*Ident:
for _, x := range n {
Walk(v, x)
// determine if it should be associated with a type
if fun.Recv != nil {
// method
- typ := doc.lookupTypeDoc(baseTypeName(fun.Recv.Type))
+ typ := doc.lookupTypeDoc(baseTypeName(fun.Recv.List[0].Type))
if typ != nil {
// exported receiver type
typ.methods[name] = fun
// perhaps a factory function
// determine result type, if any
- if len(fun.Type.Results) >= 1 {
- res := fun.Type.Results[0]
+ 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
doc.Doc = CommentText(f.Doc)
f.Doc = nil // doc consumed - remove from ast.FuncDecl node
if f.Recv != nil {
- doc.Recv = f.Recv.Type
+ doc.Recv = f.Recv.List[0].Type
}
doc.Name = f.Name.Name()
doc.Decl = f
// TODO(gri) The struct scope shouldn't get lost.
p.declFieldList(ast.NewScope(nil), fields)
- return &ast.StructType{pos, lbrace, fields, rbrace, false}
+ return &ast.StructType{pos, &ast.FieldList{lbrace, fields, rbrace}, false}
}
}
-func (p *parser) parseParameters(scope *ast.Scope, ellipsisOk bool) []*ast.Field {
+func (p *parser) parseParameters(scope *ast.Scope, ellipsisOk bool) *ast.FieldList {
if p.trace {
defer un(trace(p, "Parameters"))
}
var params []*ast.Field
- p.expect(token.LPAREN)
+ lparen := p.expect(token.LPAREN)
if p.tok != token.RPAREN {
params = p.parseParameterList(ellipsisOk)
p.declFieldList(scope, params)
}
- p.expect(token.RPAREN)
+ rparen := p.expect(token.RPAREN)
- return params
+ return &ast.FieldList{lparen, params, rparen}
}
-func (p *parser) parseResult(scope *ast.Scope) []*ast.Field {
+func (p *parser) parseResult(scope *ast.Scope) *ast.FieldList {
if p.trace {
defer un(trace(p, "Result"))
}
- var results []*ast.Field
if p.tok == token.LPAREN {
- results = p.parseParameters(scope, false)
- } else {
- typ := p.tryType()
- if typ != nil {
- results = make([]*ast.Field, 1)
- results[0] = &ast.Field{Type: typ}
- }
+ return p.parseParameters(scope, false)
+ }
+
+ typ := p.tryType()
+ if typ != nil {
+ list := make([]*ast.Field, 1)
+ list[0] = &ast.Field{Type: typ}
+ return &ast.FieldList{List: list}
}
- return results
+ return nil
}
-func (p *parser) parseSignature(scope *ast.Scope) (params []*ast.Field, results []*ast.Field) {
+func (p *parser) parseSignature(scope *ast.Scope) (params, results *ast.FieldList) {
if p.trace {
defer un(trace(p, "Signature"))
}
// TODO(gri) The interface scope shouldn't get lost.
p.declFieldList(ast.NewScope(nil), methods)
- return &ast.InterfaceType{pos, lbrace, methods, rbrace, false}
+ return &ast.InterfaceType{pos, &ast.FieldList{lbrace, methods, rbrace}, false}
}
}
-func (p *parser) parseReceiver(scope *ast.Scope) *ast.Field {
+func (p *parser) parseReceiver(scope *ast.Scope) *ast.FieldList {
if p.trace {
defer un(trace(p, "Receiver"))
}
par := p.parseParameters(scope, false)
// must have exactly one receiver
- if len(par) != 1 || len(par) == 1 && len(par[0].Names) > 1 {
+ if par.NumFields() != 1 {
p.errorExpected(pos, "exactly one receiver")
- return &ast.Field{Type: &ast.BadExpr{noPos}}
+ par.List = []*ast.Field{&ast.Field{Type: &ast.BadExpr{noPos}}}
}
- recv := par[0]
+ recv := par.List[0]
// recv type must be TypeName or *TypeName
base := recv.Type
p.errorExpected(base.Pos(), "type name")
}
- return recv
+ return par
}
pos := p.expect(token.FUNC)
scope := ast.NewScope(p.funcScope)
- var recv *ast.Field
+ var recv *ast.FieldList
if p.tok == token.LPAREN {
recv = p.parseReceiver(scope)
}
// Sets multiLine to true if the the parameter list spans multiple lines.
-func (p *printer) parameters(list []*ast.Field, multiLine *bool) {
- p.print(token.LPAREN)
- if len(list) > 0 {
- for i, par := range list {
+func (p *printer) parameters(fields *ast.FieldList, multiLine *bool) {
+ p.print(fields.Opening, token.LPAREN)
+ if len(fields.List) > 0 {
+ for i, par := range fields.List {
if i > 0 {
p.print(token.COMMA, blank)
}
p.expr(par.Type, multiLine)
}
}
- p.print(token.RPAREN)
+ p.print(fields.Closing, token.RPAREN)
}
// Sets multiLine to true if the signature spans multiple lines.
-func (p *printer) signature(params, result []*ast.Field, multiLine *bool) {
+func (p *printer) signature(params, result *ast.FieldList, multiLine *bool) {
p.parameters(params, multiLine)
- if result != nil {
+ n := result.NumFields()
+ if n > 0 {
p.print(blank)
- if len(result) == 1 && result[0].Names == nil {
+ if n == 1 && result.List[0].Names == nil {
// single anonymous result; no ()'s
- p.expr(result[0].Type, multiLine)
+ p.expr(result.List[0].Type, multiLine)
return
}
p.parameters(result, multiLine)
}
-func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace token.Position, isIncomplete bool, ctxt exprContext) {
+func (p *printer) fieldList(fields *ast.FieldList, isIncomplete bool, ctxt exprContext) {
+ lbrace := fields.Opening
+ list := fields.List
+ rbrace := fields.Closing
+
if !isIncomplete && !p.commentBefore(rbrace) {
// possibly a one-line struct/interface
if len(list) == 0 {
case *ast.StructType:
p.print(token.STRUCT)
- p.fieldList(x.Lbrace, x.Fields, x.Rbrace, x.Incomplete, ctxt|structType)
+ p.fieldList(x.Fields, x.Incomplete, ctxt|structType)
case *ast.FuncType:
p.print(token.FUNC)
case *ast.InterfaceType:
p.print(token.INTERFACE)
- p.fieldList(x.Lbrace, x.Methods, x.Rbrace, x.Incomplete, ctxt)
+ p.fieldList(x.Methods, x.Incomplete, ctxt)
case *ast.MapType:
p.print(token.MAP, token.LBRACK)
func (p *printer) funcDecl(d *ast.FuncDecl, multiLine *bool) {
p.setComment(d.Doc)
p.print(d.Pos(), token.FUNC, blank)
- if recv := d.Recv; recv != nil {
- // method: print receiver
- p.print(token.LPAREN)
- if len(recv.Names) > 0 {
- p.expr(recv.Names[0], multiLine)
- p.print(blank)
- }
- p.expr(recv.Type, multiLine)
- p.print(token.RPAREN, blank)
+ if d.Recv != nil {
+ p.parameters(d.Recv, multiLine) // method: print receiver
+ p.print(blank)
}
p.expr(d.Name, multiLine)
p.signature(d.Type.Params, d.Type.Results, multiLine)
// Some interesting interspersed comments
func _( /* this */ x /* is */ /* an */ int) {}
+func _( /* no params */ ) {}
+
+func _() {
+ f( /* no args */ )
+}
+
+func ( /* comment1 */ T /* comment2 */ ) _() {}
+
// Line comments with tabs
func _() {
func _(/* this */x/* is *//* an */ int) {
}
+func _(/* no params */) {}
+
+func _() {
+ f(/* no args */)
+}
+
+func (/* comment1 */ T /* comment2 */) _() {}
+
// Line comments with tabs
func _() {