Skip to content

Commit f6cfb29

Browse files
committed
merge
1 parent 2739a30 commit f6cfb29

File tree

4 files changed

+124
-49
lines changed

4 files changed

+124
-49
lines changed

libasr

Submodule libasr updated 941 files

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 80 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -540,15 +540,15 @@ class CommonVisitor : public AST::BaseVisitor<StructType> {
540540
}
541541
case ASR::ttypeType::String: {
542542
ASR::String_t *t = ASR::down_cast<ASR::String_t>(return_type);
543-
func_calls.push_back(t->m_len_expr);
543+
func_calls.push_back(t->m_len);
544544
fix_exprs_ttype_t(func_calls, args, f);
545-
int64_t a_len = t->m_len;
546-
if( func_calls[0] ) {
547-
diag::Diagnostics diags;
548-
a_len = ASRUtils::extract_len<SemanticAbort>(func_calls[0], loc, diags);
549-
}
550-
return ASRUtils::TYPE(ASR::make_String_t(al, loc, t->m_kind, a_len,
551-
func_calls[0], ASR::string_physical_typeType::PointerString));
545+
ASR::expr_t* a_len = func_calls[0] ? func_calls[0] : t->m_len;
546+
/*if( func_calls[0] ) {*/
547+
/* diag::Diagnostics diags;*/
548+
/* a_len = ASRUtils::extract_len<SemanticAbort>(func_calls[0], loc, diags);*/
549+
/*}*/
550+
return ASRUtils::TYPE(ASR::make_String_t(al, loc, t->m_kind,
551+
a_len, false, false, ASR::string_physical_typeType::PointerString));
552552
}
553553
case ASR::ttypeType::StructType: {
554554
ASR::StructType_t* struct_t_type = ASR::down_cast<ASR::StructType_t>(return_type);
@@ -889,7 +889,10 @@ class CommonVisitor : public AST::BaseVisitor<StructType> {
889889
type = ASRUtils::TYPE(ASR::make_Complex_t(al, loc, 8));
890890
type = ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument);
891891
} else if (var_annotation == "str") {
892-
type = ASRUtils::TYPE(ASR::make_String_t(al, loc, 1, -2, nullptr, ASR::string_physical_typeType::PointerString));
892+
type = ASRUtils::TYPE(ASR::make_String_t(al, loc, 1,
893+
ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, loc, -2,
894+
ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4)))),
895+
false, false, ASR::string_physical_typeType::PointerString));
893896
type = ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument);
894897
} else if (var_annotation == "bool" || var_annotation == "i1") {
895898
type = ASRUtils::TYPE(ASR::make_Logical_t(al, loc, 4));
@@ -1949,7 +1952,7 @@ class CommonVisitor : public AST::BaseVisitor<StructType> {
19491952
AST::ConstantStr_t *n = AST::down_cast<AST::ConstantStr_t>(&annotation);
19501953
ASR::symbol_t *sym = current_scope->resolve_symbol(n->m_value);
19511954
if ( sym == nullptr || !ASR::is_a<ASR::Struct_t>(*sym) ) {
1952-
throw SemanticError("Only StructType implemented for constant"
1955+
throw SemanticError("Only StructType implemented for con"
19531956
" str annotation", loc);
19541957
}
19551958
//TODO: Change the returned type from Class to StructType
@@ -2164,25 +2167,36 @@ class CommonVisitor : public AST::BaseVisitor<StructType> {
21642167
LCOMPILERS_ASSERT(ASRUtils::extract_n_dims_from_ttype(left_type) == 0);
21652168
right_int = ASR::down_cast<ASR::IntegerConstant_t>(
21662169
ASRUtils::expr_value(right))->m_n;
2167-
dest_len = left_type2->m_len * right_int;
2170+
int64_t strlen;
2171+
ASRUtils::extract_value(left_type2->m_len,strlen);
2172+
dest_len = strlen * right_int;
21682173
if (dest_len < 0) dest_len = 0;
2174+
ASR::expr_t* a_len = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al,
2175+
loc, dest_len, ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 8))));
21692176
dest_type = ASR::down_cast<ASR::ttype_t>(
2170-
ASR::make_String_t(al, loc, left_type2->m_kind,
2171-
dest_len, nullptr, ASR::string_physical_typeType::PointerString));
2177+
ASR::make_String_t(al, loc, left_type2->m_kind, a_len, false, false
2178+
, ASR::string_physical_typeType::PointerString));
21722179
} else if (left_is_int && ASRUtils::expr_value(left) != nullptr) {
21732180
ASR::String_t *right_type2 = ASR::down_cast<ASR::String_t>(
21742181
ASRUtils::type_get_past_array(right_type));
21752182
LCOMPILERS_ASSERT(ASRUtils::extract_n_dims_from_ttype(right_type) == 0);
21762183
left_int = ASR::down_cast<ASR::IntegerConstant_t>(
21772184
ASRUtils::expr_value(left))->m_n;
2178-
dest_len = right_type2->m_len * left_int;
2185+
int64_t strlen;
2186+
ASRUtils::extract_value(right_type2->m_len,strlen);
2187+
dest_len = strlen * left_int;
21792188
if (dest_len < 0) dest_len = 0;
2189+
ASR::expr_t* a_len = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al,
2190+
loc, dest_len, ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 8))));
21802191
dest_type = ASR::down_cast<ASR::ttype_t>(
2181-
ASR::make_String_t(al, loc, right_type2->m_kind,
2182-
dest_len, nullptr, ASR::string_physical_typeType::PointerString));
2192+
ASR::make_String_t(al, loc, right_type2->m_kind, a_len, false, false
2193+
, ASR::string_physical_typeType::PointerString));
21832194
} else {
2195+
2196+
ASR::expr_t* a_len = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al,
2197+
loc, -1, ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 8))));
21842198
dest_type = ASRUtils::TYPE(ASR::make_String_t(al,
2185-
loc, 1, -1, nullptr, ASR::string_physical_typeType::PointerString));
2199+
loc, 1, a_len, false, false, ASR::string_physical_typeType::PointerString));
21862200
}
21872201

