go/ast: import AST changes supporting typeparams from dev.go2go

Minimal changes are made to existing types in go/ast to support type
parameters. Namely:
 + FieldList is overloaded to hold type parameter lists. In this case,
   the field name becomes the type identifier, and the field type
   becomes the constraint.
 + FuncType and TypeSpec gain a TParams FieldList.
 + CallExpr gains a 'Brackets' flag, signaling that it uses '[]' rather
   than '()', representing a generic type expression with type
   parameters.

Modifications from dev.go2go: the 'UseBrackets' field was removed from
ast.File, as this support is no longer necessary.

Change-Id: I21fd7390f1800dece3c14e6ec015fb2419e9fc52
Reviewed-on: https://go-review.googlesource.com/c/go/+/264181
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Trust: Robert Griesemer <gri@golang.org>
Trust: Robert Findley <rfindley@google.com>
This commit is contained in:
Rob Findley 2020-10-21 14:36:21 -04:00 committed by Robert Findley
parent 7a8a720c80
commit a10fe9f6e7
3 changed files with 54 additions and 32 deletions

View file

@ -188,11 +188,14 @@ func isDirective(c string) bool {
// in a signature. // in a signature.
// Field.Names is nil for unnamed parameters (parameter lists which only contain types) // Field.Names is nil for unnamed parameters (parameter lists which only contain types)
// and embedded struct fields. In the latter case, the field name is the type name. // and embedded struct fields. In the latter case, the field name is the type name.
// Field.Names contains a single name "type" for elements of interface type lists.
// Types belonging to the same type list share the same "type" identifier which also
// records the position of that keyword.
// //
type Field struct { type Field struct {
Doc *CommentGroup // associated documentation; or nil Doc *CommentGroup // associated documentation; or nil
Names []*Ident // field/method/parameter names; or nil Names []*Ident // field/method/(type) parameter names, or type "type"; or nil
Type Expr // field/method/parameter type Type Expr // field/method/parameter type, type list type; or nil
Tag *BasicLit // field tag; or nil Tag *BasicLit // field tag; or nil
Comment *CommentGroup // line comments; or nil Comment *CommentGroup // line comments; or nil
} }
@ -201,15 +204,24 @@ func (f *Field) Pos() token.Pos {
if len(f.Names) > 0 { if len(f.Names) > 0 {
return f.Names[0].Pos() return f.Names[0].Pos()
} }
if f.Type != nil {
return f.Type.Pos() return f.Type.Pos()
} }
return token.NoPos
}
func (f *Field) End() token.Pos { func (f *Field) End() token.Pos {
if f.Tag != nil { if f.Tag != nil {
return f.Tag.End() return f.Tag.End()
} }
if f.Type != nil {
return f.Type.End() return f.Type.End()
} }
if len(f.Names) > 0 {
return f.Names[len(f.Names)-1].End()
}
return token.NoPos
}
// A FieldList represents a list of Fields, enclosed by parentheses or braces. // A FieldList represents a list of Fields, enclosed by parentheses or braces.
type FieldList struct { type FieldList struct {
@ -242,7 +254,7 @@ func (f *FieldList) End() token.Pos {
return token.NoPos return token.NoPos
} }
// NumFields returns the number of parameters or struct fields represented by a FieldList. // NumFields returns the number of (type) parameters or struct fields represented by a FieldList.
func (f *FieldList) NumFields() int { func (f *FieldList) NumFields() int {
n := 0 n := 0
if f != nil { if f != nil {
@ -285,12 +297,6 @@ type (
} }
// A BasicLit node represents a literal of basic type. // A BasicLit node represents a literal of basic type.
//
// Note that for the CHAR and STRING kinds, the literal is stored
// with its quotes. For example, for a double-quoted STRING, the
// first and the last rune in the Value field will be ". The
// Unquote and UnquoteChar functions in the strconv package can be
// used to unquote STRING and CHAR values, respectively.
BasicLit struct { BasicLit struct {
ValuePos token.Pos // literal position ValuePos token.Pos // literal position
Kind token.Token // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING Kind token.Token // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING
@ -361,6 +367,7 @@ type (
Args []Expr // function arguments; or nil Args []Expr // function arguments; or nil
Ellipsis token.Pos // position of "..." (token.NoPos if there is no "...") Ellipsis token.Pos // position of "..." (token.NoPos if there is no "...")
Rparen token.Pos // position of ")" Rparen token.Pos // position of ")"
Brackets bool // if set, "[" and "]" are used instead of "(" and ")"
} }
// A StarExpr node represents an expression of the form "*" Expression. // A StarExpr node represents an expression of the form "*" Expression.
@ -432,6 +439,7 @@ type (
// A FuncType node represents a function type. // A FuncType node represents a function type.
FuncType struct { FuncType struct {
Func token.Pos // position of "func" keyword (token.NoPos if there is no "func") Func token.Pos // position of "func" keyword (token.NoPos if there is no "func")
TParams *FieldList // type parameters; or nil
Params *FieldList // (incoming) parameters; non-nil Params *FieldList // (incoming) parameters; non-nil
Results *FieldList // (outgoing) results; or nil Results *FieldList // (outgoing) results; or nil
} }
@ -439,8 +447,8 @@ type (
// An InterfaceType node represents an interface type. // An InterfaceType node represents an interface type.
InterfaceType struct { InterfaceType struct {
Interface token.Pos // position of "interface" keyword Interface token.Pos // position of "interface" keyword
Methods *FieldList // list of methods Methods *FieldList // list of embedded interfaces, methods, or types
Incomplete bool // true if (source) methods are missing in the Methods list Incomplete bool // true if (source) methods or types are missing in the Methods list
} }
// A MapType node represents a map type. // A MapType node represents a map type.
@ -893,6 +901,7 @@ type (
TypeSpec struct { TypeSpec struct {
Doc *CommentGroup // associated documentation; or nil Doc *CommentGroup // associated documentation; or nil
Name *Ident // type name Name *Ident // type name
TParams *FieldList // type parameters; or nil
Assign token.Pos // position of '=', if any Assign token.Pos // position of '=', if any
Type Expr // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes Type Expr // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes
Comment *CommentGroup // line comments; or nil Comment *CommentGroup // line comments; or nil
@ -960,7 +969,7 @@ type (
GenDecl struct { GenDecl struct {
Doc *CommentGroup // associated documentation; or nil Doc *CommentGroup // associated documentation; or nil
TokPos token.Pos // position of Tok TokPos token.Pos // position of Tok
Tok token.Token // IMPORT, CONST, TYPE, VAR Tok token.Token // IMPORT, CONST, TYPE, or VAR
Lparen token.Pos // position of '(', if any Lparen token.Pos // position of '(', if any
Specs []Spec Specs []Spec
Rparen token.Pos // position of ')', if any Rparen token.Pos // position of ')', if any
@ -971,11 +980,15 @@ type (
Doc *CommentGroup // associated documentation; or nil Doc *CommentGroup // associated documentation; or nil
Recv *FieldList // receiver (methods); or nil (functions) Recv *FieldList // receiver (methods); or nil (functions)
Name *Ident // function/method name Name *Ident // function/method name
Type *FuncType // function signature: parameters, results, and position of "func" keyword Type *FuncType // function signature: type and value parameters, results, and position of "func" keyword
Body *BlockStmt // function body; or nil for external (non-Go) function Body *BlockStmt // function body; or nil for external (non-Go) function
} }
) )
func (f *FuncDecl) IsMethod() bool {
return f.Recv.NumFields() != 0
}
// Pos and End implementations for declaration nodes. // Pos and End implementations for declaration nodes.
func (d *BadDecl) Pos() token.Pos { return d.From } func (d *BadDecl) Pos() token.Pos { return d.From }

View file

@ -119,22 +119,23 @@ func main() {
// 40 . . . . . . . } // 40 . . . . . . . }
// 41 . . . . . . . Ellipsis: - // 41 . . . . . . . Ellipsis: -
// 42 . . . . . . . Rparen: 4:25 // 42 . . . . . . . Rparen: 4:25
// 43 . . . . . . } // 43 . . . . . . . Brackets: false
// 44 . . . . . } // 44 . . . . . . }
// 45 . . . . } // 45 . . . . . }
// 46 . . . . Rbrace: 5:1 // 46 . . . . }
// 47 . . . } // 47 . . . . Rbrace: 5:1
// 48 . . } // 48 . . . }
// 49 . } // 49 . . }
// 50 . Scope: *ast.Scope { // 50 . }
// 51 . . Objects: map[string]*ast.Object (len = 1) { // 51 . Scope: *ast.Scope {
// 52 . . . "main": *(obj @ 11) // 52 . . Objects: map[string]*ast.Object (len = 1) {
// 53 . . } // 53 . . . "main": *(obj @ 11)
// 54 . } // 54 . . }
// 55 . Unresolved: []*ast.Ident (len = 1) { // 55 . }
// 56 . . 0: *(obj @ 29) // 56 . Unresolved: []*ast.Ident (len = 1) {
// 57 . } // 57 . . 0: *(obj @ 29)
// 58 } // 58 . }
// 59 }
} }
// This example illustrates how to remove a variable declaration // This example illustrates how to remove a variable declaration

View file

@ -71,7 +71,9 @@ func Walk(v Visitor, node Node) {
Walk(v, n.Doc) Walk(v, n.Doc)
} }
walkIdentList(v, n.Names) walkIdentList(v, n.Names)
if n.Type != nil {
Walk(v, n.Type) Walk(v, n.Type)
}
if n.Tag != nil { if n.Tag != nil {
Walk(v, n.Tag) Walk(v, n.Tag)
} }
@ -161,6 +163,9 @@ func Walk(v Visitor, node Node) {
Walk(v, n.Fields) Walk(v, n.Fields)
case *FuncType: case *FuncType:
if n.TParams != nil {
Walk(v, n.TParams)
}
if n.Params != nil { if n.Params != nil {
Walk(v, n.Params) Walk(v, n.Params)
} }
@ -315,6 +320,9 @@ func Walk(v Visitor, node Node) {
Walk(v, n.Doc) Walk(v, n.Doc)
} }
Walk(v, n.Name) Walk(v, n.Name)
if n.TParams != nil {
Walk(v, n.TParams)
}
Walk(v, n.Type) Walk(v, n.Type)
if n.Comment != nil { if n.Comment != nil {
Walk(v, n.Comment) Walk(v, n.Comment)