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