blob: 9ff27ef74ffe83e06cdb734e8e7a42df4213adbf [file] [log] [blame]
Jeremy Hylton816e1492001-03-22 23:32:22 +00001"""Interface to the compiler's internal symbol tables"""
Jeremy Hylton816e1492001-03-22 23:32:22 +00002
3import _symtable
Pablo Galindod5b4f1b2018-10-20 01:46:00 +01004from _symtable import (USE, DEF_GLOBAL, DEF_NONLOCAL, DEF_LOCAL, DEF_PARAM,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005 DEF_IMPORT, DEF_BOUND, DEF_ANNOT, SCOPE_OFF, SCOPE_MASK, FREE,
Benjamin Peterson500c6ef2009-06-28 19:30:36 +00006 LOCAL, GLOBAL_IMPLICIT, GLOBAL_EXPLICIT, CELL)
Jeremy Hylton816e1492001-03-22 23:32:22 +00007
8import weakref
9
Benjamin Peterson3938a902008-08-20 02:33:00 +000010__all__ = ["symtable", "SymbolTable", "Class", "Function", "Symbol"]
Jeremy Hylton816e1492001-03-22 23:32:22 +000011
12def symtable(code, filename, compile_type):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -030013 """ Return the toplevel *SymbolTable* for the source code.
14
15 *filename* is the name of the file with the code
16 and *compile_type* is the *compile()* mode argument.
17 """
Benjamin Peterson89d8cd92013-10-26 13:13:51 -040018 top = _symtable.symtable(code, filename, compile_type)
Benjamin Peterson3938a902008-08-20 02:33:00 +000019 return _newSymbolTable(top, filename)
Jeremy Hylton816e1492001-03-22 23:32:22 +000020
21class SymbolTableFactory:
22 def __init__(self):
23 self.__memo = weakref.WeakValueDictionary()
24
25 def new(self, table, filename):
26 if table.type == _symtable.TYPE_FUNCTION:
27 return Function(table, filename)
28 if table.type == _symtable.TYPE_CLASS:
29 return Class(table, filename)
30 return SymbolTable(table, filename)
31
32 def __call__(self, table, filename):
33 key = table, filename
34 obj = self.__memo.get(key, None)
35 if obj is None:
36 obj = self.__memo[key] = self.new(table, filename)
37 return obj
38
Benjamin Peterson3938a902008-08-20 02:33:00 +000039_newSymbolTable = SymbolTableFactory()
Tim Petersa19a1682001-03-29 04:36:09 +000040
Jeremy Hylton816e1492001-03-22 23:32:22 +000041
Benjamin Peterson55e00f22008-08-17 18:02:44 +000042class SymbolTable(object):
43
Jeremy Hylton816e1492001-03-22 23:32:22 +000044 def __init__(self, raw_table, filename):
45 self._table = raw_table
46 self._filename = filename
47 self._symbols = {}
48
49 def __repr__(self):
50 if self.__class__ == SymbolTable:
51 kind = ""
52 else:
53 kind = "%s " % self.__class__.__name__
Tim Petersa19a1682001-03-29 04:36:09 +000054
Jeremy Hylton816e1492001-03-22 23:32:22 +000055 if self._table.name == "global":
Benjamin Peterson55e00f22008-08-17 18:02:44 +000056 return "<{0}SymbolTable for module {1}>".format(kind, self._filename)
Jeremy Hylton816e1492001-03-22 23:32:22 +000057 else:
Benjamin Peterson55e00f22008-08-17 18:02:44 +000058 return "<{0}SymbolTable for {1} in {2}>".format(kind,
59 self._table.name,
60 self._filename)
Jeremy Hylton816e1492001-03-22 23:32:22 +000061
62 def get_type(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -030063 """Return the type of the symbol table.
64
65 The values retuned are 'class', 'module' and
66 'function'.
67 """
Jeremy Hylton816e1492001-03-22 23:32:22 +000068 if self._table.type == _symtable.TYPE_MODULE:
69 return "module"
70 if self._table.type == _symtable.TYPE_FUNCTION:
71 return "function"
72 if self._table.type == _symtable.TYPE_CLASS:
73 return "class"
74 assert self._table.type in (1, 2, 3), \
Benjamin Peterson55e00f22008-08-17 18:02:44 +000075 "unexpected type: {0}".format(self._table.type)
Jeremy Hylton816e1492001-03-22 23:32:22 +000076
77 def get_id(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -030078 """Return an identifier for the table.
79 """
Jeremy Hylton816e1492001-03-22 23:32:22 +000080 return self._table.id
81
82 def get_name(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -030083 """Return the table's name.
84
85 This corresponds to the name of the class, function
86 or 'top' if the table is for a class, function or
87 global respectively.
88 """
Jeremy Hylton816e1492001-03-22 23:32:22 +000089 return self._table.name
90
91 def get_lineno(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -030092 """Return the number of the first line in the
93 block for the table.
94 """
Jeremy Hylton816e1492001-03-22 23:32:22 +000095 return self._table.lineno
96
97 def is_optimized(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -030098 """Return *True* if the locals in the table
99 are optimizable.
100 """
Benjamin Peterson1dfd2472015-04-27 21:44:22 -0400101 return bool(self._table.type == _symtable.TYPE_FUNCTION)
Jeremy Hylton816e1492001-03-22 23:32:22 +0000102
103 def is_nested(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300104 """Return *True* if the block is a nested class
105 or function."""
Jeremy Hylton816e1492001-03-22 23:32:22 +0000106 return bool(self._table.nested)
107
108 def has_children(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300109 """Return *True* if the block has nested namespaces.
110 """
Jeremy Hylton816e1492001-03-22 23:32:22 +0000111 return bool(self._table.children)
112
Jeremy Hylton816e1492001-03-22 23:32:22 +0000113 def get_identifiers(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300114 """Return a list of names of symbols in the table.
115 """
Jeremy Hylton816e1492001-03-22 23:32:22 +0000116 return self._table.symbols.keys()
117
118 def lookup(self, name):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300119 """Lookup a *name* in the table.
120
121 Returns a *Symbol* instance.
122 """
Jeremy Hylton816e1492001-03-22 23:32:22 +0000123 sym = self._symbols.get(name)
124 if sym is None:
125 flags = self._table.symbols[name]
126 namespaces = self.__check_children(name)
127 sym = self._symbols[name] = Symbol(name, flags, namespaces)
128 return sym
129
130 def get_symbols(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300131 """Return a list of *Symbol* instances for
132 names in the table.
133 """
Jeremy Hylton816e1492001-03-22 23:32:22 +0000134 return [self.lookup(ident) for ident in self.get_identifiers()]
135
136 def __check_children(self, name):
Benjamin Peterson3938a902008-08-20 02:33:00 +0000137 return [_newSymbolTable(st, self._filename)
Jeremy Hylton816e1492001-03-22 23:32:22 +0000138 for st in self._table.children
139 if st.name == name]
140
Jeremy Hylton101651c2001-03-23 15:41:14 +0000141 def get_children(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300142 """Return a list of the nested symbol tables.
143 """
Benjamin Peterson3938a902008-08-20 02:33:00 +0000144 return [_newSymbolTable(st, self._filename)
Jeremy Hylton101651c2001-03-23 15:41:14 +0000145 for st in self._table.children]
146
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000147
Jeremy Hylton816e1492001-03-22 23:32:22 +0000148class Function(SymbolTable):
149
150 # Default values for instance variables
151 __params = None
152 __locals = None
153 __frees = None
154 __globals = None
Pablo Galindod5b4f1b2018-10-20 01:46:00 +0100155 __nonlocals = None
Jeremy Hylton816e1492001-03-22 23:32:22 +0000156
157 def __idents_matching(self, test_func):
Jon Dufresne39726282017-05-18 07:35:54 -0700158 return tuple(ident for ident in self.get_identifiers()
159 if test_func(self._table.symbols[ident]))
Jeremy Hylton816e1492001-03-22 23:32:22 +0000160
161 def get_parameters(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300162 """Return a tuple of parameters to the function.
163 """
Jeremy Hylton816e1492001-03-22 23:32:22 +0000164 if self.__params is None:
165 self.__params = self.__idents_matching(lambda x:x & DEF_PARAM)
166 return self.__params
167
168 def get_locals(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300169 """Return a tuple of locals in the function.
170 """
Jeremy Hylton816e1492001-03-22 23:32:22 +0000171 if self.__locals is None:
Benjamin Peterson500c6ef2009-06-28 19:30:36 +0000172 locs = (LOCAL, CELL)
173 test = lambda x: ((x >> SCOPE_OFF) & SCOPE_MASK) in locs
Benjamin Peterson78565b22009-06-28 19:19:51 +0000174 self.__locals = self.__idents_matching(test)
Jeremy Hylton816e1492001-03-22 23:32:22 +0000175 return self.__locals
Tim Petersa19a1682001-03-29 04:36:09 +0000176
Jeremy Hylton816e1492001-03-22 23:32:22 +0000177 def get_globals(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300178 """Return a tuple of globals in the function.
179 """
Jeremy Hylton816e1492001-03-22 23:32:22 +0000180 if self.__globals is None:
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000181 glob = (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)
182 test = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) in glob
183 self.__globals = self.__idents_matching(test)
Jeremy Hylton816e1492001-03-22 23:32:22 +0000184 return self.__globals
185
Pablo Galindod5b4f1b2018-10-20 01:46:00 +0100186 def get_nonlocals(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300187 """Return a tuple of nonlocals in the function.
188 """
Pablo Galindod5b4f1b2018-10-20 01:46:00 +0100189 if self.__nonlocals is None:
190 self.__nonlocals = self.__idents_matching(lambda x:x & DEF_NONLOCAL)
191 return self.__nonlocals
192
Jeremy Hylton816e1492001-03-22 23:32:22 +0000193 def get_frees(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300194 """Return a tuple of free variables in the function.
195 """
Jeremy Hylton816e1492001-03-22 23:32:22 +0000196 if self.__frees is None:
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000197 is_free = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) == FREE
Jeremy Hylton816e1492001-03-22 23:32:22 +0000198 self.__frees = self.__idents_matching(is_free)
199 return self.__frees
200
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000201
Jeremy Hylton816e1492001-03-22 23:32:22 +0000202class Class(SymbolTable):
203
204 __methods = None
205
206 def get_methods(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300207 """Return a tuple of methods declared in the class.
208 """
Jeremy Hylton816e1492001-03-22 23:32:22 +0000209 if self.__methods is None:
210 d = {}
211 for st in self._table.children:
212 d[st.name] = 1
Benjamin Petersonb71caf12008-08-20 12:55:31 +0000213 self.__methods = tuple(d)
Jeremy Hylton816e1492001-03-22 23:32:22 +0000214 return self.__methods
215
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000216
217class Symbol(object):
218
Jeremy Hylton816e1492001-03-22 23:32:22 +0000219 def __init__(self, name, flags, namespaces=None):
220 self.__name = name
221 self.__flags = flags
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000222 self.__scope = (flags >> SCOPE_OFF) & SCOPE_MASK # like PyST_GetScope()
Jeremy Hylton816e1492001-03-22 23:32:22 +0000223 self.__namespaces = namespaces or ()
224
225 def __repr__(self):
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000226 return "<symbol {0!r}>".format(self.__name)
Jeremy Hylton816e1492001-03-22 23:32:22 +0000227
228 def get_name(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300229 """Return a name of a symbol.
230 """
Jeremy Hylton816e1492001-03-22 23:32:22 +0000231 return self.__name
232
233 def is_referenced(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300234 """Return *True* if the symbol is used in
235 its block.
236 """
Jeremy Hylton816e1492001-03-22 23:32:22 +0000237 return bool(self.__flags & _symtable.USE)
238
239 def is_parameter(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300240 """Return *True* if the symbol is a parameter.
241 """
Jeremy Hylton816e1492001-03-22 23:32:22 +0000242 return bool(self.__flags & DEF_PARAM)
243
244 def is_global(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300245 """Return *True* if the sysmbol is global.
246 """
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000247 return bool(self.__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT))
Jeremy Hylton816e1492001-03-22 23:32:22 +0000248
Pablo Galindod5b4f1b2018-10-20 01:46:00 +0100249 def is_nonlocal(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300250 """Return *True* if the symbol is nonlocal."""
Pablo Galindod5b4f1b2018-10-20 01:46:00 +0100251 return bool(self.__flags & DEF_NONLOCAL)
252
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000253 def is_declared_global(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300254 """Return *True* if the symbol is declared global
255 with a global statement."""
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000256 return bool(self.__scope == GLOBAL_EXPLICIT)
257
Jeremy Hylton816e1492001-03-22 23:32:22 +0000258 def is_local(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300259 """Return *True* if the symbol is local.
260 """
Pablo Galindo799d7d62020-04-06 17:05:57 +0100261 return bool(self.__scope in (LOCAL, CELL))
Jeremy Hylton816e1492001-03-22 23:32:22 +0000262
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700263 def is_annotated(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300264 """Return *True* if the symbol is annotated.
265 """
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700266 return bool(self.__flags & DEF_ANNOT)
267
Jeremy Hylton816e1492001-03-22 23:32:22 +0000268 def is_free(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300269 """Return *True* if a referenced symbol is
270 not assigned to.
271 """
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000272 return bool(self.__scope == FREE)
Jeremy Hylton816e1492001-03-22 23:32:22 +0000273
274 def is_imported(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300275 """Return *True* if the symbol is created from
276 an import statement.
277 """
Jeremy Hylton816e1492001-03-22 23:32:22 +0000278 return bool(self.__flags & DEF_IMPORT)
279
280 def is_assigned(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300281 """Return *True* if a symbol is assigned to."""
Jeremy Hylton816e1492001-03-22 23:32:22 +0000282 return bool(self.__flags & DEF_LOCAL)
283
Jeremy Hylton816e1492001-03-22 23:32:22 +0000284 def is_namespace(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300285 """Returns *True* if name binding introduces new namespace.
Jeremy Hylton816e1492001-03-22 23:32:22 +0000286
287 If the name is used as the target of a function or class
288 statement, this will be true.
289
290 Note that a single name can be bound to multiple objects. If
291 is_namespace() is true, the name may also be bound to other
292 objects, like an int or list, that does not introduce a new
293 namespace.
294 """
295 return bool(self.__namespaces)
296
297 def get_namespaces(self):
298 """Return a list of namespaces bound to this name"""
299 return self.__namespaces
300
301 def get_namespace(self):
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300302 """Return the single namespace bound to this name.
Jeremy Hylton816e1492001-03-22 23:32:22 +0000303
304 Raises ValueError if the name is bound to multiple namespaces.
305 """
306 if len(self.__namespaces) != 1:
Collin Winterce36ad82007-08-30 01:19:48 +0000307 raise ValueError("name is bound to multiple namespaces")
Jeremy Hylton816e1492001-03-22 23:32:22 +0000308 return self.__namespaces[0]
309
Jeremy Hylton816e1492001-03-22 23:32:22 +0000310if __name__ == "__main__":
311 import os, sys
Giampaolo Rodola'2f50aaf2013-02-12 02:04:27 +0100312 with open(sys.argv[0]) as f:
313 src = f.read()
Jeremy Hylton816e1492001-03-22 23:32:22 +0000314 mod = symtable(src, os.path.split(sys.argv[0])[1], "exec")
315 for ident in mod.get_identifiers():
316 info = mod.lookup(ident)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000317 print(info, info.is_local(), info.is_namespace())