1.1 --- a/translator.py Mon Dec 05 13:01:21 2016 +0100
1.2 +++ b/translator.py Mon Dec 05 16:03:06 2016 +0100
1.3 @@ -154,6 +154,26 @@
1.4 def __str__(self):
1.5 return str(self.node)
1.6
1.7 +class TrInstanceRef(results.InstanceRef, TranslationResult):
1.8 +
1.9 + "A reference representing instantiation of a class."
1.10 +
1.11 + def __init__(self, ref, expr):
1.12 +
1.13 + """
1.14 + Initialise the reference with 'ref' indicating the nature of the
1.15 + reference and 'expr' being an expression used to create the instance.
1.16 + """
1.17 +
1.18 + results.InstanceRef.__init__(self, ref)
1.19 + self.expr = expr
1.20 +
1.21 + def __str__(self):
1.22 + return self.expr
1.23 +
1.24 + def __repr__(self):
1.25 + return "TrResolvedInstanceRef(%r, %r)" % (self.ref, self.expr)
1.26 +
1.27 class AttrResult(Expression, TranslationResult):
1.28
1.29 "A translation result for an attribute access."
1.30 @@ -1022,6 +1042,7 @@
1.31 objpath = expr.get_origin()
1.32 target = None
1.33 function = None
1.34 + instantiation = False
1.35 literal_instantiation = False
1.36 context_required = True
1.37
1.38 @@ -1030,7 +1051,7 @@
1.39 # Literals may be instantiated specially.
1.40
1.41 if expr.is_name() and expr.name.startswith("$L") and objpath:
1.42 - literal_instantiation = True
1.43 + instantiation = literal_instantiation = objpath
1.44 parameters = None
1.45 target = encode_literal_instantiator(objpath)
1.46 context_required = False
1.47 @@ -1043,6 +1064,7 @@
1.48 # Class invocation involves instantiators.
1.49
1.50 if expr.has_kind("<class>"):
1.51 + instantiation = objpath
1.52 target = encode_instantiator_pointer(objpath)
1.53 target_structure = "&%s" % encode_bound_reference("%s.__init__" % objpath)
1.54 context_required = False
1.55 @@ -1195,7 +1217,10 @@
1.56 len(kwargs), kwcodestr, kwargstr,
1.57 len(args), argstr)
1.58
1.59 - return make_expression(output)
1.60 + if instantiation:
1.61 + return TrInstanceRef(instantiation, output)
1.62 + else:
1.63 + return make_expression(output)
1.64
1.65 def always_callable(self, refs):
1.66
1.67 @@ -1342,7 +1367,15 @@
1.68 # NOTE: Determine which raise statement variants should be permitted.
1.69
1.70 if n.expr1:
1.71 - self.writestmt("__Raise(%s);" % self.process_structure_node(n.expr1))
1.72 + exc = self.process_structure_node(n.expr1)
1.73 +
1.74 + # Raise instances, testing the kind at run-time if necessary and
1.75 + # instantiating any non-instance.
1.76 +
1.77 + if isinstance(exc, TrInstanceRef):
1.78 + self.writestmt("__Raise(%s);" % exc)
1.79 + else:
1.80 + self.writestmt("__Raise(__ensure_instance(%s));" % exc)
1.81 else:
1.82 self.writestmt("__Complete;")
1.83