Thanks to visit codestin.com
Credit goes to github.com

Skip to content

StructType node update #2743

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

Merged
merged 6 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/libasr/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ ttype
| Set(ttype type)
| List(ttype type)
| Tuple(ttype* type)
| StructType(symbol derived_type)
| StructType(ttype* data_member_types, ttype* member_function_types, bool is_cstruct, symbol derived_type)
| Enum(symbol enum_type)
| Union(symbol union_type)
| Class(symbol class_type)
Expand Down
10 changes: 5 additions & 5 deletions src/libasr/asr_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ ASR::asr_t* getStructInstanceMember_t(Allocator& al, const Location& loc,
nullptr, 0, member_variable->m_name, ASR::accessType::Public));
current_scope->add_symbol(mem_name, mem_es);
}
ASR::ttype_t* member_type = ASRUtils::TYPE(ASR::make_StructType_t(al,
ASR::ttype_t* member_type = ASRUtils::TYPE(ASRUtils::make_StructType_t_util(al,
member_variable->base.base.loc, mem_es));
return ASR::make_StructInstanceMember_t(al, loc, ASRUtils::EXPR(v_var),
mem_es, ASRUtils::fix_scoped_type(al, member_type, current_scope), nullptr);
Expand Down Expand Up @@ -543,10 +543,10 @@ ASR::asr_t* getStructInstanceMember_t(Allocator& al, const Location& loc,
} else {
der_ext = current_scope->get_symbol(mangled_name);
}
ASR::asr_t* der_new = ASR::make_StructType_t(al, loc, der_ext);
ASR::asr_t* der_new = ASRUtils::make_StructType_t_util(al, loc, der_ext);
member_type_ = ASRUtils::TYPE(der_new);
} else if(ASR::is_a<ASR::ExternalSymbol_t>(*der_type_sym)) {
member_type_ = ASRUtils::TYPE(ASR::make_StructType_t(al, loc, der_type_sym));
member_type_ = ASRUtils::TYPE(ASRUtils::make_StructType_t_util(al, loc, der_type_sym));
}
member_type = ASRUtils::make_Array_t_util(al, loc,
member_type_, m_dims, n_dims);
Expand Down Expand Up @@ -677,7 +677,7 @@ bool use_overloaded(ASR::expr_t* left, ASR::expr_t* right,
ASRUtils::symbol_name(ASRUtils::get_asr_owner(struct_t->m_derived_type)), nullptr, 0,
ASRUtils::symbol_name(struct_t->m_derived_type), ASR::accessType::Public)));
}
return_type = ASRUtils::TYPE(ASR::make_StructType_t(al, loc,
return_type = ASRUtils::TYPE(ASRUtils::make_StructType_t_util(al, loc,
curr_scope->resolve_symbol(ASRUtils::symbol_name(struct_t->m_derived_type))));
if( is_array ) {
return_type = ASRUtils::make_Array_t_util(al, loc, return_type, m_dims, n_dims);
Expand Down Expand Up @@ -768,7 +768,7 @@ void process_overloaded_unary_minus_function(ASR::symbol_t* proc, ASR::expr_t* o
ASRUtils::symbol_name(ASRUtils::get_asr_owner(struct_t->m_derived_type)), nullptr, 0,
ASRUtils::symbol_name(struct_t->m_derived_type), ASR::accessType::Public)));
}
return_type = ASRUtils::TYPE(ASR::make_StructType_t(al, loc,
return_type = ASRUtils::TYPE(ASRUtils::make_StructType_t_util(al, loc,
curr_scope->resolve_symbol(ASRUtils::symbol_name(struct_t->m_derived_type))));
if( is_array ) {
return_type = ASRUtils::make_Array_t_util(
Expand Down
47 changes: 42 additions & 5 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2450,6 +2450,30 @@ static inline bool is_aggregate_type(ASR::ttype_t* asr_type) {

static inline ASR::dimension_t* duplicate_dimensions(Allocator& al, ASR::dimension_t* m_dims, size_t n_dims);

static inline ASR::asr_t* make_StructType_t_util(Allocator& al, Location loc, ASR::symbol_t* der){
ASR::Struct_t* st = ASR::down_cast<ASR::Struct_t>(ASRUtils::symbol_get_past_external(der));
Vec<ASR::ttype_t*> members;
members.reserve(al, st->n_members);
SymbolTable* current_scope = st->m_symtab;
for(size_t i = 0; i < st->n_members; i++){
ASR::symbol_t* temp = current_scope->get_symbol(st->m_members[i]);
if(ASR::is_a<ASR::Variable_t>(*temp)){
ASR::Variable_t* var = ASR::down_cast<ASR::Variable_t>(
ASRUtils::symbol_get_past_external(temp));
members.push_back(al,var->m_type);
}
}
return ASR::make_StructType_t(al,
loc,
members.p,
members.n,
nullptr, //Correct this when mem fn added to Struct_t
0, //Correct this when mem fn added to Struct_t
true, //Correct this when mem fn added to Struct_t
der);

}

static inline ASR::ttype_t* duplicate_type(Allocator& al, const ASR::ttype_t* t,
Vec<ASR::dimension_t>* dims=nullptr,
ASR::array_physical_typeType physical_type=ASR::array_physical_typeType::DescriptorArray,
Expand Down Expand Up @@ -2506,7 +2530,13 @@ static inline ASR::ttype_t* duplicate_type(Allocator& al, const ASR::ttype_t* t,
}
case ASR::ttypeType::StructType: {
ASR::StructType_t* tnew = ASR::down_cast<ASR::StructType_t>(t);
t_ = ASRUtils::TYPE(ASR::make_StructType_t(al, t->base.loc, tnew->m_derived_type));
t_ = ASRUtils::TYPE(ASR::make_StructType_t(al, t->base.loc,
tnew->m_data_member_types,
tnew->n_data_member_types,
tnew->m_member_function_types,
tnew->n_member_function_types,
tnew->m_is_cstruct,
tnew->m_derived_type));
break;
}
case ASR::ttypeType::Class: {
Expand Down Expand Up @@ -2656,7 +2686,13 @@ static inline ASR::ttype_t* duplicate_type_without_dims(Allocator& al, const ASR
}
case ASR::ttypeType::StructType: {
ASR::StructType_t* tstruct = ASR::down_cast<ASR::StructType_t>(t);
return ASRUtils::TYPE(ASR::make_StructType_t(al, loc, tstruct->m_derived_type));
return ASRUtils::TYPE(ASR::make_StructType_t(al, t->base.loc,
tstruct->m_data_member_types,
tstruct->n_data_member_types,
tstruct->m_member_function_types,
tstruct->n_member_function_types,
tstruct->m_is_cstruct,
tstruct->m_derived_type));
}
case ASR::ttypeType::Pointer: {
ASR::Pointer_t* ptr = ASR::down_cast<ASR::Pointer_t>(t);
Expand Down Expand Up @@ -3621,9 +3657,10 @@ static inline ASR::symbol_t* import_struct_instance_member(Allocator& al, ASR::s
nullptr, 0, s2c(al, struct_type_name), ASR::accessType::Public));
scope->add_symbol(struct_type_name_, imported_struct_type);
}
mem_type = ASRUtils::TYPE(ASR::make_StructType_t(al, mem_type->base.loc, scope->get_symbol(struct_type_name_)));
mem_type = ASRUtils::TYPE(ASRUtils::make_StructType_t_util(al, mem_type->base.loc,
scope->get_symbol(struct_type_name_)));
} else {
mem_type = ASRUtils::TYPE(ASR::make_StructType_t(al, mem_type->base.loc,
mem_type = ASRUtils::TYPE(ASRUtils::make_StructType_t_util(al, mem_type->base.loc,
scope->resolve_symbol(struct_type_name)));
}
}
Expand Down Expand Up @@ -4882,7 +4919,7 @@ static inline void import_struct_t(Allocator& al,
} else {
der_sym = current_scope->resolve_symbol(sym_name);
}
var_type = ASRUtils::TYPE(ASR::make_StructType_t(al, loc, der_sym));
var_type = ASRUtils::TYPE(ASRUtils::make_StructType_t_util(al, loc, der_sym));
if( is_array ) {
var_type = ASRUtils::make_Array_t_util(al, loc, var_type, m_dims, n_dims,
ASR::abiType::Source, false, ptype, true);
Expand Down
6 changes: 3 additions & 3 deletions src/libasr/pass/instantiate_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ class SymbolInstantiator : public ASR::BaseExprStmtDuplicator<SymbolInstantiator
std::string new_struct_name = context_map[struct_name];
ASR::symbol_t *sym = func_scope->resolve_symbol(new_struct_name);
return ASRUtils::TYPE(
ASR::make_StructType_t(al, s->base.base.loc, sym));
ASRUtils::make_StructType_t_util(al, s->base.base.loc, sym));
} else {
return ttype;
}
Expand Down Expand Up @@ -1365,7 +1365,7 @@ class SymbolInstantiator : public ASR::BaseExprStmtDuplicator<SymbolInstantiator
std::string struct_name = ASRUtils::symbol_name(s->m_derived_type);
if (symbol_subs.find(struct_name) != symbol_subs.end()) {
ASR::symbol_t *sym = symbol_subs[struct_name];
return ASRUtils::TYPE(ASR::make_StructType_t(al, ttype->base.loc, sym));
return ASRUtils::TYPE(ASRUtils::make_StructType_t_util(al, ttype->base.loc, sym));
}
return ttype;
}
Expand Down Expand Up @@ -1785,7 +1785,7 @@ class BodyInstantiator : public ASR::BaseExprStmtDuplicator<BodyInstantiator>
std::string struct_name = ASRUtils::symbol_name(s->m_derived_type);
if (symbol_subs.find(struct_name) != symbol_subs.end()) {
ASR::symbol_t *sym = symbol_subs[struct_name];
ttype = ASRUtils::TYPE(ASR::make_StructType_t(al, s->base.base.loc, sym));
ttype = ASRUtils::TYPE(ASRUtils::make_StructType_t_util(al, s->base.base.loc, sym));
}
return ttype;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libasr/pass/nested_vars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ class ReplaceNestedVisitor: public ASR::CallReplacerOnExpressionsVisitor<Replace
m_derived_type = ASR::down_cast<ASR::symbol_t>(fn);
current_scope->add_symbol(fn_name, m_derived_type);
}
var_type_ = ASRUtils::TYPE(ASR::make_StructType_t(al, struct_t->base.base.loc,
var_type_ = ASRUtils::TYPE(ASRUtils::make_StructType_t_util(al, struct_t->base.base.loc,
m_derived_type));
if( ASR::is_a<ASR::Array_t>(*var_type) ) {
ASR::Array_t* array_t = ASR::down_cast<ASR::Array_t>(var_type);
Expand Down
2 changes: 1 addition & 1 deletion src/libasr/pass/pass_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace LCompilers {
struct_t->m_derived_type)->get_counter() ) { \
ASR::symbol_t* m_derived_type = current_scope->resolve_symbol( \
ASRUtils::symbol_name(struct_t->m_derived_type)); \
ASR::ttype_t* struct_type = ASRUtils::TYPE(ASR::make_StructType_t(al, \
ASR::ttype_t* struct_type = ASRUtils::TYPE(ASRUtils::make_StructType_t_util(al, \
struct_t->base.base.loc, m_derived_type)); \
array_ref_type = struct_type; \
} \
Expand Down
10 changes: 5 additions & 5 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
} else {
sym = es_s;
}
return ASRUtils::TYPE(ASR::make_StructType_t(al, loc, sym));
return ASRUtils::TYPE(ASRUtils::make_StructType_t_util(al, loc, sym));
}
default: {
return return_type;
Expand Down Expand Up @@ -822,7 +822,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
ASR::symbol_t *der_sym = ASRUtils::symbol_get_past_external(s);
if( der_sym ) {
if ( ASR::is_a<ASR::Struct_t>(*der_sym) ) {
type = ASRUtils::TYPE(ASR::make_StructType_t(al, loc, s));
type = ASRUtils::TYPE(ASRUtils::make_StructType_t_util(al, loc, s));
type = ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument);
} else if( ASR::is_a<ASR::EnumType_t>(*der_sym) ) {
type = ASRUtils::TYPE(ASR::make_Enum_t(al, loc, s));
Expand Down Expand Up @@ -1312,7 +1312,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
for (size_t i = args.size(); i < st->n_members; i++) {
args.push_back(al, st->m_initializers[i]);
}
ASR::ttype_t* der_type = ASRUtils::TYPE(ASR::make_StructType_t(al, loc, stemp));
ASR::ttype_t* der_type = ASRUtils::TYPE(ASRUtils::make_StructType_t_util(al, loc, stemp));
return ASR::make_StructConstructor_t(al, loc, stemp, args.p, args.size(), der_type, nullptr);
} else if( ASR::is_a<ASR::EnumType_t>(*s) ) {
Vec<ASR::expr_t*> args_new;
Expand Down Expand Up @@ -5831,7 +5831,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
s2c(al, struct_member_name), ASR::accessType::Public));
current_scope->add_symbol(import_name, import_struct_member);
}
member_var_type = ASRUtils::TYPE(ASR::make_StructType_t(al, loc, import_struct_member));
member_var_type = ASRUtils::TYPE(ASRUtils::make_StructType_t_util(al, loc, import_struct_member));
}
}
}
Expand Down Expand Up @@ -5965,7 +5965,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
s2c(al, struct_member_name), ASR::accessType::Public));
current_scope->add_symbol(import_name, import_struct_member);
}
member_var_type = ASRUtils::TYPE(ASR::make_StructType_t(al, loc, import_struct_member));
member_var_type = ASRUtils::TYPE(ASRUtils::make_StructType_t_util(al, loc, import_struct_member));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-intent_01-66824bc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-intent_01-66824bc.stdout",
"stdout_hash": "38b4d1ece76c889c88eefaa5555a6dc36a8af86de4aadd829c112e80",
"stdout_hash": "96424532864b51ff2a8d92da1fb40a8498342dcea99b54660a4c83c5",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
6 changes: 6 additions & 0 deletions tests/reference/asr-intent_01-66824bc.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
Default
(Array
(StructType
[(Integer 4)]
[]
.true.
2 Foo
)
[((IntegerConstant 0 (Integer 4))
Expand Down Expand Up @@ -132,6 +135,9 @@
)
(Array
(StructType
[(Integer 4)]
[]
.true.
2 Foo
)
[((IntegerConstant 0 (Integer 4))
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-structs_01-66dc2c9.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-structs_01-66dc2c9.stdout",
"stdout_hash": "ea461fd5fd7cbed415b1c4f879f266ee64487c3545e6469091eefaa6",
"stdout_hash": "153f705e29f2c88ec969ce035e2c91aca1caa2bdd3571966dd5c4f87",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
8 changes: 8 additions & 0 deletions tests/reference/asr-structs_01-66dc2c9.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@
()
Default
(StructType
[(Integer 4)
(Integer 4)]
[]
.true.
2 S
)
()
Expand Down Expand Up @@ -142,6 +146,10 @@
[((IntegerConstant 2 (Integer 4)))
(())]
(StructType
[(Integer 4)
(Integer 4)]
[]
.true.
2 S
)
()
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-structs_01-be14d49.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-structs_01-be14d49.stdout",
"stdout_hash": "9bfbbca1021052ba2e89a60c105899a6957f0adc9b1e271f33f81522",
"stdout_hash": "d70bd606bcda91e16034b677f6b03ae998de9012002a4c6a7c6a1bd2",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading
Loading