Exemple #1
0
    def redispatch_call(self, hop, call_args):
        s_instance = hop.s_result
        r_instance = hop.r_result

        if len(self.s_pbc.descriptions) == 1:
            # instantiating a single class
            if self.lowleveltype is not Void:
                assert 0, "XXX None-or-1-class instantation not implemented"
            assert isinstance(s_instance, annmodel.SomeInstance)
            classdef = s_instance.classdef
            s_init = classdef.classdesc.s_read_attribute('__init__')
            v_init = Constant("init-func-dummy")  # this value not really used

            if (isinstance(s_init, annmodel.SomeImpossibleValue)
                    and classdef.classdesc.is_exception_class()
                    and classdef.has_no_attrs()):
                # special case for instanciating simple built-in
                # exceptions: always return the same prebuilt instance,
                # and ignore any arguments passed to the constructor.
                r_instance = rclass.getinstancerepr(hop.rtyper, classdef)
                example = r_instance.get_reusable_prebuilt_instance()
                hop.exception_cannot_occur()
                return hop.inputconst(r_instance.lowleveltype, example)

            v_instance = rclass.rtype_new_instance(hop.rtyper, classdef,
                                                   hop.llops, hop)
            if isinstance(v_instance, tuple):
                v_instance, must_call_init = v_instance
                if not must_call_init:
                    return v_instance
        else:
            # instantiating a class from multiple possible classes
            vtypeptr = hop.inputarg(self, arg=0)
            try:
                access_set, r_class = self.get_access_set('__init__')
            except rclass.MissingRTypeAttribute:
                s_init = annmodel.s_ImpossibleValue
            else:
                s_init = access_set.s_value
                v_init = r_class.getpbcfield(vtypeptr, access_set, '__init__',
                                             hop.llops)
            v_instance = self._instantiate_runtime_class(
                hop, vtypeptr, r_instance)

        if isinstance(s_init, annmodel.SomeImpossibleValue):
            assert hop.nb_args == 1, ("arguments passed to __init__, "
                                      "but no __init__!")
            hop.exception_cannot_occur()
        else:
            hop2 = self.replace_class_with_inst_arg(hop, v_instance,
                                                    s_instance, call_args)
            hop2.v_s_insertfirstarg(v_init, s_init)  # add 'initfunc'
            hop2.s_result = annmodel.s_None
            hop2.r_result = self.rtyper.getrepr(hop2.s_result)
            # now hop2 looks like simple_call(initfunc, instance, args...)
            hop2.dispatch()
        return v_instance
Exemple #2
0
    def redispatch_call(self, hop, call_args):
        s_instance = hop.s_result
        r_instance = hop.r_result

        if len(self.s_pbc.descriptions) == 1:
            # instantiating a single class
            if self.lowleveltype is not Void:
                assert 0, "XXX None-or-1-class instantation not implemented"
            assert isinstance(s_instance, annmodel.SomeInstance)
            classdef = s_instance.classdef
            s_init = classdef.classdesc.s_read_attribute('__init__')
            v_init = Constant("init-func-dummy")   # this value not really used

            if (isinstance(s_init, annmodel.SomeImpossibleValue) and
                classdef.classdesc.is_exception_class() and
                classdef.has_no_attrs()):
                # special case for instanciating simple built-in
                # exceptions: always return the same prebuilt instance,
                # and ignore any arguments passed to the contructor.
                r_instance = rclass.getinstancerepr(hop.rtyper, classdef)
                example = r_instance.get_reusable_prebuilt_instance()
                hop.exception_cannot_occur()
                return hop.inputconst(r_instance.lowleveltype, example)

            v_instance = rclass.rtype_new_instance(hop.rtyper, classdef,
                                                   hop.llops, hop)
            if isinstance(v_instance, tuple):
                v_instance, must_call_init = v_instance
                if not must_call_init:
                    return v_instance
        else:
            # instantiating a class from multiple possible classes
            vtypeptr = hop.inputarg(self, arg=0)
            try:
                access_set, r_class = self.get_access_set('__init__')
            except rclass.MissingRTypeAttribute:
                s_init = annmodel.s_ImpossibleValue
            else:
                s_init = access_set.s_value
                v_init = r_class.getpbcfield(vtypeptr, access_set, '__init__',
                                             hop.llops)
            v_instance = self._instantiate_runtime_class(hop, vtypeptr, r_instance)

        if isinstance(s_init, annmodel.SomeImpossibleValue):
            assert hop.nb_args == 1, ("arguments passed to __init__, "
                                      "but no __init__!")
            hop.exception_cannot_occur()
        else:
            hop2 = self.replace_class_with_inst_arg(
                    hop, v_instance, s_instance, call_args)
            hop2.v_s_insertfirstarg(v_init, s_init)   # add 'initfunc'
            hop2.s_result = annmodel.s_None
            hop2.r_result = self.rtyper.getrepr(hop2.s_result)
            # now hop2 looks like simple_call(initfunc, instance, args...)
            hop2.dispatch()
        return v_instance
