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