1package astutil
2
3import (
4	"go/ast"
5	"go/types"
6)
7
8func RemoveParens(e ast.Expr) ast.Expr {
9	for {
10		p, isParen := e.(*ast.ParenExpr)
11		if !isParen {
12			return e
13		}
14		e = p.X
15	}
16}
17
18func SetType(info *types.Info, t types.Type, e ast.Expr) ast.Expr {
19	info.Types[e] = types.TypeAndValue{Type: t}
20	return e
21}
22
23func NewIdent(name string, t types.Type, info *types.Info, pkg *types.Package) *ast.Ident {
24	ident := ast.NewIdent(name)
25	info.Types[ident] = types.TypeAndValue{Type: t}
26	obj := types.NewVar(0, pkg, name, t)
27	info.Uses[ident] = obj
28	return ident
29}
30
31func IsTypeExpr(expr ast.Expr, info *types.Info) bool {
32	switch e := expr.(type) {
33	case *ast.ArrayType, *ast.ChanType, *ast.FuncType, *ast.InterfaceType, *ast.MapType, *ast.StructType:
34		return true
35	case *ast.StarExpr:
36		return IsTypeExpr(e.X, info)
37	case *ast.Ident:
38		_, ok := info.Uses[e].(*types.TypeName)
39		return ok
40	case *ast.SelectorExpr:
41		_, ok := info.Uses[e.Sel].(*types.TypeName)
42		return ok
43	case *ast.ParenExpr:
44		return IsTypeExpr(e.X, info)
45	default:
46		return false
47	}
48}
49