forked from cosmos72/gomacro
/
genimport.go
379 lines (334 loc) · 9.18 KB
/
genimport.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* gomacro - A Go interpreter with Lisp-like macros
*
* Copyright (C) 2017-2019 Massimiliano Ghilardi
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*
* genimport.go
*
* Created on May 26, 2017
* Author Massimiliano Ghilardi
*/
package genimport
import (
"bytes"
"fmt"
"go/constant"
"go/types"
"math"
"strconv"
"strings"
"github.com/lifepod-solutions/gomacro/base/output"
"github.com/lifepod-solutions/gomacro/base/untyped"
)
type Output = output.Output
type genimport struct {
output *Output
mode ImportMode
gpkg *types.Package
scope *types.Scope
names []string
pkgrenames map[string]string // map[path]name of packages to import, where name:s are guaranteed to be unique
out *bytes.Buffer
path string
name, name_ string
proxyprefix string
reflect string
}
func writeImportFile(o *Output, out *bytes.Buffer, path string, gpkg *types.Package, mode ImportMode) (isEmpty bool) {
gen := newGenImport(o, out, path, gpkg, mode)
if gen == nil {
return true
}
gen.write()
return false
}
func newGenImport(o *Output, out *bytes.Buffer, path string, gpkg *types.Package, mode ImportMode) *genimport {
scope := gpkg.Scope()
names := scope.Names()
isEmpty := true
for _, name := range names {
if obj := scope.Lookup(name); obj.Exported() {
switch obj.(type) {
case *types.Const, *types.Var, *types.Func, *types.TypeName:
isEmpty = false
break
}
}
}
if isEmpty {
return nil
}
gen := &genimport{output: o, mode: mode, gpkg: gpkg, scope: scope, names: names, out: out, path: path}
if mode == ImInception {
gen.reflect = "r."
gen.name = gpkg.Name()
}
if mode == ImPlugin {
gen.proxyprefix = "P_"
} else {
gen.proxyprefix = fmt.Sprintf("P_%s_", sanitizeIdent(path))
}
return gen
}
func (gen *genimport) write() {
gen.writePreamble()
gen.writeBinds()
gen.writeTypes()
gen.writeProxies()
gen.writeUntypeds()
gen.writeWrappers()
gen.out.WriteString("\n\t}\n}\n")
gen.writeInterfaceProxies()
}
type mapdecl struct {
out *bytes.Buffer
head string
foot string
}
func (gen *genimport) mapdecl(head string) mapdecl {
if strings.IndexByte(head, '%') >= 0 {
head = fmt.Sprintf(head, gen.reflect)
}
return mapdecl{gen.out, head, ""}
}
func (d *mapdecl) header() {
if len(d.head) != 0 {
d.out.WriteString(d.head)
d.out.WriteByte('{')
d.head = ""
d.foot = "\n\t}"
}
}
func (d *mapdecl) footer() {
if len(d.foot) != 0 {
d.out.WriteString(d.foot)
d.out.WriteString(", ")
}
}
func (d *mapdecl) footer1(comma bool) {
if len(d.foot) != 0 {
d.out.WriteString(d.foot)
if comma {
d.out.WriteString(", ")
}
}
}
func (gen *genimport) collectPackageImportsWithRename(requireAllInterfaceMethodsExported bool) {
gen.pkgrenames = collectPackageImportsWithRename(gen.output, gen.gpkg, requireAllInterfaceMethodsExported)
gen.name = gen.pkgrenames[gen.path]
if gen.name == "" {
gen.name = packageSanitizedName(gen.path)
}
if gen.mode != ImInception {
gen.name_ = gen.name + "."
}
}
func (gen *genimport) writePreamble() {
mode := gen.mode
out := gen.out
var alias, filepkg string
switch mode {
case ImBuiltin:
alias = "_b "
filepkg = "imports"
case ImThirdParty:
filepkg = "thirdparty"
case ImPlugin:
filepkg = "main"
case ImInception:
alias = "_i "
filepkg = gen.name
}
fmt.Fprintf(gen.out, `// this file was generated by gomacro command: import %s%q
// DO NOT EDIT! Any change will be lost when the file is re-generated
package %s
import (`, alias, gen.path, filepkg)
var imports string
if mode == ImInception {
fmt.Fprintf(gen.out, "\n\tr \"reflect\"\n\t\"github.com/lifepod-solutions/gomacro/imports\"")
imports = "imports."
} else {
fmt.Fprintf(out, "\n\t. \"reflect\"")
}
gen.collectPackageImportsWithRename(true)
for path, name := range gen.pkgrenames {
if mode == ImInception && path == gen.path {
continue // writing inside the package: it should not import itself
} else {
// always name the imported package: its name may differ from paths.FileName(path)
fmt.Fprintf(out, "\n\t%s %q", name, path)
}
}
fmt.Fprintf(out, "\n)\n")
if mode == ImInception {
gen.pkgrenames[gen.path] = "" // writing inside the package: remove the package prefix
}
if mode == ImPlugin {
fmt.Fprint(out, `
type Package = struct {
Name string
Binds map[string]Value
Types map[string]Type
Proxies map[string]Type
Untypeds map[string]string
Wrappers map[string][]string
}
var Packages = make(map[string]Package)
func main() {
}
`)
}
fmt.Fprintf(out, `
// reflection: allow interpreted code to import %q
func init() {
%sPackages[%q] = %sPackage{
Name: %q,
`, gen.path, imports, gen.path, imports, gen.gpkg.Name())
}
func (gen *genimport) writeBinds() {
d := gen.mapdecl("Binds: map[string]%sValue")
for _, name := range gen.names {
if obj := gen.scope.Lookup(name); obj.Exported() {
switch obj := obj.(type) {
case *types.Const:
val := obj.Val()
var conv1, conv2 string
if t, ok := obj.Type().(*types.Basic); ok && t.Info()&types.IsUntyped != 0 {
// untyped constants have arbitrary precision... they may overflow integers.
// this is just an approximation, use Package.Untypeds for exact value
if val.Kind() == constant.Int {
str := val.ExactString()
conv1, conv2 = detectIntKind(gen.output, gen.path, name, str)
}
}
d.header()
fmt.Fprintf(gen.out, "\n\t\t%q:\t%sValueOf(%s%s%s%s),", name, gen.reflect, conv1, gen.name_, name, conv2)
case *types.Var:
d.header()
fmt.Fprintf(gen.out, "\n\t\t%q:\t%sValueOf(&%s%s).Elem(),", name, gen.reflect, gen.name_, name)
case *types.Func:
d.header()
fmt.Fprintf(gen.out, "\n\t\t%q:\t%sValueOf(%s%s),", name, gen.reflect, gen.name_, name)
}
}
}
d.footer()
}
func (gen *genimport) writeTypes() {
d := gen.mapdecl("Types: map[string]%sType")
for _, name := range gen.names {
if obj := gen.scope.Lookup(name); obj.Exported() {
switch obj.(type) {
case *types.TypeName:
d.header()
fmt.Fprintf(gen.out, "\n\t\t%q:\t%sTypeOf((*%s%s)(nil)).Elem(),", name, gen.reflect, gen.name_, name)
}
}
}
d.footer()
}
func (gen *genimport) writeProxies() {
d := gen.mapdecl("Proxies: map[string]%sType")
for _, name := range gen.names {
if obj := gen.scope.Lookup(name); obj.Exported() {
if t := extractInterface(obj, true); t != nil {
d.header()
fmt.Fprintf(gen.out, "\n\t\t%q:\t%sTypeOf((*%s%s)(nil)).Elem(),", name, gen.reflect, gen.proxyprefix, name)
}
}
}
d.footer()
}
func (gen *genimport) writeUntypeds() {
d := gen.mapdecl("Untypeds: map[string]string")
for _, name := range gen.names {
if obj := gen.scope.Lookup(name); obj.Exported() {
switch obj := obj.(type) {
case *types.Const:
if t, ok := obj.Type().(*types.Basic); ok && t.Info()&types.IsUntyped != 0 {
kind := untyped.GoUntypedToKind(t.Kind())
str := untyped.Marshal(kind, obj.Val())
if len(str) != 0 {
d.header()
fmt.Fprintf(gen.out, "\n\t\t%q:\t%q,", name, str)
}
}
}
}
}
d.footer()
}
// find wrapper methods and write them. needed for accurate method selection.
func (gen *genimport) writeWrappers() {
d := gen.mapdecl("Wrappers: map[string][]string")
for _, name := range gen.names {
if obj := gen.scope.Lookup(name); obj.Exported() {
switch obj.(type) {
case *types.TypeName:
if t, ok := obj.Type().(*types.Named); ok {
// only structs can have embedded fields, and thus wrapper methods for embedded fields
if _, ok := t.Underlying().(*types.Struct); ok {
wrappers := new(analyzer).Analyze(t)
if len(wrappers) != 0 {
d.header()
fmt.Fprintf(gen.out, "\n\t\t%q:\t[]string{", obj.Name())
for _, wrapper := range wrappers {
fmt.Fprintf(gen.out, "%q,", wrapper)
}
fmt.Fprint(gen.out, "},")
}
}
}
}
}
}
d.footer()
}
// write proxies that pre-implement package's interfaces
func (gen *genimport) writeInterfaceProxies() {
path := gen.gpkg.Path()
for _, name := range gen.names {
obj := gen.scope.Lookup(name)
if t := extractInterface(obj, true); t != nil {
gen.writeInterfaceProxy(path, name, t)
}
}
}
func detectIntKind(o *Output, path, name, str string) (string, string) {
i, err := strconv.ParseInt(str, 0, 64)
if err == nil {
if i == int64(int32(i)) {
// constant fits int32. We can use the default (i.e. int)
// on both 32-bit and 64-bit platforms
return "", ""
} else if i == int64(uint32(i)) {
// constant fits uint32
return "uint32(", ")"
} else {
return "int64(", ")"
}
}
_, err = strconv.ParseUint(str, 0, 64)
if err == nil {
return "uint64(", ")"
}
f, err := strconv.ParseFloat(str, 64)
if err != nil {
// nothing fits... leave the default
return "", ""
} else {
prefix := "float64"
f = math.Abs(f)
if f == float64(float32(f)) && f <= math.MaxFloat32 && f >= math.SmallestNonzeroFloat32 {
// float32 loses no precision vs. float64
prefix = "float32"
}
o.Warnf("package %q: integer constant %s = %s overflows both int64 and uint64, converting to %s", path, name, str, prefix)
return prefix + "(", ")"
}
}