Skip to content

Commit d3852a4

Browse files
authored
Merge pull request #2090 from Shaikh-Ubaid/cast_unsigned
Support cast UnsignedIntegerToUnsignedInteger
2 parents 44892ff + 19e2aef commit d3852a4

File tree

6 files changed

+27
-3
lines changed

6 files changed

+27
-3
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ RUN(NAME vec_01 LABELS cpython llvm c NOFAST)
632632
RUN(NAME test_str_comparison LABELS cpython llvm c)
633633
RUN(NAME test_bit_length LABELS cpython llvm c)
634634
RUN(NAME str_to_list_cast LABELS cpython llvm c)
635+
RUN(NAME cast_01 LABELS cpython llvm c)
635636
RUN(NAME test_sys_01 LABELS cpython llvm c NOFAST)
636637
RUN(NAME intent_01 LABELS cpython llvm)
637638

integration_tests/cast_01.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from lpython import i32, u8, u32, dataclass
2+
from numpy import empty, uint8
3+
4+
@dataclass
5+
class LPBHV_small:
6+
dim : i32 = 4
7+
a : u8[4] = empty(4, dtype=uint8)
8+
9+
def main0():
10+
lphv_small : LPBHV_small = LPBHV_small()
11+
i: i32
12+
for i in range(4):
13+
lphv_small.a[i] = u8(10 + i)
14+
elt: u32 = u32(lphv_small.a[i])
15+
print(elt)
16+
assert elt == u32(10 + i)
17+
18+
main0()

src/libasr/ASR.asdl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ cast_kind
429429
| IntegerToCharacter
430430
| LogicalToCharacter
431431
| UnsignedIntegerToInteger
432+
| UnsignedIntegerToUnsignedInteger
432433
| UnsignedIntegerToReal
433434
| UnsignedIntegerToLogical
434435
| IntegerToUnsignedInteger

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1754,8 +1754,10 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
17541754
// src = src;
17551755
break;
17561756
}
1757-
case (ASR::cast_kindType::IntegerToInteger) : {
1757+
case (ASR::cast_kindType::IntegerToInteger) :
1758+
case (ASR::cast_kindType::UnsignedIntegerToUnsignedInteger) : {
17581759
// In C++, we do not need to cast int <-> long long explicitly:
1760+
// we also do not need to cast uint8_t <-> uint32_t explicitly:
17591761
// src = src;
17601762
break;
17611763
}

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6876,7 +6876,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
68766876
}
68776877
break;
68786878
}
6879-
case (ASR::cast_kindType::IntegerToInteger) : {
6879+
case (ASR::cast_kindType::IntegerToInteger) :
6880+
case (ASR::cast_kindType::UnsignedIntegerToUnsignedInteger) : {
68806881
int arg_kind = -1, dest_kind = -1;
68816882
extract_kinds(x, arg_kind, dest_kind);
68826883
if( arg_kind > 0 && dest_kind > 0 &&

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ namespace CastingUtil {
7272
const std::map<ASR::ttypeType, ASR::cast_kindType>& kind_rules = {
7373
{ASR::ttypeType::Complex, ASR::cast_kindType::ComplexToComplex},
7474
{ASR::ttypeType::Real, ASR::cast_kindType::RealToReal},
75-
{ASR::ttypeType::Integer, ASR::cast_kindType::IntegerToInteger}
75+
{ASR::ttypeType::Integer, ASR::cast_kindType::IntegerToInteger},
76+
{ASR::ttypeType::UnsignedInteger, ASR::cast_kindType::UnsignedIntegerToUnsignedInteger}
7677
};
7778

7879
int get_type_priority(ASR::ttypeType type) {

0 commit comments

Comments
 (0)