1.1 --- a/TO_DO.txt Sun Jul 03 20:11:13 2011 +0200
1.2 +++ b/TO_DO.txt Sun Jul 03 22:51:51 2011 +0200
1.3 @@ -137,8 +137,6 @@
1.4 Object Coverage
1.5 ===============
1.6
1.7 -Incorporate constants into the coverage, eliminating unused constants.
1.8 -
1.9 Support __init__ traversal (and other implicit names) more effectively.
1.10
1.11 Other
2.1 --- a/micropython/__init__.py Sun Jul 03 20:11:13 2011 +0200
2.2 +++ b/micropython/__init__.py Sun Jul 03 22:51:51 2011 +0200
2.3 @@ -376,7 +376,8 @@
2.4 # Constant records.
2.5
2.6 self.constant_values = {}
2.7 - self.constant_list = None # cache for constants
2.8 + self.constants_used = set()
2.9 + self.constant_references = {}
2.10 self.init_predefined_constants()
2.11
2.12 # Attribute usage.
2.13 @@ -579,6 +580,11 @@
2.14
2.15 self.attribute_users_visited.add(from_name)
2.16
2.17 + # Get constant references.
2.18 +
2.19 + for const in self.constant_references.get(from_name, []):
2.20 + self.constants_used.add(const)
2.21 +
2.22 # The getattr function is a special case: it can potentially reference
2.23 # any known attribute. Since accessor attributes must be known
2.24 # constants, the intersection of known constants and attributes is used
2.25 @@ -709,12 +715,24 @@
2.26
2.27 # Constant accounting.
2.28
2.29 + def use_constant(self, const, from_name):
2.30 +
2.31 + """
2.32 + Register the given 'const' as being used in the program from within an
2.33 + object with the specified 'from_name'.
2.34 + """
2.35 +
2.36 + if not self.constant_references.has_key(from_name):
2.37 + self.constant_references[from_name] = set()
2.38 +
2.39 + self.constant_references[from_name].add(const)
2.40 +
2.41 def init_predefined_constants(self):
2.42
2.43 "Ensure the predefined constants."
2.44
2.45 for name, value in self.predefined_constants.items():
2.46 - self.make_constant(value)
2.47 + self.constants_used.add(self.make_constant(value))
2.48
2.49 def get_predefined_constant(self, name):
2.50
2.51 @@ -750,10 +768,7 @@
2.52
2.53 "Return a list of constants."
2.54
2.55 - if self.constant_list is None:
2.56 - self.constant_list = list(self.constant_values.values())
2.57 -
2.58 - return self.constant_list
2.59 + return self.constants_used
2.60
2.61 # Import methods.
2.62
3.1 --- a/micropython/inspect.py Sun Jul 03 20:11:13 2011 +0200
3.2 +++ b/micropython/inspect.py Sun Jul 03 22:51:51 2011 +0200
3.3 @@ -345,6 +345,13 @@
3.4 unit = self.get_namespace()
3.5 self.importer.use_name(name, unit.full_name(), value)
3.6
3.7 + def use_constant(self, const):
3.8 +
3.9 + "Use the given 'const' within the current namespace/unit."
3.10 +
3.11 + unit = self.get_namespace()
3.12 + self.importer.use_constant(const, unit.full_name())
3.13 +
3.14 # Attribute usage methods.
3.15 # These are convenience methods which refer to the specific namespace's
3.16 # implementation of these operations.
3.17 @@ -511,7 +518,9 @@
3.18 """
3.19
3.20 self.use_specific_attribute("__builtins__", self.importer.get_constant_type_name(value))
3.21 - return self.importer.make_constant(value)
3.22 + const = self.importer.make_constant(value)
3.23 + self.use_constant(const)
3.24 + return const
3.25
3.26 def _visitFunction(self, node, name):
3.27