Extend fold_vec_perm to handle VLA vector_cst.
[official-gcc.git] / gcc / fortran / trans-decl.cc
blobb0fd25e92a3bef9237849be4b0810adc8b2876a0
1 /* Backend function setup
2 Copyright (C) 2002-2023 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
10 version.
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
15 for more details.
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 */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "target.h"
27 #include "function.h"
28 #include "tree.h"
29 #include "gfortran.h"
30 #include "gimple-expr.h" /* For create_tmp_var_raw. */
31 #include "trans.h"
32 #include "stringpool.h"
33 #include "cgraph.h"
34 #include "fold-const.h"
35 #include "stor-layout.h"
36 #include "varasm.h"
37 #include "attribs.h"
38 #include "dumpfile.h"
39 #include "toplev.h" /* For announce_function. */
40 #include "debug.h"
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"
48 #include "gimplify.h"
49 #include "omp-general.h"
50 #include "attr-fnspec.h"
52 #define MAX_LABEL_VALUE 99999
55 /* Holds the result of the function if no result variable specified. */
57 static GTY(()) tree current_fake_result_decl;
58 static GTY(()) tree parent_fake_result_decl;
61 /* Holds the variable DECLs for the current function. */
63 static GTY(()) tree saved_function_decls;
64 static GTY(()) tree saved_parent_function_decls;
66 /* Holds the variable DECLs that are locals. */
68 static GTY(()) tree saved_local_decls;
70 /* The namespace of the module we're currently generating. Only used while
71 outputting decls for module variables. Do not rely on this being set. */
73 static gfc_namespace *module_namespace;
75 /* The currently processed procedure symbol. */
76 static gfc_symbol* current_procedure_symbol = NULL;
78 /* The currently processed module. */
79 static struct module_htab_entry *cur_module;
81 /* With -fcoarray=lib: For generating the registering call
82 of static coarrays. */
83 static bool has_coarray_vars;
84 static stmtblock_t caf_init_block;
87 /* List of static constructor functions. */
89 tree gfc_static_ctors;
92 /* Whether we've seen a symbol from an IEEE module in the namespace. */
93 static int seen_ieee_symbol;
95 /* Function declarations for builtin library functions. */
97 tree gfor_fndecl_pause_numeric;
98 tree gfor_fndecl_pause_string;
99 tree gfor_fndecl_stop_numeric;
100 tree gfor_fndecl_stop_string;
101 tree gfor_fndecl_error_stop_numeric;
102 tree gfor_fndecl_error_stop_string;
103 tree gfor_fndecl_runtime_error;
104 tree gfor_fndecl_runtime_error_at;
105 tree gfor_fndecl_runtime_warning_at;
106 tree gfor_fndecl_os_error_at;
107 tree gfor_fndecl_generate_error;
108 tree gfor_fndecl_set_args;
109 tree gfor_fndecl_set_fpe;
110 tree gfor_fndecl_set_options;
111 tree gfor_fndecl_set_convert;
112 tree gfor_fndecl_set_record_marker;
113 tree gfor_fndecl_set_max_subrecord_length;
114 tree gfor_fndecl_ctime;
115 tree gfor_fndecl_fdate;
116 tree gfor_fndecl_ttynam;
117 tree gfor_fndecl_in_pack;
118 tree gfor_fndecl_in_unpack;
119 tree gfor_fndecl_associated;
120 tree gfor_fndecl_system_clock4;
121 tree gfor_fndecl_system_clock8;
122 tree gfor_fndecl_ieee_procedure_entry;
123 tree gfor_fndecl_ieee_procedure_exit;
125 /* Coarray run-time library function decls. */
126 tree gfor_fndecl_caf_init;
127 tree gfor_fndecl_caf_finalize;
128 tree gfor_fndecl_caf_this_image;
129 tree gfor_fndecl_caf_num_images;
130 tree gfor_fndecl_caf_register;
131 tree gfor_fndecl_caf_deregister;
132 tree gfor_fndecl_caf_get;
133 tree gfor_fndecl_caf_send;
134 tree gfor_fndecl_caf_sendget;
135 tree gfor_fndecl_caf_get_by_ref;
136 tree gfor_fndecl_caf_send_by_ref;
137 tree gfor_fndecl_caf_sendget_by_ref;
138 tree gfor_fndecl_caf_sync_all;
139 tree gfor_fndecl_caf_sync_memory;
140 tree gfor_fndecl_caf_sync_images;
141 tree gfor_fndecl_caf_stop_str;
142 tree gfor_fndecl_caf_stop_numeric;
143 tree gfor_fndecl_caf_error_stop;
144 tree gfor_fndecl_caf_error_stop_str;
145 tree gfor_fndecl_caf_atomic_def;
146 tree gfor_fndecl_caf_atomic_ref;
147 tree gfor_fndecl_caf_atomic_cas;
148 tree gfor_fndecl_caf_atomic_op;
149 tree gfor_fndecl_caf_lock;
150 tree gfor_fndecl_caf_unlock;
151 tree gfor_fndecl_caf_event_post;
152 tree gfor_fndecl_caf_event_wait;
153 tree gfor_fndecl_caf_event_query;
154 tree gfor_fndecl_caf_fail_image;
155 tree gfor_fndecl_caf_failed_images;
156 tree gfor_fndecl_caf_image_status;
157 tree gfor_fndecl_caf_stopped_images;
158 tree gfor_fndecl_caf_form_team;
159 tree gfor_fndecl_caf_change_team;
160 tree gfor_fndecl_caf_end_team;
161 tree gfor_fndecl_caf_sync_team;
162 tree gfor_fndecl_caf_get_team;
163 tree gfor_fndecl_caf_team_number;
164 tree gfor_fndecl_co_broadcast;
165 tree gfor_fndecl_co_max;
166 tree gfor_fndecl_co_min;
167 tree gfor_fndecl_co_reduce;
168 tree gfor_fndecl_co_sum;
169 tree gfor_fndecl_caf_is_present;
170 tree gfor_fndecl_caf_random_init;
173 /* Math functions. Many other math functions are handled in
174 trans-intrinsic.cc. */
176 gfc_powdecl_list gfor_fndecl_math_powi[4][3];
177 tree gfor_fndecl_math_ishftc4;
178 tree gfor_fndecl_math_ishftc8;
179 tree gfor_fndecl_math_ishftc16;
182 /* String functions. */
184 tree gfor_fndecl_compare_string;
185 tree gfor_fndecl_concat_string;
186 tree gfor_fndecl_string_len_trim;
187 tree gfor_fndecl_string_index;
188 tree gfor_fndecl_string_scan;
189 tree gfor_fndecl_string_verify;
190 tree gfor_fndecl_string_trim;
191 tree gfor_fndecl_string_minmax;
192 tree gfor_fndecl_adjustl;
193 tree gfor_fndecl_adjustr;
194 tree gfor_fndecl_select_string;
195 tree gfor_fndecl_compare_string_char4;
196 tree gfor_fndecl_concat_string_char4;
197 tree gfor_fndecl_string_len_trim_char4;
198 tree gfor_fndecl_string_index_char4;
199 tree gfor_fndecl_string_scan_char4;
200 tree gfor_fndecl_string_verify_char4;
201 tree gfor_fndecl_string_trim_char4;
202 tree gfor_fndecl_string_minmax_char4;
203 tree gfor_fndecl_adjustl_char4;
204 tree gfor_fndecl_adjustr_char4;
205 tree gfor_fndecl_select_string_char4;
208 /* Conversion between character kinds. */
209 tree gfor_fndecl_convert_char1_to_char4;
210 tree gfor_fndecl_convert_char4_to_char1;
213 /* Other misc. runtime library functions. */
214 tree gfor_fndecl_iargc;
215 tree gfor_fndecl_kill;
216 tree gfor_fndecl_kill_sub;
217 tree gfor_fndecl_is_contiguous0;
220 /* Intrinsic functions implemented in Fortran. */
221 tree gfor_fndecl_sc_kind;
222 tree gfor_fndecl_si_kind;
223 tree gfor_fndecl_sr_kind;
225 /* BLAS gemm functions. */
226 tree gfor_fndecl_sgemm;
227 tree gfor_fndecl_dgemm;
228 tree gfor_fndecl_cgemm;
229 tree gfor_fndecl_zgemm;
231 /* RANDOM_INIT function. */
232 tree gfor_fndecl_random_init; /* libgfortran, 1 image only. */
234 static void
235 gfc_add_decl_to_parent_function (tree decl)
237 gcc_assert (decl);
238 DECL_CONTEXT (decl) = DECL_CONTEXT (current_function_decl);
239 DECL_NONLOCAL (decl) = 1;
240 DECL_CHAIN (decl) = saved_parent_function_decls;
241 saved_parent_function_decls = decl;
244 void
245 gfc_add_decl_to_function (tree decl)
247 gcc_assert (decl);
248 TREE_USED (decl) = 1;
249 DECL_CONTEXT (decl) = current_function_decl;
250 DECL_CHAIN (decl) = saved_function_decls;
251 saved_function_decls = decl;
254 static void
255 add_decl_as_local (tree decl)
257 gcc_assert (decl);
258 TREE_USED (decl) = 1;
259 DECL_CONTEXT (decl) = current_function_decl;
260 DECL_CHAIN (decl) = saved_local_decls;
261 saved_local_decls = decl;
265 /* Build a backend label declaration. Set TREE_USED for named labels.
266 The context of the label is always the current_function_decl. All
267 labels are marked artificial. */
269 tree
270 gfc_build_label_decl (tree label_id)
272 /* 2^32 temporaries should be enough. */
273 static unsigned int tmp_num = 1;
274 tree label_decl;
275 char *label_name;
277 if (label_id == NULL_TREE)
279 /* Build an internal label name. */
280 ASM_FORMAT_PRIVATE_NAME (label_name, "L", tmp_num++);
281 label_id = get_identifier (label_name);
283 else
284 label_name = NULL;
286 /* Build the LABEL_DECL node. Labels have no type. */
287 label_decl = build_decl (input_location,
288 LABEL_DECL, label_id, void_type_node);
289 DECL_CONTEXT (label_decl) = current_function_decl;
290 SET_DECL_MODE (label_decl, VOIDmode);
292 /* We always define the label as used, even if the original source
293 file never references the label. We don't want all kinds of
294 spurious warnings for old-style Fortran code with too many
295 labels. */
296 TREE_USED (label_decl) = 1;
298 DECL_ARTIFICIAL (label_decl) = 1;
299 return label_decl;
303 /* Set the backend source location of a decl. */
305 void
306 gfc_set_decl_location (tree decl, locus * loc)
308 DECL_SOURCE_LOCATION (decl) = gfc_get_location (loc);
312 /* Return the backend label declaration for a given label structure,
313 or create it if it doesn't exist yet. */
315 tree
316 gfc_get_label_decl (gfc_st_label * lp)
318 if (lp->backend_decl)
319 return lp->backend_decl;
320 else
322 char label_name[GFC_MAX_SYMBOL_LEN + 1];
323 tree label_decl;
325 /* Validate the label declaration from the front end. */
326 gcc_assert (lp != NULL && lp->value <= MAX_LABEL_VALUE);
328 /* Build a mangled name for the label. */
329 sprintf (label_name, "__label_%.6d", lp->value);
331 /* Build the LABEL_DECL node. */
332 label_decl = gfc_build_label_decl (get_identifier (label_name));
334 /* Tell the debugger where the label came from. */
335 if (lp->value <= MAX_LABEL_VALUE) /* An internal label. */
336 gfc_set_decl_location (label_decl, &lp->where);
337 else
338 DECL_ARTIFICIAL (label_decl) = 1;
340 /* Store the label in the label list and return the LABEL_DECL. */
341 lp->backend_decl = label_decl;
342 return label_decl;
346 /* Return the name of an identifier. */
348 static const char *
349 sym_identifier (gfc_symbol *sym)
351 if (sym->attr.is_main_program && strcmp (sym->name, "main") == 0)
352 return "MAIN__";
353 else
354 return sym->name;
357 /* Convert a gfc_symbol to an identifier of the same name. */
359 static tree
360 gfc_sym_identifier (gfc_symbol * sym)
362 return get_identifier (sym_identifier (sym));
365 /* Construct mangled name from symbol name. */
367 static const char *
368 mangled_identifier (gfc_symbol *sym)
370 gfc_symbol *proc = sym->ns->proc_name;
371 static char name[3*GFC_MAX_MANGLED_SYMBOL_LEN + 14];
372 /* Prevent the mangling of identifiers that have an assigned
373 binding label (mainly those that are bind(c)). */
375 if (sym->attr.is_bind_c == 1 && sym->binding_label)
376 return sym->binding_label;
378 if (!sym->fn_result_spec
379 || (sym->module && !(proc && proc->attr.flavor == FL_PROCEDURE)))
381 if (sym->module == NULL)
382 return sym_identifier (sym);
383 else
384 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
386 else
388 /* This is an entity that is actually local to a module procedure
389 that appears in the result specification expression. Since
390 sym->module will be a zero length string, we use ns->proc_name
391 to provide the module name instead. */
392 if (proc && proc->module)
393 snprintf (name, sizeof name, "__%s_MOD__%s_PROC_%s",
394 proc->module, proc->name, sym->name);
395 else
396 snprintf (name, sizeof name, "__%s_PROC_%s",
397 proc->name, sym->name);
400 return name;
403 /* Get mangled identifier, adding the symbol to the global table if
404 it is not yet already there. */
406 static tree
407 gfc_sym_mangled_identifier (gfc_symbol * sym)
409 tree result;
410 gfc_gsymbol *gsym;
411 const char *name;
413 name = mangled_identifier (sym);
414 result = get_identifier (name);
416 gsym = gfc_find_gsymbol (gfc_gsym_root, name);
417 if (gsym == NULL)
419 gsym = gfc_get_gsymbol (name, false);
420 gsym->ns = sym->ns;
421 gsym->sym_name = sym->name;
424 return result;
427 /* Construct mangled function name from symbol name. */
429 static tree
430 gfc_sym_mangled_function_id (gfc_symbol * sym)
432 int has_underscore;
433 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
435 /* It may be possible to simply use the binding label if it's
436 provided, and remove the other checks. Then we could use it
437 for other things if we wished. */
438 if ((sym->attr.is_bind_c == 1 || sym->attr.is_iso_c == 1) &&
439 sym->binding_label)
440 /* use the binding label rather than the mangled name */
441 return get_identifier (sym->binding_label);
443 if ((sym->module == NULL || sym->attr.proc == PROC_EXTERNAL
444 || (sym->module != NULL && (sym->attr.external
445 || sym->attr.if_source == IFSRC_IFBODY)))
446 && !sym->attr.module_procedure)
448 /* Main program is mangled into MAIN__. */
449 if (sym->attr.is_main_program)
450 return get_identifier ("MAIN__");
452 /* Intrinsic procedures are never mangled. */
453 if (sym->attr.proc == PROC_INTRINSIC)
454 return get_identifier (sym->name);
456 if (flag_underscoring)
458 has_underscore = strchr (sym->name, '_') != 0;
459 if (flag_second_underscore && has_underscore)
460 snprintf (name, sizeof name, "%s__", sym->name);
461 else
462 snprintf (name, sizeof name, "%s_", sym->name);
463 return get_identifier (name);
465 else
466 return get_identifier (sym->name);
468 else
470 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
471 return get_identifier (name);
476 void
477 gfc_set_decl_assembler_name (tree decl, tree name)
479 tree target_mangled = targetm.mangle_decl_assembler_name (decl, name);
480 SET_DECL_ASSEMBLER_NAME (decl, target_mangled);
484 /* Returns true if a variable of specified size should go on the stack. */
486 bool
487 gfc_can_put_var_on_stack (tree size)
489 unsigned HOST_WIDE_INT low;
491 if (!INTEGER_CST_P (size))
492 return 0;
494 if (flag_max_stack_var_size < 0)
495 return 1;
497 if (!tree_fits_uhwi_p (size))
498 return 0;
500 low = TREE_INT_CST_LOW (size);
501 if (low > (unsigned HOST_WIDE_INT) flag_max_stack_var_size)
502 return 0;
504 /* TODO: Set a per-function stack size limit. */
506 return 1;
510 /* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
511 an expression involving its corresponding pointer. There are
512 2 cases; one for variable size arrays, and one for everything else,
513 because variable-sized arrays require one fewer level of
514 indirection. */
516 static void
517 gfc_finish_cray_pointee (tree decl, gfc_symbol *sym)
519 tree ptr_decl = gfc_get_symbol_decl (sym->cp_pointer);
520 tree value;
522 /* Parameters need to be dereferenced. */
523 if (sym->cp_pointer->attr.dummy)
524 ptr_decl = build_fold_indirect_ref_loc (input_location,
525 ptr_decl);
527 /* Check to see if we're dealing with a variable-sized array. */
528 if (sym->attr.dimension
529 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
531 /* These decls will be dereferenced later, so we don't dereference
532 them here. */
533 value = convert (TREE_TYPE (decl), ptr_decl);
535 else
537 ptr_decl = convert (build_pointer_type (TREE_TYPE (decl)),
538 ptr_decl);
539 value = build_fold_indirect_ref_loc (input_location,
540 ptr_decl);
543 SET_DECL_VALUE_EXPR (decl, value);
544 DECL_HAS_VALUE_EXPR_P (decl) = 1;
545 GFC_DECL_CRAY_POINTEE (decl) = 1;
549 /* Finish processing of a declaration without an initial value. */
551 static void
552 gfc_finish_decl (tree decl)
554 gcc_assert (TREE_CODE (decl) == PARM_DECL
555 || DECL_INITIAL (decl) == NULL_TREE);
557 if (!VAR_P (decl))
558 return;
560 if (DECL_SIZE (decl) == NULL_TREE
561 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
562 layout_decl (decl, 0);
564 /* A few consistency checks. */
565 /* A static variable with an incomplete type is an error if it is
566 initialized. Also if it is not file scope. Otherwise, let it
567 through, but if it is not `extern' then it may cause an error
568 message later. */
569 /* An automatic variable with an incomplete type is an error. */
571 /* We should know the storage size. */
572 gcc_assert (DECL_SIZE (decl) != NULL_TREE
573 || (TREE_STATIC (decl)
574 ? (!DECL_INITIAL (decl) || !DECL_CONTEXT (decl))
575 : DECL_EXTERNAL (decl)));
577 /* The storage size should be constant. */
578 gcc_assert ((!DECL_EXTERNAL (decl) && !TREE_STATIC (decl))
579 || !DECL_SIZE (decl)
580 || TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST);
584 /* Handle setting of GFC_DECL_SCALAR* on DECL. */
586 void
587 gfc_finish_decl_attrs (tree decl, symbol_attribute *attr)
589 if (!attr->dimension && !attr->codimension)
591 /* Handle scalar allocatable variables. */
592 if (attr->allocatable)
594 gfc_allocate_lang_decl (decl);
595 GFC_DECL_SCALAR_ALLOCATABLE (decl) = 1;
597 /* Handle scalar pointer variables. */
598 if (attr->pointer)
600 gfc_allocate_lang_decl (decl);
601 GFC_DECL_SCALAR_POINTER (decl) = 1;
603 if (attr->target)
605 gfc_allocate_lang_decl (decl);
606 GFC_DECL_SCALAR_TARGET (decl) = 1;
612 /* Apply symbol attributes to a variable, and add it to the function scope. */
614 static void
615 gfc_finish_var_decl (tree decl, gfc_symbol * sym)
617 tree new_type;
619 /* Set DECL_VALUE_EXPR for Cray Pointees. */
620 if (sym->attr.cray_pointee)
621 gfc_finish_cray_pointee (decl, sym);
623 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
624 This is the equivalent of the TARGET variables.
625 We also need to set this if the variable is passed by reference in a
626 CALL statement. */
627 if (sym->attr.target)
628 TREE_ADDRESSABLE (decl) = 1;
630 /* If it wasn't used we wouldn't be getting it. */
631 TREE_USED (decl) = 1;
633 if (sym->attr.flavor == FL_PARAMETER
634 && (sym->attr.dimension || sym->ts.type == BT_DERIVED))
635 TREE_READONLY (decl) = 1;
637 /* Chain this decl to the pending declarations. Don't do pushdecl()
638 because this would add them to the current scope rather than the
639 function scope. */
640 if (current_function_decl != NULL_TREE)
642 if (sym->ns->proc_name
643 && (sym->ns->proc_name->backend_decl == current_function_decl
644 || sym->result == sym))
645 gfc_add_decl_to_function (decl);
646 else if (sym->ns->proc_name
647 && sym->ns->proc_name->attr.flavor == FL_LABEL)
648 /* This is a BLOCK construct. */
649 add_decl_as_local (decl);
650 else if (sym->ns->omp_affinity_iterators)
651 /* This is a block-local iterator. */
652 add_decl_as_local (decl);
653 else
654 gfc_add_decl_to_parent_function (decl);
657 if (sym->attr.cray_pointee)
658 return;
660 if(sym->attr.is_bind_c == 1 && sym->binding_label)
662 /* We need to put variables that are bind(c) into the common
663 segment of the object file, because this is what C would do.
664 gfortran would typically put them in either the BSS or
665 initialized data segments, and only mark them as common if
666 they were part of common blocks. However, if they are not put
667 into common space, then C cannot initialize global Fortran
668 variables that it interoperates with and the draft says that
669 either Fortran or C should be able to initialize it (but not
670 both, of course.) (J3/04-007, section 15.3). */
671 TREE_PUBLIC(decl) = 1;
672 DECL_COMMON(decl) = 1;
673 if (sym->attr.access == ACCESS_PRIVATE && !sym->attr.public_used)
675 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
676 DECL_VISIBILITY_SPECIFIED (decl) = true;
680 /* If a variable is USE associated, it's always external. */
681 if (sym->attr.use_assoc || sym->attr.used_in_submodule)
683 DECL_EXTERNAL (decl) = 1;
684 TREE_PUBLIC (decl) = 1;
686 else if (sym->fn_result_spec && !sym->ns->proc_name->module)
689 if (sym->ns->proc_name->attr.if_source != IFSRC_DECL)
690 DECL_EXTERNAL (decl) = 1;
691 else
692 TREE_STATIC (decl) = 1;
694 TREE_PUBLIC (decl) = 1;
696 else if (sym->module && !sym->attr.result && !sym->attr.dummy)
698 /* TODO: Don't set sym->module for result or dummy variables. */
699 gcc_assert (current_function_decl == NULL_TREE || sym->result == sym);
701 TREE_PUBLIC (decl) = 1;
702 TREE_STATIC (decl) = 1;
703 if (sym->attr.access == ACCESS_PRIVATE && !sym->attr.public_used)
705 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
706 DECL_VISIBILITY_SPECIFIED (decl) = true;
710 /* Derived types are a bit peculiar because of the possibility of
711 a default initializer; this must be applied each time the variable
712 comes into scope it therefore need not be static. These variables
713 are SAVE_NONE but have an initializer. Otherwise explicitly
714 initialized variables are SAVE_IMPLICIT and explicitly saved are
715 SAVE_EXPLICIT. */
716 if (!sym->attr.use_assoc
717 && (sym->attr.save != SAVE_NONE || sym->attr.data
718 || (sym->value && sym->ns->proc_name->attr.is_main_program)
719 || (flag_coarray == GFC_FCOARRAY_LIB
720 && sym->attr.codimension && !sym->attr.allocatable)))
721 TREE_STATIC (decl) = 1;
723 /* If derived-type variables with DTIO procedures are not made static
724 some bits of code referencing them get optimized away.
725 TODO Understand why this is so and fix it. */
726 if (!sym->attr.use_assoc
727 && ((sym->ts.type == BT_DERIVED
728 && sym->ts.u.derived->attr.has_dtio_procs)
729 || (sym->ts.type == BT_CLASS
730 && CLASS_DATA (sym)->ts.u.derived->attr.has_dtio_procs)))
731 TREE_STATIC (decl) = 1;
733 /* Treat asynchronous variables the same as volatile, for now. */
734 if (sym->attr.volatile_ || sym->attr.asynchronous)
736 TREE_THIS_VOLATILE (decl) = 1;
737 TREE_SIDE_EFFECTS (decl) = 1;
738 new_type = build_qualified_type (TREE_TYPE (decl), TYPE_QUAL_VOLATILE);
739 TREE_TYPE (decl) = new_type;
742 /* Keep variables larger than max-stack-var-size off stack. */
743 if (!(sym->ns->proc_name && sym->ns->proc_name->attr.recursive)
744 && !sym->attr.automatic
745 && !sym->attr.associate_var
746 && sym->attr.save != SAVE_EXPLICIT
747 && sym->attr.save != SAVE_IMPLICIT
748 && INTEGER_CST_P (DECL_SIZE_UNIT (decl))
749 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
750 /* Put variable length auto array pointers always into stack. */
751 && (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
752 || sym->attr.dimension == 0
753 || sym->as->type != AS_EXPLICIT
754 || sym->attr.pointer
755 || sym->attr.allocatable)
756 && !DECL_ARTIFICIAL (decl))
758 if (flag_max_stack_var_size > 0
759 && !(sym->ns->proc_name
760 && sym->ns->proc_name->attr.is_main_program))
761 gfc_warning (OPT_Wsurprising,
762 "Array %qs at %L is larger than limit set by "
763 "%<-fmax-stack-var-size=%>, moved from stack to static "
764 "storage. This makes the procedure unsafe when called "
765 "recursively, or concurrently from multiple threads. "
766 "Consider increasing the %<-fmax-stack-var-size=%> "
767 "limit (or use %<-frecursive%>, which implies "
768 "unlimited %<-fmax-stack-var-size%>) - or change the "
769 "code to use an ALLOCATABLE array. If the variable is "
770 "never accessed concurrently, this warning can be "
771 "ignored, and the variable could also be declared with "
772 "the SAVE attribute.",
773 sym->name, &sym->declared_at);
775 TREE_STATIC (decl) = 1;
777 /* Because the size of this variable isn't known until now, we may have
778 greedily added an initializer to this variable (in build_init_assign)
779 even though the max-stack-var-size indicates the variable should be
780 static. Therefore we rip out the automatic initializer here and
781 replace it with a static one. */
782 gfc_symtree *st = gfc_find_symtree (sym->ns->sym_root, sym->name);
783 gfc_code *prev = NULL;
784 gfc_code *code = sym->ns->code;
785 while (code && code->op == EXEC_INIT_ASSIGN)
787 /* Look for an initializer meant for this symbol. */
788 if (code->expr1->symtree == st)
790 if (prev)
791 prev->next = code->next;
792 else
793 sym->ns->code = code->next;
795 break;
798 prev = code;
799 code = code->next;
801 if (code && code->op == EXEC_INIT_ASSIGN)
803 /* Keep the init expression for a static initializer. */
804 sym->value = code->expr2;
805 /* Cleanup the defunct code object, without freeing the init expr. */
806 code->expr2 = NULL;
807 gfc_free_statement (code);
808 free (code);
812 /* Handle threadprivate variables. */
813 if (sym->attr.threadprivate
814 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
815 set_decl_tls_model (decl, decl_default_tls_model (decl));
817 /* Mark weak variables. */
818 if (sym->attr.ext_attr & (1 << EXT_ATTR_WEAK))
819 declare_weak (decl);
821 gfc_finish_decl_attrs (decl, &sym->attr);
825 /* Allocate the lang-specific part of a decl. */
827 void
828 gfc_allocate_lang_decl (tree decl)
830 if (DECL_LANG_SPECIFIC (decl) == NULL)
831 DECL_LANG_SPECIFIC (decl) = ggc_cleared_alloc<struct lang_decl> ();
834 /* Remember a symbol to generate initialization/cleanup code at function
835 entry/exit. */
837 static void
838 gfc_defer_symbol_init (gfc_symbol * sym)
840 gfc_symbol *p;
841 gfc_symbol *last;
842 gfc_symbol *head;
844 /* Don't add a symbol twice. */
845 if (sym->tlink)
846 return;
848 last = head = sym->ns->proc_name;
849 p = last->tlink;
851 /* Make sure that setup code for dummy variables which are used in the
852 setup of other variables is generated first. */
853 if (sym->attr.dummy)
855 /* Find the first dummy arg seen after us, or the first non-dummy arg.
856 This is a circular list, so don't go past the head. */
857 while (p != head
858 && (!p->attr.dummy || p->dummy_order > sym->dummy_order))
860 last = p;
861 p = p->tlink;
864 /* Insert in between last and p. */
865 last->tlink = sym;
866 sym->tlink = p;
870 /* Used in gfc_get_symbol_decl and gfc_get_derived_type to obtain the
871 backend_decl for a module symbol, if it all ready exists. If the
872 module gsymbol does not exist, it is created. If the symbol does
873 not exist, it is added to the gsymbol namespace. Returns true if
874 an existing backend_decl is found. */
876 bool
877 gfc_get_module_backend_decl (gfc_symbol *sym)
879 gfc_gsymbol *gsym;
880 gfc_symbol *s;
881 gfc_symtree *st;
883 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->module);
885 if (!gsym || (gsym->ns && gsym->type == GSYM_MODULE))
887 st = NULL;
888 s = NULL;
890 /* Check for a symbol with the same name. */
891 if (gsym)
892 gfc_find_symbol (sym->name, gsym->ns, 0, &s);
894 if (!s)
896 if (!gsym)
898 gsym = gfc_get_gsymbol (sym->module, false);
899 gsym->type = GSYM_MODULE;
900 gsym->ns = gfc_get_namespace (NULL, 0);
903 st = gfc_new_symtree (&gsym->ns->sym_root, sym->name);
904 st->n.sym = sym;
905 sym->refs++;
907 else if (gfc_fl_struct (sym->attr.flavor))
909 if (s && s->attr.flavor == FL_PROCEDURE)
911 gfc_interface *intr;
912 gcc_assert (s->attr.generic);
913 for (intr = s->generic; intr; intr = intr->next)
914 if (gfc_fl_struct (intr->sym->attr.flavor))
916 s = intr->sym;
917 break;
921 /* Normally we can assume that s is a derived-type symbol since it
922 shares a name with the derived-type sym. However if sym is a
923 STRUCTURE, it may in fact share a name with any other basic type
924 variable. If s is in fact of derived type then we can continue
925 looking for a duplicate type declaration. */
926 if (sym->attr.flavor == FL_STRUCT && s->ts.type == BT_DERIVED)
928 s = s->ts.u.derived;
931 if (gfc_fl_struct (s->attr.flavor) && !s->backend_decl)
933 if (s->attr.flavor == FL_UNION)
934 s->backend_decl = gfc_get_union_type (s);
935 else
936 s->backend_decl = gfc_get_derived_type (s);
938 gfc_copy_dt_decls_ifequal (s, sym, true);
939 return true;
941 else if (s->backend_decl)
943 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
944 gfc_copy_dt_decls_ifequal (s->ts.u.derived, sym->ts.u.derived,
945 true);
946 else if (sym->ts.type == BT_CHARACTER)
947 sym->ts.u.cl->backend_decl = s->ts.u.cl->backend_decl;
948 sym->backend_decl = s->backend_decl;
949 return true;
952 return false;
956 /* Create an array index type variable with function scope. */
958 static tree
959 create_index_var (const char * pfx, int nest)
961 tree decl;
963 decl = gfc_create_var_np (gfc_array_index_type, pfx);
964 if (nest)
965 gfc_add_decl_to_parent_function (decl);
966 else
967 gfc_add_decl_to_function (decl);
968 return decl;
972 /* Create variables to hold all the non-constant bits of info for a
973 descriptorless array. Remember these in the lang-specific part of the
974 type. */
976 static void
977 gfc_build_qualified_array (tree decl, gfc_symbol * sym)
979 tree type;
980 int dim;
981 int nest;
982 gfc_namespace* procns;
983 symbol_attribute *array_attr;
984 gfc_array_spec *as;
985 bool is_classarray = IS_CLASS_ARRAY (sym);
987 type = TREE_TYPE (decl);
988 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
989 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
991 /* We just use the descriptor, if there is one. */
992 if (GFC_DESCRIPTOR_TYPE_P (type))
993 return;
995 gcc_assert (GFC_ARRAY_TYPE_P (type));
996 procns = gfc_find_proc_namespace (sym->ns);
997 nest = (procns->proc_name->backend_decl != current_function_decl)
998 && !sym->attr.contained;
1000 if (array_attr->codimension && flag_coarray == GFC_FCOARRAY_LIB
1001 && as->type != AS_ASSUMED_SHAPE
1002 && GFC_TYPE_ARRAY_CAF_TOKEN (type) == NULL_TREE)
1004 tree token;
1005 tree token_type = build_qualified_type (pvoid_type_node,
1006 TYPE_QUAL_RESTRICT);
1008 if (sym->module && (sym->attr.use_assoc
1009 || sym->ns->proc_name->attr.flavor == FL_MODULE))
1011 tree token_name
1012 = get_identifier (gfc_get_string (GFC_PREFIX ("caf_token%s"),
1013 IDENTIFIER_POINTER (gfc_sym_mangled_identifier (sym))));
1014 token = build_decl (DECL_SOURCE_LOCATION (decl), VAR_DECL, token_name,
1015 token_type);
1016 if (sym->attr.use_assoc)
1017 DECL_EXTERNAL (token) = 1;
1018 else
1019 TREE_STATIC (token) = 1;
1021 TREE_PUBLIC (token) = 1;
1023 if (sym->attr.access == ACCESS_PRIVATE && !sym->attr.public_used)
1025 DECL_VISIBILITY (token) = VISIBILITY_HIDDEN;
1026 DECL_VISIBILITY_SPECIFIED (token) = true;
1029 else
1031 token = gfc_create_var_np (token_type, "caf_token");
1032 TREE_STATIC (token) = 1;
1035 GFC_TYPE_ARRAY_CAF_TOKEN (type) = token;
1036 DECL_ARTIFICIAL (token) = 1;
1037 DECL_NONALIASED (token) = 1;
1039 if (sym->module && !sym->attr.use_assoc)
1041 pushdecl (token);
1042 DECL_CONTEXT (token) = sym->ns->proc_name->backend_decl;
1043 gfc_module_add_decl (cur_module, token);
1045 else if (sym->attr.host_assoc
1046 && TREE_CODE (DECL_CONTEXT (current_function_decl))
1047 != TRANSLATION_UNIT_DECL)
1048 gfc_add_decl_to_parent_function (token);
1049 else
1050 gfc_add_decl_to_function (token);
1053 for (dim = 0; dim < GFC_TYPE_ARRAY_RANK (type); dim++)
1055 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
1057 GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
1058 suppress_warning (GFC_TYPE_ARRAY_LBOUND (type, dim));
1060 /* Don't try to use the unknown bound for assumed shape arrays. */
1061 if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
1062 && (as->type != AS_ASSUMED_SIZE
1063 || dim < GFC_TYPE_ARRAY_RANK (type) - 1))
1065 GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
1066 suppress_warning (GFC_TYPE_ARRAY_UBOUND (type, dim));
1069 if (GFC_TYPE_ARRAY_STRIDE (type, dim) == NULL_TREE)
1071 GFC_TYPE_ARRAY_STRIDE (type, dim) = create_index_var ("stride", nest);
1072 suppress_warning (GFC_TYPE_ARRAY_STRIDE (type, dim));
1075 for (dim = GFC_TYPE_ARRAY_RANK (type);
1076 dim < GFC_TYPE_ARRAY_RANK (type) + GFC_TYPE_ARRAY_CORANK (type); dim++)
1078 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
1080 GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
1081 suppress_warning (GFC_TYPE_ARRAY_LBOUND (type, dim));
1083 /* Don't try to use the unknown ubound for the last coarray dimension. */
1084 if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
1085 && dim < GFC_TYPE_ARRAY_RANK (type) + GFC_TYPE_ARRAY_CORANK (type) - 1)
1087 GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
1088 suppress_warning (GFC_TYPE_ARRAY_UBOUND (type, dim));
1091 if (GFC_TYPE_ARRAY_OFFSET (type) == NULL_TREE)
1093 GFC_TYPE_ARRAY_OFFSET (type) = gfc_create_var_np (gfc_array_index_type,
1094 "offset");
1095 suppress_warning (GFC_TYPE_ARRAY_OFFSET (type));
1097 if (nest)
1098 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type));
1099 else
1100 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type));
1103 if (GFC_TYPE_ARRAY_SIZE (type) == NULL_TREE
1104 && as->type != AS_ASSUMED_SIZE)
1106 GFC_TYPE_ARRAY_SIZE (type) = create_index_var ("size", nest);
1107 suppress_warning (GFC_TYPE_ARRAY_SIZE (type));
1110 if (POINTER_TYPE_P (type))
1112 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type)));
1113 gcc_assert (TYPE_LANG_SPECIFIC (type)
1114 == TYPE_LANG_SPECIFIC (TREE_TYPE (type)));
1115 type = TREE_TYPE (type);
1118 if (! COMPLETE_TYPE_P (type) && GFC_TYPE_ARRAY_SIZE (type))
1120 tree size, range;
1122 size = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
1123 GFC_TYPE_ARRAY_SIZE (type), gfc_index_one_node);
1124 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1125 size);
1126 TYPE_DOMAIN (type) = range;
1127 layout_type (type);
1130 if (TYPE_NAME (type) != NULL_TREE && as->rank > 0
1131 && GFC_TYPE_ARRAY_UBOUND (type, as->rank - 1) != NULL_TREE
1132 && VAR_P (GFC_TYPE_ARRAY_UBOUND (type, as->rank - 1)))
1134 tree gtype = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1136 for (dim = 0; dim < as->rank - 1; dim++)
1138 gcc_assert (TREE_CODE (gtype) == ARRAY_TYPE);
1139 gtype = TREE_TYPE (gtype);
1141 gcc_assert (TREE_CODE (gtype) == ARRAY_TYPE);
1142 if (TYPE_MAX_VALUE (TYPE_DOMAIN (gtype)) == NULL)
1143 TYPE_NAME (type) = NULL_TREE;
1146 if (TYPE_NAME (type) == NULL_TREE)
1148 tree gtype = TREE_TYPE (type), rtype, type_decl;
1150 for (dim = as->rank - 1; dim >= 0; dim--)
1152 tree lbound, ubound;
1153 lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
1154 ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
1155 rtype = build_range_type (gfc_array_index_type, lbound, ubound);
1156 gtype = build_array_type (gtype, rtype);
1157 /* Ensure the bound variables aren't optimized out at -O0.
1158 For -O1 and above they often will be optimized out, but
1159 can be tracked by VTA. Also set DECL_NAMELESS, so that
1160 the artificial lbound.N or ubound.N DECL_NAME doesn't
1161 end up in debug info. */
1162 if (lbound
1163 && VAR_P (lbound)
1164 && DECL_ARTIFICIAL (lbound)
1165 && DECL_IGNORED_P (lbound))
1167 if (DECL_NAME (lbound)
1168 && strstr (IDENTIFIER_POINTER (DECL_NAME (lbound)),
1169 "lbound") != 0)
1170 DECL_NAMELESS (lbound) = 1;
1171 DECL_IGNORED_P (lbound) = 0;
1173 if (ubound
1174 && VAR_P (ubound)
1175 && DECL_ARTIFICIAL (ubound)
1176 && DECL_IGNORED_P (ubound))
1178 if (DECL_NAME (ubound)
1179 && strstr (IDENTIFIER_POINTER (DECL_NAME (ubound)),
1180 "ubound") != 0)
1181 DECL_NAMELESS (ubound) = 1;
1182 DECL_IGNORED_P (ubound) = 0;
1185 TYPE_NAME (type) = type_decl = build_decl (input_location,
1186 TYPE_DECL, NULL, gtype);
1187 DECL_ORIGINAL_TYPE (type_decl) = gtype;
1192 /* For some dummy arguments we don't use the actual argument directly.
1193 Instead we create a local decl and use that. This allows us to perform
1194 initialization, and construct full type information. */
1196 static tree
1197 gfc_build_dummy_array_decl (gfc_symbol * sym, tree dummy)
1199 tree decl;
1200 tree type;
1201 gfc_array_spec *as;
1202 symbol_attribute *array_attr;
1203 char *name;
1204 gfc_packed packed;
1205 int n;
1206 bool known_size;
1207 bool is_classarray = IS_CLASS_ARRAY (sym);
1209 /* Use the array as and attr. */
1210 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
1211 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
1213 /* The dummy is returned for pointer, allocatable or assumed rank arrays.
1214 For class arrays the information if sym is an allocatable or pointer
1215 object needs to be checked explicitly (IS_CLASS_ARRAY can be false for
1216 too many reasons to be of use here). */
1217 if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
1218 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
1219 || array_attr->allocatable
1220 || (as && as->type == AS_ASSUMED_RANK))
1221 return dummy;
1223 /* Add to list of variables if not a fake result variable.
1224 These symbols are set on the symbol only, not on the class component. */
1225 if (sym->attr.result || sym->attr.dummy)
1226 gfc_defer_symbol_init (sym);
1228 /* For a class array the array descriptor is in the _data component, while
1229 for a regular array the TREE_TYPE of the dummy is a pointer to the
1230 descriptor. */
1231 type = TREE_TYPE (is_classarray ? gfc_class_data_get (dummy)
1232 : TREE_TYPE (dummy));
1233 /* type now is the array descriptor w/o any indirection. */
1234 gcc_assert (TREE_CODE (dummy) == PARM_DECL
1235 && POINTER_TYPE_P (TREE_TYPE (dummy)));
1237 /* Do we know the element size? */
1238 known_size = sym->ts.type != BT_CHARACTER
1239 || INTEGER_CST_P (sym->ts.u.cl->backend_decl);
1241 if (known_size && !GFC_DESCRIPTOR_TYPE_P (type))
1243 /* For descriptorless arrays with known element size the actual
1244 argument is sufficient. */
1245 gfc_build_qualified_array (dummy, sym);
1246 return dummy;
1249 if (GFC_DESCRIPTOR_TYPE_P (type))
1251 /* Create a descriptorless array pointer. */
1252 packed = PACKED_NO;
1254 /* Even when -frepack-arrays is used, symbols with TARGET attribute
1255 are not repacked. */
1256 if (!flag_repack_arrays || sym->attr.target)
1258 if (as->type == AS_ASSUMED_SIZE)
1259 packed = PACKED_FULL;
1261 else
1263 if (as->type == AS_EXPLICIT)
1265 packed = PACKED_FULL;
1266 for (n = 0; n < as->rank; n++)
1268 if (!(as->upper[n]
1269 && as->lower[n]
1270 && as->upper[n]->expr_type == EXPR_CONSTANT
1271 && as->lower[n]->expr_type == EXPR_CONSTANT))
1273 packed = PACKED_PARTIAL;
1274 break;
1278 else
1279 packed = PACKED_PARTIAL;
1282 /* For classarrays the element type is required, but
1283 gfc_typenode_for_spec () returns the array descriptor. */
1284 type = is_classarray ? gfc_get_element_type (type)
1285 : gfc_typenode_for_spec (&sym->ts);
1286 type = gfc_get_nodesc_array_type (type, as, packed,
1287 !sym->attr.target);
1289 else
1291 /* We now have an expression for the element size, so create a fully
1292 qualified type. Reset sym->backend decl or this will just return the
1293 old type. */
1294 DECL_ARTIFICIAL (sym->backend_decl) = 1;
1295 sym->backend_decl = NULL_TREE;
1296 type = gfc_sym_type (sym);
1297 packed = PACKED_FULL;
1300 ASM_FORMAT_PRIVATE_NAME (name, IDENTIFIER_POINTER (DECL_NAME (dummy)), 0);
1301 decl = build_decl (input_location,
1302 VAR_DECL, get_identifier (name), type);
1304 DECL_ARTIFICIAL (decl) = 1;
1305 DECL_NAMELESS (decl) = 1;
1306 TREE_PUBLIC (decl) = 0;
1307 TREE_STATIC (decl) = 0;
1308 DECL_EXTERNAL (decl) = 0;
1310 /* Avoid uninitialized warnings for optional dummy arguments. */
1311 if ((sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.optional)
1312 || sym->attr.optional)
1313 suppress_warning (decl);
1315 /* We should never get deferred shape arrays here. We used to because of
1316 frontend bugs. */
1317 gcc_assert (as->type != AS_DEFERRED);
1319 if (packed == PACKED_PARTIAL)
1320 GFC_DECL_PARTIAL_PACKED_ARRAY (decl) = 1;
1321 else if (packed == PACKED_FULL)
1322 GFC_DECL_PACKED_ARRAY (decl) = 1;
1324 gfc_build_qualified_array (decl, sym);
1326 if (DECL_LANG_SPECIFIC (dummy))
1327 DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (dummy);
1328 else
1329 gfc_allocate_lang_decl (decl);
1331 GFC_DECL_SAVED_DESCRIPTOR (decl) = dummy;
1333 if (sym->ns->proc_name->backend_decl == current_function_decl
1334 || sym->attr.contained)
1335 gfc_add_decl_to_function (decl);
1336 else
1337 gfc_add_decl_to_parent_function (decl);
1339 return decl;
1342 /* Return a constant or a variable to use as a string length. Does not
1343 add the decl to the current scope. */
1345 static tree
1346 gfc_create_string_length (gfc_symbol * sym)
1348 gcc_assert (sym->ts.u.cl);
1349 gfc_conv_const_charlen (sym->ts.u.cl);
1351 if (sym->ts.u.cl->backend_decl == NULL_TREE)
1353 tree length;
1354 const char *name;
1356 /* The string length variable shall be in static memory if it is either
1357 explicitly SAVED, a module variable or with -fno-automatic. Only
1358 relevant is "len=:" - otherwise, it is either a constant length or
1359 it is an automatic variable. */
1360 bool static_length = sym->attr.save
1361 || sym->ns->proc_name->attr.flavor == FL_MODULE
1362 || (flag_max_stack_var_size == 0
1363 && sym->ts.deferred && !sym->attr.dummy
1364 && !sym->attr.result && !sym->attr.function);
1366 /* Also prefix the mangled name. We need to call GFC_PREFIX for static
1367 variables as some systems do not support the "." in the assembler name.
1368 For nonstatic variables, the "." does not appear in assembler. */
1369 if (static_length)
1371 if (sym->module)
1372 name = gfc_get_string (GFC_PREFIX ("%s_MOD_%s"), sym->module,
1373 sym->name);
1374 else
1375 name = gfc_get_string (GFC_PREFIX ("%s"), sym->name);
1377 else if (sym->module)
1378 name = gfc_get_string (".__%s_MOD_%s", sym->module, sym->name);
1379 else
1380 name = gfc_get_string (".%s", sym->name);
1382 length = build_decl (input_location,
1383 VAR_DECL, get_identifier (name),
1384 gfc_charlen_type_node);
1385 DECL_ARTIFICIAL (length) = 1;
1386 TREE_USED (length) = 1;
1387 if (sym->ns->proc_name->tlink != NULL)
1388 gfc_defer_symbol_init (sym);
1390 sym->ts.u.cl->backend_decl = length;
1392 if (static_length)
1393 TREE_STATIC (length) = 1;
1395 if (sym->ns->proc_name->attr.flavor == FL_MODULE
1396 && (sym->attr.access != ACCESS_PRIVATE || sym->attr.public_used))
1397 TREE_PUBLIC (length) = 1;
1400 gcc_assert (sym->ts.u.cl->backend_decl != NULL_TREE);
1401 return sym->ts.u.cl->backend_decl;
1404 /* If a variable is assigned a label, we add another two auxiliary
1405 variables. */
1407 static void
1408 gfc_add_assign_aux_vars (gfc_symbol * sym)
1410 tree addr;
1411 tree length;
1412 tree decl;
1414 gcc_assert (sym->backend_decl);
1416 decl = sym->backend_decl;
1417 gfc_allocate_lang_decl (decl);
1418 GFC_DECL_ASSIGN (decl) = 1;
1419 length = build_decl (input_location,
1420 VAR_DECL, create_tmp_var_name (sym->name),
1421 gfc_charlen_type_node);
1422 addr = build_decl (input_location,
1423 VAR_DECL, create_tmp_var_name (sym->name),
1424 pvoid_type_node);
1425 gfc_finish_var_decl (length, sym);
1426 gfc_finish_var_decl (addr, sym);
1427 /* STRING_LENGTH is also used as flag. Less than -1 means that
1428 ASSIGN_ADDR cannot be used. Equal -1 means that ASSIGN_ADDR is the
1429 target label's address. Otherwise, value is the length of a format string
1430 and ASSIGN_ADDR is its address. */
1431 if (TREE_STATIC (length))
1432 DECL_INITIAL (length) = build_int_cst (gfc_charlen_type_node, -2);
1433 else
1434 gfc_defer_symbol_init (sym);
1436 GFC_DECL_STRING_LEN (decl) = length;
1437 GFC_DECL_ASSIGN_ADDR (decl) = addr;
1441 static tree
1442 add_attributes_to_decl (symbol_attribute sym_attr, tree list)
1444 unsigned id;
1445 tree attr;
1447 for (id = 0; id < EXT_ATTR_NUM; id++)
1448 if (sym_attr.ext_attr & (1 << id) && ext_attr_list[id].middle_end_name)
1450 attr = build_tree_list (
1451 get_identifier (ext_attr_list[id].middle_end_name),
1452 NULL_TREE);
1453 list = chainon (list, attr);
1456 tree clauses = NULL_TREE;
1458 if (sym_attr.oacc_routine_lop != OACC_ROUTINE_LOP_NONE)
1460 omp_clause_code code;
1461 switch (sym_attr.oacc_routine_lop)
1463 case OACC_ROUTINE_LOP_GANG:
1464 code = OMP_CLAUSE_GANG;
1465 break;
1466 case OACC_ROUTINE_LOP_WORKER:
1467 code = OMP_CLAUSE_WORKER;
1468 break;
1469 case OACC_ROUTINE_LOP_VECTOR:
1470 code = OMP_CLAUSE_VECTOR;
1471 break;
1472 case OACC_ROUTINE_LOP_SEQ:
1473 code = OMP_CLAUSE_SEQ;
1474 break;
1475 case OACC_ROUTINE_LOP_NONE:
1476 case OACC_ROUTINE_LOP_ERROR:
1477 default:
1478 gcc_unreachable ();
1480 tree c = build_omp_clause (UNKNOWN_LOCATION, code);
1481 OMP_CLAUSE_CHAIN (c) = clauses;
1482 clauses = c;
1484 tree dims = oacc_build_routine_dims (clauses);
1485 list = oacc_replace_fn_attrib_attr (list, dims);
1488 if (sym_attr.oacc_routine_nohost)
1490 tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_NOHOST);
1491 OMP_CLAUSE_CHAIN (c) = clauses;
1492 clauses = c;
1495 if (sym_attr.omp_device_type != OMP_DEVICE_TYPE_UNSET)
1497 tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_DEVICE_TYPE);
1498 switch (sym_attr.omp_device_type)
1500 case OMP_DEVICE_TYPE_HOST:
1501 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = OMP_CLAUSE_DEVICE_TYPE_HOST;
1502 break;
1503 case OMP_DEVICE_TYPE_NOHOST:
1504 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
1505 break;
1506 case OMP_DEVICE_TYPE_ANY:
1507 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = OMP_CLAUSE_DEVICE_TYPE_ANY;
1508 break;
1509 default:
1510 gcc_unreachable ();
1512 OMP_CLAUSE_CHAIN (c) = clauses;
1513 clauses = c;
1516 if (sym_attr.omp_declare_target_link
1517 || sym_attr.oacc_declare_link)
1518 list = tree_cons (get_identifier ("omp declare target link"),
1519 clauses, list);
1520 else if (sym_attr.omp_declare_target
1521 || sym_attr.oacc_declare_create
1522 || sym_attr.oacc_declare_copyin
1523 || sym_attr.oacc_declare_deviceptr
1524 || sym_attr.oacc_declare_device_resident)
1525 list = tree_cons (get_identifier ("omp declare target"),
1526 clauses, list);
1528 return list;
1532 static void build_function_decl (gfc_symbol * sym, bool global);
1535 /* Return the decl for a gfc_symbol, create it if it doesn't already
1536 exist. */
1538 tree
1539 gfc_get_symbol_decl (gfc_symbol * sym)
1541 tree decl;
1542 tree length = NULL_TREE;
1543 tree attributes;
1544 int byref;
1545 bool intrinsic_array_parameter = false;
1546 bool fun_or_res;
1548 gcc_assert (sym->attr.referenced
1549 || sym->attr.flavor == FL_PROCEDURE
1550 || sym->attr.use_assoc
1551 || sym->attr.used_in_submodule
1552 || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY
1553 || (sym->module && sym->attr.if_source != IFSRC_DECL
1554 && sym->backend_decl));
1556 if (sym->attr.dummy && sym->ns->proc_name->attr.is_bind_c
1557 && is_CFI_desc (sym, NULL))
1559 gcc_assert (sym->backend_decl && (sym->ts.type != BT_CHARACTER
1560 || sym->ts.u.cl->backend_decl));
1561 return sym->backend_decl;
1564 if (sym->ns && sym->ns->proc_name && sym->ns->proc_name->attr.function)
1565 byref = gfc_return_by_reference (sym->ns->proc_name);
1566 else
1567 byref = 0;
1569 /* Make sure that the vtab for the declared type is completed. */
1570 if (sym->ts.type == BT_CLASS)
1572 gfc_component *c = CLASS_DATA (sym);
1573 if (!c->ts.u.derived->backend_decl)
1575 gfc_find_derived_vtab (c->ts.u.derived);
1576 gfc_get_derived_type (sym->ts.u.derived);
1580 /* PDT parameterized array components and string_lengths must have the
1581 'len' parameters substituted for the expressions appearing in the
1582 declaration of the entity and memory allocated/deallocated. */
1583 if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
1584 && sym->param_list != NULL
1585 && gfc_current_ns == sym->ns
1586 && !(sym->attr.use_assoc || sym->attr.dummy))
1587 gfc_defer_symbol_init (sym);
1589 /* Dummy PDT 'len' parameters should be checked when they are explicit. */
1590 if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
1591 && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
1592 && sym->param_list != NULL
1593 && sym->attr.dummy)
1594 gfc_defer_symbol_init (sym);
1596 /* All deferred character length procedures need to retain the backend
1597 decl, which is a pointer to the character length in the caller's
1598 namespace and to declare a local character length. */
1599 if (!byref && sym->attr.function
1600 && sym->ts.type == BT_CHARACTER
1601 && sym->ts.deferred
1602 && sym->ts.u.cl->passed_length == NULL
1603 && sym->ts.u.cl->backend_decl
1604 && TREE_CODE (sym->ts.u.cl->backend_decl) == PARM_DECL)
1606 sym->ts.u.cl->passed_length = sym->ts.u.cl->backend_decl;
1607 gcc_assert (POINTER_TYPE_P (TREE_TYPE (sym->ts.u.cl->passed_length)));
1608 sym->ts.u.cl->backend_decl = build_fold_indirect_ref (sym->ts.u.cl->backend_decl);
1611 fun_or_res = byref && (sym->attr.result
1612 || (sym->attr.function && sym->ts.deferred));
1613 if ((sym->attr.dummy && ! sym->attr.function) || fun_or_res)
1615 /* Return via extra parameter. */
1616 if (sym->attr.result && byref
1617 && !sym->backend_decl)
1619 sym->backend_decl =
1620 DECL_ARGUMENTS (sym->ns->proc_name->backend_decl);
1621 /* For entry master function skip over the __entry
1622 argument. */
1623 if (sym->ns->proc_name->attr.entry_master)
1624 sym->backend_decl = DECL_CHAIN (sym->backend_decl);
1627 /* Dummy variables should already have been created. */
1628 gcc_assert (sym->backend_decl);
1630 /* However, the string length of deferred arrays must be set. */
1631 if (sym->ts.type == BT_CHARACTER
1632 && sym->ts.deferred
1633 && sym->attr.dimension
1634 && sym->attr.allocatable)
1635 gfc_defer_symbol_init (sym);
1637 if (sym->attr.pointer && sym->attr.dimension && sym->ts.type != BT_CLASS)
1638 GFC_DECL_PTR_ARRAY_P (sym->backend_decl) = 1;
1640 /* Create a character length variable. */
1641 if (sym->ts.type == BT_CHARACTER)
1643 /* For a deferred dummy, make a new string length variable. */
1644 if (sym->ts.deferred
1646 (sym->ts.u.cl->passed_length == sym->ts.u.cl->backend_decl))
1647 sym->ts.u.cl->backend_decl = NULL_TREE;
1649 if (sym->ts.deferred && byref)
1651 /* The string length of a deferred char array is stored in the
1652 parameter at sym->ts.u.cl->backend_decl as a reference and
1653 marked as a result. Exempt this variable from generating a
1654 temporary for it. */
1655 if (sym->attr.result)
1657 /* We need to insert a indirect ref for param decls. */
1658 if (sym->ts.u.cl->backend_decl
1659 && TREE_CODE (sym->ts.u.cl->backend_decl) == PARM_DECL)
1661 sym->ts.u.cl->passed_length = sym->ts.u.cl->backend_decl;
1662 sym->ts.u.cl->backend_decl =
1663 build_fold_indirect_ref (sym->ts.u.cl->backend_decl);
1666 /* For all other parameters make sure, that they are copied so
1667 that the value and any modifications are local to the routine
1668 by generating a temporary variable. */
1669 else if (sym->attr.function
1670 && sym->ts.u.cl->passed_length == NULL
1671 && sym->ts.u.cl->backend_decl)
1673 sym->ts.u.cl->passed_length = sym->ts.u.cl->backend_decl;
1674 if (POINTER_TYPE_P (TREE_TYPE (sym->ts.u.cl->passed_length)))
1675 sym->ts.u.cl->backend_decl
1676 = build_fold_indirect_ref (sym->ts.u.cl->backend_decl);
1677 else
1678 sym->ts.u.cl->backend_decl = NULL_TREE;
1682 if (sym->ts.u.cl->backend_decl == NULL_TREE)
1683 length = gfc_create_string_length (sym);
1684 else
1685 length = sym->ts.u.cl->backend_decl;
1686 if (VAR_P (length) && DECL_FILE_SCOPE_P (length))
1688 /* Add the string length to the same context as the symbol. */
1689 if (DECL_CONTEXT (length) == NULL_TREE)
1691 if (sym->backend_decl == current_function_decl
1692 || (DECL_CONTEXT (sym->backend_decl)
1693 == current_function_decl))
1694 gfc_add_decl_to_function (length);
1695 else
1696 gfc_add_decl_to_parent_function (length);
1699 gcc_assert (sym->backend_decl == current_function_decl
1700 ? DECL_CONTEXT (length) == current_function_decl
1701 : (DECL_CONTEXT (sym->backend_decl)
1702 == DECL_CONTEXT (length)));
1704 gfc_defer_symbol_init (sym);
1708 /* Use a copy of the descriptor for dummy arrays. */
1709 if ((sym->attr.dimension || sym->attr.codimension)
1710 && !TREE_USED (sym->backend_decl))
1712 decl = gfc_build_dummy_array_decl (sym, sym->backend_decl);
1713 /* Prevent the dummy from being detected as unused if it is copied. */
1714 if (sym->backend_decl != NULL && decl != sym->backend_decl)
1715 DECL_ARTIFICIAL (sym->backend_decl) = 1;
1716 sym->backend_decl = decl;
1719 /* Returning the descriptor for dummy class arrays is hazardous, because
1720 some caller is expecting an expression to apply the component refs to.
1721 Therefore the descriptor is only created and stored in
1722 sym->backend_decl's GFC_DECL_SAVED_DESCRIPTOR. The caller is then
1723 responsible to extract it from there, when the descriptor is
1724 desired. */
1725 if (IS_CLASS_ARRAY (sym)
1726 && (!DECL_LANG_SPECIFIC (sym->backend_decl)
1727 || !GFC_DECL_SAVED_DESCRIPTOR (sym->backend_decl)))
1729 decl = gfc_build_dummy_array_decl (sym, sym->backend_decl);
1730 /* Prevent the dummy from being detected as unused if it is copied. */
1731 if (sym->backend_decl != NULL && decl != sym->backend_decl)
1732 DECL_ARTIFICIAL (sym->backend_decl) = 1;
1733 sym->backend_decl = decl;
1736 TREE_USED (sym->backend_decl) = 1;
1737 if (sym->attr.assign && GFC_DECL_ASSIGN (sym->backend_decl) == 0)
1738 gfc_add_assign_aux_vars (sym);
1740 if (sym->ts.type == BT_CLASS && sym->backend_decl)
1741 GFC_DECL_CLASS(sym->backend_decl) = 1;
1743 return sym->backend_decl;
1746 if (sym->result == sym && sym->attr.assign
1747 && GFC_DECL_ASSIGN (sym->backend_decl) == 0)
1748 gfc_add_assign_aux_vars (sym);
1750 if (sym->backend_decl)
1751 return sym->backend_decl;
1753 /* Special case for array-valued named constants from intrinsic
1754 procedures; those are inlined. */
1755 if (sym->attr.use_assoc && sym->attr.flavor == FL_PARAMETER
1756 && (sym->from_intmod == INTMOD_ISO_FORTRAN_ENV
1757 || sym->from_intmod == INTMOD_ISO_C_BINDING))
1758 intrinsic_array_parameter = true;
1760 /* If use associated compilation, use the module
1761 declaration. */
1762 if ((sym->attr.flavor == FL_VARIABLE
1763 || sym->attr.flavor == FL_PARAMETER)
1764 && (sym->attr.use_assoc || sym->attr.used_in_submodule)
1765 && !intrinsic_array_parameter
1766 && sym->module
1767 && gfc_get_module_backend_decl (sym))
1769 if (sym->ts.type == BT_CLASS && sym->backend_decl)
1770 GFC_DECL_CLASS(sym->backend_decl) = 1;
1771 return sym->backend_decl;
1774 if (sym->attr.flavor == FL_PROCEDURE)
1776 /* Catch functions. Only used for actual parameters,
1777 procedure pointers and procptr initialization targets. */
1778 if (sym->attr.use_assoc
1779 || sym->attr.used_in_submodule
1780 || sym->attr.intrinsic
1781 || sym->attr.if_source != IFSRC_DECL)
1783 decl = gfc_get_extern_function_decl (sym);
1785 else
1787 if (!sym->backend_decl)
1788 build_function_decl (sym, false);
1789 decl = sym->backend_decl;
1791 return decl;
1794 if (sym->ts.type == BT_UNKNOWN)
1795 gfc_fatal_error ("%s at %C has no default type", sym->name);
1797 if (sym->attr.intrinsic)
1798 gfc_internal_error ("intrinsic variable which isn't a procedure");
1800 /* Create string length decl first so that they can be used in the
1801 type declaration. For associate names, the target character
1802 length is used. Set 'length' to a constant so that if the
1803 string length is a variable, it is not finished a second time. */
1804 if (sym->ts.type == BT_CHARACTER)
1806 if (sym->attr.associate_var
1807 && sym->ts.deferred
1808 && sym->assoc && sym->assoc->target
1809 && ((sym->assoc->target->expr_type == EXPR_VARIABLE
1810 && sym->assoc->target->symtree->n.sym->ts.type != BT_CHARACTER)
1811 || sym->assoc->target->expr_type != EXPR_VARIABLE))
1812 sym->ts.u.cl->backend_decl = NULL_TREE;
1814 if (sym->attr.associate_var
1815 && sym->ts.u.cl->backend_decl
1816 && (VAR_P (sym->ts.u.cl->backend_decl)
1817 || TREE_CODE (sym->ts.u.cl->backend_decl) == PARM_DECL))
1818 length = gfc_index_zero_node;
1819 else
1820 length = gfc_create_string_length (sym);
1823 /* Create the decl for the variable. */
1824 decl = build_decl (gfc_get_location (&sym->declared_at),
1825 VAR_DECL, gfc_sym_identifier (sym), gfc_sym_type (sym));
1827 /* Add attributes to variables. Functions are handled elsewhere. */
1828 attributes = add_attributes_to_decl (sym->attr, NULL_TREE);
1829 decl_attributes (&decl, attributes, 0);
1830 if (sym->ts.deferred && VAR_P (length))
1831 decl_attributes (&length, attributes, 0);
1833 /* Symbols from modules should have their assembler names mangled.
1834 This is done here rather than in gfc_finish_var_decl because it
1835 is different for string length variables. */
1836 if (sym->module || sym->fn_result_spec)
1838 gfc_set_decl_assembler_name (decl, gfc_sym_mangled_identifier (sym));
1839 if (sym->attr.use_assoc && !intrinsic_array_parameter)
1840 DECL_IGNORED_P (decl) = 1;
1843 if (sym->attr.select_type_temporary)
1845 DECL_ARTIFICIAL (decl) = 1;
1846 DECL_IGNORED_P (decl) = 1;
1849 if (sym->attr.dimension || sym->attr.codimension)
1851 /* Create variables to hold the non-constant bits of array info. */
1852 gfc_build_qualified_array (decl, sym);
1854 if (sym->attr.contiguous
1855 || ((sym->attr.allocatable || !sym->attr.dummy) && !sym->attr.pointer))
1856 GFC_DECL_PACKED_ARRAY (decl) = 1;
1859 /* Remember this variable for allocation/cleanup. */
1860 if (sym->attr.dimension || sym->attr.allocatable || sym->attr.codimension
1861 || (sym->ts.type == BT_CLASS &&
1862 (CLASS_DATA (sym)->attr.dimension
1863 || CLASS_DATA (sym)->attr.allocatable))
1864 || (sym->ts.type == BT_DERIVED
1865 && (sym->ts.u.derived->attr.alloc_comp
1866 || (!sym->attr.pointer && !sym->attr.artificial && !sym->attr.save
1867 && !sym->ns->proc_name->attr.is_main_program
1868 && gfc_is_finalizable (sym->ts.u.derived, NULL))))
1869 /* This applies a derived type default initializer. */
1870 || (sym->ts.type == BT_DERIVED
1871 && sym->attr.save == SAVE_NONE
1872 && !sym->attr.data
1873 && !sym->attr.allocatable
1874 && (sym->value && !sym->ns->proc_name->attr.is_main_program)
1875 && !(sym->attr.use_assoc && !intrinsic_array_parameter)))
1876 gfc_defer_symbol_init (sym);
1878 /* Set the vptr of unlimited polymorphic pointer variables so that
1879 they do not cause segfaults in select type, when the selector
1880 is an intrinsic type. Arrays are captured above. */
1881 if (sym->ts.type == BT_CLASS && UNLIMITED_POLY (sym)
1882 && CLASS_DATA (sym)->attr.class_pointer
1883 && !CLASS_DATA (sym)->attr.dimension && !sym->attr.dummy
1884 && sym->attr.flavor == FL_VARIABLE && !sym->assoc)
1885 gfc_defer_symbol_init (sym);
1887 if (sym->ts.type == BT_CHARACTER
1888 && sym->attr.allocatable
1889 && !sym->attr.dimension
1890 && sym->ts.u.cl && sym->ts.u.cl->length
1891 && sym->ts.u.cl->length->expr_type == EXPR_VARIABLE)
1892 gfc_defer_symbol_init (sym);
1894 /* Associate names can use the hidden string length variable
1895 of their associated target. */
1896 if (sym->ts.type == BT_CHARACTER
1897 && TREE_CODE (length) != INTEGER_CST
1898 && TREE_CODE (sym->ts.u.cl->backend_decl) != INDIRECT_REF)
1900 length = fold_convert (gfc_charlen_type_node, length);
1901 gfc_finish_var_decl (length, sym);
1902 if (!sym->attr.associate_var
1903 && VAR_P (length)
1904 && sym->value && sym->value->expr_type != EXPR_NULL
1905 && sym->value->ts.u.cl->length)
1907 gfc_expr *len = sym->value->ts.u.cl->length;
1908 DECL_INITIAL (length) = gfc_conv_initializer (len, &len->ts,
1909 TREE_TYPE (length),
1910 false, false, false);
1911 DECL_INITIAL (length) = fold_convert (gfc_charlen_type_node,
1912 DECL_INITIAL (length));
1914 else
1915 gcc_assert (!sym->value || sym->value->expr_type == EXPR_NULL);
1918 gfc_finish_var_decl (decl, sym);
1920 if (sym->ts.type == BT_CHARACTER)
1921 /* Character variables need special handling. */
1922 gfc_allocate_lang_decl (decl);
1924 if (sym->assoc && sym->attr.subref_array_pointer)
1925 sym->attr.pointer = 1;
1927 if (sym->attr.pointer && sym->attr.dimension
1928 && !sym->ts.deferred
1929 && !(sym->attr.select_type_temporary
1930 && !sym->attr.subref_array_pointer))
1931 GFC_DECL_PTR_ARRAY_P (decl) = 1;
1933 if (sym->ts.type == BT_CLASS)
1934 GFC_DECL_CLASS(decl) = 1;
1936 sym->backend_decl = decl;
1938 if (sym->attr.assign)
1939 gfc_add_assign_aux_vars (sym);
1941 if (intrinsic_array_parameter)
1943 TREE_STATIC (decl) = 1;
1944 DECL_EXTERNAL (decl) = 0;
1947 if (TREE_STATIC (decl)
1948 && !(sym->attr.use_assoc && !intrinsic_array_parameter)
1949 && (sym->attr.save || sym->ns->proc_name->attr.is_main_program
1950 || !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
1951 || sym->attr.data || sym->ns->proc_name->attr.flavor == FL_MODULE)
1952 && (flag_coarray != GFC_FCOARRAY_LIB
1953 || !sym->attr.codimension || sym->attr.allocatable)
1954 && !(sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.pdt_type)
1955 && !(sym->ts.type == BT_CLASS
1956 && CLASS_DATA (sym)->ts.u.derived->attr.pdt_type))
1958 /* Add static initializer. For procedures, it is only needed if
1959 SAVE is specified otherwise they need to be reinitialized
1960 every time the procedure is entered. The TREE_STATIC is
1961 in this case due to -fmax-stack-var-size=. */
1963 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
1964 TREE_TYPE (decl), sym->attr.dimension
1965 || (sym->attr.codimension
1966 && sym->attr.allocatable),
1967 sym->attr.pointer || sym->attr.allocatable
1968 || sym->ts.type == BT_CLASS,
1969 sym->attr.proc_pointer);
1972 if (!TREE_STATIC (decl)
1973 && POINTER_TYPE_P (TREE_TYPE (decl))
1974 && !sym->attr.pointer
1975 && !sym->attr.allocatable
1976 && !sym->attr.proc_pointer
1977 && !sym->attr.select_type_temporary)
1978 DECL_BY_REFERENCE (decl) = 1;
1980 if (sym->attr.associate_var)
1981 GFC_DECL_ASSOCIATE_VAR_P (decl) = 1;
1983 /* We only longer mark __def_init as read-only if it actually has an
1984 initializer, it does not needlessly take up space in the
1985 read-only section and can go into the BSS instead, see PR 84487.
1986 Marking this as artificial means that OpenMP will treat this as
1987 predetermined shared. */
1989 bool def_init = startswith (sym->name, "__def_init");
1991 if (sym->attr.vtab || def_init)
1993 DECL_ARTIFICIAL (decl) = 1;
1994 if (def_init && sym->value)
1995 TREE_READONLY (decl) = 1;
1998 return decl;
2002 /* Substitute a temporary variable in place of the real one. */
2004 void
2005 gfc_shadow_sym (gfc_symbol * sym, tree decl, gfc_saved_var * save)
2007 save->attr = sym->attr;
2008 save->decl = sym->backend_decl;
2010 gfc_clear_attr (&sym->attr);
2011 sym->attr.referenced = 1;
2012 sym->attr.flavor = FL_VARIABLE;
2014 sym->backend_decl = decl;
2018 /* Restore the original variable. */
2020 void
2021 gfc_restore_sym (gfc_symbol * sym, gfc_saved_var * save)
2023 sym->attr = save->attr;
2024 sym->backend_decl = save->decl;
2028 /* Declare a procedure pointer. */
2030 static tree
2031 get_proc_pointer_decl (gfc_symbol *sym)
2033 tree decl;
2034 tree attributes;
2036 if (sym->module || sym->fn_result_spec)
2038 const char *name;
2039 gfc_gsymbol *gsym;
2041 name = mangled_identifier (sym);
2042 gsym = gfc_find_gsymbol (gfc_gsym_root, name);
2043 if (gsym != NULL)
2045 gfc_symbol *s;
2046 gfc_find_symbol (sym->name, gsym->ns, 0, &s);
2047 if (s && s->backend_decl)
2048 return s->backend_decl;
2052 decl = sym->backend_decl;
2053 if (decl)
2054 return decl;
2056 decl = build_decl (input_location,
2057 VAR_DECL, get_identifier (sym->name),
2058 build_pointer_type (gfc_get_function_type (sym)));
2060 if (sym->module)
2062 /* Apply name mangling. */
2063 gfc_set_decl_assembler_name (decl, gfc_sym_mangled_identifier (sym));
2064 if (sym->attr.use_assoc)
2065 DECL_IGNORED_P (decl) = 1;
2068 if ((sym->ns->proc_name
2069 && sym->ns->proc_name->backend_decl == current_function_decl)
2070 || sym->attr.contained)
2071 gfc_add_decl_to_function (decl);
2072 else if (sym->ns->proc_name->attr.flavor != FL_MODULE)
2073 gfc_add_decl_to_parent_function (decl);
2075 sym->backend_decl = decl;
2077 /* If a variable is USE associated, it's always external. */
2078 if (sym->attr.use_assoc)
2080 DECL_EXTERNAL (decl) = 1;
2081 TREE_PUBLIC (decl) = 1;
2083 else if (sym->module && sym->ns->proc_name->attr.flavor == FL_MODULE)
2085 /* This is the declaration of a module variable. */
2086 TREE_PUBLIC (decl) = 1;
2087 if (sym->attr.access == ACCESS_PRIVATE && !sym->attr.public_used)
2089 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2090 DECL_VISIBILITY_SPECIFIED (decl) = true;
2092 TREE_STATIC (decl) = 1;
2095 if (!sym->attr.use_assoc
2096 && (sym->attr.save != SAVE_NONE || sym->attr.data
2097 || (sym->value && sym->ns->proc_name->attr.is_main_program)))
2098 TREE_STATIC (decl) = 1;
2100 if (TREE_STATIC (decl) && sym->value)
2102 /* Add static initializer. */
2103 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
2104 TREE_TYPE (decl),
2105 sym->attr.dimension,
2106 false, true);
2109 /* Handle threadprivate procedure pointers. */
2110 if (sym->attr.threadprivate
2111 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2112 set_decl_tls_model (decl, decl_default_tls_model (decl));
2114 attributes = add_attributes_to_decl (sym->attr, NULL_TREE);
2115 decl_attributes (&decl, attributes, 0);
2117 return decl;
2121 /* Get a basic decl for an external function. */
2123 tree
2124 gfc_get_extern_function_decl (gfc_symbol * sym, gfc_actual_arglist *actual_args,
2125 const char *fnspec)
2127 tree type;
2128 tree fndecl;
2129 tree attributes;
2130 gfc_expr e;
2131 gfc_intrinsic_sym *isym;
2132 gfc_expr argexpr;
2133 char s[GFC_MAX_SYMBOL_LEN + 23]; /* "_gfortran_f2c_specific" and '\0'. */
2134 tree name;
2135 tree mangled_name;
2136 gfc_gsymbol *gsym;
2138 if (sym->backend_decl)
2139 return sym->backend_decl;
2141 /* We should never be creating external decls for alternate entry points.
2142 The procedure may be an alternate entry point, but we don't want/need
2143 to know that. */
2144 gcc_assert (!(sym->attr.entry || sym->attr.entry_master));
2146 if (sym->attr.proc_pointer)
2147 return get_proc_pointer_decl (sym);
2149 /* See if this is an external procedure from the same file. If so,
2150 return the backend_decl. If we are looking at a BIND(C)
2151 procedure and the symbol is not BIND(C), or vice versa, we
2152 haven't found the right procedure. */
2154 if (sym->binding_label)
2156 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
2157 if (gsym && !gsym->bind_c)
2158 gsym = NULL;
2160 else if (sym->module == NULL)
2162 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->name);
2163 if (gsym && gsym->bind_c)
2164 gsym = NULL;
2166 else
2168 /* Procedure from a different module. */
2169 gsym = NULL;
2172 if (gsym && !gsym->defined)
2173 gsym = NULL;
2175 /* This can happen because of C binding. */
2176 if (gsym && gsym->ns && gsym->ns->proc_name
2177 && gsym->ns->proc_name->attr.flavor == FL_MODULE)
2178 goto module_sym;
2180 if ((!sym->attr.use_assoc || sym->attr.if_source != IFSRC_DECL)
2181 && !sym->backend_decl
2182 && gsym && gsym->ns
2183 && ((gsym->type == GSYM_SUBROUTINE) || (gsym->type == GSYM_FUNCTION))
2184 && (gsym->ns->proc_name->backend_decl || !sym->attr.intrinsic))
2186 if (!gsym->ns->proc_name->backend_decl)
2188 /* By construction, the external function cannot be
2189 a contained procedure. */
2190 locus old_loc;
2192 gfc_save_backend_locus (&old_loc);
2193 push_cfun (NULL);
2195 gfc_create_function_decl (gsym->ns, true);
2197 pop_cfun ();
2198 gfc_restore_backend_locus (&old_loc);
2201 /* If the namespace has entries, the proc_name is the
2202 entry master. Find the entry and use its backend_decl.
2203 otherwise, use the proc_name backend_decl. */
2204 if (gsym->ns->entries)
2206 gfc_entry_list *entry = gsym->ns->entries;
2208 for (; entry; entry = entry->next)
2210 if (strcmp (gsym->name, entry->sym->name) == 0)
2212 sym->backend_decl = entry->sym->backend_decl;
2213 break;
2217 else
2218 sym->backend_decl = gsym->ns->proc_name->backend_decl;
2220 if (sym->backend_decl)
2222 /* Avoid problems of double deallocation of the backend declaration
2223 later in gfc_trans_use_stmts; cf. PR 45087. */
2224 if (sym->attr.if_source != IFSRC_DECL && sym->attr.use_assoc)
2225 sym->attr.use_assoc = 0;
2227 return sym->backend_decl;
2231 /* See if this is a module procedure from the same file. If so,
2232 return the backend_decl. */
2233 if (sym->module)
2234 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->module);
2236 module_sym:
2237 if (gsym && gsym->ns
2238 && (gsym->type == GSYM_MODULE
2239 || (gsym->ns->proc_name && gsym->ns->proc_name->attr.flavor == FL_MODULE)))
2241 gfc_symbol *s;
2243 s = NULL;
2244 if (gsym->type == GSYM_MODULE)
2245 gfc_find_symbol (sym->name, gsym->ns, 0, &s);
2246 else
2247 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &s);
2249 if (s && s->backend_decl)
2251 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
2252 gfc_copy_dt_decls_ifequal (s->ts.u.derived, sym->ts.u.derived,
2253 true);
2254 else if (sym->ts.type == BT_CHARACTER)
2255 sym->ts.u.cl->backend_decl = s->ts.u.cl->backend_decl;
2256 sym->backend_decl = s->backend_decl;
2257 return sym->backend_decl;
2261 if (sym->attr.intrinsic)
2263 /* Call the resolution function to get the actual name. This is
2264 a nasty hack which relies on the resolution functions only looking
2265 at the first argument. We pass NULL for the second argument
2266 otherwise things like AINT get confused. */
2267 isym = gfc_find_function (sym->name);
2268 gcc_assert (isym->resolve.f0 != NULL);
2270 memset (&e, 0, sizeof (e));
2271 e.expr_type = EXPR_FUNCTION;
2273 memset (&argexpr, 0, sizeof (argexpr));
2274 gcc_assert (isym->formal);
2275 argexpr.ts = isym->formal->ts;
2277 if (isym->formal->next == NULL)
2278 isym->resolve.f1 (&e, &argexpr);
2279 else
2281 if (isym->formal->next->next == NULL)
2282 isym->resolve.f2 (&e, &argexpr, NULL);
2283 else
2285 if (isym->formal->next->next->next == NULL)
2286 isym->resolve.f3 (&e, &argexpr, NULL, NULL);
2287 else
2289 /* All specific intrinsics take less than 5 arguments. */
2290 gcc_assert (isym->formal->next->next->next->next == NULL);
2291 isym->resolve.f4 (&e, &argexpr, NULL, NULL, NULL);
2296 if (flag_f2c
2297 && ((e.ts.type == BT_REAL && e.ts.kind == gfc_default_real_kind)
2298 || e.ts.type == BT_COMPLEX))
2300 /* Specific which needs a different implementation if f2c
2301 calling conventions are used. */
2302 sprintf (s, "_gfortran_f2c_specific%s", e.value.function.name);
2304 else
2305 sprintf (s, "_gfortran_specific%s", e.value.function.name);
2307 name = get_identifier (s);
2308 mangled_name = name;
2310 else
2312 name = gfc_sym_identifier (sym);
2313 mangled_name = gfc_sym_mangled_function_id (sym);
2316 type = gfc_get_function_type (sym, actual_args, fnspec);
2318 fndecl = build_decl (input_location,
2319 FUNCTION_DECL, name, type);
2321 /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
2322 TREE_PUBLIC specifies whether a function is globally addressable (i.e.
2323 the opposite of declaring a function as static in C). */
2324 DECL_EXTERNAL (fndecl) = 1;
2325 TREE_PUBLIC (fndecl) = 1;
2327 attributes = add_attributes_to_decl (sym->attr, NULL_TREE);
2328 decl_attributes (&fndecl, attributes, 0);
2330 gfc_set_decl_assembler_name (fndecl, mangled_name);
2332 /* Set the context of this decl. */
2333 if (0 && sym->ns && sym->ns->proc_name)
2335 /* TODO: Add external decls to the appropriate scope. */
2336 DECL_CONTEXT (fndecl) = sym->ns->proc_name->backend_decl;
2338 else
2340 /* Global declaration, e.g. intrinsic subroutine. */
2341 DECL_CONTEXT (fndecl) = NULL_TREE;
2344 /* Set attributes for PURE functions. A call to PURE function in the
2345 Fortran 95 sense is both pure and without side effects in the C
2346 sense. */
2347 if (sym->attr.pure || sym->attr.implicit_pure)
2349 if (sym->attr.function && !gfc_return_by_reference (sym))
2350 DECL_PURE_P (fndecl) = 1;
2351 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
2352 parameters and don't use alternate returns (is this
2353 allowed?). In that case, calls to them are meaningless, and
2354 can be optimized away. See also in build_function_decl(). */
2355 TREE_SIDE_EFFECTS (fndecl) = 0;
2358 /* Mark non-returning functions. */
2359 if (sym->attr.noreturn || sym->attr.ext_attr & (1 << EXT_ATTR_NORETURN))
2360 TREE_THIS_VOLATILE(fndecl) = 1;
2362 sym->backend_decl = fndecl;
2364 if (DECL_CONTEXT (fndecl) == NULL_TREE)
2365 pushdecl_top_level (fndecl);
2367 if (sym->formal_ns
2368 && sym->formal_ns->proc_name == sym)
2370 if (sym->formal_ns->omp_declare_simd)
2371 gfc_trans_omp_declare_simd (sym->formal_ns);
2372 if (flag_openmp)
2373 gfc_trans_omp_declare_variant (sym->formal_ns);
2376 return fndecl;
2380 /* Create a declaration for a procedure. For external functions (in the C
2381 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
2382 a master function with alternate entry points. */
2384 static void
2385 build_function_decl (gfc_symbol * sym, bool global)
2387 tree fndecl, type, attributes;
2388 symbol_attribute attr;
2389 tree result_decl;
2390 gfc_formal_arglist *f;
2392 bool module_procedure = sym->attr.module_procedure
2393 && sym->ns
2394 && sym->ns->proc_name
2395 && sym->ns->proc_name->attr.flavor == FL_MODULE;
2397 gcc_assert (!sym->attr.external || module_procedure);
2399 if (sym->backend_decl)
2400 return;
2402 /* Set the line and filename. sym->declared_at seems to point to the
2403 last statement for subroutines, but it'll do for now. */
2404 gfc_set_backend_locus (&sym->declared_at);
2406 /* Allow only one nesting level. Allow public declarations. */
2407 gcc_assert (current_function_decl == NULL_TREE
2408 || DECL_FILE_SCOPE_P (current_function_decl)
2409 || (TREE_CODE (DECL_CONTEXT (current_function_decl))
2410 == NAMESPACE_DECL));
2412 type = gfc_get_function_type (sym);
2413 fndecl = build_decl (input_location,
2414 FUNCTION_DECL, gfc_sym_identifier (sym), type);
2416 attr = sym->attr;
2418 /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
2419 TREE_PUBLIC specifies whether a function is globally addressable (i.e.
2420 the opposite of declaring a function as static in C). */
2421 DECL_EXTERNAL (fndecl) = 0;
2423 if (sym->attr.access == ACCESS_UNKNOWN && sym->module
2424 && (sym->ns->default_access == ACCESS_PRIVATE
2425 || (sym->ns->default_access == ACCESS_UNKNOWN
2426 && flag_module_private)))
2427 sym->attr.access = ACCESS_PRIVATE;
2429 if (!current_function_decl
2430 && !sym->attr.entry_master && !sym->attr.is_main_program
2431 && (sym->attr.access != ACCESS_PRIVATE || sym->binding_label
2432 || sym->attr.public_used))
2433 TREE_PUBLIC (fndecl) = 1;
2435 if (sym->attr.referenced || sym->attr.entry_master)
2436 TREE_USED (fndecl) = 1;
2438 attributes = add_attributes_to_decl (attr, NULL_TREE);
2439 decl_attributes (&fndecl, attributes, 0);
2441 /* Figure out the return type of the declared function, and build a
2442 RESULT_DECL for it. If this is a subroutine with alternate
2443 returns, build a RESULT_DECL for it. */
2444 result_decl = NULL_TREE;
2445 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
2446 if (attr.function)
2448 if (gfc_return_by_reference (sym))
2449 type = void_type_node;
2450 else
2452 if (sym->result != sym)
2453 result_decl = gfc_sym_identifier (sym->result);
2455 type = TREE_TYPE (TREE_TYPE (fndecl));
2458 else
2460 /* Look for alternate return placeholders. */
2461 int has_alternate_returns = 0;
2462 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2464 if (f->sym == NULL)
2466 has_alternate_returns = 1;
2467 break;
2471 if (has_alternate_returns)
2472 type = integer_type_node;
2473 else
2474 type = void_type_node;
2477 result_decl = build_decl (input_location,
2478 RESULT_DECL, result_decl, type);
2479 DECL_ARTIFICIAL (result_decl) = 1;
2480 DECL_IGNORED_P (result_decl) = 1;
2481 DECL_CONTEXT (result_decl) = fndecl;
2482 DECL_RESULT (fndecl) = result_decl;
2484 /* Don't call layout_decl for a RESULT_DECL.
2485 layout_decl (result_decl, 0); */
2487 /* TREE_STATIC means the function body is defined here. */
2488 TREE_STATIC (fndecl) = 1;
2490 /* Set attributes for PURE functions. A call to a PURE function in the
2491 Fortran 95 sense is both pure and without side effects in the C
2492 sense. */
2493 if (attr.pure || attr.implicit_pure)
2495 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
2496 including an alternate return. In that case it can also be
2497 marked as PURE. See also in gfc_get_extern_function_decl(). */
2498 if (attr.function && !gfc_return_by_reference (sym))
2499 DECL_PURE_P (fndecl) = 1;
2500 TREE_SIDE_EFFECTS (fndecl) = 0;
2503 /* Mark noinline functions. */
2504 if (attr.ext_attr & (1 << EXT_ATTR_NOINLINE))
2505 DECL_UNINLINABLE (fndecl) = 1;
2507 /* Mark noreturn functions. */
2508 if (attr.ext_attr & (1 << EXT_ATTR_NORETURN))
2509 TREE_THIS_VOLATILE (fndecl) = 1;
2511 /* Mark weak functions. */
2512 if (attr.ext_attr & (1 << EXT_ATTR_WEAK))
2513 declare_weak (fndecl);
2515 /* Layout the function declaration and put it in the binding level
2516 of the current function. */
2518 if (global)
2519 pushdecl_top_level (fndecl);
2520 else
2521 pushdecl (fndecl);
2523 /* Perform name mangling if this is a top level or module procedure. */
2524 if (current_function_decl == NULL_TREE)
2525 gfc_set_decl_assembler_name (fndecl, gfc_sym_mangled_function_id (sym));
2527 sym->backend_decl = fndecl;
2531 /* Create the DECL_ARGUMENTS for a procedure.
2532 NOTE: The arguments added here must match the argument type created by
2533 gfc_get_function_type (). */
2535 static void
2536 create_function_arglist (gfc_symbol * sym)
2538 tree fndecl;
2539 gfc_formal_arglist *f;
2540 tree typelist, hidden_typelist, optval_typelist;
2541 tree arglist, hidden_arglist, optval_arglist;
2542 tree type;
2543 tree parm;
2545 fndecl = sym->backend_decl;
2547 /* Build formal argument list. Make sure that their TREE_CONTEXT is
2548 the new FUNCTION_DECL node. */
2549 arglist = NULL_TREE;
2550 hidden_arglist = NULL_TREE;
2551 optval_arglist = NULL_TREE;
2552 typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2554 if (sym->attr.entry_master)
2556 type = TREE_VALUE (typelist);
2557 parm = build_decl (input_location,
2558 PARM_DECL, get_identifier ("__entry"), type);
2560 DECL_CONTEXT (parm) = fndecl;
2561 DECL_ARG_TYPE (parm) = type;
2562 TREE_READONLY (parm) = 1;
2563 gfc_finish_decl (parm);
2564 DECL_ARTIFICIAL (parm) = 1;
2566 arglist = chainon (arglist, parm);
2567 typelist = TREE_CHAIN (typelist);
2570 if (gfc_return_by_reference (sym))
2572 tree type = TREE_VALUE (typelist), length = NULL;
2574 if (sym->ts.type == BT_CHARACTER)
2576 /* Length of character result. */
2577 tree len_type = TREE_VALUE (TREE_CHAIN (typelist));
2579 length = build_decl (input_location,
2580 PARM_DECL,
2581 get_identifier (".__result"),
2582 len_type);
2583 if (POINTER_TYPE_P (len_type))
2585 sym->ts.u.cl->passed_length = length;
2586 TREE_USED (length) = 1;
2588 else if (!sym->ts.u.cl->length)
2590 sym->ts.u.cl->backend_decl = length;
2591 TREE_USED (length) = 1;
2593 gcc_assert (TREE_CODE (length) == PARM_DECL);
2594 DECL_CONTEXT (length) = fndecl;
2595 DECL_ARG_TYPE (length) = len_type;
2596 TREE_READONLY (length) = 1;
2597 DECL_ARTIFICIAL (length) = 1;
2598 gfc_finish_decl (length);
2599 if (sym->ts.u.cl->backend_decl == NULL
2600 || sym->ts.u.cl->backend_decl == length)
2602 gfc_symbol *arg;
2603 tree backend_decl;
2605 if (sym->ts.u.cl->backend_decl == NULL)
2607 tree len = build_decl (input_location,
2608 VAR_DECL,
2609 get_identifier ("..__result"),
2610 gfc_charlen_type_node);
2611 DECL_ARTIFICIAL (len) = 1;
2612 TREE_USED (len) = 1;
2613 sym->ts.u.cl->backend_decl = len;
2616 /* Make sure PARM_DECL type doesn't point to incomplete type. */
2617 arg = sym->result ? sym->result : sym;
2618 backend_decl = arg->backend_decl;
2619 /* Temporary clear it, so that gfc_sym_type creates complete
2620 type. */
2621 arg->backend_decl = NULL;
2622 type = gfc_sym_type (arg);
2623 arg->backend_decl = backend_decl;
2624 type = build_reference_type (type);
2628 parm = build_decl (input_location,
2629 PARM_DECL, get_identifier ("__result"), type);
2631 DECL_CONTEXT (parm) = fndecl;
2632 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
2633 TREE_READONLY (parm) = 1;
2634 DECL_ARTIFICIAL (parm) = 1;
2635 gfc_finish_decl (parm);
2637 arglist = chainon (arglist, parm);
2638 typelist = TREE_CHAIN (typelist);
2640 if (sym->ts.type == BT_CHARACTER)
2642 gfc_allocate_lang_decl (parm);
2643 arglist = chainon (arglist, length);
2644 typelist = TREE_CHAIN (typelist);
2648 hidden_typelist = typelist;
2649 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2650 if (f->sym != NULL) /* Ignore alternate returns. */
2651 hidden_typelist = TREE_CHAIN (hidden_typelist);
2653 /* Advance hidden_typelist over optional+value argument presence flags. */
2654 optval_typelist = hidden_typelist;
2655 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2656 if (f->sym != NULL
2657 && f->sym->attr.optional && f->sym->attr.value
2658 && !f->sym->attr.dimension && f->sym->ts.type != BT_CLASS
2659 && !gfc_bt_struct (f->sym->ts.type))
2660 hidden_typelist = TREE_CHAIN (hidden_typelist);
2662 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2664 char name[GFC_MAX_SYMBOL_LEN + 2];
2666 /* Ignore alternate returns. */
2667 if (f->sym == NULL)
2668 continue;
2670 type = TREE_VALUE (typelist);
2672 if (f->sym->ts.type == BT_CHARACTER
2673 && (!sym->attr.is_bind_c || sym->attr.entry_master))
2675 tree len_type = TREE_VALUE (hidden_typelist);
2676 tree length = NULL_TREE;
2677 if (!f->sym->ts.deferred)
2678 gcc_assert (len_type == gfc_charlen_type_node);
2679 else
2680 gcc_assert (POINTER_TYPE_P (len_type));
2682 strcpy (&name[1], f->sym->name);
2683 name[0] = '_';
2684 length = build_decl (input_location,
2685 PARM_DECL, get_identifier (name), len_type);
2687 hidden_arglist = chainon (hidden_arglist, length);
2688 DECL_CONTEXT (length) = fndecl;
2689 DECL_ARTIFICIAL (length) = 1;
2690 DECL_ARG_TYPE (length) = len_type;
2691 TREE_READONLY (length) = 1;
2692 gfc_finish_decl (length);
2694 /* Marking the length DECL_HIDDEN_STRING_LENGTH will lead
2695 to tail calls being disabled. Only do that if we
2696 potentially have broken callers. */
2697 if (flag_tail_call_workaround
2698 && f->sym->ts.u.cl
2699 && f->sym->ts.u.cl->length
2700 && f->sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
2701 && (flag_tail_call_workaround == 2
2702 || f->sym->ns->implicit_interface_calls))
2703 DECL_HIDDEN_STRING_LENGTH (length) = 1;
2705 /* Remember the passed value. */
2706 if (!f->sym->ts.u.cl || f->sym->ts.u.cl->passed_length)
2708 /* This can happen if the same type is used for multiple
2709 arguments. We need to copy cl as otherwise
2710 cl->passed_length gets overwritten. */
2711 f->sym->ts.u.cl = gfc_new_charlen (f->sym->ns, f->sym->ts.u.cl);
2713 f->sym->ts.u.cl->passed_length = length;
2715 /* Use the passed value for assumed length variables. */
2716 if (!f->sym->ts.u.cl->length)
2718 TREE_USED (length) = 1;
2719 gcc_assert (!f->sym->ts.u.cl->backend_decl);
2720 f->sym->ts.u.cl->backend_decl = length;
2723 hidden_typelist = TREE_CHAIN (hidden_typelist);
2725 if (f->sym->ts.u.cl->backend_decl == NULL
2726 || f->sym->ts.u.cl->backend_decl == length)
2728 if (POINTER_TYPE_P (len_type))
2729 f->sym->ts.u.cl->backend_decl
2730 = build_fold_indirect_ref_loc (input_location, length);
2731 else if (f->sym->ts.u.cl->backend_decl == NULL)
2732 gfc_create_string_length (f->sym);
2734 /* Make sure PARM_DECL type doesn't point to incomplete type. */
2735 if (f->sym->attr.flavor == FL_PROCEDURE)
2736 type = build_pointer_type (gfc_get_function_type (f->sym));
2737 else
2738 type = gfc_sym_type (f->sym);
2741 /* For scalar intrinsic types, VALUE passes the value,
2742 hence, the optional status cannot be transferred via a NULL pointer.
2743 Thus, we will use a hidden argument in that case. */
2744 if (f->sym->attr.optional && f->sym->attr.value
2745 && !f->sym->attr.dimension && f->sym->ts.type != BT_CLASS
2746 && !gfc_bt_struct (f->sym->ts.type))
2748 tree tmp;
2749 strcpy (&name[1], f->sym->name);
2750 name[0] = '.';
2751 tmp = build_decl (input_location,
2752 PARM_DECL, get_identifier (name),
2753 boolean_type_node);
2755 optval_arglist = chainon (optval_arglist, tmp);
2756 DECL_CONTEXT (tmp) = fndecl;
2757 DECL_ARTIFICIAL (tmp) = 1;
2758 DECL_ARG_TYPE (tmp) = boolean_type_node;
2759 TREE_READONLY (tmp) = 1;
2760 gfc_finish_decl (tmp);
2762 /* The presence flag must be boolean. */
2763 gcc_assert (TREE_VALUE (optval_typelist) == boolean_type_node);
2764 optval_typelist = TREE_CHAIN (optval_typelist);
2767 /* For non-constant length array arguments, make sure they use
2768 a different type node from TYPE_ARG_TYPES type. */
2769 if (f->sym->attr.dimension
2770 && type == TREE_VALUE (typelist)
2771 && TREE_CODE (type) == POINTER_TYPE
2772 && GFC_ARRAY_TYPE_P (type)
2773 && f->sym->as->type != AS_ASSUMED_SIZE
2774 && ! COMPLETE_TYPE_P (TREE_TYPE (type)))
2776 if (f->sym->attr.flavor == FL_PROCEDURE)
2777 type = build_pointer_type (gfc_get_function_type (f->sym));
2778 else
2779 type = gfc_sym_type (f->sym);
2782 if (f->sym->attr.proc_pointer)
2783 type = build_pointer_type (type);
2785 if (f->sym->attr.volatile_)
2786 type = build_qualified_type (type, TYPE_QUAL_VOLATILE);
2788 /* Build the argument declaration. For C descriptors, we use a
2789 '_'-prefixed name for the parm_decl and inside the proc the
2790 sym->name. */
2791 tree parm_name;
2792 if (sym->attr.is_bind_c && is_CFI_desc (f->sym, NULL))
2794 strcpy (&name[1], f->sym->name);
2795 name[0] = '_';
2796 parm_name = get_identifier (name);
2798 else
2799 parm_name = gfc_sym_identifier (f->sym);
2800 parm = build_decl (input_location, PARM_DECL, parm_name, type);
2802 if (f->sym->attr.volatile_)
2804 TREE_THIS_VOLATILE (parm) = 1;
2805 TREE_SIDE_EFFECTS (parm) = 1;
2808 /* Fill in arg stuff. */
2809 DECL_CONTEXT (parm) = fndecl;
2810 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
2811 /* All implementation args except for VALUE are read-only. */
2812 if (!f->sym->attr.value)
2813 TREE_READONLY (parm) = 1;
2814 if (POINTER_TYPE_P (type)
2815 && (!f->sym->attr.proc_pointer
2816 && f->sym->attr.flavor != FL_PROCEDURE))
2817 DECL_BY_REFERENCE (parm) = 1;
2818 if (f->sym->attr.optional)
2820 gfc_allocate_lang_decl (parm);
2821 GFC_DECL_OPTIONAL_ARGUMENT (parm) = 1;
2824 gfc_finish_decl (parm);
2825 gfc_finish_decl_attrs (parm, &f->sym->attr);
2827 f->sym->backend_decl = parm;
2829 /* Coarrays which are descriptorless or assumed-shape pass with
2830 -fcoarray=lib the token and the offset as hidden arguments. */
2831 if (flag_coarray == GFC_FCOARRAY_LIB
2832 && ((f->sym->ts.type != BT_CLASS && f->sym->attr.codimension
2833 && !f->sym->attr.allocatable)
2834 || (f->sym->ts.type == BT_CLASS
2835 && CLASS_DATA (f->sym)->attr.codimension
2836 && !CLASS_DATA (f->sym)->attr.allocatable)))
2838 tree caf_type;
2839 tree token;
2840 tree offset;
2842 gcc_assert (f->sym->backend_decl != NULL_TREE
2843 && !sym->attr.is_bind_c);
2844 caf_type = f->sym->ts.type == BT_CLASS
2845 ? TREE_TYPE (CLASS_DATA (f->sym)->backend_decl)
2846 : TREE_TYPE (f->sym->backend_decl);
2848 token = build_decl (input_location, PARM_DECL,
2849 create_tmp_var_name ("caf_token"),
2850 build_qualified_type (pvoid_type_node,
2851 TYPE_QUAL_RESTRICT));
2852 if ((f->sym->ts.type != BT_CLASS
2853 && f->sym->as->type != AS_DEFERRED)
2854 || (f->sym->ts.type == BT_CLASS
2855 && CLASS_DATA (f->sym)->as->type != AS_DEFERRED))
2857 gcc_assert (DECL_LANG_SPECIFIC (f->sym->backend_decl) == NULL
2858 || GFC_DECL_TOKEN (f->sym->backend_decl) == NULL_TREE);
2859 if (DECL_LANG_SPECIFIC (f->sym->backend_decl) == NULL)
2860 gfc_allocate_lang_decl (f->sym->backend_decl);
2861 GFC_DECL_TOKEN (f->sym->backend_decl) = token;
2863 else
2865 gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) == NULL_TREE);
2866 GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) = token;
2869 DECL_CONTEXT (token) = fndecl;
2870 DECL_ARTIFICIAL (token) = 1;
2871 DECL_ARG_TYPE (token) = TREE_VALUE (typelist);
2872 TREE_READONLY (token) = 1;
2873 hidden_arglist = chainon (hidden_arglist, token);
2874 hidden_typelist = TREE_CHAIN (hidden_typelist);
2875 gfc_finish_decl (token);
2877 offset = build_decl (input_location, PARM_DECL,
2878 create_tmp_var_name ("caf_offset"),
2879 gfc_array_index_type);
2881 if ((f->sym->ts.type != BT_CLASS
2882 && f->sym->as->type != AS_DEFERRED)
2883 || (f->sym->ts.type == BT_CLASS
2884 && CLASS_DATA (f->sym)->as->type != AS_DEFERRED))
2886 gcc_assert (GFC_DECL_CAF_OFFSET (f->sym->backend_decl)
2887 == NULL_TREE);
2888 GFC_DECL_CAF_OFFSET (f->sym->backend_decl) = offset;
2890 else
2892 gcc_assert (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) == NULL_TREE);
2893 GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) = offset;
2895 DECL_CONTEXT (offset) = fndecl;
2896 DECL_ARTIFICIAL (offset) = 1;
2897 DECL_ARG_TYPE (offset) = TREE_VALUE (typelist);
2898 TREE_READONLY (offset) = 1;
2899 hidden_arglist = chainon (hidden_arglist, offset);
2900 hidden_typelist = TREE_CHAIN (hidden_typelist);
2901 gfc_finish_decl (offset);
2904 arglist = chainon (arglist, parm);
2905 typelist = TREE_CHAIN (typelist);
2908 /* Add hidden present status for optional+value arguments. */
2909 arglist = chainon (arglist, optval_arglist);
2911 /* Add the hidden string length parameters, unless the procedure
2912 is bind(C). */
2913 if (!sym->attr.is_bind_c)
2914 arglist = chainon (arglist, hidden_arglist);
2916 gcc_assert (hidden_typelist == NULL_TREE
2917 || TREE_VALUE (hidden_typelist) == void_type_node);
2918 DECL_ARGUMENTS (fndecl) = arglist;
2921 /* Do the setup necessary before generating the body of a function. */
2923 static void
2924 trans_function_start (gfc_symbol * sym)
2926 tree fndecl;
2928 fndecl = sym->backend_decl;
2930 /* Let GCC know the current scope is this function. */
2931 current_function_decl = fndecl;
2933 /* Let the world know what we're about to do. */
2934 announce_function (fndecl);
2936 if (DECL_FILE_SCOPE_P (fndecl))
2938 /* Create RTL for function declaration. */
2939 rest_of_decl_compilation (fndecl, 1, 0);
2942 /* Create RTL for function definition. */
2943 make_decl_rtl (fndecl);
2945 allocate_struct_function (fndecl, false);
2947 /* function.cc requires a push at the start of the function. */
2948 pushlevel ();
2951 /* Create thunks for alternate entry points. */
2953 static void
2954 build_entry_thunks (gfc_namespace * ns, bool global)
2956 gfc_formal_arglist *formal;
2957 gfc_formal_arglist *thunk_formal;
2958 gfc_entry_list *el;
2959 gfc_symbol *thunk_sym;
2960 stmtblock_t body;
2961 tree thunk_fndecl;
2962 tree tmp;
2963 locus old_loc;
2965 /* This should always be a toplevel function. */
2966 gcc_assert (current_function_decl == NULL_TREE);
2968 gfc_save_backend_locus (&old_loc);
2969 for (el = ns->entries; el; el = el->next)
2971 vec<tree, va_gc> *args = NULL;
2972 vec<tree, va_gc> *string_args = NULL;
2974 thunk_sym = el->sym;
2976 build_function_decl (thunk_sym, global);
2977 create_function_arglist (thunk_sym);
2979 trans_function_start (thunk_sym);
2981 thunk_fndecl = thunk_sym->backend_decl;
2983 gfc_init_block (&body);
2985 /* Pass extra parameter identifying this entry point. */
2986 tmp = build_int_cst (gfc_array_index_type, el->id);
2987 vec_safe_push (args, tmp);
2989 if (thunk_sym->attr.function)
2991 if (gfc_return_by_reference (ns->proc_name))
2993 tree ref = DECL_ARGUMENTS (current_function_decl);
2994 vec_safe_push (args, ref);
2995 if (ns->proc_name->ts.type == BT_CHARACTER)
2996 vec_safe_push (args, DECL_CHAIN (ref));
3000 for (formal = gfc_sym_get_dummy_args (ns->proc_name); formal;
3001 formal = formal->next)
3003 /* Ignore alternate returns. */
3004 if (formal->sym == NULL)
3005 continue;
3007 /* We don't have a clever way of identifying arguments, so resort to
3008 a brute-force search. */
3009 for (thunk_formal = gfc_sym_get_dummy_args (thunk_sym);
3010 thunk_formal;
3011 thunk_formal = thunk_formal->next)
3013 if (thunk_formal->sym == formal->sym)
3014 break;
3017 if (thunk_formal)
3019 /* Pass the argument. */
3020 DECL_ARTIFICIAL (thunk_formal->sym->backend_decl) = 1;
3021 vec_safe_push (args, thunk_formal->sym->backend_decl);
3022 if (formal->sym->ts.type == BT_CHARACTER)
3024 tmp = thunk_formal->sym->ts.u.cl->backend_decl;
3025 vec_safe_push (string_args, tmp);
3028 else
3030 /* Pass NULL for a missing argument. */
3031 vec_safe_push (args, null_pointer_node);
3032 if (formal->sym->ts.type == BT_CHARACTER)
3034 tmp = build_int_cst (gfc_charlen_type_node, 0);
3035 vec_safe_push (string_args, tmp);
3040 /* Call the master function. */
3041 vec_safe_splice (args, string_args);
3042 tmp = ns->proc_name->backend_decl;
3043 tmp = build_call_expr_loc_vec (input_location, tmp, args);
3044 if (ns->proc_name->attr.mixed_entry_master)
3046 tree union_decl, field;
3047 tree master_type = TREE_TYPE (ns->proc_name->backend_decl);
3049 union_decl = build_decl (input_location,
3050 VAR_DECL, get_identifier ("__result"),
3051 TREE_TYPE (master_type));
3052 DECL_ARTIFICIAL (union_decl) = 1;
3053 DECL_EXTERNAL (union_decl) = 0;
3054 TREE_PUBLIC (union_decl) = 0;
3055 TREE_USED (union_decl) = 1;
3056 layout_decl (union_decl, 0);
3057 pushdecl (union_decl);
3059 DECL_CONTEXT (union_decl) = current_function_decl;
3060 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
3061 TREE_TYPE (union_decl), union_decl, tmp);
3062 gfc_add_expr_to_block (&body, tmp);
3064 for (field = TYPE_FIELDS (TREE_TYPE (union_decl));
3065 field; field = DECL_CHAIN (field))
3066 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
3067 thunk_sym->result->name) == 0)
3068 break;
3069 gcc_assert (field != NULL_TREE);
3070 tmp = fold_build3_loc (input_location, COMPONENT_REF,
3071 TREE_TYPE (field), union_decl, field,
3072 NULL_TREE);
3073 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
3074 TREE_TYPE (DECL_RESULT (current_function_decl)),
3075 DECL_RESULT (current_function_decl), tmp);
3076 tmp = build1_v (RETURN_EXPR, tmp);
3078 else if (TREE_TYPE (DECL_RESULT (current_function_decl))
3079 != void_type_node)
3081 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
3082 TREE_TYPE (DECL_RESULT (current_function_decl)),
3083 DECL_RESULT (current_function_decl), tmp);
3084 tmp = build1_v (RETURN_EXPR, tmp);
3086 gfc_add_expr_to_block (&body, tmp);
3088 /* Finish off this function and send it for code generation. */
3089 DECL_SAVED_TREE (thunk_fndecl) = gfc_finish_block (&body);
3090 tmp = getdecls ();
3091 poplevel (1, 1);
3092 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl)) = thunk_fndecl;
3093 DECL_SAVED_TREE (thunk_fndecl)
3094 = fold_build3_loc (DECL_SOURCE_LOCATION (thunk_fndecl), BIND_EXPR,
3095 void_type_node, tmp, DECL_SAVED_TREE (thunk_fndecl),
3096 DECL_INITIAL (thunk_fndecl));
3098 /* Output the GENERIC tree. */
3099 dump_function (TDI_original, thunk_fndecl);
3101 /* Store the end of the function, so that we get good line number
3102 info for the epilogue. */
3103 cfun->function_end_locus = input_location;
3105 /* We're leaving the context of this function, so zap cfun.
3106 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
3107 tree_rest_of_compilation. */
3108 set_cfun (NULL);
3110 current_function_decl = NULL_TREE;
3112 cgraph_node::finalize_function (thunk_fndecl, true);
3114 /* We share the symbols in the formal argument list with other entry
3115 points and the master function. Clear them so that they are
3116 recreated for each function. */
3117 for (formal = gfc_sym_get_dummy_args (thunk_sym); formal;
3118 formal = formal->next)
3119 if (formal->sym != NULL) /* Ignore alternate returns. */
3121 formal->sym->backend_decl = NULL_TREE;
3122 if (formal->sym->ts.type == BT_CHARACTER)
3123 formal->sym->ts.u.cl->backend_decl = NULL_TREE;
3126 if (thunk_sym->attr.function)
3128 if (thunk_sym->ts.type == BT_CHARACTER)
3129 thunk_sym->ts.u.cl->backend_decl = NULL_TREE;
3130 if (thunk_sym->result->ts.type == BT_CHARACTER)
3131 thunk_sym->result->ts.u.cl->backend_decl = NULL_TREE;
3135 gfc_restore_backend_locus (&old_loc);
3139 /* Create a decl for a function, and create any thunks for alternate entry
3140 points. If global is true, generate the function in the global binding
3141 level, otherwise in the current binding level (which can be global). */
3143 void
3144 gfc_create_function_decl (gfc_namespace * ns, bool global)
3146 /* Create a declaration for the master function. */
3147 build_function_decl (ns->proc_name, global);
3149 /* Compile the entry thunks. */
3150 if (ns->entries)
3151 build_entry_thunks (ns, global);
3153 /* Now create the read argument list. */
3154 create_function_arglist (ns->proc_name);
3156 if (ns->omp_declare_simd)
3157 gfc_trans_omp_declare_simd (ns);
3159 /* Handle 'declare variant' directives. The applicable directives might
3160 be declared in a parent namespace, so this needs to be called even if
3161 there are no local directives. */
3162 if (flag_openmp)
3163 gfc_trans_omp_declare_variant (ns);
3166 /* Return the decl used to hold the function return value. If
3167 parent_flag is set, the context is the parent_scope. */
3169 tree
3170 gfc_get_fake_result_decl (gfc_symbol * sym, int parent_flag)
3172 tree decl;
3173 tree length;
3174 tree this_fake_result_decl;
3175 tree this_function_decl;
3177 char name[GFC_MAX_SYMBOL_LEN + 10];
3179 if (parent_flag)
3181 this_fake_result_decl = parent_fake_result_decl;
3182 this_function_decl = DECL_CONTEXT (current_function_decl);
3184 else
3186 this_fake_result_decl = current_fake_result_decl;
3187 this_function_decl = current_function_decl;
3190 if (sym
3191 && sym->ns->proc_name->backend_decl == this_function_decl
3192 && sym->ns->proc_name->attr.entry_master
3193 && sym != sym->ns->proc_name)
3195 tree t = NULL, var;
3196 if (this_fake_result_decl != NULL)
3197 for (t = TREE_CHAIN (this_fake_result_decl); t; t = TREE_CHAIN (t))
3198 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t)), sym->name) == 0)
3199 break;
3200 if (t)
3201 return TREE_VALUE (t);
3202 decl = gfc_get_fake_result_decl (sym->ns->proc_name, parent_flag);
3204 if (parent_flag)
3205 this_fake_result_decl = parent_fake_result_decl;
3206 else
3207 this_fake_result_decl = current_fake_result_decl;
3209 if (decl && sym->ns->proc_name->attr.mixed_entry_master)
3211 tree field;
3213 for (field = TYPE_FIELDS (TREE_TYPE (decl));
3214 field; field = DECL_CHAIN (field))
3215 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
3216 sym->name) == 0)
3217 break;
3219 gcc_assert (field != NULL_TREE);
3220 decl = fold_build3_loc (input_location, COMPONENT_REF,
3221 TREE_TYPE (field), decl, field, NULL_TREE);
3224 var = create_tmp_var_raw (TREE_TYPE (decl), sym->name);
3225 if (parent_flag)
3226 gfc_add_decl_to_parent_function (var);
3227 else
3228 gfc_add_decl_to_function (var);
3230 SET_DECL_VALUE_EXPR (var, decl);
3231 DECL_HAS_VALUE_EXPR_P (var) = 1;
3232 GFC_DECL_RESULT (var) = 1;
3234 TREE_CHAIN (this_fake_result_decl)
3235 = tree_cons (get_identifier (sym->name), var,
3236 TREE_CHAIN (this_fake_result_decl));
3237 return var;
3240 if (this_fake_result_decl != NULL_TREE)
3241 return TREE_VALUE (this_fake_result_decl);
3243 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
3244 sym is NULL. */
3245 if (!sym)
3246 return NULL_TREE;
3248 if (sym->ts.type == BT_CHARACTER)
3250 if (sym->ts.u.cl->backend_decl == NULL_TREE)
3251 length = gfc_create_string_length (sym);
3252 else
3253 length = sym->ts.u.cl->backend_decl;
3254 if (VAR_P (length) && DECL_CONTEXT (length) == NULL_TREE)
3255 gfc_add_decl_to_function (length);
3258 if (gfc_return_by_reference (sym))
3260 decl = DECL_ARGUMENTS (this_function_decl);
3262 if (sym->ns->proc_name->backend_decl == this_function_decl
3263 && sym->ns->proc_name->attr.entry_master)
3264 decl = DECL_CHAIN (decl);
3266 TREE_USED (decl) = 1;
3267 if (sym->as)
3268 decl = gfc_build_dummy_array_decl (sym, decl);
3270 else
3272 sprintf (name, "__result_%.20s",
3273 IDENTIFIER_POINTER (DECL_NAME (this_function_decl)));
3275 if (!sym->attr.mixed_entry_master && sym->attr.function)
3276 decl = build_decl (DECL_SOURCE_LOCATION (this_function_decl),
3277 VAR_DECL, get_identifier (name),
3278 gfc_sym_type (sym));
3279 else
3280 decl = build_decl (DECL_SOURCE_LOCATION (this_function_decl),
3281 VAR_DECL, get_identifier (name),
3282 TREE_TYPE (TREE_TYPE (this_function_decl)));
3283 DECL_ARTIFICIAL (decl) = 1;
3284 DECL_EXTERNAL (decl) = 0;
3285 TREE_PUBLIC (decl) = 0;
3286 TREE_USED (decl) = 1;
3287 GFC_DECL_RESULT (decl) = 1;
3288 TREE_ADDRESSABLE (decl) = 1;
3290 layout_decl (decl, 0);
3291 gfc_finish_decl_attrs (decl, &sym->attr);
3293 if (parent_flag)
3294 gfc_add_decl_to_parent_function (decl);
3295 else
3296 gfc_add_decl_to_function (decl);
3299 if (parent_flag)
3300 parent_fake_result_decl = build_tree_list (NULL, decl);
3301 else
3302 current_fake_result_decl = build_tree_list (NULL, decl);
3304 if (sym->attr.assign)
3305 DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (sym->backend_decl);
3307 return decl;
3311 /* Builds a function decl. The remaining parameters are the types of the
3312 function arguments. Negative nargs indicates a varargs function. */
3314 static tree
3315 build_library_function_decl_1 (tree name, const char *spec,
3316 tree rettype, int nargs, va_list p)
3318 vec<tree, va_gc> *arglist;
3319 tree fntype;
3320 tree fndecl;
3321 int n;
3323 /* Library functions must be declared with global scope. */
3324 gcc_assert (current_function_decl == NULL_TREE);
3326 /* Create a list of the argument types. */
3327 vec_alloc (arglist, abs (nargs));
3328 for (n = abs (nargs); n > 0; n--)
3330 tree argtype = va_arg (p, tree);
3331 arglist->quick_push (argtype);
3334 /* Build the function type and decl. */
3335 if (nargs >= 0)
3336 fntype = build_function_type_vec (rettype, arglist);
3337 else
3338 fntype = build_varargs_function_type_vec (rettype, arglist);
3339 if (spec)
3341 tree attr_args = build_tree_list (NULL_TREE,
3342 build_string (strlen (spec), spec));
3343 tree attrs = tree_cons (get_identifier ("fn spec"),
3344 attr_args, TYPE_ATTRIBUTES (fntype));
3345 fntype = build_type_attribute_variant (fntype, attrs);
3347 fndecl = build_decl (input_location,
3348 FUNCTION_DECL, name, fntype);
3350 /* Mark this decl as external. */
3351 DECL_EXTERNAL (fndecl) = 1;
3352 TREE_PUBLIC (fndecl) = 1;
3354 pushdecl (fndecl);
3356 rest_of_decl_compilation (fndecl, 1, 0);
3358 return fndecl;
3361 /* Builds a function decl. The remaining parameters are the types of the
3362 function arguments. Negative nargs indicates a varargs function. */
3364 tree
3365 gfc_build_library_function_decl (tree name, tree rettype, int nargs, ...)
3367 tree ret;
3368 va_list args;
3369 va_start (args, nargs);
3370 ret = build_library_function_decl_1 (name, NULL, rettype, nargs, args);
3371 va_end (args);
3372 return ret;
3375 /* Builds a function decl. The remaining parameters are the types of the
3376 function arguments. Negative nargs indicates a varargs function.
3377 The SPEC parameter specifies the function argument and return type
3378 specification according to the fnspec function type attribute. */
3380 tree
3381 gfc_build_library_function_decl_with_spec (tree name, const char *spec,
3382 tree rettype, int nargs, ...)
3384 tree ret;
3385 va_list args;
3386 va_start (args, nargs);
3387 if (flag_checking)
3389 attr_fnspec fnspec (spec, strlen (spec));
3390 fnspec.verify ();
3392 ret = build_library_function_decl_1 (name, spec, rettype, nargs, args);
3393 va_end (args);
3394 return ret;
3397 static void
3398 gfc_build_intrinsic_function_decls (void)
3400 tree gfc_int4_type_node = gfc_get_int_type (4);
3401 tree gfc_pint4_type_node = build_pointer_type (gfc_int4_type_node);
3402 tree gfc_int8_type_node = gfc_get_int_type (8);
3403 tree gfc_pint8_type_node = build_pointer_type (gfc_int8_type_node);
3404 tree gfc_int16_type_node = gfc_get_int_type (16);
3405 tree gfc_logical4_type_node = gfc_get_logical_type (4);
3406 tree pchar1_type_node = gfc_get_pchar_type (1);
3407 tree pchar4_type_node = gfc_get_pchar_type (4);
3409 /* String functions. */
3410 gfor_fndecl_compare_string = gfc_build_library_function_decl_with_spec (
3411 get_identifier (PREFIX("compare_string")), ". . R . R ",
3412 integer_type_node, 4, gfc_charlen_type_node, pchar1_type_node,
3413 gfc_charlen_type_node, pchar1_type_node);
3414 DECL_PURE_P (gfor_fndecl_compare_string) = 1;
3415 TREE_NOTHROW (gfor_fndecl_compare_string) = 1;
3417 gfor_fndecl_concat_string = gfc_build_library_function_decl_with_spec (
3418 get_identifier (PREFIX("concat_string")), ". . W . R . R ",
3419 void_type_node, 6, gfc_charlen_type_node, pchar1_type_node,
3420 gfc_charlen_type_node, pchar1_type_node,
3421 gfc_charlen_type_node, pchar1_type_node);
3422 TREE_NOTHROW (gfor_fndecl_concat_string) = 1;
3424 gfor_fndecl_string_len_trim = gfc_build_library_function_decl_with_spec (
3425 get_identifier (PREFIX("string_len_trim")), ". . R ",
3426 gfc_charlen_type_node, 2, gfc_charlen_type_node, pchar1_type_node);
3427 DECL_PURE_P (gfor_fndecl_string_len_trim) = 1;
3428 TREE_NOTHROW (gfor_fndecl_string_len_trim) = 1;
3430 gfor_fndecl_string_index = gfc_build_library_function_decl_with_spec (
3431 get_identifier (PREFIX("string_index")), ". . R . R . ",
3432 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar1_type_node,
3433 gfc_charlen_type_node, pchar1_type_node, gfc_logical4_type_node);
3434 DECL_PURE_P (gfor_fndecl_string_index) = 1;
3435 TREE_NOTHROW (gfor_fndecl_string_index) = 1;
3437 gfor_fndecl_string_scan = gfc_build_library_function_decl_with_spec (
3438 get_identifier (PREFIX("string_scan")), ". . R . R . ",
3439 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar1_type_node,
3440 gfc_charlen_type_node, pchar1_type_node, gfc_logical4_type_node);
3441 DECL_PURE_P (gfor_fndecl_string_scan) = 1;
3442 TREE_NOTHROW (gfor_fndecl_string_scan) = 1;
3444 gfor_fndecl_string_verify = gfc_build_library_function_decl_with_spec (
3445 get_identifier (PREFIX("string_verify")), ". . R . R . ",
3446 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar1_type_node,
3447 gfc_charlen_type_node, pchar1_type_node, gfc_logical4_type_node);
3448 DECL_PURE_P (gfor_fndecl_string_verify) = 1;
3449 TREE_NOTHROW (gfor_fndecl_string_verify) = 1;
3451 gfor_fndecl_string_trim = gfc_build_library_function_decl_with_spec (
3452 get_identifier (PREFIX("string_trim")), ". W w . R ",
3453 void_type_node, 4, build_pointer_type (gfc_charlen_type_node),
3454 build_pointer_type (pchar1_type_node), gfc_charlen_type_node,
3455 pchar1_type_node);
3457 gfor_fndecl_string_minmax = gfc_build_library_function_decl_with_spec (
3458 get_identifier (PREFIX("string_minmax")), ". W w . R ",
3459 void_type_node, -4, build_pointer_type (gfc_charlen_type_node),
3460 build_pointer_type (pchar1_type_node), integer_type_node,
3461 integer_type_node);
3463 gfor_fndecl_adjustl = gfc_build_library_function_decl_with_spec (
3464 get_identifier (PREFIX("adjustl")), ". W . R ",
3465 void_type_node, 3, pchar1_type_node, gfc_charlen_type_node,
3466 pchar1_type_node);
3467 TREE_NOTHROW (gfor_fndecl_adjustl) = 1;
3469 gfor_fndecl_adjustr = gfc_build_library_function_decl_with_spec (
3470 get_identifier (PREFIX("adjustr")), ". W . R ",
3471 void_type_node, 3, pchar1_type_node, gfc_charlen_type_node,
3472 pchar1_type_node);
3473 TREE_NOTHROW (gfor_fndecl_adjustr) = 1;
3475 gfor_fndecl_select_string = gfc_build_library_function_decl_with_spec (
3476 get_identifier (PREFIX("select_string")), ". R . R . ",
3477 integer_type_node, 4, pvoid_type_node, integer_type_node,
3478 pchar1_type_node, gfc_charlen_type_node);
3479 DECL_PURE_P (gfor_fndecl_select_string) = 1;
3480 TREE_NOTHROW (gfor_fndecl_select_string) = 1;
3482 gfor_fndecl_compare_string_char4 = gfc_build_library_function_decl_with_spec (
3483 get_identifier (PREFIX("compare_string_char4")), ". . R . R ",
3484 integer_type_node, 4, gfc_charlen_type_node, pchar4_type_node,
3485 gfc_charlen_type_node, pchar4_type_node);
3486 DECL_PURE_P (gfor_fndecl_compare_string_char4) = 1;
3487 TREE_NOTHROW (gfor_fndecl_compare_string_char4) = 1;
3489 gfor_fndecl_concat_string_char4 = gfc_build_library_function_decl_with_spec (
3490 get_identifier (PREFIX("concat_string_char4")), ". . W . R . R ",
3491 void_type_node, 6, gfc_charlen_type_node, pchar4_type_node,
3492 gfc_charlen_type_node, pchar4_type_node, gfc_charlen_type_node,
3493 pchar4_type_node);
3494 TREE_NOTHROW (gfor_fndecl_concat_string_char4) = 1;
3496 gfor_fndecl_string_len_trim_char4 = gfc_build_library_function_decl_with_spec (
3497 get_identifier (PREFIX("string_len_trim_char4")), ". . R ",
3498 gfc_charlen_type_node, 2, gfc_charlen_type_node, pchar4_type_node);
3499 DECL_PURE_P (gfor_fndecl_string_len_trim_char4) = 1;
3500 TREE_NOTHROW (gfor_fndecl_string_len_trim_char4) = 1;
3502 gfor_fndecl_string_index_char4 = gfc_build_library_function_decl_with_spec (
3503 get_identifier (PREFIX("string_index_char4")), ". . R . R . ",
3504 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar4_type_node,
3505 gfc_charlen_type_node, pchar4_type_node, gfc_logical4_type_node);
3506 DECL_PURE_P (gfor_fndecl_string_index_char4) = 1;
3507 TREE_NOTHROW (gfor_fndecl_string_index_char4) = 1;
3509 gfor_fndecl_string_scan_char4 = gfc_build_library_function_decl_with_spec (
3510 get_identifier (PREFIX("string_scan_char4")), ". . R . R . ",
3511 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar4_type_node,
3512 gfc_charlen_type_node, pchar4_type_node, gfc_logical4_type_node);
3513 DECL_PURE_P (gfor_fndecl_string_scan_char4) = 1;
3514 TREE_NOTHROW (gfor_fndecl_string_scan_char4) = 1;
3516 gfor_fndecl_string_verify_char4 = gfc_build_library_function_decl_with_spec (
3517 get_identifier (PREFIX("string_verify_char4")), ". . R . R . ",
3518 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar4_type_node,
3519 gfc_charlen_type_node, pchar4_type_node, gfc_logical4_type_node);
3520 DECL_PURE_P (gfor_fndecl_string_verify_char4) = 1;
3521 TREE_NOTHROW (gfor_fndecl_string_verify_char4) = 1;
3523 gfor_fndecl_string_trim_char4 = gfc_build_library_function_decl_with_spec (
3524 get_identifier (PREFIX("string_trim_char4")), ". W w . R ",
3525 void_type_node, 4, build_pointer_type (gfc_charlen_type_node),
3526 build_pointer_type (pchar4_type_node), gfc_charlen_type_node,
3527 pchar4_type_node);
3529 gfor_fndecl_string_minmax_char4 = gfc_build_library_function_decl_with_spec (
3530 get_identifier (PREFIX("string_minmax_char4")), ". W w . R ",
3531 void_type_node, -4, build_pointer_type (gfc_charlen_type_node),
3532 build_pointer_type (pchar4_type_node), integer_type_node,
3533 integer_type_node);
3535 gfor_fndecl_adjustl_char4 = gfc_build_library_function_decl_with_spec (
3536 get_identifier (PREFIX("adjustl_char4")), ". W . R ",
3537 void_type_node, 3, pchar4_type_node, gfc_charlen_type_node,
3538 pchar4_type_node);
3539 TREE_NOTHROW (gfor_fndecl_adjustl_char4) = 1;
3541 gfor_fndecl_adjustr_char4 = gfc_build_library_function_decl_with_spec (
3542 get_identifier (PREFIX("adjustr_char4")), ". W . R ",
3543 void_type_node, 3, pchar4_type_node, gfc_charlen_type_node,
3544 pchar4_type_node);
3545 TREE_NOTHROW (gfor_fndecl_adjustr_char4) = 1;
3547 gfor_fndecl_select_string_char4 = gfc_build_library_function_decl_with_spec (
3548 get_identifier (PREFIX("select_string_char4")), ". R . R . ",
3549 integer_type_node, 4, pvoid_type_node, integer_type_node,
3550 pvoid_type_node, gfc_charlen_type_node);
3551 DECL_PURE_P (gfor_fndecl_select_string_char4) = 1;
3552 TREE_NOTHROW (gfor_fndecl_select_string_char4) = 1;
3555 /* Conversion between character kinds. */
3557 gfor_fndecl_convert_char1_to_char4 = gfc_build_library_function_decl_with_spec (
3558 get_identifier (PREFIX("convert_char1_to_char4")), ". w . R ",
3559 void_type_node, 3, build_pointer_type (pchar4_type_node),
3560 gfc_charlen_type_node, pchar1_type_node);
3562 gfor_fndecl_convert_char4_to_char1 = gfc_build_library_function_decl_with_spec (
3563 get_identifier (PREFIX("convert_char4_to_char1")), ". w . R ",
3564 void_type_node, 3, build_pointer_type (pchar1_type_node),
3565 gfc_charlen_type_node, pchar4_type_node);
3567 /* Misc. functions. */
3569 gfor_fndecl_ttynam = gfc_build_library_function_decl_with_spec (
3570 get_identifier (PREFIX("ttynam")), ". W . . ",
3571 void_type_node, 3, pchar_type_node, gfc_charlen_type_node,
3572 integer_type_node);
3574 gfor_fndecl_fdate = gfc_build_library_function_decl_with_spec (
3575 get_identifier (PREFIX("fdate")), ". W . ",
3576 void_type_node, 2, pchar_type_node, gfc_charlen_type_node);
3578 gfor_fndecl_ctime = gfc_build_library_function_decl_with_spec (
3579 get_identifier (PREFIX("ctime")), ". W . . ",
3580 void_type_node, 3, pchar_type_node, gfc_charlen_type_node,
3581 gfc_int8_type_node);
3583 gfor_fndecl_random_init = gfc_build_library_function_decl (
3584 get_identifier (PREFIX("random_init")),
3585 void_type_node, 3, gfc_logical4_type_node, gfc_logical4_type_node,
3586 gfc_int4_type_node);
3588 // gfor_fndecl_caf_rand_init is defined in the lib-coarray section below.
3590 gfor_fndecl_sc_kind = gfc_build_library_function_decl_with_spec (
3591 get_identifier (PREFIX("selected_char_kind")), ". . R ",
3592 gfc_int4_type_node, 2, gfc_charlen_type_node, pchar_type_node);
3593 DECL_PURE_P (gfor_fndecl_sc_kind) = 1;
3594 TREE_NOTHROW (gfor_fndecl_sc_kind) = 1;
3596 gfor_fndecl_si_kind = gfc_build_library_function_decl_with_spec (
3597 get_identifier (PREFIX("selected_int_kind")), ". R ",
3598 gfc_int4_type_node, 1, pvoid_type_node);
3599 DECL_PURE_P (gfor_fndecl_si_kind) = 1;
3600 TREE_NOTHROW (gfor_fndecl_si_kind) = 1;
3602 gfor_fndecl_sr_kind = gfc_build_library_function_decl_with_spec (
3603 get_identifier (PREFIX("selected_real_kind2008")), ". R R ",
3604 gfc_int4_type_node, 3, pvoid_type_node, pvoid_type_node,
3605 pvoid_type_node);
3606 DECL_PURE_P (gfor_fndecl_sr_kind) = 1;
3607 TREE_NOTHROW (gfor_fndecl_sr_kind) = 1;
3609 gfor_fndecl_system_clock4 = gfc_build_library_function_decl (
3610 get_identifier (PREFIX("system_clock_4")),
3611 void_type_node, 3, gfc_pint4_type_node, gfc_pint4_type_node,
3612 gfc_pint4_type_node);
3614 gfor_fndecl_system_clock8 = gfc_build_library_function_decl (
3615 get_identifier (PREFIX("system_clock_8")),
3616 void_type_node, 3, gfc_pint8_type_node, gfc_pint8_type_node,
3617 gfc_pint8_type_node);
3619 /* Power functions. */
3621 tree ctype, rtype, itype, jtype;
3622 int rkind, ikind, jkind;
3623 #define NIKINDS 3
3624 #define NRKINDS 4
3625 static int ikinds[NIKINDS] = {4, 8, 16};
3626 static int rkinds[NRKINDS] = {4, 8, 10, 16};
3627 char name[PREFIX_LEN + 12]; /* _gfortran_pow_?n_?n */
3629 for (ikind=0; ikind < NIKINDS; ikind++)
3631 itype = gfc_get_int_type (ikinds[ikind]);
3633 for (jkind=0; jkind < NIKINDS; jkind++)
3635 jtype = gfc_get_int_type (ikinds[jkind]);
3636 if (itype && jtype)
3638 sprintf (name, PREFIX("pow_i%d_i%d"), ikinds[ikind],
3639 ikinds[jkind]);
3640 gfor_fndecl_math_powi[jkind][ikind].integer =
3641 gfc_build_library_function_decl (get_identifier (name),
3642 jtype, 2, jtype, itype);
3643 TREE_READONLY (gfor_fndecl_math_powi[jkind][ikind].integer) = 1;
3644 TREE_NOTHROW (gfor_fndecl_math_powi[jkind][ikind].integer) = 1;
3648 for (rkind = 0; rkind < NRKINDS; rkind ++)
3650 rtype = gfc_get_real_type (rkinds[rkind]);
3651 if (rtype && itype)
3653 sprintf (name, PREFIX("pow_r%d_i%d"),
3654 gfc_type_abi_kind (BT_REAL, rkinds[rkind]),
3655 ikinds[ikind]);
3656 gfor_fndecl_math_powi[rkind][ikind].real =
3657 gfc_build_library_function_decl (get_identifier (name),
3658 rtype, 2, rtype, itype);
3659 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].real) = 1;
3660 TREE_NOTHROW (gfor_fndecl_math_powi[rkind][ikind].real) = 1;
3663 ctype = gfc_get_complex_type (rkinds[rkind]);
3664 if (ctype && itype)
3666 sprintf (name, PREFIX("pow_c%d_i%d"),
3667 gfc_type_abi_kind (BT_REAL, rkinds[rkind]),
3668 ikinds[ikind]);
3669 gfor_fndecl_math_powi[rkind][ikind].cmplx =
3670 gfc_build_library_function_decl (get_identifier (name),
3671 ctype, 2,ctype, itype);
3672 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1;
3673 TREE_NOTHROW (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1;
3677 #undef NIKINDS
3678 #undef NRKINDS
3681 gfor_fndecl_math_ishftc4 = gfc_build_library_function_decl (
3682 get_identifier (PREFIX("ishftc4")),
3683 gfc_int4_type_node, 3, gfc_int4_type_node, gfc_int4_type_node,
3684 gfc_int4_type_node);
3685 TREE_READONLY (gfor_fndecl_math_ishftc4) = 1;
3686 TREE_NOTHROW (gfor_fndecl_math_ishftc4) = 1;
3688 gfor_fndecl_math_ishftc8 = gfc_build_library_function_decl (
3689 get_identifier (PREFIX("ishftc8")),
3690 gfc_int8_type_node, 3, gfc_int8_type_node, gfc_int4_type_node,
3691 gfc_int4_type_node);
3692 TREE_READONLY (gfor_fndecl_math_ishftc8) = 1;
3693 TREE_NOTHROW (gfor_fndecl_math_ishftc8) = 1;
3695 if (gfc_int16_type_node)
3697 gfor_fndecl_math_ishftc16 = gfc_build_library_function_decl (
3698 get_identifier (PREFIX("ishftc16")),
3699 gfc_int16_type_node, 3, gfc_int16_type_node, gfc_int4_type_node,
3700 gfc_int4_type_node);
3701 TREE_READONLY (gfor_fndecl_math_ishftc16) = 1;
3702 TREE_NOTHROW (gfor_fndecl_math_ishftc16) = 1;
3705 /* BLAS functions. */
3707 tree pint = build_pointer_type (integer_type_node);
3708 tree ps = build_pointer_type (gfc_get_real_type (gfc_default_real_kind));
3709 tree pd = build_pointer_type (gfc_get_real_type (gfc_default_double_kind));
3710 tree pc = build_pointer_type (gfc_get_complex_type (gfc_default_real_kind));
3711 tree pz = build_pointer_type
3712 (gfc_get_complex_type (gfc_default_double_kind));
3714 gfor_fndecl_sgemm = gfc_build_library_function_decl
3715 (get_identifier
3716 (flag_underscoring ? "sgemm_" : "sgemm"),
3717 void_type_node, 15, pchar_type_node,
3718 pchar_type_node, pint, pint, pint, ps, ps, pint,
3719 ps, pint, ps, ps, pint, integer_type_node,
3720 integer_type_node);
3721 gfor_fndecl_dgemm = gfc_build_library_function_decl
3722 (get_identifier
3723 (flag_underscoring ? "dgemm_" : "dgemm"),
3724 void_type_node, 15, pchar_type_node,
3725 pchar_type_node, pint, pint, pint, pd, pd, pint,
3726 pd, pint, pd, pd, pint, integer_type_node,
3727 integer_type_node);
3728 gfor_fndecl_cgemm = gfc_build_library_function_decl
3729 (get_identifier
3730 (flag_underscoring ? "cgemm_" : "cgemm"),
3731 void_type_node, 15, pchar_type_node,
3732 pchar_type_node, pint, pint, pint, pc, pc, pint,
3733 pc, pint, pc, pc, pint, integer_type_node,
3734 integer_type_node);
3735 gfor_fndecl_zgemm = gfc_build_library_function_decl
3736 (get_identifier
3737 (flag_underscoring ? "zgemm_" : "zgemm"),
3738 void_type_node, 15, pchar_type_node,
3739 pchar_type_node, pint, pint, pint, pz, pz, pint,
3740 pz, pint, pz, pz, pint, integer_type_node,
3741 integer_type_node);
3744 /* Other functions. */
3745 gfor_fndecl_iargc = gfc_build_library_function_decl (
3746 get_identifier (PREFIX ("iargc")), gfc_int4_type_node, 0);
3747 TREE_NOTHROW (gfor_fndecl_iargc) = 1;
3749 gfor_fndecl_kill_sub = gfc_build_library_function_decl (
3750 get_identifier (PREFIX ("kill_sub")), void_type_node,
3751 3, gfc_int4_type_node, gfc_int4_type_node, gfc_pint4_type_node);
3753 gfor_fndecl_kill = gfc_build_library_function_decl (
3754 get_identifier (PREFIX ("kill")), gfc_int4_type_node,
3755 2, gfc_int4_type_node, gfc_int4_type_node);
3757 gfor_fndecl_is_contiguous0 = gfc_build_library_function_decl_with_spec (
3758 get_identifier (PREFIX("is_contiguous0")), ". R ",
3759 gfc_int4_type_node, 1, pvoid_type_node);
3760 DECL_PURE_P (gfor_fndecl_is_contiguous0) = 1;
3761 TREE_NOTHROW (gfor_fndecl_is_contiguous0) = 1;
3765 /* Make prototypes for runtime library functions. */
3767 void
3768 gfc_build_builtin_function_decls (void)
3770 tree gfc_int8_type_node = gfc_get_int_type (8);
3772 gfor_fndecl_stop_numeric = gfc_build_library_function_decl (
3773 get_identifier (PREFIX("stop_numeric")),
3774 void_type_node, 2, integer_type_node, boolean_type_node);
3775 /* STOP doesn't return. */
3776 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric) = 1;
3778 gfor_fndecl_stop_string = gfc_build_library_function_decl_with_spec (
3779 get_identifier (PREFIX("stop_string")), ". R . . ",
3780 void_type_node, 3, pchar_type_node, size_type_node,
3781 boolean_type_node);
3782 /* STOP doesn't return. */
3783 TREE_THIS_VOLATILE (gfor_fndecl_stop_string) = 1;
3785 gfor_fndecl_error_stop_numeric = gfc_build_library_function_decl (
3786 get_identifier (PREFIX("error_stop_numeric")),
3787 void_type_node, 2, integer_type_node, boolean_type_node);
3788 /* ERROR STOP doesn't return. */
3789 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_numeric) = 1;
3791 gfor_fndecl_error_stop_string = gfc_build_library_function_decl_with_spec (
3792 get_identifier (PREFIX("error_stop_string")), ". R . . ",
3793 void_type_node, 3, pchar_type_node, size_type_node,
3794 boolean_type_node);
3795 /* ERROR STOP doesn't return. */
3796 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_string) = 1;
3798 gfor_fndecl_pause_numeric = gfc_build_library_function_decl (
3799 get_identifier (PREFIX("pause_numeric")),
3800 void_type_node, 1, gfc_int8_type_node);
3802 gfor_fndecl_pause_string = gfc_build_library_function_decl_with_spec (
3803 get_identifier (PREFIX("pause_string")), ". R . ",
3804 void_type_node, 2, pchar_type_node, size_type_node);
3806 gfor_fndecl_runtime_error = gfc_build_library_function_decl_with_spec (
3807 get_identifier (PREFIX("runtime_error")), ". R ",
3808 void_type_node, -1, pchar_type_node);
3809 /* The runtime_error function does not return. */
3810 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error) = 1;
3812 gfor_fndecl_runtime_error_at = gfc_build_library_function_decl_with_spec (
3813 get_identifier (PREFIX("runtime_error_at")), ". R R ",
3814 void_type_node, -2, pchar_type_node, pchar_type_node);
3815 /* The runtime_error_at function does not return. */
3816 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at) = 1;
3818 gfor_fndecl_runtime_warning_at = gfc_build_library_function_decl_with_spec (
3819 get_identifier (PREFIX("runtime_warning_at")), ". R R ",
3820 void_type_node, -2, pchar_type_node, pchar_type_node);
3822 gfor_fndecl_generate_error = gfc_build_library_function_decl_with_spec (
3823 get_identifier (PREFIX("generate_error")), ". R . R ",
3824 void_type_node, 3, pvoid_type_node, integer_type_node,
3825 pchar_type_node);
3827 gfor_fndecl_os_error_at = gfc_build_library_function_decl_with_spec (
3828 get_identifier (PREFIX("os_error_at")), ". R R ",
3829 void_type_node, -2, pchar_type_node, pchar_type_node);
3830 /* The os_error_at function does not return. */
3831 TREE_THIS_VOLATILE (gfor_fndecl_os_error_at) = 1;
3833 gfor_fndecl_set_args = gfc_build_library_function_decl (
3834 get_identifier (PREFIX("set_args")),
3835 void_type_node, 2, integer_type_node,
3836 build_pointer_type (pchar_type_node));
3838 gfor_fndecl_set_fpe = gfc_build_library_function_decl (
3839 get_identifier (PREFIX("set_fpe")),
3840 void_type_node, 1, integer_type_node);
3842 gfor_fndecl_ieee_procedure_entry = gfc_build_library_function_decl (
3843 get_identifier (PREFIX("ieee_procedure_entry")),
3844 void_type_node, 1, pvoid_type_node);
3846 gfor_fndecl_ieee_procedure_exit = gfc_build_library_function_decl (
3847 get_identifier (PREFIX("ieee_procedure_exit")),
3848 void_type_node, 1, pvoid_type_node);
3850 /* Keep the array dimension in sync with the call, later in this file. */
3851 gfor_fndecl_set_options = gfc_build_library_function_decl_with_spec (
3852 get_identifier (PREFIX("set_options")), ". . R ",
3853 void_type_node, 2, integer_type_node,
3854 build_pointer_type (integer_type_node));
3856 gfor_fndecl_set_convert = gfc_build_library_function_decl (
3857 get_identifier (PREFIX("set_convert")),
3858 void_type_node, 1, integer_type_node);
3860 gfor_fndecl_set_record_marker = gfc_build_library_function_decl (
3861 get_identifier (PREFIX("set_record_marker")),
3862 void_type_node, 1, integer_type_node);
3864 gfor_fndecl_set_max_subrecord_length = gfc_build_library_function_decl (
3865 get_identifier (PREFIX("set_max_subrecord_length")),
3866 void_type_node, 1, integer_type_node);
3868 gfor_fndecl_in_pack = gfc_build_library_function_decl_with_spec (
3869 get_identifier (PREFIX("internal_pack")), ". r ",
3870 pvoid_type_node, 1, pvoid_type_node);
3872 gfor_fndecl_in_unpack = gfc_build_library_function_decl_with_spec (
3873 get_identifier (PREFIX("internal_unpack")), ". w R ",
3874 void_type_node, 2, pvoid_type_node, pvoid_type_node);
3876 gfor_fndecl_associated = gfc_build_library_function_decl_with_spec (
3877 get_identifier (PREFIX("associated")), ". R R ",
3878 integer_type_node, 2, ppvoid_type_node, ppvoid_type_node);
3879 DECL_PURE_P (gfor_fndecl_associated) = 1;
3880 TREE_NOTHROW (gfor_fndecl_associated) = 1;
3882 /* Coarray library calls. */
3883 if (flag_coarray == GFC_FCOARRAY_LIB)
3885 tree pint_type, pppchar_type;
3887 pint_type = build_pointer_type (integer_type_node);
3888 pppchar_type
3889 = build_pointer_type (build_pointer_type (pchar_type_node));
3891 gfor_fndecl_caf_init = gfc_build_library_function_decl_with_spec (
3892 get_identifier (PREFIX("caf_init")), ". W W ",
3893 void_type_node, 2, pint_type, pppchar_type);
3895 gfor_fndecl_caf_finalize = gfc_build_library_function_decl (
3896 get_identifier (PREFIX("caf_finalize")), void_type_node, 0);
3898 gfor_fndecl_caf_this_image = gfc_build_library_function_decl (
3899 get_identifier (PREFIX("caf_this_image")), integer_type_node,
3900 1, integer_type_node);
3902 gfor_fndecl_caf_num_images = gfc_build_library_function_decl (
3903 get_identifier (PREFIX("caf_num_images")), integer_type_node,
3904 2, integer_type_node, integer_type_node);
3906 gfor_fndecl_caf_register = gfc_build_library_function_decl_with_spec (
3907 get_identifier (PREFIX("caf_register")), ". . . W w w w . ",
3908 void_type_node, 7,
3909 size_type_node, integer_type_node, ppvoid_type_node, pvoid_type_node,
3910 pint_type, pchar_type_node, size_type_node);
3912 gfor_fndecl_caf_deregister = gfc_build_library_function_decl_with_spec (
3913 get_identifier (PREFIX("caf_deregister")), ". W . w w . ",
3914 void_type_node, 5,
3915 ppvoid_type_node, integer_type_node, pint_type, pchar_type_node,
3916 size_type_node);
3918 gfor_fndecl_caf_get = gfc_build_library_function_decl_with_spec (
3919 get_identifier (PREFIX("caf_get")), ". r . . r r w . . . w ",
3920 void_type_node, 10,
3921 pvoid_type_node, size_type_node, integer_type_node, pvoid_type_node,
3922 pvoid_type_node, pvoid_type_node, integer_type_node, integer_type_node,
3923 boolean_type_node, pint_type);
3925 gfor_fndecl_caf_send = gfc_build_library_function_decl_with_spec (
3926 get_identifier (PREFIX("caf_send")), ". r . . w r r . . . w ",
3927 void_type_node, 11,
3928 pvoid_type_node, size_type_node, integer_type_node, pvoid_type_node,
3929 pvoid_type_node, pvoid_type_node, integer_type_node, integer_type_node,
3930 boolean_type_node, pint_type, pvoid_type_node);
3932 gfor_fndecl_caf_sendget = gfc_build_library_function_decl_with_spec (
3933 get_identifier (PREFIX("caf_sendget")), ". r . . w r r . . r r . . . w ",
3934 void_type_node, 14, pvoid_type_node, size_type_node, integer_type_node,
3935 pvoid_type_node, pvoid_type_node, pvoid_type_node, size_type_node,
3936 integer_type_node, pvoid_type_node, pvoid_type_node, integer_type_node,
3937 integer_type_node, boolean_type_node, integer_type_node);
3939 gfor_fndecl_caf_get_by_ref = gfc_build_library_function_decl_with_spec (
3940 get_identifier (PREFIX("caf_get_by_ref")), ". r . w r . . . . w . ",
3941 void_type_node,
3942 10, pvoid_type_node, integer_type_node, pvoid_type_node,
3943 pvoid_type_node, integer_type_node, integer_type_node,
3944 boolean_type_node, boolean_type_node, pint_type, integer_type_node);
3946 gfor_fndecl_caf_send_by_ref = gfc_build_library_function_decl_with_spec (
3947 get_identifier (PREFIX("caf_send_by_ref")), ". r . r r . . . . w . ",
3948 void_type_node, 10, pvoid_type_node, integer_type_node, pvoid_type_node,
3949 pvoid_type_node, integer_type_node, integer_type_node,
3950 boolean_type_node, boolean_type_node, pint_type, integer_type_node);
3952 gfor_fndecl_caf_sendget_by_ref
3953 = gfc_build_library_function_decl_with_spec (
3954 get_identifier (PREFIX("caf_sendget_by_ref")),
3955 ". r . r r . r . . . w w . . ",
3956 void_type_node, 13, pvoid_type_node, integer_type_node,
3957 pvoid_type_node, pvoid_type_node, integer_type_node,
3958 pvoid_type_node, integer_type_node, integer_type_node,
3959 boolean_type_node, pint_type, pint_type, integer_type_node,
3960 integer_type_node);
3962 gfor_fndecl_caf_sync_all = gfc_build_library_function_decl_with_spec (
3963 get_identifier (PREFIX("caf_sync_all")), ". w w . ", void_type_node,
3964 3, pint_type, pchar_type_node, size_type_node);
3966 gfor_fndecl_caf_sync_memory = gfc_build_library_function_decl_with_spec (
3967 get_identifier (PREFIX("caf_sync_memory")), ". w w . ", void_type_node,
3968 3, pint_type, pchar_type_node, size_type_node);
3970 gfor_fndecl_caf_sync_images = gfc_build_library_function_decl_with_spec (
3971 get_identifier (PREFIX("caf_sync_images")), ". . r w w . ", void_type_node,
3972 5, integer_type_node, pint_type, pint_type,
3973 pchar_type_node, size_type_node);
3975 gfor_fndecl_caf_error_stop = gfc_build_library_function_decl (
3976 get_identifier (PREFIX("caf_error_stop")),
3977 void_type_node, 1, integer_type_node);
3978 /* CAF's ERROR STOP doesn't return. */
3979 TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop) = 1;
3981 gfor_fndecl_caf_error_stop_str = gfc_build_library_function_decl_with_spec (
3982 get_identifier (PREFIX("caf_error_stop_str")), ". r . ",
3983 void_type_node, 2, pchar_type_node, size_type_node);
3984 /* CAF's ERROR STOP doesn't return. */
3985 TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop_str) = 1;
3987 gfor_fndecl_caf_stop_numeric = gfc_build_library_function_decl (
3988 get_identifier (PREFIX("caf_stop_numeric")),
3989 void_type_node, 1, integer_type_node);
3990 /* CAF's STOP doesn't return. */
3991 TREE_THIS_VOLATILE (gfor_fndecl_caf_stop_numeric) = 1;
3993 gfor_fndecl_caf_stop_str = gfc_build_library_function_decl_with_spec (
3994 get_identifier (PREFIX("caf_stop_str")), ". r . ",
3995 void_type_node, 2, pchar_type_node, size_type_node);
3996 /* CAF's STOP doesn't return. */
3997 TREE_THIS_VOLATILE (gfor_fndecl_caf_stop_str) = 1;
3999 gfor_fndecl_caf_atomic_def = gfc_build_library_function_decl_with_spec (
4000 get_identifier (PREFIX("caf_atomic_define")), ". r . . w w . . ",
4001 void_type_node, 7, pvoid_type_node, size_type_node, integer_type_node,
4002 pvoid_type_node, pint_type, integer_type_node, integer_type_node);
4004 gfor_fndecl_caf_atomic_ref = gfc_build_library_function_decl_with_spec (
4005 get_identifier (PREFIX("caf_atomic_ref")), ". r . . w w . . ",
4006 void_type_node, 7, pvoid_type_node, size_type_node, integer_type_node,
4007 pvoid_type_node, pint_type, integer_type_node, integer_type_node);
4009 gfor_fndecl_caf_atomic_cas = gfc_build_library_function_decl_with_spec (
4010 get_identifier (PREFIX("caf_atomic_cas")), ". r . . w r r w . . ",
4011 void_type_node, 9, pvoid_type_node, size_type_node, integer_type_node,
4012 pvoid_type_node, pvoid_type_node, pvoid_type_node, pint_type,
4013 integer_type_node, integer_type_node);
4015 gfor_fndecl_caf_atomic_op = gfc_build_library_function_decl_with_spec (
4016 get_identifier (PREFIX("caf_atomic_op")), ". . r . . r w w . . ",
4017 void_type_node, 9, integer_type_node, pvoid_type_node, size_type_node,
4018 integer_type_node, pvoid_type_node, pvoid_type_node, pint_type,
4019 integer_type_node, integer_type_node);
4021 gfor_fndecl_caf_lock = gfc_build_library_function_decl_with_spec (
4022 get_identifier (PREFIX("caf_lock")), ". r . . w w w . ",
4023 void_type_node, 7, pvoid_type_node, size_type_node, integer_type_node,
4024 pint_type, pint_type, pchar_type_node, size_type_node);
4026 gfor_fndecl_caf_unlock = gfc_build_library_function_decl_with_spec (
4027 get_identifier (PREFIX("caf_unlock")), ". r . . w w . ",
4028 void_type_node, 6, pvoid_type_node, size_type_node, integer_type_node,
4029 pint_type, pchar_type_node, size_type_node);
4031 gfor_fndecl_caf_event_post = gfc_build_library_function_decl_with_spec (
4032 get_identifier (PREFIX("caf_event_post")), ". r . . w w . ",
4033 void_type_node, 6, pvoid_type_node, size_type_node, integer_type_node,
4034 pint_type, pchar_type_node, size_type_node);
4036 gfor_fndecl_caf_event_wait = gfc_build_library_function_decl_with_spec (
4037 get_identifier (PREFIX("caf_event_wait")), ". r . . w w . ",
4038 void_type_node, 6, pvoid_type_node, size_type_node, integer_type_node,
4039 pint_type, pchar_type_node, size_type_node);
4041 gfor_fndecl_caf_event_query = gfc_build_library_function_decl_with_spec (
4042 get_identifier (PREFIX("caf_event_query")), ". r . . w w ",
4043 void_type_node, 5, pvoid_type_node, size_type_node, integer_type_node,
4044 pint_type, pint_type);
4046 gfor_fndecl_caf_fail_image = gfc_build_library_function_decl (
4047 get_identifier (PREFIX("caf_fail_image")), void_type_node, 0);
4048 /* CAF's FAIL doesn't return. */
4049 TREE_THIS_VOLATILE (gfor_fndecl_caf_fail_image) = 1;
4051 gfor_fndecl_caf_failed_images
4052 = gfc_build_library_function_decl_with_spec (
4053 get_identifier (PREFIX("caf_failed_images")), ". w . r ",
4054 void_type_node, 3, pvoid_type_node, ppvoid_type_node,
4055 integer_type_node);
4057 gfor_fndecl_caf_form_team
4058 = gfc_build_library_function_decl_with_spec (
4059 get_identifier (PREFIX("caf_form_team")), ". . W . ",
4060 void_type_node, 3, integer_type_node, ppvoid_type_node,
4061 integer_type_node);
4063 gfor_fndecl_caf_change_team
4064 = gfc_build_library_function_decl_with_spec (
4065 get_identifier (PREFIX("caf_change_team")), ". w . ",
4066 void_type_node, 2, ppvoid_type_node,
4067 integer_type_node);
4069 gfor_fndecl_caf_end_team
4070 = gfc_build_library_function_decl (
4071 get_identifier (PREFIX("caf_end_team")), void_type_node, 0);
4073 gfor_fndecl_caf_get_team
4074 = gfc_build_library_function_decl (
4075 get_identifier (PREFIX("caf_get_team")),
4076 void_type_node, 1, integer_type_node);
4078 gfor_fndecl_caf_sync_team
4079 = gfc_build_library_function_decl_with_spec (
4080 get_identifier (PREFIX("caf_sync_team")), ". r . ",
4081 void_type_node, 2, ppvoid_type_node,
4082 integer_type_node);
4084 gfor_fndecl_caf_team_number
4085 = gfc_build_library_function_decl_with_spec (
4086 get_identifier (PREFIX("caf_team_number")), ". r ",
4087 integer_type_node, 1, integer_type_node);
4089 gfor_fndecl_caf_image_status
4090 = gfc_build_library_function_decl_with_spec (
4091 get_identifier (PREFIX("caf_image_status")), ". . r ",
4092 integer_type_node, 2, integer_type_node, ppvoid_type_node);
4094 gfor_fndecl_caf_stopped_images
4095 = gfc_build_library_function_decl_with_spec (
4096 get_identifier (PREFIX("caf_stopped_images")), ". w r r ",
4097 void_type_node, 3, pvoid_type_node, ppvoid_type_node,
4098 integer_type_node);
4100 gfor_fndecl_co_broadcast = gfc_build_library_function_decl_with_spec (
4101 get_identifier (PREFIX("caf_co_broadcast")), ". w . w w . ",
4102 void_type_node, 5, pvoid_type_node, integer_type_node,
4103 pint_type, pchar_type_node, size_type_node);
4105 gfor_fndecl_co_max = gfc_build_library_function_decl_with_spec (
4106 get_identifier (PREFIX("caf_co_max")), ". w . w w . . ",
4107 void_type_node, 6, pvoid_type_node, integer_type_node,
4108 pint_type, pchar_type_node, integer_type_node, size_type_node);
4110 gfor_fndecl_co_min = gfc_build_library_function_decl_with_spec (
4111 get_identifier (PREFIX("caf_co_min")), ". w . w w . . ",
4112 void_type_node, 6, pvoid_type_node, integer_type_node,
4113 pint_type, pchar_type_node, integer_type_node, size_type_node);
4115 gfor_fndecl_co_reduce = gfc_build_library_function_decl_with_spec (
4116 get_identifier (PREFIX("caf_co_reduce")), ". w r . . w w . . ",
4117 void_type_node, 8, pvoid_type_node,
4118 build_pointer_type (build_varargs_function_type_list (void_type_node,
4119 NULL_TREE)),
4120 integer_type_node, integer_type_node, pint_type, pchar_type_node,
4121 integer_type_node, size_type_node);
4123 gfor_fndecl_co_sum = gfc_build_library_function_decl_with_spec (
4124 get_identifier (PREFIX("caf_co_sum")), ". w . w w . ",
4125 void_type_node, 5, pvoid_type_node, integer_type_node,
4126 pint_type, pchar_type_node, size_type_node);
4128 gfor_fndecl_caf_is_present = gfc_build_library_function_decl_with_spec (
4129 get_identifier (PREFIX("caf_is_present")), ". r . r ",
4130 integer_type_node, 3, pvoid_type_node, integer_type_node,
4131 pvoid_type_node);
4133 gfor_fndecl_caf_random_init = gfc_build_library_function_decl (
4134 get_identifier (PREFIX("caf_random_init")),
4135 void_type_node, 2, logical_type_node, logical_type_node);
4138 gfc_build_intrinsic_function_decls ();
4139 gfc_build_intrinsic_lib_fndecls ();
4140 gfc_build_io_library_fndecls ();
4144 /* Evaluate the length of dummy character variables. */
4146 static void
4147 gfc_trans_dummy_character (gfc_symbol *sym, gfc_charlen *cl,
4148 gfc_wrapped_block *block)
4150 stmtblock_t init;
4152 gfc_finish_decl (cl->backend_decl);
4154 gfc_start_block (&init);
4156 /* Evaluate the string length expression. */
4157 gfc_conv_string_length (cl, NULL, &init);
4159 gfc_trans_vla_type_sizes (sym, &init);
4161 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
4165 /* Allocate and cleanup an automatic character variable. */
4167 static void
4168 gfc_trans_auto_character_variable (gfc_symbol * sym, gfc_wrapped_block * block)
4170 stmtblock_t init;
4171 tree decl;
4172 tree tmp;
4174 gcc_assert (sym->backend_decl);
4175 gcc_assert (sym->ts.u.cl && sym->ts.u.cl->length);
4177 gfc_init_block (&init);
4179 /* Evaluate the string length expression. */
4180 gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
4182 gfc_trans_vla_type_sizes (sym, &init);
4184 decl = sym->backend_decl;
4186 /* Emit a DECL_EXPR for this variable, which will cause the
4187 gimplifier to allocate storage, and all that good stuff. */
4188 tmp = fold_build1_loc (input_location, DECL_EXPR, TREE_TYPE (decl), decl);
4189 gfc_add_expr_to_block (&init, tmp);
4191 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
4194 /* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
4196 static void
4197 gfc_trans_assign_aux_var (gfc_symbol * sym, gfc_wrapped_block * block)
4199 stmtblock_t init;
4201 gcc_assert (sym->backend_decl);
4202 gfc_start_block (&init);
4204 /* Set the initial value to length. See the comments in
4205 function gfc_add_assign_aux_vars in this file. */
4206 gfc_add_modify (&init, GFC_DECL_STRING_LEN (sym->backend_decl),
4207 build_int_cst (gfc_charlen_type_node, -2));
4209 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
4212 static void
4213 gfc_trans_vla_one_sizepos (tree *tp, stmtblock_t *body)
4215 tree t = *tp, var, val;
4217 if (t == NULL || t == error_mark_node)
4218 return;
4219 if (TREE_CONSTANT (t) || DECL_P (t))
4220 return;
4222 if (TREE_CODE (t) == SAVE_EXPR)
4224 if (SAVE_EXPR_RESOLVED_P (t))
4226 *tp = TREE_OPERAND (t, 0);
4227 return;
4229 val = TREE_OPERAND (t, 0);
4231 else
4232 val = t;
4234 var = gfc_create_var_np (TREE_TYPE (t), NULL);
4235 gfc_add_decl_to_function (var);
4236 gfc_add_modify (body, var, unshare_expr (val));
4237 if (TREE_CODE (t) == SAVE_EXPR)
4238 TREE_OPERAND (t, 0) = var;
4239 *tp = var;
4242 static void
4243 gfc_trans_vla_type_sizes_1 (tree type, stmtblock_t *body)
4245 tree t;
4247 if (type == NULL || type == error_mark_node)
4248 return;
4250 type = TYPE_MAIN_VARIANT (type);
4252 if (TREE_CODE (type) == INTEGER_TYPE)
4254 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type), body);
4255 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type), body);
4257 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
4259 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
4260 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
4263 else if (TREE_CODE (type) == ARRAY_TYPE)
4265 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type), body);
4266 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type), body);
4267 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type), body);
4268 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type), body);
4270 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
4272 TYPE_SIZE (t) = TYPE_SIZE (type);
4273 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
4278 /* Make sure all type sizes and array domains are either constant,
4279 or variable or parameter decls. This is a simplified variant
4280 of gimplify_type_sizes, but we can't use it here, as none of the
4281 variables in the expressions have been gimplified yet.
4282 As type sizes and domains for various variable length arrays
4283 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
4284 time, without this routine gimplify_type_sizes in the middle-end
4285 could result in the type sizes being gimplified earlier than where
4286 those variables are initialized. */
4288 void
4289 gfc_trans_vla_type_sizes (gfc_symbol *sym, stmtblock_t *body)
4291 tree type = TREE_TYPE (sym->backend_decl);
4293 if (TREE_CODE (type) == FUNCTION_TYPE
4294 && (sym->attr.function || sym->attr.result || sym->attr.entry))
4296 if (! current_fake_result_decl)
4297 return;
4299 type = TREE_TYPE (TREE_VALUE (current_fake_result_decl));
4302 while (POINTER_TYPE_P (type))
4303 type = TREE_TYPE (type);
4305 if (GFC_DESCRIPTOR_TYPE_P (type))
4307 tree etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
4309 while (POINTER_TYPE_P (etype))
4310 etype = TREE_TYPE (etype);
4312 gfc_trans_vla_type_sizes_1 (etype, body);
4315 gfc_trans_vla_type_sizes_1 (type, body);
4319 /* Initialize a derived type by building an lvalue from the symbol
4320 and using trans_assignment to do the work. Set dealloc to false
4321 if no deallocation prior the assignment is needed. */
4322 void
4323 gfc_init_default_dt (gfc_symbol * sym, stmtblock_t * block, bool dealloc)
4325 gfc_expr *e;
4326 tree tmp;
4327 tree present;
4329 gcc_assert (block);
4331 /* Initialization of PDTs is done elsewhere. */
4332 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.pdt_type)
4333 return;
4335 gcc_assert (!sym->attr.allocatable);
4336 gfc_set_sym_referenced (sym);
4337 e = gfc_lval_expr_from_sym (sym);
4338 tmp = gfc_trans_assignment (e, sym->value, false, dealloc);
4339 if (sym->attr.dummy && (sym->attr.optional
4340 || sym->ns->proc_name->attr.entry_master))
4342 present = gfc_conv_expr_present (sym);
4343 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
4344 tmp, build_empty_stmt (input_location));
4346 gfc_add_expr_to_block (block, tmp);
4347 gfc_free_expr (e);
4351 /* Initialize INTENT(OUT) derived type dummies. As well as giving
4352 them their default initializer, if they do not have allocatable
4353 components, they have their allocatable components deallocated. */
4355 static void
4356 init_intent_out_dt (gfc_symbol * proc_sym, gfc_wrapped_block * block)
4358 stmtblock_t init;
4359 gfc_formal_arglist *f;
4360 tree tmp;
4361 tree present;
4362 gfc_symbol *s;
4363 bool dealloc_with_value = false;
4365 gfc_init_block (&init);
4366 for (f = gfc_sym_get_dummy_args (proc_sym); f; f = f->next)
4367 if (f->sym && f->sym->attr.intent == INTENT_OUT
4368 && !f->sym->attr.pointer
4369 && f->sym->ts.type == BT_DERIVED)
4371 s = f->sym;
4372 tmp = NULL_TREE;
4374 /* Note: Allocatables are excluded as they are already handled
4375 by the caller. */
4376 if (!f->sym->attr.allocatable
4377 && gfc_is_finalizable (s->ts.u.derived, NULL))
4379 stmtblock_t block;
4380 gfc_expr *e;
4382 gfc_init_block (&block);
4383 s->attr.referenced = 1;
4384 e = gfc_lval_expr_from_sym (s);
4385 gfc_add_finalizer_call (&block, e);
4386 gfc_free_expr (e);
4387 tmp = gfc_finish_block (&block);
4390 /* Note: Allocatables are excluded as they are already handled
4391 by the caller. */
4392 if (tmp == NULL_TREE && !s->attr.allocatable
4393 && s->ts.u.derived->attr.alloc_comp)
4395 tmp = gfc_deallocate_alloc_comp (s->ts.u.derived,
4396 s->backend_decl,
4397 s->as ? s->as->rank : 0);
4398 dealloc_with_value = s->value;
4401 if (tmp != NULL_TREE && (s->attr.optional
4402 || s->ns->proc_name->attr.entry_master))
4404 present = gfc_conv_expr_present (s);
4405 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
4406 present, tmp, build_empty_stmt (input_location));
4409 if (tmp != NULL_TREE && !dealloc_with_value)
4410 gfc_add_expr_to_block (&init, tmp);
4411 else if (s->value && !s->attr.allocatable)
4413 gfc_add_expr_to_block (&init, tmp);
4414 gfc_init_default_dt (s, &init, false);
4415 dealloc_with_value = false;
4418 else if (f->sym && f->sym->attr.intent == INTENT_OUT
4419 && f->sym->ts.type == BT_CLASS
4420 && !CLASS_DATA (f->sym)->attr.class_pointer
4421 && !CLASS_DATA (f->sym)->attr.allocatable)
4423 stmtblock_t block;
4424 gfc_expr *e;
4426 gfc_init_block (&block);
4427 f->sym->attr.referenced = 1;
4428 e = gfc_lval_expr_from_sym (f->sym);
4429 gfc_add_finalizer_call (&block, e);
4430 gfc_free_expr (e);
4431 tmp = gfc_finish_block (&block);
4433 if (f->sym->attr.optional || f->sym->ns->proc_name->attr.entry_master)
4435 present = gfc_conv_expr_present (f->sym);
4436 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
4437 present, tmp,
4438 build_empty_stmt (input_location));
4440 gfc_add_expr_to_block (&init, tmp);
4442 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
4446 /* Helper function to manage deferred string lengths. */
4448 static tree
4449 gfc_null_and_pass_deferred_len (gfc_symbol *sym, stmtblock_t *init,
4450 locus *loc)
4452 tree tmp;
4454 /* Character length passed by reference. */
4455 tmp = sym->ts.u.cl->passed_length;
4456 tmp = build_fold_indirect_ref_loc (input_location, tmp);
4457 tmp = fold_convert (gfc_charlen_type_node, tmp);
4459 if (!sym->attr.dummy || sym->attr.intent == INTENT_OUT)
4460 /* Zero the string length when entering the scope. */
4461 gfc_add_modify (init, sym->ts.u.cl->backend_decl,
4462 build_int_cst (gfc_charlen_type_node, 0));
4463 else
4465 tree tmp2;
4467 tmp2 = fold_build2_loc (input_location, MODIFY_EXPR,
4468 gfc_charlen_type_node,
4469 sym->ts.u.cl->backend_decl, tmp);
4470 if (sym->attr.optional)
4472 tree present = gfc_conv_expr_present (sym);
4473 tmp2 = build3_loc (input_location, COND_EXPR,
4474 void_type_node, present, tmp2,
4475 build_empty_stmt (input_location));
4477 gfc_add_expr_to_block (init, tmp2);
4480 gfc_restore_backend_locus (loc);
4482 /* Pass the final character length back. */
4483 if (sym->attr.intent != INTENT_IN)
4485 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
4486 gfc_charlen_type_node, tmp,
4487 sym->ts.u.cl->backend_decl);
4488 if (sym->attr.optional)
4490 tree present = gfc_conv_expr_present (sym);
4491 tmp = build3_loc (input_location, COND_EXPR,
4492 void_type_node, present, tmp,
4493 build_empty_stmt (input_location));
4496 else
4497 tmp = NULL_TREE;
4499 return tmp;
4503 /* Get the result expression for a procedure. */
4505 static tree
4506 get_proc_result (gfc_symbol* sym)
4508 if (sym->attr.subroutine || sym == sym->result)
4510 if (current_fake_result_decl != NULL)
4511 return TREE_VALUE (current_fake_result_decl);
4513 return NULL_TREE;
4516 return sym->result->backend_decl;
4520 /* Generate function entry and exit code, and add it to the function body.
4521 This includes:
4522 Allocation and initialization of array variables.
4523 Allocation of character string variables.
4524 Initialization and possibly repacking of dummy arrays.
4525 Initialization of ASSIGN statement auxiliary variable.
4526 Initialization of ASSOCIATE names.
4527 Automatic deallocation. */
4529 void
4530 gfc_trans_deferred_vars (gfc_symbol * proc_sym, gfc_wrapped_block * block)
4532 locus loc;
4533 gfc_symbol *sym;
4534 gfc_formal_arglist *f;
4535 stmtblock_t tmpblock;
4536 bool seen_trans_deferred_array = false;
4537 bool is_pdt_type = false;
4538 tree tmp = NULL;
4539 gfc_expr *e;
4540 gfc_se se;
4541 stmtblock_t init;
4543 /* Deal with implicit return variables. Explicit return variables will
4544 already have been added. */
4545 if (gfc_return_by_reference (proc_sym) && proc_sym->result == proc_sym)
4547 if (!current_fake_result_decl)
4549 gfc_entry_list *el = NULL;
4550 if (proc_sym->attr.entry_master)
4552 for (el = proc_sym->ns->entries; el; el = el->next)
4553 if (el->sym != el->sym->result)
4554 break;
4556 /* TODO: move to the appropriate place in resolve.cc. */
4557 if (warn_return_type > 0 && el == NULL)
4558 gfc_warning (OPT_Wreturn_type,
4559 "Return value of function %qs at %L not set",
4560 proc_sym->name, &proc_sym->declared_at);
4562 else if (proc_sym->as)
4564 tree result = TREE_VALUE (current_fake_result_decl);
4565 gfc_save_backend_locus (&loc);
4566 gfc_set_backend_locus (&proc_sym->declared_at);
4567 gfc_trans_dummy_array_bias (proc_sym, result, block);
4569 /* An automatic character length, pointer array result. */
4570 if (proc_sym->ts.type == BT_CHARACTER
4571 && VAR_P (proc_sym->ts.u.cl->backend_decl))
4573 tmp = NULL;
4574 if (proc_sym->ts.deferred)
4576 gfc_start_block (&init);
4577 tmp = gfc_null_and_pass_deferred_len (proc_sym, &init, &loc);
4578 gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
4580 else
4581 gfc_trans_dummy_character (proc_sym, proc_sym->ts.u.cl, block);
4584 else if (proc_sym->ts.type == BT_CHARACTER)
4586 if (proc_sym->ts.deferred)
4588 tmp = NULL;
4589 gfc_save_backend_locus (&loc);
4590 gfc_set_backend_locus (&proc_sym->declared_at);
4591 gfc_start_block (&init);
4592 /* Zero the string length on entry. */
4593 gfc_add_modify (&init, proc_sym->ts.u.cl->backend_decl,
4594 build_int_cst (gfc_charlen_type_node, 0));
4595 /* Null the pointer. */
4596 e = gfc_lval_expr_from_sym (proc_sym);
4597 gfc_init_se (&se, NULL);
4598 se.want_pointer = 1;
4599 gfc_conv_expr (&se, e);
4600 gfc_free_expr (e);
4601 tmp = se.expr;
4602 gfc_add_modify (&init, tmp,
4603 fold_convert (TREE_TYPE (se.expr),
4604 null_pointer_node));
4605 gfc_restore_backend_locus (&loc);
4607 /* Pass back the string length on exit. */
4608 tmp = proc_sym->ts.u.cl->backend_decl;
4609 if (TREE_CODE (tmp) != INDIRECT_REF
4610 && proc_sym->ts.u.cl->passed_length)
4612 tmp = proc_sym->ts.u.cl->passed_length;
4613 tmp = build_fold_indirect_ref_loc (input_location, tmp);
4614 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
4615 TREE_TYPE (tmp), tmp,
4616 fold_convert
4617 (TREE_TYPE (tmp),
4618 proc_sym->ts.u.cl->backend_decl));
4620 else
4621 tmp = NULL_TREE;
4623 gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
4625 else if (VAR_P (proc_sym->ts.u.cl->backend_decl))
4626 gfc_trans_dummy_character (proc_sym, proc_sym->ts.u.cl, block);
4628 else
4629 gcc_assert (flag_f2c && proc_sym->ts.type == BT_COMPLEX);
4631 else if (proc_sym == proc_sym->result && IS_CLASS_ARRAY (proc_sym))
4633 /* Nullify explicit return class arrays on entry. */
4634 tree type;
4635 tmp = get_proc_result (proc_sym);
4636 if (tmp && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
4638 gfc_start_block (&init);
4639 tmp = gfc_class_data_get (tmp);
4640 type = TREE_TYPE (gfc_conv_descriptor_data_get (tmp));
4641 gfc_conv_descriptor_data_set (&init, tmp, build_int_cst (type, 0));
4642 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
4647 /* Initialize the INTENT(OUT) derived type dummy arguments. This
4648 should be done here so that the offsets and lbounds of arrays
4649 are available. */
4650 gfc_save_backend_locus (&loc);
4651 gfc_set_backend_locus (&proc_sym->declared_at);
4652 init_intent_out_dt (proc_sym, block);
4653 gfc_restore_backend_locus (&loc);
4655 for (sym = proc_sym->tlink; sym != proc_sym; sym = sym->tlink)
4657 bool alloc_comp_or_fini = (sym->ts.type == BT_DERIVED)
4658 && (sym->ts.u.derived->attr.alloc_comp
4659 || gfc_is_finalizable (sym->ts.u.derived,
4660 NULL));
4661 if (sym->assoc)
4662 continue;
4664 /* Set the vptr of unlimited polymorphic pointer variables so that
4665 they do not cause segfaults in select type, when the selector
4666 is an intrinsic type. */
4667 if (sym->ts.type == BT_CLASS && UNLIMITED_POLY (sym)
4668 && sym->attr.flavor == FL_VARIABLE && !sym->assoc
4669 && !sym->attr.dummy && CLASS_DATA (sym)->attr.class_pointer)
4671 gfc_symbol *vtab;
4672 gfc_init_block (&tmpblock);
4673 vtab = gfc_find_vtab (&sym->ts);
4674 if (!vtab->backend_decl)
4676 if (!vtab->attr.referenced)
4677 gfc_set_sym_referenced (vtab);
4678 gfc_get_symbol_decl (vtab);
4680 tmp = gfc_class_vptr_get (sym->backend_decl);
4681 gfc_add_modify (&tmpblock, tmp,
4682 gfc_build_addr_expr (TREE_TYPE (tmp),
4683 vtab->backend_decl));
4684 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), NULL);
4687 if (sym->ts.type == BT_DERIVED
4688 && sym->ts.u.derived
4689 && sym->ts.u.derived->attr.pdt_type)
4691 is_pdt_type = true;
4692 gfc_init_block (&tmpblock);
4693 if (!(sym->attr.dummy
4694 || sym->attr.pointer
4695 || sym->attr.allocatable))
4697 tmp = gfc_allocate_pdt_comp (sym->ts.u.derived,
4698 sym->backend_decl,
4699 sym->as ? sym->as->rank : 0,
4700 sym->param_list);
4701 gfc_add_expr_to_block (&tmpblock, tmp);
4702 if (!sym->attr.result)
4703 tmp = gfc_deallocate_pdt_comp (sym->ts.u.derived,
4704 sym->backend_decl,
4705 sym->as ? sym->as->rank : 0);
4706 else
4707 tmp = NULL_TREE;
4708 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), tmp);
4710 else if (sym->attr.dummy)
4712 tmp = gfc_check_pdt_dummy (sym->ts.u.derived,
4713 sym->backend_decl,
4714 sym->as ? sym->as->rank : 0,
4715 sym->param_list);
4716 gfc_add_expr_to_block (&tmpblock, tmp);
4717 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), NULL);
4720 else if (sym->ts.type == BT_CLASS
4721 && CLASS_DATA (sym)->ts.u.derived
4722 && CLASS_DATA (sym)->ts.u.derived->attr.pdt_type)
4724 gfc_component *data = CLASS_DATA (sym);
4725 is_pdt_type = true;
4726 gfc_init_block (&tmpblock);
4727 if (!(sym->attr.dummy
4728 || CLASS_DATA (sym)->attr.pointer
4729 || CLASS_DATA (sym)->attr.allocatable))
4731 tmp = gfc_class_data_get (sym->backend_decl);
4732 tmp = gfc_allocate_pdt_comp (data->ts.u.derived, tmp,
4733 data->as ? data->as->rank : 0,
4734 sym->param_list);
4735 gfc_add_expr_to_block (&tmpblock, tmp);
4736 tmp = gfc_class_data_get (sym->backend_decl);
4737 if (!sym->attr.result)
4738 tmp = gfc_deallocate_pdt_comp (data->ts.u.derived, tmp,
4739 data->as ? data->as->rank : 0);
4740 else
4741 tmp = NULL_TREE;
4742 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), tmp);
4744 else if (sym->attr.dummy)
4746 tmp = gfc_class_data_get (sym->backend_decl);
4747 tmp = gfc_check_pdt_dummy (data->ts.u.derived, tmp,
4748 data->as ? data->as->rank : 0,
4749 sym->param_list);
4750 gfc_add_expr_to_block (&tmpblock, tmp);
4751 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), NULL);
4755 if (sym->attr.pointer && sym->attr.dimension
4756 && sym->attr.save == SAVE_NONE
4757 && !sym->attr.use_assoc
4758 && !sym->attr.host_assoc
4759 && !sym->attr.dummy
4760 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (sym->backend_decl)))
4762 gfc_init_block (&tmpblock);
4763 gfc_conv_descriptor_span_set (&tmpblock, sym->backend_decl,
4764 build_int_cst (gfc_array_index_type, 0));
4765 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock),
4766 NULL_TREE);
4769 if (sym->ts.type == BT_CLASS
4770 && (sym->attr.save || flag_max_stack_var_size == 0)
4771 && CLASS_DATA (sym)->attr.allocatable)
4773 tree vptr;
4775 if (UNLIMITED_POLY (sym))
4776 vptr = null_pointer_node;
4777 else
4779 gfc_symbol *vsym;
4780 vsym = gfc_find_derived_vtab (sym->ts.u.derived);
4781 vptr = gfc_get_symbol_decl (vsym);
4782 vptr = gfc_build_addr_expr (NULL, vptr);
4785 if (CLASS_DATA (sym)->attr.dimension
4786 || (CLASS_DATA (sym)->attr.codimension
4787 && flag_coarray != GFC_FCOARRAY_LIB))
4789 tmp = gfc_class_data_get (sym->backend_decl);
4790 tmp = gfc_build_null_descriptor (TREE_TYPE (tmp));
4792 else
4793 tmp = null_pointer_node;
4795 DECL_INITIAL (sym->backend_decl)
4796 = gfc_class_set_static_fields (sym->backend_decl, vptr, tmp);
4797 TREE_CONSTANT (DECL_INITIAL (sym->backend_decl)) = 1;
4799 else if ((sym->attr.dimension || sym->attr.codimension
4800 || (IS_CLASS_ARRAY (sym) && !CLASS_DATA (sym)->attr.allocatable)))
4802 bool is_classarray = IS_CLASS_ARRAY (sym);
4803 symbol_attribute *array_attr;
4804 gfc_array_spec *as;
4805 array_type type_of_array;
4807 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
4808 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
4809 /* Assumed-size Cray pointees need to be treated as AS_EXPLICIT. */
4810 type_of_array = as->type;
4811 if (type_of_array == AS_ASSUMED_SIZE && as->cp_was_assumed)
4812 type_of_array = AS_EXPLICIT;
4813 switch (type_of_array)
4815 case AS_EXPLICIT:
4816 if (sym->attr.dummy || sym->attr.result)
4817 gfc_trans_dummy_array_bias (sym, sym->backend_decl, block);
4818 /* Allocatable and pointer arrays need to processed
4819 explicitly. */
4820 else if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
4821 || (sym->ts.type == BT_CLASS
4822 && CLASS_DATA (sym)->attr.class_pointer)
4823 || array_attr->allocatable)
4825 if (TREE_STATIC (sym->backend_decl))
4827 gfc_save_backend_locus (&loc);
4828 gfc_set_backend_locus (&sym->declared_at);
4829 gfc_trans_static_array_pointer (sym);
4830 gfc_restore_backend_locus (&loc);
4832 else
4834 seen_trans_deferred_array = true;
4835 gfc_trans_deferred_array (sym, block);
4838 else if (sym->attr.codimension
4839 && TREE_STATIC (sym->backend_decl))
4841 gfc_init_block (&tmpblock);
4842 gfc_trans_array_cobounds (TREE_TYPE (sym->backend_decl),
4843 &tmpblock, sym);
4844 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock),
4845 NULL_TREE);
4846 continue;
4848 else
4850 gfc_save_backend_locus (&loc);
4851 gfc_set_backend_locus (&sym->declared_at);
4853 if (alloc_comp_or_fini)
4855 seen_trans_deferred_array = true;
4856 gfc_trans_deferred_array (sym, block);
4858 else if (sym->ts.type == BT_DERIVED
4859 && sym->value
4860 && !sym->attr.data
4861 && sym->attr.save == SAVE_NONE)
4863 gfc_start_block (&tmpblock);
4864 gfc_init_default_dt (sym, &tmpblock, false);
4865 gfc_add_init_cleanup (block,
4866 gfc_finish_block (&tmpblock),
4867 NULL_TREE);
4870 gfc_trans_auto_array_allocation (sym->backend_decl,
4871 sym, block);
4872 gfc_restore_backend_locus (&loc);
4874 break;
4876 case AS_ASSUMED_SIZE:
4877 /* Must be a dummy parameter. */
4878 gcc_assert (sym->attr.dummy || as->cp_was_assumed);
4880 /* We should always pass assumed size arrays the g77 way. */
4881 if (sym->attr.dummy)
4882 gfc_trans_g77_array (sym, block);
4883 break;
4885 case AS_ASSUMED_SHAPE:
4886 /* Must be a dummy parameter. */
4887 gcc_assert (sym->attr.dummy);
4889 gfc_trans_dummy_array_bias (sym, sym->backend_decl, block);
4890 break;
4892 case AS_ASSUMED_RANK:
4893 case AS_DEFERRED:
4894 seen_trans_deferred_array = true;
4895 gfc_trans_deferred_array (sym, block);
4896 if (sym->ts.type == BT_CHARACTER && sym->ts.deferred
4897 && sym->attr.result)
4899 gfc_start_block (&init);
4900 gfc_save_backend_locus (&loc);
4901 gfc_set_backend_locus (&sym->declared_at);
4902 tmp = gfc_null_and_pass_deferred_len (sym, &init, &loc);
4903 gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
4905 break;
4907 default:
4908 gcc_unreachable ();
4910 if (alloc_comp_or_fini && !seen_trans_deferred_array)
4911 gfc_trans_deferred_array (sym, block);
4913 else if ((!sym->attr.dummy || sym->ts.deferred)
4914 && (sym->ts.type == BT_CLASS
4915 && CLASS_DATA (sym)->attr.class_pointer))
4916 gfc_trans_class_array (sym, block);
4917 else if ((!sym->attr.dummy || sym->ts.deferred)
4918 && (sym->attr.allocatable
4919 || (sym->attr.pointer && sym->attr.result)
4920 || (sym->ts.type == BT_CLASS
4921 && CLASS_DATA (sym)->attr.allocatable)))
4923 if (!sym->attr.save && flag_max_stack_var_size != 0)
4925 tree descriptor = NULL_TREE;
4927 gfc_save_backend_locus (&loc);
4928 gfc_set_backend_locus (&sym->declared_at);
4929 gfc_start_block (&init);
4931 if (sym->ts.type == BT_CHARACTER
4932 && sym->attr.allocatable
4933 && !sym->attr.dimension
4934 && sym->ts.u.cl && sym->ts.u.cl->length
4935 && sym->ts.u.cl->length->expr_type == EXPR_VARIABLE)
4936 gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
4938 if (!sym->attr.pointer)
4940 /* Nullify and automatic deallocation of allocatable
4941 scalars. */
4942 e = gfc_lval_expr_from_sym (sym);
4943 if (sym->ts.type == BT_CLASS)
4944 gfc_add_data_component (e);
4946 gfc_init_se (&se, NULL);
4947 if (sym->ts.type != BT_CLASS
4948 || sym->ts.u.derived->attr.dimension
4949 || sym->ts.u.derived->attr.codimension)
4951 se.want_pointer = 1;
4952 gfc_conv_expr (&se, e);
4954 else if (sym->ts.type == BT_CLASS
4955 && !CLASS_DATA (sym)->attr.dimension
4956 && !CLASS_DATA (sym)->attr.codimension)
4958 se.want_pointer = 1;
4959 gfc_conv_expr (&se, e);
4961 else
4963 se.descriptor_only = 1;
4964 gfc_conv_expr (&se, e);
4965 descriptor = se.expr;
4966 se.expr = gfc_conv_descriptor_data_addr (se.expr);
4967 se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
4969 gfc_free_expr (e);
4971 if (!sym->attr.dummy || sym->attr.intent == INTENT_OUT)
4973 /* Nullify when entering the scope. */
4974 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
4975 TREE_TYPE (se.expr), se.expr,
4976 fold_convert (TREE_TYPE (se.expr),
4977 null_pointer_node));
4978 if (sym->attr.optional)
4980 tree present = gfc_conv_expr_present (sym);
4981 tmp = build3_loc (input_location, COND_EXPR,
4982 void_type_node, present, tmp,
4983 build_empty_stmt (input_location));
4985 gfc_add_expr_to_block (&init, tmp);
4989 if ((sym->attr.dummy || sym->attr.result)
4990 && sym->ts.type == BT_CHARACTER
4991 && sym->ts.deferred
4992 && sym->ts.u.cl->passed_length)
4993 tmp = gfc_null_and_pass_deferred_len (sym, &init, &loc);
4994 else
4996 gfc_restore_backend_locus (&loc);
4997 tmp = NULL_TREE;
5000 /* Initialize descriptor's TKR information. */
5001 if (sym->ts.type == BT_CLASS)
5002 gfc_trans_class_array (sym, block);
5004 /* Deallocate when leaving the scope. Nullifying is not
5005 needed. */
5006 if (!sym->attr.result && !sym->attr.dummy && !sym->attr.pointer
5007 && !sym->ns->proc_name->attr.is_main_program)
5009 if (sym->ts.type == BT_CLASS
5010 && CLASS_DATA (sym)->attr.codimension)
5011 tmp = gfc_deallocate_with_status (descriptor, NULL_TREE,
5012 NULL_TREE, NULL_TREE,
5013 NULL_TREE, true, NULL,
5014 GFC_CAF_COARRAY_ANALYZE);
5015 else
5017 gfc_expr *expr = gfc_lval_expr_from_sym (sym);
5018 tmp = gfc_deallocate_scalar_with_status (se.expr,
5019 NULL_TREE,
5020 NULL_TREE,
5021 true, expr,
5022 sym->ts);
5023 gfc_free_expr (expr);
5027 if (sym->ts.type == BT_CLASS)
5029 /* Initialize _vptr to declared type. */
5030 gfc_symbol *vtab;
5031 tree rhs;
5033 gfc_save_backend_locus (&loc);
5034 gfc_set_backend_locus (&sym->declared_at);
5035 e = gfc_lval_expr_from_sym (sym);
5036 gfc_add_vptr_component (e);
5037 gfc_init_se (&se, NULL);
5038 se.want_pointer = 1;
5039 gfc_conv_expr (&se, e);
5040 gfc_free_expr (e);
5041 if (UNLIMITED_POLY (sym))
5042 rhs = build_int_cst (TREE_TYPE (se.expr), 0);
5043 else
5045 vtab = gfc_find_derived_vtab (sym->ts.u.derived);
5046 rhs = gfc_build_addr_expr (TREE_TYPE (se.expr),
5047 gfc_get_symbol_decl (vtab));
5049 gfc_add_modify (&init, se.expr, rhs);
5050 gfc_restore_backend_locus (&loc);
5053 gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
5056 else if (sym->ts.type == BT_CHARACTER && sym->ts.deferred)
5058 tree tmp = NULL;
5059 stmtblock_t init;
5061 /* If we get to here, all that should be left are pointers. */
5062 gcc_assert (sym->attr.pointer);
5064 if (sym->attr.dummy)
5066 gfc_start_block (&init);
5067 gfc_save_backend_locus (&loc);
5068 gfc_set_backend_locus (&sym->declared_at);
5069 tmp = gfc_null_and_pass_deferred_len (sym, &init, &loc);
5070 gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
5073 else if (sym->ts.deferred)
5074 gfc_fatal_error ("Deferred type parameter not yet supported");
5075 else if (alloc_comp_or_fini)
5076 gfc_trans_deferred_array (sym, block);
5077 else if (sym->ts.type == BT_CHARACTER)
5079 gfc_save_backend_locus (&loc);
5080 gfc_set_backend_locus (&sym->declared_at);
5081 if (sym->attr.dummy || sym->attr.result)
5082 gfc_trans_dummy_character (sym, sym->ts.u.cl, block);
5083 else
5084 gfc_trans_auto_character_variable (sym, block);
5085 gfc_restore_backend_locus (&loc);
5087 else if (sym->attr.assign)
5089 gfc_save_backend_locus (&loc);
5090 gfc_set_backend_locus (&sym->declared_at);
5091 gfc_trans_assign_aux_var (sym, block);
5092 gfc_restore_backend_locus (&loc);
5094 else if (sym->ts.type == BT_DERIVED
5095 && sym->value
5096 && !sym->attr.data
5097 && sym->attr.save == SAVE_NONE)
5099 gfc_start_block (&tmpblock);
5100 gfc_init_default_dt (sym, &tmpblock, false);
5101 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock),
5102 NULL_TREE);
5104 else if (!(UNLIMITED_POLY(sym)) && !is_pdt_type)
5105 gcc_unreachable ();
5108 gfc_init_block (&tmpblock);
5110 for (f = gfc_sym_get_dummy_args (proc_sym); f; f = f->next)
5112 if (f->sym && f->sym->tlink == NULL && f->sym->ts.type == BT_CHARACTER
5113 && f->sym->ts.u.cl->backend_decl)
5115 if (TREE_CODE (f->sym->ts.u.cl->backend_decl) == PARM_DECL)
5116 gfc_trans_vla_type_sizes (f->sym, &tmpblock);
5120 if (gfc_return_by_reference (proc_sym) && proc_sym->ts.type == BT_CHARACTER
5121 && current_fake_result_decl != NULL)
5123 gcc_assert (proc_sym->ts.u.cl->backend_decl != NULL);
5124 if (TREE_CODE (proc_sym->ts.u.cl->backend_decl) == PARM_DECL)
5125 gfc_trans_vla_type_sizes (proc_sym, &tmpblock);
5128 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), NULL_TREE);
5132 struct module_hasher : ggc_ptr_hash<module_htab_entry>
5134 typedef const char *compare_type;
5136 static hashval_t hash (module_htab_entry *s)
5138 return htab_hash_string (s->name);
5141 static bool
5142 equal (module_htab_entry *a, const char *b)
5144 return !strcmp (a->name, b);
5148 static GTY (()) hash_table<module_hasher> *module_htab;
5150 /* Hash and equality functions for module_htab's decls. */
5152 hashval_t
5153 module_decl_hasher::hash (tree t)
5155 const_tree n = DECL_NAME (t);
5156 if (n == NULL_TREE)
5157 n = TYPE_NAME (TREE_TYPE (t));
5158 return htab_hash_string (IDENTIFIER_POINTER (n));
5161 bool
5162 module_decl_hasher::equal (tree t1, const char *x2)
5164 const_tree n1 = DECL_NAME (t1);
5165 if (n1 == NULL_TREE)
5166 n1 = TYPE_NAME (TREE_TYPE (t1));
5167 return strcmp (IDENTIFIER_POINTER (n1), x2) == 0;
5170 struct module_htab_entry *
5171 gfc_find_module (const char *name)
5173 if (! module_htab)
5174 module_htab = hash_table<module_hasher>::create_ggc (10);
5176 module_htab_entry **slot
5177 = module_htab->find_slot_with_hash (name, htab_hash_string (name), INSERT);
5178 if (*slot == NULL)
5180 module_htab_entry *entry = ggc_cleared_alloc<module_htab_entry> ();
5182 entry->name = gfc_get_string ("%s", name);
5183 entry->decls = hash_table<module_decl_hasher>::create_ggc (10);
5184 *slot = entry;
5186 return *slot;
5189 void
5190 gfc_module_add_decl (struct module_htab_entry *entry, tree decl)
5192 const char *name;
5194 if (DECL_NAME (decl))
5195 name = IDENTIFIER_POINTER (DECL_NAME (decl));
5196 else
5198 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
5199 name = IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl)));
5201 tree *slot
5202 = entry->decls->find_slot_with_hash (name, htab_hash_string (name),
5203 INSERT);
5204 if (*slot == NULL)
5205 *slot = decl;
5209 /* Generate debugging symbols for namelists. This function must come after
5210 generate_local_decl to ensure that the variables in the namelist are
5211 already declared. */
5213 static tree
5214 generate_namelist_decl (gfc_symbol * sym)
5216 gfc_namelist *nml;
5217 tree decl;
5218 vec<constructor_elt, va_gc> *nml_decls = NULL;
5220 gcc_assert (sym->attr.flavor == FL_NAMELIST);
5221 for (nml = sym->namelist; nml; nml = nml->next)
5223 if (nml->sym->backend_decl == NULL_TREE)
5225 nml->sym->attr.referenced = 1;
5226 nml->sym->backend_decl = gfc_get_symbol_decl (nml->sym);
5228 DECL_IGNORED_P (nml->sym->backend_decl) = 0;
5229 CONSTRUCTOR_APPEND_ELT (nml_decls, NULL_TREE, nml->sym->backend_decl);
5232 decl = make_node (NAMELIST_DECL);
5233 TREE_TYPE (decl) = void_type_node;
5234 NAMELIST_DECL_ASSOCIATED_DECL (decl) = build_constructor (NULL_TREE, nml_decls);
5235 DECL_NAME (decl) = get_identifier (sym->name);
5236 return decl;
5240 /* Output an initialized decl for a module variable. */
5242 static void
5243 gfc_create_module_variable (gfc_symbol * sym)
5245 tree decl;
5247 /* Module functions with alternate entries are dealt with later and
5248 would get caught by the next condition. */
5249 if (sym->attr.entry)
5250 return;
5252 /* Make sure we convert the types of the derived types from iso_c_binding
5253 into (void *). */
5254 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
5255 && sym->ts.type == BT_DERIVED)
5256 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
5258 if (gfc_fl_struct (sym->attr.flavor)
5259 && sym->backend_decl
5260 && TREE_CODE (sym->backend_decl) == RECORD_TYPE)
5262 decl = sym->backend_decl;
5263 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
5265 if (!sym->attr.use_assoc && !sym->attr.used_in_submodule)
5267 gcc_assert (TYPE_CONTEXT (decl) == NULL_TREE
5268 || TYPE_CONTEXT (decl) == sym->ns->proc_name->backend_decl);
5269 gcc_assert (DECL_CONTEXT (TYPE_STUB_DECL (decl)) == NULL_TREE
5270 || DECL_CONTEXT (TYPE_STUB_DECL (decl))
5271 == sym->ns->proc_name->backend_decl);
5273 TYPE_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
5274 DECL_CONTEXT (TYPE_STUB_DECL (decl)) = sym->ns->proc_name->backend_decl;
5275 gfc_module_add_decl (cur_module, TYPE_STUB_DECL (decl));
5278 /* Only output variables, procedure pointers and array valued,
5279 or derived type, parameters. */
5280 if (sym->attr.flavor != FL_VARIABLE
5281 && !(sym->attr.flavor == FL_PARAMETER
5282 && (sym->attr.dimension || sym->ts.type == BT_DERIVED))
5283 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer))
5284 return;
5286 if ((sym->attr.in_common || sym->attr.in_equivalence) && sym->backend_decl)
5288 decl = sym->backend_decl;
5289 gcc_assert (DECL_FILE_SCOPE_P (decl));
5290 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
5291 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
5292 gfc_module_add_decl (cur_module, decl);
5295 /* Don't generate variables from other modules. Variables from
5296 COMMONs and Cray pointees will already have been generated. */
5297 if (sym->attr.use_assoc || sym->attr.used_in_submodule
5298 || sym->attr.in_common || sym->attr.cray_pointee)
5299 return;
5301 /* Equivalenced variables arrive here after creation. */
5302 if (sym->backend_decl
5303 && (sym->equiv_built || sym->attr.in_equivalence))
5304 return;
5306 if (sym->backend_decl && !sym->attr.vtab && !sym->attr.target)
5307 gfc_internal_error ("backend decl for module variable %qs already exists",
5308 sym->name);
5310 if (sym->module && !sym->attr.result && !sym->attr.dummy
5311 && (sym->attr.access == ACCESS_UNKNOWN
5312 && (sym->ns->default_access == ACCESS_PRIVATE
5313 || (sym->ns->default_access == ACCESS_UNKNOWN
5314 && flag_module_private))))
5315 sym->attr.access = ACCESS_PRIVATE;
5317 if (warn_unused_variable && !sym->attr.referenced
5318 && sym->attr.access == ACCESS_PRIVATE)
5319 gfc_warning (OPT_Wunused_value,
5320 "Unused PRIVATE module variable %qs declared at %L",
5321 sym->name, &sym->declared_at);
5323 /* We always want module variables to be created. */
5324 sym->attr.referenced = 1;
5325 /* Create the decl. */
5326 decl = gfc_get_symbol_decl (sym);
5328 /* Create the variable. */
5329 pushdecl (decl);
5330 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE
5331 || (sym->ns->parent->proc_name->attr.flavor == FL_MODULE
5332 && sym->fn_result_spec));
5333 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
5334 rest_of_decl_compilation (decl, 1, 0);
5335 gfc_module_add_decl (cur_module, decl);
5337 /* Also add length of strings. */
5338 if (sym->ts.type == BT_CHARACTER)
5340 tree length;
5342 length = sym->ts.u.cl->backend_decl;
5343 gcc_assert (length || sym->attr.proc_pointer);
5344 if (length && !INTEGER_CST_P (length))
5346 pushdecl (length);
5347 rest_of_decl_compilation (length, 1, 0);
5351 if (sym->attr.codimension && !sym->attr.dummy && !sym->attr.allocatable
5352 && sym->attr.referenced && !sym->attr.use_assoc)
5353 has_coarray_vars = true;
5356 /* Emit debug information for USE statements. */
5358 static void
5359 gfc_trans_use_stmts (gfc_namespace * ns)
5361 gfc_use_list *use_stmt;
5362 for (use_stmt = ns->use_stmts; use_stmt; use_stmt = use_stmt->next)
5364 struct module_htab_entry *entry
5365 = gfc_find_module (use_stmt->module_name);
5366 gfc_use_rename *rent;
5368 if (entry->namespace_decl == NULL)
5370 entry->namespace_decl
5371 = build_decl (input_location,
5372 NAMESPACE_DECL,
5373 get_identifier (use_stmt->module_name),
5374 void_type_node);
5375 DECL_EXTERNAL (entry->namespace_decl) = 1;
5377 gfc_set_backend_locus (&use_stmt->where);
5378 if (!use_stmt->only_flag)
5379 (*debug_hooks->imported_module_or_decl) (entry->namespace_decl,
5380 NULL_TREE,
5381 ns->proc_name->backend_decl,
5382 false, false);
5383 for (rent = use_stmt->rename; rent; rent = rent->next)
5385 tree decl, local_name;
5387 if (rent->op != INTRINSIC_NONE)
5388 continue;
5390 hashval_t hash = htab_hash_string (rent->use_name);
5391 tree *slot = entry->decls->find_slot_with_hash (rent->use_name, hash,
5392 INSERT);
5393 if (*slot == NULL)
5395 gfc_symtree *st;
5397 st = gfc_find_symtree (ns->sym_root,
5398 rent->local_name[0]
5399 ? rent->local_name : rent->use_name);
5401 /* The following can happen if a derived type is renamed. */
5402 if (!st)
5404 char *name;
5405 name = xstrdup (rent->local_name[0]
5406 ? rent->local_name : rent->use_name);
5407 name[0] = (char) TOUPPER ((unsigned char) name[0]);
5408 st = gfc_find_symtree (ns->sym_root, name);
5409 free (name);
5410 gcc_assert (st);
5413 /* Sometimes, generic interfaces wind up being over-ruled by a
5414 local symbol (see PR41062). */
5415 if (!st->n.sym->attr.use_assoc)
5417 *slot = error_mark_node;
5418 entry->decls->clear_slot (slot);
5419 continue;
5422 if (st->n.sym->backend_decl
5423 && DECL_P (st->n.sym->backend_decl)
5424 && st->n.sym->module
5425 && strcmp (st->n.sym->module, use_stmt->module_name) == 0)
5427 gcc_assert (DECL_EXTERNAL (entry->namespace_decl)
5428 || !VAR_P (st->n.sym->backend_decl));
5429 decl = copy_node (st->n.sym->backend_decl);
5430 DECL_CONTEXT (decl) = entry->namespace_decl;
5431 DECL_EXTERNAL (decl) = 1;
5432 DECL_IGNORED_P (decl) = 0;
5433 DECL_INITIAL (decl) = NULL_TREE;
5435 else if (st->n.sym->attr.flavor == FL_NAMELIST
5436 && st->n.sym->attr.use_only
5437 && st->n.sym->module
5438 && strcmp (st->n.sym->module, use_stmt->module_name)
5439 == 0)
5441 decl = generate_namelist_decl (st->n.sym);
5442 DECL_CONTEXT (decl) = entry->namespace_decl;
5443 DECL_EXTERNAL (decl) = 1;
5444 DECL_IGNORED_P (decl) = 0;
5445 DECL_INITIAL (decl) = NULL_TREE;
5447 else
5449 *slot = error_mark_node;
5450 entry->decls->clear_slot (slot);
5451 continue;
5453 *slot = decl;
5455 decl = (tree) *slot;
5456 if (rent->local_name[0])
5457 local_name = get_identifier (rent->local_name);
5458 else
5459 local_name = NULL_TREE;
5460 gfc_set_backend_locus (&rent->where);
5461 (*debug_hooks->imported_module_or_decl) (decl, local_name,
5462 ns->proc_name->backend_decl,
5463 !use_stmt->only_flag,
5464 false);
5470 /* Return true if expr is a constant initializer that gfc_conv_initializer
5471 will handle. */
5473 static bool
5474 check_constant_initializer (gfc_expr *expr, gfc_typespec *ts, bool array,
5475 bool pointer)
5477 gfc_constructor *c;
5478 gfc_component *cm;
5480 if (pointer)
5481 return true;
5482 else if (array)
5484 if (expr->expr_type == EXPR_CONSTANT || expr->expr_type == EXPR_NULL)
5485 return true;
5486 else if (expr->expr_type == EXPR_STRUCTURE)
5487 return check_constant_initializer (expr, ts, false, false);
5488 else if (expr->expr_type != EXPR_ARRAY)
5489 return false;
5490 for (c = gfc_constructor_first (expr->value.constructor);
5491 c; c = gfc_constructor_next (c))
5493 if (c->iterator)
5494 return false;
5495 if (c->expr->expr_type == EXPR_STRUCTURE)
5497 if (!check_constant_initializer (c->expr, ts, false, false))
5498 return false;
5500 else if (c->expr->expr_type != EXPR_CONSTANT)
5501 return false;
5503 return true;
5505 else switch (ts->type)
5507 case_bt_struct:
5508 if (expr->expr_type != EXPR_STRUCTURE)
5509 return false;
5510 cm = expr->ts.u.derived->components;
5511 for (c = gfc_constructor_first (expr->value.constructor);
5512 c; c = gfc_constructor_next (c), cm = cm->next)
5514 if (!c->expr || cm->attr.allocatable)
5515 continue;
5516 if (!check_constant_initializer (c->expr, &cm->ts,
5517 cm->attr.dimension,
5518 cm->attr.pointer))
5519 return false;
5521 return true;
5522 default:
5523 return expr->expr_type == EXPR_CONSTANT;
5527 /* Emit debug info for parameters and unreferenced variables with
5528 initializers. */
5530 static void
5531 gfc_emit_parameter_debug_info (gfc_symbol *sym)
5533 tree decl;
5535 if (sym->attr.flavor != FL_PARAMETER
5536 && (sym->attr.flavor != FL_VARIABLE || sym->attr.referenced))
5537 return;
5539 if (sym->backend_decl != NULL
5540 || sym->value == NULL
5541 || sym->attr.use_assoc
5542 || sym->attr.dummy
5543 || sym->attr.result
5544 || sym->attr.function
5545 || sym->attr.intrinsic
5546 || sym->attr.pointer
5547 || sym->attr.allocatable
5548 || sym->attr.cray_pointee
5549 || sym->attr.threadprivate
5550 || sym->attr.is_bind_c
5551 || sym->attr.subref_array_pointer
5552 || sym->attr.assign)
5553 return;
5555 if (sym->ts.type == BT_CHARACTER)
5557 gfc_conv_const_charlen (sym->ts.u.cl);
5558 if (sym->ts.u.cl->backend_decl == NULL
5559 || TREE_CODE (sym->ts.u.cl->backend_decl) != INTEGER_CST)
5560 return;
5562 else if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.alloc_comp)
5563 return;
5565 if (sym->as)
5567 int n;
5569 if (sym->as->type != AS_EXPLICIT)
5570 return;
5571 for (n = 0; n < sym->as->rank; n++)
5572 if (sym->as->lower[n]->expr_type != EXPR_CONSTANT
5573 || sym->as->upper[n] == NULL
5574 || sym->as->upper[n]->expr_type != EXPR_CONSTANT)
5575 return;
5578 if (!check_constant_initializer (sym->value, &sym->ts,
5579 sym->attr.dimension, false))
5580 return;
5582 if (flag_coarray == GFC_FCOARRAY_LIB && sym->attr.codimension)
5583 return;
5585 /* Create the decl for the variable or constant. */
5586 decl = build_decl (input_location,
5587 sym->attr.flavor == FL_PARAMETER ? CONST_DECL : VAR_DECL,
5588 gfc_sym_identifier (sym), gfc_sym_type (sym));
5589 if (sym->attr.flavor == FL_PARAMETER)
5590 TREE_READONLY (decl) = 1;
5591 gfc_set_decl_location (decl, &sym->declared_at);
5592 if (sym->attr.dimension)
5593 GFC_DECL_PACKED_ARRAY (decl) = 1;
5594 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
5595 TREE_STATIC (decl) = 1;
5596 TREE_USED (decl) = 1;
5597 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
5598 TREE_PUBLIC (decl) = 1;
5599 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
5600 TREE_TYPE (decl),
5601 sym->attr.dimension,
5602 false, false);
5603 debug_hooks->early_global_decl (decl);
5607 static void
5608 generate_coarray_sym_init (gfc_symbol *sym)
5610 tree tmp, size, decl, token, desc;
5611 bool is_lock_type, is_event_type;
5612 int reg_type;
5613 gfc_se se;
5614 symbol_attribute attr;
5616 if (sym->attr.dummy || sym->attr.allocatable || !sym->attr.codimension
5617 || sym->attr.use_assoc || !sym->attr.referenced
5618 || sym->attr.associate_var
5619 || sym->attr.select_type_temporary)
5620 return;
5622 decl = sym->backend_decl;
5623 TREE_USED(decl) = 1;
5624 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
5626 is_lock_type = sym->ts.type == BT_DERIVED
5627 && sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
5628 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE;
5630 is_event_type = sym->ts.type == BT_DERIVED
5631 && sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
5632 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE;
5634 /* FIXME: Workaround for PR middle-end/49106, cf. also PR middle-end/49108
5635 to make sure the variable is not optimized away. */
5636 DECL_PRESERVE_P (DECL_CONTEXT (decl)) = 1;
5638 /* For lock types, we pass the array size as only the library knows the
5639 size of the variable. */
5640 if (is_lock_type || is_event_type)
5641 size = gfc_index_one_node;
5642 else
5643 size = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (decl)));
5645 /* Ensure that we do not have size=0 for zero-sized arrays. */
5646 size = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
5647 fold_convert (size_type_node, size),
5648 build_int_cst (size_type_node, 1));
5650 if (GFC_TYPE_ARRAY_RANK (TREE_TYPE (decl)))
5652 tmp = GFC_TYPE_ARRAY_SIZE (TREE_TYPE (decl));
5653 size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
5654 fold_convert (size_type_node, tmp), size);
5657 gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (decl)) != NULL_TREE);
5658 token = gfc_build_addr_expr (ppvoid_type_node,
5659 GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE(decl)));
5660 if (is_lock_type)
5661 reg_type = sym->attr.artificial ? GFC_CAF_CRITICAL : GFC_CAF_LOCK_STATIC;
5662 else if (is_event_type)
5663 reg_type = GFC_CAF_EVENT_STATIC;
5664 else
5665 reg_type = GFC_CAF_COARRAY_STATIC;
5667 /* Compile the symbol attribute. */
5668 if (sym->ts.type == BT_CLASS)
5670 attr = CLASS_DATA (sym)->attr;
5671 /* The pointer attribute is always set on classes, overwrite it with the
5672 class_pointer attribute, which denotes the pointer for classes. */
5673 attr.pointer = attr.class_pointer;
5675 else
5676 attr = sym->attr;
5677 gfc_init_se (&se, NULL);
5678 desc = gfc_conv_scalar_to_descriptor (&se, decl, attr);
5679 gfc_add_block_to_block (&caf_init_block, &se.pre);
5681 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_register, 7, size,
5682 build_int_cst (integer_type_node, reg_type),
5683 token, gfc_build_addr_expr (pvoid_type_node, desc),
5684 null_pointer_node, /* stat. */
5685 null_pointer_node, /* errgmsg. */
5686 build_zero_cst (size_type_node)); /* errmsg_len. */
5687 gfc_add_expr_to_block (&caf_init_block, tmp);
5688 gfc_add_modify (&caf_init_block, decl, fold_convert (TREE_TYPE (decl),
5689 gfc_conv_descriptor_data_get (desc)));
5691 /* Handle "static" initializer. */
5692 if (sym->value)
5694 if (sym->value->expr_type == EXPR_ARRAY)
5696 gfc_constructor *c, *cnext;
5698 /* Test if the array has more than one element. */
5699 c = gfc_constructor_first (sym->value->value.constructor);
5700 gcc_assert (c); /* Empty constructor should not happen here. */
5701 cnext = gfc_constructor_next (c);
5703 if (cnext)
5705 /* An EXPR_ARRAY with a rank > 1 here has to come from a
5706 DATA statement. Set its rank here as not to confuse
5707 the following steps. */
5708 sym->value->rank = 1;
5710 else
5712 /* There is only a single value in the constructor, use
5713 it directly for the assignment. */
5714 gfc_expr *new_expr;
5715 new_expr = gfc_copy_expr (c->expr);
5716 gfc_free_expr (sym->value);
5717 sym->value = new_expr;
5721 sym->attr.pointer = 1;
5722 tmp = gfc_trans_assignment (gfc_lval_expr_from_sym (sym), sym->value,
5723 true, false);
5724 sym->attr.pointer = 0;
5725 gfc_add_expr_to_block (&caf_init_block, tmp);
5727 else if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.pointer_comp)
5729 tmp = gfc_nullify_alloc_comp (sym->ts.u.derived, decl, sym->as
5730 ? sym->as->rank : 0,
5731 GFC_STRUCTURE_CAF_MODE_IN_COARRAY);
5732 gfc_add_expr_to_block (&caf_init_block, tmp);
5737 /* Generate constructor function to initialize static, nonallocatable
5738 coarrays. */
5740 static void
5741 generate_coarray_init (gfc_namespace * ns __attribute((unused)))
5743 tree fndecl, tmp, decl, save_fn_decl;
5745 save_fn_decl = current_function_decl;
5746 push_function_context ();
5748 tmp = build_function_type_list (void_type_node, NULL_TREE);
5749 fndecl = build_decl (input_location, FUNCTION_DECL,
5750 create_tmp_var_name ("_caf_init"), tmp);
5752 DECL_STATIC_CONSTRUCTOR (fndecl) = 1;
5753 SET_DECL_INIT_PRIORITY (fndecl, DEFAULT_INIT_PRIORITY);
5755 decl = build_decl (input_location, RESULT_DECL, NULL_TREE, void_type_node);
5756 DECL_ARTIFICIAL (decl) = 1;
5757 DECL_IGNORED_P (decl) = 1;
5758 DECL_CONTEXT (decl) = fndecl;
5759 DECL_RESULT (fndecl) = decl;
5761 pushdecl (fndecl);
5762 current_function_decl = fndecl;
5763 announce_function (fndecl);
5765 rest_of_decl_compilation (fndecl, 0, 0);
5766 make_decl_rtl (fndecl);
5767 allocate_struct_function (fndecl, false);
5769 pushlevel ();
5770 gfc_init_block (&caf_init_block);
5772 gfc_traverse_ns (ns, generate_coarray_sym_init);
5774 DECL_SAVED_TREE (fndecl) = gfc_finish_block (&caf_init_block);
5775 decl = getdecls ();
5777 poplevel (1, 1);
5778 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
5780 DECL_SAVED_TREE (fndecl)
5781 = fold_build3_loc (DECL_SOURCE_LOCATION (fndecl), BIND_EXPR, void_type_node,
5782 decl, DECL_SAVED_TREE (fndecl), DECL_INITIAL (fndecl));
5783 dump_function (TDI_original, fndecl);
5785 cfun->function_end_locus = input_location;
5786 set_cfun (NULL);
5788 if (decl_function_context (fndecl))
5789 (void) cgraph_node::create (fndecl);
5790 else
5791 cgraph_node::finalize_function (fndecl, true);
5793 pop_function_context ();
5794 current_function_decl = save_fn_decl;
5798 static void
5799 create_module_nml_decl (gfc_symbol *sym)
5801 if (sym->attr.flavor == FL_NAMELIST)
5803 tree decl = generate_namelist_decl (sym);
5804 pushdecl (decl);
5805 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
5806 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
5807 rest_of_decl_compilation (decl, 1, 0);
5808 gfc_module_add_decl (cur_module, decl);
5813 /* Generate all the required code for module variables. */
5815 void
5816 gfc_generate_module_vars (gfc_namespace * ns)
5818 module_namespace = ns;
5819 cur_module = gfc_find_module (ns->proc_name->name);
5821 /* Check if the frontend left the namespace in a reasonable state. */
5822 gcc_assert (ns->proc_name && !ns->proc_name->tlink);
5824 /* Generate COMMON blocks. */
5825 gfc_trans_common (ns);
5827 has_coarray_vars = false;
5829 /* Create decls for all the module variables. */
5830 gfc_traverse_ns (ns, gfc_create_module_variable);
5831 gfc_traverse_ns (ns, create_module_nml_decl);
5833 if (flag_coarray == GFC_FCOARRAY_LIB && has_coarray_vars)
5834 generate_coarray_init (ns);
5836 cur_module = NULL;
5838 gfc_trans_use_stmts (ns);
5839 gfc_traverse_ns (ns, gfc_emit_parameter_debug_info);
5843 static void
5844 gfc_generate_contained_functions (gfc_namespace * parent)
5846 gfc_namespace *ns;
5848 /* We create all the prototypes before generating any code. */
5849 for (ns = parent->contained; ns; ns = ns->sibling)
5851 /* Skip namespaces from used modules. */
5852 if (ns->parent != parent)
5853 continue;
5855 gfc_create_function_decl (ns, false);
5858 for (ns = parent->contained; ns; ns = ns->sibling)
5860 /* Skip namespaces from used modules. */
5861 if (ns->parent != parent)
5862 continue;
5864 gfc_generate_function_code (ns);
5869 /* Drill down through expressions for the array specification bounds and
5870 character length calling generate_local_decl for all those variables
5871 that have not already been declared. */
5873 static void
5874 generate_local_decl (gfc_symbol *);
5876 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
5878 static bool
5879 expr_decls (gfc_expr *e, gfc_symbol *sym,
5880 int *f ATTRIBUTE_UNUSED)
5882 if (e->expr_type != EXPR_VARIABLE
5883 || sym == e->symtree->n.sym
5884 || e->symtree->n.sym->mark
5885 || e->symtree->n.sym->ns != sym->ns)
5886 return false;
5888 generate_local_decl (e->symtree->n.sym);
5889 return false;
5892 static void
5893 generate_expr_decls (gfc_symbol *sym, gfc_expr *e)
5895 gfc_traverse_expr (e, sym, expr_decls, 0);
5899 /* Check for dependencies in the character length and array spec. */
5901 static void
5902 generate_dependency_declarations (gfc_symbol *sym)
5904 int i;
5906 if (sym->ts.type == BT_CHARACTER
5907 && sym->ts.u.cl
5908 && sym->ts.u.cl->length
5909 && sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
5910 generate_expr_decls (sym, sym->ts.u.cl->length);
5912 if (sym->as && sym->as->rank)
5914 for (i = 0; i < sym->as->rank; i++)
5916 generate_expr_decls (sym, sym->as->lower[i]);
5917 generate_expr_decls (sym, sym->as->upper[i]);
5923 /* Generate decls for all local variables. We do this to ensure correct
5924 handling of expressions which only appear in the specification of
5925 other functions. */
5927 static void
5928 generate_local_decl (gfc_symbol * sym)
5930 if (sym->attr.flavor == FL_VARIABLE)
5932 if (sym->attr.codimension && !sym->attr.dummy && !sym->attr.allocatable
5933 && sym->attr.referenced && !sym->attr.use_assoc)
5934 has_coarray_vars = true;
5936 if (!sym->attr.dummy && !sym->ns->proc_name->attr.entry_master)
5937 generate_dependency_declarations (sym);
5939 if (sym->attr.ext_attr & (1 << EXT_ATTR_WEAK))
5941 if (sym->attr.dummy)
5942 gfc_error ("Symbol %qs at %L has the WEAK attribute but is a "
5943 "dummy argument", sym->name, &sym->declared_at);
5944 else
5945 gfc_error ("Symbol %qs at %L has the WEAK attribute but is a "
5946 "local variable", sym->name, &sym->declared_at);
5949 if (sym->attr.referenced)
5950 gfc_get_symbol_decl (sym);
5952 /* Warnings for unused dummy arguments. */
5953 else if (sym->attr.dummy && !sym->attr.in_namelist)
5955 /* INTENT(out) dummy arguments are likely meant to be set. */
5956 if (warn_unused_dummy_argument && sym->attr.intent == INTENT_OUT)
5958 if (sym->ts.type != BT_DERIVED)
5959 gfc_warning (OPT_Wunused_dummy_argument,
5960 "Dummy argument %qs at %L was declared "
5961 "INTENT(OUT) but was not set", sym->name,
5962 &sym->declared_at);
5963 else if (!gfc_has_default_initializer (sym->ts.u.derived)
5964 && !sym->ts.u.derived->attr.zero_comp)
5965 gfc_warning (OPT_Wunused_dummy_argument,
5966 "Derived-type dummy argument %qs at %L was "
5967 "declared INTENT(OUT) but was not set and "
5968 "does not have a default initializer",
5969 sym->name, &sym->declared_at);
5970 if (sym->backend_decl != NULL_TREE)
5971 suppress_warning (sym->backend_decl);
5973 else if (warn_unused_dummy_argument)
5975 if (!sym->attr.artificial)
5976 gfc_warning (OPT_Wunused_dummy_argument,
5977 "Unused dummy argument %qs at %L", sym->name,
5978 &sym->declared_at);
5980 if (sym->backend_decl != NULL_TREE)
5981 suppress_warning (sym->backend_decl);
5985 /* Warn for unused variables, but not if they're inside a common
5986 block or a namelist. */
5987 else if (warn_unused_variable
5988 && !(sym->attr.in_common || sym->mark || sym->attr.in_namelist))
5990 if (sym->attr.use_only)
5992 gfc_warning (OPT_Wunused_variable,
5993 "Unused module variable %qs which has been "
5994 "explicitly imported at %L", sym->name,
5995 &sym->declared_at);
5996 if (sym->backend_decl != NULL_TREE)
5997 suppress_warning (sym->backend_decl);
5999 else if (!sym->attr.use_assoc)
6001 /* Corner case: the symbol may be an entry point. At this point,
6002 it may appear to be an unused variable. Suppress warning. */
6003 bool enter = false;
6004 gfc_entry_list *el;
6006 for (el = sym->ns->entries; el; el=el->next)
6007 if (strcmp(sym->name, el->sym->name) == 0)
6008 enter = true;
6010 if (!enter)
6011 gfc_warning (OPT_Wunused_variable,
6012 "Unused variable %qs declared at %L",
6013 sym->name, &sym->declared_at);
6014 if (sym->backend_decl != NULL_TREE)
6015 suppress_warning (sym->backend_decl);
6019 /* For variable length CHARACTER parameters, the PARM_DECL already
6020 references the length variable, so force gfc_get_symbol_decl
6021 even when not referenced. If optimize > 0, it will be optimized
6022 away anyway. But do this only after emitting -Wunused-parameter
6023 warning if requested. */
6024 if (sym->attr.dummy && !sym->attr.referenced
6025 && sym->ts.type == BT_CHARACTER
6026 && sym->ts.u.cl->backend_decl != NULL
6027 && VAR_P (sym->ts.u.cl->backend_decl))
6029 sym->attr.referenced = 1;
6030 gfc_get_symbol_decl (sym);
6033 /* INTENT(out) dummy arguments and result variables with allocatable
6034 components are reset by default and need to be set referenced to
6035 generate the code for nullification and automatic lengths. */
6036 if (!sym->attr.referenced
6037 && sym->ts.type == BT_DERIVED
6038 && sym->ts.u.derived->attr.alloc_comp
6039 && !sym->attr.pointer
6040 && ((sym->attr.dummy && sym->attr.intent == INTENT_OUT)
6042 (sym->attr.result && sym != sym->result)))
6044 sym->attr.referenced = 1;
6045 gfc_get_symbol_decl (sym);
6048 /* Check for dependencies in the array specification and string
6049 length, adding the necessary declarations to the function. We
6050 mark the symbol now, as well as in traverse_ns, to prevent
6051 getting stuck in a circular dependency. */
6052 sym->mark = 1;
6054 else if (sym->attr.flavor == FL_PARAMETER)
6056 if (warn_unused_parameter
6057 && !sym->attr.referenced)
6059 if (!sym->attr.use_assoc)
6060 gfc_warning (OPT_Wunused_parameter,
6061 "Unused parameter %qs declared at %L", sym->name,
6062 &sym->declared_at);
6063 else if (sym->attr.use_only)
6064 gfc_warning (OPT_Wunused_parameter,
6065 "Unused parameter %qs which has been explicitly "
6066 "imported at %L", sym->name, &sym->declared_at);
6069 if (sym->ns && sym->ns->construct_entities)
6071 /* Construction of the intrinsic modules within a BLOCK
6072 construct, where ONLY and RENAMED entities are included,
6073 seems to be bogus. This is a workaround that can be removed
6074 if someone ever takes on the task to creating full-fledge
6075 modules. See PR 69455. */
6076 if (sym->attr.referenced
6077 && sym->from_intmod != INTMOD_ISO_C_BINDING
6078 && sym->from_intmod != INTMOD_ISO_FORTRAN_ENV)
6079 gfc_get_symbol_decl (sym);
6080 sym->mark = 1;
6083 else if (sym->attr.flavor == FL_PROCEDURE)
6085 /* TODO: move to the appropriate place in resolve.cc. */
6086 if (warn_return_type > 0
6087 && sym->attr.function
6088 && sym->result
6089 && sym != sym->result
6090 && !sym->result->attr.referenced
6091 && !sym->attr.use_assoc
6092 && sym->attr.if_source != IFSRC_IFBODY)
6094 gfc_warning (OPT_Wreturn_type,
6095 "Return value %qs of function %qs declared at "
6096 "%L not set", sym->result->name, sym->name,
6097 &sym->result->declared_at);
6099 /* Prevents "Unused variable" warning for RESULT variables. */
6100 sym->result->mark = 1;
6104 if (sym->attr.dummy == 1)
6106 /* The tree type for scalar character dummy arguments of BIND(C)
6107 procedures, if they are passed by value, should be unsigned char.
6108 The value attribute implies the dummy is a scalar. */
6109 if (sym->attr.value == 1 && sym->backend_decl != NULL
6110 && sym->ts.type == BT_CHARACTER && sym->ts.is_c_interop
6111 && sym->ns->proc_name != NULL && sym->ns->proc_name->attr.is_bind_c)
6113 /* We used to modify the tree here. Now it is done earlier in
6114 the front-end, so we only check it here to avoid regressions. */
6115 gcc_assert (TREE_CODE (TREE_TYPE (sym->backend_decl)) == INTEGER_TYPE);
6116 gcc_assert (TYPE_UNSIGNED (TREE_TYPE (sym->backend_decl)) == 1);
6117 gcc_assert (TYPE_PRECISION (TREE_TYPE (sym->backend_decl)) == CHAR_TYPE_SIZE);
6118 gcc_assert (DECL_BY_REFERENCE (sym->backend_decl) == 0);
6121 /* Unused procedure passed as dummy argument. */
6122 if (sym->attr.flavor == FL_PROCEDURE)
6124 if (!sym->attr.referenced && !sym->attr.artificial)
6126 if (warn_unused_dummy_argument)
6127 gfc_warning (OPT_Wunused_dummy_argument,
6128 "Unused dummy argument %qs at %L", sym->name,
6129 &sym->declared_at);
6132 /* Silence bogus "unused parameter" warnings from the
6133 middle end. */
6134 if (sym->backend_decl != NULL_TREE)
6135 suppress_warning (sym->backend_decl);
6139 /* Make sure we convert the types of the derived types from iso_c_binding
6140 into (void *). */
6141 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
6142 && sym->ts.type == BT_DERIVED)
6143 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
6147 static void
6148 generate_local_nml_decl (gfc_symbol * sym)
6150 if (sym->attr.flavor == FL_NAMELIST && !sym->attr.use_assoc)
6152 tree decl = generate_namelist_decl (sym);
6153 pushdecl (decl);
6158 static void
6159 generate_local_vars (gfc_namespace * ns)
6161 gfc_traverse_ns (ns, generate_local_decl);
6162 gfc_traverse_ns (ns, generate_local_nml_decl);
6166 /* Generate a switch statement to jump to the correct entry point. Also
6167 creates the label decls for the entry points. */
6169 static tree
6170 gfc_trans_entry_master_switch (gfc_entry_list * el)
6172 stmtblock_t block;
6173 tree label;
6174 tree tmp;
6175 tree val;
6177 gfc_init_block (&block);
6178 for (; el; el = el->next)
6180 /* Add the case label. */
6181 label = gfc_build_label_decl (NULL_TREE);
6182 val = build_int_cst (gfc_array_index_type, el->id);
6183 tmp = build_case_label (val, NULL_TREE, label);
6184 gfc_add_expr_to_block (&block, tmp);
6186 /* And jump to the actual entry point. */
6187 label = gfc_build_label_decl (NULL_TREE);
6188 tmp = build1_v (GOTO_EXPR, label);
6189 gfc_add_expr_to_block (&block, tmp);
6191 /* Save the label decl. */
6192 el->label = label;
6194 tmp = gfc_finish_block (&block);
6195 /* The first argument selects the entry point. */
6196 val = DECL_ARGUMENTS (current_function_decl);
6197 tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE, val, tmp);
6198 return tmp;
6202 /* Add code to string lengths of actual arguments passed to a function against
6203 the expected lengths of the dummy arguments. */
6205 static void
6206 add_argument_checking (stmtblock_t *block, gfc_symbol *sym)
6208 gfc_formal_arglist *formal;
6210 for (formal = gfc_sym_get_dummy_args (sym); formal; formal = formal->next)
6211 if (formal->sym && formal->sym->ts.type == BT_CHARACTER
6212 && !formal->sym->ts.deferred)
6214 enum tree_code comparison;
6215 tree cond;
6216 tree argname;
6217 gfc_symbol *fsym;
6218 gfc_charlen *cl;
6219 const char *message;
6221 fsym = formal->sym;
6222 cl = fsym->ts.u.cl;
6224 gcc_assert (cl);
6225 gcc_assert (cl->passed_length != NULL_TREE);
6226 gcc_assert (cl->backend_decl != NULL_TREE);
6228 /* For POINTER, ALLOCATABLE and assumed-shape dummy arguments, the
6229 string lengths must match exactly. Otherwise, it is only required
6230 that the actual string length is *at least* the expected one.
6231 Sequence association allows for a mismatch of the string length
6232 if the actual argument is (part of) an array, but only if the
6233 dummy argument is an array. (See "Sequence association" in
6234 Section 12.4.1.4 for F95 and 12.4.1.5 for F2003.) */
6235 if (fsym->attr.pointer || fsym->attr.allocatable
6236 || (fsym->as && (fsym->as->type == AS_ASSUMED_SHAPE
6237 || fsym->as->type == AS_ASSUMED_RANK)))
6239 comparison = NE_EXPR;
6240 message = _("Actual string length does not match the declared one"
6241 " for dummy argument '%s' (%ld/%ld)");
6243 else if (fsym->as && fsym->as->rank != 0)
6244 continue;
6245 else
6247 comparison = LT_EXPR;
6248 message = _("Actual string length is shorter than the declared one"
6249 " for dummy argument '%s' (%ld/%ld)");
6252 /* Build the condition. For optional arguments, an actual length
6253 of 0 is also acceptable if the associated string is NULL, which
6254 means the argument was not passed. */
6255 cond = fold_build2_loc (input_location, comparison, logical_type_node,
6256 cl->passed_length, cl->backend_decl);
6257 if (fsym->attr.optional)
6259 tree not_absent;
6260 tree not_0length;
6261 tree absent_failed;
6263 not_0length = fold_build2_loc (input_location, NE_EXPR,
6264 logical_type_node,
6265 cl->passed_length,
6266 build_zero_cst
6267 (TREE_TYPE (cl->passed_length)));
6268 /* The symbol needs to be referenced for gfc_get_symbol_decl. */
6269 fsym->attr.referenced = 1;
6270 not_absent = gfc_conv_expr_present (fsym);
6272 absent_failed = fold_build2_loc (input_location, TRUTH_OR_EXPR,
6273 logical_type_node, not_0length,
6274 not_absent);
6276 cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
6277 logical_type_node, cond, absent_failed);
6280 /* Build the runtime check. */
6281 argname = gfc_build_cstring_const (fsym->name);
6282 argname = gfc_build_addr_expr (pchar_type_node, argname);
6283 gfc_trans_runtime_check (true, false, cond, block, &fsym->declared_at,
6284 message, argname,
6285 fold_convert (long_integer_type_node,
6286 cl->passed_length),
6287 fold_convert (long_integer_type_node,
6288 cl->backend_decl));
6293 static void
6294 create_main_function (tree fndecl)
6296 tree old_context;
6297 tree ftn_main;
6298 tree tmp, decl, result_decl, argc, argv, typelist, arglist;
6299 stmtblock_t body;
6301 old_context = current_function_decl;
6303 if (old_context)
6305 push_function_context ();
6306 saved_parent_function_decls = saved_function_decls;
6307 saved_function_decls = NULL_TREE;
6310 /* main() function must be declared with global scope. */
6311 gcc_assert (current_function_decl == NULL_TREE);
6313 /* Declare the function. */
6314 tmp = build_function_type_list (integer_type_node, integer_type_node,
6315 build_pointer_type (pchar_type_node),
6316 NULL_TREE);
6317 main_identifier_node = get_identifier ("main");
6318 ftn_main = build_decl (input_location, FUNCTION_DECL,
6319 main_identifier_node, tmp);
6320 DECL_EXTERNAL (ftn_main) = 0;
6321 TREE_PUBLIC (ftn_main) = 1;
6322 TREE_STATIC (ftn_main) = 1;
6323 DECL_ATTRIBUTES (ftn_main)
6324 = tree_cons (get_identifier("externally_visible"), NULL_TREE, NULL_TREE);
6326 /* Setup the result declaration (for "return 0"). */
6327 result_decl = build_decl (input_location,
6328 RESULT_DECL, NULL_TREE, integer_type_node);
6329 DECL_ARTIFICIAL (result_decl) = 1;
6330 DECL_IGNORED_P (result_decl) = 1;
6331 DECL_CONTEXT (result_decl) = ftn_main;
6332 DECL_RESULT (ftn_main) = result_decl;
6334 pushdecl (ftn_main);
6336 /* Get the arguments. */
6338 arglist = NULL_TREE;
6339 typelist = TYPE_ARG_TYPES (TREE_TYPE (ftn_main));
6341 tmp = TREE_VALUE (typelist);
6342 argc = build_decl (input_location, PARM_DECL, get_identifier ("argc"), tmp);
6343 DECL_CONTEXT (argc) = ftn_main;
6344 DECL_ARG_TYPE (argc) = TREE_VALUE (typelist);
6345 TREE_READONLY (argc) = 1;
6346 gfc_finish_decl (argc);
6347 arglist = chainon (arglist, argc);
6349 typelist = TREE_CHAIN (typelist);
6350 tmp = TREE_VALUE (typelist);
6351 argv = build_decl (input_location, PARM_DECL, get_identifier ("argv"), tmp);
6352 DECL_CONTEXT (argv) = ftn_main;
6353 DECL_ARG_TYPE (argv) = TREE_VALUE (typelist);
6354 TREE_READONLY (argv) = 1;
6355 DECL_BY_REFERENCE (argv) = 1;
6356 gfc_finish_decl (argv);
6357 arglist = chainon (arglist, argv);
6359 DECL_ARGUMENTS (ftn_main) = arglist;
6360 current_function_decl = ftn_main;
6361 announce_function (ftn_main);
6363 rest_of_decl_compilation (ftn_main, 1, 0);
6364 make_decl_rtl (ftn_main);
6365 allocate_struct_function (ftn_main, false);
6366 pushlevel ();
6368 gfc_init_block (&body);
6370 /* Call some libgfortran initialization routines, call then MAIN__(). */
6372 /* Call _gfortran_caf_init (*argc, ***argv). */
6373 if (flag_coarray == GFC_FCOARRAY_LIB)
6375 tree pint_type, pppchar_type;
6376 pint_type = build_pointer_type (integer_type_node);
6377 pppchar_type
6378 = build_pointer_type (build_pointer_type (pchar_type_node));
6380 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_init, 2,
6381 gfc_build_addr_expr (pint_type, argc),
6382 gfc_build_addr_expr (pppchar_type, argv));
6383 gfc_add_expr_to_block (&body, tmp);
6386 /* Call _gfortran_set_args (argc, argv). */
6387 TREE_USED (argc) = 1;
6388 TREE_USED (argv) = 1;
6389 tmp = build_call_expr_loc (input_location,
6390 gfor_fndecl_set_args, 2, argc, argv);
6391 gfc_add_expr_to_block (&body, tmp);
6393 /* Add a call to set_options to set up the runtime library Fortran
6394 language standard parameters. */
6396 tree array_type, array, var;
6397 vec<constructor_elt, va_gc> *v = NULL;
6398 static const int noptions = 7;
6400 /* Passing a new option to the library requires three modifications:
6401 + add it to the tree_cons list below
6402 + change the noptions variable above
6403 + modify the library (runtime/compile_options.c)! */
6405 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6406 build_int_cst (integer_type_node,
6407 gfc_option.warn_std));
6408 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6409 build_int_cst (integer_type_node,
6410 gfc_option.allow_std));
6411 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6412 build_int_cst (integer_type_node, pedantic));
6413 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6414 build_int_cst (integer_type_node, flag_backtrace));
6415 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6416 build_int_cst (integer_type_node, flag_sign_zero));
6417 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6418 build_int_cst (integer_type_node,
6419 (gfc_option.rtcheck
6420 & GFC_RTCHECK_BOUNDS)));
6421 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6422 build_int_cst (integer_type_node,
6423 gfc_option.fpe_summary));
6425 array_type = build_array_type_nelts (integer_type_node, noptions);
6426 array = build_constructor (array_type, v);
6427 TREE_CONSTANT (array) = 1;
6428 TREE_STATIC (array) = 1;
6430 /* Create a static variable to hold the jump table. */
6431 var = build_decl (input_location, VAR_DECL,
6432 create_tmp_var_name ("options"), array_type);
6433 DECL_ARTIFICIAL (var) = 1;
6434 DECL_IGNORED_P (var) = 1;
6435 TREE_CONSTANT (var) = 1;
6436 TREE_STATIC (var) = 1;
6437 TREE_READONLY (var) = 1;
6438 DECL_INITIAL (var) = array;
6439 pushdecl (var);
6440 var = gfc_build_addr_expr (build_pointer_type (integer_type_node), var);
6442 tmp = build_call_expr_loc (input_location,
6443 gfor_fndecl_set_options, 2,
6444 build_int_cst (integer_type_node, noptions), var);
6445 gfc_add_expr_to_block (&body, tmp);
6448 /* If -ffpe-trap option was provided, add a call to set_fpe so that
6449 the library will raise a FPE when needed. */
6450 if (gfc_option.fpe != 0)
6452 tmp = build_call_expr_loc (input_location,
6453 gfor_fndecl_set_fpe, 1,
6454 build_int_cst (integer_type_node,
6455 gfc_option.fpe));
6456 gfc_add_expr_to_block (&body, tmp);
6459 /* If this is the main program and an -fconvert option was provided,
6460 add a call to set_convert. */
6462 if (flag_convert != GFC_FLAG_CONVERT_NATIVE)
6464 tmp = build_call_expr_loc (input_location,
6465 gfor_fndecl_set_convert, 1,
6466 build_int_cst (integer_type_node, flag_convert));
6467 gfc_add_expr_to_block (&body, tmp);
6470 /* If this is the main program and an -frecord-marker option was provided,
6471 add a call to set_record_marker. */
6473 if (flag_record_marker != 0)
6475 tmp = build_call_expr_loc (input_location,
6476 gfor_fndecl_set_record_marker, 1,
6477 build_int_cst (integer_type_node,
6478 flag_record_marker));
6479 gfc_add_expr_to_block (&body, tmp);
6482 if (flag_max_subrecord_length != 0)
6484 tmp = build_call_expr_loc (input_location,
6485 gfor_fndecl_set_max_subrecord_length, 1,
6486 build_int_cst (integer_type_node,
6487 flag_max_subrecord_length));
6488 gfc_add_expr_to_block (&body, tmp);
6491 /* Call MAIN__(). */
6492 tmp = build_call_expr_loc (input_location,
6493 fndecl, 0);
6494 gfc_add_expr_to_block (&body, tmp);
6496 /* Mark MAIN__ as used. */
6497 TREE_USED (fndecl) = 1;
6499 /* Coarray: Call _gfortran_caf_finalize(void). */
6500 if (flag_coarray == GFC_FCOARRAY_LIB)
6502 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_finalize, 0);
6503 gfc_add_expr_to_block (&body, tmp);
6506 /* "return 0". */
6507 tmp = fold_build2_loc (input_location, MODIFY_EXPR, integer_type_node,
6508 DECL_RESULT (ftn_main),
6509 build_int_cst (integer_type_node, 0));
6510 tmp = build1_v (RETURN_EXPR, tmp);
6511 gfc_add_expr_to_block (&body, tmp);
6514 DECL_SAVED_TREE (ftn_main) = gfc_finish_block (&body);
6515 decl = getdecls ();
6517 /* Finish off this function and send it for code generation. */
6518 poplevel (1, 1);
6519 BLOCK_SUPERCONTEXT (DECL_INITIAL (ftn_main)) = ftn_main;
6521 DECL_SAVED_TREE (ftn_main)
6522 = fold_build3_loc (DECL_SOURCE_LOCATION (ftn_main), BIND_EXPR,
6523 void_type_node, decl, DECL_SAVED_TREE (ftn_main),
6524 DECL_INITIAL (ftn_main));
6526 /* Output the GENERIC tree. */
6527 dump_function (TDI_original, ftn_main);
6529 cgraph_node::finalize_function (ftn_main, true);
6531 if (old_context)
6533 pop_function_context ();
6534 saved_function_decls = saved_parent_function_decls;
6536 current_function_decl = old_context;
6540 /* Generate an appropriate return-statement for a procedure. */
6542 tree
6543 gfc_generate_return (void)
6545 gfc_symbol* sym;
6546 tree result;
6547 tree fndecl;
6549 sym = current_procedure_symbol;
6550 fndecl = sym->backend_decl;
6552 if (TREE_TYPE (DECL_RESULT (fndecl)) == void_type_node)
6553 result = NULL_TREE;
6554 else
6556 result = get_proc_result (sym);
6558 /* Set the return value to the dummy result variable. The
6559 types may be different for scalar default REAL functions
6560 with -ff2c, therefore we have to convert. */
6561 if (result != NULL_TREE)
6563 result = convert (TREE_TYPE (DECL_RESULT (fndecl)), result);
6564 result = fold_build2_loc (input_location, MODIFY_EXPR,
6565 TREE_TYPE (result), DECL_RESULT (fndecl),
6566 result);
6568 else
6570 /* If the function does not have a result variable, result is
6571 NULL_TREE, and a 'return' is generated without a variable.
6572 The following generates a 'return __result_XXX' where XXX is
6573 the function name. */
6574 if (sym == sym->result && sym->attr.function && !flag_f2c)
6576 result = gfc_get_fake_result_decl (sym, 0);
6577 result = fold_build2_loc (input_location, MODIFY_EXPR,
6578 TREE_TYPE (result),
6579 DECL_RESULT (fndecl), result);
6584 return build1_v (RETURN_EXPR, result);
6588 static void
6589 is_from_ieee_module (gfc_symbol *sym)
6591 if (sym->from_intmod == INTMOD_IEEE_FEATURES
6592 || sym->from_intmod == INTMOD_IEEE_EXCEPTIONS
6593 || sym->from_intmod == INTMOD_IEEE_ARITHMETIC)
6594 seen_ieee_symbol = 1;
6598 static int
6599 is_ieee_module_used (gfc_namespace *ns)
6601 seen_ieee_symbol = 0;
6602 gfc_traverse_ns (ns, is_from_ieee_module);
6603 return seen_ieee_symbol;
6607 static gfc_omp_clauses *module_oacc_clauses;
6610 static void
6611 add_clause (gfc_symbol *sym, gfc_omp_map_op map_op)
6613 gfc_omp_namelist *n;
6615 n = gfc_get_omp_namelist ();
6616 n->sym = sym;
6617 n->u.map_op = map_op;
6619 if (!module_oacc_clauses)
6620 module_oacc_clauses = gfc_get_omp_clauses ();
6622 if (module_oacc_clauses->lists[OMP_LIST_MAP])
6623 n->next = module_oacc_clauses->lists[OMP_LIST_MAP];
6625 module_oacc_clauses->lists[OMP_LIST_MAP] = n;
6629 static void
6630 find_module_oacc_declare_clauses (gfc_symbol *sym)
6632 if (sym->attr.use_assoc)
6634 gfc_omp_map_op map_op;
6636 if (sym->attr.oacc_declare_create)
6637 map_op = OMP_MAP_FORCE_ALLOC;
6639 if (sym->attr.oacc_declare_copyin)
6640 map_op = OMP_MAP_FORCE_TO;
6642 if (sym->attr.oacc_declare_deviceptr)
6643 map_op = OMP_MAP_FORCE_DEVICEPTR;
6645 if (sym->attr.oacc_declare_device_resident)
6646 map_op = OMP_MAP_DEVICE_RESIDENT;
6648 if (sym->attr.oacc_declare_create
6649 || sym->attr.oacc_declare_copyin
6650 || sym->attr.oacc_declare_deviceptr
6651 || sym->attr.oacc_declare_device_resident)
6653 sym->attr.referenced = 1;
6654 add_clause (sym, map_op);
6660 void
6661 finish_oacc_declare (gfc_namespace *ns, gfc_symbol *sym, bool block)
6663 gfc_code *code;
6664 gfc_oacc_declare *oc;
6665 locus where = gfc_current_locus;
6666 gfc_omp_clauses *omp_clauses = NULL;
6667 gfc_omp_namelist *n, *p;
6669 module_oacc_clauses = NULL;
6670 gfc_traverse_ns (ns, find_module_oacc_declare_clauses);
6672 if (module_oacc_clauses && sym->attr.flavor == FL_PROGRAM)
6674 gfc_oacc_declare *new_oc;
6676 new_oc = gfc_get_oacc_declare ();
6677 new_oc->next = ns->oacc_declare;
6678 new_oc->clauses = module_oacc_clauses;
6680 ns->oacc_declare = new_oc;
6683 if (!ns->oacc_declare)
6684 return;
6686 for (oc = ns->oacc_declare; oc; oc = oc->next)
6688 if (oc->module_var)
6689 continue;
6691 if (block)
6692 gfc_error ("Sorry, !$ACC DECLARE at %L is not allowed "
6693 "in BLOCK construct", &oc->loc);
6696 if (oc->clauses && oc->clauses->lists[OMP_LIST_MAP])
6698 if (omp_clauses == NULL)
6700 omp_clauses = oc->clauses;
6701 continue;
6704 for (n = oc->clauses->lists[OMP_LIST_MAP]; n; p = n, n = n->next)
6707 gcc_assert (p->next == NULL);
6709 p->next = omp_clauses->lists[OMP_LIST_MAP];
6710 omp_clauses = oc->clauses;
6714 if (!omp_clauses)
6715 return;
6717 for (n = omp_clauses->lists[OMP_LIST_MAP]; n; n = n->next)
6719 switch (n->u.map_op)
6721 case OMP_MAP_DEVICE_RESIDENT:
6722 n->u.map_op = OMP_MAP_FORCE_ALLOC;
6723 break;
6725 default:
6726 break;
6730 code = XCNEW (gfc_code);
6731 code->op = EXEC_OACC_DECLARE;
6732 code->loc = where;
6734 code->ext.oacc_declare = gfc_get_oacc_declare ();
6735 code->ext.oacc_declare->clauses = omp_clauses;
6737 code->block = XCNEW (gfc_code);
6738 code->block->op = EXEC_OACC_DECLARE;
6739 code->block->loc = where;
6741 if (ns->code)
6742 code->block->next = ns->code;
6744 ns->code = code;
6746 return;
6749 static void
6750 gfc_conv_cfi_to_gfc (stmtblock_t *init, stmtblock_t *finally,
6751 tree cfi_desc, tree gfc_desc, gfc_symbol *sym)
6753 stmtblock_t block;
6754 gfc_init_block (&block);
6755 tree cfi = build_fold_indirect_ref_loc (input_location, cfi_desc);
6756 tree idx, etype, tmp, tmp2, size_var = NULL_TREE, rank = NULL_TREE;
6757 bool do_copy_inout = false;
6759 /* When allocatable + intent out, free the cfi descriptor. */
6760 if (sym->attr.allocatable && sym->attr.intent == INTENT_OUT)
6762 tmp = gfc_get_cfi_desc_base_addr (cfi);
6763 tree call = builtin_decl_explicit (BUILT_IN_FREE);
6764 call = build_call_expr_loc (input_location, call, 1, tmp);
6765 gfc_add_expr_to_block (&block, fold_convert (void_type_node, call));
6766 gfc_add_modify (&block, tmp,
6767 fold_convert (TREE_TYPE (tmp), null_pointer_node));
6770 /* -fcheck=bound: Do version, rank, attribute, type and is-NULL checks. */
6771 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
6773 char *msg;
6774 tree tmp3;
6775 msg = xasprintf ("Unexpected version %%d (expected %d) in CFI descriptor "
6776 "passed to dummy argument %s", CFI_VERSION, sym->name);
6777 tmp2 = gfc_get_cfi_desc_version (cfi);
6778 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, tmp2,
6779 build_int_cst (TREE_TYPE (tmp2), CFI_VERSION));
6780 gfc_trans_runtime_check (true, false, tmp, &block, &sym->declared_at,
6781 msg, tmp2);
6782 free (msg);
6784 /* Rank check; however, for character(len=*), assumed/explicit-size arrays
6785 are permitted to differ in rank according to the Fortran rules. */
6786 if (sym->as && sym->as->type != AS_ASSUMED_SIZE
6787 && sym->as->type != AS_EXPLICIT)
6789 if (sym->as->rank != -1)
6790 msg = xasprintf ("Invalid rank %%d (expected %d) in CFI descriptor "
6791 "passed to dummy argument %s", sym->as->rank,
6792 sym->name);
6793 else
6794 msg = xasprintf ("Invalid rank %%d (expected 0..%d) in CFI "
6795 "descriptor passed to dummy argument %s",
6796 CFI_MAX_RANK, sym->name);
6798 tmp3 = tmp2 = tmp = gfc_get_cfi_desc_rank (cfi);
6799 if (sym->as->rank != -1)
6800 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
6801 tmp, build_int_cst (signed_char_type_node,
6802 sym->as->rank));
6803 else
6805 tmp = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
6806 tmp, build_zero_cst (TREE_TYPE (tmp)));
6807 tmp2 = fold_build2_loc (input_location, GT_EXPR,
6808 boolean_type_node, tmp2,
6809 build_int_cst (TREE_TYPE (tmp2),
6810 CFI_MAX_RANK));
6811 tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR,
6812 boolean_type_node, tmp, tmp2);
6814 gfc_trans_runtime_check (true, false, tmp, &block, &sym->declared_at,
6815 msg, tmp3);
6816 free (msg);
6819 tmp3 = tmp = gfc_get_cfi_desc_attribute (cfi);
6820 if (sym->attr.allocatable || sym->attr.pointer)
6822 int attr = (sym->attr.pointer ? CFI_attribute_pointer
6823 : CFI_attribute_allocatable);
6824 msg = xasprintf ("Invalid attribute %%d (expected %d) in CFI "
6825 "descriptor passed to dummy argument %s with %s "
6826 "attribute", attr, sym->name,
6827 sym->attr.pointer ? "pointer" : "allocatable");
6828 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
6829 tmp, build_int_cst (TREE_TYPE (tmp), attr));
6831 else
6833 int amin = MIN (CFI_attribute_pointer,
6834 MIN (CFI_attribute_allocatable, CFI_attribute_other));
6835 int amax = MAX (CFI_attribute_pointer,
6836 MAX (CFI_attribute_allocatable, CFI_attribute_other));
6837 msg = xasprintf ("Invalid attribute %%d (expected %d..%d) in CFI "
6838 "descriptor passed to nonallocatable, nonpointer "
6839 "dummy argument %s", amin, amax, sym->name);
6840 tmp2 = tmp;
6841 tmp = fold_build2_loc (input_location, LT_EXPR, boolean_type_node, tmp,
6842 build_int_cst (TREE_TYPE (tmp), amin));
6843 tmp2 = fold_build2_loc (input_location, GT_EXPR, boolean_type_node, tmp2,
6844 build_int_cst (TREE_TYPE (tmp2), amax));
6845 tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR,
6846 boolean_type_node, tmp, tmp2);
6847 gfc_trans_runtime_check (true, false, tmp, &block, &sym->declared_at,
6848 msg, tmp3);
6849 free (msg);
6850 msg = xasprintf ("Invalid unallocatated/unassociated CFI "
6851 "descriptor passed to nonallocatable, nonpointer "
6852 "dummy argument %s", sym->name);
6853 tmp3 = tmp = gfc_get_cfi_desc_base_addr (cfi),
6854 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
6855 tmp, null_pointer_node);
6857 gfc_trans_runtime_check (true, false, tmp, &block, &sym->declared_at,
6858 msg, tmp3);
6859 free (msg);
6861 if (sym->ts.type != BT_ASSUMED)
6863 int type = CFI_type_other;
6864 if (sym->ts.f90_type == BT_VOID)
6866 type = (sym->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
6867 ? CFI_type_cfunptr : CFI_type_cptr);
6869 else
6870 switch (sym->ts.type)
6872 case BT_INTEGER:
6873 case BT_LOGICAL:
6874 case BT_REAL:
6875 case BT_COMPLEX:
6876 type = CFI_type_from_type_kind (sym->ts.type, sym->ts.kind);
6877 break;
6878 case BT_CHARACTER:
6879 type = CFI_type_from_type_kind (CFI_type_Character,
6880 sym->ts.kind);
6881 break;
6882 case BT_DERIVED:
6883 type = CFI_type_struct;
6884 break;
6885 case BT_VOID:
6886 type = (sym->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
6887 ? CFI_type_cfunptr : CFI_type_cptr);
6888 break;
6889 case BT_ASSUMED:
6890 case BT_CLASS:
6891 case BT_PROCEDURE:
6892 case BT_HOLLERITH:
6893 case BT_UNION:
6894 case BT_BOZ:
6895 case BT_UNKNOWN:
6896 gcc_unreachable ();
6898 msg = xasprintf ("Unexpected type %%d (expected %d) in CFI descriptor"
6899 " passed to dummy argument %s", type, sym->name);
6900 tmp2 = tmp = gfc_get_cfi_desc_type (cfi);
6901 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
6902 tmp, build_int_cst (TREE_TYPE (tmp), type));
6903 gfc_trans_runtime_check (true, false, tmp, &block, &sym->declared_at,
6904 msg, tmp2);
6905 free (msg);
6909 if (!sym->attr.referenced)
6910 goto done;
6912 /* Set string length for len=* and len=:, otherwise, it is already set. */
6913 if (sym->ts.type == BT_CHARACTER && !sym->ts.u.cl->length)
6915 tmp = fold_convert (gfc_array_index_type,
6916 gfc_get_cfi_desc_elem_len (cfi));
6917 if (sym->ts.kind != 1)
6918 tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
6919 gfc_array_index_type, tmp,
6920 build_int_cst (gfc_charlen_type_node,
6921 sym->ts.kind));
6922 gfc_add_modify (&block, sym->ts.u.cl->backend_decl, tmp);
6925 if (sym->ts.type == BT_CHARACTER
6926 && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
6928 gfc_conv_string_length (sym->ts.u.cl, NULL, init);
6929 gfc_trans_vla_type_sizes (sym, init);
6932 /* gfc->data = cfi->base_addr - or for scalars: gfc = cfi->base_addr.
6933 assumed-size/explicit-size arrays end up here for character(len=*)
6934 only. */
6935 if (!sym->attr.dimension || !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
6937 tmp = gfc_get_cfi_desc_base_addr (cfi);
6938 gfc_add_modify (&block, gfc_desc,
6939 fold_convert (TREE_TYPE (gfc_desc), tmp));
6940 if (!sym->attr.dimension)
6941 goto done;
6944 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
6946 /* gfc->dtype = ... (from declaration, not from cfi). */
6947 etype = gfc_get_element_type (TREE_TYPE (gfc_desc));
6948 gfc_add_modify (&block, gfc_conv_descriptor_dtype (gfc_desc),
6949 gfc_get_dtype_rank_type (sym->as->rank, etype));
6950 /* gfc->data = cfi->base_addr. */
6951 gfc_conv_descriptor_data_set (&block, gfc_desc,
6952 gfc_get_cfi_desc_base_addr (cfi));
6955 if (sym->ts.type == BT_ASSUMED)
6957 /* For type(*), take elem_len + dtype.type from the actual argument. */
6958 gfc_add_modify (&block, gfc_conv_descriptor_elem_len (gfc_desc),
6959 gfc_get_cfi_desc_elem_len (cfi));
6960 tree cond;
6961 tree ctype = gfc_get_cfi_desc_type (cfi);
6962 ctype = fold_build2_loc (input_location, BIT_AND_EXPR, TREE_TYPE (ctype),
6963 ctype, build_int_cst (TREE_TYPE (ctype),
6964 CFI_type_mask));
6965 tree type = gfc_conv_descriptor_type (gfc_desc);
6967 /* if (CFI_type_cptr) BT_VOID else BT_UNKNOWN */
6968 /* Note: BT_VOID is could also be CFI_type_funcptr, but assume c_ptr. */
6969 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
6970 build_int_cst (TREE_TYPE (ctype), CFI_type_cptr));
6971 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, type,
6972 build_int_cst (TREE_TYPE (type), BT_VOID));
6973 tmp2 = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
6974 type,
6975 build_int_cst (TREE_TYPE (type), BT_UNKNOWN));
6976 tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6977 tmp, tmp2);
6978 /* if (CFI_type_struct) BT_DERIVED else < tmp2 > */
6979 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
6980 build_int_cst (TREE_TYPE (ctype),
6981 CFI_type_struct));
6982 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, type,
6983 build_int_cst (TREE_TYPE (type), BT_DERIVED));
6984 tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6985 tmp, tmp2);
6986 /* if (CFI_type_Character) BT_CHARACTER else < tmp2 > */
6987 /* Note: this is kind=1, CFI_type_ucs4_char is handled in the 'else if'
6988 before (see below, as generated bottom up). */
6989 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
6990 build_int_cst (TREE_TYPE (ctype),
6991 CFI_type_Character));
6992 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, type,
6993 build_int_cst (TREE_TYPE (type), BT_CHARACTER));
6994 tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6995 tmp, tmp2);
6996 /* if (CFI_type_ucs4_char) BT_CHARACTER else < tmp2 > */
6997 /* Note: gfc->elem_len = cfi->elem_len/4. */
6998 /* However, assuming that CFI_type_ucs4_char cannot be recovered, leave
6999 gfc->elem_len == cfi->elem_len, which helps with operations which use
7000 sizeof() in Fortran and cfi->elem_len in C. */
7001 tmp = gfc_get_cfi_desc_type (cfi);
7002 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, tmp,
7003 build_int_cst (TREE_TYPE (tmp),
7004 CFI_type_ucs4_char));
7005 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, type,
7006 build_int_cst (TREE_TYPE (type), BT_CHARACTER));
7007 tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
7008 tmp, tmp2);
7009 /* if (CFI_type_Complex) BT_COMPLEX + cfi->elem_len/2 else < tmp2 > */
7010 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
7011 build_int_cst (TREE_TYPE (ctype),
7012 CFI_type_Complex));
7013 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, type,
7014 build_int_cst (TREE_TYPE (type), BT_COMPLEX));
7015 tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
7016 tmp, tmp2);
7017 /* if (CFI_type_Integer || CFI_type_Logical || CFI_type_Real)
7018 ctype else <tmp2> */
7019 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
7020 build_int_cst (TREE_TYPE (ctype),
7021 CFI_type_Integer));
7022 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
7023 build_int_cst (TREE_TYPE (ctype),
7024 CFI_type_Logical));
7025 cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
7026 cond, tmp);
7027 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
7028 build_int_cst (TREE_TYPE (ctype),
7029 CFI_type_Real));
7030 cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
7031 cond, tmp);
7032 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
7033 type, fold_convert (TREE_TYPE (type), ctype));
7034 tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
7035 tmp, tmp2);
7036 gfc_add_expr_to_block (&block, tmp2);
7039 if (sym->as->rank < 0)
7041 /* Set gfc->dtype.rank, if assumed-rank. */
7042 rank = gfc_get_cfi_desc_rank (cfi);
7043 gfc_add_modify (&block, gfc_conv_descriptor_rank (gfc_desc), rank);
7045 else if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
7046 /* In that case, the CFI rank and the declared rank can differ. */
7047 rank = gfc_get_cfi_desc_rank (cfi);
7048 else
7049 rank = build_int_cst (signed_char_type_node, sym->as->rank);
7051 /* With bind(C), the standard requires that both Fortran callers and callees
7052 handle noncontiguous arrays passed to an dummy with 'contiguous' attribute
7053 and with character(len=*) + assumed-size/explicit-size arrays.
7054 cf. Fortran 2018, 18.3.6, paragraph 5 (and for the caller: para. 6). */
7055 if ((sym->ts.type == BT_CHARACTER && !sym->ts.u.cl->length
7056 && (sym->as->type == AS_ASSUMED_SIZE || sym->as->type == AS_EXPLICIT))
7057 || sym->attr.contiguous)
7059 do_copy_inout = true;
7060 gcc_assert (!sym->attr.pointer);
7061 stmtblock_t block2;
7062 tree data;
7063 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
7064 data = gfc_conv_descriptor_data_get (gfc_desc);
7065 else if (!POINTER_TYPE_P (TREE_TYPE (gfc_desc)))
7066 data = gfc_build_addr_expr (NULL, gfc_desc);
7067 else
7068 data = gfc_desc;
7070 /* Is copy-in/out needed? */
7071 /* do_copyin = rank != 0 && !assumed-size */
7072 tree cond_var = gfc_create_var (boolean_type_node, "do_copyin");
7073 tree cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7074 rank, build_zero_cst (TREE_TYPE (rank)));
7075 /* dim[rank-1].extent != -1 -> assumed size*/
7076 tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (rank),
7077 rank, build_int_cst (TREE_TYPE (rank), 1));
7078 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7079 gfc_get_cfi_dim_extent (cfi, tmp),
7080 build_int_cst (gfc_array_index_type, -1));
7081 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
7082 boolean_type_node, cond, tmp);
7083 gfc_add_modify (&block, cond_var, cond);
7084 /* if (do_copyin) do_copyin = ... || ... || ... */
7085 gfc_init_block (&block2);
7086 /* dim[0].sm != elem_len */
7087 tmp = fold_convert (gfc_array_index_type,
7088 gfc_get_cfi_desc_elem_len (cfi));
7089 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7090 gfc_get_cfi_dim_sm (cfi, gfc_index_zero_node),
7091 tmp);
7092 gfc_add_modify (&block2, cond_var, cond);
7094 /* for (i = 1; i < rank; ++i)
7095 cond &&= dim[i].sm != (dv->dim[i - 1].sm * dv->dim[i - 1].extent) */
7096 idx = gfc_create_var (TREE_TYPE (rank), "idx");
7097 stmtblock_t loop_body;
7098 gfc_init_block (&loop_body);
7099 tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (idx),
7100 idx, build_int_cst (TREE_TYPE (idx), 1));
7101 tree tmp2 = gfc_get_cfi_dim_sm (cfi, tmp);
7102 tmp = gfc_get_cfi_dim_extent (cfi, tmp);
7103 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
7104 tmp2, tmp);
7105 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7106 gfc_get_cfi_dim_sm (cfi, idx), tmp);
7107 cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
7108 cond_var, cond);
7109 gfc_add_modify (&loop_body, cond_var, cond);
7110 gfc_simple_for_loop (&block2, idx, build_int_cst (TREE_TYPE (idx), 1),
7111 rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
7112 gfc_finish_block (&loop_body));
7113 tmp = build3_v (COND_EXPR, cond_var, gfc_finish_block (&block2),
7114 build_empty_stmt (input_location));
7115 gfc_add_expr_to_block (&block, tmp);
7117 /* Copy-in body. */
7118 gfc_init_block (&block2);
7119 /* size = dim[0].extent; for (i = 1; i < rank; ++i) size *= dim[i].extent */
7120 size_var = gfc_create_var (size_type_node, "size");
7121 tmp = fold_convert (size_type_node,
7122 gfc_get_cfi_dim_extent (cfi, gfc_index_zero_node));
7123 gfc_add_modify (&block2, size_var, tmp);
7125 gfc_init_block (&loop_body);
7126 tmp = fold_convert (size_type_node,
7127 gfc_get_cfi_dim_extent (cfi, idx));
7128 tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
7129 size_var, fold_convert (size_type_node, tmp));
7130 gfc_add_modify (&loop_body, size_var, tmp);
7131 gfc_simple_for_loop (&block2, idx, build_int_cst (TREE_TYPE (idx), 1),
7132 rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
7133 gfc_finish_block (&loop_body));
7134 /* data = malloc (size * elem_len) */
7135 tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
7136 size_var, gfc_get_cfi_desc_elem_len (cfi));
7137 tree call = builtin_decl_explicit (BUILT_IN_MALLOC);
7138 call = build_call_expr_loc (input_location, call, 1, tmp);
7139 gfc_add_modify (&block2, data, fold_convert (TREE_TYPE (data), call));
7141 /* Copy the data:
7142 for (idx = 0; idx < size; ++idx)
7144 shift = 0;
7145 tmpidx = idx
7146 for (dim = 0; dim < rank; ++dim)
7148 shift += (tmpidx % extent[d]) * sm[d]
7149 tmpidx = tmpidx / extend[d]
7151 memcpy (lhs + idx*elem_len, rhs + shift, elem_len)
7152 } .*/
7153 idx = gfc_create_var (size_type_node, "arrayidx");
7154 gfc_init_block (&loop_body);
7155 tree shift = gfc_create_var (size_type_node, "shift");
7156 tree tmpidx = gfc_create_var (size_type_node, "tmpidx");
7157 gfc_add_modify (&loop_body, shift, build_zero_cst (TREE_TYPE (shift)));
7158 gfc_add_modify (&loop_body, tmpidx, idx);
7159 stmtblock_t inner_loop;
7160 gfc_init_block (&inner_loop);
7161 tree dim = gfc_create_var (TREE_TYPE (rank), "dim");
7162 /* shift += (tmpidx % extent[d]) * sm[d] */
7163 tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR,
7164 size_type_node, tmpidx,
7165 fold_convert (size_type_node,
7166 gfc_get_cfi_dim_extent (cfi, dim)));
7167 tmp = fold_build2_loc (input_location, MULT_EXPR,
7168 size_type_node, tmp,
7169 fold_convert (size_type_node,
7170 gfc_get_cfi_dim_sm (cfi, dim)));
7171 gfc_add_modify (&inner_loop, shift,
7172 fold_build2_loc (input_location, PLUS_EXPR,
7173 size_type_node, shift, tmp));
7174 /* tmpidx = tmpidx / extend[d] */
7175 tmp = fold_convert (size_type_node, gfc_get_cfi_dim_extent (cfi, dim));
7176 gfc_add_modify (&inner_loop, tmpidx,
7177 fold_build2_loc (input_location, TRUNC_DIV_EXPR,
7178 size_type_node, tmpidx, tmp));
7179 gfc_simple_for_loop (&loop_body, dim, build_zero_cst (TREE_TYPE (rank)),
7180 rank, LT_EXPR, build_int_cst (TREE_TYPE (dim), 1),
7181 gfc_finish_block (&inner_loop));
7182 /* Assign. */
7183 tmp = fold_convert (pchar_type_node, gfc_get_cfi_desc_base_addr (cfi));
7184 tmp = fold_build2 (POINTER_PLUS_EXPR, pchar_type_node, tmp, shift);
7185 tree lhs;
7186 /* memcpy (lhs + idx*elem_len, rhs + shift, elem_len) */
7187 tree elem_len;
7188 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
7189 elem_len = gfc_conv_descriptor_elem_len (gfc_desc);
7190 else
7191 elem_len = gfc_get_cfi_desc_elem_len (cfi);
7192 lhs = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
7193 elem_len, idx);
7194 lhs = fold_build2_loc (input_location, POINTER_PLUS_EXPR, pchar_type_node,
7195 fold_convert (pchar_type_node, data), lhs);
7196 tmp = fold_convert (pvoid_type_node, tmp);
7197 lhs = fold_convert (pvoid_type_node, lhs);
7198 call = builtin_decl_explicit (BUILT_IN_MEMCPY);
7199 call = build_call_expr_loc (input_location, call, 3, lhs, tmp, elem_len);
7200 gfc_add_expr_to_block (&loop_body, fold_convert (void_type_node, call));
7201 gfc_simple_for_loop (&block2, idx, build_zero_cst (TREE_TYPE (idx)),
7202 size_var, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
7203 gfc_finish_block (&loop_body));
7204 /* if (cond) { block2 } */
7205 tmp = build3_v (COND_EXPR, cond_var, gfc_finish_block (&block2),
7206 build_empty_stmt (input_location));
7207 gfc_add_expr_to_block (&block, tmp);
7210 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
7212 tree offset, type;
7213 type = TREE_TYPE (gfc_desc);
7214 gfc_trans_array_bounds (type, sym, &offset, &block);
7215 if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
7216 gfc_add_modify (&block, GFC_TYPE_ARRAY_OFFSET (type), offset);
7217 goto done;
7220 /* If cfi->data != NULL. */
7221 stmtblock_t block2;
7222 gfc_init_block (&block2);
7224 /* if do_copy_inout: gfc->dspan = gfc->dtype.elem_len
7225 We use gfc instead of cfi on the RHS as this might be a constant. */
7226 tmp = fold_convert (gfc_array_index_type,
7227 gfc_conv_descriptor_elem_len (gfc_desc));
7228 if (!do_copy_inout)
7230 /* gfc->dspan = ((cfi->dim[0].sm % gfc->elem_len)
7231 ? cfi->dim[0].sm : gfc->elem_len). */
7232 tree cond;
7233 tree tmp2 = gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]);
7234 cond = fold_build2_loc (input_location, TRUNC_MOD_EXPR,
7235 gfc_array_index_type, tmp2, tmp);
7236 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7237 cond, gfc_index_zero_node);
7238 tmp = build3_loc (input_location, COND_EXPR, gfc_array_index_type, cond,
7239 tmp2, tmp);
7241 gfc_conv_descriptor_span_set (&block2, gfc_desc, tmp);
7243 /* Calculate offset + set lbound, ubound and stride. */
7244 gfc_conv_descriptor_offset_set (&block2, gfc_desc, gfc_index_zero_node);
7245 if (sym->as->rank > 0 && !sym->attr.pointer && !sym->attr.allocatable)
7246 for (int i = 0; i < sym->as->rank; ++i)
7248 gfc_se se;
7249 gfc_init_se (&se, NULL );
7250 if (sym->as->lower[i])
7252 gfc_conv_expr (&se, sym->as->lower[i]);
7253 tmp = se.expr;
7255 else
7256 tmp = gfc_index_one_node;
7257 gfc_add_block_to_block (&block2, &se.pre);
7258 gfc_conv_descriptor_lbound_set (&block2, gfc_desc, gfc_rank_cst[i],
7259 tmp);
7260 gfc_add_block_to_block (&block2, &se.post);
7263 /* Loop: for (i = 0; i < rank; ++i). */
7264 idx = gfc_create_var (TREE_TYPE (rank), "idx");
7266 /* Loop body. */
7267 stmtblock_t loop_body;
7268 gfc_init_block (&loop_body);
7269 /* gfc->dim[i].lbound = ... */
7270 if (sym->attr.pointer || sym->attr.allocatable)
7272 tmp = gfc_get_cfi_dim_lbound (cfi, idx);
7273 gfc_conv_descriptor_lbound_set (&loop_body, gfc_desc, idx, tmp);
7275 else if (sym->as->rank < 0)
7276 gfc_conv_descriptor_lbound_set (&loop_body, gfc_desc, idx,
7277 gfc_index_one_node);
7279 /* gfc->dim[i].ubound = gfc->dim[i].lbound + cfi->dim[i].extent - 1. */
7280 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
7281 gfc_conv_descriptor_lbound_get (gfc_desc, idx),
7282 gfc_index_one_node);
7283 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
7284 gfc_get_cfi_dim_extent (cfi, idx), tmp);
7285 gfc_conv_descriptor_ubound_set (&loop_body, gfc_desc, idx, tmp);
7287 if (do_copy_inout)
7289 /* gfc->dim[i].stride
7290 = idx == 0 ? 1 : gfc->dim[i-1].stride * cfi->dim[i-1].extent */
7291 tree cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
7292 idx, build_zero_cst (TREE_TYPE (idx)));
7293 tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (idx),
7294 idx, build_int_cst (TREE_TYPE (idx), 1));
7295 tree tmp2 = gfc_get_cfi_dim_extent (cfi, tmp);
7296 tmp = gfc_conv_descriptor_stride_get (gfc_desc, tmp);
7297 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp2),
7298 tmp2, tmp);
7299 tmp = build3_loc (input_location, COND_EXPR, gfc_array_index_type, cond,
7300 gfc_index_one_node, tmp);
7302 else
7304 /* gfc->dim[i].stride = cfi->dim[i].sm / cfi>elem_len */
7305 tmp = gfc_get_cfi_dim_sm (cfi, idx);
7306 tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
7307 gfc_array_index_type, tmp,
7308 fold_convert (gfc_array_index_type,
7309 gfc_get_cfi_desc_elem_len (cfi)));
7311 gfc_conv_descriptor_stride_set (&loop_body, gfc_desc, idx, tmp);
7312 /* gfc->offset -= gfc->dim[i].stride * gfc->dim[i].lbound. */
7313 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
7314 gfc_conv_descriptor_stride_get (gfc_desc, idx),
7315 gfc_conv_descriptor_lbound_get (gfc_desc, idx));
7316 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
7317 gfc_conv_descriptor_offset_get (gfc_desc), tmp);
7318 gfc_conv_descriptor_offset_set (&loop_body, gfc_desc, tmp);
7320 /* Generate loop. */
7321 gfc_simple_for_loop (&block2, idx, build_zero_cst (TREE_TYPE (idx)),
7322 rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
7323 gfc_finish_block (&loop_body));
7324 if (sym->attr.allocatable || sym->attr.pointer)
7326 tmp = gfc_get_cfi_desc_base_addr (cfi),
7327 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7328 tmp, null_pointer_node);
7329 tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
7330 build_empty_stmt (input_location));
7331 gfc_add_expr_to_block (&block, tmp);
7333 else
7334 gfc_add_block_to_block (&block, &block2);
7336 done:
7337 /* If optional arg: 'if (arg) { block } else { local_arg = NULL; }'. */
7338 if (sym->attr.optional)
7340 tree present = fold_build2_loc (input_location, NE_EXPR,
7341 boolean_type_node, cfi_desc,
7342 null_pointer_node);
7343 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
7344 sym->backend_decl,
7345 fold_convert (TREE_TYPE (sym->backend_decl),
7346 null_pointer_node));
7347 tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block), tmp);
7348 gfc_add_expr_to_block (init, tmp);
7350 else
7351 gfc_add_block_to_block (init, &block);
7353 if (!sym->attr.referenced)
7354 return;
7356 /* If pointer not changed, nothing to be done (except copy out) */
7357 if (!do_copy_inout && ((!sym->attr.pointer && !sym->attr.allocatable)
7358 || sym->attr.intent == INTENT_IN))
7359 return;
7361 gfc_init_block (&block);
7363 /* For bind(C), Fortran does not permit mixing 'pointer' with 'contiguous' (or
7364 len=*). Thus, when copy out is needed, the bounds ofthe descriptor remain
7365 unchanged. */
7366 if (do_copy_inout)
7368 tree data, call;
7369 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
7370 data = gfc_conv_descriptor_data_get (gfc_desc);
7371 else if (!POINTER_TYPE_P (TREE_TYPE (gfc_desc)))
7372 data = gfc_build_addr_expr (NULL, gfc_desc);
7373 else
7374 data = gfc_desc;
7375 gfc_init_block (&block2);
7376 if (sym->attr.intent != INTENT_IN)
7378 /* First, create the inner copy-out loop.
7379 for (idx = 0; idx < size; ++idx)
7381 shift = 0;
7382 tmpidx = idx
7383 for (dim = 0; dim < rank; ++dim)
7385 shift += (tmpidx % extent[d]) * sm[d]
7386 tmpidx = tmpidx / extend[d]
7388 memcpy (lhs + shift, rhs + idx*elem_len, elem_len)
7389 } .*/
7390 stmtblock_t loop_body;
7391 idx = gfc_create_var (size_type_node, "arrayidx");
7392 gfc_init_block (&loop_body);
7393 tree shift = gfc_create_var (size_type_node, "shift");
7394 tree tmpidx = gfc_create_var (size_type_node, "tmpidx");
7395 gfc_add_modify (&loop_body, shift,
7396 build_zero_cst (TREE_TYPE (shift)));
7397 gfc_add_modify (&loop_body, tmpidx, idx);
7398 stmtblock_t inner_loop;
7399 gfc_init_block (&inner_loop);
7400 tree dim = gfc_create_var (TREE_TYPE (rank), "dim");
7401 /* shift += (tmpidx % extent[d]) * sm[d] */
7402 tmp = fold_convert (size_type_node,
7403 gfc_get_cfi_dim_extent (cfi, dim));
7404 tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR,
7405 size_type_node, tmpidx, tmp);
7406 tmp = fold_build2_loc (input_location, MULT_EXPR,
7407 size_type_node, tmp,
7408 fold_convert (size_type_node,
7409 gfc_get_cfi_dim_sm (cfi, dim)));
7410 gfc_add_modify (&inner_loop, shift,
7411 fold_build2_loc (input_location, PLUS_EXPR,
7412 size_type_node, shift, tmp));
7413 /* tmpidx = tmpidx / extend[d] */
7414 tmp = fold_convert (size_type_node,
7415 gfc_get_cfi_dim_extent (cfi, dim));
7416 gfc_add_modify (&inner_loop, tmpidx,
7417 fold_build2_loc (input_location, TRUNC_DIV_EXPR,
7418 size_type_node, tmpidx, tmp));
7419 gfc_simple_for_loop (&loop_body, dim,
7420 build_zero_cst (TREE_TYPE (rank)), rank, LT_EXPR,
7421 build_int_cst (TREE_TYPE (dim), 1),
7422 gfc_finish_block (&inner_loop));
7423 /* Assign. */
7424 tree rhs;
7425 tmp = fold_convert (pchar_type_node,
7426 gfc_get_cfi_desc_base_addr (cfi));
7427 tmp = fold_build2 (POINTER_PLUS_EXPR, pchar_type_node, tmp, shift);
7428 /* memcpy (lhs + shift, rhs + idx*elem_len, elem_len) */
7429 tree elem_len;
7430 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
7431 elem_len = gfc_conv_descriptor_elem_len (gfc_desc);
7432 else
7433 elem_len = gfc_get_cfi_desc_elem_len (cfi);
7434 rhs = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
7435 elem_len, idx);
7436 rhs = fold_build2_loc (input_location, POINTER_PLUS_EXPR,
7437 pchar_type_node,
7438 fold_convert (pchar_type_node, data), rhs);
7439 tmp = fold_convert (pvoid_type_node, tmp);
7440 rhs = fold_convert (pvoid_type_node, rhs);
7441 call = builtin_decl_explicit (BUILT_IN_MEMCPY);
7442 call = build_call_expr_loc (input_location, call, 3, tmp, rhs,
7443 elem_len);
7444 gfc_add_expr_to_block (&loop_body,
7445 fold_convert (void_type_node, call));
7446 gfc_simple_for_loop (&block2, idx, build_zero_cst (TREE_TYPE (idx)),
7447 size_var, LT_EXPR,
7448 build_int_cst (TREE_TYPE (idx), 1),
7449 gfc_finish_block (&loop_body));
7451 call = builtin_decl_explicit (BUILT_IN_FREE);
7452 call = build_call_expr_loc (input_location, call, 1, data);
7453 gfc_add_expr_to_block (&block2, call);
7455 /* if (cfi->base_addr != gfc->data) { copy out; free(var) }; return */
7456 tree tmp2 = gfc_get_cfi_desc_base_addr (cfi);
7457 tmp2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7458 tmp2, fold_convert (TREE_TYPE (tmp2), data));
7459 tmp = build3_v (COND_EXPR, tmp2, gfc_finish_block (&block2),
7460 build_empty_stmt (input_location));
7461 gfc_add_expr_to_block (&block, tmp);
7462 goto done_finally;
7465 /* Update pointer + array data data on exit. */
7466 tmp = gfc_get_cfi_desc_base_addr (cfi);
7467 tmp2 = (!sym->attr.dimension
7468 ? gfc_desc : gfc_conv_descriptor_data_get (gfc_desc));
7469 gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), tmp2));
7471 /* Set string length for len=:, only. */
7472 if (sym->ts.type == BT_CHARACTER && !sym->ts.u.cl->length)
7474 tmp2 = gfc_get_cfi_desc_elem_len (cfi);
7475 tmp = fold_convert (TREE_TYPE (tmp2), sym->ts.u.cl->backend_decl);
7476 if (sym->ts.kind != 1)
7477 tmp = fold_build2_loc (input_location, MULT_EXPR,
7478 TREE_TYPE (tmp2), tmp,
7479 build_int_cst (TREE_TYPE (tmp2), sym->ts.kind));
7480 gfc_add_modify (&block, tmp2, tmp);
7483 if (!sym->attr.dimension)
7484 goto done_finally;
7486 gfc_init_block (&block2);
7488 /* Loop: for (i = 0; i < rank; ++i). */
7489 idx = gfc_create_var (TREE_TYPE (rank), "idx");
7491 /* Loop body. */
7492 gfc_init_block (&loop_body);
7493 /* cfi->dim[i].lower_bound = gfc->dim[i].lbound */
7494 gfc_add_modify (&loop_body, gfc_get_cfi_dim_lbound (cfi, idx),
7495 gfc_conv_descriptor_lbound_get (gfc_desc, idx));
7496 /* cfi->dim[i].extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1. */
7497 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
7498 gfc_conv_descriptor_ubound_get (gfc_desc, idx),
7499 gfc_conv_descriptor_lbound_get (gfc_desc, idx));
7500 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type, tmp,
7501 gfc_index_one_node);
7502 gfc_add_modify (&loop_body, gfc_get_cfi_dim_extent (cfi, idx), tmp);
7503 /* d->dim[n].sm = gfc->dim[i].stride * gfc->span); */
7504 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
7505 gfc_conv_descriptor_stride_get (gfc_desc, idx),
7506 gfc_conv_descriptor_span_get (gfc_desc));
7507 gfc_add_modify (&loop_body, gfc_get_cfi_dim_sm (cfi, idx), tmp);
7509 /* Generate loop. */
7510 gfc_simple_for_loop (&block2, idx, build_zero_cst (TREE_TYPE (idx)),
7511 rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
7512 gfc_finish_block (&loop_body));
7513 /* if (gfc->data != NULL) { block2 }. */
7514 tmp = gfc_get_cfi_desc_base_addr (cfi),
7515 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7516 tmp, null_pointer_node);
7517 tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
7518 build_empty_stmt (input_location));
7519 gfc_add_expr_to_block (&block, tmp);
7521 done_finally:
7522 /* If optional arg: 'if (arg) { block } else { local_arg = NULL; }'. */
7523 if (sym->attr.optional)
7525 tree present = fold_build2_loc (input_location, NE_EXPR,
7526 boolean_type_node, cfi_desc,
7527 null_pointer_node);
7528 tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block),
7529 build_empty_stmt (input_location));
7530 gfc_add_expr_to_block (finally, tmp);
7532 else
7533 gfc_add_block_to_block (finally, &block);
7536 /* Generate code for a function. */
7538 void
7539 gfc_generate_function_code (gfc_namespace * ns)
7541 tree fndecl;
7542 tree old_context;
7543 tree decl;
7544 tree tmp;
7545 tree fpstate = NULL_TREE;
7546 stmtblock_t init, cleanup, outer_block;
7547 stmtblock_t body;
7548 gfc_wrapped_block try_block;
7549 tree recurcheckvar = NULL_TREE;
7550 gfc_symbol *sym;
7551 gfc_symbol *previous_procedure_symbol;
7552 int rank, ieee;
7553 bool is_recursive;
7555 sym = ns->proc_name;
7556 previous_procedure_symbol = current_procedure_symbol;
7557 current_procedure_symbol = sym;
7559 /* Initialize sym->tlink so that gfc_trans_deferred_vars does not get
7560 lost or worse. */
7561 sym->tlink = sym;
7563 /* Create the declaration for functions with global scope. */
7564 if (!sym->backend_decl)
7565 gfc_create_function_decl (ns, false);
7567 fndecl = sym->backend_decl;
7568 old_context = current_function_decl;
7570 if (old_context)
7572 push_function_context ();
7573 saved_parent_function_decls = saved_function_decls;
7574 saved_function_decls = NULL_TREE;
7577 trans_function_start (sym);
7578 gfc_current_locus = sym->declared_at;
7580 gfc_init_block (&init);
7581 gfc_init_block (&cleanup);
7582 gfc_init_block (&outer_block);
7584 if (ns->entries && ns->proc_name->ts.type == BT_CHARACTER)
7586 /* Copy length backend_decls to all entry point result
7587 symbols. */
7588 gfc_entry_list *el;
7589 tree backend_decl;
7591 gfc_conv_const_charlen (ns->proc_name->ts.u.cl);
7592 backend_decl = ns->proc_name->result->ts.u.cl->backend_decl;
7593 for (el = ns->entries; el; el = el->next)
7594 el->sym->result->ts.u.cl->backend_decl = backend_decl;
7597 /* Translate COMMON blocks. */
7598 gfc_trans_common (ns);
7600 /* Null the parent fake result declaration if this namespace is
7601 a module function or an external procedures. */
7602 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
7603 || ns->parent == NULL)
7604 parent_fake_result_decl = NULL_TREE;
7606 /* For BIND(C):
7607 - deallocate intent-out allocatable dummy arguments.
7608 - Create GFC variable which will later be populated by convert_CFI_desc */
7609 if (sym->attr.is_bind_c)
7610 for (gfc_formal_arglist *formal = gfc_sym_get_dummy_args (sym);
7611 formal; formal = formal->next)
7613 gfc_symbol *fsym = formal->sym;
7614 if (!is_CFI_desc (fsym, NULL))
7615 continue;
7616 if (!fsym->attr.referenced)
7618 gfc_conv_cfi_to_gfc (&init, &cleanup, fsym->backend_decl,
7619 NULL_TREE, fsym);
7620 continue;
7622 /* Let's now create a local GFI descriptor. Afterwards:
7623 desc is the local descriptor,
7624 desc_p is a pointer to it
7625 and stored in sym->backend_decl
7626 GFC_DECL_SAVED_DESCRIPTOR (desc_p) contains the CFI descriptor
7627 -> PARM_DECL and before sym->backend_decl.
7628 For scalars, decl == decl_p is a pointer variable. */
7629 tree desc_p, desc;
7630 location_t loc = gfc_get_location (&sym->declared_at);
7631 if (fsym->ts.type == BT_CHARACTER && !fsym->ts.u.cl->length)
7632 fsym->ts.u.cl->backend_decl = gfc_create_var (gfc_array_index_type,
7633 fsym->name);
7634 else if (fsym->ts.type == BT_CHARACTER && !fsym->ts.u.cl->backend_decl)
7636 gfc_se se;
7637 gfc_init_se (&se, NULL );
7638 gfc_conv_expr (&se, fsym->ts.u.cl->length);
7639 gfc_add_block_to_block (&init, &se.pre);
7640 fsym->ts.u.cl->backend_decl = se.expr;
7641 gcc_assert(se.post.head == NULL_TREE);
7643 /* Nullify, otherwise gfc_sym_type will return the CFI type. */
7644 tree tmp = fsym->backend_decl;
7645 fsym->backend_decl = NULL;
7646 tree type = gfc_sym_type (fsym);
7647 gcc_assert (POINTER_TYPE_P (type));
7648 if (POINTER_TYPE_P (TREE_TYPE (type)))
7649 /* For instance, allocatable scalars. */
7650 type = TREE_TYPE (type);
7651 if (TREE_CODE (type) == REFERENCE_TYPE)
7652 type = build_pointer_type (TREE_TYPE (type));
7653 desc_p = build_decl (loc, VAR_DECL, get_identifier (fsym->name), type);
7654 if (!fsym->attr.dimension)
7655 desc = desc_p;
7656 else if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (desc_p))))
7658 /* Character(len=*) explicit-size/assumed-size array. */
7659 desc = desc_p;
7660 gfc_build_qualified_array (desc, fsym);
7662 else
7664 tree size = size_in_bytes (TREE_TYPE (TREE_TYPE (desc_p)));
7665 tree call = builtin_decl_explicit (BUILT_IN_ALLOCA);
7666 call = build_call_expr_loc (input_location, call, 1, size);
7667 gfc_add_modify (&outer_block, desc_p,
7668 fold_convert (TREE_TYPE(desc_p), call));
7669 desc = build_fold_indirect_ref_loc (input_location, desc_p);
7671 pushdecl (desc_p);
7672 if (fsym->attr.optional)
7674 gfc_allocate_lang_decl (desc_p);
7675 GFC_DECL_OPTIONAL_ARGUMENT (desc_p) = 1;
7677 fsym->backend_decl = desc_p;
7678 gfc_conv_cfi_to_gfc (&init, &cleanup, tmp, desc, fsym);
7681 gfc_generate_contained_functions (ns);
7683 has_coarray_vars = false;
7684 generate_local_vars (ns);
7686 if (flag_coarray == GFC_FCOARRAY_LIB && has_coarray_vars)
7687 generate_coarray_init (ns);
7689 /* Keep the parent fake result declaration in module functions
7690 or external procedures. */
7691 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
7692 || ns->parent == NULL)
7693 current_fake_result_decl = parent_fake_result_decl;
7694 else
7695 current_fake_result_decl = NULL_TREE;
7697 is_recursive = sym->attr.recursive
7698 || (sym->attr.entry_master
7699 && sym->ns->entries->sym->attr.recursive);
7700 if ((gfc_option.rtcheck & GFC_RTCHECK_RECURSION)
7701 && !is_recursive && !flag_recursive && !sym->attr.artificial)
7703 char * msg;
7705 msg = xasprintf ("Recursive call to nonrecursive procedure '%s'",
7706 sym->name);
7707 recurcheckvar = gfc_create_var (logical_type_node, "is_recursive");
7708 TREE_STATIC (recurcheckvar) = 1;
7709 DECL_INITIAL (recurcheckvar) = logical_false_node;
7710 gfc_add_expr_to_block (&init, recurcheckvar);
7711 gfc_trans_runtime_check (true, false, recurcheckvar, &init,
7712 &sym->declared_at, msg);
7713 gfc_add_modify (&init, recurcheckvar, logical_true_node);
7714 free (msg);
7717 /* Check if an IEEE module is used in the procedure. If so, save
7718 the floating point state. */
7719 ieee = is_ieee_module_used (ns);
7720 if (ieee)
7721 fpstate = gfc_save_fp_state (&init);
7723 /* Now generate the code for the body of this function. */
7724 gfc_init_block (&body);
7726 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
7727 && sym->attr.subroutine)
7729 tree alternate_return;
7730 alternate_return = gfc_get_fake_result_decl (sym, 0);
7731 gfc_add_modify (&body, alternate_return, integer_zero_node);
7734 if (ns->entries)
7736 /* Jump to the correct entry point. */
7737 tmp = gfc_trans_entry_master_switch (ns->entries);
7738 gfc_add_expr_to_block (&body, tmp);
7741 /* If bounds-checking is enabled, generate code to check passed in actual
7742 arguments against the expected dummy argument attributes (e.g. string
7743 lengths). */
7744 if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !sym->attr.is_bind_c)
7745 add_argument_checking (&body, sym);
7747 finish_oacc_declare (ns, sym, false);
7749 tmp = gfc_trans_code (ns->code);
7750 gfc_add_expr_to_block (&body, tmp);
7752 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
7753 || (sym->result && sym->result != sym
7754 && sym->result->ts.type == BT_DERIVED
7755 && sym->result->ts.u.derived->attr.alloc_comp))
7757 bool artificial_result_decl = false;
7758 tree result = get_proc_result (sym);
7759 gfc_symbol *rsym = sym == sym->result ? sym : sym->result;
7761 /* Make sure that a function returning an object with
7762 alloc/pointer_components always has a result, where at least
7763 the allocatable/pointer components are set to zero. */
7764 if (result == NULL_TREE && sym->attr.function
7765 && ((sym->result->ts.type == BT_DERIVED
7766 && (sym->attr.allocatable
7767 || sym->attr.pointer
7768 || sym->result->ts.u.derived->attr.alloc_comp
7769 || sym->result->ts.u.derived->attr.pointer_comp))
7770 || (sym->result->ts.type == BT_CLASS
7771 && (CLASS_DATA (sym)->attr.allocatable
7772 || CLASS_DATA (sym)->attr.class_pointer
7773 || CLASS_DATA (sym->result)->attr.alloc_comp
7774 || CLASS_DATA (sym->result)->attr.pointer_comp))))
7776 artificial_result_decl = true;
7777 result = gfc_get_fake_result_decl (sym, 0);
7780 if (result != NULL_TREE && sym->attr.function && !sym->attr.pointer)
7782 if (sym->attr.allocatable && sym->attr.dimension == 0
7783 && sym->result == sym)
7784 gfc_add_modify (&init, result, fold_convert (TREE_TYPE (result),
7785 null_pointer_node));
7786 else if (sym->ts.type == BT_CLASS
7787 && CLASS_DATA (sym)->attr.allocatable
7788 && CLASS_DATA (sym)->attr.dimension == 0
7789 && sym->result == sym)
7791 tmp = CLASS_DATA (sym)->backend_decl;
7792 tmp = fold_build3_loc (input_location, COMPONENT_REF,
7793 TREE_TYPE (tmp), result, tmp, NULL_TREE);
7794 gfc_add_modify (&init, tmp, fold_convert (TREE_TYPE (tmp),
7795 null_pointer_node));
7797 else if (sym->ts.type == BT_DERIVED
7798 && !sym->attr.allocatable)
7800 gfc_expr *init_exp;
7801 /* Arrays are not initialized using the default initializer of
7802 their elements. Therefore only check if a default
7803 initializer is available when the result is scalar. */
7804 init_exp = rsym->as ? NULL
7805 : gfc_generate_initializer (&rsym->ts, true);
7806 if (init_exp)
7808 tmp = gfc_trans_structure_assign (result, init_exp, 0);
7809 gfc_free_expr (init_exp);
7810 gfc_add_expr_to_block (&init, tmp);
7812 else if (rsym->ts.u.derived->attr.alloc_comp)
7814 rank = rsym->as ? rsym->as->rank : 0;
7815 tmp = gfc_nullify_alloc_comp (rsym->ts.u.derived, result,
7816 rank);
7817 gfc_prepend_expr_to_block (&body, tmp);
7822 if (result == NULL_TREE || artificial_result_decl)
7824 /* TODO: move to the appropriate place in resolve.cc. */
7825 if (warn_return_type > 0 && sym == sym->result)
7826 gfc_warning (OPT_Wreturn_type,
7827 "Return value of function %qs at %L not set",
7828 sym->name, &sym->declared_at);
7829 if (warn_return_type > 0)
7830 suppress_warning (sym->backend_decl);
7832 if (result != NULL_TREE)
7833 gfc_add_expr_to_block (&body, gfc_generate_return ());
7836 /* Reset recursion-check variable. */
7837 if (recurcheckvar != NULL_TREE)
7839 gfc_add_modify (&cleanup, recurcheckvar, logical_false_node);
7840 recurcheckvar = NULL;
7843 /* If IEEE modules are loaded, restore the floating-point state. */
7844 if (ieee)
7845 gfc_restore_fp_state (&cleanup, fpstate);
7847 /* Finish the function body and add init and cleanup code. */
7848 tmp = gfc_finish_block (&body);
7849 /* Add code to create and cleanup arrays. */
7850 gfc_start_wrapped_block (&try_block, tmp);
7851 gfc_trans_deferred_vars (sym, &try_block);
7852 gfc_add_init_cleanup (&try_block, gfc_finish_block (&init),
7853 gfc_finish_block (&cleanup));
7855 /* Add all the decls we created during processing. */
7856 decl = nreverse (saved_function_decls);
7857 while (decl)
7859 tree next;
7861 next = DECL_CHAIN (decl);
7862 DECL_CHAIN (decl) = NULL_TREE;
7863 pushdecl (decl);
7864 decl = next;
7866 saved_function_decls = NULL_TREE;
7868 gfc_add_expr_to_block (&outer_block, gfc_finish_wrapped_block (&try_block));
7869 DECL_SAVED_TREE (fndecl) = gfc_finish_block (&outer_block);
7870 decl = getdecls ();
7872 /* Finish off this function and send it for code generation. */
7873 poplevel (1, 1);
7874 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7876 DECL_SAVED_TREE (fndecl)
7877 = fold_build3_loc (DECL_SOURCE_LOCATION (fndecl), BIND_EXPR, void_type_node,
7878 decl, DECL_SAVED_TREE (fndecl), DECL_INITIAL (fndecl));
7880 /* Output the GENERIC tree. */
7881 dump_function (TDI_original, fndecl);
7883 /* Store the end of the function, so that we get good line number
7884 info for the epilogue. */
7885 cfun->function_end_locus = input_location;
7887 /* We're leaving the context of this function, so zap cfun.
7888 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
7889 tree_rest_of_compilation. */
7890 set_cfun (NULL);
7892 if (old_context)
7894 pop_function_context ();
7895 saved_function_decls = saved_parent_function_decls;
7897 current_function_decl = old_context;
7899 if (decl_function_context (fndecl))
7901 /* Register this function with cgraph just far enough to get it
7902 added to our parent's nested function list.
7903 If there are static coarrays in this function, the nested _caf_init
7904 function has already called cgraph_create_node, which also created
7905 the cgraph node for this function. */
7906 if (!has_coarray_vars || flag_coarray != GFC_FCOARRAY_LIB)
7907 (void) cgraph_node::get_create (fndecl);
7909 else
7910 cgraph_node::finalize_function (fndecl, true);
7912 gfc_trans_use_stmts (ns);
7913 gfc_traverse_ns (ns, gfc_emit_parameter_debug_info);
7915 if (sym->attr.is_main_program)
7916 create_main_function (fndecl);
7918 current_procedure_symbol = previous_procedure_symbol;
7922 void
7923 gfc_generate_constructors (void)
7925 gcc_assert (gfc_static_ctors == NULL_TREE);
7926 #if 0
7927 tree fnname;
7928 tree type;
7929 tree fndecl;
7930 tree decl;
7931 tree tmp;
7933 if (gfc_static_ctors == NULL_TREE)
7934 return;
7936 fnname = get_file_function_name ("I");
7937 type = build_function_type_list (void_type_node, NULL_TREE);
7939 fndecl = build_decl (input_location,
7940 FUNCTION_DECL, fnname, type);
7941 TREE_PUBLIC (fndecl) = 1;
7943 decl = build_decl (input_location,
7944 RESULT_DECL, NULL_TREE, void_type_node);
7945 DECL_ARTIFICIAL (decl) = 1;
7946 DECL_IGNORED_P (decl) = 1;
7947 DECL_CONTEXT (decl) = fndecl;
7948 DECL_RESULT (fndecl) = decl;
7950 pushdecl (fndecl);
7952 current_function_decl = fndecl;
7954 rest_of_decl_compilation (fndecl, 1, 0);
7956 make_decl_rtl (fndecl);
7958 allocate_struct_function (fndecl, false);
7960 pushlevel ();
7962 for (; gfc_static_ctors; gfc_static_ctors = TREE_CHAIN (gfc_static_ctors))
7964 tmp = build_call_expr_loc (input_location,
7965 TREE_VALUE (gfc_static_ctors), 0);
7966 DECL_SAVED_TREE (fndecl) = build_stmt (input_location, EXPR_STMT, tmp);
7969 decl = getdecls ();
7970 poplevel (1, 1);
7972 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7973 DECL_SAVED_TREE (fndecl)
7974 = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (fndecl),
7975 DECL_INITIAL (fndecl));
7977 free_after_parsing (cfun);
7978 free_after_compilation (cfun);
7980 tree_rest_of_compilation (fndecl);
7982 current_function_decl = NULL_TREE;
7983 #endif
7986 /* Translates a BLOCK DATA program unit. This means emitting the
7987 commons contained therein plus their initializations. We also emit
7988 a globally visible symbol to make sure that each BLOCK DATA program
7989 unit remains unique. */
7991 void
7992 gfc_generate_block_data (gfc_namespace * ns)
7994 tree decl;
7995 tree id;
7997 /* Tell the backend the source location of the block data. */
7998 if (ns->proc_name)
7999 gfc_set_backend_locus (&ns->proc_name->declared_at);
8000 else
8001 gfc_set_backend_locus (&gfc_current_locus);
8003 /* Process the DATA statements. */
8004 gfc_trans_common (ns);
8006 /* Create a global symbol with the mane of the block data. This is to
8007 generate linker errors if the same name is used twice. It is never
8008 really used. */
8009 if (ns->proc_name)
8010 id = gfc_sym_mangled_function_id (ns->proc_name);
8011 else
8012 id = get_identifier ("__BLOCK_DATA__");
8014 decl = build_decl (input_location,
8015 VAR_DECL, id, gfc_array_index_type);
8016 TREE_PUBLIC (decl) = 1;
8017 TREE_STATIC (decl) = 1;
8018 DECL_IGNORED_P (decl) = 1;
8020 pushdecl (decl);
8021 rest_of_decl_compilation (decl, 1, 0);
8025 /* Process the local variables of a BLOCK construct. */
8027 void
8028 gfc_process_block_locals (gfc_namespace* ns)
8030 tree decl;
8032 saved_local_decls = NULL_TREE;
8033 has_coarray_vars = false;
8035 generate_local_vars (ns);
8037 if (flag_coarray == GFC_FCOARRAY_LIB && has_coarray_vars)
8038 generate_coarray_init (ns);
8040 decl = nreverse (saved_local_decls);
8041 while (decl)
8043 tree next;
8045 next = DECL_CHAIN (decl);
8046 DECL_CHAIN (decl) = NULL_TREE;
8047 pushdecl (decl);
8048 decl = next;
8050 saved_local_decls = NULL_TREE;
8054 #include "gt-fortran-trans-decl.h"