-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathidents.go
66 lines (55 loc) · 1.38 KB
/
idents.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
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
package goose
import (
"go/ast"
"go/types"
)
// This file gives extra info about identifiers.
// E.g., it helps track ptr wrapping, which Go doesn't easily expose
// since it's not visible in the language.
type identCtx struct {
isPtrWrapped map[types.Object]bool
}
func newIdentCtx() identCtx {
return identCtx{isPtrWrapped: make(map[types.Object]bool)}
}
func (ctx Ctx) isGlobalVar(ident *ast.Ident) bool {
obj := ctx.getObj(ident)
return obj.Pkg() != nil && obj.Parent() == obj.Pkg().Scope()
}
func (ctx Ctx) isPtrWrapped(ident *ast.Ident) bool {
obj := ctx.getObj(ident)
isWrapped, ok := ctx.idents.isPtrWrapped[obj]
if !ok {
return false
}
return isWrapped
}
func (ctx Ctx) setPtrWrapped(ident *ast.Ident) {
obj := ctx.getObj(ident)
ctx.idents.isPtrWrapped[obj] = true
}
func (ctx Ctx) getObj(ident *ast.Ident) types.Object {
obj := ctx.info.Uses[ident]
if obj == nil {
obj = ctx.info.Defs[ident]
}
if obj == nil {
ctx.unsupported(ident, "type checker doesn't have info about this ident")
}
return obj
}
func (ctx Ctx) isStruct(ident *ast.Ident) bool {
obj := ctx.getObj(ident)
_, ok := obj.Type().Underlying().(*types.Struct)
return ok
}
func getIdent(e ast.Expr) (ident string, ok bool) {
if ident, ok := e.(*ast.Ident); ok {
return ident.Name, true
}
return "", false
}
func isIdent(e ast.Expr, ident string) bool {
i, ok := getIdent(e)
return ok && i == ident
}