# HG changeset patch # User Paul Boddie # Date 1226182188 -3600 # Node ID 5272000fc62e64a3e5b70fe5bb16e9b114b1e9f1 # Parent 01b3a070af34fe323757c651adb856e7753020fe Added name accounting. diff -r 01b3a070af34 -r 5272000fc62e micropython/__init__.py --- a/micropython/__init__.py Sat Nov 08 21:47:26 2008 +0100 +++ b/micropython/__init__.py Sat Nov 08 23:09:48 2008 +0100 @@ -94,11 +94,25 @@ self.constant_list = None # cache for constants self.init_predefined_constants() + # Name records (used to track actual use of names). + + self.names_used = set() + # Main program information. self.code = None self.code_location = None + # Name accounting. + + def use_name(self, name): + + "Register the given 'name' as being used in the program." + + self.names_used.add(name) + + # Constant accounting. + def init_predefined_constants(self): "Ensure the predefined constants." @@ -136,6 +150,8 @@ return self.constant_list + # General maintenance. + def vacuum(self): "Tidy up the modules." @@ -160,6 +176,8 @@ if isinstance(obj, (micropython.inspect.Class, micropython.inspect.Function)): obj.finalise_attributes() + # Access to finalised program information. + def get_modules(self): "Return all modules known to the importer." diff -r 01b3a070af34 -r 5272000fc62e micropython/inspect.py --- a/micropython/inspect.py Sat Nov 08 21:47:26 2008 +0100 +++ b/micropython/inspect.py Sat Nov 08 23:09:48 2008 +0100 @@ -150,9 +150,9 @@ del self[name] # Remove unreferenced names. - # NOTE: This, due to the nature of the referenced attribute, assumes - # NOTE: that only explicitly mentioned classes and functions are - # NOTE: employed in the final program. + # This, due to the nature of the referenced attribute, assumes + # that only explicitly mentioned classes and functions are + # employed in the final program. elif self.should_optimise_unused_objects(): @@ -526,22 +526,28 @@ def visitGetattr(self, node): expr = self.dispatch(node.expr) + attrname = node.attrname + if isinstance(expr, Attr): value = expr.value if isinstance(value, (Class, Module)): - attr = value.namespace.get(node.attrname) + attr = value.namespace.get(attrname) elif isinstance(value, UnresolvedName): - attr = UnresolvedName(node.attrname, value.full_name(), self) + attr = UnresolvedName(attrname, value.full_name(), self) else: attr = None elif self.builtins is not None: - attr = self.builtins.get(node.attrname) + attr = self.builtins.get(attrname) else: - attr = UnresolvedName(node.attrname, value.full_name(), self) + attr = UnresolvedName(attrname, value.full_name(), self) + + # Accounting. if attr is not None: attr.set_referenced() + self.importer.use_name(attrname) + return attr def visitGlobal(self, node): @@ -609,6 +615,7 @@ def visitName(self, node): name = node.name + if self.importer.predefined_constants.has_key(name): attr = self.importer.get_predefined_constant(name) elif self.namespaces and self.namespaces[-1].has_key(name): @@ -620,9 +627,13 @@ else: attr = None + # Accounting. + if attr is not None: attr.set_referenced() + self.importer.use_name(name) + return attr visitNot = OP