1 /* Backend function setup
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software
4 Contributed by Paul Brook
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* trans-decl.c -- Handling of backend function and variable decls, etc */
26 #include "coretypes.h"
28 #include "tree-dump.h"
29 #include "tree-gimple.h"
40 #include "trans-types.h"
41 #include "trans-array.h"
42 #include "trans-const.h"
43 /* Only for gfc_trans_code. Shouldn't need to include this. */
44 #include "trans-stmt.h"
46 #define MAX_LABEL_VALUE 99999
49 /* Holds the result of the function if no result variable specified. */
51 static GTY(()) tree current_fake_result_decl;
52 static GTY(()) tree parent_fake_result_decl;
54 static GTY(()) tree current_function_return_label;
57 /* Holds the variable DECLs for the current function. */
59 static GTY(()) tree saved_function_decls;
60 static GTY(()) tree saved_parent_function_decls;
63 /* The namespace of the module we're currently generating. Only used while
64 outputting decls for module variables. Do not rely on this being set. */
66 static gfc_namespace *module_namespace;
69 /* List of static constructor functions. */
71 tree gfc_static_ctors;
74 /* Function declarations for builtin library functions. */
76 tree gfor_fndecl_pause_numeric;
77 tree gfor_fndecl_pause_string;
78 tree gfor_fndecl_stop_numeric;
79 tree gfor_fndecl_stop_string;
80 tree gfor_fndecl_select_string;
81 tree gfor_fndecl_runtime_error;
82 tree gfor_fndecl_runtime_error_at;
83 tree gfor_fndecl_os_error;
84 tree gfor_fndecl_generate_error;
85 tree gfor_fndecl_set_fpe;
86 tree gfor_fndecl_set_options;
87 tree gfor_fndecl_set_convert;
88 tree gfor_fndecl_set_record_marker;
89 tree gfor_fndecl_set_max_subrecord_length;
90 tree gfor_fndecl_ctime;
91 tree gfor_fndecl_fdate;
92 tree gfor_fndecl_ttynam;
93 tree gfor_fndecl_in_pack;
94 tree gfor_fndecl_in_unpack;
95 tree gfor_fndecl_associated;
98 /* Math functions. Many other math functions are handled in
101 gfc_powdecl_list gfor_fndecl_math_powi[4][3];
102 tree gfor_fndecl_math_ishftc4;
103 tree gfor_fndecl_math_ishftc8;
104 tree gfor_fndecl_math_ishftc16;
107 /* String functions. */
109 tree gfor_fndecl_compare_string;
110 tree gfor_fndecl_concat_string;
111 tree gfor_fndecl_string_len_trim;
112 tree gfor_fndecl_string_index;
113 tree gfor_fndecl_string_scan;
114 tree gfor_fndecl_string_verify;
115 tree gfor_fndecl_string_trim;
116 tree gfor_fndecl_string_minmax;
117 tree gfor_fndecl_adjustl;
118 tree gfor_fndecl_adjustr;
121 /* Other misc. runtime library functions. */
123 tree gfor_fndecl_size0;
124 tree gfor_fndecl_size1;
125 tree gfor_fndecl_iargc;
127 /* Intrinsic functions implemented in FORTRAN. */
128 tree gfor_fndecl_si_kind;
129 tree gfor_fndecl_sr_kind;
131 /* BLAS gemm functions. */
132 tree gfor_fndecl_sgemm;
133 tree gfor_fndecl_dgemm;
134 tree gfor_fndecl_cgemm;
135 tree gfor_fndecl_zgemm;
139 gfc_add_decl_to_parent_function (tree decl)
142 DECL_CONTEXT (decl) = DECL_CONTEXT (current_function_decl);
143 DECL_NONLOCAL (decl) = 1;
144 TREE_CHAIN (decl) = saved_parent_function_decls;
145 saved_parent_function_decls = decl;
149 gfc_add_decl_to_function (tree decl)
152 TREE_USED (decl) = 1;
153 DECL_CONTEXT (decl) = current_function_decl;
154 TREE_CHAIN (decl) = saved_function_decls;
155 saved_function_decls = decl;
159 /* Build a backend label declaration. Set TREE_USED for named labels.
160 The context of the label is always the current_function_decl. All
161 labels are marked artificial. */
164 gfc_build_label_decl (tree label_id)
166 /* 2^32 temporaries should be enough. */
167 static unsigned int tmp_num = 1;
171 if (label_id == NULL_TREE)
173 /* Build an internal label name. */
174 ASM_FORMAT_PRIVATE_NAME (label_name, "L", tmp_num++);
175 label_id = get_identifier (label_name);
180 /* Build the LABEL_DECL node. Labels have no type. */
181 label_decl = build_decl (LABEL_DECL, label_id, void_type_node);
182 DECL_CONTEXT (label_decl) = current_function_decl;
183 DECL_MODE (label_decl) = VOIDmode;
185 /* We always define the label as used, even if the original source
186 file never references the label. We don't want all kinds of
187 spurious warnings for old-style Fortran code with too many
189 TREE_USED (label_decl) = 1;
191 DECL_ARTIFICIAL (label_decl) = 1;
196 /* Returns the return label for the current function. */
199 gfc_get_return_label (void)
201 char name[GFC_MAX_SYMBOL_LEN + 10];
203 if (current_function_return_label)
204 return current_function_return_label;
206 sprintf (name, "__return_%s",
207 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
209 current_function_return_label =
210 gfc_build_label_decl (get_identifier (name));
212 DECL_ARTIFICIAL (current_function_return_label) = 1;
214 return current_function_return_label;
218 /* Set the backend source location of a decl. */
221 gfc_set_decl_location (tree decl, locus * loc)
223 DECL_SOURCE_LOCATION (decl) = loc->lb->location;
227 /* Return the backend label declaration for a given label structure,
228 or create it if it doesn't exist yet. */
231 gfc_get_label_decl (gfc_st_label * lp)
233 if (lp->backend_decl)
234 return lp->backend_decl;
237 char label_name[GFC_MAX_SYMBOL_LEN + 1];
240 /* Validate the label declaration from the front end. */
241 gcc_assert (lp != NULL && lp->value <= MAX_LABEL_VALUE);
243 /* Build a mangled name for the label. */
244 sprintf (label_name, "__label_%.6d", lp->value);
246 /* Build the LABEL_DECL node. */
247 label_decl = gfc_build_label_decl (get_identifier (label_name));
249 /* Tell the debugger where the label came from. */
250 if (lp->value <= MAX_LABEL_VALUE) /* An internal label. */
251 gfc_set_decl_location (label_decl, &lp->where);
253 DECL_ARTIFICIAL (label_decl) = 1;
255 /* Store the label in the label list and return the LABEL_DECL. */
256 lp->backend_decl = label_decl;
262 /* Convert a gfc_symbol to an identifier of the same name. */
265 gfc_sym_identifier (gfc_symbol * sym)
267 return (get_identifier (sym->name));
271 /* Construct mangled name from symbol name. */
274 gfc_sym_mangled_identifier (gfc_symbol * sym)
276 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
278 /* Prevent the mangling of identifiers that have an assigned
279 binding label (mainly those that are bind(c)). */
280 if (sym->attr.is_bind_c == 1
281 && sym->binding_label[0] != '\0')
282 return get_identifier(sym->binding_label);
284 if (sym->module == NULL)
285 return gfc_sym_identifier (sym);
288 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
289 return get_identifier (name);
294 /* Construct mangled function name from symbol name. */
297 gfc_sym_mangled_function_id (gfc_symbol * sym)
300 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
302 /* It may be possible to simply use the binding label if it's
303 provided, and remove the other checks. Then we could use it
304 for other things if we wished. */
305 if ((sym->attr.is_bind_c == 1 || sym->attr.is_iso_c == 1) &&
306 sym->binding_label[0] != '\0')
307 /* use the binding label rather than the mangled name */
308 return get_identifier (sym->binding_label);
310 if (sym->module == NULL || sym->attr.proc == PROC_EXTERNAL
311 || (sym->module != NULL && (sym->attr.external
312 || sym->attr.if_source == IFSRC_IFBODY)))
314 /* Main program is mangled into MAIN__. */
315 if (sym->attr.is_main_program)
316 return get_identifier ("MAIN__");
318 /* Intrinsic procedures are never mangled. */
319 if (sym->attr.proc == PROC_INTRINSIC)
320 return get_identifier (sym->name);
322 if (gfc_option.flag_underscoring)
324 has_underscore = strchr (sym->name, '_') != 0;
325 if (gfc_option.flag_second_underscore && has_underscore)
326 snprintf (name, sizeof name, "%s__", sym->name);
328 snprintf (name, sizeof name, "%s_", sym->name);
329 return get_identifier (name);
332 return get_identifier (sym->name);
336 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
337 return get_identifier (name);
342 /* Returns true if a variable of specified size should go on the stack. */
345 gfc_can_put_var_on_stack (tree size)
347 unsigned HOST_WIDE_INT low;
349 if (!INTEGER_CST_P (size))
352 if (gfc_option.flag_max_stack_var_size < 0)
355 if (TREE_INT_CST_HIGH (size) != 0)
358 low = TREE_INT_CST_LOW (size);
359 if (low > (unsigned HOST_WIDE_INT) gfc_option.flag_max_stack_var_size)
362 /* TODO: Set a per-function stack size limit. */
368 /* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
369 an expression involving its corresponding pointer. There are
370 2 cases; one for variable size arrays, and one for everything else,
371 because variable-sized arrays require one fewer level of
375 gfc_finish_cray_pointee (tree decl, gfc_symbol *sym)
377 tree ptr_decl = gfc_get_symbol_decl (sym->cp_pointer);
380 /* Parameters need to be dereferenced. */
381 if (sym->cp_pointer->attr.dummy)
382 ptr_decl = build_fold_indirect_ref (ptr_decl);
384 /* Check to see if we're dealing with a variable-sized array. */
385 if (sym->attr.dimension
386 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
388 /* These decls will be dereferenced later, so we don't dereference
390 value = convert (TREE_TYPE (decl), ptr_decl);
394 ptr_decl = convert (build_pointer_type (TREE_TYPE (decl)),
396 value = build_fold_indirect_ref (ptr_decl);
399 SET_DECL_VALUE_EXPR (decl, value);
400 DECL_HAS_VALUE_EXPR_P (decl) = 1;
401 GFC_DECL_CRAY_POINTEE (decl) = 1;
402 /* This is a fake variable just for debugging purposes. */
403 TREE_ASM_WRITTEN (decl) = 1;
407 /* Finish processing of a declaration without an initial value. */
410 gfc_finish_decl (tree decl)
412 gcc_assert (TREE_CODE (decl) == PARM_DECL
413 || DECL_INITIAL (decl) == NULL_TREE);
415 if (TREE_CODE (decl) != VAR_DECL)
418 if (DECL_SIZE (decl) == NULL_TREE
419 && TYPE_SIZE (TREE_TYPE (decl)) != NULL_TREE)
420 layout_decl (decl, 0);
422 /* A few consistency checks. */
423 /* A static variable with an incomplete type is an error if it is
424 initialized. Also if it is not file scope. Otherwise, let it
425 through, but if it is not `extern' then it may cause an error
427 /* An automatic variable with an incomplete type is an error. */
429 /* We should know the storage size. */
430 gcc_assert (DECL_SIZE (decl) != NULL_TREE
431 || (TREE_STATIC (decl)
432 ? (!DECL_INITIAL (decl) || !DECL_CONTEXT (decl))
433 : DECL_EXTERNAL (decl)));
435 /* The storage size should be constant. */
436 gcc_assert ((!DECL_EXTERNAL (decl) && !TREE_STATIC (decl))
438 || TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST);
442 /* Apply symbol attributes to a variable, and add it to the function scope. */
445 gfc_finish_var_decl (tree decl, gfc_symbol * sym)
448 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
449 This is the equivalent of the TARGET variables.
450 We also need to set this if the variable is passed by reference in a
453 /* Set DECL_VALUE_EXPR for Cray Pointees. */
454 if (sym->attr.cray_pointee)
455 gfc_finish_cray_pointee (decl, sym);
457 if (sym->attr.target)
458 TREE_ADDRESSABLE (decl) = 1;
459 /* If it wasn't used we wouldn't be getting it. */
460 TREE_USED (decl) = 1;
462 /* Chain this decl to the pending declarations. Don't do pushdecl()
463 because this would add them to the current scope rather than the
465 if (current_function_decl != NULL_TREE)
467 if (sym->ns->proc_name->backend_decl == current_function_decl
468 || sym->result == sym)
469 gfc_add_decl_to_function (decl);
471 gfc_add_decl_to_parent_function (decl);
474 if (sym->attr.cray_pointee)
477 if(sym->attr.is_bind_c == 1)
479 /* We need to put variables that are bind(c) into the common
480 segment of the object file, because this is what C would do.
481 gfortran would typically put them in either the BSS or
482 initialized data segments, and only mark them as common if
483 they were part of common blocks. However, if they are not put
484 into common space, then C cannot initialize global fortran
485 variables that it interoperates with and the draft says that
486 either Fortran or C should be able to initialize it (but not
487 both, of course.) (J3/04-007, section 15.3). */
488 TREE_PUBLIC(decl) = 1;
489 DECL_COMMON(decl) = 1;
492 /* If a variable is USE associated, it's always external. */
493 if (sym->attr.use_assoc)
495 DECL_EXTERNAL (decl) = 1;
496 TREE_PUBLIC (decl) = 1;
498 else if (sym->module && !sym->attr.result && !sym->attr.dummy)
500 /* TODO: Don't set sym->module for result or dummy variables. */
501 gcc_assert (current_function_decl == NULL_TREE || sym->result == sym);
502 /* This is the declaration of a module variable. */
503 TREE_PUBLIC (decl) = 1;
504 TREE_STATIC (decl) = 1;
507 /* Derived types are a bit peculiar because of the possibility of
508 a default initializer; this must be applied each time the variable
509 comes into scope it therefore need not be static. These variables
510 are SAVE_NONE but have an initializer. Otherwise explicitly
511 intitialized variables are SAVE_IMPLICIT and explicitly saved are
513 if (!sym->attr.use_assoc
514 && (sym->attr.save != SAVE_NONE || sym->attr.data
515 || (sym->ts.type == BT_DERIVED
516 && sym->ts.derived->attr.alloc_comp
518 || (sym->value && sym->ns->proc_name->attr.is_main_program)))
519 TREE_STATIC (decl) = 1;
521 if (sym->attr.volatile_)
523 TREE_THIS_VOLATILE (decl) = 1;
524 new = build_qualified_type (TREE_TYPE (decl), TYPE_QUAL_VOLATILE);
525 TREE_TYPE (decl) = new;
528 /* Keep variables larger than max-stack-var-size off stack. */
529 if (!sym->ns->proc_name->attr.recursive
530 && INTEGER_CST_P (DECL_SIZE_UNIT (decl))
531 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
532 /* Put variable length auto array pointers always into stack. */
533 && (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
534 || sym->attr.dimension == 0
535 || sym->as->type != AS_EXPLICIT
537 || sym->attr.allocatable)
538 && !DECL_ARTIFICIAL (decl))
539 TREE_STATIC (decl) = 1;
541 /* Handle threadprivate variables. */
542 if (sym->attr.threadprivate
543 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
544 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
548 /* Allocate the lang-specific part of a decl. */
551 gfc_allocate_lang_decl (tree decl)
553 DECL_LANG_SPECIFIC (decl) = (struct lang_decl *)
554 ggc_alloc_cleared (sizeof (struct lang_decl));
557 /* Remember a symbol to generate initialization/cleanup code at function
561 gfc_defer_symbol_init (gfc_symbol * sym)
567 /* Don't add a symbol twice. */
571 last = head = sym->ns->proc_name;
574 /* Make sure that setup code for dummy variables which are used in the
575 setup of other variables is generated first. */
578 /* Find the first dummy arg seen after us, or the first non-dummy arg.
579 This is a circular list, so don't go past the head. */
581 && (!p->attr.dummy || p->dummy_order > sym->dummy_order))
587 /* Insert in between last and p. */
593 /* Create an array index type variable with function scope. */
596 create_index_var (const char * pfx, int nest)
600 decl = gfc_create_var_np (gfc_array_index_type, pfx);
602 gfc_add_decl_to_parent_function (decl);
604 gfc_add_decl_to_function (decl);
609 /* Create variables to hold all the non-constant bits of info for a
610 descriptorless array. Remember these in the lang-specific part of the
614 gfc_build_qualified_array (tree decl, gfc_symbol * sym)
620 type = TREE_TYPE (decl);
622 /* We just use the descriptor, if there is one. */
623 if (GFC_DESCRIPTOR_TYPE_P (type))
626 gcc_assert (GFC_ARRAY_TYPE_P (type));
627 nest = (sym->ns->proc_name->backend_decl != current_function_decl)
628 && !sym->attr.contained;
630 for (dim = 0; dim < GFC_TYPE_ARRAY_RANK (type); dim++)
632 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
634 GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
635 TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type, dim)) = 1;
637 /* Don't try to use the unknown bound for assumed shape arrays. */
638 if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
639 && (sym->as->type != AS_ASSUMED_SIZE
640 || dim < GFC_TYPE_ARRAY_RANK (type) - 1))
642 GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
643 TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type, dim)) = 1;
646 if (GFC_TYPE_ARRAY_STRIDE (type, dim) == NULL_TREE)
648 GFC_TYPE_ARRAY_STRIDE (type, dim) = create_index_var ("stride", nest);
649 TREE_NO_WARNING (GFC_TYPE_ARRAY_STRIDE (type, dim)) = 1;
652 if (GFC_TYPE_ARRAY_OFFSET (type) == NULL_TREE)
654 GFC_TYPE_ARRAY_OFFSET (type) = gfc_create_var_np (gfc_array_index_type,
656 TREE_NO_WARNING (GFC_TYPE_ARRAY_OFFSET (type)) = 1;
659 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type));
661 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type));
664 if (GFC_TYPE_ARRAY_SIZE (type) == NULL_TREE
665 && sym->as->type != AS_ASSUMED_SIZE)
667 GFC_TYPE_ARRAY_SIZE (type) = create_index_var ("size", nest);
668 TREE_NO_WARNING (GFC_TYPE_ARRAY_SIZE (type)) = 1;
671 if (POINTER_TYPE_P (type))
673 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type)));
674 gcc_assert (TYPE_LANG_SPECIFIC (type)
675 == TYPE_LANG_SPECIFIC (TREE_TYPE (type)));
676 type = TREE_TYPE (type);
679 if (! COMPLETE_TYPE_P (type) && GFC_TYPE_ARRAY_SIZE (type))
683 size = fold_build2 (MINUS_EXPR, gfc_array_index_type,
684 GFC_TYPE_ARRAY_SIZE (type), gfc_index_one_node);
685 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
687 TYPE_DOMAIN (type) = range;
693 /* For some dummy arguments we don't use the actual argument directly.
694 Instead we create a local decl and use that. This allows us to perform
695 initialization, and construct full type information. */
698 gfc_build_dummy_array_decl (gfc_symbol * sym, tree dummy)
708 if (sym->attr.pointer || sym->attr.allocatable)
711 /* Add to list of variables if not a fake result variable. */
712 if (sym->attr.result || sym->attr.dummy)
713 gfc_defer_symbol_init (sym);
715 type = TREE_TYPE (dummy);
716 gcc_assert (TREE_CODE (dummy) == PARM_DECL
717 && POINTER_TYPE_P (type));
719 /* Do we know the element size? */
720 known_size = sym->ts.type != BT_CHARACTER
721 || INTEGER_CST_P (sym->ts.cl->backend_decl);
723 if (known_size && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type)))
725 /* For descriptorless arrays with known element size the actual
726 argument is sufficient. */
727 gcc_assert (GFC_ARRAY_TYPE_P (type));
728 gfc_build_qualified_array (dummy, sym);
732 type = TREE_TYPE (type);
733 if (GFC_DESCRIPTOR_TYPE_P (type))
735 /* Create a descriptorless array pointer. */
738 if (!gfc_option.flag_repack_arrays)
740 if (as->type == AS_ASSUMED_SIZE)
741 packed = PACKED_FULL;
745 if (as->type == AS_EXPLICIT)
747 packed = PACKED_FULL;
748 for (n = 0; n < as->rank; n++)
752 && as->upper[n]->expr_type == EXPR_CONSTANT
753 && as->lower[n]->expr_type == EXPR_CONSTANT))
754 packed = PACKED_PARTIAL;
758 packed = PACKED_PARTIAL;
761 type = gfc_typenode_for_spec (&sym->ts);
762 type = gfc_get_nodesc_array_type (type, sym->as, packed);
766 /* We now have an expression for the element size, so create a fully
767 qualified type. Reset sym->backend decl or this will just return the
769 DECL_ARTIFICIAL (sym->backend_decl) = 1;
770 sym->backend_decl = NULL_TREE;
771 type = gfc_sym_type (sym);
772 packed = PACKED_FULL;
775 ASM_FORMAT_PRIVATE_NAME (name, IDENTIFIER_POINTER (DECL_NAME (dummy)), 0);
776 decl = build_decl (VAR_DECL, get_identifier (name), type);
778 DECL_ARTIFICIAL (decl) = 1;
779 TREE_PUBLIC (decl) = 0;
780 TREE_STATIC (decl) = 0;
781 DECL_EXTERNAL (decl) = 0;
783 /* We should never get deferred shape arrays here. We used to because of
785 gcc_assert (sym->as->type != AS_DEFERRED);
787 if (packed == PACKED_PARTIAL)
788 GFC_DECL_PARTIAL_PACKED_ARRAY (decl) = 1;
789 else if (packed == PACKED_FULL)
790 GFC_DECL_PACKED_ARRAY (decl) = 1;
792 gfc_build_qualified_array (decl, sym);
794 if (DECL_LANG_SPECIFIC (dummy))
795 DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (dummy);
797 gfc_allocate_lang_decl (decl);
799 GFC_DECL_SAVED_DESCRIPTOR (decl) = dummy;
801 if (sym->ns->proc_name->backend_decl == current_function_decl
802 || sym->attr.contained)
803 gfc_add_decl_to_function (decl);
805 gfc_add_decl_to_parent_function (decl);
811 /* Return a constant or a variable to use as a string length. Does not
812 add the decl to the current scope. */
815 gfc_create_string_length (gfc_symbol * sym)
819 gcc_assert (sym->ts.cl);
820 gfc_conv_const_charlen (sym->ts.cl);
822 if (sym->ts.cl->backend_decl == NULL_TREE)
824 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
826 /* Also prefix the mangled name. */
827 strcpy (&name[1], sym->name);
829 length = build_decl (VAR_DECL, get_identifier (name),
830 gfc_charlen_type_node);
831 DECL_ARTIFICIAL (length) = 1;
832 TREE_USED (length) = 1;
833 if (sym->ns->proc_name->tlink != NULL)
834 gfc_defer_symbol_init (sym);
835 sym->ts.cl->backend_decl = length;
838 return sym->ts.cl->backend_decl;
841 /* If a variable is assigned a label, we add another two auxiliary
845 gfc_add_assign_aux_vars (gfc_symbol * sym)
851 gcc_assert (sym->backend_decl);
853 decl = sym->backend_decl;
854 gfc_allocate_lang_decl (decl);
855 GFC_DECL_ASSIGN (decl) = 1;
856 length = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
857 gfc_charlen_type_node);
858 addr = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
860 gfc_finish_var_decl (length, sym);
861 gfc_finish_var_decl (addr, sym);
862 /* STRING_LENGTH is also used as flag. Less than -1 means that
863 ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the
864 target label's address. Otherwise, value is the length of a format string
865 and ASSIGN_ADDR is its address. */
866 if (TREE_STATIC (length))
867 DECL_INITIAL (length) = build_int_cst (NULL_TREE, -2);
869 gfc_defer_symbol_init (sym);
871 GFC_DECL_STRING_LEN (decl) = length;
872 GFC_DECL_ASSIGN_ADDR (decl) = addr;
875 /* Return the decl for a gfc_symbol, create it if it doesn't already
879 gfc_get_symbol_decl (gfc_symbol * sym)
882 tree length = NULL_TREE;
885 gcc_assert (sym->attr.referenced
886 || sym->attr.use_assoc
887 || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY);
889 if (sym->ns && sym->ns->proc_name->attr.function)
890 byref = gfc_return_by_reference (sym->ns->proc_name);
894 if ((sym->attr.dummy && ! sym->attr.function) || (sym->attr.result && byref))
896 /* Return via extra parameter. */
897 if (sym->attr.result && byref
898 && !sym->backend_decl)
901 DECL_ARGUMENTS (sym->ns->proc_name->backend_decl);
902 /* For entry master function skip over the __entry
904 if (sym->ns->proc_name->attr.entry_master)
905 sym->backend_decl = TREE_CHAIN (sym->backend_decl);
908 /* Dummy variables should already have been created. */
909 gcc_assert (sym->backend_decl);
911 /* Create a character length variable. */
912 if (sym->ts.type == BT_CHARACTER)
914 if (sym->ts.cl->backend_decl == NULL_TREE)
915 length = gfc_create_string_length (sym);
917 length = sym->ts.cl->backend_decl;
918 if (TREE_CODE (length) == VAR_DECL
919 && DECL_CONTEXT (length) == NULL_TREE)
921 /* Add the string length to the same context as the symbol. */
922 if (DECL_CONTEXT (sym->backend_decl) == current_function_decl)
923 gfc_add_decl_to_function (length);
925 gfc_add_decl_to_parent_function (length);
927 gcc_assert (DECL_CONTEXT (sym->backend_decl) ==
928 DECL_CONTEXT (length));
930 gfc_defer_symbol_init (sym);
934 /* Use a copy of the descriptor for dummy arrays. */
935 if (sym->attr.dimension && !TREE_USED (sym->backend_decl))
937 decl = gfc_build_dummy_array_decl (sym, sym->backend_decl);
938 /* Prevent the dummy from being detected as unused if it is copied. */
939 if (sym->backend_decl != NULL && decl != sym->backend_decl)
940 DECL_ARTIFICIAL (sym->backend_decl) = 1;
941 sym->backend_decl = decl;
944 TREE_USED (sym->backend_decl) = 1;
945 if (sym->attr.assign && GFC_DECL_ASSIGN (sym->backend_decl) == 0)
947 gfc_add_assign_aux_vars (sym);
949 return sym->backend_decl;
952 if (sym->backend_decl)
953 return sym->backend_decl;
955 /* Catch function declarations. Only used for actual parameters. */
956 if (sym->attr.flavor == FL_PROCEDURE)
958 decl = gfc_get_extern_function_decl (sym);
962 if (sym->attr.intrinsic)
963 internal_error ("intrinsic variable which isn't a procedure");
965 /* Create string length decl first so that they can be used in the
967 if (sym->ts.type == BT_CHARACTER)
968 length = gfc_create_string_length (sym);
970 /* Create the decl for the variable. */
971 decl = build_decl (VAR_DECL, gfc_sym_identifier (sym), gfc_sym_type (sym));
973 gfc_set_decl_location (decl, &sym->declared_at);
975 /* Symbols from modules should have their assembler names mangled.
976 This is done here rather than in gfc_finish_var_decl because it
977 is different for string length variables. */
979 SET_DECL_ASSEMBLER_NAME (decl, gfc_sym_mangled_identifier (sym));
981 if (sym->attr.dimension)
983 /* Create variables to hold the non-constant bits of array info. */
984 gfc_build_qualified_array (decl, sym);
986 /* Remember this variable for allocation/cleanup. */
987 gfc_defer_symbol_init (sym);
989 if ((sym->attr.allocatable || !sym->attr.dummy) && !sym->attr.pointer)
990 GFC_DECL_PACKED_ARRAY (decl) = 1;
993 if (sym->ts.type == BT_DERIVED && sym->ts.derived->attr.alloc_comp)
994 gfc_defer_symbol_init (sym);
995 /* This applies a derived type default initializer. */
996 else if (sym->ts.type == BT_DERIVED
997 && sym->attr.save == SAVE_NONE
999 && !sym->attr.allocatable
1000 && (sym->value && !sym->ns->proc_name->attr.is_main_program)
1001 && !sym->attr.use_assoc)
1002 gfc_defer_symbol_init (sym);
1004 gfc_finish_var_decl (decl, sym);
1006 if (sym->ts.type == BT_CHARACTER)
1008 /* Character variables need special handling. */
1009 gfc_allocate_lang_decl (decl);
1011 if (TREE_CODE (length) != INTEGER_CST)
1013 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
1017 /* Also prefix the mangled name for symbols from modules. */
1018 strcpy (&name[1], sym->name);
1021 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (length)));
1022 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (name));
1024 gfc_finish_var_decl (length, sym);
1025 gcc_assert (!sym->value);
1028 else if (sym->attr.subref_array_pointer)
1030 /* We need the span for these beasts. */
1031 gfc_allocate_lang_decl (decl);
1034 if (sym->attr.subref_array_pointer)
1037 GFC_DECL_SUBREF_ARRAY_P (decl) = 1;
1038 span = build_decl (VAR_DECL, create_tmp_var_name ("span"),
1039 gfc_array_index_type);
1040 gfc_finish_var_decl (span, sym);
1041 TREE_STATIC (span) = 1;
1042 DECL_INITIAL (span) = build_int_cst (NULL_TREE, 0);
1044 GFC_DECL_SPAN (decl) = span;
1047 sym->backend_decl = decl;
1049 if (sym->attr.assign)
1050 gfc_add_assign_aux_vars (sym);
1052 if (TREE_STATIC (decl) && !sym->attr.use_assoc)
1054 /* Add static initializer. */
1055 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
1056 TREE_TYPE (decl), sym->attr.dimension,
1057 sym->attr.pointer || sym->attr.allocatable);
1064 /* Substitute a temporary variable in place of the real one. */
1067 gfc_shadow_sym (gfc_symbol * sym, tree decl, gfc_saved_var * save)
1069 save->attr = sym->attr;
1070 save->decl = sym->backend_decl;
1072 gfc_clear_attr (&sym->attr);
1073 sym->attr.referenced = 1;
1074 sym->attr.flavor = FL_VARIABLE;
1076 sym->backend_decl = decl;
1080 /* Restore the original variable. */
1083 gfc_restore_sym (gfc_symbol * sym, gfc_saved_var * save)
1085 sym->attr = save->attr;
1086 sym->backend_decl = save->decl;
1090 /* Get a basic decl for an external function. */
1093 gfc_get_extern_function_decl (gfc_symbol * sym)
1098 gfc_intrinsic_sym *isym;
1100 char s[GFC_MAX_SYMBOL_LEN + 23]; /* "_gfortran_f2c_specific" and '\0'. */
1104 if (sym->backend_decl)
1105 return sym->backend_decl;
1107 /* We should never be creating external decls for alternate entry points.
1108 The procedure may be an alternate entry point, but we don't want/need
1110 gcc_assert (!(sym->attr.entry || sym->attr.entry_master));
1112 if (sym->attr.intrinsic)
1114 /* Call the resolution function to get the actual name. This is
1115 a nasty hack which relies on the resolution functions only looking
1116 at the first argument. We pass NULL for the second argument
1117 otherwise things like AINT get confused. */
1118 isym = gfc_find_function (sym->name);
1119 gcc_assert (isym->resolve.f0 != NULL);
1121 memset (&e, 0, sizeof (e));
1122 e.expr_type = EXPR_FUNCTION;
1124 memset (&argexpr, 0, sizeof (argexpr));
1125 gcc_assert (isym->formal);
1126 argexpr.ts = isym->formal->ts;
1128 if (isym->formal->next == NULL)
1129 isym->resolve.f1 (&e, &argexpr);
1132 if (isym->formal->next->next == NULL)
1133 isym->resolve.f2 (&e, &argexpr, NULL);
1136 if (isym->formal->next->next->next == NULL)
1137 isym->resolve.f3 (&e, &argexpr, NULL, NULL);
1140 /* All specific intrinsics take less than 5 arguments. */
1141 gcc_assert (isym->formal->next->next->next->next == NULL);
1142 isym->resolve.f4 (&e, &argexpr, NULL, NULL, NULL);
1147 if (gfc_option.flag_f2c
1148 && ((e.ts.type == BT_REAL && e.ts.kind == gfc_default_real_kind)
1149 || e.ts.type == BT_COMPLEX))
1151 /* Specific which needs a different implementation if f2c
1152 calling conventions are used. */
1153 sprintf (s, "_gfortran_f2c_specific%s", e.value.function.name);
1156 sprintf (s, "_gfortran_specific%s", e.value.function.name);
1158 name = get_identifier (s);
1159 mangled_name = name;
1163 name = gfc_sym_identifier (sym);
1164 mangled_name = gfc_sym_mangled_function_id (sym);
1167 type = gfc_get_function_type (sym);
1168 fndecl = build_decl (FUNCTION_DECL, name, type);
1170 SET_DECL_ASSEMBLER_NAME (fndecl, mangled_name);
1171 /* If the return type is a pointer, avoid alias issues by setting
1172 DECL_IS_MALLOC to nonzero. This means that the function should be
1173 treated as if it were a malloc, meaning it returns a pointer that
1175 if (POINTER_TYPE_P (type))
1176 DECL_IS_MALLOC (fndecl) = 1;
1178 /* Set the context of this decl. */
1179 if (0 && sym->ns && sym->ns->proc_name)
1181 /* TODO: Add external decls to the appropriate scope. */
1182 DECL_CONTEXT (fndecl) = sym->ns->proc_name->backend_decl;
1186 /* Global declaration, e.g. intrinsic subroutine. */
1187 DECL_CONTEXT (fndecl) = NULL_TREE;
1190 DECL_EXTERNAL (fndecl) = 1;
1192 /* This specifies if a function is globally addressable, i.e. it is
1193 the opposite of declaring static in C. */
1194 TREE_PUBLIC (fndecl) = 1;
1196 /* Set attributes for PURE functions. A call to PURE function in the
1197 Fortran 95 sense is both pure and without side effects in the C
1199 if (sym->attr.pure || sym->attr.elemental)
1201 if (sym->attr.function && !gfc_return_by_reference (sym))
1202 DECL_IS_PURE (fndecl) = 1;
1203 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
1204 parameters and don't use alternate returns (is this
1205 allowed?). In that case, calls to them are meaningless, and
1206 can be optimized away. See also in build_function_decl(). */
1207 TREE_SIDE_EFFECTS (fndecl) = 0;
1210 /* Mark non-returning functions. */
1211 if (sym->attr.noreturn)
1212 TREE_THIS_VOLATILE(fndecl) = 1;
1214 sym->backend_decl = fndecl;
1216 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1217 pushdecl_top_level (fndecl);
1223 /* Create a declaration for a procedure. For external functions (in the C
1224 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
1225 a master function with alternate entry points. */
1228 build_function_decl (gfc_symbol * sym)
1231 symbol_attribute attr;
1233 gfc_formal_arglist *f;
1235 gcc_assert (!sym->backend_decl);
1236 gcc_assert (!sym->attr.external);
1238 /* Set the line and filename. sym->declared_at seems to point to the
1239 last statement for subroutines, but it'll do for now. */
1240 gfc_set_backend_locus (&sym->declared_at);
1242 /* Allow only one nesting level. Allow public declarations. */
1243 gcc_assert (current_function_decl == NULL_TREE
1244 || DECL_CONTEXT (current_function_decl) == NULL_TREE);
1246 type = gfc_get_function_type (sym);
1247 fndecl = build_decl (FUNCTION_DECL, gfc_sym_identifier (sym), type);
1249 /* Perform name mangling if this is a top level or module procedure. */
1250 if (current_function_decl == NULL_TREE)
1251 SET_DECL_ASSEMBLER_NAME (fndecl, gfc_sym_mangled_function_id (sym));
1253 /* Figure out the return type of the declared function, and build a
1254 RESULT_DECL for it. If this is a subroutine with alternate
1255 returns, build a RESULT_DECL for it. */
1258 result_decl = NULL_TREE;
1259 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
1262 if (gfc_return_by_reference (sym))
1263 type = void_type_node;
1266 if (sym->result != sym)
1267 result_decl = gfc_sym_identifier (sym->result);
1269 type = TREE_TYPE (TREE_TYPE (fndecl));
1274 /* Look for alternate return placeholders. */
1275 int has_alternate_returns = 0;
1276 for (f = sym->formal; f; f = f->next)
1280 has_alternate_returns = 1;
1285 if (has_alternate_returns)
1286 type = integer_type_node;
1288 type = void_type_node;
1291 result_decl = build_decl (RESULT_DECL, result_decl, type);
1292 DECL_ARTIFICIAL (result_decl) = 1;
1293 DECL_IGNORED_P (result_decl) = 1;
1294 DECL_CONTEXT (result_decl) = fndecl;
1295 DECL_RESULT (fndecl) = result_decl;
1297 /* Don't call layout_decl for a RESULT_DECL.
1298 layout_decl (result_decl, 0); */
1300 /* If the return type is a pointer, avoid alias issues by setting
1301 DECL_IS_MALLOC to nonzero. This means that the function should be
1302 treated as if it were a malloc, meaning it returns a pointer that
1304 if (POINTER_TYPE_P (type))
1305 DECL_IS_MALLOC (fndecl) = 1;
1307 /* Set up all attributes for the function. */
1308 DECL_CONTEXT (fndecl) = current_function_decl;
1309 DECL_EXTERNAL (fndecl) = 0;
1311 /* This specifies if a function is globally visible, i.e. it is
1312 the opposite of declaring static in C. */
1313 if (DECL_CONTEXT (fndecl) == NULL_TREE
1314 && !sym->attr.entry_master)
1315 TREE_PUBLIC (fndecl) = 1;
1317 /* TREE_STATIC means the function body is defined here. */
1318 TREE_STATIC (fndecl) = 1;
1320 /* Set attributes for PURE functions. A call to a PURE function in the
1321 Fortran 95 sense is both pure and without side effects in the C
1323 if (attr.pure || attr.elemental)
1325 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
1326 including an alternate return. In that case it can also be
1327 marked as PURE. See also in gfc_get_extern_function_decl(). */
1328 if (attr.function && !gfc_return_by_reference (sym))
1329 DECL_IS_PURE (fndecl) = 1;
1330 TREE_SIDE_EFFECTS (fndecl) = 0;
1333 /* For -fwhole-program to work well, the main program needs to have the
1334 "externally_visible" attribute. */
1335 if (attr.is_main_program)
1336 DECL_ATTRIBUTES (fndecl)
1337 = tree_cons (get_identifier("externally_visible"), NULL_TREE, NULL_TREE);
1339 /* Layout the function declaration and put it in the binding level
1340 of the current function. */
1343 sym->backend_decl = fndecl;
1347 /* Create the DECL_ARGUMENTS for a procedure. */
1350 create_function_arglist (gfc_symbol * sym)
1353 gfc_formal_arglist *f;
1354 tree typelist, hidden_typelist;
1355 tree arglist, hidden_arglist;
1359 fndecl = sym->backend_decl;
1361 /* Build formal argument list. Make sure that their TREE_CONTEXT is
1362 the new FUNCTION_DECL node. */
1363 arglist = NULL_TREE;
1364 hidden_arglist = NULL_TREE;
1365 typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1367 if (sym->attr.entry_master)
1369 type = TREE_VALUE (typelist);
1370 parm = build_decl (PARM_DECL, get_identifier ("__entry"), type);
1372 DECL_CONTEXT (parm) = fndecl;
1373 DECL_ARG_TYPE (parm) = type;
1374 TREE_READONLY (parm) = 1;
1375 gfc_finish_decl (parm);
1376 DECL_ARTIFICIAL (parm) = 1;
1378 arglist = chainon (arglist, parm);
1379 typelist = TREE_CHAIN (typelist);
1382 if (gfc_return_by_reference (sym))
1384 tree type = TREE_VALUE (typelist), length = NULL;
1386 if (sym->ts.type == BT_CHARACTER)
1388 /* Length of character result. */
1389 tree len_type = TREE_VALUE (TREE_CHAIN (typelist));
1390 gcc_assert (len_type == gfc_charlen_type_node);
1392 length = build_decl (PARM_DECL,
1393 get_identifier (".__result"),
1395 if (!sym->ts.cl->length)
1397 sym->ts.cl->backend_decl = length;
1398 TREE_USED (length) = 1;
1400 gcc_assert (TREE_CODE (length) == PARM_DECL);
1401 DECL_CONTEXT (length) = fndecl;
1402 DECL_ARG_TYPE (length) = len_type;
1403 TREE_READONLY (length) = 1;
1404 DECL_ARTIFICIAL (length) = 1;
1405 gfc_finish_decl (length);
1406 if (sym->ts.cl->backend_decl == NULL
1407 || sym->ts.cl->backend_decl == length)
1412 if (sym->ts.cl->backend_decl == NULL)
1414 tree len = build_decl (VAR_DECL,
1415 get_identifier ("..__result"),
1416 gfc_charlen_type_node);
1417 DECL_ARTIFICIAL (len) = 1;
1418 TREE_USED (len) = 1;
1419 sym->ts.cl->backend_decl = len;
1422 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1423 arg = sym->result ? sym->result : sym;
1424 backend_decl = arg->backend_decl;
1425 /* Temporary clear it, so that gfc_sym_type creates complete
1427 arg->backend_decl = NULL;
1428 type = gfc_sym_type (arg);
1429 arg->backend_decl = backend_decl;
1430 type = build_reference_type (type);
1434 parm = build_decl (PARM_DECL, get_identifier ("__result"), type);
1436 DECL_CONTEXT (parm) = fndecl;
1437 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
1438 TREE_READONLY (parm) = 1;
1439 DECL_ARTIFICIAL (parm) = 1;
1440 gfc_finish_decl (parm);
1442 arglist = chainon (arglist, parm);
1443 typelist = TREE_CHAIN (typelist);
1445 if (sym->ts.type == BT_CHARACTER)
1447 gfc_allocate_lang_decl (parm);
1448 arglist = chainon (arglist, length);
1449 typelist = TREE_CHAIN (typelist);
1453 hidden_typelist = typelist;
1454 for (f = sym->formal; f; f = f->next)
1455 if (f->sym != NULL) /* Ignore alternate returns. */
1456 hidden_typelist = TREE_CHAIN (hidden_typelist);
1458 for (f = sym->formal; f; f = f->next)
1460 char name[GFC_MAX_SYMBOL_LEN + 2];
1462 /* Ignore alternate returns. */
1466 type = TREE_VALUE (typelist);
1468 if (f->sym->ts.type == BT_CHARACTER)
1470 tree len_type = TREE_VALUE (hidden_typelist);
1471 tree length = NULL_TREE;
1472 gcc_assert (len_type == gfc_charlen_type_node);
1474 strcpy (&name[1], f->sym->name);
1476 length = build_decl (PARM_DECL, get_identifier (name), len_type);
1478 hidden_arglist = chainon (hidden_arglist, length);
1479 DECL_CONTEXT (length) = fndecl;
1480 DECL_ARTIFICIAL (length) = 1;
1481 DECL_ARG_TYPE (length) = len_type;
1482 TREE_READONLY (length) = 1;
1483 gfc_finish_decl (length);
1485 /* TODO: Check string lengths when -fbounds-check. */
1487 /* Use the passed value for assumed length variables. */
1488 if (!f->sym->ts.cl->length)
1490 TREE_USED (length) = 1;
1491 gcc_assert (!f->sym->ts.cl->backend_decl);
1492 f->sym->ts.cl->backend_decl = length;
1495 hidden_typelist = TREE_CHAIN (hidden_typelist);
1497 if (f->sym->ts.cl->backend_decl == NULL
1498 || f->sym->ts.cl->backend_decl == length)
1500 if (f->sym->ts.cl->backend_decl == NULL)
1501 gfc_create_string_length (f->sym);
1503 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1504 if (f->sym->attr.flavor == FL_PROCEDURE)
1505 type = build_pointer_type (gfc_get_function_type (f->sym));
1507 type = gfc_sym_type (f->sym);
1511 /* For non-constant length array arguments, make sure they use
1512 a different type node from TYPE_ARG_TYPES type. */
1513 if (f->sym->attr.dimension
1514 && type == TREE_VALUE (typelist)
1515 && TREE_CODE (type) == POINTER_TYPE
1516 && GFC_ARRAY_TYPE_P (type)
1517 && f->sym->as->type != AS_ASSUMED_SIZE
1518 && ! COMPLETE_TYPE_P (TREE_TYPE (type)))
1520 if (f->sym->attr.flavor == FL_PROCEDURE)
1521 type = build_pointer_type (gfc_get_function_type (f->sym));
1523 type = gfc_sym_type (f->sym);
1526 /* Build a the argument declaration. */
1527 parm = build_decl (PARM_DECL, gfc_sym_identifier (f->sym), type);
1529 /* Fill in arg stuff. */
1530 DECL_CONTEXT (parm) = fndecl;
1531 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
1532 /* All implementation args are read-only. */
1533 TREE_READONLY (parm) = 1;
1535 gfc_finish_decl (parm);
1537 f->sym->backend_decl = parm;
1539 arglist = chainon (arglist, parm);
1540 typelist = TREE_CHAIN (typelist);
1543 /* Add the hidden string length parameters, unless the procedure
1545 if (!sym->attr.is_bind_c)
1546 arglist = chainon (arglist, hidden_arglist);
1548 gcc_assert (hidden_typelist == NULL_TREE
1549 || TREE_VALUE (hidden_typelist) == void_type_node);
1550 DECL_ARGUMENTS (fndecl) = arglist;
1553 /* Convert FNDECL's code to GIMPLE and handle any nested functions. */
1556 gfc_gimplify_function (tree fndecl)
1558 struct cgraph_node *cgn;
1560 gimplify_function_tree (fndecl);
1561 dump_function (TDI_generic, fndecl);
1563 /* Generate errors for structured block violations. */
1564 /* ??? Could be done as part of resolve_labels. */
1566 diagnose_omp_structured_block_errors (fndecl);
1568 /* Convert all nested functions to GIMPLE now. We do things in this order
1569 so that items like VLA sizes are expanded properly in the context of the
1570 correct function. */
1571 cgn = cgraph_node (fndecl);
1572 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1573 gfc_gimplify_function (cgn->decl);
1577 /* Do the setup necessary before generating the body of a function. */
1580 trans_function_start (gfc_symbol * sym)
1584 fndecl = sym->backend_decl;
1586 /* Let GCC know the current scope is this function. */
1587 current_function_decl = fndecl;
1589 /* Let the world know what we're about to do. */
1590 announce_function (fndecl);
1592 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1594 /* Create RTL for function declaration. */
1595 rest_of_decl_compilation (fndecl, 1, 0);
1598 /* Create RTL for function definition. */
1599 make_decl_rtl (fndecl);
1601 init_function_start (fndecl);
1603 /* Even though we're inside a function body, we still don't want to
1604 call expand_expr to calculate the size of a variable-sized array.
1605 We haven't necessarily assigned RTL to all variables yet, so it's
1606 not safe to try to expand expressions involving them. */
1607 cfun->x_dont_save_pending_sizes_p = 1;
1609 /* function.c requires a push at the start of the function. */
1613 /* Create thunks for alternate entry points. */
1616 build_entry_thunks (gfc_namespace * ns)
1618 gfc_formal_arglist *formal;
1619 gfc_formal_arglist *thunk_formal;
1621 gfc_symbol *thunk_sym;
1629 /* This should always be a toplevel function. */
1630 gcc_assert (current_function_decl == NULL_TREE);
1632 gfc_get_backend_locus (&old_loc);
1633 for (el = ns->entries; el; el = el->next)
1635 thunk_sym = el->sym;
1637 build_function_decl (thunk_sym);
1638 create_function_arglist (thunk_sym);
1640 trans_function_start (thunk_sym);
1642 thunk_fndecl = thunk_sym->backend_decl;
1644 gfc_start_block (&body);
1646 /* Pass extra parameter identifying this entry point. */
1647 tmp = build_int_cst (gfc_array_index_type, el->id);
1648 args = tree_cons (NULL_TREE, tmp, NULL_TREE);
1649 string_args = NULL_TREE;
1651 if (thunk_sym->attr.function)
1653 if (gfc_return_by_reference (ns->proc_name))
1655 tree ref = DECL_ARGUMENTS (current_function_decl);
1656 args = tree_cons (NULL_TREE, ref, args);
1657 if (ns->proc_name->ts.type == BT_CHARACTER)
1658 args = tree_cons (NULL_TREE, TREE_CHAIN (ref),
1663 for (formal = ns->proc_name->formal; formal; formal = formal->next)
1665 /* Ignore alternate returns. */
1666 if (formal->sym == NULL)
1669 /* We don't have a clever way of identifying arguments, so resort to
1670 a brute-force search. */
1671 for (thunk_formal = thunk_sym->formal;
1673 thunk_formal = thunk_formal->next)
1675 if (thunk_formal->sym == formal->sym)
1681 /* Pass the argument. */
1682 DECL_ARTIFICIAL (thunk_formal->sym->backend_decl) = 1;
1683 args = tree_cons (NULL_TREE, thunk_formal->sym->backend_decl,
1685 if (formal->sym->ts.type == BT_CHARACTER)
1687 tmp = thunk_formal->sym->ts.cl->backend_decl;
1688 string_args = tree_cons (NULL_TREE, tmp, string_args);
1693 /* Pass NULL for a missing argument. */
1694 args = tree_cons (NULL_TREE, null_pointer_node, args);
1695 if (formal->sym->ts.type == BT_CHARACTER)
1697 tmp = build_int_cst (gfc_charlen_type_node, 0);
1698 string_args = tree_cons (NULL_TREE, tmp, string_args);
1703 /* Call the master function. */
1704 args = nreverse (args);
1705 args = chainon (args, nreverse (string_args));
1706 tmp = ns->proc_name->backend_decl;
1707 tmp = build_function_call_expr (tmp, args);
1708 if (ns->proc_name->attr.mixed_entry_master)
1710 tree union_decl, field;
1711 tree master_type = TREE_TYPE (ns->proc_name->backend_decl);
1713 union_decl = build_decl (VAR_DECL, get_identifier ("__result"),
1714 TREE_TYPE (master_type));
1715 DECL_ARTIFICIAL (union_decl) = 1;
1716 DECL_EXTERNAL (union_decl) = 0;
1717 TREE_PUBLIC (union_decl) = 0;
1718 TREE_USED (union_decl) = 1;
1719 layout_decl (union_decl, 0);
1720 pushdecl (union_decl);
1722 DECL_CONTEXT (union_decl) = current_function_decl;
1723 tmp = fold_build2 (MODIFY_EXPR, TREE_TYPE (union_decl),
1725 gfc_add_expr_to_block (&body, tmp);
1727 for (field = TYPE_FIELDS (TREE_TYPE (union_decl));
1728 field; field = TREE_CHAIN (field))
1729 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
1730 thunk_sym->result->name) == 0)
1732 gcc_assert (field != NULL_TREE);
1733 tmp = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
1734 union_decl, field, NULL_TREE);
1735 tmp = fold_build2 (MODIFY_EXPR,
1736 TREE_TYPE (DECL_RESULT (current_function_decl)),
1737 DECL_RESULT (current_function_decl), tmp);
1738 tmp = build1_v (RETURN_EXPR, tmp);
1740 else if (TREE_TYPE (DECL_RESULT (current_function_decl))
1743 tmp = fold_build2 (MODIFY_EXPR,
1744 TREE_TYPE (DECL_RESULT (current_function_decl)),
1745 DECL_RESULT (current_function_decl), tmp);
1746 tmp = build1_v (RETURN_EXPR, tmp);
1748 gfc_add_expr_to_block (&body, tmp);
1750 /* Finish off this function and send it for code generation. */
1751 DECL_SAVED_TREE (thunk_fndecl) = gfc_finish_block (&body);
1753 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl)) = thunk_fndecl;
1755 /* Output the GENERIC tree. */
1756 dump_function (TDI_original, thunk_fndecl);
1758 /* Store the end of the function, so that we get good line number
1759 info for the epilogue. */
1760 cfun->function_end_locus = input_location;
1762 /* We're leaving the context of this function, so zap cfun.
1763 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
1764 tree_rest_of_compilation. */
1767 current_function_decl = NULL_TREE;
1769 gfc_gimplify_function (thunk_fndecl);
1770 cgraph_finalize_function (thunk_fndecl, false);
1772 /* We share the symbols in the formal argument list with other entry
1773 points and the master function. Clear them so that they are
1774 recreated for each function. */
1775 for (formal = thunk_sym->formal; formal; formal = formal->next)
1776 if (formal->sym != NULL) /* Ignore alternate returns. */
1778 formal->sym->backend_decl = NULL_TREE;
1779 if (formal->sym->ts.type == BT_CHARACTER)
1780 formal->sym->ts.cl->backend_decl = NULL_TREE;
1783 if (thunk_sym->attr.function)
1785 if (thunk_sym->ts.type == BT_CHARACTER)
1786 thunk_sym->ts.cl->backend_decl = NULL_TREE;
1787 if (thunk_sym->result->ts.type == BT_CHARACTER)
1788 thunk_sym->result->ts.cl->backend_decl = NULL_TREE;
1792 gfc_set_backend_locus (&old_loc);
1796 /* Create a decl for a function, and create any thunks for alternate entry
1800 gfc_create_function_decl (gfc_namespace * ns)
1802 /* Create a declaration for the master function. */
1803 build_function_decl (ns->proc_name);
1805 /* Compile the entry thunks. */
1807 build_entry_thunks (ns);
1809 /* Now create the read argument list. */
1810 create_function_arglist (ns->proc_name);
1813 /* Return the decl used to hold the function return value. If
1814 parent_flag is set, the context is the parent_scope. */
1817 gfc_get_fake_result_decl (gfc_symbol * sym, int parent_flag)
1821 tree this_fake_result_decl;
1822 tree this_function_decl;
1824 char name[GFC_MAX_SYMBOL_LEN + 10];
1828 this_fake_result_decl = parent_fake_result_decl;
1829 this_function_decl = DECL_CONTEXT (current_function_decl);
1833 this_fake_result_decl = current_fake_result_decl;
1834 this_function_decl = current_function_decl;
1838 && sym->ns->proc_name->backend_decl == this_function_decl
1839 && sym->ns->proc_name->attr.entry_master
1840 && sym != sym->ns->proc_name)
1843 if (this_fake_result_decl != NULL)
1844 for (t = TREE_CHAIN (this_fake_result_decl); t; t = TREE_CHAIN (t))
1845 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t)), sym->name) == 0)
1848 return TREE_VALUE (t);
1849 decl = gfc_get_fake_result_decl (sym->ns->proc_name, parent_flag);
1852 this_fake_result_decl = parent_fake_result_decl;
1854 this_fake_result_decl = current_fake_result_decl;
1856 if (decl && sym->ns->proc_name->attr.mixed_entry_master)
1860 for (field = TYPE_FIELDS (TREE_TYPE (decl));
1861 field; field = TREE_CHAIN (field))
1862 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
1866 gcc_assert (field != NULL_TREE);
1867 decl = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
1868 decl, field, NULL_TREE);
1871 var = create_tmp_var_raw (TREE_TYPE (decl), sym->name);
1873 gfc_add_decl_to_parent_function (var);
1875 gfc_add_decl_to_function (var);
1877 SET_DECL_VALUE_EXPR (var, decl);
1878 DECL_HAS_VALUE_EXPR_P (var) = 1;
1879 GFC_DECL_RESULT (var) = 1;
1881 TREE_CHAIN (this_fake_result_decl)
1882 = tree_cons (get_identifier (sym->name), var,
1883 TREE_CHAIN (this_fake_result_decl));
1887 if (this_fake_result_decl != NULL_TREE)
1888 return TREE_VALUE (this_fake_result_decl);
1890 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
1895 if (sym->ts.type == BT_CHARACTER)
1897 if (sym->ts.cl->backend_decl == NULL_TREE)
1898 length = gfc_create_string_length (sym);
1900 length = sym->ts.cl->backend_decl;
1901 if (TREE_CODE (length) == VAR_DECL
1902 && DECL_CONTEXT (length) == NULL_TREE)
1903 gfc_add_decl_to_function (length);
1906 if (gfc_return_by_reference (sym))
1908 decl = DECL_ARGUMENTS (this_function_decl);
1910 if (sym->ns->proc_name->backend_decl == this_function_decl
1911 && sym->ns->proc_name->attr.entry_master)
1912 decl = TREE_CHAIN (decl);
1914 TREE_USED (decl) = 1;
1916 decl = gfc_build_dummy_array_decl (sym, decl);
1920 sprintf (name, "__result_%.20s",
1921 IDENTIFIER_POINTER (DECL_NAME (this_function_decl)));
1923 if (!sym->attr.mixed_entry_master && sym->attr.function)
1924 decl = build_decl (VAR_DECL, get_identifier (name),
1925 gfc_sym_type (sym));
1927 decl = build_decl (VAR_DECL, get_identifier (name),
1928 TREE_TYPE (TREE_TYPE (this_function_decl)));
1929 DECL_ARTIFICIAL (decl) = 1;
1930 DECL_EXTERNAL (decl) = 0;
1931 TREE_PUBLIC (decl) = 0;
1932 TREE_USED (decl) = 1;
1933 GFC_DECL_RESULT (decl) = 1;
1934 TREE_ADDRESSABLE (decl) = 1;
1936 layout_decl (decl, 0);
1939 gfc_add_decl_to_parent_function (decl);
1941 gfc_add_decl_to_function (decl);
1945 parent_fake_result_decl = build_tree_list (NULL, decl);
1947 current_fake_result_decl = build_tree_list (NULL, decl);
1953 /* Builds a function decl. The remaining parameters are the types of the
1954 function arguments. Negative nargs indicates a varargs function. */
1957 gfc_build_library_function_decl (tree name, tree rettype, int nargs, ...)
1966 /* Library functions must be declared with global scope. */
1967 gcc_assert (current_function_decl == NULL_TREE);
1969 va_start (p, nargs);
1972 /* Create a list of the argument types. */
1973 for (arglist = NULL_TREE, n = abs (nargs); n > 0; n--)
1975 argtype = va_arg (p, tree);
1976 arglist = gfc_chainon_list (arglist, argtype);
1981 /* Terminate the list. */
1982 arglist = gfc_chainon_list (arglist, void_type_node);
1985 /* Build the function type and decl. */
1986 fntype = build_function_type (rettype, arglist);
1987 fndecl = build_decl (FUNCTION_DECL, name, fntype);
1989 /* Mark this decl as external. */
1990 DECL_EXTERNAL (fndecl) = 1;
1991 TREE_PUBLIC (fndecl) = 1;
1997 rest_of_decl_compilation (fndecl, 1, 0);
2003 gfc_build_intrinsic_function_decls (void)
2005 tree gfc_int4_type_node = gfc_get_int_type (4);
2006 tree gfc_int8_type_node = gfc_get_int_type (8);
2007 tree gfc_int16_type_node = gfc_get_int_type (16);
2008 tree gfc_logical4_type_node = gfc_get_logical_type (4);
2010 /* String functions. */
2011 gfor_fndecl_compare_string =
2012 gfc_build_library_function_decl (get_identifier (PREFIX("compare_string")),
2013 integer_type_node, 4,
2014 gfc_charlen_type_node, pchar_type_node,
2015 gfc_charlen_type_node, pchar_type_node);
2017 gfor_fndecl_concat_string =
2018 gfc_build_library_function_decl (get_identifier (PREFIX("concat_string")),
2021 gfc_charlen_type_node, pchar_type_node,
2022 gfc_charlen_type_node, pchar_type_node,
2023 gfc_charlen_type_node, pchar_type_node);
2025 gfor_fndecl_string_len_trim =
2026 gfc_build_library_function_decl (get_identifier (PREFIX("string_len_trim")),
2028 2, gfc_charlen_type_node,
2031 gfor_fndecl_string_index =
2032 gfc_build_library_function_decl (get_identifier (PREFIX("string_index")),
2034 5, gfc_charlen_type_node, pchar_type_node,
2035 gfc_charlen_type_node, pchar_type_node,
2036 gfc_logical4_type_node);
2038 gfor_fndecl_string_scan =
2039 gfc_build_library_function_decl (get_identifier (PREFIX("string_scan")),
2041 5, gfc_charlen_type_node, pchar_type_node,
2042 gfc_charlen_type_node, pchar_type_node,
2043 gfc_logical4_type_node);
2045 gfor_fndecl_string_verify =
2046 gfc_build_library_function_decl (get_identifier (PREFIX("string_verify")),
2048 5, gfc_charlen_type_node, pchar_type_node,
2049 gfc_charlen_type_node, pchar_type_node,
2050 gfc_logical4_type_node);
2052 gfor_fndecl_string_trim =
2053 gfc_build_library_function_decl (get_identifier (PREFIX("string_trim")),
2056 build_pointer_type (gfc_charlen_type_node),
2058 gfc_charlen_type_node,
2061 gfor_fndecl_string_minmax =
2062 gfc_build_library_function_decl (get_identifier (PREFIX("string_minmax")),
2064 build_pointer_type (gfc_charlen_type_node),
2065 ppvoid_type_node, integer_type_node,
2068 gfor_fndecl_ttynam =
2069 gfc_build_library_function_decl (get_identifier (PREFIX("ttynam")),
2073 gfc_charlen_type_node,
2077 gfc_build_library_function_decl (get_identifier (PREFIX("fdate")),
2081 gfc_charlen_type_node);
2084 gfc_build_library_function_decl (get_identifier (PREFIX("ctime")),
2088 gfc_charlen_type_node,
2089 gfc_int8_type_node);
2091 gfor_fndecl_adjustl =
2092 gfc_build_library_function_decl (get_identifier (PREFIX("adjustl")),
2096 gfc_charlen_type_node, pchar_type_node);
2098 gfor_fndecl_adjustr =
2099 gfc_build_library_function_decl (get_identifier (PREFIX("adjustr")),
2103 gfc_charlen_type_node, pchar_type_node);
2105 gfor_fndecl_si_kind =
2106 gfc_build_library_function_decl (get_identifier
2107 (PREFIX("selected_int_kind")),
2112 gfor_fndecl_sr_kind =
2113 gfc_build_library_function_decl (get_identifier
2114 (PREFIX("selected_real_kind")),
2119 /* Power functions. */
2121 tree ctype, rtype, itype, jtype;
2122 int rkind, ikind, jkind;
2125 static int ikinds[NIKINDS] = {4, 8, 16};
2126 static int rkinds[NRKINDS] = {4, 8, 10, 16};
2127 char name[PREFIX_LEN + 12]; /* _gfortran_pow_?n_?n */
2129 for (ikind=0; ikind < NIKINDS; ikind++)
2131 itype = gfc_get_int_type (ikinds[ikind]);
2133 for (jkind=0; jkind < NIKINDS; jkind++)
2135 jtype = gfc_get_int_type (ikinds[jkind]);
2138 sprintf(name, PREFIX("pow_i%d_i%d"), ikinds[ikind],
2140 gfor_fndecl_math_powi[jkind][ikind].integer =
2141 gfc_build_library_function_decl (get_identifier (name),
2142 jtype, 2, jtype, itype);
2143 TREE_READONLY (gfor_fndecl_math_powi[jkind][ikind].integer) = 1;
2147 for (rkind = 0; rkind < NRKINDS; rkind ++)
2149 rtype = gfc_get_real_type (rkinds[rkind]);
2152 sprintf(name, PREFIX("pow_r%d_i%d"), rkinds[rkind],
2154 gfor_fndecl_math_powi[rkind][ikind].real =
2155 gfc_build_library_function_decl (get_identifier (name),
2156 rtype, 2, rtype, itype);
2157 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].real) = 1;
2160 ctype = gfc_get_complex_type (rkinds[rkind]);
2163 sprintf(name, PREFIX("pow_c%d_i%d"), rkinds[rkind],
2165 gfor_fndecl_math_powi[rkind][ikind].cmplx =
2166 gfc_build_library_function_decl (get_identifier (name),
2167 ctype, 2,ctype, itype);
2168 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1;
2176 gfor_fndecl_math_ishftc4 =
2177 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc4")),
2179 3, gfc_int4_type_node,
2180 gfc_int4_type_node, gfc_int4_type_node);
2181 gfor_fndecl_math_ishftc8 =
2182 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc8")),
2184 3, gfc_int8_type_node,
2185 gfc_int4_type_node, gfc_int4_type_node);
2186 if (gfc_int16_type_node)
2187 gfor_fndecl_math_ishftc16 =
2188 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc16")),
2189 gfc_int16_type_node, 3,
2190 gfc_int16_type_node,
2192 gfc_int4_type_node);
2194 /* BLAS functions. */
2196 tree pint = build_pointer_type (integer_type_node);
2197 tree ps = build_pointer_type (gfc_get_real_type (gfc_default_real_kind));
2198 tree pd = build_pointer_type (gfc_get_real_type (gfc_default_double_kind));
2199 tree pc = build_pointer_type (gfc_get_complex_type (gfc_default_real_kind));
2200 tree pz = build_pointer_type
2201 (gfc_get_complex_type (gfc_default_double_kind));
2203 gfor_fndecl_sgemm = gfc_build_library_function_decl
2205 (gfc_option.flag_underscoring ? "sgemm_"
2207 void_type_node, 15, pchar_type_node,
2208 pchar_type_node, pint, pint, pint, ps, ps, pint,
2209 ps, pint, ps, ps, pint, integer_type_node,
2211 gfor_fndecl_dgemm = gfc_build_library_function_decl
2213 (gfc_option.flag_underscoring ? "dgemm_"
2215 void_type_node, 15, pchar_type_node,
2216 pchar_type_node, pint, pint, pint, pd, pd, pint,
2217 pd, pint, pd, pd, pint, integer_type_node,
2219 gfor_fndecl_cgemm = gfc_build_library_function_decl
2221 (gfc_option.flag_underscoring ? "cgemm_"
2223 void_type_node, 15, pchar_type_node,
2224 pchar_type_node, pint, pint, pint, pc, pc, pint,
2225 pc, pint, pc, pc, pint, integer_type_node,
2227 gfor_fndecl_zgemm = gfc_build_library_function_decl
2229 (gfc_option.flag_underscoring ? "zgemm_"
2231 void_type_node, 15, pchar_type_node,
2232 pchar_type_node, pint, pint, pint, pz, pz, pint,
2233 pz, pint, pz, pz, pint, integer_type_node,
2237 /* Other functions. */
2239 gfc_build_library_function_decl (get_identifier (PREFIX("size0")),
2240 gfc_array_index_type,
2241 1, pvoid_type_node);
2243 gfc_build_library_function_decl (get_identifier (PREFIX("size1")),
2244 gfc_array_index_type,
2246 gfc_array_index_type);
2249 gfc_build_library_function_decl (get_identifier (PREFIX ("iargc")),
2255 /* Make prototypes for runtime library functions. */
2258 gfc_build_builtin_function_decls (void)
2260 tree gfc_int4_type_node = gfc_get_int_type (4);
2262 gfor_fndecl_stop_numeric =
2263 gfc_build_library_function_decl (get_identifier (PREFIX("stop_numeric")),
2264 void_type_node, 1, gfc_int4_type_node);
2265 /* Stop doesn't return. */
2266 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric) = 1;
2268 gfor_fndecl_stop_string =
2269 gfc_build_library_function_decl (get_identifier (PREFIX("stop_string")),
2270 void_type_node, 2, pchar_type_node,
2271 gfc_int4_type_node);
2272 /* Stop doesn't return. */
2273 TREE_THIS_VOLATILE (gfor_fndecl_stop_string) = 1;
2275 gfor_fndecl_pause_numeric =
2276 gfc_build_library_function_decl (get_identifier (PREFIX("pause_numeric")),
2277 void_type_node, 1, gfc_int4_type_node);
2279 gfor_fndecl_pause_string =
2280 gfc_build_library_function_decl (get_identifier (PREFIX("pause_string")),
2281 void_type_node, 2, pchar_type_node,
2282 gfc_int4_type_node);
2284 gfor_fndecl_select_string =
2285 gfc_build_library_function_decl (get_identifier (PREFIX("select_string")),
2286 integer_type_node, 4, pvoid_type_node,
2287 integer_type_node, pchar_type_node,
2290 gfor_fndecl_runtime_error =
2291 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error")),
2292 void_type_node, -1, pchar_type_node);
2293 /* The runtime_error function does not return. */
2294 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error) = 1;
2296 gfor_fndecl_runtime_error_at =
2297 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error_at")),
2298 void_type_node, -2, pchar_type_node,
2300 /* The runtime_error_at function does not return. */
2301 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at) = 1;
2303 gfor_fndecl_generate_error =
2304 gfc_build_library_function_decl (get_identifier (PREFIX("generate_error")),
2305 void_type_node, 3, pvoid_type_node,
2306 integer_type_node, pchar_type_node);
2308 gfor_fndecl_os_error =
2309 gfc_build_library_function_decl (get_identifier (PREFIX("os_error")),
2310 void_type_node, 1, pchar_type_node);
2311 /* The runtime_error function does not return. */
2312 TREE_THIS_VOLATILE (gfor_fndecl_os_error) = 1;
2314 gfor_fndecl_set_fpe =
2315 gfc_build_library_function_decl (get_identifier (PREFIX("set_fpe")),
2316 void_type_node, 1, integer_type_node);
2318 /* Keep the array dimension in sync with the call, later in this file. */
2319 gfor_fndecl_set_options =
2320 gfc_build_library_function_decl (get_identifier (PREFIX("set_options")),
2321 void_type_node, 2, integer_type_node,
2324 gfor_fndecl_set_convert =
2325 gfc_build_library_function_decl (get_identifier (PREFIX("set_convert")),
2326 void_type_node, 1, integer_type_node);
2328 gfor_fndecl_set_record_marker =
2329 gfc_build_library_function_decl (get_identifier (PREFIX("set_record_marker")),
2330 void_type_node, 1, integer_type_node);
2332 gfor_fndecl_set_max_subrecord_length =
2333 gfc_build_library_function_decl (get_identifier (PREFIX("set_max_subrecord_length")),
2334 void_type_node, 1, integer_type_node);
2336 gfor_fndecl_in_pack = gfc_build_library_function_decl (
2337 get_identifier (PREFIX("internal_pack")),
2338 pvoid_type_node, 1, pvoid_type_node);
2340 gfor_fndecl_in_unpack = gfc_build_library_function_decl (
2341 get_identifier (PREFIX("internal_unpack")),
2342 void_type_node, 2, pvoid_type_node, pvoid_type_node);
2344 gfor_fndecl_associated =
2345 gfc_build_library_function_decl (
2346 get_identifier (PREFIX("associated")),
2347 integer_type_node, 2, ppvoid_type_node,
2350 gfc_build_intrinsic_function_decls ();
2351 gfc_build_intrinsic_lib_fndecls ();
2352 gfc_build_io_library_fndecls ();
2356 /* Evaluate the length of dummy character variables. */
2359 gfc_trans_dummy_character (gfc_symbol *sym, gfc_charlen *cl, tree fnbody)
2363 gfc_finish_decl (cl->backend_decl);
2365 gfc_start_block (&body);
2367 /* Evaluate the string length expression. */
2368 gfc_conv_string_length (cl, &body);
2370 gfc_trans_vla_type_sizes (sym, &body);
2372 gfc_add_expr_to_block (&body, fnbody);
2373 return gfc_finish_block (&body);
2377 /* Allocate and cleanup an automatic character variable. */
2380 gfc_trans_auto_character_variable (gfc_symbol * sym, tree fnbody)
2386 gcc_assert (sym->backend_decl);
2387 gcc_assert (sym->ts.cl && sym->ts.cl->length);
2389 gfc_start_block (&body);
2391 /* Evaluate the string length expression. */
2392 gfc_conv_string_length (sym->ts.cl, &body);
2394 gfc_trans_vla_type_sizes (sym, &body);
2396 decl = sym->backend_decl;
2398 /* Emit a DECL_EXPR for this variable, which will cause the
2399 gimplifier to allocate storage, and all that good stuff. */
2400 tmp = fold_build1 (DECL_EXPR, TREE_TYPE (decl), decl);
2401 gfc_add_expr_to_block (&body, tmp);
2403 gfc_add_expr_to_block (&body, fnbody);
2404 return gfc_finish_block (&body);
2407 /* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
2410 gfc_trans_assign_aux_var (gfc_symbol * sym, tree fnbody)
2414 gcc_assert (sym->backend_decl);
2415 gfc_start_block (&body);
2417 /* Set the initial value to length. See the comments in
2418 function gfc_add_assign_aux_vars in this file. */
2419 gfc_add_modify_expr (&body, GFC_DECL_STRING_LEN (sym->backend_decl),
2420 build_int_cst (NULL_TREE, -2));
2422 gfc_add_expr_to_block (&body, fnbody);
2423 return gfc_finish_block (&body);
2427 gfc_trans_vla_one_sizepos (tree *tp, stmtblock_t *body)
2429 tree t = *tp, var, val;
2431 if (t == NULL || t == error_mark_node)
2433 if (TREE_CONSTANT (t) || DECL_P (t))
2436 if (TREE_CODE (t) == SAVE_EXPR)
2438 if (SAVE_EXPR_RESOLVED_P (t))
2440 *tp = TREE_OPERAND (t, 0);
2443 val = TREE_OPERAND (t, 0);
2448 var = gfc_create_var_np (TREE_TYPE (t), NULL);
2449 gfc_add_decl_to_function (var);
2450 gfc_add_modify_expr (body, var, val);
2451 if (TREE_CODE (t) == SAVE_EXPR)
2452 TREE_OPERAND (t, 0) = var;
2457 gfc_trans_vla_type_sizes_1 (tree type, stmtblock_t *body)
2461 if (type == NULL || type == error_mark_node)
2464 type = TYPE_MAIN_VARIANT (type);
2466 if (TREE_CODE (type) == INTEGER_TYPE)
2468 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type), body);
2469 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type), body);
2471 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2473 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
2474 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
2477 else if (TREE_CODE (type) == ARRAY_TYPE)
2479 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type), body);
2480 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type), body);
2481 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type), body);
2482 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type), body);
2484 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2486 TYPE_SIZE (t) = TYPE_SIZE (type);
2487 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
2492 /* Make sure all type sizes and array domains are either constant,
2493 or variable or parameter decls. This is a simplified variant
2494 of gimplify_type_sizes, but we can't use it here, as none of the
2495 variables in the expressions have been gimplified yet.
2496 As type sizes and domains for various variable length arrays
2497 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
2498 time, without this routine gimplify_type_sizes in the middle-end
2499 could result in the type sizes being gimplified earlier than where
2500 those variables are initialized. */
2503 gfc_trans_vla_type_sizes (gfc_symbol *sym, stmtblock_t *body)
2505 tree type = TREE_TYPE (sym->backend_decl);
2507 if (TREE_CODE (type) == FUNCTION_TYPE
2508 && (sym->attr.function || sym->attr.result || sym->attr.entry))
2510 if (! current_fake_result_decl)
2513 type = TREE_TYPE (TREE_VALUE (current_fake_result_decl));
2516 while (POINTER_TYPE_P (type))
2517 type = TREE_TYPE (type);
2519 if (GFC_DESCRIPTOR_TYPE_P (type))
2521 tree etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
2523 while (POINTER_TYPE_P (etype))
2524 etype = TREE_TYPE (etype);
2526 gfc_trans_vla_type_sizes_1 (etype, body);
2529 gfc_trans_vla_type_sizes_1 (type, body);
2533 /* Initialize a derived type by building an lvalue from the symbol
2534 and using trans_assignment to do the work. */
2536 init_default_dt (gfc_symbol * sym, tree body)
2538 stmtblock_t fnblock;
2543 gfc_init_block (&fnblock);
2544 gcc_assert (!sym->attr.allocatable);
2545 gfc_set_sym_referenced (sym);
2546 e = gfc_lval_expr_from_sym (sym);
2547 tmp = gfc_trans_assignment (e, sym->value, false);
2548 if (sym->attr.dummy)
2550 present = gfc_conv_expr_present (sym);
2551 tmp = build3 (COND_EXPR, TREE_TYPE (tmp), present,
2552 tmp, build_empty_stmt ());
2554 gfc_add_expr_to_block (&fnblock, tmp);
2556 gfc_add_expr_to_block (&fnblock, body);
2557 return gfc_finish_block (&fnblock);
2561 /* Initialize INTENT(OUT) derived type dummies. */
2563 init_intent_out_dt (gfc_symbol * proc_sym, tree body)
2565 stmtblock_t fnblock;
2566 gfc_formal_arglist *f;
2568 gfc_init_block (&fnblock);
2569 for (f = proc_sym->formal; f; f = f->next)
2570 if (f->sym && f->sym->attr.intent == INTENT_OUT
2571 && f->sym->ts.type == BT_DERIVED
2572 && !f->sym->ts.derived->attr.alloc_comp
2574 body = init_default_dt (f->sym, body);
2576 gfc_add_expr_to_block (&fnblock, body);
2577 return gfc_finish_block (&fnblock);
2581 /* Generate function entry and exit code, and add it to the function body.
2583 Allocation and initialization of array variables.
2584 Allocation of character string variables.
2585 Initialization and possibly repacking of dummy arrays.
2586 Initialization of ASSIGN statement auxiliary variable. */
2589 gfc_trans_deferred_vars (gfc_symbol * proc_sym, tree fnbody)
2593 gfc_formal_arglist *f;
2595 bool seen_trans_deferred_array = false;
2597 /* Deal with implicit return variables. Explicit return variables will
2598 already have been added. */
2599 if (gfc_return_by_reference (proc_sym) && proc_sym->result == proc_sym)
2601 if (!current_fake_result_decl)
2603 gfc_entry_list *el = NULL;
2604 if (proc_sym->attr.entry_master)
2606 for (el = proc_sym->ns->entries; el; el = el->next)
2607 if (el->sym != el->sym->result)
2610 /* TODO: move to the appropriate place in resolve.c. */
2611 if (warn_return_type && el == NULL)
2612 gfc_warning ("Return value of function '%s' at %L not set",
2613 proc_sym->name, &proc_sym->declared_at);
2615 else if (proc_sym->as)
2617 tree result = TREE_VALUE (current_fake_result_decl);
2618 fnbody = gfc_trans_dummy_array_bias (proc_sym, result, fnbody);
2620 /* An automatic character length, pointer array result. */
2621 if (proc_sym->ts.type == BT_CHARACTER
2622 && TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL)
2623 fnbody = gfc_trans_dummy_character (proc_sym, proc_sym->ts.cl,
2626 else if (proc_sym->ts.type == BT_CHARACTER)
2628 if (TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL)
2629 fnbody = gfc_trans_dummy_character (proc_sym, proc_sym->ts.cl,
2633 gcc_assert (gfc_option.flag_f2c
2634 && proc_sym->ts.type == BT_COMPLEX);
2637 /* Initialize the INTENT(OUT) derived type dummy arguments. This
2638 should be done here so that the offsets and lbounds of arrays
2640 fnbody = init_intent_out_dt (proc_sym, fnbody);
2642 for (sym = proc_sym->tlink; sym != proc_sym; sym = sym->tlink)
2644 bool sym_has_alloc_comp = (sym->ts.type == BT_DERIVED)
2645 && sym->ts.derived->attr.alloc_comp;
2646 if (sym->attr.dimension)
2648 switch (sym->as->type)
2651 if (sym->attr.dummy || sym->attr.result)
2653 gfc_trans_dummy_array_bias (sym, sym->backend_decl, fnbody);
2654 else if (sym->attr.pointer || sym->attr.allocatable)
2656 if (TREE_STATIC (sym->backend_decl))
2657 gfc_trans_static_array_pointer (sym);
2660 seen_trans_deferred_array = true;
2661 fnbody = gfc_trans_deferred_array (sym, fnbody);
2666 if (sym_has_alloc_comp)
2668 seen_trans_deferred_array = true;
2669 fnbody = gfc_trans_deferred_array (sym, fnbody);
2671 else if (sym->ts.type == BT_DERIVED
2674 && sym->attr.save == SAVE_NONE)
2675 fnbody = init_default_dt (sym, fnbody);
2677 gfc_get_backend_locus (&loc);
2678 gfc_set_backend_locus (&sym->declared_at);
2679 fnbody = gfc_trans_auto_array_allocation (sym->backend_decl,
2681 gfc_set_backend_locus (&loc);
2685 case AS_ASSUMED_SIZE:
2686 /* Must be a dummy parameter. */
2687 gcc_assert (sym->attr.dummy);
2689 /* We should always pass assumed size arrays the g77 way. */
2690 fnbody = gfc_trans_g77_array (sym, fnbody);
2693 case AS_ASSUMED_SHAPE:
2694 /* Must be a dummy parameter. */
2695 gcc_assert (sym->attr.dummy);
2697 fnbody = gfc_trans_dummy_array_bias (sym, sym->backend_decl,
2702 seen_trans_deferred_array = true;
2703 fnbody = gfc_trans_deferred_array (sym, fnbody);
2709 if (sym_has_alloc_comp && !seen_trans_deferred_array)
2710 fnbody = gfc_trans_deferred_array (sym, fnbody);
2712 else if (sym_has_alloc_comp)
2713 fnbody = gfc_trans_deferred_array (sym, fnbody);
2714 else if (sym->ts.type == BT_CHARACTER)
2716 gfc_get_backend_locus (&loc);
2717 gfc_set_backend_locus (&sym->declared_at);
2718 if (sym->attr.dummy || sym->attr.result)
2719 fnbody = gfc_trans_dummy_character (sym, sym->ts.cl, fnbody);
2721 fnbody = gfc_trans_auto_character_variable (sym, fnbody);
2722 gfc_set_backend_locus (&loc);
2724 else if (sym->attr.assign)
2726 gfc_get_backend_locus (&loc);
2727 gfc_set_backend_locus (&sym->declared_at);
2728 fnbody = gfc_trans_assign_aux_var (sym, fnbody);
2729 gfc_set_backend_locus (&loc);
2731 else if (sym->ts.type == BT_DERIVED
2734 && sym->attr.save == SAVE_NONE)
2735 fnbody = init_default_dt (sym, fnbody);
2740 gfc_init_block (&body);
2742 for (f = proc_sym->formal; f; f = f->next)
2744 if (f->sym && f->sym->tlink == NULL && f->sym->ts.type == BT_CHARACTER)
2746 gcc_assert (f->sym->ts.cl->backend_decl != NULL);
2747 if (TREE_CODE (f->sym->ts.cl->backend_decl) == PARM_DECL)
2748 gfc_trans_vla_type_sizes (f->sym, &body);
2752 if (gfc_return_by_reference (proc_sym) && proc_sym->ts.type == BT_CHARACTER
2753 && current_fake_result_decl != NULL)
2755 gcc_assert (proc_sym->ts.cl->backend_decl != NULL);
2756 if (TREE_CODE (proc_sym->ts.cl->backend_decl) == PARM_DECL)
2757 gfc_trans_vla_type_sizes (proc_sym, &body);
2760 gfc_add_expr_to_block (&body, fnbody);
2761 return gfc_finish_block (&body);
2765 /* Output an initialized decl for a module variable. */
2768 gfc_create_module_variable (gfc_symbol * sym)
2772 /* Module functions with alternate entries are dealt with later and
2773 would get caught by the next condition. */
2774 if (sym->attr.entry)
2777 /* Make sure we convert the types of the derived types from iso_c_binding
2779 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
2780 && sym->ts.type == BT_DERIVED)
2781 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
2783 /* Only output variables and array valued, or derived type,
2785 if (sym->attr.flavor != FL_VARIABLE
2786 && !(sym->attr.flavor == FL_PARAMETER
2787 && (sym->attr.dimension || sym->ts.type == BT_DERIVED)))
2790 /* Don't generate variables from other modules. Variables from
2791 COMMONs will already have been generated. */
2792 if (sym->attr.use_assoc || sym->attr.in_common)
2795 /* Equivalenced variables arrive here after creation. */
2796 if (sym->backend_decl
2797 && (sym->equiv_built || sym->attr.in_equivalence))
2800 if (sym->backend_decl)
2801 internal_error ("backend decl for module variable %s already exists",
2804 /* We always want module variables to be created. */
2805 sym->attr.referenced = 1;
2806 /* Create the decl. */
2807 decl = gfc_get_symbol_decl (sym);
2809 /* Create the variable. */
2811 rest_of_decl_compilation (decl, 1, 0);
2813 /* Also add length of strings. */
2814 if (sym->ts.type == BT_CHARACTER)
2818 length = sym->ts.cl->backend_decl;
2819 if (!INTEGER_CST_P (length))
2822 rest_of_decl_compilation (length, 1, 0);
2828 /* Generate all the required code for module variables. */
2831 gfc_generate_module_vars (gfc_namespace * ns)
2833 module_namespace = ns;
2835 /* Check if the frontend left the namespace in a reasonable state. */
2836 gcc_assert (ns->proc_name && !ns->proc_name->tlink);
2838 /* Generate COMMON blocks. */
2839 gfc_trans_common (ns);
2841 /* Create decls for all the module variables. */
2842 gfc_traverse_ns (ns, gfc_create_module_variable);
2846 gfc_generate_contained_functions (gfc_namespace * parent)
2850 /* We create all the prototypes before generating any code. */
2851 for (ns = parent->contained; ns; ns = ns->sibling)
2853 /* Skip namespaces from used modules. */
2854 if (ns->parent != parent)
2857 gfc_create_function_decl (ns);
2860 for (ns = parent->contained; ns; ns = ns->sibling)
2862 /* Skip namespaces from used modules. */
2863 if (ns->parent != parent)
2866 gfc_generate_function_code (ns);
2871 /* Drill down through expressions for the array specification bounds and
2872 character length calling generate_local_decl for all those variables
2873 that have not already been declared. */
2876 generate_local_decl (gfc_symbol *);
2878 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
2881 expr_decls (gfc_expr *e, gfc_symbol *sym,
2882 int *f ATTRIBUTE_UNUSED)
2884 if (e->expr_type != EXPR_VARIABLE
2885 || sym == e->symtree->n.sym
2886 || e->symtree->n.sym->mark
2887 || e->symtree->n.sym->ns != sym->ns)
2890 generate_local_decl (e->symtree->n.sym);
2895 generate_expr_decls (gfc_symbol *sym, gfc_expr *e)
2897 gfc_traverse_expr (e, sym, expr_decls, 0);
2901 /* Check for dependencies in the character length and array spec. */
2904 generate_dependency_declarations (gfc_symbol *sym)
2908 if (sym->ts.type == BT_CHARACTER
2910 && sym->ts.cl->length
2911 && sym->ts.cl->length->expr_type != EXPR_CONSTANT)
2912 generate_expr_decls (sym, sym->ts.cl->length);
2914 if (sym->as && sym->as->rank)
2916 for (i = 0; i < sym->as->rank; i++)
2918 generate_expr_decls (sym, sym->as->lower[i]);
2919 generate_expr_decls (sym, sym->as->upper[i]);
2925 /* Generate decls for all local variables. We do this to ensure correct
2926 handling of expressions which only appear in the specification of
2930 generate_local_decl (gfc_symbol * sym)
2932 if (sym->attr.flavor == FL_VARIABLE)
2934 /* Check for dependencies in the array specification and string
2935 length, adding the necessary declarations to the function. We
2936 mark the symbol now, as well as in traverse_ns, to prevent
2937 getting stuck in a circular dependency. */
2939 if (!sym->attr.dummy && !sym->ns->proc_name->attr.entry_master)
2940 generate_dependency_declarations (sym);
2942 if (sym->attr.referenced)
2943 gfc_get_symbol_decl (sym);
2944 /* INTENT(out) dummy arguments are likely meant to be set. */
2945 else if (warn_unused_variable
2947 && sym->attr.intent == INTENT_OUT)
2948 gfc_warning ("Dummy argument '%s' at %L was declared INTENT(OUT) but was not set",
2949 sym->name, &sym->declared_at);
2950 /* Specific warning for unused dummy arguments. */
2951 else if (warn_unused_variable && sym->attr.dummy)
2952 gfc_warning ("Unused dummy argument '%s' at %L", sym->name,
2954 /* Warn for unused variables, but not if they're inside a common
2955 block or are use-associated. */
2956 else if (warn_unused_variable
2957 && !(sym->attr.in_common || sym->attr.use_assoc || sym->mark))
2958 gfc_warning ("Unused variable '%s' declared at %L", sym->name,
2960 /* For variable length CHARACTER parameters, the PARM_DECL already
2961 references the length variable, so force gfc_get_symbol_decl
2962 even when not referenced. If optimize > 0, it will be optimized
2963 away anyway. But do this only after emitting -Wunused-parameter
2964 warning if requested. */
2965 if (sym->attr.dummy && ! sym->attr.referenced
2966 && sym->ts.type == BT_CHARACTER
2967 && sym->ts.cl->backend_decl != NULL
2968 && TREE_CODE (sym->ts.cl->backend_decl) == VAR_DECL)
2970 sym->attr.referenced = 1;
2971 gfc_get_symbol_decl (sym);
2974 /* We do not want the middle-end to warn about unused parameters
2975 as this was already done above. */
2976 if (sym->attr.dummy && sym->backend_decl != NULL_TREE)
2977 TREE_NO_WARNING(sym->backend_decl) = 1;
2979 else if (sym->attr.flavor == FL_PARAMETER)
2981 if (warn_unused_parameter
2982 && !sym->attr.referenced
2983 && !sym->attr.use_assoc)
2984 gfc_warning ("Unused parameter '%s' declared at %L", sym->name,
2987 else if (sym->attr.flavor == FL_PROCEDURE)
2989 /* TODO: move to the appropriate place in resolve.c. */
2990 if (warn_return_type
2991 && sym->attr.function
2993 && sym != sym->result
2994 && !sym->result->attr.referenced
2995 && !sym->attr.use_assoc
2996 && sym->attr.if_source != IFSRC_IFBODY)
2998 gfc_warning ("Return value '%s' of function '%s' declared at "
2999 "%L not set", sym->result->name, sym->name,
3000 &sym->result->declared_at);
3002 /* Prevents "Unused variable" warning for RESULT variables. */
3003 sym->mark = sym->result->mark = 1;
3007 if (sym->attr.dummy == 1)
3009 /* Modify the tree type for scalar character dummy arguments of bind(c)
3010 procedures if they are passed by value. The tree type for them will
3011 be promoted to INTEGER_TYPE for the middle end, which appears to be
3012 what C would do with characters passed by-value. The value attribute
3013 implies the dummy is a scalar. */
3014 if (sym->attr.value == 1 && sym->backend_decl != NULL
3015 && sym->ts.type == BT_CHARACTER && sym->ts.is_c_interop
3016 && sym->ns->proc_name != NULL && sym->ns->proc_name->attr.is_bind_c)
3017 gfc_conv_scalar_char_value (sym, NULL, NULL);
3020 /* Make sure we convert the types of the derived types from iso_c_binding
3022 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
3023 && sym->ts.type == BT_DERIVED)
3024 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
3028 generate_local_vars (gfc_namespace * ns)
3030 gfc_traverse_ns (ns, generate_local_decl);
3034 /* Generate a switch statement to jump to the correct entry point. Also
3035 creates the label decls for the entry points. */
3038 gfc_trans_entry_master_switch (gfc_entry_list * el)
3045 gfc_init_block (&block);
3046 for (; el; el = el->next)
3048 /* Add the case label. */
3049 label = gfc_build_label_decl (NULL_TREE);
3050 val = build_int_cst (gfc_array_index_type, el->id);
3051 tmp = build3_v (CASE_LABEL_EXPR, val, NULL_TREE, label);
3052 gfc_add_expr_to_block (&block, tmp);
3054 /* And jump to the actual entry point. */
3055 label = gfc_build_label_decl (NULL_TREE);
3056 tmp = build1_v (GOTO_EXPR, label);
3057 gfc_add_expr_to_block (&block, tmp);
3059 /* Save the label decl. */
3062 tmp = gfc_finish_block (&block);
3063 /* The first argument selects the entry point. */
3064 val = DECL_ARGUMENTS (current_function_decl);
3065 tmp = build3_v (SWITCH_EXPR, val, tmp, NULL_TREE);
3070 /* Generate code for a function. */
3073 gfc_generate_function_code (gfc_namespace * ns)
3086 sym = ns->proc_name;
3088 /* Check that the frontend isn't still using this. */
3089 gcc_assert (sym->tlink == NULL);
3092 /* Create the declaration for functions with global scope. */
3093 if (!sym->backend_decl)
3094 gfc_create_function_decl (ns);
3096 fndecl = sym->backend_decl;
3097 old_context = current_function_decl;
3101 push_function_context ();
3102 saved_parent_function_decls = saved_function_decls;
3103 saved_function_decls = NULL_TREE;
3106 trans_function_start (sym);
3108 gfc_start_block (&block);
3110 if (ns->entries && ns->proc_name->ts.type == BT_CHARACTER)
3112 /* Copy length backend_decls to all entry point result
3117 gfc_conv_const_charlen (ns->proc_name->ts.cl);
3118 backend_decl = ns->proc_name->result->ts.cl->backend_decl;
3119 for (el = ns->entries; el; el = el->next)
3120 el->sym->result->ts.cl->backend_decl = backend_decl;
3123 /* Translate COMMON blocks. */
3124 gfc_trans_common (ns);
3126 /* Null the parent fake result declaration if this namespace is
3127 a module function or an external procedures. */
3128 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
3129 || ns->parent == NULL)
3130 parent_fake_result_decl = NULL_TREE;
3132 gfc_generate_contained_functions (ns);
3134 generate_local_vars (ns);
3136 /* Keep the parent fake result declaration in module functions
3137 or external procedures. */
3138 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
3139 || ns->parent == NULL)
3140 current_fake_result_decl = parent_fake_result_decl;
3142 current_fake_result_decl = NULL_TREE;
3144 current_function_return_label = NULL;
3146 /* Now generate the code for the body of this function. */
3147 gfc_init_block (&body);
3149 /* If this is the main program, add a call to set_options to set up the
3150 runtime library Fortran language standard parameters. */
3151 if (sym->attr.is_main_program)
3153 tree array_type, array, var;
3155 /* Passing a new option to the library requires four modifications:
3156 + add it to the tree_cons list below
3157 + change the array size in the call to build_array_type
3158 + change the first argument to the library call
3159 gfor_fndecl_set_options
3160 + modify the library (runtime/compile_options.c)! */
3161 array = tree_cons (NULL_TREE,
3162 build_int_cst (integer_type_node,
3163 gfc_option.warn_std), NULL_TREE);
3164 array = tree_cons (NULL_TREE,
3165 build_int_cst (integer_type_node,
3166 gfc_option.allow_std), array);
3167 array = tree_cons (NULL_TREE,
3168 build_int_cst (integer_type_node, pedantic), array);
3169 array = tree_cons (NULL_TREE,
3170 build_int_cst (integer_type_node,
3171 gfc_option.flag_dump_core), array);
3172 array = tree_cons (NULL_TREE,
3173 build_int_cst (integer_type_node,
3174 gfc_option.flag_backtrace), array);
3175 array = tree_cons (NULL_TREE,
3176 build_int_cst (integer_type_node,
3177 gfc_option.flag_sign_zero), array);
3179 array = tree_cons (NULL_TREE,
3180 build_int_cst (integer_type_node,
3181 flag_bounds_check), array);
3183 array_type = build_array_type (integer_type_node,
3184 build_index_type (build_int_cst (NULL_TREE,
3186 array = build_constructor_from_list (array_type, nreverse (array));
3187 TREE_CONSTANT (array) = 1;
3188 TREE_INVARIANT (array) = 1;
3189 TREE_STATIC (array) = 1;
3191 /* Create a static variable to hold the jump table. */
3192 var = gfc_create_var (array_type, "options");
3193 TREE_CONSTANT (var) = 1;
3194 TREE_INVARIANT (var) = 1;
3195 TREE_STATIC (var) = 1;
3196 TREE_READONLY (var) = 1;
3197 DECL_INITIAL (var) = array;
3198 var = gfc_build_addr_expr (pvoid_type_node, var);
3200 tmp = build_call_expr (gfor_fndecl_set_options, 2,
3201 build_int_cst (integer_type_node, 7), var);
3202 gfc_add_expr_to_block (&body, tmp);
3205 /* If this is the main program and a -ffpe-trap option was provided,
3206 add a call to set_fpe so that the library will raise a FPE when
3208 if (sym->attr.is_main_program && gfc_option.fpe != 0)
3210 tmp = build_call_expr (gfor_fndecl_set_fpe, 1,
3211 build_int_cst (integer_type_node,
3213 gfc_add_expr_to_block (&body, tmp);
3216 /* If this is the main program and an -fconvert option was provided,
3217 add a call to set_convert. */
3219 if (sym->attr.is_main_program && gfc_option.convert != GFC_CONVERT_NATIVE)
3221 tmp = build_call_expr (gfor_fndecl_set_convert, 1,
3222 build_int_cst (integer_type_node,
3223 gfc_option.convert));
3224 gfc_add_expr_to_block (&body, tmp);
3227 /* If this is the main program and an -frecord-marker option was provided,
3228 add a call to set_record_marker. */
3230 if (sym->attr.is_main_program && gfc_option.record_marker != 0)
3232 tmp = build_call_expr (gfor_fndecl_set_record_marker, 1,
3233 build_int_cst (integer_type_node,
3234 gfc_option.record_marker));
3235 gfc_add_expr_to_block (&body, tmp);
3238 if (sym->attr.is_main_program && gfc_option.max_subrecord_length != 0)
3240 tmp = build_call_expr (gfor_fndecl_set_max_subrecord_length,
3242 build_int_cst (integer_type_node,
3243 gfc_option.max_subrecord_length));
3244 gfc_add_expr_to_block (&body, tmp);
3247 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
3248 && sym->attr.subroutine)
3250 tree alternate_return;
3251 alternate_return = gfc_get_fake_result_decl (sym, 0);
3252 gfc_add_modify_expr (&body, alternate_return, integer_zero_node);
3257 /* Jump to the correct entry point. */
3258 tmp = gfc_trans_entry_master_switch (ns->entries);
3259 gfc_add_expr_to_block (&body, tmp);
3262 tmp = gfc_trans_code (ns->code);
3263 gfc_add_expr_to_block (&body, tmp);
3265 /* Add a return label if needed. */
3266 if (current_function_return_label)
3268 tmp = build1_v (LABEL_EXPR, current_function_return_label);
3269 gfc_add_expr_to_block (&body, tmp);
3272 tmp = gfc_finish_block (&body);
3273 /* Add code to create and cleanup arrays. */
3274 tmp = gfc_trans_deferred_vars (sym, tmp);
3276 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node)
3278 if (sym->attr.subroutine || sym == sym->result)
3280 if (current_fake_result_decl != NULL)
3281 result = TREE_VALUE (current_fake_result_decl);
3284 current_fake_result_decl = NULL_TREE;
3287 result = sym->result->backend_decl;
3289 if (result != NULL_TREE && sym->attr.function
3290 && sym->ts.type == BT_DERIVED
3291 && sym->ts.derived->attr.alloc_comp
3292 && !sym->attr.pointer)
3294 rank = sym->as ? sym->as->rank : 0;
3295 tmp2 = gfc_nullify_alloc_comp (sym->ts.derived, result, rank);
3296 gfc_add_expr_to_block (&block, tmp2);
3299 gfc_add_expr_to_block (&block, tmp);
3301 if (result == NULL_TREE)
3303 /* TODO: move to the appropriate place in resolve.c. */
3304 if (warn_return_type && !sym->attr.referenced && sym == sym->result)
3305 gfc_warning ("Return value of function '%s' at %L not set",
3306 sym->name, &sym->declared_at);
3308 TREE_NO_WARNING(sym->backend_decl) = 1;
3312 /* Set the return value to the dummy result variable. The
3313 types may be different for scalar default REAL functions
3314 with -ff2c, therefore we have to convert. */
3315 tmp = convert (TREE_TYPE (DECL_RESULT (fndecl)), result);
3316 tmp = fold_build2 (MODIFY_EXPR, TREE_TYPE (tmp),
3317 DECL_RESULT (fndecl), tmp);
3318 tmp = build1_v (RETURN_EXPR, tmp);
3319 gfc_add_expr_to_block (&block, tmp);
3323 gfc_add_expr_to_block (&block, tmp);
3326 /* Add all the decls we created during processing. */
3327 decl = saved_function_decls;
3332 next = TREE_CHAIN (decl);
3333 TREE_CHAIN (decl) = NULL_TREE;
3337 saved_function_decls = NULL_TREE;
3339 DECL_SAVED_TREE (fndecl) = gfc_finish_block (&block);
3341 /* Finish off this function and send it for code generation. */
3343 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
3345 /* Output the GENERIC tree. */
3346 dump_function (TDI_original, fndecl);
3348 /* Store the end of the function, so that we get good line number
3349 info for the epilogue. */
3350 cfun->function_end_locus = input_location;
3352 /* We're leaving the context of this function, so zap cfun.
3353 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
3354 tree_rest_of_compilation. */
3359 pop_function_context ();
3360 saved_function_decls = saved_parent_function_decls;
3362 current_function_decl = old_context;
3364 if (decl_function_context (fndecl))
3365 /* Register this function with cgraph just far enough to get it
3366 added to our parent's nested function list. */
3367 (void) cgraph_node (fndecl);
3370 gfc_gimplify_function (fndecl);
3371 cgraph_finalize_function (fndecl, false);
3376 gfc_generate_constructors (void)
3378 gcc_assert (gfc_static_ctors == NULL_TREE);
3386 if (gfc_static_ctors == NULL_TREE)
3389 fnname = get_file_function_name ("I");
3390 type = build_function_type (void_type_node,
3391 gfc_chainon_list (NULL_TREE, void_type_node));
3393 fndecl = build_decl (FUNCTION_DECL, fnname, type);
3394 TREE_PUBLIC (fndecl) = 1;
3396 decl = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
3397 DECL_ARTIFICIAL (decl) = 1;
3398 DECL_IGNORED_P (decl) = 1;
3399 DECL_CONTEXT (decl) = fndecl;
3400 DECL_RESULT (fndecl) = decl;
3404 current_function_decl = fndecl;
3406 rest_of_decl_compilation (fndecl, 1, 0);
3408 make_decl_rtl (fndecl);
3410 init_function_start (fndecl);
3414 for (; gfc_static_ctors; gfc_static_ctors = TREE_CHAIN (gfc_static_ctors))
3416 tmp = build_call_expr (TREE_VALUE (gfc_static_ctors), 0);
3417 DECL_SAVED_TREE (fndecl) = build_stmt (EXPR_STMT, tmp);
3422 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
3424 free_after_parsing (cfun);
3425 free_after_compilation (cfun);
3427 tree_rest_of_compilation (fndecl);
3429 current_function_decl = NULL_TREE;
3433 /* Translates a BLOCK DATA program unit. This means emitting the
3434 commons contained therein plus their initializations. We also emit
3435 a globally visible symbol to make sure that each BLOCK DATA program
3436 unit remains unique. */
3439 gfc_generate_block_data (gfc_namespace * ns)
3444 /* Tell the backend the source location of the block data. */
3446 gfc_set_backend_locus (&ns->proc_name->declared_at);
3448 gfc_set_backend_locus (&gfc_current_locus);
3450 /* Process the DATA statements. */
3451 gfc_trans_common (ns);
3453 /* Create a global symbol with the mane of the block data. This is to
3454 generate linker errors if the same name is used twice. It is never
3457 id = gfc_sym_mangled_function_id (ns->proc_name);
3459 id = get_identifier ("__BLOCK_DATA__");
3461 decl = build_decl (VAR_DECL, id, gfc_array_index_type);
3462 TREE_PUBLIC (decl) = 1;
3463 TREE_STATIC (decl) = 1;
3466 rest_of_decl_compilation (decl, 1, 0);
3470 #include "gt-fortran-trans-decl.h"