def isInitializedNativeExpr(self, instance, ix):
        byte = ix // 8
        bit = ix % 8

        return (instance.nonref_expr.cast(
            native_ast.UInt8.pointer()).ElementPtrIntegers(
                8 + byte).load().rshift(
                    native_ast.const_uint8_expr(bit)).bitand(
                        native_ast.const_uint8_expr(1)))
示例#2
0
    def convert_to_type(self, context, expr, otherType):
        if otherType == self:
            return expr

        if otherType.typeRepresentation in self.typeRepresentation.Types:
            # this is wrong - we need to be unpacking each of the alternatives
            # and attempting to convert them. Which should probably be a function...
            assert expr.isReference

            which = tuple(self.typeRepresentation.Types).index(
                otherType.typeRepresentation)

            result = context.allocateUninitializedSlot(otherType)

            with context.ifelse(
                    expr.expr.ElementPtrIntegers(0, 0).load().eq(
                        native_ast.const_uint8_expr(which))) as (true, false):
                with true:
                    result.convert_copy_initialize(
                        self.refAs(context, expr, which))
                    context.markUninitializedSlotInitialized(result)
                with false:
                    context.pushEffect(
                        generateThrowException(context,
                                               Exception("Can't convert")))

            return result
        else:
            return super().convert_to_type(context, expr, otherType)
    def setIsInitializedExpr(self, instance, ix):
        byte = ix // 8
        bit = ix % 8

        bytePtr = (instance.nonref_expr.cast(
            native_ast.UInt8.pointer()).ElementPtrIntegers(8 + byte))

        return bytePtr.store(bytePtr.load().bitor(
            native_ast.const_uint8_expr(1 << bit)))
示例#4
0
    def convert_default_initialize(self, context, target):
        for i, t in enumerate(self.typeRepresentation.Types):
            if _types.is_default_constructible(t):
                self.refAs(context, target, i).convert_default_initialize()
                context.pushEffect(
                    target.expr.ElementPtrIntegers(0, 0).store(
                        native_ast.const_uint8_expr(i)))
                return

        context.pushException(
            TypeError, "Can't default-initialize any subtypes of %s" %
            self.typeRepresentation.__qualname__)
    def generateConstructor(self, context, out, *args):
        context.pushEffect(
            out.expr.store(
                runtime_functions.malloc.call(
                    native_ast.const_int_expr(
                        _types.bytecount(self.typeRepresentation.HeldClass) +
                        8)).cast(self.getNativeLayoutType())) >>
            # store a refcount
            out.expr.load().ElementPtrIntegers(0, 0).store(
                native_ast.const_int_expr(1)))

        # clear bits of init flags
        for byteOffset in range(self.bytesOfInitBits):
            context.pushEffect(
                out.nonref_expr.cast(
                    native_ast.UInt8.pointer()).ElementPtrIntegers(
                        8 + byteOffset).store(native_ast.const_uint8_expr(0)))

        for i in range(len(self.classType.MemberTypes)):
            if _types.wantsToDefaultConstruct(self.classType.MemberTypes[i]):
                name = self.classType.MemberNames[i]

                if name in self.classType.MemberDefaultValues:
                    defVal = self.classType.MemberDefaultValues.get(name)
                    context.pushReference(
                        self.classType.MemberTypes[i],
                        self.memberPtr(out, i)).convert_copy_initialize(
                            nativepython.python_object_representation.
                            pythonObjectRepresentation(context, defVal))
                else:
                    context.pushReference(self.classType.MemberTypes[i],
                                          self.memberPtr(
                                              out,
                                              i)).convert_default_initialize()
                context.pushEffect(self.setIsInitializedExpr(out, i))

        if '__init__' in self.typeRepresentation.MemberFunctions:
            initFuncType = typeWrapper(
                self.typeRepresentation.MemberFunctions['__init__'])
            initFuncType.convert_call(context, context.pushVoid(initFuncType),
                                      (out, ) + args, {})
        else:
            if len(args):
                context.pushException(
                    TypeError, "Can't construct a " +
                    self.typeRepresentation.__qualname__ +
                    " with positional arguments because it doesn't have an __init__"
                )
示例#6
0
    def convert_copy_initialize(self, context, expr, other):
        assert expr.isReference

        if self.is_pod:
            context.pushEffect(expr.expr.store(other.nonref_expr))
        else:
            with context.switch(
                    other.expr.ElementPtrIntegers(0, 0).load(),
                    range(len(self.typeRepresentation.Types)),
                    False) as indicesAndContexts:
                for ix, subcontext in indicesAndContexts:
                    with subcontext:
                        self.refAs(context, expr, ix).convert_copy_initialize(
                            self.refAs(context, other, ix))
                        context.pushEffect(
                            expr.expr.ElementPtrIntegers(0, 0).store(
                                native_ast.const_uint8_expr(ix)))
示例#7
0
    def convert_to_self_with_target(self, context, result, otherExpr):
        if otherExpr.expr_type == self:
            result.convert_copy_initialize(otherExpr)
        elif isinstance(otherExpr.expr_type, OneOfWrapper):
            with context.switch(
                    otherExpr.expr.ElementPtrIntegers(0, 0).load(),
                    range(len(otherExpr.expr_type.typeRepresentation.Types)),
                    False) as indicesAndContexts:
                for i, subcontext in indicesAndContexts:
                    with subcontext:
                        self.convert_to_self_with_target(
                            context, result,
                            otherExpr.expr_type.refAs(context, otherExpr, i))
        elif otherExpr.expr_type.typeRepresentation in self.typeRepresentation.Types:
            which = tuple(self.typeRepresentation.Types).index(
                otherExpr.expr_type.typeRepresentation)

            context.pushEffect(
                result.expr.ElementPtrIntegers(0, 0).store(
                    native_ast.const_uint8_expr(which)))
            self.refAs(context, result,
                       which).convert_copy_initialize(otherExpr)
        else:
            return super().convert_to_self(context, otherExpr)