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