-
Notifications
You must be signed in to change notification settings - Fork 0
/
distinctPackage.go
145 lines (121 loc) · 3.15 KB
/
distinctPackage.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package langd
import (
"fmt"
"go/ast"
"go/build"
"go/types"
"sync"
"github.com/object88/langd/collections"
"github.com/pkg/errors"
)
// DistinctPackage contains the os/arch specific package AST
type DistinctPackage struct {
Package *Package
hash collections.Hash
l *Loader
loadState loadState
m sync.Mutex
c *sync.Cond
buildPkg *build.Package
checker *types.Checker
files map[string]*File
typesPkg *types.Package
}
// NewDistinctPackage returns a new instance of DistinctPackage
func NewDistinctPackage(l *Loader, p *Package) *DistinctPackage {
hash := l.calculateDistinctPackageHash(p.AbsPath)
dp := &DistinctPackage{
Package: p,
files: map[string]*File{},
hash: hash,
l: l,
}
dp.c = sync.NewCond(&dp.m)
return dp
}
func (dp *DistinctPackage) check() error {
if dp.checker == nil {
info := &types.Info{
Defs: map[*ast.Ident]types.Object{},
// Implicits: map[ast.Node]types.Object{},
// Scopes: map[ast.Node]*types.Scope{},
Selections: map[*ast.SelectorExpr]*types.Selection{},
// Types: map[ast.Expr]types.TypeAndValue{},
Uses: map[*ast.Ident]types.Object{},
}
dp.typesPkg = types.NewPackage(dp.Package.AbsPath, dp.buildPkg.Name)
dp.checker = types.NewChecker(dp.l.config, dp.Package.Fset, dp.typesPkg, info)
}
// Loop over files and clear previous errors; all will be rechecked.
astFiles := []*ast.File{}
for _, v := range dp.files {
f := v
if !f.checked {
f.errs = []FileError{}
astFiles = append(astFiles, f.file)
}
}
if len(astFiles) == 0 {
return nil
}
dp.m.Lock()
err := dp.checker.Files(astFiles)
dp.m.Unlock()
for _, v := range dp.files {
v.checked = true
}
if err != nil {
return errors.Wrapf(err, "DistinctPackage.check (%s): Checker failed", dp)
}
return nil
}
func (dp *DistinctPackage) generateBuildPackage() error {
buildPkg, err := dp.l.context.Import(".", dp.Package.AbsPath, 0)
if err != nil {
if _, ok := err.(*build.NoGoError); ok {
// There isn't any Go code here.
return nil
}
return errors.Wrapf(err, "generateBuildPackage (%s): error while importing with build.Context", dp)
}
dp.buildPkg = buildPkg
return nil
}
// Hash returns the hash for this distinct package
func (dp *DistinctPackage) Hash() collections.Hash {
return dp.hash
}
// Invalidate sets the checker to nil and the loadState to unloaded
func (dp *DistinctPackage) Invalidate() {
dp.loadState = unloaded
dp.checker = nil
dp.typesPkg = nil
}
func (dp *DistinctPackage) String() string {
return fmt.Sprintf("%s %s", dp.l.GetTags(), dp.Package)
}
// WaitUntilReady blocks until this distinct package has loaded sufficiently
// for the requested load state.
func (dp *DistinctPackage) WaitUntilReady(loadState loadState) {
check := func() bool {
thisLoadState := dp.loadState.get()
switch loadState {
case queued:
// Does not make sense that the source loadState would be here.
case unloaded:
return thisLoadState > unloaded
case loadedGo:
return thisLoadState > unloaded
case loadedTest:
// Should pass through here.
default:
// Should never get here.
}
return false
}
dp.m.Lock()
for !check() {
dp.c.Wait()
}
dp.m.Unlock()
}