/
name_solver.go
48 lines (39 loc) · 1.04 KB
/
name_solver.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
package collector
import (
"go/types"
"github.com/hsfzxjy/dgo/dgo-gen/internal/exception"
"github.com/hsfzxjy/dgo/dgo-gen/internal/interfaces"
"github.com/hsfzxjy/dgo/dgo-gen/internal/uri"
)
type NameSolver struct {
m interfaces.Storage
}
func NewNameSolver(m interfaces.Storage) *NameSolver {
return &NameSolver{m}
}
func (r *NameSolver) HasTypeName(obj *types.TypeName) bool {
if obj.Pkg() == nil {
return false
}
pkg := r.PackageOf(obj.Pkg().Path())
return pkg.HasType(obj.Name())
}
func (r *NameSolver) ThrowAt(obj types.Object, args ...any) {
pkgPath := obj.Pkg().Path()
r.m.ParsePackages(pkgPath, true)
ppkg := r.m.Package(pkgPath).PPackage()
exception.ThrowAt(ppkg, obj, args...)
}
func (r *NameSolver) PackageOf(pkgPath string) interfaces.Package {
TRY_LOAD_PKG:
if pkg := r.m.Package(pkgPath); pkg != nil {
return pkg
} else {
r.m.ParsePackages(pkgPath, true)
goto TRY_LOAD_PKG
}
}
func (r *NameSolver) UriFor(obj types.Object) uri.Uri {
ppkg := r.PackageOf(obj.Pkg().Path()).PPackage()
return uri.UriFor(ppkg, obj.Name())
}