1 /* Backend function setup
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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"
29 #include "tree-dump.h"
30 #include "gimple.h" /* For create_tmp_var_raw. */
32 #include "diagnostic-core.h" /* For internal_error. */
33 #include "toplev.h" /* For announce_function. */
34 #include "output.h" /* For decl_default_tls_model. */
41 #include "pointer-set.h"
42 #include "constructor.h"
44 #include "trans-types.h"
45 #include "trans-array.h"
46 #include "trans-const.h"
47 /* Only for gfc_trans_code. Shouldn't need to include this. */
48 #include "trans-stmt.h"
50 #define MAX_LABEL_VALUE 99999
53 /* Holds the result of the function if no result variable specified. */
55 static GTY(()) tree current_fake_result_decl;
56 static GTY(()) tree parent_fake_result_decl;
58 static GTY(()) tree current_function_return_label;
61 /* Holds the variable DECLs for the current function. */
63 static GTY(()) tree saved_function_decls;
64 static GTY(()) tree saved_parent_function_decls;
66 static struct pointer_set_t *nonlocal_dummy_decl_pset;
67 static GTY(()) tree nonlocal_dummy_decls;
69 /* Holds the variable DECLs that are locals. */
71 static GTY(()) tree saved_local_decls;
73 /* The namespace of the module we're currently generating. Only used while
74 outputting decls for module variables. Do not rely on this being set. */
76 static gfc_namespace *module_namespace;
79 /* List of static constructor functions. */
81 tree gfc_static_ctors;
84 /* Function declarations for builtin library functions. */
86 tree gfor_fndecl_pause_numeric;
87 tree gfor_fndecl_pause_string;
88 tree gfor_fndecl_stop_numeric;
89 tree gfor_fndecl_stop_string;
90 tree gfor_fndecl_error_stop_numeric;
91 tree gfor_fndecl_error_stop_string;
92 tree gfor_fndecl_runtime_error;
93 tree gfor_fndecl_runtime_error_at;
94 tree gfor_fndecl_runtime_warning_at;
95 tree gfor_fndecl_os_error;
96 tree gfor_fndecl_generate_error;
97 tree gfor_fndecl_set_args;
98 tree gfor_fndecl_set_fpe;
99 tree gfor_fndecl_set_options;
100 tree gfor_fndecl_set_convert;
101 tree gfor_fndecl_set_record_marker;
102 tree gfor_fndecl_set_max_subrecord_length;
103 tree gfor_fndecl_ctime;
104 tree gfor_fndecl_fdate;
105 tree gfor_fndecl_ttynam;
106 tree gfor_fndecl_in_pack;
107 tree gfor_fndecl_in_unpack;
108 tree gfor_fndecl_associated;
111 /* Math functions. Many other math functions are handled in
112 trans-intrinsic.c. */
114 gfc_powdecl_list gfor_fndecl_math_powi[4][3];
115 tree gfor_fndecl_math_ishftc4;
116 tree gfor_fndecl_math_ishftc8;
117 tree gfor_fndecl_math_ishftc16;
120 /* String functions. */
122 tree gfor_fndecl_compare_string;
123 tree gfor_fndecl_concat_string;
124 tree gfor_fndecl_string_len_trim;
125 tree gfor_fndecl_string_index;
126 tree gfor_fndecl_string_scan;
127 tree gfor_fndecl_string_verify;
128 tree gfor_fndecl_string_trim;
129 tree gfor_fndecl_string_minmax;
130 tree gfor_fndecl_adjustl;
131 tree gfor_fndecl_adjustr;
132 tree gfor_fndecl_select_string;
133 tree gfor_fndecl_compare_string_char4;
134 tree gfor_fndecl_concat_string_char4;
135 tree gfor_fndecl_string_len_trim_char4;
136 tree gfor_fndecl_string_index_char4;
137 tree gfor_fndecl_string_scan_char4;
138 tree gfor_fndecl_string_verify_char4;
139 tree gfor_fndecl_string_trim_char4;
140 tree gfor_fndecl_string_minmax_char4;
141 tree gfor_fndecl_adjustl_char4;
142 tree gfor_fndecl_adjustr_char4;
143 tree gfor_fndecl_select_string_char4;
146 /* Conversion between character kinds. */
147 tree gfor_fndecl_convert_char1_to_char4;
148 tree gfor_fndecl_convert_char4_to_char1;
151 /* Other misc. runtime library functions. */
153 tree gfor_fndecl_size0;
154 tree gfor_fndecl_size1;
155 tree gfor_fndecl_iargc;
156 tree gfor_fndecl_clz128;
157 tree gfor_fndecl_ctz128;
159 /* Intrinsic functions implemented in Fortran. */
160 tree gfor_fndecl_sc_kind;
161 tree gfor_fndecl_si_kind;
162 tree gfor_fndecl_sr_kind;
164 /* BLAS gemm functions. */
165 tree gfor_fndecl_sgemm;
166 tree gfor_fndecl_dgemm;
167 tree gfor_fndecl_cgemm;
168 tree gfor_fndecl_zgemm;
172 gfc_add_decl_to_parent_function (tree decl)
175 DECL_CONTEXT (decl) = DECL_CONTEXT (current_function_decl);
176 DECL_NONLOCAL (decl) = 1;
177 TREE_CHAIN (decl) = saved_parent_function_decls;
178 saved_parent_function_decls = decl;
182 gfc_add_decl_to_function (tree decl)
185 TREE_USED (decl) = 1;
186 DECL_CONTEXT (decl) = current_function_decl;
187 TREE_CHAIN (decl) = saved_function_decls;
188 saved_function_decls = decl;
192 add_decl_as_local (tree decl)
195 TREE_USED (decl) = 1;
196 DECL_CONTEXT (decl) = current_function_decl;
197 TREE_CHAIN (decl) = saved_local_decls;
198 saved_local_decls = decl;
202 /* Build a backend label declaration. Set TREE_USED for named labels.
203 The context of the label is always the current_function_decl. All
204 labels are marked artificial. */
207 gfc_build_label_decl (tree label_id)
209 /* 2^32 temporaries should be enough. */
210 static unsigned int tmp_num = 1;
214 if (label_id == NULL_TREE)
216 /* Build an internal label name. */
217 ASM_FORMAT_PRIVATE_NAME (label_name, "L", tmp_num++);
218 label_id = get_identifier (label_name);
223 /* Build the LABEL_DECL node. Labels have no type. */
224 label_decl = build_decl (input_location,
225 LABEL_DECL, label_id, void_type_node);
226 DECL_CONTEXT (label_decl) = current_function_decl;
227 DECL_MODE (label_decl) = VOIDmode;
229 /* We always define the label as used, even if the original source
230 file never references the label. We don't want all kinds of
231 spurious warnings for old-style Fortran code with too many
233 TREE_USED (label_decl) = 1;
235 DECL_ARTIFICIAL (label_decl) = 1;
240 /* Returns the return label for the current function. */
243 gfc_get_return_label (void)
245 char name[GFC_MAX_SYMBOL_LEN + 10];
247 if (current_function_return_label)
248 return current_function_return_label;
250 sprintf (name, "__return_%s",
251 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
253 current_function_return_label =
254 gfc_build_label_decl (get_identifier (name));
256 DECL_ARTIFICIAL (current_function_return_label) = 1;
258 return current_function_return_label;
262 /* Set the backend source location of a decl. */
265 gfc_set_decl_location (tree decl, locus * loc)
267 DECL_SOURCE_LOCATION (decl) = loc->lb->location;
271 /* Return the backend label declaration for a given label structure,
272 or create it if it doesn't exist yet. */
275 gfc_get_label_decl (gfc_st_label * lp)
277 if (lp->backend_decl)
278 return lp->backend_decl;
281 char label_name[GFC_MAX_SYMBOL_LEN + 1];
284 /* Validate the label declaration from the front end. */
285 gcc_assert (lp != NULL && lp->value <= MAX_LABEL_VALUE);
287 /* Build a mangled name for the label. */
288 sprintf (label_name, "__label_%.6d", lp->value);
290 /* Build the LABEL_DECL node. */
291 label_decl = gfc_build_label_decl (get_identifier (label_name));
293 /* Tell the debugger where the label came from. */
294 if (lp->value <= MAX_LABEL_VALUE) /* An internal label. */
295 gfc_set_decl_location (label_decl, &lp->where);
297 DECL_ARTIFICIAL (label_decl) = 1;
299 /* Store the label in the label list and return the LABEL_DECL. */
300 lp->backend_decl = label_decl;
306 /* Convert a gfc_symbol to an identifier of the same name. */
309 gfc_sym_identifier (gfc_symbol * sym)
311 if (sym->attr.is_main_program && strcmp (sym->name, "main") == 0)
312 return (get_identifier ("MAIN__"));
314 return (get_identifier (sym->name));
318 /* Construct mangled name from symbol name. */
321 gfc_sym_mangled_identifier (gfc_symbol * sym)
323 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
325 /* Prevent the mangling of identifiers that have an assigned
326 binding label (mainly those that are bind(c)). */
327 if (sym->attr.is_bind_c == 1
328 && sym->binding_label[0] != '\0')
329 return get_identifier(sym->binding_label);
331 if (sym->module == NULL)
332 return gfc_sym_identifier (sym);
335 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
336 return get_identifier (name);
341 /* Construct mangled function name from symbol name. */
344 gfc_sym_mangled_function_id (gfc_symbol * sym)
347 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
349 /* It may be possible to simply use the binding label if it's
350 provided, and remove the other checks. Then we could use it
351 for other things if we wished. */
352 if ((sym->attr.is_bind_c == 1 || sym->attr.is_iso_c == 1) &&
353 sym->binding_label[0] != '\0')
354 /* use the binding label rather than the mangled name */
355 return get_identifier (sym->binding_label);
357 if (sym->module == NULL || sym->attr.proc == PROC_EXTERNAL
358 || (sym->module != NULL && (sym->attr.external
359 || sym->attr.if_source == IFSRC_IFBODY)))
361 /* Main program is mangled into MAIN__. */
362 if (sym->attr.is_main_program)
363 return get_identifier ("MAIN__");
365 /* Intrinsic procedures are never mangled. */
366 if (sym->attr.proc == PROC_INTRINSIC)
367 return get_identifier (sym->name);
369 if (gfc_option.flag_underscoring)
371 has_underscore = strchr (sym->name, '_') != 0;
372 if (gfc_option.flag_second_underscore && has_underscore)
373 snprintf (name, sizeof name, "%s__", sym->name);
375 snprintf (name, sizeof name, "%s_", sym->name);
376 return get_identifier (name);
379 return get_identifier (sym->name);
383 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
384 return get_identifier (name);
390 gfc_set_decl_assembler_name (tree decl, tree name)
392 tree target_mangled = targetm.mangle_decl_assembler_name (decl, name);
393 SET_DECL_ASSEMBLER_NAME (decl, target_mangled);
397 /* Returns true if a variable of specified size should go on the stack. */
400 gfc_can_put_var_on_stack (tree size)
402 unsigned HOST_WIDE_INT low;
404 if (!INTEGER_CST_P (size))
407 if (gfc_option.flag_max_stack_var_size < 0)
410 if (TREE_INT_CST_HIGH (size) != 0)
413 low = TREE_INT_CST_LOW (size);
414 if (low > (unsigned HOST_WIDE_INT) gfc_option.flag_max_stack_var_size)
417 /* TODO: Set a per-function stack size limit. */
423 /* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
424 an expression involving its corresponding pointer. There are
425 2 cases; one for variable size arrays, and one for everything else,
426 because variable-sized arrays require one fewer level of
430 gfc_finish_cray_pointee (tree decl, gfc_symbol *sym)
432 tree ptr_decl = gfc_get_symbol_decl (sym->cp_pointer);
435 /* Parameters need to be dereferenced. */
436 if (sym->cp_pointer->attr.dummy)
437 ptr_decl = build_fold_indirect_ref_loc (input_location,
440 /* Check to see if we're dealing with a variable-sized array. */
441 if (sym->attr.dimension
442 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
444 /* These decls will be dereferenced later, so we don't dereference
446 value = convert (TREE_TYPE (decl), ptr_decl);
450 ptr_decl = convert (build_pointer_type (TREE_TYPE (decl)),
452 value = build_fold_indirect_ref_loc (input_location,
456 SET_DECL_VALUE_EXPR (decl, value);
457 DECL_HAS_VALUE_EXPR_P (decl) = 1;
458 GFC_DECL_CRAY_POINTEE (decl) = 1;
459 /* This is a fake variable just for debugging purposes. */
460 TREE_ASM_WRITTEN (decl) = 1;
464 /* Finish processing of a declaration without an initial value. */
467 gfc_finish_decl (tree decl)
469 gcc_assert (TREE_CODE (decl) == PARM_DECL
470 || DECL_INITIAL (decl) == NULL_TREE);
472 if (TREE_CODE (decl) != VAR_DECL)
475 if (DECL_SIZE (decl) == NULL_TREE
476 && TYPE_SIZE (TREE_TYPE (decl)) != NULL_TREE)
477 layout_decl (decl, 0);
479 /* A few consistency checks. */
480 /* A static variable with an incomplete type is an error if it is
481 initialized. Also if it is not file scope. Otherwise, let it
482 through, but if it is not `extern' then it may cause an error
484 /* An automatic variable with an incomplete type is an error. */
486 /* We should know the storage size. */
487 gcc_assert (DECL_SIZE (decl) != NULL_TREE
488 || (TREE_STATIC (decl)
489 ? (!DECL_INITIAL (decl) || !DECL_CONTEXT (decl))
490 : DECL_EXTERNAL (decl)));
492 /* The storage size should be constant. */
493 gcc_assert ((!DECL_EXTERNAL (decl) && !TREE_STATIC (decl))
495 || TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST);
499 /* Apply symbol attributes to a variable, and add it to the function scope. */
502 gfc_finish_var_decl (tree decl, gfc_symbol * sym)
505 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
506 This is the equivalent of the TARGET variables.
507 We also need to set this if the variable is passed by reference in a
510 /* Set DECL_VALUE_EXPR for Cray Pointees. */
511 if (sym->attr.cray_pointee)
512 gfc_finish_cray_pointee (decl, sym);
514 if (sym->attr.target)
515 TREE_ADDRESSABLE (decl) = 1;
516 /* If it wasn't used we wouldn't be getting it. */
517 TREE_USED (decl) = 1;
519 /* Chain this decl to the pending declarations. Don't do pushdecl()
520 because this would add them to the current scope rather than the
522 if (current_function_decl != NULL_TREE)
524 if (sym->ns->proc_name->backend_decl == current_function_decl
525 || sym->result == sym)
526 gfc_add_decl_to_function (decl);
527 else if (sym->ns->proc_name->attr.flavor == FL_LABEL)
528 /* This is a BLOCK construct. */
529 add_decl_as_local (decl);
531 gfc_add_decl_to_parent_function (decl);
534 if (sym->attr.cray_pointee)
537 if(sym->attr.is_bind_c == 1)
539 /* We need to put variables that are bind(c) into the common
540 segment of the object file, because this is what C would do.
541 gfortran would typically put them in either the BSS or
542 initialized data segments, and only mark them as common if
543 they were part of common blocks. However, if they are not put
544 into common space, then C cannot initialize global Fortran
545 variables that it interoperates with and the draft says that
546 either Fortran or C should be able to initialize it (but not
547 both, of course.) (J3/04-007, section 15.3). */
548 TREE_PUBLIC(decl) = 1;
549 DECL_COMMON(decl) = 1;
552 /* If a variable is USE associated, it's always external. */
553 if (sym->attr.use_assoc)
555 DECL_EXTERNAL (decl) = 1;
556 TREE_PUBLIC (decl) = 1;
558 else if (sym->module && !sym->attr.result && !sym->attr.dummy)
560 /* TODO: Don't set sym->module for result or dummy variables. */
561 gcc_assert (current_function_decl == NULL_TREE || sym->result == sym);
562 /* This is the declaration of a module variable. */
563 TREE_PUBLIC (decl) = 1;
564 TREE_STATIC (decl) = 1;
567 /* Derived types are a bit peculiar because of the possibility of
568 a default initializer; this must be applied each time the variable
569 comes into scope it therefore need not be static. These variables
570 are SAVE_NONE but have an initializer. Otherwise explicitly
571 initialized variables are SAVE_IMPLICIT and explicitly saved are
573 if (!sym->attr.use_assoc
574 && (sym->attr.save != SAVE_NONE || sym->attr.data
575 || (sym->value && sym->ns->proc_name->attr.is_main_program)))
576 TREE_STATIC (decl) = 1;
578 if (sym->attr.volatile_)
580 TREE_THIS_VOLATILE (decl) = 1;
581 new_type = build_qualified_type (TREE_TYPE (decl), TYPE_QUAL_VOLATILE);
582 TREE_TYPE (decl) = new_type;
585 /* Keep variables larger than max-stack-var-size off stack. */
586 if (!sym->ns->proc_name->attr.recursive
587 && INTEGER_CST_P (DECL_SIZE_UNIT (decl))
588 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
589 /* Put variable length auto array pointers always into stack. */
590 && (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
591 || sym->attr.dimension == 0
592 || sym->as->type != AS_EXPLICIT
594 || sym->attr.allocatable)
595 && !DECL_ARTIFICIAL (decl))
596 TREE_STATIC (decl) = 1;
598 /* Handle threadprivate variables. */
599 if (sym->attr.threadprivate
600 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
601 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
603 if (!sym->attr.target
604 && !sym->attr.pointer
605 && !sym->attr.cray_pointee
606 && !sym->attr.proc_pointer)
607 DECL_RESTRICTED_P (decl) = 1;
611 /* Allocate the lang-specific part of a decl. */
614 gfc_allocate_lang_decl (tree decl)
616 DECL_LANG_SPECIFIC (decl) = ggc_alloc_cleared_lang_decl(sizeof
620 /* Remember a symbol to generate initialization/cleanup code at function
624 gfc_defer_symbol_init (gfc_symbol * sym)
630 /* Don't add a symbol twice. */
634 last = head = sym->ns->proc_name;
637 /* Make sure that setup code for dummy variables which are used in the
638 setup of other variables is generated first. */
641 /* Find the first dummy arg seen after us, or the first non-dummy arg.
642 This is a circular list, so don't go past the head. */
644 && (!p->attr.dummy || p->dummy_order > sym->dummy_order))
650 /* Insert in between last and p. */
656 /* Create an array index type variable with function scope. */
659 create_index_var (const char * pfx, int nest)
663 decl = gfc_create_var_np (gfc_array_index_type, pfx);
665 gfc_add_decl_to_parent_function (decl);
667 gfc_add_decl_to_function (decl);
672 /* Create variables to hold all the non-constant bits of info for a
673 descriptorless array. Remember these in the lang-specific part of the
677 gfc_build_qualified_array (tree decl, gfc_symbol * sym)
683 type = TREE_TYPE (decl);
685 /* We just use the descriptor, if there is one. */
686 if (GFC_DESCRIPTOR_TYPE_P (type))
689 gcc_assert (GFC_ARRAY_TYPE_P (type));
690 nest = (sym->ns->proc_name->backend_decl != current_function_decl)
691 && !sym->attr.contained;
693 for (dim = 0; dim < GFC_TYPE_ARRAY_RANK (type); dim++)
695 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
697 GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
698 TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type, dim)) = 1;
700 /* Don't try to use the unknown bound for assumed shape arrays. */
701 if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
702 && (sym->as->type != AS_ASSUMED_SIZE
703 || dim < GFC_TYPE_ARRAY_RANK (type) - 1))
705 GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
706 TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type, dim)) = 1;
709 if (GFC_TYPE_ARRAY_STRIDE (type, dim) == NULL_TREE)
711 GFC_TYPE_ARRAY_STRIDE (type, dim) = create_index_var ("stride", nest);
712 TREE_NO_WARNING (GFC_TYPE_ARRAY_STRIDE (type, dim)) = 1;
715 if (GFC_TYPE_ARRAY_OFFSET (type) == NULL_TREE)
717 GFC_TYPE_ARRAY_OFFSET (type) = gfc_create_var_np (gfc_array_index_type,
719 TREE_NO_WARNING (GFC_TYPE_ARRAY_OFFSET (type)) = 1;
722 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type));
724 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type));
727 if (GFC_TYPE_ARRAY_SIZE (type) == NULL_TREE
728 && sym->as->type != AS_ASSUMED_SIZE)
730 GFC_TYPE_ARRAY_SIZE (type) = create_index_var ("size", nest);
731 TREE_NO_WARNING (GFC_TYPE_ARRAY_SIZE (type)) = 1;
734 if (POINTER_TYPE_P (type))
736 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type)));
737 gcc_assert (TYPE_LANG_SPECIFIC (type)
738 == TYPE_LANG_SPECIFIC (TREE_TYPE (type)));
739 type = TREE_TYPE (type);
742 if (! COMPLETE_TYPE_P (type) && GFC_TYPE_ARRAY_SIZE (type))
746 size = fold_build2 (MINUS_EXPR, gfc_array_index_type,
747 GFC_TYPE_ARRAY_SIZE (type), gfc_index_one_node);
748 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
750 TYPE_DOMAIN (type) = range;
754 if (TYPE_NAME (type) != NULL_TREE
755 && GFC_TYPE_ARRAY_UBOUND (type, sym->as->rank - 1) != NULL_TREE
756 && TREE_CODE (GFC_TYPE_ARRAY_UBOUND (type, sym->as->rank - 1)) == VAR_DECL)
758 tree gtype = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
760 for (dim = 0; dim < sym->as->rank - 1; dim++)
762 gcc_assert (TREE_CODE (gtype) == ARRAY_TYPE);
763 gtype = TREE_TYPE (gtype);
765 gcc_assert (TREE_CODE (gtype) == ARRAY_TYPE);
766 if (TYPE_MAX_VALUE (TYPE_DOMAIN (gtype)) == NULL)
767 TYPE_NAME (type) = NULL_TREE;
770 if (TYPE_NAME (type) == NULL_TREE)
772 tree gtype = TREE_TYPE (type), rtype, type_decl;
774 for (dim = sym->as->rank - 1; dim >= 0; dim--)
777 lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
778 ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
779 rtype = build_range_type (gfc_array_index_type, lbound, ubound);
780 gtype = build_array_type (gtype, rtype);
781 /* Ensure the bound variables aren't optimized out at -O0.
782 For -O1 and above they often will be optimized out, but
783 can be tracked by VTA. Also clear the artificial
784 lbound.N or ubound.N DECL_NAME, so that it doesn't end up
786 if (lbound && TREE_CODE (lbound) == VAR_DECL
787 && DECL_ARTIFICIAL (lbound) && DECL_IGNORED_P (lbound))
789 if (DECL_NAME (lbound)
790 && strstr (IDENTIFIER_POINTER (DECL_NAME (lbound)),
792 DECL_NAME (lbound) = NULL_TREE;
793 DECL_IGNORED_P (lbound) = 0;
795 if (ubound && TREE_CODE (ubound) == VAR_DECL
796 && DECL_ARTIFICIAL (ubound) && DECL_IGNORED_P (ubound))
798 if (DECL_NAME (ubound)
799 && strstr (IDENTIFIER_POINTER (DECL_NAME (ubound)),
801 DECL_NAME (ubound) = NULL_TREE;
802 DECL_IGNORED_P (ubound) = 0;
805 TYPE_NAME (type) = type_decl = build_decl (input_location,
806 TYPE_DECL, NULL, gtype);
807 DECL_ORIGINAL_TYPE (type_decl) = gtype;
812 /* For some dummy arguments we don't use the actual argument directly.
813 Instead we create a local decl and use that. This allows us to perform
814 initialization, and construct full type information. */
817 gfc_build_dummy_array_decl (gfc_symbol * sym, tree dummy)
827 if (sym->attr.pointer || sym->attr.allocatable)
830 /* Add to list of variables if not a fake result variable. */
831 if (sym->attr.result || sym->attr.dummy)
832 gfc_defer_symbol_init (sym);
834 type = TREE_TYPE (dummy);
835 gcc_assert (TREE_CODE (dummy) == PARM_DECL
836 && POINTER_TYPE_P (type));
838 /* Do we know the element size? */
839 known_size = sym->ts.type != BT_CHARACTER
840 || INTEGER_CST_P (sym->ts.u.cl->backend_decl);
842 if (known_size && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type)))
844 /* For descriptorless arrays with known element size the actual
845 argument is sufficient. */
846 gcc_assert (GFC_ARRAY_TYPE_P (type));
847 gfc_build_qualified_array (dummy, sym);
851 type = TREE_TYPE (type);
852 if (GFC_DESCRIPTOR_TYPE_P (type))
854 /* Create a descriptorless array pointer. */
858 /* Even when -frepack-arrays is used, symbols with TARGET attribute
860 if (!gfc_option.flag_repack_arrays || sym->attr.target)
862 if (as->type == AS_ASSUMED_SIZE)
863 packed = PACKED_FULL;
867 if (as->type == AS_EXPLICIT)
869 packed = PACKED_FULL;
870 for (n = 0; n < as->rank; n++)
874 && as->upper[n]->expr_type == EXPR_CONSTANT
875 && as->lower[n]->expr_type == EXPR_CONSTANT))
876 packed = PACKED_PARTIAL;
880 packed = PACKED_PARTIAL;
883 type = gfc_typenode_for_spec (&sym->ts);
884 type = gfc_get_nodesc_array_type (type, sym->as, packed,
889 /* We now have an expression for the element size, so create a fully
890 qualified type. Reset sym->backend decl or this will just return the
892 DECL_ARTIFICIAL (sym->backend_decl) = 1;
893 sym->backend_decl = NULL_TREE;
894 type = gfc_sym_type (sym);
895 packed = PACKED_FULL;
898 ASM_FORMAT_PRIVATE_NAME (name, IDENTIFIER_POINTER (DECL_NAME (dummy)), 0);
899 decl = build_decl (input_location,
900 VAR_DECL, get_identifier (name), type);
902 DECL_ARTIFICIAL (decl) = 1;
903 TREE_PUBLIC (decl) = 0;
904 TREE_STATIC (decl) = 0;
905 DECL_EXTERNAL (decl) = 0;
907 /* We should never get deferred shape arrays here. We used to because of
909 gcc_assert (sym->as->type != AS_DEFERRED);
911 if (packed == PACKED_PARTIAL)
912 GFC_DECL_PARTIAL_PACKED_ARRAY (decl) = 1;
913 else if (packed == PACKED_FULL)
914 GFC_DECL_PACKED_ARRAY (decl) = 1;
916 gfc_build_qualified_array (decl, sym);
918 if (DECL_LANG_SPECIFIC (dummy))
919 DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (dummy);
921 gfc_allocate_lang_decl (decl);
923 GFC_DECL_SAVED_DESCRIPTOR (decl) = dummy;
925 if (sym->ns->proc_name->backend_decl == current_function_decl
926 || sym->attr.contained)
927 gfc_add_decl_to_function (decl);
929 gfc_add_decl_to_parent_function (decl);
934 /* For symbol SYM with GFC_DECL_SAVED_DESCRIPTOR used in contained
935 function add a VAR_DECL to the current function with DECL_VALUE_EXPR
936 pointing to the artificial variable for debug info purposes. */
939 gfc_nonlocal_dummy_array_decl (gfc_symbol *sym)
943 if (! nonlocal_dummy_decl_pset)
944 nonlocal_dummy_decl_pset = pointer_set_create ();
946 if (pointer_set_insert (nonlocal_dummy_decl_pset, sym->backend_decl))
949 dummy = GFC_DECL_SAVED_DESCRIPTOR (sym->backend_decl);
950 decl = build_decl (input_location, VAR_DECL, DECL_NAME (dummy),
951 TREE_TYPE (sym->backend_decl));
952 DECL_ARTIFICIAL (decl) = 0;
953 TREE_USED (decl) = 1;
954 TREE_PUBLIC (decl) = 0;
955 TREE_STATIC (decl) = 0;
956 DECL_EXTERNAL (decl) = 0;
957 if (DECL_BY_REFERENCE (dummy))
958 DECL_BY_REFERENCE (decl) = 1;
959 DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (sym->backend_decl);
960 SET_DECL_VALUE_EXPR (decl, sym->backend_decl);
961 DECL_HAS_VALUE_EXPR_P (decl) = 1;
962 DECL_CONTEXT (decl) = DECL_CONTEXT (sym->backend_decl);
963 TREE_CHAIN (decl) = nonlocal_dummy_decls;
964 nonlocal_dummy_decls = decl;
967 /* Return a constant or a variable to use as a string length. Does not
968 add the decl to the current scope. */
971 gfc_create_string_length (gfc_symbol * sym)
973 gcc_assert (sym->ts.u.cl);
974 gfc_conv_const_charlen (sym->ts.u.cl);
976 if (sym->ts.u.cl->backend_decl == NULL_TREE)
979 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
981 /* Also prefix the mangled name. */
982 strcpy (&name[1], sym->name);
984 length = build_decl (input_location,
985 VAR_DECL, get_identifier (name),
986 gfc_charlen_type_node);
987 DECL_ARTIFICIAL (length) = 1;
988 TREE_USED (length) = 1;
989 if (sym->ns->proc_name->tlink != NULL)
990 gfc_defer_symbol_init (sym);
992 sym->ts.u.cl->backend_decl = length;
995 gcc_assert (sym->ts.u.cl->backend_decl != NULL_TREE);
996 return sym->ts.u.cl->backend_decl;
999 /* If a variable is assigned a label, we add another two auxiliary
1003 gfc_add_assign_aux_vars (gfc_symbol * sym)
1009 gcc_assert (sym->backend_decl);
1011 decl = sym->backend_decl;
1012 gfc_allocate_lang_decl (decl);
1013 GFC_DECL_ASSIGN (decl) = 1;
1014 length = build_decl (input_location,
1015 VAR_DECL, create_tmp_var_name (sym->name),
1016 gfc_charlen_type_node);
1017 addr = build_decl (input_location,
1018 VAR_DECL, create_tmp_var_name (sym->name),
1020 gfc_finish_var_decl (length, sym);
1021 gfc_finish_var_decl (addr, sym);
1022 /* STRING_LENGTH is also used as flag. Less than -1 means that
1023 ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the
1024 target label's address. Otherwise, value is the length of a format string
1025 and ASSIGN_ADDR is its address. */
1026 if (TREE_STATIC (length))
1027 DECL_INITIAL (length) = build_int_cst (NULL_TREE, -2);
1029 gfc_defer_symbol_init (sym);
1031 GFC_DECL_STRING_LEN (decl) = length;
1032 GFC_DECL_ASSIGN_ADDR (decl) = addr;
1037 add_attributes_to_decl (symbol_attribute sym_attr, tree list)
1042 for (id = 0; id < EXT_ATTR_NUM; id++)
1043 if (sym_attr.ext_attr & (1 << id))
1045 attr = build_tree_list (
1046 get_identifier (ext_attr_list[id].middle_end_name),
1048 list = chainon (list, attr);
1055 /* Return the decl for a gfc_symbol, create it if it doesn't already
1059 gfc_get_symbol_decl (gfc_symbol * sym)
1062 tree length = NULL_TREE;
1066 gcc_assert (sym->attr.referenced
1067 || sym->attr.use_assoc
1068 || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY);
1070 if (sym->ns && sym->ns->proc_name && sym->ns->proc_name->attr.function)
1071 byref = gfc_return_by_reference (sym->ns->proc_name);
1075 /* Make sure that the vtab for the declared type is completed. */
1076 if (sym->ts.type == BT_CLASS)
1078 gfc_component *c = CLASS_DATA (sym);
1079 if (!c->ts.u.derived->backend_decl)
1080 gfc_find_derived_vtab (c->ts.u.derived, true);
1083 if ((sym->attr.dummy && ! sym->attr.function) || (sym->attr.result && byref))
1085 /* Return via extra parameter. */
1086 if (sym->attr.result && byref
1087 && !sym->backend_decl)
1090 DECL_ARGUMENTS (sym->ns->proc_name->backend_decl);
1091 /* For entry master function skip over the __entry
1093 if (sym->ns->proc_name->attr.entry_master)
1094 sym->backend_decl = TREE_CHAIN (sym->backend_decl);
1097 /* Dummy variables should already have been created. */
1098 gcc_assert (sym->backend_decl);
1100 /* Create a character length variable. */
1101 if (sym->ts.type == BT_CHARACTER)
1103 if (sym->ts.u.cl->backend_decl == NULL_TREE)
1104 length = gfc_create_string_length (sym);
1106 length = sym->ts.u.cl->backend_decl;
1107 if (TREE_CODE (length) == VAR_DECL
1108 && DECL_CONTEXT (length) == NULL_TREE)
1110 /* Add the string length to the same context as the symbol. */
1111 if (DECL_CONTEXT (sym->backend_decl) == current_function_decl)
1112 gfc_add_decl_to_function (length);
1114 gfc_add_decl_to_parent_function (length);
1116 gcc_assert (DECL_CONTEXT (sym->backend_decl) ==
1117 DECL_CONTEXT (length));
1119 gfc_defer_symbol_init (sym);
1123 /* Use a copy of the descriptor for dummy arrays. */
1124 if (sym->attr.dimension && !TREE_USED (sym->backend_decl))
1126 decl = gfc_build_dummy_array_decl (sym, sym->backend_decl);
1127 /* Prevent the dummy from being detected as unused if it is copied. */
1128 if (sym->backend_decl != NULL && decl != sym->backend_decl)
1129 DECL_ARTIFICIAL (sym->backend_decl) = 1;
1130 sym->backend_decl = decl;
1133 TREE_USED (sym->backend_decl) = 1;
1134 if (sym->attr.assign && GFC_DECL_ASSIGN (sym->backend_decl) == 0)
1136 gfc_add_assign_aux_vars (sym);
1139 if (sym->attr.dimension
1140 && DECL_LANG_SPECIFIC (sym->backend_decl)
1141 && GFC_DECL_SAVED_DESCRIPTOR (sym->backend_decl)
1142 && DECL_CONTEXT (sym->backend_decl) != current_function_decl)
1143 gfc_nonlocal_dummy_array_decl (sym);
1145 return sym->backend_decl;
1148 if (sym->backend_decl)
1149 return sym->backend_decl;
1151 /* If use associated and whole file compilation, use the module
1152 declaration. This is only needed for intrinsic types because
1153 they are substituted for one another during optimization. */
1154 if (gfc_option.flag_whole_file
1155 && sym->attr.flavor == FL_VARIABLE
1156 && sym->ts.type != BT_DERIVED
1157 && sym->attr.use_assoc
1162 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->module);
1163 if (gsym && gsym->ns && gsym->type == GSYM_MODULE)
1167 gfc_find_symbol (sym->name, gsym->ns, 0, &s);
1168 if (s && s->backend_decl)
1170 if (sym->ts.type == BT_CHARACTER)
1171 sym->ts.u.cl->backend_decl = s->ts.u.cl->backend_decl;
1172 return s->backend_decl;
1177 /* Catch function declarations. Only used for actual parameters and
1178 procedure pointers. */
1179 if (sym->attr.flavor == FL_PROCEDURE)
1181 decl = gfc_get_extern_function_decl (sym);
1182 gfc_set_decl_location (decl, &sym->declared_at);
1186 if (sym->attr.intrinsic)
1187 internal_error ("intrinsic variable which isn't a procedure");
1189 /* Create string length decl first so that they can be used in the
1190 type declaration. */
1191 if (sym->ts.type == BT_CHARACTER)
1192 length = gfc_create_string_length (sym);
1194 /* Create the decl for the variable. */
1195 decl = build_decl (sym->declared_at.lb->location,
1196 VAR_DECL, gfc_sym_identifier (sym), gfc_sym_type (sym));
1198 /* Add attributes to variables. Functions are handled elsewhere. */
1199 attributes = add_attributes_to_decl (sym->attr, NULL_TREE);
1200 decl_attributes (&decl, attributes, 0);
1202 /* Symbols from modules should have their assembler names mangled.
1203 This is done here rather than in gfc_finish_var_decl because it
1204 is different for string length variables. */
1207 gfc_set_decl_assembler_name (decl, gfc_sym_mangled_identifier (sym));
1208 if (sym->attr.use_assoc)
1209 DECL_IGNORED_P (decl) = 1;
1212 if (sym->attr.dimension)
1214 /* Create variables to hold the non-constant bits of array info. */
1215 gfc_build_qualified_array (decl, sym);
1217 if (sym->attr.contiguous
1218 || ((sym->attr.allocatable || !sym->attr.dummy) && !sym->attr.pointer))
1219 GFC_DECL_PACKED_ARRAY (decl) = 1;
1222 /* Remember this variable for allocation/cleanup. */
1223 if (sym->attr.dimension || sym->attr.allocatable
1224 || (sym->ts.type == BT_CLASS &&
1225 (CLASS_DATA (sym)->attr.dimension
1226 || CLASS_DATA (sym)->attr.allocatable))
1227 || (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.alloc_comp)
1228 /* This applies a derived type default initializer. */
1229 || (sym->ts.type == BT_DERIVED
1230 && sym->attr.save == SAVE_NONE
1232 && !sym->attr.allocatable
1233 && (sym->value && !sym->ns->proc_name->attr.is_main_program)
1234 && !sym->attr.use_assoc))
1235 gfc_defer_symbol_init (sym);
1237 gfc_finish_var_decl (decl, sym);
1239 if (sym->ts.type == BT_CHARACTER)
1241 /* Character variables need special handling. */
1242 gfc_allocate_lang_decl (decl);
1244 if (TREE_CODE (length) != INTEGER_CST)
1246 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
1250 /* Also prefix the mangled name for symbols from modules. */
1251 strcpy (&name[1], sym->name);
1254 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (length)));
1255 gfc_set_decl_assembler_name (decl, get_identifier (name));
1257 gfc_finish_var_decl (length, sym);
1258 gcc_assert (!sym->value);
1261 else if (sym->attr.subref_array_pointer)
1263 /* We need the span for these beasts. */
1264 gfc_allocate_lang_decl (decl);
1267 if (sym->attr.subref_array_pointer)
1270 GFC_DECL_SUBREF_ARRAY_P (decl) = 1;
1271 span = build_decl (input_location,
1272 VAR_DECL, create_tmp_var_name ("span"),
1273 gfc_array_index_type);
1274 gfc_finish_var_decl (span, sym);
1275 TREE_STATIC (span) = TREE_STATIC (decl);
1276 DECL_ARTIFICIAL (span) = 1;
1277 DECL_INITIAL (span) = build_int_cst (gfc_array_index_type, 0);
1279 GFC_DECL_SPAN (decl) = span;
1280 GFC_TYPE_ARRAY_SPAN (TREE_TYPE (decl)) = span;
1283 sym->backend_decl = decl;
1285 if (sym->attr.assign)
1286 gfc_add_assign_aux_vars (sym);
1288 if (TREE_STATIC (decl) && !sym->attr.use_assoc
1289 && (sym->attr.save || sym->ns->proc_name->attr.is_main_program
1290 || gfc_option.flag_max_stack_var_size == 0
1291 || sym->attr.data || sym->ns->proc_name->attr.flavor == FL_MODULE))
1293 /* Add static initializer. For procedures, it is only needed if
1294 SAVE is specified otherwise they need to be reinitialized
1295 every time the procedure is entered. The TREE_STATIC is
1296 in this case due to -fmax-stack-var-size=. */
1297 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
1298 TREE_TYPE (decl), sym->attr.dimension,
1299 sym->attr.pointer || sym->attr.allocatable);
1302 if (!TREE_STATIC (decl)
1303 && POINTER_TYPE_P (TREE_TYPE (decl))
1304 && !sym->attr.pointer
1305 && !sym->attr.allocatable
1306 && !sym->attr.proc_pointer)
1307 DECL_BY_REFERENCE (decl) = 1;
1313 /* Substitute a temporary variable in place of the real one. */
1316 gfc_shadow_sym (gfc_symbol * sym, tree decl, gfc_saved_var * save)
1318 save->attr = sym->attr;
1319 save->decl = sym->backend_decl;
1321 gfc_clear_attr (&sym->attr);
1322 sym->attr.referenced = 1;
1323 sym->attr.flavor = FL_VARIABLE;
1325 sym->backend_decl = decl;
1329 /* Restore the original variable. */
1332 gfc_restore_sym (gfc_symbol * sym, gfc_saved_var * save)
1334 sym->attr = save->attr;
1335 sym->backend_decl = save->decl;
1339 /* Declare a procedure pointer. */
1342 get_proc_pointer_decl (gfc_symbol *sym)
1347 decl = sym->backend_decl;
1351 decl = build_decl (input_location,
1352 VAR_DECL, get_identifier (sym->name),
1353 build_pointer_type (gfc_get_function_type (sym)));
1355 if ((sym->ns->proc_name
1356 && sym->ns->proc_name->backend_decl == current_function_decl)
1357 || sym->attr.contained)
1358 gfc_add_decl_to_function (decl);
1359 else if (sym->ns->proc_name->attr.flavor != FL_MODULE)
1360 gfc_add_decl_to_parent_function (decl);
1362 sym->backend_decl = decl;
1364 /* If a variable is USE associated, it's always external. */
1365 if (sym->attr.use_assoc)
1367 DECL_EXTERNAL (decl) = 1;
1368 TREE_PUBLIC (decl) = 1;
1370 else if (sym->module && sym->ns->proc_name->attr.flavor == FL_MODULE)
1372 /* This is the declaration of a module variable. */
1373 TREE_PUBLIC (decl) = 1;
1374 TREE_STATIC (decl) = 1;
1377 if (!sym->attr.use_assoc
1378 && (sym->attr.save != SAVE_NONE || sym->attr.data
1379 || (sym->value && sym->ns->proc_name->attr.is_main_program)))
1380 TREE_STATIC (decl) = 1;
1382 if (TREE_STATIC (decl) && sym->value)
1384 /* Add static initializer. */
1385 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
1387 sym->attr.proc_pointer ? false : sym->attr.dimension,
1388 sym->attr.proc_pointer);
1391 attributes = add_attributes_to_decl (sym->attr, NULL_TREE);
1392 decl_attributes (&decl, attributes, 0);
1398 /* Get a basic decl for an external function. */
1401 gfc_get_extern_function_decl (gfc_symbol * sym)
1407 gfc_intrinsic_sym *isym;
1409 char s[GFC_MAX_SYMBOL_LEN + 23]; /* "_gfortran_f2c_specific" and '\0'. */
1414 if (sym->backend_decl)
1415 return sym->backend_decl;
1417 /* We should never be creating external decls for alternate entry points.
1418 The procedure may be an alternate entry point, but we don't want/need
1420 gcc_assert (!(sym->attr.entry || sym->attr.entry_master));
1422 if (sym->attr.proc_pointer)
1423 return get_proc_pointer_decl (sym);
1425 /* See if this is an external procedure from the same file. If so,
1426 return the backend_decl. */
1427 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->name);
1429 if (gfc_option.flag_whole_file
1430 && !sym->attr.use_assoc
1431 && !sym->backend_decl
1433 && ((gsym->type == GSYM_SUBROUTINE) || (gsym->type == GSYM_FUNCTION))
1434 && gsym->ns->proc_name->backend_decl)
1436 /* If the namespace has entries, the proc_name is the
1437 entry master. Find the entry and use its backend_decl.
1438 otherwise, use the proc_name backend_decl. */
1439 if (gsym->ns->entries)
1441 gfc_entry_list *entry = gsym->ns->entries;
1443 for (; entry; entry = entry->next)
1445 if (strcmp (gsym->name, entry->sym->name) == 0)
1447 sym->backend_decl = entry->sym->backend_decl;
1454 sym->backend_decl = gsym->ns->proc_name->backend_decl;
1457 if (sym->backend_decl)
1458 return sym->backend_decl;
1461 /* See if this is a module procedure from the same file. If so,
1462 return the backend_decl. */
1464 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->module);
1466 if (gfc_option.flag_whole_file
1468 && gsym->type == GSYM_MODULE)
1473 gfc_find_symbol (sym->name, gsym->ns, 0, &s);
1474 if (s && s->backend_decl)
1476 sym->backend_decl = s->backend_decl;
1477 return sym->backend_decl;
1481 if (sym->attr.intrinsic)
1483 /* Call the resolution function to get the actual name. This is
1484 a nasty hack which relies on the resolution functions only looking
1485 at the first argument. We pass NULL for the second argument
1486 otherwise things like AINT get confused. */
1487 isym = gfc_find_function (sym->name);
1488 gcc_assert (isym->resolve.f0 != NULL);
1490 memset (&e, 0, sizeof (e));
1491 e.expr_type = EXPR_FUNCTION;
1493 memset (&argexpr, 0, sizeof (argexpr));
1494 gcc_assert (isym->formal);
1495 argexpr.ts = isym->formal->ts;
1497 if (isym->formal->next == NULL)
1498 isym->resolve.f1 (&e, &argexpr);
1501 if (isym->formal->next->next == NULL)
1502 isym->resolve.f2 (&e, &argexpr, NULL);
1505 if (isym->formal->next->next->next == NULL)
1506 isym->resolve.f3 (&e, &argexpr, NULL, NULL);
1509 /* All specific intrinsics take less than 5 arguments. */
1510 gcc_assert (isym->formal->next->next->next->next == NULL);
1511 isym->resolve.f4 (&e, &argexpr, NULL, NULL, NULL);
1516 if (gfc_option.flag_f2c
1517 && ((e.ts.type == BT_REAL && e.ts.kind == gfc_default_real_kind)
1518 || e.ts.type == BT_COMPLEX))
1520 /* Specific which needs a different implementation if f2c
1521 calling conventions are used. */
1522 sprintf (s, "_gfortran_f2c_specific%s", e.value.function.name);
1525 sprintf (s, "_gfortran_specific%s", e.value.function.name);
1527 name = get_identifier (s);
1528 mangled_name = name;
1532 name = gfc_sym_identifier (sym);
1533 mangled_name = gfc_sym_mangled_function_id (sym);
1536 type = gfc_get_function_type (sym);
1537 fndecl = build_decl (input_location,
1538 FUNCTION_DECL, name, type);
1540 attributes = add_attributes_to_decl (sym->attr, NULL_TREE);
1541 decl_attributes (&fndecl, attributes, 0);
1543 gfc_set_decl_assembler_name (fndecl, mangled_name);
1545 /* Set the context of this decl. */
1546 if (0 && sym->ns && sym->ns->proc_name)
1548 /* TODO: Add external decls to the appropriate scope. */
1549 DECL_CONTEXT (fndecl) = sym->ns->proc_name->backend_decl;
1553 /* Global declaration, e.g. intrinsic subroutine. */
1554 DECL_CONTEXT (fndecl) = NULL_TREE;
1557 DECL_EXTERNAL (fndecl) = 1;
1559 /* This specifies if a function is globally addressable, i.e. it is
1560 the opposite of declaring static in C. */
1561 TREE_PUBLIC (fndecl) = 1;
1563 /* Set attributes for PURE functions. A call to PURE function in the
1564 Fortran 95 sense is both pure and without side effects in the C
1566 if (sym->attr.pure || sym->attr.elemental)
1568 if (sym->attr.function && !gfc_return_by_reference (sym))
1569 DECL_PURE_P (fndecl) = 1;
1570 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
1571 parameters and don't use alternate returns (is this
1572 allowed?). In that case, calls to them are meaningless, and
1573 can be optimized away. See also in build_function_decl(). */
1574 TREE_SIDE_EFFECTS (fndecl) = 0;
1577 /* Mark non-returning functions. */
1578 if (sym->attr.noreturn)
1579 TREE_THIS_VOLATILE(fndecl) = 1;
1581 sym->backend_decl = fndecl;
1583 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1584 pushdecl_top_level (fndecl);
1590 /* Create a declaration for a procedure. For external functions (in the C
1591 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
1592 a master function with alternate entry points. */
1595 build_function_decl (gfc_symbol * sym)
1597 tree fndecl, type, attributes;
1598 symbol_attribute attr;
1600 gfc_formal_arglist *f;
1602 gcc_assert (!sym->backend_decl);
1603 gcc_assert (!sym->attr.external);
1605 /* Set the line and filename. sym->declared_at seems to point to the
1606 last statement for subroutines, but it'll do for now. */
1607 gfc_set_backend_locus (&sym->declared_at);
1609 /* Allow only one nesting level. Allow public declarations. */
1610 gcc_assert (current_function_decl == NULL_TREE
1611 || DECL_CONTEXT (current_function_decl) == NULL_TREE
1612 || TREE_CODE (DECL_CONTEXT (current_function_decl))
1615 type = gfc_get_function_type (sym);
1616 fndecl = build_decl (input_location,
1617 FUNCTION_DECL, gfc_sym_identifier (sym), type);
1621 attributes = add_attributes_to_decl (attr, NULL_TREE);
1622 decl_attributes (&fndecl, attributes, 0);
1624 /* Perform name mangling if this is a top level or module procedure. */
1625 if (current_function_decl == NULL_TREE)
1626 gfc_set_decl_assembler_name (fndecl, gfc_sym_mangled_function_id (sym));
1628 /* Figure out the return type of the declared function, and build a
1629 RESULT_DECL for it. If this is a subroutine with alternate
1630 returns, build a RESULT_DECL for it. */
1631 result_decl = NULL_TREE;
1632 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
1635 if (gfc_return_by_reference (sym))
1636 type = void_type_node;
1639 if (sym->result != sym)
1640 result_decl = gfc_sym_identifier (sym->result);
1642 type = TREE_TYPE (TREE_TYPE (fndecl));
1647 /* Look for alternate return placeholders. */
1648 int has_alternate_returns = 0;
1649 for (f = sym->formal; f; f = f->next)
1653 has_alternate_returns = 1;
1658 if (has_alternate_returns)
1659 type = integer_type_node;
1661 type = void_type_node;
1664 result_decl = build_decl (input_location,
1665 RESULT_DECL, result_decl, type);
1666 DECL_ARTIFICIAL (result_decl) = 1;
1667 DECL_IGNORED_P (result_decl) = 1;
1668 DECL_CONTEXT (result_decl) = fndecl;
1669 DECL_RESULT (fndecl) = result_decl;
1671 /* Don't call layout_decl for a RESULT_DECL.
1672 layout_decl (result_decl, 0); */
1674 /* Set up all attributes for the function. */
1675 DECL_CONTEXT (fndecl) = current_function_decl;
1676 DECL_EXTERNAL (fndecl) = 0;
1678 /* This specifies if a function is globally visible, i.e. it is
1679 the opposite of declaring static in C. */
1680 if (DECL_CONTEXT (fndecl) == NULL_TREE
1681 && !sym->attr.entry_master && !sym->attr.is_main_program)
1682 TREE_PUBLIC (fndecl) = 1;
1684 /* TREE_STATIC means the function body is defined here. */
1685 TREE_STATIC (fndecl) = 1;
1687 /* Set attributes for PURE functions. A call to a PURE function in the
1688 Fortran 95 sense is both pure and without side effects in the C
1690 if (attr.pure || attr.elemental)
1692 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
1693 including an alternate return. In that case it can also be
1694 marked as PURE. See also in gfc_get_extern_function_decl(). */
1695 if (attr.function && !gfc_return_by_reference (sym))
1696 DECL_PURE_P (fndecl) = 1;
1697 TREE_SIDE_EFFECTS (fndecl) = 0;
1701 /* Layout the function declaration and put it in the binding level
1702 of the current function. */
1705 sym->backend_decl = fndecl;
1709 /* Create the DECL_ARGUMENTS for a procedure. */
1712 create_function_arglist (gfc_symbol * sym)
1715 gfc_formal_arglist *f;
1716 tree typelist, hidden_typelist;
1717 tree arglist, hidden_arglist;
1721 fndecl = sym->backend_decl;
1723 /* Build formal argument list. Make sure that their TREE_CONTEXT is
1724 the new FUNCTION_DECL node. */
1725 arglist = NULL_TREE;
1726 hidden_arglist = NULL_TREE;
1727 typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1729 if (sym->attr.entry_master)
1731 type = TREE_VALUE (typelist);
1732 parm = build_decl (input_location,
1733 PARM_DECL, get_identifier ("__entry"), type);
1735 DECL_CONTEXT (parm) = fndecl;
1736 DECL_ARG_TYPE (parm) = type;
1737 TREE_READONLY (parm) = 1;
1738 gfc_finish_decl (parm);
1739 DECL_ARTIFICIAL (parm) = 1;
1741 arglist = chainon (arglist, parm);
1742 typelist = TREE_CHAIN (typelist);
1745 if (gfc_return_by_reference (sym))
1747 tree type = TREE_VALUE (typelist), length = NULL;
1749 if (sym->ts.type == BT_CHARACTER)
1751 /* Length of character result. */
1752 tree len_type = TREE_VALUE (TREE_CHAIN (typelist));
1753 gcc_assert (len_type == gfc_charlen_type_node);
1755 length = build_decl (input_location,
1757 get_identifier (".__result"),
1759 if (!sym->ts.u.cl->length)
1761 sym->ts.u.cl->backend_decl = length;
1762 TREE_USED (length) = 1;
1764 gcc_assert (TREE_CODE (length) == PARM_DECL);
1765 DECL_CONTEXT (length) = fndecl;
1766 DECL_ARG_TYPE (length) = len_type;
1767 TREE_READONLY (length) = 1;
1768 DECL_ARTIFICIAL (length) = 1;
1769 gfc_finish_decl (length);
1770 if (sym->ts.u.cl->backend_decl == NULL
1771 || sym->ts.u.cl->backend_decl == length)
1776 if (sym->ts.u.cl->backend_decl == NULL)
1778 tree len = build_decl (input_location,
1780 get_identifier ("..__result"),
1781 gfc_charlen_type_node);
1782 DECL_ARTIFICIAL (len) = 1;
1783 TREE_USED (len) = 1;
1784 sym->ts.u.cl->backend_decl = len;
1787 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1788 arg = sym->result ? sym->result : sym;
1789 backend_decl = arg->backend_decl;
1790 /* Temporary clear it, so that gfc_sym_type creates complete
1792 arg->backend_decl = NULL;
1793 type = gfc_sym_type (arg);
1794 arg->backend_decl = backend_decl;
1795 type = build_reference_type (type);
1799 parm = build_decl (input_location,
1800 PARM_DECL, get_identifier ("__result"), type);
1802 DECL_CONTEXT (parm) = fndecl;
1803 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
1804 TREE_READONLY (parm) = 1;
1805 DECL_ARTIFICIAL (parm) = 1;
1806 gfc_finish_decl (parm);
1808 arglist = chainon (arglist, parm);
1809 typelist = TREE_CHAIN (typelist);
1811 if (sym->ts.type == BT_CHARACTER)
1813 gfc_allocate_lang_decl (parm);
1814 arglist = chainon (arglist, length);
1815 typelist = TREE_CHAIN (typelist);
1819 hidden_typelist = typelist;
1820 for (f = sym->formal; f; f = f->next)
1821 if (f->sym != NULL) /* Ignore alternate returns. */
1822 hidden_typelist = TREE_CHAIN (hidden_typelist);
1824 for (f = sym->formal; f; f = f->next)
1826 char name[GFC_MAX_SYMBOL_LEN + 2];
1828 /* Ignore alternate returns. */
1832 type = TREE_VALUE (typelist);
1834 if (f->sym->ts.type == BT_CHARACTER
1835 && (!sym->attr.is_bind_c || sym->attr.entry_master))
1837 tree len_type = TREE_VALUE (hidden_typelist);
1838 tree length = NULL_TREE;
1839 gcc_assert (len_type == gfc_charlen_type_node);
1841 strcpy (&name[1], f->sym->name);
1843 length = build_decl (input_location,
1844 PARM_DECL, get_identifier (name), len_type);
1846 hidden_arglist = chainon (hidden_arglist, length);
1847 DECL_CONTEXT (length) = fndecl;
1848 DECL_ARTIFICIAL (length) = 1;
1849 DECL_ARG_TYPE (length) = len_type;
1850 TREE_READONLY (length) = 1;
1851 gfc_finish_decl (length);
1853 /* Remember the passed value. */
1854 if (f->sym->ts.u.cl->passed_length != NULL)
1856 /* This can happen if the same type is used for multiple
1857 arguments. We need to copy cl as otherwise
1858 cl->passed_length gets overwritten. */
1859 f->sym->ts.u.cl = gfc_new_charlen (f->sym->ns, f->sym->ts.u.cl);
1861 f->sym->ts.u.cl->passed_length = length;
1863 /* Use the passed value for assumed length variables. */
1864 if (!f->sym->ts.u.cl->length)
1866 TREE_USED (length) = 1;
1867 gcc_assert (!f->sym->ts.u.cl->backend_decl);
1868 f->sym->ts.u.cl->backend_decl = length;
1871 hidden_typelist = TREE_CHAIN (hidden_typelist);
1873 if (f->sym->ts.u.cl->backend_decl == NULL
1874 || f->sym->ts.u.cl->backend_decl == length)
1876 if (f->sym->ts.u.cl->backend_decl == NULL)
1877 gfc_create_string_length (f->sym);
1879 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1880 if (f->sym->attr.flavor == FL_PROCEDURE)
1881 type = build_pointer_type (gfc_get_function_type (f->sym));
1883 type = gfc_sym_type (f->sym);
1887 /* For non-constant length array arguments, make sure they use
1888 a different type node from TYPE_ARG_TYPES type. */
1889 if (f->sym->attr.dimension
1890 && type == TREE_VALUE (typelist)
1891 && TREE_CODE (type) == POINTER_TYPE
1892 && GFC_ARRAY_TYPE_P (type)
1893 && f->sym->as->type != AS_ASSUMED_SIZE
1894 && ! COMPLETE_TYPE_P (TREE_TYPE (type)))
1896 if (f->sym->attr.flavor == FL_PROCEDURE)
1897 type = build_pointer_type (gfc_get_function_type (f->sym));
1899 type = gfc_sym_type (f->sym);
1902 if (f->sym->attr.proc_pointer)
1903 type = build_pointer_type (type);
1905 /* Build the argument declaration. */
1906 parm = build_decl (input_location,
1907 PARM_DECL, gfc_sym_identifier (f->sym), type);
1909 /* Fill in arg stuff. */
1910 DECL_CONTEXT (parm) = fndecl;
1911 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
1912 /* All implementation args are read-only. */
1913 TREE_READONLY (parm) = 1;
1914 if (POINTER_TYPE_P (type)
1915 && (!f->sym->attr.proc_pointer
1916 && f->sym->attr.flavor != FL_PROCEDURE))
1917 DECL_BY_REFERENCE (parm) = 1;
1919 gfc_finish_decl (parm);
1921 f->sym->backend_decl = parm;
1923 arglist = chainon (arglist, parm);
1924 typelist = TREE_CHAIN (typelist);
1927 /* Add the hidden string length parameters, unless the procedure
1929 if (!sym->attr.is_bind_c)
1930 arglist = chainon (arglist, hidden_arglist);
1932 gcc_assert (hidden_typelist == NULL_TREE
1933 || TREE_VALUE (hidden_typelist) == void_type_node);
1934 DECL_ARGUMENTS (fndecl) = arglist;
1937 /* Do the setup necessary before generating the body of a function. */
1940 trans_function_start (gfc_symbol * sym)
1944 fndecl = sym->backend_decl;
1946 /* Let GCC know the current scope is this function. */
1947 current_function_decl = fndecl;
1949 /* Let the world know what we're about to do. */
1950 announce_function (fndecl);
1952 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1954 /* Create RTL for function declaration. */
1955 rest_of_decl_compilation (fndecl, 1, 0);
1958 /* Create RTL for function definition. */
1959 make_decl_rtl (fndecl);
1961 init_function_start (fndecl);
1963 /* Even though we're inside a function body, we still don't want to
1964 call expand_expr to calculate the size of a variable-sized array.
1965 We haven't necessarily assigned RTL to all variables yet, so it's
1966 not safe to try to expand expressions involving them. */
1967 cfun->dont_save_pending_sizes_p = 1;
1969 /* function.c requires a push at the start of the function. */
1973 /* Create thunks for alternate entry points. */
1976 build_entry_thunks (gfc_namespace * ns)
1978 gfc_formal_arglist *formal;
1979 gfc_formal_arglist *thunk_formal;
1981 gfc_symbol *thunk_sym;
1989 /* This should always be a toplevel function. */
1990 gcc_assert (current_function_decl == NULL_TREE);
1992 gfc_get_backend_locus (&old_loc);
1993 for (el = ns->entries; el; el = el->next)
1995 thunk_sym = el->sym;
1997 build_function_decl (thunk_sym);
1998 create_function_arglist (thunk_sym);
2000 trans_function_start (thunk_sym);
2002 thunk_fndecl = thunk_sym->backend_decl;
2004 gfc_init_block (&body);
2006 /* Pass extra parameter identifying this entry point. */
2007 tmp = build_int_cst (gfc_array_index_type, el->id);
2008 args = tree_cons (NULL_TREE, tmp, NULL_TREE);
2009 string_args = NULL_TREE;
2011 if (thunk_sym->attr.function)
2013 if (gfc_return_by_reference (ns->proc_name))
2015 tree ref = DECL_ARGUMENTS (current_function_decl);
2016 args = tree_cons (NULL_TREE, ref, args);
2017 if (ns->proc_name->ts.type == BT_CHARACTER)
2018 args = tree_cons (NULL_TREE, TREE_CHAIN (ref),
2023 for (formal = ns->proc_name->formal; formal; formal = formal->next)
2025 /* Ignore alternate returns. */
2026 if (formal->sym == NULL)
2029 /* We don't have a clever way of identifying arguments, so resort to
2030 a brute-force search. */
2031 for (thunk_formal = thunk_sym->formal;
2033 thunk_formal = thunk_formal->next)
2035 if (thunk_formal->sym == formal->sym)
2041 /* Pass the argument. */
2042 DECL_ARTIFICIAL (thunk_formal->sym->backend_decl) = 1;
2043 args = tree_cons (NULL_TREE, thunk_formal->sym->backend_decl,
2045 if (formal->sym->ts.type == BT_CHARACTER)
2047 tmp = thunk_formal->sym->ts.u.cl->backend_decl;
2048 string_args = tree_cons (NULL_TREE, tmp, string_args);
2053 /* Pass NULL for a missing argument. */
2054 args = tree_cons (NULL_TREE, null_pointer_node, args);
2055 if (formal->sym->ts.type == BT_CHARACTER)
2057 tmp = build_int_cst (gfc_charlen_type_node, 0);
2058 string_args = tree_cons (NULL_TREE, tmp, string_args);
2063 /* Call the master function. */
2064 args = nreverse (args);
2065 args = chainon (args, nreverse (string_args));
2066 tmp = ns->proc_name->backend_decl;
2067 tmp = build_function_call_expr (input_location, tmp, args);
2068 if (ns->proc_name->attr.mixed_entry_master)
2070 tree union_decl, field;
2071 tree master_type = TREE_TYPE (ns->proc_name->backend_decl);
2073 union_decl = build_decl (input_location,
2074 VAR_DECL, get_identifier ("__result"),
2075 TREE_TYPE (master_type));
2076 DECL_ARTIFICIAL (union_decl) = 1;
2077 DECL_EXTERNAL (union_decl) = 0;
2078 TREE_PUBLIC (union_decl) = 0;
2079 TREE_USED (union_decl) = 1;
2080 layout_decl (union_decl, 0);
2081 pushdecl (union_decl);
2083 DECL_CONTEXT (union_decl) = current_function_decl;
2084 tmp = fold_build2 (MODIFY_EXPR, TREE_TYPE (union_decl),
2086 gfc_add_expr_to_block (&body, tmp);
2088 for (field = TYPE_FIELDS (TREE_TYPE (union_decl));
2089 field; field = TREE_CHAIN (field))
2090 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
2091 thunk_sym->result->name) == 0)
2093 gcc_assert (field != NULL_TREE);
2094 tmp = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
2095 union_decl, field, NULL_TREE);
2096 tmp = fold_build2 (MODIFY_EXPR,
2097 TREE_TYPE (DECL_RESULT (current_function_decl)),
2098 DECL_RESULT (current_function_decl), tmp);
2099 tmp = build1_v (RETURN_EXPR, tmp);
2101 else if (TREE_TYPE (DECL_RESULT (current_function_decl))
2104 tmp = fold_build2 (MODIFY_EXPR,
2105 TREE_TYPE (DECL_RESULT (current_function_decl)),
2106 DECL_RESULT (current_function_decl), tmp);
2107 tmp = build1_v (RETURN_EXPR, tmp);
2109 gfc_add_expr_to_block (&body, tmp);
2111 /* Finish off this function and send it for code generation. */
2112 DECL_SAVED_TREE (thunk_fndecl) = gfc_finish_block (&body);
2115 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl)) = thunk_fndecl;
2116 DECL_SAVED_TREE (thunk_fndecl)
2117 = build3_v (BIND_EXPR, tmp, DECL_SAVED_TREE (thunk_fndecl),
2118 DECL_INITIAL (thunk_fndecl));
2120 /* Output the GENERIC tree. */
2121 dump_function (TDI_original, thunk_fndecl);
2123 /* Store the end of the function, so that we get good line number
2124 info for the epilogue. */
2125 cfun->function_end_locus = input_location;
2127 /* We're leaving the context of this function, so zap cfun.
2128 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
2129 tree_rest_of_compilation. */
2132 current_function_decl = NULL_TREE;
2134 cgraph_finalize_function (thunk_fndecl, true);
2136 /* We share the symbols in the formal argument list with other entry
2137 points and the master function. Clear them so that they are
2138 recreated for each function. */
2139 for (formal = thunk_sym->formal; formal; formal = formal->next)
2140 if (formal->sym != NULL) /* Ignore alternate returns. */
2142 formal->sym->backend_decl = NULL_TREE;
2143 if (formal->sym->ts.type == BT_CHARACTER)
2144 formal->sym->ts.u.cl->backend_decl = NULL_TREE;
2147 if (thunk_sym->attr.function)
2149 if (thunk_sym->ts.type == BT_CHARACTER)
2150 thunk_sym->ts.u.cl->backend_decl = NULL_TREE;
2151 if (thunk_sym->result->ts.type == BT_CHARACTER)
2152 thunk_sym->result->ts.u.cl->backend_decl = NULL_TREE;
2156 gfc_set_backend_locus (&old_loc);
2160 /* Create a decl for a function, and create any thunks for alternate entry
2164 gfc_create_function_decl (gfc_namespace * ns)
2166 /* Create a declaration for the master function. */
2167 build_function_decl (ns->proc_name);
2169 /* Compile the entry thunks. */
2171 build_entry_thunks (ns);
2173 /* Now create the read argument list. */
2174 create_function_arglist (ns->proc_name);
2177 /* Return the decl used to hold the function return value. If
2178 parent_flag is set, the context is the parent_scope. */
2181 gfc_get_fake_result_decl (gfc_symbol * sym, int parent_flag)
2185 tree this_fake_result_decl;
2186 tree this_function_decl;
2188 char name[GFC_MAX_SYMBOL_LEN + 10];
2192 this_fake_result_decl = parent_fake_result_decl;
2193 this_function_decl = DECL_CONTEXT (current_function_decl);
2197 this_fake_result_decl = current_fake_result_decl;
2198 this_function_decl = current_function_decl;
2202 && sym->ns->proc_name->backend_decl == this_function_decl
2203 && sym->ns->proc_name->attr.entry_master
2204 && sym != sym->ns->proc_name)
2207 if (this_fake_result_decl != NULL)
2208 for (t = TREE_CHAIN (this_fake_result_decl); t; t = TREE_CHAIN (t))
2209 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t)), sym->name) == 0)
2212 return TREE_VALUE (t);
2213 decl = gfc_get_fake_result_decl (sym->ns->proc_name, parent_flag);
2216 this_fake_result_decl = parent_fake_result_decl;
2218 this_fake_result_decl = current_fake_result_decl;
2220 if (decl && sym->ns->proc_name->attr.mixed_entry_master)
2224 for (field = TYPE_FIELDS (TREE_TYPE (decl));
2225 field; field = TREE_CHAIN (field))
2226 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
2230 gcc_assert (field != NULL_TREE);
2231 decl = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
2232 decl, field, NULL_TREE);
2235 var = create_tmp_var_raw (TREE_TYPE (decl), sym->name);
2237 gfc_add_decl_to_parent_function (var);
2239 gfc_add_decl_to_function (var);
2241 SET_DECL_VALUE_EXPR (var, decl);
2242 DECL_HAS_VALUE_EXPR_P (var) = 1;
2243 GFC_DECL_RESULT (var) = 1;
2245 TREE_CHAIN (this_fake_result_decl)
2246 = tree_cons (get_identifier (sym->name), var,
2247 TREE_CHAIN (this_fake_result_decl));
2251 if (this_fake_result_decl != NULL_TREE)
2252 return TREE_VALUE (this_fake_result_decl);
2254 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
2259 if (sym->ts.type == BT_CHARACTER)
2261 if (sym->ts.u.cl->backend_decl == NULL_TREE)
2262 length = gfc_create_string_length (sym);
2264 length = sym->ts.u.cl->backend_decl;
2265 if (TREE_CODE (length) == VAR_DECL
2266 && DECL_CONTEXT (length) == NULL_TREE)
2267 gfc_add_decl_to_function (length);
2270 if (gfc_return_by_reference (sym))
2272 decl = DECL_ARGUMENTS (this_function_decl);
2274 if (sym->ns->proc_name->backend_decl == this_function_decl
2275 && sym->ns->proc_name->attr.entry_master)
2276 decl = TREE_CHAIN (decl);
2278 TREE_USED (decl) = 1;
2280 decl = gfc_build_dummy_array_decl (sym, decl);
2284 sprintf (name, "__result_%.20s",
2285 IDENTIFIER_POINTER (DECL_NAME (this_function_decl)));
2287 if (!sym->attr.mixed_entry_master && sym->attr.function)
2288 decl = build_decl (DECL_SOURCE_LOCATION (this_function_decl),
2289 VAR_DECL, get_identifier (name),
2290 gfc_sym_type (sym));
2292 decl = build_decl (DECL_SOURCE_LOCATION (this_function_decl),
2293 VAR_DECL, get_identifier (name),
2294 TREE_TYPE (TREE_TYPE (this_function_decl)));
2295 DECL_ARTIFICIAL (decl) = 1;
2296 DECL_EXTERNAL (decl) = 0;
2297 TREE_PUBLIC (decl) = 0;
2298 TREE_USED (decl) = 1;
2299 GFC_DECL_RESULT (decl) = 1;
2300 TREE_ADDRESSABLE (decl) = 1;
2302 layout_decl (decl, 0);
2305 gfc_add_decl_to_parent_function (decl);
2307 gfc_add_decl_to_function (decl);
2311 parent_fake_result_decl = build_tree_list (NULL, decl);
2313 current_fake_result_decl = build_tree_list (NULL, decl);
2319 /* Builds a function decl. The remaining parameters are the types of the
2320 function arguments. Negative nargs indicates a varargs function. */
2323 build_library_function_decl_1 (tree name, const char *spec,
2324 tree rettype, int nargs, va_list p)
2332 /* Library functions must be declared with global scope. */
2333 gcc_assert (current_function_decl == NULL_TREE);
2335 /* Create a list of the argument types. */
2336 for (arglist = NULL_TREE, n = abs (nargs); n > 0; n--)
2338 argtype = va_arg (p, tree);
2339 arglist = gfc_chainon_list (arglist, argtype);
2344 /* Terminate the list. */
2345 arglist = gfc_chainon_list (arglist, void_type_node);
2348 /* Build the function type and decl. */
2349 fntype = build_function_type (rettype, arglist);
2352 tree attr_args = build_tree_list (NULL_TREE,
2353 build_string (strlen (spec), spec));
2354 tree attrs = tree_cons (get_identifier ("fn spec"),
2355 attr_args, TYPE_ATTRIBUTES (fntype));
2356 fntype = build_type_attribute_variant (fntype, attrs);
2358 fndecl = build_decl (input_location,
2359 FUNCTION_DECL, name, fntype);
2361 /* Mark this decl as external. */
2362 DECL_EXTERNAL (fndecl) = 1;
2363 TREE_PUBLIC (fndecl) = 1;
2367 rest_of_decl_compilation (fndecl, 1, 0);
2372 /* Builds a function decl. The remaining parameters are the types of the
2373 function arguments. Negative nargs indicates a varargs function. */
2376 gfc_build_library_function_decl (tree name, tree rettype, int nargs, ...)
2380 va_start (args, nargs);
2381 ret = build_library_function_decl_1 (name, NULL, rettype, nargs, args);
2386 /* Builds a function decl. The remaining parameters are the types of the
2387 function arguments. Negative nargs indicates a varargs function.
2388 The SPEC parameter specifies the function argument and return type
2389 specification according to the fnspec function type attribute. */
2392 gfc_build_library_function_decl_with_spec (tree name, const char *spec,
2393 tree rettype, int nargs, ...)
2397 va_start (args, nargs);
2398 ret = build_library_function_decl_1 (name, spec, rettype, nargs, args);
2404 gfc_build_intrinsic_function_decls (void)
2406 tree gfc_int4_type_node = gfc_get_int_type (4);
2407 tree gfc_int8_type_node = gfc_get_int_type (8);
2408 tree gfc_int16_type_node = gfc_get_int_type (16);
2409 tree gfc_logical4_type_node = gfc_get_logical_type (4);
2410 tree pchar1_type_node = gfc_get_pchar_type (1);
2411 tree pchar4_type_node = gfc_get_pchar_type (4);
2413 /* String functions. */
2414 gfor_fndecl_compare_string =
2415 gfc_build_library_function_decl (get_identifier (PREFIX("compare_string")),
2416 integer_type_node, 4,
2417 gfc_charlen_type_node, pchar1_type_node,
2418 gfc_charlen_type_node, pchar1_type_node);
2420 gfor_fndecl_concat_string =
2421 gfc_build_library_function_decl (get_identifier (PREFIX("concat_string")),
2423 gfc_charlen_type_node, pchar1_type_node,
2424 gfc_charlen_type_node, pchar1_type_node,
2425 gfc_charlen_type_node, pchar1_type_node);
2427 gfor_fndecl_string_len_trim =
2428 gfc_build_library_function_decl (get_identifier (PREFIX("string_len_trim")),
2429 gfc_charlen_type_node, 2,
2430 gfc_charlen_type_node, pchar1_type_node);
2432 gfor_fndecl_string_index =
2433 gfc_build_library_function_decl (get_identifier (PREFIX("string_index")),
2434 gfc_charlen_type_node, 5,
2435 gfc_charlen_type_node, pchar1_type_node,
2436 gfc_charlen_type_node, pchar1_type_node,
2437 gfc_logical4_type_node);
2439 gfor_fndecl_string_scan =
2440 gfc_build_library_function_decl (get_identifier (PREFIX("string_scan")),
2441 gfc_charlen_type_node, 5,
2442 gfc_charlen_type_node, pchar1_type_node,
2443 gfc_charlen_type_node, pchar1_type_node,
2444 gfc_logical4_type_node);
2446 gfor_fndecl_string_verify =
2447 gfc_build_library_function_decl (get_identifier (PREFIX("string_verify")),
2448 gfc_charlen_type_node, 5,
2449 gfc_charlen_type_node, pchar1_type_node,
2450 gfc_charlen_type_node, pchar1_type_node,
2451 gfc_logical4_type_node);
2453 gfor_fndecl_string_trim =
2454 gfc_build_library_function_decl (get_identifier (PREFIX("string_trim")),
2456 build_pointer_type (gfc_charlen_type_node),
2457 build_pointer_type (pchar1_type_node),
2458 gfc_charlen_type_node, pchar1_type_node);
2460 gfor_fndecl_string_minmax =
2461 gfc_build_library_function_decl (get_identifier (PREFIX("string_minmax")),
2463 build_pointer_type (gfc_charlen_type_node),
2464 build_pointer_type (pchar1_type_node),
2465 integer_type_node, integer_type_node);
2467 gfor_fndecl_adjustl =
2468 gfc_build_library_function_decl (get_identifier (PREFIX("adjustl")),
2469 void_type_node, 3, pchar1_type_node,
2470 gfc_charlen_type_node, pchar1_type_node);
2472 gfor_fndecl_adjustr =
2473 gfc_build_library_function_decl (get_identifier (PREFIX("adjustr")),
2474 void_type_node, 3, pchar1_type_node,
2475 gfc_charlen_type_node, pchar1_type_node);
2477 gfor_fndecl_select_string =
2478 gfc_build_library_function_decl (get_identifier (PREFIX("select_string")),
2479 integer_type_node, 4, pvoid_type_node,
2480 integer_type_node, pchar1_type_node,
2481 gfc_charlen_type_node);
2483 gfor_fndecl_compare_string_char4 =
2484 gfc_build_library_function_decl (get_identifier
2485 (PREFIX("compare_string_char4")),
2486 integer_type_node, 4,
2487 gfc_charlen_type_node, pchar4_type_node,
2488 gfc_charlen_type_node, pchar4_type_node);
2490 gfor_fndecl_concat_string_char4 =
2491 gfc_build_library_function_decl (get_identifier
2492 (PREFIX("concat_string_char4")),
2494 gfc_charlen_type_node, pchar4_type_node,
2495 gfc_charlen_type_node, pchar4_type_node,
2496 gfc_charlen_type_node, pchar4_type_node);
2498 gfor_fndecl_string_len_trim_char4 =
2499 gfc_build_library_function_decl (get_identifier
2500 (PREFIX("string_len_trim_char4")),
2501 gfc_charlen_type_node, 2,
2502 gfc_charlen_type_node, pchar4_type_node);
2504 gfor_fndecl_string_index_char4 =
2505 gfc_build_library_function_decl (get_identifier
2506 (PREFIX("string_index_char4")),
2507 gfc_charlen_type_node, 5,
2508 gfc_charlen_type_node, pchar4_type_node,
2509 gfc_charlen_type_node, pchar4_type_node,
2510 gfc_logical4_type_node);
2512 gfor_fndecl_string_scan_char4 =
2513 gfc_build_library_function_decl (get_identifier
2514 (PREFIX("string_scan_char4")),
2515 gfc_charlen_type_node, 5,
2516 gfc_charlen_type_node, pchar4_type_node,
2517 gfc_charlen_type_node, pchar4_type_node,
2518 gfc_logical4_type_node);
2520 gfor_fndecl_string_verify_char4 =
2521 gfc_build_library_function_decl (get_identifier
2522 (PREFIX("string_verify_char4")),
2523 gfc_charlen_type_node, 5,
2524 gfc_charlen_type_node, pchar4_type_node,
2525 gfc_charlen_type_node, pchar4_type_node,
2526 gfc_logical4_type_node);
2528 gfor_fndecl_string_trim_char4 =
2529 gfc_build_library_function_decl (get_identifier
2530 (PREFIX("string_trim_char4")),
2532 build_pointer_type (gfc_charlen_type_node),
2533 build_pointer_type (pchar4_type_node),
2534 gfc_charlen_type_node, pchar4_type_node);
2536 gfor_fndecl_string_minmax_char4 =
2537 gfc_build_library_function_decl (get_identifier
2538 (PREFIX("string_minmax_char4")),
2540 build_pointer_type (gfc_charlen_type_node),
2541 build_pointer_type (pchar4_type_node),
2542 integer_type_node, integer_type_node);
2544 gfor_fndecl_adjustl_char4 =
2545 gfc_build_library_function_decl (get_identifier (PREFIX("adjustl_char4")),
2546 void_type_node, 3, pchar4_type_node,
2547 gfc_charlen_type_node, pchar4_type_node);
2549 gfor_fndecl_adjustr_char4 =
2550 gfc_build_library_function_decl (get_identifier (PREFIX("adjustr_char4")),
2551 void_type_node, 3, pchar4_type_node,
2552 gfc_charlen_type_node, pchar4_type_node);
2554 gfor_fndecl_select_string_char4 =
2555 gfc_build_library_function_decl (get_identifier
2556 (PREFIX("select_string_char4")),
2557 integer_type_node, 4, pvoid_type_node,
2558 integer_type_node, pvoid_type_node,
2559 gfc_charlen_type_node);
2562 /* Conversion between character kinds. */
2564 gfor_fndecl_convert_char1_to_char4 =
2565 gfc_build_library_function_decl (get_identifier
2566 (PREFIX("convert_char1_to_char4")),
2568 build_pointer_type (pchar4_type_node),
2569 gfc_charlen_type_node, pchar1_type_node);
2571 gfor_fndecl_convert_char4_to_char1 =
2572 gfc_build_library_function_decl (get_identifier
2573 (PREFIX("convert_char4_to_char1")),
2575 build_pointer_type (pchar1_type_node),
2576 gfc_charlen_type_node, pchar4_type_node);
2578 /* Misc. functions. */
2580 gfor_fndecl_ttynam =
2581 gfc_build_library_function_decl (get_identifier (PREFIX("ttynam")),
2585 gfc_charlen_type_node,
2589 gfc_build_library_function_decl (get_identifier (PREFIX("fdate")),
2593 gfc_charlen_type_node);
2596 gfc_build_library_function_decl (get_identifier (PREFIX("ctime")),
2600 gfc_charlen_type_node,
2601 gfc_int8_type_node);
2603 gfor_fndecl_sc_kind =
2604 gfc_build_library_function_decl (get_identifier
2605 (PREFIX("selected_char_kind")),
2606 gfc_int4_type_node, 2,
2607 gfc_charlen_type_node, pchar_type_node);
2609 gfor_fndecl_si_kind =
2610 gfc_build_library_function_decl (get_identifier
2611 (PREFIX("selected_int_kind")),
2612 gfc_int4_type_node, 1, pvoid_type_node);
2614 gfor_fndecl_sr_kind =
2615 gfc_build_library_function_decl (get_identifier
2616 (PREFIX("selected_real_kind2008")),
2617 gfc_int4_type_node, 3,
2618 pvoid_type_node, pvoid_type_node,
2621 /* Power functions. */
2623 tree ctype, rtype, itype, jtype;
2624 int rkind, ikind, jkind;
2627 static int ikinds[NIKINDS] = {4, 8, 16};
2628 static int rkinds[NRKINDS] = {4, 8, 10, 16};
2629 char name[PREFIX_LEN + 12]; /* _gfortran_pow_?n_?n */
2631 for (ikind=0; ikind < NIKINDS; ikind++)
2633 itype = gfc_get_int_type (ikinds[ikind]);
2635 for (jkind=0; jkind < NIKINDS; jkind++)
2637 jtype = gfc_get_int_type (ikinds[jkind]);
2640 sprintf(name, PREFIX("pow_i%d_i%d"), ikinds[ikind],
2642 gfor_fndecl_math_powi[jkind][ikind].integer =
2643 gfc_build_library_function_decl (get_identifier (name),
2644 jtype, 2, jtype, itype);
2645 TREE_READONLY (gfor_fndecl_math_powi[jkind][ikind].integer) = 1;
2649 for (rkind = 0; rkind < NRKINDS; rkind ++)
2651 rtype = gfc_get_real_type (rkinds[rkind]);
2654 sprintf(name, PREFIX("pow_r%d_i%d"), rkinds[rkind],
2656 gfor_fndecl_math_powi[rkind][ikind].real =
2657 gfc_build_library_function_decl (get_identifier (name),
2658 rtype, 2, rtype, itype);
2659 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].real) = 1;
2662 ctype = gfc_get_complex_type (rkinds[rkind]);
2665 sprintf(name, PREFIX("pow_c%d_i%d"), rkinds[rkind],
2667 gfor_fndecl_math_powi[rkind][ikind].cmplx =
2668 gfc_build_library_function_decl (get_identifier (name),
2669 ctype, 2,ctype, itype);
2670 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1;
2678 gfor_fndecl_math_ishftc4 =
2679 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc4")),
2681 3, gfc_int4_type_node,
2682 gfc_int4_type_node, gfc_int4_type_node);
2683 gfor_fndecl_math_ishftc8 =
2684 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc8")),
2686 3, gfc_int8_type_node,
2687 gfc_int4_type_node, gfc_int4_type_node);
2688 if (gfc_int16_type_node)
2689 gfor_fndecl_math_ishftc16 =
2690 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc16")),
2691 gfc_int16_type_node, 3,
2692 gfc_int16_type_node,
2694 gfc_int4_type_node);
2696 /* BLAS functions. */
2698 tree pint = build_pointer_type (integer_type_node);
2699 tree ps = build_pointer_type (gfc_get_real_type (gfc_default_real_kind));
2700 tree pd = build_pointer_type (gfc_get_real_type (gfc_default_double_kind));
2701 tree pc = build_pointer_type (gfc_get_complex_type (gfc_default_real_kind));
2702 tree pz = build_pointer_type
2703 (gfc_get_complex_type (gfc_default_double_kind));
2705 gfor_fndecl_sgemm = gfc_build_library_function_decl
2707 (gfc_option.flag_underscoring ? "sgemm_"
2709 void_type_node, 15, pchar_type_node,
2710 pchar_type_node, pint, pint, pint, ps, ps, pint,
2711 ps, pint, ps, ps, pint, integer_type_node,
2713 gfor_fndecl_dgemm = gfc_build_library_function_decl
2715 (gfc_option.flag_underscoring ? "dgemm_"
2717 void_type_node, 15, pchar_type_node,
2718 pchar_type_node, pint, pint, pint, pd, pd, pint,
2719 pd, pint, pd, pd, pint, integer_type_node,
2721 gfor_fndecl_cgemm = gfc_build_library_function_decl
2723 (gfc_option.flag_underscoring ? "cgemm_"
2725 void_type_node, 15, pchar_type_node,
2726 pchar_type_node, pint, pint, pint, pc, pc, pint,
2727 pc, pint, pc, pc, pint, integer_type_node,
2729 gfor_fndecl_zgemm = gfc_build_library_function_decl
2731 (gfc_option.flag_underscoring ? "zgemm_"
2733 void_type_node, 15, pchar_type_node,
2734 pchar_type_node, pint, pint, pint, pz, pz, pint,
2735 pz, pint, pz, pz, pint, integer_type_node,
2739 /* Other functions. */
2741 gfc_build_library_function_decl (get_identifier (PREFIX("size0")),
2742 gfc_array_index_type,
2743 1, pvoid_type_node);
2745 gfc_build_library_function_decl (get_identifier (PREFIX("size1")),
2746 gfc_array_index_type,
2748 gfc_array_index_type);
2751 gfc_build_library_function_decl (get_identifier (PREFIX ("iargc")),
2755 if (gfc_type_for_size (128, true))
2757 tree uint128 = gfc_type_for_size (128, true);
2759 gfor_fndecl_clz128 =
2760 gfc_build_library_function_decl (get_identifier (PREFIX ("clz128")),
2761 integer_type_node, 1, uint128);
2763 gfor_fndecl_ctz128 =
2764 gfc_build_library_function_decl (get_identifier (PREFIX ("ctz128")),
2765 integer_type_node, 1, uint128);
2770 /* Make prototypes for runtime library functions. */
2773 gfc_build_builtin_function_decls (void)
2775 tree gfc_int4_type_node = gfc_get_int_type (4);
2777 gfor_fndecl_stop_numeric =
2778 gfc_build_library_function_decl (get_identifier (PREFIX("stop_numeric")),
2779 void_type_node, 1, gfc_int4_type_node);
2780 /* STOP doesn't return. */
2781 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric) = 1;
2784 gfor_fndecl_stop_string =
2785 gfc_build_library_function_decl (get_identifier (PREFIX("stop_string")),
2786 void_type_node, 2, pchar_type_node,
2787 gfc_int4_type_node);
2788 /* STOP doesn't return. */
2789 TREE_THIS_VOLATILE (gfor_fndecl_stop_string) = 1;
2792 gfor_fndecl_error_stop_numeric =
2793 gfc_build_library_function_decl (get_identifier (PREFIX("error_stop_numeric")),
2794 void_type_node, 1, gfc_int4_type_node);
2795 /* ERROR STOP doesn't return. */
2796 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_numeric) = 1;
2799 gfor_fndecl_error_stop_string =
2800 gfc_build_library_function_decl (get_identifier (PREFIX("error_stop_string")),
2801 void_type_node, 2, pchar_type_node,
2802 gfc_int4_type_node);
2803 /* ERROR STOP doesn't return. */
2804 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_string) = 1;
2807 gfor_fndecl_pause_numeric =
2808 gfc_build_library_function_decl (get_identifier (PREFIX("pause_numeric")),
2809 void_type_node, 1, gfc_int4_type_node);
2811 gfor_fndecl_pause_string =
2812 gfc_build_library_function_decl (get_identifier (PREFIX("pause_string")),
2813 void_type_node, 2, pchar_type_node,
2814 gfc_int4_type_node);
2816 gfor_fndecl_runtime_error =
2817 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error")),
2818 void_type_node, -1, pchar_type_node);
2819 /* The runtime_error function does not return. */
2820 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error) = 1;
2822 gfor_fndecl_runtime_error_at =
2823 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error_at")),
2824 void_type_node, -2, pchar_type_node,
2826 /* The runtime_error_at function does not return. */
2827 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at) = 1;
2829 gfor_fndecl_runtime_warning_at =
2830 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_warning_at")),
2831 void_type_node, -2, pchar_type_node,
2833 gfor_fndecl_generate_error =
2834 gfc_build_library_function_decl (get_identifier (PREFIX("generate_error")),
2835 void_type_node, 3, pvoid_type_node,
2836 integer_type_node, pchar_type_node);
2838 gfor_fndecl_os_error =
2839 gfc_build_library_function_decl (get_identifier (PREFIX("os_error")),
2840 void_type_node, 1, pchar_type_node);
2841 /* The runtime_error function does not return. */
2842 TREE_THIS_VOLATILE (gfor_fndecl_os_error) = 1;
2844 gfor_fndecl_set_args =
2845 gfc_build_library_function_decl (get_identifier (PREFIX("set_args")),
2846 void_type_node, 2, integer_type_node,
2847 build_pointer_type (pchar_type_node));
2849 gfor_fndecl_set_fpe =
2850 gfc_build_library_function_decl (get_identifier (PREFIX("set_fpe")),
2851 void_type_node, 1, integer_type_node);
2853 /* Keep the array dimension in sync with the call, later in this file. */
2854 gfor_fndecl_set_options =
2855 gfc_build_library_function_decl (get_identifier (PREFIX("set_options")),
2856 void_type_node, 2, integer_type_node,
2857 build_pointer_type (integer_type_node));
2859 gfor_fndecl_set_convert =
2860 gfc_build_library_function_decl (get_identifier (PREFIX("set_convert")),
2861 void_type_node, 1, integer_type_node);
2863 gfor_fndecl_set_record_marker =
2864 gfc_build_library_function_decl (get_identifier (PREFIX("set_record_marker")),
2865 void_type_node, 1, integer_type_node);
2867 gfor_fndecl_set_max_subrecord_length =
2868 gfc_build_library_function_decl (get_identifier (PREFIX("set_max_subrecord_length")),
2869 void_type_node, 1, integer_type_node);
2871 gfor_fndecl_in_pack = gfc_build_library_function_decl_with_spec (
2872 get_identifier (PREFIX("internal_pack")), ".r",
2873 pvoid_type_node, 1, pvoid_type_node);
2875 gfor_fndecl_in_unpack = gfc_build_library_function_decl_with_spec (
2876 get_identifier (PREFIX("internal_unpack")), ".wR",
2877 void_type_node, 2, pvoid_type_node, pvoid_type_node);
2879 gfor_fndecl_associated =
2880 gfc_build_library_function_decl (
2881 get_identifier (PREFIX("associated")),
2882 integer_type_node, 2, ppvoid_type_node,
2885 gfc_build_intrinsic_function_decls ();
2886 gfc_build_intrinsic_lib_fndecls ();
2887 gfc_build_io_library_fndecls ();
2891 /* Evaluate the length of dummy character variables. */
2894 gfc_trans_dummy_character (gfc_symbol *sym, gfc_charlen *cl, tree fnbody)
2898 gfc_finish_decl (cl->backend_decl);
2900 gfc_start_block (&body);
2902 /* Evaluate the string length expression. */
2903 gfc_conv_string_length (cl, NULL, &body);
2905 gfc_trans_vla_type_sizes (sym, &body);
2907 gfc_add_expr_to_block (&body, fnbody);
2908 return gfc_finish_block (&body);
2912 /* Allocate and cleanup an automatic character variable. */
2915 gfc_trans_auto_character_variable (gfc_symbol * sym, tree fnbody)
2921 gcc_assert (sym->backend_decl);
2922 gcc_assert (sym->ts.u.cl && sym->ts.u.cl->length);
2924 gfc_start_block (&body);
2926 /* Evaluate the string length expression. */
2927 gfc_conv_string_length (sym->ts.u.cl, NULL, &body);
2929 gfc_trans_vla_type_sizes (sym, &body);
2931 decl = sym->backend_decl;
2933 /* Emit a DECL_EXPR for this variable, which will cause the
2934 gimplifier to allocate storage, and all that good stuff. */
2935 tmp = fold_build1 (DECL_EXPR, TREE_TYPE (decl), decl);
2936 gfc_add_expr_to_block (&body, tmp);
2938 gfc_add_expr_to_block (&body, fnbody);
2939 return gfc_finish_block (&body);
2942 /* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
2945 gfc_trans_assign_aux_var (gfc_symbol * sym, tree fnbody)
2949 gcc_assert (sym->backend_decl);
2950 gfc_start_block (&body);
2952 /* Set the initial value to length. See the comments in
2953 function gfc_add_assign_aux_vars in this file. */
2954 gfc_add_modify (&body, GFC_DECL_STRING_LEN (sym->backend_decl),
2955 build_int_cst (NULL_TREE, -2));
2957 gfc_add_expr_to_block (&body, fnbody);
2958 return gfc_finish_block (&body);
2962 gfc_trans_vla_one_sizepos (tree *tp, stmtblock_t *body)
2964 tree t = *tp, var, val;
2966 if (t == NULL || t == error_mark_node)
2968 if (TREE_CONSTANT (t) || DECL_P (t))
2971 if (TREE_CODE (t) == SAVE_EXPR)
2973 if (SAVE_EXPR_RESOLVED_P (t))
2975 *tp = TREE_OPERAND (t, 0);
2978 val = TREE_OPERAND (t, 0);
2983 var = gfc_create_var_np (TREE_TYPE (t), NULL);
2984 gfc_add_decl_to_function (var);
2985 gfc_add_modify (body, var, val);
2986 if (TREE_CODE (t) == SAVE_EXPR)
2987 TREE_OPERAND (t, 0) = var;
2992 gfc_trans_vla_type_sizes_1 (tree type, stmtblock_t *body)
2996 if (type == NULL || type == error_mark_node)
2999 type = TYPE_MAIN_VARIANT (type);
3001 if (TREE_CODE (type) == INTEGER_TYPE)
3003 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type), body);
3004 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type), body);
3006 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
3008 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
3009 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
3012 else if (TREE_CODE (type) == ARRAY_TYPE)
3014 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type), body);
3015 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type), body);
3016 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type), body);
3017 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type), body);
3019 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
3021 TYPE_SIZE (t) = TYPE_SIZE (type);
3022 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
3027 /* Make sure all type sizes and array domains are either constant,
3028 or variable or parameter decls. This is a simplified variant
3029 of gimplify_type_sizes, but we can't use it here, as none of the
3030 variables in the expressions have been gimplified yet.
3031 As type sizes and domains for various variable length arrays
3032 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
3033 time, without this routine gimplify_type_sizes in the middle-end
3034 could result in the type sizes being gimplified earlier than where
3035 those variables are initialized. */
3038 gfc_trans_vla_type_sizes (gfc_symbol *sym, stmtblock_t *body)
3040 tree type = TREE_TYPE (sym->backend_decl);
3042 if (TREE_CODE (type) == FUNCTION_TYPE
3043 && (sym->attr.function || sym->attr.result || sym->attr.entry))
3045 if (! current_fake_result_decl)
3048 type = TREE_TYPE (TREE_VALUE (current_fake_result_decl));
3051 while (POINTER_TYPE_P (type))
3052 type = TREE_TYPE (type);
3054 if (GFC_DESCRIPTOR_TYPE_P (type))
3056 tree etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
3058 while (POINTER_TYPE_P (etype))
3059 etype = TREE_TYPE (etype);
3061 gfc_trans_vla_type_sizes_1 (etype, body);
3064 gfc_trans_vla_type_sizes_1 (type, body);
3068 /* Initialize a derived type by building an lvalue from the symbol
3069 and using trans_assignment to do the work. Set dealloc to false
3070 if no deallocation prior the assignment is needed. */
3072 gfc_init_default_dt (gfc_symbol * sym, tree body, bool dealloc)
3074 stmtblock_t fnblock;
3079 gfc_init_block (&fnblock);
3080 gcc_assert (!sym->attr.allocatable);
3081 gfc_set_sym_referenced (sym);
3082 e = gfc_lval_expr_from_sym (sym);
3083 tmp = gfc_trans_assignment (e, sym->value, false, dealloc);
3084 if (sym->attr.dummy && (sym->attr.optional
3085 || sym->ns->proc_name->attr.entry_master))
3087 present = gfc_conv_expr_present (sym);
3088 tmp = build3 (COND_EXPR, TREE_TYPE (tmp), present,
3089 tmp, build_empty_stmt (input_location));
3091 gfc_add_expr_to_block (&fnblock, tmp);
3094 gfc_add_expr_to_block (&fnblock, body);
3095 return gfc_finish_block (&fnblock);
3099 /* Initialize INTENT(OUT) derived type dummies. As well as giving
3100 them their default initializer, if they do not have allocatable
3101 components, they have their allocatable components deallocated. */
3104 init_intent_out_dt (gfc_symbol * proc_sym, tree body)
3106 stmtblock_t fnblock;
3107 gfc_formal_arglist *f;
3111 gfc_init_block (&fnblock);
3112 for (f = proc_sym->formal; f; f = f->next)
3113 if (f->sym && f->sym->attr.intent == INTENT_OUT
3114 && !f->sym->attr.pointer
3115 && f->sym->ts.type == BT_DERIVED)
3117 if (f->sym->ts.u.derived->attr.alloc_comp && !f->sym->value)
3119 tmp = gfc_deallocate_alloc_comp (f->sym->ts.u.derived,
3120 f->sym->backend_decl,
3121 f->sym->as ? f->sym->as->rank : 0);
3123 if (f->sym->attr.optional
3124 || f->sym->ns->proc_name->attr.entry_master)
3126 present = gfc_conv_expr_present (f->sym);
3127 tmp = build3 (COND_EXPR, TREE_TYPE (tmp), present,
3128 tmp, build_empty_stmt (input_location));
3131 gfc_add_expr_to_block (&fnblock, tmp);
3133 else if (f->sym->value)
3134 body = gfc_init_default_dt (f->sym, body, true);
3137 gfc_add_expr_to_block (&fnblock, body);
3138 return gfc_finish_block (&fnblock);
3142 /* Generate function entry and exit code, and add it to the function body.
3144 Allocation and initialization of array variables.
3145 Allocation of character string variables.
3146 Initialization and possibly repacking of dummy arrays.
3147 Initialization of ASSIGN statement auxiliary variable.
3148 Automatic deallocation. */
3151 gfc_trans_deferred_vars (gfc_symbol * proc_sym, tree fnbody)
3155 gfc_formal_arglist *f;
3157 bool seen_trans_deferred_array = false;
3159 /* Deal with implicit return variables. Explicit return variables will
3160 already have been added. */
3161 if (gfc_return_by_reference (proc_sym) && proc_sym->result == proc_sym)
3163 if (!current_fake_result_decl)
3165 gfc_entry_list *el = NULL;
3166 if (proc_sym->attr.entry_master)
3168 for (el = proc_sym->ns->entries; el; el = el->next)
3169 if (el->sym != el->sym->result)
3172 /* TODO: move to the appropriate place in resolve.c. */
3173 if (warn_return_type && el == NULL)
3174 gfc_warning ("Return value of function '%s' at %L not set",
3175 proc_sym->name, &proc_sym->declared_at);
3177 else if (proc_sym->as)
3179 tree result = TREE_VALUE (current_fake_result_decl);
3180 fnbody = gfc_trans_dummy_array_bias (proc_sym, result, fnbody);
3182 /* An automatic character length, pointer array result. */
3183 if (proc_sym->ts.type == BT_CHARACTER
3184 && TREE_CODE (proc_sym->ts.u.cl->backend_decl) == VAR_DECL)
3185 fnbody = gfc_trans_dummy_character (proc_sym, proc_sym->ts.u.cl,
3188 else if (proc_sym->ts.type == BT_CHARACTER)
3190 if (TREE_CODE (proc_sym->ts.u.cl->backend_decl) == VAR_DECL)
3191 fnbody = gfc_trans_dummy_character (proc_sym, proc_sym->ts.u.cl,
3195 gcc_assert (gfc_option.flag_f2c
3196 && proc_sym->ts.type == BT_COMPLEX);
3199 /* Initialize the INTENT(OUT) derived type dummy arguments. This
3200 should be done here so that the offsets and lbounds of arrays
3202 fnbody = init_intent_out_dt (proc_sym, fnbody);
3204 for (sym = proc_sym->tlink; sym != proc_sym; sym = sym->tlink)
3206 bool sym_has_alloc_comp = (sym->ts.type == BT_DERIVED)
3207 && sym->ts.u.derived->attr.alloc_comp;
3208 if (sym->attr.dimension)
3210 switch (sym->as->type)
3213 if (sym->attr.dummy || sym->attr.result)
3215 gfc_trans_dummy_array_bias (sym, sym->backend_decl, fnbody);
3216 else if (sym->attr.pointer || sym->attr.allocatable)
3218 if (TREE_STATIC (sym->backend_decl))
3219 gfc_trans_static_array_pointer (sym);
3222 seen_trans_deferred_array = true;
3223 fnbody = gfc_trans_deferred_array (sym, fnbody);
3228 if (sym_has_alloc_comp)
3230 seen_trans_deferred_array = true;
3231 fnbody = gfc_trans_deferred_array (sym, fnbody);
3233 else if (sym->ts.type == BT_DERIVED
3236 && sym->attr.save == SAVE_NONE)
3237 fnbody = gfc_init_default_dt (sym, fnbody, false);
3239 gfc_get_backend_locus (&loc);
3240 gfc_set_backend_locus (&sym->declared_at);
3241 fnbody = gfc_trans_auto_array_allocation (sym->backend_decl,
3243 gfc_set_backend_locus (&loc);
3247 case AS_ASSUMED_SIZE:
3248 /* Must be a dummy parameter. */
3249 gcc_assert (sym->attr.dummy || sym->as->cp_was_assumed);
3251 /* We should always pass assumed size arrays the g77 way. */
3252 if (sym->attr.dummy)
3253 fnbody = gfc_trans_g77_array (sym, fnbody);
3256 case AS_ASSUMED_SHAPE:
3257 /* Must be a dummy parameter. */
3258 gcc_assert (sym->attr.dummy);
3260 fnbody = gfc_trans_dummy_array_bias (sym, sym->backend_decl,
3265 seen_trans_deferred_array = true;
3266 fnbody = gfc_trans_deferred_array (sym, fnbody);
3272 if (sym_has_alloc_comp && !seen_trans_deferred_array)
3273 fnbody = gfc_trans_deferred_array (sym, fnbody);
3275 else if (sym->attr.allocatable
3276 || (sym->ts.type == BT_CLASS
3277 && CLASS_DATA (sym)->attr.allocatable))
3279 if (!sym->attr.save)
3281 /* Nullify and automatic deallocation of allocatable
3288 e = gfc_lval_expr_from_sym (sym);
3289 if (sym->ts.type == BT_CLASS)
3290 gfc_add_component_ref (e, "$data");
3292 gfc_init_se (&se, NULL);
3293 se.want_pointer = 1;
3294 gfc_conv_expr (&se, e);
3297 /* Nullify when entering the scope. */
3298 gfc_start_block (&block);
3299 gfc_add_modify (&block, se.expr,
3300 fold_convert (TREE_TYPE (se.expr),
3301 null_pointer_node));
3302 gfc_add_expr_to_block (&block, fnbody);
3304 /* Deallocate when leaving the scope. Nullifying is not
3306 tmp = gfc_deallocate_with_status (se.expr, NULL_TREE, true,
3308 gfc_add_expr_to_block (&block, tmp);
3309 fnbody = gfc_finish_block (&block);
3312 else if (sym_has_alloc_comp)
3313 fnbody = gfc_trans_deferred_array (sym, fnbody);
3314 else if (sym->ts.type == BT_CHARACTER)
3316 gfc_get_backend_locus (&loc);
3317 gfc_set_backend_locus (&sym->declared_at);
3318 if (sym->attr.dummy || sym->attr.result)
3319 fnbody = gfc_trans_dummy_character (sym, sym->ts.u.cl, fnbody);
3321 fnbody = gfc_trans_auto_character_variable (sym, fnbody);
3322 gfc_set_backend_locus (&loc);
3324 else if (sym->attr.assign)
3326 gfc_get_backend_locus (&loc);
3327 gfc_set_backend_locus (&sym->declared_at);
3328 fnbody = gfc_trans_assign_aux_var (sym, fnbody);
3329 gfc_set_backend_locus (&loc);
3331 else if (sym->ts.type == BT_DERIVED
3334 && sym->attr.save == SAVE_NONE)
3335 fnbody = gfc_init_default_dt (sym, fnbody, false);
3340 gfc_init_block (&body);
3342 for (f = proc_sym->formal; f; f = f->next)
3344 if (f->sym && f->sym->tlink == NULL && f->sym->ts.type == BT_CHARACTER)
3346 gcc_assert (f->sym->ts.u.cl->backend_decl != NULL);
3347 if (TREE_CODE (f->sym->ts.u.cl->backend_decl) == PARM_DECL)
3348 gfc_trans_vla_type_sizes (f->sym, &body);
3352 if (gfc_return_by_reference (proc_sym) && proc_sym->ts.type == BT_CHARACTER
3353 && current_fake_result_decl != NULL)
3355 gcc_assert (proc_sym->ts.u.cl->backend_decl != NULL);
3356 if (TREE_CODE (proc_sym->ts.u.cl->backend_decl) == PARM_DECL)
3357 gfc_trans_vla_type_sizes (proc_sym, &body);
3360 gfc_add_expr_to_block (&body, fnbody);
3361 return gfc_finish_block (&body);
3364 static GTY ((param_is (struct module_htab_entry))) htab_t module_htab;
3366 /* Hash and equality functions for module_htab. */
3369 module_htab_do_hash (const void *x)
3371 return htab_hash_string (((const struct module_htab_entry *)x)->name);
3375 module_htab_eq (const void *x1, const void *x2)
3377 return strcmp ((((const struct module_htab_entry *)x1)->name),
3378 (const char *)x2) == 0;
3381 /* Hash and equality functions for module_htab's decls. */
3384 module_htab_decls_hash (const void *x)
3386 const_tree t = (const_tree) x;
3387 const_tree n = DECL_NAME (t);
3389 n = TYPE_NAME (TREE_TYPE (t));
3390 return htab_hash_string (IDENTIFIER_POINTER (n));
3394 module_htab_decls_eq (const void *x1, const void *x2)
3396 const_tree t1 = (const_tree) x1;
3397 const_tree n1 = DECL_NAME (t1);
3398 if (n1 == NULL_TREE)
3399 n1 = TYPE_NAME (TREE_TYPE (t1));
3400 return strcmp (IDENTIFIER_POINTER (n1), (const char *) x2) == 0;
3403 struct module_htab_entry *
3404 gfc_find_module (const char *name)
3409 module_htab = htab_create_ggc (10, module_htab_do_hash,
3410 module_htab_eq, NULL);
3412 slot = htab_find_slot_with_hash (module_htab, name,
3413 htab_hash_string (name), INSERT);
3416 struct module_htab_entry *entry = ggc_alloc_cleared_module_htab_entry ();
3418 entry->name = gfc_get_string (name);
3419 entry->decls = htab_create_ggc (10, module_htab_decls_hash,
3420 module_htab_decls_eq, NULL);
3421 *slot = (void *) entry;
3423 return (struct module_htab_entry *) *slot;
3427 gfc_module_add_decl (struct module_htab_entry *entry, tree decl)
3432 if (DECL_NAME (decl))
3433 name = IDENTIFIER_POINTER (DECL_NAME (decl));
3436 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
3437 name = IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl)));
3439 slot = htab_find_slot_with_hash (entry->decls, name,
3440 htab_hash_string (name), INSERT);
3442 *slot = (void *) decl;
3445 static struct module_htab_entry *cur_module;
3447 /* Output an initialized decl for a module variable. */
3450 gfc_create_module_variable (gfc_symbol * sym)
3454 /* Module functions with alternate entries are dealt with later and
3455 would get caught by the next condition. */
3456 if (sym->attr.entry)
3459 /* Make sure we convert the types of the derived types from iso_c_binding
3461 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
3462 && sym->ts.type == BT_DERIVED)
3463 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
3465 if (sym->attr.flavor == FL_DERIVED
3466 && sym->backend_decl
3467 && TREE_CODE (sym->backend_decl) == RECORD_TYPE)
3469 decl = sym->backend_decl;
3470 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
3472 /* -fwhole-file mixes up the contexts so these asserts are unnecessary. */
3473 if (!(gfc_option.flag_whole_file && sym->attr.use_assoc))
3475 gcc_assert (TYPE_CONTEXT (decl) == NULL_TREE
3476 || TYPE_CONTEXT (decl) == sym->ns->proc_name->backend_decl);
3477 gcc_assert (DECL_CONTEXT (TYPE_STUB_DECL (decl)) == NULL_TREE
3478 || DECL_CONTEXT (TYPE_STUB_DECL (decl))
3479 == sym->ns->proc_name->backend_decl);
3481 TYPE_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
3482 DECL_CONTEXT (TYPE_STUB_DECL (decl)) = sym->ns->proc_name->backend_decl;
3483 gfc_module_add_decl (cur_module, TYPE_STUB_DECL (decl));
3486 /* Only output variables, procedure pointers and array valued,
3487 or derived type, parameters. */
3488 if (sym->attr.flavor != FL_VARIABLE
3489 && !(sym->attr.flavor == FL_PARAMETER
3490 && (sym->attr.dimension || sym->ts.type == BT_DERIVED))
3491 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer))
3494 if ((sym->attr.in_common || sym->attr.in_equivalence) && sym->backend_decl)
3496 decl = sym->backend_decl;
3497 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE);
3498 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
3499 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
3500 gfc_module_add_decl (cur_module, decl);
3503 /* Don't generate variables from other modules. Variables from
3504 COMMONs will already have been generated. */
3505 if (sym->attr.use_assoc || sym->attr.in_common)
3508 /* Equivalenced variables arrive here after creation. */
3509 if (sym->backend_decl
3510 && (sym->equiv_built || sym->attr.in_equivalence))
3513 if (sym->backend_decl && !sym->attr.vtab)
3514 internal_error ("backend decl for module variable %s already exists",
3517 /* We always want module variables to be created. */
3518 sym->attr.referenced = 1;
3519 /* Create the decl. */
3520 decl = gfc_get_symbol_decl (sym);
3522 /* Create the variable. */
3524 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE);
3525 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
3526 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
3527 rest_of_decl_compilation (decl, 1, 0);
3528 gfc_module_add_decl (cur_module, decl);
3530 /* Also add length of strings. */
3531 if (sym->ts.type == BT_CHARACTER)
3535 length = sym->ts.u.cl->backend_decl;
3536 gcc_assert (length || sym->attr.proc_pointer);
3537 if (length && !INTEGER_CST_P (length))
3540 rest_of_decl_compilation (length, 1, 0);
3545 /* Emit debug information for USE statements. */
3548 gfc_trans_use_stmts (gfc_namespace * ns)
3550 gfc_use_list *use_stmt;
3551 for (use_stmt = ns->use_stmts; use_stmt; use_stmt = use_stmt->next)
3553 struct module_htab_entry *entry
3554 = gfc_find_module (use_stmt->module_name);
3555 gfc_use_rename *rent;
3557 if (entry->namespace_decl == NULL)
3559 entry->namespace_decl
3560 = build_decl (input_location,
3562 get_identifier (use_stmt->module_name),
3564 DECL_EXTERNAL (entry->namespace_decl) = 1;
3566 gfc_set_backend_locus (&use_stmt->where);
3567 if (!use_stmt->only_flag)
3568 (*debug_hooks->imported_module_or_decl) (entry->namespace_decl,
3570 ns->proc_name->backend_decl,
3572 for (rent = use_stmt->rename; rent; rent = rent->next)
3574 tree decl, local_name;
3577 if (rent->op != INTRINSIC_NONE)
3580 slot = htab_find_slot_with_hash (entry->decls, rent->use_name,
3581 htab_hash_string (rent->use_name),
3587 st = gfc_find_symtree (ns->sym_root,
3589 ? rent->local_name : rent->use_name);
3592 /* Sometimes, generic interfaces wind up being over-ruled by a
3593 local symbol (see PR41062). */
3594 if (!st->n.sym->attr.use_assoc)
3597 if (st->n.sym->backend_decl
3598 && DECL_P (st->n.sym->backend_decl)
3599 && st->n.sym->module
3600 && strcmp (st->n.sym->module, use_stmt->module_name) == 0)
3602 gcc_assert (DECL_EXTERNAL (entry->namespace_decl)
3603 || (TREE_CODE (st->n.sym->backend_decl)
3605 decl = copy_node (st->n.sym->backend_decl);
3606 DECL_CONTEXT (decl) = entry->namespace_decl;
3607 DECL_EXTERNAL (decl) = 1;
3608 DECL_IGNORED_P (decl) = 0;
3609 DECL_INITIAL (decl) = NULL_TREE;
3613 *slot = error_mark_node;
3614 htab_clear_slot (entry->decls, slot);
3619 decl = (tree) *slot;
3620 if (rent->local_name[0])
3621 local_name = get_identifier (rent->local_name);
3623 local_name = NULL_TREE;
3624 gfc_set_backend_locus (&rent->where);
3625 (*debug_hooks->imported_module_or_decl) (decl, local_name,
3626 ns->proc_name->backend_decl,
3627 !use_stmt->only_flag);
3633 /* Return true if expr is a constant initializer that gfc_conv_initializer
3637 check_constant_initializer (gfc_expr *expr, gfc_typespec *ts, bool array,
3647 if (expr->expr_type == EXPR_CONSTANT || expr->expr_type == EXPR_NULL)
3649 else if (expr->expr_type == EXPR_STRUCTURE)
3650 return check_constant_initializer (expr, ts, false, false);
3651 else if (expr->expr_type != EXPR_ARRAY)
3653 for (c = gfc_constructor_first (expr->value.constructor);
3654 c; c = gfc_constructor_next (c))
3658 if (c->expr->expr_type == EXPR_STRUCTURE)
3660 if (!check_constant_initializer (c->expr, ts, false, false))
3663 else if (c->expr->expr_type != EXPR_CONSTANT)
3668 else switch (ts->type)
3671 if (expr->expr_type != EXPR_STRUCTURE)
3673 cm = expr->ts.u.derived->components;
3674 for (c = gfc_constructor_first (expr->value.constructor);
3675 c; c = gfc_constructor_next (c), cm = cm->next)
3677 if (!c->expr || cm->attr.allocatable)
3679 if (!check_constant_initializer (c->expr, &cm->ts,
3686 return expr->expr_type == EXPR_CONSTANT;
3690 /* Emit debug info for parameters and unreferenced variables with
3694 gfc_emit_parameter_debug_info (gfc_symbol *sym)
3698 if (sym->attr.flavor != FL_PARAMETER
3699 && (sym->attr.flavor != FL_VARIABLE || sym->attr.referenced))
3702 if (sym->backend_decl != NULL
3703 || sym->value == NULL
3704 || sym->attr.use_assoc
3707 || sym->attr.function
3708 || sym->attr.intrinsic
3709 || sym->attr.pointer
3710 || sym->attr.allocatable
3711 || sym->attr.cray_pointee
3712 || sym->attr.threadprivate
3713 || sym->attr.is_bind_c
3714 || sym->attr.subref_array_pointer
3715 || sym->attr.assign)
3718 if (sym->ts.type == BT_CHARACTER)
3720 gfc_conv_const_charlen (sym->ts.u.cl);
3721 if (sym->ts.u.cl->backend_decl == NULL
3722 || TREE_CODE (sym->ts.u.cl->backend_decl) != INTEGER_CST)
3725 else if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.alloc_comp)
3732 if (sym->as->type != AS_EXPLICIT)
3734 for (n = 0; n < sym->as->rank; n++)
3735 if (sym->as->lower[n]->expr_type != EXPR_CONSTANT
3736 || sym->as->upper[n] == NULL
3737 || sym->as->upper[n]->expr_type != EXPR_CONSTANT)
3741 if (!check_constant_initializer (sym->value, &sym->ts,
3742 sym->attr.dimension, false))
3745 /* Create the decl for the variable or constant. */
3746 decl = build_decl (input_location,
3747 sym->attr.flavor == FL_PARAMETER ? CONST_DECL : VAR_DECL,
3748 gfc_sym_identifier (sym), gfc_sym_type (sym));
3749 if (sym->attr.flavor == FL_PARAMETER)
3750 TREE_READONLY (decl) = 1;
3751 gfc_set_decl_location (decl, &sym->declared_at);
3752 if (sym->attr.dimension)
3753 GFC_DECL_PACKED_ARRAY (decl) = 1;
3754 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
3755 TREE_STATIC (decl) = 1;
3756 TREE_USED (decl) = 1;
3757 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
3758 TREE_PUBLIC (decl) = 1;
3760 = gfc_conv_initializer (sym->value, &sym->ts, TREE_TYPE (decl),
3761 sym->attr.dimension, 0);
3762 debug_hooks->global_decl (decl);
3765 /* Generate all the required code for module variables. */
3768 gfc_generate_module_vars (gfc_namespace * ns)
3770 module_namespace = ns;
3771 cur_module = gfc_find_module (ns->proc_name->name);
3773 /* Check if the frontend left the namespace in a reasonable state. */
3774 gcc_assert (ns->proc_name && !ns->proc_name->tlink);
3776 /* Generate COMMON blocks. */
3777 gfc_trans_common (ns);
3779 /* Create decls for all the module variables. */
3780 gfc_traverse_ns (ns, gfc_create_module_variable);
3784 gfc_trans_use_stmts (ns);
3785 gfc_traverse_ns (ns, gfc_emit_parameter_debug_info);
3790 gfc_generate_contained_functions (gfc_namespace * parent)
3794 /* We create all the prototypes before generating any code. */
3795 for (ns = parent->contained; ns; ns = ns->sibling)
3797 /* Skip namespaces from used modules. */
3798 if (ns->parent != parent)
3801 gfc_create_function_decl (ns);
3804 for (ns = parent->contained; ns; ns = ns->sibling)
3806 /* Skip namespaces from used modules. */
3807 if (ns->parent != parent)
3810 gfc_generate_function_code (ns);
3815 /* Drill down through expressions for the array specification bounds and
3816 character length calling generate_local_decl for all those variables
3817 that have not already been declared. */
3820 generate_local_decl (gfc_symbol *);
3822 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
3825 expr_decls (gfc_expr *e, gfc_symbol *sym,
3826 int *f ATTRIBUTE_UNUSED)
3828 if (e->expr_type != EXPR_VARIABLE
3829 || sym == e->symtree->n.sym
3830 || e->symtree->n.sym->mark
3831 || e->symtree->n.sym->ns != sym->ns)
3834 generate_local_decl (e->symtree->n.sym);
3839 generate_expr_decls (gfc_symbol *sym, gfc_expr *e)
3841 gfc_traverse_expr (e, sym, expr_decls, 0);
3845 /* Check for dependencies in the character length and array spec. */
3848 generate_dependency_declarations (gfc_symbol *sym)
3852 if (sym->ts.type == BT_CHARACTER
3854 && sym->ts.u.cl->length
3855 && sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
3856 generate_expr_decls (sym, sym->ts.u.cl->length);
3858 if (sym->as && sym->as->rank)
3860 for (i = 0; i < sym->as->rank; i++)
3862 generate_expr_decls (sym, sym->as->lower[i]);
3863 generate_expr_decls (sym, sym->as->upper[i]);
3869 /* Generate decls for all local variables. We do this to ensure correct
3870 handling of expressions which only appear in the specification of
3874 generate_local_decl (gfc_symbol * sym)
3876 if (sym->attr.flavor == FL_VARIABLE)
3878 if (!sym->attr.dummy && !sym->ns->proc_name->attr.entry_master)
3879 generate_dependency_declarations (sym);
3881 if (sym->attr.referenced)
3882 gfc_get_symbol_decl (sym);
3884 /* Warnings for unused dummy arguments. */
3885 else if (sym->attr.dummy)
3887 /* INTENT(out) dummy arguments are likely meant to be set. */
3888 if (gfc_option.warn_unused_dummy_argument
3889 && sym->attr.intent == INTENT_OUT)
3891 if (sym->ts.type != BT_DERIVED)
3892 gfc_warning ("Dummy argument '%s' at %L was declared "
3893 "INTENT(OUT) but was not set", sym->name,
3895 else if (!gfc_has_default_initializer (sym->ts.u.derived))
3896 gfc_warning ("Derived-type dummy argument '%s' at %L was "
3897 "declared INTENT(OUT) but was not set and "
3898 "does not have a default initializer",
3899 sym->name, &sym->declared_at);
3901 else if (gfc_option.warn_unused_dummy_argument)
3902 gfc_warning ("Unused dummy argument '%s' at %L", sym->name,
3906 /* Warn for unused variables, but not if they're inside a common
3907 block or are use-associated. */
3908 else if (warn_unused_variable
3909 && !(sym->attr.in_common || sym->attr.use_assoc || sym->mark))
3910 gfc_warning ("Unused variable '%s' declared at %L", sym->name,
3913 /* For variable length CHARACTER parameters, the PARM_DECL already
3914 references the length variable, so force gfc_get_symbol_decl
3915 even when not referenced. If optimize > 0, it will be optimized
3916 away anyway. But do this only after emitting -Wunused-parameter
3917 warning if requested. */
3918 if (sym->attr.dummy && !sym->attr.referenced
3919 && sym->ts.type == BT_CHARACTER
3920 && sym->ts.u.cl->backend_decl != NULL
3921 && TREE_CODE (sym->ts.u.cl->backend_decl) == VAR_DECL)
3923 sym->attr.referenced = 1;
3924 gfc_get_symbol_decl (sym);
3927 /* INTENT(out) dummy arguments and result variables with allocatable
3928 components are reset by default and need to be set referenced to
3929 generate the code for nullification and automatic lengths. */
3930 if (!sym->attr.referenced
3931 && sym->ts.type == BT_DERIVED
3932 && sym->ts.u.derived->attr.alloc_comp
3933 && !sym->attr.pointer
3934 && ((sym->attr.dummy && sym->attr.intent == INTENT_OUT)
3936 (sym->attr.result && sym != sym->result)))
3938 sym->attr.referenced = 1;
3939 gfc_get_symbol_decl (sym);
3942 /* Check for dependencies in the array specification and string
3943 length, adding the necessary declarations to the function. We
3944 mark the symbol now, as well as in traverse_ns, to prevent
3945 getting stuck in a circular dependency. */
3948 /* We do not want the middle-end to warn about unused parameters
3949 as this was already done above. */
3950 if (sym->attr.dummy && sym->backend_decl != NULL_TREE)
3951 TREE_NO_WARNING(sym->backend_decl) = 1;
3953 else if (sym->attr.flavor == FL_PARAMETER)
3955 if (warn_unused_parameter
3956 && !sym->attr.referenced
3957 && !sym->attr.use_assoc)
3958 gfc_warning ("Unused parameter '%s' declared at %L", sym->name,
3961 else if (sym->attr.flavor == FL_PROCEDURE)
3963 /* TODO: move to the appropriate place in resolve.c. */
3964 if (warn_return_type
3965 && sym->attr.function
3967 && sym != sym->result
3968 && !sym->result->attr.referenced
3969 && !sym->attr.use_assoc
3970 && sym->attr.if_source != IFSRC_IFBODY)
3972 gfc_warning ("Return value '%s' of function '%s' declared at "
3973 "%L not set", sym->result->name, sym->name,
3974 &sym->result->declared_at);
3976 /* Prevents "Unused variable" warning for RESULT variables. */
3977 sym->result->mark = 1;
3981 if (sym->attr.dummy == 1)
3983 /* Modify the tree type for scalar character dummy arguments of bind(c)
3984 procedures if they are passed by value. The tree type for them will
3985 be promoted to INTEGER_TYPE for the middle end, which appears to be
3986 what C would do with characters passed by-value. The value attribute
3987 implies the dummy is a scalar. */
3988 if (sym->attr.value == 1 && sym->backend_decl != NULL
3989 && sym->ts.type == BT_CHARACTER && sym->ts.is_c_interop
3990 && sym->ns->proc_name != NULL && sym->ns->proc_name->attr.is_bind_c)
3991 gfc_conv_scalar_char_value (sym, NULL, NULL);
3994 /* Make sure we convert the types of the derived types from iso_c_binding
3996 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
3997 && sym->ts.type == BT_DERIVED)
3998 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
4002 generate_local_vars (gfc_namespace * ns)
4004 gfc_traverse_ns (ns, generate_local_decl);
4008 /* Generate a switch statement to jump to the correct entry point. Also
4009 creates the label decls for the entry points. */
4012 gfc_trans_entry_master_switch (gfc_entry_list * el)
4019 gfc_init_block (&block);
4020 for (; el; el = el->next)
4022 /* Add the case label. */
4023 label = gfc_build_label_decl (NULL_TREE);
4024 val = build_int_cst (gfc_array_index_type, el->id);
4025 tmp = build3_v (CASE_LABEL_EXPR, val, NULL_TREE, label);
4026 gfc_add_expr_to_block (&block, tmp);
4028 /* And jump to the actual entry point. */
4029 label = gfc_build_label_decl (NULL_TREE);
4030 tmp = build1_v (GOTO_EXPR, label);
4031 gfc_add_expr_to_block (&block, tmp);
4033 /* Save the label decl. */
4036 tmp = gfc_finish_block (&block);
4037 /* The first argument selects the entry point. */
4038 val = DECL_ARGUMENTS (current_function_decl);
4039 tmp = build3_v (SWITCH_EXPR, val, tmp, NULL_TREE);
4044 /* Add code to string lengths of actual arguments passed to a function against
4045 the expected lengths of the dummy arguments. */
4048 add_argument_checking (stmtblock_t *block, gfc_symbol *sym)
4050 gfc_formal_arglist *formal;
4052 for (formal = sym->formal; formal; formal = formal->next)
4053 if (formal->sym && formal->sym->ts.type == BT_CHARACTER)
4055 enum tree_code comparison;
4060 const char *message;
4066 gcc_assert (cl->passed_length != NULL_TREE);
4067 gcc_assert (cl->backend_decl != NULL_TREE);
4069 /* For POINTER, ALLOCATABLE and assumed-shape dummy arguments, the
4070 string lengths must match exactly. Otherwise, it is only required
4071 that the actual string length is *at least* the expected one.
4072 Sequence association allows for a mismatch of the string length
4073 if the actual argument is (part of) an array, but only if the
4074 dummy argument is an array. (See "Sequence association" in
4075 Section 12.4.1.4 for F95 and 12.4.1.5 for F2003.) */
4076 if (fsym->attr.pointer || fsym->attr.allocatable
4077 || (fsym->as && fsym->as->type == AS_ASSUMED_SHAPE))
4079 comparison = NE_EXPR;
4080 message = _("Actual string length does not match the declared one"
4081 " for dummy argument '%s' (%ld/%ld)");
4083 else if (fsym->as && fsym->as->rank != 0)
4087 comparison = LT_EXPR;
4088 message = _("Actual string length is shorter than the declared one"
4089 " for dummy argument '%s' (%ld/%ld)");
4092 /* Build the condition. For optional arguments, an actual length
4093 of 0 is also acceptable if the associated string is NULL, which
4094 means the argument was not passed. */
4095 cond = fold_build2 (comparison, boolean_type_node,
4096 cl->passed_length, cl->backend_decl);
4097 if (fsym->attr.optional)
4103 not_0length = fold_build2 (NE_EXPR, boolean_type_node,
4105 fold_convert (gfc_charlen_type_node,
4106 integer_zero_node));
4107 /* The symbol needs to be referenced for gfc_get_symbol_decl. */
4108 fsym->attr.referenced = 1;
4109 not_absent = gfc_conv_expr_present (fsym);
4111 absent_failed = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
4112 not_0length, not_absent);
4114 cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
4115 cond, absent_failed);
4118 /* Build the runtime check. */
4119 argname = gfc_build_cstring_const (fsym->name);
4120 argname = gfc_build_addr_expr (pchar_type_node, argname);
4121 gfc_trans_runtime_check (true, false, cond, block, &fsym->declared_at,
4123 fold_convert (long_integer_type_node,
4125 fold_convert (long_integer_type_node,
4132 create_main_function (tree fndecl)
4136 tree tmp, decl, result_decl, argc, argv, typelist, arglist;
4139 old_context = current_function_decl;
4143 push_function_context ();
4144 saved_parent_function_decls = saved_function_decls;
4145 saved_function_decls = NULL_TREE;
4148 /* main() function must be declared with global scope. */
4149 gcc_assert (current_function_decl == NULL_TREE);
4151 /* Declare the function. */
4152 tmp = build_function_type_list (integer_type_node, integer_type_node,
4153 build_pointer_type (pchar_type_node),
4155 main_identifier_node = get_identifier ("main");
4156 ftn_main = build_decl (input_location, FUNCTION_DECL,
4157 main_identifier_node, tmp);
4158 DECL_EXTERNAL (ftn_main) = 0;
4159 TREE_PUBLIC (ftn_main) = 1;
4160 TREE_STATIC (ftn_main) = 1;
4161 DECL_ATTRIBUTES (ftn_main)
4162 = tree_cons (get_identifier("externally_visible"), NULL_TREE, NULL_TREE);
4164 /* Setup the result declaration (for "return 0"). */
4165 result_decl = build_decl (input_location,
4166 RESULT_DECL, NULL_TREE, integer_type_node);
4167 DECL_ARTIFICIAL (result_decl) = 1;
4168 DECL_IGNORED_P (result_decl) = 1;
4169 DECL_CONTEXT (result_decl) = ftn_main;
4170 DECL_RESULT (ftn_main) = result_decl;
4172 pushdecl (ftn_main);
4174 /* Get the arguments. */
4176 arglist = NULL_TREE;
4177 typelist = TYPE_ARG_TYPES (TREE_TYPE (ftn_main));
4179 tmp = TREE_VALUE (typelist);
4180 argc = build_decl (input_location, PARM_DECL, get_identifier ("argc"), tmp);
4181 DECL_CONTEXT (argc) = ftn_main;
4182 DECL_ARG_TYPE (argc) = TREE_VALUE (typelist);
4183 TREE_READONLY (argc) = 1;
4184 gfc_finish_decl (argc);
4185 arglist = chainon (arglist, argc);
4187 typelist = TREE_CHAIN (typelist);
4188 tmp = TREE_VALUE (typelist);
4189 argv = build_decl (input_location, PARM_DECL, get_identifier ("argv"), tmp);
4190 DECL_CONTEXT (argv) = ftn_main;
4191 DECL_ARG_TYPE (argv) = TREE_VALUE (typelist);
4192 TREE_READONLY (argv) = 1;
4193 DECL_BY_REFERENCE (argv) = 1;
4194 gfc_finish_decl (argv);
4195 arglist = chainon (arglist, argv);
4197 DECL_ARGUMENTS (ftn_main) = arglist;
4198 current_function_decl = ftn_main;
4199 announce_function (ftn_main);
4201 rest_of_decl_compilation (ftn_main, 1, 0);
4202 make_decl_rtl (ftn_main);
4203 init_function_start (ftn_main);
4206 gfc_init_block (&body);
4208 /* Call some libgfortran initialization routines, call then MAIN__(). */
4210 /* Call _gfortran_set_args (argc, argv). */
4211 TREE_USED (argc) = 1;
4212 TREE_USED (argv) = 1;
4213 tmp = build_call_expr_loc (input_location,
4214 gfor_fndecl_set_args, 2, argc, argv);
4215 gfc_add_expr_to_block (&body, tmp);
4217 /* Add a call to set_options to set up the runtime library Fortran
4218 language standard parameters. */
4220 tree array_type, array, var;
4221 VEC(constructor_elt,gc) *v = NULL;
4223 /* Passing a new option to the library requires four modifications:
4224 + add it to the tree_cons list below
4225 + change the array size in the call to build_array_type
4226 + change the first argument to the library call
4227 gfor_fndecl_set_options
4228 + modify the library (runtime/compile_options.c)! */
4230 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
4231 build_int_cst (integer_type_node,
4232 gfc_option.warn_std));
4233 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
4234 build_int_cst (integer_type_node,
4235 gfc_option.allow_std));
4236 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
4237 build_int_cst (integer_type_node, pedantic));
4238 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
4239 build_int_cst (integer_type_node,
4240 gfc_option.flag_dump_core));
4241 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
4242 build_int_cst (integer_type_node,
4243 gfc_option.flag_backtrace));
4244 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
4245 build_int_cst (integer_type_node,
4246 gfc_option.flag_sign_zero));
4247 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
4248 build_int_cst (integer_type_node,
4250 & GFC_RTCHECK_BOUNDS)));
4251 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
4252 build_int_cst (integer_type_node,
4253 gfc_option.flag_range_check));
4255 array_type = build_array_type (integer_type_node,
4256 build_index_type (build_int_cst (NULL_TREE, 7)));
4257 array = build_constructor (array_type, v);
4258 TREE_CONSTANT (array) = 1;
4259 TREE_STATIC (array) = 1;
4261 /* Create a static variable to hold the jump table. */
4262 var = gfc_create_var (array_type, "options");
4263 TREE_CONSTANT (var) = 1;
4264 TREE_STATIC (var) = 1;
4265 TREE_READONLY (var) = 1;
4266 DECL_INITIAL (var) = array;
4267 var = gfc_build_addr_expr (build_pointer_type (integer_type_node), var);
4269 tmp = build_call_expr_loc (input_location,
4270 gfor_fndecl_set_options, 2,
4271 build_int_cst (integer_type_node, 8), var);
4272 gfc_add_expr_to_block (&body, tmp);
4275 /* If -ffpe-trap option was provided, add a call to set_fpe so that
4276 the library will raise a FPE when needed. */
4277 if (gfc_option.fpe != 0)
4279 tmp = build_call_expr_loc (input_location,
4280 gfor_fndecl_set_fpe, 1,
4281 build_int_cst (integer_type_node,
4283 gfc_add_expr_to_block (&body, tmp);
4286 /* If this is the main program and an -fconvert option was provided,
4287 add a call to set_convert. */
4289 if (gfc_option.convert != GFC_CONVERT_NATIVE)
4291 tmp = build_call_expr_loc (input_location,
4292 gfor_fndecl_set_convert, 1,
4293 build_int_cst (integer_type_node,
4294 gfc_option.convert));
4295 gfc_add_expr_to_block (&body, tmp);
4298 /* If this is the main program and an -frecord-marker option was provided,
4299 add a call to set_record_marker. */
4301 if (gfc_option.record_marker != 0)
4303 tmp = build_call_expr_loc (input_location,
4304 gfor_fndecl_set_record_marker, 1,
4305 build_int_cst (integer_type_node,
4306 gfc_option.record_marker));
4307 gfc_add_expr_to_block (&body, tmp);
4310 if (gfc_option.max_subrecord_length != 0)
4312 tmp = build_call_expr_loc (input_location,
4313 gfor_fndecl_set_max_subrecord_length, 1,
4314 build_int_cst (integer_type_node,
4315 gfc_option.max_subrecord_length));
4316 gfc_add_expr_to_block (&body, tmp);
4319 /* Call MAIN__(). */
4320 tmp = build_call_expr_loc (input_location,
4322 gfc_add_expr_to_block (&body, tmp);
4324 /* Mark MAIN__ as used. */
4325 TREE_USED (fndecl) = 1;
4328 tmp = fold_build2 (MODIFY_EXPR, integer_type_node, DECL_RESULT (ftn_main),
4329 build_int_cst (integer_type_node, 0));
4330 tmp = build1_v (RETURN_EXPR, tmp);
4331 gfc_add_expr_to_block (&body, tmp);
4334 DECL_SAVED_TREE (ftn_main) = gfc_finish_block (&body);
4337 /* Finish off this function and send it for code generation. */
4339 BLOCK_SUPERCONTEXT (DECL_INITIAL (ftn_main)) = ftn_main;
4341 DECL_SAVED_TREE (ftn_main)
4342 = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (ftn_main),
4343 DECL_INITIAL (ftn_main));
4345 /* Output the GENERIC tree. */
4346 dump_function (TDI_original, ftn_main);
4348 cgraph_finalize_function (ftn_main, true);
4352 pop_function_context ();
4353 saved_function_decls = saved_parent_function_decls;
4355 current_function_decl = old_context;
4359 /* Generate code for a function. */
4362 gfc_generate_function_code (gfc_namespace * ns)
4372 tree recurcheckvar = NULL_TREE;
4377 sym = ns->proc_name;
4379 /* Check that the frontend isn't still using this. */
4380 gcc_assert (sym->tlink == NULL);
4383 /* Create the declaration for functions with global scope. */
4384 if (!sym->backend_decl)
4385 gfc_create_function_decl (ns);
4387 fndecl = sym->backend_decl;
4388 old_context = current_function_decl;
4392 push_function_context ();
4393 saved_parent_function_decls = saved_function_decls;
4394 saved_function_decls = NULL_TREE;
4397 trans_function_start (sym);
4399 gfc_init_block (&block);
4401 if (ns->entries && ns->proc_name->ts.type == BT_CHARACTER)
4403 /* Copy length backend_decls to all entry point result
4408 gfc_conv_const_charlen (ns->proc_name->ts.u.cl);
4409 backend_decl = ns->proc_name->result->ts.u.cl->backend_decl;
4410 for (el = ns->entries; el; el = el->next)
4411 el->sym->result->ts.u.cl->backend_decl = backend_decl;
4414 /* Translate COMMON blocks. */
4415 gfc_trans_common (ns);
4417 /* Null the parent fake result declaration if this namespace is
4418 a module function or an external procedures. */
4419 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
4420 || ns->parent == NULL)
4421 parent_fake_result_decl = NULL_TREE;
4423 gfc_generate_contained_functions (ns);
4425 nonlocal_dummy_decls = NULL;
4426 nonlocal_dummy_decl_pset = NULL;
4428 generate_local_vars (ns);
4430 /* Keep the parent fake result declaration in module functions
4431 or external procedures. */
4432 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
4433 || ns->parent == NULL)
4434 current_fake_result_decl = parent_fake_result_decl;
4436 current_fake_result_decl = NULL_TREE;
4438 current_function_return_label = NULL;
4440 /* Now generate the code for the body of this function. */
4441 gfc_init_block (&body);
4443 is_recursive = sym->attr.recursive
4444 || (sym->attr.entry_master
4445 && sym->ns->entries->sym->attr.recursive);
4446 if ((gfc_option.rtcheck & GFC_RTCHECK_RECURSION)
4448 && !gfc_option.flag_recursive)
4452 asprintf (&msg, "Recursive call to nonrecursive procedure '%s'",
4454 recurcheckvar = gfc_create_var (boolean_type_node, "is_recursive");
4455 TREE_STATIC (recurcheckvar) = 1;
4456 DECL_INITIAL (recurcheckvar) = boolean_false_node;
4457 gfc_add_expr_to_block (&block, recurcheckvar);
4458 gfc_trans_runtime_check (true, false, recurcheckvar, &block,
4459 &sym->declared_at, msg);
4460 gfc_add_modify (&block, recurcheckvar, boolean_true_node);
4464 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
4465 && sym->attr.subroutine)
4467 tree alternate_return;
4468 alternate_return = gfc_get_fake_result_decl (sym, 0);
4469 gfc_add_modify (&body, alternate_return, integer_zero_node);
4474 /* Jump to the correct entry point. */
4475 tmp = gfc_trans_entry_master_switch (ns->entries);
4476 gfc_add_expr_to_block (&body, tmp);
4479 /* If bounds-checking is enabled, generate code to check passed in actual
4480 arguments against the expected dummy argument attributes (e.g. string
4482 if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !sym->attr.is_bind_c)
4483 add_argument_checking (&body, sym);
4485 tmp = gfc_trans_code (ns->code);
4486 gfc_add_expr_to_block (&body, tmp);
4488 /* Add a return label if needed. */
4489 if (current_function_return_label)
4491 tmp = build1_v (LABEL_EXPR, current_function_return_label);
4492 gfc_add_expr_to_block (&body, tmp);
4495 tmp = gfc_finish_block (&body);
4496 /* Add code to create and cleanup arrays. */
4497 tmp = gfc_trans_deferred_vars (sym, tmp);
4499 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node)
4501 if (sym->attr.subroutine || sym == sym->result)
4503 if (current_fake_result_decl != NULL)
4504 result = TREE_VALUE (current_fake_result_decl);
4507 current_fake_result_decl = NULL_TREE;
4510 result = sym->result->backend_decl;
4512 if (result != NULL_TREE
4513 && sym->attr.function
4514 && !sym->attr.pointer)
4516 if (sym->ts.type == BT_DERIVED
4517 && sym->ts.u.derived->attr.alloc_comp)
4519 rank = sym->as ? sym->as->rank : 0;
4520 tmp2 = gfc_nullify_alloc_comp (sym->ts.u.derived, result, rank);
4521 gfc_add_expr_to_block (&block, tmp2);
4523 else if (sym->attr.allocatable && sym->attr.dimension == 0)
4524 gfc_add_modify (&block, result, fold_convert (TREE_TYPE (result),
4525 null_pointer_node));
4528 gfc_add_expr_to_block (&block, tmp);
4530 /* Reset recursion-check variable. */
4531 if ((gfc_option.rtcheck & GFC_RTCHECK_RECURSION)
4533 && !gfc_option.flag_openmp
4534 && recurcheckvar != NULL_TREE)
4536 gfc_add_modify (&block, recurcheckvar, boolean_false_node);
4537 recurcheckvar = NULL;
4540 if (result == NULL_TREE)
4542 /* TODO: move to the appropriate place in resolve.c. */
4543 if (warn_return_type && !sym->attr.referenced && sym == sym->result)
4544 gfc_warning ("Return value of function '%s' at %L not set",
4545 sym->name, &sym->declared_at);
4547 TREE_NO_WARNING(sym->backend_decl) = 1;
4551 /* Set the return value to the dummy result variable. The
4552 types may be different for scalar default REAL functions
4553 with -ff2c, therefore we have to convert. */
4554 tmp = convert (TREE_TYPE (DECL_RESULT (fndecl)), result);
4555 tmp = fold_build2 (MODIFY_EXPR, TREE_TYPE (tmp),
4556 DECL_RESULT (fndecl), tmp);
4557 tmp = build1_v (RETURN_EXPR, tmp);
4558 gfc_add_expr_to_block (&block, tmp);
4563 gfc_add_expr_to_block (&block, tmp);
4564 /* Reset recursion-check variable. */
4565 if ((gfc_option.rtcheck & GFC_RTCHECK_RECURSION)
4567 && !gfc_option.flag_openmp
4568 && recurcheckvar != NULL_TREE)
4570 gfc_add_modify (&block, recurcheckvar, boolean_false_node);
4571 recurcheckvar = NULL_TREE;
4576 /* Add all the decls we created during processing. */
4577 decl = saved_function_decls;
4582 next = TREE_CHAIN (decl);
4583 TREE_CHAIN (decl) = NULL_TREE;
4587 saved_function_decls = NULL_TREE;
4589 DECL_SAVED_TREE (fndecl) = gfc_finish_block (&block);
4592 /* Finish off this function and send it for code generation. */
4594 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
4596 DECL_SAVED_TREE (fndecl)
4597 = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (fndecl),
4598 DECL_INITIAL (fndecl));
4600 if (nonlocal_dummy_decls)
4602 BLOCK_VARS (DECL_INITIAL (fndecl))
4603 = chainon (BLOCK_VARS (DECL_INITIAL (fndecl)), nonlocal_dummy_decls);
4604 pointer_set_destroy (nonlocal_dummy_decl_pset);
4605 nonlocal_dummy_decls = NULL;
4606 nonlocal_dummy_decl_pset = NULL;
4609 /* Output the GENERIC tree. */
4610 dump_function (TDI_original, fndecl);
4612 /* Store the end of the function, so that we get good line number
4613 info for the epilogue. */
4614 cfun->function_end_locus = input_location;
4616 /* We're leaving the context of this function, so zap cfun.
4617 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
4618 tree_rest_of_compilation. */
4623 pop_function_context ();
4624 saved_function_decls = saved_parent_function_decls;
4626 current_function_decl = old_context;
4628 if (decl_function_context (fndecl))
4629 /* Register this function with cgraph just far enough to get it
4630 added to our parent's nested function list. */
4631 (void) cgraph_node (fndecl);
4633 cgraph_finalize_function (fndecl, true);
4635 gfc_trans_use_stmts (ns);
4636 gfc_traverse_ns (ns, gfc_emit_parameter_debug_info);
4638 if (sym->attr.is_main_program)
4639 create_main_function (fndecl);
4644 gfc_generate_constructors (void)
4646 gcc_assert (gfc_static_ctors == NULL_TREE);
4654 if (gfc_static_ctors == NULL_TREE)
4657 fnname = get_file_function_name ("I");
4658 type = build_function_type_list (void_type_node, NULL_TREE);
4660 fndecl = build_decl (input_location,
4661 FUNCTION_DECL, fnname, type);
4662 TREE_PUBLIC (fndecl) = 1;
4664 decl = build_decl (input_location,
4665 RESULT_DECL, NULL_TREE, void_type_node);
4666 DECL_ARTIFICIAL (decl) = 1;
4667 DECL_IGNORED_P (decl) = 1;
4668 DECL_CONTEXT (decl) = fndecl;
4669 DECL_RESULT (fndecl) = decl;
4673 current_function_decl = fndecl;
4675 rest_of_decl_compilation (fndecl, 1, 0);
4677 make_decl_rtl (fndecl);
4679 init_function_start (fndecl);
4683 for (; gfc_static_ctors; gfc_static_ctors = TREE_CHAIN (gfc_static_ctors))
4685 tmp = build_call_expr_loc (input_location,
4686 TREE_VALUE (gfc_static_ctors), 0);
4687 DECL_SAVED_TREE (fndecl) = build_stmt (input_location, EXPR_STMT, tmp);
4693 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
4694 DECL_SAVED_TREE (fndecl)
4695 = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (fndecl),
4696 DECL_INITIAL (fndecl));
4698 free_after_parsing (cfun);
4699 free_after_compilation (cfun);
4701 tree_rest_of_compilation (fndecl);
4703 current_function_decl = NULL_TREE;
4707 /* Translates a BLOCK DATA program unit. This means emitting the
4708 commons contained therein plus their initializations. We also emit
4709 a globally visible symbol to make sure that each BLOCK DATA program
4710 unit remains unique. */
4713 gfc_generate_block_data (gfc_namespace * ns)
4718 /* Tell the backend the source location of the block data. */
4720 gfc_set_backend_locus (&ns->proc_name->declared_at);
4722 gfc_set_backend_locus (&gfc_current_locus);
4724 /* Process the DATA statements. */
4725 gfc_trans_common (ns);
4727 /* Create a global symbol with the mane of the block data. This is to
4728 generate linker errors if the same name is used twice. It is never
4731 id = gfc_sym_mangled_function_id (ns->proc_name);
4733 id = get_identifier ("__BLOCK_DATA__");
4735 decl = build_decl (input_location,
4736 VAR_DECL, id, gfc_array_index_type);
4737 TREE_PUBLIC (decl) = 1;
4738 TREE_STATIC (decl) = 1;
4739 DECL_IGNORED_P (decl) = 1;
4742 rest_of_decl_compilation (decl, 1, 0);
4746 /* Process the local variables of a BLOCK construct. */
4749 gfc_process_block_locals (gfc_namespace* ns)
4753 gcc_assert (saved_local_decls == NULL_TREE);
4754 generate_local_vars (ns);
4756 decl = saved_local_decls;
4761 next = TREE_CHAIN (decl);
4762 TREE_CHAIN (decl) = NULL_TREE;
4766 saved_local_decls = NULL_TREE;
4770 #include "gt-fortran-trans-decl.h"