Skip to content

Commit 0ba09ab

Browse files
authored
Add support for global set declaration (#2605)
* Add support for global `set` declaration * Tests: Add test and update test references * Revert adding tests * Tests: Add tests and update test references * Update set1.py * Tests: Update test references * Add an assert statement * Tests: Add more asserts for `set` length
1 parent 6325e8d commit 0ba09ab

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ RUN(NAME test_dict_nested1 LABELS cpython llvm)
578578
RUN(NAME test_set_len LABELS cpython llvm)
579579
RUN(NAME test_set_add LABELS cpython llvm)
580580
RUN(NAME test_set_remove LABELS cpython llvm)
581+
RUN(NAME test_global_set LABELS cpython llvm)
581582
RUN(NAME test_for_loop LABELS cpython llvm c)
582583
RUN(NAME modules_01 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
583584
RUN(NAME modules_02 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from lpython import i32
2+
3+
s1: set[str] = {"a", "b", "c", "a"}
4+
s2: set[i32] = {1, 2, 3, 1}
5+
s3: set[tuple[i32, i32]] = {(1, 2), (2, 3), (4, 5)}
6+
7+
assert len(s1) == 3
8+
assert len(s2) == 3
9+
assert len(s3) == 3

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2803,6 +2803,14 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
28032803
llvm::ConstantStruct::get(dict_type,
28042804
llvm::Constant::getNullValue(dict_type)));
28052805
llvm_symtab[h] = ptr;
2806+
} else if(x.m_type->type == ASR::ttypeType::Set) {
2807+
llvm::StructType* set_type = static_cast<llvm::StructType*>(
2808+
llvm_utils->get_type_from_ttype_t_util(x.m_type, module.get()));
2809+
llvm::Constant *ptr = module->getOrInsertGlobal(x.m_name, set_type);
2810+
module->getNamedGlobal(x.m_name)->setInitializer(
2811+
llvm::ConstantStruct::get(set_type,
2812+
llvm::Constant::getNullValue(set_type)));
2813+
llvm_symtab[h] = ptr;
28062814
} else if (x.m_type->type == ASR::ttypeType::TypeParameter) {
28072815
// Ignore type variables
28082816
} else {

0 commit comments

Comments
 (0)