-
Notifications
You must be signed in to change notification settings - Fork 29
/
package.go
486 lines (419 loc) · 11.7 KB
/
package.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
Copyright 2021 The GoPlus Authors (goplus.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gogen
import (
"go/ast"
"go/token"
"go/types"
"log"
"sort"
"strings"
"syscall"
"github.com/goplus/gogen/packages"
)
type LoadNamedFunc = func(at *Package, typ *types.Named)
const (
DbgFlagInstruction = 1 << iota
DbgFlagImport
DbgFlagMatch
DbgFlagComments
DbgFlagWriteFile
DbgFlagSetDebug
DbgFlagPersistCache
DbgFlagAll = DbgFlagInstruction | DbgFlagImport | DbgFlagMatch |
DbgFlagComments | DbgFlagWriteFile | DbgFlagSetDebug | DbgFlagPersistCache
)
var (
debugInstr bool
debugMatch bool
debugImport bool
debugComments bool
debugWriteFile bool
debugImportIox bool
)
func SetDebug(dbgFlags int) {
debugInstr = (dbgFlags & DbgFlagInstruction) != 0
debugImport = (dbgFlags & DbgFlagImport) != 0
debugMatch = (dbgFlags & DbgFlagMatch) != 0
debugComments = (dbgFlags & DbgFlagComments) != 0
debugWriteFile = (dbgFlags & DbgFlagWriteFile) != 0
if (dbgFlags & DbgFlagSetDebug) != 0 {
log.Printf("SetDebug: import=%v, match=%v, instr=%v\n", debugImport, debugMatch, debugInstr)
}
}
type fatalMsg string
func fatal(msg string) {
panic(fatalMsg(msg))
}
// ----------------------------------------------------------------------------
// Recorder represents a gogen event recorder.
type Recorder interface {
// Member maps identifiers to the objects they denote.
Member(id ast.Node, obj types.Object)
// Call maps func to the the objects they denote.
Call(fn ast.Node, obj types.Object)
}
// ----------------------------------------------------------------------------
type dbgPositioner interface {
// Position gets position of a Pos.
Position(p token.Pos) token.Position
}
type NodeInterpreter interface {
// LoadExpr is called to load an expr code.
LoadExpr(expr ast.Node) string
}
// Config type
type Config struct {
// Types provides type information for the package (optional).
Types *types.Package
// Fset provides source position information for syntax trees and types (optional).
// If Fset is nil, Load will use a new fileset, but preserve Fset's value.
Fset *token.FileSet
// HandleErr is called to handle errors (optional).
HandleErr func(err error)
// NodeInterpreter is to interpret an ast.Node (optional).
NodeInterpreter NodeInterpreter
// LoadNamed is called to load a delay-loaded named type (optional).
LoadNamed LoadNamedFunc
// An Importer resolves import paths to Packages (optional).
Importer types.Importer
// DefaultGoFile specifies default file name. It can be empty.
DefaultGoFile string
// PkgPathIox specifies package path of github.com/goplus/gop/builtin/iox
PkgPathIox string
// NewBuiltin is to create the builin package (optional).
NewBuiltin func(pkg *Package, conf *Config) *types.Package
// CanImplicitCast checkes can cast V to T implicitly (optional).
CanImplicitCast func(pkg *Package, V, T types.Type, pv *Element) bool
// untyped bigint, untyped bigrat, untyped bigfloat (optional).
UntypedBigInt, UntypedBigRat, UntypedBigFloat *types.Named
// A Recorder records selected objects such as methods, etc (optional).
Recorder Recorder
// (internal) only for testing
DbgPositioner dbgPositioner
// NoSkipConstant is to disable optimization of skipping constant (optional).
NoSkipConstant bool
}
// ----------------------------------------------------------------------------
type importUsed bool
type File struct {
decls []ast.Decl
fname string
imps map[string]*ast.Ident // importPath => impRef (nil means force-import)
dirty bool
}
func newFile(fname string) *File {
return &File{fname: fname, imps: make(map[string]*ast.Ident)}
}
func (p *File) newImport(name, pkgPath string) *ast.Ident {
id := p.imps[pkgPath]
if id == nil {
id = &ast.Ident{Name: name, Obj: &ast.Object{Data: importUsed(false)}}
p.imps[pkgPath] = id
p.dirty = true
}
return id
}
func (p *File) forceImport(pkgPath string) {
if _, ok := p.imps[pkgPath]; !ok {
p.imps[pkgPath] = nil
p.dirty = true
}
}
func (p *File) markUsed(this *Package) {
if p.dirty {
astVisitor{this}.markUsed(p.decls)
p.dirty = false
}
}
// Name returns the name of this file.
func (p *File) Name() string {
return p.fname
}
type astVisitor struct {
this *Package
}
func (p astVisitor) Visit(node ast.Node) (w ast.Visitor) {
if node == nil {
return nil
}
switch v := node.(type) {
case *ast.CommentGroup, *ast.Ident, *ast.BasicLit:
case *ast.SelectorExpr:
x := v.X
if id, ok := x.(*ast.Ident); ok && id.Obj != nil {
if used, ok := id.Obj.Data.(importUsed); ok && bool(!used) {
id.Obj.Data = importUsed(true)
if name, renamed := p.this.importName(id.Name); renamed {
id.Name = name
id.Obj.Name = name
}
}
} else {
ast.Walk(p, x)
}
case *ast.FuncDecl:
ast.Walk(p, v.Type)
if v.Body != nil {
ast.Walk(p, v.Body)
}
case *ast.ValueSpec:
if v.Type != nil {
ast.Walk(p, v.Type)
}
for _, val := range v.Values {
ast.Walk(p, val)
}
case *ast.TypeSpec:
ast.Walk(p, v.Type)
case *ast.BranchStmt:
case *ast.LabeledStmt:
ast.Walk(p, v.Stmt)
default:
return p
}
return nil
}
func (p astVisitor) markUsed(decls []ast.Decl) {
for _, decl := range decls {
ast.Walk(p, decl)
}
}
func isPkgInMod(pkgPath, modPath string) bool {
if strings.HasPrefix(pkgPath, modPath) {
suffix := pkgPath[len(modPath):]
return suffix == "" || suffix[0] == '/'
}
return false
}
const (
FlagDepModGop = 1 << iota // depends module github.com/goplus/gop
FlagDepModX // depends module github.com/qiniu/x
)
// CheckGopDeps checks dependencies of Go+ modules.
// The return flags can be FlagDepModGop and FlagDepModX.
func (p *File) CheckGopDeps(this *Package) (flags int) {
p.markUsed(this)
for pkgPath, id := range p.imps {
if id == nil || id.Obj.Data.(importUsed) {
if isPkgInMod(pkgPath, "github.com/goplus/gop") {
flags |= FlagDepModGop
} else if isPkgInMod(pkgPath, "github.com/qiniu/x") {
flags |= FlagDepModX
}
}
}
return
}
func (p *File) getDecls(this *Package) (decls []ast.Decl) {
p.markUsed(this)
specs := make([]ast.Spec, 0, len(p.imps))
for pkgPath, id := range p.imps {
if id == nil { // force-used
specs = append(specs, &ast.ImportSpec{
Name: underscore, // _
Path: stringLit(pkgPath),
})
} else if id.Obj.Data.(importUsed) {
var name *ast.Ident
if id.Obj.Name != "" {
name = ident(id.Obj.Name)
}
specs = append(specs, &ast.ImportSpec{
Name: name,
Path: stringLit(pkgPath),
})
}
}
sort.Slice(specs, func(i, j int) bool {
return specs[i].(*ast.ImportSpec).Path.Value < specs[j].(*ast.ImportSpec).Path.Value
})
var valGopPkg ast.Expr
var addGopPkg bool
if p.fname == this.conf.DefaultGoFile {
valGopPkg, addGopPkg = checkGopPkg(this)
}
if len(specs) == 0 && !addGopPkg {
return p.decls
}
decls = make([]ast.Decl, 0, len(p.decls)+2)
decls = append(decls, &ast.GenDecl{Tok: token.IMPORT, Specs: specs})
if addGopPkg {
decls = append(decls, &ast.GenDecl{Tok: token.CONST, Specs: []ast.Spec{
&ast.ValueSpec{
Names: []*ast.Ident{{Name: gopPackage}},
Values: []ast.Expr{
valGopPkg,
},
},
}})
}
return append(decls, p.decls...)
}
// ----------------------------------------------------------------------------
// ObjectDocs maps an object to its document.
type ObjectDocs = map[types.Object]*ast.CommentGroup
// Package type
type Package struct {
PkgRef
Docs ObjectDocs
Fset *token.FileSet
unitMgr
autoNames
cb CodeBuilder
imp types.Importer
files map[string]*File
file *File
conf *Config
unsafe_ PkgRef
builtin PkgRef
pkgBig PkgRef
utBigInt *types.Named
utBigRat *types.Named
utBigFlt *types.Named
commentedStmts map[ast.Stmt]*ast.CommentGroup
implicitCast func(pkg *Package, V, T types.Type, pv *Element) bool
expObjTypes []types.Type // types of export objects
isGopPkg bool
allowRedecl bool // for c2go
}
const (
goxPrefix = "Gop_"
)
// NewPackage creates a new package.
func NewPackage(pkgPath, name string, conf *Config) *Package {
if conf == nil {
conf = new(Config)
}
fset := conf.Fset
if fset == nil {
fset = token.NewFileSet()
}
imp := conf.Importer
if imp == nil {
imp = packages.NewImporter(fset)
}
newBuiltin := conf.NewBuiltin
if newBuiltin == nil {
newBuiltin = newBuiltinDefault
}
fname := conf.DefaultGoFile
file := newFile(fname)
files := map[string]*File{fname: file}
pkg := &Package{
Fset: fset,
file: file,
files: files,
conf: conf,
}
pkg.unitMgr.init()
pkg.autoNames.init()
pkg.imp = imp
pkg.Types = conf.Types
if pkg.Types == nil {
pkg.Types = types.NewPackage(pkgPath, name)
}
pkg.builtin.Types = newBuiltin(pkg, conf)
pkg.implicitCast = conf.CanImplicitCast
pkg.utBigInt = conf.UntypedBigInt
pkg.utBigRat = conf.UntypedBigRat
pkg.utBigFlt = conf.UntypedBigFloat
pkg.cb.init(pkg)
return pkg
}
func (p *Package) setDoc(o types.Object, doc *ast.CommentGroup) {
if p.Docs == nil {
p.Docs = make(ObjectDocs)
}
p.Docs[o] = doc
}
func (p *Package) setStmtComments(stmt ast.Stmt, comments *ast.CommentGroup) {
if p.commentedStmts == nil {
p.commentedStmts = make(map[ast.Stmt]*ast.CommentGroup)
}
p.commentedStmts[stmt] = comments
}
// SetRedeclarable sets to allow redeclaration of variables/functions or not.
func (p *Package) SetRedeclarable(allowRedecl bool) {
p.allowRedecl = allowRedecl
}
// Sizeof returns sizeof typ in bytes.
func (p *Package) Sizeof(typ types.Type) int64 {
return align(std.Sizeof(typ), std.Alignof(typ))
}
// align returns the smallest y >= x such that y % a == 0.
func align(x, a int64) int64 {
y := x + a - 1
return y - y%a
}
func (p *Package) Offsetsof(fields []*types.Var) []int64 {
return std.Offsetsof(fields)
}
// Builtin returns the buitlin package.
func (p *Package) Builtin() PkgRef {
return p.builtin
}
// Builtin returns the unsafe package.
func (p *Package) Unsafe() PkgRef {
return p.unsafe_
}
// CB returns the code builder.
func (p *Package) CB() *CodeBuilder {
return &p.cb
}
// SetCurFile sets new current file to write.
// If createIfNotExists is true, then create a new file named `fname` if it not exists.
// It returns an `old` file to restore in the future (by calling `RestoreCurFile`).
func (p *Package) SetCurFile(fname string, createIfNotExists bool) (old *File, err error) {
old = p.file
f, ok := p.files[fname]
if !ok {
if createIfNotExists {
f = newFile(fname)
p.files[fname] = f
} else {
return nil, syscall.ENOENT
}
}
p.file = f
return
}
// CurFile returns the current file.
func (p *Package) CurFile() *File {
return p.file
}
// RestoreCurFile sets current file to an `old` file that was returned by `SetCurFile`.
func (p *Package) RestoreCurFile(file *File) (old *File) {
old = p.file
p.file = file
return
}
// File returns a file by its name.
// If `fname` is not provided, it returns the default (NOT current) file.
func (p *Package) File(fname ...string) (file *File, ok bool) {
var name string
if len(fname) == 1 {
name = fname[0]
} else {
name = p.conf.DefaultGoFile
}
file, ok = p.files[name]
return
}
// ForEachFile walks all files to `doSth`.
func (p *Package) ForEachFile(doSth func(fname string, file *File)) {
for fname, file := range p.files {
doSth(fname, file)
}
}
// ----------------------------------------------------------------------------