Branch data Line data Source code
1 : : /* Statement translation -- generate GCC trees from gfc_code.
2 : : Copyright (C) 2002-2025 Free Software Foundation, Inc.
3 : : Contributed by Paul Brook <paul@nowt.org>
4 : : and Steven Bosscher <s.bosscher@student.tudelft.nl>
5 : :
6 : : This file is part of GCC.
7 : :
8 : : GCC is free software; you can redistribute it and/or modify it under
9 : : the terms of the GNU General Public License as published by the Free
10 : : Software Foundation; either version 3, or (at your option) any later
11 : : version.
12 : :
13 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 : : for more details.
17 : :
18 : : You should have received a copy of the GNU General Public License
19 : : along with GCC; see the file COPYING3. If not see
20 : : <http://www.gnu.org/licenses/>. */
21 : :
22 : : #define INCLUDE_VECTOR
23 : : #include "config.h"
24 : : #include "system.h"
25 : : #include "coretypes.h"
26 : : #include "options.h"
27 : : #include "tree.h"
28 : : #include "gfortran.h"
29 : : #include "trans.h"
30 : : #include "stringpool.h"
31 : : #include "fold-const.h"
32 : : #include "trans-stmt.h"
33 : : #include "trans-types.h"
34 : : #include "trans-array.h"
35 : : #include "trans-const.h"
36 : : #include "dependency.h"
37 : :
38 : : typedef struct iter_info
39 : : {
40 : : tree var;
41 : : tree start;
42 : : tree end;
43 : : tree step;
44 : : gfc_loop_annot annot;
45 : : struct iter_info *next;
46 : : }
47 : : iter_info;
48 : :
49 : : typedef struct forall_info
50 : : {
51 : : iter_info *this_loop;
52 : : tree mask;
53 : : tree maskindex;
54 : : int nvar;
55 : : tree size;
56 : : struct forall_info *prev_nest;
57 : : bool do_concurrent;
58 : : }
59 : : forall_info;
60 : :
61 : : static void gfc_trans_where_2 (gfc_code *, tree, bool,
62 : : forall_info *, stmtblock_t *);
63 : :
64 : : /* Translate a F95 label number to a LABEL_EXPR. */
65 : :
66 : : tree
67 : 3502 : gfc_trans_label_here (gfc_code * code)
68 : : {
69 : 3502 : return build1_v (LABEL_EXPR, gfc_get_label_decl (code->here));
70 : : }
71 : :
72 : :
73 : : /* Given a variable expression which has been ASSIGNed to, find the decl
74 : : containing the auxiliary variables. For variables in common blocks this
75 : : is a field_decl. */
76 : :
77 : : void
78 : 187 : gfc_conv_label_variable (gfc_se * se, gfc_expr * expr)
79 : : {
80 : 187 : gcc_assert (expr->symtree->n.sym->attr.assign == 1);
81 : 187 : gfc_conv_expr (se, expr);
82 : : /* Deals with variable in common block. Get the field declaration. */
83 : 187 : if (TREE_CODE (se->expr) == COMPONENT_REF)
84 : 0 : se->expr = TREE_OPERAND (se->expr, 1);
85 : : /* Deals with dummy argument. Get the parameter declaration. */
86 : 187 : else if (INDIRECT_REF_P (se->expr))
87 : 12 : se->expr = TREE_OPERAND (se->expr, 0);
88 : 187 : }
89 : :
90 : : /* Translate a label assignment statement. */
91 : :
92 : : tree
93 : 116 : gfc_trans_label_assign (gfc_code * code)
94 : : {
95 : 116 : tree label_tree;
96 : 116 : gfc_se se;
97 : 116 : tree len;
98 : 116 : tree addr;
99 : 116 : tree len_tree;
100 : 116 : int label_len;
101 : :
102 : : /* Start a new block. */
103 : 116 : gfc_init_se (&se, NULL);
104 : 116 : gfc_start_block (&se.pre);
105 : 116 : gfc_conv_label_variable (&se, code->expr1);
106 : :
107 : 116 : len = GFC_DECL_STRING_LEN (se.expr);
108 : 116 : addr = GFC_DECL_ASSIGN_ADDR (se.expr);
109 : :
110 : 116 : label_tree = gfc_get_label_decl (code->label1);
111 : :
112 : 116 : if (code->label1->defined == ST_LABEL_TARGET
113 : 116 : || code->label1->defined == ST_LABEL_DO_TARGET)
114 : : {
115 : 115 : label_tree = gfc_build_addr_expr (pvoid_type_node, label_tree);
116 : 115 : len_tree = build_int_cst (gfc_charlen_type_node, -1);
117 : : }
118 : : else
119 : : {
120 : 1 : gfc_expr *format = code->label1->format;
121 : :
122 : 1 : label_len = format->value.character.length;
123 : 1 : len_tree = build_int_cst (gfc_charlen_type_node, label_len);
124 : 2 : label_tree = gfc_build_wide_string_const (format->ts.kind, label_len + 1,
125 : 1 : format->value.character.string);
126 : 1 : label_tree = gfc_build_addr_expr (pvoid_type_node, label_tree);
127 : : }
128 : :
129 : 116 : gfc_add_modify (&se.pre, len, fold_convert (TREE_TYPE (len), len_tree));
130 : 116 : gfc_add_modify (&se.pre, addr, label_tree);
131 : :
132 : 116 : return gfc_finish_block (&se.pre);
133 : : }
134 : :
135 : : /* Translate a GOTO statement. */
136 : :
137 : : tree
138 : 1187 : gfc_trans_goto (gfc_code * code)
139 : : {
140 : 1187 : locus loc = code->loc;
141 : 1187 : tree assigned_goto;
142 : 1187 : tree target;
143 : 1187 : tree tmp;
144 : 1187 : gfc_se se;
145 : :
146 : 1187 : if (code->label1 != NULL)
147 : 1117 : return build1_v (GOTO_EXPR, gfc_get_label_decl (code->label1));
148 : :
149 : : /* ASSIGNED GOTO. */
150 : 70 : gfc_init_se (&se, NULL);
151 : 70 : gfc_start_block (&se.pre);
152 : 70 : gfc_conv_label_variable (&se, code->expr1);
153 : 70 : tmp = GFC_DECL_STRING_LEN (se.expr);
154 : 70 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
155 : 70 : build_int_cst (TREE_TYPE (tmp), -1));
156 : 70 : gfc_trans_runtime_check (true, false, tmp, &se.pre, &loc,
157 : : "Assigned label is not a target label");
158 : :
159 : 70 : assigned_goto = GFC_DECL_ASSIGN_ADDR (se.expr);
160 : :
161 : : /* We're going to ignore a label list. It does not really change the
162 : : statement's semantics (because it is just a further restriction on
163 : : what's legal code); before, we were comparing label addresses here, but
164 : : that's a very fragile business and may break with optimization. So
165 : : just ignore it. */
166 : :
167 : 70 : target = fold_build1_loc (input_location, GOTO_EXPR, void_type_node,
168 : : assigned_goto);
169 : 70 : gfc_add_expr_to_block (&se.pre, target);
170 : 70 : return gfc_finish_block (&se.pre);
171 : : }
172 : :
173 : :
174 : : /* Translate an ENTRY statement. Just adds a label for this entry point. */
175 : : tree
176 : 1341 : gfc_trans_entry (gfc_code * code)
177 : : {
178 : 1341 : return build1_v (LABEL_EXPR, code->ext.entry->label);
179 : : }
180 : :
181 : :
182 : : /* Replace a gfc_ss structure by another both in the gfc_se struct
183 : : and the gfc_loopinfo struct. This is used in gfc_conv_elemental_dependencies
184 : : to replace a variable ss by the corresponding temporary. */
185 : :
186 : : static void
187 : 343 : replace_ss (gfc_se *se, gfc_ss *old_ss, gfc_ss *new_ss)
188 : : {
189 : 343 : gfc_ss **sess, **loopss;
190 : :
191 : : /* The old_ss is a ss for a single variable. */
192 : 343 : gcc_assert (old_ss->info->type == GFC_SS_SECTION);
193 : :
194 : 490 : for (sess = &(se->ss); *sess != gfc_ss_terminator; sess = &((*sess)->next))
195 : 490 : if (*sess == old_ss)
196 : : break;
197 : 343 : gcc_assert (*sess != gfc_ss_terminator);
198 : :
199 : 343 : *sess = new_ss;
200 : 343 : new_ss->next = old_ss->next;
201 : :
202 : : /* Make sure that trailing references are not lost. */
203 : 343 : if (old_ss->info
204 : 343 : && old_ss->info->data.array.ref
205 : 343 : && old_ss->info->data.array.ref->next
206 : 25 : && !(new_ss->info->data.array.ref
207 : 0 : && new_ss->info->data.array.ref->next))
208 : 25 : new_ss->info->data.array.ref = old_ss->info->data.array.ref;
209 : :
210 : 490 : for (loopss = &(se->loop->ss); *loopss != gfc_ss_terminator;
211 : 147 : loopss = &((*loopss)->loop_chain))
212 : 490 : if (*loopss == old_ss)
213 : : break;
214 : 343 : gcc_assert (*loopss != gfc_ss_terminator);
215 : :
216 : 343 : *loopss = new_ss;
217 : 343 : new_ss->loop_chain = old_ss->loop_chain;
218 : 343 : new_ss->loop = old_ss->loop;
219 : :
220 : 343 : gfc_free_ss (old_ss);
221 : 343 : }
222 : :
223 : :
224 : : /* Check for dependencies between INTENT(IN) and INTENT(OUT) arguments of
225 : : elemental subroutines. Make temporaries for output arguments if any such
226 : : dependencies are found. Output arguments are chosen because internal_unpack
227 : : can be used, as is, to copy the result back to the variable. */
228 : : static void
229 : 2210 : gfc_conv_elemental_dependencies (gfc_se * se, gfc_se * loopse,
230 : : gfc_symbol * sym, gfc_actual_arglist * arg,
231 : : gfc_dep_check check_variable)
232 : : {
233 : 2210 : gfc_actual_arglist *arg0;
234 : 2210 : gfc_expr *e;
235 : 2210 : gfc_formal_arglist *formal;
236 : 2210 : gfc_se parmse;
237 : 2210 : gfc_ss *ss;
238 : 2210 : gfc_symbol *fsym;
239 : 2210 : tree data;
240 : 2210 : tree size;
241 : 2210 : tree tmp;
242 : :
243 : 2210 : if (loopse->ss == NULL)
244 : 0 : return;
245 : :
246 : 2210 : ss = loopse->ss;
247 : 2210 : arg0 = arg;
248 : 2210 : formal = gfc_sym_get_dummy_args (sym);
249 : :
250 : : /* Loop over all the arguments testing for dependencies. */
251 : 11348 : for (; arg != NULL; arg = arg->next, formal = formal ? formal->next : NULL)
252 : : {
253 : 4569 : e = arg->expr;
254 : 4569 : if (e == NULL)
255 : 12 : continue;
256 : :
257 : : /* Obtain the info structure for the current argument. */
258 : 7891 : for (ss = loopse->ss; ss && ss != gfc_ss_terminator; ss = ss->next)
259 : 7625 : if (ss->info->expr == e)
260 : : break;
261 : :
262 : : /* If there is a dependency, create a temporary and use it
263 : : instead of the variable. */
264 : 4557 : fsym = formal ? formal->sym : NULL;
265 : 4557 : if (e->expr_type == EXPR_VARIABLE
266 : 3388 : && e->rank && fsym
267 : 2879 : && fsym->attr.intent != INTENT_IN
268 : 1479 : && !fsym->attr.value
269 : 5976 : && gfc_check_fncall_dependency (e, fsym->attr.intent,
270 : : sym, arg0, check_variable))
271 : : {
272 : 343 : tree initial, temptype;
273 : 343 : stmtblock_t temp_post;
274 : 343 : gfc_ss *tmp_ss;
275 : :
276 : 343 : tmp_ss = gfc_get_array_ss (gfc_ss_terminator, NULL, ss->dimen,
277 : : GFC_SS_SECTION);
278 : 343 : gfc_mark_ss_chain_used (tmp_ss, 1);
279 : 343 : tmp_ss->info->expr = ss->info->expr;
280 : 343 : replace_ss (loopse, ss, tmp_ss);
281 : :
282 : : /* Obtain the argument descriptor for unpacking. */
283 : 343 : gfc_init_se (&parmse, NULL);
284 : 343 : parmse.want_pointer = 1;
285 : 343 : gfc_conv_expr_descriptor (&parmse, e);
286 : 343 : gfc_add_block_to_block (&se->pre, &parmse.pre);
287 : :
288 : : /* If we've got INTENT(INOUT) or a derived type with INTENT(OUT),
289 : : initialize the array temporary with a copy of the values. */
290 : 343 : if (fsym->attr.intent == INTENT_INOUT
291 : 299 : || (fsym->ts.type ==BT_DERIVED
292 : 51 : && fsym->attr.intent == INTENT_OUT))
293 : 95 : initial = parmse.expr;
294 : : /* For class expressions, we always initialize with the copy of
295 : : the values. */
296 : 248 : else if (e->ts.type == BT_CLASS)
297 : 6 : initial = parmse.expr;
298 : : else
299 : : initial = NULL_TREE;
300 : :
301 : 343 : if (e->ts.type != BT_CLASS)
302 : : {
303 : : /* Find the type of the temporary to create; we don't use the type
304 : : of e itself as this breaks for subcomponent-references in e
305 : : (where the type of e is that of the final reference, but
306 : : parmse.expr's type corresponds to the full derived-type). */
307 : : /* TODO: Fix this somehow so we don't need a temporary of the whole
308 : : array but instead only the components referenced. */
309 : 337 : temptype = TREE_TYPE (parmse.expr); /* Pointer to descriptor. */
310 : 337 : gcc_assert (TREE_CODE (temptype) == POINTER_TYPE);
311 : 337 : temptype = TREE_TYPE (temptype);
312 : 337 : temptype = gfc_get_element_type (temptype);
313 : : }
314 : :
315 : : else
316 : : /* For class arrays signal that the size of the dynamic type has to
317 : : be obtained from the vtable, using the 'initial' expression. */
318 : : temptype = NULL_TREE;
319 : :
320 : : /* Generate the temporary. Cleaning up the temporary should be the
321 : : very last thing done, so we add the code to a new block and add it
322 : : to se->post as last instructions. */
323 : 343 : size = gfc_create_var (gfc_array_index_type, NULL);
324 : 343 : data = gfc_create_var (pvoid_type_node, NULL);
325 : 343 : gfc_init_block (&temp_post);
326 : 686 : tmp = gfc_trans_create_temp_array (&se->pre, &temp_post, tmp_ss,
327 : : temptype, initial, false, true,
328 : 343 : false, &arg->expr->where);
329 : 343 : gfc_add_modify (&se->pre, size, tmp);
330 : 343 : tmp = fold_convert (pvoid_type_node, tmp_ss->info->data.array.data);
331 : 343 : gfc_add_modify (&se->pre, data, tmp);
332 : :
333 : : /* Update other ss' delta. */
334 : 343 : gfc_set_delta (loopse->loop);
335 : :
336 : : /* Copy the result back using unpack..... */
337 : 343 : if (e->ts.type != BT_CLASS)
338 : 337 : tmp = build_call_expr_loc (input_location,
339 : : gfor_fndecl_in_unpack, 2, parmse.expr, data);
340 : : else
341 : : {
342 : : /* ... except for class results where the copy is
343 : : unconditional. */
344 : 6 : tmp = build_fold_indirect_ref_loc (input_location, parmse.expr);
345 : 6 : tmp = gfc_conv_descriptor_data_get (tmp);
346 : 6 : tmp = build_call_expr_loc (input_location,
347 : : builtin_decl_explicit (BUILT_IN_MEMCPY),
348 : : 3, tmp, data,
349 : : fold_convert (size_type_node, size));
350 : : }
351 : 343 : gfc_add_expr_to_block (&se->post, tmp);
352 : :
353 : : /* parmse.pre is already added above. */
354 : 343 : gfc_add_block_to_block (&se->post, &parmse.post);
355 : 343 : gfc_add_block_to_block (&se->post, &temp_post);
356 : : }
357 : : }
358 : : }
359 : :
360 : :
361 : : /* Given an executable statement referring to an intrinsic function call,
362 : : returns the intrinsic symbol. */
363 : :
364 : : static gfc_intrinsic_sym *
365 : 6106 : get_intrinsic_for_code (gfc_code *code)
366 : : {
367 : 6106 : if (code->op == EXEC_CALL)
368 : : {
369 : 5763 : gfc_intrinsic_sym * const isym = code->resolved_isym;
370 : 5763 : if (isym)
371 : : return isym;
372 : : else
373 : 5566 : return gfc_get_intrinsic_for_expr (code->expr1);
374 : : }
375 : :
376 : : return NULL;
377 : : }
378 : :
379 : :
380 : : /* Translate the CALL statement. Builds a call to an F95 subroutine. */
381 : :
382 : : tree
383 : 76814 : gfc_trans_call (gfc_code * code, bool dependency_check,
384 : : tree mask, tree count1, bool invert)
385 : : {
386 : 76814 : gfc_se se;
387 : 76814 : gfc_ss * ss;
388 : 76814 : int has_alternate_specifier;
389 : 76814 : gfc_dep_check check_variable;
390 : 76814 : tree index = NULL_TREE;
391 : 76814 : tree maskexpr = NULL_TREE;
392 : 76814 : tree tmp;
393 : 76814 : bool is_intrinsic_mvbits;
394 : :
395 : : /* A CALL starts a new block because the actual arguments may have to
396 : : be evaluated first. */
397 : 76814 : gfc_init_se (&se, NULL);
398 : 76814 : gfc_start_block (&se.pre);
399 : :
400 : 76814 : gcc_assert (code->resolved_sym);
401 : :
402 : 76814 : ss = gfc_ss_terminator;
403 : 76814 : if (code->resolved_sym->attr.elemental)
404 : 6106 : ss = gfc_walk_elemental_function_args (ss, code->ext.actual,
405 : : get_intrinsic_for_code (code),
406 : : GFC_SS_REFERENCE);
407 : :
408 : : /* MVBITS is inlined but needs the dependency checking found here. */
409 : 153628 : is_intrinsic_mvbits = code->resolved_isym
410 : 76814 : && code->resolved_isym->id == GFC_ISYM_MVBITS;
411 : :
412 : : /* Is not an elemental subroutine call with array valued arguments. */
413 : 76814 : if (ss == gfc_ss_terminator)
414 : : {
415 : :
416 : 74604 : if (is_intrinsic_mvbits)
417 : : {
418 : 130 : has_alternate_specifier = 0;
419 : 130 : gfc_conv_intrinsic_mvbits (&se, code->ext.actual, NULL);
420 : : }
421 : : else
422 : : {
423 : : /* Translate the call. */
424 : 74474 : has_alternate_specifier =
425 : 74474 : gfc_conv_procedure_call (&se, code->resolved_sym,
426 : : code->ext.actual, code->expr1, NULL);
427 : :
428 : : /* A subroutine without side-effect, by definition, does nothing! */
429 : 74474 : TREE_SIDE_EFFECTS (se.expr) = 1;
430 : : }
431 : :
432 : : /* Chain the pieces together and return the block. */
433 : 74604 : if (has_alternate_specifier)
434 : : {
435 : 140 : gfc_code *select_code;
436 : 140 : gfc_symbol *sym;
437 : 140 : select_code = code->next;
438 : 140 : gcc_assert(select_code->op == EXEC_SELECT);
439 : 140 : sym = select_code->expr1->symtree->n.sym;
440 : 140 : se.expr = convert (gfc_typenode_for_spec (&sym->ts), se.expr);
441 : 140 : if (sym->backend_decl == NULL)
442 : 1 : sym->backend_decl = gfc_get_symbol_decl (sym);
443 : 140 : gfc_add_modify (&se.pre, sym->backend_decl, se.expr);
444 : : }
445 : : else
446 : 74464 : gfc_add_expr_to_block (&se.pre, se.expr);
447 : :
448 : 74604 : gfc_add_block_to_block (&se.finalblock, &se.post);
449 : 74604 : gfc_add_block_to_block (&se.pre, &se.finalblock);
450 : : }
451 : :
452 : : else
453 : : {
454 : : /* An elemental subroutine call with array valued arguments has
455 : : to be scalarized. */
456 : 2210 : gfc_loopinfo loop;
457 : 2210 : stmtblock_t body;
458 : 2210 : stmtblock_t block;
459 : 2210 : gfc_se loopse;
460 : 2210 : gfc_se depse;
461 : :
462 : : /* gfc_walk_elemental_function_args renders the ss chain in the
463 : : reverse order to the actual argument order. */
464 : 2210 : ss = gfc_reverse_ss (ss);
465 : :
466 : : /* Initialize the loop. */
467 : 2210 : gfc_init_se (&loopse, NULL);
468 : 2210 : gfc_init_loopinfo (&loop);
469 : 2210 : gfc_add_ss_to_loop (&loop, ss);
470 : :
471 : 2210 : gfc_conv_ss_startstride (&loop);
472 : : /* TODO: gfc_conv_loop_setup generates a temporary for vector
473 : : subscripts. This could be prevented in the elemental case
474 : : as temporaries are handled separately
475 : : (below in gfc_conv_elemental_dependencies). */
476 : 2210 : if (code->expr1)
477 : 237 : gfc_conv_loop_setup (&loop, &code->expr1->where);
478 : : else
479 : 1973 : gfc_conv_loop_setup (&loop, &code->loc);
480 : :
481 : 2210 : gfc_mark_ss_chain_used (ss, 1);
482 : :
483 : : /* Convert the arguments, checking for dependencies. */
484 : 2210 : gfc_copy_loopinfo_to_se (&loopse, &loop);
485 : 2210 : loopse.ss = ss;
486 : :
487 : : /* For operator assignment, do dependency checking. */
488 : 2210 : if (dependency_check)
489 : : check_variable = ELEM_CHECK_VARIABLE;
490 : : else
491 : 1886 : check_variable = ELEM_DONT_CHECK_VARIABLE;
492 : :
493 : 2210 : gfc_init_se (&depse, NULL);
494 : 2210 : gfc_conv_elemental_dependencies (&depse, &loopse, code->resolved_sym,
495 : : code->ext.actual, check_variable);
496 : :
497 : 2210 : gfc_add_block_to_block (&loop.pre, &depse.pre);
498 : 2210 : gfc_add_block_to_block (&loop.post, &depse.post);
499 : :
500 : : /* Generate the loop body. */
501 : 2210 : gfc_start_scalarized_body (&loop, &body);
502 : 2210 : gfc_init_block (&block);
503 : :
504 : 2210 : if (mask && count1)
505 : : {
506 : : /* Form the mask expression according to the mask. */
507 : 44 : index = count1;
508 : 44 : maskexpr = gfc_build_array_ref (mask, index, NULL);
509 : 44 : if (invert)
510 : 11 : maskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
511 : 11 : TREE_TYPE (maskexpr), maskexpr);
512 : : }
513 : :
514 : 2210 : if (is_intrinsic_mvbits)
515 : : {
516 : 67 : has_alternate_specifier = 0;
517 : 67 : gfc_conv_intrinsic_mvbits (&loopse, code->ext.actual, &loop);
518 : : }
519 : : else
520 : : {
521 : : /* Add the subroutine call to the block. */
522 : 2143 : gfc_conv_procedure_call (&loopse, code->resolved_sym,
523 : : code->ext.actual, code->expr1,
524 : : NULL);
525 : : }
526 : :
527 : 2210 : if (mask && count1)
528 : : {
529 : 44 : tmp = build3_v (COND_EXPR, maskexpr, loopse.expr,
530 : : build_empty_stmt (input_location));
531 : 44 : gfc_add_expr_to_block (&loopse.pre, tmp);
532 : 44 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
533 : : gfc_array_index_type,
534 : : count1, gfc_index_one_node);
535 : 44 : gfc_add_modify (&loopse.pre, count1, tmp);
536 : : }
537 : : else
538 : 2166 : gfc_add_expr_to_block (&loopse.pre, loopse.expr);
539 : :
540 : 2210 : gfc_add_block_to_block (&block, &loopse.pre);
541 : 2210 : gfc_add_block_to_block (&block, &loopse.post);
542 : :
543 : : /* Finish up the loop block and the loop. */
544 : 2210 : gfc_add_expr_to_block (&body, gfc_finish_block (&block));
545 : 2210 : gfc_trans_scalarizing_loops (&loop, &body);
546 : 2210 : gfc_add_block_to_block (&se.pre, &loop.pre);
547 : 2210 : gfc_add_block_to_block (&se.pre, &loop.post);
548 : 2210 : gfc_add_block_to_block (&se.pre, &loopse.finalblock);
549 : 2210 : gfc_add_block_to_block (&se.pre, &se.post);
550 : 2210 : gfc_cleanup_loop (&loop);
551 : : }
552 : :
553 : 76814 : return gfc_finish_block (&se.pre);
554 : : }
555 : :
556 : :
557 : : /* Translate the RETURN statement. */
558 : :
559 : : tree
560 : 3035 : gfc_trans_return (gfc_code * code)
561 : : {
562 : 3035 : if (code->expr1)
563 : : {
564 : 50 : gfc_se se;
565 : 50 : tree tmp;
566 : 50 : tree result;
567 : :
568 : : /* If code->expr is not NULL, this return statement must appear
569 : : in a subroutine and current_fake_result_decl has already
570 : : been generated. */
571 : :
572 : 50 : result = gfc_get_fake_result_decl (NULL, 0);
573 : 50 : if (!result)
574 : : {
575 : 0 : gfc_warning (0,
576 : : "An alternate return at %L without a * dummy argument",
577 : 0 : &code->expr1->where);
578 : 0 : return gfc_generate_return ();
579 : : }
580 : :
581 : : /* Start a new block for this statement. */
582 : 50 : gfc_init_se (&se, NULL);
583 : 50 : gfc_start_block (&se.pre);
584 : :
585 : 50 : gfc_conv_expr (&se, code->expr1);
586 : :
587 : : /* Note that the actually returned expression is a simple value and
588 : : does not depend on any pointers or such; thus we can clean-up with
589 : : se.post before returning. */
590 : 50 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (result),
591 : 50 : result, fold_convert (TREE_TYPE (result),
592 : : se.expr));
593 : 50 : gfc_add_expr_to_block (&se.pre, tmp);
594 : 50 : gfc_add_block_to_block (&se.pre, &se.post);
595 : :
596 : 50 : tmp = gfc_generate_return ();
597 : 50 : gfc_add_expr_to_block (&se.pre, tmp);
598 : 50 : return gfc_finish_block (&se.pre);
599 : : }
600 : :
601 : 2985 : return gfc_generate_return ();
602 : : }
603 : :
604 : :
605 : : /* Translate the PAUSE statement. We have to translate this statement
606 : : to a runtime library call. */
607 : :
608 : : tree
609 : 28 : gfc_trans_pause (gfc_code * code)
610 : : {
611 : 28 : tree gfc_int8_type_node = gfc_get_int_type (8);
612 : 28 : gfc_se se;
613 : 28 : tree tmp;
614 : :
615 : : /* Start a new block for this statement. */
616 : 28 : gfc_init_se (&se, NULL);
617 : 28 : gfc_start_block (&se.pre);
618 : :
619 : :
620 : 28 : if (code->expr1 == NULL)
621 : : {
622 : 10 : tmp = build_int_cst (size_type_node, 0);
623 : 10 : tmp = build_call_expr_loc (input_location,
624 : : gfor_fndecl_pause_string, 2,
625 : : build_int_cst (pchar_type_node, 0), tmp);
626 : : }
627 : 18 : else if (code->expr1->ts.type == BT_INTEGER)
628 : : {
629 : 9 : gfc_conv_expr (&se, code->expr1);
630 : 9 : tmp = build_call_expr_loc (input_location,
631 : : gfor_fndecl_pause_numeric, 1,
632 : : fold_convert (gfc_int8_type_node, se.expr));
633 : : }
634 : : else
635 : : {
636 : 9 : gfc_conv_expr_reference (&se, code->expr1);
637 : 9 : tmp = build_call_expr_loc (input_location,
638 : : gfor_fndecl_pause_string, 2,
639 : : se.expr, fold_convert (size_type_node,
640 : : se.string_length));
641 : : }
642 : :
643 : 28 : gfc_add_expr_to_block (&se.pre, tmp);
644 : :
645 : 28 : gfc_add_block_to_block (&se.pre, &se.post);
646 : :
647 : 28 : return gfc_finish_block (&se.pre);
648 : : }
649 : :
650 : :
651 : : /* Translate the STOP statement. We have to translate this statement
652 : : to a runtime library call. */
653 : :
654 : : tree
655 : 211126 : gfc_trans_stop (gfc_code *code, bool error_stop)
656 : : {
657 : 211126 : gfc_se se;
658 : 211126 : tree tmp;
659 : 211126 : tree quiet;
660 : :
661 : : /* Start a new block for this statement. */
662 : 211126 : gfc_init_se (&se, NULL);
663 : 211126 : gfc_start_block (&se.pre);
664 : :
665 : 211126 : if (code->expr2)
666 : : {
667 : 25 : gfc_conv_expr_val (&se, code->expr2);
668 : 25 : quiet = fold_convert (boolean_type_node, se.expr);
669 : : }
670 : : else
671 : 211101 : quiet = boolean_false_node;
672 : :
673 : 211126 : if (code->expr1 == NULL)
674 : : {
675 : 20383 : tmp = build_int_cst (size_type_node, 0);
676 : 40766 : tmp = build_call_expr_loc (input_location,
677 : : error_stop
678 : 19455 : ? (flag_coarray == GFC_FCOARRAY_LIB
679 : 19455 : ? gfor_fndecl_caf_error_stop_str
680 : : : gfor_fndecl_error_stop_string)
681 : 928 : : (flag_coarray == GFC_FCOARRAY_LIB
682 : 928 : ? gfor_fndecl_caf_stop_str
683 : : : gfor_fndecl_stop_string),
684 : : 3, build_int_cst (pchar_type_node, 0), tmp,
685 : : quiet);
686 : : }
687 : 190743 : else if (code->expr1->ts.type == BT_INTEGER)
688 : : {
689 : 190344 : gfc_conv_expr (&se, code->expr1);
690 : 380688 : tmp = build_call_expr_loc (input_location,
691 : : error_stop
692 : 19024 : ? (flag_coarray == GFC_FCOARRAY_LIB
693 : 19024 : ? gfor_fndecl_caf_error_stop
694 : : : gfor_fndecl_error_stop_numeric)
695 : 171320 : : (flag_coarray == GFC_FCOARRAY_LIB
696 : 171320 : ? gfor_fndecl_caf_stop_numeric
697 : : : gfor_fndecl_stop_numeric), 2,
698 : : fold_convert (integer_type_node, se.expr),
699 : : quiet);
700 : : }
701 : : else
702 : : {
703 : 399 : gfc_conv_expr_reference (&se, code->expr1);
704 : 798 : tmp = build_call_expr_loc (input_location,
705 : : error_stop
706 : 291 : ? (flag_coarray == GFC_FCOARRAY_LIB
707 : 291 : ? gfor_fndecl_caf_error_stop_str
708 : : : gfor_fndecl_error_stop_string)
709 : 108 : : (flag_coarray == GFC_FCOARRAY_LIB
710 : 108 : ? gfor_fndecl_caf_stop_str
711 : : : gfor_fndecl_stop_string),
712 : : 3, se.expr, fold_convert (size_type_node,
713 : : se.string_length),
714 : : quiet);
715 : : }
716 : :
717 : 211126 : gfc_add_expr_to_block (&se.pre, tmp);
718 : :
719 : 211126 : gfc_add_block_to_block (&se.pre, &se.post);
720 : :
721 : 211126 : return gfc_finish_block (&se.pre);
722 : : }
723 : :
724 : : tree
725 : 52 : trans_exit ()
726 : : {
727 : 52 : const char *name = gfc_get_string (PREFIX ("exit_i%d"), 4);
728 : 52 : gfc_symbol *exsym = gfc_get_intrinsic_sub_symbol (name);
729 : 52 : tree tmp = gfc_get_symbol_decl (exsym);
730 : 52 : return build_call_expr_loc (input_location, tmp, 1, integer_zero_node);
731 : : }
732 : :
733 : : /* Translate the FAIL IMAGE statement. */
734 : :
735 : : tree
736 : 3 : gfc_trans_fail_image (gfc_code *code ATTRIBUTE_UNUSED)
737 : : {
738 : 3 : if (flag_coarray == GFC_FCOARRAY_LIB)
739 : 2 : return build_call_expr_loc (input_location,
740 : 2 : gfor_fndecl_caf_fail_image, 0);
741 : : else
742 : 1 : return trans_exit ();
743 : : }
744 : :
745 : : void
746 : 399 : gfc_trans_sync_stat (struct sync_stat *sync_stat, gfc_se *se, tree *stat,
747 : : tree *errmsg, tree *errmsg_len)
748 : : {
749 : 399 : gfc_se argse;
750 : :
751 : 399 : if (sync_stat->stat)
752 : : {
753 : 72 : gfc_init_se (&argse, NULL);
754 : 72 : gfc_conv_expr (&argse, sync_stat->stat);
755 : 72 : gfc_add_block_to_block (&se->pre, &argse.pre);
756 : :
757 : 72 : if (TREE_TYPE (argse.expr) != integer_type_node)
758 : : {
759 : 6 : tree tstat = gfc_create_var (integer_type_node, "stat");
760 : 6 : TREE_THIS_VOLATILE (tstat) = 1;
761 : 6 : gfc_add_modify (&se->pre, tstat,
762 : : fold_convert (integer_type_node, argse.expr));
763 : 6 : gfc_add_modify (&se->post, argse.expr,
764 : 6 : fold_convert (TREE_TYPE (argse.expr), tstat));
765 : 6 : *stat = build_fold_addr_expr (tstat);
766 : : }
767 : : else
768 : 66 : *stat = build_fold_addr_expr (argse.expr);
769 : : }
770 : : else
771 : 327 : *stat = null_pointer_node;
772 : :
773 : 399 : if (sync_stat->errmsg)
774 : : {
775 : 42 : gfc_init_se (&argse, NULL);
776 : 42 : gfc_conv_expr_reference (&argse, sync_stat->errmsg);
777 : 42 : gfc_add_block_to_block (&se->pre, &argse.pre);
778 : 42 : *errmsg = argse.expr;
779 : 42 : *errmsg_len = fold_convert (size_type_node, argse.string_length);
780 : : }
781 : : else
782 : : {
783 : 357 : *errmsg = null_pointer_node;
784 : 357 : *errmsg_len = build_zero_cst (size_type_node);
785 : : }
786 : 399 : }
787 : :
788 : : /* Translate the FORM TEAM statement. */
789 : :
790 : : tree
791 : 96 : gfc_trans_form_team (gfc_code *code)
792 : : {
793 : 96 : if (flag_coarray == GFC_FCOARRAY_LIB)
794 : : {
795 : 64 : gfc_se se, argse;
796 : 64 : tree team_id, team_type, new_index, stat, errmsg, errmsg_len, tmp;
797 : :
798 : 64 : gfc_init_se (&se, NULL);
799 : 64 : gfc_init_se (&argse, NULL);
800 : :
801 : 64 : gfc_conv_expr_val (&argse, code->expr1);
802 : 64 : team_id = fold_convert (integer_type_node, argse.expr);
803 : 64 : gfc_conv_expr_reference (&argse, code->expr2);
804 : 64 : team_type = argse.expr;
805 : :
806 : : /* NEW_INDEX=. */
807 : 64 : if (code->expr3)
808 : : {
809 : 25 : gfc_conv_expr_reference (&argse, code->expr3);
810 : 25 : new_index = argse.expr;
811 : : }
812 : : else
813 : 39 : new_index = null_pointer_node;
814 : :
815 : 64 : gfc_add_block_to_block (&se.post, &argse.post);
816 : :
817 : 64 : gfc_trans_sync_stat (&code->ext.sync_stat, &se, &stat, &errmsg,
818 : : &errmsg_len);
819 : :
820 : 64 : gfc_add_block_to_block (&se.pre, &argse.pre);
821 : :
822 : 64 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_form_team, 6,
823 : : team_id, team_type, new_index, stat, errmsg,
824 : : errmsg_len);
825 : 64 : gfc_add_expr_to_block (&se.pre, tmp);
826 : 64 : gfc_add_block_to_block (&se.pre, &se.post);
827 : 64 : return gfc_finish_block (&se.pre);
828 : : }
829 : : else
830 : 32 : return trans_exit ();
831 : : }
832 : :
833 : : /* Translate the CHANGE TEAM statement. */
834 : :
835 : : tree
836 : 51 : gfc_trans_change_team (gfc_code *code)
837 : : {
838 : 51 : if (flag_coarray == GFC_FCOARRAY_LIB)
839 : : {
840 : 32 : stmtblock_t block;
841 : 32 : gfc_se se;
842 : 32 : tree team_type, stat, errmsg, errmsg_len, tmp;
843 : :
844 : 32 : gfc_init_se (&se, NULL);
845 : 32 : gfc_start_block (&block);
846 : :
847 : 32 : gfc_conv_expr_val (&se, code->expr1);
848 : 32 : team_type = se.expr;
849 : :
850 : 32 : gfc_trans_sync_stat (&code->ext.block.sync_stat, &se, &stat, &errmsg,
851 : : &errmsg_len);
852 : :
853 : 32 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_change_team, 4,
854 : : team_type, stat, errmsg, errmsg_len);
855 : :
856 : 32 : gfc_add_expr_to_block (&se.pre, tmp);
857 : 32 : gfc_add_block_to_block (&se.pre, &se.post);
858 : 32 : gfc_add_block_to_block (&block, &se.pre);
859 : 32 : gfc_add_expr_to_block (&block, gfc_trans_block_construct (code));
860 : 32 : return gfc_finish_block (&block);
861 : : }
862 : : else
863 : 19 : return trans_exit ();
864 : : }
865 : :
866 : : /* Translate the END TEAM statement. */
867 : :
868 : : tree
869 : 32 : gfc_trans_end_team (gfc_code *code)
870 : : {
871 : 32 : if (flag_coarray == GFC_FCOARRAY_LIB)
872 : : {
873 : 32 : gfc_se se;
874 : 32 : tree stat, errmsg, errmsg_len, tmp;
875 : :
876 : 32 : gfc_init_se (&se, NULL);
877 : 32 : gfc_start_block (&se.pre);
878 : :
879 : 32 : gfc_trans_sync_stat (&code->ext.sync_stat, &se, &stat, &errmsg,
880 : : &errmsg_len);
881 : :
882 : 32 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_end_team, 3,
883 : : stat, errmsg, errmsg_len);
884 : 32 : gfc_add_expr_to_block (&se.pre, tmp);
885 : 32 : gfc_add_block_to_block (&se.pre, &se.post);
886 : 32 : return gfc_finish_block (&se.pre);
887 : : }
888 : : else
889 : 0 : return trans_exit ();
890 : : }
891 : :
892 : : /* Translate the SYNC TEAM statement. */
893 : :
894 : : tree
895 : 18 : gfc_trans_sync_team (gfc_code *code)
896 : : {
897 : 18 : if (flag_coarray == GFC_FCOARRAY_LIB)
898 : : {
899 : 18 : gfc_se se;
900 : 18 : tree team_type, stat, errmsg, errmsg_len, tmp;
901 : :
902 : 18 : gfc_init_se (&se, NULL);
903 : :
904 : 18 : gfc_conv_expr_val (&se, code->expr1);
905 : 18 : team_type = se.expr;
906 : :
907 : 18 : gfc_trans_sync_stat (&code->ext.sync_stat, &se, &stat, &errmsg,
908 : : &errmsg_len);
909 : :
910 : 18 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_team, 4,
911 : : team_type, stat, errmsg, errmsg_len);
912 : 18 : gfc_add_expr_to_block (&se.pre, tmp);
913 : 18 : gfc_add_block_to_block (&se.pre, &se.post);
914 : 18 : return gfc_finish_block (&se.pre);
915 : : }
916 : : else
917 : 0 : return trans_exit ();
918 : : }
919 : :
920 : : tree
921 : 84 : gfc_trans_lock_unlock (gfc_code *code, gfc_exec_op op)
922 : : {
923 : 84 : gfc_se se, argse;
924 : 84 : tree stat = NULL_TREE, stat2 = NULL_TREE;
925 : 84 : tree lock_acquired = NULL_TREE, lock_acquired2 = NULL_TREE;
926 : :
927 : : /* Short cut: For single images without STAT= or LOCK_ACQUIRED
928 : : return early. (ERRMSG= is always untouched for -fcoarray=single.) */
929 : 84 : if (!code->expr2 && !code->expr4 && flag_coarray != GFC_FCOARRAY_LIB)
930 : : return NULL_TREE;
931 : :
932 : 66 : if (code->expr2)
933 : : {
934 : 28 : gcc_assert (code->expr2->expr_type == EXPR_VARIABLE);
935 : 28 : gfc_init_se (&argse, NULL);
936 : 28 : gfc_conv_expr_val (&argse, code->expr2);
937 : 28 : stat = argse.expr;
938 : : }
939 : 38 : else if (flag_coarray == GFC_FCOARRAY_LIB)
940 : 32 : stat = null_pointer_node;
941 : :
942 : 66 : if (code->expr4)
943 : : {
944 : 14 : gcc_assert (code->expr4->expr_type == EXPR_VARIABLE);
945 : 14 : gfc_init_se (&argse, NULL);
946 : 14 : gfc_conv_expr_val (&argse, code->expr4);
947 : 14 : lock_acquired = argse.expr;
948 : : }
949 : 52 : else if (flag_coarray == GFC_FCOARRAY_LIB)
950 : 40 : lock_acquired = null_pointer_node;
951 : :
952 : 66 : gfc_start_block (&se.pre);
953 : 66 : if (flag_coarray == GFC_FCOARRAY_LIB)
954 : : {
955 : 48 : tree tmp, token, image_index, errmsg, errmsg_len;
956 : 48 : tree index = build_zero_cst (gfc_array_index_type);
957 : 48 : tree caf_decl = gfc_get_tree_for_caf_expr (code->expr1);
958 : :
959 : 48 : if (code->expr1->symtree->n.sym->ts.type != BT_DERIVED
960 : 48 : || code->expr1->symtree->n.sym->ts.u.derived->from_intmod
961 : : != INTMOD_ISO_FORTRAN_ENV
962 : 44 : || code->expr1->symtree->n.sym->ts.u.derived->intmod_sym_id
963 : : != ISOFORTRAN_LOCK_TYPE)
964 : : {
965 : 4 : gfc_error ("Sorry, the lock component of derived type at %L is not "
966 : : "yet supported", &code->expr1->where);
967 : 4 : return NULL_TREE;
968 : : }
969 : :
970 : 44 : gfc_get_caf_token_offset (&se, &token, NULL, caf_decl, NULL_TREE,
971 : : code->expr1);
972 : :
973 : 44 : if (gfc_is_coindexed (code->expr1))
974 : 16 : image_index = gfc_caf_get_image_index (&se.pre, code->expr1, caf_decl);
975 : : else
976 : 28 : image_index = integer_zero_node;
977 : :
978 : : /* For arrays, obtain the array index. */
979 : 44 : if (gfc_expr_attr (code->expr1).dimension)
980 : : {
981 : 28 : tree desc, tmp, extent, lbound, ubound;
982 : 28 : gfc_array_ref *ar, ar2;
983 : 28 : int i, rank;
984 : :
985 : : /* TODO: Extend this, once DT components are supported. */
986 : 28 : ar = &code->expr1->ref->u.ar;
987 : 28 : ar2 = *ar;
988 : 28 : memset (ar, '\0', sizeof (*ar));
989 : 28 : ar->as = ar2.as;
990 : 28 : ar->type = AR_FULL;
991 : 28 : rank = code->expr1->rank;
992 : 28 : code->expr1->rank = ar->as->rank;
993 : :
994 : 28 : gfc_init_se (&argse, NULL);
995 : 28 : argse.descriptor_only = 1;
996 : 28 : gfc_conv_expr_descriptor (&argse, code->expr1);
997 : 28 : gfc_add_block_to_block (&se.pre, &argse.pre);
998 : 28 : desc = argse.expr;
999 : 28 : *ar = ar2;
1000 : 28 : code->expr1->rank = rank;
1001 : :
1002 : 28 : extent = build_one_cst (gfc_array_index_type);
1003 : 98 : for (i = 0; i < ar->dimen; i++)
1004 : : {
1005 : 42 : gfc_init_se (&argse, NULL);
1006 : 42 : gfc_conv_expr_type (&argse, ar->start[i], gfc_array_index_type);
1007 : 42 : gfc_add_block_to_block (&argse.pre, &argse.pre);
1008 : 42 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
1009 : 42 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
1010 : 42 : TREE_TYPE (lbound), argse.expr, lbound);
1011 : 42 : tmp = fold_build2_loc (input_location, MULT_EXPR,
1012 : 42 : TREE_TYPE (tmp), extent, tmp);
1013 : 42 : index = fold_build2_loc (input_location, PLUS_EXPR,
1014 : 42 : TREE_TYPE (tmp), index, tmp);
1015 : 42 : if (i < ar->dimen - 1)
1016 : : {
1017 : 14 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
1018 : 14 : tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
1019 : 14 : extent = fold_build2_loc (input_location, MULT_EXPR,
1020 : 14 : TREE_TYPE (tmp), extent, tmp);
1021 : : }
1022 : : }
1023 : : }
1024 : :
1025 : : /* errmsg. */
1026 : 44 : if (code->expr3)
1027 : : {
1028 : 0 : gfc_init_se (&argse, NULL);
1029 : 0 : argse.want_pointer = 1;
1030 : 0 : gfc_conv_expr (&argse, code->expr3);
1031 : 0 : gfc_add_block_to_block (&se.pre, &argse.pre);
1032 : 0 : errmsg = argse.expr;
1033 : 0 : errmsg_len = fold_convert (size_type_node, argse.string_length);
1034 : : }
1035 : : else
1036 : : {
1037 : 44 : errmsg = null_pointer_node;
1038 : 44 : errmsg_len = build_zero_cst (size_type_node);
1039 : : }
1040 : :
1041 : 44 : if (stat != null_pointer_node && TREE_TYPE (stat) != integer_type_node)
1042 : : {
1043 : 0 : stat2 = stat;
1044 : 0 : stat = gfc_create_var (integer_type_node, "stat");
1045 : : }
1046 : :
1047 : 44 : if (lock_acquired != null_pointer_node
1048 : 44 : && TREE_TYPE (lock_acquired) != integer_type_node)
1049 : : {
1050 : 8 : lock_acquired2 = lock_acquired;
1051 : 8 : lock_acquired = gfc_create_var (integer_type_node, "acquired");
1052 : : }
1053 : :
1054 : 44 : index = fold_convert (size_type_node, index);
1055 : 44 : if (op == EXEC_LOCK)
1056 : 22 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_lock, 7,
1057 : : token, index, image_index,
1058 : 22 : lock_acquired != null_pointer_node
1059 : 8 : ? gfc_build_addr_expr (NULL, lock_acquired)
1060 : : : lock_acquired,
1061 : 22 : stat != null_pointer_node
1062 : 8 : ? gfc_build_addr_expr (NULL, stat) : stat,
1063 : : errmsg, errmsg_len);
1064 : : else
1065 : 22 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_unlock, 6,
1066 : : token, index, image_index,
1067 : 22 : stat != null_pointer_node
1068 : 8 : ? gfc_build_addr_expr (NULL, stat) : stat,
1069 : : errmsg, errmsg_len);
1070 : 44 : gfc_add_expr_to_block (&se.pre, tmp);
1071 : :
1072 : : /* It guarantees memory consistency within the same segment */
1073 : 44 : tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
1074 : 44 : tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1075 : : gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1076 : : tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1077 : 44 : ASM_VOLATILE_P (tmp) = 1;
1078 : :
1079 : 44 : gfc_add_expr_to_block (&se.pre, tmp);
1080 : :
1081 : 44 : if (stat2 != NULL_TREE)
1082 : 0 : gfc_add_modify (&se.pre, stat2,
1083 : 0 : fold_convert (TREE_TYPE (stat2), stat));
1084 : :
1085 : 44 : if (lock_acquired2 != NULL_TREE)
1086 : 8 : gfc_add_modify (&se.pre, lock_acquired2,
1087 : 8 : fold_convert (TREE_TYPE (lock_acquired2),
1088 : : lock_acquired));
1089 : :
1090 : 44 : return gfc_finish_block (&se.pre);
1091 : : }
1092 : :
1093 : 18 : if (stat != NULL_TREE)
1094 : 12 : gfc_add_modify (&se.pre, stat, build_int_cst (TREE_TYPE (stat), 0));
1095 : :
1096 : 18 : if (lock_acquired != NULL_TREE)
1097 : 6 : gfc_add_modify (&se.pre, lock_acquired,
1098 : 6 : fold_convert (TREE_TYPE (lock_acquired),
1099 : : boolean_true_node));
1100 : :
1101 : 18 : return gfc_finish_block (&se.pre);
1102 : : }
1103 : :
1104 : : tree
1105 : 39 : gfc_trans_event_post_wait (gfc_code *code, gfc_exec_op op)
1106 : : {
1107 : 39 : gfc_se se, argse;
1108 : 39 : tree stat = NULL_TREE, stat2 = NULL_TREE;
1109 : 39 : tree until_count = NULL_TREE;
1110 : :
1111 : 39 : if (code->expr2)
1112 : : {
1113 : 8 : gcc_assert (code->expr2->expr_type == EXPR_VARIABLE);
1114 : 8 : gfc_init_se (&argse, NULL);
1115 : 8 : gfc_conv_expr_val (&argse, code->expr2);
1116 : 8 : stat = argse.expr;
1117 : : }
1118 : 31 : else if (flag_coarray == GFC_FCOARRAY_LIB)
1119 : 16 : stat = null_pointer_node;
1120 : :
1121 : 39 : if (code->expr4)
1122 : : {
1123 : 12 : gfc_init_se (&argse, NULL);
1124 : 12 : gfc_conv_expr_val (&argse, code->expr4);
1125 : 12 : until_count = fold_convert (integer_type_node, argse.expr);
1126 : : }
1127 : : else
1128 : 27 : until_count = integer_one_node;
1129 : :
1130 : 39 : if (flag_coarray != GFC_FCOARRAY_LIB)
1131 : : {
1132 : 19 : gfc_start_block (&se.pre);
1133 : 19 : gfc_init_se (&argse, NULL);
1134 : 19 : gfc_conv_expr_val (&argse, code->expr1);
1135 : :
1136 : 19 : if (op == EXEC_EVENT_POST)
1137 : 22 : gfc_add_modify (&se.pre, argse.expr,
1138 : : fold_build2_loc (input_location, PLUS_EXPR,
1139 : 11 : TREE_TYPE (argse.expr), argse.expr,
1140 : 11 : build_int_cst (TREE_TYPE (argse.expr), 1)));
1141 : : else
1142 : 16 : gfc_add_modify (&se.pre, argse.expr,
1143 : : fold_build2_loc (input_location, MINUS_EXPR,
1144 : 8 : TREE_TYPE (argse.expr), argse.expr,
1145 : 8 : fold_convert (TREE_TYPE (argse.expr),
1146 : : until_count)));
1147 : 19 : if (stat != NULL_TREE)
1148 : 4 : gfc_add_modify (&se.pre, stat, build_int_cst (TREE_TYPE (stat), 0));
1149 : :
1150 : 19 : return gfc_finish_block (&se.pre);
1151 : : }
1152 : :
1153 : 20 : gfc_start_block (&se.pre);
1154 : 20 : tree tmp, token, image_index, errmsg, errmsg_len;
1155 : 20 : tree index = build_zero_cst (gfc_array_index_type);
1156 : 20 : tree caf_decl = gfc_get_tree_for_caf_expr (code->expr1);
1157 : :
1158 : 20 : if (code->expr1->symtree->n.sym->ts.type != BT_DERIVED
1159 : 20 : || code->expr1->symtree->n.sym->ts.u.derived->from_intmod
1160 : : != INTMOD_ISO_FORTRAN_ENV
1161 : 20 : || code->expr1->symtree->n.sym->ts.u.derived->intmod_sym_id
1162 : : != ISOFORTRAN_EVENT_TYPE)
1163 : : {
1164 : 0 : gfc_error ("Sorry, the event component of derived type at %L is not "
1165 : : "yet supported", &code->expr1->where);
1166 : 0 : return NULL_TREE;
1167 : : }
1168 : :
1169 : 20 : gfc_init_se (&argse, NULL);
1170 : 20 : gfc_get_caf_token_offset (&argse, &token, NULL, caf_decl, NULL_TREE,
1171 : : code->expr1);
1172 : 20 : gfc_add_block_to_block (&se.pre, &argse.pre);
1173 : :
1174 : 20 : if (gfc_is_coindexed (code->expr1))
1175 : 6 : image_index = gfc_caf_get_image_index (&se.pre, code->expr1, caf_decl);
1176 : : else
1177 : 14 : image_index = integer_zero_node;
1178 : :
1179 : : /* For arrays, obtain the array index. */
1180 : 20 : if (gfc_expr_attr (code->expr1).dimension)
1181 : : {
1182 : 7 : tree desc, tmp, extent, lbound, ubound;
1183 : 7 : gfc_array_ref *ar, ar2;
1184 : 7 : int i;
1185 : :
1186 : : /* TODO: Extend this, once DT components are supported. */
1187 : 7 : ar = &code->expr1->ref->u.ar;
1188 : 7 : ar2 = *ar;
1189 : 7 : memset (ar, '\0', sizeof (*ar));
1190 : 7 : ar->as = ar2.as;
1191 : 7 : ar->type = AR_FULL;
1192 : :
1193 : 7 : gfc_init_se (&argse, NULL);
1194 : 7 : argse.descriptor_only = 1;
1195 : 7 : gfc_conv_expr_descriptor (&argse, code->expr1);
1196 : 7 : gfc_add_block_to_block (&se.pre, &argse.pre);
1197 : 7 : desc = argse.expr;
1198 : 7 : *ar = ar2;
1199 : :
1200 : 7 : extent = build_one_cst (gfc_array_index_type);
1201 : 21 : for (i = 0; i < ar->dimen; i++)
1202 : : {
1203 : 7 : gfc_init_se (&argse, NULL);
1204 : 7 : gfc_conv_expr_type (&argse, ar->start[i], gfc_array_index_type);
1205 : 7 : gfc_add_block_to_block (&argse.pre, &argse.pre);
1206 : 7 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
1207 : 7 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
1208 : 7 : TREE_TYPE (lbound), argse.expr, lbound);
1209 : 7 : tmp = fold_build2_loc (input_location, MULT_EXPR,
1210 : 7 : TREE_TYPE (tmp), extent, tmp);
1211 : 7 : index = fold_build2_loc (input_location, PLUS_EXPR,
1212 : 7 : TREE_TYPE (tmp), index, tmp);
1213 : 7 : if (i < ar->dimen - 1)
1214 : : {
1215 : 0 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
1216 : 0 : tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
1217 : 0 : extent = fold_build2_loc (input_location, MULT_EXPR,
1218 : 0 : TREE_TYPE (tmp), extent, tmp);
1219 : : }
1220 : : }
1221 : : }
1222 : :
1223 : : /* errmsg. */
1224 : 20 : if (code->expr3)
1225 : : {
1226 : 0 : gfc_init_se (&argse, NULL);
1227 : 0 : argse.want_pointer = 1;
1228 : 0 : gfc_conv_expr (&argse, code->expr3);
1229 : 0 : gfc_add_block_to_block (&se.pre, &argse.pre);
1230 : 0 : errmsg = argse.expr;
1231 : 0 : errmsg_len = fold_convert (size_type_node, argse.string_length);
1232 : : }
1233 : : else
1234 : : {
1235 : 20 : errmsg = null_pointer_node;
1236 : 20 : errmsg_len = build_zero_cst (size_type_node);
1237 : : }
1238 : :
1239 : 20 : if (stat != null_pointer_node && TREE_TYPE (stat) != integer_type_node)
1240 : : {
1241 : 0 : stat2 = stat;
1242 : 0 : stat = gfc_create_var (integer_type_node, "stat");
1243 : : }
1244 : :
1245 : 20 : index = fold_convert (size_type_node, index);
1246 : 20 : if (op == EXEC_EVENT_POST)
1247 : 12 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_event_post, 6,
1248 : : token, index, image_index,
1249 : 12 : stat != null_pointer_node
1250 : 2 : ? gfc_build_addr_expr (NULL, stat) : stat,
1251 : : errmsg, errmsg_len);
1252 : : else
1253 : 8 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_event_wait, 6,
1254 : : token, index, until_count,
1255 : 8 : stat != null_pointer_node
1256 : 2 : ? gfc_build_addr_expr (NULL, stat) : stat,
1257 : : errmsg, errmsg_len);
1258 : 20 : gfc_add_expr_to_block (&se.pre, tmp);
1259 : :
1260 : : /* It guarantees memory consistency within the same segment */
1261 : 20 : tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
1262 : 20 : tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1263 : : gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1264 : : tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1265 : 20 : ASM_VOLATILE_P (tmp) = 1;
1266 : 20 : gfc_add_expr_to_block (&se.pre, tmp);
1267 : :
1268 : 20 : if (stat2 != NULL_TREE)
1269 : 0 : gfc_add_modify (&se.pre, stat2, fold_convert (TREE_TYPE (stat2), stat));
1270 : :
1271 : 20 : return gfc_finish_block (&se.pre);
1272 : : }
1273 : :
1274 : : tree
1275 : 718 : gfc_trans_sync (gfc_code *code, gfc_exec_op type)
1276 : : {
1277 : 718 : gfc_se se, argse;
1278 : 718 : tree tmp;
1279 : 718 : tree images = NULL_TREE, stat = NULL_TREE,
1280 : 718 : errmsg = NULL_TREE, errmsglen = NULL_TREE;
1281 : :
1282 : : /* Short cut: For single images without bound checking or without STAT=,
1283 : : return early. (ERRMSG= is always untouched for -fcoarray=single.) */
1284 : 718 : if (!code->expr2 && !(gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
1285 : 620 : && flag_coarray != GFC_FCOARRAY_LIB)
1286 : : return NULL_TREE;
1287 : :
1288 : 392 : gfc_init_se (&se, NULL);
1289 : 392 : gfc_start_block (&se.pre);
1290 : :
1291 : 392 : if (code->expr1 && code->expr1->rank == 0)
1292 : : {
1293 : 13 : gfc_init_se (&argse, NULL);
1294 : 13 : gfc_conv_expr_val (&argse, code->expr1);
1295 : 13 : images = argse.expr;
1296 : : }
1297 : :
1298 : 392 : if (code->expr2)
1299 : : {
1300 : 71 : gcc_assert (code->expr2->expr_type == EXPR_VARIABLE
1301 : : || code->expr2->expr_type == EXPR_FUNCTION);
1302 : 71 : gfc_init_se (&argse, NULL);
1303 : 71 : gfc_conv_expr_val (&argse, code->expr2);
1304 : 71 : stat = argse.expr;
1305 : : }
1306 : : else
1307 : 321 : stat = null_pointer_node;
1308 : :
1309 : 392 : if (code->expr3 && flag_coarray == GFC_FCOARRAY_LIB)
1310 : : {
1311 : 14 : gcc_assert (code->expr3->expr_type == EXPR_VARIABLE
1312 : : || code->expr3->expr_type == EXPR_FUNCTION);
1313 : 14 : gfc_init_se (&argse, NULL);
1314 : 14 : argse.want_pointer = 1;
1315 : 14 : gfc_conv_expr (&argse, code->expr3);
1316 : 14 : gfc_conv_string_parameter (&argse);
1317 : 14 : errmsg = gfc_build_addr_expr (NULL, argse.expr);
1318 : 14 : errmsglen = fold_convert (size_type_node, argse.string_length);
1319 : : }
1320 : 378 : else if (flag_coarray == GFC_FCOARRAY_LIB)
1321 : : {
1322 : 318 : errmsg = null_pointer_node;
1323 : 318 : errmsglen = build_int_cst (size_type_node, 0);
1324 : : }
1325 : :
1326 : : /* Check SYNC IMAGES(imageset) for valid image index.
1327 : : FIXME: Add a check for image-set arrays. */
1328 : 392 : if (code->expr1 && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
1329 : 13 : && code->expr1->rank == 0)
1330 : : {
1331 : 11 : tree images2 = fold_convert (integer_type_node, images);
1332 : 11 : tree cond;
1333 : 11 : if (flag_coarray != GFC_FCOARRAY_LIB)
1334 : 4 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1335 : 4 : images, build_int_cst (TREE_TYPE (images), 1));
1336 : : else
1337 : : {
1338 : 7 : tree cond2;
1339 : 7 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images,
1340 : : 2, null_pointer_node, null_pointer_node);
1341 : 7 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
1342 : : images2, tmp);
1343 : 7 : cond2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
1344 : : images,
1345 : 7 : build_int_cst (TREE_TYPE (images), 1));
1346 : 7 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
1347 : : logical_type_node, cond, cond2);
1348 : : }
1349 : 11 : gfc_trans_runtime_check (true, false, cond, &se.pre,
1350 : 11 : &code->expr1->where, "Invalid image number "
1351 : : "%d in SYNC IMAGES", images2);
1352 : : }
1353 : :
1354 : : /* Per F2008, 8.5.1, a SYNC MEMORY is implied by calling the
1355 : : image control statements SYNC IMAGES and SYNC ALL. */
1356 : 392 : if (flag_coarray == GFC_FCOARRAY_LIB)
1357 : : {
1358 : 332 : tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
1359 : 332 : tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1360 : : gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1361 : : tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1362 : 332 : ASM_VOLATILE_P (tmp) = 1;
1363 : 332 : gfc_add_expr_to_block (&se.pre, tmp);
1364 : : }
1365 : :
1366 : 392 : if (flag_coarray != GFC_FCOARRAY_LIB)
1367 : : {
1368 : : /* Set STAT to zero. */
1369 : 60 : if (code->expr2)
1370 : 48 : gfc_add_modify (&se.pre, stat, build_int_cst (TREE_TYPE (stat), 0));
1371 : : }
1372 : 332 : else if (type == EXEC_SYNC_ALL || type == EXEC_SYNC_MEMORY)
1373 : : {
1374 : : /* SYNC ALL => stat == null_pointer_node
1375 : : SYNC ALL(stat=s) => stat has an integer type
1376 : :
1377 : : If "stat" has the wrong integer type, use a temp variable of
1378 : : the right type and later cast the result back into "stat". */
1379 : 315 : if (stat == null_pointer_node || TREE_TYPE (stat) == integer_type_node)
1380 : : {
1381 : 315 : if (TREE_TYPE (stat) == integer_type_node)
1382 : 19 : stat = gfc_build_addr_expr (NULL, stat);
1383 : :
1384 : 315 : if(type == EXEC_SYNC_MEMORY)
1385 : 14 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_memory,
1386 : : 3, stat, errmsg, errmsglen);
1387 : : else
1388 : 301 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_all,
1389 : : 3, stat, errmsg, errmsglen);
1390 : :
1391 : 315 : gfc_add_expr_to_block (&se.pre, tmp);
1392 : : }
1393 : : else
1394 : : {
1395 : 0 : tree tmp_stat = gfc_create_var (integer_type_node, "stat");
1396 : :
1397 : 0 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_all,
1398 : : 3, gfc_build_addr_expr (NULL, tmp_stat),
1399 : : errmsg, errmsglen);
1400 : 0 : gfc_add_expr_to_block (&se.pre, tmp);
1401 : :
1402 : 0 : gfc_add_modify (&se.pre, stat,
1403 : 0 : fold_convert (TREE_TYPE (stat), tmp_stat));
1404 : : }
1405 : : }
1406 : : else
1407 : : {
1408 : 17 : tree len;
1409 : :
1410 : 17 : gcc_assert (type == EXEC_SYNC_IMAGES);
1411 : :
1412 : 17 : if (!code->expr1)
1413 : : {
1414 : 6 : len = build_int_cst (integer_type_node, -1);
1415 : 6 : images = null_pointer_node;
1416 : : }
1417 : 11 : else if (code->expr1->rank == 0)
1418 : : {
1419 : 9 : len = integer_one_node;
1420 : 9 : images = gfc_build_addr_expr (NULL_TREE, images);
1421 : : }
1422 : : else
1423 : : {
1424 : : /* FIXME. */
1425 : 2 : if (code->expr1->ts.kind != gfc_c_int_kind)
1426 : 0 : gfc_fatal_error ("Sorry, only support for integer kind %d "
1427 : : "implemented for image-set at %L",
1428 : : gfc_c_int_kind, &code->expr1->where);
1429 : :
1430 : 2 : gfc_conv_array_parameter (&se, code->expr1, true, NULL, NULL, &len);
1431 : 2 : images = se.expr;
1432 : :
1433 : 2 : tmp = gfc_typenode_for_spec (&code->expr1->ts);
1434 : 2 : if (GFC_ARRAY_TYPE_P (tmp) || GFC_DESCRIPTOR_TYPE_P (tmp))
1435 : 0 : tmp = gfc_get_element_type (tmp);
1436 : :
1437 : 4 : len = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
1438 : 2 : TREE_TYPE (len), len,
1439 : 2 : fold_convert (TREE_TYPE (len),
1440 : : TYPE_SIZE_UNIT (tmp)));
1441 : 2 : len = fold_convert (integer_type_node, len);
1442 : : }
1443 : :
1444 : : /* SYNC IMAGES(imgs) => stat == null_pointer_node
1445 : : SYNC IMAGES(imgs,stat=s) => stat has an integer type
1446 : :
1447 : : If "stat" has the wrong integer type, use a temp variable of
1448 : : the right type and later cast the result back into "stat". */
1449 : 17 : if (stat == null_pointer_node || TREE_TYPE (stat) == integer_type_node)
1450 : : {
1451 : 17 : if (TREE_TYPE (stat) == integer_type_node)
1452 : 4 : stat = gfc_build_addr_expr (NULL, stat);
1453 : :
1454 : 17 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_images,
1455 : : 5, fold_convert (integer_type_node, len),
1456 : : images, stat, errmsg, errmsglen);
1457 : 17 : gfc_add_expr_to_block (&se.pre, tmp);
1458 : : }
1459 : : else
1460 : : {
1461 : 0 : tree tmp_stat = gfc_create_var (integer_type_node, "stat");
1462 : :
1463 : 0 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_images,
1464 : : 5, fold_convert (integer_type_node, len),
1465 : : images, gfc_build_addr_expr (NULL, tmp_stat),
1466 : : errmsg, errmsglen);
1467 : 0 : gfc_add_expr_to_block (&se.pre, tmp);
1468 : :
1469 : 0 : gfc_add_modify (&se.pre, stat,
1470 : 0 : fold_convert (TREE_TYPE (stat), tmp_stat));
1471 : : }
1472 : : }
1473 : :
1474 : 392 : return gfc_finish_block (&se.pre);
1475 : : }
1476 : :
1477 : :
1478 : : /* Generate GENERIC for the IF construct. This function also deals with
1479 : : the simple IF statement, because the front end translates the IF
1480 : : statement into an IF construct.
1481 : :
1482 : : We translate:
1483 : :
1484 : : IF (cond) THEN
1485 : : then_clause
1486 : : ELSEIF (cond2)
1487 : : elseif_clause
1488 : : ELSE
1489 : : else_clause
1490 : : ENDIF
1491 : :
1492 : : into:
1493 : :
1494 : : pre_cond_s;
1495 : : if (cond_s)
1496 : : {
1497 : : then_clause;
1498 : : }
1499 : : else
1500 : : {
1501 : : pre_cond_s
1502 : : if (cond_s)
1503 : : {
1504 : : elseif_clause
1505 : : }
1506 : : else
1507 : : {
1508 : : else_clause;
1509 : : }
1510 : : }
1511 : :
1512 : : where COND_S is the simplified version of the predicate. PRE_COND_S
1513 : : are the pre side-effects produced by the translation of the
1514 : : conditional.
1515 : : We need to build the chain recursively otherwise we run into
1516 : : problems with folding incomplete statements. */
1517 : :
1518 : : static tree
1519 : 237810 : gfc_trans_if_1 (gfc_code * code)
1520 : : {
1521 : 237810 : gfc_se if_se;
1522 : 237810 : tree stmt, elsestmt;
1523 : 237810 : location_t loc, saved_loc = UNKNOWN_LOCATION;
1524 : :
1525 : : /* Check for an unconditional ELSE clause. */
1526 : 237810 : if (!code->expr1)
1527 : 6603 : return gfc_trans_code (code->next);
1528 : :
1529 : : /* Initialize a statement builder for each block. Puts in NULL_TREEs. */
1530 : 231207 : gfc_init_se (&if_se, NULL);
1531 : 231207 : gfc_start_block (&if_se.pre);
1532 : :
1533 : : /* Calculate the IF condition expression. */
1534 : 231207 : if (GFC_LOCUS_IS_SET (code->expr1->where))
1535 : : {
1536 : 231207 : saved_loc = input_location;
1537 : 231207 : input_location = gfc_get_location (&code->expr1->where);
1538 : : }
1539 : :
1540 : 231207 : gfc_conv_expr_val (&if_se, code->expr1);
1541 : :
1542 : 231207 : if (saved_loc != UNKNOWN_LOCATION)
1543 : 231207 : input_location = saved_loc;
1544 : :
1545 : : /* Translate the THEN clause. */
1546 : 231207 : stmt = gfc_trans_code (code->next);
1547 : :
1548 : : /* Translate the ELSE clause. */
1549 : 231207 : if (code->block)
1550 : 6845 : elsestmt = gfc_trans_if_1 (code->block);
1551 : : else
1552 : 224362 : elsestmt = build_empty_stmt (input_location);
1553 : :
1554 : : /* Build the condition expression and add it to the condition block. */
1555 : 461866 : loc = (GFC_LOCUS_IS_SET (code->expr1->where)
1556 : 461866 : ? gfc_get_location (&code->expr1->where) : input_location);
1557 : 231207 : stmt = fold_build3_loc (loc, COND_EXPR, void_type_node, if_se.expr, stmt,
1558 : : elsestmt);
1559 : :
1560 : 231207 : gfc_add_expr_to_block (&if_se.pre, stmt);
1561 : :
1562 : : /* Finish off this statement. */
1563 : 231207 : return gfc_finish_block (&if_se.pre);
1564 : : }
1565 : :
1566 : : tree
1567 : 230965 : gfc_trans_if (gfc_code * code)
1568 : : {
1569 : 230965 : stmtblock_t body;
1570 : 230965 : tree exit_label;
1571 : :
1572 : : /* Create exit label so it is available for trans'ing the body code. */
1573 : 230965 : exit_label = gfc_build_label_decl (NULL_TREE);
1574 : 230965 : code->exit_label = exit_label;
1575 : :
1576 : : /* Translate the actual code in code->block. */
1577 : 230965 : gfc_init_block (&body);
1578 : 230965 : gfc_add_expr_to_block (&body, gfc_trans_if_1 (code->block));
1579 : :
1580 : : /* Add exit label. */
1581 : 230965 : gfc_add_expr_to_block (&body, build1_v (LABEL_EXPR, exit_label));
1582 : :
1583 : 230965 : return gfc_finish_block (&body);
1584 : : }
1585 : :
1586 : :
1587 : : /* Translate an arithmetic IF expression.
1588 : :
1589 : : IF (cond) label1, label2, label3 translates to
1590 : :
1591 : : if (cond <= 0)
1592 : : {
1593 : : if (cond < 0)
1594 : : goto label1;
1595 : : else // cond == 0
1596 : : goto label2;
1597 : : }
1598 : : else // cond > 0
1599 : : goto label3;
1600 : :
1601 : : An optimized version can be generated in case of equal labels.
1602 : : E.g., if label1 is equal to label2, we can translate it to
1603 : :
1604 : : if (cond <= 0)
1605 : : goto label1;
1606 : : else
1607 : : goto label3;
1608 : : */
1609 : :
1610 : : tree
1611 : 64 : gfc_trans_arithmetic_if (gfc_code * code)
1612 : : {
1613 : 64 : gfc_se se;
1614 : 64 : tree tmp;
1615 : 64 : tree branch1;
1616 : 64 : tree branch2;
1617 : 64 : tree zero;
1618 : :
1619 : : /* Start a new block. */
1620 : 64 : gfc_init_se (&se, NULL);
1621 : 64 : gfc_start_block (&se.pre);
1622 : :
1623 : : /* Pre-evaluate COND. */
1624 : 64 : gfc_conv_expr_val (&se, code->expr1);
1625 : 64 : se.expr = gfc_evaluate_now (se.expr, &se.pre);
1626 : :
1627 : : /* Build something to compare with. */
1628 : 64 : zero = gfc_build_const (TREE_TYPE (se.expr), integer_zero_node);
1629 : :
1630 : 64 : if (code->label1->value != code->label2->value)
1631 : : {
1632 : : /* If (cond < 0) take branch1 else take branch2.
1633 : : First build jumps to the COND .LT. 0 and the COND .EQ. 0 cases. */
1634 : 49 : branch1 = build1_v (GOTO_EXPR, gfc_get_label_decl (code->label1));
1635 : 49 : branch2 = build1_v (GOTO_EXPR, gfc_get_label_decl (code->label2));
1636 : :
1637 : 49 : if (code->label1->value != code->label3->value)
1638 : 36 : tmp = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
1639 : : se.expr, zero);
1640 : : else
1641 : 13 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1642 : : se.expr, zero);
1643 : :
1644 : 49 : branch1 = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1645 : : tmp, branch1, branch2);
1646 : : }
1647 : : else
1648 : 15 : branch1 = build1_v (GOTO_EXPR, gfc_get_label_decl (code->label1));
1649 : :
1650 : 64 : if (code->label1->value != code->label3->value
1651 : 45 : && code->label2->value != code->label3->value)
1652 : : {
1653 : : /* if (cond <= 0) take branch1 else take branch2. */
1654 : 37 : branch2 = build1_v (GOTO_EXPR, gfc_get_label_decl (code->label3));
1655 : 37 : tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
1656 : : se.expr, zero);
1657 : 37 : branch1 = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1658 : : tmp, branch1, branch2);
1659 : : }
1660 : :
1661 : : /* Append the COND_EXPR to the evaluation of COND, and return. */
1662 : 64 : gfc_add_expr_to_block (&se.pre, branch1);
1663 : 64 : return gfc_finish_block (&se.pre);
1664 : : }
1665 : :
1666 : :
1667 : : /* Translate a CRITICAL block. */
1668 : :
1669 : : tree
1670 : 33 : gfc_trans_critical (gfc_code *code)
1671 : : {
1672 : 33 : stmtblock_t block;
1673 : 33 : tree tmp, token = NULL_TREE;
1674 : 33 : tree stat = NULL_TREE, errmsg, errmsg_len;
1675 : :
1676 : 33 : gfc_start_block (&block);
1677 : :
1678 : 33 : if (flag_coarray == GFC_FCOARRAY_LIB)
1679 : : {
1680 : 17 : gfc_se se;
1681 : :
1682 : 17 : gfc_init_se (&se, NULL);
1683 : 17 : gfc_trans_sync_stat (&code->ext.sync_stat, &se, &stat, &errmsg,
1684 : : &errmsg_len);
1685 : 17 : gfc_add_block_to_block (&block, &se.pre);
1686 : :
1687 : 17 : token = gfc_get_symbol_decl (code->resolved_sym);
1688 : 17 : token = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (token));
1689 : 17 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_lock, 7,
1690 : : token, integer_zero_node, integer_one_node,
1691 : : null_pointer_node, stat, errmsg, errmsg_len);
1692 : 17 : gfc_add_expr_to_block (&block, tmp);
1693 : 17 : gfc_add_block_to_block (&block, &se.post);
1694 : :
1695 : : /* It guarantees memory consistency within the same segment. */
1696 : 17 : tmp = gfc_build_string_const (strlen ("memory") + 1, "memory"),
1697 : 17 : tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1698 : : gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1699 : : tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1700 : 17 : ASM_VOLATILE_P (tmp) = 1;
1701 : :
1702 : 17 : gfc_add_expr_to_block (&block, tmp);
1703 : : }
1704 : :
1705 : 33 : tmp = gfc_trans_code (code->block->next);
1706 : 33 : gfc_add_expr_to_block (&block, tmp);
1707 : :
1708 : 33 : if (flag_coarray == GFC_FCOARRAY_LIB)
1709 : : {
1710 : : /* END CRITICAL does not accept STAT or ERRMSG arguments.
1711 : : * If STAT= is specified for CRITICAL, pass a stat argument to
1712 : : * _gfortran_caf_lock_unlock to prevent termination in the event of an
1713 : : * error, but ignore any value assigned to it.
1714 : : */
1715 : 17 : tmp = build_call_expr_loc (
1716 : : input_location, gfor_fndecl_caf_unlock, 6, token, integer_zero_node,
1717 : : integer_one_node,
1718 : 17 : stat != NULL_TREE
1719 : 17 : ? gfc_build_addr_expr (NULL,
1720 : : gfc_create_var (integer_type_node, "stat"))
1721 : : : null_pointer_node,
1722 : : null_pointer_node, integer_zero_node);
1723 : 17 : gfc_add_expr_to_block (&block, tmp);
1724 : :
1725 : : /* It guarantees memory consistency within the same segment */
1726 : 17 : tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
1727 : 17 : tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1728 : : gfc_build_string_const (1, ""),
1729 : : NULL_TREE, NULL_TREE,
1730 : : tree_cons (NULL_TREE, tmp, NULL_TREE),
1731 : : NULL_TREE);
1732 : 17 : ASM_VOLATILE_P (tmp) = 1;
1733 : :
1734 : 17 : gfc_add_expr_to_block (&block, tmp);
1735 : : }
1736 : :
1737 : 33 : return gfc_finish_block (&block);
1738 : : }
1739 : :
1740 : :
1741 : : /* Return true, when the class has a _len component. */
1742 : :
1743 : : static bool
1744 : 711 : class_has_len_component (gfc_symbol *sym)
1745 : : {
1746 : 711 : gfc_component *comp = sym->ts.u.derived->components;
1747 : 2040 : while (comp)
1748 : : {
1749 : 1746 : if (strcmp (comp->name, "_len") == 0)
1750 : : return true;
1751 : 1329 : comp = comp->next;
1752 : : }
1753 : : return false;
1754 : : }
1755 : :
1756 : :
1757 : : static void
1758 : 1720 : copy_descriptor (stmtblock_t *block, tree dst, tree src, int rank)
1759 : : {
1760 : 1720 : int n;
1761 : 1720 : tree dim;
1762 : 1720 : tree tmp;
1763 : 1720 : tree tmp2;
1764 : 1720 : tree size;
1765 : 1720 : tree offset;
1766 : :
1767 : 1720 : offset = gfc_index_zero_node;
1768 : :
1769 : : /* Use memcpy to copy the descriptor. The size is the minimum of
1770 : : the sizes of 'src' and 'dst'. This avoids a non-trivial conversion. */
1771 : 1720 : tmp = TYPE_SIZE_UNIT (TREE_TYPE (src));
1772 : 1720 : tmp2 = TYPE_SIZE_UNIT (TREE_TYPE (dst));
1773 : 1720 : size = fold_build2_loc (input_location, MIN_EXPR,
1774 : 1720 : TREE_TYPE (tmp), tmp, tmp2);
1775 : 1720 : tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
1776 : 1720 : tmp = build_call_expr_loc (input_location, tmp, 3,
1777 : : gfc_build_addr_expr (NULL_TREE, dst),
1778 : : gfc_build_addr_expr (NULL_TREE, src),
1779 : : fold_convert (size_type_node, size));
1780 : 1720 : gfc_add_expr_to_block (block, tmp);
1781 : :
1782 : : /* Set the offset correctly. */
1783 : 8592 : for (n = 0; n < rank; n++)
1784 : : {
1785 : 5152 : dim = gfc_rank_cst[n];
1786 : 5152 : tmp = gfc_conv_descriptor_lbound_get (src, dim);
1787 : 5152 : tmp2 = gfc_conv_descriptor_stride_get (src, dim);
1788 : 5152 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
1789 : : tmp, tmp2);
1790 : 5152 : offset = fold_build2_loc (input_location, MINUS_EXPR,
1791 : 5152 : TREE_TYPE (offset), offset, tmp);
1792 : 5152 : offset = gfc_evaluate_now (offset, block);
1793 : : }
1794 : :
1795 : 1720 : gfc_conv_descriptor_offset_set (block, dst, offset);
1796 : 1720 : }
1797 : :
1798 : :
1799 : : /* Do proper initialization for ASSOCIATE names. */
1800 : :
1801 : : static void
1802 : 6885 : trans_associate_var (gfc_symbol *sym, gfc_wrapped_block *block)
1803 : : {
1804 : 6885 : gfc_expr *e;
1805 : 6885 : tree tmp;
1806 : 6885 : bool class_target;
1807 : 6885 : bool unlimited;
1808 : 6885 : tree desc;
1809 : 6885 : tree offset;
1810 : 6885 : tree dim;
1811 : 6885 : int n;
1812 : 6885 : tree charlen;
1813 : 6885 : bool need_len_assign;
1814 : 6885 : bool whole_array = true;
1815 : 6885 : bool same_class;
1816 : 6885 : gfc_ref *ref;
1817 : 6885 : gfc_symbol *sym2;
1818 : :
1819 : 6885 : gcc_assert (sym->assoc);
1820 : 6885 : e = sym->assoc->target;
1821 : :
1822 : 16157 : class_target = (e->expr_type == EXPR_VARIABLE)
1823 : 6244 : && e->ts.type == BT_CLASS
1824 : 9352 : && (gfc_is_class_scalar_expr (e)
1825 : 1963 : || gfc_is_class_array_ref (e, NULL));
1826 : 2387 : same_class = class_target && sym->ts.type == BT_CLASS
1827 : 1066 : && strcmp (sym->ts.u.derived->name, e->ts.u.derived->name) == 0;
1828 : :
1829 : 6885 : unlimited = UNLIMITED_POLY (e);
1830 : :
1831 : 15169 : for (ref = e->ref; ref; ref = ref->next)
1832 : 8332 : if (ref->type == REF_ARRAY && ref->u.ar.type == AR_FULL
1833 : 3301 : && ref->u.ar.dimen != 0 && ref->next)
1834 : : {
1835 : : whole_array = false;
1836 : : break;
1837 : : }
1838 : :
1839 : : /* Assignments to the string length need to be generated, when
1840 : : ( sym is a char array or
1841 : : sym has a _len component)
1842 : : and the associated expression is unlimited polymorphic, which is
1843 : : not (yet) correctly in 'unlimited', because for an already associated
1844 : : BT_DERIVED the u-poly flag is not set, i.e.,
1845 : : __tmp_CHARACTER_0_1 => w => arg
1846 : : ^ generated temp ^ from code, the w does not have the u-poly
1847 : : flag set, where UNLIMITED_POLY(e) expects it. */
1848 : 5623 : need_len_assign = ((unlimited || (e->ts.type == BT_DERIVED
1849 : 2268 : && e->ts.u.derived->attr.unlimited_polymorphic))
1850 : 2081 : && (sym->ts.type == BT_CHARACTER
1851 : 1351 : || ((sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED)
1852 : 711 : && class_has_len_component (sym)))
1853 : 8032 : && !sym->attr.select_rank_temporary);
1854 : :
1855 : : /* Do a `pointer assignment' with updated descriptor (or assign descriptor
1856 : : to array temporary) for arrays with either unknown shape or if associating
1857 : : to a variable. Select rank temporaries need somewhat different treatment
1858 : : to other associate names and case temporaries. This because the selector
1859 : : is assumed rank and so the offset in particular has to be changed. Also,
1860 : : the case temporaries carry both allocatable and target attributes if
1861 : : present in the selector. This means that an allocatation or change of
1862 : : association can occur and so has to be dealt with. */
1863 : 6885 : if (sym->attr.select_rank_temporary)
1864 : : {
1865 : 1362 : gfc_se se;
1866 : 1362 : tree class_decl = NULL_TREE;
1867 : 1362 : int rank = 0;
1868 : 1362 : bool class_ptr;
1869 : :
1870 : 1362 : sym2 = e->symtree->n.sym;
1871 : 1362 : gfc_init_se (&se, NULL);
1872 : 1362 : if (e->ts.type == BT_CLASS)
1873 : : {
1874 : : /* Go straight to the class data. */
1875 : 145 : if (sym2->attr.dummy && !sym2->attr.optional)
1876 : : {
1877 : 121 : class_decl = sym2->backend_decl;
1878 : 121 : if (DECL_LANG_SPECIFIC (class_decl)
1879 : 121 : && GFC_DECL_SAVED_DESCRIPTOR (class_decl))
1880 : 0 : class_decl = GFC_DECL_SAVED_DESCRIPTOR (class_decl);
1881 : 121 : if (POINTER_TYPE_P (TREE_TYPE (class_decl)))
1882 : 121 : class_decl = build_fold_indirect_ref_loc (input_location,
1883 : : class_decl);
1884 : 121 : gcc_assert (GFC_CLASS_TYPE_P (TREE_TYPE (class_decl)));
1885 : 121 : se.expr = gfc_class_data_get (class_decl);
1886 : : }
1887 : : else
1888 : : {
1889 : 24 : class_decl = sym2->backend_decl;
1890 : 24 : gfc_conv_expr_descriptor (&se, e);
1891 : 24 : if (POINTER_TYPE_P (TREE_TYPE (se.expr)))
1892 : 0 : se.expr = build_fold_indirect_ref_loc (input_location,
1893 : : se.expr);
1894 : : }
1895 : :
1896 : 145 : if (CLASS_DATA (sym)->as && CLASS_DATA (sym)->as->rank > 0)
1897 : 145 : rank = CLASS_DATA (sym)->as->rank;
1898 : : }
1899 : : else
1900 : : {
1901 : 1217 : gfc_conv_expr_descriptor (&se, e);
1902 : 1217 : if (sym->as && sym->as->rank > 0)
1903 : 1168 : rank = sym->as->rank;
1904 : : }
1905 : :
1906 : 1362 : desc = sym->backend_decl;
1907 : :
1908 : : /* The SELECT TYPE mechanisms turn class temporaries into pointers, which
1909 : : point to the selector. */
1910 : 1362 : class_ptr = class_decl != NULL_TREE && POINTER_TYPE_P (TREE_TYPE (desc));
1911 : 145 : if (class_ptr)
1912 : : {
1913 : 145 : tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (desc)), "class");
1914 : 145 : tmp = gfc_build_addr_expr (NULL, tmp);
1915 : 145 : gfc_add_modify (&se.pre, desc, tmp);
1916 : :
1917 : 145 : tmp = gfc_class_vptr_get (class_decl);
1918 : 145 : gfc_add_modify (&se.pre, gfc_class_vptr_get (desc), tmp);
1919 : 145 : if (UNLIMITED_POLY (sym))
1920 : 101 : gfc_add_modify (&se.pre, gfc_class_len_get (desc),
1921 : : gfc_class_len_get (class_decl));
1922 : :
1923 : 145 : desc = gfc_class_data_get (desc);
1924 : : }
1925 : :
1926 : : /* SELECT RANK temporaries can carry the allocatable and pointer
1927 : : attributes so the selector descriptor must be copied in and
1928 : : copied out. */
1929 : 1362 : if (rank > 0)
1930 : 1282 : copy_descriptor (&se.pre, desc, se.expr, rank);
1931 : : else
1932 : : {
1933 : 80 : tmp = gfc_conv_descriptor_data_get (se.expr);
1934 : 80 : gfc_add_modify (&se.pre, desc,
1935 : 80 : fold_convert (TREE_TYPE (desc), tmp));
1936 : : }
1937 : :
1938 : : /* Deal with associate_name => selector. Class associate names are
1939 : : treated in the same way as in SELECT TYPE. */
1940 : 1362 : sym2 = sym->assoc->target->symtree->n.sym;
1941 : 1362 : if (sym2->assoc && sym->assoc->target && sym2->ts.type != BT_CLASS)
1942 : : {
1943 : 54 : sym2 = sym2->assoc->target->symtree->n.sym;
1944 : 54 : se.expr = sym2->backend_decl;
1945 : :
1946 : 54 : if (POINTER_TYPE_P (TREE_TYPE (se.expr)))
1947 : 54 : se.expr = build_fold_indirect_ref_loc (input_location,
1948 : : se.expr);
1949 : : }
1950 : :
1951 : : /* There could have been reallocation. Copy descriptor back to the
1952 : : selector and update the offset. */
1953 : 1362 : if (sym->attr.allocatable || sym->attr.pointer
1954 : 996 : || (sym->ts.type == BT_CLASS
1955 : 127 : && (CLASS_DATA (sym)->attr.allocatable
1956 : 79 : || CLASS_DATA (sym)->attr.pointer)))
1957 : : {
1958 : 493 : if (rank > 0)
1959 : 438 : copy_descriptor (&se.post, se.expr, desc, rank);
1960 : : else
1961 : 55 : gfc_conv_descriptor_data_set (&se.post, se.expr, desc);
1962 : :
1963 : : /* The dynamic type could have changed too. */
1964 : 493 : if (sym->ts.type == BT_CLASS)
1965 : : {
1966 : 145 : tmp = sym->backend_decl;
1967 : 145 : if (class_ptr)
1968 : 145 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
1969 : 145 : gfc_add_modify (&se.post, gfc_class_vptr_get (class_decl),
1970 : : gfc_class_vptr_get (tmp));
1971 : 145 : if (UNLIMITED_POLY (sym))
1972 : 101 : gfc_add_modify (&se.post, gfc_class_len_get (class_decl),
1973 : : gfc_class_len_get (tmp));
1974 : : }
1975 : : }
1976 : :
1977 : 1362 : tmp = gfc_finish_block (&se.post);
1978 : :
1979 : 1362 : gfc_add_init_cleanup (block, gfc_finish_block (&se.pre), tmp);
1980 : : }
1981 : :
1982 : : /* Now all the other kinds of associate variable. */
1983 : :
1984 : : /* First we do the F202y ASSOCIATE construct with an assumed rank selector.
1985 : : Since this requires rank remapping, the simplest implementation builds an
1986 : : array reference, using the array ref attached to the association_list,
1987 : : followed by gfc_trans_pointer_assignment. */
1988 : 5523 : else if (e->rank == -1 && sym->assoc->ar)
1989 : : {
1990 : 24 : gfc_array_ref *ar;
1991 : 24 : gfc_expr *expr1 = gfc_lval_expr_from_sym (sym);
1992 : 24 : stmtblock_t init;
1993 : 24 : gfc_init_block (&init);
1994 : :
1995 : : /* Build the array reference and add to expr1. */
1996 : 24 : gfc_free_ref_list (expr1->ref);
1997 : 24 : expr1->ref = gfc_get_ref();
1998 : 24 : expr1->ref->type = REF_ARRAY;
1999 : 24 : ar = gfc_copy_array_ref (sym->assoc->ar);
2000 : 24 : expr1->ref->u.ar = *ar;
2001 : 24 : expr1->ref->u.ar.type = AR_SECTION;
2002 : :
2003 : : /* For class objects, insert the _data component reference. Since the
2004 : : associate-name is a pointer, it needs a target, which is created using
2005 : : its typespec. If unlimited polymorphic, the _len field will be filled
2006 : : by the pointer assignment. */
2007 : 24 : if (expr1->ts.type == BT_CLASS)
2008 : : {
2009 : 12 : need_len_assign = false;
2010 : 12 : gfc_ref *ref;
2011 : 12 : gfc_find_component (expr1->ts.u.derived, "_data", true, true, &ref);
2012 : 12 : ref->next = expr1->ref;
2013 : 12 : expr1->ref = ref;
2014 : 12 : expr1->rank = CLASS_DATA (sym)->as->rank;
2015 : 12 : tmp = gfc_create_var (gfc_typenode_for_spec (&sym->ts), "class");
2016 : 12 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
2017 : 12 : gfc_add_modify (&init, sym->backend_decl, tmp);
2018 : : }
2019 : :
2020 : : /* Do the pointer assignment and clean up. */
2021 : 24 : gfc_expr *expr2 = gfc_copy_expr (e);
2022 : 24 : gfc_add_expr_to_block (&init,
2023 : : gfc_trans_pointer_assignment (expr1, expr2));
2024 : 24 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL);
2025 : 24 : gfc_free_expr (expr1);
2026 : 24 : gfc_free_expr (expr2);
2027 : 24 : }
2028 : 5499 : else if ((sym->attr.dimension || sym->attr.codimension) && !class_target
2029 : 613 : && (sym->as->type == AS_DEFERRED || sym->assoc->variable))
2030 : : {
2031 : 613 : gfc_se se;
2032 : 613 : tree desc;
2033 : 613 : bool cst_array_ctor;
2034 : 613 : stmtblock_t init;
2035 : 613 : gfc_init_block (&init);
2036 : :
2037 : 613 : desc = sym->backend_decl;
2038 : 1226 : cst_array_ctor = e->expr_type == EXPR_ARRAY
2039 : 97 : && gfc_constant_array_constructor_p (e->value.constructor)
2040 : 631 : && e->ts.type != BT_CHARACTER;
2041 : :
2042 : : /* If association is to an expression, evaluate it and create temporary.
2043 : : Otherwise, get descriptor of target for pointer assignment. */
2044 : 613 : gfc_init_se (&se, NULL);
2045 : :
2046 : 613 : if (sym->assoc->variable || cst_array_ctor)
2047 : : {
2048 : 388 : se.direct_byref = 1;
2049 : 388 : se.use_offset = 1;
2050 : 388 : se.expr = desc;
2051 : 388 : GFC_DECL_PTR_ARRAY_P (sym->backend_decl) = 1;
2052 : : }
2053 : :
2054 : 613 : if (sym->attr.codimension)
2055 : 11 : se.want_coarray = 1;
2056 : :
2057 : 613 : gfc_conv_expr_descriptor (&se, e);
2058 : :
2059 : 613 : if (flag_coarray == GFC_FCOARRAY_LIB && sym->attr.codimension)
2060 : : {
2061 : 3 : tree token = gfc_conv_descriptor_token (se.expr),
2062 : : size
2063 : 3 : = sym->attr.dimension
2064 : 3 : ? fold_build2 (MULT_EXPR, gfc_array_index_type,
2065 : : gfc_conv_descriptor_size (se.expr, e->rank),
2066 : : gfc_conv_descriptor_span_get (se.expr))
2067 : 2 : : gfc_conv_descriptor_span_get (se.expr);
2068 : : /* Create a new token, because in the token the modified descriptor
2069 : : is stored. The modified descriptor is needed for accesses on the
2070 : : remote image. In the scalar case, the base address needs to be
2071 : : associated correctly, which also needs a new token.
2072 : : The token is freed automatically be the end team statement. */
2073 : 3 : gfc_add_expr_to_block (
2074 : : &se.pre,
2075 : : build_call_expr_loc (
2076 : : input_location, gfor_fndecl_caf_register, 7, size,
2077 : : build_int_cst (integer_type_node, GFC_CAF_COARRAY_MAP_EXISTING),
2078 : : gfc_build_addr_expr (pvoid_type_node, token),
2079 : : gfc_build_addr_expr (NULL_TREE, se.expr), null_pointer_node,
2080 : : null_pointer_node, integer_zero_node));
2081 : : }
2082 : :
2083 : 613 : if (sym->ts.type == BT_CHARACTER
2084 : 266 : && !sym->attr.select_type_temporary
2085 : 266 : && sym->ts.u.cl->backend_decl
2086 : 266 : && VAR_P (sym->ts.u.cl->backend_decl)
2087 : 222 : && se.string_length
2088 : 222 : && se.string_length != sym->ts.u.cl->backend_decl)
2089 : : {
2090 : : /* When the target is a variable, its length is already known. */
2091 : 222 : tree len = fold_convert (TREE_TYPE (sym->ts.u.cl->backend_decl),
2092 : : se.string_length);
2093 : 222 : if (e->expr_type == EXPR_VARIABLE)
2094 : 130 : gfc_add_modify (&init, sym->ts.u.cl->backend_decl, len);
2095 : : else
2096 : 92 : gfc_add_modify (&se.pre, sym->ts.u.cl->backend_decl, len);
2097 : : }
2098 : :
2099 : : /* If we didn't already do the pointer assignment, set associate-name
2100 : : descriptor to the one generated for the temporary. */
2101 : 613 : if ((!sym->assoc->variable && !cst_array_ctor)
2102 : 388 : || !whole_array)
2103 : : {
2104 : 225 : int dim;
2105 : :
2106 : 225 : if (whole_array)
2107 : 225 : gfc_add_modify (&se.pre, desc, se.expr);
2108 : :
2109 : : /* The generated descriptor has lower bound zero (as array
2110 : : temporary), shift bounds so we get lower bounds of 1. */
2111 : 569 : for (dim = 0; dim < e->rank; ++dim)
2112 : 296 : gfc_conv_shift_descriptor_lbound (&se.pre, desc,
2113 : : dim, gfc_index_one_node);
2114 : : }
2115 : :
2116 : : /* If this is a subreference array pointer associate name use the
2117 : : associate variable element size for the value of 'span'. */
2118 : 613 : if (sym->attr.subref_array_pointer && !se.direct_byref)
2119 : : {
2120 : 6 : gcc_assert (e->expr_type == EXPR_VARIABLE);
2121 : 6 : tmp = gfc_get_array_span (se.expr, e);
2122 : :
2123 : 6 : gfc_conv_descriptor_span_set (&se.pre, desc, tmp);
2124 : : }
2125 : :
2126 : 613 : if (e->expr_type == EXPR_FUNCTION
2127 : 90 : && sym->ts.type == BT_DERIVED
2128 : 43 : && sym->ts.u.derived
2129 : 43 : && sym->ts.u.derived->attr.pdt_type)
2130 : : {
2131 : 0 : tmp = gfc_deallocate_pdt_comp (sym->ts.u.derived, se.expr,
2132 : 0 : sym->as->rank);
2133 : 0 : gfc_add_expr_to_block (&se.post, tmp);
2134 : : }
2135 : :
2136 : : /* Done, register stuff as init / cleanup code. */
2137 : 613 : gfc_add_block_to_block (&init, &se.pre);
2138 : 613 : gfc_add_init_cleanup (block, gfc_finish_block (&init),
2139 : : gfc_finish_block (&se.post));
2140 : 613 : }
2141 : :
2142 : : /* Temporaries, arising from TYPE IS, just need the descriptor of class
2143 : : arrays to be assigned directly. */
2144 : 4886 : else if (class_target && (sym->attr.dimension || sym->attr.codimension)
2145 : 1321 : && (sym->ts.type == BT_DERIVED || unlimited))
2146 : : {
2147 : 1321 : gfc_se se;
2148 : :
2149 : 1321 : gfc_init_se (&se, NULL);
2150 : 1321 : se.descriptor_only = 1;
2151 : : /* In a select type the (temporary) associate variable shall point to
2152 : : a standard fortran array (lower bound == 1), but conv_expr ()
2153 : : just maps to the input array in the class object, whose lbound may
2154 : : be arbitrary. conv_expr_descriptor solves this by inserting a
2155 : : temporary array descriptor. */
2156 : 1321 : gfc_conv_expr_descriptor (&se, e);
2157 : :
2158 : 1321 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr))
2159 : : || GFC_ARRAY_TYPE_P (TREE_TYPE (se.expr)));
2160 : 1321 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (sym->backend_decl)));
2161 : :
2162 : 1321 : if (sym->ts.type == BT_CHARACTER)
2163 : : {
2164 : : /* Emit a DECL_EXPR for the variable sized array type in so the
2165 : : gimplification of its type sizes works correctly. */
2166 : 302 : tree arraytype;
2167 : 302 : tmp = TREE_TYPE (sym->backend_decl);
2168 : 302 : arraytype = TREE_TYPE (GFC_TYPE_ARRAY_DATAPTR_TYPE (tmp));
2169 : 302 : if (! TYPE_NAME (arraytype))
2170 : 51 : TYPE_NAME (arraytype) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
2171 : : NULL_TREE, arraytype);
2172 : 302 : gfc_add_expr_to_block (&se.pre, build1 (DECL_EXPR,
2173 : 302 : arraytype, TYPE_NAME (arraytype)));
2174 : : }
2175 : :
2176 : 1321 : if (GFC_ARRAY_TYPE_P (TREE_TYPE (se.expr)))
2177 : : {
2178 : 0 : if (INDIRECT_REF_P (se.expr))
2179 : 0 : tmp = TREE_OPERAND (se.expr, 0);
2180 : : else
2181 : : tmp = se.expr;
2182 : :
2183 : 0 : gfc_add_modify (&se.pre, sym->backend_decl,
2184 : 0 : gfc_class_data_get (GFC_DECL_SAVED_DESCRIPTOR (tmp)));
2185 : : }
2186 : : else
2187 : 1321 : gfc_add_modify (&se.pre, sym->backend_decl, se.expr);
2188 : :
2189 : 1321 : if (unlimited)
2190 : : {
2191 : : /* Recover the dtype, which has been overwritten by the
2192 : : assignment from an unlimited polymorphic object. */
2193 : 797 : tmp = gfc_conv_descriptor_dtype (sym->backend_decl);
2194 : 797 : gfc_add_modify (&se.pre, tmp,
2195 : 797 : gfc_get_dtype (TREE_TYPE (sym->backend_decl)));
2196 : : }
2197 : :
2198 : 1321 : gfc_add_init_cleanup (block, gfc_finish_block (&se.pre),
2199 : : gfc_finish_block (&se.post));
2200 : 1321 : }
2201 : :
2202 : : /* Do a scalar pointer assignment; this is for scalar variable targets. */
2203 : 3565 : else if (gfc_is_associate_pointer (sym))
2204 : : {
2205 : 3236 : gfc_se se;
2206 : :
2207 : 3236 : gcc_assert (!sym->attr.dimension && !sym->attr.codimension);
2208 : :
2209 : 3236 : gfc_init_se (&se, NULL);
2210 : :
2211 : : /* Class associate-names come this way because they are
2212 : : unconditionally associate pointers and the symbol is scalar. */
2213 : 3236 : if (sym->ts.type == BT_CLASS && e->expr_type == EXPR_FUNCTION)
2214 : : {
2215 : 99 : gfc_conv_expr (&se, e);
2216 : 99 : se.expr = gfc_evaluate_now (se.expr, &se.pre);
2217 : : /* Finalize the expression and free if it is allocatable. */
2218 : 99 : gfc_finalize_tree_expr (&se, NULL, gfc_expr_attr (e), e->rank);
2219 : 99 : gfc_add_block_to_block (&se.post, &se.finalblock);
2220 : 99 : need_len_assign = false;
2221 : : }
2222 : 3137 : else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.dimension)
2223 : : {
2224 : 431 : tree target_expr;
2225 : : /* For a class array we need a descriptor for the selector. */
2226 : 431 : gfc_conv_expr_descriptor (&se, e);
2227 : : /* Needed to get/set the _len component below. */
2228 : 431 : target_expr = se.expr;
2229 : :
2230 : : /* Obtain a temporary class container for the result. */
2231 : 431 : gfc_conv_class_to_class (&se, e, sym->ts, false, true, false, false);
2232 : 431 : se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
2233 : :
2234 : 431 : desc = gfc_class_data_get (se.expr);
2235 : :
2236 : : /* Set the offset. */
2237 : 431 : offset = gfc_index_zero_node;
2238 : 912 : for (n = 0; n < e->rank; n++)
2239 : : {
2240 : 481 : dim = gfc_rank_cst[n];
2241 : 481 : tmp = fold_build2_loc (input_location, MULT_EXPR,
2242 : : gfc_array_index_type,
2243 : : gfc_conv_descriptor_stride_get (desc, dim),
2244 : : gfc_conv_descriptor_lbound_get (desc, dim));
2245 : 481 : offset = fold_build2_loc (input_location, MINUS_EXPR,
2246 : : gfc_array_index_type,
2247 : : offset, tmp);
2248 : : }
2249 : 431 : gfc_conv_descriptor_offset_set (&se.pre, desc, offset);
2250 : :
2251 : 431 : if (need_len_assign)
2252 : : {
2253 : 175 : if (e->symtree
2254 : 175 : && DECL_LANG_SPECIFIC (e->symtree->n.sym->backend_decl)
2255 : 84 : && GFC_DECL_SAVED_DESCRIPTOR (e->symtree->n.sym->backend_decl)
2256 : 205 : && TREE_CODE (target_expr) != COMPONENT_REF)
2257 : : /* Use the original class descriptor stored in the saved
2258 : : descriptor to get the target_expr. */
2259 : 36 : target_expr =
2260 : 18 : GFC_DECL_SAVED_DESCRIPTOR (e->symtree->n.sym->backend_decl);
2261 : : else
2262 : : /* Strip the _data component from the target_expr. */
2263 : 157 : target_expr = TREE_OPERAND (target_expr, 0);
2264 : : /* Add a reference to the _len comp to the target expr. */
2265 : 175 : tmp = gfc_class_len_get (target_expr);
2266 : : /* Get the component-ref for the temp structure's _len comp. */
2267 : 175 : charlen = gfc_class_len_get (se.expr);
2268 : : /* Add the assign to the beginning of the block... */
2269 : 175 : gfc_add_modify (&se.pre, charlen,
2270 : 175 : fold_convert (TREE_TYPE (charlen), tmp));
2271 : : /* and the oposite way at the end of the block, to hand changes
2272 : : on the string length back. */
2273 : 175 : gfc_add_modify (&se.post, tmp,
2274 : 175 : fold_convert (TREE_TYPE (tmp), charlen));
2275 : : /* Length assignment done, prevent adding it again below. */
2276 : 175 : need_len_assign = false;
2277 : : }
2278 : : }
2279 : 2706 : else if (sym->ts.type == BT_CLASS && e->ts.type == BT_CLASS
2280 : 570 : && CLASS_DATA (e)->attr.dimension)
2281 : : {
2282 : : /* This is bound to be a class array element. */
2283 : 80 : gfc_conv_expr_reference (&se, e);
2284 : : /* Obtain a temporary class container for the result. */
2285 : 80 : gfc_conv_derived_to_class (&se, e, sym, se.expr, false, false,
2286 : 80 : e->symtree->name);
2287 : 80 : need_len_assign = false;
2288 : : }
2289 : 2626 : else if (whole_array && (same_class || unlimited)
2290 : 249 : && e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.codimension)
2291 : : {
2292 : 9 : gfc_expr *class_e = gfc_find_and_cut_at_last_class_ref (e);
2293 : 9 : gfc_conv_expr (&se, class_e);
2294 : 9 : gfc_free_expr (class_e);
2295 : 9 : need_len_assign = false;
2296 : 9 : }
2297 : : else
2298 : : {
2299 : : /* For BT_CLASS and BT_DERIVED, this boils down to a pointer assign,
2300 : : which has the string length included. For CHARACTERS it is still
2301 : : needed and will be done at the end of this routine. */
2302 : 2617 : gfc_conv_expr (&se, e);
2303 : 2617 : need_len_assign = need_len_assign && sym->ts.type == BT_CHARACTER;
2304 : : }
2305 : :
2306 : 3236 : if (sym->ts.type == BT_CHARACTER
2307 : 519 : && !sym->attr.select_type_temporary
2308 : 91 : && VAR_P (sym->ts.u.cl->backend_decl)
2309 : 55 : && se.string_length != sym->ts.u.cl->backend_decl)
2310 : : {
2311 : 55 : gfc_add_modify (&se.pre, sym->ts.u.cl->backend_decl,
2312 : 55 : fold_convert (TREE_TYPE (sym->ts.u.cl->backend_decl),
2313 : : se.string_length));
2314 : 55 : if (e->expr_type == EXPR_FUNCTION)
2315 : : {
2316 : 6 : tmp = gfc_call_free (sym->backend_decl);
2317 : 6 : gfc_add_expr_to_block (&se.post, tmp);
2318 : : }
2319 : : }
2320 : :
2321 : 519 : if (sym->ts.type == BT_CHARACTER && e->ts.type == BT_CHARACTER
2322 : 3327 : && POINTER_TYPE_P (TREE_TYPE (se.expr)))
2323 : : {
2324 : : /* These are pointer types already. */
2325 : 73 : tmp = fold_convert (TREE_TYPE (sym->backend_decl), se.expr);
2326 : : }
2327 : : else
2328 : : {
2329 : 3163 : tree ctree = gfc_get_class_from_expr (se.expr);
2330 : 3163 : tmp = TREE_TYPE (sym->backend_decl);
2331 : :
2332 : 3163 : if (sym->ts.type == BT_CLASS && e->ts.type == BT_CLASS)
2333 : : {
2334 : : /* F2018:19.5.1.6 "If a selector has the POINTER attribute,
2335 : : it shall be associated; the associate name is associated
2336 : : with the target of the pointer and does not have the
2337 : : POINTER attribute." */
2338 : 615 : if (e->rank == 0 && ctree
2339 : 1703 : && (!GFC_CLASS_TYPE_P (TREE_TYPE (se.expr))
2340 : 533 : || CLASS_DATA (e)->attr.class_pointer))
2341 : : {
2342 : 284 : tree stmp;
2343 : 284 : tree dtmp;
2344 : 284 : tree ctmp;
2345 : :
2346 : 284 : ctmp = ctree;
2347 : 284 : dtmp = TREE_TYPE (TREE_TYPE (sym->backend_decl));
2348 : 284 : ctree = gfc_create_var (dtmp, "class");
2349 : :
2350 : 284 : if (IS_INFERRED_TYPE (e)
2351 : 6 : && !GFC_CLASS_TYPE_P (TREE_TYPE (se.expr)))
2352 : : stmp = se.expr;
2353 : : else
2354 : 284 : stmp = gfc_class_data_get (ctmp);
2355 : :
2356 : 284 : if (!CLASS_DATA (sym)->attr.codimension
2357 : 284 : && !POINTER_TYPE_P (TREE_TYPE (stmp)))
2358 : 0 : stmp = gfc_build_addr_expr (NULL, stmp);
2359 : :
2360 : 284 : dtmp = gfc_class_data_get (ctree);
2361 : 284 : stmp = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (dtmp), stmp);
2362 : 284 : gfc_add_modify (&se.pre, dtmp, stmp);
2363 : 284 : stmp = gfc_class_vptr_get (ctmp);
2364 : 284 : dtmp = gfc_class_vptr_get (ctree);
2365 : 284 : stmp = fold_convert (TREE_TYPE (dtmp), stmp);
2366 : 284 : gfc_add_modify (&se.pre, dtmp, stmp);
2367 : 284 : if (UNLIMITED_POLY (sym))
2368 : : {
2369 : 66 : stmp = gfc_class_len_get (ctmp);
2370 : 66 : dtmp = gfc_class_len_get (ctree);
2371 : 66 : stmp = fold_convert (TREE_TYPE (dtmp), stmp);
2372 : 66 : gfc_add_modify (&se.pre, dtmp, stmp);
2373 : 66 : need_len_assign = false;
2374 : : }
2375 : 284 : se.expr = ctree;
2376 : : }
2377 : 804 : else if (CLASS_DATA (sym)->attr.codimension)
2378 : : {
2379 : 27 : gfc_conv_class_to_class (&se, e, sym->ts, false, false, false,
2380 : : false);
2381 : 27 : tmp = se.expr;
2382 : : }
2383 : : }
2384 : : /* For non-pointer types in se.expr, the first condition holds.
2385 : : For pointer or reference types in se.expr, a double TREE_TYPE ()
2386 : : is possible and an associate variable always is a pointer. */
2387 : 6298 : if (!POINTER_TYPE_P (TREE_TYPE (se.expr))
2388 : 3163 : || TREE_TYPE (TREE_TYPE (se.expr))
2389 : 28 : != TREE_TYPE (TREE_TYPE (sym->backend_decl)))
2390 : 3136 : tmp = gfc_build_addr_expr (tmp, se.expr);
2391 : : }
2392 : :
2393 : 3236 : gfc_add_modify (&se.pre, sym->backend_decl, tmp);
2394 : :
2395 : 3236 : gfc_add_init_cleanup (block, gfc_finish_block( &se.pre),
2396 : : gfc_finish_block (&se.post));
2397 : : }
2398 : :
2399 : : /* Do a simple assignment. This is for scalar expressions, where we
2400 : : can simply use expression assignment. */
2401 : : else
2402 : : {
2403 : 329 : gfc_expr *lhs;
2404 : 329 : tree res;
2405 : 329 : gfc_se se;
2406 : 329 : stmtblock_t final_block;
2407 : :
2408 : 329 : gfc_init_se (&se, NULL);
2409 : :
2410 : : /* resolve.cc converts some associate names to allocatable so that
2411 : : allocation can take place automatically in gfc_trans_assignment.
2412 : : The frontend prevents them from being either allocated,
2413 : : deallocated or reallocated. */
2414 : 329 : if (sym->ts.type == BT_DERIVED
2415 : 73 : && sym->ts.u.derived->attr.alloc_comp)
2416 : : {
2417 : 12 : tmp = sym->backend_decl;
2418 : 12 : tmp = gfc_nullify_alloc_comp (sym->ts.u.derived, tmp,
2419 : 12 : sym->attr.dimension ? sym->as->rank : 0);
2420 : 12 : gfc_add_expr_to_block (&se.pre, tmp);
2421 : : }
2422 : :
2423 : 329 : if (sym->attr.allocatable)
2424 : : {
2425 : 12 : tmp = sym->backend_decl;
2426 : 12 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
2427 : 0 : tmp = gfc_conv_descriptor_data_get (tmp);
2428 : 12 : gfc_add_modify (&se.pre, tmp, fold_convert (TREE_TYPE (tmp),
2429 : : null_pointer_node));
2430 : : }
2431 : :
2432 : 329 : lhs = gfc_lval_expr_from_sym (sym);
2433 : 329 : lhs->must_finalize = 0;
2434 : 329 : res = gfc_trans_assignment (lhs, e, false, true);
2435 : 329 : gfc_add_expr_to_block (&se.pre, res);
2436 : :
2437 : 329 : gfc_init_block (&final_block);
2438 : :
2439 : 329 : if (sym->attr.associate_var
2440 : 329 : && sym->ts.type == BT_DERIVED
2441 : 73 : && sym->ts.u.derived->attr.defined_assign_comp
2442 : 0 : && gfc_may_be_finalized (sym->ts)
2443 : 329 : && e->expr_type == EXPR_FUNCTION)
2444 : : {
2445 : 0 : gfc_expr *ef;
2446 : 0 : ef = gfc_lval_expr_from_sym (sym);
2447 : 0 : gfc_add_finalizer_call (&final_block, ef);
2448 : 0 : gfc_free_expr (ef);
2449 : : }
2450 : :
2451 : 329 : if (sym->ts.type == BT_DERIVED
2452 : 73 : && sym->ts.u.derived->attr.alloc_comp)
2453 : : {
2454 : 12 : tmp = sym->backend_decl;
2455 : 12 : tmp = gfc_deallocate_alloc_comp (sym->ts.u.derived,
2456 : : tmp, 0);
2457 : 12 : gfc_add_expr_to_block (&final_block, tmp);
2458 : : }
2459 : :
2460 : 329 : tmp = sym->backend_decl;
2461 : 329 : if (e->expr_type == EXPR_FUNCTION
2462 : 76 : && sym->ts.type == BT_DERIVED
2463 : 39 : && sym->ts.u.derived
2464 : 39 : && sym->ts.u.derived->attr.pdt_type)
2465 : : {
2466 : 6 : tmp = gfc_deallocate_pdt_comp (sym->ts.u.derived, tmp,
2467 : : 0);
2468 : : }
2469 : 323 : else if (e->expr_type == EXPR_FUNCTION
2470 : 70 : && sym->ts.type == BT_CLASS
2471 : 0 : && CLASS_DATA (sym)->ts.u.derived
2472 : 0 : && CLASS_DATA (sym)->ts.u.derived->attr.pdt_type)
2473 : : {
2474 : 0 : tmp = gfc_class_data_get (tmp);
2475 : 0 : tmp = gfc_deallocate_pdt_comp (CLASS_DATA (sym)->ts.u.derived,
2476 : : tmp, 0);
2477 : : }
2478 : 323 : else if (sym->attr.allocatable)
2479 : : {
2480 : 12 : tmp = sym->backend_decl;
2481 : :
2482 : 12 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
2483 : 0 : tmp = gfc_conv_descriptor_data_get (tmp);
2484 : :
2485 : : /* A simple call to free suffices here. */
2486 : 12 : tmp = gfc_call_free (tmp);
2487 : :
2488 : : /* Make sure that reallocation on assignment cannot occur. */
2489 : 12 : sym->attr.allocatable = 0;
2490 : : }
2491 : : else
2492 : : tmp = NULL_TREE;
2493 : :
2494 : 329 : gfc_add_expr_to_block (&final_block, tmp);
2495 : 329 : tmp = gfc_finish_block (&final_block);
2496 : 329 : res = gfc_finish_block (&se.pre);
2497 : 329 : gfc_add_init_cleanup (block, res, tmp);
2498 : 329 : gfc_free_expr (lhs);
2499 : : }
2500 : :
2501 : : /* Set the stringlength, when needed. */
2502 : 6885 : if (need_len_assign)
2503 : : {
2504 : 730 : gfc_se se;
2505 : 730 : gfc_init_se (&se, NULL);
2506 : 730 : if (e->symtree->n.sym->ts.type == BT_CHARACTER)
2507 : : {
2508 : : /* Deferred strings are dealt with in the preceding. */
2509 : 0 : gcc_assert (!e->symtree->n.sym->ts.deferred);
2510 : 0 : tmp = e->symtree->n.sym->ts.u.cl->backend_decl;
2511 : : }
2512 : 730 : else if (e->symtree->n.sym->attr.function
2513 : 12 : && e->symtree->n.sym == e->symtree->n.sym->result)
2514 : : {
2515 : 12 : tmp = gfc_get_fake_result_decl (e->symtree->n.sym, 0);
2516 : 12 : tmp = gfc_class_len_get (tmp);
2517 : : }
2518 : : else
2519 : 718 : tmp = gfc_class_len_get (gfc_get_symbol_decl (e->symtree->n.sym));
2520 : 730 : gfc_get_symbol_decl (sym);
2521 : 730 : charlen = sym->ts.type == BT_CHARACTER ? sym->ts.u.cl->backend_decl
2522 : 0 : : gfc_class_len_get (sym->backend_decl);
2523 : : /* Prevent adding a noop len= len. */
2524 : 730 : if (tmp != charlen)
2525 : : {
2526 : 730 : gfc_add_modify (&se.pre, charlen,
2527 : 730 : fold_convert (TREE_TYPE (charlen), tmp));
2528 : 730 : gfc_add_init_cleanup (block, gfc_finish_block (&se.pre),
2529 : : gfc_finish_block (&se.post));
2530 : : }
2531 : : }
2532 : 6885 : }
2533 : :
2534 : :
2535 : : /* Translate a BLOCK construct. This is basically what we would do for a
2536 : : procedure body. */
2537 : :
2538 : : tree
2539 : 13309 : gfc_trans_block_construct (gfc_code* code)
2540 : : {
2541 : 13309 : gfc_namespace* ns;
2542 : 13309 : gfc_symbol* sym;
2543 : 13309 : gfc_wrapped_block block;
2544 : 13309 : tree exit_label;
2545 : 13309 : stmtblock_t body;
2546 : 13309 : gfc_association_list *ass;
2547 : 13309 : tree translated_body;
2548 : :
2549 : 13309 : ns = code->ext.block.ns;
2550 : 13309 : gcc_assert (ns);
2551 : 13309 : sym = ns->proc_name;
2552 : 13309 : gcc_assert (sym);
2553 : :
2554 : : /* Process local variables. */
2555 : 13309 : gcc_assert (!sym->tlink);
2556 : 13309 : sym->tlink = sym;
2557 : 13309 : gfc_process_block_locals (ns);
2558 : :
2559 : : /* Generate code including exit-label. */
2560 : 13309 : gfc_init_block (&body);
2561 : 13309 : exit_label = gfc_build_label_decl (NULL_TREE);
2562 : 13309 : code->exit_label = exit_label;
2563 : :
2564 : 13309 : finish_oacc_declare (ns, sym, true);
2565 : :
2566 : 13309 : translated_body = gfc_trans_code (ns->code);
2567 : 13309 : if (ns->omp_structured_block)
2568 : 452 : translated_body = build1 (OMP_STRUCTURED_BLOCK, void_type_node,
2569 : : translated_body);
2570 : 13309 : gfc_add_expr_to_block (&body, translated_body);
2571 : 13309 : gfc_add_expr_to_block (&body, build1_v (LABEL_EXPR, exit_label));
2572 : :
2573 : : /* Finish everything. */
2574 : 13309 : gfc_start_wrapped_block (&block, gfc_finish_block (&body));
2575 : 13309 : gfc_trans_deferred_vars (sym, &block);
2576 : 20194 : for (ass = code->ext.block.assoc; ass; ass = ass->next)
2577 : 6885 : trans_associate_var (ass->st->n.sym, &block);
2578 : :
2579 : 13309 : return gfc_finish_wrapped_block (&block);
2580 : : }
2581 : :
2582 : : /* Translate the simple DO construct in a C-style manner.
2583 : : This is where the loop variable has integer type and step +-1.
2584 : : Following code will generate infinite loop in case where TO is INT_MAX
2585 : : (for +1 step) or INT_MIN (for -1 step)
2586 : :
2587 : : We translate a do loop from:
2588 : :
2589 : : DO dovar = from, to, step
2590 : : body
2591 : : END DO
2592 : :
2593 : : to:
2594 : :
2595 : : [Evaluate loop bounds and step]
2596 : : dovar = from;
2597 : : for (;;)
2598 : : {
2599 : : if (dovar > to)
2600 : : goto end_label;
2601 : : body;
2602 : : cycle_label:
2603 : : dovar += step;
2604 : : }
2605 : : end_label:
2606 : :
2607 : : This helps the optimizers by avoiding the extra pre-header condition and
2608 : : we save a register as we just compare the updated IV (not a value in
2609 : : previous step). */
2610 : :
2611 : : static tree
2612 : 24962 : gfc_trans_simple_do (gfc_code * code, stmtblock_t *pblock, tree dovar,
2613 : : tree from, tree to, tree step, tree exit_cond)
2614 : : {
2615 : 24962 : stmtblock_t body;
2616 : 24962 : tree type;
2617 : 24962 : tree cond;
2618 : 24962 : tree tmp;
2619 : 24962 : tree saved_dovar = NULL;
2620 : 24962 : tree cycle_label;
2621 : 24962 : tree exit_label;
2622 : 24962 : location_t loc;
2623 : 24962 : type = TREE_TYPE (dovar);
2624 : 24962 : bool is_step_positive = tree_int_cst_sgn (step) > 0;
2625 : :
2626 : 24962 : loc = gfc_get_location (&code->ext.iterator->start->where);
2627 : :
2628 : : /* Initialize the DO variable: dovar = from. */
2629 : 24962 : gfc_add_modify_loc (loc, pblock, dovar,
2630 : 24962 : fold_convert (TREE_TYPE (dovar), from));
2631 : :
2632 : : /* Save value for do-tinkering checking. */
2633 : 24962 : if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2634 : : {
2635 : 191 : saved_dovar = gfc_create_var (type, ".saved_dovar");
2636 : 191 : gfc_add_modify_loc (loc, pblock, saved_dovar, dovar);
2637 : : }
2638 : :
2639 : : /* Cycle and exit statements are implemented with gotos. */
2640 : 24962 : cycle_label = gfc_build_label_decl (NULL_TREE);
2641 : 24962 : exit_label = gfc_build_label_decl (NULL_TREE);
2642 : :
2643 : : /* Put the labels where they can be found later. See gfc_trans_do(). */
2644 : 24962 : code->cycle_label = cycle_label;
2645 : 24962 : code->exit_label = exit_label;
2646 : :
2647 : : /* Loop body. */
2648 : 24962 : gfc_start_block (&body);
2649 : :
2650 : : /* Exit the loop if there is an I/O result condition or error. */
2651 : 24962 : if (exit_cond)
2652 : : {
2653 : 320 : tmp = build1_v (GOTO_EXPR, exit_label);
2654 : 320 : tmp = fold_build3_loc (loc, COND_EXPR, void_type_node,
2655 : : exit_cond, tmp,
2656 : : build_empty_stmt (loc));
2657 : 320 : gfc_add_expr_to_block (&body, tmp);
2658 : : }
2659 : :
2660 : : /* Evaluate the loop condition. */
2661 : 24962 : if (is_step_positive)
2662 : 24847 : cond = fold_build2_loc (loc, GT_EXPR, logical_type_node, dovar,
2663 : : fold_convert (type, to));
2664 : : else
2665 : 115 : cond = fold_build2_loc (loc, LT_EXPR, logical_type_node, dovar,
2666 : : fold_convert (type, to));
2667 : :
2668 : 24962 : cond = gfc_evaluate_now_loc (loc, cond, &body);
2669 : 24962 : if (code->ext.iterator->annot.unroll && cond != error_mark_node)
2670 : 12 : cond
2671 : 12 : = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2672 : : build_int_cst (integer_type_node, annot_expr_unroll_kind),
2673 : : build_int_cst (integer_type_node,
2674 : 12 : code->ext.iterator->annot.unroll));
2675 : :
2676 : 24962 : if (code->ext.iterator->annot.ivdep && cond != error_mark_node)
2677 : 2 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2678 : : build_int_cst (integer_type_node, annot_expr_ivdep_kind),
2679 : : integer_zero_node);
2680 : 24962 : if (code->ext.iterator->annot.vector && cond != error_mark_node)
2681 : 2 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2682 : : build_int_cst (integer_type_node, annot_expr_vector_kind),
2683 : : integer_zero_node);
2684 : 24962 : if (code->ext.iterator->annot.novector && cond != error_mark_node)
2685 : 2 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2686 : : build_int_cst (integer_type_node, annot_expr_no_vector_kind),
2687 : : integer_zero_node);
2688 : :
2689 : : /* The loop exit. */
2690 : 24962 : tmp = fold_build1_loc (loc, GOTO_EXPR, void_type_node, exit_label);
2691 : 24962 : TREE_USED (exit_label) = 1;
2692 : 24962 : tmp = fold_build3_loc (loc, COND_EXPR, void_type_node,
2693 : : cond, tmp, build_empty_stmt (loc));
2694 : 24962 : gfc_add_expr_to_block (&body, tmp);
2695 : :
2696 : : /* Check whether the induction variable is equal to INT_MAX
2697 : : (respectively to INT_MIN). */
2698 : 24962 : if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2699 : : {
2700 : 191 : tree boundary = is_step_positive ? TYPE_MAX_VALUE (type)
2701 : 191 : : TYPE_MIN_VALUE (type);
2702 : :
2703 : 191 : tmp = fold_build2_loc (loc, EQ_EXPR, logical_type_node,
2704 : : dovar, boundary);
2705 : 191 : gfc_trans_runtime_check (true, false, tmp, &body, &code->loc,
2706 : : "Loop iterates infinitely");
2707 : : }
2708 : :
2709 : : /* Main loop body. */
2710 : 24962 : tmp = gfc_trans_code_cond (code->block->next, exit_cond);
2711 : 24962 : gfc_add_expr_to_block (&body, tmp);
2712 : :
2713 : : /* Label for cycle statements (if needed). */
2714 : 24962 : if (TREE_USED (cycle_label))
2715 : : {
2716 : 24962 : tmp = build1_v (LABEL_EXPR, cycle_label);
2717 : 24962 : gfc_add_expr_to_block (&body, tmp);
2718 : : }
2719 : :
2720 : : /* Check whether someone has modified the loop variable. */
2721 : 24962 : if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2722 : : {
2723 : 191 : tmp = fold_build2_loc (loc, NE_EXPR, logical_type_node,
2724 : : dovar, saved_dovar);
2725 : 191 : gfc_trans_runtime_check (true, false, tmp, &body, &code->loc,
2726 : : "Loop variable has been modified");
2727 : : }
2728 : :
2729 : : /* Increment the loop variable. */
2730 : 24962 : tmp = fold_build2_loc (loc, PLUS_EXPR, type, dovar, step);
2731 : 24962 : gfc_add_modify_loc (loc, &body, dovar, tmp);
2732 : :
2733 : 24962 : if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2734 : 191 : gfc_add_modify_loc (loc, &body, saved_dovar, dovar);
2735 : :
2736 : : /* Finish the loop body. */
2737 : 24962 : tmp = gfc_finish_block (&body);
2738 : 24962 : tmp = fold_build1_loc (loc, LOOP_EXPR, void_type_node, tmp);
2739 : :
2740 : 24962 : gfc_add_expr_to_block (pblock, tmp);
2741 : :
2742 : : /* Add the exit label. */
2743 : 24962 : tmp = build1_v (LABEL_EXPR, exit_label);
2744 : 24962 : gfc_add_expr_to_block (pblock, tmp);
2745 : :
2746 : 24962 : return gfc_finish_block (pblock);
2747 : : }
2748 : :
2749 : : /* Translate the DO construct. This obviously is one of the most
2750 : : important ones to get right with any compiler, but especially
2751 : : so for Fortran.
2752 : :
2753 : : We special case some loop forms as described in gfc_trans_simple_do.
2754 : : For other cases we implement them with a separate loop count,
2755 : : as described in the standard.
2756 : :
2757 : : We translate a do loop from:
2758 : :
2759 : : DO dovar = from, to, step
2760 : : body
2761 : : END DO
2762 : :
2763 : : to:
2764 : :
2765 : : [evaluate loop bounds and step]
2766 : : empty = (step > 0 ? to < from : to > from);
2767 : : countm1 = (to - from) / step;
2768 : : dovar = from;
2769 : : if (empty) goto exit_label;
2770 : : for (;;)
2771 : : {
2772 : : body;
2773 : : cycle_label:
2774 : : dovar += step
2775 : : countm1t = countm1;
2776 : : countm1--;
2777 : : if (countm1t == 0) goto exit_label;
2778 : : }
2779 : : exit_label:
2780 : :
2781 : : countm1 is an unsigned integer. It is equal to the loop count minus one,
2782 : : because the loop count itself can overflow. */
2783 : :
2784 : : tree
2785 : 25997 : gfc_trans_do (gfc_code * code, tree exit_cond)
2786 : : {
2787 : 25997 : gfc_se se;
2788 : 25997 : tree dovar;
2789 : 25997 : tree saved_dovar = NULL;
2790 : 25997 : tree from;
2791 : 25997 : tree to;
2792 : 25997 : tree step;
2793 : 25997 : tree countm1;
2794 : 25997 : tree type;
2795 : 25997 : tree utype;
2796 : 25997 : tree cond;
2797 : 25997 : tree cycle_label;
2798 : 25997 : tree exit_label;
2799 : 25997 : tree tmp;
2800 : 25997 : stmtblock_t block;
2801 : 25997 : stmtblock_t body;
2802 : 25997 : location_t loc;
2803 : :
2804 : 25997 : gfc_start_block (&block);
2805 : :
2806 : 25997 : loc = gfc_get_location (&code->ext.iterator->start->where);
2807 : :
2808 : : /* Evaluate all the expressions in the iterator. */
2809 : 25997 : gfc_init_se (&se, NULL);
2810 : 25997 : gfc_conv_expr_lhs (&se, code->ext.iterator->var);
2811 : 25997 : gfc_add_block_to_block (&block, &se.pre);
2812 : 25997 : dovar = se.expr;
2813 : 25997 : type = TREE_TYPE (dovar);
2814 : :
2815 : 25997 : gfc_init_se (&se, NULL);
2816 : 25997 : gfc_conv_expr_val (&se, code->ext.iterator->start);
2817 : 25997 : gfc_add_block_to_block (&block, &se.pre);
2818 : 25997 : from = gfc_evaluate_now (se.expr, &block);
2819 : :
2820 : 25997 : gfc_init_se (&se, NULL);
2821 : 25997 : gfc_conv_expr_val (&se, code->ext.iterator->end);
2822 : 25997 : gfc_add_block_to_block (&block, &se.pre);
2823 : 25997 : to = gfc_evaluate_now (se.expr, &block);
2824 : :
2825 : 25997 : gfc_init_se (&se, NULL);
2826 : 25997 : gfc_conv_expr_val (&se, code->ext.iterator->step);
2827 : 25997 : gfc_add_block_to_block (&block, &se.pre);
2828 : 25997 : step = gfc_evaluate_now (se.expr, &block);
2829 : :
2830 : 25997 : if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2831 : : {
2832 : 203 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, step,
2833 : : build_zero_cst (type));
2834 : 203 : gfc_trans_runtime_check (true, false, tmp, &block, &code->loc,
2835 : : "DO step value is zero");
2836 : : }
2837 : :
2838 : : /* Special case simple loops. */
2839 : 25997 : if (TREE_CODE (type) == INTEGER_TYPE
2840 : 25997 : && (integer_onep (step)
2841 : 1068 : || tree_int_cst_equal (step, integer_minus_one_node)))
2842 : 24962 : return gfc_trans_simple_do (code, &block, dovar, from, to, step,
2843 : 24962 : exit_cond);
2844 : :
2845 : 1035 : if (TREE_CODE (type) == INTEGER_TYPE)
2846 : 953 : utype = unsigned_type_for (type);
2847 : : else
2848 : 82 : utype = unsigned_type_for (gfc_array_index_type);
2849 : 1035 : countm1 = gfc_create_var (utype, "countm1");
2850 : :
2851 : : /* Cycle and exit statements are implemented with gotos. */
2852 : 1035 : cycle_label = gfc_build_label_decl (NULL_TREE);
2853 : 1035 : exit_label = gfc_build_label_decl (NULL_TREE);
2854 : 1035 : TREE_USED (exit_label) = 1;
2855 : :
2856 : : /* Put these labels where they can be found later. */
2857 : 1035 : code->cycle_label = cycle_label;
2858 : 1035 : code->exit_label = exit_label;
2859 : :
2860 : : /* Initialize the DO variable: dovar = from. */
2861 : 1035 : gfc_add_modify (&block, dovar, from);
2862 : :
2863 : : /* Save value for do-tinkering checking. */
2864 : 1035 : if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2865 : : {
2866 : 12 : saved_dovar = gfc_create_var (type, ".saved_dovar");
2867 : 12 : gfc_add_modify_loc (loc, &block, saved_dovar, dovar);
2868 : : }
2869 : :
2870 : : /* Initialize loop count and jump to exit label if the loop is empty.
2871 : : This code is executed before we enter the loop body. We generate:
2872 : : if (step > 0)
2873 : : {
2874 : : countm1 = (to - from) / step;
2875 : : if (to < from)
2876 : : goto exit_label;
2877 : : }
2878 : : else
2879 : : {
2880 : : countm1 = (from - to) / -step;
2881 : : if (to > from)
2882 : : goto exit_label;
2883 : : }
2884 : : */
2885 : :
2886 : 1035 : if (TREE_CODE (type) == INTEGER_TYPE)
2887 : : {
2888 : 953 : tree pos, neg, tou, fromu, stepu, tmp2;
2889 : :
2890 : : /* The distance from FROM to TO cannot always be represented in a signed
2891 : : type, thus use unsigned arithmetic, also to avoid any undefined
2892 : : overflow issues. */
2893 : 953 : tou = fold_convert (utype, to);
2894 : 953 : fromu = fold_convert (utype, from);
2895 : 953 : stepu = fold_convert (utype, step);
2896 : :
2897 : : /* For a positive step, when to < from, exit, otherwise compute
2898 : : countm1 = ((unsigned)to - (unsigned)from) / (unsigned)step */
2899 : 953 : tmp = fold_build2_loc (loc, LT_EXPR, logical_type_node, to, from);
2900 : 953 : tmp2 = fold_build2_loc (loc, TRUNC_DIV_EXPR, utype,
2901 : : fold_build2_loc (loc, MINUS_EXPR, utype,
2902 : : tou, fromu),
2903 : : stepu);
2904 : 953 : pos = build2 (COMPOUND_EXPR, void_type_node,
2905 : : fold_build2 (MODIFY_EXPR, void_type_node,
2906 : : countm1, tmp2),
2907 : : build3_loc (loc, COND_EXPR, void_type_node,
2908 : : gfc_unlikely (tmp, PRED_FORTRAN_LOOP_PREHEADER),
2909 : : build1_loc (loc, GOTO_EXPR, void_type_node,
2910 : : exit_label), NULL_TREE));
2911 : :
2912 : : /* For a negative step, when to > from, exit, otherwise compute
2913 : : countm1 = ((unsigned)from - (unsigned)to) / -(unsigned)step */
2914 : 953 : tmp = fold_build2_loc (loc, GT_EXPR, logical_type_node, to, from);
2915 : 953 : tmp2 = fold_build2_loc (loc, TRUNC_DIV_EXPR, utype,
2916 : : fold_build2_loc (loc, MINUS_EXPR, utype,
2917 : : fromu, tou),
2918 : : fold_build1_loc (loc, NEGATE_EXPR, utype, stepu));
2919 : 953 : neg = build2 (COMPOUND_EXPR, void_type_node,
2920 : : fold_build2 (MODIFY_EXPR, void_type_node,
2921 : : countm1, tmp2),
2922 : : build3_loc (loc, COND_EXPR, void_type_node,
2923 : : gfc_unlikely (tmp, PRED_FORTRAN_LOOP_PREHEADER),
2924 : : build1_loc (loc, GOTO_EXPR, void_type_node,
2925 : : exit_label), NULL_TREE));
2926 : :
2927 : 953 : tmp = fold_build2_loc (loc, LT_EXPR, logical_type_node, step,
2928 : 953 : build_int_cst (TREE_TYPE (step), 0));
2929 : 953 : tmp = fold_build3_loc (loc, COND_EXPR, void_type_node, tmp, neg, pos);
2930 : :
2931 : 953 : gfc_add_expr_to_block (&block, tmp);
2932 : : }
2933 : : else
2934 : : {
2935 : 82 : tree pos_step;
2936 : :
2937 : : /* TODO: We could use the same width as the real type.
2938 : : This would probably cause more problems that it solves
2939 : : when we implement "long double" types. */
2940 : :
2941 : 82 : tmp = fold_build2_loc (loc, MINUS_EXPR, type, to, from);
2942 : 82 : tmp = fold_build2_loc (loc, RDIV_EXPR, type, tmp, step);
2943 : 82 : tmp = fold_build1_loc (loc, FIX_TRUNC_EXPR, utype, tmp);
2944 : 82 : gfc_add_modify (&block, countm1, tmp);
2945 : :
2946 : : /* We need a special check for empty loops:
2947 : : empty = (step > 0 ? to < from : to > from); */
2948 : 82 : pos_step = fold_build2_loc (loc, GT_EXPR, logical_type_node, step,
2949 : : build_zero_cst (type));
2950 : 82 : tmp = fold_build3_loc (loc, COND_EXPR, logical_type_node, pos_step,
2951 : : fold_build2_loc (loc, LT_EXPR,
2952 : : logical_type_node, to, from),
2953 : : fold_build2_loc (loc, GT_EXPR,
2954 : : logical_type_node, to, from));
2955 : : /* If the loop is empty, go directly to the exit label. */
2956 : 82 : tmp = fold_build3_loc (loc, COND_EXPR, void_type_node, tmp,
2957 : : build1_v (GOTO_EXPR, exit_label),
2958 : : build_empty_stmt (input_location));
2959 : 82 : gfc_add_expr_to_block (&block, tmp);
2960 : : }
2961 : :
2962 : : /* Loop body. */
2963 : 1035 : gfc_start_block (&body);
2964 : :
2965 : : /* Main loop body. */
2966 : 1035 : tmp = gfc_trans_code_cond (code->block->next, exit_cond);
2967 : 1035 : gfc_add_expr_to_block (&body, tmp);
2968 : :
2969 : : /* Label for cycle statements (if needed). */
2970 : 1035 : if (TREE_USED (cycle_label))
2971 : : {
2972 : 1035 : tmp = build1_v (LABEL_EXPR, cycle_label);
2973 : 1035 : gfc_add_expr_to_block (&body, tmp);
2974 : : }
2975 : :
2976 : : /* Check whether someone has modified the loop variable. */
2977 : 1035 : if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2978 : : {
2979 : 12 : tmp = fold_build2_loc (loc, NE_EXPR, logical_type_node, dovar,
2980 : : saved_dovar);
2981 : 12 : gfc_trans_runtime_check (true, false, tmp, &body, &code->loc,
2982 : : "Loop variable has been modified");
2983 : : }
2984 : :
2985 : : /* Exit the loop if there is an I/O result condition or error. */
2986 : 1035 : if (exit_cond)
2987 : : {
2988 : 1 : tmp = build1_v (GOTO_EXPR, exit_label);
2989 : 1 : tmp = fold_build3_loc (loc, COND_EXPR, void_type_node,
2990 : : exit_cond, tmp,
2991 : : build_empty_stmt (input_location));
2992 : 1 : gfc_add_expr_to_block (&body, tmp);
2993 : : }
2994 : :
2995 : : /* Increment the loop variable. */
2996 : 1035 : tmp = fold_build2_loc (loc, PLUS_EXPR, type, dovar, step);
2997 : 1035 : gfc_add_modify_loc (loc, &body, dovar, tmp);
2998 : :
2999 : 1035 : if (gfc_option.rtcheck & GFC_RTCHECK_DO)
3000 : 12 : gfc_add_modify_loc (loc, &body, saved_dovar, dovar);
3001 : :
3002 : : /* Initialize countm1t. */
3003 : 1035 : tree countm1t = gfc_create_var (utype, "countm1t");
3004 : 1035 : gfc_add_modify_loc (loc, &body, countm1t, countm1);
3005 : :
3006 : : /* Decrement the loop count. */
3007 : 1035 : tmp = fold_build2_loc (loc, MINUS_EXPR, utype, countm1,
3008 : : build_int_cst (utype, 1));
3009 : 1035 : gfc_add_modify_loc (loc, &body, countm1, tmp);
3010 : :
3011 : : /* End with the loop condition. Loop until countm1t == 0. */
3012 : 1035 : cond = fold_build2_loc (loc, EQ_EXPR, logical_type_node, countm1t,
3013 : : build_int_cst (utype, 0));
3014 : 1035 : if (code->ext.iterator->annot.unroll && cond != error_mark_node)
3015 : 3 : cond
3016 : 3 : = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
3017 : : build_int_cst (integer_type_node, annot_expr_unroll_kind),
3018 : : build_int_cst (integer_type_node,
3019 : 3 : code->ext.iterator->annot.unroll));
3020 : :
3021 : 1035 : if (code->ext.iterator->annot.ivdep && cond != error_mark_node)
3022 : 0 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
3023 : : build_int_cst (integer_type_node, annot_expr_ivdep_kind),
3024 : : integer_zero_node);
3025 : 1035 : if (code->ext.iterator->annot.vector && cond != error_mark_node)
3026 : 0 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
3027 : : build_int_cst (integer_type_node, annot_expr_vector_kind),
3028 : : integer_zero_node);
3029 : 1035 : if (code->ext.iterator->annot.novector && cond != error_mark_node)
3030 : 0 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
3031 : : build_int_cst (integer_type_node, annot_expr_no_vector_kind),
3032 : : integer_zero_node);
3033 : :
3034 : 1035 : tmp = fold_build1_loc (loc, GOTO_EXPR, void_type_node, exit_label);
3035 : 1035 : tmp = fold_build3_loc (loc, COND_EXPR, void_type_node,
3036 : : cond, tmp, build_empty_stmt (loc));
3037 : 1035 : gfc_add_expr_to_block (&body, tmp);
3038 : :
3039 : : /* End of loop body. */
3040 : 1035 : tmp = gfc_finish_block (&body);
3041 : :
3042 : : /* The for loop itself. */
3043 : 1035 : tmp = fold_build1_loc (loc, LOOP_EXPR, void_type_node, tmp);
3044 : 1035 : gfc_add_expr_to_block (&block, tmp);
3045 : :
3046 : : /* Add the exit label. */
3047 : 1035 : tmp = build1_v (LABEL_EXPR, exit_label);
3048 : 1035 : gfc_add_expr_to_block (&block, tmp);
3049 : :
3050 : 1035 : return gfc_finish_block (&block);
3051 : : }
3052 : :
3053 : :
3054 : : /* Translate the DO WHILE construct.
3055 : :
3056 : : We translate
3057 : :
3058 : : DO WHILE (cond)
3059 : : body
3060 : : END DO
3061 : :
3062 : : to:
3063 : :
3064 : : for ( ; ; )
3065 : : {
3066 : : pre_cond;
3067 : : if (! cond) goto exit_label;
3068 : : body;
3069 : : cycle_label:
3070 : : }
3071 : : exit_label:
3072 : :
3073 : : Because the evaluation of the exit condition `cond' may have side
3074 : : effects, we can't do much for empty loop bodies. The backend optimizers
3075 : : should be smart enough to eliminate any dead loops. */
3076 : :
3077 : : tree
3078 : 495 : gfc_trans_do_while (gfc_code * code)
3079 : : {
3080 : 495 : gfc_se cond;
3081 : 495 : tree tmp;
3082 : 495 : tree cycle_label;
3083 : 495 : tree exit_label;
3084 : 495 : stmtblock_t block;
3085 : :
3086 : : /* Everything we build here is part of the loop body. */
3087 : 495 : gfc_start_block (&block);
3088 : :
3089 : : /* Cycle and exit statements are implemented with gotos. */
3090 : 495 : cycle_label = gfc_build_label_decl (NULL_TREE);
3091 : 495 : exit_label = gfc_build_label_decl (NULL_TREE);
3092 : :
3093 : : /* Put the labels where they can be found later. See gfc_trans_do(). */
3094 : 495 : code->cycle_label = cycle_label;
3095 : 495 : code->exit_label = exit_label;
3096 : :
3097 : : /* Create a GIMPLE version of the exit condition. */
3098 : 495 : gfc_init_se (&cond, NULL);
3099 : 495 : gfc_conv_expr_val (&cond, code->expr1);
3100 : 495 : gfc_add_block_to_block (&block, &cond.pre);
3101 : 495 : cond.expr = fold_build1_loc (gfc_get_location (&code->expr1->where),
3102 : 495 : TRUTH_NOT_EXPR, TREE_TYPE (cond.expr),
3103 : : cond.expr);
3104 : :
3105 : : /* Build "IF (! cond) GOTO exit_label". */
3106 : 495 : tmp = build1_v (GOTO_EXPR, exit_label);
3107 : 495 : TREE_USED (exit_label) = 1;
3108 : 495 : tmp = fold_build3_loc (gfc_get_location (&code->expr1->where), COND_EXPR,
3109 : : void_type_node, cond.expr, tmp,
3110 : : build_empty_stmt (gfc_get_location (
3111 : 495 : &code->expr1->where)));
3112 : 495 : gfc_add_expr_to_block (&block, tmp);
3113 : :
3114 : : /* The main body of the loop. */
3115 : 495 : tmp = gfc_trans_code (code->block->next);
3116 : 495 : gfc_add_expr_to_block (&block, tmp);
3117 : :
3118 : : /* Label for cycle statements (if needed). */
3119 : 495 : if (TREE_USED (cycle_label))
3120 : : {
3121 : 495 : tmp = build1_v (LABEL_EXPR, cycle_label);
3122 : 495 : gfc_add_expr_to_block (&block, tmp);
3123 : : }
3124 : :
3125 : : /* End of loop body. */
3126 : 495 : tmp = gfc_finish_block (&block);
3127 : :
3128 : 495 : gfc_init_block (&block);
3129 : : /* Build the loop. */
3130 : 495 : tmp = fold_build1_loc (gfc_get_location (&code->expr1->where), LOOP_EXPR,
3131 : : void_type_node, tmp);
3132 : 495 : gfc_add_expr_to_block (&block, tmp);
3133 : :
3134 : : /* Add the exit label. */
3135 : 495 : tmp = build1_v (LABEL_EXPR, exit_label);
3136 : 495 : gfc_add_expr_to_block (&block, tmp);
3137 : :
3138 : 495 : return gfc_finish_block (&block);
3139 : : }
3140 : :
3141 : :
3142 : : /* Deal with the particular case of SELECT_TYPE, where the vtable
3143 : : addresses are used for the selection. Since these are not sorted,
3144 : : the selection has to be made by a series of if statements. */
3145 : :
3146 : : static tree
3147 : 2812 : gfc_trans_select_type_cases (gfc_code * code)
3148 : : {
3149 : 2812 : gfc_code *c;
3150 : 2812 : gfc_case *cp;
3151 : 2812 : tree tmp;
3152 : 2812 : tree cond;
3153 : 2812 : tree low;
3154 : 2812 : tree high;
3155 : 2812 : gfc_se se;
3156 : 2812 : gfc_se cse;
3157 : 2812 : stmtblock_t block;
3158 : 2812 : stmtblock_t body;
3159 : 2812 : bool def = false;
3160 : 2812 : gfc_expr *e;
3161 : 2812 : gfc_start_block (&block);
3162 : :
3163 : : /* Calculate the switch expression. */
3164 : 2812 : gfc_init_se (&se, NULL);
3165 : 2812 : gfc_conv_expr_val (&se, code->expr1);
3166 : 2812 : gfc_add_block_to_block (&block, &se.pre);
3167 : :
3168 : : /* Generate an expression for the selector hash value, for
3169 : : use to resolve character cases. */
3170 : 2812 : e = gfc_copy_expr (code->expr1->value.function.actual->expr);
3171 : 2812 : gfc_add_hash_component (e);
3172 : :
3173 : 2812 : TREE_USED (code->exit_label) = 0;
3174 : :
3175 : 5624 : repeat:
3176 : 15568 : for (c = code->block; c; c = c->block)
3177 : : {
3178 : 9944 : cp = c->ext.block.case_list;
3179 : :
3180 : : /* Assume it's the default case. */
3181 : 9944 : low = NULL_TREE;
3182 : 9944 : high = NULL_TREE;
3183 : 9944 : tmp = NULL_TREE;
3184 : :
3185 : : /* Put the default case at the end. */
3186 : 9944 : if ((!def && !cp->low) || (def && cp->low))
3187 : 4972 : continue;
3188 : :
3189 : 4972 : if (cp->low && (cp->ts.type == BT_CLASS
3190 : 3241 : || cp->ts.type == BT_DERIVED))
3191 : : {
3192 : 1871 : gfc_init_se (&cse, NULL);
3193 : 1871 : gfc_conv_expr_val (&cse, cp->low);
3194 : 1871 : gfc_add_block_to_block (&block, &cse.pre);
3195 : 1871 : low = cse.expr;
3196 : : }
3197 : 3101 : else if (cp->ts.type != BT_UNKNOWN)
3198 : : {
3199 : 1370 : gcc_assert (cp->high);
3200 : 1370 : gfc_init_se (&cse, NULL);
3201 : 1370 : gfc_conv_expr_val (&cse, cp->high);
3202 : 1370 : gfc_add_block_to_block (&block, &cse.pre);
3203 : 1370 : high = cse.expr;
3204 : : }
3205 : :
3206 : 4972 : gfc_init_block (&body);
3207 : :
3208 : : /* Add the statements for this case. */
3209 : 4972 : tmp = gfc_trans_code (c->next);
3210 : 4972 : gfc_add_expr_to_block (&body, tmp);
3211 : :
3212 : : /* Break to the end of the SELECT TYPE construct. The default
3213 : : case just falls through. */
3214 : 4972 : if (!def)
3215 : : {
3216 : 3241 : TREE_USED (code->exit_label) = 1;
3217 : 3241 : tmp = build1_v (GOTO_EXPR, code->exit_label);
3218 : 3241 : gfc_add_expr_to_block (&body, tmp);
3219 : : }
3220 : :
3221 : 4972 : tmp = gfc_finish_block (&body);
3222 : :
3223 : 4972 : if (low != NULL_TREE)
3224 : : {
3225 : : /* Compare vtable pointers. */
3226 : 1871 : cond = fold_build2_loc (input_location, EQ_EXPR,
3227 : 1871 : TREE_TYPE (se.expr), se.expr, low);
3228 : 1871 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
3229 : : cond, tmp,
3230 : : build_empty_stmt (input_location));
3231 : : }
3232 : 3101 : else if (high != NULL_TREE)
3233 : : {
3234 : : /* Compare hash values for character cases. */
3235 : 1370 : gfc_init_se (&cse, NULL);
3236 : 1370 : gfc_conv_expr_val (&cse, e);
3237 : 1370 : gfc_add_block_to_block (&block, &cse.pre);
3238 : :
3239 : 1370 : cond = fold_build2_loc (input_location, EQ_EXPR,
3240 : 1370 : TREE_TYPE (se.expr), high, cse.expr);
3241 : 1370 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
3242 : : cond, tmp,
3243 : : build_empty_stmt (input_location));
3244 : : }
3245 : :
3246 : 4972 : gfc_add_expr_to_block (&block, tmp);
3247 : : }
3248 : :
3249 : 5624 : if (!def)
3250 : : {
3251 : 2812 : def = true;
3252 : 2812 : goto repeat;
3253 : : }
3254 : :
3255 : 2812 : gfc_free_expr (e);
3256 : :
3257 : 2812 : return gfc_finish_block (&block);
3258 : : }
3259 : :
3260 : :
3261 : : /* Translate the SELECT CASE construct for INTEGER case expressions,
3262 : : without killing all potential optimizations. The problem is that
3263 : : Fortran allows unbounded cases, but the back-end does not, so we
3264 : : need to intercept those before we enter the equivalent SWITCH_EXPR
3265 : : we can build.
3266 : :
3267 : : For example, we translate this,
3268 : :
3269 : : SELECT CASE (expr)
3270 : : CASE (:100,101,105:115)
3271 : : block_1
3272 : : CASE (190:199,200:)
3273 : : block_2
3274 : : CASE (300)
3275 : : block_3
3276 : : CASE DEFAULT
3277 : : block_4
3278 : : END SELECT
3279 : :
3280 : : to the GENERIC equivalent,
3281 : :
3282 : : switch (expr)
3283 : : {
3284 : : case (minimum value for typeof(expr) ... 100:
3285 : : case 101:
3286 : : case 105 ... 114:
3287 : : block1:
3288 : : goto end_label;
3289 : :
3290 : : case 200 ... (maximum value for typeof(expr):
3291 : : case 190 ... 199:
3292 : : block2;
3293 : : goto end_label;
3294 : :
3295 : : case 300:
3296 : : block_3;
3297 : : goto end_label;
3298 : :
3299 : : default:
3300 : : block_4;
3301 : : goto end_label;
3302 : : }
3303 : :
3304 : : end_label: */
3305 : :
3306 : : static tree
3307 : 872 : gfc_trans_integer_select (gfc_code * code)
3308 : : {
3309 : 872 : gfc_code *c;
3310 : 872 : gfc_case *cp;
3311 : 872 : tree end_label;
3312 : 872 : tree tmp;
3313 : 872 : gfc_se se;
3314 : 872 : stmtblock_t block;
3315 : 872 : stmtblock_t body;
3316 : :
3317 : 872 : gfc_start_block (&block);
3318 : :
3319 : : /* Calculate the switch expression. */
3320 : 872 : gfc_init_se (&se, NULL);
3321 : 872 : gfc_conv_expr_val (&se, code->expr1);
3322 : 872 : gfc_add_block_to_block (&block, &se.pre);
3323 : :
3324 : 872 : end_label = gfc_build_label_decl (NULL_TREE);
3325 : :
3326 : 872 : gfc_init_block (&body);
3327 : :
3328 : 2666 : for (c = code->block; c; c = c->block)
3329 : : {
3330 : 3660 : for (cp = c->ext.block.case_list; cp; cp = cp->next)
3331 : : {
3332 : 1866 : tree low, high;
3333 : 1866 : tree label;
3334 : :
3335 : : /* Assume it's the default case. */
3336 : 1866 : low = high = NULL_TREE;
3337 : :
3338 : 1866 : if (cp->low)
3339 : : {
3340 : 1478 : if (cp->low->ts.type == BT_INTEGER)
3341 : 1436 : low = gfc_conv_mpz_to_tree (cp->low->value.integer,
3342 : : cp->low->ts.kind);
3343 : : else
3344 : 42 : low = gfc_conv_mpz_unsigned_to_tree (cp->low->value.integer,
3345 : : cp->low->ts.kind);
3346 : :
3347 : : /* If there's only a lower bound, set the high bound to the
3348 : : maximum value of the case expression. */
3349 : 1478 : if (!cp->high)
3350 : 45 : high = TYPE_MAX_VALUE (TREE_TYPE (se.expr));
3351 : : }
3352 : :
3353 : 1866 : if (cp->high)
3354 : : {
3355 : : /* Three cases are possible here:
3356 : :
3357 : : 1) There is no lower bound, e.g. CASE (:N).
3358 : : 2) There is a lower bound .NE. high bound, that is
3359 : : a case range, e.g. CASE (N:M) where M>N (we make
3360 : : sure that M>N during type resolution).
3361 : : 3) There is a lower bound, and it has the same value
3362 : : as the high bound, e.g. CASE (N:N). This is our
3363 : : internal representation of CASE(N).
3364 : :
3365 : : In the first and second case, we need to set a value for
3366 : : high. In the third case, we don't because the GCC middle
3367 : : end represents a single case value by just letting high be
3368 : : a NULL_TREE. We can't do that because we need to be able
3369 : : to represent unbounded cases. */
3370 : :
3371 : 1477 : if (!cp->low
3372 : 1433 : || (mpz_cmp (cp->low->value.integer,
3373 : 1433 : cp->high->value.integer) != 0))
3374 : : {
3375 : 73 : if (cp->high->ts.type == BT_INTEGER)
3376 : 73 : high = gfc_conv_mpz_to_tree (cp->high->value.integer,
3377 : : cp->high->ts.kind);
3378 : : else
3379 : 0 : high
3380 : 0 : = gfc_conv_mpz_unsigned_to_tree (cp->high->value.integer,
3381 : : cp->high->ts.kind);
3382 : : }
3383 : :
3384 : : /* Unbounded case. */
3385 : 1477 : if (!cp->low)
3386 : 44 : low = TYPE_MIN_VALUE (TREE_TYPE (se.expr));
3387 : : }
3388 : :
3389 : : /* Build a label. */
3390 : 1866 : label = gfc_build_label_decl (NULL_TREE);
3391 : :
3392 : : /* Add this case label.
3393 : : Add parameter 'label', make it match GCC backend. */
3394 : 1866 : tmp = build_case_label (low, high, label);
3395 : 1866 : gfc_add_expr_to_block (&body, tmp);
3396 : : }
3397 : :
3398 : : /* Add the statements for this case. */
3399 : 1794 : tmp = gfc_trans_code (c->next);
3400 : 1794 : gfc_add_expr_to_block (&body, tmp);
3401 : :
3402 : : /* Break to the end of the construct. */
3403 : 1794 : tmp = build1_v (GOTO_EXPR, end_label);
3404 : 1794 : gfc_add_expr_to_block (&body, tmp);
3405 : : }
3406 : :
3407 : 872 : tmp = gfc_finish_block (&body);
3408 : 872 : tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE, se.expr, tmp);
3409 : 872 : gfc_add_expr_to_block (&block, tmp);
3410 : :
3411 : 872 : tmp = build1_v (LABEL_EXPR, end_label);
3412 : 872 : gfc_add_expr_to_block (&block, tmp);
3413 : :
3414 : 872 : return gfc_finish_block (&block);
3415 : : }
3416 : :
3417 : :
3418 : : /* Translate the SELECT CASE construct for LOGICAL case expressions.
3419 : :
3420 : : There are only two cases possible here, even though the standard
3421 : : does allow three cases in a LOGICAL SELECT CASE construct: .TRUE.,
3422 : : .FALSE., and DEFAULT.
3423 : :
3424 : : We never generate more than two blocks here. Instead, we always
3425 : : try to eliminate the DEFAULT case. This way, we can translate this
3426 : : kind of SELECT construct to a simple
3427 : :
3428 : : if {} else {};
3429 : :
3430 : : expression in GENERIC. */
3431 : :
3432 : : static tree
3433 : 54 : gfc_trans_logical_select (gfc_code * code)
3434 : : {
3435 : 54 : gfc_code *c;
3436 : 54 : gfc_code *t, *f, *d;
3437 : 54 : gfc_case *cp;
3438 : 54 : gfc_se se;
3439 : 54 : stmtblock_t block;
3440 : :
3441 : : /* Assume we don't have any cases at all. */
3442 : 54 : t = f = d = NULL;
3443 : :
3444 : : /* Now see which ones we actually do have. We can have at most two
3445 : : cases in a single case list: one for .TRUE. and one for .FALSE.
3446 : : The default case is always separate. If the cases for .TRUE. and
3447 : : .FALSE. are in the same case list, the block for that case list
3448 : : always executed, and we don't generate code a COND_EXPR. */
3449 : 171 : for (c = code->block; c; c = c->block)
3450 : : {
3451 : 243 : for (cp = c->ext.block.case_list; cp; cp = cp->next)
3452 : : {
3453 : 126 : if (cp->low)
3454 : : {
3455 : 72 : if (cp->low->value.logical == 0) /* .FALSE. */
3456 : : f = c;
3457 : : else /* if (cp->value.logical != 0), thus .TRUE. */
3458 : 36 : t = c;
3459 : : }
3460 : : else
3461 : : d = c;
3462 : : }
3463 : : }
3464 : :
3465 : : /* Start a new block. */
3466 : 54 : gfc_start_block (&block);
3467 : :
3468 : : /* Calculate the switch expression. We always need to do this
3469 : : because it may have side effects. */
3470 : 54 : gfc_init_se (&se, NULL);
3471 : 54 : gfc_conv_expr_val (&se, code->expr1);
3472 : 54 : gfc_add_block_to_block (&block, &se.pre);
3473 : :
3474 : 54 : if (t == f && t != NULL)
3475 : : {
3476 : : /* Cases for .TRUE. and .FALSE. are in the same block. Just
3477 : : translate the code for these cases, append it to the current
3478 : : block. */
3479 : 9 : gfc_add_expr_to_block (&block, gfc_trans_code (t->next));
3480 : : }
3481 : : else
3482 : : {
3483 : 45 : tree true_tree, false_tree, stmt;
3484 : :
3485 : 45 : true_tree = build_empty_stmt (input_location);
3486 : 45 : false_tree = build_empty_stmt (input_location);
3487 : :
3488 : : /* If we have a case for .TRUE. and for .FALSE., discard the default case.
3489 : : Otherwise, if .TRUE. or .FALSE. is missing and there is a default case,
3490 : : make the missing case the default case. */
3491 : 45 : if (t != NULL && f != NULL)
3492 : 63 : d = NULL;
3493 : 36 : else if (d != NULL)
3494 : : {
3495 : 36 : if (t == NULL)
3496 : : t = d;
3497 : : else
3498 : : f = d;
3499 : : }
3500 : :
3501 : : /* Translate the code for each of these blocks, and append it to
3502 : : the current block. */
3503 : 18 : if (t != NULL)
3504 : 45 : true_tree = gfc_trans_code (t->next);
3505 : :
3506 : 45 : if (f != NULL)
3507 : 45 : false_tree = gfc_trans_code (f->next);
3508 : :
3509 : 45 : stmt = fold_build3_loc (input_location, COND_EXPR, void_type_node,
3510 : : se.expr, true_tree, false_tree);
3511 : 45 : gfc_add_expr_to_block (&block, stmt);
3512 : : }
3513 : :
3514 : 54 : return gfc_finish_block (&block);
3515 : : }
3516 : :
3517 : :
3518 : : /* The jump table types are stored in static variables to avoid
3519 : : constructing them from scratch every single time. */
3520 : : static GTY(()) tree select_struct[2];
3521 : :
3522 : : /* Translate the SELECT CASE construct for CHARACTER case expressions.
3523 : : Instead of generating compares and jumps, it is far simpler to
3524 : : generate a data structure describing the cases in order and call a
3525 : : library subroutine that locates the right case.
3526 : : This is particularly true because this is the only case where we
3527 : : might have to dispose of a temporary.
3528 : : The library subroutine returns a pointer to jump to or NULL if no
3529 : : branches are to be taken. */
3530 : :
3531 : : static tree
3532 : 75 : gfc_trans_character_select (gfc_code *code)
3533 : : {
3534 : 75 : tree init, end_label, tmp, type, case_num, label, fndecl;
3535 : 75 : stmtblock_t block, body;
3536 : 75 : gfc_case *cp, *d;
3537 : 75 : gfc_code *c;
3538 : 75 : gfc_se se, expr1se;
3539 : 75 : int n, k;
3540 : 75 : vec<constructor_elt, va_gc> *inits = NULL;
3541 : :
3542 : 75 : tree pchartype = gfc_get_pchar_type (code->expr1->ts.kind);
3543 : :
3544 : : /* The jump table types are stored in static variables to avoid
3545 : : constructing them from scratch every single time. */
3546 : 75 : static tree ss_string1[2], ss_string1_len[2];
3547 : 75 : static tree ss_string2[2], ss_string2_len[2];
3548 : 75 : static tree ss_target[2];
3549 : :
3550 : 75 : cp = code->block->ext.block.case_list;
3551 : 241 : while (cp->left != NULL)
3552 : : cp = cp->left;
3553 : :
3554 : : /* Generate the body */
3555 : 75 : gfc_start_block (&block);
3556 : 75 : gfc_init_se (&expr1se, NULL);
3557 : 75 : gfc_conv_expr_reference (&expr1se, code->expr1);
3558 : :
3559 : 75 : gfc_add_block_to_block (&block, &expr1se.pre);
3560 : :
3561 : 75 : end_label = gfc_build_label_decl (NULL_TREE);
3562 : :
3563 : 75 : gfc_init_block (&body);
3564 : :
3565 : : /* Attempt to optimize length 1 selects. */
3566 : 75 : if (integer_onep (expr1se.string_length))
3567 : : {
3568 : 126 : for (d = cp; d; d = d->right)
3569 : : {
3570 : 110 : gfc_charlen_t i;
3571 : 110 : if (d->low)
3572 : : {
3573 : 100 : gcc_assert (d->low->expr_type == EXPR_CONSTANT
3574 : : && d->low->ts.type == BT_CHARACTER);
3575 : 100 : if (d->low->value.character.length > 1)
3576 : : {
3577 : 2 : for (i = 1; i < d->low->value.character.length; i++)
3578 : 2 : if (d->low->value.character.string[i] != ' ')
3579 : : break;
3580 : 2 : if (i != d->low->value.character.length)
3581 : : {
3582 : 2 : if (optimize && d->high && i == 1)
3583 : : {
3584 : 2 : gcc_assert (d->high->expr_type == EXPR_CONSTANT
3585 : : && d->high->ts.type == BT_CHARACTER);
3586 : 2 : if (d->high->value.character.length > 1
3587 : 2 : && (d->low->value.character.string[0]
3588 : 2 : == d->high->value.character.string[0])
3589 : 2 : && d->high->value.character.string[1] != ' '
3590 : 4 : && ((d->low->value.character.string[1] < ' ')
3591 : : == (d->high->value.character.string[1]
3592 : 2 : < ' ')))
3593 : 2 : continue;
3594 : : }
3595 : : break;
3596 : : }
3597 : : }
3598 : : }
3599 : 108 : if (d->high)
3600 : : {
3601 : 98 : gcc_assert (d->high->expr_type == EXPR_CONSTANT
3602 : : && d->high->ts.type == BT_CHARACTER);
3603 : 98 : if (d->high->value.character.length > 1)
3604 : : {
3605 : 3 : for (i = 1; i < d->high->value.character.length; i++)
3606 : 2 : if (d->high->value.character.string[i] != ' ')
3607 : : break;
3608 : 1 : if (i != d->high->value.character.length)
3609 : : break;
3610 : : }
3611 : : }
3612 : : }
3613 : 16 : if (d == NULL)
3614 : : {
3615 : 16 : tree ctype = gfc_get_char_type (code->expr1->ts.kind);
3616 : :
3617 : 58 : for (c = code->block; c; c = c->block)
3618 : : {
3619 : 152 : for (cp = c->ext.block.case_list; cp; cp = cp->next)
3620 : : {
3621 : 110 : tree low, high;
3622 : 110 : tree label;
3623 : 110 : gfc_char_t r;
3624 : :
3625 : : /* Assume it's the default case. */
3626 : 110 : low = high = NULL_TREE;
3627 : :
3628 : 110 : if (cp->low)
3629 : : {
3630 : : /* CASE ('ab') or CASE ('ab':'az') will never match
3631 : : any length 1 character. */
3632 : 100 : if (cp->low->value.character.length > 1
3633 : 2 : && cp->low->value.character.string[1] != ' ')
3634 : 2 : continue;
3635 : :
3636 : 98 : if (cp->low->value.character.length > 0)
3637 : 97 : r = cp->low->value.character.string[0];
3638 : : else
3639 : : r = ' ';
3640 : 98 : low = build_int_cst (ctype, r);
3641 : :
3642 : : /* If there's only a lower bound, set the high bound
3643 : : to the maximum value of the case expression. */
3644 : 98 : if (!cp->high)
3645 : 0 : high = TYPE_MAX_VALUE (ctype);
3646 : : }
3647 : :
3648 : 108 : if (cp->high)
3649 : : {
3650 : 98 : if (!cp->low
3651 : 98 : || (cp->low->value.character.string[0]
3652 : 98 : != cp->high->value.character.string[0]))
3653 : : {
3654 : 2 : if (cp->high->value.character.length > 0)
3655 : 2 : r = cp->high->value.character.string[0];
3656 : : else
3657 : : r = ' ';
3658 : 2 : high = build_int_cst (ctype, r);
3659 : : }
3660 : :
3661 : : /* Unbounded case. */
3662 : 98 : if (!cp->low)
3663 : 0 : low = TYPE_MIN_VALUE (ctype);
3664 : : }
3665 : :
3666 : : /* Build a label. */
3667 : 108 : label = gfc_build_label_decl (NULL_TREE);
3668 : :
3669 : : /* Add this case label.
3670 : : Add parameter 'label', make it match GCC backend. */
3671 : 108 : tmp = build_case_label (low, high, label);
3672 : 108 : gfc_add_expr_to_block (&body, tmp);
3673 : : }
3674 : :
3675 : : /* Add the statements for this case. */
3676 : 42 : tmp = gfc_trans_code (c->next);
3677 : 42 : gfc_add_expr_to_block (&body, tmp);
3678 : :
3679 : : /* Break to the end of the construct. */
3680 : 42 : tmp = build1_v (GOTO_EXPR, end_label);
3681 : 42 : gfc_add_expr_to_block (&body, tmp);
3682 : : }
3683 : :
3684 : 32 : tmp = gfc_string_to_single_character (expr1se.string_length,
3685 : : expr1se.expr,
3686 : 16 : code->expr1->ts.kind);
3687 : 16 : case_num = gfc_create_var (ctype, "case_num");
3688 : 16 : gfc_add_modify (&block, case_num, tmp);
3689 : :
3690 : 16 : gfc_add_block_to_block (&block, &expr1se.post);
3691 : :
3692 : 16 : tmp = gfc_finish_block (&body);
3693 : 16 : tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE,
3694 : : case_num, tmp);
3695 : 16 : gfc_add_expr_to_block (&block, tmp);
3696 : :
3697 : 16 : tmp = build1_v (LABEL_EXPR, end_label);
3698 : 16 : gfc_add_expr_to_block (&block, tmp);
3699 : :
3700 : 16 : return gfc_finish_block (&block);
3701 : : }
3702 : : }
3703 : :
3704 : 59 : if (code->expr1->ts.kind == 1)
3705 : : k = 0;
3706 : 6 : else if (code->expr1->ts.kind == 4)
3707 : : k = 1;
3708 : : else
3709 : 0 : gcc_unreachable ();
3710 : :
3711 : 59 : if (select_struct[k] == NULL)
3712 : : {
3713 : 53 : tree *chain = NULL;
3714 : 53 : select_struct[k] = make_node (RECORD_TYPE);
3715 : :
3716 : 53 : if (code->expr1->ts.kind == 1)
3717 : 47 : TYPE_NAME (select_struct[k]) = get_identifier ("_jump_struct_char1");
3718 : 6 : else if (code->expr1->ts.kind == 4)
3719 : 6 : TYPE_NAME (select_struct[k]) = get_identifier ("_jump_struct_char4");
3720 : : else
3721 : 0 : gcc_unreachable ();
3722 : :
3723 : : #undef ADD_FIELD
3724 : : #define ADD_FIELD(NAME, TYPE) \
3725 : : ss_##NAME[k] = gfc_add_field_to_struct (select_struct[k], \
3726 : : get_identifier (stringize(NAME)), \
3727 : : TYPE, \
3728 : : &chain)
3729 : :
3730 : 53 : ADD_FIELD (string1, pchartype);
3731 : 53 : ADD_FIELD (string1_len, gfc_charlen_type_node);
3732 : :
3733 : 53 : ADD_FIELD (string2, pchartype);
3734 : 53 : ADD_FIELD (string2_len, gfc_charlen_type_node);
3735 : :
3736 : 53 : ADD_FIELD (target, integer_type_node);
3737 : : #undef ADD_FIELD
3738 : :
3739 : 53 : gfc_finish_type (select_struct[k]);
3740 : : }
3741 : :
3742 : : n = 0;
3743 : 311 : for (d = cp; d; d = d->right)
3744 : 252 : d->n = n++;
3745 : :
3746 : 263 : for (c = code->block; c; c = c->block)
3747 : : {
3748 : 456 : for (d = c->ext.block.case_list; d; d = d->next)
3749 : : {
3750 : 252 : label = gfc_build_label_decl (NULL_TREE);
3751 : 452 : tmp = build_case_label ((d->low == NULL && d->high == NULL)
3752 : : ? NULL
3753 : 200 : : build_int_cst (integer_type_node, d->n),
3754 : : NULL, label);
3755 : 252 : gfc_add_expr_to_block (&body, tmp);
3756 : : }
3757 : :
3758 : 204 : tmp = gfc_trans_code (c->next);
3759 : 204 : gfc_add_expr_to_block (&body, tmp);
3760 : :
3761 : 204 : tmp = build1_v (GOTO_EXPR, end_label);
3762 : 204 : gfc_add_expr_to_block (&body, tmp);
3763 : : }
3764 : :
3765 : : /* Generate the structure describing the branches */
3766 : 311 : for (d = cp; d; d = d->right)
3767 : : {
3768 : 252 : vec<constructor_elt, va_gc> *node = NULL;
3769 : :
3770 : 252 : gfc_init_se (&se, NULL);
3771 : :
3772 : 252 : if (d->low == NULL)
3773 : : {
3774 : 52 : CONSTRUCTOR_APPEND_ELT (node, ss_string1[k], null_pointer_node);
3775 : 52 : CONSTRUCTOR_APPEND_ELT (node, ss_string1_len[k], build_zero_cst (gfc_charlen_type_node));
3776 : : }
3777 : : else
3778 : : {
3779 : 200 : gfc_conv_expr_reference (&se, d->low);
3780 : :
3781 : 200 : CONSTRUCTOR_APPEND_ELT (node, ss_string1[k], se.expr);
3782 : 200 : CONSTRUCTOR_APPEND_ELT (node, ss_string1_len[k], se.string_length);
3783 : : }
3784 : :
3785 : 252 : if (d->high == NULL)
3786 : : {
3787 : 52 : CONSTRUCTOR_APPEND_ELT (node, ss_string2[k], null_pointer_node);
3788 : 52 : CONSTRUCTOR_APPEND_ELT (node, ss_string2_len[k], build_zero_cst (gfc_charlen_type_node));
3789 : : }
3790 : : else
3791 : : {
3792 : 200 : gfc_init_se (&se, NULL);
3793 : 200 : gfc_conv_expr_reference (&se, d->high);
3794 : :
3795 : 200 : CONSTRUCTOR_APPEND_ELT (node, ss_string2[k], se.expr);
3796 : 200 : CONSTRUCTOR_APPEND_ELT (node, ss_string2_len[k], se.string_length);
3797 : : }
3798 : :
3799 : 252 : CONSTRUCTOR_APPEND_ELT (node, ss_target[k],
3800 : : build_int_cst (integer_type_node, d->n));
3801 : :
3802 : 252 : tmp = build_constructor (select_struct[k], node);
3803 : 252 : CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, tmp);
3804 : : }
3805 : :
3806 : 59 : type = build_array_type (select_struct[k],
3807 : 59 : build_index_type (size_int (n-1)));
3808 : :
3809 : 59 : init = build_constructor (type, inits);
3810 : 59 : TREE_CONSTANT (init) = 1;
3811 : 59 : TREE_STATIC (init) = 1;
3812 : : /* Create a static variable to hold the jump table. */
3813 : 59 : tmp = gfc_create_var (type, "jumptable");
3814 : 59 : TREE_CONSTANT (tmp) = 1;
3815 : 59 : TREE_STATIC (tmp) = 1;
3816 : 59 : TREE_READONLY (tmp) = 1;
3817 : 59 : DECL_INITIAL (tmp) = init;
3818 : 59 : init = tmp;
3819 : :
3820 : : /* Build the library call */
3821 : 59 : init = gfc_build_addr_expr (pvoid_type_node, init);
3822 : :
3823 : 59 : if (code->expr1->ts.kind == 1)
3824 : 53 : fndecl = gfor_fndecl_select_string;
3825 : 6 : else if (code->expr1->ts.kind == 4)
3826 : 6 : fndecl = gfor_fndecl_select_string_char4;
3827 : : else
3828 : 0 : gcc_unreachable ();
3829 : :
3830 : 59 : tmp = build_call_expr_loc (input_location,
3831 : : fndecl, 4, init,
3832 : 59 : build_int_cst (gfc_charlen_type_node, n),
3833 : : expr1se.expr, expr1se.string_length);
3834 : 59 : case_num = gfc_create_var (integer_type_node, "case_num");
3835 : 59 : gfc_add_modify (&block, case_num, tmp);
3836 : :
3837 : 59 : gfc_add_block_to_block (&block, &expr1se.post);
3838 : :
3839 : 59 : tmp = gfc_finish_block (&body);
3840 : 59 : tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE,
3841 : : case_num, tmp);
3842 : 59 : gfc_add_expr_to_block (&block, tmp);
3843 : :
3844 : 59 : tmp = build1_v (LABEL_EXPR, end_label);
3845 : 59 : gfc_add_expr_to_block (&block, tmp);
3846 : :
3847 : 59 : return gfc_finish_block (&block);
3848 : : }
3849 : :
3850 : :
3851 : : /* Translate the three variants of the SELECT CASE construct.
3852 : :
3853 : : SELECT CASEs with INTEGER case expressions can be translated to an
3854 : : equivalent GENERIC switch statement, and for LOGICAL case
3855 : : expressions we build one or two if-else compares.
3856 : :
3857 : : SELECT CASEs with CHARACTER case expressions are a whole different
3858 : : story, because they don't exist in GENERIC. So we sort them and
3859 : : do a binary search at runtime.
3860 : :
3861 : : Fortran has no BREAK statement, and it does not allow jumps from
3862 : : one case block to another. That makes things a lot easier for
3863 : : the optimizers. */
3864 : :
3865 : : tree
3866 : 1003 : gfc_trans_select (gfc_code * code)
3867 : : {
3868 : 1003 : stmtblock_t block;
3869 : 1003 : tree body;
3870 : 1003 : tree exit_label;
3871 : :
3872 : 1003 : gcc_assert (code && code->expr1);
3873 : 1003 : gfc_init_block (&block);
3874 : :
3875 : : /* Build the exit label and hang it in. */
3876 : 1003 : exit_label = gfc_build_label_decl (NULL_TREE);
3877 : 1003 : code->exit_label = exit_label;
3878 : :
3879 : : /* Empty SELECT constructs are legal. */
3880 : 1003 : if (code->block == NULL)
3881 : 2 : body = build_empty_stmt (input_location);
3882 : :
3883 : : /* Select the correct translation function. */
3884 : : else
3885 : 1001 : switch (code->expr1->ts.type)
3886 : : {
3887 : 54 : case BT_LOGICAL:
3888 : 54 : body = gfc_trans_logical_select (code);
3889 : 54 : break;
3890 : :
3891 : 872 : case BT_INTEGER:
3892 : 872 : case BT_UNSIGNED:
3893 : 872 : body = gfc_trans_integer_select (code);
3894 : 872 : break;
3895 : :
3896 : 75 : case BT_CHARACTER:
3897 : 75 : body = gfc_trans_character_select (code);
3898 : 75 : break;
3899 : :
3900 : 0 : default:
3901 : 0 : gfc_internal_error ("gfc_trans_select(): Bad type for case expr.");
3902 : : /* Not reached */
3903 : : }
3904 : :
3905 : : /* Build everything together. */
3906 : 1003 : gfc_add_expr_to_block (&block, body);
3907 : 1003 : gfc_add_expr_to_block (&block, build1_v (LABEL_EXPR, exit_label));
3908 : :
3909 : 1003 : return gfc_finish_block (&block);
3910 : : }
3911 : :
3912 : : tree
3913 : 2812 : gfc_trans_select_type (gfc_code * code)
3914 : : {
3915 : 2812 : stmtblock_t block;
3916 : 2812 : tree body;
3917 : 2812 : tree exit_label;
3918 : :
3919 : 2812 : gcc_assert (code && code->expr1);
3920 : 2812 : gfc_init_block (&block);
3921 : :
3922 : : /* Build the exit label and hang it in. */
3923 : 2812 : exit_label = gfc_build_label_decl (NULL_TREE);
3924 : 2812 : code->exit_label = exit_label;
3925 : :
3926 : : /* Empty SELECT constructs are legal. */
3927 : 2812 : if (code->block == NULL)
3928 : 0 : body = build_empty_stmt (input_location);
3929 : : else
3930 : 2812 : body = gfc_trans_select_type_cases (code);
3931 : :
3932 : : /* Build everything together. */
3933 : 2812 : gfc_add_expr_to_block (&block, body);
3934 : :
3935 : 2812 : if (TREE_USED (exit_label))
3936 : 2624 : gfc_add_expr_to_block (&block, build1_v (LABEL_EXPR, exit_label));
3937 : :
3938 : 2812 : return gfc_finish_block (&block);
3939 : : }
3940 : :
3941 : :
3942 : : static tree
3943 : 1000 : gfc_trans_select_rank_cases (gfc_code * code)
3944 : : {
3945 : 1000 : gfc_code *c;
3946 : 1000 : gfc_case *cp;
3947 : 1000 : tree tmp;
3948 : 1000 : tree cond;
3949 : 1000 : tree low;
3950 : 1000 : tree rank;
3951 : 1000 : gfc_se se;
3952 : 1000 : gfc_se cse;
3953 : 1000 : stmtblock_t block;
3954 : 1000 : stmtblock_t body;
3955 : 1000 : bool def = false;
3956 : :
3957 : 1000 : gfc_start_block (&block);
3958 : :
3959 : : /* Calculate the switch expression. */
3960 : 1000 : gfc_init_se (&se, NULL);
3961 : 1000 : gfc_conv_expr_descriptor (&se, code->expr1);
3962 : 1000 : rank = gfc_conv_descriptor_rank (se.expr);
3963 : 1000 : rank = gfc_evaluate_now (rank, &block);
3964 : 1000 : symbol_attribute attr = gfc_expr_attr (code->expr1);
3965 : 1000 : if (!attr.pointer && !attr.allocatable)
3966 : : {
3967 : : /* Special case for assumed-rank ('rank(*)', internally -1):
3968 : : rank = (rank == 0 || ubound[rank-1] != -1) ? rank : -1. */
3969 : 766 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3970 : 766 : rank, build_int_cst (TREE_TYPE (rank), 0));
3971 : 766 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
3972 : : fold_convert (gfc_array_index_type, rank),
3973 : : gfc_index_one_node);
3974 : 766 : tmp = gfc_conv_descriptor_ubound_get (se.expr, tmp);
3975 : 766 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
3976 : 766 : tmp, build_int_cst (TREE_TYPE (tmp), -1));
3977 : 766 : cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
3978 : : logical_type_node, cond, tmp);
3979 : 766 : tmp = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (rank),
3980 : 766 : cond, rank, build_int_cst (TREE_TYPE (rank), -1));
3981 : 766 : rank = gfc_evaluate_now (tmp, &block);
3982 : : }
3983 : 1000 : TREE_USED (code->exit_label) = 0;
3984 : :
3985 : 2000 : repeat:
3986 : 6554 : for (c = code->block; c; c = c->block)
3987 : : {
3988 : 4554 : cp = c->ext.block.case_list;
3989 : :
3990 : : /* Assume it's the default case. */
3991 : 4554 : low = NULL_TREE;
3992 : 4554 : tmp = NULL_TREE;
3993 : :
3994 : : /* Put the default case at the end. */
3995 : 4554 : if ((!def && !cp->low) || (def && cp->low))
3996 : 2277 : continue;
3997 : :
3998 : 2277 : if (cp->low)
3999 : : {
4000 : 1362 : gfc_init_se (&cse, NULL);
4001 : 1362 : gfc_conv_expr_val (&cse, cp->low);
4002 : 1362 : gfc_add_block_to_block (&block, &cse.pre);
4003 : 1362 : low = cse.expr;
4004 : : }
4005 : :
4006 : 2277 : gfc_init_block (&body);
4007 : :
4008 : : /* Add the statements for this case. */
4009 : 2277 : tmp = gfc_trans_code (c->next);
4010 : 2277 : gfc_add_expr_to_block (&body, tmp);
4011 : :
4012 : : /* Break to the end of the SELECT RANK construct. The default
4013 : : case just falls through. */
4014 : 2277 : if (!def)
4015 : : {
4016 : 1362 : TREE_USED (code->exit_label) = 1;
4017 : 1362 : tmp = build1_v (GOTO_EXPR, code->exit_label);
4018 : 1362 : gfc_add_expr_to_block (&body, tmp);
4019 : : }
4020 : :
4021 : 2277 : tmp = gfc_finish_block (&body);
4022 : :
4023 : 2277 : if (low != NULL_TREE)
4024 : : {
4025 : 2724 : cond = fold_build2_loc (input_location, EQ_EXPR,
4026 : 1362 : TREE_TYPE (rank), rank,
4027 : 1362 : fold_convert (TREE_TYPE (rank), low));
4028 : 1362 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
4029 : : cond, tmp,
4030 : : build_empty_stmt (input_location));
4031 : : }
4032 : :
4033 : 2277 : gfc_add_expr_to_block (&block, tmp);
4034 : : }
4035 : :
4036 : 2000 : if (!def)
4037 : : {
4038 : 1000 : def = true;
4039 : 1000 : goto repeat;
4040 : : }
4041 : :
4042 : 1000 : return gfc_finish_block (&block);
4043 : : }
4044 : :
4045 : :
4046 : : tree
4047 : 1001 : gfc_trans_select_rank (gfc_code * code)
4048 : : {
4049 : 1001 : stmtblock_t block;
4050 : 1001 : tree body;
4051 : 1001 : tree exit_label;
4052 : :
4053 : 1001 : gcc_assert (code && code->expr1);
4054 : 1001 : gfc_init_block (&block);
4055 : :
4056 : : /* Build the exit label and hang it in. */
4057 : 1001 : exit_label = gfc_build_label_decl (NULL_TREE);
4058 : 1001 : code->exit_label = exit_label;
4059 : :
4060 : : /* Empty SELECT constructs are legal. */
4061 : 1001 : if (code->block == NULL)
4062 : 1 : body = build_empty_stmt (input_location);
4063 : : else
4064 : 1000 : body = gfc_trans_select_rank_cases (code);
4065 : :
4066 : : /* Build everything together. */
4067 : 1001 : gfc_add_expr_to_block (&block, body);
4068 : :
4069 : 1001 : if (TREE_USED (exit_label))
4070 : 1001 : gfc_add_expr_to_block (&block, build1_v (LABEL_EXPR, exit_label));
4071 : :
4072 : 1001 : return gfc_finish_block (&block);
4073 : : }
4074 : :
4075 : :
4076 : : /* Traversal function to substitute a replacement symtree if the symbol
4077 : : in the expression is the same as that passed. f == 2 signals that
4078 : : that variable itself is not to be checked - only the references.
4079 : : This group of functions is used when the variable expression in a
4080 : : FORALL assignment has internal references. For example:
4081 : : FORALL (i = 1:4) p(p(i)) = i
4082 : : The only recourse here is to store a copy of 'p' for the index
4083 : : expression. */
4084 : :
4085 : : static gfc_symtree *new_symtree;
4086 : : static gfc_symtree *old_symtree;
4087 : :
4088 : : static bool
4089 : 710 : forall_replace (gfc_expr *expr, gfc_symbol *sym, int *f)
4090 : : {
4091 : 710 : if (expr->expr_type != EXPR_VARIABLE)
4092 : : return false;
4093 : :
4094 : 406 : if (*f == 2)
4095 : 62 : *f = 1;
4096 : 344 : else if (expr->symtree->n.sym == sym)
4097 : 72 : expr->symtree = new_symtree;
4098 : :
4099 : : return false;
4100 : : }
4101 : :
4102 : : static void
4103 : 124 : forall_replace_symtree (gfc_expr *e, gfc_symbol *sym, int f)
4104 : : {
4105 : 0 : gfc_traverse_expr (e, sym, forall_replace, f);
4106 : 0 : }
4107 : :
4108 : : static bool
4109 : 710 : forall_restore (gfc_expr *expr,
4110 : : gfc_symbol *sym ATTRIBUTE_UNUSED,
4111 : : int *f ATTRIBUTE_UNUSED)
4112 : : {
4113 : 710 : if (expr->expr_type != EXPR_VARIABLE)
4114 : : return false;
4115 : :
4116 : 406 : if (expr->symtree == new_symtree)
4117 : 72 : expr->symtree = old_symtree;
4118 : :
4119 : : return false;
4120 : : }
4121 : :
4122 : : static void
4123 : 124 : forall_restore_symtree (gfc_expr *e)
4124 : : {
4125 : 0 : gfc_traverse_expr (e, NULL, forall_restore, 0);
4126 : 0 : }
4127 : :
4128 : : static void
4129 : 62 : forall_make_variable_temp (gfc_code *c, stmtblock_t *pre, stmtblock_t *post)
4130 : : {
4131 : 62 : gfc_se tse;
4132 : 62 : gfc_se rse;
4133 : 62 : gfc_expr *e;
4134 : 62 : gfc_symbol *new_sym;
4135 : 62 : gfc_symbol *old_sym;
4136 : 62 : gfc_symtree *root;
4137 : 62 : tree tmp;
4138 : :
4139 : : /* Build a copy of the lvalue. */
4140 : 62 : old_symtree = c->expr1->symtree;
4141 : 62 : old_sym = old_symtree->n.sym;
4142 : 62 : e = gfc_lval_expr_from_sym (old_sym);
4143 : 62 : if (old_sym->attr.dimension)
4144 : : {
4145 : 30 : gfc_init_se (&tse, NULL);
4146 : 30 : gfc_conv_subref_array_arg (&tse, e, 0, INTENT_IN, false);
4147 : 30 : gfc_add_block_to_block (pre, &tse.pre);
4148 : 30 : gfc_add_block_to_block (post, &tse.post);
4149 : 30 : tse.expr = build_fold_indirect_ref_loc (input_location, tse.expr);
4150 : :
4151 : 30 : if (c->expr1->ref->u.ar.type != AR_SECTION)
4152 : : {
4153 : : /* Use the variable offset for the temporary. */
4154 : 24 : tmp = gfc_conv_array_offset (old_sym->backend_decl);
4155 : 24 : gfc_conv_descriptor_offset_set (pre, tse.expr, tmp);
4156 : : }
4157 : : }
4158 : : else
4159 : : {
4160 : 32 : gfc_init_se (&tse, NULL);
4161 : 32 : gfc_init_se (&rse, NULL);
4162 : 32 : gfc_conv_expr (&rse, e);
4163 : 32 : if (e->ts.type == BT_CHARACTER)
4164 : : {
4165 : 32 : tse.string_length = rse.string_length;
4166 : 32 : tmp = gfc_get_character_type_len (gfc_default_character_kind,
4167 : : tse.string_length);
4168 : 32 : tse.expr = gfc_conv_string_tmp (&tse, build_pointer_type (tmp),
4169 : : rse.string_length);
4170 : 32 : gfc_add_block_to_block (pre, &tse.pre);
4171 : 32 : gfc_add_block_to_block (post, &tse.post);
4172 : : }
4173 : : else
4174 : : {
4175 : 0 : tmp = gfc_typenode_for_spec (&e->ts);
4176 : 0 : tse.expr = gfc_create_var (tmp, "temp");
4177 : : }
4178 : :
4179 : 64 : tmp = gfc_trans_scalar_assign (&tse, &rse, e->ts,
4180 : 32 : e->expr_type == EXPR_VARIABLE, false);
4181 : 32 : gfc_add_expr_to_block (pre, tmp);
4182 : : }
4183 : 62 : gfc_free_expr (e);
4184 : :
4185 : : /* Create a new symbol to represent the lvalue. */
4186 : 62 : new_sym = gfc_new_symbol (old_sym->name, NULL);
4187 : 62 : new_sym->ts = old_sym->ts;
4188 : 62 : new_sym->attr.referenced = 1;
4189 : 62 : new_sym->attr.temporary = 1;
4190 : 62 : new_sym->attr.dimension = old_sym->attr.dimension;
4191 : 62 : new_sym->attr.flavor = old_sym->attr.flavor;
4192 : :
4193 : : /* Use the temporary as the backend_decl. */
4194 : 62 : new_sym->backend_decl = tse.expr;
4195 : :
4196 : : /* Create a fake symtree for it. */
4197 : 62 : root = NULL;
4198 : 62 : new_symtree = gfc_new_symtree (&root, old_sym->name);
4199 : 62 : new_symtree->n.sym = new_sym;
4200 : 62 : gcc_assert (new_symtree == root);
4201 : :
4202 : : /* Go through the expression reference replacing the old_symtree
4203 : : with the new. */
4204 : 62 : forall_replace_symtree (c->expr1, old_sym, 2);
4205 : :
4206 : : /* Now we have made this temporary, we might as well use it for
4207 : : the right hand side. */
4208 : 62 : forall_replace_symtree (c->expr2, old_sym, 1);
4209 : 62 : }
4210 : :
4211 : :
4212 : : /* Handles dependencies in forall assignments. */
4213 : : static int
4214 : 1825 : check_forall_dependencies (gfc_code *c, stmtblock_t *pre, stmtblock_t *post)
4215 : : {
4216 : 1825 : gfc_ref *lref;
4217 : 1825 : gfc_ref *rref;
4218 : 1825 : int need_temp;
4219 : 1825 : gfc_symbol *lsym;
4220 : :
4221 : 1825 : lsym = c->expr1->symtree->n.sym;
4222 : 1825 : need_temp = gfc_check_dependency (c->expr1, c->expr2, 0);
4223 : :
4224 : : /* Now check for dependencies within the 'variable'
4225 : : expression itself. These are treated by making a complete
4226 : : copy of variable and changing all the references to it
4227 : : point to the copy instead. Note that the shallow copy of
4228 : : the variable will not suffice for derived types with
4229 : : pointer components. We therefore leave these to their
4230 : : own devices. Likewise for allocatable components. */
4231 : 1825 : if (lsym->ts.type == BT_DERIVED
4232 : 149 : && (lsym->ts.u.derived->attr.pointer_comp
4233 : 139 : || lsym->ts.u.derived->attr.alloc_comp))
4234 : : return need_temp;
4235 : :
4236 : 1730 : new_symtree = NULL;
4237 : 1730 : if (find_forall_index (c->expr1, lsym, 2))
4238 : : {
4239 : 12 : forall_make_variable_temp (c, pre, post);
4240 : 12 : need_temp = 0;
4241 : : }
4242 : :
4243 : : /* Substrings with dependencies are treated in the same
4244 : : way. */
4245 : 1730 : if (c->expr1->ts.type == BT_CHARACTER
4246 : 685 : && c->expr1->ref
4247 : 685 : && c->expr2->expr_type == EXPR_VARIABLE
4248 : 492 : && lsym == c->expr2->symtree->n.sym)
4249 : : {
4250 : 124 : for (lref = c->expr1->ref; lref; lref = lref->next)
4251 : 117 : if (lref->type == REF_SUBSTRING)
4252 : : break;
4253 : 124 : for (rref = c->expr2->ref; rref; rref = rref->next)
4254 : 117 : if (rref->type == REF_SUBSTRING)
4255 : : break;
4256 : :
4257 : 81 : if (rref && lref
4258 : 81 : && gfc_dep_compare_expr (rref->u.ss.start, lref->u.ss.start) < 0)
4259 : : {
4260 : 50 : forall_make_variable_temp (c, pre, post);
4261 : 50 : need_temp = 0;
4262 : : }
4263 : : }
4264 : : return need_temp;
4265 : : }
4266 : :
4267 : :
4268 : : static void
4269 : 62 : cleanup_forall_symtrees (gfc_code *c)
4270 : : {
4271 : 62 : forall_restore_symtree (c->expr1);
4272 : 62 : forall_restore_symtree (c->expr2);
4273 : 62 : free (new_symtree->n.sym);
4274 : 62 : free (new_symtree);
4275 : 62 : }
4276 : :
4277 : :
4278 : : /* Generate the loops for a FORALL block, specified by FORALL_TMP. BODY
4279 : : is the contents of the FORALL block/stmt to be iterated. MASK_FLAG
4280 : : indicates whether we should generate code to test the FORALLs mask
4281 : : array. OUTER is the loop header to be used for initializing mask
4282 : : indices.
4283 : :
4284 : : The generated loop format is:
4285 : : count = (end - start + step) / step
4286 : : loopvar = start
4287 : : while (1)
4288 : : {
4289 : : if (count <=0 )
4290 : : goto end_of_loop
4291 : : <body>
4292 : : loopvar += step
4293 : : count --
4294 : : }
4295 : : end_of_loop: */
4296 : :
4297 : : static tree
4298 : 3492 : gfc_trans_forall_loop (forall_info *forall_tmp, tree body,
4299 : : int mask_flag, stmtblock_t *outer)
4300 : : {
4301 : 3492 : int n, nvar;
4302 : 3492 : tree tmp;
4303 : 3492 : tree cond;
4304 : 3492 : stmtblock_t block;
4305 : 3492 : tree exit_label;
4306 : 3492 : tree count;
4307 : 3492 : tree var, start, end, step;
4308 : 3492 : iter_info *iter;
4309 : :
4310 : : /* Initialize the mask index outside the FORALL nest. */
4311 : 3492 : if (mask_flag && forall_tmp->mask)
4312 : 1067 : gfc_add_modify (outer, forall_tmp->maskindex, gfc_index_zero_node);
4313 : :
4314 : 3492 : iter = forall_tmp->this_loop;
4315 : 3492 : nvar = forall_tmp->nvar;
4316 : 9653 : for (n = 0; n < nvar; n++)
4317 : : {
4318 : 6161 : var = iter->var;
4319 : 6161 : start = iter->start;
4320 : 6161 : end = iter->end;
4321 : 6161 : step = iter->step;
4322 : :
4323 : 6161 : exit_label = gfc_build_label_decl (NULL_TREE);
4324 : 6161 : TREE_USED (exit_label) = 1;
4325 : :
4326 : : /* The loop counter. */
4327 : 6161 : count = gfc_create_var (TREE_TYPE (var), "count");
4328 : :
4329 : : /* The body of the loop. */
4330 : 6161 : gfc_init_block (&block);
4331 : :
4332 : : /* The exit condition. */
4333 : 6161 : cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
4334 : 6161 : count, build_int_cst (TREE_TYPE (count), 0));
4335 : :
4336 : : /* PR 83064 means that we cannot use annot_expr_parallel_kind until
4337 : : the autoparallelizer can handle this. */
4338 : 6161 : if (forall_tmp->do_concurrent || iter->annot.ivdep)
4339 : 124 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
4340 : : build_int_cst (integer_type_node,
4341 : : annot_expr_ivdep_kind),
4342 : : integer_zero_node);
4343 : :
4344 : 6161 : if (iter->annot.unroll && cond != error_mark_node)
4345 : 1 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
4346 : : build_int_cst (integer_type_node,
4347 : : annot_expr_unroll_kind),
4348 : 1 : build_int_cst (integer_type_node, iter->annot.unroll));
4349 : :
4350 : 6161 : if (iter->annot.vector && cond != error_mark_node)
4351 : 1 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
4352 : : build_int_cst (integer_type_node,
4353 : : annot_expr_vector_kind),
4354 : : integer_zero_node);
4355 : :
4356 : 6161 : if (iter->annot.novector && cond != error_mark_node)
4357 : 2 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
4358 : : build_int_cst (integer_type_node,
4359 : : annot_expr_no_vector_kind),
4360 : : integer_zero_node);
4361 : :
4362 : 6161 : tmp = build1_v (GOTO_EXPR, exit_label);
4363 : 6161 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
4364 : : cond, tmp, build_empty_stmt (input_location));
4365 : 6161 : gfc_add_expr_to_block (&block, tmp);
4366 : :
4367 : : /* The main loop body. */
4368 : 6161 : gfc_add_expr_to_block (&block, body);
4369 : :
4370 : : /* Increment the loop variable. */
4371 : 6161 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (var), var,
4372 : : step);
4373 : 6161 : gfc_add_modify (&block, var, tmp);
4374 : :
4375 : : /* Advance to the next mask element. Only do this for the
4376 : : innermost loop. */
4377 : 6161 : if (n == 0 && mask_flag && forall_tmp->mask)
4378 : : {
4379 : 1067 : tree maskindex = forall_tmp->maskindex;
4380 : 1067 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4381 : : maskindex, gfc_index_one_node);
4382 : 1067 : gfc_add_modify (&block, maskindex, tmp);
4383 : : }
4384 : :
4385 : : /* Decrement the loop counter. */
4386 : 6161 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (var), count,
4387 : 6161 : build_int_cst (TREE_TYPE (var), 1));
4388 : 6161 : gfc_add_modify (&block, count, tmp);
4389 : :
4390 : 6161 : body = gfc_finish_block (&block);
4391 : :
4392 : : /* Loop var initialization. */
4393 : 6161 : gfc_init_block (&block);
4394 : 6161 : gfc_add_modify (&block, var, start);
4395 : :
4396 : :
4397 : : /* Initialize the loop counter. */
4398 : 6161 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (var), step,
4399 : : start);
4400 : 6161 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (var), end,
4401 : : tmp);
4402 : 6161 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR, TREE_TYPE (var),
4403 : : tmp, step);
4404 : 6161 : gfc_add_modify (&block, count, tmp);
4405 : :
4406 : : /* The loop expression. */
4407 : 6161 : tmp = build1_v (LOOP_EXPR, body);
4408 : 6161 : gfc_add_expr_to_block (&block, tmp);
4409 : :
4410 : : /* The exit label. */
4411 : 6161 : tmp = build1_v (LABEL_EXPR, exit_label);
4412 : 6161 : gfc_add_expr_to_block (&block, tmp);
4413 : :
4414 : 6161 : body = gfc_finish_block (&block);
4415 : 6161 : iter = iter->next;
4416 : : }
4417 : 3492 : return body;
4418 : : }
4419 : :
4420 : :
4421 : : /* Generate the body and loops according to MASK_FLAG. If MASK_FLAG
4422 : : is nonzero, the body is controlled by all masks in the forall nest.
4423 : : Otherwise, the innermost loop is not controlled by it's mask. This
4424 : : is used for initializing that mask. */
4425 : :
4426 : : static tree
4427 : 3292 : gfc_trans_nested_forall_loop (forall_info * nested_forall_info, tree body,
4428 : : int mask_flag)
4429 : : {
4430 : 3292 : tree tmp;
4431 : 3292 : stmtblock_t header;
4432 : 3292 : forall_info *forall_tmp;
4433 : 3292 : tree mask, maskindex;
4434 : :
4435 : 3292 : gfc_start_block (&header);
4436 : :
4437 : 3292 : forall_tmp = nested_forall_info;
4438 : 10076 : while (forall_tmp != NULL)
4439 : : {
4440 : : /* Generate body with masks' control. */
4441 : 3492 : if (mask_flag)
4442 : : {
4443 : 2769 : mask = forall_tmp->mask;
4444 : 2769 : maskindex = forall_tmp->maskindex;
4445 : :
4446 : : /* If a mask was specified make the assignment conditional. */
4447 : 2769 : if (mask)
4448 : : {
4449 : 1067 : tmp = gfc_build_array_ref (mask, maskindex, NULL);
4450 : 1067 : body = build3_v (COND_EXPR, tmp, body,
4451 : : build_empty_stmt (input_location));
4452 : : }
4453 : : }
4454 : 3492 : body = gfc_trans_forall_loop (forall_tmp, body, mask_flag, &header);
4455 : 3492 : forall_tmp = forall_tmp->prev_nest;
4456 : 3492 : mask_flag = 1;
4457 : : }
4458 : :
4459 : 3292 : gfc_add_expr_to_block (&header, body);
4460 : 3292 : return gfc_finish_block (&header);
4461 : : }
4462 : :
4463 : :
4464 : : /* Allocate data for holding a temporary array. Returns either a local
4465 : : temporary array or a pointer variable. */
4466 : :
4467 : : static tree
4468 : 1381 : gfc_do_allocate (tree bytesize, tree size, tree * pdata, stmtblock_t * pblock,
4469 : : tree elem_type)
4470 : : {
4471 : 1381 : tree tmpvar;
4472 : 1381 : tree type;
4473 : 1381 : tree tmp;
4474 : :
4475 : 1381 : if (INTEGER_CST_P (size))
4476 : 1139 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
4477 : : size, gfc_index_one_node);
4478 : : else
4479 : : tmp = NULL_TREE;
4480 : :
4481 : 1381 : type = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
4482 : 1381 : type = build_array_type (elem_type, type);
4483 : 1381 : if (gfc_can_put_var_on_stack (bytesize) && INTEGER_CST_P (size))
4484 : : {
4485 : 1139 : tmpvar = gfc_create_var (type, "temp");
4486 : 1139 : *pdata = NULL_TREE;
4487 : : }
4488 : : else
4489 : : {
4490 : 242 : tmpvar = gfc_create_var (build_pointer_type (type), "temp");
4491 : 242 : *pdata = convert (pvoid_type_node, tmpvar);
4492 : :
4493 : 242 : tmp = gfc_call_malloc (pblock, TREE_TYPE (tmpvar), bytesize);
4494 : 242 : gfc_add_modify (pblock, tmpvar, tmp);
4495 : : }
4496 : 1381 : return tmpvar;
4497 : : }
4498 : :
4499 : :
4500 : : /* Generate codes to copy the temporary to the actual lhs. */
4501 : :
4502 : : static tree
4503 : 224 : generate_loop_for_temp_to_lhs (gfc_expr *expr, tree tmp1, tree count3,
4504 : : tree count1,
4505 : : gfc_ss *lss, gfc_ss *rss,
4506 : : tree wheremask, bool invert)
4507 : : {
4508 : 224 : stmtblock_t block, body1;
4509 : 224 : gfc_loopinfo loop;
4510 : 224 : gfc_se lse;
4511 : 224 : gfc_se rse;
4512 : 224 : tree tmp;
4513 : 224 : tree wheremaskexpr;
4514 : :
4515 : 224 : (void) rss; /* TODO: unused. */
4516 : :
4517 : 224 : gfc_start_block (&block);
4518 : :
4519 : 224 : gfc_init_se (&rse, NULL);
4520 : 224 : gfc_init_se (&lse, NULL);
4521 : :
4522 : 224 : if (lss == gfc_ss_terminator)
4523 : : {
4524 : 149 : gfc_init_block (&body1);
4525 : 149 : gfc_conv_expr (&lse, expr);
4526 : 149 : rse.expr = gfc_build_array_ref (tmp1, count1, NULL);
4527 : : }
4528 : : else
4529 : : {
4530 : : /* Initialize the loop. */
4531 : 75 : gfc_init_loopinfo (&loop);
4532 : :
4533 : : /* We may need LSS to determine the shape of the expression. */
4534 : 75 : gfc_add_ss_to_loop (&loop, lss);
4535 : :
4536 : 75 : gfc_conv_ss_startstride (&loop);
4537 : 75 : gfc_conv_loop_setup (&loop, &expr->where);
4538 : :
4539 : 75 : gfc_mark_ss_chain_used (lss, 1);
4540 : : /* Start the loop body. */
4541 : 75 : gfc_start_scalarized_body (&loop, &body1);
4542 : :
4543 : : /* Translate the expression. */
4544 : 75 : gfc_copy_loopinfo_to_se (&lse, &loop);
4545 : 75 : lse.ss = lss;
4546 : 75 : gfc_conv_expr (&lse, expr);
4547 : :
4548 : : /* Form the expression of the temporary. */
4549 : 75 : rse.expr = gfc_build_array_ref (tmp1, count1, NULL);
4550 : : }
4551 : :
4552 : : /* Use the scalar assignment. */
4553 : 224 : rse.string_length = lse.string_length;
4554 : 448 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts,
4555 : 224 : expr->expr_type == EXPR_VARIABLE, false);
4556 : :
4557 : : /* Form the mask expression according to the mask tree list. */
4558 : 224 : if (wheremask)
4559 : : {
4560 : 27 : wheremaskexpr = gfc_build_array_ref (wheremask, count3, NULL);
4561 : 27 : if (invert)
4562 : 0 : wheremaskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
4563 : 0 : TREE_TYPE (wheremaskexpr),
4564 : : wheremaskexpr);
4565 : 27 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
4566 : : wheremaskexpr, tmp,
4567 : : build_empty_stmt (input_location));
4568 : : }
4569 : :
4570 : 224 : gfc_add_expr_to_block (&body1, tmp);
4571 : :
4572 : 224 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (count1),
4573 : : count1, gfc_index_one_node);
4574 : 224 : gfc_add_modify (&body1, count1, tmp);
4575 : :
4576 : 224 : if (lss == gfc_ss_terminator)
4577 : 149 : gfc_add_block_to_block (&block, &body1);
4578 : : else
4579 : : {
4580 : : /* Increment count3. */
4581 : 75 : if (count3)
4582 : : {
4583 : 27 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
4584 : : gfc_array_index_type,
4585 : : count3, gfc_index_one_node);
4586 : 27 : gfc_add_modify (&body1, count3, tmp);
4587 : : }
4588 : :
4589 : : /* Generate the copying loops. */
4590 : 75 : gfc_trans_scalarizing_loops (&loop, &body1);
4591 : :
4592 : 75 : gfc_add_block_to_block (&block, &loop.pre);
4593 : 75 : gfc_add_block_to_block (&block, &loop.post);
4594 : :
4595 : 75 : gfc_cleanup_loop (&loop);
4596 : : /* TODO: Reuse lss and rss when copying temp->lhs. Need to be careful
4597 : : as tree nodes in SS may not be valid in different scope. */
4598 : : }
4599 : :
4600 : 224 : tmp = gfc_finish_block (&block);
4601 : 224 : return tmp;
4602 : : }
4603 : :
4604 : :
4605 : : /* Generate codes to copy rhs to the temporary. TMP1 is the address of
4606 : : temporary, LSS and RSS are formed in function compute_inner_temp_size(),
4607 : : and should not be freed. WHEREMASK is the conditional execution mask
4608 : : whose sense may be inverted by INVERT. */
4609 : :
4610 : : static tree
4611 : 224 : generate_loop_for_rhs_to_temp (gfc_expr *expr2, tree tmp1, tree count3,
4612 : : tree count1, gfc_ss *lss, gfc_ss *rss,
4613 : : tree wheremask, bool invert)
4614 : : {
4615 : 224 : stmtblock_t block, body1;
4616 : 224 : gfc_loopinfo loop;
4617 : 224 : gfc_se lse;
4618 : 224 : gfc_se rse;
4619 : 224 : tree tmp;
4620 : 224 : tree wheremaskexpr;
4621 : :
4622 : 224 : gfc_start_block (&block);
4623 : :
4624 : 224 : gfc_init_se (&rse, NULL);
4625 : 224 : gfc_init_se (&lse, NULL);
4626 : :
4627 : 224 : if (lss == gfc_ss_terminator)
4628 : : {
4629 : 149 : gfc_init_block (&body1);
4630 : 149 : gfc_conv_expr (&rse, expr2);
4631 : 149 : lse.expr = gfc_build_array_ref (tmp1, count1, NULL);
4632 : : }
4633 : : else
4634 : : {
4635 : : /* Initialize the loop. */
4636 : 75 : gfc_init_loopinfo (&loop);
4637 : :
4638 : : /* We may need LSS to determine the shape of the expression. */
4639 : 75 : gfc_add_ss_to_loop (&loop, lss);
4640 : 75 : gfc_add_ss_to_loop (&loop, rss);
4641 : :
4642 : 75 : gfc_conv_ss_startstride (&loop);
4643 : 75 : gfc_conv_loop_setup (&loop, &expr2->where);
4644 : :
4645 : 75 : gfc_mark_ss_chain_used (rss, 1);
4646 : : /* Start the loop body. */
4647 : 75 : gfc_start_scalarized_body (&loop, &body1);
4648 : :
4649 : : /* Translate the expression. */
4650 : 75 : gfc_copy_loopinfo_to_se (&rse, &loop);
4651 : 75 : rse.ss = rss;
4652 : 75 : gfc_conv_expr (&rse, expr2);
4653 : :
4654 : : /* Form the expression of the temporary. */
4655 : 75 : lse.expr = gfc_build_array_ref (tmp1, count1, NULL);
4656 : : }
4657 : :
4658 : : /* Use the scalar assignment. */
4659 : 224 : lse.string_length = rse.string_length;
4660 : 448 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr2->ts,
4661 : 224 : expr2->expr_type == EXPR_VARIABLE, false);
4662 : :
4663 : : /* Form the mask expression according to the mask tree list. */
4664 : 224 : if (wheremask)
4665 : : {
4666 : 27 : wheremaskexpr = gfc_build_array_ref (wheremask, count3, NULL);
4667 : 27 : if (invert)
4668 : 0 : wheremaskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
4669 : 0 : TREE_TYPE (wheremaskexpr),
4670 : : wheremaskexpr);
4671 : 27 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
4672 : : wheremaskexpr, tmp,
4673 : : build_empty_stmt (input_location));
4674 : : }
4675 : :
4676 : 224 : gfc_add_expr_to_block (&body1, tmp);
4677 : :
4678 : 224 : if (lss == gfc_ss_terminator)
4679 : : {
4680 : 149 : gfc_add_block_to_block (&block, &body1);
4681 : :
4682 : : /* Increment count1. */
4683 : 149 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (count1),
4684 : : count1, gfc_index_one_node);
4685 : 149 : gfc_add_modify (&block, count1, tmp);
4686 : : }
4687 : : else
4688 : : {
4689 : : /* Increment count1. */
4690 : 75 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4691 : : count1, gfc_index_one_node);
4692 : 75 : gfc_add_modify (&body1, count1, tmp);
4693 : :
4694 : : /* Increment count3. */
4695 : 75 : if (count3)
4696 : : {
4697 : 27 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
4698 : : gfc_array_index_type,
4699 : : count3, gfc_index_one_node);
4700 : 27 : gfc_add_modify (&body1, count3, tmp);
4701 : : }
4702 : :
4703 : : /* Generate the copying loops. */
4704 : 75 : gfc_trans_scalarizing_loops (&loop, &body1);
4705 : :
4706 : 75 : gfc_add_block_to_block (&block, &loop.pre);
4707 : 75 : gfc_add_block_to_block (&block, &loop.post);
4708 : :
4709 : 75 : gfc_cleanup_loop (&loop);
4710 : : /* TODO: Reuse lss and rss when copying temp->lhs. Need to be careful
4711 : : as tree nodes in SS may not be valid in different scope. */
4712 : : }
4713 : :
4714 : 224 : tmp = gfc_finish_block (&block);
4715 : 224 : return tmp;
4716 : : }
4717 : :
4718 : :
4719 : : /* Calculate the size of temporary needed in the assignment inside forall.
4720 : : LSS and RSS are filled in this function. */
4721 : :
4722 : : static tree
4723 : 779 : compute_inner_temp_size (gfc_expr *expr1, gfc_expr *expr2,
4724 : : stmtblock_t * pblock,
4725 : : gfc_ss **lss, gfc_ss **rss)
4726 : : {
4727 : 779 : gfc_loopinfo loop;
4728 : 779 : tree size;
4729 : 779 : int i;
4730 : 779 : int save_flag;
4731 : 779 : tree tmp;
4732 : :
4733 : 779 : *lss = gfc_walk_expr (expr1);
4734 : 779 : *rss = NULL;
4735 : :
4736 : 779 : size = gfc_index_one_node;
4737 : 779 : if (*lss != gfc_ss_terminator)
4738 : : {
4739 : 481 : gfc_init_loopinfo (&loop);
4740 : :
4741 : : /* Walk the RHS of the expression. */
4742 : 481 : *rss = gfc_walk_expr (expr2);
4743 : 481 : if (*rss == gfc_ss_terminator)
4744 : : /* The rhs is scalar. Add a ss for the expression. */
4745 : 0 : *rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
4746 : :
4747 : : /* Associate the SS with the loop. */
4748 : 481 : gfc_add_ss_to_loop (&loop, *lss);
4749 : : /* We don't actually need to add the rhs at this point, but it might
4750 : : make guessing the loop bounds a bit easier. */
4751 : 481 : gfc_add_ss_to_loop (&loop, *rss);
4752 : :
4753 : : /* We only want the shape of the expression, not rest of the junk
4754 : : generated by the scalarizer. */
4755 : 481 : loop.array_parameter = 1;
4756 : :
4757 : : /* Calculate the bounds of the scalarization. */
4758 : 481 : save_flag = gfc_option.rtcheck;
4759 : 481 : gfc_option.rtcheck &= ~GFC_RTCHECK_BOUNDS;
4760 : 481 : gfc_conv_ss_startstride (&loop);
4761 : 481 : gfc_option.rtcheck = save_flag;
4762 : 481 : gfc_conv_loop_setup (&loop, &expr2->where);
4763 : :
4764 : : /* Figure out how many elements we need. */
4765 : 1473 : for (i = 0; i < loop.dimen; i++)
4766 : : {
4767 : 511 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
4768 : : gfc_array_index_type,
4769 : : gfc_index_one_node, loop.from[i]);
4770 : 511 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
4771 : : gfc_array_index_type, tmp, loop.to[i]);
4772 : 511 : size = fold_build2_loc (input_location, MULT_EXPR,
4773 : : gfc_array_index_type, size, tmp);
4774 : : }
4775 : 481 : gfc_add_block_to_block (pblock, &loop.pre);
4776 : 481 : size = gfc_evaluate_now (size, pblock);
4777 : 481 : gfc_add_block_to_block (pblock, &loop.post);
4778 : :
4779 : : /* TODO: write a function that cleans up a loopinfo without freeing
4780 : : the SS chains. Currently a NOP. */
4781 : : }
4782 : :
4783 : 779 : return size;
4784 : : }
4785 : :
4786 : :
4787 : : /* Calculate the overall iterator number of the nested forall construct.
4788 : : This routine actually calculates the number of times the body of the
4789 : : nested forall specified by NESTED_FORALL_INFO is executed and multiplies
4790 : : that by the expression INNER_SIZE. The BLOCK argument specifies the
4791 : : block in which to calculate the result, and the optional INNER_SIZE_BODY
4792 : : argument contains any statements that need to executed (inside the loop)
4793 : : to initialize or calculate INNER_SIZE. */
4794 : :
4795 : : static tree
4796 : 1298 : compute_overall_iter_number (forall_info *nested_forall_info, tree inner_size,
4797 : : stmtblock_t *inner_size_body, stmtblock_t *block)
4798 : : {
4799 : 1298 : forall_info *forall_tmp = nested_forall_info;
4800 : 1298 : tree tmp, number;
4801 : 1298 : stmtblock_t body;
4802 : :
4803 : : /* We can eliminate the innermost unconditional loops with constant
4804 : : array bounds. */
4805 : 1298 : if (INTEGER_CST_P (inner_size))
4806 : : {
4807 : : while (forall_tmp
4808 : 267 : && !forall_tmp->mask
4809 : 1506 : && INTEGER_CST_P (forall_tmp->size))
4810 : : {
4811 : 117 : inner_size = fold_build2_loc (input_location, MULT_EXPR,
4812 : : gfc_array_index_type,
4813 : : inner_size, forall_tmp->size);
4814 : 117 : forall_tmp = forall_tmp->prev_nest;
4815 : : }
4816 : :
4817 : : /* If there are no loops left, we have our constant result. */
4818 : 1207 : if (!forall_tmp)
4819 : : return inner_size;
4820 : : }
4821 : :
4822 : : /* Otherwise, create a temporary variable to compute the result. */
4823 : 241 : number = gfc_create_var (gfc_array_index_type, "num");
4824 : 241 : gfc_add_modify (block, number, gfc_index_zero_node);
4825 : :
4826 : 241 : gfc_start_block (&body);
4827 : 241 : if (inner_size_body)
4828 : 188 : gfc_add_block_to_block (&body, inner_size_body);
4829 : 241 : if (forall_tmp)
4830 : 225 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
4831 : : gfc_array_index_type, number, inner_size);
4832 : : else
4833 : : tmp = inner_size;
4834 : 241 : gfc_add_modify (&body, number, tmp);
4835 : 241 : tmp = gfc_finish_block (&body);
4836 : :
4837 : : /* Generate loops. */
4838 : 241 : if (forall_tmp != NULL)
4839 : 225 : tmp = gfc_trans_nested_forall_loop (forall_tmp, tmp, 1);
4840 : :
4841 : 241 : gfc_add_expr_to_block (block, tmp);
4842 : :
4843 : 241 : return number;
4844 : : }
4845 : :
4846 : :
4847 : : /* Allocate temporary for forall construct. SIZE is the size of temporary
4848 : : needed. PTEMP1 is returned for space free. */
4849 : :
4850 : : static tree
4851 : 1381 : allocate_temp_for_forall_nest_1 (tree type, tree size, stmtblock_t * block,
4852 : : tree * ptemp1)
4853 : : {
4854 : 1381 : tree bytesize;
4855 : 1381 : tree unit;
4856 : 1381 : tree tmp;
4857 : :
4858 : 1381 : unit = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (type));
4859 : 1381 : if (!integer_onep (unit))
4860 : 166 : bytesize = fold_build2_loc (input_location, MULT_EXPR,
4861 : : gfc_array_index_type, size, unit);
4862 : : else
4863 : : bytesize = size;
4864 : :
4865 : 1381 : *ptemp1 = NULL;
4866 : 1381 : tmp = gfc_do_allocate (bytesize, size, ptemp1, block, type);
4867 : :
4868 : 1381 : if (*ptemp1)
4869 : 242 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
4870 : 1381 : return tmp;
4871 : : }
4872 : :
4873 : :
4874 : : /* Allocate temporary for forall construct according to the information in
4875 : : nested_forall_info. INNER_SIZE is the size of temporary needed in the
4876 : : assignment inside forall. PTEMP1 is returned for space free. */
4877 : :
4878 : : static tree
4879 : 967 : allocate_temp_for_forall_nest (forall_info * nested_forall_info, tree type,
4880 : : tree inner_size, stmtblock_t * inner_size_body,
4881 : : stmtblock_t * block, tree * ptemp1)
4882 : : {
4883 : 967 : tree size;
4884 : :
4885 : : /* Calculate the total size of temporary needed in forall construct. */
4886 : 967 : size = compute_overall_iter_number (nested_forall_info, inner_size,
4887 : : inner_size_body, block);
4888 : :
4889 : 967 : return allocate_temp_for_forall_nest_1 (type, size, block, ptemp1);
4890 : : }
4891 : :
4892 : :
4893 : : /* Handle assignments inside forall which need temporary.
4894 : :
4895 : : forall (i=start:end:stride; maskexpr)
4896 : : e<i> = f<i>
4897 : : end forall
4898 : : (where e,f<i> are arbitrary expressions possibly involving i
4899 : : and there is a dependency between e<i> and f<i>)
4900 : : Translates to:
4901 : : masktmp(:) = maskexpr(:)
4902 : :
4903 : : maskindex = 0;
4904 : : count1 = 0;
4905 : : num = 0;
4906 : : for (i = start; i <= end; i += stride)
4907 : : num += SIZE (f<i>)
4908 : : count1 = 0;
4909 : : ALLOCATE (tmp(num))
4910 : : for (i = start; i <= end; i += stride)
4911 : : {
4912 : : if (masktmp[maskindex++])
4913 : : tmp[count1++] = f<i>
4914 : : }
4915 : : maskindex = 0;
4916 : : count1 = 0;
4917 : : for (i = start; i <= end; i += stride)
4918 : : {
4919 : : if (masktmp[maskindex++])
4920 : : e<i> = tmp[count1++]
4921 : : }
4922 : : DEALLOCATE (tmp)
4923 : : */
4924 : : static void
4925 : 224 : gfc_trans_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2,
4926 : : tree wheremask, bool invert,
4927 : : forall_info * nested_forall_info,
4928 : : stmtblock_t * block)
4929 : : {
4930 : 224 : tree type;
4931 : 224 : tree inner_size;
4932 : 224 : gfc_ss *lss, *rss;
4933 : 224 : tree count, count1;
4934 : 224 : tree tmp, tmp1;
4935 : 224 : tree ptemp1;
4936 : 224 : stmtblock_t inner_size_body;
4937 : :
4938 : : /* Create vars. count1 is the current iterator number of the nested
4939 : : forall. */
4940 : 224 : count1 = gfc_create_var (gfc_array_index_type, "count1");
4941 : :
4942 : : /* Count is the wheremask index. */
4943 : 224 : if (wheremask)
4944 : : {
4945 : 27 : count = gfc_create_var (gfc_array_index_type, "count");
4946 : 27 : gfc_add_modify (block, count, gfc_index_zero_node);
4947 : : }
4948 : : else
4949 : : count = NULL;
4950 : :
4951 : : /* Initialize count1. */
4952 : 224 : gfc_add_modify (block, count1, gfc_index_zero_node);
4953 : :
4954 : : /* Calculate the size of temporary needed in the assignment. Return loop, lss
4955 : : and rss which are used in function generate_loop_for_rhs_to_temp(). */
4956 : : /* The type of LHS. Used in function allocate_temp_for_forall_nest */
4957 : 224 : if (expr1->ts.type == BT_CHARACTER)
4958 : : {
4959 : 103 : type = NULL;
4960 : 103 : if (expr1->ref && expr1->ref->type == REF_SUBSTRING)
4961 : : {
4962 : 72 : gfc_se ssse;
4963 : 72 : gfc_init_se (&ssse, NULL);
4964 : 72 : gfc_conv_expr (&ssse, expr1);
4965 : 72 : type = gfc_get_character_type_len (gfc_default_character_kind,
4966 : : ssse.string_length);
4967 : 72 : }
4968 : : else
4969 : : {
4970 : 31 : if (!expr1->ts.u.cl->backend_decl)
4971 : : {
4972 : 6 : gfc_se tse;
4973 : 6 : gcc_assert (expr1->ts.u.cl->length);
4974 : 6 : gfc_init_se (&tse, NULL);
4975 : 6 : gfc_conv_expr (&tse, expr1->ts.u.cl->length);
4976 : 6 : expr1->ts.u.cl->backend_decl = tse.expr;
4977 : : }
4978 : 31 : type = gfc_get_character_type_len (gfc_default_character_kind,
4979 : 31 : expr1->ts.u.cl->backend_decl);
4980 : : }
4981 : : }
4982 : : else
4983 : 121 : type = gfc_typenode_for_spec (&expr1->ts);
4984 : :
4985 : 224 : gfc_init_block (&inner_size_body);
4986 : 224 : inner_size = compute_inner_temp_size (expr1, expr2, &inner_size_body,
4987 : : &lss, &rss);
4988 : :
4989 : : /* Allocate temporary for nested forall construct according to the
4990 : : information in nested_forall_info and inner_size. */
4991 : 224 : tmp1 = allocate_temp_for_forall_nest (nested_forall_info, type, inner_size,
4992 : : &inner_size_body, block, &ptemp1);
4993 : :
4994 : : /* Generate codes to copy rhs to the temporary . */
4995 : 224 : tmp = generate_loop_for_rhs_to_temp (expr2, tmp1, count, count1, lss, rss,
4996 : : wheremask, invert);
4997 : :
4998 : : /* Generate body and loops according to the information in
4999 : : nested_forall_info. */
5000 : 224 : tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
5001 : 224 : gfc_add_expr_to_block (block, tmp);
5002 : :
5003 : : /* Reset count1. */
5004 : 224 : gfc_add_modify (block, count1, gfc_index_zero_node);
5005 : :
5006 : : /* Reset count. */
5007 : 224 : if (wheremask)
5008 : 27 : gfc_add_modify (block, count, gfc_index_zero_node);
5009 : :
5010 : : /* TODO: Second call to compute_inner_temp_size to initialize lss and
5011 : : rss; there must be a better way. */
5012 : 224 : inner_size = compute_inner_temp_size (expr1, expr2, &inner_size_body,
5013 : : &lss, &rss);
5014 : :
5015 : : /* Generate codes to copy the temporary to lhs. */
5016 : 224 : tmp = generate_loop_for_temp_to_lhs (expr1, tmp1, count, count1,
5017 : : lss, rss,
5018 : : wheremask, invert);
5019 : :
5020 : : /* Generate body and loops according to the information in
5021 : : nested_forall_info. */
5022 : 224 : tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
5023 : 224 : gfc_add_expr_to_block (block, tmp);
5024 : :
5025 : 224 : if (ptemp1)
5026 : : {
5027 : : /* Free the temporary. */
5028 : 145 : tmp = gfc_call_free (ptemp1);
5029 : 145 : gfc_add_expr_to_block (block, tmp);
5030 : : }
5031 : 224 : }
5032 : :
5033 : :
5034 : : /* Translate pointer assignment inside FORALL which need temporary. */
5035 : :
5036 : : static void
5037 : 20 : gfc_trans_pointer_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2,
5038 : : forall_info * nested_forall_info,
5039 : : stmtblock_t * block)
5040 : : {
5041 : 20 : tree type;
5042 : 20 : tree inner_size;
5043 : 20 : gfc_ss *lss, *rss;
5044 : 20 : gfc_se lse;
5045 : 20 : gfc_se rse;
5046 : 20 : gfc_array_info *info;
5047 : 20 : gfc_loopinfo loop;
5048 : 20 : tree desc;
5049 : 20 : tree parm;
5050 : 20 : tree parmtype;
5051 : 20 : stmtblock_t body;
5052 : 20 : tree count;
5053 : 20 : tree tmp, tmp1, ptemp1;
5054 : :
5055 : 20 : count = gfc_create_var (gfc_array_index_type, "count");
5056 : 20 : gfc_add_modify (block, count, gfc_index_zero_node);
5057 : :
5058 : 20 : inner_size = gfc_index_one_node;
5059 : 20 : lss = gfc_walk_expr (expr1);
5060 : 20 : rss = gfc_walk_expr (expr2);
5061 : 20 : if (lss == gfc_ss_terminator)
5062 : : {
5063 : 11 : type = gfc_typenode_for_spec (&expr1->ts);
5064 : 11 : type = build_pointer_type (type);
5065 : :
5066 : : /* Allocate temporary for nested forall construct according to the
5067 : : information in nested_forall_info and inner_size. */
5068 : 11 : tmp1 = allocate_temp_for_forall_nest (nested_forall_info, type,
5069 : : inner_size, NULL, block, &ptemp1);
5070 : 11 : gfc_start_block (&body);
5071 : 11 : gfc_init_se (&lse, NULL);
5072 : 11 : lse.expr = gfc_build_array_ref (tmp1, count, NULL);
5073 : 11 : gfc_init_se (&rse, NULL);
5074 : 11 : rse.want_pointer = 1;
5075 : 11 : gfc_conv_expr (&rse, expr2);
5076 : 11 : gfc_add_block_to_block (&body, &rse.pre);
5077 : 11 : gfc_add_modify (&body, lse.expr,
5078 : 11 : fold_convert (TREE_TYPE (lse.expr), rse.expr));
5079 : 11 : gfc_add_block_to_block (&body, &rse.post);
5080 : :
5081 : : /* Increment count. */
5082 : 11 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5083 : : count, gfc_index_one_node);
5084 : 11 : gfc_add_modify (&body, count, tmp);
5085 : :
5086 : 11 : tmp = gfc_finish_block (&body);
5087 : :
5088 : : /* Generate body and loops according to the information in
5089 : : nested_forall_info. */
5090 : 11 : tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
5091 : 11 : gfc_add_expr_to_block (block, tmp);
5092 : :
5093 : : /* Reset count. */
5094 : 11 : gfc_add_modify (block, count, gfc_index_zero_node);
5095 : :
5096 : 11 : gfc_start_block (&body);
5097 : 11 : gfc_init_se (&lse, NULL);
5098 : 11 : gfc_init_se (&rse, NULL);
5099 : 11 : rse.expr = gfc_build_array_ref (tmp1, count, NULL);
5100 : 11 : lse.want_pointer = 1;
5101 : 11 : gfc_conv_expr (&lse, expr1);
5102 : 11 : gfc_add_block_to_block (&body, &lse.pre);
5103 : 11 : gfc_add_modify (&body, lse.expr, rse.expr);
5104 : 11 : gfc_add_block_to_block (&body, &lse.post);
5105 : : /* Increment count. */
5106 : 11 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5107 : : count, gfc_index_one_node);
5108 : 11 : gfc_add_modify (&body, count, tmp);
5109 : 11 : tmp = gfc_finish_block (&body);
5110 : :
5111 : : /* Generate body and loops according to the information in
5112 : : nested_forall_info. */
5113 : 11 : tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
5114 : 11 : gfc_add_expr_to_block (block, tmp);
5115 : : }
5116 : : else
5117 : : {
5118 : 9 : gfc_init_loopinfo (&loop);
5119 : :
5120 : : /* Associate the SS with the loop. */
5121 : 9 : gfc_add_ss_to_loop (&loop, rss);
5122 : :
5123 : : /* Setup the scalarizing loops and bounds. */
5124 : 9 : gfc_conv_ss_startstride (&loop);
5125 : :
5126 : 9 : gfc_conv_loop_setup (&loop, &expr2->where);
5127 : :
5128 : 9 : info = &rss->info->data.array;
5129 : 9 : desc = info->descriptor;
5130 : :
5131 : : /* Make a new descriptor. */
5132 : 9 : parmtype = gfc_get_element_type (TREE_TYPE (desc));
5133 : 9 : parmtype = gfc_get_array_type_bounds (parmtype, loop.dimen, 0,
5134 : : loop.from, loop.to, 1,
5135 : : GFC_ARRAY_UNKNOWN, true);
5136 : :
5137 : : /* Allocate temporary for nested forall construct. */
5138 : 9 : tmp1 = allocate_temp_for_forall_nest (nested_forall_info, parmtype,
5139 : : inner_size, NULL, block, &ptemp1);
5140 : 9 : gfc_start_block (&body);
5141 : 9 : gfc_init_se (&lse, NULL);
5142 : 9 : lse.expr = gfc_build_array_ref (tmp1, count, NULL);
5143 : 9 : lse.direct_byref = 1;
5144 : 9 : gfc_conv_expr_descriptor (&lse, expr2);
5145 : :
5146 : 9 : gfc_add_block_to_block (&body, &lse.pre);
5147 : 9 : gfc_add_block_to_block (&body, &lse.post);
5148 : :
5149 : : /* Increment count. */
5150 : 9 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5151 : : count, gfc_index_one_node);
5152 : 9 : gfc_add_modify (&body, count, tmp);
5153 : :
5154 : 9 : tmp = gfc_finish_block (&body);
5155 : :
5156 : : /* Generate body and loops according to the information in
5157 : : nested_forall_info. */
5158 : 9 : tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
5159 : 9 : gfc_add_expr_to_block (block, tmp);
5160 : :
5161 : : /* Reset count. */
5162 : 9 : gfc_add_modify (block, count, gfc_index_zero_node);
5163 : :
5164 : 9 : parm = gfc_build_array_ref (tmp1, count, NULL);
5165 : 9 : gfc_init_se (&lse, NULL);
5166 : 9 : gfc_conv_expr_descriptor (&lse, expr1);
5167 : 9 : gfc_add_modify (&lse.pre, lse.expr, parm);
5168 : 9 : gfc_start_block (&body);
5169 : 9 : gfc_add_block_to_block (&body, &lse.pre);
5170 : 9 : gfc_add_block_to_block (&body, &lse.post);
5171 : :
5172 : : /* Increment count. */
5173 : 9 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5174 : : count, gfc_index_one_node);
5175 : 9 : gfc_add_modify (&body, count, tmp);
5176 : :
5177 : 9 : tmp = gfc_finish_block (&body);
5178 : :
5179 : 9 : tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
5180 : 9 : gfc_add_expr_to_block (block, tmp);
5181 : : }
5182 : : /* Free the temporary. */
5183 : 20 : if (ptemp1)
5184 : : {
5185 : 1 : tmp = gfc_call_free (ptemp1);
5186 : 1 : gfc_add_expr_to_block (block, tmp);
5187 : : }
5188 : 20 : }
5189 : :
5190 : : /* For saving the outer-variable data when doing
5191 : : LOCAL and LOCAL_INIT substitution. */
5192 : : struct symbol_and_tree_t
5193 : : {
5194 : : gfc_symbol *sym;
5195 : : gfc_expr *value;
5196 : : tree decl;
5197 : : symbol_attribute attr;
5198 : : };
5199 : :
5200 : : /* Handle the LOCAL and LOCAL_INIT locality specifiers. This has to be
5201 : : called twice, once with after_body=false - and then after the loop
5202 : : body has been processed with after_body=true.
5203 : :
5204 : : Creates a copy of the variables that appear in the LOCAL and LOCAL_INIT
5205 : : locality specifiers of 'do concurrent' - and use it in the original
5206 : : gfc_symbol. The declaration is then reset by after_body=true.
5207 : :
5208 : : Variables in LOCAL_INIT are set in every loop iteration. */
5209 : :
5210 : : void
5211 : 226 : gfc_trans_concurrent_locality_spec (bool after_body, stmtblock_t *body,
5212 : : std::vector<symbol_and_tree_t> *saved_decls,
5213 : : gfc_expr_list **locality_list)
5214 : : {
5215 : 226 : if (!locality_list[LOCALITY_LOCAL] && !locality_list[LOCALITY_LOCAL_INIT])
5216 : : return;
5217 : :
5218 : 96 : if (after_body)
5219 : : {
5220 : 205 : for (unsigned i = 0; i < saved_decls->size (); i++)
5221 : : {
5222 : 157 : (*saved_decls)[i].sym->backend_decl = (*saved_decls)[i].decl;
5223 : 157 : (*saved_decls)[i].sym->attr = (*saved_decls)[i].attr;
5224 : 157 : (*saved_decls)[i].sym->value = (*saved_decls)[i].value;
5225 : : }
5226 : : return;
5227 : : }
5228 : :
5229 : : gfc_expr_list *el;
5230 : : int cnt = 0;
5231 : 144 : for (int i = 0; i <= 1; i++)
5232 : 96 : for (el = locality_list[i == 0 ? LOCALITY_LOCAL : LOCALITY_LOCAL_INIT];
5233 : 253 : el; el = el->next)
5234 : : {
5235 : 157 : gfc_symbol *outer_sym = el->expr->symtree->n.sym;
5236 : 157 : if (!outer_sym->backend_decl)
5237 : 0 : outer_sym->backend_decl = gfc_get_symbol_decl (outer_sym);
5238 : 157 : cnt++;
5239 : : }
5240 : 48 : saved_decls->resize (cnt);
5241 : :
5242 : : /* The variables have to be created in the scope of the loop body. */
5243 : 48 : if (!body->has_scope)
5244 : : {
5245 : 48 : gcc_checking_assert (body->head == NULL_TREE);
5246 : 48 : gfc_start_block (body);
5247 : : }
5248 : 48 : gfc_start_saved_local_decls ();
5249 : :
5250 : 48 : cnt = 0;
5251 : 48 : static_assert (LOCALITY_LOCAL_INIT - LOCALITY_LOCAL == 1, "locality_type");
5252 : 144 : for (int type = LOCALITY_LOCAL;
5253 : 144 : type <= LOCALITY_LOCAL_INIT; type++)
5254 : 253 : for (el = locality_list[type]; el; el = el->next)
5255 : : {
5256 : 157 : gfc_symbol *sym = el->expr->symtree->n.sym;
5257 : 157 : (*saved_decls)[cnt].sym = sym;
5258 : 157 : (*saved_decls)[cnt].attr = sym->attr;
5259 : 157 : (*saved_decls)[cnt].value = sym->value;
5260 : 157 : (*saved_decls)[cnt].decl = sym->backend_decl;
5261 : :
5262 : 157 : if (sym->attr.dimension && sym->as->type == AS_ASSUMED_SHAPE)
5263 : : {
5264 : 3 : gfc_error ("Sorry, %s specifier at %L for assumed-size array %qs "
5265 : : "is not yet supported",
5266 : : type == LOCALITY_LOCAL ? "LOCAL" : "LOCAL_INIT",
5267 : : &el->expr->where, sym->name);
5268 : 2 : continue;
5269 : : }
5270 : :
5271 : 155 : gfc_symbol outer_sym = *sym;
5272 : :
5273 : : /* Create the inner local variable. */
5274 : 155 : sym->backend_decl = NULL;
5275 : 155 : sym->value = NULL;
5276 : 155 : sym->attr.save = SAVE_NONE;
5277 : 155 : sym->attr.value = 0;
5278 : 155 : sym->attr.dummy = 0;
5279 : 155 : sym->attr.optional = 0;
5280 : :
5281 : 155 : {
5282 : : /* Slightly ugly hack for adding the decl via add_decl_as_local. */
5283 : 155 : gfc_symbol dummy_block_sym;
5284 : 155 : dummy_block_sym.attr.flavor = FL_LABEL;
5285 : 155 : gfc_symbol *saved_proc_name = sym->ns->proc_name;
5286 : 155 : sym->ns->proc_name = &dummy_block_sym;
5287 : :
5288 : 155 : gfc_get_symbol_decl (sym);
5289 : 310 : DECL_SOURCE_LOCATION (sym->backend_decl)
5290 : 155 : = gfc_get_location (&el->expr->where);
5291 : :
5292 : 155 : sym->ns->proc_name = saved_proc_name;
5293 : : }
5294 : :
5295 : 155 : symbol_attribute attr = gfc_expr_attr (el->expr);
5296 : 155 : if (type == LOCALITY_LOCAL
5297 : 89 : && !attr.pointer
5298 : 41 : && sym->ts.type == BT_DERIVED
5299 : 183 : && gfc_has_default_initializer (sym->ts.u.derived))
5300 : : /* Cf. PR fortran/ */
5301 : 4 : gfc_error ("Sorry, LOCAL specifier at %L for %qs of derived type with"
5302 : : " default initializer is not yet supported",
5303 : 4 : &el->expr->where, sym->name);
5304 : 155 : if (type == LOCALITY_LOCAL_INIT)
5305 : : {
5306 : : /* LOCAL_INIT: local_var = outer_var. */
5307 : 66 : gfc_symtree st = *el->expr->symtree;
5308 : 66 : st.n.sym = &outer_sym;
5309 : 66 : gfc_expr expr = *el->expr;
5310 : 66 : expr.symtree = &st;
5311 : 66 : tree t = (attr.pointer
5312 : 66 : ? gfc_trans_pointer_assignment (el->expr, &expr)
5313 : 42 : : gfc_trans_assignment (el->expr, &expr, false, false,
5314 : : false, false));
5315 : 66 : gfc_add_expr_to_block (body, t);
5316 : : }
5317 : 155 : cnt++;
5318 : : }
5319 : 48 : gfc_stop_saved_local_decls ();
5320 : : }
5321 : :
5322 : :
5323 : : /* FORALL and WHERE statements are really nasty, especially when you nest
5324 : : them. All the rhs of a forall assignment must be evaluated before the
5325 : : actual assignments are performed. Presumably this also applies to all the
5326 : : assignments in an inner where statement. */
5327 : :
5328 : : /* Generate code for a FORALL statement. Any temporaries are allocated as a
5329 : : linear array, relying on the fact that we process in the same order in all
5330 : : loops.
5331 : :
5332 : : forall (i=start:end:stride; maskexpr)
5333 : : e<i> = f<i>
5334 : : g<i> = h<i>
5335 : : end forall
5336 : : (where e,f,g,h<i> are arbitrary expressions possibly involving i)
5337 : : Translates to:
5338 : : count = ((end + 1 - start) / stride)
5339 : : masktmp(:) = maskexpr(:)
5340 : :
5341 : : maskindex = 0;
5342 : : for (i = start; i <= end; i += stride)
5343 : : {
5344 : : if (masktmp[maskindex++])
5345 : : e<i> = f<i>
5346 : : }
5347 : : maskindex = 0;
5348 : : for (i = start; i <= end; i += stride)
5349 : : {
5350 : : if (masktmp[maskindex++])
5351 : : g<i> = h<i>
5352 : : }
5353 : :
5354 : : Note that this code only works when there are no dependencies.
5355 : : Forall loop with array assignments and data dependencies are a real pain,
5356 : : because the size of the temporary cannot always be determined before the
5357 : : loop is executed. This problem is compounded by the presence of nested
5358 : : FORALL constructs.
5359 : : */
5360 : :
5361 : : static tree
5362 : 2076 : gfc_trans_forall_1 (gfc_code * code, forall_info * nested_forall_info)
5363 : : {
5364 : 2076 : stmtblock_t pre;
5365 : 2076 : stmtblock_t post;
5366 : 2076 : stmtblock_t block;
5367 : 2076 : stmtblock_t body;
5368 : 2076 : tree *var;
5369 : 2076 : tree *start;
5370 : 2076 : tree *end;
5371 : 2076 : tree *step;
5372 : 2076 : gfc_expr **varexpr;
5373 : 2076 : tree tmp;
5374 : 2076 : tree assign;
5375 : 2076 : tree size;
5376 : 2076 : tree maskindex;
5377 : 2076 : tree mask;
5378 : 2076 : tree pmask;
5379 : 2076 : tree cycle_label = NULL_TREE;
5380 : 2076 : int n;
5381 : 2076 : int nvar;
5382 : 2076 : int need_temp;
5383 : 2076 : gfc_forall_iterator *fa;
5384 : 2076 : gfc_se se;
5385 : 2076 : gfc_code *c;
5386 : 2076 : gfc_saved_var *saved_vars;
5387 : 2076 : iter_info *this_forall;
5388 : 2076 : forall_info *info;
5389 : 2076 : bool need_mask;
5390 : :
5391 : : /* Do nothing if the mask is false. */
5392 : 2076 : if (code->expr1
5393 : 725 : && code->expr1->expr_type == EXPR_CONSTANT
5394 : 2 : && !code->expr1->value.logical)
5395 : 1 : return build_empty_stmt (input_location);
5396 : :
5397 : 2075 : n = 0;
5398 : : /* Count the FORALL index number. */
5399 : 6054 : for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
5400 : 3979 : n++;
5401 : 2075 : nvar = n;
5402 : :
5403 : : /* Allocate the space for var, start, end, step, varexpr. */
5404 : 2075 : var = XCNEWVEC (tree, nvar);
5405 : 2075 : start = XCNEWVEC (tree, nvar);
5406 : 2075 : end = XCNEWVEC (tree, nvar);
5407 : 2075 : step = XCNEWVEC (tree, nvar);
5408 : 2075 : varexpr = XCNEWVEC (gfc_expr *, nvar);
5409 : 2075 : saved_vars = XCNEWVEC (gfc_saved_var, nvar);
5410 : :
5411 : : /* Allocate the space for info. */
5412 : 2075 : info = XCNEW (forall_info);
5413 : :
5414 : 2075 : gfc_start_block (&pre);
5415 : 2075 : gfc_init_block (&post);
5416 : 2075 : gfc_init_block (&block);
5417 : :
5418 : 2075 : n = 0;
5419 : 6054 : for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
5420 : : {
5421 : 3979 : gfc_symbol *sym = fa->var->symtree->n.sym;
5422 : :
5423 : : /* Allocate space for this_forall. */
5424 : 3979 : this_forall = XCNEW (iter_info);
5425 : :
5426 : : /* Create a temporary variable for the FORALL index. */
5427 : 3979 : tmp = gfc_typenode_for_spec (&sym->ts);
5428 : 3979 : var[n] = gfc_create_var (tmp, sym->name);
5429 : 3979 : gfc_shadow_sym (sym, var[n], &saved_vars[n]);
5430 : :
5431 : : /* Record it in this_forall. */
5432 : 3979 : this_forall->var = var[n];
5433 : :
5434 : : /* Replace the index symbol's backend_decl with the temporary decl. */
5435 : 3979 : sym->backend_decl = var[n];
5436 : :
5437 : : /* Work out the start, end and stride for the loop. */
5438 : 3979 : gfc_init_se (&se, NULL);
5439 : 3979 : gfc_conv_expr_val (&se, fa->start);
5440 : : /* Record it in this_forall. */
5441 : 3979 : this_forall->start = se.expr;
5442 : 3979 : gfc_add_block_to_block (&block, &se.pre);
5443 : 3979 : start[n] = se.expr;
5444 : :
5445 : 3979 : gfc_init_se (&se, NULL);
5446 : 3979 : gfc_conv_expr_val (&se, fa->end);
5447 : : /* Record it in this_forall. */
5448 : 3979 : this_forall->end = se.expr;
5449 : 3979 : gfc_make_safe_expr (&se);
5450 : 3979 : gfc_add_block_to_block (&block, &se.pre);
5451 : 3979 : end[n] = se.expr;
5452 : :
5453 : 3979 : gfc_init_se (&se, NULL);
5454 : 3979 : gfc_conv_expr_val (&se, fa->stride);
5455 : : /* Record it in this_forall. */
5456 : 3979 : this_forall->step = se.expr;
5457 : 3979 : gfc_make_safe_expr (&se);
5458 : 3979 : gfc_add_block_to_block (&block, &se.pre);
5459 : 3979 : step[n] = se.expr;
5460 : :
5461 : : /* Copy loop annotations. */
5462 : 3979 : this_forall->annot = fa->annot;
5463 : :
5464 : : /* Set the NEXT field of this_forall to NULL. */
5465 : 3979 : this_forall->next = NULL;
5466 : : /* Link this_forall to the info construct. */
5467 : 3979 : if (info->this_loop)
5468 : : {
5469 : : iter_info *iter_tmp = info->this_loop;
5470 : 2830 : while (iter_tmp->next != NULL)
5471 : : iter_tmp = iter_tmp->next;
5472 : 1904 : iter_tmp->next = this_forall;
5473 : : }
5474 : : else
5475 : 2075 : info->this_loop = this_forall;
5476 : :
5477 : 3979 : n++;
5478 : : }
5479 : 2075 : nvar = n;
5480 : :
5481 : : /* Calculate the size needed for the current forall level. */
5482 : 2075 : size = gfc_index_one_node;
5483 : 6054 : for (n = 0; n < nvar; n++)
5484 : : {
5485 : : /* size = (end + step - start) / step. */
5486 : 3979 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (start[n]),
5487 : 3979 : step[n], start[n]);
5488 : 3979 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (end[n]),
5489 : 3979 : end[n], tmp);
5490 : 3979 : tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR, TREE_TYPE (tmp),
5491 : : tmp, step[n]);
5492 : 3979 : tmp = convert (gfc_array_index_type, tmp);
5493 : :
5494 : 3979 : size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
5495 : : size, tmp);
5496 : : }
5497 : :
5498 : : /* Record the nvar and size of current forall level. */
5499 : 2075 : info->nvar = nvar;
5500 : 2075 : info->size = size;
5501 : :
5502 : 2075 : if (code->expr1)
5503 : : {
5504 : : /* If the mask is .true., consider the FORALL unconditional. */
5505 : 724 : if (code->expr1->expr_type == EXPR_CONSTANT
5506 : 1 : && code->expr1->value.logical)
5507 : : need_mask = false;
5508 : : else
5509 : 723 : need_mask = true;
5510 : : }
5511 : : else
5512 : : need_mask = false;
5513 : :
5514 : : /* First we need to allocate the mask. */
5515 : 723 : if (need_mask)
5516 : : {
5517 : : /* As the mask array can be very big, prefer compact boolean types. */
5518 : 723 : tree mask_type = gfc_get_logical_type (gfc_logical_kinds[0].kind);
5519 : 723 : mask = allocate_temp_for_forall_nest (nested_forall_info, mask_type,
5520 : : size, NULL, &block, &pmask);
5521 : 723 : maskindex = gfc_create_var_np (gfc_array_index_type, "mi");
5522 : :
5523 : : /* Record them in the info structure. */
5524 : 723 : info->maskindex = maskindex;
5525 : 723 : info->mask = mask;
5526 : : }
5527 : : else
5528 : : {
5529 : : /* No mask was specified. */
5530 : 1352 : maskindex = NULL_TREE;
5531 : 1352 : mask = pmask = NULL_TREE;
5532 : : }
5533 : :
5534 : : /* Link the current forall level to nested_forall_info. */
5535 : 2075 : info->prev_nest = nested_forall_info;
5536 : 2075 : nested_forall_info = info;
5537 : :
5538 : : /* Copy the mask into a temporary variable if required.
5539 : : For now we assume a mask temporary is needed. */
5540 : 2075 : if (need_mask)
5541 : : {
5542 : : /* As the mask array can be very big, prefer compact boolean types. */
5543 : 723 : tree mask_type = gfc_get_logical_type (gfc_logical_kinds[0].kind);
5544 : :
5545 : 723 : gfc_add_modify (&block, maskindex, gfc_index_zero_node);
5546 : :
5547 : : /* Start of mask assignment loop body. */
5548 : 723 : gfc_start_block (&body);
5549 : :
5550 : : /* Evaluate the mask expression. */
5551 : 723 : gfc_init_se (&se, NULL);
5552 : 723 : gfc_conv_expr_val (&se, code->expr1);
5553 : 723 : gfc_add_block_to_block (&body, &se.pre);
5554 : :
5555 : : /* Store the mask. */
5556 : 723 : se.expr = convert (mask_type, se.expr);
5557 : :
5558 : 723 : tmp = gfc_build_array_ref (mask, maskindex, NULL);
5559 : 723 : gfc_add_modify (&body, tmp, se.expr);
5560 : :
5561 : : /* Advance to the next mask element. */
5562 : 723 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5563 : : maskindex, gfc_index_one_node);
5564 : 723 : gfc_add_modify (&body, maskindex, tmp);
5565 : :
5566 : : /* Generate the loops. */
5567 : 723 : tmp = gfc_finish_block (&body);
5568 : 723 : tmp = gfc_trans_nested_forall_loop (info, tmp, 0);
5569 : 723 : gfc_add_expr_to_block (&block, tmp);
5570 : : }
5571 : :
5572 : 2075 : if (code->op == EXEC_DO_CONCURRENT)
5573 : : {
5574 : 113 : gfc_init_block (&body);
5575 : 113 : cycle_label = gfc_build_label_decl (NULL_TREE);
5576 : 113 : code->cycle_label = cycle_label;
5577 : :
5578 : : /* Handle LOCAL and LOCAL_INIT. */
5579 : 113 : std::vector<symbol_and_tree_t> saved_decls;
5580 : 113 : gfc_trans_concurrent_locality_spec (false, &body, &saved_decls,
5581 : 113 : code->ext.concur.locality);
5582 : :
5583 : : /* Translate the body. */
5584 : 113 : tmp = gfc_trans_code (code->block->next);
5585 : 113 : gfc_add_expr_to_block (&body, tmp);
5586 : :
5587 : : /* Reset locality variables. */
5588 : 113 : gfc_trans_concurrent_locality_spec (true, &body, &saved_decls,
5589 : : code->ext.concur.locality);
5590 : 113 : if (TREE_USED (cycle_label))
5591 : : {
5592 : 113 : tmp = build1_v (LABEL_EXPR, cycle_label);
5593 : 113 : gfc_add_expr_to_block (&body, tmp);
5594 : : }
5595 : :
5596 : 113 : tmp = gfc_finish_block (&body);
5597 : 113 : nested_forall_info->do_concurrent = true;
5598 : 113 : tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
5599 : 113 : gfc_add_expr_to_block (&block, tmp);
5600 : 113 : goto done;
5601 : 113 : }
5602 : :
5603 : 1962 : c = code->block->next;
5604 : :
5605 : : /* TODO: loop merging in FORALL statements. */
5606 : : /* Now that we've got a copy of the mask, generate the assignment loops. */
5607 : 3942 : while (c)
5608 : : {
5609 : 1980 : switch (c->op)
5610 : : {
5611 : 1825 : case EXEC_ASSIGN:
5612 : : /* A scalar or array assignment. DO the simple check for
5613 : : lhs to rhs dependencies. These make a temporary for the
5614 : : rhs and form a second forall block to copy to variable. */
5615 : 1825 : need_temp = check_forall_dependencies(c, &pre, &post);
5616 : :
5617 : : /* Temporaries due to array assignment data dependencies introduce
5618 : : no end of problems. */
5619 : 1825 : if (need_temp || flag_test_forall_temp)
5620 : 197 : gfc_trans_assign_need_temp (c->expr1, c->expr2, NULL, false,
5621 : : nested_forall_info, &block);
5622 : : else
5623 : : {
5624 : : /* Use the normal assignment copying routines. */
5625 : 1628 : assign = gfc_trans_assignment (c->expr1, c->expr2, false, true);
5626 : :
5627 : : /* Generate body and loops. */
5628 : 1628 : tmp = gfc_trans_nested_forall_loop (nested_forall_info,
5629 : : assign, 1);
5630 : 1628 : gfc_add_expr_to_block (&block, tmp);
5631 : : }
5632 : :
5633 : : /* Cleanup any temporary symtrees that have been made to deal
5634 : : with dependencies. */
5635 : 1825 : if (new_symtree)
5636 : 62 : cleanup_forall_symtrees (c);
5637 : :
5638 : : break;
5639 : :
5640 : 46 : case EXEC_WHERE:
5641 : : /* Translate WHERE or WHERE construct nested in FORALL. */
5642 : 46 : gfc_trans_where_2 (c, NULL, false, nested_forall_info, &block);
5643 : 46 : break;
5644 : :
5645 : : /* Pointer assignment inside FORALL. */
5646 : 27 : case EXEC_POINTER_ASSIGN:
5647 : 27 : need_temp = gfc_check_dependency (c->expr1, c->expr2, 0);
5648 : : /* Avoid cases where a temporary would never be needed and where
5649 : : the temp code is guaranteed to fail. */
5650 : 27 : if (need_temp
5651 : 7 : || (flag_test_forall_temp
5652 : 0 : && c->expr2->expr_type != EXPR_CONSTANT
5653 : 0 : && c->expr2->expr_type != EXPR_NULL))
5654 : 20 : gfc_trans_pointer_assign_need_temp (c->expr1, c->expr2,
5655 : : nested_forall_info, &block);
5656 : : else
5657 : : {
5658 : : /* Use the normal assignment copying routines. */
5659 : 7 : assign = gfc_trans_pointer_assignment (c->expr1, c->expr2);
5660 : :
5661 : : /* Generate body and loops. */
5662 : 7 : tmp = gfc_trans_nested_forall_loop (nested_forall_info,
5663 : : assign, 1);
5664 : 7 : gfc_add_expr_to_block (&block, tmp);
5665 : : }
5666 : : break;
5667 : :
5668 : 76 : case EXEC_FORALL:
5669 : 76 : tmp = gfc_trans_forall_1 (c, nested_forall_info);
5670 : 76 : gfc_add_expr_to_block (&block, tmp);
5671 : 76 : break;
5672 : :
5673 : : /* Explicit subroutine calls are prevented by the frontend but interface
5674 : : assignments can legitimately produce them. */
5675 : 6 : case EXEC_ASSIGN_CALL:
5676 : 6 : assign = gfc_trans_call (c, true, NULL_TREE, NULL_TREE, false);
5677 : 6 : tmp = gfc_trans_nested_forall_loop (nested_forall_info, assign, 1);
5678 : 6 : gfc_add_expr_to_block (&block, tmp);
5679 : 6 : break;
5680 : :
5681 : 0 : default:
5682 : 0 : gcc_unreachable ();
5683 : : }
5684 : :
5685 : 1980 : c = c->next;
5686 : : }
5687 : :
5688 : 1962 : done:
5689 : : /* Restore the original index variables. */
5690 : 6054 : for (fa = code->ext.concur.forall_iterator, n = 0; fa; fa = fa->next, n++)
5691 : 3979 : gfc_restore_sym (fa->var->symtree->n.sym, &saved_vars[n]);
5692 : :
5693 : : /* Free the space for var, start, end, step, varexpr. */
5694 : 2075 : free (var);
5695 : 2075 : free (start);
5696 : 2075 : free (end);
5697 : 2075 : free (step);
5698 : 2075 : free (varexpr);
5699 : 2075 : free (saved_vars);
5700 : :
5701 : 6054 : for (this_forall = info->this_loop; this_forall;)
5702 : : {
5703 : 3979 : iter_info *next = this_forall->next;
5704 : 3979 : free (this_forall);
5705 : 3979 : this_forall = next;
5706 : : }
5707 : :
5708 : : /* Free the space for this forall_info. */
5709 : 2075 : free (info);
5710 : :
5711 : 2075 : if (pmask)
5712 : : {
5713 : : /* Free the temporary for the mask. */
5714 : 52 : tmp = gfc_call_free (pmask);
5715 : 52 : gfc_add_expr_to_block (&block, tmp);
5716 : : }
5717 : 2075 : if (maskindex)
5718 : 723 : pushdecl (maskindex);
5719 : :
5720 : 2075 : gfc_add_block_to_block (&pre, &block);
5721 : 2075 : gfc_add_block_to_block (&pre, &post);
5722 : :
5723 : 2075 : return gfc_finish_block (&pre);
5724 : : }
5725 : :
5726 : :
5727 : : /* Translate the FORALL statement or construct. */
5728 : :
5729 : 1887 : tree gfc_trans_forall (gfc_code * code)
5730 : : {
5731 : 1887 : return gfc_trans_forall_1 (code, NULL);
5732 : : }
5733 : :
5734 : :
5735 : : /* Translate the DO CONCURRENT construct. */
5736 : :
5737 : 113 : tree gfc_trans_do_concurrent (gfc_code * code)
5738 : : {
5739 : 113 : return gfc_trans_forall_1 (code, NULL);
5740 : : }
5741 : :
5742 : :
5743 : : /* Evaluate the WHERE mask expression, copy its value to a temporary.
5744 : : If the WHERE construct is nested in FORALL, compute the overall temporary
5745 : : needed by the WHERE mask expression multiplied by the iterator number of
5746 : : the nested forall.
5747 : : ME is the WHERE mask expression.
5748 : : MASK is the current execution mask upon input, whose sense may or may
5749 : : not be inverted as specified by the INVERT argument.
5750 : : CMASK is the updated execution mask on output, or NULL if not required.
5751 : : PMASK is the pending execution mask on output, or NULL if not required.
5752 : : BLOCK is the block in which to place the condition evaluation loops. */
5753 : :
5754 : : static void
5755 : 527 : gfc_evaluate_where_mask (gfc_expr * me, forall_info * nested_forall_info,
5756 : : tree mask, bool invert, tree cmask, tree pmask,
5757 : : tree mask_type, stmtblock_t * block)
5758 : : {
5759 : 527 : tree tmp, tmp1;
5760 : 527 : gfc_ss *lss, *rss;
5761 : 527 : gfc_loopinfo loop;
5762 : 527 : stmtblock_t body, body1;
5763 : 527 : tree count, cond, mtmp;
5764 : 527 : gfc_se lse, rse;
5765 : :
5766 : 527 : gfc_init_loopinfo (&loop);
5767 : :
5768 : 527 : lss = gfc_walk_expr (me);
5769 : 527 : rss = gfc_walk_expr (me);
5770 : :
5771 : : /* Variable to index the temporary. */
5772 : 527 : count = gfc_create_var (gfc_array_index_type, "count");
5773 : : /* Initialize count. */
5774 : 527 : gfc_add_modify (block, count, gfc_index_zero_node);
5775 : :
5776 : 527 : gfc_start_block (&body);
5777 : :
5778 : 527 : gfc_init_se (&rse, NULL);
5779 : 527 : gfc_init_se (&lse, NULL);
5780 : :
5781 : 527 : if (lss == gfc_ss_terminator)
5782 : : {
5783 : 0 : gfc_init_block (&body1);
5784 : : }
5785 : : else
5786 : : {
5787 : : /* Initialize the loop. */
5788 : 527 : gfc_init_loopinfo (&loop);
5789 : :
5790 : : /* We may need LSS to determine the shape of the expression. */
5791 : 527 : gfc_add_ss_to_loop (&loop, lss);
5792 : 527 : gfc_add_ss_to_loop (&loop, rss);
5793 : :
5794 : 527 : gfc_conv_ss_startstride (&loop);
5795 : 527 : gfc_conv_loop_setup (&loop, &me->where);
5796 : :
5797 : 527 : gfc_mark_ss_chain_used (rss, 1);
5798 : : /* Start the loop body. */
5799 : 527 : gfc_start_scalarized_body (&loop, &body1);
5800 : :
5801 : : /* Translate the expression. */
5802 : 527 : gfc_copy_loopinfo_to_se (&rse, &loop);
5803 : 527 : rse.ss = rss;
5804 : 527 : gfc_conv_expr (&rse, me);
5805 : : }
5806 : :
5807 : : /* Variable to evaluate mask condition. */
5808 : 527 : cond = gfc_create_var (mask_type, "cond");
5809 : 527 : if (mask && (cmask || pmask))
5810 : 234 : mtmp = gfc_create_var (mask_type, "mask");
5811 : : else mtmp = NULL_TREE;
5812 : :
5813 : 527 : gfc_add_block_to_block (&body1, &lse.pre);
5814 : 527 : gfc_add_block_to_block (&body1, &rse.pre);
5815 : :
5816 : 527 : gfc_add_modify (&body1, cond, fold_convert (mask_type, rse.expr));
5817 : :
5818 : 527 : if (mask && (cmask || pmask))
5819 : : {
5820 : 234 : tmp = gfc_build_array_ref (mask, count, NULL);
5821 : 234 : if (invert)
5822 : 99 : tmp = fold_build1_loc (input_location, TRUTH_NOT_EXPR, mask_type, tmp);
5823 : 234 : gfc_add_modify (&body1, mtmp, tmp);
5824 : : }
5825 : :
5826 : 527 : if (cmask)
5827 : : {
5828 : 509 : tmp1 = gfc_build_array_ref (cmask, count, NULL);
5829 : 509 : tmp = cond;
5830 : 509 : if (mask)
5831 : 234 : tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, mask_type,
5832 : : mtmp, tmp);
5833 : 509 : gfc_add_modify (&body1, tmp1, tmp);
5834 : : }
5835 : :
5836 : 527 : if (pmask)
5837 : : {
5838 : 146 : tmp1 = gfc_build_array_ref (pmask, count, NULL);
5839 : 146 : tmp = fold_build1_loc (input_location, TRUTH_NOT_EXPR, mask_type, cond);
5840 : 146 : if (mask)
5841 : 146 : tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, mask_type, mtmp,
5842 : : tmp);
5843 : 146 : gfc_add_modify (&body1, tmp1, tmp);
5844 : : }
5845 : :
5846 : 527 : gfc_add_block_to_block (&body1, &lse.post);
5847 : 527 : gfc_add_block_to_block (&body1, &rse.post);
5848 : :
5849 : 527 : if (lss == gfc_ss_terminator)
5850 : : {
5851 : 0 : gfc_add_block_to_block (&body, &body1);
5852 : : }
5853 : : else
5854 : : {
5855 : : /* Increment count. */
5856 : 527 : tmp1 = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5857 : : count, gfc_index_one_node);
5858 : 527 : gfc_add_modify (&body1, count, tmp1);
5859 : :
5860 : : /* Generate the copying loops. */
5861 : 527 : gfc_trans_scalarizing_loops (&loop, &body1);
5862 : :
5863 : 527 : gfc_add_block_to_block (&body, &loop.pre);
5864 : 527 : gfc_add_block_to_block (&body, &loop.post);
5865 : :
5866 : 527 : gfc_cleanup_loop (&loop);
5867 : : /* TODO: Reuse lss and rss when copying temp->lhs. Need to be careful
5868 : : as tree nodes in SS may not be valid in different scope. */
5869 : : }
5870 : :
5871 : 527 : tmp1 = gfc_finish_block (&body);
5872 : : /* If the WHERE construct is inside FORALL, fill the full temporary. */
5873 : 527 : if (nested_forall_info != NULL)
5874 : 64 : tmp1 = gfc_trans_nested_forall_loop (nested_forall_info, tmp1, 1);
5875 : :
5876 : 527 : gfc_add_expr_to_block (block, tmp1);
5877 : 527 : }
5878 : :
5879 : :
5880 : : /* Translate an assignment statement in a WHERE statement or construct
5881 : : statement. The MASK expression is used to control which elements
5882 : : of EXPR1 shall be assigned. The sense of MASK is specified by
5883 : : INVERT. */
5884 : :
5885 : : static tree
5886 : 544 : gfc_trans_where_assign (gfc_expr *expr1, gfc_expr *expr2,
5887 : : tree mask, bool invert,
5888 : : tree count1, tree count2,
5889 : : gfc_code *cnext)
5890 : : {
5891 : 544 : gfc_se lse;
5892 : 544 : gfc_se rse;
5893 : 544 : gfc_ss *lss;
5894 : 544 : gfc_ss *lss_section;
5895 : 544 : gfc_ss *rss;
5896 : :
5897 : 544 : gfc_loopinfo loop;
5898 : 544 : tree tmp;
5899 : 544 : stmtblock_t block;
5900 : 544 : stmtblock_t body;
5901 : 544 : tree index, maskexpr;
5902 : :
5903 : : /* A defined assignment. */
5904 : 544 : if (cnext && cnext->resolved_sym)
5905 : 44 : return gfc_trans_call (cnext, true, mask, count1, invert);
5906 : :
5907 : : #if 0
5908 : : /* TODO: handle this special case.
5909 : : Special case a single function returning an array. */
5910 : : if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
5911 : : {
5912 : : tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
5913 : : if (tmp)
5914 : : return tmp;
5915 : : }
5916 : : #endif
5917 : :
5918 : : /* Assignment of the form lhs = rhs. */
5919 : 500 : gfc_start_block (&block);
5920 : :
5921 : 500 : gfc_init_se (&lse, NULL);
5922 : 500 : gfc_init_se (&rse, NULL);
5923 : :
5924 : : /* Walk the lhs. */
5925 : 500 : lss = gfc_walk_expr (expr1);
5926 : 500 : rss = NULL;
5927 : :
5928 : : /* In each where-assign-stmt, the mask-expr and the variable being
5929 : : defined shall be arrays of the same shape. */
5930 : 500 : gcc_assert (lss != gfc_ss_terminator);
5931 : :
5932 : : /* The assignment needs scalarization. */
5933 : : lss_section = lss;
5934 : :
5935 : : /* Find a non-scalar SS from the lhs. */
5936 : : while (lss_section != gfc_ss_terminator
5937 : 500 : && lss_section->info->type != GFC_SS_SECTION)
5938 : 0 : lss_section = lss_section->next;
5939 : :
5940 : 500 : gcc_assert (lss_section != gfc_ss_terminator);
5941 : :
5942 : : /* Initialize the scalarizer. */
5943 : 500 : gfc_init_loopinfo (&loop);
5944 : :
5945 : : /* Walk the rhs. */
5946 : 500 : rss = gfc_walk_expr (expr2);
5947 : 500 : if (rss == gfc_ss_terminator)
5948 : : {
5949 : : /* The rhs is scalar. Add a ss for the expression. */
5950 : 343 : rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
5951 : 343 : rss->info->where = 1;
5952 : : }
5953 : :
5954 : : /* Associate the SS with the loop. */
5955 : 500 : gfc_add_ss_to_loop (&loop, lss);
5956 : 500 : gfc_add_ss_to_loop (&loop, rss);
5957 : :
5958 : : /* Calculate the bounds of the scalarization. */
5959 : 500 : gfc_conv_ss_startstride (&loop);
5960 : :
5961 : : /* Resolve any data dependencies in the statement. */
5962 : 500 : gfc_conv_resolve_dependencies (&loop, lss_section, rss);
5963 : :
5964 : : /* Setup the scalarizing loops. */
5965 : 500 : gfc_conv_loop_setup (&loop, &expr2->where);
5966 : :
5967 : : /* Setup the gfc_se structures. */
5968 : 500 : gfc_copy_loopinfo_to_se (&lse, &loop);
5969 : 500 : gfc_copy_loopinfo_to_se (&rse, &loop);
5970 : :
5971 : 500 : rse.ss = rss;
5972 : 500 : gfc_mark_ss_chain_used (rss, 1);
5973 : 500 : if (loop.temp_ss == NULL)
5974 : : {
5975 : 422 : lse.ss = lss;
5976 : 422 : gfc_mark_ss_chain_used (lss, 1);
5977 : : }
5978 : : else
5979 : : {
5980 : 78 : lse.ss = loop.temp_ss;
5981 : 78 : gfc_mark_ss_chain_used (lss, 3);
5982 : 78 : gfc_mark_ss_chain_used (loop.temp_ss, 3);
5983 : : }
5984 : :
5985 : : /* Start the scalarized loop body. */
5986 : 500 : gfc_start_scalarized_body (&loop, &body);
5987 : :
5988 : : /* Translate the expression. */
5989 : 500 : gfc_conv_expr (&rse, expr2);
5990 : 500 : if (lss != gfc_ss_terminator && loop.temp_ss != NULL)
5991 : 78 : gfc_conv_tmp_array_ref (&lse);
5992 : : else
5993 : 422 : gfc_conv_expr (&lse, expr1);
5994 : :
5995 : : /* Form the mask expression according to the mask. */
5996 : 500 : index = count1;
5997 : 500 : maskexpr = gfc_build_array_ref (mask, index, NULL);
5998 : 500 : if (invert)
5999 : 24 : maskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
6000 : 24 : TREE_TYPE (maskexpr), maskexpr);
6001 : :
6002 : : /* Use the scalar assignment as is. */
6003 : 1000 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
6004 : 500 : false, loop.temp_ss == NULL);
6005 : :
6006 : 500 : tmp = build3_v (COND_EXPR, maskexpr, tmp, build_empty_stmt (input_location));
6007 : :
6008 : 500 : gfc_add_expr_to_block (&body, tmp);
6009 : :
6010 : 500 : if (lss == gfc_ss_terminator)
6011 : : {
6012 : : /* Increment count1. */
6013 : : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
6014 : : count1, gfc_index_one_node);
6015 : : gfc_add_modify (&body, count1, tmp);
6016 : :
6017 : : /* Use the scalar assignment as is. */
6018 : : gfc_add_block_to_block (&block, &body);
6019 : : }
6020 : : else
6021 : : {
6022 : 500 : gcc_assert (lse.ss == gfc_ss_terminator
6023 : : && rse.ss == gfc_ss_terminator);
6024 : :
6025 : 500 : if (loop.temp_ss != NULL)
6026 : : {
6027 : : /* Increment count1 before finish the main body of a scalarized
6028 : : expression. */
6029 : 78 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
6030 : : gfc_array_index_type, count1, gfc_index_one_node);
6031 : 78 : gfc_add_modify (&body, count1, tmp);
6032 : 78 : gfc_trans_scalarized_loop_boundary (&loop, &body);
6033 : :
6034 : : /* We need to copy the temporary to the actual lhs. */
6035 : 78 : gfc_init_se (&lse, NULL);
6036 : 78 : gfc_init_se (&rse, NULL);
6037 : 78 : gfc_copy_loopinfo_to_se (&lse, &loop);
6038 : 78 : gfc_copy_loopinfo_to_se (&rse, &loop);
6039 : :
6040 : 78 : rse.ss = loop.temp_ss;
6041 : 78 : lse.ss = lss;
6042 : :
6043 : 78 : gfc_conv_tmp_array_ref (&rse);
6044 : 78 : gfc_conv_expr (&lse, expr1);
6045 : :
6046 : 78 : gcc_assert (lse.ss == gfc_ss_terminator
6047 : : && rse.ss == gfc_ss_terminator);
6048 : :
6049 : : /* Form the mask expression according to the mask tree list. */
6050 : 78 : index = count2;
6051 : 78 : maskexpr = gfc_build_array_ref (mask, index, NULL);
6052 : 78 : if (invert)
6053 : 0 : maskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
6054 : 0 : TREE_TYPE (maskexpr), maskexpr);
6055 : :
6056 : : /* Use the scalar assignment as is. */
6057 : 78 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts, false, true);
6058 : 78 : tmp = build3_v (COND_EXPR, maskexpr, tmp,
6059 : : build_empty_stmt (input_location));
6060 : 78 : gfc_add_expr_to_block (&body, tmp);
6061 : :
6062 : : /* Increment count2. */
6063 : 78 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
6064 : : gfc_array_index_type, count2,
6065 : : gfc_index_one_node);
6066 : 78 : gfc_add_modify (&body, count2, tmp);
6067 : : }
6068 : : else
6069 : : {
6070 : : /* Increment count1. */
6071 : 422 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
6072 : : gfc_array_index_type, count1,
6073 : : gfc_index_one_node);
6074 : 422 : gfc_add_modify (&body, count1, tmp);
6075 : : }
6076 : :
6077 : : /* Generate the copying loops. */
6078 : 500 : gfc_trans_scalarizing_loops (&loop, &body);
6079 : :
6080 : : /* Wrap the whole thing up. */
6081 : 500 : gfc_add_block_to_block (&block, &loop.pre);
6082 : 500 : gfc_add_block_to_block (&block, &loop.post);
6083 : 500 : gfc_cleanup_loop (&loop);
6084 : : }
6085 : :
6086 : 500 : return gfc_finish_block (&block);
6087 : : }
6088 : :
6089 : :
6090 : : /* Translate the WHERE construct or statement.
6091 : : This function can be called iteratively to translate the nested WHERE
6092 : : construct or statement.
6093 : : MASK is the control mask. */
6094 : :
6095 : : static void
6096 : 349 : gfc_trans_where_2 (gfc_code * code, tree mask, bool invert,
6097 : : forall_info * nested_forall_info, stmtblock_t * block)
6098 : : {
6099 : 349 : stmtblock_t inner_size_body;
6100 : 349 : tree inner_size, size;
6101 : 349 : gfc_ss *lss, *rss;
6102 : 349 : tree mask_type;
6103 : 349 : gfc_expr *expr1;
6104 : 349 : gfc_expr *expr2;
6105 : 349 : gfc_code *cblock;
6106 : 349 : gfc_code *cnext;
6107 : 349 : tree tmp;
6108 : 349 : tree cond;
6109 : 349 : tree count1, count2;
6110 : 349 : bool need_cmask;
6111 : 349 : bool need_pmask;
6112 : 349 : int need_temp;
6113 : 349 : tree pcmask = NULL_TREE;
6114 : 349 : tree ppmask = NULL_TREE;
6115 : 349 : tree cmask = NULL_TREE;
6116 : 349 : tree pmask = NULL_TREE;
6117 : 349 : gfc_actual_arglist *arg;
6118 : :
6119 : : /* the WHERE statement or the WHERE construct statement. */
6120 : 349 : cblock = code->block;
6121 : :
6122 : : /* As the mask array can be very big, prefer compact boolean types. */
6123 : 349 : mask_type = gfc_get_logical_type (gfc_logical_kinds[0].kind);
6124 : :
6125 : : /* Determine which temporary masks are needed. */
6126 : 349 : if (!cblock->block)
6127 : : {
6128 : : /* One clause: No ELSEWHEREs. */
6129 : 168 : need_cmask = (cblock->next != 0);
6130 : 168 : need_pmask = false;
6131 : : }
6132 : 181 : else if (cblock->block->block)
6133 : : {
6134 : : /* Three or more clauses: Conditional ELSEWHEREs. */
6135 : : need_cmask = true;
6136 : : need_pmask = true;
6137 : : }
6138 : 102 : else if (cblock->next)
6139 : : {
6140 : : /* Two clauses, the first non-empty. */
6141 : 84 : need_cmask = true;
6142 : 84 : need_pmask = (mask != NULL_TREE
6143 : 84 : && cblock->block->next != 0);
6144 : : }
6145 : 18 : else if (!cblock->block->next)
6146 : : {
6147 : : /* Two clauses, both empty. */
6148 : : need_cmask = false;
6149 : : need_pmask = false;
6150 : : }
6151 : : /* Two clauses, the first empty, the second non-empty. */
6152 : 9 : else if (mask)
6153 : : {
6154 : 0 : need_cmask = (cblock->block->expr1 != 0);
6155 : 0 : need_pmask = true;
6156 : : }
6157 : : else
6158 : : {
6159 : : need_cmask = true;
6160 : : need_pmask = false;
6161 : : }
6162 : :
6163 : 168 : if (need_cmask || need_pmask)
6164 : : {
6165 : : /* Calculate the size of temporary needed by the mask-expr. */
6166 : 331 : gfc_init_block (&inner_size_body);
6167 : 331 : inner_size = compute_inner_temp_size (cblock->expr1, cblock->expr1,
6168 : : &inner_size_body, &lss, &rss);
6169 : :
6170 : 331 : gfc_free_ss_chain (lss);
6171 : 331 : gfc_free_ss_chain (rss);
6172 : :
6173 : : /* Calculate the total size of temporary needed. */
6174 : 331 : size = compute_overall_iter_number (nested_forall_info, inner_size,
6175 : : &inner_size_body, block);
6176 : :
6177 : : /* Check whether the size is negative. */
6178 : 331 : cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node, size,
6179 : : gfc_index_zero_node);
6180 : 331 : size = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type,
6181 : : cond, gfc_index_zero_node, size);
6182 : 331 : size = gfc_evaluate_now (size, block);
6183 : :
6184 : : /* Allocate temporary for WHERE mask if needed. */
6185 : 331 : if (need_cmask)
6186 : 331 : cmask = allocate_temp_for_forall_nest_1 (mask_type, size, block,
6187 : : &pcmask);
6188 : :
6189 : : /* Allocate temporary for !mask if needed. */
6190 : 331 : if (need_pmask)
6191 : 83 : pmask = allocate_temp_for_forall_nest_1 (mask_type, size, block,
6192 : : &ppmask);
6193 : : }
6194 : :
6195 : 994 : while (cblock)
6196 : : {
6197 : : /* Each time around this loop, the where clause is conditional
6198 : : on the value of mask and invert, which are updated at the
6199 : : bottom of the loop. */
6200 : :
6201 : : /* Has mask-expr. */
6202 : 645 : if (cblock->expr1)
6203 : : {
6204 : : /* Ensure that the WHERE mask will be evaluated exactly once.
6205 : : If there are no statements in this WHERE/ELSEWHERE clause,
6206 : : then we don't need to update the control mask (cmask).
6207 : : If this is the last clause of the WHERE construct, then
6208 : : we don't need to update the pending control mask (pmask). */
6209 : 527 : if (mask)
6210 : 234 : gfc_evaluate_where_mask (cblock->expr1, nested_forall_info,
6211 : : mask, invert,
6212 : 234 : cblock->next ? cmask : NULL_TREE,
6213 : 234 : cblock->block ? pmask : NULL_TREE,
6214 : : mask_type, block);
6215 : : else
6216 : 293 : gfc_evaluate_where_mask (cblock->expr1, nested_forall_info,
6217 : : NULL_TREE, false,
6218 : 293 : (cblock->next || cblock->block)
6219 : : ? cmask : NULL_TREE,
6220 : : NULL_TREE, mask_type, block);
6221 : :
6222 : : invert = false;
6223 : : }
6224 : : /* It's a final elsewhere-stmt. No mask-expr is present. */
6225 : : else
6226 : : cmask = mask;
6227 : :
6228 : : /* The body of this where clause are controlled by cmask with
6229 : : sense specified by invert. */
6230 : :
6231 : : /* Get the assignment statement of a WHERE statement, or the first
6232 : : statement in where-body-construct of a WHERE construct. */
6233 : 645 : cnext = cblock->next;
6234 : 1272 : while (cnext)
6235 : : {
6236 : 627 : switch (cnext->op)
6237 : : {
6238 : : /* WHERE assignment statement. */
6239 : 44 : case EXEC_ASSIGN_CALL:
6240 : :
6241 : 44 : arg = cnext->ext.actual;
6242 : 44 : expr1 = expr2 = NULL;
6243 : 132 : for (; arg; arg = arg->next)
6244 : : {
6245 : 88 : if (!arg->expr)
6246 : 0 : continue;
6247 : 88 : if (expr1 == NULL)
6248 : : expr1 = arg->expr;
6249 : : else
6250 : 44 : expr2 = arg->expr;
6251 : : }
6252 : 44 : goto evaluate;
6253 : :
6254 : 527 : case EXEC_ASSIGN:
6255 : 527 : expr1 = cnext->expr1;
6256 : 527 : expr2 = cnext->expr2;
6257 : 571 : evaluate:
6258 : 571 : if (nested_forall_info != NULL)
6259 : : {
6260 : 65 : need_temp = gfc_check_dependency (expr1, expr2, 0);
6261 : 65 : if ((need_temp || flag_test_forall_temp)
6262 : 28 : && cnext->op != EXEC_ASSIGN_CALL)
6263 : 27 : gfc_trans_assign_need_temp (expr1, expr2,
6264 : : cmask, invert,
6265 : : nested_forall_info, block);
6266 : : else
6267 : : {
6268 : : /* Variables to control maskexpr. */
6269 : 38 : count1 = gfc_create_var (gfc_array_index_type, "count1");
6270 : 38 : count2 = gfc_create_var (gfc_array_index_type, "count2");
6271 : 38 : gfc_add_modify (block, count1, gfc_index_zero_node);
6272 : 38 : gfc_add_modify (block, count2, gfc_index_zero_node);
6273 : :
6274 : 38 : tmp = gfc_trans_where_assign (expr1, expr2,
6275 : : cmask, invert,
6276 : : count1, count2,
6277 : : cnext);
6278 : :
6279 : 38 : tmp = gfc_trans_nested_forall_loop (nested_forall_info,
6280 : : tmp, 1);
6281 : 38 : gfc_add_expr_to_block (block, tmp);
6282 : : }
6283 : : }
6284 : : else
6285 : : {
6286 : : /* Variables to control maskexpr. */
6287 : 506 : count1 = gfc_create_var (gfc_array_index_type, "count1");
6288 : 506 : count2 = gfc_create_var (gfc_array_index_type, "count2");
6289 : 506 : gfc_add_modify (block, count1, gfc_index_zero_node);
6290 : 506 : gfc_add_modify (block, count2, gfc_index_zero_node);
6291 : :
6292 : 506 : tmp = gfc_trans_where_assign (expr1, expr2,
6293 : : cmask, invert,
6294 : : count1, count2,
6295 : : cnext);
6296 : 506 : gfc_add_expr_to_block (block, tmp);
6297 : :
6298 : : }
6299 : : break;
6300 : :
6301 : : /* WHERE or WHERE construct is part of a where-body-construct. */
6302 : 56 : case EXEC_WHERE:
6303 : 56 : gfc_trans_where_2 (cnext, cmask, invert,
6304 : : nested_forall_info, block);
6305 : 56 : break;
6306 : :
6307 : 0 : default:
6308 : 0 : gcc_unreachable ();
6309 : : }
6310 : :
6311 : : /* The next statement within the same where-body-construct. */
6312 : 627 : cnext = cnext->next;
6313 : : }
6314 : : /* The next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt. */
6315 : 645 : cblock = cblock->block;
6316 : 645 : if (mask == NULL_TREE)
6317 : : {
6318 : : /* If we're the initial WHERE, we can simply invert the sense
6319 : : of the current mask to obtain the "mask" for the remaining
6320 : : ELSEWHEREs. */
6321 : : invert = true;
6322 : : mask = cmask;
6323 : : }
6324 : : else
6325 : : {
6326 : : /* Otherwise, for nested WHERE's we need to use the pending mask. */
6327 : 343 : invert = false;
6328 : 343 : mask = pmask;
6329 : : }
6330 : : }
6331 : :
6332 : : /* If we allocated a pending mask array, deallocate it now. */
6333 : 349 : if (ppmask)
6334 : : {
6335 : 1 : tmp = gfc_call_free (ppmask);
6336 : 1 : gfc_add_expr_to_block (block, tmp);
6337 : : }
6338 : :
6339 : : /* If we allocated a current mask array, deallocate it now. */
6340 : 349 : if (pcmask)
6341 : : {
6342 : 43 : tmp = gfc_call_free (pcmask);
6343 : 43 : gfc_add_expr_to_block (block, tmp);
6344 : : }
6345 : 349 : }
6346 : :
6347 : : /* Translate a simple WHERE construct or statement without dependencies.
6348 : : CBLOCK is the "then" clause of the WHERE statement, where CBLOCK->EXPR
6349 : : is the mask condition, and EBLOCK if non-NULL is the "else" clause.
6350 : : Currently both CBLOCK and EBLOCK are restricted to single assignments. */
6351 : :
6352 : : static tree
6353 : 94 : gfc_trans_where_3 (gfc_code * cblock, gfc_code * eblock)
6354 : : {
6355 : 94 : stmtblock_t block, body;
6356 : 94 : gfc_expr *cond, *tdst, *tsrc, *edst, *esrc;
6357 : 94 : tree tmp, cexpr, tstmt, estmt;
6358 : 94 : gfc_ss *css, *tdss, *tsss;
6359 : 94 : gfc_se cse, tdse, tsse, edse, esse;
6360 : 94 : gfc_loopinfo loop;
6361 : 94 : gfc_ss *edss = 0;
6362 : 94 : gfc_ss *esss = 0;
6363 : 94 : bool maybe_workshare = false;
6364 : :
6365 : : /* Allow the scalarizer to workshare simple where loops. */
6366 : 94 : if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_BODY))
6367 : : == OMPWS_WORKSHARE_FLAG)
6368 : : {
6369 : 13 : maybe_workshare = true;
6370 : 13 : ompws_flags |= OMPWS_SCALARIZER_WS | OMPWS_SCALARIZER_BODY;
6371 : : }
6372 : :
6373 : 94 : cond = cblock->expr1;
6374 : 94 : tdst = cblock->next->expr1;
6375 : 94 : tsrc = cblock->next->expr2;
6376 : 94 : edst = eblock ? eblock->next->expr1 : NULL;
6377 : 13 : esrc = eblock ? eblock->next->expr2 : NULL;
6378 : :
6379 : 94 : gfc_start_block (&block);
6380 : 94 : gfc_init_loopinfo (&loop);
6381 : :
6382 : : /* Handle the condition. */
6383 : 94 : gfc_init_se (&cse, NULL);
6384 : 94 : css = gfc_walk_expr (cond);
6385 : 94 : gfc_add_ss_to_loop (&loop, css);
6386 : :
6387 : : /* Handle the then-clause. */
6388 : 94 : gfc_init_se (&tdse, NULL);
6389 : 94 : gfc_init_se (&tsse, NULL);
6390 : 94 : tdss = gfc_walk_expr (tdst);
6391 : 94 : tsss = gfc_walk_expr (tsrc);
6392 : 94 : if (tsss == gfc_ss_terminator)
6393 : : {
6394 : 58 : tsss = gfc_get_scalar_ss (gfc_ss_terminator, tsrc);
6395 : 58 : tsss->info->where = 1;
6396 : : }
6397 : 94 : gfc_add_ss_to_loop (&loop, tdss);
6398 : 94 : gfc_add_ss_to_loop (&loop, tsss);
6399 : :
6400 : 94 : if (eblock)
6401 : : {
6402 : : /* Handle the else clause. */
6403 : 13 : gfc_init_se (&edse, NULL);
6404 : 13 : gfc_init_se (&esse, NULL);
6405 : 13 : edss = gfc_walk_expr (edst);
6406 : 13 : esss = gfc_walk_expr (esrc);
6407 : 13 : if (esss == gfc_ss_terminator)
6408 : : {
6409 : 13 : esss = gfc_get_scalar_ss (gfc_ss_terminator, esrc);
6410 : 13 : esss->info->where = 1;
6411 : : }
6412 : 13 : gfc_add_ss_to_loop (&loop, edss);
6413 : 13 : gfc_add_ss_to_loop (&loop, esss);
6414 : : }
6415 : :
6416 : 94 : gfc_conv_ss_startstride (&loop);
6417 : 94 : gfc_conv_loop_setup (&loop, &tdst->where);
6418 : :
6419 : 94 : gfc_mark_ss_chain_used (css, 1);
6420 : 94 : gfc_mark_ss_chain_used (tdss, 1);
6421 : 94 : gfc_mark_ss_chain_used (tsss, 1);
6422 : 94 : if (eblock)
6423 : : {
6424 : 13 : gfc_mark_ss_chain_used (edss, 1);
6425 : 13 : gfc_mark_ss_chain_used (esss, 1);
6426 : : }
6427 : :
6428 : 94 : gfc_start_scalarized_body (&loop, &body);
6429 : :
6430 : 94 : gfc_copy_loopinfo_to_se (&cse, &loop);
6431 : 94 : gfc_copy_loopinfo_to_se (&tdse, &loop);
6432 : 94 : gfc_copy_loopinfo_to_se (&tsse, &loop);
6433 : 94 : cse.ss = css;
6434 : 94 : tdse.ss = tdss;
6435 : 94 : tsse.ss = tsss;
6436 : 94 : if (eblock)
6437 : : {
6438 : 13 : gfc_copy_loopinfo_to_se (&edse, &loop);
6439 : 13 : gfc_copy_loopinfo_to_se (&esse, &loop);
6440 : 13 : edse.ss = edss;
6441 : 13 : esse.ss = esss;
6442 : : }
6443 : :
6444 : 94 : gfc_conv_expr (&cse, cond);
6445 : 94 : gfc_add_block_to_block (&body, &cse.pre);
6446 : 94 : cexpr = cse.expr;
6447 : :
6448 : 94 : gfc_conv_expr (&tsse, tsrc);
6449 : 94 : if (tdss != gfc_ss_terminator && loop.temp_ss != NULL)
6450 : 0 : gfc_conv_tmp_array_ref (&tdse);
6451 : : else
6452 : 94 : gfc_conv_expr (&tdse, tdst);
6453 : :
6454 : 94 : if (eblock)
6455 : : {
6456 : 13 : gfc_conv_expr (&esse, esrc);
6457 : 13 : if (edss != gfc_ss_terminator && loop.temp_ss != NULL)
6458 : 0 : gfc_conv_tmp_array_ref (&edse);
6459 : : else
6460 : 13 : gfc_conv_expr (&edse, edst);
6461 : : }
6462 : :
6463 : 94 : tstmt = gfc_trans_scalar_assign (&tdse, &tsse, tdst->ts, false, true);
6464 : 94 : estmt = eblock ? gfc_trans_scalar_assign (&edse, &esse, edst->ts,
6465 : : false, true)
6466 : 81 : : build_empty_stmt (input_location);
6467 : 94 : tmp = build3_v (COND_EXPR, cexpr, tstmt, estmt);
6468 : 94 : gfc_add_expr_to_block (&body, tmp);
6469 : 94 : gfc_add_block_to_block (&body, &cse.post);
6470 : :
6471 : 94 : if (maybe_workshare)
6472 : 13 : ompws_flags &= ~OMPWS_SCALARIZER_BODY;
6473 : 94 : gfc_trans_scalarizing_loops (&loop, &body);
6474 : 94 : gfc_add_block_to_block (&block, &loop.pre);
6475 : 94 : gfc_add_block_to_block (&block, &loop.post);
6476 : 94 : gfc_cleanup_loop (&loop);
6477 : :
6478 : 94 : return gfc_finish_block (&block);
6479 : : }
6480 : :
6481 : : /* As the WHERE or WHERE construct statement can be nested, we call
6482 : : gfc_trans_where_2 to do the translation, and pass the initial
6483 : : NULL values for both the control mask and the pending control mask. */
6484 : :
6485 : : tree
6486 : 341 : gfc_trans_where (gfc_code * code)
6487 : : {
6488 : 341 : stmtblock_t block;
6489 : 341 : gfc_code *cblock;
6490 : 341 : gfc_code *eblock;
6491 : :
6492 : 341 : cblock = code->block;
6493 : 341 : if (cblock->next
6494 : 314 : && cblock->next->op == EXEC_ASSIGN
6495 : 269 : && !cblock->next->next)
6496 : : {
6497 : 267 : eblock = cblock->block;
6498 : 267 : if (!eblock)
6499 : : {
6500 : : /* A simple "WHERE (cond) x = y" statement or block is
6501 : : dependence free if cond is not dependent upon writing x,
6502 : : and the source y is unaffected by the destination x. */
6503 : 162 : if (!gfc_check_dependency (cblock->next->expr1,
6504 : : cblock->expr1, 0)
6505 : 268 : && !gfc_check_dependency (cblock->next->expr1,
6506 : 106 : cblock->next->expr2, 0))
6507 : 81 : return gfc_trans_where_3 (cblock, NULL);
6508 : : }
6509 : 105 : else if (!eblock->expr1
6510 : 35 : && !eblock->block
6511 : 35 : && eblock->next
6512 : 26 : && eblock->next->op == EXEC_ASSIGN
6513 : 25 : && !eblock->next->next)
6514 : : {
6515 : : /* A simple "WHERE (cond) x1 = y1 ELSEWHERE x2 = y2 ENDWHERE"
6516 : : block is dependence free if cond is not dependent on writes
6517 : : to x1 and x2, y1 is not dependent on writes to x2, and y2
6518 : : is not dependent on writes to x1, and both y's are not
6519 : : dependent upon their own x's. In addition to this, the
6520 : : final two dependency checks below exclude all but the same
6521 : : array reference if the where and elswhere destinations
6522 : : are the same. In short, this is VERY conservative and this
6523 : : is needed because the two loops, required by the standard
6524 : : are coalesced in gfc_trans_where_3. */
6525 : 25 : if (!gfc_check_dependency (cblock->next->expr1,
6526 : : cblock->expr1, 0)
6527 : 25 : && !gfc_check_dependency (eblock->next->expr1,
6528 : : cblock->expr1, 0)
6529 : 25 : && !gfc_check_dependency (cblock->next->expr1,
6530 : 25 : eblock->next->expr2, 1)
6531 : 19 : && !gfc_check_dependency (eblock->next->expr1,
6532 : 19 : cblock->next->expr2, 1)
6533 : 19 : && !gfc_check_dependency (cblock->next->expr1,
6534 : 19 : cblock->next->expr2, 1)
6535 : 19 : && !gfc_check_dependency (eblock->next->expr1,
6536 : 19 : eblock->next->expr2, 1)
6537 : 19 : && !gfc_check_dependency (cblock->next->expr1,
6538 : 19 : eblock->next->expr1, 0)
6539 : 44 : && !gfc_check_dependency (eblock->next->expr1,
6540 : 19 : cblock->next->expr1, 0))
6541 : 13 : return gfc_trans_where_3 (cblock, eblock);
6542 : : }
6543 : : }
6544 : :
6545 : 247 : gfc_start_block (&block);
6546 : :
6547 : 247 : gfc_trans_where_2 (code, NULL, false, NULL, &block);
6548 : :
6549 : 247 : return gfc_finish_block (&block);
6550 : : }
6551 : :
6552 : :
6553 : : /* CYCLE a DO loop. The label decl has already been created by
6554 : : gfc_trans_do(), it's in TREE_PURPOSE (backend_decl) of the gfc_code
6555 : : node at the head of the loop. We must mark the label as used. */
6556 : :
6557 : : tree
6558 : 106 : gfc_trans_cycle (gfc_code * code)
6559 : : {
6560 : 106 : tree cycle_label;
6561 : :
6562 : 106 : cycle_label = code->ext.which_construct->cycle_label;
6563 : 106 : gcc_assert (cycle_label);
6564 : :
6565 : 106 : TREE_USED (cycle_label) = 1;
6566 : 106 : return build1_v (GOTO_EXPR, cycle_label);
6567 : : }
6568 : :
6569 : :
6570 : : /* EXIT a DO loop. Similar to CYCLE, but now the label is in
6571 : : TREE_VALUE (backend_decl) of the gfc_code node at the head of the
6572 : : loop. */
6573 : :
6574 : : tree
6575 : 679 : gfc_trans_exit (gfc_code * code)
6576 : : {
6577 : 679 : tree exit_label;
6578 : :
6579 : 679 : exit_label = code->ext.which_construct->exit_label;
6580 : 679 : gcc_assert (exit_label);
6581 : :
6582 : 679 : TREE_USED (exit_label) = 1;
6583 : 679 : return build1_v (GOTO_EXPR, exit_label);
6584 : : }
6585 : :
6586 : :
6587 : : /* Get the initializer expression for the code and expr of an allocate.
6588 : : When no initializer is needed return NULL. */
6589 : :
6590 : : static gfc_expr *
6591 : 12915 : allocate_get_initializer (gfc_code * code, gfc_expr * expr)
6592 : : {
6593 : 12915 : if (!gfc_bt_struct (expr->ts.type) && expr->ts.type != BT_CLASS)
6594 : : return NULL;
6595 : :
6596 : : /* An explicit type was given in allocate ( T:: object). */
6597 : 3643 : if (code->ext.alloc.ts.type == BT_DERIVED
6598 : 3643 : && (code->ext.alloc.ts.u.derived->attr.alloc_comp
6599 : 505 : || gfc_has_default_initializer (code->ext.alloc.ts.u.derived)))
6600 : 345 : return gfc_default_initializer (&code->ext.alloc.ts);
6601 : :
6602 : 551 : if (gfc_bt_struct (expr->ts.type)
6603 : 3298 : && (expr->ts.u.derived->attr.alloc_comp
6604 : 1872 : || gfc_has_default_initializer (expr->ts.u.derived)))
6605 : 1328 : return gfc_default_initializer (&expr->ts);
6606 : :
6607 : 1970 : if (expr->ts.type == BT_CLASS
6608 : 1970 : && (CLASS_DATA (expr)->ts.u.derived->attr.alloc_comp
6609 : 461 : || gfc_has_default_initializer (CLASS_DATA (expr)->ts.u.derived)))
6610 : 173 : return gfc_default_initializer (&CLASS_DATA (expr)->ts);
6611 : :
6612 : : return NULL;
6613 : : }
6614 : :
6615 : : /* Translate the ALLOCATE statement. */
6616 : :
6617 : : tree
6618 : 13637 : gfc_trans_allocate (gfc_code * code, gfc_omp_namelist *omp_allocate)
6619 : : {
6620 : 13637 : gfc_alloc *al;
6621 : 13637 : gfc_expr *expr, *e3rhs = NULL, *init_expr;
6622 : 13637 : gfc_se se, se_sz;
6623 : 13637 : tree tmp;
6624 : 13637 : tree parm;
6625 : 13637 : tree stat;
6626 : 13637 : tree errmsg;
6627 : 13637 : tree errlen;
6628 : 13637 : tree label_errmsg;
6629 : 13637 : tree label_finish;
6630 : 13637 : tree memsz;
6631 : 13637 : tree al_vptr, al_len;
6632 : : /* If an expr3 is present, then store the tree for accessing its
6633 : : _vptr, and _len components in the variables, respectively. The
6634 : : element size, i.e. _vptr%size, is stored in expr3_esize. Any of
6635 : : the trees may be the NULL_TREE indicating that this is not
6636 : : available for expr3's type. */
6637 : 13637 : tree expr3, expr3_vptr, expr3_len, expr3_esize;
6638 : : /* Classify what expr3 stores. */
6639 : 13637 : enum { E3_UNSET = 0, E3_SOURCE, E3_MOLD, E3_DESC } e3_is;
6640 : 13637 : stmtblock_t block;
6641 : 13637 : stmtblock_t post;
6642 : 13637 : stmtblock_t final_block;
6643 : 13637 : tree nelems;
6644 : 13637 : bool upoly_expr, tmp_expr3_len_flag = false, al_len_needs_set, is_coarray;
6645 : 13637 : bool needs_caf_sync, caf_refs_comp;
6646 : 13637 : bool e3_has_nodescriptor = false;
6647 : 13637 : gfc_symtree *newsym = NULL;
6648 : 13637 : symbol_attribute caf_attr;
6649 : 13637 : gfc_actual_arglist *param_list;
6650 : 13637 : tree ts_string_length = NULL_TREE;
6651 : :
6652 : 13637 : if (!code->ext.alloc.list)
6653 : : return NULL_TREE;
6654 : :
6655 : 13637 : stat = tmp = memsz = al_vptr = al_len = NULL_TREE;
6656 : 13637 : expr3 = expr3_vptr = expr3_len = expr3_esize = NULL_TREE;
6657 : 13637 : label_errmsg = label_finish = errmsg = errlen = NULL_TREE;
6658 : 13637 : e3_is = E3_UNSET;
6659 : 13637 : is_coarray = needs_caf_sync = false;
6660 : :
6661 : 13637 : gfc_init_block (&block);
6662 : 13637 : gfc_init_block (&post);
6663 : 13637 : gfc_init_block (&final_block);
6664 : :
6665 : : /* STAT= (and maybe ERRMSG=) is present. */
6666 : 13637 : if (code->expr1)
6667 : : {
6668 : : /* STAT=. */
6669 : 266 : tree gfc_int4_type_node = gfc_get_int_type (4);
6670 : 266 : stat = gfc_create_var (gfc_int4_type_node, "stat");
6671 : :
6672 : : /* ERRMSG= only makes sense with STAT=. */
6673 : 266 : if (code->expr2)
6674 : : {
6675 : 68 : gfc_init_se (&se, NULL);
6676 : 68 : se.want_pointer = 1;
6677 : 68 : gfc_conv_expr_lhs (&se, code->expr2);
6678 : 68 : errmsg = se.expr;
6679 : 68 : errlen = se.string_length;
6680 : : }
6681 : : else
6682 : : {
6683 : 198 : errmsg = null_pointer_node;
6684 : 198 : errlen = build_int_cst (gfc_charlen_type_node, 0);
6685 : : }
6686 : :
6687 : : /* GOTO destinations. */
6688 : 266 : label_errmsg = gfc_build_label_decl (NULL_TREE);
6689 : 266 : label_finish = gfc_build_label_decl (NULL_TREE);
6690 : 266 : TREE_USED (label_finish) = 0;
6691 : : }
6692 : :
6693 : : /* When an expr3 is present evaluate it only once. The standards prevent a
6694 : : dependency of expr3 on the objects in the allocate list. An expr3 can
6695 : : be pre-evaluated in all cases. One just has to make sure, to use the
6696 : : correct way, i.e., to get the descriptor or to get a reference
6697 : : expression. */
6698 : 13637 : if (code->expr3)
6699 : : {
6700 : 3710 : bool vtab_needed = false, temp_var_needed = false,
6701 : 3710 : temp_obj_created = false;
6702 : :
6703 : 3710 : is_coarray = gfc_is_coarray (code->expr3);
6704 : :
6705 : 279 : if (code->expr3->expr_type == EXPR_FUNCTION && !code->expr3->mold
6706 : 3953 : && (gfc_is_class_array_function (code->expr3)
6707 : 213 : || gfc_is_alloc_class_scalar_function (code->expr3)))
6708 : 78 : code->expr3->must_finalize = 1;
6709 : :
6710 : : /* Figure whether we need the vtab from expr3. */
6711 : 7432 : for (al = code->ext.alloc.list; !vtab_needed && al != NULL;
6712 : 3722 : al = al->next)
6713 : 3722 : vtab_needed = (al->expr->ts.type == BT_CLASS);
6714 : :
6715 : 3710 : gfc_init_se (&se, NULL);
6716 : : /* When expr3 is a variable, i.e., a very simple expression, then
6717 : : convert it once here. If one has a source expression that has
6718 : : substring references, part-refs, or %re/%im inquiries, wrap the
6719 : : entity in parentheses to force evaluation of the expression. */
6720 : 3710 : if (code->expr3->expr_type == EXPR_VARIABLE
6721 : 3710 : && is_subref_array (code->expr3))
6722 : 60 : code->expr3 = gfc_get_parentheses (code->expr3);
6723 : :
6724 : 3710 : if (code->expr3->expr_type == EXPR_VARIABLE
6725 : 2432 : || code->expr3->expr_type == EXPR_ARRAY
6726 : 1450 : || code->expr3->expr_type == EXPR_CONSTANT)
6727 : : {
6728 : 2669 : if (!code->expr3->mold
6729 : 232 : || code->expr3->ts.type == BT_CHARACTER
6730 : 104 : || vtab_needed
6731 : 69 : || code->ext.alloc.arr_spec_from_expr3)
6732 : : {
6733 : : /* Convert expr3 to a tree. For all "simple" expression just
6734 : : get the descriptor or the reference, respectively, depending
6735 : : on the rank of the expr. */
6736 : 2669 : if (code->ext.alloc.arr_spec_from_expr3 || code->expr3->rank != 0)
6737 : 1532 : gfc_conv_expr_descriptor (&se, code->expr3);
6738 : : else
6739 : : {
6740 : 1137 : gfc_conv_expr_reference (&se, code->expr3);
6741 : :
6742 : : /* gfc_conv_expr_reference wraps POINTER_PLUS_EXPR in a
6743 : : NOP_EXPR, which prevents gfortran from getting the vptr
6744 : : from the source=-expression. Remove the NOP_EXPR and go
6745 : : with the POINTER_PLUS_EXPR in this case. */
6746 : 1137 : if (code->expr3->ts.type == BT_CLASS
6747 : 256 : && TREE_CODE (se.expr) == NOP_EXPR
6748 : 1293 : && (TREE_CODE (TREE_OPERAND (se.expr, 0))
6749 : : == POINTER_PLUS_EXPR
6750 : 138 : || is_coarray))
6751 : 30 : se.expr = TREE_OPERAND (se.expr, 0);
6752 : : }
6753 : : /* Create a temp variable only for component refs to prevent
6754 : : having to go through the full deref-chain each time and to
6755 : : simplify computation of array properties. */
6756 : 2669 : temp_var_needed = TREE_CODE (se.expr) == COMPONENT_REF;
6757 : : }
6758 : : }
6759 : : else
6760 : : {
6761 : : /* In all other cases evaluate the expr3. */
6762 : 1041 : symbol_attribute attr;
6763 : : /* Get the descriptor for all arrays, that are not allocatable or
6764 : : pointer, because the latter are descriptors already.
6765 : : The exception are function calls returning a class object:
6766 : : The descriptor is stored in their results _data component, which
6767 : : is easier to access, when first a temporary variable for the
6768 : : result is created and the descriptor retrieved from there. */
6769 : 1041 : attr = gfc_expr_attr (code->expr3);
6770 : 1041 : if (code->expr3->rank != 0
6771 : 248 : && ((!attr.allocatable && !attr.pointer)
6772 : 71 : || (code->expr3->expr_type == EXPR_FUNCTION
6773 : 71 : && (code->expr3->ts.type != BT_CLASS
6774 : 53 : || (code->expr3->value.function.isym
6775 : 12 : && code->expr3->value.function.isym
6776 : 12 : ->transformational)))))
6777 : 207 : gfc_conv_expr_descriptor (&se, code->expr3);
6778 : : else
6779 : 834 : gfc_conv_expr_reference (&se, code->expr3);
6780 : 1041 : if (code->expr3->ts.type == BT_CLASS)
6781 : 132 : gfc_conv_class_to_class (&se, code->expr3,
6782 : : code->expr3->ts,
6783 : : false, true,
6784 : : false, false);
6785 : 1041 : temp_obj_created = temp_var_needed = !VAR_P (se.expr);
6786 : : }
6787 : 3710 : gfc_add_block_to_block (&block, &se.pre);
6788 : 3710 : if (code->expr3->must_finalize)
6789 : : {
6790 : 78 : gfc_add_block_to_block (&final_block, &se.finalblock);
6791 : 78 : gfc_add_block_to_block (&final_block, &se.post);
6792 : : }
6793 : : else
6794 : 3632 : gfc_add_block_to_block (&post, &se.post);
6795 : :
6796 : : /* Special case when string in expr3 is scalar and has length zero. */
6797 : 3710 : if (code->expr3->ts.type == BT_CHARACTER
6798 : 793 : && code->expr3->rank == 0
6799 : 4204 : && integer_zerop (se.string_length))
6800 : : {
6801 : 6 : gfc_init_se (&se, NULL);
6802 : 6 : temp_var_needed = false;
6803 : 6 : expr3_len = build_zero_cst (gfc_charlen_type_node);
6804 : 6 : e3_is = E3_MOLD;
6805 : : }
6806 : : /* Prevent aliasing, i.e., se.expr may be already a
6807 : : variable declaration. */
6808 : 3704 : else if (se.expr != NULL_TREE && temp_var_needed)
6809 : : {
6810 : 907 : tree var, desc;
6811 : 907 : tmp = (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr))
6812 : 845 : || is_coarray
6813 : 833 : || (code->expr3->ts.type == BT_CHARACTER
6814 : 929 : && code->expr3->rank == 0)) ?
6815 : : se.expr
6816 : 811 : : build_fold_indirect_ref_loc (input_location, se.expr);
6817 : :
6818 : : /* Get the array descriptor and prepare it to be assigned to the
6819 : : temporary variable var. For classes the array descriptor is
6820 : : in the _data component and the object goes into the
6821 : : GFC_DECL_SAVED_DESCRIPTOR. */
6822 : 907 : if (code->expr3->ts.type == BT_CLASS
6823 : 187 : && code->expr3->rank != 0)
6824 : : {
6825 : : /* When an array_ref was in expr3, then the descriptor is the
6826 : : first operand. */
6827 : 96 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)) || is_coarray)
6828 : : {
6829 : 49 : desc = TREE_OPERAND (tmp, 0);
6830 : : }
6831 : : else
6832 : : {
6833 : 47 : desc = tmp;
6834 : 47 : tmp = gfc_class_data_get (tmp);
6835 : : }
6836 : 96 : if (code->ext.alloc.arr_spec_from_expr3)
6837 : 39 : e3_is = E3_DESC;
6838 : : }
6839 : : else
6840 : 823 : desc = !is_coarray ? se.expr
6841 : 12 : : TREE_OPERAND (TREE_OPERAND (se.expr, 0), 0);
6842 : : /* We need a regular (non-UID) symbol here, therefore give a
6843 : : prefix. */
6844 : 907 : var = gfc_create_var (TREE_TYPE (tmp), "source");
6845 : 907 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)) || is_coarray)
6846 : : {
6847 : 121 : gfc_allocate_lang_decl (var);
6848 : 121 : GFC_DECL_SAVED_DESCRIPTOR (var) = desc;
6849 : : }
6850 : 907 : gfc_add_modify_loc (input_location, &block, var, tmp);
6851 : :
6852 : 907 : expr3 = var;
6853 : 907 : if (se.string_length)
6854 : : /* Evaluate it assuming that it also is complicated like expr3. */
6855 : 29 : expr3_len = gfc_evaluate_now (se.string_length, &block);
6856 : : }
6857 : : else
6858 : : {
6859 : 2797 : expr3 = se.expr;
6860 : 2797 : expr3_len = se.string_length;
6861 : : }
6862 : :
6863 : : /* Deallocate any allocatable components in expressions that use a
6864 : : temporary object, i.e. are not a simple alias of to an EXPR_VARIABLE.
6865 : : E.g. temporaries of a function call need freeing of their components
6866 : : here. Explicit derived type allocation of class entities uses expr3
6867 : : to carry the default initializer. This must not be deallocated or
6868 : : finalized. */
6869 : 3710 : if ((code->expr3->ts.type == BT_DERIVED
6870 : 2442 : || code->expr3->ts.type == BT_CLASS)
6871 : 1789 : && (code->expr3->expr_type != EXPR_VARIABLE || temp_obj_created)
6872 : 1204 : && code->expr3->ts.u.derived->attr.alloc_comp
6873 : 302 : && !code->expr3->must_finalize
6874 : 296 : && !code->ext.alloc.expr3_not_explicit)
6875 : : {
6876 : 216 : tmp = gfc_deallocate_alloc_comp (code->expr3->ts.u.derived,
6877 : : expr3, code->expr3->rank);
6878 : 216 : gfc_prepend_expr_to_block (&post, tmp);
6879 : : }
6880 : :
6881 : : /* Store what the expr3 is to be used for. */
6882 : 3710 : if (e3_is == E3_UNSET)
6883 : 6054 : e3_is = expr3 != NULL_TREE ?
6884 : 3665 : (code->ext.alloc.arr_spec_from_expr3 ?
6885 : : E3_DESC
6886 : 2562 : : (code->expr3->mold ? E3_MOLD : E3_SOURCE))
6887 : : : E3_UNSET;
6888 : :
6889 : : /* Figure how to get the _vtab entry. This also obtains the tree
6890 : : expression for accessing the _len component, because only
6891 : : unlimited polymorphic objects, which are a subcategory of class
6892 : : types, have a _len component. */
6893 : 3710 : if (code->expr3->ts.type == BT_CLASS)
6894 : : {
6895 : 521 : gfc_expr *rhs;
6896 : 777 : tmp = expr3 != NULL_TREE && POINTER_TYPE_P (TREE_TYPE (expr3)) ?
6897 : 256 : build_fold_indirect_ref (expr3): expr3;
6898 : : /* Polymorphic SOURCE: VPTR must be determined at run time.
6899 : : expr3 may be a temporary array declaration, therefore check for
6900 : : GFC_CLASS_TYPE_P before trying to get the _vptr component. */
6901 : 521 : if (tmp != NULL_TREE
6902 : 521 : && (e3_is == E3_DESC
6903 : 440 : || (GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
6904 : 280 : && (VAR_P (tmp) || !code->expr3->ref))
6905 : 186 : || (VAR_P (tmp) && DECL_LANG_SPECIFIC (tmp))))
6906 : 440 : tmp = gfc_class_vptr_get (expr3);
6907 : : else
6908 : : {
6909 : 81 : rhs = gfc_find_and_cut_at_last_class_ref (code->expr3);
6910 : 81 : gfc_add_vptr_component (rhs);
6911 : 81 : gfc_init_se (&se, NULL);
6912 : 81 : se.want_pointer = 1;
6913 : 81 : gfc_conv_expr (&se, rhs);
6914 : 81 : tmp = se.expr;
6915 : 81 : gfc_free_expr (rhs);
6916 : : }
6917 : : /* Set the element size. */
6918 : 521 : expr3_esize = gfc_vptr_size_get (tmp);
6919 : 521 : if (vtab_needed)
6920 : 515 : expr3_vptr = tmp;
6921 : : /* Initialize the ref to the _len component. */
6922 : 521 : if (expr3_len == NULL_TREE && UNLIMITED_POLY (code->expr3))
6923 : : {
6924 : : /* Same like for retrieving the _vptr. */
6925 : 164 : if (expr3 != NULL_TREE && !code->expr3->ref)
6926 : 92 : expr3_len = gfc_class_len_get (expr3);
6927 : : else
6928 : : {
6929 : 72 : rhs = gfc_find_and_cut_at_last_class_ref (code->expr3);
6930 : 72 : gfc_add_len_component (rhs);
6931 : 72 : gfc_init_se (&se, NULL);
6932 : 72 : gfc_conv_expr (&se, rhs);
6933 : 72 : expr3_len = se.expr;
6934 : 72 : gfc_free_expr (rhs);
6935 : : }
6936 : : }
6937 : : }
6938 : : else
6939 : : {
6940 : : /* When the object to allocate is polymorphic type, then it
6941 : : needs its vtab set correctly, so deduce the required _vtab
6942 : : and _len from the source expression. */
6943 : 3189 : if (vtab_needed)
6944 : : {
6945 : : /* VPTR is fixed at compile time. */
6946 : 1193 : gfc_symbol *vtab;
6947 : :
6948 : 1193 : vtab = gfc_find_vtab (&code->expr3->ts);
6949 : 1193 : gcc_assert (vtab);
6950 : 1193 : expr3_vptr = gfc_get_symbol_decl (vtab);
6951 : 1193 : expr3_vptr = gfc_build_addr_expr (NULL_TREE,
6952 : : expr3_vptr);
6953 : : }
6954 : : /* _len component needs to be set, when ts is a character
6955 : : array. */
6956 : 3189 : if (expr3_len == NULL_TREE
6957 : 2396 : && code->expr3->ts.type == BT_CHARACTER)
6958 : : {
6959 : 0 : if (code->expr3->ts.u.cl
6960 : 0 : && code->expr3->ts.u.cl->length)
6961 : : {
6962 : 0 : gfc_init_se (&se, NULL);
6963 : 0 : gfc_conv_expr (&se, code->expr3->ts.u.cl->length);
6964 : 0 : gfc_add_block_to_block (&block, &se.pre);
6965 : 0 : expr3_len = gfc_evaluate_now (se.expr, &block);
6966 : : }
6967 : 0 : gcc_assert (expr3_len);
6968 : : }
6969 : : /* For character arrays only the kind's size is needed, because
6970 : : the array mem_size is _len * (elem_size = kind_size).
6971 : : For all other get the element size in the normal way. */
6972 : 3189 : if (code->expr3->ts.type == BT_CHARACTER)
6973 : 793 : expr3_esize = TYPE_SIZE_UNIT (
6974 : : gfc_get_char_type (code->expr3->ts.kind));
6975 : : else
6976 : 2396 : expr3_esize = TYPE_SIZE_UNIT (
6977 : : gfc_typenode_for_spec (&code->expr3->ts));
6978 : : }
6979 : 3710 : gcc_assert (expr3_esize);
6980 : 3710 : expr3_esize = fold_convert (sizetype, expr3_esize);
6981 : 3710 : if (e3_is == E3_MOLD)
6982 : : /* The expr3 is no longer valid after this point. */
6983 : 179 : expr3 = NULL_TREE;
6984 : : }
6985 : 9927 : else if (code->ext.alloc.ts.type != BT_UNKNOWN)
6986 : : {
6987 : : /* Compute the explicit typespec given only once for all objects
6988 : : to allocate. */
6989 : 1196 : if (code->ext.alloc.ts.type != BT_CHARACTER)
6990 : 762 : expr3_esize = TYPE_SIZE_UNIT (
6991 : : gfc_typenode_for_spec (&code->ext.alloc.ts));
6992 : 434 : else if (code->ext.alloc.ts.u.cl->length != NULL)
6993 : : {
6994 : 428 : gfc_expr *sz;
6995 : 428 : sz = gfc_copy_expr (code->ext.alloc.ts.u.cl->length);
6996 : 428 : gfc_init_se (&se_sz, NULL);
6997 : 428 : gfc_conv_expr (&se_sz, sz);
6998 : 428 : gfc_free_expr (sz);
6999 : 428 : ts_string_length = fold_convert (gfc_charlen_type_node, se_sz.expr);
7000 : 428 : tmp = gfc_get_char_type (code->ext.alloc.ts.kind);
7001 : 428 : tmp = TYPE_SIZE_UNIT (tmp);
7002 : 428 : tmp = fold_convert (TREE_TYPE (se_sz.expr), tmp);
7003 : 428 : gfc_add_block_to_block (&block, &se_sz.pre);
7004 : 428 : expr3_esize = fold_build2_loc (input_location, MULT_EXPR,
7005 : 428 : TREE_TYPE (se_sz.expr),
7006 : : tmp, se_sz.expr);
7007 : 428 : expr3_esize = gfc_evaluate_now (expr3_esize, &block);
7008 : : }
7009 : : else
7010 : : expr3_esize = NULL_TREE;
7011 : : }
7012 : :
7013 : : /* The routine gfc_trans_assignment () already implements all
7014 : : techniques needed. Unfortunately we may have a temporary
7015 : : variable for the source= expression here. When that is the
7016 : : case convert this variable into a temporary gfc_expr of type
7017 : : EXPR_VARIABLE and used it as rhs for the assignment. The
7018 : : advantage is, that we get scalarizer support for free,
7019 : : don't have to take care about scalar to array treatment and
7020 : : will benefit of every enhancements gfc_trans_assignment ()
7021 : : gets.
7022 : : No need to check whether e3_is is E3_UNSET, because that is
7023 : : done by expr3 != NULL_TREE.
7024 : : Exclude variables since the following block does not handle
7025 : : array sections. In any case, there is no harm in sending
7026 : : variables to gfc_trans_assignment because there is no
7027 : : evaluation of variables. */
7028 : 13637 : if (code->expr3)
7029 : : {
7030 : 3710 : if (code->expr3->expr_type != EXPR_VARIABLE
7031 : 2432 : && e3_is != E3_MOLD && expr3 != NULL_TREE
7032 : 6074 : && DECL_P (expr3) && DECL_ARTIFICIAL (expr3))
7033 : : {
7034 : : /* Build a temporary symtree and symbol. Do not add it to the current
7035 : : namespace to prevent accidentaly modifying a colliding
7036 : : symbol's as. */
7037 : 1989 : newsym = XCNEW (gfc_symtree);
7038 : : /* The name of the symtree should be unique, because gfc_create_var ()
7039 : : took care about generating the identifier. */
7040 : 1989 : newsym->name
7041 : 1989 : = gfc_get_string ("%s", IDENTIFIER_POINTER (DECL_NAME (expr3)));
7042 : 1989 : newsym->n.sym = gfc_new_symbol (newsym->name, NULL);
7043 : : /* The backend_decl is known. It is expr3, which is inserted
7044 : : here. */
7045 : 1989 : newsym->n.sym->backend_decl = expr3;
7046 : 1989 : e3rhs = gfc_get_expr ();
7047 : 1989 : e3rhs->rank = code->expr3->rank;
7048 : 1989 : e3rhs->corank = code->expr3->corank;
7049 : 1989 : e3rhs->symtree = newsym;
7050 : : /* Mark the symbol referenced or gfc_trans_assignment will bug. */
7051 : 1989 : newsym->n.sym->attr.referenced = 1;
7052 : 1989 : e3rhs->expr_type = EXPR_VARIABLE;
7053 : 1989 : e3rhs->where = code->expr3->where;
7054 : : /* Set the symbols type, upto it was BT_UNKNOWN. */
7055 : 1989 : if (IS_CLASS_ARRAY (code->expr3)
7056 : 54 : && code->expr3->expr_type == EXPR_FUNCTION
7057 : 42 : && code->expr3->value.function.isym
7058 : 12 : && code->expr3->value.function.isym->transformational)
7059 : : {
7060 : 12 : e3rhs->ts = CLASS_DATA (code->expr3)->ts;
7061 : : }
7062 : 1977 : else if (code->expr3->ts.type == BT_CLASS
7063 : 1977 : && !GFC_CLASS_TYPE_P (TREE_TYPE (expr3)))
7064 : 46 : e3rhs->ts = CLASS_DATA (code->expr3)->ts;
7065 : : else
7066 : 1931 : e3rhs->ts = code->expr3->ts;
7067 : 1989 : newsym->n.sym->ts = e3rhs->ts;
7068 : : /* Check whether the expr3 is array valued. */
7069 : 1989 : if (e3rhs->rank)
7070 : : {
7071 : 1223 : gfc_array_spec *arr;
7072 : 1223 : arr = gfc_get_array_spec ();
7073 : 1223 : arr->rank = e3rhs->rank;
7074 : 1223 : arr->corank = e3rhs->corank;
7075 : 1223 : arr->type = AS_DEFERRED;
7076 : : /* Set the dimension and pointer attribute for arrays
7077 : : to be on the safe side. */
7078 : 1223 : newsym->n.sym->attr.dimension = 1;
7079 : 1223 : newsym->n.sym->attr.pointer = 1;
7080 : 1223 : newsym->n.sym->as = arr;
7081 : 1223 : if (IS_CLASS_ARRAY (code->expr3)
7082 : 54 : && code->expr3->expr_type == EXPR_FUNCTION
7083 : 42 : && code->expr3->value.function.isym
7084 : 12 : && code->expr3->value.function.isym->transformational)
7085 : : {
7086 : 12 : gfc_array_spec *tarr;
7087 : 12 : tarr = gfc_get_array_spec ();
7088 : 12 : *tarr = *arr;
7089 : 12 : e3rhs->ts.u.derived->as = tarr;
7090 : : }
7091 : 1223 : gfc_add_full_array_ref (e3rhs, arr);
7092 : : }
7093 : 766 : else if (POINTER_TYPE_P (TREE_TYPE (expr3)))
7094 : 45 : newsym->n.sym->attr.pointer = 1;
7095 : : /* The string length is known, too. Set it for char arrays. */
7096 : 1989 : if (e3rhs->ts.type == BT_CHARACTER)
7097 : 287 : newsym->n.sym->ts.u.cl->backend_decl = expr3_len;
7098 : 1989 : gfc_commit_symbol (newsym->n.sym);
7099 : : }
7100 : : else
7101 : 1721 : e3rhs = gfc_copy_expr (code->expr3);
7102 : :
7103 : : // We need to propagate the bounds of the expr3 for source=/mold=.
7104 : : // However, for non-named arrays, the lbound has to be 1 and neither the
7105 : : // bound used inside the called function even when returning an
7106 : : // allocatable/pointer nor the zero used internally.
7107 : 3710 : if (e3_is == E3_DESC
7108 : 1142 : && code->expr3->expr_type != EXPR_VARIABLE)
7109 : 13637 : e3_has_nodescriptor = true;
7110 : : }
7111 : :
7112 : : /* Loop over all objects to allocate. */
7113 : 30225 : for (al = code->ext.alloc.list; al != NULL; al = al->next)
7114 : : {
7115 : 16588 : expr = gfc_copy_expr (al->expr);
7116 : : /* UNLIMITED_POLY () needs the _data component to be set, when
7117 : : expr is a unlimited polymorphic object. But the _data component
7118 : : has not been set yet, so check the derived type's attr for the
7119 : : unlimited polymorphic flag to be safe. */
7120 : 16588 : upoly_expr = UNLIMITED_POLY (expr)
7121 : 32583 : || (expr->ts.type == BT_DERIVED
7122 : 2420 : && expr->ts.u.derived->attr.unlimited_polymorphic);
7123 : 16588 : gfc_init_se (&se, NULL);
7124 : :
7125 : : /* For class types prepare the expressions to ref the _vptr
7126 : : and the _len component. The latter for unlimited polymorphic
7127 : : types only. */
7128 : 16588 : if (expr->ts.type == BT_CLASS)
7129 : : {
7130 : 3430 : gfc_expr *expr_ref_vptr, *expr_ref_len;
7131 : 3430 : gfc_add_data_component (expr);
7132 : : /* Prep the vptr handle. */
7133 : 3430 : expr_ref_vptr = gfc_copy_expr (al->expr);
7134 : 3430 : gfc_add_vptr_component (expr_ref_vptr);
7135 : 3430 : se.want_pointer = 1;
7136 : 3430 : gfc_conv_expr (&se, expr_ref_vptr);
7137 : 3430 : al_vptr = se.expr;
7138 : 3430 : se.want_pointer = 0;
7139 : 3430 : gfc_free_expr (expr_ref_vptr);
7140 : : /* Allocated unlimited polymorphic objects always have a _len
7141 : : component. */
7142 : 3430 : if (upoly_expr)
7143 : : {
7144 : 593 : expr_ref_len = gfc_copy_expr (al->expr);
7145 : 593 : gfc_add_len_component (expr_ref_len);
7146 : 593 : gfc_conv_expr (&se, expr_ref_len);
7147 : 593 : al_len = se.expr;
7148 : 593 : gfc_free_expr (expr_ref_len);
7149 : : }
7150 : : else
7151 : : /* In a loop ensure that all loop variable dependent variables
7152 : : are initialized at the same spot in all execution paths. */
7153 : : al_len = NULL_TREE;
7154 : : }
7155 : : else
7156 : : al_vptr = al_len = NULL_TREE;
7157 : :
7158 : 16588 : se.want_pointer = 1;
7159 : 16588 : se.descriptor_only = 1;
7160 : :
7161 : 16588 : gfc_conv_expr (&se, expr);
7162 : 16588 : if (expr->ts.type == BT_CHARACTER && expr->ts.deferred)
7163 : : /* se.string_length now stores the .string_length variable of expr
7164 : : needed to allocate character(len=:) arrays. */
7165 : 1037 : al_len = se.string_length;
7166 : :
7167 : 16588 : al_len_needs_set = al_len != NULL_TREE;
7168 : : /* When allocating an array one cannot use much of the
7169 : : pre-evaluated expr3 expressions, because for most of them the
7170 : : scalarizer is needed which is not available in the pre-evaluation
7171 : : step. Therefore gfc_array_allocate () is responsible (and able)
7172 : : to handle the complete array allocation. Only the element size
7173 : : needs to be provided, which is done most of the time by the
7174 : : pre-evaluation step. */
7175 : 16588 : nelems = NULL_TREE;
7176 : 16588 : if (expr3_len && (code->expr3->ts.type == BT_CHARACTER
7177 : 963 : || code->expr3->ts.type == BT_CLASS))
7178 : : {
7179 : : /* When al is an array, then the element size for each element
7180 : : in the array is needed, which is the product of the len and
7181 : : esize for char arrays. For unlimited polymorphics len can be
7182 : : zero, therefore take the maximum of len and one. */
7183 : 963 : tree lhs_len;
7184 : :
7185 : : /* If an allocatable character variable has fixed length, use it.
7186 : : Otherwise use source length. As different lengths are not
7187 : : allowed by the standard, generate a runtime check. */
7188 : 963 : if (expr->ts.type == BT_CHARACTER && !expr->ts.deferred)
7189 : : {
7190 : 111 : gfc_trans_same_strlen_check ("ALLOCATE with SOURCE= or MOLD=",
7191 : : &code->expr3->where,
7192 : : se.string_length, expr3_len,
7193 : : &block);
7194 : 111 : lhs_len = fold_convert (TREE_TYPE (expr3_len), se.string_length);
7195 : : }
7196 : : else
7197 : : lhs_len = expr3_len;
7198 : :
7199 : 1926 : tmp = fold_build2_loc (input_location, MAX_EXPR,
7200 : 963 : TREE_TYPE (expr3_len),
7201 : 963 : lhs_len, fold_convert (TREE_TYPE (expr3_len),
7202 : : integer_one_node));
7203 : 1926 : tmp = fold_build2_loc (input_location, MULT_EXPR,
7204 : 963 : TREE_TYPE (expr3_esize), expr3_esize,
7205 : 963 : fold_convert (TREE_TYPE (expr3_esize), tmp));
7206 : 963 : }
7207 : : else
7208 : : tmp = expr3_esize;
7209 : :
7210 : : /* Create runtime check for ALLOCATE of character with type-spec. */
7211 : 16588 : if (expr->ts.type == BT_CHARACTER && !expr->ts.deferred
7212 : 737 : && ts_string_length
7213 : 19 : && se.string_length)
7214 : 19 : gfc_trans_same_strlen_check ("ALLOCATE with type-spec",
7215 : 19 : &al->expr->where,
7216 : : ts_string_length, se.string_length,
7217 : : &block);
7218 : :
7219 : 16588 : gfc_omp_namelist *omp_alloc_item = NULL;
7220 : 16588 : if (omp_allocate)
7221 : : {
7222 : : gfc_omp_namelist *n = NULL;
7223 : : gfc_omp_namelist *n_null = NULL;
7224 : 130 : for (n = omp_allocate; n; n = n->next)
7225 : : {
7226 : 88 : if (n->sym == NULL)
7227 : : {
7228 : 41 : n_null = n;
7229 : 41 : continue;
7230 : : }
7231 : 47 : if (expr->expr_type == EXPR_VARIABLE
7232 : 47 : && expr->symtree->n.sym == n->sym)
7233 : : {
7234 : 25 : gfc_ref *ref;
7235 : 38 : for (ref = expr->ref; ref; ref = ref->next)
7236 : 16 : if (ref->type == REF_COMPONENT)
7237 : : break;
7238 : : if (ref == NULL)
7239 : : break;
7240 : : }
7241 : : }
7242 : 64 : omp_alloc_item = n ? n : n_null;
7243 : :
7244 : : }
7245 : :
7246 : 16588 : if (!gfc_array_allocate (&se, expr, stat, errmsg, errlen,
7247 : : label_finish, tmp, &nelems,
7248 : : e3rhs ? e3rhs : code->expr3,
7249 : : e3_is == E3_DESC ? expr3 : NULL_TREE,
7250 : : e3_has_nodescriptor, omp_alloc_item,
7251 : 16588 : code->ext.alloc.ts.type != BT_UNKNOWN))
7252 : : {
7253 : : /* A scalar or derived type. First compute the size to
7254 : : allocate.
7255 : :
7256 : : expr3_len is set when expr3 is an unlimited polymorphic
7257 : : object or a deferred length string.
7258 : :
7259 : : If an allocatable character variable has fixed length, use it.
7260 : : Otherwise use source length. As different lengths are not
7261 : : allowed by the standard, a runtime check was inserted
7262 : : above. */
7263 : 5014 : if (expr3_len != NULL_TREE)
7264 : : {
7265 : 537 : tree lhs_len;
7266 : 537 : if (expr->ts.type == BT_CHARACTER && !expr->ts.deferred)
7267 : 56 : lhs_len = fold_convert (TREE_TYPE (expr3_len),
7268 : : se.string_length);
7269 : : else
7270 : : lhs_len = expr3_len;
7271 : :
7272 : 537 : tmp = fold_convert (TREE_TYPE (expr3_esize), lhs_len);
7273 : 537 : tmp = fold_build2_loc (input_location, MULT_EXPR,
7274 : 537 : TREE_TYPE (expr3_esize),
7275 : : expr3_esize, tmp);
7276 : 537 : if (code->expr3->ts.type != BT_CLASS)
7277 : : /* expr3 is a deferred length string, i.e., we are
7278 : : done. */
7279 : : memsz = tmp;
7280 : : else
7281 : : {
7282 : : /* For unlimited polymorphic enties build
7283 : : (len > 0) ? element_size * len : element_size
7284 : : to compute the number of bytes to allocate.
7285 : : This allows the allocation of unlimited polymorphic
7286 : : objects from an expr3 that is also unlimited
7287 : : polymorphic and stores a _len dependent object,
7288 : : e.g., a string. */
7289 : 98 : memsz = fold_build2_loc (input_location, GT_EXPR,
7290 : : logical_type_node, expr3_len,
7291 : : build_zero_cst
7292 : 98 : (TREE_TYPE (expr3_len)));
7293 : 98 : memsz = fold_build3_loc (input_location, COND_EXPR,
7294 : 98 : TREE_TYPE (expr3_esize),
7295 : : memsz, tmp, expr3_esize);
7296 : : }
7297 : : }
7298 : 4477 : else if (expr3_esize != NULL_TREE)
7299 : : /* Any other object in expr3 just needs element size in
7300 : : bytes. */
7301 : : memsz = expr3_esize;
7302 : 2743 : else if ((expr->ts.type == BT_CHARACTER && expr->ts.deferred)
7303 : 2743 : || (upoly_expr
7304 : 0 : && code->ext.alloc.ts.type == BT_CHARACTER))
7305 : : {
7306 : : /* Allocating deferred length char arrays need the length
7307 : : to allocate in the alloc_type_spec. But also unlimited
7308 : : polymorphic objects may be allocated as char arrays.
7309 : : Both are handled here. */
7310 : 0 : gfc_init_se (&se_sz, NULL);
7311 : 0 : gfc_conv_expr (&se_sz, code->ext.alloc.ts.u.cl->length);
7312 : 0 : gfc_add_block_to_block (&se.pre, &se_sz.pre);
7313 : 0 : se_sz.expr = gfc_evaluate_now (se_sz.expr, &se.pre);
7314 : 0 : gfc_add_block_to_block (&se.pre, &se_sz.post);
7315 : 0 : expr3_len = se_sz.expr;
7316 : 0 : tmp_expr3_len_flag = true;
7317 : 0 : tmp = TYPE_SIZE_UNIT (
7318 : : gfc_get_char_type (code->ext.alloc.ts.kind));
7319 : 0 : memsz = fold_build2_loc (input_location, MULT_EXPR,
7320 : 0 : TREE_TYPE (tmp),
7321 : 0 : fold_convert (TREE_TYPE (tmp),
7322 : : expr3_len),
7323 : : tmp);
7324 : : }
7325 : 2743 : else if (expr->ts.type == BT_CHARACTER)
7326 : : {
7327 : : /* Compute the number of bytes needed to allocate a fixed
7328 : : length char array. */
7329 : 164 : gcc_assert (se.string_length != NULL_TREE);
7330 : 164 : tmp = TYPE_SIZE_UNIT (gfc_get_char_type (expr->ts.kind));
7331 : 328 : memsz = fold_build2_loc (input_location, MULT_EXPR,
7332 : 164 : TREE_TYPE (tmp), tmp,
7333 : 164 : fold_convert (TREE_TYPE (tmp),
7334 : : se.string_length));
7335 : : }
7336 : 2579 : else if (code->ext.alloc.ts.type != BT_UNKNOWN)
7337 : : /* Handle all types, where the alloc_type_spec is set. */
7338 : 0 : memsz = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&code->ext.alloc.ts));
7339 : : else
7340 : : /* Handle size computation of the type declared to alloc. */
7341 : 2579 : memsz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (se.expr)));
7342 : :
7343 : 5014 : bool use_coarray_alloc
7344 : 5014 : = (flag_coarray == GFC_FCOARRAY_LIB
7345 : 5014 : && (caf_attr = gfc_caf_attr (expr, true, &caf_refs_comp))
7346 : 70 : .codimension);
7347 : 5014 : tree omp_cond = NULL_TREE;
7348 : 5014 : tree omp_alt_alloc = NULL_TREE;
7349 : 5014 : tree succ_add_expr = NULL_TREE;
7350 : 5014 : if (!use_coarray_alloc && omp_alloc_item)
7351 : : {
7352 : 28 : tree align, alloc, sz;
7353 : 28 : gfc_se se2;
7354 : :
7355 : 28 : omp_cond = boolean_true_node;
7356 : 28 : if (omp_alloc_item->u2.allocator)
7357 : : {
7358 : 2 : gfc_init_se (&se2, NULL);
7359 : 2 : gfc_conv_expr (&se2, omp_alloc_item->u2.allocator);
7360 : 2 : gfc_add_block_to_block (&se.pre, &se2.pre);
7361 : 2 : alloc = gfc_evaluate_now (se2.expr, &se.pre);
7362 : 2 : gfc_add_block_to_block (&se.pre, &se2.post);
7363 : : }
7364 : : else
7365 : 26 : alloc = build_zero_cst (ptr_type_node);
7366 : 28 : tmp = TREE_TYPE (TREE_TYPE (se.expr));
7367 : 28 : if (tmp == void_type_node)
7368 : 3 : tmp = gfc_typenode_for_spec (&expr->ts, 0);
7369 : 28 : if (omp_alloc_item->u.align)
7370 : : {
7371 : 14 : gfc_init_se (&se2, NULL);
7372 : 14 : gfc_conv_expr (&se2, omp_alloc_item->u.align);
7373 : 14 : gcc_assert (CONSTANT_CLASS_P (se2.expr)
7374 : : && se2.pre.head == NULL
7375 : : && se2.post.head == NULL);
7376 : 14 : align = build_int_cst (size_type_node,
7377 : 14 : MAX (tree_to_uhwi (se2.expr),
7378 : : TYPE_ALIGN_UNIT (tmp)));
7379 : : }
7380 : : else
7381 : 14 : align = build_int_cst (size_type_node, TYPE_ALIGN_UNIT (tmp));
7382 : 28 : sz = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
7383 : : fold_convert (size_type_node, memsz),
7384 : : build_int_cst (size_type_node, 1));
7385 : 28 : omp_alt_alloc = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
7386 : 28 : DECL_ATTRIBUTES (omp_alt_alloc)
7387 : 28 : = tree_cons (get_identifier ("omp allocator"),
7388 : : build_tree_list (NULL_TREE, alloc),
7389 : 28 : DECL_ATTRIBUTES (omp_alt_alloc));
7390 : 28 : omp_alt_alloc = build_call_expr (omp_alt_alloc, 3, align, sz, alloc);
7391 : 28 : succ_add_expr = gfc_omp_call_add_alloc (se.expr);
7392 : : }
7393 : :
7394 : : /* Store the caf-attributes for latter use. */
7395 : 5014 : if (use_coarray_alloc)
7396 : : {
7397 : : /* Scalar allocatable components in coarray'ed derived types make
7398 : : it here and are treated now. */
7399 : 65 : tree caf_decl, token;
7400 : 65 : gfc_se caf_se;
7401 : :
7402 : 65 : is_coarray = true;
7403 : : /* Set flag, to add synchronize after the allocate. */
7404 : 130 : needs_caf_sync = needs_caf_sync
7405 : 65 : || caf_attr.coarray_comp || !caf_refs_comp;
7406 : :
7407 : 65 : gfc_init_se (&caf_se, NULL);
7408 : :
7409 : 65 : caf_decl = gfc_get_tree_for_caf_expr (expr);
7410 : 65 : gfc_get_caf_token_offset (&caf_se, &token, NULL, caf_decl,
7411 : : NULL_TREE, NULL);
7412 : 65 : gfc_add_block_to_block (&se.pre, &caf_se.pre);
7413 : 65 : gfc_allocate_allocatable (&se.pre, se.expr, memsz,
7414 : : gfc_build_addr_expr (NULL_TREE, token),
7415 : : NULL_TREE, NULL_TREE, NULL_TREE,
7416 : : label_finish, expr, 1);
7417 : : }
7418 : : /* Allocate - for non-pointers with re-alloc checking. */
7419 : 4949 : else if (gfc_expr_attr (expr).allocatable)
7420 : 3398 : gfc_allocate_allocatable (&se.pre, se.expr, memsz,
7421 : : NULL_TREE, stat, errmsg, errlen,
7422 : : label_finish, expr, 0,
7423 : : omp_cond, omp_alt_alloc, succ_add_expr);
7424 : : else
7425 : 1551 : gfc_allocate_using_malloc (&se.pre, se.expr, memsz, stat,
7426 : : omp_cond, omp_alt_alloc, succ_add_expr);
7427 : : }
7428 : : else
7429 : : {
7430 : : /* Allocating coarrays needs a sync after the allocate executed.
7431 : : Set the flag to add the sync after all objects are allocated. */
7432 : 11574 : if (flag_coarray == GFC_FCOARRAY_LIB
7433 : 11574 : && (caf_attr = gfc_caf_attr (expr, true, &caf_refs_comp))
7434 : 344 : .codimension)
7435 : : {
7436 : 302 : is_coarray = true;
7437 : 302 : needs_caf_sync = needs_caf_sync
7438 : 302 : || caf_attr.coarray_comp || !caf_refs_comp;
7439 : : }
7440 : :
7441 : 11574 : if (expr->ts.type == BT_CHARACTER && al_len != NULL_TREE
7442 : 1013 : && expr3_len != NULL_TREE)
7443 : : {
7444 : : /* Arrays need to have a _len set before the array
7445 : : descriptor is filled. */
7446 : 284 : gfc_add_modify (&block, al_len,
7447 : 284 : fold_convert (TREE_TYPE (al_len), expr3_len));
7448 : : /* Prevent setting the length twice. */
7449 : 284 : al_len_needs_set = false;
7450 : : }
7451 : 11290 : else if (expr->ts.type == BT_CHARACTER && al_len != NULL_TREE
7452 : 222 : && code->ext.alloc.ts.u.cl->length)
7453 : : {
7454 : : /* Cover the cases where a string length is explicitly
7455 : : specified by a type spec for deferred length character
7456 : : arrays or unlimited polymorphic objects without a
7457 : : source= or mold= expression. */
7458 : 222 : gfc_init_se (&se_sz, NULL);
7459 : 222 : gfc_conv_expr (&se_sz, code->ext.alloc.ts.u.cl->length);
7460 : 222 : gfc_add_block_to_block (&block, &se_sz.pre);
7461 : 222 : gfc_add_modify (&block, al_len,
7462 : 222 : fold_convert (TREE_TYPE (al_len),
7463 : : se_sz.expr));
7464 : 222 : al_len_needs_set = false;
7465 : : }
7466 : : }
7467 : :
7468 : 16588 : gfc_add_block_to_block (&block, &se.pre);
7469 : :
7470 : : /* Error checking -- Note: ERRMSG only makes sense with STAT. */
7471 : 16588 : if (code->expr1)
7472 : : {
7473 : 278 : tmp = build1_v (GOTO_EXPR, label_errmsg);
7474 : 278 : parm = fold_build2_loc (input_location, NE_EXPR,
7475 : : logical_type_node, stat,
7476 : 278 : build_int_cst (TREE_TYPE (stat), 0));
7477 : 278 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
7478 : : gfc_unlikely (parm, PRED_FORTRAN_FAIL_ALLOC),
7479 : : tmp, build_empty_stmt (input_location));
7480 : 278 : gfc_add_expr_to_block (&block, tmp);
7481 : : }
7482 : :
7483 : : /* Set the vptr only when no source= is set. When source= is set, then
7484 : : the trans_assignment below will set the vptr. */
7485 : 16588 : if (al_vptr != NULL_TREE && (!code->expr3 || code->expr3->mold))
7486 : : {
7487 : 1669 : if (expr3_vptr != NULL_TREE)
7488 : : /* The vtab is already known, so just assign it. */
7489 : 73 : gfc_add_modify (&block, al_vptr,
7490 : 73 : fold_convert (TREE_TYPE (al_vptr), expr3_vptr));
7491 : : else
7492 : : {
7493 : : /* VPTR is fixed at compile time. */
7494 : 1596 : gfc_symbol *vtab;
7495 : 1596 : gfc_typespec *ts;
7496 : :
7497 : 1596 : if (code->expr3)
7498 : : /* Although expr3 is pre-evaluated above, it may happen,
7499 : : that for arrays or in mold= cases the pre-evaluation
7500 : : was not successful. In these rare cases take the vtab
7501 : : from the typespec of expr3 here. */
7502 : 0 : ts = &code->expr3->ts;
7503 : 1596 : else if (code->ext.alloc.ts.type == BT_DERIVED || upoly_expr)
7504 : : /* The alloc_type_spec gives the type to allocate or the
7505 : : al is unlimited polymorphic, which enforces the use of
7506 : : an alloc_type_spec that is not necessarily a BT_DERIVED. */
7507 : 713 : ts = &code->ext.alloc.ts;
7508 : : else
7509 : : /* Prepare for setting the vtab as declared. */
7510 : 883 : ts = &expr->ts;
7511 : :
7512 : 1596 : vtab = gfc_find_vtab (ts);
7513 : 1596 : gcc_assert (vtab);
7514 : 1596 : tmp = gfc_build_addr_expr (NULL_TREE,
7515 : : gfc_get_symbol_decl (vtab));
7516 : 1596 : gfc_add_modify (&block, al_vptr,
7517 : 1596 : fold_convert (TREE_TYPE (al_vptr), tmp));
7518 : : }
7519 : : }
7520 : :
7521 : : /* Add assignment for string length. */
7522 : 16588 : if (al_len != NULL_TREE && al_len_needs_set)
7523 : : {
7524 : 1124 : if (expr3_len != NULL_TREE)
7525 : : {
7526 : 568 : gfc_add_modify (&block, al_len,
7527 : 568 : fold_convert (TREE_TYPE (al_len),
7528 : : expr3_len));
7529 : : /* When tmp_expr3_len_flag is set, then expr3_len is
7530 : : abused to carry the length information from the
7531 : : alloc_type. Clear it to prevent setting incorrect len
7532 : : information in future loop iterations. */
7533 : 568 : if (tmp_expr3_len_flag)
7534 : : /* No need to reset tmp_expr3_len_flag, because the
7535 : : presence of an expr3 cannot change within in the
7536 : : loop. */
7537 : 556 : expr3_len = NULL_TREE;
7538 : : }
7539 : 556 : else if (code->ext.alloc.ts.type == BT_CHARACTER
7540 : 221 : && code->ext.alloc.ts.u.cl->length)
7541 : : {
7542 : : /* Cover the cases where a string length is explicitly
7543 : : specified by a type spec for deferred length character
7544 : : arrays or unlimited polymorphic objects without a
7545 : : source= or mold= expression. */
7546 : 221 : if (expr3_esize == NULL_TREE || code->ext.alloc.ts.kind != 1)
7547 : : {
7548 : 75 : gfc_init_se (&se_sz, NULL);
7549 : 75 : gfc_conv_expr (&se_sz, code->ext.alloc.ts.u.cl->length);
7550 : 75 : gfc_add_block_to_block (&block, &se_sz.pre);
7551 : 75 : gfc_add_modify (&block, al_len,
7552 : 75 : fold_convert (TREE_TYPE (al_len),
7553 : : se_sz.expr));
7554 : : }
7555 : : else
7556 : 146 : gfc_add_modify (&block, al_len,
7557 : 146 : fold_convert (TREE_TYPE (al_len),
7558 : : expr3_esize));
7559 : : }
7560 : : else
7561 : : /* No length information needed, because type to allocate
7562 : : has no length. Set _len to 0. */
7563 : 335 : gfc_add_modify (&block, al_len,
7564 : 335 : fold_convert (TREE_TYPE (al_len),
7565 : : integer_zero_node));
7566 : : }
7567 : :
7568 : 16588 : init_expr = NULL;
7569 : 16588 : if (code->expr3 && !code->expr3->mold && e3_is != E3_MOLD)
7570 : : {
7571 : : /* Initialization via SOURCE block (or static default initializer).
7572 : : Switch off automatic reallocation since we have just done the
7573 : : ALLOCATE. */
7574 : 3548 : int realloc_lhs = flag_realloc_lhs;
7575 : 3548 : gfc_expr *init_expr = gfc_expr_to_initialize (expr);
7576 : 3548 : gfc_expr *rhs = e3rhs ? e3rhs : gfc_copy_expr (code->expr3);
7577 : 3548 : flag_realloc_lhs = 0;
7578 : :
7579 : : /* The handling of code->expr3 above produces a derived type of
7580 : : type "STAR", whose size defaults to size(void*). In order to
7581 : : have the right type information for the assignment, we must
7582 : : reconstruct an unlimited polymorphic rhs. */
7583 : 3548 : if (UNLIMITED_POLY (code->expr3)
7584 : 151 : && e3rhs && e3rhs->ts.type == BT_DERIVED
7585 : 6 : && !strcmp (e3rhs->ts.u.derived->name, "STAR"))
7586 : : {
7587 : 6 : gfc_ref *ref;
7588 : 6 : gcc_assert (TREE_CODE (expr3_vptr) == COMPONENT_REF);
7589 : 6 : tmp = gfc_create_var (gfc_typenode_for_spec (&code->expr3->ts),
7590 : : "e3");
7591 : 6 : gfc_add_modify (&block, tmp,
7592 : : gfc_get_class_from_expr (expr3_vptr));
7593 : 6 : rhs->symtree->n.sym->backend_decl = tmp;
7594 : 6 : rhs->ts = code->expr3->ts;
7595 : 6 : rhs->symtree->n.sym->ts = rhs->ts;
7596 : 6 : for (ref = init_expr->ref; ref; ref = ref->next)
7597 : : {
7598 : : /* Copy over the lhs _data component ref followed by the
7599 : : full array reference for source expressions with rank.
7600 : : Otherwise, just copy the _data component ref. */
7601 : 6 : if (code->expr3->rank
7602 : 6 : && ref && ref->next && !ref->next->next)
7603 : : {
7604 : 6 : rhs->ref = gfc_copy_ref (ref);
7605 : 6 : break;
7606 : : }
7607 : 0 : else if ((init_expr->rank && !code->expr3->rank
7608 : 0 : && ref && ref->next && !ref->next->next)
7609 : 0 : || (ref && !ref->next))
7610 : : {
7611 : 0 : rhs->ref = gfc_copy_ref (ref);
7612 : 0 : gfc_free_ref_list (rhs->ref->next);
7613 : 0 : rhs->ref->next = NULL;
7614 : 0 : break;
7615 : : }
7616 : : }
7617 : : }
7618 : :
7619 : : /* Set the symbol to be artificial so that the result is not finalized. */
7620 : 3548 : init_expr->symtree->n.sym->attr.artificial = 1;
7621 : 3548 : tmp = gfc_trans_assignment (init_expr, rhs, true, false, true,
7622 : : false);
7623 : 3548 : init_expr->symtree->n.sym->attr.artificial = 0;
7624 : :
7625 : 3548 : flag_realloc_lhs = realloc_lhs;
7626 : : /* Free the expression allocated for init_expr. */
7627 : 3548 : gfc_free_expr (init_expr);
7628 : 3548 : if (rhs != e3rhs)
7629 : 0 : gfc_free_expr (rhs);
7630 : 3548 : gfc_add_expr_to_block (&block, tmp);
7631 : 3548 : }
7632 : : /* Set KIND and LEN PDT components and allocate those that are
7633 : : parameterized. */
7634 : 13040 : else if (expr->ts.type == BT_DERIVED
7635 : 3178 : && expr->ts.u.derived->attr.pdt_type)
7636 : : {
7637 : 64 : if (code->expr3 && code->expr3->param_list)
7638 : : param_list = code->expr3->param_list;
7639 : 64 : else if (expr->param_list)
7640 : : param_list = expr->param_list;
7641 : : else
7642 : 25 : param_list = expr->symtree->n.sym->param_list;
7643 : 64 : tmp = gfc_allocate_pdt_comp (expr->ts.u.derived, se.expr,
7644 : : expr->rank, param_list);
7645 : 64 : gfc_add_expr_to_block (&block, tmp);
7646 : : }
7647 : : /* Ditto for CLASS expressions. */
7648 : 12976 : else if (expr->ts.type == BT_CLASS
7649 : 590 : && CLASS_DATA (expr)->ts.u.derived->attr.pdt_type)
7650 : : {
7651 : 0 : if (code->expr3 && code->expr3->param_list)
7652 : : param_list = code->expr3->param_list;
7653 : 0 : else if (expr->param_list)
7654 : : param_list = expr->param_list;
7655 : : else
7656 : 0 : param_list = expr->symtree->n.sym->param_list;
7657 : 0 : tmp = gfc_allocate_pdt_comp (CLASS_DATA (expr)->ts.u.derived,
7658 : : se.expr, expr->rank, param_list);
7659 : 0 : gfc_add_expr_to_block (&block, tmp);
7660 : : }
7661 : 12976 : else if (code->expr3 && code->expr3->mold
7662 : 294 : && code->expr3->ts.type == BT_CLASS)
7663 : : {
7664 : : /* Use class_init_assign to initialize expr. */
7665 : 61 : gfc_code *ini;
7666 : 61 : ini = gfc_get_code (EXEC_ALLOCATE);
7667 : 61 : ini->expr1 = gfc_find_and_cut_at_last_class_ref (expr, true);
7668 : 61 : tmp = gfc_trans_class_init_assign (ini);
7669 : 61 : gfc_free_statements (ini);
7670 : 61 : if (tmp != NULL_TREE)
7671 : 61 : gfc_add_expr_to_block (&block, tmp);
7672 : : }
7673 : 12915 : else if ((init_expr = allocate_get_initializer (code, expr)))
7674 : : {
7675 : : /* Use class_init_assign to initialize expr. */
7676 : 1846 : gfc_code *ini;
7677 : 1846 : int realloc_lhs = flag_realloc_lhs;
7678 : 1846 : ini = gfc_get_code (EXEC_INIT_ASSIGN);
7679 : 1846 : ini->expr1 = gfc_expr_to_initialize (expr);
7680 : 1846 : ini->expr2 = init_expr;
7681 : 1846 : flag_realloc_lhs = 0;
7682 : 1846 : tmp= gfc_trans_init_assign (ini);
7683 : 1846 : flag_realloc_lhs = realloc_lhs;
7684 : 1846 : gfc_free_statements (ini);
7685 : : /* Init_expr is freeed by above free_statements, just need to null
7686 : : it here. */
7687 : 1846 : init_expr = NULL;
7688 : 1846 : gfc_add_expr_to_block (&block, tmp);
7689 : : }
7690 : :
7691 : : /* Nullify all pointers in derived type coarrays. This registers a
7692 : : token for them which allows their allocation. */
7693 : 16588 : if (is_coarray)
7694 : : {
7695 : 406 : gfc_symbol *type = NULL;
7696 : 406 : symbol_attribute caf_attr;
7697 : 406 : int rank = 0;
7698 : 406 : if (code->ext.alloc.ts.type == BT_DERIVED
7699 : 9 : && code->ext.alloc.ts.u.derived->attr.pointer_comp)
7700 : : {
7701 : 0 : type = code->ext.alloc.ts.u.derived;
7702 : 0 : rank = type->attr.dimension ? type->as->rank : 0;
7703 : 0 : gfc_clear_attr (&caf_attr);
7704 : : }
7705 : 406 : else if (expr->ts.type == BT_DERIVED
7706 : 95 : && expr->ts.u.derived->attr.pointer_comp)
7707 : : {
7708 : 14 : type = expr->ts.u.derived;
7709 : 14 : rank = expr->rank;
7710 : 14 : caf_attr = gfc_caf_attr (expr, true);
7711 : : }
7712 : :
7713 : : /* Initialize the tokens of pointer components in derived type
7714 : : coarrays. */
7715 : 14 : if (type)
7716 : : {
7717 : 16 : tmp = (caf_attr.codimension && !caf_attr.dimension)
7718 : 16 : ? gfc_conv_descriptor_data_get (se.expr) : se.expr;
7719 : 14 : tmp = gfc_nullify_alloc_comp (type, tmp, rank,
7720 : : GFC_STRUCTURE_CAF_MODE_IN_COARRAY);
7721 : 14 : gfc_add_expr_to_block (&block, tmp);
7722 : : }
7723 : : }
7724 : :
7725 : 16588 : gfc_free_expr (expr);
7726 : : } // for-loop
7727 : :
7728 : 13637 : if (e3rhs)
7729 : : {
7730 : 3710 : if (newsym)
7731 : : {
7732 : 1989 : gfc_free_symbol (newsym->n.sym);
7733 : 1989 : XDELETE (newsym);
7734 : : }
7735 : 3710 : gfc_free_expr (e3rhs);
7736 : : }
7737 : : /* STAT. */
7738 : 13637 : if (code->expr1)
7739 : : {
7740 : 266 : tmp = build1_v (LABEL_EXPR, label_errmsg);
7741 : 266 : gfc_add_expr_to_block (&block, tmp);
7742 : : }
7743 : :
7744 : : /* ERRMSG - only useful if STAT is present. */
7745 : 13637 : if (code->expr1 && code->expr2)
7746 : : {
7747 : 68 : const char *msg = "Attempt to allocate an allocated object";
7748 : 68 : const char *oommsg = "Insufficient virtual memory";
7749 : 68 : tree slen, dlen, errmsg_str, oom_str, oom_loc;
7750 : 68 : stmtblock_t errmsg_block;
7751 : :
7752 : 68 : gfc_init_block (&errmsg_block);
7753 : :
7754 : 68 : errmsg_str = gfc_create_var (pchar_type_node, "ERRMSG");
7755 : 68 : gfc_add_modify (&errmsg_block, errmsg_str,
7756 : : gfc_build_addr_expr (pchar_type_node,
7757 : : gfc_build_localized_cstring_const (msg)));
7758 : :
7759 : 68 : slen = build_int_cst (gfc_charlen_type_node, strlen (msg));
7760 : 68 : dlen = gfc_get_expr_charlen (code->expr2);
7761 : 68 : slen = fold_build2_loc (input_location, MIN_EXPR,
7762 : 68 : TREE_TYPE (slen), dlen, slen);
7763 : :
7764 : 68 : gfc_trans_string_copy (&errmsg_block, dlen, errmsg,
7765 : 68 : code->expr2->ts.kind,
7766 : : slen, errmsg_str,
7767 : : gfc_default_character_kind);
7768 : 68 : dlen = gfc_finish_block (&errmsg_block);
7769 : :
7770 : 68 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7771 : 68 : stat, build_int_cst (TREE_TYPE (stat),
7772 : : LIBERROR_ALLOCATION));
7773 : :
7774 : 68 : tmp = build3_v (COND_EXPR, tmp,
7775 : : dlen, build_empty_stmt (input_location));
7776 : :
7777 : 68 : gfc_add_expr_to_block (&block, tmp);
7778 : :
7779 : 68 : oom_str = gfc_create_var (pchar_type_node, "OOMMSG");
7780 : 68 : oom_loc = gfc_build_localized_cstring_const (oommsg);
7781 : 68 : gfc_add_modify (&errmsg_block, oom_str,
7782 : : gfc_build_addr_expr (pchar_type_node, oom_loc));
7783 : :
7784 : 68 : slen = build_int_cst (gfc_charlen_type_node, strlen (oommsg));
7785 : 68 : dlen = gfc_get_expr_charlen (code->expr2);
7786 : 68 : slen = fold_build2_loc (input_location, MIN_EXPR,
7787 : 68 : TREE_TYPE (slen), dlen, slen);
7788 : :
7789 : 68 : gfc_trans_string_copy (&errmsg_block, dlen, errmsg,
7790 : 68 : code->expr2->ts.kind,
7791 : : slen, oom_str,
7792 : : gfc_default_character_kind);
7793 : 68 : dlen = gfc_finish_block (&errmsg_block);
7794 : :
7795 : 68 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7796 : 68 : stat, build_int_cst (TREE_TYPE (stat),
7797 : : LIBERROR_NO_MEMORY));
7798 : :
7799 : 68 : tmp = build3_v (COND_EXPR, tmp,
7800 : : dlen, build_empty_stmt (input_location));
7801 : :
7802 : 68 : gfc_add_expr_to_block (&block, tmp);
7803 : : }
7804 : :
7805 : : /* STAT block. */
7806 : 13637 : if (code->expr1)
7807 : : {
7808 : 266 : if (TREE_USED (label_finish))
7809 : : {
7810 : 12 : tmp = build1_v (LABEL_EXPR, label_finish);
7811 : 12 : gfc_add_expr_to_block (&block, tmp);
7812 : : }
7813 : :
7814 : 266 : gfc_init_se (&se, NULL);
7815 : 266 : gfc_conv_expr_lhs (&se, code->expr1);
7816 : 266 : tmp = convert (TREE_TYPE (se.expr), stat);
7817 : 266 : gfc_add_modify (&block, se.expr, tmp);
7818 : : }
7819 : :
7820 : 13637 : if (needs_caf_sync)
7821 : : {
7822 : : /* Add a sync all after the allocation has been executed. */
7823 : 147 : tree zero_size = build_zero_cst (size_type_node);
7824 : 147 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_all,
7825 : : 3, null_pointer_node, null_pointer_node,
7826 : : zero_size);
7827 : 147 : gfc_add_expr_to_block (&post, tmp);
7828 : : }
7829 : :
7830 : 13637 : gfc_add_block_to_block (&block, &se.post);
7831 : 13637 : gfc_add_block_to_block (&block, &post);
7832 : 13637 : if (code->expr3 && code->expr3->must_finalize)
7833 : 78 : gfc_add_block_to_block (&block, &final_block);
7834 : :
7835 : 13637 : return gfc_finish_block (&block);
7836 : : }
7837 : :
7838 : :
7839 : : /* Translate a DEALLOCATE statement. */
7840 : :
7841 : : tree
7842 : 7876 : gfc_trans_deallocate (gfc_code *code)
7843 : : {
7844 : 7876 : gfc_se se;
7845 : 7876 : gfc_alloc *al;
7846 : 7876 : tree apstat, pstat, stat, errmsg, errlen, tmp;
7847 : 7876 : tree label_finish, label_errmsg;
7848 : 7876 : stmtblock_t block;
7849 : :
7850 : 7876 : pstat = apstat = stat = errmsg = errlen = tmp = NULL_TREE;
7851 : 7876 : label_finish = label_errmsg = NULL_TREE;
7852 : :
7853 : 7876 : gfc_start_block (&block);
7854 : :
7855 : : /* Count the number of failed deallocations. If deallocate() was
7856 : : called with STAT= , then set STAT to the count. If deallocate
7857 : : was called with ERRMSG, then set ERRMG to a string. */
7858 : 7876 : if (code->expr1)
7859 : : {
7860 : 2143 : tree gfc_int4_type_node = gfc_get_int_type (4);
7861 : :
7862 : 2143 : stat = gfc_create_var (gfc_int4_type_node, "stat");
7863 : 2143 : pstat = gfc_build_addr_expr (NULL_TREE, stat);
7864 : :
7865 : : /* GOTO destinations. */
7866 : 2143 : label_errmsg = gfc_build_label_decl (NULL_TREE);
7867 : 2143 : label_finish = gfc_build_label_decl (NULL_TREE);
7868 : 2143 : TREE_USED (label_finish) = 0;
7869 : : }
7870 : :
7871 : : /* Set ERRMSG - only needed if STAT is available. */
7872 : 7876 : if (code->expr1 && code->expr2)
7873 : : {
7874 : 51 : gfc_init_se (&se, NULL);
7875 : 51 : se.want_pointer = 1;
7876 : 51 : gfc_conv_expr_lhs (&se, code->expr2);
7877 : 51 : errmsg = se.expr;
7878 : 51 : errlen = se.string_length;
7879 : : }
7880 : :
7881 : 17896 : for (al = code->ext.alloc.list; al != NULL; al = al->next)
7882 : : {
7883 : 10020 : gfc_expr *expr = gfc_copy_expr (al->expr);
7884 : 10020 : bool is_coarray = false, is_coarray_array = false;
7885 : 10020 : int caf_mode = 0;
7886 : :
7887 : 10020 : gcc_assert (expr->expr_type == EXPR_VARIABLE);
7888 : :
7889 : 10020 : if (expr->ts.type == BT_CLASS)
7890 : 1905 : gfc_add_data_component (expr);
7891 : :
7892 : 10020 : gfc_init_se (&se, NULL);
7893 : 10020 : gfc_start_block (&se.pre);
7894 : :
7895 : 10020 : se.want_pointer = 1;
7896 : 10020 : se.descriptor_only = 1;
7897 : 10020 : gfc_conv_expr (&se, expr);
7898 : :
7899 : : /* Deallocate PDT components that are parameterized. */
7900 : 10020 : tmp = NULL;
7901 : 10020 : if (expr->ts.type == BT_DERIVED
7902 : 2488 : && expr->ts.u.derived->attr.pdt_type
7903 : 59 : && expr->symtree->n.sym->param_list)
7904 : 39 : tmp = gfc_deallocate_pdt_comp (expr->ts.u.derived, se.expr, expr->rank);
7905 : 9981 : else if (expr->ts.type == BT_CLASS
7906 : 1063 : && CLASS_DATA (expr)->ts.u.derived->attr.pdt_type
7907 : 0 : && expr->symtree->n.sym->param_list)
7908 : 0 : tmp = gfc_deallocate_pdt_comp (CLASS_DATA (expr)->ts.u.derived,
7909 : : se.expr, expr->rank);
7910 : :
7911 : 39 : if (tmp)
7912 : 39 : gfc_add_expr_to_block (&block, tmp);
7913 : :
7914 : 10020 : if (flag_coarray == GFC_FCOARRAY_LIB
7915 : 10020 : || flag_coarray == GFC_FCOARRAY_SINGLE)
7916 : : {
7917 : 393 : bool comp_ref;
7918 : 393 : symbol_attribute caf_attr = gfc_caf_attr (expr, false, &comp_ref);
7919 : 393 : if (caf_attr.codimension)
7920 : : {
7921 : 280 : is_coarray = true;
7922 : 126 : is_coarray_array = caf_attr.dimension || !comp_ref
7923 : 332 : || caf_attr.coarray_comp;
7924 : :
7925 : 280 : if (flag_coarray == GFC_FCOARRAY_LIB)
7926 : : /* When the expression to deallocate is referencing a
7927 : : component, then only deallocate it, but do not
7928 : : deregister. */
7929 : 69 : caf_mode = GFC_STRUCTURE_CAF_MODE_IN_COARRAY
7930 : 175 : | (comp_ref && !caf_attr.coarray_comp
7931 : : ? GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY : 0);
7932 : : }
7933 : : }
7934 : :
7935 : 10020 : if (expr->rank || is_coarray_array)
7936 : : {
7937 : 6796 : gfc_ref *ref;
7938 : :
7939 : 5736 : if (gfc_bt_struct (expr->ts.type)
7940 : 1060 : && expr->ts.u.derived->attr.alloc_comp
7941 : 7268 : && !gfc_is_finalizable (expr->ts.u.derived, NULL))
7942 : : {
7943 : 469 : gfc_ref *last = NULL;
7944 : :
7945 : 1272 : for (ref = expr->ref; ref; ref = ref->next)
7946 : 803 : if (ref->type == REF_COMPONENT)
7947 : 286 : last = ref;
7948 : :
7949 : : /* Do not deallocate the components of a derived type
7950 : : ultimate pointer component. */
7951 : 469 : if (!(last && last->u.c.component->attr.pointer)
7952 : 210 : && !(!last && expr->symtree->n.sym->attr.pointer))
7953 : : {
7954 : 26 : if (is_coarray && expr->rank == 0
7955 : 18 : && (!last || !last->u.c.component->attr.dimension)
7956 : 463 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr)))
7957 : : {
7958 : : /* Add the ref to the data member only, when this is not
7959 : : a regular array or deallocate_alloc_comp will try to
7960 : : add another one. */
7961 : 18 : tmp = gfc_conv_descriptor_data_get (se.expr);
7962 : : }
7963 : : else
7964 : 427 : tmp = se.expr;
7965 : 445 : tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, tmp,
7966 : : expr->rank, caf_mode);
7967 : 445 : gfc_add_expr_to_block (&se.pre, tmp);
7968 : : }
7969 : : }
7970 : :
7971 : 6796 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr)))
7972 : : {
7973 : 6757 : gfc_coarray_deregtype caf_dtype;
7974 : :
7975 : 6757 : if (is_coarray)
7976 : 411 : caf_dtype = gfc_caf_is_dealloc_only (caf_mode)
7977 : 240 : ? GFC_CAF_COARRAY_DEALLOCATE_ONLY
7978 : : : GFC_CAF_COARRAY_DEREGISTER;
7979 : : else
7980 : : caf_dtype = GFC_CAF_COARRAY_NOCOARRAY;
7981 : 6757 : tmp = gfc_deallocate_with_status (se.expr, pstat, errmsg, errlen,
7982 : : label_finish, false, expr,
7983 : : caf_dtype);
7984 : 6757 : gfc_add_expr_to_block (&se.pre, tmp);
7985 : : }
7986 : 39 : else if (TREE_CODE (se.expr) == COMPONENT_REF
7987 : 39 : && TREE_CODE (TREE_TYPE (se.expr)) == ARRAY_TYPE
7988 : 78 : && TREE_CODE (TREE_TYPE (TREE_TYPE (se.expr)))
7989 : : == RECORD_TYPE)
7990 : : {
7991 : : /* class.cc(finalize_component) generates these, when a
7992 : : finalizable entity has a non-allocatable derived type array
7993 : : component, which has allocatable components. Obtain the
7994 : : derived type of the array and deallocate the allocatable
7995 : : components. */
7996 : 45 : for (ref = expr->ref; ref; ref = ref->next)
7997 : : {
7998 : 45 : if (ref->u.c.component->attr.dimension
7999 : 39 : && ref->u.c.component->ts.type == BT_DERIVED)
8000 : : break;
8001 : : }
8002 : :
8003 : 39 : if (ref && ref->u.c.component->ts.u.derived->attr.alloc_comp
8004 : 78 : && !gfc_is_finalizable (ref->u.c.component->ts.u.derived,
8005 : : NULL))
8006 : : {
8007 : 39 : tmp = gfc_deallocate_alloc_comp
8008 : 39 : (ref->u.c.component->ts.u.derived,
8009 : : se.expr, expr->rank);
8010 : 39 : gfc_add_expr_to_block (&se.pre, tmp);
8011 : : }
8012 : : }
8013 : :
8014 : 6796 : if (al->expr->ts.type == BT_CLASS)
8015 : : {
8016 : 1072 : gfc_reset_vptr (&se.pre, al->expr);
8017 : 1072 : if (UNLIMITED_POLY (al->expr)
8018 : 731 : || (al->expr->ts.type == BT_DERIVED
8019 : 0 : && al->expr->ts.u.derived->attr.unlimited_polymorphic))
8020 : : /* Clear _len, too. */
8021 : 341 : gfc_reset_len (&se.pre, al->expr);
8022 : : }
8023 : : }
8024 : : else
8025 : : {
8026 : 6448 : tmp = gfc_deallocate_scalar_with_status (se.expr, pstat, label_finish,
8027 : : false, al->expr,
8028 : 3224 : al->expr->ts, NULL_TREE,
8029 : : is_coarray);
8030 : 3224 : gfc_add_expr_to_block (&se.pre, tmp);
8031 : :
8032 : : /* Set to zero after deallocation. */
8033 : 3224 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
8034 : : se.expr,
8035 : 3224 : build_int_cst (TREE_TYPE (se.expr), 0));
8036 : 3224 : gfc_add_expr_to_block (&se.pre, tmp);
8037 : :
8038 : 3224 : if (al->expr->ts.type == BT_CLASS)
8039 : : {
8040 : 833 : gfc_reset_vptr (&se.pre, al->expr);
8041 : 833 : if (UNLIMITED_POLY (al->expr)
8042 : 613 : || (al->expr->ts.type == BT_DERIVED
8043 : 0 : && al->expr->ts.u.derived->attr.unlimited_polymorphic))
8044 : : /* Clear _len, too. */
8045 : 220 : gfc_reset_len (&se.pre, al->expr);
8046 : : }
8047 : : }
8048 : :
8049 : 10020 : if (code->expr1)
8050 : : {
8051 : 2182 : tree cond;
8052 : :
8053 : 2182 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, stat,
8054 : 2182 : build_int_cst (TREE_TYPE (stat), 0));
8055 : 2182 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
8056 : : gfc_unlikely (cond, PRED_FORTRAN_FAIL_ALLOC),
8057 : : build1_v (GOTO_EXPR, label_errmsg),
8058 : : build_empty_stmt (input_location));
8059 : 2182 : gfc_add_expr_to_block (&se.pre, tmp);
8060 : : }
8061 : :
8062 : 10020 : tmp = gfc_finish_block (&se.pre);
8063 : 10020 : gfc_add_expr_to_block (&block, tmp);
8064 : 10020 : gfc_free_expr (expr);
8065 : : }
8066 : :
8067 : 7876 : if (code->expr1)
8068 : : {
8069 : 2143 : tmp = build1_v (LABEL_EXPR, label_errmsg);
8070 : 2143 : gfc_add_expr_to_block (&block, tmp);
8071 : : }
8072 : :
8073 : : /* Set ERRMSG - only needed if STAT is available. */
8074 : 7876 : if (code->expr1 && code->expr2)
8075 : : {
8076 : 51 : const char *msg = "Attempt to deallocate an unallocated object";
8077 : 51 : stmtblock_t errmsg_block;
8078 : 51 : tree errmsg_str, slen, dlen, cond;
8079 : :
8080 : 51 : gfc_init_block (&errmsg_block);
8081 : :
8082 : 51 : errmsg_str = gfc_create_var (pchar_type_node, "ERRMSG");
8083 : 51 : gfc_add_modify (&errmsg_block, errmsg_str,
8084 : : gfc_build_addr_expr (pchar_type_node,
8085 : : gfc_build_localized_cstring_const (msg)));
8086 : 51 : slen = build_int_cst (gfc_charlen_type_node, strlen (msg));
8087 : 51 : dlen = gfc_get_expr_charlen (code->expr2);
8088 : :
8089 : 51 : gfc_trans_string_copy (&errmsg_block, dlen, errmsg, code->expr2->ts.kind,
8090 : : slen, errmsg_str, gfc_default_character_kind);
8091 : 51 : tmp = gfc_finish_block (&errmsg_block);
8092 : :
8093 : 51 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, stat,
8094 : 51 : build_int_cst (TREE_TYPE (stat), 0));
8095 : 51 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
8096 : : gfc_unlikely (cond, PRED_FORTRAN_FAIL_ALLOC), tmp,
8097 : : build_empty_stmt (input_location));
8098 : :
8099 : 51 : gfc_add_expr_to_block (&block, tmp);
8100 : : }
8101 : :
8102 : 7876 : if (code->expr1 && TREE_USED (label_finish))
8103 : : {
8104 : 14 : tmp = build1_v (LABEL_EXPR, label_finish);
8105 : 14 : gfc_add_expr_to_block (&block, tmp);
8106 : : }
8107 : :
8108 : : /* Set STAT. */
8109 : 7876 : if (code->expr1)
8110 : : {
8111 : 2143 : gfc_init_se (&se, NULL);
8112 : 2143 : gfc_conv_expr_lhs (&se, code->expr1);
8113 : 2143 : tmp = convert (TREE_TYPE (se.expr), stat);
8114 : 2143 : gfc_add_modify (&block, se.expr, tmp);
8115 : : }
8116 : :
8117 : 7876 : return gfc_finish_block (&block);
8118 : : }
8119 : :
8120 : : #include "gt-fortran-trans-stmt.h"
|