Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 1 | """Module symbol-table generator""" |
| 2 | |
| 3 | from compiler import ast |
Neil Schemenauer | 92c3b21 | 2009-02-07 00:54:41 +0000 | [diff] [blame] | 4 | from compiler.consts import SC_LOCAL, SC_GLOBAL_IMPLICIT, SC_GLOBAL_EXPLICT, \ |
| 5 | SC_FREE, SC_CELL, SC_UNKNOWN |
Jeremy Hylton | c59e220 | 2001-08-27 22:56:16 +0000 | [diff] [blame] | 6 | from compiler.misc import mangle |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 7 | import types |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 8 | |
Jeremy Hylton | c59e220 | 2001-08-27 22:56:16 +0000 | [diff] [blame] | 9 | |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 10 | import sys |
| 11 | |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 12 | MANGLE_LEN = 256 |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 13 | |
| 14 | class Scope: |
| 15 | # XXX how much information do I need about each name? |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 16 | def __init__(self, name, module, klass=None): |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 17 | self.name = name |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 18 | self.module = module |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 19 | self.defs = {} |
| 20 | self.uses = {} |
| 21 | self.globals = {} |
| 22 | self.params = {} |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 23 | self.frees = {} |
| 24 | self.cells = {} |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 25 | self.children = [] |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 26 | # nested is true if the class could contain free variables, |
| 27 | # i.e. if it is nested within another function. |
| 28 | self.nested = None |
Jeremy Hylton | d4be10d | 2001-08-29 18:10:51 +0000 | [diff] [blame] | 29 | self.generator = None |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 30 | self.klass = None |
| 31 | if klass is not None: |
| 32 | for i in range(len(klass)): |
| 33 | if klass[i] != '_': |
| 34 | self.klass = klass[i:] |
| 35 | break |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 36 | |
| 37 | def __repr__(self): |
| 38 | return "<%s: %s>" % (self.__class__.__name__, self.name) |
| 39 | |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 40 | def mangle(self, name): |
| 41 | if self.klass is None: |
| 42 | return name |
Jeremy Hylton | c59e220 | 2001-08-27 22:56:16 +0000 | [diff] [blame] | 43 | return mangle(name, self.klass) |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 44 | |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 45 | def add_def(self, name): |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 46 | self.defs[self.mangle(name)] = 1 |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 47 | |
| 48 | def add_use(self, name): |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 49 | self.uses[self.mangle(name)] = 1 |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 50 | |
| 51 | def add_global(self, name): |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 52 | name = self.mangle(name) |
Georg Brandl | 2d2fe57 | 2008-12-15 08:58:59 +0000 | [diff] [blame] | 53 | if name in self.uses or name in self.defs: |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 54 | pass # XXX warn about global following def/use |
Georg Brandl | 2d2fe57 | 2008-12-15 08:58:59 +0000 | [diff] [blame] | 55 | if name in self.params: |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 56 | raise SyntaxError, "%s in %s is global and parameter" % \ |
| 57 | (name, self.name) |
| 58 | self.globals[name] = 1 |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 59 | self.module.add_def(name) |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 60 | |
| 61 | def add_param(self, name): |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 62 | name = self.mangle(name) |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 63 | self.defs[name] = 1 |
| 64 | self.params[name] = 1 |
| 65 | |
| 66 | def get_names(self): |
| 67 | d = {} |
| 68 | d.update(self.defs) |
| 69 | d.update(self.uses) |
Jeremy Hylton | dbdb28e | 2001-04-09 20:11:59 +0000 | [diff] [blame] | 70 | d.update(self.globals) |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 71 | return d.keys() |
| 72 | |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 73 | def add_child(self, child): |
| 74 | self.children.append(child) |
| 75 | |
| 76 | def get_children(self): |
| 77 | return self.children |
| 78 | |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 79 | def DEBUG(self): |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 80 | print >> sys.stderr, self.name, self.nested and "nested" or "" |
| 81 | print >> sys.stderr, "\tglobals: ", self.globals |
| 82 | print >> sys.stderr, "\tcells: ", self.cells |
| 83 | print >> sys.stderr, "\tdefs: ", self.defs |
| 84 | print >> sys.stderr, "\tuses: ", self.uses |
| 85 | print >> sys.stderr, "\tfrees:", self.frees |
| 86 | |
| 87 | def check_name(self, name): |
| 88 | """Return scope of name. |
| 89 | |
| 90 | The scope of a name could be LOCAL, GLOBAL, FREE, or CELL. |
| 91 | """ |
Georg Brandl | 2d2fe57 | 2008-12-15 08:58:59 +0000 | [diff] [blame] | 92 | if name in self.globals: |
Neil Schemenauer | 92c3b21 | 2009-02-07 00:54:41 +0000 | [diff] [blame] | 93 | return SC_GLOBAL_EXPLICT |
Georg Brandl | 2d2fe57 | 2008-12-15 08:58:59 +0000 | [diff] [blame] | 94 | if name in self.cells: |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 95 | return SC_CELL |
Georg Brandl | 2d2fe57 | 2008-12-15 08:58:59 +0000 | [diff] [blame] | 96 | if name in self.defs: |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 97 | return SC_LOCAL |
Georg Brandl | 2d2fe57 | 2008-12-15 08:58:59 +0000 | [diff] [blame] | 98 | if self.nested and (name in self.frees or name in self.uses): |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 99 | return SC_FREE |
| 100 | if self.nested: |
| 101 | return SC_UNKNOWN |
| 102 | else: |
Neil Schemenauer | 92c3b21 | 2009-02-07 00:54:41 +0000 | [diff] [blame] | 103 | return SC_GLOBAL_IMPLICIT |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 104 | |
| 105 | def get_free_vars(self): |
| 106 | if not self.nested: |
| 107 | return () |
| 108 | free = {} |
| 109 | free.update(self.frees) |
| 110 | for name in self.uses.keys(): |
Georg Brandl | 2d2fe57 | 2008-12-15 08:58:59 +0000 | [diff] [blame] | 111 | if name not in self.defs and name not in self.globals: |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 112 | free[name] = 1 |
| 113 | return free.keys() |
| 114 | |
| 115 | def handle_children(self): |
| 116 | for child in self.children: |
| 117 | frees = child.get_free_vars() |
| 118 | globals = self.add_frees(frees) |
| 119 | for name in globals: |
| 120 | child.force_global(name) |
| 121 | |
| 122 | def force_global(self, name): |
| 123 | """Force name to be global in scope. |
| 124 | |
| 125 | Some child of the current node had a free reference to name. |
| 126 | When the child was processed, it was labelled a free |
| 127 | variable. Now that all its enclosing scope have been |
| 128 | processed, the name is known to be a global or builtin. So |
| 129 | walk back down the child chain and set the name to be global |
| 130 | rather than free. |
| 131 | |
| 132 | Be careful to stop if a child does not think the name is |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 133 | free. |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 134 | """ |
| 135 | self.globals[name] = 1 |
Georg Brandl | 2d2fe57 | 2008-12-15 08:58:59 +0000 | [diff] [blame] | 136 | if name in self.frees: |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 137 | del self.frees[name] |
| 138 | for child in self.children: |
| 139 | if child.check_name(name) == SC_FREE: |
| 140 | child.force_global(name) |
| 141 | |
| 142 | def add_frees(self, names): |
| 143 | """Process list of free vars from nested scope. |
| 144 | |
| 145 | Returns a list of names that are either 1) declared global in the |
| 146 | parent or 2) undefined in a top-level parent. In either case, |
| 147 | the nested scope should treat them as globals. |
| 148 | """ |
| 149 | child_globals = [] |
| 150 | for name in names: |
| 151 | sc = self.check_name(name) |
| 152 | if self.nested: |
| 153 | if sc == SC_UNKNOWN or sc == SC_FREE \ |
| 154 | or isinstance(self, ClassScope): |
| 155 | self.frees[name] = 1 |
Neil Schemenauer | 92c3b21 | 2009-02-07 00:54:41 +0000 | [diff] [blame] | 156 | elif sc == SC_GLOBAL_IMPLICIT: |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 157 | child_globals.append(name) |
| 158 | elif isinstance(self, FunctionScope) and sc == SC_LOCAL: |
| 159 | self.cells[name] = 1 |
Jeremy Hylton | cd8a127 | 2001-08-27 21:06:35 +0000 | [diff] [blame] | 160 | elif sc != SC_CELL: |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 161 | child_globals.append(name) |
| 162 | else: |
| 163 | if sc == SC_LOCAL: |
| 164 | self.cells[name] = 1 |
Jeremy Hylton | cd8a127 | 2001-08-27 21:06:35 +0000 | [diff] [blame] | 165 | elif sc != SC_CELL: |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 166 | child_globals.append(name) |
| 167 | return child_globals |
| 168 | |
| 169 | def get_cell_vars(self): |
| 170 | return self.cells.keys() |
| 171 | |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 172 | class ModuleScope(Scope): |
| 173 | __super_init = Scope.__init__ |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 174 | |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 175 | def __init__(self): |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 176 | self.__super_init("global", self) |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 177 | |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 178 | class FunctionScope(Scope): |
| 179 | pass |
| 180 | |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 181 | class GenExprScope(Scope): |
| 182 | __super_init = Scope.__init__ |
| 183 | |
| 184 | __counter = 1 |
| 185 | |
| 186 | def __init__(self, module, klass=None): |
| 187 | i = self.__counter |
| 188 | self.__counter += 1 |
| 189 | self.__super_init("generator expression<%d>"%i, module, klass) |
Neil Schemenauer | 4c6b0d5 | 2006-08-16 23:38:05 +0000 | [diff] [blame] | 190 | self.add_param('.0') |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 191 | |
| 192 | def get_names(self): |
Neal Norwitz | 6aaccc6 | 2006-06-11 08:35:14 +0000 | [diff] [blame] | 193 | keys = Scope.get_names(self) |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 194 | return keys |
| 195 | |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 196 | class LambdaScope(FunctionScope): |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 197 | __super_init = Scope.__init__ |
| 198 | |
| 199 | __counter = 1 |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 200 | |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 201 | def __init__(self, module, klass=None): |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 202 | i = self.__counter |
| 203 | self.__counter += 1 |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 204 | self.__super_init("lambda.%d" % i, module, klass) |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 205 | |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 206 | class ClassScope(Scope): |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 207 | __super_init = Scope.__init__ |
| 208 | |
| 209 | def __init__(self, name, module): |
| 210 | self.__super_init(name, module, name) |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 211 | |
| 212 | class SymbolVisitor: |
| 213 | def __init__(self): |
| 214 | self.scopes = {} |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 215 | self.klass = None |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 216 | |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 217 | # node that define new scopes |
| 218 | |
| 219 | def visitModule(self, node): |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 220 | scope = self.module = self.scopes[node] = ModuleScope() |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 221 | self.visit(node.node, scope) |
| 222 | |
Barry Warsaw | 52acb49 | 2001-12-21 20:04:22 +0000 | [diff] [blame] | 223 | visitExpression = visitModule |
| 224 | |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 225 | def visitFunction(self, node, parent): |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 226 | if node.decorators: |
| 227 | self.visit(node.decorators, parent) |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 228 | parent.add_def(node.name) |
| 229 | for n in node.defaults: |
| 230 | self.visit(n, parent) |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 231 | scope = FunctionScope(node.name, self.module, self.klass) |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 232 | if parent.nested or isinstance(parent, FunctionScope): |
| 233 | scope.nested = 1 |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 234 | self.scopes[node] = scope |
Jeremy Hylton | dbdb28e | 2001-04-09 20:11:59 +0000 | [diff] [blame] | 235 | self._do_args(scope, node.argnames) |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 236 | self.visit(node.code, scope) |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 237 | self.handle_free_vars(scope, parent) |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 238 | |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 239 | def visitGenExpr(self, node, parent): |
| 240 | scope = GenExprScope(self.module, self.klass); |
| 241 | if parent.nested or isinstance(parent, FunctionScope) \ |
| 242 | or isinstance(parent, GenExprScope): |
| 243 | scope.nested = 1 |
| 244 | |
| 245 | self.scopes[node] = scope |
| 246 | self.visit(node.code, scope) |
| 247 | |
| 248 | self.handle_free_vars(scope, parent) |
| 249 | |
| 250 | def visitGenExprInner(self, node, scope): |
| 251 | for genfor in node.quals: |
| 252 | self.visit(genfor, scope) |
| 253 | |
| 254 | self.visit(node.expr, scope) |
| 255 | |
| 256 | def visitGenExprFor(self, node, scope): |
| 257 | self.visit(node.assign, scope, 1) |
| 258 | self.visit(node.iter, scope) |
| 259 | for if_ in node.ifs: |
| 260 | self.visit(if_, scope) |
| 261 | |
| 262 | def visitGenExprIf(self, node, scope): |
| 263 | self.visit(node.test, scope) |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 264 | |
Jeremy Hylton | ead21f5 | 2003-08-28 02:09:26 +0000 | [diff] [blame] | 265 | def visitLambda(self, node, parent, assign=0): |
| 266 | # Lambda is an expression, so it could appear in an expression |
| 267 | # context where assign is passed. The transformer should catch |
| 268 | # any code that has a lambda on the left-hand side. |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 269 | assert not assign |
| 270 | |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 271 | for n in node.defaults: |
| 272 | self.visit(n, parent) |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 273 | scope = LambdaScope(self.module, self.klass) |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 274 | if parent.nested or isinstance(parent, FunctionScope): |
| 275 | scope.nested = 1 |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 276 | self.scopes[node] = scope |
Jeremy Hylton | dbdb28e | 2001-04-09 20:11:59 +0000 | [diff] [blame] | 277 | self._do_args(scope, node.argnames) |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 278 | self.visit(node.code, scope) |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 279 | self.handle_free_vars(scope, parent) |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 280 | |
Jeremy Hylton | dbdb28e | 2001-04-09 20:11:59 +0000 | [diff] [blame] | 281 | def _do_args(self, scope, args): |
| 282 | for name in args: |
| 283 | if type(name) == types.TupleType: |
| 284 | self._do_args(scope, name) |
| 285 | else: |
| 286 | scope.add_param(name) |
| 287 | |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 288 | def handle_free_vars(self, scope, parent): |
| 289 | parent.add_child(scope) |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 290 | scope.handle_children() |
| 291 | |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 292 | def visitClass(self, node, parent): |
| 293 | parent.add_def(node.name) |
| 294 | for n in node.bases: |
| 295 | self.visit(n, parent) |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 296 | scope = ClassScope(node.name, self.module) |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 297 | if parent.nested or isinstance(parent, FunctionScope): |
| 298 | scope.nested = 1 |
Jeremy Hylton | accb62b | 2002-12-31 18:17:44 +0000 | [diff] [blame] | 299 | if node.doc is not None: |
| 300 | scope.add_def('__doc__') |
| 301 | scope.add_def('__module__') |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 302 | self.scopes[node] = scope |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 303 | prev = self.klass |
| 304 | self.klass = node.name |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 305 | self.visit(node.code, scope) |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 306 | self.klass = prev |
Jeremy Hylton | 364f9b9 | 2001-04-12 06:40:42 +0000 | [diff] [blame] | 307 | self.handle_free_vars(scope, parent) |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 308 | |
| 309 | # name can be a def or a use |
| 310 | |
| 311 | # XXX a few calls and nodes expect a third "assign" arg that is |
| 312 | # true if the name is being used as an assignment. only |
| 313 | # expressions contained within statements may have the assign arg. |
| 314 | |
| 315 | def visitName(self, node, scope, assign=0): |
| 316 | if assign: |
| 317 | scope.add_def(node.name) |
| 318 | else: |
| 319 | scope.add_use(node.name) |
| 320 | |
| 321 | # operations that bind new names |
| 322 | |
| 323 | def visitFor(self, node, scope): |
| 324 | self.visit(node.assign, scope, 1) |
| 325 | self.visit(node.list, scope) |
| 326 | self.visit(node.body, scope) |
| 327 | if node.else_: |
| 328 | self.visit(node.else_, scope) |
| 329 | |
| 330 | def visitFrom(self, node, scope): |
| 331 | for name, asname in node.names: |
| 332 | if name == "*": |
| 333 | continue |
| 334 | scope.add_def(asname or name) |
| 335 | |
| 336 | def visitImport(self, node, scope): |
| 337 | for name, asname in node.names: |
| 338 | i = name.find(".") |
| 339 | if i > -1: |
| 340 | name = name[:i] |
| 341 | scope.add_def(asname or name) |
| 342 | |
Jeremy Hylton | d4be10d | 2001-08-29 18:10:51 +0000 | [diff] [blame] | 343 | def visitGlobal(self, node, scope): |
| 344 | for name in node.names: |
| 345 | scope.add_global(name) |
| 346 | |
| 347 | def visitAssign(self, node, scope): |
| 348 | """Propagate assignment flag down to child nodes. |
| 349 | |
| 350 | The Assign node doesn't itself contains the variables being |
| 351 | assigned to. Instead, the children in node.nodes are visited |
| 352 | with the assign flag set to true. When the names occur in |
| 353 | those nodes, they are marked as defs. |
| 354 | |
| 355 | Some names that occur in an assignment target are not bound by |
| 356 | the assignment, e.g. a name occurring inside a slice. The |
| 357 | visitor handles these nodes specially; they do not propagate |
| 358 | the assign flag to their children. |
| 359 | """ |
| 360 | for n in node.nodes: |
| 361 | self.visit(n, scope, 1) |
| 362 | self.visit(node.expr, scope) |
| 363 | |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 364 | def visitAssName(self, node, scope, assign=1): |
| 365 | scope.add_def(node.name) |
| 366 | |
Jeremy Hylton | cd8a127 | 2001-08-27 21:06:35 +0000 | [diff] [blame] | 367 | def visitAssAttr(self, node, scope, assign=0): |
| 368 | self.visit(node.expr, scope, 0) |
| 369 | |
| 370 | def visitSubscript(self, node, scope, assign=0): |
| 371 | self.visit(node.expr, scope, 0) |
| 372 | for n in node.subs: |
| 373 | self.visit(n, scope, 0) |
Jeremy Hylton | d4be10d | 2001-08-29 18:10:51 +0000 | [diff] [blame] | 374 | |
| 375 | def visitSlice(self, node, scope, assign=0): |
Jeremy Hylton | 652a224 | 2001-09-14 22:45:57 +0000 | [diff] [blame] | 376 | self.visit(node.expr, scope, 0) |
Jeremy Hylton | d4be10d | 2001-08-29 18:10:51 +0000 | [diff] [blame] | 377 | if node.lower: |
| 378 | self.visit(node.lower, scope, 0) |
| 379 | if node.upper: |
| 380 | self.visit(node.upper, scope, 0) |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 381 | |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 382 | def visitAugAssign(self, node, scope): |
Jeremy Hylton | 5c9aad6 | 2001-04-12 07:06:25 +0000 | [diff] [blame] | 383 | # If the LHS is a name, then this counts as assignment. |
| 384 | # Otherwise, it's just use. |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 385 | self.visit(node.node, scope) |
Jeremy Hylton | 5c9aad6 | 2001-04-12 07:06:25 +0000 | [diff] [blame] | 386 | if isinstance(node.node, ast.Name): |
| 387 | self.visit(node.node, scope, 1) # XXX worry about this |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 388 | self.visit(node.expr, scope) |
| 389 | |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 390 | # prune if statements if tests are false |
| 391 | |
| 392 | _const_types = types.StringType, types.IntType, types.FloatType |
| 393 | |
| 394 | def visitIf(self, node, scope): |
| 395 | for test, body in node.tests: |
| 396 | if isinstance(test, ast.Const): |
| 397 | if type(test.value) in self._const_types: |
| 398 | if not test.value: |
| 399 | continue |
| 400 | self.visit(test, scope) |
| 401 | self.visit(body, scope) |
| 402 | if node.else_: |
| 403 | self.visit(node.else_, scope) |
| 404 | |
Jeremy Hylton | d4be10d | 2001-08-29 18:10:51 +0000 | [diff] [blame] | 405 | # a yield statement signals a generator |
| 406 | |
| 407 | def visitYield(self, node, scope): |
Jeremy Hylton | 652a224 | 2001-09-14 22:45:57 +0000 | [diff] [blame] | 408 | scope.generator = 1 |
Jeremy Hylton | d4be10d | 2001-08-29 18:10:51 +0000 | [diff] [blame] | 409 | self.visit(node.value, scope) |
| 410 | |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 411 | def list_eq(l1, l2): |
Neal Norwitz | f923267 | 2005-11-25 03:16:34 +0000 | [diff] [blame] | 412 | return sorted(l1) == sorted(l2) |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 413 | |
| 414 | if __name__ == "__main__": |
| 415 | import sys |
| 416 | from compiler import parseFile, walk |
| 417 | import symtable |
| 418 | |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 419 | def get_names(syms): |
| 420 | return [s for s in [s.get_name() for s in syms.get_symbols()] |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 421 | if not (s.startswith('_[') or s.startswith('.'))] |
| 422 | |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 423 | for file in sys.argv[1:]: |
| 424 | print file |
| 425 | f = open(file) |
| 426 | buf = f.read() |
| 427 | f.close() |
| 428 | syms = symtable.symtable(buf, file, "exec") |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 429 | mod_names = get_names(syms) |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 430 | tree = parseFile(file) |
| 431 | s = SymbolVisitor() |
| 432 | walk(tree, s) |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 433 | |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 434 | # compare module-level symbols |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 435 | names2 = s.scopes[tree].get_names() |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 436 | |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 437 | if not list_eq(mod_names, names2): |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 438 | print |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 439 | print "oops", file |
Neal Norwitz | f923267 | 2005-11-25 03:16:34 +0000 | [diff] [blame] | 440 | print sorted(mod_names) |
| 441 | print sorted(names2) |
Jeremy Hylton | 8b966dc | 2001-04-09 04:35:35 +0000 | [diff] [blame] | 442 | sys.exit(-1) |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 443 | |
| 444 | d = {} |
| 445 | d.update(s.scopes) |
| 446 | del d[tree] |
| 447 | scopes = d.values() |
| 448 | del d |
| 449 | |
| 450 | for s in syms.get_symbols(): |
| 451 | if s.is_namespace(): |
| 452 | l = [sc for sc in scopes |
| 453 | if sc.name == s.get_name()] |
| 454 | if len(l) > 1: |
| 455 | print "skipping", s.get_name() |
| 456 | else: |
| 457 | if not list_eq(get_names(s.get_namespace()), |
| 458 | l[0].get_names()): |
| 459 | print s.get_name() |
Neal Norwitz | f923267 | 2005-11-25 03:16:34 +0000 | [diff] [blame] | 460 | print sorted(get_names(s.get_namespace())) |
| 461 | print sorted(l[0].get_names()) |
Jeremy Hylton | f870c95 | 2001-04-09 13:57:32 +0000 | [diff] [blame] | 462 | sys.exit(-1) |