@@ -25,37 +25,37 @@ type Scope uint8
25
25
26
26
// Scope definitions
27
27
const (
28
- scopeInvalid Scope = iota
29
- scopeLocal
30
- scopeGlobalExplicit
31
- scopeGlobalImplicit
32
- scopeFree
33
- scopeCell
28
+ ScopeInvalid Scope = iota
29
+ ScopeLocal
30
+ ScopeGlobalExplicit
31
+ ScopeGlobalImplicit
32
+ ScopeFree
33
+ ScopeCell
34
34
)
35
35
36
36
// Accumulate Scopes
37
37
type Scopes map [string ]Scope
38
38
39
39
// Def-use flag information
40
- type DefUse uint8
40
+ type DefUseFlags uint8
41
41
42
42
// Flags for def-use information
43
43
const (
44
- defGlobal DefUse = 1 << iota // global stmt
45
- defLocal // assignment in code block
46
- defParam // formal parameter
47
- defNonlocal // nonlocal stmt
48
- defUse // name is used
49
- defFree // name used but not defined in nested block
50
- defFreeClass // free variable from class's method
51
- defImport // assignment occurred via import
52
-
53
- defBound = (defLocal | defParam | defImport )
54
-
55
- // scopeGlobalExplicit and scopeGlobalImplicit are used internally by the symbol
44
+ DefGlobal DefUseFlags = 1 << iota // global stmt
45
+ DefLocal // assignment in code block
46
+ DefParam // formal parameter
47
+ DefNonlocal // nonlocal stmt
48
+ DefUse // name is used
49
+ DefFree // name used but not defined in nested block
50
+ DefFreeClass // free variable from class's method
51
+ DefImport // assignment occurred via import
52
+
53
+ DefBound = (DefLocal | DefParam | DefImport )
54
+
55
+ // ScopeGlobalExplicit and ScopeGlobalImplicit are used internally by the symbol
56
56
// table. GLOBAL is returned from PyST_GetScope() for either of them.
57
57
// It is stored in ste_symbols at bits 12-15.
58
- defScopeMask = (defGlobal | defLocal | defParam | defNonlocal )
58
+ DefScopeMask = (DefGlobal | DefLocal | DefParam | DefNonlocal )
59
59
)
60
60
61
61
// BlockType for SymTable
@@ -80,7 +80,7 @@ const (
80
80
// Info about a symbol
81
81
type Symbol struct {
82
82
Scope Scope
83
- Flags DefUse
83
+ Flags DefUseFlags
84
84
}
85
85
86
86
type Symbols map [string ]Symbol
@@ -163,17 +163,17 @@ func (st *SymTable) addArgumentsToSymbolTable(node *ast.Arguments) {
163
163
// skip default arguments inside function block
164
164
// XXX should ast be different?
165
165
for _ , arg := range node .Args {
166
- st .AddDef (arg .Arg , defParam )
166
+ st .AddDef (arg .Arg , DefParam )
167
167
}
168
168
for _ , arg := range node .Kwonlyargs {
169
- st .AddDef (arg .Arg , defParam )
169
+ st .AddDef (arg .Arg , DefParam )
170
170
}
171
171
if node .Vararg != nil {
172
- st .AddDef (node .Vararg .Arg , defParam )
172
+ st .AddDef (node .Vararg .Arg , DefParam )
173
173
st .Varargs = true
174
174
}
175
175
if node .Kwarg != nil {
176
- st .AddDef (node .Kwarg .Arg , defParam )
176
+ st .AddDef (node .Kwarg .Arg , DefParam )
177
177
st .Varkeywords = true
178
178
}
179
179
}
@@ -186,47 +186,47 @@ func (st *SymTable) Parse(Ast ast.Ast) {
186
186
for _ , name := range node .Names {
187
187
cur , ok := st .Symbols [string (name )]
188
188
if ok {
189
- if (cur .Flags & defLocal ) != 0 {
189
+ if (cur .Flags & DefLocal ) != 0 {
190
190
// FIXME this should be a warning
191
191
log .Printf ("name '%s' is assigned to before nonlocal declaration" , name )
192
192
193
193
}
194
- if (cur .Flags & defUse ) != 0 {
194
+ if (cur .Flags & DefUse ) != 0 {
195
195
// FIXME this should be a warning
196
196
log .Printf ("name '%s' is used prior to nonlocal declaration" , name )
197
197
}
198
198
}
199
- st .AddDef (name , defNonlocal )
199
+ st .AddDef (name , DefNonlocal )
200
200
}
201
201
case * ast.Global :
202
202
for _ , name := range node .Names {
203
203
cur , ok := st .Symbols [string (name )]
204
204
if ok {
205
- if (cur .Flags & defLocal ) != 0 {
205
+ if (cur .Flags & DefLocal ) != 0 {
206
206
// FIXME this should be a warning
207
207
log .Printf ("name '%s' is assigned to before global declaration" , name )
208
208
209
209
}
210
- if (cur .Flags & defUse ) != 0 {
210
+ if (cur .Flags & DefUse ) != 0 {
211
211
// FIXME this should be a warning
212
212
log .Printf ("name '%s' is used prior to global declaration" , name )
213
213
}
214
214
}
215
- st .AddDef (name , defGlobal )
215
+ st .AddDef (name , DefGlobal )
216
216
}
217
217
case * ast.Name :
218
218
if node .Ctx == ast .Load {
219
- st .AddDef (node .Id , defUse )
219
+ st .AddDef (node .Id , DefUse )
220
220
} else {
221
- st .AddDef (node .Id , defLocal )
221
+ st .AddDef (node .Id , DefLocal )
222
222
}
223
223
// Special-case super: it counts as a use of __class__
224
224
if node .Ctx == ast .Load && st .Type == FunctionBlock && node .Id == "super" {
225
- st .AddDef (ast .Identifier ("__class__" ), defUse )
225
+ st .AddDef (ast .Identifier ("__class__" ), DefUse )
226
226
}
227
227
case * ast.FunctionDef :
228
228
// Add the function name to the SymTable
229
- st .AddDef (node .Name , defLocal )
229
+ st .AddDef (node .Name , DefLocal )
230
230
name := string (node .Name )
231
231
232
232
// Walk these things in original symbol table
@@ -252,7 +252,7 @@ func (st *SymTable) Parse(Ast ast.Ast) {
252
252
// return false to stop the parse
253
253
return false
254
254
case * ast.ClassDef :
255
- st .AddDef (node .Name , defLocal )
255
+ st .AddDef (node .Name , DefLocal )
256
256
name := string (node .Name )
257
257
// Parse in the original symtable
258
258
for _ , expr := range node .Bases {
@@ -310,7 +310,7 @@ func (st *SymTable) Parse(Ast ast.Ast) {
310
310
return false
311
311
case * ast.ExceptHandler :
312
312
if node .Name != "" {
313
- st .AddDef (node .Name , defLocal )
313
+ st .AddDef (node .Name , DefLocal )
314
314
}
315
315
case * ast.Alias :
316
316
// Compute store_name, the name actually bound by the import
@@ -326,7 +326,7 @@ func (st *SymTable) Parse(Ast ast.Ast) {
326
326
store_name = name [:dot ]
327
327
}
328
328
if name != "*" {
329
- st .AddDef (store_name , defImport )
329
+ st .AddDef (store_name , DefImport )
330
330
} else {
331
331
if st .Type != ModuleBlock {
332
332
panic (py .ExceptionNewf (py .SyntaxError , "import * only allowed at module level" ))
@@ -346,7 +346,7 @@ func (st *SymTable) Parse(Ast ast.Ast) {
346
346
func (st * SymTable ) newTmpName () {
347
347
st .TmpName ++
348
348
id := ast .Identifier (fmt .Sprintf ("_[%d]" , st .TmpName ))
349
- st .AddDef (id , defLocal )
349
+ st .AddDef (id , DefLocal )
350
350
}
351
351
352
352
func (st * SymTable ) parseComprehension (Ast ast.Ast , scopeName string , generators []ast.Comprehension , elt ast.Expr , value ast.Expr ) {
@@ -360,7 +360,7 @@ func (st *SymTable) parseComprehension(Ast ast.Ast, scopeName string, generators
360
360
stNew .Generator = isGenerator
361
361
// Outermost iter is received as an argument
362
362
id := ast .Identifier (fmt .Sprintf (".%d" , 0 ))
363
- stNew .AddDef (id , defParam )
363
+ stNew .AddDef (id , DefParam )
364
364
// Allocate temporary name if needed
365
365
if needsTmp {
366
366
stNew .newTmpName ()
@@ -383,13 +383,13 @@ func (st *SymTable) parseComprehension(Ast ast.Ast, scopeName string, generators
383
383
}
384
384
385
385
// Add a symbol into the symble table
386
- func (st * SymTable ) AddDef (name ast.Identifier , flags DefUse ) {
386
+ func (st * SymTable ) AddDef (name ast.Identifier , flags DefUseFlags ) {
387
387
// FIXME mangled := _Py_Mangle(st.Private, name)
388
388
mangled := string (name )
389
389
390
390
// Add or update the symbol in the Symbols
391
391
if sym , ok := st .Symbols [mangled ]; ok {
392
- if (flags & defParam ) != 0 && (sym .Flags & defParam ) != 0 {
392
+ if (flags & DefParam ) != 0 && (sym .Flags & DefParam ) != 0 {
393
393
// Is it better to use 'mangled' or 'name' here?
394
394
panic (py .ExceptionNewf (py .SyntaxError , "duplicate argument '%s' in function definition" , name ))
395
395
// FIXME
@@ -406,13 +406,13 @@ func (st *SymTable) AddDef(name ast.Identifier, flags DefUse) {
406
406
}
407
407
}
408
408
409
- if (flags & defParam ) != 0 {
409
+ if (flags & DefParam ) != 0 {
410
410
st .Varnames = append (st .Varnames , mangled )
411
- } else if (flags & defGlobal ) != 0 {
411
+ } else if (flags & DefGlobal ) != 0 {
412
412
// If it is a global definition then add it in the global Symbols
413
413
//
414
- // XXX need to update defGlobal for other flags too;
415
- // perhaps only defFreeClass
414
+ // XXX need to update DefGlobal for other flags too;
415
+ // perhaps only DefFreeClass
416
416
if sym , ok := st .Global .Symbols [mangled ]; ok {
417
417
sym .Flags |= flags
418
418
st .Global .Symbols [mangled ] = sym
@@ -515,23 +515,23 @@ func (s StringSet) Contains(k string) bool {
515
515
about the new name. For example, a new global will add an entry to
516
516
global. A name that was global can be changed to local.
517
517
*/
518
- func (st * SymTable ) AnalyzeName (scopes Scopes , name string , flags DefUse , bound , local , free , global StringSet ) {
519
- if (flags & defGlobal ) != 0 {
520
- if (flags & defParam ) != 0 {
518
+ func (st * SymTable ) AnalyzeName (scopes Scopes , name string , flags DefUseFlags , bound , local , free , global StringSet ) {
519
+ if (flags & DefGlobal ) != 0 {
520
+ if (flags & DefParam ) != 0 {
521
521
panic (py .ExceptionNewf (py .SyntaxError , "name '%s' is parameter and global" , name ))
522
522
}
523
- if (flags & defNonlocal ) != 0 {
523
+ if (flags & DefNonlocal ) != 0 {
524
524
panic (py .ExceptionNewf (py .SyntaxError , "name '%s' is nonlocal and global" , name ))
525
525
}
526
- scopes [name ] = scopeGlobalExplicit
526
+ scopes [name ] = ScopeGlobalExplicit
527
527
global .Add (name )
528
528
if bound != nil {
529
529
bound .Discard (name )
530
530
}
531
531
return
532
532
}
533
- if (flags & defNonlocal ) != 0 {
534
- if (flags & defParam ) != 0 {
533
+ if (flags & DefNonlocal ) != 0 {
534
+ if (flags & DefParam ) != 0 {
535
535
panic (py .ExceptionNewf (py .SyntaxError , "name '%s' is parameter and nonlocal" , name ))
536
536
}
537
537
if bound == nil {
@@ -540,13 +540,13 @@ func (st *SymTable) AnalyzeName(scopes Scopes, name string, flags DefUse, bound,
540
540
if ! bound .Contains (name ) {
541
541
panic (py .ExceptionNewf (py .SyntaxError , "no binding for nonlocal '%s' found" , name ))
542
542
}
543
- scopes [name ] = scopeFree
543
+ scopes [name ] = ScopeFree
544
544
st .Free = true
545
545
free .Add (name )
546
546
return
547
547
}
548
- if (flags & defBound ) != 0 {
549
- scopes [name ] = scopeLocal
548
+ if (flags & DefBound ) != 0 {
549
+ scopes [name ] = ScopeLocal
550
550
local .Add (name )
551
551
global .Discard (name )
552
552
return
@@ -557,7 +557,7 @@ func (st *SymTable) AnalyzeName(scopes Scopes, name string, flags DefUse, bound,
557
557
is nested.
558
558
*/
559
559
if bound != nil && bound .Contains (name ) {
560
- scopes [name ] = scopeFree
560
+ scopes [name ] = ScopeFree
561
561
st .Free = true
562
562
free .Add (name )
563
563
return
@@ -566,13 +566,13 @@ func (st *SymTable) AnalyzeName(scopes Scopes, name string, flags DefUse, bound,
566
566
explicit? It could also be global implicit.
567
567
*/
568
568
if global != nil && global .Contains (name ) {
569
- scopes [name ] = scopeGlobalImplicit
569
+ scopes [name ] = ScopeGlobalImplicit
570
570
return
571
571
}
572
572
if st .Nested {
573
573
st .Free = true
574
574
}
575
- scopes [name ] = scopeGlobalImplicit
575
+ scopes [name ] = ScopeGlobalImplicit
576
576
}
577
577
578
578
/* If a name is defined in free and also in locals, then this block
@@ -584,7 +584,7 @@ func (st *SymTable) AnalyzeName(scopes Scopes, name string, flags DefUse, bound,
584
584
*/
585
585
func AnalyzeCells (scopes Scopes , free StringSet ) {
586
586
for name , scope := range scopes {
587
- if scope != scopeLocal {
587
+ if scope != ScopeLocal {
588
588
continue
589
589
}
590
590
if ! free .Contains (name ) {
@@ -594,7 +594,7 @@ func AnalyzeCells(scopes Scopes, free StringSet) {
594
594
from free. It is safe to replace the value of name
595
595
in the dict, because it will not cause a resize.
596
596
*/
597
- scopes [name ] = scopeCell
597
+ scopes [name ] = ScopeCell
598
598
free .Discard (name )
599
599
}
600
600
}
@@ -656,8 +656,8 @@ func (symbols Symbols) Update(scopes Scopes, bound, free StringSet, classflag bo
656
656
the class that has the same name as a local
657
657
or global in the class scope.
658
658
*/
659
- if classflag && (symbol .Flags & (defBound | defGlobal )) != 0 {
660
- symbol .Flags |= defFreeClass
659
+ if classflag && (symbol .Flags & (DefBound | DefGlobal )) != 0 {
660
+ symbol .Flags |= DefFreeClass
661
661
symbols [name ] = symbol
662
662
}
663
663
/* It's a cell, or already free in this scope */
@@ -668,7 +668,7 @@ func (symbols Symbols) Update(scopes Scopes, bound, free StringSet, classflag bo
668
668
continue /* it's a global */
669
669
}
670
670
/* Propagate new free symbol up the lexical stack */
671
- symbols [name ] = Symbol {Scope : scopeFree }
671
+ symbols [name ] = Symbol {Scope : ScopeFree }
672
672
}
673
673
}
674
674
0 commit comments