1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
18 "golang.org/x/tools/go/ast/astutil"
21 func objectKind(obj types.Object) string {
22 switch obj := obj.(type) {
24 return "imported package name"
32 if obj.Type().(*types.Signature).Recv() != nil {
36 // label, func, var, const
37 return strings.ToLower(strings.TrimPrefix(reflect.TypeOf(obj).String(), "*types."))
40 func typeKind(T types.Type) string {
41 return strings.ToLower(strings.TrimPrefix(reflect.TypeOf(T.Underlying()).String(), "*types."))
44 // NB: for renamings, blank is not considered valid.
45 func isValidIdentifier(id string) bool {
46 if id == "" || id == "_" {
49 for i, r := range id {
50 if !isLetter(r) && (i == 0 || !isDigit(r)) {
54 return token.Lookup(id) == token.IDENT
57 // isLocal reports whether obj is local to some function.
58 // Precondition: not a struct field or interface method.
59 func isLocal(obj types.Object) bool {
60 // [... 5=stmt 4=func 3=file 2=pkg 1=universe]
62 for scope := obj.Parent(); scope != nil; scope = scope.Parent() {
68 func isPackageLevel(obj types.Object) bool {
69 return obj.Pkg().Scope().Lookup(obj.Name()) == obj
72 // -- Plundered from go/scanner: ---------------------------------------
74 func isLetter(ch rune) bool {
75 return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= 0x80 && unicode.IsLetter(ch)
78 func isDigit(ch rune) bool {
79 return '0' <= ch && ch <= '9' || ch >= 0x80 && unicode.IsDigit(ch)
82 // -- Plundered from golang.org/x/tools/cmd/guru -----------------
84 // sameFile returns true if x and y have the same basename and denote
87 func sameFile(x, y string) bool {
88 if runtime.GOOS == "windows" {
89 x = filepath.ToSlash(x)
90 y = filepath.ToSlash(y)
95 if filepath.Base(x) == filepath.Base(y) { // (optimisation)
96 if xi, err := os.Stat(x); err == nil {
97 if yi, err := os.Stat(y); err == nil {
98 return os.SameFile(xi, yi)
105 func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) }