1 /* Code translation -- generate GCC trees from gfc_code.
2 Copyright (C) 2002-2016 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");
48 const char gfc_msg_wrong_return
[] = N_("Incorrect function return value");
51 /* Advance along TREE_CHAIN n times. */
54 gfc_advance_chain (tree t
, int n
)
58 gcc_assert (t
!= NULL_TREE
);
65 /* Strip off a legitimate source ending from the input
66 string NAME of length LEN. */
69 remove_suffix (char *name
, int len
)
73 for (i
= 2; i
< 8 && len
> i
; i
++)
75 if (name
[len
- i
] == '.')
84 /* Creates a variable declaration with a given TYPE. */
87 gfc_create_var_np (tree type
, const char *prefix
)
91 t
= create_tmp_var_raw (type
, prefix
);
93 /* No warnings for anonymous variables. */
95 TREE_NO_WARNING (t
) = 1;
101 /* Like above, but also adds it to the current scope. */
104 gfc_create_var (tree type
, const char *prefix
)
108 tmp
= gfc_create_var_np (type
, prefix
);
116 /* If the expression is not constant, evaluate it now. We assign the
117 result of the expression to an artificially created variable VAR, and
118 return a pointer to the VAR_DECL node for this variable. */
121 gfc_evaluate_now_loc (location_t loc
, tree expr
, stmtblock_t
* pblock
)
125 if (CONSTANT_CLASS_P (expr
))
128 var
= gfc_create_var (TREE_TYPE (expr
), NULL
);
129 gfc_add_modify_loc (loc
, pblock
, var
, expr
);
136 gfc_evaluate_now (tree expr
, stmtblock_t
* pblock
)
138 return gfc_evaluate_now_loc (input_location
, expr
, pblock
);
142 /* Build a MODIFY_EXPR node and add it to a given statement block PBLOCK.
143 A MODIFY_EXPR is an assignment:
147 gfc_add_modify_loc (location_t loc
, stmtblock_t
* pblock
, tree lhs
, tree rhs
)
152 t1
= TREE_TYPE (rhs
);
153 t2
= TREE_TYPE (lhs
);
154 /* Make sure that the types of the rhs and the lhs are the same
155 for scalar assignments. We should probably have something
156 similar for aggregates, but right now removing that check just
157 breaks everything. */
158 gcc_checking_assert (t1
== t2
159 || AGGREGATE_TYPE_P (TREE_TYPE (lhs
)));
161 tmp
= fold_build2_loc (loc
, MODIFY_EXPR
, void_type_node
, lhs
,
163 gfc_add_expr_to_block (pblock
, tmp
);
168 gfc_add_modify (stmtblock_t
* pblock
, tree lhs
, tree rhs
)
170 gfc_add_modify_loc (input_location
, pblock
, lhs
, rhs
);
174 /* Create a new scope/binding level and initialize a block. Care must be
175 taken when translating expressions as any temporaries will be placed in
176 the innermost scope. */
179 gfc_start_block (stmtblock_t
* block
)
181 /* Start a new binding level. */
183 block
->has_scope
= 1;
185 /* The block is empty. */
186 block
->head
= NULL_TREE
;
190 /* Initialize a block without creating a new scope. */
193 gfc_init_block (stmtblock_t
* block
)
195 block
->head
= NULL_TREE
;
196 block
->has_scope
= 0;
200 /* Sometimes we create a scope but it turns out that we don't actually
201 need it. This function merges the scope of BLOCK with its parent.
202 Only variable decls will be merged, you still need to add the code. */
205 gfc_merge_block_scope (stmtblock_t
* block
)
210 gcc_assert (block
->has_scope
);
211 block
->has_scope
= 0;
213 /* Remember the decls in this scope. */
217 /* Add them to the parent scope. */
218 while (decl
!= NULL_TREE
)
220 next
= DECL_CHAIN (decl
);
221 DECL_CHAIN (decl
) = NULL_TREE
;
229 /* Finish a scope containing a block of statements. */
232 gfc_finish_block (stmtblock_t
* stmtblock
)
238 expr
= stmtblock
->head
;
240 expr
= build_empty_stmt (input_location
);
242 stmtblock
->head
= NULL_TREE
;
244 if (stmtblock
->has_scope
)
250 block
= poplevel (1, 0);
251 expr
= build3_v (BIND_EXPR
, decl
, expr
, block
);
261 /* Build an ADDR_EXPR and cast the result to TYPE. If TYPE is NULL, the
262 natural type is used. */
265 gfc_build_addr_expr (tree type
, tree t
)
267 tree base_type
= TREE_TYPE (t
);
270 if (type
&& POINTER_TYPE_P (type
)
271 && TREE_CODE (base_type
) == ARRAY_TYPE
272 && TYPE_MAIN_VARIANT (TREE_TYPE (type
))
273 == TYPE_MAIN_VARIANT (TREE_TYPE (base_type
)))
275 tree min_val
= size_zero_node
;
276 tree type_domain
= TYPE_DOMAIN (base_type
);
277 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
278 min_val
= TYPE_MIN_VALUE (type_domain
);
279 t
= fold (build4_loc (input_location
, ARRAY_REF
, TREE_TYPE (type
),
280 t
, min_val
, NULL_TREE
, NULL_TREE
));
284 natural_type
= build_pointer_type (base_type
);
286 if (TREE_CODE (t
) == INDIRECT_REF
)
290 t
= TREE_OPERAND (t
, 0);
291 natural_type
= TREE_TYPE (t
);
295 tree base
= get_base_address (t
);
296 if (base
&& DECL_P (base
))
297 TREE_ADDRESSABLE (base
) = 1;
298 t
= fold_build1_loc (input_location
, ADDR_EXPR
, natural_type
, t
);
301 if (type
&& natural_type
!= type
)
302 t
= convert (type
, t
);
308 /* Build an ARRAY_REF with its natural type. */
311 gfc_build_array_ref (tree base
, tree offset
, tree decl
, tree vptr
)
313 tree type
= TREE_TYPE (base
);
317 if (GFC_ARRAY_TYPE_P (type
) && GFC_TYPE_ARRAY_RANK (type
) == 0)
319 gcc_assert (GFC_TYPE_ARRAY_CORANK (type
) > 0);
321 return fold_convert (TYPE_MAIN_VARIANT (type
), base
);
324 /* Scalar coarray, there is nothing to do. */
325 if (TREE_CODE (type
) != ARRAY_TYPE
)
327 gcc_assert (decl
== NULL_TREE
);
328 gcc_assert (integer_zerop (offset
));
332 type
= TREE_TYPE (type
);
334 /* Use pointer arithmetic for deferred character length array
336 if (type
&& TREE_CODE (type
) == ARRAY_TYPE
337 && TYPE_MAXVAL (TYPE_DOMAIN (type
)) != NULL_TREE
338 && (TREE_CODE (TYPE_MAXVAL (TYPE_DOMAIN (type
))) == VAR_DECL
339 || TREE_CODE (TYPE_MAXVAL (TYPE_DOMAIN (type
))) == INDIRECT_REF
)
341 && (TREE_CODE (TYPE_MAXVAL (TYPE_DOMAIN (type
))) == INDIRECT_REF
342 || TREE_CODE (decl
) == FUNCTION_DECL
343 || DECL_CONTEXT (TYPE_MAXVAL (TYPE_DOMAIN (type
)))
344 == DECL_CONTEXT (decl
)))
345 span
= TYPE_MAXVAL (TYPE_DOMAIN (type
));
350 TREE_ADDRESSABLE (base
) = 1;
352 /* Strip NON_LVALUE_EXPR nodes. */
353 STRIP_TYPE_NOPS (offset
);
355 /* If the array reference is to a pointer, whose target contains a
356 subreference, use the span that is stored with the backend decl
357 and reference the element with pointer arithmetic. */
358 if ((decl
&& (TREE_CODE (decl
) == FIELD_DECL
359 || TREE_CODE (decl
) == VAR_DECL
360 || TREE_CODE (decl
) == PARM_DECL
361 || TREE_CODE (decl
) == FUNCTION_DECL
)
362 && ((GFC_DECL_SUBREF_ARRAY_P (decl
)
363 && !integer_zerop (GFC_DECL_SPAN (decl
)))
364 || GFC_DECL_CLASS (decl
)
365 || span
!= NULL_TREE
))
366 || vptr
!= NULL_TREE
)
370 if (GFC_DECL_CLASS (decl
))
372 /* When a temporary is in place for the class array, then the
373 original class' declaration is stored in the saved
375 if (DECL_LANG_SPECIFIC (decl
) && GFC_DECL_SAVED_DESCRIPTOR (decl
))
376 decl
= GFC_DECL_SAVED_DESCRIPTOR (decl
);
379 /* Allow for dummy arguments and other good things. */
380 if (POINTER_TYPE_P (TREE_TYPE (decl
)))
381 decl
= build_fold_indirect_ref_loc (input_location
, decl
);
383 /* Check if '_data' is an array descriptor. If it is not,
384 the array must be one of the components of the class
385 object, so return a normal array reference. */
386 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (
387 gfc_class_data_get (decl
))))
388 return build4_loc (input_location
, ARRAY_REF
, type
, base
,
389 offset
, NULL_TREE
, NULL_TREE
);
392 span
= gfc_class_vtab_size_get (decl
);
394 else if (GFC_DECL_SUBREF_ARRAY_P (decl
))
395 span
= GFC_DECL_SPAN (decl
);
397 span
= fold_convert (gfc_array_index_type
, span
);
402 span
= gfc_vptr_size_get (vptr
);
406 offset
= fold_build2_loc (input_location
, MULT_EXPR
,
407 gfc_array_index_type
,
409 tmp
= gfc_build_addr_expr (pvoid_type_node
, base
);
410 tmp
= fold_build_pointer_plus_loc (input_location
, tmp
, offset
);
411 tmp
= fold_convert (build_pointer_type (type
), tmp
);
412 if (!TYPE_STRING_FLAG (type
))
413 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
417 /* Otherwise use a straightforward array reference. */
418 return build4_loc (input_location
, ARRAY_REF
, type
, base
, offset
,
419 NULL_TREE
, NULL_TREE
);
423 /* Generate a call to print a runtime error possibly including multiple
424 arguments and a locus. */
427 trans_runtime_error_vararg (bool error
, locus
* where
, const char* msgid
,
440 /* Compute the number of extra arguments from the format string. */
441 for (p
= msgid
, nargs
= 0; *p
; p
++)
449 /* The code to generate the error. */
450 gfc_start_block (&block
);
454 line
= LOCATION_LINE (where
->lb
->location
);
455 message
= xasprintf ("At line %d of file %s", line
,
456 where
->lb
->file
->filename
);
459 message
= xasprintf ("In file '%s', around line %d",
460 gfc_source_file
, LOCATION_LINE (input_location
) + 1);
462 arg
= gfc_build_addr_expr (pchar_type_node
,
463 gfc_build_localized_cstring_const (message
));
466 message
= xasprintf ("%s", _(msgid
));
467 arg2
= gfc_build_addr_expr (pchar_type_node
,
468 gfc_build_localized_cstring_const (message
));
471 /* Build the argument array. */
472 argarray
= XALLOCAVEC (tree
, nargs
+ 2);
475 for (i
= 0; i
< nargs
; i
++)
476 argarray
[2 + i
] = va_arg (ap
, tree
);
478 /* Build the function call to runtime_(warning,error)_at; because of the
479 variable number of arguments, we can't use build_call_expr_loc dinput_location,
482 fntype
= TREE_TYPE (gfor_fndecl_runtime_error_at
);
484 fntype
= TREE_TYPE (gfor_fndecl_runtime_warning_at
);
486 loc
= where
? where
->lb
->location
: input_location
;
487 tmp
= fold_build_call_array_loc (loc
, TREE_TYPE (fntype
),
488 fold_build1_loc (loc
, ADDR_EXPR
,
489 build_pointer_type (fntype
),
491 ? gfor_fndecl_runtime_error_at
492 : gfor_fndecl_runtime_warning_at
),
493 nargs
+ 2, argarray
);
494 gfc_add_expr_to_block (&block
, tmp
);
496 return gfc_finish_block (&block
);
501 gfc_trans_runtime_error (bool error
, locus
* where
, const char* msgid
, ...)
506 va_start (ap
, msgid
);
507 result
= trans_runtime_error_vararg (error
, where
, msgid
, ap
);
513 /* Generate a runtime error if COND is true. */
516 gfc_trans_runtime_check (bool error
, bool once
, tree cond
, stmtblock_t
* pblock
,
517 locus
* where
, const char * msgid
, ...)
525 if (integer_zerop (cond
))
530 tmpvar
= gfc_create_var (boolean_type_node
, "print_warning");
531 TREE_STATIC (tmpvar
) = 1;
532 DECL_INITIAL (tmpvar
) = boolean_true_node
;
533 gfc_add_expr_to_block (pblock
, tmpvar
);
536 gfc_start_block (&block
);
538 /* For error, runtime_error_at already implies PRED_NORETURN. */
540 gfc_add_expr_to_block (&block
, build_predict_expr (PRED_FORTRAN_WARN_ONCE
,
543 /* The code to generate the error. */
544 va_start (ap
, msgid
);
545 gfc_add_expr_to_block (&block
,
546 trans_runtime_error_vararg (error
, where
,
551 gfc_add_modify (&block
, tmpvar
, boolean_false_node
);
553 body
= gfc_finish_block (&block
);
555 if (integer_onep (cond
))
557 gfc_add_expr_to_block (pblock
, body
);
562 cond
= fold_build2_loc (where
->lb
->location
, TRUTH_AND_EXPR
,
563 long_integer_type_node
, tmpvar
, cond
);
565 cond
= fold_convert (long_integer_type_node
, cond
);
567 tmp
= fold_build3_loc (where
->lb
->location
, COND_EXPR
, void_type_node
,
569 build_empty_stmt (where
->lb
->location
));
570 gfc_add_expr_to_block (pblock
, tmp
);
575 /* Call malloc to allocate size bytes of memory, with special conditions:
576 + if size == 0, return a malloced area of size 1,
577 + if malloc returns NULL, issue a runtime error. */
579 gfc_call_malloc (stmtblock_t
* block
, tree type
, tree size
)
581 tree tmp
, msg
, malloc_result
, null_result
, res
, malloc_tree
;
584 /* Create a variable to hold the result. */
585 res
= gfc_create_var (prvoid_type_node
, NULL
);
588 gfc_start_block (&block2
);
590 size
= fold_convert (size_type_node
, size
);
591 size
= fold_build2_loc (input_location
, MAX_EXPR
, size_type_node
, size
,
592 build_int_cst (size_type_node
, 1));
594 malloc_tree
= builtin_decl_explicit (BUILT_IN_MALLOC
);
595 gfc_add_modify (&block2
, res
,
596 fold_convert (prvoid_type_node
,
597 build_call_expr_loc (input_location
,
598 malloc_tree
, 1, size
)));
600 /* Optionally check whether malloc was successful. */
601 if (gfc_option
.rtcheck
& GFC_RTCHECK_MEM
)
603 null_result
= fold_build2_loc (input_location
, EQ_EXPR
,
604 boolean_type_node
, res
,
605 build_int_cst (pvoid_type_node
, 0));
606 msg
= gfc_build_addr_expr (pchar_type_node
,
607 gfc_build_localized_cstring_const ("Memory allocation failed"));
608 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
610 build_call_expr_loc (input_location
,
611 gfor_fndecl_os_error
, 1, msg
),
612 build_empty_stmt (input_location
));
613 gfc_add_expr_to_block (&block2
, tmp
);
616 malloc_result
= gfc_finish_block (&block2
);
617 gfc_add_expr_to_block (block
, malloc_result
);
620 res
= fold_convert (type
, res
);
625 /* Allocate memory, using an optional status argument.
627 This function follows the following pseudo-code:
630 allocate (size_t size, integer_type stat)
637 newmem = malloc (MAX (size, 1));
641 *stat = LIBERROR_ALLOCATION;
643 runtime_error ("Allocation would exceed memory limit");
648 gfc_allocate_using_malloc (stmtblock_t
* block
, tree pointer
,
649 tree size
, tree status
)
651 tree tmp
, error_cond
;
652 stmtblock_t on_error
;
653 tree status_type
= status
? TREE_TYPE (status
) : NULL_TREE
;
655 /* If successful and stat= is given, set status to 0. */
656 if (status
!= NULL_TREE
)
657 gfc_add_expr_to_block (block
,
658 fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
,
659 status
, build_int_cst (status_type
, 0)));
661 /* The allocation itself. */
662 size
= fold_convert (size_type_node
, size
);
663 gfc_add_modify (block
, pointer
,
664 fold_convert (TREE_TYPE (pointer
),
665 build_call_expr_loc (input_location
,
666 builtin_decl_explicit (BUILT_IN_MALLOC
), 1,
667 fold_build2_loc (input_location
,
668 MAX_EXPR
, size_type_node
, size
,
669 build_int_cst (size_type_node
, 1)))));
671 /* What to do in case of error. */
672 gfc_start_block (&on_error
);
673 if (status
!= NULL_TREE
)
675 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
, status
,
676 build_int_cst (status_type
, LIBERROR_ALLOCATION
));
677 gfc_add_expr_to_block (&on_error
, tmp
);
681 /* Here, os_error already implies PRED_NORETURN. */
682 tmp
= build_call_expr_loc (input_location
, gfor_fndecl_os_error
, 1,
683 gfc_build_addr_expr (pchar_type_node
,
684 gfc_build_localized_cstring_const
685 ("Allocation would exceed memory limit")));
686 gfc_add_expr_to_block (&on_error
, tmp
);
689 error_cond
= fold_build2_loc (input_location
, EQ_EXPR
,
690 boolean_type_node
, pointer
,
691 build_int_cst (prvoid_type_node
, 0));
692 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
693 gfc_unlikely (error_cond
, PRED_FORTRAN_FAIL_ALLOC
),
694 gfc_finish_block (&on_error
),
695 build_empty_stmt (input_location
));
697 gfc_add_expr_to_block (block
, tmp
);
701 /* Allocate memory, using an optional status argument.
703 This function follows the following pseudo-code:
706 allocate (size_t size, void** token, int *stat, char* errmsg, int errlen)
710 newmem = _caf_register (size, regtype, token, &stat, errmsg, errlen);
714 gfc_allocate_using_lib (stmtblock_t
* block
, tree pointer
, tree size
,
715 tree token
, tree status
, tree errmsg
, tree errlen
,
716 bool lock_var
, bool event_var
)
720 gcc_assert (token
!= NULL_TREE
);
722 /* The allocation itself. */
723 if (status
== NULL_TREE
)
724 pstat
= null_pointer_node
;
726 pstat
= gfc_build_addr_expr (NULL_TREE
, status
);
728 if (errmsg
== NULL_TREE
)
730 gcc_assert(errlen
== NULL_TREE
);
731 errmsg
= null_pointer_node
;
732 errlen
= build_int_cst (integer_type_node
, 0);
735 size
= fold_convert (size_type_node
, size
);
736 tmp
= build_call_expr_loc (input_location
,
737 gfor_fndecl_caf_register
, 7,
738 fold_build2_loc (input_location
,
739 MAX_EXPR
, size_type_node
, size
,
740 build_int_cst (size_type_node
, 1)),
741 build_int_cst (integer_type_node
,
742 lock_var
? GFC_CAF_LOCK_ALLOC
743 : event_var
? GFC_CAF_EVENT_ALLOC
744 : GFC_CAF_COARRAY_ALLOC
),
745 token
, gfc_build_addr_expr (pvoid_type_node
, pointer
),
746 pstat
, errmsg
, errlen
);
748 gfc_add_expr_to_block (block
, tmp
);
750 /* It guarantees memory consistency within the same segment */
751 tmp
= gfc_build_string_const (strlen ("memory")+1, "memory"),
752 tmp
= build5_loc (input_location
, ASM_EXPR
, void_type_node
,
753 gfc_build_string_const (1, ""), NULL_TREE
, NULL_TREE
,
754 tree_cons (NULL_TREE
, tmp
, NULL_TREE
), NULL_TREE
);
755 ASM_VOLATILE_P (tmp
) = 1;
756 gfc_add_expr_to_block (block
, tmp
);
760 /* Generate code for an ALLOCATE statement when the argument is an
761 allocatable variable. If the variable is currently allocated, it is an
762 error to allocate it again.
764 This function follows the following pseudo-code:
767 allocate_allocatable (void *mem, size_t size, integer_type stat)
770 return allocate (size, stat);
774 stat = LIBERROR_ALLOCATION;
776 runtime_error ("Attempting to allocate already allocated variable");
780 expr must be set to the original expression being allocated for its locus
781 and variable name in case a runtime error has to be printed. */
783 gfc_allocate_allocatable (stmtblock_t
* block
, tree mem
, tree size
,
784 tree token
, tree status
, tree errmsg
, tree errlen
,
785 tree label_finish
, gfc_expr
* expr
, int corank
)
787 stmtblock_t alloc_block
;
788 tree tmp
, null_mem
, alloc
, error
;
789 tree type
= TREE_TYPE (mem
);
790 symbol_attribute caf_attr
;
791 bool need_assign
= false;
793 size
= fold_convert (size_type_node
, size
);
794 null_mem
= gfc_unlikely (fold_build2_loc (input_location
, NE_EXPR
,
795 boolean_type_node
, mem
,
796 build_int_cst (type
, 0)),
797 PRED_FORTRAN_REALLOC
);
799 /* If mem is NULL, we call gfc_allocate_using_malloc or
800 gfc_allocate_using_lib. */
801 gfc_start_block (&alloc_block
);
803 if (flag_coarray
== GFC_FCOARRAY_LIB
)
804 caf_attr
= gfc_caf_attr (expr
, true);
806 if (flag_coarray
== GFC_FCOARRAY_LIB
807 && (corank
> 0 || caf_attr
.codimension
))
810 bool lock_var
= expr
->ts
.type
== BT_DERIVED
811 && expr
->ts
.u
.derived
->from_intmod
812 == INTMOD_ISO_FORTRAN_ENV
813 && expr
->ts
.u
.derived
->intmod_sym_id
814 == ISOFORTRAN_LOCK_TYPE
;
815 bool event_var
= expr
->ts
.type
== BT_DERIVED
816 && expr
->ts
.u
.derived
->from_intmod
817 == INTMOD_ISO_FORTRAN_ENV
818 && expr
->ts
.u
.derived
->intmod_sym_id
819 == ISOFORTRAN_EVENT_TYPE
;
821 gfc_init_se (&se
, NULL
);
823 tree sub_caf_tree
= gfc_get_ultimate_alloc_ptr_comps_caf_token (&se
,
825 if (sub_caf_tree
== NULL_TREE
)
826 sub_caf_tree
= token
;
828 /* When mem is an array ref, then strip the .data-ref. */
829 if (TREE_CODE (mem
) == COMPONENT_REF
830 && !(GFC_ARRAY_TYPE_P (TREE_TYPE (mem
))))
831 tmp
= TREE_OPERAND (mem
, 0);
835 if (!(GFC_ARRAY_TYPE_P (TREE_TYPE (tmp
))
836 && TYPE_LANG_SPECIFIC (TREE_TYPE (tmp
))->corank
== 0)
837 && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp
)))
839 symbol_attribute attr
;
841 gfc_clear_attr (&attr
);
842 tmp
= gfc_conv_scalar_to_descriptor (&se
, mem
, attr
);
845 gfc_add_block_to_block (&alloc_block
, &se
.pre
);
847 /* In the front end, we represent the lock variable as pointer. However,
848 the FE only passes the pointer around and leaves the actual
849 representation to the library. Hence, we have to convert back to the
850 number of elements. */
851 if (lock_var
|| event_var
)
852 size
= fold_build2_loc (input_location
, TRUNC_DIV_EXPR
, size_type_node
,
853 size
, TYPE_SIZE_UNIT (ptr_type_node
));
855 gfc_allocate_using_lib (&alloc_block
, tmp
, size
, sub_caf_tree
,
856 status
, errmsg
, errlen
, lock_var
, event_var
);
858 gfc_add_modify (&alloc_block
, mem
, fold_convert (TREE_TYPE (mem
),
859 gfc_conv_descriptor_data_get (tmp
)));
860 if (status
!= NULL_TREE
)
862 TREE_USED (label_finish
) = 1;
863 tmp
= build1_v (GOTO_EXPR
, label_finish
);
864 cond
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
865 status
, build_zero_cst (TREE_TYPE (status
)));
866 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
867 gfc_unlikely (cond
, PRED_FORTRAN_FAIL_ALLOC
),
868 tmp
, build_empty_stmt (input_location
));
869 gfc_add_expr_to_block (&alloc_block
, tmp
);
873 gfc_allocate_using_malloc (&alloc_block
, mem
, size
, status
);
875 alloc
= gfc_finish_block (&alloc_block
);
877 /* If mem is not NULL, we issue a runtime error or set the
883 gcc_assert (expr
->expr_type
== EXPR_VARIABLE
&& expr
->symtree
);
884 varname
= gfc_build_cstring_const (expr
->symtree
->name
);
885 varname
= gfc_build_addr_expr (pchar_type_node
, varname
);
887 error
= gfc_trans_runtime_error (true, &expr
->where
,
888 "Attempting to allocate already"
889 " allocated variable '%s'",
893 error
= gfc_trans_runtime_error (true, NULL
,
894 "Attempting to allocate already allocated"
897 if (status
!= NULL_TREE
)
899 tree status_type
= TREE_TYPE (status
);
901 error
= fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
,
902 status
, build_int_cst (status_type
, LIBERROR_ALLOCATION
));
905 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, null_mem
,
907 gfc_add_expr_to_block (block
, tmp
);
911 /* Free a given variable. */
914 gfc_call_free (tree var
)
916 return build_call_expr_loc (input_location
,
917 builtin_decl_explicit (BUILT_IN_FREE
),
918 1, fold_convert (pvoid_type_node
, var
));
922 /* Build a call to a FINAL procedure, which finalizes "var". */
925 gfc_build_final_call (gfc_typespec ts
, gfc_expr
*final_wrapper
, gfc_expr
*var
,
926 bool fini_coarray
, gfc_expr
*class_size
)
930 tree final_fndecl
, array
, size
, tmp
;
931 symbol_attribute attr
;
933 gcc_assert (final_wrapper
->expr_type
== EXPR_VARIABLE
);
936 gfc_start_block (&block
);
937 gfc_init_se (&se
, NULL
);
938 gfc_conv_expr (&se
, final_wrapper
);
939 final_fndecl
= se
.expr
;
940 if (POINTER_TYPE_P (TREE_TYPE (final_fndecl
)))
941 final_fndecl
= build_fold_indirect_ref_loc (input_location
, final_fndecl
);
943 if (ts
.type
== BT_DERIVED
)
947 gcc_assert (!class_size
);
948 elem_size
= gfc_typenode_for_spec (&ts
);
949 elem_size
= TYPE_SIZE_UNIT (elem_size
);
950 size
= fold_convert (gfc_array_index_type
, elem_size
);
952 gfc_init_se (&se
, NULL
);
956 se
.descriptor_only
= 1;
957 gfc_conv_expr_descriptor (&se
, var
);
962 gfc_conv_expr (&se
, var
);
963 gcc_assert (se
.pre
.head
== NULL_TREE
&& se
.post
.head
== NULL_TREE
);
966 /* No copy back needed, hence set attr's allocatable/pointer
968 gfc_clear_attr (&attr
);
969 gfc_init_se (&se
, NULL
);
970 array
= gfc_conv_scalar_to_descriptor (&se
, array
, attr
);
971 gcc_assert (se
.post
.head
== NULL_TREE
);
976 gfc_expr
*array_expr
;
977 gcc_assert (class_size
);
978 gfc_init_se (&se
, NULL
);
979 gfc_conv_expr (&se
, class_size
);
980 gfc_add_block_to_block (&block
, &se
.pre
);
981 gcc_assert (se
.post
.head
== NULL_TREE
);
984 array_expr
= gfc_copy_expr (var
);
985 gfc_init_se (&se
, NULL
);
987 if (array_expr
->rank
)
989 gfc_add_class_array_ref (array_expr
);
990 se
.descriptor_only
= 1;
991 gfc_conv_expr_descriptor (&se
, array_expr
);
996 gfc_add_data_component (array_expr
);
997 gfc_conv_expr (&se
, array_expr
);
998 gfc_add_block_to_block (&block
, &se
.pre
);
999 gcc_assert (se
.post
.head
== NULL_TREE
);
1001 if (TREE_CODE (array
) == ADDR_EXPR
1002 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (array
, 0))))
1003 tmp
= TREE_OPERAND (array
, 0);
1005 if (!gfc_is_coarray (array_expr
))
1007 /* No copy back needed, hence set attr's allocatable/pointer
1009 gfc_clear_attr (&attr
);
1010 gfc_init_se (&se
, NULL
);
1011 array
= gfc_conv_scalar_to_descriptor (&se
, array
, attr
);
1013 gcc_assert (se
.post
.head
== NULL_TREE
);
1015 gfc_free_expr (array_expr
);
1018 if (!POINTER_TYPE_P (TREE_TYPE (array
)))
1019 array
= gfc_build_addr_expr (NULL
, array
);
1021 gfc_add_block_to_block (&block
, &se
.pre
);
1022 tmp
= build_call_expr_loc (input_location
,
1023 final_fndecl
, 3, array
,
1024 size
, fini_coarray
? boolean_true_node
1025 : boolean_false_node
);
1026 gfc_add_block_to_block (&block
, &se
.post
);
1027 gfc_add_expr_to_block (&block
, tmp
);
1028 return gfc_finish_block (&block
);
1033 gfc_add_comp_finalizer_call (stmtblock_t
*block
, tree decl
, gfc_component
*comp
,
1038 tree final_fndecl
, size
, array
, tmp
, cond
;
1039 symbol_attribute attr
;
1040 gfc_expr
*final_expr
= NULL
;
1042 if (comp
->ts
.type
!= BT_DERIVED
&& comp
->ts
.type
!= BT_CLASS
)
1045 gfc_init_block (&block2
);
1047 if (comp
->ts
.type
== BT_DERIVED
)
1049 if (comp
->attr
.pointer
)
1052 gfc_is_finalizable (comp
->ts
.u
.derived
, &final_expr
);
1056 gfc_init_se (&se
, NULL
);
1057 gfc_conv_expr (&se
, final_expr
);
1058 final_fndecl
= se
.expr
;
1059 size
= gfc_typenode_for_spec (&comp
->ts
);
1060 size
= TYPE_SIZE_UNIT (size
);
1061 size
= fold_convert (gfc_array_index_type
, size
);
1065 else /* comp->ts.type == BT_CLASS. */
1067 if (CLASS_DATA (comp
)->attr
.class_pointer
)
1070 gfc_is_finalizable (CLASS_DATA (comp
)->ts
.u
.derived
, &final_expr
);
1071 final_fndecl
= gfc_class_vtab_final_get (decl
);
1072 size
= gfc_class_vtab_size_get (decl
);
1073 array
= gfc_class_data_get (decl
);
1076 if (comp
->attr
.allocatable
1077 || (comp
->ts
.type
== BT_CLASS
&& CLASS_DATA (comp
)->attr
.allocatable
))
1079 tmp
= GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array
))
1080 ? gfc_conv_descriptor_data_get (array
) : array
;
1081 cond
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
1082 tmp
, fold_convert (TREE_TYPE (tmp
),
1083 null_pointer_node
));
1086 cond
= boolean_true_node
;
1088 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array
)))
1090 gfc_clear_attr (&attr
);
1091 gfc_init_se (&se
, NULL
);
1092 array
= gfc_conv_scalar_to_descriptor (&se
, array
, attr
);
1093 gfc_add_block_to_block (&block2
, &se
.pre
);
1094 gcc_assert (se
.post
.head
== NULL_TREE
);
1097 if (!POINTER_TYPE_P (TREE_TYPE (array
)))
1098 array
= gfc_build_addr_expr (NULL
, array
);
1102 tmp
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
1104 fold_convert (TREE_TYPE (final_fndecl
),
1105 null_pointer_node
));
1106 cond
= fold_build2_loc (input_location
, TRUTH_ANDIF_EXPR
,
1107 boolean_type_node
, cond
, tmp
);
1110 if (POINTER_TYPE_P (TREE_TYPE (final_fndecl
)))
1111 final_fndecl
= build_fold_indirect_ref_loc (input_location
, final_fndecl
);
1113 tmp
= build_call_expr_loc (input_location
,
1114 final_fndecl
, 3, array
,
1115 size
, fini_coarray
? boolean_true_node
1116 : boolean_false_node
);
1117 gfc_add_expr_to_block (&block2
, tmp
);
1118 tmp
= gfc_finish_block (&block2
);
1120 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
, tmp
,
1121 build_empty_stmt (input_location
));
1122 gfc_add_expr_to_block (block
, tmp
);
1128 /* Add a call to the finalizer, using the passed *expr. Returns
1129 true when a finalizer call has been inserted. */
1132 gfc_add_finalizer_call (stmtblock_t
*block
, gfc_expr
*expr2
)
1137 gfc_expr
*final_expr
= NULL
;
1138 gfc_expr
*elem_size
= NULL
;
1139 bool has_finalizer
= false;
1141 if (!expr2
|| (expr2
->ts
.type
!= BT_DERIVED
&& expr2
->ts
.type
!= BT_CLASS
))
1144 if (expr2
->ts
.type
== BT_DERIVED
)
1146 gfc_is_finalizable (expr2
->ts
.u
.derived
, &final_expr
);
1151 /* If we have a class array, we need go back to the class
1153 expr
= gfc_copy_expr (expr2
);
1155 if (expr
->ref
&& expr
->ref
->next
&& !expr
->ref
->next
->next
1156 && expr
->ref
->next
->type
== REF_ARRAY
1157 && expr
->ref
->type
== REF_COMPONENT
1158 && strcmp (expr
->ref
->u
.c
.component
->name
, "_data") == 0)
1160 gfc_free_ref_list (expr
->ref
);
1164 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
1165 if (ref
->next
&& ref
->next
->next
&& !ref
->next
->next
->next
1166 && ref
->next
->next
->type
== REF_ARRAY
1167 && ref
->next
->type
== REF_COMPONENT
1168 && strcmp (ref
->next
->u
.c
.component
->name
, "_data") == 0)
1170 gfc_free_ref_list (ref
->next
);
1174 if (expr
->ts
.type
== BT_CLASS
)
1176 has_finalizer
= gfc_is_finalizable (expr
->ts
.u
.derived
, NULL
);
1178 if (!expr2
->rank
&& !expr2
->ref
&& CLASS_DATA (expr2
->symtree
->n
.sym
)->as
)
1179 expr
->rank
= CLASS_DATA (expr2
->symtree
->n
.sym
)->as
->rank
;
1181 final_expr
= gfc_copy_expr (expr
);
1182 gfc_add_vptr_component (final_expr
);
1183 gfc_add_final_component (final_expr
);
1185 elem_size
= gfc_copy_expr (expr
);
1186 gfc_add_vptr_component (elem_size
);
1187 gfc_add_size_component (elem_size
);
1190 gcc_assert (final_expr
->expr_type
== EXPR_VARIABLE
);
1192 tmp
= gfc_build_final_call (expr
->ts
, final_expr
, expr
,
1195 if (expr
->ts
.type
== BT_CLASS
&& !has_finalizer
)
1200 gfc_init_se (&se
, NULL
);
1201 se
.want_pointer
= 1;
1202 gfc_conv_expr (&se
, final_expr
);
1203 cond
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
1204 se
.expr
, build_int_cst (TREE_TYPE (se
.expr
), 0));
1206 /* For CLASS(*) not only sym->_vtab->_final can be NULL
1207 but already sym->_vtab itself. */
1208 if (UNLIMITED_POLY (expr
))
1211 gfc_expr
*vptr_expr
;
1213 vptr_expr
= gfc_copy_expr (expr
);
1214 gfc_add_vptr_component (vptr_expr
);
1216 gfc_init_se (&se
, NULL
);
1217 se
.want_pointer
= 1;
1218 gfc_conv_expr (&se
, vptr_expr
);
1219 gfc_free_expr (vptr_expr
);
1221 cond2
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
1223 build_int_cst (TREE_TYPE (se
.expr
), 0));
1224 cond
= fold_build2_loc (input_location
, TRUTH_ANDIF_EXPR
,
1225 boolean_type_node
, cond2
, cond
);
1228 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
1229 cond
, tmp
, build_empty_stmt (input_location
));
1232 gfc_add_expr_to_block (block
, tmp
);
1238 /* User-deallocate; we emit the code directly from the front-end, and the
1239 logic is the same as the previous library function:
1242 deallocate (void *pointer, GFC_INTEGER_4 * stat)
1249 runtime_error ("Attempt to DEALLOCATE unallocated memory.");
1259 In this front-end version, status doesn't have to be GFC_INTEGER_4.
1260 Moreover, if CAN_FAIL is true, then we will not emit a runtime error,
1261 even when no status variable is passed to us (this is used for
1262 unconditional deallocation generated by the front-end at end of
1265 If a runtime-message is possible, `expr' must point to the original
1266 expression being deallocated for its locus and variable name.
1268 For coarrays, "pointer" must be the array descriptor and not its
1269 "data" component. */
1271 gfc_deallocate_with_status (tree pointer
, tree status
, tree errmsg
,
1272 tree errlen
, tree label_finish
,
1273 bool can_fail
, gfc_expr
* expr
, bool coarray
)
1275 stmtblock_t null
, non_null
;
1276 tree cond
, tmp
, error
;
1277 tree status_type
= NULL_TREE
;
1278 tree caf_decl
= NULL_TREE
;
1282 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (pointer
)));
1284 pointer
= gfc_conv_descriptor_data_get (caf_decl
);
1285 STRIP_NOPS (pointer
);
1288 cond
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
, pointer
,
1289 build_int_cst (TREE_TYPE (pointer
), 0));
1291 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
1292 we emit a runtime error. */
1293 gfc_start_block (&null
);
1298 gcc_assert (expr
&& expr
->expr_type
== EXPR_VARIABLE
&& expr
->symtree
);
1300 varname
= gfc_build_cstring_const (expr
->symtree
->name
);
1301 varname
= gfc_build_addr_expr (pchar_type_node
, varname
);
1303 error
= gfc_trans_runtime_error (true, &expr
->where
,
1304 "Attempt to DEALLOCATE unallocated '%s'",
1308 error
= build_empty_stmt (input_location
);
1310 if (status
!= NULL_TREE
&& !integer_zerop (status
))
1314 status_type
= TREE_TYPE (TREE_TYPE (status
));
1315 cond2
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
1316 status
, build_int_cst (TREE_TYPE (status
), 0));
1317 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
,
1318 fold_build1_loc (input_location
, INDIRECT_REF
,
1319 status_type
, status
),
1320 build_int_cst (status_type
, 1));
1321 error
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
1325 gfc_add_expr_to_block (&null
, error
);
1327 /* When POINTER is not NULL, we free it. */
1328 gfc_start_block (&non_null
);
1329 gfc_add_finalizer_call (&non_null
, expr
);
1330 if (!coarray
|| flag_coarray
!= GFC_FCOARRAY_LIB
)
1332 tmp
= build_call_expr_loc (input_location
,
1333 builtin_decl_explicit (BUILT_IN_FREE
), 1,
1334 fold_convert (pvoid_type_node
, pointer
));
1335 gfc_add_expr_to_block (&non_null
, tmp
);
1337 if (status
!= NULL_TREE
&& !integer_zerop (status
))
1339 /* We set STATUS to zero if it is present. */
1340 tree status_type
= TREE_TYPE (TREE_TYPE (status
));
1343 cond2
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
1345 build_int_cst (TREE_TYPE (status
), 0));
1346 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
,
1347 fold_build1_loc (input_location
, INDIRECT_REF
,
1348 status_type
, status
),
1349 build_int_cst (status_type
, 0));
1350 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
1351 gfc_unlikely (cond2
, PRED_FORTRAN_FAIL_ALLOC
),
1352 tmp
, build_empty_stmt (input_location
));
1353 gfc_add_expr_to_block (&non_null
, tmp
);
1358 tree caf_type
, token
, cond2
;
1359 tree pstat
= null_pointer_node
;
1361 if (errmsg
== NULL_TREE
)
1363 gcc_assert (errlen
== NULL_TREE
);
1364 errmsg
= null_pointer_node
;
1365 errlen
= build_zero_cst (integer_type_node
);
1369 gcc_assert (errlen
!= NULL_TREE
);
1370 if (!POINTER_TYPE_P (TREE_TYPE (errmsg
)))
1371 errmsg
= gfc_build_addr_expr (NULL_TREE
, errmsg
);
1374 caf_type
= TREE_TYPE (caf_decl
);
1376 if (status
!= NULL_TREE
&& !integer_zerop (status
))
1378 gcc_assert (status_type
== integer_type_node
);
1382 if (GFC_DESCRIPTOR_TYPE_P (caf_type
)
1383 && GFC_TYPE_ARRAY_AKIND (caf_type
) == GFC_ARRAY_ALLOCATABLE
)
1384 token
= gfc_conv_descriptor_token (caf_decl
);
1385 else if (DECL_LANG_SPECIFIC (caf_decl
)
1386 && GFC_DECL_TOKEN (caf_decl
) != NULL_TREE
)
1387 token
= GFC_DECL_TOKEN (caf_decl
);
1390 gcc_assert (GFC_ARRAY_TYPE_P (caf_type
)
1391 && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
) != NULL_TREE
);
1392 token
= GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
);
1395 token
= gfc_build_addr_expr (NULL_TREE
, token
);
1396 tmp
= build_call_expr_loc (input_location
,
1397 gfor_fndecl_caf_deregister
, 4,
1398 token
, pstat
, errmsg
, errlen
);
1399 gfc_add_expr_to_block (&non_null
, tmp
);
1401 /* It guarantees memory consistency within the same segment */
1402 tmp
= gfc_build_string_const (strlen ("memory")+1, "memory"),
1403 tmp
= build5_loc (input_location
, ASM_EXPR
, void_type_node
,
1404 gfc_build_string_const (1, ""), NULL_TREE
, NULL_TREE
,
1405 tree_cons (NULL_TREE
, tmp
, NULL_TREE
), NULL_TREE
);
1406 ASM_VOLATILE_P (tmp
) = 1;
1407 gfc_add_expr_to_block (&non_null
, tmp
);
1409 if (status
!= NULL_TREE
)
1411 tree stat
= build_fold_indirect_ref_loc (input_location
, status
);
1413 TREE_USED (label_finish
) = 1;
1414 tmp
= build1_v (GOTO_EXPR
, label_finish
);
1415 cond2
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
1416 stat
, build_zero_cst (TREE_TYPE (stat
)));
1417 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
1418 gfc_unlikely (cond2
, PRED_FORTRAN_REALLOC
),
1419 tmp
, build_empty_stmt (input_location
));
1420 gfc_add_expr_to_block (&non_null
, tmp
);
1424 return fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
,
1425 gfc_finish_block (&null
),
1426 gfc_finish_block (&non_null
));
1430 /* Generate code for deallocation of allocatable scalars (variables or
1431 components). Before the object itself is freed, any allocatable
1432 subcomponents are being deallocated. */
1435 gfc_deallocate_scalar_with_status (tree pointer
, tree status
, bool can_fail
,
1436 gfc_expr
* expr
, gfc_typespec ts
)
1438 stmtblock_t null
, non_null
;
1439 tree cond
, tmp
, error
;
1442 cond
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
, pointer
,
1443 build_int_cst (TREE_TYPE (pointer
), 0));
1445 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
1446 we emit a runtime error. */
1447 gfc_start_block (&null
);
1452 gcc_assert (expr
&& expr
->expr_type
== EXPR_VARIABLE
&& expr
->symtree
);
1454 varname
= gfc_build_cstring_const (expr
->symtree
->name
);
1455 varname
= gfc_build_addr_expr (pchar_type_node
, varname
);
1457 error
= gfc_trans_runtime_error (true, &expr
->where
,
1458 "Attempt to DEALLOCATE unallocated '%s'",
1462 error
= build_empty_stmt (input_location
);
1464 if (status
!= NULL_TREE
&& !integer_zerop (status
))
1466 tree status_type
= TREE_TYPE (TREE_TYPE (status
));
1469 cond2
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
1470 status
, build_int_cst (TREE_TYPE (status
), 0));
1471 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
,
1472 fold_build1_loc (input_location
, INDIRECT_REF
,
1473 status_type
, status
),
1474 build_int_cst (status_type
, 1));
1475 error
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
1479 gfc_add_expr_to_block (&null
, error
);
1481 /* When POINTER is not NULL, we free it. */
1482 gfc_start_block (&non_null
);
1484 /* Free allocatable components. */
1485 finalizable
= gfc_add_finalizer_call (&non_null
, expr
);
1486 if (!finalizable
&& ts
.type
== BT_DERIVED
&& ts
.u
.derived
->attr
.alloc_comp
)
1488 tmp
= build_fold_indirect_ref_loc (input_location
, pointer
);
1489 tmp
= gfc_deallocate_alloc_comp (ts
.u
.derived
, tmp
, 0);
1490 gfc_add_expr_to_block (&non_null
, tmp
);
1493 tmp
= build_call_expr_loc (input_location
,
1494 builtin_decl_explicit (BUILT_IN_FREE
), 1,
1495 fold_convert (pvoid_type_node
, pointer
));
1496 gfc_add_expr_to_block (&non_null
, tmp
);
1498 if (status
!= NULL_TREE
&& !integer_zerop (status
))
1500 /* We set STATUS to zero if it is present. */
1501 tree status_type
= TREE_TYPE (TREE_TYPE (status
));
1504 cond2
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
1505 status
, build_int_cst (TREE_TYPE (status
), 0));
1506 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
,
1507 fold_build1_loc (input_location
, INDIRECT_REF
,
1508 status_type
, status
),
1509 build_int_cst (status_type
, 0));
1510 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond2
,
1511 tmp
, build_empty_stmt (input_location
));
1512 gfc_add_expr_to_block (&non_null
, tmp
);
1515 return fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
,
1516 gfc_finish_block (&null
),
1517 gfc_finish_block (&non_null
));
1521 /* Reallocate MEM so it has SIZE bytes of data. This behaves like the
1522 following pseudo-code:
1525 internal_realloc (void *mem, size_t size)
1527 res = realloc (mem, size);
1528 if (!res && size != 0)
1529 _gfortran_os_error ("Allocation would exceed memory limit");
1534 gfc_call_realloc (stmtblock_t
* block
, tree mem
, tree size
)
1536 tree msg
, res
, nonzero
, null_result
, tmp
;
1537 tree type
= TREE_TYPE (mem
);
1539 /* Only evaluate the size once. */
1540 size
= save_expr (fold_convert (size_type_node
, size
));
1542 /* Create a variable to hold the result. */
1543 res
= gfc_create_var (type
, NULL
);
1545 /* Call realloc and check the result. */
1546 tmp
= build_call_expr_loc (input_location
,
1547 builtin_decl_explicit (BUILT_IN_REALLOC
), 2,
1548 fold_convert (pvoid_type_node
, mem
), size
);
1549 gfc_add_modify (block
, res
, fold_convert (type
, tmp
));
1550 null_result
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
,
1551 res
, build_int_cst (pvoid_type_node
, 0));
1552 nonzero
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
, size
,
1553 build_int_cst (size_type_node
, 0));
1554 null_result
= fold_build2_loc (input_location
, TRUTH_AND_EXPR
, boolean_type_node
,
1555 null_result
, nonzero
);
1556 msg
= gfc_build_addr_expr (pchar_type_node
, gfc_build_localized_cstring_const
1557 ("Allocation would exceed memory limit"));
1558 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
1560 build_call_expr_loc (input_location
,
1561 gfor_fndecl_os_error
, 1, msg
),
1562 build_empty_stmt (input_location
));
1563 gfc_add_expr_to_block (block
, tmp
);
1569 /* Add an expression to another one, either at the front or the back. */
1572 add_expr_to_chain (tree
* chain
, tree expr
, bool front
)
1574 if (expr
== NULL_TREE
|| IS_EMPTY_STMT (expr
))
1579 if (TREE_CODE (*chain
) != STATEMENT_LIST
)
1585 append_to_statement_list (tmp
, chain
);
1590 tree_stmt_iterator i
;
1592 i
= tsi_start (*chain
);
1593 tsi_link_before (&i
, expr
, TSI_CONTINUE_LINKING
);
1596 append_to_statement_list (expr
, chain
);
1603 /* Add a statement at the end of a block. */
1606 gfc_add_expr_to_block (stmtblock_t
* block
, tree expr
)
1609 add_expr_to_chain (&block
->head
, expr
, false);
1613 /* Add a statement at the beginning of a block. */
1616 gfc_prepend_expr_to_block (stmtblock_t
* block
, tree expr
)
1619 add_expr_to_chain (&block
->head
, expr
, true);
1623 /* Add a block the end of a block. */
1626 gfc_add_block_to_block (stmtblock_t
* block
, stmtblock_t
* append
)
1628 gcc_assert (append
);
1629 gcc_assert (!append
->has_scope
);
1631 gfc_add_expr_to_block (block
, append
->head
);
1632 append
->head
= NULL_TREE
;
1636 /* Save the current locus. The structure may not be complete, and should
1637 only be used with gfc_restore_backend_locus. */
1640 gfc_save_backend_locus (locus
* loc
)
1642 loc
->lb
= XCNEW (gfc_linebuf
);
1643 loc
->lb
->location
= input_location
;
1644 loc
->lb
->file
= gfc_current_backend_file
;
1648 /* Set the current locus. */
1651 gfc_set_backend_locus (locus
* loc
)
1653 gfc_current_backend_file
= loc
->lb
->file
;
1654 input_location
= loc
->lb
->location
;
1658 /* Restore the saved locus. Only used in conjunction with
1659 gfc_save_backend_locus, to free the memory when we are done. */
1662 gfc_restore_backend_locus (locus
* loc
)
1664 gfc_set_backend_locus (loc
);
1669 /* Translate an executable statement. The tree cond is used by gfc_trans_do.
1670 This static function is wrapped by gfc_trans_code_cond and
1674 trans_code (gfc_code
* code
, tree cond
)
1680 return build_empty_stmt (input_location
);
1682 gfc_start_block (&block
);
1684 /* Translate statements one by one into GENERIC trees until we reach
1685 the end of this gfc_code branch. */
1686 for (; code
; code
= code
->next
)
1688 if (code
->here
!= 0)
1690 res
= gfc_trans_label_here (code
);
1691 gfc_add_expr_to_block (&block
, res
);
1694 gfc_current_locus
= code
->loc
;
1695 gfc_set_backend_locus (&code
->loc
);
1700 case EXEC_END_BLOCK
:
1701 case EXEC_END_NESTED_BLOCK
:
1702 case EXEC_END_PROCEDURE
:
1707 if (code
->expr1
->ts
.type
== BT_CLASS
)
1708 res
= gfc_trans_class_assign (code
->expr1
, code
->expr2
, code
->op
);
1710 res
= gfc_trans_assign (code
);
1713 case EXEC_LABEL_ASSIGN
:
1714 res
= gfc_trans_label_assign (code
);
1717 case EXEC_POINTER_ASSIGN
:
1718 if (code
->expr1
->ts
.type
== BT_CLASS
)
1719 res
= gfc_trans_class_assign (code
->expr1
, code
->expr2
, code
->op
);
1720 else if (UNLIMITED_POLY (code
->expr2
)
1721 && code
->expr1
->ts
.type
== BT_DERIVED
1722 && (code
->expr1
->ts
.u
.derived
->attr
.sequence
1723 || code
->expr1
->ts
.u
.derived
->attr
.is_bind_c
))
1725 res
= gfc_trans_class_assign (code
->expr1
, code
->expr2
, code
->op
);
1727 res
= gfc_trans_pointer_assign (code
);
1730 case EXEC_INIT_ASSIGN
:
1731 if (code
->expr1
->ts
.type
== BT_CLASS
)
1732 res
= gfc_trans_class_init_assign (code
);
1734 res
= gfc_trans_init_assign (code
);
1742 res
= gfc_trans_critical (code
);
1746 res
= gfc_trans_cycle (code
);
1750 res
= gfc_trans_exit (code
);
1754 res
= gfc_trans_goto (code
);
1758 res
= gfc_trans_entry (code
);
1762 res
= gfc_trans_pause (code
);
1766 case EXEC_ERROR_STOP
:
1767 res
= gfc_trans_stop (code
, code
->op
== EXEC_ERROR_STOP
);
1771 /* For MVBITS we've got the special exception that we need a
1772 dependency check, too. */
1774 bool is_mvbits
= false;
1776 if (code
->resolved_isym
)
1778 res
= gfc_conv_intrinsic_subroutine (code
);
1779 if (res
!= NULL_TREE
)
1783 if (code
->resolved_isym
1784 && code
->resolved_isym
->id
== GFC_ISYM_MVBITS
)
1787 res
= gfc_trans_call (code
, is_mvbits
, NULL_TREE
,
1793 res
= gfc_trans_call (code
, false, NULL_TREE
,
1797 case EXEC_ASSIGN_CALL
:
1798 res
= gfc_trans_call (code
, true, NULL_TREE
,
1803 res
= gfc_trans_return (code
);
1807 res
= gfc_trans_if (code
);
1810 case EXEC_ARITHMETIC_IF
:
1811 res
= gfc_trans_arithmetic_if (code
);
1815 res
= gfc_trans_block_construct (code
);
1819 res
= gfc_trans_do (code
, cond
);
1822 case EXEC_DO_CONCURRENT
:
1823 res
= gfc_trans_do_concurrent (code
);
1827 res
= gfc_trans_do_while (code
);
1831 res
= gfc_trans_select (code
);
1834 case EXEC_SELECT_TYPE
:
1835 /* Do nothing. SELECT TYPE statements should be transformed into
1836 an ordinary SELECT CASE at resolution stage.
1837 TODO: Add an error message here once this is done. */
1842 res
= gfc_trans_flush (code
);
1846 case EXEC_SYNC_IMAGES
:
1847 case EXEC_SYNC_MEMORY
:
1848 res
= gfc_trans_sync (code
, code
->op
);
1853 res
= gfc_trans_lock_unlock (code
, code
->op
);
1856 case EXEC_EVENT_POST
:
1857 case EXEC_EVENT_WAIT
:
1858 res
= gfc_trans_event_post_wait (code
, code
->op
);
1862 res
= gfc_trans_forall (code
);
1866 res
= gfc_trans_where (code
);
1870 res
= gfc_trans_allocate (code
);
1873 case EXEC_DEALLOCATE
:
1874 res
= gfc_trans_deallocate (code
);
1878 res
= gfc_trans_open (code
);
1882 res
= gfc_trans_close (code
);
1886 res
= gfc_trans_read (code
);
1890 res
= gfc_trans_write (code
);
1894 res
= gfc_trans_iolength (code
);
1897 case EXEC_BACKSPACE
:
1898 res
= gfc_trans_backspace (code
);
1902 res
= gfc_trans_endfile (code
);
1906 res
= gfc_trans_inquire (code
);
1910 res
= gfc_trans_wait (code
);
1914 res
= gfc_trans_rewind (code
);
1918 res
= gfc_trans_transfer (code
);
1922 res
= gfc_trans_dt_end (code
);
1925 case EXEC_OMP_ATOMIC
:
1926 case EXEC_OMP_BARRIER
:
1927 case EXEC_OMP_CANCEL
:
1928 case EXEC_OMP_CANCELLATION_POINT
:
1929 case EXEC_OMP_CRITICAL
:
1930 case EXEC_OMP_DISTRIBUTE
:
1931 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO
:
1932 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
1933 case EXEC_OMP_DISTRIBUTE_SIMD
:
1935 case EXEC_OMP_DO_SIMD
:
1936 case EXEC_OMP_FLUSH
:
1937 case EXEC_OMP_MASTER
:
1938 case EXEC_OMP_ORDERED
:
1939 case EXEC_OMP_PARALLEL
:
1940 case EXEC_OMP_PARALLEL_DO
:
1941 case EXEC_OMP_PARALLEL_DO_SIMD
:
1942 case EXEC_OMP_PARALLEL_SECTIONS
:
1943 case EXEC_OMP_PARALLEL_WORKSHARE
:
1944 case EXEC_OMP_SECTIONS
:
1946 case EXEC_OMP_SINGLE
:
1947 case EXEC_OMP_TARGET
:
1948 case EXEC_OMP_TARGET_DATA
:
1949 case EXEC_OMP_TARGET_TEAMS
:
1950 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE
:
1951 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
1952 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
1953 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
1954 case EXEC_OMP_TARGET_UPDATE
:
1956 case EXEC_OMP_TASKGROUP
:
1957 case EXEC_OMP_TASKWAIT
:
1958 case EXEC_OMP_TASKYIELD
:
1959 case EXEC_OMP_TEAMS
:
1960 case EXEC_OMP_TEAMS_DISTRIBUTE
:
1961 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
1962 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
1963 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD
:
1964 case EXEC_OMP_WORKSHARE
:
1965 res
= gfc_trans_omp_directive (code
);
1968 case EXEC_OACC_CACHE
:
1969 case EXEC_OACC_WAIT
:
1970 case EXEC_OACC_UPDATE
:
1971 case EXEC_OACC_LOOP
:
1972 case EXEC_OACC_HOST_DATA
:
1973 case EXEC_OACC_DATA
:
1974 case EXEC_OACC_KERNELS
:
1975 case EXEC_OACC_KERNELS_LOOP
:
1976 case EXEC_OACC_PARALLEL
:
1977 case EXEC_OACC_PARALLEL_LOOP
:
1978 case EXEC_OACC_ENTER_DATA
:
1979 case EXEC_OACC_EXIT_DATA
:
1980 case EXEC_OACC_ATOMIC
:
1981 case EXEC_OACC_DECLARE
:
1982 res
= gfc_trans_oacc_directive (code
);
1986 gfc_internal_error ("gfc_trans_code(): Bad statement code");
1989 gfc_set_backend_locus (&code
->loc
);
1991 if (res
!= NULL_TREE
&& ! IS_EMPTY_STMT (res
))
1993 if (TREE_CODE (res
) != STATEMENT_LIST
)
1994 SET_EXPR_LOCATION (res
, input_location
);
1996 /* Add the new statement to the block. */
1997 gfc_add_expr_to_block (&block
, res
);
2001 /* Return the finished block. */
2002 return gfc_finish_block (&block
);
2006 /* Translate an executable statement with condition, cond. The condition is
2007 used by gfc_trans_do to test for IO result conditions inside implied
2008 DO loops of READ and WRITE statements. See build_dt in trans-io.c. */
2011 gfc_trans_code_cond (gfc_code
* code
, tree cond
)
2013 return trans_code (code
, cond
);
2016 /* Translate an executable statement without condition. */
2019 gfc_trans_code (gfc_code
* code
)
2021 return trans_code (code
, NULL_TREE
);
2025 /* This function is called after a complete program unit has been parsed
2029 gfc_generate_code (gfc_namespace
* ns
)
2032 if (ns
->is_block_data
)
2034 gfc_generate_block_data (ns
);
2038 gfc_generate_function_code (ns
);
2042 /* This function is called after a complete module has been parsed
2046 gfc_generate_module_code (gfc_namespace
* ns
)
2049 struct module_htab_entry
*entry
;
2051 gcc_assert (ns
->proc_name
->backend_decl
== NULL
);
2052 ns
->proc_name
->backend_decl
2053 = build_decl (ns
->proc_name
->declared_at
.lb
->location
,
2054 NAMESPACE_DECL
, get_identifier (ns
->proc_name
->name
),
2056 entry
= gfc_find_module (ns
->proc_name
->name
);
2057 if (entry
->namespace_decl
)
2058 /* Buggy sourcecode, using a module before defining it? */
2059 entry
->decls
->empty ();
2060 entry
->namespace_decl
= ns
->proc_name
->backend_decl
;
2062 gfc_generate_module_vars (ns
);
2064 /* We need to generate all module function prototypes first, to allow
2066 for (n
= ns
->contained
; n
; n
= n
->sibling
)
2073 gfc_create_function_decl (n
, false);
2074 DECL_CONTEXT (n
->proc_name
->backend_decl
) = ns
->proc_name
->backend_decl
;
2075 gfc_module_add_decl (entry
, n
->proc_name
->backend_decl
);
2076 for (el
= ns
->entries
; el
; el
= el
->next
)
2078 DECL_CONTEXT (el
->sym
->backend_decl
) = ns
->proc_name
->backend_decl
;
2079 gfc_module_add_decl (entry
, el
->sym
->backend_decl
);
2083 for (n
= ns
->contained
; n
; n
= n
->sibling
)
2088 gfc_generate_function_code (n
);
2093 /* Initialize an init/cleanup block with existing code. */
2096 gfc_start_wrapped_block (gfc_wrapped_block
* block
, tree code
)
2100 block
->init
= NULL_TREE
;
2102 block
->cleanup
= NULL_TREE
;
2106 /* Add a new pair of initializers/clean-up code. */
2109 gfc_add_init_cleanup (gfc_wrapped_block
* block
, tree init
, tree cleanup
)
2113 /* The new pair of init/cleanup should be "wrapped around" the existing
2114 block of code, thus the initialization is added to the front and the
2115 cleanup to the back. */
2116 add_expr_to_chain (&block
->init
, init
, true);
2117 add_expr_to_chain (&block
->cleanup
, cleanup
, false);
2121 /* Finish up a wrapped block by building a corresponding try-finally expr. */
2124 gfc_finish_wrapped_block (gfc_wrapped_block
* block
)
2130 /* Build the final expression. For this, just add init and body together,
2131 and put clean-up with that into a TRY_FINALLY_EXPR. */
2132 result
= block
->init
;
2133 add_expr_to_chain (&result
, block
->code
, false);
2135 result
= build2_loc (input_location
, TRY_FINALLY_EXPR
, void_type_node
,
2136 result
, block
->cleanup
);
2138 /* Clear the block. */
2139 block
->init
= NULL_TREE
;
2140 block
->code
= NULL_TREE
;
2141 block
->cleanup
= NULL_TREE
;
2147 /* Helper function for marking a boolean expression tree as unlikely. */
2150 gfc_unlikely (tree cond
, enum br_predictor predictor
)
2156 cond
= fold_convert (long_integer_type_node
, cond
);
2157 tmp
= build_zero_cst (long_integer_type_node
);
2158 cond
= build_call_expr_loc (input_location
,
2159 builtin_decl_explicit (BUILT_IN_EXPECT
),
2161 build_int_cst (integer_type_node
,
2164 cond
= fold_convert (boolean_type_node
, cond
);
2169 /* Helper function for marking a boolean expression tree as likely. */
2172 gfc_likely (tree cond
, enum br_predictor predictor
)
2178 cond
= fold_convert (long_integer_type_node
, cond
);
2179 tmp
= build_one_cst (long_integer_type_node
);
2180 cond
= build_call_expr_loc (input_location
,
2181 builtin_decl_explicit (BUILT_IN_EXPECT
),
2183 build_int_cst (integer_type_node
,
2186 cond
= fold_convert (boolean_type_node
, cond
);
2191 /* Get the string length for a deferred character length component. */
2194 gfc_deferred_strlen (gfc_component
*c
, tree
*decl
)
2196 char name
[GFC_MAX_SYMBOL_LEN
+9];
2197 gfc_component
*strlen
;
2198 if (!(c
->ts
.type
== BT_CHARACTER
&& c
->ts
.deferred
))
2200 sprintf (name
, "_%s_length", c
->name
);
2201 for (strlen
= c
; strlen
; strlen
= strlen
->next
)
2202 if (strcmp (strlen
->name
, name
) == 0)
2204 *decl
= strlen
? strlen
->backend_decl
: NULL_TREE
;
2205 return strlen
!= NULL
;