Skip to content
This repository has been archived by the owner on Sep 20, 2022. It is now read-only.

Commit

Permalink
Rename some things for greater clarity.
Browse files Browse the repository at this point in the history
Specifically, in order to distinguish between declaration and
usage of generic types, we now use the terms "type parameters" and
"type arguments". Type parameters are declared names for arbitrary
types and appear in the declaration of functions and named types.
Type arguments are concrete types supplied as arguments when a
generic function or named type is used.
  • Loading branch information
albrow committed May 1, 2018
1 parent 6133a60 commit 3ec7411
Show file tree
Hide file tree
Showing 16 changed files with 151 additions and 151 deletions.
14 changes: 7 additions & 7 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,12 @@ type (
Rbrack token.Pos // position of "]"
}

// TypeParamExpr represents a type or identifier followed by a list of
// type parameters.
TypeParamExpr struct {
// TypeArgExpr represents a type or identifier followed by a list of
// type arguments.
TypeArgExpr struct {
X Expr // type name or identifier
Lbrack token.Pos // position of "["
Params []Expr // list of type names or identifiers
Types []Expr // list of type names or identifiers
Rbrack token.Pos // position of "]"
}

Expand Down Expand Up @@ -469,7 +469,7 @@ func (x *ParenExpr) Pos() token.Pos { return x.Lparen }
func (x *SelectorExpr) Pos() token.Pos { return x.X.Pos() }
func (x *IndexExpr) Pos() token.Pos { return x.X.Pos() }
func (x *SliceExpr) Pos() token.Pos { return x.X.Pos() }
func (x *TypeParamExpr) Pos() token.Pos { return x.Lbrack }
func (x *TypeArgExpr) Pos() token.Pos { return x.Lbrack }
func (x *TypeAssertExpr) Pos() token.Pos { return x.X.Pos() }
func (x *CallExpr) Pos() token.Pos { return x.Fun.Pos() }
func (x *StarExpr) Pos() token.Pos { return x.Star }
Expand Down Expand Up @@ -503,7 +503,7 @@ func (x *ParenExpr) End() token.Pos { return x.Rparen + 1 }
func (x *SelectorExpr) End() token.Pos { return x.Sel.End() }
func (x *IndexExpr) End() token.Pos { return x.Rbrack + 1 }
func (x *SliceExpr) End() token.Pos { return x.Rbrack + 1 }
func (x *TypeParamExpr) End() token.Pos { return x.Rbrack + 1 }
func (x *TypeArgExpr) End() token.Pos { return x.Rbrack + 1 }
func (x *TypeAssertExpr) End() token.Pos { return x.Rparen + 1 }
func (x *CallExpr) End() token.Pos { return x.Rparen + 1 }
func (x *StarExpr) End() token.Pos { return x.X.End() }
Expand Down Expand Up @@ -535,7 +535,7 @@ func (*ParenExpr) exprNode() {}
func (*SelectorExpr) exprNode() {}
func (*IndexExpr) exprNode() {}
func (*SliceExpr) exprNode() {}
func (*TypeParamExpr) exprNode() {}
func (*TypeArgExpr) exprNode() {}
func (*TypeAssertExpr) exprNode() {}
func (*CallExpr) exprNode() {}
func (*StarExpr) exprNode() {}
Expand Down
6 changes: 3 additions & 3 deletions astclone/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ func Clone(node ast.Node) ast.Node {
Rbrack: n.Rbrack,
}

case *ast.TypeParamExpr:
return &ast.TypeParamExpr{
case *ast.TypeArgExpr:
return &ast.TypeArgExpr{
X: cloneExpr(n.X),
Lbrack: n.Lbrack,
Params: cloneExprList(n.Params),
Types: cloneExprList(n.Types),
Rbrack: n.Rbrack,
}

Expand Down
6 changes: 3 additions & 3 deletions astcmp/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ func Equal(x, y ast.Node, mode Mode) bool {
return false
}

case *ast.TypeParamExpr:
y := y.(*ast.TypeParamExpr)
case *ast.TypeArgExpr:
y := y.(*ast.TypeArgExpr)
if mode&IgnorePos == 0 {
if x.Lbrack != y.Lbrack {
return false
Expand All @@ -176,7 +176,7 @@ func Equal(x, y ast.Node, mode Mode) bool {
if !Equal(x.X, y.X, mode) {
return false
}
if !compareExprs(x.Params, y.Params, mode) {
if !compareExprs(x.Types, y.Types, mode) {
return false
}

Expand Down
4 changes: 2 additions & 2 deletions astutil/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast.
case *ast.TypeParamDecl:
a.applyList(n, "Names")

case *ast.TypeParamExpr:
case *ast.TypeArgExpr:
a.apply(n, "X", nil, n.X)
a.applyList(n, "Params")
a.applyList(n, "Types")

case *ast.TypeAssertExpr:
a.apply(n, "X", nil, n.X)
Expand Down
62 changes: 31 additions & 31 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ func (p *parser) parseType() ast.Expr {

// If the result is an identifier, it is not resolved. If allowTypeParams is
// true, a bracket following the type name will be interpreted as the start of
// a TypeParamExpr.
// a TypeArgExpr.
func (p *parser) parseTypeName(allowTypeParams bool) ast.Expr {
if p.trace {
defer un(trace(p, "TypeName"))
Expand All @@ -661,10 +661,10 @@ func (p *parser) parseTypeName(allowTypeParams bool) ast.Expr {
lbrack := p.expect(token.LBRACK)
params := p.parseTypeList()
rbrack := p.expect(token.RBRACK)
return &ast.TypeParamExpr{
return &ast.TypeArgExpr{
X: x,
Lbrack: lbrack,
Params: params,
Types: params,
Rbrack: rbrack,
}
}
Expand Down Expand Up @@ -728,7 +728,7 @@ func (p *parser) parseFieldDecl(scope *ast.Scope) *ast.Field {
x := p.parseVarType(false, false)

if p.tok == token.LBRACK {
// We need to disambiguate between TypeParamExpr and
// We need to disambiguate between TypeArgExpr and
// (Ident SliceType | Ident ArrayType)
lbrack := p.expect(token.LBRACK)
if p.tok == token.IDENT {
Expand All @@ -739,27 +739,27 @@ func (p *parser) parseFieldDecl(scope *ast.Scope) *ast.Field {
len := p.parseRhs()
p.exprLev--
if p.tok == token.COMMA {
// TypeParamExpr
// TypeArgExpr
p.next()
params := append([]ast.Expr{len}, p.parseTypeList()...)
rbrack := p.expect(token.RBRACK)
x = &ast.TypeParamExpr{
x = &ast.TypeArgExpr{
X: x,
Lbrack: lbrack,
Params: params,
Types: params,
Rbrack: rbrack,
}
} else if p.tok == token.RBRACK {
// Still ambiguous. We need to look one more token ahead.
rbrack := p.expect(token.RBRACK)
if p.tok == token.SEMICOLON || p.tok == token.STRING || p.tok == token.RBRACE {
// TypeParamExpr
// TypeArgExpr
// We reached the end of this field decl which means the type
// is a TypeParamExpr and not an ArrayType.
x = &ast.TypeParamExpr{
// is a TypeArgExpr and not an ArrayType.
x = &ast.TypeArgExpr{
X: x,
Lbrack: lbrack,
Params: []ast.Expr{p.checkExprOrType(len)},
Types: []ast.Expr{p.checkExprOrType(len)},
Rbrack: rbrack,
}
} else {
Expand Down Expand Up @@ -916,7 +916,7 @@ func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params [
x := p.parseVarType(ellipsisOk, false)

if p.tok == token.LBRACK {
// We need to disambiguate between TypeParamExpr and
// We need to disambiguate between TypeArgExpr and
// IdentifierList (SliceType | ArrayType)
lbrack := p.expect(token.LBRACK)
if p.tok == token.IDENT {
Expand All @@ -927,33 +927,33 @@ func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params [
len := p.parseRhs()
p.exprLev--
if p.tok == token.COMMA {
// TypeParamExpr
// TypeArgExpr
p.next()
params := append([]ast.Expr{len}, p.parseTypeList()...)
rbrack := p.expect(token.RBRACK)
x = &ast.TypeParamExpr{
x = &ast.TypeArgExpr{
X: x,
Lbrack: lbrack,
Params: params,
Types: params,
Rbrack: rbrack,
}
} else if p.tok == token.RBRACK {
// Still ambiguous. We need to look one more token ahead.
rbrack := p.expect(token.RBRACK)
if p.tok == token.COMMA || p.tok == token.SEMICOLON || p.tok == token.RPAREN {
// TypeParamExpr
// TypeArgExpr
// Since nothing follows the closing bracket, we know we have a
// TypeParamExpr and not an ArrayType.
x = &ast.TypeParamExpr{
// TypeArgExpr and not an ArrayType.
x = &ast.TypeArgExpr{
X: x,
Lbrack: lbrack,
Params: []ast.Expr{p.checkExprOrType(len)},
Types: []ast.Expr{p.checkExprOrType(len)},
Rbrack: rbrack,
}
} else {
// Identifier ArrayType (with constant as the len)
// Since something follows the closing bracket, we know we have an
// ArrayType and not a TypeParamExpr.
// ArrayType and not a TypeArgExpr.
list = append(list, x)
elt := p.parseType()
typ := &ast.ArrayType{Lbrack: lbrack, Len: len, Elt: elt}
Expand Down Expand Up @@ -1427,14 +1427,14 @@ func (p *parser) parseBracketExpr(x ast.Expr) ast.Expr {
params := append([]ast.Expr{index[0]}, p.parseTypeList()...)
rbrack := p.expect(token.RBRACK)
p.exprLev--
return &ast.TypeParamExpr{
return &ast.TypeArgExpr{
X: x,
Lbrack: lbrack,
Params: params,
Types: params,
Rbrack: rbrack,
}
default:
p.errorExpected(p.pos, "generic type parameters or index or slice expression")
p.errorExpected(p.pos, "generic type arguments or index or slice expression")
p.exprLev--
return &ast.BadExpr{From: lbrack, To: p.pos}
}
Expand Down Expand Up @@ -1575,19 +1575,19 @@ func (p *parser) parseElementList() (list []ast.Expr) {
}

// parseGenericLiteralValue is like parseLiteralValue but is designed to work
// when typ is a generic type with concrete type parameters supplied. If typ is
// when typ is a generic type with concrete type arguments supplied. If typ is
// an ambiguous IndexExpr, it will disambiguate it by returning a new
// CompositeLit expression wherein the type is converted from an IndexExpr to a
// TypeParamExpr.
// TypeArgExpr.
func (p *parser) parseGenericLiteralValue(typ ast.Expr) ast.Expr {
if x, ok := typ.(*ast.IndexExpr); ok {
// IndexExpr is sometimes ambiguous. However, if we know that the parser
// expected a type as part of a literal value, we can disambiguate because
// arrays and slices cannot hold types.
typ = &ast.TypeParamExpr{
typ = &ast.TypeArgExpr{
X: x.X,
Lbrack: x.Lbrack,
Params: []ast.Expr{x.Index},
Types: []ast.Expr{x.Index},
Rbrack: x.Rbrack,
}
}
Expand Down Expand Up @@ -1623,8 +1623,8 @@ func (p *parser) checkExpr(x ast.Expr) ast.Expr {
case *ast.SelectorExpr:
case *ast.IndexExpr:
case *ast.SliceExpr:
case *ast.TypeParamExpr:
// TypeParamExpr may be a function value in which case it is considered an
case *ast.TypeArgExpr:
// TypeArgExpr may be a function value in which case it is considered an
// expression. It may also be a Type which is not an expression. We have
// to let the type checker handle this.
case *ast.TypeAssertExpr:
Expand All @@ -1650,7 +1650,7 @@ func isTypeName(x ast.Expr) bool {
switch t := x.(type) {
case *ast.BadExpr:
case *ast.Ident:
case *ast.TypeParamExpr:
case *ast.TypeArgExpr:
case *ast.SelectorExpr:
_, isIdent := t.X.(*ast.Ident)
return isIdent
Expand All @@ -1665,7 +1665,7 @@ func isLiteralType(x ast.Expr) bool {
switch t := x.(type) {
case *ast.BadExpr:
case *ast.Ident:
case *ast.TypeParamExpr:
case *ast.TypeArgExpr:
case *ast.SelectorExpr:
_, isIdent := t.X.(*ast.Ident)
return isIdent
Expand Down
Loading

0 comments on commit 3ec7411

Please sign in to comment.