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