PR fortran/54833
[official-gcc.git] / gcc / fortran / trans.c
blobf30809a9fda1b33242c79873139f35209bf9a9ad
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
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 "gfortran.h"
25 #include "alias.h"
26 #include "tree.h"
27 #include "options.h"
28 #include "fold-const.h"
29 #include "gimple-expr.h" /* For create_tmp_var_raw. */
30 #include "stringpool.h"
31 #include "tree-iterator.h"
32 #include "diagnostic-core.h" /* For internal_error. */
33 #include "flags.h"
34 #include "trans.h"
35 #include "trans-stmt.h"
36 #include "trans-array.h"
37 #include "trans-types.h"
38 #include "trans-const.h"
40 /* Naming convention for backend interface code:
42 gfc_trans_* translate gfc_code into STMT trees.
44 gfc_conv_* expression conversion
46 gfc_get_* get a backend tree representation of a decl or type */
48 static gfc_file *gfc_current_backend_file;
50 const char gfc_msg_fault[] = N_("Array reference out of bounds");
51 const char gfc_msg_wrong_return[] = N_("Incorrect function return value");
54 /* Advance along TREE_CHAIN n times. */
56 tree
57 gfc_advance_chain (tree t, int n)
59 for (; n > 0; n--)
61 gcc_assert (t != NULL_TREE);
62 t = DECL_CHAIN (t);
64 return t;
68 /* Strip off a legitimate source ending from the input
69 string NAME of length LEN. */
71 static inline void
72 remove_suffix (char *name, int len)
74 int i;
76 for (i = 2; i < 8 && len > i; i++)
78 if (name[len - i] == '.')
80 name[len - i] = '\0';
81 break;
87 /* Creates a variable declaration with a given TYPE. */
89 tree
90 gfc_create_var_np (tree type, const char *prefix)
92 tree t;
94 t = create_tmp_var_raw (type, prefix);
96 /* No warnings for anonymous variables. */
97 if (prefix == NULL)
98 TREE_NO_WARNING (t) = 1;
100 return t;
104 /* Like above, but also adds it to the current scope. */
106 tree
107 gfc_create_var (tree type, const char *prefix)
109 tree tmp;
111 tmp = gfc_create_var_np (type, prefix);
113 pushdecl (tmp);
115 return tmp;
119 /* If the expression is not constant, evaluate it now. We assign the
120 result of the expression to an artificially created variable VAR, and
121 return a pointer to the VAR_DECL node for this variable. */
123 tree
124 gfc_evaluate_now_loc (location_t loc, tree expr, stmtblock_t * pblock)
126 tree var;
128 if (CONSTANT_CLASS_P (expr))
129 return expr;
131 var = gfc_create_var (TREE_TYPE (expr), NULL);
132 gfc_add_modify_loc (loc, pblock, var, expr);
134 return var;
138 tree
139 gfc_evaluate_now (tree expr, stmtblock_t * pblock)
141 return gfc_evaluate_now_loc (input_location, expr, pblock);
145 /* Build a MODIFY_EXPR node and add it to a given statement block PBLOCK.
146 A MODIFY_EXPR is an assignment:
147 LHS <- RHS. */
149 void
150 gfc_add_modify_loc (location_t loc, stmtblock_t * pblock, tree lhs, tree rhs)
152 tree tmp;
154 #ifdef ENABLE_CHECKING
155 tree t1, t2;
156 t1 = TREE_TYPE (rhs);
157 t2 = TREE_TYPE (lhs);
158 /* Make sure that the types of the rhs and the lhs are the same
159 for scalar assignments. We should probably have something
160 similar for aggregates, but right now removing that check just
161 breaks everything. */
162 gcc_assert (t1 == t2
163 || AGGREGATE_TYPE_P (TREE_TYPE (lhs)));
164 #endif
166 tmp = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, lhs,
167 rhs);
168 gfc_add_expr_to_block (pblock, tmp);
172 void
173 gfc_add_modify (stmtblock_t * pblock, tree lhs, tree rhs)
175 gfc_add_modify_loc (input_location, pblock, lhs, rhs);
179 /* Create a new scope/binding level and initialize a block. Care must be
180 taken when translating expressions as any temporaries will be placed in
181 the innermost scope. */
183 void
184 gfc_start_block (stmtblock_t * block)
186 /* Start a new binding level. */
187 pushlevel ();
188 block->has_scope = 1;
190 /* The block is empty. */
191 block->head = NULL_TREE;
195 /* Initialize a block without creating a new scope. */
197 void
198 gfc_init_block (stmtblock_t * block)
200 block->head = NULL_TREE;
201 block->has_scope = 0;
205 /* Sometimes we create a scope but it turns out that we don't actually
206 need it. This function merges the scope of BLOCK with its parent.
207 Only variable decls will be merged, you still need to add the code. */
209 void
210 gfc_merge_block_scope (stmtblock_t * block)
212 tree decl;
213 tree next;
215 gcc_assert (block->has_scope);
216 block->has_scope = 0;
218 /* Remember the decls in this scope. */
219 decl = getdecls ();
220 poplevel (0, 0);
222 /* Add them to the parent scope. */
223 while (decl != NULL_TREE)
225 next = DECL_CHAIN (decl);
226 DECL_CHAIN (decl) = NULL_TREE;
228 pushdecl (decl);
229 decl = next;
234 /* Finish a scope containing a block of statements. */
236 tree
237 gfc_finish_block (stmtblock_t * stmtblock)
239 tree decl;
240 tree expr;
241 tree block;
243 expr = stmtblock->head;
244 if (!expr)
245 expr = build_empty_stmt (input_location);
247 stmtblock->head = NULL_TREE;
249 if (stmtblock->has_scope)
251 decl = getdecls ();
253 if (decl)
255 block = poplevel (1, 0);
256 expr = build3_v (BIND_EXPR, decl, expr, block);
258 else
259 poplevel (0, 0);
262 return expr;
266 /* Build an ADDR_EXPR and cast the result to TYPE. If TYPE is NULL, the
267 natural type is used. */
269 tree
270 gfc_build_addr_expr (tree type, tree t)
272 tree base_type = TREE_TYPE (t);
273 tree natural_type;
275 if (type && POINTER_TYPE_P (type)
276 && TREE_CODE (base_type) == ARRAY_TYPE
277 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
278 == TYPE_MAIN_VARIANT (TREE_TYPE (base_type)))
280 tree min_val = size_zero_node;
281 tree type_domain = TYPE_DOMAIN (base_type);
282 if (type_domain && TYPE_MIN_VALUE (type_domain))
283 min_val = TYPE_MIN_VALUE (type_domain);
284 t = fold (build4_loc (input_location, ARRAY_REF, TREE_TYPE (type),
285 t, min_val, NULL_TREE, NULL_TREE));
286 natural_type = type;
288 else
289 natural_type = build_pointer_type (base_type);
291 if (TREE_CODE (t) == INDIRECT_REF)
293 if (!type)
294 type = natural_type;
295 t = TREE_OPERAND (t, 0);
296 natural_type = TREE_TYPE (t);
298 else
300 tree base = get_base_address (t);
301 if (base && DECL_P (base))
302 TREE_ADDRESSABLE (base) = 1;
303 t = fold_build1_loc (input_location, ADDR_EXPR, natural_type, t);
306 if (type && natural_type != type)
307 t = convert (type, t);
309 return t;
313 /* Build an ARRAY_REF with its natural type. */
315 tree
316 gfc_build_array_ref (tree base, tree offset, tree decl, tree vptr)
318 tree type = TREE_TYPE (base);
319 tree tmp;
320 tree span;
322 if (GFC_ARRAY_TYPE_P (type) && GFC_TYPE_ARRAY_RANK (type) == 0)
324 gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
326 return fold_convert (TYPE_MAIN_VARIANT (type), base);
329 /* Scalar coarray, there is nothing to do. */
330 if (TREE_CODE (type) != ARRAY_TYPE)
332 gcc_assert (decl == NULL_TREE);
333 gcc_assert (integer_zerop (offset));
334 return base;
337 type = TREE_TYPE (type);
339 if (DECL_P (base))
340 TREE_ADDRESSABLE (base) = 1;
342 /* Strip NON_LVALUE_EXPR nodes. */
343 STRIP_TYPE_NOPS (offset);
345 /* If the array reference is to a pointer, whose target contains a
346 subreference, use the span that is stored with the backend decl
347 and reference the element with pointer arithmetic. */
348 if ((decl && (TREE_CODE (decl) == FIELD_DECL
349 || TREE_CODE (decl) == VAR_DECL
350 || TREE_CODE (decl) == PARM_DECL)
351 && ((GFC_DECL_SUBREF_ARRAY_P (decl)
352 && !integer_zerop (GFC_DECL_SPAN (decl)))
353 || GFC_DECL_CLASS (decl)))
354 || vptr)
356 if (decl)
358 if (GFC_DECL_CLASS (decl))
360 /* When a temporary is in place for the class array, then the
361 original class' declaration is stored in the saved
362 descriptor. */
363 if (DECL_LANG_SPECIFIC (decl) && GFC_DECL_SAVED_DESCRIPTOR (decl))
364 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
365 else
367 /* Allow for dummy arguments and other good things. */
368 if (POINTER_TYPE_P (TREE_TYPE (decl)))
369 decl = build_fold_indirect_ref_loc (input_location, decl);
371 /* Check if '_data' is an array descriptor. If it is not,
372 the array must be one of the components of the class
373 object, so return a normal array reference. */
374 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (
375 gfc_class_data_get (decl))))
376 return build4_loc (input_location, ARRAY_REF, type, base,
377 offset, NULL_TREE, NULL_TREE);
380 span = gfc_class_vtab_size_get (decl);
382 else if (GFC_DECL_SUBREF_ARRAY_P (decl))
383 span = GFC_DECL_SPAN (decl);
384 else
385 gcc_unreachable ();
387 else if (vptr)
388 span = gfc_vptr_size_get (vptr);
389 else
390 gcc_unreachable ();
392 offset = fold_build2_loc (input_location, MULT_EXPR,
393 gfc_array_index_type,
394 offset, span);
395 tmp = gfc_build_addr_expr (pvoid_type_node, base);
396 tmp = fold_build_pointer_plus_loc (input_location, tmp, offset);
397 tmp = fold_convert (build_pointer_type (type), tmp);
398 if (!TYPE_STRING_FLAG (type))
399 tmp = build_fold_indirect_ref_loc (input_location, tmp);
400 return tmp;
402 else
403 /* Otherwise use a straightforward array reference. */
404 return build4_loc (input_location, ARRAY_REF, type, base, offset,
405 NULL_TREE, NULL_TREE);
409 /* Generate a call to print a runtime error possibly including multiple
410 arguments and a locus. */
412 static tree
413 trans_runtime_error_vararg (bool error, locus* where, const char* msgid,
414 va_list ap)
416 stmtblock_t block;
417 tree tmp;
418 tree arg, arg2;
419 tree *argarray;
420 tree fntype;
421 char *message;
422 const char *p;
423 int line, nargs, i;
424 location_t loc;
426 /* Compute the number of extra arguments from the format string. */
427 for (p = msgid, nargs = 0; *p; p++)
428 if (*p == '%')
430 p++;
431 if (*p != '%')
432 nargs++;
435 /* The code to generate the error. */
436 gfc_start_block (&block);
438 if (where)
440 line = LOCATION_LINE (where->lb->location);
441 message = xasprintf ("At line %d of file %s", line,
442 where->lb->file->filename);
444 else
445 message = xasprintf ("In file '%s', around line %d",
446 gfc_source_file, LOCATION_LINE (input_location) + 1);
448 arg = gfc_build_addr_expr (pchar_type_node,
449 gfc_build_localized_cstring_const (message));
450 free (message);
452 message = xasprintf ("%s", _(msgid));
453 arg2 = gfc_build_addr_expr (pchar_type_node,
454 gfc_build_localized_cstring_const (message));
455 free (message);
457 /* Build the argument array. */
458 argarray = XALLOCAVEC (tree, nargs + 2);
459 argarray[0] = arg;
460 argarray[1] = arg2;
461 for (i = 0; i < nargs; i++)
462 argarray[2 + i] = va_arg (ap, tree);
464 /* Build the function call to runtime_(warning,error)_at; because of the
465 variable number of arguments, we can't use build_call_expr_loc dinput_location,
466 irectly. */
467 if (error)
468 fntype = TREE_TYPE (gfor_fndecl_runtime_error_at);
469 else
470 fntype = TREE_TYPE (gfor_fndecl_runtime_warning_at);
472 loc = where ? where->lb->location : input_location;
473 tmp = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
474 fold_build1_loc (loc, ADDR_EXPR,
475 build_pointer_type (fntype),
476 error
477 ? gfor_fndecl_runtime_error_at
478 : gfor_fndecl_runtime_warning_at),
479 nargs + 2, argarray);
480 gfc_add_expr_to_block (&block, tmp);
482 return gfc_finish_block (&block);
486 tree
487 gfc_trans_runtime_error (bool error, locus* where, const char* msgid, ...)
489 va_list ap;
490 tree result;
492 va_start (ap, msgid);
493 result = trans_runtime_error_vararg (error, where, msgid, ap);
494 va_end (ap);
495 return result;
499 /* Generate a runtime error if COND is true. */
501 void
502 gfc_trans_runtime_check (bool error, bool once, tree cond, stmtblock_t * pblock,
503 locus * where, const char * msgid, ...)
505 va_list ap;
506 stmtblock_t block;
507 tree body;
508 tree tmp;
509 tree tmpvar = NULL;
511 if (integer_zerop (cond))
512 return;
514 if (once)
516 tmpvar = gfc_create_var (boolean_type_node, "print_warning");
517 TREE_STATIC (tmpvar) = 1;
518 DECL_INITIAL (tmpvar) = boolean_true_node;
519 gfc_add_expr_to_block (pblock, tmpvar);
522 gfc_start_block (&block);
524 /* For error, runtime_error_at already implies PRED_NORETURN. */
525 if (!error && once)
526 gfc_add_expr_to_block (&block, build_predict_expr (PRED_FORTRAN_WARN_ONCE,
527 NOT_TAKEN));
529 /* The code to generate the error. */
530 va_start (ap, msgid);
531 gfc_add_expr_to_block (&block,
532 trans_runtime_error_vararg (error, where,
533 msgid, ap));
534 va_end (ap);
536 if (once)
537 gfc_add_modify (&block, tmpvar, boolean_false_node);
539 body = gfc_finish_block (&block);
541 if (integer_onep (cond))
543 gfc_add_expr_to_block (pblock, body);
545 else
547 if (once)
548 cond = fold_build2_loc (where->lb->location, TRUTH_AND_EXPR,
549 long_integer_type_node, tmpvar, cond);
550 else
551 cond = fold_convert (long_integer_type_node, cond);
553 tmp = fold_build3_loc (where->lb->location, COND_EXPR, void_type_node,
554 cond, body,
555 build_empty_stmt (where->lb->location));
556 gfc_add_expr_to_block (pblock, tmp);
561 /* Call malloc to allocate size bytes of memory, with special conditions:
562 + if size == 0, return a malloced area of size 1,
563 + if malloc returns NULL, issue a runtime error. */
564 tree
565 gfc_call_malloc (stmtblock_t * block, tree type, tree size)
567 tree tmp, msg, malloc_result, null_result, res, malloc_tree;
568 stmtblock_t block2;
570 /* Create a variable to hold the result. */
571 res = gfc_create_var (prvoid_type_node, NULL);
573 /* Call malloc. */
574 gfc_start_block (&block2);
576 size = fold_convert (size_type_node, size);
577 size = fold_build2_loc (input_location, MAX_EXPR, size_type_node, size,
578 build_int_cst (size_type_node, 1));
580 malloc_tree = builtin_decl_explicit (BUILT_IN_MALLOC);
581 gfc_add_modify (&block2, res,
582 fold_convert (prvoid_type_node,
583 build_call_expr_loc (input_location,
584 malloc_tree, 1, size)));
586 /* Optionally check whether malloc was successful. */
587 if (gfc_option.rtcheck & GFC_RTCHECK_MEM)
589 null_result = fold_build2_loc (input_location, EQ_EXPR,
590 boolean_type_node, res,
591 build_int_cst (pvoid_type_node, 0));
592 msg = gfc_build_addr_expr (pchar_type_node,
593 gfc_build_localized_cstring_const ("Memory allocation failed"));
594 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
595 null_result,
596 build_call_expr_loc (input_location,
597 gfor_fndecl_os_error, 1, msg),
598 build_empty_stmt (input_location));
599 gfc_add_expr_to_block (&block2, tmp);
602 malloc_result = gfc_finish_block (&block2);
603 gfc_add_expr_to_block (block, malloc_result);
605 if (type != NULL)
606 res = fold_convert (type, res);
607 return res;
611 /* Allocate memory, using an optional status argument.
613 This function follows the following pseudo-code:
615 void *
616 allocate (size_t size, integer_type stat)
618 void *newmem;
620 if (stat requested)
621 stat = 0;
623 newmem = malloc (MAX (size, 1));
624 if (newmem == NULL)
626 if (stat)
627 *stat = LIBERROR_ALLOCATION;
628 else
629 runtime_error ("Allocation would exceed memory limit");
631 return newmem;
632 } */
633 void
634 gfc_allocate_using_malloc (stmtblock_t * block, tree pointer,
635 tree size, tree status)
637 tree tmp, error_cond;
638 stmtblock_t on_error;
639 tree status_type = status ? TREE_TYPE (status) : NULL_TREE;
641 /* If successful and stat= is given, set status to 0. */
642 if (status != NULL_TREE)
643 gfc_add_expr_to_block (block,
644 fold_build2_loc (input_location, MODIFY_EXPR, status_type,
645 status, build_int_cst (status_type, 0)));
647 /* The allocation itself. */
648 size = fold_convert (size_type_node, size);
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,
663 NOT_TAKEN));
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);
668 else
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:
693 void *
694 allocate (size_t size, void** token, int *stat, char* errmsg, int errlen)
696 void *newmem;
698 newmem = _caf_register (size, regtype, token, &stat, errmsg, errlen);
699 return newmem;
700 } */
701 static void
702 gfc_allocate_using_lib (stmtblock_t * block, tree pointer, tree size,
703 tree token, tree status, tree errmsg, tree errlen,
704 bool lock_var)
706 tree tmp, pstat;
708 gcc_assert (token != NULL_TREE);
710 /* The allocation itself. */
711 if (status == NULL_TREE)
712 pstat = null_pointer_node;
713 else
714 pstat = gfc_build_addr_expr (NULL_TREE, status);
716 if (errmsg == NULL_TREE)
718 gcc_assert(errlen == NULL_TREE);
719 errmsg = null_pointer_node;
720 errlen = build_int_cst (integer_type_node, 0);
723 size = fold_convert (size_type_node, size);
724 tmp = build_call_expr_loc (input_location,
725 gfor_fndecl_caf_register, 6,
726 fold_build2_loc (input_location,
727 MAX_EXPR, size_type_node, size,
728 build_int_cst (size_type_node, 1)),
729 build_int_cst (integer_type_node,
730 lock_var ? GFC_CAF_LOCK_ALLOC
731 : GFC_CAF_COARRAY_ALLOC),
732 token, pstat, errmsg, errlen);
734 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
735 TREE_TYPE (pointer), pointer,
736 fold_convert ( TREE_TYPE (pointer), tmp));
737 gfc_add_expr_to_block (block, tmp);
741 /* Generate code for an ALLOCATE statement when the argument is an
742 allocatable variable. If the variable is currently allocated, it is an
743 error to allocate it again.
745 This function follows the following pseudo-code:
747 void *
748 allocate_allocatable (void *mem, size_t size, integer_type stat)
750 if (mem == NULL)
751 return allocate (size, stat);
752 else
754 if (stat)
755 stat = LIBERROR_ALLOCATION;
756 else
757 runtime_error ("Attempting to allocate already allocated variable");
761 expr must be set to the original expression being allocated for its locus
762 and variable name in case a runtime error has to be printed. */
763 void
764 gfc_allocate_allocatable (stmtblock_t * block, tree mem, tree size, tree token,
765 tree status, tree errmsg, tree errlen, tree label_finish,
766 gfc_expr* expr)
768 stmtblock_t alloc_block;
769 tree tmp, null_mem, alloc, error;
770 tree type = TREE_TYPE (mem);
772 size = fold_convert (size_type_node, size);
773 null_mem = gfc_unlikely (fold_build2_loc (input_location, NE_EXPR,
774 boolean_type_node, mem,
775 build_int_cst (type, 0)),
776 PRED_FORTRAN_FAIL_ALLOC);
778 /* If mem is NULL, we call gfc_allocate_using_malloc or
779 gfc_allocate_using_lib. */
780 gfc_start_block (&alloc_block);
782 if (flag_coarray == GFC_FCOARRAY_LIB
783 && gfc_expr_attr (expr).codimension)
785 tree cond;
786 bool lock_var = expr->ts.type == BT_DERIVED
787 && expr->ts.u.derived->from_intmod
788 == INTMOD_ISO_FORTRAN_ENV
789 && expr->ts.u.derived->intmod_sym_id
790 == ISOFORTRAN_LOCK_TYPE;
791 /* In the front end, we represent the lock variable as pointer. However,
792 the FE only passes the pointer around and leaves the actual
793 representation to the library. Hence, we have to convert back to the
794 number of elements. */
795 if (lock_var)
796 size = fold_build2_loc (input_location, TRUNC_DIV_EXPR, size_type_node,
797 size, TYPE_SIZE_UNIT (ptr_type_node));
799 gfc_allocate_using_lib (&alloc_block, mem, size, token, status,
800 errmsg, errlen, lock_var);
802 if (status != NULL_TREE)
804 TREE_USED (label_finish) = 1;
805 tmp = build1_v (GOTO_EXPR, label_finish);
806 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
807 status, build_zero_cst (TREE_TYPE (status)));
808 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
809 gfc_unlikely (cond, PRED_FORTRAN_FAIL_ALLOC),
810 tmp, build_empty_stmt (input_location));
811 gfc_add_expr_to_block (&alloc_block, tmp);
814 else
815 gfc_allocate_using_malloc (&alloc_block, mem, size, status);
817 alloc = gfc_finish_block (&alloc_block);
819 /* If mem is not NULL, we issue a runtime error or set the
820 status variable. */
821 if (expr)
823 tree varname;
825 gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->symtree);
826 varname = gfc_build_cstring_const (expr->symtree->name);
827 varname = gfc_build_addr_expr (pchar_type_node, varname);
829 error = gfc_trans_runtime_error (true, &expr->where,
830 "Attempting to allocate already"
831 " allocated variable '%s'",
832 varname);
834 else
835 error = gfc_trans_runtime_error (true, NULL,
836 "Attempting to allocate already allocated"
837 " variable");
839 if (status != NULL_TREE)
841 tree status_type = TREE_TYPE (status);
843 error = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
844 status, build_int_cst (status_type, LIBERROR_ALLOCATION));
847 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, null_mem,
848 error, alloc);
849 gfc_add_expr_to_block (block, tmp);
853 /* Free a given variable. */
855 tree
856 gfc_call_free (tree var)
858 return build_call_expr_loc (input_location,
859 builtin_decl_explicit (BUILT_IN_FREE),
860 1, fold_convert (pvoid_type_node, var));
864 /* Build a call to a FINAL procedure, which finalizes "var". */
866 static tree
867 gfc_build_final_call (gfc_typespec ts, gfc_expr *final_wrapper, gfc_expr *var,
868 bool fini_coarray, gfc_expr *class_size)
870 stmtblock_t block;
871 gfc_se se;
872 tree final_fndecl, array, size, tmp;
873 symbol_attribute attr;
875 gcc_assert (final_wrapper->expr_type == EXPR_VARIABLE);
876 gcc_assert (var);
878 gfc_start_block (&block);
879 gfc_init_se (&se, NULL);
880 gfc_conv_expr (&se, final_wrapper);
881 final_fndecl = se.expr;
882 if (POINTER_TYPE_P (TREE_TYPE (final_fndecl)))
883 final_fndecl = build_fold_indirect_ref_loc (input_location, final_fndecl);
885 if (ts.type == BT_DERIVED)
887 tree elem_size;
889 gcc_assert (!class_size);
890 elem_size = gfc_typenode_for_spec (&ts);
891 elem_size = TYPE_SIZE_UNIT (elem_size);
892 size = fold_convert (gfc_array_index_type, elem_size);
894 gfc_init_se (&se, NULL);
895 se.want_pointer = 1;
896 if (var->rank)
898 se.descriptor_only = 1;
899 gfc_conv_expr_descriptor (&se, var);
900 array = se.expr;
902 else
904 gfc_conv_expr (&se, var);
905 gcc_assert (se.pre.head == NULL_TREE && se.post.head == NULL_TREE);
906 array = se.expr;
908 /* No copy back needed, hence set attr's allocatable/pointer
909 to zero. */
910 gfc_clear_attr (&attr);
911 gfc_init_se (&se, NULL);
912 array = gfc_conv_scalar_to_descriptor (&se, array, attr);
913 gcc_assert (se.post.head == NULL_TREE);
916 else
918 gfc_expr *array_expr;
919 gcc_assert (class_size);
920 gfc_init_se (&se, NULL);
921 gfc_conv_expr (&se, class_size);
922 gfc_add_block_to_block (&block, &se.pre);
923 gcc_assert (se.post.head == NULL_TREE);
924 size = se.expr;
926 array_expr = gfc_copy_expr (var);
927 gfc_init_se (&se, NULL);
928 se.want_pointer = 1;
929 if (array_expr->rank)
931 gfc_add_class_array_ref (array_expr);
932 se.descriptor_only = 1;
933 gfc_conv_expr_descriptor (&se, array_expr);
934 array = se.expr;
936 else
938 gfc_add_data_component (array_expr);
939 gfc_conv_expr (&se, array_expr);
940 gfc_add_block_to_block (&block, &se.pre);
941 gcc_assert (se.post.head == NULL_TREE);
942 array = se.expr;
943 if (TREE_CODE (array) == ADDR_EXPR
944 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (array, 0))))
945 tmp = TREE_OPERAND (array, 0);
947 if (!gfc_is_coarray (array_expr))
949 /* No copy back needed, hence set attr's allocatable/pointer
950 to zero. */
951 gfc_clear_attr (&attr);
952 gfc_init_se (&se, NULL);
953 array = gfc_conv_scalar_to_descriptor (&se, array, attr);
955 gcc_assert (se.post.head == NULL_TREE);
957 gfc_free_expr (array_expr);
960 if (!POINTER_TYPE_P (TREE_TYPE (array)))
961 array = gfc_build_addr_expr (NULL, array);
963 gfc_add_block_to_block (&block, &se.pre);
964 tmp = build_call_expr_loc (input_location,
965 final_fndecl, 3, array,
966 size, fini_coarray ? boolean_true_node
967 : boolean_false_node);
968 gfc_add_block_to_block (&block, &se.post);
969 gfc_add_expr_to_block (&block, tmp);
970 return gfc_finish_block (&block);
974 bool
975 gfc_add_comp_finalizer_call (stmtblock_t *block, tree decl, gfc_component *comp,
976 bool fini_coarray)
978 gfc_se se;
979 stmtblock_t block2;
980 tree final_fndecl, size, array, tmp, cond;
981 symbol_attribute attr;
982 gfc_expr *final_expr = NULL;
984 if (comp->ts.type != BT_DERIVED && comp->ts.type != BT_CLASS)
985 return false;
987 gfc_init_block (&block2);
989 if (comp->ts.type == BT_DERIVED)
991 if (comp->attr.pointer)
992 return false;
994 gfc_is_finalizable (comp->ts.u.derived, &final_expr);
995 if (!final_expr)
996 return false;
998 gfc_init_se (&se, NULL);
999 gfc_conv_expr (&se, final_expr);
1000 final_fndecl = se.expr;
1001 size = gfc_typenode_for_spec (&comp->ts);
1002 size = TYPE_SIZE_UNIT (size);
1003 size = fold_convert (gfc_array_index_type, size);
1005 array = decl;
1007 else /* comp->ts.type == BT_CLASS. */
1009 if (CLASS_DATA (comp)->attr.class_pointer)
1010 return false;
1012 gfc_is_finalizable (CLASS_DATA (comp)->ts.u.derived, &final_expr);
1013 final_fndecl = gfc_class_vtab_final_get (decl);
1014 size = gfc_class_vtab_size_get (decl);
1015 array = gfc_class_data_get (decl);
1018 if (comp->attr.allocatable
1019 || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->attr.allocatable))
1021 tmp = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array))
1022 ? gfc_conv_descriptor_data_get (array) : array;
1023 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1024 tmp, fold_convert (TREE_TYPE (tmp),
1025 null_pointer_node));
1027 else
1028 cond = boolean_true_node;
1030 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array)))
1032 gfc_clear_attr (&attr);
1033 gfc_init_se (&se, NULL);
1034 array = gfc_conv_scalar_to_descriptor (&se, array, attr);
1035 gfc_add_block_to_block (&block2, &se.pre);
1036 gcc_assert (se.post.head == NULL_TREE);
1039 if (!POINTER_TYPE_P (TREE_TYPE (array)))
1040 array = gfc_build_addr_expr (NULL, array);
1042 if (!final_expr)
1044 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1045 final_fndecl,
1046 fold_convert (TREE_TYPE (final_fndecl),
1047 null_pointer_node));
1048 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1049 boolean_type_node, cond, tmp);
1052 if (POINTER_TYPE_P (TREE_TYPE (final_fndecl)))
1053 final_fndecl = build_fold_indirect_ref_loc (input_location, final_fndecl);
1055 tmp = build_call_expr_loc (input_location,
1056 final_fndecl, 3, array,
1057 size, fini_coarray ? boolean_true_node
1058 : boolean_false_node);
1059 gfc_add_expr_to_block (&block2, tmp);
1060 tmp = gfc_finish_block (&block2);
1062 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
1063 build_empty_stmt (input_location));
1064 gfc_add_expr_to_block (block, tmp);
1066 return true;
1070 /* Add a call to the finalizer, using the passed *expr. Returns
1071 true when a finalizer call has been inserted. */
1073 bool
1074 gfc_add_finalizer_call (stmtblock_t *block, gfc_expr *expr2)
1076 tree tmp;
1077 gfc_ref *ref;
1078 gfc_expr *expr;
1079 gfc_expr *final_expr = NULL;
1080 gfc_expr *elem_size = NULL;
1081 bool has_finalizer = false;
1083 if (!expr2 || (expr2->ts.type != BT_DERIVED && expr2->ts.type != BT_CLASS))
1084 return false;
1086 if (expr2->ts.type == BT_DERIVED)
1088 gfc_is_finalizable (expr2->ts.u.derived, &final_expr);
1089 if (!final_expr)
1090 return false;
1093 /* If we have a class array, we need go back to the class
1094 container. */
1095 expr = gfc_copy_expr (expr2);
1097 if (expr->ref && expr->ref->next && !expr->ref->next->next
1098 && expr->ref->next->type == REF_ARRAY
1099 && expr->ref->type == REF_COMPONENT
1100 && strcmp (expr->ref->u.c.component->name, "_data") == 0)
1102 gfc_free_ref_list (expr->ref);
1103 expr->ref = NULL;
1105 else
1106 for (ref = expr->ref; ref; ref = ref->next)
1107 if (ref->next && ref->next->next && !ref->next->next->next
1108 && ref->next->next->type == REF_ARRAY
1109 && ref->next->type == REF_COMPONENT
1110 && strcmp (ref->next->u.c.component->name, "_data") == 0)
1112 gfc_free_ref_list (ref->next);
1113 ref->next = NULL;
1116 if (expr->ts.type == BT_CLASS)
1118 has_finalizer = gfc_is_finalizable (expr->ts.u.derived, NULL);
1120 if (!expr2->rank && !expr2->ref && CLASS_DATA (expr2->symtree->n.sym)->as)
1121 expr->rank = CLASS_DATA (expr2->symtree->n.sym)->as->rank;
1123 final_expr = gfc_copy_expr (expr);
1124 gfc_add_vptr_component (final_expr);
1125 gfc_add_component_ref (final_expr, "_final");
1127 elem_size = gfc_copy_expr (expr);
1128 gfc_add_vptr_component (elem_size);
1129 gfc_add_component_ref (elem_size, "_size");
1132 gcc_assert (final_expr->expr_type == EXPR_VARIABLE);
1134 tmp = gfc_build_final_call (expr->ts, final_expr, expr,
1135 false, elem_size);
1137 if (expr->ts.type == BT_CLASS && !has_finalizer)
1139 tree cond;
1140 gfc_se se;
1142 gfc_init_se (&se, NULL);
1143 se.want_pointer = 1;
1144 gfc_conv_expr (&se, final_expr);
1145 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1146 se.expr, build_int_cst (TREE_TYPE (se.expr), 0));
1148 /* For CLASS(*) not only sym->_vtab->_final can be NULL
1149 but already sym->_vtab itself. */
1150 if (UNLIMITED_POLY (expr))
1152 tree cond2;
1153 gfc_expr *vptr_expr;
1155 vptr_expr = gfc_copy_expr (expr);
1156 gfc_add_vptr_component (vptr_expr);
1158 gfc_init_se (&se, NULL);
1159 se.want_pointer = 1;
1160 gfc_conv_expr (&se, vptr_expr);
1161 gfc_free_expr (vptr_expr);
1163 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1164 se.expr,
1165 build_int_cst (TREE_TYPE (se.expr), 0));
1166 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1167 boolean_type_node, cond2, cond);
1170 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1171 cond, tmp, build_empty_stmt (input_location));
1174 gfc_add_expr_to_block (block, tmp);
1176 return true;
1180 /* User-deallocate; we emit the code directly from the front-end, and the
1181 logic is the same as the previous library function:
1183 void
1184 deallocate (void *pointer, GFC_INTEGER_4 * stat)
1186 if (!pointer)
1188 if (stat)
1189 *stat = 1;
1190 else
1191 runtime_error ("Attempt to DEALLOCATE unallocated memory.");
1193 else
1195 free (pointer);
1196 if (stat)
1197 *stat = 0;
1201 In this front-end version, status doesn't have to be GFC_INTEGER_4.
1202 Moreover, if CAN_FAIL is true, then we will not emit a runtime error,
1203 even when no status variable is passed to us (this is used for
1204 unconditional deallocation generated by the front-end at end of
1205 each procedure).
1207 If a runtime-message is possible, `expr' must point to the original
1208 expression being deallocated for its locus and variable name.
1210 For coarrays, "pointer" must be the array descriptor and not its
1211 "data" component. */
1212 tree
1213 gfc_deallocate_with_status (tree pointer, tree status, tree errmsg,
1214 tree errlen, tree label_finish,
1215 bool can_fail, gfc_expr* expr, bool coarray)
1217 stmtblock_t null, non_null;
1218 tree cond, tmp, error;
1219 tree status_type = NULL_TREE;
1220 tree caf_decl = NULL_TREE;
1222 if (coarray)
1224 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (pointer)));
1225 caf_decl = pointer;
1226 pointer = gfc_conv_descriptor_data_get (caf_decl);
1227 STRIP_NOPS (pointer);
1230 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, pointer,
1231 build_int_cst (TREE_TYPE (pointer), 0));
1233 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
1234 we emit a runtime error. */
1235 gfc_start_block (&null);
1236 if (!can_fail)
1238 tree varname;
1240 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
1242 varname = gfc_build_cstring_const (expr->symtree->name);
1243 varname = gfc_build_addr_expr (pchar_type_node, varname);
1245 error = gfc_trans_runtime_error (true, &expr->where,
1246 "Attempt to DEALLOCATE unallocated '%s'",
1247 varname);
1249 else
1250 error = build_empty_stmt (input_location);
1252 if (status != NULL_TREE && !integer_zerop (status))
1254 tree cond2;
1256 status_type = TREE_TYPE (TREE_TYPE (status));
1257 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1258 status, build_int_cst (TREE_TYPE (status), 0));
1259 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
1260 fold_build1_loc (input_location, INDIRECT_REF,
1261 status_type, status),
1262 build_int_cst (status_type, 1));
1263 error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1264 cond2, tmp, error);
1267 gfc_add_expr_to_block (&null, error);
1269 /* When POINTER is not NULL, we free it. */
1270 gfc_start_block (&non_null);
1271 gfc_add_finalizer_call (&non_null, expr);
1272 if (!coarray || flag_coarray != GFC_FCOARRAY_LIB)
1274 tmp = build_call_expr_loc (input_location,
1275 builtin_decl_explicit (BUILT_IN_FREE), 1,
1276 fold_convert (pvoid_type_node, pointer));
1277 gfc_add_expr_to_block (&non_null, tmp);
1279 if (status != NULL_TREE && !integer_zerop (status))
1281 /* We set STATUS to zero if it is present. */
1282 tree status_type = TREE_TYPE (TREE_TYPE (status));
1283 tree cond2;
1285 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1286 status,
1287 build_int_cst (TREE_TYPE (status), 0));
1288 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
1289 fold_build1_loc (input_location, INDIRECT_REF,
1290 status_type, status),
1291 build_int_cst (status_type, 0));
1292 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1293 gfc_unlikely (cond2, PRED_FORTRAN_FAIL_ALLOC),
1294 tmp, build_empty_stmt (input_location));
1295 gfc_add_expr_to_block (&non_null, tmp);
1298 else
1300 tree caf_type, token, cond2;
1301 tree pstat = null_pointer_node;
1303 if (errmsg == NULL_TREE)
1305 gcc_assert (errlen == NULL_TREE);
1306 errmsg = null_pointer_node;
1307 errlen = build_zero_cst (integer_type_node);
1309 else
1311 gcc_assert (errlen != NULL_TREE);
1312 if (!POINTER_TYPE_P (TREE_TYPE (errmsg)))
1313 errmsg = gfc_build_addr_expr (NULL_TREE, errmsg);
1316 caf_type = TREE_TYPE (caf_decl);
1318 if (status != NULL_TREE && !integer_zerop (status))
1320 gcc_assert (status_type == integer_type_node);
1321 pstat = status;
1324 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
1325 && GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE)
1326 token = gfc_conv_descriptor_token (caf_decl);
1327 else if (DECL_LANG_SPECIFIC (caf_decl)
1328 && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
1329 token = GFC_DECL_TOKEN (caf_decl);
1330 else
1332 gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
1333 && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
1334 token = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
1337 token = gfc_build_addr_expr (NULL_TREE, token);
1338 tmp = build_call_expr_loc (input_location,
1339 gfor_fndecl_caf_deregister, 4,
1340 token, pstat, errmsg, errlen);
1341 gfc_add_expr_to_block (&non_null, tmp);
1343 if (status != NULL_TREE)
1345 tree stat = build_fold_indirect_ref_loc (input_location, status);
1347 TREE_USED (label_finish) = 1;
1348 tmp = build1_v (GOTO_EXPR, label_finish);
1349 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1350 stat, build_zero_cst (TREE_TYPE (stat)));
1351 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1352 gfc_unlikely (cond2, PRED_FORTRAN_FAIL_ALLOC),
1353 tmp, build_empty_stmt (input_location));
1354 gfc_add_expr_to_block (&non_null, tmp);
1358 return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
1359 gfc_finish_block (&null),
1360 gfc_finish_block (&non_null));
1364 /* Generate code for deallocation of allocatable scalars (variables or
1365 components). Before the object itself is freed, any allocatable
1366 subcomponents are being deallocated. */
1368 tree
1369 gfc_deallocate_scalar_with_status (tree pointer, tree status, bool can_fail,
1370 gfc_expr* expr, gfc_typespec ts)
1372 stmtblock_t null, non_null;
1373 tree cond, tmp, error;
1374 bool finalizable;
1376 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, pointer,
1377 build_int_cst (TREE_TYPE (pointer), 0));
1379 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
1380 we emit a runtime error. */
1381 gfc_start_block (&null);
1382 if (!can_fail)
1384 tree varname;
1386 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
1388 varname = gfc_build_cstring_const (expr->symtree->name);
1389 varname = gfc_build_addr_expr (pchar_type_node, varname);
1391 error = gfc_trans_runtime_error (true, &expr->where,
1392 "Attempt to DEALLOCATE unallocated '%s'",
1393 varname);
1395 else
1396 error = build_empty_stmt (input_location);
1398 if (status != NULL_TREE && !integer_zerop (status))
1400 tree status_type = TREE_TYPE (TREE_TYPE (status));
1401 tree cond2;
1403 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1404 status, build_int_cst (TREE_TYPE (status), 0));
1405 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
1406 fold_build1_loc (input_location, INDIRECT_REF,
1407 status_type, status),
1408 build_int_cst (status_type, 1));
1409 error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1410 cond2, tmp, error);
1413 gfc_add_expr_to_block (&null, error);
1415 /* When POINTER is not NULL, we free it. */
1416 gfc_start_block (&non_null);
1418 /* Free allocatable components. */
1419 finalizable = gfc_add_finalizer_call (&non_null, expr);
1420 if (!finalizable && ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp)
1422 tmp = build_fold_indirect_ref_loc (input_location, pointer);
1423 tmp = gfc_deallocate_alloc_comp (ts.u.derived, tmp, 0);
1424 gfc_add_expr_to_block (&non_null, tmp);
1427 tmp = build_call_expr_loc (input_location,
1428 builtin_decl_explicit (BUILT_IN_FREE), 1,
1429 fold_convert (pvoid_type_node, pointer));
1430 gfc_add_expr_to_block (&non_null, tmp);
1432 if (status != NULL_TREE && !integer_zerop (status))
1434 /* We set STATUS to zero if it is present. */
1435 tree status_type = TREE_TYPE (TREE_TYPE (status));
1436 tree cond2;
1438 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1439 status, build_int_cst (TREE_TYPE (status), 0));
1440 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
1441 fold_build1_loc (input_location, INDIRECT_REF,
1442 status_type, status),
1443 build_int_cst (status_type, 0));
1444 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
1445 tmp, build_empty_stmt (input_location));
1446 gfc_add_expr_to_block (&non_null, tmp);
1449 return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
1450 gfc_finish_block (&null),
1451 gfc_finish_block (&non_null));
1455 /* Reallocate MEM so it has SIZE bytes of data. This behaves like the
1456 following pseudo-code:
1458 void *
1459 internal_realloc (void *mem, size_t size)
1461 res = realloc (mem, size);
1462 if (!res && size != 0)
1463 _gfortran_os_error ("Allocation would exceed memory limit");
1465 return res;
1466 } */
1467 tree
1468 gfc_call_realloc (stmtblock_t * block, tree mem, tree size)
1470 tree msg, res, nonzero, null_result, tmp;
1471 tree type = TREE_TYPE (mem);
1473 /* Only evaluate the size once. */
1474 size = save_expr (fold_convert (size_type_node, size));
1476 /* Create a variable to hold the result. */
1477 res = gfc_create_var (type, NULL);
1479 /* Call realloc and check the result. */
1480 tmp = build_call_expr_loc (input_location,
1481 builtin_decl_explicit (BUILT_IN_REALLOC), 2,
1482 fold_convert (pvoid_type_node, mem), size);
1483 gfc_add_modify (block, res, fold_convert (type, tmp));
1484 null_result = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
1485 res, build_int_cst (pvoid_type_node, 0));
1486 nonzero = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, size,
1487 build_int_cst (size_type_node, 0));
1488 null_result = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
1489 null_result, nonzero);
1490 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
1491 ("Allocation would exceed memory limit"));
1492 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1493 null_result,
1494 build_call_expr_loc (input_location,
1495 gfor_fndecl_os_error, 1, msg),
1496 build_empty_stmt (input_location));
1497 gfc_add_expr_to_block (block, tmp);
1499 return res;
1503 /* Add an expression to another one, either at the front or the back. */
1505 static void
1506 add_expr_to_chain (tree* chain, tree expr, bool front)
1508 if (expr == NULL_TREE || IS_EMPTY_STMT (expr))
1509 return;
1511 if (*chain)
1513 if (TREE_CODE (*chain) != STATEMENT_LIST)
1515 tree tmp;
1517 tmp = *chain;
1518 *chain = NULL_TREE;
1519 append_to_statement_list (tmp, chain);
1522 if (front)
1524 tree_stmt_iterator i;
1526 i = tsi_start (*chain);
1527 tsi_link_before (&i, expr, TSI_CONTINUE_LINKING);
1529 else
1530 append_to_statement_list (expr, chain);
1532 else
1533 *chain = expr;
1537 /* Add a statement at the end of a block. */
1539 void
1540 gfc_add_expr_to_block (stmtblock_t * block, tree expr)
1542 gcc_assert (block);
1543 add_expr_to_chain (&block->head, expr, false);
1547 /* Add a statement at the beginning of a block. */
1549 void
1550 gfc_prepend_expr_to_block (stmtblock_t * block, tree expr)
1552 gcc_assert (block);
1553 add_expr_to_chain (&block->head, expr, true);
1557 /* Add a block the end of a block. */
1559 void
1560 gfc_add_block_to_block (stmtblock_t * block, stmtblock_t * append)
1562 gcc_assert (append);
1563 gcc_assert (!append->has_scope);
1565 gfc_add_expr_to_block (block, append->head);
1566 append->head = NULL_TREE;
1570 /* Save the current locus. The structure may not be complete, and should
1571 only be used with gfc_restore_backend_locus. */
1573 void
1574 gfc_save_backend_locus (locus * loc)
1576 loc->lb = XCNEW (gfc_linebuf);
1577 loc->lb->location = input_location;
1578 loc->lb->file = gfc_current_backend_file;
1582 /* Set the current locus. */
1584 void
1585 gfc_set_backend_locus (locus * loc)
1587 gfc_current_backend_file = loc->lb->file;
1588 input_location = loc->lb->location;
1592 /* Restore the saved locus. Only used in conjunction with
1593 gfc_save_backend_locus, to free the memory when we are done. */
1595 void
1596 gfc_restore_backend_locus (locus * loc)
1598 gfc_set_backend_locus (loc);
1599 free (loc->lb);
1603 /* Translate an executable statement. The tree cond is used by gfc_trans_do.
1604 This static function is wrapped by gfc_trans_code_cond and
1605 gfc_trans_code. */
1607 static tree
1608 trans_code (gfc_code * code, tree cond)
1610 stmtblock_t block;
1611 tree res;
1613 if (!code)
1614 return build_empty_stmt (input_location);
1616 gfc_start_block (&block);
1618 /* Translate statements one by one into GENERIC trees until we reach
1619 the end of this gfc_code branch. */
1620 for (; code; code = code->next)
1622 if (code->here != 0)
1624 res = gfc_trans_label_here (code);
1625 gfc_add_expr_to_block (&block, res);
1628 gfc_set_backend_locus (&code->loc);
1630 switch (code->op)
1632 case EXEC_NOP:
1633 case EXEC_END_BLOCK:
1634 case EXEC_END_NESTED_BLOCK:
1635 case EXEC_END_PROCEDURE:
1636 res = NULL_TREE;
1637 break;
1639 case EXEC_ASSIGN:
1640 if (code->expr1->ts.type == BT_CLASS)
1641 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1642 else
1643 res = gfc_trans_assign (code);
1644 break;
1646 case EXEC_LABEL_ASSIGN:
1647 res = gfc_trans_label_assign (code);
1648 break;
1650 case EXEC_POINTER_ASSIGN:
1651 if (code->expr1->ts.type == BT_CLASS)
1652 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1653 else if (UNLIMITED_POLY (code->expr2)
1654 && code->expr1->ts.type == BT_DERIVED
1655 && (code->expr1->ts.u.derived->attr.sequence
1656 || code->expr1->ts.u.derived->attr.is_bind_c))
1657 /* F2003: C717 */
1658 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1659 else
1660 res = gfc_trans_pointer_assign (code);
1661 break;
1663 case EXEC_INIT_ASSIGN:
1664 if (code->expr1->ts.type == BT_CLASS)
1665 res = gfc_trans_class_init_assign (code);
1666 else
1667 res = gfc_trans_init_assign (code);
1668 break;
1670 case EXEC_CONTINUE:
1671 res = NULL_TREE;
1672 break;
1674 case EXEC_CRITICAL:
1675 res = gfc_trans_critical (code);
1676 break;
1678 case EXEC_CYCLE:
1679 res = gfc_trans_cycle (code);
1680 break;
1682 case EXEC_EXIT:
1683 res = gfc_trans_exit (code);
1684 break;
1686 case EXEC_GOTO:
1687 res = gfc_trans_goto (code);
1688 break;
1690 case EXEC_ENTRY:
1691 res = gfc_trans_entry (code);
1692 break;
1694 case EXEC_PAUSE:
1695 res = gfc_trans_pause (code);
1696 break;
1698 case EXEC_STOP:
1699 case EXEC_ERROR_STOP:
1700 res = gfc_trans_stop (code, code->op == EXEC_ERROR_STOP);
1701 break;
1703 case EXEC_CALL:
1704 /* For MVBITS we've got the special exception that we need a
1705 dependency check, too. */
1707 bool is_mvbits = false;
1709 if (code->resolved_isym)
1711 res = gfc_conv_intrinsic_subroutine (code);
1712 if (res != NULL_TREE)
1713 break;
1716 if (code->resolved_isym
1717 && code->resolved_isym->id == GFC_ISYM_MVBITS)
1718 is_mvbits = true;
1720 res = gfc_trans_call (code, is_mvbits, NULL_TREE,
1721 NULL_TREE, false);
1723 break;
1725 case EXEC_CALL_PPC:
1726 res = gfc_trans_call (code, false, NULL_TREE,
1727 NULL_TREE, false);
1728 break;
1730 case EXEC_ASSIGN_CALL:
1731 res = gfc_trans_call (code, true, NULL_TREE,
1732 NULL_TREE, false);
1733 break;
1735 case EXEC_RETURN:
1736 res = gfc_trans_return (code);
1737 break;
1739 case EXEC_IF:
1740 res = gfc_trans_if (code);
1741 break;
1743 case EXEC_ARITHMETIC_IF:
1744 res = gfc_trans_arithmetic_if (code);
1745 break;
1747 case EXEC_BLOCK:
1748 res = gfc_trans_block_construct (code);
1749 break;
1751 case EXEC_DO:
1752 res = gfc_trans_do (code, cond);
1753 break;
1755 case EXEC_DO_CONCURRENT:
1756 res = gfc_trans_do_concurrent (code);
1757 break;
1759 case EXEC_DO_WHILE:
1760 res = gfc_trans_do_while (code);
1761 break;
1763 case EXEC_SELECT:
1764 res = gfc_trans_select (code);
1765 break;
1767 case EXEC_SELECT_TYPE:
1768 /* Do nothing. SELECT TYPE statements should be transformed into
1769 an ordinary SELECT CASE at resolution stage.
1770 TODO: Add an error message here once this is done. */
1771 res = NULL_TREE;
1772 break;
1774 case EXEC_FLUSH:
1775 res = gfc_trans_flush (code);
1776 break;
1778 case EXEC_SYNC_ALL:
1779 case EXEC_SYNC_IMAGES:
1780 case EXEC_SYNC_MEMORY:
1781 res = gfc_trans_sync (code, code->op);
1782 break;
1784 case EXEC_LOCK:
1785 case EXEC_UNLOCK:
1786 res = gfc_trans_lock_unlock (code, code->op);
1787 break;
1789 case EXEC_FORALL:
1790 res = gfc_trans_forall (code);
1791 break;
1793 case EXEC_WHERE:
1794 res = gfc_trans_where (code);
1795 break;
1797 case EXEC_ALLOCATE:
1798 res = gfc_trans_allocate (code);
1799 break;
1801 case EXEC_DEALLOCATE:
1802 res = gfc_trans_deallocate (code);
1803 break;
1805 case EXEC_OPEN:
1806 res = gfc_trans_open (code);
1807 break;
1809 case EXEC_CLOSE:
1810 res = gfc_trans_close (code);
1811 break;
1813 case EXEC_READ:
1814 res = gfc_trans_read (code);
1815 break;
1817 case EXEC_WRITE:
1818 res = gfc_trans_write (code);
1819 break;
1821 case EXEC_IOLENGTH:
1822 res = gfc_trans_iolength (code);
1823 break;
1825 case EXEC_BACKSPACE:
1826 res = gfc_trans_backspace (code);
1827 break;
1829 case EXEC_ENDFILE:
1830 res = gfc_trans_endfile (code);
1831 break;
1833 case EXEC_INQUIRE:
1834 res = gfc_trans_inquire (code);
1835 break;
1837 case EXEC_WAIT:
1838 res = gfc_trans_wait (code);
1839 break;
1841 case EXEC_REWIND:
1842 res = gfc_trans_rewind (code);
1843 break;
1845 case EXEC_TRANSFER:
1846 res = gfc_trans_transfer (code);
1847 break;
1849 case EXEC_DT_END:
1850 res = gfc_trans_dt_end (code);
1851 break;
1853 case EXEC_OMP_ATOMIC:
1854 case EXEC_OMP_BARRIER:
1855 case EXEC_OMP_CANCEL:
1856 case EXEC_OMP_CANCELLATION_POINT:
1857 case EXEC_OMP_CRITICAL:
1858 case EXEC_OMP_DISTRIBUTE:
1859 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
1860 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
1861 case EXEC_OMP_DISTRIBUTE_SIMD:
1862 case EXEC_OMP_DO:
1863 case EXEC_OMP_DO_SIMD:
1864 case EXEC_OMP_FLUSH:
1865 case EXEC_OMP_MASTER:
1866 case EXEC_OMP_ORDERED:
1867 case EXEC_OMP_PARALLEL:
1868 case EXEC_OMP_PARALLEL_DO:
1869 case EXEC_OMP_PARALLEL_DO_SIMD:
1870 case EXEC_OMP_PARALLEL_SECTIONS:
1871 case EXEC_OMP_PARALLEL_WORKSHARE:
1872 case EXEC_OMP_SECTIONS:
1873 case EXEC_OMP_SIMD:
1874 case EXEC_OMP_SINGLE:
1875 case EXEC_OMP_TARGET:
1876 case EXEC_OMP_TARGET_DATA:
1877 case EXEC_OMP_TARGET_TEAMS:
1878 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
1879 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
1880 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
1881 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
1882 case EXEC_OMP_TARGET_UPDATE:
1883 case EXEC_OMP_TASK:
1884 case EXEC_OMP_TASKGROUP:
1885 case EXEC_OMP_TASKWAIT:
1886 case EXEC_OMP_TASKYIELD:
1887 case EXEC_OMP_TEAMS:
1888 case EXEC_OMP_TEAMS_DISTRIBUTE:
1889 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
1890 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
1891 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
1892 case EXEC_OMP_WORKSHARE:
1893 res = gfc_trans_omp_directive (code);
1894 break;
1896 case EXEC_OACC_CACHE:
1897 case EXEC_OACC_WAIT:
1898 case EXEC_OACC_UPDATE:
1899 case EXEC_OACC_LOOP:
1900 case EXEC_OACC_HOST_DATA:
1901 case EXEC_OACC_DATA:
1902 case EXEC_OACC_KERNELS:
1903 case EXEC_OACC_KERNELS_LOOP:
1904 case EXEC_OACC_PARALLEL:
1905 case EXEC_OACC_PARALLEL_LOOP:
1906 case EXEC_OACC_ENTER_DATA:
1907 case EXEC_OACC_EXIT_DATA:
1908 res = gfc_trans_oacc_directive (code);
1909 break;
1911 default:
1912 gfc_internal_error ("gfc_trans_code(): Bad statement code");
1915 gfc_set_backend_locus (&code->loc);
1917 if (res != NULL_TREE && ! IS_EMPTY_STMT (res))
1919 if (TREE_CODE (res) != STATEMENT_LIST)
1920 SET_EXPR_LOCATION (res, input_location);
1922 /* Add the new statement to the block. */
1923 gfc_add_expr_to_block (&block, res);
1927 /* Return the finished block. */
1928 return gfc_finish_block (&block);
1932 /* Translate an executable statement with condition, cond. The condition is
1933 used by gfc_trans_do to test for IO result conditions inside implied
1934 DO loops of READ and WRITE statements. See build_dt in trans-io.c. */
1936 tree
1937 gfc_trans_code_cond (gfc_code * code, tree cond)
1939 return trans_code (code, cond);
1942 /* Translate an executable statement without condition. */
1944 tree
1945 gfc_trans_code (gfc_code * code)
1947 return trans_code (code, NULL_TREE);
1951 /* This function is called after a complete program unit has been parsed
1952 and resolved. */
1954 void
1955 gfc_generate_code (gfc_namespace * ns)
1957 ompws_flags = 0;
1958 if (ns->is_block_data)
1960 gfc_generate_block_data (ns);
1961 return;
1964 gfc_generate_function_code (ns);
1968 /* This function is called after a complete module has been parsed
1969 and resolved. */
1971 void
1972 gfc_generate_module_code (gfc_namespace * ns)
1974 gfc_namespace *n;
1975 struct module_htab_entry *entry;
1977 gcc_assert (ns->proc_name->backend_decl == NULL);
1978 ns->proc_name->backend_decl
1979 = build_decl (ns->proc_name->declared_at.lb->location,
1980 NAMESPACE_DECL, get_identifier (ns->proc_name->name),
1981 void_type_node);
1982 entry = gfc_find_module (ns->proc_name->name);
1983 if (entry->namespace_decl)
1984 /* Buggy sourcecode, using a module before defining it? */
1985 entry->decls->empty ();
1986 entry->namespace_decl = ns->proc_name->backend_decl;
1988 gfc_generate_module_vars (ns);
1990 /* We need to generate all module function prototypes first, to allow
1991 sibling calls. */
1992 for (n = ns->contained; n; n = n->sibling)
1994 gfc_entry_list *el;
1996 if (!n->proc_name)
1997 continue;
1999 gfc_create_function_decl (n, false);
2000 DECL_CONTEXT (n->proc_name->backend_decl) = ns->proc_name->backend_decl;
2001 gfc_module_add_decl (entry, n->proc_name->backend_decl);
2002 for (el = ns->entries; el; el = el->next)
2004 DECL_CONTEXT (el->sym->backend_decl) = ns->proc_name->backend_decl;
2005 gfc_module_add_decl (entry, el->sym->backend_decl);
2009 for (n = ns->contained; n; n = n->sibling)
2011 if (!n->proc_name)
2012 continue;
2014 gfc_generate_function_code (n);
2019 /* Initialize an init/cleanup block with existing code. */
2021 void
2022 gfc_start_wrapped_block (gfc_wrapped_block* block, tree code)
2024 gcc_assert (block);
2026 block->init = NULL_TREE;
2027 block->code = code;
2028 block->cleanup = NULL_TREE;
2032 /* Add a new pair of initializers/clean-up code. */
2034 void
2035 gfc_add_init_cleanup (gfc_wrapped_block* block, tree init, tree cleanup)
2037 gcc_assert (block);
2039 /* The new pair of init/cleanup should be "wrapped around" the existing
2040 block of code, thus the initialization is added to the front and the
2041 cleanup to the back. */
2042 add_expr_to_chain (&block->init, init, true);
2043 add_expr_to_chain (&block->cleanup, cleanup, false);
2047 /* Finish up a wrapped block by building a corresponding try-finally expr. */
2049 tree
2050 gfc_finish_wrapped_block (gfc_wrapped_block* block)
2052 tree result;
2054 gcc_assert (block);
2056 /* Build the final expression. For this, just add init and body together,
2057 and put clean-up with that into a TRY_FINALLY_EXPR. */
2058 result = block->init;
2059 add_expr_to_chain (&result, block->code, false);
2060 if (block->cleanup)
2061 result = build2_loc (input_location, TRY_FINALLY_EXPR, void_type_node,
2062 result, block->cleanup);
2064 /* Clear the block. */
2065 block->init = NULL_TREE;
2066 block->code = NULL_TREE;
2067 block->cleanup = NULL_TREE;
2069 return result;
2073 /* Helper function for marking a boolean expression tree as unlikely. */
2075 tree
2076 gfc_unlikely (tree cond, enum br_predictor predictor)
2078 tree tmp;
2080 if (optimize)
2082 cond = fold_convert (long_integer_type_node, cond);
2083 tmp = build_zero_cst (long_integer_type_node);
2084 cond = build_call_expr_loc (input_location,
2085 builtin_decl_explicit (BUILT_IN_EXPECT),
2086 3, cond, tmp,
2087 build_int_cst (integer_type_node,
2088 predictor));
2090 cond = fold_convert (boolean_type_node, cond);
2091 return cond;
2095 /* Helper function for marking a boolean expression tree as likely. */
2097 tree
2098 gfc_likely (tree cond, enum br_predictor predictor)
2100 tree tmp;
2102 if (optimize)
2104 cond = fold_convert (long_integer_type_node, cond);
2105 tmp = build_one_cst (long_integer_type_node);
2106 cond = build_call_expr_loc (input_location,
2107 builtin_decl_explicit (BUILT_IN_EXPECT),
2108 3, cond, tmp,
2109 build_int_cst (integer_type_node,
2110 predictor));
2112 cond = fold_convert (boolean_type_node, cond);
2113 return cond;
2117 /* Get the string length for a deferred character length component. */
2119 bool
2120 gfc_deferred_strlen (gfc_component *c, tree *decl)
2122 char name[GFC_MAX_SYMBOL_LEN+9];
2123 gfc_component *strlen;
2124 if (!(c->ts.type == BT_CHARACTER && c->ts.deferred))
2125 return false;
2126 sprintf (name, "_%s_length", c->name);
2127 for (strlen = c; strlen; strlen = strlen->next)
2128 if (strcmp (strlen->name, name) == 0)
2129 break;
2130 *decl = strlen ? strlen->backend_decl : NULL_TREE;
2131 return strlen != NULL;