-
Notifications
You must be signed in to change notification settings - Fork 0
/
references.go
183 lines (155 loc) · 4.08 KB
/
references.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package parser
import (
"strings"
)
// -----------------------------------------------------------------------------
type refResolver struct {
ModulesMap map[string][]*ParsedFile
currentFile *ParsedFile
currentDecl *ParsedDeclaration
}
// -----------------------------------------------------------------------------
// ResolveReferences tries to resolve all ParsedNonNativeType references
func ResolveReferences(parsedFiles []*ParsedFile) {
rr := refResolver{
ModulesMap: make(map[string][]*ParsedFile),
}
// Create a map of files classified by module and package
for _, pf := range parsedFiles {
moduleName := pf.Module.FullName()
pFiles, ok := rr.ModulesMap[moduleName]
if !ok {
pFiles = make([]*ParsedFile, 0)
}
rr.ModulesMap[moduleName] = append(pFiles, pf)
}
for _, pf := range parsedFiles {
rr.currentFile = pf
for pdIdx, pd := range pf.Declarations {
rr.currentDecl = &pf.Declarations[pdIdx]
rr.resolve(pd.Type)
}
}
}
func (rr *refResolver) resolve(t interface{}) {
switch tType := t.(type) {
case *ParsedNonNativeType:
rr.resolveNonNativeType(tType)
case *ParsedStruct:
rr.processStruct(tType)
case *ParsedInterface:
rr.processInterface(tType)
case *ParsedArray:
rr.processArray(tType)
case *ParsedMap:
rr.processMap(tType)
case *ParsedPointer:
rr.processPointer(tType)
case *ParsedChannel:
rr.processChannel(tType)
case *ParsedFunction:
rr.processFunction(tType)
}
}
func (rr *refResolver) processStruct(ps *ParsedStruct) {
for _, field := range ps.Fields {
rr.resolve(field.Type)
}
}
func (rr *refResolver) processInterface(pi *ParsedInterface) {
for _, field := range pi.Methods {
rr.resolve(field.Type)
}
}
func (rr *refResolver) processArray(pa *ParsedArray) {
rr.resolve(pa.ValueType)
}
func (rr *refResolver) processMap(pm *ParsedMap) {
rr.resolve(pm.KeyType)
rr.resolve(pm.ValueType)
}
func (rr *refResolver) processPointer(pp *ParsedPointer) {
rr.resolve(pp.ToType)
}
func (rr *refResolver) processChannel(pc *ParsedChannel) {
rr.resolve(pc.Type)
}
func (rr *refResolver) processFunction(pf *ParsedFunction) {
for _, field := range pf.TypeParams {
rr.resolve(field.Type)
}
for _, field := range pf.Params {
rr.resolve(field.Type)
}
for _, field := range pf.Results {
rr.resolve(field.Type)
}
}
func (rr *refResolver) resolveNonNativeType(pnnt *ParsedNonNativeType) {
if pnnt.Ref != nil {
return // Already resolved
}
moduleName := rr.currentFile.Module.FullName()
// Assume the local reference by default
objName := pnnt.Name
dotIdx := strings.Index(pnnt.Name, ".")
if dotIdx >= 0 {
// Find the import
importedPackageName := pnnt.Name[:dotIdx]
objName = objName[(dotIdx + 1):]
importPath := ""
for _, pi := range rr.currentFile.Imports {
if pi.Name == importedPackageName {
importPath = pi.Path
break
}
}
if len(importPath) == 0 {
return // Unable to determine import path, let's continue
}
if importPath != "." {
if strings.HasPrefix(importPath, ".") {
// Assume a relative path
tempPath := importPath
if len(rr.currentFile.Module.SubDir) > 0 {
tempPath = rr.currentFile.Module.SubDir + "/" + tempPath
}
fragments := strings.Split(tempPath, "/")
idx := 0
for idx < len(fragments) {
if fragments[idx] == "." {
fragments = append(fragments[0:idx], fragments[(idx+1):]...)
} else if fragments[idx] == ".." {
if idx == 0 {
return // Invalid path
}
fragments = append(fragments[0:(idx-1)], fragments[(idx+1):]...)
idx -= 1
} else {
idx += 1
}
}
moduleName = rr.currentFile.Module.Name
if len(fragments) > 0 {
moduleName += "/" + strings.Join(fragments, "/")
}
} else {
moduleName = importPath
}
}
}
parsedFilesToCheck, ok := rr.ModulesMap[moduleName]
if (!ok) || len(parsedFilesToCheck) == 0 {
return // Unable to find other modules
}
for _, pf2c := range parsedFilesToCheck {
for pd2cIdx := range pf2c.Declarations {
pd2c := &pf2c.Declarations[pd2cIdx]
if pd2c.Name == objName {
// Found the reference
pnnt.Ref = pd2c
return
}
}
}
}