diff --git a/integration_tests/test_c_interop_01.py b/integration_tests/test_c_interop_01.py index 20643d7641..3e22156a27 100644 --- a/integration_tests/test_c_interop_01.py +++ b/integration_tests/test_c_interop_01.py @@ -17,6 +17,10 @@ def _lfortran_bgt32(i: i32, j: i32) -> i32: def _lfortran_bgt64(i: i64, j: i64) -> i64: pass +@ccall +def _lfortran_random_number(n: i64, v: f64[:]): + pass + assert abs(_lfortran_dsin(pi) - 0) < 1e-12 assert abs(_lfortran_dsin(pi/2) - 1) < 1e-12 assert abs(_lfortran_ssin(pi) - 0) < 1e-6 diff --git a/src/runtime/ltypes/ltypes.py b/src/runtime/ltypes/ltypes.py index a8aac2a5c5..c8267355e4 100644 --- a/src/runtime/ltypes/ltypes.py +++ b/src/runtime/ltypes/ltypes.py @@ -1,17 +1,28 @@ from inspect import getfullargspec, getcallargs -from typing import types import os import ctypes import platform # data-types -i32 = types.new_class("i32") -i64 = types.new_class("i64") -f32 = types.new_class("f32") -f64 = types.new_class("f64") -c32 = types.new_class("c32") -c64 = types.new_class("c64") +class Type: + def __init__(self, name): + self._name = name + + def __getitem__(self, params): + return Array(self, params) + +class Array: + def __init__(self, type, dims): + self._type = type + self._dims = dims + +i32 = Type("i32") +i64 = Type("i64") +f32 = Type("f32") +f64 = Type("f64") +c32 = Type("c32") +c64 = Type("c64") # Overloading support @@ -93,6 +104,11 @@ def convert_type_to_ctype(arg): return ctypes.c_int64 elif arg == i32: return ctypes.c_int32 + elif arg is None: + raise NotImplementedError("Type cannot be None") + elif isinstance(arg, Array): + type = convert_type_to_ctype(arg._type) + return ctypes.POINTER(type) else: raise NotImplementedError("Type not implemented") def get_rtlib_dir(): @@ -121,8 +137,10 @@ def get_crtlib_path(): arg_ctype = convert_type_to_ctype(arg_type) argtypes.append(arg_ctype) self.cf.argtypes = argtypes - res_type = self.annotations["return"] - self.cf.restype = convert_type_to_ctype(res_type) + if "return" in self.annotations: + res_type = self.annotations["return"] + if res_type is not None: + self.cf.restype = convert_type_to_ctype(res_type) def __call__(self, *args, **kwargs): if len(kwargs) > 0: