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