You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.0 KiB
49 lines
1.0 KiB
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
|
|
}
|
|
}
|