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@213 | 24 | from simplified.utils import Structure, WithName, name |
paulb@213 | 25 | |
paulb@213 | 26 | # Special non-program nodes. |
paulb@213 | 27 | |
paulb@213 | 28 | class _Class(Structure, WithName): |
paulb@213 | 29 | |
paulb@213 | 30 | "A Python class." |
paulb@213 | 31 | |
paulb@213 | 32 | def __init__(self, *args, **kw): |
paulb@213 | 33 | Structure.__init__(self, *args, **kw) |
paulb@213 | 34 | WithName.__init__(self) |
paulb@213 | 35 | |
paulb@213 | 36 | def full_name(self): |
paulb@213 | 37 | return "class %s" % self._full_name |
paulb@213 | 38 | |
paulb@213 | 39 | class SingleInstanceClass(_Class): |
paulb@213 | 40 | |
paulb@213 | 41 | "A Python class." |
paulb@213 | 42 | |
paulb@213 | 43 | def __init__(self, *args, **kw): |
paulb@213 | 44 | _Class.__init__(self, *args, **kw) |
paulb@213 | 45 | self.instance = None |
paulb@213 | 46 | |
paulb@213 | 47 | def has_instance(self, node): |
paulb@213 | 48 | return self.instance is not None |
paulb@213 | 49 | |
paulb@213 | 50 | def add_instance(self, node, instance): |
paulb@213 | 51 | self.instance = instance |
paulb@213 | 52 | |
paulb@213 | 53 | def get_instance(self, node): |
paulb@213 | 54 | return self.instance |
paulb@213 | 55 | |
paulb@213 | 56 | def get_instance_name(self, instance): |
paulb@213 | 57 | return self._full_name |
paulb@213 | 58 | |
paulb@213 | 59 | # Attribute propagation. |
paulb@213 | 60 | |
paulb@213 | 61 | def get_attribute_for_instance(self, attribute, instance): |
paulb@213 | 62 | return attribute |
paulb@213 | 63 | |
paulb@213 | 64 | class MultipleInstanceClass(_Class): |
paulb@213 | 65 | |
paulb@213 | 66 | "A Python class." |
paulb@213 | 67 | |
paulb@213 | 68 | def __init__(self, *args, **kw): |
paulb@213 | 69 | _Class.__init__(self, *args, **kw) |
paulb@213 | 70 | self.instances = {} |
paulb@213 | 71 | self.attributes_for_instances = {} |
paulb@213 | 72 | |
paulb@213 | 73 | def _get_key(self, node): |
paulb@213 | 74 | return id(getattr(node, "original", None)) # self.module.original |
paulb@213 | 75 | |
paulb@213 | 76 | def has_instance(self, node): |
paulb@213 | 77 | return self.instances.has_key(self._get_key(node)) |
paulb@213 | 78 | |
paulb@213 | 79 | def add_instance(self, node, instance): |
paulb@213 | 80 | self.instances[self._get_key(node)] = instance |
paulb@213 | 81 | |
paulb@213 | 82 | def get_instance(self, node): |
paulb@213 | 83 | return self.instances[self._get_key(node)] |
paulb@213 | 84 | |
paulb@213 | 85 | def get_instance_name(self, instance): |
paulb@213 | 86 | return name(instance, self._full_name) |
paulb@213 | 87 | |
paulb@213 | 88 | # Attribute propagation. |
paulb@213 | 89 | |
paulb@213 | 90 | def get_attribute_for_instance(self, attribute, instance): |
paulb@213 | 91 | |
paulb@213 | 92 | # Create specialised methods. |
paulb@213 | 93 | |
paulb@213 | 94 | if isinstance(attribute.type, Subprogram): |
paulb@213 | 95 | subprogram = attribute.type |
paulb@213 | 96 | |
paulb@213 | 97 | # Each instance may have its own version of the subprogram. |
paulb@213 | 98 | |
paulb@213 | 99 | key = (subprogram, instance) |
paulb@213 | 100 | if not self.attributes_for_instances.has_key(key): |
paulb@213 | 101 | new_subprogram = subprogram.copy(instance, subprogram.full_name()) |
paulb@213 | 102 | subprogram.module.simplifier.subnames[new_subprogram.full_name()] = new_subprogram |
paulb@213 | 103 | self.attributes_for_instances[key] = Attribute(attribute.context, new_subprogram) |
paulb@213 | 104 | print "New subprogram", new_subprogram, "for", key |
paulb@213 | 105 | |
paulb@213 | 106 | return self.attributes_for_instances[key] |
paulb@213 | 107 | |
paulb@213 | 108 | # The original nodes are returned for other attributes. |
paulb@213 | 109 | |
paulb@213 | 110 | else: |
paulb@213 | 111 | return attribute |
paulb@213 | 112 | |
paulb@213 | 113 | class SelectiveMultipleInstanceClass(MultipleInstanceClass): |
paulb@213 | 114 | |
paulb@213 | 115 | "A Python class which provides multiple instances depending on the class." |
paulb@213 | 116 | |
paulb@213 | 117 | def _get_key(self, node): |
paulb@213 | 118 | if self.namespace.has_key("__atomic__"): |
paulb@213 | 119 | return id(self) |
paulb@213 | 120 | else: |
paulb@213 | 121 | return MultipleInstanceClass._get_key(self, node) |
paulb@213 | 122 | |
paulb@213 | 123 | class ProlificMultipleInstanceClass(MultipleInstanceClass): |
paulb@213 | 124 | |
paulb@213 | 125 | """ |
paulb@213 | 126 | A Python class which provides multiple instances for different versions of |
paulb@213 | 127 | methods. In order to avoid unbounded instance production (since new |
paulb@213 | 128 | instances cause new copies of methods which in turn would cause new |
paulb@213 | 129 | instances), a relations dictionary is maintained which attempts to map |
paulb@213 | 130 | "requesting instances" to existing instances, suggesting such instances in |
paulb@213 | 131 | preference to new ones. |
paulb@213 | 132 | """ |
paulb@213 | 133 | |
paulb@213 | 134 | def __init__(self, *args, **kw): |
paulb@213 | 135 | MultipleInstanceClass.__init__(self, *args, **kw) |
paulb@213 | 136 | self.instance_relations = {} |
paulb@213 | 137 | |
paulb@213 | 138 | def _get_key(self, node): |
paulb@213 | 139 | if self.namespace.has_key("__atomic__"): |
paulb@213 | 140 | return id(self) |
paulb@213 | 141 | else: |
paulb@213 | 142 | return id(node) |
paulb@213 | 143 | |
paulb@213 | 144 | def has_instance(self, node): |
paulb@213 | 145 | requesting_instance = getattr(node, "instance", None) |
paulb@213 | 146 | #return requesting_instance is not None and requesting_instance.get_class() is self or \ |
paulb@213 | 147 | return self.instance_relations.has_key(requesting_instance) or self.instances.has_key(self._get_key(node)) |
paulb@213 | 148 | |
paulb@213 | 149 | def add_instance(self, node, instance): |
paulb@213 | 150 | requesting_instance = getattr(node, "instance", None) |
paulb@213 | 151 | print "New instance", instance, "for", id(node), requesting_instance |
paulb@213 | 152 | self.instances[self._get_key(node)] = instance |
paulb@213 | 153 | if requesting_instance is not None: |
paulb@213 | 154 | self.instance_relations[requesting_instance] = instance |
paulb@213 | 155 | requesting_instance.get_class().instance_relations[instance] = requesting_instance |
paulb@213 | 156 | |
paulb@213 | 157 | def get_instance(self, node): |
paulb@213 | 158 | requesting_instance = getattr(node, "instance", None) |
paulb@213 | 159 | #if requesting_instance is not None and requesting_instance.get_class() is self: |
paulb@213 | 160 | # return requesting_instance |
paulb@213 | 161 | return self.instance_relations.get(requesting_instance) or self.instances[self._get_key(node)] |
paulb@213 | 162 | |
paulb@213 | 163 | class Instance(Structure): |
paulb@213 | 164 | |
paulb@213 | 165 | "An instance." |
paulb@213 | 166 | |
paulb@213 | 167 | def full_name(self): |
paulb@213 | 168 | return self.get_class().get_instance_name(self) |
paulb@213 | 169 | |
paulb@213 | 170 | def get_class(self): |
paulb@213 | 171 | for n in self.namespace.load("__class__"): |
paulb@213 | 172 | return n.type |
paulb@213 | 173 | else: |
paulb@213 | 174 | raise ValueError, "__class__" |
paulb@213 | 175 | |
paulb@213 | 176 | def __repr__(self): |
paulb@213 | 177 | return "Instance of type '%s'" % self.full_name() |
paulb@213 | 178 | |
paulb@213 | 179 | def __eq__(self, other): |
paulb@213 | 180 | # NOTE: Single instance: all instances are the same |
paulb@213 | 181 | # NOTE: Multiple instances: all instances are different |
paulb@213 | 182 | return self.full_name() == other.full_name() |
paulb@213 | 183 | |
paulb@213 | 184 | def __hash__(self): |
paulb@213 | 185 | return id(self) |
paulb@213 | 186 | |
paulb@213 | 187 | class Constant: |
paulb@213 | 188 | |
paulb@213 | 189 | "A constant initialised with a type name for future processing." |
paulb@213 | 190 | |
paulb@213 | 191 | def __init__(self, name, value): |
paulb@213 | 192 | self.name = name |
paulb@213 | 193 | self.value = value |
paulb@213 | 194 | self.typename = self.value.__class__.__name__ |
paulb@213 | 195 | |
paulb@213 | 196 | class Attribute: |
paulb@213 | 197 | |
paulb@213 | 198 | """ |
paulb@213 | 199 | An attribute abstraction, indicating the type of the attribute along with |
paulb@213 | 200 | its context or origin. |
paulb@213 | 201 | """ |
paulb@213 | 202 | |
paulb@213 | 203 | def __init__(self, context, type): |
paulb@213 | 204 | self.context = context |
paulb@213 | 205 | self.type = type |
paulb@213 | 206 | |
paulb@213 | 207 | def __eq__(self, other): |
paulb@213 | 208 | return hasattr(other, "type") and other.type == self.type or other == self.type |
paulb@213 | 209 | |
paulb@213 | 210 | def __repr__(self): |
paulb@213 | 211 | return "Attribute(%s, %s)" % (repr(self.context), repr(self.type)) |
paulb@213 | 212 | |
paulb@213 | 213 | def __hash__(self): |
paulb@213 | 214 | return id(self.type) |
paulb@213 | 215 | |
paulb@213 | 216 | # Configuration setting. |
paulb@213 | 217 | |
paulb@213 | 218 | Class = SingleInstanceClass |
paulb@213 | 219 | #Class = MultipleInstanceClass |
paulb@213 | 220 | |
paulb@213 | 221 | def set_single_instance_mode(): |
paulb@213 | 222 | global Class |
paulb@213 | 223 | Class = SingleInstanceClass |
paulb@213 | 224 | |
paulb@213 | 225 | def set_multiple_instance_mode(): |
paulb@213 | 226 | global Class |
paulb@213 | 227 | Class = MultipleInstanceClass |
paulb@213 | 228 | |
paulb@213 | 229 | def set_selective_multiple_instance_mode(): |
paulb@213 | 230 | global Class |
paulb@213 | 231 | Class = SelectiveMultipleInstanceClass |
paulb@213 | 232 | |
paulb@213 | 233 | def set_prolific_multiple_instance_mode(): |
paulb@213 | 234 | global Class |
paulb@213 | 235 | Class = ProlificMultipleInstanceClass |
paulb@213 | 236 | |
paulb@213 | 237 | # vim: tabstop=4 expandtab shiftwidth=4 |