1 /* Code translation -- generate GCC trees from gfc_code.
2 Copyright (C) 2002-2015 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"
28 #include "double-int.h"
36 #include "fold-const.h"
37 #include "gimple-expr.h" /* For create_tmp_var_raw. */
38 #include "stringpool.h"
39 #include "tree-iterator.h"
40 #include "diagnostic-core.h" /* For internal_error. */
43 #include "trans-stmt.h"
44 #include "trans-array.h"
45 #include "trans-types.h"
46 #include "trans-const.h"
48 /* Naming convention for backend interface code:
50 gfc_trans_* translate gfc_code into STMT trees.
52 gfc_conv_* expression conversion
54 gfc_get_* get a backend tree representation of a decl or type */
56 static gfc_file
*gfc_current_backend_file
;
58 const char gfc_msg_fault
[] = N_("Array reference out of bounds");
59 const char gfc_msg_wrong_return
[] = N_("Incorrect function return value");
62 /* Advance along TREE_CHAIN n times. */
65 gfc_advance_chain (tree t
, int n
)
69 gcc_assert (t
!= NULL_TREE
);
76 /* Strip off a legitimate source ending from the input
77 string NAME of length LEN. */
80 remove_suffix (char *name
, int len
)
84 for (i
= 2; i
< 8 && len
> i
; i
++)
86 if (name
[len
- i
] == '.')
95 /* Creates a variable declaration with a given TYPE. */
98 gfc_create_var_np (tree type
, const char *prefix
)
102 t
= create_tmp_var_raw (type
, prefix
);
104 /* No warnings for anonymous variables. */
106 TREE_NO_WARNING (t
) = 1;
112 /* Like above, but also adds it to the current scope. */
115 gfc_create_var (tree type
, const char *prefix
)
119 tmp
= gfc_create_var_np (type
, prefix
);
127 /* If the expression is not constant, evaluate it now. We assign the
128 result of the expression to an artificially created variable VAR, and
129 return a pointer to the VAR_DECL node for this variable. */
132 gfc_evaluate_now_loc (location_t loc
, tree expr
, stmtblock_t
* pblock
)
136 if (CONSTANT_CLASS_P (expr
))
139 var
= gfc_create_var (TREE_TYPE (expr
), NULL
);
140 gfc_add_modify_loc (loc
, pblock
, var
, expr
);
147 gfc_evaluate_now (tree expr
, stmtblock_t
* pblock
)
149 return gfc_evaluate_now_loc (input_location
, expr
, pblock
);
153 /* Build a MODIFY_EXPR node and add it to a given statement block PBLOCK.
154 A MODIFY_EXPR is an assignment:
158 gfc_add_modify_loc (location_t loc
, stmtblock_t
* pblock
, tree lhs
, tree rhs
)
162 #ifdef ENABLE_CHECKING
164 t1
= TREE_TYPE (rhs
);
165 t2
= TREE_TYPE (lhs
);
166 /* Make sure that the types of the rhs and the lhs are the same
167 for scalar assignments. We should probably have something
168 similar for aggregates, but right now removing that check just
169 breaks everything. */
171 || AGGREGATE_TYPE_P (TREE_TYPE (lhs
)));
174 tmp
= fold_build2_loc (loc
, MODIFY_EXPR
, void_type_node
, lhs
,
176 gfc_add_expr_to_block (pblock
, tmp
);
181 gfc_add_modify (stmtblock_t
* pblock
, tree lhs
, tree rhs
)
183 gfc_add_modify_loc (input_location
, pblock
, lhs
, rhs
);
187 /* Create a new scope/binding level and initialize a block. Care must be
188 taken when translating expressions as any temporaries will be placed in
189 the innermost scope. */
192 gfc_start_block (stmtblock_t
* block
)
194 /* Start a new binding level. */
196 block
->has_scope
= 1;
198 /* The block is empty. */
199 block
->head
= NULL_TREE
;
203 /* Initialize a block without creating a new scope. */
206 gfc_init_block (stmtblock_t
* block
)
208 block
->head
= NULL_TREE
;
209 block
->has_scope
= 0;
213 /* Sometimes we create a scope but it turns out that we don't actually
214 need it. This function merges the scope of BLOCK with its parent.
215 Only variable decls will be merged, you still need to add the code. */
218 gfc_merge_block_scope (stmtblock_t
* block
)
223 gcc_assert (block
->has_scope
);
224 block
->has_scope
= 0;
226 /* Remember the decls in this scope. */
230 /* Add them to the parent scope. */
231 while (decl
!= NULL_TREE
)
233 next
= DECL_CHAIN (decl
);
234 DECL_CHAIN (decl
) = NULL_TREE
;
242 /* Finish a scope containing a block of statements. */
245 gfc_finish_block (stmtblock_t
* stmtblock
)
251 expr
= stmtblock
->head
;
253 expr
= build_empty_stmt (input_location
);
255 stmtblock
->head
= NULL_TREE
;
257 if (stmtblock
->has_scope
)
263 block
= poplevel (1, 0);
264 expr
= build3_v (BIND_EXPR
, decl
, expr
, block
);
274 /* Build an ADDR_EXPR and cast the result to TYPE. If TYPE is NULL, the
275 natural type is used. */
278 gfc_build_addr_expr (tree type
, tree t
)
280 tree base_type
= TREE_TYPE (t
);
283 if (type
&& POINTER_TYPE_P (type
)
284 && TREE_CODE (base_type
) == ARRAY_TYPE
285 && TYPE_MAIN_VARIANT (TREE_TYPE (type
))
286 == TYPE_MAIN_VARIANT (TREE_TYPE (base_type
)))
288 tree min_val
= size_zero_node
;
289 tree type_domain
= TYPE_DOMAIN (base_type
);
290 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
291 min_val
= TYPE_MIN_VALUE (type_domain
);
292 t
= fold (build4_loc (input_location
, ARRAY_REF
, TREE_TYPE (type
),
293 t
, min_val
, NULL_TREE
, NULL_TREE
));
297 natural_type
= build_pointer_type (base_type
);
299 if (TREE_CODE (t
) == INDIRECT_REF
)
303 t
= TREE_OPERAND (t
, 0);
304 natural_type
= TREE_TYPE (t
);
308 tree base
= get_base_address (t
);
309 if (base
&& DECL_P (base
))
310 TREE_ADDRESSABLE (base
) = 1;
311 t
= fold_build1_loc (input_location
, ADDR_EXPR
, natural_type
, t
);
314 if (type
&& natural_type
!= type
)
315 t
= convert (type
, t
);
321 /* Build an ARRAY_REF with its natural type. */
324 gfc_build_array_ref (tree base
, tree offset
, tree decl
)
326 tree type
= TREE_TYPE (base
);
330 if (GFC_ARRAY_TYPE_P (type
) && GFC_TYPE_ARRAY_RANK (type
) == 0)
332 gcc_assert (GFC_TYPE_ARRAY_CORANK (type
) > 0);
334 return fold_convert (TYPE_MAIN_VARIANT (type
), base
);
337 /* Scalar coarray, there is nothing to do. */
338 if (TREE_CODE (type
) != ARRAY_TYPE
)
340 gcc_assert (decl
== NULL_TREE
);
341 gcc_assert (integer_zerop (offset
));
345 type
= TREE_TYPE (type
);
348 TREE_ADDRESSABLE (base
) = 1;
350 /* Strip NON_LVALUE_EXPR nodes. */
351 STRIP_TYPE_NOPS (offset
);
353 /* If the array reference is to a pointer, whose target contains a
354 subreference, use the span that is stored with the backend decl
355 and reference the element with pointer arithmetic. */
356 if (decl
&& (TREE_CODE (decl
) == FIELD_DECL
357 || TREE_CODE (decl
) == VAR_DECL
358 || TREE_CODE (decl
) == PARM_DECL
)
359 && ((GFC_DECL_SUBREF_ARRAY_P (decl
)
360 && !integer_zerop (GFC_DECL_SPAN(decl
)))
361 || GFC_DECL_CLASS (decl
)))
363 if (GFC_DECL_CLASS (decl
))
365 /* Allow for dummy arguments and other good things. */
366 if (POINTER_TYPE_P (TREE_TYPE (decl
)))
367 decl
= build_fold_indirect_ref_loc (input_location
, decl
);
369 /* Check if '_data' is an array descriptor. If it is not,
370 the array must be one of the components of the class object,
371 so return a normal array reference. */
372 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_class_data_get (decl
))))
373 return build4_loc (input_location
, ARRAY_REF
, type
, base
,
374 offset
, NULL_TREE
, NULL_TREE
);
376 span
= gfc_vtable_size_get (decl
);
378 else if (GFC_DECL_SUBREF_ARRAY_P (decl
))
379 span
= GFC_DECL_SPAN(decl
);
383 offset
= fold_build2_loc (input_location
, MULT_EXPR
,
384 gfc_array_index_type
,
386 tmp
= gfc_build_addr_expr (pvoid_type_node
, base
);
387 tmp
= fold_build_pointer_plus_loc (input_location
, tmp
, offset
);
388 tmp
= fold_convert (build_pointer_type (type
), tmp
);
389 if (!TYPE_STRING_FLAG (type
))
390 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
394 /* Otherwise use a straightforward array reference. */
395 return build4_loc (input_location
, ARRAY_REF
, type
, base
, offset
,
396 NULL_TREE
, NULL_TREE
);
400 /* Generate a call to print a runtime error possibly including multiple
401 arguments and a locus. */
404 trans_runtime_error_vararg (bool error
, locus
* where
, const char* msgid
,
417 /* Compute the number of extra arguments from the format string. */
418 for (p
= msgid
, nargs
= 0; *p
; p
++)
426 /* The code to generate the error. */
427 gfc_start_block (&block
);
431 line
= LOCATION_LINE (where
->lb
->location
);
432 message
= xasprintf ("At line %d of file %s", line
,
433 where
->lb
->file
->filename
);
436 message
= xasprintf ("In file '%s', around line %d",
437 gfc_source_file
, LOCATION_LINE (input_location
) + 1);
439 arg
= gfc_build_addr_expr (pchar_type_node
,
440 gfc_build_localized_cstring_const (message
));
443 message
= xasprintf ("%s", _(msgid
));
444 arg2
= gfc_build_addr_expr (pchar_type_node
,
445 gfc_build_localized_cstring_const (message
));
448 /* Build the argument array. */
449 argarray
= XALLOCAVEC (tree
, nargs
+ 2);
452 for (i
= 0; i
< nargs
; i
++)
453 argarray
[2 + i
] = va_arg (ap
, tree
);
455 /* Build the function call to runtime_(warning,error)_at; because of the
456 variable number of arguments, we can't use build_call_expr_loc dinput_location,
459 fntype
= TREE_TYPE (gfor_fndecl_runtime_error_at
);
461 fntype
= TREE_TYPE (gfor_fndecl_runtime_warning_at
);
463 loc
= where
? where
->lb
->location
: input_location
;
464 tmp
= fold_build_call_array_loc (loc
, TREE_TYPE (fntype
),
465 fold_build1_loc (loc
, ADDR_EXPR
,
466 build_pointer_type (fntype
),
468 ? gfor_fndecl_runtime_error_at
469 : gfor_fndecl_runtime_warning_at
),
470 nargs
+ 2, argarray
);
471 gfc_add_expr_to_block (&block
, tmp
);
473 return gfc_finish_block (&block
);
478 gfc_trans_runtime_error (bool error
, locus
* where
, const char* msgid
, ...)
483 va_start (ap
, msgid
);
484 result
= trans_runtime_error_vararg (error
, where
, msgid
, ap
);
490 /* Generate a runtime error if COND is true. */
493 gfc_trans_runtime_check (bool error
, bool once
, tree cond
, stmtblock_t
* pblock
,
494 locus
* where
, const char * msgid
, ...)
502 if (integer_zerop (cond
))
507 tmpvar
= gfc_create_var (boolean_type_node
, "print_warning");
508 TREE_STATIC (tmpvar
) = 1;
509 DECL_INITIAL (tmpvar
) = boolean_true_node
;
510 gfc_add_expr_to_block (pblock
, tmpvar
);
513 gfc_start_block (&block
);
515 /* For error, runtime_error_at already implies PRED_NORETURN. */
517 gfc_add_expr_to_block (&block
, build_predict_expr (PRED_FORTRAN_WARN_ONCE
,
520 /* The code to generate the error. */
521 va_start (ap
, msgid
);
522 gfc_add_expr_to_block (&block
,
523 trans_runtime_error_vararg (error
, where
,
528 gfc_add_modify (&block
, tmpvar
, boolean_false_node
);
530 body
= gfc_finish_block (&block
);
532 if (integer_onep (cond
))
534 gfc_add_expr_to_block (pblock
, body
);
539 cond
= fold_build2_loc (where
->lb
->location
, TRUTH_AND_EXPR
,
540 long_integer_type_node
, tmpvar
, cond
);
542 cond
= fold_convert (long_integer_type_node
, cond
);
544 tmp
= fold_build3_loc (where
->lb
->location
, COND_EXPR
, void_type_node
,
546 build_empty_stmt (where
->lb
->location
));
547 gfc_add_expr_to_block (pblock
, tmp
);
552 /* Call malloc to allocate size bytes of memory, with special conditions:
553 + if size == 0, return a malloced area of size 1,
554 + if malloc returns NULL, issue a runtime error. */
556 gfc_call_malloc (stmtblock_t
* block
, tree type
, tree size
)
558 tree tmp
, msg
, malloc_result
, null_result
, res
, malloc_tree
;
561 size
= gfc_evaluate_now (size
, block
);
563 if (TREE_TYPE (size
) != TREE_TYPE (size_type_node
))
564 size
= fold_convert (size_type_node
, size
);
566 /* Create a variable to hold the result. */
567 res
= gfc_create_var (prvoid_type_node
, NULL
);
570 gfc_start_block (&block2
);
572 size
= fold_build2_loc (input_location
, MAX_EXPR
, size_type_node
, size
,
573 build_int_cst (size_type_node
, 1));
575 malloc_tree
= builtin_decl_explicit (BUILT_IN_MALLOC
);
576 gfc_add_modify (&block2
, res
,
577 fold_convert (prvoid_type_node
,
578 build_call_expr_loc (input_location
,
579 malloc_tree
, 1, size
)));
581 /* Optionally check whether malloc was successful. */
582 if (gfc_option
.rtcheck
& GFC_RTCHECK_MEM
)
584 null_result
= fold_build2_loc (input_location
, EQ_EXPR
,
585 boolean_type_node
, res
,
586 build_int_cst (pvoid_type_node
, 0));
587 msg
= gfc_build_addr_expr (pchar_type_node
,
588 gfc_build_localized_cstring_const ("Memory allocation failed"));
589 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
591 build_call_expr_loc (input_location
,
592 gfor_fndecl_os_error
, 1, msg
),
593 build_empty_stmt (input_location
));
594 gfc_add_expr_to_block (&block2
, tmp
);
597 malloc_result
= gfc_finish_block (&block2
);
599 gfc_add_expr_to_block (block
, malloc_result
);
602 res
= fold_convert (type
, res
);
607 /* Allocate memory, using an optional status argument.
609 This function follows the following pseudo-code:
612 allocate (size_t size, integer_type stat)
619 newmem = malloc (MAX (size, 1));
623 *stat = LIBERROR_ALLOCATION;
625 runtime_error ("Allocation would exceed memory limit");
630 gfc_allocate_using_malloc (stmtblock_t
* block
, tree pointer
,
631 tree size
, tree status
)
633 tree tmp
, error_cond
;
634 stmtblock_t on_error
;
635 tree status_type
= status
? TREE_TYPE (status
) : NULL_TREE
;
637 /* Evaluate size only once, and make sure it has the right type. */
638 size
= gfc_evaluate_now (size
, block
);
639 if (TREE_TYPE (size
) != TREE_TYPE (size_type_node
))
640 size
= fold_convert (size_type_node
, size
);
642 /* If successful and stat= is given, set status to 0. */
643 if (status
!= NULL_TREE
)
644 gfc_add_expr_to_block (block
,
645 fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
,
646 status
, build_int_cst (status_type
, 0)));
648 /* The allocation itself. */
649 gfc_add_modify (block
, pointer
,
650 fold_convert (TREE_TYPE (pointer
),
651 build_call_expr_loc (input_location
,
652 builtin_decl_explicit (BUILT_IN_MALLOC
), 1,
653 fold_build2_loc (input_location
,
654 MAX_EXPR
, size_type_node
, size
,
655 build_int_cst (size_type_node
, 1)))));
657 /* What to do in case of error. */
658 gfc_start_block (&on_error
);
659 if (status
!= NULL_TREE
)
661 gfc_add_expr_to_block (&on_error
,
662 build_predict_expr (PRED_FORTRAN_FAIL_ALLOC
,
664 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
, status
,
665 build_int_cst (status_type
, LIBERROR_ALLOCATION
));
666 gfc_add_expr_to_block (&on_error
, tmp
);
670 /* Here, os_error already implies PRED_NORETURN. */
671 tmp
= build_call_expr_loc (input_location
, gfor_fndecl_os_error
, 1,
672 gfc_build_addr_expr (pchar_type_node
,
673 gfc_build_localized_cstring_const
674 ("Allocation would exceed memory limit")));
675 gfc_add_expr_to_block (&on_error
, tmp
);
678 error_cond
= fold_build2_loc (input_location
, EQ_EXPR
,
679 boolean_type_node
, pointer
,
680 build_int_cst (prvoid_type_node
, 0));
681 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
682 error_cond
, gfc_finish_block (&on_error
),
683 build_empty_stmt (input_location
));
685 gfc_add_expr_to_block (block
, tmp
);
689 /* Allocate memory, using an optional status argument.
691 This function follows the following pseudo-code:
694 allocate (size_t size, void** token, int *stat, char* errmsg, int errlen)
698 newmem = _caf_register (size, regtype, token, &stat, errmsg, errlen);
702 gfc_allocate_using_lib (stmtblock_t
* block
, tree pointer
, tree size
,
703 tree token
, tree status
, tree errmsg
, tree errlen
)
707 gcc_assert (token
!= NULL_TREE
);
709 /* Evaluate size only once, and make sure it has the right type. */
710 size
= gfc_evaluate_now (size
, block
);
711 if (TREE_TYPE (size
) != TREE_TYPE (size_type_node
))
712 size
= fold_convert (size_type_node
, size
);
714 /* The allocation itself. */
715 if (status
== NULL_TREE
)
716 pstat
= null_pointer_node
;
718 pstat
= gfc_build_addr_expr (NULL_TREE
, status
);
720 if (errmsg
== NULL_TREE
)
722 gcc_assert(errlen
== NULL_TREE
);
723 errmsg
= null_pointer_node
;
724 errlen
= build_int_cst (integer_type_node
, 0);
727 tmp
= build_call_expr_loc (input_location
,
728 gfor_fndecl_caf_register
, 6,
729 fold_build2_loc (input_location
,
730 MAX_EXPR
, size_type_node
, size
,
731 build_int_cst (size_type_node
, 1)),
732 build_int_cst (integer_type_node
,
733 GFC_CAF_COARRAY_ALLOC
),
734 token
, pstat
, errmsg
, errlen
);
736 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
737 TREE_TYPE (pointer
), pointer
,
738 fold_convert ( TREE_TYPE (pointer
), tmp
));
739 gfc_add_expr_to_block (block
, tmp
);
743 /* Generate code for an ALLOCATE statement when the argument is an
744 allocatable variable. If the variable is currently allocated, it is an
745 error to allocate it again.
747 This function follows the following pseudo-code:
750 allocate_allocatable (void *mem, size_t size, integer_type stat)
753 return allocate (size, stat);
757 stat = LIBERROR_ALLOCATION;
759 runtime_error ("Attempting to allocate already allocated variable");
763 expr must be set to the original expression being allocated for its locus
764 and variable name in case a runtime error has to be printed. */
766 gfc_allocate_allocatable (stmtblock_t
* block
, tree mem
, tree size
, tree token
,
767 tree status
, tree errmsg
, tree errlen
, tree label_finish
,
770 stmtblock_t alloc_block
;
771 tree tmp
, null_mem
, alloc
, error
;
772 tree type
= TREE_TYPE (mem
);
774 if (TREE_TYPE (size
) != TREE_TYPE (size_type_node
))
775 size
= fold_convert (size_type_node
, size
);
777 null_mem
= gfc_unlikely (fold_build2_loc (input_location
, NE_EXPR
,
778 boolean_type_node
, mem
,
779 build_int_cst (type
, 0)),
780 PRED_FORTRAN_FAIL_ALLOC
);
782 /* If mem is NULL, we call gfc_allocate_using_malloc or
783 gfc_allocate_using_lib. */
784 gfc_start_block (&alloc_block
);
786 if (flag_coarray
== GFC_FCOARRAY_LIB
787 && gfc_expr_attr (expr
).codimension
)
791 gfc_allocate_using_lib (&alloc_block
, mem
, size
, token
, status
,
793 if (status
!= NULL_TREE
)
795 TREE_USED (label_finish
) = 1;
796 tmp
= build1_v (GOTO_EXPR
, label_finish
);
797 cond
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
798 status
, build_zero_cst (TREE_TYPE (status
)));
799 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
800 gfc_unlikely (cond
, PRED_FORTRAN_FAIL_ALLOC
),
801 tmp
, build_empty_stmt (input_location
));
802 gfc_add_expr_to_block (&alloc_block
, tmp
);
806 gfc_allocate_using_malloc (&alloc_block
, mem
, size
, status
);
808 alloc
= gfc_finish_block (&alloc_block
);
810 /* If mem is not NULL, we issue a runtime error or set the
816 gcc_assert (expr
->expr_type
== EXPR_VARIABLE
&& expr
->symtree
);
817 varname
= gfc_build_cstring_const (expr
->symtree
->name
);
818 varname
= gfc_build_addr_expr (pchar_type_node
, varname
);
820 error
= gfc_trans_runtime_error (true, &expr
->where
,
821 "Attempting to allocate already"
822 " allocated variable '%s'",
826 error
= gfc_trans_runtime_error (true, NULL
,
827 "Attempting to allocate already allocated"
830 if (status
!= NULL_TREE
)
832 tree status_type
= TREE_TYPE (status
);
834 error
= fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
,
835 status
, build_int_cst (status_type
, LIBERROR_ALLOCATION
));
838 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, null_mem
,
840 gfc_add_expr_to_block (block
, tmp
);
844 /* Free a given variable, if it's not NULL. */
846 gfc_call_free (tree var
)
849 tree tmp
, cond
, call
;
851 if (TREE_TYPE (var
) != TREE_TYPE (pvoid_type_node
))
852 var
= fold_convert (pvoid_type_node
, var
);
854 gfc_start_block (&block
);
855 var
= gfc_evaluate_now (var
, &block
);
856 cond
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
, var
,
857 build_int_cst (pvoid_type_node
, 0));
858 call
= build_call_expr_loc (input_location
,
859 builtin_decl_explicit (BUILT_IN_FREE
),
861 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
, call
,
862 build_empty_stmt (input_location
));
863 gfc_add_expr_to_block (&block
, tmp
);
865 return gfc_finish_block (&block
);
869 /* Build a call to a FINAL procedure, which finalizes "var". */
872 gfc_build_final_call (gfc_typespec ts
, gfc_expr
*final_wrapper
, gfc_expr
*var
,
873 bool fini_coarray
, gfc_expr
*class_size
)
877 tree final_fndecl
, array
, size
, tmp
;
878 symbol_attribute attr
;
880 gcc_assert (final_wrapper
->expr_type
== EXPR_VARIABLE
);
883 gfc_start_block (&block
);
884 gfc_init_se (&se
, NULL
);
885 gfc_conv_expr (&se
, final_wrapper
);
886 final_fndecl
= se
.expr
;
887 if (POINTER_TYPE_P (TREE_TYPE (final_fndecl
)))
888 final_fndecl
= build_fold_indirect_ref_loc (input_location
, final_fndecl
);
890 if (ts
.type
== BT_DERIVED
)
894 gcc_assert (!class_size
);
895 elem_size
= gfc_typenode_for_spec (&ts
);
896 elem_size
= TYPE_SIZE_UNIT (elem_size
);
897 size
= fold_convert (gfc_array_index_type
, elem_size
);
899 gfc_init_se (&se
, NULL
);
903 se
.descriptor_only
= 1;
904 gfc_conv_expr_descriptor (&se
, var
);
909 gfc_conv_expr (&se
, var
);
910 gcc_assert (se
.pre
.head
== NULL_TREE
&& se
.post
.head
== NULL_TREE
);
913 /* No copy back needed, hence set attr's allocatable/pointer
915 gfc_clear_attr (&attr
);
916 gfc_init_se (&se
, NULL
);
917 array
= gfc_conv_scalar_to_descriptor (&se
, array
, attr
);
918 gcc_assert (se
.post
.head
== NULL_TREE
);
923 gfc_expr
*array_expr
;
924 gcc_assert (class_size
);
925 gfc_init_se (&se
, NULL
);
926 gfc_conv_expr (&se
, class_size
);
927 gfc_add_block_to_block (&block
, &se
.pre
);
928 gcc_assert (se
.post
.head
== NULL_TREE
);
931 array_expr
= gfc_copy_expr (var
);
932 gfc_init_se (&se
, NULL
);
934 if (array_expr
->rank
)
936 gfc_add_class_array_ref (array_expr
);
937 se
.descriptor_only
= 1;
938 gfc_conv_expr_descriptor (&se
, array_expr
);
943 gfc_add_data_component (array_expr
);
944 gfc_conv_expr (&se
, array_expr
);
945 gfc_add_block_to_block (&block
, &se
.pre
);
946 gcc_assert (se
.post
.head
== NULL_TREE
);
948 if (TREE_CODE (array
) == ADDR_EXPR
949 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (array
, 0))))
950 tmp
= TREE_OPERAND (array
, 0);
952 if (!gfc_is_coarray (array_expr
))
954 /* No copy back needed, hence set attr's allocatable/pointer
956 gfc_clear_attr (&attr
);
957 gfc_init_se (&se
, NULL
);
958 array
= gfc_conv_scalar_to_descriptor (&se
, array
, attr
);
960 gcc_assert (se
.post
.head
== NULL_TREE
);
962 gfc_free_expr (array_expr
);
965 if (!POINTER_TYPE_P (TREE_TYPE (array
)))
966 array
= gfc_build_addr_expr (NULL
, array
);
968 gfc_add_block_to_block (&block
, &se
.pre
);
969 tmp
= build_call_expr_loc (input_location
,
970 final_fndecl
, 3, array
,
971 size
, fini_coarray
? boolean_true_node
972 : boolean_false_node
);
973 gfc_add_block_to_block (&block
, &se
.post
);
974 gfc_add_expr_to_block (&block
, tmp
);
975 return gfc_finish_block (&block
);
980 gfc_add_comp_finalizer_call (stmtblock_t
*block
, tree decl
, gfc_component
*comp
,
985 tree final_fndecl
, size
, array
, tmp
, cond
;
986 symbol_attribute attr
;
987 gfc_expr
*final_expr
= NULL
;
989 if (comp
->ts
.type
!= BT_DERIVED
&& comp
->ts
.type
!= BT_CLASS
)
992 gfc_init_block (&block2
);
994 if (comp
->ts
.type
== BT_DERIVED
)
996 if (comp
->attr
.pointer
)
999 gfc_is_finalizable (comp
->ts
.u
.derived
, &final_expr
);
1003 gfc_init_se (&se
, NULL
);
1004 gfc_conv_expr (&se
, final_expr
);
1005 final_fndecl
= se
.expr
;
1006 size
= gfc_typenode_for_spec (&comp
->ts
);
1007 size
= TYPE_SIZE_UNIT (size
);
1008 size
= fold_convert (gfc_array_index_type
, size
);
1012 else /* comp->ts.type == BT_CLASS. */
1014 if (CLASS_DATA (comp
)->attr
.class_pointer
)
1017 gfc_is_finalizable (CLASS_DATA (comp
)->ts
.u
.derived
, &final_expr
);
1018 final_fndecl
= gfc_vtable_final_get (decl
);
1019 size
= gfc_vtable_size_get (decl
);
1020 array
= gfc_class_data_get (decl
);
1023 if (comp
->attr
.allocatable
1024 || (comp
->ts
.type
== BT_CLASS
&& CLASS_DATA (comp
)->attr
.allocatable
))
1026 tmp
= GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array
))
1027 ? gfc_conv_descriptor_data_get (array
) : array
;
1028 cond
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
1029 tmp
, fold_convert (TREE_TYPE (tmp
),
1030 null_pointer_node
));
1033 cond
= boolean_true_node
;
1035 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array
)))
1037 gfc_clear_attr (&attr
);
1038 gfc_init_se (&se
, NULL
);
1039 array
= gfc_conv_scalar_to_descriptor (&se
, array
, attr
);
1040 gfc_add_block_to_block (&block2
, &se
.pre
);
1041 gcc_assert (se
.post
.head
== NULL_TREE
);
1044 if (!POINTER_TYPE_P (TREE_TYPE (array
)))
1045 array
= gfc_build_addr_expr (NULL
, array
);
1049 tmp
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
1051 fold_convert (TREE_TYPE (final_fndecl
),
1052 null_pointer_node
));
1053 cond
= fold_build2_loc (input_location
, TRUTH_ANDIF_EXPR
,
1054 boolean_type_node
, cond
, tmp
);
1057 if (POINTER_TYPE_P (TREE_TYPE (final_fndecl
)))
1058 final_fndecl
= build_fold_indirect_ref_loc (input_location
, final_fndecl
);
1060 tmp
= build_call_expr_loc (input_location
,
1061 final_fndecl
, 3, array
,
1062 size
, fini_coarray
? boolean_true_node
1063 : boolean_false_node
);
1064 gfc_add_expr_to_block (&block2
, tmp
);
1065 tmp
= gfc_finish_block (&block2
);
1067 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
, tmp
,
1068 build_empty_stmt (input_location
));
1069 gfc_add_expr_to_block (block
, tmp
);
1075 /* Add a call to the finalizer, using the passed *expr. Returns
1076 true when a finalizer call has been inserted. */
1079 gfc_add_finalizer_call (stmtblock_t
*block
, gfc_expr
*expr2
)
1084 gfc_expr
*final_expr
= NULL
;
1085 gfc_expr
*elem_size
= NULL
;
1086 bool has_finalizer
= false;
1088 if (!expr2
|| (expr2
->ts
.type
!= BT_DERIVED
&& expr2
->ts
.type
!= BT_CLASS
))
1091 if (expr2
->ts
.type
== BT_DERIVED
)
1093 gfc_is_finalizable (expr2
->ts
.u
.derived
, &final_expr
);
1098 /* If we have a class array, we need go back to the class
1100 expr
= gfc_copy_expr (expr2
);
1102 if (expr
->ref
&& expr
->ref
->next
&& !expr
->ref
->next
->next
1103 && expr
->ref
->next
->type
== REF_ARRAY
1104 && expr
->ref
->type
== REF_COMPONENT
1105 && strcmp (expr
->ref
->u
.c
.component
->name
, "_data") == 0)
1107 gfc_free_ref_list (expr
->ref
);
1111 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
1112 if (ref
->next
&& ref
->next
->next
&& !ref
->next
->next
->next
1113 && ref
->next
->next
->type
== REF_ARRAY
1114 && ref
->next
->type
== REF_COMPONENT
1115 && strcmp (ref
->next
->u
.c
.component
->name
, "_data") == 0)
1117 gfc_free_ref_list (ref
->next
);
1121 if (expr
->ts
.type
== BT_CLASS
)
1123 has_finalizer
= gfc_is_finalizable (expr
->ts
.u
.derived
, NULL
);
1125 if (!expr2
->rank
&& !expr2
->ref
&& CLASS_DATA (expr2
->symtree
->n
.sym
)->as
)
1126 expr
->rank
= CLASS_DATA (expr2
->symtree
->n
.sym
)->as
->rank
;
1128 final_expr
= gfc_copy_expr (expr
);
1129 gfc_add_vptr_component (final_expr
);
1130 gfc_add_component_ref (final_expr
, "_final");
1132 elem_size
= gfc_copy_expr (expr
);
1133 gfc_add_vptr_component (elem_size
);
1134 gfc_add_component_ref (elem_size
, "_size");
1137 gcc_assert (final_expr
->expr_type
== EXPR_VARIABLE
);
1139 tmp
= gfc_build_final_call (expr
->ts
, final_expr
, expr
,
1142 if (expr
->ts
.type
== BT_CLASS
&& !has_finalizer
)
1147 gfc_init_se (&se
, NULL
);
1148 se
.want_pointer
= 1;
1149 gfc_conv_expr (&se
, final_expr
);
1150 cond
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
1151 se
.expr
, build_int_cst (TREE_TYPE (se
.expr
), 0));
1153 /* For CLASS(*) not only sym->_vtab->_final can be NULL
1154 but already sym->_vtab itself. */
1155 if (UNLIMITED_POLY (expr
))
1158 gfc_expr
*vptr_expr
;
1160 vptr_expr
= gfc_copy_expr (expr
);
1161 gfc_add_vptr_component (vptr_expr
);
1163 gfc_init_se (&se
, NULL
);
1164 se
.want_pointer
= 1;
1165 gfc_conv_expr (&se
, vptr_expr
);
1166 gfc_free_expr (vptr_expr
);
1168 cond2
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
1170 build_int_cst (TREE_TYPE (se
.expr
), 0));
1171 cond
= fold_build2_loc (input_location
, TRUTH_ANDIF_EXPR
,
1172 boolean_type_node
, cond2
, cond
);
1175 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
1176 cond
, tmp
, build_empty_stmt (input_location
));
1179 gfc_add_expr_to_block (block
, tmp
);
1185 /* User-deallocate; we emit the code directly from the front-end, and the
1186 logic is the same as the previous library function:
1189 deallocate (void *pointer, GFC_INTEGER_4 * stat)
1196 runtime_error ("Attempt to DEALLOCATE unallocated memory.");
1206 In this front-end version, status doesn't have to be GFC_INTEGER_4.
1207 Moreover, if CAN_FAIL is true, then we will not emit a runtime error,
1208 even when no status variable is passed to us (this is used for
1209 unconditional deallocation generated by the front-end at end of
1212 If a runtime-message is possible, `expr' must point to the original
1213 expression being deallocated for its locus and variable name.
1215 For coarrays, "pointer" must be the array descriptor and not its
1216 "data" component. */
1218 gfc_deallocate_with_status (tree pointer
, tree status
, tree errmsg
,
1219 tree errlen
, tree label_finish
,
1220 bool can_fail
, gfc_expr
* expr
, bool coarray
)
1222 stmtblock_t null
, non_null
;
1223 tree cond
, tmp
, error
;
1224 tree status_type
= NULL_TREE
;
1225 tree caf_decl
= NULL_TREE
;
1229 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (pointer
)));
1231 pointer
= gfc_conv_descriptor_data_get (caf_decl
);
1232 STRIP_NOPS (pointer
);
1235 cond
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
, pointer
,
1236 build_int_cst (TREE_TYPE (pointer
), 0));
1238 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
1239 we emit a runtime error. */
1240 gfc_start_block (&null
);
1245 gcc_assert (expr
&& expr
->expr_type
== EXPR_VARIABLE
&& expr
->symtree
);
1247 varname
= gfc_build_cstring_const (expr
->symtree
->name
);
1248 varname
= gfc_build_addr_expr (pchar_type_node
, varname
);
1250 error
= gfc_trans_runtime_error (true, &expr
->where
,
1251 "Attempt to DEALLOCATE unallocated '%s'",
1255 error
= build_empty_stmt (input_location
);
1257 if (status
!= NULL_TREE
&& !integer_zerop (status
))
1261 status_type
= TREE_TYPE (TREE_TYPE (status
));
1262 cond2
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
1263 status
, build_int_cst (TREE_TYPE (status
), 0));
1264 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
,
1265 fold_build1_loc (input_location
, INDIRECT_REF
,
1266 status_type
, status
),
1267 build_int_cst (status_type
, 1));
1268 error
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
1272 gfc_add_expr_to_block (&null
, error
);
1274 /* When POINTER is not NULL, we free it. */
1275 gfc_start_block (&non_null
);
1276 gfc_add_finalizer_call (&non_null
, expr
);
1277 if (!coarray
|| flag_coarray
!= GFC_FCOARRAY_LIB
)
1279 tmp
= build_call_expr_loc (input_location
,
1280 builtin_decl_explicit (BUILT_IN_FREE
), 1,
1281 fold_convert (pvoid_type_node
, pointer
));
1282 gfc_add_expr_to_block (&non_null
, tmp
);
1284 if (status
!= NULL_TREE
&& !integer_zerop (status
))
1286 /* We set STATUS to zero if it is present. */
1287 tree status_type
= TREE_TYPE (TREE_TYPE (status
));
1290 cond2
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
1292 build_int_cst (TREE_TYPE (status
), 0));
1293 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
,
1294 fold_build1_loc (input_location
, INDIRECT_REF
,
1295 status_type
, status
),
1296 build_int_cst (status_type
, 0));
1297 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
1298 gfc_unlikely (cond2
, PRED_FORTRAN_FAIL_ALLOC
),
1299 tmp
, build_empty_stmt (input_location
));
1300 gfc_add_expr_to_block (&non_null
, tmp
);
1305 tree caf_type
, token
, cond2
;
1306 tree pstat
= null_pointer_node
;
1308 if (errmsg
== NULL_TREE
)
1310 gcc_assert (errlen
== NULL_TREE
);
1311 errmsg
= null_pointer_node
;
1312 errlen
= build_zero_cst (integer_type_node
);
1316 gcc_assert (errlen
!= NULL_TREE
);
1317 if (!POINTER_TYPE_P (TREE_TYPE (errmsg
)))
1318 errmsg
= gfc_build_addr_expr (NULL_TREE
, errmsg
);
1321 caf_type
= TREE_TYPE (caf_decl
);
1323 if (status
!= NULL_TREE
&& !integer_zerop (status
))
1325 gcc_assert (status_type
== integer_type_node
);
1329 if (GFC_DESCRIPTOR_TYPE_P (caf_type
)
1330 && GFC_TYPE_ARRAY_AKIND (caf_type
) == GFC_ARRAY_ALLOCATABLE
)
1331 token
= gfc_conv_descriptor_token (caf_decl
);
1332 else if (DECL_LANG_SPECIFIC (caf_decl
)
1333 && GFC_DECL_TOKEN (caf_decl
) != NULL_TREE
)
1334 token
= GFC_DECL_TOKEN (caf_decl
);
1337 gcc_assert (GFC_ARRAY_TYPE_P (caf_type
)
1338 && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
) != NULL_TREE
);
1339 token
= GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
);
1342 token
= gfc_build_addr_expr (NULL_TREE
, token
);
1343 tmp
= build_call_expr_loc (input_location
,
1344 gfor_fndecl_caf_deregister
, 4,
1345 token
, pstat
, errmsg
, errlen
);
1346 gfc_add_expr_to_block (&non_null
, tmp
);
1348 if (status
!= NULL_TREE
)
1350 tree stat
= build_fold_indirect_ref_loc (input_location
, status
);
1352 TREE_USED (label_finish
) = 1;
1353 tmp
= build1_v (GOTO_EXPR
, label_finish
);
1354 cond2
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
1355 stat
, build_zero_cst (TREE_TYPE (stat
)));
1356 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
1357 gfc_unlikely (cond2
, PRED_FORTRAN_FAIL_ALLOC
),
1358 tmp
, build_empty_stmt (input_location
));
1359 gfc_add_expr_to_block (&non_null
, tmp
);
1363 return fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
,
1364 gfc_finish_block (&null
),
1365 gfc_finish_block (&non_null
));
1369 /* Generate code for deallocation of allocatable scalars (variables or
1370 components). Before the object itself is freed, any allocatable
1371 subcomponents are being deallocated. */
1374 gfc_deallocate_scalar_with_status (tree pointer
, tree status
, bool can_fail
,
1375 gfc_expr
* expr
, gfc_typespec ts
)
1377 stmtblock_t null
, non_null
;
1378 tree cond
, tmp
, error
;
1381 cond
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
, pointer
,
1382 build_int_cst (TREE_TYPE (pointer
), 0));
1384 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
1385 we emit a runtime error. */
1386 gfc_start_block (&null
);
1391 gcc_assert (expr
&& expr
->expr_type
== EXPR_VARIABLE
&& expr
->symtree
);
1393 varname
= gfc_build_cstring_const (expr
->symtree
->name
);
1394 varname
= gfc_build_addr_expr (pchar_type_node
, varname
);
1396 error
= gfc_trans_runtime_error (true, &expr
->where
,
1397 "Attempt to DEALLOCATE unallocated '%s'",
1401 error
= build_empty_stmt (input_location
);
1403 if (status
!= NULL_TREE
&& !integer_zerop (status
))
1405 tree status_type
= TREE_TYPE (TREE_TYPE (status
));
1408 cond2
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
1409 status
, build_int_cst (TREE_TYPE (status
), 0));
1410 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
,
1411 fold_build1_loc (input_location
, INDIRECT_REF
,
1412 status_type
, status
),
1413 build_int_cst (status_type
, 1));
1414 error
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
1418 gfc_add_expr_to_block (&null
, error
);
1420 /* When POINTER is not NULL, we free it. */
1421 gfc_start_block (&non_null
);
1423 /* Free allocatable components. */
1424 finalizable
= gfc_add_finalizer_call (&non_null
, expr
);
1425 if (!finalizable
&& ts
.type
== BT_DERIVED
&& ts
.u
.derived
->attr
.alloc_comp
)
1427 tmp
= build_fold_indirect_ref_loc (input_location
, pointer
);
1428 tmp
= gfc_deallocate_alloc_comp (ts
.u
.derived
, tmp
, 0);
1429 gfc_add_expr_to_block (&non_null
, tmp
);
1432 tmp
= build_call_expr_loc (input_location
,
1433 builtin_decl_explicit (BUILT_IN_FREE
), 1,
1434 fold_convert (pvoid_type_node
, pointer
));
1435 gfc_add_expr_to_block (&non_null
, tmp
);
1437 if (status
!= NULL_TREE
&& !integer_zerop (status
))
1439 /* We set STATUS to zero if it is present. */
1440 tree status_type
= TREE_TYPE (TREE_TYPE (status
));
1443 cond2
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
1444 status
, build_int_cst (TREE_TYPE (status
), 0));
1445 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, status_type
,
1446 fold_build1_loc (input_location
, INDIRECT_REF
,
1447 status_type
, status
),
1448 build_int_cst (status_type
, 0));
1449 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond2
,
1450 tmp
, build_empty_stmt (input_location
));
1451 gfc_add_expr_to_block (&non_null
, tmp
);
1454 return fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
,
1455 gfc_finish_block (&null
),
1456 gfc_finish_block (&non_null
));
1460 /* Reallocate MEM so it has SIZE bytes of data. This behaves like the
1461 following pseudo-code:
1464 internal_realloc (void *mem, size_t size)
1466 res = realloc (mem, size);
1467 if (!res && size != 0)
1468 _gfortran_os_error ("Allocation would exceed memory limit");
1473 gfc_call_realloc (stmtblock_t
* block
, tree mem
, tree size
)
1475 tree msg
, res
, nonzero
, null_result
, tmp
;
1476 tree type
= TREE_TYPE (mem
);
1478 size
= gfc_evaluate_now (size
, block
);
1480 if (TREE_TYPE (size
) != TREE_TYPE (size_type_node
))
1481 size
= fold_convert (size_type_node
, size
);
1483 /* Create a variable to hold the result. */
1484 res
= gfc_create_var (type
, NULL
);
1486 /* Call realloc and check the result. */
1487 tmp
= build_call_expr_loc (input_location
,
1488 builtin_decl_explicit (BUILT_IN_REALLOC
), 2,
1489 fold_convert (pvoid_type_node
, mem
), size
);
1490 gfc_add_modify (block
, res
, fold_convert (type
, tmp
));
1491 null_result
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
,
1492 res
, build_int_cst (pvoid_type_node
, 0));
1493 nonzero
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
, size
,
1494 build_int_cst (size_type_node
, 0));
1495 null_result
= fold_build2_loc (input_location
, TRUTH_AND_EXPR
, boolean_type_node
,
1496 null_result
, nonzero
);
1497 msg
= gfc_build_addr_expr (pchar_type_node
, gfc_build_localized_cstring_const
1498 ("Allocation would exceed memory limit"));
1499 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
1501 build_call_expr_loc (input_location
,
1502 gfor_fndecl_os_error
, 1, msg
),
1503 build_empty_stmt (input_location
));
1504 gfc_add_expr_to_block (block
, tmp
);
1510 /* Add an expression to another one, either at the front or the back. */
1513 add_expr_to_chain (tree
* chain
, tree expr
, bool front
)
1515 if (expr
== NULL_TREE
|| IS_EMPTY_STMT (expr
))
1520 if (TREE_CODE (*chain
) != STATEMENT_LIST
)
1526 append_to_statement_list (tmp
, chain
);
1531 tree_stmt_iterator i
;
1533 i
= tsi_start (*chain
);
1534 tsi_link_before (&i
, expr
, TSI_CONTINUE_LINKING
);
1537 append_to_statement_list (expr
, chain
);
1544 /* Add a statement at the end of a block. */
1547 gfc_add_expr_to_block (stmtblock_t
* block
, tree expr
)
1550 add_expr_to_chain (&block
->head
, expr
, false);
1554 /* Add a statement at the beginning of a block. */
1557 gfc_prepend_expr_to_block (stmtblock_t
* block
, tree expr
)
1560 add_expr_to_chain (&block
->head
, expr
, true);
1564 /* Add a block the end of a block. */
1567 gfc_add_block_to_block (stmtblock_t
* block
, stmtblock_t
* append
)
1569 gcc_assert (append
);
1570 gcc_assert (!append
->has_scope
);
1572 gfc_add_expr_to_block (block
, append
->head
);
1573 append
->head
= NULL_TREE
;
1577 /* Save the current locus. The structure may not be complete, and should
1578 only be used with gfc_restore_backend_locus. */
1581 gfc_save_backend_locus (locus
* loc
)
1583 loc
->lb
= XCNEW (gfc_linebuf
);
1584 loc
->lb
->location
= input_location
;
1585 loc
->lb
->file
= gfc_current_backend_file
;
1589 /* Set the current locus. */
1592 gfc_set_backend_locus (locus
* loc
)
1594 gfc_current_backend_file
= loc
->lb
->file
;
1595 input_location
= loc
->lb
->location
;
1599 /* Restore the saved locus. Only used in conjunction with
1600 gfc_save_backend_locus, to free the memory when we are done. */
1603 gfc_restore_backend_locus (locus
* loc
)
1605 gfc_set_backend_locus (loc
);
1610 /* Translate an executable statement. The tree cond is used by gfc_trans_do.
1611 This static function is wrapped by gfc_trans_code_cond and
1615 trans_code (gfc_code
* code
, tree cond
)
1621 return build_empty_stmt (input_location
);
1623 gfc_start_block (&block
);
1625 /* Translate statements one by one into GENERIC trees until we reach
1626 the end of this gfc_code branch. */
1627 for (; code
; code
= code
->next
)
1629 if (code
->here
!= 0)
1631 res
= gfc_trans_label_here (code
);
1632 gfc_add_expr_to_block (&block
, res
);
1635 gfc_set_backend_locus (&code
->loc
);
1640 case EXEC_END_BLOCK
:
1641 case EXEC_END_NESTED_BLOCK
:
1642 case EXEC_END_PROCEDURE
:
1647 if (code
->expr1
->ts
.type
== BT_CLASS
)
1648 res
= gfc_trans_class_assign (code
->expr1
, code
->expr2
, code
->op
);
1650 res
= gfc_trans_assign (code
);
1653 case EXEC_LABEL_ASSIGN
:
1654 res
= gfc_trans_label_assign (code
);
1657 case EXEC_POINTER_ASSIGN
:
1658 if (code
->expr1
->ts
.type
== BT_CLASS
)
1659 res
= gfc_trans_class_assign (code
->expr1
, code
->expr2
, code
->op
);
1660 else if (UNLIMITED_POLY (code
->expr2
)
1661 && code
->expr1
->ts
.type
== BT_DERIVED
1662 && (code
->expr1
->ts
.u
.derived
->attr
.sequence
1663 || code
->expr1
->ts
.u
.derived
->attr
.is_bind_c
))
1665 res
= gfc_trans_class_assign (code
->expr1
, code
->expr2
, code
->op
);
1667 res
= gfc_trans_pointer_assign (code
);
1670 case EXEC_INIT_ASSIGN
:
1671 if (code
->expr1
->ts
.type
== BT_CLASS
)
1672 res
= gfc_trans_class_init_assign (code
);
1674 res
= gfc_trans_init_assign (code
);
1682 res
= gfc_trans_critical (code
);
1686 res
= gfc_trans_cycle (code
);
1690 res
= gfc_trans_exit (code
);
1694 res
= gfc_trans_goto (code
);
1698 res
= gfc_trans_entry (code
);
1702 res
= gfc_trans_pause (code
);
1706 case EXEC_ERROR_STOP
:
1707 res
= gfc_trans_stop (code
, code
->op
== EXEC_ERROR_STOP
);
1711 /* For MVBITS we've got the special exception that we need a
1712 dependency check, too. */
1714 bool is_mvbits
= false;
1716 if (code
->resolved_isym
)
1718 res
= gfc_conv_intrinsic_subroutine (code
);
1719 if (res
!= NULL_TREE
)
1723 if (code
->resolved_isym
1724 && code
->resolved_isym
->id
== GFC_ISYM_MVBITS
)
1727 res
= gfc_trans_call (code
, is_mvbits
, NULL_TREE
,
1733 res
= gfc_trans_call (code
, false, NULL_TREE
,
1737 case EXEC_ASSIGN_CALL
:
1738 res
= gfc_trans_call (code
, true, NULL_TREE
,
1743 res
= gfc_trans_return (code
);
1747 res
= gfc_trans_if (code
);
1750 case EXEC_ARITHMETIC_IF
:
1751 res
= gfc_trans_arithmetic_if (code
);
1755 res
= gfc_trans_block_construct (code
);
1759 res
= gfc_trans_do (code
, cond
);
1762 case EXEC_DO_CONCURRENT
:
1763 res
= gfc_trans_do_concurrent (code
);
1767 res
= gfc_trans_do_while (code
);
1771 res
= gfc_trans_select (code
);
1774 case EXEC_SELECT_TYPE
:
1775 /* Do nothing. SELECT TYPE statements should be transformed into
1776 an ordinary SELECT CASE at resolution stage.
1777 TODO: Add an error message here once this is done. */
1782 res
= gfc_trans_flush (code
);
1786 case EXEC_SYNC_IMAGES
:
1787 case EXEC_SYNC_MEMORY
:
1788 res
= gfc_trans_sync (code
, code
->op
);
1793 res
= gfc_trans_lock_unlock (code
, code
->op
);
1797 res
= gfc_trans_forall (code
);
1801 res
= gfc_trans_where (code
);
1805 res
= gfc_trans_allocate (code
);
1808 case EXEC_DEALLOCATE
:
1809 res
= gfc_trans_deallocate (code
);
1813 res
= gfc_trans_open (code
);
1817 res
= gfc_trans_close (code
);
1821 res
= gfc_trans_read (code
);
1825 res
= gfc_trans_write (code
);
1829 res
= gfc_trans_iolength (code
);
1832 case EXEC_BACKSPACE
:
1833 res
= gfc_trans_backspace (code
);
1837 res
= gfc_trans_endfile (code
);
1841 res
= gfc_trans_inquire (code
);
1845 res
= gfc_trans_wait (code
);
1849 res
= gfc_trans_rewind (code
);
1853 res
= gfc_trans_transfer (code
);
1857 res
= gfc_trans_dt_end (code
);
1860 case EXEC_OMP_ATOMIC
:
1861 case EXEC_OMP_BARRIER
:
1862 case EXEC_OMP_CANCEL
:
1863 case EXEC_OMP_CANCELLATION_POINT
:
1864 case EXEC_OMP_CRITICAL
:
1865 case EXEC_OMP_DISTRIBUTE
:
1866 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO
:
1867 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
1868 case EXEC_OMP_DISTRIBUTE_SIMD
:
1870 case EXEC_OMP_DO_SIMD
:
1871 case EXEC_OMP_FLUSH
:
1872 case EXEC_OMP_MASTER
:
1873 case EXEC_OMP_ORDERED
:
1874 case EXEC_OMP_PARALLEL
:
1875 case EXEC_OMP_PARALLEL_DO
:
1876 case EXEC_OMP_PARALLEL_DO_SIMD
:
1877 case EXEC_OMP_PARALLEL_SECTIONS
:
1878 case EXEC_OMP_PARALLEL_WORKSHARE
:
1879 case EXEC_OMP_SECTIONS
:
1881 case EXEC_OMP_SINGLE
:
1882 case EXEC_OMP_TARGET
:
1883 case EXEC_OMP_TARGET_DATA
:
1884 case EXEC_OMP_TARGET_TEAMS
:
1885 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE
:
1886 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
1887 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
1888 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
1889 case EXEC_OMP_TARGET_UPDATE
:
1891 case EXEC_OMP_TASKGROUP
:
1892 case EXEC_OMP_TASKWAIT
:
1893 case EXEC_OMP_TASKYIELD
:
1894 case EXEC_OMP_TEAMS
:
1895 case EXEC_OMP_TEAMS_DISTRIBUTE
:
1896 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
1897 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
1898 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD
:
1899 case EXEC_OMP_WORKSHARE
:
1900 res
= gfc_trans_omp_directive (code
);
1903 case EXEC_OACC_CACHE
:
1904 case EXEC_OACC_WAIT
:
1905 case EXEC_OACC_UPDATE
:
1906 case EXEC_OACC_LOOP
:
1907 case EXEC_OACC_HOST_DATA
:
1908 case EXEC_OACC_DATA
:
1909 case EXEC_OACC_KERNELS
:
1910 case EXEC_OACC_KERNELS_LOOP
:
1911 case EXEC_OACC_PARALLEL
:
1912 case EXEC_OACC_PARALLEL_LOOP
:
1913 case EXEC_OACC_ENTER_DATA
:
1914 case EXEC_OACC_EXIT_DATA
:
1915 res
= gfc_trans_oacc_directive (code
);
1919 gfc_internal_error ("gfc_trans_code(): Bad statement code");
1922 gfc_set_backend_locus (&code
->loc
);
1924 if (res
!= NULL_TREE
&& ! IS_EMPTY_STMT (res
))
1926 if (TREE_CODE (res
) != STATEMENT_LIST
)
1927 SET_EXPR_LOCATION (res
, input_location
);
1929 /* Add the new statement to the block. */
1930 gfc_add_expr_to_block (&block
, res
);
1934 /* Return the finished block. */
1935 return gfc_finish_block (&block
);
1939 /* Translate an executable statement with condition, cond. The condition is
1940 used by gfc_trans_do to test for IO result conditions inside implied
1941 DO loops of READ and WRITE statements. See build_dt in trans-io.c. */
1944 gfc_trans_code_cond (gfc_code
* code
, tree cond
)
1946 return trans_code (code
, cond
);
1949 /* Translate an executable statement without condition. */
1952 gfc_trans_code (gfc_code
* code
)
1954 return trans_code (code
, NULL_TREE
);
1958 /* This function is called after a complete program unit has been parsed
1962 gfc_generate_code (gfc_namespace
* ns
)
1965 if (ns
->is_block_data
)
1967 gfc_generate_block_data (ns
);
1971 gfc_generate_function_code (ns
);
1975 /* This function is called after a complete module has been parsed
1979 gfc_generate_module_code (gfc_namespace
* ns
)
1982 struct module_htab_entry
*entry
;
1984 gcc_assert (ns
->proc_name
->backend_decl
== NULL
);
1985 ns
->proc_name
->backend_decl
1986 = build_decl (ns
->proc_name
->declared_at
.lb
->location
,
1987 NAMESPACE_DECL
, get_identifier (ns
->proc_name
->name
),
1989 entry
= gfc_find_module (ns
->proc_name
->name
);
1990 if (entry
->namespace_decl
)
1991 /* Buggy sourcecode, using a module before defining it? */
1992 entry
->decls
->empty ();
1993 entry
->namespace_decl
= ns
->proc_name
->backend_decl
;
1995 gfc_generate_module_vars (ns
);
1997 /* We need to generate all module function prototypes first, to allow
1999 for (n
= ns
->contained
; n
; n
= n
->sibling
)
2006 gfc_create_function_decl (n
, false);
2007 DECL_CONTEXT (n
->proc_name
->backend_decl
) = ns
->proc_name
->backend_decl
;
2008 gfc_module_add_decl (entry
, n
->proc_name
->backend_decl
);
2009 for (el
= ns
->entries
; el
; el
= el
->next
)
2011 DECL_CONTEXT (el
->sym
->backend_decl
) = ns
->proc_name
->backend_decl
;
2012 gfc_module_add_decl (entry
, el
->sym
->backend_decl
);
2016 for (n
= ns
->contained
; n
; n
= n
->sibling
)
2021 gfc_generate_function_code (n
);
2026 /* Initialize an init/cleanup block with existing code. */
2029 gfc_start_wrapped_block (gfc_wrapped_block
* block
, tree code
)
2033 block
->init
= NULL_TREE
;
2035 block
->cleanup
= NULL_TREE
;
2039 /* Add a new pair of initializers/clean-up code. */
2042 gfc_add_init_cleanup (gfc_wrapped_block
* block
, tree init
, tree cleanup
)
2046 /* The new pair of init/cleanup should be "wrapped around" the existing
2047 block of code, thus the initialization is added to the front and the
2048 cleanup to the back. */
2049 add_expr_to_chain (&block
->init
, init
, true);
2050 add_expr_to_chain (&block
->cleanup
, cleanup
, false);
2054 /* Finish up a wrapped block by building a corresponding try-finally expr. */
2057 gfc_finish_wrapped_block (gfc_wrapped_block
* block
)
2063 /* Build the final expression. For this, just add init and body together,
2064 and put clean-up with that into a TRY_FINALLY_EXPR. */
2065 result
= block
->init
;
2066 add_expr_to_chain (&result
, block
->code
, false);
2068 result
= build2_loc (input_location
, TRY_FINALLY_EXPR
, void_type_node
,
2069 result
, block
->cleanup
);
2071 /* Clear the block. */
2072 block
->init
= NULL_TREE
;
2073 block
->code
= NULL_TREE
;
2074 block
->cleanup
= NULL_TREE
;
2080 /* Helper function for marking a boolean expression tree as unlikely. */
2083 gfc_unlikely (tree cond
, enum br_predictor predictor
)
2089 cond
= fold_convert (long_integer_type_node
, cond
);
2090 tmp
= build_zero_cst (long_integer_type_node
);
2091 cond
= build_call_expr_loc (input_location
,
2092 builtin_decl_explicit (BUILT_IN_EXPECT
),
2094 build_int_cst (integer_type_node
,
2097 cond
= fold_convert (boolean_type_node
, cond
);
2102 /* Helper function for marking a boolean expression tree as likely. */
2105 gfc_likely (tree cond
, enum br_predictor predictor
)
2111 cond
= fold_convert (long_integer_type_node
, cond
);
2112 tmp
= build_one_cst (long_integer_type_node
);
2113 cond
= build_call_expr_loc (input_location
,
2114 builtin_decl_explicit (BUILT_IN_EXPECT
),
2116 build_int_cst (integer_type_node
,
2119 cond
= fold_convert (boolean_type_node
, cond
);
2124 /* Get the string length for a deferred character length component. */
2127 gfc_deferred_strlen (gfc_component
*c
, tree
*decl
)
2129 char name
[GFC_MAX_SYMBOL_LEN
+9];
2130 gfc_component
*strlen
;
2131 if (!(c
->ts
.type
== BT_CHARACTER
&& c
->ts
.deferred
))
2133 sprintf (name
, "_%s_length", c
->name
);
2134 for (strlen
= c
; strlen
; strlen
= strlen
->next
)
2135 if (strcmp (strlen
->name
, name
) == 0)
2137 *decl
= strlen
? strlen
->backend_decl
: NULL_TREE
;
2138 return strlen
!= NULL
;