1 /* Backend function setup
2 Copyright (C) 2002-2024 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.cc -- 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"
49 #include "omp-general.h"
50 #include "attr-fnspec.h"
51 #include "tree-iterator.h"
53 #define MAX_LABEL_VALUE 99999
56 /* Holds the result of the function if no result variable specified. */
58 static GTY(()) tree current_fake_result_decl
;
59 static GTY(()) tree parent_fake_result_decl
;
62 /* Holds the variable DECLs for the current function. */
64 static GTY(()) tree saved_function_decls
;
65 static GTY(()) tree saved_parent_function_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_at
;
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
;
126 /* Coarray run-time library function decls. */
127 tree gfor_fndecl_caf_init
;
128 tree gfor_fndecl_caf_finalize
;
129 tree gfor_fndecl_caf_this_image
;
130 tree gfor_fndecl_caf_num_images
;
131 tree gfor_fndecl_caf_register
;
132 tree gfor_fndecl_caf_deregister
;
133 tree gfor_fndecl_caf_get
;
134 tree gfor_fndecl_caf_send
;
135 tree gfor_fndecl_caf_sendget
;
136 tree gfor_fndecl_caf_get_by_ref
;
137 tree gfor_fndecl_caf_send_by_ref
;
138 tree gfor_fndecl_caf_sendget_by_ref
;
139 tree gfor_fndecl_caf_sync_all
;
140 tree gfor_fndecl_caf_sync_memory
;
141 tree gfor_fndecl_caf_sync_images
;
142 tree gfor_fndecl_caf_stop_str
;
143 tree gfor_fndecl_caf_stop_numeric
;
144 tree gfor_fndecl_caf_error_stop
;
145 tree gfor_fndecl_caf_error_stop_str
;
146 tree gfor_fndecl_caf_atomic_def
;
147 tree gfor_fndecl_caf_atomic_ref
;
148 tree gfor_fndecl_caf_atomic_cas
;
149 tree gfor_fndecl_caf_atomic_op
;
150 tree gfor_fndecl_caf_lock
;
151 tree gfor_fndecl_caf_unlock
;
152 tree gfor_fndecl_caf_event_post
;
153 tree gfor_fndecl_caf_event_wait
;
154 tree gfor_fndecl_caf_event_query
;
155 tree gfor_fndecl_caf_fail_image
;
156 tree gfor_fndecl_caf_failed_images
;
157 tree gfor_fndecl_caf_image_status
;
158 tree gfor_fndecl_caf_stopped_images
;
159 tree gfor_fndecl_caf_form_team
;
160 tree gfor_fndecl_caf_change_team
;
161 tree gfor_fndecl_caf_end_team
;
162 tree gfor_fndecl_caf_sync_team
;
163 tree gfor_fndecl_caf_get_team
;
164 tree gfor_fndecl_caf_team_number
;
165 tree gfor_fndecl_co_broadcast
;
166 tree gfor_fndecl_co_max
;
167 tree gfor_fndecl_co_min
;
168 tree gfor_fndecl_co_reduce
;
169 tree gfor_fndecl_co_sum
;
170 tree gfor_fndecl_caf_is_present
;
171 tree gfor_fndecl_caf_random_init
;
174 /* Math functions. Many other math functions are handled in
175 trans-intrinsic.cc. */
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_iargc
;
216 tree gfor_fndecl_kill
;
217 tree gfor_fndecl_kill_sub
;
218 tree gfor_fndecl_is_contiguous0
;
221 /* Intrinsic functions implemented in Fortran. */
222 tree gfor_fndecl_sc_kind
;
223 tree gfor_fndecl_si_kind
;
224 tree gfor_fndecl_sr_kind
;
226 /* BLAS gemm functions. */
227 tree gfor_fndecl_sgemm
;
228 tree gfor_fndecl_dgemm
;
229 tree gfor_fndecl_cgemm
;
230 tree gfor_fndecl_zgemm
;
232 /* RANDOM_INIT function. */
233 tree gfor_fndecl_random_init
; /* libgfortran, 1 image only. */
236 gfc_add_decl_to_parent_function (tree decl
)
239 DECL_CONTEXT (decl
) = DECL_CONTEXT (current_function_decl
);
240 DECL_NONLOCAL (decl
) = 1;
241 DECL_CHAIN (decl
) = saved_parent_function_decls
;
242 saved_parent_function_decls
= decl
;
246 gfc_add_decl_to_function (tree decl
)
249 TREE_USED (decl
) = 1;
250 DECL_CONTEXT (decl
) = current_function_decl
;
251 DECL_CHAIN (decl
) = saved_function_decls
;
252 saved_function_decls
= decl
;
256 add_decl_as_local (tree decl
)
259 TREE_USED (decl
) = 1;
260 DECL_CONTEXT (decl
) = current_function_decl
;
261 DECL_CHAIN (decl
) = saved_local_decls
;
262 saved_local_decls
= decl
;
266 /* Build a backend label declaration. Set TREE_USED for named labels.
267 The context of the label is always the current_function_decl. All
268 labels are marked artificial. */
271 gfc_build_label_decl (tree label_id
)
273 /* 2^32 temporaries should be enough. */
274 static unsigned int tmp_num
= 1;
278 if (label_id
== NULL_TREE
)
280 /* Build an internal label name. */
281 ASM_FORMAT_PRIVATE_NAME (label_name
, "L", tmp_num
++);
282 label_id
= get_identifier (label_name
);
287 /* Build the LABEL_DECL node. Labels have no type. */
288 label_decl
= build_decl (input_location
,
289 LABEL_DECL
, label_id
, void_type_node
);
290 DECL_CONTEXT (label_decl
) = current_function_decl
;
291 SET_DECL_MODE (label_decl
, VOIDmode
);
293 /* We always define the label as used, even if the original source
294 file never references the label. We don't want all kinds of
295 spurious warnings for old-style Fortran code with too many
297 TREE_USED (label_decl
) = 1;
299 DECL_ARTIFICIAL (label_decl
) = 1;
304 /* Set the backend source location of a decl. */
307 gfc_set_decl_location (tree decl
, locus
* loc
)
309 DECL_SOURCE_LOCATION (decl
) = gfc_get_location (loc
);
313 /* Return the backend label declaration for a given label structure,
314 or create it if it doesn't exist yet. */
317 gfc_get_label_decl (gfc_st_label
* lp
)
319 if (lp
->backend_decl
)
320 return lp
->backend_decl
;
323 char label_name
[GFC_MAX_SYMBOL_LEN
+ 1];
326 /* Validate the label declaration from the front end. */
327 gcc_assert (lp
!= NULL
&& lp
->value
<= MAX_LABEL_VALUE
);
329 /* Build a mangled name for the label. */
330 sprintf (label_name
, "__label_%.6d", lp
->value
);
332 /* Build the LABEL_DECL node. */
333 label_decl
= gfc_build_label_decl (get_identifier (label_name
));
335 /* Tell the debugger where the label came from. */
336 if (lp
->value
<= MAX_LABEL_VALUE
) /* An internal label. */
337 gfc_set_decl_location (label_decl
, &lp
->where
);
339 DECL_ARTIFICIAL (label_decl
) = 1;
341 /* Store the label in the label list and return the LABEL_DECL. */
342 lp
->backend_decl
= label_decl
;
347 /* Return the name of an identifier. */
350 sym_identifier (gfc_symbol
*sym
)
352 if (sym
->attr
.is_main_program
&& strcmp (sym
->name
, "main") == 0)
358 /* Convert a gfc_symbol to an identifier of the same name. */
361 gfc_sym_identifier (gfc_symbol
* sym
)
363 return get_identifier (sym_identifier (sym
));
366 /* Construct mangled name from symbol name. */
369 mangled_identifier (gfc_symbol
*sym
)
371 gfc_symbol
*proc
= sym
->ns
->proc_name
;
372 static char name
[3*GFC_MAX_MANGLED_SYMBOL_LEN
+ 14];
373 /* Prevent the mangling of identifiers that have an assigned
374 binding label (mainly those that are bind(c)). */
376 if (sym
->attr
.is_bind_c
== 1 && sym
->binding_label
)
377 return sym
->binding_label
;
379 if (!sym
->fn_result_spec
380 || (sym
->module
&& !(proc
&& proc
->attr
.flavor
== FL_PROCEDURE
)))
382 if (sym
->module
== NULL
)
383 return sym_identifier (sym
);
385 snprintf (name
, sizeof name
, "__%s_MOD_%s", sym
->module
, sym
->name
);
389 /* This is an entity that is actually local to a module procedure
390 that appears in the result specification expression. Since
391 sym->module will be a zero length string, we use ns->proc_name
392 to provide the module name instead. */
393 if (proc
&& proc
->module
)
394 snprintf (name
, sizeof name
, "__%s_MOD__%s_PROC_%s",
395 proc
->module
, proc
->name
, sym
->name
);
397 snprintf (name
, sizeof name
, "__%s_PROC_%s",
398 proc
->name
, sym
->name
);
404 /* Get mangled identifier, adding the symbol to the global table if
405 it is not yet already there. */
408 gfc_sym_mangled_identifier (gfc_symbol
* sym
)
414 name
= mangled_identifier (sym
);
415 result
= get_identifier (name
);
417 gsym
= gfc_find_gsymbol (gfc_gsym_root
, name
);
420 gsym
= gfc_get_gsymbol (name
, false);
422 gsym
->sym_name
= sym
->name
;
428 /* Construct mangled function name from symbol name. */
431 gfc_sym_mangled_function_id (gfc_symbol
* sym
)
434 char name
[GFC_MAX_MANGLED_SYMBOL_LEN
+ 1];
436 /* It may be possible to simply use the binding label if it's
437 provided, and remove the other checks. Then we could use it
438 for other things if we wished. */
439 if ((sym
->attr
.is_bind_c
== 1 || sym
->attr
.is_iso_c
== 1) &&
441 /* use the binding label rather than the mangled name */
442 return get_identifier (sym
->binding_label
);
444 if ((sym
->module
== NULL
|| sym
->attr
.proc
== PROC_EXTERNAL
445 || (sym
->module
!= NULL
&& (sym
->attr
.external
446 || sym
->attr
.if_source
== IFSRC_IFBODY
)))
447 && !sym
->attr
.module_procedure
)
449 /* Main program is mangled into MAIN__. */
450 if (sym
->attr
.is_main_program
)
451 return get_identifier ("MAIN__");
453 /* Intrinsic procedures are never mangled. */
454 if (sym
->attr
.proc
== PROC_INTRINSIC
)
455 return get_identifier (sym
->name
);
457 if (flag_underscoring
)
459 has_underscore
= strchr (sym
->name
, '_') != 0;
460 if (flag_second_underscore
&& has_underscore
)
461 snprintf (name
, sizeof name
, "%s__", sym
->name
);
463 snprintf (name
, sizeof name
, "%s_", sym
->name
);
464 return get_identifier (name
);
467 return get_identifier (sym
->name
);
471 snprintf (name
, sizeof name
, "__%s_MOD_%s", sym
->module
, sym
->name
);
472 return get_identifier (name
);
478 gfc_set_decl_assembler_name (tree decl
, tree name
)
480 tree target_mangled
= targetm
.mangle_decl_assembler_name (decl
, name
);
481 SET_DECL_ASSEMBLER_NAME (decl
, target_mangled
);
485 /* Returns true if a variable of specified size should go on the stack. */
488 gfc_can_put_var_on_stack (tree size
)
490 unsigned HOST_WIDE_INT low
;
492 if (!INTEGER_CST_P (size
))
495 if (flag_max_stack_var_size
< 0)
498 if (!tree_fits_uhwi_p (size
))
501 low
= TREE_INT_CST_LOW (size
);
502 if (low
> (unsigned HOST_WIDE_INT
) flag_max_stack_var_size
)
505 /* TODO: Set a per-function stack size limit. */
511 /* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
512 an expression involving its corresponding pointer. There are
513 2 cases; one for variable size arrays, and one for everything else,
514 because variable-sized arrays require one fewer level of
518 gfc_finish_cray_pointee (tree decl
, gfc_symbol
*sym
)
520 tree ptr_decl
= gfc_get_symbol_decl (sym
->cp_pointer
);
523 /* Parameters need to be dereferenced. */
524 if (sym
->cp_pointer
->attr
.dummy
)
525 ptr_decl
= build_fold_indirect_ref_loc (input_location
,
528 /* Check to see if we're dealing with a variable-sized array. */
529 if (sym
->attr
.dimension
530 && TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
)
532 /* These decls will be dereferenced later, so we don't dereference
534 value
= convert (TREE_TYPE (decl
), ptr_decl
);
538 ptr_decl
= convert (build_pointer_type (TREE_TYPE (decl
)),
540 value
= build_fold_indirect_ref_loc (input_location
,
544 SET_DECL_VALUE_EXPR (decl
, value
);
545 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
546 GFC_DECL_CRAY_POINTEE (decl
) = 1;
550 /* Finish processing of a declaration without an initial value. */
553 gfc_finish_decl (tree decl
)
555 gcc_assert (TREE_CODE (decl
) == PARM_DECL
556 || DECL_INITIAL (decl
) == NULL_TREE
);
561 if (DECL_SIZE (decl
) == NULL_TREE
562 && COMPLETE_TYPE_P (TREE_TYPE (decl
)))
563 layout_decl (decl
, 0);
565 /* A few consistency checks. */
566 /* A static variable with an incomplete type is an error if it is
567 initialized. Also if it is not file scope. Otherwise, let it
568 through, but if it is not `extern' then it may cause an error
570 /* An automatic variable with an incomplete type is an error. */
572 /* We should know the storage size. */
573 gcc_assert (DECL_SIZE (decl
) != NULL_TREE
574 || (TREE_STATIC (decl
)
575 ? (!DECL_INITIAL (decl
) || !DECL_CONTEXT (decl
))
576 : DECL_EXTERNAL (decl
)));
578 /* The storage size should be constant. */
579 gcc_assert ((!DECL_EXTERNAL (decl
) && !TREE_STATIC (decl
))
581 || TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
);
585 /* Handle setting of GFC_DECL_SCALAR* on DECL. */
588 gfc_finish_decl_attrs (tree decl
, symbol_attribute
*attr
)
590 if (!attr
->dimension
&& !attr
->codimension
)
592 /* Handle scalar allocatable variables. */
593 if (attr
->allocatable
)
595 gfc_allocate_lang_decl (decl
);
596 GFC_DECL_SCALAR_ALLOCATABLE (decl
) = 1;
598 /* Handle scalar pointer variables. */
601 gfc_allocate_lang_decl (decl
);
602 GFC_DECL_SCALAR_POINTER (decl
) = 1;
606 gfc_allocate_lang_decl (decl
);
607 GFC_DECL_SCALAR_TARGET (decl
) = 1;
613 /* Apply symbol attributes to a variable, and add it to the function scope. */
616 gfc_finish_var_decl (tree decl
, gfc_symbol
* sym
)
620 /* Set DECL_VALUE_EXPR for Cray Pointees. */
621 if (sym
->attr
.cray_pointee
)
622 gfc_finish_cray_pointee (decl
, sym
);
624 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
625 This is the equivalent of the TARGET variables.
626 We also need to set this if the variable is passed by reference in a
628 if (sym
->attr
.target
)
629 TREE_ADDRESSABLE (decl
) = 1;
631 /* If it wasn't used we wouldn't be getting it. */
632 TREE_USED (decl
) = 1;
634 if (sym
->attr
.flavor
== FL_PARAMETER
635 && (sym
->attr
.dimension
|| sym
->ts
.type
== BT_DERIVED
))
636 TREE_READONLY (decl
) = 1;
638 /* Chain this decl to the pending declarations. Don't do pushdecl()
639 because this would add them to the current scope rather than the
641 if (current_function_decl
!= NULL_TREE
)
643 if (sym
->ns
->proc_name
644 && (sym
->ns
->proc_name
->backend_decl
== current_function_decl
645 || sym
->result
== sym
))
646 gfc_add_decl_to_function (decl
);
647 else if (sym
->ns
->proc_name
648 && sym
->ns
->proc_name
->attr
.flavor
== FL_LABEL
)
649 /* This is a BLOCK construct. */
650 add_decl_as_local (decl
);
651 else if (sym
->ns
->omp_affinity_iterators
)
652 /* This is a block-local iterator. */
653 add_decl_as_local (decl
);
655 gfc_add_decl_to_parent_function (decl
);
658 if (sym
->attr
.cray_pointee
)
661 if(sym
->attr
.is_bind_c
== 1 && sym
->binding_label
)
663 /* We need to put variables that are bind(c) into the common
664 segment of the object file, because this is what C would do.
665 gfortran would typically put them in either the BSS or
666 initialized data segments, and only mark them as common if
667 they were part of common blocks. However, if they are not put
668 into common space, then C cannot initialize global Fortran
669 variables that it interoperates with and the draft says that
670 either Fortran or C should be able to initialize it (but not
671 both, of course.) (J3/04-007, section 15.3). */
672 TREE_PUBLIC(decl
) = 1;
673 DECL_COMMON(decl
) = 1;
674 if (sym
->attr
.access
== ACCESS_PRIVATE
&& !sym
->attr
.public_used
)
676 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
677 DECL_VISIBILITY_SPECIFIED (decl
) = true;
681 /* If a variable is USE associated, it's always external. */
682 if (sym
->attr
.use_assoc
|| sym
->attr
.used_in_submodule
)
684 DECL_EXTERNAL (decl
) = 1;
685 TREE_PUBLIC (decl
) = 1;
687 else if (sym
->fn_result_spec
&& !sym
->ns
->proc_name
->module
)
690 if (sym
->ns
->proc_name
->attr
.if_source
!= IFSRC_DECL
)
691 DECL_EXTERNAL (decl
) = 1;
693 TREE_STATIC (decl
) = 1;
695 TREE_PUBLIC (decl
) = 1;
697 else if (sym
->module
&& !sym
->attr
.result
&& !sym
->attr
.dummy
)
699 /* TODO: Don't set sym->module for result or dummy variables. */
700 gcc_assert (current_function_decl
== NULL_TREE
|| sym
->result
== sym
);
702 TREE_PUBLIC (decl
) = 1;
703 TREE_STATIC (decl
) = 1;
704 if (sym
->attr
.access
== ACCESS_PRIVATE
&& !sym
->attr
.public_used
)
706 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
707 DECL_VISIBILITY_SPECIFIED (decl
) = true;
711 /* Derived types are a bit peculiar because of the possibility of
712 a default initializer; this must be applied each time the variable
713 comes into scope it therefore need not be static. These variables
714 are SAVE_NONE but have an initializer. Otherwise explicitly
715 initialized variables are SAVE_IMPLICIT and explicitly saved are
717 if (!sym
->attr
.use_assoc
718 && (sym
->attr
.save
!= SAVE_NONE
|| sym
->attr
.data
719 || (sym
->value
&& sym
->ns
->proc_name
->attr
.is_main_program
)
720 || (flag_coarray
== GFC_FCOARRAY_LIB
721 && sym
->attr
.codimension
&& !sym
->attr
.allocatable
)))
722 TREE_STATIC (decl
) = 1;
724 /* If derived-type variables with DTIO procedures are not made static
725 some bits of code referencing them get optimized away.
726 TODO Understand why this is so and fix it. */
727 if (!sym
->attr
.use_assoc
728 && ((sym
->ts
.type
== BT_DERIVED
729 && sym
->ts
.u
.derived
->attr
.has_dtio_procs
)
730 || (sym
->ts
.type
== BT_CLASS
731 && CLASS_DATA (sym
)->ts
.u
.derived
->attr
.has_dtio_procs
)))
732 TREE_STATIC (decl
) = 1;
734 /* Treat asynchronous variables the same as volatile, for now. */
735 if (sym
->attr
.volatile_
|| sym
->attr
.asynchronous
)
737 TREE_THIS_VOLATILE (decl
) = 1;
738 TREE_SIDE_EFFECTS (decl
) = 1;
739 new_type
= build_qualified_type (TREE_TYPE (decl
), TYPE_QUAL_VOLATILE
);
740 TREE_TYPE (decl
) = new_type
;
743 /* Keep variables larger than max-stack-var-size off stack. */
744 if (!(sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.recursive
)
745 && !sym
->attr
.automatic
746 && !sym
->attr
.associate_var
747 && sym
->attr
.save
!= SAVE_EXPLICIT
748 && sym
->attr
.save
!= SAVE_IMPLICIT
749 && INTEGER_CST_P (DECL_SIZE_UNIT (decl
))
750 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl
))
751 /* Put variable length auto array pointers always into stack. */
752 && (TREE_CODE (TREE_TYPE (decl
)) != POINTER_TYPE
753 || sym
->attr
.dimension
== 0
754 || sym
->as
->type
!= AS_EXPLICIT
756 || sym
->attr
.allocatable
)
757 && !DECL_ARTIFICIAL (decl
))
759 if (flag_max_stack_var_size
> 0
760 && !(sym
->ns
->proc_name
761 && sym
->ns
->proc_name
->attr
.is_main_program
))
762 gfc_warning (OPT_Wsurprising
,
763 "Array %qs at %L is larger than limit set by "
764 "%<-fmax-stack-var-size=%>, moved from stack to static "
765 "storage. This makes the procedure unsafe when called "
766 "recursively, or concurrently from multiple threads. "
767 "Consider increasing the %<-fmax-stack-var-size=%> "
768 "limit (or use %<-frecursive%>, which implies "
769 "unlimited %<-fmax-stack-var-size%>) - or change the "
770 "code to use an ALLOCATABLE array. If the variable is "
771 "never accessed concurrently, this warning can be "
772 "ignored, and the variable could also be declared with "
773 "the SAVE attribute.",
774 sym
->name
, &sym
->declared_at
);
776 TREE_STATIC (decl
) = 1;
778 /* Because the size of this variable isn't known until now, we may have
779 greedily added an initializer to this variable (in build_init_assign)
780 even though the max-stack-var-size indicates the variable should be
781 static. Therefore we rip out the automatic initializer here and
782 replace it with a static one. */
783 gfc_symtree
*st
= gfc_find_symtree (sym
->ns
->sym_root
, sym
->name
);
784 gfc_code
*prev
= NULL
;
785 gfc_code
*code
= sym
->ns
->code
;
786 while (code
&& code
->op
== EXEC_INIT_ASSIGN
)
788 /* Look for an initializer meant for this symbol. */
789 if (code
->expr1
->symtree
== st
)
792 prev
->next
= code
->next
;
794 sym
->ns
->code
= code
->next
;
802 if (code
&& code
->op
== EXEC_INIT_ASSIGN
)
804 /* Keep the init expression for a static initializer. */
805 sym
->value
= code
->expr2
;
806 /* Cleanup the defunct code object, without freeing the init expr. */
808 gfc_free_statement (code
);
813 /* Handle threadprivate variables. */
814 if (sym
->attr
.threadprivate
815 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
816 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
818 /* Mark weak variables. */
819 if (sym
->attr
.ext_attr
& (1 << EXT_ATTR_WEAK
))
822 gfc_finish_decl_attrs (decl
, &sym
->attr
);
826 /* Allocate the lang-specific part of a decl. */
829 gfc_allocate_lang_decl (tree decl
)
831 if (DECL_LANG_SPECIFIC (decl
) == NULL
)
832 DECL_LANG_SPECIFIC (decl
) = ggc_cleared_alloc
<struct lang_decl
> ();
835 /* Remember a symbol to generate initialization/cleanup code at function
839 gfc_defer_symbol_init (gfc_symbol
* sym
)
845 /* Don't add a symbol twice. */
849 last
= head
= sym
->ns
->proc_name
;
852 /* Make sure that setup code for dummy variables which are used in the
853 setup of other variables is generated first. */
856 /* Find the first dummy arg seen after us, or the first non-dummy arg.
857 This is a circular list, so don't go past the head. */
859 && (!p
->attr
.dummy
|| p
->dummy_order
> sym
->dummy_order
))
865 /* Insert in between last and p. */
871 /* Used in gfc_get_symbol_decl and gfc_get_derived_type to obtain the
872 backend_decl for a module symbol, if it all ready exists. If the
873 module gsymbol does not exist, it is created. If the symbol does
874 not exist, it is added to the gsymbol namespace. Returns true if
875 an existing backend_decl is found. */
878 gfc_get_module_backend_decl (gfc_symbol
*sym
)
884 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->module
);
886 if (!gsym
|| (gsym
->ns
&& gsym
->type
== GSYM_MODULE
))
891 /* Check for a symbol with the same name. */
893 gfc_find_symbol (sym
->name
, gsym
->ns
, 0, &s
);
899 gsym
= gfc_get_gsymbol (sym
->module
, false);
900 gsym
->type
= GSYM_MODULE
;
901 gsym
->ns
= gfc_get_namespace (NULL
, 0);
904 st
= gfc_new_symtree (&gsym
->ns
->sym_root
, sym
->name
);
908 else if (gfc_fl_struct (sym
->attr
.flavor
))
910 if (s
&& s
->attr
.flavor
== FL_PROCEDURE
)
913 gcc_assert (s
->attr
.generic
);
914 for (intr
= s
->generic
; intr
; intr
= intr
->next
)
915 if (gfc_fl_struct (intr
->sym
->attr
.flavor
))
922 /* Normally we can assume that s is a derived-type symbol since it
923 shares a name with the derived-type sym. However if sym is a
924 STRUCTURE, it may in fact share a name with any other basic type
925 variable. If s is in fact of derived type then we can continue
926 looking for a duplicate type declaration. */
927 if (sym
->attr
.flavor
== FL_STRUCT
&& s
->ts
.type
== BT_DERIVED
)
932 if (gfc_fl_struct (s
->attr
.flavor
) && !s
->backend_decl
)
934 if (s
->attr
.flavor
== FL_UNION
)
935 s
->backend_decl
= gfc_get_union_type (s
);
937 s
->backend_decl
= gfc_get_derived_type (s
);
939 gfc_copy_dt_decls_ifequal (s
, sym
, true);
942 else if (s
->backend_decl
)
944 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
945 gfc_copy_dt_decls_ifequal (s
->ts
.u
.derived
, sym
->ts
.u
.derived
,
947 else if (sym
->ts
.type
== BT_CHARACTER
)
948 sym
->ts
.u
.cl
->backend_decl
= s
->ts
.u
.cl
->backend_decl
;
949 sym
->backend_decl
= s
->backend_decl
;
957 /* Create an array index type variable with function scope. */
960 create_index_var (const char * pfx
, int nest
)
964 decl
= gfc_create_var_np (gfc_array_index_type
, pfx
);
966 gfc_add_decl_to_parent_function (decl
);
968 gfc_add_decl_to_function (decl
);
973 /* Create variables to hold all the non-constant bits of info for a
974 descriptorless array. Remember these in the lang-specific part of the
978 gfc_build_qualified_array (tree decl
, gfc_symbol
* sym
)
983 gfc_namespace
* procns
;
984 symbol_attribute
*array_attr
;
986 bool is_classarray
= IS_CLASS_ARRAY (sym
);
988 type
= TREE_TYPE (decl
);
989 array_attr
= is_classarray
? &CLASS_DATA (sym
)->attr
: &sym
->attr
;
990 as
= is_classarray
? CLASS_DATA (sym
)->as
: sym
->as
;
992 /* We just use the descriptor, if there is one. */
993 if (GFC_DESCRIPTOR_TYPE_P (type
))
996 gcc_assert (GFC_ARRAY_TYPE_P (type
));
997 procns
= gfc_find_proc_namespace (sym
->ns
);
998 nest
= (procns
->proc_name
->backend_decl
!= current_function_decl
)
999 && !sym
->attr
.contained
;
1001 if (array_attr
->codimension
&& flag_coarray
== GFC_FCOARRAY_LIB
1002 && as
->type
!= AS_ASSUMED_SHAPE
1003 && GFC_TYPE_ARRAY_CAF_TOKEN (type
) == NULL_TREE
)
1006 tree token_type
= build_qualified_type (pvoid_type_node
,
1007 TYPE_QUAL_RESTRICT
);
1009 if (sym
->module
&& (sym
->attr
.use_assoc
1010 || sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
))
1013 = get_identifier (gfc_get_string (GFC_PREFIX ("caf_token%s"),
1014 IDENTIFIER_POINTER (gfc_sym_mangled_identifier (sym
))));
1015 token
= build_decl (DECL_SOURCE_LOCATION (decl
), VAR_DECL
, token_name
,
1017 if (sym
->attr
.use_assoc
)
1018 DECL_EXTERNAL (token
) = 1;
1020 TREE_STATIC (token
) = 1;
1022 TREE_PUBLIC (token
) = 1;
1024 if (sym
->attr
.access
== ACCESS_PRIVATE
&& !sym
->attr
.public_used
)
1026 DECL_VISIBILITY (token
) = VISIBILITY_HIDDEN
;
1027 DECL_VISIBILITY_SPECIFIED (token
) = true;
1032 token
= gfc_create_var_np (token_type
, "caf_token");
1033 TREE_STATIC (token
) = 1;
1036 GFC_TYPE_ARRAY_CAF_TOKEN (type
) = token
;
1037 DECL_ARTIFICIAL (token
) = 1;
1038 DECL_NONALIASED (token
) = 1;
1040 if (sym
->module
&& !sym
->attr
.use_assoc
)
1043 DECL_CONTEXT (token
) = sym
->ns
->proc_name
->backend_decl
;
1044 gfc_module_add_decl (cur_module
, token
);
1046 else if (sym
->attr
.host_assoc
1047 && TREE_CODE (DECL_CONTEXT (current_function_decl
))
1048 != TRANSLATION_UNIT_DECL
)
1049 gfc_add_decl_to_parent_function (token
);
1051 gfc_add_decl_to_function (token
);
1054 for (dim
= 0; dim
< GFC_TYPE_ARRAY_RANK (type
); dim
++)
1056 if (GFC_TYPE_ARRAY_LBOUND (type
, dim
) == NULL_TREE
)
1058 GFC_TYPE_ARRAY_LBOUND (type
, dim
) = create_index_var ("lbound", nest
);
1059 suppress_warning (GFC_TYPE_ARRAY_LBOUND (type
, dim
));
1061 /* Don't try to use the unknown bound for assumed shape arrays. */
1062 if (GFC_TYPE_ARRAY_UBOUND (type
, dim
) == NULL_TREE
1063 && (as
->type
!= AS_ASSUMED_SIZE
1064 || dim
< GFC_TYPE_ARRAY_RANK (type
) - 1))
1066 GFC_TYPE_ARRAY_UBOUND (type
, dim
) = create_index_var ("ubound", nest
);
1067 suppress_warning (GFC_TYPE_ARRAY_UBOUND (type
, dim
));
1070 if (GFC_TYPE_ARRAY_STRIDE (type
, dim
) == NULL_TREE
)
1072 GFC_TYPE_ARRAY_STRIDE (type
, dim
) = create_index_var ("stride", nest
);
1073 suppress_warning (GFC_TYPE_ARRAY_STRIDE (type
, dim
));
1076 for (dim
= GFC_TYPE_ARRAY_RANK (type
);
1077 dim
< GFC_TYPE_ARRAY_RANK (type
) + GFC_TYPE_ARRAY_CORANK (type
); dim
++)
1079 if (GFC_TYPE_ARRAY_LBOUND (type
, dim
) == NULL_TREE
)
1081 GFC_TYPE_ARRAY_LBOUND (type
, dim
) = create_index_var ("lbound", nest
);
1082 suppress_warning (GFC_TYPE_ARRAY_LBOUND (type
, dim
));
1084 /* Don't try to use the unknown ubound for the last coarray dimension. */
1085 if (GFC_TYPE_ARRAY_UBOUND (type
, dim
) == NULL_TREE
1086 && dim
< GFC_TYPE_ARRAY_RANK (type
) + GFC_TYPE_ARRAY_CORANK (type
) - 1)
1088 GFC_TYPE_ARRAY_UBOUND (type
, dim
) = create_index_var ("ubound", nest
);
1089 suppress_warning (GFC_TYPE_ARRAY_UBOUND (type
, dim
));
1092 if (GFC_TYPE_ARRAY_OFFSET (type
) == NULL_TREE
)
1094 GFC_TYPE_ARRAY_OFFSET (type
) = gfc_create_var_np (gfc_array_index_type
,
1096 suppress_warning (GFC_TYPE_ARRAY_OFFSET (type
));
1099 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type
));
1101 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type
));
1104 if (GFC_TYPE_ARRAY_SIZE (type
) == NULL_TREE
1105 && as
->type
!= AS_ASSUMED_SIZE
)
1107 GFC_TYPE_ARRAY_SIZE (type
) = create_index_var ("size", nest
);
1108 suppress_warning (GFC_TYPE_ARRAY_SIZE (type
));
1111 if (POINTER_TYPE_P (type
))
1113 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type
)));
1114 gcc_assert (TYPE_LANG_SPECIFIC (type
)
1115 == TYPE_LANG_SPECIFIC (TREE_TYPE (type
)));
1116 type
= TREE_TYPE (type
);
1119 if (! COMPLETE_TYPE_P (type
) && GFC_TYPE_ARRAY_SIZE (type
))
1123 size
= fold_build2_loc (input_location
, MINUS_EXPR
, gfc_array_index_type
,
1124 GFC_TYPE_ARRAY_SIZE (type
), gfc_index_one_node
);
1125 range
= build_range_type (gfc_array_index_type
, gfc_index_zero_node
,
1127 TYPE_DOMAIN (type
) = range
;
1131 if (TYPE_NAME (type
) != NULL_TREE
&& as
->rank
> 0
1132 && GFC_TYPE_ARRAY_UBOUND (type
, as
->rank
- 1) != NULL_TREE
1133 && VAR_P (GFC_TYPE_ARRAY_UBOUND (type
, as
->rank
- 1)))
1135 tree gtype
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
1137 for (dim
= 0; dim
< as
->rank
- 1; dim
++)
1139 gcc_assert (TREE_CODE (gtype
) == ARRAY_TYPE
);
1140 gtype
= TREE_TYPE (gtype
);
1142 gcc_assert (TREE_CODE (gtype
) == ARRAY_TYPE
);
1143 if (TYPE_MAX_VALUE (TYPE_DOMAIN (gtype
)) == NULL
)
1144 TYPE_NAME (type
) = NULL_TREE
;
1147 if (TYPE_NAME (type
) == NULL_TREE
)
1149 tree gtype
= TREE_TYPE (type
), rtype
, type_decl
;
1151 for (dim
= as
->rank
- 1; dim
>= 0; dim
--)
1153 tree lbound
, ubound
;
1154 lbound
= GFC_TYPE_ARRAY_LBOUND (type
, dim
);
1155 ubound
= GFC_TYPE_ARRAY_UBOUND (type
, dim
);
1156 rtype
= build_range_type (gfc_array_index_type
, lbound
, ubound
);
1157 gtype
= build_array_type (gtype
, rtype
);
1158 /* Ensure the bound variables aren't optimized out at -O0.
1159 For -O1 and above they often will be optimized out, but
1160 can be tracked by VTA. Also set DECL_NAMELESS, so that
1161 the artificial lbound.N or ubound.N DECL_NAME doesn't
1162 end up in debug info. */
1165 && DECL_ARTIFICIAL (lbound
)
1166 && DECL_IGNORED_P (lbound
))
1168 if (DECL_NAME (lbound
)
1169 && strstr (IDENTIFIER_POINTER (DECL_NAME (lbound
)),
1171 DECL_NAMELESS (lbound
) = 1;
1172 DECL_IGNORED_P (lbound
) = 0;
1176 && DECL_ARTIFICIAL (ubound
)
1177 && DECL_IGNORED_P (ubound
))
1179 if (DECL_NAME (ubound
)
1180 && strstr (IDENTIFIER_POINTER (DECL_NAME (ubound
)),
1182 DECL_NAMELESS (ubound
) = 1;
1183 DECL_IGNORED_P (ubound
) = 0;
1186 TYPE_NAME (type
) = type_decl
= build_decl (input_location
,
1187 TYPE_DECL
, NULL
, gtype
);
1188 DECL_ORIGINAL_TYPE (type_decl
) = gtype
;
1193 /* For some dummy arguments we don't use the actual argument directly.
1194 Instead we create a local decl and use that. This allows us to perform
1195 initialization, and construct full type information. */
1198 gfc_build_dummy_array_decl (gfc_symbol
* sym
, tree dummy
)
1203 symbol_attribute
*array_attr
;
1208 bool is_classarray
= IS_CLASS_ARRAY (sym
);
1210 /* Use the array as and attr. */
1211 as
= is_classarray
? CLASS_DATA (sym
)->as
: sym
->as
;
1212 array_attr
= is_classarray
? &CLASS_DATA (sym
)->attr
: &sym
->attr
;
1214 /* The dummy is returned for pointer, allocatable or assumed rank arrays.
1215 For class arrays the information if sym is an allocatable or pointer
1216 object needs to be checked explicitly (IS_CLASS_ARRAY can be false for
1217 too many reasons to be of use here). */
1218 if ((sym
->ts
.type
!= BT_CLASS
&& sym
->attr
.pointer
)
1219 || (sym
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym
)->attr
.class_pointer
)
1220 || array_attr
->allocatable
1221 || (as
&& as
->type
== AS_ASSUMED_RANK
))
1224 /* Add to list of variables if not a fake result variable.
1225 These symbols are set on the symbol only, not on the class component. */
1226 if (sym
->attr
.result
|| sym
->attr
.dummy
)
1227 gfc_defer_symbol_init (sym
);
1229 /* For a class array the array descriptor is in the _data component, while
1230 for a regular array the TREE_TYPE of the dummy is a pointer to the
1232 type
= TREE_TYPE (is_classarray
? gfc_class_data_get (dummy
)
1233 : TREE_TYPE (dummy
));
1234 /* type now is the array descriptor w/o any indirection. */
1235 gcc_assert (TREE_CODE (dummy
) == PARM_DECL
1236 && POINTER_TYPE_P (TREE_TYPE (dummy
)));
1238 /* Do we know the element size? */
1239 known_size
= sym
->ts
.type
!= BT_CHARACTER
1240 || INTEGER_CST_P (sym
->ts
.u
.cl
->backend_decl
);
1242 if (known_size
&& !GFC_DESCRIPTOR_TYPE_P (type
))
1244 /* For descriptorless arrays with known element size the actual
1245 argument is sufficient. */
1246 gfc_build_qualified_array (dummy
, sym
);
1250 if (GFC_DESCRIPTOR_TYPE_P (type
))
1252 /* Create a descriptorless array pointer. */
1255 /* Even when -frepack-arrays is used, symbols with TARGET attribute
1256 are not repacked. */
1257 if (!flag_repack_arrays
|| sym
->attr
.target
)
1259 if (as
->type
== AS_ASSUMED_SIZE
)
1260 packed
= PACKED_FULL
;
1264 if (as
->type
== AS_EXPLICIT
)
1266 packed
= PACKED_FULL
;
1267 for (n
= 0; n
< as
->rank
; n
++)
1271 && as
->upper
[n
]->expr_type
== EXPR_CONSTANT
1272 && as
->lower
[n
]->expr_type
== EXPR_CONSTANT
))
1274 packed
= PACKED_PARTIAL
;
1280 packed
= PACKED_PARTIAL
;
1283 /* For classarrays the element type is required, but
1284 gfc_typenode_for_spec () returns the array descriptor. */
1285 type
= is_classarray
? gfc_get_element_type (type
)
1286 : gfc_typenode_for_spec (&sym
->ts
);
1287 type
= gfc_get_nodesc_array_type (type
, as
, packed
,
1292 /* We now have an expression for the element size, so create a fully
1293 qualified type. Reset sym->backend decl or this will just return the
1295 DECL_ARTIFICIAL (sym
->backend_decl
) = 1;
1296 sym
->backend_decl
= NULL_TREE
;
1297 type
= gfc_sym_type (sym
);
1298 packed
= PACKED_FULL
;
1301 ASM_FORMAT_PRIVATE_NAME (name
, IDENTIFIER_POINTER (DECL_NAME (dummy
)), 0);
1302 decl
= build_decl (input_location
,
1303 VAR_DECL
, get_identifier (name
), type
);
1305 DECL_ARTIFICIAL (decl
) = 1;
1306 DECL_NAMELESS (decl
) = 1;
1307 TREE_PUBLIC (decl
) = 0;
1308 TREE_STATIC (decl
) = 0;
1309 DECL_EXTERNAL (decl
) = 0;
1311 /* Avoid uninitialized warnings for optional dummy arguments. */
1312 if ((sym
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym
)->attr
.optional
)
1313 || sym
->attr
.optional
)
1314 suppress_warning (decl
);
1316 /* We should never get deferred shape arrays here. We used to because of
1318 gcc_assert (as
->type
!= AS_DEFERRED
);
1320 if (packed
== PACKED_PARTIAL
)
1321 GFC_DECL_PARTIAL_PACKED_ARRAY (decl
) = 1;
1322 else if (packed
== PACKED_FULL
)
1323 GFC_DECL_PACKED_ARRAY (decl
) = 1;
1325 gfc_build_qualified_array (decl
, sym
);
1327 if (DECL_LANG_SPECIFIC (dummy
))
1328 DECL_LANG_SPECIFIC (decl
) = DECL_LANG_SPECIFIC (dummy
);
1330 gfc_allocate_lang_decl (decl
);
1332 GFC_DECL_SAVED_DESCRIPTOR (decl
) = dummy
;
1334 if (sym
->ns
->proc_name
->backend_decl
== current_function_decl
1335 || sym
->attr
.contained
)
1336 gfc_add_decl_to_function (decl
);
1338 gfc_add_decl_to_parent_function (decl
);
1343 /* Return a constant or a variable to use as a string length. Does not
1344 add the decl to the current scope. */
1347 gfc_create_string_length (gfc_symbol
* sym
)
1349 gcc_assert (sym
->ts
.u
.cl
);
1350 gfc_conv_const_charlen (sym
->ts
.u
.cl
);
1352 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
1357 /* The string length variable shall be in static memory if it is either
1358 explicitly SAVED, a module variable or with -fno-automatic. Only
1359 relevant is "len=:" - otherwise, it is either a constant length or
1360 it is an automatic variable. */
1361 bool static_length
= sym
->attr
.save
1362 || sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
1363 || (flag_max_stack_var_size
== 0
1364 && sym
->ts
.deferred
&& !sym
->attr
.dummy
1365 && !sym
->attr
.result
&& !sym
->attr
.function
);
1367 /* Also prefix the mangled name. We need to call GFC_PREFIX for static
1368 variables as some systems do not support the "." in the assembler name.
1369 For nonstatic variables, the "." does not appear in assembler. */
1373 name
= gfc_get_string (GFC_PREFIX ("%s_MOD_%s"), sym
->module
,
1376 name
= gfc_get_string (GFC_PREFIX ("%s"), sym
->name
);
1378 else if (sym
->module
)
1379 name
= gfc_get_string (".__%s_MOD_%s", sym
->module
, sym
->name
);
1381 name
= gfc_get_string (".%s", sym
->name
);
1383 length
= build_decl (input_location
,
1384 VAR_DECL
, get_identifier (name
),
1385 gfc_charlen_type_node
);
1386 DECL_ARTIFICIAL (length
) = 1;
1387 TREE_USED (length
) = 1;
1388 if (sym
->ns
->proc_name
->tlink
!= NULL
)
1389 gfc_defer_symbol_init (sym
);
1391 sym
->ts
.u
.cl
->backend_decl
= length
;
1394 TREE_STATIC (length
) = 1;
1396 if (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
1397 && (sym
->attr
.access
!= ACCESS_PRIVATE
|| sym
->attr
.public_used
))
1398 TREE_PUBLIC (length
) = 1;
1401 gcc_assert (sym
->ts
.u
.cl
->backend_decl
!= NULL_TREE
);
1402 return sym
->ts
.u
.cl
->backend_decl
;
1405 /* If a variable is assigned a label, we add another two auxiliary
1409 gfc_add_assign_aux_vars (gfc_symbol
* sym
)
1415 gcc_assert (sym
->backend_decl
);
1417 decl
= sym
->backend_decl
;
1418 gfc_allocate_lang_decl (decl
);
1419 GFC_DECL_ASSIGN (decl
) = 1;
1420 length
= build_decl (input_location
,
1421 VAR_DECL
, create_tmp_var_name (sym
->name
),
1422 gfc_charlen_type_node
);
1423 addr
= build_decl (input_location
,
1424 VAR_DECL
, create_tmp_var_name (sym
->name
),
1426 gfc_finish_var_decl (length
, sym
);
1427 gfc_finish_var_decl (addr
, sym
);
1428 /* STRING_LENGTH is also used as flag. Less than -1 means that
1429 ASSIGN_ADDR cannot be used. Equal -1 means that ASSIGN_ADDR is the
1430 target label's address. Otherwise, value is the length of a format string
1431 and ASSIGN_ADDR is its address. */
1432 if (TREE_STATIC (length
))
1433 DECL_INITIAL (length
) = build_int_cst (gfc_charlen_type_node
, -2);
1435 gfc_defer_symbol_init (sym
);
1437 GFC_DECL_STRING_LEN (decl
) = length
;
1438 GFC_DECL_ASSIGN_ADDR (decl
) = addr
;
1443 add_attributes_to_decl (symbol_attribute sym_attr
, tree list
)
1448 for (id
= 0; id
< EXT_ATTR_NUM
; id
++)
1449 if (sym_attr
.ext_attr
& (1 << id
) && ext_attr_list
[id
].middle_end_name
)
1451 attr
= build_tree_list (
1452 get_identifier (ext_attr_list
[id
].middle_end_name
),
1454 list
= chainon (list
, attr
);
1457 tree clauses
= NULL_TREE
;
1459 if (sym_attr
.oacc_routine_lop
!= OACC_ROUTINE_LOP_NONE
)
1461 omp_clause_code code
;
1462 switch (sym_attr
.oacc_routine_lop
)
1464 case OACC_ROUTINE_LOP_GANG
:
1465 code
= OMP_CLAUSE_GANG
;
1467 case OACC_ROUTINE_LOP_WORKER
:
1468 code
= OMP_CLAUSE_WORKER
;
1470 case OACC_ROUTINE_LOP_VECTOR
:
1471 code
= OMP_CLAUSE_VECTOR
;
1473 case OACC_ROUTINE_LOP_SEQ
:
1474 code
= OMP_CLAUSE_SEQ
;
1476 case OACC_ROUTINE_LOP_NONE
:
1477 case OACC_ROUTINE_LOP_ERROR
:
1481 tree c
= build_omp_clause (UNKNOWN_LOCATION
, code
);
1482 OMP_CLAUSE_CHAIN (c
) = clauses
;
1485 tree dims
= oacc_build_routine_dims (clauses
);
1486 list
= oacc_replace_fn_attrib_attr (list
, dims
);
1489 if (sym_attr
.oacc_routine_nohost
)
1491 tree c
= build_omp_clause (UNKNOWN_LOCATION
, OMP_CLAUSE_NOHOST
);
1492 OMP_CLAUSE_CHAIN (c
) = clauses
;
1496 if (sym_attr
.omp_device_type
!= OMP_DEVICE_TYPE_UNSET
)
1498 tree c
= build_omp_clause (UNKNOWN_LOCATION
, OMP_CLAUSE_DEVICE_TYPE
);
1499 switch (sym_attr
.omp_device_type
)
1501 case OMP_DEVICE_TYPE_HOST
:
1502 OMP_CLAUSE_DEVICE_TYPE_KIND (c
) = OMP_CLAUSE_DEVICE_TYPE_HOST
;
1504 case OMP_DEVICE_TYPE_NOHOST
:
1505 OMP_CLAUSE_DEVICE_TYPE_KIND (c
) = OMP_CLAUSE_DEVICE_TYPE_NOHOST
;
1507 case OMP_DEVICE_TYPE_ANY
:
1508 OMP_CLAUSE_DEVICE_TYPE_KIND (c
) = OMP_CLAUSE_DEVICE_TYPE_ANY
;
1513 OMP_CLAUSE_CHAIN (c
) = clauses
;
1517 if (sym_attr
.omp_declare_target_link
1518 || sym_attr
.oacc_declare_link
)
1519 list
= tree_cons (get_identifier ("omp declare target link"),
1521 else if (sym_attr
.omp_declare_target
1522 || sym_attr
.oacc_declare_create
1523 || sym_attr
.oacc_declare_copyin
1524 || sym_attr
.oacc_declare_deviceptr
1525 || sym_attr
.oacc_declare_device_resident
)
1526 list
= tree_cons (get_identifier ("omp declare target"),
1533 static void build_function_decl (gfc_symbol
* sym
, bool global
);
1536 /* Return the decl for a gfc_symbol, create it if it doesn't already
1540 gfc_get_symbol_decl (gfc_symbol
* sym
)
1543 tree length
= NULL_TREE
;
1546 bool intrinsic_array_parameter
= false;
1549 gcc_assert (sym
->attr
.referenced
1550 || sym
->attr
.flavor
== FL_PROCEDURE
1551 || sym
->attr
.use_assoc
1552 || sym
->attr
.used_in_submodule
1553 || sym
->ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
1554 || (sym
->module
&& sym
->attr
.if_source
!= IFSRC_DECL
1555 && sym
->backend_decl
));
1557 if (sym
->attr
.dummy
&& sym
->ns
->proc_name
->attr
.is_bind_c
1558 && is_CFI_desc (sym
, NULL
))
1560 gcc_assert (sym
->backend_decl
&& (sym
->ts
.type
!= BT_CHARACTER
1561 || sym
->ts
.u
.cl
->backend_decl
));
1562 return sym
->backend_decl
;
1565 if (sym
->ns
&& sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.function
)
1566 byref
= gfc_return_by_reference (sym
->ns
->proc_name
);
1570 /* Make sure that the vtab for the declared type is completed. */
1571 if (sym
->ts
.type
== BT_CLASS
)
1573 gfc_component
*c
= CLASS_DATA (sym
);
1574 if (!c
->ts
.u
.derived
->backend_decl
)
1576 gfc_find_derived_vtab (c
->ts
.u
.derived
);
1577 gfc_get_derived_type (sym
->ts
.u
.derived
);
1581 /* PDT parameterized array components and string_lengths must have the
1582 'len' parameters substituted for the expressions appearing in the
1583 declaration of the entity and memory allocated/deallocated. */
1584 if ((sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
1585 && sym
->param_list
!= NULL
1586 && gfc_current_ns
== sym
->ns
1587 && !(sym
->attr
.use_assoc
|| sym
->attr
.dummy
))
1588 gfc_defer_symbol_init (sym
);
1590 /* Dummy PDT 'len' parameters should be checked when they are explicit. */
1591 if ((sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
1592 && (gfc_option
.rtcheck
& GFC_RTCHECK_BOUNDS
)
1593 && sym
->param_list
!= NULL
1595 gfc_defer_symbol_init (sym
);
1597 /* All deferred character length procedures need to retain the backend
1598 decl, which is a pointer to the character length in the caller's
1599 namespace and to declare a local character length. */
1600 if (!byref
&& sym
->attr
.function
1601 && sym
->ts
.type
== BT_CHARACTER
1603 && sym
->ts
.u
.cl
->passed_length
== NULL
1604 && sym
->ts
.u
.cl
->backend_decl
1605 && TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
1607 sym
->ts
.u
.cl
->passed_length
= sym
->ts
.u
.cl
->backend_decl
;
1608 gcc_assert (POINTER_TYPE_P (TREE_TYPE (sym
->ts
.u
.cl
->passed_length
)));
1609 sym
->ts
.u
.cl
->backend_decl
= build_fold_indirect_ref (sym
->ts
.u
.cl
->backend_decl
);
1612 fun_or_res
= byref
&& (sym
->attr
.result
1613 || (sym
->attr
.function
&& sym
->ts
.deferred
));
1614 if ((sym
->attr
.dummy
&& ! sym
->attr
.function
) || fun_or_res
)
1616 /* Return via extra parameter. */
1617 if (sym
->attr
.result
&& byref
1618 && !sym
->backend_decl
)
1621 DECL_ARGUMENTS (sym
->ns
->proc_name
->backend_decl
);
1622 /* For entry master function skip over the __entry
1624 if (sym
->ns
->proc_name
->attr
.entry_master
)
1625 sym
->backend_decl
= DECL_CHAIN (sym
->backend_decl
);
1628 /* Dummy variables should already have been created. */
1629 gcc_assert (sym
->backend_decl
);
1631 /* However, the string length of deferred arrays must be set. */
1632 if (sym
->ts
.type
== BT_CHARACTER
1634 && sym
->attr
.dimension
1635 && sym
->attr
.allocatable
)
1636 gfc_defer_symbol_init (sym
);
1638 if (sym
->attr
.pointer
&& sym
->attr
.dimension
&& sym
->ts
.type
!= BT_CLASS
)
1639 GFC_DECL_PTR_ARRAY_P (sym
->backend_decl
) = 1;
1641 /* Create a character length variable. */
1642 if (sym
->ts
.type
== BT_CHARACTER
)
1644 /* For a deferred dummy, make a new string length variable. */
1645 if (sym
->ts
.deferred
1647 (sym
->ts
.u
.cl
->passed_length
== sym
->ts
.u
.cl
->backend_decl
))
1648 sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
1650 if (sym
->ts
.deferred
&& byref
)
1652 /* The string length of a deferred char array is stored in the
1653 parameter at sym->ts.u.cl->backend_decl as a reference and
1654 marked as a result. Exempt this variable from generating a
1655 temporary for it. */
1656 if (sym
->attr
.result
)
1658 /* We need to insert a indirect ref for param decls. */
1659 if (sym
->ts
.u
.cl
->backend_decl
1660 && TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
1662 sym
->ts
.u
.cl
->passed_length
= sym
->ts
.u
.cl
->backend_decl
;
1663 sym
->ts
.u
.cl
->backend_decl
=
1664 build_fold_indirect_ref (sym
->ts
.u
.cl
->backend_decl
);
1667 /* For all other parameters make sure, that they are copied so
1668 that the value and any modifications are local to the routine
1669 by generating a temporary variable. */
1670 else if (sym
->attr
.function
1671 && sym
->ts
.u
.cl
->passed_length
== NULL
1672 && sym
->ts
.u
.cl
->backend_decl
)
1674 sym
->ts
.u
.cl
->passed_length
= sym
->ts
.u
.cl
->backend_decl
;
1675 if (POINTER_TYPE_P (TREE_TYPE (sym
->ts
.u
.cl
->passed_length
)))
1676 sym
->ts
.u
.cl
->backend_decl
1677 = build_fold_indirect_ref (sym
->ts
.u
.cl
->backend_decl
);
1679 sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
1683 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
1684 length
= gfc_create_string_length (sym
);
1686 length
= sym
->ts
.u
.cl
->backend_decl
;
1687 if (VAR_P (length
) && DECL_FILE_SCOPE_P (length
))
1689 /* Add the string length to the same context as the symbol. */
1690 if (DECL_CONTEXT (length
) == NULL_TREE
)
1692 if (sym
->backend_decl
== current_function_decl
1693 || (DECL_CONTEXT (sym
->backend_decl
)
1694 == current_function_decl
))
1695 gfc_add_decl_to_function (length
);
1697 gfc_add_decl_to_parent_function (length
);
1700 gcc_assert (sym
->backend_decl
== current_function_decl
1701 ? DECL_CONTEXT (length
) == current_function_decl
1702 : (DECL_CONTEXT (sym
->backend_decl
)
1703 == DECL_CONTEXT (length
)));
1705 gfc_defer_symbol_init (sym
);
1709 /* Use a copy of the descriptor for dummy arrays. */
1710 if ((sym
->attr
.dimension
|| sym
->attr
.codimension
)
1711 && !TREE_USED (sym
->backend_decl
))
1713 decl
= gfc_build_dummy_array_decl (sym
, sym
->backend_decl
);
1714 /* Prevent the dummy from being detected as unused if it is copied. */
1715 if (sym
->backend_decl
!= NULL
&& decl
!= sym
->backend_decl
)
1716 DECL_ARTIFICIAL (sym
->backend_decl
) = 1;
1717 sym
->backend_decl
= decl
;
1720 /* Returning the descriptor for dummy class arrays is hazardous, because
1721 some caller is expecting an expression to apply the component refs to.
1722 Therefore the descriptor is only created and stored in
1723 sym->backend_decl's GFC_DECL_SAVED_DESCRIPTOR. The caller is then
1724 responsible to extract it from there, when the descriptor is
1726 if (IS_CLASS_ARRAY (sym
)
1727 && (!DECL_LANG_SPECIFIC (sym
->backend_decl
)
1728 || !GFC_DECL_SAVED_DESCRIPTOR (sym
->backend_decl
)))
1730 decl
= gfc_build_dummy_array_decl (sym
, sym
->backend_decl
);
1731 /* Prevent the dummy from being detected as unused if it is copied. */
1732 if (sym
->backend_decl
!= NULL
&& decl
!= sym
->backend_decl
)
1733 DECL_ARTIFICIAL (sym
->backend_decl
) = 1;
1734 sym
->backend_decl
= decl
;
1737 TREE_USED (sym
->backend_decl
) = 1;
1738 if (sym
->attr
.assign
&& GFC_DECL_ASSIGN (sym
->backend_decl
) == 0)
1739 gfc_add_assign_aux_vars (sym
);
1741 if (sym
->ts
.type
== BT_CLASS
&& sym
->backend_decl
)
1742 GFC_DECL_CLASS(sym
->backend_decl
) = 1;
1744 return sym
->backend_decl
;
1747 if (sym
->result
== sym
&& sym
->attr
.assign
1748 && GFC_DECL_ASSIGN (sym
->backend_decl
) == 0)
1749 gfc_add_assign_aux_vars (sym
);
1751 if (sym
->backend_decl
)
1752 return sym
->backend_decl
;
1754 /* Special case for array-valued named constants from intrinsic
1755 procedures; those are inlined. */
1756 if (sym
->attr
.use_assoc
&& sym
->attr
.flavor
== FL_PARAMETER
1757 && (sym
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
1758 || sym
->from_intmod
== INTMOD_ISO_C_BINDING
))
1759 intrinsic_array_parameter
= true;
1761 /* If use associated compilation, use the module
1763 if ((sym
->attr
.flavor
== FL_VARIABLE
1764 || sym
->attr
.flavor
== FL_PARAMETER
)
1765 && (sym
->attr
.use_assoc
|| sym
->attr
.used_in_submodule
)
1766 && !intrinsic_array_parameter
1768 && gfc_get_module_backend_decl (sym
))
1770 if (sym
->ts
.type
== BT_CLASS
&& sym
->backend_decl
)
1771 GFC_DECL_CLASS(sym
->backend_decl
) = 1;
1772 return sym
->backend_decl
;
1775 if (sym
->attr
.flavor
== FL_PROCEDURE
)
1777 /* Catch functions. Only used for actual parameters,
1778 procedure pointers and procptr initialization targets. */
1779 if (sym
->attr
.use_assoc
1780 || sym
->attr
.used_in_submodule
1781 || sym
->attr
.intrinsic
1782 || sym
->attr
.if_source
!= IFSRC_DECL
)
1784 decl
= gfc_get_extern_function_decl (sym
);
1788 if (!sym
->backend_decl
)
1789 build_function_decl (sym
, false);
1790 decl
= sym
->backend_decl
;
1795 if (sym
->ts
.type
== BT_UNKNOWN
)
1796 gfc_fatal_error ("%s at %C has no default type", sym
->name
);
1798 if (sym
->attr
.intrinsic
)
1799 gfc_internal_error ("intrinsic variable which isn't a procedure");
1801 /* Create string length decl first so that they can be used in the
1802 type declaration. For associate names, the target character
1803 length is used. Set 'length' to a constant so that if the
1804 string length is a variable, it is not finished a second time. */
1805 if (sym
->ts
.type
== BT_CHARACTER
)
1807 if (sym
->attr
.associate_var
1809 && sym
->assoc
&& sym
->assoc
->target
1810 && ((sym
->assoc
->target
->expr_type
== EXPR_VARIABLE
1811 && sym
->assoc
->target
->symtree
->n
.sym
->ts
.type
!= BT_CHARACTER
)
1812 || sym
->assoc
->target
->expr_type
!= EXPR_VARIABLE
))
1813 sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
1815 if (sym
->attr
.associate_var
1816 && sym
->ts
.u
.cl
->backend_decl
1817 && (VAR_P (sym
->ts
.u
.cl
->backend_decl
)
1818 || TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
))
1819 length
= gfc_index_zero_node
;
1821 length
= gfc_create_string_length (sym
);
1824 /* Create the decl for the variable. */
1825 decl
= build_decl (gfc_get_location (&sym
->declared_at
),
1826 VAR_DECL
, gfc_sym_identifier (sym
), gfc_sym_type (sym
));
1828 /* Add attributes to variables. Functions are handled elsewhere. */
1829 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
1830 decl_attributes (&decl
, attributes
, 0);
1831 if (sym
->ts
.deferred
&& VAR_P (length
))
1832 decl_attributes (&length
, attributes
, 0);
1834 /* Symbols from modules should have their assembler names mangled.
1835 This is done here rather than in gfc_finish_var_decl because it
1836 is different for string length variables. */
1837 if (sym
->module
|| sym
->fn_result_spec
)
1839 gfc_set_decl_assembler_name (decl
, gfc_sym_mangled_identifier (sym
));
1840 if (sym
->attr
.use_assoc
&& !intrinsic_array_parameter
)
1841 DECL_IGNORED_P (decl
) = 1;
1844 if (sym
->attr
.select_type_temporary
)
1846 DECL_ARTIFICIAL (decl
) = 1;
1847 DECL_IGNORED_P (decl
) = 1;
1850 if (sym
->attr
.dimension
|| sym
->attr
.codimension
)
1852 /* Create variables to hold the non-constant bits of array info. */
1853 gfc_build_qualified_array (decl
, sym
);
1855 if (sym
->attr
.contiguous
1856 || ((sym
->attr
.allocatable
|| !sym
->attr
.dummy
) && !sym
->attr
.pointer
))
1857 GFC_DECL_PACKED_ARRAY (decl
) = 1;
1860 /* Remember this variable for allocation/cleanup. */
1861 if (sym
->attr
.dimension
|| sym
->attr
.allocatable
|| sym
->attr
.codimension
1862 || (sym
->ts
.type
== BT_CLASS
&&
1863 (CLASS_DATA (sym
)->attr
.dimension
1864 || CLASS_DATA (sym
)->attr
.allocatable
))
1865 || (sym
->ts
.type
== BT_DERIVED
1866 && (sym
->ts
.u
.derived
->attr
.alloc_comp
1867 || (!sym
->attr
.pointer
&& !sym
->attr
.artificial
&& !sym
->attr
.save
1868 && !sym
->ns
->proc_name
->attr
.is_main_program
1869 && gfc_is_finalizable (sym
->ts
.u
.derived
, NULL
))))
1870 /* This applies a derived type default initializer. */
1871 || (sym
->ts
.type
== BT_DERIVED
1872 && sym
->attr
.save
== SAVE_NONE
1874 && !sym
->attr
.allocatable
1875 && (sym
->value
&& !sym
->ns
->proc_name
->attr
.is_main_program
)
1876 && !(sym
->attr
.use_assoc
&& !intrinsic_array_parameter
)))
1877 gfc_defer_symbol_init (sym
);
1879 /* Set the vptr of unlimited polymorphic pointer variables so that
1880 they do not cause segfaults in select type, when the selector
1881 is an intrinsic type. Arrays are captured above. */
1882 if (sym
->ts
.type
== BT_CLASS
&& UNLIMITED_POLY (sym
)
1883 && CLASS_DATA (sym
)->attr
.class_pointer
1884 && !CLASS_DATA (sym
)->attr
.dimension
&& !sym
->attr
.dummy
1885 && sym
->attr
.flavor
== FL_VARIABLE
&& !sym
->assoc
)
1886 gfc_defer_symbol_init (sym
);
1888 if (sym
->ts
.type
== BT_CHARACTER
1889 && sym
->attr
.allocatable
1890 && !sym
->attr
.dimension
1891 && sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
1892 && sym
->ts
.u
.cl
->length
->expr_type
== EXPR_VARIABLE
)
1893 gfc_defer_symbol_init (sym
);
1895 /* Associate names can use the hidden string length variable
1896 of their associated target. */
1897 if (sym
->ts
.type
== BT_CHARACTER
1898 && TREE_CODE (length
) != INTEGER_CST
1899 && TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) != INDIRECT_REF
)
1901 length
= fold_convert (gfc_charlen_type_node
, length
);
1902 gfc_finish_var_decl (length
, sym
);
1903 if (!sym
->attr
.associate_var
1905 && sym
->value
&& sym
->value
->expr_type
!= EXPR_NULL
1906 && sym
->value
->ts
.u
.cl
->length
)
1908 gfc_expr
*len
= sym
->value
->ts
.u
.cl
->length
;
1909 DECL_INITIAL (length
) = gfc_conv_initializer (len
, &len
->ts
,
1911 false, false, false);
1912 DECL_INITIAL (length
) = fold_convert (gfc_charlen_type_node
,
1913 DECL_INITIAL (length
));
1916 gcc_assert (!sym
->value
|| sym
->value
->expr_type
== EXPR_NULL
);
1919 gfc_finish_var_decl (decl
, sym
);
1921 if (sym
->ts
.type
== BT_CHARACTER
)
1922 /* Character variables need special handling. */
1923 gfc_allocate_lang_decl (decl
);
1925 if (sym
->assoc
&& sym
->attr
.subref_array_pointer
)
1926 sym
->attr
.pointer
= 1;
1928 if (sym
->attr
.pointer
&& sym
->attr
.dimension
1929 && !sym
->ts
.deferred
1930 && !(sym
->attr
.select_type_temporary
1931 && !sym
->attr
.subref_array_pointer
))
1932 GFC_DECL_PTR_ARRAY_P (decl
) = 1;
1934 if (sym
->ts
.type
== BT_CLASS
)
1935 GFC_DECL_CLASS(decl
) = 1;
1937 sym
->backend_decl
= decl
;
1939 if (sym
->attr
.assign
)
1940 gfc_add_assign_aux_vars (sym
);
1942 if (intrinsic_array_parameter
)
1944 TREE_STATIC (decl
) = 1;
1945 DECL_EXTERNAL (decl
) = 0;
1948 if (TREE_STATIC (decl
)
1949 && !(sym
->attr
.use_assoc
&& !intrinsic_array_parameter
)
1950 && (sym
->attr
.save
|| sym
->ns
->proc_name
->attr
.is_main_program
1951 || !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl
))
1952 || sym
->attr
.data
|| sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
1953 && (flag_coarray
!= GFC_FCOARRAY_LIB
1954 || !sym
->attr
.codimension
|| sym
->attr
.allocatable
)
1955 && !(sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.pdt_type
)
1956 && !(sym
->ts
.type
== BT_CLASS
1957 && CLASS_DATA (sym
)->ts
.u
.derived
->attr
.pdt_type
))
1959 /* Add static initializer. For procedures, it is only needed if
1960 SAVE is specified otherwise they need to be reinitialized
1961 every time the procedure is entered. The TREE_STATIC is
1962 in this case due to -fmax-stack-var-size=. */
1964 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
1965 TREE_TYPE (decl
), sym
->attr
.dimension
1966 || (sym
->attr
.codimension
1967 && sym
->attr
.allocatable
),
1968 sym
->attr
.pointer
|| sym
->attr
.allocatable
1969 || sym
->ts
.type
== BT_CLASS
,
1970 sym
->attr
.proc_pointer
);
1973 if (!TREE_STATIC (decl
)
1974 && POINTER_TYPE_P (TREE_TYPE (decl
))
1975 && !sym
->attr
.pointer
1976 && !sym
->attr
.allocatable
1977 && !sym
->attr
.proc_pointer
1978 && !sym
->attr
.select_type_temporary
)
1979 DECL_BY_REFERENCE (decl
) = 1;
1981 if (sym
->attr
.associate_var
)
1982 GFC_DECL_ASSOCIATE_VAR_P (decl
) = 1;
1984 /* We only longer mark __def_init as read-only if it actually has an
1985 initializer, it does not needlessly take up space in the
1986 read-only section and can go into the BSS instead, see PR 84487.
1987 Marking this as artificial means that OpenMP will treat this as
1988 predetermined shared. */
1990 bool def_init
= startswith (sym
->name
, "__def_init");
1992 if (sym
->attr
.vtab
|| def_init
)
1994 DECL_ARTIFICIAL (decl
) = 1;
1995 if (def_init
&& sym
->value
)
1996 TREE_READONLY (decl
) = 1;
2003 /* Substitute a temporary variable in place of the real one. */
2006 gfc_shadow_sym (gfc_symbol
* sym
, tree decl
, gfc_saved_var
* save
)
2008 save
->attr
= sym
->attr
;
2009 save
->decl
= sym
->backend_decl
;
2011 gfc_clear_attr (&sym
->attr
);
2012 sym
->attr
.referenced
= 1;
2013 sym
->attr
.flavor
= FL_VARIABLE
;
2015 sym
->backend_decl
= decl
;
2019 /* Restore the original variable. */
2022 gfc_restore_sym (gfc_symbol
* sym
, gfc_saved_var
* save
)
2024 sym
->attr
= save
->attr
;
2025 sym
->backend_decl
= save
->decl
;
2029 /* Declare a procedure pointer. */
2032 get_proc_pointer_decl (gfc_symbol
*sym
)
2037 if (sym
->module
|| sym
->fn_result_spec
)
2042 name
= mangled_identifier (sym
);
2043 gsym
= gfc_find_gsymbol (gfc_gsym_root
, name
);
2047 gfc_find_symbol (sym
->name
, gsym
->ns
, 0, &s
);
2048 if (s
&& s
->backend_decl
)
2049 return s
->backend_decl
;
2053 decl
= sym
->backend_decl
;
2057 decl
= build_decl (input_location
,
2058 VAR_DECL
, get_identifier (sym
->name
),
2059 build_pointer_type (gfc_get_function_type (sym
)));
2063 /* Apply name mangling. */
2064 gfc_set_decl_assembler_name (decl
, gfc_sym_mangled_identifier (sym
));
2065 if (sym
->attr
.use_assoc
)
2066 DECL_IGNORED_P (decl
) = 1;
2069 if ((sym
->ns
->proc_name
2070 && sym
->ns
->proc_name
->backend_decl
== current_function_decl
)
2071 || sym
->attr
.contained
)
2072 gfc_add_decl_to_function (decl
);
2073 else if (sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
2074 gfc_add_decl_to_parent_function (decl
);
2076 sym
->backend_decl
= decl
;
2078 /* If a variable is USE associated, it's always external. */
2079 if (sym
->attr
.use_assoc
)
2081 DECL_EXTERNAL (decl
) = 1;
2082 TREE_PUBLIC (decl
) = 1;
2084 else if (sym
->module
&& sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
2086 /* This is the declaration of a module variable. */
2087 TREE_PUBLIC (decl
) = 1;
2088 if (sym
->attr
.access
== ACCESS_PRIVATE
&& !sym
->attr
.public_used
)
2090 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2091 DECL_VISIBILITY_SPECIFIED (decl
) = true;
2093 TREE_STATIC (decl
) = 1;
2096 if (!sym
->attr
.use_assoc
2097 && (sym
->attr
.save
!= SAVE_NONE
|| sym
->attr
.data
2098 || (sym
->value
&& sym
->ns
->proc_name
->attr
.is_main_program
)))
2099 TREE_STATIC (decl
) = 1;
2101 if (TREE_STATIC (decl
) && sym
->value
)
2103 /* Add static initializer. */
2104 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
2106 sym
->attr
.dimension
,
2110 /* Handle threadprivate procedure pointers. */
2111 if (sym
->attr
.threadprivate
2112 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
2113 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
2115 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
2116 decl_attributes (&decl
, attributes
, 0);
2122 /* Get a basic decl for an external function. */
2125 gfc_get_extern_function_decl (gfc_symbol
* sym
, gfc_actual_arglist
*actual_args
,
2132 gfc_intrinsic_sym
*isym
;
2134 char s
[GFC_MAX_SYMBOL_LEN
+ 23]; /* "_gfortran_f2c_specific" and '\0'. */
2139 if (sym
->backend_decl
)
2140 return sym
->backend_decl
;
2142 /* We should never be creating external decls for alternate entry points.
2143 The procedure may be an alternate entry point, but we don't want/need
2145 gcc_assert (!(sym
->attr
.entry
|| sym
->attr
.entry_master
));
2147 if (sym
->attr
.proc_pointer
)
2148 return get_proc_pointer_decl (sym
);
2150 /* See if this is an external procedure from the same file. If so,
2151 return the backend_decl. If we are looking at a BIND(C)
2152 procedure and the symbol is not BIND(C), or vice versa, we
2153 haven't found the right procedure. */
2155 if (sym
->binding_label
)
2157 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->binding_label
);
2158 if (gsym
&& !gsym
->bind_c
)
2161 else if (sym
->module
== NULL
)
2163 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->name
);
2164 if (gsym
&& gsym
->bind_c
)
2169 /* Procedure from a different module. */
2173 if (gsym
&& !gsym
->defined
)
2176 /* This can happen because of C binding. */
2177 if (gsym
&& gsym
->ns
&& gsym
->ns
->proc_name
2178 && gsym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
2181 if ((!sym
->attr
.use_assoc
|| sym
->attr
.if_source
!= IFSRC_DECL
)
2182 && !sym
->backend_decl
2184 && ((gsym
->type
== GSYM_SUBROUTINE
) || (gsym
->type
== GSYM_FUNCTION
))
2185 && (gsym
->ns
->proc_name
->backend_decl
|| !sym
->attr
.intrinsic
))
2187 if (!gsym
->ns
->proc_name
->backend_decl
)
2189 /* By construction, the external function cannot be
2190 a contained procedure. */
2193 gfc_save_backend_locus (&old_loc
);
2196 gfc_create_function_decl (gsym
->ns
, true);
2199 gfc_restore_backend_locus (&old_loc
);
2202 /* If the namespace has entries, the proc_name is the
2203 entry master. Find the entry and use its backend_decl.
2204 otherwise, use the proc_name backend_decl. */
2205 if (gsym
->ns
->entries
)
2207 gfc_entry_list
*entry
= gsym
->ns
->entries
;
2209 for (; entry
; entry
= entry
->next
)
2211 if (strcmp (gsym
->name
, entry
->sym
->name
) == 0)
2213 sym
->backend_decl
= entry
->sym
->backend_decl
;
2219 sym
->backend_decl
= gsym
->ns
->proc_name
->backend_decl
;
2221 if (sym
->backend_decl
)
2223 /* Avoid problems of double deallocation of the backend declaration
2224 later in gfc_trans_use_stmts; cf. PR 45087. */
2225 if (sym
->attr
.if_source
!= IFSRC_DECL
&& sym
->attr
.use_assoc
)
2226 sym
->attr
.use_assoc
= 0;
2228 return sym
->backend_decl
;
2232 /* See if this is a module procedure from the same file. If so,
2233 return the backend_decl. */
2235 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->module
);
2238 if (gsym
&& gsym
->ns
2239 && (gsym
->type
== GSYM_MODULE
2240 || (gsym
->ns
->proc_name
&& gsym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)))
2245 if (gsym
->type
== GSYM_MODULE
)
2246 gfc_find_symbol (sym
->name
, gsym
->ns
, 0, &s
);
2248 gfc_find_symbol (gsym
->sym_name
, gsym
->ns
, 0, &s
);
2250 if (s
&& s
->backend_decl
)
2252 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
2253 gfc_copy_dt_decls_ifequal (s
->ts
.u
.derived
, sym
->ts
.u
.derived
,
2255 else if (sym
->ts
.type
== BT_CHARACTER
)
2256 sym
->ts
.u
.cl
->backend_decl
= s
->ts
.u
.cl
->backend_decl
;
2257 sym
->backend_decl
= s
->backend_decl
;
2258 return sym
->backend_decl
;
2262 if (sym
->attr
.intrinsic
)
2264 /* Call the resolution function to get the actual name. This is
2265 a nasty hack which relies on the resolution functions only looking
2266 at the first argument. We pass NULL for the second argument
2267 otherwise things like AINT get confused. */
2268 isym
= gfc_find_function (sym
->name
);
2269 gcc_assert (isym
->resolve
.f0
!= NULL
);
2271 memset (&e
, 0, sizeof (e
));
2272 e
.expr_type
= EXPR_FUNCTION
;
2274 memset (&argexpr
, 0, sizeof (argexpr
));
2275 gcc_assert (isym
->formal
);
2276 argexpr
.ts
= isym
->formal
->ts
;
2278 if (isym
->formal
->next
== NULL
)
2279 isym
->resolve
.f1 (&e
, &argexpr
);
2282 if (isym
->formal
->next
->next
== NULL
)
2283 isym
->resolve
.f2 (&e
, &argexpr
, NULL
);
2286 if (isym
->formal
->next
->next
->next
== NULL
)
2287 isym
->resolve
.f3 (&e
, &argexpr
, NULL
, NULL
);
2290 /* All specific intrinsics take less than 5 arguments. */
2291 gcc_assert (isym
->formal
->next
->next
->next
->next
== NULL
);
2292 isym
->resolve
.f4 (&e
, &argexpr
, NULL
, NULL
, NULL
);
2298 && ((e
.ts
.type
== BT_REAL
&& e
.ts
.kind
== gfc_default_real_kind
)
2299 || e
.ts
.type
== BT_COMPLEX
))
2301 /* Specific which needs a different implementation if f2c
2302 calling conventions are used. */
2303 sprintf (s
, "_gfortran_f2c_specific%s", e
.value
.function
.name
);
2306 sprintf (s
, "_gfortran_specific%s", e
.value
.function
.name
);
2308 name
= get_identifier (s
);
2309 mangled_name
= name
;
2313 name
= gfc_sym_identifier (sym
);
2314 mangled_name
= gfc_sym_mangled_function_id (sym
);
2317 type
= gfc_get_function_type (sym
, actual_args
, fnspec
);
2319 fndecl
= build_decl (input_location
,
2320 FUNCTION_DECL
, name
, type
);
2322 /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
2323 TREE_PUBLIC specifies whether a function is globally addressable (i.e.
2324 the opposite of declaring a function as static in C). */
2325 DECL_EXTERNAL (fndecl
) = 1;
2326 TREE_PUBLIC (fndecl
) = 1;
2328 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
2329 decl_attributes (&fndecl
, attributes
, 0);
2331 gfc_set_decl_assembler_name (fndecl
, mangled_name
);
2333 /* Set the context of this decl. */
2334 if (0 && sym
->ns
&& sym
->ns
->proc_name
)
2336 /* TODO: Add external decls to the appropriate scope. */
2337 DECL_CONTEXT (fndecl
) = sym
->ns
->proc_name
->backend_decl
;
2341 /* Global declaration, e.g. intrinsic subroutine. */
2342 DECL_CONTEXT (fndecl
) = NULL_TREE
;
2345 /* Set attributes for PURE functions. A call to PURE function in the
2346 Fortran 95 sense is both pure and without side effects in the C
2348 if (sym
->attr
.pure
|| sym
->attr
.implicit_pure
)
2350 if (sym
->attr
.function
&& !gfc_return_by_reference (sym
))
2351 DECL_PURE_P (fndecl
) = 1;
2352 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
2353 parameters and don't use alternate returns (is this
2354 allowed?). In that case, calls to them are meaningless, and
2355 can be optimized away. See also in build_function_decl(). */
2356 TREE_SIDE_EFFECTS (fndecl
) = 0;
2359 /* Mark non-returning functions. */
2360 if (sym
->attr
.noreturn
|| sym
->attr
.ext_attr
& (1 << EXT_ATTR_NORETURN
))
2361 TREE_THIS_VOLATILE(fndecl
) = 1;
2363 sym
->backend_decl
= fndecl
;
2365 if (DECL_CONTEXT (fndecl
) == NULL_TREE
)
2366 pushdecl_top_level (fndecl
);
2369 && sym
->formal_ns
->proc_name
== sym
)
2371 if (sym
->formal_ns
->omp_declare_simd
)
2372 gfc_trans_omp_declare_simd (sym
->formal_ns
);
2374 gfc_trans_omp_declare_variant (sym
->formal_ns
);
2381 /* Create a declaration for a procedure. For external functions (in the C
2382 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
2383 a master function with alternate entry points. */
2386 build_function_decl (gfc_symbol
* sym
, bool global
)
2388 tree fndecl
, type
, attributes
;
2389 symbol_attribute attr
;
2391 gfc_formal_arglist
*f
;
2393 bool module_procedure
= sym
->attr
.module_procedure
2395 && sym
->ns
->proc_name
2396 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
;
2398 gcc_assert (!sym
->attr
.external
|| module_procedure
);
2400 if (sym
->backend_decl
)
2403 /* Set the line and filename. sym->declared_at seems to point to the
2404 last statement for subroutines, but it'll do for now. */
2405 gfc_set_backend_locus (&sym
->declared_at
);
2407 /* Allow only one nesting level. Allow public declarations. */
2408 gcc_assert (current_function_decl
== NULL_TREE
2409 || DECL_FILE_SCOPE_P (current_function_decl
)
2410 || (TREE_CODE (DECL_CONTEXT (current_function_decl
))
2411 == NAMESPACE_DECL
));
2413 type
= gfc_get_function_type (sym
);
2414 fndecl
= build_decl (input_location
,
2415 FUNCTION_DECL
, gfc_sym_identifier (sym
), type
);
2419 /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
2420 TREE_PUBLIC specifies whether a function is globally addressable (i.e.
2421 the opposite of declaring a function as static in C). */
2422 DECL_EXTERNAL (fndecl
) = 0;
2424 if (sym
->attr
.access
== ACCESS_UNKNOWN
&& sym
->module
2425 && (sym
->ns
->default_access
== ACCESS_PRIVATE
2426 || (sym
->ns
->default_access
== ACCESS_UNKNOWN
2427 && flag_module_private
)))
2428 sym
->attr
.access
= ACCESS_PRIVATE
;
2430 if (!current_function_decl
2431 && !sym
->attr
.entry_master
&& !sym
->attr
.is_main_program
2432 && (sym
->attr
.access
!= ACCESS_PRIVATE
|| sym
->binding_label
2433 || sym
->attr
.public_used
))
2434 TREE_PUBLIC (fndecl
) = 1;
2436 if (sym
->attr
.referenced
|| sym
->attr
.entry_master
)
2437 TREE_USED (fndecl
) = 1;
2439 attributes
= add_attributes_to_decl (attr
, NULL_TREE
);
2440 decl_attributes (&fndecl
, attributes
, 0);
2442 /* Figure out the return type of the declared function, and build a
2443 RESULT_DECL for it. If this is a subroutine with alternate
2444 returns, build a RESULT_DECL for it. */
2445 result_decl
= NULL_TREE
;
2446 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
2449 if (gfc_return_by_reference (sym
))
2450 type
= void_type_node
;
2453 if (sym
->result
!= sym
)
2454 result_decl
= gfc_sym_identifier (sym
->result
);
2456 type
= TREE_TYPE (TREE_TYPE (fndecl
));
2461 /* Look for alternate return placeholders. */
2462 int has_alternate_returns
= 0;
2463 for (f
= gfc_sym_get_dummy_args (sym
); f
; f
= f
->next
)
2467 has_alternate_returns
= 1;
2472 if (has_alternate_returns
)
2473 type
= integer_type_node
;
2475 type
= void_type_node
;
2478 result_decl
= build_decl (input_location
,
2479 RESULT_DECL
, result_decl
, type
);
2480 DECL_ARTIFICIAL (result_decl
) = 1;
2481 DECL_IGNORED_P (result_decl
) = 1;
2482 DECL_CONTEXT (result_decl
) = fndecl
;
2483 DECL_RESULT (fndecl
) = result_decl
;
2485 /* Don't call layout_decl for a RESULT_DECL.
2486 layout_decl (result_decl, 0); */
2488 /* TREE_STATIC means the function body is defined here. */
2489 TREE_STATIC (fndecl
) = 1;
2491 /* Set attributes for PURE functions. A call to a PURE function in the
2492 Fortran 95 sense is both pure and without side effects in the C
2494 if (attr
.pure
|| attr
.implicit_pure
)
2496 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
2497 including an alternate return. In that case it can also be
2498 marked as PURE. See also in gfc_get_extern_function_decl(). */
2499 if (attr
.function
&& !gfc_return_by_reference (sym
))
2500 DECL_PURE_P (fndecl
) = 1;
2501 TREE_SIDE_EFFECTS (fndecl
) = 0;
2504 /* Mark noinline functions. */
2505 if (attr
.ext_attr
& (1 << EXT_ATTR_NOINLINE
))
2506 DECL_UNINLINABLE (fndecl
) = 1;
2508 /* Mark noreturn functions. */
2509 if (attr
.ext_attr
& (1 << EXT_ATTR_NORETURN
))
2510 TREE_THIS_VOLATILE (fndecl
) = 1;
2512 /* Mark weak functions. */
2513 if (attr
.ext_attr
& (1 << EXT_ATTR_WEAK
))
2514 declare_weak (fndecl
);
2516 /* Layout the function declaration and put it in the binding level
2517 of the current function. */
2520 pushdecl_top_level (fndecl
);
2524 /* Perform name mangling if this is a top level or module procedure. */
2525 if (current_function_decl
== NULL_TREE
)
2526 gfc_set_decl_assembler_name (fndecl
, gfc_sym_mangled_function_id (sym
));
2528 sym
->backend_decl
= fndecl
;
2532 /* Create the DECL_ARGUMENTS for a procedure.
2533 NOTE: The arguments added here must match the argument type created by
2534 gfc_get_function_type (). */
2537 create_function_arglist (gfc_symbol
* sym
)
2540 gfc_formal_arglist
*f
;
2541 tree typelist
, hidden_typelist
, optval_typelist
;
2542 tree arglist
, hidden_arglist
, optval_arglist
;
2546 fndecl
= sym
->backend_decl
;
2548 /* Build formal argument list. Make sure that their TREE_CONTEXT is
2549 the new FUNCTION_DECL node. */
2550 arglist
= NULL_TREE
;
2551 hidden_arglist
= NULL_TREE
;
2552 optval_arglist
= NULL_TREE
;
2553 typelist
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
2555 if (sym
->attr
.entry_master
)
2557 type
= TREE_VALUE (typelist
);
2558 parm
= build_decl (input_location
,
2559 PARM_DECL
, get_identifier ("__entry"), type
);
2561 DECL_CONTEXT (parm
) = fndecl
;
2562 DECL_ARG_TYPE (parm
) = type
;
2563 TREE_READONLY (parm
) = 1;
2564 gfc_finish_decl (parm
);
2565 DECL_ARTIFICIAL (parm
) = 1;
2567 arglist
= chainon (arglist
, parm
);
2568 typelist
= TREE_CHAIN (typelist
);
2571 if (gfc_return_by_reference (sym
))
2573 tree type
= TREE_VALUE (typelist
), length
= NULL
;
2575 if (sym
->ts
.type
== BT_CHARACTER
)
2577 /* Length of character result. */
2578 tree len_type
= TREE_VALUE (TREE_CHAIN (typelist
));
2580 length
= build_decl (input_location
,
2582 get_identifier (".__result"),
2584 if (POINTER_TYPE_P (len_type
))
2586 sym
->ts
.u
.cl
->passed_length
= length
;
2587 TREE_USED (length
) = 1;
2589 else if (!sym
->ts
.u
.cl
->length
)
2591 sym
->ts
.u
.cl
->backend_decl
= length
;
2592 TREE_USED (length
) = 1;
2594 gcc_assert (TREE_CODE (length
) == PARM_DECL
);
2595 DECL_CONTEXT (length
) = fndecl
;
2596 DECL_ARG_TYPE (length
) = len_type
;
2597 TREE_READONLY (length
) = 1;
2598 DECL_ARTIFICIAL (length
) = 1;
2599 gfc_finish_decl (length
);
2600 if (sym
->ts
.u
.cl
->backend_decl
== NULL
2601 || sym
->ts
.u
.cl
->backend_decl
== length
)
2606 if (sym
->ts
.u
.cl
->backend_decl
== NULL
)
2608 tree len
= build_decl (input_location
,
2610 get_identifier ("..__result"),
2611 gfc_charlen_type_node
);
2612 DECL_ARTIFICIAL (len
) = 1;
2613 TREE_USED (len
) = 1;
2614 sym
->ts
.u
.cl
->backend_decl
= len
;
2617 /* Make sure PARM_DECL type doesn't point to incomplete type. */
2618 arg
= sym
->result
? sym
->result
: sym
;
2619 backend_decl
= arg
->backend_decl
;
2620 /* Temporary clear it, so that gfc_sym_type creates complete
2622 arg
->backend_decl
= NULL
;
2623 type
= gfc_sym_type (arg
);
2624 arg
->backend_decl
= backend_decl
;
2625 type
= build_reference_type (type
);
2629 parm
= build_decl (input_location
,
2630 PARM_DECL
, get_identifier ("__result"), type
);
2632 DECL_CONTEXT (parm
) = fndecl
;
2633 DECL_ARG_TYPE (parm
) = TREE_VALUE (typelist
);
2634 TREE_READONLY (parm
) = 1;
2635 DECL_ARTIFICIAL (parm
) = 1;
2636 gfc_finish_decl (parm
);
2638 arglist
= chainon (arglist
, parm
);
2639 typelist
= TREE_CHAIN (typelist
);
2641 if (sym
->ts
.type
== BT_CHARACTER
)
2643 gfc_allocate_lang_decl (parm
);
2644 arglist
= chainon (arglist
, length
);
2645 typelist
= TREE_CHAIN (typelist
);
2649 hidden_typelist
= typelist
;
2650 for (f
= gfc_sym_get_dummy_args (sym
); f
; f
= f
->next
)
2651 if (f
->sym
!= NULL
) /* Ignore alternate returns. */
2652 hidden_typelist
= TREE_CHAIN (hidden_typelist
);
2654 /* Advance hidden_typelist over optional+value argument presence flags. */
2655 optval_typelist
= hidden_typelist
;
2656 for (f
= gfc_sym_get_dummy_args (sym
); f
; f
= f
->next
)
2658 && f
->sym
->attr
.optional
&& f
->sym
->attr
.value
2659 && !f
->sym
->attr
.dimension
&& f
->sym
->ts
.type
!= BT_CLASS
2660 && !gfc_bt_struct (f
->sym
->ts
.type
))
2661 hidden_typelist
= TREE_CHAIN (hidden_typelist
);
2663 for (f
= gfc_sym_get_dummy_args (sym
); f
; f
= f
->next
)
2665 char name
[GFC_MAX_SYMBOL_LEN
+ 2];
2667 /* Ignore alternate returns. */
2671 type
= TREE_VALUE (typelist
);
2673 if (f
->sym
->ts
.type
== BT_CHARACTER
2674 && (!sym
->attr
.is_bind_c
|| sym
->attr
.entry_master
))
2676 tree len_type
= TREE_VALUE (hidden_typelist
);
2677 tree length
= NULL_TREE
;
2678 if (!f
->sym
->ts
.deferred
)
2679 gcc_assert (len_type
== gfc_charlen_type_node
);
2681 gcc_assert (POINTER_TYPE_P (len_type
));
2683 strcpy (&name
[1], f
->sym
->name
);
2685 length
= build_decl (input_location
,
2686 PARM_DECL
, get_identifier (name
), len_type
);
2688 hidden_arglist
= chainon (hidden_arglist
, length
);
2689 DECL_CONTEXT (length
) = fndecl
;
2690 DECL_ARTIFICIAL (length
) = 1;
2691 DECL_ARG_TYPE (length
) = len_type
;
2692 TREE_READONLY (length
) = 1;
2693 gfc_finish_decl (length
);
2695 /* Marking the length DECL_HIDDEN_STRING_LENGTH will lead
2696 to tail calls being disabled. Only do that if we
2697 potentially have broken callers. */
2698 if (flag_tail_call_workaround
2700 && f
->sym
->ts
.u
.cl
->length
2701 && f
->sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
2702 && (flag_tail_call_workaround
== 2
2703 || f
->sym
->ns
->implicit_interface_calls
))
2704 DECL_HIDDEN_STRING_LENGTH (length
) = 1;
2706 /* Remember the passed value. */
2707 if (!f
->sym
->ts
.u
.cl
|| f
->sym
->ts
.u
.cl
->passed_length
)
2709 /* This can happen if the same type is used for multiple
2710 arguments. We need to copy cl as otherwise
2711 cl->passed_length gets overwritten. */
2712 f
->sym
->ts
.u
.cl
= gfc_new_charlen (f
->sym
->ns
, f
->sym
->ts
.u
.cl
);
2714 f
->sym
->ts
.u
.cl
->passed_length
= length
;
2716 /* Use the passed value for assumed length variables. */
2717 if (!f
->sym
->ts
.u
.cl
->length
)
2719 TREE_USED (length
) = 1;
2720 gcc_assert (!f
->sym
->ts
.u
.cl
->backend_decl
);
2721 f
->sym
->ts
.u
.cl
->backend_decl
= length
;
2724 hidden_typelist
= TREE_CHAIN (hidden_typelist
);
2726 if (f
->sym
->ts
.u
.cl
->backend_decl
== NULL
2727 || f
->sym
->ts
.u
.cl
->backend_decl
== length
)
2729 if (POINTER_TYPE_P (len_type
))
2730 f
->sym
->ts
.u
.cl
->backend_decl
2731 = build_fold_indirect_ref_loc (input_location
, length
);
2732 else if (f
->sym
->ts
.u
.cl
->backend_decl
== NULL
)
2733 gfc_create_string_length (f
->sym
);
2735 /* Make sure PARM_DECL type doesn't point to incomplete type. */
2736 if (f
->sym
->attr
.flavor
== FL_PROCEDURE
)
2737 type
= build_pointer_type (gfc_get_function_type (f
->sym
));
2739 type
= gfc_sym_type (f
->sym
);
2742 /* For scalar intrinsic types, VALUE passes the value,
2743 hence, the optional status cannot be transferred via a NULL pointer.
2744 Thus, we will use a hidden argument in that case. */
2745 if (f
->sym
->attr
.optional
&& f
->sym
->attr
.value
2746 && !f
->sym
->attr
.dimension
&& f
->sym
->ts
.type
!= BT_CLASS
2747 && !gfc_bt_struct (f
->sym
->ts
.type
))
2750 strcpy (&name
[1], f
->sym
->name
);
2752 tmp
= build_decl (input_location
,
2753 PARM_DECL
, get_identifier (name
),
2756 optval_arglist
= chainon (optval_arglist
, tmp
);
2757 DECL_CONTEXT (tmp
) = fndecl
;
2758 DECL_ARTIFICIAL (tmp
) = 1;
2759 DECL_ARG_TYPE (tmp
) = boolean_type_node
;
2760 TREE_READONLY (tmp
) = 1;
2761 gfc_finish_decl (tmp
);
2763 /* The presence flag must be boolean. */
2764 gcc_assert (TREE_VALUE (optval_typelist
) == boolean_type_node
);
2765 optval_typelist
= TREE_CHAIN (optval_typelist
);
2768 /* For non-constant length array arguments, make sure they use
2769 a different type node from TYPE_ARG_TYPES type. */
2770 if (f
->sym
->attr
.dimension
2771 && type
== TREE_VALUE (typelist
)
2772 && TREE_CODE (type
) == POINTER_TYPE
2773 && GFC_ARRAY_TYPE_P (type
)
2774 && f
->sym
->as
->type
!= AS_ASSUMED_SIZE
2775 && ! COMPLETE_TYPE_P (TREE_TYPE (type
)))
2777 if (f
->sym
->attr
.flavor
== FL_PROCEDURE
)
2778 type
= build_pointer_type (gfc_get_function_type (f
->sym
));
2780 type
= gfc_sym_type (f
->sym
);
2783 if (f
->sym
->attr
.proc_pointer
)
2784 type
= build_pointer_type (type
);
2786 if (f
->sym
->attr
.volatile_
)
2787 type
= build_qualified_type (type
, TYPE_QUAL_VOLATILE
);
2789 /* Build the argument declaration. For C descriptors, we use a
2790 '_'-prefixed name for the parm_decl and inside the proc the
2793 if (sym
->attr
.is_bind_c
&& is_CFI_desc (f
->sym
, NULL
))
2795 strcpy (&name
[1], f
->sym
->name
);
2797 parm_name
= get_identifier (name
);
2800 parm_name
= gfc_sym_identifier (f
->sym
);
2801 parm
= build_decl (input_location
, PARM_DECL
, parm_name
, type
);
2803 if (f
->sym
->attr
.volatile_
)
2805 TREE_THIS_VOLATILE (parm
) = 1;
2806 TREE_SIDE_EFFECTS (parm
) = 1;
2809 /* Fill in arg stuff. */
2810 DECL_CONTEXT (parm
) = fndecl
;
2811 DECL_ARG_TYPE (parm
) = TREE_VALUE (typelist
);
2812 /* All implementation args except for VALUE are read-only. */
2813 if (!f
->sym
->attr
.value
)
2814 TREE_READONLY (parm
) = 1;
2815 if (POINTER_TYPE_P (type
)
2816 && (!f
->sym
->attr
.proc_pointer
2817 && f
->sym
->attr
.flavor
!= FL_PROCEDURE
))
2818 DECL_BY_REFERENCE (parm
) = 1;
2819 if (f
->sym
->attr
.optional
)
2821 gfc_allocate_lang_decl (parm
);
2822 GFC_DECL_OPTIONAL_ARGUMENT (parm
) = 1;
2825 gfc_finish_decl (parm
);
2826 gfc_finish_decl_attrs (parm
, &f
->sym
->attr
);
2828 f
->sym
->backend_decl
= parm
;
2830 /* Coarrays which are descriptorless or assumed-shape pass with
2831 -fcoarray=lib the token and the offset as hidden arguments. */
2832 if (flag_coarray
== GFC_FCOARRAY_LIB
2833 && ((f
->sym
->ts
.type
!= BT_CLASS
&& f
->sym
->attr
.codimension
2834 && !f
->sym
->attr
.allocatable
)
2835 || (f
->sym
->ts
.type
== BT_CLASS
2836 && CLASS_DATA (f
->sym
)->attr
.codimension
2837 && !CLASS_DATA (f
->sym
)->attr
.allocatable
)))
2843 gcc_assert (f
->sym
->backend_decl
!= NULL_TREE
2844 && !sym
->attr
.is_bind_c
);
2845 caf_type
= f
->sym
->ts
.type
== BT_CLASS
2846 ? TREE_TYPE (CLASS_DATA (f
->sym
)->backend_decl
)
2847 : TREE_TYPE (f
->sym
->backend_decl
);
2849 token
= build_decl (input_location
, PARM_DECL
,
2850 create_tmp_var_name ("caf_token"),
2851 build_qualified_type (pvoid_type_node
,
2852 TYPE_QUAL_RESTRICT
));
2853 if ((f
->sym
->ts
.type
!= BT_CLASS
2854 && f
->sym
->as
->type
!= AS_DEFERRED
)
2855 || (f
->sym
->ts
.type
== BT_CLASS
2856 && CLASS_DATA (f
->sym
)->as
->type
!= AS_DEFERRED
))
2858 gcc_assert (DECL_LANG_SPECIFIC (f
->sym
->backend_decl
) == NULL
2859 || GFC_DECL_TOKEN (f
->sym
->backend_decl
) == NULL_TREE
);
2860 if (DECL_LANG_SPECIFIC (f
->sym
->backend_decl
) == NULL
)
2861 gfc_allocate_lang_decl (f
->sym
->backend_decl
);
2862 GFC_DECL_TOKEN (f
->sym
->backend_decl
) = token
;
2866 gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
) == NULL_TREE
);
2867 GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
) = token
;
2870 DECL_CONTEXT (token
) = fndecl
;
2871 DECL_ARTIFICIAL (token
) = 1;
2872 DECL_ARG_TYPE (token
) = TREE_VALUE (typelist
);
2873 TREE_READONLY (token
) = 1;
2874 hidden_arglist
= chainon (hidden_arglist
, token
);
2875 hidden_typelist
= TREE_CHAIN (hidden_typelist
);
2876 gfc_finish_decl (token
);
2878 offset
= build_decl (input_location
, PARM_DECL
,
2879 create_tmp_var_name ("caf_offset"),
2880 gfc_array_index_type
);
2882 if ((f
->sym
->ts
.type
!= BT_CLASS
2883 && f
->sym
->as
->type
!= AS_DEFERRED
)
2884 || (f
->sym
->ts
.type
== BT_CLASS
2885 && CLASS_DATA (f
->sym
)->as
->type
!= AS_DEFERRED
))
2887 gcc_assert (GFC_DECL_CAF_OFFSET (f
->sym
->backend_decl
)
2889 GFC_DECL_CAF_OFFSET (f
->sym
->backend_decl
) = offset
;
2893 gcc_assert (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type
) == NULL_TREE
);
2894 GFC_TYPE_ARRAY_CAF_OFFSET (caf_type
) = offset
;
2896 DECL_CONTEXT (offset
) = fndecl
;
2897 DECL_ARTIFICIAL (offset
) = 1;
2898 DECL_ARG_TYPE (offset
) = TREE_VALUE (typelist
);
2899 TREE_READONLY (offset
) = 1;
2900 hidden_arglist
= chainon (hidden_arglist
, offset
);
2901 hidden_typelist
= TREE_CHAIN (hidden_typelist
);
2902 gfc_finish_decl (offset
);
2905 arglist
= chainon (arglist
, parm
);
2906 typelist
= TREE_CHAIN (typelist
);
2909 /* Add hidden present status for optional+value arguments. */
2910 arglist
= chainon (arglist
, optval_arglist
);
2912 /* Add the hidden string length parameters, unless the procedure
2914 if (!sym
->attr
.is_bind_c
)
2915 arglist
= chainon (arglist
, hidden_arglist
);
2917 gcc_assert (hidden_typelist
== NULL_TREE
2918 || TREE_VALUE (hidden_typelist
) == void_type_node
);
2919 DECL_ARGUMENTS (fndecl
) = arglist
;
2922 /* Do the setup necessary before generating the body of a function. */
2925 trans_function_start (gfc_symbol
* sym
)
2929 fndecl
= sym
->backend_decl
;
2931 /* Let GCC know the current scope is this function. */
2932 current_function_decl
= fndecl
;
2934 /* Let the world know what we're about to do. */
2935 announce_function (fndecl
);
2937 if (DECL_FILE_SCOPE_P (fndecl
))
2939 /* Create RTL for function declaration. */
2940 rest_of_decl_compilation (fndecl
, 1, 0);
2943 /* Create RTL for function definition. */
2944 make_decl_rtl (fndecl
);
2946 allocate_struct_function (fndecl
, false);
2948 /* function.cc requires a push at the start of the function. */
2952 /* Create thunks for alternate entry points. */
2955 build_entry_thunks (gfc_namespace
* ns
, bool global
)
2957 gfc_formal_arglist
*formal
;
2958 gfc_formal_arglist
*thunk_formal
;
2960 gfc_symbol
*thunk_sym
;
2966 /* This should always be a toplevel function. */
2967 gcc_assert (current_function_decl
== NULL_TREE
);
2969 gfc_save_backend_locus (&old_loc
);
2970 for (el
= ns
->entries
; el
; el
= el
->next
)
2972 vec
<tree
, va_gc
> *args
= NULL
;
2973 vec
<tree
, va_gc
> *string_args
= NULL
;
2975 thunk_sym
= el
->sym
;
2977 build_function_decl (thunk_sym
, global
);
2978 create_function_arglist (thunk_sym
);
2980 trans_function_start (thunk_sym
);
2982 thunk_fndecl
= thunk_sym
->backend_decl
;
2984 gfc_init_block (&body
);
2986 /* Pass extra parameter identifying this entry point. */
2987 tmp
= build_int_cst (gfc_array_index_type
, el
->id
);
2988 vec_safe_push (args
, tmp
);
2990 if (thunk_sym
->attr
.function
)
2992 if (gfc_return_by_reference (ns
->proc_name
))
2994 tree ref
= DECL_ARGUMENTS (current_function_decl
);
2995 vec_safe_push (args
, ref
);
2996 if (ns
->proc_name
->ts
.type
== BT_CHARACTER
)
2997 vec_safe_push (args
, DECL_CHAIN (ref
));
3001 for (formal
= gfc_sym_get_dummy_args (ns
->proc_name
); formal
;
3002 formal
= formal
->next
)
3004 /* Ignore alternate returns. */
3005 if (formal
->sym
== NULL
)
3008 /* We don't have a clever way of identifying arguments, so resort to
3009 a brute-force search. */
3010 for (thunk_formal
= gfc_sym_get_dummy_args (thunk_sym
);
3012 thunk_formal
= thunk_formal
->next
)
3014 if (thunk_formal
->sym
== formal
->sym
)
3020 /* Pass the argument. */
3021 DECL_ARTIFICIAL (thunk_formal
->sym
->backend_decl
) = 1;
3022 vec_safe_push (args
, thunk_formal
->sym
->backend_decl
);
3023 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
3025 tmp
= thunk_formal
->sym
->ts
.u
.cl
->backend_decl
;
3026 vec_safe_push (string_args
, tmp
);
3031 /* Pass NULL for a missing argument. */
3032 vec_safe_push (args
, null_pointer_node
);
3033 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
3035 tmp
= build_int_cst (gfc_charlen_type_node
, 0);
3036 vec_safe_push (string_args
, tmp
);
3041 /* Call the master function. */
3042 vec_safe_splice (args
, string_args
);
3043 tmp
= ns
->proc_name
->backend_decl
;
3044 tmp
= build_call_expr_loc_vec (input_location
, tmp
, args
);
3045 if (ns
->proc_name
->attr
.mixed_entry_master
)
3047 tree union_decl
, field
;
3048 tree master_type
= TREE_TYPE (ns
->proc_name
->backend_decl
);
3050 union_decl
= build_decl (input_location
,
3051 VAR_DECL
, get_identifier ("__result"),
3052 TREE_TYPE (master_type
));
3053 DECL_ARTIFICIAL (union_decl
) = 1;
3054 DECL_EXTERNAL (union_decl
) = 0;
3055 TREE_PUBLIC (union_decl
) = 0;
3056 TREE_USED (union_decl
) = 1;
3057 layout_decl (union_decl
, 0);
3058 pushdecl (union_decl
);
3060 DECL_CONTEXT (union_decl
) = current_function_decl
;
3061 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
3062 TREE_TYPE (union_decl
), union_decl
, tmp
);
3063 gfc_add_expr_to_block (&body
, tmp
);
3065 for (field
= TYPE_FIELDS (TREE_TYPE (union_decl
));
3066 field
; field
= DECL_CHAIN (field
))
3067 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field
)),
3068 thunk_sym
->result
->name
) == 0)
3070 gcc_assert (field
!= NULL_TREE
);
3071 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
,
3072 TREE_TYPE (field
), union_decl
, field
,
3074 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
3075 TREE_TYPE (DECL_RESULT (current_function_decl
)),
3076 DECL_RESULT (current_function_decl
), tmp
);
3077 tmp
= build1_v (RETURN_EXPR
, tmp
);
3079 else if (TREE_TYPE (DECL_RESULT (current_function_decl
))
3082 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
3083 TREE_TYPE (DECL_RESULT (current_function_decl
)),
3084 DECL_RESULT (current_function_decl
), tmp
);
3085 tmp
= build1_v (RETURN_EXPR
, tmp
);
3087 gfc_add_expr_to_block (&body
, tmp
);
3089 /* Finish off this function and send it for code generation. */
3090 DECL_SAVED_TREE (thunk_fndecl
) = gfc_finish_block (&body
);
3093 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl
)) = thunk_fndecl
;
3094 DECL_SAVED_TREE (thunk_fndecl
)
3095 = fold_build3_loc (DECL_SOURCE_LOCATION (thunk_fndecl
), BIND_EXPR
,
3096 void_type_node
, tmp
, DECL_SAVED_TREE (thunk_fndecl
),
3097 DECL_INITIAL (thunk_fndecl
));
3099 /* Output the GENERIC tree. */
3100 dump_function (TDI_original
, thunk_fndecl
);
3102 /* Store the end of the function, so that we get good line number
3103 info for the epilogue. */
3104 cfun
->function_end_locus
= input_location
;
3106 /* We're leaving the context of this function, so zap cfun.
3107 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
3108 tree_rest_of_compilation. */
3111 current_function_decl
= NULL_TREE
;
3113 cgraph_node::finalize_function (thunk_fndecl
, true);
3115 /* We share the symbols in the formal argument list with other entry
3116 points and the master function. Clear them so that they are
3117 recreated for each function. */
3118 for (formal
= gfc_sym_get_dummy_args (thunk_sym
); formal
;
3119 formal
= formal
->next
)
3120 if (formal
->sym
!= NULL
) /* Ignore alternate returns. */
3122 formal
->sym
->backend_decl
= NULL_TREE
;
3123 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
3124 formal
->sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
3127 if (thunk_sym
->attr
.function
)
3129 if (thunk_sym
->ts
.type
== BT_CHARACTER
)
3130 thunk_sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
3131 if (thunk_sym
->result
->ts
.type
== BT_CHARACTER
)
3132 thunk_sym
->result
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
3136 gfc_restore_backend_locus (&old_loc
);
3140 /* Create a decl for a function, and create any thunks for alternate entry
3141 points. If global is true, generate the function in the global binding
3142 level, otherwise in the current binding level (which can be global). */
3145 gfc_create_function_decl (gfc_namespace
* ns
, bool global
)
3147 /* Create a declaration for the master function. */
3148 build_function_decl (ns
->proc_name
, global
);
3150 /* Compile the entry thunks. */
3152 build_entry_thunks (ns
, global
);
3154 /* Now create the read argument list. */
3155 create_function_arglist (ns
->proc_name
);
3157 if (ns
->omp_declare_simd
)
3158 gfc_trans_omp_declare_simd (ns
);
3160 /* Handle 'declare variant' directives. The applicable directives might
3161 be declared in a parent namespace, so this needs to be called even if
3162 there are no local directives. */
3164 gfc_trans_omp_declare_variant (ns
);
3167 /* Return the decl used to hold the function return value. If
3168 parent_flag is set, the context is the parent_scope. */
3171 gfc_get_fake_result_decl (gfc_symbol
* sym
, int parent_flag
)
3175 tree this_fake_result_decl
;
3176 tree this_function_decl
;
3178 char name
[GFC_MAX_SYMBOL_LEN
+ 10];
3182 this_fake_result_decl
= parent_fake_result_decl
;
3183 this_function_decl
= DECL_CONTEXT (current_function_decl
);
3187 this_fake_result_decl
= current_fake_result_decl
;
3188 this_function_decl
= current_function_decl
;
3192 && sym
->ns
->proc_name
->backend_decl
== this_function_decl
3193 && sym
->ns
->proc_name
->attr
.entry_master
3194 && sym
!= sym
->ns
->proc_name
)
3197 if (this_fake_result_decl
!= NULL
)
3198 for (t
= TREE_CHAIN (this_fake_result_decl
); t
; t
= TREE_CHAIN (t
))
3199 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t
)), sym
->name
) == 0)
3202 return TREE_VALUE (t
);
3203 decl
= gfc_get_fake_result_decl (sym
->ns
->proc_name
, parent_flag
);
3206 this_fake_result_decl
= parent_fake_result_decl
;
3208 this_fake_result_decl
= current_fake_result_decl
;
3210 if (decl
&& sym
->ns
->proc_name
->attr
.mixed_entry_master
)
3214 for (field
= TYPE_FIELDS (TREE_TYPE (decl
));
3215 field
; field
= DECL_CHAIN (field
))
3216 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field
)),
3220 gcc_assert (field
!= NULL_TREE
);
3221 decl
= fold_build3_loc (input_location
, COMPONENT_REF
,
3222 TREE_TYPE (field
), decl
, field
, NULL_TREE
);
3225 var
= create_tmp_var_raw (TREE_TYPE (decl
), sym
->name
);
3227 gfc_add_decl_to_parent_function (var
);
3229 gfc_add_decl_to_function (var
);
3231 SET_DECL_VALUE_EXPR (var
, decl
);
3232 DECL_HAS_VALUE_EXPR_P (var
) = 1;
3233 GFC_DECL_RESULT (var
) = 1;
3235 TREE_CHAIN (this_fake_result_decl
)
3236 = tree_cons (get_identifier (sym
->name
), var
,
3237 TREE_CHAIN (this_fake_result_decl
));
3241 if (this_fake_result_decl
!= NULL_TREE
)
3242 return TREE_VALUE (this_fake_result_decl
);
3244 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
3249 if (sym
->ts
.type
== BT_CHARACTER
)
3251 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
3252 length
= gfc_create_string_length (sym
);
3254 length
= sym
->ts
.u
.cl
->backend_decl
;
3255 if (VAR_P (length
) && DECL_CONTEXT (length
) == NULL_TREE
)
3256 gfc_add_decl_to_function (length
);
3259 if (gfc_return_by_reference (sym
))
3261 decl
= DECL_ARGUMENTS (this_function_decl
);
3263 if (sym
->ns
->proc_name
->backend_decl
== this_function_decl
3264 && sym
->ns
->proc_name
->attr
.entry_master
)
3265 decl
= DECL_CHAIN (decl
);
3267 TREE_USED (decl
) = 1;
3269 decl
= gfc_build_dummy_array_decl (sym
, decl
);
3273 sprintf (name
, "__result_%.20s",
3274 IDENTIFIER_POINTER (DECL_NAME (this_function_decl
)));
3276 if (!sym
->attr
.mixed_entry_master
&& sym
->attr
.function
)
3277 decl
= build_decl (DECL_SOURCE_LOCATION (this_function_decl
),
3278 VAR_DECL
, get_identifier (name
),
3279 gfc_sym_type (sym
));
3281 decl
= build_decl (DECL_SOURCE_LOCATION (this_function_decl
),
3282 VAR_DECL
, get_identifier (name
),
3283 TREE_TYPE (TREE_TYPE (this_function_decl
)));
3284 DECL_ARTIFICIAL (decl
) = 1;
3285 DECL_EXTERNAL (decl
) = 0;
3286 TREE_PUBLIC (decl
) = 0;
3287 TREE_USED (decl
) = 1;
3288 GFC_DECL_RESULT (decl
) = 1;
3289 TREE_ADDRESSABLE (decl
) = 1;
3291 layout_decl (decl
, 0);
3292 gfc_finish_decl_attrs (decl
, &sym
->attr
);
3295 gfc_add_decl_to_parent_function (decl
);
3297 gfc_add_decl_to_function (decl
);
3301 parent_fake_result_decl
= build_tree_list (NULL
, decl
);
3303 current_fake_result_decl
= build_tree_list (NULL
, decl
);
3305 if (sym
->attr
.assign
)
3306 DECL_LANG_SPECIFIC (decl
) = DECL_LANG_SPECIFIC (sym
->backend_decl
);
3312 /* Builds a function decl. The remaining parameters are the types of the
3313 function arguments. Negative nargs indicates a varargs function. */
3316 build_library_function_decl_1 (tree name
, const char *spec
,
3317 tree rettype
, int nargs
, va_list p
)
3319 vec
<tree
, va_gc
> *arglist
;
3324 /* Library functions must be declared with global scope. */
3325 gcc_assert (current_function_decl
== NULL_TREE
);
3327 /* Create a list of the argument types. */
3328 vec_alloc (arglist
, abs (nargs
));
3329 for (n
= abs (nargs
); n
> 0; n
--)
3331 tree argtype
= va_arg (p
, tree
);
3332 arglist
->quick_push (argtype
);
3335 /* Build the function type and decl. */
3337 fntype
= build_function_type_vec (rettype
, arglist
);
3339 fntype
= build_varargs_function_type_vec (rettype
, arglist
);
3342 tree attr_args
= build_tree_list (NULL_TREE
,
3343 build_string (strlen (spec
), spec
));
3344 tree attrs
= tree_cons (get_identifier ("fn spec"),
3345 attr_args
, TYPE_ATTRIBUTES (fntype
));
3346 fntype
= build_type_attribute_variant (fntype
, attrs
);
3348 fndecl
= build_decl (input_location
,
3349 FUNCTION_DECL
, name
, fntype
);
3351 /* Mark this decl as external. */
3352 DECL_EXTERNAL (fndecl
) = 1;
3353 TREE_PUBLIC (fndecl
) = 1;
3357 rest_of_decl_compilation (fndecl
, 1, 0);
3362 /* Builds a function decl. The remaining parameters are the types of the
3363 function arguments. Negative nargs indicates a varargs function. */
3366 gfc_build_library_function_decl (tree name
, tree rettype
, int nargs
, ...)
3370 va_start (args
, nargs
);
3371 ret
= build_library_function_decl_1 (name
, NULL
, rettype
, nargs
, args
);
3376 /* Builds a function decl. The remaining parameters are the types of the
3377 function arguments. Negative nargs indicates a varargs function.
3378 The SPEC parameter specifies the function argument and return type
3379 specification according to the fnspec function type attribute. */
3382 gfc_build_library_function_decl_with_spec (tree name
, const char *spec
,
3383 tree rettype
, int nargs
, ...)
3387 va_start (args
, nargs
);
3390 attr_fnspec
fnspec (spec
, strlen (spec
));
3393 ret
= build_library_function_decl_1 (name
, spec
, rettype
, nargs
, args
);
3399 gfc_build_intrinsic_function_decls (void)
3401 tree gfc_int4_type_node
= gfc_get_int_type (4);
3402 tree gfc_pint4_type_node
= build_pointer_type (gfc_int4_type_node
);
3403 tree gfc_int8_type_node
= gfc_get_int_type (8);
3404 tree gfc_pint8_type_node
= build_pointer_type (gfc_int8_type_node
);
3405 tree gfc_int16_type_node
= gfc_get_int_type (16);
3406 tree gfc_logical4_type_node
= gfc_get_logical_type (4);
3407 tree pchar1_type_node
= gfc_get_pchar_type (1);
3408 tree pchar4_type_node
= gfc_get_pchar_type (4);
3410 /* String functions. */
3411 gfor_fndecl_compare_string
= gfc_build_library_function_decl_with_spec (
3412 get_identifier (PREFIX("compare_string")), ". . R . R ",
3413 integer_type_node
, 4, gfc_charlen_type_node
, pchar1_type_node
,
3414 gfc_charlen_type_node
, pchar1_type_node
);
3415 DECL_PURE_P (gfor_fndecl_compare_string
) = 1;
3416 TREE_NOTHROW (gfor_fndecl_compare_string
) = 1;
3418 gfor_fndecl_concat_string
= gfc_build_library_function_decl_with_spec (
3419 get_identifier (PREFIX("concat_string")), ". . W . R . R ",
3420 void_type_node
, 6, gfc_charlen_type_node
, pchar1_type_node
,
3421 gfc_charlen_type_node
, pchar1_type_node
,
3422 gfc_charlen_type_node
, pchar1_type_node
);
3423 TREE_NOTHROW (gfor_fndecl_concat_string
) = 1;
3425 gfor_fndecl_string_len_trim
= gfc_build_library_function_decl_with_spec (
3426 get_identifier (PREFIX("string_len_trim")), ". . R ",
3427 gfc_charlen_type_node
, 2, gfc_charlen_type_node
, pchar1_type_node
);
3428 DECL_PURE_P (gfor_fndecl_string_len_trim
) = 1;
3429 TREE_NOTHROW (gfor_fndecl_string_len_trim
) = 1;
3431 gfor_fndecl_string_index
= gfc_build_library_function_decl_with_spec (
3432 get_identifier (PREFIX("string_index")), ". . R . R . ",
3433 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
3434 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
3435 DECL_PURE_P (gfor_fndecl_string_index
) = 1;
3436 TREE_NOTHROW (gfor_fndecl_string_index
) = 1;
3438 gfor_fndecl_string_scan
= gfc_build_library_function_decl_with_spec (
3439 get_identifier (PREFIX("string_scan")), ". . R . R . ",
3440 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
3441 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
3442 DECL_PURE_P (gfor_fndecl_string_scan
) = 1;
3443 TREE_NOTHROW (gfor_fndecl_string_scan
) = 1;
3445 gfor_fndecl_string_verify
= gfc_build_library_function_decl_with_spec (
3446 get_identifier (PREFIX("string_verify")), ". . R . R . ",
3447 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
3448 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
3449 DECL_PURE_P (gfor_fndecl_string_verify
) = 1;
3450 TREE_NOTHROW (gfor_fndecl_string_verify
) = 1;
3452 gfor_fndecl_string_trim
= gfc_build_library_function_decl_with_spec (
3453 get_identifier (PREFIX("string_trim")), ". W w . R ",
3454 void_type_node
, 4, build_pointer_type (gfc_charlen_type_node
),
3455 build_pointer_type (pchar1_type_node
), gfc_charlen_type_node
,
3458 gfor_fndecl_string_minmax
= gfc_build_library_function_decl_with_spec (
3459 get_identifier (PREFIX("string_minmax")), ". W w . R ",
3460 void_type_node
, -4, build_pointer_type (gfc_charlen_type_node
),
3461 build_pointer_type (pchar1_type_node
), integer_type_node
,
3464 gfor_fndecl_adjustl
= gfc_build_library_function_decl_with_spec (
3465 get_identifier (PREFIX("adjustl")), ". W . R ",
3466 void_type_node
, 3, pchar1_type_node
, gfc_charlen_type_node
,
3468 TREE_NOTHROW (gfor_fndecl_adjustl
) = 1;
3470 gfor_fndecl_adjustr
= gfc_build_library_function_decl_with_spec (
3471 get_identifier (PREFIX("adjustr")), ". W . R ",
3472 void_type_node
, 3, pchar1_type_node
, gfc_charlen_type_node
,
3474 TREE_NOTHROW (gfor_fndecl_adjustr
) = 1;
3476 gfor_fndecl_select_string
= gfc_build_library_function_decl_with_spec (
3477 get_identifier (PREFIX("select_string")), ". R . R . ",
3478 integer_type_node
, 4, pvoid_type_node
, integer_type_node
,
3479 pchar1_type_node
, gfc_charlen_type_node
);
3480 DECL_PURE_P (gfor_fndecl_select_string
) = 1;
3481 TREE_NOTHROW (gfor_fndecl_select_string
) = 1;
3483 gfor_fndecl_compare_string_char4
= gfc_build_library_function_decl_with_spec (
3484 get_identifier (PREFIX("compare_string_char4")), ". . R . R ",
3485 integer_type_node
, 4, gfc_charlen_type_node
, pchar4_type_node
,
3486 gfc_charlen_type_node
, pchar4_type_node
);
3487 DECL_PURE_P (gfor_fndecl_compare_string_char4
) = 1;
3488 TREE_NOTHROW (gfor_fndecl_compare_string_char4
) = 1;
3490 gfor_fndecl_concat_string_char4
= gfc_build_library_function_decl_with_spec (
3491 get_identifier (PREFIX("concat_string_char4")), ". . W . R . R ",
3492 void_type_node
, 6, gfc_charlen_type_node
, pchar4_type_node
,
3493 gfc_charlen_type_node
, pchar4_type_node
, gfc_charlen_type_node
,
3495 TREE_NOTHROW (gfor_fndecl_concat_string_char4
) = 1;
3497 gfor_fndecl_string_len_trim_char4
= gfc_build_library_function_decl_with_spec (
3498 get_identifier (PREFIX("string_len_trim_char4")), ". . R ",
3499 gfc_charlen_type_node
, 2, gfc_charlen_type_node
, pchar4_type_node
);
3500 DECL_PURE_P (gfor_fndecl_string_len_trim_char4
) = 1;
3501 TREE_NOTHROW (gfor_fndecl_string_len_trim_char4
) = 1;
3503 gfor_fndecl_string_index_char4
= gfc_build_library_function_decl_with_spec (
3504 get_identifier (PREFIX("string_index_char4")), ". . R . R . ",
3505 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
3506 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
3507 DECL_PURE_P (gfor_fndecl_string_index_char4
) = 1;
3508 TREE_NOTHROW (gfor_fndecl_string_index_char4
) = 1;
3510 gfor_fndecl_string_scan_char4
= gfc_build_library_function_decl_with_spec (
3511 get_identifier (PREFIX("string_scan_char4")), ". . R . R . ",
3512 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
3513 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
3514 DECL_PURE_P (gfor_fndecl_string_scan_char4
) = 1;
3515 TREE_NOTHROW (gfor_fndecl_string_scan_char4
) = 1;
3517 gfor_fndecl_string_verify_char4
= gfc_build_library_function_decl_with_spec (
3518 get_identifier (PREFIX("string_verify_char4")), ". . R . R . ",
3519 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
3520 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
3521 DECL_PURE_P (gfor_fndecl_string_verify_char4
) = 1;
3522 TREE_NOTHROW (gfor_fndecl_string_verify_char4
) = 1;
3524 gfor_fndecl_string_trim_char4
= gfc_build_library_function_decl_with_spec (
3525 get_identifier (PREFIX("string_trim_char4")), ". W w . R ",
3526 void_type_node
, 4, build_pointer_type (gfc_charlen_type_node
),
3527 build_pointer_type (pchar4_type_node
), gfc_charlen_type_node
,
3530 gfor_fndecl_string_minmax_char4
= gfc_build_library_function_decl_with_spec (
3531 get_identifier (PREFIX("string_minmax_char4")), ". W w . R ",
3532 void_type_node
, -4, build_pointer_type (gfc_charlen_type_node
),
3533 build_pointer_type (pchar4_type_node
), integer_type_node
,
3536 gfor_fndecl_adjustl_char4
= gfc_build_library_function_decl_with_spec (
3537 get_identifier (PREFIX("adjustl_char4")), ". W . R ",
3538 void_type_node
, 3, pchar4_type_node
, gfc_charlen_type_node
,
3540 TREE_NOTHROW (gfor_fndecl_adjustl_char4
) = 1;
3542 gfor_fndecl_adjustr_char4
= gfc_build_library_function_decl_with_spec (
3543 get_identifier (PREFIX("adjustr_char4")), ". W . R ",
3544 void_type_node
, 3, pchar4_type_node
, gfc_charlen_type_node
,
3546 TREE_NOTHROW (gfor_fndecl_adjustr_char4
) = 1;
3548 gfor_fndecl_select_string_char4
= gfc_build_library_function_decl_with_spec (
3549 get_identifier (PREFIX("select_string_char4")), ". R . R . ",
3550 integer_type_node
, 4, pvoid_type_node
, integer_type_node
,
3551 pvoid_type_node
, gfc_charlen_type_node
);
3552 DECL_PURE_P (gfor_fndecl_select_string_char4
) = 1;
3553 TREE_NOTHROW (gfor_fndecl_select_string_char4
) = 1;
3556 /* Conversion between character kinds. */
3558 gfor_fndecl_convert_char1_to_char4
= gfc_build_library_function_decl_with_spec (
3559 get_identifier (PREFIX("convert_char1_to_char4")), ". w . R ",
3560 void_type_node
, 3, build_pointer_type (pchar4_type_node
),
3561 gfc_charlen_type_node
, pchar1_type_node
);
3563 gfor_fndecl_convert_char4_to_char1
= gfc_build_library_function_decl_with_spec (
3564 get_identifier (PREFIX("convert_char4_to_char1")), ". w . R ",
3565 void_type_node
, 3, build_pointer_type (pchar1_type_node
),
3566 gfc_charlen_type_node
, pchar4_type_node
);
3568 /* Misc. functions. */
3570 gfor_fndecl_ttynam
= gfc_build_library_function_decl_with_spec (
3571 get_identifier (PREFIX("ttynam")), ". W . . ",
3572 void_type_node
, 3, pchar_type_node
, gfc_charlen_type_node
,
3575 gfor_fndecl_fdate
= gfc_build_library_function_decl_with_spec (
3576 get_identifier (PREFIX("fdate")), ". W . ",
3577 void_type_node
, 2, pchar_type_node
, gfc_charlen_type_node
);
3579 gfor_fndecl_ctime
= gfc_build_library_function_decl_with_spec (
3580 get_identifier (PREFIX("ctime")), ". W . . ",
3581 void_type_node
, 3, pchar_type_node
, gfc_charlen_type_node
,
3582 gfc_int8_type_node
);
3584 gfor_fndecl_random_init
= gfc_build_library_function_decl (
3585 get_identifier (PREFIX("random_init")),
3586 void_type_node
, 3, gfc_logical4_type_node
, gfc_logical4_type_node
,
3587 gfc_int4_type_node
);
3589 // gfor_fndecl_caf_rand_init is defined in the lib-coarray section below.
3591 gfor_fndecl_sc_kind
= gfc_build_library_function_decl_with_spec (
3592 get_identifier (PREFIX("selected_char_kind")), ". . R ",
3593 gfc_int4_type_node
, 2, gfc_charlen_type_node
, pchar_type_node
);
3594 DECL_PURE_P (gfor_fndecl_sc_kind
) = 1;
3595 TREE_NOTHROW (gfor_fndecl_sc_kind
) = 1;
3597 gfor_fndecl_si_kind
= gfc_build_library_function_decl_with_spec (
3598 get_identifier (PREFIX("selected_int_kind")), ". R ",
3599 gfc_int4_type_node
, 1, pvoid_type_node
);
3600 DECL_PURE_P (gfor_fndecl_si_kind
) = 1;
3601 TREE_NOTHROW (gfor_fndecl_si_kind
) = 1;
3603 gfor_fndecl_sr_kind
= gfc_build_library_function_decl_with_spec (
3604 get_identifier (PREFIX("selected_real_kind2008")), ". R R ",
3605 gfc_int4_type_node
, 3, pvoid_type_node
, pvoid_type_node
,
3607 DECL_PURE_P (gfor_fndecl_sr_kind
) = 1;
3608 TREE_NOTHROW (gfor_fndecl_sr_kind
) = 1;
3610 gfor_fndecl_system_clock4
= gfc_build_library_function_decl (
3611 get_identifier (PREFIX("system_clock_4")),
3612 void_type_node
, 3, gfc_pint4_type_node
, gfc_pint4_type_node
,
3613 gfc_pint4_type_node
);
3615 gfor_fndecl_system_clock8
= gfc_build_library_function_decl (
3616 get_identifier (PREFIX("system_clock_8")),
3617 void_type_node
, 3, gfc_pint8_type_node
, gfc_pint8_type_node
,
3618 gfc_pint8_type_node
);
3620 /* Power functions. */
3622 tree ctype
, rtype
, itype
, jtype
;
3623 int rkind
, ikind
, jkind
;
3626 static int ikinds
[NIKINDS
] = {4, 8, 16};
3627 static int rkinds
[NRKINDS
] = {4, 8, 10, 16};
3628 char name
[PREFIX_LEN
+ 12]; /* _gfortran_pow_?n_?n */
3630 for (ikind
=0; ikind
< NIKINDS
; ikind
++)
3632 itype
= gfc_get_int_type (ikinds
[ikind
]);
3634 for (jkind
=0; jkind
< NIKINDS
; jkind
++)
3636 jtype
= gfc_get_int_type (ikinds
[jkind
]);
3639 sprintf (name
, PREFIX("pow_i%d_i%d"), ikinds
[ikind
],
3641 gfor_fndecl_math_powi
[jkind
][ikind
].integer
=
3642 gfc_build_library_function_decl (get_identifier (name
),
3643 jtype
, 2, jtype
, itype
);
3644 TREE_READONLY (gfor_fndecl_math_powi
[jkind
][ikind
].integer
) = 1;
3645 TREE_NOTHROW (gfor_fndecl_math_powi
[jkind
][ikind
].integer
) = 1;
3649 for (rkind
= 0; rkind
< NRKINDS
; rkind
++)
3651 rtype
= gfc_get_real_type (rkinds
[rkind
]);
3654 sprintf (name
, PREFIX("pow_r%d_i%d"),
3655 gfc_type_abi_kind (BT_REAL
, rkinds
[rkind
]),
3657 gfor_fndecl_math_powi
[rkind
][ikind
].real
=
3658 gfc_build_library_function_decl (get_identifier (name
),
3659 rtype
, 2, rtype
, itype
);
3660 TREE_READONLY (gfor_fndecl_math_powi
[rkind
][ikind
].real
) = 1;
3661 TREE_NOTHROW (gfor_fndecl_math_powi
[rkind
][ikind
].real
) = 1;
3664 ctype
= gfc_get_complex_type (rkinds
[rkind
]);
3667 sprintf (name
, PREFIX("pow_c%d_i%d"),
3668 gfc_type_abi_kind (BT_REAL
, rkinds
[rkind
]),
3670 gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
=
3671 gfc_build_library_function_decl (get_identifier (name
),
3672 ctype
, 2,ctype
, itype
);
3673 TREE_READONLY (gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
) = 1;
3674 TREE_NOTHROW (gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
) = 1;
3682 gfor_fndecl_math_ishftc4
= gfc_build_library_function_decl (
3683 get_identifier (PREFIX("ishftc4")),
3684 gfc_int4_type_node
, 3, gfc_int4_type_node
, gfc_int4_type_node
,
3685 gfc_int4_type_node
);
3686 TREE_READONLY (gfor_fndecl_math_ishftc4
) = 1;
3687 TREE_NOTHROW (gfor_fndecl_math_ishftc4
) = 1;
3689 gfor_fndecl_math_ishftc8
= gfc_build_library_function_decl (
3690 get_identifier (PREFIX("ishftc8")),
3691 gfc_int8_type_node
, 3, gfc_int8_type_node
, gfc_int4_type_node
,
3692 gfc_int4_type_node
);
3693 TREE_READONLY (gfor_fndecl_math_ishftc8
) = 1;
3694 TREE_NOTHROW (gfor_fndecl_math_ishftc8
) = 1;
3696 if (gfc_int16_type_node
)
3698 gfor_fndecl_math_ishftc16
= gfc_build_library_function_decl (
3699 get_identifier (PREFIX("ishftc16")),
3700 gfc_int16_type_node
, 3, gfc_int16_type_node
, gfc_int4_type_node
,
3701 gfc_int4_type_node
);
3702 TREE_READONLY (gfor_fndecl_math_ishftc16
) = 1;
3703 TREE_NOTHROW (gfor_fndecl_math_ishftc16
) = 1;
3706 /* BLAS functions. */
3708 tree pint
= build_pointer_type (integer_type_node
);
3709 tree ps
= build_pointer_type (gfc_get_real_type (gfc_default_real_kind
));
3710 tree pd
= build_pointer_type (gfc_get_real_type (gfc_default_double_kind
));
3711 tree pc
= build_pointer_type (gfc_get_complex_type (gfc_default_real_kind
));
3712 tree pz
= build_pointer_type
3713 (gfc_get_complex_type (gfc_default_double_kind
));
3715 gfor_fndecl_sgemm
= gfc_build_library_function_decl
3717 (flag_underscoring
? "sgemm_" : "sgemm"),
3718 void_type_node
, 15, pchar_type_node
,
3719 pchar_type_node
, pint
, pint
, pint
, ps
, ps
, pint
,
3720 ps
, pint
, ps
, ps
, pint
, integer_type_node
,
3722 gfor_fndecl_dgemm
= gfc_build_library_function_decl
3724 (flag_underscoring
? "dgemm_" : "dgemm"),
3725 void_type_node
, 15, pchar_type_node
,
3726 pchar_type_node
, pint
, pint
, pint
, pd
, pd
, pint
,
3727 pd
, pint
, pd
, pd
, pint
, integer_type_node
,
3729 gfor_fndecl_cgemm
= gfc_build_library_function_decl
3731 (flag_underscoring
? "cgemm_" : "cgemm"),
3732 void_type_node
, 15, pchar_type_node
,
3733 pchar_type_node
, pint
, pint
, pint
, pc
, pc
, pint
,
3734 pc
, pint
, pc
, pc
, pint
, integer_type_node
,
3736 gfor_fndecl_zgemm
= gfc_build_library_function_decl
3738 (flag_underscoring
? "zgemm_" : "zgemm"),
3739 void_type_node
, 15, pchar_type_node
,
3740 pchar_type_node
, pint
, pint
, pint
, pz
, pz
, pint
,
3741 pz
, pint
, pz
, pz
, pint
, integer_type_node
,
3745 /* Other functions. */
3746 gfor_fndecl_iargc
= gfc_build_library_function_decl (
3747 get_identifier (PREFIX ("iargc")), gfc_int4_type_node
, 0);
3748 TREE_NOTHROW (gfor_fndecl_iargc
) = 1;
3750 gfor_fndecl_kill_sub
= gfc_build_library_function_decl (
3751 get_identifier (PREFIX ("kill_sub")), void_type_node
,
3752 3, gfc_int4_type_node
, gfc_int4_type_node
, gfc_pint4_type_node
);
3754 gfor_fndecl_kill
= gfc_build_library_function_decl (
3755 get_identifier (PREFIX ("kill")), gfc_int4_type_node
,
3756 2, gfc_int4_type_node
, gfc_int4_type_node
);
3758 gfor_fndecl_is_contiguous0
= gfc_build_library_function_decl_with_spec (
3759 get_identifier (PREFIX("is_contiguous0")), ". R ",
3760 gfc_int4_type_node
, 1, pvoid_type_node
);
3761 DECL_PURE_P (gfor_fndecl_is_contiguous0
) = 1;
3762 TREE_NOTHROW (gfor_fndecl_is_contiguous0
) = 1;
3766 /* Make prototypes for runtime library functions. */
3769 gfc_build_builtin_function_decls (void)
3771 tree gfc_int8_type_node
= gfc_get_int_type (8);
3773 gfor_fndecl_stop_numeric
= gfc_build_library_function_decl (
3774 get_identifier (PREFIX("stop_numeric")),
3775 void_type_node
, 2, integer_type_node
, boolean_type_node
);
3776 /* STOP doesn't return. */
3777 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric
) = 1;
3779 gfor_fndecl_stop_string
= gfc_build_library_function_decl_with_spec (
3780 get_identifier (PREFIX("stop_string")), ". R . . ",
3781 void_type_node
, 3, pchar_type_node
, size_type_node
,
3783 /* STOP doesn't return. */
3784 TREE_THIS_VOLATILE (gfor_fndecl_stop_string
) = 1;
3786 gfor_fndecl_error_stop_numeric
= gfc_build_library_function_decl (
3787 get_identifier (PREFIX("error_stop_numeric")),
3788 void_type_node
, 2, integer_type_node
, boolean_type_node
);
3789 /* ERROR STOP doesn't return. */
3790 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_numeric
) = 1;
3792 gfor_fndecl_error_stop_string
= gfc_build_library_function_decl_with_spec (
3793 get_identifier (PREFIX("error_stop_string")), ". R . . ",
3794 void_type_node
, 3, pchar_type_node
, size_type_node
,
3796 /* ERROR STOP doesn't return. */
3797 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_string
) = 1;
3799 gfor_fndecl_pause_numeric
= gfc_build_library_function_decl (
3800 get_identifier (PREFIX("pause_numeric")),
3801 void_type_node
, 1, gfc_int8_type_node
);
3803 gfor_fndecl_pause_string
= gfc_build_library_function_decl_with_spec (
3804 get_identifier (PREFIX("pause_string")), ". R . ",
3805 void_type_node
, 2, pchar_type_node
, size_type_node
);
3807 gfor_fndecl_runtime_error
= gfc_build_library_function_decl_with_spec (
3808 get_identifier (PREFIX("runtime_error")), ". R ",
3809 void_type_node
, -1, pchar_type_node
);
3810 /* The runtime_error function does not return. */
3811 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error
) = 1;
3813 gfor_fndecl_runtime_error_at
= gfc_build_library_function_decl_with_spec (
3814 get_identifier (PREFIX("runtime_error_at")), ". R R ",
3815 void_type_node
, -2, pchar_type_node
, pchar_type_node
);
3816 /* The runtime_error_at function does not return. */
3817 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at
) = 1;
3819 gfor_fndecl_runtime_warning_at
= gfc_build_library_function_decl_with_spec (
3820 get_identifier (PREFIX("runtime_warning_at")), ". R R ",
3821 void_type_node
, -2, pchar_type_node
, pchar_type_node
);
3823 gfor_fndecl_generate_error
= gfc_build_library_function_decl_with_spec (
3824 get_identifier (PREFIX("generate_error")), ". W . R ",
3825 void_type_node
, 3, pvoid_type_node
, integer_type_node
,
3828 gfor_fndecl_os_error_at
= gfc_build_library_function_decl_with_spec (
3829 get_identifier (PREFIX("os_error_at")), ". R R ",
3830 void_type_node
, -2, pchar_type_node
, pchar_type_node
);
3831 /* The os_error_at function does not return. */
3832 TREE_THIS_VOLATILE (gfor_fndecl_os_error_at
) = 1;
3834 gfor_fndecl_set_args
= gfc_build_library_function_decl (
3835 get_identifier (PREFIX("set_args")),
3836 void_type_node
, 2, integer_type_node
,
3837 build_pointer_type (pchar_type_node
));
3839 gfor_fndecl_set_fpe
= gfc_build_library_function_decl (
3840 get_identifier (PREFIX("set_fpe")),
3841 void_type_node
, 1, integer_type_node
);
3843 gfor_fndecl_ieee_procedure_entry
= gfc_build_library_function_decl (
3844 get_identifier (PREFIX("ieee_procedure_entry")),
3845 void_type_node
, 1, pvoid_type_node
);
3847 gfor_fndecl_ieee_procedure_exit
= gfc_build_library_function_decl (
3848 get_identifier (PREFIX("ieee_procedure_exit")),
3849 void_type_node
, 1, pvoid_type_node
);
3851 /* Keep the array dimension in sync with the call, later in this file. */
3852 gfor_fndecl_set_options
= gfc_build_library_function_decl_with_spec (
3853 get_identifier (PREFIX("set_options")), ". . R ",
3854 void_type_node
, 2, integer_type_node
,
3855 build_pointer_type (integer_type_node
));
3857 gfor_fndecl_set_convert
= gfc_build_library_function_decl (
3858 get_identifier (PREFIX("set_convert")),
3859 void_type_node
, 1, integer_type_node
);
3861 gfor_fndecl_set_record_marker
= gfc_build_library_function_decl (
3862 get_identifier (PREFIX("set_record_marker")),
3863 void_type_node
, 1, integer_type_node
);
3865 gfor_fndecl_set_max_subrecord_length
= gfc_build_library_function_decl (
3866 get_identifier (PREFIX("set_max_subrecord_length")),
3867 void_type_node
, 1, integer_type_node
);
3869 gfor_fndecl_in_pack
= gfc_build_library_function_decl_with_spec (
3870 get_identifier (PREFIX("internal_pack")), ". r ",
3871 pvoid_type_node
, 1, pvoid_type_node
);
3873 gfor_fndecl_in_unpack
= gfc_build_library_function_decl_with_spec (
3874 get_identifier (PREFIX("internal_unpack")), ". w R ",
3875 void_type_node
, 2, pvoid_type_node
, pvoid_type_node
);
3877 gfor_fndecl_associated
= gfc_build_library_function_decl_with_spec (
3878 get_identifier (PREFIX("associated")), ". R R ",
3879 integer_type_node
, 2, ppvoid_type_node
, ppvoid_type_node
);
3880 DECL_PURE_P (gfor_fndecl_associated
) = 1;
3881 TREE_NOTHROW (gfor_fndecl_associated
) = 1;
3883 /* Coarray library calls. */
3884 if (flag_coarray
== GFC_FCOARRAY_LIB
)
3886 tree pint_type
, pppchar_type
;
3888 pint_type
= build_pointer_type (integer_type_node
);
3890 = build_pointer_type (build_pointer_type (pchar_type_node
));
3892 gfor_fndecl_caf_init
= gfc_build_library_function_decl_with_spec (
3893 get_identifier (PREFIX("caf_init")), ". W W ",
3894 void_type_node
, 2, pint_type
, pppchar_type
);
3896 gfor_fndecl_caf_finalize
= gfc_build_library_function_decl (
3897 get_identifier (PREFIX("caf_finalize")), void_type_node
, 0);
3899 gfor_fndecl_caf_this_image
= gfc_build_library_function_decl (
3900 get_identifier (PREFIX("caf_this_image")), integer_type_node
,
3901 1, integer_type_node
);
3903 gfor_fndecl_caf_num_images
= gfc_build_library_function_decl (
3904 get_identifier (PREFIX("caf_num_images")), integer_type_node
,
3905 2, integer_type_node
, integer_type_node
);
3907 gfor_fndecl_caf_register
= gfc_build_library_function_decl_with_spec (
3908 get_identifier (PREFIX("caf_register")), ". . . W w w w . ",
3910 size_type_node
, integer_type_node
, ppvoid_type_node
, pvoid_type_node
,
3911 pint_type
, pchar_type_node
, size_type_node
);
3913 gfor_fndecl_caf_deregister
= gfc_build_library_function_decl_with_spec (
3914 get_identifier (PREFIX("caf_deregister")), ". W . w w . ",
3916 ppvoid_type_node
, integer_type_node
, pint_type
, pchar_type_node
,
3919 gfor_fndecl_caf_get
= gfc_build_library_function_decl_with_spec (
3920 get_identifier (PREFIX("caf_get")), ". r . . r r w . . . w ",
3922 pvoid_type_node
, size_type_node
, integer_type_node
, pvoid_type_node
,
3923 pvoid_type_node
, pvoid_type_node
, integer_type_node
, integer_type_node
,
3924 boolean_type_node
, pint_type
);
3926 gfor_fndecl_caf_send
= gfc_build_library_function_decl_with_spec (
3927 get_identifier (PREFIX("caf_send")), ". r . . w r r . . . w ",
3929 pvoid_type_node
, size_type_node
, integer_type_node
, pvoid_type_node
,
3930 pvoid_type_node
, pvoid_type_node
, integer_type_node
, integer_type_node
,
3931 boolean_type_node
, pint_type
, pvoid_type_node
);
3933 gfor_fndecl_caf_sendget
= gfc_build_library_function_decl_with_spec (
3934 get_identifier (PREFIX("caf_sendget")), ". r . . w r r . . r r . . . w ",
3935 void_type_node
, 14, pvoid_type_node
, size_type_node
, integer_type_node
,
3936 pvoid_type_node
, pvoid_type_node
, pvoid_type_node
, size_type_node
,
3937 integer_type_node
, pvoid_type_node
, pvoid_type_node
, integer_type_node
,
3938 integer_type_node
, boolean_type_node
, integer_type_node
);
3940 gfor_fndecl_caf_get_by_ref
= gfc_build_library_function_decl_with_spec (
3941 get_identifier (PREFIX("caf_get_by_ref")), ". r . w r . . . . w . ",
3943 10, pvoid_type_node
, integer_type_node
, pvoid_type_node
,
3944 pvoid_type_node
, integer_type_node
, integer_type_node
,
3945 boolean_type_node
, boolean_type_node
, pint_type
, integer_type_node
);
3947 gfor_fndecl_caf_send_by_ref
= gfc_build_library_function_decl_with_spec (
3948 get_identifier (PREFIX("caf_send_by_ref")), ". r . r r . . . . w . ",
3949 void_type_node
, 10, pvoid_type_node
, integer_type_node
, pvoid_type_node
,
3950 pvoid_type_node
, integer_type_node
, integer_type_node
,
3951 boolean_type_node
, boolean_type_node
, pint_type
, integer_type_node
);
3953 gfor_fndecl_caf_sendget_by_ref
3954 = gfc_build_library_function_decl_with_spec (
3955 get_identifier (PREFIX("caf_sendget_by_ref")),
3956 ". r . r r . r . . . w w . . ",
3957 void_type_node
, 13, pvoid_type_node
, integer_type_node
,
3958 pvoid_type_node
, pvoid_type_node
, integer_type_node
,
3959 pvoid_type_node
, integer_type_node
, integer_type_node
,
3960 boolean_type_node
, pint_type
, pint_type
, integer_type_node
,
3963 gfor_fndecl_caf_sync_all
= gfc_build_library_function_decl_with_spec (
3964 get_identifier (PREFIX("caf_sync_all")), ". w w . ", void_type_node
,
3965 3, pint_type
, pchar_type_node
, size_type_node
);
3967 gfor_fndecl_caf_sync_memory
= gfc_build_library_function_decl_with_spec (
3968 get_identifier (PREFIX("caf_sync_memory")), ". w w . ", void_type_node
,
3969 3, pint_type
, pchar_type_node
, size_type_node
);
3971 gfor_fndecl_caf_sync_images
= gfc_build_library_function_decl_with_spec (
3972 get_identifier (PREFIX("caf_sync_images")), ". . r w w . ", void_type_node
,
3973 5, integer_type_node
, pint_type
, pint_type
,
3974 pchar_type_node
, size_type_node
);
3976 gfor_fndecl_caf_error_stop
= gfc_build_library_function_decl (
3977 get_identifier (PREFIX("caf_error_stop")),
3978 void_type_node
, 1, integer_type_node
);
3979 /* CAF's ERROR STOP doesn't return. */
3980 TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop
) = 1;
3982 gfor_fndecl_caf_error_stop_str
= gfc_build_library_function_decl_with_spec (
3983 get_identifier (PREFIX("caf_error_stop_str")), ". r . ",
3984 void_type_node
, 2, pchar_type_node
, size_type_node
);
3985 /* CAF's ERROR STOP doesn't return. */
3986 TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop_str
) = 1;
3988 gfor_fndecl_caf_stop_numeric
= gfc_build_library_function_decl (
3989 get_identifier (PREFIX("caf_stop_numeric")),
3990 void_type_node
, 1, integer_type_node
);
3991 /* CAF's STOP doesn't return. */
3992 TREE_THIS_VOLATILE (gfor_fndecl_caf_stop_numeric
) = 1;
3994 gfor_fndecl_caf_stop_str
= gfc_build_library_function_decl_with_spec (
3995 get_identifier (PREFIX("caf_stop_str")), ". r . ",
3996 void_type_node
, 2, pchar_type_node
, size_type_node
);
3997 /* CAF's STOP doesn't return. */
3998 TREE_THIS_VOLATILE (gfor_fndecl_caf_stop_str
) = 1;
4000 gfor_fndecl_caf_atomic_def
= gfc_build_library_function_decl_with_spec (
4001 get_identifier (PREFIX("caf_atomic_define")), ". r . . w w . . ",
4002 void_type_node
, 7, pvoid_type_node
, size_type_node
, integer_type_node
,
4003 pvoid_type_node
, pint_type
, integer_type_node
, integer_type_node
);
4005 gfor_fndecl_caf_atomic_ref
= gfc_build_library_function_decl_with_spec (
4006 get_identifier (PREFIX("caf_atomic_ref")), ". r . . w w . . ",
4007 void_type_node
, 7, pvoid_type_node
, size_type_node
, integer_type_node
,
4008 pvoid_type_node
, pint_type
, integer_type_node
, integer_type_node
);
4010 gfor_fndecl_caf_atomic_cas
= gfc_build_library_function_decl_with_spec (
4011 get_identifier (PREFIX("caf_atomic_cas")), ". r . . w r r w . . ",
4012 void_type_node
, 9, pvoid_type_node
, size_type_node
, integer_type_node
,
4013 pvoid_type_node
, pvoid_type_node
, pvoid_type_node
, pint_type
,
4014 integer_type_node
, integer_type_node
);
4016 gfor_fndecl_caf_atomic_op
= gfc_build_library_function_decl_with_spec (
4017 get_identifier (PREFIX("caf_atomic_op")), ". . r . . r w w . . ",
4018 void_type_node
, 9, integer_type_node
, pvoid_type_node
, size_type_node
,
4019 integer_type_node
, pvoid_type_node
, pvoid_type_node
, pint_type
,
4020 integer_type_node
, integer_type_node
);
4022 gfor_fndecl_caf_lock
= gfc_build_library_function_decl_with_spec (
4023 get_identifier (PREFIX("caf_lock")), ". r . . w w w . ",
4024 void_type_node
, 7, pvoid_type_node
, size_type_node
, integer_type_node
,
4025 pint_type
, pint_type
, pchar_type_node
, size_type_node
);
4027 gfor_fndecl_caf_unlock
= gfc_build_library_function_decl_with_spec (
4028 get_identifier (PREFIX("caf_unlock")), ". r . . w w . ",
4029 void_type_node
, 6, pvoid_type_node
, size_type_node
, integer_type_node
,
4030 pint_type
, pchar_type_node
, size_type_node
);
4032 gfor_fndecl_caf_event_post
= gfc_build_library_function_decl_with_spec (
4033 get_identifier (PREFIX("caf_event_post")), ". r . . w w . ",
4034 void_type_node
, 6, pvoid_type_node
, size_type_node
, integer_type_node
,
4035 pint_type
, pchar_type_node
, size_type_node
);
4037 gfor_fndecl_caf_event_wait
= gfc_build_library_function_decl_with_spec (
4038 get_identifier (PREFIX("caf_event_wait")), ". r . . w w . ",
4039 void_type_node
, 6, pvoid_type_node
, size_type_node
, integer_type_node
,
4040 pint_type
, pchar_type_node
, size_type_node
);
4042 gfor_fndecl_caf_event_query
= gfc_build_library_function_decl_with_spec (
4043 get_identifier (PREFIX("caf_event_query")), ". r . . w w ",
4044 void_type_node
, 5, pvoid_type_node
, size_type_node
, integer_type_node
,
4045 pint_type
, pint_type
);
4047 gfor_fndecl_caf_fail_image
= gfc_build_library_function_decl (
4048 get_identifier (PREFIX("caf_fail_image")), void_type_node
, 0);
4049 /* CAF's FAIL doesn't return. */
4050 TREE_THIS_VOLATILE (gfor_fndecl_caf_fail_image
) = 1;
4052 gfor_fndecl_caf_failed_images
4053 = gfc_build_library_function_decl_with_spec (
4054 get_identifier (PREFIX("caf_failed_images")), ". w . r ",
4055 void_type_node
, 3, pvoid_type_node
, ppvoid_type_node
,
4058 gfor_fndecl_caf_form_team
4059 = gfc_build_library_function_decl_with_spec (
4060 get_identifier (PREFIX("caf_form_team")), ". . W . ",
4061 void_type_node
, 3, integer_type_node
, ppvoid_type_node
,
4064 gfor_fndecl_caf_change_team
4065 = gfc_build_library_function_decl_with_spec (
4066 get_identifier (PREFIX("caf_change_team")), ". w . ",
4067 void_type_node
, 2, ppvoid_type_node
,
4070 gfor_fndecl_caf_end_team
4071 = gfc_build_library_function_decl (
4072 get_identifier (PREFIX("caf_end_team")), void_type_node
, 0);
4074 gfor_fndecl_caf_get_team
4075 = gfc_build_library_function_decl (
4076 get_identifier (PREFIX("caf_get_team")),
4077 void_type_node
, 1, integer_type_node
);
4079 gfor_fndecl_caf_sync_team
4080 = gfc_build_library_function_decl_with_spec (
4081 get_identifier (PREFIX("caf_sync_team")), ". r . ",
4082 void_type_node
, 2, ppvoid_type_node
,
4085 gfor_fndecl_caf_team_number
4086 = gfc_build_library_function_decl_with_spec (
4087 get_identifier (PREFIX("caf_team_number")), ". r ",
4088 integer_type_node
, 1, integer_type_node
);
4090 gfor_fndecl_caf_image_status
4091 = gfc_build_library_function_decl_with_spec (
4092 get_identifier (PREFIX("caf_image_status")), ". . r ",
4093 integer_type_node
, 2, integer_type_node
, ppvoid_type_node
);
4095 gfor_fndecl_caf_stopped_images
4096 = gfc_build_library_function_decl_with_spec (
4097 get_identifier (PREFIX("caf_stopped_images")), ". w r r ",
4098 void_type_node
, 3, pvoid_type_node
, ppvoid_type_node
,
4101 gfor_fndecl_co_broadcast
= gfc_build_library_function_decl_with_spec (
4102 get_identifier (PREFIX("caf_co_broadcast")), ". w . w w . ",
4103 void_type_node
, 5, pvoid_type_node
, integer_type_node
,
4104 pint_type
, pchar_type_node
, size_type_node
);
4106 gfor_fndecl_co_max
= gfc_build_library_function_decl_with_spec (
4107 get_identifier (PREFIX("caf_co_max")), ". w . w w . . ",
4108 void_type_node
, 6, pvoid_type_node
, integer_type_node
,
4109 pint_type
, pchar_type_node
, integer_type_node
, size_type_node
);
4111 gfor_fndecl_co_min
= gfc_build_library_function_decl_with_spec (
4112 get_identifier (PREFIX("caf_co_min")), ". w . w w . . ",
4113 void_type_node
, 6, pvoid_type_node
, integer_type_node
,
4114 pint_type
, pchar_type_node
, integer_type_node
, size_type_node
);
4116 gfor_fndecl_co_reduce
= gfc_build_library_function_decl_with_spec (
4117 get_identifier (PREFIX("caf_co_reduce")), ". w r . . w w . . ",
4118 void_type_node
, 8, pvoid_type_node
,
4119 build_pointer_type (build_varargs_function_type_list (void_type_node
,
4121 integer_type_node
, integer_type_node
, pint_type
, pchar_type_node
,
4122 integer_type_node
, size_type_node
);
4124 gfor_fndecl_co_sum
= gfc_build_library_function_decl_with_spec (
4125 get_identifier (PREFIX("caf_co_sum")), ". w . w w . ",
4126 void_type_node
, 5, pvoid_type_node
, integer_type_node
,
4127 pint_type
, pchar_type_node
, size_type_node
);
4129 gfor_fndecl_caf_is_present
= gfc_build_library_function_decl_with_spec (
4130 get_identifier (PREFIX("caf_is_present")), ". r . r ",
4131 integer_type_node
, 3, pvoid_type_node
, integer_type_node
,
4134 gfor_fndecl_caf_random_init
= gfc_build_library_function_decl (
4135 get_identifier (PREFIX("caf_random_init")),
4136 void_type_node
, 2, logical_type_node
, logical_type_node
);
4139 gfc_build_intrinsic_function_decls ();
4140 gfc_build_intrinsic_lib_fndecls ();
4141 gfc_build_io_library_fndecls ();
4145 /* Evaluate the length of dummy character variables. */
4148 gfc_trans_dummy_character (gfc_symbol
*sym
, gfc_charlen
*cl
,
4149 gfc_wrapped_block
*block
)
4153 gfc_finish_decl (cl
->backend_decl
);
4155 gfc_start_block (&init
);
4157 /* Evaluate the string length expression. */
4158 gfc_conv_string_length (cl
, NULL
, &init
);
4160 gfc_trans_vla_type_sizes (sym
, &init
);
4162 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
4166 /* Allocate and cleanup an automatic character variable. */
4169 gfc_trans_auto_character_variable (gfc_symbol
* sym
, gfc_wrapped_block
* block
)
4175 gcc_assert (sym
->backend_decl
);
4176 gcc_assert (sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
);
4178 gfc_init_block (&init
);
4180 /* Evaluate the string length expression. */
4181 gfc_conv_string_length (sym
->ts
.u
.cl
, NULL
, &init
);
4183 gfc_trans_vla_type_sizes (sym
, &init
);
4185 decl
= sym
->backend_decl
;
4187 /* Emit a DECL_EXPR for this variable, which will cause the
4188 gimplifier to allocate storage, and all that good stuff. */
4189 tmp
= fold_build1_loc (input_location
, DECL_EXPR
, TREE_TYPE (decl
), decl
);
4190 gfc_add_expr_to_block (&init
, tmp
);
4192 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
4195 /* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
4198 gfc_trans_assign_aux_var (gfc_symbol
* sym
, gfc_wrapped_block
* block
)
4202 gcc_assert (sym
->backend_decl
);
4203 gfc_start_block (&init
);
4205 /* Set the initial value to length. See the comments in
4206 function gfc_add_assign_aux_vars in this file. */
4207 gfc_add_modify (&init
, GFC_DECL_STRING_LEN (sym
->backend_decl
),
4208 build_int_cst (gfc_charlen_type_node
, -2));
4210 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
4214 gfc_trans_vla_one_sizepos (tree
*tp
, stmtblock_t
*body
)
4216 tree t
= *tp
, var
, val
;
4218 if (t
== NULL
|| t
== error_mark_node
)
4220 if (TREE_CONSTANT (t
) || DECL_P (t
))
4223 if (TREE_CODE (t
) == SAVE_EXPR
)
4225 if (SAVE_EXPR_RESOLVED_P (t
))
4227 *tp
= TREE_OPERAND (t
, 0);
4230 val
= TREE_OPERAND (t
, 0);
4235 var
= gfc_create_var_np (TREE_TYPE (t
), NULL
);
4236 gfc_add_decl_to_function (var
);
4237 gfc_add_modify (body
, var
, unshare_expr (val
));
4238 if (TREE_CODE (t
) == SAVE_EXPR
)
4239 TREE_OPERAND (t
, 0) = var
;
4244 gfc_trans_vla_type_sizes_1 (tree type
, stmtblock_t
*body
)
4248 if (type
== NULL
|| type
== error_mark_node
)
4251 type
= TYPE_MAIN_VARIANT (type
);
4253 if (TREE_CODE (type
) == INTEGER_TYPE
)
4255 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type
), body
);
4256 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type
), body
);
4258 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
4260 TYPE_MIN_VALUE (t
) = TYPE_MIN_VALUE (type
);
4261 TYPE_MAX_VALUE (t
) = TYPE_MAX_VALUE (type
);
4264 else if (TREE_CODE (type
) == ARRAY_TYPE
)
4266 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type
), body
);
4267 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type
), body
);
4268 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type
), body
);
4269 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type
), body
);
4271 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
4273 TYPE_SIZE (t
) = TYPE_SIZE (type
);
4274 TYPE_SIZE_UNIT (t
) = TYPE_SIZE_UNIT (type
);
4279 /* Make sure all type sizes and array domains are either constant,
4280 or variable or parameter decls. This is a simplified variant
4281 of gimplify_type_sizes, but we can't use it here, as none of the
4282 variables in the expressions have been gimplified yet.
4283 As type sizes and domains for various variable length arrays
4284 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
4285 time, without this routine gimplify_type_sizes in the middle-end
4286 could result in the type sizes being gimplified earlier than where
4287 those variables are initialized. */
4290 gfc_trans_vla_type_sizes (gfc_symbol
*sym
, stmtblock_t
*body
)
4292 tree type
= TREE_TYPE (sym
->backend_decl
);
4294 if (TREE_CODE (type
) == FUNCTION_TYPE
4295 && (sym
->attr
.function
|| sym
->attr
.result
|| sym
->attr
.entry
))
4297 if (! current_fake_result_decl
)
4300 type
= TREE_TYPE (TREE_VALUE (current_fake_result_decl
));
4303 while (POINTER_TYPE_P (type
))
4304 type
= TREE_TYPE (type
);
4306 if (GFC_DESCRIPTOR_TYPE_P (type
))
4308 tree etype
= GFC_TYPE_ARRAY_DATAPTR_TYPE (type
);
4310 while (POINTER_TYPE_P (etype
))
4311 etype
= TREE_TYPE (etype
);
4313 gfc_trans_vla_type_sizes_1 (etype
, body
);
4316 gfc_trans_vla_type_sizes_1 (type
, body
);
4320 /* Initialize a derived type by building an lvalue from the symbol
4321 and using trans_assignment to do the work. Set dealloc to false
4322 if no deallocation prior the assignment is needed. */
4324 gfc_init_default_dt (gfc_symbol
* sym
, stmtblock_t
* block
, bool dealloc
)
4332 /* Initialization of PDTs is done elsewhere. */
4333 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.pdt_type
)
4336 gcc_assert (!sym
->attr
.allocatable
);
4337 gfc_set_sym_referenced (sym
);
4338 e
= gfc_lval_expr_from_sym (sym
);
4339 tmp
= gfc_trans_assignment (e
, sym
->value
, false, dealloc
);
4340 if (sym
->attr
.dummy
&& (sym
->attr
.optional
4341 || sym
->ns
->proc_name
->attr
.entry_master
))
4343 present
= gfc_conv_expr_present (sym
);
4344 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
), present
,
4345 tmp
, build_empty_stmt (input_location
));
4347 gfc_add_expr_to_block (block
, tmp
);
4352 /* Initialize INTENT(OUT) derived type dummies. As well as giving
4353 them their default initializer, if they have allocatable
4354 components, they have their allocatable components deallocated. */
4357 init_intent_out_dt (gfc_symbol
* proc_sym
, gfc_wrapped_block
* block
)
4360 gfc_formal_arglist
*f
;
4364 bool dealloc_with_value
= false;
4366 gfc_init_block (&init
);
4367 for (f
= gfc_sym_get_dummy_args (proc_sym
); f
; f
= f
->next
)
4368 if (f
->sym
&& f
->sym
->attr
.intent
== INTENT_OUT
4369 && !f
->sym
->attr
.pointer
4370 && f
->sym
->ts
.type
== BT_DERIVED
)
4375 /* Note: Allocatables are excluded as they are already handled
4377 if (!f
->sym
->attr
.allocatable
4378 && gfc_is_finalizable (s
->ts
.u
.derived
, NULL
))
4383 gfc_init_block (&block
);
4384 s
->attr
.referenced
= 1;
4385 e
= gfc_lval_expr_from_sym (s
);
4386 gfc_add_finalizer_call (&block
, e
);
4388 tmp
= gfc_finish_block (&block
);
4391 /* Note: Allocatables are excluded as they are already handled
4393 if (tmp
== NULL_TREE
&& !s
->attr
.allocatable
4394 && s
->ts
.u
.derived
->attr
.alloc_comp
)
4396 tmp
= gfc_deallocate_alloc_comp (s
->ts
.u
.derived
,
4398 s
->as
? s
->as
->rank
: 0);
4399 dealloc_with_value
= s
->value
;
4402 if (tmp
!= NULL_TREE
&& (s
->attr
.optional
4403 || s
->ns
->proc_name
->attr
.entry_master
))
4405 present
= gfc_conv_expr_present (s
);
4406 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
),
4407 present
, tmp
, build_empty_stmt (input_location
));
4410 if (tmp
!= NULL_TREE
&& !dealloc_with_value
)
4411 gfc_add_expr_to_block (&init
, tmp
);
4412 else if (s
->value
&& !s
->attr
.allocatable
)
4414 gfc_add_expr_to_block (&init
, tmp
);
4415 gfc_init_default_dt (s
, &init
, false);
4416 dealloc_with_value
= false;
4419 else if (f
->sym
&& f
->sym
->attr
.intent
== INTENT_OUT
4420 && f
->sym
->ts
.type
== BT_CLASS
4421 && !CLASS_DATA (f
->sym
)->attr
.class_pointer
4422 && !CLASS_DATA (f
->sym
)->attr
.allocatable
)
4427 gfc_init_block (&block
);
4428 f
->sym
->attr
.referenced
= 1;
4429 e
= gfc_lval_expr_from_sym (f
->sym
);
4430 gfc_add_finalizer_call (&block
, e
);
4432 tmp
= gfc_finish_block (&block
);
4434 if (f
->sym
->attr
.optional
|| f
->sym
->ns
->proc_name
->attr
.entry_master
)
4436 present
= gfc_conv_expr_present (f
->sym
);
4437 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
),
4439 build_empty_stmt (input_location
));
4441 gfc_add_expr_to_block (&init
, tmp
);
4443 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
4447 /* Helper function to manage deferred string lengths. */
4450 gfc_null_and_pass_deferred_len (gfc_symbol
*sym
, stmtblock_t
*init
,
4455 /* Character length passed by reference. */
4456 tmp
= sym
->ts
.u
.cl
->passed_length
;
4457 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
4458 tmp
= fold_convert (gfc_charlen_type_node
, tmp
);
4460 if (!sym
->attr
.dummy
|| sym
->attr
.intent
== INTENT_OUT
)
4461 /* Zero the string length when entering the scope. */
4462 gfc_add_modify (init
, sym
->ts
.u
.cl
->backend_decl
,
4463 build_int_cst (gfc_charlen_type_node
, 0));
4468 tmp2
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4469 gfc_charlen_type_node
,
4470 sym
->ts
.u
.cl
->backend_decl
, tmp
);
4471 if (sym
->attr
.optional
)
4473 tree present
= gfc_conv_expr_present (sym
);
4474 tmp2
= build3_loc (input_location
, COND_EXPR
,
4475 void_type_node
, present
, tmp2
,
4476 build_empty_stmt (input_location
));
4478 gfc_add_expr_to_block (init
, tmp2
);
4481 gfc_restore_backend_locus (loc
);
4483 /* Pass the final character length back. */
4484 if (sym
->attr
.intent
!= INTENT_IN
)
4486 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4487 gfc_charlen_type_node
, tmp
,
4488 sym
->ts
.u
.cl
->backend_decl
);
4489 if (sym
->attr
.optional
)
4491 tree present
= gfc_conv_expr_present (sym
);
4492 tmp
= build3_loc (input_location
, COND_EXPR
,
4493 void_type_node
, present
, tmp
,
4494 build_empty_stmt (input_location
));
4504 /* Get the result expression for a procedure. */
4507 get_proc_result (gfc_symbol
* sym
)
4509 if (sym
->attr
.subroutine
|| sym
== sym
->result
)
4511 if (current_fake_result_decl
!= NULL
)
4512 return TREE_VALUE (current_fake_result_decl
);
4517 return sym
->result
->backend_decl
;
4521 /* Generate function entry and exit code, and add it to the function body.
4523 Allocation and initialization of array variables.
4524 Allocation of character string variables.
4525 Initialization and possibly repacking of dummy arrays.
4526 Initialization of ASSIGN statement auxiliary variable.
4527 Initialization of ASSOCIATE names.
4528 Automatic deallocation. */
4531 gfc_trans_deferred_vars (gfc_symbol
* proc_sym
, gfc_wrapped_block
* block
)
4535 gfc_formal_arglist
*f
;
4536 stmtblock_t tmpblock
;
4537 bool seen_trans_deferred_array
= false;
4538 bool is_pdt_type
= false;
4544 /* Deal with implicit return variables. Explicit return variables will
4545 already have been added. */
4546 if (gfc_return_by_reference (proc_sym
) && proc_sym
->result
== proc_sym
)
4548 if (!current_fake_result_decl
)
4550 gfc_entry_list
*el
= NULL
;
4551 if (proc_sym
->attr
.entry_master
)
4553 for (el
= proc_sym
->ns
->entries
; el
; el
= el
->next
)
4554 if (el
->sym
!= el
->sym
->result
)
4557 /* TODO: move to the appropriate place in resolve.cc. */
4558 if (warn_return_type
> 0 && el
== NULL
)
4559 gfc_warning (OPT_Wreturn_type
,
4560 "Return value of function %qs at %L not set",
4561 proc_sym
->name
, &proc_sym
->declared_at
);
4563 else if (proc_sym
->as
)
4565 tree result
= TREE_VALUE (current_fake_result_decl
);
4566 gfc_save_backend_locus (&loc
);
4567 gfc_set_backend_locus (&proc_sym
->declared_at
);
4568 gfc_trans_dummy_array_bias (proc_sym
, result
, block
);
4570 /* An automatic character length, pointer array result. */
4571 if (proc_sym
->ts
.type
== BT_CHARACTER
4572 && VAR_P (proc_sym
->ts
.u
.cl
->backend_decl
))
4575 if (proc_sym
->ts
.deferred
)
4577 gfc_start_block (&init
);
4578 tmp
= gfc_null_and_pass_deferred_len (proc_sym
, &init
, &loc
);
4579 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
4582 gfc_trans_dummy_character (proc_sym
, proc_sym
->ts
.u
.cl
, block
);
4585 else if (proc_sym
->ts
.type
== BT_CHARACTER
)
4587 if (proc_sym
->ts
.deferred
)
4590 gfc_save_backend_locus (&loc
);
4591 gfc_set_backend_locus (&proc_sym
->declared_at
);
4592 gfc_start_block (&init
);
4593 /* Zero the string length on entry. */
4594 gfc_add_modify (&init
, proc_sym
->ts
.u
.cl
->backend_decl
,
4595 build_int_cst (gfc_charlen_type_node
, 0));
4596 /* Null the pointer. */
4597 e
= gfc_lval_expr_from_sym (proc_sym
);
4598 gfc_init_se (&se
, NULL
);
4599 se
.want_pointer
= 1;
4600 gfc_conv_expr (&se
, e
);
4603 gfc_add_modify (&init
, tmp
,
4604 fold_convert (TREE_TYPE (se
.expr
),
4605 null_pointer_node
));
4606 gfc_restore_backend_locus (&loc
);
4608 /* Pass back the string length on exit. */
4609 tmp
= proc_sym
->ts
.u
.cl
->backend_decl
;
4610 if (TREE_CODE (tmp
) != INDIRECT_REF
4611 && proc_sym
->ts
.u
.cl
->passed_length
)
4613 tmp
= proc_sym
->ts
.u
.cl
->passed_length
;
4614 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
4615 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4616 TREE_TYPE (tmp
), tmp
,
4619 proc_sym
->ts
.u
.cl
->backend_decl
));
4624 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
4626 else if (VAR_P (proc_sym
->ts
.u
.cl
->backend_decl
))
4627 gfc_trans_dummy_character (proc_sym
, proc_sym
->ts
.u
.cl
, block
);
4630 gcc_assert (flag_f2c
&& proc_sym
->ts
.type
== BT_COMPLEX
);
4632 else if (proc_sym
== proc_sym
->result
&& IS_CLASS_ARRAY (proc_sym
))
4634 /* Nullify explicit return class arrays on entry. */
4636 tmp
= get_proc_result (proc_sym
);
4637 if (tmp
&& GFC_CLASS_TYPE_P (TREE_TYPE (tmp
)))
4639 gfc_start_block (&init
);
4640 tmp
= gfc_class_data_get (tmp
);
4641 type
= TREE_TYPE (gfc_conv_descriptor_data_get (tmp
));
4642 gfc_conv_descriptor_data_set (&init
, tmp
, build_int_cst (type
, 0));
4643 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
4648 /* Initialize the INTENT(OUT) derived type dummy arguments. This
4649 should be done here so that the offsets and lbounds of arrays
4651 gfc_save_backend_locus (&loc
);
4652 gfc_set_backend_locus (&proc_sym
->declared_at
);
4653 init_intent_out_dt (proc_sym
, block
);
4654 gfc_restore_backend_locus (&loc
);
4656 /* For some reasons, internal procedures point to the parent's
4657 namespace. Top-level procedure and variables inside BLOCK are fine. */
4658 gfc_namespace
*omp_ns
= proc_sym
->ns
;
4659 if (proc_sym
->ns
->proc_name
!= proc_sym
)
4660 for (omp_ns
= proc_sym
->ns
->contained
; omp_ns
;
4661 omp_ns
= omp_ns
->sibling
)
4662 if (omp_ns
->proc_name
== proc_sym
)
4665 /* Add 'omp allocate' attribute for gfc_trans_auto_array_allocation and
4666 unset attr.omp_allocate for 'omp allocate allocator(omp_default_mem_alloc),
4667 which has the normal codepath except for an invalid-use check in the ME.
4668 The main processing happens later in this function. */
4669 for (struct gfc_omp_namelist
*n
= omp_ns
? omp_ns
->omp_allocate
: NULL
;
4671 if (!TREE_STATIC (n
->sym
->backend_decl
))
4673 /* Add empty entries - described and to be filled below. */
4674 tree tmp
= build_tree_list (NULL_TREE
, NULL_TREE
);
4675 TREE_CHAIN (tmp
) = build_tree_list (NULL_TREE
, NULL_TREE
);
4676 DECL_ATTRIBUTES (n
->sym
->backend_decl
)
4677 = tree_cons (get_identifier ("omp allocate"), tmp
,
4678 DECL_ATTRIBUTES (n
->sym
->backend_decl
));
4679 if (n
->u
.align
== NULL
4680 && n
->u2
.allocator
!= NULL
4681 && n
->u2
.allocator
->expr_type
== EXPR_CONSTANT
4682 && mpz_cmp_si (n
->u2
.allocator
->value
.integer
, 1) == 0)
4683 n
->sym
->attr
.omp_allocate
= 0;
4686 for (sym
= proc_sym
->tlink
; sym
!= proc_sym
; sym
= sym
->tlink
)
4688 bool alloc_comp_or_fini
= (sym
->ts
.type
== BT_DERIVED
)
4689 && (sym
->ts
.u
.derived
->attr
.alloc_comp
4690 || gfc_is_finalizable (sym
->ts
.u
.derived
,
4695 /* Set the vptr of unlimited polymorphic pointer variables so that
4696 they do not cause segfaults in select type, when the selector
4697 is an intrinsic type. */
4698 if (sym
->ts
.type
== BT_CLASS
&& UNLIMITED_POLY (sym
)
4699 && sym
->attr
.flavor
== FL_VARIABLE
&& !sym
->assoc
4700 && !sym
->attr
.dummy
&& CLASS_DATA (sym
)->attr
.class_pointer
)
4703 gfc_init_block (&tmpblock
);
4704 vtab
= gfc_find_vtab (&sym
->ts
);
4705 if (!vtab
->backend_decl
)
4707 if (!vtab
->attr
.referenced
)
4708 gfc_set_sym_referenced (vtab
);
4709 gfc_get_symbol_decl (vtab
);
4711 tmp
= gfc_class_vptr_get (sym
->backend_decl
);
4712 gfc_add_modify (&tmpblock
, tmp
,
4713 gfc_build_addr_expr (TREE_TYPE (tmp
),
4714 vtab
->backend_decl
));
4715 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
), NULL
);
4718 if (sym
->ts
.type
== BT_DERIVED
4719 && sym
->ts
.u
.derived
4720 && sym
->ts
.u
.derived
->attr
.pdt_type
)
4723 gfc_init_block (&tmpblock
);
4724 if (!(sym
->attr
.dummy
4725 || sym
->attr
.pointer
4726 || sym
->attr
.allocatable
))
4728 tmp
= gfc_allocate_pdt_comp (sym
->ts
.u
.derived
,
4730 sym
->as
? sym
->as
->rank
: 0,
4732 gfc_add_expr_to_block (&tmpblock
, tmp
);
4733 if (!sym
->attr
.result
)
4734 tmp
= gfc_deallocate_pdt_comp (sym
->ts
.u
.derived
,
4736 sym
->as
? sym
->as
->rank
: 0);
4739 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
), tmp
);
4741 else if (sym
->attr
.dummy
)
4743 tmp
= gfc_check_pdt_dummy (sym
->ts
.u
.derived
,
4745 sym
->as
? sym
->as
->rank
: 0,
4747 gfc_add_expr_to_block (&tmpblock
, tmp
);
4748 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
), NULL
);
4751 else if (sym
->ts
.type
== BT_CLASS
4752 && CLASS_DATA (sym
)->ts
.u
.derived
4753 && CLASS_DATA (sym
)->ts
.u
.derived
->attr
.pdt_type
)
4755 gfc_component
*data
= CLASS_DATA (sym
);
4757 gfc_init_block (&tmpblock
);
4758 if (!(sym
->attr
.dummy
4759 || CLASS_DATA (sym
)->attr
.pointer
4760 || CLASS_DATA (sym
)->attr
.allocatable
))
4762 tmp
= gfc_class_data_get (sym
->backend_decl
);
4763 tmp
= gfc_allocate_pdt_comp (data
->ts
.u
.derived
, tmp
,
4764 data
->as
? data
->as
->rank
: 0,
4766 gfc_add_expr_to_block (&tmpblock
, tmp
);
4767 tmp
= gfc_class_data_get (sym
->backend_decl
);
4768 if (!sym
->attr
.result
)
4769 tmp
= gfc_deallocate_pdt_comp (data
->ts
.u
.derived
, tmp
,
4770 data
->as
? data
->as
->rank
: 0);
4773 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
), tmp
);
4775 else if (sym
->attr
.dummy
)
4777 tmp
= gfc_class_data_get (sym
->backend_decl
);
4778 tmp
= gfc_check_pdt_dummy (data
->ts
.u
.derived
, tmp
,
4779 data
->as
? data
->as
->rank
: 0,
4781 gfc_add_expr_to_block (&tmpblock
, tmp
);
4782 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
), NULL
);
4786 if (sym
->attr
.pointer
&& sym
->attr
.dimension
4787 && sym
->attr
.save
== SAVE_NONE
4788 && !sym
->attr
.use_assoc
4789 && !sym
->attr
.host_assoc
4791 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (sym
->backend_decl
)))
4793 gfc_init_block (&tmpblock
);
4794 gfc_conv_descriptor_span_set (&tmpblock
, sym
->backend_decl
,
4795 build_int_cst (gfc_array_index_type
, 0));
4796 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
),
4800 if (sym
->ts
.type
== BT_CLASS
4801 && (sym
->attr
.save
|| flag_max_stack_var_size
== 0)
4802 && CLASS_DATA (sym
)->attr
.allocatable
)
4806 if (UNLIMITED_POLY (sym
))
4807 vptr
= null_pointer_node
;
4811 vsym
= gfc_find_derived_vtab (sym
->ts
.u
.derived
);
4812 vptr
= gfc_get_symbol_decl (vsym
);
4813 vptr
= gfc_build_addr_expr (NULL
, vptr
);
4816 if (CLASS_DATA (sym
)->attr
.dimension
4817 || (CLASS_DATA (sym
)->attr
.codimension
4818 && flag_coarray
!= GFC_FCOARRAY_LIB
))
4820 tmp
= gfc_class_data_get (sym
->backend_decl
);
4821 tmp
= gfc_build_null_descriptor (TREE_TYPE (tmp
));
4824 tmp
= null_pointer_node
;
4826 DECL_INITIAL (sym
->backend_decl
)
4827 = gfc_class_set_static_fields (sym
->backend_decl
, vptr
, tmp
);
4828 TREE_CONSTANT (DECL_INITIAL (sym
->backend_decl
)) = 1;
4830 else if ((sym
->attr
.dimension
|| sym
->attr
.codimension
4831 || (IS_CLASS_ARRAY (sym
) && !CLASS_DATA (sym
)->attr
.allocatable
)))
4833 bool is_classarray
= IS_CLASS_ARRAY (sym
);
4834 symbol_attribute
*array_attr
;
4836 array_type type_of_array
;
4838 array_attr
= is_classarray
? &CLASS_DATA (sym
)->attr
: &sym
->attr
;
4839 as
= is_classarray
? CLASS_DATA (sym
)->as
: sym
->as
;
4840 /* Assumed-size Cray pointees need to be treated as AS_EXPLICIT. */
4841 type_of_array
= as
->type
;
4842 if (type_of_array
== AS_ASSUMED_SIZE
&& as
->cp_was_assumed
)
4843 type_of_array
= AS_EXPLICIT
;
4844 switch (type_of_array
)
4847 if (sym
->attr
.dummy
|| sym
->attr
.result
)
4848 gfc_trans_dummy_array_bias (sym
, sym
->backend_decl
, block
);
4849 /* Allocatable and pointer arrays need to processed
4851 else if ((sym
->ts
.type
!= BT_CLASS
&& sym
->attr
.pointer
)
4852 || (sym
->ts
.type
== BT_CLASS
4853 && CLASS_DATA (sym
)->attr
.class_pointer
)
4854 || array_attr
->allocatable
)
4856 if (TREE_STATIC (sym
->backend_decl
))
4858 gfc_save_backend_locus (&loc
);
4859 gfc_set_backend_locus (&sym
->declared_at
);
4860 gfc_trans_static_array_pointer (sym
);
4861 gfc_restore_backend_locus (&loc
);
4865 seen_trans_deferred_array
= true;
4866 gfc_trans_deferred_array (sym
, block
);
4869 else if (sym
->attr
.codimension
4870 && TREE_STATIC (sym
->backend_decl
))
4872 gfc_init_block (&tmpblock
);
4873 gfc_trans_array_cobounds (TREE_TYPE (sym
->backend_decl
),
4875 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
),
4881 gfc_save_backend_locus (&loc
);
4882 gfc_set_backend_locus (&sym
->declared_at
);
4884 if (alloc_comp_or_fini
)
4886 seen_trans_deferred_array
= true;
4887 gfc_trans_deferred_array (sym
, block
);
4889 else if (sym
->ts
.type
== BT_DERIVED
4892 && sym
->attr
.save
== SAVE_NONE
)
4894 gfc_start_block (&tmpblock
);
4895 gfc_init_default_dt (sym
, &tmpblock
, false);
4896 gfc_add_init_cleanup (block
,
4897 gfc_finish_block (&tmpblock
),
4901 gfc_trans_auto_array_allocation (sym
->backend_decl
,
4903 gfc_restore_backend_locus (&loc
);
4907 case AS_ASSUMED_SIZE
:
4908 /* Must be a dummy parameter. */
4909 gcc_assert (sym
->attr
.dummy
|| as
->cp_was_assumed
);
4911 /* We should always pass assumed size arrays the g77 way. */
4912 if (sym
->attr
.dummy
)
4913 gfc_trans_g77_array (sym
, block
);
4916 case AS_ASSUMED_SHAPE
:
4917 /* Must be a dummy parameter. */
4918 gcc_assert (sym
->attr
.dummy
);
4920 gfc_trans_dummy_array_bias (sym
, sym
->backend_decl
, block
);
4923 case AS_ASSUMED_RANK
:
4925 seen_trans_deferred_array
= true;
4926 gfc_trans_deferred_array (sym
, block
);
4927 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.deferred
4928 && sym
->attr
.result
)
4930 gfc_start_block (&init
);
4931 gfc_save_backend_locus (&loc
);
4932 gfc_set_backend_locus (&sym
->declared_at
);
4933 tmp
= gfc_null_and_pass_deferred_len (sym
, &init
, &loc
);
4934 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
4941 if (alloc_comp_or_fini
&& !seen_trans_deferred_array
)
4942 gfc_trans_deferred_array (sym
, block
);
4944 else if ((!sym
->attr
.dummy
|| sym
->ts
.deferred
)
4945 && (sym
->ts
.type
== BT_CLASS
4946 && CLASS_DATA (sym
)->attr
.class_pointer
))
4947 gfc_trans_class_array (sym
, block
);
4948 else if ((!sym
->attr
.dummy
|| sym
->ts
.deferred
)
4949 && (sym
->attr
.allocatable
4950 || (sym
->attr
.pointer
&& sym
->attr
.result
)
4951 || (sym
->ts
.type
== BT_CLASS
4952 && CLASS_DATA (sym
)->attr
.allocatable
)))
4954 if (!sym
->attr
.save
&& flag_max_stack_var_size
!= 0)
4956 tree descriptor
= NULL_TREE
;
4958 gfc_save_backend_locus (&loc
);
4959 gfc_set_backend_locus (&sym
->declared_at
);
4960 gfc_start_block (&init
);
4962 if (sym
->ts
.type
== BT_CHARACTER
4963 && sym
->attr
.allocatable
4964 && !sym
->attr
.dimension
4965 && sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
4966 && sym
->ts
.u
.cl
->length
->expr_type
== EXPR_VARIABLE
)
4967 gfc_conv_string_length (sym
->ts
.u
.cl
, NULL
, &init
);
4969 if (!sym
->attr
.pointer
)
4971 /* Nullify and automatic deallocation of allocatable
4973 e
= gfc_lval_expr_from_sym (sym
);
4974 if (sym
->ts
.type
== BT_CLASS
)
4975 gfc_add_data_component (e
);
4977 gfc_init_se (&se
, NULL
);
4978 if (sym
->ts
.type
!= BT_CLASS
4979 || sym
->ts
.u
.derived
->attr
.dimension
4980 || sym
->ts
.u
.derived
->attr
.codimension
)
4982 se
.want_pointer
= 1;
4983 gfc_conv_expr (&se
, e
);
4985 else if (sym
->ts
.type
== BT_CLASS
4986 && !CLASS_DATA (sym
)->attr
.dimension
4987 && !CLASS_DATA (sym
)->attr
.codimension
)
4989 se
.want_pointer
= 1;
4990 gfc_conv_expr (&se
, e
);
4994 se
.descriptor_only
= 1;
4995 gfc_conv_expr (&se
, e
);
4996 descriptor
= se
.expr
;
4997 se
.expr
= gfc_conv_descriptor_data_addr (se
.expr
);
4998 se
.expr
= build_fold_indirect_ref_loc (input_location
, se
.expr
);
5002 if (!sym
->attr
.dummy
|| sym
->attr
.intent
== INTENT_OUT
)
5004 /* Nullify when entering the scope. */
5005 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
5006 TREE_TYPE (se
.expr
), se
.expr
,
5007 fold_convert (TREE_TYPE (se
.expr
),
5008 null_pointer_node
));
5009 if (sym
->attr
.optional
)
5011 tree present
= gfc_conv_expr_present (sym
);
5012 tmp
= build3_loc (input_location
, COND_EXPR
,
5013 void_type_node
, present
, tmp
,
5014 build_empty_stmt (input_location
));
5016 gfc_add_expr_to_block (&init
, tmp
);
5020 if ((sym
->attr
.dummy
|| sym
->attr
.result
)
5021 && sym
->ts
.type
== BT_CHARACTER
5023 && sym
->ts
.u
.cl
->passed_length
)
5024 tmp
= gfc_null_and_pass_deferred_len (sym
, &init
, &loc
);
5027 gfc_restore_backend_locus (&loc
);
5031 /* Initialize descriptor's TKR information. */
5032 if (sym
->ts
.type
== BT_CLASS
)
5033 gfc_trans_class_array (sym
, block
);
5035 /* Deallocate when leaving the scope. Nullifying is not
5037 if (!sym
->attr
.result
&& !sym
->attr
.dummy
&& !sym
->attr
.pointer
5038 && !sym
->ns
->proc_name
->attr
.is_main_program
)
5040 if (sym
->ts
.type
== BT_CLASS
5041 && CLASS_DATA (sym
)->attr
.codimension
)
5042 tmp
= gfc_deallocate_with_status (descriptor
, NULL_TREE
,
5043 NULL_TREE
, NULL_TREE
,
5044 NULL_TREE
, true, NULL
,
5045 GFC_CAF_COARRAY_ANALYZE
);
5048 gfc_expr
*expr
= gfc_lval_expr_from_sym (sym
);
5049 tmp
= gfc_deallocate_scalar_with_status (se
.expr
,
5054 gfc_free_expr (expr
);
5058 if (sym
->ts
.type
== BT_CLASS
)
5060 /* Initialize _vptr to declared type. */
5064 gfc_save_backend_locus (&loc
);
5065 gfc_set_backend_locus (&sym
->declared_at
);
5066 e
= gfc_lval_expr_from_sym (sym
);
5067 gfc_add_vptr_component (e
);
5068 gfc_init_se (&se
, NULL
);
5069 se
.want_pointer
= 1;
5070 gfc_conv_expr (&se
, e
);
5072 if (UNLIMITED_POLY (sym
))
5073 rhs
= build_int_cst (TREE_TYPE (se
.expr
), 0);
5076 vtab
= gfc_find_derived_vtab (sym
->ts
.u
.derived
);
5077 rhs
= gfc_build_addr_expr (TREE_TYPE (se
.expr
),
5078 gfc_get_symbol_decl (vtab
));
5080 gfc_add_modify (&init
, se
.expr
, rhs
);
5081 gfc_restore_backend_locus (&loc
);
5084 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
5087 else if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.deferred
)
5092 /* If we get to here, all that should be left are pointers. */
5093 gcc_assert (sym
->attr
.pointer
);
5095 if (sym
->attr
.dummy
)
5097 gfc_start_block (&init
);
5098 gfc_save_backend_locus (&loc
);
5099 gfc_set_backend_locus (&sym
->declared_at
);
5100 tmp
= gfc_null_and_pass_deferred_len (sym
, &init
, &loc
);
5101 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
5104 else if (sym
->ts
.deferred
)
5105 gfc_fatal_error ("Deferred type parameter not yet supported");
5106 else if (alloc_comp_or_fini
)
5107 gfc_trans_deferred_array (sym
, block
);
5108 else if (sym
->ts
.type
== BT_CHARACTER
)
5110 gfc_save_backend_locus (&loc
);
5111 gfc_set_backend_locus (&sym
->declared_at
);
5112 if (sym
->attr
.dummy
|| sym
->attr
.result
)
5113 gfc_trans_dummy_character (sym
, sym
->ts
.u
.cl
, block
);
5115 gfc_trans_auto_character_variable (sym
, block
);
5116 gfc_restore_backend_locus (&loc
);
5118 else if (sym
->attr
.assign
)
5120 gfc_save_backend_locus (&loc
);
5121 gfc_set_backend_locus (&sym
->declared_at
);
5122 gfc_trans_assign_aux_var (sym
, block
);
5123 gfc_restore_backend_locus (&loc
);
5125 else if (sym
->ts
.type
== BT_DERIVED
5128 && sym
->attr
.save
== SAVE_NONE
)
5130 gfc_start_block (&tmpblock
);
5131 gfc_init_default_dt (sym
, &tmpblock
, false);
5132 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
),
5135 else if (!(UNLIMITED_POLY(sym
)) && !is_pdt_type
)
5139 /* Handle 'omp allocate'. This has to be after the block above as
5140 gfc_add_init_cleanup (..., init, ...) puts 'init' of later calls
5141 before earlier calls. The code is a bit more complex as gfortran does
5142 not really work with bind expressions / BIND_EXPR_VARS properly, i.e.
5143 gimplify_bind_expr needs some help for placing the GOMP_alloc. Thus,
5144 we pass on the location of the allocate-assignment expression and,
5145 if the size is not constant, the size variable if Fortran computes this
5146 differently. We also might add an expression location after which the
5147 code has to be added, e.g. for character len expressions, which affect
5149 gfc_expr
*last_allocator
= NULL
;
5150 if (omp_ns
&& omp_ns
->omp_allocate
)
5152 if (!block
->init
|| TREE_CODE (block
->init
) != STATEMENT_LIST
)
5154 tree tmp
= build1_v (LABEL_EXPR
, gfc_build_label_decl (NULL_TREE
));
5155 append_to_statement_list (tmp
, &block
->init
);
5157 if (!block
->cleanup
|| TREE_CODE (block
->cleanup
) != STATEMENT_LIST
)
5159 tree tmp
= build1_v (LABEL_EXPR
, gfc_build_label_decl (NULL_TREE
));
5160 append_to_statement_list (tmp
, &block
->cleanup
);
5163 tree init_stmtlist
= block
->init
;
5164 tree cleanup_stmtlist
= block
->cleanup
;
5165 se
.expr
= NULL_TREE
;
5166 for (struct gfc_omp_namelist
*n
= omp_ns
? omp_ns
->omp_allocate
: NULL
;
5168 if (!TREE_STATIC (n
->sym
->backend_decl
))
5170 tree align
= (n
->u
.align
? gfc_conv_constant_to_tree (n
->u
.align
)
5172 if (last_allocator
!= n
->u2
.allocator
)
5174 location_t loc
= input_location
;
5175 gfc_init_se (&se
, NULL
);
5176 if (n
->u2
.allocator
)
5178 input_location
= gfc_get_location (&n
->u2
.allocator
->where
);
5179 gfc_conv_expr (&se
, n
->u2
.allocator
);
5181 /* We need to evalulate non-constants - also to find the location
5182 after which the GOMP_alloc has to be added to - also as BLOCK
5183 does not yield a new BIND_EXPR_BODY. */
5185 && (!(CONSTANT_CLASS_P (se
.expr
) && DECL_P (se
.expr
))
5186 || se
.pre
.head
|| se
.post
.head
))
5188 stmtblock_t tmpblock
;
5189 gfc_init_block (&tmpblock
);
5190 se
.expr
= gfc_evaluate_now (se
.expr
, &tmpblock
);
5191 /* First post then pre because the new code is inserted
5193 gfc_add_init_cleanup (block
, gfc_finish_block (&se
.post
), NULL
);
5194 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
),
5196 gfc_add_init_cleanup (block
, gfc_finish_block (&se
.pre
), NULL
);
5198 last_allocator
= n
->u2
.allocator
;
5199 input_location
= loc
;
5202 /* 'omp allocate( {purpose: allocator, value: align},
5203 {purpose: init-stmtlist, value: cleanup-stmtlist},
5204 {purpose: size-var, value: last-size-expr}}
5205 where init-stmt/cleanup-stmt is the STATEMENT list to find the
5206 try-final block; last-size-expr is to find the location after
5207 which to add the code and 'size-var' is for the proper size, cf.
5208 gfc_trans_auto_array_allocation - either or both of the latter
5210 tree tmp
= lookup_attribute ("omp allocate",
5211 DECL_ATTRIBUTES (n
->sym
->backend_decl
));
5212 tmp
= TREE_VALUE (tmp
);
5213 TREE_PURPOSE (tmp
) = se
.expr
;
5214 TREE_VALUE (tmp
) = align
;
5215 TREE_PURPOSE (TREE_CHAIN (tmp
)) = init_stmtlist
;
5216 TREE_VALUE (TREE_CHAIN (tmp
)) = cleanup_stmtlist
;
5218 else if (n
->sym
->attr
.in_common
)
5220 gfc_error ("Sorry, !$OMP allocate for COMMON block variable %qs at %L "
5221 "not supported", n
->sym
->common_block
->name
,
5222 &n
->sym
->common_block
->where
);
5227 gfc_error ("Sorry, !$OMP allocate for variable %qs at %L with SAVE "
5228 "attribute not yet implemented", n
->sym
->name
,
5229 &n
->sym
->declared_at
);
5230 /* FIXME: Remember to handle last_allocator. */
5234 gfc_init_block (&tmpblock
);
5236 for (f
= gfc_sym_get_dummy_args (proc_sym
); f
; f
= f
->next
)
5238 if (f
->sym
&& f
->sym
->tlink
== NULL
&& f
->sym
->ts
.type
== BT_CHARACTER
5239 && f
->sym
->ts
.u
.cl
->backend_decl
)
5241 if (TREE_CODE (f
->sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
5242 gfc_trans_vla_type_sizes (f
->sym
, &tmpblock
);
5246 if (gfc_return_by_reference (proc_sym
) && proc_sym
->ts
.type
== BT_CHARACTER
5247 && current_fake_result_decl
!= NULL
)
5249 gcc_assert (proc_sym
->ts
.u
.cl
->backend_decl
!= NULL
);
5250 if (TREE_CODE (proc_sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
5251 gfc_trans_vla_type_sizes (proc_sym
, &tmpblock
);
5254 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
), NULL_TREE
);
5258 struct module_hasher
: ggc_ptr_hash
<module_htab_entry
>
5260 typedef const char *compare_type
;
5262 static hashval_t
hash (module_htab_entry
*s
)
5264 return htab_hash_string (s
->name
);
5268 equal (module_htab_entry
*a
, const char *b
)
5270 return !strcmp (a
->name
, b
);
5274 static GTY (()) hash_table
<module_hasher
> *module_htab
;
5276 /* Hash and equality functions for module_htab's decls. */
5279 module_decl_hasher::hash (tree t
)
5281 const_tree n
= DECL_NAME (t
);
5283 n
= TYPE_NAME (TREE_TYPE (t
));
5284 return htab_hash_string (IDENTIFIER_POINTER (n
));
5288 module_decl_hasher::equal (tree t1
, const char *x2
)
5290 const_tree n1
= DECL_NAME (t1
);
5291 if (n1
== NULL_TREE
)
5292 n1
= TYPE_NAME (TREE_TYPE (t1
));
5293 return strcmp (IDENTIFIER_POINTER (n1
), x2
) == 0;
5296 struct module_htab_entry
*
5297 gfc_find_module (const char *name
)
5300 module_htab
= hash_table
<module_hasher
>::create_ggc (10);
5302 module_htab_entry
**slot
5303 = module_htab
->find_slot_with_hash (name
, htab_hash_string (name
), INSERT
);
5306 module_htab_entry
*entry
= ggc_cleared_alloc
<module_htab_entry
> ();
5308 entry
->name
= gfc_get_string ("%s", name
);
5309 entry
->decls
= hash_table
<module_decl_hasher
>::create_ggc (10);
5316 gfc_module_add_decl (struct module_htab_entry
*entry
, tree decl
)
5320 if (DECL_NAME (decl
))
5321 name
= IDENTIFIER_POINTER (DECL_NAME (decl
));
5324 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
5325 name
= IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl
)));
5328 = entry
->decls
->find_slot_with_hash (name
, htab_hash_string (name
),
5335 /* Generate debugging symbols for namelists. This function must come after
5336 generate_local_decl to ensure that the variables in the namelist are
5337 already declared. */
5340 generate_namelist_decl (gfc_symbol
* sym
)
5344 vec
<constructor_elt
, va_gc
> *nml_decls
= NULL
;
5346 gcc_assert (sym
->attr
.flavor
== FL_NAMELIST
);
5347 for (nml
= sym
->namelist
; nml
; nml
= nml
->next
)
5349 if (nml
->sym
->backend_decl
== NULL_TREE
)
5351 nml
->sym
->attr
.referenced
= 1;
5352 nml
->sym
->backend_decl
= gfc_get_symbol_decl (nml
->sym
);
5354 DECL_IGNORED_P (nml
->sym
->backend_decl
) = 0;
5355 CONSTRUCTOR_APPEND_ELT (nml_decls
, NULL_TREE
, nml
->sym
->backend_decl
);
5358 decl
= make_node (NAMELIST_DECL
);
5359 TREE_TYPE (decl
) = void_type_node
;
5360 NAMELIST_DECL_ASSOCIATED_DECL (decl
) = build_constructor (NULL_TREE
, nml_decls
);
5361 DECL_NAME (decl
) = get_identifier (sym
->name
);
5366 /* Output an initialized decl for a module variable. */
5369 gfc_create_module_variable (gfc_symbol
* sym
)
5373 /* Module functions with alternate entries are dealt with later and
5374 would get caught by the next condition. */
5375 if (sym
->attr
.entry
)
5378 /* Make sure we convert the types of the derived types from iso_c_binding
5380 if (sym
->attr
.flavor
!= FL_PROCEDURE
&& sym
->attr
.is_iso_c
5381 && sym
->ts
.type
== BT_DERIVED
)
5382 sym
->backend_decl
= gfc_typenode_for_spec (&(sym
->ts
));
5384 if (gfc_fl_struct (sym
->attr
.flavor
)
5385 && sym
->backend_decl
5386 && TREE_CODE (sym
->backend_decl
) == RECORD_TYPE
)
5388 decl
= sym
->backend_decl
;
5389 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
5391 if (!sym
->attr
.use_assoc
&& !sym
->attr
.used_in_submodule
)
5393 gcc_assert (TYPE_CONTEXT (decl
) == NULL_TREE
5394 || TYPE_CONTEXT (decl
) == sym
->ns
->proc_name
->backend_decl
);
5395 gcc_assert (DECL_CONTEXT (TYPE_STUB_DECL (decl
)) == NULL_TREE
5396 || DECL_CONTEXT (TYPE_STUB_DECL (decl
))
5397 == sym
->ns
->proc_name
->backend_decl
);
5399 TYPE_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
5400 DECL_CONTEXT (TYPE_STUB_DECL (decl
)) = sym
->ns
->proc_name
->backend_decl
;
5401 gfc_module_add_decl (cur_module
, TYPE_STUB_DECL (decl
));
5404 /* Only output variables, procedure pointers and array valued,
5405 or derived type, parameters. */
5406 if (sym
->attr
.flavor
!= FL_VARIABLE
5407 && !(sym
->attr
.flavor
== FL_PARAMETER
5408 && (sym
->attr
.dimension
|| sym
->ts
.type
== BT_DERIVED
))
5409 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.proc_pointer
))
5412 if ((sym
->attr
.in_common
|| sym
->attr
.in_equivalence
) && sym
->backend_decl
)
5414 decl
= sym
->backend_decl
;
5415 gcc_assert (DECL_FILE_SCOPE_P (decl
));
5416 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
5417 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
5418 gfc_module_add_decl (cur_module
, decl
);
5421 /* Don't generate variables from other modules. Variables from
5422 COMMONs and Cray pointees will already have been generated. */
5423 if (sym
->attr
.use_assoc
|| sym
->attr
.used_in_submodule
5424 || sym
->attr
.in_common
|| sym
->attr
.cray_pointee
)
5427 /* Equivalenced variables arrive here after creation. */
5428 if (sym
->backend_decl
5429 && (sym
->equiv_built
|| sym
->attr
.in_equivalence
))
5432 if (sym
->backend_decl
&& !sym
->attr
.vtab
&& !sym
->attr
.target
)
5433 gfc_internal_error ("backend decl for module variable %qs already exists",
5436 if (sym
->module
&& !sym
->attr
.result
&& !sym
->attr
.dummy
5437 && (sym
->attr
.access
== ACCESS_UNKNOWN
5438 && (sym
->ns
->default_access
== ACCESS_PRIVATE
5439 || (sym
->ns
->default_access
== ACCESS_UNKNOWN
5440 && flag_module_private
))))
5441 sym
->attr
.access
= ACCESS_PRIVATE
;
5443 if (warn_unused_variable
&& !sym
->attr
.referenced
5444 && sym
->attr
.access
== ACCESS_PRIVATE
)
5445 gfc_warning (OPT_Wunused_value
,
5446 "Unused PRIVATE module variable %qs declared at %L",
5447 sym
->name
, &sym
->declared_at
);
5449 /* We always want module variables to be created. */
5450 sym
->attr
.referenced
= 1;
5451 /* Create the decl. */
5452 decl
= gfc_get_symbol_decl (sym
);
5454 /* Create the variable. */
5456 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
5457 || (sym
->ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
5458 && sym
->fn_result_spec
));
5459 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
5460 rest_of_decl_compilation (decl
, 1, 0);
5461 gfc_module_add_decl (cur_module
, decl
);
5463 /* Also add length of strings. */
5464 if (sym
->ts
.type
== BT_CHARACTER
)
5468 length
= sym
->ts
.u
.cl
->backend_decl
;
5469 gcc_assert (length
|| sym
->attr
.proc_pointer
);
5470 if (length
&& !INTEGER_CST_P (length
))
5473 rest_of_decl_compilation (length
, 1, 0);
5477 if (sym
->attr
.codimension
&& !sym
->attr
.dummy
&& !sym
->attr
.allocatable
5478 && sym
->attr
.referenced
&& !sym
->attr
.use_assoc
)
5479 has_coarray_vars
= true;
5482 /* Emit debug information for USE statements. */
5485 gfc_trans_use_stmts (gfc_namespace
* ns
)
5487 gfc_use_list
*use_stmt
;
5488 for (use_stmt
= ns
->use_stmts
; use_stmt
; use_stmt
= use_stmt
->next
)
5490 struct module_htab_entry
*entry
5491 = gfc_find_module (use_stmt
->module_name
);
5492 gfc_use_rename
*rent
;
5494 if (entry
->namespace_decl
== NULL
)
5496 entry
->namespace_decl
5497 = build_decl (input_location
,
5499 get_identifier (use_stmt
->module_name
),
5501 DECL_EXTERNAL (entry
->namespace_decl
) = 1;
5503 gfc_set_backend_locus (&use_stmt
->where
);
5504 if (!use_stmt
->only_flag
)
5505 (*debug_hooks
->imported_module_or_decl
) (entry
->namespace_decl
,
5507 ns
->proc_name
->backend_decl
,
5509 for (rent
= use_stmt
->rename
; rent
; rent
= rent
->next
)
5511 tree decl
, local_name
;
5513 if (rent
->op
!= INTRINSIC_NONE
)
5516 hashval_t hash
= htab_hash_string (rent
->use_name
);
5517 tree
*slot
= entry
->decls
->find_slot_with_hash (rent
->use_name
, hash
,
5523 st
= gfc_find_symtree (ns
->sym_root
,
5525 ? rent
->local_name
: rent
->use_name
);
5527 /* The following can happen if a derived type is renamed. */
5531 name
= xstrdup (rent
->local_name
[0]
5532 ? rent
->local_name
: rent
->use_name
);
5533 name
[0] = (char) TOUPPER ((unsigned char) name
[0]);
5534 st
= gfc_find_symtree (ns
->sym_root
, name
);
5539 /* Sometimes, generic interfaces wind up being over-ruled by a
5540 local symbol (see PR41062). */
5541 if (!st
->n
.sym
->attr
.use_assoc
)
5543 *slot
= error_mark_node
;
5544 entry
->decls
->clear_slot (slot
);
5548 if (st
->n
.sym
->backend_decl
5549 && DECL_P (st
->n
.sym
->backend_decl
)
5550 && st
->n
.sym
->module
5551 && strcmp (st
->n
.sym
->module
, use_stmt
->module_name
) == 0)
5553 gcc_assert (DECL_EXTERNAL (entry
->namespace_decl
)
5554 || !VAR_P (st
->n
.sym
->backend_decl
));
5555 decl
= copy_node (st
->n
.sym
->backend_decl
);
5556 DECL_CONTEXT (decl
) = entry
->namespace_decl
;
5557 DECL_EXTERNAL (decl
) = 1;
5558 DECL_IGNORED_P (decl
) = 0;
5559 DECL_INITIAL (decl
) = NULL_TREE
;
5561 else if (st
->n
.sym
->attr
.flavor
== FL_NAMELIST
5562 && st
->n
.sym
->attr
.use_only
5563 && st
->n
.sym
->module
5564 && strcmp (st
->n
.sym
->module
, use_stmt
->module_name
)
5567 decl
= generate_namelist_decl (st
->n
.sym
);
5568 DECL_CONTEXT (decl
) = entry
->namespace_decl
;
5569 DECL_EXTERNAL (decl
) = 1;
5570 DECL_IGNORED_P (decl
) = 0;
5571 DECL_INITIAL (decl
) = NULL_TREE
;
5575 *slot
= error_mark_node
;
5576 entry
->decls
->clear_slot (slot
);
5581 decl
= (tree
) *slot
;
5582 if (rent
->local_name
[0])
5583 local_name
= get_identifier (rent
->local_name
);
5585 local_name
= NULL_TREE
;
5586 gfc_set_backend_locus (&rent
->where
);
5587 (*debug_hooks
->imported_module_or_decl
) (decl
, local_name
,
5588 ns
->proc_name
->backend_decl
,
5589 !use_stmt
->only_flag
,
5596 /* Return true if expr is a constant initializer that gfc_conv_initializer
5600 check_constant_initializer (gfc_expr
*expr
, gfc_typespec
*ts
, bool array
,
5610 if (expr
->expr_type
== EXPR_CONSTANT
|| expr
->expr_type
== EXPR_NULL
)
5612 else if (expr
->expr_type
== EXPR_STRUCTURE
)
5613 return check_constant_initializer (expr
, ts
, false, false);
5614 else if (expr
->expr_type
!= EXPR_ARRAY
)
5616 for (c
= gfc_constructor_first (expr
->value
.constructor
);
5617 c
; c
= gfc_constructor_next (c
))
5621 if (c
->expr
->expr_type
== EXPR_STRUCTURE
)
5623 if (!check_constant_initializer (c
->expr
, ts
, false, false))
5626 else if (c
->expr
->expr_type
!= EXPR_CONSTANT
)
5631 else switch (ts
->type
)
5634 if (expr
->expr_type
!= EXPR_STRUCTURE
)
5636 cm
= expr
->ts
.u
.derived
->components
;
5637 for (c
= gfc_constructor_first (expr
->value
.constructor
);
5638 c
; c
= gfc_constructor_next (c
), cm
= cm
->next
)
5640 if (!c
->expr
|| cm
->attr
.allocatable
)
5642 if (!check_constant_initializer (c
->expr
, &cm
->ts
,
5649 return expr
->expr_type
== EXPR_CONSTANT
;
5653 /* Emit debug info for parameters and unreferenced variables with
5657 gfc_emit_parameter_debug_info (gfc_symbol
*sym
)
5661 if (sym
->attr
.flavor
!= FL_PARAMETER
5662 && (sym
->attr
.flavor
!= FL_VARIABLE
|| sym
->attr
.referenced
))
5665 if (sym
->backend_decl
!= NULL
5666 || sym
->value
== NULL
5667 || sym
->attr
.use_assoc
5670 || sym
->attr
.function
5671 || sym
->attr
.intrinsic
5672 || sym
->attr
.pointer
5673 || sym
->attr
.allocatable
5674 || sym
->attr
.cray_pointee
5675 || sym
->attr
.threadprivate
5676 || sym
->attr
.is_bind_c
5677 || sym
->attr
.subref_array_pointer
5678 || sym
->attr
.assign
)
5681 if (sym
->ts
.type
== BT_CHARACTER
)
5683 gfc_conv_const_charlen (sym
->ts
.u
.cl
);
5684 if (sym
->ts
.u
.cl
->backend_decl
== NULL
5685 || TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) != INTEGER_CST
)
5688 else if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.alloc_comp
)
5695 if (sym
->as
->type
!= AS_EXPLICIT
)
5697 for (n
= 0; n
< sym
->as
->rank
; n
++)
5698 if (sym
->as
->lower
[n
]->expr_type
!= EXPR_CONSTANT
5699 || sym
->as
->upper
[n
] == NULL
5700 || sym
->as
->upper
[n
]->expr_type
!= EXPR_CONSTANT
)
5704 if (!check_constant_initializer (sym
->value
, &sym
->ts
,
5705 sym
->attr
.dimension
, false))
5708 if (flag_coarray
== GFC_FCOARRAY_LIB
&& sym
->attr
.codimension
)
5711 /* Create the decl for the variable or constant. */
5712 decl
= build_decl (input_location
,
5713 sym
->attr
.flavor
== FL_PARAMETER
? CONST_DECL
: VAR_DECL
,
5714 gfc_sym_identifier (sym
), gfc_sym_type (sym
));
5715 if (sym
->attr
.flavor
== FL_PARAMETER
)
5716 TREE_READONLY (decl
) = 1;
5717 gfc_set_decl_location (decl
, &sym
->declared_at
);
5718 if (sym
->attr
.dimension
)
5719 GFC_DECL_PACKED_ARRAY (decl
) = 1;
5720 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
5721 TREE_STATIC (decl
) = 1;
5722 TREE_USED (decl
) = 1;
5723 if (DECL_CONTEXT (decl
) && TREE_CODE (DECL_CONTEXT (decl
)) == NAMESPACE_DECL
)
5724 TREE_PUBLIC (decl
) = 1;
5725 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
5727 sym
->attr
.dimension
,
5729 debug_hooks
->early_global_decl (decl
);
5734 generate_coarray_sym_init (gfc_symbol
*sym
)
5736 tree tmp
, size
, decl
, token
, desc
;
5737 bool is_lock_type
, is_event_type
;
5740 symbol_attribute attr
;
5742 if (sym
->attr
.dummy
|| sym
->attr
.allocatable
|| !sym
->attr
.codimension
5743 || sym
->attr
.use_assoc
|| !sym
->attr
.referenced
5744 || sym
->attr
.associate_var
5745 || sym
->attr
.select_type_temporary
)
5748 decl
= sym
->backend_decl
;
5749 TREE_USED(decl
) = 1;
5750 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (decl
)));
5752 is_lock_type
= sym
->ts
.type
== BT_DERIVED
5753 && sym
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
5754 && sym
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_LOCK_TYPE
;
5756 is_event_type
= sym
->ts
.type
== BT_DERIVED
5757 && sym
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
5758 && sym
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_EVENT_TYPE
;
5760 /* FIXME: Workaround for PR middle-end/49106, cf. also PR middle-end/49108
5761 to make sure the variable is not optimized away. */
5762 DECL_PRESERVE_P (DECL_CONTEXT (decl
)) = 1;
5764 /* For lock types, we pass the array size as only the library knows the
5765 size of the variable. */
5766 if (is_lock_type
|| is_event_type
)
5767 size
= gfc_index_one_node
;
5769 size
= TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (decl
)));
5771 /* Ensure that we do not have size=0 for zero-sized arrays. */
5772 size
= fold_build2_loc (input_location
, MAX_EXPR
, size_type_node
,
5773 fold_convert (size_type_node
, size
),
5774 build_int_cst (size_type_node
, 1));
5776 if (GFC_TYPE_ARRAY_RANK (TREE_TYPE (decl
)))
5778 tmp
= GFC_TYPE_ARRAY_SIZE (TREE_TYPE (decl
));
5779 size
= fold_build2_loc (input_location
, MULT_EXPR
, size_type_node
,
5780 fold_convert (size_type_node
, tmp
), size
);
5783 gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (decl
)) != NULL_TREE
);
5784 token
= gfc_build_addr_expr (ppvoid_type_node
,
5785 GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE(decl
)));
5787 reg_type
= sym
->attr
.artificial
? GFC_CAF_CRITICAL
: GFC_CAF_LOCK_STATIC
;
5788 else if (is_event_type
)
5789 reg_type
= GFC_CAF_EVENT_STATIC
;
5791 reg_type
= GFC_CAF_COARRAY_STATIC
;
5793 /* Compile the symbol attribute. */
5794 if (sym
->ts
.type
== BT_CLASS
)
5796 attr
= CLASS_DATA (sym
)->attr
;
5797 /* The pointer attribute is always set on classes, overwrite it with the
5798 class_pointer attribute, which denotes the pointer for classes. */
5799 attr
.pointer
= attr
.class_pointer
;
5803 gfc_init_se (&se
, NULL
);
5804 desc
= gfc_conv_scalar_to_descriptor (&se
, decl
, attr
);
5805 gfc_add_block_to_block (&caf_init_block
, &se
.pre
);
5807 tmp
= build_call_expr_loc (input_location
, gfor_fndecl_caf_register
, 7, size
,
5808 build_int_cst (integer_type_node
, reg_type
),
5809 token
, gfc_build_addr_expr (pvoid_type_node
, desc
),
5810 null_pointer_node
, /* stat. */
5811 null_pointer_node
, /* errgmsg. */
5812 build_zero_cst (size_type_node
)); /* errmsg_len. */
5813 gfc_add_expr_to_block (&caf_init_block
, tmp
);
5814 gfc_add_modify (&caf_init_block
, decl
, fold_convert (TREE_TYPE (decl
),
5815 gfc_conv_descriptor_data_get (desc
)));
5817 /* Handle "static" initializer. */
5820 if (sym
->value
->expr_type
== EXPR_ARRAY
)
5822 gfc_constructor
*c
, *cnext
;
5824 /* Test if the array has more than one element. */
5825 c
= gfc_constructor_first (sym
->value
->value
.constructor
);
5826 gcc_assert (c
); /* Empty constructor should not happen here. */
5827 cnext
= gfc_constructor_next (c
);
5831 /* An EXPR_ARRAY with a rank > 1 here has to come from a
5832 DATA statement. Set its rank here as not to confuse
5833 the following steps. */
5834 sym
->value
->rank
= 1;
5838 /* There is only a single value in the constructor, use
5839 it directly for the assignment. */
5841 new_expr
= gfc_copy_expr (c
->expr
);
5842 gfc_free_expr (sym
->value
);
5843 sym
->value
= new_expr
;
5847 sym
->attr
.pointer
= 1;
5848 tmp
= gfc_trans_assignment (gfc_lval_expr_from_sym (sym
), sym
->value
,
5850 sym
->attr
.pointer
= 0;
5851 gfc_add_expr_to_block (&caf_init_block
, tmp
);
5853 else if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.pointer_comp
)
5855 tmp
= gfc_nullify_alloc_comp (sym
->ts
.u
.derived
, decl
, sym
->as
5856 ? sym
->as
->rank
: 0,
5857 GFC_STRUCTURE_CAF_MODE_IN_COARRAY
);
5858 gfc_add_expr_to_block (&caf_init_block
, tmp
);
5863 /* Generate constructor function to initialize static, nonallocatable
5867 generate_coarray_init (gfc_namespace
* ns
__attribute((unused
)))
5869 tree fndecl
, tmp
, decl
, save_fn_decl
;
5871 save_fn_decl
= current_function_decl
;
5872 push_function_context ();
5874 tmp
= build_function_type_list (void_type_node
, NULL_TREE
);
5875 fndecl
= build_decl (input_location
, FUNCTION_DECL
,
5876 create_tmp_var_name ("_caf_init"), tmp
);
5878 DECL_STATIC_CONSTRUCTOR (fndecl
) = 1;
5879 SET_DECL_INIT_PRIORITY (fndecl
, DEFAULT_INIT_PRIORITY
);
5881 decl
= build_decl (input_location
, RESULT_DECL
, NULL_TREE
, void_type_node
);
5882 DECL_ARTIFICIAL (decl
) = 1;
5883 DECL_IGNORED_P (decl
) = 1;
5884 DECL_CONTEXT (decl
) = fndecl
;
5885 DECL_RESULT (fndecl
) = decl
;
5888 current_function_decl
= fndecl
;
5889 announce_function (fndecl
);
5891 rest_of_decl_compilation (fndecl
, 0, 0);
5892 make_decl_rtl (fndecl
);
5893 allocate_struct_function (fndecl
, false);
5896 gfc_init_block (&caf_init_block
);
5898 gfc_traverse_ns (ns
, generate_coarray_sym_init
);
5900 DECL_SAVED_TREE (fndecl
) = gfc_finish_block (&caf_init_block
);
5904 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
5906 DECL_SAVED_TREE (fndecl
)
5907 = fold_build3_loc (DECL_SOURCE_LOCATION (fndecl
), BIND_EXPR
, void_type_node
,
5908 decl
, DECL_SAVED_TREE (fndecl
), DECL_INITIAL (fndecl
));
5909 dump_function (TDI_original
, fndecl
);
5911 cfun
->function_end_locus
= input_location
;
5914 if (decl_function_context (fndecl
))
5915 (void) cgraph_node::create (fndecl
);
5917 cgraph_node::finalize_function (fndecl
, true);
5919 pop_function_context ();
5920 current_function_decl
= save_fn_decl
;
5925 create_module_nml_decl (gfc_symbol
*sym
)
5927 if (sym
->attr
.flavor
== FL_NAMELIST
)
5929 tree decl
= generate_namelist_decl (sym
);
5931 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
5932 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
5933 rest_of_decl_compilation (decl
, 1, 0);
5934 gfc_module_add_decl (cur_module
, decl
);
5939 /* Generate all the required code for module variables. */
5942 gfc_generate_module_vars (gfc_namespace
* ns
)
5944 module_namespace
= ns
;
5945 cur_module
= gfc_find_module (ns
->proc_name
->name
);
5947 /* Check if the frontend left the namespace in a reasonable state. */
5948 gcc_assert (ns
->proc_name
&& !ns
->proc_name
->tlink
);
5950 /* Generate COMMON blocks. */
5951 gfc_trans_common (ns
);
5953 has_coarray_vars
= false;
5955 /* Create decls for all the module variables. */
5956 gfc_traverse_ns (ns
, gfc_create_module_variable
);
5957 gfc_traverse_ns (ns
, create_module_nml_decl
);
5959 if (flag_coarray
== GFC_FCOARRAY_LIB
&& has_coarray_vars
)
5960 generate_coarray_init (ns
);
5964 gfc_trans_use_stmts (ns
);
5965 gfc_traverse_ns (ns
, gfc_emit_parameter_debug_info
);
5970 gfc_generate_contained_functions (gfc_namespace
* parent
)
5974 /* We create all the prototypes before generating any code. */
5975 for (ns
= parent
->contained
; ns
; ns
= ns
->sibling
)
5977 /* Skip namespaces from used modules. */
5978 if (ns
->parent
!= parent
)
5981 gfc_create_function_decl (ns
, false);
5984 for (ns
= parent
->contained
; ns
; ns
= ns
->sibling
)
5986 /* Skip namespaces from used modules. */
5987 if (ns
->parent
!= parent
)
5990 gfc_generate_function_code (ns
);
5995 /* Drill down through expressions for the array specification bounds and
5996 character length calling generate_local_decl for all those variables
5997 that have not already been declared. */
6000 generate_local_decl (gfc_symbol
*);
6002 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
6005 expr_decls (gfc_expr
*e
, gfc_symbol
*sym
,
6006 int *f ATTRIBUTE_UNUSED
)
6008 if (e
->expr_type
!= EXPR_VARIABLE
6009 || sym
== e
->symtree
->n
.sym
6010 || e
->symtree
->n
.sym
->mark
6011 || e
->symtree
->n
.sym
->ns
!= sym
->ns
)
6014 generate_local_decl (e
->symtree
->n
.sym
);
6019 generate_expr_decls (gfc_symbol
*sym
, gfc_expr
*e
)
6021 gfc_traverse_expr (e
, sym
, expr_decls
, 0);
6025 /* Check for dependencies in the character length and array spec. */
6028 generate_dependency_declarations (gfc_symbol
*sym
)
6032 if (sym
->ts
.type
== BT_CHARACTER
6034 && sym
->ts
.u
.cl
->length
6035 && sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
)
6036 generate_expr_decls (sym
, sym
->ts
.u
.cl
->length
);
6038 if (sym
->as
&& sym
->as
->rank
)
6040 for (i
= 0; i
< sym
->as
->rank
; i
++)
6042 generate_expr_decls (sym
, sym
->as
->lower
[i
]);
6043 generate_expr_decls (sym
, sym
->as
->upper
[i
]);
6049 /* Generate decls for all local variables. We do this to ensure correct
6050 handling of expressions which only appear in the specification of
6054 generate_local_decl (gfc_symbol
* sym
)
6056 if (sym
->attr
.flavor
== FL_VARIABLE
)
6058 if (sym
->attr
.codimension
&& !sym
->attr
.dummy
&& !sym
->attr
.allocatable
6059 && sym
->attr
.referenced
&& !sym
->attr
.use_assoc
)
6060 has_coarray_vars
= true;
6062 if (!sym
->attr
.dummy
&& !sym
->ns
->proc_name
->attr
.entry_master
)
6063 generate_dependency_declarations (sym
);
6065 if (sym
->attr
.ext_attr
& (1 << EXT_ATTR_WEAK
))
6067 if (sym
->attr
.dummy
)
6068 gfc_error ("Symbol %qs at %L has the WEAK attribute but is a "
6069 "dummy argument", sym
->name
, &sym
->declared_at
);
6071 gfc_error ("Symbol %qs at %L has the WEAK attribute but is a "
6072 "local variable", sym
->name
, &sym
->declared_at
);
6075 if (sym
->attr
.referenced
)
6076 gfc_get_symbol_decl (sym
);
6078 /* Warnings for unused dummy arguments. */
6079 else if (sym
->attr
.dummy
&& !sym
->attr
.in_namelist
)
6081 /* INTENT(out) dummy arguments are likely meant to be set. */
6082 if (warn_unused_dummy_argument
&& sym
->attr
.intent
== INTENT_OUT
)
6084 if (sym
->ts
.type
!= BT_DERIVED
)
6085 gfc_warning (OPT_Wunused_dummy_argument
,
6086 "Dummy argument %qs at %L was declared "
6087 "INTENT(OUT) but was not set", sym
->name
,
6089 else if (!gfc_has_default_initializer (sym
->ts
.u
.derived
)
6090 && !sym
->ts
.u
.derived
->attr
.zero_comp
)
6091 gfc_warning (OPT_Wunused_dummy_argument
,
6092 "Derived-type dummy argument %qs at %L was "
6093 "declared INTENT(OUT) but was not set and "
6094 "does not have a default initializer",
6095 sym
->name
, &sym
->declared_at
);
6096 if (sym
->backend_decl
!= NULL_TREE
)
6097 suppress_warning (sym
->backend_decl
);
6099 else if (warn_unused_dummy_argument
)
6101 if (!sym
->attr
.artificial
)
6102 gfc_warning (OPT_Wunused_dummy_argument
,
6103 "Unused dummy argument %qs at %L", sym
->name
,
6106 if (sym
->backend_decl
!= NULL_TREE
)
6107 suppress_warning (sym
->backend_decl
);
6111 /* Warn for unused variables, but not if they're inside a common
6112 block or a namelist. */
6113 else if (warn_unused_variable
6114 && !(sym
->attr
.in_common
|| sym
->mark
|| sym
->attr
.in_namelist
))
6116 if (sym
->attr
.use_only
)
6118 gfc_warning (OPT_Wunused_variable
,
6119 "Unused module variable %qs which has been "
6120 "explicitly imported at %L", sym
->name
,
6122 if (sym
->backend_decl
!= NULL_TREE
)
6123 suppress_warning (sym
->backend_decl
);
6125 else if (!sym
->attr
.use_assoc
)
6127 /* Corner case: the symbol may be an entry point. At this point,
6128 it may appear to be an unused variable. Suppress warning. */
6132 for (el
= sym
->ns
->entries
; el
; el
=el
->next
)
6133 if (strcmp(sym
->name
, el
->sym
->name
) == 0)
6137 gfc_warning (OPT_Wunused_variable
,
6138 "Unused variable %qs declared at %L",
6139 sym
->name
, &sym
->declared_at
);
6140 if (sym
->backend_decl
!= NULL_TREE
)
6141 suppress_warning (sym
->backend_decl
);
6145 /* For variable length CHARACTER parameters, the PARM_DECL already
6146 references the length variable, so force gfc_get_symbol_decl
6147 even when not referenced. If optimize > 0, it will be optimized
6148 away anyway. But do this only after emitting -Wunused-parameter
6149 warning if requested. */
6150 if (sym
->attr
.dummy
&& !sym
->attr
.referenced
6151 && sym
->ts
.type
== BT_CHARACTER
6152 && sym
->ts
.u
.cl
->backend_decl
!= NULL
6153 && VAR_P (sym
->ts
.u
.cl
->backend_decl
))
6155 sym
->attr
.referenced
= 1;
6156 gfc_get_symbol_decl (sym
);
6159 /* INTENT(out) dummy arguments and result variables with allocatable
6160 components are reset by default and need to be set referenced to
6161 generate the code for nullification and automatic lengths. */
6162 if (!sym
->attr
.referenced
6163 && sym
->ts
.type
== BT_DERIVED
6164 && sym
->ts
.u
.derived
->attr
.alloc_comp
6165 && !sym
->attr
.pointer
6166 && ((sym
->attr
.dummy
&& sym
->attr
.intent
== INTENT_OUT
)
6168 (sym
->attr
.result
&& sym
!= sym
->result
)))
6170 sym
->attr
.referenced
= 1;
6171 gfc_get_symbol_decl (sym
);
6174 /* Check for dependencies in the array specification and string
6175 length, adding the necessary declarations to the function. We
6176 mark the symbol now, as well as in traverse_ns, to prevent
6177 getting stuck in a circular dependency. */
6180 else if (sym
->attr
.flavor
== FL_PARAMETER
)
6182 if (warn_unused_parameter
6183 && !sym
->attr
.referenced
)
6185 if (!sym
->attr
.use_assoc
)
6186 gfc_warning (OPT_Wunused_parameter
,
6187 "Unused parameter %qs declared at %L", sym
->name
,
6189 else if (sym
->attr
.use_only
)
6190 gfc_warning (OPT_Wunused_parameter
,
6191 "Unused parameter %qs which has been explicitly "
6192 "imported at %L", sym
->name
, &sym
->declared_at
);
6195 if (sym
->ns
&& sym
->ns
->construct_entities
)
6197 /* Construction of the intrinsic modules within a BLOCK
6198 construct, where ONLY and RENAMED entities are included,
6199 seems to be bogus. This is a workaround that can be removed
6200 if someone ever takes on the task to creating full-fledge
6201 modules. See PR 69455. */
6202 if (sym
->attr
.referenced
6203 && sym
->from_intmod
!= INTMOD_ISO_C_BINDING
6204 && sym
->from_intmod
!= INTMOD_ISO_FORTRAN_ENV
)
6205 gfc_get_symbol_decl (sym
);
6209 else if (sym
->attr
.flavor
== FL_PROCEDURE
)
6211 /* TODO: move to the appropriate place in resolve.cc. */
6212 if (warn_return_type
> 0
6213 && sym
->attr
.function
6215 && sym
!= sym
->result
6216 && !sym
->result
->attr
.referenced
6217 && !sym
->attr
.use_assoc
6218 && sym
->attr
.if_source
!= IFSRC_IFBODY
)
6220 gfc_warning (OPT_Wreturn_type
,
6221 "Return value %qs of function %qs declared at "
6222 "%L not set", sym
->result
->name
, sym
->name
,
6223 &sym
->result
->declared_at
);
6225 /* Prevents "Unused variable" warning for RESULT variables. */
6226 sym
->result
->mark
= 1;
6230 if (sym
->attr
.dummy
== 1)
6232 /* The tree type for scalar character dummy arguments of BIND(C)
6233 procedures, if they are passed by value, should be unsigned char.
6234 The value attribute implies the dummy is a scalar. */
6235 if (sym
->attr
.value
== 1 && sym
->backend_decl
!= NULL
6236 && sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.is_c_interop
6237 && sym
->ns
->proc_name
!= NULL
&& sym
->ns
->proc_name
->attr
.is_bind_c
)
6239 /* We used to modify the tree here. Now it is done earlier in
6240 the front-end, so we only check it here to avoid regressions. */
6241 gcc_assert (TREE_CODE (TREE_TYPE (sym
->backend_decl
)) == INTEGER_TYPE
);
6242 gcc_assert (TYPE_UNSIGNED (TREE_TYPE (sym
->backend_decl
)) == 1);
6243 gcc_assert (TYPE_PRECISION (TREE_TYPE (sym
->backend_decl
)) == CHAR_TYPE_SIZE
);
6244 gcc_assert (DECL_BY_REFERENCE (sym
->backend_decl
) == 0);
6247 /* Unused procedure passed as dummy argument. */
6248 if (sym
->attr
.flavor
== FL_PROCEDURE
)
6250 if (!sym
->attr
.referenced
&& !sym
->attr
.artificial
)
6252 if (warn_unused_dummy_argument
)
6253 gfc_warning (OPT_Wunused_dummy_argument
,
6254 "Unused dummy argument %qs at %L", sym
->name
,
6258 /* Silence bogus "unused parameter" warnings from the
6260 if (sym
->backend_decl
!= NULL_TREE
)
6261 suppress_warning (sym
->backend_decl
);
6265 /* Make sure we convert the types of the derived types from iso_c_binding
6267 if (sym
->attr
.flavor
!= FL_PROCEDURE
&& sym
->attr
.is_iso_c
6268 && sym
->ts
.type
== BT_DERIVED
)
6269 sym
->backend_decl
= gfc_typenode_for_spec (&(sym
->ts
));
6274 generate_local_nml_decl (gfc_symbol
* sym
)
6276 if (sym
->attr
.flavor
== FL_NAMELIST
&& !sym
->attr
.use_assoc
)
6278 tree decl
= generate_namelist_decl (sym
);
6285 generate_local_vars (gfc_namespace
* ns
)
6287 gfc_traverse_ns (ns
, generate_local_decl
);
6288 gfc_traverse_ns (ns
, generate_local_nml_decl
);
6292 /* Generate a switch statement to jump to the correct entry point. Also
6293 creates the label decls for the entry points. */
6296 gfc_trans_entry_master_switch (gfc_entry_list
* el
)
6303 gfc_init_block (&block
);
6304 for (; el
; el
= el
->next
)
6306 /* Add the case label. */
6307 label
= gfc_build_label_decl (NULL_TREE
);
6308 val
= build_int_cst (gfc_array_index_type
, el
->id
);
6309 tmp
= build_case_label (val
, NULL_TREE
, label
);
6310 gfc_add_expr_to_block (&block
, tmp
);
6312 /* And jump to the actual entry point. */
6313 label
= gfc_build_label_decl (NULL_TREE
);
6314 tmp
= build1_v (GOTO_EXPR
, label
);
6315 gfc_add_expr_to_block (&block
, tmp
);
6317 /* Save the label decl. */
6320 tmp
= gfc_finish_block (&block
);
6321 /* The first argument selects the entry point. */
6322 val
= DECL_ARGUMENTS (current_function_decl
);
6323 tmp
= fold_build2_loc (input_location
, SWITCH_EXPR
, NULL_TREE
, val
, tmp
);
6328 /* Add code to string lengths of actual arguments passed to a function against
6329 the expected lengths of the dummy arguments. */
6332 add_argument_checking (stmtblock_t
*block
, gfc_symbol
*sym
)
6334 gfc_formal_arglist
*formal
;
6336 for (formal
= gfc_sym_get_dummy_args (sym
); formal
; formal
= formal
->next
)
6337 if (formal
->sym
&& formal
->sym
->ts
.type
== BT_CHARACTER
6338 && !formal
->sym
->ts
.deferred
)
6340 enum tree_code comparison
;
6345 const char *message
;
6351 gcc_assert (cl
->passed_length
!= NULL_TREE
);
6352 gcc_assert (cl
->backend_decl
!= NULL_TREE
);
6354 /* For POINTER, ALLOCATABLE and assumed-shape dummy arguments, the
6355 string lengths must match exactly. Otherwise, it is only required
6356 that the actual string length is *at least* the expected one.
6357 Sequence association allows for a mismatch of the string length
6358 if the actual argument is (part of) an array, but only if the
6359 dummy argument is an array. (See "Sequence association" in
6360 Section 12.4.1.4 for F95 and 12.4.1.5 for F2003.) */
6361 if (fsym
->attr
.pointer
|| fsym
->attr
.allocatable
6362 || (fsym
->as
&& (fsym
->as
->type
== AS_ASSUMED_SHAPE
6363 || fsym
->as
->type
== AS_ASSUMED_RANK
)))
6365 comparison
= NE_EXPR
;
6366 message
= _("Actual string length does not match the declared one"
6367 " for dummy argument '%s' (%ld/%ld)");
6369 else if (fsym
->as
&& fsym
->as
->rank
!= 0)
6373 comparison
= LT_EXPR
;
6374 message
= _("Actual string length is shorter than the declared one"
6375 " for dummy argument '%s' (%ld/%ld)");
6378 /* Build the condition. For optional arguments, an actual length
6379 of 0 is also acceptable if the associated string is NULL, which
6380 means the argument was not passed. */
6381 cond
= fold_build2_loc (input_location
, comparison
, logical_type_node
,
6382 cl
->passed_length
, cl
->backend_decl
);
6383 if (fsym
->attr
.optional
)
6389 not_0length
= fold_build2_loc (input_location
, NE_EXPR
,
6393 (TREE_TYPE (cl
->passed_length
)));
6394 /* The symbol needs to be referenced for gfc_get_symbol_decl. */
6395 fsym
->attr
.referenced
= 1;
6396 not_absent
= gfc_conv_expr_present (fsym
);
6398 absent_failed
= fold_build2_loc (input_location
, TRUTH_OR_EXPR
,
6399 logical_type_node
, not_0length
,
6402 cond
= fold_build2_loc (input_location
, TRUTH_AND_EXPR
,
6403 logical_type_node
, cond
, absent_failed
);
6406 /* Build the runtime check. */
6407 argname
= gfc_build_cstring_const (fsym
->name
);
6408 argname
= gfc_build_addr_expr (pchar_type_node
, argname
);
6409 gfc_trans_runtime_check (true, false, cond
, block
, &fsym
->declared_at
,
6411 fold_convert (long_integer_type_node
,
6413 fold_convert (long_integer_type_node
,
6420 create_main_function (tree fndecl
)
6424 tree tmp
, decl
, result_decl
, argc
, argv
, typelist
, arglist
;
6427 old_context
= current_function_decl
;
6431 push_function_context ();
6432 saved_parent_function_decls
= saved_function_decls
;
6433 saved_function_decls
= NULL_TREE
;
6436 /* main() function must be declared with global scope. */
6437 gcc_assert (current_function_decl
== NULL_TREE
);
6439 /* Declare the function. */
6440 tmp
= build_function_type_list (integer_type_node
, integer_type_node
,
6441 build_pointer_type (pchar_type_node
),
6443 main_identifier_node
= get_identifier ("main");
6444 ftn_main
= build_decl (input_location
, FUNCTION_DECL
,
6445 main_identifier_node
, tmp
);
6446 DECL_EXTERNAL (ftn_main
) = 0;
6447 TREE_PUBLIC (ftn_main
) = 1;
6448 TREE_STATIC (ftn_main
) = 1;
6449 DECL_ATTRIBUTES (ftn_main
)
6450 = tree_cons (get_identifier("externally_visible"), NULL_TREE
, NULL_TREE
);
6452 /* Setup the result declaration (for "return 0"). */
6453 result_decl
= build_decl (input_location
,
6454 RESULT_DECL
, NULL_TREE
, integer_type_node
);
6455 DECL_ARTIFICIAL (result_decl
) = 1;
6456 DECL_IGNORED_P (result_decl
) = 1;
6457 DECL_CONTEXT (result_decl
) = ftn_main
;
6458 DECL_RESULT (ftn_main
) = result_decl
;
6460 pushdecl (ftn_main
);
6462 /* Get the arguments. */
6464 arglist
= NULL_TREE
;
6465 typelist
= TYPE_ARG_TYPES (TREE_TYPE (ftn_main
));
6467 tmp
= TREE_VALUE (typelist
);
6468 argc
= build_decl (input_location
, PARM_DECL
, get_identifier ("argc"), tmp
);
6469 DECL_CONTEXT (argc
) = ftn_main
;
6470 DECL_ARG_TYPE (argc
) = TREE_VALUE (typelist
);
6471 TREE_READONLY (argc
) = 1;
6472 gfc_finish_decl (argc
);
6473 arglist
= chainon (arglist
, argc
);
6475 typelist
= TREE_CHAIN (typelist
);
6476 tmp
= TREE_VALUE (typelist
);
6477 argv
= build_decl (input_location
, PARM_DECL
, get_identifier ("argv"), tmp
);
6478 DECL_CONTEXT (argv
) = ftn_main
;
6479 DECL_ARG_TYPE (argv
) = TREE_VALUE (typelist
);
6480 TREE_READONLY (argv
) = 1;
6481 DECL_BY_REFERENCE (argv
) = 1;
6482 gfc_finish_decl (argv
);
6483 arglist
= chainon (arglist
, argv
);
6485 DECL_ARGUMENTS (ftn_main
) = arglist
;
6486 current_function_decl
= ftn_main
;
6487 announce_function (ftn_main
);
6489 rest_of_decl_compilation (ftn_main
, 1, 0);
6490 make_decl_rtl (ftn_main
);
6491 allocate_struct_function (ftn_main
, false);
6494 gfc_init_block (&body
);
6496 /* Call some libgfortran initialization routines, call then MAIN__(). */
6498 /* Call _gfortran_caf_init (*argc, ***argv). */
6499 if (flag_coarray
== GFC_FCOARRAY_LIB
)
6501 tree pint_type
, pppchar_type
;
6502 pint_type
= build_pointer_type (integer_type_node
);
6504 = build_pointer_type (build_pointer_type (pchar_type_node
));
6506 tmp
= build_call_expr_loc (input_location
, gfor_fndecl_caf_init
, 2,
6507 gfc_build_addr_expr (pint_type
, argc
),
6508 gfc_build_addr_expr (pppchar_type
, argv
));
6509 gfc_add_expr_to_block (&body
, tmp
);
6512 /* Call _gfortran_set_args (argc, argv). */
6513 TREE_USED (argc
) = 1;
6514 TREE_USED (argv
) = 1;
6515 tmp
= build_call_expr_loc (input_location
,
6516 gfor_fndecl_set_args
, 2, argc
, argv
);
6517 gfc_add_expr_to_block (&body
, tmp
);
6519 /* Add a call to set_options to set up the runtime library Fortran
6520 language standard parameters. */
6522 tree array_type
, array
, var
;
6523 vec
<constructor_elt
, va_gc
> *v
= NULL
;
6524 static const int noptions
= 7;
6526 /* Passing a new option to the library requires three modifications:
6527 + add it to the tree_cons list below
6528 + change the noptions variable above
6529 + modify the library (runtime/compile_options.c)! */
6531 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
6532 build_int_cst (integer_type_node
,
6533 gfc_option
.warn_std
));
6534 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
6535 build_int_cst (integer_type_node
,
6536 gfc_option
.allow_std
));
6537 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
6538 build_int_cst (integer_type_node
, pedantic
));
6539 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
6540 build_int_cst (integer_type_node
, flag_backtrace
));
6541 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
6542 build_int_cst (integer_type_node
, flag_sign_zero
));
6543 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
6544 build_int_cst (integer_type_node
,
6546 & GFC_RTCHECK_BOUNDS
)));
6547 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
6548 build_int_cst (integer_type_node
,
6549 gfc_option
.fpe_summary
));
6551 array_type
= build_array_type_nelts (integer_type_node
, noptions
);
6552 array
= build_constructor (array_type
, v
);
6553 TREE_CONSTANT (array
) = 1;
6554 TREE_STATIC (array
) = 1;
6556 /* Create a static variable to hold the jump table. */
6557 var
= build_decl (input_location
, VAR_DECL
,
6558 create_tmp_var_name ("options"), array_type
);
6559 DECL_ARTIFICIAL (var
) = 1;
6560 DECL_IGNORED_P (var
) = 1;
6561 TREE_CONSTANT (var
) = 1;
6562 TREE_STATIC (var
) = 1;
6563 TREE_READONLY (var
) = 1;
6564 DECL_INITIAL (var
) = array
;
6566 var
= gfc_build_addr_expr (build_pointer_type (integer_type_node
), var
);
6568 tmp
= build_call_expr_loc (input_location
,
6569 gfor_fndecl_set_options
, 2,
6570 build_int_cst (integer_type_node
, noptions
), var
);
6571 gfc_add_expr_to_block (&body
, tmp
);
6574 /* If -ffpe-trap option was provided, add a call to set_fpe so that
6575 the library will raise a FPE when needed. */
6576 if (gfc_option
.fpe
!= 0)
6578 tmp
= build_call_expr_loc (input_location
,
6579 gfor_fndecl_set_fpe
, 1,
6580 build_int_cst (integer_type_node
,
6582 gfc_add_expr_to_block (&body
, tmp
);
6585 /* If this is the main program and an -fconvert option was provided,
6586 add a call to set_convert. */
6588 if (flag_convert
!= GFC_FLAG_CONVERT_NATIVE
)
6590 tmp
= build_call_expr_loc (input_location
,
6591 gfor_fndecl_set_convert
, 1,
6592 build_int_cst (integer_type_node
, flag_convert
));
6593 gfc_add_expr_to_block (&body
, tmp
);
6596 /* If this is the main program and an -frecord-marker option was provided,
6597 add a call to set_record_marker. */
6599 if (flag_record_marker
!= 0)
6601 tmp
= build_call_expr_loc (input_location
,
6602 gfor_fndecl_set_record_marker
, 1,
6603 build_int_cst (integer_type_node
,
6604 flag_record_marker
));
6605 gfc_add_expr_to_block (&body
, tmp
);
6608 if (flag_max_subrecord_length
!= 0)
6610 tmp
= build_call_expr_loc (input_location
,
6611 gfor_fndecl_set_max_subrecord_length
, 1,
6612 build_int_cst (integer_type_node
,
6613 flag_max_subrecord_length
));
6614 gfc_add_expr_to_block (&body
, tmp
);
6617 /* Call MAIN__(). */
6618 tmp
= build_call_expr_loc (input_location
,
6620 gfc_add_expr_to_block (&body
, tmp
);
6622 /* Mark MAIN__ as used. */
6623 TREE_USED (fndecl
) = 1;
6625 /* Coarray: Call _gfortran_caf_finalize(void). */
6626 if (flag_coarray
== GFC_FCOARRAY_LIB
)
6628 tmp
= build_call_expr_loc (input_location
, gfor_fndecl_caf_finalize
, 0);
6629 gfc_add_expr_to_block (&body
, tmp
);
6633 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, integer_type_node
,
6634 DECL_RESULT (ftn_main
),
6635 build_int_cst (integer_type_node
, 0));
6636 tmp
= build1_v (RETURN_EXPR
, tmp
);
6637 gfc_add_expr_to_block (&body
, tmp
);
6640 DECL_SAVED_TREE (ftn_main
) = gfc_finish_block (&body
);
6643 /* Finish off this function and send it for code generation. */
6645 BLOCK_SUPERCONTEXT (DECL_INITIAL (ftn_main
)) = ftn_main
;
6647 DECL_SAVED_TREE (ftn_main
)
6648 = fold_build3_loc (DECL_SOURCE_LOCATION (ftn_main
), BIND_EXPR
,
6649 void_type_node
, decl
, DECL_SAVED_TREE (ftn_main
),
6650 DECL_INITIAL (ftn_main
));
6652 /* Output the GENERIC tree. */
6653 dump_function (TDI_original
, ftn_main
);
6655 cgraph_node::finalize_function (ftn_main
, true);
6659 pop_function_context ();
6660 saved_function_decls
= saved_parent_function_decls
;
6662 current_function_decl
= old_context
;
6666 /* Generate an appropriate return-statement for a procedure. */
6669 gfc_generate_return (void)
6675 sym
= current_procedure_symbol
;
6676 fndecl
= sym
->backend_decl
;
6678 if (TREE_TYPE (DECL_RESULT (fndecl
)) == void_type_node
)
6682 result
= get_proc_result (sym
);
6684 /* Set the return value to the dummy result variable. The
6685 types may be different for scalar default REAL functions
6686 with -ff2c, therefore we have to convert. */
6687 if (result
!= NULL_TREE
)
6689 result
= convert (TREE_TYPE (DECL_RESULT (fndecl
)), result
);
6690 result
= fold_build2_loc (input_location
, MODIFY_EXPR
,
6691 TREE_TYPE (result
), DECL_RESULT (fndecl
),
6696 /* If the function does not have a result variable, result is
6697 NULL_TREE, and a 'return' is generated without a variable.
6698 The following generates a 'return __result_XXX' where XXX is
6699 the function name. */
6700 if (sym
== sym
->result
&& sym
->attr
.function
&& !flag_f2c
)
6702 result
= gfc_get_fake_result_decl (sym
, 0);
6703 result
= fold_build2_loc (input_location
, MODIFY_EXPR
,
6705 DECL_RESULT (fndecl
), result
);
6710 return build1_v (RETURN_EXPR
, result
);
6715 is_from_ieee_module (gfc_symbol
*sym
)
6717 if (sym
->from_intmod
== INTMOD_IEEE_FEATURES
6718 || sym
->from_intmod
== INTMOD_IEEE_EXCEPTIONS
6719 || sym
->from_intmod
== INTMOD_IEEE_ARITHMETIC
)
6720 seen_ieee_symbol
= 1;
6725 is_ieee_module_used (gfc_namespace
*ns
)
6727 seen_ieee_symbol
= 0;
6728 gfc_traverse_ns (ns
, is_from_ieee_module
);
6729 return seen_ieee_symbol
;
6733 static gfc_omp_clauses
*module_oacc_clauses
;
6737 add_clause (gfc_symbol
*sym
, gfc_omp_map_op map_op
)
6739 gfc_omp_namelist
*n
;
6741 n
= gfc_get_omp_namelist ();
6743 n
->u
.map_op
= map_op
;
6745 if (!module_oacc_clauses
)
6746 module_oacc_clauses
= gfc_get_omp_clauses ();
6748 if (module_oacc_clauses
->lists
[OMP_LIST_MAP
])
6749 n
->next
= module_oacc_clauses
->lists
[OMP_LIST_MAP
];
6751 module_oacc_clauses
->lists
[OMP_LIST_MAP
] = n
;
6756 find_module_oacc_declare_clauses (gfc_symbol
*sym
)
6758 if (sym
->attr
.use_assoc
)
6760 gfc_omp_map_op map_op
;
6762 if (sym
->attr
.oacc_declare_create
)
6763 map_op
= OMP_MAP_FORCE_ALLOC
;
6765 if (sym
->attr
.oacc_declare_copyin
)
6766 map_op
= OMP_MAP_FORCE_TO
;
6768 if (sym
->attr
.oacc_declare_deviceptr
)
6769 map_op
= OMP_MAP_FORCE_DEVICEPTR
;
6771 if (sym
->attr
.oacc_declare_device_resident
)
6772 map_op
= OMP_MAP_DEVICE_RESIDENT
;
6774 if (sym
->attr
.oacc_declare_create
6775 || sym
->attr
.oacc_declare_copyin
6776 || sym
->attr
.oacc_declare_deviceptr
6777 || sym
->attr
.oacc_declare_device_resident
)
6779 sym
->attr
.referenced
= 1;
6780 add_clause (sym
, map_op
);
6787 finish_oacc_declare (gfc_namespace
*ns
, gfc_symbol
*sym
, bool block
)
6790 gfc_oacc_declare
*oc
;
6791 locus where
= gfc_current_locus
;
6792 gfc_omp_clauses
*omp_clauses
= NULL
;
6793 gfc_omp_namelist
*n
, *p
;
6795 module_oacc_clauses
= NULL
;
6796 gfc_traverse_ns (ns
, find_module_oacc_declare_clauses
);
6798 if (module_oacc_clauses
&& sym
->attr
.flavor
== FL_PROGRAM
)
6800 gfc_oacc_declare
*new_oc
;
6802 new_oc
= gfc_get_oacc_declare ();
6803 new_oc
->next
= ns
->oacc_declare
;
6804 new_oc
->clauses
= module_oacc_clauses
;
6806 ns
->oacc_declare
= new_oc
;
6809 if (!ns
->oacc_declare
)
6812 for (oc
= ns
->oacc_declare
; oc
; oc
= oc
->next
)
6818 gfc_error ("Sorry, !$ACC DECLARE at %L is not allowed "
6819 "in BLOCK construct", &oc
->loc
);
6822 if (oc
->clauses
&& oc
->clauses
->lists
[OMP_LIST_MAP
])
6824 if (omp_clauses
== NULL
)
6826 omp_clauses
= oc
->clauses
;
6830 for (n
= oc
->clauses
->lists
[OMP_LIST_MAP
]; n
; p
= n
, n
= n
->next
)
6833 gcc_assert (p
->next
== NULL
);
6835 p
->next
= omp_clauses
->lists
[OMP_LIST_MAP
];
6836 omp_clauses
= oc
->clauses
;
6843 for (n
= omp_clauses
->lists
[OMP_LIST_MAP
]; n
; n
= n
->next
)
6845 switch (n
->u
.map_op
)
6847 case OMP_MAP_DEVICE_RESIDENT
:
6848 n
->u
.map_op
= OMP_MAP_FORCE_ALLOC
;
6856 code
= XCNEW (gfc_code
);
6857 code
->op
= EXEC_OACC_DECLARE
;
6860 code
->ext
.oacc_declare
= gfc_get_oacc_declare ();
6861 code
->ext
.oacc_declare
->clauses
= omp_clauses
;
6863 code
->block
= XCNEW (gfc_code
);
6864 code
->block
->op
= EXEC_OACC_DECLARE
;
6865 code
->block
->loc
= where
;
6868 code
->block
->next
= ns
->code
;
6876 gfc_conv_cfi_to_gfc (stmtblock_t
*init
, stmtblock_t
*finally
,
6877 tree cfi_desc
, tree gfc_desc
, gfc_symbol
*sym
)
6880 gfc_init_block (&block
);
6881 tree cfi
= build_fold_indirect_ref_loc (input_location
, cfi_desc
);
6882 tree idx
, etype
, tmp
, tmp2
, size_var
= NULL_TREE
, rank
= NULL_TREE
;
6883 bool do_copy_inout
= false;
6885 /* When allocatable + intent out, free the cfi descriptor. */
6886 if (sym
->attr
.allocatable
&& sym
->attr
.intent
== INTENT_OUT
)
6888 tmp
= gfc_get_cfi_desc_base_addr (cfi
);
6889 tree call
= builtin_decl_explicit (BUILT_IN_FREE
);
6890 call
= build_call_expr_loc (input_location
, call
, 1, tmp
);
6891 gfc_add_expr_to_block (&block
, fold_convert (void_type_node
, call
));
6892 gfc_add_modify (&block
, tmp
,
6893 fold_convert (TREE_TYPE (tmp
), null_pointer_node
));
6896 /* -fcheck=bound: Do version, rank, attribute, type and is-NULL checks. */
6897 if (gfc_option
.rtcheck
& GFC_RTCHECK_BOUNDS
)
6901 msg
= xasprintf ("Unexpected version %%d (expected %d) in CFI descriptor "
6902 "passed to dummy argument %s", CFI_VERSION
, sym
->name
);
6903 tmp2
= gfc_get_cfi_desc_version (cfi
);
6904 tmp
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
, tmp2
,
6905 build_int_cst (TREE_TYPE (tmp2
), CFI_VERSION
));
6906 gfc_trans_runtime_check (true, false, tmp
, &block
, &sym
->declared_at
,
6910 /* Rank check; however, for character(len=*), assumed/explicit-size arrays
6911 are permitted to differ in rank according to the Fortran rules. */
6912 if (sym
->as
&& sym
->as
->type
!= AS_ASSUMED_SIZE
6913 && sym
->as
->type
!= AS_EXPLICIT
)
6915 if (sym
->as
->rank
!= -1)
6916 msg
= xasprintf ("Invalid rank %%d (expected %d) in CFI descriptor "
6917 "passed to dummy argument %s", sym
->as
->rank
,
6920 msg
= xasprintf ("Invalid rank %%d (expected 0..%d) in CFI "
6921 "descriptor passed to dummy argument %s",
6922 CFI_MAX_RANK
, sym
->name
);
6924 tmp3
= tmp2
= tmp
= gfc_get_cfi_desc_rank (cfi
);
6925 if (sym
->as
->rank
!= -1)
6926 tmp
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
6927 tmp
, build_int_cst (signed_char_type_node
,
6931 tmp
= fold_build2_loc (input_location
, LT_EXPR
, boolean_type_node
,
6932 tmp
, build_zero_cst (TREE_TYPE (tmp
)));
6933 tmp2
= fold_build2_loc (input_location
, GT_EXPR
,
6934 boolean_type_node
, tmp2
,
6935 build_int_cst (TREE_TYPE (tmp2
),
6937 tmp
= fold_build2_loc (input_location
, TRUTH_AND_EXPR
,
6938 boolean_type_node
, tmp
, tmp2
);
6940 gfc_trans_runtime_check (true, false, tmp
, &block
, &sym
->declared_at
,
6945 tmp3
= tmp
= gfc_get_cfi_desc_attribute (cfi
);
6946 if (sym
->attr
.allocatable
|| sym
->attr
.pointer
)
6948 int attr
= (sym
->attr
.pointer
? CFI_attribute_pointer
6949 : CFI_attribute_allocatable
);
6950 msg
= xasprintf ("Invalid attribute %%d (expected %d) in CFI "
6951 "descriptor passed to dummy argument %s with %s "
6952 "attribute", attr
, sym
->name
,
6953 sym
->attr
.pointer
? "pointer" : "allocatable");
6954 tmp
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
6955 tmp
, build_int_cst (TREE_TYPE (tmp
), attr
));
6959 int amin
= MIN (CFI_attribute_pointer
,
6960 MIN (CFI_attribute_allocatable
, CFI_attribute_other
));
6961 int amax
= MAX (CFI_attribute_pointer
,
6962 MAX (CFI_attribute_allocatable
, CFI_attribute_other
));
6963 msg
= xasprintf ("Invalid attribute %%d (expected %d..%d) in CFI "
6964 "descriptor passed to nonallocatable, nonpointer "
6965 "dummy argument %s", amin
, amax
, sym
->name
);
6967 tmp
= fold_build2_loc (input_location
, LT_EXPR
, boolean_type_node
, tmp
,
6968 build_int_cst (TREE_TYPE (tmp
), amin
));
6969 tmp2
= fold_build2_loc (input_location
, GT_EXPR
, boolean_type_node
, tmp2
,
6970 build_int_cst (TREE_TYPE (tmp2
), amax
));
6971 tmp
= fold_build2_loc (input_location
, TRUTH_AND_EXPR
,
6972 boolean_type_node
, tmp
, tmp2
);
6973 gfc_trans_runtime_check (true, false, tmp
, &block
, &sym
->declared_at
,
6976 msg
= xasprintf ("Invalid unallocatated/unassociated CFI "
6977 "descriptor passed to nonallocatable, nonpointer "
6978 "dummy argument %s", sym
->name
);
6979 tmp3
= tmp
= gfc_get_cfi_desc_base_addr (cfi
),
6980 tmp
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
,
6981 tmp
, null_pointer_node
);
6983 gfc_trans_runtime_check (true, false, tmp
, &block
, &sym
->declared_at
,
6987 if (sym
->ts
.type
!= BT_ASSUMED
)
6989 int type
= CFI_type_other
;
6990 if (sym
->ts
.f90_type
== BT_VOID
)
6992 type
= (sym
->ts
.u
.derived
->intmod_sym_id
== ISOCBINDING_FUNPTR
6993 ? CFI_type_cfunptr
: CFI_type_cptr
);
6996 switch (sym
->ts
.type
)
7002 type
= CFI_type_from_type_kind (sym
->ts
.type
, sym
->ts
.kind
);
7005 type
= CFI_type_from_type_kind (CFI_type_Character
,
7009 type
= CFI_type_struct
;
7012 type
= (sym
->ts
.u
.derived
->intmod_sym_id
== ISOCBINDING_FUNPTR
7013 ? CFI_type_cfunptr
: CFI_type_cptr
);
7024 msg
= xasprintf ("Unexpected type %%d (expected %d) in CFI descriptor"
7025 " passed to dummy argument %s", type
, sym
->name
);
7026 tmp2
= tmp
= gfc_get_cfi_desc_type (cfi
);
7027 tmp
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
7028 tmp
, build_int_cst (TREE_TYPE (tmp
), type
));
7029 gfc_trans_runtime_check (true, false, tmp
, &block
, &sym
->declared_at
,
7035 if (!sym
->attr
.referenced
)
7038 /* Set string length for len=* and len=:, otherwise, it is already set. */
7039 if (sym
->ts
.type
== BT_CHARACTER
&& !sym
->ts
.u
.cl
->length
)
7041 tmp
= fold_convert (gfc_array_index_type
,
7042 gfc_get_cfi_desc_elem_len (cfi
));
7043 if (sym
->ts
.kind
!= 1)
7044 tmp
= fold_build2_loc (input_location
, TRUNC_DIV_EXPR
,
7045 gfc_array_index_type
, tmp
,
7046 build_int_cst (gfc_charlen_type_node
,
7048 gfc_add_modify (&block
, sym
->ts
.u
.cl
->backend_decl
, tmp
);
7051 if (sym
->ts
.type
== BT_CHARACTER
7052 && !INTEGER_CST_P (sym
->ts
.u
.cl
->backend_decl
))
7054 gfc_conv_string_length (sym
->ts
.u
.cl
, NULL
, init
);
7055 gfc_trans_vla_type_sizes (sym
, init
);
7058 /* gfc->data = cfi->base_addr - or for scalars: gfc = cfi->base_addr.
7059 assumed-size/explicit-size arrays end up here for character(len=*)
7061 if (!sym
->attr
.dimension
|| !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc
)))
7063 tmp
= gfc_get_cfi_desc_base_addr (cfi
);
7064 gfc_add_modify (&block
, gfc_desc
,
7065 fold_convert (TREE_TYPE (gfc_desc
), tmp
));
7066 if (!sym
->attr
.dimension
)
7070 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc
)))
7072 /* gfc->dtype = ... (from declaration, not from cfi). */
7073 etype
= gfc_get_element_type (TREE_TYPE (gfc_desc
));
7074 gfc_add_modify (&block
, gfc_conv_descriptor_dtype (gfc_desc
),
7075 gfc_get_dtype_rank_type (sym
->as
->rank
, etype
));
7076 /* gfc->data = cfi->base_addr. */
7077 gfc_conv_descriptor_data_set (&block
, gfc_desc
,
7078 gfc_get_cfi_desc_base_addr (cfi
));
7081 if (sym
->ts
.type
== BT_ASSUMED
)
7083 /* For type(*), take elem_len + dtype.type from the actual argument. */
7084 gfc_add_modify (&block
, gfc_conv_descriptor_elem_len (gfc_desc
),
7085 gfc_get_cfi_desc_elem_len (cfi
));
7087 tree ctype
= gfc_get_cfi_desc_type (cfi
);
7088 ctype
= fold_build2_loc (input_location
, BIT_AND_EXPR
, TREE_TYPE (ctype
),
7089 ctype
, build_int_cst (TREE_TYPE (ctype
),
7091 tree type
= gfc_conv_descriptor_type (gfc_desc
);
7093 /* if (CFI_type_cptr) BT_VOID else BT_UNKNOWN */
7094 /* Note: BT_VOID is could also be CFI_type_funcptr, but assume c_ptr. */
7095 cond
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
, ctype
,
7096 build_int_cst (TREE_TYPE (ctype
), CFI_type_cptr
));
7097 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, void_type_node
, type
,
7098 build_int_cst (TREE_TYPE (type
), BT_VOID
));
7099 tmp2
= fold_build2_loc (input_location
, MODIFY_EXPR
, void_type_node
,
7101 build_int_cst (TREE_TYPE (type
), BT_UNKNOWN
));
7102 tmp2
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
,
7104 /* if (CFI_type_struct) BT_DERIVED else < tmp2 > */
7105 cond
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
, ctype
,
7106 build_int_cst (TREE_TYPE (ctype
),
7108 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, void_type_node
, type
,
7109 build_int_cst (TREE_TYPE (type
), BT_DERIVED
));
7110 tmp2
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
,
7112 /* if (CFI_type_Character) BT_CHARACTER else < tmp2 > */
7113 /* Note: this is kind=1, CFI_type_ucs4_char is handled in the 'else if'
7114 before (see below, as generated bottom up). */
7115 cond
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
, ctype
,
7116 build_int_cst (TREE_TYPE (ctype
),
7117 CFI_type_Character
));
7118 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, void_type_node
, type
,
7119 build_int_cst (TREE_TYPE (type
), BT_CHARACTER
));
7120 tmp2
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
,
7122 /* if (CFI_type_ucs4_char) BT_CHARACTER else < tmp2 > */
7123 /* Note: gfc->elem_len = cfi->elem_len/4. */
7124 /* However, assuming that CFI_type_ucs4_char cannot be recovered, leave
7125 gfc->elem_len == cfi->elem_len, which helps with operations which use
7126 sizeof() in Fortran and cfi->elem_len in C. */
7127 tmp
= gfc_get_cfi_desc_type (cfi
);
7128 cond
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
, tmp
,
7129 build_int_cst (TREE_TYPE (tmp
),
7130 CFI_type_ucs4_char
));
7131 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, void_type_node
, type
,
7132 build_int_cst (TREE_TYPE (type
), BT_CHARACTER
));
7133 tmp2
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
,
7135 /* if (CFI_type_Complex) BT_COMPLEX + cfi->elem_len/2 else < tmp2 > */
7136 cond
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
, ctype
,
7137 build_int_cst (TREE_TYPE (ctype
),
7139 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, void_type_node
, type
,
7140 build_int_cst (TREE_TYPE (type
), BT_COMPLEX
));
7141 tmp2
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
,
7143 /* if (CFI_type_Integer || CFI_type_Logical || CFI_type_Real)
7144 ctype else <tmp2> */
7145 cond
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
, ctype
,
7146 build_int_cst (TREE_TYPE (ctype
),
7148 tmp
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
, ctype
,
7149 build_int_cst (TREE_TYPE (ctype
),
7151 cond
= fold_build2_loc (input_location
, TRUTH_OR_EXPR
, boolean_type_node
,
7153 tmp
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
, ctype
,
7154 build_int_cst (TREE_TYPE (ctype
),
7156 cond
= fold_build2_loc (input_location
, TRUTH_OR_EXPR
, boolean_type_node
,
7158 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, void_type_node
,
7159 type
, fold_convert (TREE_TYPE (type
), ctype
));
7160 tmp2
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
,
7162 gfc_add_expr_to_block (&block
, tmp2
);
7165 if (sym
->as
->rank
< 0)
7167 /* Set gfc->dtype.rank, if assumed-rank. */
7168 rank
= gfc_get_cfi_desc_rank (cfi
);
7169 gfc_add_modify (&block
, gfc_conv_descriptor_rank (gfc_desc
), rank
);
7171 else if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc
)))
7172 /* In that case, the CFI rank and the declared rank can differ. */
7173 rank
= gfc_get_cfi_desc_rank (cfi
);
7175 rank
= build_int_cst (signed_char_type_node
, sym
->as
->rank
);
7177 /* With bind(C), the standard requires that both Fortran callers and callees
7178 handle noncontiguous arrays passed to an dummy with 'contiguous' attribute
7179 and with character(len=*) + assumed-size/explicit-size arrays.
7180 cf. Fortran 2018, 18.3.6, paragraph 5 (and for the caller: para. 6). */
7181 if ((sym
->ts
.type
== BT_CHARACTER
&& !sym
->ts
.u
.cl
->length
7182 && (sym
->as
->type
== AS_ASSUMED_SIZE
|| sym
->as
->type
== AS_EXPLICIT
))
7183 || sym
->attr
.contiguous
)
7185 do_copy_inout
= true;
7186 gcc_assert (!sym
->attr
.pointer
);
7189 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc
)))
7190 data
= gfc_conv_descriptor_data_get (gfc_desc
);
7191 else if (!POINTER_TYPE_P (TREE_TYPE (gfc_desc
)))
7192 data
= gfc_build_addr_expr (NULL
, gfc_desc
);
7196 /* Is copy-in/out needed? */
7197 /* do_copyin = rank != 0 && !assumed-size */
7198 tree cond_var
= gfc_create_var (boolean_type_node
, "do_copyin");
7199 tree cond
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
7200 rank
, build_zero_cst (TREE_TYPE (rank
)));
7201 /* dim[rank-1].extent != -1 -> assumed size*/
7202 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
, TREE_TYPE (rank
),
7203 rank
, build_int_cst (TREE_TYPE (rank
), 1));
7204 tmp
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
7205 gfc_get_cfi_dim_extent (cfi
, tmp
),
7206 build_int_cst (gfc_array_index_type
, -1));
7207 cond
= fold_build2_loc (input_location
, TRUTH_ANDIF_EXPR
,
7208 boolean_type_node
, cond
, tmp
);
7209 gfc_add_modify (&block
, cond_var
, cond
);
7210 /* if (do_copyin) do_copyin = ... || ... || ... */
7211 gfc_init_block (&block2
);
7212 /* dim[0].sm != elem_len */
7213 tmp
= fold_convert (gfc_array_index_type
,
7214 gfc_get_cfi_desc_elem_len (cfi
));
7215 cond
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
7216 gfc_get_cfi_dim_sm (cfi
, gfc_index_zero_node
),
7218 gfc_add_modify (&block2
, cond_var
, cond
);
7220 /* for (i = 1; i < rank; ++i)
7221 cond &&= dim[i].sm != (dv->dim[i - 1].sm * dv->dim[i - 1].extent) */
7222 idx
= gfc_create_var (TREE_TYPE (rank
), "idx");
7223 stmtblock_t loop_body
;
7224 gfc_init_block (&loop_body
);
7225 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
, TREE_TYPE (idx
),
7226 idx
, build_int_cst (TREE_TYPE (idx
), 1));
7227 tree tmp2
= gfc_get_cfi_dim_sm (cfi
, tmp
);
7228 tmp
= gfc_get_cfi_dim_extent (cfi
, tmp
);
7229 tmp
= fold_build2_loc (input_location
, MULT_EXPR
, TREE_TYPE (tmp
),
7231 cond
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
7232 gfc_get_cfi_dim_sm (cfi
, idx
), tmp
);
7233 cond
= fold_build2_loc (input_location
, TRUTH_OR_EXPR
, boolean_type_node
,
7235 gfc_add_modify (&loop_body
, cond_var
, cond
);
7236 gfc_simple_for_loop (&block2
, idx
, build_int_cst (TREE_TYPE (idx
), 1),
7237 rank
, LT_EXPR
, build_int_cst (TREE_TYPE (idx
), 1),
7238 gfc_finish_block (&loop_body
));
7239 tmp
= build3_v (COND_EXPR
, cond_var
, gfc_finish_block (&block2
),
7240 build_empty_stmt (input_location
));
7241 gfc_add_expr_to_block (&block
, tmp
);
7244 gfc_init_block (&block2
);
7245 /* size = dim[0].extent; for (i = 1; i < rank; ++i) size *= dim[i].extent */
7246 size_var
= gfc_create_var (size_type_node
, "size");
7247 tmp
= fold_convert (size_type_node
,
7248 gfc_get_cfi_dim_extent (cfi
, gfc_index_zero_node
));
7249 gfc_add_modify (&block2
, size_var
, tmp
);
7251 gfc_init_block (&loop_body
);
7252 tmp
= fold_convert (size_type_node
,
7253 gfc_get_cfi_dim_extent (cfi
, idx
));
7254 tmp
= fold_build2_loc (input_location
, MULT_EXPR
, size_type_node
,
7255 size_var
, fold_convert (size_type_node
, tmp
));
7256 gfc_add_modify (&loop_body
, size_var
, tmp
);
7257 gfc_simple_for_loop (&block2
, idx
, build_int_cst (TREE_TYPE (idx
), 1),
7258 rank
, LT_EXPR
, build_int_cst (TREE_TYPE (idx
), 1),
7259 gfc_finish_block (&loop_body
));
7260 /* data = malloc (size * elem_len) */
7261 tmp
= fold_build2_loc (input_location
, MULT_EXPR
, size_type_node
,
7262 size_var
, gfc_get_cfi_desc_elem_len (cfi
));
7263 tree call
= builtin_decl_explicit (BUILT_IN_MALLOC
);
7264 call
= build_call_expr_loc (input_location
, call
, 1, tmp
);
7265 gfc_add_modify (&block2
, data
, fold_convert (TREE_TYPE (data
), call
));
7268 for (idx = 0; idx < size; ++idx)
7272 for (dim = 0; dim < rank; ++dim)
7274 shift += (tmpidx % extent[d]) * sm[d]
7275 tmpidx = tmpidx / extend[d]
7277 memcpy (lhs + idx*elem_len, rhs + shift, elem_len)
7279 idx
= gfc_create_var (size_type_node
, "arrayidx");
7280 gfc_init_block (&loop_body
);
7281 tree shift
= gfc_create_var (size_type_node
, "shift");
7282 tree tmpidx
= gfc_create_var (size_type_node
, "tmpidx");
7283 gfc_add_modify (&loop_body
, shift
, build_zero_cst (TREE_TYPE (shift
)));
7284 gfc_add_modify (&loop_body
, tmpidx
, idx
);
7285 stmtblock_t inner_loop
;
7286 gfc_init_block (&inner_loop
);
7287 tree dim
= gfc_create_var (TREE_TYPE (rank
), "dim");
7288 /* shift += (tmpidx % extent[d]) * sm[d] */
7289 tmp
= fold_build2_loc (input_location
, TRUNC_MOD_EXPR
,
7290 size_type_node
, tmpidx
,
7291 fold_convert (size_type_node
,
7292 gfc_get_cfi_dim_extent (cfi
, dim
)));
7293 tmp
= fold_build2_loc (input_location
, MULT_EXPR
,
7294 size_type_node
, tmp
,
7295 fold_convert (size_type_node
,
7296 gfc_get_cfi_dim_sm (cfi
, dim
)));
7297 gfc_add_modify (&inner_loop
, shift
,
7298 fold_build2_loc (input_location
, PLUS_EXPR
,
7299 size_type_node
, shift
, tmp
));
7300 /* tmpidx = tmpidx / extend[d] */
7301 tmp
= fold_convert (size_type_node
, gfc_get_cfi_dim_extent (cfi
, dim
));
7302 gfc_add_modify (&inner_loop
, tmpidx
,
7303 fold_build2_loc (input_location
, TRUNC_DIV_EXPR
,
7304 size_type_node
, tmpidx
, tmp
));
7305 gfc_simple_for_loop (&loop_body
, dim
, build_zero_cst (TREE_TYPE (rank
)),
7306 rank
, LT_EXPR
, build_int_cst (TREE_TYPE (dim
), 1),
7307 gfc_finish_block (&inner_loop
));
7309 tmp
= fold_convert (pchar_type_node
, gfc_get_cfi_desc_base_addr (cfi
));
7310 tmp
= fold_build2 (POINTER_PLUS_EXPR
, pchar_type_node
, tmp
, shift
);
7312 /* memcpy (lhs + idx*elem_len, rhs + shift, elem_len) */
7314 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc
)))
7315 elem_len
= gfc_conv_descriptor_elem_len (gfc_desc
);
7317 elem_len
= gfc_get_cfi_desc_elem_len (cfi
);
7318 lhs
= fold_build2_loc (input_location
, MULT_EXPR
, size_type_node
,
7320 lhs
= fold_build2_loc (input_location
, POINTER_PLUS_EXPR
, pchar_type_node
,
7321 fold_convert (pchar_type_node
, data
), lhs
);
7322 tmp
= fold_convert (pvoid_type_node
, tmp
);
7323 lhs
= fold_convert (pvoid_type_node
, lhs
);
7324 call
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
7325 call
= build_call_expr_loc (input_location
, call
, 3, lhs
, tmp
, elem_len
);
7326 gfc_add_expr_to_block (&loop_body
, fold_convert (void_type_node
, call
));
7327 gfc_simple_for_loop (&block2
, idx
, build_zero_cst (TREE_TYPE (idx
)),
7328 size_var
, LT_EXPR
, build_int_cst (TREE_TYPE (idx
), 1),
7329 gfc_finish_block (&loop_body
));
7330 /* if (cond) { block2 } */
7331 tmp
= build3_v (COND_EXPR
, cond_var
, gfc_finish_block (&block2
),
7332 build_empty_stmt (input_location
));
7333 gfc_add_expr_to_block (&block
, tmp
);
7336 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc
)))
7339 type
= TREE_TYPE (gfc_desc
);
7340 gfc_trans_array_bounds (type
, sym
, &offset
, &block
);
7341 if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type
)))
7342 gfc_add_modify (&block
, GFC_TYPE_ARRAY_OFFSET (type
), offset
);
7346 /* If cfi->data != NULL. */
7348 gfc_init_block (&block2
);
7350 /* if do_copy_inout: gfc->dspan = gfc->dtype.elem_len
7351 We use gfc instead of cfi on the RHS as this might be a constant. */
7352 tmp
= fold_convert (gfc_array_index_type
,
7353 gfc_conv_descriptor_elem_len (gfc_desc
));
7356 /* gfc->dspan = ((cfi->dim[0].sm % gfc->elem_len)
7357 ? cfi->dim[0].sm : gfc->elem_len). */
7359 tree tmp2
= gfc_get_cfi_dim_sm (cfi
, gfc_rank_cst
[0]);
7360 cond
= fold_build2_loc (input_location
, TRUNC_MOD_EXPR
,
7361 gfc_array_index_type
, tmp2
, tmp
);
7362 cond
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
7363 cond
, gfc_index_zero_node
);
7364 tmp
= build3_loc (input_location
, COND_EXPR
, gfc_array_index_type
, cond
,
7367 gfc_conv_descriptor_span_set (&block2
, gfc_desc
, tmp
);
7369 /* Calculate offset + set lbound, ubound and stride. */
7370 gfc_conv_descriptor_offset_set (&block2
, gfc_desc
, gfc_index_zero_node
);
7371 if (sym
->as
->rank
> 0 && !sym
->attr
.pointer
&& !sym
->attr
.allocatable
)
7372 for (int i
= 0; i
< sym
->as
->rank
; ++i
)
7375 gfc_init_se (&se
, NULL
);
7376 if (sym
->as
->lower
[i
])
7378 gfc_conv_expr (&se
, sym
->as
->lower
[i
]);
7382 tmp
= gfc_index_one_node
;
7383 gfc_add_block_to_block (&block2
, &se
.pre
);
7384 gfc_conv_descriptor_lbound_set (&block2
, gfc_desc
, gfc_rank_cst
[i
],
7386 gfc_add_block_to_block (&block2
, &se
.post
);
7389 /* Loop: for (i = 0; i < rank; ++i). */
7390 idx
= gfc_create_var (TREE_TYPE (rank
), "idx");
7393 stmtblock_t loop_body
;
7394 gfc_init_block (&loop_body
);
7395 /* gfc->dim[i].lbound = ... */
7396 if (sym
->attr
.pointer
|| sym
->attr
.allocatable
)
7398 tmp
= gfc_get_cfi_dim_lbound (cfi
, idx
);
7399 gfc_conv_descriptor_lbound_set (&loop_body
, gfc_desc
, idx
, tmp
);
7401 else if (sym
->as
->rank
< 0)
7402 gfc_conv_descriptor_lbound_set (&loop_body
, gfc_desc
, idx
,
7403 gfc_index_one_node
);
7405 /* gfc->dim[i].ubound = gfc->dim[i].lbound + cfi->dim[i].extent - 1. */
7406 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
, gfc_array_index_type
,
7407 gfc_conv_descriptor_lbound_get (gfc_desc
, idx
),
7408 gfc_index_one_node
);
7409 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
, gfc_array_index_type
,
7410 gfc_get_cfi_dim_extent (cfi
, idx
), tmp
);
7411 gfc_conv_descriptor_ubound_set (&loop_body
, gfc_desc
, idx
, tmp
);
7415 /* gfc->dim[i].stride
7416 = idx == 0 ? 1 : gfc->dim[i-1].stride * cfi->dim[i-1].extent */
7417 tree cond
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
,
7418 idx
, build_zero_cst (TREE_TYPE (idx
)));
7419 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
, TREE_TYPE (idx
),
7420 idx
, build_int_cst (TREE_TYPE (idx
), 1));
7421 tree tmp2
= gfc_get_cfi_dim_extent (cfi
, tmp
);
7422 tmp
= gfc_conv_descriptor_stride_get (gfc_desc
, tmp
);
7423 tmp
= fold_build2_loc (input_location
, MULT_EXPR
, TREE_TYPE (tmp2
),
7425 tmp
= build3_loc (input_location
, COND_EXPR
, gfc_array_index_type
, cond
,
7426 gfc_index_one_node
, tmp
);
7430 /* gfc->dim[i].stride = cfi->dim[i].sm / cfi>elem_len */
7431 tmp
= gfc_get_cfi_dim_sm (cfi
, idx
);
7432 tmp
= fold_build2_loc (input_location
, TRUNC_DIV_EXPR
,
7433 gfc_array_index_type
, tmp
,
7434 fold_convert (gfc_array_index_type
,
7435 gfc_get_cfi_desc_elem_len (cfi
)));
7437 gfc_conv_descriptor_stride_set (&loop_body
, gfc_desc
, idx
, tmp
);
7438 /* gfc->offset -= gfc->dim[i].stride * gfc->dim[i].lbound. */
7439 tmp
= fold_build2_loc (input_location
, MULT_EXPR
, gfc_array_index_type
,
7440 gfc_conv_descriptor_stride_get (gfc_desc
, idx
),
7441 gfc_conv_descriptor_lbound_get (gfc_desc
, idx
));
7442 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
, gfc_array_index_type
,
7443 gfc_conv_descriptor_offset_get (gfc_desc
), tmp
);
7444 gfc_conv_descriptor_offset_set (&loop_body
, gfc_desc
, tmp
);
7446 /* Generate loop. */
7447 gfc_simple_for_loop (&block2
, idx
, build_zero_cst (TREE_TYPE (idx
)),
7448 rank
, LT_EXPR
, build_int_cst (TREE_TYPE (idx
), 1),
7449 gfc_finish_block (&loop_body
));
7450 if (sym
->attr
.allocatable
|| sym
->attr
.pointer
)
7452 tmp
= gfc_get_cfi_desc_base_addr (cfi
),
7453 tmp
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
7454 tmp
, null_pointer_node
);
7455 tmp
= build3_v (COND_EXPR
, tmp
, gfc_finish_block (&block2
),
7456 build_empty_stmt (input_location
));
7457 gfc_add_expr_to_block (&block
, tmp
);
7460 gfc_add_block_to_block (&block
, &block2
);
7463 /* If optional arg: 'if (arg) { block } else { local_arg = NULL; }'. */
7464 if (sym
->attr
.optional
)
7466 tree present
= fold_build2_loc (input_location
, NE_EXPR
,
7467 boolean_type_node
, cfi_desc
,
7469 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, void_type_node
,
7471 fold_convert (TREE_TYPE (sym
->backend_decl
),
7472 null_pointer_node
));
7473 tmp
= build3_v (COND_EXPR
, present
, gfc_finish_block (&block
), tmp
);
7474 gfc_add_expr_to_block (init
, tmp
);
7477 gfc_add_block_to_block (init
, &block
);
7479 if (!sym
->attr
.referenced
)
7482 /* If pointer not changed, nothing to be done (except copy out) */
7483 if (!do_copy_inout
&& ((!sym
->attr
.pointer
&& !sym
->attr
.allocatable
)
7484 || sym
->attr
.intent
== INTENT_IN
))
7487 gfc_init_block (&block
);
7489 /* For bind(C), Fortran does not permit mixing 'pointer' with 'contiguous' (or
7490 len=*). Thus, when copy out is needed, the bounds ofthe descriptor remain
7495 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc
)))
7496 data
= gfc_conv_descriptor_data_get (gfc_desc
);
7497 else if (!POINTER_TYPE_P (TREE_TYPE (gfc_desc
)))
7498 data
= gfc_build_addr_expr (NULL
, gfc_desc
);
7501 gfc_init_block (&block2
);
7502 if (sym
->attr
.intent
!= INTENT_IN
)
7504 /* First, create the inner copy-out loop.
7505 for (idx = 0; idx < size; ++idx)
7509 for (dim = 0; dim < rank; ++dim)
7511 shift += (tmpidx % extent[d]) * sm[d]
7512 tmpidx = tmpidx / extend[d]
7514 memcpy (lhs + shift, rhs + idx*elem_len, elem_len)
7516 stmtblock_t loop_body
;
7517 idx
= gfc_create_var (size_type_node
, "arrayidx");
7518 gfc_init_block (&loop_body
);
7519 tree shift
= gfc_create_var (size_type_node
, "shift");
7520 tree tmpidx
= gfc_create_var (size_type_node
, "tmpidx");
7521 gfc_add_modify (&loop_body
, shift
,
7522 build_zero_cst (TREE_TYPE (shift
)));
7523 gfc_add_modify (&loop_body
, tmpidx
, idx
);
7524 stmtblock_t inner_loop
;
7525 gfc_init_block (&inner_loop
);
7526 tree dim
= gfc_create_var (TREE_TYPE (rank
), "dim");
7527 /* shift += (tmpidx % extent[d]) * sm[d] */
7528 tmp
= fold_convert (size_type_node
,
7529 gfc_get_cfi_dim_extent (cfi
, dim
));
7530 tmp
= fold_build2_loc (input_location
, TRUNC_MOD_EXPR
,
7531 size_type_node
, tmpidx
, tmp
);
7532 tmp
= fold_build2_loc (input_location
, MULT_EXPR
,
7533 size_type_node
, tmp
,
7534 fold_convert (size_type_node
,
7535 gfc_get_cfi_dim_sm (cfi
, dim
)));
7536 gfc_add_modify (&inner_loop
, shift
,
7537 fold_build2_loc (input_location
, PLUS_EXPR
,
7538 size_type_node
, shift
, tmp
));
7539 /* tmpidx = tmpidx / extend[d] */
7540 tmp
= fold_convert (size_type_node
,
7541 gfc_get_cfi_dim_extent (cfi
, dim
));
7542 gfc_add_modify (&inner_loop
, tmpidx
,
7543 fold_build2_loc (input_location
, TRUNC_DIV_EXPR
,
7544 size_type_node
, tmpidx
, tmp
));
7545 gfc_simple_for_loop (&loop_body
, dim
,
7546 build_zero_cst (TREE_TYPE (rank
)), rank
, LT_EXPR
,
7547 build_int_cst (TREE_TYPE (dim
), 1),
7548 gfc_finish_block (&inner_loop
));
7551 tmp
= fold_convert (pchar_type_node
,
7552 gfc_get_cfi_desc_base_addr (cfi
));
7553 tmp
= fold_build2 (POINTER_PLUS_EXPR
, pchar_type_node
, tmp
, shift
);
7554 /* memcpy (lhs + shift, rhs + idx*elem_len, elem_len) */
7556 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc
)))
7557 elem_len
= gfc_conv_descriptor_elem_len (gfc_desc
);
7559 elem_len
= gfc_get_cfi_desc_elem_len (cfi
);
7560 rhs
= fold_build2_loc (input_location
, MULT_EXPR
, size_type_node
,
7562 rhs
= fold_build2_loc (input_location
, POINTER_PLUS_EXPR
,
7564 fold_convert (pchar_type_node
, data
), rhs
);
7565 tmp
= fold_convert (pvoid_type_node
, tmp
);
7566 rhs
= fold_convert (pvoid_type_node
, rhs
);
7567 call
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
7568 call
= build_call_expr_loc (input_location
, call
, 3, tmp
, rhs
,
7570 gfc_add_expr_to_block (&loop_body
,
7571 fold_convert (void_type_node
, call
));
7572 gfc_simple_for_loop (&block2
, idx
, build_zero_cst (TREE_TYPE (idx
)),
7574 build_int_cst (TREE_TYPE (idx
), 1),
7575 gfc_finish_block (&loop_body
));
7577 call
= builtin_decl_explicit (BUILT_IN_FREE
);
7578 call
= build_call_expr_loc (input_location
, call
, 1, data
);
7579 gfc_add_expr_to_block (&block2
, call
);
7581 /* if (cfi->base_addr != gfc->data) { copy out; free(var) }; return */
7582 tree tmp2
= gfc_get_cfi_desc_base_addr (cfi
);
7583 tmp2
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
7584 tmp2
, fold_convert (TREE_TYPE (tmp2
), data
));
7585 tmp
= build3_v (COND_EXPR
, tmp2
, gfc_finish_block (&block2
),
7586 build_empty_stmt (input_location
));
7587 gfc_add_expr_to_block (&block
, tmp
);
7591 /* Update pointer + array data data on exit. */
7592 tmp
= gfc_get_cfi_desc_base_addr (cfi
);
7593 tmp2
= (!sym
->attr
.dimension
7594 ? gfc_desc
: gfc_conv_descriptor_data_get (gfc_desc
));
7595 gfc_add_modify (&block
, tmp
, fold_convert (TREE_TYPE (tmp
), tmp2
));
7597 /* Set string length for len=:, only. */
7598 if (sym
->ts
.type
== BT_CHARACTER
&& !sym
->ts
.u
.cl
->length
)
7600 tmp2
= gfc_get_cfi_desc_elem_len (cfi
);
7601 tmp
= fold_convert (TREE_TYPE (tmp2
), sym
->ts
.u
.cl
->backend_decl
);
7602 if (sym
->ts
.kind
!= 1)
7603 tmp
= fold_build2_loc (input_location
, MULT_EXPR
,
7604 TREE_TYPE (tmp2
), tmp
,
7605 build_int_cst (TREE_TYPE (tmp2
), sym
->ts
.kind
));
7606 gfc_add_modify (&block
, tmp2
, tmp
);
7609 if (!sym
->attr
.dimension
)
7612 gfc_init_block (&block2
);
7614 /* Loop: for (i = 0; i < rank; ++i). */
7615 idx
= gfc_create_var (TREE_TYPE (rank
), "idx");
7618 gfc_init_block (&loop_body
);
7619 /* cfi->dim[i].lower_bound = gfc->dim[i].lbound */
7620 gfc_add_modify (&loop_body
, gfc_get_cfi_dim_lbound (cfi
, idx
),
7621 gfc_conv_descriptor_lbound_get (gfc_desc
, idx
));
7622 /* cfi->dim[i].extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1. */
7623 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
, gfc_array_index_type
,
7624 gfc_conv_descriptor_ubound_get (gfc_desc
, idx
),
7625 gfc_conv_descriptor_lbound_get (gfc_desc
, idx
));
7626 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
, gfc_array_index_type
, tmp
,
7627 gfc_index_one_node
);
7628 gfc_add_modify (&loop_body
, gfc_get_cfi_dim_extent (cfi
, idx
), tmp
);
7629 /* d->dim[n].sm = gfc->dim[i].stride * gfc->span); */
7630 tmp
= fold_build2_loc (input_location
, MULT_EXPR
, gfc_array_index_type
,
7631 gfc_conv_descriptor_stride_get (gfc_desc
, idx
),
7632 gfc_conv_descriptor_span_get (gfc_desc
));
7633 gfc_add_modify (&loop_body
, gfc_get_cfi_dim_sm (cfi
, idx
), tmp
);
7635 /* Generate loop. */
7636 gfc_simple_for_loop (&block2
, idx
, build_zero_cst (TREE_TYPE (idx
)),
7637 rank
, LT_EXPR
, build_int_cst (TREE_TYPE (idx
), 1),
7638 gfc_finish_block (&loop_body
));
7639 /* if (gfc->data != NULL) { block2 }. */
7640 tmp
= gfc_get_cfi_desc_base_addr (cfi
),
7641 tmp
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
7642 tmp
, null_pointer_node
);
7643 tmp
= build3_v (COND_EXPR
, tmp
, gfc_finish_block (&block2
),
7644 build_empty_stmt (input_location
));
7645 gfc_add_expr_to_block (&block
, tmp
);
7648 /* If optional arg: 'if (arg) { block } else { local_arg = NULL; }'. */
7649 if (sym
->attr
.optional
)
7651 tree present
= fold_build2_loc (input_location
, NE_EXPR
,
7652 boolean_type_node
, cfi_desc
,
7654 tmp
= build3_v (COND_EXPR
, present
, gfc_finish_block (&block
),
7655 build_empty_stmt (input_location
));
7656 gfc_add_expr_to_block (finally
, tmp
);
7659 gfc_add_block_to_block (finally
, &block
);
7662 /* Generate code for a function. */
7665 gfc_generate_function_code (gfc_namespace
* ns
)
7671 tree fpstate
= NULL_TREE
;
7672 stmtblock_t init
, cleanup
, outer_block
;
7674 gfc_wrapped_block try_block
;
7675 tree recurcheckvar
= NULL_TREE
;
7677 gfc_symbol
*previous_procedure_symbol
;
7681 sym
= ns
->proc_name
;
7682 previous_procedure_symbol
= current_procedure_symbol
;
7683 current_procedure_symbol
= sym
;
7685 /* Initialize sym->tlink so that gfc_trans_deferred_vars does not get
7689 /* Create the declaration for functions with global scope. */
7690 if (!sym
->backend_decl
)
7691 gfc_create_function_decl (ns
, false);
7693 fndecl
= sym
->backend_decl
;
7694 old_context
= current_function_decl
;
7698 push_function_context ();
7699 saved_parent_function_decls
= saved_function_decls
;
7700 saved_function_decls
= NULL_TREE
;
7703 trans_function_start (sym
);
7704 gfc_current_locus
= sym
->declared_at
;
7706 gfc_init_block (&init
);
7707 gfc_init_block (&cleanup
);
7708 gfc_init_block (&outer_block
);
7710 if (ns
->entries
&& ns
->proc_name
->ts
.type
== BT_CHARACTER
)
7712 /* Copy length backend_decls to all entry point result
7717 gfc_conv_const_charlen (ns
->proc_name
->ts
.u
.cl
);
7718 backend_decl
= ns
->proc_name
->result
->ts
.u
.cl
->backend_decl
;
7719 for (el
= ns
->entries
; el
; el
= el
->next
)
7720 el
->sym
->result
->ts
.u
.cl
->backend_decl
= backend_decl
;
7723 /* Translate COMMON blocks. */
7724 gfc_trans_common (ns
);
7726 /* Null the parent fake result declaration if this namespace is
7727 a module function or an external procedures. */
7728 if ((ns
->parent
&& ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
7729 || ns
->parent
== NULL
)
7730 parent_fake_result_decl
= NULL_TREE
;
7733 - deallocate intent-out allocatable dummy arguments.
7734 - Create GFC variable which will later be populated by convert_CFI_desc */
7735 if (sym
->attr
.is_bind_c
)
7736 for (gfc_formal_arglist
*formal
= gfc_sym_get_dummy_args (sym
);
7737 formal
; formal
= formal
->next
)
7739 gfc_symbol
*fsym
= formal
->sym
;
7740 if (!is_CFI_desc (fsym
, NULL
))
7742 if (!fsym
->attr
.referenced
)
7744 gfc_conv_cfi_to_gfc (&init
, &cleanup
, fsym
->backend_decl
,
7748 /* Let's now create a local GFI descriptor. Afterwards:
7749 desc is the local descriptor,
7750 desc_p is a pointer to it
7751 and stored in sym->backend_decl
7752 GFC_DECL_SAVED_DESCRIPTOR (desc_p) contains the CFI descriptor
7753 -> PARM_DECL and before sym->backend_decl.
7754 For scalars, decl == decl_p is a pointer variable. */
7756 location_t loc
= gfc_get_location (&sym
->declared_at
);
7757 if (fsym
->ts
.type
== BT_CHARACTER
&& !fsym
->ts
.u
.cl
->length
)
7758 fsym
->ts
.u
.cl
->backend_decl
= gfc_create_var (gfc_array_index_type
,
7760 else if (fsym
->ts
.type
== BT_CHARACTER
&& !fsym
->ts
.u
.cl
->backend_decl
)
7763 gfc_init_se (&se
, NULL
);
7764 gfc_conv_expr (&se
, fsym
->ts
.u
.cl
->length
);
7765 gfc_add_block_to_block (&init
, &se
.pre
);
7766 fsym
->ts
.u
.cl
->backend_decl
= se
.expr
;
7767 gcc_assert(se
.post
.head
== NULL_TREE
);
7769 /* Nullify, otherwise gfc_sym_type will return the CFI type. */
7770 tree tmp
= fsym
->backend_decl
;
7771 fsym
->backend_decl
= NULL
;
7772 tree type
= gfc_sym_type (fsym
);
7773 gcc_assert (POINTER_TYPE_P (type
));
7774 if (POINTER_TYPE_P (TREE_TYPE (type
)))
7775 /* For instance, allocatable scalars. */
7776 type
= TREE_TYPE (type
);
7777 if (TREE_CODE (type
) == REFERENCE_TYPE
)
7778 type
= build_pointer_type (TREE_TYPE (type
));
7779 desc_p
= build_decl (loc
, VAR_DECL
, get_identifier (fsym
->name
), type
);
7780 if (!fsym
->attr
.dimension
)
7782 else if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (desc_p
))))
7784 /* Character(len=*) explicit-size/assumed-size array. */
7786 gfc_build_qualified_array (desc
, fsym
);
7790 tree size
= size_in_bytes (TREE_TYPE (TREE_TYPE (desc_p
)));
7791 tree call
= builtin_decl_explicit (BUILT_IN_ALLOCA
);
7792 call
= build_call_expr_loc (input_location
, call
, 1, size
);
7793 gfc_add_modify (&outer_block
, desc_p
,
7794 fold_convert (TREE_TYPE(desc_p
), call
));
7795 desc
= build_fold_indirect_ref_loc (input_location
, desc_p
);
7798 if (fsym
->attr
.optional
)
7800 gfc_allocate_lang_decl (desc_p
);
7801 GFC_DECL_OPTIONAL_ARGUMENT (desc_p
) = 1;
7803 fsym
->backend_decl
= desc_p
;
7804 gfc_conv_cfi_to_gfc (&init
, &cleanup
, tmp
, desc
, fsym
);
7807 gfc_generate_contained_functions (ns
);
7809 has_coarray_vars
= false;
7810 generate_local_vars (ns
);
7812 if (flag_coarray
== GFC_FCOARRAY_LIB
&& has_coarray_vars
)
7813 generate_coarray_init (ns
);
7815 /* Keep the parent fake result declaration in module functions
7816 or external procedures. */
7817 if ((ns
->parent
&& ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
7818 || ns
->parent
== NULL
)
7819 current_fake_result_decl
= parent_fake_result_decl
;
7821 current_fake_result_decl
= NULL_TREE
;
7823 is_recursive
= sym
->attr
.recursive
7824 || (sym
->attr
.entry_master
7825 && sym
->ns
->entries
->sym
->attr
.recursive
);
7826 if ((gfc_option
.rtcheck
& GFC_RTCHECK_RECURSION
)
7827 && !is_recursive
&& !flag_recursive
&& !sym
->attr
.artificial
)
7831 msg
= xasprintf ("Recursive call to nonrecursive procedure '%s'",
7833 recurcheckvar
= gfc_create_var (logical_type_node
, "is_recursive");
7834 TREE_STATIC (recurcheckvar
) = 1;
7835 DECL_INITIAL (recurcheckvar
) = logical_false_node
;
7836 gfc_add_expr_to_block (&init
, recurcheckvar
);
7837 gfc_trans_runtime_check (true, false, recurcheckvar
, &init
,
7838 &sym
->declared_at
, msg
);
7839 gfc_add_modify (&init
, recurcheckvar
, logical_true_node
);
7843 /* Check if an IEEE module is used in the procedure. If so, save
7844 the floating point state. */
7845 ieee
= is_ieee_module_used (ns
);
7847 fpstate
= gfc_save_fp_state (&init
);
7849 /* Now generate the code for the body of this function. */
7850 gfc_init_block (&body
);
7852 if (TREE_TYPE (DECL_RESULT (fndecl
)) != void_type_node
7853 && sym
->attr
.subroutine
)
7855 tree alternate_return
;
7856 alternate_return
= gfc_get_fake_result_decl (sym
, 0);
7857 gfc_add_modify (&body
, alternate_return
, integer_zero_node
);
7862 /* Jump to the correct entry point. */
7863 tmp
= gfc_trans_entry_master_switch (ns
->entries
);
7864 gfc_add_expr_to_block (&body
, tmp
);
7867 /* If bounds-checking is enabled, generate code to check passed in actual
7868 arguments against the expected dummy argument attributes (e.g. string
7870 if ((gfc_option
.rtcheck
& GFC_RTCHECK_BOUNDS
) && !sym
->attr
.is_bind_c
)
7871 add_argument_checking (&body
, sym
);
7873 finish_oacc_declare (ns
, sym
, false);
7875 tmp
= gfc_trans_code (ns
->code
);
7876 gfc_add_expr_to_block (&body
, tmp
);
7878 if (TREE_TYPE (DECL_RESULT (fndecl
)) != void_type_node
7879 || (sym
->result
&& sym
->result
!= sym
7880 && sym
->result
->ts
.type
== BT_DERIVED
7881 && sym
->result
->ts
.u
.derived
->attr
.alloc_comp
))
7883 bool artificial_result_decl
= false;
7884 tree result
= get_proc_result (sym
);
7885 gfc_symbol
*rsym
= sym
== sym
->result
? sym
: sym
->result
;
7887 /* Make sure that a function returning an object with
7888 alloc/pointer_components always has a result, where at least
7889 the allocatable/pointer components are set to zero. */
7890 if (result
== NULL_TREE
&& sym
->attr
.function
7891 && ((sym
->result
->ts
.type
== BT_DERIVED
7892 && (sym
->attr
.allocatable
7893 || sym
->attr
.pointer
7894 || sym
->result
->ts
.u
.derived
->attr
.alloc_comp
7895 || sym
->result
->ts
.u
.derived
->attr
.pointer_comp
))
7896 || (sym
->result
->ts
.type
== BT_CLASS
7897 && (CLASS_DATA (sym
)->attr
.allocatable
7898 || CLASS_DATA (sym
)->attr
.class_pointer
7899 || CLASS_DATA (sym
->result
)->attr
.alloc_comp
7900 || CLASS_DATA (sym
->result
)->attr
.pointer_comp
))))
7902 artificial_result_decl
= true;
7903 result
= gfc_get_fake_result_decl (sym
, 0);
7906 if (result
!= NULL_TREE
&& sym
->attr
.function
&& !sym
->attr
.pointer
)
7908 if (sym
->attr
.allocatable
&& sym
->attr
.dimension
== 0
7909 && sym
->result
== sym
)
7910 gfc_add_modify (&init
, result
, fold_convert (TREE_TYPE (result
),
7911 null_pointer_node
));
7912 else if (sym
->ts
.type
== BT_CLASS
7913 && CLASS_DATA (sym
)->attr
.allocatable
7914 && CLASS_DATA (sym
)->attr
.dimension
== 0
7915 && sym
->result
== sym
)
7917 tmp
= CLASS_DATA (sym
)->backend_decl
;
7918 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
,
7919 TREE_TYPE (tmp
), result
, tmp
, NULL_TREE
);
7920 gfc_add_modify (&init
, tmp
, fold_convert (TREE_TYPE (tmp
),
7921 null_pointer_node
));
7923 else if (sym
->ts
.type
== BT_DERIVED
7924 && !sym
->attr
.allocatable
)
7927 /* Arrays are not initialized using the default initializer of
7928 their elements. Therefore only check if a default
7929 initializer is available when the result is scalar. */
7930 init_exp
= rsym
->as
? NULL
7931 : gfc_generate_initializer (&rsym
->ts
, true);
7934 tmp
= gfc_trans_structure_assign (result
, init_exp
, 0);
7935 gfc_free_expr (init_exp
);
7936 gfc_add_expr_to_block (&init
, tmp
);
7938 else if (rsym
->ts
.u
.derived
->attr
.alloc_comp
)
7940 rank
= rsym
->as
? rsym
->as
->rank
: 0;
7941 tmp
= gfc_nullify_alloc_comp (rsym
->ts
.u
.derived
, result
,
7943 gfc_prepend_expr_to_block (&body
, tmp
);
7948 if (result
== NULL_TREE
|| artificial_result_decl
)
7950 /* TODO: move to the appropriate place in resolve.cc. */
7951 if (warn_return_type
> 0 && sym
== sym
->result
)
7952 gfc_warning (OPT_Wreturn_type
,
7953 "Return value of function %qs at %L not set",
7954 sym
->name
, &sym
->declared_at
);
7955 if (warn_return_type
> 0)
7956 suppress_warning (sym
->backend_decl
);
7958 if (result
!= NULL_TREE
)
7959 gfc_add_expr_to_block (&body
, gfc_generate_return ());
7962 /* Reset recursion-check variable. */
7963 if (recurcheckvar
!= NULL_TREE
)
7965 gfc_add_modify (&cleanup
, recurcheckvar
, logical_false_node
);
7966 recurcheckvar
= NULL
;
7969 /* If IEEE modules are loaded, restore the floating-point state. */
7971 gfc_restore_fp_state (&cleanup
, fpstate
);
7973 /* Finish the function body and add init and cleanup code. */
7974 tmp
= gfc_finish_block (&body
);
7975 /* Add code to create and cleanup arrays. */
7976 gfc_start_wrapped_block (&try_block
, tmp
);
7977 gfc_trans_deferred_vars (sym
, &try_block
);
7978 gfc_add_init_cleanup (&try_block
, gfc_finish_block (&init
),
7979 gfc_finish_block (&cleanup
));
7981 /* Add all the decls we created during processing. */
7982 decl
= nreverse (saved_function_decls
);
7987 next
= DECL_CHAIN (decl
);
7988 DECL_CHAIN (decl
) = NULL_TREE
;
7992 saved_function_decls
= NULL_TREE
;
7994 gfc_add_expr_to_block (&outer_block
, gfc_finish_wrapped_block (&try_block
));
7995 DECL_SAVED_TREE (fndecl
) = gfc_finish_block (&outer_block
);
7998 /* Finish off this function and send it for code generation. */
8000 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
8002 DECL_SAVED_TREE (fndecl
)
8003 = fold_build3_loc (DECL_SOURCE_LOCATION (fndecl
), BIND_EXPR
, void_type_node
,
8004 decl
, DECL_SAVED_TREE (fndecl
), DECL_INITIAL (fndecl
));
8006 /* Output the GENERIC tree. */
8007 dump_function (TDI_original
, fndecl
);
8009 /* Store the end of the function, so that we get good line number
8010 info for the epilogue. */
8011 cfun
->function_end_locus
= input_location
;
8013 /* We're leaving the context of this function, so zap cfun.
8014 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
8015 tree_rest_of_compilation. */
8020 pop_function_context ();
8021 saved_function_decls
= saved_parent_function_decls
;
8023 current_function_decl
= old_context
;
8025 if (decl_function_context (fndecl
))
8027 /* Register this function with cgraph just far enough to get it
8028 added to our parent's nested function list.
8029 If there are static coarrays in this function, the nested _caf_init
8030 function has already called cgraph_create_node, which also created
8031 the cgraph node for this function. */
8032 if (!has_coarray_vars
|| flag_coarray
!= GFC_FCOARRAY_LIB
)
8033 (void) cgraph_node::get_create (fndecl
);
8036 cgraph_node::finalize_function (fndecl
, true);
8038 gfc_trans_use_stmts (ns
);
8039 gfc_traverse_ns (ns
, gfc_emit_parameter_debug_info
);
8041 if (sym
->attr
.is_main_program
)
8042 create_main_function (fndecl
);
8044 current_procedure_symbol
= previous_procedure_symbol
;
8049 gfc_generate_constructors (void)
8051 gcc_assert (gfc_static_ctors
== NULL_TREE
);
8059 if (gfc_static_ctors
== NULL_TREE
)
8062 fnname
= get_file_function_name ("I");
8063 type
= build_function_type_list (void_type_node
, NULL_TREE
);
8065 fndecl
= build_decl (input_location
,
8066 FUNCTION_DECL
, fnname
, type
);
8067 TREE_PUBLIC (fndecl
) = 1;
8069 decl
= build_decl (input_location
,
8070 RESULT_DECL
, NULL_TREE
, void_type_node
);
8071 DECL_ARTIFICIAL (decl
) = 1;
8072 DECL_IGNORED_P (decl
) = 1;
8073 DECL_CONTEXT (decl
) = fndecl
;
8074 DECL_RESULT (fndecl
) = decl
;
8078 current_function_decl
= fndecl
;
8080 rest_of_decl_compilation (fndecl
, 1, 0);
8082 make_decl_rtl (fndecl
);
8084 allocate_struct_function (fndecl
, false);
8088 for (; gfc_static_ctors
; gfc_static_ctors
= TREE_CHAIN (gfc_static_ctors
))
8090 tmp
= build_call_expr_loc (input_location
,
8091 TREE_VALUE (gfc_static_ctors
), 0);
8092 DECL_SAVED_TREE (fndecl
) = build_stmt (input_location
, EXPR_STMT
, tmp
);
8098 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
8099 DECL_SAVED_TREE (fndecl
)
8100 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (fndecl
),
8101 DECL_INITIAL (fndecl
));
8103 free_after_parsing (cfun
);
8104 free_after_compilation (cfun
);
8106 tree_rest_of_compilation (fndecl
);
8108 current_function_decl
= NULL_TREE
;
8112 /* Translates a BLOCK DATA program unit. This means emitting the
8113 commons contained therein plus their initializations. We also emit
8114 a globally visible symbol to make sure that each BLOCK DATA program
8115 unit remains unique. */
8118 gfc_generate_block_data (gfc_namespace
* ns
)
8123 /* Tell the backend the source location of the block data. */
8125 gfc_set_backend_locus (&ns
->proc_name
->declared_at
);
8127 gfc_set_backend_locus (&gfc_current_locus
);
8129 /* Process the DATA statements. */
8130 gfc_trans_common (ns
);
8132 /* Create a global symbol with the mane of the block data. This is to
8133 generate linker errors if the same name is used twice. It is never
8136 id
= gfc_sym_mangled_function_id (ns
->proc_name
);
8138 id
= get_identifier ("__BLOCK_DATA__");
8140 decl
= build_decl (input_location
,
8141 VAR_DECL
, id
, gfc_array_index_type
);
8142 TREE_PUBLIC (decl
) = 1;
8143 TREE_STATIC (decl
) = 1;
8144 DECL_IGNORED_P (decl
) = 1;
8147 rest_of_decl_compilation (decl
, 1, 0);
8151 /* Process the local variables of a BLOCK construct. */
8154 gfc_process_block_locals (gfc_namespace
* ns
)
8158 saved_local_decls
= NULL_TREE
;
8159 has_coarray_vars
= false;
8161 generate_local_vars (ns
);
8163 if (flag_coarray
== GFC_FCOARRAY_LIB
&& has_coarray_vars
)
8164 generate_coarray_init (ns
);
8166 decl
= nreverse (saved_local_decls
);
8171 next
= DECL_CHAIN (decl
);
8172 DECL_CHAIN (decl
) = NULL_TREE
;
8176 saved_local_decls
= NULL_TREE
;
8180 #include "gt-fortran-trans-decl.h"