Skip to content

Commit ad59efb

Browse files
committed
[dev.typeparams] go/ast: remove the typeparams build constraint
This CL removes the typeparams build constraint guarding changes to the go/ast and go/types APIs. Notably it does not remove all indirection added to hide the type parameter API: the go/internal/typeparams package is not yet deleted, nor have go/parser or go/types been updated to access type parameter data directly. This will be done in a follow-up CL; the intent of this CL is to make it easier to support the new type set syntax, and to experiment with different AST APIs. Change-Id: I13ea0285752991b87b3aead1d1371e1f3f817b1a Reviewed-on: https://go-review.googlesource.com/c/go/+/325689 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 1ba2074 commit ad59efb

File tree

11 files changed

+64
-300
lines changed

11 files changed

+64
-300
lines changed

src/go/ast/ast.go

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,13 @@ type (
374374
Rparen token.Pos // position of ")"
375375
}
376376

377+
// A ListExpr node represents a list of expressions separated by commas.
378+
// ListExpr nodes are used as index in IndexExpr nodes representing type
379+
// or function instantiations with more than one type argument.
380+
ListExpr struct {
381+
ElemList []Expr
382+
}
383+
377384
// A StarExpr node represents an expression of the form "*" Expression.
378385
// Semantically it could be a unary "*" expression, or a pointer type.
379386
//
@@ -440,6 +447,14 @@ type (
440447

441448
// Pointer types are represented via StarExpr nodes.
442449

450+
// A FuncType node represents a function type.
451+
FuncType struct {
452+
Func token.Pos // position of "func" keyword (token.NoPos if there is no "func")
453+
TParams *FieldList // type parameters; or nil
454+
Params *FieldList // (incoming) parameters; non-nil
455+
Results *FieldList // (outgoing) results; or nil
456+
}
457+
443458
// An InterfaceType node represents an interface type.
444459
InterfaceType struct {
445460
Interface token.Pos // position of "interface" keyword
@@ -482,12 +497,18 @@ func (x *IndexExpr) Pos() token.Pos { return x.X.Pos() }
482497
func (x *SliceExpr) Pos() token.Pos { return x.X.Pos() }
483498
func (x *TypeAssertExpr) Pos() token.Pos { return x.X.Pos() }
484499
func (x *CallExpr) Pos() token.Pos { return x.Fun.Pos() }
485-
func (x *StarExpr) Pos() token.Pos { return x.Star }
486-
func (x *UnaryExpr) Pos() token.Pos { return x.OpPos }
487-
func (x *BinaryExpr) Pos() token.Pos { return x.X.Pos() }
488-
func (x *KeyValueExpr) Pos() token.Pos { return x.Key.Pos() }
489-
func (x *ArrayType) Pos() token.Pos { return x.Lbrack }
490-
func (x *StructType) Pos() token.Pos { return x.Struct }
500+
func (x *ListExpr) Pos() token.Pos {
501+
if len(x.ElemList) > 0 {
502+
return x.ElemList[0].Pos()
503+
}
504+
return token.NoPos
505+
}
506+
func (x *StarExpr) Pos() token.Pos { return x.Star }
507+
func (x *UnaryExpr) Pos() token.Pos { return x.OpPos }
508+
func (x *BinaryExpr) Pos() token.Pos { return x.X.Pos() }
509+
func (x *KeyValueExpr) Pos() token.Pos { return x.Key.Pos() }
510+
func (x *ArrayType) Pos() token.Pos { return x.Lbrack }
511+
func (x *StructType) Pos() token.Pos { return x.Struct }
491512
func (x *FuncType) Pos() token.Pos {
492513
if x.Func.IsValid() || x.Params == nil { // see issue 3870
493514
return x.Func
@@ -515,12 +536,18 @@ func (x *IndexExpr) End() token.Pos { return x.Rbrack + 1 }
515536
func (x *SliceExpr) End() token.Pos { return x.Rbrack + 1 }
516537
func (x *TypeAssertExpr) End() token.Pos { return x.Rparen + 1 }
517538
func (x *CallExpr) End() token.Pos { return x.Rparen + 1 }
518-
func (x *StarExpr) End() token.Pos { return x.X.End() }
519-
func (x *UnaryExpr) End() token.Pos { return x.X.End() }
520-
func (x *BinaryExpr) End() token.Pos { return x.Y.End() }
521-
func (x *KeyValueExpr) End() token.Pos { return x.Value.End() }
522-
func (x *ArrayType) End() token.Pos { return x.Elt.End() }
523-
func (x *StructType) End() token.Pos { return x.Fields.End() }
539+
func (x *ListExpr) End() token.Pos {
540+
if len(x.ElemList) > 0 {
541+
return x.ElemList[len(x.ElemList)-1].End()
542+
}
543+
return token.NoPos
544+
}
545+
func (x *StarExpr) End() token.Pos { return x.X.End() }
546+
func (x *UnaryExpr) End() token.Pos { return x.X.End() }
547+
func (x *BinaryExpr) End() token.Pos { return x.Y.End() }
548+
func (x *KeyValueExpr) End() token.Pos { return x.Value.End() }
549+
func (x *ArrayType) End() token.Pos { return x.Elt.End() }
550+
func (x *StructType) End() token.Pos { return x.Fields.End() }
524551
func (x *FuncType) End() token.Pos {
525552
if x.Results != nil {
526553
return x.Results.End()
@@ -546,6 +573,7 @@ func (*IndexExpr) exprNode() {}
546573
func (*SliceExpr) exprNode() {}
547574
func (*TypeAssertExpr) exprNode() {}
548575
func (*CallExpr) exprNode() {}
576+
func (*ListExpr) exprNode() {}
549577
func (*StarExpr) exprNode() {}
550578
func (*UnaryExpr) exprNode() {}
551579
func (*BinaryExpr) exprNode() {}
@@ -892,6 +920,16 @@ type (
892920
Values []Expr // initial values; or nil
893921
Comment *CommentGroup // line comments; or nil
894922
}
923+
924+
// A TypeSpec node represents a type declaration (TypeSpec production).
925+
TypeSpec struct {
926+
Doc *CommentGroup // associated documentation; or nil
927+
Name *Ident // type name
928+
TParams *FieldList // type parameters; or nil
929+
Assign token.Pos // position of '=', if any
930+
Type Expr // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes
931+
Comment *CommentGroup // line comments; or nil
932+
}
895933
)
896934

897935
// Pos and End implementations for spec nodes.

src/go/ast/ast_notypeparams.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/go/ast/ast_typeparams.go

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/go/ast/walk.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package ast
66

7+
import "fmt"
8+
79
// A Visitor's Visit method is invoked for each node encountered by Walk.
810
// If the result visitor w is not nil, Walk visits each of the children
911
// of node with the visitor w, followed by a call of w.Visit(nil).
@@ -136,6 +138,11 @@ func Walk(v Visitor, node Node) {
136138
Walk(v, n.Fun)
137139
walkExprList(v, n.Args)
138140

141+
case *ListExpr:
142+
for _, elem := range n.ElemList {
143+
Walk(v, elem)
144+
}
145+
139146
case *StarExpr:
140147
Walk(v, n.X)
141148

@@ -161,7 +168,9 @@ func Walk(v Visitor, node Node) {
161168
Walk(v, n.Fields)
162169

163170
case *FuncType:
164-
walkFuncTypeParams(v, n)
171+
if n.TParams != nil {
172+
Walk(v, n.TParams)
173+
}
165174
if n.Params != nil {
166175
Walk(v, n.Params)
167176
}
@@ -316,7 +325,9 @@ func Walk(v Visitor, node Node) {
316325
Walk(v, n.Doc)
317326
}
318327
Walk(v, n.Name)
319-
walkTypeSpecParams(v, n)
328+
if n.TParams != nil {
329+
Walk(v, n.TParams)
330+
}
320331
Walk(v, n.Type)
321332
if n.Comment != nil {
322333
Walk(v, n.Comment)
@@ -363,7 +374,7 @@ func Walk(v Visitor, node Node) {
363374
}
364375

365376
default:
366-
walkOtherNodes(v, n)
377+
panic(fmt.Sprintf("ast.Walk: unexpected node type %T", n))
367378
}
368379

369380
v.Visit(nil)

src/go/ast/walk_notypeparams.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/go/ast/walk_typeparams.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/go/internal/typeparams/notypeparams.go

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/go/internal/typeparams/typeparams.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build typeparams
6-
// +build typeparams
7-
85
package typeparams
96

107
import (

0 commit comments

Comments
 (0)