1.1 --- a/micropython/__init__.py Sat Sep 10 20:33:52 2011 +0200
1.2 +++ b/micropython/__init__.py Mon Sep 12 23:08:54 2011 +0200
1.3 @@ -38,8 +38,8 @@
1.4 """
1.5
1.6 from micropython.common import *
1.7 +from micropython.data import *
1.8 import micropython.ast
1.9 -import micropython.data
1.10 import micropython.opt
1.11 import micropython.inspect
1.12 import micropython.table
1.13 @@ -143,7 +143,7 @@
1.14 # Append classes and functions to the image.
1.15
1.16 for obj in module.all_objects:
1.17 - if isinstance(obj, micropython.data.Class):
1.18 + if isinstance(obj, Class):
1.19
1.20 # Add header details.
1.21
1.22 @@ -169,7 +169,7 @@
1.23 # level, and the code location is set within the code
1.24 # generation process for the module.
1.25
1.26 - elif isinstance(obj, micropython.data.Function):
1.27 + elif isinstance(obj, Function):
1.28
1.29 # Add header details.
1.30
1.31 @@ -264,35 +264,53 @@
1.32 if self.objtable is None or reset:
1.33
1.34 t = self.objtable = micropython.table.ObjectTable()
1.35 +
1.36 + # First, get all active modules and classes.
1.37 +
1.38 + all_objects = set()
1.39 +
1.40 for module in self.importer.get_modules():
1.41 + all_objects.add(module)
1.42 + for obj in module.all_objects:
1.43 + if isinstance(obj, Class):
1.44 + all_objects.add(obj)
1.45 +
1.46 + # Then, visit the modules and classes.
1.47 +
1.48 + for obj in all_objects:
1.49
1.50 # Add module attributes and module identity information.
1.51
1.52 - full_name = module.full_name()
1.53 - attributes = {full_name : module}
1.54 - attributes.update(module.module_attributes())
1.55 - t.add(full_name, attributes)
1.56 + if isinstance(obj, Module):
1.57 + full_name = obj.full_name()
1.58 + attributes = {full_name : obj}
1.59 + attributes.update(obj.module_attributes())
1.60 + t.add(full_name, attributes)
1.61
1.62 # Add class and instance attributes for all classes, together
1.63 # with descendant information.
1.64
1.65 - for obj in module.all_objects:
1.66 - if isinstance(obj, micropython.data.Class):
1.67 + elif isinstance(obj, Class):
1.68 +
1.69 + # Prevent ambiguous classes.
1.70
1.71 - # Prevent ambiguous classes.
1.72 + full_name = obj.full_name()
1.73
1.74 - full_name = obj.full_name()
1.75 + #if obj.module.has_key(name) and obj.module[name].defines_ambiguous_class():
1.76 + # raise TableGenerationError, "Class %r in module %r is ambiguously defined." % (name, obj.module.full_name())
1.77 +
1.78 + # Define a table entry for the class.
1.79
1.80 - #name = obj.name
1.81 - #if module.has_key(name) and module[name].defines_ambiguous_class():
1.82 - # raise TableGenerationError, "Class %r in module %r is ambiguously defined." % (name, module.full_name())
1.83 + attributes = {full_name : obj}
1.84 + attributes.update(obj.all_attributes())
1.85 +
1.86 + # Filter out unused classes.
1.87
1.88 - # Define a table entry for the class.
1.89 + for name, descendant in obj.all_descendants().items():
1.90 + if descendant in all_objects:
1.91 + attributes[name] = descendant
1.92
1.93 - attributes = {full_name : obj}
1.94 - attributes.update(obj.all_attributes())
1.95 - attributes.update(obj.all_descendants())
1.96 - t.add(full_name, attributes)
1.97 + t.add(full_name, attributes)
1.98
1.99 return self.objtable
1.100
1.101 @@ -309,14 +327,14 @@
1.102
1.103 for module in self.importer.get_modules():
1.104 for obj in module.all_objects:
1.105 - if isinstance(obj, micropython.data.Function):
1.106 + if isinstance(obj, Function):
1.107 t.add(obj.full_name(), obj.parameters())
1.108
1.109 # Classes are callable, too.
1.110 # Take details of the appropriate __init__ method to make an
1.111 # entry for an instantiation function for the class.
1.112
1.113 - elif isinstance(obj, micropython.data.Class):
1.114 + elif isinstance(obj, Class):
1.115 t.add(obj.get_instantiator().full_name(), obj.get_instantiator().parameters())
1.116
1.117 # Filter out all parameter table entries not referenced by keyword
1.118 @@ -692,7 +710,7 @@
1.119 parent = attr.parent
1.120 if parent is None:
1.121 continue
1.122 - elif isinstance(parent, micropython.data.Instance):
1.123 + elif isinstance(parent, Instance):
1.124 parentname = objname
1.125 else:
1.126 parentname = parent.full_name()
1.127 @@ -706,11 +724,11 @@
1.128 # NOTE: Here, an instance actually represents any kind
1.129 # NOTE: of object.
1.130
1.131 - if isinstance(parent, micropython.data.Instance):
1.132 + if isinstance(parent, Instance):
1.133 for attrvalue in attrvalues:
1.134 for name in objtable.any_possible_objects([attrname]):
1.135 parent = objtable.access(name, name)
1.136 - if not isinstance(parent, micropython.data.Class) or \
1.137 + if not isinstance(parent, Class) or \
1.138 not parent.instance_attributes().has_key(attrname):
1.139 parent.set(attrname, attrvalue, 0)
1.140 else:
1.141 @@ -759,7 +777,7 @@
1.142
1.143 "Return a constant for the given 'value'."
1.144
1.145 - const = micropython.data.Const(value)
1.146 + const = Const(value)
1.147 return self.constant_values[const]
1.148
1.149 def get_constant_type_name(self, value):
1.150 @@ -774,7 +792,7 @@
1.151
1.152 # Make a constant object and return it.
1.153
1.154 - const = micropython.data.Const(value)
1.155 + const = Const(value)
1.156 if not self.constant_values.has_key(const):
1.157 self.constant_values[const] = const
1.158 return self.constant_values[const]