21882202
if (ASRUtils::expr_value(left) != nullptr && ASRUtils::expr_value(right) != nullptr) {
@@ -2216,9 +2230,14 @@ class CommonVisitor : public AST::BaseVisitor<StructType> {
22162230
ASRUtils::type_get_past_array(right_type));
22172231
LCOMPILERS_ASSERT(ASRUtils::extract_n_dims_from_ttype(left_type) == 0);
22182232
LCOMPILERS_ASSERT(ASRUtils::extract_n_dims_from_ttype(right_type) == 0);
2233+
int64_t left_len, right_len;
2234+
ASRUtils::extract_value(left_type2->m_len, left_len);
2235+
ASRUtils::extract_value(right_type2->m_len, right_len);
2236+
ASR::expr_t* a_len = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, loc, left_len+right_len,
2237+
ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 8))));
22192238
dest_type = ASR::down_cast<ASR::ttype_t>(
22202239
ASR::make_String_t(al, loc, left_type2->m_kind,
2221-
left_type2->m_len + right_type2->m_len, nullptr,
2240+
a_len, false, false,
22222241
ASR::string_physical_typeType::PointerString));
22232242
if (ASRUtils::expr_value(left) != nullptr && ASRUtils::expr_value(right) != nullptr) {
22242243
char* left_value = ASR::down_cast<ASR::StringConstant_t>(
@@ -2228,7 +2247,9 @@ class CommonVisitor : public AST::BaseVisitor<StructType> {
22282247
char* result;
22292248
std::string result_s = std::string(left_value) + std::string(right_value);
22302249
result = s2c(al, result_s);
2231-
LCOMPILERS_ASSERT((int64_t)strlen(result) == ASR::down_cast<ASR::String_t>(dest_type)->m_len)
2250+
int64_t dest_len;
2251+
ASRUtils::extract_value(ASR::down_cast<ASR::String_t>(dest_type)->m_len, dest_len);
2252+
LCOMPILERS_ASSERT((int64_t)strlen(result) == dest_len)
22322253
value = ASR::down_cast<ASR::expr_t>(ASR::make_StringConstant_t(
22332254
al, loc, result, dest_type));
22342255
}
@@ -3422,9 +3443,10 @@ class CommonVisitor : public AST::BaseVisitor<StructType> {
34223443
void add_name(const Location &loc) {
34233444
std::string var_name = "__name__";
34243445
std::string var_value = module_name;
3425-
size_t s_size = var_value.size();
3446+
ASR::expr_t* a_len = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, loc, var_value.size(),
3447+
ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 8))));
34263448
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_String_t(al, loc,
3427-
1, s_size, nullptr, ASR::string_physical_typeType::PointerString));
3449+
1, a_len, false, false, ASR::string_physical_typeType::PointerString));
34283450
ASR::expr_t *value = ASRUtils::EXPR(ASR::make_StringConstant_t(al,
34293451
loc, s2c(al, var_value), type));
34303452
ASR::expr_t *init_expr = value;
@@ -3450,9 +3472,10 @@ class CommonVisitor : public AST::BaseVisitor<StructType> {
34503472
void add_lpython_version(const Location &loc) {
34513473
std::string var_name = "__LPYTHON_VERSION__";
34523474
std::string var_value = LFORTRAN_VERSION;
3453-
size_t s_size = var_value.size();
3475+
ASR::expr_t* a_len = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, loc, var_value.size(),
3476+
ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 8))));
34543477
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_String_t(al, loc,
3455-
1, s_size, nullptr, ASR::string_physical_typeType::PointerString));
3478+
1, a_len, false, false, ASR::string_physical_typeType::PointerString));
34563479
ASR::expr_t *value = ASRUtils::EXPR(ASR::make_StringConstant_t(al,
34573480
loc, s2c(al, var_value), type));
34583481
ASR::expr_t *init_expr = value;
@@ -3565,9 +3588,10 @@ class CommonVisitor : public AST::BaseVisitor<StructType> {
35653588

35663589
void visit_ConstantStr(const AST::ConstantStr_t &x) {
35673590
char *s = x.m_value;
3568-
size_t s_size = std::string(s).size();
3591+
ASR::expr_t* a_len = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, x.base.base.loc, std::string(s).size(),
3592+
ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc, 8))));
35693593
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_String_t(al, x.base.base.loc,
3570-
1, s_size, nullptr, ASR::string_physical_typeType::PointerString));
3594+
1, a_len, false, false, ASR::string_physical_typeType::PointerString));
35713595
tmp = ASR::make_StringConstant_t(al, x.base.base.loc, s, type);
35723596
}
35733597

