/Lib/compiler/symbols.py

http://unladen-swallow.googlecode.com/ · Python · 461 lines · 371 code · 18 blank · 72 comment · 25 complexity · 598b445945dea974046719896ec41263 MD5 · raw file

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