1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/access_plan.py Mon Sep 04 01:38:36 2023 +0200
1.3 @@ -0,0 +1,529 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Attribute access plan translation.
1.8 +
1.9 +Copyright (C) 2014-2018, 2023 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +from encoders import encode_access_location
1.26 +
1.27 +class AccessPlan:
1.28 +
1.29 + "An attribute access plan."
1.30 +
1.31 + def __init__(self, name, test, test_type, base, traversed, traversal_modes,
1.32 + remaining, context, context_test, first_method, final_method,
1.33 + origin, accessor_kinds):
1.34 +
1.35 + "Initialise the plan."
1.36 +
1.37 + # With instance attribute initialisers, the assignment below would be
1.38 + # generated automatically.
1.39 +
1.40 + (
1.41 + self.name, self.test, self.test_type, self.base,
1.42 + self.traversed, self.traversal_modes, self.remaining,
1.43 + self.context, self.context_test,
1.44 + self.first_method, self.final_method,
1.45 + self.origin, self.accessor_kinds) = (
1.46 +
1.47 + name, test, test_type, base,
1.48 + traversed, traversal_modes, remaining,
1.49 + context, context_test,
1.50 + first_method, final_method,
1.51 + origin, accessor_kinds)
1.52 +
1.53 + # Define the first attribute access and subsequent accesses.
1.54 +
1.55 + self.first_attrname = None
1.56 + self.traversed_attrnames = traversed
1.57 + self.traversed_attrname_modes = traversal_modes
1.58 + self.remaining_attrnames = remaining
1.59 +
1.60 + if traversed:
1.61 + self.first_attrname = traversed[0]
1.62 + self.traversed_attrnames = traversed[1:]
1.63 + self.traversed_attrname_modes = traversal_modes[1:]
1.64 + elif remaining:
1.65 + self.first_attrname = remaining[0]
1.66 + self.remaining_attrnames = remaining[1:]
1.67 +
1.68 + def access_first_attribute(self):
1.69 +
1.70 + "Return whether the first attribute is to be accessed."
1.71 +
1.72 + return self.final_method in ("access", "access-invoke", "assign") or \
1.73 + self.all_subsequent_attributes()
1.74 +
1.75 + def assigning_first_attribute(self):
1.76 +
1.77 + "Return whether the first attribute access involves assignment."
1.78 +
1.79 + return not self.all_subsequent_attributes() and self.final_method == "assign"
1.80 +
1.81 + def get_first_attribute_name(self):
1.82 +
1.83 + "Return any first attribute name to be used in an initial access."
1.84 +
1.85 + return self.first_attrname
1.86 +
1.87 + def all_subsequent_attributes(self):
1.88 +
1.89 + "Return all subsequent attribute names involved in accesses."
1.90 +
1.91 + return self.traversed_attrnames + self.remaining_attrnames
1.92 +
1.93 + def attribute_traversals(self):
1.94 +
1.95 + "Return a collection of (attribute name, traversal mode) tuples."
1.96 +
1.97 + return zip(self.traversed_attrnames, self.traversed_attrname_modes)
1.98 +
1.99 + def stored_accessor(self):
1.100 +
1.101 + "Return the variable used to obtain the accessor."
1.102 +
1.103 + return self.assigning_first_attribute() and "<target_accessor>" or "<accessor>"
1.104 +
1.105 + def stored_accessor_modifier(self):
1.106 +
1.107 + "Return the variable used to set the accessor."
1.108 +
1.109 + return self.assigning_first_attribute() and "<set_target_accessor>" or "<set_accessor>"
1.110 +
1.111 + def get_original_accessor(self):
1.112 +
1.113 + "Return the original accessor details."
1.114 +
1.115 + # Identify any static original accessor.
1.116 +
1.117 + if self.base:
1.118 + return self.base
1.119 +
1.120 + # Employ names as contexts unless the context needs testing and
1.121 + # potentially updating. In such cases, temporary context storage is
1.122 + # used instead.
1.123 +
1.124 + elif self.name and not (self.context_test == "test" and
1.125 + self.final_method in ("access-invoke", "static-invoke")):
1.126 +
1.127 + return "<name>"
1.128 +
1.129 + # Use a generic placeholder representing the access expression in
1.130 + # the general case.
1.131 +
1.132 + else:
1.133 + return "<expr>"
1.134 +
1.135 + def get_instructions(self):
1.136 +
1.137 + "Return a list of instructions corresponding to the plan."
1.138 +
1.139 + # Emit instructions by appending them to a list.
1.140 +
1.141 + instructions = []
1.142 + emit = instructions.append
1.143 +
1.144 + # Set up any initial instructions.
1.145 +
1.146 + accessor, context = self.process_initialisation(emit)
1.147 +
1.148 + # Apply any test.
1.149 +
1.150 + if self.test[0] == "test":
1.151 + test_accessor = accessor = ("__%s_%s_%s" % self.test, accessor, self.test_type)
1.152 + else:
1.153 + test_accessor = None
1.154 +
1.155 + # Perform the first or final access.
1.156 + # The access only needs performing if the resulting accessor is used.
1.157 +
1.158 + accessor = self.process_first_attribute(accessor, emit)
1.159 +
1.160 + # Perform accesses for the traversed and remaining attributes.
1.161 +
1.162 + accessor, context = self.process_traversed_attributes(accessor, context, emit)
1.163 + accessor, context = self.process_remaining_attributes(accessor, context, emit)
1.164 +
1.165 + # Make any accessor test available if not emitted.
1.166 +
1.167 + test_accessor = not instructions and test_accessor or None
1.168 +
1.169 + # Perform the access on the actual target.
1.170 +
1.171 + accessor = self.process_attribute_access(accessor, context, test_accessor, emit)
1.172 +
1.173 + # Produce an advisory instruction regarding the context.
1.174 +
1.175 + self.process_context_identity(context, emit)
1.176 +
1.177 + # Produce an advisory instruction regarding the final attribute.
1.178 +
1.179 + if self.origin:
1.180 + emit(("<final_identity>", self.origin))
1.181 +
1.182 + return instructions
1.183 +
1.184 + def process_initialisation(self, emit):
1.185 +
1.186 + """
1.187 + Use 'emit' to generate instructions for any initialisation of attribute
1.188 + access. Return the potentially revised accessor and context indicators.
1.189 + """
1.190 +
1.191 + # Identify any static original accessor.
1.192 +
1.193 + original_accessor = self.get_original_accessor()
1.194 +
1.195 + # Determine whether the first access involves assignment.
1.196 +
1.197 + set_accessor = self.stored_accessor_modifier()
1.198 + stored_accessor = self.stored_accessor()
1.199 +
1.200 + # Set the context if already available.
1.201 +
1.202 + context = None
1.203 +
1.204 + if self.context == "base":
1.205 + accessor = context = (self.base,)
1.206 + elif self.context == "original-accessor":
1.207 +
1.208 + # Prevent re-evaluation of any dynamic expression by storing it.
1.209 +
1.210 + if original_accessor == "<expr>":
1.211 + if self.final_method in ("access-invoke", "static-invoke"):
1.212 + emit(("<set_context>", original_accessor))
1.213 + accessor = context = ("<context>",)
1.214 + else:
1.215 + emit((set_accessor, original_accessor))
1.216 + accessor = context = (stored_accessor,)
1.217 + else:
1.218 + accessor = context = (original_accessor,)
1.219 +
1.220 + # Assigning does not set the context.
1.221 +
1.222 + elif self.context in ("final-accessor", "unset") and self.access_first_attribute():
1.223 +
1.224 + # Prevent re-evaluation of any dynamic expression by storing it.
1.225 +
1.226 + if original_accessor == "<expr>":
1.227 + emit((set_accessor, original_accessor))
1.228 + accessor = (stored_accessor,)
1.229 + else:
1.230 + accessor = (original_accessor,)
1.231 + else:
1.232 + accessor = None
1.233 +
1.234 + return accessor, context
1.235 +
1.236 + def process_first_attribute(self, accessor, emit):
1.237 +
1.238 + """
1.239 + Using 'accessor', use 'emit' to generate instructions for any first
1.240 + attribute access. Return the potentially revised accessor.
1.241 + """
1.242 +
1.243 + if self.access_first_attribute():
1.244 + attrname = self.get_first_attribute_name()
1.245 + assigning = self.assigning_first_attribute()
1.246 +
1.247 + # Access via the accessor's class.
1.248 +
1.249 + if self.first_method == "relative-class":
1.250 + if assigning:
1.251 + emit(("<set_attr_ref>", ("__get_class_attr_ref", accessor, attrname)))
1.252 + emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
1.253 + else:
1.254 + accessor = ("__load_via_class", accessor, attrname)
1.255 +
1.256 + # Access via the accessor itself.
1.257 +
1.258 + elif self.first_method == "relative-object":
1.259 + if assigning:
1.260 + emit(("<set_attr_ref>", ("__get_object_attr_ref", accessor, attrname)))
1.261 + emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
1.262 + else:
1.263 + accessor = ("__load_via_object", accessor, attrname)
1.264 +
1.265 + # Access via a class accessor or the accessor's class.
1.266 +
1.267 + elif self.first_method == "relative-object-class":
1.268 + if assigning:
1.269 + emit(("__raise_type_error",))
1.270 + else:
1.271 + accessor = ("__get_class_and_load", accessor, attrname)
1.272 +
1.273 + # Access via the accessor's class.
1.274 +
1.275 + elif self.first_method == "check-class":
1.276 + if assigning:
1.277 + emit(("__raise_type_error",))
1.278 + else:
1.279 + accessor = ("__check_and_load_via_class", accessor, attrname)
1.280 +
1.281 + # Access via the accessor itself.
1.282 +
1.283 + elif self.first_method == "check-object":
1.284 + if assigning:
1.285 + emit(("<set_attr_ref>", ("__check_and_get_object_attr_ref", accessor, attrname)))
1.286 + emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
1.287 + else:
1.288 + accessor = ("__check_and_load_via_object", accessor, attrname)
1.289 +
1.290 + # Access via a class accessor or the accessor's class.
1.291 + # Here, only access via the accessor is supported.
1.292 +
1.293 + elif self.first_method == "check-object-class":
1.294 + if assigning:
1.295 + emit(("<set_attr_ref>", ("__check_and_get_object_attr_ref", accessor, attrname)))
1.296 + emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
1.297 + else:
1.298 + accessor = ("__check_and_load_via_any", accessor, attrname)
1.299 +
1.300 + return accessor
1.301 +
1.302 + def process_traversed_attributes(self, accessor, context, emit):
1.303 +
1.304 + """
1.305 + Using 'accessor' and 'context', use 'emit' to generate instructions
1.306 + for the traversed attribute accesses. Return the potentially revised
1.307 + accessor and context indicators.
1.308 + """
1.309 +
1.310 + # Traverse attributes using the accessor.
1.311 +
1.312 + num_remaining = len(self.all_subsequent_attributes())
1.313 +
1.314 + if self.traversed_attrnames:
1.315 + for attrname, traversal_mode in self.attribute_traversals():
1.316 + assigning = num_remaining == 1 and self.final_method == "assign"
1.317 +
1.318 + # Set the context, if appropriate.
1.319 +
1.320 + if num_remaining == 1 and self.final_method != "assign" and self.context == "final-accessor":
1.321 +
1.322 + # Invoked attributes employ a separate context accessed
1.323 + # during invocation.
1.324 +
1.325 + if self.final_method in ("access-invoke", "static-invoke"):
1.326 + emit(("<set_context>", accessor))
1.327 + accessor = context = "<context>"
1.328 +
1.329 + # A private context within the access is otherwise
1.330 + # retained.
1.331 +
1.332 + else:
1.333 + emit(("<set_private_context>", accessor))
1.334 + accessor = context = "<private_context>"
1.335 +
1.336 + # Perform the access only if not achieved directly.
1.337 +
1.338 + if num_remaining > 1 or self.final_method in ("access", "access-invoke", "assign"):
1.339 +
1.340 + if traversal_mode == "class":
1.341 + if assigning:
1.342 + emit(("<set_attr_ref>", ("__get_class_attr_ref", accessor, attrname)))
1.343 + emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
1.344 + else:
1.345 + accessor = ("__load_via_class", accessor, attrname)
1.346 + else:
1.347 + if assigning:
1.348 + emit(("<set_attr_ref>", ("__get_object_attr_ref", accessor, attrname)))
1.349 + emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
1.350 + else:
1.351 + accessor = ("__load_via_object", accessor, attrname)
1.352 +
1.353 + num_remaining -= 1
1.354 +
1.355 + return accessor, context
1.356 +
1.357 + def process_remaining_attributes(self, accessor, context, emit):
1.358 +
1.359 + """
1.360 + Using 'accessor' and 'context', use 'emit' to generate instructions
1.361 + for the remaining attribute accesses. Return the potentially revised
1.362 + accessor and context indicators.
1.363 + """
1.364 +
1.365 + remaining = self.remaining_attrnames
1.366 +
1.367 + if remaining:
1.368 + num_remaining = len(remaining)
1.369 +
1.370 + for attrname in remaining:
1.371 + assigning = num_remaining == 1 and self.final_method == "assign"
1.372 +
1.373 + # Set the context, if appropriate.
1.374 +
1.375 + if num_remaining == 1 and self.final_method != "assign" and self.context == "final-accessor":
1.376 +
1.377 + # Invoked attributes employ a separate context accessed
1.378 + # during invocation.
1.379 +
1.380 + if self.final_method in ("access-invoke", "static-invoke"):
1.381 + emit(("<set_context>", accessor))
1.382 + accessor = context = "<context>"
1.383 +
1.384 + # A private context within the access is otherwise
1.385 + # retained.
1.386 +
1.387 + else:
1.388 + emit(("<set_private_context>", accessor))
1.389 + accessor = context = "<private_context>"
1.390 +
1.391 + # Perform the access only if not achieved directly.
1.392 +
1.393 + if num_remaining > 1 or self.final_method in ("access", "access-invoke", "assign"):
1.394 +
1.395 + # Constrain instructions involving certain special
1.396 + # attribute names.
1.397 +
1.398 + to_search = attrname != "__data__" and "any" or "object"
1.399 +
1.400 + if assigning:
1.401 + emit(("<set_attr_ref>", ("__check_and_get_object_attr_ref", accessor, attrname)))
1.402 + emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
1.403 + else:
1.404 + accessor = ("__check_and_load_via_%s" % to_search, accessor, attrname)
1.405 +
1.406 + num_remaining -= 1
1.407 +
1.408 + return accessor, context
1.409 +
1.410 + def process_attribute_access(self, accessor, context, test_accessor, emit):
1.411 +
1.412 + """
1.413 + Using 'accessor','context' and any 'test_accessor' operation, use 'emit'
1.414 + to generate instructions for the final attribute access. Return the
1.415 + potentially revised accessor.
1.416 + """
1.417 +
1.418 + # Define or emit the means of accessing the actual target.
1.419 +
1.420 + if self.final_method in ("static", "static-assign", "static-invoke"):
1.421 +
1.422 + if test_accessor:
1.423 + emit(test_accessor)
1.424 +
1.425 + # Assignments to known attributes.
1.426 +
1.427 + if self.final_method == "static-assign":
1.428 + parent, attrname = self.origin.rsplit(".", 1)
1.429 + emit(("<set_attr_ref>", ("__get_object_attr_ref", parent, attrname)))
1.430 + emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
1.431 +
1.432 + # Invoked attributes employ a separate context.
1.433 +
1.434 + elif self.final_method in ("static", "static-invoke"):
1.435 + accessor = ("__load_static_ignore", self.origin)
1.436 +
1.437 + # Wrap accesses in context operations.
1.438 +
1.439 + if self.context_test == "test":
1.440 +
1.441 + # Test and combine the context with static attribute details.
1.442 +
1.443 + if self.final_method == "static":
1.444 + emit(("__load_static_test", context, self.origin))
1.445 +
1.446 + # Test the context, storing it separately if required for the
1.447 + # immediately invoked static attribute.
1.448 +
1.449 + elif self.final_method == "static-invoke":
1.450 + emit(("<test_context_static>", context, self.origin))
1.451 +
1.452 + # Test the context, storing it separately if required for an
1.453 + # immediately invoked attribute.
1.454 +
1.455 + elif self.final_method == "access-invoke":
1.456 + emit(("<test_context_revert>", context, accessor))
1.457 +
1.458 + # Test the context and update the attribute details if
1.459 + # appropriate.
1.460 +
1.461 + else:
1.462 + emit(("__test_context", context, accessor))
1.463 +
1.464 + elif self.context_test == "replace":
1.465 +
1.466 + # Produce an object with updated context.
1.467 +
1.468 + if self.final_method == "static":
1.469 + emit(("__load_static_replace", context, self.origin))
1.470 +
1.471 + # Omit the context update operation where the target is static
1.472 + # and the context is recorded separately.
1.473 +
1.474 + elif self.final_method == "static-invoke":
1.475 + pass
1.476 +
1.477 + # If a separate context is used for an immediate invocation,
1.478 + # produce the attribute details unchanged.
1.479 +
1.480 + elif self.final_method == "access-invoke":
1.481 + emit(accessor)
1.482 +
1.483 + # Update the context in the attribute details.
1.484 +
1.485 + else:
1.486 + emit(("__update_context", context, accessor))
1.487 +
1.488 + # Omit the accessor for assignments and for invocations of static
1.489 + # targets. Otherwise, emit the accessor which may involve the
1.490 + # invocation of a test.
1.491 +
1.492 + elif self.final_method not in ("assign", "static-assign", "static-invoke"):
1.493 + emit(accessor)
1.494 +
1.495 + return accessor
1.496 +
1.497 + def process_context_identity(self, context, emit):
1.498 +
1.499 + """
1.500 + Using 'context', use 'emit' to generate instructions to test the context
1.501 + identity.
1.502 + """
1.503 +
1.504 + if context:
1.505 +
1.506 + # Only verify the context for invocation purposes if a suitable
1.507 + # test has been performed.
1.508 +
1.509 + if self.context_test in ("ignore", "replace") or \
1.510 + self.final_method in ("access-invoke", "static-invoke"):
1.511 +
1.512 + emit(("<context_identity_verified>", context))
1.513 + else:
1.514 + emit(("<context_identity>", context))
1.515 +
1.516 + def write(self, f, location):
1.517 +
1.518 + "Write the plan to file 'f' with the given 'location' information."
1.519 +
1.520 + print >>f, encode_access_location(location), \
1.521 + self.name or "{}", \
1.522 + self.test and "-".join(self.test) or "{}", \
1.523 + self.test_type or "{}", \
1.524 + self.base or "{}", \
1.525 + ".".join(self.traversed_attrnames) or "{}", \
1.526 + ".".join(self.traversed_attrname_modes) or "{}", \
1.527 + ".".join(self.remaining_attrnames) or "{}", \
1.528 + self.context, self.context_test, \
1.529 + self.first_method, self.final_method, self.origin or "{}", \
1.530 + ",".join(self.accessor_kinds)
1.531 +
1.532 +# vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/deducer.py Sun Sep 03 18:56:21 2023 +0200
2.2 +++ b/deducer.py Mon Sep 04 01:38:36 2023 +0200
2.3 @@ -19,6 +19,7 @@
2.4 this program. If not, see <http://www.gnu.org/licenses/>.
2.5 """
2.6
2.7 +from access_plan import AccessPlan
2.8 from common import first, get_assigned_attributes, get_attrnames, \
2.9 get_invoked_attributes, get_name_path, init_item, \
2.10 order_dependencies_partial, sorted_output, \
2.11 @@ -477,23 +478,7 @@
2.12 locations.sort()
2.13
2.14 for location in locations:
2.15 - name, test, test_type, base, \
2.16 - traversed, traversal_modes, attrnames, \
2.17 - context, context_test, \
2.18 - first_method, final_method, \
2.19 - attr, accessor_kinds = self.access_plans[location]
2.20 -
2.21 - print >>f_attrs, encode_access_location(location), \
2.22 - name or "{}", \
2.23 - test and "-".join(test) or "{}", \
2.24 - test_type or "{}", \
2.25 - base or "{}", \
2.26 - ".".join(traversed) or "{}", \
2.27 - ".".join(traversal_modes) or "{}", \
2.28 - ".".join(attrnames) or "{}", \
2.29 - context, context_test, \
2.30 - first_method, final_method, attr or "{}", \
2.31 - ",".join(accessor_kinds)
2.32 + self.access_plans[location].write(f_attrs, location)
2.33
2.34 finally:
2.35 f_attrs.close()
2.36 @@ -2637,354 +2622,18 @@
2.37 (base and "base" or "original-accessor") or \
2.38 "final-accessor"
2.39
2.40 - return name, test, test_type, base, \
2.41 - traversed, traversal_modes, remaining, \
2.42 - context, context_test, \
2.43 - first_method, final_method, \
2.44 - origin, accessor_kinds
2.45 + return AccessPlan(name, test, test_type, base,
2.46 + traversed, traversal_modes, remaining,
2.47 + context, context_test,
2.48 + first_method, final_method,
2.49 + origin, accessor_kinds)
2.50
2.51 def initialise_access_instructions(self):
2.52
2.53 "Expand access plans into instruction sequences."
2.54
2.55 for access_location, access_plan in self.access_plans.items():
2.56 -
2.57 - # Obtain the access details.
2.58 -
2.59 - name, test, test_type, base, \
2.60 - traversed, traversal_modes, attrnames, \
2.61 - context, context_test, \
2.62 - first_method, final_method, \
2.63 - origin, accessor_kinds = access_plan
2.64 -
2.65 - # Emit instructions by appending them to a list.
2.66 -
2.67 - instructions = []
2.68 - emit = instructions.append
2.69 -
2.70 - # Identify any static original accessor.
2.71 -
2.72 - if base:
2.73 - original_accessor = base
2.74 -
2.75 - # Employ names as contexts unless the context needs testing and
2.76 - # potentially updating. In such cases, temporary context storage is
2.77 - # used instead.
2.78 -
2.79 - elif name and not (context_test == "test" and
2.80 - final_method in ("access-invoke", "static-invoke")):
2.81 - original_accessor = "<name>" # refers to the name
2.82 -
2.83 - # Use a generic placeholder representing the access expression in
2.84 - # the general case.
2.85 -
2.86 - else:
2.87 - original_accessor = "<expr>"
2.88 -
2.89 - # Prepare for any first attribute access.
2.90 -
2.91 - if traversed:
2.92 - attrname = traversed[0]
2.93 - del traversed[0]
2.94 - elif attrnames:
2.95 - attrname = attrnames[0]
2.96 - del attrnames[0]
2.97 -
2.98 - # Perform the first access explicitly if at least one operation
2.99 - # requires it.
2.100 -
2.101 - access_first_attribute = final_method in ("access", "access-invoke", "assign") or traversed or attrnames
2.102 -
2.103 - # Determine whether the first access involves assignment.
2.104 -
2.105 - assigning = not traversed and not attrnames and final_method == "assign"
2.106 - set_accessor = assigning and "<set_target_accessor>" or "<set_accessor>"
2.107 - stored_accessor = assigning and "<target_accessor>" or "<accessor>"
2.108 -
2.109 - # Set the context if already available.
2.110 -
2.111 - context_var = None
2.112 -
2.113 - if context == "base":
2.114 - accessor = context_var = (base,)
2.115 - elif context == "original-accessor":
2.116 -
2.117 - # Prevent re-evaluation of any dynamic expression by storing it.
2.118 -
2.119 - if original_accessor == "<expr>":
2.120 - if final_method in ("access-invoke", "static-invoke"):
2.121 - emit(("<set_context>", original_accessor))
2.122 - accessor = context_var = ("<context>",)
2.123 - else:
2.124 - emit((set_accessor, original_accessor))
2.125 - accessor = context_var = (stored_accessor,)
2.126 - else:
2.127 - accessor = context_var = (original_accessor,)
2.128 -
2.129 - # Assigning does not set the context.
2.130 -
2.131 - elif context in ("final-accessor", "unset") and access_first_attribute:
2.132 -
2.133 - # Prevent re-evaluation of any dynamic expression by storing it.
2.134 -
2.135 - if original_accessor == "<expr>":
2.136 - emit((set_accessor, original_accessor))
2.137 - accessor = (stored_accessor,)
2.138 - else:
2.139 - accessor = (original_accessor,)
2.140 - else:
2.141 - accessor = None
2.142 -
2.143 - # Apply any test.
2.144 -
2.145 - if test[0] == "test":
2.146 - test_accessor = accessor = ("__%s_%s_%s" % test, accessor, test_type)
2.147 - else:
2.148 - test_accessor = None
2.149 -
2.150 - # Perform the first or final access.
2.151 - # The access only needs performing if the resulting accessor is used.
2.152 -
2.153 - remaining = len(traversed + attrnames)
2.154 -
2.155 - if access_first_attribute:
2.156 -
2.157 - # Access via the accessor's class.
2.158 -
2.159 - if first_method == "relative-class":
2.160 - if assigning:
2.161 - emit(("<set_attr_ref>", ("__get_class_attr_ref", accessor, attrname)))
2.162 - emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
2.163 - else:
2.164 - accessor = ("__load_via_class", accessor, attrname)
2.165 -
2.166 - # Access via the accessor itself.
2.167 -
2.168 - elif first_method == "relative-object":
2.169 - if assigning:
2.170 - emit(("<set_attr_ref>", ("__get_object_attr_ref", accessor, attrname)))
2.171 - emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
2.172 - else:
2.173 - accessor = ("__load_via_object", accessor, attrname)
2.174 -
2.175 - # Access via a class accessor or the accessor's class.
2.176 -
2.177 - elif first_method == "relative-object-class":
2.178 - if assigning:
2.179 - emit(("__raise_type_error",))
2.180 - else:
2.181 - accessor = ("__get_class_and_load", accessor, attrname)
2.182 -
2.183 - # Access via the accessor's class.
2.184 -
2.185 - elif first_method == "check-class":
2.186 - if assigning:
2.187 - emit(("__raise_type_error",))
2.188 - else:
2.189 - accessor = ("__check_and_load_via_class", accessor, attrname)
2.190 -
2.191 - # Access via the accessor itself.
2.192 -
2.193 - elif first_method == "check-object":
2.194 - if assigning:
2.195 - emit(("<set_attr_ref>", ("__check_and_get_object_attr_ref", accessor, attrname)))
2.196 - emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
2.197 - else:
2.198 - accessor = ("__check_and_load_via_object", accessor, attrname)
2.199 -
2.200 - # Access via a class accessor or the accessor's class.
2.201 - # Here, only access via the accessor is supported.
2.202 -
2.203 - elif first_method == "check-object-class":
2.204 - if assigning:
2.205 - emit(("<set_attr_ref>", ("__check_and_get_object_attr_ref", accessor, attrname)))
2.206 - emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
2.207 - else:
2.208 - accessor = ("__check_and_load_via_any", accessor, attrname)
2.209 -
2.210 - # Traverse attributes using the accessor.
2.211 -
2.212 - if traversed:
2.213 - for attrname, traversal_mode in zip(traversed, traversal_modes):
2.214 - assigning = remaining == 1 and final_method == "assign"
2.215 -
2.216 - # Set the context, if appropriate.
2.217 -
2.218 - if remaining == 1 and final_method != "assign" and context == "final-accessor":
2.219 -
2.220 - # Invoked attributes employ a separate context accessed
2.221 - # during invocation.
2.222 -
2.223 - if final_method in ("access-invoke", "static-invoke"):
2.224 - emit(("<set_context>", accessor))
2.225 - accessor = context_var = "<context>"
2.226 -
2.227 - # A private context within the access is otherwise
2.228 - # retained.
2.229 -
2.230 - else:
2.231 - emit(("<set_private_context>", accessor))
2.232 - accessor = context_var = "<private_context>"
2.233 -
2.234 - # Perform the access only if not achieved directly.
2.235 -
2.236 - if remaining > 1 or final_method in ("access", "access-invoke", "assign"):
2.237 -
2.238 - if traversal_mode == "class":
2.239 - if assigning:
2.240 - emit(("<set_attr_ref>", ("__get_class_attr_ref", accessor, attrname)))
2.241 - emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
2.242 - else:
2.243 - accessor = ("__load_via_class", accessor, attrname)
2.244 - else:
2.245 - if assigning:
2.246 - emit(("<set_attr_ref>", ("__get_object_attr_ref", accessor, attrname)))
2.247 - emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
2.248 - else:
2.249 - accessor = ("__load_via_object", accessor, attrname)
2.250 -
2.251 - remaining -= 1
2.252 -
2.253 - if attrnames:
2.254 - for attrname in attrnames:
2.255 - assigning = remaining == 1 and final_method == "assign"
2.256 -
2.257 - # Set the context, if appropriate.
2.258 -
2.259 - if remaining == 1 and final_method != "assign" and context == "final-accessor":
2.260 -
2.261 - # Invoked attributes employ a separate context accessed
2.262 - # during invocation.
2.263 -
2.264 - if final_method in ("access-invoke", "static-invoke"):
2.265 - emit(("<set_context>", accessor))
2.266 - accessor = context_var = "<context>"
2.267 -
2.268 - # A private context within the access is otherwise
2.269 - # retained.
2.270 -
2.271 - else:
2.272 - emit(("<set_private_context>", accessor))
2.273 - accessor = context_var = "<private_context>"
2.274 -
2.275 - # Perform the access only if not achieved directly.
2.276 -
2.277 - if remaining > 1 or final_method in ("access", "access-invoke", "assign"):
2.278 -
2.279 - # Constrain instructions involving certain special
2.280 - # attribute names.
2.281 -
2.282 - to_search = attrname != "__data__" and "any" or "object"
2.283 -
2.284 - if assigning:
2.285 - emit(("<set_attr_ref>", ("__check_and_get_object_attr_ref", accessor, attrname)))
2.286 - emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
2.287 - else:
2.288 - accessor = ("__check_and_load_via_%s" % to_search, accessor, attrname)
2.289 -
2.290 - remaining -= 1
2.291 -
2.292 - # Make any accessor test available if not emitted.
2.293 -
2.294 - test_accessor = not instructions and test_accessor or None
2.295 -
2.296 - # Define or emit the means of accessing the actual target.
2.297 -
2.298 - if final_method in ("static", "static-assign", "static-invoke"):
2.299 -
2.300 - if test_accessor:
2.301 - emit(test_accessor)
2.302 -
2.303 - # Assignments to known attributes.
2.304 -
2.305 - if final_method == "static-assign":
2.306 - parent, attrname = origin.rsplit(".", 1)
2.307 - emit(("<set_attr_ref>", ("__get_object_attr_ref", parent, attrname)))
2.308 - emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
2.309 -
2.310 - # Invoked attributes employ a separate context.
2.311 -
2.312 - elif final_method in ("static", "static-invoke"):
2.313 - accessor = ("__load_static_ignore", origin)
2.314 -
2.315 - # Wrap accesses in context operations.
2.316 -
2.317 - if context_test == "test":
2.318 -
2.319 - # Test and combine the context with static attribute details.
2.320 -
2.321 - if final_method == "static":
2.322 - emit(("__load_static_test", context_var, origin))
2.323 -
2.324 - # Test the context, storing it separately if required for the
2.325 - # immediately invoked static attribute.
2.326 -
2.327 - elif final_method == "static-invoke":
2.328 - emit(("<test_context_static>", context_var, origin))
2.329 -
2.330 - # Test the context, storing it separately if required for an
2.331 - # immediately invoked attribute.
2.332 -
2.333 - elif final_method == "access-invoke":
2.334 - emit(("<test_context_revert>", context_var, accessor))
2.335 -
2.336 - # Test the context and update the attribute details if
2.337 - # appropriate.
2.338 -
2.339 - else:
2.340 - emit(("__test_context", context_var, accessor))
2.341 -
2.342 - elif context_test == "replace":
2.343 -
2.344 - # Produce an object with updated context.
2.345 -
2.346 - if final_method == "static":
2.347 - emit(("__load_static_replace", context_var, origin))
2.348 -
2.349 - # Omit the context update operation where the target is static
2.350 - # and the context is recorded separately.
2.351 -
2.352 - elif final_method == "static-invoke":
2.353 - pass
2.354 -
2.355 - # If a separate context is used for an immediate invocation,
2.356 - # produce the attribute details unchanged.
2.357 -
2.358 - elif final_method == "access-invoke":
2.359 - emit(accessor)
2.360 -
2.361 - # Update the context in the attribute details.
2.362 -
2.363 - else:
2.364 - emit(("__update_context", context_var, accessor))
2.365 -
2.366 - # Omit the accessor for assignments and for invocations of static
2.367 - # targets. Otherwise, emit the accessor which may involve the
2.368 - # invocation of a test.
2.369 -
2.370 - elif final_method not in ("assign", "static-assign", "static-invoke"):
2.371 - emit(accessor)
2.372 -
2.373 - # Produce an advisory instruction regarding the context.
2.374 -
2.375 - if context_var:
2.376 -
2.377 - # Only verify the context for invocation purposes if a suitable
2.378 - # test has been performed.
2.379 -
2.380 - if context_test in ("ignore", "replace") or \
2.381 - final_method in ("access-invoke", "static-invoke"):
2.382 -
2.383 - emit(("<context_identity_verified>", context_var))
2.384 - else:
2.385 - emit(("<context_identity>", context_var))
2.386 -
2.387 - # Produce an advisory instruction regarding the final attribute.
2.388 -
2.389 - if origin:
2.390 - emit(("<final_identity>", origin))
2.391 -
2.392 - self.access_instructions[access_location] = instructions
2.393 - self.accessor_kinds[access_location] = accessor_kinds
2.394 + self.access_instructions[access_location] = access_plan.get_instructions()
2.395 + self.accessor_kinds[access_location] = access_plan.accessor_kinds
2.396
2.397 # vim: tabstop=4 expandtab shiftwidth=4