diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index bb03534..465fe3f 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -208,6 +208,10 @@ RUN(NAME array_22.cpp LABELS gcc llvm) RUN(NAME array_23.cpp LABELS gcc llvm) RUN(NAME array_24.cpp LABELS gcc llvm) RUN(NAME array_25.cpp LABELS gcc llvm) +RUN(NAME array_26.cpp LABELS gcc llvm) +RUN(NAME array_27.cpp LABELS gcc) +RUN(NAME array_28.cpp LABELS gcc) + RUN(NAME struct_01.cpp LABELS gcc llvm) RUN(NAME struct_02.cpp LABELS gcc llvm) diff --git a/integration_tests/array_26.cpp b/integration_tests/array_26.cpp new file mode 100644 index 0000000..1589c82 --- /dev/null +++ b/integration_tests/array_26.cpp @@ -0,0 +1,18 @@ +#include +#include +#include +#include +#include "xtensor/xio.hpp" + +// LFortran: ./integration_tests/arrays_31.f90 + +int main() { + xt::xtensor x = xt::empty({5}); + + int pqr = x.size(); + + std::cout << pqr << std::endl; + if (pqr != 5) { + exit(2); + } +} diff --git a/integration_tests/array_27.cpp b/integration_tests/array_27.cpp new file mode 100644 index 0000000..ea77627 --- /dev/null +++ b/integration_tests/array_27.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include "xtensor/xio.hpp" + +// LFortran: ./integration_tests/arrays_34.f90 + + +void sub(xt::xtensor& x, int z) { + switch (z) { + case 1: + x.resize({1}); // Resize x to have size 1 + x(0) = 1; + break; + default: + std::cout << "z = " << z << std::endl; + std::cout << "z not supported." << std::endl; + } +} + +int main() { + xt::xtensor x; + + sub(x, 1); + std::cout << x << std::endl; + if (x(0) != 1) { + exit(2); + } + + return 0; +} diff --git a/integration_tests/array_28.cpp b/integration_tests/array_28.cpp new file mode 100644 index 0000000..2c19ffb --- /dev/null +++ b/integration_tests/array_28.cpp @@ -0,0 +1,16 @@ +#include +#include +#include +#include +#include "xtensor/xio.hpp" + +// LFortran: ./integration_tests/arrays_35.f90 + +int main() { + xt::xtensor x = xt::empty({2}); + x = { 9.0, 2.1 }; + + std::cout << x << std::endl; + if (std::abs(x(0) - 9.0) > 1e-7) exit(2); + if (std::abs(x(1) - 2.1) > 1e-7) exit(2); +} diff --git a/src/libasr/asr_utils.h b/src/libasr/asr_utils.h index 742c165..919f180 100644 --- a/src/libasr/asr_utils.h +++ b/src/libasr/asr_utils.h @@ -2482,6 +2482,11 @@ static inline ASR::ttype_t* duplicate_type_without_dims(Allocator& al, const ASR ASR::TypeParameter_t* tp = ASR::down_cast(t); return ASRUtils::TYPE(ASR::make_TypeParameter_t(al, loc, tp->m_param)); } + case ASR::ttypeType::Const: { + ASR::Const_t* c = ASR::down_cast(t); + return ASRUtils::TYPE(ASR::make_Const_t(al, loc, + duplicate_type_without_dims(al, c->m_type, loc))); + } default : throw LCompilersException("Not implemented " + std::to_string(t->type)); } } diff --git a/src/libasr/casting_utils.cpp b/src/libasr/casting_utils.cpp index ee5d468..cce6731 100644 --- a/src/libasr/casting_utils.cpp +++ b/src/libasr/casting_utils.cpp @@ -146,6 +146,14 @@ namespace LCompilers::CastingUtil { } else { dest = ASRUtils::extract_type(dest); } + if (ASR::is_a(*expr)) { + ASR::ArrayConstant_t* arr = ASR::down_cast(expr); + for (size_t i = 0; i < arr->n_args; i++) { + arr->m_args[i] = ASRUtils::expr_value(ASRUtils::EXPR(ASRUtils::make_Cast_t_value(al, loc, arr->m_args[i], + cast_kind, ASRUtils::expr_type(arr->m_args[i])))); + } + return ASRUtils::EXPR((ASR::asr_t*) arr); + } return ASRUtils::EXPR(ASRUtils::make_Cast_t_value(al, loc, expr, cast_kind, dest)); } } diff --git a/src/libasr/codegen/llvm_utils.cpp b/src/libasr/codegen/llvm_utils.cpp index 88f77ec..6c55c33 100644 --- a/src/libasr/codegen/llvm_utils.cpp +++ b/src/libasr/codegen/llvm_utils.cpp @@ -507,6 +507,11 @@ namespace LCompilers { el_type = character_type; break; } + case ASR::ttypeType::Const: { + ASR::ttype_t* t2 = ASR::down_cast(m_type_)->m_type; + el_type = get_el_type(t2, module); + break; + } default: LCOMPILERS_ASSERT(false); break; diff --git a/tests/reference/asr-array_28-e2b02d8.json b/tests/reference/asr-array_28-e2b02d8.json new file mode 100644 index 0000000..6b429b9 --- /dev/null +++ b/tests/reference/asr-array_28-e2b02d8.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-array_28-e2b02d8", + "cmd": "lc --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/../integration_tests/array_28.cpp", + "infile_hash": "d2a5fada0de4142e17c05431214c0b594476f069036ddddbbfeaa334", + "outfile": null, + "outfile_hash": null, + "stdout": "asr-array_28-e2b02d8.stdout", + "stdout_hash": "797d6000c250772de2e9d7017adb9194c0bd9a6694b04d4ff6b472d6", + "stderr": null, + "stderr_hash": null, + "returncode": 0 +} \ No newline at end of file diff --git a/tests/reference/asr-array_28-e2b02d8.stdout b/tests/reference/asr-array_28-e2b02d8.stdout new file mode 100644 index 0000000..249b9c5 --- /dev/null +++ b/tests/reference/asr-array_28-e2b02d8.stdout @@ -0,0 +1,254 @@ +(TranslationUnit + (SymbolTable + 1 + { + main: + (Function + (SymbolTable + 2 + { + __return_var: + (Variable + 2 + __return_var + [] + ReturnVar + () + () + Default + (Integer 4) + () + Source + Public + Required + .false. + ), + x: + (Variable + 2 + x + [] + Local + () + () + Default + (Allocatable + (Array + (Real 8) + [(() + ())] + DescriptorArray + ) + ) + () + Source + Public + Required + .false. + ) + }) + main + (FunctionType + [] + (Integer 4) + Source + Implementation + () + .false. + .false. + .false. + .false. + .false. + [] + .false. + ) + [] + [] + [(Allocate + [((Var 2 x) + [((IntegerConstant 0 (Integer 4)) + (IntegerConstant 2 (Integer 4)))] + () + ())] + () + () + () + ) + (Allocate + [((Var 2 x) + [((IntegerConstant 0 (Integer 4)) + (IntegerConstant 2 (Integer 4)))] + () + ())] + () + () + () + ) + (= + (Var 2 x) + (ArrayConstant + [(RealConstant + 9.000000 + (Const + (Real 8) + ) + ) + (RealConstant + 2.100000 + (Const + (Real 8) + ) + )] + (Array + (Const + (Real 8) + ) + [((IntegerConstant 0 (Integer 4)) + (IntegerConstant 2 (Integer 4)))] + FixedSizeArray + ) + RowMajor + ) + () + ) + (Print + [(Var 2 x)] + (StringConstant + "" + (Character 1 1 ()) + ) + (StringConstant + "" + (Character 1 1 ()) + ) + ) + (If + (RealCompare + (IntrinsicScalarFunction + Abs + [(RealBinOp + (Cast + (ArrayItem + (Var 2 x) + [(() + (IntegerConstant 0 (Integer 4)) + ())] + (Real 8) + RowMajor + () + ) + RealToReal + (Real 8) + () + ) + Sub + (RealConstant + 9.000000 + (Real 8) + ) + (Real 8) + () + )] + 0 + (Real 8) + () + ) + Gt + (RealConstant + 0.000000 + (Real 8) + ) + (Logical 4) + () + ) + [] + [] + ) + (If + (RealCompare + (IntrinsicScalarFunction + Abs + [(RealBinOp + (Cast + (ArrayItem + (Var 2 x) + [(() + (IntegerConstant 1 (Integer 4)) + ())] + (Real 8) + RowMajor + () + ) + RealToReal + (Real 8) + () + ) + Sub + (RealConstant + 2.100000 + (Real 8) + ) + (Real 8) + () + )] + 0 + (Real 8) + () + ) + Gt + (RealConstant + 0.000000 + (Real 8) + ) + (Logical 4) + () + ) + [] + [] + )] + (Var 2 __return_var) + Public + .false. + .false. + () + ), + main_program: + (Program + (SymbolTable + 3 + { + exit_code: + (Variable + 3 + exit_code + [] + Local + () + () + Default + (Integer 4) + () + Source + Public + Required + .false. + ) + }) + main_program + [main] + [(= + (Var 3 exit_code) + (FunctionCall + 1 main + 1 main + [] + (Integer 4) + () + () + ) + () + )] + ) + }) + [] +) diff --git a/tests/tests.toml b/tests/tests.toml index c72ce0d..4bd3836 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -33,3 +33,7 @@ asr = true [[test]] filename = "../integration_tests/array_04.cpp" asr = true + +[[test]] +filename = "../integration_tests/array_28.cpp" +asr = true