1 /* Code translation -- generate GCC trees from gfc_code.
2 Copyright (C) 2002-2023 Free Software Foundation, Inc.
3 Contributed by Paul Brook
5 This file is part of GCC.
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
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
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/>. */
23 #include "coretypes.h"
27 #include "gimple-expr.h" /* For create_tmp_var_raw. */
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"
37 /* Naming convention for backend interface code:
39 gfc_trans_* translate gfc_code into STMT trees.
41 gfc_conv_* expression conversion
43 gfc_get_* get a backend tree representation of a decl or type */
45 static gfc_file
*gfc_current_backend_file
;
47 const char gfc_msg_fault
[] = N_("Array reference out of bounds");
50 /* Return a location_t suitable for 'tree' for a gfortran locus. The way the
51 parser works in gfortran, loc->lb->location contains only the line number
52 and LOCATION_COLUMN is 0; hence, the column has to be added when generating
53 locations for 'tree'. Cf. error.cc's gfc_format_decoder. */
56 gfc_get_location (locus
*loc
)
58 return linemap_position_for_loc_and_offset (line_table
, loc
->lb
->location
,
59 loc
->nextc
- loc
->lb
->line
);
62 /* Advance along TREE_CHAIN n times. */
65 gfc_advance_chain (tree t
, int n
)
69 gcc_assert (t
!= NULL_TREE
);
77 #define MAX_PREFIX_LEN 20
80 create_var_debug_raw (tree type
, const char *prefix
)
82 /* Space for prefix + "_" + 10-digit-number + \0. */
83 char name_buf
[MAX_PREFIX_LEN
+ 1 + 10 + 1];
90 gcc_assert (strlen (prefix
) <= MAX_PREFIX_LEN
);
92 for (i
= 0; prefix
[i
] != 0; i
++)
93 name_buf
[i
] = gfc_wide_toupper (prefix
[i
]);
95 snprintf (name_buf
+ i
, sizeof (name_buf
) - i
, "_%d", num_var
++);
97 t
= build_decl (input_location
, VAR_DECL
, get_identifier (name_buf
), type
);
99 /* Not setting this causes some regressions. */
100 DECL_ARTIFICIAL (t
) = 1;
102 /* We want debug info for it. */
103 DECL_IGNORED_P (t
) = 0;
104 /* It should not be nameless. */
105 DECL_NAMELESS (t
) = 0;
107 /* Make the variable writable. */
108 TREE_READONLY (t
) = 0;
110 DECL_EXTERNAL (t
) = 0;
117 /* Creates a variable declaration with a given TYPE. */
120 gfc_create_var_np (tree type
, const char *prefix
)
124 if (flag_debug_aux_vars
)
125 return create_var_debug_raw (type
, prefix
);
127 t
= create_tmp_var_raw (type
, prefix
);
129 /* No warnings for anonymous variables. */
131 suppress_warning (t
);
137 /* Like above, but also adds it to the current scope. */
140 gfc_create_var (tree type
, const char *prefix
)
144 tmp
= gfc_create_var_np (type
, prefix
);
152 /* If the expression is not constant, evaluate it now. We assign the
153 result of the expression to an artificially created variable VAR, and
154 return a pointer to the VAR_DECL node for this variable. */
157 gfc_evaluate_now_loc (location_t loc
, tree expr
, stmtblock_t
* pblock
)
161 if (CONSTANT_CLASS_P (expr
))
164 var
= gfc_create_var (TREE_TYPE (expr
), NULL
);
165 gfc_add_modify_loc (loc
, pblock
, var
, expr
);
172 gfc_evaluate_now (tree expr
, stmtblock_t
* pblock
)
174 return gfc_evaluate_now_loc (input_location
, expr
, pblock
);
177 /* Like gfc_evaluate_now, but add the created variable to the
181 gfc_evaluate_now_function_scope (tree expr
, stmtblock_t
* pblock
)
184 var
= gfc_create_var_np (TREE_TYPE (expr
), NULL
);
185 gfc_add_decl_to_function (var
);
186 gfc_add_modify (pblock
, var
, expr
);
191 /* Build a MODIFY_EXPR node and add it to a given statement block PBLOCK.
192 A MODIFY_EXPR is an assignment:
196 gfc_add_modify_loc (location_t loc
, stmtblock_t
* pblock
, tree lhs
, tree rhs
)
201 t1
= TREE_TYPE (rhs
);
202 t2
= TREE_TYPE (lhs
);
203 /* Make sure that the types of the rhs and the lhs are compatible
204 for scalar assignments. We should probably have something
205 similar for aggregates, but right now removing that check just
206 breaks everything. */
207 gcc_checking_assert (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
)
208 || AGGREGATE_TYPE_P (TREE_TYPE (lhs
)));
210 tmp
= fold_build2_loc (loc
, MODIFY_EXPR
, void_type_node
, lhs
,
212 gfc_add_expr_to_block (pblock
, tmp
);
217 gfc_add_modify (stmtblock_t
* pblock
, tree lhs
, tree rhs
)
219 gfc_add_modify_loc (input_location
, pblock
, lhs
, rhs
);
223 /* Create a new scope/binding level and initialize a block. Care must be
224 taken when translating expressions as any temporaries will be placed in
225 the innermost scope. */
228 gfc_start_block (stmtblock_t
* block
)
230 /* Start a new binding level. */
232 block
->has_scope
= 1;
234 /* The block is empty. */
235 block
->head
= NULL_TREE
;
239 /* Initialize a block without creating a new scope. */
242 gfc_init_block (stmtblock_t
* block
)
244 block
->head
= NULL_TREE
;
245 block
->has_scope
= 0;
249 /* Sometimes we create a scope but it turns out that we don't actually
250 need it. This function merges the scope of BLOCK with its parent.
251 Only variable decls will be merged, you still need to add the code. */
254 gfc_merge_block_scope (stmtblock_t
* block
)
259 gcc_assert (block
->has_scope
);
260 block
->has_scope
= 0;
262 /* Remember the decls in this scope. */
266 /* Add them to the parent scope. */
267 while (decl
!= NULL_TREE
)
269 next
= DECL_CHAIN (decl
);
270 DECL_CHAIN (decl
) = NULL_TREE
;
278 /* Finish a scope containing a block of statements. */
281 gfc_finish_block (stmtblock_t
* stmtblock
)
287 expr
= stmtblock
->head
;
289 expr
= build_empty_stmt (input_location
);
291 stmtblock
->head
= NULL_TREE
;
293 if (stmtblock
->has_scope
)
299 block
= poplevel (1, 0);
300 expr
= build3_v (BIND_EXPR
, decl
, expr
, block
);
310 /* Build an ADDR_EXPR and cast the result to TYPE. If TYPE is NULL, the
311 natural type is used. */
314 gfc_build_addr_expr (tree type
, tree t
)
316 tree base_type
= TREE_TYPE (t
);
319 if (type
&& POINTER_TYPE_P (type
)
320 && TREE_CODE (base_type
) == ARRAY_TYPE
321 && TYPE_MAIN_VARIANT (TREE_TYPE (type
))
322 == TYPE_MAIN_VARIANT (TREE_TYPE (base_type
)))
324 tree min_val
= size_zero_node
;
325 tree type_domain
= TYPE_DOMAIN (base_type
);
326 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
327 min_val
= TYPE_MIN_VALUE (type_domain
);
328 t
= fold (build4_loc (input_location
, ARRAY_REF
, TREE_TYPE (type
),
329 t
, min_val
, NULL_TREE
, NULL_TREE
));
333 natural_type
= build_pointer_type (base_type
);
335 if (TREE_CODE (t
) == INDIRECT_REF
)
339 t
= TREE_OPERAND (t
, 0);
340 natural_type
= TREE_TYPE (t
);
344 tree base
= get_base_address (t
);
345 if (base
&& DECL_P (base
))
346 TREE_ADDRESSABLE (base
) = 1;
347 t
= fold_build1_loc (input_location
, ADDR_EXPR
, natural_type
, t
);
350 if (type
&& natural_type
!= type
)
351 t
= convert (type
, t
);
358 get_array_span (tree type
, tree decl
)
362 /* Component references are guaranteed to have a reliable value for
363 'span'. Likewise indirect references since they emerge from the
364 conversion of a CFI descriptor or the hidden dummy descriptor. */
365 if (TREE_CODE (decl
) == COMPONENT_REF
366 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl
)))
367 return gfc_conv_descriptor_span_get (decl
);
368 else if (TREE_CODE (decl
) == INDIRECT_REF
369 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl
)))
370 return gfc_conv_descriptor_span_get (decl
);
372 /* Return the span for deferred character length array references. */
373 if (type
&& TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_STRING_FLAG (type
))
375 if (TREE_CODE (decl
) == PARM_DECL
)
376 decl
= build_fold_indirect_ref_loc (input_location
, decl
);
377 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl
)))
378 span
= gfc_conv_descriptor_span_get (decl
);
380 span
= gfc_get_character_len_in_bytes (type
);
381 span
= (span
&& !integer_zerop (span
))
382 ? (fold_convert (gfc_array_index_type
, span
)) : (NULL_TREE
);
384 /* Likewise for class array or pointer array references. */
385 else if (TREE_CODE (decl
) == FIELD_DECL
386 || VAR_OR_FUNCTION_DECL_P (decl
)
387 || TREE_CODE (decl
) == PARM_DECL
)
389 if (GFC_DECL_CLASS (decl
))
391 /* When a temporary is in place for the class array, then the
392 original class' declaration is stored in the saved
394 if (DECL_LANG_SPECIFIC (decl
) && GFC_DECL_SAVED_DESCRIPTOR (decl
))
395 decl
= GFC_DECL_SAVED_DESCRIPTOR (decl
);
398 /* Allow for dummy arguments and other good things. */
399 if (POINTER_TYPE_P (TREE_TYPE (decl
)))
400 decl
= build_fold_indirect_ref_loc (input_location
, decl
);
402 /* Check if '_data' is an array descriptor. If it is not,
403 the array must be one of the components of the class
404 object, so return a null span. */
405 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (
406 gfc_class_data_get (decl
))))
409 span
= gfc_class_vtab_size_get (decl
);
410 /* For unlimited polymorphic entities then _len component needs
411 to be multiplied with the size. */
412 span
= gfc_resize_class_size_with_len (NULL
, decl
, span
);
414 else if (GFC_DECL_PTR_ARRAY_P (decl
))
416 if (TREE_CODE (decl
) == PARM_DECL
)
417 decl
= build_fold_indirect_ref_loc (input_location
, decl
);
418 span
= gfc_conv_descriptor_span_get (decl
);
431 gfc_build_spanned_array_ref (tree base
, tree offset
, tree span
)
435 type
= TREE_TYPE (TREE_TYPE (base
));
436 offset
= fold_build2_loc (input_location
, MULT_EXPR
,
437 gfc_array_index_type
,
439 tmp
= gfc_build_addr_expr (pvoid_type_node
, base
);
440 tmp
= fold_build_pointer_plus_loc (input_location
, tmp
, offset
);
441 tmp
= fold_convert (build_pointer_type (type
), tmp
);
442 if ((TREE_CODE (type
) != INTEGER_TYPE
&& TREE_CODE (type
) != ARRAY_TYPE
)
443 || !TYPE_STRING_FLAG (type
))
444 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
449 /* Build an ARRAY_REF with its natural type.
450 NON_NEGATIVE_OFFSET indicates if it’s true that OFFSET can’t be negative,
451 and thus that an ARRAY_REF can safely be generated. If it’s false, we
452 have to play it safe and use pointer arithmetic. */
455 gfc_build_array_ref (tree base
, tree offset
, tree decl
,
456 bool non_negative_offset
, tree vptr
)
458 tree type
= TREE_TYPE (base
);
459 tree span
= NULL_TREE
;
461 if (GFC_ARRAY_TYPE_P (type
) && GFC_TYPE_ARRAY_RANK (type
) == 0)
463 gcc_assert (GFC_TYPE_ARRAY_CORANK (type
) > 0);
465 return fold_convert (TYPE_MAIN_VARIANT (type
), base
);
468 /* Scalar coarray, there is nothing to do. */
469 if (TREE_CODE (type
) != ARRAY_TYPE
)
471 gcc_assert (decl
== NULL_TREE
);
472 gcc_assert (integer_zerop (offset
));
476 type
= TREE_TYPE (type
);
479 TREE_ADDRESSABLE (base
) = 1;
481 /* Strip NON_LVALUE_EXPR nodes. */
482 STRIP_TYPE_NOPS (offset
);
484 /* If decl or vptr are non-null, pointer arithmetic for the array reference
485 is likely. Generate the 'span' for the array reference. */
488 span
= gfc_vptr_size_get (vptr
);
490 /* Check if this is an unlimited polymorphic object carrying a character
491 payload. In this case, the 'len' field is non-zero. */
492 if (decl
&& GFC_CLASS_TYPE_P (TREE_TYPE (decl
)))
493 span
= gfc_resize_class_size_with_len (NULL
, decl
, span
);
496 span
= get_array_span (type
, decl
);
498 /* If a non-null span has been generated reference the element with
499 pointer arithmetic. */
500 if (span
!= NULL_TREE
)
501 return gfc_build_spanned_array_ref (base
, offset
, span
);
502 /* Else use a straightforward array reference if possible. */
503 else if (non_negative_offset
)
504 return build4_loc (input_location
, ARRAY_REF
, type
, base
, offset
,
505 NULL_TREE
, NULL_TREE
);
506 /* Otherwise use pointer arithmetic. */
509 gcc_assert (TREE_CODE (TREE_TYPE (base
)) == ARRAY_TYPE
);
510 tree min
= NULL_TREE
;
511 if (TYPE_DOMAIN (TREE_TYPE (base
))
512 && !integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (base
)))))
513 min
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (base
)));
515 tree zero_based_index
516 = min
? fold_build2_loc (input_location
, MINUS_EXPR
,
517 gfc_array_index_type
,
518 fold_convert (gfc_array_index_type
, offset
),
519 fold_convert (gfc_array_index_type
, min
))
520 : fold_convert (gfc_array_index_type
, offset
);
522 tree elt_size
= fold_convert (gfc_array_index_type
,
523 TYPE_SIZE_UNIT (type
));
525 tree offset_bytes
= fold_build2_loc (input_location
, MULT_EXPR
,
526 gfc_array_index_type
,
527 zero_based_index
, elt_size
);
529 tree base_addr
= gfc_build_addr_expr (pvoid_type_node
, base
);
531 tree ptr
= fold_build_pointer_plus_loc (input_location
, base_addr
,
533 return build1_loc (input_location
, INDIRECT_REF
, type
,
534 fold_convert (build_pointer_type (type
), ptr
));
539 /* Generate a call to print a runtime error possibly including multiple
540 arguments and a locus. */
543 trans_runtime_error_vararg (tree errorfunc
, locus
* where
, const char* msgid
,
556 /* Compute the number of extra arguments from the format string. */
557 for (p
= msgid
, nargs
= 0; *p
; p
++)
565 /* The code to generate the error. */
566 gfc_start_block (&block
);
570 line
= LOCATION_LINE (where
->lb
->location
);
571 message
= xasprintf ("At line %d of file %s", line
,
572 where
->lb
->file
->filename
);
575 message
= xasprintf ("In file '%s', around line %d",
576 gfc_source_file
, LOCATION_LINE (input_location
) + 1);
578 arg
= gfc_build_addr_expr (pchar_type_node
,
579 gfc_build_localized_cstring_const (message
));
582 message
= xasprintf ("%s", _(msgid
));
583 arg2
= gfc_build_addr_expr (pchar_type_node
,
584 gfc_build_localized_cstring_const (message
));
587 /* Build the argument array. */
588 argarray
= XALLOCAVEC (tree
, nargs
+ 2);
591 for (i
= 0; i
< nargs
; i
++)
592 argarray
[2 + i
] = va_arg (ap
, tree
);
594 /* Build the function call to runtime_(warning,error)_at; because of the
595 variable number of arguments, we can't use build_call_expr_loc dinput_location,
597 fntype
= TREE_TYPE (errorfunc
);
599 loc
= where
? gfc_get_location (where
) : input_location
;
600 tmp
= fold_build_call_array_loc (loc
, TREE_TYPE (fntype
),
601 fold_build1_loc (loc
, ADDR_EXPR
,
602 build_pointer_type (fntype
),
604 nargs
+ 2, argarray
);
605 gfc_add_expr_to_block (&block
, tmp
);
607 return gfc_finish_block (&block
);
612 gfc_trans_runtime_error (bool error
, locus
* where
, const char* msgid
, ...)
617 va_start (ap
, msgid
);
618 result
= trans_runtime_error_vararg (error
619 ? gfor_fndecl_runtime_error_at
620 : gfor_fndecl_runtime_warning_at
,
627 /* Generate a runtime error if COND is true. */
630 gfc_trans_runtime_check (bool error
, bool once
, tree cond
, stmtblock_t
* pblock
,
631 locus
* where
, const char * msgid
, ...)
639 if (integer_zerop (cond
))
644 tmpvar
= gfc_create_var (boolean_type_node
, "print_warning");
645 TREE_STATIC (tmpvar
) = 1;
646 DECL_INITIAL (tmpvar
) = boolean_true_node
;
647 gfc_add_expr_to_block (pblock
, tmpvar
);
650 gfc_start_block (&block
);
652 /* For error, runtime_error_at already implies PRED_NORETURN. */
654 gfc_add_expr_to_block (&block
, build_predict_expr (PRED_FORTRAN_WARN_ONCE
,
657 /* The code to generate the error. */
658 va_start (ap
, msgid
);
659 gfc_add_expr_to_block (&block
,
660 trans_runtime_error_vararg
661 (error
? gfor_fndecl_runtime_error_at
662 : gfor_fndecl_runtime_warning_at
,
667 gfc_add_modify (&block
, tmpvar
, boolean_false_node
);
669 body
= gfc_finish_block (&block
);
671 if (integer_onep (cond
))
673 gfc_add_expr_to_block (pblock
, body
);
678 cond
= fold_build2_loc (gfc_get_location (where
), TRUTH_AND_EXPR
,
679 boolean_type_node
, tmpvar
,
680 fold_convert (boolean_type_node
, cond
));
682 tmp
= fold_build3_loc (gfc_get_location (where
), COND_EXPR
, void_type_node
,
684 build_empty_stmt (gfc_get_location (where
)));
685 gfc_add_expr_to_block (pblock
, tmp
);
691 trans_os_error_at (locus
* where
, const char* msgid
, ...)
696 va_start (ap
, msgid
);
697 result
= trans_runtime_error_vararg (gfor_fndecl_os_error_at
,
705 /* Call malloc to allocate size bytes of memory, with special conditions:
706 + if size == 0, return a malloced area of size 1,
707 + if malloc returns NULL, issue a runtime error. */
709 gfc_call_malloc (stmtblock_t
* block
, tree type
, tree size
)
711 tree tmp
, malloc_result
, null_result
, res
, malloc_tree
;
714 /* Create a variable to hold the result. */
715 res
= gfc_create_var (prvoid_type_node
, NULL
);
718 gfc_start_block (&block2
);
720 if (size
== NULL_TREE
)
721 size
= build_int_cst (size_type_node
, 1);
723 size
= fold_convert (size_type_node
, size
);
724 size
= fold_build2_loc (input_location
, MAX_EXPR
, size_type_node
, size
,
725 build_int_cst (size_type_node
, 1));
727 malloc_tree
= builtin_decl_explicit (BUILT_IN_MALLOC
);
728 gfc_add_modify (&block2
, res
,
729 fold_convert (prvoid_type_node
,
730 build_call_expr_loc (input_location
,
731 malloc_tree
, 1, size
)));
733 /* Optionally check whether malloc was successful. */
734 if (gfc_option
.rtcheck
& GFC_RTCHECK_MEM
)
736 null_result
= fold_build2_loc (input_location
, EQ_EXPR
,
737 logical_type_node
, res
,
738 build_int_cst (pvoid_type_node
, 0));
739 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
741 trans_os_error_at (NULL
,
742 "Error allocating %lu bytes",
744 (long_unsigned_type_node
,
746 build_empty_stmt (input_location
));
747 gfc_add_expr_to_block (&block2
, tmp
);
750 malloc_result
= gfc_finish_block (&block2
);
751 gfc_add_expr_to_block (block
, malloc_result
);
754 res
= fold_convert (type
, res
);
759 /* Allocate memory, using an optional status argument.
761 This function follows the following pseudo-code:
764 allocate (size_t size, integer_type stat)
771 newmem = malloc (MAX (size, 1));
775 *stat = LIBERROR_NO_MEMORY;
777 runtime_error ("Allocation would exceed memory limit");
782 gfc_allocate_using_malloc (stmtblock_t
* block
, tree pointer
,
783 tree size
, tree status
)
785 tree tmp
, error_cond
;
786 stmtblock_t on_error
;
787 tree status_type
= status
? TREE_TYPE (status
) : NULL_TREE
;
789 /* If successful and stat= is given, set status to 0. */
790 if (status
!= NULL_TREE
)
791 gfc_add_expr_to_block (block
,
792 fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
,
793 status
, build_int_cst (status_type
, 0)));
795 /* The allocation itself. */
796 size
= fold_convert (size_type_node
, size
);
797 gfc_add_modify (block
, pointer
,
798 fold_convert (TREE_TYPE (pointer
),
799 build_call_expr_loc (input_location
,
800 builtin_decl_explicit (BUILT_IN_MALLOC
), 1,
801 fold_build2_loc (input_location
,
802 MAX_EXPR
, size_type_node
, size
,
803 build_int_cst (size_type_node
, 1)))));
805 /* What to do in case of error. */
806 gfc_start_block (&on_error
);
807 if (status
!= NULL_TREE
)
809 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
, status
,
810 build_int_cst (status_type
, LIBERROR_NO_MEMORY
));
811 gfc_add_expr_to_block (&on_error
, tmp
);
815 /* Here, os_error_at already implies PRED_NORETURN. */
816 tree lusize
= fold_convert (long_unsigned_type_node
, size
);
817 tmp
= trans_os_error_at (NULL
, "Error allocating %lu bytes", lusize
);
818 gfc_add_expr_to_block (&on_error
, tmp
);
821 error_cond
= fold_build2_loc (input_location
, EQ_EXPR
,
822 logical_type_node
, pointer
,
823 build_int_cst (prvoid_type_node
, 0));
824 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
825 gfc_unlikely (error_cond
, PRED_FORTRAN_FAIL_ALLOC
),
826 gfc_finish_block (&on_error
),
827 build_empty_stmt (input_location
));
829 gfc_add_expr_to_block (block
, tmp
);
833 /* Allocate memory, using an optional status argument.
835 This function follows the following pseudo-code:
838 allocate (size_t size, void** token, int *stat, char* errmsg, int errlen)
842 newmem = _caf_register (size, regtype, token, &stat, errmsg, errlen);
846 gfc_allocate_using_caf_lib (stmtblock_t
* block
, tree pointer
, tree size
,
847 tree token
, tree status
, tree errmsg
, tree errlen
,
848 gfc_coarray_regtype alloc_type
)
852 gcc_assert (token
!= NULL_TREE
);
854 /* The allocation itself. */
855 if (status
== NULL_TREE
)
856 pstat
= null_pointer_node
;
858 pstat
= gfc_build_addr_expr (NULL_TREE
, status
);
860 if (errmsg
== NULL_TREE
)
862 gcc_assert(errlen
== NULL_TREE
);
863 errmsg
= null_pointer_node
;
864 errlen
= build_int_cst (integer_type_node
, 0);
867 size
= fold_convert (size_type_node
, size
);
868 tmp
= build_call_expr_loc (input_location
,
869 gfor_fndecl_caf_register
, 7,
870 fold_build2_loc (input_location
,
871 MAX_EXPR
, size_type_node
, size
, size_one_node
),
872 build_int_cst (integer_type_node
, alloc_type
),
873 token
, gfc_build_addr_expr (pvoid_type_node
, pointer
),
874 pstat
, errmsg
, errlen
);
876 gfc_add_expr_to_block (block
, tmp
);
878 /* It guarantees memory consistency within the same segment */
879 tmp
= gfc_build_string_const (strlen ("memory")+1, "memory"),
880 tmp
= build5_loc (input_location
, ASM_EXPR
, void_type_node
,
881 gfc_build_string_const (1, ""), NULL_TREE
, NULL_TREE
,
882 tree_cons (NULL_TREE
, tmp
, NULL_TREE
), NULL_TREE
);
883 ASM_VOLATILE_P (tmp
) = 1;
884 gfc_add_expr_to_block (block
, tmp
);
888 /* Generate code for an ALLOCATE statement when the argument is an
889 allocatable variable. If the variable is currently allocated, it is an
890 error to allocate it again.
892 This function follows the following pseudo-code:
895 allocate_allocatable (void *mem, size_t size, integer_type stat)
898 return allocate (size, stat);
902 stat = LIBERROR_ALLOCATION;
904 runtime_error ("Attempting to allocate already allocated variable");
908 expr must be set to the original expression being allocated for its locus
909 and variable name in case a runtime error has to be printed. */
911 gfc_allocate_allocatable (stmtblock_t
* block
, tree mem
, tree size
,
912 tree token
, tree status
, tree errmsg
, tree errlen
,
913 tree label_finish
, gfc_expr
* expr
, int corank
)
915 stmtblock_t alloc_block
;
916 tree tmp
, null_mem
, alloc
, error
;
917 tree type
= TREE_TYPE (mem
);
918 symbol_attribute caf_attr
;
919 bool need_assign
= false, refs_comp
= false;
920 gfc_coarray_regtype caf_alloc_type
= GFC_CAF_COARRAY_ALLOC
;
922 size
= fold_convert (size_type_node
, size
);
923 null_mem
= gfc_unlikely (fold_build2_loc (input_location
, NE_EXPR
,
924 logical_type_node
, mem
,
925 build_int_cst (type
, 0)),
926 PRED_FORTRAN_REALLOC
);
928 /* If mem is NULL, we call gfc_allocate_using_malloc or
929 gfc_allocate_using_lib. */
930 gfc_start_block (&alloc_block
);
932 if (flag_coarray
== GFC_FCOARRAY_LIB
)
933 caf_attr
= gfc_caf_attr (expr
, true, &refs_comp
);
935 if (flag_coarray
== GFC_FCOARRAY_LIB
936 && (corank
> 0 || caf_attr
.codimension
))
938 tree cond
, sub_caf_tree
;
940 bool compute_special_caf_types_size
= false;
942 if (expr
->ts
.type
== BT_DERIVED
943 && expr
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
944 && expr
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_LOCK_TYPE
)
946 compute_special_caf_types_size
= true;
947 caf_alloc_type
= GFC_CAF_LOCK_ALLOC
;
949 else if (expr
->ts
.type
== BT_DERIVED
950 && expr
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
951 && expr
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_EVENT_TYPE
)
953 compute_special_caf_types_size
= true;
954 caf_alloc_type
= GFC_CAF_EVENT_ALLOC
;
956 else if (!caf_attr
.coarray_comp
&& refs_comp
)
957 /* Only allocatable components in a derived type coarray can be
959 caf_alloc_type
= GFC_CAF_COARRAY_ALLOC_ALLOCATE_ONLY
;
961 gfc_init_se (&se
, NULL
);
962 sub_caf_tree
= gfc_get_ultimate_alloc_ptr_comps_caf_token (&se
, expr
);
963 if (sub_caf_tree
== NULL_TREE
)
964 sub_caf_tree
= token
;
966 /* When mem is an array ref, then strip the .data-ref. */
967 if (TREE_CODE (mem
) == COMPONENT_REF
968 && !(GFC_ARRAY_TYPE_P (TREE_TYPE (mem
))))
969 tmp
= TREE_OPERAND (mem
, 0);
973 if (!(GFC_ARRAY_TYPE_P (TREE_TYPE (tmp
))
974 && TYPE_LANG_SPECIFIC (TREE_TYPE (tmp
))->corank
== 0)
975 && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp
)))
977 symbol_attribute attr
;
979 gfc_clear_attr (&attr
);
980 tmp
= gfc_conv_scalar_to_descriptor (&se
, mem
, attr
);
983 gfc_add_block_to_block (&alloc_block
, &se
.pre
);
985 /* In the front end, we represent the lock variable as pointer. However,
986 the FE only passes the pointer around and leaves the actual
987 representation to the library. Hence, we have to convert back to the
988 number of elements. */
989 if (compute_special_caf_types_size
)
990 size
= fold_build2_loc (input_location
, TRUNC_DIV_EXPR
, size_type_node
,
991 size
, TYPE_SIZE_UNIT (ptr_type_node
));
993 gfc_allocate_using_caf_lib (&alloc_block
, tmp
, size
, sub_caf_tree
,
994 status
, errmsg
, errlen
, caf_alloc_type
);
996 gfc_add_modify (&alloc_block
, mem
, fold_convert (TREE_TYPE (mem
),
997 gfc_conv_descriptor_data_get (tmp
)));
998 if (status
!= NULL_TREE
)
1000 TREE_USED (label_finish
) = 1;
1001 tmp
= build1_v (GOTO_EXPR
, label_finish
);
1002 cond
= fold_build2_loc (input_location
, NE_EXPR
, logical_type_node
,
1003 status
, build_zero_cst (TREE_TYPE (status
)));
1004 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
1005 gfc_unlikely (cond
, PRED_FORTRAN_FAIL_ALLOC
),
1006 tmp
, build_empty_stmt (input_location
));
1007 gfc_add_expr_to_block (&alloc_block
, tmp
);
1011 gfc_allocate_using_malloc (&alloc_block
, mem
, size
, status
);
1013 alloc
= gfc_finish_block (&alloc_block
);
1015 /* If mem is not NULL, we issue a runtime error or set the
1021 gcc_assert (expr
->expr_type
== EXPR_VARIABLE
&& expr
->symtree
);
1022 varname
= gfc_build_cstring_const (expr
->symtree
->name
);
1023 varname
= gfc_build_addr_expr (pchar_type_node
, varname
);
1025 error
= gfc_trans_runtime_error (true, &expr
->where
,
1026 "Attempting to allocate already"
1027 " allocated variable '%s'",
1031 error
= gfc_trans_runtime_error (true, NULL
,
1032 "Attempting to allocate already allocated"
1035 if (status
!= NULL_TREE
)
1037 tree status_type
= TREE_TYPE (status
);
1039 error
= fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
,
1040 status
, build_int_cst (status_type
, LIBERROR_ALLOCATION
));
1043 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, null_mem
,
1045 gfc_add_expr_to_block (block
, tmp
);
1049 /* Free a given variable. */
1052 gfc_call_free (tree var
)
1054 return build_call_expr_loc (input_location
,
1055 builtin_decl_explicit (BUILT_IN_FREE
),
1056 1, fold_convert (pvoid_type_node
, var
));
1060 /* Build a call to a FINAL procedure, which finalizes "var". */
1063 gfc_build_final_call (gfc_typespec ts
, gfc_expr
*final_wrapper
, gfc_expr
*var
,
1064 bool fini_coarray
, gfc_expr
*class_size
)
1068 tree final_fndecl
, array
, size
, tmp
;
1069 symbol_attribute attr
;
1071 gcc_assert (final_wrapper
->expr_type
== EXPR_VARIABLE
);
1074 gfc_start_block (&block
);
1075 gfc_init_se (&se
, NULL
);
1076 gfc_conv_expr (&se
, final_wrapper
);
1077 final_fndecl
= se
.expr
;
1078 if (POINTER_TYPE_P (TREE_TYPE (final_fndecl
)))
1079 final_fndecl
= build_fold_indirect_ref_loc (input_location
, final_fndecl
);
1081 if (ts
.type
== BT_DERIVED
)
1085 gcc_assert (!class_size
);
1086 elem_size
= gfc_typenode_for_spec (&ts
);
1087 elem_size
= TYPE_SIZE_UNIT (elem_size
);
1088 size
= fold_convert (gfc_array_index_type
, elem_size
);
1090 gfc_init_se (&se
, NULL
);
1091 se
.want_pointer
= 1;
1094 se
.descriptor_only
= 1;
1095 gfc_conv_expr_descriptor (&se
, var
);
1100 gfc_conv_expr (&se
, var
);
1101 // gcc_assert (se.pre.head == NULL_TREE && se.post.head == NULL_TREE);
1104 /* No copy back needed, hence set attr's allocatable/pointer
1106 gfc_clear_attr (&attr
);
1107 gfc_init_se (&se
, NULL
);
1108 array
= gfc_conv_scalar_to_descriptor (&se
, array
, attr
);
1109 gcc_assert (se
.post
.head
== NULL_TREE
);
1114 gfc_expr
*array_expr
;
1115 gcc_assert (class_size
);
1116 gfc_init_se (&se
, NULL
);
1117 gfc_conv_expr (&se
, class_size
);
1118 gfc_add_block_to_block (&block
, &se
.pre
);
1119 gcc_assert (se
.post
.head
== NULL_TREE
);
1122 array_expr
= gfc_copy_expr (var
);
1123 gfc_init_se (&se
, NULL
);
1124 se
.want_pointer
= 1;
1125 if (array_expr
->rank
)
1127 gfc_add_class_array_ref (array_expr
);
1128 se
.descriptor_only
= 1;
1129 gfc_conv_expr_descriptor (&se
, array_expr
);
1134 gfc_add_data_component (array_expr
);
1135 gfc_conv_expr (&se
, array_expr
);
1136 gfc_add_block_to_block (&block
, &se
.pre
);
1137 gcc_assert (se
.post
.head
== NULL_TREE
);
1140 if (!gfc_is_coarray (array_expr
))
1142 /* No copy back needed, hence set attr's allocatable/pointer
1144 gfc_clear_attr (&attr
);
1145 gfc_init_se (&se
, NULL
);
1146 array
= gfc_conv_scalar_to_descriptor (&se
, array
, attr
);
1148 gcc_assert (se
.post
.head
== NULL_TREE
);
1150 gfc_free_expr (array_expr
);
1153 if (!POINTER_TYPE_P (TREE_TYPE (array
)))
1154 array
= gfc_build_addr_expr (NULL
, array
);
1156 gfc_add_block_to_block (&block
, &se
.pre
);
1157 tmp
= build_call_expr_loc (input_location
,
1158 final_fndecl
, 3, array
,
1159 size
, fini_coarray
? boolean_true_node
1160 : boolean_false_node
);
1161 gfc_add_block_to_block (&block
, &se
.post
);
1162 gfc_add_expr_to_block (&block
, tmp
);
1163 return gfc_finish_block (&block
);
1168 gfc_add_comp_finalizer_call (stmtblock_t
*block
, tree decl
, gfc_component
*comp
,
1173 tree final_fndecl
, size
, array
, tmp
, cond
;
1174 symbol_attribute attr
;
1175 gfc_expr
*final_expr
= NULL
;
1177 if (comp
->ts
.type
!= BT_DERIVED
&& comp
->ts
.type
!= BT_CLASS
)
1180 gfc_init_block (&block2
);
1182 if (comp
->ts
.type
== BT_DERIVED
)
1184 if (comp
->attr
.pointer
)
1187 gfc_is_finalizable (comp
->ts
.u
.derived
, &final_expr
);
1191 gfc_init_se (&se
, NULL
);
1192 gfc_conv_expr (&se
, final_expr
);
1193 final_fndecl
= se
.expr
;
1194 size
= gfc_typenode_for_spec (&comp
->ts
);
1195 size
= TYPE_SIZE_UNIT (size
);
1196 size
= fold_convert (gfc_array_index_type
, size
);
1200 else /* comp->ts.type == BT_CLASS. */
1202 if (CLASS_DATA (comp
)->attr
.class_pointer
)
1205 gfc_is_finalizable (CLASS_DATA (comp
)->ts
.u
.derived
, &final_expr
);
1206 final_fndecl
= gfc_class_vtab_final_get (decl
);
1207 size
= gfc_class_vtab_size_get (decl
);
1208 array
= gfc_class_data_get (decl
);
1211 if (comp
->attr
.allocatable
1212 || (comp
->ts
.type
== BT_CLASS
&& CLASS_DATA (comp
)->attr
.allocatable
))
1214 tmp
= GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array
))
1215 ? gfc_conv_descriptor_data_get (array
) : array
;
1216 cond
= fold_build2_loc (input_location
, NE_EXPR
, logical_type_node
,
1217 tmp
, fold_convert (TREE_TYPE (tmp
),
1218 null_pointer_node
));
1221 cond
= logical_true_node
;
1223 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array
)))
1225 gfc_clear_attr (&attr
);
1226 gfc_init_se (&se
, NULL
);
1227 array
= gfc_conv_scalar_to_descriptor (&se
, array
, attr
);
1228 gfc_add_block_to_block (&block2
, &se
.pre
);
1229 gcc_assert (se
.post
.head
== NULL_TREE
);
1232 if (!POINTER_TYPE_P (TREE_TYPE (array
)))
1233 array
= gfc_build_addr_expr (NULL
, array
);
1237 tmp
= fold_build2_loc (input_location
, NE_EXPR
, logical_type_node
,
1239 fold_convert (TREE_TYPE (final_fndecl
),
1240 null_pointer_node
));
1241 cond
= fold_build2_loc (input_location
, TRUTH_ANDIF_EXPR
,
1242 logical_type_node
, cond
, tmp
);
1245 if (POINTER_TYPE_P (TREE_TYPE (final_fndecl
)))
1246 final_fndecl
= build_fold_indirect_ref_loc (input_location
, final_fndecl
);
1248 tmp
= build_call_expr_loc (input_location
,
1249 final_fndecl
, 3, array
,
1250 size
, fini_coarray
? boolean_true_node
1251 : boolean_false_node
);
1252 gfc_add_expr_to_block (&block2
, tmp
);
1253 tmp
= gfc_finish_block (&block2
);
1255 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
, tmp
,
1256 build_empty_stmt (input_location
));
1257 gfc_add_expr_to_block (block
, tmp
);
1263 /* Add a call to the finalizer, using the passed *expr. Returns
1264 true when a finalizer call has been inserted. */
1267 gfc_add_finalizer_call (stmtblock_t
*block
, gfc_expr
*expr2
)
1272 gfc_expr
*final_expr
= NULL
;
1273 gfc_expr
*elem_size
= NULL
;
1274 bool has_finalizer
= false;
1276 if (!expr2
|| (expr2
->ts
.type
!= BT_DERIVED
&& expr2
->ts
.type
!= BT_CLASS
))
1279 /* Finalization of these temporaries is made by explicit calls in
1280 resolve.cc(generate_component_assignments). */
1281 if (expr2
->expr_type
== EXPR_VARIABLE
1282 && expr2
->symtree
->n
.sym
->name
[0] == '_'
1283 && expr2
->ts
.type
== BT_DERIVED
1284 && expr2
->ts
.u
.derived
->attr
.defined_assign_comp
)
1287 if (expr2
->ts
.type
== BT_DERIVED
)
1289 gfc_is_finalizable (expr2
->ts
.u
.derived
, &final_expr
);
1294 /* If we have a class array, we need go back to the class
1296 expr
= gfc_copy_expr (expr2
);
1298 if (expr
->ref
&& expr
->ref
->next
&& !expr
->ref
->next
->next
1299 && expr
->ref
->next
->type
== REF_ARRAY
1300 && expr
->ref
->type
== REF_COMPONENT
1301 && strcmp (expr
->ref
->u
.c
.component
->name
, "_data") == 0)
1303 gfc_free_ref_list (expr
->ref
);
1307 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
1308 if (ref
->next
&& ref
->next
->next
&& !ref
->next
->next
->next
1309 && ref
->next
->next
->type
== REF_ARRAY
1310 && ref
->next
->type
== REF_COMPONENT
1311 && strcmp (ref
->next
->u
.c
.component
->name
, "_data") == 0)
1313 gfc_free_ref_list (ref
->next
);
1317 if (expr
->ts
.type
== BT_CLASS
)
1319 has_finalizer
= gfc_is_finalizable (expr
->ts
.u
.derived
, NULL
);
1321 if (!expr2
->rank
&& !expr2
->ref
&& CLASS_DATA (expr2
->symtree
->n
.sym
)->as
)
1322 expr
->rank
= CLASS_DATA (expr2
->symtree
->n
.sym
)->as
->rank
;
1324 final_expr
= gfc_copy_expr (expr
);
1325 gfc_add_vptr_component (final_expr
);
1326 gfc_add_final_component (final_expr
);
1328 elem_size
= gfc_copy_expr (expr
);
1329 gfc_add_vptr_component (elem_size
);
1330 gfc_add_size_component (elem_size
);
1333 gcc_assert (final_expr
->expr_type
== EXPR_VARIABLE
);
1335 tmp
= gfc_build_final_call (expr
->ts
, final_expr
, expr
,
1338 if (expr
->ts
.type
== BT_CLASS
&& !has_finalizer
)
1343 gfc_init_se (&se
, NULL
);
1344 se
.want_pointer
= 1;
1345 gfc_conv_expr (&se
, final_expr
);
1346 cond
= fold_build2_loc (input_location
, NE_EXPR
, logical_type_node
,
1347 se
.expr
, build_int_cst (TREE_TYPE (se
.expr
), 0));
1349 /* For CLASS(*) not only sym->_vtab->_final can be NULL
1350 but already sym->_vtab itself. */
1351 if (UNLIMITED_POLY (expr
))
1354 gfc_expr
*vptr_expr
;
1356 vptr_expr
= gfc_copy_expr (expr
);
1357 gfc_add_vptr_component (vptr_expr
);
1359 gfc_init_se (&se
, NULL
);
1360 se
.want_pointer
= 1;
1361 gfc_conv_expr (&se
, vptr_expr
);
1362 gfc_free_expr (vptr_expr
);
1364 cond2
= fold_build2_loc (input_location
, NE_EXPR
, logical_type_node
,
1366 build_int_cst (TREE_TYPE (se
.expr
), 0));
1367 cond
= fold_build2_loc (input_location
, TRUTH_ANDIF_EXPR
,
1368 logical_type_node
, cond2
, cond
);
1371 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
1372 cond
, tmp
, build_empty_stmt (input_location
));
1375 gfc_add_expr_to_block (block
, tmp
);
1381 /* F2018 (7.5.6.3): "When an intrinsic assignment statement is executed
1382 (10.2.1.3), if the variable is not an unallocated allocatable variable,
1383 it is finalized after evaluation of expr and before the definition of
1384 the variable. If the variable is an allocated allocatable variable, or
1385 has an allocated allocatable subobject, that would be deallocated by
1386 intrinsic assignment, the finalization occurs before the deallocation */
1389 gfc_assignment_finalizer_call (gfc_se
*lse
, gfc_expr
*expr1
, bool init_flag
)
1391 symbol_attribute lhs_attr
;
1396 gfc_symbol
*sym
= expr1
->symtree
->n
.sym
;
1397 gfc_ref
*ref
= expr1
->ref
;
1398 stmtblock_t final_block
;
1399 gfc_init_block (&final_block
);
1400 gfc_expr
*finalize_expr
;
1401 bool class_array_ref
;
1403 /* We have to exclude vtable procedures (_copy and _final especially), uses
1404 of gfc_trans_assignment_1 in initialization and allocation before trying
1405 to build a final call. */
1406 if (!expr1
->must_finalize
1407 || sym
->attr
.artificial
1408 || sym
->ns
->proc_name
->attr
.artificial
1412 class_array_ref
= ref
&& ref
->type
== REF_COMPONENT
1413 && !strcmp (ref
->u
.c
.component
->name
, "_data")
1414 && ref
->next
&& ref
->next
->type
== REF_ARRAY
1415 && !ref
->next
->next
;
1417 if (class_array_ref
)
1419 finalize_expr
= gfc_lval_expr_from_sym (sym
);
1420 finalize_expr
->must_finalize
= 1;
1424 finalize_expr
= gfc_copy_expr (expr1
);
1426 /* F2018 7.5.6.2: Only finalizable entities are finalized. */
1427 if (!(expr1
->ts
.type
== BT_DERIVED
1428 && gfc_is_finalizable (expr1
->ts
.u
.derived
, NULL
))
1429 && expr1
->ts
.type
!= BT_CLASS
)
1432 if (!gfc_may_be_finalized (sym
->ts
))
1435 gfc_init_block (&final_block
);
1436 bool finalizable
= gfc_add_finalizer_call (&final_block
, finalize_expr
);
1437 gfc_free_expr (finalize_expr
);
1442 lhs_attr
= gfc_expr_attr (expr1
);
1444 /* Check allocatable/pointer is allocated/associated. */
1445 if (lhs_attr
.allocatable
|| lhs_attr
.pointer
)
1447 if (expr1
->ts
.type
== BT_CLASS
)
1449 ptr
= gfc_get_class_from_gfc_expr (expr1
);
1450 gcc_assert (ptr
!= NULL_TREE
);
1451 ptr
= gfc_class_data_get (ptr
);
1452 if (lhs_attr
.dimension
)
1453 ptr
= gfc_conv_descriptor_data_get (ptr
);
1457 gfc_init_se (&se
, NULL
);
1460 gfc_conv_expr_descriptor (&se
, expr1
);
1461 ptr
= gfc_conv_descriptor_data_get (se
.expr
);
1465 gfc_conv_expr (&se
, expr1
);
1466 ptr
= gfc_build_addr_expr (NULL_TREE
, se
.expr
);
1470 cond
= fold_build2_loc (input_location
, NE_EXPR
, logical_type_node
,
1471 ptr
, build_zero_cst (TREE_TYPE (ptr
)));
1472 final_expr
= build3_loc (input_location
, COND_EXPR
, void_type_node
,
1473 cond
, gfc_finish_block (&final_block
),
1474 build_empty_stmt (input_location
));
1477 final_expr
= gfc_finish_block (&final_block
);
1479 /* Check optional present. */
1480 if (sym
->attr
.optional
)
1482 cond
= gfc_conv_expr_present (sym
);
1483 final_expr
= build3_loc (input_location
, COND_EXPR
, void_type_node
,
1485 build_empty_stmt (input_location
));
1488 gfc_add_expr_to_block (&lse
->finalblock
, final_expr
);
1494 /* Finalize a TREE expression using the finalizer wrapper. The result is
1495 fixed in order to prevent repeated calls. */
1498 gfc_finalize_tree_expr (gfc_se
*se
, gfc_symbol
*derived
,
1499 symbol_attribute attr
, int rank
)
1501 tree vptr
, final_fndecl
, desc
, tmp
, size
, is_final
;
1502 tree data_ptr
, data_null
, cond
;
1505 bool is_class
= GFC_CLASS_TYPE_P (TREE_TYPE (se
->expr
));
1510 /* Derived type function results with components that have defined
1511 assignements are handled in resolve.cc(generate_component_assignments) */
1512 if (derived
&& (derived
->attr
.is_c_interop
1513 || derived
->attr
.is_iso_c
1514 || derived
->attr
.is_bind_c
1515 || derived
->attr
.defined_assign_comp
))
1520 if (!VAR_P (se
->expr
))
1522 desc
= gfc_evaluate_now (se
->expr
, &se
->pre
);
1525 desc
= gfc_class_data_get (se
->expr
);
1526 vptr
= gfc_class_vptr_get (se
->expr
);
1528 else if (derived
&& gfc_is_finalizable (derived
, NULL
))
1530 if (derived
->attr
.zero_comp
&& !rank
)
1532 /* Any attempt to assign zero length entities, causes the gimplifier
1533 all manner of problems. Instead, a variable is created to act as
1534 as the argument for the final call. */
1535 desc
= gfc_create_var (TREE_TYPE (se
->expr
), "zero");
1537 else if (se
->direct_byref
)
1539 desc
= gfc_evaluate_now (se
->expr
, &se
->finalblock
);
1540 if (derived
->attr
.alloc_comp
)
1542 /* Need to copy allocated components and not finalize. */
1543 tmp
= gfc_copy_alloc_comp_no_fini (derived
, se
->expr
, desc
, rank
, 0);
1544 gfc_add_expr_to_block (&se
->finalblock
, tmp
);
1549 desc
= gfc_evaluate_now (se
->expr
, &se
->pre
);
1550 se
->expr
= gfc_evaluate_now (desc
, &se
->pre
);
1551 if (derived
->attr
.alloc_comp
)
1553 /* Need to copy allocated components and not finalize. */
1554 tmp
= gfc_copy_alloc_comp_no_fini (derived
, se
->expr
, desc
, rank
, 0);
1555 gfc_add_expr_to_block (&se
->pre
, tmp
);
1559 vtab
= gfc_find_derived_vtab (derived
);
1560 if (vtab
->backend_decl
== NULL_TREE
)
1561 vptr
= gfc_get_symbol_decl (vtab
);
1563 vptr
= vtab
->backend_decl
;
1564 vptr
= gfc_build_addr_expr (NULL
, vptr
);
1569 size
= gfc_vptr_size_get (vptr
);
1570 final_fndecl
= gfc_vptr_final_get (vptr
);
1571 is_final
= fold_build2_loc (input_location
, NE_EXPR
,
1574 fold_convert (TREE_TYPE (final_fndecl
),
1575 null_pointer_node
));
1577 final_fndecl
= build_fold_indirect_ref_loc (input_location
,
1579 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc
)))
1582 desc
= gfc_conv_scalar_to_descriptor (se
, desc
, attr
);
1585 gfc_init_se (&post_se
, NULL
);
1586 desc
= gfc_conv_scalar_to_descriptor (&post_se
, desc
, attr
);
1587 gfc_add_expr_to_block (&se
->pre
, gfc_finish_block (&post_se
.pre
));
1591 if (derived
&& derived
->attr
.zero_comp
)
1593 /* All the conditions below break down for zero length derived types. */
1594 tmp
= build_call_expr_loc (input_location
, final_fndecl
, 3,
1595 gfc_build_addr_expr (NULL
, desc
),
1596 size
, boolean_false_node
);
1597 gfc_add_expr_to_block (&se
->finalblock
, tmp
);
1603 tmp
= gfc_create_var (TREE_TYPE (desc
), "res");
1604 if (se
->direct_byref
)
1605 gfc_add_modify (&se
->finalblock
, tmp
, desc
);
1607 gfc_add_modify (&se
->pre
, tmp
, desc
);
1611 data_ptr
= gfc_conv_descriptor_data_get (desc
);
1612 data_null
= fold_convert (TREE_TYPE (data_ptr
), null_pointer_node
);
1613 cond
= fold_build2_loc (input_location
, NE_EXPR
,
1614 logical_type_node
, data_ptr
, data_null
);
1615 is_final
= fold_build2_loc (input_location
, TRUTH_AND_EXPR
,
1616 logical_type_node
, is_final
, cond
);
1617 tmp
= build_call_expr_loc (input_location
, final_fndecl
, 3,
1618 gfc_build_addr_expr (NULL
, desc
),
1619 size
, boolean_false_node
);
1620 tmp
= fold_build3_loc (input_location
, COND_EXPR
,
1621 void_type_node
, is_final
, tmp
,
1622 build_empty_stmt (input_location
));
1624 if (is_class
&& se
->ss
&& se
->ss
->loop
)
1626 gfc_add_expr_to_block (&se
->loop
->post
, tmp
);
1627 tmp
= fold_build3_loc (input_location
, COND_EXPR
,
1628 void_type_node
, cond
,
1629 gfc_call_free (data_ptr
),
1630 build_empty_stmt (input_location
));
1631 gfc_add_expr_to_block (&se
->loop
->post
, tmp
);
1632 gfc_add_modify (&se
->loop
->post
, data_ptr
, data_null
);
1636 gfc_add_expr_to_block (&se
->finalblock
, tmp
);
1638 /* Let the scalarizer take care of freeing of temporary arrays. */
1639 if (attr
.allocatable
&& !(se
->loop
&& se
->loop
->temp_dim
))
1641 tmp
= fold_build3_loc (input_location
, COND_EXPR
,
1642 void_type_node
, cond
,
1643 gfc_call_free (data_ptr
),
1644 build_empty_stmt (input_location
));
1645 gfc_add_expr_to_block (&se
->finalblock
, tmp
);
1646 gfc_add_modify (&se
->finalblock
, data_ptr
, data_null
);
1652 /* User-deallocate; we emit the code directly from the front-end, and the
1653 logic is the same as the previous library function:
1656 deallocate (void *pointer, GFC_INTEGER_4 * stat)
1663 runtime_error ("Attempt to DEALLOCATE unallocated memory.");
1673 In this front-end version, status doesn't have to be GFC_INTEGER_4.
1674 Moreover, if CAN_FAIL is true, then we will not emit a runtime error,
1675 even when no status variable is passed to us (this is used for
1676 unconditional deallocation generated by the front-end at end of
1679 If a runtime-message is possible, `expr' must point to the original
1680 expression being deallocated for its locus and variable name.
1682 For coarrays, "pointer" must be the array descriptor and not its
1685 COARRAY_DEALLOC_MODE gives the mode unregister coarrays. Available modes are
1686 the ones of GFC_CAF_DEREGTYPE, -1 when the mode for deregistration is to be
1687 analyzed and set by this routine, and -2 to indicate that a non-coarray is to
1690 gfc_deallocate_with_status (tree pointer
, tree status
, tree errmsg
,
1691 tree errlen
, tree label_finish
,
1692 bool can_fail
, gfc_expr
* expr
,
1693 int coarray_dealloc_mode
, tree add_when_allocated
,
1696 stmtblock_t null
, non_null
;
1697 tree cond
, tmp
, error
;
1698 tree status_type
= NULL_TREE
;
1699 tree token
= NULL_TREE
;
1700 gfc_coarray_deregtype caf_dereg_type
= GFC_CAF_COARRAY_DEREGISTER
;
1702 if (coarray_dealloc_mode
>= GFC_CAF_COARRAY_ANALYZE
)
1704 if (flag_coarray
== GFC_FCOARRAY_LIB
)
1710 tree caf_type
, caf_decl
= pointer
;
1711 pointer
= gfc_conv_descriptor_data_get (caf_decl
);
1712 caf_type
= TREE_TYPE (caf_decl
);
1713 STRIP_NOPS (pointer
);
1714 if (GFC_DESCRIPTOR_TYPE_P (caf_type
))
1715 token
= gfc_conv_descriptor_token (caf_decl
);
1716 else if (DECL_LANG_SPECIFIC (caf_decl
)
1717 && GFC_DECL_TOKEN (caf_decl
) != NULL_TREE
)
1718 token
= GFC_DECL_TOKEN (caf_decl
);
1721 gcc_assert (GFC_ARRAY_TYPE_P (caf_type
)
1722 && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
)
1724 token
= GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
);
1728 if (coarray_dealloc_mode
== GFC_CAF_COARRAY_ANALYZE
)
1731 if (expr
&& !gfc_caf_attr (expr
, false, &comp_ref
).coarray_comp
1733 caf_dereg_type
= GFC_CAF_COARRAY_DEALLOCATE_ONLY
;
1734 // else do a deregister as set by default.
1737 caf_dereg_type
= (enum gfc_coarray_deregtype
) coarray_dealloc_mode
;
1739 else if (flag_coarray
== GFC_FCOARRAY_SINGLE
)
1740 pointer
= gfc_conv_descriptor_data_get (pointer
);
1742 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (pointer
)))
1743 pointer
= gfc_conv_descriptor_data_get (pointer
);
1745 cond
= fold_build2_loc (input_location
, EQ_EXPR
, logical_type_node
, pointer
,
1746 build_int_cst (TREE_TYPE (pointer
), 0));
1748 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
1749 we emit a runtime error. */
1750 gfc_start_block (&null
);
1755 gcc_assert (expr
&& expr
->expr_type
== EXPR_VARIABLE
&& expr
->symtree
);
1757 varname
= gfc_build_cstring_const (expr
->symtree
->name
);
1758 varname
= gfc_build_addr_expr (pchar_type_node
, varname
);
1760 error
= gfc_trans_runtime_error (true, &expr
->where
,
1761 "Attempt to DEALLOCATE unallocated '%s'",
1765 error
= build_empty_stmt (input_location
);
1767 if (status
!= NULL_TREE
&& !integer_zerop (status
))
1771 status_type
= TREE_TYPE (TREE_TYPE (status
));
1772 cond2
= fold_build2_loc (input_location
, NE_EXPR
, logical_type_node
,
1773 status
, build_int_cst (TREE_TYPE (status
), 0));
1774 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
,
1775 fold_build1_loc (input_location
, INDIRECT_REF
,
1776 status_type
, status
),
1777 build_int_cst (status_type
, 1));
1778 error
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
1782 gfc_add_expr_to_block (&null
, error
);
1784 /* When POINTER is not NULL, we free it. */
1785 gfc_start_block (&non_null
);
1786 if (add_when_allocated
)
1787 gfc_add_expr_to_block (&non_null
, add_when_allocated
);
1788 gfc_add_finalizer_call (&non_null
, expr
);
1789 if (coarray_dealloc_mode
== GFC_CAF_COARRAY_NOCOARRAY
1790 || flag_coarray
!= GFC_FCOARRAY_LIB
)
1792 tmp
= build_call_expr_loc (input_location
,
1793 builtin_decl_explicit (BUILT_IN_FREE
), 1,
1794 fold_convert (pvoid_type_node
, pointer
));
1795 gfc_add_expr_to_block (&non_null
, tmp
);
1796 gfc_add_modify (&non_null
, pointer
, build_int_cst (TREE_TYPE (pointer
),
1799 if (status
!= NULL_TREE
&& !integer_zerop (status
))
1801 /* We set STATUS to zero if it is present. */
1802 tree status_type
= TREE_TYPE (TREE_TYPE (status
));
1805 cond2
= fold_build2_loc (input_location
, NE_EXPR
, logical_type_node
,
1807 build_int_cst (TREE_TYPE (status
), 0));
1808 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
,
1809 fold_build1_loc (input_location
, INDIRECT_REF
,
1810 status_type
, status
),
1811 build_int_cst (status_type
, 0));
1812 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
1813 gfc_unlikely (cond2
, PRED_FORTRAN_FAIL_ALLOC
),
1814 tmp
, build_empty_stmt (input_location
));
1815 gfc_add_expr_to_block (&non_null
, tmp
);
1820 tree cond2
, pstat
= null_pointer_node
;
1822 if (errmsg
== NULL_TREE
)
1824 gcc_assert (errlen
== NULL_TREE
);
1825 errmsg
= null_pointer_node
;
1826 errlen
= build_zero_cst (integer_type_node
);
1830 gcc_assert (errlen
!= NULL_TREE
);
1831 if (!POINTER_TYPE_P (TREE_TYPE (errmsg
)))
1832 errmsg
= gfc_build_addr_expr (NULL_TREE
, errmsg
);
1835 if (status
!= NULL_TREE
&& !integer_zerop (status
))
1837 gcc_assert (status_type
== integer_type_node
);
1841 token
= gfc_build_addr_expr (NULL_TREE
, token
);
1842 gcc_assert (caf_dereg_type
> GFC_CAF_COARRAY_ANALYZE
);
1843 tmp
= build_call_expr_loc (input_location
,
1844 gfor_fndecl_caf_deregister
, 5,
1845 token
, build_int_cst (integer_type_node
,
1847 pstat
, errmsg
, errlen
);
1848 gfc_add_expr_to_block (&non_null
, tmp
);
1850 /* It guarantees memory consistency within the same segment */
1851 tmp
= gfc_build_string_const (strlen ("memory")+1, "memory"),
1852 tmp
= build5_loc (input_location
, ASM_EXPR
, void_type_node
,
1853 gfc_build_string_const (1, ""), NULL_TREE
, NULL_TREE
,
1854 tree_cons (NULL_TREE
, tmp
, NULL_TREE
), NULL_TREE
);
1855 ASM_VOLATILE_P (tmp
) = 1;
1856 gfc_add_expr_to_block (&non_null
, tmp
);
1858 if (status
!= NULL_TREE
)
1860 tree stat
= build_fold_indirect_ref_loc (input_location
, status
);
1861 tree nullify
= fold_build2_loc (input_location
, MODIFY_EXPR
,
1862 void_type_node
, pointer
,
1863 build_int_cst (TREE_TYPE (pointer
),
1866 TREE_USED (label_finish
) = 1;
1867 tmp
= build1_v (GOTO_EXPR
, label_finish
);
1868 cond2
= fold_build2_loc (input_location
, NE_EXPR
, logical_type_node
,
1869 stat
, build_zero_cst (TREE_TYPE (stat
)));
1870 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
1871 gfc_unlikely (cond2
, PRED_FORTRAN_REALLOC
),
1873 gfc_add_expr_to_block (&non_null
, tmp
);
1876 gfc_add_modify (&non_null
, pointer
, build_int_cst (TREE_TYPE (pointer
),
1880 return fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
,
1881 gfc_finish_block (&null
),
1882 gfc_finish_block (&non_null
));
1886 /* Generate code for deallocation of allocatable scalars (variables or
1887 components). Before the object itself is freed, any allocatable
1888 subcomponents are being deallocated. */
1891 gfc_deallocate_scalar_with_status (tree pointer
, tree status
, tree label_finish
,
1892 bool can_fail
, gfc_expr
* expr
,
1893 gfc_typespec ts
, bool coarray
)
1895 stmtblock_t null
, non_null
;
1896 tree cond
, tmp
, error
;
1897 bool finalizable
, comp_ref
;
1898 gfc_coarray_deregtype caf_dereg_type
= GFC_CAF_COARRAY_DEREGISTER
;
1900 if (coarray
&& expr
&& !gfc_caf_attr (expr
, false, &comp_ref
).coarray_comp
1902 caf_dereg_type
= GFC_CAF_COARRAY_DEALLOCATE_ONLY
;
1904 cond
= fold_build2_loc (input_location
, EQ_EXPR
, logical_type_node
, pointer
,
1905 build_int_cst (TREE_TYPE (pointer
), 0));
1907 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
1908 we emit a runtime error. */
1909 gfc_start_block (&null
);
1914 gcc_assert (expr
&& expr
->expr_type
== EXPR_VARIABLE
&& expr
->symtree
);
1916 varname
= gfc_build_cstring_const (expr
->symtree
->name
);
1917 varname
= gfc_build_addr_expr (pchar_type_node
, varname
);
1919 error
= gfc_trans_runtime_error (true, &expr
->where
,
1920 "Attempt to DEALLOCATE unallocated '%s'",
1924 error
= build_empty_stmt (input_location
);
1926 if (status
!= NULL_TREE
&& !integer_zerop (status
))
1928 tree status_type
= TREE_TYPE (TREE_TYPE (status
));
1931 cond2
= fold_build2_loc (input_location
, NE_EXPR
, logical_type_node
,
1932 status
, build_int_cst (TREE_TYPE (status
), 0));
1933 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
,
1934 fold_build1_loc (input_location
, INDIRECT_REF
,
1935 status_type
, status
),
1936 build_int_cst (status_type
, 1));
1937 error
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
1940 gfc_add_expr_to_block (&null
, error
);
1942 /* When POINTER is not NULL, we free it. */
1943 gfc_start_block (&non_null
);
1945 /* Free allocatable components. */
1946 finalizable
= gfc_add_finalizer_call (&non_null
, expr
);
1947 if (!finalizable
&& ts
.type
== BT_DERIVED
&& ts
.u
.derived
->attr
.alloc_comp
)
1949 int caf_mode
= coarray
1950 ? ((caf_dereg_type
== GFC_CAF_COARRAY_DEALLOCATE_ONLY
1951 ? GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY
: 0)
1952 | GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
1953 | GFC_STRUCTURE_CAF_MODE_IN_COARRAY
)
1955 if (coarray
&& GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (pointer
)))
1956 tmp
= gfc_conv_descriptor_data_get (pointer
);
1958 tmp
= build_fold_indirect_ref_loc (input_location
, pointer
);
1959 tmp
= gfc_deallocate_alloc_comp (ts
.u
.derived
, tmp
, 0, caf_mode
);
1960 gfc_add_expr_to_block (&non_null
, tmp
);
1963 if (!coarray
|| flag_coarray
== GFC_FCOARRAY_SINGLE
)
1965 tmp
= build_call_expr_loc (input_location
,
1966 builtin_decl_explicit (BUILT_IN_FREE
), 1,
1967 fold_convert (pvoid_type_node
, pointer
));
1968 gfc_add_expr_to_block (&non_null
, tmp
);
1970 if (status
!= NULL_TREE
&& !integer_zerop (status
))
1972 /* We set STATUS to zero if it is present. */
1973 tree status_type
= TREE_TYPE (TREE_TYPE (status
));
1976 cond2
= fold_build2_loc (input_location
, NE_EXPR
, logical_type_node
,
1978 build_int_cst (TREE_TYPE (status
), 0));
1979 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
,
1980 fold_build1_loc (input_location
, INDIRECT_REF
,
1981 status_type
, status
),
1982 build_int_cst (status_type
, 0));
1983 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
1984 cond2
, tmp
, build_empty_stmt (input_location
));
1985 gfc_add_expr_to_block (&non_null
, tmp
);
1991 tree pstat
= null_pointer_node
;
1994 gfc_init_se (&se
, NULL
);
1995 token
= gfc_get_ultimate_alloc_ptr_comps_caf_token (&se
, expr
);
1996 gcc_assert (token
!= NULL_TREE
);
1998 if (status
!= NULL_TREE
&& !integer_zerop (status
))
2000 gcc_assert (TREE_TYPE (TREE_TYPE (status
)) == integer_type_node
);
2004 tmp
= build_call_expr_loc (input_location
,
2005 gfor_fndecl_caf_deregister
, 5,
2006 token
, build_int_cst (integer_type_node
,
2008 pstat
, null_pointer_node
, integer_zero_node
);
2009 gfc_add_expr_to_block (&non_null
, tmp
);
2011 /* It guarantees memory consistency within the same segment. */
2012 tmp
= gfc_build_string_const (strlen ("memory")+1, "memory");
2013 tmp
= build5_loc (input_location
, ASM_EXPR
, void_type_node
,
2014 gfc_build_string_const (1, ""), NULL_TREE
, NULL_TREE
,
2015 tree_cons (NULL_TREE
, tmp
, NULL_TREE
), NULL_TREE
);
2016 ASM_VOLATILE_P (tmp
) = 1;
2017 gfc_add_expr_to_block (&non_null
, tmp
);
2019 if (status
!= NULL_TREE
)
2021 tree stat
= build_fold_indirect_ref_loc (input_location
, status
);
2024 TREE_USED (label_finish
) = 1;
2025 tmp
= build1_v (GOTO_EXPR
, label_finish
);
2026 cond2
= fold_build2_loc (input_location
, NE_EXPR
, logical_type_node
,
2027 stat
, build_zero_cst (TREE_TYPE (stat
)));
2028 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
2029 gfc_unlikely (cond2
, PRED_FORTRAN_REALLOC
),
2030 tmp
, build_empty_stmt (input_location
));
2031 gfc_add_expr_to_block (&non_null
, tmp
);
2035 return fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
,
2036 gfc_finish_block (&null
),
2037 gfc_finish_block (&non_null
));
2040 /* Reallocate MEM so it has SIZE bytes of data. This behaves like the
2041 following pseudo-code:
2044 internal_realloc (void *mem, size_t size)
2046 res = realloc (mem, size);
2047 if (!res && size != 0)
2048 _gfortran_os_error ("Allocation would exceed memory limit");
2053 gfc_call_realloc (stmtblock_t
* block
, tree mem
, tree size
)
2055 tree res
, nonzero
, null_result
, tmp
;
2056 tree type
= TREE_TYPE (mem
);
2058 /* Only evaluate the size once. */
2059 size
= save_expr (fold_convert (size_type_node
, size
));
2061 /* Create a variable to hold the result. */
2062 res
= gfc_create_var (type
, NULL
);
2064 /* Call realloc and check the result. */
2065 tmp
= build_call_expr_loc (input_location
,
2066 builtin_decl_explicit (BUILT_IN_REALLOC
), 2,
2067 fold_convert (pvoid_type_node
, mem
), size
);
2068 gfc_add_modify (block
, res
, fold_convert (type
, tmp
));
2069 null_result
= fold_build2_loc (input_location
, EQ_EXPR
, logical_type_node
,
2070 res
, build_int_cst (pvoid_type_node
, 0));
2071 nonzero
= fold_build2_loc (input_location
, NE_EXPR
, logical_type_node
, size
,
2072 build_int_cst (size_type_node
, 0));
2073 null_result
= fold_build2_loc (input_location
, TRUTH_AND_EXPR
, logical_type_node
,
2074 null_result
, nonzero
);
2075 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
2077 trans_os_error_at (NULL
,
2078 "Error reallocating to %lu bytes",
2080 (long_unsigned_type_node
, size
)),
2081 build_empty_stmt (input_location
));
2082 gfc_add_expr_to_block (block
, tmp
);
2088 /* Add an expression to another one, either at the front or the back. */
2091 add_expr_to_chain (tree
* chain
, tree expr
, bool front
)
2093 if (expr
== NULL_TREE
|| IS_EMPTY_STMT (expr
))
2098 if (TREE_CODE (*chain
) != STATEMENT_LIST
)
2104 append_to_statement_list (tmp
, chain
);
2109 tree_stmt_iterator i
;
2111 i
= tsi_start (*chain
);
2112 tsi_link_before (&i
, expr
, TSI_CONTINUE_LINKING
);
2115 append_to_statement_list (expr
, chain
);
2122 /* Add a statement at the end of a block. */
2125 gfc_add_expr_to_block (stmtblock_t
* block
, tree expr
)
2128 add_expr_to_chain (&block
->head
, expr
, false);
2132 /* Add a statement at the beginning of a block. */
2135 gfc_prepend_expr_to_block (stmtblock_t
* block
, tree expr
)
2138 add_expr_to_chain (&block
->head
, expr
, true);
2142 /* Add a block the end of a block. */
2145 gfc_add_block_to_block (stmtblock_t
* block
, stmtblock_t
* append
)
2147 gcc_assert (append
);
2148 gcc_assert (!append
->has_scope
);
2150 gfc_add_expr_to_block (block
, append
->head
);
2151 append
->head
= NULL_TREE
;
2155 /* Save the current locus. The structure may not be complete, and should
2156 only be used with gfc_restore_backend_locus. */
2159 gfc_save_backend_locus (locus
* loc
)
2161 loc
->lb
= XCNEW (gfc_linebuf
);
2162 loc
->lb
->location
= input_location
;
2163 loc
->lb
->file
= gfc_current_backend_file
;
2167 /* Set the current locus. */
2170 gfc_set_backend_locus (locus
* loc
)
2172 gfc_current_backend_file
= loc
->lb
->file
;
2173 input_location
= gfc_get_location (loc
);
2177 /* Restore the saved locus. Only used in conjunction with
2178 gfc_save_backend_locus, to free the memory when we are done. */
2181 gfc_restore_backend_locus (locus
* loc
)
2183 /* This only restores the information captured by gfc_save_backend_locus,
2184 intentionally does not use gfc_get_location. */
2185 input_location
= loc
->lb
->location
;
2186 gfc_current_backend_file
= loc
->lb
->file
;
2191 /* Translate an executable statement. The tree cond is used by gfc_trans_do.
2192 This static function is wrapped by gfc_trans_code_cond and
2196 trans_code (gfc_code
* code
, tree cond
)
2202 return build_empty_stmt (input_location
);
2204 gfc_start_block (&block
);
2206 /* Translate statements one by one into GENERIC trees until we reach
2207 the end of this gfc_code branch. */
2208 for (; code
; code
= code
->next
)
2210 if (code
->here
!= 0)
2212 res
= gfc_trans_label_here (code
);
2213 gfc_add_expr_to_block (&block
, res
);
2216 gfc_current_locus
= code
->loc
;
2217 gfc_set_backend_locus (&code
->loc
);
2222 case EXEC_END_BLOCK
:
2223 case EXEC_END_NESTED_BLOCK
:
2224 case EXEC_END_PROCEDURE
:
2229 res
= gfc_trans_assign (code
);
2232 case EXEC_LABEL_ASSIGN
:
2233 res
= gfc_trans_label_assign (code
);
2236 case EXEC_POINTER_ASSIGN
:
2237 res
= gfc_trans_pointer_assign (code
);
2240 case EXEC_INIT_ASSIGN
:
2241 if (code
->expr1
->ts
.type
== BT_CLASS
)
2242 res
= gfc_trans_class_init_assign (code
);
2244 res
= gfc_trans_init_assign (code
);
2252 res
= gfc_trans_critical (code
);
2256 res
= gfc_trans_cycle (code
);
2260 res
= gfc_trans_exit (code
);
2264 res
= gfc_trans_goto (code
);
2268 res
= gfc_trans_entry (code
);
2272 res
= gfc_trans_pause (code
);
2276 case EXEC_ERROR_STOP
:
2277 res
= gfc_trans_stop (code
, code
->op
== EXEC_ERROR_STOP
);
2281 /* For MVBITS we've got the special exception that we need a
2282 dependency check, too. */
2284 bool is_mvbits
= false;
2286 if (code
->resolved_isym
)
2288 res
= gfc_conv_intrinsic_subroutine (code
);
2289 if (res
!= NULL_TREE
)
2293 if (code
->resolved_isym
2294 && code
->resolved_isym
->id
== GFC_ISYM_MVBITS
)
2297 res
= gfc_trans_call (code
, is_mvbits
, NULL_TREE
,
2303 res
= gfc_trans_call (code
, false, NULL_TREE
,
2307 case EXEC_ASSIGN_CALL
:
2308 res
= gfc_trans_call (code
, true, NULL_TREE
,
2313 res
= gfc_trans_return (code
);
2317 res
= gfc_trans_if (code
);
2320 case EXEC_ARITHMETIC_IF
:
2321 res
= gfc_trans_arithmetic_if (code
);
2325 res
= gfc_trans_block_construct (code
);
2329 res
= gfc_trans_do (code
, cond
);
2332 case EXEC_DO_CONCURRENT
:
2333 res
= gfc_trans_do_concurrent (code
);
2337 res
= gfc_trans_do_while (code
);
2341 res
= gfc_trans_select (code
);
2344 case EXEC_SELECT_TYPE
:
2345 res
= gfc_trans_select_type (code
);
2348 case EXEC_SELECT_RANK
:
2349 res
= gfc_trans_select_rank (code
);
2353 res
= gfc_trans_flush (code
);
2357 case EXEC_SYNC_IMAGES
:
2358 case EXEC_SYNC_MEMORY
:
2359 res
= gfc_trans_sync (code
, code
->op
);
2364 res
= gfc_trans_lock_unlock (code
, code
->op
);
2367 case EXEC_EVENT_POST
:
2368 case EXEC_EVENT_WAIT
:
2369 res
= gfc_trans_event_post_wait (code
, code
->op
);
2372 case EXEC_FAIL_IMAGE
:
2373 res
= gfc_trans_fail_image (code
);
2377 res
= gfc_trans_forall (code
);
2380 case EXEC_FORM_TEAM
:
2381 res
= gfc_trans_form_team (code
);
2384 case EXEC_CHANGE_TEAM
:
2385 res
= gfc_trans_change_team (code
);
2389 res
= gfc_trans_end_team (code
);
2392 case EXEC_SYNC_TEAM
:
2393 res
= gfc_trans_sync_team (code
);
2397 res
= gfc_trans_where (code
);
2401 res
= gfc_trans_allocate (code
);
2404 case EXEC_DEALLOCATE
:
2405 res
= gfc_trans_deallocate (code
);
2409 res
= gfc_trans_open (code
);
2413 res
= gfc_trans_close (code
);
2417 res
= gfc_trans_read (code
);
2421 res
= gfc_trans_write (code
);
2425 res
= gfc_trans_iolength (code
);
2428 case EXEC_BACKSPACE
:
2429 res
= gfc_trans_backspace (code
);
2433 res
= gfc_trans_endfile (code
);
2437 res
= gfc_trans_inquire (code
);
2441 res
= gfc_trans_wait (code
);
2445 res
= gfc_trans_rewind (code
);
2449 res
= gfc_trans_transfer (code
);
2453 res
= gfc_trans_dt_end (code
);
2456 case EXEC_OMP_ASSUME
:
2457 case EXEC_OMP_ATOMIC
:
2458 case EXEC_OMP_BARRIER
:
2459 case EXEC_OMP_CANCEL
:
2460 case EXEC_OMP_CANCELLATION_POINT
:
2461 case EXEC_OMP_CRITICAL
:
2462 case EXEC_OMP_DEPOBJ
:
2463 case EXEC_OMP_DISTRIBUTE
:
2464 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO
:
2465 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
2466 case EXEC_OMP_DISTRIBUTE_SIMD
:
2468 case EXEC_OMP_DO_SIMD
:
2470 case EXEC_OMP_ERROR
:
2471 case EXEC_OMP_FLUSH
:
2472 case EXEC_OMP_MASKED
:
2473 case EXEC_OMP_MASKED_TASKLOOP
:
2474 case EXEC_OMP_MASKED_TASKLOOP_SIMD
:
2475 case EXEC_OMP_MASTER
:
2476 case EXEC_OMP_MASTER_TASKLOOP
:
2477 case EXEC_OMP_MASTER_TASKLOOP_SIMD
:
2478 case EXEC_OMP_ORDERED
:
2479 case EXEC_OMP_PARALLEL
:
2480 case EXEC_OMP_PARALLEL_DO
:
2481 case EXEC_OMP_PARALLEL_DO_SIMD
:
2482 case EXEC_OMP_PARALLEL_LOOP
:
2483 case EXEC_OMP_PARALLEL_MASKED
:
2484 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP
:
2485 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD
:
2486 case EXEC_OMP_PARALLEL_MASTER
:
2487 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP
:
2488 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD
:
2489 case EXEC_OMP_PARALLEL_SECTIONS
:
2490 case EXEC_OMP_PARALLEL_WORKSHARE
:
2491 case EXEC_OMP_SCOPE
:
2492 case EXEC_OMP_SECTIONS
:
2494 case EXEC_OMP_SINGLE
:
2495 case EXEC_OMP_TARGET
:
2496 case EXEC_OMP_TARGET_DATA
:
2497 case EXEC_OMP_TARGET_ENTER_DATA
:
2498 case EXEC_OMP_TARGET_EXIT_DATA
:
2499 case EXEC_OMP_TARGET_PARALLEL
:
2500 case EXEC_OMP_TARGET_PARALLEL_DO
:
2501 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD
:
2502 case EXEC_OMP_TARGET_PARALLEL_LOOP
:
2503 case EXEC_OMP_TARGET_SIMD
:
2504 case EXEC_OMP_TARGET_TEAMS
:
2505 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE
:
2506 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
2507 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
2508 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
2509 case EXEC_OMP_TARGET_TEAMS_LOOP
:
2510 case EXEC_OMP_TARGET_UPDATE
:
2512 case EXEC_OMP_TASKGROUP
:
2513 case EXEC_OMP_TASKLOOP
:
2514 case EXEC_OMP_TASKLOOP_SIMD
:
2515 case EXEC_OMP_TASKWAIT
:
2516 case EXEC_OMP_TASKYIELD
:
2517 case EXEC_OMP_TEAMS
:
2518 case EXEC_OMP_TEAMS_DISTRIBUTE
:
2519 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
2520 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
2521 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD
:
2522 case EXEC_OMP_TEAMS_LOOP
:
2523 case EXEC_OMP_WORKSHARE
:
2524 res
= gfc_trans_omp_directive (code
);
2527 case EXEC_OACC_CACHE
:
2528 case EXEC_OACC_WAIT
:
2529 case EXEC_OACC_UPDATE
:
2530 case EXEC_OACC_LOOP
:
2531 case EXEC_OACC_HOST_DATA
:
2532 case EXEC_OACC_DATA
:
2533 case EXEC_OACC_KERNELS
:
2534 case EXEC_OACC_KERNELS_LOOP
:
2535 case EXEC_OACC_PARALLEL
:
2536 case EXEC_OACC_PARALLEL_LOOP
:
2537 case EXEC_OACC_SERIAL
:
2538 case EXEC_OACC_SERIAL_LOOP
:
2539 case EXEC_OACC_ENTER_DATA
:
2540 case EXEC_OACC_EXIT_DATA
:
2541 case EXEC_OACC_ATOMIC
:
2542 case EXEC_OACC_DECLARE
:
2543 res
= gfc_trans_oacc_directive (code
);
2547 gfc_internal_error ("gfc_trans_code(): Bad statement code");
2550 gfc_set_backend_locus (&code
->loc
);
2552 if (res
!= NULL_TREE
&& ! IS_EMPTY_STMT (res
))
2554 if (TREE_CODE (res
) != STATEMENT_LIST
)
2555 SET_EXPR_LOCATION (res
, input_location
);
2557 /* Add the new statement to the block. */
2558 gfc_add_expr_to_block (&block
, res
);
2562 /* Return the finished block. */
2563 return gfc_finish_block (&block
);
2567 /* Translate an executable statement with condition, cond. The condition is
2568 used by gfc_trans_do to test for IO result conditions inside implied
2569 DO loops of READ and WRITE statements. See build_dt in trans-io.cc. */
2572 gfc_trans_code_cond (gfc_code
* code
, tree cond
)
2574 return trans_code (code
, cond
);
2577 /* Translate an executable statement without condition. */
2580 gfc_trans_code (gfc_code
* code
)
2582 return trans_code (code
, NULL_TREE
);
2586 /* This function is called after a complete program unit has been parsed
2590 gfc_generate_code (gfc_namespace
* ns
)
2593 if (ns
->is_block_data
)
2595 gfc_generate_block_data (ns
);
2599 gfc_generate_function_code (ns
);
2603 /* This function is called after a complete module has been parsed
2607 gfc_generate_module_code (gfc_namespace
* ns
)
2610 struct module_htab_entry
*entry
;
2612 gcc_assert (ns
->proc_name
->backend_decl
== NULL
);
2613 ns
->proc_name
->backend_decl
2614 = build_decl (gfc_get_location (&ns
->proc_name
->declared_at
),
2615 NAMESPACE_DECL
, get_identifier (ns
->proc_name
->name
),
2617 entry
= gfc_find_module (ns
->proc_name
->name
);
2618 if (entry
->namespace_decl
)
2619 /* Buggy sourcecode, using a module before defining it? */
2620 entry
->decls
->empty ();
2621 entry
->namespace_decl
= ns
->proc_name
->backend_decl
;
2623 gfc_generate_module_vars (ns
);
2625 /* We need to generate all module function prototypes first, to allow
2627 for (n
= ns
->contained
; n
; n
= n
->sibling
)
2634 gfc_create_function_decl (n
, false);
2635 DECL_CONTEXT (n
->proc_name
->backend_decl
) = ns
->proc_name
->backend_decl
;
2636 gfc_module_add_decl (entry
, n
->proc_name
->backend_decl
);
2637 for (el
= ns
->entries
; el
; el
= el
->next
)
2639 DECL_CONTEXT (el
->sym
->backend_decl
) = ns
->proc_name
->backend_decl
;
2640 gfc_module_add_decl (entry
, el
->sym
->backend_decl
);
2644 for (n
= ns
->contained
; n
; n
= n
->sibling
)
2649 gfc_generate_function_code (n
);
2654 /* Initialize an init/cleanup block with existing code. */
2657 gfc_start_wrapped_block (gfc_wrapped_block
* block
, tree code
)
2661 block
->init
= NULL_TREE
;
2663 block
->cleanup
= NULL_TREE
;
2667 /* Add a new pair of initializers/clean-up code. */
2670 gfc_add_init_cleanup (gfc_wrapped_block
* block
, tree init
, tree cleanup
)
2674 /* The new pair of init/cleanup should be "wrapped around" the existing
2675 block of code, thus the initialization is added to the front and the
2676 cleanup to the back. */
2677 add_expr_to_chain (&block
->init
, init
, true);
2678 add_expr_to_chain (&block
->cleanup
, cleanup
, false);
2682 /* Finish up a wrapped block by building a corresponding try-finally expr. */
2685 gfc_finish_wrapped_block (gfc_wrapped_block
* block
)
2691 /* Build the final expression. For this, just add init and body together,
2692 and put clean-up with that into a TRY_FINALLY_EXPR. */
2693 result
= block
->init
;
2694 add_expr_to_chain (&result
, block
->code
, false);
2696 result
= build2_loc (input_location
, TRY_FINALLY_EXPR
, void_type_node
,
2697 result
, block
->cleanup
);
2699 /* Clear the block. */
2700 block
->init
= NULL_TREE
;
2701 block
->code
= NULL_TREE
;
2702 block
->cleanup
= NULL_TREE
;
2708 /* Helper function for marking a boolean expression tree as unlikely. */
2711 gfc_unlikely (tree cond
, enum br_predictor predictor
)
2717 cond
= fold_convert (long_integer_type_node
, cond
);
2718 tmp
= build_zero_cst (long_integer_type_node
);
2719 cond
= build_call_expr_loc (input_location
,
2720 builtin_decl_explicit (BUILT_IN_EXPECT
),
2722 build_int_cst (integer_type_node
,
2729 /* Helper function for marking a boolean expression tree as likely. */
2732 gfc_likely (tree cond
, enum br_predictor predictor
)
2738 cond
= fold_convert (long_integer_type_node
, cond
);
2739 tmp
= build_one_cst (long_integer_type_node
);
2740 cond
= build_call_expr_loc (input_location
,
2741 builtin_decl_explicit (BUILT_IN_EXPECT
),
2743 build_int_cst (integer_type_node
,
2750 /* Get the string length for a deferred character length component. */
2753 gfc_deferred_strlen (gfc_component
*c
, tree
*decl
)
2755 char name
[GFC_MAX_SYMBOL_LEN
+9];
2756 gfc_component
*strlen
;
2757 if (!(c
->ts
.type
== BT_CHARACTER
2758 && (c
->ts
.deferred
|| c
->attr
.pdt_string
)))
2760 sprintf (name
, "_%s_length", c
->name
);
2761 for (strlen
= c
; strlen
; strlen
= strlen
->next
)
2762 if (strcmp (strlen
->name
, name
) == 0)
2764 *decl
= strlen
? strlen
->backend_decl
: NULL_TREE
;
2765 return strlen
!= NULL
;