Exemple #3
0
def rtype_instantiate(hop):
    hop.exception_cannot_occur()
    s_class = hop.args_s[0]
    assert isinstance(s_class, annmodel.SomePBC)
    if len(s_class.descriptions) != 1:
        # instantiate() on a variable class
        vtypeptr, = hop.inputargs(rclass.get_type_repr(hop.rtyper))
        r_class = hop.args_r[0]
        return r_class._instantiate_runtime_class(hop, vtypeptr,
                                                  hop.r_result.lowleveltype)
    classdef = s_class.any_description().getuniqueclassdef()
    return rclass.rtype_new_instance(hop.rtyper, classdef, hop.llops)
Exemple #4
0
def rtype_instantiate(hop):
    hop.exception_cannot_occur()
    s_class = hop.args_s[0]
    assert isinstance(s_class, annmodel.SomePBC)
    if len(s_class.descriptions) != 1:
        # instantiate() on a variable class
        vtypeptr, = hop.inputargs(rclass.get_type_repr(hop.rtyper))
        r_class = hop.args_r[0]
        return r_class._instantiate_runtime_class(hop, vtypeptr,
                                                  hop.r_result.lowleveltype)
    classdef = s_class.any_description().getuniqueclassdef()
    return rclass.rtype_new_instance(hop.rtyper, classdef, hop.llops)
def rtype_instantiate(hop, i_nonmovable=None):
    hop.exception_cannot_occur()
    s_class = hop.args_s[0]
    assert isinstance(s_class, annmodel.SomePBC)
    v_nonmovable, = parse_kwds(hop, (i_nonmovable, None))
    nonmovable = (i_nonmovable is not None and v_nonmovable.value)
    if len(s_class.descriptions) != 1:
        # instantiate() on a variable class
        if nonmovable:
            raise TyperError("instantiate(x, nonmovable=True) cannot be used "
                             "if x is not a constant class")
        vtypeptr, = hop.inputargs(rclass.get_type_repr(hop.rtyper))
        r_class = hop.args_r[0]
        return r_class._instantiate_runtime_class(hop, vtypeptr,
                                                  hop.r_result.lowleveltype)
    classdef = s_class.any_description().getuniqueclassdef()
    return rclass.rtype_new_instance(hop.rtyper, classdef, hop.llops,
                                     nonmovable=nonmovable)
Exemple #6
0
def rtype_instantiate(hop, i_nonmovable=None):
    hop.exception_cannot_occur()
    s_class = hop.args_s[0]
    assert isinstance(s_class, annmodel.SomePBC)
    v_nonmovable, = parse_kwds(hop, (i_nonmovable, None))
    nonmovable = (i_nonmovable is not None and v_nonmovable.value)
    if len(s_class.descriptions) != 1:
        # instantiate() on a variable class
        if nonmovable:
            raise TyperError("instantiate(x, nonmovable=True) cannot be used "
                             "if x is not a constant class")
        vtypeptr, = hop.inputargs(rclass.get_type_repr(hop.rtyper))
        r_class = hop.args_r[0]
        return r_class._instantiate_runtime_class(hop, vtypeptr,
                                                  hop.r_result.lowleveltype)
    classdef = s_class.any_description().getuniqueclassdef()
    return rclass.rtype_new_instance(hop.rtyper,
                                     classdef,
                                     hop.llops,
                                     nonmovable=nonmovable)
Exemple #7
0
 def translate_op_instantiate1(self, hop):
     if not isinstance(hop.s_result, annmodel.SomeInstance):
         raise TyperError("instantiate1 got s_result=%r" % (hop.s_result, ))
     classdef = hop.s_result.classdef
     return rclass.rtype_new_instance(self, classdef, hop.llops)
Exemple #8
0
 def translate_op_instantiate1(self, hop):
     if not isinstance(hop.s_result, annmodel.SomeInstance):
         raise TyperError("instantiate1 got s_result=%r" % (hop.s_result,))
     classdef = hop.s_result.classdef
     return rclass.rtype_new_instance(self, classdef, hop.llops)