Skip to content

Commit c472750

Browse files
authored
Merge pull request #2079 from czgdp1807/phys_types
Implement physical types for arrays
2 parents 39e1a26 + 0b98e36 commit c472750

File tree

71 files changed

+3835
-2106
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+3835
-2106
lines changed

integration_tests/test_dict_increment.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ def test_dict_increment():
2222
d_int_int[1] += i1
2323
i2 += i1
2424
assert d_int_int[1] == i2
25-
25+
2626
i2 = 10
2727
d_int_int = {0: 0, 1: 0}
2828
for i1 in range(i2):
2929
d_int_int[i1 % 2] += 1
3030
assert d_int_int[0] == d_int_int[1]
3131
assert d_int_int[0] == i2 // 2
32-
32+
3333
j1 = 2.0
3434
d_int_float = {2: j1}
3535
while j1 < 4.0:
3636
d_int_float[2] += 0.1
3737
j1 += 0.1
3838
assert d_int_float[2] == j1
39-
39+
4040
j1 = 0.0
4141
j2 = 0.0
4242
d_bool_float = {True: 0.0, False: 0.0}
@@ -63,4 +63,4 @@ def test_dict_increment():
6363
s1 += str(i1)
6464
assert d_int_str[-1] == s1
6565

66-
test_dict_increment()
66+
test_dict_increment()

src/libasr/ASR.asdl

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,22 @@ expr
322322
| OverloadedCompare(expr left, cmpop op, expr right, ttype type, expr? value, expr overloaded)
323323
| OverloadedBinOp(expr left, binop op, expr right, ttype type, expr? value, expr overloaded)
324324
| OverloadedUnaryMinus(expr arg, ttype type, expr? value, expr overloaded)
325+
-- This Cast changes the value (the bits) of the `arg`:
325326
| Cast(expr arg, cast_kind kind, ttype type, expr? value)
327+
-- This ArrayPhysicalCast we only change the physical type, the logical type does not change
328+
-- Note: the "new" physical type here will also be part of the "type" member
329+
-- This allow to represent any combination, but we'll only support a few, at least we need:
330+
-- Maybe it's easier to add an enumeration here:
331+
-- Descriptor -> Pointer
332+
-- Pointer -> Descriptor
333+
334+
-- CompileTimeFixedSizeArray -> Pointer
335+
-- CompileTimeFixedSizeArray -> Descriptor
336+
-- Descriptor -> NumPy
337+
-- NumPy -> Descriptor
338+
-- ISODescriptor -> Descriptor
339+
-- Descriptor -> ISODescriptor
340+
| ArrayPhysicalCast(expr arg, array_physical_type old, array_physical_type new, ttype type, expr? value)
326341
| ComplexRe(expr arg, ttype type, expr? value)
327342
| ComplexIm(expr arg, ttype type, expr? value)
328343
| DictItem(expr a, expr key, expr? default, ttype type, expr? value)
@@ -387,13 +402,19 @@ ttype
387402
| CPtr()
388403
| SymbolicExpression()
389404
| TypeParameter(identifier param)
390-
| Array(ttype type, dimension* dims)
405+
| Array(ttype type, dimension* dims, array_physical_type physical_type)
391406
| FunctionType(ttype* arg_types, ttype? return_var_type,
392407
abi abi, deftype deftype, string? bindc_name, bool elemental,
393408
bool pure, bool module, bool inline, bool static, ttype* type_params,
394409
symbol* restrictions, bool is_restriction)
395410

396-
restriction_arg = RestrictionArg(identifier restriction_name, symbol restriction_func)
411+
-- TODO: prefix the enumerators here, improve the names
412+
array_physical_type
413+
= DescriptorArray
414+
| PointerToDataArray
415+
| FixedSizeArray
416+
| NumPyArray
417+
| ISODescriptorArray
397418

398419
binop = Add | Sub | Mul | Div | Pow | BitAnd | BitOr | BitXor | BitLShift | BitRShift
399420

