Beispiel #1
0
    def _create_wrapper_check(self, check_var, parse_args, types_dict, used_names, func_name):
        check_func_body = []
        flags = (len(types_dict) - 1) * 4
        for arg in types_dict:
            var_name = ""
            body = []
            types = []
            arg_type_check_list = list(types_dict[arg])
            arg_type_check_list.sort(key= lambda x : x[0].precision)
            for elem in arg_type_check_list:
                var_name = elem[0].name
                value = elem[2] << flags
                body.append((elem[1], [AugAssign(check_var, '+' ,value)]))
                types.append(elem[0])
            flags -= 4
            error = ' or '.join(['{} bit {}'.format(v.precision * 8 , str_dtype(v.dtype)) if not isinstance(v.dtype, NativeBool)
                            else  str_dtype(v.dtype) for v in types])
            body.append((LiteralTrue(), [PyErr_SetString('PyExc_TypeError', '"{} must be {}"'.format(var_name, error)), Return([LiteralInteger(0)])]))
            check_func_body += [If(*body)]

        check_func_body = [Assign(check_var, LiteralInteger(0))] + check_func_body
        check_func_body.append(Return([check_var]))
        # Creating check function definition
        check_func_name = self.get_new_name(used_names.union(self._global_names), 'type_check')
        self._global_names.add(check_func_name)
        check_func_def = FunctionDef(name = check_func_name,
            arguments = parse_args,
            results = [check_var],
            body = check_func_body,
            local_vars = [])
        return check_func_def
Beispiel #2
0
    def __new__(cls, shape, fill_value, dtype=None, order='C'):

        # Convert shape to PythonTuple
        shape = process_shape(shape)

        # If there is no dtype, extract it from fill_value
        # TODO: must get dtype from an annotated node
        if (dtype is None) or isinstance(dtype, Nil):
            dtype = fill_value.dtype

        # Verify dtype and get precision
        dtype, precision = process_dtype(dtype)

        # Verify array ordering
        order = NumpyNewArray._process_order(order)

        # Cast fill_value to correct type
        # TODO [YG, 09.11.2020]: treat difficult case of LiteralComplex
        from pyccel.ast.datatypes import str_dtype
        stype = str_dtype(dtype)
        if stype != 'complex':
            from pyccel.codegen.printing.fcode import python_builtin_datatypes
            cast_func = python_builtin_datatypes[stype]
            fill_value = cast_func(fill_value)

        return Basic.__new__(cls, shape, dtype, order, precision, fill_value)
Beispiel #3
0
 def __init__(self, x):
     self._shape = x.shape
     self._rank = x.rank
     self._dtype = NativeInteger(
     ) if x.dtype is NativeInteger() else NativeReal()
     self._precision = default_precision[str_dtype(self._dtype)]
     self._order = x.order
Beispiel #4
0
 def __init__(self, arg):
     if not isinstance(arg, (list, tuple, PythonTuple, Tuple, PythonList,
                             Variable, Expr)):
         raise TypeError('Uknown type of  %s.' % type(arg))
     PyccelInternalFunction.__init__(self, arg)
     self._dtype = arg.dtype
     self._rank  = 0
     self._shape = ()
     self._precision = default_precision[str_dtype(self._dtype)]
Beispiel #5
0
    def _get_check_type_statement(self, variable, collect_var):

        if variable.rank > 0 :
            numpy_dtype = self.find_in_numpy_dtype_registry(variable)
            check = PyccelEq(FunctionCall(numpy_get_type, [collect_var]), numpy_dtype)

        else :
            python_check = PythonType_Check(variable, collect_var)
            numpy_check = NumpyType_Check(variable, collect_var)
            if variable.precision == default_precision[str_dtype(variable.dtype)] :
                check = PyccelOr(python_check, numpy_check)
            else :
                check = PyccelAssociativeParenthesis(PyccelAnd(PyccelNot(python_check), numpy_check))

        if isinstance(variable, ValuedVariable):
            default = PyccelNot(VariableAddress(collect_var)) if variable.rank > 0 else PyccelEq(VariableAddress(collect_var), VariableAddress(Py_None))
            check = PyccelAssociativeParenthesis(PyccelOr(default, check))

        return check
Beispiel #6
0
 def __init__(self, arg):
     self._dtype = arg.dtype
     self._rank = 0
     self._shape = ()
     self._precision = default_precision[str_dtype(self._dtype)]
Beispiel #7
0
 def __init__(self, x):
     self._shape = x.shape
     self._rank = x.rank
     self._dtype = NativeReal()
     self._precision = default_precision[str_dtype(self._dtype)]
Beispiel #8
0
 def __init__(self, x):
     self._shape = x.shape
     self._rank = x.rank
     self._dtype = x.dtype if x.dtype is NativeComplex() else NativeReal()
     self._precision = default_precision[str_dtype(self._dtype)]
Beispiel #9
0
 def _set_dtype_precision(self, x):
     self._dtype     = NativeReal()
     self._precision = default_precision[str_dtype(self._dtype)]
Beispiel #10
0
 def _set_dtype_precision(self, x):
     self._dtype     = NativeInteger() if x.dtype is NativeInteger() else NativeReal()
     self._precision = default_precision[str_dtype(self._dtype)]
Beispiel #11
0
 def _set_dtype_precision(self, x):
     self._dtype      = x.dtype if x.dtype is NativeComplex() else NativeReal()
     self._precision  = default_precision[str_dtype(self._dtype)]