Branch data Line data Source code
1 : : /* Code translation -- generate GCC trees from gfc_code.
2 : : Copyright (C) 2002-2025 Free Software Foundation, Inc.
3 : : Contributed by Paul Brook
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it under
8 : : the terms of the GNU General Public License as published by the Free
9 : : Software Foundation; either version 3, or (at your option) any later
10 : : version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : : for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : #include "config.h"
22 : : #include "system.h"
23 : : #include "coretypes.h"
24 : : #include "options.h"
25 : : #include "tree.h"
26 : : #include "gfortran.h"
27 : : #include "gimple-expr.h" /* For create_tmp_var_raw. */
28 : : #include "trans.h"
29 : : #include "stringpool.h"
30 : : #include "fold-const.h"
31 : : #include "tree-iterator.h"
32 : : #include "trans-stmt.h"
33 : : #include "trans-array.h"
34 : : #include "trans-types.h"
35 : : #include "trans-const.h"
36 : :
37 : : /* Naming convention for backend interface code:
38 : :
39 : : gfc_trans_* translate gfc_code into STMT trees.
40 : :
41 : : gfc_conv_* expression conversion
42 : :
43 : : gfc_get_* get a backend tree representation of a decl or type */
44 : :
45 : : const char gfc_msg_fault[] = N_("Array reference out of bounds");
46 : :
47 : :
48 : : /* Advance along TREE_CHAIN n times. */
49 : :
50 : : tree
51 : 5598068 : gfc_advance_chain (tree t, int n)
52 : : {
53 : 16187013 : for (; n > 0; n--)
54 : : {
55 : 10588945 : gcc_assert (t != NULL_TREE);
56 : 10588945 : t = DECL_CHAIN (t);
57 : : }
58 : 5598068 : return t;
59 : : }
60 : :
61 : : void
62 : 94658 : gfc_locus_from_location (locus *where, location_t loc)
63 : : {
64 : 94658 : where->nextc = (gfc_char_t *) -1;
65 : 94658 : where->u.location = loc;
66 : 94658 : }
67 : :
68 : :
69 : : static int num_var;
70 : :
71 : : #define MAX_PREFIX_LEN 20
72 : :
73 : : static tree
74 : 0 : create_var_debug_raw (tree type, const char *prefix)
75 : : {
76 : : /* Space for prefix + "_" + 10-digit-number + \0. */
77 : 0 : char name_buf[MAX_PREFIX_LEN + 1 + 10 + 1];
78 : 0 : tree t;
79 : 0 : int i;
80 : :
81 : 0 : if (prefix == NULL)
82 : : prefix = "gfc";
83 : : else
84 : 0 : gcc_assert (strlen (prefix) <= MAX_PREFIX_LEN);
85 : :
86 : 0 : for (i = 0; prefix[i] != 0; i++)
87 : 0 : name_buf[i] = gfc_wide_toupper (prefix[i]);
88 : :
89 : 0 : snprintf (name_buf + i, sizeof (name_buf) - i, "_%d", num_var++);
90 : :
91 : 0 : t = build_decl (input_location, VAR_DECL, get_identifier (name_buf), type);
92 : :
93 : : /* Not setting this causes some regressions. */
94 : 0 : DECL_ARTIFICIAL (t) = 1;
95 : :
96 : : /* We want debug info for it. */
97 : 0 : DECL_IGNORED_P (t) = 0;
98 : : /* It should not be nameless. */
99 : 0 : DECL_NAMELESS (t) = 0;
100 : :
101 : : /* Make the variable writable. */
102 : 0 : TREE_READONLY (t) = 0;
103 : :
104 : 0 : DECL_EXTERNAL (t) = 0;
105 : 0 : TREE_STATIC (t) = 0;
106 : 0 : TREE_USED (t) = 1;
107 : :
108 : 0 : return t;
109 : : }
110 : :
111 : : /* Creates a variable declaration with a given TYPE. */
112 : :
113 : : tree
114 : 1558645 : gfc_create_var_np (tree type, const char *prefix)
115 : : {
116 : 1558645 : tree t;
117 : :
118 : 1558645 : if (flag_debug_aux_vars)
119 : 0 : return create_var_debug_raw (type, prefix);
120 : :
121 : 1558645 : t = create_tmp_var_raw (type, prefix);
122 : :
123 : : /* No warnings for anonymous variables. */
124 : 1558645 : if (prefix == NULL)
125 : 941106 : suppress_warning (t);
126 : :
127 : : return t;
128 : : }
129 : :
130 : :
131 : : /* Like above, but also adds it to the current scope. */
132 : :
133 : : tree
134 : 1439572 : gfc_create_var (tree type, const char *prefix)
135 : : {
136 : 1439572 : tree tmp;
137 : :
138 : 1439572 : tmp = gfc_create_var_np (type, prefix);
139 : :
140 : 1439572 : pushdecl (tmp);
141 : :
142 : 1439572 : return tmp;
143 : : }
144 : :
145 : :
146 : : /* If the expression is not constant, evaluate it now. We assign the
147 : : result of the expression to an artificially created variable VAR, and
148 : : return a pointer to the VAR_DECL node for this variable. */
149 : :
150 : : tree
151 : 2093098 : gfc_evaluate_now_loc (location_t loc, tree expr, stmtblock_t * pblock)
152 : : {
153 : 2093098 : tree var;
154 : :
155 : 2093098 : if (CONSTANT_CLASS_P (expr))
156 : : return expr;
157 : :
158 : 808117 : var = gfc_create_var (TREE_TYPE (expr), NULL);
159 : 808117 : gfc_add_modify_loc (loc, pblock, var, expr);
160 : :
161 : 808117 : return var;
162 : : }
163 : :
164 : :
165 : : tree
166 : 2057924 : gfc_evaluate_now (tree expr, stmtblock_t * pblock)
167 : : {
168 : 2057924 : return gfc_evaluate_now_loc (input_location, expr, pblock);
169 : : }
170 : :
171 : :
172 : : /* Returns a fresh pointer variable pointing to the same data as EXPR, adding
173 : : in BLOCK the initialization code that makes it point to EXPR. */
174 : :
175 : : tree
176 : 666 : gfc_evaluate_data_ref_now (tree expr, stmtblock_t *block)
177 : : {
178 : 666 : tree t = expr;
179 : :
180 : 666 : STRIP_NOPS (t);
181 : :
182 : : /* If EXPR can be used as lhs of an assignment, we have to take the address
183 : : of EXPR. Otherwise, reassigning the pointer would retarget it to some
184 : : other data without EXPR being retargetted as well. */
185 : 666 : bool lvalue_p = DECL_P (t) || REFERENCE_CLASS_P (t) || INDIRECT_REF_P (t);
186 : :
187 : 143 : tree value;
188 : 143 : if (lvalue_p)
189 : : {
190 : 523 : value = gfc_build_addr_expr (NULL_TREE, expr);
191 : 523 : value = gfc_evaluate_now (value, block);
192 : 523 : return build_fold_indirect_ref_loc (input_location, value);
193 : : }
194 : : else
195 : 143 : return gfc_evaluate_now (expr, block);
196 : : }
197 : :
198 : :
199 : : /* Like gfc_evaluate_now, but add the created variable to the
200 : : function scope. */
201 : :
202 : : tree
203 : 114 : gfc_evaluate_now_function_scope (tree expr, stmtblock_t * pblock)
204 : : {
205 : 114 : tree var;
206 : 114 : var = gfc_create_var_np (TREE_TYPE (expr), NULL);
207 : 114 : gfc_add_decl_to_function (var);
208 : 114 : gfc_add_modify (pblock, var, expr);
209 : :
210 : 114 : return var;
211 : : }
212 : :
213 : : /* Build a MODIFY_EXPR node and add it to a given statement block PBLOCK.
214 : : A MODIFY_EXPR is an assignment:
215 : : LHS <- RHS. */
216 : :
217 : : void
218 : 3588507 : gfc_add_modify_loc (location_t loc, stmtblock_t * pblock, tree lhs, tree rhs)
219 : : {
220 : 3588507 : tree tmp;
221 : :
222 : 3588507 : tree t1, t2;
223 : 3588507 : t1 = TREE_TYPE (rhs);
224 : 3588507 : t2 = TREE_TYPE (lhs);
225 : : /* Make sure that the types of the rhs and the lhs are compatible
226 : : for scalar assignments. We should probably have something
227 : : similar for aggregates, but right now removing that check just
228 : : breaks everything. */
229 : 3588507 : gcc_checking_assert (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)
230 : : || AGGREGATE_TYPE_P (TREE_TYPE (lhs)));
231 : :
232 : 3588507 : tmp = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, lhs,
233 : : rhs);
234 : 3588507 : gfc_add_expr_to_block (pblock, tmp);
235 : 3588507 : }
236 : :
237 : :
238 : : void
239 : 2724367 : gfc_add_modify (stmtblock_t * pblock, tree lhs, tree rhs)
240 : : {
241 : 2724367 : gfc_add_modify_loc (input_location, pblock, lhs, rhs);
242 : 2724367 : }
243 : :
244 : : tree
245 : 831 : gfc_trans_force_lval (stmtblock_t *pblock, tree e)
246 : : {
247 : 831 : if (VAR_P (e))
248 : : return e;
249 : :
250 : 736 : tree v = gfc_create_var (TREE_TYPE (e), NULL);
251 : 736 : gfc_add_modify (pblock, v, e);
252 : 736 : return v;
253 : : }
254 : :
255 : : /* Create a new scope/binding level and initialize a block. Care must be
256 : : taken when translating expressions as any temporaries will be placed in
257 : : the innermost scope. */
258 : :
259 : : void
260 : 2186887 : gfc_start_block (stmtblock_t * block)
261 : : {
262 : : /* Start a new binding level. */
263 : 2186887 : pushlevel ();
264 : 2186887 : block->has_scope = 1;
265 : :
266 : : /* The block is empty. */
267 : 2186887 : block->head = NULL_TREE;
268 : 2186887 : }
269 : :
270 : :
271 : : /* Initialize a block without creating a new scope. */
272 : :
273 : : void
274 : 16422269 : gfc_init_block (stmtblock_t * block)
275 : : {
276 : 16422269 : block->head = NULL_TREE;
277 : 16422269 : block->has_scope = 0;
278 : 16422269 : }
279 : :
280 : :
281 : : /* Sometimes we create a scope but it turns out that we don't actually
282 : : need it. This function merges the scope of BLOCK with its parent.
283 : : Only variable decls will be merged, you still need to add the code. */
284 : :
285 : : void
286 : 85 : gfc_merge_block_scope (stmtblock_t * block)
287 : : {
288 : 85 : tree decl;
289 : 85 : tree next;
290 : :
291 : 85 : gcc_assert (block->has_scope);
292 : 85 : block->has_scope = 0;
293 : :
294 : : /* Remember the decls in this scope. */
295 : 85 : decl = getdecls ();
296 : 85 : poplevel (0, 0);
297 : :
298 : : /* Add them to the parent scope. */
299 : 283 : while (decl != NULL_TREE)
300 : : {
301 : 113 : next = DECL_CHAIN (decl);
302 : 113 : DECL_CHAIN (decl) = NULL_TREE;
303 : :
304 : 113 : pushdecl (decl);
305 : 113 : decl = next;
306 : : }
307 : 85 : }
308 : :
309 : :
310 : : /* Finish a scope containing a block of statements. */
311 : :
312 : : tree
313 : 3833990 : gfc_finish_block (stmtblock_t * stmtblock)
314 : : {
315 : 3833990 : tree decl;
316 : 3833990 : tree expr;
317 : 3833990 : tree block;
318 : :
319 : 3833990 : expr = stmtblock->head;
320 : 3833990 : if (!expr)
321 : 479995 : expr = build_empty_stmt (input_location);
322 : :
323 : 3833990 : stmtblock->head = NULL_TREE;
324 : :
325 : 3833990 : if (stmtblock->has_scope)
326 : : {
327 : 2186795 : decl = getdecls ();
328 : :
329 : 2186795 : if (decl)
330 : : {
331 : 552499 : block = poplevel (1, 0);
332 : 552499 : expr = build3_v (BIND_EXPR, decl, expr, block);
333 : : }
334 : : else
335 : 1634296 : poplevel (0, 0);
336 : : }
337 : :
338 : 3833990 : return expr;
339 : : }
340 : :
341 : :
342 : : /* Build an ADDR_EXPR and cast the result to TYPE. If TYPE is NULL, the
343 : : natural type is used. */
344 : :
345 : : tree
346 : 1514260 : gfc_build_addr_expr (tree type, tree t)
347 : : {
348 : 1514260 : tree base_type = TREE_TYPE (t);
349 : 1514260 : tree natural_type;
350 : :
351 : 640337 : if (type && POINTER_TYPE_P (type)
352 : 640337 : && TREE_CODE (base_type) == ARRAY_TYPE
353 : 2088212 : && TYPE_MAIN_VARIANT (TREE_TYPE (type))
354 : 573952 : == TYPE_MAIN_VARIANT (TREE_TYPE (base_type)))
355 : : {
356 : 397753 : tree min_val = size_zero_node;
357 : 397753 : tree type_domain = TYPE_DOMAIN (base_type);
358 : 397753 : if (type_domain && TYPE_MIN_VALUE (type_domain))
359 : 397753 : min_val = TYPE_MIN_VALUE (type_domain);
360 : 397753 : t = fold (build4_loc (input_location, ARRAY_REF, TREE_TYPE (type),
361 : : t, min_val, NULL_TREE, NULL_TREE));
362 : 397753 : natural_type = type;
363 : : }
364 : : else
365 : 1116507 : natural_type = build_pointer_type (base_type);
366 : :
367 : 1514260 : if (INDIRECT_REF_P (t))
368 : : {
369 : 147860 : if (!type)
370 : 72632 : type = natural_type;
371 : 147860 : t = TREE_OPERAND (t, 0);
372 : 147860 : natural_type = TREE_TYPE (t);
373 : : }
374 : : else
375 : : {
376 : 1366400 : tree base = get_base_address (t);
377 : 1366400 : if (base && DECL_P (base))
378 : 953492 : TREE_ADDRESSABLE (base) = 1;
379 : 1366400 : t = fold_build1_loc (input_location, ADDR_EXPR, natural_type, t);
380 : : }
381 : :
382 : 1514260 : if (type && natural_type != type)
383 : 186218 : t = convert (type, t);
384 : :
385 : 1514260 : return t;
386 : : }
387 : :
388 : :
389 : : static tree
390 : 19661 : get_array_span (tree type, tree decl)
391 : : {
392 : 19661 : tree span;
393 : :
394 : : /* Component references are guaranteed to have a reliable value for
395 : : 'span'. Likewise indirect references since they emerge from the
396 : : conversion of a CFI descriptor or the hidden dummy descriptor. */
397 : 19661 : if (TREE_CODE (decl) == COMPONENT_REF
398 : 19661 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
399 : 3083 : return gfc_conv_descriptor_span_get (decl);
400 : 16578 : else if (INDIRECT_REF_P (decl)
401 : 16578 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
402 : 2321 : return gfc_conv_descriptor_span_get (decl);
403 : :
404 : : /* Return the span for deferred character length array references. */
405 : 14257 : if (type
406 : 14257 : && (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == INTEGER_TYPE)
407 : 23726 : && TYPE_STRING_FLAG (type))
408 : : {
409 : 6896 : if (TREE_CODE (decl) == PARM_DECL)
410 : 393 : decl = build_fold_indirect_ref_loc (input_location, decl);
411 : 6896 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
412 : 5186 : span = gfc_conv_descriptor_span_get (decl);
413 : : else
414 : 1710 : span = gfc_get_character_len_in_bytes (type);
415 : 13792 : span = (span && !integer_zerop (span))
416 : 13792 : ? (fold_convert (gfc_array_index_type, span)) : (NULL_TREE);
417 : : }
418 : : /* Likewise for class array or pointer array references. */
419 : 7361 : else if (TREE_CODE (decl) == FIELD_DECL
420 : : || VAR_OR_FUNCTION_DECL_P (decl)
421 : : || TREE_CODE (decl) == PARM_DECL)
422 : : {
423 : 7361 : if (GFC_DECL_CLASS (decl))
424 : : {
425 : : /* When a temporary is in place for the class array, then the
426 : : original class' declaration is stored in the saved
427 : : descriptor. */
428 : 0 : if (DECL_LANG_SPECIFIC (decl) && GFC_DECL_SAVED_DESCRIPTOR (decl))
429 : 0 : decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
430 : : else
431 : : {
432 : : /* Allow for dummy arguments and other good things. */
433 : 0 : if (POINTER_TYPE_P (TREE_TYPE (decl)))
434 : 0 : decl = build_fold_indirect_ref_loc (input_location, decl);
435 : :
436 : : /* Check if '_data' is an array descriptor. If it is not,
437 : : the array must be one of the components of the class
438 : : object, so return a null span. */
439 : 0 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (
440 : : gfc_class_data_get (decl))))
441 : : return NULL_TREE;
442 : : }
443 : 0 : span = gfc_class_vtab_size_get (decl);
444 : : /* For unlimited polymorphic entities then _len component needs
445 : : to be multiplied with the size. */
446 : 0 : span = gfc_resize_class_size_with_len (NULL, decl, span);
447 : : }
448 : 7361 : else if (GFC_DECL_PTR_ARRAY_P (decl))
449 : : {
450 : 7186 : if (TREE_CODE (decl) == PARM_DECL)
451 : 1965 : decl = build_fold_indirect_ref_loc (input_location, decl);
452 : 7186 : span = gfc_conv_descriptor_span_get (decl);
453 : : }
454 : : else
455 : : span = NULL_TREE;
456 : : }
457 : : else
458 : : span = NULL_TREE;
459 : :
460 : : return span;
461 : : }
462 : :
463 : :
464 : : tree
465 : 26429 : gfc_build_spanned_array_ref (tree base, tree offset, tree span)
466 : : {
467 : 26429 : tree type;
468 : 26429 : tree tmp;
469 : 26429 : type = TREE_TYPE (TREE_TYPE (base));
470 : 26429 : offset = fold_build2_loc (input_location, MULT_EXPR,
471 : : gfc_array_index_type,
472 : : offset, span);
473 : 26429 : tmp = gfc_build_addr_expr (pvoid_type_node, base);
474 : 26429 : tmp = fold_build_pointer_plus_loc (input_location, tmp, offset);
475 : 26429 : tmp = fold_convert (build_pointer_type (type), tmp);
476 : 21905 : if ((TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != ARRAY_TYPE)
477 : 35474 : || !TYPE_STRING_FLAG (type))
478 : 17321 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
479 : 26429 : return tmp;
480 : : }
481 : :
482 : :
483 : : /* Build an ARRAY_REF with its natural type.
484 : : NON_NEGATIVE_OFFSET indicates if it’s true that OFFSET can’t be negative,
485 : : and thus that an ARRAY_REF can safely be generated. If it’s false, we
486 : : have to play it safe and use pointer arithmetic. */
487 : :
488 : : tree
489 : 1414274 : gfc_build_array_ref (tree base, tree offset, tree decl,
490 : : bool non_negative_offset, tree vptr)
491 : : {
492 : 1414274 : tree type = TREE_TYPE (base);
493 : 1414274 : tree span = NULL_TREE;
494 : :
495 : 1414274 : if (GFC_ARRAY_TYPE_P (type) && GFC_TYPE_ARRAY_RANK (type) == 0)
496 : : {
497 : 96 : gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
498 : :
499 : 96 : return fold_convert (TYPE_MAIN_VARIANT (type), base);
500 : : }
501 : :
502 : : /* Scalar coarray, there is nothing to do. */
503 : 1414178 : if (TREE_CODE (type) != ARRAY_TYPE)
504 : : {
505 : 20 : gcc_assert (decl == NULL_TREE);
506 : 20 : gcc_assert (integer_zerop (offset));
507 : : return base;
508 : : }
509 : :
510 : 1414158 : type = TREE_TYPE (type);
511 : :
512 : 1414158 : if (DECL_P (base))
513 : 201980 : TREE_ADDRESSABLE (base) = 1;
514 : :
515 : : /* Strip NON_LVALUE_EXPR nodes. */
516 : 1448705 : STRIP_TYPE_NOPS (offset);
517 : :
518 : : /* If decl or vptr are non-null, pointer arithmetic for the array reference
519 : : is likely. Generate the 'span' for the array reference. */
520 : 1414158 : if (vptr)
521 : : {
522 : 3401 : span = gfc_vptr_size_get (vptr);
523 : :
524 : : /* Check if this is an unlimited polymorphic object carrying a character
525 : : payload. In this case, the 'len' field is non-zero. */
526 : 3401 : if (decl && GFC_CLASS_TYPE_P (TREE_TYPE (decl)))
527 : 98 : span = gfc_resize_class_size_with_len (NULL, decl, span);
528 : : }
529 : 1410757 : else if (decl)
530 : 19661 : span = get_array_span (type, decl);
531 : :
532 : : /* If a non-null span has been generated reference the element with
533 : : pointer arithmetic. */
534 : 23062 : if (span != NULL_TREE)
535 : 22887 : return gfc_build_spanned_array_ref (base, offset, span);
536 : : /* Else use a straightforward array reference if possible. */
537 : 1391271 : else if (non_negative_offset)
538 : 1348838 : return build4_loc (input_location, ARRAY_REF, type, base, offset,
539 : 1348838 : NULL_TREE, NULL_TREE);
540 : : /* Otherwise use pointer arithmetic. */
541 : : else
542 : : {
543 : 42433 : gcc_assert (TREE_CODE (TREE_TYPE (base)) == ARRAY_TYPE);
544 : 42433 : tree min = NULL_TREE;
545 : 42433 : if (TYPE_DOMAIN (TREE_TYPE (base))
546 : 42433 : && !integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (base)))))
547 : 320 : min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (base)));
548 : :
549 : 320 : tree zero_based_index
550 : 320 : = min ? fold_build2_loc (input_location, MINUS_EXPR,
551 : : gfc_array_index_type,
552 : : fold_convert (gfc_array_index_type, offset),
553 : : fold_convert (gfc_array_index_type, min))
554 : 42113 : : fold_convert (gfc_array_index_type, offset);
555 : :
556 : 42433 : tree elt_size = fold_convert (gfc_array_index_type,
557 : : TYPE_SIZE_UNIT (type));
558 : :
559 : 42433 : tree offset_bytes = fold_build2_loc (input_location, MULT_EXPR,
560 : : gfc_array_index_type,
561 : : zero_based_index, elt_size);
562 : :
563 : 42433 : tree base_addr = gfc_build_addr_expr (pvoid_type_node, base);
564 : :
565 : 42433 : tree ptr = fold_build_pointer_plus_loc (input_location, base_addr,
566 : : offset_bytes);
567 : 42433 : return build1_loc (input_location, INDIRECT_REF, type,
568 : 42433 : fold_convert (build_pointer_type (type), ptr));
569 : : }
570 : : }
571 : :
572 : :
573 : : /* Generate a call to print a runtime error possibly including multiple
574 : : arguments and a locus. */
575 : :
576 : : static tree
577 : 79036 : trans_runtime_error_vararg (tree errorfunc, locus* where, const char* msgid,
578 : : va_list ap)
579 : : {
580 : 79036 : stmtblock_t block;
581 : 79036 : tree tmp;
582 : 79036 : tree arg, arg2;
583 : 79036 : tree *argarray;
584 : 79036 : tree fntype;
585 : 79036 : char *message;
586 : 79036 : const char *p;
587 : 79036 : int nargs, i;
588 : 79036 : location_t loc;
589 : :
590 : : /* Compute the number of extra arguments from the format string. */
591 : 4201349 : for (p = msgid, nargs = 0; *p; p++)
592 : 4122313 : if (*p == '%')
593 : : {
594 : 114937 : p++;
595 : 114937 : if (*p != '%')
596 : 114208 : nargs++;
597 : : }
598 : :
599 : : /* The code to generate the error. */
600 : 79036 : gfc_start_block (&block);
601 : :
602 : 79036 : if (where)
603 : : {
604 : 60505 : location_t loc = gfc_get_location (where);
605 : 60505 : message = xasprintf ("At line %d of file %s", LOCATION_LINE (loc),
606 : 121010 : LOCATION_FILE (loc));
607 : : }
608 : : else
609 : 18531 : message = xasprintf ("In file '%s', around line %d",
610 : 37062 : gfc_source_file, LOCATION_LINE (input_location));
611 : :
612 : 79036 : arg = gfc_build_addr_expr (pchar_type_node,
613 : : gfc_build_localized_cstring_const (message));
614 : 79036 : free (message);
615 : :
616 : 79036 : message = xasprintf ("%s", _(msgid));
617 : 79036 : arg2 = gfc_build_addr_expr (pchar_type_node,
618 : : gfc_build_localized_cstring_const (message));
619 : 79036 : free (message);
620 : :
621 : : /* Build the argument array. */
622 : 79036 : argarray = XALLOCAVEC (tree, nargs + 2);
623 : 79036 : argarray[0] = arg;
624 : 79036 : argarray[1] = arg2;
625 : 193244 : for (i = 0; i < nargs; i++)
626 : 114208 : argarray[2 + i] = va_arg (ap, tree);
627 : :
628 : : /* Build the function call to runtime_(warning,error)_at; because of the
629 : : variable number of arguments, we can't use build_call_expr_loc dinput_location,
630 : : irectly. */
631 : 79036 : fntype = TREE_TYPE (errorfunc);
632 : :
633 : 79036 : loc = where ? gfc_get_location (where) : input_location;
634 : 79036 : tmp = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
635 : : fold_build1_loc (loc, ADDR_EXPR,
636 : : build_pointer_type (fntype),
637 : : errorfunc),
638 : : nargs + 2, argarray);
639 : 79036 : gfc_add_expr_to_block (&block, tmp);
640 : :
641 : 79036 : return gfc_finish_block (&block);
642 : : }
643 : :
644 : :
645 : : tree
646 : 23101 : gfc_trans_runtime_error (bool error, locus* where, const char* msgid, ...)
647 : : {
648 : 23101 : va_list ap;
649 : 23101 : tree result;
650 : :
651 : 23101 : va_start (ap, msgid);
652 : 23101 : result = trans_runtime_error_vararg (error
653 : : ? gfor_fndecl_runtime_error_at
654 : : : gfor_fndecl_runtime_warning_at,
655 : : where, msgid, ap);
656 : 23101 : va_end (ap);
657 : 23101 : return result;
658 : : }
659 : :
660 : :
661 : : /* Generate a runtime error if COND is true. */
662 : :
663 : : void
664 : 164630 : gfc_trans_runtime_check (bool error, bool once, tree cond, stmtblock_t * pblock,
665 : : locus * where, const char * msgid, ...)
666 : : {
667 : 164630 : va_list ap;
668 : 164630 : stmtblock_t block;
669 : 164630 : tree body;
670 : 164630 : tree tmp;
671 : 164630 : tree tmpvar = NULL;
672 : :
673 : 164630 : if (integer_zerop (cond))
674 : 127166 : return;
675 : :
676 : 37464 : if (once)
677 : : {
678 : 954 : tmpvar = gfc_create_var (boolean_type_node, "print_warning");
679 : 954 : TREE_STATIC (tmpvar) = 1;
680 : 954 : DECL_INITIAL (tmpvar) = boolean_true_node;
681 : 954 : gfc_add_expr_to_block (pblock, tmpvar);
682 : : }
683 : :
684 : 37464 : gfc_start_block (&block);
685 : :
686 : : /* For error, runtime_error_at already implies PRED_NORETURN. */
687 : 37464 : if (!error && once)
688 : 954 : gfc_add_expr_to_block (&block, build_predict_expr (PRED_FORTRAN_WARN_ONCE,
689 : : NOT_TAKEN));
690 : :
691 : : /* The code to generate the error. */
692 : 37464 : va_start (ap, msgid);
693 : 37464 : gfc_add_expr_to_block (&block,
694 : : trans_runtime_error_vararg
695 : : (error ? gfor_fndecl_runtime_error_at
696 : : : gfor_fndecl_runtime_warning_at,
697 : : where, msgid, ap));
698 : 37464 : va_end (ap);
699 : :
700 : 37464 : if (once)
701 : 954 : gfc_add_modify (&block, tmpvar, boolean_false_node);
702 : :
703 : 37464 : body = gfc_finish_block (&block);
704 : :
705 : 37464 : if (integer_onep (cond))
706 : : {
707 : 892 : gfc_add_expr_to_block (pblock, body);
708 : : }
709 : : else
710 : : {
711 : 36572 : location_t loc = where ? gfc_get_location (where) : input_location;
712 : 36572 : if (once)
713 : 86 : cond = fold_build2_loc (loc, TRUTH_AND_EXPR, boolean_type_node, tmpvar,
714 : : fold_convert (boolean_type_node, cond));
715 : :
716 : 36572 : tmp = fold_build3_loc (loc, COND_EXPR, void_type_node, cond, body,
717 : : build_empty_stmt (loc));
718 : 36572 : gfc_add_expr_to_block (pblock, tmp);
719 : : }
720 : : }
721 : :
722 : :
723 : : static tree
724 : 18471 : trans_os_error_at (locus* where, const char* msgid, ...)
725 : : {
726 : 18471 : va_list ap;
727 : 18471 : tree result;
728 : :
729 : 18471 : va_start (ap, msgid);
730 : 18471 : result = trans_runtime_error_vararg (gfor_fndecl_os_error_at,
731 : : where, msgid, ap);
732 : 18471 : va_end (ap);
733 : 18471 : return result;
734 : : }
735 : :
736 : :
737 : :
738 : : /* Call malloc to allocate size bytes of memory, with special conditions:
739 : : + if size == 0, return a malloced area of size 1,
740 : : + if malloc returns NULL, issue a runtime error. */
741 : : tree
742 : 19634 : gfc_call_malloc (stmtblock_t * block, tree type, tree size)
743 : : {
744 : 19634 : tree tmp, malloc_result, null_result, res, malloc_tree;
745 : 19634 : stmtblock_t block2;
746 : :
747 : : /* Create a variable to hold the result. */
748 : 19634 : res = gfc_create_var (prvoid_type_node, NULL);
749 : :
750 : : /* Call malloc. */
751 : 19634 : gfc_start_block (&block2);
752 : :
753 : 19634 : if (size == NULL_TREE)
754 : 1 : size = build_int_cst (size_type_node, 1);
755 : :
756 : 19634 : size = fold_convert (size_type_node, size);
757 : 19634 : size = fold_build2_loc (input_location, MAX_EXPR, size_type_node, size,
758 : : build_int_cst (size_type_node, 1));
759 : :
760 : 19634 : malloc_tree = builtin_decl_explicit (BUILT_IN_MALLOC);
761 : 19634 : gfc_add_modify (&block2, res,
762 : : fold_convert (prvoid_type_node,
763 : : build_call_expr_loc (input_location,
764 : : malloc_tree, 1, size)));
765 : :
766 : : /* Optionally check whether malloc was successful. */
767 : 19634 : if (gfc_option.rtcheck & GFC_RTCHECK_MEM)
768 : : {
769 : 105 : null_result = fold_build2_loc (input_location, EQ_EXPR,
770 : : logical_type_node, res,
771 : : build_int_cst (pvoid_type_node, 0));
772 : 105 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
773 : : null_result,
774 : : trans_os_error_at (NULL,
775 : : "Error allocating %lu bytes",
776 : : fold_convert
777 : : (long_unsigned_type_node,
778 : : size)),
779 : : build_empty_stmt (input_location));
780 : 105 : gfc_add_expr_to_block (&block2, tmp);
781 : : }
782 : :
783 : 19634 : malloc_result = gfc_finish_block (&block2);
784 : 19634 : gfc_add_expr_to_block (block, malloc_result);
785 : :
786 : 19634 : if (type != NULL)
787 : 14741 : res = fold_convert (type, res);
788 : 19634 : return res;
789 : : }
790 : :
791 : :
792 : : /* Allocate memory, using an optional status argument.
793 : :
794 : : This function follows the following pseudo-code:
795 : :
796 : : void *
797 : : allocate (size_t size, integer_type stat)
798 : : {
799 : : void *newmem;
800 : :
801 : : if (stat requested)
802 : : stat = 0;
803 : :
804 : : // if cond == NULL_NULL:
805 : : newmem = malloc (MAX (size, 1));
806 : : // otherwise:
807 : : newmem = <cond> ? <alt_alloc> : malloc (MAX (size, 1))
808 : : if (newmem == NULL)
809 : : {
810 : : if (stat)
811 : : *stat = LIBERROR_NO_MEMORY;
812 : : else
813 : : runtime_error ("Allocation would exceed memory limit");
814 : : }
815 : : return newmem;
816 : : } */
817 : : void
818 : 17452 : gfc_allocate_using_malloc (stmtblock_t * block, tree pointer,
819 : : tree size, tree status, tree cond, tree alt_alloc,
820 : : tree extra_success_expr)
821 : : {
822 : 17452 : tree tmp, error_cond;
823 : 17452 : stmtblock_t on_error;
824 : 17452 : tree status_type = status ? TREE_TYPE (status) : NULL_TREE;
825 : 17452 : bool cond_is_true = cond == boolean_true_node;
826 : :
827 : : /* If successful and stat= is given, set status to 0. */
828 : 17187 : if (status != NULL_TREE)
829 : 265 : gfc_add_expr_to_block (block,
830 : : fold_build2_loc (input_location, MODIFY_EXPR, status_type,
831 : : status, build_int_cst (status_type, 0)));
832 : :
833 : : /* The allocation itself. */
834 : 17452 : size = fold_convert (size_type_node, size);
835 : 17452 : tmp = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
836 : : size, build_int_cst (size_type_node, 1));
837 : :
838 : 17452 : if (!cond_is_true)
839 : 17391 : tmp = build_call_expr_loc (input_location,
840 : : builtin_decl_explicit (BUILT_IN_MALLOC), 1, tmp);
841 : : else
842 : : tmp = alt_alloc;
843 : :
844 : 17452 : if (!cond_is_true && cond)
845 : 0 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
846 : : alt_alloc, tmp);
847 : :
848 : 17452 : gfc_add_modify (block, pointer, fold_convert (TREE_TYPE (pointer), tmp));
849 : :
850 : : /* What to do in case of error. */
851 : 17452 : gfc_start_block (&on_error);
852 : 17452 : if (status != NULL_TREE)
853 : : {
854 : 265 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type, status,
855 : : build_int_cst (status_type, LIBERROR_NO_MEMORY));
856 : 265 : gfc_add_expr_to_block (&on_error, tmp);
857 : : }
858 : : else
859 : : {
860 : : /* Here, os_error_at already implies PRED_NORETURN. */
861 : 17187 : tree lusize = fold_convert (long_unsigned_type_node, size);
862 : 17187 : tmp = trans_os_error_at (NULL, "Error allocating %lu bytes", lusize);
863 : 17187 : gfc_add_expr_to_block (&on_error, tmp);
864 : : }
865 : :
866 : 17452 : error_cond = fold_build2_loc (input_location, EQ_EXPR,
867 : : logical_type_node, pointer,
868 : : build_int_cst (prvoid_type_node, 0));
869 : 34843 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
870 : : gfc_unlikely (error_cond, PRED_FORTRAN_FAIL_ALLOC),
871 : : gfc_finish_block (&on_error),
872 : : extra_success_expr
873 : : ? extra_success_expr
874 : 17391 : : build_empty_stmt (input_location));
875 : :
876 : 17452 : gfc_add_expr_to_block (block, tmp);
877 : 17452 : }
878 : :
879 : :
880 : : /* Allocate memory, using an optional status argument.
881 : :
882 : : This function follows the following pseudo-code:
883 : :
884 : : void *
885 : : allocate (size_t size, void** token, int *stat, char* errmsg, int errlen)
886 : : {
887 : : void *newmem;
888 : :
889 : : newmem = _caf_register (size, regtype, token, &stat, errmsg, errlen);
890 : : return newmem;
891 : : } */
892 : : void
893 : 637 : gfc_allocate_using_caf_lib (stmtblock_t * block, tree pointer, tree size,
894 : : tree token, tree status, tree errmsg, tree errlen,
895 : : gfc_coarray_regtype alloc_type)
896 : : {
897 : 637 : tree tmp, pstat;
898 : :
899 : 637 : gcc_assert (token != NULL_TREE);
900 : :
901 : : /* The allocation itself. */
902 : 637 : if (status == NULL_TREE)
903 : 624 : pstat = null_pointer_node;
904 : : else
905 : 13 : pstat = gfc_build_addr_expr (NULL_TREE, status);
906 : :
907 : 637 : if (errmsg == NULL_TREE)
908 : : {
909 : 624 : gcc_assert(errlen == NULL_TREE);
910 : 624 : errmsg = null_pointer_node;
911 : 624 : errlen = integer_zero_node;
912 : : }
913 : :
914 : 637 : size = fold_convert (size_type_node, size);
915 : 637 : tmp = build_call_expr_loc (input_location,
916 : : gfor_fndecl_caf_register, 7,
917 : : fold_build2_loc (input_location,
918 : : MAX_EXPR, size_type_node, size, size_one_node),
919 : 637 : build_int_cst (integer_type_node, alloc_type),
920 : : token, gfc_build_addr_expr (pvoid_type_node, pointer),
921 : : pstat, errmsg, errlen);
922 : :
923 : 637 : gfc_add_expr_to_block (block, tmp);
924 : :
925 : : /* It guarantees memory consistency within the same segment */
926 : 637 : tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
927 : 637 : tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
928 : : gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
929 : : tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
930 : 637 : ASM_VOLATILE_P (tmp) = 1;
931 : 637 : gfc_add_expr_to_block (block, tmp);
932 : 637 : }
933 : :
934 : :
935 : : /* Generate code for an ALLOCATE statement when the argument is an
936 : : allocatable variable. If the variable is currently allocated, it is an
937 : : error to allocate it again.
938 : :
939 : : This function follows the following pseudo-code:
940 : :
941 : : void *
942 : : allocate_allocatable (void *mem, size_t size, integer_type stat)
943 : : {
944 : : if (mem == NULL)
945 : : return allocate (size, stat);
946 : : else
947 : : {
948 : : if (stat)
949 : : stat = LIBERROR_ALLOCATION;
950 : : else
951 : : runtime_error ("Attempting to allocate already allocated variable");
952 : : }
953 : : }
954 : :
955 : : expr must be set to the original expression being allocated for its locus
956 : : and variable name in case a runtime error has to be printed. */
957 : : void
958 : 13024 : gfc_allocate_allocatable (stmtblock_t * block, tree mem, tree size,
959 : : tree token, tree status, tree errmsg, tree errlen,
960 : : tree label_finish, gfc_expr* expr, int corank,
961 : : tree cond, tree alt_alloc, tree extra_success_expr)
962 : : {
963 : 13024 : stmtblock_t alloc_block;
964 : 13024 : tree tmp, null_mem, alloc, error;
965 : 13024 : tree type = TREE_TYPE (mem);
966 : 13024 : symbol_attribute caf_attr;
967 : 13024 : bool need_assign = false, refs_comp = false;
968 : 13024 : gfc_coarray_regtype caf_alloc_type = GFC_CAF_COARRAY_ALLOC;
969 : :
970 : 13024 : size = fold_convert (size_type_node, size);
971 : 13024 : null_mem = gfc_unlikely (fold_build2_loc (input_location, NE_EXPR,
972 : : logical_type_node, mem,
973 : : build_int_cst (type, 0)),
974 : : PRED_FORTRAN_REALLOC);
975 : :
976 : : /* If mem is NULL, we call gfc_allocate_using_malloc or
977 : : gfc_allocate_using_lib. */
978 : 13024 : gfc_start_block (&alloc_block);
979 : :
980 : 13024 : if (flag_coarray == GFC_FCOARRAY_LIB)
981 : 367 : caf_attr = gfc_caf_attr (expr, true, &refs_comp);
982 : :
983 : 13024 : if (flag_coarray == GFC_FCOARRAY_LIB
984 : 367 : && (corank > 0 || caf_attr.codimension))
985 : : {
986 : 332 : tree cond2, sub_caf_tree;
987 : 332 : gfc_se se;
988 : 332 : bool compute_special_caf_types_size = false;
989 : :
990 : 332 : if (expr->ts.type == BT_DERIVED
991 : 83 : && expr->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
992 : 5 : && expr->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
993 : : {
994 : : compute_special_caf_types_size = true;
995 : : caf_alloc_type = GFC_CAF_LOCK_ALLOC;
996 : : }
997 : 328 : else if (expr->ts.type == BT_DERIVED
998 : 79 : && expr->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
999 : 1 : && expr->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
1000 : : {
1001 : : compute_special_caf_types_size = true;
1002 : : caf_alloc_type = GFC_CAF_EVENT_ALLOC;
1003 : : }
1004 : 327 : else if (!caf_attr.coarray_comp && refs_comp)
1005 : : /* Only allocatable components in a derived type coarray can be
1006 : : allocate only. */
1007 : 332 : caf_alloc_type = GFC_CAF_COARRAY_ALLOC_ALLOCATE_ONLY;
1008 : :
1009 : 332 : gfc_init_se (&se, NULL);
1010 : 332 : sub_caf_tree = gfc_get_ultimate_alloc_ptr_comps_caf_token (&se, expr);
1011 : 332 : if (sub_caf_tree == NULL_TREE)
1012 : 141 : sub_caf_tree = token;
1013 : :
1014 : : /* When mem is an array ref, then strip the .data-ref. */
1015 : 332 : if (TREE_CODE (mem) == COMPONENT_REF
1016 : 332 : && !(GFC_ARRAY_TYPE_P (TREE_TYPE (mem))))
1017 : 332 : tmp = TREE_OPERAND (mem, 0);
1018 : : else
1019 : : tmp = mem;
1020 : :
1021 : 332 : if (!(GFC_ARRAY_TYPE_P (TREE_TYPE (tmp))
1022 : 47 : && TYPE_LANG_SPECIFIC (TREE_TYPE (tmp))->corank == 0)
1023 : 379 : && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
1024 : : {
1025 : 96 : symbol_attribute attr;
1026 : :
1027 : 96 : gfc_clear_attr (&attr);
1028 : 96 : tmp = gfc_conv_scalar_to_descriptor (&se, mem, attr);
1029 : 96 : need_assign = true;
1030 : : }
1031 : 332 : gfc_add_block_to_block (&alloc_block, &se.pre);
1032 : :
1033 : : /* In the front end, we represent the lock variable as pointer. However,
1034 : : the FE only passes the pointer around and leaves the actual
1035 : : representation to the library. Hence, we have to convert back to the
1036 : : number of elements. */
1037 : 332 : if (compute_special_caf_types_size)
1038 : 5 : size = fold_build2_loc (input_location, TRUNC_DIV_EXPR, size_type_node,
1039 : 5 : size, TYPE_SIZE_UNIT (ptr_type_node));
1040 : :
1041 : 332 : gfc_allocate_using_caf_lib (&alloc_block, tmp, size, sub_caf_tree,
1042 : : status, errmsg, errlen, caf_alloc_type);
1043 : 332 : if (need_assign)
1044 : 96 : gfc_add_modify (&alloc_block, mem, fold_convert (TREE_TYPE (mem),
1045 : : gfc_conv_descriptor_data_get (tmp)));
1046 : 332 : if (status != NULL_TREE)
1047 : : {
1048 : 13 : TREE_USED (label_finish) = 1;
1049 : 13 : tmp = build1_v (GOTO_EXPR, label_finish);
1050 : 13 : cond2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1051 : 13 : status, build_zero_cst (TREE_TYPE (status)));
1052 : 13 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1053 : : gfc_unlikely (cond2, PRED_FORTRAN_FAIL_ALLOC),
1054 : : tmp, build_empty_stmt (input_location));
1055 : 13 : gfc_add_expr_to_block (&alloc_block, tmp);
1056 : : }
1057 : 332 : }
1058 : : else
1059 : 12692 : gfc_allocate_using_malloc (&alloc_block, mem, size, status,
1060 : : cond, alt_alloc, extra_success_expr);
1061 : :
1062 : 13024 : alloc = gfc_finish_block (&alloc_block);
1063 : :
1064 : : /* If mem is not NULL, we issue a runtime error or set the
1065 : : status variable. */
1066 : 13024 : if (expr)
1067 : : {
1068 : 13024 : tree varname;
1069 : :
1070 : 13024 : gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->symtree);
1071 : 13024 : varname = gfc_build_cstring_const (expr->symtree->name);
1072 : 13024 : varname = gfc_build_addr_expr (pchar_type_node, varname);
1073 : :
1074 : 13024 : error = gfc_trans_runtime_error (true, &expr->where,
1075 : : "Attempting to allocate already"
1076 : : " allocated variable '%s'",
1077 : : varname);
1078 : : }
1079 : : else
1080 : 0 : error = gfc_trans_runtime_error (true, NULL,
1081 : : "Attempting to allocate already allocated"
1082 : : " variable");
1083 : :
1084 : 13024 : if (status != NULL_TREE)
1085 : : {
1086 : 256 : tree status_type = TREE_TYPE (status);
1087 : :
1088 : 256 : error = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
1089 : : status, build_int_cst (status_type, LIBERROR_ALLOCATION));
1090 : : }
1091 : :
1092 : 13024 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, null_mem,
1093 : : error, alloc);
1094 : 13024 : gfc_add_expr_to_block (block, tmp);
1095 : 13024 : }
1096 : :
1097 : :
1098 : : /* Free a given variable. */
1099 : :
1100 : : tree
1101 : 21880 : gfc_call_free (tree var)
1102 : : {
1103 : 21880 : return build_call_expr_loc (input_location,
1104 : : builtin_decl_explicit (BUILT_IN_FREE),
1105 : 21880 : 1, fold_convert (pvoid_type_node, var));
1106 : : }
1107 : :
1108 : :
1109 : : /* Generate the data reference to the finalization procedure pointer associated
1110 : : with the expression passed as argument in EXPR. */
1111 : :
1112 : : static void
1113 : 4577 : get_final_proc_ref (gfc_se *se, gfc_expr *expr, tree class_container)
1114 : : {
1115 : 4577 : gfc_expr *final_wrapper = NULL;
1116 : :
1117 : 4577 : gcc_assert (expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS);
1118 : :
1119 : 4577 : bool using_class_container = false;
1120 : 4577 : if (expr->ts.type == BT_DERIVED)
1121 : 699 : gfc_is_finalizable (expr->ts.u.derived, &final_wrapper);
1122 : 3878 : else if (class_container)
1123 : : {
1124 : 266 : using_class_container = true;
1125 : 266 : se->expr = gfc_class_vtab_final_get (class_container);
1126 : : }
1127 : : else
1128 : : {
1129 : 3612 : final_wrapper = gfc_copy_expr (expr);
1130 : 3612 : gfc_add_vptr_component (final_wrapper);
1131 : 3612 : gfc_add_final_component (final_wrapper);
1132 : : }
1133 : :
1134 : 4577 : if (!using_class_container)
1135 : : {
1136 : 4311 : gcc_assert (final_wrapper->expr_type == EXPR_VARIABLE);
1137 : :
1138 : 4311 : gfc_conv_expr (se, final_wrapper);
1139 : : }
1140 : :
1141 : 4577 : if (POINTER_TYPE_P (TREE_TYPE (se->expr)))
1142 : 965 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
1143 : :
1144 : 4577 : if (expr->ts.type != BT_DERIVED && !using_class_container)
1145 : 3612 : gfc_free_expr (final_wrapper);
1146 : 4577 : }
1147 : :
1148 : :
1149 : : /* Generate the code to obtain the value of the element size of the expression
1150 : : passed as argument in EXPR. */
1151 : :
1152 : : static void
1153 : 4577 : get_elem_size (gfc_se *se, gfc_expr *expr, tree class_container)
1154 : : {
1155 : 4577 : gcc_assert (expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS);
1156 : :
1157 : 4577 : if (expr->ts.type == BT_DERIVED)
1158 : : {
1159 : 699 : se->expr = gfc_typenode_for_spec (&expr->ts);
1160 : 699 : se->expr = TYPE_SIZE_UNIT (se->expr);
1161 : 699 : se->expr = fold_convert (gfc_array_index_type, se->expr);
1162 : : }
1163 : 3878 : else if (class_container)
1164 : 266 : se->expr = gfc_class_vtab_size_get (class_container);
1165 : : else
1166 : : {
1167 : 3612 : gfc_expr *class_size = gfc_copy_expr (expr);
1168 : 3612 : gfc_add_vptr_component (class_size);
1169 : 3612 : gfc_add_size_component (class_size);
1170 : :
1171 : 3612 : gfc_conv_expr (se, class_size);
1172 : 3612 : gcc_assert (se->post.head == NULL_TREE);
1173 : 3612 : gfc_free_expr (class_size);
1174 : : }
1175 : 4577 : }
1176 : :
1177 : :
1178 : : /* Generate the data reference (array) descriptor corresponding to the
1179 : : expression passed as argument in VAR. */
1180 : :
1181 : : static void
1182 : 4577 : get_var_descr (gfc_se *se, gfc_expr *var, tree class_container)
1183 : : {
1184 : 4577 : gfc_se tmp_se;
1185 : :
1186 : 4577 : gcc_assert (var);
1187 : :
1188 : 4577 : gfc_init_se (&tmp_se, NULL);
1189 : :
1190 : 4577 : if (var->ts.type == BT_DERIVED)
1191 : : {
1192 : 699 : tmp_se.want_pointer = 1;
1193 : 699 : if (var->rank)
1194 : : {
1195 : 164 : tmp_se.descriptor_only = 1;
1196 : 164 : gfc_conv_expr_descriptor (&tmp_se, var);
1197 : : }
1198 : : else
1199 : 535 : gfc_conv_expr (&tmp_se, var);
1200 : : }
1201 : 3878 : else if (class_container)
1202 : 266 : tmp_se.expr = gfc_class_data_get (class_container);
1203 : : else
1204 : : {
1205 : 3612 : gfc_expr *array_expr;
1206 : :
1207 : 3612 : array_expr = gfc_copy_expr (var);
1208 : :
1209 : 3612 : tmp_se.want_pointer = 1;
1210 : 3612 : if (array_expr->rank)
1211 : : {
1212 : 1893 : gfc_add_class_array_ref (array_expr);
1213 : 1893 : tmp_se.descriptor_only = 1;
1214 : 1893 : gfc_conv_expr_descriptor (&tmp_se, array_expr);
1215 : : }
1216 : : else
1217 : : {
1218 : 1719 : gfc_add_data_component (array_expr);
1219 : 1719 : gfc_conv_expr (&tmp_se, array_expr);
1220 : 1719 : gcc_assert (tmp_se.post.head == NULL_TREE);
1221 : : }
1222 : 3612 : gfc_free_expr (array_expr);
1223 : : }
1224 : :
1225 : 4577 : if (var->rank == 0)
1226 : : {
1227 : 2410 : if (var->ts.type == BT_DERIVED
1228 : 2410 : || !gfc_is_coarray (var))
1229 : : {
1230 : : /* No copy back needed, hence set attr's allocatable/pointer
1231 : : to zero. */
1232 : 2370 : symbol_attribute attr;
1233 : 2370 : gfc_clear_attr (&attr);
1234 : 2370 : tmp_se.expr = gfc_conv_scalar_to_descriptor (&tmp_se, tmp_se.expr,
1235 : : attr);
1236 : : }
1237 : 2410 : gcc_assert (tmp_se.post.head == NULL_TREE);
1238 : : }
1239 : :
1240 : 4577 : if (!POINTER_TYPE_P (TREE_TYPE (tmp_se.expr)))
1241 : 2480 : tmp_se.expr = gfc_build_addr_expr (NULL, tmp_se.expr);
1242 : :
1243 : 4577 : gfc_add_block_to_block (&se->pre, &tmp_se.pre);
1244 : 4577 : gfc_add_block_to_block (&se->post, &tmp_se.post);
1245 : 4577 : se->expr = tmp_se.expr;
1246 : 4577 : }
1247 : :
1248 : :
1249 : : static void
1250 : 1013 : get_vptr (gfc_se *se, gfc_expr *expr, tree class_container)
1251 : : {
1252 : 1013 : if (class_container)
1253 : 42 : se->expr = gfc_class_vptr_get (class_container);
1254 : : else
1255 : : {
1256 : 971 : gfc_expr *vptr_expr = gfc_copy_expr (expr);
1257 : 971 : gfc_add_vptr_component (vptr_expr);
1258 : :
1259 : 971 : gfc_se tmp_se;
1260 : 971 : gfc_init_se (&tmp_se, NULL);
1261 : 971 : tmp_se.want_pointer = 1;
1262 : 971 : gfc_conv_expr (&tmp_se, vptr_expr);
1263 : 971 : gfc_free_expr (vptr_expr);
1264 : :
1265 : 971 : gfc_add_block_to_block (&se->pre, &tmp_se.pre);
1266 : 971 : gfc_add_block_to_block (&se->post, &tmp_se.post);
1267 : 971 : se->expr = tmp_se.expr;
1268 : : }
1269 : 1013 : }
1270 : :
1271 : :
1272 : : bool
1273 : 2725 : gfc_add_comp_finalizer_call (stmtblock_t *block, tree decl, gfc_component *comp,
1274 : : bool fini_coarray)
1275 : : {
1276 : 2725 : gfc_se se;
1277 : 2725 : stmtblock_t block2;
1278 : 2725 : tree final_fndecl, size, array, tmp, cond;
1279 : 2725 : symbol_attribute attr;
1280 : 2725 : gfc_expr *final_expr = NULL;
1281 : :
1282 : 2725 : if (comp->ts.type != BT_DERIVED && comp->ts.type != BT_CLASS)
1283 : : return false;
1284 : :
1285 : 2725 : gfc_init_block (&block2);
1286 : :
1287 : 2725 : if (comp->ts.type == BT_DERIVED)
1288 : : {
1289 : 1828 : if (comp->attr.pointer)
1290 : : return false;
1291 : :
1292 : 1828 : gfc_is_finalizable (comp->ts.u.derived, &final_expr);
1293 : 1828 : if (!final_expr)
1294 : : return false;
1295 : :
1296 : 27 : gfc_init_se (&se, NULL);
1297 : 27 : gfc_conv_expr (&se, final_expr);
1298 : 27 : final_fndecl = se.expr;
1299 : 27 : size = gfc_typenode_for_spec (&comp->ts);
1300 : 27 : size = TYPE_SIZE_UNIT (size);
1301 : 27 : size = fold_convert (gfc_array_index_type, size);
1302 : :
1303 : 27 : array = decl;
1304 : : }
1305 : : else /* comp->ts.type == BT_CLASS. */
1306 : : {
1307 : 897 : if (CLASS_DATA (comp)->attr.class_pointer)
1308 : : return false;
1309 : :
1310 : 897 : gfc_is_finalizable (CLASS_DATA (comp)->ts.u.derived, &final_expr);
1311 : 897 : final_fndecl = gfc_class_vtab_final_get (decl);
1312 : 897 : size = gfc_class_vtab_size_get (decl);
1313 : 897 : array = gfc_class_data_get (decl);
1314 : : }
1315 : :
1316 : 924 : if (comp->attr.allocatable
1317 : 897 : || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->attr.allocatable))
1318 : : {
1319 : 924 : tmp = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array))
1320 : 924 : ? gfc_conv_descriptor_data_get (array) : array;
1321 : 924 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1322 : 924 : tmp, fold_convert (TREE_TYPE (tmp),
1323 : : null_pointer_node));
1324 : : }
1325 : : else
1326 : 0 : cond = logical_true_node;
1327 : :
1328 : 924 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array)))
1329 : : {
1330 : 490 : gfc_clear_attr (&attr);
1331 : 490 : gfc_init_se (&se, NULL);
1332 : 490 : array = gfc_conv_scalar_to_descriptor (&se, array, attr);
1333 : 490 : gfc_add_block_to_block (&block2, &se.pre);
1334 : 490 : gcc_assert (se.post.head == NULL_TREE);
1335 : : }
1336 : :
1337 : 924 : if (!POINTER_TYPE_P (TREE_TYPE (array)))
1338 : 924 : array = gfc_build_addr_expr (NULL, array);
1339 : :
1340 : 924 : if (!final_expr)
1341 : : {
1342 : 895 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1343 : : final_fndecl,
1344 : 895 : fold_convert (TREE_TYPE (final_fndecl),
1345 : : null_pointer_node));
1346 : 895 : cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1347 : : logical_type_node, cond, tmp);
1348 : : }
1349 : :
1350 : 924 : if (POINTER_TYPE_P (TREE_TYPE (final_fndecl)))
1351 : 924 : final_fndecl = build_fold_indirect_ref_loc (input_location, final_fndecl);
1352 : :
1353 : 924 : tmp = build_call_expr_loc (input_location,
1354 : : final_fndecl, 3, array,
1355 : : size, fini_coarray ? boolean_true_node
1356 : : : boolean_false_node);
1357 : 924 : gfc_add_expr_to_block (&block2, tmp);
1358 : 924 : tmp = gfc_finish_block (&block2);
1359 : :
1360 : 924 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
1361 : : build_empty_stmt (input_location));
1362 : 924 : gfc_add_expr_to_block (block, tmp);
1363 : :
1364 : 924 : return true;
1365 : : }
1366 : :
1367 : :
1368 : : /* Add a call to the finalizer, using the passed *expr. Returns
1369 : : true when a finalizer call has been inserted. */
1370 : :
1371 : : bool
1372 : 25776 : gfc_add_finalizer_call (stmtblock_t *block, gfc_expr *expr2,
1373 : : tree class_container)
1374 : : {
1375 : 25776 : tree tmp;
1376 : 25776 : gfc_ref *ref;
1377 : 25776 : gfc_expr *expr;
1378 : :
1379 : 25776 : if (!expr2 || (expr2->ts.type != BT_DERIVED && expr2->ts.type != BT_CLASS))
1380 : : return false;
1381 : :
1382 : : /* Finalization of these temporaries is made by explicit calls in
1383 : : resolve.cc(generate_component_assignments). */
1384 : 6403 : if (expr2->expr_type == EXPR_VARIABLE
1385 : 6403 : && expr2->symtree->n.sym->name[0] == '_'
1386 : 91 : && expr2->ts.type == BT_DERIVED
1387 : 37 : && expr2->ts.u.derived->attr.defined_assign_comp)
1388 : : return false;
1389 : :
1390 : 6372 : if (expr2->ts.type == BT_DERIVED
1391 : 6372 : && !gfc_is_finalizable (expr2->ts.u.derived, NULL))
1392 : : return false;
1393 : :
1394 : : /* If we have a class array, we need go back to the class
1395 : : container. */
1396 : 4577 : expr = gfc_copy_expr (expr2);
1397 : :
1398 : 4577 : if (expr->ref && expr->ref->next && !expr->ref->next->next
1399 : 1003 : && expr->ref->next->type == REF_ARRAY
1400 : 936 : && expr->ref->type == REF_COMPONENT
1401 : 936 : && strcmp (expr->ref->u.c.component->name, "_data") == 0)
1402 : : {
1403 : 935 : gfc_free_ref_list (expr->ref);
1404 : 935 : expr->ref = NULL;
1405 : : }
1406 : : else
1407 : 5488 : for (ref = expr->ref; ref; ref = ref->next)
1408 : 1846 : if (ref->next && ref->next->next && !ref->next->next->next
1409 : 328 : && ref->next->next->type == REF_ARRAY
1410 : 309 : && ref->next->type == REF_COMPONENT
1411 : 309 : && strcmp (ref->next->u.c.component->name, "_data") == 0)
1412 : : {
1413 : 309 : gfc_free_ref_list (ref->next);
1414 : 309 : ref->next = NULL;
1415 : : }
1416 : :
1417 : 4577 : if (expr->ts.type == BT_CLASS && (!expr2->rank || !expr2->corank)
1418 : 3833 : && !expr2->ref && CLASS_DATA (expr2->symtree->n.sym)->as)
1419 : : {
1420 : 3 : expr->rank = CLASS_DATA (expr2->symtree->n.sym)->as->rank;
1421 : 3 : expr->corank = CLASS_DATA (expr2->symtree->n.sym)->as->corank;
1422 : : }
1423 : :
1424 : 4577 : stmtblock_t tmp_block;
1425 : 4577 : gfc_start_block (&tmp_block);
1426 : :
1427 : 4577 : gfc_se final_se;
1428 : 4577 : gfc_init_se (&final_se, NULL);
1429 : 4577 : get_final_proc_ref (&final_se, expr, class_container);
1430 : 4577 : gfc_add_block_to_block (block, &final_se.pre);
1431 : :
1432 : 4577 : gfc_se size_se;
1433 : 4577 : gfc_init_se (&size_se, NULL);
1434 : 4577 : get_elem_size (&size_se, expr, class_container);
1435 : 4577 : gfc_add_block_to_block (&tmp_block, &size_se.pre);
1436 : :
1437 : 4577 : gfc_se desc_se;
1438 : 4577 : gfc_init_se (&desc_se, NULL);
1439 : 4577 : get_var_descr (&desc_se, expr, class_container);
1440 : 4577 : gfc_add_block_to_block (&tmp_block, &desc_se.pre);
1441 : :
1442 : 4577 : tmp = build_call_expr_loc (input_location, final_se.expr, 3,
1443 : : desc_se.expr, size_se.expr,
1444 : : boolean_false_node);
1445 : :
1446 : 4577 : gfc_add_expr_to_block (&tmp_block, tmp);
1447 : :
1448 : 4577 : gfc_add_block_to_block (&tmp_block, &desc_se.post);
1449 : 4577 : gfc_add_block_to_block (&tmp_block, &size_se.post);
1450 : :
1451 : 4577 : tmp = gfc_finish_block (&tmp_block);
1452 : :
1453 : 4577 : if (expr->ts.type == BT_CLASS
1454 : 4577 : && !gfc_is_finalizable (expr->ts.u.derived, NULL))
1455 : : {
1456 : 3878 : tree cond;
1457 : :
1458 : 3878 : tree ptr = gfc_build_addr_expr (NULL_TREE, final_se.expr);
1459 : :
1460 : 3878 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1461 : 3878 : ptr, build_int_cst (TREE_TYPE (ptr), 0));
1462 : :
1463 : : /* For CLASS(*) not only sym->_vtab->_final can be NULL
1464 : : but already sym->_vtab itself. */
1465 : 3878 : if (UNLIMITED_POLY (expr))
1466 : : {
1467 : 1013 : tree cond2;
1468 : 1013 : gfc_se vptr_se;
1469 : :
1470 : 1013 : gfc_init_se (&vptr_se, NULL);
1471 : 1013 : get_vptr (&vptr_se, expr, class_container);
1472 : :
1473 : 1013 : cond2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1474 : : vptr_se.expr,
1475 : 1013 : build_int_cst (TREE_TYPE (vptr_se.expr), 0));
1476 : 1013 : cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1477 : : logical_type_node, cond2, cond);
1478 : : }
1479 : :
1480 : 3878 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1481 : : cond, tmp, build_empty_stmt (input_location));
1482 : : }
1483 : :
1484 : 4577 : gfc_add_expr_to_block (block, tmp);
1485 : 4577 : gfc_add_block_to_block (block, &final_se.post);
1486 : 4577 : gfc_free_expr (expr);
1487 : :
1488 : 4577 : return true;
1489 : : }
1490 : :
1491 : :
1492 : : /* F2018 (7.5.6.3): "When an intrinsic assignment statement is executed
1493 : : (10.2.1.3), if the variable is not an unallocated allocatable variable,
1494 : : it is finalized after evaluation of expr and before the definition of
1495 : : the variable. If the variable is an allocated allocatable variable, or
1496 : : has an allocated allocatable subobject, that would be deallocated by
1497 : : intrinsic assignment, the finalization occurs before the deallocation */
1498 : :
1499 : : bool
1500 : 299338 : gfc_assignment_finalizer_call (gfc_se *lse, gfc_expr *expr1, bool init_flag)
1501 : : {
1502 : 299338 : symbol_attribute lhs_attr;
1503 : 299338 : tree final_expr;
1504 : 299338 : tree ptr;
1505 : 299338 : tree cond;
1506 : 299338 : gfc_se se;
1507 : 299338 : gfc_symbol *sym = expr1->symtree->n.sym;
1508 : 299338 : gfc_ref *ref = expr1->ref;
1509 : 299338 : stmtblock_t final_block;
1510 : 299338 : gfc_init_block (&final_block);
1511 : 299338 : gfc_expr *finalize_expr;
1512 : 299338 : bool class_array_ref;
1513 : :
1514 : : /* We have to exclude vtable procedures (_copy and _final especially), uses
1515 : : of gfc_trans_assignment_1 in initialization and allocation before trying
1516 : : to build a final call. */
1517 : 299338 : if (!expr1->must_finalize
1518 : 1066 : || sym->attr.artificial
1519 : 1066 : || sym->ns->proc_name->attr.artificial
1520 : 1066 : || init_flag)
1521 : : return false;
1522 : :
1523 : 672 : class_array_ref = ref && ref->type == REF_COMPONENT
1524 : 583 : && !strcmp (ref->u.c.component->name, "_data")
1525 : 491 : && ref->next && ref->next->type == REF_ARRAY
1526 : 1557 : && !ref->next->next;
1527 : :
1528 : 1066 : if (class_array_ref)
1529 : : {
1530 : 479 : finalize_expr = gfc_lval_expr_from_sym (sym);
1531 : 479 : finalize_expr->must_finalize = 1;
1532 : 479 : ref = NULL;
1533 : : }
1534 : : else
1535 : 587 : finalize_expr = gfc_copy_expr (expr1);
1536 : :
1537 : : /* F2018 7.5.6.2: Only finalizable entities are finalized. */
1538 : 232 : if (!(expr1->ts.type == BT_DERIVED
1539 : 232 : && gfc_is_finalizable (expr1->ts.u.derived, NULL))
1540 : 1066 : && expr1->ts.type != BT_CLASS)
1541 : : return false;
1542 : :
1543 : 1066 : if (!gfc_may_be_finalized (sym->ts))
1544 : : return false;
1545 : :
1546 : 1016 : gfc_init_block (&final_block);
1547 : 1016 : bool finalizable = gfc_add_finalizer_call (&final_block, finalize_expr);
1548 : 1016 : gfc_free_expr (finalize_expr);
1549 : :
1550 : 1016 : if (!finalizable)
1551 : : return false;
1552 : :
1553 : 1016 : lhs_attr = gfc_expr_attr (expr1);
1554 : :
1555 : : /* Check allocatable/pointer is allocated/associated. */
1556 : 1016 : if (lhs_attr.allocatable || lhs_attr.pointer)
1557 : : {
1558 : 862 : if (expr1->ts.type == BT_CLASS)
1559 : : {
1560 : 784 : ptr = gfc_get_class_from_gfc_expr (expr1);
1561 : 784 : gcc_assert (ptr != NULL_TREE);
1562 : 784 : ptr = gfc_class_data_get (ptr);
1563 : 784 : if (lhs_attr.dimension)
1564 : 536 : ptr = gfc_conv_descriptor_data_get (ptr);
1565 : : }
1566 : : else
1567 : : {
1568 : 78 : gfc_init_se (&se, NULL);
1569 : 78 : if (expr1->rank)
1570 : : {
1571 : 30 : gfc_conv_expr_descriptor (&se, expr1);
1572 : 30 : ptr = gfc_conv_descriptor_data_get (se.expr);
1573 : : }
1574 : : else
1575 : : {
1576 : 48 : gfc_conv_expr (&se, expr1);
1577 : 48 : ptr = gfc_build_addr_expr (NULL_TREE, se.expr);
1578 : : }
1579 : : }
1580 : :
1581 : 862 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1582 : 862 : ptr, build_zero_cst (TREE_TYPE (ptr)));
1583 : 862 : final_expr = build3_loc (input_location, COND_EXPR, void_type_node,
1584 : : cond, gfc_finish_block (&final_block),
1585 : : build_empty_stmt (input_location));
1586 : : }
1587 : : else
1588 : 154 : final_expr = gfc_finish_block (&final_block);
1589 : :
1590 : : /* Check optional present. */
1591 : 1016 : if (sym->attr.optional)
1592 : : {
1593 : 0 : cond = gfc_conv_expr_present (sym);
1594 : 0 : final_expr = build3_loc (input_location, COND_EXPR, void_type_node,
1595 : : cond, final_expr,
1596 : : build_empty_stmt (input_location));
1597 : : }
1598 : :
1599 : 1016 : gfc_add_expr_to_block (&lse->finalblock, final_expr);
1600 : :
1601 : 1016 : return true;
1602 : : }
1603 : :
1604 : :
1605 : : /* Finalize a TREE expression using the finalizer wrapper. The result is
1606 : : fixed in order to prevent repeated calls. */
1607 : :
1608 : : void
1609 : 605 : gfc_finalize_tree_expr (gfc_se *se, gfc_symbol *derived,
1610 : : symbol_attribute attr, int rank)
1611 : : {
1612 : 605 : tree vptr, final_fndecl, desc, tmp, size, is_final;
1613 : 605 : tree data_ptr, data_null, cond;
1614 : 605 : gfc_symbol *vtab;
1615 : 605 : gfc_se post_se;
1616 : 605 : bool is_class = GFC_CLASS_TYPE_P (TREE_TYPE (se->expr));
1617 : :
1618 : 605 : if (attr.pointer)
1619 : 52 : return;
1620 : :
1621 : : /* Derived type function results with components that have defined
1622 : : assignements are handled in resolve.cc(generate_component_assignments) */
1623 : 602 : if (derived && (derived->attr.is_c_interop
1624 : 217 : || derived->attr.is_iso_c
1625 : 217 : || derived->attr.is_bind_c
1626 : 217 : || derived->attr.defined_assign_comp))
1627 : : return;
1628 : :
1629 : 596 : if (is_class)
1630 : : {
1631 : 366 : if (!VAR_P (se->expr))
1632 : : {
1633 : 0 : desc = gfc_evaluate_now (se->expr, &se->pre);
1634 : 0 : se->expr = desc;
1635 : : }
1636 : 366 : desc = gfc_class_data_get (se->expr);
1637 : 366 : vptr = gfc_class_vptr_get (se->expr);
1638 : : }
1639 : 230 : else if (derived && gfc_is_finalizable (derived, NULL))
1640 : : {
1641 : 191 : if (!derived->components && (!rank || attr.elemental))
1642 : : {
1643 : : /* Any attempt to assign zero length entities, causes the gimplifier
1644 : : all manner of problems. Instead, a variable is created to act as
1645 : : as the argument for the final call. */
1646 : 4 : desc = gfc_create_var (TREE_TYPE (se->expr), "zero");
1647 : : }
1648 : 187 : else if (se->direct_byref)
1649 : : {
1650 : 0 : desc = gfc_evaluate_now (se->expr, &se->finalblock);
1651 : 0 : if (derived->attr.alloc_comp)
1652 : : {
1653 : : /* Need to copy allocated components and not finalize. */
1654 : 0 : tmp = gfc_copy_alloc_comp_no_fini (derived, se->expr, desc, rank, 0);
1655 : 0 : gfc_add_expr_to_block (&se->finalblock, tmp);
1656 : : }
1657 : : }
1658 : : else
1659 : : {
1660 : 187 : desc = gfc_evaluate_now (se->expr, &se->pre);
1661 : 187 : se->expr = gfc_evaluate_now (desc, &se->pre);
1662 : 187 : if (derived->attr.alloc_comp)
1663 : : {
1664 : : /* Need to copy allocated components and not finalize. */
1665 : 37 : tmp = gfc_copy_alloc_comp_no_fini (derived, se->expr, desc, rank, 0);
1666 : 37 : gfc_add_expr_to_block (&se->pre, tmp);
1667 : : }
1668 : : }
1669 : :
1670 : 191 : vtab = gfc_find_derived_vtab (derived);
1671 : 191 : if (vtab->backend_decl == NULL_TREE)
1672 : 0 : vptr = gfc_get_symbol_decl (vtab);
1673 : : else
1674 : : vptr = vtab->backend_decl;
1675 : 191 : vptr = gfc_build_addr_expr (NULL, vptr);
1676 : : }
1677 : : else
1678 : 39 : return;
1679 : :
1680 : 557 : size = gfc_vptr_size_get (vptr);
1681 : 557 : final_fndecl = gfc_vptr_final_get (vptr);
1682 : 557 : is_final = fold_build2_loc (input_location, NE_EXPR,
1683 : : logical_type_node,
1684 : : final_fndecl,
1685 : 557 : fold_convert (TREE_TYPE (final_fndecl),
1686 : : null_pointer_node));
1687 : :
1688 : 557 : final_fndecl = build_fold_indirect_ref_loc (input_location,
1689 : : final_fndecl);
1690 : 557 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
1691 : : {
1692 : 317 : if (is_class || attr.elemental)
1693 : 190 : desc = gfc_conv_scalar_to_descriptor (se, desc, attr);
1694 : : else
1695 : : {
1696 : 127 : gfc_init_se (&post_se, NULL);
1697 : 127 : desc = gfc_conv_scalar_to_descriptor (&post_se, desc, attr);
1698 : 127 : gfc_add_expr_to_block (&se->pre, gfc_finish_block (&post_se.pre));
1699 : : }
1700 : : }
1701 : :
1702 : 557 : if (derived && !derived->components)
1703 : : {
1704 : : /* All the conditions below break down for zero length derived types. */
1705 : 4 : tmp = build_call_expr_loc (input_location, final_fndecl, 3,
1706 : : gfc_build_addr_expr (NULL, desc),
1707 : : size, boolean_false_node);
1708 : 4 : gfc_add_expr_to_block (&se->finalblock, tmp);
1709 : 4 : return;
1710 : : }
1711 : :
1712 : 553 : if (!VAR_P (desc))
1713 : : {
1714 : 216 : tmp = gfc_create_var (TREE_TYPE (desc), "res");
1715 : 216 : if (se->direct_byref)
1716 : 0 : gfc_add_modify (&se->finalblock, tmp, desc);
1717 : : else
1718 : 216 : gfc_add_modify (&se->pre, tmp, desc);
1719 : : desc = tmp;
1720 : : }
1721 : :
1722 : 553 : data_ptr = gfc_conv_descriptor_data_get (desc);
1723 : 553 : data_null = fold_convert (TREE_TYPE (data_ptr), null_pointer_node);
1724 : 553 : cond = fold_build2_loc (input_location, NE_EXPR,
1725 : : logical_type_node, data_ptr, data_null);
1726 : 553 : is_final = fold_build2_loc (input_location, TRUTH_AND_EXPR,
1727 : : logical_type_node, is_final, cond);
1728 : 553 : tmp = build_call_expr_loc (input_location, final_fndecl, 3,
1729 : : gfc_build_addr_expr (NULL, desc),
1730 : : size, boolean_false_node);
1731 : 553 : tmp = fold_build3_loc (input_location, COND_EXPR,
1732 : : void_type_node, is_final, tmp,
1733 : : build_empty_stmt (input_location));
1734 : :
1735 : 553 : if (is_class && se->ss && se->ss->loop)
1736 : : {
1737 : 134 : gfc_add_expr_to_block (&se->loop->post, tmp);
1738 : 134 : tmp = fold_build3_loc (input_location, COND_EXPR,
1739 : : void_type_node, cond,
1740 : : gfc_call_free (data_ptr),
1741 : : build_empty_stmt (input_location));
1742 : 134 : gfc_add_expr_to_block (&se->loop->post, tmp);
1743 : 134 : gfc_add_modify (&se->loop->post, data_ptr, data_null);
1744 : : }
1745 : : else
1746 : : {
1747 : 419 : gfc_add_expr_to_block (&se->finalblock, tmp);
1748 : :
1749 : : /* Let the scalarizer take care of freeing of temporary arrays. */
1750 : 419 : if (attr.allocatable && !(se->loop && se->loop->temp_dim))
1751 : : {
1752 : 232 : tmp = fold_build3_loc (input_location, COND_EXPR,
1753 : : void_type_node, cond,
1754 : : gfc_call_free (data_ptr),
1755 : : build_empty_stmt (input_location));
1756 : 232 : gfc_add_expr_to_block (&se->finalblock, tmp);
1757 : 232 : gfc_add_modify (&se->finalblock, data_ptr, data_null);
1758 : : }
1759 : : }
1760 : : }
1761 : :
1762 : :
1763 : : /* User-deallocate; we emit the code directly from the front-end, and the
1764 : : logic is the same as the previous library function:
1765 : :
1766 : : void
1767 : : deallocate (void *pointer, GFC_INTEGER_4 * stat)
1768 : : {
1769 : : if (!pointer)
1770 : : {
1771 : : if (stat)
1772 : : *stat = 1;
1773 : : else
1774 : : runtime_error ("Attempt to DEALLOCATE unallocated memory.");
1775 : : }
1776 : : else
1777 : : {
1778 : : free (pointer);
1779 : : if (stat)
1780 : : *stat = 0;
1781 : : }
1782 : : }
1783 : :
1784 : : In this front-end version, status doesn't have to be GFC_INTEGER_4.
1785 : : Moreover, if CAN_FAIL is true, then we will not emit a runtime error,
1786 : : even when no status variable is passed to us (this is used for
1787 : : unconditional deallocation generated by the front-end at end of
1788 : : each procedure).
1789 : :
1790 : : If a runtime-message is possible, `expr' must point to the original
1791 : : expression being deallocated for its locus and variable name.
1792 : :
1793 : : For coarrays, "pointer" must be the array descriptor and not its
1794 : : "data" component.
1795 : :
1796 : : COARRAY_DEALLOC_MODE gives the mode unregister coarrays. Available modes are
1797 : : the ones of GFC_CAF_DEREGTYPE, -1 when the mode for deregistration is to be
1798 : : analyzed and set by this routine, and -2 to indicate that a non-coarray is to
1799 : : be deallocated. */
1800 : : tree
1801 : 19186 : gfc_deallocate_with_status (tree pointer, tree status, tree errmsg, tree errlen,
1802 : : tree label_finish, bool can_fail, gfc_expr *expr,
1803 : : int coarray_dealloc_mode, tree class_container,
1804 : : tree add_when_allocated, tree caf_token,
1805 : : bool unalloc_ok)
1806 : : {
1807 : 19186 : stmtblock_t null, non_null;
1808 : 19186 : tree cond, tmp, error;
1809 : 19186 : tree status_type = NULL_TREE;
1810 : 19186 : tree token = NULL_TREE;
1811 : 19186 : tree descr = NULL_TREE;
1812 : 19186 : gfc_coarray_deregtype caf_dereg_type = GFC_CAF_COARRAY_DEREGISTER;
1813 : :
1814 : 19186 : if (coarray_dealloc_mode >= GFC_CAF_COARRAY_ANALYZE)
1815 : : {
1816 : 370 : if (flag_coarray == GFC_FCOARRAY_LIB)
1817 : : {
1818 : 211 : if (caf_token)
1819 : : {
1820 : 54 : token = caf_token;
1821 : 54 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (pointer)))
1822 : 38 : pointer = gfc_conv_descriptor_data_get (pointer);
1823 : : }
1824 : : else
1825 : : {
1826 : 157 : tree caf_type, caf_decl = pointer;
1827 : 157 : pointer = gfc_conv_descriptor_data_get (caf_decl);
1828 : 157 : caf_type = TREE_TYPE (caf_decl);
1829 : 157 : STRIP_NOPS (pointer);
1830 : 157 : if (GFC_DESCRIPTOR_TYPE_P (caf_type))
1831 : 157 : token = gfc_conv_descriptor_token (caf_decl);
1832 : 0 : else if (DECL_LANG_SPECIFIC (caf_decl)
1833 : 0 : && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
1834 : 0 : token = GFC_DECL_TOKEN (caf_decl);
1835 : : else
1836 : : {
1837 : 0 : gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
1838 : : && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type)
1839 : : != NULL_TREE);
1840 : 0 : token = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
1841 : : }
1842 : : }
1843 : :
1844 : 211 : if (coarray_dealloc_mode == GFC_CAF_COARRAY_ANALYZE)
1845 : : {
1846 : 4 : bool comp_ref;
1847 : 4 : if (expr && !gfc_caf_attr (expr, false, &comp_ref).coarray_comp
1848 : 4 : && comp_ref)
1849 : 0 : caf_dereg_type = GFC_CAF_COARRAY_DEALLOCATE_ONLY;
1850 : : // else do a deregister as set by default.
1851 : : }
1852 : : else
1853 : : caf_dereg_type = (enum gfc_coarray_deregtype) coarray_dealloc_mode;
1854 : : }
1855 : 159 : else if (flag_coarray == GFC_FCOARRAY_SINGLE
1856 : 159 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (pointer)))
1857 : 159 : pointer = gfc_conv_descriptor_data_get (pointer);
1858 : : }
1859 : 18816 : else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (pointer)))
1860 : : {
1861 : 15001 : descr = pointer;
1862 : 15001 : pointer = gfc_conv_descriptor_data_get (pointer);
1863 : : }
1864 : :
1865 : 19186 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, pointer,
1866 : 19186 : build_int_cst (TREE_TYPE (pointer), 0));
1867 : :
1868 : : /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
1869 : : we emit a runtime error. */
1870 : 19186 : gfc_start_block (&null);
1871 : 19186 : if (!can_fail)
1872 : : {
1873 : 6817 : tree varname;
1874 : :
1875 : 6817 : gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
1876 : :
1877 : 6817 : varname = gfc_build_cstring_const (expr->symtree->name);
1878 : 6817 : varname = gfc_build_addr_expr (pchar_type_node, varname);
1879 : :
1880 : 6817 : error = gfc_trans_runtime_error (true, &expr->where,
1881 : : "Attempt to DEALLOCATE unallocated '%s'",
1882 : : varname);
1883 : : }
1884 : : else
1885 : 12369 : error = build_empty_stmt (input_location);
1886 : :
1887 : 19186 : if (status != NULL_TREE && !integer_zerop (status))
1888 : : {
1889 : 1494 : tree cond2;
1890 : :
1891 : 1494 : status_type = TREE_TYPE (TREE_TYPE (status));
1892 : 1494 : cond2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1893 : 1494 : status, build_int_cst (TREE_TYPE (status), 0));
1894 : 1494 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
1895 : : fold_build1_loc (input_location, INDIRECT_REF,
1896 : : status_type, status),
1897 : 2988 : build_int_cst (status_type, unalloc_ok ? 0 : 1));
1898 : 1494 : error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1899 : : cond2, tmp, error);
1900 : : }
1901 : :
1902 : 19186 : gfc_add_expr_to_block (&null, error);
1903 : :
1904 : : /* When POINTER is not NULL, we free it. */
1905 : 19186 : gfc_start_block (&non_null);
1906 : 19186 : if (add_when_allocated)
1907 : 5179 : gfc_add_expr_to_block (&non_null, add_when_allocated);
1908 : 19186 : gfc_add_finalizer_call (&non_null, expr, class_container);
1909 : 19186 : if (coarray_dealloc_mode == GFC_CAF_COARRAY_NOCOARRAY
1910 : 370 : || flag_coarray != GFC_FCOARRAY_LIB)
1911 : : {
1912 : 18975 : tmp = build_call_expr_loc (input_location,
1913 : : builtin_decl_explicit (BUILT_IN_FREE), 1,
1914 : : fold_convert (pvoid_type_node, pointer));
1915 : 18975 : if (flag_openmp_allocators && coarray_dealloc_mode < GFC_CAF_COARRAY_ANALYZE)
1916 : : {
1917 : 61 : tree cond, omp_tmp;
1918 : 61 : if (descr)
1919 : 46 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
1920 : : gfc_conv_descriptor_version (descr),
1921 : : integer_one_node);
1922 : : else
1923 : 15 : cond = gfc_omp_call_is_alloc (pointer);
1924 : 61 : omp_tmp = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
1925 : 61 : omp_tmp = build_call_expr_loc (input_location, omp_tmp, 2, pointer,
1926 : : build_zero_cst (ptr_type_node));
1927 : 61 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
1928 : : omp_tmp, tmp);
1929 : : }
1930 : 18975 : gfc_add_expr_to_block (&non_null, tmp);
1931 : 18975 : gfc_add_modify (&non_null, pointer, build_int_cst (TREE_TYPE (pointer),
1932 : : 0));
1933 : 18975 : if (flag_openmp_allocators && descr)
1934 : 46 : gfc_add_modify (&non_null, gfc_conv_descriptor_version (descr),
1935 : : integer_zero_node);
1936 : :
1937 : 18975 : if (status != NULL_TREE && !integer_zerop (status))
1938 : : {
1939 : : /* We set STATUS to zero if it is present. */
1940 : 1477 : tree status_type = TREE_TYPE (TREE_TYPE (status));
1941 : 1477 : tree cond2;
1942 : :
1943 : 1477 : cond2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1944 : : status,
1945 : 1477 : build_int_cst (TREE_TYPE (status), 0));
1946 : 1477 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
1947 : : fold_build1_loc (input_location, INDIRECT_REF,
1948 : : status_type, status),
1949 : : build_int_cst (status_type, 0));
1950 : 1477 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1951 : : gfc_unlikely (cond2, PRED_FORTRAN_FAIL_ALLOC),
1952 : : tmp, build_empty_stmt (input_location));
1953 : 1477 : gfc_add_expr_to_block (&non_null, tmp);
1954 : : }
1955 : : }
1956 : : else
1957 : : {
1958 : 211 : tree cond2, pstat = null_pointer_node;
1959 : :
1960 : 211 : if (errmsg == NULL_TREE)
1961 : : {
1962 : 201 : gcc_assert (errlen == NULL_TREE);
1963 : 201 : errmsg = null_pointer_node;
1964 : 201 : errlen = integer_zero_node;
1965 : : }
1966 : : else
1967 : : {
1968 : 10 : gcc_assert (errlen != NULL_TREE);
1969 : 10 : if (!POINTER_TYPE_P (TREE_TYPE (errmsg)))
1970 : 0 : errmsg = gfc_build_addr_expr (NULL_TREE, errmsg);
1971 : : }
1972 : :
1973 : 211 : if (status != NULL_TREE && !integer_zerop (status))
1974 : : {
1975 : 17 : gcc_assert (status_type == integer_type_node);
1976 : : pstat = status;
1977 : : }
1978 : :
1979 : 211 : token = gfc_build_addr_expr (NULL_TREE, token);
1980 : 211 : gcc_assert (caf_dereg_type > GFC_CAF_COARRAY_ANALYZE);
1981 : 211 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_deregister, 5,
1982 : : token,
1983 : : build_int_cst (integer_type_node,
1984 : 211 : caf_dereg_type),
1985 : : pstat, errmsg, errlen);
1986 : 211 : gfc_add_expr_to_block (&non_null, tmp);
1987 : :
1988 : : /* It guarantees memory consistency within the same segment */
1989 : 211 : tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
1990 : 211 : tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1991 : : gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1992 : : tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1993 : 211 : ASM_VOLATILE_P (tmp) = 1;
1994 : 211 : gfc_add_expr_to_block (&non_null, tmp);
1995 : :
1996 : 211 : if (status != NULL_TREE && !integer_zerop (status))
1997 : : {
1998 : 17 : tree stat = build_fold_indirect_ref_loc (input_location, status);
1999 : 17 : tree nullify = fold_build2_loc (input_location, MODIFY_EXPR,
2000 : : void_type_node, pointer,
2001 : 17 : build_int_cst (TREE_TYPE (pointer),
2002 : : 0));
2003 : :
2004 : 17 : TREE_USED (label_finish) = 1;
2005 : 17 : tmp = build1_v (GOTO_EXPR, label_finish);
2006 : 17 : cond2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
2007 : 17 : stat, build_zero_cst (TREE_TYPE (stat)));
2008 : 17 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
2009 : : gfc_unlikely (cond2, PRED_FORTRAN_REALLOC),
2010 : : tmp, nullify);
2011 : 17 : gfc_add_expr_to_block (&non_null, tmp);
2012 : : }
2013 : : else
2014 : 194 : gfc_add_modify (&non_null, pointer, build_int_cst (TREE_TYPE (pointer),
2015 : : 0));
2016 : : }
2017 : :
2018 : 19186 : return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
2019 : : gfc_finish_block (&null),
2020 : 19186 : gfc_finish_block (&non_null));
2021 : : }
2022 : :
2023 : :
2024 : : /* Generate code for deallocation of allocatable scalars (variables or
2025 : : components). Before the object itself is freed, any allocatable
2026 : : subcomponents are being deallocated. */
2027 : :
2028 : : tree
2029 : 4958 : gfc_deallocate_scalar_with_status (tree pointer, tree status, tree label_finish,
2030 : : bool can_fail, gfc_expr *expr,
2031 : : gfc_typespec ts, tree class_container,
2032 : : bool coarray, bool unalloc_ok, tree errmsg,
2033 : : tree errmsg_len)
2034 : : {
2035 : 4958 : stmtblock_t null, non_null;
2036 : 4958 : tree cond, tmp, error;
2037 : 4958 : bool finalizable, comp_ref;
2038 : 4958 : gfc_coarray_deregtype caf_dereg_type = GFC_CAF_COARRAY_DEREGISTER;
2039 : :
2040 : 4958 : if (coarray && expr && !gfc_caf_attr (expr, false, &comp_ref).coarray_comp
2041 : 4998 : && comp_ref)
2042 : 40 : caf_dereg_type = GFC_CAF_COARRAY_DEALLOCATE_ONLY;
2043 : :
2044 : 4958 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, pointer,
2045 : 4958 : build_int_cst (TREE_TYPE (pointer), 0));
2046 : :
2047 : : /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
2048 : : we emit a runtime error. */
2049 : 4958 : gfc_start_block (&null);
2050 : 4958 : if (!can_fail)
2051 : : {
2052 : 3230 : tree varname;
2053 : :
2054 : 3230 : gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
2055 : :
2056 : 3230 : varname = gfc_build_cstring_const (expr->symtree->name);
2057 : 3230 : varname = gfc_build_addr_expr (pchar_type_node, varname);
2058 : :
2059 : 3230 : error = gfc_trans_runtime_error (true, &expr->where,
2060 : : "Attempt to DEALLOCATE unallocated '%s'",
2061 : : varname);
2062 : : }
2063 : : else
2064 : 1728 : error = build_empty_stmt (input_location);
2065 : :
2066 : 4958 : if (status != NULL_TREE && !integer_zerop (status))
2067 : : {
2068 : 697 : tree status_type = TREE_TYPE (TREE_TYPE (status));
2069 : 697 : tree cond2;
2070 : :
2071 : 697 : cond2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
2072 : 697 : status, build_int_cst (TREE_TYPE (status), 0));
2073 : 697 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
2074 : : fold_build1_loc (input_location, INDIRECT_REF,
2075 : : status_type, status),
2076 : 1394 : build_int_cst (status_type, unalloc_ok ? 0 : 1));
2077 : 697 : error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
2078 : : cond2, tmp, error);
2079 : : }
2080 : 4958 : gfc_add_expr_to_block (&null, error);
2081 : :
2082 : : /* When POINTER is not NULL, we free it. */
2083 : 4958 : gfc_start_block (&non_null);
2084 : :
2085 : : /* Free allocatable components. */
2086 : 4958 : finalizable = gfc_add_finalizer_call (&non_null, expr, class_container);
2087 : 4958 : if (!finalizable && ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp)
2088 : : {
2089 : 0 : int caf_mode = coarray
2090 : 465 : ? ((caf_dereg_type == GFC_CAF_COARRAY_DEALLOCATE_ONLY
2091 : : ? GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY : 0)
2092 : : | GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
2093 : 3 : | GFC_STRUCTURE_CAF_MODE_IN_COARRAY)
2094 : : : 0;
2095 : 3 : if (coarray && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (pointer)))
2096 : 0 : tmp = gfc_conv_descriptor_data_get (pointer);
2097 : : else
2098 : 465 : tmp = build_fold_indirect_ref_loc (input_location, pointer);
2099 : 465 : tmp = gfc_deallocate_alloc_comp (ts.u.derived, tmp, 0, caf_mode);
2100 : 465 : gfc_add_expr_to_block (&non_null, tmp);
2101 : : }
2102 : :
2103 : 4958 : if (!coarray || flag_coarray == GFC_FCOARRAY_SINGLE)
2104 : : {
2105 : 4921 : tmp = build_call_expr_loc (input_location,
2106 : : builtin_decl_explicit (BUILT_IN_FREE), 1,
2107 : : fold_convert (pvoid_type_node, pointer));
2108 : 4921 : if (flag_openmp_allocators)
2109 : : {
2110 : 31 : tree cond, omp_tmp;
2111 : 31 : cond = gfc_omp_call_is_alloc (pointer);
2112 : 31 : omp_tmp = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
2113 : 31 : omp_tmp = build_call_expr_loc (input_location, omp_tmp, 2, pointer,
2114 : : build_zero_cst (ptr_type_node));
2115 : 31 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
2116 : : omp_tmp, tmp);
2117 : : }
2118 : 4921 : gfc_add_expr_to_block (&non_null, tmp);
2119 : :
2120 : 4921 : if (status != NULL_TREE && !integer_zerop (status))
2121 : : {
2122 : : /* We set STATUS to zero if it is present. */
2123 : 697 : tree status_type = TREE_TYPE (TREE_TYPE (status));
2124 : 697 : tree cond2;
2125 : :
2126 : 697 : cond2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
2127 : : status,
2128 : 697 : build_int_cst (TREE_TYPE (status), 0));
2129 : 697 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
2130 : : fold_build1_loc (input_location, INDIRECT_REF,
2131 : : status_type, status),
2132 : : build_int_cst (status_type, 0));
2133 : 697 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
2134 : : cond2, tmp, build_empty_stmt (input_location));
2135 : 697 : gfc_add_expr_to_block (&non_null, tmp);
2136 : : }
2137 : : }
2138 : : else
2139 : : {
2140 : 37 : tree token;
2141 : 37 : tree pstat = null_pointer_node, perrmsg = null_pointer_node,
2142 : 37 : perrlen = size_zero_node;
2143 : 37 : gfc_se se;
2144 : :
2145 : 37 : gfc_init_se (&se, NULL);
2146 : 37 : token = gfc_get_ultimate_alloc_ptr_comps_caf_token (&se, expr);
2147 : 37 : gcc_assert (token != NULL_TREE);
2148 : :
2149 : 37 : if (status != NULL_TREE && !integer_zerop (status))
2150 : : {
2151 : 0 : gcc_assert (TREE_TYPE (TREE_TYPE (status)) == integer_type_node);
2152 : : pstat = status;
2153 : : }
2154 : :
2155 : 37 : if (errmsg != NULL_TREE)
2156 : : {
2157 : 0 : perrmsg = errmsg;
2158 : 0 : perrlen = errmsg_len;
2159 : : }
2160 : :
2161 : 37 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_deregister, 5,
2162 : : token,
2163 : : build_int_cst (integer_type_node,
2164 : 37 : caf_dereg_type),
2165 : : pstat, perrmsg, perrlen);
2166 : 37 : gfc_add_expr_to_block (&non_null, tmp);
2167 : :
2168 : : /* It guarantees memory consistency within the same segment. */
2169 : 37 : tmp = gfc_build_string_const (strlen ("memory")+1, "memory");
2170 : 37 : tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
2171 : : gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
2172 : : tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
2173 : 37 : ASM_VOLATILE_P (tmp) = 1;
2174 : 37 : gfc_add_expr_to_block (&non_null, tmp);
2175 : :
2176 : 37 : if (status != NULL_TREE)
2177 : : {
2178 : 0 : tree stat = build_fold_indirect_ref_loc (input_location, status);
2179 : 0 : tree cond2;
2180 : :
2181 : 0 : TREE_USED (label_finish) = 1;
2182 : 0 : tmp = build1_v (GOTO_EXPR, label_finish);
2183 : 0 : cond2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
2184 : 0 : stat, build_zero_cst (TREE_TYPE (stat)));
2185 : 0 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
2186 : : gfc_unlikely (cond2, PRED_FORTRAN_REALLOC),
2187 : : tmp, build_empty_stmt (input_location));
2188 : 0 : gfc_add_expr_to_block (&non_null, tmp);
2189 : : }
2190 : : }
2191 : :
2192 : 4958 : return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
2193 : : gfc_finish_block (&null),
2194 : 4958 : gfc_finish_block (&non_null));
2195 : : }
2196 : :
2197 : : /* Reallocate MEM so it has SIZE bytes of data. This behaves like the
2198 : : following pseudo-code:
2199 : :
2200 : : void *
2201 : : internal_realloc (void *mem, size_t size)
2202 : : {
2203 : : res = realloc (mem, size);
2204 : : if (!res && size != 0)
2205 : : _gfortran_os_error ("Allocation would exceed memory limit");
2206 : :
2207 : : return res;
2208 : : } */
2209 : : tree
2210 : 1179 : gfc_call_realloc (stmtblock_t * block, tree mem, tree size)
2211 : : {
2212 : 1179 : tree res, nonzero, null_result, tmp;
2213 : 1179 : tree type = TREE_TYPE (mem);
2214 : :
2215 : : /* Only evaluate the size once. */
2216 : 1179 : size = save_expr (fold_convert (size_type_node, size));
2217 : :
2218 : : /* Create a variable to hold the result. */
2219 : 1179 : res = gfc_create_var (type, NULL);
2220 : :
2221 : : /* Call realloc and check the result. */
2222 : 1179 : tmp = build_call_expr_loc (input_location,
2223 : : builtin_decl_explicit (BUILT_IN_REALLOC), 2,
2224 : : fold_convert (pvoid_type_node, mem), size);
2225 : 1179 : gfc_add_modify (block, res, fold_convert (type, tmp));
2226 : 1179 : null_result = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
2227 : : res, build_int_cst (pvoid_type_node, 0));
2228 : 1179 : nonzero = fold_build2_loc (input_location, NE_EXPR, logical_type_node, size,
2229 : : build_int_cst (size_type_node, 0));
2230 : 1179 : null_result = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
2231 : : null_result, nonzero);
2232 : 1179 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
2233 : : null_result,
2234 : : trans_os_error_at (NULL,
2235 : : "Error reallocating to %lu bytes",
2236 : : fold_convert
2237 : : (long_unsigned_type_node, size)),
2238 : : build_empty_stmt (input_location));
2239 : 1179 : gfc_add_expr_to_block (block, tmp);
2240 : :
2241 : 1179 : return res;
2242 : : }
2243 : :
2244 : :
2245 : : /* Add an expression to another one, either at the front or the back. */
2246 : :
2247 : : static void
2248 : 18136806 : add_expr_to_chain (tree* chain, tree expr, bool front)
2249 : : {
2250 : 18136806 : if (expr == NULL_TREE || IS_EMPTY_STMT (expr))
2251 : 8582325 : return;
2252 : :
2253 : 9554481 : if (*chain)
2254 : : {
2255 : 5138599 : if (TREE_CODE (*chain) != STATEMENT_LIST)
2256 : : {
2257 : 1458112 : tree tmp;
2258 : :
2259 : 1458112 : tmp = *chain;
2260 : 1458112 : *chain = NULL_TREE;
2261 : 1458112 : append_to_statement_list (tmp, chain);
2262 : : }
2263 : :
2264 : 5138599 : if (front)
2265 : : {
2266 : 23979 : tree_stmt_iterator i;
2267 : :
2268 : 23979 : i = tsi_start (*chain);
2269 : 23979 : tsi_link_before (&i, expr, TSI_CONTINUE_LINKING);
2270 : : }
2271 : : else
2272 : 5114620 : append_to_statement_list (expr, chain);
2273 : : }
2274 : : else
2275 : 4415882 : *chain = expr;
2276 : : }
2277 : :
2278 : :
2279 : : /* Add a statement at the end of a block. */
2280 : :
2281 : : void
2282 : 17338077 : gfc_add_expr_to_block (stmtblock_t * block, tree expr)
2283 : : {
2284 : 17338077 : gcc_assert (block);
2285 : 17338077 : add_expr_to_chain (&block->head, expr, false);
2286 : 17338077 : }
2287 : :
2288 : :
2289 : : /* Add a statement at the beginning of a block. */
2290 : :
2291 : : void
2292 : 3260 : gfc_prepend_expr_to_block (stmtblock_t * block, tree expr)
2293 : : {
2294 : 3260 : gcc_assert (block);
2295 : 3260 : add_expr_to_chain (&block->head, expr, true);
2296 : 3260 : }
2297 : :
2298 : :
2299 : : /* Add a block the end of a block. */
2300 : :
2301 : : void
2302 : 8787079 : gfc_add_block_to_block (stmtblock_t * block, stmtblock_t * append)
2303 : : {
2304 : 8787079 : gcc_assert (append);
2305 : 8787079 : gcc_assert (!append->has_scope);
2306 : :
2307 : 8787079 : gfc_add_expr_to_block (block, append->head);
2308 : 8787079 : append->head = NULL_TREE;
2309 : 8787079 : }
2310 : :
2311 : :
2312 : : /* Translate an executable statement. The tree cond is used by gfc_trans_do.
2313 : : This static function is wrapped by gfc_trans_code_cond and
2314 : : gfc_trans_code. */
2315 : :
2316 : : static tree
2317 : 420785 : trans_code (gfc_code * code, tree cond)
2318 : : {
2319 : 420785 : stmtblock_t block;
2320 : 420785 : tree res;
2321 : :
2322 : 420785 : if (!code)
2323 : 1978 : return build_empty_stmt (input_location);
2324 : :
2325 : 418807 : gfc_start_block (&block);
2326 : :
2327 : : /* Translate statements one by one into GENERIC trees until we reach
2328 : : the end of this gfc_code branch. */
2329 : 1540784 : for (; code; code = code->next)
2330 : : {
2331 : 1121977 : if (code->here != 0)
2332 : : {
2333 : 3502 : res = gfc_trans_label_here (code);
2334 : 3502 : gfc_add_expr_to_block (&block, res);
2335 : : }
2336 : :
2337 : 1121977 : input_location = gfc_get_location (&code->loc);
2338 : :
2339 : 1121977 : switch (code->op)
2340 : : {
2341 : : case EXEC_NOP:
2342 : : case EXEC_END_BLOCK:
2343 : : case EXEC_END_NESTED_BLOCK:
2344 : : case EXEC_END_PROCEDURE:
2345 : : res = NULL_TREE;
2346 : : break;
2347 : :
2348 : 293376 : case EXEC_ASSIGN:
2349 : 293376 : res = gfc_trans_assign (code);
2350 : 293376 : break;
2351 : :
2352 : 116 : case EXEC_LABEL_ASSIGN:
2353 : 116 : res = gfc_trans_label_assign (code);
2354 : 116 : break;
2355 : :
2356 : 9812 : case EXEC_POINTER_ASSIGN:
2357 : 9812 : res = gfc_trans_pointer_assign (code);
2358 : 9812 : break;
2359 : :
2360 : 10416 : case EXEC_INIT_ASSIGN:
2361 : 10416 : if (code->expr1->ts.type == BT_CLASS)
2362 : 380 : res = gfc_trans_class_init_assign (code);
2363 : : else
2364 : 10036 : res = gfc_trans_init_assign (code);
2365 : : break;
2366 : :
2367 : : case EXEC_CONTINUE:
2368 : : res = NULL_TREE;
2369 : : break;
2370 : :
2371 : 33 : case EXEC_CRITICAL:
2372 : 33 : res = gfc_trans_critical (code);
2373 : 33 : break;
2374 : :
2375 : 118 : case EXEC_CYCLE:
2376 : 118 : res = gfc_trans_cycle (code);
2377 : 118 : break;
2378 : :
2379 : 680 : case EXEC_EXIT:
2380 : 680 : res = gfc_trans_exit (code);
2381 : 680 : break;
2382 : :
2383 : 1187 : case EXEC_GOTO:
2384 : 1187 : res = gfc_trans_goto (code);
2385 : 1187 : break;
2386 : :
2387 : 1341 : case EXEC_ENTRY:
2388 : 1341 : res = gfc_trans_entry (code);
2389 : 1341 : break;
2390 : :
2391 : 28 : case EXEC_PAUSE:
2392 : 28 : res = gfc_trans_pause (code);
2393 : 28 : break;
2394 : :
2395 : 211340 : case EXEC_STOP:
2396 : 211340 : case EXEC_ERROR_STOP:
2397 : 211340 : res = gfc_trans_stop (code, code->op == EXEC_ERROR_STOP);
2398 : 211340 : break;
2399 : :
2400 : 79877 : case EXEC_CALL:
2401 : : /* For MVBITS we've got the special exception that we need a
2402 : : dependency check, too. */
2403 : 79877 : {
2404 : 79877 : bool is_mvbits = false;
2405 : :
2406 : 79877 : if (code->resolved_isym)
2407 : : {
2408 : 5919 : res = gfc_conv_intrinsic_subroutine (code);
2409 : 5919 : if (res != NULL_TREE)
2410 : : break;
2411 : : }
2412 : :
2413 : 75806 : if (code->resolved_isym
2414 : 1848 : && code->resolved_isym->id == GFC_ISYM_MVBITS)
2415 : 75806 : is_mvbits = true;
2416 : :
2417 : 75806 : res = gfc_trans_call (code, is_mvbits, NULL_TREE,
2418 : : NULL_TREE, false);
2419 : : }
2420 : 75806 : break;
2421 : :
2422 : 114 : case EXEC_CALL_PPC:
2423 : 114 : res = gfc_trans_call (code, false, NULL_TREE,
2424 : : NULL_TREE, false);
2425 : 114 : break;
2426 : :
2427 : 691 : case EXEC_ASSIGN_CALL:
2428 : 691 : res = gfc_trans_call (code, true, NULL_TREE,
2429 : : NULL_TREE, false);
2430 : 691 : break;
2431 : :
2432 : 3038 : case EXEC_RETURN:
2433 : 3038 : res = gfc_trans_return (code);
2434 : 3038 : break;
2435 : :
2436 : 231274 : case EXEC_IF:
2437 : 231274 : res = gfc_trans_if (code);
2438 : 231274 : break;
2439 : :
2440 : 64 : case EXEC_ARITHMETIC_IF:
2441 : 64 : res = gfc_trans_arithmetic_if (code);
2442 : 64 : break;
2443 : :
2444 : 13290 : case EXEC_BLOCK:
2445 : 13290 : res = gfc_trans_block_construct (code);
2446 : 13290 : break;
2447 : :
2448 : 26119 : case EXEC_DO:
2449 : 26119 : res = gfc_trans_do (code, cond);
2450 : 26119 : break;
2451 : :
2452 : 113 : case EXEC_DO_CONCURRENT:
2453 : 113 : res = gfc_trans_do_concurrent (code);
2454 : 113 : break;
2455 : :
2456 : 496 : case EXEC_DO_WHILE:
2457 : 496 : res = gfc_trans_do_while (code);
2458 : 496 : break;
2459 : :
2460 : 1009 : case EXEC_SELECT:
2461 : 1009 : res = gfc_trans_select (code);
2462 : 1009 : break;
2463 : :
2464 : 2821 : case EXEC_SELECT_TYPE:
2465 : 2821 : res = gfc_trans_select_type (code);
2466 : 2821 : break;
2467 : :
2468 : 1001 : case EXEC_SELECT_RANK:
2469 : 1001 : res = gfc_trans_select_rank (code);
2470 : 1001 : break;
2471 : :
2472 : 73 : case EXEC_FLUSH:
2473 : 73 : res = gfc_trans_flush (code);
2474 : 73 : break;
2475 : :
2476 : 718 : case EXEC_SYNC_ALL:
2477 : 718 : case EXEC_SYNC_IMAGES:
2478 : 718 : case EXEC_SYNC_MEMORY:
2479 : 718 : res = gfc_trans_sync (code, code->op);
2480 : 718 : break;
2481 : :
2482 : 84 : case EXEC_LOCK:
2483 : 84 : case EXEC_UNLOCK:
2484 : 84 : res = gfc_trans_lock_unlock (code, code->op);
2485 : 84 : break;
2486 : :
2487 : 39 : case EXEC_EVENT_POST:
2488 : 39 : case EXEC_EVENT_WAIT:
2489 : 39 : res = gfc_trans_event_post_wait (code, code->op);
2490 : 39 : break;
2491 : :
2492 : 3 : case EXEC_FAIL_IMAGE:
2493 : 3 : res = gfc_trans_fail_image (code);
2494 : 3 : break;
2495 : :
2496 : 1863 : case EXEC_FORALL:
2497 : 1863 : res = gfc_trans_forall (code);
2498 : 1863 : break;
2499 : :
2500 : 96 : case EXEC_FORM_TEAM:
2501 : 96 : res = gfc_trans_form_team (code);
2502 : 96 : break;
2503 : :
2504 : 51 : case EXEC_CHANGE_TEAM:
2505 : 51 : res = gfc_trans_change_team (code);
2506 : 51 : break;
2507 : :
2508 : 32 : case EXEC_END_TEAM:
2509 : 32 : res = gfc_trans_end_team (code);
2510 : 32 : break;
2511 : :
2512 : 18 : case EXEC_SYNC_TEAM:
2513 : 18 : res = gfc_trans_sync_team (code);
2514 : 18 : break;
2515 : :
2516 : 322 : case EXEC_WHERE:
2517 : 322 : res = gfc_trans_where (code);
2518 : 322 : break;
2519 : :
2520 : 13639 : case EXEC_ALLOCATE:
2521 : 13639 : res = gfc_trans_allocate (code, NULL);
2522 : 13639 : break;
2523 : :
2524 : 7942 : case EXEC_DEALLOCATE:
2525 : 7942 : res = gfc_trans_deallocate (code);
2526 : 7942 : break;
2527 : :
2528 : 3530 : case EXEC_OPEN:
2529 : 3530 : res = gfc_trans_open (code);
2530 : 3530 : break;
2531 : :
2532 : 3005 : case EXEC_CLOSE:
2533 : 3005 : res = gfc_trans_close (code);
2534 : 3005 : break;
2535 : :
2536 : 5980 : case EXEC_READ:
2537 : 5980 : res = gfc_trans_read (code);
2538 : 5980 : break;
2539 : :
2540 : 23847 : case EXEC_WRITE:
2541 : 23847 : res = gfc_trans_write (code);
2542 : 23847 : break;
2543 : :
2544 : 84 : case EXEC_IOLENGTH:
2545 : 84 : res = gfc_trans_iolength (code);
2546 : 84 : break;
2547 : :
2548 : 389 : case EXEC_BACKSPACE:
2549 : 389 : res = gfc_trans_backspace (code);
2550 : 389 : break;
2551 : :
2552 : 56 : case EXEC_ENDFILE:
2553 : 56 : res = gfc_trans_endfile (code);
2554 : 56 : break;
2555 : :
2556 : 759 : case EXEC_INQUIRE:
2557 : 759 : res = gfc_trans_inquire (code);
2558 : 759 : break;
2559 : :
2560 : 74 : case EXEC_WAIT:
2561 : 74 : res = gfc_trans_wait (code);
2562 : 74 : break;
2563 : :
2564 : 2185 : case EXEC_REWIND:
2565 : 2185 : res = gfc_trans_rewind (code);
2566 : 2185 : break;
2567 : :
2568 : 43543 : case EXEC_TRANSFER:
2569 : 43543 : res = gfc_trans_transfer (code);
2570 : 43543 : break;
2571 : :
2572 : 29911 : case EXEC_DT_END:
2573 : 29911 : res = gfc_trans_dt_end (code);
2574 : 29911 : break;
2575 : :
2576 : 18658 : case EXEC_OMP_ALLOCATE:
2577 : 18658 : case EXEC_OMP_ALLOCATORS:
2578 : 18658 : case EXEC_OMP_ASSUME:
2579 : 18658 : case EXEC_OMP_ATOMIC:
2580 : 18658 : case EXEC_OMP_BARRIER:
2581 : 18658 : case EXEC_OMP_CANCEL:
2582 : 18658 : case EXEC_OMP_CANCELLATION_POINT:
2583 : 18658 : case EXEC_OMP_CRITICAL:
2584 : 18658 : case EXEC_OMP_DEPOBJ:
2585 : 18658 : case EXEC_OMP_DISPATCH:
2586 : 18658 : case EXEC_OMP_DISTRIBUTE:
2587 : 18658 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
2588 : 18658 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
2589 : 18658 : case EXEC_OMP_DISTRIBUTE_SIMD:
2590 : 18658 : case EXEC_OMP_DO:
2591 : 18658 : case EXEC_OMP_DO_SIMD:
2592 : 18658 : case EXEC_OMP_ERROR:
2593 : 18658 : case EXEC_OMP_FLUSH:
2594 : 18658 : case EXEC_OMP_INTEROP:
2595 : 18658 : case EXEC_OMP_LOOP:
2596 : 18658 : case EXEC_OMP_MASKED:
2597 : 18658 : case EXEC_OMP_MASKED_TASKLOOP:
2598 : 18658 : case EXEC_OMP_MASKED_TASKLOOP_SIMD:
2599 : 18658 : case EXEC_OMP_MASTER:
2600 : 18658 : case EXEC_OMP_MASTER_TASKLOOP:
2601 : 18658 : case EXEC_OMP_MASTER_TASKLOOP_SIMD:
2602 : 18658 : case EXEC_OMP_METADIRECTIVE:
2603 : 18658 : case EXEC_OMP_ORDERED:
2604 : 18658 : case EXEC_OMP_PARALLEL:
2605 : 18658 : case EXEC_OMP_PARALLEL_DO:
2606 : 18658 : case EXEC_OMP_PARALLEL_DO_SIMD:
2607 : 18658 : case EXEC_OMP_PARALLEL_LOOP:
2608 : 18658 : case EXEC_OMP_PARALLEL_MASKED:
2609 : 18658 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
2610 : 18658 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
2611 : 18658 : case EXEC_OMP_PARALLEL_MASTER:
2612 : 18658 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
2613 : 18658 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
2614 : 18658 : case EXEC_OMP_PARALLEL_SECTIONS:
2615 : 18658 : case EXEC_OMP_PARALLEL_WORKSHARE:
2616 : 18658 : case EXEC_OMP_SCOPE:
2617 : 18658 : case EXEC_OMP_SECTIONS:
2618 : 18658 : case EXEC_OMP_SIMD:
2619 : 18658 : case EXEC_OMP_SINGLE:
2620 : 18658 : case EXEC_OMP_TARGET:
2621 : 18658 : case EXEC_OMP_TARGET_DATA:
2622 : 18658 : case EXEC_OMP_TARGET_ENTER_DATA:
2623 : 18658 : case EXEC_OMP_TARGET_EXIT_DATA:
2624 : 18658 : case EXEC_OMP_TARGET_PARALLEL:
2625 : 18658 : case EXEC_OMP_TARGET_PARALLEL_DO:
2626 : 18658 : case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
2627 : 18658 : case EXEC_OMP_TARGET_PARALLEL_LOOP:
2628 : 18658 : case EXEC_OMP_TARGET_SIMD:
2629 : 18658 : case EXEC_OMP_TARGET_TEAMS:
2630 : 18658 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
2631 : 18658 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2632 : 18658 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2633 : 18658 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
2634 : 18658 : case EXEC_OMP_TARGET_TEAMS_LOOP:
2635 : 18658 : case EXEC_OMP_TARGET_UPDATE:
2636 : 18658 : case EXEC_OMP_TASK:
2637 : 18658 : case EXEC_OMP_TASKGROUP:
2638 : 18658 : case EXEC_OMP_TASKLOOP:
2639 : 18658 : case EXEC_OMP_TASKLOOP_SIMD:
2640 : 18658 : case EXEC_OMP_TASKWAIT:
2641 : 18658 : case EXEC_OMP_TASKYIELD:
2642 : 18658 : case EXEC_OMP_TEAMS:
2643 : 18658 : case EXEC_OMP_TEAMS_DISTRIBUTE:
2644 : 18658 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
2645 : 18658 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2646 : 18658 : case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
2647 : 18658 : case EXEC_OMP_TEAMS_LOOP:
2648 : 18658 : case EXEC_OMP_TILE:
2649 : 18658 : case EXEC_OMP_UNROLL:
2650 : 18658 : case EXEC_OMP_WORKSHARE:
2651 : 18658 : res = gfc_trans_omp_directive (code);
2652 : 18658 : break;
2653 : :
2654 : 12020 : case EXEC_OACC_CACHE:
2655 : 12020 : case EXEC_OACC_WAIT:
2656 : 12020 : case EXEC_OACC_UPDATE:
2657 : 12020 : case EXEC_OACC_LOOP:
2658 : 12020 : case EXEC_OACC_HOST_DATA:
2659 : 12020 : case EXEC_OACC_DATA:
2660 : 12020 : case EXEC_OACC_KERNELS:
2661 : 12020 : case EXEC_OACC_KERNELS_LOOP:
2662 : 12020 : case EXEC_OACC_PARALLEL:
2663 : 12020 : case EXEC_OACC_PARALLEL_LOOP:
2664 : 12020 : case EXEC_OACC_SERIAL:
2665 : 12020 : case EXEC_OACC_SERIAL_LOOP:
2666 : 12020 : case EXEC_OACC_ENTER_DATA:
2667 : 12020 : case EXEC_OACC_EXIT_DATA:
2668 : 12020 : case EXEC_OACC_ATOMIC:
2669 : 12020 : case EXEC_OACC_DECLARE:
2670 : 12020 : res = gfc_trans_oacc_directive (code);
2671 : 12020 : break;
2672 : :
2673 : 0 : default:
2674 : 0 : gfc_internal_error ("gfc_trans_code(): Bad statement code");
2675 : : }
2676 : :
2677 : 1121977 : input_location = gfc_get_location (&code->loc);
2678 : :
2679 : 1121977 : if (res != NULL_TREE && ! IS_EMPTY_STMT (res))
2680 : : {
2681 : 1056713 : if (TREE_CODE (res) != STATEMENT_LIST)
2682 : 787141 : SET_EXPR_LOCATION (res, input_location);
2683 : :
2684 : : /* Add the new statement to the block. */
2685 : 1056713 : gfc_add_expr_to_block (&block, res);
2686 : : }
2687 : : }
2688 : :
2689 : : /* Return the finished block. */
2690 : 418807 : return gfc_finish_block (&block);
2691 : : }
2692 : :
2693 : :
2694 : : /* Translate an executable statement with condition, cond. The condition is
2695 : : used by gfc_trans_do to test for IO result conditions inside implied
2696 : : DO loops of READ and WRITE statements. See build_dt in trans-io.cc. */
2697 : :
2698 : : tree
2699 : 56030 : gfc_trans_code_cond (gfc_code * code, tree cond)
2700 : : {
2701 : 56030 : return trans_code (code, cond);
2702 : : }
2703 : :
2704 : : /* Translate an executable statement without condition. */
2705 : :
2706 : : tree
2707 : 364755 : gfc_trans_code (gfc_code * code)
2708 : : {
2709 : 364755 : return trans_code (code, NULL_TREE);
2710 : : }
2711 : :
2712 : :
2713 : : /* This function is called after a complete program unit has been parsed
2714 : : and resolved. */
2715 : :
2716 : : void
2717 : 35244 : gfc_generate_code (gfc_namespace * ns)
2718 : : {
2719 : 35244 : ompws_flags = 0;
2720 : 35244 : if (ns->is_block_data)
2721 : : {
2722 : 72 : gfc_generate_block_data (ns);
2723 : 72 : return;
2724 : : }
2725 : :
2726 : 35172 : gfc_generate_function_code (ns);
2727 : : }
2728 : :
2729 : :
2730 : : /* This function is called after a complete module has been parsed
2731 : : and resolved. */
2732 : :
2733 : : void
2734 : 8652 : gfc_generate_module_code (gfc_namespace * ns)
2735 : : {
2736 : 8652 : gfc_namespace *n;
2737 : 8652 : struct module_htab_entry *entry;
2738 : :
2739 : 8652 : gcc_assert (ns->proc_name->backend_decl == NULL);
2740 : 17304 : ns->proc_name->backend_decl
2741 : 8652 : = build_decl (gfc_get_location (&ns->proc_name->declared_at),
2742 : : NAMESPACE_DECL, get_identifier (ns->proc_name->name),
2743 : : void_type_node);
2744 : 8652 : entry = gfc_find_module (ns->proc_name->name);
2745 : 8652 : if (entry->namespace_decl)
2746 : : /* Buggy sourcecode, using a module before defining it? */
2747 : 0 : entry->decls->empty ();
2748 : 8652 : entry->namespace_decl = ns->proc_name->backend_decl;
2749 : :
2750 : 8652 : gfc_generate_module_vars (ns);
2751 : :
2752 : : /* We need to generate all module function prototypes first, to allow
2753 : : sibling calls. */
2754 : 32751 : for (n = ns->contained; n; n = n->sibling)
2755 : : {
2756 : 24099 : gfc_entry_list *el;
2757 : :
2758 : 24099 : if (!n->proc_name)
2759 : 0 : continue;
2760 : :
2761 : 24099 : gfc_create_function_decl (n, false);
2762 : 24099 : DECL_CONTEXT (n->proc_name->backend_decl) = ns->proc_name->backend_decl;
2763 : 24099 : gfc_module_add_decl (entry, n->proc_name->backend_decl);
2764 : 24099 : for (el = ns->entries; el; el = el->next)
2765 : : {
2766 : 0 : DECL_CONTEXT (el->sym->backend_decl) = ns->proc_name->backend_decl;
2767 : 0 : gfc_module_add_decl (entry, el->sym->backend_decl);
2768 : : }
2769 : : }
2770 : :
2771 : 32751 : for (n = ns->contained; n; n = n->sibling)
2772 : : {
2773 : 24099 : if (!n->proc_name)
2774 : 0 : continue;
2775 : :
2776 : 24099 : gfc_generate_function_code (n);
2777 : : }
2778 : 8652 : }
2779 : :
2780 : :
2781 : : /* Initialize an init/cleanup block with existing code. */
2782 : :
2783 : : void
2784 : 94569 : gfc_start_wrapped_block (gfc_wrapped_block* block, tree code)
2785 : : {
2786 : 94569 : gcc_assert (block);
2787 : :
2788 : 94569 : block->init = NULL_TREE;
2789 : 94569 : block->code = code;
2790 : 94569 : block->cleanup = NULL_TREE;
2791 : 94569 : }
2792 : :
2793 : :
2794 : : /* Add a new pair of initializers/clean-up code. */
2795 : :
2796 : : void
2797 : 350450 : gfc_add_init_cleanup (gfc_wrapped_block* block, tree init, tree cleanup,
2798 : : bool back)
2799 : : {
2800 : 350450 : gcc_assert (block);
2801 : :
2802 : : /* The new pair of init/cleanup should be "wrapped around" the existing
2803 : : block of code, thus the initialization is added to the front and the
2804 : : cleanup to the back. */
2805 : 350450 : add_expr_to_chain (&block->init, init, !back);
2806 : 350450 : add_expr_to_chain (&block->cleanup, cleanup, false);
2807 : 350450 : }
2808 : :
2809 : :
2810 : : /* Finish up a wrapped block by building a corresponding try-finally expr. */
2811 : :
2812 : : tree
2813 : 94569 : gfc_finish_wrapped_block (gfc_wrapped_block* block)
2814 : : {
2815 : 94569 : tree result;
2816 : :
2817 : 94569 : gcc_assert (block);
2818 : :
2819 : : /* Build the final expression. For this, just add init and body together,
2820 : : and put clean-up with that into a TRY_FINALLY_EXPR. */
2821 : 94569 : result = block->init;
2822 : 94569 : add_expr_to_chain (&result, block->code, false);
2823 : 94569 : if (block->cleanup)
2824 : 9875 : result = build2_loc (input_location, TRY_FINALLY_EXPR, void_type_node,
2825 : : result, block->cleanup);
2826 : :
2827 : : /* Clear the block. */
2828 : 94569 : block->init = NULL_TREE;
2829 : 94569 : block->code = NULL_TREE;
2830 : 94569 : block->cleanup = NULL_TREE;
2831 : :
2832 : 94569 : return result;
2833 : : }
2834 : :
2835 : :
2836 : : /* Helper function for marking a boolean expression tree as unlikely. */
2837 : :
2838 : : tree
2839 : 120996 : gfc_unlikely (tree cond, enum br_predictor predictor)
2840 : : {
2841 : 120996 : tree tmp;
2842 : :
2843 : 120996 : if (optimize)
2844 : : {
2845 : 103757 : cond = fold_convert (long_integer_type_node, cond);
2846 : 103757 : tmp = build_zero_cst (long_integer_type_node);
2847 : 103757 : cond = build_call_expr_loc (input_location,
2848 : : builtin_decl_explicit (BUILT_IN_EXPECT),
2849 : : 3, cond, tmp,
2850 : : build_int_cst (integer_type_node,
2851 : 103757 : predictor));
2852 : : }
2853 : 120996 : return cond;
2854 : : }
2855 : :
2856 : :
2857 : : /* Helper function for marking a boolean expression tree as likely. */
2858 : :
2859 : : tree
2860 : 2533 : gfc_likely (tree cond, enum br_predictor predictor)
2861 : : {
2862 : 2533 : tree tmp;
2863 : :
2864 : 2533 : if (optimize)
2865 : : {
2866 : 2214 : cond = fold_convert (long_integer_type_node, cond);
2867 : 2214 : tmp = build_one_cst (long_integer_type_node);
2868 : 2214 : cond = build_call_expr_loc (input_location,
2869 : : builtin_decl_explicit (BUILT_IN_EXPECT),
2870 : : 3, cond, tmp,
2871 : : build_int_cst (integer_type_node,
2872 : 2214 : predictor));
2873 : : }
2874 : 2533 : return cond;
2875 : : }
2876 : :
2877 : :
2878 : : /* Get the string length for a deferred character length component. */
2879 : :
2880 : : bool
2881 : 189333 : gfc_deferred_strlen (gfc_component *c, tree *decl)
2882 : : {
2883 : 189333 : char name[GFC_MAX_SYMBOL_LEN+9];
2884 : 189333 : gfc_component *strlen;
2885 : 189333 : if (!(c->ts.type == BT_CHARACTER
2886 : 11283 : && (c->ts.deferred || c->attr.pdt_string)))
2887 : : return false;
2888 : 4247 : sprintf (name, "_%s_length", c->name);
2889 : 13393 : for (strlen = c; strlen; strlen = strlen->next)
2890 : 13382 : if (strcmp (strlen->name, name) == 0)
2891 : : break;
2892 : 4247 : *decl = strlen ? strlen->backend_decl : NULL_TREE;
2893 : 4247 : return strlen != NULL;
2894 : : }
|