-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplanner_visibility.go
More file actions
260 lines (237 loc) · 6.71 KB
/
Copy pathplanner_visibility.go
File metadata and controls
260 lines (237 loc) · 6.71 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package morph
import (
"cmp"
"fmt"
"go/ast"
"slices"
"strings"
"github.com/seeruk/morph/plan"
"github.com/seeruk/morph/types"
)
type visibilityRequirementSite struct {
From string
Path string
Reason string
Identifier string
Package types.PackageRef
Type *plan.Type
Value *plan.Value
}
func typeSignatureVisibilitySites(ctx walkContext) []visibilityRequirementSite {
if ctx.Type == nil {
return nil
}
sites := typeVisibilityRequirementSites(
ctx.Location.ImportPath,
ctx.Path,
fmt.Sprintf("mapper %q source signature", ctx.Type.FunctionName),
ctx.Type.SourceType,
ctx.Type,
nil,
)
sites = append(sites, typeVisibilityRequirementSites(
ctx.Location.ImportPath,
ctx.Path,
fmt.Sprintf("mapper %q target signature", ctx.Type.FunctionName),
ctx.Type.TargetType,
ctx.Type,
nil,
)...)
return sites
}
func valueVisibilitySites(ctx walkContext) []visibilityRequirementSite {
value := ctx.Value
if value == nil {
return nil
}
var sites []visibilityRequirementSite
if len(value.SourceAdaptations) > 0 || len(value.TargetAdaptations) > 0 {
sites = append(sites, typeVisibilityRequirementSites(
ctx.Location.ImportPath,
ctx.Path,
"adapted target type",
value.Target,
ctx.Owner,
value,
)...)
}
switch value.Operation {
case plan.OperationConvert:
sites = append(sites, typeVisibilityRequirementSites(
ctx.Location.ImportPath,
ctx.Path,
"conversion target type",
value.Target,
ctx.Owner,
value,
)...)
case plan.OperationSlice, plan.OperationArray, plan.OperationMap:
sites = append(sites, typeVisibilityRequirementSites(
ctx.Location.ImportPath,
ctx.Path,
"container target type",
value.Target,
ctx.Owner,
value,
)...)
}
for i := range value.CallableMapperArgs {
arg := &value.CallableMapperArgs[i]
argPath := callableMapperArgPath(ctx.Path, i)
sites = append(sites, typeVisibilityRequirementSites(
ctx.Location.ImportPath,
argPath,
fmt.Sprintf("callable argument %d source type", i+1),
arg.Mapping.Source,
ctx.Owner,
&arg.Mapping,
)...)
sites = append(sites, typeVisibilityRequirementSites(
ctx.Location.ImportPath,
argPath,
fmt.Sprintf("callable argument %d target type", i+1),
arg.Mapping.Target,
ctx.Owner,
&arg.Mapping,
)...)
}
return sites
}
func typeVisibilityRequirementSites(
from string,
path string,
reason string,
typ types.Type,
owner *plan.Type,
value *plan.Value,
) []visibilityRequirementSite {
var sites []visibilityRequirementSite
collectTypeVisibilityRequirementSites(from, path, reason, typ, owner, value, &sites)
return sites
}
func collectTypeVisibilityRequirementSites(
from string,
path string,
reason string,
typ types.Type,
owner *plan.Type,
value *plan.Value,
refs *[]visibilityRequirementSite,
) {
typ = types.UnwrapAlias(typ)
switch typ.Kind {
case types.TypeKindNamed, types.TypeKindAlias:
if !isIdentifierAccessibleFrom(typ.Package, typ.Name, from) {
*refs = append(*refs, visibilityRequirementSite{
From: from,
Path: path,
Reason: reason,
Identifier: typ.Name,
Package: typ.Package,
Type: owner,
Value: value,
})
}
for _, arg := range typ.TypeArgs {
collectTypeVisibilityRequirementSites(from, path, reason, arg, owner, value, refs)
}
case types.TypeKindPointer, types.TypeKindSlice, types.TypeKindArray, types.TypeKindChan:
if typ.Elem != nil {
collectTypeVisibilityRequirementSites(from, path, reason, *typ.Elem, owner, value, refs)
}
case types.TypeKindMap:
if typ.Key != nil {
collectTypeVisibilityRequirementSites(from, path, reason, *typ.Key, owner, value, refs)
}
if typ.Value != nil {
collectTypeVisibilityRequirementSites(from, path, reason, *typ.Value, owner, value, refs)
}
case types.TypeKindSignature:
for _, param := range typ.Params {
collectTypeVisibilityRequirementSites(from, path, reason, param.Type, owner, value, refs)
}
for _, result := range typ.Results {
collectTypeVisibilityRequirementSites(from, path, reason, result.Type, owner, value, refs)
}
case types.TypeKindStruct:
for _, field := range typ.Fields {
collectTypeVisibilityRequirementSites(from, path, reason, field.Type, owner, value, refs)
}
case types.TypeKindInterface:
for _, method := range typ.Methods {
if method.Receiver != nil {
collectTypeVisibilityRequirementSites(from, path, reason, method.Receiver.Type, owner, value, refs)
}
for _, param := range method.Params {
collectTypeVisibilityRequirementSites(from, path, reason, param.Type, owner, value, refs)
}
for _, result := range method.Results {
collectTypeVisibilityRequirementSites(from, path, reason, result.Type, owner, value, refs)
}
}
}
}
func dedupeVisibilityRequirementSites(
refs []visibilityRequirementSite,
) []visibilityRequirementSite {
slices.SortFunc(refs, func(a, b visibilityRequirementSite) int {
return cmp.Or(
strings.Compare(a.From, b.From),
strings.Compare(a.Package.ImportPath, b.Package.ImportPath),
strings.Compare(a.Identifier, b.Identifier),
strings.Compare(a.Path, b.Path),
strings.Compare(a.Reason, b.Reason),
)
})
out := refs[:0]
var previous string
for i, ref := range refs {
key := visibilityRequirementKey(ref)
if i > 0 && key == previous {
continue
}
out = append(out, ref)
previous = key
}
return out
}
func visibilityRequirementKey(ref visibilityRequirementSite) string {
return strings.Join([]string{
ref.From,
ref.Package.ImportPath,
ref.Identifier,
ref.Path,
ref.Reason,
}, "\x00")
}
func (p *attemptPlanner) validateVisibilityRequirements(sites []visibilityRequirementSite) {
for _, site := range sites {
diagnostic := visibilityRequirementDiagnostic(site)
if site.Value != nil {
site.Value.Diagnostics = appendDiagnostic(site.Value.Diagnostics, diagnostic)
}
if site.Type != nil {
site.Type.Diagnostics = appendDiagnostic(site.Type.Diagnostics, diagnostic)
}
p.diagnostics = appendDiagnostic(p.diagnostics, diagnostic)
}
}
func visibilityRequirementDiagnostic(site visibilityRequirementSite) plan.Diagnostic {
return plan.Diagnostic{
Level: plan.DiagnosticLevelFatal,
Path: site.Path,
Message: fmt.Sprintf(
"%s references unexported type %q from package %q, but generated package %q cannot name it; unexported types can only be named from their declaring package",
site.Reason,
site.Identifier,
site.Package.ImportPath,
site.From,
),
}
}
func isIdentifierAccessibleFrom(pkg types.PackageRef, name string, from string) bool {
return pkg.ImportPath == "" || ast.IsExported(name) || pkg.ImportPath == from
}
func isFieldAccessibleFrom(typeDecl types.TypeDecl, field types.Field, from string) bool {
return field.IsExported || typeDecl.Package.ImportPath == from
}