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
;
59 /* Holds the variable DECLs for the current function. */
61 static GTY(()) tree saved_function_decls
;
62 static GTY(()) tree saved_parent_function_decls
;
64 static struct pointer_set_t
*nonlocal_dummy_decl_pset
;
65 static GTY(()) tree nonlocal_dummy_decls
;
67 /* Holds the variable DECLs that are locals. */
69 static GTY(()) tree saved_local_decls
;
71 /* The namespace of the module we're currently generating. Only used while
72 outputting decls for module variables. Do not rely on this being set. */
74 static gfc_namespace
*module_namespace
;
76 /* The currently processed procedure symbol. */
77 static gfc_symbol
* current_procedure_symbol
= NULL
;
80 /* List of static constructor functions. */
82 tree gfc_static_ctors
;
85 /* Function declarations for builtin library functions. */
87 tree gfor_fndecl_pause_numeric
;
88 tree gfor_fndecl_pause_string
;
89 tree gfor_fndecl_stop_numeric
;
90 tree gfor_fndecl_stop_numeric_f08
;
91 tree gfor_fndecl_stop_string
;
92 tree gfor_fndecl_error_stop_numeric
;
93 tree gfor_fndecl_error_stop_string
;
94 tree gfor_fndecl_runtime_error
;
95 tree gfor_fndecl_runtime_error_at
;
96 tree gfor_fndecl_runtime_warning_at
;
97 tree gfor_fndecl_os_error
;
98 tree gfor_fndecl_generate_error
;
99 tree gfor_fndecl_set_args
;
100 tree gfor_fndecl_set_fpe
;
101 tree gfor_fndecl_set_options
;
102 tree gfor_fndecl_set_convert
;
103 tree gfor_fndecl_set_record_marker
;
104 tree gfor_fndecl_set_max_subrecord_length
;
105 tree gfor_fndecl_ctime
;
106 tree gfor_fndecl_fdate
;
107 tree gfor_fndecl_ttynam
;
108 tree gfor_fndecl_in_pack
;
109 tree gfor_fndecl_in_unpack
;
110 tree gfor_fndecl_associated
;
113 /* Math functions. Many other math functions are handled in
114 trans-intrinsic.c. */
116 gfc_powdecl_list gfor_fndecl_math_powi
[4][3];
117 tree gfor_fndecl_math_ishftc4
;
118 tree gfor_fndecl_math_ishftc8
;
119 tree gfor_fndecl_math_ishftc16
;
122 /* String functions. */
124 tree gfor_fndecl_compare_string
;
125 tree gfor_fndecl_concat_string
;
126 tree gfor_fndecl_string_len_trim
;
127 tree gfor_fndecl_string_index
;
128 tree gfor_fndecl_string_scan
;
129 tree gfor_fndecl_string_verify
;
130 tree gfor_fndecl_string_trim
;
131 tree gfor_fndecl_string_minmax
;
132 tree gfor_fndecl_adjustl
;
133 tree gfor_fndecl_adjustr
;
134 tree gfor_fndecl_select_string
;
135 tree gfor_fndecl_compare_string_char4
;
136 tree gfor_fndecl_concat_string_char4
;
137 tree gfor_fndecl_string_len_trim_char4
;
138 tree gfor_fndecl_string_index_char4
;
139 tree gfor_fndecl_string_scan_char4
;
140 tree gfor_fndecl_string_verify_char4
;
141 tree gfor_fndecl_string_trim_char4
;
142 tree gfor_fndecl_string_minmax_char4
;
143 tree gfor_fndecl_adjustl_char4
;
144 tree gfor_fndecl_adjustr_char4
;
145 tree gfor_fndecl_select_string_char4
;
148 /* Conversion between character kinds. */
149 tree gfor_fndecl_convert_char1_to_char4
;
150 tree gfor_fndecl_convert_char4_to_char1
;
153 /* Other misc. runtime library functions. */
154 tree gfor_fndecl_size0
;
155 tree gfor_fndecl_size1
;
156 tree gfor_fndecl_iargc
;
158 /* Intrinsic functions implemented in Fortran. */
159 tree gfor_fndecl_sc_kind
;
160 tree gfor_fndecl_si_kind
;
161 tree gfor_fndecl_sr_kind
;
163 /* BLAS gemm functions. */
164 tree gfor_fndecl_sgemm
;
165 tree gfor_fndecl_dgemm
;
166 tree gfor_fndecl_cgemm
;
167 tree gfor_fndecl_zgemm
;
171 gfc_add_decl_to_parent_function (tree decl
)
174 DECL_CONTEXT (decl
) = DECL_CONTEXT (current_function_decl
);
175 DECL_NONLOCAL (decl
) = 1;
176 DECL_CHAIN (decl
) = saved_parent_function_decls
;
177 saved_parent_function_decls
= decl
;
181 gfc_add_decl_to_function (tree decl
)
184 TREE_USED (decl
) = 1;
185 DECL_CONTEXT (decl
) = current_function_decl
;
186 DECL_CHAIN (decl
) = saved_function_decls
;
187 saved_function_decls
= decl
;
191 add_decl_as_local (tree decl
)
194 TREE_USED (decl
) = 1;
195 DECL_CONTEXT (decl
) = current_function_decl
;
196 DECL_CHAIN (decl
) = saved_local_decls
;
197 saved_local_decls
= decl
;
201 /* Build a backend label declaration. Set TREE_USED for named labels.
202 The context of the label is always the current_function_decl. All
203 labels are marked artificial. */
206 gfc_build_label_decl (tree label_id
)
208 /* 2^32 temporaries should be enough. */
209 static unsigned int tmp_num
= 1;
213 if (label_id
== NULL_TREE
)
215 /* Build an internal label name. */
216 ASM_FORMAT_PRIVATE_NAME (label_name
, "L", tmp_num
++);
217 label_id
= get_identifier (label_name
);
222 /* Build the LABEL_DECL node. Labels have no type. */
223 label_decl
= build_decl (input_location
,
224 LABEL_DECL
, label_id
, void_type_node
);
225 DECL_CONTEXT (label_decl
) = current_function_decl
;
226 DECL_MODE (label_decl
) = VOIDmode
;
228 /* We always define the label as used, even if the original source
229 file never references the label. We don't want all kinds of
230 spurious warnings for old-style Fortran code with too many
232 TREE_USED (label_decl
) = 1;
234 DECL_ARTIFICIAL (label_decl
) = 1;
239 /* Set the backend source location of a decl. */
242 gfc_set_decl_location (tree decl
, locus
* loc
)
244 DECL_SOURCE_LOCATION (decl
) = loc
->lb
->location
;
248 /* Return the backend label declaration for a given label structure,
249 or create it if it doesn't exist yet. */
252 gfc_get_label_decl (gfc_st_label
* lp
)
254 if (lp
->backend_decl
)
255 return lp
->backend_decl
;
258 char label_name
[GFC_MAX_SYMBOL_LEN
+ 1];
261 /* Validate the label declaration from the front end. */
262 gcc_assert (lp
!= NULL
&& lp
->value
<= MAX_LABEL_VALUE
);
264 /* Build a mangled name for the label. */
265 sprintf (label_name
, "__label_%.6d", lp
->value
);
267 /* Build the LABEL_DECL node. */
268 label_decl
= gfc_build_label_decl (get_identifier (label_name
));
270 /* Tell the debugger where the label came from. */
271 if (lp
->value
<= MAX_LABEL_VALUE
) /* An internal label. */
272 gfc_set_decl_location (label_decl
, &lp
->where
);
274 DECL_ARTIFICIAL (label_decl
) = 1;
276 /* Store the label in the label list and return the LABEL_DECL. */
277 lp
->backend_decl
= label_decl
;
283 /* Convert a gfc_symbol to an identifier of the same name. */
286 gfc_sym_identifier (gfc_symbol
* sym
)
288 if (sym
->attr
.is_main_program
&& strcmp (sym
->name
, "main") == 0)
289 return (get_identifier ("MAIN__"));
291 return (get_identifier (sym
->name
));
295 /* Construct mangled name from symbol name. */
298 gfc_sym_mangled_identifier (gfc_symbol
* sym
)
300 char name
[GFC_MAX_MANGLED_SYMBOL_LEN
+ 1];
302 /* Prevent the mangling of identifiers that have an assigned
303 binding label (mainly those that are bind(c)). */
304 if (sym
->attr
.is_bind_c
== 1
305 && sym
->binding_label
[0] != '\0')
306 return get_identifier(sym
->binding_label
);
308 if (sym
->module
== NULL
)
309 return gfc_sym_identifier (sym
);
312 snprintf (name
, sizeof name
, "__%s_MOD_%s", sym
->module
, sym
->name
);
313 return get_identifier (name
);
318 /* Construct mangled function name from symbol name. */
321 gfc_sym_mangled_function_id (gfc_symbol
* sym
)
324 char name
[GFC_MAX_MANGLED_SYMBOL_LEN
+ 1];
326 /* It may be possible to simply use the binding label if it's
327 provided, and remove the other checks. Then we could use it
328 for other things if we wished. */
329 if ((sym
->attr
.is_bind_c
== 1 || sym
->attr
.is_iso_c
== 1) &&
330 sym
->binding_label
[0] != '\0')
331 /* use the binding label rather than the mangled name */
332 return get_identifier (sym
->binding_label
);
334 if (sym
->module
== NULL
|| sym
->attr
.proc
== PROC_EXTERNAL
335 || (sym
->module
!= NULL
&& (sym
->attr
.external
336 || sym
->attr
.if_source
== IFSRC_IFBODY
)))
338 /* Main program is mangled into MAIN__. */
339 if (sym
->attr
.is_main_program
)
340 return get_identifier ("MAIN__");
342 /* Intrinsic procedures are never mangled. */
343 if (sym
->attr
.proc
== PROC_INTRINSIC
)
344 return get_identifier (sym
->name
);
346 if (gfc_option
.flag_underscoring
)
348 has_underscore
= strchr (sym
->name
, '_') != 0;
349 if (gfc_option
.flag_second_underscore
&& has_underscore
)
350 snprintf (name
, sizeof name
, "%s__", sym
->name
);
352 snprintf (name
, sizeof name
, "%s_", sym
->name
);
353 return get_identifier (name
);
356 return get_identifier (sym
->name
);
360 snprintf (name
, sizeof name
, "__%s_MOD_%s", sym
->module
, sym
->name
);
361 return get_identifier (name
);
367 gfc_set_decl_assembler_name (tree decl
, tree name
)
369 tree target_mangled
= targetm
.mangle_decl_assembler_name (decl
, name
);
370 SET_DECL_ASSEMBLER_NAME (decl
, target_mangled
);
374 /* Returns true if a variable of specified size should go on the stack. */
377 gfc_can_put_var_on_stack (tree size
)
379 unsigned HOST_WIDE_INT low
;
381 if (!INTEGER_CST_P (size
))
384 if (gfc_option
.flag_max_stack_var_size
< 0)
387 if (TREE_INT_CST_HIGH (size
) != 0)
390 low
= TREE_INT_CST_LOW (size
);
391 if (low
> (unsigned HOST_WIDE_INT
) gfc_option
.flag_max_stack_var_size
)
394 /* TODO: Set a per-function stack size limit. */
400 /* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
401 an expression involving its corresponding pointer. There are
402 2 cases; one for variable size arrays, and one for everything else,
403 because variable-sized arrays require one fewer level of
407 gfc_finish_cray_pointee (tree decl
, gfc_symbol
*sym
)
409 tree ptr_decl
= gfc_get_symbol_decl (sym
->cp_pointer
);
412 /* Parameters need to be dereferenced. */
413 if (sym
->cp_pointer
->attr
.dummy
)
414 ptr_decl
= build_fold_indirect_ref_loc (input_location
,
417 /* Check to see if we're dealing with a variable-sized array. */
418 if (sym
->attr
.dimension
419 && TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
)
421 /* These decls will be dereferenced later, so we don't dereference
423 value
= convert (TREE_TYPE (decl
), ptr_decl
);
427 ptr_decl
= convert (build_pointer_type (TREE_TYPE (decl
)),
429 value
= build_fold_indirect_ref_loc (input_location
,
433 SET_DECL_VALUE_EXPR (decl
, value
);
434 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
435 GFC_DECL_CRAY_POINTEE (decl
) = 1;
436 /* This is a fake variable just for debugging purposes. */
437 TREE_ASM_WRITTEN (decl
) = 1;
441 /* Finish processing of a declaration without an initial value. */
444 gfc_finish_decl (tree decl
)
446 gcc_assert (TREE_CODE (decl
) == PARM_DECL
447 || DECL_INITIAL (decl
) == NULL_TREE
);
449 if (TREE_CODE (decl
) != VAR_DECL
)
452 if (DECL_SIZE (decl
) == NULL_TREE
453 && TYPE_SIZE (TREE_TYPE (decl
)) != NULL_TREE
)
454 layout_decl (decl
, 0);
456 /* A few consistency checks. */
457 /* A static variable with an incomplete type is an error if it is
458 initialized. Also if it is not file scope. Otherwise, let it
459 through, but if it is not `extern' then it may cause an error
461 /* An automatic variable with an incomplete type is an error. */
463 /* We should know the storage size. */
464 gcc_assert (DECL_SIZE (decl
) != NULL_TREE
465 || (TREE_STATIC (decl
)
466 ? (!DECL_INITIAL (decl
) || !DECL_CONTEXT (decl
))
467 : DECL_EXTERNAL (decl
)));
469 /* The storage size should be constant. */
470 gcc_assert ((!DECL_EXTERNAL (decl
) && !TREE_STATIC (decl
))
472 || TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
);
476 /* Apply symbol attributes to a variable, and add it to the function scope. */
479 gfc_finish_var_decl (tree decl
, gfc_symbol
* sym
)
482 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
483 This is the equivalent of the TARGET variables.
484 We also need to set this if the variable is passed by reference in a
487 /* Set DECL_VALUE_EXPR for Cray Pointees. */
488 if (sym
->attr
.cray_pointee
)
489 gfc_finish_cray_pointee (decl
, sym
);
491 if (sym
->attr
.target
)
492 TREE_ADDRESSABLE (decl
) = 1;
493 /* If it wasn't used we wouldn't be getting it. */
494 TREE_USED (decl
) = 1;
496 /* Chain this decl to the pending declarations. Don't do pushdecl()
497 because this would add them to the current scope rather than the
499 if (current_function_decl
!= NULL_TREE
)
501 if (sym
->ns
->proc_name
->backend_decl
== current_function_decl
502 || sym
->result
== sym
)
503 gfc_add_decl_to_function (decl
);
504 else if (sym
->ns
->proc_name
->attr
.flavor
== FL_LABEL
)
505 /* This is a BLOCK construct. */
506 add_decl_as_local (decl
);
508 gfc_add_decl_to_parent_function (decl
);
511 if (sym
->attr
.cray_pointee
)
514 if(sym
->attr
.is_bind_c
== 1)
516 /* We need to put variables that are bind(c) into the common
517 segment of the object file, because this is what C would do.
518 gfortran would typically put them in either the BSS or
519 initialized data segments, and only mark them as common if
520 they were part of common blocks. However, if they are not put
521 into common space, then C cannot initialize global Fortran
522 variables that it interoperates with and the draft says that
523 either Fortran or C should be able to initialize it (but not
524 both, of course.) (J3/04-007, section 15.3). */
525 TREE_PUBLIC(decl
) = 1;
526 DECL_COMMON(decl
) = 1;
529 /* If a variable is USE associated, it's always external. */
530 if (sym
->attr
.use_assoc
)
532 DECL_EXTERNAL (decl
) = 1;
533 TREE_PUBLIC (decl
) = 1;
535 else if (sym
->module
&& !sym
->attr
.result
&& !sym
->attr
.dummy
)
537 /* TODO: Don't set sym->module for result or dummy variables. */
538 gcc_assert (current_function_decl
== NULL_TREE
|| sym
->result
== sym
);
539 /* This is the declaration of a module variable. */
540 TREE_PUBLIC (decl
) = 1;
541 TREE_STATIC (decl
) = 1;
544 /* Derived types are a bit peculiar because of the possibility of
545 a default initializer; this must be applied each time the variable
546 comes into scope it therefore need not be static. These variables
547 are SAVE_NONE but have an initializer. Otherwise explicitly
548 initialized variables are SAVE_IMPLICIT and explicitly saved are
550 if (!sym
->attr
.use_assoc
551 && (sym
->attr
.save
!= SAVE_NONE
|| sym
->attr
.data
552 || (sym
->value
&& sym
->ns
->proc_name
->attr
.is_main_program
)))
553 TREE_STATIC (decl
) = 1;
555 if (sym
->attr
.volatile_
)
557 TREE_THIS_VOLATILE (decl
) = 1;
558 TREE_SIDE_EFFECTS (decl
) = 1;
559 new_type
= build_qualified_type (TREE_TYPE (decl
), TYPE_QUAL_VOLATILE
);
560 TREE_TYPE (decl
) = new_type
;
563 /* Keep variables larger than max-stack-var-size off stack. */
564 if (!sym
->ns
->proc_name
->attr
.recursive
565 && INTEGER_CST_P (DECL_SIZE_UNIT (decl
))
566 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl
))
567 /* Put variable length auto array pointers always into stack. */
568 && (TREE_CODE (TREE_TYPE (decl
)) != POINTER_TYPE
569 || sym
->attr
.dimension
== 0
570 || sym
->as
->type
!= AS_EXPLICIT
572 || sym
->attr
.allocatable
)
573 && !DECL_ARTIFICIAL (decl
))
574 TREE_STATIC (decl
) = 1;
576 /* Handle threadprivate variables. */
577 if (sym
->attr
.threadprivate
578 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
579 DECL_TLS_MODEL (decl
) = decl_default_tls_model (decl
);
581 if (!sym
->attr
.target
582 && !sym
->attr
.pointer
583 && !sym
->attr
.cray_pointee
584 && !sym
->attr
.proc_pointer
)
585 DECL_RESTRICTED_P (decl
) = 1;
589 /* Allocate the lang-specific part of a decl. */
592 gfc_allocate_lang_decl (tree decl
)
594 DECL_LANG_SPECIFIC (decl
) = ggc_alloc_cleared_lang_decl(sizeof
598 /* Remember a symbol to generate initialization/cleanup code at function
602 gfc_defer_symbol_init (gfc_symbol
* sym
)
608 /* Don't add a symbol twice. */
612 last
= head
= sym
->ns
->proc_name
;
615 /* Make sure that setup code for dummy variables which are used in the
616 setup of other variables is generated first. */
619 /* Find the first dummy arg seen after us, or the first non-dummy arg.
620 This is a circular list, so don't go past the head. */
622 && (!p
->attr
.dummy
|| p
->dummy_order
> sym
->dummy_order
))
628 /* Insert in between last and p. */
634 /* Create an array index type variable with function scope. */
637 create_index_var (const char * pfx
, int nest
)
641 decl
= gfc_create_var_np (gfc_array_index_type
, pfx
);
643 gfc_add_decl_to_parent_function (decl
);
645 gfc_add_decl_to_function (decl
);
650 /* Create variables to hold all the non-constant bits of info for a
651 descriptorless array. Remember these in the lang-specific part of the
655 gfc_build_qualified_array (tree decl
, gfc_symbol
* sym
)
660 gfc_namespace
* procns
;
662 type
= TREE_TYPE (decl
);
664 /* We just use the descriptor, if there is one. */
665 if (GFC_DESCRIPTOR_TYPE_P (type
))
668 gcc_assert (GFC_ARRAY_TYPE_P (type
));
669 procns
= gfc_find_proc_namespace (sym
->ns
);
670 nest
= (procns
->proc_name
->backend_decl
!= current_function_decl
)
671 && !sym
->attr
.contained
;
673 for (dim
= 0; dim
< GFC_TYPE_ARRAY_RANK (type
); dim
++)
675 if (GFC_TYPE_ARRAY_LBOUND (type
, dim
) == NULL_TREE
)
677 GFC_TYPE_ARRAY_LBOUND (type
, dim
) = create_index_var ("lbound", nest
);
678 TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type
, dim
)) = 1;
680 /* Don't try to use the unknown bound for assumed shape arrays. */
681 if (GFC_TYPE_ARRAY_UBOUND (type
, dim
) == NULL_TREE
682 && (sym
->as
->type
!= AS_ASSUMED_SIZE
683 || dim
< GFC_TYPE_ARRAY_RANK (type
) - 1))
685 GFC_TYPE_ARRAY_UBOUND (type
, dim
) = create_index_var ("ubound", nest
);
686 TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type
, dim
)) = 1;
689 if (GFC_TYPE_ARRAY_STRIDE (type
, dim
) == NULL_TREE
)
691 GFC_TYPE_ARRAY_STRIDE (type
, dim
) = create_index_var ("stride", nest
);
692 TREE_NO_WARNING (GFC_TYPE_ARRAY_STRIDE (type
, dim
)) = 1;
695 if (GFC_TYPE_ARRAY_OFFSET (type
) == NULL_TREE
)
697 GFC_TYPE_ARRAY_OFFSET (type
) = gfc_create_var_np (gfc_array_index_type
,
699 TREE_NO_WARNING (GFC_TYPE_ARRAY_OFFSET (type
)) = 1;
702 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type
));
704 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type
));
707 if (GFC_TYPE_ARRAY_SIZE (type
) == NULL_TREE
708 && sym
->as
->type
!= AS_ASSUMED_SIZE
)
710 GFC_TYPE_ARRAY_SIZE (type
) = create_index_var ("size", nest
);
711 TREE_NO_WARNING (GFC_TYPE_ARRAY_SIZE (type
)) = 1;
714 if (POINTER_TYPE_P (type
))
716 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type
)));
717 gcc_assert (TYPE_LANG_SPECIFIC (type
)
718 == TYPE_LANG_SPECIFIC (TREE_TYPE (type
)));
719 type
= TREE_TYPE (type
);
722 if (! COMPLETE_TYPE_P (type
) && GFC_TYPE_ARRAY_SIZE (type
))
726 size
= fold_build2_loc (input_location
, MINUS_EXPR
, gfc_array_index_type
,
727 GFC_TYPE_ARRAY_SIZE (type
), gfc_index_one_node
);
728 range
= build_range_type (gfc_array_index_type
, gfc_index_zero_node
,
730 TYPE_DOMAIN (type
) = range
;
734 if (TYPE_NAME (type
) != NULL_TREE
735 && GFC_TYPE_ARRAY_UBOUND (type
, sym
->as
->rank
- 1) != NULL_TREE
736 && TREE_CODE (GFC_TYPE_ARRAY_UBOUND (type
, sym
->as
->rank
- 1)) == VAR_DECL
)
738 tree gtype
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
740 for (dim
= 0; dim
< sym
->as
->rank
- 1; dim
++)
742 gcc_assert (TREE_CODE (gtype
) == ARRAY_TYPE
);
743 gtype
= TREE_TYPE (gtype
);
745 gcc_assert (TREE_CODE (gtype
) == ARRAY_TYPE
);
746 if (TYPE_MAX_VALUE (TYPE_DOMAIN (gtype
)) == NULL
)
747 TYPE_NAME (type
) = NULL_TREE
;
750 if (TYPE_NAME (type
) == NULL_TREE
)
752 tree gtype
= TREE_TYPE (type
), rtype
, type_decl
;
754 for (dim
= sym
->as
->rank
- 1; dim
>= 0; dim
--)
757 lbound
= GFC_TYPE_ARRAY_LBOUND (type
, dim
);
758 ubound
= GFC_TYPE_ARRAY_UBOUND (type
, dim
);
759 rtype
= build_range_type (gfc_array_index_type
, lbound
, ubound
);
760 gtype
= build_array_type (gtype
, rtype
);
761 /* Ensure the bound variables aren't optimized out at -O0.
762 For -O1 and above they often will be optimized out, but
763 can be tracked by VTA. Also set DECL_NAMELESS, so that
764 the artificial lbound.N or ubound.N DECL_NAME doesn't
765 end up in debug info. */
766 if (lbound
&& TREE_CODE (lbound
) == VAR_DECL
767 && DECL_ARTIFICIAL (lbound
) && DECL_IGNORED_P (lbound
))
769 if (DECL_NAME (lbound
)
770 && strstr (IDENTIFIER_POINTER (DECL_NAME (lbound
)),
772 DECL_NAMELESS (lbound
) = 1;
773 DECL_IGNORED_P (lbound
) = 0;
775 if (ubound
&& TREE_CODE (ubound
) == VAR_DECL
776 && DECL_ARTIFICIAL (ubound
) && DECL_IGNORED_P (ubound
))
778 if (DECL_NAME (ubound
)
779 && strstr (IDENTIFIER_POINTER (DECL_NAME (ubound
)),
781 DECL_NAMELESS (ubound
) = 1;
782 DECL_IGNORED_P (ubound
) = 0;
785 TYPE_NAME (type
) = type_decl
= build_decl (input_location
,
786 TYPE_DECL
, NULL
, gtype
);
787 DECL_ORIGINAL_TYPE (type_decl
) = gtype
;
792 /* For some dummy arguments we don't use the actual argument directly.
793 Instead we create a local decl and use that. This allows us to perform
794 initialization, and construct full type information. */
797 gfc_build_dummy_array_decl (gfc_symbol
* sym
, tree dummy
)
807 if (sym
->attr
.pointer
|| sym
->attr
.allocatable
)
810 /* Add to list of variables if not a fake result variable. */
811 if (sym
->attr
.result
|| sym
->attr
.dummy
)
812 gfc_defer_symbol_init (sym
);
814 type
= TREE_TYPE (dummy
);
815 gcc_assert (TREE_CODE (dummy
) == PARM_DECL
816 && POINTER_TYPE_P (type
));
818 /* Do we know the element size? */
819 known_size
= sym
->ts
.type
!= BT_CHARACTER
820 || INTEGER_CST_P (sym
->ts
.u
.cl
->backend_decl
);
822 if (known_size
&& !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type
)))
824 /* For descriptorless arrays with known element size the actual
825 argument is sufficient. */
826 gcc_assert (GFC_ARRAY_TYPE_P (type
));
827 gfc_build_qualified_array (dummy
, sym
);
831 type
= TREE_TYPE (type
);
832 if (GFC_DESCRIPTOR_TYPE_P (type
))
834 /* Create a descriptorless array pointer. */
838 /* Even when -frepack-arrays is used, symbols with TARGET attribute
840 if (!gfc_option
.flag_repack_arrays
|| sym
->attr
.target
)
842 if (as
->type
== AS_ASSUMED_SIZE
)
843 packed
= PACKED_FULL
;
847 if (as
->type
== AS_EXPLICIT
)
849 packed
= PACKED_FULL
;
850 for (n
= 0; n
< as
->rank
; n
++)
854 && as
->upper
[n
]->expr_type
== EXPR_CONSTANT
855 && as
->lower
[n
]->expr_type
== EXPR_CONSTANT
))
856 packed
= PACKED_PARTIAL
;
860 packed
= PACKED_PARTIAL
;
863 type
= gfc_typenode_for_spec (&sym
->ts
);
864 type
= gfc_get_nodesc_array_type (type
, sym
->as
, packed
,
869 /* We now have an expression for the element size, so create a fully
870 qualified type. Reset sym->backend decl or this will just return the
872 DECL_ARTIFICIAL (sym
->backend_decl
) = 1;
873 sym
->backend_decl
= NULL_TREE
;
874 type
= gfc_sym_type (sym
);
875 packed
= PACKED_FULL
;
878 ASM_FORMAT_PRIVATE_NAME (name
, IDENTIFIER_POINTER (DECL_NAME (dummy
)), 0);
879 decl
= build_decl (input_location
,
880 VAR_DECL
, get_identifier (name
), type
);
882 DECL_ARTIFICIAL (decl
) = 1;
883 DECL_NAMELESS (decl
) = 1;
884 TREE_PUBLIC (decl
) = 0;
885 TREE_STATIC (decl
) = 0;
886 DECL_EXTERNAL (decl
) = 0;
888 /* We should never get deferred shape arrays here. We used to because of
890 gcc_assert (sym
->as
->type
!= AS_DEFERRED
);
892 if (packed
== PACKED_PARTIAL
)
893 GFC_DECL_PARTIAL_PACKED_ARRAY (decl
) = 1;
894 else if (packed
== PACKED_FULL
)
895 GFC_DECL_PACKED_ARRAY (decl
) = 1;
897 gfc_build_qualified_array (decl
, sym
);
899 if (DECL_LANG_SPECIFIC (dummy
))
900 DECL_LANG_SPECIFIC (decl
) = DECL_LANG_SPECIFIC (dummy
);
902 gfc_allocate_lang_decl (decl
);
904 GFC_DECL_SAVED_DESCRIPTOR (decl
) = dummy
;
906 if (sym
->ns
->proc_name
->backend_decl
== current_function_decl
907 || sym
->attr
.contained
)
908 gfc_add_decl_to_function (decl
);
910 gfc_add_decl_to_parent_function (decl
);
915 /* For symbol SYM with GFC_DECL_SAVED_DESCRIPTOR used in contained
916 function add a VAR_DECL to the current function with DECL_VALUE_EXPR
917 pointing to the artificial variable for debug info purposes. */
920 gfc_nonlocal_dummy_array_decl (gfc_symbol
*sym
)
924 if (! nonlocal_dummy_decl_pset
)
925 nonlocal_dummy_decl_pset
= pointer_set_create ();
927 if (pointer_set_insert (nonlocal_dummy_decl_pset
, sym
->backend_decl
))
930 dummy
= GFC_DECL_SAVED_DESCRIPTOR (sym
->backend_decl
);
931 decl
= build_decl (input_location
, VAR_DECL
, DECL_NAME (dummy
),
932 TREE_TYPE (sym
->backend_decl
));
933 DECL_ARTIFICIAL (decl
) = 0;
934 TREE_USED (decl
) = 1;
935 TREE_PUBLIC (decl
) = 0;
936 TREE_STATIC (decl
) = 0;
937 DECL_EXTERNAL (decl
) = 0;
938 if (DECL_BY_REFERENCE (dummy
))
939 DECL_BY_REFERENCE (decl
) = 1;
940 DECL_LANG_SPECIFIC (decl
) = DECL_LANG_SPECIFIC (sym
->backend_decl
);
941 SET_DECL_VALUE_EXPR (decl
, sym
->backend_decl
);
942 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
943 DECL_CONTEXT (decl
) = DECL_CONTEXT (sym
->backend_decl
);
944 DECL_CHAIN (decl
) = nonlocal_dummy_decls
;
945 nonlocal_dummy_decls
= decl
;
948 /* Return a constant or a variable to use as a string length. Does not
949 add the decl to the current scope. */
952 gfc_create_string_length (gfc_symbol
* sym
)
954 gcc_assert (sym
->ts
.u
.cl
);
955 gfc_conv_const_charlen (sym
->ts
.u
.cl
);
957 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
960 char name
[GFC_MAX_MANGLED_SYMBOL_LEN
+ 2];
962 /* Also prefix the mangled name. */
963 strcpy (&name
[1], sym
->name
);
965 length
= build_decl (input_location
,
966 VAR_DECL
, get_identifier (name
),
967 gfc_charlen_type_node
);
968 DECL_ARTIFICIAL (length
) = 1;
969 TREE_USED (length
) = 1;
970 if (sym
->ns
->proc_name
->tlink
!= NULL
)
971 gfc_defer_symbol_init (sym
);
973 sym
->ts
.u
.cl
->backend_decl
= length
;
976 gcc_assert (sym
->ts
.u
.cl
->backend_decl
!= NULL_TREE
);
977 return sym
->ts
.u
.cl
->backend_decl
;
980 /* If a variable is assigned a label, we add another two auxiliary
984 gfc_add_assign_aux_vars (gfc_symbol
* sym
)
990 gcc_assert (sym
->backend_decl
);
992 decl
= sym
->backend_decl
;
993 gfc_allocate_lang_decl (decl
);
994 GFC_DECL_ASSIGN (decl
) = 1;
995 length
= build_decl (input_location
,
996 VAR_DECL
, create_tmp_var_name (sym
->name
),
997 gfc_charlen_type_node
);
998 addr
= build_decl (input_location
,
999 VAR_DECL
, create_tmp_var_name (sym
->name
),
1001 gfc_finish_var_decl (length
, sym
);
1002 gfc_finish_var_decl (addr
, sym
);
1003 /* STRING_LENGTH is also used as flag. Less than -1 means that
1004 ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the
1005 target label's address. Otherwise, value is the length of a format string
1006 and ASSIGN_ADDR is its address. */
1007 if (TREE_STATIC (length
))
1008 DECL_INITIAL (length
) = build_int_cst (NULL_TREE
, -2);
1010 gfc_defer_symbol_init (sym
);
1012 GFC_DECL_STRING_LEN (decl
) = length
;
1013 GFC_DECL_ASSIGN_ADDR (decl
) = addr
;
1018 add_attributes_to_decl (symbol_attribute sym_attr
, tree list
)
1023 for (id
= 0; id
< EXT_ATTR_NUM
; id
++)
1024 if (sym_attr
.ext_attr
& (1 << id
))
1026 attr
= build_tree_list (
1027 get_identifier (ext_attr_list
[id
].middle_end_name
),
1029 list
= chainon (list
, attr
);
1036 static void build_function_decl (gfc_symbol
* sym
, bool global
);
1039 /* Return the decl for a gfc_symbol, create it if it doesn't already
1043 gfc_get_symbol_decl (gfc_symbol
* sym
)
1046 tree length
= NULL_TREE
;
1049 bool intrinsic_array_parameter
= false;
1051 gcc_assert (sym
->attr
.referenced
1052 || sym
->attr
.use_assoc
1053 || sym
->ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
1054 || (sym
->module
&& sym
->attr
.if_source
!= IFSRC_DECL
1055 && sym
->backend_decl
));
1057 if (sym
->ns
&& sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.function
)
1058 byref
= gfc_return_by_reference (sym
->ns
->proc_name
);
1062 /* Make sure that the vtab for the declared type is completed. */
1063 if (sym
->ts
.type
== BT_CLASS
)
1065 gfc_component
*c
= CLASS_DATA (sym
);
1066 if (!c
->ts
.u
.derived
->backend_decl
)
1067 gfc_find_derived_vtab (c
->ts
.u
.derived
);
1070 if ((sym
->attr
.dummy
&& ! sym
->attr
.function
) || (sym
->attr
.result
&& byref
))
1072 /* Return via extra parameter. */
1073 if (sym
->attr
.result
&& byref
1074 && !sym
->backend_decl
)
1077 DECL_ARGUMENTS (sym
->ns
->proc_name
->backend_decl
);
1078 /* For entry master function skip over the __entry
1080 if (sym
->ns
->proc_name
->attr
.entry_master
)
1081 sym
->backend_decl
= DECL_CHAIN (sym
->backend_decl
);
1084 /* Dummy variables should already have been created. */
1085 gcc_assert (sym
->backend_decl
);
1087 /* Create a character length variable. */
1088 if (sym
->ts
.type
== BT_CHARACTER
)
1090 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
1091 length
= gfc_create_string_length (sym
);
1093 length
= sym
->ts
.u
.cl
->backend_decl
;
1094 if (TREE_CODE (length
) == VAR_DECL
1095 && DECL_FILE_SCOPE_P (length
))
1097 /* Add the string length to the same context as the symbol. */
1098 if (DECL_CONTEXT (sym
->backend_decl
) == current_function_decl
)
1099 gfc_add_decl_to_function (length
);
1101 gfc_add_decl_to_parent_function (length
);
1103 gcc_assert (DECL_CONTEXT (sym
->backend_decl
) ==
1104 DECL_CONTEXT (length
));
1106 gfc_defer_symbol_init (sym
);
1110 /* Use a copy of the descriptor for dummy arrays. */
1111 if (sym
->attr
.dimension
&& !TREE_USED (sym
->backend_decl
))
1113 decl
= gfc_build_dummy_array_decl (sym
, sym
->backend_decl
);
1114 /* Prevent the dummy from being detected as unused if it is copied. */
1115 if (sym
->backend_decl
!= NULL
&& decl
!= sym
->backend_decl
)
1116 DECL_ARTIFICIAL (sym
->backend_decl
) = 1;
1117 sym
->backend_decl
= decl
;
1120 TREE_USED (sym
->backend_decl
) = 1;
1121 if (sym
->attr
.assign
&& GFC_DECL_ASSIGN (sym
->backend_decl
) == 0)
1123 gfc_add_assign_aux_vars (sym
);
1126 if (sym
->attr
.dimension
1127 && DECL_LANG_SPECIFIC (sym
->backend_decl
)
1128 && GFC_DECL_SAVED_DESCRIPTOR (sym
->backend_decl
)
1129 && DECL_CONTEXT (sym
->backend_decl
) != current_function_decl
)
1130 gfc_nonlocal_dummy_array_decl (sym
);
1132 return sym
->backend_decl
;
1135 if (sym
->backend_decl
)
1136 return sym
->backend_decl
;
1138 /* Special case for array-valued named constants from intrinsic
1139 procedures; those are inlined. */
1140 if (sym
->attr
.use_assoc
&& sym
->from_intmod
1141 && sym
->attr
.flavor
== FL_PARAMETER
)
1142 intrinsic_array_parameter
= true;
1144 /* If use associated and whole file compilation, use the module
1146 if (gfc_option
.flag_whole_file
1147 && (sym
->attr
.flavor
== FL_VARIABLE
1148 || sym
->attr
.flavor
== FL_PARAMETER
)
1149 && sym
->attr
.use_assoc
&& !intrinsic_array_parameter
1154 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->module
);
1155 if (gsym
&& gsym
->ns
&& gsym
->type
== GSYM_MODULE
)
1159 gfc_find_symbol (sym
->name
, gsym
->ns
, 0, &s
);
1160 if (s
&& s
->backend_decl
)
1162 if (sym
->ts
.type
== BT_DERIVED
)
1163 gfc_copy_dt_decls_ifequal (s
->ts
.u
.derived
, sym
->ts
.u
.derived
,
1165 if (sym
->ts
.type
== BT_CHARACTER
)
1166 sym
->ts
.u
.cl
->backend_decl
= s
->ts
.u
.cl
->backend_decl
;
1167 sym
->backend_decl
= s
->backend_decl
;
1168 return sym
->backend_decl
;
1173 if (sym
->attr
.flavor
== FL_PROCEDURE
)
1175 /* Catch function declarations. Only used for actual parameters,
1176 procedure pointers and procptr initialization targets. */
1177 if (sym
->attr
.external
|| sym
->attr
.use_assoc
|| sym
->attr
.intrinsic
)
1179 decl
= gfc_get_extern_function_decl (sym
);
1180 gfc_set_decl_location (decl
, &sym
->declared_at
);
1184 if (!sym
->backend_decl
)
1185 build_function_decl (sym
, false);
1186 decl
= sym
->backend_decl
;
1191 if (sym
->attr
.intrinsic
)
1192 internal_error ("intrinsic variable which isn't a procedure");
1194 /* Create string length decl first so that they can be used in the
1195 type declaration. */
1196 if (sym
->ts
.type
== BT_CHARACTER
)
1197 length
= gfc_create_string_length (sym
);
1199 /* Create the decl for the variable. */
1200 decl
= build_decl (sym
->declared_at
.lb
->location
,
1201 VAR_DECL
, gfc_sym_identifier (sym
), gfc_sym_type (sym
));
1203 /* Add attributes to variables. Functions are handled elsewhere. */
1204 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
1205 decl_attributes (&decl
, attributes
, 0);
1207 /* Symbols from modules should have their assembler names mangled.
1208 This is done here rather than in gfc_finish_var_decl because it
1209 is different for string length variables. */
1212 gfc_set_decl_assembler_name (decl
, gfc_sym_mangled_identifier (sym
));
1213 if (sym
->attr
.use_assoc
&& !intrinsic_array_parameter
)
1214 DECL_IGNORED_P (decl
) = 1;
1217 if (sym
->attr
.dimension
)
1219 /* Create variables to hold the non-constant bits of array info. */
1220 gfc_build_qualified_array (decl
, sym
);
1222 if (sym
->attr
.contiguous
1223 || ((sym
->attr
.allocatable
|| !sym
->attr
.dummy
) && !sym
->attr
.pointer
))
1224 GFC_DECL_PACKED_ARRAY (decl
) = 1;
1227 /* Remember this variable for allocation/cleanup. */
1228 if (sym
->attr
.dimension
|| sym
->attr
.allocatable
1229 || (sym
->ts
.type
== BT_CLASS
&&
1230 (CLASS_DATA (sym
)->attr
.dimension
1231 || CLASS_DATA (sym
)->attr
.allocatable
))
1232 || (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.alloc_comp
)
1233 /* This applies a derived type default initializer. */
1234 || (sym
->ts
.type
== BT_DERIVED
1235 && sym
->attr
.save
== SAVE_NONE
1237 && !sym
->attr
.allocatable
1238 && (sym
->value
&& !sym
->ns
->proc_name
->attr
.is_main_program
)
1239 && !(sym
->attr
.use_assoc
&& !intrinsic_array_parameter
)))
1240 gfc_defer_symbol_init (sym
);
1242 gfc_finish_var_decl (decl
, sym
);
1244 if (sym
->ts
.type
== BT_CHARACTER
)
1246 /* Character variables need special handling. */
1247 gfc_allocate_lang_decl (decl
);
1249 if (TREE_CODE (length
) != INTEGER_CST
)
1251 char name
[GFC_MAX_MANGLED_SYMBOL_LEN
+ 2];
1255 /* Also prefix the mangled name for symbols from modules. */
1256 strcpy (&name
[1], sym
->name
);
1259 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (length
)));
1260 gfc_set_decl_assembler_name (decl
, get_identifier (name
));
1262 gfc_finish_var_decl (length
, sym
);
1263 gcc_assert (!sym
->value
);
1266 else if (sym
->attr
.subref_array_pointer
)
1268 /* We need the span for these beasts. */
1269 gfc_allocate_lang_decl (decl
);
1272 if (sym
->attr
.subref_array_pointer
)
1275 GFC_DECL_SUBREF_ARRAY_P (decl
) = 1;
1276 span
= build_decl (input_location
,
1277 VAR_DECL
, create_tmp_var_name ("span"),
1278 gfc_array_index_type
);
1279 gfc_finish_var_decl (span
, sym
);
1280 TREE_STATIC (span
) = TREE_STATIC (decl
);
1281 DECL_ARTIFICIAL (span
) = 1;
1282 DECL_INITIAL (span
) = build_int_cst (gfc_array_index_type
, 0);
1284 GFC_DECL_SPAN (decl
) = span
;
1285 GFC_TYPE_ARRAY_SPAN (TREE_TYPE (decl
)) = span
;
1288 sym
->backend_decl
= decl
;
1290 if (sym
->attr
.assign
)
1291 gfc_add_assign_aux_vars (sym
);
1293 if (intrinsic_array_parameter
)
1295 TREE_STATIC (decl
) = 1;
1296 DECL_EXTERNAL (decl
) = 0;
1299 if (TREE_STATIC (decl
)
1300 && !(sym
->attr
.use_assoc
&& !intrinsic_array_parameter
)
1301 && (sym
->attr
.save
|| sym
->ns
->proc_name
->attr
.is_main_program
1302 || gfc_option
.flag_max_stack_var_size
== 0
1303 || sym
->attr
.data
|| sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
))
1305 /* Add static initializer. For procedures, it is only needed if
1306 SAVE is specified otherwise they need to be reinitialized
1307 every time the procedure is entered. The TREE_STATIC is
1308 in this case due to -fmax-stack-var-size=. */
1309 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
1311 sym
->attr
.dimension
,
1313 || sym
->attr
.allocatable
,
1314 sym
->attr
.proc_pointer
);
1317 if (!TREE_STATIC (decl
)
1318 && POINTER_TYPE_P (TREE_TYPE (decl
))
1319 && !sym
->attr
.pointer
1320 && !sym
->attr
.allocatable
1321 && !sym
->attr
.proc_pointer
)
1322 DECL_BY_REFERENCE (decl
) = 1;
1328 /* Substitute a temporary variable in place of the real one. */
1331 gfc_shadow_sym (gfc_symbol
* sym
, tree decl
, gfc_saved_var
* save
)
1333 save
->attr
= sym
->attr
;
1334 save
->decl
= sym
->backend_decl
;
1336 gfc_clear_attr (&sym
->attr
);
1337 sym
->attr
.referenced
= 1;
1338 sym
->attr
.flavor
= FL_VARIABLE
;
1340 sym
->backend_decl
= decl
;
1344 /* Restore the original variable. */
1347 gfc_restore_sym (gfc_symbol
* sym
, gfc_saved_var
* save
)
1349 sym
->attr
= save
->attr
;
1350 sym
->backend_decl
= save
->decl
;
1354 /* Declare a procedure pointer. */
1357 get_proc_pointer_decl (gfc_symbol
*sym
)
1362 decl
= sym
->backend_decl
;
1366 decl
= build_decl (input_location
,
1367 VAR_DECL
, get_identifier (sym
->name
),
1368 build_pointer_type (gfc_get_function_type (sym
)));
1370 if ((sym
->ns
->proc_name
1371 && sym
->ns
->proc_name
->backend_decl
== current_function_decl
)
1372 || sym
->attr
.contained
)
1373 gfc_add_decl_to_function (decl
);
1374 else if (sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
1375 gfc_add_decl_to_parent_function (decl
);
1377 sym
->backend_decl
= decl
;
1379 /* If a variable is USE associated, it's always external. */
1380 if (sym
->attr
.use_assoc
)
1382 DECL_EXTERNAL (decl
) = 1;
1383 TREE_PUBLIC (decl
) = 1;
1385 else if (sym
->module
&& sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
1387 /* This is the declaration of a module variable. */
1388 TREE_PUBLIC (decl
) = 1;
1389 TREE_STATIC (decl
) = 1;
1392 if (!sym
->attr
.use_assoc
1393 && (sym
->attr
.save
!= SAVE_NONE
|| sym
->attr
.data
1394 || (sym
->value
&& sym
->ns
->proc_name
->attr
.is_main_program
)))
1395 TREE_STATIC (decl
) = 1;
1397 if (TREE_STATIC (decl
) && sym
->value
)
1399 /* Add static initializer. */
1400 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
1402 sym
->attr
.dimension
,
1406 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
1407 decl_attributes (&decl
, attributes
, 0);
1413 /* Get a basic decl for an external function. */
1416 gfc_get_extern_function_decl (gfc_symbol
* sym
)
1422 gfc_intrinsic_sym
*isym
;
1424 char s
[GFC_MAX_SYMBOL_LEN
+ 23]; /* "_gfortran_f2c_specific" and '\0'. */
1429 if (sym
->backend_decl
)
1430 return sym
->backend_decl
;
1432 /* We should never be creating external decls for alternate entry points.
1433 The procedure may be an alternate entry point, but we don't want/need
1435 gcc_assert (!(sym
->attr
.entry
|| sym
->attr
.entry_master
));
1437 if (sym
->attr
.proc_pointer
)
1438 return get_proc_pointer_decl (sym
);
1440 /* See if this is an external procedure from the same file. If so,
1441 return the backend_decl. */
1442 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->name
);
1444 if (gfc_option
.flag_whole_file
1445 && (!sym
->attr
.use_assoc
|| sym
->attr
.if_source
!= IFSRC_DECL
)
1446 && !sym
->backend_decl
1448 && ((gsym
->type
== GSYM_SUBROUTINE
) || (gsym
->type
== GSYM_FUNCTION
))
1449 && (gsym
->ns
->proc_name
->backend_decl
|| !sym
->attr
.intrinsic
))
1451 if (!gsym
->ns
->proc_name
->backend_decl
)
1453 /* By construction, the external function cannot be
1454 a contained procedure. */
1456 tree save_fn_decl
= current_function_decl
;
1458 current_function_decl
= NULL_TREE
;
1459 gfc_save_backend_locus (&old_loc
);
1462 gfc_create_function_decl (gsym
->ns
, true);
1465 gfc_restore_backend_locus (&old_loc
);
1466 current_function_decl
= save_fn_decl
;
1469 /* If the namespace has entries, the proc_name is the
1470 entry master. Find the entry and use its backend_decl.
1471 otherwise, use the proc_name backend_decl. */
1472 if (gsym
->ns
->entries
)
1474 gfc_entry_list
*entry
= gsym
->ns
->entries
;
1476 for (; entry
; entry
= entry
->next
)
1478 if (strcmp (gsym
->name
, entry
->sym
->name
) == 0)
1480 sym
->backend_decl
= entry
->sym
->backend_decl
;
1486 sym
->backend_decl
= gsym
->ns
->proc_name
->backend_decl
;
1488 if (sym
->backend_decl
)
1490 /* Avoid problems of double deallocation of the backend declaration
1491 later in gfc_trans_use_stmts; cf. PR 45087. */
1492 if (sym
->attr
.if_source
!= IFSRC_DECL
&& sym
->attr
.use_assoc
)
1493 sym
->attr
.use_assoc
= 0;
1495 return sym
->backend_decl
;
1499 /* See if this is a module procedure from the same file. If so,
1500 return the backend_decl. */
1502 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->module
);
1504 if (gfc_option
.flag_whole_file
1506 && gsym
->type
== GSYM_MODULE
)
1511 gfc_find_symbol (sym
->name
, gsym
->ns
, 0, &s
);
1512 if (s
&& s
->backend_decl
)
1514 sym
->backend_decl
= s
->backend_decl
;
1515 return sym
->backend_decl
;
1519 if (sym
->attr
.intrinsic
)
1521 /* Call the resolution function to get the actual name. This is
1522 a nasty hack which relies on the resolution functions only looking
1523 at the first argument. We pass NULL for the second argument
1524 otherwise things like AINT get confused. */
1525 isym
= gfc_find_function (sym
->name
);
1526 gcc_assert (isym
->resolve
.f0
!= NULL
);
1528 memset (&e
, 0, sizeof (e
));
1529 e
.expr_type
= EXPR_FUNCTION
;
1531 memset (&argexpr
, 0, sizeof (argexpr
));
1532 gcc_assert (isym
->formal
);
1533 argexpr
.ts
= isym
->formal
->ts
;
1535 if (isym
->formal
->next
== NULL
)
1536 isym
->resolve
.f1 (&e
, &argexpr
);
1539 if (isym
->formal
->next
->next
== NULL
)
1540 isym
->resolve
.f2 (&e
, &argexpr
, NULL
);
1543 if (isym
->formal
->next
->next
->next
== NULL
)
1544 isym
->resolve
.f3 (&e
, &argexpr
, NULL
, NULL
);
1547 /* All specific intrinsics take less than 5 arguments. */
1548 gcc_assert (isym
->formal
->next
->next
->next
->next
== NULL
);
1549 isym
->resolve
.f4 (&e
, &argexpr
, NULL
, NULL
, NULL
);
1554 if (gfc_option
.flag_f2c
1555 && ((e
.ts
.type
== BT_REAL
&& e
.ts
.kind
== gfc_default_real_kind
)
1556 || e
.ts
.type
== BT_COMPLEX
))
1558 /* Specific which needs a different implementation if f2c
1559 calling conventions are used. */
1560 sprintf (s
, "_gfortran_f2c_specific%s", e
.value
.function
.name
);
1563 sprintf (s
, "_gfortran_specific%s", e
.value
.function
.name
);
1565 name
= get_identifier (s
);
1566 mangled_name
= name
;
1570 name
= gfc_sym_identifier (sym
);
1571 mangled_name
= gfc_sym_mangled_function_id (sym
);
1574 type
= gfc_get_function_type (sym
);
1575 fndecl
= build_decl (input_location
,
1576 FUNCTION_DECL
, name
, type
);
1578 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
1579 decl_attributes (&fndecl
, attributes
, 0);
1581 gfc_set_decl_assembler_name (fndecl
, mangled_name
);
1583 /* Set the context of this decl. */
1584 if (0 && sym
->ns
&& sym
->ns
->proc_name
)
1586 /* TODO: Add external decls to the appropriate scope. */
1587 DECL_CONTEXT (fndecl
) = sym
->ns
->proc_name
->backend_decl
;
1591 /* Global declaration, e.g. intrinsic subroutine. */
1592 DECL_CONTEXT (fndecl
) = NULL_TREE
;
1595 DECL_EXTERNAL (fndecl
) = 1;
1597 /* This specifies if a function is globally addressable, i.e. it is
1598 the opposite of declaring static in C. */
1599 TREE_PUBLIC (fndecl
) = 1;
1601 /* Set attributes for PURE functions. A call to PURE function in the
1602 Fortran 95 sense is both pure and without side effects in the C
1604 if (sym
->attr
.pure
|| sym
->attr
.elemental
)
1606 if (sym
->attr
.function
&& !gfc_return_by_reference (sym
))
1607 DECL_PURE_P (fndecl
) = 1;
1608 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
1609 parameters and don't use alternate returns (is this
1610 allowed?). In that case, calls to them are meaningless, and
1611 can be optimized away. See also in build_function_decl(). */
1612 TREE_SIDE_EFFECTS (fndecl
) = 0;
1615 /* Mark non-returning functions. */
1616 if (sym
->attr
.noreturn
)
1617 TREE_THIS_VOLATILE(fndecl
) = 1;
1619 sym
->backend_decl
= fndecl
;
1621 if (DECL_CONTEXT (fndecl
) == NULL_TREE
)
1622 pushdecl_top_level (fndecl
);
1628 /* Create a declaration for a procedure. For external functions (in the C
1629 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
1630 a master function with alternate entry points. */
1633 build_function_decl (gfc_symbol
* sym
, bool global
)
1635 tree fndecl
, type
, attributes
;
1636 symbol_attribute attr
;
1638 gfc_formal_arglist
*f
;
1640 gcc_assert (!sym
->attr
.external
);
1642 if (sym
->backend_decl
)
1645 /* Set the line and filename. sym->declared_at seems to point to the
1646 last statement for subroutines, but it'll do for now. */
1647 gfc_set_backend_locus (&sym
->declared_at
);
1649 /* Allow only one nesting level. Allow public declarations. */
1650 gcc_assert (current_function_decl
== NULL_TREE
1651 || DECL_FILE_SCOPE_P (current_function_decl
)
1652 || (TREE_CODE (DECL_CONTEXT (current_function_decl
))
1653 == NAMESPACE_DECL
));
1655 type
= gfc_get_function_type (sym
);
1656 fndecl
= build_decl (input_location
,
1657 FUNCTION_DECL
, gfc_sym_identifier (sym
), type
);
1661 attributes
= add_attributes_to_decl (attr
, NULL_TREE
);
1662 decl_attributes (&fndecl
, attributes
, 0);
1664 /* Figure out the return type of the declared function, and build a
1665 RESULT_DECL for it. If this is a subroutine with alternate
1666 returns, build a RESULT_DECL for it. */
1667 result_decl
= NULL_TREE
;
1668 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
1671 if (gfc_return_by_reference (sym
))
1672 type
= void_type_node
;
1675 if (sym
->result
!= sym
)
1676 result_decl
= gfc_sym_identifier (sym
->result
);
1678 type
= TREE_TYPE (TREE_TYPE (fndecl
));
1683 /* Look for alternate return placeholders. */
1684 int has_alternate_returns
= 0;
1685 for (f
= sym
->formal
; f
; f
= f
->next
)
1689 has_alternate_returns
= 1;
1694 if (has_alternate_returns
)
1695 type
= integer_type_node
;
1697 type
= void_type_node
;
1700 result_decl
= build_decl (input_location
,
1701 RESULT_DECL
, result_decl
, type
);
1702 DECL_ARTIFICIAL (result_decl
) = 1;
1703 DECL_IGNORED_P (result_decl
) = 1;
1704 DECL_CONTEXT (result_decl
) = fndecl
;
1705 DECL_RESULT (fndecl
) = result_decl
;
1707 /* Don't call layout_decl for a RESULT_DECL.
1708 layout_decl (result_decl, 0); */
1710 /* Set up all attributes for the function. */
1711 DECL_EXTERNAL (fndecl
) = 0;
1713 /* This specifies if a function is globally visible, i.e. it is
1714 the opposite of declaring static in C. */
1715 if (!current_function_decl
1716 && !sym
->attr
.entry_master
&& !sym
->attr
.is_main_program
)
1717 TREE_PUBLIC (fndecl
) = 1;
1719 /* TREE_STATIC means the function body is defined here. */
1720 TREE_STATIC (fndecl
) = 1;
1722 /* Set attributes for PURE functions. A call to a PURE function in the
1723 Fortran 95 sense is both pure and without side effects in the C
1725 if (attr
.pure
|| attr
.elemental
)
1727 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
1728 including an alternate return. In that case it can also be
1729 marked as PURE. See also in gfc_get_extern_function_decl(). */
1730 if (attr
.function
&& !gfc_return_by_reference (sym
))
1731 DECL_PURE_P (fndecl
) = 1;
1732 TREE_SIDE_EFFECTS (fndecl
) = 0;
1736 /* Layout the function declaration and put it in the binding level
1737 of the current function. */
1740 pushdecl_top_level (fndecl
);
1744 /* Perform name mangling if this is a top level or module procedure. */
1745 if (current_function_decl
== NULL_TREE
)
1746 gfc_set_decl_assembler_name (fndecl
, gfc_sym_mangled_function_id (sym
));
1748 sym
->backend_decl
= fndecl
;
1752 /* Create the DECL_ARGUMENTS for a procedure. */
1755 create_function_arglist (gfc_symbol
* sym
)
1758 gfc_formal_arglist
*f
;
1759 tree typelist
, hidden_typelist
;
1760 tree arglist
, hidden_arglist
;
1764 fndecl
= sym
->backend_decl
;
1766 /* Build formal argument list. Make sure that their TREE_CONTEXT is
1767 the new FUNCTION_DECL node. */
1768 arglist
= NULL_TREE
;
1769 hidden_arglist
= NULL_TREE
;
1770 typelist
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
1772 if (sym
->attr
.entry_master
)
1774 type
= TREE_VALUE (typelist
);
1775 parm
= build_decl (input_location
,
1776 PARM_DECL
, get_identifier ("__entry"), type
);
1778 DECL_CONTEXT (parm
) = fndecl
;
1779 DECL_ARG_TYPE (parm
) = type
;
1780 TREE_READONLY (parm
) = 1;
1781 gfc_finish_decl (parm
);
1782 DECL_ARTIFICIAL (parm
) = 1;
1784 arglist
= chainon (arglist
, parm
);
1785 typelist
= TREE_CHAIN (typelist
);
1788 if (gfc_return_by_reference (sym
))
1790 tree type
= TREE_VALUE (typelist
), length
= NULL
;
1792 if (sym
->ts
.type
== BT_CHARACTER
)
1794 /* Length of character result. */
1795 tree len_type
= TREE_VALUE (TREE_CHAIN (typelist
));
1796 gcc_assert (len_type
== gfc_charlen_type_node
);
1798 length
= build_decl (input_location
,
1800 get_identifier (".__result"),
1802 if (!sym
->ts
.u
.cl
->length
)
1804 sym
->ts
.u
.cl
->backend_decl
= length
;
1805 TREE_USED (length
) = 1;
1807 gcc_assert (TREE_CODE (length
) == PARM_DECL
);
1808 DECL_CONTEXT (length
) = fndecl
;
1809 DECL_ARG_TYPE (length
) = len_type
;
1810 TREE_READONLY (length
) = 1;
1811 DECL_ARTIFICIAL (length
) = 1;
1812 gfc_finish_decl (length
);
1813 if (sym
->ts
.u
.cl
->backend_decl
== NULL
1814 || sym
->ts
.u
.cl
->backend_decl
== length
)
1819 if (sym
->ts
.u
.cl
->backend_decl
== NULL
)
1821 tree len
= build_decl (input_location
,
1823 get_identifier ("..__result"),
1824 gfc_charlen_type_node
);
1825 DECL_ARTIFICIAL (len
) = 1;
1826 TREE_USED (len
) = 1;
1827 sym
->ts
.u
.cl
->backend_decl
= len
;
1830 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1831 arg
= sym
->result
? sym
->result
: sym
;
1832 backend_decl
= arg
->backend_decl
;
1833 /* Temporary clear it, so that gfc_sym_type creates complete
1835 arg
->backend_decl
= NULL
;
1836 type
= gfc_sym_type (arg
);
1837 arg
->backend_decl
= backend_decl
;
1838 type
= build_reference_type (type
);
1842 parm
= build_decl (input_location
,
1843 PARM_DECL
, get_identifier ("__result"), type
);
1845 DECL_CONTEXT (parm
) = fndecl
;
1846 DECL_ARG_TYPE (parm
) = TREE_VALUE (typelist
);
1847 TREE_READONLY (parm
) = 1;
1848 DECL_ARTIFICIAL (parm
) = 1;
1849 gfc_finish_decl (parm
);
1851 arglist
= chainon (arglist
, parm
);
1852 typelist
= TREE_CHAIN (typelist
);
1854 if (sym
->ts
.type
== BT_CHARACTER
)
1856 gfc_allocate_lang_decl (parm
);
1857 arglist
= chainon (arglist
, length
);
1858 typelist
= TREE_CHAIN (typelist
);
1862 hidden_typelist
= typelist
;
1863 for (f
= sym
->formal
; f
; f
= f
->next
)
1864 if (f
->sym
!= NULL
) /* Ignore alternate returns. */
1865 hidden_typelist
= TREE_CHAIN (hidden_typelist
);
1867 for (f
= sym
->formal
; f
; f
= f
->next
)
1869 char name
[GFC_MAX_SYMBOL_LEN
+ 2];
1871 /* Ignore alternate returns. */
1875 type
= TREE_VALUE (typelist
);
1877 if (f
->sym
->ts
.type
== BT_CHARACTER
1878 && (!sym
->attr
.is_bind_c
|| sym
->attr
.entry_master
))
1880 tree len_type
= TREE_VALUE (hidden_typelist
);
1881 tree length
= NULL_TREE
;
1882 gcc_assert (len_type
== gfc_charlen_type_node
);
1884 strcpy (&name
[1], f
->sym
->name
);
1886 length
= build_decl (input_location
,
1887 PARM_DECL
, get_identifier (name
), len_type
);
1889 hidden_arglist
= chainon (hidden_arglist
, length
);
1890 DECL_CONTEXT (length
) = fndecl
;
1891 DECL_ARTIFICIAL (length
) = 1;
1892 DECL_ARG_TYPE (length
) = len_type
;
1893 TREE_READONLY (length
) = 1;
1894 gfc_finish_decl (length
);
1896 /* Remember the passed value. */
1897 if (f
->sym
->ts
.u
.cl
->passed_length
!= NULL
)
1899 /* This can happen if the same type is used for multiple
1900 arguments. We need to copy cl as otherwise
1901 cl->passed_length gets overwritten. */
1902 f
->sym
->ts
.u
.cl
= gfc_new_charlen (f
->sym
->ns
, f
->sym
->ts
.u
.cl
);
1904 f
->sym
->ts
.u
.cl
->passed_length
= length
;
1906 /* Use the passed value for assumed length variables. */
1907 if (!f
->sym
->ts
.u
.cl
->length
)
1909 TREE_USED (length
) = 1;
1910 gcc_assert (!f
->sym
->ts
.u
.cl
->backend_decl
);
1911 f
->sym
->ts
.u
.cl
->backend_decl
= length
;
1914 hidden_typelist
= TREE_CHAIN (hidden_typelist
);
1916 if (f
->sym
->ts
.u
.cl
->backend_decl
== NULL
1917 || f
->sym
->ts
.u
.cl
->backend_decl
== length
)
1919 if (f
->sym
->ts
.u
.cl
->backend_decl
== NULL
)
1920 gfc_create_string_length (f
->sym
);
1922 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1923 if (f
->sym
->attr
.flavor
== FL_PROCEDURE
)
1924 type
= build_pointer_type (gfc_get_function_type (f
->sym
));
1926 type
= gfc_sym_type (f
->sym
);
1930 /* For non-constant length array arguments, make sure they use
1931 a different type node from TYPE_ARG_TYPES type. */
1932 if (f
->sym
->attr
.dimension
1933 && type
== TREE_VALUE (typelist
)
1934 && TREE_CODE (type
) == POINTER_TYPE
1935 && GFC_ARRAY_TYPE_P (type
)
1936 && f
->sym
->as
->type
!= AS_ASSUMED_SIZE
1937 && ! COMPLETE_TYPE_P (TREE_TYPE (type
)))
1939 if (f
->sym
->attr
.flavor
== FL_PROCEDURE
)
1940 type
= build_pointer_type (gfc_get_function_type (f
->sym
));
1942 type
= gfc_sym_type (f
->sym
);
1945 if (f
->sym
->attr
.proc_pointer
)
1946 type
= build_pointer_type (type
);
1948 if (f
->sym
->attr
.volatile_
)
1949 type
= build_qualified_type (type
, TYPE_QUAL_VOLATILE
);
1951 /* Build the argument declaration. */
1952 parm
= build_decl (input_location
,
1953 PARM_DECL
, gfc_sym_identifier (f
->sym
), type
);
1955 if (f
->sym
->attr
.volatile_
)
1957 TREE_THIS_VOLATILE (parm
) = 1;
1958 TREE_SIDE_EFFECTS (parm
) = 1;
1961 /* Fill in arg stuff. */
1962 DECL_CONTEXT (parm
) = fndecl
;
1963 DECL_ARG_TYPE (parm
) = TREE_VALUE (typelist
);
1964 /* All implementation args are read-only. */
1965 TREE_READONLY (parm
) = 1;
1966 if (POINTER_TYPE_P (type
)
1967 && (!f
->sym
->attr
.proc_pointer
1968 && f
->sym
->attr
.flavor
!= FL_PROCEDURE
))
1969 DECL_BY_REFERENCE (parm
) = 1;
1971 gfc_finish_decl (parm
);
1973 f
->sym
->backend_decl
= parm
;
1975 arglist
= chainon (arglist
, parm
);
1976 typelist
= TREE_CHAIN (typelist
);
1979 /* Add the hidden string length parameters, unless the procedure
1981 if (!sym
->attr
.is_bind_c
)
1982 arglist
= chainon (arglist
, hidden_arglist
);
1984 gcc_assert (hidden_typelist
== NULL_TREE
1985 || TREE_VALUE (hidden_typelist
) == void_type_node
);
1986 DECL_ARGUMENTS (fndecl
) = arglist
;
1989 /* Do the setup necessary before generating the body of a function. */
1992 trans_function_start (gfc_symbol
* sym
)
1996 fndecl
= sym
->backend_decl
;
1998 /* Let GCC know the current scope is this function. */
1999 current_function_decl
= fndecl
;
2001 /* Let the world know what we're about to do. */
2002 announce_function (fndecl
);
2004 if (DECL_FILE_SCOPE_P (fndecl
))
2006 /* Create RTL for function declaration. */
2007 rest_of_decl_compilation (fndecl
, 1, 0);
2010 /* Create RTL for function definition. */
2011 make_decl_rtl (fndecl
);
2013 init_function_start (fndecl
);
2015 /* Even though we're inside a function body, we still don't want to
2016 call expand_expr to calculate the size of a variable-sized array.
2017 We haven't necessarily assigned RTL to all variables yet, so it's
2018 not safe to try to expand expressions involving them. */
2019 cfun
->dont_save_pending_sizes_p
= 1;
2021 /* function.c requires a push at the start of the function. */
2025 /* Create thunks for alternate entry points. */
2028 build_entry_thunks (gfc_namespace
* ns
, bool global
)
2030 gfc_formal_arglist
*formal
;
2031 gfc_formal_arglist
*thunk_formal
;
2033 gfc_symbol
*thunk_sym
;
2039 /* This should always be a toplevel function. */
2040 gcc_assert (current_function_decl
== NULL_TREE
);
2042 gfc_save_backend_locus (&old_loc
);
2043 for (el
= ns
->entries
; el
; el
= el
->next
)
2045 VEC(tree
,gc
) *args
= NULL
;
2046 VEC(tree
,gc
) *string_args
= NULL
;
2048 thunk_sym
= el
->sym
;
2050 build_function_decl (thunk_sym
, global
);
2051 create_function_arglist (thunk_sym
);
2053 trans_function_start (thunk_sym
);
2055 thunk_fndecl
= thunk_sym
->backend_decl
;
2057 gfc_init_block (&body
);
2059 /* Pass extra parameter identifying this entry point. */
2060 tmp
= build_int_cst (gfc_array_index_type
, el
->id
);
2061 VEC_safe_push (tree
, gc
, args
, tmp
);
2063 if (thunk_sym
->attr
.function
)
2065 if (gfc_return_by_reference (ns
->proc_name
))
2067 tree ref
= DECL_ARGUMENTS (current_function_decl
);
2068 VEC_safe_push (tree
, gc
, args
, ref
);
2069 if (ns
->proc_name
->ts
.type
== BT_CHARACTER
)
2070 VEC_safe_push (tree
, gc
, args
, DECL_CHAIN (ref
));
2074 for (formal
= ns
->proc_name
->formal
; formal
; formal
= formal
->next
)
2076 /* Ignore alternate returns. */
2077 if (formal
->sym
== NULL
)
2080 /* We don't have a clever way of identifying arguments, so resort to
2081 a brute-force search. */
2082 for (thunk_formal
= thunk_sym
->formal
;
2084 thunk_formal
= thunk_formal
->next
)
2086 if (thunk_formal
->sym
== formal
->sym
)
2092 /* Pass the argument. */
2093 DECL_ARTIFICIAL (thunk_formal
->sym
->backend_decl
) = 1;
2094 VEC_safe_push (tree
, gc
, args
, thunk_formal
->sym
->backend_decl
);
2095 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
2097 tmp
= thunk_formal
->sym
->ts
.u
.cl
->backend_decl
;
2098 VEC_safe_push (tree
, gc
, string_args
, tmp
);
2103 /* Pass NULL for a missing argument. */
2104 VEC_safe_push (tree
, gc
, args
, null_pointer_node
);
2105 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
2107 tmp
= build_int_cst (gfc_charlen_type_node
, 0);
2108 VEC_safe_push (tree
, gc
, string_args
, tmp
);
2113 /* Call the master function. */
2114 VEC_safe_splice (tree
, gc
, args
, string_args
);
2115 tmp
= ns
->proc_name
->backend_decl
;
2116 tmp
= build_call_expr_loc_vec (input_location
, tmp
, args
);
2117 if (ns
->proc_name
->attr
.mixed_entry_master
)
2119 tree union_decl
, field
;
2120 tree master_type
= TREE_TYPE (ns
->proc_name
->backend_decl
);
2122 union_decl
= build_decl (input_location
,
2123 VAR_DECL
, get_identifier ("__result"),
2124 TREE_TYPE (master_type
));
2125 DECL_ARTIFICIAL (union_decl
) = 1;
2126 DECL_EXTERNAL (union_decl
) = 0;
2127 TREE_PUBLIC (union_decl
) = 0;
2128 TREE_USED (union_decl
) = 1;
2129 layout_decl (union_decl
, 0);
2130 pushdecl (union_decl
);
2132 DECL_CONTEXT (union_decl
) = current_function_decl
;
2133 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
2134 TREE_TYPE (union_decl
), union_decl
, tmp
);
2135 gfc_add_expr_to_block (&body
, tmp
);
2137 for (field
= TYPE_FIELDS (TREE_TYPE (union_decl
));
2138 field
; field
= DECL_CHAIN (field
))
2139 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field
)),
2140 thunk_sym
->result
->name
) == 0)
2142 gcc_assert (field
!= NULL_TREE
);
2143 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
,
2144 TREE_TYPE (field
), union_decl
, field
,
2146 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
2147 TREE_TYPE (DECL_RESULT (current_function_decl
)),
2148 DECL_RESULT (current_function_decl
), tmp
);
2149 tmp
= build1_v (RETURN_EXPR
, tmp
);
2151 else if (TREE_TYPE (DECL_RESULT (current_function_decl
))
2154 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
2155 TREE_TYPE (DECL_RESULT (current_function_decl
)),
2156 DECL_RESULT (current_function_decl
), tmp
);
2157 tmp
= build1_v (RETURN_EXPR
, tmp
);
2159 gfc_add_expr_to_block (&body
, tmp
);
2161 /* Finish off this function and send it for code generation. */
2162 DECL_SAVED_TREE (thunk_fndecl
) = gfc_finish_block (&body
);
2165 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl
)) = thunk_fndecl
;
2166 DECL_SAVED_TREE (thunk_fndecl
)
2167 = build3_v (BIND_EXPR
, tmp
, DECL_SAVED_TREE (thunk_fndecl
),
2168 DECL_INITIAL (thunk_fndecl
));
2170 /* Output the GENERIC tree. */
2171 dump_function (TDI_original
, thunk_fndecl
);
2173 /* Store the end of the function, so that we get good line number
2174 info for the epilogue. */
2175 cfun
->function_end_locus
= input_location
;
2177 /* We're leaving the context of this function, so zap cfun.
2178 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
2179 tree_rest_of_compilation. */
2182 current_function_decl
= NULL_TREE
;
2184 cgraph_finalize_function (thunk_fndecl
, true);
2186 /* We share the symbols in the formal argument list with other entry
2187 points and the master function. Clear them so that they are
2188 recreated for each function. */
2189 for (formal
= thunk_sym
->formal
; formal
; formal
= formal
->next
)
2190 if (formal
->sym
!= NULL
) /* Ignore alternate returns. */
2192 formal
->sym
->backend_decl
= NULL_TREE
;
2193 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
2194 formal
->sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
2197 if (thunk_sym
->attr
.function
)
2199 if (thunk_sym
->ts
.type
== BT_CHARACTER
)
2200 thunk_sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
2201 if (thunk_sym
->result
->ts
.type
== BT_CHARACTER
)
2202 thunk_sym
->result
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
2206 gfc_restore_backend_locus (&old_loc
);
2210 /* Create a decl for a function, and create any thunks for alternate entry
2211 points. If global is true, generate the function in the global binding
2212 level, otherwise in the current binding level (which can be global). */
2215 gfc_create_function_decl (gfc_namespace
* ns
, bool global
)
2217 /* Create a declaration for the master function. */
2218 build_function_decl (ns
->proc_name
, global
);
2220 /* Compile the entry thunks. */
2222 build_entry_thunks (ns
, global
);
2224 /* Now create the read argument list. */
2225 create_function_arglist (ns
->proc_name
);
2228 /* Return the decl used to hold the function return value. If
2229 parent_flag is set, the context is the parent_scope. */
2232 gfc_get_fake_result_decl (gfc_symbol
* sym
, int parent_flag
)
2236 tree this_fake_result_decl
;
2237 tree this_function_decl
;
2239 char name
[GFC_MAX_SYMBOL_LEN
+ 10];
2243 this_fake_result_decl
= parent_fake_result_decl
;
2244 this_function_decl
= DECL_CONTEXT (current_function_decl
);
2248 this_fake_result_decl
= current_fake_result_decl
;
2249 this_function_decl
= current_function_decl
;
2253 && sym
->ns
->proc_name
->backend_decl
== this_function_decl
2254 && sym
->ns
->proc_name
->attr
.entry_master
2255 && sym
!= sym
->ns
->proc_name
)
2258 if (this_fake_result_decl
!= NULL
)
2259 for (t
= TREE_CHAIN (this_fake_result_decl
); t
; t
= TREE_CHAIN (t
))
2260 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t
)), sym
->name
) == 0)
2263 return TREE_VALUE (t
);
2264 decl
= gfc_get_fake_result_decl (sym
->ns
->proc_name
, parent_flag
);
2267 this_fake_result_decl
= parent_fake_result_decl
;
2269 this_fake_result_decl
= current_fake_result_decl
;
2271 if (decl
&& sym
->ns
->proc_name
->attr
.mixed_entry_master
)
2275 for (field
= TYPE_FIELDS (TREE_TYPE (decl
));
2276 field
; field
= DECL_CHAIN (field
))
2277 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field
)),
2281 gcc_assert (field
!= NULL_TREE
);
2282 decl
= fold_build3_loc (input_location
, COMPONENT_REF
,
2283 TREE_TYPE (field
), decl
, field
, NULL_TREE
);
2286 var
= create_tmp_var_raw (TREE_TYPE (decl
), sym
->name
);
2288 gfc_add_decl_to_parent_function (var
);
2290 gfc_add_decl_to_function (var
);
2292 SET_DECL_VALUE_EXPR (var
, decl
);
2293 DECL_HAS_VALUE_EXPR_P (var
) = 1;
2294 GFC_DECL_RESULT (var
) = 1;
2296 TREE_CHAIN (this_fake_result_decl
)
2297 = tree_cons (get_identifier (sym
->name
), var
,
2298 TREE_CHAIN (this_fake_result_decl
));
2302 if (this_fake_result_decl
!= NULL_TREE
)
2303 return TREE_VALUE (this_fake_result_decl
);
2305 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
2310 if (sym
->ts
.type
== BT_CHARACTER
)
2312 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
2313 length
= gfc_create_string_length (sym
);
2315 length
= sym
->ts
.u
.cl
->backend_decl
;
2316 if (TREE_CODE (length
) == VAR_DECL
2317 && DECL_CONTEXT (length
) == NULL_TREE
)
2318 gfc_add_decl_to_function (length
);
2321 if (gfc_return_by_reference (sym
))
2323 decl
= DECL_ARGUMENTS (this_function_decl
);
2325 if (sym
->ns
->proc_name
->backend_decl
== this_function_decl
2326 && sym
->ns
->proc_name
->attr
.entry_master
)
2327 decl
= DECL_CHAIN (decl
);
2329 TREE_USED (decl
) = 1;
2331 decl
= gfc_build_dummy_array_decl (sym
, decl
);
2335 sprintf (name
, "__result_%.20s",
2336 IDENTIFIER_POINTER (DECL_NAME (this_function_decl
)));
2338 if (!sym
->attr
.mixed_entry_master
&& sym
->attr
.function
)
2339 decl
= build_decl (DECL_SOURCE_LOCATION (this_function_decl
),
2340 VAR_DECL
, get_identifier (name
),
2341 gfc_sym_type (sym
));
2343 decl
= build_decl (DECL_SOURCE_LOCATION (this_function_decl
),
2344 VAR_DECL
, get_identifier (name
),
2345 TREE_TYPE (TREE_TYPE (this_function_decl
)));
2346 DECL_ARTIFICIAL (decl
) = 1;
2347 DECL_EXTERNAL (decl
) = 0;
2348 TREE_PUBLIC (decl
) = 0;
2349 TREE_USED (decl
) = 1;
2350 GFC_DECL_RESULT (decl
) = 1;
2351 TREE_ADDRESSABLE (decl
) = 1;
2353 layout_decl (decl
, 0);
2356 gfc_add_decl_to_parent_function (decl
);
2358 gfc_add_decl_to_function (decl
);
2362 parent_fake_result_decl
= build_tree_list (NULL
, decl
);
2364 current_fake_result_decl
= build_tree_list (NULL
, decl
);
2370 /* Builds a function decl. The remaining parameters are the types of the
2371 function arguments. Negative nargs indicates a varargs function. */
2374 build_library_function_decl_1 (tree name
, const char *spec
,
2375 tree rettype
, int nargs
, va_list p
)
2383 /* Library functions must be declared with global scope. */
2384 gcc_assert (current_function_decl
== NULL_TREE
);
2386 /* Create a list of the argument types. */
2387 for (arglist
= NULL_TREE
, n
= abs (nargs
); n
> 0; n
--)
2389 argtype
= va_arg (p
, tree
);
2390 arglist
= gfc_chainon_list (arglist
, argtype
);
2395 /* Terminate the list. */
2396 arglist
= chainon (arglist
, void_list_node
);
2399 /* Build the function type and decl. */
2400 fntype
= build_function_type (rettype
, arglist
);
2403 tree attr_args
= build_tree_list (NULL_TREE
,
2404 build_string (strlen (spec
), spec
));
2405 tree attrs
= tree_cons (get_identifier ("fn spec"),
2406 attr_args
, TYPE_ATTRIBUTES (fntype
));
2407 fntype
= build_type_attribute_variant (fntype
, attrs
);
2409 fndecl
= build_decl (input_location
,
2410 FUNCTION_DECL
, name
, fntype
);
2412 /* Mark this decl as external. */
2413 DECL_EXTERNAL (fndecl
) = 1;
2414 TREE_PUBLIC (fndecl
) = 1;
2418 rest_of_decl_compilation (fndecl
, 1, 0);
2423 /* Builds a function decl. The remaining parameters are the types of the
2424 function arguments. Negative nargs indicates a varargs function. */
2427 gfc_build_library_function_decl (tree name
, tree rettype
, int nargs
, ...)
2431 va_start (args
, nargs
);
2432 ret
= build_library_function_decl_1 (name
, NULL
, rettype
, nargs
, args
);
2437 /* Builds a function decl. The remaining parameters are the types of the
2438 function arguments. Negative nargs indicates a varargs function.
2439 The SPEC parameter specifies the function argument and return type
2440 specification according to the fnspec function type attribute. */
2443 gfc_build_library_function_decl_with_spec (tree name
, const char *spec
,
2444 tree rettype
, int nargs
, ...)
2448 va_start (args
, nargs
);
2449 ret
= build_library_function_decl_1 (name
, spec
, rettype
, nargs
, args
);
2455 gfc_build_intrinsic_function_decls (void)
2457 tree gfc_int4_type_node
= gfc_get_int_type (4);
2458 tree gfc_int8_type_node
= gfc_get_int_type (8);
2459 tree gfc_int16_type_node
= gfc_get_int_type (16);
2460 tree gfc_logical4_type_node
= gfc_get_logical_type (4);
2461 tree pchar1_type_node
= gfc_get_pchar_type (1);
2462 tree pchar4_type_node
= gfc_get_pchar_type (4);
2464 /* String functions. */
2465 gfor_fndecl_compare_string
= gfc_build_library_function_decl_with_spec (
2466 get_identifier (PREFIX("compare_string")), "..R.R",
2467 integer_type_node
, 4, gfc_charlen_type_node
, pchar1_type_node
,
2468 gfc_charlen_type_node
, pchar1_type_node
);
2469 DECL_PURE_P (gfor_fndecl_compare_string
) = 1;
2470 TREE_NOTHROW (gfor_fndecl_compare_string
) = 1;
2472 gfor_fndecl_concat_string
= gfc_build_library_function_decl_with_spec (
2473 get_identifier (PREFIX("concat_string")), "..W.R.R",
2474 void_type_node
, 6, gfc_charlen_type_node
, pchar1_type_node
,
2475 gfc_charlen_type_node
, pchar1_type_node
,
2476 gfc_charlen_type_node
, pchar1_type_node
);
2477 TREE_NOTHROW (gfor_fndecl_concat_string
) = 1;
2479 gfor_fndecl_string_len_trim
= gfc_build_library_function_decl_with_spec (
2480 get_identifier (PREFIX("string_len_trim")), "..R",
2481 gfc_charlen_type_node
, 2, gfc_charlen_type_node
, pchar1_type_node
);
2482 DECL_PURE_P (gfor_fndecl_string_len_trim
) = 1;
2483 TREE_NOTHROW (gfor_fndecl_string_len_trim
) = 1;
2485 gfor_fndecl_string_index
= gfc_build_library_function_decl_with_spec (
2486 get_identifier (PREFIX("string_index")), "..R.R.",
2487 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
2488 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
2489 DECL_PURE_P (gfor_fndecl_string_index
) = 1;
2490 TREE_NOTHROW (gfor_fndecl_string_index
) = 1;
2492 gfor_fndecl_string_scan
= gfc_build_library_function_decl_with_spec (
2493 get_identifier (PREFIX("string_scan")), "..R.R.",
2494 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
2495 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
2496 DECL_PURE_P (gfor_fndecl_string_scan
) = 1;
2497 TREE_NOTHROW (gfor_fndecl_string_scan
) = 1;
2499 gfor_fndecl_string_verify
= gfc_build_library_function_decl_with_spec (
2500 get_identifier (PREFIX("string_verify")), "..R.R.",
2501 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
2502 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
2503 DECL_PURE_P (gfor_fndecl_string_verify
) = 1;
2504 TREE_NOTHROW (gfor_fndecl_string_verify
) = 1;
2506 gfor_fndecl_string_trim
= gfc_build_library_function_decl_with_spec (
2507 get_identifier (PREFIX("string_trim")), ".Ww.R",
2508 void_type_node
, 4, build_pointer_type (gfc_charlen_type_node
),
2509 build_pointer_type (pchar1_type_node
), gfc_charlen_type_node
,
2512 gfor_fndecl_string_minmax
= gfc_build_library_function_decl_with_spec (
2513 get_identifier (PREFIX("string_minmax")), ".Ww.R",
2514 void_type_node
, -4, build_pointer_type (gfc_charlen_type_node
),
2515 build_pointer_type (pchar1_type_node
), integer_type_node
,
2518 gfor_fndecl_adjustl
= gfc_build_library_function_decl_with_spec (
2519 get_identifier (PREFIX("adjustl")), ".W.R",
2520 void_type_node
, 3, pchar1_type_node
, gfc_charlen_type_node
,
2522 TREE_NOTHROW (gfor_fndecl_adjustl
) = 1;
2524 gfor_fndecl_adjustr
= gfc_build_library_function_decl_with_spec (
2525 get_identifier (PREFIX("adjustr")), ".W.R",
2526 void_type_node
, 3, pchar1_type_node
, gfc_charlen_type_node
,
2528 TREE_NOTHROW (gfor_fndecl_adjustr
) = 1;
2530 gfor_fndecl_select_string
= gfc_build_library_function_decl_with_spec (
2531 get_identifier (PREFIX("select_string")), ".R.R.",
2532 integer_type_node
, 4, pvoid_type_node
, integer_type_node
,
2533 pchar1_type_node
, gfc_charlen_type_node
);
2534 DECL_PURE_P (gfor_fndecl_select_string
) = 1;
2535 TREE_NOTHROW (gfor_fndecl_select_string
) = 1;
2537 gfor_fndecl_compare_string_char4
= gfc_build_library_function_decl_with_spec (
2538 get_identifier (PREFIX("compare_string_char4")), "..R.R",
2539 integer_type_node
, 4, gfc_charlen_type_node
, pchar4_type_node
,
2540 gfc_charlen_type_node
, pchar4_type_node
);
2541 DECL_PURE_P (gfor_fndecl_compare_string_char4
) = 1;
2542 TREE_NOTHROW (gfor_fndecl_compare_string_char4
) = 1;
2544 gfor_fndecl_concat_string_char4
= gfc_build_library_function_decl_with_spec (
2545 get_identifier (PREFIX("concat_string_char4")), "..W.R.R",
2546 void_type_node
, 6, gfc_charlen_type_node
, pchar4_type_node
,
2547 gfc_charlen_type_node
, pchar4_type_node
, gfc_charlen_type_node
,
2549 TREE_NOTHROW (gfor_fndecl_concat_string_char4
) = 1;
2551 gfor_fndecl_string_len_trim_char4
= gfc_build_library_function_decl_with_spec (
2552 get_identifier (PREFIX("string_len_trim_char4")), "..R",
2553 gfc_charlen_type_node
, 2, gfc_charlen_type_node
, pchar4_type_node
);
2554 DECL_PURE_P (gfor_fndecl_string_len_trim_char4
) = 1;
2555 TREE_NOTHROW (gfor_fndecl_string_len_trim_char4
) = 1;
2557 gfor_fndecl_string_index_char4
= gfc_build_library_function_decl_with_spec (
2558 get_identifier (PREFIX("string_index_char4")), "..R.R.",
2559 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
2560 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
2561 DECL_PURE_P (gfor_fndecl_string_index_char4
) = 1;
2562 TREE_NOTHROW (gfor_fndecl_string_index_char4
) = 1;
2564 gfor_fndecl_string_scan_char4
= gfc_build_library_function_decl_with_spec (
2565 get_identifier (PREFIX("string_scan_char4")), "..R.R.",
2566 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
2567 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
2568 DECL_PURE_P (gfor_fndecl_string_scan_char4
) = 1;
2569 TREE_NOTHROW (gfor_fndecl_string_scan_char4
) = 1;
2571 gfor_fndecl_string_verify_char4
= gfc_build_library_function_decl_with_spec (
2572 get_identifier (PREFIX("string_verify_char4")), "..R.R.",
2573 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
2574 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
2575 DECL_PURE_P (gfor_fndecl_string_verify_char4
) = 1;
2576 TREE_NOTHROW (gfor_fndecl_string_verify_char4
) = 1;
2578 gfor_fndecl_string_trim_char4
= gfc_build_library_function_decl_with_spec (
2579 get_identifier (PREFIX("string_trim_char4")), ".Ww.R",
2580 void_type_node
, 4, build_pointer_type (gfc_charlen_type_node
),
2581 build_pointer_type (pchar4_type_node
), gfc_charlen_type_node
,
2584 gfor_fndecl_string_minmax_char4
= gfc_build_library_function_decl_with_spec (
2585 get_identifier (PREFIX("string_minmax_char4")), ".Ww.R",
2586 void_type_node
, -4, build_pointer_type (gfc_charlen_type_node
),
2587 build_pointer_type (pchar4_type_node
), integer_type_node
,
2590 gfor_fndecl_adjustl_char4
= gfc_build_library_function_decl_with_spec (
2591 get_identifier (PREFIX("adjustl_char4")), ".W.R",
2592 void_type_node
, 3, pchar4_type_node
, gfc_charlen_type_node
,
2594 TREE_NOTHROW (gfor_fndecl_adjustl_char4
) = 1;
2596 gfor_fndecl_adjustr_char4
= gfc_build_library_function_decl_with_spec (
2597 get_identifier (PREFIX("adjustr_char4")), ".W.R",
2598 void_type_node
, 3, pchar4_type_node
, gfc_charlen_type_node
,
2600 TREE_NOTHROW (gfor_fndecl_adjustr_char4
) = 1;
2602 gfor_fndecl_select_string_char4
= gfc_build_library_function_decl_with_spec (
2603 get_identifier (PREFIX("select_string_char4")), ".R.R.",
2604 integer_type_node
, 4, pvoid_type_node
, integer_type_node
,
2605 pvoid_type_node
, gfc_charlen_type_node
);
2606 DECL_PURE_P (gfor_fndecl_select_string_char4
) = 1;
2607 TREE_NOTHROW (gfor_fndecl_select_string_char4
) = 1;
2610 /* Conversion between character kinds. */
2612 gfor_fndecl_convert_char1_to_char4
= gfc_build_library_function_decl_with_spec (
2613 get_identifier (PREFIX("convert_char1_to_char4")), ".w.R",
2614 void_type_node
, 3, build_pointer_type (pchar4_type_node
),
2615 gfc_charlen_type_node
, pchar1_type_node
);
2617 gfor_fndecl_convert_char4_to_char1
= gfc_build_library_function_decl_with_spec (
2618 get_identifier (PREFIX("convert_char4_to_char1")), ".w.R",
2619 void_type_node
, 3, build_pointer_type (pchar1_type_node
),
2620 gfc_charlen_type_node
, pchar4_type_node
);
2622 /* Misc. functions. */
2624 gfor_fndecl_ttynam
= gfc_build_library_function_decl_with_spec (
2625 get_identifier (PREFIX("ttynam")), ".W",
2626 void_type_node
, 3, pchar_type_node
, gfc_charlen_type_node
,
2629 gfor_fndecl_fdate
= gfc_build_library_function_decl_with_spec (
2630 get_identifier (PREFIX("fdate")), ".W",
2631 void_type_node
, 2, pchar_type_node
, gfc_charlen_type_node
);
2633 gfor_fndecl_ctime
= gfc_build_library_function_decl_with_spec (
2634 get_identifier (PREFIX("ctime")), ".W",
2635 void_type_node
, 3, pchar_type_node
, gfc_charlen_type_node
,
2636 gfc_int8_type_node
);
2638 gfor_fndecl_sc_kind
= gfc_build_library_function_decl_with_spec (
2639 get_identifier (PREFIX("selected_char_kind")), "..R",
2640 gfc_int4_type_node
, 2, gfc_charlen_type_node
, pchar_type_node
);
2641 DECL_PURE_P (gfor_fndecl_sc_kind
) = 1;
2642 TREE_NOTHROW (gfor_fndecl_sc_kind
) = 1;
2644 gfor_fndecl_si_kind
= gfc_build_library_function_decl_with_spec (
2645 get_identifier (PREFIX("selected_int_kind")), ".R",
2646 gfc_int4_type_node
, 1, pvoid_type_node
);
2647 DECL_PURE_P (gfor_fndecl_si_kind
) = 1;
2648 TREE_NOTHROW (gfor_fndecl_si_kind
) = 1;
2650 gfor_fndecl_sr_kind
= gfc_build_library_function_decl_with_spec (
2651 get_identifier (PREFIX("selected_real_kind2008")), ".RR",
2652 gfc_int4_type_node
, 3, pvoid_type_node
, pvoid_type_node
,
2654 DECL_PURE_P (gfor_fndecl_sr_kind
) = 1;
2655 TREE_NOTHROW (gfor_fndecl_sr_kind
) = 1;
2657 /* Power functions. */
2659 tree ctype
, rtype
, itype
, jtype
;
2660 int rkind
, ikind
, jkind
;
2663 static int ikinds
[NIKINDS
] = {4, 8, 16};
2664 static int rkinds
[NRKINDS
] = {4, 8, 10, 16};
2665 char name
[PREFIX_LEN
+ 12]; /* _gfortran_pow_?n_?n */
2667 for (ikind
=0; ikind
< NIKINDS
; ikind
++)
2669 itype
= gfc_get_int_type (ikinds
[ikind
]);
2671 for (jkind
=0; jkind
< NIKINDS
; jkind
++)
2673 jtype
= gfc_get_int_type (ikinds
[jkind
]);
2676 sprintf(name
, PREFIX("pow_i%d_i%d"), ikinds
[ikind
],
2678 gfor_fndecl_math_powi
[jkind
][ikind
].integer
=
2679 gfc_build_library_function_decl (get_identifier (name
),
2680 jtype
, 2, jtype
, itype
);
2681 TREE_READONLY (gfor_fndecl_math_powi
[jkind
][ikind
].integer
) = 1;
2682 TREE_NOTHROW (gfor_fndecl_math_powi
[jkind
][ikind
].integer
) = 1;
2686 for (rkind
= 0; rkind
< NRKINDS
; rkind
++)
2688 rtype
= gfc_get_real_type (rkinds
[rkind
]);
2691 sprintf(name
, PREFIX("pow_r%d_i%d"), rkinds
[rkind
],
2693 gfor_fndecl_math_powi
[rkind
][ikind
].real
=
2694 gfc_build_library_function_decl (get_identifier (name
),
2695 rtype
, 2, rtype
, itype
);
2696 TREE_READONLY (gfor_fndecl_math_powi
[rkind
][ikind
].real
) = 1;
2697 TREE_NOTHROW (gfor_fndecl_math_powi
[rkind
][ikind
].real
) = 1;
2700 ctype
= gfc_get_complex_type (rkinds
[rkind
]);
2703 sprintf(name
, PREFIX("pow_c%d_i%d"), rkinds
[rkind
],
2705 gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
=
2706 gfc_build_library_function_decl (get_identifier (name
),
2707 ctype
, 2,ctype
, itype
);
2708 TREE_READONLY (gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
) = 1;
2709 TREE_NOTHROW (gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
) = 1;
2717 gfor_fndecl_math_ishftc4
= gfc_build_library_function_decl (
2718 get_identifier (PREFIX("ishftc4")),
2719 gfc_int4_type_node
, 3, gfc_int4_type_node
, gfc_int4_type_node
,
2720 gfc_int4_type_node
);
2721 TREE_READONLY (gfor_fndecl_math_ishftc4
) = 1;
2722 TREE_NOTHROW (gfor_fndecl_math_ishftc4
) = 1;
2724 gfor_fndecl_math_ishftc8
= gfc_build_library_function_decl (
2725 get_identifier (PREFIX("ishftc8")),
2726 gfc_int8_type_node
, 3, gfc_int8_type_node
, gfc_int4_type_node
,
2727 gfc_int4_type_node
);
2728 TREE_READONLY (gfor_fndecl_math_ishftc8
) = 1;
2729 TREE_NOTHROW (gfor_fndecl_math_ishftc8
) = 1;
2731 if (gfc_int16_type_node
)
2733 gfor_fndecl_math_ishftc16
= gfc_build_library_function_decl (
2734 get_identifier (PREFIX("ishftc16")),
2735 gfc_int16_type_node
, 3, gfc_int16_type_node
, gfc_int4_type_node
,
2736 gfc_int4_type_node
);
2737 TREE_READONLY (gfor_fndecl_math_ishftc16
) = 1;
2738 TREE_NOTHROW (gfor_fndecl_math_ishftc16
) = 1;
2741 /* BLAS functions. */
2743 tree pint
= build_pointer_type (integer_type_node
);
2744 tree ps
= build_pointer_type (gfc_get_real_type (gfc_default_real_kind
));
2745 tree pd
= build_pointer_type (gfc_get_real_type (gfc_default_double_kind
));
2746 tree pc
= build_pointer_type (gfc_get_complex_type (gfc_default_real_kind
));
2747 tree pz
= build_pointer_type
2748 (gfc_get_complex_type (gfc_default_double_kind
));
2750 gfor_fndecl_sgemm
= gfc_build_library_function_decl
2752 (gfc_option
.flag_underscoring
? "sgemm_"
2754 void_type_node
, 15, pchar_type_node
,
2755 pchar_type_node
, pint
, pint
, pint
, ps
, ps
, pint
,
2756 ps
, pint
, ps
, ps
, pint
, integer_type_node
,
2758 gfor_fndecl_dgemm
= gfc_build_library_function_decl
2760 (gfc_option
.flag_underscoring
? "dgemm_"
2762 void_type_node
, 15, pchar_type_node
,
2763 pchar_type_node
, pint
, pint
, pint
, pd
, pd
, pint
,
2764 pd
, pint
, pd
, pd
, pint
, integer_type_node
,
2766 gfor_fndecl_cgemm
= gfc_build_library_function_decl
2768 (gfc_option
.flag_underscoring
? "cgemm_"
2770 void_type_node
, 15, pchar_type_node
,
2771 pchar_type_node
, pint
, pint
, pint
, pc
, pc
, pint
,
2772 pc
, pint
, pc
, pc
, pint
, integer_type_node
,
2774 gfor_fndecl_zgemm
= gfc_build_library_function_decl
2776 (gfc_option
.flag_underscoring
? "zgemm_"
2778 void_type_node
, 15, pchar_type_node
,
2779 pchar_type_node
, pint
, pint
, pint
, pz
, pz
, pint
,
2780 pz
, pint
, pz
, pz
, pint
, integer_type_node
,
2784 /* Other functions. */
2785 gfor_fndecl_size0
= gfc_build_library_function_decl_with_spec (
2786 get_identifier (PREFIX("size0")), ".R",
2787 gfc_array_index_type
, 1, pvoid_type_node
);
2788 DECL_PURE_P (gfor_fndecl_size0
) = 1;
2789 TREE_NOTHROW (gfor_fndecl_size0
) = 1;
2791 gfor_fndecl_size1
= gfc_build_library_function_decl_with_spec (
2792 get_identifier (PREFIX("size1")), ".R",
2793 gfc_array_index_type
, 2, pvoid_type_node
, gfc_array_index_type
);
2794 DECL_PURE_P (gfor_fndecl_size1
) = 1;
2795 TREE_NOTHROW (gfor_fndecl_size1
) = 1;
2797 gfor_fndecl_iargc
= gfc_build_library_function_decl (
2798 get_identifier (PREFIX ("iargc")), gfc_int4_type_node
, 0);
2799 TREE_NOTHROW (gfor_fndecl_iargc
) = 1;
2803 /* Make prototypes for runtime library functions. */
2806 gfc_build_builtin_function_decls (void)
2808 tree gfc_int4_type_node
= gfc_get_int_type (4);
2810 gfor_fndecl_stop_numeric
= gfc_build_library_function_decl (
2811 get_identifier (PREFIX("stop_numeric")),
2812 void_type_node
, 1, gfc_int4_type_node
);
2813 /* STOP doesn't return. */
2814 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric
) = 1;
2816 gfor_fndecl_stop_numeric_f08
= gfc_build_library_function_decl (
2817 get_identifier (PREFIX("stop_numeric_f08")),
2818 void_type_node
, 1, gfc_int4_type_node
);
2819 /* STOP doesn't return. */
2820 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric_f08
) = 1;
2822 gfor_fndecl_stop_string
= gfc_build_library_function_decl_with_spec (
2823 get_identifier (PREFIX("stop_string")), ".R.",
2824 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
2825 /* STOP doesn't return. */
2826 TREE_THIS_VOLATILE (gfor_fndecl_stop_string
) = 1;
2828 gfor_fndecl_error_stop_numeric
= gfc_build_library_function_decl (
2829 get_identifier (PREFIX("error_stop_numeric")),
2830 void_type_node
, 1, gfc_int4_type_node
);
2831 /* ERROR STOP doesn't return. */
2832 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_numeric
) = 1;
2834 gfor_fndecl_error_stop_string
= gfc_build_library_function_decl_with_spec (
2835 get_identifier (PREFIX("error_stop_string")), ".R.",
2836 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
2837 /* ERROR STOP doesn't return. */
2838 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_string
) = 1;
2840 gfor_fndecl_pause_numeric
= gfc_build_library_function_decl (
2841 get_identifier (PREFIX("pause_numeric")),
2842 void_type_node
, 1, gfc_int4_type_node
);
2844 gfor_fndecl_pause_string
= gfc_build_library_function_decl_with_spec (
2845 get_identifier (PREFIX("pause_string")), ".R.",
2846 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
2848 gfor_fndecl_runtime_error
= gfc_build_library_function_decl_with_spec (
2849 get_identifier (PREFIX("runtime_error")), ".R",
2850 void_type_node
, -1, pchar_type_node
);
2851 /* The runtime_error function does not return. */
2852 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error
) = 1;
2854 gfor_fndecl_runtime_error_at
= gfc_build_library_function_decl_with_spec (
2855 get_identifier (PREFIX("runtime_error_at")), ".RR",
2856 void_type_node
, -2, pchar_type_node
, pchar_type_node
);
2857 /* The runtime_error_at function does not return. */
2858 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at
) = 1;
2860 gfor_fndecl_runtime_warning_at
= gfc_build_library_function_decl_with_spec (
2861 get_identifier (PREFIX("runtime_warning_at")), ".RR",
2862 void_type_node
, -2, pchar_type_node
, pchar_type_node
);
2864 gfor_fndecl_generate_error
= gfc_build_library_function_decl_with_spec (
2865 get_identifier (PREFIX("generate_error")), ".R.R",
2866 void_type_node
, 3, pvoid_type_node
, integer_type_node
,
2869 gfor_fndecl_os_error
= gfc_build_library_function_decl_with_spec (
2870 get_identifier (PREFIX("os_error")), ".R",
2871 void_type_node
, 1, pchar_type_node
);
2872 /* The runtime_error function does not return. */
2873 TREE_THIS_VOLATILE (gfor_fndecl_os_error
) = 1;
2875 gfor_fndecl_set_args
= gfc_build_library_function_decl (
2876 get_identifier (PREFIX("set_args")),
2877 void_type_node
, 2, integer_type_node
,
2878 build_pointer_type (pchar_type_node
));
2880 gfor_fndecl_set_fpe
= gfc_build_library_function_decl (
2881 get_identifier (PREFIX("set_fpe")),
2882 void_type_node
, 1, integer_type_node
);
2884 /* Keep the array dimension in sync with the call, later in this file. */
2885 gfor_fndecl_set_options
= gfc_build_library_function_decl_with_spec (
2886 get_identifier (PREFIX("set_options")), "..R",
2887 void_type_node
, 2, integer_type_node
,
2888 build_pointer_type (integer_type_node
));
2890 gfor_fndecl_set_convert
= gfc_build_library_function_decl (
2891 get_identifier (PREFIX("set_convert")),
2892 void_type_node
, 1, integer_type_node
);
2894 gfor_fndecl_set_record_marker
= gfc_build_library_function_decl (
2895 get_identifier (PREFIX("set_record_marker")),
2896 void_type_node
, 1, integer_type_node
);
2898 gfor_fndecl_set_max_subrecord_length
= gfc_build_library_function_decl (
2899 get_identifier (PREFIX("set_max_subrecord_length")),
2900 void_type_node
, 1, integer_type_node
);
2902 gfor_fndecl_in_pack
= gfc_build_library_function_decl_with_spec (
2903 get_identifier (PREFIX("internal_pack")), ".r",
2904 pvoid_type_node
, 1, pvoid_type_node
);
2906 gfor_fndecl_in_unpack
= gfc_build_library_function_decl_with_spec (
2907 get_identifier (PREFIX("internal_unpack")), ".wR",
2908 void_type_node
, 2, pvoid_type_node
, pvoid_type_node
);
2910 gfor_fndecl_associated
= gfc_build_library_function_decl_with_spec (
2911 get_identifier (PREFIX("associated")), ".RR",
2912 integer_type_node
, 2, ppvoid_type_node
, ppvoid_type_node
);
2913 DECL_PURE_P (gfor_fndecl_associated
) = 1;
2914 TREE_NOTHROW (gfor_fndecl_associated
) = 1;
2916 gfc_build_intrinsic_function_decls ();
2917 gfc_build_intrinsic_lib_fndecls ();
2918 gfc_build_io_library_fndecls ();
2922 /* Evaluate the length of dummy character variables. */
2925 gfc_trans_dummy_character (gfc_symbol
*sym
, gfc_charlen
*cl
,
2926 gfc_wrapped_block
*block
)
2930 gfc_finish_decl (cl
->backend_decl
);
2932 gfc_start_block (&init
);
2934 /* Evaluate the string length expression. */
2935 gfc_conv_string_length (cl
, NULL
, &init
);
2937 gfc_trans_vla_type_sizes (sym
, &init
);
2939 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
2943 /* Allocate and cleanup an automatic character variable. */
2946 gfc_trans_auto_character_variable (gfc_symbol
* sym
, gfc_wrapped_block
* block
)
2952 gcc_assert (sym
->backend_decl
);
2953 gcc_assert (sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
);
2955 gfc_start_block (&init
);
2957 /* Evaluate the string length expression. */
2958 gfc_conv_string_length (sym
->ts
.u
.cl
, NULL
, &init
);
2960 gfc_trans_vla_type_sizes (sym
, &init
);
2962 decl
= sym
->backend_decl
;
2964 /* Emit a DECL_EXPR for this variable, which will cause the
2965 gimplifier to allocate storage, and all that good stuff. */
2966 tmp
= fold_build1_loc (input_location
, DECL_EXPR
, TREE_TYPE (decl
), decl
);
2967 gfc_add_expr_to_block (&init
, tmp
);
2969 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
2972 /* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
2975 gfc_trans_assign_aux_var (gfc_symbol
* sym
, gfc_wrapped_block
* block
)
2979 gcc_assert (sym
->backend_decl
);
2980 gfc_start_block (&init
);
2982 /* Set the initial value to length. See the comments in
2983 function gfc_add_assign_aux_vars in this file. */
2984 gfc_add_modify (&init
, GFC_DECL_STRING_LEN (sym
->backend_decl
),
2985 build_int_cst (NULL_TREE
, -2));
2987 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
2991 gfc_trans_vla_one_sizepos (tree
*tp
, stmtblock_t
*body
)
2993 tree t
= *tp
, var
, val
;
2995 if (t
== NULL
|| t
== error_mark_node
)
2997 if (TREE_CONSTANT (t
) || DECL_P (t
))
3000 if (TREE_CODE (t
) == SAVE_EXPR
)
3002 if (SAVE_EXPR_RESOLVED_P (t
))
3004 *tp
= TREE_OPERAND (t
, 0);
3007 val
= TREE_OPERAND (t
, 0);
3012 var
= gfc_create_var_np (TREE_TYPE (t
), NULL
);
3013 gfc_add_decl_to_function (var
);
3014 gfc_add_modify (body
, var
, val
);
3015 if (TREE_CODE (t
) == SAVE_EXPR
)
3016 TREE_OPERAND (t
, 0) = var
;
3021 gfc_trans_vla_type_sizes_1 (tree type
, stmtblock_t
*body
)
3025 if (type
== NULL
|| type
== error_mark_node
)
3028 type
= TYPE_MAIN_VARIANT (type
);
3030 if (TREE_CODE (type
) == INTEGER_TYPE
)
3032 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type
), body
);
3033 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type
), body
);
3035 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
3037 TYPE_MIN_VALUE (t
) = TYPE_MIN_VALUE (type
);
3038 TYPE_MAX_VALUE (t
) = TYPE_MAX_VALUE (type
);
3041 else if (TREE_CODE (type
) == ARRAY_TYPE
)
3043 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type
), body
);
3044 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type
), body
);
3045 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type
), body
);
3046 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type
), body
);
3048 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
3050 TYPE_SIZE (t
) = TYPE_SIZE (type
);
3051 TYPE_SIZE_UNIT (t
) = TYPE_SIZE_UNIT (type
);
3056 /* Make sure all type sizes and array domains are either constant,
3057 or variable or parameter decls. This is a simplified variant
3058 of gimplify_type_sizes, but we can't use it here, as none of the
3059 variables in the expressions have been gimplified yet.
3060 As type sizes and domains for various variable length arrays
3061 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
3062 time, without this routine gimplify_type_sizes in the middle-end
3063 could result in the type sizes being gimplified earlier than where
3064 those variables are initialized. */
3067 gfc_trans_vla_type_sizes (gfc_symbol
*sym
, stmtblock_t
*body
)
3069 tree type
= TREE_TYPE (sym
->backend_decl
);
3071 if (TREE_CODE (type
) == FUNCTION_TYPE
3072 && (sym
->attr
.function
|| sym
->attr
.result
|| sym
->attr
.entry
))
3074 if (! current_fake_result_decl
)
3077 type
= TREE_TYPE (TREE_VALUE (current_fake_result_decl
));
3080 while (POINTER_TYPE_P (type
))
3081 type
= TREE_TYPE (type
);
3083 if (GFC_DESCRIPTOR_TYPE_P (type
))
3085 tree etype
= GFC_TYPE_ARRAY_DATAPTR_TYPE (type
);
3087 while (POINTER_TYPE_P (etype
))
3088 etype
= TREE_TYPE (etype
);
3090 gfc_trans_vla_type_sizes_1 (etype
, body
);
3093 gfc_trans_vla_type_sizes_1 (type
, body
);
3097 /* Initialize a derived type by building an lvalue from the symbol
3098 and using trans_assignment to do the work. Set dealloc to false
3099 if no deallocation prior the assignment is needed. */
3101 gfc_init_default_dt (gfc_symbol
* sym
, stmtblock_t
* block
, bool dealloc
)
3109 gcc_assert (!sym
->attr
.allocatable
);
3110 gfc_set_sym_referenced (sym
);
3111 e
= gfc_lval_expr_from_sym (sym
);
3112 tmp
= gfc_trans_assignment (e
, sym
->value
, false, dealloc
);
3113 if (sym
->attr
.dummy
&& (sym
->attr
.optional
3114 || sym
->ns
->proc_name
->attr
.entry_master
))
3116 present
= gfc_conv_expr_present (sym
);
3117 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
), present
,
3118 tmp
, build_empty_stmt (input_location
));
3120 gfc_add_expr_to_block (block
, tmp
);
3125 /* Initialize INTENT(OUT) derived type dummies. As well as giving
3126 them their default initializer, if they do not have allocatable
3127 components, they have their allocatable components deallocated. */
3130 init_intent_out_dt (gfc_symbol
* proc_sym
, gfc_wrapped_block
* block
)
3133 gfc_formal_arglist
*f
;
3137 gfc_init_block (&init
);
3138 for (f
= proc_sym
->formal
; f
; f
= f
->next
)
3139 if (f
->sym
&& f
->sym
->attr
.intent
== INTENT_OUT
3140 && !f
->sym
->attr
.pointer
3141 && f
->sym
->ts
.type
== BT_DERIVED
)
3143 if (f
->sym
->ts
.u
.derived
->attr
.alloc_comp
&& !f
->sym
->value
)
3145 tmp
= gfc_deallocate_alloc_comp (f
->sym
->ts
.u
.derived
,
3146 f
->sym
->backend_decl
,
3147 f
->sym
->as
? f
->sym
->as
->rank
: 0);
3149 if (f
->sym
->attr
.optional
3150 || f
->sym
->ns
->proc_name
->attr
.entry_master
)
3152 present
= gfc_conv_expr_present (f
->sym
);
3153 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
),
3155 build_empty_stmt (input_location
));
3158 gfc_add_expr_to_block (&init
, tmp
);
3160 else if (f
->sym
->value
)
3161 gfc_init_default_dt (f
->sym
, &init
, true);
3164 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
3168 /* Generate function entry and exit code, and add it to the function body.
3170 Allocation and initialization of array variables.
3171 Allocation of character string variables.
3172 Initialization and possibly repacking of dummy arrays.
3173 Initialization of ASSIGN statement auxiliary variable.
3174 Initialization of ASSOCIATE names.
3175 Automatic deallocation. */
3178 gfc_trans_deferred_vars (gfc_symbol
* proc_sym
, gfc_wrapped_block
* block
)
3182 gfc_formal_arglist
*f
;
3183 stmtblock_t tmpblock
;
3184 bool seen_trans_deferred_array
= false;
3186 /* Deal with implicit return variables. Explicit return variables will
3187 already have been added. */
3188 if (gfc_return_by_reference (proc_sym
) && proc_sym
->result
== proc_sym
)
3190 if (!current_fake_result_decl
)
3192 gfc_entry_list
*el
= NULL
;
3193 if (proc_sym
->attr
.entry_master
)
3195 for (el
= proc_sym
->ns
->entries
; el
; el
= el
->next
)
3196 if (el
->sym
!= el
->sym
->result
)
3199 /* TODO: move to the appropriate place in resolve.c. */
3200 if (warn_return_type
&& el
== NULL
)
3201 gfc_warning ("Return value of function '%s' at %L not set",
3202 proc_sym
->name
, &proc_sym
->declared_at
);
3204 else if (proc_sym
->as
)
3206 tree result
= TREE_VALUE (current_fake_result_decl
);
3207 gfc_trans_dummy_array_bias (proc_sym
, result
, block
);
3209 /* An automatic character length, pointer array result. */
3210 if (proc_sym
->ts
.type
== BT_CHARACTER
3211 && TREE_CODE (proc_sym
->ts
.u
.cl
->backend_decl
) == VAR_DECL
)
3212 gfc_trans_dummy_character (proc_sym
, proc_sym
->ts
.u
.cl
, block
);
3214 else if (proc_sym
->ts
.type
== BT_CHARACTER
)
3216 if (TREE_CODE (proc_sym
->ts
.u
.cl
->backend_decl
) == VAR_DECL
)
3217 gfc_trans_dummy_character (proc_sym
, proc_sym
->ts
.u
.cl
, block
);
3220 gcc_assert (gfc_option
.flag_f2c
3221 && proc_sym
->ts
.type
== BT_COMPLEX
);
3224 /* Initialize the INTENT(OUT) derived type dummy arguments. This
3225 should be done here so that the offsets and lbounds of arrays
3227 init_intent_out_dt (proc_sym
, block
);
3229 for (sym
= proc_sym
->tlink
; sym
!= proc_sym
; sym
= sym
->tlink
)
3231 bool sym_has_alloc_comp
= (sym
->ts
.type
== BT_DERIVED
)
3232 && sym
->ts
.u
.derived
->attr
.alloc_comp
;
3236 if (sym
->attr
.dimension
)
3238 switch (sym
->as
->type
)
3241 if (sym
->attr
.dummy
|| sym
->attr
.result
)
3242 gfc_trans_dummy_array_bias (sym
, sym
->backend_decl
, block
);
3243 else if (sym
->attr
.pointer
|| sym
->attr
.allocatable
)
3245 if (TREE_STATIC (sym
->backend_decl
))
3246 gfc_trans_static_array_pointer (sym
);
3249 seen_trans_deferred_array
= true;
3250 gfc_trans_deferred_array (sym
, block
);
3255 if (sym_has_alloc_comp
)
3257 seen_trans_deferred_array
= true;
3258 gfc_trans_deferred_array (sym
, block
);
3260 else if (sym
->ts
.type
== BT_DERIVED
3263 && sym
->attr
.save
== SAVE_NONE
)
3265 gfc_start_block (&tmpblock
);
3266 gfc_init_default_dt (sym
, &tmpblock
, false);
3267 gfc_add_init_cleanup (block
,
3268 gfc_finish_block (&tmpblock
),
3272 gfc_save_backend_locus (&loc
);
3273 gfc_set_backend_locus (&sym
->declared_at
);
3274 gfc_trans_auto_array_allocation (sym
->backend_decl
,
3276 gfc_restore_backend_locus (&loc
);
3280 case AS_ASSUMED_SIZE
:
3281 /* Must be a dummy parameter. */
3282 gcc_assert (sym
->attr
.dummy
|| sym
->as
->cp_was_assumed
);
3284 /* We should always pass assumed size arrays the g77 way. */
3285 if (sym
->attr
.dummy
)
3286 gfc_trans_g77_array (sym
, block
);
3289 case AS_ASSUMED_SHAPE
:
3290 /* Must be a dummy parameter. */
3291 gcc_assert (sym
->attr
.dummy
);
3293 gfc_trans_dummy_array_bias (sym
, sym
->backend_decl
, block
);
3297 seen_trans_deferred_array
= true;
3298 gfc_trans_deferred_array (sym
, block
);
3304 if (sym_has_alloc_comp
&& !seen_trans_deferred_array
)
3305 gfc_trans_deferred_array (sym
, block
);
3307 else if (sym
->attr
.allocatable
3308 || (sym
->ts
.type
== BT_CLASS
3309 && CLASS_DATA (sym
)->attr
.allocatable
))
3311 if (!sym
->attr
.save
)
3313 /* Nullify and automatic deallocation of allocatable
3320 e
= gfc_lval_expr_from_sym (sym
);
3321 if (sym
->ts
.type
== BT_CLASS
)
3322 gfc_add_data_component (e
);
3324 gfc_init_se (&se
, NULL
);
3325 se
.want_pointer
= 1;
3326 gfc_conv_expr (&se
, e
);
3329 /* Nullify when entering the scope. */
3330 gfc_start_block (&init
);
3331 gfc_add_modify (&init
, se
.expr
,
3332 fold_convert (TREE_TYPE (se
.expr
),
3333 null_pointer_node
));
3335 /* Deallocate when leaving the scope. Nullifying is not
3337 if (!sym
->attr
.result
)
3338 tmp
= gfc_deallocate_scalar_with_status (se
.expr
, NULL
, true,
3342 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
3345 else if (sym
->ts
.deferred
)
3346 gfc_fatal_error ("Deferred type parameter not yet supported");
3347 else if (sym_has_alloc_comp
)
3348 gfc_trans_deferred_array (sym
, block
);
3349 else if (sym
->ts
.type
== BT_CHARACTER
)
3351 gfc_save_backend_locus (&loc
);
3352 gfc_set_backend_locus (&sym
->declared_at
);
3353 if (sym
->attr
.dummy
|| sym
->attr
.result
)
3354 gfc_trans_dummy_character (sym
, sym
->ts
.u
.cl
, block
);
3356 gfc_trans_auto_character_variable (sym
, block
);
3357 gfc_restore_backend_locus (&loc
);
3359 else if (sym
->attr
.assign
)
3361 gfc_save_backend_locus (&loc
);
3362 gfc_set_backend_locus (&sym
->declared_at
);
3363 gfc_trans_assign_aux_var (sym
, block
);
3364 gfc_restore_backend_locus (&loc
);
3366 else if (sym
->ts
.type
== BT_DERIVED
3369 && sym
->attr
.save
== SAVE_NONE
)
3371 gfc_start_block (&tmpblock
);
3372 gfc_init_default_dt (sym
, &tmpblock
, false);
3373 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
),
3380 gfc_init_block (&tmpblock
);
3382 for (f
= proc_sym
->formal
; f
; f
= f
->next
)
3384 if (f
->sym
&& f
->sym
->tlink
== NULL
&& f
->sym
->ts
.type
== BT_CHARACTER
)
3386 gcc_assert (f
->sym
->ts
.u
.cl
->backend_decl
!= NULL
);
3387 if (TREE_CODE (f
->sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
3388 gfc_trans_vla_type_sizes (f
->sym
, &tmpblock
);
3392 if (gfc_return_by_reference (proc_sym
) && proc_sym
->ts
.type
== BT_CHARACTER
3393 && current_fake_result_decl
!= NULL
)
3395 gcc_assert (proc_sym
->ts
.u
.cl
->backend_decl
!= NULL
);
3396 if (TREE_CODE (proc_sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
3397 gfc_trans_vla_type_sizes (proc_sym
, &tmpblock
);
3400 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
), NULL_TREE
);
3403 static GTY ((param_is (struct module_htab_entry
))) htab_t module_htab
;
3405 /* Hash and equality functions for module_htab. */
3408 module_htab_do_hash (const void *x
)
3410 return htab_hash_string (((const struct module_htab_entry
*)x
)->name
);
3414 module_htab_eq (const void *x1
, const void *x2
)
3416 return strcmp ((((const struct module_htab_entry
*)x1
)->name
),
3417 (const char *)x2
) == 0;
3420 /* Hash and equality functions for module_htab's decls. */
3423 module_htab_decls_hash (const void *x
)
3425 const_tree t
= (const_tree
) x
;
3426 const_tree n
= DECL_NAME (t
);
3428 n
= TYPE_NAME (TREE_TYPE (t
));
3429 return htab_hash_string (IDENTIFIER_POINTER (n
));
3433 module_htab_decls_eq (const void *x1
, const void *x2
)
3435 const_tree t1
= (const_tree
) x1
;
3436 const_tree n1
= DECL_NAME (t1
);
3437 if (n1
== NULL_TREE
)
3438 n1
= TYPE_NAME (TREE_TYPE (t1
));
3439 return strcmp (IDENTIFIER_POINTER (n1
), (const char *) x2
) == 0;
3442 struct module_htab_entry
*
3443 gfc_find_module (const char *name
)
3448 module_htab
= htab_create_ggc (10, module_htab_do_hash
,
3449 module_htab_eq
, NULL
);
3451 slot
= htab_find_slot_with_hash (module_htab
, name
,
3452 htab_hash_string (name
), INSERT
);
3455 struct module_htab_entry
*entry
= ggc_alloc_cleared_module_htab_entry ();
3457 entry
->name
= gfc_get_string (name
);
3458 entry
->decls
= htab_create_ggc (10, module_htab_decls_hash
,
3459 module_htab_decls_eq
, NULL
);
3460 *slot
= (void *) entry
;
3462 return (struct module_htab_entry
*) *slot
;
3466 gfc_module_add_decl (struct module_htab_entry
*entry
, tree decl
)
3471 if (DECL_NAME (decl
))
3472 name
= IDENTIFIER_POINTER (DECL_NAME (decl
));
3475 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
3476 name
= IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl
)));
3478 slot
= htab_find_slot_with_hash (entry
->decls
, name
,
3479 htab_hash_string (name
), INSERT
);
3481 *slot
= (void *) decl
;
3484 static struct module_htab_entry
*cur_module
;
3486 /* Output an initialized decl for a module variable. */
3489 gfc_create_module_variable (gfc_symbol
* sym
)
3493 /* Module functions with alternate entries are dealt with later and
3494 would get caught by the next condition. */
3495 if (sym
->attr
.entry
)
3498 /* Make sure we convert the types of the derived types from iso_c_binding
3500 if (sym
->attr
.flavor
!= FL_PROCEDURE
&& sym
->attr
.is_iso_c
3501 && sym
->ts
.type
== BT_DERIVED
)
3502 sym
->backend_decl
= gfc_typenode_for_spec (&(sym
->ts
));
3504 if (sym
->attr
.flavor
== FL_DERIVED
3505 && sym
->backend_decl
3506 && TREE_CODE (sym
->backend_decl
) == RECORD_TYPE
)
3508 decl
= sym
->backend_decl
;
3509 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
3511 /* -fwhole-file mixes up the contexts so these asserts are unnecessary. */
3512 if (!(gfc_option
.flag_whole_file
&& sym
->attr
.use_assoc
))
3514 gcc_assert (TYPE_CONTEXT (decl
) == NULL_TREE
3515 || TYPE_CONTEXT (decl
) == sym
->ns
->proc_name
->backend_decl
);
3516 gcc_assert (DECL_CONTEXT (TYPE_STUB_DECL (decl
)) == NULL_TREE
3517 || DECL_CONTEXT (TYPE_STUB_DECL (decl
))
3518 == sym
->ns
->proc_name
->backend_decl
);
3520 TYPE_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
3521 DECL_CONTEXT (TYPE_STUB_DECL (decl
)) = sym
->ns
->proc_name
->backend_decl
;
3522 gfc_module_add_decl (cur_module
, TYPE_STUB_DECL (decl
));
3525 /* Only output variables, procedure pointers and array valued,
3526 or derived type, parameters. */
3527 if (sym
->attr
.flavor
!= FL_VARIABLE
3528 && !(sym
->attr
.flavor
== FL_PARAMETER
3529 && (sym
->attr
.dimension
|| sym
->ts
.type
== BT_DERIVED
))
3530 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.proc_pointer
))
3533 if ((sym
->attr
.in_common
|| sym
->attr
.in_equivalence
) && sym
->backend_decl
)
3535 decl
= sym
->backend_decl
;
3536 gcc_assert (DECL_FILE_SCOPE_P (decl
));
3537 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
3538 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
3539 gfc_module_add_decl (cur_module
, decl
);
3542 /* Don't generate variables from other modules. Variables from
3543 COMMONs will already have been generated. */
3544 if (sym
->attr
.use_assoc
|| sym
->attr
.in_common
)
3547 /* Equivalenced variables arrive here after creation. */
3548 if (sym
->backend_decl
3549 && (sym
->equiv_built
|| sym
->attr
.in_equivalence
))
3552 if (sym
->backend_decl
&& !sym
->attr
.vtab
&& !sym
->attr
.target
)
3553 internal_error ("backend decl for module variable %s already exists",
3556 /* We always want module variables to be created. */
3557 sym
->attr
.referenced
= 1;
3558 /* Create the decl. */
3559 decl
= gfc_get_symbol_decl (sym
);
3561 /* Create the variable. */
3563 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
3564 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
3565 rest_of_decl_compilation (decl
, 1, 0);
3566 gfc_module_add_decl (cur_module
, decl
);
3568 /* Also add length of strings. */
3569 if (sym
->ts
.type
== BT_CHARACTER
)
3573 length
= sym
->ts
.u
.cl
->backend_decl
;
3574 gcc_assert (length
|| sym
->attr
.proc_pointer
);
3575 if (length
&& !INTEGER_CST_P (length
))
3578 rest_of_decl_compilation (length
, 1, 0);
3583 /* Emit debug information for USE statements. */
3586 gfc_trans_use_stmts (gfc_namespace
* ns
)
3588 gfc_use_list
*use_stmt
;
3589 for (use_stmt
= ns
->use_stmts
; use_stmt
; use_stmt
= use_stmt
->next
)
3591 struct module_htab_entry
*entry
3592 = gfc_find_module (use_stmt
->module_name
);
3593 gfc_use_rename
*rent
;
3595 if (entry
->namespace_decl
== NULL
)
3597 entry
->namespace_decl
3598 = build_decl (input_location
,
3600 get_identifier (use_stmt
->module_name
),
3602 DECL_EXTERNAL (entry
->namespace_decl
) = 1;
3604 gfc_set_backend_locus (&use_stmt
->where
);
3605 if (!use_stmt
->only_flag
)
3606 (*debug_hooks
->imported_module_or_decl
) (entry
->namespace_decl
,
3608 ns
->proc_name
->backend_decl
,
3610 for (rent
= use_stmt
->rename
; rent
; rent
= rent
->next
)
3612 tree decl
, local_name
;
3615 if (rent
->op
!= INTRINSIC_NONE
)
3618 slot
= htab_find_slot_with_hash (entry
->decls
, rent
->use_name
,
3619 htab_hash_string (rent
->use_name
),
3625 st
= gfc_find_symtree (ns
->sym_root
,
3627 ? rent
->local_name
: rent
->use_name
);
3630 /* Sometimes, generic interfaces wind up being over-ruled by a
3631 local symbol (see PR41062). */
3632 if (!st
->n
.sym
->attr
.use_assoc
)
3635 if (st
->n
.sym
->backend_decl
3636 && DECL_P (st
->n
.sym
->backend_decl
)
3637 && st
->n
.sym
->module
3638 && strcmp (st
->n
.sym
->module
, use_stmt
->module_name
) == 0)
3640 gcc_assert (DECL_EXTERNAL (entry
->namespace_decl
)
3641 || (TREE_CODE (st
->n
.sym
->backend_decl
)
3643 decl
= copy_node (st
->n
.sym
->backend_decl
);
3644 DECL_CONTEXT (decl
) = entry
->namespace_decl
;
3645 DECL_EXTERNAL (decl
) = 1;
3646 DECL_IGNORED_P (decl
) = 0;
3647 DECL_INITIAL (decl
) = NULL_TREE
;
3651 *slot
= error_mark_node
;
3652 htab_clear_slot (entry
->decls
, slot
);
3657 decl
= (tree
) *slot
;
3658 if (rent
->local_name
[0])
3659 local_name
= get_identifier (rent
->local_name
);
3661 local_name
= NULL_TREE
;
3662 gfc_set_backend_locus (&rent
->where
);
3663 (*debug_hooks
->imported_module_or_decl
) (decl
, local_name
,
3664 ns
->proc_name
->backend_decl
,
3665 !use_stmt
->only_flag
);
3671 /* Return true if expr is a constant initializer that gfc_conv_initializer
3675 check_constant_initializer (gfc_expr
*expr
, gfc_typespec
*ts
, bool array
,
3685 if (expr
->expr_type
== EXPR_CONSTANT
|| expr
->expr_type
== EXPR_NULL
)
3687 else if (expr
->expr_type
== EXPR_STRUCTURE
)
3688 return check_constant_initializer (expr
, ts
, false, false);
3689 else if (expr
->expr_type
!= EXPR_ARRAY
)
3691 for (c
= gfc_constructor_first (expr
->value
.constructor
);
3692 c
; c
= gfc_constructor_next (c
))
3696 if (c
->expr
->expr_type
== EXPR_STRUCTURE
)
3698 if (!check_constant_initializer (c
->expr
, ts
, false, false))
3701 else if (c
->expr
->expr_type
!= EXPR_CONSTANT
)
3706 else switch (ts
->type
)
3709 if (expr
->expr_type
!= EXPR_STRUCTURE
)
3711 cm
= expr
->ts
.u
.derived
->components
;
3712 for (c
= gfc_constructor_first (expr
->value
.constructor
);
3713 c
; c
= gfc_constructor_next (c
), cm
= cm
->next
)
3715 if (!c
->expr
|| cm
->attr
.allocatable
)
3717 if (!check_constant_initializer (c
->expr
, &cm
->ts
,
3724 return expr
->expr_type
== EXPR_CONSTANT
;
3728 /* Emit debug info for parameters and unreferenced variables with
3732 gfc_emit_parameter_debug_info (gfc_symbol
*sym
)
3736 if (sym
->attr
.flavor
!= FL_PARAMETER
3737 && (sym
->attr
.flavor
!= FL_VARIABLE
|| sym
->attr
.referenced
))
3740 if (sym
->backend_decl
!= NULL
3741 || sym
->value
== NULL
3742 || sym
->attr
.use_assoc
3745 || sym
->attr
.function
3746 || sym
->attr
.intrinsic
3747 || sym
->attr
.pointer
3748 || sym
->attr
.allocatable
3749 || sym
->attr
.cray_pointee
3750 || sym
->attr
.threadprivate
3751 || sym
->attr
.is_bind_c
3752 || sym
->attr
.subref_array_pointer
3753 || sym
->attr
.assign
)
3756 if (sym
->ts
.type
== BT_CHARACTER
)
3758 gfc_conv_const_charlen (sym
->ts
.u
.cl
);
3759 if (sym
->ts
.u
.cl
->backend_decl
== NULL
3760 || TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) != INTEGER_CST
)
3763 else if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.alloc_comp
)
3770 if (sym
->as
->type
!= AS_EXPLICIT
)
3772 for (n
= 0; n
< sym
->as
->rank
; n
++)
3773 if (sym
->as
->lower
[n
]->expr_type
!= EXPR_CONSTANT
3774 || sym
->as
->upper
[n
] == NULL
3775 || sym
->as
->upper
[n
]->expr_type
!= EXPR_CONSTANT
)
3779 if (!check_constant_initializer (sym
->value
, &sym
->ts
,
3780 sym
->attr
.dimension
, false))
3783 /* Create the decl for the variable or constant. */
3784 decl
= build_decl (input_location
,
3785 sym
->attr
.flavor
== FL_PARAMETER
? CONST_DECL
: VAR_DECL
,
3786 gfc_sym_identifier (sym
), gfc_sym_type (sym
));
3787 if (sym
->attr
.flavor
== FL_PARAMETER
)
3788 TREE_READONLY (decl
) = 1;
3789 gfc_set_decl_location (decl
, &sym
->declared_at
);
3790 if (sym
->attr
.dimension
)
3791 GFC_DECL_PACKED_ARRAY (decl
) = 1;
3792 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
3793 TREE_STATIC (decl
) = 1;
3794 TREE_USED (decl
) = 1;
3795 if (DECL_CONTEXT (decl
) && TREE_CODE (DECL_CONTEXT (decl
)) == NAMESPACE_DECL
)
3796 TREE_PUBLIC (decl
) = 1;
3797 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
3799 sym
->attr
.dimension
,
3801 debug_hooks
->global_decl (decl
);
3804 /* Generate all the required code for module variables. */
3807 gfc_generate_module_vars (gfc_namespace
* ns
)
3809 module_namespace
= ns
;
3810 cur_module
= gfc_find_module (ns
->proc_name
->name
);
3812 /* Check if the frontend left the namespace in a reasonable state. */
3813 gcc_assert (ns
->proc_name
&& !ns
->proc_name
->tlink
);
3815 /* Generate COMMON blocks. */
3816 gfc_trans_common (ns
);
3818 /* Create decls for all the module variables. */
3819 gfc_traverse_ns (ns
, gfc_create_module_variable
);
3823 gfc_trans_use_stmts (ns
);
3824 gfc_traverse_ns (ns
, gfc_emit_parameter_debug_info
);
3829 gfc_generate_contained_functions (gfc_namespace
* parent
)
3833 /* We create all the prototypes before generating any code. */
3834 for (ns
= parent
->contained
; ns
; ns
= ns
->sibling
)
3836 /* Skip namespaces from used modules. */
3837 if (ns
->parent
!= parent
)
3840 gfc_create_function_decl (ns
, false);
3843 for (ns
= parent
->contained
; ns
; ns
= ns
->sibling
)
3845 /* Skip namespaces from used modules. */
3846 if (ns
->parent
!= parent
)
3849 gfc_generate_function_code (ns
);
3854 /* Drill down through expressions for the array specification bounds and
3855 character length calling generate_local_decl for all those variables
3856 that have not already been declared. */
3859 generate_local_decl (gfc_symbol
*);
3861 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
3864 expr_decls (gfc_expr
*e
, gfc_symbol
*sym
,
3865 int *f ATTRIBUTE_UNUSED
)
3867 if (e
->expr_type
!= EXPR_VARIABLE
3868 || sym
== e
->symtree
->n
.sym
3869 || e
->symtree
->n
.sym
->mark
3870 || e
->symtree
->n
.sym
->ns
!= sym
->ns
)
3873 generate_local_decl (e
->symtree
->n
.sym
);
3878 generate_expr_decls (gfc_symbol
*sym
, gfc_expr
*e
)
3880 gfc_traverse_expr (e
, sym
, expr_decls
, 0);
3884 /* Check for dependencies in the character length and array spec. */
3887 generate_dependency_declarations (gfc_symbol
*sym
)
3891 if (sym
->ts
.type
== BT_CHARACTER
3893 && sym
->ts
.u
.cl
->length
3894 && sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
)
3895 generate_expr_decls (sym
, sym
->ts
.u
.cl
->length
);
3897 if (sym
->as
&& sym
->as
->rank
)
3899 for (i
= 0; i
< sym
->as
->rank
; i
++)
3901 generate_expr_decls (sym
, sym
->as
->lower
[i
]);
3902 generate_expr_decls (sym
, sym
->as
->upper
[i
]);
3908 /* Generate decls for all local variables. We do this to ensure correct
3909 handling of expressions which only appear in the specification of
3913 generate_local_decl (gfc_symbol
* sym
)
3915 if (sym
->attr
.flavor
== FL_VARIABLE
)
3917 if (!sym
->attr
.dummy
&& !sym
->ns
->proc_name
->attr
.entry_master
)
3918 generate_dependency_declarations (sym
);
3920 if (sym
->attr
.referenced
)
3921 gfc_get_symbol_decl (sym
);
3923 /* Warnings for unused dummy arguments. */
3924 else if (sym
->attr
.dummy
)
3926 /* INTENT(out) dummy arguments are likely meant to be set. */
3927 if (gfc_option
.warn_unused_dummy_argument
3928 && sym
->attr
.intent
== INTENT_OUT
)
3930 if (sym
->ts
.type
!= BT_DERIVED
)
3931 gfc_warning ("Dummy argument '%s' at %L was declared "
3932 "INTENT(OUT) but was not set", sym
->name
,
3934 else if (!gfc_has_default_initializer (sym
->ts
.u
.derived
))
3935 gfc_warning ("Derived-type dummy argument '%s' at %L was "
3936 "declared INTENT(OUT) but was not set and "
3937 "does not have a default initializer",
3938 sym
->name
, &sym
->declared_at
);
3940 else if (gfc_option
.warn_unused_dummy_argument
)
3941 gfc_warning ("Unused dummy argument '%s' at %L", sym
->name
,
3945 /* Warn for unused variables, but not if they're inside a common
3946 block, a namelist, or are use-associated. */
3947 else if (warn_unused_variable
3948 && !(sym
->attr
.in_common
|| sym
->attr
.use_assoc
|| sym
->mark
3949 || sym
->attr
.in_namelist
))
3950 gfc_warning ("Unused variable '%s' declared at %L", sym
->name
,
3953 /* For variable length CHARACTER parameters, the PARM_DECL already
3954 references the length variable, so force gfc_get_symbol_decl
3955 even when not referenced. If optimize > 0, it will be optimized
3956 away anyway. But do this only after emitting -Wunused-parameter
3957 warning if requested. */
3958 if (sym
->attr
.dummy
&& !sym
->attr
.referenced
3959 && sym
->ts
.type
== BT_CHARACTER
3960 && sym
->ts
.u
.cl
->backend_decl
!= NULL
3961 && TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) == VAR_DECL
)
3963 sym
->attr
.referenced
= 1;
3964 gfc_get_symbol_decl (sym
);
3967 /* INTENT(out) dummy arguments and result variables with allocatable
3968 components are reset by default and need to be set referenced to
3969 generate the code for nullification and automatic lengths. */
3970 if (!sym
->attr
.referenced
3971 && sym
->ts
.type
== BT_DERIVED
3972 && sym
->ts
.u
.derived
->attr
.alloc_comp
3973 && !sym
->attr
.pointer
3974 && ((sym
->attr
.dummy
&& sym
->attr
.intent
== INTENT_OUT
)
3976 (sym
->attr
.result
&& sym
!= sym
->result
)))
3978 sym
->attr
.referenced
= 1;
3979 gfc_get_symbol_decl (sym
);
3982 /* Check for dependencies in the array specification and string
3983 length, adding the necessary declarations to the function. We
3984 mark the symbol now, as well as in traverse_ns, to prevent
3985 getting stuck in a circular dependency. */
3988 /* We do not want the middle-end to warn about unused parameters
3989 as this was already done above. */
3990 if (sym
->attr
.dummy
&& sym
->backend_decl
!= NULL_TREE
)
3991 TREE_NO_WARNING(sym
->backend_decl
) = 1;
3993 else if (sym
->attr
.flavor
== FL_PARAMETER
)
3995 if (warn_unused_parameter
3996 && !sym
->attr
.referenced
3997 && !sym
->attr
.use_assoc
)
3998 gfc_warning ("Unused parameter '%s' declared at %L", sym
->name
,
4001 else if (sym
->attr
.flavor
== FL_PROCEDURE
)
4003 /* TODO: move to the appropriate place in resolve.c. */
4004 if (warn_return_type
4005 && sym
->attr
.function
4007 && sym
!= sym
->result
4008 && !sym
->result
->attr
.referenced
4009 && !sym
->attr
.use_assoc
4010 && sym
->attr
.if_source
!= IFSRC_IFBODY
)
4012 gfc_warning ("Return value '%s' of function '%s' declared at "
4013 "%L not set", sym
->result
->name
, sym
->name
,
4014 &sym
->result
->declared_at
);
4016 /* Prevents "Unused variable" warning for RESULT variables. */
4017 sym
->result
->mark
= 1;
4021 if (sym
->attr
.dummy
== 1)
4023 /* Modify the tree type for scalar character dummy arguments of bind(c)
4024 procedures if they are passed by value. The tree type for them will
4025 be promoted to INTEGER_TYPE for the middle end, which appears to be
4026 what C would do with characters passed by-value. The value attribute
4027 implies the dummy is a scalar. */
4028 if (sym
->attr
.value
== 1 && sym
->backend_decl
!= NULL
4029 && sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.is_c_interop
4030 && sym
->ns
->proc_name
!= NULL
&& sym
->ns
->proc_name
->attr
.is_bind_c
)
4031 gfc_conv_scalar_char_value (sym
, NULL
, NULL
);
4034 /* Make sure we convert the types of the derived types from iso_c_binding
4036 if (sym
->attr
.flavor
!= FL_PROCEDURE
&& sym
->attr
.is_iso_c
4037 && sym
->ts
.type
== BT_DERIVED
)
4038 sym
->backend_decl
= gfc_typenode_for_spec (&(sym
->ts
));
4042 generate_local_vars (gfc_namespace
* ns
)
4044 gfc_traverse_ns (ns
, generate_local_decl
);
4048 /* Generate a switch statement to jump to the correct entry point. Also
4049 creates the label decls for the entry points. */
4052 gfc_trans_entry_master_switch (gfc_entry_list
* el
)
4059 gfc_init_block (&block
);
4060 for (; el
; el
= el
->next
)
4062 /* Add the case label. */
4063 label
= gfc_build_label_decl (NULL_TREE
);
4064 val
= build_int_cst (gfc_array_index_type
, el
->id
);
4065 tmp
= build3_v (CASE_LABEL_EXPR
, val
, NULL_TREE
, label
);
4066 gfc_add_expr_to_block (&block
, tmp
);
4068 /* And jump to the actual entry point. */
4069 label
= gfc_build_label_decl (NULL_TREE
);
4070 tmp
= build1_v (GOTO_EXPR
, label
);
4071 gfc_add_expr_to_block (&block
, tmp
);
4073 /* Save the label decl. */
4076 tmp
= gfc_finish_block (&block
);
4077 /* The first argument selects the entry point. */
4078 val
= DECL_ARGUMENTS (current_function_decl
);
4079 tmp
= build3_v (SWITCH_EXPR
, val
, tmp
, NULL_TREE
);
4084 /* Add code to string lengths of actual arguments passed to a function against
4085 the expected lengths of the dummy arguments. */
4088 add_argument_checking (stmtblock_t
*block
, gfc_symbol
*sym
)
4090 gfc_formal_arglist
*formal
;
4092 for (formal
= sym
->formal
; formal
; formal
= formal
->next
)
4093 if (formal
->sym
&& formal
->sym
->ts
.type
== BT_CHARACTER
)
4095 enum tree_code comparison
;
4100 const char *message
;
4106 gcc_assert (cl
->passed_length
!= NULL_TREE
);
4107 gcc_assert (cl
->backend_decl
!= NULL_TREE
);
4109 /* For POINTER, ALLOCATABLE and assumed-shape dummy arguments, the
4110 string lengths must match exactly. Otherwise, it is only required
4111 that the actual string length is *at least* the expected one.
4112 Sequence association allows for a mismatch of the string length
4113 if the actual argument is (part of) an array, but only if the
4114 dummy argument is an array. (See "Sequence association" in
4115 Section 12.4.1.4 for F95 and 12.4.1.5 for F2003.) */
4116 if (fsym
->attr
.pointer
|| fsym
->attr
.allocatable
4117 || (fsym
->as
&& fsym
->as
->type
== AS_ASSUMED_SHAPE
))
4119 comparison
= NE_EXPR
;
4120 message
= _("Actual string length does not match the declared one"
4121 " for dummy argument '%s' (%ld/%ld)");
4123 else if (fsym
->as
&& fsym
->as
->rank
!= 0)
4127 comparison
= LT_EXPR
;
4128 message
= _("Actual string length is shorter than the declared one"
4129 " for dummy argument '%s' (%ld/%ld)");
4132 /* Build the condition. For optional arguments, an actual length
4133 of 0 is also acceptable if the associated string is NULL, which
4134 means the argument was not passed. */
4135 cond
= fold_build2_loc (input_location
, comparison
, boolean_type_node
,
4136 cl
->passed_length
, cl
->backend_decl
);
4137 if (fsym
->attr
.optional
)
4143 not_0length
= fold_build2_loc (input_location
, NE_EXPR
,
4146 build_zero_cst (gfc_charlen_type_node
));
4147 /* The symbol needs to be referenced for gfc_get_symbol_decl. */
4148 fsym
->attr
.referenced
= 1;
4149 not_absent
= gfc_conv_expr_present (fsym
);
4151 absent_failed
= fold_build2_loc (input_location
, TRUTH_OR_EXPR
,
4152 boolean_type_node
, not_0length
,
4155 cond
= fold_build2_loc (input_location
, TRUTH_AND_EXPR
,
4156 boolean_type_node
, cond
, absent_failed
);
4159 /* Build the runtime check. */
4160 argname
= gfc_build_cstring_const (fsym
->name
);
4161 argname
= gfc_build_addr_expr (pchar_type_node
, argname
);
4162 gfc_trans_runtime_check (true, false, cond
, block
, &fsym
->declared_at
,
4164 fold_convert (long_integer_type_node
,
4166 fold_convert (long_integer_type_node
,
4173 create_main_function (tree fndecl
)
4177 tree tmp
, decl
, result_decl
, argc
, argv
, typelist
, arglist
;
4180 old_context
= current_function_decl
;
4184 push_function_context ();
4185 saved_parent_function_decls
= saved_function_decls
;
4186 saved_function_decls
= NULL_TREE
;
4189 /* main() function must be declared with global scope. */
4190 gcc_assert (current_function_decl
== NULL_TREE
);
4192 /* Declare the function. */
4193 tmp
= build_function_type_list (integer_type_node
, integer_type_node
,
4194 build_pointer_type (pchar_type_node
),
4196 main_identifier_node
= get_identifier ("main");
4197 ftn_main
= build_decl (input_location
, FUNCTION_DECL
,
4198 main_identifier_node
, tmp
);
4199 DECL_EXTERNAL (ftn_main
) = 0;
4200 TREE_PUBLIC (ftn_main
) = 1;
4201 TREE_STATIC (ftn_main
) = 1;
4202 DECL_ATTRIBUTES (ftn_main
)
4203 = tree_cons (get_identifier("externally_visible"), NULL_TREE
, NULL_TREE
);
4205 /* Setup the result declaration (for "return 0"). */
4206 result_decl
= build_decl (input_location
,
4207 RESULT_DECL
, NULL_TREE
, integer_type_node
);
4208 DECL_ARTIFICIAL (result_decl
) = 1;
4209 DECL_IGNORED_P (result_decl
) = 1;
4210 DECL_CONTEXT (result_decl
) = ftn_main
;
4211 DECL_RESULT (ftn_main
) = result_decl
;
4213 pushdecl (ftn_main
);
4215 /* Get the arguments. */
4217 arglist
= NULL_TREE
;
4218 typelist
= TYPE_ARG_TYPES (TREE_TYPE (ftn_main
));
4220 tmp
= TREE_VALUE (typelist
);
4221 argc
= build_decl (input_location
, PARM_DECL
, get_identifier ("argc"), tmp
);
4222 DECL_CONTEXT (argc
) = ftn_main
;
4223 DECL_ARG_TYPE (argc
) = TREE_VALUE (typelist
);
4224 TREE_READONLY (argc
) = 1;
4225 gfc_finish_decl (argc
);
4226 arglist
= chainon (arglist
, argc
);
4228 typelist
= TREE_CHAIN (typelist
);
4229 tmp
= TREE_VALUE (typelist
);
4230 argv
= build_decl (input_location
, PARM_DECL
, get_identifier ("argv"), tmp
);
4231 DECL_CONTEXT (argv
) = ftn_main
;
4232 DECL_ARG_TYPE (argv
) = TREE_VALUE (typelist
);
4233 TREE_READONLY (argv
) = 1;
4234 DECL_BY_REFERENCE (argv
) = 1;
4235 gfc_finish_decl (argv
);
4236 arglist
= chainon (arglist
, argv
);
4238 DECL_ARGUMENTS (ftn_main
) = arglist
;
4239 current_function_decl
= ftn_main
;
4240 announce_function (ftn_main
);
4242 rest_of_decl_compilation (ftn_main
, 1, 0);
4243 make_decl_rtl (ftn_main
);
4244 init_function_start (ftn_main
);
4247 gfc_init_block (&body
);
4249 /* Call some libgfortran initialization routines, call then MAIN__(). */
4251 /* Call _gfortran_set_args (argc, argv). */
4252 TREE_USED (argc
) = 1;
4253 TREE_USED (argv
) = 1;
4254 tmp
= build_call_expr_loc (input_location
,
4255 gfor_fndecl_set_args
, 2, argc
, argv
);
4256 gfc_add_expr_to_block (&body
, tmp
);
4258 /* Add a call to set_options to set up the runtime library Fortran
4259 language standard parameters. */
4261 tree array_type
, array
, var
;
4262 VEC(constructor_elt
,gc
) *v
= NULL
;
4264 /* Passing a new option to the library requires four modifications:
4265 + add it to the tree_cons list below
4266 + change the array size in the call to build_array_type
4267 + change the first argument to the library call
4268 gfor_fndecl_set_options
4269 + modify the library (runtime/compile_options.c)! */
4271 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
4272 build_int_cst (integer_type_node
,
4273 gfc_option
.warn_std
));
4274 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
4275 build_int_cst (integer_type_node
,
4276 gfc_option
.allow_std
));
4277 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
4278 build_int_cst (integer_type_node
, pedantic
));
4279 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
4280 build_int_cst (integer_type_node
,
4281 gfc_option
.flag_dump_core
));
4282 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
4283 build_int_cst (integer_type_node
,
4284 gfc_option
.flag_backtrace
));
4285 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
4286 build_int_cst (integer_type_node
,
4287 gfc_option
.flag_sign_zero
));
4288 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
4289 build_int_cst (integer_type_node
,
4291 & GFC_RTCHECK_BOUNDS
)));
4292 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
4293 build_int_cst (integer_type_node
,
4294 gfc_option
.flag_range_check
));
4296 array_type
= build_array_type (integer_type_node
,
4297 build_index_type (build_int_cst (NULL_TREE
, 7)));
4298 array
= build_constructor (array_type
, v
);
4299 TREE_CONSTANT (array
) = 1;
4300 TREE_STATIC (array
) = 1;
4302 /* Create a static variable to hold the jump table. */
4303 var
= gfc_create_var (array_type
, "options");
4304 TREE_CONSTANT (var
) = 1;
4305 TREE_STATIC (var
) = 1;
4306 TREE_READONLY (var
) = 1;
4307 DECL_INITIAL (var
) = array
;
4308 var
= gfc_build_addr_expr (build_pointer_type (integer_type_node
), var
);
4310 tmp
= build_call_expr_loc (input_location
,
4311 gfor_fndecl_set_options
, 2,
4312 build_int_cst (integer_type_node
, 8), var
);
4313 gfc_add_expr_to_block (&body
, tmp
);
4316 /* If -ffpe-trap option was provided, add a call to set_fpe so that
4317 the library will raise a FPE when needed. */
4318 if (gfc_option
.fpe
!= 0)
4320 tmp
= build_call_expr_loc (input_location
,
4321 gfor_fndecl_set_fpe
, 1,
4322 build_int_cst (integer_type_node
,
4324 gfc_add_expr_to_block (&body
, tmp
);
4327 /* If this is the main program and an -fconvert option was provided,
4328 add a call to set_convert. */
4330 if (gfc_option
.convert
!= GFC_CONVERT_NATIVE
)
4332 tmp
= build_call_expr_loc (input_location
,
4333 gfor_fndecl_set_convert
, 1,
4334 build_int_cst (integer_type_node
,
4335 gfc_option
.convert
));
4336 gfc_add_expr_to_block (&body
, tmp
);
4339 /* If this is the main program and an -frecord-marker option was provided,
4340 add a call to set_record_marker. */
4342 if (gfc_option
.record_marker
!= 0)
4344 tmp
= build_call_expr_loc (input_location
,
4345 gfor_fndecl_set_record_marker
, 1,
4346 build_int_cst (integer_type_node
,
4347 gfc_option
.record_marker
));
4348 gfc_add_expr_to_block (&body
, tmp
);
4351 if (gfc_option
.max_subrecord_length
!= 0)
4353 tmp
= build_call_expr_loc (input_location
,
4354 gfor_fndecl_set_max_subrecord_length
, 1,
4355 build_int_cst (integer_type_node
,
4356 gfc_option
.max_subrecord_length
));
4357 gfc_add_expr_to_block (&body
, tmp
);
4360 /* Call MAIN__(). */
4361 tmp
= build_call_expr_loc (input_location
,
4363 gfc_add_expr_to_block (&body
, tmp
);
4365 /* Mark MAIN__ as used. */
4366 TREE_USED (fndecl
) = 1;
4369 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, integer_type_node
,
4370 DECL_RESULT (ftn_main
),
4371 build_int_cst (integer_type_node
, 0));
4372 tmp
= build1_v (RETURN_EXPR
, tmp
);
4373 gfc_add_expr_to_block (&body
, tmp
);
4376 DECL_SAVED_TREE (ftn_main
) = gfc_finish_block (&body
);
4379 /* Finish off this function and send it for code generation. */
4381 BLOCK_SUPERCONTEXT (DECL_INITIAL (ftn_main
)) = ftn_main
;
4383 DECL_SAVED_TREE (ftn_main
)
4384 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (ftn_main
),
4385 DECL_INITIAL (ftn_main
));
4387 /* Output the GENERIC tree. */
4388 dump_function (TDI_original
, ftn_main
);
4390 cgraph_finalize_function (ftn_main
, true);
4394 pop_function_context ();
4395 saved_function_decls
= saved_parent_function_decls
;
4397 current_function_decl
= old_context
;
4401 /* Get the result expression for a procedure. */
4404 get_proc_result (gfc_symbol
* sym
)
4406 if (sym
->attr
.subroutine
|| sym
== sym
->result
)
4408 if (current_fake_result_decl
!= NULL
)
4409 return TREE_VALUE (current_fake_result_decl
);
4414 return sym
->result
->backend_decl
;
4418 /* Generate an appropriate return-statement for a procedure. */
4421 gfc_generate_return (void)
4427 sym
= current_procedure_symbol
;
4428 fndecl
= sym
->backend_decl
;
4430 if (TREE_TYPE (DECL_RESULT (fndecl
)) == void_type_node
)
4434 result
= get_proc_result (sym
);
4436 /* Set the return value to the dummy result variable. The
4437 types may be different for scalar default REAL functions
4438 with -ff2c, therefore we have to convert. */
4439 if (result
!= NULL_TREE
)
4441 result
= convert (TREE_TYPE (DECL_RESULT (fndecl
)), result
);
4442 result
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4443 TREE_TYPE (result
), DECL_RESULT (fndecl
),
4448 return build1_v (RETURN_EXPR
, result
);
4452 /* Generate code for a function. */
4455 gfc_generate_function_code (gfc_namespace
* ns
)
4461 stmtblock_t init
, cleanup
;
4463 gfc_wrapped_block try_block
;
4464 tree recurcheckvar
= NULL_TREE
;
4466 gfc_symbol
*previous_procedure_symbol
;
4470 sym
= ns
->proc_name
;
4471 previous_procedure_symbol
= current_procedure_symbol
;
4472 current_procedure_symbol
= sym
;
4474 /* Check that the frontend isn't still using this. */
4475 gcc_assert (sym
->tlink
== NULL
);
4478 /* Create the declaration for functions with global scope. */
4479 if (!sym
->backend_decl
)
4480 gfc_create_function_decl (ns
, false);
4482 fndecl
= sym
->backend_decl
;
4483 old_context
= current_function_decl
;
4487 push_function_context ();
4488 saved_parent_function_decls
= saved_function_decls
;
4489 saved_function_decls
= NULL_TREE
;
4492 trans_function_start (sym
);
4494 gfc_init_block (&init
);
4496 if (ns
->entries
&& ns
->proc_name
->ts
.type
== BT_CHARACTER
)
4498 /* Copy length backend_decls to all entry point result
4503 gfc_conv_const_charlen (ns
->proc_name
->ts
.u
.cl
);
4504 backend_decl
= ns
->proc_name
->result
->ts
.u
.cl
->backend_decl
;
4505 for (el
= ns
->entries
; el
; el
= el
->next
)
4506 el
->sym
->result
->ts
.u
.cl
->backend_decl
= backend_decl
;
4509 /* Translate COMMON blocks. */
4510 gfc_trans_common (ns
);
4512 /* Null the parent fake result declaration if this namespace is
4513 a module function or an external procedures. */
4514 if ((ns
->parent
&& ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
4515 || ns
->parent
== NULL
)
4516 parent_fake_result_decl
= NULL_TREE
;
4518 gfc_generate_contained_functions (ns
);
4520 nonlocal_dummy_decls
= NULL
;
4521 nonlocal_dummy_decl_pset
= NULL
;
4523 generate_local_vars (ns
);
4525 /* Keep the parent fake result declaration in module functions
4526 or external procedures. */
4527 if ((ns
->parent
&& ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
4528 || ns
->parent
== NULL
)
4529 current_fake_result_decl
= parent_fake_result_decl
;
4531 current_fake_result_decl
= NULL_TREE
;
4533 is_recursive
= sym
->attr
.recursive
4534 || (sym
->attr
.entry_master
4535 && sym
->ns
->entries
->sym
->attr
.recursive
);
4536 if ((gfc_option
.rtcheck
& GFC_RTCHECK_RECURSION
)
4538 && !gfc_option
.flag_recursive
)
4542 asprintf (&msg
, "Recursive call to nonrecursive procedure '%s'",
4544 recurcheckvar
= gfc_create_var (boolean_type_node
, "is_recursive");
4545 TREE_STATIC (recurcheckvar
) = 1;
4546 DECL_INITIAL (recurcheckvar
) = boolean_false_node
;
4547 gfc_add_expr_to_block (&init
, recurcheckvar
);
4548 gfc_trans_runtime_check (true, false, recurcheckvar
, &init
,
4549 &sym
->declared_at
, msg
);
4550 gfc_add_modify (&init
, recurcheckvar
, boolean_true_node
);
4554 /* Now generate the code for the body of this function. */
4555 gfc_init_block (&body
);
4557 if (TREE_TYPE (DECL_RESULT (fndecl
)) != void_type_node
4558 && sym
->attr
.subroutine
)
4560 tree alternate_return
;
4561 alternate_return
= gfc_get_fake_result_decl (sym
, 0);
4562 gfc_add_modify (&body
, alternate_return
, integer_zero_node
);
4567 /* Jump to the correct entry point. */
4568 tmp
= gfc_trans_entry_master_switch (ns
->entries
);
4569 gfc_add_expr_to_block (&body
, tmp
);
4572 /* If bounds-checking is enabled, generate code to check passed in actual
4573 arguments against the expected dummy argument attributes (e.g. string
4575 if ((gfc_option
.rtcheck
& GFC_RTCHECK_BOUNDS
) && !sym
->attr
.is_bind_c
)
4576 add_argument_checking (&body
, sym
);
4578 tmp
= gfc_trans_code (ns
->code
);
4579 gfc_add_expr_to_block (&body
, tmp
);
4581 if (TREE_TYPE (DECL_RESULT (fndecl
)) != void_type_node
)
4583 tree result
= get_proc_result (sym
);
4585 if (result
!= NULL_TREE
4586 && sym
->attr
.function
4587 && !sym
->attr
.pointer
)
4589 if (sym
->ts
.type
== BT_DERIVED
4590 && sym
->ts
.u
.derived
->attr
.alloc_comp
)
4592 rank
= sym
->as
? sym
->as
->rank
: 0;
4593 tmp
= gfc_nullify_alloc_comp (sym
->ts
.u
.derived
, result
, rank
);
4594 gfc_add_expr_to_block (&init
, tmp
);
4596 else if (sym
->attr
.allocatable
&& sym
->attr
.dimension
== 0)
4597 gfc_add_modify (&init
, result
, fold_convert (TREE_TYPE (result
),
4598 null_pointer_node
));
4601 if (result
== NULL_TREE
)
4603 /* TODO: move to the appropriate place in resolve.c. */
4604 if (warn_return_type
&& !sym
->attr
.referenced
&& sym
== sym
->result
)
4605 gfc_warning ("Return value of function '%s' at %L not set",
4606 sym
->name
, &sym
->declared_at
);
4608 TREE_NO_WARNING(sym
->backend_decl
) = 1;
4611 gfc_add_expr_to_block (&body
, gfc_generate_return ());
4614 gfc_init_block (&cleanup
);
4616 /* Reset recursion-check variable. */
4617 if ((gfc_option
.rtcheck
& GFC_RTCHECK_RECURSION
)
4619 && !gfc_option
.gfc_flag_openmp
4620 && recurcheckvar
!= NULL_TREE
)
4622 gfc_add_modify (&cleanup
, recurcheckvar
, boolean_false_node
);
4623 recurcheckvar
= NULL
;
4626 /* Finish the function body and add init and cleanup code. */
4627 tmp
= gfc_finish_block (&body
);
4628 gfc_start_wrapped_block (&try_block
, tmp
);
4629 /* Add code to create and cleanup arrays. */
4630 gfc_trans_deferred_vars (sym
, &try_block
);
4631 gfc_add_init_cleanup (&try_block
, gfc_finish_block (&init
),
4632 gfc_finish_block (&cleanup
));
4634 /* Add all the decls we created during processing. */
4635 decl
= saved_function_decls
;
4640 next
= DECL_CHAIN (decl
);
4641 DECL_CHAIN (decl
) = NULL_TREE
;
4645 saved_function_decls
= NULL_TREE
;
4647 DECL_SAVED_TREE (fndecl
) = gfc_finish_wrapped_block (&try_block
);
4650 /* Finish off this function and send it for code generation. */
4652 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
4654 DECL_SAVED_TREE (fndecl
)
4655 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (fndecl
),
4656 DECL_INITIAL (fndecl
));
4658 if (nonlocal_dummy_decls
)
4660 BLOCK_VARS (DECL_INITIAL (fndecl
))
4661 = chainon (BLOCK_VARS (DECL_INITIAL (fndecl
)), nonlocal_dummy_decls
);
4662 pointer_set_destroy (nonlocal_dummy_decl_pset
);
4663 nonlocal_dummy_decls
= NULL
;
4664 nonlocal_dummy_decl_pset
= NULL
;
4667 /* Output the GENERIC tree. */
4668 dump_function (TDI_original
, fndecl
);
4670 /* Store the end of the function, so that we get good line number
4671 info for the epilogue. */
4672 cfun
->function_end_locus
= input_location
;
4674 /* We're leaving the context of this function, so zap cfun.
4675 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
4676 tree_rest_of_compilation. */
4681 pop_function_context ();
4682 saved_function_decls
= saved_parent_function_decls
;
4684 current_function_decl
= old_context
;
4686 if (decl_function_context (fndecl
))
4687 /* Register this function with cgraph just far enough to get it
4688 added to our parent's nested function list. */
4689 (void) cgraph_node (fndecl
);
4691 cgraph_finalize_function (fndecl
, true);
4693 gfc_trans_use_stmts (ns
);
4694 gfc_traverse_ns (ns
, gfc_emit_parameter_debug_info
);
4696 if (sym
->attr
.is_main_program
)
4697 create_main_function (fndecl
);
4699 current_procedure_symbol
= previous_procedure_symbol
;
4704 gfc_generate_constructors (void)
4706 gcc_assert (gfc_static_ctors
== NULL_TREE
);
4714 if (gfc_static_ctors
== NULL_TREE
)
4717 fnname
= get_file_function_name ("I");
4718 type
= build_function_type_list (void_type_node
, NULL_TREE
);
4720 fndecl
= build_decl (input_location
,
4721 FUNCTION_DECL
, fnname
, type
);
4722 TREE_PUBLIC (fndecl
) = 1;
4724 decl
= build_decl (input_location
,
4725 RESULT_DECL
, NULL_TREE
, void_type_node
);
4726 DECL_ARTIFICIAL (decl
) = 1;
4727 DECL_IGNORED_P (decl
) = 1;
4728 DECL_CONTEXT (decl
) = fndecl
;
4729 DECL_RESULT (fndecl
) = decl
;
4733 current_function_decl
= fndecl
;
4735 rest_of_decl_compilation (fndecl
, 1, 0);
4737 make_decl_rtl (fndecl
);
4739 init_function_start (fndecl
);
4743 for (; gfc_static_ctors
; gfc_static_ctors
= TREE_CHAIN (gfc_static_ctors
))
4745 tmp
= build_call_expr_loc (input_location
,
4746 TREE_VALUE (gfc_static_ctors
), 0);
4747 DECL_SAVED_TREE (fndecl
) = build_stmt (input_location
, EXPR_STMT
, tmp
);
4753 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
4754 DECL_SAVED_TREE (fndecl
)
4755 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (fndecl
),
4756 DECL_INITIAL (fndecl
));
4758 free_after_parsing (cfun
);
4759 free_after_compilation (cfun
);
4761 tree_rest_of_compilation (fndecl
);
4763 current_function_decl
= NULL_TREE
;
4767 /* Translates a BLOCK DATA program unit. This means emitting the
4768 commons contained therein plus their initializations. We also emit
4769 a globally visible symbol to make sure that each BLOCK DATA program
4770 unit remains unique. */
4773 gfc_generate_block_data (gfc_namespace
* ns
)
4778 /* Tell the backend the source location of the block data. */
4780 gfc_set_backend_locus (&ns
->proc_name
->declared_at
);
4782 gfc_set_backend_locus (&gfc_current_locus
);
4784 /* Process the DATA statements. */
4785 gfc_trans_common (ns
);
4787 /* Create a global symbol with the mane of the block data. This is to
4788 generate linker errors if the same name is used twice. It is never
4791 id
= gfc_sym_mangled_function_id (ns
->proc_name
);
4793 id
= get_identifier ("__BLOCK_DATA__");
4795 decl
= build_decl (input_location
,
4796 VAR_DECL
, id
, gfc_array_index_type
);
4797 TREE_PUBLIC (decl
) = 1;
4798 TREE_STATIC (decl
) = 1;
4799 DECL_IGNORED_P (decl
) = 1;
4802 rest_of_decl_compilation (decl
, 1, 0);
4806 /* Process the local variables of a BLOCK construct. */
4809 gfc_process_block_locals (gfc_namespace
* ns
)
4813 gcc_assert (saved_local_decls
== NULL_TREE
);
4814 generate_local_vars (ns
);
4816 decl
= saved_local_decls
;
4821 next
= DECL_CHAIN (decl
);
4822 DECL_CHAIN (decl
) = NULL_TREE
;
4826 saved_local_decls
= NULL_TREE
;
4830 #include "gt-fortran-trans-decl.h"