1 /* Backend function setup
2 Copyright (C) 2002-2018 Free Software Foundation, Inc.
3 Contributed by Paul Brook
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* trans-decl.c -- Handling of backend function and variable decls, etc */
25 #include "coretypes.h"
30 #include "gimple-expr.h" /* For create_tmp_var_raw. */
32 #include "stringpool.h"
34 #include "fold-const.h"
35 #include "stor-layout.h"
39 #include "toplev.h" /* For announce_function. */
41 #include "constructor.h"
42 #include "trans-types.h"
43 #include "trans-array.h"
44 #include "trans-const.h"
45 /* Only for gfc_trans_code. Shouldn't need to include this. */
46 #include "trans-stmt.h"
47 #include "gomp-constants.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 hash_set
<tree
> *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
;
79 /* The currently processed module. */
80 static struct module_htab_entry
*cur_module
;
82 /* With -fcoarray=lib: For generating the registering call
83 of static coarrays. */
84 static bool has_coarray_vars
;
85 static stmtblock_t caf_init_block
;
88 /* List of static constructor functions. */
90 tree gfc_static_ctors
;
93 /* Whether we've seen a symbol from an IEEE module in the namespace. */
94 static int seen_ieee_symbol
;
96 /* Function declarations for builtin library functions. */
98 tree gfor_fndecl_pause_numeric
;
99 tree gfor_fndecl_pause_string
;
100 tree gfor_fndecl_stop_numeric
;
101 tree gfor_fndecl_stop_string
;
102 tree gfor_fndecl_error_stop_numeric
;
103 tree gfor_fndecl_error_stop_string
;
104 tree gfor_fndecl_runtime_error
;
105 tree gfor_fndecl_runtime_error_at
;
106 tree gfor_fndecl_runtime_warning_at
;
107 tree gfor_fndecl_os_error
;
108 tree gfor_fndecl_generate_error
;
109 tree gfor_fndecl_set_args
;
110 tree gfor_fndecl_set_fpe
;
111 tree gfor_fndecl_set_options
;
112 tree gfor_fndecl_set_convert
;
113 tree gfor_fndecl_set_record_marker
;
114 tree gfor_fndecl_set_max_subrecord_length
;
115 tree gfor_fndecl_ctime
;
116 tree gfor_fndecl_fdate
;
117 tree gfor_fndecl_ttynam
;
118 tree gfor_fndecl_in_pack
;
119 tree gfor_fndecl_in_unpack
;
120 tree gfor_fndecl_associated
;
121 tree gfor_fndecl_system_clock4
;
122 tree gfor_fndecl_system_clock8
;
123 tree gfor_fndecl_ieee_procedure_entry
;
124 tree gfor_fndecl_ieee_procedure_exit
;
127 /* Coarray run-time library function decls. */
128 tree gfor_fndecl_caf_init
;
129 tree gfor_fndecl_caf_finalize
;
130 tree gfor_fndecl_caf_this_image
;
131 tree gfor_fndecl_caf_num_images
;
132 tree gfor_fndecl_caf_register
;
133 tree gfor_fndecl_caf_deregister
;
134 tree gfor_fndecl_caf_get
;
135 tree gfor_fndecl_caf_send
;
136 tree gfor_fndecl_caf_sendget
;
137 tree gfor_fndecl_caf_get_by_ref
;
138 tree gfor_fndecl_caf_send_by_ref
;
139 tree gfor_fndecl_caf_sendget_by_ref
;
140 tree gfor_fndecl_caf_sync_all
;
141 tree gfor_fndecl_caf_sync_memory
;
142 tree gfor_fndecl_caf_sync_images
;
143 tree gfor_fndecl_caf_stop_str
;
144 tree gfor_fndecl_caf_stop_numeric
;
145 tree gfor_fndecl_caf_error_stop
;
146 tree gfor_fndecl_caf_error_stop_str
;
147 tree gfor_fndecl_caf_atomic_def
;
148 tree gfor_fndecl_caf_atomic_ref
;
149 tree gfor_fndecl_caf_atomic_cas
;
150 tree gfor_fndecl_caf_atomic_op
;
151 tree gfor_fndecl_caf_lock
;
152 tree gfor_fndecl_caf_unlock
;
153 tree gfor_fndecl_caf_event_post
;
154 tree gfor_fndecl_caf_event_wait
;
155 tree gfor_fndecl_caf_event_query
;
156 tree gfor_fndecl_caf_fail_image
;
157 tree gfor_fndecl_caf_failed_images
;
158 tree gfor_fndecl_caf_image_status
;
159 tree gfor_fndecl_caf_stopped_images
;
160 tree gfor_fndecl_caf_form_team
;
161 tree gfor_fndecl_caf_change_team
;
162 tree gfor_fndecl_caf_end_team
;
163 tree gfor_fndecl_caf_sync_team
;
164 tree gfor_fndecl_caf_get_team
;
165 tree gfor_fndecl_caf_team_number
;
166 tree gfor_fndecl_co_broadcast
;
167 tree gfor_fndecl_co_max
;
168 tree gfor_fndecl_co_min
;
169 tree gfor_fndecl_co_reduce
;
170 tree gfor_fndecl_co_sum
;
171 tree gfor_fndecl_caf_is_present
;
174 /* Math functions. Many other math functions are handled in
175 trans-intrinsic.c. */
177 gfc_powdecl_list gfor_fndecl_math_powi
[4][3];
178 tree gfor_fndecl_math_ishftc4
;
179 tree gfor_fndecl_math_ishftc8
;
180 tree gfor_fndecl_math_ishftc16
;
183 /* String functions. */
185 tree gfor_fndecl_compare_string
;
186 tree gfor_fndecl_concat_string
;
187 tree gfor_fndecl_string_len_trim
;
188 tree gfor_fndecl_string_index
;
189 tree gfor_fndecl_string_scan
;
190 tree gfor_fndecl_string_verify
;
191 tree gfor_fndecl_string_trim
;
192 tree gfor_fndecl_string_minmax
;
193 tree gfor_fndecl_adjustl
;
194 tree gfor_fndecl_adjustr
;
195 tree gfor_fndecl_select_string
;
196 tree gfor_fndecl_compare_string_char4
;
197 tree gfor_fndecl_concat_string_char4
;
198 tree gfor_fndecl_string_len_trim_char4
;
199 tree gfor_fndecl_string_index_char4
;
200 tree gfor_fndecl_string_scan_char4
;
201 tree gfor_fndecl_string_verify_char4
;
202 tree gfor_fndecl_string_trim_char4
;
203 tree gfor_fndecl_string_minmax_char4
;
204 tree gfor_fndecl_adjustl_char4
;
205 tree gfor_fndecl_adjustr_char4
;
206 tree gfor_fndecl_select_string_char4
;
209 /* Conversion between character kinds. */
210 tree gfor_fndecl_convert_char1_to_char4
;
211 tree gfor_fndecl_convert_char4_to_char1
;
214 /* Other misc. runtime library functions. */
215 tree gfor_fndecl_size0
;
216 tree gfor_fndecl_size1
;
217 tree gfor_fndecl_iargc
;
219 /* Intrinsic functions implemented in Fortran. */
220 tree gfor_fndecl_sc_kind
;
221 tree gfor_fndecl_si_kind
;
222 tree gfor_fndecl_sr_kind
;
224 /* BLAS gemm functions. */
225 tree gfor_fndecl_sgemm
;
226 tree gfor_fndecl_dgemm
;
227 tree gfor_fndecl_cgemm
;
228 tree gfor_fndecl_zgemm
;
232 gfc_add_decl_to_parent_function (tree decl
)
235 DECL_CONTEXT (decl
) = DECL_CONTEXT (current_function_decl
);
236 DECL_NONLOCAL (decl
) = 1;
237 DECL_CHAIN (decl
) = saved_parent_function_decls
;
238 saved_parent_function_decls
= decl
;
242 gfc_add_decl_to_function (tree decl
)
245 TREE_USED (decl
) = 1;
246 DECL_CONTEXT (decl
) = current_function_decl
;
247 DECL_CHAIN (decl
) = saved_function_decls
;
248 saved_function_decls
= decl
;
252 add_decl_as_local (tree decl
)
255 TREE_USED (decl
) = 1;
256 DECL_CONTEXT (decl
) = current_function_decl
;
257 DECL_CHAIN (decl
) = saved_local_decls
;
258 saved_local_decls
= decl
;
262 /* Build a backend label declaration. Set TREE_USED for named labels.
263 The context of the label is always the current_function_decl. All
264 labels are marked artificial. */
267 gfc_build_label_decl (tree label_id
)
269 /* 2^32 temporaries should be enough. */
270 static unsigned int tmp_num
= 1;
274 if (label_id
== NULL_TREE
)
276 /* Build an internal label name. */
277 ASM_FORMAT_PRIVATE_NAME (label_name
, "L", tmp_num
++);
278 label_id
= get_identifier (label_name
);
283 /* Build the LABEL_DECL node. Labels have no type. */
284 label_decl
= build_decl (input_location
,
285 LABEL_DECL
, label_id
, void_type_node
);
286 DECL_CONTEXT (label_decl
) = current_function_decl
;
287 SET_DECL_MODE (label_decl
, VOIDmode
);
289 /* We always define the label as used, even if the original source
290 file never references the label. We don't want all kinds of
291 spurious warnings for old-style Fortran code with too many
293 TREE_USED (label_decl
) = 1;
295 DECL_ARTIFICIAL (label_decl
) = 1;
300 /* Set the backend source location of a decl. */
303 gfc_set_decl_location (tree decl
, locus
* loc
)
305 DECL_SOURCE_LOCATION (decl
) = loc
->lb
->location
;
309 /* Return the backend label declaration for a given label structure,
310 or create it if it doesn't exist yet. */
313 gfc_get_label_decl (gfc_st_label
* lp
)
315 if (lp
->backend_decl
)
316 return lp
->backend_decl
;
319 char label_name
[GFC_MAX_SYMBOL_LEN
+ 1];
322 /* Validate the label declaration from the front end. */
323 gcc_assert (lp
!= NULL
&& lp
->value
<= MAX_LABEL_VALUE
);
325 /* Build a mangled name for the label. */
326 sprintf (label_name
, "__label_%.6d", lp
->value
);
328 /* Build the LABEL_DECL node. */
329 label_decl
= gfc_build_label_decl (get_identifier (label_name
));
331 /* Tell the debugger where the label came from. */
332 if (lp
->value
<= MAX_LABEL_VALUE
) /* An internal label. */
333 gfc_set_decl_location (label_decl
, &lp
->where
);
335 DECL_ARTIFICIAL (label_decl
) = 1;
337 /* Store the label in the label list and return the LABEL_DECL. */
338 lp
->backend_decl
= label_decl
;
344 /* Convert a gfc_symbol to an identifier of the same name. */
347 gfc_sym_identifier (gfc_symbol
* sym
)
349 if (sym
->attr
.is_main_program
&& strcmp (sym
->name
, "main") == 0)
350 return (get_identifier ("MAIN__"));
352 return (get_identifier (sym
->name
));
356 /* Construct mangled name from symbol name. */
359 gfc_sym_mangled_identifier (gfc_symbol
* sym
)
361 char name
[GFC_MAX_MANGLED_SYMBOL_LEN
+ 1];
363 /* Prevent the mangling of identifiers that have an assigned
364 binding label (mainly those that are bind(c)). */
365 if (sym
->attr
.is_bind_c
== 1 && sym
->binding_label
)
366 return get_identifier (sym
->binding_label
);
368 if (!sym
->fn_result_spec
)
370 if (sym
->module
== NULL
)
371 return gfc_sym_identifier (sym
);
374 snprintf (name
, sizeof name
, "__%s_MOD_%s", sym
->module
, sym
->name
);
375 return get_identifier (name
);
380 /* This is an entity that is actually local to a module procedure
381 that appears in the result specification expression. Since
382 sym->module will be a zero length string, we use ns->proc_name
384 if (sym
->ns
->proc_name
&& sym
->ns
->proc_name
->module
)
386 snprintf (name
, sizeof name
, "__%s_MOD__%s_PROC_%s",
387 sym
->ns
->proc_name
->module
,
388 sym
->ns
->proc_name
->name
,
390 return get_identifier (name
);
394 snprintf (name
, sizeof name
, "__%s_PROC_%s",
395 sym
->ns
->proc_name
->name
, sym
->name
);
396 return get_identifier (name
);
402 /* Construct mangled function name from symbol name. */
405 gfc_sym_mangled_function_id (gfc_symbol
* sym
)
408 char name
[GFC_MAX_MANGLED_SYMBOL_LEN
+ 1];
410 /* It may be possible to simply use the binding label if it's
411 provided, and remove the other checks. Then we could use it
412 for other things if we wished. */
413 if ((sym
->attr
.is_bind_c
== 1 || sym
->attr
.is_iso_c
== 1) &&
415 /* use the binding label rather than the mangled name */
416 return get_identifier (sym
->binding_label
);
418 if ((sym
->module
== NULL
|| sym
->attr
.proc
== PROC_EXTERNAL
419 || (sym
->module
!= NULL
&& (sym
->attr
.external
420 || sym
->attr
.if_source
== IFSRC_IFBODY
)))
421 && !sym
->attr
.module_procedure
)
423 /* Main program is mangled into MAIN__. */
424 if (sym
->attr
.is_main_program
)
425 return get_identifier ("MAIN__");
427 /* Intrinsic procedures are never mangled. */
428 if (sym
->attr
.proc
== PROC_INTRINSIC
)
429 return get_identifier (sym
->name
);
431 if (flag_underscoring
)
433 has_underscore
= strchr (sym
->name
, '_') != 0;
434 if (flag_second_underscore
&& has_underscore
)
435 snprintf (name
, sizeof name
, "%s__", sym
->name
);
437 snprintf (name
, sizeof name
, "%s_", sym
->name
);
438 return get_identifier (name
);
441 return get_identifier (sym
->name
);
445 snprintf (name
, sizeof name
, "__%s_MOD_%s", sym
->module
, sym
->name
);
446 return get_identifier (name
);
452 gfc_set_decl_assembler_name (tree decl
, tree name
)
454 tree target_mangled
= targetm
.mangle_decl_assembler_name (decl
, name
);
455 SET_DECL_ASSEMBLER_NAME (decl
, target_mangled
);
459 /* Returns true if a variable of specified size should go on the stack. */
462 gfc_can_put_var_on_stack (tree size
)
464 unsigned HOST_WIDE_INT low
;
466 if (!INTEGER_CST_P (size
))
469 if (flag_max_stack_var_size
< 0)
472 if (!tree_fits_uhwi_p (size
))
475 low
= TREE_INT_CST_LOW (size
);
476 if (low
> (unsigned HOST_WIDE_INT
) flag_max_stack_var_size
)
479 /* TODO: Set a per-function stack size limit. */
485 /* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
486 an expression involving its corresponding pointer. There are
487 2 cases; one for variable size arrays, and one for everything else,
488 because variable-sized arrays require one fewer level of
492 gfc_finish_cray_pointee (tree decl
, gfc_symbol
*sym
)
494 tree ptr_decl
= gfc_get_symbol_decl (sym
->cp_pointer
);
497 /* Parameters need to be dereferenced. */
498 if (sym
->cp_pointer
->attr
.dummy
)
499 ptr_decl
= build_fold_indirect_ref_loc (input_location
,
502 /* Check to see if we're dealing with a variable-sized array. */
503 if (sym
->attr
.dimension
504 && TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
)
506 /* These decls will be dereferenced later, so we don't dereference
508 value
= convert (TREE_TYPE (decl
), ptr_decl
);
512 ptr_decl
= convert (build_pointer_type (TREE_TYPE (decl
)),
514 value
= build_fold_indirect_ref_loc (input_location
,
518 SET_DECL_VALUE_EXPR (decl
, value
);
519 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
520 GFC_DECL_CRAY_POINTEE (decl
) = 1;
524 /* Finish processing of a declaration without an initial value. */
527 gfc_finish_decl (tree decl
)
529 gcc_assert (TREE_CODE (decl
) == PARM_DECL
530 || DECL_INITIAL (decl
) == NULL_TREE
);
535 if (DECL_SIZE (decl
) == NULL_TREE
536 && TYPE_SIZE (TREE_TYPE (decl
)) != NULL_TREE
)
537 layout_decl (decl
, 0);
539 /* A few consistency checks. */
540 /* A static variable with an incomplete type is an error if it is
541 initialized. Also if it is not file scope. Otherwise, let it
542 through, but if it is not `extern' then it may cause an error
544 /* An automatic variable with an incomplete type is an error. */
546 /* We should know the storage size. */
547 gcc_assert (DECL_SIZE (decl
) != NULL_TREE
548 || (TREE_STATIC (decl
)
549 ? (!DECL_INITIAL (decl
) || !DECL_CONTEXT (decl
))
550 : DECL_EXTERNAL (decl
)));
552 /* The storage size should be constant. */
553 gcc_assert ((!DECL_EXTERNAL (decl
) && !TREE_STATIC (decl
))
555 || TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
);
559 /* Handle setting of GFC_DECL_SCALAR* on DECL. */
562 gfc_finish_decl_attrs (tree decl
, symbol_attribute
*attr
)
564 if (!attr
->dimension
&& !attr
->codimension
)
566 /* Handle scalar allocatable variables. */
567 if (attr
->allocatable
)
569 gfc_allocate_lang_decl (decl
);
570 GFC_DECL_SCALAR_ALLOCATABLE (decl
) = 1;
572 /* Handle scalar pointer variables. */
575 gfc_allocate_lang_decl (decl
);
576 GFC_DECL_SCALAR_POINTER (decl
) = 1;
582 /* Apply symbol attributes to a variable, and add it to the function scope. */
585 gfc_finish_var_decl (tree decl
, gfc_symbol
* sym
)
589 /* Set DECL_VALUE_EXPR for Cray Pointees. */
590 if (sym
->attr
.cray_pointee
)
591 gfc_finish_cray_pointee (decl
, sym
);
593 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
594 This is the equivalent of the TARGET variables.
595 We also need to set this if the variable is passed by reference in a
597 if (sym
->attr
.target
)
598 TREE_ADDRESSABLE (decl
) = 1;
600 /* If it wasn't used we wouldn't be getting it. */
601 TREE_USED (decl
) = 1;
603 if (sym
->attr
.flavor
== FL_PARAMETER
604 && (sym
->attr
.dimension
|| sym
->ts
.type
== BT_DERIVED
))
605 TREE_READONLY (decl
) = 1;
607 /* Chain this decl to the pending declarations. Don't do pushdecl()
608 because this would add them to the current scope rather than the
610 if (current_function_decl
!= NULL_TREE
)
612 if (sym
->ns
->proc_name
->backend_decl
== current_function_decl
613 || sym
->result
== sym
)
614 gfc_add_decl_to_function (decl
);
615 else if (sym
->ns
->proc_name
->attr
.flavor
== FL_LABEL
)
616 /* This is a BLOCK construct. */
617 add_decl_as_local (decl
);
619 gfc_add_decl_to_parent_function (decl
);
622 if (sym
->attr
.cray_pointee
)
625 if(sym
->attr
.is_bind_c
== 1 && sym
->binding_label
)
627 /* We need to put variables that are bind(c) into the common
628 segment of the object file, because this is what C would do.
629 gfortran would typically put them in either the BSS or
630 initialized data segments, and only mark them as common if
631 they were part of common blocks. However, if they are not put
632 into common space, then C cannot initialize global Fortran
633 variables that it interoperates with and the draft says that
634 either Fortran or C should be able to initialize it (but not
635 both, of course.) (J3/04-007, section 15.3). */
636 TREE_PUBLIC(decl
) = 1;
637 DECL_COMMON(decl
) = 1;
638 if (sym
->attr
.access
== ACCESS_PRIVATE
&& !sym
->attr
.public_used
)
640 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
641 DECL_VISIBILITY_SPECIFIED (decl
) = true;
645 /* If a variable is USE associated, it's always external. */
646 if (sym
->attr
.use_assoc
|| sym
->attr
.used_in_submodule
)
648 DECL_EXTERNAL (decl
) = 1;
649 TREE_PUBLIC (decl
) = 1;
651 else if (sym
->fn_result_spec
&& !sym
->ns
->proc_name
->module
)
654 if (sym
->ns
->proc_name
->attr
.if_source
!= IFSRC_DECL
)
655 DECL_EXTERNAL (decl
) = 1;
657 TREE_STATIC (decl
) = 1;
659 TREE_PUBLIC (decl
) = 1;
661 else if (sym
->module
&& !sym
->attr
.result
&& !sym
->attr
.dummy
)
663 /* TODO: Don't set sym->module for result or dummy variables. */
664 gcc_assert (current_function_decl
== NULL_TREE
|| sym
->result
== sym
);
666 TREE_PUBLIC (decl
) = 1;
667 TREE_STATIC (decl
) = 1;
668 if (sym
->attr
.access
== ACCESS_PRIVATE
&& !sym
->attr
.public_used
)
670 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
671 DECL_VISIBILITY_SPECIFIED (decl
) = true;
675 /* Derived types are a bit peculiar because of the possibility of
676 a default initializer; this must be applied each time the variable
677 comes into scope it therefore need not be static. These variables
678 are SAVE_NONE but have an initializer. Otherwise explicitly
679 initialized variables are SAVE_IMPLICIT and explicitly saved are
681 if (!sym
->attr
.use_assoc
682 && (sym
->attr
.save
!= SAVE_NONE
|| sym
->attr
.data
683 || (sym
->value
&& sym
->ns
->proc_name
->attr
.is_main_program
)
684 || (flag_coarray
== GFC_FCOARRAY_LIB
685 && sym
->attr
.codimension
&& !sym
->attr
.allocatable
)))
686 TREE_STATIC (decl
) = 1;
688 /* If derived-type variables with DTIO procedures are not made static
689 some bits of code referencing them get optimized away.
690 TODO Understand why this is so and fix it. */
691 if (!sym
->attr
.use_assoc
692 && ((sym
->ts
.type
== BT_DERIVED
693 && sym
->ts
.u
.derived
->attr
.has_dtio_procs
)
694 || (sym
->ts
.type
== BT_CLASS
695 && CLASS_DATA (sym
)->ts
.u
.derived
->attr
.has_dtio_procs
)))
696 TREE_STATIC (decl
) = 1;
698 if (sym
->attr
.volatile_
)
700 TREE_THIS_VOLATILE (decl
) = 1;
701 TREE_SIDE_EFFECTS (decl
) = 1;
702 new_type
= build_qualified_type (TREE_TYPE (decl
), TYPE_QUAL_VOLATILE
);
703 TREE_TYPE (decl
) = new_type
;
706 /* Keep variables larger than max-stack-var-size off stack. */
707 if (!sym
->ns
->proc_name
->attr
.recursive
&& !sym
->attr
.automatic
708 && INTEGER_CST_P (DECL_SIZE_UNIT (decl
))
709 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl
))
710 /* Put variable length auto array pointers always into stack. */
711 && (TREE_CODE (TREE_TYPE (decl
)) != POINTER_TYPE
712 || sym
->attr
.dimension
== 0
713 || sym
->as
->type
!= AS_EXPLICIT
715 || sym
->attr
.allocatable
)
716 && !DECL_ARTIFICIAL (decl
))
718 TREE_STATIC (decl
) = 1;
720 /* Because the size of this variable isn't known until now, we may have
721 greedily added an initializer to this variable (in build_init_assign)
722 even though the max-stack-var-size indicates the variable should be
723 static. Therefore we rip out the automatic initializer here and
724 replace it with a static one. */
725 gfc_symtree
*st
= gfc_find_symtree (sym
->ns
->sym_root
, sym
->name
);
726 gfc_code
*prev
= NULL
;
727 gfc_code
*code
= sym
->ns
->code
;
728 while (code
&& code
->op
== EXEC_INIT_ASSIGN
)
730 /* Look for an initializer meant for this symbol. */
731 if (code
->expr1
->symtree
== st
)
734 prev
->next
= code
->next
;
736 sym
->ns
->code
= code
->next
;
744 if (code
&& code
->op
== EXEC_INIT_ASSIGN
)
746 /* Keep the init expression for a static initializer. */
747 sym
->value
= code
->expr2
;
748 /* Cleanup the defunct code object, without freeing the init expr. */
750 gfc_free_statement (code
);
755 /* Handle threadprivate variables. */
756 if (sym
->attr
.threadprivate
757 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
758 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
760 gfc_finish_decl_attrs (decl
, &sym
->attr
);
764 /* Allocate the lang-specific part of a decl. */
767 gfc_allocate_lang_decl (tree decl
)
769 if (DECL_LANG_SPECIFIC (decl
) == NULL
)
770 DECL_LANG_SPECIFIC (decl
) = ggc_cleared_alloc
<struct lang_decl
> ();
773 /* Remember a symbol to generate initialization/cleanup code at function
777 gfc_defer_symbol_init (gfc_symbol
* sym
)
783 /* Don't add a symbol twice. */
787 last
= head
= sym
->ns
->proc_name
;
790 /* Make sure that setup code for dummy variables which are used in the
791 setup of other variables is generated first. */
794 /* Find the first dummy arg seen after us, or the first non-dummy arg.
795 This is a circular list, so don't go past the head. */
797 && (!p
->attr
.dummy
|| p
->dummy_order
> sym
->dummy_order
))
803 /* Insert in between last and p. */
809 /* Used in gfc_get_symbol_decl and gfc_get_derived_type to obtain the
810 backend_decl for a module symbol, if it all ready exists. If the
811 module gsymbol does not exist, it is created. If the symbol does
812 not exist, it is added to the gsymbol namespace. Returns true if
813 an existing backend_decl is found. */
816 gfc_get_module_backend_decl (gfc_symbol
*sym
)
822 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->module
);
824 if (!gsym
|| (gsym
->ns
&& gsym
->type
== GSYM_MODULE
))
829 /* Check for a symbol with the same name. */
831 gfc_find_symbol (sym
->name
, gsym
->ns
, 0, &s
);
837 gsym
= gfc_get_gsymbol (sym
->module
);
838 gsym
->type
= GSYM_MODULE
;
839 gsym
->ns
= gfc_get_namespace (NULL
, 0);
842 st
= gfc_new_symtree (&gsym
->ns
->sym_root
, sym
->name
);
846 else if (gfc_fl_struct (sym
->attr
.flavor
))
848 if (s
&& s
->attr
.flavor
== FL_PROCEDURE
)
851 gcc_assert (s
->attr
.generic
);
852 for (intr
= s
->generic
; intr
; intr
= intr
->next
)
853 if (gfc_fl_struct (intr
->sym
->attr
.flavor
))
860 /* Normally we can assume that s is a derived-type symbol since it
861 shares a name with the derived-type sym. However if sym is a
862 STRUCTURE, it may in fact share a name with any other basic type
863 variable. If s is in fact of derived type then we can continue
864 looking for a duplicate type declaration. */
865 if (sym
->attr
.flavor
== FL_STRUCT
&& s
->ts
.type
== BT_DERIVED
)
870 if (gfc_fl_struct (s
->attr
.flavor
) && !s
->backend_decl
)
872 if (s
->attr
.flavor
== FL_UNION
)
873 s
->backend_decl
= gfc_get_union_type (s
);
875 s
->backend_decl
= gfc_get_derived_type (s
);
877 gfc_copy_dt_decls_ifequal (s
, sym
, true);
880 else if (s
->backend_decl
)
882 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
883 gfc_copy_dt_decls_ifequal (s
->ts
.u
.derived
, sym
->ts
.u
.derived
,
885 else if (sym
->ts
.type
== BT_CHARACTER
)
886 sym
->ts
.u
.cl
->backend_decl
= s
->ts
.u
.cl
->backend_decl
;
887 sym
->backend_decl
= s
->backend_decl
;
895 /* Create an array index type variable with function scope. */
898 create_index_var (const char * pfx
, int nest
)
902 decl
= gfc_create_var_np (gfc_array_index_type
, pfx
);
904 gfc_add_decl_to_parent_function (decl
);
906 gfc_add_decl_to_function (decl
);
911 /* Create variables to hold all the non-constant bits of info for a
912 descriptorless array. Remember these in the lang-specific part of the
916 gfc_build_qualified_array (tree decl
, gfc_symbol
* sym
)
921 gfc_namespace
* procns
;
922 symbol_attribute
*array_attr
;
924 bool is_classarray
= IS_CLASS_ARRAY (sym
);
926 type
= TREE_TYPE (decl
);
927 array_attr
= is_classarray
? &CLASS_DATA (sym
)->attr
: &sym
->attr
;
928 as
= is_classarray
? CLASS_DATA (sym
)->as
: sym
->as
;
930 /* We just use the descriptor, if there is one. */
931 if (GFC_DESCRIPTOR_TYPE_P (type
))
934 gcc_assert (GFC_ARRAY_TYPE_P (type
));
935 procns
= gfc_find_proc_namespace (sym
->ns
);
936 nest
= (procns
->proc_name
->backend_decl
!= current_function_decl
)
937 && !sym
->attr
.contained
;
939 if (array_attr
->codimension
&& flag_coarray
== GFC_FCOARRAY_LIB
940 && as
->type
!= AS_ASSUMED_SHAPE
941 && GFC_TYPE_ARRAY_CAF_TOKEN (type
) == NULL_TREE
)
944 tree token_type
= build_qualified_type (pvoid_type_node
,
947 if (sym
->module
&& (sym
->attr
.use_assoc
948 || sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
))
951 = get_identifier (gfc_get_string (GFC_PREFIX ("caf_token%s"),
952 IDENTIFIER_POINTER (gfc_sym_mangled_identifier (sym
))));
953 token
= build_decl (DECL_SOURCE_LOCATION (decl
), VAR_DECL
, token_name
,
955 if (sym
->attr
.use_assoc
)
956 DECL_EXTERNAL (token
) = 1;
958 TREE_STATIC (token
) = 1;
960 TREE_PUBLIC (token
) = 1;
962 if (sym
->attr
.access
== ACCESS_PRIVATE
&& !sym
->attr
.public_used
)
964 DECL_VISIBILITY (token
) = VISIBILITY_HIDDEN
;
965 DECL_VISIBILITY_SPECIFIED (token
) = true;
970 token
= gfc_create_var_np (token_type
, "caf_token");
971 TREE_STATIC (token
) = 1;
974 GFC_TYPE_ARRAY_CAF_TOKEN (type
) = token
;
975 DECL_ARTIFICIAL (token
) = 1;
976 DECL_NONALIASED (token
) = 1;
978 if (sym
->module
&& !sym
->attr
.use_assoc
)
981 DECL_CONTEXT (token
) = sym
->ns
->proc_name
->backend_decl
;
982 gfc_module_add_decl (cur_module
, token
);
984 else if (sym
->attr
.host_assoc
985 && TREE_CODE (DECL_CONTEXT (current_function_decl
))
986 != TRANSLATION_UNIT_DECL
)
987 gfc_add_decl_to_parent_function (token
);
989 gfc_add_decl_to_function (token
);
992 for (dim
= 0; dim
< GFC_TYPE_ARRAY_RANK (type
); dim
++)
994 if (GFC_TYPE_ARRAY_LBOUND (type
, dim
) == NULL_TREE
)
996 GFC_TYPE_ARRAY_LBOUND (type
, dim
) = create_index_var ("lbound", nest
);
997 TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type
, dim
)) = 1;
999 /* Don't try to use the unknown bound for assumed shape arrays. */
1000 if (GFC_TYPE_ARRAY_UBOUND (type
, dim
) == NULL_TREE
1001 && (as
->type
!= AS_ASSUMED_SIZE
1002 || dim
< GFC_TYPE_ARRAY_RANK (type
) - 1))
1004 GFC_TYPE_ARRAY_UBOUND (type
, dim
) = create_index_var ("ubound", nest
);
1005 TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type
, dim
)) = 1;
1008 if (GFC_TYPE_ARRAY_STRIDE (type
, dim
) == NULL_TREE
)
1010 GFC_TYPE_ARRAY_STRIDE (type
, dim
) = create_index_var ("stride", nest
);
1011 TREE_NO_WARNING (GFC_TYPE_ARRAY_STRIDE (type
, dim
)) = 1;
1014 for (dim
= GFC_TYPE_ARRAY_RANK (type
);
1015 dim
< GFC_TYPE_ARRAY_RANK (type
) + GFC_TYPE_ARRAY_CORANK (type
); dim
++)
1017 if (GFC_TYPE_ARRAY_LBOUND (type
, dim
) == NULL_TREE
)
1019 GFC_TYPE_ARRAY_LBOUND (type
, dim
) = create_index_var ("lbound", nest
);
1020 TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type
, dim
)) = 1;
1022 /* Don't try to use the unknown ubound for the last coarray dimension. */
1023 if (GFC_TYPE_ARRAY_UBOUND (type
, dim
) == NULL_TREE
1024 && dim
< GFC_TYPE_ARRAY_RANK (type
) + GFC_TYPE_ARRAY_CORANK (type
) - 1)
1026 GFC_TYPE_ARRAY_UBOUND (type
, dim
) = create_index_var ("ubound", nest
);
1027 TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type
, dim
)) = 1;
1030 if (GFC_TYPE_ARRAY_OFFSET (type
) == NULL_TREE
)
1032 GFC_TYPE_ARRAY_OFFSET (type
) = gfc_create_var_np (gfc_array_index_type
,
1034 TREE_NO_WARNING (GFC_TYPE_ARRAY_OFFSET (type
)) = 1;
1037 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type
));
1039 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type
));
1042 if (GFC_TYPE_ARRAY_SIZE (type
) == NULL_TREE
1043 && as
->type
!= AS_ASSUMED_SIZE
)
1045 GFC_TYPE_ARRAY_SIZE (type
) = create_index_var ("size", nest
);
1046 TREE_NO_WARNING (GFC_TYPE_ARRAY_SIZE (type
)) = 1;
1049 if (POINTER_TYPE_P (type
))
1051 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type
)));
1052 gcc_assert (TYPE_LANG_SPECIFIC (type
)
1053 == TYPE_LANG_SPECIFIC (TREE_TYPE (type
)));
1054 type
= TREE_TYPE (type
);
1057 if (! COMPLETE_TYPE_P (type
) && GFC_TYPE_ARRAY_SIZE (type
))
1061 size
= fold_build2_loc (input_location
, MINUS_EXPR
, gfc_array_index_type
,
1062 GFC_TYPE_ARRAY_SIZE (type
), gfc_index_one_node
);
1063 range
= build_range_type (gfc_array_index_type
, gfc_index_zero_node
,
1065 TYPE_DOMAIN (type
) = range
;
1069 if (TYPE_NAME (type
) != NULL_TREE
&& as
->rank
> 0
1070 && GFC_TYPE_ARRAY_UBOUND (type
, as
->rank
- 1) != NULL_TREE
1071 && VAR_P (GFC_TYPE_ARRAY_UBOUND (type
, as
->rank
- 1)))
1073 tree gtype
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
1075 for (dim
= 0; dim
< as
->rank
- 1; dim
++)
1077 gcc_assert (TREE_CODE (gtype
) == ARRAY_TYPE
);
1078 gtype
= TREE_TYPE (gtype
);
1080 gcc_assert (TREE_CODE (gtype
) == ARRAY_TYPE
);
1081 if (TYPE_MAX_VALUE (TYPE_DOMAIN (gtype
)) == NULL
)
1082 TYPE_NAME (type
) = NULL_TREE
;
1085 if (TYPE_NAME (type
) == NULL_TREE
)
1087 tree gtype
= TREE_TYPE (type
), rtype
, type_decl
;
1089 for (dim
= as
->rank
- 1; dim
>= 0; dim
--)
1091 tree lbound
, ubound
;
1092 lbound
= GFC_TYPE_ARRAY_LBOUND (type
, dim
);
1093 ubound
= GFC_TYPE_ARRAY_UBOUND (type
, dim
);
1094 rtype
= build_range_type (gfc_array_index_type
, lbound
, ubound
);
1095 gtype
= build_array_type (gtype
, rtype
);
1096 /* Ensure the bound variables aren't optimized out at -O0.
1097 For -O1 and above they often will be optimized out, but
1098 can be tracked by VTA. Also set DECL_NAMELESS, so that
1099 the artificial lbound.N or ubound.N DECL_NAME doesn't
1100 end up in debug info. */
1103 && DECL_ARTIFICIAL (lbound
)
1104 && DECL_IGNORED_P (lbound
))
1106 if (DECL_NAME (lbound
)
1107 && strstr (IDENTIFIER_POINTER (DECL_NAME (lbound
)),
1109 DECL_NAMELESS (lbound
) = 1;
1110 DECL_IGNORED_P (lbound
) = 0;
1114 && DECL_ARTIFICIAL (ubound
)
1115 && DECL_IGNORED_P (ubound
))
1117 if (DECL_NAME (ubound
)
1118 && strstr (IDENTIFIER_POINTER (DECL_NAME (ubound
)),
1120 DECL_NAMELESS (ubound
) = 1;
1121 DECL_IGNORED_P (ubound
) = 0;
1124 TYPE_NAME (type
) = type_decl
= build_decl (input_location
,
1125 TYPE_DECL
, NULL
, gtype
);
1126 DECL_ORIGINAL_TYPE (type_decl
) = gtype
;
1131 /* For some dummy arguments we don't use the actual argument directly.
1132 Instead we create a local decl and use that. This allows us to perform
1133 initialization, and construct full type information. */
1136 gfc_build_dummy_array_decl (gfc_symbol
* sym
, tree dummy
)
1141 symbol_attribute
*array_attr
;
1146 bool is_classarray
= IS_CLASS_ARRAY (sym
);
1148 /* Use the array as and attr. */
1149 as
= is_classarray
? CLASS_DATA (sym
)->as
: sym
->as
;
1150 array_attr
= is_classarray
? &CLASS_DATA (sym
)->attr
: &sym
->attr
;
1152 /* The dummy is returned for pointer, allocatable or assumed rank arrays.
1153 For class arrays the information if sym is an allocatable or pointer
1154 object needs to be checked explicitly (IS_CLASS_ARRAY can be false for
1155 too many reasons to be of use here). */
1156 if ((sym
->ts
.type
!= BT_CLASS
&& sym
->attr
.pointer
)
1157 || (sym
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym
)->attr
.class_pointer
)
1158 || array_attr
->allocatable
1159 || (as
&& as
->type
== AS_ASSUMED_RANK
))
1162 /* Add to list of variables if not a fake result variable.
1163 These symbols are set on the symbol only, not on the class component. */
1164 if (sym
->attr
.result
|| sym
->attr
.dummy
)
1165 gfc_defer_symbol_init (sym
);
1167 /* For a class array the array descriptor is in the _data component, while
1168 for a regular array the TREE_TYPE of the dummy is a pointer to the
1170 type
= TREE_TYPE (is_classarray
? gfc_class_data_get (dummy
)
1171 : TREE_TYPE (dummy
));
1172 /* type now is the array descriptor w/o any indirection. */
1173 gcc_assert (TREE_CODE (dummy
) == PARM_DECL
1174 && POINTER_TYPE_P (TREE_TYPE (dummy
)));
1176 /* Do we know the element size? */
1177 known_size
= sym
->ts
.type
!= BT_CHARACTER
1178 || INTEGER_CST_P (sym
->ts
.u
.cl
->backend_decl
);
1180 if (known_size
&& !GFC_DESCRIPTOR_TYPE_P (type
))
1182 /* For descriptorless arrays with known element size the actual
1183 argument is sufficient. */
1184 gfc_build_qualified_array (dummy
, sym
);
1188 if (GFC_DESCRIPTOR_TYPE_P (type
))
1190 /* Create a descriptorless array pointer. */
1193 /* Even when -frepack-arrays is used, symbols with TARGET attribute
1194 are not repacked. */
1195 if (!flag_repack_arrays
|| sym
->attr
.target
)
1197 if (as
->type
== AS_ASSUMED_SIZE
)
1198 packed
= PACKED_FULL
;
1202 if (as
->type
== AS_EXPLICIT
)
1204 packed
= PACKED_FULL
;
1205 for (n
= 0; n
< as
->rank
; n
++)
1209 && as
->upper
[n
]->expr_type
== EXPR_CONSTANT
1210 && as
->lower
[n
]->expr_type
== EXPR_CONSTANT
))
1212 packed
= PACKED_PARTIAL
;
1218 packed
= PACKED_PARTIAL
;
1221 /* For classarrays the element type is required, but
1222 gfc_typenode_for_spec () returns the array descriptor. */
1223 type
= is_classarray
? gfc_get_element_type (type
)
1224 : gfc_typenode_for_spec (&sym
->ts
);
1225 type
= gfc_get_nodesc_array_type (type
, as
, packed
,
1230 /* We now have an expression for the element size, so create a fully
1231 qualified type. Reset sym->backend decl or this will just return the
1233 DECL_ARTIFICIAL (sym
->backend_decl
) = 1;
1234 sym
->backend_decl
= NULL_TREE
;
1235 type
= gfc_sym_type (sym
);
1236 packed
= PACKED_FULL
;
1239 ASM_FORMAT_PRIVATE_NAME (name
, IDENTIFIER_POINTER (DECL_NAME (dummy
)), 0);
1240 decl
= build_decl (input_location
,
1241 VAR_DECL
, get_identifier (name
), type
);
1243 DECL_ARTIFICIAL (decl
) = 1;
1244 DECL_NAMELESS (decl
) = 1;
1245 TREE_PUBLIC (decl
) = 0;
1246 TREE_STATIC (decl
) = 0;
1247 DECL_EXTERNAL (decl
) = 0;
1249 /* Avoid uninitialized warnings for optional dummy arguments. */
1250 if (sym
->attr
.optional
)
1251 TREE_NO_WARNING (decl
) = 1;
1253 /* We should never get deferred shape arrays here. We used to because of
1255 gcc_assert (as
->type
!= AS_DEFERRED
);
1257 if (packed
== PACKED_PARTIAL
)
1258 GFC_DECL_PARTIAL_PACKED_ARRAY (decl
) = 1;
1259 else if (packed
== PACKED_FULL
)
1260 GFC_DECL_PACKED_ARRAY (decl
) = 1;
1262 gfc_build_qualified_array (decl
, sym
);
1264 if (DECL_LANG_SPECIFIC (dummy
))
1265 DECL_LANG_SPECIFIC (decl
) = DECL_LANG_SPECIFIC (dummy
);
1267 gfc_allocate_lang_decl (decl
);
1269 GFC_DECL_SAVED_DESCRIPTOR (decl
) = dummy
;
1271 if (sym
->ns
->proc_name
->backend_decl
== current_function_decl
1272 || sym
->attr
.contained
)
1273 gfc_add_decl_to_function (decl
);
1275 gfc_add_decl_to_parent_function (decl
);
1280 /* For symbol SYM with GFC_DECL_SAVED_DESCRIPTOR used in contained
1281 function add a VAR_DECL to the current function with DECL_VALUE_EXPR
1282 pointing to the artificial variable for debug info purposes. */
1285 gfc_nonlocal_dummy_array_decl (gfc_symbol
*sym
)
1289 if (! nonlocal_dummy_decl_pset
)
1290 nonlocal_dummy_decl_pset
= new hash_set
<tree
>;
1292 if (nonlocal_dummy_decl_pset
->add (sym
->backend_decl
))
1295 dummy
= GFC_DECL_SAVED_DESCRIPTOR (sym
->backend_decl
);
1296 decl
= build_decl (input_location
, VAR_DECL
, DECL_NAME (dummy
),
1297 TREE_TYPE (sym
->backend_decl
));
1298 DECL_ARTIFICIAL (decl
) = 0;
1299 TREE_USED (decl
) = 1;
1300 TREE_PUBLIC (decl
) = 0;
1301 TREE_STATIC (decl
) = 0;
1302 DECL_EXTERNAL (decl
) = 0;
1303 if (DECL_BY_REFERENCE (dummy
))
1304 DECL_BY_REFERENCE (decl
) = 1;
1305 DECL_LANG_SPECIFIC (decl
) = DECL_LANG_SPECIFIC (sym
->backend_decl
);
1306 SET_DECL_VALUE_EXPR (decl
, sym
->backend_decl
);
1307 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
1308 DECL_CONTEXT (decl
) = DECL_CONTEXT (sym
->backend_decl
);
1309 DECL_CHAIN (decl
) = nonlocal_dummy_decls
;
1310 nonlocal_dummy_decls
= decl
;
1313 /* Return a constant or a variable to use as a string length. Does not
1314 add the decl to the current scope. */
1317 gfc_create_string_length (gfc_symbol
* sym
)
1319 gcc_assert (sym
->ts
.u
.cl
);
1320 gfc_conv_const_charlen (sym
->ts
.u
.cl
);
1322 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
1327 /* The string length variable shall be in static memory if it is either
1328 explicitly SAVED, a module variable or with -fno-automatic. Only
1329 relevant is "len=:" - otherwise, it is either a constant length or
1330 it is an automatic variable. */
1331 bool static_length
= sym
->attr
.save
1332 || sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
1333 || (flag_max_stack_var_size
== 0
1334 && sym
->ts
.deferred
&& !sym
->attr
.dummy
1335 && !sym
->attr
.result
&& !sym
->attr
.function
);
1337 /* Also prefix the mangled name. We need to call GFC_PREFIX for static
1338 variables as some systems do not support the "." in the assembler name.
1339 For nonstatic variables, the "." does not appear in assembler. */
1343 name
= gfc_get_string (GFC_PREFIX ("%s_MOD_%s"), sym
->module
,
1346 name
= gfc_get_string (GFC_PREFIX ("%s"), sym
->name
);
1348 else if (sym
->module
)
1349 name
= gfc_get_string (".__%s_MOD_%s", sym
->module
, sym
->name
);
1351 name
= gfc_get_string (".%s", sym
->name
);
1353 length
= build_decl (input_location
,
1354 VAR_DECL
, get_identifier (name
),
1355 gfc_charlen_type_node
);
1356 DECL_ARTIFICIAL (length
) = 1;
1357 TREE_USED (length
) = 1;
1358 if (sym
->ns
->proc_name
->tlink
!= NULL
)
1359 gfc_defer_symbol_init (sym
);
1361 sym
->ts
.u
.cl
->backend_decl
= length
;
1364 TREE_STATIC (length
) = 1;
1366 if (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
1367 && (sym
->attr
.access
!= ACCESS_PRIVATE
|| sym
->attr
.public_used
))
1368 TREE_PUBLIC (length
) = 1;
1371 gcc_assert (sym
->ts
.u
.cl
->backend_decl
!= NULL_TREE
);
1372 return sym
->ts
.u
.cl
->backend_decl
;
1375 /* If a variable is assigned a label, we add another two auxiliary
1379 gfc_add_assign_aux_vars (gfc_symbol
* sym
)
1385 gcc_assert (sym
->backend_decl
);
1387 decl
= sym
->backend_decl
;
1388 gfc_allocate_lang_decl (decl
);
1389 GFC_DECL_ASSIGN (decl
) = 1;
1390 length
= build_decl (input_location
,
1391 VAR_DECL
, create_tmp_var_name (sym
->name
),
1392 gfc_charlen_type_node
);
1393 addr
= build_decl (input_location
,
1394 VAR_DECL
, create_tmp_var_name (sym
->name
),
1396 gfc_finish_var_decl (length
, sym
);
1397 gfc_finish_var_decl (addr
, sym
);
1398 /* STRING_LENGTH is also used as flag. Less than -1 means that
1399 ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the
1400 target label's address. Otherwise, value is the length of a format string
1401 and ASSIGN_ADDR is its address. */
1402 if (TREE_STATIC (length
))
1403 DECL_INITIAL (length
) = build_int_cst (gfc_charlen_type_node
, -2);
1405 gfc_defer_symbol_init (sym
);
1407 GFC_DECL_STRING_LEN (decl
) = length
;
1408 GFC_DECL_ASSIGN_ADDR (decl
) = addr
;
1413 add_attributes_to_decl (symbol_attribute sym_attr
, tree list
)
1418 for (id
= 0; id
< EXT_ATTR_NUM
; id
++)
1419 if (sym_attr
.ext_attr
& (1 << id
))
1421 attr
= build_tree_list (
1422 get_identifier (ext_attr_list
[id
].middle_end_name
),
1424 list
= chainon (list
, attr
);
1427 if (sym_attr
.omp_declare_target_link
)
1428 list
= tree_cons (get_identifier ("omp declare target link"),
1430 else if (sym_attr
.omp_declare_target
)
1431 list
= tree_cons (get_identifier ("omp declare target"),
1434 if (sym_attr
.oacc_function
)
1436 tree dims
= NULL_TREE
;
1438 int level
= sym_attr
.oacc_function
- 1;
1440 for (ix
= GOMP_DIM_MAX
; ix
--;)
1441 dims
= tree_cons (build_int_cst (boolean_type_node
, ix
>= level
),
1442 integer_zero_node
, dims
);
1444 list
= tree_cons (get_identifier ("oacc function"),
1452 static void build_function_decl (gfc_symbol
* sym
, bool global
);
1455 /* Return the decl for a gfc_symbol, create it if it doesn't already
1459 gfc_get_symbol_decl (gfc_symbol
* sym
)
1462 tree length
= NULL_TREE
;
1465 bool intrinsic_array_parameter
= false;
1468 gcc_assert (sym
->attr
.referenced
1469 || sym
->attr
.flavor
== FL_PROCEDURE
1470 || sym
->attr
.use_assoc
1471 || sym
->attr
.used_in_submodule
1472 || sym
->ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
1473 || (sym
->module
&& sym
->attr
.if_source
!= IFSRC_DECL
1474 && sym
->backend_decl
));
1476 if (sym
->ns
&& sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.function
)
1477 byref
= gfc_return_by_reference (sym
->ns
->proc_name
);
1481 /* Make sure that the vtab for the declared type is completed. */
1482 if (sym
->ts
.type
== BT_CLASS
)
1484 gfc_component
*c
= CLASS_DATA (sym
);
1485 if (!c
->ts
.u
.derived
->backend_decl
)
1487 gfc_find_derived_vtab (c
->ts
.u
.derived
);
1488 gfc_get_derived_type (sym
->ts
.u
.derived
);
1492 /* PDT parameterized array components and string_lengths must have the
1493 'len' parameters substituted for the expressions appearing in the
1494 declaration of the entity and memory allocated/deallocated. */
1495 if ((sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
1496 && sym
->param_list
!= NULL
1497 && !(sym
->attr
.host_assoc
|| sym
->attr
.use_assoc
|| sym
->attr
.dummy
))
1498 gfc_defer_symbol_init (sym
);
1500 /* Dummy PDT 'len' parameters should be checked when they are explicit. */
1501 if ((sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
1502 && (gfc_option
.rtcheck
& GFC_RTCHECK_BOUNDS
)
1503 && sym
->param_list
!= NULL
1505 gfc_defer_symbol_init (sym
);
1507 /* All deferred character length procedures need to retain the backend
1508 decl, which is a pointer to the character length in the caller's
1509 namespace and to declare a local character length. */
1510 if (!byref
&& sym
->attr
.function
1511 && sym
->ts
.type
== BT_CHARACTER
1513 && sym
->ts
.u
.cl
->passed_length
== NULL
1514 && sym
->ts
.u
.cl
->backend_decl
1515 && TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
1517 sym
->ts
.u
.cl
->passed_length
= sym
->ts
.u
.cl
->backend_decl
;
1518 gcc_assert (POINTER_TYPE_P (TREE_TYPE (sym
->ts
.u
.cl
->passed_length
)));
1519 sym
->ts
.u
.cl
->backend_decl
= build_fold_indirect_ref (sym
->ts
.u
.cl
->backend_decl
);
1522 fun_or_res
= byref
&& (sym
->attr
.result
1523 || (sym
->attr
.function
&& sym
->ts
.deferred
));
1524 if ((sym
->attr
.dummy
&& ! sym
->attr
.function
) || fun_or_res
)
1526 /* Return via extra parameter. */
1527 if (sym
->attr
.result
&& byref
1528 && !sym
->backend_decl
)
1531 DECL_ARGUMENTS (sym
->ns
->proc_name
->backend_decl
);
1532 /* For entry master function skip over the __entry
1534 if (sym
->ns
->proc_name
->attr
.entry_master
)
1535 sym
->backend_decl
= DECL_CHAIN (sym
->backend_decl
);
1538 /* Dummy variables should already have been created. */
1539 gcc_assert (sym
->backend_decl
);
1541 if (sym
->attr
.pointer
&& sym
->attr
.dimension
&& sym
->ts
.type
!= BT_CLASS
)
1542 GFC_DECL_PTR_ARRAY_P (sym
->backend_decl
) = 1;
1544 /* Create a character length variable. */
1545 if (sym
->ts
.type
== BT_CHARACTER
)
1547 /* For a deferred dummy, make a new string length variable. */
1548 if (sym
->ts
.deferred
1550 (sym
->ts
.u
.cl
->passed_length
== sym
->ts
.u
.cl
->backend_decl
))
1551 sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
1553 if (sym
->ts
.deferred
&& byref
)
1555 /* The string length of a deferred char array is stored in the
1556 parameter at sym->ts.u.cl->backend_decl as a reference and
1557 marked as a result. Exempt this variable from generating a
1558 temporary for it. */
1559 if (sym
->attr
.result
)
1561 /* We need to insert a indirect ref for param decls. */
1562 if (sym
->ts
.u
.cl
->backend_decl
1563 && TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
1565 sym
->ts
.u
.cl
->passed_length
= sym
->ts
.u
.cl
->backend_decl
;
1566 sym
->ts
.u
.cl
->backend_decl
=
1567 build_fold_indirect_ref (sym
->ts
.u
.cl
->backend_decl
);
1570 /* For all other parameters make sure, that they are copied so
1571 that the value and any modifications are local to the routine
1572 by generating a temporary variable. */
1573 else if (sym
->attr
.function
1574 && sym
->ts
.u
.cl
->passed_length
== NULL
1575 && sym
->ts
.u
.cl
->backend_decl
)
1577 sym
->ts
.u
.cl
->passed_length
= sym
->ts
.u
.cl
->backend_decl
;
1578 if (POINTER_TYPE_P (TREE_TYPE (sym
->ts
.u
.cl
->passed_length
)))
1579 sym
->ts
.u
.cl
->backend_decl
1580 = build_fold_indirect_ref (sym
->ts
.u
.cl
->backend_decl
);
1582 sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
1586 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
1587 length
= gfc_create_string_length (sym
);
1589 length
= sym
->ts
.u
.cl
->backend_decl
;
1590 if (VAR_P (length
) && DECL_FILE_SCOPE_P (length
))
1592 /* Add the string length to the same context as the symbol. */
1593 if (DECL_CONTEXT (sym
->backend_decl
) == current_function_decl
)
1594 gfc_add_decl_to_function (length
);
1596 gfc_add_decl_to_parent_function (length
);
1598 gcc_assert (DECL_CONTEXT (sym
->backend_decl
) ==
1599 DECL_CONTEXT (length
));
1601 gfc_defer_symbol_init (sym
);
1605 /* Use a copy of the descriptor for dummy arrays. */
1606 if ((sym
->attr
.dimension
|| sym
->attr
.codimension
)
1607 && !TREE_USED (sym
->backend_decl
))
1609 decl
= gfc_build_dummy_array_decl (sym
, sym
->backend_decl
);
1610 /* Prevent the dummy from being detected as unused if it is copied. */
1611 if (sym
->backend_decl
!= NULL
&& decl
!= sym
->backend_decl
)
1612 DECL_ARTIFICIAL (sym
->backend_decl
) = 1;
1613 sym
->backend_decl
= decl
;
1616 /* Returning the descriptor for dummy class arrays is hazardous, because
1617 some caller is expecting an expression to apply the component refs to.
1618 Therefore the descriptor is only created and stored in
1619 sym->backend_decl's GFC_DECL_SAVED_DESCRIPTOR. The caller is then
1620 responsible to extract it from there, when the descriptor is
1622 if (IS_CLASS_ARRAY (sym
)
1623 && (!DECL_LANG_SPECIFIC (sym
->backend_decl
)
1624 || !GFC_DECL_SAVED_DESCRIPTOR (sym
->backend_decl
)))
1626 decl
= gfc_build_dummy_array_decl (sym
, sym
->backend_decl
);
1627 /* Prevent the dummy from being detected as unused if it is copied. */
1628 if (sym
->backend_decl
!= NULL
&& decl
!= sym
->backend_decl
)
1629 DECL_ARTIFICIAL (sym
->backend_decl
) = 1;
1630 sym
->backend_decl
= decl
;
1633 TREE_USED (sym
->backend_decl
) = 1;
1634 if (sym
->attr
.assign
&& GFC_DECL_ASSIGN (sym
->backend_decl
) == 0)
1636 gfc_add_assign_aux_vars (sym
);
1639 if ((sym
->attr
.dimension
|| IS_CLASS_ARRAY (sym
))
1640 && DECL_LANG_SPECIFIC (sym
->backend_decl
)
1641 && GFC_DECL_SAVED_DESCRIPTOR (sym
->backend_decl
)
1642 && DECL_CONTEXT (sym
->backend_decl
) != current_function_decl
)
1643 gfc_nonlocal_dummy_array_decl (sym
);
1645 if (sym
->ts
.type
== BT_CLASS
&& sym
->backend_decl
)
1646 GFC_DECL_CLASS(sym
->backend_decl
) = 1;
1648 return sym
->backend_decl
;
1651 if (sym
->backend_decl
)
1652 return sym
->backend_decl
;
1654 /* Special case for array-valued named constants from intrinsic
1655 procedures; those are inlined. */
1656 if (sym
->attr
.use_assoc
&& sym
->attr
.flavor
== FL_PARAMETER
1657 && (sym
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
1658 || sym
->from_intmod
== INTMOD_ISO_C_BINDING
))
1659 intrinsic_array_parameter
= true;
1661 /* If use associated compilation, use the module
1663 if ((sym
->attr
.flavor
== FL_VARIABLE
1664 || sym
->attr
.flavor
== FL_PARAMETER
)
1665 && (sym
->attr
.use_assoc
|| sym
->attr
.used_in_submodule
)
1666 && !intrinsic_array_parameter
1668 && gfc_get_module_backend_decl (sym
))
1670 if (sym
->ts
.type
== BT_CLASS
&& sym
->backend_decl
)
1671 GFC_DECL_CLASS(sym
->backend_decl
) = 1;
1672 return sym
->backend_decl
;
1675 if (sym
->attr
.flavor
== FL_PROCEDURE
)
1677 /* Catch functions. Only used for actual parameters,
1678 procedure pointers and procptr initialization targets. */
1679 if (sym
->attr
.use_assoc
1680 || sym
->attr
.used_in_submodule
1681 || sym
->attr
.intrinsic
1682 || sym
->attr
.if_source
!= IFSRC_DECL
)
1684 decl
= gfc_get_extern_function_decl (sym
);
1685 gfc_set_decl_location (decl
, &sym
->declared_at
);
1689 if (!sym
->backend_decl
)
1690 build_function_decl (sym
, false);
1691 decl
= sym
->backend_decl
;
1696 if (sym
->attr
.intrinsic
)
1697 gfc_internal_error ("intrinsic variable which isn't a procedure");
1699 /* Create string length decl first so that they can be used in the
1700 type declaration. For associate names, the target character
1701 length is used. Set 'length' to a constant so that if the
1702 string length is a variable, it is not finished a second time. */
1703 if (sym
->ts
.type
== BT_CHARACTER
)
1705 if (sym
->attr
.associate_var
1707 && sym
->assoc
&& sym
->assoc
->target
1708 && ((sym
->assoc
->target
->expr_type
== EXPR_VARIABLE
1709 && sym
->assoc
->target
->symtree
->n
.sym
->ts
.type
!= BT_CHARACTER
)
1710 || sym
->assoc
->target
->expr_type
== EXPR_FUNCTION
))
1711 sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
1713 if (sym
->attr
.associate_var
1714 && sym
->ts
.u
.cl
->backend_decl
1715 && VAR_P (sym
->ts
.u
.cl
->backend_decl
))
1716 length
= gfc_index_zero_node
;
1718 length
= gfc_create_string_length (sym
);
1721 /* Create the decl for the variable. */
1722 decl
= build_decl (sym
->declared_at
.lb
->location
,
1723 VAR_DECL
, gfc_sym_identifier (sym
), gfc_sym_type (sym
));
1725 /* Add attributes to variables. Functions are handled elsewhere. */
1726 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
1727 decl_attributes (&decl
, attributes
, 0);
1729 /* Symbols from modules should have their assembler names mangled.
1730 This is done here rather than in gfc_finish_var_decl because it
1731 is different for string length variables. */
1732 if (sym
->module
|| sym
->fn_result_spec
)
1734 gfc_set_decl_assembler_name (decl
, gfc_sym_mangled_identifier (sym
));
1735 if (sym
->attr
.use_assoc
&& !intrinsic_array_parameter
)
1736 DECL_IGNORED_P (decl
) = 1;
1739 if (sym
->attr
.select_type_temporary
)
1741 DECL_ARTIFICIAL (decl
) = 1;
1742 DECL_IGNORED_P (decl
) = 1;
1745 if (sym
->attr
.dimension
|| sym
->attr
.codimension
)
1747 /* Create variables to hold the non-constant bits of array info. */
1748 gfc_build_qualified_array (decl
, sym
);
1750 if (sym
->attr
.contiguous
1751 || ((sym
->attr
.allocatable
|| !sym
->attr
.dummy
) && !sym
->attr
.pointer
))
1752 GFC_DECL_PACKED_ARRAY (decl
) = 1;
1755 /* Remember this variable for allocation/cleanup. */
1756 if (sym
->attr
.dimension
|| sym
->attr
.allocatable
|| sym
->attr
.codimension
1757 || (sym
->ts
.type
== BT_CLASS
&&
1758 (CLASS_DATA (sym
)->attr
.dimension
1759 || CLASS_DATA (sym
)->attr
.allocatable
))
1760 || (sym
->ts
.type
== BT_DERIVED
1761 && (sym
->ts
.u
.derived
->attr
.alloc_comp
1762 || (!sym
->attr
.pointer
&& !sym
->attr
.artificial
&& !sym
->attr
.save
1763 && !sym
->ns
->proc_name
->attr
.is_main_program
1764 && gfc_is_finalizable (sym
->ts
.u
.derived
, NULL
))))
1765 /* This applies a derived type default initializer. */
1766 || (sym
->ts
.type
== BT_DERIVED
1767 && sym
->attr
.save
== SAVE_NONE
1769 && !sym
->attr
.allocatable
1770 && (sym
->value
&& !sym
->ns
->proc_name
->attr
.is_main_program
)
1771 && !(sym
->attr
.use_assoc
&& !intrinsic_array_parameter
)))
1772 gfc_defer_symbol_init (sym
);
1774 /* Associate names can use the hidden string length variable
1775 of their associated target. */
1776 if (sym
->ts
.type
== BT_CHARACTER
1777 && TREE_CODE (length
) != INTEGER_CST
1778 && TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) != INDIRECT_REF
)
1780 gfc_finish_var_decl (length
, sym
);
1781 gcc_assert (!sym
->value
);
1784 gfc_finish_var_decl (decl
, sym
);
1786 if (sym
->ts
.type
== BT_CHARACTER
)
1787 /* Character variables need special handling. */
1788 gfc_allocate_lang_decl (decl
);
1790 if (sym
->assoc
&& sym
->attr
.subref_array_pointer
)
1791 sym
->attr
.pointer
= 1;
1793 if (sym
->attr
.pointer
&& sym
->attr
.dimension
1794 && !sym
->ts
.deferred
1795 && !(sym
->attr
.select_type_temporary
1796 && !sym
->attr
.subref_array_pointer
))
1797 GFC_DECL_PTR_ARRAY_P (decl
) = 1;
1799 if (sym
->ts
.type
== BT_CLASS
)
1800 GFC_DECL_CLASS(decl
) = 1;
1802 sym
->backend_decl
= decl
;
1804 if (sym
->attr
.assign
)
1805 gfc_add_assign_aux_vars (sym
);
1807 if (intrinsic_array_parameter
)
1809 TREE_STATIC (decl
) = 1;
1810 DECL_EXTERNAL (decl
) = 0;
1813 if (TREE_STATIC (decl
)
1814 && !(sym
->attr
.use_assoc
&& !intrinsic_array_parameter
)
1815 && (sym
->attr
.save
|| sym
->ns
->proc_name
->attr
.is_main_program
1816 || !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl
))
1817 || sym
->attr
.data
|| sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
1818 && (flag_coarray
!= GFC_FCOARRAY_LIB
1819 || !sym
->attr
.codimension
|| sym
->attr
.allocatable
)
1820 && !(sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.pdt_type
)
1821 && !(sym
->ts
.type
== BT_CLASS
1822 && CLASS_DATA (sym
)->ts
.u
.derived
->attr
.pdt_type
))
1824 /* Add static initializer. For procedures, it is only needed if
1825 SAVE is specified otherwise they need to be reinitialized
1826 every time the procedure is entered. The TREE_STATIC is
1827 in this case due to -fmax-stack-var-size=. */
1829 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
1830 TREE_TYPE (decl
), sym
->attr
.dimension
1831 || (sym
->attr
.codimension
1832 && sym
->attr
.allocatable
),
1833 sym
->attr
.pointer
|| sym
->attr
.allocatable
1834 || sym
->ts
.type
== BT_CLASS
,
1835 sym
->attr
.proc_pointer
);
1838 if (!TREE_STATIC (decl
)
1839 && POINTER_TYPE_P (TREE_TYPE (decl
))
1840 && !sym
->attr
.pointer
1841 && !sym
->attr
.allocatable
1842 && !sym
->attr
.proc_pointer
1843 && !sym
->attr
.select_type_temporary
)
1844 DECL_BY_REFERENCE (decl
) = 1;
1846 if (sym
->attr
.associate_var
)
1847 GFC_DECL_ASSOCIATE_VAR_P (decl
) = 1;
1850 || (sym
->name
[0] == '_' && strncmp ("__def_init", sym
->name
, 10) == 0))
1851 TREE_READONLY (decl
) = 1;
1857 /* Substitute a temporary variable in place of the real one. */
1860 gfc_shadow_sym (gfc_symbol
* sym
, tree decl
, gfc_saved_var
* save
)
1862 save
->attr
= sym
->attr
;
1863 save
->decl
= sym
->backend_decl
;
1865 gfc_clear_attr (&sym
->attr
);
1866 sym
->attr
.referenced
= 1;
1867 sym
->attr
.flavor
= FL_VARIABLE
;
1869 sym
->backend_decl
= decl
;
1873 /* Restore the original variable. */
1876 gfc_restore_sym (gfc_symbol
* sym
, gfc_saved_var
* save
)
1878 sym
->attr
= save
->attr
;
1879 sym
->backend_decl
= save
->decl
;
1883 /* Declare a procedure pointer. */
1886 get_proc_pointer_decl (gfc_symbol
*sym
)
1891 decl
= sym
->backend_decl
;
1895 decl
= build_decl (input_location
,
1896 VAR_DECL
, get_identifier (sym
->name
),
1897 build_pointer_type (gfc_get_function_type (sym
)));
1901 /* Apply name mangling. */
1902 gfc_set_decl_assembler_name (decl
, gfc_sym_mangled_identifier (sym
));
1903 if (sym
->attr
.use_assoc
)
1904 DECL_IGNORED_P (decl
) = 1;
1907 if ((sym
->ns
->proc_name
1908 && sym
->ns
->proc_name
->backend_decl
== current_function_decl
)
1909 || sym
->attr
.contained
)
1910 gfc_add_decl_to_function (decl
);
1911 else if (sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
1912 gfc_add_decl_to_parent_function (decl
);
1914 sym
->backend_decl
= decl
;
1916 /* If a variable is USE associated, it's always external. */
1917 if (sym
->attr
.use_assoc
)
1919 DECL_EXTERNAL (decl
) = 1;
1920 TREE_PUBLIC (decl
) = 1;
1922 else if (sym
->module
&& sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
1924 /* This is the declaration of a module variable. */
1925 TREE_PUBLIC (decl
) = 1;
1926 if (sym
->attr
.access
== ACCESS_PRIVATE
&& !sym
->attr
.public_used
)
1928 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
1929 DECL_VISIBILITY_SPECIFIED (decl
) = true;
1931 TREE_STATIC (decl
) = 1;
1934 if (!sym
->attr
.use_assoc
1935 && (sym
->attr
.save
!= SAVE_NONE
|| sym
->attr
.data
1936 || (sym
->value
&& sym
->ns
->proc_name
->attr
.is_main_program
)))
1937 TREE_STATIC (decl
) = 1;
1939 if (TREE_STATIC (decl
) && sym
->value
)
1941 /* Add static initializer. */
1942 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
1944 sym
->attr
.dimension
,
1948 /* Handle threadprivate procedure pointers. */
1949 if (sym
->attr
.threadprivate
1950 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1951 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
1953 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
1954 decl_attributes (&decl
, attributes
, 0);
1960 /* Get a basic decl for an external function. */
1963 gfc_get_extern_function_decl (gfc_symbol
* sym
)
1969 gfc_intrinsic_sym
*isym
;
1971 char s
[GFC_MAX_SYMBOL_LEN
+ 23]; /* "_gfortran_f2c_specific" and '\0'. */
1976 if (sym
->backend_decl
)
1977 return sym
->backend_decl
;
1979 /* We should never be creating external decls for alternate entry points.
1980 The procedure may be an alternate entry point, but we don't want/need
1982 gcc_assert (!(sym
->attr
.entry
|| sym
->attr
.entry_master
));
1984 if (sym
->attr
.proc_pointer
)
1985 return get_proc_pointer_decl (sym
);
1987 /* See if this is an external procedure from the same file. If so,
1988 return the backend_decl. */
1989 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->binding_label
1990 ? sym
->binding_label
: sym
->name
);
1992 if (gsym
&& !gsym
->defined
)
1995 /* This can happen because of C binding. */
1996 if (gsym
&& gsym
->ns
&& gsym
->ns
->proc_name
1997 && gsym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
2000 if ((!sym
->attr
.use_assoc
|| sym
->attr
.if_source
!= IFSRC_DECL
)
2001 && !sym
->backend_decl
2003 && ((gsym
->type
== GSYM_SUBROUTINE
) || (gsym
->type
== GSYM_FUNCTION
))
2004 && (gsym
->ns
->proc_name
->backend_decl
|| !sym
->attr
.intrinsic
))
2006 if (!gsym
->ns
->proc_name
->backend_decl
)
2008 /* By construction, the external function cannot be
2009 a contained procedure. */
2012 gfc_save_backend_locus (&old_loc
);
2015 gfc_create_function_decl (gsym
->ns
, true);
2018 gfc_restore_backend_locus (&old_loc
);
2021 /* If the namespace has entries, the proc_name is the
2022 entry master. Find the entry and use its backend_decl.
2023 otherwise, use the proc_name backend_decl. */
2024 if (gsym
->ns
->entries
)
2026 gfc_entry_list
*entry
= gsym
->ns
->entries
;
2028 for (; entry
; entry
= entry
->next
)
2030 if (strcmp (gsym
->name
, entry
->sym
->name
) == 0)
2032 sym
->backend_decl
= entry
->sym
->backend_decl
;
2038 sym
->backend_decl
= gsym
->ns
->proc_name
->backend_decl
;
2040 if (sym
->backend_decl
)
2042 /* Avoid problems of double deallocation of the backend declaration
2043 later in gfc_trans_use_stmts; cf. PR 45087. */
2044 if (sym
->attr
.if_source
!= IFSRC_DECL
&& sym
->attr
.use_assoc
)
2045 sym
->attr
.use_assoc
= 0;
2047 return sym
->backend_decl
;
2051 /* See if this is a module procedure from the same file. If so,
2052 return the backend_decl. */
2054 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->module
);
2057 if (gsym
&& gsym
->ns
2058 && (gsym
->type
== GSYM_MODULE
2059 || (gsym
->ns
->proc_name
&& gsym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)))
2064 if (gsym
->type
== GSYM_MODULE
)
2065 gfc_find_symbol (sym
->name
, gsym
->ns
, 0, &s
);
2067 gfc_find_symbol (gsym
->sym_name
, gsym
->ns
, 0, &s
);
2069 if (s
&& s
->backend_decl
)
2071 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
2072 gfc_copy_dt_decls_ifequal (s
->ts
.u
.derived
, sym
->ts
.u
.derived
,
2074 else if (sym
->ts
.type
== BT_CHARACTER
)
2075 sym
->ts
.u
.cl
->backend_decl
= s
->ts
.u
.cl
->backend_decl
;
2076 sym
->backend_decl
= s
->backend_decl
;
2077 return sym
->backend_decl
;
2081 if (sym
->attr
.intrinsic
)
2083 /* Call the resolution function to get the actual name. This is
2084 a nasty hack which relies on the resolution functions only looking
2085 at the first argument. We pass NULL for the second argument
2086 otherwise things like AINT get confused. */
2087 isym
= gfc_find_function (sym
->name
);
2088 gcc_assert (isym
->resolve
.f0
!= NULL
);
2090 memset (&e
, 0, sizeof (e
));
2091 e
.expr_type
= EXPR_FUNCTION
;
2093 memset (&argexpr
, 0, sizeof (argexpr
));
2094 gcc_assert (isym
->formal
);
2095 argexpr
.ts
= isym
->formal
->ts
;
2097 if (isym
->formal
->next
== NULL
)
2098 isym
->resolve
.f1 (&e
, &argexpr
);
2101 if (isym
->formal
->next
->next
== NULL
)
2102 isym
->resolve
.f2 (&e
, &argexpr
, NULL
);
2105 if (isym
->formal
->next
->next
->next
== NULL
)
2106 isym
->resolve
.f3 (&e
, &argexpr
, NULL
, NULL
);
2109 /* All specific intrinsics take less than 5 arguments. */
2110 gcc_assert (isym
->formal
->next
->next
->next
->next
== NULL
);
2111 isym
->resolve
.f4 (&e
, &argexpr
, NULL
, NULL
, NULL
);
2117 && ((e
.ts
.type
== BT_REAL
&& e
.ts
.kind
== gfc_default_real_kind
)
2118 || e
.ts
.type
== BT_COMPLEX
))
2120 /* Specific which needs a different implementation if f2c
2121 calling conventions are used. */
2122 sprintf (s
, "_gfortran_f2c_specific%s", e
.value
.function
.name
);
2125 sprintf (s
, "_gfortran_specific%s", e
.value
.function
.name
);
2127 name
= get_identifier (s
);
2128 mangled_name
= name
;
2132 name
= gfc_sym_identifier (sym
);
2133 mangled_name
= gfc_sym_mangled_function_id (sym
);
2136 type
= gfc_get_function_type (sym
);
2137 fndecl
= build_decl (input_location
,
2138 FUNCTION_DECL
, name
, type
);
2140 /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
2141 TREE_PUBLIC specifies whether a function is globally addressable (i.e.
2142 the opposite of declaring a function as static in C). */
2143 DECL_EXTERNAL (fndecl
) = 1;
2144 TREE_PUBLIC (fndecl
) = 1;
2146 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
2147 decl_attributes (&fndecl
, attributes
, 0);
2149 gfc_set_decl_assembler_name (fndecl
, mangled_name
);
2151 /* Set the context of this decl. */
2152 if (0 && sym
->ns
&& sym
->ns
->proc_name
)
2154 /* TODO: Add external decls to the appropriate scope. */
2155 DECL_CONTEXT (fndecl
) = sym
->ns
->proc_name
->backend_decl
;
2159 /* Global declaration, e.g. intrinsic subroutine. */
2160 DECL_CONTEXT (fndecl
) = NULL_TREE
;
2163 /* Set attributes for PURE functions. A call to PURE function in the
2164 Fortran 95 sense is both pure and without side effects in the C
2166 if (sym
->attr
.pure
|| sym
->attr
.implicit_pure
)
2168 if (sym
->attr
.function
&& !gfc_return_by_reference (sym
))
2169 DECL_PURE_P (fndecl
) = 1;
2170 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
2171 parameters and don't use alternate returns (is this
2172 allowed?). In that case, calls to them are meaningless, and
2173 can be optimized away. See also in build_function_decl(). */
2174 TREE_SIDE_EFFECTS (fndecl
) = 0;
2177 /* Mark non-returning functions. */
2178 if (sym
->attr
.noreturn
)
2179 TREE_THIS_VOLATILE(fndecl
) = 1;
2181 sym
->backend_decl
= fndecl
;
2183 if (DECL_CONTEXT (fndecl
) == NULL_TREE
)
2184 pushdecl_top_level (fndecl
);
2187 && sym
->formal_ns
->proc_name
== sym
2188 && sym
->formal_ns
->omp_declare_simd
)
2189 gfc_trans_omp_declare_simd (sym
->formal_ns
);
2195 /* Create a declaration for a procedure. For external functions (in the C
2196 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
2197 a master function with alternate entry points. */
2200 build_function_decl (gfc_symbol
* sym
, bool global
)
2202 tree fndecl
, type
, attributes
;
2203 symbol_attribute attr
;
2205 gfc_formal_arglist
*f
;
2207 bool module_procedure
= sym
->attr
.module_procedure
2209 && sym
->ns
->proc_name
2210 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
;
2212 gcc_assert (!sym
->attr
.external
|| module_procedure
);
2214 if (sym
->backend_decl
)
2217 /* Set the line and filename. sym->declared_at seems to point to the
2218 last statement for subroutines, but it'll do for now. */
2219 gfc_set_backend_locus (&sym
->declared_at
);
2221 /* Allow only one nesting level. Allow public declarations. */
2222 gcc_assert (current_function_decl
== NULL_TREE
2223 || DECL_FILE_SCOPE_P (current_function_decl
)
2224 || (TREE_CODE (DECL_CONTEXT (current_function_decl
))
2225 == NAMESPACE_DECL
));
2227 type
= gfc_get_function_type (sym
);
2228 fndecl
= build_decl (input_location
,
2229 FUNCTION_DECL
, gfc_sym_identifier (sym
), type
);
2233 /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
2234 TREE_PUBLIC specifies whether a function is globally addressable (i.e.
2235 the opposite of declaring a function as static in C). */
2236 DECL_EXTERNAL (fndecl
) = 0;
2238 if (sym
->attr
.access
== ACCESS_UNKNOWN
&& sym
->module
2239 && (sym
->ns
->default_access
== ACCESS_PRIVATE
2240 || (sym
->ns
->default_access
== ACCESS_UNKNOWN
2241 && flag_module_private
)))
2242 sym
->attr
.access
= ACCESS_PRIVATE
;
2244 if (!current_function_decl
2245 && !sym
->attr
.entry_master
&& !sym
->attr
.is_main_program
2246 && (sym
->attr
.access
!= ACCESS_PRIVATE
|| sym
->binding_label
2247 || sym
->attr
.public_used
))
2248 TREE_PUBLIC (fndecl
) = 1;
2250 if (sym
->attr
.referenced
|| sym
->attr
.entry_master
)
2251 TREE_USED (fndecl
) = 1;
2253 attributes
= add_attributes_to_decl (attr
, NULL_TREE
);
2254 decl_attributes (&fndecl
, attributes
, 0);
2256 /* Figure out the return type of the declared function, and build a
2257 RESULT_DECL for it. If this is a subroutine with alternate
2258 returns, build a RESULT_DECL for it. */
2259 result_decl
= NULL_TREE
;
2260 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
2263 if (gfc_return_by_reference (sym
))
2264 type
= void_type_node
;
2267 if (sym
->result
!= sym
)
2268 result_decl
= gfc_sym_identifier (sym
->result
);
2270 type
= TREE_TYPE (TREE_TYPE (fndecl
));
2275 /* Look for alternate return placeholders. */
2276 int has_alternate_returns
= 0;
2277 for (f
= gfc_sym_get_dummy_args (sym
); f
; f
= f
->next
)
2281 has_alternate_returns
= 1;
2286 if (has_alternate_returns
)
2287 type
= integer_type_node
;
2289 type
= void_type_node
;
2292 result_decl
= build_decl (input_location
,
2293 RESULT_DECL
, result_decl
, type
);
2294 DECL_ARTIFICIAL (result_decl
) = 1;
2295 DECL_IGNORED_P (result_decl
) = 1;
2296 DECL_CONTEXT (result_decl
) = fndecl
;
2297 DECL_RESULT (fndecl
) = result_decl
;
2299 /* Don't call layout_decl for a RESULT_DECL.
2300 layout_decl (result_decl, 0); */
2302 /* TREE_STATIC means the function body is defined here. */
2303 TREE_STATIC (fndecl
) = 1;
2305 /* Set attributes for PURE functions. A call to a PURE function in the
2306 Fortran 95 sense is both pure and without side effects in the C
2308 if (attr
.pure
|| attr
.implicit_pure
)
2310 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
2311 including an alternate return. In that case it can also be
2312 marked as PURE. See also in gfc_get_extern_function_decl(). */
2313 if (attr
.function
&& !gfc_return_by_reference (sym
))
2314 DECL_PURE_P (fndecl
) = 1;
2315 TREE_SIDE_EFFECTS (fndecl
) = 0;
2319 /* Layout the function declaration and put it in the binding level
2320 of the current function. */
2323 pushdecl_top_level (fndecl
);
2327 /* Perform name mangling if this is a top level or module procedure. */
2328 if (current_function_decl
== NULL_TREE
)
2329 gfc_set_decl_assembler_name (fndecl
, gfc_sym_mangled_function_id (sym
));
2331 sym
->backend_decl
= fndecl
;
2335 /* Create the DECL_ARGUMENTS for a procedure. */
2338 create_function_arglist (gfc_symbol
* sym
)
2341 gfc_formal_arglist
*f
;
2342 tree typelist
, hidden_typelist
;
2343 tree arglist
, hidden_arglist
;
2347 fndecl
= sym
->backend_decl
;
2349 /* Build formal argument list. Make sure that their TREE_CONTEXT is
2350 the new FUNCTION_DECL node. */
2351 arglist
= NULL_TREE
;
2352 hidden_arglist
= NULL_TREE
;
2353 typelist
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
2355 if (sym
->attr
.entry_master
)
2357 type
= TREE_VALUE (typelist
);
2358 parm
= build_decl (input_location
,
2359 PARM_DECL
, get_identifier ("__entry"), type
);
2361 DECL_CONTEXT (parm
) = fndecl
;
2362 DECL_ARG_TYPE (parm
) = type
;
2363 TREE_READONLY (parm
) = 1;
2364 gfc_finish_decl (parm
);
2365 DECL_ARTIFICIAL (parm
) = 1;
2367 arglist
= chainon (arglist
, parm
);
2368 typelist
= TREE_CHAIN (typelist
);
2371 if (gfc_return_by_reference (sym
))
2373 tree type
= TREE_VALUE (typelist
), length
= NULL
;
2375 if (sym
->ts
.type
== BT_CHARACTER
)
2377 /* Length of character result. */
2378 tree len_type
= TREE_VALUE (TREE_CHAIN (typelist
));
2380 length
= build_decl (input_location
,
2382 get_identifier (".__result"),
2384 if (POINTER_TYPE_P (len_type
))
2386 sym
->ts
.u
.cl
->passed_length
= length
;
2387 TREE_USED (length
) = 1;
2389 else if (!sym
->ts
.u
.cl
->length
)
2391 sym
->ts
.u
.cl
->backend_decl
= length
;
2392 TREE_USED (length
) = 1;
2394 gcc_assert (TREE_CODE (length
) == PARM_DECL
);
2395 DECL_CONTEXT (length
) = fndecl
;
2396 DECL_ARG_TYPE (length
) = len_type
;
2397 TREE_READONLY (length
) = 1;
2398 DECL_ARTIFICIAL (length
) = 1;
2399 gfc_finish_decl (length
);
2400 if (sym
->ts
.u
.cl
->backend_decl
== NULL
2401 || sym
->ts
.u
.cl
->backend_decl
== length
)
2406 if (sym
->ts
.u
.cl
->backend_decl
== NULL
)
2408 tree len
= build_decl (input_location
,
2410 get_identifier ("..__result"),
2411 gfc_charlen_type_node
);
2412 DECL_ARTIFICIAL (len
) = 1;
2413 TREE_USED (len
) = 1;
2414 sym
->ts
.u
.cl
->backend_decl
= len
;
2417 /* Make sure PARM_DECL type doesn't point to incomplete type. */
2418 arg
= sym
->result
? sym
->result
: sym
;
2419 backend_decl
= arg
->backend_decl
;
2420 /* Temporary clear it, so that gfc_sym_type creates complete
2422 arg
->backend_decl
= NULL
;
2423 type
= gfc_sym_type (arg
);
2424 arg
->backend_decl
= backend_decl
;
2425 type
= build_reference_type (type
);
2429 parm
= build_decl (input_location
,
2430 PARM_DECL
, get_identifier ("__result"), type
);
2432 DECL_CONTEXT (parm
) = fndecl
;
2433 DECL_ARG_TYPE (parm
) = TREE_VALUE (typelist
);
2434 TREE_READONLY (parm
) = 1;
2435 DECL_ARTIFICIAL (parm
) = 1;
2436 gfc_finish_decl (parm
);
2438 arglist
= chainon (arglist
, parm
);
2439 typelist
= TREE_CHAIN (typelist
);
2441 if (sym
->ts
.type
== BT_CHARACTER
)
2443 gfc_allocate_lang_decl (parm
);
2444 arglist
= chainon (arglist
, length
);
2445 typelist
= TREE_CHAIN (typelist
);
2449 hidden_typelist
= typelist
;
2450 for (f
= gfc_sym_get_dummy_args (sym
); f
; f
= f
->next
)
2451 if (f
->sym
!= NULL
) /* Ignore alternate returns. */
2452 hidden_typelist
= TREE_CHAIN (hidden_typelist
);
2454 for (f
= gfc_sym_get_dummy_args (sym
); f
; f
= f
->next
)
2456 char name
[GFC_MAX_SYMBOL_LEN
+ 2];
2458 /* Ignore alternate returns. */
2462 type
= TREE_VALUE (typelist
);
2464 if (f
->sym
->ts
.type
== BT_CHARACTER
2465 && (!sym
->attr
.is_bind_c
|| sym
->attr
.entry_master
))
2467 tree len_type
= TREE_VALUE (hidden_typelist
);
2468 tree length
= NULL_TREE
;
2469 if (!f
->sym
->ts
.deferred
)
2470 gcc_assert (len_type
== gfc_charlen_type_node
);
2472 gcc_assert (POINTER_TYPE_P (len_type
));
2474 strcpy (&name
[1], f
->sym
->name
);
2476 length
= build_decl (input_location
,
2477 PARM_DECL
, get_identifier (name
), len_type
);
2479 hidden_arglist
= chainon (hidden_arglist
, length
);
2480 DECL_CONTEXT (length
) = fndecl
;
2481 DECL_ARTIFICIAL (length
) = 1;
2482 DECL_ARG_TYPE (length
) = len_type
;
2483 TREE_READONLY (length
) = 1;
2484 gfc_finish_decl (length
);
2486 /* Remember the passed value. */
2487 if (!f
->sym
->ts
.u
.cl
|| f
->sym
->ts
.u
.cl
->passed_length
)
2489 /* This can happen if the same type is used for multiple
2490 arguments. We need to copy cl as otherwise
2491 cl->passed_length gets overwritten. */
2492 f
->sym
->ts
.u
.cl
= gfc_new_charlen (f
->sym
->ns
, f
->sym
->ts
.u
.cl
);
2494 f
->sym
->ts
.u
.cl
->passed_length
= length
;
2496 /* Use the passed value for assumed length variables. */
2497 if (!f
->sym
->ts
.u
.cl
->length
)
2499 TREE_USED (length
) = 1;
2500 gcc_assert (!f
->sym
->ts
.u
.cl
->backend_decl
);
2501 f
->sym
->ts
.u
.cl
->backend_decl
= length
;
2504 hidden_typelist
= TREE_CHAIN (hidden_typelist
);
2506 if (f
->sym
->ts
.u
.cl
->backend_decl
== NULL
2507 || f
->sym
->ts
.u
.cl
->backend_decl
== length
)
2509 if (POINTER_TYPE_P (len_type
))
2510 f
->sym
->ts
.u
.cl
->backend_decl
=
2511 build_fold_indirect_ref_loc (input_location
, length
);
2512 else if (f
->sym
->ts
.u
.cl
->backend_decl
== NULL
)
2513 gfc_create_string_length (f
->sym
);
2515 /* Make sure PARM_DECL type doesn't point to incomplete type. */
2516 if (f
->sym
->attr
.flavor
== FL_PROCEDURE
)
2517 type
= build_pointer_type (gfc_get_function_type (f
->sym
));
2519 type
= gfc_sym_type (f
->sym
);
2522 /* For noncharacter scalar intrinsic types, VALUE passes the value,
2523 hence, the optional status cannot be transferred via a NULL pointer.
2524 Thus, we will use a hidden argument in that case. */
2525 else if (f
->sym
->attr
.optional
&& f
->sym
->attr
.value
2526 && !f
->sym
->attr
.dimension
&& f
->sym
->ts
.type
!= BT_CLASS
2527 && !gfc_bt_struct (f
->sym
->ts
.type
))
2530 strcpy (&name
[1], f
->sym
->name
);
2532 tmp
= build_decl (input_location
,
2533 PARM_DECL
, get_identifier (name
),
2536 hidden_arglist
= chainon (hidden_arglist
, tmp
);
2537 DECL_CONTEXT (tmp
) = fndecl
;
2538 DECL_ARTIFICIAL (tmp
) = 1;
2539 DECL_ARG_TYPE (tmp
) = boolean_type_node
;
2540 TREE_READONLY (tmp
) = 1;
2541 gfc_finish_decl (tmp
);
2544 /* For non-constant length array arguments, make sure they use
2545 a different type node from TYPE_ARG_TYPES type. */
2546 if (f
->sym
->attr
.dimension
2547 && type
== TREE_VALUE (typelist
)
2548 && TREE_CODE (type
) == POINTER_TYPE
2549 && GFC_ARRAY_TYPE_P (type
)
2550 && f
->sym
->as
->type
!= AS_ASSUMED_SIZE
2551 && ! COMPLETE_TYPE_P (TREE_TYPE (type
)))
2553 if (f
->sym
->attr
.flavor
== FL_PROCEDURE
)
2554 type
= build_pointer_type (gfc_get_function_type (f
->sym
));
2556 type
= gfc_sym_type (f
->sym
);
2559 if (f
->sym
->attr
.proc_pointer
)
2560 type
= build_pointer_type (type
);
2562 if (f
->sym
->attr
.volatile_
)
2563 type
= build_qualified_type (type
, TYPE_QUAL_VOLATILE
);
2565 /* Build the argument declaration. */
2566 parm
= build_decl (input_location
,
2567 PARM_DECL
, gfc_sym_identifier (f
->sym
), type
);
2569 if (f
->sym
->attr
.volatile_
)
2571 TREE_THIS_VOLATILE (parm
) = 1;
2572 TREE_SIDE_EFFECTS (parm
) = 1;
2575 /* Fill in arg stuff. */
2576 DECL_CONTEXT (parm
) = fndecl
;
2577 DECL_ARG_TYPE (parm
) = TREE_VALUE (typelist
);
2578 /* All implementation args except for VALUE are read-only. */
2579 if (!f
->sym
->attr
.value
)
2580 TREE_READONLY (parm
) = 1;
2581 if (POINTER_TYPE_P (type
)
2582 && (!f
->sym
->attr
.proc_pointer
2583 && f
->sym
->attr
.flavor
!= FL_PROCEDURE
))
2584 DECL_BY_REFERENCE (parm
) = 1;
2586 gfc_finish_decl (parm
);
2587 gfc_finish_decl_attrs (parm
, &f
->sym
->attr
);
2589 f
->sym
->backend_decl
= parm
;
2591 /* Coarrays which are descriptorless or assumed-shape pass with
2592 -fcoarray=lib the token and the offset as hidden arguments. */
2593 if (flag_coarray
== GFC_FCOARRAY_LIB
2594 && ((f
->sym
->ts
.type
!= BT_CLASS
&& f
->sym
->attr
.codimension
2595 && !f
->sym
->attr
.allocatable
)
2596 || (f
->sym
->ts
.type
== BT_CLASS
2597 && CLASS_DATA (f
->sym
)->attr
.codimension
2598 && !CLASS_DATA (f
->sym
)->attr
.allocatable
)))
2604 gcc_assert (f
->sym
->backend_decl
!= NULL_TREE
2605 && !sym
->attr
.is_bind_c
);
2606 caf_type
= f
->sym
->ts
.type
== BT_CLASS
2607 ? TREE_TYPE (CLASS_DATA (f
->sym
)->backend_decl
)
2608 : TREE_TYPE (f
->sym
->backend_decl
);
2610 token
= build_decl (input_location
, PARM_DECL
,
2611 create_tmp_var_name ("caf_token"),
2612 build_qualified_type (pvoid_type_node
,
2613 TYPE_QUAL_RESTRICT
));
2614 if ((f
->sym
->ts
.type
!= BT_CLASS
2615 && f
->sym
->as
->type
!= AS_DEFERRED
)
2616 || (f
->sym
->ts
.type
== BT_CLASS
2617 && CLASS_DATA (f
->sym
)->as
->type
!= AS_DEFERRED
))
2619 gcc_assert (DECL_LANG_SPECIFIC (f
->sym
->backend_decl
) == NULL
2620 || GFC_DECL_TOKEN (f
->sym
->backend_decl
) == NULL_TREE
);
2621 if (DECL_LANG_SPECIFIC (f
->sym
->backend_decl
) == NULL
)
2622 gfc_allocate_lang_decl (f
->sym
->backend_decl
);
2623 GFC_DECL_TOKEN (f
->sym
->backend_decl
) = token
;
2627 gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
) == NULL_TREE
);
2628 GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
) = token
;
2631 DECL_CONTEXT (token
) = fndecl
;
2632 DECL_ARTIFICIAL (token
) = 1;
2633 DECL_ARG_TYPE (token
) = TREE_VALUE (typelist
);
2634 TREE_READONLY (token
) = 1;
2635 hidden_arglist
= chainon (hidden_arglist
, token
);
2636 gfc_finish_decl (token
);
2638 offset
= build_decl (input_location
, PARM_DECL
,
2639 create_tmp_var_name ("caf_offset"),
2640 gfc_array_index_type
);
2642 if ((f
->sym
->ts
.type
!= BT_CLASS
2643 && f
->sym
->as
->type
!= AS_DEFERRED
)
2644 || (f
->sym
->ts
.type
== BT_CLASS
2645 && CLASS_DATA (f
->sym
)->as
->type
!= AS_DEFERRED
))
2647 gcc_assert (GFC_DECL_CAF_OFFSET (f
->sym
->backend_decl
)
2649 GFC_DECL_CAF_OFFSET (f
->sym
->backend_decl
) = offset
;
2653 gcc_assert (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type
) == NULL_TREE
);
2654 GFC_TYPE_ARRAY_CAF_OFFSET (caf_type
) = offset
;
2656 DECL_CONTEXT (offset
) = fndecl
;
2657 DECL_ARTIFICIAL (offset
) = 1;
2658 DECL_ARG_TYPE (offset
) = TREE_VALUE (typelist
);
2659 TREE_READONLY (offset
) = 1;
2660 hidden_arglist
= chainon (hidden_arglist
, offset
);
2661 gfc_finish_decl (offset
);
2664 arglist
= chainon (arglist
, parm
);
2665 typelist
= TREE_CHAIN (typelist
);
2668 /* Add the hidden string length parameters, unless the procedure
2670 if (!sym
->attr
.is_bind_c
)
2671 arglist
= chainon (arglist
, hidden_arglist
);
2673 gcc_assert (hidden_typelist
== NULL_TREE
2674 || TREE_VALUE (hidden_typelist
) == void_type_node
);
2675 DECL_ARGUMENTS (fndecl
) = arglist
;
2678 /* Do the setup necessary before generating the body of a function. */
2681 trans_function_start (gfc_symbol
* sym
)
2685 fndecl
= sym
->backend_decl
;
2687 /* Let GCC know the current scope is this function. */
2688 current_function_decl
= fndecl
;
2690 /* Let the world know what we're about to do. */
2691 announce_function (fndecl
);
2693 if (DECL_FILE_SCOPE_P (fndecl
))
2695 /* Create RTL for function declaration. */
2696 rest_of_decl_compilation (fndecl
, 1, 0);
2699 /* Create RTL for function definition. */
2700 make_decl_rtl (fndecl
);
2702 allocate_struct_function (fndecl
, false);
2704 /* function.c requires a push at the start of the function. */
2708 /* Create thunks for alternate entry points. */
2711 build_entry_thunks (gfc_namespace
* ns
, bool global
)
2713 gfc_formal_arglist
*formal
;
2714 gfc_formal_arglist
*thunk_formal
;
2716 gfc_symbol
*thunk_sym
;
2722 /* This should always be a toplevel function. */
2723 gcc_assert (current_function_decl
== NULL_TREE
);
2725 gfc_save_backend_locus (&old_loc
);
2726 for (el
= ns
->entries
; el
; el
= el
->next
)
2728 vec
<tree
, va_gc
> *args
= NULL
;
2729 vec
<tree
, va_gc
> *string_args
= NULL
;
2731 thunk_sym
= el
->sym
;
2733 build_function_decl (thunk_sym
, global
);
2734 create_function_arglist (thunk_sym
);
2736 trans_function_start (thunk_sym
);
2738 thunk_fndecl
= thunk_sym
->backend_decl
;
2740 gfc_init_block (&body
);
2742 /* Pass extra parameter identifying this entry point. */
2743 tmp
= build_int_cst (gfc_array_index_type
, el
->id
);
2744 vec_safe_push (args
, tmp
);
2746 if (thunk_sym
->attr
.function
)
2748 if (gfc_return_by_reference (ns
->proc_name
))
2750 tree ref
= DECL_ARGUMENTS (current_function_decl
);
2751 vec_safe_push (args
, ref
);
2752 if (ns
->proc_name
->ts
.type
== BT_CHARACTER
)
2753 vec_safe_push (args
, DECL_CHAIN (ref
));
2757 for (formal
= gfc_sym_get_dummy_args (ns
->proc_name
); formal
;
2758 formal
= formal
->next
)
2760 /* Ignore alternate returns. */
2761 if (formal
->sym
== NULL
)
2764 /* We don't have a clever way of identifying arguments, so resort to
2765 a brute-force search. */
2766 for (thunk_formal
= gfc_sym_get_dummy_args (thunk_sym
);
2768 thunk_formal
= thunk_formal
->next
)
2770 if (thunk_formal
->sym
== formal
->sym
)
2776 /* Pass the argument. */
2777 DECL_ARTIFICIAL (thunk_formal
->sym
->backend_decl
) = 1;
2778 vec_safe_push (args
, thunk_formal
->sym
->backend_decl
);
2779 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
2781 tmp
= thunk_formal
->sym
->ts
.u
.cl
->backend_decl
;
2782 vec_safe_push (string_args
, tmp
);
2787 /* Pass NULL for a missing argument. */
2788 vec_safe_push (args
, null_pointer_node
);
2789 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
2791 tmp
= build_int_cst (gfc_charlen_type_node
, 0);
2792 vec_safe_push (string_args
, tmp
);
2797 /* Call the master function. */
2798 vec_safe_splice (args
, string_args
);
2799 tmp
= ns
->proc_name
->backend_decl
;
2800 tmp
= build_call_expr_loc_vec (input_location
, tmp
, args
);
2801 if (ns
->proc_name
->attr
.mixed_entry_master
)
2803 tree union_decl
, field
;
2804 tree master_type
= TREE_TYPE (ns
->proc_name
->backend_decl
);
2806 union_decl
= build_decl (input_location
,
2807 VAR_DECL
, get_identifier ("__result"),
2808 TREE_TYPE (master_type
));
2809 DECL_ARTIFICIAL (union_decl
) = 1;
2810 DECL_EXTERNAL (union_decl
) = 0;
2811 TREE_PUBLIC (union_decl
) = 0;
2812 TREE_USED (union_decl
) = 1;
2813 layout_decl (union_decl
, 0);
2814 pushdecl (union_decl
);
2816 DECL_CONTEXT (union_decl
) = current_function_decl
;
2817 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
2818 TREE_TYPE (union_decl
), union_decl
, tmp
);
2819 gfc_add_expr_to_block (&body
, tmp
);
2821 for (field
= TYPE_FIELDS (TREE_TYPE (union_decl
));
2822 field
; field
= DECL_CHAIN (field
))
2823 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field
)),
2824 thunk_sym
->result
->name
) == 0)
2826 gcc_assert (field
!= NULL_TREE
);
2827 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
,
2828 TREE_TYPE (field
), union_decl
, field
,
2830 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
2831 TREE_TYPE (DECL_RESULT (current_function_decl
)),
2832 DECL_RESULT (current_function_decl
), tmp
);
2833 tmp
= build1_v (RETURN_EXPR
, tmp
);
2835 else if (TREE_TYPE (DECL_RESULT (current_function_decl
))
2838 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
2839 TREE_TYPE (DECL_RESULT (current_function_decl
)),
2840 DECL_RESULT (current_function_decl
), tmp
);
2841 tmp
= build1_v (RETURN_EXPR
, tmp
);
2843 gfc_add_expr_to_block (&body
, tmp
);
2845 /* Finish off this function and send it for code generation. */
2846 DECL_SAVED_TREE (thunk_fndecl
) = gfc_finish_block (&body
);
2849 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl
)) = thunk_fndecl
;
2850 DECL_SAVED_TREE (thunk_fndecl
)
2851 = build3_v (BIND_EXPR
, tmp
, DECL_SAVED_TREE (thunk_fndecl
),
2852 DECL_INITIAL (thunk_fndecl
));
2854 /* Output the GENERIC tree. */
2855 dump_function (TDI_original
, thunk_fndecl
);
2857 /* Store the end of the function, so that we get good line number
2858 info for the epilogue. */
2859 cfun
->function_end_locus
= input_location
;
2861 /* We're leaving the context of this function, so zap cfun.
2862 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
2863 tree_rest_of_compilation. */
2866 current_function_decl
= NULL_TREE
;
2868 cgraph_node::finalize_function (thunk_fndecl
, true);
2870 /* We share the symbols in the formal argument list with other entry
2871 points and the master function. Clear them so that they are
2872 recreated for each function. */
2873 for (formal
= gfc_sym_get_dummy_args (thunk_sym
); formal
;
2874 formal
= formal
->next
)
2875 if (formal
->sym
!= NULL
) /* Ignore alternate returns. */
2877 formal
->sym
->backend_decl
= NULL_TREE
;
2878 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
2879 formal
->sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
2882 if (thunk_sym
->attr
.function
)
2884 if (thunk_sym
->ts
.type
== BT_CHARACTER
)
2885 thunk_sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
2886 if (thunk_sym
->result
->ts
.type
== BT_CHARACTER
)
2887 thunk_sym
->result
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
2891 gfc_restore_backend_locus (&old_loc
);
2895 /* Create a decl for a function, and create any thunks for alternate entry
2896 points. If global is true, generate the function in the global binding
2897 level, otherwise in the current binding level (which can be global). */
2900 gfc_create_function_decl (gfc_namespace
* ns
, bool global
)
2902 /* Create a declaration for the master function. */
2903 build_function_decl (ns
->proc_name
, global
);
2905 /* Compile the entry thunks. */
2907 build_entry_thunks (ns
, global
);
2909 /* Now create the read argument list. */
2910 create_function_arglist (ns
->proc_name
);
2912 if (ns
->omp_declare_simd
)
2913 gfc_trans_omp_declare_simd (ns
);
2916 /* Return the decl used to hold the function return value. If
2917 parent_flag is set, the context is the parent_scope. */
2920 gfc_get_fake_result_decl (gfc_symbol
* sym
, int parent_flag
)
2924 tree this_fake_result_decl
;
2925 tree this_function_decl
;
2927 char name
[GFC_MAX_SYMBOL_LEN
+ 10];
2931 this_fake_result_decl
= parent_fake_result_decl
;
2932 this_function_decl
= DECL_CONTEXT (current_function_decl
);
2936 this_fake_result_decl
= current_fake_result_decl
;
2937 this_function_decl
= current_function_decl
;
2941 && sym
->ns
->proc_name
->backend_decl
== this_function_decl
2942 && sym
->ns
->proc_name
->attr
.entry_master
2943 && sym
!= sym
->ns
->proc_name
)
2946 if (this_fake_result_decl
!= NULL
)
2947 for (t
= TREE_CHAIN (this_fake_result_decl
); t
; t
= TREE_CHAIN (t
))
2948 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t
)), sym
->name
) == 0)
2951 return TREE_VALUE (t
);
2952 decl
= gfc_get_fake_result_decl (sym
->ns
->proc_name
, parent_flag
);
2955 this_fake_result_decl
= parent_fake_result_decl
;
2957 this_fake_result_decl
= current_fake_result_decl
;
2959 if (decl
&& sym
->ns
->proc_name
->attr
.mixed_entry_master
)
2963 for (field
= TYPE_FIELDS (TREE_TYPE (decl
));
2964 field
; field
= DECL_CHAIN (field
))
2965 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field
)),
2969 gcc_assert (field
!= NULL_TREE
);
2970 decl
= fold_build3_loc (input_location
, COMPONENT_REF
,
2971 TREE_TYPE (field
), decl
, field
, NULL_TREE
);
2974 var
= create_tmp_var_raw (TREE_TYPE (decl
), sym
->name
);
2976 gfc_add_decl_to_parent_function (var
);
2978 gfc_add_decl_to_function (var
);
2980 SET_DECL_VALUE_EXPR (var
, decl
);
2981 DECL_HAS_VALUE_EXPR_P (var
) = 1;
2982 GFC_DECL_RESULT (var
) = 1;
2984 TREE_CHAIN (this_fake_result_decl
)
2985 = tree_cons (get_identifier (sym
->name
), var
,
2986 TREE_CHAIN (this_fake_result_decl
));
2990 if (this_fake_result_decl
!= NULL_TREE
)
2991 return TREE_VALUE (this_fake_result_decl
);
2993 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
2998 if (sym
->ts
.type
== BT_CHARACTER
)
3000 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
3001 length
= gfc_create_string_length (sym
);
3003 length
= sym
->ts
.u
.cl
->backend_decl
;
3004 if (VAR_P (length
) && DECL_CONTEXT (length
) == NULL_TREE
)
3005 gfc_add_decl_to_function (length
);
3008 if (gfc_return_by_reference (sym
))
3010 decl
= DECL_ARGUMENTS (this_function_decl
);
3012 if (sym
->ns
->proc_name
->backend_decl
== this_function_decl
3013 && sym
->ns
->proc_name
->attr
.entry_master
)
3014 decl
= DECL_CHAIN (decl
);
3016 TREE_USED (decl
) = 1;
3018 decl
= gfc_build_dummy_array_decl (sym
, decl
);
3022 sprintf (name
, "__result_%.20s",
3023 IDENTIFIER_POINTER (DECL_NAME (this_function_decl
)));
3025 if (!sym
->attr
.mixed_entry_master
&& sym
->attr
.function
)
3026 decl
= build_decl (DECL_SOURCE_LOCATION (this_function_decl
),
3027 VAR_DECL
, get_identifier (name
),
3028 gfc_sym_type (sym
));
3030 decl
= build_decl (DECL_SOURCE_LOCATION (this_function_decl
),
3031 VAR_DECL
, get_identifier (name
),
3032 TREE_TYPE (TREE_TYPE (this_function_decl
)));
3033 DECL_ARTIFICIAL (decl
) = 1;
3034 DECL_EXTERNAL (decl
) = 0;
3035 TREE_PUBLIC (decl
) = 0;
3036 TREE_USED (decl
) = 1;
3037 GFC_DECL_RESULT (decl
) = 1;
3038 TREE_ADDRESSABLE (decl
) = 1;
3040 layout_decl (decl
, 0);
3041 gfc_finish_decl_attrs (decl
, &sym
->attr
);
3044 gfc_add_decl_to_parent_function (decl
);
3046 gfc_add_decl_to_function (decl
);
3050 parent_fake_result_decl
= build_tree_list (NULL
, decl
);
3052 current_fake_result_decl
= build_tree_list (NULL
, decl
);
3058 /* Builds a function decl. The remaining parameters are the types of the
3059 function arguments. Negative nargs indicates a varargs function. */
3062 build_library_function_decl_1 (tree name
, const char *spec
,
3063 tree rettype
, int nargs
, va_list p
)
3065 vec
<tree
, va_gc
> *arglist
;
3070 /* Library functions must be declared with global scope. */
3071 gcc_assert (current_function_decl
== NULL_TREE
);
3073 /* Create a list of the argument types. */
3074 vec_alloc (arglist
, abs (nargs
));
3075 for (n
= abs (nargs
); n
> 0; n
--)
3077 tree argtype
= va_arg (p
, tree
);
3078 arglist
->quick_push (argtype
);
3081 /* Build the function type and decl. */
3083 fntype
= build_function_type_vec (rettype
, arglist
);
3085 fntype
= build_varargs_function_type_vec (rettype
, arglist
);
3088 tree attr_args
= build_tree_list (NULL_TREE
,
3089 build_string (strlen (spec
), spec
));
3090 tree attrs
= tree_cons (get_identifier ("fn spec"),
3091 attr_args
, TYPE_ATTRIBUTES (fntype
));
3092 fntype
= build_type_attribute_variant (fntype
, attrs
);
3094 fndecl
= build_decl (input_location
,
3095 FUNCTION_DECL
, name
, fntype
);
3097 /* Mark this decl as external. */
3098 DECL_EXTERNAL (fndecl
) = 1;
3099 TREE_PUBLIC (fndecl
) = 1;
3103 rest_of_decl_compilation (fndecl
, 1, 0);
3108 /* Builds a function decl. The remaining parameters are the types of the
3109 function arguments. Negative nargs indicates a varargs function. */
3112 gfc_build_library_function_decl (tree name
, tree rettype
, int nargs
, ...)
3116 va_start (args
, nargs
);
3117 ret
= build_library_function_decl_1 (name
, NULL
, rettype
, nargs
, args
);
3122 /* Builds a function decl. The remaining parameters are the types of the
3123 function arguments. Negative nargs indicates a varargs function.
3124 The SPEC parameter specifies the function argument and return type
3125 specification according to the fnspec function type attribute. */
3128 gfc_build_library_function_decl_with_spec (tree name
, const char *spec
,
3129 tree rettype
, int nargs
, ...)
3133 va_start (args
, nargs
);
3134 ret
= build_library_function_decl_1 (name
, spec
, rettype
, nargs
, args
);
3140 gfc_build_intrinsic_function_decls (void)
3142 tree gfc_int4_type_node
= gfc_get_int_type (4);
3143 tree gfc_pint4_type_node
= build_pointer_type (gfc_int4_type_node
);
3144 tree gfc_int8_type_node
= gfc_get_int_type (8);
3145 tree gfc_pint8_type_node
= build_pointer_type (gfc_int8_type_node
);
3146 tree gfc_int16_type_node
= gfc_get_int_type (16);
3147 tree gfc_logical4_type_node
= gfc_get_logical_type (4);
3148 tree pchar1_type_node
= gfc_get_pchar_type (1);
3149 tree pchar4_type_node
= gfc_get_pchar_type (4);
3151 /* String functions. */
3152 gfor_fndecl_compare_string
= gfc_build_library_function_decl_with_spec (
3153 get_identifier (PREFIX("compare_string")), "..R.R",
3154 integer_type_node
, 4, gfc_charlen_type_node
, pchar1_type_node
,
3155 gfc_charlen_type_node
, pchar1_type_node
);
3156 DECL_PURE_P (gfor_fndecl_compare_string
) = 1;
3157 TREE_NOTHROW (gfor_fndecl_compare_string
) = 1;
3159 gfor_fndecl_concat_string
= gfc_build_library_function_decl_with_spec (
3160 get_identifier (PREFIX("concat_string")), "..W.R.R",
3161 void_type_node
, 6, gfc_charlen_type_node
, pchar1_type_node
,
3162 gfc_charlen_type_node
, pchar1_type_node
,
3163 gfc_charlen_type_node
, pchar1_type_node
);
3164 TREE_NOTHROW (gfor_fndecl_concat_string
) = 1;
3166 gfor_fndecl_string_len_trim
= gfc_build_library_function_decl_with_spec (
3167 get_identifier (PREFIX("string_len_trim")), "..R",
3168 gfc_charlen_type_node
, 2, gfc_charlen_type_node
, pchar1_type_node
);
3169 DECL_PURE_P (gfor_fndecl_string_len_trim
) = 1;
3170 TREE_NOTHROW (gfor_fndecl_string_len_trim
) = 1;
3172 gfor_fndecl_string_index
= gfc_build_library_function_decl_with_spec (
3173 get_identifier (PREFIX("string_index")), "..R.R.",
3174 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
3175 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
3176 DECL_PURE_P (gfor_fndecl_string_index
) = 1;
3177 TREE_NOTHROW (gfor_fndecl_string_index
) = 1;
3179 gfor_fndecl_string_scan
= gfc_build_library_function_decl_with_spec (
3180 get_identifier (PREFIX("string_scan")), "..R.R.",
3181 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
3182 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
3183 DECL_PURE_P (gfor_fndecl_string_scan
) = 1;
3184 TREE_NOTHROW (gfor_fndecl_string_scan
) = 1;
3186 gfor_fndecl_string_verify
= gfc_build_library_function_decl_with_spec (
3187 get_identifier (PREFIX("string_verify")), "..R.R.",
3188 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
3189 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
3190 DECL_PURE_P (gfor_fndecl_string_verify
) = 1;
3191 TREE_NOTHROW (gfor_fndecl_string_verify
) = 1;
3193 gfor_fndecl_string_trim
= gfc_build_library_function_decl_with_spec (
3194 get_identifier (PREFIX("string_trim")), ".Ww.R",
3195 void_type_node
, 4, build_pointer_type (gfc_charlen_type_node
),
3196 build_pointer_type (pchar1_type_node
), gfc_charlen_type_node
,
3199 gfor_fndecl_string_minmax
= gfc_build_library_function_decl_with_spec (
3200 get_identifier (PREFIX("string_minmax")), ".Ww.R",
3201 void_type_node
, -4, build_pointer_type (gfc_charlen_type_node
),
3202 build_pointer_type (pchar1_type_node
), integer_type_node
,
3205 gfor_fndecl_adjustl
= gfc_build_library_function_decl_with_spec (
3206 get_identifier (PREFIX("adjustl")), ".W.R",
3207 void_type_node
, 3, pchar1_type_node
, gfc_charlen_type_node
,
3209 TREE_NOTHROW (gfor_fndecl_adjustl
) = 1;
3211 gfor_fndecl_adjustr
= gfc_build_library_function_decl_with_spec (
3212 get_identifier (PREFIX("adjustr")), ".W.R",
3213 void_type_node
, 3, pchar1_type_node
, gfc_charlen_type_node
,
3215 TREE_NOTHROW (gfor_fndecl_adjustr
) = 1;
3217 gfor_fndecl_select_string
= gfc_build_library_function_decl_with_spec (
3218 get_identifier (PREFIX("select_string")), ".R.R.",
3219 integer_type_node
, 4, pvoid_type_node
, integer_type_node
,
3220 pchar1_type_node
, gfc_charlen_type_node
);
3221 DECL_PURE_P (gfor_fndecl_select_string
) = 1;
3222 TREE_NOTHROW (gfor_fndecl_select_string
) = 1;
3224 gfor_fndecl_compare_string_char4
= gfc_build_library_function_decl_with_spec (
3225 get_identifier (PREFIX("compare_string_char4")), "..R.R",
3226 integer_type_node
, 4, gfc_charlen_type_node
, pchar4_type_node
,
3227 gfc_charlen_type_node
, pchar4_type_node
);
3228 DECL_PURE_P (gfor_fndecl_compare_string_char4
) = 1;
3229 TREE_NOTHROW (gfor_fndecl_compare_string_char4
) = 1;
3231 gfor_fndecl_concat_string_char4
= gfc_build_library_function_decl_with_spec (
3232 get_identifier (PREFIX("concat_string_char4")), "..W.R.R",
3233 void_type_node
, 6, gfc_charlen_type_node
, pchar4_type_node
,
3234 gfc_charlen_type_node
, pchar4_type_node
, gfc_charlen_type_node
,
3236 TREE_NOTHROW (gfor_fndecl_concat_string_char4
) = 1;
3238 gfor_fndecl_string_len_trim_char4
= gfc_build_library_function_decl_with_spec (
3239 get_identifier (PREFIX("string_len_trim_char4")), "..R",
3240 gfc_charlen_type_node
, 2, gfc_charlen_type_node
, pchar4_type_node
);
3241 DECL_PURE_P (gfor_fndecl_string_len_trim_char4
) = 1;
3242 TREE_NOTHROW (gfor_fndecl_string_len_trim_char4
) = 1;
3244 gfor_fndecl_string_index_char4
= gfc_build_library_function_decl_with_spec (
3245 get_identifier (PREFIX("string_index_char4")), "..R.R.",
3246 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
3247 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
3248 DECL_PURE_P (gfor_fndecl_string_index_char4
) = 1;
3249 TREE_NOTHROW (gfor_fndecl_string_index_char4
) = 1;
3251 gfor_fndecl_string_scan_char4
= gfc_build_library_function_decl_with_spec (
3252 get_identifier (PREFIX("string_scan_char4")), "..R.R.",
3253 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
3254 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
3255 DECL_PURE_P (gfor_fndecl_string_scan_char4
) = 1;
3256 TREE_NOTHROW (gfor_fndecl_string_scan_char4
) = 1;
3258 gfor_fndecl_string_verify_char4
= gfc_build_library_function_decl_with_spec (
3259 get_identifier (PREFIX("string_verify_char4")), "..R.R.",
3260 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
3261 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
3262 DECL_PURE_P (gfor_fndecl_string_verify_char4
) = 1;
3263 TREE_NOTHROW (gfor_fndecl_string_verify_char4
) = 1;
3265 gfor_fndecl_string_trim_char4
= gfc_build_library_function_decl_with_spec (
3266 get_identifier (PREFIX("string_trim_char4")), ".Ww.R",
3267 void_type_node
, 4, build_pointer_type (gfc_charlen_type_node
),
3268 build_pointer_type (pchar4_type_node
), gfc_charlen_type_node
,
3271 gfor_fndecl_string_minmax_char4
= gfc_build_library_function_decl_with_spec (
3272 get_identifier (PREFIX("string_minmax_char4")), ".Ww.R",
3273 void_type_node
, -4, build_pointer_type (gfc_charlen_type_node
),
3274 build_pointer_type (pchar4_type_node
), integer_type_node
,
3277 gfor_fndecl_adjustl_char4
= gfc_build_library_function_decl_with_spec (
3278 get_identifier (PREFIX("adjustl_char4")), ".W.R",
3279 void_type_node
, 3, pchar4_type_node
, gfc_charlen_type_node
,
3281 TREE_NOTHROW (gfor_fndecl_adjustl_char4
) = 1;
3283 gfor_fndecl_adjustr_char4
= gfc_build_library_function_decl_with_spec (
3284 get_identifier (PREFIX("adjustr_char4")), ".W.R",
3285 void_type_node
, 3, pchar4_type_node
, gfc_charlen_type_node
,
3287 TREE_NOTHROW (gfor_fndecl_adjustr_char4
) = 1;
3289 gfor_fndecl_select_string_char4
= gfc_build_library_function_decl_with_spec (
3290 get_identifier (PREFIX("select_string_char4")), ".R.R.",
3291 integer_type_node
, 4, pvoid_type_node
, integer_type_node
,
3292 pvoid_type_node
, gfc_charlen_type_node
);
3293 DECL_PURE_P (gfor_fndecl_select_string_char4
) = 1;
3294 TREE_NOTHROW (gfor_fndecl_select_string_char4
) = 1;
3297 /* Conversion between character kinds. */
3299 gfor_fndecl_convert_char1_to_char4
= gfc_build_library_function_decl_with_spec (
3300 get_identifier (PREFIX("convert_char1_to_char4")), ".w.R",
3301 void_type_node
, 3, build_pointer_type (pchar4_type_node
),
3302 gfc_charlen_type_node
, pchar1_type_node
);
3304 gfor_fndecl_convert_char4_to_char1
= gfc_build_library_function_decl_with_spec (
3305 get_identifier (PREFIX("convert_char4_to_char1")), ".w.R",
3306 void_type_node
, 3, build_pointer_type (pchar1_type_node
),
3307 gfc_charlen_type_node
, pchar4_type_node
);
3309 /* Misc. functions. */
3311 gfor_fndecl_ttynam
= gfc_build_library_function_decl_with_spec (
3312 get_identifier (PREFIX("ttynam")), ".W",
3313 void_type_node
, 3, pchar_type_node
, gfc_charlen_type_node
,
3316 gfor_fndecl_fdate
= gfc_build_library_function_decl_with_spec (
3317 get_identifier (PREFIX("fdate")), ".W",
3318 void_type_node
, 2, pchar_type_node
, gfc_charlen_type_node
);
3320 gfor_fndecl_ctime
= gfc_build_library_function_decl_with_spec (
3321 get_identifier (PREFIX("ctime")), ".W",
3322 void_type_node
, 3, pchar_type_node
, gfc_charlen_type_node
,
3323 gfc_int8_type_node
);
3325 gfor_fndecl_sc_kind
= gfc_build_library_function_decl_with_spec (
3326 get_identifier (PREFIX("selected_char_kind")), "..R",
3327 gfc_int4_type_node
, 2, gfc_charlen_type_node
, pchar_type_node
);
3328 DECL_PURE_P (gfor_fndecl_sc_kind
) = 1;
3329 TREE_NOTHROW (gfor_fndecl_sc_kind
) = 1;
3331 gfor_fndecl_si_kind
= gfc_build_library_function_decl_with_spec (
3332 get_identifier (PREFIX("selected_int_kind")), ".R",
3333 gfc_int4_type_node
, 1, pvoid_type_node
);
3334 DECL_PURE_P (gfor_fndecl_si_kind
) = 1;
3335 TREE_NOTHROW (gfor_fndecl_si_kind
) = 1;
3337 gfor_fndecl_sr_kind
= gfc_build_library_function_decl_with_spec (
3338 get_identifier (PREFIX("selected_real_kind2008")), ".RR",
3339 gfc_int4_type_node
, 3, pvoid_type_node
, pvoid_type_node
,
3341 DECL_PURE_P (gfor_fndecl_sr_kind
) = 1;
3342 TREE_NOTHROW (gfor_fndecl_sr_kind
) = 1;
3344 gfor_fndecl_system_clock4
= gfc_build_library_function_decl (
3345 get_identifier (PREFIX("system_clock_4")),
3346 void_type_node
, 3, gfc_pint4_type_node
, gfc_pint4_type_node
,
3347 gfc_pint4_type_node
);
3349 gfor_fndecl_system_clock8
= gfc_build_library_function_decl (
3350 get_identifier (PREFIX("system_clock_8")),
3351 void_type_node
, 3, gfc_pint8_type_node
, gfc_pint8_type_node
,
3352 gfc_pint8_type_node
);
3354 /* Power functions. */
3356 tree ctype
, rtype
, itype
, jtype
;
3357 int rkind
, ikind
, jkind
;
3360 static int ikinds
[NIKINDS
] = {4, 8, 16};
3361 static int rkinds
[NRKINDS
] = {4, 8, 10, 16};
3362 char name
[PREFIX_LEN
+ 12]; /* _gfortran_pow_?n_?n */
3364 for (ikind
=0; ikind
< NIKINDS
; ikind
++)
3366 itype
= gfc_get_int_type (ikinds
[ikind
]);
3368 for (jkind
=0; jkind
< NIKINDS
; jkind
++)
3370 jtype
= gfc_get_int_type (ikinds
[jkind
]);
3373 sprintf (name
, PREFIX("pow_i%d_i%d"), ikinds
[ikind
],
3375 gfor_fndecl_math_powi
[jkind
][ikind
].integer
=
3376 gfc_build_library_function_decl (get_identifier (name
),
3377 jtype
, 2, jtype
, itype
);
3378 TREE_READONLY (gfor_fndecl_math_powi
[jkind
][ikind
].integer
) = 1;
3379 TREE_NOTHROW (gfor_fndecl_math_powi
[jkind
][ikind
].integer
) = 1;
3383 for (rkind
= 0; rkind
< NRKINDS
; rkind
++)
3385 rtype
= gfc_get_real_type (rkinds
[rkind
]);
3388 sprintf (name
, PREFIX("pow_r%d_i%d"), rkinds
[rkind
],
3390 gfor_fndecl_math_powi
[rkind
][ikind
].real
=
3391 gfc_build_library_function_decl (get_identifier (name
),
3392 rtype
, 2, rtype
, itype
);
3393 TREE_READONLY (gfor_fndecl_math_powi
[rkind
][ikind
].real
) = 1;
3394 TREE_NOTHROW (gfor_fndecl_math_powi
[rkind
][ikind
].real
) = 1;
3397 ctype
= gfc_get_complex_type (rkinds
[rkind
]);
3400 sprintf (name
, PREFIX("pow_c%d_i%d"), rkinds
[rkind
],
3402 gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
=
3403 gfc_build_library_function_decl (get_identifier (name
),
3404 ctype
, 2,ctype
, itype
);
3405 TREE_READONLY (gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
) = 1;
3406 TREE_NOTHROW (gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
) = 1;
3414 gfor_fndecl_math_ishftc4
= gfc_build_library_function_decl (
3415 get_identifier (PREFIX("ishftc4")),
3416 gfc_int4_type_node
, 3, gfc_int4_type_node
, gfc_int4_type_node
,
3417 gfc_int4_type_node
);
3418 TREE_READONLY (gfor_fndecl_math_ishftc4
) = 1;
3419 TREE_NOTHROW (gfor_fndecl_math_ishftc4
) = 1;
3421 gfor_fndecl_math_ishftc8
= gfc_build_library_function_decl (
3422 get_identifier (PREFIX("ishftc8")),
3423 gfc_int8_type_node
, 3, gfc_int8_type_node
, gfc_int4_type_node
,
3424 gfc_int4_type_node
);
3425 TREE_READONLY (gfor_fndecl_math_ishftc8
) = 1;
3426 TREE_NOTHROW (gfor_fndecl_math_ishftc8
) = 1;
3428 if (gfc_int16_type_node
)
3430 gfor_fndecl_math_ishftc16
= gfc_build_library_function_decl (
3431 get_identifier (PREFIX("ishftc16")),
3432 gfc_int16_type_node
, 3, gfc_int16_type_node
, gfc_int4_type_node
,
3433 gfc_int4_type_node
);
3434 TREE_READONLY (gfor_fndecl_math_ishftc16
) = 1;
3435 TREE_NOTHROW (gfor_fndecl_math_ishftc16
) = 1;
3438 /* BLAS functions. */
3440 tree pint
= build_pointer_type (integer_type_node
);
3441 tree ps
= build_pointer_type (gfc_get_real_type (gfc_default_real_kind
));
3442 tree pd
= build_pointer_type (gfc_get_real_type (gfc_default_double_kind
));
3443 tree pc
= build_pointer_type (gfc_get_complex_type (gfc_default_real_kind
));
3444 tree pz
= build_pointer_type
3445 (gfc_get_complex_type (gfc_default_double_kind
));
3447 gfor_fndecl_sgemm
= gfc_build_library_function_decl
3449 (flag_underscoring
? "sgemm_" : "sgemm"),
3450 void_type_node
, 15, pchar_type_node
,
3451 pchar_type_node
, pint
, pint
, pint
, ps
, ps
, pint
,
3452 ps
, pint
, ps
, ps
, pint
, integer_type_node
,
3454 gfor_fndecl_dgemm
= gfc_build_library_function_decl
3456 (flag_underscoring
? "dgemm_" : "dgemm"),
3457 void_type_node
, 15, pchar_type_node
,
3458 pchar_type_node
, pint
, pint
, pint
, pd
, pd
, pint
,
3459 pd
, pint
, pd
, pd
, pint
, integer_type_node
,
3461 gfor_fndecl_cgemm
= gfc_build_library_function_decl
3463 (flag_underscoring
? "cgemm_" : "cgemm"),
3464 void_type_node
, 15, pchar_type_node
,
3465 pchar_type_node
, pint
, pint
, pint
, pc
, pc
, pint
,
3466 pc
, pint
, pc
, pc
, pint
, integer_type_node
,
3468 gfor_fndecl_zgemm
= gfc_build_library_function_decl
3470 (flag_underscoring
? "zgemm_" : "zgemm"),
3471 void_type_node
, 15, pchar_type_node
,
3472 pchar_type_node
, pint
, pint
, pint
, pz
, pz
, pint
,
3473 pz
, pint
, pz
, pz
, pint
, integer_type_node
,
3477 /* Other functions. */
3478 gfor_fndecl_size0
= gfc_build_library_function_decl_with_spec (
3479 get_identifier (PREFIX("size0")), ".R",
3480 gfc_array_index_type
, 1, pvoid_type_node
);
3481 DECL_PURE_P (gfor_fndecl_size0
) = 1;
3482 TREE_NOTHROW (gfor_fndecl_size0
) = 1;
3484 gfor_fndecl_size1
= gfc_build_library_function_decl_with_spec (
3485 get_identifier (PREFIX("size1")), ".R",
3486 gfc_array_index_type
, 2, pvoid_type_node
, gfc_array_index_type
);
3487 DECL_PURE_P (gfor_fndecl_size1
) = 1;
3488 TREE_NOTHROW (gfor_fndecl_size1
) = 1;
3490 gfor_fndecl_iargc
= gfc_build_library_function_decl (
3491 get_identifier (PREFIX ("iargc")), gfc_int4_type_node
, 0);
3492 TREE_NOTHROW (gfor_fndecl_iargc
) = 1;
3496 /* Make prototypes for runtime library functions. */
3499 gfc_build_builtin_function_decls (void)
3501 tree gfc_int4_type_node
= gfc_get_int_type (4);
3503 gfor_fndecl_stop_numeric
= gfc_build_library_function_decl (
3504 get_identifier (PREFIX("stop_numeric")),
3505 void_type_node
, 1, gfc_int4_type_node
);
3506 /* STOP doesn't return. */
3507 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric
) = 1;
3509 gfor_fndecl_stop_string
= gfc_build_library_function_decl_with_spec (
3510 get_identifier (PREFIX("stop_string")), ".R.",
3511 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
3512 /* STOP doesn't return. */
3513 TREE_THIS_VOLATILE (gfor_fndecl_stop_string
) = 1;
3515 gfor_fndecl_error_stop_numeric
= gfc_build_library_function_decl (
3516 get_identifier (PREFIX("error_stop_numeric")),
3517 void_type_node
, 1, gfc_int4_type_node
);
3518 /* ERROR STOP doesn't return. */
3519 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_numeric
) = 1;
3521 gfor_fndecl_error_stop_string
= gfc_build_library_function_decl_with_spec (
3522 get_identifier (PREFIX("error_stop_string")), ".R.",
3523 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
3524 /* ERROR STOP doesn't return. */
3525 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_string
) = 1;
3527 gfor_fndecl_pause_numeric
= gfc_build_library_function_decl (
3528 get_identifier (PREFIX("pause_numeric")),
3529 void_type_node
, 1, gfc_int4_type_node
);
3531 gfor_fndecl_pause_string
= gfc_build_library_function_decl_with_spec (
3532 get_identifier (PREFIX("pause_string")), ".R.",
3533 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
3535 gfor_fndecl_runtime_error
= gfc_build_library_function_decl_with_spec (
3536 get_identifier (PREFIX("runtime_error")), ".R",
3537 void_type_node
, -1, pchar_type_node
);
3538 /* The runtime_error function does not return. */
3539 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error
) = 1;
3541 gfor_fndecl_runtime_error_at
= gfc_build_library_function_decl_with_spec (
3542 get_identifier (PREFIX("runtime_error_at")), ".RR",
3543 void_type_node
, -2, pchar_type_node
, pchar_type_node
);
3544 /* The runtime_error_at function does not return. */
3545 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at
) = 1;
3547 gfor_fndecl_runtime_warning_at
= gfc_build_library_function_decl_with_spec (
3548 get_identifier (PREFIX("runtime_warning_at")), ".RR",
3549 void_type_node
, -2, pchar_type_node
, pchar_type_node
);
3551 gfor_fndecl_generate_error
= gfc_build_library_function_decl_with_spec (
3552 get_identifier (PREFIX("generate_error")), ".R.R",
3553 void_type_node
, 3, pvoid_type_node
, integer_type_node
,
3556 gfor_fndecl_os_error
= gfc_build_library_function_decl_with_spec (
3557 get_identifier (PREFIX("os_error")), ".R",
3558 void_type_node
, 1, pchar_type_node
);
3559 /* The runtime_error function does not return. */
3560 TREE_THIS_VOLATILE (gfor_fndecl_os_error
) = 1;
3562 gfor_fndecl_set_args
= gfc_build_library_function_decl (
3563 get_identifier (PREFIX("set_args")),
3564 void_type_node
, 2, integer_type_node
,
3565 build_pointer_type (pchar_type_node
));
3567 gfor_fndecl_set_fpe
= gfc_build_library_function_decl (
3568 get_identifier (PREFIX("set_fpe")),
3569 void_type_node
, 1, integer_type_node
);
3571 gfor_fndecl_ieee_procedure_entry
= gfc_build_library_function_decl (
3572 get_identifier (PREFIX("ieee_procedure_entry")),
3573 void_type_node
, 1, pvoid_type_node
);
3575 gfor_fndecl_ieee_procedure_exit
= gfc_build_library_function_decl (
3576 get_identifier (PREFIX("ieee_procedure_exit")),
3577 void_type_node
, 1, pvoid_type_node
);
3579 /* Keep the array dimension in sync with the call, later in this file. */
3580 gfor_fndecl_set_options
= gfc_build_library_function_decl_with_spec (
3581 get_identifier (PREFIX("set_options")), "..R",
3582 void_type_node
, 2, integer_type_node
,
3583 build_pointer_type (integer_type_node
));
3585 gfor_fndecl_set_convert
= gfc_build_library_function_decl (
3586 get_identifier (PREFIX("set_convert")),
3587 void_type_node
, 1, integer_type_node
);
3589 gfor_fndecl_set_record_marker
= gfc_build_library_function_decl (
3590 get_identifier (PREFIX("set_record_marker")),
3591 void_type_node
, 1, integer_type_node
);
3593 gfor_fndecl_set_max_subrecord_length
= gfc_build_library_function_decl (
3594 get_identifier (PREFIX("set_max_subrecord_length")),
3595 void_type_node
, 1, integer_type_node
);
3597 gfor_fndecl_in_pack
= gfc_build_library_function_decl_with_spec (
3598 get_identifier (PREFIX("internal_pack")), ".r",
3599 pvoid_type_node
, 1, pvoid_type_node
);
3601 gfor_fndecl_in_unpack
= gfc_build_library_function_decl_with_spec (
3602 get_identifier (PREFIX("internal_unpack")), ".wR",
3603 void_type_node
, 2, pvoid_type_node
, pvoid_type_node
);
3605 gfor_fndecl_associated
= gfc_build_library_function_decl_with_spec (
3606 get_identifier (PREFIX("associated")), ".RR",
3607 integer_type_node
, 2, ppvoid_type_node
, ppvoid_type_node
);
3608 DECL_PURE_P (gfor_fndecl_associated
) = 1;
3609 TREE_NOTHROW (gfor_fndecl_associated
) = 1;
3611 /* Coarray library calls. */
3612 if (flag_coarray
== GFC_FCOARRAY_LIB
)
3614 tree pint_type
, pppchar_type
;
3616 pint_type
= build_pointer_type (integer_type_node
);
3618 = build_pointer_type (build_pointer_type (pchar_type_node
));
3620 gfor_fndecl_caf_init
= gfc_build_library_function_decl (
3621 get_identifier (PREFIX("caf_init")), void_type_node
,
3622 2, pint_type
, pppchar_type
);
3624 gfor_fndecl_caf_finalize
= gfc_build_library_function_decl (
3625 get_identifier (PREFIX("caf_finalize")), void_type_node
, 0);
3627 gfor_fndecl_caf_this_image
= gfc_build_library_function_decl (
3628 get_identifier (PREFIX("caf_this_image")), integer_type_node
,
3629 1, integer_type_node
);
3631 gfor_fndecl_caf_num_images
= gfc_build_library_function_decl (
3632 get_identifier (PREFIX("caf_num_images")), integer_type_node
,
3633 2, integer_type_node
, integer_type_node
);
3635 gfor_fndecl_caf_register
= gfc_build_library_function_decl_with_spec (
3636 get_identifier (PREFIX("caf_register")), "RRWWWWR", void_type_node
, 7,
3637 size_type_node
, integer_type_node
, ppvoid_type_node
, pvoid_type_node
,
3638 pint_type
, pchar_type_node
, integer_type_node
);
3640 gfor_fndecl_caf_deregister
= gfc_build_library_function_decl_with_spec (
3641 get_identifier (PREFIX("caf_deregister")), "WRWWR", void_type_node
, 5,
3642 ppvoid_type_node
, integer_type_node
, pint_type
, pchar_type_node
,
3645 gfor_fndecl_caf_get
= gfc_build_library_function_decl_with_spec (
3646 get_identifier (PREFIX("caf_get")), ".R.RRWRRRW", void_type_node
, 10,
3647 pvoid_type_node
, size_type_node
, integer_type_node
, pvoid_type_node
,
3648 pvoid_type_node
, pvoid_type_node
, integer_type_node
, integer_type_node
,
3649 boolean_type_node
, pint_type
);
3651 gfor_fndecl_caf_send
= gfc_build_library_function_decl_with_spec (
3652 get_identifier (PREFIX("caf_send")), ".R.RRRRRRWR", void_type_node
, 11,
3653 pvoid_type_node
, size_type_node
, integer_type_node
, pvoid_type_node
,
3654 pvoid_type_node
, pvoid_type_node
, integer_type_node
, integer_type_node
,
3655 boolean_type_node
, pint_type
, pvoid_type_node
);
3657 gfor_fndecl_caf_sendget
= gfc_build_library_function_decl_with_spec (
3658 get_identifier (PREFIX("caf_sendget")), ".R.RRRR.RRRRRR",
3659 void_type_node
, 14, pvoid_type_node
, size_type_node
, integer_type_node
,
3660 pvoid_type_node
, pvoid_type_node
, pvoid_type_node
, size_type_node
,
3661 integer_type_node
, pvoid_type_node
, pvoid_type_node
, integer_type_node
,
3662 integer_type_node
, boolean_type_node
, integer_type_node
);
3664 gfor_fndecl_caf_get_by_ref
= gfc_build_library_function_decl_with_spec (
3665 get_identifier (PREFIX("caf_get_by_ref")), ".RWRRRRRW", void_type_node
,
3666 9, pvoid_type_node
, integer_type_node
, pvoid_type_node
, pvoid_type_node
,
3667 integer_type_node
, integer_type_node
, boolean_type_node
,
3668 boolean_type_node
, pint_type
);
3670 gfor_fndecl_caf_send_by_ref
= gfc_build_library_function_decl_with_spec (
3671 get_identifier (PREFIX("caf_send_by_ref")), ".RRRRRRRW", void_type_node
,
3672 9, pvoid_type_node
, integer_type_node
, pvoid_type_node
, pvoid_type_node
,
3673 integer_type_node
, integer_type_node
, boolean_type_node
,
3674 boolean_type_node
, pint_type
);
3676 gfor_fndecl_caf_sendget_by_ref
3677 = gfc_build_library_function_decl_with_spec (
3678 get_identifier (PREFIX("caf_sendget_by_ref")), ".RR.RRRRRWW",
3679 void_type_node
, 11, pvoid_type_node
, integer_type_node
,
3680 pvoid_type_node
, pvoid_type_node
, integer_type_node
,
3681 pvoid_type_node
, integer_type_node
, integer_type_node
,
3682 boolean_type_node
, pint_type
, pint_type
);
3684 gfor_fndecl_caf_sync_all
= gfc_build_library_function_decl_with_spec (
3685 get_identifier (PREFIX("caf_sync_all")), ".WW", void_type_node
,
3686 3, pint_type
, pchar_type_node
, integer_type_node
);
3688 gfor_fndecl_caf_sync_memory
= gfc_build_library_function_decl_with_spec (
3689 get_identifier (PREFIX("caf_sync_memory")), ".WW", void_type_node
,
3690 3, pint_type
, pchar_type_node
, integer_type_node
);
3692 gfor_fndecl_caf_sync_images
= gfc_build_library_function_decl_with_spec (
3693 get_identifier (PREFIX("caf_sync_images")), ".RRWW", void_type_node
,
3694 5, integer_type_node
, pint_type
, pint_type
,
3695 pchar_type_node
, integer_type_node
);
3697 gfor_fndecl_caf_error_stop
= gfc_build_library_function_decl (
3698 get_identifier (PREFIX("caf_error_stop")),
3699 void_type_node
, 1, gfc_int4_type_node
);
3700 /* CAF's ERROR STOP doesn't return. */
3701 TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop
) = 1;
3703 gfor_fndecl_caf_error_stop_str
= gfc_build_library_function_decl_with_spec (
3704 get_identifier (PREFIX("caf_error_stop_str")), ".R.",
3705 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
3706 /* CAF's ERROR STOP doesn't return. */
3707 TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop_str
) = 1;
3709 gfor_fndecl_caf_stop_numeric
= gfc_build_library_function_decl_with_spec (
3710 get_identifier (PREFIX("caf_stop_numeric")), ".R.",
3711 void_type_node
, 1, gfc_int4_type_node
);
3712 /* CAF's STOP doesn't return. */
3713 TREE_THIS_VOLATILE (gfor_fndecl_caf_stop_numeric
) = 1;
3715 gfor_fndecl_caf_stop_str
= gfc_build_library_function_decl_with_spec (
3716 get_identifier (PREFIX("caf_stop_str")), ".R.",
3717 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
3718 /* CAF's STOP doesn't return. */
3719 TREE_THIS_VOLATILE (gfor_fndecl_caf_stop_str
) = 1;
3721 gfor_fndecl_caf_atomic_def
= gfc_build_library_function_decl_with_spec (
3722 get_identifier (PREFIX("caf_atomic_define")), "R..RW",
3723 void_type_node
, 7, pvoid_type_node
, size_type_node
, integer_type_node
,
3724 pvoid_type_node
, pint_type
, integer_type_node
, integer_type_node
);
3726 gfor_fndecl_caf_atomic_ref
= gfc_build_library_function_decl_with_spec (
3727 get_identifier (PREFIX("caf_atomic_ref")), "R..WW",
3728 void_type_node
, 7, pvoid_type_node
, size_type_node
, integer_type_node
,
3729 pvoid_type_node
, pint_type
, integer_type_node
, integer_type_node
);
3731 gfor_fndecl_caf_atomic_cas
= gfc_build_library_function_decl_with_spec (
3732 get_identifier (PREFIX("caf_atomic_cas")), "R..WRRW",
3733 void_type_node
, 9, pvoid_type_node
, size_type_node
, integer_type_node
,
3734 pvoid_type_node
, pvoid_type_node
, pvoid_type_node
, pint_type
,
3735 integer_type_node
, integer_type_node
);
3737 gfor_fndecl_caf_atomic_op
= gfc_build_library_function_decl_with_spec (
3738 get_identifier (PREFIX("caf_atomic_op")), ".R..RWW",
3739 void_type_node
, 9, integer_type_node
, pvoid_type_node
, size_type_node
,
3740 integer_type_node
, pvoid_type_node
, pvoid_type_node
, pint_type
,
3741 integer_type_node
, integer_type_node
);
3743 gfor_fndecl_caf_lock
= gfc_build_library_function_decl_with_spec (
3744 get_identifier (PREFIX("caf_lock")), "R..WWW",
3745 void_type_node
, 7, pvoid_type_node
, size_type_node
, integer_type_node
,
3746 pint_type
, pint_type
, pchar_type_node
, integer_type_node
);
3748 gfor_fndecl_caf_unlock
= gfc_build_library_function_decl_with_spec (
3749 get_identifier (PREFIX("caf_unlock")), "R..WW",
3750 void_type_node
, 6, pvoid_type_node
, size_type_node
, integer_type_node
,
3751 pint_type
, pchar_type_node
, integer_type_node
);
3753 gfor_fndecl_caf_event_post
= gfc_build_library_function_decl_with_spec (
3754 get_identifier (PREFIX("caf_event_post")), "R..WW",
3755 void_type_node
, 6, pvoid_type_node
, size_type_node
, integer_type_node
,
3756 pint_type
, pchar_type_node
, integer_type_node
);
3758 gfor_fndecl_caf_event_wait
= gfc_build_library_function_decl_with_spec (
3759 get_identifier (PREFIX("caf_event_wait")), "R..WW",
3760 void_type_node
, 6, pvoid_type_node
, size_type_node
, integer_type_node
,
3761 pint_type
, pchar_type_node
, integer_type_node
);
3763 gfor_fndecl_caf_event_query
= gfc_build_library_function_decl_with_spec (
3764 get_identifier (PREFIX("caf_event_query")), "R..WW",
3765 void_type_node
, 5, pvoid_type_node
, size_type_node
, integer_type_node
,
3766 pint_type
, pint_type
);
3768 gfor_fndecl_caf_fail_image
= gfc_build_library_function_decl (
3769 get_identifier (PREFIX("caf_fail_image")), void_type_node
, 0);
3770 /* CAF's FAIL doesn't return. */
3771 TREE_THIS_VOLATILE (gfor_fndecl_caf_fail_image
) = 1;
3773 gfor_fndecl_caf_failed_images
3774 = gfc_build_library_function_decl_with_spec (
3775 get_identifier (PREFIX("caf_failed_images")), "WRR",
3776 void_type_node
, 3, pvoid_type_node
, ppvoid_type_node
,
3779 gfor_fndecl_caf_form_team
3780 = gfc_build_library_function_decl_with_spec (
3781 get_identifier (PREFIX("caf_form_team")), "RWR",
3782 void_type_node
, 3, integer_type_node
, ppvoid_type_node
,
3785 gfor_fndecl_caf_change_team
3786 = gfc_build_library_function_decl_with_spec (
3787 get_identifier (PREFIX("caf_change_team")), "RR",
3788 void_type_node
, 2, ppvoid_type_node
,
3791 gfor_fndecl_caf_end_team
3792 = gfc_build_library_function_decl (
3793 get_identifier (PREFIX("caf_end_team")), void_type_node
, 0);
3795 gfor_fndecl_caf_get_team
3796 = gfc_build_library_function_decl_with_spec (
3797 get_identifier (PREFIX("caf_get_team")), "R",
3798 void_type_node
, 1, integer_type_node
);
3800 gfor_fndecl_caf_sync_team
3801 = gfc_build_library_function_decl_with_spec (
3802 get_identifier (PREFIX("caf_sync_team")), "RR",
3803 void_type_node
, 2, ppvoid_type_node
,
3806 gfor_fndecl_caf_team_number
3807 = gfc_build_library_function_decl_with_spec (
3808 get_identifier (PREFIX("caf_team_number")), "R",
3809 integer_type_node
, 1, integer_type_node
);
3811 gfor_fndecl_caf_image_status
3812 = gfc_build_library_function_decl_with_spec (
3813 get_identifier (PREFIX("caf_image_status")), "RR",
3814 integer_type_node
, 2, integer_type_node
, ppvoid_type_node
);
3816 gfor_fndecl_caf_stopped_images
3817 = gfc_build_library_function_decl_with_spec (
3818 get_identifier (PREFIX("caf_stopped_images")), "WRR",
3819 void_type_node
, 3, pvoid_type_node
, ppvoid_type_node
,
3822 gfor_fndecl_co_broadcast
= gfc_build_library_function_decl_with_spec (
3823 get_identifier (PREFIX("caf_co_broadcast")), "W.WW",
3824 void_type_node
, 5, pvoid_type_node
, integer_type_node
,
3825 pint_type
, pchar_type_node
, integer_type_node
);
3827 gfor_fndecl_co_max
= gfc_build_library_function_decl_with_spec (
3828 get_identifier (PREFIX("caf_co_max")), "W.WW",
3829 void_type_node
, 6, pvoid_type_node
, integer_type_node
,
3830 pint_type
, pchar_type_node
, integer_type_node
, integer_type_node
);
3832 gfor_fndecl_co_min
= gfc_build_library_function_decl_with_spec (
3833 get_identifier (PREFIX("caf_co_min")), "W.WW",
3834 void_type_node
, 6, pvoid_type_node
, integer_type_node
,
3835 pint_type
, pchar_type_node
, integer_type_node
, integer_type_node
);
3837 gfor_fndecl_co_reduce
= gfc_build_library_function_decl_with_spec (
3838 get_identifier (PREFIX("caf_co_reduce")), "W.R.WW",
3839 void_type_node
, 8, pvoid_type_node
,
3840 build_pointer_type (build_varargs_function_type_list (void_type_node
,
3842 integer_type_node
, integer_type_node
, pint_type
, pchar_type_node
,
3843 integer_type_node
, integer_type_node
);
3845 gfor_fndecl_co_sum
= gfc_build_library_function_decl_with_spec (
3846 get_identifier (PREFIX("caf_co_sum")), "W.WW",
3847 void_type_node
, 5, pvoid_type_node
, integer_type_node
,
3848 pint_type
, pchar_type_node
, integer_type_node
);
3850 gfor_fndecl_caf_is_present
= gfc_build_library_function_decl_with_spec (
3851 get_identifier (PREFIX("caf_is_present")), "RRR",
3852 integer_type_node
, 3, pvoid_type_node
, integer_type_node
,
3856 gfc_build_intrinsic_function_decls ();
3857 gfc_build_intrinsic_lib_fndecls ();
3858 gfc_build_io_library_fndecls ();
3862 /* Evaluate the length of dummy character variables. */
3865 gfc_trans_dummy_character (gfc_symbol
*sym
, gfc_charlen
*cl
,
3866 gfc_wrapped_block
*block
)
3870 gfc_finish_decl (cl
->backend_decl
);
3872 gfc_start_block (&init
);
3874 /* Evaluate the string length expression. */
3875 gfc_conv_string_length (cl
, NULL
, &init
);
3877 gfc_trans_vla_type_sizes (sym
, &init
);
3879 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
3883 /* Allocate and cleanup an automatic character variable. */
3886 gfc_trans_auto_character_variable (gfc_symbol
* sym
, gfc_wrapped_block
* block
)
3892 gcc_assert (sym
->backend_decl
);
3893 gcc_assert (sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
);
3895 gfc_init_block (&init
);
3897 /* Evaluate the string length expression. */
3898 gfc_conv_string_length (sym
->ts
.u
.cl
, NULL
, &init
);
3900 gfc_trans_vla_type_sizes (sym
, &init
);
3902 decl
= sym
->backend_decl
;
3904 /* Emit a DECL_EXPR for this variable, which will cause the
3905 gimplifier to allocate storage, and all that good stuff. */
3906 tmp
= fold_build1_loc (input_location
, DECL_EXPR
, TREE_TYPE (decl
), decl
);
3907 gfc_add_expr_to_block (&init
, tmp
);
3909 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
3912 /* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
3915 gfc_trans_assign_aux_var (gfc_symbol
* sym
, gfc_wrapped_block
* block
)
3919 gcc_assert (sym
->backend_decl
);
3920 gfc_start_block (&init
);
3922 /* Set the initial value to length. See the comments in
3923 function gfc_add_assign_aux_vars in this file. */
3924 gfc_add_modify (&init
, GFC_DECL_STRING_LEN (sym
->backend_decl
),
3925 build_int_cst (gfc_charlen_type_node
, -2));
3927 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
3931 gfc_trans_vla_one_sizepos (tree
*tp
, stmtblock_t
*body
)
3933 tree t
= *tp
, var
, val
;
3935 if (t
== NULL
|| t
== error_mark_node
)
3937 if (TREE_CONSTANT (t
) || DECL_P (t
))
3940 if (TREE_CODE (t
) == SAVE_EXPR
)
3942 if (SAVE_EXPR_RESOLVED_P (t
))
3944 *tp
= TREE_OPERAND (t
, 0);
3947 val
= TREE_OPERAND (t
, 0);
3952 var
= gfc_create_var_np (TREE_TYPE (t
), NULL
);
3953 gfc_add_decl_to_function (var
);
3954 gfc_add_modify (body
, var
, unshare_expr (val
));
3955 if (TREE_CODE (t
) == SAVE_EXPR
)
3956 TREE_OPERAND (t
, 0) = var
;
3961 gfc_trans_vla_type_sizes_1 (tree type
, stmtblock_t
*body
)
3965 if (type
== NULL
|| type
== error_mark_node
)
3968 type
= TYPE_MAIN_VARIANT (type
);
3970 if (TREE_CODE (type
) == INTEGER_TYPE
)
3972 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type
), body
);
3973 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type
), body
);
3975 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
3977 TYPE_MIN_VALUE (t
) = TYPE_MIN_VALUE (type
);
3978 TYPE_MAX_VALUE (t
) = TYPE_MAX_VALUE (type
);
3981 else if (TREE_CODE (type
) == ARRAY_TYPE
)
3983 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type
), body
);
3984 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type
), body
);
3985 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type
), body
);
3986 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type
), body
);
3988 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
3990 TYPE_SIZE (t
) = TYPE_SIZE (type
);
3991 TYPE_SIZE_UNIT (t
) = TYPE_SIZE_UNIT (type
);
3996 /* Make sure all type sizes and array domains are either constant,
3997 or variable or parameter decls. This is a simplified variant
3998 of gimplify_type_sizes, but we can't use it here, as none of the
3999 variables in the expressions have been gimplified yet.
4000 As type sizes and domains for various variable length arrays
4001 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
4002 time, without this routine gimplify_type_sizes in the middle-end
4003 could result in the type sizes being gimplified earlier than where
4004 those variables are initialized. */
4007 gfc_trans_vla_type_sizes (gfc_symbol
*sym
, stmtblock_t
*body
)
4009 tree type
= TREE_TYPE (sym
->backend_decl
);
4011 if (TREE_CODE (type
) == FUNCTION_TYPE
4012 && (sym
->attr
.function
|| sym
->attr
.result
|| sym
->attr
.entry
))
4014 if (! current_fake_result_decl
)
4017 type
= TREE_TYPE (TREE_VALUE (current_fake_result_decl
));
4020 while (POINTER_TYPE_P (type
))
4021 type
= TREE_TYPE (type
);
4023 if (GFC_DESCRIPTOR_TYPE_P (type
))
4025 tree etype
= GFC_TYPE_ARRAY_DATAPTR_TYPE (type
);
4027 while (POINTER_TYPE_P (etype
))
4028 etype
= TREE_TYPE (etype
);
4030 gfc_trans_vla_type_sizes_1 (etype
, body
);
4033 gfc_trans_vla_type_sizes_1 (type
, body
);
4037 /* Initialize a derived type by building an lvalue from the symbol
4038 and using trans_assignment to do the work. Set dealloc to false
4039 if no deallocation prior the assignment is needed. */
4041 gfc_init_default_dt (gfc_symbol
* sym
, stmtblock_t
* block
, bool dealloc
)
4049 /* Initialization of PDTs is done elsewhere. */
4050 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.pdt_type
)
4053 gcc_assert (!sym
->attr
.allocatable
);
4054 gfc_set_sym_referenced (sym
);
4055 e
= gfc_lval_expr_from_sym (sym
);
4056 tmp
= gfc_trans_assignment (e
, sym
->value
, false, dealloc
);
4057 if (sym
->attr
.dummy
&& (sym
->attr
.optional
4058 || sym
->ns
->proc_name
->attr
.entry_master
))
4060 present
= gfc_conv_expr_present (sym
);
4061 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
), present
,
4062 tmp
, build_empty_stmt (input_location
));
4064 gfc_add_expr_to_block (block
, tmp
);
4069 /* Initialize INTENT(OUT) derived type dummies. As well as giving
4070 them their default initializer, if they do not have allocatable
4071 components, they have their allocatable components deallocated. */
4074 init_intent_out_dt (gfc_symbol
* proc_sym
, gfc_wrapped_block
* block
)
4077 gfc_formal_arglist
*f
;
4081 gfc_init_block (&init
);
4082 for (f
= gfc_sym_get_dummy_args (proc_sym
); f
; f
= f
->next
)
4083 if (f
->sym
&& f
->sym
->attr
.intent
== INTENT_OUT
4084 && !f
->sym
->attr
.pointer
4085 && f
->sym
->ts
.type
== BT_DERIVED
)
4089 /* Note: Allocatables are excluded as they are already handled
4091 if (!f
->sym
->attr
.allocatable
4092 && gfc_is_finalizable (f
->sym
->ts
.u
.derived
, NULL
))
4097 gfc_init_block (&block
);
4098 f
->sym
->attr
.referenced
= 1;
4099 e
= gfc_lval_expr_from_sym (f
->sym
);
4100 gfc_add_finalizer_call (&block
, e
);
4102 tmp
= gfc_finish_block (&block
);
4105 if (tmp
== NULL_TREE
&& !f
->sym
->attr
.allocatable
4106 && f
->sym
->ts
.u
.derived
->attr
.alloc_comp
&& !f
->sym
->value
)
4107 tmp
= gfc_deallocate_alloc_comp (f
->sym
->ts
.u
.derived
,
4108 f
->sym
->backend_decl
,
4109 f
->sym
->as
? f
->sym
->as
->rank
: 0);
4111 if (tmp
!= NULL_TREE
&& (f
->sym
->attr
.optional
4112 || f
->sym
->ns
->proc_name
->attr
.entry_master
))
4114 present
= gfc_conv_expr_present (f
->sym
);
4115 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
),
4116 present
, tmp
, build_empty_stmt (input_location
));
4119 if (tmp
!= NULL_TREE
)
4120 gfc_add_expr_to_block (&init
, tmp
);
4121 else if (f
->sym
->value
&& !f
->sym
->attr
.allocatable
)
4122 gfc_init_default_dt (f
->sym
, &init
, true);
4124 else if (f
->sym
&& f
->sym
->attr
.intent
== INTENT_OUT
4125 && f
->sym
->ts
.type
== BT_CLASS
4126 && !CLASS_DATA (f
->sym
)->attr
.class_pointer
4127 && !CLASS_DATA (f
->sym
)->attr
.allocatable
)
4132 gfc_init_block (&block
);
4133 f
->sym
->attr
.referenced
= 1;
4134 e
= gfc_lval_expr_from_sym (f
->sym
);
4135 gfc_add_finalizer_call (&block
, e
);
4137 tmp
= gfc_finish_block (&block
);
4139 if (f
->sym
->attr
.optional
|| f
->sym
->ns
->proc_name
->attr
.entry_master
)
4141 present
= gfc_conv_expr_present (f
->sym
);
4142 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
),
4144 build_empty_stmt (input_location
));
4147 gfc_add_expr_to_block (&init
, tmp
);
4150 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
4154 /* Helper function to manage deferred string lengths. */
4157 gfc_null_and_pass_deferred_len (gfc_symbol
*sym
, stmtblock_t
*init
,
4162 /* Character length passed by reference. */
4163 tmp
= sym
->ts
.u
.cl
->passed_length
;
4164 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
4165 tmp
= fold_convert (gfc_charlen_type_node
, tmp
);
4167 if (!sym
->attr
.dummy
|| sym
->attr
.intent
== INTENT_OUT
)
4168 /* Zero the string length when entering the scope. */
4169 gfc_add_modify (init
, sym
->ts
.u
.cl
->backend_decl
,
4170 build_int_cst (gfc_charlen_type_node
, 0));
4175 tmp2
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4176 gfc_charlen_type_node
,
4177 sym
->ts
.u
.cl
->backend_decl
, tmp
);
4178 if (sym
->attr
.optional
)
4180 tree present
= gfc_conv_expr_present (sym
);
4181 tmp2
= build3_loc (input_location
, COND_EXPR
,
4182 void_type_node
, present
, tmp2
,
4183 build_empty_stmt (input_location
));
4185 gfc_add_expr_to_block (init
, tmp2
);
4188 gfc_restore_backend_locus (loc
);
4190 /* Pass the final character length back. */
4191 if (sym
->attr
.intent
!= INTENT_IN
)
4193 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4194 gfc_charlen_type_node
, tmp
,
4195 sym
->ts
.u
.cl
->backend_decl
);
4196 if (sym
->attr
.optional
)
4198 tree present
= gfc_conv_expr_present (sym
);
4199 tmp
= build3_loc (input_location
, COND_EXPR
,
4200 void_type_node
, present
, tmp
,
4201 build_empty_stmt (input_location
));
4211 /* Get the result expression for a procedure. */
4214 get_proc_result (gfc_symbol
* sym
)
4216 if (sym
->attr
.subroutine
|| sym
== sym
->result
)
4218 if (current_fake_result_decl
!= NULL
)
4219 return TREE_VALUE (current_fake_result_decl
);
4224 return sym
->result
->backend_decl
;
4228 /* Generate function entry and exit code, and add it to the function body.
4230 Allocation and initialization of array variables.
4231 Allocation of character string variables.
4232 Initialization and possibly repacking of dummy arrays.
4233 Initialization of ASSIGN statement auxiliary variable.
4234 Initialization of ASSOCIATE names.
4235 Automatic deallocation. */
4238 gfc_trans_deferred_vars (gfc_symbol
* proc_sym
, gfc_wrapped_block
* block
)
4242 gfc_formal_arglist
*f
;
4243 stmtblock_t tmpblock
;
4244 bool seen_trans_deferred_array
= false;
4245 bool is_pdt_type
= false;
4251 /* Deal with implicit return variables. Explicit return variables will
4252 already have been added. */
4253 if (gfc_return_by_reference (proc_sym
) && proc_sym
->result
== proc_sym
)
4255 if (!current_fake_result_decl
)
4257 gfc_entry_list
*el
= NULL
;
4258 if (proc_sym
->attr
.entry_master
)
4260 for (el
= proc_sym
->ns
->entries
; el
; el
= el
->next
)
4261 if (el
->sym
!= el
->sym
->result
)
4264 /* TODO: move to the appropriate place in resolve.c. */
4265 if (warn_return_type
> 0 && el
== NULL
)
4266 gfc_warning (OPT_Wreturn_type
,
4267 "Return value of function %qs at %L not set",
4268 proc_sym
->name
, &proc_sym
->declared_at
);
4270 else if (proc_sym
->as
)
4272 tree result
= TREE_VALUE (current_fake_result_decl
);
4273 gfc_save_backend_locus (&loc
);
4274 gfc_set_backend_locus (&proc_sym
->declared_at
);
4275 gfc_trans_dummy_array_bias (proc_sym
, result
, block
);
4277 /* An automatic character length, pointer array result. */
4278 if (proc_sym
->ts
.type
== BT_CHARACTER
4279 && VAR_P (proc_sym
->ts
.u
.cl
->backend_decl
))
4282 if (proc_sym
->ts
.deferred
)
4284 gfc_start_block (&init
);
4285 tmp
= gfc_null_and_pass_deferred_len (proc_sym
, &init
, &loc
);
4286 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
4289 gfc_trans_dummy_character (proc_sym
, proc_sym
->ts
.u
.cl
, block
);
4292 else if (proc_sym
->ts
.type
== BT_CHARACTER
)
4294 if (proc_sym
->ts
.deferred
)
4297 gfc_save_backend_locus (&loc
);
4298 gfc_set_backend_locus (&proc_sym
->declared_at
);
4299 gfc_start_block (&init
);
4300 /* Zero the string length on entry. */
4301 gfc_add_modify (&init
, proc_sym
->ts
.u
.cl
->backend_decl
,
4302 build_int_cst (gfc_charlen_type_node
, 0));
4303 /* Null the pointer. */
4304 e
= gfc_lval_expr_from_sym (proc_sym
);
4305 gfc_init_se (&se
, NULL
);
4306 se
.want_pointer
= 1;
4307 gfc_conv_expr (&se
, e
);
4310 gfc_add_modify (&init
, tmp
,
4311 fold_convert (TREE_TYPE (se
.expr
),
4312 null_pointer_node
));
4313 gfc_restore_backend_locus (&loc
);
4315 /* Pass back the string length on exit. */
4316 tmp
= proc_sym
->ts
.u
.cl
->backend_decl
;
4317 if (TREE_CODE (tmp
) != INDIRECT_REF
4318 && proc_sym
->ts
.u
.cl
->passed_length
)
4320 tmp
= proc_sym
->ts
.u
.cl
->passed_length
;
4321 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
4322 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4323 TREE_TYPE (tmp
), tmp
,
4326 proc_sym
->ts
.u
.cl
->backend_decl
));
4331 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
4333 else if (VAR_P (proc_sym
->ts
.u
.cl
->backend_decl
))
4334 gfc_trans_dummy_character (proc_sym
, proc_sym
->ts
.u
.cl
, block
);
4337 gcc_assert (flag_f2c
&& proc_sym
->ts
.type
== BT_COMPLEX
);
4339 else if (proc_sym
== proc_sym
->result
&& IS_CLASS_ARRAY (proc_sym
))
4341 /* Nullify explicit return class arrays on entry. */
4343 tmp
= get_proc_result (proc_sym
);
4344 if (tmp
&& GFC_CLASS_TYPE_P (TREE_TYPE (tmp
)))
4346 gfc_start_block (&init
);
4347 tmp
= gfc_class_data_get (tmp
);
4348 type
= TREE_TYPE (gfc_conv_descriptor_data_get (tmp
));
4349 gfc_conv_descriptor_data_set (&init
, tmp
, build_int_cst (type
, 0));
4350 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
4355 /* Initialize the INTENT(OUT) derived type dummy arguments. This
4356 should be done here so that the offsets and lbounds of arrays
4358 gfc_save_backend_locus (&loc
);
4359 gfc_set_backend_locus (&proc_sym
->declared_at
);
4360 init_intent_out_dt (proc_sym
, block
);
4361 gfc_restore_backend_locus (&loc
);
4363 for (sym
= proc_sym
->tlink
; sym
!= proc_sym
; sym
= sym
->tlink
)
4365 bool alloc_comp_or_fini
= (sym
->ts
.type
== BT_DERIVED
)
4366 && (sym
->ts
.u
.derived
->attr
.alloc_comp
4367 || gfc_is_finalizable (sym
->ts
.u
.derived
,
4372 if (sym
->ts
.type
== BT_DERIVED
4373 && sym
->ts
.u
.derived
4374 && sym
->ts
.u
.derived
->attr
.pdt_type
)
4377 gfc_init_block (&tmpblock
);
4378 if (!(sym
->attr
.dummy
4379 || sym
->attr
.pointer
4380 || sym
->attr
.allocatable
))
4382 tmp
= gfc_allocate_pdt_comp (sym
->ts
.u
.derived
,
4384 sym
->as
? sym
->as
->rank
: 0,
4386 gfc_add_expr_to_block (&tmpblock
, tmp
);
4387 if (!sym
->attr
.result
)
4388 tmp
= gfc_deallocate_pdt_comp (sym
->ts
.u
.derived
,
4390 sym
->as
? sym
->as
->rank
: 0);
4393 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
), tmp
);
4395 else if (sym
->attr
.dummy
)
4397 tmp
= gfc_check_pdt_dummy (sym
->ts
.u
.derived
,
4399 sym
->as
? sym
->as
->rank
: 0,
4401 gfc_add_expr_to_block (&tmpblock
, tmp
);
4402 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
), NULL
);
4405 else if (sym
->ts
.type
== BT_CLASS
4406 && CLASS_DATA (sym
)->ts
.u
.derived
4407 && CLASS_DATA (sym
)->ts
.u
.derived
->attr
.pdt_type
)
4409 gfc_component
*data
= CLASS_DATA (sym
);
4411 gfc_init_block (&tmpblock
);
4412 if (!(sym
->attr
.dummy
4413 || CLASS_DATA (sym
)->attr
.pointer
4414 || CLASS_DATA (sym
)->attr
.allocatable
))
4416 tmp
= gfc_class_data_get (sym
->backend_decl
);
4417 tmp
= gfc_allocate_pdt_comp (data
->ts
.u
.derived
, tmp
,
4418 data
->as
? data
->as
->rank
: 0,
4420 gfc_add_expr_to_block (&tmpblock
, tmp
);
4421 tmp
= gfc_class_data_get (sym
->backend_decl
);
4422 if (!sym
->attr
.result
)
4423 tmp
= gfc_deallocate_pdt_comp (data
->ts
.u
.derived
, tmp
,
4424 data
->as
? data
->as
->rank
: 0);
4427 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
), tmp
);
4429 else if (sym
->attr
.dummy
)
4431 tmp
= gfc_class_data_get (sym
->backend_decl
);
4432 tmp
= gfc_check_pdt_dummy (data
->ts
.u
.derived
, tmp
,
4433 data
->as
? data
->as
->rank
: 0,
4435 gfc_add_expr_to_block (&tmpblock
, tmp
);
4436 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
), NULL
);
4440 if (sym
->attr
.pointer
&& sym
->attr
.dimension
4441 && sym
->attr
.save
== SAVE_NONE
4442 && !sym
->attr
.use_assoc
4443 && !sym
->attr
.host_assoc
4445 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (sym
->backend_decl
)))
4447 gfc_init_block (&tmpblock
);
4448 gfc_conv_descriptor_span_set (&tmpblock
, sym
->backend_decl
,
4449 build_int_cst (gfc_array_index_type
, 0));
4450 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
),
4454 if (sym
->ts
.type
== BT_CLASS
4455 && (sym
->attr
.save
|| flag_max_stack_var_size
== 0)
4456 && CLASS_DATA (sym
)->attr
.allocatable
)
4460 if (UNLIMITED_POLY (sym
))
4461 vptr
= null_pointer_node
;
4465 vsym
= gfc_find_derived_vtab (sym
->ts
.u
.derived
);
4466 vptr
= gfc_get_symbol_decl (vsym
);
4467 vptr
= gfc_build_addr_expr (NULL
, vptr
);
4470 if (CLASS_DATA (sym
)->attr
.dimension
4471 || (CLASS_DATA (sym
)->attr
.codimension
4472 && flag_coarray
!= GFC_FCOARRAY_LIB
))
4474 tmp
= gfc_class_data_get (sym
->backend_decl
);
4475 tmp
= gfc_build_null_descriptor (TREE_TYPE (tmp
));
4478 tmp
= null_pointer_node
;
4480 DECL_INITIAL (sym
->backend_decl
)
4481 = gfc_class_set_static_fields (sym
->backend_decl
, vptr
, tmp
);
4482 TREE_CONSTANT (DECL_INITIAL (sym
->backend_decl
)) = 1;
4484 else if ((sym
->attr
.dimension
|| sym
->attr
.codimension
4485 || (IS_CLASS_ARRAY (sym
) && !CLASS_DATA (sym
)->attr
.allocatable
)))
4487 bool is_classarray
= IS_CLASS_ARRAY (sym
);
4488 symbol_attribute
*array_attr
;
4490 array_type type_of_array
;
4492 array_attr
= is_classarray
? &CLASS_DATA (sym
)->attr
: &sym
->attr
;
4493 as
= is_classarray
? CLASS_DATA (sym
)->as
: sym
->as
;
4494 /* Assumed-size Cray pointees need to be treated as AS_EXPLICIT. */
4495 type_of_array
= as
->type
;
4496 if (type_of_array
== AS_ASSUMED_SIZE
&& as
->cp_was_assumed
)
4497 type_of_array
= AS_EXPLICIT
;
4498 switch (type_of_array
)
4501 if (sym
->attr
.dummy
|| sym
->attr
.result
)
4502 gfc_trans_dummy_array_bias (sym
, sym
->backend_decl
, block
);
4503 /* Allocatable and pointer arrays need to processed
4505 else if ((sym
->ts
.type
!= BT_CLASS
&& sym
->attr
.pointer
)
4506 || (sym
->ts
.type
== BT_CLASS
4507 && CLASS_DATA (sym
)->attr
.class_pointer
)
4508 || array_attr
->allocatable
)
4510 if (TREE_STATIC (sym
->backend_decl
))
4512 gfc_save_backend_locus (&loc
);
4513 gfc_set_backend_locus (&sym
->declared_at
);
4514 gfc_trans_static_array_pointer (sym
);
4515 gfc_restore_backend_locus (&loc
);
4519 seen_trans_deferred_array
= true;
4520 gfc_trans_deferred_array (sym
, block
);
4523 else if (sym
->attr
.codimension
4524 && TREE_STATIC (sym
->backend_decl
))
4526 gfc_init_block (&tmpblock
);
4527 gfc_trans_array_cobounds (TREE_TYPE (sym
->backend_decl
),
4529 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
),
4535 gfc_save_backend_locus (&loc
);
4536 gfc_set_backend_locus (&sym
->declared_at
);
4538 if (alloc_comp_or_fini
)
4540 seen_trans_deferred_array
= true;
4541 gfc_trans_deferred_array (sym
, block
);
4543 else if (sym
->ts
.type
== BT_DERIVED
4546 && sym
->attr
.save
== SAVE_NONE
)
4548 gfc_start_block (&tmpblock
);
4549 gfc_init_default_dt (sym
, &tmpblock
, false);
4550 gfc_add_init_cleanup (block
,
4551 gfc_finish_block (&tmpblock
),
4555 gfc_trans_auto_array_allocation (sym
->backend_decl
,
4557 gfc_restore_backend_locus (&loc
);
4561 case AS_ASSUMED_SIZE
:
4562 /* Must be a dummy parameter. */
4563 gcc_assert (sym
->attr
.dummy
|| as
->cp_was_assumed
);
4565 /* We should always pass assumed size arrays the g77 way. */
4566 if (sym
->attr
.dummy
)
4567 gfc_trans_g77_array (sym
, block
);
4570 case AS_ASSUMED_SHAPE
:
4571 /* Must be a dummy parameter. */
4572 gcc_assert (sym
->attr
.dummy
);
4574 gfc_trans_dummy_array_bias (sym
, sym
->backend_decl
, block
);
4577 case AS_ASSUMED_RANK
:
4579 seen_trans_deferred_array
= true;
4580 gfc_trans_deferred_array (sym
, block
);
4581 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.deferred
4582 && sym
->attr
.result
)
4584 gfc_start_block (&init
);
4585 gfc_save_backend_locus (&loc
);
4586 gfc_set_backend_locus (&sym
->declared_at
);
4587 tmp
= gfc_null_and_pass_deferred_len (sym
, &init
, &loc
);
4588 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
4595 if (alloc_comp_or_fini
&& !seen_trans_deferred_array
)
4596 gfc_trans_deferred_array (sym
, block
);
4598 else if ((!sym
->attr
.dummy
|| sym
->ts
.deferred
)
4599 && (sym
->ts
.type
== BT_CLASS
4600 && CLASS_DATA (sym
)->attr
.class_pointer
))
4602 else if ((!sym
->attr
.dummy
|| sym
->ts
.deferred
)
4603 && (sym
->attr
.allocatable
4604 || (sym
->attr
.pointer
&& sym
->attr
.result
)
4605 || (sym
->ts
.type
== BT_CLASS
4606 && CLASS_DATA (sym
)->attr
.allocatable
)))
4608 if (!sym
->attr
.save
&& flag_max_stack_var_size
!= 0)
4610 tree descriptor
= NULL_TREE
;
4612 gfc_save_backend_locus (&loc
);
4613 gfc_set_backend_locus (&sym
->declared_at
);
4614 gfc_start_block (&init
);
4616 if (!sym
->attr
.pointer
)
4618 /* Nullify and automatic deallocation of allocatable
4620 e
= gfc_lval_expr_from_sym (sym
);
4621 if (sym
->ts
.type
== BT_CLASS
)
4622 gfc_add_data_component (e
);
4624 gfc_init_se (&se
, NULL
);
4625 if (sym
->ts
.type
!= BT_CLASS
4626 || sym
->ts
.u
.derived
->attr
.dimension
4627 || sym
->ts
.u
.derived
->attr
.codimension
)
4629 se
.want_pointer
= 1;
4630 gfc_conv_expr (&se
, e
);
4632 else if (sym
->ts
.type
== BT_CLASS
4633 && !CLASS_DATA (sym
)->attr
.dimension
4634 && !CLASS_DATA (sym
)->attr
.codimension
)
4636 se
.want_pointer
= 1;
4637 gfc_conv_expr (&se
, e
);
4641 se
.descriptor_only
= 1;
4642 gfc_conv_expr (&se
, e
);
4643 descriptor
= se
.expr
;
4644 se
.expr
= gfc_conv_descriptor_data_addr (se
.expr
);
4645 se
.expr
= build_fold_indirect_ref_loc (input_location
, se
.expr
);
4649 if (!sym
->attr
.dummy
|| sym
->attr
.intent
== INTENT_OUT
)
4651 /* Nullify when entering the scope. */
4652 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4653 TREE_TYPE (se
.expr
), se
.expr
,
4654 fold_convert (TREE_TYPE (se
.expr
),
4655 null_pointer_node
));
4656 if (sym
->attr
.optional
)
4658 tree present
= gfc_conv_expr_present (sym
);
4659 tmp
= build3_loc (input_location
, COND_EXPR
,
4660 void_type_node
, present
, tmp
,
4661 build_empty_stmt (input_location
));
4663 gfc_add_expr_to_block (&init
, tmp
);
4667 if ((sym
->attr
.dummy
|| sym
->attr
.result
)
4668 && sym
->ts
.type
== BT_CHARACTER
4670 && sym
->ts
.u
.cl
->passed_length
)
4671 tmp
= gfc_null_and_pass_deferred_len (sym
, &init
, &loc
);
4674 gfc_restore_backend_locus (&loc
);
4678 /* Deallocate when leaving the scope. Nullifying is not
4680 if (!sym
->attr
.result
&& !sym
->attr
.dummy
&& !sym
->attr
.pointer
4681 && !sym
->ns
->proc_name
->attr
.is_main_program
)
4683 if (sym
->ts
.type
== BT_CLASS
4684 && CLASS_DATA (sym
)->attr
.codimension
)
4685 tmp
= gfc_deallocate_with_status (descriptor
, NULL_TREE
,
4686 NULL_TREE
, NULL_TREE
,
4687 NULL_TREE
, true, NULL
,
4688 GFC_CAF_COARRAY_ANALYZE
);
4691 gfc_expr
*expr
= gfc_lval_expr_from_sym (sym
);
4692 tmp
= gfc_deallocate_scalar_with_status (se
.expr
,
4697 gfc_free_expr (expr
);
4701 if (sym
->ts
.type
== BT_CLASS
)
4703 /* Initialize _vptr to declared type. */
4707 gfc_save_backend_locus (&loc
);
4708 gfc_set_backend_locus (&sym
->declared_at
);
4709 e
= gfc_lval_expr_from_sym (sym
);
4710 gfc_add_vptr_component (e
);
4711 gfc_init_se (&se
, NULL
);
4712 se
.want_pointer
= 1;
4713 gfc_conv_expr (&se
, e
);
4715 if (UNLIMITED_POLY (sym
))
4716 rhs
= build_int_cst (TREE_TYPE (se
.expr
), 0);
4719 vtab
= gfc_find_derived_vtab (sym
->ts
.u
.derived
);
4720 rhs
= gfc_build_addr_expr (TREE_TYPE (se
.expr
),
4721 gfc_get_symbol_decl (vtab
));
4723 gfc_add_modify (&init
, se
.expr
, rhs
);
4724 gfc_restore_backend_locus (&loc
);
4727 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
4730 else if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.deferred
)
4735 /* If we get to here, all that should be left are pointers. */
4736 gcc_assert (sym
->attr
.pointer
);
4738 if (sym
->attr
.dummy
)
4740 gfc_start_block (&init
);
4741 gfc_save_backend_locus (&loc
);
4742 gfc_set_backend_locus (&sym
->declared_at
);
4743 tmp
= gfc_null_and_pass_deferred_len (sym
, &init
, &loc
);
4744 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
4747 else if (sym
->ts
.deferred
)
4748 gfc_fatal_error ("Deferred type parameter not yet supported");
4749 else if (alloc_comp_or_fini
)
4750 gfc_trans_deferred_array (sym
, block
);
4751 else if (sym
->ts
.type
== BT_CHARACTER
)
4753 gfc_save_backend_locus (&loc
);
4754 gfc_set_backend_locus (&sym
->declared_at
);
4755 if (sym
->attr
.dummy
|| sym
->attr
.result
)
4756 gfc_trans_dummy_character (sym
, sym
->ts
.u
.cl
, block
);
4758 gfc_trans_auto_character_variable (sym
, block
);
4759 gfc_restore_backend_locus (&loc
);
4761 else if (sym
->attr
.assign
)
4763 gfc_save_backend_locus (&loc
);
4764 gfc_set_backend_locus (&sym
->declared_at
);
4765 gfc_trans_assign_aux_var (sym
, block
);
4766 gfc_restore_backend_locus (&loc
);
4768 else if (sym
->ts
.type
== BT_DERIVED
4771 && sym
->attr
.save
== SAVE_NONE
)
4773 gfc_start_block (&tmpblock
);
4774 gfc_init_default_dt (sym
, &tmpblock
, false);
4775 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
),
4778 else if (!(UNLIMITED_POLY(sym
)) && !is_pdt_type
)
4782 gfc_init_block (&tmpblock
);
4784 for (f
= gfc_sym_get_dummy_args (proc_sym
); f
; f
= f
->next
)
4786 if (f
->sym
&& f
->sym
->tlink
== NULL
&& f
->sym
->ts
.type
== BT_CHARACTER
)
4788 gcc_assert (f
->sym
->ts
.u
.cl
->backend_decl
!= NULL
);
4789 if (TREE_CODE (f
->sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
4790 gfc_trans_vla_type_sizes (f
->sym
, &tmpblock
);
4794 if (gfc_return_by_reference (proc_sym
) && proc_sym
->ts
.type
== BT_CHARACTER
4795 && current_fake_result_decl
!= NULL
)
4797 gcc_assert (proc_sym
->ts
.u
.cl
->backend_decl
!= NULL
);
4798 if (TREE_CODE (proc_sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
4799 gfc_trans_vla_type_sizes (proc_sym
, &tmpblock
);
4802 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
), NULL_TREE
);
4806 struct module_hasher
: ggc_ptr_hash
<module_htab_entry
>
4808 typedef const char *compare_type
;
4810 static hashval_t
hash (module_htab_entry
*s
) { return htab_hash_string (s
); }
4812 equal (module_htab_entry
*a
, const char *b
)
4814 return !strcmp (a
->name
, b
);
4818 static GTY (()) hash_table
<module_hasher
> *module_htab
;
4820 /* Hash and equality functions for module_htab's decls. */
4823 module_decl_hasher::hash (tree t
)
4825 const_tree n
= DECL_NAME (t
);
4827 n
= TYPE_NAME (TREE_TYPE (t
));
4828 return htab_hash_string (IDENTIFIER_POINTER (n
));
4832 module_decl_hasher::equal (tree t1
, const char *x2
)
4834 const_tree n1
= DECL_NAME (t1
);
4835 if (n1
== NULL_TREE
)
4836 n1
= TYPE_NAME (TREE_TYPE (t1
));
4837 return strcmp (IDENTIFIER_POINTER (n1
), x2
) == 0;
4840 struct module_htab_entry
*
4841 gfc_find_module (const char *name
)
4844 module_htab
= hash_table
<module_hasher
>::create_ggc (10);
4846 module_htab_entry
**slot
4847 = module_htab
->find_slot_with_hash (name
, htab_hash_string (name
), INSERT
);
4850 module_htab_entry
*entry
= ggc_cleared_alloc
<module_htab_entry
> ();
4852 entry
->name
= gfc_get_string ("%s", name
);
4853 entry
->decls
= hash_table
<module_decl_hasher
>::create_ggc (10);
4860 gfc_module_add_decl (struct module_htab_entry
*entry
, tree decl
)
4864 if (DECL_NAME (decl
))
4865 name
= IDENTIFIER_POINTER (DECL_NAME (decl
));
4868 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
4869 name
= IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl
)));
4872 = entry
->decls
->find_slot_with_hash (name
, htab_hash_string (name
),
4879 /* Generate debugging symbols for namelists. This function must come after
4880 generate_local_decl to ensure that the variables in the namelist are
4881 already declared. */
4884 generate_namelist_decl (gfc_symbol
* sym
)
4888 vec
<constructor_elt
, va_gc
> *nml_decls
= NULL
;
4890 gcc_assert (sym
->attr
.flavor
== FL_NAMELIST
);
4891 for (nml
= sym
->namelist
; nml
; nml
= nml
->next
)
4893 if (nml
->sym
->backend_decl
== NULL_TREE
)
4895 nml
->sym
->attr
.referenced
= 1;
4896 nml
->sym
->backend_decl
= gfc_get_symbol_decl (nml
->sym
);
4898 DECL_IGNORED_P (nml
->sym
->backend_decl
) = 0;
4899 CONSTRUCTOR_APPEND_ELT (nml_decls
, NULL_TREE
, nml
->sym
->backend_decl
);
4902 decl
= make_node (NAMELIST_DECL
);
4903 TREE_TYPE (decl
) = void_type_node
;
4904 NAMELIST_DECL_ASSOCIATED_DECL (decl
) = build_constructor (NULL_TREE
, nml_decls
);
4905 DECL_NAME (decl
) = get_identifier (sym
->name
);
4910 /* Output an initialized decl for a module variable. */
4913 gfc_create_module_variable (gfc_symbol
* sym
)
4917 /* Module functions with alternate entries are dealt with later and
4918 would get caught by the next condition. */
4919 if (sym
->attr
.entry
)
4922 /* Make sure we convert the types of the derived types from iso_c_binding
4924 if (sym
->attr
.flavor
!= FL_PROCEDURE
&& sym
->attr
.is_iso_c
4925 && sym
->ts
.type
== BT_DERIVED
)
4926 sym
->backend_decl
= gfc_typenode_for_spec (&(sym
->ts
));
4928 if (gfc_fl_struct (sym
->attr
.flavor
)
4929 && sym
->backend_decl
4930 && TREE_CODE (sym
->backend_decl
) == RECORD_TYPE
)
4932 decl
= sym
->backend_decl
;
4933 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
4935 if (!sym
->attr
.use_assoc
&& !sym
->attr
.used_in_submodule
)
4937 gcc_assert (TYPE_CONTEXT (decl
) == NULL_TREE
4938 || TYPE_CONTEXT (decl
) == sym
->ns
->proc_name
->backend_decl
);
4939 gcc_assert (DECL_CONTEXT (TYPE_STUB_DECL (decl
)) == NULL_TREE
4940 || DECL_CONTEXT (TYPE_STUB_DECL (decl
))
4941 == sym
->ns
->proc_name
->backend_decl
);
4943 TYPE_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
4944 DECL_CONTEXT (TYPE_STUB_DECL (decl
)) = sym
->ns
->proc_name
->backend_decl
;
4945 gfc_module_add_decl (cur_module
, TYPE_STUB_DECL (decl
));
4948 /* Only output variables, procedure pointers and array valued,
4949 or derived type, parameters. */
4950 if (sym
->attr
.flavor
!= FL_VARIABLE
4951 && !(sym
->attr
.flavor
== FL_PARAMETER
4952 && (sym
->attr
.dimension
|| sym
->ts
.type
== BT_DERIVED
))
4953 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.proc_pointer
))
4956 if ((sym
->attr
.in_common
|| sym
->attr
.in_equivalence
) && sym
->backend_decl
)
4958 decl
= sym
->backend_decl
;
4959 gcc_assert (DECL_FILE_SCOPE_P (decl
));
4960 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
4961 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
4962 gfc_module_add_decl (cur_module
, decl
);
4965 /* Don't generate variables from other modules. Variables from
4966 COMMONs and Cray pointees will already have been generated. */
4967 if (sym
->attr
.use_assoc
|| sym
->attr
.used_in_submodule
4968 || sym
->attr
.in_common
|| sym
->attr
.cray_pointee
)
4971 /* Equivalenced variables arrive here after creation. */
4972 if (sym
->backend_decl
4973 && (sym
->equiv_built
|| sym
->attr
.in_equivalence
))
4976 if (sym
->backend_decl
&& !sym
->attr
.vtab
&& !sym
->attr
.target
)
4977 gfc_internal_error ("backend decl for module variable %qs already exists",
4980 if (sym
->module
&& !sym
->attr
.result
&& !sym
->attr
.dummy
4981 && (sym
->attr
.access
== ACCESS_UNKNOWN
4982 && (sym
->ns
->default_access
== ACCESS_PRIVATE
4983 || (sym
->ns
->default_access
== ACCESS_UNKNOWN
4984 && flag_module_private
))))
4985 sym
->attr
.access
= ACCESS_PRIVATE
;
4987 if (warn_unused_variable
&& !sym
->attr
.referenced
4988 && sym
->attr
.access
== ACCESS_PRIVATE
)
4989 gfc_warning (OPT_Wunused_value
,
4990 "Unused PRIVATE module variable %qs declared at %L",
4991 sym
->name
, &sym
->declared_at
);
4993 /* We always want module variables to be created. */
4994 sym
->attr
.referenced
= 1;
4995 /* Create the decl. */
4996 decl
= gfc_get_symbol_decl (sym
);
4998 /* Create the variable. */
5000 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
5001 || (sym
->ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
5002 && sym
->fn_result_spec
));
5003 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
5004 rest_of_decl_compilation (decl
, 1, 0);
5005 gfc_module_add_decl (cur_module
, decl
);
5007 /* Also add length of strings. */
5008 if (sym
->ts
.type
== BT_CHARACTER
)
5012 length
= sym
->ts
.u
.cl
->backend_decl
;
5013 gcc_assert (length
|| sym
->attr
.proc_pointer
);
5014 if (length
&& !INTEGER_CST_P (length
))
5017 rest_of_decl_compilation (length
, 1, 0);
5021 if (sym
->attr
.codimension
&& !sym
->attr
.dummy
&& !sym
->attr
.allocatable
5022 && sym
->attr
.referenced
&& !sym
->attr
.use_assoc
)
5023 has_coarray_vars
= true;
5026 /* Emit debug information for USE statements. */
5029 gfc_trans_use_stmts (gfc_namespace
* ns
)
5031 gfc_use_list
*use_stmt
;
5032 for (use_stmt
= ns
->use_stmts
; use_stmt
; use_stmt
= use_stmt
->next
)
5034 struct module_htab_entry
*entry
5035 = gfc_find_module (use_stmt
->module_name
);
5036 gfc_use_rename
*rent
;
5038 if (entry
->namespace_decl
== NULL
)
5040 entry
->namespace_decl
5041 = build_decl (input_location
,
5043 get_identifier (use_stmt
->module_name
),
5045 DECL_EXTERNAL (entry
->namespace_decl
) = 1;
5047 gfc_set_backend_locus (&use_stmt
->where
);
5048 if (!use_stmt
->only_flag
)
5049 (*debug_hooks
->imported_module_or_decl
) (entry
->namespace_decl
,
5051 ns
->proc_name
->backend_decl
,
5053 for (rent
= use_stmt
->rename
; rent
; rent
= rent
->next
)
5055 tree decl
, local_name
;
5057 if (rent
->op
!= INTRINSIC_NONE
)
5060 hashval_t hash
= htab_hash_string (rent
->use_name
);
5061 tree
*slot
= entry
->decls
->find_slot_with_hash (rent
->use_name
, hash
,
5067 st
= gfc_find_symtree (ns
->sym_root
,
5069 ? rent
->local_name
: rent
->use_name
);
5071 /* The following can happen if a derived type is renamed. */
5075 name
= xstrdup (rent
->local_name
[0]
5076 ? rent
->local_name
: rent
->use_name
);
5077 name
[0] = (char) TOUPPER ((unsigned char) name
[0]);
5078 st
= gfc_find_symtree (ns
->sym_root
, name
);
5083 /* Sometimes, generic interfaces wind up being over-ruled by a
5084 local symbol (see PR41062). */
5085 if (!st
->n
.sym
->attr
.use_assoc
)
5088 if (st
->n
.sym
->backend_decl
5089 && DECL_P (st
->n
.sym
->backend_decl
)
5090 && st
->n
.sym
->module
5091 && strcmp (st
->n
.sym
->module
, use_stmt
->module_name
) == 0)
5093 gcc_assert (DECL_EXTERNAL (entry
->namespace_decl
)
5094 || !VAR_P (st
->n
.sym
->backend_decl
));
5095 decl
= copy_node (st
->n
.sym
->backend_decl
);
5096 DECL_CONTEXT (decl
) = entry
->namespace_decl
;
5097 DECL_EXTERNAL (decl
) = 1;
5098 DECL_IGNORED_P (decl
) = 0;
5099 DECL_INITIAL (decl
) = NULL_TREE
;
5101 else if (st
->n
.sym
->attr
.flavor
== FL_NAMELIST
5102 && st
->n
.sym
->attr
.use_only
5103 && st
->n
.sym
->module
5104 && strcmp (st
->n
.sym
->module
, use_stmt
->module_name
)
5107 decl
= generate_namelist_decl (st
->n
.sym
);
5108 DECL_CONTEXT (decl
) = entry
->namespace_decl
;
5109 DECL_EXTERNAL (decl
) = 1;
5110 DECL_IGNORED_P (decl
) = 0;
5111 DECL_INITIAL (decl
) = NULL_TREE
;
5115 *slot
= error_mark_node
;
5116 entry
->decls
->clear_slot (slot
);
5121 decl
= (tree
) *slot
;
5122 if (rent
->local_name
[0])
5123 local_name
= get_identifier (rent
->local_name
);
5125 local_name
= NULL_TREE
;
5126 gfc_set_backend_locus (&rent
->where
);
5127 (*debug_hooks
->imported_module_or_decl
) (decl
, local_name
,
5128 ns
->proc_name
->backend_decl
,
5129 !use_stmt
->only_flag
,
5136 /* Return true if expr is a constant initializer that gfc_conv_initializer
5140 check_constant_initializer (gfc_expr
*expr
, gfc_typespec
*ts
, bool array
,
5150 if (expr
->expr_type
== EXPR_CONSTANT
|| expr
->expr_type
== EXPR_NULL
)
5152 else if (expr
->expr_type
== EXPR_STRUCTURE
)
5153 return check_constant_initializer (expr
, ts
, false, false);
5154 else if (expr
->expr_type
!= EXPR_ARRAY
)
5156 for (c
= gfc_constructor_first (expr
->value
.constructor
);
5157 c
; c
= gfc_constructor_next (c
))
5161 if (c
->expr
->expr_type
== EXPR_STRUCTURE
)
5163 if (!check_constant_initializer (c
->expr
, ts
, false, false))
5166 else if (c
->expr
->expr_type
!= EXPR_CONSTANT
)
5171 else switch (ts
->type
)
5174 if (expr
->expr_type
!= EXPR_STRUCTURE
)
5176 cm
= expr
->ts
.u
.derived
->components
;
5177 for (c
= gfc_constructor_first (expr
->value
.constructor
);
5178 c
; c
= gfc_constructor_next (c
), cm
= cm
->next
)
5180 if (!c
->expr
|| cm
->attr
.allocatable
)
5182 if (!check_constant_initializer (c
->expr
, &cm
->ts
,
5189 return expr
->expr_type
== EXPR_CONSTANT
;
5193 /* Emit debug info for parameters and unreferenced variables with
5197 gfc_emit_parameter_debug_info (gfc_symbol
*sym
)
5201 if (sym
->attr
.flavor
!= FL_PARAMETER
5202 && (sym
->attr
.flavor
!= FL_VARIABLE
|| sym
->attr
.referenced
))
5205 if (sym
->backend_decl
!= NULL
5206 || sym
->value
== NULL
5207 || sym
->attr
.use_assoc
5210 || sym
->attr
.function
5211 || sym
->attr
.intrinsic
5212 || sym
->attr
.pointer
5213 || sym
->attr
.allocatable
5214 || sym
->attr
.cray_pointee
5215 || sym
->attr
.threadprivate
5216 || sym
->attr
.is_bind_c
5217 || sym
->attr
.subref_array_pointer
5218 || sym
->attr
.assign
)
5221 if (sym
->ts
.type
== BT_CHARACTER
)
5223 gfc_conv_const_charlen (sym
->ts
.u
.cl
);
5224 if (sym
->ts
.u
.cl
->backend_decl
== NULL
5225 || TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) != INTEGER_CST
)
5228 else if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.alloc_comp
)
5235 if (sym
->as
->type
!= AS_EXPLICIT
)
5237 for (n
= 0; n
< sym
->as
->rank
; n
++)
5238 if (sym
->as
->lower
[n
]->expr_type
!= EXPR_CONSTANT
5239 || sym
->as
->upper
[n
] == NULL
5240 || sym
->as
->upper
[n
]->expr_type
!= EXPR_CONSTANT
)
5244 if (!check_constant_initializer (sym
->value
, &sym
->ts
,
5245 sym
->attr
.dimension
, false))
5248 if (flag_coarray
== GFC_FCOARRAY_LIB
&& sym
->attr
.codimension
)
5251 /* Create the decl for the variable or constant. */
5252 decl
= build_decl (input_location
,
5253 sym
->attr
.flavor
== FL_PARAMETER
? CONST_DECL
: VAR_DECL
,
5254 gfc_sym_identifier (sym
), gfc_sym_type (sym
));
5255 if (sym
->attr
.flavor
== FL_PARAMETER
)
5256 TREE_READONLY (decl
) = 1;
5257 gfc_set_decl_location (decl
, &sym
->declared_at
);
5258 if (sym
->attr
.dimension
)
5259 GFC_DECL_PACKED_ARRAY (decl
) = 1;
5260 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
5261 TREE_STATIC (decl
) = 1;
5262 TREE_USED (decl
) = 1;
5263 if (DECL_CONTEXT (decl
) && TREE_CODE (DECL_CONTEXT (decl
)) == NAMESPACE_DECL
)
5264 TREE_PUBLIC (decl
) = 1;
5265 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
5267 sym
->attr
.dimension
,
5269 debug_hooks
->early_global_decl (decl
);
5274 generate_coarray_sym_init (gfc_symbol
*sym
)
5276 tree tmp
, size
, decl
, token
, desc
;
5277 bool is_lock_type
, is_event_type
;
5280 symbol_attribute attr
;
5282 if (sym
->attr
.dummy
|| sym
->attr
.allocatable
|| !sym
->attr
.codimension
5283 || sym
->attr
.use_assoc
|| !sym
->attr
.referenced
5284 || sym
->attr
.select_type_temporary
)
5287 decl
= sym
->backend_decl
;
5288 TREE_USED(decl
) = 1;
5289 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (decl
)));
5291 is_lock_type
= sym
->ts
.type
== BT_DERIVED
5292 && sym
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
5293 && sym
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_LOCK_TYPE
;
5295 is_event_type
= sym
->ts
.type
== BT_DERIVED
5296 && sym
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
5297 && sym
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_EVENT_TYPE
;
5299 /* FIXME: Workaround for PR middle-end/49106, cf. also PR middle-end/49108
5300 to make sure the variable is not optimized away. */
5301 DECL_PRESERVE_P (DECL_CONTEXT (decl
)) = 1;
5303 /* For lock types, we pass the array size as only the library knows the
5304 size of the variable. */
5305 if (is_lock_type
|| is_event_type
)
5306 size
= gfc_index_one_node
;
5308 size
= TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (decl
)));
5310 /* Ensure that we do not have size=0 for zero-sized arrays. */
5311 size
= fold_build2_loc (input_location
, MAX_EXPR
, size_type_node
,
5312 fold_convert (size_type_node
, size
),
5313 build_int_cst (size_type_node
, 1));
5315 if (GFC_TYPE_ARRAY_RANK (TREE_TYPE (decl
)))
5317 tmp
= GFC_TYPE_ARRAY_SIZE (TREE_TYPE (decl
));
5318 size
= fold_build2_loc (input_location
, MULT_EXPR
, size_type_node
,
5319 fold_convert (size_type_node
, tmp
), size
);
5322 gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (decl
)) != NULL_TREE
);
5323 token
= gfc_build_addr_expr (ppvoid_type_node
,
5324 GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE(decl
)));
5326 reg_type
= sym
->attr
.artificial
? GFC_CAF_CRITICAL
: GFC_CAF_LOCK_STATIC
;
5327 else if (is_event_type
)
5328 reg_type
= GFC_CAF_EVENT_STATIC
;
5330 reg_type
= GFC_CAF_COARRAY_STATIC
;
5332 /* Compile the symbol attribute. */
5333 if (sym
->ts
.type
== BT_CLASS
)
5335 attr
= CLASS_DATA (sym
)->attr
;
5336 /* The pointer attribute is always set on classes, overwrite it with the
5337 class_pointer attribute, which denotes the pointer for classes. */
5338 attr
.pointer
= attr
.class_pointer
;
5342 gfc_init_se (&se
, NULL
);
5343 desc
= gfc_conv_scalar_to_descriptor (&se
, decl
, attr
);
5344 gfc_add_block_to_block (&caf_init_block
, &se
.pre
);
5346 tmp
= build_call_expr_loc (input_location
, gfor_fndecl_caf_register
, 7, size
,
5347 build_int_cst (integer_type_node
, reg_type
),
5348 token
, gfc_build_addr_expr (pvoid_type_node
, desc
),
5349 null_pointer_node
, /* stat. */
5350 null_pointer_node
, /* errgmsg. */
5351 integer_zero_node
); /* errmsg_len. */
5352 gfc_add_expr_to_block (&caf_init_block
, tmp
);
5353 gfc_add_modify (&caf_init_block
, decl
, fold_convert (TREE_TYPE (decl
),
5354 gfc_conv_descriptor_data_get (desc
)));
5356 /* Handle "static" initializer. */
5359 sym
->attr
.pointer
= 1;
5360 tmp
= gfc_trans_assignment (gfc_lval_expr_from_sym (sym
), sym
->value
,
5362 sym
->attr
.pointer
= 0;
5363 gfc_add_expr_to_block (&caf_init_block
, tmp
);
5365 else if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.pointer_comp
)
5367 tmp
= gfc_nullify_alloc_comp (sym
->ts
.u
.derived
, decl
, sym
->as
5368 ? sym
->as
->rank
: 0,
5369 GFC_STRUCTURE_CAF_MODE_IN_COARRAY
);
5370 gfc_add_expr_to_block (&caf_init_block
, tmp
);
5375 /* Generate constructor function to initialize static, nonallocatable
5379 generate_coarray_init (gfc_namespace
* ns
__attribute((unused
)))
5381 tree fndecl
, tmp
, decl
, save_fn_decl
;
5383 save_fn_decl
= current_function_decl
;
5384 push_function_context ();
5386 tmp
= build_function_type_list (void_type_node
, NULL_TREE
);
5387 fndecl
= build_decl (input_location
, FUNCTION_DECL
,
5388 create_tmp_var_name ("_caf_init"), tmp
);
5390 DECL_STATIC_CONSTRUCTOR (fndecl
) = 1;
5391 SET_DECL_INIT_PRIORITY (fndecl
, DEFAULT_INIT_PRIORITY
);
5393 decl
= build_decl (input_location
, RESULT_DECL
, NULL_TREE
, void_type_node
);
5394 DECL_ARTIFICIAL (decl
) = 1;
5395 DECL_IGNORED_P (decl
) = 1;
5396 DECL_CONTEXT (decl
) = fndecl
;
5397 DECL_RESULT (fndecl
) = decl
;
5400 current_function_decl
= fndecl
;
5401 announce_function (fndecl
);
5403 rest_of_decl_compilation (fndecl
, 0, 0);
5404 make_decl_rtl (fndecl
);
5405 allocate_struct_function (fndecl
, false);
5408 gfc_init_block (&caf_init_block
);
5410 gfc_traverse_ns (ns
, generate_coarray_sym_init
);
5412 DECL_SAVED_TREE (fndecl
) = gfc_finish_block (&caf_init_block
);
5416 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
5418 DECL_SAVED_TREE (fndecl
)
5419 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (fndecl
),
5420 DECL_INITIAL (fndecl
));
5421 dump_function (TDI_original
, fndecl
);
5423 cfun
->function_end_locus
= input_location
;
5426 if (decl_function_context (fndecl
))
5427 (void) cgraph_node::create (fndecl
);
5429 cgraph_node::finalize_function (fndecl
, true);
5431 pop_function_context ();
5432 current_function_decl
= save_fn_decl
;
5437 create_module_nml_decl (gfc_symbol
*sym
)
5439 if (sym
->attr
.flavor
== FL_NAMELIST
)
5441 tree decl
= generate_namelist_decl (sym
);
5443 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
5444 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
5445 rest_of_decl_compilation (decl
, 1, 0);
5446 gfc_module_add_decl (cur_module
, decl
);
5451 /* Generate all the required code for module variables. */
5454 gfc_generate_module_vars (gfc_namespace
* ns
)
5456 module_namespace
= ns
;
5457 cur_module
= gfc_find_module (ns
->proc_name
->name
);
5459 /* Check if the frontend left the namespace in a reasonable state. */
5460 gcc_assert (ns
->proc_name
&& !ns
->proc_name
->tlink
);
5462 /* Generate COMMON blocks. */
5463 gfc_trans_common (ns
);
5465 has_coarray_vars
= false;
5467 /* Create decls for all the module variables. */
5468 gfc_traverse_ns (ns
, gfc_create_module_variable
);
5469 gfc_traverse_ns (ns
, create_module_nml_decl
);
5471 if (flag_coarray
== GFC_FCOARRAY_LIB
&& has_coarray_vars
)
5472 generate_coarray_init (ns
);
5476 gfc_trans_use_stmts (ns
);
5477 gfc_traverse_ns (ns
, gfc_emit_parameter_debug_info
);
5482 gfc_generate_contained_functions (gfc_namespace
* parent
)
5486 /* We create all the prototypes before generating any code. */
5487 for (ns
= parent
->contained
; ns
; ns
= ns
->sibling
)
5489 /* Skip namespaces from used modules. */
5490 if (ns
->parent
!= parent
)
5493 gfc_create_function_decl (ns
, false);
5496 for (ns
= parent
->contained
; ns
; ns
= ns
->sibling
)
5498 /* Skip namespaces from used modules. */
5499 if (ns
->parent
!= parent
)
5502 gfc_generate_function_code (ns
);
5507 /* Drill down through expressions for the array specification bounds and
5508 character length calling generate_local_decl for all those variables
5509 that have not already been declared. */
5512 generate_local_decl (gfc_symbol
*);
5514 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
5517 expr_decls (gfc_expr
*e
, gfc_symbol
*sym
,
5518 int *f ATTRIBUTE_UNUSED
)
5520 if (e
->expr_type
!= EXPR_VARIABLE
5521 || sym
== e
->symtree
->n
.sym
5522 || e
->symtree
->n
.sym
->mark
5523 || e
->symtree
->n
.sym
->ns
!= sym
->ns
)
5526 generate_local_decl (e
->symtree
->n
.sym
);
5531 generate_expr_decls (gfc_symbol
*sym
, gfc_expr
*e
)
5533 gfc_traverse_expr (e
, sym
, expr_decls
, 0);
5537 /* Check for dependencies in the character length and array spec. */
5540 generate_dependency_declarations (gfc_symbol
*sym
)
5544 if (sym
->ts
.type
== BT_CHARACTER
5546 && sym
->ts
.u
.cl
->length
5547 && sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
)
5548 generate_expr_decls (sym
, sym
->ts
.u
.cl
->length
);
5550 if (sym
->as
&& sym
->as
->rank
)
5552 for (i
= 0; i
< sym
->as
->rank
; i
++)
5554 generate_expr_decls (sym
, sym
->as
->lower
[i
]);
5555 generate_expr_decls (sym
, sym
->as
->upper
[i
]);
5561 /* Generate decls for all local variables. We do this to ensure correct
5562 handling of expressions which only appear in the specification of
5566 generate_local_decl (gfc_symbol
* sym
)
5568 if (sym
->attr
.flavor
== FL_VARIABLE
)
5570 if (sym
->attr
.codimension
&& !sym
->attr
.dummy
&& !sym
->attr
.allocatable
5571 && sym
->attr
.referenced
&& !sym
->attr
.use_assoc
)
5572 has_coarray_vars
= true;
5574 if (!sym
->attr
.dummy
&& !sym
->ns
->proc_name
->attr
.entry_master
)
5575 generate_dependency_declarations (sym
);
5577 if (sym
->attr
.referenced
)
5578 gfc_get_symbol_decl (sym
);
5580 /* Warnings for unused dummy arguments. */
5581 else if (sym
->attr
.dummy
&& !sym
->attr
.in_namelist
)
5583 /* INTENT(out) dummy arguments are likely meant to be set. */
5584 if (warn_unused_dummy_argument
&& sym
->attr
.intent
== INTENT_OUT
)
5586 if (sym
->ts
.type
!= BT_DERIVED
)
5587 gfc_warning (OPT_Wunused_dummy_argument
,
5588 "Dummy argument %qs at %L was declared "
5589 "INTENT(OUT) but was not set", sym
->name
,
5591 else if (!gfc_has_default_initializer (sym
->ts
.u
.derived
)
5592 && !sym
->ts
.u
.derived
->attr
.zero_comp
)
5593 gfc_warning (OPT_Wunused_dummy_argument
,
5594 "Derived-type dummy argument %qs at %L was "
5595 "declared INTENT(OUT) but was not set and "
5596 "does not have a default initializer",
5597 sym
->name
, &sym
->declared_at
);
5598 if (sym
->backend_decl
!= NULL_TREE
)
5599 TREE_NO_WARNING(sym
->backend_decl
) = 1;
5601 else if (warn_unused_dummy_argument
)
5603 gfc_warning (OPT_Wunused_dummy_argument
,
5604 "Unused dummy argument %qs at %L", sym
->name
,
5606 if (sym
->backend_decl
!= NULL_TREE
)
5607 TREE_NO_WARNING(sym
->backend_decl
) = 1;
5611 /* Warn for unused variables, but not if they're inside a common
5612 block or a namelist. */
5613 else if (warn_unused_variable
5614 && !(sym
->attr
.in_common
|| sym
->mark
|| sym
->attr
.in_namelist
))
5616 if (sym
->attr
.use_only
)
5618 gfc_warning (OPT_Wunused_variable
,
5619 "Unused module variable %qs which has been "
5620 "explicitly imported at %L", sym
->name
,
5622 if (sym
->backend_decl
!= NULL_TREE
)
5623 TREE_NO_WARNING(sym
->backend_decl
) = 1;
5625 else if (!sym
->attr
.use_assoc
)
5627 /* Corner case: the symbol may be an entry point. At this point,
5628 it may appear to be an unused variable. Suppress warning. */
5632 for (el
= sym
->ns
->entries
; el
; el
=el
->next
)
5633 if (strcmp(sym
->name
, el
->sym
->name
) == 0)
5637 gfc_warning (OPT_Wunused_variable
,
5638 "Unused variable %qs declared at %L",
5639 sym
->name
, &sym
->declared_at
);
5640 if (sym
->backend_decl
!= NULL_TREE
)
5641 TREE_NO_WARNING(sym
->backend_decl
) = 1;
5645 /* For variable length CHARACTER parameters, the PARM_DECL already
5646 references the length variable, so force gfc_get_symbol_decl
5647 even when not referenced. If optimize > 0, it will be optimized
5648 away anyway. But do this only after emitting -Wunused-parameter
5649 warning if requested. */
5650 if (sym
->attr
.dummy
&& !sym
->attr
.referenced
5651 && sym
->ts
.type
== BT_CHARACTER
5652 && sym
->ts
.u
.cl
->backend_decl
!= NULL
5653 && VAR_P (sym
->ts
.u
.cl
->backend_decl
))
5655 sym
->attr
.referenced
= 1;
5656 gfc_get_symbol_decl (sym
);
5659 /* INTENT(out) dummy arguments and result variables with allocatable
5660 components are reset by default and need to be set referenced to
5661 generate the code for nullification and automatic lengths. */
5662 if (!sym
->attr
.referenced
5663 && sym
->ts
.type
== BT_DERIVED
5664 && sym
->ts
.u
.derived
->attr
.alloc_comp
5665 && !sym
->attr
.pointer
5666 && ((sym
->attr
.dummy
&& sym
->attr
.intent
== INTENT_OUT
)
5668 (sym
->attr
.result
&& sym
!= sym
->result
)))
5670 sym
->attr
.referenced
= 1;
5671 gfc_get_symbol_decl (sym
);
5674 /* Check for dependencies in the array specification and string
5675 length, adding the necessary declarations to the function. We
5676 mark the symbol now, as well as in traverse_ns, to prevent
5677 getting stuck in a circular dependency. */
5680 else if (sym
->attr
.flavor
== FL_PARAMETER
)
5682 if (warn_unused_parameter
5683 && !sym
->attr
.referenced
)
5685 if (!sym
->attr
.use_assoc
)
5686 gfc_warning (OPT_Wunused_parameter
,
5687 "Unused parameter %qs declared at %L", sym
->name
,
5689 else if (sym
->attr
.use_only
)
5690 gfc_warning (OPT_Wunused_parameter
,
5691 "Unused parameter %qs which has been explicitly "
5692 "imported at %L", sym
->name
, &sym
->declared_at
);
5697 && sym
->ns
->parent
->code
5698 && sym
->ns
->parent
->code
->op
== EXEC_BLOCK
)
5700 if (sym
->attr
.referenced
)
5701 gfc_get_symbol_decl (sym
);
5705 else if (sym
->attr
.flavor
== FL_PROCEDURE
)
5707 /* TODO: move to the appropriate place in resolve.c. */
5708 if (warn_return_type
> 0
5709 && sym
->attr
.function
5711 && sym
!= sym
->result
5712 && !sym
->result
->attr
.referenced
5713 && !sym
->attr
.use_assoc
5714 && sym
->attr
.if_source
!= IFSRC_IFBODY
)
5716 gfc_warning (OPT_Wreturn_type
,
5717 "Return value %qs of function %qs declared at "
5718 "%L not set", sym
->result
->name
, sym
->name
,
5719 &sym
->result
->declared_at
);
5721 /* Prevents "Unused variable" warning for RESULT variables. */
5722 sym
->result
->mark
= 1;
5726 if (sym
->attr
.dummy
== 1)
5728 /* Modify the tree type for scalar character dummy arguments of bind(c)
5729 procedures if they are passed by value. The tree type for them will
5730 be promoted to INTEGER_TYPE for the middle end, which appears to be
5731 what C would do with characters passed by-value. The value attribute
5732 implies the dummy is a scalar. */
5733 if (sym
->attr
.value
== 1 && sym
->backend_decl
!= NULL
5734 && sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.is_c_interop
5735 && sym
->ns
->proc_name
!= NULL
&& sym
->ns
->proc_name
->attr
.is_bind_c
)
5736 gfc_conv_scalar_char_value (sym
, NULL
, NULL
);
5738 /* Unused procedure passed as dummy argument. */
5739 if (sym
->attr
.flavor
== FL_PROCEDURE
)
5741 if (!sym
->attr
.referenced
)
5743 if (warn_unused_dummy_argument
)
5744 gfc_warning (OPT_Wunused_dummy_argument
,
5745 "Unused dummy argument %qs at %L", sym
->name
,
5749 /* Silence bogus "unused parameter" warnings from the
5751 if (sym
->backend_decl
!= NULL_TREE
)
5752 TREE_NO_WARNING (sym
->backend_decl
) = 1;
5756 /* Make sure we convert the types of the derived types from iso_c_binding
5758 if (sym
->attr
.flavor
!= FL_PROCEDURE
&& sym
->attr
.is_iso_c
5759 && sym
->ts
.type
== BT_DERIVED
)
5760 sym
->backend_decl
= gfc_typenode_for_spec (&(sym
->ts
));
5765 generate_local_nml_decl (gfc_symbol
* sym
)
5767 if (sym
->attr
.flavor
== FL_NAMELIST
&& !sym
->attr
.use_assoc
)
5769 tree decl
= generate_namelist_decl (sym
);
5776 generate_local_vars (gfc_namespace
* ns
)
5778 gfc_traverse_ns (ns
, generate_local_decl
);
5779 gfc_traverse_ns (ns
, generate_local_nml_decl
);
5783 /* Generate a switch statement to jump to the correct entry point. Also
5784 creates the label decls for the entry points. */
5787 gfc_trans_entry_master_switch (gfc_entry_list
* el
)
5794 gfc_init_block (&block
);
5795 for (; el
; el
= el
->next
)
5797 /* Add the case label. */
5798 label
= gfc_build_label_decl (NULL_TREE
);
5799 val
= build_int_cst (gfc_array_index_type
, el
->id
);
5800 tmp
= build_case_label (val
, NULL_TREE
, label
);
5801 gfc_add_expr_to_block (&block
, tmp
);
5803 /* And jump to the actual entry point. */
5804 label
= gfc_build_label_decl (NULL_TREE
);
5805 tmp
= build1_v (GOTO_EXPR
, label
);
5806 gfc_add_expr_to_block (&block
, tmp
);
5808 /* Save the label decl. */
5811 tmp
= gfc_finish_block (&block
);
5812 /* The first argument selects the entry point. */
5813 val
= DECL_ARGUMENTS (current_function_decl
);
5814 tmp
= fold_build2_loc (input_location
, SWITCH_EXPR
, NULL_TREE
, val
, tmp
);
5819 /* Add code to string lengths of actual arguments passed to a function against
5820 the expected lengths of the dummy arguments. */
5823 add_argument_checking (stmtblock_t
*block
, gfc_symbol
*sym
)
5825 gfc_formal_arglist
*formal
;
5827 for (formal
= gfc_sym_get_dummy_args (sym
); formal
; formal
= formal
->next
)
5828 if (formal
->sym
&& formal
->sym
->ts
.type
== BT_CHARACTER
5829 && !formal
->sym
->ts
.deferred
)
5831 enum tree_code comparison
;
5836 const char *message
;
5842 gcc_assert (cl
->passed_length
!= NULL_TREE
);
5843 gcc_assert (cl
->backend_decl
!= NULL_TREE
);
5845 /* For POINTER, ALLOCATABLE and assumed-shape dummy arguments, the
5846 string lengths must match exactly. Otherwise, it is only required
5847 that the actual string length is *at least* the expected one.
5848 Sequence association allows for a mismatch of the string length
5849 if the actual argument is (part of) an array, but only if the
5850 dummy argument is an array. (See "Sequence association" in
5851 Section 12.4.1.4 for F95 and 12.4.1.5 for F2003.) */
5852 if (fsym
->attr
.pointer
|| fsym
->attr
.allocatable
5853 || (fsym
->as
&& (fsym
->as
->type
== AS_ASSUMED_SHAPE
5854 || fsym
->as
->type
== AS_ASSUMED_RANK
)))
5856 comparison
= NE_EXPR
;
5857 message
= _("Actual string length does not match the declared one"
5858 " for dummy argument '%s' (%ld/%ld)");
5860 else if (fsym
->as
&& fsym
->as
->rank
!= 0)
5864 comparison
= LT_EXPR
;
5865 message
= _("Actual string length is shorter than the declared one"
5866 " for dummy argument '%s' (%ld/%ld)");
5869 /* Build the condition. For optional arguments, an actual length
5870 of 0 is also acceptable if the associated string is NULL, which
5871 means the argument was not passed. */
5872 cond
= fold_build2_loc (input_location
, comparison
, logical_type_node
,
5873 cl
->passed_length
, cl
->backend_decl
);
5874 if (fsym
->attr
.optional
)
5880 not_0length
= fold_build2_loc (input_location
, NE_EXPR
,
5884 (TREE_TYPE (cl
->passed_length
)));
5885 /* The symbol needs to be referenced for gfc_get_symbol_decl. */
5886 fsym
->attr
.referenced
= 1;
5887 not_absent
= gfc_conv_expr_present (fsym
);
5889 absent_failed
= fold_build2_loc (input_location
, TRUTH_OR_EXPR
,
5890 logical_type_node
, not_0length
,
5893 cond
= fold_build2_loc (input_location
, TRUTH_AND_EXPR
,
5894 logical_type_node
, cond
, absent_failed
);
5897 /* Build the runtime check. */
5898 argname
= gfc_build_cstring_const (fsym
->name
);
5899 argname
= gfc_build_addr_expr (pchar_type_node
, argname
);
5900 gfc_trans_runtime_check (true, false, cond
, block
, &fsym
->declared_at
,
5902 fold_convert (long_integer_type_node
,
5904 fold_convert (long_integer_type_node
,
5911 create_main_function (tree fndecl
)
5915 tree tmp
, decl
, result_decl
, argc
, argv
, typelist
, arglist
;
5918 old_context
= current_function_decl
;
5922 push_function_context ();
5923 saved_parent_function_decls
= saved_function_decls
;
5924 saved_function_decls
= NULL_TREE
;
5927 /* main() function must be declared with global scope. */
5928 gcc_assert (current_function_decl
== NULL_TREE
);
5930 /* Declare the function. */
5931 tmp
= build_function_type_list (integer_type_node
, integer_type_node
,
5932 build_pointer_type (pchar_type_node
),
5934 main_identifier_node
= get_identifier ("main");
5935 ftn_main
= build_decl (input_location
, FUNCTION_DECL
,
5936 main_identifier_node
, tmp
);
5937 DECL_EXTERNAL (ftn_main
) = 0;
5938 TREE_PUBLIC (ftn_main
) = 1;
5939 TREE_STATIC (ftn_main
) = 1;
5940 DECL_ATTRIBUTES (ftn_main
)
5941 = tree_cons (get_identifier("externally_visible"), NULL_TREE
, NULL_TREE
);
5943 /* Setup the result declaration (for "return 0"). */
5944 result_decl
= build_decl (input_location
,
5945 RESULT_DECL
, NULL_TREE
, integer_type_node
);
5946 DECL_ARTIFICIAL (result_decl
) = 1;
5947 DECL_IGNORED_P (result_decl
) = 1;
5948 DECL_CONTEXT (result_decl
) = ftn_main
;
5949 DECL_RESULT (ftn_main
) = result_decl
;
5951 pushdecl (ftn_main
);
5953 /* Get the arguments. */
5955 arglist
= NULL_TREE
;
5956 typelist
= TYPE_ARG_TYPES (TREE_TYPE (ftn_main
));
5958 tmp
= TREE_VALUE (typelist
);
5959 argc
= build_decl (input_location
, PARM_DECL
, get_identifier ("argc"), tmp
);
5960 DECL_CONTEXT (argc
) = ftn_main
;
5961 DECL_ARG_TYPE (argc
) = TREE_VALUE (typelist
);
5962 TREE_READONLY (argc
) = 1;
5963 gfc_finish_decl (argc
);
5964 arglist
= chainon (arglist
, argc
);
5966 typelist
= TREE_CHAIN (typelist
);
5967 tmp
= TREE_VALUE (typelist
);
5968 argv
= build_decl (input_location
, PARM_DECL
, get_identifier ("argv"), tmp
);
5969 DECL_CONTEXT (argv
) = ftn_main
;
5970 DECL_ARG_TYPE (argv
) = TREE_VALUE (typelist
);
5971 TREE_READONLY (argv
) = 1;
5972 DECL_BY_REFERENCE (argv
) = 1;
5973 gfc_finish_decl (argv
);
5974 arglist
= chainon (arglist
, argv
);
5976 DECL_ARGUMENTS (ftn_main
) = arglist
;
5977 current_function_decl
= ftn_main
;
5978 announce_function (ftn_main
);
5980 rest_of_decl_compilation (ftn_main
, 1, 0);
5981 make_decl_rtl (ftn_main
);
5982 allocate_struct_function (ftn_main
, false);
5985 gfc_init_block (&body
);
5987 /* Call some libgfortran initialization routines, call then MAIN__(). */
5989 /* Call _gfortran_caf_init (*argc, ***argv). */
5990 if (flag_coarray
== GFC_FCOARRAY_LIB
)
5992 tree pint_type
, pppchar_type
;
5993 pint_type
= build_pointer_type (integer_type_node
);
5995 = build_pointer_type (build_pointer_type (pchar_type_node
));
5997 tmp
= build_call_expr_loc (input_location
, gfor_fndecl_caf_init
, 2,
5998 gfc_build_addr_expr (pint_type
, argc
),
5999 gfc_build_addr_expr (pppchar_type
, argv
));
6000 gfc_add_expr_to_block (&body
, tmp
);
6003 /* Call _gfortran_set_args (argc, argv). */
6004 TREE_USED (argc
) = 1;
6005 TREE_USED (argv
) = 1;
6006 tmp
= build_call_expr_loc (input_location
,
6007 gfor_fndecl_set_args
, 2, argc
, argv
);
6008 gfc_add_expr_to_block (&body
, tmp
);
6010 /* Add a call to set_options to set up the runtime library Fortran
6011 language standard parameters. */
6013 tree array_type
, array
, var
;
6014 vec
<constructor_elt
, va_gc
> *v
= NULL
;
6015 static const int noptions
= 7;
6017 /* Passing a new option to the library requires three modifications:
6018 + add it to the tree_cons list below
6019 + change the noptions variable above
6020 + modify the library (runtime/compile_options.c)! */
6022 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
6023 build_int_cst (integer_type_node
,
6024 gfc_option
.warn_std
));
6025 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
6026 build_int_cst (integer_type_node
,
6027 gfc_option
.allow_std
));
6028 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
6029 build_int_cst (integer_type_node
, pedantic
));
6030 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
6031 build_int_cst (integer_type_node
, flag_backtrace
));
6032 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
6033 build_int_cst (integer_type_node
, flag_sign_zero
));
6034 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
6035 build_int_cst (integer_type_node
,
6037 & GFC_RTCHECK_BOUNDS
)));
6038 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
6039 build_int_cst (integer_type_node
,
6040 gfc_option
.fpe_summary
));
6042 array_type
= build_array_type_nelts (integer_type_node
, noptions
);
6043 array
= build_constructor (array_type
, v
);
6044 TREE_CONSTANT (array
) = 1;
6045 TREE_STATIC (array
) = 1;
6047 /* Create a static variable to hold the jump table. */
6048 var
= build_decl (input_location
, VAR_DECL
,
6049 create_tmp_var_name ("options"), array_type
);
6050 DECL_ARTIFICIAL (var
) = 1;
6051 DECL_IGNORED_P (var
) = 1;
6052 TREE_CONSTANT (var
) = 1;
6053 TREE_STATIC (var
) = 1;
6054 TREE_READONLY (var
) = 1;
6055 DECL_INITIAL (var
) = array
;
6057 var
= gfc_build_addr_expr (build_pointer_type (integer_type_node
), var
);
6059 tmp
= build_call_expr_loc (input_location
,
6060 gfor_fndecl_set_options
, 2,
6061 build_int_cst (integer_type_node
, noptions
), var
);
6062 gfc_add_expr_to_block (&body
, tmp
);
6065 /* If -ffpe-trap option was provided, add a call to set_fpe so that
6066 the library will raise a FPE when needed. */
6067 if (gfc_option
.fpe
!= 0)
6069 tmp
= build_call_expr_loc (input_location
,
6070 gfor_fndecl_set_fpe
, 1,
6071 build_int_cst (integer_type_node
,
6073 gfc_add_expr_to_block (&body
, tmp
);
6076 /* If this is the main program and an -fconvert option was provided,
6077 add a call to set_convert. */
6079 if (flag_convert
!= GFC_FLAG_CONVERT_NATIVE
)
6081 tmp
= build_call_expr_loc (input_location
,
6082 gfor_fndecl_set_convert
, 1,
6083 build_int_cst (integer_type_node
, flag_convert
));
6084 gfc_add_expr_to_block (&body
, tmp
);
6087 /* If this is the main program and an -frecord-marker option was provided,
6088 add a call to set_record_marker. */
6090 if (flag_record_marker
!= 0)
6092 tmp
= build_call_expr_loc (input_location
,
6093 gfor_fndecl_set_record_marker
, 1,
6094 build_int_cst (integer_type_node
,
6095 flag_record_marker
));
6096 gfc_add_expr_to_block (&body
, tmp
);
6099 if (flag_max_subrecord_length
!= 0)
6101 tmp
= build_call_expr_loc (input_location
,
6102 gfor_fndecl_set_max_subrecord_length
, 1,
6103 build_int_cst (integer_type_node
,
6104 flag_max_subrecord_length
));
6105 gfc_add_expr_to_block (&body
, tmp
);
6108 /* Call MAIN__(). */
6109 tmp
= build_call_expr_loc (input_location
,
6111 gfc_add_expr_to_block (&body
, tmp
);
6113 /* Mark MAIN__ as used. */
6114 TREE_USED (fndecl
) = 1;
6116 /* Coarray: Call _gfortran_caf_finalize(void). */
6117 if (flag_coarray
== GFC_FCOARRAY_LIB
)
6119 tmp
= build_call_expr_loc (input_location
, gfor_fndecl_caf_finalize
, 0);
6120 gfc_add_expr_to_block (&body
, tmp
);
6124 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, integer_type_node
,
6125 DECL_RESULT (ftn_main
),
6126 build_int_cst (integer_type_node
, 0));
6127 tmp
= build1_v (RETURN_EXPR
, tmp
);
6128 gfc_add_expr_to_block (&body
, tmp
);
6131 DECL_SAVED_TREE (ftn_main
) = gfc_finish_block (&body
);
6134 /* Finish off this function and send it for code generation. */
6136 BLOCK_SUPERCONTEXT (DECL_INITIAL (ftn_main
)) = ftn_main
;
6138 DECL_SAVED_TREE (ftn_main
)
6139 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (ftn_main
),
6140 DECL_INITIAL (ftn_main
));
6142 /* Output the GENERIC tree. */
6143 dump_function (TDI_original
, ftn_main
);
6145 cgraph_node::finalize_function (ftn_main
, true);
6149 pop_function_context ();
6150 saved_function_decls
= saved_parent_function_decls
;
6152 current_function_decl
= old_context
;
6156 /* Generate an appropriate return-statement for a procedure. */
6159 gfc_generate_return (void)
6165 sym
= current_procedure_symbol
;
6166 fndecl
= sym
->backend_decl
;
6168 if (TREE_TYPE (DECL_RESULT (fndecl
)) == void_type_node
)
6172 result
= get_proc_result (sym
);
6174 /* Set the return value to the dummy result variable. The
6175 types may be different for scalar default REAL functions
6176 with -ff2c, therefore we have to convert. */
6177 if (result
!= NULL_TREE
)
6179 result
= convert (TREE_TYPE (DECL_RESULT (fndecl
)), result
);
6180 result
= fold_build2_loc (input_location
, MODIFY_EXPR
,
6181 TREE_TYPE (result
), DECL_RESULT (fndecl
),
6186 return build1_v (RETURN_EXPR
, result
);
6191 is_from_ieee_module (gfc_symbol
*sym
)
6193 if (sym
->from_intmod
== INTMOD_IEEE_FEATURES
6194 || sym
->from_intmod
== INTMOD_IEEE_EXCEPTIONS
6195 || sym
->from_intmod
== INTMOD_IEEE_ARITHMETIC
)
6196 seen_ieee_symbol
= 1;
6201 is_ieee_module_used (gfc_namespace
*ns
)
6203 seen_ieee_symbol
= 0;
6204 gfc_traverse_ns (ns
, is_from_ieee_module
);
6205 return seen_ieee_symbol
;
6209 static gfc_omp_clauses
*module_oacc_clauses
;
6213 add_clause (gfc_symbol
*sym
, gfc_omp_map_op map_op
)
6215 gfc_omp_namelist
*n
;
6217 n
= gfc_get_omp_namelist ();
6219 n
->u
.map_op
= map_op
;
6221 if (!module_oacc_clauses
)
6222 module_oacc_clauses
= gfc_get_omp_clauses ();
6224 if (module_oacc_clauses
->lists
[OMP_LIST_MAP
])
6225 n
->next
= module_oacc_clauses
->lists
[OMP_LIST_MAP
];
6227 module_oacc_clauses
->lists
[OMP_LIST_MAP
] = n
;
6232 find_module_oacc_declare_clauses (gfc_symbol
*sym
)
6234 if (sym
->attr
.use_assoc
)
6236 gfc_omp_map_op map_op
;
6238 if (sym
->attr
.oacc_declare_create
)
6239 map_op
= OMP_MAP_FORCE_ALLOC
;
6241 if (sym
->attr
.oacc_declare_copyin
)
6242 map_op
= OMP_MAP_FORCE_TO
;
6244 if (sym
->attr
.oacc_declare_deviceptr
)
6245 map_op
= OMP_MAP_FORCE_DEVICEPTR
;
6247 if (sym
->attr
.oacc_declare_device_resident
)
6248 map_op
= OMP_MAP_DEVICE_RESIDENT
;
6250 if (sym
->attr
.oacc_declare_create
6251 || sym
->attr
.oacc_declare_copyin
6252 || sym
->attr
.oacc_declare_deviceptr
6253 || sym
->attr
.oacc_declare_device_resident
)
6255 sym
->attr
.referenced
= 1;
6256 add_clause (sym
, map_op
);
6263 finish_oacc_declare (gfc_namespace
*ns
, gfc_symbol
*sym
, bool block
)
6266 gfc_oacc_declare
*oc
;
6267 locus where
= gfc_current_locus
;
6268 gfc_omp_clauses
*omp_clauses
= NULL
;
6269 gfc_omp_namelist
*n
, *p
;
6271 gfc_traverse_ns (ns
, find_module_oacc_declare_clauses
);
6273 if (module_oacc_clauses
&& sym
->attr
.flavor
== FL_PROGRAM
)
6275 gfc_oacc_declare
*new_oc
;
6277 new_oc
= gfc_get_oacc_declare ();
6278 new_oc
->next
= ns
->oacc_declare
;
6279 new_oc
->clauses
= module_oacc_clauses
;
6281 ns
->oacc_declare
= new_oc
;
6282 module_oacc_clauses
= NULL
;
6285 if (!ns
->oacc_declare
)
6288 for (oc
= ns
->oacc_declare
; oc
; oc
= oc
->next
)
6294 gfc_error ("Sorry, !$ACC DECLARE at %L is not allowed "
6295 "in BLOCK construct", &oc
->loc
);
6298 if (oc
->clauses
&& oc
->clauses
->lists
[OMP_LIST_MAP
])
6300 if (omp_clauses
== NULL
)
6302 omp_clauses
= oc
->clauses
;
6306 for (n
= oc
->clauses
->lists
[OMP_LIST_MAP
]; n
; p
= n
, n
= n
->next
)
6309 gcc_assert (p
->next
== NULL
);
6311 p
->next
= omp_clauses
->lists
[OMP_LIST_MAP
];
6312 omp_clauses
= oc
->clauses
;
6319 for (n
= omp_clauses
->lists
[OMP_LIST_MAP
]; n
; n
= n
->next
)
6321 switch (n
->u
.map_op
)
6323 case OMP_MAP_DEVICE_RESIDENT
:
6324 n
->u
.map_op
= OMP_MAP_FORCE_ALLOC
;
6332 code
= XCNEW (gfc_code
);
6333 code
->op
= EXEC_OACC_DECLARE
;
6336 code
->ext
.oacc_declare
= gfc_get_oacc_declare ();
6337 code
->ext
.oacc_declare
->clauses
= omp_clauses
;
6339 code
->block
= XCNEW (gfc_code
);
6340 code
->block
->op
= EXEC_OACC_DECLARE
;
6341 code
->block
->loc
= where
;
6344 code
->block
->next
= ns
->code
;
6352 /* Generate code for a function. */
6355 gfc_generate_function_code (gfc_namespace
* ns
)
6361 tree fpstate
= NULL_TREE
;
6362 stmtblock_t init
, cleanup
;
6364 gfc_wrapped_block try_block
;
6365 tree recurcheckvar
= NULL_TREE
;
6367 gfc_symbol
*previous_procedure_symbol
;
6371 sym
= ns
->proc_name
;
6372 previous_procedure_symbol
= current_procedure_symbol
;
6373 current_procedure_symbol
= sym
;
6375 /* Initialize sym->tlink so that gfc_trans_deferred_vars does not get
6379 /* Create the declaration for functions with global scope. */
6380 if (!sym
->backend_decl
)
6381 gfc_create_function_decl (ns
, false);
6383 fndecl
= sym
->backend_decl
;
6384 old_context
= current_function_decl
;
6388 push_function_context ();
6389 saved_parent_function_decls
= saved_function_decls
;
6390 saved_function_decls
= NULL_TREE
;
6393 trans_function_start (sym
);
6395 gfc_init_block (&init
);
6397 if (ns
->entries
&& ns
->proc_name
->ts
.type
== BT_CHARACTER
)
6399 /* Copy length backend_decls to all entry point result
6404 gfc_conv_const_charlen (ns
->proc_name
->ts
.u
.cl
);
6405 backend_decl
= ns
->proc_name
->result
->ts
.u
.cl
->backend_decl
;
6406 for (el
= ns
->entries
; el
; el
= el
->next
)
6407 el
->sym
->result
->ts
.u
.cl
->backend_decl
= backend_decl
;
6410 /* Translate COMMON blocks. */
6411 gfc_trans_common (ns
);
6413 /* Null the parent fake result declaration if this namespace is
6414 a module function or an external procedures. */
6415 if ((ns
->parent
&& ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
6416 || ns
->parent
== NULL
)
6417 parent_fake_result_decl
= NULL_TREE
;
6419 gfc_generate_contained_functions (ns
);
6421 nonlocal_dummy_decls
= NULL
;
6422 nonlocal_dummy_decl_pset
= NULL
;
6424 has_coarray_vars
= false;
6425 generate_local_vars (ns
);
6427 if (flag_coarray
== GFC_FCOARRAY_LIB
&& has_coarray_vars
)
6428 generate_coarray_init (ns
);
6430 /* Keep the parent fake result declaration in module functions
6431 or external procedures. */
6432 if ((ns
->parent
&& ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
6433 || ns
->parent
== NULL
)
6434 current_fake_result_decl
= parent_fake_result_decl
;
6436 current_fake_result_decl
= NULL_TREE
;
6438 is_recursive
= sym
->attr
.recursive
6439 || (sym
->attr
.entry_master
6440 && sym
->ns
->entries
->sym
->attr
.recursive
);
6441 if ((gfc_option
.rtcheck
& GFC_RTCHECK_RECURSION
)
6442 && !is_recursive
&& !flag_recursive
)
6446 msg
= xasprintf ("Recursive call to nonrecursive procedure '%s'",
6448 recurcheckvar
= gfc_create_var (logical_type_node
, "is_recursive");
6449 TREE_STATIC (recurcheckvar
) = 1;
6450 DECL_INITIAL (recurcheckvar
) = logical_false_node
;
6451 gfc_add_expr_to_block (&init
, recurcheckvar
);
6452 gfc_trans_runtime_check (true, false, recurcheckvar
, &init
,
6453 &sym
->declared_at
, msg
);
6454 gfc_add_modify (&init
, recurcheckvar
, logical_true_node
);
6458 /* Check if an IEEE module is used in the procedure. If so, save
6459 the floating point state. */
6460 ieee
= is_ieee_module_used (ns
);
6462 fpstate
= gfc_save_fp_state (&init
);
6464 /* Now generate the code for the body of this function. */
6465 gfc_init_block (&body
);
6467 if (TREE_TYPE (DECL_RESULT (fndecl
)) != void_type_node
6468 && sym
->attr
.subroutine
)
6470 tree alternate_return
;
6471 alternate_return
= gfc_get_fake_result_decl (sym
, 0);
6472 gfc_add_modify (&body
, alternate_return
, integer_zero_node
);
6477 /* Jump to the correct entry point. */
6478 tmp
= gfc_trans_entry_master_switch (ns
->entries
);
6479 gfc_add_expr_to_block (&body
, tmp
);
6482 /* If bounds-checking is enabled, generate code to check passed in actual
6483 arguments against the expected dummy argument attributes (e.g. string
6485 if ((gfc_option
.rtcheck
& GFC_RTCHECK_BOUNDS
) && !sym
->attr
.is_bind_c
)
6486 add_argument_checking (&body
, sym
);
6488 finish_oacc_declare (ns
, sym
, false);
6490 tmp
= gfc_trans_code (ns
->code
);
6491 gfc_add_expr_to_block (&body
, tmp
);
6493 if (TREE_TYPE (DECL_RESULT (fndecl
)) != void_type_node
6494 || (sym
->result
&& sym
->result
!= sym
6495 && sym
->result
->ts
.type
== BT_DERIVED
6496 && sym
->result
->ts
.u
.derived
->attr
.alloc_comp
))
6498 bool artificial_result_decl
= false;
6499 tree result
= get_proc_result (sym
);
6500 gfc_symbol
*rsym
= sym
== sym
->result
? sym
: sym
->result
;
6502 /* Make sure that a function returning an object with
6503 alloc/pointer_components always has a result, where at least
6504 the allocatable/pointer components are set to zero. */
6505 if (result
== NULL_TREE
&& sym
->attr
.function
6506 && ((sym
->result
->ts
.type
== BT_DERIVED
6507 && (sym
->attr
.allocatable
6508 || sym
->attr
.pointer
6509 || sym
->result
->ts
.u
.derived
->attr
.alloc_comp
6510 || sym
->result
->ts
.u
.derived
->attr
.pointer_comp
))
6511 || (sym
->result
->ts
.type
== BT_CLASS
6512 && (CLASS_DATA (sym
)->attr
.allocatable
6513 || CLASS_DATA (sym
)->attr
.class_pointer
6514 || CLASS_DATA (sym
->result
)->attr
.alloc_comp
6515 || CLASS_DATA (sym
->result
)->attr
.pointer_comp
))))
6517 artificial_result_decl
= true;
6518 result
= gfc_get_fake_result_decl (sym
, 0);
6521 if (result
!= NULL_TREE
&& sym
->attr
.function
&& !sym
->attr
.pointer
)
6523 if (sym
->attr
.allocatable
&& sym
->attr
.dimension
== 0
6524 && sym
->result
== sym
)
6525 gfc_add_modify (&init
, result
, fold_convert (TREE_TYPE (result
),
6526 null_pointer_node
));
6527 else if (sym
->ts
.type
== BT_CLASS
6528 && CLASS_DATA (sym
)->attr
.allocatable
6529 && CLASS_DATA (sym
)->attr
.dimension
== 0
6530 && sym
->result
== sym
)
6532 tmp
= CLASS_DATA (sym
)->backend_decl
;
6533 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
,
6534 TREE_TYPE (tmp
), result
, tmp
, NULL_TREE
);
6535 gfc_add_modify (&init
, tmp
, fold_convert (TREE_TYPE (tmp
),
6536 null_pointer_node
));
6538 else if (sym
->ts
.type
== BT_DERIVED
6539 && !sym
->attr
.allocatable
)
6542 /* Arrays are not initialized using the default initializer of
6543 their elements. Therefore only check if a default
6544 initializer is available when the result is scalar. */
6545 init_exp
= rsym
->as
? NULL
6546 : gfc_generate_initializer (&rsym
->ts
, true);
6549 tmp
= gfc_trans_structure_assign (result
, init_exp
, 0);
6550 gfc_free_expr (init_exp
);
6551 gfc_add_expr_to_block (&init
, tmp
);
6553 else if (rsym
->ts
.u
.derived
->attr
.alloc_comp
)
6555 rank
= rsym
->as
? rsym
->as
->rank
: 0;
6556 tmp
= gfc_nullify_alloc_comp (rsym
->ts
.u
.derived
, result
,
6558 gfc_prepend_expr_to_block (&body
, tmp
);
6563 if (result
== NULL_TREE
|| artificial_result_decl
)
6565 /* TODO: move to the appropriate place in resolve.c. */
6566 if (warn_return_type
> 0 && sym
== sym
->result
)
6567 gfc_warning (OPT_Wreturn_type
,
6568 "Return value of function %qs at %L not set",
6569 sym
->name
, &sym
->declared_at
);
6570 if (warn_return_type
> 0)
6571 TREE_NO_WARNING(sym
->backend_decl
) = 1;
6573 if (result
!= NULL_TREE
)
6574 gfc_add_expr_to_block (&body
, gfc_generate_return ());
6577 gfc_init_block (&cleanup
);
6579 /* Reset recursion-check variable. */
6580 if ((gfc_option
.rtcheck
& GFC_RTCHECK_RECURSION
)
6581 && !is_recursive
&& !flag_openmp
&& recurcheckvar
!= NULL_TREE
)
6583 gfc_add_modify (&cleanup
, recurcheckvar
, logical_false_node
);
6584 recurcheckvar
= NULL
;
6587 /* If IEEE modules are loaded, restore the floating-point state. */
6589 gfc_restore_fp_state (&cleanup
, fpstate
);
6591 /* Finish the function body and add init and cleanup code. */
6592 tmp
= gfc_finish_block (&body
);
6593 gfc_start_wrapped_block (&try_block
, tmp
);
6594 /* Add code to create and cleanup arrays. */
6595 gfc_trans_deferred_vars (sym
, &try_block
);
6596 gfc_add_init_cleanup (&try_block
, gfc_finish_block (&init
),
6597 gfc_finish_block (&cleanup
));
6599 /* Add all the decls we created during processing. */
6600 decl
= nreverse (saved_function_decls
);
6605 next
= DECL_CHAIN (decl
);
6606 DECL_CHAIN (decl
) = NULL_TREE
;
6610 saved_function_decls
= NULL_TREE
;
6612 DECL_SAVED_TREE (fndecl
) = gfc_finish_wrapped_block (&try_block
);
6615 /* Finish off this function and send it for code generation. */
6617 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
6619 DECL_SAVED_TREE (fndecl
)
6620 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (fndecl
),
6621 DECL_INITIAL (fndecl
));
6623 if (nonlocal_dummy_decls
)
6625 BLOCK_VARS (DECL_INITIAL (fndecl
))
6626 = chainon (BLOCK_VARS (DECL_INITIAL (fndecl
)), nonlocal_dummy_decls
);
6627 delete nonlocal_dummy_decl_pset
;
6628 nonlocal_dummy_decls
= NULL
;
6629 nonlocal_dummy_decl_pset
= NULL
;
6632 /* Output the GENERIC tree. */
6633 dump_function (TDI_original
, fndecl
);
6635 /* Store the end of the function, so that we get good line number
6636 info for the epilogue. */
6637 cfun
->function_end_locus
= input_location
;
6639 /* We're leaving the context of this function, so zap cfun.
6640 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6641 tree_rest_of_compilation. */
6646 pop_function_context ();
6647 saved_function_decls
= saved_parent_function_decls
;
6649 current_function_decl
= old_context
;
6651 if (decl_function_context (fndecl
))
6653 /* Register this function with cgraph just far enough to get it
6654 added to our parent's nested function list.
6655 If there are static coarrays in this function, the nested _caf_init
6656 function has already called cgraph_create_node, which also created
6657 the cgraph node for this function. */
6658 if (!has_coarray_vars
|| flag_coarray
!= GFC_FCOARRAY_LIB
)
6659 (void) cgraph_node::get_create (fndecl
);
6662 cgraph_node::finalize_function (fndecl
, true);
6664 gfc_trans_use_stmts (ns
);
6665 gfc_traverse_ns (ns
, gfc_emit_parameter_debug_info
);
6667 if (sym
->attr
.is_main_program
)
6668 create_main_function (fndecl
);
6670 current_procedure_symbol
= previous_procedure_symbol
;
6675 gfc_generate_constructors (void)
6677 gcc_assert (gfc_static_ctors
== NULL_TREE
);
6685 if (gfc_static_ctors
== NULL_TREE
)
6688 fnname
= get_file_function_name ("I");
6689 type
= build_function_type_list (void_type_node
, NULL_TREE
);
6691 fndecl
= build_decl (input_location
,
6692 FUNCTION_DECL
, fnname
, type
);
6693 TREE_PUBLIC (fndecl
) = 1;
6695 decl
= build_decl (input_location
,
6696 RESULT_DECL
, NULL_TREE
, void_type_node
);
6697 DECL_ARTIFICIAL (decl
) = 1;
6698 DECL_IGNORED_P (decl
) = 1;
6699 DECL_CONTEXT (decl
) = fndecl
;
6700 DECL_RESULT (fndecl
) = decl
;
6704 current_function_decl
= fndecl
;
6706 rest_of_decl_compilation (fndecl
, 1, 0);
6708 make_decl_rtl (fndecl
);
6710 allocate_struct_function (fndecl
, false);
6714 for (; gfc_static_ctors
; gfc_static_ctors
= TREE_CHAIN (gfc_static_ctors
))
6716 tmp
= build_call_expr_loc (input_location
,
6717 TREE_VALUE (gfc_static_ctors
), 0);
6718 DECL_SAVED_TREE (fndecl
) = build_stmt (input_location
, EXPR_STMT
, tmp
);
6724 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
6725 DECL_SAVED_TREE (fndecl
)
6726 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (fndecl
),
6727 DECL_INITIAL (fndecl
));
6729 free_after_parsing (cfun
);
6730 free_after_compilation (cfun
);
6732 tree_rest_of_compilation (fndecl
);
6734 current_function_decl
= NULL_TREE
;
6738 /* Translates a BLOCK DATA program unit. This means emitting the
6739 commons contained therein plus their initializations. We also emit
6740 a globally visible symbol to make sure that each BLOCK DATA program
6741 unit remains unique. */
6744 gfc_generate_block_data (gfc_namespace
* ns
)
6749 /* Tell the backend the source location of the block data. */
6751 gfc_set_backend_locus (&ns
->proc_name
->declared_at
);
6753 gfc_set_backend_locus (&gfc_current_locus
);
6755 /* Process the DATA statements. */
6756 gfc_trans_common (ns
);
6758 /* Create a global symbol with the mane of the block data. This is to
6759 generate linker errors if the same name is used twice. It is never
6762 id
= gfc_sym_mangled_function_id (ns
->proc_name
);
6764 id
= get_identifier ("__BLOCK_DATA__");
6766 decl
= build_decl (input_location
,
6767 VAR_DECL
, id
, gfc_array_index_type
);
6768 TREE_PUBLIC (decl
) = 1;
6769 TREE_STATIC (decl
) = 1;
6770 DECL_IGNORED_P (decl
) = 1;
6773 rest_of_decl_compilation (decl
, 1, 0);
6777 /* Process the local variables of a BLOCK construct. */
6780 gfc_process_block_locals (gfc_namespace
* ns
)
6784 gcc_assert (saved_local_decls
== NULL_TREE
);
6785 has_coarray_vars
= false;
6787 generate_local_vars (ns
);
6789 if (flag_coarray
== GFC_FCOARRAY_LIB
&& has_coarray_vars
)
6790 generate_coarray_init (ns
);
6792 decl
= nreverse (saved_local_decls
);
6797 next
= DECL_CHAIN (decl
);
6798 DECL_CHAIN (decl
) = NULL_TREE
;
6802 saved_local_decls
= NULL_TREE
;
6806 #include "gt-fortran-trans-decl.h"