-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpackage.go
73 lines (61 loc) · 1.9 KB
/
package.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
67
68
69
70
71
72
73
// Copyright 2020 murosan. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gollect
import (
"go/ast"
"go/types"
)
// Package represents analyzing information.
type Package struct {
path string // package path
files []*ast.File // container of ast files
objects map[string]*ast.Object // map of package-level objects
info *types.Info // uses info
}
// NewPackage returns new Package.
func NewPackage(path string) *Package {
return &Package{
path: path,
files: nil,
objects: make(map[string]*ast.Object),
info: &types.Info{
Uses: make(map[*ast.Ident]types.Object),
// Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
},
}
}
// Path returns package path.
func (pkg *Package) Path() string { return pkg.path }
// InitObjects compiles all files' objects into one map.
// This is called after parsing all ast files and before
// start analyzing dependencies.
func (pkg *Package) InitObjects() {
for _, file := range pkg.files {
for k, v := range file.Scope.Objects {
k, v := k, v
pkg.objects[k] = v
}
}
}
// GetObject gets and returns object which scope is package-level.
func (pkg *Package) GetObject(key string) (*ast.Object, bool) {
o, ok := pkg.objects[key]
return o, ok
}
// PushAstFile push ast.File to files.
func (pkg *Package) PushAstFile(f *ast.File) {
pkg.files = append(pkg.files, f)
}
func (pkg *Package) Info() *types.Info { return pkg.info }
// PackageSet is a map of Package.
type PackageSet map[string]*Package
// Add sets the Package to set.
func (p PackageSet) Add(path string, pkg *Package) { p[path] = pkg }
// Get gets a Package from set.
func (p PackageSet) Get(path string) (*Package, bool) {
pkg, ok := p[path]
return pkg, ok
}