gcc/testsuite/ChangeLog:
[official-gcc.git] / gcc / fortran / trans.c
blob9210e0f71e5dae7f76c9fcc3d70d95697cc6cdc0
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
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "tree.h"
26 #include "gfortran.h"
27 #include "gimple-expr.h" /* For create_tmp_var_raw. */
28 #include "trans.h"
29 #include "stringpool.h"
30 #include "fold-const.h"
31 #include "tree-iterator.h"
32 #include "trans-stmt.h"
33 #include "trans-array.h"
34 #include "trans-types.h"
35 #include "trans-const.h"
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. */
53 tree
54 gfc_advance_chain (tree t, int n)
56 for (; n > 0; n--)
58 gcc_assert (t != NULL_TREE);
59 t = DECL_CHAIN (t);
61 return t;
65 /* Strip off a legitimate source ending from the input
66 string NAME of length LEN. */
68 static inline void
69 remove_suffix (char *name, int len)
71 int i;
73 for (i = 2; i < 8 && len > i; i++)
75 if (name[len - i] == '.')
77 name[len - i] = '\0';
78 break;
84 /* Creates a variable declaration with a given TYPE. */
86 tree
87 gfc_create_var_np (tree type, const char *prefix)
89 tree t;
91 t = create_tmp_var_raw (type, prefix);
93 /* No warnings for anonymous variables. */
94 if (prefix == NULL)
95 TREE_NO_WARNING (t) = 1;
97 return t;
101 /* Like above, but also adds it to the current scope. */
103 tree
104 gfc_create_var (tree type, const char *prefix)
106 tree tmp;
108 tmp = gfc_create_var_np (type, prefix);
110 pushdecl (tmp);
112 return tmp;
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. */
120 tree
121 gfc_evaluate_now_loc (location_t loc, tree expr, stmtblock_t * pblock)
123 tree var;
125 if (CONSTANT_CLASS_P (expr))
126 return expr;
128 var = gfc_create_var (TREE_TYPE (expr), NULL);
129 gfc_add_modify_loc (loc, pblock, var, expr);
131 return var;
135 tree
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:
144 LHS <- RHS. */
146 void
147 gfc_add_modify_loc (location_t loc, stmtblock_t * pblock, tree lhs, tree rhs)
149 tree tmp;
151 tree t1, t2;
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,
162 rhs);
163 gfc_add_expr_to_block (pblock, tmp);
167 void
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. */
178 void
179 gfc_start_block (stmtblock_t * block)
181 /* Start a new binding level. */
182 pushlevel ();
183 block->has_scope = 1;
185 /* The block is empty. */
186 block->head = NULL_TREE;
190 /* Initialize a block without creating a new scope. */
192 void
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. */
204 void
205 gfc_merge_block_scope (stmtblock_t * block)
207 tree decl;
208 tree next;
210 gcc_assert (block->has_scope);
211 block->has_scope = 0;
213 /* Remember the decls in this scope. */
214 decl = getdecls ();
215 poplevel (0, 0);
217 /* Add them to the parent scope. */
218 while (decl != NULL_TREE)
220 next = DECL_CHAIN (decl);
221 DECL_CHAIN (decl) = NULL_TREE;
223 pushdecl (decl);
224 decl = next;
229 /* Finish a scope containing a block of statements. */
231 tree
232 gfc_finish_block (stmtblock_t * stmtblock)
234 tree decl;
235 tree expr;
236 tree block;
238 expr = stmtblock->head;
239 if (!expr)
240 expr = build_empty_stmt (input_location);
242 stmtblock->head = NULL_TREE;
244 if (stmtblock->has_scope)
246 decl = getdecls ();
248 if (decl)
250 block = poplevel (1, 0);
251 expr = build3_v (BIND_EXPR, decl, expr, block);
253 else
254 poplevel (0, 0);
257 return expr;
261 /* Build an ADDR_EXPR and cast the result to TYPE. If TYPE is NULL, the
262 natural type is used. */
264 tree
265 gfc_build_addr_expr (tree type, tree t)
267 tree base_type = TREE_TYPE (t);
268 tree natural_type;
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));
281 natural_type = type;
283 else
284 natural_type = build_pointer_type (base_type);
286 if (TREE_CODE (t) == INDIRECT_REF)
288 if (!type)
289 type = natural_type;
290 t = TREE_OPERAND (t, 0);
291 natural_type = TREE_TYPE (t);
293 else
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);
304 return t;
308 /* Build an ARRAY_REF with its natural type. */
310 tree
311 gfc_build_array_ref (tree base, tree offset, tree decl, tree vptr)
313 tree type = TREE_TYPE (base);
314 tree tmp;
315 tree span;
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));
329 return base;
332 type = TREE_TYPE (type);
334 /* Use pointer arithmetic for deferred character length array
335 references. */
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)
340 && decl
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));
346 else
347 span = NULL_TREE;
349 if (DECL_P (base))
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)
368 if (decl)
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
374 descriptor. */
375 if (DECL_LANG_SPECIFIC (decl) && GFC_DECL_SAVED_DESCRIPTOR (decl))
376 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
377 else
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);
396 else if (span)
397 span = fold_convert (gfc_array_index_type, span);
398 else
399 gcc_unreachable ();
401 else if (vptr)
402 span = gfc_vptr_size_get (vptr);
403 else
404 gcc_unreachable ();
406 offset = fold_build2_loc (input_location, MULT_EXPR,
407 gfc_array_index_type,
408 offset, span);
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);
414 return tmp;
416 else
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. */
426 static tree
427 trans_runtime_error_vararg (bool error, locus* where, const char* msgid,
428 va_list ap)
430 stmtblock_t block;
431 tree tmp;
432 tree arg, arg2;
433 tree *argarray;
434 tree fntype;
435 char *message;
436 const char *p;
437 int line, nargs, i;
438 location_t loc;
440 /* Compute the number of extra arguments from the format string. */
441 for (p = msgid, nargs = 0; *p; p++)
442 if (*p == '%')
444 p++;
445 if (*p != '%')
446 nargs++;
449 /* The code to generate the error. */
450 gfc_start_block (&block);
452 if (where)
454 line = LOCATION_LINE (where->lb->location);
455 message = xasprintf ("At line %d of file %s", line,
456 where->lb->file->filename);
458 else
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));
464 free (message);
466 message = xasprintf ("%s", _(msgid));
467 arg2 = gfc_build_addr_expr (pchar_type_node,
468 gfc_build_localized_cstring_const (message));
469 free (message);
471 /* Build the argument array. */
472 argarray = XALLOCAVEC (tree, nargs + 2);
473 argarray[0] = arg;
474 argarray[1] = arg2;
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,
480 irectly. */
481 if (error)
482 fntype = TREE_TYPE (gfor_fndecl_runtime_error_at);
483 else
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),
490 error
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);
500 tree
501 gfc_trans_runtime_error (bool error, locus* where, const char* msgid, ...)
503 va_list ap;
504 tree result;
506 va_start (ap, msgid);
507 result = trans_runtime_error_vararg (error, where, msgid, ap);
508 va_end (ap);
509 return result;
513 /* Generate a runtime error if COND is true. */
515 void
516 gfc_trans_runtime_check (bool error, bool once, tree cond, stmtblock_t * pblock,
517 locus * where, const char * msgid, ...)
519 va_list ap;
520 stmtblock_t block;
521 tree body;
522 tree tmp;
523 tree tmpvar = NULL;
525 if (integer_zerop (cond))
526 return;
528 if (once)
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. */
539 if (!error && once)
540 gfc_add_expr_to_block (&block, build_predict_expr (PRED_FORTRAN_WARN_ONCE,
541 NOT_TAKEN));
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,
547 msgid, ap));
548 va_end (ap);
550 if (once)
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);
559 else
561 if (once)
562 cond = fold_build2_loc (where->lb->location, TRUTH_AND_EXPR,
563 long_integer_type_node, tmpvar, cond);
564 else
565 cond = fold_convert (long_integer_type_node, cond);
567 tmp = fold_build3_loc (where->lb->location, COND_EXPR, void_type_node,
568 cond, body,
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. */
578 tree
579 gfc_call_malloc (stmtblock_t * block, tree type, tree size)
581 tree tmp, msg, malloc_result, null_result, res, malloc_tree;
582 stmtblock_t block2;
584 /* Create a variable to hold the result. */
585 res = gfc_create_var (prvoid_type_node, NULL);
587 /* Call malloc. */
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,
609 null_result,
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);
619 if (type != NULL)
620 res = fold_convert (type, res);
621 return res;
625 /* Allocate memory, using an optional status argument.
627 This function follows the following pseudo-code:
629 void *
630 allocate (size_t size, integer_type stat)
632 void *newmem;
634 if (stat requested)
635 stat = 0;
637 newmem = malloc (MAX (size, 1));
638 if (newmem == NULL)
640 if (stat)
641 *stat = LIBERROR_ALLOCATION;
642 else
643 runtime_error ("Allocation would exceed memory limit");
645 return newmem;
646 } */
647 void
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);
679 else
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:
705 void *
706 allocate (size_t size, void** token, int *stat, char* errmsg, int errlen)
708 void *newmem;
710 newmem = _caf_register (size, regtype, token, &stat, errmsg, errlen);
711 return newmem;
712 } */
713 static void
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)
718 tree tmp, pstat;
720 gcc_assert (token != NULL_TREE);
722 /* The allocation itself. */
723 if (status == NULL_TREE)
724 pstat = null_pointer_node;
725 else
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:
766 void *
767 allocate_allocatable (void *mem, size_t size, integer_type stat)
769 if (mem == NULL)
770 return allocate (size, stat);
771 else
773 if (stat)
774 stat = LIBERROR_ALLOCATION;
775 else
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. */
782 void
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))
809 tree cond;
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;
820 gfc_se se;
821 gfc_init_se (&se, NULL);
823 tree sub_caf_tree = gfc_get_ultimate_alloc_ptr_comps_caf_token (&se,
824 expr);
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);
832 else
833 tmp = mem;
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);
843 need_assign = true;
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);
857 if (need_assign)
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);
872 else
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
878 status variable. */
879 if (expr)
881 tree varname;
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'",
890 varname);
892 else
893 error = gfc_trans_runtime_error (true, NULL,
894 "Attempting to allocate already allocated"
895 " variable");
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,
906 error, alloc);
907 gfc_add_expr_to_block (block, tmp);
911 /* Free a given variable. */
913 tree
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". */
924 static tree
925 gfc_build_final_call (gfc_typespec ts, gfc_expr *final_wrapper, gfc_expr *var,
926 bool fini_coarray, gfc_expr *class_size)
928 stmtblock_t block;
929 gfc_se se;
930 tree final_fndecl, array, size, tmp;
931 symbol_attribute attr;
933 gcc_assert (final_wrapper->expr_type == EXPR_VARIABLE);
934 gcc_assert (var);
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)
945 tree elem_size;
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);
953 se.want_pointer = 1;
954 if (var->rank)
956 se.descriptor_only = 1;
957 gfc_conv_expr_descriptor (&se, var);
958 array = se.expr;
960 else
962 gfc_conv_expr (&se, var);
963 gcc_assert (se.pre.head == NULL_TREE && se.post.head == NULL_TREE);
964 array = se.expr;
966 /* No copy back needed, hence set attr's allocatable/pointer
967 to zero. */
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);
974 else
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);
982 size = se.expr;
984 array_expr = gfc_copy_expr (var);
985 gfc_init_se (&se, NULL);
986 se.want_pointer = 1;
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);
992 array = se.expr;
994 else
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);
1000 array = se.expr;
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
1008 to zero. */
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);
1032 bool
1033 gfc_add_comp_finalizer_call (stmtblock_t *block, tree decl, gfc_component *comp,
1034 bool fini_coarray)
1036 gfc_se se;
1037 stmtblock_t block2;
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)
1043 return false;
1045 gfc_init_block (&block2);
1047 if (comp->ts.type == BT_DERIVED)
1049 if (comp->attr.pointer)
1050 return false;
1052 gfc_is_finalizable (comp->ts.u.derived, &final_expr);
1053 if (!final_expr)
1054 return false;
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);
1063 array = decl;
1065 else /* comp->ts.type == BT_CLASS. */
1067 if (CLASS_DATA (comp)->attr.class_pointer)
1068 return false;
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));
1085 else
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);
1100 if (!final_expr)
1102 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1103 final_fndecl,
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);
1124 return true;
1128 /* Add a call to the finalizer, using the passed *expr. Returns
1129 true when a finalizer call has been inserted. */
1131 bool
1132 gfc_add_finalizer_call (stmtblock_t *block, gfc_expr *expr2)
1134 tree tmp;
1135 gfc_ref *ref;
1136 gfc_expr *expr;
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))
1142 return false;
1144 if (expr2->ts.type == BT_DERIVED)
1146 gfc_is_finalizable (expr2->ts.u.derived, &final_expr);
1147 if (!final_expr)
1148 return false;
1151 /* If we have a class array, we need go back to the class
1152 container. */
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);
1161 expr->ref = NULL;
1163 else
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);
1171 ref->next = NULL;
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,
1193 false, elem_size);
1195 if (expr->ts.type == BT_CLASS && !has_finalizer)
1197 tree cond;
1198 gfc_se se;
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))
1210 tree cond2;
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,
1222 se.expr,
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);
1234 return true;
1238 /* User-deallocate; we emit the code directly from the front-end, and the
1239 logic is the same as the previous library function:
1241 void
1242 deallocate (void *pointer, GFC_INTEGER_4 * stat)
1244 if (!pointer)
1246 if (stat)
1247 *stat = 1;
1248 else
1249 runtime_error ("Attempt to DEALLOCATE unallocated memory.");
1251 else
1253 free (pointer);
1254 if (stat)
1255 *stat = 0;
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
1263 each procedure).
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. */
1270 tree
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;
1280 if (coarray)
1282 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (pointer)));
1283 caf_decl = 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);
1294 if (!can_fail)
1296 tree varname;
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'",
1305 varname);
1307 else
1308 error = build_empty_stmt (input_location);
1310 if (status != NULL_TREE && !integer_zerop (status))
1312 tree cond2;
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,
1322 cond2, tmp, error);
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));
1341 tree cond2;
1343 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1344 status,
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);
1356 else
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);
1367 else
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);
1379 pstat = status;
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);
1388 else
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. */
1434 tree
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;
1440 bool finalizable;
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);
1448 if (!can_fail)
1450 tree varname;
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'",
1459 varname);
1461 else
1462 error = build_empty_stmt (input_location);
1464 if (status != NULL_TREE && !integer_zerop (status))
1466 tree status_type = TREE_TYPE (TREE_TYPE (status));
1467 tree cond2;
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,
1476 cond2, tmp, error);
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));
1502 tree cond2;
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:
1524 void *
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");
1531 return res;
1532 } */
1533 tree
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,
1559 null_result,
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);
1565 return res;
1569 /* Add an expression to another one, either at the front or the back. */
1571 static void
1572 add_expr_to_chain (tree* chain, tree expr, bool front)
1574 if (expr == NULL_TREE || IS_EMPTY_STMT (expr))
1575 return;
1577 if (*chain)
1579 if (TREE_CODE (*chain) != STATEMENT_LIST)
1581 tree tmp;
1583 tmp = *chain;
1584 *chain = NULL_TREE;
1585 append_to_statement_list (tmp, chain);
1588 if (front)
1590 tree_stmt_iterator i;
1592 i = tsi_start (*chain);
1593 tsi_link_before (&i, expr, TSI_CONTINUE_LINKING);
1595 else
1596 append_to_statement_list (expr, chain);
1598 else
1599 *chain = expr;
1603 /* Add a statement at the end of a block. */
1605 void
1606 gfc_add_expr_to_block (stmtblock_t * block, tree expr)
1608 gcc_assert (block);
1609 add_expr_to_chain (&block->head, expr, false);
1613 /* Add a statement at the beginning of a block. */
1615 void
1616 gfc_prepend_expr_to_block (stmtblock_t * block, tree expr)
1618 gcc_assert (block);
1619 add_expr_to_chain (&block->head, expr, true);
1623 /* Add a block the end of a block. */
1625 void
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. */
1639 void
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. */
1650 void
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. */
1661 void
1662 gfc_restore_backend_locus (locus * loc)
1664 gfc_set_backend_locus (loc);
1665 free (loc->lb);
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
1671 gfc_trans_code. */
1673 static tree
1674 trans_code (gfc_code * code, tree cond)
1676 stmtblock_t block;
1677 tree res;
1679 if (!code)
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);
1697 switch (code->op)
1699 case EXEC_NOP:
1700 case EXEC_END_BLOCK:
1701 case EXEC_END_NESTED_BLOCK:
1702 case EXEC_END_PROCEDURE:
1703 res = NULL_TREE;
1704 break;
1706 case EXEC_ASSIGN:
1707 if (code->expr1->ts.type == BT_CLASS)
1708 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1709 else
1710 res = gfc_trans_assign (code);
1711 break;
1713 case EXEC_LABEL_ASSIGN:
1714 res = gfc_trans_label_assign (code);
1715 break;
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))
1724 /* F2003: C717 */
1725 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1726 else
1727 res = gfc_trans_pointer_assign (code);
1728 break;
1730 case EXEC_INIT_ASSIGN:
1731 if (code->expr1->ts.type == BT_CLASS)
1732 res = gfc_trans_class_init_assign (code);
1733 else
1734 res = gfc_trans_init_assign (code);
1735 break;
1737 case EXEC_CONTINUE:
1738 res = NULL_TREE;
1739 break;
1741 case EXEC_CRITICAL:
1742 res = gfc_trans_critical (code);
1743 break;
1745 case EXEC_CYCLE:
1746 res = gfc_trans_cycle (code);
1747 break;
1749 case EXEC_EXIT:
1750 res = gfc_trans_exit (code);
1751 break;
1753 case EXEC_GOTO:
1754 res = gfc_trans_goto (code);
1755 break;
1757 case EXEC_ENTRY:
1758 res = gfc_trans_entry (code);
1759 break;
1761 case EXEC_PAUSE:
1762 res = gfc_trans_pause (code);
1763 break;
1765 case EXEC_STOP:
1766 case EXEC_ERROR_STOP:
1767 res = gfc_trans_stop (code, code->op == EXEC_ERROR_STOP);
1768 break;
1770 case EXEC_CALL:
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)
1780 break;
1783 if (code->resolved_isym
1784 && code->resolved_isym->id == GFC_ISYM_MVBITS)
1785 is_mvbits = true;
1787 res = gfc_trans_call (code, is_mvbits, NULL_TREE,
1788 NULL_TREE, false);
1790 break;
1792 case EXEC_CALL_PPC:
1793 res = gfc_trans_call (code, false, NULL_TREE,
1794 NULL_TREE, false);
1795 break;
1797 case EXEC_ASSIGN_CALL:
1798 res = gfc_trans_call (code, true, NULL_TREE,
1799 NULL_TREE, false);
1800 break;
1802 case EXEC_RETURN:
1803 res = gfc_trans_return (code);
1804 break;
1806 case EXEC_IF:
1807 res = gfc_trans_if (code);
1808 break;
1810 case EXEC_ARITHMETIC_IF:
1811 res = gfc_trans_arithmetic_if (code);
1812 break;
1814 case EXEC_BLOCK:
1815 res = gfc_trans_block_construct (code);
1816 break;
1818 case EXEC_DO:
1819 res = gfc_trans_do (code, cond);
1820 break;
1822 case EXEC_DO_CONCURRENT:
1823 res = gfc_trans_do_concurrent (code);
1824 break;
1826 case EXEC_DO_WHILE:
1827 res = gfc_trans_do_while (code);
1828 break;
1830 case EXEC_SELECT:
1831 res = gfc_trans_select (code);
1832 break;
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. */
1838 res = NULL_TREE;
1839 break;
1841 case EXEC_FLUSH:
1842 res = gfc_trans_flush (code);
1843 break;
1845 case EXEC_SYNC_ALL:
1846 case EXEC_SYNC_IMAGES:
1847 case EXEC_SYNC_MEMORY:
1848 res = gfc_trans_sync (code, code->op);
1849 break;
1851 case EXEC_LOCK:
1852 case EXEC_UNLOCK:
1853 res = gfc_trans_lock_unlock (code, code->op);
1854 break;
1856 case EXEC_EVENT_POST:
1857 case EXEC_EVENT_WAIT:
1858 res = gfc_trans_event_post_wait (code, code->op);
1859 break;
1861 case EXEC_FORALL:
1862 res = gfc_trans_forall (code);
1863 break;
1865 case EXEC_WHERE:
1866 res = gfc_trans_where (code);
1867 break;
1869 case EXEC_ALLOCATE:
1870 res = gfc_trans_allocate (code);
1871 break;
1873 case EXEC_DEALLOCATE:
1874 res = gfc_trans_deallocate (code);
1875 break;
1877 case EXEC_OPEN:
1878 res = gfc_trans_open (code);
1879 break;
1881 case EXEC_CLOSE:
1882 res = gfc_trans_close (code);
1883 break;
1885 case EXEC_READ:
1886 res = gfc_trans_read (code);
1887 break;
1889 case EXEC_WRITE:
1890 res = gfc_trans_write (code);
1891 break;
1893 case EXEC_IOLENGTH:
1894 res = gfc_trans_iolength (code);
1895 break;
1897 case EXEC_BACKSPACE:
1898 res = gfc_trans_backspace (code);
1899 break;
1901 case EXEC_ENDFILE:
1902 res = gfc_trans_endfile (code);
1903 break;
1905 case EXEC_INQUIRE:
1906 res = gfc_trans_inquire (code);
1907 break;
1909 case EXEC_WAIT:
1910 res = gfc_trans_wait (code);
1911 break;
1913 case EXEC_REWIND:
1914 res = gfc_trans_rewind (code);
1915 break;
1917 case EXEC_TRANSFER:
1918 res = gfc_trans_transfer (code);
1919 break;
1921 case EXEC_DT_END:
1922 res = gfc_trans_dt_end (code);
1923 break;
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:
1934 case EXEC_OMP_DO:
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:
1945 case EXEC_OMP_SIMD:
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:
1955 case EXEC_OMP_TASK:
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);
1966 break;
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);
1983 break;
1985 default:
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. */
2010 tree
2011 gfc_trans_code_cond (gfc_code * code, tree cond)
2013 return trans_code (code, cond);
2016 /* Translate an executable statement without condition. */
2018 tree
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
2026 and resolved. */
2028 void
2029 gfc_generate_code (gfc_namespace * ns)
2031 ompws_flags = 0;
2032 if (ns->is_block_data)
2034 gfc_generate_block_data (ns);
2035 return;
2038 gfc_generate_function_code (ns);
2042 /* This function is called after a complete module has been parsed
2043 and resolved. */
2045 void
2046 gfc_generate_module_code (gfc_namespace * ns)
2048 gfc_namespace *n;
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),
2055 void_type_node);
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
2065 sibling calls. */
2066 for (n = ns->contained; n; n = n->sibling)
2068 gfc_entry_list *el;
2070 if (!n->proc_name)
2071 continue;
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)
2085 if (!n->proc_name)
2086 continue;
2088 gfc_generate_function_code (n);
2093 /* Initialize an init/cleanup block with existing code. */
2095 void
2096 gfc_start_wrapped_block (gfc_wrapped_block* block, tree code)
2098 gcc_assert (block);
2100 block->init = NULL_TREE;
2101 block->code = code;
2102 block->cleanup = NULL_TREE;
2106 /* Add a new pair of initializers/clean-up code. */
2108 void
2109 gfc_add_init_cleanup (gfc_wrapped_block* block, tree init, tree cleanup)
2111 gcc_assert (block);
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. */
2123 tree
2124 gfc_finish_wrapped_block (gfc_wrapped_block* block)
2126 tree result;
2128 gcc_assert (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);
2134 if (block->cleanup)
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;
2143 return result;
2147 /* Helper function for marking a boolean expression tree as unlikely. */
2149 tree
2150 gfc_unlikely (tree cond, enum br_predictor predictor)
2152 tree tmp;
2154 if (optimize)
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),
2160 3, cond, tmp,
2161 build_int_cst (integer_type_node,
2162 predictor));
2164 cond = fold_convert (boolean_type_node, cond);
2165 return cond;
2169 /* Helper function for marking a boolean expression tree as likely. */
2171 tree
2172 gfc_likely (tree cond, enum br_predictor predictor)
2174 tree tmp;
2176 if (optimize)
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),
2182 3, cond, tmp,
2183 build_int_cst (integer_type_node,
2184 predictor));
2186 cond = fold_convert (boolean_type_node, cond);
2187 return cond;
2191 /* Get the string length for a deferred character length component. */
2193 bool
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))
2199 return false;
2200 sprintf (name, "_%s_length", c->name);
2201 for (strlen = c; strlen; strlen = strlen->next)
2202 if (strcmp (strlen->name, name) == 0)
2203 break;
2204 *decl = strlen ? strlen->backend_decl : NULL_TREE;
2205 return strlen != NULL;