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
|
package astutil
import (
"go/ast"
"go/types"
)
func RemoveParens(e ast.Expr) ast.Expr {
for {
p, isParen := e.(*ast.ParenExpr)
if !isParen {
return e
}
e = p.X
}
}
func SetType(info *types.Info, t types.Type, e ast.Expr) ast.Expr {
info.Types[e] = types.TypeAndValue{Type: t}
return e
}
func NewIdent(name string, t types.Type, info *types.Info, pkg *types.Package) *ast.Ident {
ident := ast.NewIdent(name)
info.Types[ident] = types.TypeAndValue{Type: t}
obj := types.NewVar(0, pkg, name, t)
info.Uses[ident] = obj
return ident
}
func IsTypeExpr(expr ast.Expr, info *types.Info) bool {
switch e := expr.(type) {
case *ast.ArrayType, *ast.ChanType, *ast.FuncType, *ast.InterfaceType, *ast.MapType, *ast.StructType:
return true
case *ast.StarExpr:
return IsTypeExpr(e.X, info)
case *ast.Ident:
_, ok := info.Uses[e].(*types.TypeName)
return ok
case *ast.SelectorExpr:
_, ok := info.Uses[e.Sel].(*types.TypeName)
return ok
case *ast.ParenExpr:
return IsTypeExpr(e.X, info)
default:
return false
}
}
|