Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for item access from Const data-structures #2579

Merged
merged 33 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
5d40049
Add support for accessing items from data-structures within `Const`
kmr-srbh Mar 7, 2024
2fb304a
Update type checking logic for `Const dict`
kmr-srbh Mar 7, 2024
416d1c3
Minor formatting change
kmr-srbh Mar 7, 2024
7077516
Merge branch 'main' into support-const-access
kmr-srbh Mar 8, 2024
e622a9d
Handle negative indices for `const list` and minor formatting changes
kmr-srbh Mar 10, 2024
1a3a5d5
Add tests
kmr-srbh Mar 10, 2024
1f74255
Update test references
kmr-srbh Mar 10, 2024
20aa660
Heavily simplify handling `const`
kmr-srbh Mar 10, 2024
510b65b
Merge branch 'main' into support-const-access
kmr-srbh Mar 11, 2024
32b691e
Tests: Add compile time test
kmr-srbh Mar 12, 2024
0553c04
Merge branch 'main' into support-const-access
kmr-srbh Mar 18, 2024
c8a5020
Merge branch 'main' into support-const-access
kmr-srbh Mar 19, 2024
b62f675
Merge branch 'main' into support-const-access
kmr-srbh Apr 21, 2024
5ebc24f
Merge branch 'main' into support-const-access
kmr-srbh May 1, 2024
cb8a0f2
Remove calls to `type_get_past_const()`
kmr-srbh May 1, 2024
9803138
Tests: Update test references
kmr-srbh May 1, 2024
d8d82ea
Remove extra newline
kmr-srbh May 1, 2024
55ba905
Delete tests/reference/asr-test_const_access-82a9a24.json
kmr-srbh May 1, 2024
89ad7e3
Delete tests/reference/asr-test_const_access-82a9a24.stdout
kmr-srbh May 1, 2024
36a1716
Delete tests/reference/asr-test_const_str_access-59ff543.stderr
kmr-srbh May 1, 2024
b3bbe78
Delete tests/reference/asr-test_const_tuple_access-0d4c6df.json
kmr-srbh May 1, 2024
3da9626
Delete tests/reference/asr-test_const_tuple_access-0d4c6df.stderr
kmr-srbh May 1, 2024
733541e
Delete tests/reference/asr-test_const_str_access-59ff543.json
kmr-srbh May 1, 2024
41514a2
Formatting changes
kmr-srbh May 1, 2024
ed8662d
Tests: Add test to CMakeLists and update error references
kmr-srbh May 1, 2024
e69f52a
Merge branch 'main' into support-const-access
kmr-srbh May 1, 2024
334f429
Update asr_to_llvm.cpp
kmr-srbh May 1, 2024
251b5d4
Undo formatting changes
kmr-srbh May 1, 2024
be48acf
Undo formatting changes
kmr-srbh May 1, 2024
2620956
Revert throwing error for `Const` annotated tuples and strings
kmr-srbh May 1, 2024
c0407b5
Tests: Remove error references
kmr-srbh May 1, 2024
f489720
Remove redundant visitor
kmr-srbh May 1, 2024
970ce8c
Undo moving `index`
kmr-srbh May 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions integration_tests/test_const_access.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from lpython import i32, str, list, dict, Const


def test_const_access():
CONST_LIST: Const[list[i32]] = [1, 2, 3, 4, 5]
CONST_DICTIONARY: Const[dict[str, i32]] = {"a": 1, "b": 2, "c": 3}

assert CONST_LIST[0] == 1
assert CONST_LIST[-2] == 4

assert CONST_DICTIONARY["a"] == 1


test_const_access()
9 changes: 5 additions & 4 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1523,8 +1523,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
}

void visit_ListItem(const ASR::ListItem_t& x) {
ASR::ttype_t* el_type = ASRUtils::get_contained_type(
kmr-srbh marked this conversation as resolved.
Show resolved Hide resolved
ASRUtils::expr_type(x.m_a));
ASR::ttype_t *el_type = ASR::is_a<ASR::Const_t>(*ASRUtils::expr_type(x.m_a))
? ASRUtils::get_contained_type(ASRUtils::type_get_past_const(ASRUtils::expr_type(x.m_a)))
: ASRUtils::get_contained_type(ASRUtils::expr_type(x.m_a));
int64_t ptr_loads_copy = ptr_loads;
ptr_loads = 0;
this->visit_expr(*x.m_a);
Expand All @@ -1540,8 +1541,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
}

