symtable.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. """Interface to the compiler's internal symbol tables"""
  2. import _symtable
  3. from _symtable import (USE, DEF_GLOBAL, DEF_NONLOCAL, DEF_LOCAL, DEF_PARAM,
  4. DEF_IMPORT, DEF_BOUND, DEF_ANNOT, SCOPE_OFF, SCOPE_MASK, FREE,
  5. LOCAL, GLOBAL_IMPLICIT, GLOBAL_EXPLICIT, CELL)
  6. import weakref
  7. __all__ = ["symtable", "SymbolTable", "Class", "Function", "Symbol"]
  8. def symtable(code, filename, compile_type):
  9. """ Return the toplevel *SymbolTable* for the source code.
  10. *filename* is the name of the file with the code
  11. and *compile_type* is the *compile()* mode argument.
  12. """
  13. top = _symtable.symtable(code, filename, compile_type)
  14. return _newSymbolTable(top, filename)
  15. class SymbolTableFactory:
  16. def __init__(self):
  17. self.__memo = weakref.WeakValueDictionary()
  18. def new(self, table, filename):
  19. if table.type == _symtable.TYPE_FUNCTION:
  20. return Function(table, filename)
  21. if table.type == _symtable.TYPE_CLASS:
  22. return Class(table, filename)
  23. return SymbolTable(table, filename)
  24. def __call__(self, table, filename):
  25. key = table, filename
  26. obj = self.__memo.get(key, None)
  27. if obj is None:
  28. obj = self.__memo[key] = self.new(table, filename)
  29. return obj
  30. _newSymbolTable = SymbolTableFactory()
  31. class SymbolTable:
  32. def __init__(self, raw_table, filename):
  33. self._table = raw_table
  34. self._filename = filename
  35. self._symbols = {}
  36. def __repr__(self):
  37. if self.__class__ == SymbolTable:
  38. kind = ""
  39. else:
  40. kind = "%s " % self.__class__.__name__
  41. if self._table.name == "top":
  42. return "<{0}SymbolTable for module {1}>".format(kind, self._filename)
  43. else:
  44. return "<{0}SymbolTable for {1} in {2}>".format(kind,
  45. self._table.name,
  46. self._filename)
  47. def get_type(self):
  48. """Return the type of the symbol table.
  49. The values returned are 'class', 'module', 'function',
  50. 'annotation', 'TypeVar bound', 'type alias', and 'type parameter'.
  51. """
  52. if self._table.type == _symtable.TYPE_MODULE:
  53. return "module"
  54. if self._table.type == _symtable.TYPE_FUNCTION:
  55. return "function"
  56. if self._table.type == _symtable.TYPE_CLASS:
  57. return "class"
  58. if self._table.type == _symtable.TYPE_ANNOTATION:
  59. return "annotation"
  60. if self._table.type == _symtable.TYPE_TYPE_VAR_BOUND:
  61. return "TypeVar bound"
  62. if self._table.type == _symtable.TYPE_TYPE_ALIAS:
  63. return "type alias"
  64. if self._table.type == _symtable.TYPE_TYPE_PARAM:
  65. return "type parameter"
  66. assert False, f"unexpected type: {self._table.type}"
  67. def get_id(self):
  68. """Return an identifier for the table.
  69. """
  70. return self._table.id
  71. def get_name(self):
  72. """Return the table's name.
  73. This corresponds to the name of the class, function
  74. or 'top' if the table is for a class, function or
  75. global respectively.
  76. """
  77. return self._table.name
  78. def get_lineno(self):
  79. """Return the number of the first line in the
  80. block for the table.
  81. """
  82. return self._table.lineno
  83. def is_optimized(self):
  84. """Return *True* if the locals in the table
  85. are optimizable.
  86. """
  87. return bool(self._table.type == _symtable.TYPE_FUNCTION)
  88. def is_nested(self):
  89. """Return *True* if the block is a nested class
  90. or function."""
  91. return bool(self._table.nested)
  92. def has_children(self):
  93. """Return *True* if the block has nested namespaces.
  94. """
  95. return bool(self._table.children)
  96. def get_identifiers(self):
  97. """Return a view object containing the names of symbols in the table.
  98. """
  99. return self._table.symbols.keys()
  100. def lookup(self, name):
  101. """Lookup a *name* in the table.
  102. Returns a *Symbol* instance.
  103. """
  104. sym = self._symbols.get(name)
  105. if sym is None:
  106. flags = self._table.symbols[name]
  107. namespaces = self.__check_children(name)
  108. module_scope = (self._table.name == "top")
  109. sym = self._symbols[name] = Symbol(name, flags, namespaces,
  110. module_scope=module_scope)
  111. return sym
  112. def get_symbols(self):
  113. """Return a list of *Symbol* instances for
  114. names in the table.
  115. """
  116. return [self.lookup(ident) for ident in self.get_identifiers()]
  117. def __check_children(self, name):
  118. return [_newSymbolTable(st, self._filename)
  119. for st in self._table.children
  120. if st.name == name]
  121. def get_children(self):
  122. """Return a list of the nested symbol tables.
  123. """
  124. return [_newSymbolTable(st, self._filename)
  125. for st in self._table.children]
  126. class Function(SymbolTable):
  127. # Default values for instance variables
  128. __params = None
  129. __locals = None
  130. __frees = None
  131. __globals = None
  132. __nonlocals = None
  133. def __idents_matching(self, test_func):
  134. return tuple(ident for ident in self.get_identifiers()
  135. if test_func(self._table.symbols[ident]))
  136. def get_parameters(self):
  137. """Return a tuple of parameters to the function.
  138. """
  139. if self.__params is None:
  140. self.__params = self.__idents_matching(lambda x:x & DEF_PARAM)
  141. return self.__params
  142. def get_locals(self):
  143. """Return a tuple of locals in the function.
  144. """
  145. if self.__locals is None:
  146. locs = (LOCAL, CELL)
  147. test = lambda x: ((x >> SCOPE_OFF) & SCOPE_MASK) in locs
  148. self.__locals = self.__idents_matching(test)
  149. return self.__locals
  150. def get_globals(self):
  151. """Return a tuple of globals in the function.
  152. """
  153. if self.__globals is None:
  154. glob = (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)
  155. test = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) in glob
  156. self.__globals = self.__idents_matching(test)
  157. return self.__globals
  158. def get_nonlocals(self):
  159. """Return a tuple of nonlocals in the function.
  160. """
  161. if self.__nonlocals is None:
  162. self.__nonlocals = self.__idents_matching(lambda x:x & DEF_NONLOCAL)
  163. return self.__nonlocals
  164. def get_frees(self):
  165. """Return a tuple of free variables in the function.
  166. """
  167. if self.__frees is None:
  168. is_free = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) == FREE
  169. self.__frees = self.__idents_matching(is_free)
  170. return self.__frees
  171. class Class(SymbolTable):
  172. __methods = None
  173. def get_methods(self):
  174. """Return a tuple of methods declared in the class.
  175. """
  176. if self.__methods is None:
  177. d = {}
  178. def is_local_symbol(ident):
  179. flags = self._table.symbols.get(ident, 0)
  180. return ((flags >> SCOPE_OFF) & SCOPE_MASK) == LOCAL
  181. for st in self._table.children:
  182. # pick the function-like symbols that are local identifiers
  183. if is_local_symbol(st.name):
  184. match st.type:
  185. case _symtable.TYPE_FUNCTION:
  186. # generators are of type TYPE_FUNCTION with a ".0"
  187. # parameter as a first parameter (which makes them
  188. # distinguishable from a function named 'genexpr')
  189. if st.name == 'genexpr' and '.0' in st.varnames:
  190. continue
  191. d[st.name] = 1
  192. case _symtable.TYPE_TYPE_PARAM:
  193. # Get the function-def block in the annotation
  194. # scope 'st' with the same identifier, if any.
  195. scope_name = st.name
  196. for c in st.children:
  197. if c.name == scope_name and c.type == _symtable.TYPE_FUNCTION:
  198. # A generic generator of type TYPE_FUNCTION
  199. # cannot be a direct child of 'st' (but it
  200. # can be a descendant), e.g.:
  201. #
  202. # class A:
  203. # type genexpr[genexpr] = (x for x in [])
  204. assert scope_name != 'genexpr' or '.0' not in c.varnames
  205. d[scope_name] = 1
  206. break
  207. self.__methods = tuple(d)
  208. return self.__methods
  209. class Symbol:
  210. def __init__(self, name, flags, namespaces=None, *, module_scope=False):
  211. self.__name = name
  212. self.__flags = flags
  213. self.__scope = (flags >> SCOPE_OFF) & SCOPE_MASK # like PyST_GetScope()
  214. self.__namespaces = namespaces or ()
  215. self.__module_scope = module_scope
  216. def __repr__(self):
  217. return "<symbol {0!r}>".format(self.__name)
  218. def get_name(self):
  219. """Return a name of a symbol.
  220. """
  221. return self.__name
  222. def is_referenced(self):
  223. """Return *True* if the symbol is used in
  224. its block.
  225. """
  226. return bool(self.__flags & _symtable.USE)
  227. def is_parameter(self):
  228. """Return *True* if the symbol is a parameter.
  229. """
  230. return bool(self.__flags & DEF_PARAM)
  231. def is_global(self):
  232. """Return *True* if the symbol is global.
  233. """
  234. return bool(self.__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)
  235. or (self.__module_scope and self.__flags & DEF_BOUND))
  236. def is_nonlocal(self):
  237. """Return *True* if the symbol is nonlocal."""
  238. return bool(self.__flags & DEF_NONLOCAL)
  239. def is_declared_global(self):
  240. """Return *True* if the symbol is declared global
  241. with a global statement."""
  242. return bool(self.__scope == GLOBAL_EXPLICIT)
  243. def is_local(self):
  244. """Return *True* if the symbol is local.
  245. """
  246. return bool(self.__scope in (LOCAL, CELL)
  247. or (self.__module_scope and self.__flags & DEF_BOUND))
  248. def is_annotated(self):
  249. """Return *True* if the symbol is annotated.
  250. """
  251. return bool(self.__flags & DEF_ANNOT)
  252. def is_free(self):
  253. """Return *True* if a referenced symbol is
  254. not assigned to.
  255. """
  256. return bool(self.__scope == FREE)
  257. def is_imported(self):
  258. """Return *True* if the symbol is created from
  259. an import statement.
  260. """
  261. return bool(self.__flags & DEF_IMPORT)
  262. def is_assigned(self):
  263. """Return *True* if a symbol is assigned to."""
  264. return bool(self.__flags & DEF_LOCAL)
  265. def is_namespace(self):
  266. """Returns *True* if name binding introduces new namespace.
  267. If the name is used as the target of a function or class
  268. statement, this will be true.
  269. Note that a single name can be bound to multiple objects. If
  270. is_namespace() is true, the name may also be bound to other
  271. objects, like an int or list, that does not introduce a new
  272. namespace.
  273. """
  274. return bool(self.__namespaces)
  275. def get_namespaces(self):
  276. """Return a list of namespaces bound to this name"""
  277. return self.__namespaces
  278. def get_namespace(self):
  279. """Return the single namespace bound to this name.
  280. Raises ValueError if the name is bound to multiple namespaces
  281. or no namespace.
  282. """
  283. if len(self.__namespaces) == 0:
  284. raise ValueError("name is not bound to any namespaces")
  285. elif len(self.__namespaces) > 1:
  286. raise ValueError("name is bound to multiple namespaces")
  287. else:
  288. return self.__namespaces[0]
  289. if __name__ == "__main__":
  290. import os, sys
  291. with open(sys.argv[0]) as f:
  292. src = f.read()
  293. mod = symtable(src, os.path.split(sys.argv[0])[1], "exec")
  294. for ident in mod.get_identifiers():
  295. info = mod.lookup(ident)
  296. print(info, info.is_local(), info.is_namespace())