Skip to content

Commit 21129d0

Browse files
committed
symtable: make definitions public
1 parent 08446c2 commit 21129d0

File tree

5 files changed

+574
-574
lines changed

5 files changed

+574
-574
lines changed

symtable/make_symtable_test.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -183,23 +183,23 @@ def dump_strings(ss):
183183

184184
# Scope numbers to names (from symtable.h)
185185
SCOPES = {
186-
1: "scopeLocal",
187-
2: "scopeGlobalExplicit",
188-
3: "scopeGlobalImplicit",
189-
4: "scopeFree",
190-
5: "scopeCell",
186+
1: "ScopeLocal",
187+
2: "ScopeGlobalExplicit",
188+
3: "ScopeGlobalImplicit",
189+
4: "ScopeFree",
190+
5: "ScopeCell",
191191
}
192192

193193
#def-use flags to names (from symtable.h)
194194
DEF_FLAGS = (
195-
("defGlobal", 1), # global stmt
196-
("defLocal", 2), # assignment in code block
197-
("defParam", 2<<1), # formal parameter
198-
("defNonlocal", 2<<2), # nonlocal stmt
199-
("defUse", 2<<3), # name is used
200-
("defFree", 2<<4), # name used but not defined in nested block
201-
("defFreeClass", 2<<5),# free variable from class's method
202-
("defImport", 2<<6), # assignment occurred via import
195+
("DefGlobal", 1), # global stmt
196+
("DefLocal", 2), # assignment in code block
197+
("DefParam", 2<<1), # formal parameter
198+
("DefNonlocal", 2<<2), # nonlocal stmt
199+
("DefUse", 2<<3), # name is used
200+
("DefFree", 2<<4), # name used but not defined in nested block
201+
("DefFreeClass", 2<<5),# free variable from class's method
202+
("DefImport", 2<<6), # assignment occurred via import
203203
)
204204

205205
#opt flags flags to names (from symtable.h)

symtable/stringer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package compile
44

55
import "fmt"
66

7-
const _Scope_name = "scopeInvalidscopeLocalscopeGlobalExplicitscopeGlobalImplicitscopeFreescopeCell"
7+
const _Scope_name = "ScopeInvalidScopeLocalScopeGlobalExplicitScopeGlobalImplicitScopeFreeScopeCell"
88

99
var _Scope_index = [...]uint8{0, 12, 22, 41, 60, 69, 78}
1010

symtable/symtable.go

+64-64
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,37 @@ type Scope uint8
2525

2626
// Scope definitions
2727
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
3434
)
3535

3636
// Accumulate Scopes
3737
type Scopes map[string]Scope
3838

3939
// Def-use flag information
40-
type DefUse uint8
40+
type DefUseFlags uint8
4141

4242
// Flags for def-use information
4343
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
5656
// table. GLOBAL is returned from PyST_GetScope() for either of them.
5757
// It is stored in ste_symbols at bits 12-15.
58-
defScopeMask = (defGlobal | defLocal | defParam | defNonlocal)
58+
DefScopeMask = (DefGlobal | DefLocal | DefParam | DefNonlocal)
5959
)
6060