src/libasr/asr_utils.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,8 @@ ASR::asr_t* getStructInstanceMember_t(Allocator& al, const Location& loc,
341341
} else {
342342
LCOMPILERS_ASSERT(ASR::is_a<ASR::Variable_t>(*member));
343343
ASR::Variable_t* member_variable = ASR::down_cast<ASR::Variable_t>(member);
344-
ASR::ttype_t* member_type = ASRUtils::type_get_past_allocatable(member_variable->m_type);
345-
bool is_pointer = false;
346-
if (ASRUtils::is_pointer(member_type)) {
347-
is_pointer = true;
348-
member_type = ASR::down_cast<ASR::Pointer_t>(member_type)->m_type;
349-
}
344+
ASR::ttype_t* member_type = ASRUtils::type_get_past_pointer(
345+
ASRUtils::type_get_past_allocatable(member_variable->m_type));
350346
ASR::ttype_t* member_type_ = ASRUtils::type_get_past_array(member_type);
351347
ASR::dimension_t* m_dims = nullptr;
352348
size_t n_dims = ASRUtils::extract_dimensions_from_ttype(member_type, m_dims);
@@ -406,19 +402,20 @@ ASR::asr_t* getStructInstanceMember_t(Allocator& al, const Location& loc,
406402
} else if(ASR::is_a<ASR::ExternalSymbol_t>(*der_type_sym)) {
407403
member_type_ = ASRUtils::TYPE(ASR::make_Struct_t(al, loc, der_type_sym));
408404
}
409-
member_type = member_type_;
410-
if( n_dims > 0 ) {
411-
member_type = ASRUtils::make_Array_t_util(al, loc,
412-
member_type_, m_dims, n_dims);
413-
}
405+
member_type = ASRUtils::make_Array_t_util(al, loc,
406+
member_type_, m_dims, n_dims);
414407
break;
415408
}
416409
default :
417410
break;
418411
}
419-
if (is_pointer) {
420-
member_type = ASRUtils::TYPE(ASR::make_Pointer_t(al, loc,
421-
ASRUtils::type_get_past_allocatable(member_type)));
412+
413+
if( ASR::is_a<ASR::Allocatable_t>(*member_variable->m_type) ) {
414+
member_type = ASRUtils::TYPE(ASR::make_Allocatable_t(al,
415+
member_variable->base.base.loc, member_type));
416+
} else if( ASR::is_a<ASR::Pointer_t>(*member_variable->m_type) ) {
417+
member_type = ASRUtils::TYPE(ASR::make_Pointer_t(al,
418+
member_variable->base.base.loc, member_type));
422419
}
423420
ASR::symbol_t* member_ext = ASRUtils::import_struct_instance_member(al, member, current_scope, member_type);
424421
ASR::expr_t* value = nullptr;
@@ -537,7 +534,7 @@ bool use_overloaded(ASR::expr_t* left, ASR::expr_t* right,
537534
current_function_dependencies.push_back(al, s2c(al, matched_func_name));
538535
ASRUtils::insert_module_dependency(a_name, al, current_module_dependencies);
539536
ASRUtils::set_absent_optional_arguments_to_null(a_args, func, al);
540-
asr = ASR::make_FunctionCall_t(al, loc, a_name, sym,
537+
asr = ASRUtils::make_FunctionCall_t_util(al, loc, a_name, sym,
541538
a_args.p, 2,
542539
return_type,
543540
nullptr, nullptr);
@@ -612,14 +609,15 @@ void process_overloaded_unary_minus_function(ASR::symbol_t* proc, ASR::expr_t* o
612609
return_type = ASRUtils::TYPE(ASR::make_Struct_t(al, loc,
613610
curr_scope->resolve_symbol(ASRUtils::symbol_name(struct_t->m_derived_type))));
614611
if( is_array ) {
615-
return_type = ASRUtils::make_Array_t_util(al, loc, return_type, m_dims, n_dims);
612+
return_type = ASRUtils::make_Array_t_util(
613+
al, loc, return_type, m_dims, n_dims);
616614
}
617615
}
618616
}
619617
current_function_dependencies.push_back(al, s2c(al, matched_func_name));
620618
ASRUtils::insert_module_dependency(a_name, al, current_module_dependencies);
621619
ASRUtils::set_absent_optional_arguments_to_null(a_args, func, al);
622-
asr = ASR::make_FunctionCall_t(al, loc, a_name, proc,
620+
asr = ASRUtils::make_FunctionCall_t_util(al, loc, a_name, proc,
623621
a_args.p, 1,
624622
return_type,
625623
nullptr, nullptr);
@@ -790,7 +788,7 @@ void process_overloaded_assignment_function(ASR::symbol_t* proc, ASR::expr_t* ta
790788
current_function_dependencies.push_back(al, s2c(al, matched_subrout_name));
791789
ASRUtils::insert_module_dependency(a_name, al, current_module_dependencies);
792790
ASRUtils::set_absent_optional_arguments_to_null(a_args, subrout, al);
793-
asr = ASR::make_SubroutineCall_t(al, loc, a_name, sym,
791+
asr = ASRUtils::make_SubroutineCall_t_util(al, loc, a_name, sym,
794792
a_args.p, 2, nullptr);
795793
}
796794
}
@@ -930,7 +928,7 @@ bool use_overloaded(ASR::expr_t* left, ASR::expr_t* right,
930928
current_function_dependencies.push_back(al, s2c(al, matched_func_name));
931929
ASRUtils::insert_module_dependency(a_name, al, current_module_dependencies);
932930
ASRUtils::set_absent_optional_arguments_to_null(a_args, func, al);
933-
asr = ASR::make_FunctionCall_t(al, loc, a_name, sym,
931+
asr = ASRUtils::make_FunctionCall_t_util(al, loc, a_name, sym,
934932
a_args.p, 2,
935933
return_type,
936934
nullptr, nullptr);
@@ -1129,14 +1127,14 @@ ASR::asr_t* symbol_resolve_external_generic_procedure_without_eval(
11291127
if( func ) {
11301128
ASRUtils::set_absent_optional_arguments_to_null(args, func, al);
11311129
}
1132-
return ASR::make_SubroutineCall_t(al, loc, final_sym,
1130+
return ASRUtils::make_SubroutineCall_t_util(al, loc, final_sym,
11331131
v, args.p, args.size(),
11341132
nullptr);
11351133
} else {
11361134
if( func ) {
11371135
ASRUtils::set_absent_optional_arguments_to_null(args, func, al);
11381136
}
1139-
return ASR::make_FunctionCall_t(al, loc, final_sym,
1137+
return ASRUtils::make_FunctionCall_t_util(al, loc, final_sym,
11401138
v, args.p, args.size(),
11411139
return_type,
11421140
nullptr, nullptr);

0 commit comments

Comments
 (0)