Skip to content

Commit

Permalink
casts: Implicit cast for IntegerToReal [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoZeke committed Aug 13, 2021
1 parent 7d95272 commit 69b2cf5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ RUN(NAME parameter_01 LABELS gfortran)
RUN(NAME parameter_02 LABELS gfortran)
RUN(NAME parameter_03 LABELS gfortran llvm)
RUN(NAME parameter_04 LABELS gfortran llvm) # selected_real,int_kind symboltable
RUN(NAME parameter_05 LABELS gfortran llvm) # Implicit IntegerToReal

RUN(NAME modules_01 LABELS gfortran llvm)
RUN(NAME modules_02 LABELS gfortran llvm)
Expand Down
9 changes: 9 additions & 0 deletions integration_tests/parameter_05.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
program paramter_05
implicit none

integer, parameter :: x = 5
real, parameter :: y = 5
real(kind=8), parameter :: z = 5
print*, x,y,z

end program paramter_05
13 changes: 13 additions & 0 deletions src/lfortran/semantics/asr_implicit_cast_rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ class ImplicitCastRules {
value = (ASR::expr_t *)ASR::make_ConstantInteger_t(al, a_loc,
i, dest_type);
}
} else if ((ASR::cast_kindType)cast_kind == ASR::cast_kindType::IntegerToReal) {
if (ASRUtils::expr_value(*convert_can)) {
LFORTRAN_ASSERT(ASR::is_a<ASR::Real_t>(*dest_type)
|| ASR::is_a<ASR::RealPointer_t>(*dest_type))
LFORTRAN_ASSERT(ASR::is_a<ASR::Integer_t>(*ASRUtils::expr_type(*convert_can)))
value = ASRUtils::expr_value(*convert_can);
LFORTRAN_ASSERT(ASR::is_a<ASR::ConstantInteger_t>(*value))
ASR::ConstantInteger_t *i = ASR::down_cast<ASR::ConstantInteger_t>(value);
double rval = static_cast<double>(i->m_n);
value = (ASR::expr_t *)ASR::make_ConstantReal_t(al, a_loc,
rval, dest_type);
}

} else if ((ASR::cast_kindType)cast_kind == ASR::cast_kindType::RealToReal) {
if (ASRUtils::expr_value(*convert_can)) {
LFORTRAN_ASSERT(ASR::is_a<ASR::Real_t>(*dest_type)
Expand Down
5 changes: 5 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,11 @@ asr = true
filename = "../integration_tests/parameter_04.f90"
asr = true

# IntegerToReal
[[test]]
filename = "../integration_tests/parameter_05.f90"
asr = true

[[test]]
filename = "do_concurrent1.f90"
ast = true
Expand Down

0 comments on commit 69b2cf5

Please sign in to comment.