Reverting merge from trunk
[official-gcc.git] / gcc / fortran / trans.c
blobef20a20f0182cd227d233fdd1fa0dc49d2143d9b
1 /* Code translation -- generate GCC trees from gfc_code.
2 Copyright (C) 2002-2013 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 "tree.h"
25 #include "gimple-expr.h" /* For create_tmp_var_raw. */
26 #include "tree-iterator.h"
27 #include "diagnostic-core.h" /* For internal_error. */
28 #include "flags.h"
29 #include "gfortran.h"
30 #include "trans.h"
31 #include "trans-stmt.h"
32 #include "trans-array.h"
33 #include "trans-types.h"
34 #include "trans-const.h"
36 /* Naming convention for backend interface code:
38 gfc_trans_* translate gfc_code into STMT trees.
40 gfc_conv_* expression conversion
42 gfc_get_* get a backend tree representation of a decl or type */
44 static gfc_file *gfc_current_backend_file;
46 const char gfc_msg_fault[] = N_("Array reference out of bounds");
47 const char gfc_msg_wrong_return[] = N_("Incorrect function return value");
50 /* Advance along TREE_CHAIN n times. */
52 tree
53 gfc_advance_chain (tree t, int n)
55 for (; n > 0; n--)
57 gcc_assert (t != NULL_TREE);
58 t = DECL_CHAIN (t);
60 return t;
64 /* Strip off a legitimate source ending from the input
65 string NAME of length LEN. */
67 static inline void
68 remove_suffix (char *name, int len)
70 int i;
72 for (i = 2; i < 8 && len > i; i++)
74 if (name[len - i] == '.')
76 name[len - i] = '\0';
77 break;
83 /* Creates a variable declaration with a given TYPE. */
85 tree
86 gfc_create_var_np (tree type, const char *prefix)
88 tree t;
90 t = create_tmp_var_raw (type, prefix);
92 /* No warnings for anonymous variables. */
93 if (prefix == NULL)
94 TREE_NO_WARNING (t) = 1;
96 return t;
100 /* Like above, but also adds it to the current scope. */
102 tree
103 gfc_create_var (tree type, const char *prefix)
105 tree tmp;
107 tmp = gfc_create_var_np (type, prefix);
109 pushdecl (tmp);
111 return tmp;
115 /* If the expression is not constant, evaluate it now. We assign the
116 result of the expression to an artificially created variable VAR, and
117 return a pointer to the VAR_DECL node for this variable. */
119 tree
120 gfc_evaluate_now_loc (location_t loc, tree expr, stmtblock_t * pblock)
122 tree var;
124 if (CONSTANT_CLASS_P (expr))
125 return expr;
127 var = gfc_create_var (TREE_TYPE (expr), NULL);
128 gfc_add_modify_loc (loc, pblock, var, expr);
130 return var;
134 tree
135 gfc_evaluate_now (tree expr, stmtblock_t * pblock)
137 return gfc_evaluate_now_loc (input_location, expr, pblock);
141 /* Build a MODIFY_EXPR node and add it to a given statement block PBLOCK.
142 A MODIFY_EXPR is an assignment:
143 LHS <- RHS. */
145 void
146 gfc_add_modify_loc (location_t loc, stmtblock_t * pblock, tree lhs, tree rhs)
148 tree tmp;
150 #ifdef ENABLE_CHECKING
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_assert (t1 == t2
159 || AGGREGATE_TYPE_P (TREE_TYPE (lhs)));
160 #endif
162 tmp = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, lhs,
163 rhs);
164 gfc_add_expr_to_block (pblock, tmp);
168 void
169 gfc_add_modify (stmtblock_t * pblock, tree lhs, tree rhs)
171 gfc_add_modify_loc (input_location, pblock, lhs, rhs);
175 /* Create a new scope/binding level and initialize a block. Care must be
176 taken when translating expressions as any temporaries will be placed in
177 the innermost scope. */
179 void
180 gfc_start_block (stmtblock_t * block)
182 /* Start a new binding level. */
183 pushlevel ();
184 block->has_scope = 1;
186 /* The block is empty. */
187 block->head = NULL_TREE;
191 /* Initialize a block without creating a new scope. */
193 void
194 gfc_init_block (stmtblock_t * block)
196 block->head = NULL_TREE;
197 block->has_scope = 0;
201 /* Sometimes we create a scope but it turns out that we don't actually
202 need it. This function merges the scope of BLOCK with its parent.
203 Only variable decls will be merged, you still need to add the code. */
205 void
206 gfc_merge_block_scope (stmtblock_t * block)
208 tree decl;
209 tree next;
211 gcc_assert (block->has_scope);
212 block->has_scope = 0;
214 /* Remember the decls in this scope. */
215 decl = getdecls ();
216 poplevel (0, 0);
218 /* Add them to the parent scope. */
219 while (decl != NULL_TREE)
221 next = DECL_CHAIN (decl);
222 DECL_CHAIN (decl) = NULL_TREE;
224 pushdecl (decl);
225 decl = next;
230 /* Finish a scope containing a block of statements. */
232 tree
233 gfc_finish_block (stmtblock_t * stmtblock)
235 tree decl;
236 tree expr;
237 tree block;
239 expr = stmtblock->head;
240 if (!expr)
241 expr = build_empty_stmt (input_location);
243 stmtblock->head = NULL_TREE;
245 if (stmtblock->has_scope)
247 decl = getdecls ();
249 if (decl)
251 block = poplevel (1, 0);
252 expr = build3_v (BIND_EXPR, decl, expr, block);
254 else
255 poplevel (0, 0);
258 return expr;
262 /* Build an ADDR_EXPR and cast the result to TYPE. If TYPE is NULL, the
263 natural type is used. */
265 tree
266 gfc_build_addr_expr (tree type, tree t)
268 tree base_type = TREE_TYPE (t);
269 tree natural_type;
271 if (type && POINTER_TYPE_P (type)
272 && TREE_CODE (base_type) == ARRAY_TYPE
273 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
274 == TYPE_MAIN_VARIANT (TREE_TYPE (base_type)))
276 tree min_val = size_zero_node;
277 tree type_domain = TYPE_DOMAIN (base_type);
278 if (type_domain && TYPE_MIN_VALUE (type_domain))
279 min_val = TYPE_MIN_VALUE (type_domain);
280 t = fold (build4_loc (input_location, ARRAY_REF, TREE_TYPE (type),
281 t, min_val, NULL_TREE, NULL_TREE));
282 natural_type = type;
284 else
285 natural_type = build_pointer_type (base_type);
287 if (TREE_CODE (t) == INDIRECT_REF)
289 if (!type)
290 type = natural_type;
291 t = TREE_OPERAND (t, 0);
292 natural_type = TREE_TYPE (t);
294 else
296 tree base = get_base_address (t);
297 if (base && DECL_P (base))
298 TREE_ADDRESSABLE (base) = 1;
299 t = fold_build1_loc (input_location, ADDR_EXPR, natural_type, t);
302 if (type && natural_type != type)
303 t = convert (type, t);
305 return t;
309 /* Build an ARRAY_REF with its natural type. */
311 tree
312 gfc_build_array_ref (tree base, tree offset, tree decl)
314 tree type = TREE_TYPE (base);
315 tree tmp;
316 tree span;
318 if (GFC_ARRAY_TYPE_P (type) && GFC_TYPE_ARRAY_RANK (type) == 0)
320 gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
322 return fold_convert (TYPE_MAIN_VARIANT (type), base);
325 /* Scalar coarray, there is nothing to do. */
326 if (TREE_CODE (type) != ARRAY_TYPE)
328 gcc_assert (decl == NULL_TREE);
329 gcc_assert (integer_zerop (offset));
330 return base;
333 type = TREE_TYPE (type);
335 if (DECL_P (base))
336 TREE_ADDRESSABLE (base) = 1;
338 /* Strip NON_LVALUE_EXPR nodes. */
339 STRIP_TYPE_NOPS (offset);
341 /* If the array reference is to a pointer, whose target contains a
342 subreference, use the span that is stored with the backend decl
343 and reference the element with pointer arithmetic. */
344 if (decl && (TREE_CODE (decl) == FIELD_DECL
345 || TREE_CODE (decl) == VAR_DECL
346 || TREE_CODE (decl) == PARM_DECL)
347 && ((GFC_DECL_SUBREF_ARRAY_P (decl)
348 && !integer_zerop (GFC_DECL_SPAN(decl)))
349 || GFC_DECL_CLASS (decl)))
351 if (GFC_DECL_CLASS (decl))
353 /* Allow for dummy arguments and other good things. */
354 if (POINTER_TYPE_P (TREE_TYPE (decl)))
355 decl = build_fold_indirect_ref_loc (input_location, decl);
357 /* Check if '_data' is an array descriptor. If it is not,
358 the array must be one of the components of the class object,
359 so return a normal array reference. */
360 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_class_data_get (decl))))
361 return build4_loc (input_location, ARRAY_REF, type, base,
362 offset, NULL_TREE, NULL_TREE);
364 span = gfc_vtable_size_get (decl);
366 else if (GFC_DECL_SUBREF_ARRAY_P (decl))
367 span = GFC_DECL_SPAN(decl);
368 else
369 gcc_unreachable ();
371 offset = fold_build2_loc (input_location, MULT_EXPR,
372 gfc_array_index_type,
373 offset, span);
374 tmp = gfc_build_addr_expr (pvoid_type_node, base);
375 tmp = fold_build_pointer_plus_loc (input_location, tmp, offset);
376 tmp = fold_convert (build_pointer_type (type), tmp);
377 if (!TYPE_STRING_FLAG (type))
378 tmp = build_fold_indirect_ref_loc (input_location, tmp);
379 return tmp;
381 else
382 /* Otherwise use a straightforward array reference. */
383 return build4_loc (input_location, ARRAY_REF, type, base, offset,
384 NULL_TREE, NULL_TREE);
388 /* Generate a call to print a runtime error possibly including multiple
389 arguments and a locus. */
391 static tree
392 trans_runtime_error_vararg (bool error, locus* where, const char* msgid,
393 va_list ap)
395 stmtblock_t block;
396 tree tmp;
397 tree arg, arg2;
398 tree *argarray;
399 tree fntype;
400 char *message;
401 const char *p;
402 int line, nargs, i;
403 location_t loc;
405 /* Compute the number of extra arguments from the format string. */
406 for (p = msgid, nargs = 0; *p; p++)
407 if (*p == '%')
409 p++;
410 if (*p != '%')
411 nargs++;
414 /* The code to generate the error. */
415 gfc_start_block (&block);
417 if (where)
419 line = LOCATION_LINE (where->lb->location);
420 asprintf (&message, "At line %d of file %s", line,
421 where->lb->file->filename);
423 else
424 asprintf (&message, "In file '%s', around line %d",
425 gfc_source_file, input_line + 1);
427 arg = gfc_build_addr_expr (pchar_type_node,
428 gfc_build_localized_cstring_const (message));
429 free (message);
431 asprintf (&message, "%s", _(msgid));
432 arg2 = gfc_build_addr_expr (pchar_type_node,
433 gfc_build_localized_cstring_const (message));
434 free (message);
436 /* Build the argument array. */
437 argarray = XALLOCAVEC (tree, nargs + 2);
438 argarray[0] = arg;
439 argarray[1] = arg2;
440 for (i = 0; i < nargs; i++)
441 argarray[2 + i] = va_arg (ap, tree);
443 /* Build the function call to runtime_(warning,error)_at; because of the
444 variable number of arguments, we can't use build_call_expr_loc dinput_location,
445 irectly. */
446 if (error)
447 fntype = TREE_TYPE (gfor_fndecl_runtime_error_at);
448 else
449 fntype = TREE_TYPE (gfor_fndecl_runtime_warning_at);
451 loc = where ? where->lb->location : input_location;
452 tmp = fold_builtin_call_array (loc, TREE_TYPE (fntype),
453 fold_build1_loc (loc, ADDR_EXPR,
454 build_pointer_type (fntype),
455 error
456 ? gfor_fndecl_runtime_error_at
457 : gfor_fndecl_runtime_warning_at),
458 nargs + 2, argarray);
459 gfc_add_expr_to_block (&block, tmp);
461 return gfc_finish_block (&block);
465 tree
466 gfc_trans_runtime_error (bool error, locus* where, const char* msgid, ...)
468 va_list ap;
469 tree result;
471 va_start (ap, msgid);
472 result = trans_runtime_error_vararg (error, where, msgid, ap);
473 va_end (ap);
474 return result;
478 /* Generate a runtime error if COND is true. */
480 void
481 gfc_trans_runtime_check (bool error, bool once, tree cond, stmtblock_t * pblock,
482 locus * where, const char * msgid, ...)
484 va_list ap;
485 stmtblock_t block;
486 tree body;
487 tree tmp;
488 tree tmpvar = NULL;
490 if (integer_zerop (cond))
491 return;
493 if (once)
495 tmpvar = gfc_create_var (boolean_type_node, "print_warning");
496 TREE_STATIC (tmpvar) = 1;
497 DECL_INITIAL (tmpvar) = boolean_true_node;
498 gfc_add_expr_to_block (pblock, tmpvar);
501 gfc_start_block (&block);
503 /* The code to generate the error. */
504 va_start (ap, msgid);
505 gfc_add_expr_to_block (&block,
506 trans_runtime_error_vararg (error, where,
507 msgid, ap));
508 va_end (ap);
510 if (once)
511 gfc_add_modify (&block, tmpvar, boolean_false_node);
513 body = gfc_finish_block (&block);
515 if (integer_onep (cond))
517 gfc_add_expr_to_block (pblock, body);
519 else
521 /* Tell the compiler that this isn't likely. */
522 if (once)
523 cond = fold_build2_loc (where->lb->location, TRUTH_AND_EXPR,
524 long_integer_type_node, tmpvar, cond);
525 else
526 cond = fold_convert (long_integer_type_node, cond);
528 cond = gfc_unlikely (cond);
529 tmp = fold_build3_loc (where->lb->location, COND_EXPR, void_type_node,
530 cond, body,
531 build_empty_stmt (where->lb->location));
532 gfc_add_expr_to_block (pblock, tmp);
537 /* Call malloc to allocate size bytes of memory, with special conditions:
538 + if size == 0, return a malloced area of size 1,
539 + if malloc returns NULL, issue a runtime error. */
540 tree
541 gfc_call_malloc (stmtblock_t * block, tree type, tree size)
543 tree tmp, msg, malloc_result, null_result, res, malloc_tree;
544 stmtblock_t block2;
546 size = gfc_evaluate_now (size, block);
548 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
549 size = fold_convert (size_type_node, size);
551 /* Create a variable to hold the result. */
552 res = gfc_create_var (prvoid_type_node, NULL);
554 /* Call malloc. */
555 gfc_start_block (&block2);
557 size = fold_build2_loc (input_location, MAX_EXPR, size_type_node, size,
558 build_int_cst (size_type_node, 1));
560 malloc_tree = builtin_decl_explicit (BUILT_IN_MALLOC);
561 gfc_add_modify (&block2, res,
562 fold_convert (prvoid_type_node,
563 build_call_expr_loc (input_location,
564 malloc_tree, 1, size)));
566 /* Optionally check whether malloc was successful. */
567 if (gfc_option.rtcheck & GFC_RTCHECK_MEM)
569 null_result = fold_build2_loc (input_location, EQ_EXPR,
570 boolean_type_node, res,
571 build_int_cst (pvoid_type_node, 0));
572 msg = gfc_build_addr_expr (pchar_type_node,
573 gfc_build_localized_cstring_const ("Memory allocation failed"));
574 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
575 null_result,
576 build_call_expr_loc (input_location,
577 gfor_fndecl_os_error, 1, msg),
578 build_empty_stmt (input_location));
579 gfc_add_expr_to_block (&block2, tmp);
582 malloc_result = gfc_finish_block (&block2);
584 gfc_add_expr_to_block (block, malloc_result);
586 if (type != NULL)
587 res = fold_convert (type, res);
588 return res;
592 /* Allocate memory, using an optional status argument.
594 This function follows the following pseudo-code:
596 void *
597 allocate (size_t size, integer_type stat)
599 void *newmem;
601 if (stat requested)
602 stat = 0;
604 newmem = malloc (MAX (size, 1));
605 if (newmem == NULL)
607 if (stat)
608 *stat = LIBERROR_ALLOCATION;
609 else
610 runtime_error ("Allocation would exceed memory limit");
612 return newmem;
613 } */
614 void
615 gfc_allocate_using_malloc (stmtblock_t * block, tree pointer,
616 tree size, tree status)
618 tree tmp, on_error, error_cond;
619 tree status_type = status ? TREE_TYPE (status) : NULL_TREE;
621 /* Evaluate size only once, and make sure it has the right type. */
622 size = gfc_evaluate_now (size, block);
623 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
624 size = fold_convert (size_type_node, size);
626 /* If successful and stat= is given, set status to 0. */
627 if (status != NULL_TREE)
628 gfc_add_expr_to_block (block,
629 fold_build2_loc (input_location, MODIFY_EXPR, status_type,
630 status, build_int_cst (status_type, 0)));
632 /* The allocation itself. */
633 gfc_add_modify (block, pointer,
634 fold_convert (TREE_TYPE (pointer),
635 build_call_expr_loc (input_location,
636 builtin_decl_explicit (BUILT_IN_MALLOC), 1,
637 fold_build2_loc (input_location,
638 MAX_EXPR, size_type_node, size,
639 build_int_cst (size_type_node, 1)))));
641 /* What to do in case of error. */
642 if (status != NULL_TREE)
643 on_error = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
644 status, build_int_cst (status_type, LIBERROR_ALLOCATION));
645 else
646 on_error = build_call_expr_loc (input_location, gfor_fndecl_os_error, 1,
647 gfc_build_addr_expr (pchar_type_node,
648 gfc_build_localized_cstring_const
649 ("Allocation would exceed memory limit")));
651 error_cond = fold_build2_loc (input_location, EQ_EXPR,
652 boolean_type_node, pointer,
653 build_int_cst (prvoid_type_node, 0));
654 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
655 gfc_unlikely (error_cond), on_error,
656 build_empty_stmt (input_location));
658 gfc_add_expr_to_block (block, tmp);
662 /* Allocate memory, using an optional status argument.
664 This function follows the following pseudo-code:
666 void *
667 allocate (size_t size, void** token, int *stat, char* errmsg, int errlen)
669 void *newmem;
671 newmem = _caf_register (size, regtype, token, &stat, errmsg, errlen);
672 return newmem;
673 } */
674 static void
675 gfc_allocate_using_lib (stmtblock_t * block, tree pointer, tree size,
676 tree token, tree status, tree errmsg, tree errlen)
678 tree tmp, pstat;
680 gcc_assert (token != NULL_TREE);
682 /* Evaluate size only once, and make sure it has the right type. */
683 size = gfc_evaluate_now (size, block);
684 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
685 size = fold_convert (size_type_node, size);
687 /* The allocation itself. */
688 if (status == NULL_TREE)
689 pstat = null_pointer_node;
690 else
691 pstat = gfc_build_addr_expr (NULL_TREE, status);
693 if (errmsg == NULL_TREE)
695 gcc_assert(errlen == NULL_TREE);
696 errmsg = null_pointer_node;
697 errlen = build_int_cst (integer_type_node, 0);
700 tmp = build_call_expr_loc (input_location,
701 gfor_fndecl_caf_register, 6,
702 fold_build2_loc (input_location,
703 MAX_EXPR, size_type_node, size,
704 build_int_cst (size_type_node, 1)),
705 build_int_cst (integer_type_node,
706 GFC_CAF_COARRAY_ALLOC),
707 token, pstat, errmsg, errlen);
709 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
710 TREE_TYPE (pointer), pointer,
711 fold_convert ( TREE_TYPE (pointer), tmp));
712 gfc_add_expr_to_block (block, tmp);
716 /* Generate code for an ALLOCATE statement when the argument is an
717 allocatable variable. If the variable is currently allocated, it is an
718 error to allocate it again.
720 This function follows the following pseudo-code:
722 void *
723 allocate_allocatable (void *mem, size_t size, integer_type stat)
725 if (mem == NULL)
726 return allocate (size, stat);
727 else
729 if (stat)
730 stat = LIBERROR_ALLOCATION;
731 else
732 runtime_error ("Attempting to allocate already allocated variable");
736 expr must be set to the original expression being allocated for its locus
737 and variable name in case a runtime error has to be printed. */
738 void
739 gfc_allocate_allocatable (stmtblock_t * block, tree mem, tree size, tree token,
740 tree status, tree errmsg, tree errlen, tree label_finish,
741 gfc_expr* expr)
743 stmtblock_t alloc_block;
744 tree tmp, null_mem, alloc, error;
745 tree type = TREE_TYPE (mem);
747 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
748 size = fold_convert (size_type_node, size);
750 null_mem = gfc_unlikely (fold_build2_loc (input_location, NE_EXPR,
751 boolean_type_node, mem,
752 build_int_cst (type, 0)));
754 /* If mem is NULL, we call gfc_allocate_using_malloc or
755 gfc_allocate_using_lib. */
756 gfc_start_block (&alloc_block);
758 if (gfc_option.coarray == GFC_FCOARRAY_LIB
759 && gfc_expr_attr (expr).codimension)
761 tree cond;
763 gfc_allocate_using_lib (&alloc_block, mem, size, token, status,
764 errmsg, errlen);
765 if (status != NULL_TREE)
767 TREE_USED (label_finish) = 1;
768 tmp = build1_v (GOTO_EXPR, label_finish);
769 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
770 status, build_zero_cst (TREE_TYPE (status)));
771 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
772 gfc_unlikely (cond), tmp,
773 build_empty_stmt (input_location));
774 gfc_add_expr_to_block (&alloc_block, tmp);
777 else
778 gfc_allocate_using_malloc (&alloc_block, mem, size, status);
780 alloc = gfc_finish_block (&alloc_block);
782 /* If mem is not NULL, we issue a runtime error or set the
783 status variable. */
784 if (expr)
786 tree varname;
788 gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->symtree);
789 varname = gfc_build_cstring_const (expr->symtree->name);
790 varname = gfc_build_addr_expr (pchar_type_node, varname);
792 error = gfc_trans_runtime_error (true, &expr->where,
793 "Attempting to allocate already"
794 " allocated variable '%s'",
795 varname);
797 else
798 error = gfc_trans_runtime_error (true, NULL,
799 "Attempting to allocate already allocated"
800 " variable");
802 if (status != NULL_TREE)
804 tree status_type = TREE_TYPE (status);
806 error = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
807 status, build_int_cst (status_type, LIBERROR_ALLOCATION));
810 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, null_mem,
811 error, alloc);
812 gfc_add_expr_to_block (block, tmp);
816 /* Free a given variable, if it's not NULL. */
817 tree
818 gfc_call_free (tree var)
820 stmtblock_t block;
821 tree tmp, cond, call;
823 if (TREE_TYPE (var) != TREE_TYPE (pvoid_type_node))
824 var = fold_convert (pvoid_type_node, var);
826 gfc_start_block (&block);
827 var = gfc_evaluate_now (var, &block);
828 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, var,
829 build_int_cst (pvoid_type_node, 0));
830 call = build_call_expr_loc (input_location,
831 builtin_decl_explicit (BUILT_IN_FREE),
832 1, var);
833 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, call,
834 build_empty_stmt (input_location));
835 gfc_add_expr_to_block (&block, tmp);
837 return gfc_finish_block (&block);
841 /* Build a call to a FINAL procedure, which finalizes "var". */
843 static tree
844 gfc_build_final_call (gfc_typespec ts, gfc_expr *final_wrapper, gfc_expr *var,
845 bool fini_coarray, gfc_expr *class_size)
847 stmtblock_t block;
848 gfc_se se;
849 tree final_fndecl, array, size, tmp;
850 symbol_attribute attr;
852 gcc_assert (final_wrapper->expr_type == EXPR_VARIABLE);
853 gcc_assert (var);
855 gfc_start_block (&block);
856 gfc_init_se (&se, NULL);
857 gfc_conv_expr (&se, final_wrapper);
858 final_fndecl = se.expr;
859 if (POINTER_TYPE_P (TREE_TYPE (final_fndecl)))
860 final_fndecl = build_fold_indirect_ref_loc (input_location, final_fndecl);
862 if (ts.type == BT_DERIVED)
864 tree elem_size;
866 gcc_assert (!class_size);
867 elem_size = gfc_typenode_for_spec (&ts);
868 elem_size = TYPE_SIZE_UNIT (elem_size);
869 size = fold_convert (gfc_array_index_type, elem_size);
871 gfc_init_se (&se, NULL);
872 se.want_pointer = 1;
873 if (var->rank)
875 se.descriptor_only = 1;
876 gfc_conv_expr_descriptor (&se, var);
877 array = se.expr;
879 else
881 gfc_conv_expr (&se, var);
882 gcc_assert (se.pre.head == NULL_TREE && se.post.head == NULL_TREE);
883 array = se.expr;
885 /* No copy back needed, hence set attr's allocatable/pointer
886 to zero. */
887 gfc_clear_attr (&attr);
888 gfc_init_se (&se, NULL);
889 array = gfc_conv_scalar_to_descriptor (&se, array, attr);
890 gcc_assert (se.post.head == NULL_TREE);
893 else
895 gfc_expr *array_expr;
896 gcc_assert (class_size);
897 gfc_init_se (&se, NULL);
898 gfc_conv_expr (&se, class_size);
899 gfc_add_block_to_block (&block, &se.pre);
900 gcc_assert (se.post.head == NULL_TREE);
901 size = se.expr;
903 array_expr = gfc_copy_expr (var);
904 gfc_init_se (&se, NULL);
905 se.want_pointer = 1;
906 if (array_expr->rank)
908 gfc_add_class_array_ref (array_expr);
909 se.descriptor_only = 1;
910 gfc_conv_expr_descriptor (&se, array_expr);
911 array = se.expr;
913 else
915 gfc_add_data_component (array_expr);
916 gfc_conv_expr (&se, array_expr);
917 gfc_add_block_to_block (&block, &se.pre);
918 gcc_assert (se.post.head == NULL_TREE);
919 array = se.expr;
920 if (TREE_CODE (array) == ADDR_EXPR
921 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (array, 0))))
922 tmp = TREE_OPERAND (array, 0);
924 if (!gfc_is_coarray (array_expr))
926 /* No copy back needed, hence set attr's allocatable/pointer
927 to zero. */
928 gfc_clear_attr (&attr);
929 gfc_init_se (&se, NULL);
930 array = gfc_conv_scalar_to_descriptor (&se, array, attr);
932 gcc_assert (se.post.head == NULL_TREE);
934 gfc_free_expr (array_expr);
937 if (!POINTER_TYPE_P (TREE_TYPE (array)))
938 array = gfc_build_addr_expr (NULL, array);
940 gfc_add_block_to_block (&block, &se.pre);
941 tmp = build_call_expr_loc (input_location,
942 final_fndecl, 3, array,
943 size, fini_coarray ? boolean_true_node
944 : boolean_false_node);
945 gfc_add_block_to_block (&block, &se.post);
946 gfc_add_expr_to_block (&block, tmp);
947 return gfc_finish_block (&block);
951 bool
952 gfc_add_comp_finalizer_call (stmtblock_t *block, tree decl, gfc_component *comp,
953 bool fini_coarray)
955 gfc_se se;
956 stmtblock_t block2;
957 tree final_fndecl, size, array, tmp, cond;
958 symbol_attribute attr;
959 gfc_expr *final_expr = NULL;
961 if (comp->ts.type != BT_DERIVED && comp->ts.type != BT_CLASS)
962 return false;
964 gfc_init_block (&block2);
966 if (comp->ts.type == BT_DERIVED)
968 if (comp->attr.pointer)
969 return false;
971 gfc_is_finalizable (comp->ts.u.derived, &final_expr);
972 if (!final_expr)
973 return false;
975 gfc_init_se (&se, NULL);
976 gfc_conv_expr (&se, final_expr);
977 final_fndecl = se.expr;
978 size = gfc_typenode_for_spec (&comp->ts);
979 size = TYPE_SIZE_UNIT (size);
980 size = fold_convert (gfc_array_index_type, size);
982 array = decl;
984 else /* comp->ts.type == BT_CLASS. */
986 if (CLASS_DATA (comp)->attr.class_pointer)
987 return false;
989 gfc_is_finalizable (CLASS_DATA (comp)->ts.u.derived, &final_expr);
990 final_fndecl = gfc_vtable_final_get (decl);
991 size = gfc_vtable_size_get (decl);
992 array = gfc_class_data_get (decl);
995 if (comp->attr.allocatable
996 || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->attr.allocatable))
998 tmp = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array))
999 ? gfc_conv_descriptor_data_get (array) : array;
1000 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1001 tmp, fold_convert (TREE_TYPE (tmp),
1002 null_pointer_node));
1004 else
1005 cond = boolean_true_node;
1007 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array)))
1009 gfc_clear_attr (&attr);
1010 gfc_init_se (&se, NULL);
1011 array = gfc_conv_scalar_to_descriptor (&se, array, attr);
1012 gfc_add_block_to_block (&block2, &se.pre);
1013 gcc_assert (se.post.head == NULL_TREE);
1016 if (!POINTER_TYPE_P (TREE_TYPE (array)))
1017 array = gfc_build_addr_expr (NULL, array);
1019 if (!final_expr)
1021 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1022 final_fndecl,
1023 fold_convert (TREE_TYPE (final_fndecl),
1024 null_pointer_node));
1025 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1026 boolean_type_node, cond, tmp);
1029 if (POINTER_TYPE_P (TREE_TYPE (final_fndecl)))
1030 final_fndecl = build_fold_indirect_ref_loc (input_location, final_fndecl);
1032 tmp = build_call_expr_loc (input_location,
1033 final_fndecl, 3, array,
1034 size, fini_coarray ? boolean_true_node
1035 : boolean_false_node);
1036 gfc_add_expr_to_block (&block2, tmp);
1037 tmp = gfc_finish_block (&block2);
1039 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
1040 build_empty_stmt (input_location));
1041 gfc_add_expr_to_block (block, tmp);
1043 return true;
1047 /* Add a call to the finalizer, using the passed *expr. Returns
1048 true when a finalizer call has been inserted. */
1050 bool
1051 gfc_add_finalizer_call (stmtblock_t *block, gfc_expr *expr2)
1053 tree tmp;
1054 gfc_ref *ref;
1055 gfc_expr *expr;
1056 gfc_expr *final_expr = NULL;
1057 gfc_expr *elem_size = NULL;
1058 bool has_finalizer = false;
1060 if (!expr2 || (expr2->ts.type != BT_DERIVED && expr2->ts.type != BT_CLASS))
1061 return false;
1063 if (expr2->ts.type == BT_DERIVED)
1065 gfc_is_finalizable (expr2->ts.u.derived, &final_expr);
1066 if (!final_expr)
1067 return false;
1070 /* If we have a class array, we need go back to the class
1071 container. */
1072 expr = gfc_copy_expr (expr2);
1074 if (expr->ref && expr->ref->next && !expr->ref->next->next
1075 && expr->ref->next->type == REF_ARRAY
1076 && expr->ref->type == REF_COMPONENT
1077 && strcmp (expr->ref->u.c.component->name, "_data") == 0)
1079 gfc_free_ref_list (expr->ref);
1080 expr->ref = NULL;
1082 else
1083 for (ref = expr->ref; ref; ref = ref->next)
1084 if (ref->next && ref->next->next && !ref->next->next->next
1085 && ref->next->next->type == REF_ARRAY
1086 && ref->next->type == REF_COMPONENT
1087 && strcmp (ref->next->u.c.component->name, "_data") == 0)
1089 gfc_free_ref_list (ref->next);
1090 ref->next = NULL;
1093 if (expr->ts.type == BT_CLASS)
1095 has_finalizer = gfc_is_finalizable (expr->ts.u.derived, NULL);
1097 if (!expr2->rank && !expr2->ref && CLASS_DATA (expr2->symtree->n.sym)->as)
1098 expr->rank = CLASS_DATA (expr2->symtree->n.sym)->as->rank;
1100 final_expr = gfc_copy_expr (expr);
1101 gfc_add_vptr_component (final_expr);
1102 gfc_add_component_ref (final_expr, "_final");
1104 elem_size = gfc_copy_expr (expr);
1105 gfc_add_vptr_component (elem_size);
1106 gfc_add_component_ref (elem_size, "_size");
1109 gcc_assert (final_expr->expr_type == EXPR_VARIABLE);
1111 tmp = gfc_build_final_call (expr->ts, final_expr, expr,
1112 false, elem_size);
1114 if (expr->ts.type == BT_CLASS && !has_finalizer)
1116 tree cond;
1117 gfc_se se;
1119 gfc_init_se (&se, NULL);
1120 se.want_pointer = 1;
1121 gfc_conv_expr (&se, final_expr);
1122 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1123 se.expr, build_int_cst (TREE_TYPE (se.expr), 0));
1125 /* For CLASS(*) not only sym->_vtab->_final can be NULL
1126 but already sym->_vtab itself. */
1127 if (UNLIMITED_POLY (expr))
1129 tree cond2;
1130 gfc_expr *vptr_expr;
1132 vptr_expr = gfc_copy_expr (expr);
1133 gfc_add_vptr_component (vptr_expr);
1135 gfc_init_se (&se, NULL);
1136 se.want_pointer = 1;
1137 gfc_conv_expr (&se, vptr_expr);
1138 gfc_free_expr (vptr_expr);
1140 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1141 se.expr,
1142 build_int_cst (TREE_TYPE (se.expr), 0));
1143 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1144 boolean_type_node, cond2, cond);
1147 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1148 cond, tmp, build_empty_stmt (input_location));
1151 gfc_add_expr_to_block (block, tmp);
1153 return true;
1157 /* User-deallocate; we emit the code directly from the front-end, and the
1158 logic is the same as the previous library function:
1160 void
1161 deallocate (void *pointer, GFC_INTEGER_4 * stat)
1163 if (!pointer)
1165 if (stat)
1166 *stat = 1;
1167 else
1168 runtime_error ("Attempt to DEALLOCATE unallocated memory.");
1170 else
1172 free (pointer);
1173 if (stat)
1174 *stat = 0;
1178 In this front-end version, status doesn't have to be GFC_INTEGER_4.
1179 Moreover, if CAN_FAIL is true, then we will not emit a runtime error,
1180 even when no status variable is passed to us (this is used for
1181 unconditional deallocation generated by the front-end at end of
1182 each procedure).
1184 If a runtime-message is possible, `expr' must point to the original
1185 expression being deallocated for its locus and variable name.
1187 For coarrays, "pointer" must be the array descriptor and not its
1188 "data" component. */
1189 tree
1190 gfc_deallocate_with_status (tree pointer, tree status, tree errmsg,
1191 tree errlen, tree label_finish,
1192 bool can_fail, gfc_expr* expr, bool coarray)
1194 stmtblock_t null, non_null;
1195 tree cond, tmp, error;
1196 tree status_type = NULL_TREE;
1197 tree caf_decl = NULL_TREE;
1199 if (coarray)
1201 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (pointer)));
1202 caf_decl = pointer;
1203 pointer = gfc_conv_descriptor_data_get (caf_decl);
1204 STRIP_NOPS (pointer);
1207 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, pointer,
1208 build_int_cst (TREE_TYPE (pointer), 0));
1210 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
1211 we emit a runtime error. */
1212 gfc_start_block (&null);
1213 if (!can_fail)
1215 tree varname;
1217 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
1219 varname = gfc_build_cstring_const (expr->symtree->name);
1220 varname = gfc_build_addr_expr (pchar_type_node, varname);
1222 error = gfc_trans_runtime_error (true, &expr->where,
1223 "Attempt to DEALLOCATE unallocated '%s'",
1224 varname);
1226 else
1227 error = build_empty_stmt (input_location);
1229 if (status != NULL_TREE && !integer_zerop (status))
1231 tree cond2;
1233 status_type = TREE_TYPE (TREE_TYPE (status));
1234 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1235 status, build_int_cst (TREE_TYPE (status), 0));
1236 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
1237 fold_build1_loc (input_location, INDIRECT_REF,
1238 status_type, status),
1239 build_int_cst (status_type, 1));
1240 error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1241 cond2, tmp, error);
1244 gfc_add_expr_to_block (&null, error);
1246 /* When POINTER is not NULL, we free it. */
1247 gfc_start_block (&non_null);
1248 gfc_add_finalizer_call (&non_null, expr);
1249 if (!coarray || gfc_option.coarray != GFC_FCOARRAY_LIB)
1251 tmp = build_call_expr_loc (input_location,
1252 builtin_decl_explicit (BUILT_IN_FREE), 1,
1253 fold_convert (pvoid_type_node, pointer));
1254 gfc_add_expr_to_block (&non_null, tmp);
1256 if (status != NULL_TREE && !integer_zerop (status))
1258 /* We set STATUS to zero if it is present. */
1259 tree status_type = TREE_TYPE (TREE_TYPE (status));
1260 tree cond2;
1262 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1263 status,
1264 build_int_cst (TREE_TYPE (status), 0));
1265 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
1266 fold_build1_loc (input_location, INDIRECT_REF,
1267 status_type, status),
1268 build_int_cst (status_type, 0));
1269 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1270 gfc_unlikely (cond2), tmp,
1271 build_empty_stmt (input_location));
1272 gfc_add_expr_to_block (&non_null, tmp);
1275 else
1277 tree caf_type, token, cond2;
1278 tree pstat = null_pointer_node;
1280 if (errmsg == NULL_TREE)
1282 gcc_assert (errlen == NULL_TREE);
1283 errmsg = null_pointer_node;
1284 errlen = build_zero_cst (integer_type_node);
1286 else
1288 gcc_assert (errlen != NULL_TREE);
1289 if (!POINTER_TYPE_P (TREE_TYPE (errmsg)))
1290 errmsg = gfc_build_addr_expr (NULL_TREE, errmsg);
1293 caf_type = TREE_TYPE (caf_decl);
1295 if (status != NULL_TREE && !integer_zerop (status))
1297 gcc_assert (status_type == integer_type_node);
1298 pstat = status;
1301 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
1302 && GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE)
1303 token = gfc_conv_descriptor_token (caf_decl);
1304 else if (DECL_LANG_SPECIFIC (caf_decl)
1305 && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
1306 token = GFC_DECL_TOKEN (caf_decl);
1307 else
1309 gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
1310 && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
1311 token = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
1314 token = gfc_build_addr_expr (NULL_TREE, token);
1315 tmp = build_call_expr_loc (input_location,
1316 gfor_fndecl_caf_deregister, 4,
1317 token, pstat, errmsg, errlen);
1318 gfc_add_expr_to_block (&non_null, tmp);
1320 if (status != NULL_TREE)
1322 tree stat = build_fold_indirect_ref_loc (input_location, status);
1324 TREE_USED (label_finish) = 1;
1325 tmp = build1_v (GOTO_EXPR, label_finish);
1326 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1327 stat, build_zero_cst (TREE_TYPE (stat)));
1328 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1329 gfc_unlikely (cond2), tmp,
1330 build_empty_stmt (input_location));
1331 gfc_add_expr_to_block (&non_null, tmp);
1335 return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
1336 gfc_finish_block (&null),
1337 gfc_finish_block (&non_null));
1341 /* Generate code for deallocation of allocatable scalars (variables or
1342 components). Before the object itself is freed, any allocatable
1343 subcomponents are being deallocated. */
1345 tree
1346 gfc_deallocate_scalar_with_status (tree pointer, tree status, bool can_fail,
1347 gfc_expr* expr, gfc_typespec ts)
1349 stmtblock_t null, non_null;
1350 tree cond, tmp, error;
1351 bool finalizable;
1353 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, pointer,
1354 build_int_cst (TREE_TYPE (pointer), 0));
1356 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
1357 we emit a runtime error. */
1358 gfc_start_block (&null);
1359 if (!can_fail)
1361 tree varname;
1363 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
1365 varname = gfc_build_cstring_const (expr->symtree->name);
1366 varname = gfc_build_addr_expr (pchar_type_node, varname);
1368 error = gfc_trans_runtime_error (true, &expr->where,
1369 "Attempt to DEALLOCATE unallocated '%s'",
1370 varname);
1372 else
1373 error = build_empty_stmt (input_location);
1375 if (status != NULL_TREE && !integer_zerop (status))
1377 tree status_type = TREE_TYPE (TREE_TYPE (status));
1378 tree cond2;
1380 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1381 status, build_int_cst (TREE_TYPE (status), 0));
1382 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
1383 fold_build1_loc (input_location, INDIRECT_REF,
1384 status_type, status),
1385 build_int_cst (status_type, 1));
1386 error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1387 cond2, tmp, error);
1390 gfc_add_expr_to_block (&null, error);
1392 /* When POINTER is not NULL, we free it. */
1393 gfc_start_block (&non_null);
1395 /* Free allocatable components. */
1396 finalizable = gfc_add_finalizer_call (&non_null, expr);
1397 if (!finalizable && ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp)
1399 tmp = build_fold_indirect_ref_loc (input_location, pointer);
1400 tmp = gfc_deallocate_alloc_comp (ts.u.derived, tmp, 0);
1401 gfc_add_expr_to_block (&non_null, tmp);
1404 tmp = build_call_expr_loc (input_location,
1405 builtin_decl_explicit (BUILT_IN_FREE), 1,
1406 fold_convert (pvoid_type_node, pointer));
1407 gfc_add_expr_to_block (&non_null, tmp);
1409 if (status != NULL_TREE && !integer_zerop (status))
1411 /* We set STATUS to zero if it is present. */
1412 tree status_type = TREE_TYPE (TREE_TYPE (status));
1413 tree cond2;
1415 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1416 status, build_int_cst (TREE_TYPE (status), 0));
1417 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
1418 fold_build1_loc (input_location, INDIRECT_REF,
1419 status_type, status),
1420 build_int_cst (status_type, 0));
1421 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
1422 tmp, build_empty_stmt (input_location));
1423 gfc_add_expr_to_block (&non_null, tmp);
1426 return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
1427 gfc_finish_block (&null),
1428 gfc_finish_block (&non_null));
1432 /* Reallocate MEM so it has SIZE bytes of data. This behaves like the
1433 following pseudo-code:
1435 void *
1436 internal_realloc (void *mem, size_t size)
1438 res = realloc (mem, size);
1439 if (!res && size != 0)
1440 _gfortran_os_error ("Allocation would exceed memory limit");
1442 return res;
1443 } */
1444 tree
1445 gfc_call_realloc (stmtblock_t * block, tree mem, tree size)
1447 tree msg, res, nonzero, null_result, tmp;
1448 tree type = TREE_TYPE (mem);
1450 size = gfc_evaluate_now (size, block);
1452 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
1453 size = fold_convert (size_type_node, size);
1455 /* Create a variable to hold the result. */
1456 res = gfc_create_var (type, NULL);
1458 /* Call realloc and check the result. */
1459 tmp = build_call_expr_loc (input_location,
1460 builtin_decl_explicit (BUILT_IN_REALLOC), 2,
1461 fold_convert (pvoid_type_node, mem), size);
1462 gfc_add_modify (block, res, fold_convert (type, tmp));
1463 null_result = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
1464 res, build_int_cst (pvoid_type_node, 0));
1465 nonzero = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, size,
1466 build_int_cst (size_type_node, 0));
1467 null_result = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
1468 null_result, nonzero);
1469 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
1470 ("Allocation would exceed memory limit"));
1471 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1472 null_result,
1473 build_call_expr_loc (input_location,
1474 gfor_fndecl_os_error, 1, msg),
1475 build_empty_stmt (input_location));
1476 gfc_add_expr_to_block (block, tmp);
1478 return res;
1482 /* Add an expression to another one, either at the front or the back. */
1484 static void
1485 add_expr_to_chain (tree* chain, tree expr, bool front)
1487 if (expr == NULL_TREE || IS_EMPTY_STMT (expr))
1488 return;
1490 if (*chain)
1492 if (TREE_CODE (*chain) != STATEMENT_LIST)
1494 tree tmp;
1496 tmp = *chain;
1497 *chain = NULL_TREE;
1498 append_to_statement_list (tmp, chain);
1501 if (front)
1503 tree_stmt_iterator i;
1505 i = tsi_start (*chain);
1506 tsi_link_before (&i, expr, TSI_CONTINUE_LINKING);
1508 else
1509 append_to_statement_list (expr, chain);
1511 else
1512 *chain = expr;
1516 /* Add a statement at the end of a block. */
1518 void
1519 gfc_add_expr_to_block (stmtblock_t * block, tree expr)
1521 gcc_assert (block);
1522 add_expr_to_chain (&block->head, expr, false);
1526 /* Add a statement at the beginning of a block. */
1528 void
1529 gfc_prepend_expr_to_block (stmtblock_t * block, tree expr)
1531 gcc_assert (block);
1532 add_expr_to_chain (&block->head, expr, true);
1536 /* Add a block the end of a block. */
1538 void
1539 gfc_add_block_to_block (stmtblock_t * block, stmtblock_t * append)
1541 gcc_assert (append);
1542 gcc_assert (!append->has_scope);
1544 gfc_add_expr_to_block (block, append->head);
1545 append->head = NULL_TREE;
1549 /* Save the current locus. The structure may not be complete, and should
1550 only be used with gfc_restore_backend_locus. */
1552 void
1553 gfc_save_backend_locus (locus * loc)
1555 loc->lb = XCNEW (gfc_linebuf);
1556 loc->lb->location = input_location;
1557 loc->lb->file = gfc_current_backend_file;
1561 /* Set the current locus. */
1563 void
1564 gfc_set_backend_locus (locus * loc)
1566 gfc_current_backend_file = loc->lb->file;
1567 input_location = loc->lb->location;
1571 /* Restore the saved locus. Only used in conjunction with
1572 gfc_save_backend_locus, to free the memory when we are done. */
1574 void
1575 gfc_restore_backend_locus (locus * loc)
1577 gfc_set_backend_locus (loc);
1578 free (loc->lb);
1582 /* Translate an executable statement. The tree cond is used by gfc_trans_do.
1583 This static function is wrapped by gfc_trans_code_cond and
1584 gfc_trans_code. */
1586 static tree
1587 trans_code (gfc_code * code, tree cond)
1589 stmtblock_t block;
1590 tree res;
1592 if (!code)
1593 return build_empty_stmt (input_location);
1595 gfc_start_block (&block);
1597 /* Translate statements one by one into GENERIC trees until we reach
1598 the end of this gfc_code branch. */
1599 for (; code; code = code->next)
1601 if (code->here != 0)
1603 res = gfc_trans_label_here (code);
1604 gfc_add_expr_to_block (&block, res);
1607 gfc_set_backend_locus (&code->loc);
1609 switch (code->op)
1611 case EXEC_NOP:
1612 case EXEC_END_BLOCK:
1613 case EXEC_END_NESTED_BLOCK:
1614 case EXEC_END_PROCEDURE:
1615 res = NULL_TREE;
1616 break;
1618 case EXEC_ASSIGN:
1619 if (code->expr1->ts.type == BT_CLASS)
1620 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1621 else
1622 res = gfc_trans_assign (code);
1623 break;
1625 case EXEC_LABEL_ASSIGN:
1626 res = gfc_trans_label_assign (code);
1627 break;
1629 case EXEC_POINTER_ASSIGN:
1630 if (code->expr1->ts.type == BT_CLASS)
1631 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1632 else if (UNLIMITED_POLY (code->expr2)
1633 && code->expr1->ts.type == BT_DERIVED
1634 && (code->expr1->ts.u.derived->attr.sequence
1635 || code->expr1->ts.u.derived->attr.is_bind_c))
1636 /* F2003: C717 */
1637 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1638 else
1639 res = gfc_trans_pointer_assign (code);
1640 break;
1642 case EXEC_INIT_ASSIGN:
1643 if (code->expr1->ts.type == BT_CLASS)
1644 res = gfc_trans_class_init_assign (code);
1645 else
1646 res = gfc_trans_init_assign (code);
1647 break;
1649 case EXEC_CONTINUE:
1650 res = NULL_TREE;
1651 break;
1653 case EXEC_CRITICAL:
1654 res = gfc_trans_critical (code);
1655 break;
1657 case EXEC_CYCLE:
1658 res = gfc_trans_cycle (code);
1659 break;
1661 case EXEC_EXIT:
1662 res = gfc_trans_exit (code);
1663 break;
1665 case EXEC_GOTO:
1666 res = gfc_trans_goto (code);
1667 break;
1669 case EXEC_ENTRY:
1670 res = gfc_trans_entry (code);
1671 break;
1673 case EXEC_PAUSE:
1674 res = gfc_trans_pause (code);
1675 break;
1677 case EXEC_STOP:
1678 case EXEC_ERROR_STOP:
1679 res = gfc_trans_stop (code, code->op == EXEC_ERROR_STOP);
1680 break;
1682 case EXEC_CALL:
1683 /* For MVBITS we've got the special exception that we need a
1684 dependency check, too. */
1686 bool is_mvbits = false;
1688 if (code->resolved_isym)
1690 res = gfc_conv_intrinsic_subroutine (code);
1691 if (res != NULL_TREE)
1692 break;
1695 if (code->resolved_isym
1696 && code->resolved_isym->id == GFC_ISYM_MVBITS)
1697 is_mvbits = true;
1699 res = gfc_trans_call (code, is_mvbits, NULL_TREE,
1700 NULL_TREE, false);
1702 break;
1704 case EXEC_CALL_PPC:
1705 res = gfc_trans_call (code, false, NULL_TREE,
1706 NULL_TREE, false);
1707 break;
1709 case EXEC_ASSIGN_CALL:
1710 res = gfc_trans_call (code, true, NULL_TREE,
1711 NULL_TREE, false);
1712 break;
1714 case EXEC_RETURN:
1715 res = gfc_trans_return (code);
1716 break;
1718 case EXEC_IF:
1719 res = gfc_trans_if (code);
1720 break;
1722 case EXEC_ARITHMETIC_IF:
1723 res = gfc_trans_arithmetic_if (code);
1724 break;
1726 case EXEC_BLOCK:
1727 res = gfc_trans_block_construct (code);
1728 break;
1730 case EXEC_DO:
1731 res = gfc_trans_do (code, cond);
1732 break;
1734 case EXEC_DO_CONCURRENT:
1735 res = gfc_trans_do_concurrent (code);
1736 break;
1738 case EXEC_DO_WHILE:
1739 res = gfc_trans_do_while (code);
1740 break;
1742 case EXEC_SELECT:
1743 res = gfc_trans_select (code);
1744 break;
1746 case EXEC_SELECT_TYPE:
1747 /* Do nothing. SELECT TYPE statements should be transformed into
1748 an ordinary SELECT CASE at resolution stage.
1749 TODO: Add an error message here once this is done. */
1750 res = NULL_TREE;
1751 break;
1753 case EXEC_FLUSH:
1754 res = gfc_trans_flush (code);
1755 break;
1757 case EXEC_SYNC_ALL:
1758 case EXEC_SYNC_IMAGES:
1759 case EXEC_SYNC_MEMORY:
1760 res = gfc_trans_sync (code, code->op);
1761 break;
1763 case EXEC_LOCK:
1764 case EXEC_UNLOCK:
1765 res = gfc_trans_lock_unlock (code, code->op);
1766 break;
1768 case EXEC_FORALL:
1769 res = gfc_trans_forall (code);
1770 break;
1772 case EXEC_WHERE:
1773 res = gfc_trans_where (code);
1774 break;
1776 case EXEC_ALLOCATE:
1777 res = gfc_trans_allocate (code);
1778 break;
1780 case EXEC_DEALLOCATE:
1781 res = gfc_trans_deallocate (code);
1782 break;
1784 case EXEC_OPEN:
1785 res = gfc_trans_open (code);
1786 break;
1788 case EXEC_CLOSE:
1789 res = gfc_trans_close (code);
1790 break;
1792 case EXEC_READ:
1793 res = gfc_trans_read (code);
1794 break;
1796 case EXEC_WRITE:
1797 res = gfc_trans_write (code);
1798 break;
1800 case EXEC_IOLENGTH:
1801 res = gfc_trans_iolength (code);
1802 break;
1804 case EXEC_BACKSPACE:
1805 res = gfc_trans_backspace (code);
1806 break;
1808 case EXEC_ENDFILE:
1809 res = gfc_trans_endfile (code);
1810 break;
1812 case EXEC_INQUIRE:
1813 res = gfc_trans_inquire (code);
1814 break;
1816 case EXEC_WAIT:
1817 res = gfc_trans_wait (code);
1818 break;
1820 case EXEC_REWIND:
1821 res = gfc_trans_rewind (code);
1822 break;
1824 case EXEC_TRANSFER:
1825 res = gfc_trans_transfer (code);
1826 break;
1828 case EXEC_DT_END:
1829 res = gfc_trans_dt_end (code);
1830 break;
1832 case EXEC_OMP_ATOMIC:
1833 case EXEC_OMP_BARRIER:
1834 case EXEC_OMP_CRITICAL:
1835 case EXEC_OMP_DO:
1836 case EXEC_OMP_FLUSH:
1837 case EXEC_OMP_MASTER:
1838 case EXEC_OMP_ORDERED:
1839 case EXEC_OMP_PARALLEL:
1840 case EXEC_OMP_PARALLEL_DO:
1841 case EXEC_OMP_PARALLEL_SECTIONS:
1842 case EXEC_OMP_PARALLEL_WORKSHARE:
1843 case EXEC_OMP_SECTIONS:
1844 case EXEC_OMP_SINGLE:
1845 case EXEC_OMP_TASK:
1846 case EXEC_OMP_TASKWAIT:
1847 case EXEC_OMP_TASKYIELD:
1848 case EXEC_OMP_WORKSHARE:
1849 res = gfc_trans_omp_directive (code);
1850 break;
1852 default:
1853 internal_error ("gfc_trans_code(): Bad statement code");
1856 gfc_set_backend_locus (&code->loc);
1858 if (res != NULL_TREE && ! IS_EMPTY_STMT (res))
1860 if (TREE_CODE (res) != STATEMENT_LIST)
1861 SET_EXPR_LOCATION (res, input_location);
1863 /* Add the new statement to the block. */
1864 gfc_add_expr_to_block (&block, res);
1868 /* Return the finished block. */
1869 return gfc_finish_block (&block);
1873 /* Translate an executable statement with condition, cond. The condition is
1874 used by gfc_trans_do to test for IO result conditions inside implied
1875 DO loops of READ and WRITE statements. See build_dt in trans-io.c. */
1877 tree
1878 gfc_trans_code_cond (gfc_code * code, tree cond)
1880 return trans_code (code, cond);
1883 /* Translate an executable statement without condition. */
1885 tree
1886 gfc_trans_code (gfc_code * code)
1888 return trans_code (code, NULL_TREE);
1892 /* This function is called after a complete program unit has been parsed
1893 and resolved. */
1895 void
1896 gfc_generate_code (gfc_namespace * ns)
1898 ompws_flags = 0;
1899 if (ns->is_block_data)
1901 gfc_generate_block_data (ns);
1902 return;
1905 gfc_generate_function_code (ns);
1909 /* This function is called after a complete module has been parsed
1910 and resolved. */
1912 void
1913 gfc_generate_module_code (gfc_namespace * ns)
1915 gfc_namespace *n;
1916 struct module_htab_entry *entry;
1918 gcc_assert (ns->proc_name->backend_decl == NULL);
1919 ns->proc_name->backend_decl
1920 = build_decl (ns->proc_name->declared_at.lb->location,
1921 NAMESPACE_DECL, get_identifier (ns->proc_name->name),
1922 void_type_node);
1923 entry = gfc_find_module (ns->proc_name->name);
1924 if (entry->namespace_decl)
1925 /* Buggy sourcecode, using a module before defining it? */
1926 htab_empty (entry->decls);
1927 entry->namespace_decl = ns->proc_name->backend_decl;
1929 gfc_generate_module_vars (ns);
1931 /* We need to generate all module function prototypes first, to allow
1932 sibling calls. */
1933 for (n = ns->contained; n; n = n->sibling)
1935 gfc_entry_list *el;
1937 if (!n->proc_name)
1938 continue;
1940 gfc_create_function_decl (n, false);
1941 DECL_CONTEXT (n->proc_name->backend_decl) = ns->proc_name->backend_decl;
1942 gfc_module_add_decl (entry, n->proc_name->backend_decl);
1943 for (el = ns->entries; el; el = el->next)
1945 DECL_CONTEXT (el->sym->backend_decl) = ns->proc_name->backend_decl;
1946 gfc_module_add_decl (entry, el->sym->backend_decl);
1950 for (n = ns->contained; n; n = n->sibling)
1952 if (!n->proc_name)
1953 continue;
1955 gfc_generate_function_code (n);
1960 /* Initialize an init/cleanup block with existing code. */
1962 void
1963 gfc_start_wrapped_block (gfc_wrapped_block* block, tree code)
1965 gcc_assert (block);
1967 block->init = NULL_TREE;
1968 block->code = code;
1969 block->cleanup = NULL_TREE;
1973 /* Add a new pair of initializers/clean-up code. */
1975 void
1976 gfc_add_init_cleanup (gfc_wrapped_block* block, tree init, tree cleanup)
1978 gcc_assert (block);
1980 /* The new pair of init/cleanup should be "wrapped around" the existing
1981 block of code, thus the initialization is added to the front and the
1982 cleanup to the back. */
1983 add_expr_to_chain (&block->init, init, true);
1984 add_expr_to_chain (&block->cleanup, cleanup, false);
1988 /* Finish up a wrapped block by building a corresponding try-finally expr. */
1990 tree
1991 gfc_finish_wrapped_block (gfc_wrapped_block* block)
1993 tree result;
1995 gcc_assert (block);
1997 /* Build the final expression. For this, just add init and body together,
1998 and put clean-up with that into a TRY_FINALLY_EXPR. */
1999 result = block->init;
2000 add_expr_to_chain (&result, block->code, false);
2001 if (block->cleanup)
2002 result = build2_loc (input_location, TRY_FINALLY_EXPR, void_type_node,
2003 result, block->cleanup);
2005 /* Clear the block. */
2006 block->init = NULL_TREE;
2007 block->code = NULL_TREE;
2008 block->cleanup = NULL_TREE;
2010 return result;
2014 /* Helper function for marking a boolean expression tree as unlikely. */
2016 tree
2017 gfc_unlikely (tree cond)
2019 tree tmp;
2021 cond = fold_convert (long_integer_type_node, cond);
2022 tmp = build_zero_cst (long_integer_type_node);
2023 cond = build_call_expr_loc (input_location,
2024 builtin_decl_explicit (BUILT_IN_EXPECT),
2025 2, cond, tmp);
2026 cond = fold_convert (boolean_type_node, cond);
2027 return cond;
2031 /* Helper function for marking a boolean expression tree as likely. */
2033 tree
2034 gfc_likely (tree cond)
2036 tree tmp;
2038 cond = fold_convert (long_integer_type_node, cond);
2039 tmp = build_one_cst (long_integer_type_node);
2040 cond = build_call_expr_loc (input_location,
2041 builtin_decl_explicit (BUILT_IN_EXPECT),
2042 2, cond, tmp);
2043 cond = fold_convert (boolean_type_node, cond);
2044 return cond;