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_sc_kind;
129 tree gfor_fndecl_si_kind;
130 tree gfor_fndecl_sr_kind;
132 /* BLAS gemm functions. */
133 tree gfor_fndecl_sgemm;
134 tree gfor_fndecl_dgemm;
135 tree gfor_fndecl_cgemm;
136 tree gfor_fndecl_zgemm;
140 gfc_add_decl_to_parent_function (tree decl)
143 DECL_CONTEXT (decl) = DECL_CONTEXT (current_function_decl);
144 DECL_NONLOCAL (decl) = 1;
145 TREE_CHAIN (decl) = saved_parent_function_decls;
146 saved_parent_function_decls = decl;
150 gfc_add_decl_to_function (tree decl)
153 TREE_USED (decl) = 1;
154 DECL_CONTEXT (decl) = current_function_decl;
155 TREE_CHAIN (decl) = saved_function_decls;
156 saved_function_decls = decl;
160 /* Build a backend label declaration. Set TREE_USED for named labels.
161 The context of the label is always the current_function_decl. All
162 labels are marked artificial. */
165 gfc_build_label_decl (tree label_id)
167 /* 2^32 temporaries should be enough. */
168 static unsigned int tmp_num = 1;
172 if (label_id == NULL_TREE)
174 /* Build an internal label name. */
175 ASM_FORMAT_PRIVATE_NAME (label_name, "L", tmp_num++);
176 label_id = get_identifier (label_name);
181 /* Build the LABEL_DECL node. Labels have no type. */
182 label_decl = build_decl (LABEL_DECL, label_id, void_type_node);
183 DECL_CONTEXT (label_decl) = current_function_decl;
184 DECL_MODE (label_decl) = VOIDmode;
186 /* We always define the label as used, even if the original source
187 file never references the label. We don't want all kinds of
188 spurious warnings for old-style Fortran code with too many
190 TREE_USED (label_decl) = 1;
192 DECL_ARTIFICIAL (label_decl) = 1;
197 /* Returns the return label for the current function. */
200 gfc_get_return_label (void)
202 char name[GFC_MAX_SYMBOL_LEN + 10];
204 if (current_function_return_label)
205 return current_function_return_label;
207 sprintf (name, "__return_%s",
208 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
210 current_function_return_label =
211 gfc_build_label_decl (get_identifier (name));
213 DECL_ARTIFICIAL (current_function_return_label) = 1;
215 return current_function_return_label;
219 /* Set the backend source location of a decl. */
222 gfc_set_decl_location (tree decl, locus * loc)
224 DECL_SOURCE_LOCATION (decl) = loc->lb->location;
228 /* Return the backend label declaration for a given label structure,
229 or create it if it doesn't exist yet. */
232 gfc_get_label_decl (gfc_st_label * lp)
234 if (lp->backend_decl)
235 return lp->backend_decl;
238 char label_name[GFC_MAX_SYMBOL_LEN + 1];
241 /* Validate the label declaration from the front end. */
242 gcc_assert (lp != NULL && lp->value <= MAX_LABEL_VALUE);
244 /* Build a mangled name for the label. */
245 sprintf (label_name, "__label_%.6d", lp->value);
247 /* Build the LABEL_DECL node. */
248 label_decl = gfc_build_label_decl (get_identifier (label_name));
250 /* Tell the debugger where the label came from. */
251 if (lp->value <= MAX_LABEL_VALUE) /* An internal label. */
252 gfc_set_decl_location (label_decl, &lp->where);
254 DECL_ARTIFICIAL (label_decl) = 1;
256 /* Store the label in the label list and return the LABEL_DECL. */
257 lp->backend_decl = label_decl;
263 /* Convert a gfc_symbol to an identifier of the same name. */
266 gfc_sym_identifier (gfc_symbol * sym)
268 return (get_identifier (sym->name));
272 /* Construct mangled name from symbol name. */
275 gfc_sym_mangled_identifier (gfc_symbol * sym)
277 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
279 /* Prevent the mangling of identifiers that have an assigned
280 binding label (mainly those that are bind(c)). */
281 if (sym->attr.is_bind_c == 1
282 && sym->binding_label[0] != '\0')
283 return get_identifier(sym->binding_label);
285 if (sym->module == NULL)
286 return gfc_sym_identifier (sym);
289 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
290 return get_identifier (name);
295 /* Construct mangled function name from symbol name. */
298 gfc_sym_mangled_function_id (gfc_symbol * sym)
301 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
303 /* It may be possible to simply use the binding label if it's
304 provided, and remove the other checks. Then we could use it
305 for other things if we wished. */
306 if ((sym->attr.is_bind_c == 1 || sym->attr.is_iso_c == 1) &&
307 sym->binding_label[0] != '\0')
308 /* use the binding label rather than the mangled name */
309 return get_identifier (sym->binding_label);
311 if (sym->module == NULL || sym->attr.proc == PROC_EXTERNAL
312 || (sym->module != NULL && (sym->attr.external
313 || sym->attr.if_source == IFSRC_IFBODY)))
315 /* Main program is mangled into MAIN__. */
316 if (sym->attr.is_main_program)
317 return get_identifier ("MAIN__");
319 /* Intrinsic procedures are never mangled. */
320 if (sym->attr.proc == PROC_INTRINSIC)
321 return get_identifier (sym->name);
323 if (gfc_option.flag_underscoring)
325 has_underscore = strchr (sym->name, '_') != 0;
326 if (gfc_option.flag_second_underscore && has_underscore)
327 snprintf (name, sizeof name, "%s__", sym->name);
329 snprintf (name, sizeof name, "%s_", sym->name);
330 return get_identifier (name);
333 return get_identifier (sym->name);
337 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
338 return get_identifier (name);
343 /* Returns true if a variable of specified size should go on the stack. */
346 gfc_can_put_var_on_stack (tree size)
348 unsigned HOST_WIDE_INT low;
350 if (!INTEGER_CST_P (size))
353 if (gfc_option.flag_max_stack_var_size < 0)
356 if (TREE_INT_CST_HIGH (size) != 0)
359 low = TREE_INT_CST_LOW (size);
360 if (low > (unsigned HOST_WIDE_INT) gfc_option.flag_max_stack_var_size)
363 /* TODO: Set a per-function stack size limit. */
369 /* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
370 an expression involving its corresponding pointer. There are
371 2 cases; one for variable size arrays, and one for everything else,
372 because variable-sized arrays require one fewer level of
376 gfc_finish_cray_pointee (tree decl, gfc_symbol *sym)
378 tree ptr_decl = gfc_get_symbol_decl (sym->cp_pointer);
381 /* Parameters need to be dereferenced. */
382 if (sym->cp_pointer->attr.dummy)
383 ptr_decl = build_fold_indirect_ref (ptr_decl);
385 /* Check to see if we're dealing with a variable-sized array. */
386 if (sym->attr.dimension
387 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
389 /* These decls will be dereferenced later, so we don't dereference
391 value = convert (TREE_TYPE (decl), ptr_decl);
395 ptr_decl = convert (build_pointer_type (TREE_TYPE (decl)),
397 value = build_fold_indirect_ref (ptr_decl);
400 SET_DECL_VALUE_EXPR (decl, value);
401 DECL_HAS_VALUE_EXPR_P (decl) = 1;
402 GFC_DECL_CRAY_POINTEE (decl) = 1;
403 /* This is a fake variable just for debugging purposes. */
404 TREE_ASM_WRITTEN (decl) = 1;
408 /* Finish processing of a declaration without an initial value. */
411 gfc_finish_decl (tree decl)
413 gcc_assert (TREE_CODE (decl) == PARM_DECL
414 || DECL_INITIAL (decl) == NULL_TREE);
416 if (TREE_CODE (decl) != VAR_DECL)
419 if (DECL_SIZE (decl) == NULL_TREE
420 && TYPE_SIZE (TREE_TYPE (decl)) != NULL_TREE)
421 layout_decl (decl, 0);
423 /* A few consistency checks. */
424 /* A static variable with an incomplete type is an error if it is
425 initialized. Also if it is not file scope. Otherwise, let it
426 through, but if it is not `extern' then it may cause an error
428 /* An automatic variable with an incomplete type is an error. */
430 /* We should know the storage size. */
431 gcc_assert (DECL_SIZE (decl) != NULL_TREE
432 || (TREE_STATIC (decl)
433 ? (!DECL_INITIAL (decl) || !DECL_CONTEXT (decl))
434 : DECL_EXTERNAL (decl)));
436 /* The storage size should be constant. */
437 gcc_assert ((!DECL_EXTERNAL (decl) && !TREE_STATIC (decl))
439 || TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST);
443 /* Apply symbol attributes to a variable, and add it to the function scope. */
446 gfc_finish_var_decl (tree decl, gfc_symbol * sym)
449 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
450 This is the equivalent of the TARGET variables.
451 We also need to set this if the variable is passed by reference in a
454 /* Set DECL_VALUE_EXPR for Cray Pointees. */
455 if (sym->attr.cray_pointee)
456 gfc_finish_cray_pointee (decl, sym);
458 if (sym->attr.target)
459 TREE_ADDRESSABLE (decl) = 1;
460 /* If it wasn't used we wouldn't be getting it. */
461 TREE_USED (decl) = 1;
463 /* Chain this decl to the pending declarations. Don't do pushdecl()
464 because this would add them to the current scope rather than the
466 if (current_function_decl != NULL_TREE)
468 if (sym->ns->proc_name->backend_decl == current_function_decl
469 || sym->result == sym)
470 gfc_add_decl_to_function (decl);
472 gfc_add_decl_to_parent_function (decl);
475 if (sym->attr.cray_pointee)
478 if(sym->attr.is_bind_c == 1)
480 /* We need to put variables that are bind(c) into the common
481 segment of the object file, because this is what C would do.
482 gfortran would typically put them in either the BSS or
483 initialized data segments, and only mark them as common if
484 they were part of common blocks. However, if they are not put
485 into common space, then C cannot initialize global fortran
486 variables that it interoperates with and the draft says that
487 either Fortran or C should be able to initialize it (but not
488 both, of course.) (J3/04-007, section 15.3). */
489 TREE_PUBLIC(decl) = 1;
490 DECL_COMMON(decl) = 1;
493 /* If a variable is USE associated, it's always external. */
494 if (sym->attr.use_assoc)
496 DECL_EXTERNAL (decl) = 1;
497 TREE_PUBLIC (decl) = 1;
499 else if (sym->module && !sym->attr.result && !sym->attr.dummy)
501 /* TODO: Don't set sym->module for result or dummy variables. */
502 gcc_assert (current_function_decl == NULL_TREE || sym->result == sym);
503 /* This is the declaration of a module variable. */
504 TREE_PUBLIC (decl) = 1;
505 TREE_STATIC (decl) = 1;
508 /* Derived types are a bit peculiar because of the possibility of
509 a default initializer; this must be applied each time the variable
510 comes into scope it therefore need not be static. These variables
511 are SAVE_NONE but have an initializer. Otherwise explicitly
512 intitialized variables are SAVE_IMPLICIT and explicitly saved are
514 if (!sym->attr.use_assoc
515 && (sym->attr.save != SAVE_NONE || sym->attr.data
516 || (sym->value && sym->ns->proc_name->attr.is_main_program)))
517 TREE_STATIC (decl) = 1;
519 if (sym->attr.volatile_)
521 TREE_THIS_VOLATILE (decl) = 1;
522 new = build_qualified_type (TREE_TYPE (decl), TYPE_QUAL_VOLATILE);
523 TREE_TYPE (decl) = new;
526 /* Keep variables larger than max-stack-var-size off stack. */
527 if (!sym->ns->proc_name->attr.recursive
528 && INTEGER_CST_P (DECL_SIZE_UNIT (decl))
529 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
530 /* Put variable length auto array pointers always into stack. */
531 && (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
532 || sym->attr.dimension == 0
533 || sym->as->type != AS_EXPLICIT
535 || sym->attr.allocatable)
536 && !DECL_ARTIFICIAL (decl))
537 TREE_STATIC (decl) = 1;
539 /* Handle threadprivate variables. */
540 if (sym->attr.threadprivate
541 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
542 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
546 /* Allocate the lang-specific part of a decl. */
549 gfc_allocate_lang_decl (tree decl)
551 DECL_LANG_SPECIFIC (decl) = (struct lang_decl *)
552 ggc_alloc_cleared (sizeof (struct lang_decl));
555 /* Remember a symbol to generate initialization/cleanup code at function
559 gfc_defer_symbol_init (gfc_symbol * sym)
565 /* Don't add a symbol twice. */
569 last = head = sym->ns->proc_name;
572 /* Make sure that setup code for dummy variables which are used in the
573 setup of other variables is generated first. */
576 /* Find the first dummy arg seen after us, or the first non-dummy arg.
577 This is a circular list, so don't go past the head. */
579 && (!p->attr.dummy || p->dummy_order > sym->dummy_order))
585 /* Insert in between last and p. */
591 /* Create an array index type variable with function scope. */
594 create_index_var (const char * pfx, int nest)
598 decl = gfc_create_var_np (gfc_array_index_type, pfx);
600 gfc_add_decl_to_parent_function (decl);
602 gfc_add_decl_to_function (decl);
607 /* Create variables to hold all the non-constant bits of info for a
608 descriptorless array. Remember these in the lang-specific part of the
612 gfc_build_qualified_array (tree decl, gfc_symbol * sym)
618 type = TREE_TYPE (decl);
620 /* We just use the descriptor, if there is one. */
621 if (GFC_DESCRIPTOR_TYPE_P (type))
624 gcc_assert (GFC_ARRAY_TYPE_P (type));
625 nest = (sym->ns->proc_name->backend_decl != current_function_decl)
626 && !sym->attr.contained;
628 for (dim = 0; dim < GFC_TYPE_ARRAY_RANK (type); dim++)
630 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
632 GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
633 TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type, dim)) = 1;
635 /* Don't try to use the unknown bound for assumed shape arrays. */
636 if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
637 && (sym->as->type != AS_ASSUMED_SIZE
638 || dim < GFC_TYPE_ARRAY_RANK (type) - 1))
640 GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
641 TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type, dim)) = 1;
644 if (GFC_TYPE_ARRAY_STRIDE (type, dim) == NULL_TREE)
646 GFC_TYPE_ARRAY_STRIDE (type, dim) = create_index_var ("stride", nest);
647 TREE_NO_WARNING (GFC_TYPE_ARRAY_STRIDE (type, dim)) = 1;
650 if (GFC_TYPE_ARRAY_OFFSET (type) == NULL_TREE)
652 GFC_TYPE_ARRAY_OFFSET (type) = gfc_create_var_np (gfc_array_index_type,
654 TREE_NO_WARNING (GFC_TYPE_ARRAY_OFFSET (type)) = 1;
657 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type));
659 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type));
662 if (GFC_TYPE_ARRAY_SIZE (type) == NULL_TREE
663 && sym->as->type != AS_ASSUMED_SIZE)
665 GFC_TYPE_ARRAY_SIZE (type) = create_index_var ("size", nest);
666 TREE_NO_WARNING (GFC_TYPE_ARRAY_SIZE (type)) = 1;
669 if (POINTER_TYPE_P (type))
671 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type)));
672 gcc_assert (TYPE_LANG_SPECIFIC (type)
673 == TYPE_LANG_SPECIFIC (TREE_TYPE (type)));
674 type = TREE_TYPE (type);
677 if (! COMPLETE_TYPE_P (type) && GFC_TYPE_ARRAY_SIZE (type))
681 size = fold_build2 (MINUS_EXPR, gfc_array_index_type,
682 GFC_TYPE_ARRAY_SIZE (type), gfc_index_one_node);
683 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
685 TYPE_DOMAIN (type) = range;
691 /* For some dummy arguments we don't use the actual argument directly.
692 Instead we create a local decl and use that. This allows us to perform
693 initialization, and construct full type information. */
696 gfc_build_dummy_array_decl (gfc_symbol * sym, tree dummy)
706 if (sym->attr.pointer || sym->attr.allocatable)
709 /* Add to list of variables if not a fake result variable. */
710 if (sym->attr.result || sym->attr.dummy)
711 gfc_defer_symbol_init (sym);
713 type = TREE_TYPE (dummy);
714 gcc_assert (TREE_CODE (dummy) == PARM_DECL
715 && POINTER_TYPE_P (type));
717 /* Do we know the element size? */
718 known_size = sym->ts.type != BT_CHARACTER
719 || INTEGER_CST_P (sym->ts.cl->backend_decl);
721 if (known_size && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type)))
723 /* For descriptorless arrays with known element size the actual
724 argument is sufficient. */
725 gcc_assert (GFC_ARRAY_TYPE_P (type));
726 gfc_build_qualified_array (dummy, sym);
730 type = TREE_TYPE (type);
731 if (GFC_DESCRIPTOR_TYPE_P (type))
733 /* Create a descriptorless array pointer. */
736 if (!gfc_option.flag_repack_arrays)
738 if (as->type == AS_ASSUMED_SIZE)
739 packed = PACKED_FULL;
743 if (as->type == AS_EXPLICIT)
745 packed = PACKED_FULL;
746 for (n = 0; n < as->rank; n++)
750 && as->upper[n]->expr_type == EXPR_CONSTANT
751 && as->lower[n]->expr_type == EXPR_CONSTANT))
752 packed = PACKED_PARTIAL;
756 packed = PACKED_PARTIAL;
759 type = gfc_typenode_for_spec (&sym->ts);
760 type = gfc_get_nodesc_array_type (type, sym->as, packed);
764 /* We now have an expression for the element size, so create a fully
765 qualified type. Reset sym->backend decl or this will just return the
767 DECL_ARTIFICIAL (sym->backend_decl) = 1;
768 sym->backend_decl = NULL_TREE;
769 type = gfc_sym_type (sym);
770 packed = PACKED_FULL;
773 ASM_FORMAT_PRIVATE_NAME (name, IDENTIFIER_POINTER (DECL_NAME (dummy)), 0);
774 decl = build_decl (VAR_DECL, get_identifier (name), type);
776 DECL_ARTIFICIAL (decl) = 1;
777 TREE_PUBLIC (decl) = 0;
778 TREE_STATIC (decl) = 0;
779 DECL_EXTERNAL (decl) = 0;
781 /* We should never get deferred shape arrays here. We used to because of
783 gcc_assert (sym->as->type != AS_DEFERRED);
785 if (packed == PACKED_PARTIAL)
786 GFC_DECL_PARTIAL_PACKED_ARRAY (decl) = 1;
787 else if (packed == PACKED_FULL)
788 GFC_DECL_PACKED_ARRAY (decl) = 1;
790 gfc_build_qualified_array (decl, sym);
792 if (DECL_LANG_SPECIFIC (dummy))
793 DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (dummy);
795 gfc_allocate_lang_decl (decl);
797 GFC_DECL_SAVED_DESCRIPTOR (decl) = dummy;
799 if (sym->ns->proc_name->backend_decl == current_function_decl
800 || sym->attr.contained)
801 gfc_add_decl_to_function (decl);
803 gfc_add_decl_to_parent_function (decl);
809 /* Return a constant or a variable to use as a string length. Does not
810 add the decl to the current scope. */
813 gfc_create_string_length (gfc_symbol * sym)
817 gcc_assert (sym->ts.cl);
818 gfc_conv_const_charlen (sym->ts.cl);
820 if (sym->ts.cl->backend_decl == NULL_TREE)
822 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
824 /* Also prefix the mangled name. */
825 strcpy (&name[1], sym->name);
827 length = build_decl (VAR_DECL, get_identifier (name),
828 gfc_charlen_type_node);
829 DECL_ARTIFICIAL (length) = 1;
830 TREE_USED (length) = 1;
831 if (sym->ns->proc_name->tlink != NULL)
832 gfc_defer_symbol_init (sym);
833 sym->ts.cl->backend_decl = length;
836 return sym->ts.cl->backend_decl;
839 /* If a variable is assigned a label, we add another two auxiliary
843 gfc_add_assign_aux_vars (gfc_symbol * sym)
849 gcc_assert (sym->backend_decl);
851 decl = sym->backend_decl;
852 gfc_allocate_lang_decl (decl);
853 GFC_DECL_ASSIGN (decl) = 1;
854 length = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
855 gfc_charlen_type_node);
856 addr = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
858 gfc_finish_var_decl (length, sym);
859 gfc_finish_var_decl (addr, sym);
860 /* STRING_LENGTH is also used as flag. Less than -1 means that
861 ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the
862 target label's address. Otherwise, value is the length of a format string
863 and ASSIGN_ADDR is its address. */
864 if (TREE_STATIC (length))
865 DECL_INITIAL (length) = build_int_cst (NULL_TREE, -2);
867 gfc_defer_symbol_init (sym);
869 GFC_DECL_STRING_LEN (decl) = length;
870 GFC_DECL_ASSIGN_ADDR (decl) = addr;
873 /* Return the decl for a gfc_symbol, create it if it doesn't already
877 gfc_get_symbol_decl (gfc_symbol * sym)
880 tree length = NULL_TREE;
883 gcc_assert (sym->attr.referenced
884 || sym->attr.use_assoc
885 || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY);
887 if (sym->ns && sym->ns->proc_name->attr.function)
888 byref = gfc_return_by_reference (sym->ns->proc_name);
892 if ((sym->attr.dummy && ! sym->attr.function) || (sym->attr.result && byref))
894 /* Return via extra parameter. */
895 if (sym->attr.result && byref
896 && !sym->backend_decl)
899 DECL_ARGUMENTS (sym->ns->proc_name->backend_decl);
900 /* For entry master function skip over the __entry
902 if (sym->ns->proc_name->attr.entry_master)
903 sym->backend_decl = TREE_CHAIN (sym->backend_decl);
906 /* Dummy variables should already have been created. */
907 gcc_assert (sym->backend_decl);
909 /* Create a character length variable. */
910 if (sym->ts.type == BT_CHARACTER)
912 if (sym->ts.cl->backend_decl == NULL_TREE)
913 length = gfc_create_string_length (sym);
915 length = sym->ts.cl->backend_decl;
916 if (TREE_CODE (length) == VAR_DECL
917 && DECL_CONTEXT (length) == NULL_TREE)
919 /* Add the string length to the same context as the symbol. */
920 if (DECL_CONTEXT (sym->backend_decl) == current_function_decl)
921 gfc_add_decl_to_function (length);
923 gfc_add_decl_to_parent_function (length);
925 gcc_assert (DECL_CONTEXT (sym->backend_decl) ==
926 DECL_CONTEXT (length));
928 gfc_defer_symbol_init (sym);
932 /* Use a copy of the descriptor for dummy arrays. */
933 if (sym->attr.dimension && !TREE_USED (sym->backend_decl))
935 decl = gfc_build_dummy_array_decl (sym, sym->backend_decl);
936 /* Prevent the dummy from being detected as unused if it is copied. */
937 if (sym->backend_decl != NULL && decl != sym->backend_decl)
938 DECL_ARTIFICIAL (sym->backend_decl) = 1;
939 sym->backend_decl = decl;
942 TREE_USED (sym->backend_decl) = 1;
943 if (sym->attr.assign && GFC_DECL_ASSIGN (sym->backend_decl) == 0)
945 gfc_add_assign_aux_vars (sym);
947 return sym->backend_decl;
950 if (sym->backend_decl)
951 return sym->backend_decl;
953 /* Catch function declarations. Only used for actual parameters. */
954 if (sym->attr.flavor == FL_PROCEDURE)
956 decl = gfc_get_extern_function_decl (sym);
960 if (sym->attr.intrinsic)
961 internal_error ("intrinsic variable which isn't a procedure");
963 /* Create string length decl first so that they can be used in the
965 if (sym->ts.type == BT_CHARACTER)
966 length = gfc_create_string_length (sym);
968 /* Create the decl for the variable. */
969 decl = build_decl (VAR_DECL, gfc_sym_identifier (sym), gfc_sym_type (sym));
971 gfc_set_decl_location (decl, &sym->declared_at);
973 /* Symbols from modules should have their assembler names mangled.
974 This is done here rather than in gfc_finish_var_decl because it
975 is different for string length variables. */
977 SET_DECL_ASSEMBLER_NAME (decl, gfc_sym_mangled_identifier (sym));
979 if (sym->attr.dimension)
981 /* Create variables to hold the non-constant bits of array info. */
982 gfc_build_qualified_array (decl, sym);
984 /* Remember this variable for allocation/cleanup. */
985 gfc_defer_symbol_init (sym);
987 if ((sym->attr.allocatable || !sym->attr.dummy) && !sym->attr.pointer)
988 GFC_DECL_PACKED_ARRAY (decl) = 1;
991 if (sym->ts.type == BT_DERIVED && sym->ts.derived->attr.alloc_comp)
992 gfc_defer_symbol_init (sym);
993 /* This applies a derived type default initializer. */
994 else if (sym->ts.type == BT_DERIVED
995 && sym->attr.save == SAVE_NONE
997 && !sym->attr.allocatable
998 && (sym->value && !sym->ns->proc_name->attr.is_main_program)
999 && !sym->attr.use_assoc)
1000 gfc_defer_symbol_init (sym);
1002 gfc_finish_var_decl (decl, sym);
1004 if (sym->ts.type == BT_CHARACTER)
1006 /* Character variables need special handling. */
1007 gfc_allocate_lang_decl (decl);
1009 if (TREE_CODE (length) != INTEGER_CST)
1011 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
1015 /* Also prefix the mangled name for symbols from modules. */
1016 strcpy (&name[1], sym->name);
1019 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (length)));
1020 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (name));
1022 gfc_finish_var_decl (length, sym);
1023 gcc_assert (!sym->value);
1026 else if (sym->attr.subref_array_pointer)
1028 /* We need the span for these beasts. */
1029 gfc_allocate_lang_decl (decl);
1032 if (sym->attr.subref_array_pointer)
1035 GFC_DECL_SUBREF_ARRAY_P (decl) = 1;
1036 span = build_decl (VAR_DECL, create_tmp_var_name ("span"),
1037 gfc_array_index_type);
1038 gfc_finish_var_decl (span, sym);
1039 TREE_STATIC (span) = 1;
1040 DECL_INITIAL (span) = build_int_cst (NULL_TREE, 0);
1042 GFC_DECL_SPAN (decl) = span;
1045 sym->backend_decl = decl;
1047 if (sym->attr.assign)
1048 gfc_add_assign_aux_vars (sym);
1050 if (TREE_STATIC (decl) && !sym->attr.use_assoc)
1052 /* Add static initializer. */
1053 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
1054 TREE_TYPE (decl), sym->attr.dimension,
1055 sym->attr.pointer || sym->attr.allocatable);
1062 /* Substitute a temporary variable in place of the real one. */
1065 gfc_shadow_sym (gfc_symbol * sym, tree decl, gfc_saved_var * save)
1067 save->attr = sym->attr;
1068 save->decl = sym->backend_decl;
1070 gfc_clear_attr (&sym->attr);
1071 sym->attr.referenced = 1;
1072 sym->attr.flavor = FL_VARIABLE;
1074 sym->backend_decl = decl;
1078 /* Restore the original variable. */
1081 gfc_restore_sym (gfc_symbol * sym, gfc_saved_var * save)
1083 sym->attr = save->attr;
1084 sym->backend_decl = save->decl;
1088 /* Get a basic decl for an external function. */
1091 gfc_get_extern_function_decl (gfc_symbol * sym)
1096 gfc_intrinsic_sym *isym;
1098 char s[GFC_MAX_SYMBOL_LEN + 23]; /* "_gfortran_f2c_specific" and '\0'. */
1102 if (sym->backend_decl)
1103 return sym->backend_decl;
1105 /* We should never be creating external decls for alternate entry points.
1106 The procedure may be an alternate entry point, but we don't want/need
1108 gcc_assert (!(sym->attr.entry || sym->attr.entry_master));
1110 if (sym->attr.intrinsic)
1112 /* Call the resolution function to get the actual name. This is
1113 a nasty hack which relies on the resolution functions only looking
1114 at the first argument. We pass NULL for the second argument
1115 otherwise things like AINT get confused. */
1116 isym = gfc_find_function (sym->name);
1117 gcc_assert (isym->resolve.f0 != NULL);
1119 memset (&e, 0, sizeof (e));
1120 e.expr_type = EXPR_FUNCTION;
1122 memset (&argexpr, 0, sizeof (argexpr));
1123 gcc_assert (isym->formal);
1124 argexpr.ts = isym->formal->ts;
1126 if (isym->formal->next == NULL)
1127 isym->resolve.f1 (&e, &argexpr);
1130 if (isym->formal->next->next == NULL)
1131 isym->resolve.f2 (&e, &argexpr, NULL);
1134 if (isym->formal->next->next->next == NULL)
1135 isym->resolve.f3 (&e, &argexpr, NULL, NULL);
1138 /* All specific intrinsics take less than 5 arguments. */
1139 gcc_assert (isym->formal->next->next->next->next == NULL);
1140 isym->resolve.f4 (&e, &argexpr, NULL, NULL, NULL);
1145 if (gfc_option.flag_f2c
1146 && ((e.ts.type == BT_REAL && e.ts.kind == gfc_default_real_kind)
1147 || e.ts.type == BT_COMPLEX))
1149 /* Specific which needs a different implementation if f2c
1150 calling conventions are used. */
1151 sprintf (s, "_gfortran_f2c_specific%s", e.value.function.name);
1154 sprintf (s, "_gfortran_specific%s", e.value.function.name);
1156 name = get_identifier (s);
1157 mangled_name = name;
1161 name = gfc_sym_identifier (sym);
1162 mangled_name = gfc_sym_mangled_function_id (sym);
1165 type = gfc_get_function_type (sym);
1166 fndecl = build_decl (FUNCTION_DECL, name, type);
1168 SET_DECL_ASSEMBLER_NAME (fndecl, mangled_name);
1169 /* If the return type is a pointer, avoid alias issues by setting
1170 DECL_IS_MALLOC to nonzero. This means that the function should be
1171 treated as if it were a malloc, meaning it returns a pointer that
1173 if (POINTER_TYPE_P (type))
1174 DECL_IS_MALLOC (fndecl) = 1;
1176 /* Set the context of this decl. */
1177 if (0 && sym->ns && sym->ns->proc_name)
1179 /* TODO: Add external decls to the appropriate scope. */
1180 DECL_CONTEXT (fndecl) = sym->ns->proc_name->backend_decl;
1184 /* Global declaration, e.g. intrinsic subroutine. */
1185 DECL_CONTEXT (fndecl) = NULL_TREE;
1188 DECL_EXTERNAL (fndecl) = 1;
1190 /* This specifies if a function is globally addressable, i.e. it is
1191 the opposite of declaring static in C. */
1192 TREE_PUBLIC (fndecl) = 1;
1194 /* Set attributes for PURE functions. A call to PURE function in the
1195 Fortran 95 sense is both pure and without side effects in the C
1197 if (sym->attr.pure || sym->attr.elemental)
1199 if (sym->attr.function && !gfc_return_by_reference (sym))
1200 DECL_IS_PURE (fndecl) = 1;
1201 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
1202 parameters and don't use alternate returns (is this
1203 allowed?). In that case, calls to them are meaningless, and
1204 can be optimized away. See also in build_function_decl(). */
1205 TREE_SIDE_EFFECTS (fndecl) = 0;
1208 /* Mark non-returning functions. */
1209 if (sym->attr.noreturn)
1210 TREE_THIS_VOLATILE(fndecl) = 1;
1212 sym->backend_decl = fndecl;
1214 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1215 pushdecl_top_level (fndecl);
1221 /* Create a declaration for a procedure. For external functions (in the C
1222 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
1223 a master function with alternate entry points. */
1226 build_function_decl (gfc_symbol * sym)
1229 symbol_attribute attr;
1231 gfc_formal_arglist *f;
1233 gcc_assert (!sym->backend_decl);
1234 gcc_assert (!sym->attr.external);
1236 /* Set the line and filename. sym->declared_at seems to point to the
1237 last statement for subroutines, but it'll do for now. */
1238 gfc_set_backend_locus (&sym->declared_at);
1240 /* Allow only one nesting level. Allow public declarations. */
1241 gcc_assert (current_function_decl == NULL_TREE
1242 || DECL_CONTEXT (current_function_decl) == NULL_TREE);
1244 type = gfc_get_function_type (sym);
1245 fndecl = build_decl (FUNCTION_DECL, gfc_sym_identifier (sym), type);
1247 /* Perform name mangling if this is a top level or module procedure. */
1248 if (current_function_decl == NULL_TREE)
1249 SET_DECL_ASSEMBLER_NAME (fndecl, gfc_sym_mangled_function_id (sym));
1251 /* Figure out the return type of the declared function, and build a
1252 RESULT_DECL for it. If this is a subroutine with alternate
1253 returns, build a RESULT_DECL for it. */
1256 result_decl = NULL_TREE;
1257 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
1260 if (gfc_return_by_reference (sym))
1261 type = void_type_node;
1264 if (sym->result != sym)
1265 result_decl = gfc_sym_identifier (sym->result);
1267 type = TREE_TYPE (TREE_TYPE (fndecl));
1272 /* Look for alternate return placeholders. */
1273 int has_alternate_returns = 0;
1274 for (f = sym->formal; f; f = f->next)
1278 has_alternate_returns = 1;
1283 if (has_alternate_returns)
1284 type = integer_type_node;
1286 type = void_type_node;
1289 result_decl = build_decl (RESULT_DECL, result_decl, type);
1290 DECL_ARTIFICIAL (result_decl) = 1;
1291 DECL_IGNORED_P (result_decl) = 1;
1292 DECL_CONTEXT (result_decl) = fndecl;
1293 DECL_RESULT (fndecl) = result_decl;
1295 /* Don't call layout_decl for a RESULT_DECL.
1296 layout_decl (result_decl, 0); */
1298 /* If the return type is a pointer, avoid alias issues by setting
1299 DECL_IS_MALLOC to nonzero. This means that the function should be
1300 treated as if it were a malloc, meaning it returns a pointer that
1302 if (POINTER_TYPE_P (type))
1303 DECL_IS_MALLOC (fndecl) = 1;
1305 /* Set up all attributes for the function. */
1306 DECL_CONTEXT (fndecl) = current_function_decl;
1307 DECL_EXTERNAL (fndecl) = 0;
1309 /* This specifies if a function is globally visible, i.e. it is
1310 the opposite of declaring static in C. */
1311 if (DECL_CONTEXT (fndecl) == NULL_TREE
1312 && !sym->attr.entry_master)
1313 TREE_PUBLIC (fndecl) = 1;
1315 /* TREE_STATIC means the function body is defined here. */
1316 TREE_STATIC (fndecl) = 1;
1318 /* Set attributes for PURE functions. A call to a PURE function in the
1319 Fortran 95 sense is both pure and without side effects in the C
1321 if (attr.pure || attr.elemental)
1323 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
1324 including an alternate return. In that case it can also be
1325 marked as PURE. See also in gfc_get_extern_function_decl(). */
1326 if (attr.function && !gfc_return_by_reference (sym))
1327 DECL_IS_PURE (fndecl) = 1;
1328 TREE_SIDE_EFFECTS (fndecl) = 0;
1331 /* For -fwhole-program to work well, the main program needs to have the
1332 "externally_visible" attribute. */
1333 if (attr.is_main_program)
1334 DECL_ATTRIBUTES (fndecl)
1335 = tree_cons (get_identifier("externally_visible"), NULL_TREE, NULL_TREE);
1337 /* Layout the function declaration and put it in the binding level
1338 of the current function. */
1341 sym->backend_decl = fndecl;
1345 /* Create the DECL_ARGUMENTS for a procedure. */
1348 create_function_arglist (gfc_symbol * sym)
1351 gfc_formal_arglist *f;
1352 tree typelist, hidden_typelist;
1353 tree arglist, hidden_arglist;
1357 fndecl = sym->backend_decl;
1359 /* Build formal argument list. Make sure that their TREE_CONTEXT is
1360 the new FUNCTION_DECL node. */
1361 arglist = NULL_TREE;
1362 hidden_arglist = NULL_TREE;
1363 typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1365 if (sym->attr.entry_master)
1367 type = TREE_VALUE (typelist);
1368 parm = build_decl (PARM_DECL, get_identifier ("__entry"), type);
1370 DECL_CONTEXT (parm) = fndecl;
1371 DECL_ARG_TYPE (parm) = type;
1372 TREE_READONLY (parm) = 1;
1373 gfc_finish_decl (parm);
1374 DECL_ARTIFICIAL (parm) = 1;
1376 arglist = chainon (arglist, parm);
1377 typelist = TREE_CHAIN (typelist);
1380 if (gfc_return_by_reference (sym))
1382 tree type = TREE_VALUE (typelist), length = NULL;
1384 if (sym->ts.type == BT_CHARACTER)
1386 /* Length of character result. */
1387 tree len_type = TREE_VALUE (TREE_CHAIN (typelist));
1388 gcc_assert (len_type == gfc_charlen_type_node);
1390 length = build_decl (PARM_DECL,
1391 get_identifier (".__result"),
1393 if (!sym->ts.cl->length)
1395 sym->ts.cl->backend_decl = length;
1396 TREE_USED (length) = 1;
1398 gcc_assert (TREE_CODE (length) == PARM_DECL);
1399 DECL_CONTEXT (length) = fndecl;
1400 DECL_ARG_TYPE (length) = len_type;
1401 TREE_READONLY (length) = 1;
1402 DECL_ARTIFICIAL (length) = 1;
1403 gfc_finish_decl (length);
1404 if (sym->ts.cl->backend_decl == NULL
1405 || sym->ts.cl->backend_decl == length)
1410 if (sym->ts.cl->backend_decl == NULL)
1412 tree len = build_decl (VAR_DECL,
1413 get_identifier ("..__result"),
1414 gfc_charlen_type_node);
1415 DECL_ARTIFICIAL (len) = 1;
1416 TREE_USED (len) = 1;
1417 sym->ts.cl->backend_decl = len;
1420 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1421 arg = sym->result ? sym->result : sym;
1422 backend_decl = arg->backend_decl;
1423 /* Temporary clear it, so that gfc_sym_type creates complete
1425 arg->backend_decl = NULL;
1426 type = gfc_sym_type (arg);
1427 arg->backend_decl = backend_decl;
1428 type = build_reference_type (type);
1432 parm = build_decl (PARM_DECL, get_identifier ("__result"), type);
1434 DECL_CONTEXT (parm) = fndecl;
1435 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
1436 TREE_READONLY (parm) = 1;
1437 DECL_ARTIFICIAL (parm) = 1;
1438 gfc_finish_decl (parm);
1440 arglist = chainon (arglist, parm);
1441 typelist = TREE_CHAIN (typelist);
1443 if (sym->ts.type == BT_CHARACTER)
1445 gfc_allocate_lang_decl (parm);
1446 arglist = chainon (arglist, length);
1447 typelist = TREE_CHAIN (typelist);
1451 hidden_typelist = typelist;
1452 for (f = sym->formal; f; f = f->next)
1453 if (f->sym != NULL) /* Ignore alternate returns. */
1454 hidden_typelist = TREE_CHAIN (hidden_typelist);
1456 for (f = sym->formal; f; f = f->next)
1458 char name[GFC_MAX_SYMBOL_LEN + 2];
1460 /* Ignore alternate returns. */
1464 type = TREE_VALUE (typelist);
1466 if (f->sym->ts.type == BT_CHARACTER)
1468 tree len_type = TREE_VALUE (hidden_typelist);
1469 tree length = NULL_TREE;
1470 gcc_assert (len_type == gfc_charlen_type_node);
1472 strcpy (&name[1], f->sym->name);
1474 length = build_decl (PARM_DECL, get_identifier (name), len_type);
1476 hidden_arglist = chainon (hidden_arglist, length);
1477 DECL_CONTEXT (length) = fndecl;
1478 DECL_ARTIFICIAL (length) = 1;
1479 DECL_ARG_TYPE (length) = len_type;
1480 TREE_READONLY (length) = 1;
1481 gfc_finish_decl (length);
1483 /* TODO: Check string lengths when -fbounds-check. */
1485 /* Use the passed value for assumed length variables. */
1486 if (!f->sym->ts.cl->length)
1488 TREE_USED (length) = 1;
1489 gcc_assert (!f->sym->ts.cl->backend_decl);
1490 f->sym->ts.cl->backend_decl = length;
1493 hidden_typelist = TREE_CHAIN (hidden_typelist);
1495 if (f->sym->ts.cl->backend_decl == NULL
1496 || f->sym->ts.cl->backend_decl == length)
1498 if (f->sym->ts.cl->backend_decl == NULL)
1499 gfc_create_string_length (f->sym);
1501 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1502 if (f->sym->attr.flavor == FL_PROCEDURE)
1503 type = build_pointer_type (gfc_get_function_type (f->sym));
1505 type = gfc_sym_type (f->sym);
1509 /* For non-constant length array arguments, make sure they use
1510 a different type node from TYPE_ARG_TYPES type. */
1511 if (f->sym->attr.dimension
1512 && type == TREE_VALUE (typelist)
1513 && TREE_CODE (type) == POINTER_TYPE
1514 && GFC_ARRAY_TYPE_P (type)
1515 && f->sym->as->type != AS_ASSUMED_SIZE
1516 && ! COMPLETE_TYPE_P (TREE_TYPE (type)))
1518 if (f->sym->attr.flavor == FL_PROCEDURE)
1519 type = build_pointer_type (gfc_get_function_type (f->sym));
1521 type = gfc_sym_type (f->sym);
1524 /* Build a the argument declaration. */
1525 parm = build_decl (PARM_DECL, gfc_sym_identifier (f->sym), type);
1527 /* Fill in arg stuff. */
1528 DECL_CONTEXT (parm) = fndecl;
1529 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
1530 /* All implementation args are read-only. */
1531 TREE_READONLY (parm) = 1;
1533 gfc_finish_decl (parm);
1535 f->sym->backend_decl = parm;
1537 arglist = chainon (arglist, parm);
1538 typelist = TREE_CHAIN (typelist);
1541 /* Add the hidden string length parameters, unless the procedure
1543 if (!sym->attr.is_bind_c)
1544 arglist = chainon (arglist, hidden_arglist);
1546 gcc_assert (hidden_typelist == NULL_TREE
1547 || TREE_VALUE (hidden_typelist) == void_type_node);
1548 DECL_ARGUMENTS (fndecl) = arglist;
1551 /* Convert FNDECL's code to GIMPLE and handle any nested functions. */
1554 gfc_gimplify_function (tree fndecl)
1556 struct cgraph_node *cgn;
1558 gimplify_function_tree (fndecl);
1559 dump_function (TDI_generic, fndecl);
1561 /* Generate errors for structured block violations. */
1562 /* ??? Could be done as part of resolve_labels. */
1564 diagnose_omp_structured_block_errors (fndecl);
1566 /* Convert all nested functions to GIMPLE now. We do things in this order
1567 so that items like VLA sizes are expanded properly in the context of the
1568 correct function. */
1569 cgn = cgraph_node (fndecl);
1570 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1571 gfc_gimplify_function (cgn->decl);
1575 /* Do the setup necessary before generating the body of a function. */
1578 trans_function_start (gfc_symbol * sym)
1582 fndecl = sym->backend_decl;
1584 /* Let GCC know the current scope is this function. */
1585 current_function_decl = fndecl;
1587 /* Let the world know what we're about to do. */
1588 announce_function (fndecl);
1590 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1592 /* Create RTL for function declaration. */
1593 rest_of_decl_compilation (fndecl, 1, 0);
1596 /* Create RTL for function definition. */
1597 make_decl_rtl (fndecl);
1599 init_function_start (fndecl);
1601 /* Even though we're inside a function body, we still don't want to
1602 call expand_expr to calculate the size of a variable-sized array.
1603 We haven't necessarily assigned RTL to all variables yet, so it's
1604 not safe to try to expand expressions involving them. */
1605 cfun->dont_save_pending_sizes_p = 1;
1607 /* function.c requires a push at the start of the function. */
1611 /* Create thunks for alternate entry points. */
1614 build_entry_thunks (gfc_namespace * ns)
1616 gfc_formal_arglist *formal;
1617 gfc_formal_arglist *thunk_formal;
1619 gfc_symbol *thunk_sym;
1627 /* This should always be a toplevel function. */
1628 gcc_assert (current_function_decl == NULL_TREE);
1630 gfc_get_backend_locus (&old_loc);
1631 for (el = ns->entries; el; el = el->next)
1633 thunk_sym = el->sym;
1635 build_function_decl (thunk_sym);
1636 create_function_arglist (thunk_sym);
1638 trans_function_start (thunk_sym);
1640 thunk_fndecl = thunk_sym->backend_decl;
1642 gfc_start_block (&body);
1644 /* Pass extra parameter identifying this entry point. */
1645 tmp = build_int_cst (gfc_array_index_type, el->id);
1646 args = tree_cons (NULL_TREE, tmp, NULL_TREE);
1647 string_args = NULL_TREE;
1649 if (thunk_sym->attr.function)
1651 if (gfc_return_by_reference (ns->proc_name))
1653 tree ref = DECL_ARGUMENTS (current_function_decl);
1654 args = tree_cons (NULL_TREE, ref, args);
1655 if (ns->proc_name->ts.type == BT_CHARACTER)
1656 args = tree_cons (NULL_TREE, TREE_CHAIN (ref),
1661 for (formal = ns->proc_name->formal; formal; formal = formal->next)
1663 /* Ignore alternate returns. */
1664 if (formal->sym == NULL)
1667 /* We don't have a clever way of identifying arguments, so resort to
1668 a brute-force search. */
1669 for (thunk_formal = thunk_sym->formal;
1671 thunk_formal = thunk_formal->next)
1673 if (thunk_formal->sym == formal->sym)
1679 /* Pass the argument. */
1680 DECL_ARTIFICIAL (thunk_formal->sym->backend_decl) = 1;
1681 args = tree_cons (NULL_TREE, thunk_formal->sym->backend_decl,
1683 if (formal->sym->ts.type == BT_CHARACTER)
1685 tmp = thunk_formal->sym->ts.cl->backend_decl;
1686 string_args = tree_cons (NULL_TREE, tmp, string_args);
1691 /* Pass NULL for a missing argument. */
1692 args = tree_cons (NULL_TREE, null_pointer_node, args);
1693 if (formal->sym->ts.type == BT_CHARACTER)
1695 tmp = build_int_cst (gfc_charlen_type_node, 0);
1696 string_args = tree_cons (NULL_TREE, tmp, string_args);
1701 /* Call the master function. */
1702 args = nreverse (args);
1703 args = chainon (args, nreverse (string_args));
1704 tmp = ns->proc_name->backend_decl;
1705 tmp = build_function_call_expr (tmp, args);
1706 if (ns->proc_name->attr.mixed_entry_master)
1708 tree union_decl, field;
1709 tree master_type = TREE_TYPE (ns->proc_name->backend_decl);
1711 union_decl = build_decl (VAR_DECL, get_identifier ("__result"),
1712 TREE_TYPE (master_type));
1713 DECL_ARTIFICIAL (union_decl) = 1;
1714 DECL_EXTERNAL (union_decl) = 0;
1715 TREE_PUBLIC (union_decl) = 0;
1716 TREE_USED (union_decl) = 1;
1717 layout_decl (union_decl, 0);
1718 pushdecl (union_decl);
1720 DECL_CONTEXT (union_decl) = current_function_decl;
1721 tmp = fold_build2 (MODIFY_EXPR, TREE_TYPE (union_decl),
1723 gfc_add_expr_to_block (&body, tmp);
1725 for (field = TYPE_FIELDS (TREE_TYPE (union_decl));
1726 field; field = TREE_CHAIN (field))
1727 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
1728 thunk_sym->result->name) == 0)
1730 gcc_assert (field != NULL_TREE);
1731 tmp = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
1732 union_decl, field, NULL_TREE);
1733 tmp = fold_build2 (MODIFY_EXPR,
1734 TREE_TYPE (DECL_RESULT (current_function_decl)),
1735 DECL_RESULT (current_function_decl), tmp);
1736 tmp = build1_v (RETURN_EXPR, tmp);
1738 else if (TREE_TYPE (DECL_RESULT (current_function_decl))
1741 tmp = fold_build2 (MODIFY_EXPR,
1742 TREE_TYPE (DECL_RESULT (current_function_decl)),
1743 DECL_RESULT (current_function_decl), tmp);
1744 tmp = build1_v (RETURN_EXPR, tmp);
1746 gfc_add_expr_to_block (&body, tmp);
1748 /* Finish off this function and send it for code generation. */
1749 DECL_SAVED_TREE (thunk_fndecl) = gfc_finish_block (&body);
1751 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl)) = thunk_fndecl;
1753 /* Output the GENERIC tree. */
1754 dump_function (TDI_original, thunk_fndecl);
1756 /* Store the end of the function, so that we get good line number
1757 info for the epilogue. */
1758 cfun->function_end_locus = input_location;
1760 /* We're leaving the context of this function, so zap cfun.
1761 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
1762 tree_rest_of_compilation. */
1765 current_function_decl = NULL_TREE;
1767 gfc_gimplify_function (thunk_fndecl);
1768 cgraph_finalize_function (thunk_fndecl, false);
1770 /* We share the symbols in the formal argument list with other entry
1771 points and the master function. Clear them so that they are
1772 recreated for each function. */
1773 for (formal = thunk_sym->formal; formal; formal = formal->next)
1774 if (formal->sym != NULL) /* Ignore alternate returns. */
1776 formal->sym->backend_decl = NULL_TREE;
1777 if (formal->sym->ts.type == BT_CHARACTER)
1778 formal->sym->ts.cl->backend_decl = NULL_TREE;
1781 if (thunk_sym->attr.function)
1783 if (thunk_sym->ts.type == BT_CHARACTER)
1784 thunk_sym->ts.cl->backend_decl = NULL_TREE;
1785 if (thunk_sym->result->ts.type == BT_CHARACTER)
1786 thunk_sym->result->ts.cl->backend_decl = NULL_TREE;
1790 gfc_set_backend_locus (&old_loc);
1794 /* Create a decl for a function, and create any thunks for alternate entry
1798 gfc_create_function_decl (gfc_namespace * ns)
1800 /* Create a declaration for the master function. */
1801 build_function_decl (ns->proc_name);
1803 /* Compile the entry thunks. */
1805 build_entry_thunks (ns);
1807 /* Now create the read argument list. */
1808 create_function_arglist (ns->proc_name);
1811 /* Return the decl used to hold the function return value. If
1812 parent_flag is set, the context is the parent_scope. */
1815 gfc_get_fake_result_decl (gfc_symbol * sym, int parent_flag)
1819 tree this_fake_result_decl;
1820 tree this_function_decl;
1822 char name[GFC_MAX_SYMBOL_LEN + 10];
1826 this_fake_result_decl = parent_fake_result_decl;
1827 this_function_decl = DECL_CONTEXT (current_function_decl);
1831 this_fake_result_decl = current_fake_result_decl;
1832 this_function_decl = current_function_decl;
1836 && sym->ns->proc_name->backend_decl == this_function_decl
1837 && sym->ns->proc_name->attr.entry_master
1838 && sym != sym->ns->proc_name)
1841 if (this_fake_result_decl != NULL)
1842 for (t = TREE_CHAIN (this_fake_result_decl); t; t = TREE_CHAIN (t))
1843 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t)), sym->name) == 0)
1846 return TREE_VALUE (t);
1847 decl = gfc_get_fake_result_decl (sym->ns->proc_name, parent_flag);
1850 this_fake_result_decl = parent_fake_result_decl;
1852 this_fake_result_decl = current_fake_result_decl;
1854 if (decl && sym->ns->proc_name->attr.mixed_entry_master)
1858 for (field = TYPE_FIELDS (TREE_TYPE (decl));
1859 field; field = TREE_CHAIN (field))
1860 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
1864 gcc_assert (field != NULL_TREE);
1865 decl = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
1866 decl, field, NULL_TREE);
1869 var = create_tmp_var_raw (TREE_TYPE (decl), sym->name);
1871 gfc_add_decl_to_parent_function (var);
1873 gfc_add_decl_to_function (var);
1875 SET_DECL_VALUE_EXPR (var, decl);
1876 DECL_HAS_VALUE_EXPR_P (var) = 1;
1877 GFC_DECL_RESULT (var) = 1;
1879 TREE_CHAIN (this_fake_result_decl)
1880 = tree_cons (get_identifier (sym->name), var,
1881 TREE_CHAIN (this_fake_result_decl));
1885 if (this_fake_result_decl != NULL_TREE)
1886 return TREE_VALUE (this_fake_result_decl);
1888 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
1893 if (sym->ts.type == BT_CHARACTER)
1895 if (sym->ts.cl->backend_decl == NULL_TREE)
1896 length = gfc_create_string_length (sym);
1898 length = sym->ts.cl->backend_decl;
1899 if (TREE_CODE (length) == VAR_DECL
1900 && DECL_CONTEXT (length) == NULL_TREE)
1901 gfc_add_decl_to_function (length);
1904 if (gfc_return_by_reference (sym))
1906 decl = DECL_ARGUMENTS (this_function_decl);
1908 if (sym->ns->proc_name->backend_decl == this_function_decl
1909 && sym->ns->proc_name->attr.entry_master)
1910 decl = TREE_CHAIN (decl);
1912 TREE_USED (decl) = 1;
1914 decl = gfc_build_dummy_array_decl (sym, decl);
1918 sprintf (name, "__result_%.20s",
1919 IDENTIFIER_POINTER (DECL_NAME (this_function_decl)));
1921 if (!sym->attr.mixed_entry_master && sym->attr.function)
1922 decl = build_decl (VAR_DECL, get_identifier (name),
1923 gfc_sym_type (sym));
1925 decl = build_decl (VAR_DECL, get_identifier (name),
1926 TREE_TYPE (TREE_TYPE (this_function_decl)));
1927 DECL_ARTIFICIAL (decl) = 1;
1928 DECL_EXTERNAL (decl) = 0;
1929 TREE_PUBLIC (decl) = 0;
1930 TREE_USED (decl) = 1;
1931 GFC_DECL_RESULT (decl) = 1;
1932 TREE_ADDRESSABLE (decl) = 1;
1934 layout_decl (decl, 0);
1937 gfc_add_decl_to_parent_function (decl);
1939 gfc_add_decl_to_function (decl);
1943 parent_fake_result_decl = build_tree_list (NULL, decl);
1945 current_fake_result_decl = build_tree_list (NULL, decl);
1951 /* Builds a function decl. The remaining parameters are the types of the
1952 function arguments. Negative nargs indicates a varargs function. */
1955 gfc_build_library_function_decl (tree name, tree rettype, int nargs, ...)
1964 /* Library functions must be declared with global scope. */
1965 gcc_assert (current_function_decl == NULL_TREE);
1967 va_start (p, nargs);
1970 /* Create a list of the argument types. */
1971 for (arglist = NULL_TREE, n = abs (nargs); n > 0; n--)
1973 argtype = va_arg (p, tree);
1974 arglist = gfc_chainon_list (arglist, argtype);
1979 /* Terminate the list. */
1980 arglist = gfc_chainon_list (arglist, void_type_node);
1983 /* Build the function type and decl. */
1984 fntype = build_function_type (rettype, arglist);
1985 fndecl = build_decl (FUNCTION_DECL, name, fntype);
1987 /* Mark this decl as external. */
1988 DECL_EXTERNAL (fndecl) = 1;
1989 TREE_PUBLIC (fndecl) = 1;
1995 rest_of_decl_compilation (fndecl, 1, 0);
2001 gfc_build_intrinsic_function_decls (void)
2003 tree gfc_int4_type_node = gfc_get_int_type (4);
2004 tree gfc_int8_type_node = gfc_get_int_type (8);
2005 tree gfc_int16_type_node = gfc_get_int_type (16);
2006 tree gfc_logical4_type_node = gfc_get_logical_type (4);
2008 /* String functions. */
2009 gfor_fndecl_compare_string =
2010 gfc_build_library_function_decl (get_identifier (PREFIX("compare_string")),
2011 integer_type_node, 4,
2012 gfc_charlen_type_node, pchar_type_node,
2013 gfc_charlen_type_node, pchar_type_node);
2015 gfor_fndecl_concat_string =
2016 gfc_build_library_function_decl (get_identifier (PREFIX("concat_string")),
2019 gfc_charlen_type_node, pchar_type_node,
2020 gfc_charlen_type_node, pchar_type_node,
2021 gfc_charlen_type_node, pchar_type_node);
2023 gfor_fndecl_string_len_trim =
2024 gfc_build_library_function_decl (get_identifier (PREFIX("string_len_trim")),
2026 2, gfc_charlen_type_node,
2029 gfor_fndecl_string_index =
2030 gfc_build_library_function_decl (get_identifier (PREFIX("string_index")),
2032 5, gfc_charlen_type_node, pchar_type_node,
2033 gfc_charlen_type_node, pchar_type_node,
2034 gfc_logical4_type_node);
2036 gfor_fndecl_string_scan =
2037 gfc_build_library_function_decl (get_identifier (PREFIX("string_scan")),
2039 5, gfc_charlen_type_node, pchar_type_node,
2040 gfc_charlen_type_node, pchar_type_node,
2041 gfc_logical4_type_node);
2043 gfor_fndecl_string_verify =
2044 gfc_build_library_function_decl (get_identifier (PREFIX("string_verify")),
2046 5, gfc_charlen_type_node, pchar_type_node,
2047 gfc_charlen_type_node, pchar_type_node,
2048 gfc_logical4_type_node);
2050 gfor_fndecl_string_trim =
2051 gfc_build_library_function_decl (get_identifier (PREFIX("string_trim")),
2054 build_pointer_type (gfc_charlen_type_node),
2056 gfc_charlen_type_node,
2059 gfor_fndecl_string_minmax =
2060 gfc_build_library_function_decl (get_identifier (PREFIX("string_minmax")),
2062 build_pointer_type (gfc_charlen_type_node),
2063 ppvoid_type_node, integer_type_node,
2066 gfor_fndecl_ttynam =
2067 gfc_build_library_function_decl (get_identifier (PREFIX("ttynam")),
2071 gfc_charlen_type_node,
2075 gfc_build_library_function_decl (get_identifier (PREFIX("fdate")),
2079 gfc_charlen_type_node);
2082 gfc_build_library_function_decl (get_identifier (PREFIX("ctime")),
2086 gfc_charlen_type_node,
2087 gfc_int8_type_node);
2089 gfor_fndecl_adjustl =
2090 gfc_build_library_function_decl (get_identifier (PREFIX("adjustl")),
2094 gfc_charlen_type_node, pchar_type_node);
2096 gfor_fndecl_adjustr =
2097 gfc_build_library_function_decl (get_identifier (PREFIX("adjustr")),
2101 gfc_charlen_type_node, pchar_type_node);
2103 gfor_fndecl_sc_kind =
2104 gfc_build_library_function_decl (get_identifier
2105 (PREFIX("selected_char_kind")),
2106 gfc_int4_type_node, 2,
2107 gfc_charlen_type_node, pchar_type_node);
2109 gfor_fndecl_si_kind =
2110 gfc_build_library_function_decl (get_identifier
2111 (PREFIX("selected_int_kind")),
2112 gfc_int4_type_node, 1, pvoid_type_node);
2114 gfor_fndecl_sr_kind =
2115 gfc_build_library_function_decl (get_identifier
2116 (PREFIX("selected_real_kind")),
2117 gfc_int4_type_node, 2,
2118 pvoid_type_node, pvoid_type_node);
2120 /* Power functions. */
2122 tree ctype, rtype, itype, jtype;
2123 int rkind, ikind, jkind;
2126 static int ikinds[NIKINDS] = {4, 8, 16};
2127 static int rkinds[NRKINDS] = {4, 8, 10, 16};
2128 char name[PREFIX_LEN + 12]; /* _gfortran_pow_?n_?n */
2130 for (ikind=0; ikind < NIKINDS; ikind++)
2132 itype = gfc_get_int_type (ikinds[ikind]);
2134 for (jkind=0; jkind < NIKINDS; jkind++)
2136 jtype = gfc_get_int_type (ikinds[jkind]);
2139 sprintf(name, PREFIX("pow_i%d_i%d"), ikinds[ikind],
2141 gfor_fndecl_math_powi[jkind][ikind].integer =
2142 gfc_build_library_function_decl (get_identifier (name),
2143 jtype, 2, jtype, itype);
2144 TREE_READONLY (gfor_fndecl_math_powi[jkind][ikind].integer) = 1;
2148 for (rkind = 0; rkind < NRKINDS; rkind ++)
2150 rtype = gfc_get_real_type (rkinds[rkind]);
2153 sprintf(name, PREFIX("pow_r%d_i%d"), rkinds[rkind],
2155 gfor_fndecl_math_powi[rkind][ikind].real =
2156 gfc_build_library_function_decl (get_identifier (name),
2157 rtype, 2, rtype, itype);
2158 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].real) = 1;
2161 ctype = gfc_get_complex_type (rkinds[rkind]);
2164 sprintf(name, PREFIX("pow_c%d_i%d"), rkinds[rkind],
2166 gfor_fndecl_math_powi[rkind][ikind].cmplx =
2167 gfc_build_library_function_decl (get_identifier (name),
2168 ctype, 2,ctype, itype);
2169 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1;
2177 gfor_fndecl_math_ishftc4 =
2178 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc4")),
2180 3, gfc_int4_type_node,
2181 gfc_int4_type_node, gfc_int4_type_node);
2182 gfor_fndecl_math_ishftc8 =
2183 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc8")),
2185 3, gfc_int8_type_node,
2186 gfc_int4_type_node, gfc_int4_type_node);
2187 if (gfc_int16_type_node)
2188 gfor_fndecl_math_ishftc16 =
2189 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc16")),
2190 gfc_int16_type_node, 3,
2191 gfc_int16_type_node,
2193 gfc_int4_type_node);
2195 /* BLAS functions. */
2197 tree pint = build_pointer_type (integer_type_node);
2198 tree ps = build_pointer_type (gfc_get_real_type (gfc_default_real_kind));
2199 tree pd = build_pointer_type (gfc_get_real_type (gfc_default_double_kind));
2200 tree pc = build_pointer_type (gfc_get_complex_type (gfc_default_real_kind));
2201 tree pz = build_pointer_type
2202 (gfc_get_complex_type (gfc_default_double_kind));
2204 gfor_fndecl_sgemm = gfc_build_library_function_decl
2206 (gfc_option.flag_underscoring ? "sgemm_"
2208 void_type_node, 15, pchar_type_node,
2209 pchar_type_node, pint, pint, pint, ps, ps, pint,
2210 ps, pint, ps, ps, pint, integer_type_node,
2212 gfor_fndecl_dgemm = gfc_build_library_function_decl
2214 (gfc_option.flag_underscoring ? "dgemm_"
2216 void_type_node, 15, pchar_type_node,
2217 pchar_type_node, pint, pint, pint, pd, pd, pint,
2218 pd, pint, pd, pd, pint, integer_type_node,
2220 gfor_fndecl_cgemm = gfc_build_library_function_decl
2222 (gfc_option.flag_underscoring ? "cgemm_"
2224 void_type_node, 15, pchar_type_node,
2225 pchar_type_node, pint, pint, pint, pc, pc, pint,
2226 pc, pint, pc, pc, pint, integer_type_node,
2228 gfor_fndecl_zgemm = gfc_build_library_function_decl
2230 (gfc_option.flag_underscoring ? "zgemm_"
2232 void_type_node, 15, pchar_type_node,
2233 pchar_type_node, pint, pint, pint, pz, pz, pint,
2234 pz, pint, pz, pz, pint, integer_type_node,
2238 /* Other functions. */
2240 gfc_build_library_function_decl (get_identifier (PREFIX("size0")),
2241 gfc_array_index_type,
2242 1, pvoid_type_node);
2244 gfc_build_library_function_decl (get_identifier (PREFIX("size1")),
2245 gfc_array_index_type,
2247 gfc_array_index_type);
2250 gfc_build_library_function_decl (get_identifier (PREFIX ("iargc")),
2256 /* Make prototypes for runtime library functions. */
2259 gfc_build_builtin_function_decls (void)
2261 tree gfc_int4_type_node = gfc_get_int_type (4);
2263 gfor_fndecl_stop_numeric =
2264 gfc_build_library_function_decl (get_identifier (PREFIX("stop_numeric")),
2265 void_type_node, 1, gfc_int4_type_node);
2266 /* Stop doesn't return. */
2267 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric) = 1;
2269 gfor_fndecl_stop_string =
2270 gfc_build_library_function_decl (get_identifier (PREFIX("stop_string")),
2271 void_type_node, 2, pchar_type_node,
2272 gfc_int4_type_node);
2273 /* Stop doesn't return. */
2274 TREE_THIS_VOLATILE (gfor_fndecl_stop_string) = 1;
2276 gfor_fndecl_pause_numeric =
2277 gfc_build_library_function_decl (get_identifier (PREFIX("pause_numeric")),
2278 void_type_node, 1, gfc_int4_type_node);
2280 gfor_fndecl_pause_string =
2281 gfc_build_library_function_decl (get_identifier (PREFIX("pause_string")),
2282 void_type_node, 2, pchar_type_node,
2283 gfc_int4_type_node);
2285 gfor_fndecl_select_string =
2286 gfc_build_library_function_decl (get_identifier (PREFIX("select_string")),
2287 integer_type_node, 4, pvoid_type_node,
2288 integer_type_node, pchar_type_node,
2291 gfor_fndecl_runtime_error =
2292 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error")),
2293 void_type_node, -1, pchar_type_node);
2294 /* The runtime_error function does not return. */
2295 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error) = 1;
2297 gfor_fndecl_runtime_error_at =
2298 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error_at")),
2299 void_type_node, -2, pchar_type_node,
2301 /* The runtime_error_at function does not return. */
2302 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at) = 1;
2304 gfor_fndecl_generate_error =
2305 gfc_build_library_function_decl (get_identifier (PREFIX("generate_error")),
2306 void_type_node, 3, pvoid_type_node,
2307 integer_type_node, pchar_type_node);
2309 gfor_fndecl_os_error =
2310 gfc_build_library_function_decl (get_identifier (PREFIX("os_error")),
2311 void_type_node, 1, pchar_type_node);
2312 /* The runtime_error function does not return. */
2313 TREE_THIS_VOLATILE (gfor_fndecl_os_error) = 1;
2315 gfor_fndecl_set_fpe =
2316 gfc_build_library_function_decl (get_identifier (PREFIX("set_fpe")),
2317 void_type_node, 1, integer_type_node);
2319 /* Keep the array dimension in sync with the call, later in this file. */
2320 gfor_fndecl_set_options =
2321 gfc_build_library_function_decl (get_identifier (PREFIX("set_options")),
2322 void_type_node, 2, integer_type_node,
2325 gfor_fndecl_set_convert =
2326 gfc_build_library_function_decl (get_identifier (PREFIX("set_convert")),
2327 void_type_node, 1, integer_type_node);
2329 gfor_fndecl_set_record_marker =
2330 gfc_build_library_function_decl (get_identifier (PREFIX("set_record_marker")),
2331 void_type_node, 1, integer_type_node);
2333 gfor_fndecl_set_max_subrecord_length =
2334 gfc_build_library_function_decl (get_identifier (PREFIX("set_max_subrecord_length")),
2335 void_type_node, 1, integer_type_node);
2337 gfor_fndecl_in_pack = gfc_build_library_function_decl (
2338 get_identifier (PREFIX("internal_pack")),
2339 pvoid_type_node, 1, pvoid_type_node);
2341 gfor_fndecl_in_unpack = gfc_build_library_function_decl (
2342 get_identifier (PREFIX("internal_unpack")),
2343 void_type_node, 2, pvoid_type_node, pvoid_type_node);
2345 gfor_fndecl_associated =
2346 gfc_build_library_function_decl (
2347 get_identifier (PREFIX("associated")),
2348 integer_type_node, 2, ppvoid_type_node,
2351 gfc_build_intrinsic_function_decls ();
2352 gfc_build_intrinsic_lib_fndecls ();
2353 gfc_build_io_library_fndecls ();
2357 /* Evaluate the length of dummy character variables. */
2360 gfc_trans_dummy_character (gfc_symbol *sym, gfc_charlen *cl, tree fnbody)
2364 gfc_finish_decl (cl->backend_decl);
2366 gfc_start_block (&body);
2368 /* Evaluate the string length expression. */
2369 gfc_conv_string_length (cl, &body);
2371 gfc_trans_vla_type_sizes (sym, &body);
2373 gfc_add_expr_to_block (&body, fnbody);
2374 return gfc_finish_block (&body);
2378 /* Allocate and cleanup an automatic character variable. */
2381 gfc_trans_auto_character_variable (gfc_symbol * sym, tree fnbody)
2387 gcc_assert (sym->backend_decl);
2388 gcc_assert (sym->ts.cl && sym->ts.cl->length);
2390 gfc_start_block (&body);
2392 /* Evaluate the string length expression. */
2393 gfc_conv_string_length (sym->ts.cl, &body);
2395 gfc_trans_vla_type_sizes (sym, &body);
2397 decl = sym->backend_decl;
2399 /* Emit a DECL_EXPR for this variable, which will cause the
2400 gimplifier to allocate storage, and all that good stuff. */
2401 tmp = fold_build1 (DECL_EXPR, TREE_TYPE (decl), decl);
2402 gfc_add_expr_to_block (&body, tmp);
2404 gfc_add_expr_to_block (&body, fnbody);
2405 return gfc_finish_block (&body);
2408 /* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
2411 gfc_trans_assign_aux_var (gfc_symbol * sym, tree fnbody)
2415 gcc_assert (sym->backend_decl);
2416 gfc_start_block (&body);
2418 /* Set the initial value to length. See the comments in
2419 function gfc_add_assign_aux_vars in this file. */
2420 gfc_add_modify_expr (&body, GFC_DECL_STRING_LEN (sym->backend_decl),
2421 build_int_cst (NULL_TREE, -2));
2423 gfc_add_expr_to_block (&body, fnbody);
2424 return gfc_finish_block (&body);
2428 gfc_trans_vla_one_sizepos (tree *tp, stmtblock_t *body)
2430 tree t = *tp, var, val;
2432 if (t == NULL || t == error_mark_node)
2434 if (TREE_CONSTANT (t) || DECL_P (t))
2437 if (TREE_CODE (t) == SAVE_EXPR)
2439 if (SAVE_EXPR_RESOLVED_P (t))
2441 *tp = TREE_OPERAND (t, 0);
2444 val = TREE_OPERAND (t, 0);
2449 var = gfc_create_var_np (TREE_TYPE (t), NULL);
2450 gfc_add_decl_to_function (var);
2451 gfc_add_modify_expr (body, var, val);
2452 if (TREE_CODE (t) == SAVE_EXPR)
2453 TREE_OPERAND (t, 0) = var;
2458 gfc_trans_vla_type_sizes_1 (tree type, stmtblock_t *body)
2462 if (type == NULL || type == error_mark_node)
2465 type = TYPE_MAIN_VARIANT (type);
2467 if (TREE_CODE (type) == INTEGER_TYPE)
2469 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type), body);
2470 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type), body);
2472 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2474 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
2475 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
2478 else if (TREE_CODE (type) == ARRAY_TYPE)
2480 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type), body);
2481 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type), body);
2482 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type), body);
2483 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type), body);
2485 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2487 TYPE_SIZE (t) = TYPE_SIZE (type);
2488 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
2493 /* Make sure all type sizes and array domains are either constant,
2494 or variable or parameter decls. This is a simplified variant
2495 of gimplify_type_sizes, but we can't use it here, as none of the
2496 variables in the expressions have been gimplified yet.
2497 As type sizes and domains for various variable length arrays
2498 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
2499 time, without this routine gimplify_type_sizes in the middle-end
2500 could result in the type sizes being gimplified earlier than where
2501 those variables are initialized. */
2504 gfc_trans_vla_type_sizes (gfc_symbol *sym, stmtblock_t *body)
2506 tree type = TREE_TYPE (sym->backend_decl);
2508 if (TREE_CODE (type) == FUNCTION_TYPE
2509 && (sym->attr.function || sym->attr.result || sym->attr.entry))
2511 if (! current_fake_result_decl)
2514 type = TREE_TYPE (TREE_VALUE (current_fake_result_decl));
2517 while (POINTER_TYPE_P (type))
2518 type = TREE_TYPE (type);
2520 if (GFC_DESCRIPTOR_TYPE_P (type))
2522 tree etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
2524 while (POINTER_TYPE_P (etype))
2525 etype = TREE_TYPE (etype);
2527 gfc_trans_vla_type_sizes_1 (etype, body);
2530 gfc_trans_vla_type_sizes_1 (type, body);
2534 /* Initialize a derived type by building an lvalue from the symbol
2535 and using trans_assignment to do the work. */
2537 gfc_init_default_dt (gfc_symbol * sym, tree body)
2539 stmtblock_t fnblock;
2544 gfc_init_block (&fnblock);
2545 gcc_assert (!sym->attr.allocatable);
2546 gfc_set_sym_referenced (sym);
2547 e = gfc_lval_expr_from_sym (sym);
2548 tmp = gfc_trans_assignment (e, sym->value, false);
2549 if (sym->attr.dummy)
2551 present = gfc_conv_expr_present (sym);
2552 tmp = build3 (COND_EXPR, TREE_TYPE (tmp), present,
2553 tmp, build_empty_stmt ());
2555 gfc_add_expr_to_block (&fnblock, tmp);
2558 gfc_add_expr_to_block (&fnblock, body);
2559 return gfc_finish_block (&fnblock);
2563 /* Initialize INTENT(OUT) derived type dummies. */
2565 init_intent_out_dt (gfc_symbol * proc_sym, tree body)
2567 stmtblock_t fnblock;
2568 gfc_formal_arglist *f;
2570 gfc_init_block (&fnblock);
2571 for (f = proc_sym->formal; f; f = f->next)
2572 if (f->sym && f->sym->attr.intent == INTENT_OUT
2573 && f->sym->ts.type == BT_DERIVED
2574 && !f->sym->ts.derived->attr.alloc_comp
2576 body = gfc_init_default_dt (f->sym, body);
2578 gfc_add_expr_to_block (&fnblock, body);
2579 return gfc_finish_block (&fnblock);
2583 /* Generate function entry and exit code, and add it to the function body.
2585 Allocation and initialization of array variables.
2586 Allocation of character string variables.
2587 Initialization and possibly repacking of dummy arrays.
2588 Initialization of ASSIGN statement auxiliary variable. */
2591 gfc_trans_deferred_vars (gfc_symbol * proc_sym, tree fnbody)
2595 gfc_formal_arglist *f;
2597 bool seen_trans_deferred_array = false;
2599 /* Deal with implicit return variables. Explicit return variables will
2600 already have been added. */
2601 if (gfc_return_by_reference (proc_sym) && proc_sym->result == proc_sym)
2603 if (!current_fake_result_decl)
2605 gfc_entry_list *el = NULL;
2606 if (proc_sym->attr.entry_master)
2608 for (el = proc_sym->ns->entries; el; el = el->next)
2609 if (el->sym != el->sym->result)
2612 /* TODO: move to the appropriate place in resolve.c. */
2613 if (warn_return_type && el == NULL)
2614 gfc_warning ("Return value of function '%s' at %L not set",
2615 proc_sym->name, &proc_sym->declared_at);
2617 else if (proc_sym->as)
2619 tree result = TREE_VALUE (current_fake_result_decl);
2620 fnbody = gfc_trans_dummy_array_bias (proc_sym, result, fnbody);
2622 /* An automatic character length, pointer array result. */
2623 if (proc_sym->ts.type == BT_CHARACTER
2624 && TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL)
2625 fnbody = gfc_trans_dummy_character (proc_sym, proc_sym->ts.cl,
2628 else if (proc_sym->ts.type == BT_CHARACTER)
2630 if (TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL)
2631 fnbody = gfc_trans_dummy_character (proc_sym, proc_sym->ts.cl,
2635 gcc_assert (gfc_option.flag_f2c
2636 && proc_sym->ts.type == BT_COMPLEX);
2639 /* Initialize the INTENT(OUT) derived type dummy arguments. This
2640 should be done here so that the offsets and lbounds of arrays
2642 fnbody = init_intent_out_dt (proc_sym, fnbody);
2644 for (sym = proc_sym->tlink; sym != proc_sym; sym = sym->tlink)
2646 bool sym_has_alloc_comp = (sym->ts.type == BT_DERIVED)
2647 && sym->ts.derived->attr.alloc_comp;
2648 if (sym->attr.dimension)
2650 switch (sym->as->type)
2653 if (sym->attr.dummy || sym->attr.result)
2655 gfc_trans_dummy_array_bias (sym, sym->backend_decl, fnbody);
2656 else if (sym->attr.pointer || sym->attr.allocatable)
2658 if (TREE_STATIC (sym->backend_decl))
2659 gfc_trans_static_array_pointer (sym);
2662 seen_trans_deferred_array = true;
2663 fnbody = gfc_trans_deferred_array (sym, fnbody);
2668 if (sym_has_alloc_comp)
2670 seen_trans_deferred_array = true;
2671 fnbody = gfc_trans_deferred_array (sym, fnbody);
2673 else if (sym->ts.type == BT_DERIVED
2676 && sym->attr.save == SAVE_NONE)
2677 fnbody = gfc_init_default_dt (sym, fnbody);
2679 gfc_get_backend_locus (&loc);
2680 gfc_set_backend_locus (&sym->declared_at);
2681 fnbody = gfc_trans_auto_array_allocation (sym->backend_decl,
2683 gfc_set_backend_locus (&loc);
2687 case AS_ASSUMED_SIZE:
2688 /* Must be a dummy parameter. */
2689 gcc_assert (sym->attr.dummy);
2691 /* We should always pass assumed size arrays the g77 way. */
2692 fnbody = gfc_trans_g77_array (sym, fnbody);
2695 case AS_ASSUMED_SHAPE:
2696 /* Must be a dummy parameter. */
2697 gcc_assert (sym->attr.dummy);
2699 fnbody = gfc_trans_dummy_array_bias (sym, sym->backend_decl,
2704 seen_trans_deferred_array = true;
2705 fnbody = gfc_trans_deferred_array (sym, fnbody);
2711 if (sym_has_alloc_comp && !seen_trans_deferred_array)
2712 fnbody = gfc_trans_deferred_array (sym, fnbody);
2714 else if (sym_has_alloc_comp)
2715 fnbody = gfc_trans_deferred_array (sym, fnbody);
2716 else if (sym->ts.type == BT_CHARACTER)
2718 gfc_get_backend_locus (&loc);
2719 gfc_set_backend_locus (&sym->declared_at);
2720 if (sym->attr.dummy || sym->attr.result)
2721 fnbody = gfc_trans_dummy_character (sym, sym->ts.cl, fnbody);
2723 fnbody = gfc_trans_auto_character_variable (sym, fnbody);
2724 gfc_set_backend_locus (&loc);
2726 else if (sym->attr.assign)
2728 gfc_get_backend_locus (&loc);
2729 gfc_set_backend_locus (&sym->declared_at);
2730 fnbody = gfc_trans_assign_aux_var (sym, fnbody);
2731 gfc_set_backend_locus (&loc);
2733 else if (sym->ts.type == BT_DERIVED
2736 && sym->attr.save == SAVE_NONE)
2737 fnbody = gfc_init_default_dt (sym, fnbody);
2742 gfc_init_block (&body);
2744 for (f = proc_sym->formal; f; f = f->next)
2746 if (f->sym && f->sym->tlink == NULL && f->sym->ts.type == BT_CHARACTER)
2748 gcc_assert (f->sym->ts.cl->backend_decl != NULL);
2749 if (TREE_CODE (f->sym->ts.cl->backend_decl) == PARM_DECL)
2750 gfc_trans_vla_type_sizes (f->sym, &body);
2754 if (gfc_return_by_reference (proc_sym) && proc_sym->ts.type == BT_CHARACTER
2755 && current_fake_result_decl != NULL)
2757 gcc_assert (proc_sym->ts.cl->backend_decl != NULL);
2758 if (TREE_CODE (proc_sym->ts.cl->backend_decl) == PARM_DECL)
2759 gfc_trans_vla_type_sizes (proc_sym, &body);
2762 gfc_add_expr_to_block (&body, fnbody);
2763 return gfc_finish_block (&body);
2767 /* Output an initialized decl for a module variable. */
2770 gfc_create_module_variable (gfc_symbol * sym)
2774 /* Module functions with alternate entries are dealt with later and
2775 would get caught by the next condition. */
2776 if (sym->attr.entry)
2779 /* Make sure we convert the types of the derived types from iso_c_binding
2781 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
2782 && sym->ts.type == BT_DERIVED)
2783 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
2785 /* Only output variables and array valued, or derived type,
2787 if (sym->attr.flavor != FL_VARIABLE
2788 && !(sym->attr.flavor == FL_PARAMETER
2789 && (sym->attr.dimension || sym->ts.type == BT_DERIVED)))
2792 /* Don't generate variables from other modules. Variables from
2793 COMMONs will already have been generated. */
2794 if (sym->attr.use_assoc || sym->attr.in_common)
2797 /* Equivalenced variables arrive here after creation. */
2798 if (sym->backend_decl
2799 && (sym->equiv_built || sym->attr.in_equivalence))
2802 if (sym->backend_decl)
2803 internal_error ("backend decl for module variable %s already exists",
2806 /* We always want module variables to be created. */
2807 sym->attr.referenced = 1;
2808 /* Create the decl. */
2809 decl = gfc_get_symbol_decl (sym);
2811 /* Create the variable. */
2813 rest_of_decl_compilation (decl, 1, 0);
2815 /* Also add length of strings. */
2816 if (sym->ts.type == BT_CHARACTER)
2820 length = sym->ts.cl->backend_decl;
2821 if (!INTEGER_CST_P (length))
2824 rest_of_decl_compilation (length, 1, 0);
2830 /* Generate all the required code for module variables. */
2833 gfc_generate_module_vars (gfc_namespace * ns)
2835 module_namespace = ns;
2837 /* Check if the frontend left the namespace in a reasonable state. */
2838 gcc_assert (ns->proc_name && !ns->proc_name->tlink);
2840 /* Generate COMMON blocks. */
2841 gfc_trans_common (ns);
2843 /* Create decls for all the module variables. */
2844 gfc_traverse_ns (ns, gfc_create_module_variable);
2848 gfc_generate_contained_functions (gfc_namespace * parent)
2852 /* We create all the prototypes before generating any code. */
2853 for (ns = parent->contained; ns; ns = ns->sibling)
2855 /* Skip namespaces from used modules. */
2856 if (ns->parent != parent)
2859 gfc_create_function_decl (ns);
2862 for (ns = parent->contained; ns; ns = ns->sibling)
2864 /* Skip namespaces from used modules. */
2865 if (ns->parent != parent)
2868 gfc_generate_function_code (ns);
2873 /* Drill down through expressions for the array specification bounds and
2874 character length calling generate_local_decl for all those variables
2875 that have not already been declared. */
2878 generate_local_decl (gfc_symbol *);
2880 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
2883 expr_decls (gfc_expr *e, gfc_symbol *sym,
2884 int *f ATTRIBUTE_UNUSED)
2886 if (e->expr_type != EXPR_VARIABLE
2887 || sym == e->symtree->n.sym
2888 || e->symtree->n.sym->mark
2889 || e->symtree->n.sym->ns != sym->ns)
2892 generate_local_decl (e->symtree->n.sym);
2897 generate_expr_decls (gfc_symbol *sym, gfc_expr *e)
2899 gfc_traverse_expr (e, sym, expr_decls, 0);
2903 /* Check for dependencies in the character length and array spec. */
2906 generate_dependency_declarations (gfc_symbol *sym)
2910 if (sym->ts.type == BT_CHARACTER
2912 && sym->ts.cl->length
2913 && sym->ts.cl->length->expr_type != EXPR_CONSTANT)
2914 generate_expr_decls (sym, sym->ts.cl->length);
2916 if (sym->as && sym->as->rank)
2918 for (i = 0; i < sym->as->rank; i++)
2920 generate_expr_decls (sym, sym->as->lower[i]);
2921 generate_expr_decls (sym, sym->as->upper[i]);
2927 /* Generate decls for all local variables. We do this to ensure correct
2928 handling of expressions which only appear in the specification of
2932 generate_local_decl (gfc_symbol * sym)
2934 if (sym->attr.flavor == FL_VARIABLE)
2936 /* Check for dependencies in the array specification and string
2937 length, adding the necessary declarations to the function. We
2938 mark the symbol now, as well as in traverse_ns, to prevent
2939 getting stuck in a circular dependency. */
2941 if (!sym->attr.dummy && !sym->ns->proc_name->attr.entry_master)
2942 generate_dependency_declarations (sym);
2944 if (sym->attr.referenced)
2945 gfc_get_symbol_decl (sym);
2946 /* INTENT(out) dummy arguments are likely meant to be set. */
2947 else if (warn_unused_variable
2949 && sym->attr.intent == INTENT_OUT)
2950 gfc_warning ("Dummy argument '%s' at %L was declared INTENT(OUT) but was not set",
2951 sym->name, &sym->declared_at);
2952 /* Specific warning for unused dummy arguments. */
2953 else if (warn_unused_variable && sym->attr.dummy)
2954 gfc_warning ("Unused dummy argument '%s' at %L", sym->name,
2956 /* Warn for unused variables, but not if they're inside a common
2957 block or are use-associated. */
2958 else if (warn_unused_variable
2959 && !(sym->attr.in_common || sym->attr.use_assoc || sym->mark))
2960 gfc_warning ("Unused variable '%s' declared at %L", sym->name,
2962 /* For variable length CHARACTER parameters, the PARM_DECL already
2963 references the length variable, so force gfc_get_symbol_decl
2964 even when not referenced. If optimize > 0, it will be optimized
2965 away anyway. But do this only after emitting -Wunused-parameter
2966 warning if requested. */
2967 if (sym->attr.dummy && ! sym->attr.referenced
2968 && sym->ts.type == BT_CHARACTER
2969 && sym->ts.cl->backend_decl != NULL
2970 && TREE_CODE (sym->ts.cl->backend_decl) == VAR_DECL)
2972 sym->attr.referenced = 1;
2973 gfc_get_symbol_decl (sym);
2976 /* We do not want the middle-end to warn about unused parameters
2977 as this was already done above. */
2978 if (sym->attr.dummy && sym->backend_decl != NULL_TREE)
2979 TREE_NO_WARNING(sym->backend_decl) = 1;
2981 else if (sym->attr.flavor == FL_PARAMETER)
2983 if (warn_unused_parameter
2984 && !sym->attr.referenced
2985 && !sym->attr.use_assoc)
2986 gfc_warning ("Unused parameter '%s' declared at %L", sym->name,
2989 else if (sym->attr.flavor == FL_PROCEDURE)
2991 /* TODO: move to the appropriate place in resolve.c. */
2992 if (warn_return_type
2993 && sym->attr.function
2995 && sym != sym->result
2996 && !sym->result->attr.referenced
2997 && !sym->attr.use_assoc
2998 && sym->attr.if_source != IFSRC_IFBODY)
3000 gfc_warning ("Return value '%s' of function '%s' declared at "
3001 "%L not set", sym->result->name, sym->name,
3002 &sym->result->declared_at);
3004 /* Prevents "Unused variable" warning for RESULT variables. */
3005 sym->mark = sym->result->mark = 1;
3009 if (sym->attr.dummy == 1)
3011 /* Modify the tree type for scalar character dummy arguments of bind(c)
3012 procedures if they are passed by value. The tree type for them will
3013 be promoted to INTEGER_TYPE for the middle end, which appears to be
3014 what C would do with characters passed by-value. The value attribute
3015 implies the dummy is a scalar. */
3016 if (sym->attr.value == 1 && sym->backend_decl != NULL
3017 && sym->ts.type == BT_CHARACTER && sym->ts.is_c_interop
3018 && sym->ns->proc_name != NULL && sym->ns->proc_name->attr.is_bind_c)
3019 gfc_conv_scalar_char_value (sym, NULL, NULL);
3022 /* Make sure we convert the types of the derived types from iso_c_binding
3024 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
3025 && sym->ts.type == BT_DERIVED)
3026 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
3030 generate_local_vars (gfc_namespace * ns)
3032 gfc_traverse_ns (ns, generate_local_decl);
3036 /* Generate a switch statement to jump to the correct entry point. Also
3037 creates the label decls for the entry points. */
3040 gfc_trans_entry_master_switch (gfc_entry_list * el)
3047 gfc_init_block (&block);
3048 for (; el; el = el->next)
3050 /* Add the case label. */
3051 label = gfc_build_label_decl (NULL_TREE);
3052 val = build_int_cst (gfc_array_index_type, el->id);
3053 tmp = build3_v (CASE_LABEL_EXPR, val, NULL_TREE, label);
3054 gfc_add_expr_to_block (&block, tmp);
3056 /* And jump to the actual entry point. */
3057 label = gfc_build_label_decl (NULL_TREE);
3058 tmp = build1_v (GOTO_EXPR, label);
3059 gfc_add_expr_to_block (&block, tmp);
3061 /* Save the label decl. */
3064 tmp = gfc_finish_block (&block);
3065 /* The first argument selects the entry point. */
3066 val = DECL_ARGUMENTS (current_function_decl);
3067 tmp = build3_v (SWITCH_EXPR, val, tmp, NULL_TREE);
3072 /* Generate code for a function. */
3075 gfc_generate_function_code (gfc_namespace * ns)
3088 sym = ns->proc_name;
3090 /* Check that the frontend isn't still using this. */
3091 gcc_assert (sym->tlink == NULL);
3094 /* Create the declaration for functions with global scope. */
3095 if (!sym->backend_decl)
3096 gfc_create_function_decl (ns);
3098 fndecl = sym->backend_decl;
3099 old_context = current_function_decl;
3103 push_function_context ();
3104 saved_parent_function_decls = saved_function_decls;
3105 saved_function_decls = NULL_TREE;
3108 trans_function_start (sym);
3110 gfc_start_block (&block);
3112 if (ns->entries && ns->proc_name->ts.type == BT_CHARACTER)
3114 /* Copy length backend_decls to all entry point result
3119 gfc_conv_const_charlen (ns->proc_name->ts.cl);
3120 backend_decl = ns->proc_name->result->ts.cl->backend_decl;
3121 for (el = ns->entries; el; el = el->next)
3122 el->sym->result->ts.cl->backend_decl = backend_decl;
3125 /* Translate COMMON blocks. */
3126 gfc_trans_common (ns);
3128 /* Null the parent fake result declaration if this namespace is
3129 a module function or an external procedures. */
3130 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
3131 || ns->parent == NULL)
3132 parent_fake_result_decl = NULL_TREE;
3134 gfc_generate_contained_functions (ns);
3136 generate_local_vars (ns);
3138 /* Keep the parent fake result declaration in module functions
3139 or external procedures. */
3140 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
3141 || ns->parent == NULL)
3142 current_fake_result_decl = parent_fake_result_decl;
3144 current_fake_result_decl = NULL_TREE;
3146 current_function_return_label = NULL;
3148 /* Now generate the code for the body of this function. */
3149 gfc_init_block (&body);
3151 /* If this is the main program, add a call to set_options to set up the
3152 runtime library Fortran language standard parameters. */
3153 if (sym->attr.is_main_program)
3155 tree array_type, array, var;
3157 /* Passing a new option to the library requires four modifications:
3158 + add it to the tree_cons list below
3159 + change the array size in the call to build_array_type
3160 + change the first argument to the library call
3161 gfor_fndecl_set_options
3162 + modify the library (runtime/compile_options.c)! */
3163 array = tree_cons (NULL_TREE,
3164 build_int_cst (integer_type_node,
3165 gfc_option.warn_std), NULL_TREE);
3166 array = tree_cons (NULL_TREE,
3167 build_int_cst (integer_type_node,
3168 gfc_option.allow_std), array);
3169 array = tree_cons (NULL_TREE,
3170 build_int_cst (integer_type_node, pedantic), array);
3171 array = tree_cons (NULL_TREE,
3172 build_int_cst (integer_type_node,
3173 gfc_option.flag_dump_core), array);
3174 array = tree_cons (NULL_TREE,
3175 build_int_cst (integer_type_node,
3176 gfc_option.flag_backtrace), array);
3177 array = tree_cons (NULL_TREE,
3178 build_int_cst (integer_type_node,
3179 gfc_option.flag_sign_zero), array);
3181 array = tree_cons (NULL_TREE,
3182 build_int_cst (integer_type_node,
3183 flag_bounds_check), array);
3185 array_type = build_array_type (integer_type_node,
3186 build_index_type (build_int_cst (NULL_TREE,
3188 array = build_constructor_from_list (array_type, nreverse (array));
3189 TREE_CONSTANT (array) = 1;
3190 TREE_STATIC (array) = 1;
3192 /* Create a static variable to hold the jump table. */
3193 var = gfc_create_var (array_type, "options");
3194 TREE_CONSTANT (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"