-
Notifications
You must be signed in to change notification settings - Fork 3
/
predicates.go
36 lines (31 loc) · 935 Bytes
/
predicates.go
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
package typep
import (
"go/ast"
"go/types"
)
// IsTypeExpr reports whether x represents a type expression.
//
// Type expression does not evaluate to any run time value,
// but rather describes a type that is used inside Go expression.
//
// For example, (*T)(v) is a CallExpr that "calls" (*T).
// (*T) is a type expression that tells Go compiler type v should be converted to.
func IsTypeExpr(info *types.Info, x ast.Expr) bool {
switch x := x.(type) {
case *ast.StarExpr:
return IsTypeExpr(info, x.X)
case *ast.ParenExpr:
return IsTypeExpr(info, x.X)
case *ast.SelectorExpr:
return IsTypeExpr(info, x.Sel)
case *ast.Ident:
// Identifier may be a type expression if object
// it reffers to is a type name.
_, ok := info.ObjectOf(x).(*types.TypeName)
return ok
case *ast.FuncType, *ast.StructType, *ast.InterfaceType, *ast.ArrayType, *ast.MapType, *ast.ChanType:
return true
default:
return false
}
}