Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 1 | """Interface to the compiler's internal symbol tables""" |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 2 | |
| 3 | import _symtable |
Pablo Galindo | d5b4f1b | 2018-10-20 01:46:00 +0100 | [diff] [blame] | 4 | from _symtable import (USE, DEF_GLOBAL, DEF_NONLOCAL, DEF_LOCAL, DEF_PARAM, |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5 | DEF_IMPORT, DEF_BOUND, DEF_ANNOT, SCOPE_OFF, SCOPE_MASK, FREE, |
Benjamin Peterson | 500c6ef | 2009-06-28 19:30:36 +0000 | [diff] [blame] | 6 | LOCAL, GLOBAL_IMPLICIT, GLOBAL_EXPLICIT, CELL) |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 7 | |
| 8 | import weakref |
| 9 | |
Benjamin Peterson | 3938a90 | 2008-08-20 02:33:00 +0000 | [diff] [blame] | 10 | __all__ = ["symtable", "SymbolTable", "Class", "Function", "Symbol"] |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 11 | |
| 12 | def symtable(code, filename, compile_type): |
Benjamin Peterson | 89d8cd9 | 2013-10-26 13:13:51 -0400 | [diff] [blame] | 13 | top = _symtable.symtable(code, filename, compile_type) |
Benjamin Peterson | 3938a90 | 2008-08-20 02:33:00 +0000 | [diff] [blame] | 14 | return _newSymbolTable(top, filename) |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 15 | |
| 16 | class SymbolTableFactory: |
| 17 | def __init__(self): |
| 18 | self.__memo = weakref.WeakValueDictionary() |
| 19 | |
| 20 | def new(self, table, filename): |
| 21 | if table.type == _symtable.TYPE_FUNCTION: |
| 22 | return Function(table, filename) |
| 23 | if table.type == _symtable.TYPE_CLASS: |
| 24 | return Class(table, filename) |
| 25 | return SymbolTable(table, filename) |
| 26 | |
| 27 | def __call__(self, table, filename): |
| 28 | key = table, filename |
| 29 | obj = self.__memo.get(key, None) |
| 30 | if obj is None: |
| 31 | obj = self.__memo[key] = self.new(table, filename) |
| 32 | return obj |
| 33 | |
Benjamin Peterson | 3938a90 | 2008-08-20 02:33:00 +0000 | [diff] [blame] | 34 | _newSymbolTable = SymbolTableFactory() |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 35 | |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 36 | |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 37 | class SymbolTable(object): |
| 38 | |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 39 | def __init__(self, raw_table, filename): |
| 40 | self._table = raw_table |
| 41 | self._filename = filename |
| 42 | self._symbols = {} |
| 43 | |
| 44 | def __repr__(self): |
| 45 | if self.__class__ == SymbolTable: |
| 46 | kind = "" |
| 47 | else: |
| 48 | kind = "%s " % self.__class__.__name__ |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 49 | |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 50 | if self._table.name == "global": |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 51 | return "<{0}SymbolTable for module {1}>".format(kind, self._filename) |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 52 | else: |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 53 | return "<{0}SymbolTable for {1} in {2}>".format(kind, |
| 54 | self._table.name, |
| 55 | self._filename) |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 56 | |
| 57 | def get_type(self): |
| 58 | if self._table.type == _symtable.TYPE_MODULE: |
| 59 | return "module" |
| 60 | if self._table.type == _symtable.TYPE_FUNCTION: |
| 61 | return "function" |
| 62 | if self._table.type == _symtable.TYPE_CLASS: |
| 63 | return "class" |
| 64 | assert self._table.type in (1, 2, 3), \ |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 65 | "unexpected type: {0}".format(self._table.type) |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 66 | |
| 67 | def get_id(self): |
| 68 | return self._table.id |
| 69 | |
| 70 | def get_name(self): |
| 71 | return self._table.name |
| 72 | |
| 73 | def get_lineno(self): |
| 74 | return self._table.lineno |
| 75 | |
| 76 | def is_optimized(self): |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 77 | return bool(self._table.type == _symtable.TYPE_FUNCTION) |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 78 | |
| 79 | def is_nested(self): |
| 80 | return bool(self._table.nested) |
| 81 | |
| 82 | def has_children(self): |
| 83 | return bool(self._table.children) |
| 84 | |
| 85 | def has_exec(self): |
Guido van Rossum | 2def557 | 2006-09-06 22:37:15 +0000 | [diff] [blame] | 86 | """Return true if the scope uses exec. Deprecated method.""" |
| 87 | return False |
Jeremy Hylton | 5030cf1 | 2001-04-16 18:43:18 +0000 | [diff] [blame] | 88 | |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 89 | def get_identifiers(self): |
| 90 | return self._table.symbols.keys() |
| 91 | |
| 92 | def lookup(self, name): |
| 93 | sym = self._symbols.get(name) |
| 94 | if sym is None: |
| 95 | flags = self._table.symbols[name] |
| 96 | namespaces = self.__check_children(name) |
| 97 | sym = self._symbols[name] = Symbol(name, flags, namespaces) |
| 98 | return sym |
| 99 | |
| 100 | def get_symbols(self): |
| 101 | return [self.lookup(ident) for ident in self.get_identifiers()] |
| 102 | |
| 103 | def __check_children(self, name): |
Benjamin Peterson | 3938a90 | 2008-08-20 02:33:00 +0000 | [diff] [blame] | 104 | return [_newSymbolTable(st, self._filename) |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 105 | for st in self._table.children |
| 106 | if st.name == name] |
| 107 | |
Jeremy Hylton | 101651c | 2001-03-23 15:41:14 +0000 | [diff] [blame] | 108 | def get_children(self): |
Benjamin Peterson | 3938a90 | 2008-08-20 02:33:00 +0000 | [diff] [blame] | 109 | return [_newSymbolTable(st, self._filename) |
Jeremy Hylton | 101651c | 2001-03-23 15:41:14 +0000 | [diff] [blame] | 110 | for st in self._table.children] |
| 111 | |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 112 | |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 113 | class Function(SymbolTable): |
| 114 | |
| 115 | # Default values for instance variables |
| 116 | __params = None |
| 117 | __locals = None |
| 118 | __frees = None |
| 119 | __globals = None |
Pablo Galindo | d5b4f1b | 2018-10-20 01:46:00 +0100 | [diff] [blame] | 120 | __nonlocals = None |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 121 | |
| 122 | def __idents_matching(self, test_func): |
Jon Dufresne | 3972628 | 2017-05-18 07:35:54 -0700 | [diff] [blame] | 123 | return tuple(ident for ident in self.get_identifiers() |
| 124 | if test_func(self._table.symbols[ident])) |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 125 | |
| 126 | def get_parameters(self): |
| 127 | if self.__params is None: |
| 128 | self.__params = self.__idents_matching(lambda x:x & DEF_PARAM) |
| 129 | return self.__params |
| 130 | |
| 131 | def get_locals(self): |
| 132 | if self.__locals is None: |
Benjamin Peterson | 500c6ef | 2009-06-28 19:30:36 +0000 | [diff] [blame] | 133 | locs = (LOCAL, CELL) |
| 134 | test = lambda x: ((x >> SCOPE_OFF) & SCOPE_MASK) in locs |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 135 | self.__locals = self.__idents_matching(test) |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 136 | return self.__locals |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 137 | |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 138 | def get_globals(self): |
| 139 | if self.__globals is None: |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 140 | glob = (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT) |
| 141 | test = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) in glob |
| 142 | self.__globals = self.__idents_matching(test) |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 143 | return self.__globals |
| 144 | |
Pablo Galindo | d5b4f1b | 2018-10-20 01:46:00 +0100 | [diff] [blame] | 145 | def get_nonlocals(self): |
| 146 | if self.__nonlocals is None: |
| 147 | self.__nonlocals = self.__idents_matching(lambda x:x & DEF_NONLOCAL) |
| 148 | return self.__nonlocals |
| 149 | |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 150 | def get_frees(self): |
| 151 | if self.__frees is None: |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 152 | is_free = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) == FREE |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 153 | self.__frees = self.__idents_matching(is_free) |
| 154 | return self.__frees |
| 155 | |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 156 | |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 157 | class Class(SymbolTable): |
| 158 | |
| 159 | __methods = None |
| 160 | |
| 161 | def get_methods(self): |
| 162 | if self.__methods is None: |
| 163 | d = {} |
| 164 | for st in self._table.children: |
| 165 | d[st.name] = 1 |
Benjamin Peterson | b71caf1 | 2008-08-20 12:55:31 +0000 | [diff] [blame] | 166 | self.__methods = tuple(d) |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 167 | return self.__methods |
| 168 | |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 169 | |
| 170 | class Symbol(object): |
| 171 | |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 172 | def __init__(self, name, flags, namespaces=None): |
| 173 | self.__name = name |
| 174 | self.__flags = flags |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 175 | self.__scope = (flags >> SCOPE_OFF) & SCOPE_MASK # like PyST_GetScope() |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 176 | self.__namespaces = namespaces or () |
| 177 | |
| 178 | def __repr__(self): |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 179 | return "<symbol {0!r}>".format(self.__name) |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 180 | |
| 181 | def get_name(self): |
| 182 | return self.__name |
| 183 | |
| 184 | def is_referenced(self): |
| 185 | return bool(self.__flags & _symtable.USE) |
| 186 | |
| 187 | def is_parameter(self): |
| 188 | return bool(self.__flags & DEF_PARAM) |
| 189 | |
| 190 | def is_global(self): |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 191 | return bool(self.__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)) |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 192 | |
Pablo Galindo | d5b4f1b | 2018-10-20 01:46:00 +0100 | [diff] [blame] | 193 | def is_nonlocal(self): |
| 194 | return bool(self.__flags & DEF_NONLOCAL) |
| 195 | |
Jeremy Hylton | f37708e | 2009-03-31 15:26:37 +0000 | [diff] [blame] | 196 | def is_declared_global(self): |
| 197 | return bool(self.__scope == GLOBAL_EXPLICIT) |
| 198 | |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 199 | def is_local(self): |
| 200 | return bool(self.__flags & DEF_BOUND) |
| 201 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 202 | def is_annotated(self): |
| 203 | return bool(self.__flags & DEF_ANNOT) |
| 204 | |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 205 | def is_free(self): |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 206 | return bool(self.__scope == FREE) |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 207 | |
| 208 | def is_imported(self): |
| 209 | return bool(self.__flags & DEF_IMPORT) |
| 210 | |
| 211 | def is_assigned(self): |
| 212 | return bool(self.__flags & DEF_LOCAL) |
| 213 | |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 214 | def is_namespace(self): |
| 215 | """Returns true if name binding introduces new namespace. |
| 216 | |
| 217 | If the name is used as the target of a function or class |
| 218 | statement, this will be true. |
| 219 | |
| 220 | Note that a single name can be bound to multiple objects. If |
| 221 | is_namespace() is true, the name may also be bound to other |
| 222 | objects, like an int or list, that does not introduce a new |
| 223 | namespace. |
| 224 | """ |
| 225 | return bool(self.__namespaces) |
| 226 | |
| 227 | def get_namespaces(self): |
| 228 | """Return a list of namespaces bound to this name""" |
| 229 | return self.__namespaces |
| 230 | |
| 231 | def get_namespace(self): |
| 232 | """Returns the single namespace bound to this name. |
| 233 | |
| 234 | Raises ValueError if the name is bound to multiple namespaces. |
| 235 | """ |
| 236 | if len(self.__namespaces) != 1: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 237 | raise ValueError("name is bound to multiple namespaces") |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 238 | return self.__namespaces[0] |
| 239 | |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 240 | if __name__ == "__main__": |
| 241 | import os, sys |
Giampaolo Rodola' | 2f50aaf | 2013-02-12 02:04:27 +0100 | [diff] [blame] | 242 | with open(sys.argv[0]) as f: |
| 243 | src = f.read() |
Jeremy Hylton | 816e149 | 2001-03-22 23:32:22 +0000 | [diff] [blame] | 244 | mod = symtable(src, os.path.split(sys.argv[0])[1], "exec") |
| 245 | for ident in mod.get_identifiers(): |
| 246 | info = mod.lookup(ident) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 247 | print(info, info.is_local(), info.is_namespace()) |