1 /* Backend function setup
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
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"
40 #include "pointer-set.h"
42 #include "trans-types.h"
43 #include "trans-array.h"
44 #include "trans-const.h"
45 /* Only for gfc_trans_code. Shouldn't need to include this. */
46 #include "trans-stmt.h"
48 #define MAX_LABEL_VALUE 99999
51 /* Holds the result of the function if no result variable specified. */
53 static GTY(()) tree current_fake_result_decl;
54 static GTY(()) tree parent_fake_result_decl;
56 static GTY(()) tree current_function_return_label;
59 /* Holds the variable DECLs for the current function. */
61 static GTY(()) tree saved_function_decls;
62 static GTY(()) tree saved_parent_function_decls;
64 static struct pointer_set_t *nonlocal_dummy_decl_pset;
65 static GTY(()) tree nonlocal_dummy_decls;
67 /* The namespace of the module we're currently generating. Only used while
68 outputting decls for module variables. Do not rely on this being set. */
70 static gfc_namespace *module_namespace;
73 /* List of static constructor functions. */
75 tree gfc_static_ctors;
78 /* Function declarations for builtin library functions. */
80 tree gfor_fndecl_pause_numeric;
81 tree gfor_fndecl_pause_string;
82 tree gfor_fndecl_stop_numeric;
83 tree gfor_fndecl_stop_string;
84 tree gfor_fndecl_runtime_error;
85 tree gfor_fndecl_runtime_error_at;
86 tree gfor_fndecl_runtime_warning_at;
87 tree gfor_fndecl_os_error;
88 tree gfor_fndecl_generate_error;
89 tree gfor_fndecl_set_args;
90 tree gfor_fndecl_set_fpe;
91 tree gfor_fndecl_set_options;
92 tree gfor_fndecl_set_convert;
93 tree gfor_fndecl_set_record_marker;
94 tree gfor_fndecl_set_max_subrecord_length;
95 tree gfor_fndecl_ctime;
96 tree gfor_fndecl_fdate;
97 tree gfor_fndecl_ttynam;
98 tree gfor_fndecl_in_pack;
99 tree gfor_fndecl_in_unpack;
100 tree gfor_fndecl_associated;
103 /* Math functions. Many other math functions are handled in
104 trans-intrinsic.c. */
106 gfc_powdecl_list gfor_fndecl_math_powi[4][3];
107 tree gfor_fndecl_math_ishftc4;
108 tree gfor_fndecl_math_ishftc8;
109 tree gfor_fndecl_math_ishftc16;
112 /* String functions. */
114 tree gfor_fndecl_compare_string;
115 tree gfor_fndecl_concat_string;
116 tree gfor_fndecl_string_len_trim;
117 tree gfor_fndecl_string_index;
118 tree gfor_fndecl_string_scan;
119 tree gfor_fndecl_string_verify;
120 tree gfor_fndecl_string_trim;
121 tree gfor_fndecl_string_minmax;
122 tree gfor_fndecl_adjustl;
123 tree gfor_fndecl_adjustr;
124 tree gfor_fndecl_select_string;
125 tree gfor_fndecl_compare_string_char4;
126 tree gfor_fndecl_concat_string_char4;
127 tree gfor_fndecl_string_len_trim_char4;
128 tree gfor_fndecl_string_index_char4;
129 tree gfor_fndecl_string_scan_char4;
130 tree gfor_fndecl_string_verify_char4;
131 tree gfor_fndecl_string_trim_char4;
132 tree gfor_fndecl_string_minmax_char4;
133 tree gfor_fndecl_adjustl_char4;
134 tree gfor_fndecl_adjustr_char4;
135 tree gfor_fndecl_select_string_char4;
138 /* Conversion between character kinds. */
139 tree gfor_fndecl_convert_char1_to_char4;
140 tree gfor_fndecl_convert_char4_to_char1;
143 /* Other misc. runtime library functions. */
145 tree gfor_fndecl_size0;
146 tree gfor_fndecl_size1;
147 tree gfor_fndecl_iargc;
149 /* Intrinsic functions implemented in Fortran. */
150 tree gfor_fndecl_sc_kind;
151 tree gfor_fndecl_si_kind;
152 tree gfor_fndecl_sr_kind;
154 /* BLAS gemm functions. */
155 tree gfor_fndecl_sgemm;
156 tree gfor_fndecl_dgemm;
157 tree gfor_fndecl_cgemm;
158 tree gfor_fndecl_zgemm;
162 gfc_add_decl_to_parent_function (tree decl)
165 DECL_CONTEXT (decl) = DECL_CONTEXT (current_function_decl);
166 DECL_NONLOCAL (decl) = 1;
167 TREE_CHAIN (decl) = saved_parent_function_decls;
168 saved_parent_function_decls = decl;
172 gfc_add_decl_to_function (tree decl)
175 TREE_USED (decl) = 1;
176 DECL_CONTEXT (decl) = current_function_decl;
177 TREE_CHAIN (decl) = saved_function_decls;
178 saved_function_decls = decl;
182 /* Build a backend label declaration. Set TREE_USED for named labels.
183 The context of the label is always the current_function_decl. All
184 labels are marked artificial. */
187 gfc_build_label_decl (tree label_id)
189 /* 2^32 temporaries should be enough. */
190 static unsigned int tmp_num = 1;
194 if (label_id == NULL_TREE)
196 /* Build an internal label name. */
197 ASM_FORMAT_PRIVATE_NAME (label_name, "L", tmp_num++);
198 label_id = get_identifier (label_name);
203 /* Build the LABEL_DECL node. Labels have no type. */
204 label_decl = build_decl (LABEL_DECL, label_id, void_type_node);
205 DECL_CONTEXT (label_decl) = current_function_decl;
206 DECL_MODE (label_decl) = VOIDmode;
208 /* We always define the label as used, even if the original source
209 file never references the label. We don't want all kinds of
210 spurious warnings for old-style Fortran code with too many
212 TREE_USED (label_decl) = 1;
214 DECL_ARTIFICIAL (label_decl) = 1;
219 /* Returns the return label for the current function. */
222 gfc_get_return_label (void)
224 char name[GFC_MAX_SYMBOL_LEN + 10];
226 if (current_function_return_label)
227 return current_function_return_label;
229 sprintf (name, "__return_%s",
230 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
232 current_function_return_label =
233 gfc_build_label_decl (get_identifier (name));
235 DECL_ARTIFICIAL (current_function_return_label) = 1;
237 return current_function_return_label;
241 /* Set the backend source location of a decl. */
244 gfc_set_decl_location (tree decl, locus * loc)
246 DECL_SOURCE_LOCATION (decl) = loc->lb->location;
250 /* Return the backend label declaration for a given label structure,
251 or create it if it doesn't exist yet. */
254 gfc_get_label_decl (gfc_st_label * lp)
256 if (lp->backend_decl)
257 return lp->backend_decl;
260 char label_name[GFC_MAX_SYMBOL_LEN + 1];
263 /* Validate the label declaration from the front end. */
264 gcc_assert (lp != NULL && lp->value <= MAX_LABEL_VALUE);
266 /* Build a mangled name for the label. */
267 sprintf (label_name, "__label_%.6d", lp->value);
269 /* Build the LABEL_DECL node. */
270 label_decl = gfc_build_label_decl (get_identifier (label_name));
272 /* Tell the debugger where the label came from. */
273 if (lp->value <= MAX_LABEL_VALUE) /* An internal label. */
274 gfc_set_decl_location (label_decl, &lp->where);
276 DECL_ARTIFICIAL (label_decl) = 1;
278 /* Store the label in the label list and return the LABEL_DECL. */
279 lp->backend_decl = label_decl;
285 /* Convert a gfc_symbol to an identifier of the same name. */
288 gfc_sym_identifier (gfc_symbol * sym)
290 return (get_identifier (sym->name));
294 /* Construct mangled name from symbol name. */
297 gfc_sym_mangled_identifier (gfc_symbol * sym)
299 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
301 /* Prevent the mangling of identifiers that have an assigned
302 binding label (mainly those that are bind(c)). */
303 if (sym->attr.is_bind_c == 1
304 && sym->binding_label[0] != '\0')
305 return get_identifier(sym->binding_label);
307 if (sym->module == NULL)
308 return gfc_sym_identifier (sym);
311 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
312 return get_identifier (name);
317 /* Construct mangled function name from symbol name. */
320 gfc_sym_mangled_function_id (gfc_symbol * sym)
323 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
325 /* It may be possible to simply use the binding label if it's
326 provided, and remove the other checks. Then we could use it
327 for other things if we wished. */
328 if ((sym->attr.is_bind_c == 1 || sym->attr.is_iso_c == 1) &&
329 sym->binding_label[0] != '\0')
330 /* use the binding label rather than the mangled name */
331 return get_identifier (sym->binding_label);
333 if (sym->module == NULL || sym->attr.proc == PROC_EXTERNAL
334 || (sym->module != NULL && (sym->attr.external
335 || sym->attr.if_source == IFSRC_IFBODY)))
337 /* Main program is mangled into MAIN__. */
338 if (sym->attr.is_main_program)
339 return get_identifier ("MAIN__");
341 /* Intrinsic procedures are never mangled. */
342 if (sym->attr.proc == PROC_INTRINSIC)
343 return get_identifier (sym->name);
345 if (gfc_option.flag_underscoring)
347 has_underscore = strchr (sym->name, '_') != 0;
348 if (gfc_option.flag_second_underscore && has_underscore)
349 snprintf (name, sizeof name, "%s__", sym->name);
351 snprintf (name, sizeof name, "%s_", sym->name);
352 return get_identifier (name);
355 return get_identifier (sym->name);
359 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
360 return get_identifier (name);
365 /* Returns true if a variable of specified size should go on the stack. */
368 gfc_can_put_var_on_stack (tree size)
370 unsigned HOST_WIDE_INT low;
372 if (!INTEGER_CST_P (size))
375 if (gfc_option.flag_max_stack_var_size < 0)
378 if (TREE_INT_CST_HIGH (size) != 0)
381 low = TREE_INT_CST_LOW (size);
382 if (low > (unsigned HOST_WIDE_INT) gfc_option.flag_max_stack_var_size)
385 /* TODO: Set a per-function stack size limit. */
391 /* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
392 an expression involving its corresponding pointer. There are
393 2 cases; one for variable size arrays, and one for everything else,
394 because variable-sized arrays require one fewer level of
398 gfc_finish_cray_pointee (tree decl, gfc_symbol *sym)
400 tree ptr_decl = gfc_get_symbol_decl (sym->cp_pointer);
403 /* Parameters need to be dereferenced. */
404 if (sym->cp_pointer->attr.dummy)
405 ptr_decl = build_fold_indirect_ref (ptr_decl);
407 /* Check to see if we're dealing with a variable-sized array. */
408 if (sym->attr.dimension
409 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
411 /* These decls will be dereferenced later, so we don't dereference
413 value = convert (TREE_TYPE (decl), ptr_decl);
417 ptr_decl = convert (build_pointer_type (TREE_TYPE (decl)),
419 value = build_fold_indirect_ref (ptr_decl);
422 SET_DECL_VALUE_EXPR (decl, value);
423 DECL_HAS_VALUE_EXPR_P (decl) = 1;
424 GFC_DECL_CRAY_POINTEE (decl) = 1;
425 /* This is a fake variable just for debugging purposes. */
426 TREE_ASM_WRITTEN (decl) = 1;
430 /* Finish processing of a declaration without an initial value. */
433 gfc_finish_decl (tree decl)
435 gcc_assert (TREE_CODE (decl) == PARM_DECL
436 || DECL_INITIAL (decl) == NULL_TREE);
438 if (TREE_CODE (decl) != VAR_DECL)
441 if (DECL_SIZE (decl) == NULL_TREE
442 && TYPE_SIZE (TREE_TYPE (decl)) != NULL_TREE)
443 layout_decl (decl, 0);
445 /* A few consistency checks. */
446 /* A static variable with an incomplete type is an error if it is
447 initialized. Also if it is not file scope. Otherwise, let it
448 through, but if it is not `extern' then it may cause an error
450 /* An automatic variable with an incomplete type is an error. */
452 /* We should know the storage size. */
453 gcc_assert (DECL_SIZE (decl) != NULL_TREE
454 || (TREE_STATIC (decl)
455 ? (!DECL_INITIAL (decl) || !DECL_CONTEXT (decl))
456 : DECL_EXTERNAL (decl)));
458 /* The storage size should be constant. */
459 gcc_assert ((!DECL_EXTERNAL (decl) && !TREE_STATIC (decl))
461 || TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST);
465 /* Apply symbol attributes to a variable, and add it to the function scope. */
468 gfc_finish_var_decl (tree decl, gfc_symbol * sym)
471 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
472 This is the equivalent of the TARGET variables.
473 We also need to set this if the variable is passed by reference in a
476 /* Set DECL_VALUE_EXPR for Cray Pointees. */
477 if (sym->attr.cray_pointee)
478 gfc_finish_cray_pointee (decl, sym);
480 if (sym->attr.target)
481 TREE_ADDRESSABLE (decl) = 1;
482 /* If it wasn't used we wouldn't be getting it. */
483 TREE_USED (decl) = 1;
485 /* Chain this decl to the pending declarations. Don't do pushdecl()
486 because this would add them to the current scope rather than the
488 if (current_function_decl != NULL_TREE)
490 if (sym->ns->proc_name->backend_decl == current_function_decl
491 || sym->result == sym)
492 gfc_add_decl_to_function (decl);
494 gfc_add_decl_to_parent_function (decl);
497 if (sym->attr.cray_pointee)
500 if(sym->attr.is_bind_c == 1)
502 /* We need to put variables that are bind(c) into the common
503 segment of the object file, because this is what C would do.
504 gfortran would typically put them in either the BSS or
505 initialized data segments, and only mark them as common if
506 they were part of common blocks. However, if they are not put
507 into common space, then C cannot initialize global fortran
508 variables that it interoperates with and the draft says that
509 either Fortran or C should be able to initialize it (but not
510 both, of course.) (J3/04-007, section 15.3). */
511 TREE_PUBLIC(decl) = 1;
512 DECL_COMMON(decl) = 1;
515 /* If a variable is USE associated, it's always external. */
516 if (sym->attr.use_assoc)
518 DECL_EXTERNAL (decl) = 1;
519 TREE_PUBLIC (decl) = 1;
521 else if (sym->module && !sym->attr.result && !sym->attr.dummy)
523 /* TODO: Don't set sym->module for result or dummy variables. */
524 gcc_assert (current_function_decl == NULL_TREE || sym->result == sym);
525 /* This is the declaration of a module variable. */
526 TREE_PUBLIC (decl) = 1;
527 TREE_STATIC (decl) = 1;
530 /* Derived types are a bit peculiar because of the possibility of
531 a default initializer; this must be applied each time the variable
532 comes into scope it therefore need not be static. These variables
533 are SAVE_NONE but have an initializer. Otherwise explicitly
534 initialized variables are SAVE_IMPLICIT and explicitly saved are
536 if (!sym->attr.use_assoc
537 && (sym->attr.save != SAVE_NONE || sym->attr.data
538 || (sym->value && sym->ns->proc_name->attr.is_main_program)))
539 TREE_STATIC (decl) = 1;
541 if (sym->attr.volatile_)
543 TREE_THIS_VOLATILE (decl) = 1;
544 new_type = build_qualified_type (TREE_TYPE (decl), TYPE_QUAL_VOLATILE);
545 TREE_TYPE (decl) = new_type;
548 /* Keep variables larger than max-stack-var-size off stack. */
549 if (!sym->ns->proc_name->attr.recursive
550 && INTEGER_CST_P (DECL_SIZE_UNIT (decl))
551 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
552 /* Put variable length auto array pointers always into stack. */
553 && (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
554 || sym->attr.dimension == 0
555 || sym->as->type != AS_EXPLICIT
557 || sym->attr.allocatable)
558 && !DECL_ARTIFICIAL (decl))
559 TREE_STATIC (decl) = 1;
561 /* Handle threadprivate variables. */
562 if (sym->attr.threadprivate
563 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
564 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
568 /* Allocate the lang-specific part of a decl. */
571 gfc_allocate_lang_decl (tree decl)
573 DECL_LANG_SPECIFIC (decl) = (struct lang_decl *)
574 ggc_alloc_cleared (sizeof (struct lang_decl));
577 /* Remember a symbol to generate initialization/cleanup code at function
581 gfc_defer_symbol_init (gfc_symbol * sym)
587 /* Don't add a symbol twice. */
591 last = head = sym->ns->proc_name;
594 /* Make sure that setup code for dummy variables which are used in the
595 setup of other variables is generated first. */
598 /* Find the first dummy arg seen after us, or the first non-dummy arg.
599 This is a circular list, so don't go past the head. */
601 && (!p->attr.dummy || p->dummy_order > sym->dummy_order))
607 /* Insert in between last and p. */
613 /* Create an array index type variable with function scope. */
616 create_index_var (const char * pfx, int nest)
620 decl = gfc_create_var_np (gfc_array_index_type, pfx);
622 gfc_add_decl_to_parent_function (decl);
624 gfc_add_decl_to_function (decl);
629 /* Create variables to hold all the non-constant bits of info for a
630 descriptorless array. Remember these in the lang-specific part of the
634 gfc_build_qualified_array (tree decl, gfc_symbol * sym)
640 type = TREE_TYPE (decl);
642 /* We just use the descriptor, if there is one. */
643 if (GFC_DESCRIPTOR_TYPE_P (type))
646 gcc_assert (GFC_ARRAY_TYPE_P (type));
647 nest = (sym->ns->proc_name->backend_decl != current_function_decl)
648 && !sym->attr.contained;
650 for (dim = 0; dim < GFC_TYPE_ARRAY_RANK (type); dim++)
652 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
654 GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
655 TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type, dim)) = 1;
657 /* Don't try to use the unknown bound for assumed shape arrays. */
658 if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
659 && (sym->as->type != AS_ASSUMED_SIZE
660 || dim < GFC_TYPE_ARRAY_RANK (type) - 1))
662 GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
663 TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type, dim)) = 1;
666 if (GFC_TYPE_ARRAY_STRIDE (type, dim) == NULL_TREE)
668 GFC_TYPE_ARRAY_STRIDE (type, dim) = create_index_var ("stride", nest);
669 TREE_NO_WARNING (GFC_TYPE_ARRAY_STRIDE (type, dim)) = 1;
672 if (GFC_TYPE_ARRAY_OFFSET (type) == NULL_TREE)
674 GFC_TYPE_ARRAY_OFFSET (type) = gfc_create_var_np (gfc_array_index_type,
676 TREE_NO_WARNING (GFC_TYPE_ARRAY_OFFSET (type)) = 1;
679 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type));
681 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type));
684 if (GFC_TYPE_ARRAY_SIZE (type) == NULL_TREE
685 && sym->as->type != AS_ASSUMED_SIZE)
687 GFC_TYPE_ARRAY_SIZE (type) = create_index_var ("size", nest);
688 TREE_NO_WARNING (GFC_TYPE_ARRAY_SIZE (type)) = 1;
691 if (POINTER_TYPE_P (type))
693 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type)));
694 gcc_assert (TYPE_LANG_SPECIFIC (type)
695 == TYPE_LANG_SPECIFIC (TREE_TYPE (type)));
696 type = TREE_TYPE (type);
699 if (! COMPLETE_TYPE_P (type) && GFC_TYPE_ARRAY_SIZE (type))
703 size = fold_build2 (MINUS_EXPR, gfc_array_index_type,
704 GFC_TYPE_ARRAY_SIZE (type), gfc_index_one_node);
705 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
707 TYPE_DOMAIN (type) = range;
711 if (write_symbols == NO_DEBUG)
714 if (TYPE_NAME (type) != NULL_TREE
715 && GFC_TYPE_ARRAY_UBOUND (type, sym->as->rank - 1) != NULL_TREE
716 && TREE_CODE (GFC_TYPE_ARRAY_UBOUND (type, sym->as->rank - 1)) == VAR_DECL)
718 tree gtype = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
720 for (dim = 0; dim < sym->as->rank - 1; dim++)
722 gcc_assert (TREE_CODE (gtype) == ARRAY_TYPE);
723 gtype = TREE_TYPE (gtype);
725 gcc_assert (TREE_CODE (gtype) == ARRAY_TYPE);
726 if (TYPE_MAX_VALUE (TYPE_DOMAIN (gtype)) == NULL)
727 TYPE_NAME (type) = NULL_TREE;
730 if (TYPE_NAME (type) == NULL_TREE)
732 tree gtype = TREE_TYPE (type), rtype, type_decl;
734 for (dim = sym->as->rank - 1; dim >= 0; dim--)
736 rtype = build_range_type (gfc_array_index_type,
737 GFC_TYPE_ARRAY_LBOUND (type, dim),
738 GFC_TYPE_ARRAY_UBOUND (type, dim));
739 gtype = build_array_type (gtype, rtype);
740 /* Ensure the bound variables aren't optimized out at -O0. */
743 if (GFC_TYPE_ARRAY_LBOUND (type, dim)
744 && TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) == VAR_DECL)
745 DECL_IGNORED_P (GFC_TYPE_ARRAY_LBOUND (type, dim)) = 0;
746 if (GFC_TYPE_ARRAY_UBOUND (type, dim)
747 && TREE_CODE (GFC_TYPE_ARRAY_UBOUND (type, dim)) == VAR_DECL)
748 DECL_IGNORED_P (GFC_TYPE_ARRAY_UBOUND (type, dim)) = 0;
751 TYPE_NAME (type) = type_decl = build_decl (TYPE_DECL, NULL, gtype);
752 DECL_ORIGINAL_TYPE (type_decl) = gtype;
757 /* For some dummy arguments we don't use the actual argument directly.
758 Instead we create a local decl and use that. This allows us to perform
759 initialization, and construct full type information. */
762 gfc_build_dummy_array_decl (gfc_symbol * sym, tree dummy)
772 if (sym->attr.pointer || sym->attr.allocatable)
775 /* Add to list of variables if not a fake result variable. */
776 if (sym->attr.result || sym->attr.dummy)
777 gfc_defer_symbol_init (sym);
779 type = TREE_TYPE (dummy);
780 gcc_assert (TREE_CODE (dummy) == PARM_DECL
781 && POINTER_TYPE_P (type));
783 /* Do we know the element size? */
784 known_size = sym->ts.type != BT_CHARACTER
785 || INTEGER_CST_P (sym->ts.cl->backend_decl);
787 if (known_size && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type)))
789 /* For descriptorless arrays with known element size the actual
790 argument is sufficient. */
791 gcc_assert (GFC_ARRAY_TYPE_P (type));
792 gfc_build_qualified_array (dummy, sym);
796 type = TREE_TYPE (type);
797 if (GFC_DESCRIPTOR_TYPE_P (type))
799 /* Create a descriptorless array pointer. */
803 /* Even when -frepack-arrays is used, symbols with TARGET attribute
805 if (!gfc_option.flag_repack_arrays || sym->attr.target)
807 if (as->type == AS_ASSUMED_SIZE)
808 packed = PACKED_FULL;
812 if (as->type == AS_EXPLICIT)
814 packed = PACKED_FULL;
815 for (n = 0; n < as->rank; n++)
819 && as->upper[n]->expr_type == EXPR_CONSTANT
820 && as->lower[n]->expr_type == EXPR_CONSTANT))
821 packed = PACKED_PARTIAL;
825 packed = PACKED_PARTIAL;
828 type = gfc_typenode_for_spec (&sym->ts);
829 type = gfc_get_nodesc_array_type (type, sym->as, packed);
833 /* We now have an expression for the element size, so create a fully
834 qualified type. Reset sym->backend decl or this will just return the
836 DECL_ARTIFICIAL (sym->backend_decl) = 1;
837 sym->backend_decl = NULL_TREE;
838 type = gfc_sym_type (sym);
839 packed = PACKED_FULL;
842 ASM_FORMAT_PRIVATE_NAME (name, IDENTIFIER_POINTER (DECL_NAME (dummy)), 0);
843 decl = build_decl (VAR_DECL, get_identifier (name), type);
845 DECL_ARTIFICIAL (decl) = 1;
846 TREE_PUBLIC (decl) = 0;
847 TREE_STATIC (decl) = 0;
848 DECL_EXTERNAL (decl) = 0;
850 /* We should never get deferred shape arrays here. We used to because of
852 gcc_assert (sym->as->type != AS_DEFERRED);
854 if (packed == PACKED_PARTIAL)
855 GFC_DECL_PARTIAL_PACKED_ARRAY (decl) = 1;
856 else if (packed == PACKED_FULL)
857 GFC_DECL_PACKED_ARRAY (decl) = 1;
859 gfc_build_qualified_array (decl, sym);
861 if (DECL_LANG_SPECIFIC (dummy))
862 DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (dummy);
864 gfc_allocate_lang_decl (decl);
866 GFC_DECL_SAVED_DESCRIPTOR (decl) = dummy;
868 if (sym->ns->proc_name->backend_decl == current_function_decl
869 || sym->attr.contained)
870 gfc_add_decl_to_function (decl);
872 gfc_add_decl_to_parent_function (decl);
877 /* For symbol SYM with GFC_DECL_SAVED_DESCRIPTOR used in contained
878 function add a VAR_DECL to the current function with DECL_VALUE_EXPR
879 pointing to the artificial variable for debug info purposes. */
882 gfc_nonlocal_dummy_array_decl (gfc_symbol *sym)
886 if (! nonlocal_dummy_decl_pset)
887 nonlocal_dummy_decl_pset = pointer_set_create ();
889 if (pointer_set_insert (nonlocal_dummy_decl_pset, sym->backend_decl))
892 dummy = GFC_DECL_SAVED_DESCRIPTOR (sym->backend_decl);
893 decl = build_decl (VAR_DECL, DECL_NAME (dummy),
894 TREE_TYPE (sym->backend_decl));
895 DECL_ARTIFICIAL (decl) = 0;
896 TREE_USED (decl) = 1;
897 TREE_PUBLIC (decl) = 0;
898 TREE_STATIC (decl) = 0;
899 DECL_EXTERNAL (decl) = 0;
900 if (DECL_BY_REFERENCE (dummy))
901 DECL_BY_REFERENCE (decl) = 1;
902 DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (sym->backend_decl);
903 SET_DECL_VALUE_EXPR (decl, sym->backend_decl);
904 DECL_HAS_VALUE_EXPR_P (decl) = 1;
905 DECL_CONTEXT (decl) = DECL_CONTEXT (sym->backend_decl);
906 TREE_CHAIN (decl) = nonlocal_dummy_decls;
907 nonlocal_dummy_decls = decl;
910 /* Return a constant or a variable to use as a string length. Does not
911 add the decl to the current scope. */
914 gfc_create_string_length (gfc_symbol * sym)
916 gcc_assert (sym->ts.cl);
917 gfc_conv_const_charlen (sym->ts.cl);
919 if (sym->ts.cl->backend_decl == NULL_TREE)
922 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
924 /* Also prefix the mangled name. */
925 strcpy (&name[1], sym->name);
927 length = build_decl (VAR_DECL, get_identifier (name),
928 gfc_charlen_type_node);
929 DECL_ARTIFICIAL (length) = 1;
930 TREE_USED (length) = 1;
931 if (sym->ns->proc_name->tlink != NULL)
932 gfc_defer_symbol_init (sym);
934 sym->ts.cl->backend_decl = length;
937 gcc_assert (sym->ts.cl->backend_decl != NULL_TREE);
938 return sym->ts.cl->backend_decl;
941 /* If a variable is assigned a label, we add another two auxiliary
945 gfc_add_assign_aux_vars (gfc_symbol * sym)
951 gcc_assert (sym->backend_decl);
953 decl = sym->backend_decl;
954 gfc_allocate_lang_decl (decl);
955 GFC_DECL_ASSIGN (decl) = 1;
956 length = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
957 gfc_charlen_type_node);
958 addr = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
960 gfc_finish_var_decl (length, sym);
961 gfc_finish_var_decl (addr, sym);
962 /* STRING_LENGTH is also used as flag. Less than -1 means that
963 ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the
964 target label's address. Otherwise, value is the length of a format string
965 and ASSIGN_ADDR is its address. */
966 if (TREE_STATIC (length))
967 DECL_INITIAL (length) = build_int_cst (NULL_TREE, -2);
969 gfc_defer_symbol_init (sym);
971 GFC_DECL_STRING_LEN (decl) = length;
972 GFC_DECL_ASSIGN_ADDR (decl) = addr;
975 /* Return the decl for a gfc_symbol, create it if it doesn't already
979 gfc_get_symbol_decl (gfc_symbol * sym)
982 tree length = NULL_TREE;
985 gcc_assert (sym->attr.referenced
986 || sym->attr.use_assoc
987 || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY);
989 if (sym->ns && sym->ns->proc_name->attr.function)
990 byref = gfc_return_by_reference (sym->ns->proc_name);
994 if ((sym->attr.dummy && ! sym->attr.function) || (sym->attr.result && byref))
996 /* Return via extra parameter. */
997 if (sym->attr.result && byref
998 && !sym->backend_decl)
1001 DECL_ARGUMENTS (sym->ns->proc_name->backend_decl);
1002 /* For entry master function skip over the __entry
1004 if (sym->ns->proc_name->attr.entry_master)
1005 sym->backend_decl = TREE_CHAIN (sym->backend_decl);
1008 /* Dummy variables should already have been created. */
1009 gcc_assert (sym->backend_decl);
1011 /* Create a character length variable. */
1012 if (sym->ts.type == BT_CHARACTER)
1014 if (sym->ts.cl->backend_decl == NULL_TREE)
1015 length = gfc_create_string_length (sym);
1017 length = sym->ts.cl->backend_decl;
1018 if (TREE_CODE (length) == VAR_DECL
1019 && DECL_CONTEXT (length) == NULL_TREE)
1021 /* Add the string length to the same context as the symbol. */
1022 if (DECL_CONTEXT (sym->backend_decl) == current_function_decl)
1023 gfc_add_decl_to_function (length);
1025 gfc_add_decl_to_parent_function (length);
1027 gcc_assert (DECL_CONTEXT (sym->backend_decl) ==
1028 DECL_CONTEXT (length));
1030 gfc_defer_symbol_init (sym);
1034 /* Use a copy of the descriptor for dummy arrays. */
1035 if (sym->attr.dimension && !TREE_USED (sym->backend_decl))
1037 decl = gfc_build_dummy_array_decl (sym, sym->backend_decl);
1038 /* Prevent the dummy from being detected as unused if it is copied. */
1039 if (sym->backend_decl != NULL && decl != sym->backend_decl)
1040 DECL_ARTIFICIAL (sym->backend_decl) = 1;
1041 sym->backend_decl = decl;
1044 TREE_USED (sym->backend_decl) = 1;
1045 if (sym->attr.assign && GFC_DECL_ASSIGN (sym->backend_decl) == 0)
1047 gfc_add_assign_aux_vars (sym);
1050 if (sym->attr.dimension
1051 && DECL_LANG_SPECIFIC (sym->backend_decl)
1052 && GFC_DECL_SAVED_DESCRIPTOR (sym->backend_decl)
1053 && DECL_CONTEXT (sym->backend_decl) != current_function_decl)
1054 gfc_nonlocal_dummy_array_decl (sym);
1056 return sym->backend_decl;
1059 if (sym->backend_decl)
1060 return sym->backend_decl;
1062 /* Catch function declarations. Only used for actual parameters and
1063 procedure pointers. */
1064 if (sym->attr.flavor == FL_PROCEDURE)
1066 decl = gfc_get_extern_function_decl (sym);
1067 gfc_set_decl_location (decl, &sym->declared_at);
1071 if (sym->attr.intrinsic)
1072 internal_error ("intrinsic variable which isn't a procedure");
1074 /* Create string length decl first so that they can be used in the
1075 type declaration. */
1076 if (sym->ts.type == BT_CHARACTER)
1077 length = gfc_create_string_length (sym);
1079 /* Create the decl for the variable. */
1080 decl = build_decl (VAR_DECL, gfc_sym_identifier (sym), gfc_sym_type (sym));
1082 gfc_set_decl_location (decl, &sym->declared_at);
1084 /* Symbols from modules should have their assembler names mangled.
1085 This is done here rather than in gfc_finish_var_decl because it
1086 is different for string length variables. */
1089 SET_DECL_ASSEMBLER_NAME (decl, gfc_sym_mangled_identifier (sym));
1090 if (sym->attr.use_assoc)
1091 DECL_IGNORED_P (decl) = 1;
1094 if (sym->attr.dimension)
1096 /* Create variables to hold the non-constant bits of array info. */
1097 gfc_build_qualified_array (decl, sym);
1099 /* Remember this variable for allocation/cleanup. */
1100 gfc_defer_symbol_init (sym);
1102 if ((sym->attr.allocatable || !sym->attr.dummy) && !sym->attr.pointer)
1103 GFC_DECL_PACKED_ARRAY (decl) = 1;
1106 if (sym->ts.type == BT_DERIVED && sym->ts.derived->attr.alloc_comp)
1107 gfc_defer_symbol_init (sym);
1108 /* This applies a derived type default initializer. */
1109 else if (sym->ts.type == BT_DERIVED
1110 && sym->attr.save == SAVE_NONE
1112 && !sym->attr.allocatable
1113 && (sym->value && !sym->ns->proc_name->attr.is_main_program)
1114 && !sym->attr.use_assoc)
1115 gfc_defer_symbol_init (sym);
1117 gfc_finish_var_decl (decl, sym);
1119 if (sym->ts.type == BT_CHARACTER)
1121 /* Character variables need special handling. */
1122 gfc_allocate_lang_decl (decl);
1124 if (TREE_CODE (length) != INTEGER_CST)
1126 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
1130 /* Also prefix the mangled name for symbols from modules. */
1131 strcpy (&name[1], sym->name);
1134 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (length)));
1135 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (name));
1137 gfc_finish_var_decl (length, sym);
1138 gcc_assert (!sym->value);
1141 else if (sym->attr.subref_array_pointer)
1143 /* We need the span for these beasts. */
1144 gfc_allocate_lang_decl (decl);
1147 if (sym->attr.subref_array_pointer)
1150 GFC_DECL_SUBREF_ARRAY_P (decl) = 1;
1151 span = build_decl (VAR_DECL, create_tmp_var_name ("span"),
1152 gfc_array_index_type);
1153 gfc_finish_var_decl (span, sym);
1154 TREE_STATIC (span) = TREE_STATIC (decl);
1155 DECL_ARTIFICIAL (span) = 1;
1156 DECL_INITIAL (span) = build_int_cst (gfc_array_index_type, 0);
1158 GFC_DECL_SPAN (decl) = span;
1159 GFC_TYPE_ARRAY_SPAN (TREE_TYPE (decl)) = span;
1162 sym->backend_decl = decl;
1164 if (sym->attr.assign)
1165 gfc_add_assign_aux_vars (sym);
1167 if (TREE_STATIC (decl) && !sym->attr.use_assoc)
1169 /* Add static initializer. */
1170 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
1171 TREE_TYPE (decl), sym->attr.dimension,
1172 sym->attr.pointer || sym->attr.allocatable);
1175 if (!TREE_STATIC (decl)
1176 && POINTER_TYPE_P (TREE_TYPE (decl))
1177 && !sym->attr.pointer
1178 && !sym->attr.allocatable
1179 && !sym->attr.proc_pointer)
1180 DECL_BY_REFERENCE (decl) = 1;
1186 /* Substitute a temporary variable in place of the real one. */
1189 gfc_shadow_sym (gfc_symbol * sym, tree decl, gfc_saved_var * save)
1191 save->attr = sym->attr;
1192 save->decl = sym->backend_decl;
1194 gfc_clear_attr (&sym->attr);
1195 sym->attr.referenced = 1;
1196 sym->attr.flavor = FL_VARIABLE;
1198 sym->backend_decl = decl;
1202 /* Restore the original variable. */
1205 gfc_restore_sym (gfc_symbol * sym, gfc_saved_var * save)
1207 sym->attr = save->attr;
1208 sym->backend_decl = save->decl;
1212 /* Declare a procedure pointer. */
1215 get_proc_pointer_decl (gfc_symbol *sym)
1219 decl = sym->backend_decl;
1223 decl = build_decl (VAR_DECL, get_identifier (sym->name),
1224 build_pointer_type (gfc_get_function_type (sym)));
1226 if ((sym->ns->proc_name
1227 && sym->ns->proc_name->backend_decl == current_function_decl)
1228 || sym->attr.contained)
1229 gfc_add_decl_to_function (decl);
1230 else if (sym->ns->proc_name->attr.flavor != FL_MODULE)
1231 gfc_add_decl_to_parent_function (decl);
1233 sym->backend_decl = decl;
1235 /* If a variable is USE associated, it's always external. */
1236 if (sym->attr.use_assoc)
1238 DECL_EXTERNAL (decl) = 1;
1239 TREE_PUBLIC (decl) = 1;
1241 else if (sym->module && sym->ns->proc_name->attr.flavor == FL_MODULE)
1243 /* This is the declaration of a module variable. */
1244 TREE_PUBLIC (decl) = 1;
1245 TREE_STATIC (decl) = 1;
1248 if (!sym->attr.use_assoc
1249 && (sym->attr.save != SAVE_NONE || sym->attr.data
1250 || (sym->value && sym->ns->proc_name->attr.is_main_program)))
1251 TREE_STATIC (decl) = 1;
1253 if (TREE_STATIC (decl) && sym->value)
1255 /* Add static initializer. */
1256 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
1257 TREE_TYPE (decl), sym->attr.dimension, sym->attr.proc_pointer);
1264 /* Get a basic decl for an external function. */
1267 gfc_get_extern_function_decl (gfc_symbol * sym)
1272 gfc_intrinsic_sym *isym;
1274 char s[GFC_MAX_SYMBOL_LEN + 23]; /* "_gfortran_f2c_specific" and '\0'. */
1279 if (sym->backend_decl)
1280 return sym->backend_decl;
1282 /* We should never be creating external decls for alternate entry points.
1283 The procedure may be an alternate entry point, but we don't want/need
1285 gcc_assert (!(sym->attr.entry || sym->attr.entry_master));
1287 if (sym->attr.proc_pointer)
1288 return get_proc_pointer_decl (sym);
1290 /* See if this is an external procedure from the same file. If so,
1291 return the backend_decl. */
1292 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->name);
1294 if (gfc_option.flag_whole_file
1295 && !sym->backend_decl
1297 && ((gsym->type == GSYM_SUBROUTINE) || (gsym->type == GSYM_FUNCTION))
1298 && gsym->ns->proc_name->backend_decl)
1300 /* If the namespace has entries, the proc_name is the
1301 entry master. Find the entry and use its backend_decl.
1302 otherwise, use the proc_name backend_decl. */
1303 if (gsym->ns->entries)
1305 gfc_entry_list *entry = gsym->ns->entries;
1307 for (; entry; entry = entry->next)
1309 if (strcmp (gsym->name, entry->sym->name) == 0)
1311 sym->backend_decl = entry->sym->backend_decl;
1318 sym->backend_decl = gsym->ns->proc_name->backend_decl;
1321 if (sym->backend_decl)
1322 return sym->backend_decl;
1325 if (sym->attr.intrinsic)
1327 /* Call the resolution function to get the actual name. This is
1328 a nasty hack which relies on the resolution functions only looking
1329 at the first argument. We pass NULL for the second argument
1330 otherwise things like AINT get confused. */
1331 isym = gfc_find_function (sym->name);
1332 gcc_assert (isym->resolve.f0 != NULL);
1334 memset (&e, 0, sizeof (e));
1335 e.expr_type = EXPR_FUNCTION;
1337 memset (&argexpr, 0, sizeof (argexpr));
1338 gcc_assert (isym->formal);
1339 argexpr.ts = isym->formal->ts;
1341 if (isym->formal->next == NULL)
1342 isym->resolve.f1 (&e, &argexpr);
1345 if (isym->formal->next->next == NULL)
1346 isym->resolve.f2 (&e, &argexpr, NULL);
1349 if (isym->formal->next->next->next == NULL)
1350 isym->resolve.f3 (&e, &argexpr, NULL, NULL);
1353 /* All specific intrinsics take less than 5 arguments. */
1354 gcc_assert (isym->formal->next->next->next->next == NULL);
1355 isym->resolve.f4 (&e, &argexpr, NULL, NULL, NULL);
1360 if (gfc_option.flag_f2c
1361 && ((e.ts.type == BT_REAL && e.ts.kind == gfc_default_real_kind)
1362 || e.ts.type == BT_COMPLEX))
1364 /* Specific which needs a different implementation if f2c
1365 calling conventions are used. */
1366 sprintf (s, "_gfortran_f2c_specific%s", e.value.function.name);
1369 sprintf (s, "_gfortran_specific%s", e.value.function.name);
1371 name = get_identifier (s);
1372 mangled_name = name;
1376 name = gfc_sym_identifier (sym);
1377 mangled_name = gfc_sym_mangled_function_id (sym);
1380 type = gfc_get_function_type (sym);
1381 fndecl = build_decl (FUNCTION_DECL, name, type);
1383 SET_DECL_ASSEMBLER_NAME (fndecl, mangled_name);
1384 /* If the return type is a pointer, avoid alias issues by setting
1385 DECL_IS_MALLOC to nonzero. This means that the function should be
1386 treated as if it were a malloc, meaning it returns a pointer that
1388 if (POINTER_TYPE_P (type))
1389 DECL_IS_MALLOC (fndecl) = 1;
1391 /* Set the context of this decl. */
1392 if (0 && sym->ns && sym->ns->proc_name)
1394 /* TODO: Add external decls to the appropriate scope. */
1395 DECL_CONTEXT (fndecl) = sym->ns->proc_name->backend_decl;
1399 /* Global declaration, e.g. intrinsic subroutine. */
1400 DECL_CONTEXT (fndecl) = NULL_TREE;
1403 DECL_EXTERNAL (fndecl) = 1;
1405 /* This specifies if a function is globally addressable, i.e. it is
1406 the opposite of declaring static in C. */
1407 TREE_PUBLIC (fndecl) = 1;
1409 /* Set attributes for PURE functions. A call to PURE function in the
1410 Fortran 95 sense is both pure and without side effects in the C
1412 if (sym->attr.pure || sym->attr.elemental)
1414 if (sym->attr.function && !gfc_return_by_reference (sym))
1415 DECL_PURE_P (fndecl) = 1;
1416 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
1417 parameters and don't use alternate returns (is this
1418 allowed?). In that case, calls to them are meaningless, and
1419 can be optimized away. See also in build_function_decl(). */
1420 TREE_SIDE_EFFECTS (fndecl) = 0;
1423 /* Mark non-returning functions. */
1424 if (sym->attr.noreturn)
1425 TREE_THIS_VOLATILE(fndecl) = 1;
1427 sym->backend_decl = fndecl;
1429 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1430 pushdecl_top_level (fndecl);
1436 /* Create a declaration for a procedure. For external functions (in the C
1437 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
1438 a master function with alternate entry points. */
1441 build_function_decl (gfc_symbol * sym)
1444 symbol_attribute attr;
1446 gfc_formal_arglist *f;
1448 gcc_assert (!sym->backend_decl);
1449 gcc_assert (!sym->attr.external);
1451 /* Set the line and filename. sym->declared_at seems to point to the
1452 last statement for subroutines, but it'll do for now. */
1453 gfc_set_backend_locus (&sym->declared_at);
1455 /* Allow only one nesting level. Allow public declarations. */
1456 gcc_assert (current_function_decl == NULL_TREE
1457 || DECL_CONTEXT (current_function_decl) == NULL_TREE
1458 || TREE_CODE (DECL_CONTEXT (current_function_decl))
1461 type = gfc_get_function_type (sym);
1462 fndecl = build_decl (FUNCTION_DECL, gfc_sym_identifier (sym), type);
1464 /* Perform name mangling if this is a top level or module procedure. */
1465 if (current_function_decl == NULL_TREE)
1466 SET_DECL_ASSEMBLER_NAME (fndecl, gfc_sym_mangled_function_id (sym));
1468 /* Figure out the return type of the declared function, and build a
1469 RESULT_DECL for it. If this is a subroutine with alternate
1470 returns, build a RESULT_DECL for it. */
1473 result_decl = NULL_TREE;
1474 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
1477 if (gfc_return_by_reference (sym))
1478 type = void_type_node;
1481 if (sym->result != sym)
1482 result_decl = gfc_sym_identifier (sym->result);
1484 type = TREE_TYPE (TREE_TYPE (fndecl));
1489 /* Look for alternate return placeholders. */
1490 int has_alternate_returns = 0;
1491 for (f = sym->formal; f; f = f->next)
1495 has_alternate_returns = 1;
1500 if (has_alternate_returns)
1501 type = integer_type_node;
1503 type = void_type_node;
1506 result_decl = build_decl (RESULT_DECL, result_decl, type);
1507 DECL_ARTIFICIAL (result_decl) = 1;
1508 DECL_IGNORED_P (result_decl) = 1;
1509 DECL_CONTEXT (result_decl) = fndecl;
1510 DECL_RESULT (fndecl) = result_decl;
1512 /* Don't call layout_decl for a RESULT_DECL.
1513 layout_decl (result_decl, 0); */
1515 /* If the return type is a pointer, avoid alias issues by setting
1516 DECL_IS_MALLOC to nonzero. This means that the function should be
1517 treated as if it were a malloc, meaning it returns a pointer that
1519 if (POINTER_TYPE_P (type))
1520 DECL_IS_MALLOC (fndecl) = 1;
1522 /* Set up all attributes for the function. */
1523 DECL_CONTEXT (fndecl) = current_function_decl;
1524 DECL_EXTERNAL (fndecl) = 0;
1526 /* This specifies if a function is globally visible, i.e. it is
1527 the opposite of declaring static in C. */
1528 if (DECL_CONTEXT (fndecl) == NULL_TREE
1529 && !sym->attr.entry_master && !sym->attr.is_main_program)
1530 TREE_PUBLIC (fndecl) = 1;
1532 /* TREE_STATIC means the function body is defined here. */
1533 TREE_STATIC (fndecl) = 1;
1535 /* Set attributes for PURE functions. A call to a PURE function in the
1536 Fortran 95 sense is both pure and without side effects in the C
1538 if (attr.pure || attr.elemental)
1540 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
1541 including an alternate return. In that case it can also be
1542 marked as PURE. See also in gfc_get_extern_function_decl(). */
1543 if (attr.function && !gfc_return_by_reference (sym))
1544 DECL_PURE_P (fndecl) = 1;
1545 TREE_SIDE_EFFECTS (fndecl) = 0;
1548 /* Layout the function declaration and put it in the binding level
1549 of the current function. */
1552 sym->backend_decl = fndecl;
1556 /* Create the DECL_ARGUMENTS for a procedure. */
1559 create_function_arglist (gfc_symbol * sym)
1562 gfc_formal_arglist *f;
1563 tree typelist, hidden_typelist;
1564 tree arglist, hidden_arglist;
1568 fndecl = sym->backend_decl;
1570 /* Build formal argument list. Make sure that their TREE_CONTEXT is
1571 the new FUNCTION_DECL node. */
1572 arglist = NULL_TREE;
1573 hidden_arglist = NULL_TREE;
1574 typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1576 if (sym->attr.entry_master)
1578 type = TREE_VALUE (typelist);
1579 parm = build_decl (PARM_DECL, get_identifier ("__entry"), type);
1581 DECL_CONTEXT (parm) = fndecl;
1582 DECL_ARG_TYPE (parm) = type;
1583 TREE_READONLY (parm) = 1;
1584 gfc_finish_decl (parm);
1585 DECL_ARTIFICIAL (parm) = 1;
1587 arglist = chainon (arglist, parm);
1588 typelist = TREE_CHAIN (typelist);
1591 if (gfc_return_by_reference (sym))
1593 tree type = TREE_VALUE (typelist), length = NULL;
1595 if (sym->ts.type == BT_CHARACTER)
1597 /* Length of character result. */
1598 tree len_type = TREE_VALUE (TREE_CHAIN (typelist));
1599 gcc_assert (len_type == gfc_charlen_type_node);
1601 length = build_decl (PARM_DECL,
1602 get_identifier (".__result"),
1604 if (!sym->ts.cl->length)
1606 sym->ts.cl->backend_decl = length;
1607 TREE_USED (length) = 1;
1609 gcc_assert (TREE_CODE (length) == PARM_DECL);
1610 DECL_CONTEXT (length) = fndecl;
1611 DECL_ARG_TYPE (length) = len_type;
1612 TREE_READONLY (length) = 1;
1613 DECL_ARTIFICIAL (length) = 1;
1614 gfc_finish_decl (length);
1615 if (sym->ts.cl->backend_decl == NULL
1616 || sym->ts.cl->backend_decl == length)
1621 if (sym->ts.cl->backend_decl == NULL)
1623 tree len = build_decl (VAR_DECL,
1624 get_identifier ("..__result"),
1625 gfc_charlen_type_node);
1626 DECL_ARTIFICIAL (len) = 1;
1627 TREE_USED (len) = 1;
1628 sym->ts.cl->backend_decl = len;
1631 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1632 arg = sym->result ? sym->result : sym;
1633 backend_decl = arg->backend_decl;
1634 /* Temporary clear it, so that gfc_sym_type creates complete
1636 arg->backend_decl = NULL;
1637 type = gfc_sym_type (arg);
1638 arg->backend_decl = backend_decl;
1639 type = build_reference_type (type);
1643 parm = build_decl (PARM_DECL, get_identifier ("__result"), type);
1645 DECL_CONTEXT (parm) = fndecl;
1646 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
1647 TREE_READONLY (parm) = 1;
1648 DECL_ARTIFICIAL (parm) = 1;
1649 gfc_finish_decl (parm);
1651 arglist = chainon (arglist, parm);
1652 typelist = TREE_CHAIN (typelist);
1654 if (sym->ts.type == BT_CHARACTER)
1656 gfc_allocate_lang_decl (parm);
1657 arglist = chainon (arglist, length);
1658 typelist = TREE_CHAIN (typelist);
1662 hidden_typelist = typelist;
1663 for (f = sym->formal; f; f = f->next)
1664 if (f->sym != NULL) /* Ignore alternate returns. */
1665 hidden_typelist = TREE_CHAIN (hidden_typelist);
1667 for (f = sym->formal; f; f = f->next)
1669 char name[GFC_MAX_SYMBOL_LEN + 2];
1671 /* Ignore alternate returns. */
1675 type = TREE_VALUE (typelist);
1677 if (f->sym->ts.type == BT_CHARACTER)
1679 tree len_type = TREE_VALUE (hidden_typelist);
1680 tree length = NULL_TREE;
1681 gcc_assert (len_type == gfc_charlen_type_node);
1683 strcpy (&name[1], f->sym->name);
1685 length = build_decl (PARM_DECL, get_identifier (name), len_type);
1687 hidden_arglist = chainon (hidden_arglist, length);
1688 DECL_CONTEXT (length) = fndecl;
1689 DECL_ARTIFICIAL (length) = 1;
1690 DECL_ARG_TYPE (length) = len_type;
1691 TREE_READONLY (length) = 1;
1692 gfc_finish_decl (length);
1694 /* Remember the passed value. */
1695 f->sym->ts.cl->passed_length = length;
1697 /* Use the passed value for assumed length variables. */
1698 if (!f->sym->ts.cl->length)
1700 TREE_USED (length) = 1;
1701 gcc_assert (!f->sym->ts.cl->backend_decl);
1702 f->sym->ts.cl->backend_decl = length;
1705 hidden_typelist = TREE_CHAIN (hidden_typelist);
1707 if (f->sym->ts.cl->backend_decl == NULL
1708 || f->sym->ts.cl->backend_decl == length)
1710 if (f->sym->ts.cl->backend_decl == NULL)
1711 gfc_create_string_length (f->sym);
1713 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1714 if (f->sym->attr.flavor == FL_PROCEDURE)
1715 type = build_pointer_type (gfc_get_function_type (f->sym));
1717 type = gfc_sym_type (f->sym);
1721 /* For non-constant length array arguments, make sure they use
1722 a different type node from TYPE_ARG_TYPES type. */
1723 if (f->sym->attr.dimension
1724 && type == TREE_VALUE (typelist)
1725 && TREE_CODE (type) == POINTER_TYPE
1726 && GFC_ARRAY_TYPE_P (type)
1727 && f->sym->as->type != AS_ASSUMED_SIZE
1728 && ! COMPLETE_TYPE_P (TREE_TYPE (type)))
1730 if (f->sym->attr.flavor == FL_PROCEDURE)
1731 type = build_pointer_type (gfc_get_function_type (f->sym));
1733 type = gfc_sym_type (f->sym);
1736 if (f->sym->attr.proc_pointer)
1737 type = build_pointer_type (type);
1739 /* Build the argument declaration. */
1740 parm = build_decl (PARM_DECL, gfc_sym_identifier (f->sym), type);
1742 /* Fill in arg stuff. */
1743 DECL_CONTEXT (parm) = fndecl;
1744 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
1745 /* All implementation args are read-only. */
1746 TREE_READONLY (parm) = 1;
1747 if (POINTER_TYPE_P (type)
1748 && (!f->sym->attr.proc_pointer
1749 && f->sym->attr.flavor != FL_PROCEDURE))
1750 DECL_BY_REFERENCE (parm) = 1;
1752 gfc_finish_decl (parm);
1754 f->sym->backend_decl = parm;
1756 arglist = chainon (arglist, parm);
1757 typelist = TREE_CHAIN (typelist);
1760 /* Add the hidden string length parameters, unless the procedure
1762 if (!sym->attr.is_bind_c)
1763 arglist = chainon (arglist, hidden_arglist);
1765 gcc_assert (hidden_typelist == NULL_TREE
1766 || TREE_VALUE (hidden_typelist) == void_type_node);
1767 DECL_ARGUMENTS (fndecl) = arglist;
1770 /* Convert FNDECL's code to GIMPLE and handle any nested functions. */
1773 gfc_gimplify_function (tree fndecl)
1775 struct cgraph_node *cgn;
1777 gimplify_function_tree (fndecl);
1778 dump_function (TDI_generic, fndecl);
1780 /* Generate errors for structured block violations. */
1781 /* ??? Could be done as part of resolve_labels. */
1783 diagnose_omp_structured_block_errors (fndecl);
1785 /* Convert all nested functions to GIMPLE now. We do things in this order
1786 so that items like VLA sizes are expanded properly in the context of the
1787 correct function. */
1788 cgn = cgraph_node (fndecl);
1789 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1790 gfc_gimplify_function (cgn->decl);
1794 /* Do the setup necessary before generating the body of a function. */
1797 trans_function_start (gfc_symbol * sym)
1801 fndecl = sym->backend_decl;
1803 /* Let GCC know the current scope is this function. */
1804 current_function_decl = fndecl;
1806 /* Let the world know what we're about to do. */
1807 announce_function (fndecl);
1809 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1811 /* Create RTL for function declaration. */
1812 rest_of_decl_compilation (fndecl, 1, 0);
1815 /* Create RTL for function definition. */
1816 make_decl_rtl (fndecl);
1818 init_function_start (fndecl);
1820 /* Even though we're inside a function body, we still don't want to
1821 call expand_expr to calculate the size of a variable-sized array.
1822 We haven't necessarily assigned RTL to all variables yet, so it's
1823 not safe to try to expand expressions involving them. */
1824 cfun->dont_save_pending_sizes_p = 1;
1826 /* function.c requires a push at the start of the function. */
1830 /* Create thunks for alternate entry points. */
1833 build_entry_thunks (gfc_namespace * ns)
1835 gfc_formal_arglist *formal;
1836 gfc_formal_arglist *thunk_formal;
1838 gfc_symbol *thunk_sym;
1846 /* This should always be a toplevel function. */
1847 gcc_assert (current_function_decl == NULL_TREE);
1849 gfc_get_backend_locus (&old_loc);
1850 for (el = ns->entries; el; el = el->next)
1852 thunk_sym = el->sym;
1854 build_function_decl (thunk_sym);
1855 create_function_arglist (thunk_sym);
1857 trans_function_start (thunk_sym);
1859 thunk_fndecl = thunk_sym->backend_decl;
1861 gfc_init_block (&body);
1863 /* Pass extra parameter identifying this entry point. */
1864 tmp = build_int_cst (gfc_array_index_type, el->id);
1865 args = tree_cons (NULL_TREE, tmp, NULL_TREE);
1866 string_args = NULL_TREE;
1868 if (thunk_sym->attr.function)
1870 if (gfc_return_by_reference (ns->proc_name))
1872 tree ref = DECL_ARGUMENTS (current_function_decl);
1873 args = tree_cons (NULL_TREE, ref, args);
1874 if (ns->proc_name->ts.type == BT_CHARACTER)
1875 args = tree_cons (NULL_TREE, TREE_CHAIN (ref),
1880 for (formal = ns->proc_name->formal; formal; formal = formal->next)
1882 /* Ignore alternate returns. */
1883 if (formal->sym == NULL)
1886 /* We don't have a clever way of identifying arguments, so resort to
1887 a brute-force search. */
1888 for (thunk_formal = thunk_sym->formal;
1890 thunk_formal = thunk_formal->next)
1892 if (thunk_formal->sym == formal->sym)
1898 /* Pass the argument. */
1899 DECL_ARTIFICIAL (thunk_formal->sym->backend_decl) = 1;
1900 args = tree_cons (NULL_TREE, thunk_formal->sym->backend_decl,
1902 if (formal->sym->ts.type == BT_CHARACTER)
1904 tmp = thunk_formal->sym->ts.cl->backend_decl;
1905 string_args = tree_cons (NULL_TREE, tmp, string_args);
1910 /* Pass NULL for a missing argument. */
1911 args = tree_cons (NULL_TREE, null_pointer_node, args);
1912 if (formal->sym->ts.type == BT_CHARACTER)
1914 tmp = build_int_cst (gfc_charlen_type_node, 0);
1915 string_args = tree_cons (NULL_TREE, tmp, string_args);
1920 /* Call the master function. */
1921 args = nreverse (args);
1922 args = chainon (args, nreverse (string_args));
1923 tmp = ns->proc_name->backend_decl;
1924 tmp = build_function_call_expr (tmp, args);
1925 if (ns->proc_name->attr.mixed_entry_master)
1927 tree union_decl, field;
1928 tree master_type = TREE_TYPE (ns->proc_name->backend_decl);
1930 union_decl = build_decl (VAR_DECL, get_identifier ("__result"),
1931 TREE_TYPE (master_type));
1932 DECL_ARTIFICIAL (union_decl) = 1;
1933 DECL_EXTERNAL (union_decl) = 0;
1934 TREE_PUBLIC (union_decl) = 0;
1935 TREE_USED (union_decl) = 1;
1936 layout_decl (union_decl, 0);
1937 pushdecl (union_decl);
1939 DECL_CONTEXT (union_decl) = current_function_decl;
1940 tmp = fold_build2 (MODIFY_EXPR, TREE_TYPE (union_decl),
1942 gfc_add_expr_to_block (&body, tmp);
1944 for (field = TYPE_FIELDS (TREE_TYPE (union_decl));
1945 field; field = TREE_CHAIN (field))
1946 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
1947 thunk_sym->result->name) == 0)
1949 gcc_assert (field != NULL_TREE);
1950 tmp = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
1951 union_decl, field, NULL_TREE);
1952 tmp = fold_build2 (MODIFY_EXPR,
1953 TREE_TYPE (DECL_RESULT (current_function_decl)),
1954 DECL_RESULT (current_function_decl), tmp);
1955 tmp = build1_v (RETURN_EXPR, tmp);
1957 else if (TREE_TYPE (DECL_RESULT (current_function_decl))
1960 tmp = fold_build2 (MODIFY_EXPR,
1961 TREE_TYPE (DECL_RESULT (current_function_decl)),
1962 DECL_RESULT (current_function_decl), tmp);
1963 tmp = build1_v (RETURN_EXPR, tmp);
1965 gfc_add_expr_to_block (&body, tmp);
1967 /* Finish off this function and send it for code generation. */
1968 DECL_SAVED_TREE (thunk_fndecl) = gfc_finish_block (&body);
1971 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl)) = thunk_fndecl;
1972 DECL_SAVED_TREE (thunk_fndecl)
1973 = build3_v (BIND_EXPR, tmp, DECL_SAVED_TREE (thunk_fndecl),
1974 DECL_INITIAL (thunk_fndecl));
1976 /* Output the GENERIC tree. */
1977 dump_function (TDI_original, thunk_fndecl);
1979 /* Store the end of the function, so that we get good line number
1980 info for the epilogue. */
1981 cfun->function_end_locus = input_location;
1983 /* We're leaving the context of this function, so zap cfun.
1984 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
1985 tree_rest_of_compilation. */
1988 current_function_decl = NULL_TREE;
1990 gfc_gimplify_function (thunk_fndecl);
1991 cgraph_finalize_function (thunk_fndecl, false);
1993 /* We share the symbols in the formal argument list with other entry
1994 points and the master function. Clear them so that they are
1995 recreated for each function. */
1996 for (formal = thunk_sym->formal; formal; formal = formal->next)
1997 if (formal->sym != NULL) /* Ignore alternate returns. */
1999 formal->sym->backend_decl = NULL_TREE;
2000 if (formal->sym->ts.type == BT_CHARACTER)
2001 formal->sym->ts.cl->backend_decl = NULL_TREE;
2004 if (thunk_sym->attr.function)
2006 if (thunk_sym->ts.type == BT_CHARACTER)
2007 thunk_sym->ts.cl->backend_decl = NULL_TREE;
2008 if (thunk_sym->result->ts.type == BT_CHARACTER)
2009 thunk_sym->result->ts.cl->backend_decl = NULL_TREE;
2013 gfc_set_backend_locus (&old_loc);
2017 /* Create a decl for a function, and create any thunks for alternate entry
2021 gfc_create_function_decl (gfc_namespace * ns)
2023 /* Create a declaration for the master function. */
2024 build_function_decl (ns->proc_name);
2026 /* Compile the entry thunks. */
2028 build_entry_thunks (ns);
2030 /* Now create the read argument list. */
2031 create_function_arglist (ns->proc_name);
2034 /* Return the decl used to hold the function return value. If
2035 parent_flag is set, the context is the parent_scope. */
2038 gfc_get_fake_result_decl (gfc_symbol * sym, int parent_flag)
2042 tree this_fake_result_decl;
2043 tree this_function_decl;
2045 char name[GFC_MAX_SYMBOL_LEN + 10];
2049 this_fake_result_decl = parent_fake_result_decl;
2050 this_function_decl = DECL_CONTEXT (current_function_decl);
2054 this_fake_result_decl = current_fake_result_decl;
2055 this_function_decl = current_function_decl;
2059 && sym->ns->proc_name->backend_decl == this_function_decl
2060 && sym->ns->proc_name->attr.entry_master
2061 && sym != sym->ns->proc_name)
2064 if (this_fake_result_decl != NULL)
2065 for (t = TREE_CHAIN (this_fake_result_decl); t; t = TREE_CHAIN (t))
2066 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t)), sym->name) == 0)
2069 return TREE_VALUE (t);
2070 decl = gfc_get_fake_result_decl (sym->ns->proc_name, parent_flag);
2073 this_fake_result_decl = parent_fake_result_decl;
2075 this_fake_result_decl = current_fake_result_decl;
2077 if (decl && sym->ns->proc_name->attr.mixed_entry_master)
2081 for (field = TYPE_FIELDS (TREE_TYPE (decl));
2082 field; field = TREE_CHAIN (field))
2083 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
2087 gcc_assert (field != NULL_TREE);
2088 decl = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
2089 decl, field, NULL_TREE);
2092 var = create_tmp_var_raw (TREE_TYPE (decl), sym->name);
2094 gfc_add_decl_to_parent_function (var);
2096 gfc_add_decl_to_function (var);
2098 SET_DECL_VALUE_EXPR (var, decl);
2099 DECL_HAS_VALUE_EXPR_P (var) = 1;
2100 GFC_DECL_RESULT (var) = 1;
2102 TREE_CHAIN (this_fake_result_decl)
2103 = tree_cons (get_identifier (sym->name), var,
2104 TREE_CHAIN (this_fake_result_decl));
2108 if (this_fake_result_decl != NULL_TREE)
2109 return TREE_VALUE (this_fake_result_decl);
2111 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
2116 if (sym->ts.type == BT_CHARACTER)
2118 if (sym->ts.cl->backend_decl == NULL_TREE)
2119 length = gfc_create_string_length (sym);
2121 length = sym->ts.cl->backend_decl;
2122 if (TREE_CODE (length) == VAR_DECL
2123 && DECL_CONTEXT (length) == NULL_TREE)
2124 gfc_add_decl_to_function (length);
2127 if (gfc_return_by_reference (sym))
2129 decl = DECL_ARGUMENTS (this_function_decl);
2131 if (sym->ns->proc_name->backend_decl == this_function_decl
2132 && sym->ns->proc_name->attr.entry_master)
2133 decl = TREE_CHAIN (decl);
2135 TREE_USED (decl) = 1;
2137 decl = gfc_build_dummy_array_decl (sym, decl);
2141 sprintf (name, "__result_%.20s",
2142 IDENTIFIER_POINTER (DECL_NAME (this_function_decl)));
2144 if (!sym->attr.mixed_entry_master && sym->attr.function)
2145 decl = build_decl (VAR_DECL, get_identifier (name),
2146 gfc_sym_type (sym));
2148 decl = build_decl (VAR_DECL, get_identifier (name),
2149 TREE_TYPE (TREE_TYPE (this_function_decl)));
2150 DECL_ARTIFICIAL (decl) = 1;
2151 DECL_EXTERNAL (decl) = 0;
2152 TREE_PUBLIC (decl) = 0;
2153 TREE_USED (decl) = 1;
2154 GFC_DECL_RESULT (decl) = 1;
2155 TREE_ADDRESSABLE (decl) = 1;
2157 layout_decl (decl, 0);
2160 gfc_add_decl_to_parent_function (decl);
2162 gfc_add_decl_to_function (decl);
2166 parent_fake_result_decl = build_tree_list (NULL, decl);
2168 current_fake_result_decl = build_tree_list (NULL, decl);
2174 /* Builds a function decl. The remaining parameters are the types of the
2175 function arguments. Negative nargs indicates a varargs function. */
2178 gfc_build_library_function_decl (tree name, tree rettype, int nargs, ...)
2187 /* Library functions must be declared with global scope. */
2188 gcc_assert (current_function_decl == NULL_TREE);
2190 va_start (p, nargs);
2193 /* Create a list of the argument types. */
2194 for (arglist = NULL_TREE, n = abs (nargs); n > 0; n--)
2196 argtype = va_arg (p, tree);
2197 arglist = gfc_chainon_list (arglist, argtype);
2202 /* Terminate the list. */
2203 arglist = gfc_chainon_list (arglist, void_type_node);
2206 /* Build the function type and decl. */
2207 fntype = build_function_type (rettype, arglist);
2208 fndecl = build_decl (FUNCTION_DECL, name, fntype);
2210 /* Mark this decl as external. */
2211 DECL_EXTERNAL (fndecl) = 1;
2212 TREE_PUBLIC (fndecl) = 1;
2218 rest_of_decl_compilation (fndecl, 1, 0);
2224 gfc_build_intrinsic_function_decls (void)
2226 tree gfc_int4_type_node = gfc_get_int_type (4);
2227 tree gfc_int8_type_node = gfc_get_int_type (8);
2228 tree gfc_int16_type_node = gfc_get_int_type (16);
2229 tree gfc_logical4_type_node = gfc_get_logical_type (4);
2230 tree pchar1_type_node = gfc_get_pchar_type (1);
2231 tree pchar4_type_node = gfc_get_pchar_type (4);
2233 /* String functions. */
2234 gfor_fndecl_compare_string =
2235 gfc_build_library_function_decl (get_identifier (PREFIX("compare_string")),
2236 integer_type_node, 4,
2237 gfc_charlen_type_node, pchar1_type_node,
2238 gfc_charlen_type_node, pchar1_type_node);
2240 gfor_fndecl_concat_string =
2241 gfc_build_library_function_decl (get_identifier (PREFIX("concat_string")),
2243 gfc_charlen_type_node, pchar1_type_node,
2244 gfc_charlen_type_node, pchar1_type_node,
2245 gfc_charlen_type_node, pchar1_type_node);
2247 gfor_fndecl_string_len_trim =
2248 gfc_build_library_function_decl (get_identifier (PREFIX("string_len_trim")),
2249 gfc_int4_type_node, 2,
2250 gfc_charlen_type_node, pchar1_type_node);
2252 gfor_fndecl_string_index =
2253 gfc_build_library_function_decl (get_identifier (PREFIX("string_index")),
2254 gfc_int4_type_node, 5,
2255 gfc_charlen_type_node, pchar1_type_node,
2256 gfc_charlen_type_node, pchar1_type_node,
2257 gfc_logical4_type_node);
2259 gfor_fndecl_string_scan =
2260 gfc_build_library_function_decl (get_identifier (PREFIX("string_scan")),
2261 gfc_int4_type_node, 5,
2262 gfc_charlen_type_node, pchar1_type_node,
2263 gfc_charlen_type_node, pchar1_type_node,
2264 gfc_logical4_type_node);
2266 gfor_fndecl_string_verify =
2267 gfc_build_library_function_decl (get_identifier (PREFIX("string_verify")),
2268 gfc_int4_type_node, 5,
2269 gfc_charlen_type_node, pchar1_type_node,
2270 gfc_charlen_type_node, pchar1_type_node,
2271 gfc_logical4_type_node);
2273 gfor_fndecl_string_trim =
2274 gfc_build_library_function_decl (get_identifier (PREFIX("string_trim")),
2276 build_pointer_type (gfc_charlen_type_node),
2277 build_pointer_type (pchar1_type_node),
2278 gfc_charlen_type_node, pchar1_type_node);
2280 gfor_fndecl_string_minmax =
2281 gfc_build_library_function_decl (get_identifier (PREFIX("string_minmax")),
2283 build_pointer_type (gfc_charlen_type_node),
2284 build_pointer_type (pchar1_type_node),
2285 integer_type_node, integer_type_node);
2287 gfor_fndecl_adjustl =
2288 gfc_build_library_function_decl (get_identifier (PREFIX("adjustl")),
2289 void_type_node, 3, pchar1_type_node,
2290 gfc_charlen_type_node, pchar1_type_node);
2292 gfor_fndecl_adjustr =
2293 gfc_build_library_function_decl (get_identifier (PREFIX("adjustr")),
2294 void_type_node, 3, pchar1_type_node,
2295 gfc_charlen_type_node, pchar1_type_node);
2297 gfor_fndecl_select_string =
2298 gfc_build_library_function_decl (get_identifier (PREFIX("select_string")),
2299 integer_type_node, 4, pvoid_type_node,
2300 integer_type_node, pchar1_type_node,
2301 gfc_charlen_type_node);
2303 gfor_fndecl_compare_string_char4 =
2304 gfc_build_library_function_decl (get_identifier
2305 (PREFIX("compare_string_char4")),
2306 integer_type_node, 4,
2307 gfc_charlen_type_node, pchar4_type_node,
2308 gfc_charlen_type_node, pchar4_type_node);
2310 gfor_fndecl_concat_string_char4 =
2311 gfc_build_library_function_decl (get_identifier
2312 (PREFIX("concat_string_char4")),
2314 gfc_charlen_type_node, pchar4_type_node,
2315 gfc_charlen_type_node, pchar4_type_node,
2316 gfc_charlen_type_node, pchar4_type_node);
2318 gfor_fndecl_string_len_trim_char4 =
2319 gfc_build_library_function_decl (get_identifier
2320 (PREFIX("string_len_trim_char4")),
2321 gfc_charlen_type_node, 2,
2322 gfc_charlen_type_node, pchar4_type_node);
2324 gfor_fndecl_string_index_char4 =
2325 gfc_build_library_function_decl (get_identifier
2326 (PREFIX("string_index_char4")),
2327 gfc_charlen_type_node, 5,
2328 gfc_charlen_type_node, pchar4_type_node,
2329 gfc_charlen_type_node, pchar4_type_node,
2330 gfc_logical4_type_node);
2332 gfor_fndecl_string_scan_char4 =
2333 gfc_build_library_function_decl (get_identifier
2334 (PREFIX("string_scan_char4")),
2335 gfc_charlen_type_node, 5,
2336 gfc_charlen_type_node, pchar4_type_node,
2337 gfc_charlen_type_node, pchar4_type_node,
2338 gfc_logical4_type_node);
2340 gfor_fndecl_string_verify_char4 =
2341 gfc_build_library_function_decl (get_identifier
2342 (PREFIX("string_verify_char4")),
2343 gfc_charlen_type_node, 5,
2344 gfc_charlen_type_node, pchar4_type_node,
2345 gfc_charlen_type_node, pchar4_type_node,
2346 gfc_logical4_type_node);
2348 gfor_fndecl_string_trim_char4 =
2349 gfc_build_library_function_decl (get_identifier
2350 (PREFIX("string_trim_char4")),
2352 build_pointer_type (gfc_charlen_type_node),
2353 build_pointer_type (pchar4_type_node),
2354 gfc_charlen_type_node, pchar4_type_node);
2356 gfor_fndecl_string_minmax_char4 =
2357 gfc_build_library_function_decl (get_identifier
2358 (PREFIX("string_minmax_char4")),
2360 build_pointer_type (gfc_charlen_type_node),
2361 build_pointer_type (pchar4_type_node),
2362 integer_type_node, integer_type_node);
2364 gfor_fndecl_adjustl_char4 =
2365 gfc_build_library_function_decl (get_identifier (PREFIX("adjustl_char4")),
2366 void_type_node, 3, pchar4_type_node,
2367 gfc_charlen_type_node, pchar4_type_node);
2369 gfor_fndecl_adjustr_char4 =
2370 gfc_build_library_function_decl (get_identifier (PREFIX("adjustr_char4")),
2371 void_type_node, 3, pchar4_type_node,
2372 gfc_charlen_type_node, pchar4_type_node);
2374 gfor_fndecl_select_string_char4 =
2375 gfc_build_library_function_decl (get_identifier
2376 (PREFIX("select_string_char4")),
2377 integer_type_node, 4, pvoid_type_node,
2378 integer_type_node, pvoid_type_node,
2379 gfc_charlen_type_node);
2382 /* Conversion between character kinds. */
2384 gfor_fndecl_convert_char1_to_char4 =
2385 gfc_build_library_function_decl (get_identifier
2386 (PREFIX("convert_char1_to_char4")),
2388 build_pointer_type (pchar4_type_node),
2389 gfc_charlen_type_node, pchar1_type_node);
2391 gfor_fndecl_convert_char4_to_char1 =
2392 gfc_build_library_function_decl (get_identifier
2393 (PREFIX("convert_char4_to_char1")),
2395 build_pointer_type (pchar1_type_node),
2396 gfc_charlen_type_node, pchar4_type_node);
2398 /* Misc. functions. */
2400 gfor_fndecl_ttynam =
2401 gfc_build_library_function_decl (get_identifier (PREFIX("ttynam")),
2405 gfc_charlen_type_node,
2409 gfc_build_library_function_decl (get_identifier (PREFIX("fdate")),
2413 gfc_charlen_type_node);
2416 gfc_build_library_function_decl (get_identifier (PREFIX("ctime")),
2420 gfc_charlen_type_node,
2421 gfc_int8_type_node);
2423 gfor_fndecl_sc_kind =
2424 gfc_build_library_function_decl (get_identifier
2425 (PREFIX("selected_char_kind")),
2426 gfc_int4_type_node, 2,
2427 gfc_charlen_type_node, pchar_type_node);
2429 gfor_fndecl_si_kind =
2430 gfc_build_library_function_decl (get_identifier
2431 (PREFIX("selected_int_kind")),
2432 gfc_int4_type_node, 1, pvoid_type_node);
2434 gfor_fndecl_sr_kind =
2435 gfc_build_library_function_decl (get_identifier
2436 (PREFIX("selected_real_kind")),
2437 gfc_int4_type_node, 2,
2438 pvoid_type_node, pvoid_type_node);
2440 /* Power functions. */
2442 tree ctype, rtype, itype, jtype;
2443 int rkind, ikind, jkind;
2446 static int ikinds[NIKINDS] = {4, 8, 16};
2447 static int rkinds[NRKINDS] = {4, 8, 10, 16};
2448 char name[PREFIX_LEN + 12]; /* _gfortran_pow_?n_?n */
2450 for (ikind=0; ikind < NIKINDS; ikind++)
2452 itype = gfc_get_int_type (ikinds[ikind]);
2454 for (jkind=0; jkind < NIKINDS; jkind++)
2456 jtype = gfc_get_int_type (ikinds[jkind]);
2459 sprintf(name, PREFIX("pow_i%d_i%d"), ikinds[ikind],
2461 gfor_fndecl_math_powi[jkind][ikind].integer =
2462 gfc_build_library_function_decl (get_identifier (name),
2463 jtype, 2, jtype, itype);
2464 TREE_READONLY (gfor_fndecl_math_powi[jkind][ikind].integer) = 1;
2468 for (rkind = 0; rkind < NRKINDS; rkind ++)
2470 rtype = gfc_get_real_type (rkinds[rkind]);
2473 sprintf(name, PREFIX("pow_r%d_i%d"), rkinds[rkind],
2475 gfor_fndecl_math_powi[rkind][ikind].real =
2476 gfc_build_library_function_decl (get_identifier (name),
2477 rtype, 2, rtype, itype);
2478 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].real) = 1;
2481 ctype = gfc_get_complex_type (rkinds[rkind]);
2484 sprintf(name, PREFIX("pow_c%d_i%d"), rkinds[rkind],
2486 gfor_fndecl_math_powi[rkind][ikind].cmplx =
2487 gfc_build_library_function_decl (get_identifier (name),
2488 ctype, 2,ctype, itype);
2489 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1;
2497 gfor_fndecl_math_ishftc4 =
2498 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc4")),
2500 3, gfc_int4_type_node,
2501 gfc_int4_type_node, gfc_int4_type_node);
2502 gfor_fndecl_math_ishftc8 =
2503 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc8")),
2505 3, gfc_int8_type_node,
2506 gfc_int4_type_node, gfc_int4_type_node);
2507 if (gfc_int16_type_node)
2508 gfor_fndecl_math_ishftc16 =
2509 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc16")),
2510 gfc_int16_type_node, 3,
2511 gfc_int16_type_node,
2513 gfc_int4_type_node);
2515 /* BLAS functions. */
2517 tree pint = build_pointer_type (integer_type_node);
2518 tree ps = build_pointer_type (gfc_get_real_type (gfc_default_real_kind));
2519 tree pd = build_pointer_type (gfc_get_real_type (gfc_default_double_kind));
2520 tree pc = build_pointer_type (gfc_get_complex_type (gfc_default_real_kind));
2521 tree pz = build_pointer_type
2522 (gfc_get_complex_type (gfc_default_double_kind));
2524 gfor_fndecl_sgemm = gfc_build_library_function_decl
2526 (gfc_option.flag_underscoring ? "sgemm_"
2528 void_type_node, 15, pchar_type_node,
2529 pchar_type_node, pint, pint, pint, ps, ps, pint,
2530 ps, pint, ps, ps, pint, integer_type_node,
2532 gfor_fndecl_dgemm = gfc_build_library_function_decl
2534 (gfc_option.flag_underscoring ? "dgemm_"
2536 void_type_node, 15, pchar_type_node,
2537 pchar_type_node, pint, pint, pint, pd, pd, pint,
2538 pd, pint, pd, pd, pint, integer_type_node,
2540 gfor_fndecl_cgemm = gfc_build_library_function_decl
2542 (gfc_option.flag_underscoring ? "cgemm_"
2544 void_type_node, 15, pchar_type_node,
2545 pchar_type_node, pint, pint, pint, pc, pc, pint,
2546 pc, pint, pc, pc, pint, integer_type_node,
2548 gfor_fndecl_zgemm = gfc_build_library_function_decl
2550 (gfc_option.flag_underscoring ? "zgemm_"
2552 void_type_node, 15, pchar_type_node,
2553 pchar_type_node, pint, pint, pint, pz, pz, pint,
2554 pz, pint, pz, pz, pint, integer_type_node,
2558 /* Other functions. */
2560 gfc_build_library_function_decl (get_identifier (PREFIX("size0")),
2561 gfc_array_index_type,
2562 1, pvoid_type_node);
2564 gfc_build_library_function_decl (get_identifier (PREFIX("size1")),
2565 gfc_array_index_type,
2567 gfc_array_index_type);
2570 gfc_build_library_function_decl (get_identifier (PREFIX ("iargc")),
2576 /* Make prototypes for runtime library functions. */
2579 gfc_build_builtin_function_decls (void)
2581 tree gfc_int4_type_node = gfc_get_int_type (4);
2583 gfor_fndecl_stop_numeric =
2584 gfc_build_library_function_decl (get_identifier (PREFIX("stop_numeric")),
2585 void_type_node, 1, gfc_int4_type_node);
2586 /* Stop doesn't return. */
2587 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric) = 1;
2589 gfor_fndecl_stop_string =
2590 gfc_build_library_function_decl (get_identifier (PREFIX("stop_string")),
2591 void_type_node, 2, pchar_type_node,
2592 gfc_int4_type_node);
2593 /* Stop doesn't return. */
2594 TREE_THIS_VOLATILE (gfor_fndecl_stop_string) = 1;
2596 gfor_fndecl_pause_numeric =
2597 gfc_build_library_function_decl (get_identifier (PREFIX("pause_numeric")),
2598 void_type_node, 1, gfc_int4_type_node);
2600 gfor_fndecl_pause_string =
2601 gfc_build_library_function_decl (get_identifier (PREFIX("pause_string")),
2602 void_type_node, 2, pchar_type_node,
2603 gfc_int4_type_node);
2605 gfor_fndecl_runtime_error =
2606 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error")),
2607 void_type_node, -1, pchar_type_node);
2608 /* The runtime_error function does not return. */
2609 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error) = 1;
2611 gfor_fndecl_runtime_error_at =
2612 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error_at")),
2613 void_type_node, -2, pchar_type_node,
2615 /* The runtime_error_at function does not return. */
2616 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at) = 1;
2618 gfor_fndecl_runtime_warning_at =
2619 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_warning_at")),
2620 void_type_node, -2, pchar_type_node,
2622 gfor_fndecl_generate_error =
2623 gfc_build_library_function_decl (get_identifier (PREFIX("generate_error")),
2624 void_type_node, 3, pvoid_type_node,
2625 integer_type_node, pchar_type_node);
2627 gfor_fndecl_os_error =
2628 gfc_build_library_function_decl (get_identifier (PREFIX("os_error")),
2629 void_type_node, 1, pchar_type_node);
2630 /* The runtime_error function does not return. */
2631 TREE_THIS_VOLATILE (gfor_fndecl_os_error) = 1;
2633 gfor_fndecl_set_args =
2634 gfc_build_library_function_decl (get_identifier (PREFIX("set_args")),
2635 void_type_node, 2, integer_type_node,
2636 build_pointer_type (pchar_type_node));
2638 gfor_fndecl_set_fpe =
2639 gfc_build_library_function_decl (get_identifier (PREFIX("set_fpe")),
2640 void_type_node, 1, integer_type_node);
2642 /* Keep the array dimension in sync with the call, later in this file. */
2643 gfor_fndecl_set_options =
2644 gfc_build_library_function_decl (get_identifier (PREFIX("set_options")),
2645 void_type_node, 2, integer_type_node,
2646 build_pointer_type (integer_type_node));
2648 gfor_fndecl_set_convert =
2649 gfc_build_library_function_decl (get_identifier (PREFIX("set_convert")),
2650 void_type_node, 1, integer_type_node);
2652 gfor_fndecl_set_record_marker =
2653 gfc_build_library_function_decl (get_identifier (PREFIX("set_record_marker")),
2654 void_type_node, 1, integer_type_node);
2656 gfor_fndecl_set_max_subrecord_length =
2657 gfc_build_library_function_decl (get_identifier (PREFIX("set_max_subrecord_length")),
2658 void_type_node, 1, integer_type_node);
2660 gfor_fndecl_in_pack = gfc_build_library_function_decl (
2661 get_identifier (PREFIX("internal_pack")),
2662 pvoid_type_node, 1, pvoid_type_node);
2664 gfor_fndecl_in_unpack = gfc_build_library_function_decl (
2665 get_identifier (PREFIX("internal_unpack")),
2666 void_type_node, 2, pvoid_type_node, pvoid_type_node);
2668 gfor_fndecl_associated =
2669 gfc_build_library_function_decl (
2670 get_identifier (PREFIX("associated")),
2671 integer_type_node, 2, ppvoid_type_node,
2674 gfc_build_intrinsic_function_decls ();
2675 gfc_build_intrinsic_lib_fndecls ();
2676 gfc_build_io_library_fndecls ();
2680 /* Evaluate the length of dummy character variables. */
2683 gfc_trans_dummy_character (gfc_symbol *sym, gfc_charlen *cl, tree fnbody)
2687 gfc_finish_decl (cl->backend_decl);
2689 gfc_start_block (&body);
2691 /* Evaluate the string length expression. */
2692 gfc_conv_string_length (cl, NULL, &body);
2694 gfc_trans_vla_type_sizes (sym, &body);
2696 gfc_add_expr_to_block (&body, fnbody);
2697 return gfc_finish_block (&body);
2701 /* Allocate and cleanup an automatic character variable. */
2704 gfc_trans_auto_character_variable (gfc_symbol * sym, tree fnbody)
2710 gcc_assert (sym->backend_decl);
2711 gcc_assert (sym->ts.cl && sym->ts.cl->length);
2713 gfc_start_block (&body);
2715 /* Evaluate the string length expression. */
2716 gfc_conv_string_length (sym->ts.cl, NULL, &body);
2718 gfc_trans_vla_type_sizes (sym, &body);
2720 decl = sym->backend_decl;
2722 /* Emit a DECL_EXPR for this variable, which will cause the
2723 gimplifier to allocate storage, and all that good stuff. */
2724 tmp = fold_build1 (DECL_EXPR, TREE_TYPE (decl), decl);
2725 gfc_add_expr_to_block (&body, tmp);
2727 gfc_add_expr_to_block (&body, fnbody);
2728 return gfc_finish_block (&body);
2731 /* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
2734 gfc_trans_assign_aux_var (gfc_symbol * sym, tree fnbody)
2738 gcc_assert (sym->backend_decl);
2739 gfc_start_block (&body);
2741 /* Set the initial value to length. See the comments in
2742 function gfc_add_assign_aux_vars in this file. */
2743 gfc_add_modify (&body, GFC_DECL_STRING_LEN (sym->backend_decl),
2744 build_int_cst (NULL_TREE, -2));
2746 gfc_add_expr_to_block (&body, fnbody);
2747 return gfc_finish_block (&body);
2751 gfc_trans_vla_one_sizepos (tree *tp, stmtblock_t *body)
2753 tree t = *tp, var, val;
2755 if (t == NULL || t == error_mark_node)
2757 if (TREE_CONSTANT (t) || DECL_P (t))
2760 if (TREE_CODE (t) == SAVE_EXPR)
2762 if (SAVE_EXPR_RESOLVED_P (t))
2764 *tp = TREE_OPERAND (t, 0);
2767 val = TREE_OPERAND (t, 0);
2772 var = gfc_create_var_np (TREE_TYPE (t), NULL);
2773 gfc_add_decl_to_function (var);
2774 gfc_add_modify (body, var, val);
2775 if (TREE_CODE (t) == SAVE_EXPR)
2776 TREE_OPERAND (t, 0) = var;
2781 gfc_trans_vla_type_sizes_1 (tree type, stmtblock_t *body)
2785 if (type == NULL || type == error_mark_node)
2788 type = TYPE_MAIN_VARIANT (type);
2790 if (TREE_CODE (type) == INTEGER_TYPE)
2792 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type), body);
2793 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type), body);
2795 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2797 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
2798 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
2801 else if (TREE_CODE (type) == ARRAY_TYPE)
2803 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type), body);
2804 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type), body);
2805 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type), body);
2806 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type), body);
2808 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2810 TYPE_SIZE (t) = TYPE_SIZE (type);
2811 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
2816 /* Make sure all type sizes and array domains are either constant,
2817 or variable or parameter decls. This is a simplified variant
2818 of gimplify_type_sizes, but we can't use it here, as none of the
2819 variables in the expressions have been gimplified yet.
2820 As type sizes and domains for various variable length arrays
2821 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
2822 time, without this routine gimplify_type_sizes in the middle-end
2823 could result in the type sizes being gimplified earlier than where
2824 those variables are initialized. */
2827 gfc_trans_vla_type_sizes (gfc_symbol *sym, stmtblock_t *body)
2829 tree type = TREE_TYPE (sym->backend_decl);
2831 if (TREE_CODE (type) == FUNCTION_TYPE
2832 && (sym->attr.function || sym->attr.result || sym->attr.entry))
2834 if (! current_fake_result_decl)
2837 type = TREE_TYPE (TREE_VALUE (current_fake_result_decl));
2840 while (POINTER_TYPE_P (type))
2841 type = TREE_TYPE (type);
2843 if (GFC_DESCRIPTOR_TYPE_P (type))
2845 tree etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
2847 while (POINTER_TYPE_P (etype))
2848 etype = TREE_TYPE (etype);
2850 gfc_trans_vla_type_sizes_1 (etype, body);
2853 gfc_trans_vla_type_sizes_1 (type, body);
2857 /* Initialize a derived type by building an lvalue from the symbol
2858 and using trans_assignment to do the work. */
2860 gfc_init_default_dt (gfc_symbol * sym, tree body)
2862 stmtblock_t fnblock;
2867 gfc_init_block (&fnblock);
2868 gcc_assert (!sym->attr.allocatable);
2869 gfc_set_sym_referenced (sym);
2870 e = gfc_lval_expr_from_sym (sym);
2871 tmp = gfc_trans_assignment (e, sym->value, false);
2872 if (sym->attr.dummy)
2874 present = gfc_conv_expr_present (sym);
2875 tmp = build3 (COND_EXPR, TREE_TYPE (tmp), present,
2876 tmp, build_empty_stmt ());
2878 gfc_add_expr_to_block (&fnblock, tmp);
2881 gfc_add_expr_to_block (&fnblock, body);
2882 return gfc_finish_block (&fnblock);
2886 /* Initialize INTENT(OUT) derived type dummies. As well as giving
2887 them their default initializer, if they do not have allocatable
2888 components, they have their allocatable components deallocated. */
2891 init_intent_out_dt (gfc_symbol * proc_sym, tree body)
2893 stmtblock_t fnblock;
2894 gfc_formal_arglist *f;
2898 gfc_init_block (&fnblock);
2899 for (f = proc_sym->formal; f; f = f->next)
2900 if (f->sym && f->sym->attr.intent == INTENT_OUT
2901 && f->sym->ts.type == BT_DERIVED)
2903 if (f->sym->ts.derived->attr.alloc_comp)
2905 tmp = gfc_deallocate_alloc_comp (f->sym->ts.derived,
2906 f->sym->backend_decl,
2907 f->sym->as ? f->sym->as->rank : 0);
2909 present = gfc_conv_expr_present (f->sym);
2910 tmp = build3 (COND_EXPR, TREE_TYPE (tmp), present,
2911 tmp, build_empty_stmt ());
2913 gfc_add_expr_to_block (&fnblock, tmp);
2916 if (!f->sym->ts.derived->attr.alloc_comp
2918 body = gfc_init_default_dt (f->sym, body);
2921 gfc_add_expr_to_block (&fnblock, body);
2922 return gfc_finish_block (&fnblock);
2926 /* Generate function entry and exit code, and add it to the function body.
2928 Allocation and initialization of array variables.
2929 Allocation of character string variables.
2930 Initialization and possibly repacking of dummy arrays.
2931 Initialization of ASSIGN statement auxiliary variable. */
2934 gfc_trans_deferred_vars (gfc_symbol * proc_sym, tree fnbody)
2938 gfc_formal_arglist *f;
2940 bool seen_trans_deferred_array = false;
2942 /* Deal with implicit return variables. Explicit return variables will
2943 already have been added. */
2944 if (gfc_return_by_reference (proc_sym) && proc_sym->result == proc_sym)
2946 if (!current_fake_result_decl)
2948 gfc_entry_list *el = NULL;
2949 if (proc_sym->attr.entry_master)
2951 for (el = proc_sym->ns->entries; el; el = el->next)
2952 if (el->sym != el->sym->result)
2955 /* TODO: move to the appropriate place in resolve.c. */
2956 if (warn_return_type && el == NULL)
2957 gfc_warning ("Return value of function '%s' at %L not set",
2958 proc_sym->name, &proc_sym->declared_at);
2960 else if (proc_sym->as)
2962 tree result = TREE_VALUE (current_fake_result_decl);
2963 fnbody = gfc_trans_dummy_array_bias (proc_sym, result, fnbody);
2965 /* An automatic character length, pointer array result. */
2966 if (proc_sym->ts.type == BT_CHARACTER
2967 && TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL)
2968 fnbody = gfc_trans_dummy_character (proc_sym, proc_sym->ts.cl,
2971 else if (proc_sym->ts.type == BT_CHARACTER)
2973 if (TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL)
2974 fnbody = gfc_trans_dummy_character (proc_sym, proc_sym->ts.cl,
2978 gcc_assert (gfc_option.flag_f2c
2979 && proc_sym->ts.type == BT_COMPLEX);
2982 /* Initialize the INTENT(OUT) derived type dummy arguments. This
2983 should be done here so that the offsets and lbounds of arrays
2985 fnbody = init_intent_out_dt (proc_sym, fnbody);
2987 for (sym = proc_sym->tlink; sym != proc_sym; sym = sym->tlink)
2989 bool sym_has_alloc_comp = (sym->ts.type == BT_DERIVED)
2990 && sym->ts.derived->attr.alloc_comp;
2991 if (sym->attr.dimension)
2993 switch (sym->as->type)
2996 if (sym->attr.dummy || sym->attr.result)
2998 gfc_trans_dummy_array_bias (sym, sym->backend_decl, fnbody);
2999 else if (sym->attr.pointer || sym->attr.allocatable)
3001 if (TREE_STATIC (sym->backend_decl))
3002 gfc_trans_static_array_pointer (sym);
3005 seen_trans_deferred_array = true;
3006 fnbody = gfc_trans_deferred_array (sym, fnbody);
3011 if (sym_has_alloc_comp)
3013 seen_trans_deferred_array = true;
3014 fnbody = gfc_trans_deferred_array (sym, fnbody);
3016 else if (sym->ts.type == BT_DERIVED
3019 && sym->attr.save == SAVE_NONE)
3020 fnbody = gfc_init_default_dt (sym, fnbody);
3022 gfc_get_backend_locus (&loc);
3023 gfc_set_backend_locus (&sym->declared_at);
3024 fnbody = gfc_trans_auto_array_allocation (sym->backend_decl,
3026 gfc_set_backend_locus (&loc);
3030 case AS_ASSUMED_SIZE:
3031 /* Must be a dummy parameter. */
3032 gcc_assert (sym->attr.dummy);
3034 /* We should always pass assumed size arrays the g77 way. */
3035 fnbody = gfc_trans_g77_array (sym, fnbody);
3038 case AS_ASSUMED_SHAPE:
3039 /* Must be a dummy parameter. */
3040 gcc_assert (sym->attr.dummy);
3042 fnbody = gfc_trans_dummy_array_bias (sym, sym->backend_decl,
3047 seen_trans_deferred_array = true;
3048 fnbody = gfc_trans_deferred_array (sym, fnbody);
3054 if (sym_has_alloc_comp && !seen_trans_deferred_array)
3055 fnbody = gfc_trans_deferred_array (sym, fnbody);
3057 else if (sym_has_alloc_comp)
3058 fnbody = gfc_trans_deferred_array (sym, fnbody);
3059 else if (sym->ts.type == BT_CHARACTER)
3061 gfc_get_backend_locus (&loc);
3062 gfc_set_backend_locus (&sym->declared_at);
3063 if (sym->attr.dummy || sym->attr.result)
3064 fnbody = gfc_trans_dummy_character (sym, sym->ts.cl, fnbody);
3066 fnbody = gfc_trans_auto_character_variable (sym, fnbody);
3067 gfc_set_backend_locus (&loc);
3069 else if (sym->attr.assign)
3071 gfc_get_backend_locus (&loc);
3072 gfc_set_backend_locus (&sym->declared_at);
3073 fnbody = gfc_trans_assign_aux_var (sym, fnbody);
3074 gfc_set_backend_locus (&loc);
3076 else if (sym->ts.type == BT_DERIVED
3079 && sym->attr.save == SAVE_NONE)
3080 fnbody = gfc_init_default_dt (sym, fnbody);
3085 gfc_init_block (&body);
3087 for (f = proc_sym->formal; f; f = f->next)
3089 if (f->sym && f->sym->tlink == NULL && f->sym->ts.type == BT_CHARACTER)
3091 gcc_assert (f->sym->ts.cl->backend_decl != NULL);
3092 if (TREE_CODE (f->sym->ts.cl->backend_decl) == PARM_DECL)
3093 gfc_trans_vla_type_sizes (f->sym, &body);
3097 if (gfc_return_by_reference (proc_sym) && proc_sym->ts.type == BT_CHARACTER
3098 && current_fake_result_decl != NULL)
3100 gcc_assert (proc_sym->ts.cl->backend_decl != NULL);
3101 if (TREE_CODE (proc_sym->ts.cl->backend_decl) == PARM_DECL)
3102 gfc_trans_vla_type_sizes (proc_sym, &body);
3105 gfc_add_expr_to_block (&body, fnbody);
3106 return gfc_finish_block (&body);
3109 static GTY ((param_is (struct module_htab_entry))) htab_t module_htab;
3111 /* Hash and equality functions for module_htab. */
3114 module_htab_do_hash (const void *x)
3116 return htab_hash_string (((const struct module_htab_entry *)x)->name);
3120 module_htab_eq (const void *x1, const void *x2)
3122 return strcmp ((((const struct module_htab_entry *)x1)->name),
3123 (const char *)x2) == 0;
3126 /* Hash and equality functions for module_htab's decls. */
3129 module_htab_decls_hash (const void *x)
3131 const_tree t = (const_tree) x;
3132 const_tree n = DECL_NAME (t);
3134 n = TYPE_NAME (TREE_TYPE (t));
3135 return htab_hash_string (IDENTIFIER_POINTER (n));
3139 module_htab_decls_eq (const void *x1, const void *x2)
3141 const_tree t1 = (const_tree) x1;
3142 const_tree n1 = DECL_NAME (t1);
3143 if (n1 == NULL_TREE)
3144 n1 = TYPE_NAME (TREE_TYPE (t1));
3145 return strcmp (IDENTIFIER_POINTER (n1), (const char *) x2) == 0;
3148 struct module_htab_entry *
3149 gfc_find_module (const char *name)
3154 module_htab = htab_create_ggc (10, module_htab_do_hash,
3155 module_htab_eq, NULL);
3157 slot = htab_find_slot_with_hash (module_htab, name,
3158 htab_hash_string (name), INSERT);
3161 struct module_htab_entry *entry = GGC_CNEW (struct module_htab_entry);
3163 entry->name = gfc_get_string (name);
3164 entry->decls = htab_create_ggc (10, module_htab_decls_hash,
3165 module_htab_decls_eq, NULL);
3166 *slot = (void *) entry;
3168 return (struct module_htab_entry *) *slot;
3172 gfc_module_add_decl (struct module_htab_entry *entry, tree decl)
3177 if (DECL_NAME (decl))
3178 name = IDENTIFIER_POINTER (DECL_NAME (decl));
3181 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
3182 name = IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl)));
3184 slot = htab_find_slot_with_hash (entry->decls, name,
3185 htab_hash_string (name), INSERT);
3187 *slot = (void *) decl;
3190 static struct module_htab_entry *cur_module;
3192 /* Output an initialized decl for a module variable. */
3195 gfc_create_module_variable (gfc_symbol * sym)
3199 /* Module functions with alternate entries are dealt with later and
3200 would get caught by the next condition. */
3201 if (sym->attr.entry)
3204 /* Make sure we convert the types of the derived types from iso_c_binding
3206 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
3207 && sym->ts.type == BT_DERIVED)
3208 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
3210 if (sym->attr.flavor == FL_DERIVED
3211 && sym->backend_decl
3212 && TREE_CODE (sym->backend_decl) == RECORD_TYPE)
3214 decl = sym->backend_decl;
3215 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
3216 gcc_assert (TYPE_CONTEXT (decl) == NULL_TREE
3217 || TYPE_CONTEXT (decl) == sym->ns->proc_name->backend_decl);
3218 gcc_assert (DECL_CONTEXT (TYPE_STUB_DECL (decl)) == NULL_TREE
3219 || DECL_CONTEXT (TYPE_STUB_DECL (decl))
3220 == sym->ns->proc_name->backend_decl);
3221 TYPE_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
3222 DECL_CONTEXT (TYPE_STUB_DECL (decl)) = sym->ns->proc_name->backend_decl;
3223 gfc_module_add_decl (cur_module, TYPE_STUB_DECL (decl));
3226 /* Only output variables, procedure pointers and array valued,
3227 or derived type, parameters. */
3228 if (sym->attr.flavor != FL_VARIABLE
3229 && !(sym->attr.flavor == FL_PARAMETER
3230 && (sym->attr.dimension || sym->ts.type == BT_DERIVED))
3231 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer))
3234 if ((sym->attr.in_common || sym->attr.in_equivalence) && sym->backend_decl)
3236 decl = sym->backend_decl;
3237 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE);
3238 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
3239 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
3240 gfc_module_add_decl (cur_module, decl);
3243 /* Don't generate variables from other modules. Variables from
3244 COMMONs will already have been generated. */
3245 if (sym->attr.use_assoc || sym->attr.in_common)
3248 /* Equivalenced variables arrive here after creation. */
3249 if (sym->backend_decl
3250 && (sym->equiv_built || sym->attr.in_equivalence))
3253 if (sym->backend_decl)
3254 internal_error ("backend decl for module variable %s already exists",
3257 /* We always want module variables to be created. */
3258 sym->attr.referenced = 1;
3259 /* Create the decl. */
3260 decl = gfc_get_symbol_decl (sym);
3262 /* Create the variable. */
3264 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE);
3265 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
3266 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
3267 rest_of_decl_compilation (decl, 1, 0);
3268 gfc_module_add_decl (cur_module, decl);
3270 /* Also add length of strings. */
3271 if (sym->ts.type == BT_CHARACTER)
3275 length = sym->ts.cl->backend_decl;
3276 if (!INTEGER_CST_P (length))
3279 rest_of_decl_compilation (length, 1, 0);
3284 /* Emit debug information for USE statements. */
3287 gfc_trans_use_stmts (gfc_namespace * ns)
3289 gfc_use_list *use_stmt;
3290 for (use_stmt = ns->use_stmts; use_stmt; use_stmt = use_stmt->next)
3292 struct module_htab_entry *entry
3293 = gfc_find_module (use_stmt->module_name);
3294 gfc_use_rename *rent;
3296 if (entry->namespace_decl == NULL)
3298 entry->namespace_decl
3299 = build_decl (NAMESPACE_DECL,
3300 get_identifier (use_stmt->module_name),
3302 DECL_EXTERNAL (entry->namespace_decl) = 1;
3304 gfc_set_backend_locus (&use_stmt->where);
3305 if (!use_stmt->only_flag)
3306 (*debug_hooks->imported_module_or_decl) (entry->namespace_decl,
3308 ns->proc_name->backend_decl,
3310 for (rent = use_stmt->rename; rent; rent = rent->next)
3312 tree decl, local_name;
3315 if (rent->op != INTRINSIC_NONE)
3318 slot = htab_find_slot_with_hash (entry->decls, rent->use_name,
3319 htab_hash_string (rent->use_name),
3325 st = gfc_find_symtree (ns->sym_root,
3327 ? rent->local_name : rent->use_name);
3328 gcc_assert (st && st->n.sym->attr.use_assoc);
3329 if (st->n.sym->backend_decl
3330 && DECL_P (st->n.sym->backend_decl)
3331 && st->n.sym->module
3332 && strcmp (st->n.sym->module, use_stmt->module_name) == 0)
3334 gcc_assert (DECL_EXTERNAL (entry->namespace_decl)
3335 || (TREE_CODE (st->n.sym->backend_decl)
3337 decl = copy_node (st->n.sym->backend_decl);
3338 DECL_CONTEXT (decl) = entry->namespace_decl;
3339 DECL_EXTERNAL (decl) = 1;
3340 DECL_IGNORED_P (decl) = 0;
3341 DECL_INITIAL (decl) = NULL_TREE;
3345 *slot = error_mark_node;
3346 htab_clear_slot (entry->decls, slot);
3351 decl = (tree) *slot;
3352 if (rent->local_name[0])
3353 local_name = get_identifier (rent->local_name);
3355 local_name = NULL_TREE;
3356 gfc_set_backend_locus (&rent->where);
3357 (*debug_hooks->imported_module_or_decl) (decl, local_name,
3358 ns->proc_name->backend_decl,
3359 !use_stmt->only_flag);
3365 /* Return true if expr is a constant initializer that gfc_conv_initializer
3369 check_constant_initializer (gfc_expr *expr, gfc_typespec *ts, bool array,
3379 if (expr->expr_type == EXPR_CONSTANT || expr->expr_type == EXPR_NULL)
3381 else if (expr->expr_type == EXPR_STRUCTURE)
3382 return check_constant_initializer (expr, ts, false, false);
3383 else if (expr->expr_type != EXPR_ARRAY)
3385 for (c = expr->value.constructor; c; c = c->next)
3389 if (c->expr->expr_type == EXPR_STRUCTURE)
3391 if (!check_constant_initializer (c->expr, ts, false, false))
3394 else if (c->expr->expr_type != EXPR_CONSTANT)
3399 else switch (ts->type)
3402 if (expr->expr_type != EXPR_STRUCTURE)
3404 cm = expr->ts.derived->components;
3405 for (c = expr->value.constructor; c; c = c->next, cm = cm->next)
3407 if (!c->expr || cm->attr.allocatable)
3409 if (!check_constant_initializer (c->expr, &cm->ts,
3416 return expr->expr_type == EXPR_CONSTANT;
3420 /* Emit debug info for parameters and unreferenced variables with
3424 gfc_emit_parameter_debug_info (gfc_symbol *sym)
3428 if (sym->attr.flavor != FL_PARAMETER
3429 && (sym->attr.flavor != FL_VARIABLE || sym->attr.referenced))
3432 if (sym->backend_decl != NULL
3433 || sym->value == NULL
3434 || sym->attr.use_assoc
3437 || sym->attr.function
3438 || sym->attr.intrinsic
3439 || sym->attr.pointer
3440 || sym->attr.allocatable
3441 || sym->attr.cray_pointee
3442 || sym->attr.threadprivate
3443 || sym->attr.is_bind_c
3444 || sym->attr.subref_array_pointer
3445 || sym->attr.assign)
3448 if (sym->ts.type == BT_CHARACTER)
3450 gfc_conv_const_charlen (sym->ts.cl);
3451 if (sym->ts.cl->backend_decl == NULL
3452 || TREE_CODE (sym->ts.cl->backend_decl) != INTEGER_CST)
3455 else if (sym->ts.type == BT_DERIVED && sym->ts.derived->attr.alloc_comp)
3462 if (sym->as->type != AS_EXPLICIT)
3464 for (n = 0; n < sym->as->rank; n++)
3465 if (sym->as->lower[n]->expr_type != EXPR_CONSTANT
3466 || sym->as->upper[n] == NULL
3467 || sym->as->upper[n]->expr_type != EXPR_CONSTANT)
3471 if (!check_constant_initializer (sym->value, &sym->ts,
3472 sym->attr.dimension, false))
3475 /* Create the decl for the variable or constant. */
3476 decl = build_decl (sym->attr.flavor == FL_PARAMETER ? CONST_DECL : VAR_DECL,
3477 gfc_sym_identifier (sym), gfc_sym_type (sym));
3478 if (sym->attr.flavor == FL_PARAMETER)
3479 TREE_READONLY (decl) = 1;
3480 gfc_set_decl_location (decl, &sym->declared_at);
3481 if (sym->attr.dimension)
3482 GFC_DECL_PACKED_ARRAY (decl) = 1;
3483 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
3484 TREE_STATIC (decl) = 1;
3485 TREE_USED (decl) = 1;
3486 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
3487 TREE_PUBLIC (decl) = 1;
3489 = gfc_conv_initializer (sym->value, &sym->ts, TREE_TYPE (decl),
3490 sym->attr.dimension, 0);
3491 debug_hooks->global_decl (decl);
3494 /* Generate all the required code for module variables. */
3497 gfc_generate_module_vars (gfc_namespace * ns)
3499 module_namespace = ns;
3500 cur_module = gfc_find_module (ns->proc_name->name);
3502 /* Check if the frontend left the namespace in a reasonable state. */
3503 gcc_assert (ns->proc_name && !ns->proc_name->tlink);
3505 /* Generate COMMON blocks. */
3506 gfc_trans_common (ns);
3508 /* Create decls for all the module variables. */
3509 gfc_traverse_ns (ns, gfc_create_module_variable);
3513 gfc_trans_use_stmts (ns);
3514 gfc_traverse_ns (ns, gfc_emit_parameter_debug_info);
3519 gfc_generate_contained_functions (gfc_namespace * parent)
3523 /* We create all the prototypes before generating any code. */
3524 for (ns = parent->contained; ns; ns = ns->sibling)
3526 /* Skip namespaces from used modules. */
3527 if (ns->parent != parent)
3530 gfc_create_function_decl (ns);
3533 for (ns = parent->contained; ns; ns = ns->sibling)
3535 /* Skip namespaces from used modules. */
3536 if (ns->parent != parent)
3539 gfc_generate_function_code (ns);
3544 /* Drill down through expressions for the array specification bounds and
3545 character length calling generate_local_decl for all those variables
3546 that have not already been declared. */
3549 generate_local_decl (gfc_symbol *);
3551 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
3554 expr_decls (gfc_expr *e, gfc_symbol *sym,
3555 int *f ATTRIBUTE_UNUSED)
3557 if (e->expr_type != EXPR_VARIABLE
3558 || sym == e->symtree->n.sym
3559 || e->symtree->n.sym->mark
3560 || e->symtree->n.sym->ns != sym->ns)
3563 generate_local_decl (e->symtree->n.sym);
3568 generate_expr_decls (gfc_symbol *sym, gfc_expr *e)
3570 gfc_traverse_expr (e, sym, expr_decls, 0);
3574 /* Check for dependencies in the character length and array spec. */
3577 generate_dependency_declarations (gfc_symbol *sym)
3581 if (sym->ts.type == BT_CHARACTER
3583 && sym->ts.cl->length
3584 && sym->ts.cl->length->expr_type != EXPR_CONSTANT)
3585 generate_expr_decls (sym, sym->ts.cl->length);
3587 if (sym->as && sym->as->rank)
3589 for (i = 0; i < sym->as->rank; i++)
3591 generate_expr_decls (sym, sym->as->lower[i]);
3592 generate_expr_decls (sym, sym->as->upper[i]);
3598 /* Generate decls for all local variables. We do this to ensure correct
3599 handling of expressions which only appear in the specification of
3603 generate_local_decl (gfc_symbol * sym)
3605 if (sym->attr.flavor == FL_VARIABLE)
3607 if (!sym->attr.dummy && !sym->ns->proc_name->attr.entry_master)
3608 generate_dependency_declarations (sym);
3610 if (sym->attr.referenced)
3611 gfc_get_symbol_decl (sym);
3612 /* INTENT(out) dummy arguments are likely meant to be set. */
3613 else if (warn_unused_variable
3615 && sym->attr.intent == INTENT_OUT)
3616 gfc_warning ("Dummy argument '%s' at %L was declared INTENT(OUT) but was not set",
3617 sym->name, &sym->declared_at);
3618 /* Specific warning for unused dummy arguments. */
3619 else if (warn_unused_variable && sym->attr.dummy)
3620 gfc_warning ("Unused dummy argument '%s' at %L", sym->name,
3622 /* Warn for unused variables, but not if they're inside a common
3623 block or are use-associated. */
3624 else if (warn_unused_variable
3625 && !(sym->attr.in_common || sym->attr.use_assoc || sym->mark))
3626 gfc_warning ("Unused variable '%s' declared at %L", sym->name,
3629 /* For variable length CHARACTER parameters, the PARM_DECL already
3630 references the length variable, so force gfc_get_symbol_decl
3631 even when not referenced. If optimize > 0, it will be optimized
3632 away anyway. But do this only after emitting -Wunused-parameter
3633 warning if requested. */
3634 if (sym->attr.dummy && !sym->attr.referenced
3635 && sym->ts.type == BT_CHARACTER
3636 && sym->ts.cl->backend_decl != NULL
3637 && TREE_CODE (sym->ts.cl->backend_decl) == VAR_DECL)
3639 sym->attr.referenced = 1;
3640 gfc_get_symbol_decl (sym);
3643 /* INTENT(out) dummy arguments with allocatable components are reset
3644 by default and need to be set referenced to generate the code for
3645 automatic lengths. */
3646 if (sym->attr.dummy && !sym->attr.referenced
3647 && sym->ts.type == BT_DERIVED
3648 && sym->ts.derived->attr.alloc_comp
3649 && sym->attr.intent == INTENT_OUT)
3651 sym->attr.referenced = 1;
3652 gfc_get_symbol_decl (sym);
3656 /* Check for dependencies in the array specification and string
3657 length, adding the necessary declarations to the function. We
3658 mark the symbol now, as well as in traverse_ns, to prevent
3659 getting stuck in a circular dependency. */
3662 /* We do not want the middle-end to warn about unused parameters
3663 as this was already done above. */
3664 if (sym->attr.dummy && sym->backend_decl != NULL_TREE)
3665 TREE_NO_WARNING(sym->backend_decl) = 1;
3667 else if (sym->attr.flavor == FL_PARAMETER)
3669 if (warn_unused_parameter
3670 && !sym->attr.referenced
3671 && !sym->attr.use_assoc)
3672 gfc_warning ("Unused parameter '%s' declared at %L", sym->name,
3675 else if (sym->attr.flavor == FL_PROCEDURE)
3677 /* TODO: move to the appropriate place in resolve.c. */
3678 if (warn_return_type
3679 && sym->attr.function
3681 && sym != sym->result
3682 && !sym->result->attr.referenced
3683 && !sym->attr.use_assoc
3684 && sym->attr.if_source != IFSRC_IFBODY)
3686 gfc_warning ("Return value '%s' of function '%s' declared at "
3687 "%L not set", sym->result->name, sym->name,
3688 &sym->result->declared_at);
3690 /* Prevents "Unused variable" warning for RESULT variables. */
3691 sym->result->mark = 1;
3695 if (sym->attr.dummy == 1)
3697 /* Modify the tree type for scalar character dummy arguments of bind(c)
3698 procedures if they are passed by value. The tree type for them will
3699 be promoted to INTEGER_TYPE for the middle end, which appears to be
3700 what C would do with characters passed by-value. The value attribute
3701 implies the dummy is a scalar. */
3702 if (sym->attr.value == 1 && sym->backend_decl != NULL
3703 && sym->ts.type == BT_CHARACTER && sym->ts.is_c_interop
3704 && sym->ns->proc_name != NULL && sym->ns->proc_name->attr.is_bind_c)
3705 gfc_conv_scalar_char_value (sym, NULL, NULL);
3708 /* Make sure we convert the types of the derived types from iso_c_binding
3710 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
3711 && sym->ts.type == BT_DERIVED)
3712 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
3716 generate_local_vars (gfc_namespace * ns)
3718 gfc_traverse_ns (ns, generate_local_decl);
3722 /* Generate a switch statement to jump to the correct entry point. Also
3723 creates the label decls for the entry points. */
3726 gfc_trans_entry_master_switch (gfc_entry_list * el)
3733 gfc_init_block (&block);
3734 for (; el; el = el->next)
3736 /* Add the case label. */
3737 label = gfc_build_label_decl (NULL_TREE);
3738 val = build_int_cst (gfc_array_index_type, el->id);
3739 tmp = build3_v (CASE_LABEL_EXPR, val, NULL_TREE, label);
3740 gfc_add_expr_to_block (&block, tmp);
3742 /* And jump to the actual entry point. */
3743 label = gfc_build_label_decl (NULL_TREE);
3744 tmp = build1_v (GOTO_EXPR, label);
3745 gfc_add_expr_to_block (&block, tmp);
3747 /* Save the label decl. */
3750 tmp = gfc_finish_block (&block);
3751 /* The first argument selects the entry point. */
3752 val = DECL_ARGUMENTS (current_function_decl);
3753 tmp = build3_v (SWITCH_EXPR, val, tmp, NULL_TREE);
3758 /* Add code to string lengths of actual arguments passed to a function against
3759 the expected lengths of the dummy arguments. */
3762 add_argument_checking (stmtblock_t *block, gfc_symbol *sym)
3764 gfc_formal_arglist *formal;
3766 for (formal = sym->formal; formal; formal = formal->next)
3767 if (formal->sym && formal->sym->ts.type == BT_CHARACTER)
3769 enum tree_code comparison;
3774 const char *message;
3780 gcc_assert (cl->passed_length != NULL_TREE);
3781 gcc_assert (cl->backend_decl != NULL_TREE);
3783 /* For POINTER, ALLOCATABLE and assumed-shape dummy arguments, the
3784 string lengths must match exactly. Otherwise, it is only required
3785 that the actual string length is *at least* the expected one. */
3786 if (fsym->attr.pointer || fsym->attr.allocatable
3787 || (fsym->as && fsym->as->type == AS_ASSUMED_SHAPE))
3789 comparison = NE_EXPR;
3790 message = _("Actual string length does not match the declared one"
3791 " for dummy argument '%s' (%ld/%ld)");
3795 comparison = LT_EXPR;
3796 message = _("Actual string length is shorter than the declared one"
3797 " for dummy argument '%s' (%ld/%ld)");
3800 /* Build the condition. For optional arguments, an actual length
3801 of 0 is also acceptable if the associated string is NULL, which
3802 means the argument was not passed. */
3803 cond = fold_build2 (comparison, boolean_type_node,
3804 cl->passed_length, cl->backend_decl);
3805 if (fsym->attr.optional)
3811 not_0length = fold_build2 (NE_EXPR, boolean_type_node,
3813 fold_convert (gfc_charlen_type_node,
3814 integer_zero_node));
3815 not_absent = fold_build2 (NE_EXPR, boolean_type_node,
3816 fsym->backend_decl, null_pointer_node);
3818 absent_failed = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
3819 not_0length, not_absent);
3821 cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
3822 cond, absent_failed);
3825 /* Build the runtime check. */
3826 argname = gfc_build_cstring_const (fsym->name);
3827 argname = gfc_build_addr_expr (pchar_type_node, argname);
3828 gfc_trans_runtime_check (true, false, cond, block, &fsym->declared_at,
3830 fold_convert (long_integer_type_node,
3832 fold_convert (long_integer_type_node,
3839 create_main_function (tree fndecl)
3843 tree tmp, decl, result_decl, argc, argv, typelist, arglist;
3846 /* main() function must be declared with global scope. */
3847 gcc_assert (current_function_decl == NULL_TREE);
3849 /* Declare the function. */
3850 tmp = build_function_type_list (integer_type_node, integer_type_node,
3851 build_pointer_type (pchar_type_node),
3853 ftn_main = build_decl (FUNCTION_DECL, get_identifier ("main"), tmp);
3854 DECL_EXTERNAL (ftn_main) = 0;
3855 TREE_PUBLIC (ftn_main) = 1;
3856 TREE_STATIC (ftn_main) = 1;
3857 DECL_ATTRIBUTES (ftn_main)
3858 = tree_cons (get_identifier("externally_visible"), NULL_TREE, NULL_TREE);
3860 /* Setup the result declaration (for "return 0"). */
3861 result_decl = build_decl (RESULT_DECL, NULL_TREE, integer_type_node);
3862 DECL_ARTIFICIAL (result_decl) = 1;
3863 DECL_IGNORED_P (result_decl) = 1;
3864 DECL_CONTEXT (result_decl) = ftn_main;
3865 DECL_RESULT (ftn_main) = result_decl;
3867 pushdecl (ftn_main);
3869 /* Get the arguments. */
3871 arglist = NULL_TREE;
3872 typelist = TYPE_ARG_TYPES (TREE_TYPE (ftn_main));
3874 tmp = TREE_VALUE (typelist);
3875 argc = build_decl (PARM_DECL, get_identifier ("argc"), tmp);
3876 DECL_CONTEXT (argc) = ftn_main;
3877 DECL_ARG_TYPE (argc) = TREE_VALUE (typelist);
3878 TREE_READONLY (argc) = 1;
3879 gfc_finish_decl (argc);
3880 arglist = chainon (arglist, argc);
3882 typelist = TREE_CHAIN (typelist);
3883 tmp = TREE_VALUE (typelist);
3884 argv = build_decl (PARM_DECL, get_identifier ("argv"), tmp);
3885 DECL_CONTEXT (argv) = ftn_main;
3886 DECL_ARG_TYPE (argv) = TREE_VALUE (typelist);
3887 TREE_READONLY (argv) = 1;
3888 DECL_BY_REFERENCE (argv) = 1;
3889 gfc_finish_decl (argv);
3890 arglist = chainon (arglist, argv);
3892 DECL_ARGUMENTS (ftn_main) = arglist;
3893 current_function_decl = ftn_main;
3894 announce_function (ftn_main);
3896 rest_of_decl_compilation (ftn_main, 1, 0);
3897 make_decl_rtl (ftn_main);
3898 init_function_start (ftn_main);
3901 gfc_init_block (&body);
3903 /* Call some libgfortran initialization routines, call then MAIN__(). */
3905 /* Call _gfortran_set_args (argc, argv). */
3906 tmp = build_call_expr (gfor_fndecl_set_args, 2, argc, argv);
3907 gfc_add_expr_to_block (&body, tmp);
3909 /* Add a call to set_options to set up the runtime library Fortran
3910 language standard parameters. */
3912 tree array_type, array, var;
3914 /* Passing a new option to the library requires four modifications:
3915 + add it to the tree_cons list below
3916 + change the array size in the call to build_array_type
3917 + change the first argument to the library call
3918 gfor_fndecl_set_options
3919 + modify the library (runtime/compile_options.c)! */
3921 array = tree_cons (NULL_TREE, build_int_cst (integer_type_node,
3922 gfc_option.warn_std), NULL_TREE);
3923 array = tree_cons (NULL_TREE, build_int_cst (integer_type_node,
3924 gfc_option.allow_std), array);
3925 array = tree_cons (NULL_TREE, build_int_cst (integer_type_node, pedantic),
3927 array = tree_cons (NULL_TREE, build_int_cst (integer_type_node,
3928 gfc_option.flag_dump_core), array);
3929 array = tree_cons (NULL_TREE, build_int_cst (integer_type_node,
3930 gfc_option.flag_backtrace), array);
3931 array = tree_cons (NULL_TREE, build_int_cst (integer_type_node,
3932 gfc_option.flag_sign_zero), array);
3934 array = tree_cons (NULL_TREE, build_int_cst (integer_type_node,
3935 (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)), array);
3937 array = tree_cons (NULL_TREE, build_int_cst (integer_type_node,
3938 gfc_option.flag_range_check), array);
3940 array_type = build_array_type (integer_type_node,
3941 build_index_type (build_int_cst (NULL_TREE, 7)));
3942 array = build_constructor_from_list (array_type, nreverse (array));
3943 TREE_CONSTANT (array) = 1;
3944 TREE_STATIC (array) = 1;
3946 /* Create a static variable to hold the jump table. */
3947 var = gfc_create_var (array_type, "options");
3948 TREE_CONSTANT (var) = 1;
3949 TREE_STATIC (var) = 1;
3950 TREE_READONLY (var) = 1;
3951 DECL_INITIAL (var) = array;
3952 var = gfc_build_addr_expr (build_pointer_type (integer_type_node), var);
3954 tmp = build_call_expr (gfor_fndecl_set_options, 2,
3955 build_int_cst (integer_type_node, 8), var);
3956 gfc_add_expr_to_block (&body, tmp);
3959 /* If -ffpe-trap option was provided, add a call to set_fpe so that
3960 the library will raise a FPE when needed. */
3961 if (gfc_option.fpe != 0)
3963 tmp = build_call_expr (gfor_fndecl_set_fpe, 1,
3964 build_int_cst (integer_type_node,
3966 gfc_add_expr_to_block (&body, tmp);
3969 /* If this is the main program and an -fconvert option was provided,
3970 add a call to set_convert. */
3972 if (gfc_option.convert != GFC_CONVERT_NATIVE)
3974 tmp = build_call_expr (gfor_fndecl_set_convert, 1,
3975 build_int_cst (integer_type_node,
3976 gfc_option.convert));
3977 gfc_add_expr_to_block (&body, tmp);
3980 /* If this is the main program and an -frecord-marker option was provided,
3981 add a call to set_record_marker. */
3983 if (gfc_option.record_marker != 0)
3985 tmp = build_call_expr (gfor_fndecl_set_record_marker, 1,
3986 build_int_cst (integer_type_node,
3987 gfc_option.record_marker));
3988 gfc_add_expr_to_block (&body, tmp);
3991 if (gfc_option.max_subrecord_length != 0)
3993 tmp = build_call_expr (gfor_fndecl_set_max_subrecord_length, 1,
3994 build_int_cst (integer_type_node,
3995 gfc_option.max_subrecord_length));
3996 gfc_add_expr_to_block (&body, tmp);
3999 /* Call MAIN__(). */
4000 tmp = build_call_expr (fndecl, 0);
4001 gfc_add_expr_to_block (&body, tmp);
4004 tmp = fold_build2 (MODIFY_EXPR, integer_type_node, DECL_RESULT (ftn_main),
4005 build_int_cst (integer_type_node, 0));
4006 tmp = build1_v (RETURN_EXPR, tmp);
4007 gfc_add_expr_to_block (&body, tmp);
4010 DECL_SAVED_TREE (ftn_main) = gfc_finish_block (&body);
4013 /* Finish off this function and send it for code generation. */
4015 BLOCK_SUPERCONTEXT (DECL_INITIAL (ftn_main)) = ftn_main;
4017 DECL_SAVED_TREE (ftn_main)
4018 = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (ftn_main),
4019 DECL_INITIAL (ftn_main));
4021 /* Output the GENERIC tree. */
4022 dump_function (TDI_original, ftn_main);
4024 gfc_gimplify_function (ftn_main);
4025 cgraph_finalize_function (ftn_main, false);
4029 /* Generate code for a function. */
4032 gfc_generate_function_code (gfc_namespace * ns)
4042 tree recurcheckvar = NULL;
4047 sym = ns->proc_name;
4049 /* Check that the frontend isn't still using this. */
4050 gcc_assert (sym->tlink == NULL);
4053 /* Create the declaration for functions with global scope. */
4054 if (!sym->backend_decl)
4055 gfc_create_function_decl (ns);
4057 fndecl = sym->backend_decl;
4058 old_context = current_function_decl;
4062 push_function_context ();
4063 saved_parent_function_decls = saved_function_decls;
4064 saved_function_decls = NULL_TREE;
4067 trans_function_start (sym);
4069 gfc_init_block (&block);
4071 if (ns->entries && ns->proc_name->ts.type == BT_CHARACTER)
4073 /* Copy length backend_decls to all entry point result
4078 gfc_conv_const_charlen (ns->proc_name->ts.cl);
4079 backend_decl = ns->proc_name->result->ts.cl->backend_decl;
4080 for (el = ns->entries; el; el = el->next)
4081 el->sym->result->ts.cl->backend_decl = backend_decl;
4084 /* Translate COMMON blocks. */
4085 gfc_trans_common (ns);
4087 /* Null the parent fake result declaration if this namespace is
4088 a module function or an external procedures. */
4089 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
4090 || ns->parent == NULL)
4091 parent_fake_result_decl = NULL_TREE;
4093 gfc_generate_contained_functions (ns);
4095 nonlocal_dummy_decls = NULL;
4096 nonlocal_dummy_decl_pset = NULL;
4098 generate_local_vars (ns);
4100 /* Keep the parent fake result declaration in module functions
4101 or external procedures. */
4102 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
4103 || ns->parent == NULL)
4104 current_fake_result_decl = parent_fake_result_decl;
4106 current_fake_result_decl = NULL_TREE;
4108 current_function_return_label = NULL;
4110 /* Now generate the code for the body of this function. */
4111 gfc_init_block (&body);
4113 is_recursive = sym->attr.recursive
4114 || (sym->attr.entry_master
4115 && sym->ns->entries->sym->attr.recursive);
4116 if ((gfc_option.rtcheck & GFC_RTCHECK_RECURSION) && !is_recursive)
4120 asprintf (&msg, "Recursive call to nonrecursive procedure '%s'",
4122 recurcheckvar = gfc_create_var (boolean_type_node, "is_recursive");
4123 TREE_STATIC (recurcheckvar) = 1;
4124 DECL_INITIAL (recurcheckvar) = boolean_false_node;
4125 gfc_add_expr_to_block (&block, recurcheckvar);
4126 gfc_trans_runtime_check (true, false, recurcheckvar, &block,
4127 &sym->declared_at, msg);
4128 gfc_add_modify (&block, recurcheckvar, boolean_true_node);
4132 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
4133 && sym->attr.subroutine)
4135 tree alternate_return;
4136 alternate_return = gfc_get_fake_result_decl (sym, 0);
4137 gfc_add_modify (&body, alternate_return, integer_zero_node);
4142 /* Jump to the correct entry point. */
4143 tmp = gfc_trans_entry_master_switch (ns->entries);
4144 gfc_add_expr_to_block (&body, tmp);
4147 /* If bounds-checking is enabled, generate code to check passed in actual
4148 arguments against the expected dummy argument attributes (e.g. string
4150 if (flag_bounds_check)
4151 add_argument_checking (&body, sym);
4153 tmp = gfc_trans_code (ns->code);
4154 gfc_add_expr_to_block (&body, tmp);
4156 /* Add a return label if needed. */
4157 if (current_function_return_label)
4159 tmp = build1_v (LABEL_EXPR, current_function_return_label);
4160 gfc_add_expr_to_block (&body, tmp);
4163 tmp = gfc_finish_block (&body);
4164 /* Add code to create and cleanup arrays. */
4165 tmp = gfc_trans_deferred_vars (sym, tmp);
4167 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node)
4169 if (sym->attr.subroutine || sym == sym->result)
4171 if (current_fake_result_decl != NULL)
4172 result = TREE_VALUE (current_fake_result_decl);
4175 current_fake_result_decl = NULL_TREE;
4178 result = sym->result->backend_decl;
4180 if (result != NULL_TREE && sym->attr.function
4181 && sym->ts.type == BT_DERIVED
4182 && sym->ts.derived->attr.alloc_comp
4183 && !sym->attr.pointer)
4185 rank = sym->as ? sym->as->rank : 0;
4186 tmp2 = gfc_nullify_alloc_comp (sym->ts.derived, result, rank);
4187 gfc_add_expr_to_block (&block, tmp2);
4190 gfc_add_expr_to_block (&block, tmp);
4192 /* Reset recursion-check variable. */
4193 if ((gfc_option.rtcheck & GFC_RTCHECK_RECURSION) && !is_recursive)
4195 gfc_add_modify (&block, recurcheckvar, boolean_false_node);
4196 recurcheckvar = NULL;
4199 if (result == NULL_TREE)
4201 /* TODO: move to the appropriate place in resolve.c. */
4202 if (warn_return_type && !sym->attr.referenced && sym == sym->result)
4203 gfc_warning ("Return value of function '%s' at %L not set",
4204 sym->name, &sym->declared_at);
4206 TREE_NO_WARNING(sym->backend_decl) = 1;
4210 /* Set the return value to the dummy result variable. The
4211 types may be different for scalar default REAL functions
4212 with -ff2c, therefore we have to convert. */
4213 tmp = convert (TREE_TYPE (DECL_RESULT (fndecl)), result);
4214 tmp = fold_build2 (MODIFY_EXPR, TREE_TYPE (tmp),
4215 DECL_RESULT (fndecl), tmp);
4216 tmp = build1_v (RETURN_EXPR, tmp);
4217 gfc_add_expr_to_block (&block, tmp);
4222 gfc_add_expr_to_block (&block, tmp);
4223 /* Reset recursion-check variable. */
4224 if ((gfc_option.rtcheck & GFC_RTCHECK_RECURSION) && !is_recursive)
4226 gfc_add_modify (&block, recurcheckvar, boolean_false_node);
4227 recurcheckvar = NULL;
4232 /* Add all the decls we created during processing. */
4233 decl = saved_function_decls;
4238 next = TREE_CHAIN (decl);
4239 TREE_CHAIN (decl) = NULL_TREE;
4243 saved_function_decls = NULL_TREE;
4245 DECL_SAVED_TREE (fndecl) = gfc_finish_block (&block);
4248 /* Finish off this function and send it for code generation. */
4250 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
4252 DECL_SAVED_TREE (fndecl)
4253 = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (fndecl),
4254 DECL_INITIAL (fndecl));
4256 if (nonlocal_dummy_decls)
4258 BLOCK_VARS (DECL_INITIAL (fndecl))
4259 = chainon (BLOCK_VARS (DECL_INITIAL (fndecl)), nonlocal_dummy_decls);
4260 pointer_set_destroy (nonlocal_dummy_decl_pset);
4261 nonlocal_dummy_decls = NULL;
4262 nonlocal_dummy_decl_pset = NULL;
4265 /* Output the GENERIC tree. */
4266 dump_function (TDI_original, fndecl);
4268 /* Store the end of the function, so that we get good line number
4269 info for the epilogue. */
4270 cfun->function_end_locus = input_location;
4272 /* We're leaving the context of this function, so zap cfun.
4273 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
4274 tree_rest_of_compilation. */
4279 pop_function_context ();
4280 saved_function_decls = saved_parent_function_decls;
4282 current_function_decl = old_context;
4284 if (decl_function_context (fndecl))
4285 /* Register this function with cgraph just far enough to get it
4286 added to our parent's nested function list. */
4287 (void) cgraph_node (fndecl);
4290 gfc_gimplify_function (fndecl);
4291 cgraph_finalize_function (fndecl, false);
4294 gfc_trans_use_stmts (ns);
4295 gfc_traverse_ns (ns, gfc_emit_parameter_debug_info);
4297 if (sym->attr.is_main_program)
4298 create_main_function (fndecl);
4303 gfc_generate_constructors (void)
4305 gcc_assert (gfc_static_ctors == NULL_TREE);
4313 if (gfc_static_ctors == NULL_TREE)
4316 fnname = get_file_function_name ("I");
4317 type = build_function_type (void_type_node,
4318 gfc_chainon_list (NULL_TREE, void_type_node));
4320 fndecl = build_decl (FUNCTION_DECL, fnname, type);
4321 TREE_PUBLIC (fndecl) = 1;
4323 decl = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
4324 DECL_ARTIFICIAL (decl) = 1;
4325 DECL_IGNORED_P (decl) = 1;
4326 DECL_CONTEXT (decl) = fndecl;
4327 DECL_RESULT (fndecl) = decl;
4331 current_function_decl = fndecl;
4333 rest_of_decl_compilation (fndecl, 1, 0);
4335 make_decl_rtl (fndecl);
4337 init_function_start (fndecl);
4341 for (; gfc_static_ctors; gfc_static_ctors = TREE_CHAIN (gfc_static_ctors))
4343 tmp = build_call_expr (TREE_VALUE (gfc_static_ctors), 0);
4344 DECL_SAVED_TREE (fndecl) = build_stmt (EXPR_STMT, tmp);
4350 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
4351 DECL_SAVED_TREE (fndecl)
4352 = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (fndecl),
4353 DECL_INITIAL (fndecl));
4355 free_after_parsing (cfun);
4356 free_after_compilation (cfun);
4358 tree_rest_of_compilation (fndecl);
4360 current_function_decl = NULL_TREE;
4364 /* Translates a BLOCK DATA program unit. This means emitting the
4365 commons contained therein plus their initializations. We also emit
4366 a globally visible symbol to make sure that each BLOCK DATA program
4367 unit remains unique. */
4370 gfc_generate_block_data (gfc_namespace * ns)
4375 /* Tell the backend the source location of the block data. */
4377 gfc_set_backend_locus (&ns->proc_name->declared_at);
4379 gfc_set_backend_locus (&gfc_current_locus);
4381 /* Process the DATA statements. */
4382 gfc_trans_common (ns);
4384 /* Create a global symbol with the mane of the block data. This is to
4385 generate linker errors if the same name is used twice. It is never
4388 id = gfc_sym_mangled_function_id (ns->proc_name);
4390 id = get_identifier ("__BLOCK_DATA__");
4392 decl = build_decl (VAR_DECL, id, gfc_array_index_type);
4393 TREE_PUBLIC (decl) = 1;
4394 TREE_STATIC (decl) = 1;
4395 DECL_IGNORED_P (decl) = 1;
4398 rest_of_decl_compilation (decl, 1, 0);
4402 #include "gt-fortran-trans-decl.h"