void visit_DictItem(const ASR::DictItem_t& x) {
ASR::Dict_t* dict_type = ASR::down_cast<ASR::Dict_t>(
ASRUtils::expr_type(x.m_a));
ASR::Dict_t *dict_type = ASR::down_cast<ASR::Dict_t>(ASRUtils::type_get_past_const(ASRUtils::expr_type(x.m_a)));

int64_t ptr_loads_copy = ptr_loads;
ptr_loads = 0;
this->visit_expr(*x.m_a);
Expand Down
51 changes: 50 additions & 1 deletion src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1750,6 +1750,12 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
} else if (var_annotation == "Const") {
ASR::ttype_t *type = ast_expr_to_asr_type(loc, *s->m_slice,
is_allocatable, raise_error, abi, is_argument);
if (ASR::is_a<ASR::Tuple_t>(*type)) {
throw SemanticError("'Const' not required as tuples are already immutable", loc);
}
else if (ASR::is_a<ASR::Character_t>(*type)) {
throw SemanticError("'Const' not required as strings are already immutable", loc);
}
return ASRUtils::TYPE(ASR::make_Const_t(al, loc, type));
} else {
AST::expr_t* dim_info = s->m_slice;
Expand Down Expand Up @@ -3730,6 +3736,12 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
} else if (ASR::is_a<ASR::Dict_t>(*type)) {
throw SemanticError("unhashable type in dict: 'slice'", loc);
}
if (ASR::is_a<ASR::Const_t>(*type))
{
if (ASR::is_a<ASR::List_t>(*ASRUtils::type_get_past_const(type))) {
throw SemanticError("slicing on a const list is not implemented till now", loc);
}
}
} else if(AST::is_a<AST::Tuple_t>(*m_slice) &&
ASRUtils::is_array(type)) {
bool final_result = true;
Expand All @@ -3740,7 +3752,44 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
}
return final_result;
} else {
ASR::expr_t *index = nullptr;
this->visit_expr(*m_slice);
if (ASR::is_a<ASR::Const_t>(*type)) {
ASR::ttype_t *contained_type = ASRUtils::type_get_past_const(type);
if (ASR::is_a<ASR::Dict_t>(*contained_type)) {
index = ASRUtils::EXPR(tmp);
ASR::ttype_t *key_type = ASR::down_cast<ASR::Dict_t>(contained_type)->m_key_type;
if (!ASRUtils::check_equal_type(ASRUtils::expr_type(index), key_type)) {
throw SemanticError("Key type should be '" + ASRUtils::type_to_str_python(key_type) +
"' instead of '" +
ASRUtils::type_to_str_python(ASRUtils::expr_type(index)) + "'",
index->base.loc);
}
tmp = ASR::make_DictItem_t(al, loc, value, index, nullptr,
ASR::down_cast<ASR::Dict_t>(contained_type)->m_value_type, nullptr);
return false;
}
else if (ASR::is_a<ASR::List_t>(*contained_type)) {
this->visit_expr(*m_slice);
index = ASRUtils::EXPR(tmp);
ASR::expr_t* val = ASRUtils::expr_value(index);
if (val && ASR::is_a<ASR::IntegerConstant_t>(*val)) {
if (ASR::down_cast<ASR::IntegerConstant_t>(val)->m_n < 0) {
// Replace `x[-1]` to `x[len(x)+(-1)]`
ASR::ttype_t *int_type = ASRUtils::TYPE(ASR::make_Integer_t(
al, loc, 4));
ASR::expr_t *list_len = ASRUtils::EXPR(ASR::make_ListLen_t(
al, loc, value, int_type, nullptr));
ASR::expr_t *neg_idx = ASRUtils::expr_value(index);
index = ASRUtils::EXPR(ASR::make_IntegerBinOp_t(al, loc,
list_len, ASR::binopType::Add, neg_idx, int_type, nullptr));
}
}
tmp = make_ListItem_t(al, loc, value, index,
ASR::down_cast<ASR::List_t>(contained_type)->m_type, nullptr);
return false;
}
}
kmr-srbh marked this conversation as resolved.
Show resolved Hide resolved
if (!ASR::is_a<ASR::Dict_t>(*type) &&
!ASRUtils::is_integer(*ASRUtils::expr_type(ASRUtils::EXPR(tmp)))) {
std::string fnd = ASRUtils::type_to_str_python(ASRUtils::expr_type(ASRUtils::EXPR(tmp)));
Expand All @@ -3753,8 +3802,8 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
);
throw SemanticAbort();
}
ASR::expr_t *index = nullptr;
if (ASR::is_a<ASR::Dict_t>(*type)) {
kmr-srbh marked this conversation as resolved.
Show resolved Hide resolved
this->visit_expr(*m_slice);
kmr-srbh marked this conversation as resolved.
Show resolved Hide resolved
index = ASRUtils::EXPR(tmp);
ASR::ttype_t *key_type = ASR::down_cast<ASR::Dict_t>(type)->m_key_type;
if (!ASRUtils::check_equal_type(ASRUtils::expr_type(index), key_type)) {
Expand Down
8 changes: 8 additions & 0 deletions tests/errors/test_const_str_access.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from lpython import str, Const


def test_const_str_access():
CONST_STRING: Const[str] = "Hello, LPython!"


test_const_str_access()
9 changes: 9 additions & 0 deletions tests/errors/test_const_tuple_access.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from lpython import str, tuple, Const


def test_const_tuple_access():
CONST_TUPLE: Const[tuple[str, str, str]] = ("hello", "LPython", "!")


test_const_tuple_access()

13 changes: 13 additions & 0 deletions tests/reference/asr-test_const_access-82a9a24.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-test_const_access-82a9a24",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/test_const_access.py",
"infile_hash": "c25c7c1c19db38e066b47dc2891abaa5ddd7379b4df07892cebb94e3",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-test_const_access-82a9a24.stdout",
"stdout_hash": "a98657aca0fefd42749ce13d47c09daf48deed01ec24170d242f5ee3",
"stderr": null,
"stderr_hash": null,
"returncode": 0
}
Loading
Loading