paul@11 | 1 | #!/usr/bin/env python |
paul@11 | 2 | |
paul@11 | 3 | """ |
paul@11 | 4 | Result abstractions. |
paul@11 | 5 | |
paul@510 | 6 | Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@11 | 7 | |
paul@11 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@11 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@11 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@11 | 11 | version. |
paul@11 | 12 | |
paul@11 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@11 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@11 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@11 | 16 | details. |
paul@11 | 17 | |
paul@11 | 18 | You should have received a copy of the GNU General Public License along with |
paul@11 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@11 | 20 | """ |
paul@11 | 21 | |
paul@27 | 22 | from referencing import Reference |
paul@27 | 23 | |
paul@11 | 24 | # Classes representing inspection and translation observations. |
paul@11 | 25 | |
paul@11 | 26 | class Result: |
paul@11 | 27 | |
paul@11 | 28 | "An abstract expression result." |
paul@11 | 29 | |
paul@11 | 30 | def is_name(self): |
paul@11 | 31 | return False |
paul@118 | 32 | |
paul@226 | 33 | def reference(self): |
paul@226 | 34 | return None |
paul@226 | 35 | |
paul@552 | 36 | def references(self): |
paul@552 | 37 | return None |
paul@552 | 38 | |
paul@554 | 39 | def access_location(self): |
paul@553 | 40 | return None |
paul@553 | 41 | |
paul@226 | 42 | def get_name(self): |
paul@226 | 43 | return None |
paul@226 | 44 | |
paul@11 | 45 | def get_origin(self): |
paul@11 | 46 | return None |
paul@11 | 47 | |
paul@226 | 48 | def static(self): |
paul@118 | 49 | return None |
paul@118 | 50 | |
paul@226 | 51 | def final(self): |
paul@226 | 52 | return None |
paul@226 | 53 | |
paul@226 | 54 | def has_kind(self, kinds): |
paul@226 | 55 | return False |
paul@226 | 56 | |
paul@11 | 57 | class AccessRef(Result): |
paul@11 | 58 | |
paul@11 | 59 | """ |
paul@11 | 60 | A reference to an attribute access that is generally only returned from a |
paul@11 | 61 | processed access for possible initialiser resolution for assignments. |
paul@11 | 62 | """ |
paul@11 | 63 | |
paul@11 | 64 | def __init__(self, original_name, attrnames, number): |
paul@11 | 65 | self.original_name = original_name |
paul@11 | 66 | self.attrnames = attrnames |
paul@11 | 67 | self.number = number |
paul@11 | 68 | |
paul@11 | 69 | def __repr__(self): |
paul@11 | 70 | return "AccessRef(%r, %r, %r)" % (self.original_name, self.attrnames, self.number) |
paul@11 | 71 | |
paul@11 | 72 | class InvocationRef(Result): |
paul@11 | 73 | |
paul@11 | 74 | "An invocation of a name reference." |
paul@11 | 75 | |
paul@11 | 76 | def __init__(self, name_ref): |
paul@11 | 77 | self.name_ref = name_ref |
paul@11 | 78 | |
paul@27 | 79 | def reference(self): |
paul@27 | 80 | origin = self.name_ref.get_origin() |
paul@27 | 81 | if origin: |
paul@27 | 82 | return Reference("<invoke>", origin) |
paul@27 | 83 | else: |
paul@27 | 84 | return Reference("<var>") |
paul@27 | 85 | |
paul@11 | 86 | def __repr__(self): |
paul@11 | 87 | return "InvocationRef(%r)" % self.name_ref |
paul@11 | 88 | |
paul@11 | 89 | class NameRef(Result): |
paul@11 | 90 | |
paul@11 | 91 | "A reference to a name." |
paul@11 | 92 | |
paul@11 | 93 | def __init__(self, name, expr=None): |
paul@11 | 94 | self.name = name |
paul@11 | 95 | self.expr = expr |
paul@11 | 96 | |
paul@11 | 97 | def is_name(self): |
paul@11 | 98 | return True |
paul@11 | 99 | |
paul@11 | 100 | def final(self): |
paul@11 | 101 | return None |
paul@11 | 102 | |
paul@11 | 103 | def __repr__(self): |
paul@11 | 104 | return "NameRef(%r, %r)" % (self.name, self.expr) |
paul@11 | 105 | |
paul@11 | 106 | class LocalNameRef(NameRef): |
paul@11 | 107 | |
paul@11 | 108 | "A reference to a local name." |
paul@11 | 109 | |
paul@11 | 110 | def __init__(self, name, number): |
paul@11 | 111 | NameRef.__init__(self, name) |
paul@11 | 112 | self.number = number |
paul@11 | 113 | |
paul@11 | 114 | def __repr__(self): |
paul@11 | 115 | return "LocalNameRef(%r, %r)" % (self.name, self.number) |
paul@11 | 116 | |
paul@317 | 117 | class ResolvedRef: |
paul@11 | 118 | |
paul@317 | 119 | "A resolved reference mix-in." |
paul@11 | 120 | |
paul@552 | 121 | def __init__(self, ref): |
paul@552 | 122 | self.ref = ref |
paul@552 | 123 | |
paul@11 | 124 | def reference(self): |
paul@11 | 125 | return self.ref |
paul@11 | 126 | |
paul@552 | 127 | def references(self): |
paul@552 | 128 | return [self.ref] |
paul@552 | 129 | |
paul@11 | 130 | def get_name(self): |
paul@11 | 131 | return self.ref and self.ref.get_name() or None |
paul@11 | 132 | |
paul@11 | 133 | def get_origin(self): |
paul@11 | 134 | return self.ref and self.ref.get_origin() or None |
paul@11 | 135 | |
paul@11 | 136 | def static(self): |
paul@11 | 137 | return self.ref and self.ref.static() or None |
paul@11 | 138 | |
paul@11 | 139 | def final(self): |
paul@11 | 140 | return self.ref and self.ref.final() or None |
paul@11 | 141 | |
paul@11 | 142 | def has_kind(self, kinds): |
paul@11 | 143 | return self.ref and self.ref.has_kind(kinds) |
paul@11 | 144 | |
paul@338 | 145 | def is_constant_alias(self): |
paul@338 | 146 | return self.ref and self.ref.is_constant_alias() |
paul@338 | 147 | |
paul@317 | 148 | class ResolvedNameRef(ResolvedRef, NameRef): |
paul@317 | 149 | |
paul@317 | 150 | "A resolved name-based reference." |
paul@317 | 151 | |
paul@317 | 152 | def __init__(self, name, ref, expr=None): |
paul@317 | 153 | NameRef.__init__(self, name, expr) |
paul@552 | 154 | ResolvedRef.__init__(self, ref) |
paul@317 | 155 | |
paul@11 | 156 | def __repr__(self): |
paul@11 | 157 | return "ResolvedNameRef(%r, %r, %r)" % (self.name, self.ref, self.expr) |
paul@11 | 158 | |
paul@11 | 159 | class ConstantValueRef(ResolvedNameRef): |
paul@11 | 160 | |
paul@11 | 161 | "A constant reference representing a single literal value." |
paul@11 | 162 | |
paul@11 | 163 | def __init__(self, name, ref, value, number=None): |
paul@11 | 164 | ResolvedNameRef.__init__(self, name, ref) |
paul@11 | 165 | self.value = value |
paul@11 | 166 | self.number = number |
paul@11 | 167 | |
paul@11 | 168 | def __repr__(self): |
paul@11 | 169 | return "ConstantValueRef(%r, %r, %r, %r)" % (self.name, self.ref, self.value, self.number) |
paul@11 | 170 | |
paul@317 | 171 | class InstanceRef(ResolvedRef, Result): |
paul@11 | 172 | |
paul@11 | 173 | "An instance reference." |
paul@11 | 174 | |
paul@11 | 175 | def reference(self): |
paul@11 | 176 | return self.ref |
paul@11 | 177 | |
paul@11 | 178 | def __repr__(self): |
paul@11 | 179 | return "InstanceRef(%r)" % self.ref |
paul@11 | 180 | |
paul@11 | 181 | class LiteralSequenceRef(ResolvedNameRef): |
paul@11 | 182 | |
paul@11 | 183 | "A reference representing a sequence of values." |
paul@11 | 184 | |
paul@11 | 185 | def __init__(self, name, ref, node, items=None): |
paul@11 | 186 | ResolvedNameRef.__init__(self, name, ref) |
paul@11 | 187 | self.node = node |
paul@11 | 188 | self.items = items |
paul@11 | 189 | |
paul@11 | 190 | def __repr__(self): |
paul@11 | 191 | return "LiteralSequenceRef(%r, %r, %r, %r)" % (self.name, self.ref, self.node, self.items) |
paul@11 | 192 | |
paul@226 | 193 | class VariableRef(Result): |
paul@226 | 194 | |
paul@226 | 195 | "A variable reference." |
paul@226 | 196 | |
paul@226 | 197 | def __repr__(self): |
paul@226 | 198 | return "VariableRef()" |
paul@226 | 199 | |
paul@11 | 200 | # vim: tabstop=4 expandtab shiftwidth=4 |