6161
// BlockType for SymTable
@@ -80,7 +80,7 @@ const (
8080
// Info about a symbol
8181
type Symbol struct {
8282
Scope Scope
83-
Flags DefUse
83+
Flags DefUseFlags
8484
}
8585

8686
type Symbols map[string]Symbol
@@ -163,17 +163,17 @@ func (st *SymTable) addArgumentsToSymbolTable(node *ast.Arguments) {
163163
// skip default arguments inside function block
164164
// XXX should ast be different?
165165
for _, arg := range node.Args {
166-
st.AddDef(arg.Arg, defParam)
166+
st.AddDef(arg.Arg, DefParam)
167167
}
168168
for _, arg := range node.Kwonlyargs {
169-
st.AddDef(arg.Arg, defParam)
169+
st.AddDef(arg.Arg, DefParam)
170170
}
171171
if node.Vararg != nil {
172-
st.AddDef(node.Vararg.Arg, defParam)
172+
st.AddDef(node.Vararg.Arg, DefParam)
173173
st.Varargs = true
174174
}
175175
if node.Kwarg != nil {
176-
st.AddDef(node.Kwarg.Arg, defParam)
176+
st.AddDef(node.Kwarg.Arg, DefParam)
177177
st.Varkeywords = true
178178
}
179179
}
@@ -186,47 +186,47 @@ func (st *SymTable) Parse(Ast ast.Ast) {
186186
for _, name := range node.Names {
187187
cur, ok := st.Symbols[string(name)]
188188
if ok {
189-
if (cur.Flags & defLocal) != 0 {
189+
if (cur.Flags & DefLocal) != 0 {
190190
// FIXME this should be a warning
191191
log.Printf("name '%s' is assigned to before nonlocal declaration", name)
192192

193193
}
194-
if (cur.Flags & defUse) != 0 {
194+
if (cur.Flags & DefUse) != 0 {
195195
// FIXME this should be a warning
196196
log.Printf("name '%s' is used prior to nonlocal declaration", name)
197197
}
198198
}
199-
st.AddDef(name, defNonlocal)
199+
st.AddDef(name, DefNonlocal)
200200
}
201201
case *ast.Global:
202202
for _, name := range node.Names {
203203
cur, ok := st.Symbols[string(name)]
204204
if ok {
205-
if (cur.Flags & defLocal) != 0 {
205+
if (cur.Flags & DefLocal) != 0 {
206206
// FIXME this should be a warning
207207
log.Printf("name '%s' is assigned to before global declaration", name)
208208

209209
}
210-
if (cur.Flags & defUse) != 0 {
210+
if (cur.Flags & DefUse) != 0 {
211211
// FIXME this should be a warning
212212
log.Printf("name '%s' is used prior to global declaration", name)
213213
}
214214
}
215-
st.AddDef(name, defGlobal)
215+
st.AddDef(name, DefGlobal)
216216
}
217217
case *ast.Name:
218218
if node.Ctx == ast.Load {
219-
st.AddDef(node.Id, defUse)
219+
st.AddDef(node.Id, DefUse)
220220
} else {
221-
st.AddDef(node.Id, defLocal)
221+
st.AddDef(node.Id, DefLocal)
222222
}
223223
// Special-case super: it counts as a use of __class__
224224
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)
226226
}
227227
case *ast.FunctionDef:
228228
// Add the function name to the SymTable
229-
st.AddDef(node.Name, defLocal)
229+
st.AddDef(node.Name, DefLocal)
230230
name := string(node.Name)
231231

232232
// Walk these things in original symbol table
@@ -252,7 +252,7 @@ func (st *SymTable) Parse(Ast ast.Ast) {
252252
// return false to stop the parse
253253
return false
254254
case *ast.ClassDef:
255-
st.AddDef(node.Name, defLocal)
255+
st.AddDef(node.Name, DefLocal)
256256
name := string(node.Name)
257257
// Parse in the original symtable
258258
for _, expr := range node.Bases {
@@ -310,7 +310,7 @@ func (st *SymTable) Parse(Ast ast.Ast) {
310310
return false
311311
case *ast.ExceptHandler:
312312
if node.Name != "" {
313-
st.AddDef(node.Name, defLocal)
313+
st.AddDef(node.Name, DefLocal)
314314
}
315315
case *ast.Alias:
316316
// Compute store_name, the name actually bound by the import
@@ -326,7 +326,7 @@ func (st *SymTable) Parse(Ast ast.Ast) {
326326
store_name = name[:dot]
327327
}
328328
if name != "*" {
329-
st.AddDef(store_name, defImport)
329+
st.AddDef(store_name, DefImport)
330330
} else {
331331
if st.Type != ModuleBlock {
332332
panic(py.ExceptionNewf(py.SyntaxError, "import * only allowed at module level"))
@@ -346,7 +346,7 @@ func (st *SymTable) Parse(Ast ast.Ast) {
346346
func (st *SymTable) newTmpName() {
347347
st.TmpName++
348348
id := ast.Identifier(fmt.Sprintf("_[%d]", st.TmpName))
349-
st.AddDef(id, defLocal)
349+
st.AddDef(id, DefLocal)
350350
}
351351

352352
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
360360
stNew.Generator = isGenerator
361361
// Outermost iter is received as an argument
362362
id := ast.Identifier(fmt.Sprintf(".%d", 0))
363-
stNew.AddDef(id, defParam)
363+
stNew.AddDef(id, DefParam)
364364
// Allocate temporary name if needed
365365
if needsTmp {
366366
stNew.newTmpName()
@@ -383,13 +383,13 @@ func (st *SymTable) parseComprehension(Ast ast.Ast, scopeName string, generators
383383
}
384384

385385
// 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) {
387387
// FIXME mangled := _Py_Mangle(st.Private, name)
388388
mangled := string(name)
389389

390390
// Add or update the symbol in the Symbols
391391
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 {
393393
// Is it better to use 'mangled' or 'name' here?
394394
panic(py.ExceptionNewf(py.SyntaxError, "duplicate argument '%s' in function definition", name))
395395
// FIXME
@@ -406,13 +406,13 @@ func (st *SymTable) AddDef(name ast.Identifier, flags DefUse) {
406406
}
407407
}
408408

409-
if (flags & defParam) != 0 {
409+
if (flags & DefParam) != 0 {
410410
st.Varnames = append(st.Varnames, mangled)
411-
} else if (flags & defGlobal) != 0 {
411+
} else if (flags & DefGlobal) != 0 {
412412
// If it is a global definition then add it in the global Symbols
413413
//
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
416416
if sym, ok := st.Global.Symbols[mangled]; ok {
417417
sym.Flags |= flags
418418
st.Global.Symbols[mangled] = sym
@@ -515,23 +515,23 @@ func (s StringSet) Contains(k string) bool {
515515
about the new name. For example, a new global will add an entry to
516516
global. A name that was global can be changed to local.
517517
*/
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 {
521521
panic(py.ExceptionNewf(py.SyntaxError, "name '%s' is parameter and global", name))
522522
}
523-
if (flags & defNonlocal) != 0 {
523+
if (flags & DefNonlocal) != 0 {
524524
panic(py.ExceptionNewf(py.SyntaxError, "name '%s' is nonlocal and global", name))
525525
}
526-
scopes[name] = scopeGlobalExplicit
526+
scopes[name] = ScopeGlobalExplicit
527527
global.Add(name)
528528
if bound != nil {
529529
bound.Discard(name)
530530
}
531531
return
532532
}
533-
if (flags & defNonlocal) != 0 {
534-
if (flags & defParam) != 0 {
533+
if (flags & DefNonlocal) != 0 {
534+
if (flags & DefParam) != 0 {
535535
panic(py.ExceptionNewf(py.SyntaxError, "name '%s' is parameter and nonlocal", name))
536536
}
537537
if bound == nil {
@@ -540,13 +540,13 @@ func (st *SymTable) AnalyzeName(scopes Scopes, name string, flags DefUse, bound,
540540
if !bound.Contains(name) {
541541
panic(py.ExceptionNewf(py.SyntaxError, "no binding for nonlocal '%s' found", name))
542542
}
543-
scopes[name] = scopeFree
543+
scopes[name] = ScopeFree
544544
st.Free = true
545545
free.Add(name)
546546
return
547547
}
548-
if (flags & defBound) != 0 {
549-
scopes[name] = scopeLocal
548+
if (flags & DefBound) != 0 {
549+
scopes[name] = ScopeLocal
550550
local.Add(name)
551551
global.Discard(name)
552552
return
@@ -557,7 +557,7 @@ func (st *SymTable) AnalyzeName(scopes Scopes, name string, flags DefUse, bound,
557557
is nested.
558558
*/
559559
if bound != nil && bound.Contains(name) {
560-
scopes[name] = scopeFree
560+
scopes[name] = ScopeFree
561561
st.Free = true
562562
free.Add(name)
563563
return
@@ -566,13 +566,13 @@ func (st *SymTable) AnalyzeName(scopes Scopes, name string, flags DefUse, bound,
566566
explicit? It could also be global implicit.
567567
*/
568568
if global != nil && global.Contains(name) {
569-
scopes[name] = scopeGlobalImplicit
569+
scopes[name] = ScopeGlobalImplicit
570570
return
571571
}
572572
if st.Nested {
573573
st.Free = true
574574
}
575-
scopes[name] = scopeGlobalImplicit
575+
scopes[name] = ScopeGlobalImplicit
576576
}
577577

578578
/* 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,
584584
*/
585585
func AnalyzeCells(scopes Scopes, free StringSet) {
586586
for name, scope := range scopes {
587-
if scope != scopeLocal {
587+
if scope != ScopeLocal {
588588
continue
589589
}
590590
if !free.Contains(name) {
@@ -594,7 +594,7 @@ func AnalyzeCells(scopes Scopes, free StringSet) {
594594
from free. It is safe to replace the value of name
595595
in the dict, because it will not cause a resize.
596596
*/
597-
scopes[name] = scopeCell
597+
scopes[name] = ScopeCell
598598
free.Discard(name)
599599
}
600600
}
@@ -656,8 +656,8 @@ func (symbols Symbols) Update(scopes Scopes, bound, free StringSet, classflag bo
656656
the class that has the same name as a local
657657
or global in the class scope.
658658
*/
659-
if classflag && (symbol.Flags&(defBound|defGlobal)) != 0 {
660-
symbol.Flags |= defFreeClass
659+
if classflag && (symbol.Flags&(DefBound|DefGlobal)) != 0 {
660+
symbol.Flags |= DefFreeClass
661661
symbols[name] = symbol
662662
}
663663
/* 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
668668
continue /* it's a global */
669669
}
670670
/* Propagate new free symbol up the lexical stack */
671-
symbols[name] = Symbol{Scope: scopeFree}
671+
symbols[name] = Symbol{Scope: ScopeFree}
672672
}
673673
}
674674

0 commit comments

Comments
 (0)