@@ -3247,10 +3247,12 @@ class ReplaceWithFunctionParamVisitor: public ASR::BaseExprReplacer<ReplaceWithF
32473247
32483248 size_t n_args;
32493249
3250+ SymbolTable* current_scope;
3251+
32503252 public:
32513253
32523254 ReplaceWithFunctionParamVisitor (Allocator& al_, ASR::expr_t ** m_args_, size_t n_args_) :
3253- al (al_), m_args(m_args_), n_args(n_args_) {}
3255+ al (al_), m_args(m_args_), n_args(n_args_), current_scope( nullptr ) {}
32543256
32553257 void replace_Var (ASR::Var_t* x) {
32563258 size_t arg_idx = 0 ;
@@ -3268,14 +3270,26 @@ class ReplaceWithFunctionParamVisitor: public ASR::BaseExprReplacer<ReplaceWithF
32683270 if ( idx_found ) {
32693271 LCOMPILERS_ASSERT (current_expr);
32703272 ASR::ttype_t * t_ = replace_args_with_FunctionParam (
3271- ASRUtils::symbol_type (x->m_v ));
3273+ ASRUtils::symbol_type (x->m_v ), current_scope );
32723274 *current_expr = ASRUtils::EXPR (ASR::make_FunctionParam_t (
32733275 al, m_args[arg_idx]->base .loc , arg_idx,
32743276 t_, nullptr ));
32753277 }
32763278 }
32773279
3278- ASR::ttype_t * replace_args_with_FunctionParam (ASR::ttype_t * t) {
3280+ void replace_Struct (ASR::Struct_t *x) {
3281+ std::string derived_type_name = ASRUtils::symbol_name (x->m_derived_type );
3282+ ASR::symbol_t * derived_type_sym = current_scope->resolve_symbol (derived_type_name);
3283+ LCOMPILERS_ASSERT_MSG ( derived_type_sym != nullptr ,
3284+ " derived_type_sym cannot be nullptr" );
3285+ if (derived_type_sym != x->m_derived_type ) {
3286+ x->m_derived_type = derived_type_sym;
3287+ }
3288+ }
3289+
3290+ ASR::ttype_t * replace_args_with_FunctionParam (ASR::ttype_t * t, SymbolTable* current_scope) {
3291+ this ->current_scope = current_scope;
3292+
32793293 ASRUtils::ExprStmtDuplicator duplicator (al);
32803294 duplicator.allow_procedure_calls = true ;
32813295
@@ -3312,21 +3326,21 @@ inline ASR::asr_t* make_FunctionType_t_util(Allocator &al,
33123326 ASR::expr_t * a_return_var, ASR::abiType a_abi, ASR::deftypeType a_deftype,
33133327 char * a_bindc_name, bool a_elemental, bool a_pure, bool a_module, bool a_inline,
33143328 bool a_static,
3315- ASR::symbol_t ** a_restrictions, size_t n_restrictions, bool a_is_restriction) {
3329+ ASR::symbol_t ** a_restrictions, size_t n_restrictions, bool a_is_restriction, SymbolTable* current_scope ) {
33163330 Vec<ASR::ttype_t *> arg_types;
33173331 arg_types.reserve (al, n_args);
33183332 ReplaceWithFunctionParamVisitor replacer (al, a_args, n_args);
33193333 for ( size_t i = 0 ; i < n_args; i++ ) {
33203334 // We need to substitute all direct argument variable references with
33213335 // FunctionParam.
33223336 ASR::ttype_t *t = replacer.replace_args_with_FunctionParam (
3323- expr_type (a_args[i]));
3337+ expr_type (a_args[i]), current_scope );
33243338 arg_types.push_back (al, t);
33253339 }
33263340 ASR::ttype_t * return_var_type = nullptr ;
33273341 if ( a_return_var ) {
33283342 return_var_type = replacer.replace_args_with_FunctionParam (
3329- ASRUtils::expr_type (a_return_var));
3343+ ASRUtils::expr_type (a_return_var), current_scope );
33303344 }
33313345
33323346 LCOMPILERS_ASSERT (arg_types.size () == n_args);
@@ -3338,12 +3352,12 @@ inline ASR::asr_t* make_FunctionType_t_util(Allocator &al,
33383352}
33393353
33403354inline ASR::asr_t * make_FunctionType_t_util (Allocator &al, const Location &a_loc,
3341- ASR::expr_t ** a_args, size_t n_args, ASR::expr_t * a_return_var, ASR::FunctionType_t* ft) {
3355+ ASR::expr_t ** a_args, size_t n_args, ASR::expr_t * a_return_var, ASR::FunctionType_t* ft, SymbolTable* current_scope ) {
33423356 return ASRUtils::make_FunctionType_t_util (al, a_loc, a_args, n_args, a_return_var,
33433357 ft->m_abi , ft->m_deftype , ft->m_bindc_name , ft->m_elemental ,
33443358 ft->m_pure , ft->m_module , ft->m_inline , ft->m_static ,
33453359 ft->m_restrictions ,
3346- ft->n_restrictions , ft->m_is_restriction );
3360+ ft->n_restrictions , ft->m_is_restriction , current_scope );
33473361}
33483362
33493363inline ASR::asr_t * make_Function_t_util (Allocator& al, const Location& loc,
@@ -3357,7 +3371,7 @@ inline ASR::asr_t* make_Function_t_util(Allocator& al, const Location& loc,
33573371 ASR::ttype_t * func_type = ASRUtils::TYPE (ASRUtils::make_FunctionType_t_util (
33583372 al, loc, a_args, n_args, m_return_var, m_abi, m_deftype, m_bindc_name,
33593373 m_elemental, m_pure, m_module, m_inline, m_static,
3360- m_restrictions, n_restrictions, m_is_restriction));
3374+ m_restrictions, n_restrictions, m_is_restriction, m_symtab ));
33613375 return ASR::make_Function_t (
33623376 al, loc, m_symtab, m_name, func_type, m_dependencies, n_dependencies,
33633377 a_args, n_args, m_body, n_body, m_return_var, m_access, m_deterministic,
0 commit comments