@@ -6251,8 +6275,10 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
62516275
ASR::Enum_t* enum_type = ASR::down_cast<ASR::Enum_t>(enum_->m_enum_type);
62526276
tmp = ASR::make_EnumValue_t(al, loc, e, type, enum_type->m_type, nullptr);
62536277
} else if( std::string(attr_char) == "name" ) {
6278+
ASR::expr_t* a_len = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, loc, -2,
6279+
ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 8))));
62546280
ASR::ttype_t* char_type = ASRUtils::TYPE(ASR::make_String_t(
6255-
al, loc, 1, -2, nullptr, ASR::string_physical_typeType::PointerString));
6281+
al, loc, 1, a_len, false, false, ASR::string_physical_typeType::PointerString));
62566282
tmp = ASR::make_EnumName_t(al, loc, e, type, char_type, nullptr);
62576283
}
62586284
} else if(ASR::is_a<ASR::UnionType_t>(*type)) {
@@ -6457,8 +6483,11 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
64576483
if( attr_name == "value" ) {
64586484
tmp = ASR::make_EnumValue_t(al, loc, t_mem, type, enum_type->m_type, nullptr);
64596485
} else if( attr_name == "name" ) {
6486+
6487+
ASR::expr_t* a_len = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al,
6488+
loc, -2, ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 8))));
64606489
ASR::ttype_t* char_type = ASRUtils::TYPE(ASR::make_String_t(
6461-
al, loc, 1, -2, nullptr, ASR::string_physical_typeType::PointerString));
6490+
al, loc, 1, a_len, true, true, ASR::string_physical_typeType::PointerString));
64626491
tmp = ASR::make_EnumName_t(al, loc, t_mem, type, char_type, nullptr);
64636492
}
64646493
} else if (ASR::is_a<ASR::UnionType_t>(*type)) {
@@ -6594,9 +6623,10 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
65946623
ASR::EnumStaticMember_t* enum_Var = ASR::down_cast<ASR::EnumStaticMember_t>(enum_ref->m_v);
65956624
ASR::Variable_t* enum_m_var = ASR::down_cast<ASR::Variable_t>(enum_Var->m_m);
65966625
char *s = enum_m_var->m_name;
6597-
size_t s_size = std::string(s).size();
6626+
ASR::expr_t* a_len = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, x.base.base.loc,
6627+
std::string(s).size(), ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc, 8))));
65986628
enum_ref_type = ASRUtils::TYPE(ASR::make_String_t(
6599-
al, x.base.base.loc, 1, s_size, nullptr,
6629+
al, x.base.base.loc, 1, a_len, false, false,
66006630
ASR::string_physical_typeType::PointerString));
66016631
enum_ref_value = ASRUtils::EXPR(ASR::make_StringConstant_t(al, x.base.base.loc,
66026632
s, enum_ref_type));
@@ -7706,8 +7736,10 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
77067736
args.reserve(al, 1);
77077737
ASR::call_arg_t str_arg;
77087738
str_arg.loc = loc;
7739+
ASR::expr_t* a_len = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al,
7740+
loc, s_var.size(), ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 8))));
77097741
ASR::ttype_t *str_type = ASRUtils::TYPE(ASR::make_String_t(al, loc,
7710-
1, s_var.size(), nullptr, ASR::string_physical_typeType::PointerString));
7742+
1, a_len, false, false, ASR::string_physical_typeType::PointerString));
77117743
str_arg.m_value = ASRUtils::EXPR(
77127744
ASR::make_StringConstant_t(al, loc, s2c(al, s_var), str_type));
77137745
ASR::call_arg_t sub_arg;
@@ -7741,8 +7773,10 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
77417773
args.reserve(al, 1);
77427774
ASR::call_arg_t str_arg;
77437775
str_arg.loc = loc;
7776+
ASR::expr_t* a_len = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al,
7777+
loc, s_var.size(), ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 8))));
77447778
ASR::ttype_t *str_type = ASRUtils::TYPE(ASR::make_String_t(al, loc,
7745-
1, s_var.size(), nullptr, ASR::string_physical_typeType::PointerString));
7779+
1, a_len, true, true, ASR::string_physical_typeType::PointerString));
77467780
str_arg.m_value = ASRUtils::EXPR(
77477781
ASR::make_StringConstant_t(al, loc, s2c(al, s_var), str_type));
77487782
ASR::call_arg_t sub_arg;
@@ -7828,8 +7862,10 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
78287862
args.reserve(al, 1);
78297863
ASR::call_arg_t str_arg;
78307864
str_arg.loc = loc;
7865+
ASR::expr_t* a_len = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al,
7866+
loc, s_var.size(), ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 8))));
78317867
ASR::ttype_t *str_type = ASRUtils::TYPE(ASR::make_String_t(al, loc,
7832-
1, s_var.size(), nullptr, ASR::string_physical_typeType::PointerString));
7868+
1, a_len, false, false, ASR::string_physical_typeType::PointerString));
78337869
str_arg.m_value = ASRUtils::EXPR(
78347870
ASR::make_StringConstant_t(al, loc, s2c(al, s_var), str_type));
78357871
ASR::call_arg_t sub_arg;
@@ -7884,8 +7920,10 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
78847920
args.reserve(al, 1);
78857921
ASR::call_arg_t str_arg;
78867922
str_arg.loc = loc;
7923+
ASR::expr_t* a_len = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al,
7924+
loc, s_var.size(), ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 8))));
78877925
ASR::ttype_t *str_type = ASRUtils::TYPE(ASR::make_String_t(al, loc,
7888-
1, s_var.size(), nullptr, ASR::string_physical_typeType::PointerString));
7926+
1, a_len, false, false, ASR::string_physical_typeType::PointerString));
78897927
str_arg.m_value = ASRUtils::EXPR(
78907928
ASR::make_StringConstant_t(al, loc, s2c(al, s_var), str_type));
78917929
ASR::call_arg_t sub_arg;
@@ -7911,8 +7949,10 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
79117949
throw SemanticError("String to undergo partition cannot be empty",
79127950
loc);
79137951
}
7952+
ASR::expr_t* a_len = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al,
7953+
loc, s_var.size(), ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 8))));
79147954
ASR::ttype_t *char_type = ASRUtils::TYPE(ASR::make_String_t(al,
7915-
loc, 1, s_var.size(), nullptr, ASR::string_physical_typeType::PointerString));
7955+
loc, 1, a_len, false, false, ASR::string_physical_typeType::PointerString));
79167956
ASR::expr_t *str = ASRUtils::EXPR(ASR::make_StringConstant_t(al,
79177957
loc, s2c(al, s_var), char_type));
79187958
tmp = ASRUtils::Partition::create_partition(al, loc, args_, str, diag);
@@ -8115,8 +8155,10 @@ we will have to use something else.
81158155
throw SemanticError("'str' object has no attribute '" + attr_name + "'",
81168156
loc);
81178157
}
8158+
ASR::expr_t* a_len = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al,
8159+
loc, s_var.size(), ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 8))));
81188160
ASR::ttype_t *str_type = ASRUtils::TYPE(ASR::make_String_t(al, loc,
8119-
1, s_var.size(), nullptr, ASR::string_physical_typeType::PointerString));
8161+
1, a_len, false, false, ASR::string_physical_typeType::PointerString));
81208162
tmp = ASR::make_StringConstant_t(al, loc, s2c(al, s_var), str_type);
81218163
}
81228164

@@ -8547,8 +8589,10 @@ we will have to use something else.
85478589
}
85488590
}
85498591
}
8592+
ASR::expr_t* a_len = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, x.base.base.loc,
8593+
0, ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc, 8))));
85508594
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_String_t(
8551-
al, x.base.base.loc, -1, 0, nullptr, ASR::string_physical_typeType::PointerString));
8595+
al, x.base.base.loc, -1, a_len, false, false, ASR::string_physical_typeType::PointerString));
85528596
ASR::expr_t* string_format = ASRUtils::EXPR(ASRUtils::make_StringFormat_t_util(al, x.base.base.loc,
85538597
nullptr, args_expr.p, args_expr.size(), ASR::string_format_kindType::FormatPythonFormat,
85548598
type, nullptr));

0 commit comments

Comments
 (0)