mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-14 13:23:53 +00:00
Implement arrays in the .NET backend.
Estimated hours taken: 15 Branches: main Implement arrays in the .NET backend. Arrays are implemented in an unusual way. array(T) is mapped to the .NET type T[] for all concrete types T. However, if T is a type variable, we map array(T) to System.Array. System.Array is the superclass of all array types, so we can always pass T[] where System.Array is expected. This mapping allows us to use the Mercury array library module to operate on arrays that may be exposed by other .NET components (for example Byte[] or int[]). In some cases the Mercury compiler will know the value of T, but it may be calling code that operates on array(T) in a generic fashion. If the callee returns an array(T), the Mercury compiler can be sure that this is really T[], but the .NET backend is not that clever. In that case we have to cast from System.Array to T[] after the call. (This is very similar to how we unbox return values with type T where T is known in the caller but not the callee). We also insert casts from T[] to System.Array -- although they are not strictly necessary. compiler/ml_call_gen.m: Implement the cast to the known instance of array(T) for return values of array. compiler/mlds.m: Add a new MLDS type mlds__mercury_array_type to represent the Mercury array/1 type. Turn the Mercury type array/1 into mlds__mercury_array_type. compiler/mlds_to_c.m: compiler/mlds_to_gcc.m: Use MR_Word to represent mlds__mercury_array_type, unless we are using --high-level-data, in which case we use the appropriately named high-level-data type. compiler/mlds_to_il.m: Draw a distinction between the il_object_array_type (object[]) and the il_generic_array_type (class System.Array). Map mlds__mercury_array_type(ElemType) to class System.Array if ElemType is a type variable, or ElemType[] otherwise. compiler/mlds_to_java.m: Map mlds__mercury_array_type(ElemType) to class Object[] if ElemType is a type variable, or ElemType[] otherwise. (type variables are mapped to Object anyway, so this falls out of the code anyway). (This is untested).
This commit is contained in:
@@ -856,6 +856,7 @@ get_java_type_initializer(mercury_type(_, tuple_type)) = "null".
|
||||
get_java_type_initializer(mercury_type(_, enum_type)) = "null".
|
||||
get_java_type_initializer(mercury_type(_, polymorphic_type)) = "null".
|
||||
get_java_type_initializer(mercury_type(_, user_type)) = "null".
|
||||
get_java_type_initializer(mlds__mercury_array_type(_)) = "null".
|
||||
get_java_type_initializer(mlds__cont_type(_)) = "null".
|
||||
get_java_type_initializer(mlds__commit_type) = "null".
|
||||
get_java_type_initializer(mlds__native_bool_type) = "false".
|
||||
@@ -1206,6 +1207,14 @@ output_data_name(tabling_pointer(ProcLabel)) -->
|
||||
|
||||
output_type(mercury_type(Type, TypeCategory)) -->
|
||||
output_mercury_type(Type, TypeCategory).
|
||||
|
||||
% XXX array types need to be output as
|
||||
% MLDSType varname[]
|
||||
% not
|
||||
% MLDSType[] varname
|
||||
output_type(mercury_array_type(MLDSType)) -->
|
||||
output_type(MLDSType),
|
||||
io__write_string("[]").
|
||||
output_type(mlds__native_int_type) --> io__write_string("int").
|
||||
output_type(mlds__native_float_type) --> io__write_string("double").
|
||||
output_type(mlds__native_bool_type) --> io__write_string("boolean").
|
||||
@@ -1225,6 +1234,10 @@ output_type(mlds__ptr_type(Type)) -->
|
||||
;
|
||||
output_type(Type)
|
||||
).
|
||||
% XXX array types need to be output as
|
||||
% MLDSType varname[]
|
||||
% not
|
||||
% MLDSType[] varname
|
||||
output_type(mlds__array_type(Type)) -->
|
||||
output_type(Type),
|
||||
io__write_string("[]").
|
||||
|
||||
Reference in New Issue
Block a user