openmp.c (match_acc): New generic function to parse OpenACC directives.
[official-gcc.git] / gcc / fortran / trans.c
blobd6b4a564bf5fb85f7947a2e52d8e85902c8a814b
1 /* Code translation -- generate GCC trees from gfc_code.
2 Copyright (C) 2002-2016 Free Software Foundation, Inc.
3 Contributed by Paul Brook
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "tree.h"
26 #include "gfortran.h"
27 #include "gimple-expr.h" /* For create_tmp_var_raw. */
28 #include "trans.h"
29 #include "stringpool.h"
30 #include "fold-const.h"
31 #include "tree-iterator.h"
32 #include "trans-stmt.h"
33 #include "trans-array.h"
34 #include "trans-types.h"
35 #include "trans-const.h"
37 /* Naming convention for backend interface code:
39 gfc_trans_* translate gfc_code into STMT trees.
41 gfc_conv_* expression conversion
43 gfc_get_* get a backend tree representation of a decl or type */
45 static gfc_file *gfc_current_backend_file;
47 const char gfc_msg_fault[] = N_("Array reference out of bounds");
48 const char gfc_msg_wrong_return[] = N_("Incorrect function return value");
51 /* Advance along TREE_CHAIN n times. */
53 tree
54 gfc_advance_chain (tree t, int n)
56 for (; n > 0; n--)
58 gcc_assert (t != NULL_TREE);
59 t = DECL_CHAIN (t);
61 return t;
65 /* Strip off a legitimate source ending from the input
66 string NAME of length LEN. */
68 static inline void
69 remove_suffix (char *name, int len)
71 int i;
73 for (i = 2; i < 8 && len > i; i++)
75 if (name[len - i] == '.')
77 name[len - i] = '\0';
78 break;
84 /* Creates a variable declaration with a given TYPE. */
86 tree
87 gfc_create_var_np (tree type, const char *prefix)
89 tree t;
91 t = create_tmp_var_raw (type, prefix);
93 /* No warnings for anonymous variables. */
94 if (prefix == NULL)
95 TREE_NO_WARNING (t) = 1;
97 return t;
101 /* Like above, but also adds it to the current scope. */
103 tree
104 gfc_create_var (tree type, const char *prefix)
106 tree tmp;
108 tmp = gfc_create_var_np (type, prefix);
110 pushdecl (tmp);
112 return tmp;
116 /* If the expression is not constant, evaluate it now. We assign the
117 result of the expression to an artificially created variable VAR, and
118 return a pointer to the VAR_DECL node for this variable. */
120 tree
121 gfc_evaluate_now_loc (location_t loc, tree expr, stmtblock_t * pblock)
123 tree var;
125 if (CONSTANT_CLASS_P (expr))
126 return expr;
128 var = gfc_create_var (TREE_TYPE (expr), NULL);
129 gfc_add_modify_loc (loc, pblock, var, expr);
131 return var;
135 tree
136 gfc_evaluate_now (tree expr, stmtblock_t * pblock)
138 return gfc_evaluate_now_loc (input_location, expr, pblock);
142 /* Build a MODIFY_EXPR node and add it to a given statement block PBLOCK.
143 A MODIFY_EXPR is an assignment:
144 LHS <- RHS. */
146 void
147 gfc_add_modify_loc (location_t loc, stmtblock_t * pblock, tree lhs, tree rhs)
149 tree tmp;
151 tree t1, t2;
152 t1 = TREE_TYPE (rhs);
153 t2 = TREE_TYPE (lhs);
154 /* Make sure that the types of the rhs and the lhs are the same
155 for scalar assignments. We should probably have something
156 similar for aggregates, but right now removing that check just
157 breaks everything. */
158 gcc_checking_assert (t1 == t2
159 || AGGREGATE_TYPE_P (TREE_TYPE (lhs)));
161 tmp = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, lhs,
162 rhs);
163 gfc_add_expr_to_block (pblock, tmp);
167 void
168 gfc_add_modify (stmtblock_t * pblock, tree lhs, tree rhs)
170 gfc_add_modify_loc (input_location, pblock, lhs, rhs);
174 /* Create a new scope/binding level and initialize a block. Care must be
175 taken when translating expressions as any temporaries will be placed in
176 the innermost scope. */
178 void
179 gfc_start_block (stmtblock_t * block)
181 /* Start a new binding level. */
182 pushlevel ();
183 block->has_scope = 1;
185 /* The block is empty. */
186 block->head = NULL_TREE;
190 /* Initialize a block without creating a new scope. */
192 void
193 gfc_init_block (stmtblock_t * block)
195 block->head = NULL_TREE;
196 block->has_scope = 0;
200 /* Sometimes we create a scope but it turns out that we don't actually
201 need it. This function merges the scope of BLOCK with its parent.
202 Only variable decls will be merged, you still need to add the code. */
204 void
205 gfc_merge_block_scope (stmtblock_t * block)
207 tree decl;
208 tree next;
210 gcc_assert (block->has_scope);
211 block->has_scope = 0;
213 /* Remember the decls in this scope. */
214 decl = getdecls ();
215 poplevel (0, 0);
217 /* Add them to the parent scope. */
218 while (decl != NULL_TREE)
220 next = DECL_CHAIN (decl);
221 DECL_CHAIN (decl) = NULL_TREE;
223 pushdecl (decl);
224 decl = next;
229 /* Finish a scope containing a block of statements. */
231 tree
232 gfc_finish_block (stmtblock_t * stmtblock)
234 tree decl;
235 tree expr;
236 tree block;
238 expr = stmtblock->head;
239 if (!expr)
240 expr = build_empty_stmt (input_location);
242 stmtblock->head = NULL_TREE;
244 if (stmtblock->has_scope)
246 decl = getdecls ();
248 if (decl)
250 block = poplevel (1, 0);
251 expr = build3_v (BIND_EXPR, decl, expr, block);
253 else
254 poplevel (0, 0);
257 return expr;
261 /* Build an ADDR_EXPR and cast the result to TYPE. If TYPE is NULL, the
262 natural type is used. */
264 tree
265 gfc_build_addr_expr (tree type, tree t)
267 tree base_type = TREE_TYPE (t);
268 tree natural_type;
270 if (type && POINTER_TYPE_P (type)
271 && TREE_CODE (base_type) == ARRAY_TYPE
272 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
273 == TYPE_MAIN_VARIANT (TREE_TYPE (base_type)))
275 tree min_val = size_zero_node;
276 tree type_domain = TYPE_DOMAIN (base_type);
277 if (type_domain && TYPE_MIN_VALUE (type_domain))
278 min_val = TYPE_MIN_VALUE (type_domain);
279 t = fold (build4_loc (input_location, ARRAY_REF, TREE_TYPE (type),
280 t, min_val, NULL_TREE, NULL_TREE));
281 natural_type = type;
283 else
284 natural_type = build_pointer_type (base_type);
286 if (TREE_CODE (t) == INDIRECT_REF)
288 if (!type)
289 type = natural_type;
290 t = TREE_OPERAND (t, 0);
291 natural_type = TREE_TYPE (t);
293 else
295 tree base = get_base_address (t);
296 if (base && DECL_P (base))
297 TREE_ADDRESSABLE (base) = 1;
298 t = fold_build1_loc (input_location, ADDR_EXPR, natural_type, t);
301 if (type && natural_type != type)
302 t = convert (type, t);
304 return t;
308 /* Build an ARRAY_REF with its natural type. */
310 tree
311 gfc_build_array_ref (tree base, tree offset, tree decl, tree vptr)
313 tree type = TREE_TYPE (base);
314 tree tmp;
315 tree span;
317 if (GFC_ARRAY_TYPE_P (type) && GFC_TYPE_ARRAY_RANK (type) == 0)
319 gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
321 return fold_convert (TYPE_MAIN_VARIANT (type), base);
324 /* Scalar coarray, there is nothing to do. */
325 if (TREE_CODE (type) != ARRAY_TYPE)
327 gcc_assert (decl == NULL_TREE);
328 gcc_assert (integer_zerop (offset));
329 return base;
332 type = TREE_TYPE (type);
334 /* Use pointer arithmetic for deferred character length array
335 references. */
336 if (type && TREE_CODE (type) == ARRAY_TYPE
337 && TYPE_MAXVAL (TYPE_DOMAIN (type)) != NULL_TREE
338 && (TREE_CODE (TYPE_MAXVAL (TYPE_DOMAIN (type))) == VAR_DECL
339 || TREE_CODE (TYPE_MAXVAL (TYPE_DOMAIN (type))) == INDIRECT_REF)
340 && decl
341 && (TREE_CODE (TYPE_MAXVAL (TYPE_DOMAIN (type))) == INDIRECT_REF
342 || TREE_CODE (decl) == FUNCTION_DECL
343 || DECL_CONTEXT (TYPE_MAXVAL (TYPE_DOMAIN (type)))
344 == DECL_CONTEXT (decl)))
345 span = TYPE_MAXVAL (TYPE_DOMAIN (type));
346 else
347 span = NULL_TREE;
349 if (DECL_P (base))
350 TREE_ADDRESSABLE (base) = 1;
352 /* Strip NON_LVALUE_EXPR nodes. */
353 STRIP_TYPE_NOPS (offset);
355 /* If the array reference is to a pointer, whose target contains a
356 subreference, use the span that is stored with the backend decl
357 and reference the element with pointer arithmetic. */
358 if ((decl && (TREE_CODE (decl) == FIELD_DECL
359 || TREE_CODE (decl) == VAR_DECL
360 || TREE_CODE (decl) == PARM_DECL
361 || TREE_CODE (decl) == FUNCTION_DECL)
362 && ((GFC_DECL_SUBREF_ARRAY_P (decl)
363 && !integer_zerop (GFC_DECL_SPAN (decl)))
364 || GFC_DECL_CLASS (decl)
365 || span != NULL_TREE))
366 || vptr != NULL_TREE)
368 if (decl)
370 if (GFC_DECL_CLASS (decl))
372 /* When a temporary is in place for the class array, then the
373 original class' declaration is stored in the saved
374 descriptor. */
375 if (DECL_LANG_SPECIFIC (decl) && GFC_DECL_SAVED_DESCRIPTOR (decl))
376 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
377 else
379 /* Allow for dummy arguments and other good things. */
380 if (POINTER_TYPE_P (TREE_TYPE (decl)))
381 decl = build_fold_indirect_ref_loc (input_location, decl);
383 /* Check if '_data' is an array descriptor. If it is not,
384 the array must be one of the components of the class
385 object, so return a normal array reference. */
386 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (
387 gfc_class_data_get (decl))))
388 return build4_loc (input_location, ARRAY_REF, type, base,
389 offset, NULL_TREE, NULL_TREE);
392 span = gfc_class_vtab_size_get (decl);
394 else if (GFC_DECL_SUBREF_ARRAY_P (decl))
395 span = GFC_DECL_SPAN (decl);
396 else if (span)
397 span = fold_convert (gfc_array_index_type, span);
398 else
399 gcc_unreachable ();
401 else if (vptr)
402 span = gfc_vptr_size_get (vptr);
403 else
404 gcc_unreachable ();
406 offset = fold_build2_loc (input_location, MULT_EXPR,
407 gfc_array_index_type,
408 offset, span);
409 tmp = gfc_build_addr_expr (pvoid_type_node, base);
410 tmp = fold_build_pointer_plus_loc (input_location, tmp, offset);
411 tmp = fold_convert (build_pointer_type (type), tmp);
412 if (!TYPE_STRING_FLAG (type))
413 tmp = build_fold_indirect_ref_loc (input_location, tmp);
414 return tmp;
416 else
417 /* Otherwise use a straightforward array reference. */
418 return build4_loc (input_location, ARRAY_REF, type, base, offset,
419 NULL_TREE, NULL_TREE);
423 /* Generate a call to print a runtime error possibly including multiple
424 arguments and a locus. */
426 static tree
427 trans_runtime_error_vararg (bool error, locus* where, const char* msgid,
428 va_list ap)
430 stmtblock_t block;
431 tree tmp;
432 tree arg, arg2;
433 tree *argarray;
434 tree fntype;
435 char *message;
436 const char *p;
437 int line, nargs, i;
438 location_t loc;
440 /* Compute the number of extra arguments from the format string. */
441 for (p = msgid, nargs = 0; *p; p++)
442 if (*p == '%')
444 p++;
445 if (*p != '%')
446 nargs++;
449 /* The code to generate the error. */
450 gfc_start_block (&block);
452 if (where)
454 line = LOCATION_LINE (where->lb->location);
455 message = xasprintf ("At line %d of file %s", line,
456 where->lb->file->filename);
458 else
459 message = xasprintf ("In file '%s', around line %d",
460 gfc_source_file, LOCATION_LINE (input_location) + 1);
462 arg = gfc_build_addr_expr (pchar_type_node,
463 gfc_build_localized_cstring_const (message));
464 free (message);
466 message = xasprintf ("%s", _(msgid));
467 arg2 = gfc_build_addr_expr (pchar_type_node,
468 gfc_build_localized_cstring_const (message));
469 free (message);
471 /* Build the argument array. */
472 argarray = XALLOCAVEC (tree, nargs + 2);
473 argarray[0] = arg;
474 argarray[1] = arg2;
475 for (i = 0; i < nargs; i++)
476 argarray[2 + i] = va_arg (ap, tree);
478 /* Build the function call to runtime_(warning,error)_at; because of the
479 variable number of arguments, we can't use build_call_expr_loc dinput_location,
480 irectly. */
481 if (error)
482 fntype = TREE_TYPE (gfor_fndecl_runtime_error_at);
483 else
484 fntype = TREE_TYPE (gfor_fndecl_runtime_warning_at);
486 loc = where ? where->lb->location : input_location;
487 tmp = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
488 fold_build1_loc (loc, ADDR_EXPR,
489 build_pointer_type (fntype),
490 error
491 ? gfor_fndecl_runtime_error_at
492 : gfor_fndecl_runtime_warning_at),
493 nargs + 2, argarray);
494 gfc_add_expr_to_block (&block, tmp);
496 return gfc_finish_block (&block);
500 tree
501 gfc_trans_runtime_error (bool error, locus* where, const char* msgid, ...)
503 va_list ap;
504 tree result;
506 va_start (ap, msgid);
507 result = trans_runtime_error_vararg (error, where, msgid, ap);
508 va_end (ap);
509 return result;
513 /* Generate a runtime error if COND is true. */
515 void
516 gfc_trans_runtime_check (bool error, bool once, tree cond, stmtblock_t * pblock,
517 locus * where, const char * msgid, ...)
519 va_list ap;
520 stmtblock_t block;
521 tree body;
522 tree tmp;
523 tree tmpvar = NULL;
525 if (integer_zerop (cond))
526 return;
528 if (once)
530 tmpvar = gfc_create_var (boolean_type_node, "print_warning");
531 TREE_STATIC (tmpvar) = 1;
532 DECL_INITIAL (tmpvar) = boolean_true_node;
533 gfc_add_expr_to_block (pblock, tmpvar);
536 gfc_start_block (&block);
538 /* For error, runtime_error_at already implies PRED_NORETURN. */
539 if (!error && once)
540 gfc_add_expr_to_block (&block, build_predict_expr (PRED_FORTRAN_WARN_ONCE,
541 NOT_TAKEN));
543 /* The code to generate the error. */
544 va_start (ap, msgid);
545 gfc_add_expr_to_block (&block,
546 trans_runtime_error_vararg (error, where,
547 msgid, ap));
548 va_end (ap);
550 if (once)
551 gfc_add_modify (&block, tmpvar, boolean_false_node);
553 body = gfc_finish_block (&block);
555 if (integer_onep (cond))
557 gfc_add_expr_to_block (pblock, body);
559 else
561 if (once)
562 cond = fold_build2_loc (where->lb->location, TRUTH_AND_EXPR,
563 long_integer_type_node, tmpvar, cond);
564 else
565 cond = fold_convert (long_integer_type_node, cond);
567 tmp = fold_build3_loc (where->lb->location, COND_EXPR, void_type_node,
568 cond, body,
569 build_empty_stmt (where->lb->location));
570 gfc_add_expr_to_block (pblock, tmp);
575 /* Call malloc to allocate size bytes of memory, with special conditions:
576 + if size == 0, return a malloced area of size 1,
577 + if malloc returns NULL, issue a runtime error. */
578 tree
579 gfc_call_malloc (stmtblock_t * block, tree type, tree size)
581 tree tmp, msg, malloc_result, null_result, res, malloc_tree;
582 stmtblock_t block2;
584 /* Create a variable to hold the result. */
585 res = gfc_create_var (prvoid_type_node, NULL);
587 /* Call malloc. */
588 gfc_start_block (&block2);
590 size = fold_convert (size_type_node, size);
591 size = fold_build2_loc (input_location, MAX_EXPR, size_type_node, size,
592 build_int_cst (size_type_node, 1));
594 malloc_tree = builtin_decl_explicit (BUILT_IN_MALLOC);
595 gfc_add_modify (&block2, res,
596 fold_convert (prvoid_type_node,
597 build_call_expr_loc (input_location,
598 malloc_tree, 1, size)));
600 /* Optionally check whether malloc was successful. */
601 if (gfc_option.rtcheck & GFC_RTCHECK_MEM)
603 null_result = fold_build2_loc (input_location, EQ_EXPR,
604 boolean_type_node, res,
605 build_int_cst (pvoid_type_node, 0));
606 msg = gfc_build_addr_expr (pchar_type_node,
607 gfc_build_localized_cstring_const ("Memory allocation failed"));
608 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
609 null_result,
610 build_call_expr_loc (input_location,
611 gfor_fndecl_os_error, 1, msg),
612 build_empty_stmt (input_location));
613 gfc_add_expr_to_block (&block2, tmp);
616 malloc_result = gfc_finish_block (&block2);
617 gfc_add_expr_to_block (block, malloc_result);
619 if (type != NULL)
620 res = fold_convert (type, res);
621 return res;
625 /* Allocate memory, using an optional status argument.
627 This function follows the following pseudo-code:
629 void *
630 allocate (size_t size, integer_type stat)
632 void *newmem;
634 if (stat requested)
635 stat = 0;
637 newmem = malloc (MAX (size, 1));
638 if (newmem == NULL)
640 if (stat)
641 *stat = LIBERROR_ALLOCATION;
642 else
643 runtime_error ("Allocation would exceed memory limit");
645 return newmem;
646 } */
647 void
648 gfc_allocate_using_malloc (stmtblock_t * block, tree pointer,
649 tree size, tree status)
651 tree tmp, error_cond;
652 stmtblock_t on_error;
653 tree status_type = status ? TREE_TYPE (status) : NULL_TREE;
655 /* If successful and stat= is given, set status to 0. */
656 if (status != NULL_TREE)
657 gfc_add_expr_to_block (block,
658 fold_build2_loc (input_location, MODIFY_EXPR, status_type,
659 status, build_int_cst (status_type, 0)));
661 /* The allocation itself. */
662 size = fold_convert (size_type_node, size);
663 gfc_add_modify (block, pointer,
664 fold_convert (TREE_TYPE (pointer),
665 build_call_expr_loc (input_location,
666 builtin_decl_explicit (BUILT_IN_MALLOC), 1,
667 fold_build2_loc (input_location,
668 MAX_EXPR, size_type_node, size,
669 build_int_cst (size_type_node, 1)))));
671 /* What to do in case of error. */
672 gfc_start_block (&on_error);
673 if (status != NULL_TREE)
675 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type, status,
676 build_int_cst (status_type, LIBERROR_ALLOCATION));
677 gfc_add_expr_to_block (&on_error, tmp);
679 else
681 /* Here, os_error already implies PRED_NORETURN. */
682 tmp = build_call_expr_loc (input_location, gfor_fndecl_os_error, 1,
683 gfc_build_addr_expr (pchar_type_node,
684 gfc_build_localized_cstring_const
685 ("Allocation would exceed memory limit")));
686 gfc_add_expr_to_block (&on_error, tmp);
689 error_cond = fold_build2_loc (input_location, EQ_EXPR,
690 boolean_type_node, pointer,
691 build_int_cst (prvoid_type_node, 0));
692 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
693 gfc_unlikely (error_cond, PRED_FORTRAN_FAIL_ALLOC),
694 gfc_finish_block (&on_error),
695 build_empty_stmt (input_location));
697 gfc_add_expr_to_block (block, tmp);
701 /* Allocate memory, using an optional status argument.
703 This function follows the following pseudo-code:
705 void *
706 allocate (size_t size, void** token, int *stat, char* errmsg, int errlen)
708 void *newmem;
710 newmem = _caf_register (size, regtype, token, &stat, errmsg, errlen);
711 return newmem;
712 } */
713 static void
714 gfc_allocate_using_lib (stmtblock_t * block, tree pointer, tree size,
715 tree token, tree status, tree errmsg, tree errlen,
716 bool lock_var, bool event_var)
718 tree tmp, pstat;
720 gcc_assert (token != NULL_TREE);
722 /* The allocation itself. */
723 if (status == NULL_TREE)
724 pstat = null_pointer_node;
725 else
726 pstat = gfc_build_addr_expr (NULL_TREE, status);
728 if (errmsg == NULL_TREE)
730 gcc_assert(errlen == NULL_TREE);
731 errmsg = null_pointer_node;
732 errlen = build_int_cst (integer_type_node, 0);
735 size = fold_convert (size_type_node, size);
736 tmp = build_call_expr_loc (input_location,
737 gfor_fndecl_caf_register, 6,
738 fold_build2_loc (input_location,
739 MAX_EXPR, size_type_node, size,
740 build_int_cst (size_type_node, 1)),
741 build_int_cst (integer_type_node,
742 lock_var ? GFC_CAF_LOCK_ALLOC
743 : event_var ? GFC_CAF_EVENT_ALLOC
744 : GFC_CAF_COARRAY_ALLOC),
745 token, pstat, errmsg, errlen);
747 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
748 TREE_TYPE (pointer), pointer,
749 fold_convert ( TREE_TYPE (pointer), tmp));
750 gfc_add_expr_to_block (block, tmp);
752 /* It guarantees memory consistency within the same segment */
753 tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
754 tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
755 gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
756 tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
757 ASM_VOLATILE_P (tmp) = 1;
758 gfc_add_expr_to_block (block, tmp);
762 /* Generate code for an ALLOCATE statement when the argument is an
763 allocatable variable. If the variable is currently allocated, it is an
764 error to allocate it again.
766 This function follows the following pseudo-code:
768 void *
769 allocate_allocatable (void *mem, size_t size, integer_type stat)
771 if (mem == NULL)
772 return allocate (size, stat);
773 else
775 if (stat)
776 stat = LIBERROR_ALLOCATION;
777 else
778 runtime_error ("Attempting to allocate already allocated variable");
782 expr must be set to the original expression being allocated for its locus
783 and variable name in case a runtime error has to be printed. */
784 void
785 gfc_allocate_allocatable (stmtblock_t * block, tree mem, tree size, tree token,
786 tree status, tree errmsg, tree errlen, tree label_finish,
787 gfc_expr* expr)
789 stmtblock_t alloc_block;
790 tree tmp, null_mem, alloc, error;
791 tree type = TREE_TYPE (mem);
793 size = fold_convert (size_type_node, size);
794 null_mem = gfc_unlikely (fold_build2_loc (input_location, NE_EXPR,
795 boolean_type_node, mem,
796 build_int_cst (type, 0)),
797 PRED_FORTRAN_REALLOC);
799 /* If mem is NULL, we call gfc_allocate_using_malloc or
800 gfc_allocate_using_lib. */
801 gfc_start_block (&alloc_block);
803 if (flag_coarray == GFC_FCOARRAY_LIB
804 && gfc_expr_attr (expr).codimension)
806 tree cond;
807 bool lock_var = expr->ts.type == BT_DERIVED
808 && expr->ts.u.derived->from_intmod
809 == INTMOD_ISO_FORTRAN_ENV
810 && expr->ts.u.derived->intmod_sym_id
811 == ISOFORTRAN_LOCK_TYPE;
812 bool event_var = expr->ts.type == BT_DERIVED
813 && expr->ts.u.derived->from_intmod
814 == INTMOD_ISO_FORTRAN_ENV
815 && expr->ts.u.derived->intmod_sym_id
816 == ISOFORTRAN_EVENT_TYPE;
817 /* In the front end, we represent the lock variable as pointer. However,
818 the FE only passes the pointer around and leaves the actual
819 representation to the library. Hence, we have to convert back to the
820 number of elements. */
821 if (lock_var || event_var)
822 size = fold_build2_loc (input_location, TRUNC_DIV_EXPR, size_type_node,
823 size, TYPE_SIZE_UNIT (ptr_type_node));
825 gfc_allocate_using_lib (&alloc_block, mem, size, token, status,
826 errmsg, errlen, lock_var, event_var);
828 if (status != NULL_TREE)
830 TREE_USED (label_finish) = 1;
831 tmp = build1_v (GOTO_EXPR, label_finish);
832 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
833 status, build_zero_cst (TREE_TYPE (status)));
834 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
835 gfc_unlikely (cond, PRED_FORTRAN_FAIL_ALLOC),
836 tmp, build_empty_stmt (input_location));
837 gfc_add_expr_to_block (&alloc_block, tmp);
840 else
841 gfc_allocate_using_malloc (&alloc_block, mem, size, status);
843 alloc = gfc_finish_block (&alloc_block);
845 /* If mem is not NULL, we issue a runtime error or set the
846 status variable. */
847 if (expr)
849 tree varname;
851 gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->symtree);
852 varname = gfc_build_cstring_const (expr->symtree->name);
853 varname = gfc_build_addr_expr (pchar_type_node, varname);
855 error = gfc_trans_runtime_error (true, &expr->where,
856 "Attempting to allocate already"
857 " allocated variable '%s'",
858 varname);
860 else
861 error = gfc_trans_runtime_error (true, NULL,
862 "Attempting to allocate already allocated"
863 " variable");
865 if (status != NULL_TREE)
867 tree status_type = TREE_TYPE (status);
869 error = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
870 status, build_int_cst (status_type, LIBERROR_ALLOCATION));
873 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, null_mem,
874 error, alloc);
875 gfc_add_expr_to_block (block, tmp);
879 /* Free a given variable. */
881 tree
882 gfc_call_free (tree var)
884 return build_call_expr_loc (input_location,
885 builtin_decl_explicit (BUILT_IN_FREE),
886 1, fold_convert (pvoid_type_node, var));
890 /* Build a call to a FINAL procedure, which finalizes "var". */
892 static tree
893 gfc_build_final_call (gfc_typespec ts, gfc_expr *final_wrapper, gfc_expr *var,
894 bool fini_coarray, gfc_expr *class_size)
896 stmtblock_t block;
897 gfc_se se;
898 tree final_fndecl, array, size, tmp;
899 symbol_attribute attr;
901 gcc_assert (final_wrapper->expr_type == EXPR_VARIABLE);
902 gcc_assert (var);
904 gfc_start_block (&block);
905 gfc_init_se (&se, NULL);
906 gfc_conv_expr (&se, final_wrapper);
907 final_fndecl = se.expr;
908 if (POINTER_TYPE_P (TREE_TYPE (final_fndecl)))
909 final_fndecl = build_fold_indirect_ref_loc (input_location, final_fndecl);
911 if (ts.type == BT_DERIVED)
913 tree elem_size;
915 gcc_assert (!class_size);
916 elem_size = gfc_typenode_for_spec (&ts);
917 elem_size = TYPE_SIZE_UNIT (elem_size);
918 size = fold_convert (gfc_array_index_type, elem_size);
920 gfc_init_se (&se, NULL);
921 se.want_pointer = 1;
922 if (var->rank)
924 se.descriptor_only = 1;
925 gfc_conv_expr_descriptor (&se, var);
926 array = se.expr;
928 else
930 gfc_conv_expr (&se, var);
931 gcc_assert (se.pre.head == NULL_TREE && se.post.head == NULL_TREE);
932 array = se.expr;
934 /* No copy back needed, hence set attr's allocatable/pointer
935 to zero. */
936 gfc_clear_attr (&attr);
937 gfc_init_se (&se, NULL);
938 array = gfc_conv_scalar_to_descriptor (&se, array, attr);
939 gcc_assert (se.post.head == NULL_TREE);
942 else
944 gfc_expr *array_expr;
945 gcc_assert (class_size);
946 gfc_init_se (&se, NULL);
947 gfc_conv_expr (&se, class_size);
948 gfc_add_block_to_block (&block, &se.pre);
949 gcc_assert (se.post.head == NULL_TREE);
950 size = se.expr;
952 array_expr = gfc_copy_expr (var);
953 gfc_init_se (&se, NULL);
954 se.want_pointer = 1;
955 if (array_expr->rank)
957 gfc_add_class_array_ref (array_expr);
958 se.descriptor_only = 1;
959 gfc_conv_expr_descriptor (&se, array_expr);
960 array = se.expr;
962 else
964 gfc_add_data_component (array_expr);
965 gfc_conv_expr (&se, array_expr);
966 gfc_add_block_to_block (&block, &se.pre);
967 gcc_assert (se.post.head == NULL_TREE);
968 array = se.expr;
969 if (TREE_CODE (array) == ADDR_EXPR
970 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (array, 0))))
971 tmp = TREE_OPERAND (array, 0);
973 if (!gfc_is_coarray (array_expr))
975 /* No copy back needed, hence set attr's allocatable/pointer
976 to zero. */
977 gfc_clear_attr (&attr);
978 gfc_init_se (&se, NULL);
979 array = gfc_conv_scalar_to_descriptor (&se, array, attr);
981 gcc_assert (se.post.head == NULL_TREE);
983 gfc_free_expr (array_expr);
986 if (!POINTER_TYPE_P (TREE_TYPE (array)))
987 array = gfc_build_addr_expr (NULL, array);
989 gfc_add_block_to_block (&block, &se.pre);
990 tmp = build_call_expr_loc (input_location,
991 final_fndecl, 3, array,
992 size, fini_coarray ? boolean_true_node
993 : boolean_false_node);
994 gfc_add_block_to_block (&block, &se.post);
995 gfc_add_expr_to_block (&block, tmp);
996 return gfc_finish_block (&block);
1000 bool
1001 gfc_add_comp_finalizer_call (stmtblock_t *block, tree decl, gfc_component *comp,
1002 bool fini_coarray)
1004 gfc_se se;
1005 stmtblock_t block2;
1006 tree final_fndecl, size, array, tmp, cond;
1007 symbol_attribute attr;
1008 gfc_expr *final_expr = NULL;
1010 if (comp->ts.type != BT_DERIVED && comp->ts.type != BT_CLASS)
1011 return false;
1013 gfc_init_block (&block2);
1015 if (comp->ts.type == BT_DERIVED)
1017 if (comp->attr.pointer)
1018 return false;
1020 gfc_is_finalizable (comp->ts.u.derived, &final_expr);
1021 if (!final_expr)
1022 return false;
1024 gfc_init_se (&se, NULL);
1025 gfc_conv_expr (&se, final_expr);
1026 final_fndecl = se.expr;
1027 size = gfc_typenode_for_spec (&comp->ts);
1028 size = TYPE_SIZE_UNIT (size);
1029 size = fold_convert (gfc_array_index_type, size);
1031 array = decl;
1033 else /* comp->ts.type == BT_CLASS. */
1035 if (CLASS_DATA (comp)->attr.class_pointer)
1036 return false;
1038 gfc_is_finalizable (CLASS_DATA (comp)->ts.u.derived, &final_expr);
1039 final_fndecl = gfc_class_vtab_final_get (decl);
1040 size = gfc_class_vtab_size_get (decl);
1041 array = gfc_class_data_get (decl);
1044 if (comp->attr.allocatable
1045 || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->attr.allocatable))
1047 tmp = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array))
1048 ? gfc_conv_descriptor_data_get (array) : array;
1049 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1050 tmp, fold_convert (TREE_TYPE (tmp),
1051 null_pointer_node));
1053 else
1054 cond = boolean_true_node;
1056 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array)))
1058 gfc_clear_attr (&attr);
1059 gfc_init_se (&se, NULL);
1060 array = gfc_conv_scalar_to_descriptor (&se, array, attr);
1061 gfc_add_block_to_block (&block2, &se.pre);
1062 gcc_assert (se.post.head == NULL_TREE);
1065 if (!POINTER_TYPE_P (TREE_TYPE (array)))
1066 array = gfc_build_addr_expr (NULL, array);
1068 if (!final_expr)
1070 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1071 final_fndecl,
1072 fold_convert (TREE_TYPE (final_fndecl),
1073 null_pointer_node));
1074 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1075 boolean_type_node, cond, tmp);
1078 if (POINTER_TYPE_P (TREE_TYPE (final_fndecl)))
1079 final_fndecl = build_fold_indirect_ref_loc (input_location, final_fndecl);
1081 tmp = build_call_expr_loc (input_location,
1082 final_fndecl, 3, array,
1083 size, fini_coarray ? boolean_true_node
1084 : boolean_false_node);
1085 gfc_add_expr_to_block (&block2, tmp);
1086 tmp = gfc_finish_block (&block2);
1088 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
1089 build_empty_stmt (input_location));
1090 gfc_add_expr_to_block (block, tmp);
1092 return true;
1096 /* Add a call to the finalizer, using the passed *expr. Returns
1097 true when a finalizer call has been inserted. */
1099 bool
1100 gfc_add_finalizer_call (stmtblock_t *block, gfc_expr *expr2)
1102 tree tmp;
1103 gfc_ref *ref;
1104 gfc_expr *expr;
1105 gfc_expr *final_expr = NULL;
1106 gfc_expr *elem_size = NULL;
1107 bool has_finalizer = false;
1109 if (!expr2 || (expr2->ts.type != BT_DERIVED && expr2->ts.type != BT_CLASS))
1110 return false;
1112 if (expr2->ts.type == BT_DERIVED)
1114 gfc_is_finalizable (expr2->ts.u.derived, &final_expr);
1115 if (!final_expr)
1116 return false;
1119 /* If we have a class array, we need go back to the class
1120 container. */
1121 expr = gfc_copy_expr (expr2);
1123 if (expr->ref && expr->ref->next && !expr->ref->next->next
1124 && expr->ref->next->type == REF_ARRAY
1125 && expr->ref->type == REF_COMPONENT
1126 && strcmp (expr->ref->u.c.component->name, "_data") == 0)
1128 gfc_free_ref_list (expr->ref);
1129 expr->ref = NULL;
1131 else
1132 for (ref = expr->ref; ref; ref = ref->next)
1133 if (ref->next && ref->next->next && !ref->next->next->next
1134 && ref->next->next->type == REF_ARRAY
1135 && ref->next->type == REF_COMPONENT
1136 && strcmp (ref->next->u.c.component->name, "_data") == 0)
1138 gfc_free_ref_list (ref->next);
1139 ref->next = NULL;
1142 if (expr->ts.type == BT_CLASS)
1144 has_finalizer = gfc_is_finalizable (expr->ts.u.derived, NULL);
1146 if (!expr2->rank && !expr2->ref && CLASS_DATA (expr2->symtree->n.sym)->as)
1147 expr->rank = CLASS_DATA (expr2->symtree->n.sym)->as->rank;
1149 final_expr = gfc_copy_expr (expr);
1150 gfc_add_vptr_component (final_expr);
1151 gfc_add_component_ref (final_expr, "_final");
1153 elem_size = gfc_copy_expr (expr);
1154 gfc_add_vptr_component (elem_size);
1155 gfc_add_component_ref (elem_size, "_size");
1158 gcc_assert (final_expr->expr_type == EXPR_VARIABLE);
1160 tmp = gfc_build_final_call (expr->ts, final_expr, expr,
1161 false, elem_size);
1163 if (expr->ts.type == BT_CLASS && !has_finalizer)
1165 tree cond;
1166 gfc_se se;
1168 gfc_init_se (&se, NULL);
1169 se.want_pointer = 1;
1170 gfc_conv_expr (&se, final_expr);
1171 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1172 se.expr, build_int_cst (TREE_TYPE (se.expr), 0));
1174 /* For CLASS(*) not only sym->_vtab->_final can be NULL
1175 but already sym->_vtab itself. */
1176 if (UNLIMITED_POLY (expr))
1178 tree cond2;
1179 gfc_expr *vptr_expr;
1181 vptr_expr = gfc_copy_expr (expr);
1182 gfc_add_vptr_component (vptr_expr);
1184 gfc_init_se (&se, NULL);
1185 se.want_pointer = 1;
1186 gfc_conv_expr (&se, vptr_expr);
1187 gfc_free_expr (vptr_expr);
1189 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1190 se.expr,
1191 build_int_cst (TREE_TYPE (se.expr), 0));
1192 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1193 boolean_type_node, cond2, cond);
1196 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1197 cond, tmp, build_empty_stmt (input_location));
1200 gfc_add_expr_to_block (block, tmp);
1202 return true;
1206 /* User-deallocate; we emit the code directly from the front-end, and the
1207 logic is the same as the previous library function:
1209 void
1210 deallocate (void *pointer, GFC_INTEGER_4 * stat)
1212 if (!pointer)
1214 if (stat)
1215 *stat = 1;
1216 else
1217 runtime_error ("Attempt to DEALLOCATE unallocated memory.");
1219 else
1221 free (pointer);
1222 if (stat)
1223 *stat = 0;
1227 In this front-end version, status doesn't have to be GFC_INTEGER_4.
1228 Moreover, if CAN_FAIL is true, then we will not emit a runtime error,
1229 even when no status variable is passed to us (this is used for
1230 unconditional deallocation generated by the front-end at end of
1231 each procedure).
1233 If a runtime-message is possible, `expr' must point to the original
1234 expression being deallocated for its locus and variable name.
1236 For coarrays, "pointer" must be the array descriptor and not its
1237 "data" component. */
1238 tree
1239 gfc_deallocate_with_status (tree pointer, tree status, tree errmsg,
1240 tree errlen, tree label_finish,
1241 bool can_fail, gfc_expr* expr, bool coarray)
1243 stmtblock_t null, non_null;
1244 tree cond, tmp, error;
1245 tree status_type = NULL_TREE;
1246 tree caf_decl = NULL_TREE;
1248 if (coarray)
1250 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (pointer)));
1251 caf_decl = pointer;
1252 pointer = gfc_conv_descriptor_data_get (caf_decl);
1253 STRIP_NOPS (pointer);
1256 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, pointer,
1257 build_int_cst (TREE_TYPE (pointer), 0));
1259 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
1260 we emit a runtime error. */
1261 gfc_start_block (&null);
1262 if (!can_fail)
1264 tree varname;
1266 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
1268 varname = gfc_build_cstring_const (expr->symtree->name);
1269 varname = gfc_build_addr_expr (pchar_type_node, varname);
1271 error = gfc_trans_runtime_error (true, &expr->where,
1272 "Attempt to DEALLOCATE unallocated '%s'",
1273 varname);
1275 else
1276 error = build_empty_stmt (input_location);
1278 if (status != NULL_TREE && !integer_zerop (status))
1280 tree cond2;
1282 status_type = TREE_TYPE (TREE_TYPE (status));
1283 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1284 status, build_int_cst (TREE_TYPE (status), 0));
1285 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
1286 fold_build1_loc (input_location, INDIRECT_REF,
1287 status_type, status),
1288 build_int_cst (status_type, 1));
1289 error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1290 cond2, tmp, error);
1293 gfc_add_expr_to_block (&null, error);
1295 /* When POINTER is not NULL, we free it. */
1296 gfc_start_block (&non_null);
1297 gfc_add_finalizer_call (&non_null, expr);
1298 if (!coarray || flag_coarray != GFC_FCOARRAY_LIB)
1300 tmp = build_call_expr_loc (input_location,
1301 builtin_decl_explicit (BUILT_IN_FREE), 1,
1302 fold_convert (pvoid_type_node, pointer));
1303 gfc_add_expr_to_block (&non_null, tmp);
1305 if (status != NULL_TREE && !integer_zerop (status))
1307 /* We set STATUS to zero if it is present. */
1308 tree status_type = TREE_TYPE (TREE_TYPE (status));
1309 tree cond2;
1311 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1312 status,
1313 build_int_cst (TREE_TYPE (status), 0));
1314 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
1315 fold_build1_loc (input_location, INDIRECT_REF,
1316 status_type, status),
1317 build_int_cst (status_type, 0));
1318 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1319 gfc_unlikely (cond2, PRED_FORTRAN_FAIL_ALLOC),
1320 tmp, build_empty_stmt (input_location));
1321 gfc_add_expr_to_block (&non_null, tmp);
1324 else
1326 tree caf_type, token, cond2;
1327 tree pstat = null_pointer_node;
1329 if (errmsg == NULL_TREE)
1331 gcc_assert (errlen == NULL_TREE);
1332 errmsg = null_pointer_node;
1333 errlen = build_zero_cst (integer_type_node);
1335 else
1337 gcc_assert (errlen != NULL_TREE);
1338 if (!POINTER_TYPE_P (TREE_TYPE (errmsg)))
1339 errmsg = gfc_build_addr_expr (NULL_TREE, errmsg);
1342 caf_type = TREE_TYPE (caf_decl);
1344 if (status != NULL_TREE && !integer_zerop (status))
1346 gcc_assert (status_type == integer_type_node);
1347 pstat = status;
1350 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
1351 && GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE)
1352 token = gfc_conv_descriptor_token (caf_decl);
1353 else if (DECL_LANG_SPECIFIC (caf_decl)
1354 && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
1355 token = GFC_DECL_TOKEN (caf_decl);
1356 else
1358 gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
1359 && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
1360 token = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
1363 token = gfc_build_addr_expr (NULL_TREE, token);
1364 tmp = build_call_expr_loc (input_location,
1365 gfor_fndecl_caf_deregister, 4,
1366 token, pstat, errmsg, errlen);
1367 gfc_add_expr_to_block (&non_null, tmp);
1369 /* It guarantees memory consistency within the same segment */
1370 tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
1371 tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1372 gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1373 tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1374 ASM_VOLATILE_P (tmp) = 1;
1375 gfc_add_expr_to_block (&non_null, tmp);
1377 if (status != NULL_TREE)
1379 tree stat = build_fold_indirect_ref_loc (input_location, status);
1381 TREE_USED (label_finish) = 1;
1382 tmp = build1_v (GOTO_EXPR, label_finish);
1383 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1384 stat, build_zero_cst (TREE_TYPE (stat)));
1385 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1386 gfc_unlikely (cond2, PRED_FORTRAN_REALLOC),
1387 tmp, build_empty_stmt (input_location));
1388 gfc_add_expr_to_block (&non_null, tmp);
1392 return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
1393 gfc_finish_block (&null),
1394 gfc_finish_block (&non_null));
1398 /* Generate code for deallocation of allocatable scalars (variables or
1399 components). Before the object itself is freed, any allocatable
1400 subcomponents are being deallocated. */
1402 tree
1403 gfc_deallocate_scalar_with_status (tree pointer, tree status, bool can_fail,
1404 gfc_expr* expr, gfc_typespec ts)
1406 stmtblock_t null, non_null;
1407 tree cond, tmp, error;
1408 bool finalizable;
1410 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, pointer,
1411 build_int_cst (TREE_TYPE (pointer), 0));
1413 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
1414 we emit a runtime error. */
1415 gfc_start_block (&null);
1416 if (!can_fail)
1418 tree varname;
1420 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
1422 varname = gfc_build_cstring_const (expr->symtree->name);
1423 varname = gfc_build_addr_expr (pchar_type_node, varname);
1425 error = gfc_trans_runtime_error (true, &expr->where,
1426 "Attempt to DEALLOCATE unallocated '%s'",
1427 varname);
1429 else
1430 error = build_empty_stmt (input_location);
1432 if (status != NULL_TREE && !integer_zerop (status))
1434 tree status_type = TREE_TYPE (TREE_TYPE (status));
1435 tree cond2;
1437 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1438 status, build_int_cst (TREE_TYPE (status), 0));
1439 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
1440 fold_build1_loc (input_location, INDIRECT_REF,
1441 status_type, status),
1442 build_int_cst (status_type, 1));
1443 error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1444 cond2, tmp, error);
1447 gfc_add_expr_to_block (&null, error);
1449 /* When POINTER is not NULL, we free it. */
1450 gfc_start_block (&non_null);
1452 /* Free allocatable components. */
1453 finalizable = gfc_add_finalizer_call (&non_null, expr);
1454 if (!finalizable && ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp)
1456 tmp = build_fold_indirect_ref_loc (input_location, pointer);
1457 tmp = gfc_deallocate_alloc_comp (ts.u.derived, tmp, 0);
1458 gfc_add_expr_to_block (&non_null, tmp);
1461 tmp = build_call_expr_loc (input_location,
1462 builtin_decl_explicit (BUILT_IN_FREE), 1,
1463 fold_convert (pvoid_type_node, pointer));
1464 gfc_add_expr_to_block (&non_null, tmp);
1466 if (status != NULL_TREE && !integer_zerop (status))
1468 /* We set STATUS to zero if it is present. */
1469 tree status_type = TREE_TYPE (TREE_TYPE (status));
1470 tree cond2;
1472 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1473 status, build_int_cst (TREE_TYPE (status), 0));
1474 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
1475 fold_build1_loc (input_location, INDIRECT_REF,
1476 status_type, status),
1477 build_int_cst (status_type, 0));
1478 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
1479 tmp, build_empty_stmt (input_location));
1480 gfc_add_expr_to_block (&non_null, tmp);
1483 return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
1484 gfc_finish_block (&null),
1485 gfc_finish_block (&non_null));
1489 /* Reallocate MEM so it has SIZE bytes of data. This behaves like the
1490 following pseudo-code:
1492 void *
1493 internal_realloc (void *mem, size_t size)
1495 res = realloc (mem, size);
1496 if (!res && size != 0)
1497 _gfortran_os_error ("Allocation would exceed memory limit");
1499 return res;
1500 } */
1501 tree
1502 gfc_call_realloc (stmtblock_t * block, tree mem, tree size)
1504 tree msg, res, nonzero, null_result, tmp;
1505 tree type = TREE_TYPE (mem);
1507 /* Only evaluate the size once. */
1508 size = save_expr (fold_convert (size_type_node, size));
1510 /* Create a variable to hold the result. */
1511 res = gfc_create_var (type, NULL);
1513 /* Call realloc and check the result. */
1514 tmp = build_call_expr_loc (input_location,
1515 builtin_decl_explicit (BUILT_IN_REALLOC), 2,
1516 fold_convert (pvoid_type_node, mem), size);
1517 gfc_add_modify (block, res, fold_convert (type, tmp));
1518 null_result = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
1519 res, build_int_cst (pvoid_type_node, 0));
1520 nonzero = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, size,
1521 build_int_cst (size_type_node, 0));
1522 null_result = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
1523 null_result, nonzero);
1524 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
1525 ("Allocation would exceed memory limit"));
1526 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1527 null_result,
1528 build_call_expr_loc (input_location,
1529 gfor_fndecl_os_error, 1, msg),
1530 build_empty_stmt (input_location));
1531 gfc_add_expr_to_block (block, tmp);
1533 return res;
1537 /* Add an expression to another one, either at the front or the back. */
1539 static void
1540 add_expr_to_chain (tree* chain, tree expr, bool front)
1542 if (expr == NULL_TREE || IS_EMPTY_STMT (expr))
1543 return;
1545 if (*chain)
1547 if (TREE_CODE (*chain) != STATEMENT_LIST)
1549 tree tmp;
1551 tmp = *chain;
1552 *chain = NULL_TREE;
1553 append_to_statement_list (tmp, chain);
1556 if (front)
1558 tree_stmt_iterator i;
1560 i = tsi_start (*chain);
1561 tsi_link_before (&i, expr, TSI_CONTINUE_LINKING);
1563 else
1564 append_to_statement_list (expr, chain);
1566 else
1567 *chain = expr;
1571 /* Add a statement at the end of a block. */
1573 void
1574 gfc_add_expr_to_block (stmtblock_t * block, tree expr)
1576 gcc_assert (block);
1577 add_expr_to_chain (&block->head, expr, false);
1581 /* Add a statement at the beginning of a block. */
1583 void
1584 gfc_prepend_expr_to_block (stmtblock_t * block, tree expr)
1586 gcc_assert (block);
1587 add_expr_to_chain (&block->head, expr, true);
1591 /* Add a block the end of a block. */
1593 void
1594 gfc_add_block_to_block (stmtblock_t * block, stmtblock_t * append)
1596 gcc_assert (append);
1597 gcc_assert (!append->has_scope);
1599 gfc_add_expr_to_block (block, append->head);
1600 append->head = NULL_TREE;
1604 /* Save the current locus. The structure may not be complete, and should
1605 only be used with gfc_restore_backend_locus. */
1607 void
1608 gfc_save_backend_locus (locus * loc)
1610 loc->lb = XCNEW (gfc_linebuf);
1611 loc->lb->location = input_location;
1612 loc->lb->file = gfc_current_backend_file;
1616 /* Set the current locus. */
1618 void
1619 gfc_set_backend_locus (locus * loc)
1621 gfc_current_backend_file = loc->lb->file;
1622 input_location = loc->lb->location;
1626 /* Restore the saved locus. Only used in conjunction with
1627 gfc_save_backend_locus, to free the memory when we are done. */
1629 void
1630 gfc_restore_backend_locus (locus * loc)
1632 gfc_set_backend_locus (loc);
1633 free (loc->lb);
1637 /* Translate an executable statement. The tree cond is used by gfc_trans_do.
1638 This static function is wrapped by gfc_trans_code_cond and
1639 gfc_trans_code. */
1641 static tree
1642 trans_code (gfc_code * code, tree cond)
1644 stmtblock_t block;
1645 tree res;
1647 if (!code)
1648 return build_empty_stmt (input_location);
1650 gfc_start_block (&block);
1652 /* Translate statements one by one into GENERIC trees until we reach
1653 the end of this gfc_code branch. */
1654 for (; code; code = code->next)
1656 if (code->here != 0)
1658 res = gfc_trans_label_here (code);
1659 gfc_add_expr_to_block (&block, res);
1662 gfc_current_locus = code->loc;
1663 gfc_set_backend_locus (&code->loc);
1665 switch (code->op)
1667 case EXEC_NOP:
1668 case EXEC_END_BLOCK:
1669 case EXEC_END_NESTED_BLOCK:
1670 case EXEC_END_PROCEDURE:
1671 res = NULL_TREE;
1672 break;
1674 case EXEC_ASSIGN:
1675 if (code->expr1->ts.type == BT_CLASS)
1676 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1677 else
1678 res = gfc_trans_assign (code);
1679 break;
1681 case EXEC_LABEL_ASSIGN:
1682 res = gfc_trans_label_assign (code);
1683 break;
1685 case EXEC_POINTER_ASSIGN:
1686 if (code->expr1->ts.type == BT_CLASS)
1687 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1688 else if (UNLIMITED_POLY (code->expr2)
1689 && code->expr1->ts.type == BT_DERIVED
1690 && (code->expr1->ts.u.derived->attr.sequence
1691 || code->expr1->ts.u.derived->attr.is_bind_c))
1692 /* F2003: C717 */
1693 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1694 else
1695 res = gfc_trans_pointer_assign (code);
1696 break;
1698 case EXEC_INIT_ASSIGN:
1699 if (code->expr1->ts.type == BT_CLASS)
1700 res = gfc_trans_class_init_assign (code);
1701 else
1702 res = gfc_trans_init_assign (code);
1703 break;
1705 case EXEC_CONTINUE:
1706 res = NULL_TREE;
1707 break;
1709 case EXEC_CRITICAL:
1710 res = gfc_trans_critical (code);
1711 break;
1713 case EXEC_CYCLE:
1714 res = gfc_trans_cycle (code);
1715 break;
1717 case EXEC_EXIT:
1718 res = gfc_trans_exit (code);
1719 break;
1721 case EXEC_GOTO:
1722 res = gfc_trans_goto (code);
1723 break;
1725 case EXEC_ENTRY:
1726 res = gfc_trans_entry (code);
1727 break;
1729 case EXEC_PAUSE:
1730 res = gfc_trans_pause (code);
1731 break;
1733 case EXEC_STOP:
1734 case EXEC_ERROR_STOP:
1735 res = gfc_trans_stop (code, code->op == EXEC_ERROR_STOP);
1736 break;
1738 case EXEC_CALL:
1739 /* For MVBITS we've got the special exception that we need a
1740 dependency check, too. */
1742 bool is_mvbits = false;
1744 if (code->resolved_isym)
1746 res = gfc_conv_intrinsic_subroutine (code);
1747 if (res != NULL_TREE)
1748 break;
1751 if (code->resolved_isym
1752 && code->resolved_isym->id == GFC_ISYM_MVBITS)
1753 is_mvbits = true;
1755 res = gfc_trans_call (code, is_mvbits, NULL_TREE,
1756 NULL_TREE, false);
1758 break;
1760 case EXEC_CALL_PPC:
1761 res = gfc_trans_call (code, false, NULL_TREE,
1762 NULL_TREE, false);
1763 break;
1765 case EXEC_ASSIGN_CALL:
1766 res = gfc_trans_call (code, true, NULL_TREE,
1767 NULL_TREE, false);
1768 break;
1770 case EXEC_RETURN:
1771 res = gfc_trans_return (code);
1772 break;
1774 case EXEC_IF:
1775 res = gfc_trans_if (code);
1776 break;
1778 case EXEC_ARITHMETIC_IF:
1779 res = gfc_trans_arithmetic_if (code);
1780 break;
1782 case EXEC_BLOCK:
1783 res = gfc_trans_block_construct (code);
1784 break;
1786 case EXEC_DO:
1787 res = gfc_trans_do (code, cond);
1788 break;
1790 case EXEC_DO_CONCURRENT:
1791 res = gfc_trans_do_concurrent (code);
1792 break;
1794 case EXEC_DO_WHILE:
1795 res = gfc_trans_do_while (code);
1796 break;
1798 case EXEC_SELECT:
1799 res = gfc_trans_select (code);
1800 break;
1802 case EXEC_SELECT_TYPE:
1803 /* Do nothing. SELECT TYPE statements should be transformed into
1804 an ordinary SELECT CASE at resolution stage.
1805 TODO: Add an error message here once this is done. */
1806 res = NULL_TREE;
1807 break;
1809 case EXEC_FLUSH:
1810 res = gfc_trans_flush (code);
1811 break;
1813 case EXEC_SYNC_ALL:
1814 case EXEC_SYNC_IMAGES:
1815 case EXEC_SYNC_MEMORY:
1816 res = gfc_trans_sync (code, code->op);
1817 break;
1819 case EXEC_LOCK:
1820 case EXEC_UNLOCK:
1821 res = gfc_trans_lock_unlock (code, code->op);
1822 break;
1824 case EXEC_EVENT_POST:
1825 case EXEC_EVENT_WAIT:
1826 res = gfc_trans_event_post_wait (code, code->op);
1827 break;
1829 case EXEC_FORALL:
1830 res = gfc_trans_forall (code);
1831 break;
1833 case EXEC_WHERE:
1834 res = gfc_trans_where (code);
1835 break;
1837 case EXEC_ALLOCATE:
1838 res = gfc_trans_allocate (code);
1839 break;
1841 case EXEC_DEALLOCATE:
1842 res = gfc_trans_deallocate (code);
1843 break;
1845 case EXEC_OPEN:
1846 res = gfc_trans_open (code);
1847 break;
1849 case EXEC_CLOSE:
1850 res = gfc_trans_close (code);
1851 break;
1853 case EXEC_READ:
1854 res = gfc_trans_read (code);
1855 break;
1857 case EXEC_WRITE:
1858 res = gfc_trans_write (code);
1859 break;
1861 case EXEC_IOLENGTH:
1862 res = gfc_trans_iolength (code);
1863 break;
1865 case EXEC_BACKSPACE:
1866 res = gfc_trans_backspace (code);
1867 break;
1869 case EXEC_ENDFILE:
1870 res = gfc_trans_endfile (code);
1871 break;
1873 case EXEC_INQUIRE:
1874 res = gfc_trans_inquire (code);
1875 break;
1877 case EXEC_WAIT:
1878 res = gfc_trans_wait (code);
1879 break;
1881 case EXEC_REWIND:
1882 res = gfc_trans_rewind (code);
1883 break;
1885 case EXEC_TRANSFER:
1886 res = gfc_trans_transfer (code);
1887 break;
1889 case EXEC_DT_END:
1890 res = gfc_trans_dt_end (code);
1891 break;
1893 case EXEC_OMP_ATOMIC:
1894 case EXEC_OMP_BARRIER:
1895 case EXEC_OMP_CANCEL:
1896 case EXEC_OMP_CANCELLATION_POINT:
1897 case EXEC_OMP_CRITICAL:
1898 case EXEC_OMP_DISTRIBUTE:
1899 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
1900 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
1901 case EXEC_OMP_DISTRIBUTE_SIMD:
1902 case EXEC_OMP_DO:
1903 case EXEC_OMP_DO_SIMD:
1904 case EXEC_OMP_FLUSH:
1905 case EXEC_OMP_MASTER:
1906 case EXEC_OMP_ORDERED:
1907 case EXEC_OMP_PARALLEL:
1908 case EXEC_OMP_PARALLEL_DO:
1909 case EXEC_OMP_PARALLEL_DO_SIMD:
1910 case EXEC_OMP_PARALLEL_SECTIONS:
1911 case EXEC_OMP_PARALLEL_WORKSHARE:
1912 case EXEC_OMP_SECTIONS:
1913 case EXEC_OMP_SIMD:
1914 case EXEC_OMP_SINGLE:
1915 case EXEC_OMP_TARGET:
1916 case EXEC_OMP_TARGET_DATA:
1917 case EXEC_OMP_TARGET_TEAMS:
1918 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
1919 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
1920 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
1921 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
1922 case EXEC_OMP_TARGET_UPDATE:
1923 case EXEC_OMP_TASK:
1924 case EXEC_OMP_TASKGROUP:
1925 case EXEC_OMP_TASKWAIT:
1926 case EXEC_OMP_TASKYIELD:
1927 case EXEC_OMP_TEAMS:
1928 case EXEC_OMP_TEAMS_DISTRIBUTE:
1929 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
1930 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
1931 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
1932 case EXEC_OMP_WORKSHARE:
1933 res = gfc_trans_omp_directive (code);
1934 break;
1936 case EXEC_OACC_CACHE:
1937 case EXEC_OACC_WAIT:
1938 case EXEC_OACC_UPDATE:
1939 case EXEC_OACC_LOOP:
1940 case EXEC_OACC_HOST_DATA:
1941 case EXEC_OACC_DATA:
1942 case EXEC_OACC_KERNELS:
1943 case EXEC_OACC_KERNELS_LOOP:
1944 case EXEC_OACC_PARALLEL:
1945 case EXEC_OACC_PARALLEL_LOOP:
1946 case EXEC_OACC_ENTER_DATA:
1947 case EXEC_OACC_EXIT_DATA:
1948 case EXEC_OACC_ATOMIC:
1949 case EXEC_OACC_DECLARE:
1950 res = gfc_trans_oacc_directive (code);
1951 break;
1953 default:
1954 gfc_internal_error ("gfc_trans_code(): Bad statement code");
1957 gfc_set_backend_locus (&code->loc);
1959 if (res != NULL_TREE && ! IS_EMPTY_STMT (res))
1961 if (TREE_CODE (res) != STATEMENT_LIST)
1962 SET_EXPR_LOCATION (res, input_location);
1964 /* Add the new statement to the block. */
1965 gfc_add_expr_to_block (&block, res);
1969 /* Return the finished block. */
1970 return gfc_finish_block (&block);
1974 /* Translate an executable statement with condition, cond. The condition is
1975 used by gfc_trans_do to test for IO result conditions inside implied
1976 DO loops of READ and WRITE statements. See build_dt in trans-io.c. */
1978 tree
1979 gfc_trans_code_cond (gfc_code * code, tree cond)
1981 return trans_code (code, cond);
1984 /* Translate an executable statement without condition. */
1986 tree
1987 gfc_trans_code (gfc_code * code)
1989 return trans_code (code, NULL_TREE);
1993 /* This function is called after a complete program unit has been parsed
1994 and resolved. */
1996 void
1997 gfc_generate_code (gfc_namespace * ns)
1999 ompws_flags = 0;
2000 if (ns->is_block_data)
2002 gfc_generate_block_data (ns);
2003 return;
2006 gfc_generate_function_code (ns);
2010 /* This function is called after a complete module has been parsed
2011 and resolved. */
2013 void
2014 gfc_generate_module_code (gfc_namespace * ns)
2016 gfc_namespace *n;
2017 struct module_htab_entry *entry;
2019 gcc_assert (ns->proc_name->backend_decl == NULL);
2020 ns->proc_name->backend_decl
2021 = build_decl (ns->proc_name->declared_at.lb->location,
2022 NAMESPACE_DECL, get_identifier (ns->proc_name->name),
2023 void_type_node);
2024 entry = gfc_find_module (ns->proc_name->name);
2025 if (entry->namespace_decl)
2026 /* Buggy sourcecode, using a module before defining it? */
2027 entry->decls->empty ();
2028 entry->namespace_decl = ns->proc_name->backend_decl;
2030 gfc_generate_module_vars (ns);
2032 /* We need to generate all module function prototypes first, to allow
2033 sibling calls. */
2034 for (n = ns->contained; n; n = n->sibling)
2036 gfc_entry_list *el;
2038 if (!n->proc_name)
2039 continue;
2041 gfc_create_function_decl (n, false);
2042 DECL_CONTEXT (n->proc_name->backend_decl) = ns->proc_name->backend_decl;
2043 gfc_module_add_decl (entry, n->proc_name->backend_decl);
2044 for (el = ns->entries; el; el = el->next)
2046 DECL_CONTEXT (el->sym->backend_decl) = ns->proc_name->backend_decl;
2047 gfc_module_add_decl (entry, el->sym->backend_decl);
2051 for (n = ns->contained; n; n = n->sibling)
2053 if (!n->proc_name)
2054 continue;
2056 gfc_generate_function_code (n);
2061 /* Initialize an init/cleanup block with existing code. */
2063 void
2064 gfc_start_wrapped_block (gfc_wrapped_block* block, tree code)
2066 gcc_assert (block);
2068 block->init = NULL_TREE;
2069 block->code = code;
2070 block->cleanup = NULL_TREE;
2074 /* Add a new pair of initializers/clean-up code. */
2076 void
2077 gfc_add_init_cleanup (gfc_wrapped_block* block, tree init, tree cleanup)
2079 gcc_assert (block);
2081 /* The new pair of init/cleanup should be "wrapped around" the existing
2082 block of code, thus the initialization is added to the front and the
2083 cleanup to the back. */
2084 add_expr_to_chain (&block->init, init, true);
2085 add_expr_to_chain (&block->cleanup, cleanup, false);
2089 /* Finish up a wrapped block by building a corresponding try-finally expr. */
2091 tree
2092 gfc_finish_wrapped_block (gfc_wrapped_block* block)
2094 tree result;
2096 gcc_assert (block);
2098 /* Build the final expression. For this, just add init and body together,
2099 and put clean-up with that into a TRY_FINALLY_EXPR. */
2100 result = block->init;
2101 add_expr_to_chain (&result, block->code, false);
2102 if (block->cleanup)
2103 result = build2_loc (input_location, TRY_FINALLY_EXPR, void_type_node,
2104 result, block->cleanup);
2106 /* Clear the block. */
2107 block->init = NULL_TREE;
2108 block->code = NULL_TREE;
2109 block->cleanup = NULL_TREE;
2111 return result;
2115 /* Helper function for marking a boolean expression tree as unlikely. */
2117 tree
2118 gfc_unlikely (tree cond, enum br_predictor predictor)
2120 tree tmp;
2122 if (optimize)
2124 cond = fold_convert (long_integer_type_node, cond);
2125 tmp = build_zero_cst (long_integer_type_node);
2126 cond = build_call_expr_loc (input_location,
2127 builtin_decl_explicit (BUILT_IN_EXPECT),
2128 3, cond, tmp,
2129 build_int_cst (integer_type_node,
2130 predictor));
2132 cond = fold_convert (boolean_type_node, cond);
2133 return cond;
2137 /* Helper function for marking a boolean expression tree as likely. */
2139 tree
2140 gfc_likely (tree cond, enum br_predictor predictor)
2142 tree tmp;
2144 if (optimize)
2146 cond = fold_convert (long_integer_type_node, cond);
2147 tmp = build_one_cst (long_integer_type_node);
2148 cond = build_call_expr_loc (input_location,
2149 builtin_decl_explicit (BUILT_IN_EXPECT),
2150 3, cond, tmp,
2151 build_int_cst (integer_type_node,
2152 predictor));
2154 cond = fold_convert (boolean_type_node, cond);
2155 return cond;
2159 /* Get the string length for a deferred character length component. */
2161 bool
2162 gfc_deferred_strlen (gfc_component *c, tree *decl)
2164 char name[GFC_MAX_SYMBOL_LEN+9];
2165 gfc_component *strlen;
2166 if (!(c->ts.type == BT_CHARACTER && c->ts.deferred))
2167 return false;
2168 sprintf (name, "_%s_length", c->name);
2169 for (strlen = c; strlen; strlen = strlen->next)
2170 if (strcmp (strlen->name, name) == 0)
2171 break;
2172 *decl = strlen ? strlen->backend_decl : NULL_TREE;
2173 return strlen != NULL;