@@ -20,146 +20,148 @@ using ASRUtils::EXPR;
20
20
*/
21
21
void pass_wrap_global_stmts_into_function (Allocator &al,
22
22
ASR::TranslationUnit_t &unit, const LCompilers::PassOptions& pass_options) {
23
+ if (unit.n_items == 0 ) {
24
+ return ;
25
+ }
26
+
23
27
std::string fn_name_s = pass_options.run_fun ;
24
- if (unit.n_items > 0 ) {
25
- // Add an anonymous function
26
- Str s;
27
- s.from_str_view (fn_name_s);
28
- char *fn_name = s.c_str (al);
29
- SymbolTable *fn_scope = al.make_new <SymbolTable>(unit.m_global_scope );
28
+ // Add an anonymous function
29
+ Str s;
30
+ s.from_str_view (fn_name_s);
31
+ char *fn_name = s.c_str (al);
32
+ SymbolTable *fn_scope = al.make_new <SymbolTable>(unit.m_global_scope );
30
33
31
- ASR::ttype_t *type;
32
- Location loc = unit.base .base .loc ;
33
- ASR::asr_t *return_var=nullptr ;
34
- ASR::expr_t *return_var_ref=nullptr ;
35
- char *var_name;
36
- int idx = 1 ;
34
+ ASR::ttype_t *type;
35
+ Location loc = unit.base .base .loc ;
36
+ ASR::asr_t *return_var=nullptr ;
37
+ ASR::expr_t *return_var_ref=nullptr ;
38
+ char *var_name;
39
+ int idx = 1 ;
37
40
38
- Vec<ASR::stmt_t *> body;
39
- body.reserve (al, unit.n_items );
40
- for (size_t i=0 ; i<unit.n_items ; i++) {
41
- if (unit.m_items [i]->type == ASR::asrType::expr) {
42
- ASR::expr_t *target;
43
- ASR::expr_t *value = EXPR (unit.m_items [i]);
44
- // Create a new variable with the right type
45
- if (ASRUtils::expr_type (value)->type == ASR::ttypeType::Integer) {
46
- s.from_str (al, fn_name_s + std::to_string (idx));
47
- var_name = s.c_str (al);
41
+ Vec<ASR::stmt_t *> body;
42
+ body.reserve (al, unit.n_items );
43
+ for (size_t i=0 ; i<unit.n_items ; i++) {
44
+ if (unit.m_items [i]->type == ASR::asrType::expr) {
45
+ ASR::expr_t *target;
46
+ ASR::expr_t *value = EXPR (unit.m_items [i]);
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);
48
51
49
- int a_kind = down_cast<ASR::Integer_t>(ASRUtils::expr_type (value))->m_kind ;
52
+ int a_kind = down_cast<ASR::Integer_t>(ASRUtils::expr_type (value))->m_kind ;
50
53
51
- type = ASRUtils::TYPE (ASR::make_Integer_t (al, loc, a_kind, nullptr , 0 ));
52
- return_var = ASR::make_Variable_t (al, loc,
53
- fn_scope, var_name, nullptr , 0 , ASRUtils::intent_local, nullptr , nullptr ,
54
- ASR::storage_typeType::Default, type,
55
- ASR::abiType::BindC,
56
- ASR::Public, ASR::presenceType::Required, false );
57
- return_var_ref = EXPR (ASR::make_Var_t (al, loc,
58
- down_cast<ASR::symbol_t >(return_var)));
59
- fn_scope->add_symbol (std::string (var_name), down_cast<ASR::symbol_t >(return_var));
60
- target = return_var_ref;
61
- idx++;
62
- } else if (ASRUtils::expr_type (value)->type == ASR::ttypeType::Real) {
63
- s.from_str (al, fn_name_s + std::to_string (idx));
64
- var_name = s.c_str (al);
65
- type = ASRUtils::expr_type (value);
66
- return_var = ASR::make_Variable_t (al, loc,
67
- fn_scope, var_name, nullptr , 0 , ASRUtils::intent_local, nullptr , nullptr ,
68
- ASR::storage_typeType::Default, type,
69
- ASR::abiType::BindC,
70
- ASR::Public, ASR::presenceType::Required, false );
71
- return_var_ref = EXPR (ASR::make_Var_t (al, loc,
72
- down_cast<ASR::symbol_t >(return_var)));
73
- fn_scope->add_symbol (std::string (var_name), down_cast<ASR::symbol_t >(return_var));
74
- target = return_var_ref;
75
- idx++;
76
- } else if (ASRUtils::expr_type (value)->type == ASR::ttypeType::Complex) {
77
- s.from_str (al, fn_name_s + std::to_string (idx));
78
- var_name = s.c_str (al);
79
- type = ASRUtils::expr_type (value);
80
- return_var = ASR::make_Variable_t (al, loc,
81
- fn_scope, var_name, nullptr , 0 , ASRUtils::intent_local, nullptr , nullptr ,
82
- ASR::storage_typeType::Default, type,
83
- ASR::abiType::BindC,
84
- ASR::Public, ASR::presenceType::Required, false );
85
- return_var_ref = EXPR (ASR::make_Var_t (al, loc,
86
- down_cast<ASR::symbol_t >(return_var)));
87
- fn_scope->add_symbol (std::string (var_name), down_cast<ASR::symbol_t >(return_var));
88
- target = return_var_ref;
89
- idx++;
90
- } else {
91
- throw LCompilersException (" Return type not supported in interactive mode" );
92
- }
93
- ASR::stmt_t * asr_stmt = ASRUtils::STMT (ASR::make_Assignment_t (al, loc, target, value, nullptr ));
94
- body.push_back (al, asr_stmt);
95
- } else if (unit.m_items [i]->type == ASR::asrType::stmt) {
96
- ASR::stmt_t * asr_stmt = ASRUtils::STMT (unit.m_items [i]);
97
- body.push_back (al, asr_stmt);
98
- return_var = nullptr ;
54
+ type = ASRUtils::TYPE (ASR::make_Integer_t (al, loc, a_kind, nullptr , 0 ));
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
+ 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::Real) {
66
+ s.from_str (al, fn_name_s + std::to_string (idx));
67
+ var_name = s.c_str (al);
68
+ type = ASRUtils::expr_type (value);
69
+ return_var = ASR::make_Variable_t (al, loc,
70
+ fn_scope, var_name, nullptr , 0 , ASRUtils::intent_local, nullptr , nullptr ,
71
+ ASR::storage_typeType::Default, type,
72
+ ASR::abiType::BindC,
73
+ ASR::Public, ASR::presenceType::Required, false );
74
+ return_var_ref = EXPR (ASR::make_Var_t (al, loc,
75
+ down_cast<ASR::symbol_t >(return_var)));
76
+ fn_scope->add_symbol (std::string (var_name), down_cast<ASR::symbol_t >(return_var));
77
+ target = return_var_ref;
78
+ idx++;
79
+ } else if (ASRUtils::expr_type (value)->type == ASR::ttypeType::Complex) {
80
+ s.from_str (al, fn_name_s + std::to_string (idx));
81
+ var_name = s.c_str (al);
82
+ type = ASRUtils::expr_type (value);
83
+ return_var = ASR::make_Variable_t (al, loc,
84
+ fn_scope, var_name, nullptr , 0 , ASRUtils::intent_local, nullptr , nullptr ,
85
+ ASR::storage_typeType::Default, type,
86
+ ASR::abiType::BindC,
87
+ ASR::Public, ASR::presenceType::Required, false );
88
+ return_var_ref = EXPR (ASR::make_Var_t (al, loc,
89
+ down_cast<ASR::symbol_t >(return_var)));
90
+ fn_scope->add_symbol (std::string (var_name), down_cast<ASR::symbol_t >(return_var));
91
+ target = return_var_ref;
92
+ idx++;
99
93
} else {
100
- throw LCompilersException (" Unsupported type of global scope node " );
94
+ throw LCompilersException (" Return type not supported in interactive mode " );
101
95
}
96
+ ASR::stmt_t * asr_stmt = ASRUtils::STMT (ASR::make_Assignment_t (al, loc, target, value, nullptr ));
97
+ body.push_back (al, asr_stmt);
98
+ } else if (unit.m_items [i]->type == ASR::asrType::stmt) {
99
+ ASR::stmt_t * asr_stmt = ASRUtils::STMT (unit.m_items [i]);
100
+ body.push_back (al, asr_stmt);
101
+ return_var = nullptr ;
102
+ } else {
103
+ throw LCompilersException (" Unsupported type of global scope node" );
102
104
}
105
+ }
103
106
104
- if (return_var) {
105
- // The last item was an expression, create a function returning it
107
+ if (return_var) {
108
+ // The last item was an expression, create a function returning it
106
109
107
- // The last defined `return_var` is the actual return value
108
- ASR::down_cast2<ASR::Variable_t>(return_var)->m_intent = ASRUtils::intent_return_var;
110
+ // The last defined `return_var` is the actual return value
111
+ ASR::down_cast2<ASR::Variable_t>(return_var)->m_intent = ASRUtils::intent_return_var;
109
112
110
113
111
- ASR::asr_t *fn = ASRUtils::make_Function_t_util (
112
- al, loc,
113
- /* a_symtab */ fn_scope,
114
- /* a_name */ fn_name,
115
- nullptr , 0 ,
116
- /* a_args */ nullptr ,
117
- /* n_args */ 0 ,
118
- /* a_body */ body.p ,
119
- /* n_body */ body.size (),
120
- /* a_return_var */ return_var_ref,
121
- ASR::abiType::BindC,
122
- ASR::Public, ASR::Implementation,
123
- nullptr ,
124
- false , false , false , false , false ,
125
- nullptr , 0 ,
126
- nullptr , 0 ,
127
- false , false , false );
128
- std::string sym_name = fn_name;
129
- if (unit.m_global_scope ->get_symbol (sym_name) != nullptr ) {
130
- throw LCompilersException (" Function already defined" );
131
- }
132
- unit.m_global_scope ->add_symbol (sym_name, down_cast<ASR::symbol_t >(fn));
133
- } else {
134
- // The last item was a statement, create a subroutine (returning
135
- // nothing)
136
- ASR::asr_t *fn = ASRUtils::make_Function_t_util (
137
- al, loc,
138
- /* a_symtab */ fn_scope,
139
- /* a_name */ fn_name,
140
- nullptr , 0 ,
141
- /* a_args */ nullptr ,
142
- /* n_args */ 0 ,
143
- /* a_body */ body.p ,
144
- /* n_body */ body.size (),
145
- nullptr ,
146
- ASR::abiType::Source,
147
- ASR::Public, ASR::Implementation, nullptr ,
148
- false , false , false , false , false ,
149
- nullptr , 0 ,
150
- nullptr , 0 ,
151
- false , false , false );
152
- std::string sym_name = fn_name;
153
- if (unit.m_global_scope ->get_symbol (sym_name) != nullptr ) {
154
- throw LCompilersException (" Function already defined" );
155
- }
156
- unit.m_global_scope ->add_symbol (sym_name, down_cast<ASR::symbol_t >(fn));
114
+ ASR::asr_t *fn = ASRUtils::make_Function_t_util (
115
+ al, loc,
116
+ /* a_symtab */ fn_scope,
117
+ /* a_name */ fn_name,
118
+ nullptr , 0 ,
119
+ /* a_args */ nullptr ,
120
+ /* n_args */ 0 ,
121
+ /* a_body */ body.p ,
122
+ /* n_body */ body.size (),
123
+ /* a_return_var */ return_var_ref,
124
+ ASR::abiType::BindC,
125
+ ASR::Public, ASR::Implementation,
126
+ nullptr ,
127
+ false , false , false , false , false ,
128
+ nullptr , 0 ,
129
+ nullptr , 0 ,
130
+ false , false , false );
131
+ std::string sym_name = fn_name;
132
+ if (unit.m_global_scope ->get_symbol (sym_name) != nullptr ) {
133
+ throw LCompilersException (" Function already defined" );
134
+ }
135
+ unit.m_global_scope ->add_symbol (sym_name, down_cast<ASR::symbol_t >(fn));
136
+ } else {
137
+ // The last item was a statement, create a subroutine (returning
138
+ // nothing)
139
+ ASR::asr_t *fn = ASRUtils::make_Function_t_util (
140
+ al, loc,
141
+ /* a_symtab */ fn_scope,
142
+ /* a_name */ fn_name,
143
+ nullptr , 0 ,
144
+ /* a_args */ nullptr ,
145
+ /* n_args */ 0 ,
146
+ /* a_body */ body.p ,
147
+ /* n_body */ body.size (),
148
+ nullptr ,
149
+ ASR::abiType::Source,
150
+ ASR::Public, ASR::Implementation, nullptr ,
151
+ false , false , false , false , false ,
152
+ nullptr , 0 ,
153
+ nullptr , 0 ,
154
+ false , false , false );
155
+ std::string sym_name = fn_name;
156
+ if (unit.m_global_scope ->get_symbol (sym_name) != nullptr ) {
157
+ throw LCompilersException (" Function already defined" );
157
158
}
158
- unit.m_items = nullptr ;
159
- unit.n_items = 0 ;
160
- PassUtils::UpdateDependenciesVisitor v (al);
161
- v.visit_TranslationUnit (unit);
159
+ unit.m_global_scope ->add_symbol (sym_name, down_cast<ASR::symbol_t >(fn));
162
160
}
161
+ unit.m_items = nullptr ;
162
+ unit.n_items = 0 ;
163
+ PassUtils::UpdateDependenciesVisitor v (al);
164
+ v.visit_TranslationUnit (unit);
163
165
}
164
166
165
167
} // namespace LCompilers
0 commit comments