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