@@ -31,7 +31,7 @@ void pass_wrap_global_stmts(Allocator &al,
31
31
char *fn_name = s.c_str (al);
32
32
SymbolTable *fn_scope = al.make_new <SymbolTable>(unit.m_symtab );
33
33
34
- ASR::ttype_t *type;
34
+ ASR::ttype_t *type = nullptr ;
35
35
Location loc = unit.base .base .loc ;
36
36
ASR::asr_t *return_var=nullptr ;
37
37
ASR::expr_t *return_var_ref=nullptr ;
@@ -45,58 +45,14 @@ void pass_wrap_global_stmts(Allocator &al,
45
45
ASR::expr_t *target;
46
46
ASR::expr_t *value = EXPR (unit.m_items [i]);
47
47
// Create a new variable with the right type
48
- if (ASRUtils::expr_type (value)->type == ASR::ttypeType::Integer) {
49
- s.from_str (al, fn_name_s + std::to_string (idx));
50
- var_name = s.c_str (al);
51
-
52
- int a_kind = down_cast<ASR::Integer_t>(ASRUtils::expr_type (value))->m_kind ;
53
-
54
- type = ASRUtils::TYPE (ASR::make_Integer_t (al, loc, a_kind));
55
- return_var = ASR::make_Variable_t (al, loc,
56
- fn_scope, var_name, nullptr , 0 , ASRUtils::intent_local, nullptr , nullptr ,
57
- ASR::storage_typeType::Default, type,
58
- nullptr , ASR::abiType::BindC,
59
- ASR::Public, ASR::presenceType::Required, false );
60
- return_var_ref = EXPR (ASR::make_Var_t (al, loc,
61
- down_cast<ASR::symbol_t >(return_var)));
62
- fn_scope->add_symbol (std::string (var_name), down_cast<ASR::symbol_t >(return_var));
63
- target = return_var_ref;
64
- idx++;
65
- } else if (ASRUtils::expr_type (value)->type == ASR::ttypeType::UnsignedInteger) {
66
- s.from_str (al, fn_name_s + std::to_string (idx));
67
- var_name = s.c_str (al);
68
-
69
- int a_kind = down_cast<ASR::UnsignedInteger_t>(ASRUtils::expr_type (value))->m_kind ;
70
-
71
- type = ASRUtils::TYPE (ASR::make_UnsignedInteger_t (al, loc, a_kind));
72
- return_var = ASR::make_Variable_t (al, loc,
73
- fn_scope, var_name, nullptr , 0 , ASRUtils::intent_local, nullptr , nullptr ,
74
- ASR::storage_typeType::Default, type,
75
- nullptr , ASR::abiType::BindC,
76
- ASR::Public, ASR::presenceType::Required, false );
77
- return_var_ref = EXPR (ASR::make_Var_t (al, loc,
78
- down_cast<ASR::symbol_t >(return_var)));
79
- fn_scope->add_symbol (std::string (var_name), down_cast<ASR::symbol_t >(return_var));
80
- target = return_var_ref;
81
- idx++;
82
- } else if (ASRUtils::expr_type (value)->type == ASR::ttypeType::Logical) {
83
- s.from_str (al, fn_name_s + std::to_string (idx));
84
- var_name = s.c_str (al);
85
-
86
- int a_kind = down_cast<ASR::Logical_t>(ASRUtils::expr_type (value))->m_kind ;
87
-
88
- type = ASRUtils::TYPE (ASR::make_Logical_t (al, loc, a_kind));
89
- return_var = ASR::make_Variable_t (al, loc,
90
- fn_scope, var_name, nullptr , 0 , ASRUtils::intent_local, nullptr , nullptr ,
91
- ASR::storage_typeType::Default, type,
92
- nullptr , ASR::abiType::BindC,
93
- ASR::Public, ASR::presenceType::Required, false );
94
- return_var_ref = EXPR (ASR::make_Var_t (al, loc,
95
- down_cast<ASR::symbol_t >(return_var)));
96
- fn_scope->add_symbol (std::string (var_name), down_cast<ASR::symbol_t >(return_var));
97
- target = return_var_ref;
98
- idx++;
99
- } else if (ASRUtils::expr_type (value)->type == ASR::ttypeType::Real) {
48
+ if ((ASRUtils::expr_type (value)->type == ASR::ttypeType::Integer) ||
49
+ (ASRUtils::expr_type (value)->type == ASR::ttypeType::UnsignedInteger) ||
50
+ (ASRUtils::expr_type (value)->type == ASR::ttypeType::Logical) ||
51
+ (ASRUtils::expr_type (value)->type == ASR::ttypeType::Real) ||
52
+ (ASRUtils::expr_type (value)->type == ASR::ttypeType::Complex) ||
53
+ (ASRUtils::expr_type (value)->type == ASR::ttypeType::Character) ||
54
+ (ASRUtils::expr_type (value)->type == ASR::ttypeType::List) ||
55
+ (ASRUtils::expr_type (value)->type == ASR::ttypeType::Tuple)) {
100
56
s.from_str (al, fn_name_s + std::to_string (idx));
101
57
var_name = s.c_str (al);
102
58
type = ASRUtils::expr_type (value);
@@ -110,22 +66,7 @@ void pass_wrap_global_stmts(Allocator &al,
110
66
fn_scope->add_symbol (std::string (var_name), down_cast<ASR::symbol_t >(return_var));
111
67
target = return_var_ref;
112
68
idx++;
113
- } else if ((ASRUtils::expr_type (value)->type == ASR::ttypeType::Complex) ||
114
- (ASRUtils::expr_type (value)->type == ASR::ttypeType::Character)) {
115
- s.from_str (al, fn_name_s + std::to_string (idx));
116
- var_name = s.c_str (al);
117
- type = ASRUtils::expr_type (value);
118
- return_var = ASR::make_Variable_t (al, loc,
119
- fn_scope, var_name, nullptr , 0 , ASRUtils::intent_local, nullptr , nullptr ,
120
- ASR::storage_typeType::Default, type,
121
- nullptr , ASR::abiType::BindC,
122
- ASR::Public, ASR::presenceType::Required, false );
123
- return_var_ref = EXPR (ASR::make_Var_t (al, loc,
124
- down_cast<ASR::symbol_t >(return_var)));
125
- fn_scope->add_symbol (std::string (var_name), down_cast<ASR::symbol_t >(return_var));
126
- target = return_var_ref;
127
- idx++;
128
- } else {
69
+ } else {
129
70
throw LCompilersException (" Return type not supported in interactive mode" );
130
71
}
131
72
ASR::stmt_t * asr_stmt = ASRUtils::STMT (ASR::make_Assignment_t (al, loc, target, value, nullptr ));
@@ -141,7 +82,26 @@ void pass_wrap_global_stmts(Allocator &al,
141
82
142
83
if (return_var) {
143
84
// The last defined `return_var` is the actual return value
85
+ LCOMPILERS_ASSERT (type)
86
+ LCOMPILERS_ASSERT (return_var_ref)
87
+
144
88
ASR::down_cast2<ASR::Variable_t>(return_var)->m_intent = ASRUtils::intent_return_var;
89
+ std::string global_underscore_name = " _" + fn_name_s;
90
+ s.from_str (al, global_underscore_name);
91
+
92
+ ASR::asr_t *global_underscore = ASR::make_Variable_t (al, loc,
93
+ unit.m_symtab , s.c_str (al), nullptr , 0 , ASRUtils::intent_local, nullptr , nullptr ,
94
+ ASR::storage_typeType::Default, type,
95
+ nullptr , ASR::abiType::Source,
96
+ ASR::Public, ASR::presenceType::Required, false );
97
+ ASR::expr_t * global_underscore_ref = EXPR (ASR::make_Var_t (al, loc, down_cast<ASR::symbol_t >(global_underscore)));
98
+
99
+ if (fn_scope->parent ->get_symbol (global_underscore_name) != nullptr ) {
100
+ throw LCompilersException (" Global variable already defined" );
101
+ }
102
+ unit.m_symtab ->add_symbol (global_underscore_name, down_cast<ASR::symbol_t >(global_underscore));
103
+ ASR::stmt_t * asr_stmt = ASRUtils::STMT (ASR::make_Assignment_t (al, loc, global_underscore_ref, return_var_ref, nullptr ));
104
+ body.push_back (al, asr_stmt);
145
105
}
146
106
147
107
ASR::asr_t *fn = ASRUtils::make_Function_t_util (
0 commit comments