Daily bump.
[official-gcc.git] / gcc / fortran / trans.c
blobb7ec0e52cf978aaa97b3e7205eba44459c328dd3
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 "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "options.h"
33 #include "wide-int.h"
34 #include "inchash.h"
35 #include "tree.h"
36 #include "fold-const.h"
37 #include "gimple-expr.h" /* For create_tmp_var_raw. */
38 #include "stringpool.h"
39 #include "tree-iterator.h"
40 #include "diagnostic-core.h" /* For internal_error. */
41 #include "flags.h"
42 #include "trans.h"
43 #include "trans-stmt.h"
44 #include "trans-array.h"
45 #include "trans-types.h"
46 #include "trans-const.h"
48 /* Naming convention for backend interface code:
50 gfc_trans_* translate gfc_code into STMT trees.
52 gfc_conv_* expression conversion
54 gfc_get_* get a backend tree representation of a decl or type */
56 static gfc_file *gfc_current_backend_file;
58 const char gfc_msg_fault[] = N_("Array reference out of bounds");
59 const char gfc_msg_wrong_return[] = N_("Incorrect function return value");
62 /* Advance along TREE_CHAIN n times. */
64 tree
65 gfc_advance_chain (tree t, int n)
67 for (; n > 0; n--)
69 gcc_assert (t != NULL_TREE);
70 t = DECL_CHAIN (t);
72 return t;
76 /* Strip off a legitimate source ending from the input
77 string NAME of length LEN. */
79 static inline void
80 remove_suffix (char *name, int len)
82 int i;
84 for (i = 2; i < 8 && len > i; i++)
86 if (name[len - i] == '.')
88 name[len - i] = '\0';
89 break;
95 /* Creates a variable declaration with a given TYPE. */
97 tree
98 gfc_create_var_np (tree type, const char *prefix)
100 tree t;
102 t = create_tmp_var_raw (type, prefix);
104 /* No warnings for anonymous variables. */
105 if (prefix == NULL)
106 TREE_NO_WARNING (t) = 1;
108 return t;
112 /* Like above, but also adds it to the current scope. */
114 tree
115 gfc_create_var (tree type, const char *prefix)
117 tree tmp;
119 tmp = gfc_create_var_np (type, prefix);
121 pushdecl (tmp);
123 return tmp;
127 /* If the expression is not constant, evaluate it now. We assign the
128 result of the expression to an artificially created variable VAR, and
129 return a pointer to the VAR_DECL node for this variable. */
131 tree
132 gfc_evaluate_now_loc (location_t loc, tree expr, stmtblock_t * pblock)
134 tree var;
136 if (CONSTANT_CLASS_P (expr))
137 return expr;
139 var = gfc_create_var (TREE_TYPE (expr), NULL);
140 gfc_add_modify_loc (loc, pblock, var, expr);
142 return var;
146 tree
147 gfc_evaluate_now (tree expr, stmtblock_t * pblock)
149 return gfc_evaluate_now_loc (input_location, expr, pblock);
153 /* Build a MODIFY_EXPR node and add it to a given statement block PBLOCK.
154 A MODIFY_EXPR is an assignment:
155 LHS <- RHS. */
157 void
158 gfc_add_modify_loc (location_t loc, stmtblock_t * pblock, tree lhs, tree rhs)
160 tree tmp;
162 #ifdef ENABLE_CHECKING
163 tree t1, t2;
164 t1 = TREE_TYPE (rhs);
165 t2 = TREE_TYPE (lhs);
166 /* Make sure that the types of the rhs and the lhs are the same
167 for scalar assignments. We should probably have something
168 similar for aggregates, but right now removing that check just
169 breaks everything. */
170 gcc_assert (t1 == t2
171 || AGGREGATE_TYPE_P (TREE_TYPE (lhs)));
172 #endif
174 tmp = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, lhs,
175 rhs);
176 gfc_add_expr_to_block (pblock, tmp);
180 void
181 gfc_add_modify (stmtblock_t * pblock, tree lhs, tree rhs)
183 gfc_add_modify_loc (input_location, pblock, lhs, rhs);
187 /* Create a new scope/binding level and initialize a block. Care must be
188 taken when translating expressions as any temporaries will be placed in
189 the innermost scope. */
191 void
192 gfc_start_block (stmtblock_t * block)
194 /* Start a new binding level. */
195 pushlevel ();
196 block->has_scope = 1;
198 /* The block is empty. */
199 block->head = NULL_TREE;
203 /* Initialize a block without creating a new scope. */
205 void
206 gfc_init_block (stmtblock_t * block)
208 block->head = NULL_TREE;
209 block->has_scope = 0;
213 /* Sometimes we create a scope but it turns out that we don't actually
214 need it. This function merges the scope of BLOCK with its parent.
215 Only variable decls will be merged, you still need to add the code. */
217 void
218 gfc_merge_block_scope (stmtblock_t * block)
220 tree decl;
221 tree next;
223 gcc_assert (block->has_scope);
224 block->has_scope = 0;
226 /* Remember the decls in this scope. */
227 decl = getdecls ();
228 poplevel (0, 0);
230 /* Add them to the parent scope. */
231 while (decl != NULL_TREE)
233 next = DECL_CHAIN (decl);
234 DECL_CHAIN (decl) = NULL_TREE;
236 pushdecl (decl);
237 decl = next;
242 /* Finish a scope containing a block of statements. */
244 tree
245 gfc_finish_block (stmtblock_t * stmtblock)
247 tree decl;
248 tree expr;
249 tree block;
251 expr = stmtblock->head;
252 if (!expr)
253 expr = build_empty_stmt (input_location);
255 stmtblock->head = NULL_TREE;
257 if (stmtblock->has_scope)
259 decl = getdecls ();
261 if (decl)
263 block = poplevel (1, 0);
264 expr = build3_v (BIND_EXPR, decl, expr, block);
266 else
267 poplevel (0, 0);
270 return expr;
274 /* Build an ADDR_EXPR and cast the result to TYPE. If TYPE is NULL, the
275 natural type is used. */
277 tree
278 gfc_build_addr_expr (tree type, tree t)
280 tree base_type = TREE_TYPE (t);
281 tree natural_type;
283 if (type && POINTER_TYPE_P (type)
284 && TREE_CODE (base_type) == ARRAY_TYPE
285 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
286 == TYPE_MAIN_VARIANT (TREE_TYPE (base_type)))
288 tree min_val = size_zero_node;
289 tree type_domain = TYPE_DOMAIN (base_type);
290 if (type_domain && TYPE_MIN_VALUE (type_domain))
291 min_val = TYPE_MIN_VALUE (type_domain);
292 t = fold (build4_loc (input_location, ARRAY_REF, TREE_TYPE (type),
293 t, min_val, NULL_TREE, NULL_TREE));
294 natural_type = type;
296 else
297 natural_type = build_pointer_type (base_type);
299 if (TREE_CODE (t) == INDIRECT_REF)
301 if (!type)
302 type = natural_type;
303 t = TREE_OPERAND (t, 0);
304 natural_type = TREE_TYPE (t);
306 else
308 tree base = get_base_address (t);
309 if (base && DECL_P (base))
310 TREE_ADDRESSABLE (base) = 1;
311 t = fold_build1_loc (input_location, ADDR_EXPR, natural_type, t);
314 if (type && natural_type != type)
315 t = convert (type, t);
317 return t;
321 /* Build an ARRAY_REF with its natural type. */
323 tree
324 gfc_build_array_ref (tree base, tree offset, tree decl)
326 tree type = TREE_TYPE (base);
327 tree tmp;
328 tree span;
330 if (GFC_ARRAY_TYPE_P (type) && GFC_TYPE_ARRAY_RANK (type) == 0)
332 gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
334 return fold_convert (TYPE_MAIN_VARIANT (type), base);
337 /* Scalar coarray, there is nothing to do. */
338 if (TREE_CODE (type) != ARRAY_TYPE)
340 gcc_assert (decl == NULL_TREE);
341 gcc_assert (integer_zerop (offset));
342 return base;
345 type = TREE_TYPE (type);
347 if (DECL_P (base))
348 TREE_ADDRESSABLE (base) = 1;
350 /* Strip NON_LVALUE_EXPR nodes. */
351 STRIP_TYPE_NOPS (offset);
353 /* If the array reference is to a pointer, whose target contains a
354 subreference, use the span that is stored with the backend decl
355 and reference the element with pointer arithmetic. */
356 if (decl && (TREE_CODE (decl) == FIELD_DECL
357 || TREE_CODE (decl) == VAR_DECL
358 || TREE_CODE (decl) == PARM_DECL)
359 && ((GFC_DECL_SUBREF_ARRAY_P (decl)
360 && !integer_zerop (GFC_DECL_SPAN(decl)))
361 || GFC_DECL_CLASS (decl)))
363 if (GFC_DECL_CLASS (decl))
365 /* Allow for dummy arguments and other good things. */
366 if (POINTER_TYPE_P (TREE_TYPE (decl)))
367 decl = build_fold_indirect_ref_loc (input_location, decl);
369 /* Check if '_data' is an array descriptor. If it is not,
370 the array must be one of the components of the class object,
371 so return a normal array reference. */
372 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_class_data_get (decl))))
373 return build4_loc (input_location, ARRAY_REF, type, base,
374 offset, NULL_TREE, NULL_TREE);
376 span = gfc_class_vtab_size_get (decl);
378 else if (GFC_DECL_SUBREF_ARRAY_P (decl))
379 span = GFC_DECL_SPAN(decl);
380 else
381 gcc_unreachable ();
383 offset = fold_build2_loc (input_location, MULT_EXPR,
384 gfc_array_index_type,
385 offset, span);
386 tmp = gfc_build_addr_expr (pvoid_type_node, base);
387 tmp = fold_build_pointer_plus_loc (input_location, tmp, offset);
388 tmp = fold_convert (build_pointer_type (type), tmp);
389 if (!TYPE_STRING_FLAG (type))
390 tmp = build_fold_indirect_ref_loc (input_location, tmp);
391 return tmp;
393 else
394 /* Otherwise use a straightforward array reference. */
395 return build4_loc (input_location, ARRAY_REF, type, base, offset,
396 NULL_TREE, NULL_TREE);
400 /* Generate a call to print a runtime error possibly including multiple
401 arguments and a locus. */
403 static tree
404 trans_runtime_error_vararg (bool error, locus* where, const char* msgid,
405 va_list ap)
407 stmtblock_t block;
408 tree tmp;
409 tree arg, arg2;
410 tree *argarray;
411 tree fntype;
412 char *message;
413 const char *p;
414 int line, nargs, i;
415 location_t loc;
417 /* Compute the number of extra arguments from the format string. */
418 for (p = msgid, nargs = 0; *p; p++)
419 if (*p == '%')
421 p++;
422 if (*p != '%')
423 nargs++;
426 /* The code to generate the error. */
427 gfc_start_block (&block);
429 if (where)
431 line = LOCATION_LINE (where->lb->location);
432 message = xasprintf ("At line %d of file %s", line,
433 where->lb->file->filename);
435 else
436 message = xasprintf ("In file '%s', around line %d",
437 gfc_source_file, LOCATION_LINE (input_location) + 1);
439 arg = gfc_build_addr_expr (pchar_type_node,
440 gfc_build_localized_cstring_const (message));
441 free (message);
443 message = xasprintf ("%s", _(msgid));
444 arg2 = gfc_build_addr_expr (pchar_type_node,
445 gfc_build_localized_cstring_const (message));
446 free (message);
448 /* Build the argument array. */
449 argarray = XALLOCAVEC (tree, nargs + 2);
450 argarray[0] = arg;
451 argarray[1] = arg2;
452 for (i = 0; i < nargs; i++)
453 argarray[2 + i] = va_arg (ap, tree);
455 /* Build the function call to runtime_(warning,error)_at; because of the
456 variable number of arguments, we can't use build_call_expr_loc dinput_location,
457 irectly. */
458 if (error)
459 fntype = TREE_TYPE (gfor_fndecl_runtime_error_at);
460 else
461 fntype = TREE_TYPE (gfor_fndecl_runtime_warning_at);
463 loc = where ? where->lb->location : input_location;
464 tmp = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
465 fold_build1_loc (loc, ADDR_EXPR,
466 build_pointer_type (fntype),
467 error
468 ? gfor_fndecl_runtime_error_at
469 : gfor_fndecl_runtime_warning_at),
470 nargs + 2, argarray);
471 gfc_add_expr_to_block (&block, tmp);
473 return gfc_finish_block (&block);
477 tree
478 gfc_trans_runtime_error (bool error, locus* where, const char* msgid, ...)
480 va_list ap;
481 tree result;
483 va_start (ap, msgid);
484 result = trans_runtime_error_vararg (error, where, msgid, ap);
485 va_end (ap);
486 return result;
490 /* Generate a runtime error if COND is true. */
492 void
493 gfc_trans_runtime_check (bool error, bool once, tree cond, stmtblock_t * pblock,
494 locus * where, const char * msgid, ...)
496 va_list ap;
497 stmtblock_t block;
498 tree body;
499 tree tmp;
500 tree tmpvar = NULL;
502 if (integer_zerop (cond))
503 return;
505 if (once)
507 tmpvar = gfc_create_var (boolean_type_node, "print_warning");
508 TREE_STATIC (tmpvar) = 1;
509 DECL_INITIAL (tmpvar) = boolean_true_node;
510 gfc_add_expr_to_block (pblock, tmpvar);
513 gfc_start_block (&block);
515 /* For error, runtime_error_at already implies PRED_NORETURN. */
516 if (!error && once)
517 gfc_add_expr_to_block (&block, build_predict_expr (PRED_FORTRAN_WARN_ONCE,
518 NOT_TAKEN));
520 /* The code to generate the error. */
521 va_start (ap, msgid);
522 gfc_add_expr_to_block (&block,
523 trans_runtime_error_vararg (error, where,
524 msgid, ap));
525 va_end (ap);
527 if (once)
528 gfc_add_modify (&block, tmpvar, boolean_false_node);
530 body = gfc_finish_block (&block);
532 if (integer_onep (cond))
534 gfc_add_expr_to_block (pblock, body);
536 else
538 if (once)
539 cond = fold_build2_loc (where->lb->location, TRUTH_AND_EXPR,
540 long_integer_type_node, tmpvar, cond);
541 else
542 cond = fold_convert (long_integer_type_node, cond);
544 tmp = fold_build3_loc (where->lb->location, COND_EXPR, void_type_node,
545 cond, body,
546 build_empty_stmt (where->lb->location));
547 gfc_add_expr_to_block (pblock, tmp);
552 /* Call malloc to allocate size bytes of memory, with special conditions:
553 + if size == 0, return a malloced area of size 1,
554 + if malloc returns NULL, issue a runtime error. */
555 tree
556 gfc_call_malloc (stmtblock_t * block, tree type, tree size)
558 tree tmp, msg, malloc_result, null_result, res, malloc_tree;
559 stmtblock_t block2;
561 size = gfc_evaluate_now (size, block);
563 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
564 size = fold_convert (size_type_node, size);
566 /* Create a variable to hold the result. */
567 res = gfc_create_var (prvoid_type_node, NULL);
569 /* Call malloc. */
570 gfc_start_block (&block2);
572 size = fold_build2_loc (input_location, MAX_EXPR, size_type_node, size,
573 build_int_cst (size_type_node, 1));
575 malloc_tree = builtin_decl_explicit (BUILT_IN_MALLOC);
576 gfc_add_modify (&block2, res,
577 fold_convert (prvoid_type_node,
578 build_call_expr_loc (input_location,
579 malloc_tree, 1, size)));
581 /* Optionally check whether malloc was successful. */
582 if (gfc_option.rtcheck & GFC_RTCHECK_MEM)
584 null_result = fold_build2_loc (input_location, EQ_EXPR,
585 boolean_type_node, res,
586 build_int_cst (pvoid_type_node, 0));
587 msg = gfc_build_addr_expr (pchar_type_node,
588 gfc_build_localized_cstring_const ("Memory allocation failed"));
589 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
590 null_result,
591 build_call_expr_loc (input_location,
592 gfor_fndecl_os_error, 1, msg),
593 build_empty_stmt (input_location));
594 gfc_add_expr_to_block (&block2, tmp);
597 malloc_result = gfc_finish_block (&block2);
599 gfc_add_expr_to_block (block, malloc_result);
601 if (type != NULL)
602 res = fold_convert (type, res);
603 return res;
607 /* Allocate memory, using an optional status argument.
609 This function follows the following pseudo-code:
611 void *
612 allocate (size_t size, integer_type stat)
614 void *newmem;
616 if (stat requested)
617 stat = 0;
619 newmem = malloc (MAX (size, 1));
620 if (newmem == NULL)
622 if (stat)
623 *stat = LIBERROR_ALLOCATION;
624 else
625 runtime_error ("Allocation would exceed memory limit");
627 return newmem;
628 } */
629 void
630 gfc_allocate_using_malloc (stmtblock_t * block, tree pointer,
631 tree size, tree status)
633 tree tmp, error_cond;
634 stmtblock_t on_error;
635 tree status_type = status ? TREE_TYPE (status) : NULL_TREE;
637 /* Evaluate size only once, and make sure it has the right type. */
638 size = gfc_evaluate_now (size, block);
639 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
640 size = fold_convert (size_type_node, size);
642 /* If successful and stat= is given, set status to 0. */
643 if (status != NULL_TREE)
644 gfc_add_expr_to_block (block,
645 fold_build2_loc (input_location, MODIFY_EXPR, status_type,
646 status, build_int_cst (status_type, 0)));
648 /* The allocation itself. */
649 gfc_add_modify (block, pointer,
650 fold_convert (TREE_TYPE (pointer),
651 build_call_expr_loc (input_location,
652 builtin_decl_explicit (BUILT_IN_MALLOC), 1,
653 fold_build2_loc (input_location,
654 MAX_EXPR, size_type_node, size,
655 build_int_cst (size_type_node, 1)))));
657 /* What to do in case of error. */
658 gfc_start_block (&on_error);
659 if (status != NULL_TREE)
661 gfc_add_expr_to_block (&on_error,
662 build_predict_expr (PRED_FORTRAN_FAIL_ALLOC,
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)
705 tree tmp, pstat;
707 gcc_assert (token != NULL_TREE);
709 /* Evaluate size only once, and make sure it has the right type. */
710 size = gfc_evaluate_now (size, block);
711 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
712 size = fold_convert (size_type_node, size);
714 /* The allocation itself. */
715 if (status == NULL_TREE)
716 pstat = null_pointer_node;
717 else
718 pstat = gfc_build_addr_expr (NULL_TREE, status);
720 if (errmsg == NULL_TREE)
722 gcc_assert(errlen == NULL_TREE);
723 errmsg = null_pointer_node;
724 errlen = build_int_cst (integer_type_node, 0);
727 tmp = build_call_expr_loc (input_location,
728 gfor_fndecl_caf_register, 6,
729 fold_build2_loc (input_location,
730 MAX_EXPR, size_type_node, size,
731 build_int_cst (size_type_node, 1)),
732 build_int_cst (integer_type_node,
733 GFC_CAF_COARRAY_ALLOC),
734 token, pstat, errmsg, errlen);
736 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
737 TREE_TYPE (pointer), pointer,
738 fold_convert ( TREE_TYPE (pointer), tmp));
739 gfc_add_expr_to_block (block, tmp);
743 /* Generate code for an ALLOCATE statement when the argument is an
744 allocatable variable. If the variable is currently allocated, it is an
745 error to allocate it again.
747 This function follows the following pseudo-code:
749 void *
750 allocate_allocatable (void *mem, size_t size, integer_type stat)
752 if (mem == NULL)
753 return allocate (size, stat);
754 else
756 if (stat)
757 stat = LIBERROR_ALLOCATION;
758 else
759 runtime_error ("Attempting to allocate already allocated variable");
763 expr must be set to the original expression being allocated for its locus
764 and variable name in case a runtime error has to be printed. */
765 void
766 gfc_allocate_allocatable (stmtblock_t * block, tree mem, tree size, tree token,
767 tree status, tree errmsg, tree errlen, tree label_finish,
768 gfc_expr* expr)
770 stmtblock_t alloc_block;
771 tree tmp, null_mem, alloc, error;
772 tree type = TREE_TYPE (mem);
774 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
775 size = fold_convert (size_type_node, size);
777 null_mem = gfc_unlikely (fold_build2_loc (input_location, NE_EXPR,
778 boolean_type_node, mem,
779 build_int_cst (type, 0)),
780 PRED_FORTRAN_FAIL_ALLOC);
782 /* If mem is NULL, we call gfc_allocate_using_malloc or
783 gfc_allocate_using_lib. */
784 gfc_start_block (&alloc_block);
786 if (flag_coarray == GFC_FCOARRAY_LIB
787 && gfc_expr_attr (expr).codimension)
789 tree cond;
791 gfc_allocate_using_lib (&alloc_block, mem, size, token, status,
792 errmsg, errlen);
793 if (status != NULL_TREE)
795 TREE_USED (label_finish) = 1;
796 tmp = build1_v (GOTO_EXPR, label_finish);
797 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
798 status, build_zero_cst (TREE_TYPE (status)));
799 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
800 gfc_unlikely (cond, PRED_FORTRAN_FAIL_ALLOC),
801 tmp, build_empty_stmt (input_location));
802 gfc_add_expr_to_block (&alloc_block, tmp);
805 else
806 gfc_allocate_using_malloc (&alloc_block, mem, size, status);
808 alloc = gfc_finish_block (&alloc_block);
810 /* If mem is not NULL, we issue a runtime error or set the
811 status variable. */
812 if (expr)
814 tree varname;
816 gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->symtree);
817 varname = gfc_build_cstring_const (expr->symtree->name);
818 varname = gfc_build_addr_expr (pchar_type_node, varname);
820 error = gfc_trans_runtime_error (true, &expr->where,
821 "Attempting to allocate already"
822 " allocated variable '%s'",
823 varname);
825 else
826 error = gfc_trans_runtime_error (true, NULL,
827 "Attempting to allocate already allocated"
828 " variable");
830 if (status != NULL_TREE)
832 tree status_type = TREE_TYPE (status);
834 error = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
835 status, build_int_cst (status_type, LIBERROR_ALLOCATION));
838 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, null_mem,
839 error, alloc);
840 gfc_add_expr_to_block (block, tmp);
844 /* Free a given variable, if it's not NULL. */
845 tree
846 gfc_call_free (tree var)
848 stmtblock_t block;
849 tree tmp, cond, call;
851 if (TREE_TYPE (var) != TREE_TYPE (pvoid_type_node))
852 var = fold_convert (pvoid_type_node, var);
854 gfc_start_block (&block);
855 var = gfc_evaluate_now (var, &block);
856 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, var,
857 build_int_cst (pvoid_type_node, 0));
858 call = build_call_expr_loc (input_location,
859 builtin_decl_explicit (BUILT_IN_FREE),
860 1, var);
861 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, call,
862 build_empty_stmt (input_location));
863 gfc_add_expr_to_block (&block, tmp);
865 return gfc_finish_block (&block);
869 /* Build a call to a FINAL procedure, which finalizes "var". */
871 static tree
872 gfc_build_final_call (gfc_typespec ts, gfc_expr *final_wrapper, gfc_expr *var,
873 bool fini_coarray, gfc_expr *class_size)
875 stmtblock_t block;
876 gfc_se se;
877 tree final_fndecl, array, size, tmp;
878 symbol_attribute attr;
880 gcc_assert (final_wrapper->expr_type == EXPR_VARIABLE);
881 gcc_assert (var);
883 gfc_start_block (&block);
884 gfc_init_se (&se, NULL);
885 gfc_conv_expr (&se, final_wrapper);
886 final_fndecl = se.expr;
887 if (POINTER_TYPE_P (TREE_TYPE (final_fndecl)))
888 final_fndecl = build_fold_indirect_ref_loc (input_location, final_fndecl);
890 if (ts.type == BT_DERIVED)
892 tree elem_size;
894 gcc_assert (!class_size);
895 elem_size = gfc_typenode_for_spec (&ts);
896 elem_size = TYPE_SIZE_UNIT (elem_size);
897 size = fold_convert (gfc_array_index_type, elem_size);
899 gfc_init_se (&se, NULL);
900 se.want_pointer = 1;
901 if (var->rank)
903 se.descriptor_only = 1;
904 gfc_conv_expr_descriptor (&se, var);
905 array = se.expr;
907 else
909 gfc_conv_expr (&se, var);
910 gcc_assert (se.pre.head == NULL_TREE && se.post.head == NULL_TREE);
911 array = se.expr;
913 /* No copy back needed, hence set attr's allocatable/pointer
914 to zero. */
915 gfc_clear_attr (&attr);
916 gfc_init_se (&se, NULL);
917 array = gfc_conv_scalar_to_descriptor (&se, array, attr);
918 gcc_assert (se.post.head == NULL_TREE);
921 else
923 gfc_expr *array_expr;
924 gcc_assert (class_size);
925 gfc_init_se (&se, NULL);
926 gfc_conv_expr (&se, class_size);
927 gfc_add_block_to_block (&block, &se.pre);
928 gcc_assert (se.post.head == NULL_TREE);
929 size = se.expr;
931 array_expr = gfc_copy_expr (var);
932 gfc_init_se (&se, NULL);
933 se.want_pointer = 1;
934 if (array_expr->rank)
936 gfc_add_class_array_ref (array_expr);
937 se.descriptor_only = 1;
938 gfc_conv_expr_descriptor (&se, array_expr);
939 array = se.expr;
941 else
943 gfc_add_data_component (array_expr);
944 gfc_conv_expr (&se, array_expr);
945 gfc_add_block_to_block (&block, &se.pre);
946 gcc_assert (se.post.head == NULL_TREE);
947 array = se.expr;
948 if (TREE_CODE (array) == ADDR_EXPR
949 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (array, 0))))
950 tmp = TREE_OPERAND (array, 0);
952 if (!gfc_is_coarray (array_expr))
954 /* No copy back needed, hence set attr's allocatable/pointer
955 to zero. */
956 gfc_clear_attr (&attr);
957 gfc_init_se (&se, NULL);
958 array = gfc_conv_scalar_to_descriptor (&se, array, attr);
960 gcc_assert (se.post.head == NULL_TREE);
962 gfc_free_expr (array_expr);
965 if (!POINTER_TYPE_P (TREE_TYPE (array)))
966 array = gfc_build_addr_expr (NULL, array);
968 gfc_add_block_to_block (&block, &se.pre);
969 tmp = build_call_expr_loc (input_location,
970 final_fndecl, 3, array,
971 size, fini_coarray ? boolean_true_node
972 : boolean_false_node);
973 gfc_add_block_to_block (&block, &se.post);
974 gfc_add_expr_to_block (&block, tmp);
975 return gfc_finish_block (&block);
979 bool
980 gfc_add_comp_finalizer_call (stmtblock_t *block, tree decl, gfc_component *comp,
981 bool fini_coarray)
983 gfc_se se;
984 stmtblock_t block2;
985 tree final_fndecl, size, array, tmp, cond;
986 symbol_attribute attr;
987 gfc_expr *final_expr = NULL;
989 if (comp->ts.type != BT_DERIVED && comp->ts.type != BT_CLASS)
990 return false;
992 gfc_init_block (&block2);
994 if (comp->ts.type == BT_DERIVED)
996 if (comp->attr.pointer)
997 return false;
999 gfc_is_finalizable (comp->ts.u.derived, &final_expr);
1000 if (!final_expr)
1001 return false;
1003 gfc_init_se (&se, NULL);
1004 gfc_conv_expr (&se, final_expr);
1005 final_fndecl = se.expr;
1006 size = gfc_typenode_for_spec (&comp->ts);
1007 size = TYPE_SIZE_UNIT (size);
1008 size = fold_convert (gfc_array_index_type, size);
1010 array = decl;
1012 else /* comp->ts.type == BT_CLASS. */
1014 if (CLASS_DATA (comp)->attr.class_pointer)
1015 return false;
1017 gfc_is_finalizable (CLASS_DATA (comp)->ts.u.derived, &final_expr);
1018 final_fndecl = gfc_class_vtab_final_get (decl);
1019 size = gfc_class_vtab_size_get (decl);
1020 array = gfc_class_data_get (decl);
1023 if (comp->attr.allocatable
1024 || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->attr.allocatable))
1026 tmp = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array))
1027 ? gfc_conv_descriptor_data_get (array) : array;
1028 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1029 tmp, fold_convert (TREE_TYPE (tmp),
1030 null_pointer_node));
1032 else
1033 cond = boolean_true_node;
1035 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array)))
1037 gfc_clear_attr (&attr);
1038 gfc_init_se (&se, NULL);
1039 array = gfc_conv_scalar_to_descriptor (&se, array, attr);
1040 gfc_add_block_to_block (&block2, &se.pre);
1041 gcc_assert (se.post.head == NULL_TREE);
1044 if (!POINTER_TYPE_P (TREE_TYPE (array)))
1045 array = gfc_build_addr_expr (NULL, array);
1047 if (!final_expr)
1049 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1050 final_fndecl,
1051 fold_convert (TREE_TYPE (final_fndecl),
1052 null_pointer_node));
1053 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1054 boolean_type_node, cond, tmp);
1057 if (POINTER_TYPE_P (TREE_TYPE (final_fndecl)))
1058 final_fndecl = build_fold_indirect_ref_loc (input_location, final_fndecl);
1060 tmp = build_call_expr_loc (input_location,
1061 final_fndecl, 3, array,
1062 size, fini_coarray ? boolean_true_node
1063 : boolean_false_node);
1064 gfc_add_expr_to_block (&block2, tmp);
1065 tmp = gfc_finish_block (&block2);
1067 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
1068 build_empty_stmt (input_location));
1069 gfc_add_expr_to_block (block, tmp);
1071 return true;
1075 /* Add a call to the finalizer, using the passed *expr. Returns
1076 true when a finalizer call has been inserted. */
1078 bool
1079 gfc_add_finalizer_call (stmtblock_t *block, gfc_expr *expr2)
1081 tree tmp;
1082 gfc_ref *ref;
1083 gfc_expr *expr;
1084 gfc_expr *final_expr = NULL;
1085 gfc_expr *elem_size = NULL;
1086 bool has_finalizer = false;
1088 if (!expr2 || (expr2->ts.type != BT_DERIVED && expr2->ts.type != BT_CLASS))
1089 return false;
1091 if (expr2->ts.type == BT_DERIVED)
1093 gfc_is_finalizable (expr2->ts.u.derived, &final_expr);
1094 if (!final_expr)
1095 return false;
1098 /* If we have a class array, we need go back to the class
1099 container. */
1100 expr = gfc_copy_expr (expr2);
1102 if (expr->ref && expr->ref->next && !expr->ref->next->next
1103 && expr->ref->next->type == REF_ARRAY
1104 && expr->ref->type == REF_COMPONENT
1105 && strcmp (expr->ref->u.c.component->name, "_data") == 0)
1107 gfc_free_ref_list (expr->ref);
1108 expr->ref = NULL;
1110 else
1111 for (ref = expr->ref; ref; ref = ref->next)
1112 if (ref->next && ref->next->next && !ref->next->next->next
1113 && ref->next->next->type == REF_ARRAY
1114 && ref->next->type == REF_COMPONENT
1115 && strcmp (ref->next->u.c.component->name, "_data") == 0)
1117 gfc_free_ref_list (ref->next);
1118 ref->next = NULL;
1121 if (expr->ts.type == BT_CLASS)
1123 has_finalizer = gfc_is_finalizable (expr->ts.u.derived, NULL);
1125 if (!expr2->rank && !expr2->ref && CLASS_DATA (expr2->symtree->n.sym)->as)
1126 expr->rank = CLASS_DATA (expr2->symtree->n.sym)->as->rank;
1128 final_expr = gfc_copy_expr (expr);
1129 gfc_add_vptr_component (final_expr);
1130 gfc_add_component_ref (final_expr, "_final");
1132 elem_size = gfc_copy_expr (expr);
1133 gfc_add_vptr_component (elem_size);
1134 gfc_add_component_ref (elem_size, "_size");
1137 gcc_assert (final_expr->expr_type == EXPR_VARIABLE);
1139 tmp = gfc_build_final_call (expr->ts, final_expr, expr,
1140 false, elem_size);
1142 if (expr->ts.type == BT_CLASS && !has_finalizer)
1144 tree cond;
1145 gfc_se se;
1147 gfc_init_se (&se, NULL);
1148 se.want_pointer = 1;
1149 gfc_conv_expr (&se, final_expr);
1150 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1151 se.expr, build_int_cst (TREE_TYPE (se.expr), 0));
1153 /* For CLASS(*) not only sym->_vtab->_final can be NULL
1154 but already sym->_vtab itself. */
1155 if (UNLIMITED_POLY (expr))
1157 tree cond2;
1158 gfc_expr *vptr_expr;
1160 vptr_expr = gfc_copy_expr (expr);
1161 gfc_add_vptr_component (vptr_expr);
1163 gfc_init_se (&se, NULL);
1164 se.want_pointer = 1;
1165 gfc_conv_expr (&se, vptr_expr);
1166 gfc_free_expr (vptr_expr);
1168 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1169 se.expr,
1170 build_int_cst (TREE_TYPE (se.expr), 0));
1171 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1172 boolean_type_node, cond2, cond);
1175 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1176 cond, tmp, build_empty_stmt (input_location));
1179 gfc_add_expr_to_block (block, tmp);
1181 return true;
1185 /* User-deallocate; we emit the code directly from the front-end, and the
1186 logic is the same as the previous library function:
1188 void
1189 deallocate (void *pointer, GFC_INTEGER_4 * stat)
1191 if (!pointer)
1193 if (stat)
1194 *stat = 1;
1195 else
1196 runtime_error ("Attempt to DEALLOCATE unallocated memory.");
1198 else
1200 free (pointer);
1201 if (stat)
1202 *stat = 0;
1206 In this front-end version, status doesn't have to be GFC_INTEGER_4.
1207 Moreover, if CAN_FAIL is true, then we will not emit a runtime error,
1208 even when no status variable is passed to us (this is used for
1209 unconditional deallocation generated by the front-end at end of
1210 each procedure).
1212 If a runtime-message is possible, `expr' must point to the original
1213 expression being deallocated for its locus and variable name.
1215 For coarrays, "pointer" must be the array descriptor and not its
1216 "data" component. */
1217 tree
1218 gfc_deallocate_with_status (tree pointer, tree status, tree errmsg,
1219 tree errlen, tree label_finish,
1220 bool can_fail, gfc_expr* expr, bool coarray)
1222 stmtblock_t null, non_null;
1223 tree cond, tmp, error;
1224 tree status_type = NULL_TREE;
1225 tree caf_decl = NULL_TREE;
1227 if (coarray)
1229 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (pointer)));
1230 caf_decl = pointer;
1231 pointer = gfc_conv_descriptor_data_get (caf_decl);
1232 STRIP_NOPS (pointer);
1235 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, pointer,
1236 build_int_cst (TREE_TYPE (pointer), 0));
1238 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
1239 we emit a runtime error. */
1240 gfc_start_block (&null);
1241 if (!can_fail)
1243 tree varname;
1245 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
1247 varname = gfc_build_cstring_const (expr->symtree->name);
1248 varname = gfc_build_addr_expr (pchar_type_node, varname);
1250 error = gfc_trans_runtime_error (true, &expr->where,
1251 "Attempt to DEALLOCATE unallocated '%s'",
1252 varname);
1254 else
1255 error = build_empty_stmt (input_location);
1257 if (status != NULL_TREE && !integer_zerop (status))
1259 tree cond2;
1261 status_type = TREE_TYPE (TREE_TYPE (status));
1262 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1263 status, build_int_cst (TREE_TYPE (status), 0));
1264 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
1265 fold_build1_loc (input_location, INDIRECT_REF,
1266 status_type, status),
1267 build_int_cst (status_type, 1));
1268 error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1269 cond2, tmp, error);
1272 gfc_add_expr_to_block (&null, error);
1274 /* When POINTER is not NULL, we free it. */
1275 gfc_start_block (&non_null);
1276 gfc_add_finalizer_call (&non_null, expr);
1277 if (!coarray || flag_coarray != GFC_FCOARRAY_LIB)
1279 tmp = build_call_expr_loc (input_location,
1280 builtin_decl_explicit (BUILT_IN_FREE), 1,
1281 fold_convert (pvoid_type_node, pointer));
1282 gfc_add_expr_to_block (&non_null, tmp);
1284 if (status != NULL_TREE && !integer_zerop (status))
1286 /* We set STATUS to zero if it is present. */
1287 tree status_type = TREE_TYPE (TREE_TYPE (status));
1288 tree cond2;
1290 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1291 status,
1292 build_int_cst (TREE_TYPE (status), 0));
1293 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
1294 fold_build1_loc (input_location, INDIRECT_REF,
1295 status_type, status),
1296 build_int_cst (status_type, 0));
1297 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1298 gfc_unlikely (cond2, PRED_FORTRAN_FAIL_ALLOC),
1299 tmp, build_empty_stmt (input_location));
1300 gfc_add_expr_to_block (&non_null, tmp);
1303 else
1305 tree caf_type, token, cond2;
1306 tree pstat = null_pointer_node;
1308 if (errmsg == NULL_TREE)
1310 gcc_assert (errlen == NULL_TREE);
1311 errmsg = null_pointer_node;
1312 errlen = build_zero_cst (integer_type_node);
1314 else
1316 gcc_assert (errlen != NULL_TREE);
1317 if (!POINTER_TYPE_P (TREE_TYPE (errmsg)))
1318 errmsg = gfc_build_addr_expr (NULL_TREE, errmsg);
1321 caf_type = TREE_TYPE (caf_decl);
1323 if (status != NULL_TREE && !integer_zerop (status))
1325 gcc_assert (status_type == integer_type_node);
1326 pstat = status;
1329 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
1330 && GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE)
1331 token = gfc_conv_descriptor_token (caf_decl);
1332 else if (DECL_LANG_SPECIFIC (caf_decl)
1333 && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
1334 token = GFC_DECL_TOKEN (caf_decl);
1335 else
1337 gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
1338 && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
1339 token = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
1342 token = gfc_build_addr_expr (NULL_TREE, token);
1343 tmp = build_call_expr_loc (input_location,
1344 gfor_fndecl_caf_deregister, 4,
1345 token, pstat, errmsg, errlen);
1346 gfc_add_expr_to_block (&non_null, tmp);
1348 if (status != NULL_TREE)
1350 tree stat = build_fold_indirect_ref_loc (input_location, status);
1352 TREE_USED (label_finish) = 1;
1353 tmp = build1_v (GOTO_EXPR, label_finish);
1354 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1355 stat, build_zero_cst (TREE_TYPE (stat)));
1356 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1357 gfc_unlikely (cond2, PRED_FORTRAN_FAIL_ALLOC),
1358 tmp, build_empty_stmt (input_location));
1359 gfc_add_expr_to_block (&non_null, tmp);
1363 return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
1364 gfc_finish_block (&null),
1365 gfc_finish_block (&non_null));
1369 /* Generate code for deallocation of allocatable scalars (variables or
1370 components). Before the object itself is freed, any allocatable
1371 subcomponents are being deallocated. */
1373 tree
1374 gfc_deallocate_scalar_with_status (tree pointer, tree status, bool can_fail,
1375 gfc_expr* expr, gfc_typespec ts)
1377 stmtblock_t null, non_null;
1378 tree cond, tmp, error;
1379 bool finalizable;
1381 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, pointer,
1382 build_int_cst (TREE_TYPE (pointer), 0));
1384 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
1385 we emit a runtime error. */
1386 gfc_start_block (&null);
1387 if (!can_fail)
1389 tree varname;
1391 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
1393 varname = gfc_build_cstring_const (expr->symtree->name);
1394 varname = gfc_build_addr_expr (pchar_type_node, varname);
1396 error = gfc_trans_runtime_error (true, &expr->where,
1397 "Attempt to DEALLOCATE unallocated '%s'",
1398 varname);
1400 else
1401 error = build_empty_stmt (input_location);
1403 if (status != NULL_TREE && !integer_zerop (status))
1405 tree status_type = TREE_TYPE (TREE_TYPE (status));
1406 tree cond2;
1408 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1409 status, build_int_cst (TREE_TYPE (status), 0));
1410 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
1411 fold_build1_loc (input_location, INDIRECT_REF,
1412 status_type, status),
1413 build_int_cst (status_type, 1));
1414 error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1415 cond2, tmp, error);
1418 gfc_add_expr_to_block (&null, error);
1420 /* When POINTER is not NULL, we free it. */
1421 gfc_start_block (&non_null);
1423 /* Free allocatable components. */
1424 finalizable = gfc_add_finalizer_call (&non_null, expr);
1425 if (!finalizable && ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp)
1427 tmp = build_fold_indirect_ref_loc (input_location, pointer);
1428 tmp = gfc_deallocate_alloc_comp (ts.u.derived, tmp, 0);
1429 gfc_add_expr_to_block (&non_null, tmp);
1432 tmp = build_call_expr_loc (input_location,
1433 builtin_decl_explicit (BUILT_IN_FREE), 1,
1434 fold_convert (pvoid_type_node, pointer));
1435 gfc_add_expr_to_block (&non_null, tmp);
1437 if (status != NULL_TREE && !integer_zerop (status))
1439 /* We set STATUS to zero if it is present. */
1440 tree status_type = TREE_TYPE (TREE_TYPE (status));
1441 tree cond2;
1443 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1444 status, build_int_cst (TREE_TYPE (status), 0));
1445 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
1446 fold_build1_loc (input_location, INDIRECT_REF,
1447 status_type, status),
1448 build_int_cst (status_type, 0));
1449 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
1450 tmp, build_empty_stmt (input_location));
1451 gfc_add_expr_to_block (&non_null, tmp);
1454 return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
1455 gfc_finish_block (&null),
1456 gfc_finish_block (&non_null));
1460 /* Reallocate MEM so it has SIZE bytes of data. This behaves like the
1461 following pseudo-code:
1463 void *
1464 internal_realloc (void *mem, size_t size)
1466 res = realloc (mem, size);
1467 if (!res && size != 0)
1468 _gfortran_os_error ("Allocation would exceed memory limit");
1470 return res;
1471 } */
1472 tree
1473 gfc_call_realloc (stmtblock_t * block, tree mem, tree size)
1475 tree msg, res, nonzero, null_result, tmp;
1476 tree type = TREE_TYPE (mem);
1478 size = gfc_evaluate_now (size, block);
1480 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
1481 size = fold_convert (size_type_node, size);
1483 /* Create a variable to hold the result. */
1484 res = gfc_create_var (type, NULL);
1486 /* Call realloc and check the result. */
1487 tmp = build_call_expr_loc (input_location,
1488 builtin_decl_explicit (BUILT_IN_REALLOC), 2,
1489 fold_convert (pvoid_type_node, mem), size);
1490 gfc_add_modify (block, res, fold_convert (type, tmp));
1491 null_result = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
1492 res, build_int_cst (pvoid_type_node, 0));
1493 nonzero = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, size,
1494 build_int_cst (size_type_node, 0));
1495 null_result = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
1496 null_result, nonzero);
1497 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
1498 ("Allocation would exceed memory limit"));
1499 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1500 null_result,
1501 build_call_expr_loc (input_location,
1502 gfor_fndecl_os_error, 1, msg),
1503 build_empty_stmt (input_location));
1504 gfc_add_expr_to_block (block, tmp);
1506 return res;
1510 /* Add an expression to another one, either at the front or the back. */
1512 static void
1513 add_expr_to_chain (tree* chain, tree expr, bool front)
1515 if (expr == NULL_TREE || IS_EMPTY_STMT (expr))
1516 return;
1518 if (*chain)
1520 if (TREE_CODE (*chain) != STATEMENT_LIST)
1522 tree tmp;
1524 tmp = *chain;
1525 *chain = NULL_TREE;
1526 append_to_statement_list (tmp, chain);
1529 if (front)
1531 tree_stmt_iterator i;
1533 i = tsi_start (*chain);
1534 tsi_link_before (&i, expr, TSI_CONTINUE_LINKING);
1536 else
1537 append_to_statement_list (expr, chain);
1539 else
1540 *chain = expr;
1544 /* Add a statement at the end of a block. */
1546 void
1547 gfc_add_expr_to_block (stmtblock_t * block, tree expr)
1549 gcc_assert (block);
1550 add_expr_to_chain (&block->head, expr, false);
1554 /* Add a statement at the beginning of a block. */
1556 void
1557 gfc_prepend_expr_to_block (stmtblock_t * block, tree expr)
1559 gcc_assert (block);
1560 add_expr_to_chain (&block->head, expr, true);
1564 /* Add a block the end of a block. */
1566 void
1567 gfc_add_block_to_block (stmtblock_t * block, stmtblock_t * append)
1569 gcc_assert (append);
1570 gcc_assert (!append->has_scope);
1572 gfc_add_expr_to_block (block, append->head);
1573 append->head = NULL_TREE;
1577 /* Save the current locus. The structure may not be complete, and should
1578 only be used with gfc_restore_backend_locus. */
1580 void
1581 gfc_save_backend_locus (locus * loc)
1583 loc->lb = XCNEW (gfc_linebuf);
1584 loc->lb->location = input_location;
1585 loc->lb->file = gfc_current_backend_file;
1589 /* Set the current locus. */
1591 void
1592 gfc_set_backend_locus (locus * loc)
1594 gfc_current_backend_file = loc->lb->file;
1595 input_location = loc->lb->location;
1599 /* Restore the saved locus. Only used in conjunction with
1600 gfc_save_backend_locus, to free the memory when we are done. */
1602 void
1603 gfc_restore_backend_locus (locus * loc)
1605 gfc_set_backend_locus (loc);
1606 free (loc->lb);
1610 /* Translate an executable statement. The tree cond is used by gfc_trans_do.
1611 This static function is wrapped by gfc_trans_code_cond and
1612 gfc_trans_code. */
1614 static tree
1615 trans_code (gfc_code * code, tree cond)
1617 stmtblock_t block;
1618 tree res;
1620 if (!code)
1621 return build_empty_stmt (input_location);
1623 gfc_start_block (&block);
1625 /* Translate statements one by one into GENERIC trees until we reach
1626 the end of this gfc_code branch. */
1627 for (; code; code = code->next)
1629 if (code->here != 0)
1631 res = gfc_trans_label_here (code);
1632 gfc_add_expr_to_block (&block, res);
1635 gfc_set_backend_locus (&code->loc);
1637 switch (code->op)
1639 case EXEC_NOP:
1640 case EXEC_END_BLOCK:
1641 case EXEC_END_NESTED_BLOCK:
1642 case EXEC_END_PROCEDURE:
1643 res = NULL_TREE;
1644 break;
1646 case EXEC_ASSIGN:
1647 if (code->expr1->ts.type == BT_CLASS)
1648 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1649 else
1650 res = gfc_trans_assign (code);
1651 break;
1653 case EXEC_LABEL_ASSIGN:
1654 res = gfc_trans_label_assign (code);
1655 break;
1657 case EXEC_POINTER_ASSIGN:
1658 if (code->expr1->ts.type == BT_CLASS)
1659 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1660 else if (UNLIMITED_POLY (code->expr2)
1661 && code->expr1->ts.type == BT_DERIVED
1662 && (code->expr1->ts.u.derived->attr.sequence
1663 || code->expr1->ts.u.derived->attr.is_bind_c))
1664 /* F2003: C717 */
1665 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1666 else
1667 res = gfc_trans_pointer_assign (code);
1668 break;
1670 case EXEC_INIT_ASSIGN:
1671 if (code->expr1->ts.type == BT_CLASS)
1672 res = gfc_trans_class_init_assign (code);
1673 else
1674 res = gfc_trans_init_assign (code);
1675 break;
1677 case EXEC_CONTINUE:
1678 res = NULL_TREE;
1679 break;
1681 case EXEC_CRITICAL:
1682 res = gfc_trans_critical (code);
1683 break;
1685 case EXEC_CYCLE:
1686 res = gfc_trans_cycle (code);
1687 break;
1689 case EXEC_EXIT:
1690 res = gfc_trans_exit (code);
1691 break;
1693 case EXEC_GOTO:
1694 res = gfc_trans_goto (code);
1695 break;
1697 case EXEC_ENTRY:
1698 res = gfc_trans_entry (code);
1699 break;
1701 case EXEC_PAUSE:
1702 res = gfc_trans_pause (code);
1703 break;
1705 case EXEC_STOP:
1706 case EXEC_ERROR_STOP:
1707 res = gfc_trans_stop (code, code->op == EXEC_ERROR_STOP);
1708 break;
1710 case EXEC_CALL:
1711 /* For MVBITS we've got the special exception that we need a
1712 dependency check, too. */
1714 bool is_mvbits = false;
1716 if (code->resolved_isym)
1718 res = gfc_conv_intrinsic_subroutine (code);
1719 if (res != NULL_TREE)
1720 break;
1723 if (code->resolved_isym
1724 && code->resolved_isym->id == GFC_ISYM_MVBITS)
1725 is_mvbits = true;
1727 res = gfc_trans_call (code, is_mvbits, NULL_TREE,
1728 NULL_TREE, false);
1730 break;
1732 case EXEC_CALL_PPC:
1733 res = gfc_trans_call (code, false, NULL_TREE,
1734 NULL_TREE, false);
1735 break;
1737 case EXEC_ASSIGN_CALL:
1738 res = gfc_trans_call (code, true, NULL_TREE,
1739 NULL_TREE, false);
1740 break;
1742 case EXEC_RETURN:
1743 res = gfc_trans_return (code);
1744 break;
1746 case EXEC_IF:
1747 res = gfc_trans_if (code);
1748 break;
1750 case EXEC_ARITHMETIC_IF:
1751 res = gfc_trans_arithmetic_if (code);
1752 break;
1754 case EXEC_BLOCK:
1755 res = gfc_trans_block_construct (code);
1756 break;
1758 case EXEC_DO:
1759 res = gfc_trans_do (code, cond);
1760 break;
1762 case EXEC_DO_CONCURRENT:
1763 res = gfc_trans_do_concurrent (code);
1764 break;
1766 case EXEC_DO_WHILE:
1767 res = gfc_trans_do_while (code);
1768 break;
1770 case EXEC_SELECT:
1771 res = gfc_trans_select (code);
1772 break;
1774 case EXEC_SELECT_TYPE:
1775 /* Do nothing. SELECT TYPE statements should be transformed into
1776 an ordinary SELECT CASE at resolution stage.
1777 TODO: Add an error message here once this is done. */
1778 res = NULL_TREE;
1779 break;
1781 case EXEC_FLUSH:
1782 res = gfc_trans_flush (code);
1783 break;
1785 case EXEC_SYNC_ALL:
1786 case EXEC_SYNC_IMAGES:
1787 case EXEC_SYNC_MEMORY:
1788 res = gfc_trans_sync (code, code->op);
1789 break;
1791 case EXEC_LOCK:
1792 case EXEC_UNLOCK:
1793 res = gfc_trans_lock_unlock (code, code->op);
1794 break;
1796 case EXEC_FORALL:
1797 res = gfc_trans_forall (code);
1798 break;
1800 case EXEC_WHERE:
1801 res = gfc_trans_where (code);
1802 break;
1804 case EXEC_ALLOCATE:
1805 res = gfc_trans_allocate (code);
1806 break;
1808 case EXEC_DEALLOCATE:
1809 res = gfc_trans_deallocate (code);
1810 break;
1812 case EXEC_OPEN:
1813 res = gfc_trans_open (code);
1814 break;
1816 case EXEC_CLOSE:
1817 res = gfc_trans_close (code);
1818 break;
1820 case EXEC_READ:
1821 res = gfc_trans_read (code);
1822 break;
1824 case EXEC_WRITE:
1825 res = gfc_trans_write (code);
1826 break;
1828 case EXEC_IOLENGTH:
1829 res = gfc_trans_iolength (code);
1830 break;
1832 case EXEC_BACKSPACE:
1833 res = gfc_trans_backspace (code);
1834 break;
1836 case EXEC_ENDFILE:
1837 res = gfc_trans_endfile (code);
1838 break;
1840 case EXEC_INQUIRE:
1841 res = gfc_trans_inquire (code);
1842 break;
1844 case EXEC_WAIT:
1845 res = gfc_trans_wait (code);
1846 break;
1848 case EXEC_REWIND:
1849 res = gfc_trans_rewind (code);
1850 break;
1852 case EXEC_TRANSFER:
1853 res = gfc_trans_transfer (code);
1854 break;
1856 case EXEC_DT_END:
1857 res = gfc_trans_dt_end (code);
1858 break;
1860 case EXEC_OMP_ATOMIC:
1861 case EXEC_OMP_BARRIER:
1862 case EXEC_OMP_CANCEL:
1863 case EXEC_OMP_CANCELLATION_POINT:
1864 case EXEC_OMP_CRITICAL:
1865 case EXEC_OMP_DISTRIBUTE:
1866 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
1867 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
1868 case EXEC_OMP_DISTRIBUTE_SIMD:
1869 case EXEC_OMP_DO:
1870 case EXEC_OMP_DO_SIMD:
1871 case EXEC_OMP_FLUSH:
1872 case EXEC_OMP_MASTER:
1873 case EXEC_OMP_ORDERED:
1874 case EXEC_OMP_PARALLEL:
1875 case EXEC_OMP_PARALLEL_DO:
1876 case EXEC_OMP_PARALLEL_DO_SIMD:
1877 case EXEC_OMP_PARALLEL_SECTIONS:
1878 case EXEC_OMP_PARALLEL_WORKSHARE:
1879 case EXEC_OMP_SECTIONS:
1880 case EXEC_OMP_SIMD:
1881 case EXEC_OMP_SINGLE:
1882 case EXEC_OMP_TARGET:
1883 case EXEC_OMP_TARGET_DATA:
1884 case EXEC_OMP_TARGET_TEAMS:
1885 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
1886 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
1887 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
1888 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
1889 case EXEC_OMP_TARGET_UPDATE:
1890 case EXEC_OMP_TASK:
1891 case EXEC_OMP_TASKGROUP:
1892 case EXEC_OMP_TASKWAIT:
1893 case EXEC_OMP_TASKYIELD:
1894 case EXEC_OMP_TEAMS:
1895 case EXEC_OMP_TEAMS_DISTRIBUTE:
1896 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
1897 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
1898 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
1899 case EXEC_OMP_WORKSHARE:
1900 res = gfc_trans_omp_directive (code);
1901 break;
1903 case EXEC_OACC_CACHE:
1904 case EXEC_OACC_WAIT:
1905 case EXEC_OACC_UPDATE:
1906 case EXEC_OACC_LOOP:
1907 case EXEC_OACC_HOST_DATA:
1908 case EXEC_OACC_DATA:
1909 case EXEC_OACC_KERNELS:
1910 case EXEC_OACC_KERNELS_LOOP:
1911 case EXEC_OACC_PARALLEL:
1912 case EXEC_OACC_PARALLEL_LOOP:
1913 case EXEC_OACC_ENTER_DATA:
1914 case EXEC_OACC_EXIT_DATA:
1915 res = gfc_trans_oacc_directive (code);
1916 break;
1918 default:
1919 gfc_internal_error ("gfc_trans_code(): Bad statement code");
1922 gfc_set_backend_locus (&code->loc);
1924 if (res != NULL_TREE && ! IS_EMPTY_STMT (res))
1926 if (TREE_CODE (res) != STATEMENT_LIST)
1927 SET_EXPR_LOCATION (res, input_location);
1929 /* Add the new statement to the block. */
1930 gfc_add_expr_to_block (&block, res);
1934 /* Return the finished block. */
1935 return gfc_finish_block (&block);
1939 /* Translate an executable statement with condition, cond. The condition is
1940 used by gfc_trans_do to test for IO result conditions inside implied
1941 DO loops of READ and WRITE statements. See build_dt in trans-io.c. */
1943 tree
1944 gfc_trans_code_cond (gfc_code * code, tree cond)
1946 return trans_code (code, cond);
1949 /* Translate an executable statement without condition. */
1951 tree
1952 gfc_trans_code (gfc_code * code)
1954 return trans_code (code, NULL_TREE);
1958 /* This function is called after a complete program unit has been parsed
1959 and resolved. */
1961 void
1962 gfc_generate_code (gfc_namespace * ns)
1964 ompws_flags = 0;
1965 if (ns->is_block_data)
1967 gfc_generate_block_data (ns);
1968 return;
1971 gfc_generate_function_code (ns);
1975 /* This function is called after a complete module has been parsed
1976 and resolved. */
1978 void
1979 gfc_generate_module_code (gfc_namespace * ns)
1981 gfc_namespace *n;
1982 struct module_htab_entry *entry;
1984 gcc_assert (ns->proc_name->backend_decl == NULL);
1985 ns->proc_name->backend_decl
1986 = build_decl (ns->proc_name->declared_at.lb->location,
1987 NAMESPACE_DECL, get_identifier (ns->proc_name->name),
1988 void_type_node);
1989 entry = gfc_find_module (ns->proc_name->name);
1990 if (entry->namespace_decl)
1991 /* Buggy sourcecode, using a module before defining it? */
1992 entry->decls->empty ();
1993 entry->namespace_decl = ns->proc_name->backend_decl;
1995 gfc_generate_module_vars (ns);
1997 /* We need to generate all module function prototypes first, to allow
1998 sibling calls. */
1999 for (n = ns->contained; n; n = n->sibling)
2001 gfc_entry_list *el;
2003 if (!n->proc_name)
2004 continue;
2006 gfc_create_function_decl (n, false);
2007 DECL_CONTEXT (n->proc_name->backend_decl) = ns->proc_name->backend_decl;
2008 gfc_module_add_decl (entry, n->proc_name->backend_decl);
2009 for (el = ns->entries; el; el = el->next)
2011 DECL_CONTEXT (el->sym->backend_decl) = ns->proc_name->backend_decl;
2012 gfc_module_add_decl (entry, el->sym->backend_decl);
2016 for (n = ns->contained; n; n = n->sibling)
2018 if (!n->proc_name)
2019 continue;
2021 gfc_generate_function_code (n);
2026 /* Initialize an init/cleanup block with existing code. */
2028 void
2029 gfc_start_wrapped_block (gfc_wrapped_block* block, tree code)
2031 gcc_assert (block);
2033 block->init = NULL_TREE;
2034 block->code = code;
2035 block->cleanup = NULL_TREE;
2039 /* Add a new pair of initializers/clean-up code. */
2041 void
2042 gfc_add_init_cleanup (gfc_wrapped_block* block, tree init, tree cleanup)
2044 gcc_assert (block);
2046 /* The new pair of init/cleanup should be "wrapped around" the existing
2047 block of code, thus the initialization is added to the front and the
2048 cleanup to the back. */
2049 add_expr_to_chain (&block->init, init, true);
2050 add_expr_to_chain (&block->cleanup, cleanup, false);
2054 /* Finish up a wrapped block by building a corresponding try-finally expr. */
2056 tree
2057 gfc_finish_wrapped_block (gfc_wrapped_block* block)
2059 tree result;
2061 gcc_assert (block);
2063 /* Build the final expression. For this, just add init and body together,
2064 and put clean-up with that into a TRY_FINALLY_EXPR. */
2065 result = block->init;
2066 add_expr_to_chain (&result, block->code, false);
2067 if (block->cleanup)
2068 result = build2_loc (input_location, TRY_FINALLY_EXPR, void_type_node,
2069 result, block->cleanup);
2071 /* Clear the block. */
2072 block->init = NULL_TREE;
2073 block->code = NULL_TREE;
2074 block->cleanup = NULL_TREE;
2076 return result;
2080 /* Helper function for marking a boolean expression tree as unlikely. */
2082 tree
2083 gfc_unlikely (tree cond, enum br_predictor predictor)
2085 tree tmp;
2087 if (optimize)
2089 cond = fold_convert (long_integer_type_node, cond);
2090 tmp = build_zero_cst (long_integer_type_node);
2091 cond = build_call_expr_loc (input_location,
2092 builtin_decl_explicit (BUILT_IN_EXPECT),
2093 3, cond, tmp,
2094 build_int_cst (integer_type_node,
2095 predictor));
2097 cond = fold_convert (boolean_type_node, cond);
2098 return cond;
2102 /* Helper function for marking a boolean expression tree as likely. */
2104 tree
2105 gfc_likely (tree cond, enum br_predictor predictor)
2107 tree tmp;
2109 if (optimize)
2111 cond = fold_convert (long_integer_type_node, cond);
2112 tmp = build_one_cst (long_integer_type_node);
2113 cond = build_call_expr_loc (input_location,
2114 builtin_decl_explicit (BUILT_IN_EXPECT),
2115 3, cond, tmp,
2116 build_int_cst (integer_type_node,
2117 predictor));
2119 cond = fold_convert (boolean_type_node, cond);
2120 return cond;
2124 /* Get the string length for a deferred character length component. */
2126 bool
2127 gfc_deferred_strlen (gfc_component *c, tree *decl)
2129 char name[GFC_MAX_SYMBOL_LEN+9];
2130 gfc_component *strlen;
2131 if (!(c->ts.type == BT_CHARACTER && c->ts.deferred))
2132 return false;
2133 sprintf (name, "_%s_length", c->name);
2134 for (strlen = c; strlen; strlen = strlen->next)
2135 if (strcmp (strlen->name, name) == 0)
2136 break;
2137 *decl = strlen ? strlen->backend_decl : NULL_TREE;
2138 return strlen != NULL;