forked from goplus/gogen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage.go
475 lines (416 loc) · 11.8 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
/*
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 gox
import (
"go/ast"
"go/token"
"go/types"
"log"
"path"
"reflect"
"strconv"
"strings"
"syscall"
"github.com/goplus/gox/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 gox event recorder.
type Recorder interface {
// Member maps identifiers to the objects they denote.
Member(id 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
// Context represents all things between packages (optional).
Context *Context
// 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
// NoSkipConstant is to disable optimization of skipping constant (optional).
NoSkipConstant bool
// (internal) only for testing
DbgPositioner dbgPositioner
}
// ----------------------------------------------------------------------------
type File struct {
decls []ast.Decl
allPkgPaths []string
importPkgs map[string]*PkgRef
pkgBig *PkgRef
pkgUnsafe *PkgRef
fname string
removedExprs bool
defaultFile bool
}
// Name returns the name of this file.
func (p *File) Name() string {
return p.fname
}
func (p *File) importPkg(this *Package, pkgPath string, src ast.Node) *PkgRef {
if strings.HasPrefix(pkgPath, ".") { // canonical pkgPath
pkgPath = path.Join(this.Path(), pkgPath)
}
pkgImport, ok := p.importPkgs[pkgPath]
if !ok {
pkgImp, err := this.imp.Import(pkgPath)
if err != nil {
e := &ImportError{Path: pkgPath, Err: err}
if src != nil {
e.Fset = this.cb.fset
e.Pos = src.Pos()
}
panic(e)
} else {
this.ctx.InitGopPkg(this.imp, pkgImp)
}
pkgImport = &PkgRef{Types: pkgImp}
p.importPkgs[pkgPath] = pkgImport
p.allPkgPaths = append(p.allPkgPaths, pkgPath)
}
return pkgImport
}
func (p *File) markUsed(this *Package) {
if p.removedExprs {
// travel all ast nodes to mark used
p.markUsedBy(this, reflect.ValueOf(p.decls))
return
}
// no removed exprs, mark used simplely
for _, pkgImport := range p.importPkgs {
if pkgImport.nameRefs != nil {
pkgImport.isUsed = true
}
}
}
var (
tyAstNode = reflect.TypeOf((*ast.Node)(nil)).Elem()
tySelExprPtr = reflect.TypeOf((*ast.SelectorExpr)(nil))
)
func (p *File) markUsedBy(this *Package, val reflect.Value) {
retry:
switch val.Kind() {
case reflect.Slice:
for i, n := 0, val.Len(); i < n; i++ {
p.markUsedBy(this, val.Index(i))
}
case reflect.Ptr:
if val.IsNil() {
return
}
t := val.Type()
if t == tySelExprPtr {
x := val.Interface().(*ast.SelectorExpr).X
if sym, ok := x.(*ast.Ident); ok {
name := sym.Name
for _, at := range p.importPkgs {
if at.Types != nil && at.Types.Name() == name { // pkg.Object
at.markUsed(sym)
}
}
} else {
p.markUsedBy(this, reflect.ValueOf(x))
}
} else if t.Implements(tyAstNode) { // ast.Node
elem := val.Elem()
for i, n := 0, elem.NumField(); i < n; i++ {
p.markUsedBy(this, elem.Field(i))
}
}
case reflect.Interface:
val = val.Elem()
goto retry
}
}
func (p *File) getDecls(this *Package) (decls []ast.Decl) {
p.markUsed(this)
n := len(p.allPkgPaths)
if n == 0 {
return p.decls
}
specs := make([]ast.Spec, 0, n)
names := this.newAutoNames()
for _, pkgPath := range p.allPkgPaths {
pkgImport := p.importPkgs[pkgPath]
if !pkgImport.isUsed { // unused
if pkgImport.isForceUsed { // force-used
specs = append(specs, &ast.ImportSpec{
Name: underscore, // _
Path: &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(pkgPath)},
})
}
continue
}
pkgName, renamed := names.RequireName(pkgImport.Types.Name())
var name *ast.Ident
if renamed {
for _, nameRef := range pkgImport.nameRefs {
nameRef.Name = pkgName
}
name = ident(pkgName)
} else {
if pkgName != path.Base(pkgImport.Types.Path()) {
name = ident(pkgName)
}
}
specs = append(specs, &ast.ImportSpec{
Name: name,
Path: &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(pkgPath)},
})
}
addGopPkg := p.defaultFile && shouldAddGopPkg(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{
&ast.Ident{Name: "true"},
},
},
}})
}
decls = append(decls, p.decls...)
return
}
func (p *File) big(this *Package) *PkgRef {
if p.pkgBig == nil {
p.pkgBig = p.importPkg(this, "math/big", nil)
}
return p.pkgBig
}
func (p *File) unsafe(this *Package) *PkgRef {
if p.pkgUnsafe == nil {
p.pkgUnsafe = p.importPkg(this, "unsafe", nil)
}
return p.pkgUnsafe
}
// ----------------------------------------------------------------------------
// 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
cb CodeBuilder
imp types.Importer
files map[string]*File
file *File
conf *Config
ctx *Context
builtin *types.Package
utBigInt *types.Named
utBigRat *types.Named
utBigFlt *types.Named
autoIdx int
commentedStmts map[ast.Stmt]*ast.CommentGroup
implicitCast func(pkg *Package, V, T types.Type, pv *Element) bool
allowRedecl bool // for c2go
isGopPkg bool
}
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()
}
ctx := conf.Context
if ctx == nil {
ctx = NewContext()
}
imp := conf.Importer
if imp == nil {
imp = packages.NewImporter(fset)
}
newBuiltin := conf.NewBuiltin
if newBuiltin == nil {
newBuiltin = newBuiltinDefault
}
fname := conf.DefaultGoFile
file := &File{importPkgs: make(map[string]*PkgRef), fname: fname, defaultFile: true}
files := map[string]*File{fname: file}
pkg := &Package{
Fset: fset,
file: file,
files: files,
conf: conf,
ctx: ctx,
}
pkg.imp = imp
pkg.Types = conf.Types
if pkg.Types == nil {
pkg.Types = types.NewPackage(pkgPath, name)
}
pkg.builtin = 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 &PkgRef{Types: p.builtin}
}
// 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 = &File{importPkgs: make(map[string]*PkgRef), fname: 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) {
p.file = file
}
// 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)
}
}
// ----------------------------------------------------------------------------