aboutsummaryrefslogtreecommitdiff
path: root/src/go/internal/typeparams/typeparams.go
blob: 871e95d9984d7ad8bc54b01464233609323d65f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build typeparams
// +build typeparams

package typeparams

import (
	"fmt"
	"go/ast"
)

const Enabled = true

func PackExpr(list []ast.Expr) ast.Expr {
	switch len(list) {
	case 0:
		// Return an empty ListExpr here, rather than nil, as IndexExpr.Index must
		// never be nil.
		// TODO(rFindley) would a BadExpr be more appropriate here?
		return &ast.ListExpr{}
	case 1:
		return list[0]
	default:
		return &ast.ListExpr{ElemList: list}
	}
}

// TODO(gri) Should find a more efficient solution that doesn't
//           require introduction of a new slice for simple
//           expressions.
func UnpackExpr(x ast.Expr) []ast.Expr {
	if x, _ := x.(*ast.ListExpr); x != nil {
		return x.ElemList
	}
	if x != nil {
		return []ast.Expr{x}
	}
	return nil
}

func IsListExpr(n ast.Node) bool {
	_, ok := n.(*ast.ListExpr)
	return ok
}

func Get(n ast.Node) *ast.FieldList {
	switch n := n.(type) {
	case *ast.TypeSpec:
		return n.TParams
	case *ast.FuncType:
		return n.TParams
	default:
		panic(fmt.Sprintf("node type %T has no type parameters", n))
	}
}

func Set(n ast.Node, params *ast.FieldList) {
	switch n := n.(type) {
	case *ast.TypeSpec:
		n.TParams = params
	case *ast.FuncType:
		n.TParams = params
	default:
		panic(fmt.Sprintf("node type %T has no type parameters", n))
	}
}