-
Notifications
You must be signed in to change notification settings - Fork 18
/
checker_scopes.go
520 lines (468 loc) · 16.9 KB
/
checker_scopes.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// Copyright 2021 The Scriggo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package compiler
import (
"reflect"
"github.com/open2b/scriggo/ast"
)
// scopes represents the universe block, global block, file/package block and
// function scopes.
//
// 0, 1 are the universe block. 1 is for the format types.
// 2 is the global block, empty for programs.
// 3 is the file/package block.
// 4+ are the scopes in functions. For templates, 4 is the main block.
type scopes struct {
s []scope
path string
allowUnused bool
}
// scope is a scope.
type scope struct {
// fn is function, with the related labels, where the scope is located.
fn scopeFunc
// Position of the block of the scope.
block *ast.Position
// names are the declared names in the scope.
names map[string]scopeName
}
// scopeFunc represents a function scope. Only scopes 4 onwards have a function scope.
type scopeFunc struct {
// node is the node of the function.
// It is nil in scopes 0, 1, 2 and 3.
node *ast.Func
// labels are the labels, declared or only used, in the scope of the function.
// It is nil if there are no labels, and it is always nil in scopes 0, 1, 2 and 3.
labels map[string]scopeLabel
}
// scopeName is a scope name.
type scopeName struct {
// ti is the type info.
ti *typeInfo
// decl is the identifier in the declaration node.
// It is nil for native identifiers (scopes 0, 1 and 2), packages and
// native imported names (scope 3).
decl *ast.Identifier
// impor is the import declaration of a package or imported name (scope 3).
// It is nil for not-imported names
impor *ast.Import
// used indicates if it has been used.
used bool
}
// scopeLabel is a label in a function scope.
type scopeLabel struct {
// Position of the block in which the label is declared.
block *ast.Position
// node is the node of the label.
node *ast.Label
// used indicates if it has been used.
used bool
// goto statements referring the label.
gotos []scopeGoto
}
// scopeGoto represents a goto statement referring a label that has not yet
// been declared.
type scopeGoto struct {
// pos is the position of the label identifier in the goto statement.
pos *ast.Position
// blocks are the positions of the goto blocks (with scope >= 4).
blocks []*ast.Position
}
// newScopes returns a new scopes given the format types and the global block.
func newScopes(path string, formats map[ast.Format]reflect.Type, global map[string]scopeName) *scopes {
// TODO(marco): change type: fn(string, map[string]*typeInfo, global map[string]*typeInfo)
var formatScope scope
if len(formats) > 0 {
formatScope = scope{names: map[string]scopeName{}}
for f, t := range formats {
name := formatTypeName[f]
formatScope.names[name] = scopeName{ti: &typeInfo{
Type: t,
Properties: propertyIsType | propertyIsFormatType | propertyUniverse,
}}
}
}
return &scopes{
path: path,
s: []scope{{names: universe}, formatScope, {names: global}, {names: map[string]scopeName{}}},
}
}
// AllowUnused does not check unused variables and labels.
func (scopes *scopes) AllowUnused() {
scopes.allowUnused = true
}
// Universe returns the type info of name as declared in the universe block
// and true. Otherwise it returns nil and false.
func (scopes *scopes) Universe(name string) (*typeInfo, bool) {
n, ok := universe[name]
if ok {
return n.ti, true
}
n, ok = scopes.s[1].names[name]
return n.ti, ok
}
// Global returns the type info of name as declared in the global block and
// true. Otherwise it returns nil and false.
func (scopes *scopes) Global(name string) (*typeInfo, bool) {
n, ok := scopes.s[2].names[name]
return n.ti, ok
}
// FilePackage returns the type info of name as declared in the file/package
// block and true. Otherwise it returns nil and false.
func (scopes *scopes) FilePackage(name string) (*typeInfo, bool) {
n, ok := scopes.s[3].names[name]
return n.ti, ok
}
// Current returns the identifier of name as declared in the current scope and
// true. Otherwise it returns nil and false.
func (scopes *scopes) Current(name string) (*ast.Identifier, bool) {
n, ok := scopes.s[len(scopes.s)-1].names[name]
return n.decl, ok
}
// ExportedDeclarations returns the exported declarations in the file/package
// block, as a name/type info map, that have not been imported from another
// package.
func (scopes *scopes) ExportedDeclarations() map[string]*typeInfo {
decls := map[string]*typeInfo{}
for name, n := range scopes.s[3].names {
if n.impor == nil && isExported(name) {
decls[name] = n.ti
}
}
return decls
}
// ExportedDeclarationNodes returns the exported declarations nodes in the
// file/package block, as a name/declaration's identifier map, that have not
// been imported from another package.
func (scopes *scopes) ExportedDeclarationNodes() map[string]*ast.Identifier {
decls := map[string]*ast.Identifier{}
for name, n := range scopes.s[3].names {
if n.impor == nil && isExported(name) {
decls[name] = n.decl
}
}
return decls
}
// Declare declares name with its type info and declaration node and returns
// true. If name is already declared in the current scope, it does nothing and
// returns false.
// decl is the identifier node that declared name, or nil if native.
// impor is the node of the import declaration that imported name, is nil if
// not imported.
func (scopes *scopes) Declare(name string, ti *typeInfo, decl *ast.Identifier, impor *ast.Import) bool {
c := len(scopes.s) - 1
n := scopeName{ti: ti}
n.decl = decl
n.impor = impor
if names := scopes.s[c].names; names == nil {
scopes.s[c].names = map[string]scopeName{name: n}
} else if _, ok := names[name]; ok {
return false
} else {
names[name] = n
}
return true
}
// DeclareLabel declares a label.
func (scopes *scopes) DeclareLabel(label *ast.Label) {
c := len(scopes.s) - 1
current := scopes.s[c]
name := label.Ident.Name
lbl, used := current.fn.labels[name]
lbl.block = current.block
lbl.node = label
current.fn.labels[name] = lbl
if !used {
return
}
for _, g := range lbl.gotos {
if i := c - 4; len(g.blocks) <= i || g.blocks[i] != current.block {
panic(checkError(scopes.path, g.pos, "goto %s jumps into block starting at %s:%s",
name, scopes.path, current.block))
}
var v *ast.Identifier
s := g.pos.Start
for _, n := range current.names {
if n.ti.Addressable() && n.decl.Position.Start > s {
v = n.decl
s = n.decl.Position.Start
}
}
if v != nil {
panic(checkError(scopes.path, g.pos, "goto %s jumps over declaration of %s at %s:%s",
name, v.Name, scopes.path, v.Position))
}
}
return
}
// Lookup lookups name in all scopes, and returns its type info, its
// node and true. Otherwise, it returns nil, nil and false.
//
// node is an *ast.Import value for packages and imported names, otherwise nil
// for predeclared names, otherwise an *ast.Identifier value for all other
// names.
func (scopes *scopes) Lookup(name string) (*typeInfo, ast.Node, bool) {
n, i := scopes.lookup(name, 0)
var node ast.Node = n.decl
if n.decl == nil {
node = n.impor
}
return n.ti, node, i != -1
}
// LookupImport returns the import declaration that imported name and true, if
// name has been imported, otherwise returns nil and false.
func (scopes *scopes) LookupImport(name string) (*ast.Import, bool) {
n, _ := scopes.lookup(name, 3)
if n.impor == nil {
return nil, false
}
return n.impor, true
}
// LookupInFunc lookups name in function scopes and returns its type info, its
// identifier and true. Otherwise, it returns nil, nil and false.
func (scopes *scopes) LookupInFunc(name string) (*typeInfo, *ast.Identifier, bool) {
n, i := scopes.lookup(name, 4)
return n.ti, n.decl, i != -1
}
// LookupLabel lookups a label in the current function scope and returns the
// label node and true if it is declared. Otherwise it returns nil and false.
func (scopes *scopes) LookupLabel(name string) (*ast.Label, bool) {
c := len(scopes.s) - 1
lbl := scopes.s[c].fn.labels[name]
if lbl.node == nil {
return nil, false
}
return lbl.node, true
}
// lookup lookups name and returns its scope name and scope index in which it
// is defined. Otherwise it returns the zero value of scopeName and -1.
// start is the index of the scope from which to start the lookup.
func (scopes *scopes) lookup(name string, start int) (scopeName, int) {
for i := len(scopes.s) - 1; i >= start; i-- {
if n, ok := scopes.s[i].names[name]; ok {
return n, i
}
}
return scopeName{}, -1
}
// Use sets name as used and returns a boolean indicating whether it was
// already set as used.
func (scopes *scopes) Use(name string) bool {
n, i := scopes.lookup(name, 3)
if i != -1 && !n.used {
n.used = true
scopes.s[i].names[name] = n
return false
}
return true
}
// UseLabel sets the label ident as used. stmt is "goto", "break" or
// "continue". Returns the label node or nil if ident is nil.
func (scopes *scopes) UseLabel(stmt string, ident *ast.Identifier) *ast.Label {
if ident == nil {
return nil
}
c := len(scopes.s) - 1
current := scopes.s[c]
name := ident.Name
lbl := current.fn.labels[name]
defer func() {
current.fn.labels[name] = lbl
}()
lbl.used = true
if stmt == "goto" {
if lbl.node == nil {
blocks := make([]*ast.Position, len(scopes.s)-4)
for i, n := range scopes.s[4:] {
blocks[i] = n.block
}
lbl.gotos = append(lbl.gotos, scopeGoto{pos: ident.Position, blocks: blocks})
return nil
}
for i := c; i >= 4; i-- {
if scopes.s[i].block == lbl.block {
return lbl.node
}
}
panic(checkError(scopes.path, lbl.block, "goto %s jumps into block starting at :%s", name, lbl.block))
}
if lbl.node == nil {
panic(checkError(scopes.path, ident, "%s label not defined: %s", stmt, name))
}
return lbl.node
}
// UnusedImport returns the declaration of the first unused import, by
// position in the source. If all imports are used, it returns nil.
func (scopes *scopes) UnusedImport() *ast.Import {
unused := map[*ast.Import]bool{}
for _, n := range scopes.s[3].names {
if n.impor == nil {
continue
}
if n.ti.IsPackage() {
if !n.used {
unused[n.impor] = true
}
continue
}
if _, ok := unused[n.impor]; !ok || n.used {
unused[n.impor] = !n.used
}
}
var node *ast.Import
for im, ok := range unused {
if ok && (node == nil || im.Position.Start < node.Start) {
node = im
}
}
return node
}
// CurrentFunction returns the function of the current scope or nil if there
// is no function.
func (scopes *scopes) CurrentFunction() *ast.Func {
c := len(scopes.s) - 1
return scopes.s[c].fn.node
}
// Function returns the function in which name is declared. If name is not
// declared in a function, it returns nil.
func (scopes *scopes) Function(name string) *ast.Func {
if _, i := scopes.lookup(name, 4); i != -1 {
return scopes.s[i].fn.node
}
return nil
}
// Functions returns all the functions up to the function of the current
// scope. If there is no function, it returns nil.
func (scopes *scopes) Functions() []*ast.Func {
n := 0
for i, sc := range scopes.s {
if sc.fn.node != nil && sc.fn.node != scopes.s[i-1].fn.node {
n++
}
}
if n == 0 {
return nil
}
functions := make([]*ast.Func, 0, n)
for i, sc := range scopes.s {
if sc.fn.node != nil && sc.fn.node != scopes.s[i-1].fn.node {
functions = append(functions, sc.fn.node)
}
}
return functions
}
// Enter enters a new scope. block is the block of the scope.
func (scopes *scopes) Enter(block ast.Node) {
s := scope{block: block.Pos()}
if node, ok := block.(*ast.Func); ok {
s.fn.node = node
} else {
c := len(scopes.s) - 1
s.fn = scopes.s[c].fn
}
if s.fn.labels == nil {
s.fn.labels = map[string]scopeLabel{}
}
scopes.s = append(scopes.s, s)
}
// isFuncBlock reports whether the current scope if a function block scope.
func (scopes *scopes) isFuncBlock() bool {
c := len(scopes.s) - 1
return c == 4 || c > 4 && scopes.s[c].fn.node != scopes.s[c-1].fn.node
}
// Exit exits the current scope.
func (scopes *scopes) Exit() {
c := len(scopes.s) - 1
// Check for goto statements referring non-defined labels.
if scopes.isFuncBlock() {
for name, lbl := range scopes.s[c].fn.labels {
if lbl.node == nil {
panic(checkError(scopes.path, lbl.gotos[0].pos, "label %s not defined", name))
}
}
}
if !scopes.allowUnused {
// Check for unused labels.
if scopes.isFuncBlock() {
var label *ast.Label
for _, lbl := range scopes.s[c].fn.labels {
if !lbl.used && (label == nil || lbl.node.Position.Start < label.Start) {
label = lbl.node
}
}
if label != nil {
panic(checkError(scopes.path, label, "label %s defined and not used", label.Ident))
}
}
// Check for unused variables.
var ident *ast.Identifier
for _, n := range scopes.s[c].names {
if n.used || n.ti.IsConstant() || n.ti.IsType() {
continue
}
if ident == nil || n.decl.Position.Start < ident.Start {
ident = n.decl
}
}
if ident != nil {
panic(checkError(scopes.path, ident, "%s declared but not used", ident))
}
}
// Exit the scope.
last := len(scopes.s) - 1
if last == 3 {
panic("scopes: no scope to exit of")
}
scopes.s = scopes.s[:last]
return
}
var boolType = reflect.TypeOf(false)
var uintType = reflect.TypeOf(uint(0))
var uint8Type = reflect.TypeOf(uint8(0))
var int32Type = reflect.TypeOf(int32(0))
var errorType = reflect.TypeOf((*error)(nil)).Elem()
// universe is the universe scope.
var universe = map[string]scopeName{
"append": {ti: &typeInfo{Properties: propertyUniverse}},
"cap": {ti: &typeInfo{Properties: propertyUniverse}},
"close": {ti: &typeInfo{Properties: propertyUniverse}},
"complex": {ti: &typeInfo{Properties: propertyUniverse}},
"copy": {ti: &typeInfo{Properties: propertyUniverse}},
"delete": {ti: &typeInfo{Properties: propertyUniverse}},
"imag": {ti: &typeInfo{Properties: propertyUniverse}},
"iota": {ti: &typeInfo{Properties: propertyUniverse, Type: intType}},
"itea": {ti: &typeInfo{Properties: propertyUniverse | propertyUntyped | propertyAddressable}},
"len": {ti: &typeInfo{Properties: propertyUniverse}},
"make": {ti: &typeInfo{Properties: propertyUniverse}},
"new": {ti: &typeInfo{Properties: propertyUniverse}},
"nil": {ti: &typeInfo{Properties: propertyUntyped | propertyUniverse}},
"panic": {ti: &typeInfo{Properties: propertyUniverse}},
"print": {ti: &typeInfo{Properties: propertyUniverse}},
"println": {ti: &typeInfo{Properties: propertyUniverse}},
"real": {ti: &typeInfo{Properties: propertyUniverse}},
"recover": {ti: &typeInfo{Properties: propertyUniverse}},
"any": {ti: &typeInfo{Type: emptyInterfaceType, Alias: "any", Properties: propertyIsType | propertyUniverse}},
"byte": {ti: &typeInfo{Type: uint8Type, Alias: "byte", Properties: propertyIsType | propertyUniverse}},
"bool": {ti: &typeInfo{Type: boolType, Properties: propertyIsType | propertyUniverse}},
"complex128": {ti: &typeInfo{Type: complex128Type, Properties: propertyIsType | propertyUniverse}},
"complex64": {ti: &typeInfo{Type: complex64Type, Properties: propertyIsType | propertyUniverse}},
"error": {ti: &typeInfo{Type: errorType, Properties: propertyIsType | propertyUniverse}},
"float32": {ti: &typeInfo{Type: reflect.TypeOf(float32(0)), Properties: propertyIsType | propertyUniverse}},
"float64": {ti: &typeInfo{Type: float64Type, Properties: propertyIsType | propertyUniverse}},
"false": {ti: &typeInfo{Type: boolType, Properties: propertyUniverse | propertyUntyped, Constant: boolConst(false)}},
"int": {ti: &typeInfo{Type: intType, Properties: propertyIsType | propertyUniverse}},
"int16": {ti: &typeInfo{Type: reflect.TypeOf(int16(0)), Properties: propertyIsType | propertyUniverse}},
"int32": {ti: &typeInfo{Type: int32Type, Properties: propertyIsType | propertyUniverse}},
"int64": {ti: &typeInfo{Type: reflect.TypeOf(int64(0)), Properties: propertyIsType | propertyUniverse}},
"int8": {ti: &typeInfo{Type: reflect.TypeOf(int8(0)), Properties: propertyIsType | propertyUniverse}},
"rune": {ti: &typeInfo{Type: int32Type, Alias: "rune", Properties: propertyIsType | propertyUniverse}},
"string": {ti: &typeInfo{Type: stringType, Properties: propertyIsType | propertyIsFormatType | propertyUniverse}},
"true": {ti: &typeInfo{Type: boolType, Properties: propertyUniverse | propertyUntyped, Constant: boolConst(true)}},
"uint": {ti: &typeInfo{Type: uintType, Properties: propertyIsType | propertyUniverse}},
"uint16": {ti: &typeInfo{Type: reflect.TypeOf(uint16(0)), Properties: propertyIsType | propertyUniverse}},
"uint32": {ti: &typeInfo{Type: reflect.TypeOf(uint32(0)), Properties: propertyIsType | propertyUniverse}},
"uint64": {ti: &typeInfo{Type: reflect.TypeOf(uint64(0)), Properties: propertyIsType | propertyUniverse}},
"uint8": {ti: &typeInfo{Type: uint8Type, Properties: propertyIsType | propertyUniverse}},
"uintptr": {ti: &typeInfo{Type: reflect.TypeOf(uintptr(0)), Properties: propertyIsType | propertyUniverse}},
}