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->value && sym->ns->proc_name->attr.is_main_program)))
516 TREE_STATIC (decl) = 1;
518 if (sym->attr.volatile_)
520 TREE_THIS_VOLATILE (decl) = 1;
521 new = build_qualified_type (TREE_TYPE (decl), TYPE_QUAL_VOLATILE);
522 TREE_TYPE (decl) = new;
525 /* Keep variables larger than max-stack-var-size off stack. */
526 if (!sym->ns->proc_name->attr.recursive
527 && INTEGER_CST_P (DECL_SIZE_UNIT (decl))
528 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
529 /* Put variable length auto array pointers always into stack. */
530 && (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
531 || sym->attr.dimension == 0
532 || sym->as->type != AS_EXPLICIT
534 || sym->attr.allocatable)
535 && !DECL_ARTIFICIAL (decl))
536 TREE_STATIC (decl) = 1;
538 /* Handle threadprivate variables. */
539 if (sym->attr.threadprivate
540 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
541 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
545 /* Allocate the lang-specific part of a decl. */
548 gfc_allocate_lang_decl (tree decl)
550 DECL_LANG_SPECIFIC (decl) = (struct lang_decl *)
551 ggc_alloc_cleared (sizeof (struct lang_decl));
554 /* Remember a symbol to generate initialization/cleanup code at function
558 gfc_defer_symbol_init (gfc_symbol * sym)
564 /* Don't add a symbol twice. */
568 last = head = sym->ns->proc_name;
571 /* Make sure that setup code for dummy variables which are used in the
572 setup of other variables is generated first. */
575 /* Find the first dummy arg seen after us, or the first non-dummy arg.
576 This is a circular list, so don't go past the head. */
578 && (!p->attr.dummy || p->dummy_order > sym->dummy_order))
584 /* Insert in between last and p. */
590 /* Create an array index type variable with function scope. */
593 create_index_var (const char * pfx, int nest)
597 decl = gfc_create_var_np (gfc_array_index_type, pfx);
599 gfc_add_decl_to_parent_function (decl);
601 gfc_add_decl_to_function (decl);
606 /* Create variables to hold all the non-constant bits of info for a
607 descriptorless array. Remember these in the lang-specific part of the
611 gfc_build_qualified_array (tree decl, gfc_symbol * sym)
617 type = TREE_TYPE (decl);
619 /* We just use the descriptor, if there is one. */
620 if (GFC_DESCRIPTOR_TYPE_P (type))
623 gcc_assert (GFC_ARRAY_TYPE_P (type));
624 nest = (sym->ns->proc_name->backend_decl != current_function_decl)
625 && !sym->attr.contained;
627 for (dim = 0; dim < GFC_TYPE_ARRAY_RANK (type); dim++)
629 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
631 GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
632 TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type, dim)) = 1;
634 /* Don't try to use the unknown bound for assumed shape arrays. */
635 if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
636 && (sym->as->type != AS_ASSUMED_SIZE
637 || dim < GFC_TYPE_ARRAY_RANK (type) - 1))
639 GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
640 TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type, dim)) = 1;
643 if (GFC_TYPE_ARRAY_STRIDE (type, dim) == NULL_TREE)
645 GFC_TYPE_ARRAY_STRIDE (type, dim) = create_index_var ("stride", nest);
646 TREE_NO_WARNING (GFC_TYPE_ARRAY_STRIDE (type, dim)) = 1;
649 if (GFC_TYPE_ARRAY_OFFSET (type) == NULL_TREE)
651 GFC_TYPE_ARRAY_OFFSET (type) = gfc_create_var_np (gfc_array_index_type,
653 TREE_NO_WARNING (GFC_TYPE_ARRAY_OFFSET (type)) = 1;
656 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type));
658 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type));
661 if (GFC_TYPE_ARRAY_SIZE (type) == NULL_TREE
662 && sym->as->type != AS_ASSUMED_SIZE)
664 GFC_TYPE_ARRAY_SIZE (type) = create_index_var ("size", nest);
665 TREE_NO_WARNING (GFC_TYPE_ARRAY_SIZE (type)) = 1;
668 if (POINTER_TYPE_P (type))
670 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type)));
671 gcc_assert (TYPE_LANG_SPECIFIC (type)
672 == TYPE_LANG_SPECIFIC (TREE_TYPE (type)));
673 type = TREE_TYPE (type);
676 if (! COMPLETE_TYPE_P (type) && GFC_TYPE_ARRAY_SIZE (type))
680 size = fold_build2 (MINUS_EXPR, gfc_array_index_type,
681 GFC_TYPE_ARRAY_SIZE (type), gfc_index_one_node);
682 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
684 TYPE_DOMAIN (type) = range;
690 /* For some dummy arguments we don't use the actual argument directly.
691 Instead we create a local decl and use that. This allows us to perform
692 initialization, and construct full type information. */
695 gfc_build_dummy_array_decl (gfc_symbol * sym, tree dummy)
705 if (sym->attr.pointer || sym->attr.allocatable)
708 /* Add to list of variables if not a fake result variable. */
709 if (sym->attr.result || sym->attr.dummy)
710 gfc_defer_symbol_init (sym);
712 type = TREE_TYPE (dummy);
713 gcc_assert (TREE_CODE (dummy) == PARM_DECL
714 && POINTER_TYPE_P (type));
716 /* Do we know the element size? */
717 known_size = sym->ts.type != BT_CHARACTER
718 || INTEGER_CST_P (sym->ts.cl->backend_decl);
720 if (known_size && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type)))
722 /* For descriptorless arrays with known element size the actual
723 argument is sufficient. */
724 gcc_assert (GFC_ARRAY_TYPE_P (type));
725 gfc_build_qualified_array (dummy, sym);
729 type = TREE_TYPE (type);
730 if (GFC_DESCRIPTOR_TYPE_P (type))
732 /* Create a descriptorless array pointer. */
735 if (!gfc_option.flag_repack_arrays)
737 if (as->type == AS_ASSUMED_SIZE)
738 packed = PACKED_FULL;
742 if (as->type == AS_EXPLICIT)
744 packed = PACKED_FULL;
745 for (n = 0; n < as->rank; n++)
749 && as->upper[n]->expr_type == EXPR_CONSTANT
750 && as->lower[n]->expr_type == EXPR_CONSTANT))
751 packed = PACKED_PARTIAL;
755 packed = PACKED_PARTIAL;
758 type = gfc_typenode_for_spec (&sym->ts);
759 type = gfc_get_nodesc_array_type (type, sym->as, packed);
763 /* We now have an expression for the element size, so create a fully
764 qualified type. Reset sym->backend decl or this will just return the
766 DECL_ARTIFICIAL (sym->backend_decl) = 1;
767 sym->backend_decl = NULL_TREE;
768 type = gfc_sym_type (sym);
769 packed = PACKED_FULL;
772 ASM_FORMAT_PRIVATE_NAME (name, IDENTIFIER_POINTER (DECL_NAME (dummy)), 0);
773 decl = build_decl (VAR_DECL, get_identifier (name), type);
775 DECL_ARTIFICIAL (decl) = 1;
776 TREE_PUBLIC (decl) = 0;
777 TREE_STATIC (decl) = 0;
778 DECL_EXTERNAL (decl) = 0;
780 /* We should never get deferred shape arrays here. We used to because of
782 gcc_assert (sym->as->type != AS_DEFERRED);
784 if (packed == PACKED_PARTIAL)
785 GFC_DECL_PARTIAL_PACKED_ARRAY (decl) = 1;
786 else if (packed == PACKED_FULL)
787 GFC_DECL_PACKED_ARRAY (decl) = 1;
789 gfc_build_qualified_array (decl, sym);
791 if (DECL_LANG_SPECIFIC (dummy))
792 DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (dummy);
794 gfc_allocate_lang_decl (decl);
796 GFC_DECL_SAVED_DESCRIPTOR (decl) = dummy;
798 if (sym->ns->proc_name->backend_decl == current_function_decl
799 || sym->attr.contained)
800 gfc_add_decl_to_function (decl);
802 gfc_add_decl_to_parent_function (decl);
808 /* Return a constant or a variable to use as a string length. Does not
809 add the decl to the current scope. */
812 gfc_create_string_length (gfc_symbol * sym)
816 gcc_assert (sym->ts.cl);
817 gfc_conv_const_charlen (sym->ts.cl);
819 if (sym->ts.cl->backend_decl == NULL_TREE)
821 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
823 /* Also prefix the mangled name. */
824 strcpy (&name[1], sym->name);
826 length = build_decl (VAR_DECL, get_identifier (name),
827 gfc_charlen_type_node);
828 DECL_ARTIFICIAL (length) = 1;
829 TREE_USED (length) = 1;
830 if (sym->ns->proc_name->tlink != NULL)
831 gfc_defer_symbol_init (sym);
832 sym->ts.cl->backend_decl = length;
835 return sym->ts.cl->backend_decl;
838 /* If a variable is assigned a label, we add another two auxiliary
842 gfc_add_assign_aux_vars (gfc_symbol * sym)
848 gcc_assert (sym->backend_decl);
850 decl = sym->backend_decl;
851 gfc_allocate_lang_decl (decl);
852 GFC_DECL_ASSIGN (decl) = 1;
853 length = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
854 gfc_charlen_type_node);
855 addr = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
857 gfc_finish_var_decl (length, sym);
858 gfc_finish_var_decl (addr, sym);
859 /* STRING_LENGTH is also used as flag. Less than -1 means that
860 ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the
861 target label's address. Otherwise, value is the length of a format string
862 and ASSIGN_ADDR is its address. */
863 if (TREE_STATIC (length))
864 DECL_INITIAL (length) = build_int_cst (NULL_TREE, -2);
866 gfc_defer_symbol_init (sym);
868 GFC_DECL_STRING_LEN (decl) = length;
869 GFC_DECL_ASSIGN_ADDR (decl) = addr;
872 /* Return the decl for a gfc_symbol, create it if it doesn't already
876 gfc_get_symbol_decl (gfc_symbol * sym)
879 tree length = NULL_TREE;
882 gcc_assert (sym->attr.referenced
883 || sym->attr.use_assoc
884 || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY);
886 if (sym->ns && sym->ns->proc_name->attr.function)
887 byref = gfc_return_by_reference (sym->ns->proc_name);
891 if ((sym->attr.dummy && ! sym->attr.function) || (sym->attr.result && byref))
893 /* Return via extra parameter. */
894 if (sym->attr.result && byref
895 && !sym->backend_decl)
898 DECL_ARGUMENTS (sym->ns->proc_name->backend_decl);
899 /* For entry master function skip over the __entry
901 if (sym->ns->proc_name->attr.entry_master)
902 sym->backend_decl = TREE_CHAIN (sym->backend_decl);
905 /* Dummy variables should already have been created. */
906 gcc_assert (sym->backend_decl);
908 /* Create a character length variable. */
909 if (sym->ts.type == BT_CHARACTER)
911 if (sym->ts.cl->backend_decl == NULL_TREE)
912 length = gfc_create_string_length (sym);
914 length = sym->ts.cl->backend_decl;
915 if (TREE_CODE (length) == VAR_DECL
916 && DECL_CONTEXT (length) == NULL_TREE)
918 /* Add the string length to the same context as the symbol. */
919 if (DECL_CONTEXT (sym->backend_decl) == current_function_decl)
920 gfc_add_decl_to_function (length);
922 gfc_add_decl_to_parent_function (length);
924 gcc_assert (DECL_CONTEXT (sym->backend_decl) ==
925 DECL_CONTEXT (length));
927 gfc_defer_symbol_init (sym);
931 /* Use a copy of the descriptor for dummy arrays. */
932 if (sym->attr.dimension && !TREE_USED (sym->backend_decl))
934 decl = gfc_build_dummy_array_decl (sym, sym->backend_decl);
935 /* Prevent the dummy from being detected as unused if it is copied. */
936 if (sym->backend_decl != NULL && decl != sym->backend_decl)
937 DECL_ARTIFICIAL (sym->backend_decl) = 1;
938 sym->backend_decl = decl;
941 TREE_USED (sym->backend_decl) = 1;
942 if (sym->attr.assign && GFC_DECL_ASSIGN (sym->backend_decl) == 0)
944 gfc_add_assign_aux_vars (sym);
946 return sym->backend_decl;
949 if (sym->backend_decl)
950 return sym->backend_decl;
952 /* Catch function declarations. Only used for actual parameters. */
953 if (sym->attr.flavor == FL_PROCEDURE)
955 decl = gfc_get_extern_function_decl (sym);
959 if (sym->attr.intrinsic)
960 internal_error ("intrinsic variable which isn't a procedure");
962 /* Create string length decl first so that they can be used in the
964 if (sym->ts.type == BT_CHARACTER)
965 length = gfc_create_string_length (sym);
967 /* Create the decl for the variable. */
968 decl = build_decl (VAR_DECL, gfc_sym_identifier (sym), gfc_sym_type (sym));
970 gfc_set_decl_location (decl, &sym->declared_at);
972 /* Symbols from modules should have their assembler names mangled.
973 This is done here rather than in gfc_finish_var_decl because it
974 is different for string length variables. */
976 SET_DECL_ASSEMBLER_NAME (decl, gfc_sym_mangled_identifier (sym));
978 if (sym->attr.dimension)
980 /* Create variables to hold the non-constant bits of array info. */
981 gfc_build_qualified_array (decl, sym);
983 /* Remember this variable for allocation/cleanup. */
984 gfc_defer_symbol_init (sym);
986 if ((sym->attr.allocatable || !sym->attr.dummy) && !sym->attr.pointer)
987 GFC_DECL_PACKED_ARRAY (decl) = 1;
990 if (sym->ts.type == BT_DERIVED && sym->ts.derived->attr.alloc_comp)
991 gfc_defer_symbol_init (sym);
992 /* This applies a derived type default initializer. */
993 else if (sym->ts.type == BT_DERIVED
994 && sym->attr.save == SAVE_NONE
996 && !sym->attr.allocatable
997 && (sym->value && !sym->ns->proc_name->attr.is_main_program)
998 && !sym->attr.use_assoc)
999 gfc_defer_symbol_init (sym);
1001 gfc_finish_var_decl (decl, sym);
1003 if (sym->ts.type == BT_CHARACTER)
1005 /* Character variables need special handling. */
1006 gfc_allocate_lang_decl (decl);
1008 if (TREE_CODE (length) != INTEGER_CST)
1010 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
1014 /* Also prefix the mangled name for symbols from modules. */
1015 strcpy (&name[1], sym->name);
1018 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (length)));
1019 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (name));
1021 gfc_finish_var_decl (length, sym);
1022 gcc_assert (!sym->value);
1025 else if (sym->attr.subref_array_pointer)
1027 /* We need the span for these beasts. */
1028 gfc_allocate_lang_decl (decl);
1031 if (sym->attr.subref_array_pointer)
1034 GFC_DECL_SUBREF_ARRAY_P (decl) = 1;
1035 span = build_decl (VAR_DECL, create_tmp_var_name ("span"),
1036 gfc_array_index_type);
1037 gfc_finish_var_decl (span, sym);
1038 TREE_STATIC (span) = 1;
1039 DECL_INITIAL (span) = build_int_cst (NULL_TREE, 0);
1041 GFC_DECL_SPAN (decl) = span;
1044 sym->backend_decl = decl;
1046 if (sym->attr.assign)
1047 gfc_add_assign_aux_vars (sym);
1049 if (TREE_STATIC (decl) && !sym->attr.use_assoc)
1051 /* Add static initializer. */
1052 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
1053 TREE_TYPE (decl), sym->attr.dimension,
1054 sym->attr.pointer || sym->attr.allocatable);
1061 /* Substitute a temporary variable in place of the real one. */
1064 gfc_shadow_sym (gfc_symbol * sym, tree decl, gfc_saved_var * save)
1066 save->attr = sym->attr;
1067 save->decl = sym->backend_decl;
1069 gfc_clear_attr (&sym->attr);
1070 sym->attr.referenced = 1;
1071 sym->attr.flavor = FL_VARIABLE;
1073 sym->backend_decl = decl;
1077 /* Restore the original variable. */
1080 gfc_restore_sym (gfc_symbol * sym, gfc_saved_var * save)
1082 sym->attr = save->attr;
1083 sym->backend_decl = save->decl;
1087 /* Get a basic decl for an external function. */
1090 gfc_get_extern_function_decl (gfc_symbol * sym)
1095 gfc_intrinsic_sym *isym;
1097 char s[GFC_MAX_SYMBOL_LEN + 23]; /* "_gfortran_f2c_specific" and '\0'. */
1101 if (sym->backend_decl)
1102 return sym->backend_decl;
1104 /* We should never be creating external decls for alternate entry points.
1105 The procedure may be an alternate entry point, but we don't want/need
1107 gcc_assert (!(sym->attr.entry || sym->attr.entry_master));
1109 if (sym->attr.intrinsic)
1111 /* Call the resolution function to get the actual name. This is
1112 a nasty hack which relies on the resolution functions only looking
1113 at the first argument. We pass NULL for the second argument
1114 otherwise things like AINT get confused. */
1115 isym = gfc_find_function (sym->name);
1116 gcc_assert (isym->resolve.f0 != NULL);
1118 memset (&e, 0, sizeof (e));
1119 e.expr_type = EXPR_FUNCTION;
1121 memset (&argexpr, 0, sizeof (argexpr));
1122 gcc_assert (isym->formal);
1123 argexpr.ts = isym->formal->ts;
1125 if (isym->formal->next == NULL)
1126 isym->resolve.f1 (&e, &argexpr);
1129 if (isym->formal->next->next == NULL)
1130 isym->resolve.f2 (&e, &argexpr, NULL);
1133 if (isym->formal->next->next->next == NULL)
1134 isym->resolve.f3 (&e, &argexpr, NULL, NULL);
1137 /* All specific intrinsics take less than 5 arguments. */
1138 gcc_assert (isym->formal->next->next->next->next == NULL);
1139 isym->resolve.f4 (&e, &argexpr, NULL, NULL, NULL);
1144 if (gfc_option.flag_f2c
1145 && ((e.ts.type == BT_REAL && e.ts.kind == gfc_default_real_kind)
1146 || e.ts.type == BT_COMPLEX))
1148 /* Specific which needs a different implementation if f2c
1149 calling conventions are used. */
1150 sprintf (s, "_gfortran_f2c_specific%s", e.value.function.name);
1153 sprintf (s, "_gfortran_specific%s", e.value.function.name);
1155 name = get_identifier (s);
1156 mangled_name = name;
1160 name = gfc_sym_identifier (sym);
1161 mangled_name = gfc_sym_mangled_function_id (sym);
1164 type = gfc_get_function_type (sym);
1165 fndecl = build_decl (FUNCTION_DECL, name, type);
1167 SET_DECL_ASSEMBLER_NAME (fndecl, mangled_name);
1168 /* If the return type is a pointer, avoid alias issues by setting
1169 DECL_IS_MALLOC to nonzero. This means that the function should be
1170 treated as if it were a malloc, meaning it returns a pointer that
1172 if (POINTER_TYPE_P (type))
1173 DECL_IS_MALLOC (fndecl) = 1;
1175 /* Set the context of this decl. */
1176 if (0 && sym->ns && sym->ns->proc_name)
1178 /* TODO: Add external decls to the appropriate scope. */
1179 DECL_CONTEXT (fndecl) = sym->ns->proc_name->backend_decl;
1183 /* Global declaration, e.g. intrinsic subroutine. */
1184 DECL_CONTEXT (fndecl) = NULL_TREE;
1187 DECL_EXTERNAL (fndecl) = 1;
1189 /* This specifies if a function is globally addressable, i.e. it is
1190 the opposite of declaring static in C. */
1191 TREE_PUBLIC (fndecl) = 1;
1193 /* Set attributes for PURE functions. A call to PURE function in the
1194 Fortran 95 sense is both pure and without side effects in the C
1196 if (sym->attr.pure || sym->attr.elemental)
1198 if (sym->attr.function && !gfc_return_by_reference (sym))
1199 DECL_IS_PURE (fndecl) = 1;
1200 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
1201 parameters and don't use alternate returns (is this
1202 allowed?). In that case, calls to them are meaningless, and
1203 can be optimized away. See also in build_function_decl(). */
1204 TREE_SIDE_EFFECTS (fndecl) = 0;
1207 /* Mark non-returning functions. */
1208 if (sym->attr.noreturn)
1209 TREE_THIS_VOLATILE(fndecl) = 1;
1211 sym->backend_decl = fndecl;
1213 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1214 pushdecl_top_level (fndecl);
1220 /* Create a declaration for a procedure. For external functions (in the C
1221 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
1222 a master function with alternate entry points. */
1225 build_function_decl (gfc_symbol * sym)
1228 symbol_attribute attr;
1230 gfc_formal_arglist *f;
1232 gcc_assert (!sym->backend_decl);
1233 gcc_assert (!sym->attr.external);
1235 /* Set the line and filename. sym->declared_at seems to point to the
1236 last statement for subroutines, but it'll do for now. */
1237 gfc_set_backend_locus (&sym->declared_at);
1239 /* Allow only one nesting level. Allow public declarations. */
1240 gcc_assert (current_function_decl == NULL_TREE
1241 || DECL_CONTEXT (current_function_decl) == NULL_TREE);
1243 type = gfc_get_function_type (sym);
1244 fndecl = build_decl (FUNCTION_DECL, gfc_sym_identifier (sym), type);
1246 /* Perform name mangling if this is a top level or module procedure. */
1247 if (current_function_decl == NULL_TREE)
1248 SET_DECL_ASSEMBLER_NAME (fndecl, gfc_sym_mangled_function_id (sym));
1250 /* Figure out the return type of the declared function, and build a
1251 RESULT_DECL for it. If this is a subroutine with alternate
1252 returns, build a RESULT_DECL for it. */
1255 result_decl = NULL_TREE;
1256 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
1259 if (gfc_return_by_reference (sym))
1260 type = void_type_node;
1263 if (sym->result != sym)
1264 result_decl = gfc_sym_identifier (sym->result);
1266 type = TREE_TYPE (TREE_TYPE (fndecl));
1271 /* Look for alternate return placeholders. */
1272 int has_alternate_returns = 0;
1273 for (f = sym->formal; f; f = f->next)
1277 has_alternate_returns = 1;
1282 if (has_alternate_returns)
1283 type = integer_type_node;
1285 type = void_type_node;
1288 result_decl = build_decl (RESULT_DECL, result_decl, type);
1289 DECL_ARTIFICIAL (result_decl) = 1;
1290 DECL_IGNORED_P (result_decl) = 1;
1291 DECL_CONTEXT (result_decl) = fndecl;
1292 DECL_RESULT (fndecl) = result_decl;
1294 /* Don't call layout_decl for a RESULT_DECL.
1295 layout_decl (result_decl, 0); */
1297 /* If the return type is a pointer, avoid alias issues by setting
1298 DECL_IS_MALLOC to nonzero. This means that the function should be
1299 treated as if it were a malloc, meaning it returns a pointer that
1301 if (POINTER_TYPE_P (type))
1302 DECL_IS_MALLOC (fndecl) = 1;
1304 /* Set up all attributes for the function. */
1305 DECL_CONTEXT (fndecl) = current_function_decl;
1306 DECL_EXTERNAL (fndecl) = 0;
1308 /* This specifies if a function is globally visible, i.e. it is
1309 the opposite of declaring static in C. */
1310 if (DECL_CONTEXT (fndecl) == NULL_TREE
1311 && !sym->attr.entry_master)
1312 TREE_PUBLIC (fndecl) = 1;
1314 /* TREE_STATIC means the function body is defined here. */
1315 TREE_STATIC (fndecl) = 1;
1317 /* Set attributes for PURE functions. A call to a PURE function in the
1318 Fortran 95 sense is both pure and without side effects in the C
1320 if (attr.pure || attr.elemental)
1322 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
1323 including an alternate return. In that case it can also be
1324 marked as PURE. See also in gfc_get_extern_function_decl(). */
1325 if (attr.function && !gfc_return_by_reference (sym))
1326 DECL_IS_PURE (fndecl) = 1;
1327 TREE_SIDE_EFFECTS (fndecl) = 0;
1330 /* For -fwhole-program to work well, the main program needs to have the
1331 "externally_visible" attribute. */
1332 if (attr.is_main_program)
1333 DECL_ATTRIBUTES (fndecl)
1334 = tree_cons (get_identifier("externally_visible"), NULL_TREE, NULL_TREE);
1336 /* Layout the function declaration and put it in the binding level
1337 of the current function. */
1340 sym->backend_decl = fndecl;
1344 /* Create the DECL_ARGUMENTS for a procedure. */
1347 create_function_arglist (gfc_symbol * sym)
1350 gfc_formal_arglist *f;
1351 tree typelist, hidden_typelist;
1352 tree arglist, hidden_arglist;
1356 fndecl = sym->backend_decl;
1358 /* Build formal argument list. Make sure that their TREE_CONTEXT is
1359 the new FUNCTION_DECL node. */
1360 arglist = NULL_TREE;
1361 hidden_arglist = NULL_TREE;
1362 typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1364 if (sym->attr.entry_master)
1366 type = TREE_VALUE (typelist);
1367 parm = build_decl (PARM_DECL, get_identifier ("__entry"), type);
1369 DECL_CONTEXT (parm) = fndecl;
1370 DECL_ARG_TYPE (parm) = type;
1371 TREE_READONLY (parm) = 1;
1372 gfc_finish_decl (parm);
1373 DECL_ARTIFICIAL (parm) = 1;
1375 arglist = chainon (arglist, parm);
1376 typelist = TREE_CHAIN (typelist);
1379 if (gfc_return_by_reference (sym))
1381 tree type = TREE_VALUE (typelist), length = NULL;
1383 if (sym->ts.type == BT_CHARACTER)
1385 /* Length of character result. */
1386 tree len_type = TREE_VALUE (TREE_CHAIN (typelist));
1387 gcc_assert (len_type == gfc_charlen_type_node);
1389 length = build_decl (PARM_DECL,
1390 get_identifier (".__result"),
1392 if (!sym->ts.cl->length)
1394 sym->ts.cl->backend_decl = length;
1395 TREE_USED (length) = 1;
1397 gcc_assert (TREE_CODE (length) == PARM_DECL);
1398 DECL_CONTEXT (length) = fndecl;
1399 DECL_ARG_TYPE (length) = len_type;
1400 TREE_READONLY (length) = 1;
1401 DECL_ARTIFICIAL (length) = 1;
1402 gfc_finish_decl (length);
1403 if (sym->ts.cl->backend_decl == NULL
1404 || sym->ts.cl->backend_decl == length)
1409 if (sym->ts.cl->backend_decl == NULL)
1411 tree len = build_decl (VAR_DECL,
1412 get_identifier ("..__result"),
1413 gfc_charlen_type_node);
1414 DECL_ARTIFICIAL (len) = 1;
1415 TREE_USED (len) = 1;
1416 sym->ts.cl->backend_decl = len;
1419 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1420 arg = sym->result ? sym->result : sym;
1421 backend_decl = arg->backend_decl;
1422 /* Temporary clear it, so that gfc_sym_type creates complete
1424 arg->backend_decl = NULL;
1425 type = gfc_sym_type (arg);
1426 arg->backend_decl = backend_decl;
1427 type = build_reference_type (type);
1431 parm = build_decl (PARM_DECL, get_identifier ("__result"), type);
1433 DECL_CONTEXT (parm) = fndecl;
1434 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
1435 TREE_READONLY (parm) = 1;
1436 DECL_ARTIFICIAL (parm) = 1;
1437 gfc_finish_decl (parm);
1439 arglist = chainon (arglist, parm);
1440 typelist = TREE_CHAIN (typelist);
1442 if (sym->ts.type == BT_CHARACTER)
1444 gfc_allocate_lang_decl (parm);
1445 arglist = chainon (arglist, length);
1446 typelist = TREE_CHAIN (typelist);
1450 hidden_typelist = typelist;
1451 for (f = sym->formal; f; f = f->next)
1452 if (f->sym != NULL) /* Ignore alternate returns. */
1453 hidden_typelist = TREE_CHAIN (hidden_typelist);
1455 for (f = sym->formal; f; f = f->next)
1457 char name[GFC_MAX_SYMBOL_LEN + 2];
1459 /* Ignore alternate returns. */
1463 type = TREE_VALUE (typelist);
1465 if (f->sym->ts.type == BT_CHARACTER)
1467 tree len_type = TREE_VALUE (hidden_typelist);
1468 tree length = NULL_TREE;
1469 gcc_assert (len_type == gfc_charlen_type_node);
1471 strcpy (&name[1], f->sym->name);
1473 length = build_decl (PARM_DECL, get_identifier (name), len_type);
1475 hidden_arglist = chainon (hidden_arglist, length);
1476 DECL_CONTEXT (length) = fndecl;
1477 DECL_ARTIFICIAL (length) = 1;
1478 DECL_ARG_TYPE (length) = len_type;
1479 TREE_READONLY (length) = 1;
1480 gfc_finish_decl (length);
1482 /* TODO: Check string lengths when -fbounds-check. */
1484 /* Use the passed value for assumed length variables. */
1485 if (!f->sym->ts.cl->length)
1487 TREE_USED (length) = 1;
1488 gcc_assert (!f->sym->ts.cl->backend_decl);
1489 f->sym->ts.cl->backend_decl = length;
1492 hidden_typelist = TREE_CHAIN (hidden_typelist);
1494 if (f->sym->ts.cl->backend_decl == NULL
1495 || f->sym->ts.cl->backend_decl == length)
1497 if (f->sym->ts.cl->backend_decl == NULL)
1498 gfc_create_string_length (f->sym);
1500 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1501 if (f->sym->attr.flavor == FL_PROCEDURE)
1502 type = build_pointer_type (gfc_get_function_type (f->sym));
1504 type = gfc_sym_type (f->sym);
1508 /* For non-constant length array arguments, make sure they use
1509 a different type node from TYPE_ARG_TYPES type. */
1510 if (f->sym->attr.dimension
1511 && type == TREE_VALUE (typelist)
1512 && TREE_CODE (type) == POINTER_TYPE
1513 && GFC_ARRAY_TYPE_P (type)
1514 && f->sym->as->type != AS_ASSUMED_SIZE
1515 && ! COMPLETE_TYPE_P (TREE_TYPE (type)))
1517 if (f->sym->attr.flavor == FL_PROCEDURE)
1518 type = build_pointer_type (gfc_get_function_type (f->sym));
1520 type = gfc_sym_type (f->sym);
1523 /* Build a the argument declaration. */
1524 parm = build_decl (PARM_DECL, gfc_sym_identifier (f->sym), type);
1526 /* Fill in arg stuff. */
1527 DECL_CONTEXT (parm) = fndecl;
1528 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
1529 /* All implementation args are read-only. */
1530 TREE_READONLY (parm) = 1;
1532 gfc_finish_decl (parm);
1534 f->sym->backend_decl = parm;
1536 arglist = chainon (arglist, parm);
1537 typelist = TREE_CHAIN (typelist);
1540 /* Add the hidden string length parameters, unless the procedure
1542 if (!sym->attr.is_bind_c)
1543 arglist = chainon (arglist, hidden_arglist);
1545 gcc_assert (hidden_typelist == NULL_TREE
1546 || TREE_VALUE (hidden_typelist) == void_type_node);
1547 DECL_ARGUMENTS (fndecl) = arglist;
1550 /* Convert FNDECL's code to GIMPLE and handle any nested functions. */
1553 gfc_gimplify_function (tree fndecl)
1555 struct cgraph_node *cgn;
1557 gimplify_function_tree (fndecl);
1558 dump_function (TDI_generic, fndecl);
1560 /* Generate errors for structured block violations. */
1561 /* ??? Could be done as part of resolve_labels. */
1563 diagnose_omp_structured_block_errors (fndecl);
1565 /* Convert all nested functions to GIMPLE now. We do things in this order
1566 so that items like VLA sizes are expanded properly in the context of the
1567 correct function. */
1568 cgn = cgraph_node (fndecl);
1569 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1570 gfc_gimplify_function (cgn->decl);
1574 /* Do the setup necessary before generating the body of a function. */
1577 trans_function_start (gfc_symbol * sym)
1581 fndecl = sym->backend_decl;
1583 /* Let GCC know the current scope is this function. */
1584 current_function_decl = fndecl;
1586 /* Let the world know what we're about to do. */
1587 announce_function (fndecl);
1589 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1591 /* Create RTL for function declaration. */
1592 rest_of_decl_compilation (fndecl, 1, 0);
1595 /* Create RTL for function definition. */
1596 make_decl_rtl (fndecl);
1598 init_function_start (fndecl);
1600 /* Even though we're inside a function body, we still don't want to
1601 call expand_expr to calculate the size of a variable-sized array.
1602 We haven't necessarily assigned RTL to all variables yet, so it's
1603 not safe to try to expand expressions involving them. */
1604 cfun->x_dont_save_pending_sizes_p = 1;
1606 /* function.c requires a push at the start of the function. */
1610 /* Create thunks for alternate entry points. */
1613 build_entry_thunks (gfc_namespace * ns)
1615 gfc_formal_arglist *formal;
1616 gfc_formal_arglist *thunk_formal;
1618 gfc_symbol *thunk_sym;
1626 /* This should always be a toplevel function. */
1627 gcc_assert (current_function_decl == NULL_TREE);
1629 gfc_get_backend_locus (&old_loc);
1630 for (el = ns->entries; el; el = el->next)
1632 thunk_sym = el->sym;
1634 build_function_decl (thunk_sym);
1635 create_function_arglist (thunk_sym);
1637 trans_function_start (thunk_sym);
1639 thunk_fndecl = thunk_sym->backend_decl;
1641 gfc_start_block (&body);
1643 /* Pass extra parameter identifying this entry point. */
1644 tmp = build_int_cst (gfc_array_index_type, el->id);
1645 args = tree_cons (NULL_TREE, tmp, NULL_TREE);
1646 string_args = NULL_TREE;
1648 if (thunk_sym->attr.function)
1650 if (gfc_return_by_reference (ns->proc_name))
1652 tree ref = DECL_ARGUMENTS (current_function_decl);
1653 args = tree_cons (NULL_TREE, ref, args);
1654 if (ns->proc_name->ts.type == BT_CHARACTER)
1655 args = tree_cons (NULL_TREE, TREE_CHAIN (ref),
1660 for (formal = ns->proc_name->formal; formal; formal = formal->next)
1662 /* Ignore alternate returns. */
1663 if (formal->sym == NULL)
1666 /* We don't have a clever way of identifying arguments, so resort to
1667 a brute-force search. */
1668 for (thunk_formal = thunk_sym->formal;
1670 thunk_formal = thunk_formal->next)
1672 if (thunk_formal->sym == formal->sym)
1678 /* Pass the argument. */
1679 DECL_ARTIFICIAL (thunk_formal->sym->backend_decl) = 1;
1680 args = tree_cons (NULL_TREE, thunk_formal->sym->backend_decl,
1682 if (formal->sym->ts.type == BT_CHARACTER)
1684 tmp = thunk_formal->sym->ts.cl->backend_decl;
1685 string_args = tree_cons (NULL_TREE, tmp, string_args);
1690 /* Pass NULL for a missing argument. */
1691 args = tree_cons (NULL_TREE, null_pointer_node, args);
1692 if (formal->sym->ts.type == BT_CHARACTER)
1694 tmp = build_int_cst (gfc_charlen_type_node, 0);
1695 string_args = tree_cons (NULL_TREE, tmp, string_args);
1700 /* Call the master function. */
1701 args = nreverse (args);
1702 args = chainon (args, nreverse (string_args));
1703 tmp = ns->proc_name->backend_decl;
1704 tmp = build_function_call_expr (tmp, args);
1705 if (ns->proc_name->attr.mixed_entry_master)
1707 tree union_decl, field;
1708 tree master_type = TREE_TYPE (ns->proc_name->backend_decl);
1710 union_decl = build_decl (VAR_DECL, get_identifier ("__result"),
1711 TREE_TYPE (master_type));
1712 DECL_ARTIFICIAL (union_decl) = 1;
1713 DECL_EXTERNAL (union_decl) = 0;
1714 TREE_PUBLIC (union_decl) = 0;
1715 TREE_USED (union_decl) = 1;
1716 layout_decl (union_decl, 0);
1717 pushdecl (union_decl);
1719 DECL_CONTEXT (union_decl) = current_function_decl;
1720 tmp = fold_build2 (MODIFY_EXPR, TREE_TYPE (union_decl),
1722 gfc_add_expr_to_block (&body, tmp);
1724 for (field = TYPE_FIELDS (TREE_TYPE (union_decl));
1725 field; field = TREE_CHAIN (field))
1726 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
1727 thunk_sym->result->name) == 0)
1729 gcc_assert (field != NULL_TREE);
1730 tmp = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
1731 union_decl, field, NULL_TREE);
1732 tmp = fold_build2 (MODIFY_EXPR,
1733 TREE_TYPE (DECL_RESULT (current_function_decl)),
1734 DECL_RESULT (current_function_decl), tmp);
1735 tmp = build1_v (RETURN_EXPR, tmp);
1737 else if (TREE_TYPE (DECL_RESULT (current_function_decl))
1740 tmp = fold_build2 (MODIFY_EXPR,
1741 TREE_TYPE (DECL_RESULT (current_function_decl)),
1742 DECL_RESULT (current_function_decl), tmp);
1743 tmp = build1_v (RETURN_EXPR, tmp);
1745 gfc_add_expr_to_block (&body, tmp);
1747 /* Finish off this function and send it for code generation. */
1748 DECL_SAVED_TREE (thunk_fndecl) = gfc_finish_block (&body);
1750 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl)) = thunk_fndecl;
1752 /* Output the GENERIC tree. */
1753 dump_function (TDI_original, thunk_fndecl);
1755 /* Store the end of the function, so that we get good line number
1756 info for the epilogue. */
1757 cfun->function_end_locus = input_location;
1759 /* We're leaving the context of this function, so zap cfun.
1760 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
1761 tree_rest_of_compilation. */
1764 current_function_decl = NULL_TREE;
1766 gfc_gimplify_function (thunk_fndecl);
1767 cgraph_finalize_function (thunk_fndecl, false);
1769 /* We share the symbols in the formal argument list with other entry
1770 points and the master function. Clear them so that they are
1771 recreated for each function. */
1772 for (formal = thunk_sym->formal; formal; formal = formal->next)
1773 if (formal->sym != NULL) /* Ignore alternate returns. */
1775 formal->sym->backend_decl = NULL_TREE;
1776 if (formal->sym->ts.type == BT_CHARACTER)
1777 formal->sym->ts.cl->backend_decl = NULL_TREE;
1780 if (thunk_sym->attr.function)
1782 if (thunk_sym->ts.type == BT_CHARACTER)
1783 thunk_sym->ts.cl->backend_decl = NULL_TREE;
1784 if (thunk_sym->result->ts.type == BT_CHARACTER)
1785 thunk_sym->result->ts.cl->backend_decl = NULL_TREE;
1789 gfc_set_backend_locus (&old_loc);
1793 /* Create a decl for a function, and create any thunks for alternate entry
1797 gfc_create_function_decl (gfc_namespace * ns)
1799 /* Create a declaration for the master function. */
1800 build_function_decl (ns->proc_name);
1802 /* Compile the entry thunks. */
1804 build_entry_thunks (ns);
1806 /* Now create the read argument list. */
1807 create_function_arglist (ns->proc_name);
1810 /* Return the decl used to hold the function return value. If
1811 parent_flag is set, the context is the parent_scope. */
1814 gfc_get_fake_result_decl (gfc_symbol * sym, int parent_flag)
1818 tree this_fake_result_decl;
1819 tree this_function_decl;
1821 char name[GFC_MAX_SYMBOL_LEN + 10];
1825 this_fake_result_decl = parent_fake_result_decl;
1826 this_function_decl = DECL_CONTEXT (current_function_decl);
1830 this_fake_result_decl = current_fake_result_decl;
1831 this_function_decl = current_function_decl;
1835 && sym->ns->proc_name->backend_decl == this_function_decl
1836 && sym->ns->proc_name->attr.entry_master
1837 && sym != sym->ns->proc_name)
1840 if (this_fake_result_decl != NULL)
1841 for (t = TREE_CHAIN (this_fake_result_decl); t; t = TREE_CHAIN (t))
1842 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t)), sym->name) == 0)
1845 return TREE_VALUE (t);
1846 decl = gfc_get_fake_result_decl (sym->ns->proc_name, parent_flag);
1849 this_fake_result_decl = parent_fake_result_decl;
1851 this_fake_result_decl = current_fake_result_decl;
1853 if (decl && sym->ns->proc_name->attr.mixed_entry_master)
1857 for (field = TYPE_FIELDS (TREE_TYPE (decl));
1858 field; field = TREE_CHAIN (field))
1859 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
1863 gcc_assert (field != NULL_TREE);
1864 decl = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
1865 decl, field, NULL_TREE);
1868 var = create_tmp_var_raw (TREE_TYPE (decl), sym->name);
1870 gfc_add_decl_to_parent_function (var);
1872 gfc_add_decl_to_function (var);
1874 SET_DECL_VALUE_EXPR (var, decl);
1875 DECL_HAS_VALUE_EXPR_P (var) = 1;
1876 GFC_DECL_RESULT (var) = 1;
1878 TREE_CHAIN (this_fake_result_decl)
1879 = tree_cons (get_identifier (sym->name), var,
1880 TREE_CHAIN (this_fake_result_decl));
1884 if (this_fake_result_decl != NULL_TREE)
1885 return TREE_VALUE (this_fake_result_decl);
1887 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
1892 if (sym->ts.type == BT_CHARACTER)
1894 if (sym->ts.cl->backend_decl == NULL_TREE)
1895 length = gfc_create_string_length (sym);
1897 length = sym->ts.cl->backend_decl;
1898 if (TREE_CODE (length) == VAR_DECL
1899 && DECL_CONTEXT (length) == NULL_TREE)
1900 gfc_add_decl_to_function (length);
1903 if (gfc_return_by_reference (sym))
1905 decl = DECL_ARGUMENTS (this_function_decl);
1907 if (sym->ns->proc_name->backend_decl == this_function_decl
1908 && sym->ns->proc_name->attr.entry_master)
1909 decl = TREE_CHAIN (decl);
1911 TREE_USED (decl) = 1;
1913 decl = gfc_build_dummy_array_decl (sym, decl);
1917 sprintf (name, "__result_%.20s",
1918 IDENTIFIER_POINTER (DECL_NAME (this_function_decl)));
1920 if (!sym->attr.mixed_entry_master && sym->attr.function)
1921 decl = build_decl (VAR_DECL, get_identifier (name),
1922 gfc_sym_type (sym));
1924 decl = build_decl (VAR_DECL, get_identifier (name),
1925 TREE_TYPE (TREE_TYPE (this_function_decl)));
1926 DECL_ARTIFICIAL (decl) = 1;
1927 DECL_EXTERNAL (decl) = 0;
1928 TREE_PUBLIC (decl) = 0;
1929 TREE_USED (decl) = 1;
1930 GFC_DECL_RESULT (decl) = 1;
1931 TREE_ADDRESSABLE (decl) = 1;
1933 layout_decl (decl, 0);
1936 gfc_add_decl_to_parent_function (decl);
1938 gfc_add_decl_to_function (decl);
1942 parent_fake_result_decl = build_tree_list (NULL, decl);
1944 current_fake_result_decl = build_tree_list (NULL, decl);
1950 /* Builds a function decl. The remaining parameters are the types of the
1951 function arguments. Negative nargs indicates a varargs function. */
1954 gfc_build_library_function_decl (tree name, tree rettype, int nargs, ...)
1963 /* Library functions must be declared with global scope. */
1964 gcc_assert (current_function_decl == NULL_TREE);
1966 va_start (p, nargs);
1969 /* Create a list of the argument types. */
1970 for (arglist = NULL_TREE, n = abs (nargs); n > 0; n--)
1972 argtype = va_arg (p, tree);
1973 arglist = gfc_chainon_list (arglist, argtype);
1978 /* Terminate the list. */
1979 arglist = gfc_chainon_list (arglist, void_type_node);
1982 /* Build the function type and decl. */
1983 fntype = build_function_type (rettype, arglist);
1984 fndecl = build_decl (FUNCTION_DECL, name, fntype);
1986 /* Mark this decl as external. */
1987 DECL_EXTERNAL (fndecl) = 1;
1988 TREE_PUBLIC (fndecl) = 1;
1994 rest_of_decl_compilation (fndecl, 1, 0);
2000 gfc_build_intrinsic_function_decls (void)
2002 tree gfc_int4_type_node = gfc_get_int_type (4);
2003 tree gfc_int8_type_node = gfc_get_int_type (8);
2004 tree gfc_int16_type_node = gfc_get_int_type (16);
2005 tree gfc_logical4_type_node = gfc_get_logical_type (4);
2007 /* String functions. */
2008 gfor_fndecl_compare_string =
2009 gfc_build_library_function_decl (get_identifier (PREFIX("compare_string")),
2010 integer_type_node, 4,
2011 gfc_charlen_type_node, pchar_type_node,
2012 gfc_charlen_type_node, pchar_type_node);
2014 gfor_fndecl_concat_string =
2015 gfc_build_library_function_decl (get_identifier (PREFIX("concat_string")),
2018 gfc_charlen_type_node, pchar_type_node,
2019 gfc_charlen_type_node, pchar_type_node,
2020 gfc_charlen_type_node, pchar_type_node);
2022 gfor_fndecl_string_len_trim =
2023 gfc_build_library_function_decl (get_identifier (PREFIX("string_len_trim")),
2025 2, gfc_charlen_type_node,
2028 gfor_fndecl_string_index =
2029 gfc_build_library_function_decl (get_identifier (PREFIX("string_index")),
2031 5, gfc_charlen_type_node, pchar_type_node,
2032 gfc_charlen_type_node, pchar_type_node,
2033 gfc_logical4_type_node);
2035 gfor_fndecl_string_scan =
2036 gfc_build_library_function_decl (get_identifier (PREFIX("string_scan")),
2038 5, gfc_charlen_type_node, pchar_type_node,
2039 gfc_charlen_type_node, pchar_type_node,
2040 gfc_logical4_type_node);
2042 gfor_fndecl_string_verify =
2043 gfc_build_library_function_decl (get_identifier (PREFIX("string_verify")),
2045 5, gfc_charlen_type_node, pchar_type_node,
2046 gfc_charlen_type_node, pchar_type_node,
2047 gfc_logical4_type_node);
2049 gfor_fndecl_string_trim =
2050 gfc_build_library_function_decl (get_identifier (PREFIX("string_trim")),
2053 build_pointer_type (gfc_charlen_type_node),
2055 gfc_charlen_type_node,
2058 gfor_fndecl_string_minmax =
2059 gfc_build_library_function_decl (get_identifier (PREFIX("string_minmax")),
2061 build_pointer_type (gfc_charlen_type_node),
2062 ppvoid_type_node, integer_type_node,
2065 gfor_fndecl_ttynam =
2066 gfc_build_library_function_decl (get_identifier (PREFIX("ttynam")),
2070 gfc_charlen_type_node,
2074 gfc_build_library_function_decl (get_identifier (PREFIX("fdate")),
2078 gfc_charlen_type_node);
2081 gfc_build_library_function_decl (get_identifier (PREFIX("ctime")),
2085 gfc_charlen_type_node,
2086 gfc_int8_type_node);
2088 gfor_fndecl_adjustl =
2089 gfc_build_library_function_decl (get_identifier (PREFIX("adjustl")),
2093 gfc_charlen_type_node, pchar_type_node);
2095 gfor_fndecl_adjustr =
2096 gfc_build_library_function_decl (get_identifier (PREFIX("adjustr")),
2100 gfc_charlen_type_node, pchar_type_node);
2102 gfor_fndecl_si_kind =
2103 gfc_build_library_function_decl (get_identifier
2104 (PREFIX("selected_int_kind")),
2109 gfor_fndecl_sr_kind =
2110 gfc_build_library_function_decl (get_identifier
2111 (PREFIX("selected_real_kind")),
2116 /* Power functions. */
2118 tree ctype, rtype, itype, jtype;
2119 int rkind, ikind, jkind;
2122 static int ikinds[NIKINDS] = {4, 8, 16};
2123 static int rkinds[NRKINDS] = {4, 8, 10, 16};
2124 char name[PREFIX_LEN + 12]; /* _gfortran_pow_?n_?n */
2126 for (ikind=0; ikind < NIKINDS; ikind++)
2128 itype = gfc_get_int_type (ikinds[ikind]);
2130 for (jkind=0; jkind < NIKINDS; jkind++)
2132 jtype = gfc_get_int_type (ikinds[jkind]);
2135 sprintf(name, PREFIX("pow_i%d_i%d"), ikinds[ikind],
2137 gfor_fndecl_math_powi[jkind][ikind].integer =
2138 gfc_build_library_function_decl (get_identifier (name),
2139 jtype, 2, jtype, itype);
2140 TREE_READONLY (gfor_fndecl_math_powi[jkind][ikind].integer) = 1;
2144 for (rkind = 0; rkind < NRKINDS; rkind ++)
2146 rtype = gfc_get_real_type (rkinds[rkind]);
2149 sprintf(name, PREFIX("pow_r%d_i%d"), rkinds[rkind],
2151 gfor_fndecl_math_powi[rkind][ikind].real =
2152 gfc_build_library_function_decl (get_identifier (name),
2153 rtype, 2, rtype, itype);
2154 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].real) = 1;
2157 ctype = gfc_get_complex_type (rkinds[rkind]);
2160 sprintf(name, PREFIX("pow_c%d_i%d"), rkinds[rkind],
2162 gfor_fndecl_math_powi[rkind][ikind].cmplx =
2163 gfc_build_library_function_decl (get_identifier (name),
2164 ctype, 2,ctype, itype);
2165 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1;
2173 gfor_fndecl_math_ishftc4 =
2174 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc4")),
2176 3, gfc_int4_type_node,
2177 gfc_int4_type_node, gfc_int4_type_node);
2178 gfor_fndecl_math_ishftc8 =
2179 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc8")),
2181 3, gfc_int8_type_node,
2182 gfc_int4_type_node, gfc_int4_type_node);
2183 if (gfc_int16_type_node)
2184 gfor_fndecl_math_ishftc16 =
2185 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc16")),
2186 gfc_int16_type_node, 3,
2187 gfc_int16_type_node,
2189 gfc_int4_type_node);
2191 /* BLAS functions. */
2193 tree pint = build_pointer_type (integer_type_node);
2194 tree ps = build_pointer_type (gfc_get_real_type (gfc_default_real_kind));
2195 tree pd = build_pointer_type (gfc_get_real_type (gfc_default_double_kind));
2196 tree pc = build_pointer_type (gfc_get_complex_type (gfc_default_real_kind));
2197 tree pz = build_pointer_type
2198 (gfc_get_complex_type (gfc_default_double_kind));
2200 gfor_fndecl_sgemm = gfc_build_library_function_decl
2202 (gfc_option.flag_underscoring ? "sgemm_"
2204 void_type_node, 15, pchar_type_node,
2205 pchar_type_node, pint, pint, pint, ps, ps, pint,
2206 ps, pint, ps, ps, pint, integer_type_node,
2208 gfor_fndecl_dgemm = gfc_build_library_function_decl
2210 (gfc_option.flag_underscoring ? "dgemm_"
2212 void_type_node, 15, pchar_type_node,
2213 pchar_type_node, pint, pint, pint, pd, pd, pint,
2214 pd, pint, pd, pd, pint, integer_type_node,
2216 gfor_fndecl_cgemm = gfc_build_library_function_decl
2218 (gfc_option.flag_underscoring ? "cgemm_"
2220 void_type_node, 15, pchar_type_node,
2221 pchar_type_node, pint, pint, pint, pc, pc, pint,
2222 pc, pint, pc, pc, pint, integer_type_node,
2224 gfor_fndecl_zgemm = gfc_build_library_function_decl
2226 (gfc_option.flag_underscoring ? "zgemm_"
2228 void_type_node, 15, pchar_type_node,
2229 pchar_type_node, pint, pint, pint, pz, pz, pint,
2230 pz, pint, pz, pz, pint, integer_type_node,
2234 /* Other functions. */
2236 gfc_build_library_function_decl (get_identifier (PREFIX("size0")),
2237 gfc_array_index_type,
2238 1, pvoid_type_node);
2240 gfc_build_library_function_decl (get_identifier (PREFIX("size1")),
2241 gfc_array_index_type,
2243 gfc_array_index_type);
2246 gfc_build_library_function_decl (get_identifier (PREFIX ("iargc")),
2252 /* Make prototypes for runtime library functions. */
2255 gfc_build_builtin_function_decls (void)
2257 tree gfc_int4_type_node = gfc_get_int_type (4);
2259 gfor_fndecl_stop_numeric =
2260 gfc_build_library_function_decl (get_identifier (PREFIX("stop_numeric")),
2261 void_type_node, 1, gfc_int4_type_node);
2262 /* Stop doesn't return. */
2263 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric) = 1;
2265 gfor_fndecl_stop_string =
2266 gfc_build_library_function_decl (get_identifier (PREFIX("stop_string")),
2267 void_type_node, 2, pchar_type_node,
2268 gfc_int4_type_node);
2269 /* Stop doesn't return. */
2270 TREE_THIS_VOLATILE (gfor_fndecl_stop_string) = 1;
2272 gfor_fndecl_pause_numeric =
2273 gfc_build_library_function_decl (get_identifier (PREFIX("pause_numeric")),
2274 void_type_node, 1, gfc_int4_type_node);
2276 gfor_fndecl_pause_string =
2277 gfc_build_library_function_decl (get_identifier (PREFIX("pause_string")),
2278 void_type_node, 2, pchar_type_node,
2279 gfc_int4_type_node);
2281 gfor_fndecl_select_string =
2282 gfc_build_library_function_decl (get_identifier (PREFIX("select_string")),
2283 integer_type_node, 4, pvoid_type_node,
2284 integer_type_node, pchar_type_node,
2287 gfor_fndecl_runtime_error =
2288 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error")),
2289 void_type_node, -1, pchar_type_node);
2290 /* The runtime_error function does not return. */
2291 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error) = 1;
2293 gfor_fndecl_runtime_error_at =
2294 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error_at")),
2295 void_type_node, -2, pchar_type_node,
2297 /* The runtime_error_at function does not return. */
2298 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at) = 1;
2300 gfor_fndecl_generate_error =
2301 gfc_build_library_function_decl (get_identifier (PREFIX("generate_error")),
2302 void_type_node, 3, pvoid_type_node,
2303 integer_type_node, pchar_type_node);
2305 gfor_fndecl_os_error =
2306 gfc_build_library_function_decl (get_identifier (PREFIX("os_error")),
2307 void_type_node, 1, pchar_type_node);
2308 /* The runtime_error function does not return. */
2309 TREE_THIS_VOLATILE (gfor_fndecl_os_error) = 1;
2311 gfor_fndecl_set_fpe =
2312 gfc_build_library_function_decl (get_identifier (PREFIX("set_fpe")),
2313 void_type_node, 1, integer_type_node);
2315 /* Keep the array dimension in sync with the call, later in this file. */
2316 gfor_fndecl_set_options =
2317 gfc_build_library_function_decl (get_identifier (PREFIX("set_options")),
2318 void_type_node, 2, integer_type_node,
2321 gfor_fndecl_set_convert =
2322 gfc_build_library_function_decl (get_identifier (PREFIX("set_convert")),
2323 void_type_node, 1, integer_type_node);
2325 gfor_fndecl_set_record_marker =
2326 gfc_build_library_function_decl (get_identifier (PREFIX("set_record_marker")),
2327 void_type_node, 1, integer_type_node);
2329 gfor_fndecl_set_max_subrecord_length =
2330 gfc_build_library_function_decl (get_identifier (PREFIX("set_max_subrecord_length")),
2331 void_type_node, 1, integer_type_node);
2333 gfor_fndecl_in_pack = gfc_build_library_function_decl (
2334 get_identifier (PREFIX("internal_pack")),
2335 pvoid_type_node, 1, pvoid_type_node);
2337 gfor_fndecl_in_unpack = gfc_build_library_function_decl (
2338 get_identifier (PREFIX("internal_unpack")),
2339 void_type_node, 2, pvoid_type_node, pvoid_type_node);
2341 gfor_fndecl_associated =
2342 gfc_build_library_function_decl (
2343 get_identifier (PREFIX("associated")),
2344 integer_type_node, 2, ppvoid_type_node,
2347 gfc_build_intrinsic_function_decls ();
2348 gfc_build_intrinsic_lib_fndecls ();
2349 gfc_build_io_library_fndecls ();
2353 /* Evaluate the length of dummy character variables. */
2356 gfc_trans_dummy_character (gfc_symbol *sym, gfc_charlen *cl, tree fnbody)
2360 gfc_finish_decl (cl->backend_decl);
2362 gfc_start_block (&body);
2364 /* Evaluate the string length expression. */
2365 gfc_conv_string_length (cl, &body);
2367 gfc_trans_vla_type_sizes (sym, &body);
2369 gfc_add_expr_to_block (&body, fnbody);
2370 return gfc_finish_block (&body);
2374 /* Allocate and cleanup an automatic character variable. */
2377 gfc_trans_auto_character_variable (gfc_symbol * sym, tree fnbody)
2383 gcc_assert (sym->backend_decl);
2384 gcc_assert (sym->ts.cl && sym->ts.cl->length);
2386 gfc_start_block (&body);
2388 /* Evaluate the string length expression. */
2389 gfc_conv_string_length (sym->ts.cl, &body);
2391 gfc_trans_vla_type_sizes (sym, &body);
2393 decl = sym->backend_decl;
2395 /* Emit a DECL_EXPR for this variable, which will cause the
2396 gimplifier to allocate storage, and all that good stuff. */
2397 tmp = fold_build1 (DECL_EXPR, TREE_TYPE (decl), decl);
2398 gfc_add_expr_to_block (&body, tmp);
2400 gfc_add_expr_to_block (&body, fnbody);
2401 return gfc_finish_block (&body);
2404 /* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
2407 gfc_trans_assign_aux_var (gfc_symbol * sym, tree fnbody)
2411 gcc_assert (sym->backend_decl);
2412 gfc_start_block (&body);
2414 /* Set the initial value to length. See the comments in
2415 function gfc_add_assign_aux_vars in this file. */
2416 gfc_add_modify_expr (&body, GFC_DECL_STRING_LEN (sym->backend_decl),
2417 build_int_cst (NULL_TREE, -2));
2419 gfc_add_expr_to_block (&body, fnbody);
2420 return gfc_finish_block (&body);
2424 gfc_trans_vla_one_sizepos (tree *tp, stmtblock_t *body)
2426 tree t = *tp, var, val;
2428 if (t == NULL || t == error_mark_node)
2430 if (TREE_CONSTANT (t) || DECL_P (t))
2433 if (TREE_CODE (t) == SAVE_EXPR)
2435 if (SAVE_EXPR_RESOLVED_P (t))
2437 *tp = TREE_OPERAND (t, 0);
2440 val = TREE_OPERAND (t, 0);
2445 var = gfc_create_var_np (TREE_TYPE (t), NULL);
2446 gfc_add_decl_to_function (var);
2447 gfc_add_modify_expr (body, var, val);
2448 if (TREE_CODE (t) == SAVE_EXPR)
2449 TREE_OPERAND (t, 0) = var;
2454 gfc_trans_vla_type_sizes_1 (tree type, stmtblock_t *body)
2458 if (type == NULL || type == error_mark_node)
2461 type = TYPE_MAIN_VARIANT (type);
2463 if (TREE_CODE (type) == INTEGER_TYPE)
2465 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type), body);
2466 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type), body);
2468 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2470 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
2471 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
2474 else if (TREE_CODE (type) == ARRAY_TYPE)
2476 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type), body);
2477 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type), body);
2478 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type), body);
2479 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type), body);
2481 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2483 TYPE_SIZE (t) = TYPE_SIZE (type);
2484 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
2489 /* Make sure all type sizes and array domains are either constant,
2490 or variable or parameter decls. This is a simplified variant
2491 of gimplify_type_sizes, but we can't use it here, as none of the
2492 variables in the expressions have been gimplified yet.
2493 As type sizes and domains for various variable length arrays
2494 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
2495 time, without this routine gimplify_type_sizes in the middle-end
2496 could result in the type sizes being gimplified earlier than where
2497 those variables are initialized. */
2500 gfc_trans_vla_type_sizes (gfc_symbol *sym, stmtblock_t *body)
2502 tree type = TREE_TYPE (sym->backend_decl);
2504 if (TREE_CODE (type) == FUNCTION_TYPE
2505 && (sym->attr.function || sym->attr.result || sym->attr.entry))
2507 if (! current_fake_result_decl)
2510 type = TREE_TYPE (TREE_VALUE (current_fake_result_decl));
2513 while (POINTER_TYPE_P (type))
2514 type = TREE_TYPE (type);
2516 if (GFC_DESCRIPTOR_TYPE_P (type))
2518 tree etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
2520 while (POINTER_TYPE_P (etype))
2521 etype = TREE_TYPE (etype);
2523 gfc_trans_vla_type_sizes_1 (etype, body);
2526 gfc_trans_vla_type_sizes_1 (type, body);
2530 /* Initialize a derived type by building an lvalue from the symbol
2531 and using trans_assignment to do the work. */
2533 gfc_init_default_dt (gfc_symbol * sym, tree body)
2535 stmtblock_t fnblock;
2540 gfc_init_block (&fnblock);
2541 gcc_assert (!sym->attr.allocatable);
2542 gfc_set_sym_referenced (sym);
2543 e = gfc_lval_expr_from_sym (sym);
2544 tmp = gfc_trans_assignment (e, sym->value, false);
2545 if (sym->attr.dummy)
2547 present = gfc_conv_expr_present (sym);
2548 tmp = build3 (COND_EXPR, TREE_TYPE (tmp), present,
2549 tmp, build_empty_stmt ());
2551 gfc_add_expr_to_block (&fnblock, tmp);
2554 gfc_add_expr_to_block (&fnblock, body);
2555 return gfc_finish_block (&fnblock);
2559 /* Initialize INTENT(OUT) derived type dummies. */
2561 init_intent_out_dt (gfc_symbol * proc_sym, tree body)
2563 stmtblock_t fnblock;
2564 gfc_formal_arglist *f;
2566 gfc_init_block (&fnblock);
2567 for (f = proc_sym->formal; f; f = f->next)
2568 if (f->sym && f->sym->attr.intent == INTENT_OUT
2569 && f->sym->ts.type == BT_DERIVED
2570 && !f->sym->ts.derived->attr.alloc_comp
2572 body = gfc_init_default_dt (f->sym, body);
2574 gfc_add_expr_to_block (&fnblock, body);
2575 return gfc_finish_block (&fnblock);
2579 /* Generate function entry and exit code, and add it to the function body.
2581 Allocation and initialization of array variables.
2582 Allocation of character string variables.
2583 Initialization and possibly repacking of dummy arrays.
2584 Initialization of ASSIGN statement auxiliary variable. */
2587 gfc_trans_deferred_vars (gfc_symbol * proc_sym, tree fnbody)
2591 gfc_formal_arglist *f;
2593 bool seen_trans_deferred_array = false;
2595 /* Deal with implicit return variables. Explicit return variables will
2596 already have been added. */
2597 if (gfc_return_by_reference (proc_sym) && proc_sym->result == proc_sym)
2599 if (!current_fake_result_decl)
2601 gfc_entry_list *el = NULL;
2602 if (proc_sym->attr.entry_master)
2604 for (el = proc_sym->ns->entries; el; el = el->next)
2605 if (el->sym != el->sym->result)
2608 /* TODO: move to the appropriate place in resolve.c. */
2609 if (warn_return_type && el == NULL)
2610 gfc_warning ("Return value of function '%s' at %L not set",
2611 proc_sym->name, &proc_sym->declared_at);
2613 else if (proc_sym->as)
2615 tree result = TREE_VALUE (current_fake_result_decl);
2616 fnbody = gfc_trans_dummy_array_bias (proc_sym, result, fnbody);
2618 /* An automatic character length, pointer array result. */
2619 if (proc_sym->ts.type == BT_CHARACTER
2620 && TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL)
2621 fnbody = gfc_trans_dummy_character (proc_sym, proc_sym->ts.cl,
2624 else if (proc_sym->ts.type == BT_CHARACTER)
2626 if (TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL)
2627 fnbody = gfc_trans_dummy_character (proc_sym, proc_sym->ts.cl,
2631 gcc_assert (gfc_option.flag_f2c
2632 && proc_sym->ts.type == BT_COMPLEX);
2635 /* Initialize the INTENT(OUT) derived type dummy arguments. This
2636 should be done here so that the offsets and lbounds of arrays
2638 fnbody = init_intent_out_dt (proc_sym, fnbody);
2640 for (sym = proc_sym->tlink; sym != proc_sym; sym = sym->tlink)
2642 bool sym_has_alloc_comp = (sym->ts.type == BT_DERIVED)
2643 && sym->ts.derived->attr.alloc_comp;
2644 if (sym->attr.dimension)
2646 switch (sym->as->type)
2649 if (sym->attr.dummy || sym->attr.result)
2651 gfc_trans_dummy_array_bias (sym, sym->backend_decl, fnbody);
2652 else if (sym->attr.pointer || sym->attr.allocatable)
2654 if (TREE_STATIC (sym->backend_decl))
2655 gfc_trans_static_array_pointer (sym);
2658 seen_trans_deferred_array = true;
2659 fnbody = gfc_trans_deferred_array (sym, fnbody);
2664 if (sym_has_alloc_comp)
2666 seen_trans_deferred_array = true;
2667 fnbody = gfc_trans_deferred_array (sym, fnbody);
2669 else if (sym->ts.type == BT_DERIVED
2672 && sym->attr.save == SAVE_NONE)
2673 fnbody = gfc_init_default_dt (sym, fnbody);
2675 gfc_get_backend_locus (&loc);
2676 gfc_set_backend_locus (&sym->declared_at);
2677 fnbody = gfc_trans_auto_array_allocation (sym->backend_decl,
2679 gfc_set_backend_locus (&loc);
2683 case AS_ASSUMED_SIZE:
2684 /* Must be a dummy parameter. */
2685 gcc_assert (sym->attr.dummy);
2687 /* We should always pass assumed size arrays the g77 way. */
2688 fnbody = gfc_trans_g77_array (sym, fnbody);
2691 case AS_ASSUMED_SHAPE:
2692 /* Must be a dummy parameter. */
2693 gcc_assert (sym->attr.dummy);
2695 fnbody = gfc_trans_dummy_array_bias (sym, sym->backend_decl,
2700 seen_trans_deferred_array = true;
2701 fnbody = gfc_trans_deferred_array (sym, fnbody);
2707 if (sym_has_alloc_comp && !seen_trans_deferred_array)
2708 fnbody = gfc_trans_deferred_array (sym, fnbody);
2710 else if (sym_has_alloc_comp)
2711 fnbody = gfc_trans_deferred_array (sym, fnbody);
2712 else if (sym->ts.type == BT_CHARACTER)
2714 gfc_get_backend_locus (&loc);
2715 gfc_set_backend_locus (&sym->declared_at);
2716 if (sym->attr.dummy || sym->attr.result)
2717 fnbody = gfc_trans_dummy_character (sym, sym->ts.cl, fnbody);
2719 fnbody = gfc_trans_auto_character_variable (sym, fnbody);
2720 gfc_set_backend_locus (&loc);
2722 else if (sym->attr.assign)
2724 gfc_get_backend_locus (&loc);
2725 gfc_set_backend_locus (&sym->declared_at);
2726 fnbody = gfc_trans_assign_aux_var (sym, fnbody);
2727 gfc_set_backend_locus (&loc);
2729 else if (sym->ts.type == BT_DERIVED
2732 && sym->attr.save == SAVE_NONE)
2733 fnbody = gfc_init_default_dt (sym, fnbody);
2738 gfc_init_block (&body);
2740 for (f = proc_sym->formal; f; f = f->next)
2742 if (f->sym && f->sym->tlink == NULL && f->sym->ts.type == BT_CHARACTER)
2744 gcc_assert (f->sym->ts.cl->backend_decl != NULL);
2745 if (TREE_CODE (f->sym->ts.cl->backend_decl) == PARM_DECL)
2746 gfc_trans_vla_type_sizes (f->sym, &body);
2750 if (gfc_return_by_reference (proc_sym) && proc_sym->ts.type == BT_CHARACTER
2751 && current_fake_result_decl != NULL)
2753 gcc_assert (proc_sym->ts.cl->backend_decl != NULL);
2754 if (TREE_CODE (proc_sym->ts.cl->backend_decl) == PARM_DECL)
2755 gfc_trans_vla_type_sizes (proc_sym, &body);
2758 gfc_add_expr_to_block (&body, fnbody);
2759 return gfc_finish_block (&body);
2763 /* Output an initialized decl for a module variable. */
2766 gfc_create_module_variable (gfc_symbol * sym)
2770 /* Module functions with alternate entries are dealt with later and
2771 would get caught by the next condition. */
2772 if (sym->attr.entry)
2775 /* Make sure we convert the types of the derived types from iso_c_binding
2777 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
2778 && sym->ts.type == BT_DERIVED)
2779 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
2781 /* Only output variables and array valued, or derived type,
2783 if (sym->attr.flavor != FL_VARIABLE
2784 && !(sym->attr.flavor == FL_PARAMETER
2785 && (sym->attr.dimension || sym->ts.type == BT_DERIVED)))
2788 /* Don't generate variables from other modules. Variables from
2789 COMMONs will already have been generated. */
2790 if (sym->attr.use_assoc || sym->attr.in_common)
2793 /* Equivalenced variables arrive here after creation. */
2794 if (sym->backend_decl
2795 && (sym->equiv_built || sym->attr.in_equivalence))
2798 if (sym->backend_decl)
2799 internal_error ("backend decl for module variable %s already exists",
2802 /* We always want module variables to be created. */
2803 sym->attr.referenced = 1;
2804 /* Create the decl. */
2805 decl = gfc_get_symbol_decl (sym);
2807 /* Create the variable. */
2809 rest_of_decl_compilation (decl, 1, 0);
2811 /* Also add length of strings. */
2812 if (sym->ts.type == BT_CHARACTER)
2816 length = sym->ts.cl->backend_decl;
2817 if (!INTEGER_CST_P (length))
2820 rest_of_decl_compilation (length, 1, 0);
2826 /* Generate all the required code for module variables. */
2829 gfc_generate_module_vars (gfc_namespace * ns)
2831 module_namespace = ns;
2833 /* Check if the frontend left the namespace in a reasonable state. */
2834 gcc_assert (ns->proc_name && !ns->proc_name->tlink);
2836 /* Generate COMMON blocks. */
2837 gfc_trans_common (ns);
2839 /* Create decls for all the module variables. */
2840 gfc_traverse_ns (ns, gfc_create_module_variable);
2844 gfc_generate_contained_functions (gfc_namespace * parent)
2848 /* We create all the prototypes before generating any code. */
2849 for (ns = parent->contained; ns; ns = ns->sibling)
2851 /* Skip namespaces from used modules. */
2852 if (ns->parent != parent)
2855 gfc_create_function_decl (ns);
2858 for (ns = parent->contained; ns; ns = ns->sibling)
2860 /* Skip namespaces from used modules. */
2861 if (ns->parent != parent)
2864 gfc_generate_function_code (ns);
2869 /* Drill down through expressions for the array specification bounds and
2870 character length calling generate_local_decl for all those variables
2871 that have not already been declared. */
2874 generate_local_decl (gfc_symbol *);
2876 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
2879 expr_decls (gfc_expr *e, gfc_symbol *sym,
2880 int *f ATTRIBUTE_UNUSED)
2882 if (e->expr_type != EXPR_VARIABLE
2883 || sym == e->symtree->n.sym
2884 || e->symtree->n.sym->mark
2885 || e->symtree->n.sym->ns != sym->ns)
2888 generate_local_decl (e->symtree->n.sym);
2893 generate_expr_decls (gfc_symbol *sym, gfc_expr *e)
2895 gfc_traverse_expr (e, sym, expr_decls, 0);
2899 /* Check for dependencies in the character length and array spec. */
2902 generate_dependency_declarations (gfc_symbol *sym)
2906 if (sym->ts.type == BT_CHARACTER
2908 && sym->ts.cl->length
2909 && sym->ts.cl->length->expr_type != EXPR_CONSTANT)
2910 generate_expr_decls (sym, sym->ts.cl->length);
2912 if (sym->as && sym->as->rank)
2914 for (i = 0; i < sym->as->rank; i++)
2916 generate_expr_decls (sym, sym->as->lower[i]);
2917 generate_expr_decls (sym, sym->as->upper[i]);
2923 /* Generate decls for all local variables. We do this to ensure correct
2924 handling of expressions which only appear in the specification of
2928 generate_local_decl (gfc_symbol * sym)
2930 if (sym->attr.flavor == FL_VARIABLE)
2932 /* Check for dependencies in the array specification and string
2933 length, adding the necessary declarations to the function. We
2934 mark the symbol now, as well as in traverse_ns, to prevent
2935 getting stuck in a circular dependency. */
2937 if (!sym->attr.dummy && !sym->ns->proc_name->attr.entry_master)
2938 generate_dependency_declarations (sym);
2940 if (sym->attr.referenced)
2941 gfc_get_symbol_decl (sym);
2942 /* INTENT(out) dummy arguments are likely meant to be set. */
2943 else if (warn_unused_variable
2945 && sym->attr.intent == INTENT_OUT)
2946 gfc_warning ("Dummy argument '%s' at %L was declared INTENT(OUT) but was not set",
2947 sym->name, &sym->declared_at);
2948 /* Specific warning for unused dummy arguments. */
2949 else if (warn_unused_variable && sym->attr.dummy)
2950 gfc_warning ("Unused dummy argument '%s' at %L", sym->name,
2952 /* Warn for unused variables, but not if they're inside a common
2953 block or are use-associated. */
2954 else if (warn_unused_variable
2955 && !(sym->attr.in_common || sym->attr.use_assoc || sym->mark))
2956 gfc_warning ("Unused variable '%s' declared at %L", sym->name,
2958 /* For variable length CHARACTER parameters, the PARM_DECL already
2959 references the length variable, so force gfc_get_symbol_decl
2960 even when not referenced. If optimize > 0, it will be optimized
2961 away anyway. But do this only after emitting -Wunused-parameter
2962 warning if requested. */
2963 if (sym->attr.dummy && ! sym->attr.referenced
2964 && sym->ts.type == BT_CHARACTER
2965 && sym->ts.cl->backend_decl != NULL
2966 && TREE_CODE (sym->ts.cl->backend_decl) == VAR_DECL)
2968 sym->attr.referenced = 1;
2969 gfc_get_symbol_decl (sym);
2972 /* We do not want the middle-end to warn about unused parameters
2973 as this was already done above. */
2974 if (sym->attr.dummy && sym->backend_decl != NULL_TREE)
2975 TREE_NO_WARNING(sym->backend_decl) = 1;
2977 else if (sym->attr.flavor == FL_PARAMETER)
2979 if (warn_unused_parameter
2980 && !sym->attr.referenced
2981 && !sym->attr.use_assoc)
2982 gfc_warning ("Unused parameter '%s' declared at %L", sym->name,
2985 else if (sym->attr.flavor == FL_PROCEDURE)
2987 /* TODO: move to the appropriate place in resolve.c. */
2988 if (warn_return_type
2989 && sym->attr.function
2991 && sym != sym->result
2992 && !sym->result->attr.referenced
2993 && !sym->attr.use_assoc
2994 && sym->attr.if_source != IFSRC_IFBODY)
2996 gfc_warning ("Return value '%s' of function '%s' declared at "
2997 "%L not set", sym->result->name, sym->name,
2998 &sym->result->declared_at);
3000 /* Prevents "Unused variable" warning for RESULT variables. */
3001 sym->mark = sym->result->mark = 1;
3005 if (sym->attr.dummy == 1)
3007 /* Modify the tree type for scalar character dummy arguments of bind(c)
3008 procedures if they are passed by value. The tree type for them will
3009 be promoted to INTEGER_TYPE for the middle end, which appears to be
3010 what C would do with characters passed by-value. The value attribute
3011 implies the dummy is a scalar. */
3012 if (sym->attr.value == 1 && sym->backend_decl != NULL
3013 && sym->ts.type == BT_CHARACTER && sym->ts.is_c_interop
3014 && sym->ns->proc_name != NULL && sym->ns->proc_name->attr.is_bind_c)
3015 gfc_conv_scalar_char_value (sym, NULL, NULL);
3018 /* Make sure we convert the types of the derived types from iso_c_binding
3020 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
3021 && sym->ts.type == BT_DERIVED)
3022 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
3026 generate_local_vars (gfc_namespace * ns)
3028 gfc_traverse_ns (ns, generate_local_decl);
3032 /* Generate a switch statement to jump to the correct entry point. Also
3033 creates the label decls for the entry points. */
3036 gfc_trans_entry_master_switch (gfc_entry_list * el)
3043 gfc_init_block (&block);
3044 for (; el; el = el->next)
3046 /* Add the case label. */
3047 label = gfc_build_label_decl (NULL_TREE);
3048 val = build_int_cst (gfc_array_index_type, el->id);
3049 tmp = build3_v (CASE_LABEL_EXPR, val, NULL_TREE, label);
3050 gfc_add_expr_to_block (&block, tmp);
3052 /* And jump to the actual entry point. */
3053 label = gfc_build_label_decl (NULL_TREE);
3054 tmp = build1_v (GOTO_EXPR, label);
3055 gfc_add_expr_to_block (&block, tmp);
3057 /* Save the label decl. */
3060 tmp = gfc_finish_block (&block);
3061 /* The first argument selects the entry point. */
3062 val = DECL_ARGUMENTS (current_function_decl);
3063 tmp = build3_v (SWITCH_EXPR, val, tmp, NULL_TREE);
3068 /* Generate code for a function. */
3071 gfc_generate_function_code (gfc_namespace * ns)
3084 sym = ns->proc_name;
3086 /* Check that the frontend isn't still using this. */
3087 gcc_assert (sym->tlink == NULL);
3090 /* Create the declaration for functions with global scope. */
3091 if (!sym->backend_decl)
3092 gfc_create_function_decl (ns);
3094 fndecl = sym->backend_decl;
3095 old_context = current_function_decl;
3099 push_function_context ();
3100 saved_parent_function_decls = saved_function_decls;
3101 saved_function_decls = NULL_TREE;
3104 trans_function_start (sym);
3106 gfc_start_block (&block);
3108 if (ns->entries && ns->proc_name->ts.type == BT_CHARACTER)
3110 /* Copy length backend_decls to all entry point result
3115 gfc_conv_const_charlen (ns->proc_name->ts.cl);
3116 backend_decl = ns->proc_name->result->ts.cl->backend_decl;
3117 for (el = ns->entries; el; el = el->next)
3118 el->sym->result->ts.cl->backend_decl = backend_decl;
3121 /* Translate COMMON blocks. */
3122 gfc_trans_common (ns);
3124 /* Null the parent fake result declaration if this namespace is
3125 a module function or an external procedures. */
3126 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
3127 || ns->parent == NULL)
3128 parent_fake_result_decl = NULL_TREE;
3130 gfc_generate_contained_functions (ns);
3132 generate_local_vars (ns);
3134 /* Keep the parent fake result declaration in module functions
3135 or external procedures. */
3136 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
3137 || ns->parent == NULL)
3138 current_fake_result_decl = parent_fake_result_decl;
3140 current_fake_result_decl = NULL_TREE;
3142 current_function_return_label = NULL;
3144 /* Now generate the code for the body of this function. */
3145 gfc_init_block (&body);
3147 /* If this is the main program, add a call to set_options to set up the
3148 runtime library Fortran language standard parameters. */
3149 if (sym->attr.is_main_program)
3151 tree array_type, array, var;
3153 /* Passing a new option to the library requires four modifications:
3154 + add it to the tree_cons list below
3155 + change the array size in the call to build_array_type
3156 + change the first argument to the library call
3157 gfor_fndecl_set_options
3158 + modify the library (runtime/compile_options.c)! */
3159 array = tree_cons (NULL_TREE,
3160 build_int_cst (integer_type_node,
3161 gfc_option.warn_std), NULL_TREE);
3162 array = tree_cons (NULL_TREE,
3163 build_int_cst (integer_type_node,
3164 gfc_option.allow_std), array);
3165 array = tree_cons (NULL_TREE,
3166 build_int_cst (integer_type_node, pedantic), array);
3167 array = tree_cons (NULL_TREE,
3168 build_int_cst (integer_type_node,
3169 gfc_option.flag_dump_core), array);
3170 array = tree_cons (NULL_TREE,
3171 build_int_cst (integer_type_node,
3172 gfc_option.flag_backtrace), array);
3173 array = tree_cons (NULL_TREE,
3174 build_int_cst (integer_type_node,
3175 gfc_option.flag_sign_zero), array);
3177 array = tree_cons (NULL_TREE,
3178 build_int_cst (integer_type_node,
3179 flag_bounds_check), array);
3181 array_type = build_array_type (integer_type_node,
3182 build_index_type (build_int_cst (NULL_TREE,
3184 array = build_constructor_from_list (array_type, nreverse (array));
3185 TREE_CONSTANT (array) = 1;
3186 TREE_STATIC (array) = 1;
3188 /* Create a static variable to hold the jump table. */
3189 var = gfc_create_var (array_type, "options");
3190 TREE_CONSTANT (var) = 1;
3191 TREE_STATIC (var) = 1;
3192 TREE_READONLY (var) = 1;
3193 DECL_INITIAL (var) = array;
3194 var = gfc_build_addr_expr (pvoid_type_node, var);
3196 tmp = build_call_expr (gfor_fndecl_set_options, 2,
3197 build_int_cst (integer_type_node, 7), var);
3198 gfc_add_expr_to_block (&body, tmp);
3201 /* If this is the main program and a -ffpe-trap option was provided,
3202 add a call to set_fpe so that the library will raise a FPE when
3204 if (sym->attr.is_main_program && gfc_option.fpe != 0)
3206 tmp = build_call_expr (gfor_fndecl_set_fpe, 1,
3207 build_int_cst (integer_type_node,
3209 gfc_add_expr_to_block (&body, tmp);
3212 /* If this is the main program and an -fconvert option was provided,
3213 add a call to set_convert. */
3215 if (sym->attr.is_main_program && gfc_option.convert != GFC_CONVERT_NATIVE)
3217 tmp = build_call_expr (gfor_fndecl_set_convert, 1,
3218 build_int_cst (integer_type_node,
3219 gfc_option.convert));
3220 gfc_add_expr_to_block (&body, tmp);
3223 /* If this is the main program and an -frecord-marker option was provided,
3224 add a call to set_record_marker. */
3226 if (sym->attr.is_main_program && gfc_option.record_marker != 0)
3228 tmp = build_call_expr (gfor_fndecl_set_record_marker, 1,
3229 build_int_cst (integer_type_node,
3230 gfc_option.record_marker));
3231 gfc_add_expr_to_block (&body, tmp);
3234 if (sym->attr.is_main_program && gfc_option.max_subrecord_length != 0)
3236 tmp = build_call_expr (gfor_fndecl_set_max_subrecord_length,
3238 build_int_cst (integer_type_node,
3239 gfc_option.max_subrecord_length));
3240 gfc_add_expr_to_block (&body, tmp);
3243 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
3244 && sym->attr.subroutine)
3246 tree alternate_return;
3247 alternate_return = gfc_get_fake_result_decl (sym, 0);
3248 gfc_add_modify_expr (&body, alternate_return, integer_zero_node);
3253 /* Jump to the correct entry point. */
3254 tmp = gfc_trans_entry_master_switch (ns->entries);
3255 gfc_add_expr_to_block (&body, tmp);
3258 tmp = gfc_trans_code (ns->code);
3259 gfc_add_expr_to_block (&body, tmp);
3261 /* Add a return label if needed. */
3262 if (current_function_return_label)
3264 tmp = build1_v (LABEL_EXPR, current_function_return_label);
3265 gfc_add_expr_to_block (&body, tmp);
3268 tmp = gfc_finish_block (&body);
3269 /* Add code to create and cleanup arrays. */
3270 tmp = gfc_trans_deferred_vars (sym, tmp);
3272 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node)
3274 if (sym->attr.subroutine || sym == sym->result)
3276 if (current_fake_result_decl != NULL)
3277 result = TREE_VALUE (current_fake_result_decl);
3280 current_fake_result_decl = NULL_TREE;
3283 result = sym->result->backend_decl;
3285 if (result != NULL_TREE && sym->attr.function
3286 && sym->ts.type == BT_DERIVED
3287 && sym->ts.derived->attr.alloc_comp
3288 && !sym->attr.pointer)
3290 rank = sym->as ? sym->as->rank : 0;
3291 tmp2 = gfc_nullify_alloc_comp (sym->ts.derived, result, rank);
3292 gfc_add_expr_to_block (&block, tmp2);
3295 gfc_add_expr_to_block (&block, tmp);
3297 if (result == NULL_TREE)
3299 /* TODO: move to the appropriate place in resolve.c. */
3300 if (warn_return_type && !sym->attr.referenced && sym == sym->result)
3301 gfc_warning ("Return value of function '%s' at %L not set",
3302 sym->name, &sym->declared_at);
3304 TREE_NO_WARNING(sym->backend_decl) = 1;
3308 /* Set the return value to the dummy result variable. The
3309 types may be different for scalar default REAL functions
3310 with -ff2c, therefore we have to convert. */
3311 tmp = convert (TREE_TYPE (DECL_RESULT (fndecl)), result);
3312 tmp = fold_build2 (MODIFY_EXPR, TREE_TYPE (tmp),
3313 DECL_RESULT (fndecl), tmp);
3314 tmp = build1_v (RETURN_EXPR, tmp);
3315 gfc_add_expr_to_block (&block, tmp);
3319 gfc_add_expr_to_block (&block, tmp);
3322 /* Add all the decls we created during processing. */
3323 decl = saved_function_decls;
3328 next = TREE_CHAIN (decl);
3329 TREE_CHAIN (decl) = NULL_TREE;
3333 saved_function_decls = NULL_TREE;
3335 DECL_SAVED_TREE (fndecl) = gfc_finish_block (&block);
3337 /* Finish off this function and send it for code generation. */
3339 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
3341 /* Output the GENERIC tree. */
3342 dump_function (TDI_original, fndecl);
3344 /* Store the end of the function, so that we get good line number
3345 info for the epilogue. */
3346 cfun->function_end_locus = input_location;
3348 /* We're leaving the context of this function, so zap cfun.
3349 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
3350 tree_rest_of_compilation. */
3355 pop_function_context ();
3356 saved_function_decls = saved_parent_function_decls;
3358 current_function_decl = old_context;
3360 if (decl_function_context (fndecl))
3361 /* Register this function with cgraph just far enough to get it
3362 added to our parent's nested function list. */
3363 (void) cgraph_node (fndecl);
3366 gfc_gimplify_function (fndecl);
3367 cgraph_finalize_function (fndecl, false);
3372 gfc_generate_constructors (void)
3374 gcc_assert (gfc_static_ctors == NULL_TREE);
3382 if (gfc_static_ctors == NULL_TREE)
3385 fnname = get_file_function_name ("I");
3386 type = build_function_type (void_type_node,
3387 gfc_chainon_list (NULL_TREE, void_type_node));
3389 fndecl = build_decl (FUNCTION_DECL, fnname, type);
3390 TREE_PUBLIC (fndecl) = 1;
3392 decl = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
3393 DECL_ARTIFICIAL (decl) = 1;
3394 DECL_IGNORED_P (decl) = 1;
3395 DECL_CONTEXT (decl) = fndecl;
3396 DECL_RESULT (fndecl) = decl;
3400 current_function_decl = fndecl;
3402 rest_of_decl_compilation (fndecl, 1, 0);
3404 make_decl_rtl (fndecl);
3406 init_function_start (fndecl);
3410 for (; gfc_static_ctors; gfc_static_ctors = TREE_CHAIN (gfc_static_ctors))
3412 tmp = build_call_expr (TREE_VALUE (gfc_static_ctors), 0);
3413 DECL_SAVED_TREE (fndecl) = build_stmt (EXPR_STMT, tmp);
3418 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
3420 free_after_parsing (cfun);
3421 free_after_compilation (cfun);
3423 tree_rest_of_compilation (fndecl);
3425 current_function_decl = NULL_TREE;
3429 /* Translates a BLOCK DATA program unit. This means emitting the
3430 commons contained therein plus their initializations. We also emit
3431 a globally visible symbol to make sure that each BLOCK DATA program
3432 unit remains unique. */
3435 gfc_generate_block_data (gfc_namespace * ns)
3440 /* Tell the backend the source location of the block data. */
3442 gfc_set_backend_locus (&ns->proc_name->declared_at);
3444 gfc_set_backend_locus (&gfc_current_locus);
3446 /* Process the DATA statements. */
3447 gfc_trans_common (ns);
3449 /* Create a global symbol with the mane of the block data. This is to
3450 generate linker errors if the same name is used twice. It is never
3453 id = gfc_sym_mangled_function_id (ns->proc_name);
3455 id = get_identifier ("__BLOCK_DATA__");
3457 decl = build_decl (VAR_DECL, id, gfc_array_index_type);
3458 TREE_PUBLIC (decl) = 1;
3459 TREE_STATIC (decl) = 1;
3462 rest_of_decl_compilation (decl, 1, 0);
3466 #include "gt-fortran-trans-decl.h"