@@ -59,7 +59,7 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
59
59
PassUtils::create_idx_vars (idx_vars, n_dims, loc, al, current_scope);
60
60
ASR::stmt_t * doloop = nullptr ;
61
61
ASR::stmt_t * empty_print_endl = ASRUtils::STMT (ASR::make_Print_t (al, loc,
62
- nullptr , 0 , nullptr , nullptr ));
62
+ nullptr , 0 , nullptr , nullptr ));
63
63
ASR::ttype_t *str_type_len_1 = ASRUtils::TYPE (ASR::make_Character_t (
64
64
al, loc, 1 , 1 , nullptr ));
65
65
ASR::ttype_t *str_type_len_2 = ASRUtils::TYPE (ASR::make_Character_t (
@@ -106,28 +106,7 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
106
106
return doloop;
107
107
}
108
108
109
- void print_fixed_sized_array (ASR::expr_t *arr_expr,std::vector<ASR::expr_t *>& print_body, const Location &loc) {
110
- ASR::dimension_t * m_dims;
111
- int n_dims = ASRUtils::extract_dimensions_from_ttype (ASRUtils::expr_type (arr_expr), m_dims);
112
- int m_dim_length = ASR::down_cast<ASR::IntegerConstant_t>(m_dims->m_length )->m_n ;
113
- Vec<ASR::expr_t *> idx_vars;
114
- PassUtils::create_idx_vars (idx_vars, n_dims, loc, al, current_scope);
115
- ASR::ttype_t *int32_type = ASRUtils::TYPE (ASR::make_Integer_t (al, loc, 4 ));
116
- ASR::expr_t * one = ASRUtils::EXPR (ASR::make_IntegerConstant_t (al, loc, 1 , int32_type));
117
- for (int n = 0 ; n < n_dims; n++) {
118
- ASR::expr_t * idx_var = idx_vars[n];
119
- idx_var = one;
120
- for (int m = 0 ; m < m_dim_length; m++) {
121
- ASR::expr_t * ref = PassUtils::create_array_ref (arr_expr, idx_var, al, current_scope);
122
- print_body.push_back (ref);
123
- idx_var = ASRUtils::EXPR (ASR::make_IntegerBinOp_t (al, loc, idx_var,
124
- ASR::binopType::Add, one, int32_type, nullptr ));
125
- }
126
- }
127
- }
128
-
129
- ASR::stmt_t * create_formatstmt (std::vector<ASR::expr_t *> &print_body, ASR::StringFormat_t* format, const Location &loc, ASR::stmtType _type,
130
- ASR::expr_t * unit = nullptr , ASR::expr_t * separator = nullptr , ASR::expr_t * end = nullptr ) {
109
+ ASR::stmt_t * create_formatstmt (std::vector<ASR::expr_t *> &print_body, ASR::StringFormat_t* format, const Location &loc, ASR::stmtType _type) {
131
110
Vec<ASR::expr_t *> body;
132
111
body.reserve (al, print_body.size ());
133
112
for (size_t j=0 ; j<print_body.size (); j++) {
@@ -142,10 +121,10 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
142
121
ASR::stmt_t * statement = nullptr ;
143
122
if (_type == ASR::stmtType::Print) {
144
123
statement = ASRUtils::STMT (ASR::make_Print_t (al, loc,
145
- print_args.p , print_args.size (), nullptr , nullptr ));
124
+ print_args.p , print_args.size (), nullptr , nullptr ));
146
125
} else if (_type == ASR::stmtType::FileWrite) {
147
- statement = ASRUtils::STMT (ASR::make_FileWrite_t (al, loc, 0 , unit ,
148
- nullptr , nullptr , nullptr , print_args.p , print_args.size (), separator, end ));
126
+ statement = ASRUtils::STMT (ASR::make_FileWrite_t (al, loc, 0 , nullptr ,
127
+ nullptr , nullptr , nullptr , print_args.p , print_args.size (), nullptr , nullptr ));
149
128
}
150
129
print_body.clear ();
151
130
return statement;
@@ -157,21 +136,17 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
157
136
ASR::stmt_t * print_stmt;
158
137
if (x.m_values [0 ] != nullptr && ASR::is_a<ASR::StringFormat_t>(*x.m_values [0 ])) {
159
138
empty_print_endl = ASRUtils::STMT (ASR::make_Print_t (al, x.base .base .loc ,
160
- nullptr , 0 , nullptr , nullptr ));
139
+ nullptr , 0 , nullptr , nullptr ));
161
140
ASR::StringFormat_t* format = ASR::down_cast<ASR::StringFormat_t>(x.m_values [0 ]);
162
141
for (size_t i=0 ; i<format->n_args ; i++) {
163
142
if (PassUtils::is_array (format->m_args [i])) {
164
- if (ASRUtils::is_fixed_size_array (ASRUtils::expr_type (format->m_args [i]))) {
165
- print_fixed_sized_array (format->m_args [i], print_body, x.base .base .loc );
166
- } else {
167
- if (print_body.size () > 0 ) {
168
- print_stmt = create_formatstmt (print_body, format, x.base .base .loc , ASR::stmtType::Print);
169
- pass_result.push_back (al, print_stmt);
170
- }
171
- print_stmt = print_array_using_doloop (format->m_args [i],format, x.base .base .loc );
143
+ if (print_body.size () > 0 ) {
144
+ print_stmt = create_formatstmt (print_body, format, x.base .base .loc , ASR::stmtType::Print);
172
145
pass_result.push_back (al, print_stmt);
173
- pass_result.push_back (al, empty_print_endl);
174
146
}
147
+ print_stmt = print_array_using_doloop (format->m_args [i],format, x.base .base .loc );
148
+ pass_result.push_back (al, print_stmt);
149
+ pass_result.push_back (al, empty_print_endl);
175
150
} else {
176
151
print_body.push_back (format->m_args [i]);
177
152
}
@@ -189,12 +164,17 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
189
164
ASR::expr_t *backspace = ASRUtils::EXPR (ASR::make_StringConstant_t (
190
165
al, x.base .base .loc , s2c (al, " \b " ), str_type_len_1));
191
166
ASR::stmt_t * back = ASRUtils::STMT (ASR::make_Print_t (al, x.base .base .loc ,
192
- nullptr , 0 , nullptr , backspace));
167
+ nullptr , 0 , nullptr , backspace));
193
168
for (size_t i=0 ; i<x.n_values ; i++) {
194
- // DIVERGENCE between LFortran and LPython
195
- // If a pointer array variable is provided
196
- // then it will be printed as a normal array.
197
- if (PassUtils::is_array (x.m_values [i])) {
169
+ // TODO: This will disallow printing array pointer in Fortran
170
+ // Pointers are treated the same as normal variables in Fortran
171
+ // However, LPython prints the address of pointers when you do
172
+ // print(some_pointer). Same goes for C/C++ (if we add their frontends in future).
173
+ // So we need to figure out a way to de-couple printing support from libasr
174
+ // or add nodes according to the frontends because each frontend will have a different
175
+ // way of handling printing of pointers and non-pointers
176
+ if (!ASR::is_a<ASR::Pointer_t>(*ASRUtils::expr_type (x.m_values [i])) &&
177
+ PassUtils::is_array (x.m_values [i])) {
198
178
if (print_body.size () > 0 ) {
199
179
Vec<ASR::expr_t *> body;
200
180
body.reserve (al, print_body.size ());
@@ -219,18 +199,18 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
219
199
if (x.m_separator ) {
220
200
if (i == x.n_values - 1 ) {
221
201
empty_print_endl = ASRUtils::STMT (ASR::make_Print_t (al, x.base .base .loc ,
222
- nullptr , 0 , nullptr , x.m_end ));
202
+ nullptr , 0 , nullptr , x.m_end ));
223
203
} else {
224
204
empty_print_endl = ASRUtils::STMT (ASR::make_Print_t (al, x.base .base .loc ,
225
- nullptr , 0 , nullptr , x.m_separator ));
205
+ nullptr , 0 , nullptr , x.m_separator ));
226
206
}
227
207
} else {
228
208
if (i == x.n_values - 1 ) {
229
209
empty_print_endl = ASRUtils::STMT (ASR::make_Print_t (al, x.base .base .loc ,
230
- nullptr , 0 , nullptr , x.m_end ));
210
+ nullptr , 0 , nullptr , x.m_end ));
231
211
} else {
232
212
empty_print_endl = ASRUtils::STMT (ASR::make_Print_t (al, x.base .base .loc ,
233
- nullptr , 0 , nullptr , nullptr ));
213
+ nullptr , 0 , nullptr , nullptr ));
234
214
}
235
215
}
236
216
pass_result.push_back (al, empty_print_endl);
@@ -252,7 +232,7 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
252
232
}
253
233
}
254
234
255
- ASR::stmt_t * write_array_using_doloop (ASR::expr_t *arr_expr, ASR::StringFormat_t* format, ASR:: expr_t * unit, const Location &loc) {
235
+ ASR::stmt_t * write_array_using_doloop (ASR::expr_t *arr_expr, ASR::StringFormat_t* format, const Location &loc) {
256
236
int n_dims = PassUtils::get_rank (arr_expr);
257
237
Vec<ASR::expr_t *> idx_vars;
258
238
PassUtils::create_idx_vars (idx_vars, n_dims, loc, al, current_scope);
@@ -262,7 +242,7 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
262
242
ASR::expr_t *empty_space = ASRUtils::EXPR (ASR::make_StringConstant_t (
263
243
al, loc, s2c (al, " " ), str_type_len));
264
244
ASR::stmt_t * empty_file_write_endl = ASRUtils::STMT (ASR::make_FileWrite_t (al, loc,
265
- 0 , unit , nullptr , nullptr , nullptr , nullptr , 0 , nullptr , nullptr ));
245
+ 0 , nullptr , nullptr , nullptr , nullptr , nullptr , 0 , nullptr , nullptr ));
266
246
for ( int i = n_dims - 1 ; i >= 0 ; i-- ) {
267
247
ASR::do_loop_head_t head;
268
248
head.m_v = idx_vars[i];
@@ -286,11 +266,11 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
286
266
format_args.reserve (al, 1 );
287
267
format_args.push_back (al, string_format);
288
268
write_stmt = ASRUtils::STMT (ASR::make_FileWrite_t (
289
- al, loc, i, unit , nullptr , nullptr , nullptr ,
269
+ al, loc, i, nullptr , nullptr , nullptr , nullptr ,
290
270
format_args.p , format_args.size (), nullptr , empty_space));
291
271
} else {
292
272
write_stmt = ASRUtils::STMT (ASR::make_FileWrite_t (
293
- al, loc, i, unit , nullptr , nullptr , nullptr ,
273
+ al, loc, i, nullptr , nullptr , nullptr , nullptr ,
294
274
print_args.p , print_args.size (), nullptr , nullptr ));
295
275
}
296
276
doloop_body.push_back (al, write_stmt);
@@ -307,56 +287,54 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
307
287
Vec<ASR::expr_t *> body;
308
288
body.from_pointer_n_copy (al, write_body.data (), write_body.size ());
309
289
ASR::stmt_t * write_stmt = ASRUtils::STMT (ASR::make_FileWrite_t (
310
- al, x.base .base .loc , x.m_label , x.m_unit , x.m_iomsg ,
311
- x.m_iostat , x. m_id , body.p , body.size (), x.m_separator , x.m_end ));
290
+ al, x.base .base .loc , x.m_label , x.m_unit , x.m_iomsg , x. m_iostat ,
291
+ x.m_id , body.p , body.size (), x.m_separator , x.m_end ));
312
292
pass_result.push_back (al, write_stmt);
313
293
write_body.clear ();
314
294
}
315
295
316
296
void visit_FileWrite (const ASR::FileWrite_t& x) {
317
- if (x.m_unit && ASRUtils::is_character (*ASRUtils::expr_type (x.m_unit ))) {
318
- // Skip for character write
319
- return ;
320
- }
321
297
std::vector<ASR::expr_t *> write_body;
322
298
ASR::stmt_t * write_stmt;
323
299
ASR::stmt_t * empty_file_write_endl = ASRUtils::STMT (ASR::make_FileWrite_t (al, x.base .base .loc ,
324
- x.m_label , x.m_unit , nullptr , nullptr , nullptr , nullptr , 0 , nullptr , nullptr ));
300
+ x.m_label , nullptr , nullptr , nullptr , nullptr ,
301
+ nullptr , 0 , nullptr , nullptr ));
325
302
if (x.m_values && x.m_values [0 ] != nullptr && ASR::is_a<ASR::StringFormat_t>(*x.m_values [0 ])){
326
303
ASR::StringFormat_t* format = ASR::down_cast<ASR::StringFormat_t>(x.m_values [0 ]);
327
304
for (size_t i=0 ; i<format->n_args ; i++) {
328
305
if (PassUtils::is_array (format->m_args [i])) {
329
- if (ASRUtils::is_fixed_size_array (ASRUtils::expr_type (format->m_args [i]))) {
330
- print_fixed_sized_array (format->m_args [i], write_body, x.base .base .loc );
331
- } else {
332
- if (write_body.size () > 0 ) {
333
- write_stmt = create_formatstmt (write_body, format, x.base .base .loc , ASR::stmtType::FileWrite, x.m_unit , x.m_separator , x.m_end );
334
- pass_result.push_back (al, write_stmt);
335
- }
336
- write_stmt = write_array_using_doloop (format->m_args [i], format, x.m_unit , x.base .base .loc );
306
+ if (write_body.size () > 0 ) {
307
+ write_stmt = create_formatstmt (write_body, format, x.base .base .loc , ASR::stmtType::FileWrite);
337
308
pass_result.push_back (al, write_stmt);
338
- pass_result.push_back (al, empty_file_write_endl);
339
309
}
310
+ write_stmt = write_array_using_doloop (format->m_args [i],format, x.base .base .loc );
311
+ pass_result.push_back (al, write_stmt);
312
+ pass_result.push_back (al, empty_file_write_endl);
340
313
} else {
341
314
write_body.push_back (format->m_args [i]);
342
315
}
343
316
}
344
317
if (write_body.size () > 0 ) {
345
- write_stmt = create_formatstmt (write_body, format, x.base .base .loc , ASR::stmtType::FileWrite, x. m_unit , x. m_separator , x. m_end );
318
+ write_stmt = create_formatstmt (write_body, format, x.base .base .loc , ASR::stmtType::FileWrite);
346
319
pass_result.push_back (al, write_stmt);
347
320
}
348
321
return ;
349
322
}
350
323
for (size_t i=0 ; i<x.n_values ; i++) {
351
- // DIVERGENCE between LFortran and LPython
352
- // If a pointer array variable is provided
353
- // then it will be printed as a normal array.
354
- if (PassUtils::is_array (x.m_values [i])) {
324
+ // TODO: This will disallow printing array pointer in Fortran
325
+ // Pointers are treated the same as normal variables in Fortran
326
+ // However, LPython prints the address of pointers when you do
327
+ // print(some_pointer). Same goes for C/C++ (if we add their frontends in future).
328
+ // So we need to figure out a way to de-couple printing support from libasr
329
+ // or add nodes according to the frontends because each frontend will have a different
330
+ // way of handling printing of pointers and non-pointers
331
+ if (!ASR::is_a<ASR::Pointer_t>(*ASRUtils::expr_type (x.m_values [i])) &&
332
+ PassUtils::is_array (x.m_values [i])) {
355
333
if (write_body.size () > 0 ) {
356
334
print_args_apart_from_arrays (write_body, x);
357
335
pass_result.push_back (al, empty_file_write_endl);
358
336
}
359
- write_stmt = write_array_using_doloop (x.m_values [i], nullptr , x.m_unit , x. base .base .loc );
337
+ write_stmt = write_array_using_doloop (x.m_values [i], nullptr , x.base .base .loc );
360
338
pass_result.push_back (al, write_stmt);
361
339
pass_result.push_back (al, empty_file_write_endl);
362
340
} else {
0 commit comments