paul@636 | 1 | #!/usr/bin/env python |
paul@636 | 2 | |
paul@636 | 3 | """ |
paul@636 | 4 | Translation result abstractions. |
paul@636 | 5 | |
paul@636 | 6 | Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@636 | 7 | |
paul@636 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@636 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@636 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@636 | 11 | version. |
paul@636 | 12 | |
paul@636 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@636 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@636 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@636 | 16 | details. |
paul@636 | 17 | |
paul@636 | 18 | You should have received a copy of the GNU General Public License along with |
paul@636 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@636 | 20 | """ |
paul@636 | 21 | |
paul@636 | 22 | from common import first, InstructionSequence |
paul@636 | 23 | from encoders import encode_instructions, encode_literal_constant, encode_path |
paul@636 | 24 | from results import ConstantValueRef, InstanceRef, LiteralSequenceRef, \ |
paul@636 | 25 | ResolvedNameRef, Result |
paul@636 | 26 | |
paul@636 | 27 | # Classes representing intermediate translation results. |
paul@636 | 28 | |
paul@636 | 29 | class ReturnRef: |
paul@636 | 30 | |
paul@636 | 31 | "Indicates usage of a return statement." |
paul@636 | 32 | |
paul@636 | 33 | pass |
paul@636 | 34 | |
paul@636 | 35 | class Expression(Result): |
paul@636 | 36 | |
paul@636 | 37 | "A general expression." |
paul@636 | 38 | |
paul@636 | 39 | def __init__(self, s): |
paul@636 | 40 | self.s = s |
paul@636 | 41 | def __str__(self): |
paul@636 | 42 | return self.s |
paul@636 | 43 | def __repr__(self): |
paul@636 | 44 | return "Expression(%r)" % self.s |
paul@636 | 45 | |
paul@636 | 46 | class TrResolvedNameRef(ResolvedNameRef): |
paul@636 | 47 | |
paul@636 | 48 | "A reference to a name in the translation." |
paul@636 | 49 | |
paul@636 | 50 | def __init__(self, name, ref, expr=None, is_global=False, parameter=None, location=None): |
paul@636 | 51 | ResolvedNameRef.__init__(self, name, ref, expr, is_global) |
paul@636 | 52 | self.parameter = parameter |
paul@636 | 53 | self.location = location |
paul@636 | 54 | |
paul@636 | 55 | def access_location(self): |
paul@636 | 56 | return self.location |
paul@636 | 57 | |
paul@636 | 58 | def __str__(self): |
paul@636 | 59 | |
paul@636 | 60 | "Return an output representation of the referenced name." |
paul@636 | 61 | |
paul@636 | 62 | # For sources, any identified static origin will be constant and thus |
paul@636 | 63 | # usable directly. For targets, no constant should be assigned and thus |
paul@636 | 64 | # the alias (or any plain name) will be used. |
paul@636 | 65 | |
paul@636 | 66 | ref = self.static() |
paul@636 | 67 | origin = ref and self.get_origin() |
paul@636 | 68 | static_name = origin and encode_path(origin) |
paul@636 | 69 | |
paul@636 | 70 | # Determine whether a qualified name is involved. |
paul@636 | 71 | |
paul@636 | 72 | t = (not self.is_constant_alias() and self.get_name() or self.name).rsplit(".", 1) |
paul@636 | 73 | parent = len(t) > 1 and t[0] or None |
paul@636 | 74 | attrname = t[-1] and encode_path(t[-1]) |
paul@636 | 75 | |
paul@636 | 76 | # Assignments. |
paul@636 | 77 | |
paul@636 | 78 | if self.expr: |
paul@636 | 79 | |
paul@636 | 80 | # Eliminate assignments between constants. |
paul@636 | 81 | |
paul@636 | 82 | if ref and isinstance(self.expr, ResolvedNameRef) and self.expr.static(): |
paul@636 | 83 | return "" |
paul@636 | 84 | |
paul@636 | 85 | # Qualified names must be converted into parent-relative assignments. |
paul@636 | 86 | |
paul@636 | 87 | elif parent: |
paul@636 | 88 | return "__store_via_object(&%s, %s, %s)" % ( |
paul@636 | 89 | encode_path(parent), attrname, self.expr) |
paul@636 | 90 | |
paul@636 | 91 | # All other assignments involve the names as they were given. |
paul@636 | 92 | |
paul@636 | 93 | else: |
paul@636 | 94 | return "(%s%s) = %s" % (self.parameter and "*" or "", attrname, self.expr) |
paul@636 | 95 | |
paul@636 | 96 | # Expressions. |
paul@636 | 97 | |
paul@636 | 98 | elif static_name: |
paul@636 | 99 | parent = ref.parent() |
paul@636 | 100 | context = ref.has_kind("<function>") and encode_path(parent) or None |
paul@636 | 101 | return "__ATTRVALUE(&%s)" % static_name |
paul@636 | 102 | |
paul@636 | 103 | # Qualified names must be converted into parent-relative accesses. |
paul@636 | 104 | |
paul@636 | 105 | elif parent: |
paul@636 | 106 | return "__load_via_object(&%s, %s)" % ( |
paul@636 | 107 | encode_path(parent), attrname) |
paul@636 | 108 | |
paul@636 | 109 | # All other accesses involve the names as they were given. |
paul@636 | 110 | |
paul@636 | 111 | else: |
paul@636 | 112 | return "(%s%s)" % (self.parameter and "*" or "", attrname) |
paul@636 | 113 | |
paul@636 | 114 | class TrConstantValueRef(ConstantValueRef): |
paul@636 | 115 | |
paul@636 | 116 | "A constant value reference in the translation." |
paul@636 | 117 | |
paul@636 | 118 | def __str__(self): |
paul@636 | 119 | return encode_literal_constant(self.number) |
paul@636 | 120 | |
paul@636 | 121 | class TrLiteralSequenceRef(LiteralSequenceRef): |
paul@636 | 122 | |
paul@636 | 123 | "A reference representing a sequence of values." |
paul@636 | 124 | |
paul@636 | 125 | def __str__(self): |
paul@636 | 126 | return str(self.node) |
paul@636 | 127 | |
paul@636 | 128 | class TrInstanceRef(InstanceRef): |
paul@636 | 129 | |
paul@636 | 130 | "A reference representing instantiation of a class." |
paul@636 | 131 | |
paul@636 | 132 | def __init__(self, ref, expr): |
paul@636 | 133 | |
paul@636 | 134 | """ |
paul@636 | 135 | Initialise the reference with 'ref' indicating the nature of the |
paul@636 | 136 | reference and 'expr' being an expression used to create the instance. |
paul@636 | 137 | """ |
paul@636 | 138 | |
paul@636 | 139 | InstanceRef.__init__(self, ref) |
paul@636 | 140 | self.expr = expr |
paul@636 | 141 | |
paul@636 | 142 | def __str__(self): |
paul@636 | 143 | return self.expr |
paul@636 | 144 | |
paul@636 | 145 | def __repr__(self): |
paul@636 | 146 | return "TrResolvedInstanceRef(%r, %r)" % (self.ref, self.expr) |
paul@636 | 147 | |
paul@636 | 148 | class AttrResult(Result, InstructionSequence): |
paul@636 | 149 | |
paul@636 | 150 | "A translation result for an attribute access." |
paul@636 | 151 | |
paul@636 | 152 | def __init__(self, instructions, refs, location, context_identity): |
paul@636 | 153 | InstructionSequence.__init__(self, instructions) |
paul@636 | 154 | self.refs = refs |
paul@636 | 155 | self.location = location |
paul@636 | 156 | self.context_identity = context_identity |
paul@636 | 157 | |
paul@636 | 158 | def references(self): |
paul@636 | 159 | return self.refs |
paul@636 | 160 | |
paul@636 | 161 | def access_location(self): |
paul@636 | 162 | return self.location |
paul@636 | 163 | |
paul@636 | 164 | def context(self): |
paul@636 | 165 | return self.context_identity |
paul@636 | 166 | |
paul@636 | 167 | def get_origin(self): |
paul@636 | 168 | return self.refs and len(self.refs) == 1 and first(self.refs).get_origin() |
paul@636 | 169 | |
paul@636 | 170 | def has_kind(self, kinds): |
paul@636 | 171 | if not self.refs: |
paul@636 | 172 | return False |
paul@636 | 173 | for ref in self.refs: |
paul@636 | 174 | if ref.has_kind(kinds): |
paul@636 | 175 | return True |
paul@636 | 176 | return False |
paul@636 | 177 | |
paul@636 | 178 | def __nonzero__(self): |
paul@636 | 179 | return bool(self.instructions) |
paul@636 | 180 | |
paul@636 | 181 | def __str__(self): |
paul@636 | 182 | return encode_instructions(self.instructions) |
paul@636 | 183 | |
paul@636 | 184 | def __repr__(self): |
paul@636 | 185 | return "AttrResult(%r, %r, %r)" % (self.instructions, self.refs, self.location) |
paul@636 | 186 | |
paul@636 | 187 | class InvocationResult(Result, InstructionSequence): |
paul@636 | 188 | |
paul@636 | 189 | "A translation result for an invocation." |
paul@636 | 190 | |
paul@636 | 191 | def __str__(self): |
paul@636 | 192 | return encode_instructions(self.instructions) |
paul@636 | 193 | |
paul@636 | 194 | def __repr__(self): |
paul@636 | 195 | return "InvocationResult(%r)" % self.instructions |
paul@636 | 196 | |
paul@636 | 197 | class InstantiationResult(InvocationResult, TrInstanceRef): |
paul@636 | 198 | |
paul@636 | 199 | "An instantiation result acting like an invocation result." |
paul@636 | 200 | |
paul@636 | 201 | def __init__(self, ref, instructions): |
paul@636 | 202 | InstanceRef.__init__(self, ref) |
paul@636 | 203 | InvocationResult.__init__(self, instructions) |
paul@636 | 204 | |
paul@636 | 205 | def __repr__(self): |
paul@636 | 206 | return "InstantiationResult(%r, %r)" % (self.ref, self.instructions) |
paul@636 | 207 | |
paul@636 | 208 | class PredefinedConstantRef(Result): |
paul@636 | 209 | |
paul@636 | 210 | "A predefined constant reference." |
paul@636 | 211 | |
paul@636 | 212 | def __init__(self, value, expr=None): |
paul@636 | 213 | self.value = value |
paul@636 | 214 | self.expr = expr |
paul@636 | 215 | |
paul@636 | 216 | def __str__(self): |
paul@636 | 217 | |
paul@636 | 218 | # Eliminate predefined constant assignments. |
paul@636 | 219 | |
paul@636 | 220 | if self.expr: |
paul@636 | 221 | return "" |
paul@636 | 222 | |
paul@636 | 223 | # Generate the specific constants. |
paul@636 | 224 | |
paul@636 | 225 | if self.value in ("False", "True"): |
paul@636 | 226 | return encode_path("__builtins__.boolean.%s" % self.value) |
paul@636 | 227 | elif self.value == "None": |
paul@636 | 228 | return encode_path("__builtins__.none.%s" % self.value) |
paul@636 | 229 | elif self.value == "NotImplemented": |
paul@636 | 230 | return encode_path("__builtins__.notimplemented.%s" % self.value) |
paul@636 | 231 | else: |
paul@636 | 232 | return self.value |
paul@636 | 233 | |
paul@636 | 234 | def __repr__(self): |
paul@636 | 235 | return "PredefinedConstantRef(%r)" % self.value |
paul@636 | 236 | |
paul@636 | 237 | class LogicalResult(Result): |
paul@636 | 238 | |
paul@636 | 239 | "A logical expression result." |
paul@636 | 240 | |
paul@636 | 241 | def _convert(self, expr): |
paul@636 | 242 | |
paul@636 | 243 | "Return 'expr' converted to a testable value." |
paul@636 | 244 | |
paul@636 | 245 | if isinstance(expr, LogicalResult): |
paul@636 | 246 | return expr.apply_test() |
paul@636 | 247 | else: |
paul@636 | 248 | return "__BOOL(%s)" % expr |
paul@636 | 249 | |
paul@636 | 250 | class NegationResult(LogicalResult): |
paul@636 | 251 | |
paul@636 | 252 | "A negation expression result." |
paul@636 | 253 | |
paul@636 | 254 | def __init__(self, expr): |
paul@636 | 255 | self.expr = expr |
paul@636 | 256 | |
paul@636 | 257 | def apply_test(self): |
paul@636 | 258 | |
paul@636 | 259 | "Return the result in a form suitable for direct testing." |
paul@636 | 260 | |
paul@636 | 261 | expr = self._convert(self.expr) |
paul@636 | 262 | return "(!%s)" % expr |
paul@636 | 263 | |
paul@636 | 264 | def __str__(self): |
paul@636 | 265 | return "(%s ? %s : %s)" % ( |
paul@636 | 266 | self._convert(self.expr), |
paul@636 | 267 | PredefinedConstantRef("False"), |
paul@636 | 268 | PredefinedConstantRef("True")) |
paul@636 | 269 | |
paul@636 | 270 | def __repr__(self): |
paul@636 | 271 | return "NegationResult(%r)" % self.expr |
paul@636 | 272 | |
paul@636 | 273 | class LogicalOperationResult(LogicalResult): |
paul@636 | 274 | |
paul@636 | 275 | "A logical operation result." |
paul@636 | 276 | |
paul@636 | 277 | def __init__(self, exprs, conjunction): |
paul@636 | 278 | self.exprs = exprs |
paul@636 | 279 | self.conjunction = conjunction |
paul@636 | 280 | |
paul@636 | 281 | def apply_test(self): |
paul@636 | 282 | |
paul@636 | 283 | """ |
paul@636 | 284 | Return the result in a form suitable for direct testing. |
paul@636 | 285 | |
paul@636 | 286 | Convert ... to ... |
paul@636 | 287 | |
paul@636 | 288 | <a> and <b> |
paul@636 | 289 | ((__BOOL(<a>)) && (__BOOL(<b>))) |
paul@636 | 290 | |
paul@636 | 291 | <a> or <b> |
paul@636 | 292 | ((__BOOL(<a>)) || (__BOOL(<b>))) |
paul@636 | 293 | """ |
paul@636 | 294 | |
paul@636 | 295 | results = [] |
paul@636 | 296 | for expr in self.exprs: |
paul@636 | 297 | results.append(self._convert(expr)) |
paul@636 | 298 | |
paul@636 | 299 | if self.conjunction: |
paul@636 | 300 | return "(%s)" % " && ".join(results) |
paul@636 | 301 | else: |
paul@636 | 302 | return "(%s)" % " || ".join(results) |
paul@636 | 303 | |
paul@636 | 304 | def __str__(self): |
paul@636 | 305 | |
paul@636 | 306 | """ |
paul@636 | 307 | Convert ... to ... |
paul@636 | 308 | |
paul@636 | 309 | <a> and <b> |
paul@636 | 310 | (__tmp_result = <a>, !__BOOL(__tmp_result)) ? __tmp_result : <b> |
paul@636 | 311 | |
paul@636 | 312 | <a> or <b> |
paul@636 | 313 | (__tmp_result = <a>, __BOOL(__tmp_result)) ? __tmp_result : <b> |
paul@636 | 314 | """ |
paul@636 | 315 | |
paul@636 | 316 | results = [] |
paul@636 | 317 | for expr in self.exprs[:-1]: |
paul@636 | 318 | results.append("(__tmp_result = %s, %s__BOOL(__tmp_result)) ? __tmp_result : " % (expr, self.conjunction and "!" or "")) |
paul@636 | 319 | results.append(str(self.exprs[-1])) |
paul@636 | 320 | |
paul@636 | 321 | return "(%s)" % "".join(results) |
paul@636 | 322 | |
paul@636 | 323 | def __repr__(self): |
paul@636 | 324 | return "LogicalOperationResult(%r, %r)" % (self.exprs, self.conjunction) |
paul@636 | 325 | |
paul@636 | 326 | # vim: tabstop=4 expandtab shiftwidth=4 |