paulb@213 | 1 | #!/usr/bin/env python |
paulb@213 | 2 | |
paulb@213 | 3 | """ |
paulb@213 | 4 | Simplified program data. |
paulb@213 | 5 | |
paulb@213 | 6 | Copyright (C) 2006, 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@213 | 7 | |
paulb@213 | 8 | This software is free software; you can redistribute it and/or |
paulb@213 | 9 | modify it under the terms of the GNU General Public License as |
paulb@213 | 10 | published by the Free Software Foundation; either version 2 of |
paulb@213 | 11 | the License, or (at your option) any later version. |
paulb@213 | 12 | |
paulb@213 | 13 | This software is distributed in the hope that it will be useful, |
paulb@213 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@213 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paulb@213 | 16 | GNU General Public License for more details. |
paulb@213 | 17 | |
paulb@213 | 18 | You should have received a copy of the GNU General Public |
paulb@213 | 19 | License along with this library; see the file LICENCE.txt |
paulb@213 | 20 | If not, write to the Free Software Foundation, Inc., |
paulb@213 | 21 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@213 | 22 | """ |
paulb@213 | 23 | |
paulb@221 | 24 | from simplify.simplified.utils import Structure, WithName, name |
paulb@222 | 25 | from simplify.simplified.program import Subprogram |
paulb@213 | 26 | |
paulb@248 | 27 | # Housekeeping. |
paulb@248 | 28 | |
paulb@248 | 29 | current_structures = set() |
paulb@248 | 30 | |
paulb@213 | 31 | # Special non-program nodes. |
paulb@213 | 32 | |
paulb@249 | 33 | class GeneralClass(Structure, WithName): |
paulb@213 | 34 | |
paulb@234 | 35 | """ |
paulb@234 | 36 | The basis of a Python class. Classes with specific instantiation behaviour |
paulb@234 | 37 | should inherit from this class. |
paulb@234 | 38 | """ |
paulb@213 | 39 | |
paulb@213 | 40 | def __init__(self, *args, **kw): |
paulb@213 | 41 | Structure.__init__(self, *args, **kw) |
paulb@213 | 42 | WithName.__init__(self) |
paulb@213 | 43 | |
paulb@213 | 44 | def full_name(self): |
paulb@213 | 45 | return "class %s" % self._full_name |
paulb@213 | 46 | |
paulb@234 | 47 | # Utility methods. |
paulb@234 | 48 | |
paulb@233 | 49 | def get_instance_attribute_names(self): |
paulb@233 | 50 | |
paulb@233 | 51 | "Return all attribute names used by the instances of this class." |
paulb@233 | 52 | |
paulb@233 | 53 | names = set() |
paulb@244 | 54 | for instance in self.get_instances(): |
paulb@233 | 55 | for name in instance.namespace.keys(): |
paulb@233 | 56 | names.add(name) |
paulb@233 | 57 | return names |
paulb@233 | 58 | |
paulb@235 | 59 | def get_names_to_instances(self, distinct=0): |
paulb@233 | 60 | |
paulb@233 | 61 | """ |
paulb@233 | 62 | Return a tuple containing a mapping from names to instances, and a list |
paulb@233 | 63 | of sorted instance names. |
paulb@233 | 64 | """ |
paulb@233 | 65 | |
paulb@233 | 66 | d = {} |
paulb@233 | 67 | names = [] |
paulb@235 | 68 | if distinct: |
paulb@235 | 69 | instances = set(self.get_distinct_instances().values()) |
paulb@235 | 70 | else: |
paulb@244 | 71 | instances = self.get_instances() |
paulb@235 | 72 | |
paulb@235 | 73 | for instance in instances: |
paulb@233 | 74 | name = instance.full_name() |
paulb@233 | 75 | names.append(name) |
paulb@233 | 76 | d[name] = instance |
paulb@235 | 77 | |
paulb@233 | 78 | names.sort() |
paulb@233 | 79 | return d, names |
paulb@233 | 80 | |
paulb@234 | 81 | def get_distinct_instances(self): |
paulb@234 | 82 | |
paulb@234 | 83 | """ |
paulb@235 | 84 | Return a dictionary mapping instances to a set of instances whose |
paulb@235 | 85 | attribute types are distinct. |
paulb@234 | 86 | """ |
paulb@234 | 87 | |
paulb@248 | 88 | in_current_structure = (self in current_structures) |
paulb@248 | 89 | current_structures.add(self) |
paulb@241 | 90 | |
paulb@235 | 91 | instances = {} |
paulb@234 | 92 | names_found = [] |
paulb@235 | 93 | instances_found = [] |
paulb@234 | 94 | |
paulb@234 | 95 | # Rather than use the instances directly, get them in name order in |
paulb@234 | 96 | # order to favour those earlier according to the sorting. |
paulb@234 | 97 | |
paulb@234 | 98 | names_to_instances, instance_names = self.get_names_to_instances() |
paulb@234 | 99 | |
paulb@234 | 100 | for instance_name in instance_names: |
paulb@234 | 101 | instance = names_to_instances[instance_name] |
paulb@248 | 102 | |
paulb@248 | 103 | if in_current_structure: |
paulb@248 | 104 | instances[instance] = instance |
paulb@248 | 105 | continue |
paulb@248 | 106 | |
paulb@234 | 107 | names = instance.namespace.names |
paulb@235 | 108 | try: |
paulb@235 | 109 | i = names_found.index(names) |
paulb@235 | 110 | instances[instance] = instances_found[i] |
paulb@235 | 111 | except ValueError: |
paulb@234 | 112 | names_found.append(names) |
paulb@235 | 113 | instances_found.append(instance) |
paulb@236 | 114 | instances[instance] = instance |
paulb@235 | 115 | |
paulb@248 | 116 | current_structures.remove(self) |
paulb@248 | 117 | |
paulb@234 | 118 | return instances |
paulb@234 | 119 | |
paulb@249 | 120 | class SingleInstanceClass(GeneralClass): |
paulb@213 | 121 | |
paulb@234 | 122 | "A Python class producing only one instance." |
paulb@213 | 123 | |
paulb@213 | 124 | def __init__(self, *args, **kw): |
paulb@249 | 125 | GeneralClass.__init__(self, *args, **kw) |
paulb@213 | 126 | self.instance = None |
paulb@213 | 127 | |
paulb@213 | 128 | def has_instance(self, node): |
paulb@213 | 129 | return self.instance is not None |
paulb@213 | 130 | |
paulb@213 | 131 | def add_instance(self, node, instance): |
paulb@213 | 132 | self.instance = instance |
paulb@213 | 133 | |
paulb@213 | 134 | def get_instance(self, node): |
paulb@213 | 135 | return self.instance |
paulb@213 | 136 | |
paulb@213 | 137 | def get_instance_name(self, instance): |
paulb@213 | 138 | return self._full_name |
paulb@213 | 139 | |
paulb@244 | 140 | def get_instances(self): |
paulb@244 | 141 | if self.instance is not None: |
paulb@244 | 142 | return [self.instance] |
paulb@244 | 143 | else: |
paulb@244 | 144 | return [] |
paulb@244 | 145 | |
paulb@213 | 146 | # Attribute propagation. |
paulb@213 | 147 | |
paulb@213 | 148 | def get_attribute_for_instance(self, attribute, instance): |
paulb@213 | 149 | return attribute |
paulb@213 | 150 | |
paulb@249 | 151 | class MultipleInstanceClass(GeneralClass): |
paulb@213 | 152 | |
paulb@234 | 153 | "A Python class producing many instances." |
paulb@213 | 154 | |
paulb@213 | 155 | def __init__(self, *args, **kw): |
paulb@249 | 156 | GeneralClass.__init__(self, *args, **kw) |
paulb@213 | 157 | self.instances = {} |
paulb@213 | 158 | self.attributes_for_instances = {} |
paulb@213 | 159 | |
paulb@213 | 160 | def _get_key(self, node): |
paulb@233 | 161 | return getattr(node, "original", None) # self.module.original |
paulb@213 | 162 | |
paulb@213 | 163 | def has_instance(self, node): |
paulb@213 | 164 | return self.instances.has_key(self._get_key(node)) |
paulb@213 | 165 | |
paulb@213 | 166 | def add_instance(self, node, instance): |
paulb@213 | 167 | self.instances[self._get_key(node)] = instance |
paulb@213 | 168 | |
paulb@213 | 169 | def get_instance(self, node): |
paulb@213 | 170 | return self.instances[self._get_key(node)] |
paulb@213 | 171 | |
paulb@213 | 172 | def get_instance_name(self, instance): |
paulb@213 | 173 | return name(instance, self._full_name) |
paulb@213 | 174 | |
paulb@244 | 175 | def get_instances(self): |
paulb@244 | 176 | return self.instances.values() |
paulb@244 | 177 | |
paulb@213 | 178 | # Attribute propagation. |
paulb@213 | 179 | |
paulb@213 | 180 | def get_attribute_for_instance(self, attribute, instance): |
paulb@213 | 181 | |
paulb@213 | 182 | # Create specialised methods. |
paulb@213 | 183 | |
paulb@213 | 184 | if isinstance(attribute.type, Subprogram): |
paulb@213 | 185 | subprogram = attribute.type |
paulb@213 | 186 | |
paulb@213 | 187 | # Each instance may have its own version of the subprogram. |
paulb@213 | 188 | |
paulb@213 | 189 | key = (subprogram, instance) |
paulb@213 | 190 | if not self.attributes_for_instances.has_key(key): |
paulb@213 | 191 | new_subprogram = subprogram.copy(instance, subprogram.full_name()) |
paulb@213 | 192 | subprogram.module.simplifier.subnames[new_subprogram.full_name()] = new_subprogram |
paulb@213 | 193 | self.attributes_for_instances[key] = Attribute(attribute.context, new_subprogram) |
paulb@213 | 194 | print "New subprogram", new_subprogram, "for", key |
paulb@213 | 195 | |
paulb@213 | 196 | return self.attributes_for_instances[key] |
paulb@213 | 197 | |
paulb@213 | 198 | # The original nodes are returned for other attributes. |
paulb@213 | 199 | |
paulb@213 | 200 | else: |
paulb@213 | 201 | return attribute |
paulb@213 | 202 | |
paulb@213 | 203 | class SelectiveMultipleInstanceClass(MultipleInstanceClass): |
paulb@213 | 204 | |
paulb@213 | 205 | "A Python class which provides multiple instances depending on the class." |
paulb@213 | 206 | |
paulb@213 | 207 | def _get_key(self, node): |
paulb@213 | 208 | if self.namespace.has_key("__atomic__"): |
paulb@233 | 209 | return self |
paulb@213 | 210 | else: |
paulb@213 | 211 | return MultipleInstanceClass._get_key(self, node) |
paulb@213 | 212 | |
paulb@213 | 213 | class ProlificMultipleInstanceClass(MultipleInstanceClass): |
paulb@213 | 214 | |
paulb@213 | 215 | """ |
paulb@213 | 216 | A Python class which provides multiple instances for different versions of |
paulb@213 | 217 | methods. In order to avoid unbounded instance production (since new |
paulb@213 | 218 | instances cause new copies of methods which in turn would cause new |
paulb@213 | 219 | instances), a relations dictionary is maintained which attempts to map |
paulb@213 | 220 | "requesting instances" to existing instances, suggesting such instances in |
paulb@213 | 221 | preference to new ones. |
paulb@213 | 222 | """ |
paulb@213 | 223 | |
paulb@213 | 224 | def __init__(self, *args, **kw): |
paulb@213 | 225 | MultipleInstanceClass.__init__(self, *args, **kw) |
paulb@213 | 226 | self.instance_relations = {} |
paulb@213 | 227 | |
paulb@213 | 228 | def _get_key(self, node): |
paulb@213 | 229 | if self.namespace.has_key("__atomic__"): |
paulb@233 | 230 | return self |
paulb@213 | 231 | else: |
paulb@233 | 232 | return node |
paulb@213 | 233 | |
paulb@213 | 234 | def has_instance(self, node): |
paulb@213 | 235 | requesting_instance = getattr(node, "instance", None) |
paulb@213 | 236 | #return requesting_instance is not None and requesting_instance.get_class() is self or \ |
paulb@213 | 237 | return self.instance_relations.has_key(requesting_instance) or self.instances.has_key(self._get_key(node)) |
paulb@213 | 238 | |
paulb@213 | 239 | def add_instance(self, node, instance): |
paulb@213 | 240 | requesting_instance = getattr(node, "instance", None) |
paulb@213 | 241 | print "New instance", instance, "for", id(node), requesting_instance |
paulb@213 | 242 | self.instances[self._get_key(node)] = instance |
paulb@213 | 243 | if requesting_instance is not None: |
paulb@213 | 244 | self.instance_relations[requesting_instance] = instance |
paulb@213 | 245 | requesting_instance.get_class().instance_relations[instance] = requesting_instance |
paulb@213 | 246 | |
paulb@213 | 247 | def get_instance(self, node): |
paulb@213 | 248 | requesting_instance = getattr(node, "instance", None) |
paulb@213 | 249 | #if requesting_instance is not None and requesting_instance.get_class() is self: |
paulb@213 | 250 | # return requesting_instance |
paulb@213 | 251 | return self.instance_relations.get(requesting_instance) or self.instances[self._get_key(node)] |
paulb@213 | 252 | |
paulb@213 | 253 | class Instance(Structure): |
paulb@213 | 254 | |
paulb@213 | 255 | "An instance." |
paulb@213 | 256 | |
paulb@213 | 257 | def full_name(self): |
paulb@213 | 258 | return self.get_class().get_instance_name(self) |
paulb@213 | 259 | |
paulb@213 | 260 | def get_class(self): |
paulb@213 | 261 | for n in self.namespace.load("__class__"): |
paulb@213 | 262 | return n.type |
paulb@213 | 263 | else: |
paulb@213 | 264 | raise ValueError, "__class__" |
paulb@213 | 265 | |
paulb@213 | 266 | class Constant: |
paulb@213 | 267 | |
paulb@213 | 268 | "A constant initialised with a type name for future processing." |
paulb@213 | 269 | |
paulb@213 | 270 | def __init__(self, name, value): |
paulb@213 | 271 | self.name = name |
paulb@213 | 272 | self.value = value |
paulb@213 | 273 | self.typename = self.value.__class__.__name__ |
paulb@213 | 274 | |
paulb@213 | 275 | class Attribute: |
paulb@213 | 276 | |
paulb@213 | 277 | """ |
paulb@213 | 278 | An attribute abstraction, indicating the type of the attribute along with |
paulb@213 | 279 | its context or origin. |
paulb@213 | 280 | """ |
paulb@213 | 281 | |
paulb@213 | 282 | def __init__(self, context, type): |
paulb@213 | 283 | self.context = context |
paulb@213 | 284 | self.type = type |
paulb@213 | 285 | |
paulb@233 | 286 | def __repr__(self): |
paulb@233 | 287 | return "Attribute(%s, %s)" % (repr(self.context), repr(self.type)) |
paulb@233 | 288 | |
paulb@213 | 289 | def __eq__(self, other): |
paulb@236 | 290 | return hasattr(other, "type") and other.type == self.type |
paulb@213 | 291 | |
paulb@213 | 292 | def __hash__(self): |
paulb@236 | 293 | return hash(self.type) |
paulb@213 | 294 | |
paulb@213 | 295 | # vim: tabstop=4 expandtab shiftwidth=4 |