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