[RS6000] PowerPC64 soft-float
[official-gcc.git] / gcc / fortran / trans-decl.c
blob83cbcd123fe8579cf987d17d4d37a02545b28947
1 /* Backend function setup
2 Copyright (C) 2002-2018 Free Software Foundation, Inc.
3 Contributed by Paul Brook
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
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.c -- 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"
50 #define MAX_LABEL_VALUE 99999
53 /* Holds the result of the function if no result variable specified. */
55 static GTY(()) tree current_fake_result_decl;
56 static GTY(()) tree parent_fake_result_decl;
59 /* Holds the variable DECLs for the current function. */
61 static GTY(()) tree saved_function_decls;
62 static GTY(()) tree saved_parent_function_decls;
64 /* Holds the variable DECLs that are locals. */
66 static GTY(()) tree saved_local_decls;
68 /* The namespace of the module we're currently generating. Only used while
69 outputting decls for module variables. Do not rely on this being set. */
71 static gfc_namespace *module_namespace;
73 /* The currently processed procedure symbol. */
74 static gfc_symbol* current_procedure_symbol = NULL;
76 /* The currently processed module. */
77 static struct module_htab_entry *cur_module;
79 /* With -fcoarray=lib: For generating the registering call
80 of static coarrays. */
81 static bool has_coarray_vars;
82 static stmtblock_t caf_init_block;
85 /* List of static constructor functions. */
87 tree gfc_static_ctors;
90 /* Whether we've seen a symbol from an IEEE module in the namespace. */
91 static int seen_ieee_symbol;
93 /* Function declarations for builtin library functions. */
95 tree gfor_fndecl_pause_numeric;
96 tree gfor_fndecl_pause_string;
97 tree gfor_fndecl_stop_numeric;
98 tree gfor_fndecl_stop_string;
99 tree gfor_fndecl_error_stop_numeric;
100 tree gfor_fndecl_error_stop_string;
101 tree gfor_fndecl_runtime_error;
102 tree gfor_fndecl_runtime_error_at;
103 tree gfor_fndecl_runtime_warning_at;
104 tree gfor_fndecl_os_error;
105 tree gfor_fndecl_generate_error;
106 tree gfor_fndecl_set_args;
107 tree gfor_fndecl_set_fpe;
108 tree gfor_fndecl_set_options;
109 tree gfor_fndecl_set_convert;
110 tree gfor_fndecl_set_record_marker;
111 tree gfor_fndecl_set_max_subrecord_length;
112 tree gfor_fndecl_ctime;
113 tree gfor_fndecl_fdate;
114 tree gfor_fndecl_ttynam;
115 tree gfor_fndecl_in_pack;
116 tree gfor_fndecl_in_unpack;
117 tree gfor_fndecl_associated;
118 tree gfor_fndecl_system_clock4;
119 tree gfor_fndecl_system_clock8;
120 tree gfor_fndecl_ieee_procedure_entry;
121 tree gfor_fndecl_ieee_procedure_exit;
123 /* Coarray run-time library function decls. */
124 tree gfor_fndecl_caf_init;
125 tree gfor_fndecl_caf_finalize;
126 tree gfor_fndecl_caf_this_image;
127 tree gfor_fndecl_caf_num_images;
128 tree gfor_fndecl_caf_register;
129 tree gfor_fndecl_caf_deregister;
130 tree gfor_fndecl_caf_get;
131 tree gfor_fndecl_caf_send;
132 tree gfor_fndecl_caf_sendget;
133 tree gfor_fndecl_caf_get_by_ref;
134 tree gfor_fndecl_caf_send_by_ref;
135 tree gfor_fndecl_caf_sendget_by_ref;
136 tree gfor_fndecl_caf_sync_all;
137 tree gfor_fndecl_caf_sync_memory;
138 tree gfor_fndecl_caf_sync_images;
139 tree gfor_fndecl_caf_stop_str;
140 tree gfor_fndecl_caf_stop_numeric;
141 tree gfor_fndecl_caf_error_stop;
142 tree gfor_fndecl_caf_error_stop_str;
143 tree gfor_fndecl_caf_atomic_def;
144 tree gfor_fndecl_caf_atomic_ref;
145 tree gfor_fndecl_caf_atomic_cas;
146 tree gfor_fndecl_caf_atomic_op;
147 tree gfor_fndecl_caf_lock;
148 tree gfor_fndecl_caf_unlock;
149 tree gfor_fndecl_caf_event_post;
150 tree gfor_fndecl_caf_event_wait;
151 tree gfor_fndecl_caf_event_query;
152 tree gfor_fndecl_caf_fail_image;
153 tree gfor_fndecl_caf_failed_images;
154 tree gfor_fndecl_caf_image_status;
155 tree gfor_fndecl_caf_stopped_images;
156 tree gfor_fndecl_caf_form_team;
157 tree gfor_fndecl_caf_change_team;
158 tree gfor_fndecl_caf_end_team;
159 tree gfor_fndecl_caf_sync_team;
160 tree gfor_fndecl_caf_get_team;
161 tree gfor_fndecl_caf_team_number;
162 tree gfor_fndecl_co_broadcast;
163 tree gfor_fndecl_co_max;
164 tree gfor_fndecl_co_min;
165 tree gfor_fndecl_co_reduce;
166 tree gfor_fndecl_co_sum;
167 tree gfor_fndecl_caf_is_present;
170 /* Math functions. Many other math functions are handled in
171 trans-intrinsic.c. */
173 gfc_powdecl_list gfor_fndecl_math_powi[4][3];
174 tree gfor_fndecl_math_ishftc4;
175 tree gfor_fndecl_math_ishftc8;
176 tree gfor_fndecl_math_ishftc16;
179 /* String functions. */
181 tree gfor_fndecl_compare_string;
182 tree gfor_fndecl_concat_string;
183 tree gfor_fndecl_string_len_trim;
184 tree gfor_fndecl_string_index;
185 tree gfor_fndecl_string_scan;
186 tree gfor_fndecl_string_verify;
187 tree gfor_fndecl_string_trim;
188 tree gfor_fndecl_string_minmax;
189 tree gfor_fndecl_adjustl;
190 tree gfor_fndecl_adjustr;
191 tree gfor_fndecl_select_string;
192 tree gfor_fndecl_compare_string_char4;
193 tree gfor_fndecl_concat_string_char4;
194 tree gfor_fndecl_string_len_trim_char4;
195 tree gfor_fndecl_string_index_char4;
196 tree gfor_fndecl_string_scan_char4;
197 tree gfor_fndecl_string_verify_char4;
198 tree gfor_fndecl_string_trim_char4;
199 tree gfor_fndecl_string_minmax_char4;
200 tree gfor_fndecl_adjustl_char4;
201 tree gfor_fndecl_adjustr_char4;
202 tree gfor_fndecl_select_string_char4;
205 /* Conversion between character kinds. */
206 tree gfor_fndecl_convert_char1_to_char4;
207 tree gfor_fndecl_convert_char4_to_char1;
210 /* Other misc. runtime library functions. */
211 tree gfor_fndecl_size0;
212 tree gfor_fndecl_size1;
213 tree gfor_fndecl_iargc;
214 tree gfor_fndecl_kill;
215 tree gfor_fndecl_kill_sub;
218 /* Intrinsic functions implemented in Fortran. */
219 tree gfor_fndecl_sc_kind;
220 tree gfor_fndecl_si_kind;
221 tree gfor_fndecl_sr_kind;
223 /* BLAS gemm functions. */
224 tree gfor_fndecl_sgemm;
225 tree gfor_fndecl_dgemm;
226 tree gfor_fndecl_cgemm;
227 tree gfor_fndecl_zgemm;
229 /* RANDOM_INIT function. */
230 tree gfor_fndecl_random_init;
232 static void
233 gfc_add_decl_to_parent_function (tree decl)
235 gcc_assert (decl);
236 DECL_CONTEXT (decl) = DECL_CONTEXT (current_function_decl);
237 DECL_NONLOCAL (decl) = 1;
238 DECL_CHAIN (decl) = saved_parent_function_decls;
239 saved_parent_function_decls = decl;
242 void
243 gfc_add_decl_to_function (tree decl)
245 gcc_assert (decl);
246 TREE_USED (decl) = 1;
247 DECL_CONTEXT (decl) = current_function_decl;
248 DECL_CHAIN (decl) = saved_function_decls;
249 saved_function_decls = decl;
252 static void
253 add_decl_as_local (tree decl)
255 gcc_assert (decl);
256 TREE_USED (decl) = 1;
257 DECL_CONTEXT (decl) = current_function_decl;
258 DECL_CHAIN (decl) = saved_local_decls;
259 saved_local_decls = decl;
263 /* Build a backend label declaration. Set TREE_USED for named labels.
264 The context of the label is always the current_function_decl. All
265 labels are marked artificial. */
267 tree
268 gfc_build_label_decl (tree label_id)
270 /* 2^32 temporaries should be enough. */
271 static unsigned int tmp_num = 1;
272 tree label_decl;
273 char *label_name;
275 if (label_id == NULL_TREE)
277 /* Build an internal label name. */
278 ASM_FORMAT_PRIVATE_NAME (label_name, "L", tmp_num++);
279 label_id = get_identifier (label_name);
281 else
282 label_name = NULL;
284 /* Build the LABEL_DECL node. Labels have no type. */
285 label_decl = build_decl (input_location,
286 LABEL_DECL, label_id, void_type_node);
287 DECL_CONTEXT (label_decl) = current_function_decl;
288 SET_DECL_MODE (label_decl, VOIDmode);
290 /* We always define the label as used, even if the original source
291 file never references the label. We don't want all kinds of
292 spurious warnings for old-style Fortran code with too many
293 labels. */
294 TREE_USED (label_decl) = 1;
296 DECL_ARTIFICIAL (label_decl) = 1;
297 return label_decl;
301 /* Set the backend source location of a decl. */
303 void
304 gfc_set_decl_location (tree decl, locus * loc)
306 DECL_SOURCE_LOCATION (decl) = loc->lb->location;
310 /* Return the backend label declaration for a given label structure,
311 or create it if it doesn't exist yet. */
313 tree
314 gfc_get_label_decl (gfc_st_label * lp)
316 if (lp->backend_decl)
317 return lp->backend_decl;
318 else
320 char label_name[GFC_MAX_SYMBOL_LEN + 1];
321 tree label_decl;
323 /* Validate the label declaration from the front end. */
324 gcc_assert (lp != NULL && lp->value <= MAX_LABEL_VALUE);
326 /* Build a mangled name for the label. */
327 sprintf (label_name, "__label_%.6d", lp->value);
329 /* Build the LABEL_DECL node. */
330 label_decl = gfc_build_label_decl (get_identifier (label_name));
332 /* Tell the debugger where the label came from. */
333 if (lp->value <= MAX_LABEL_VALUE) /* An internal label. */
334 gfc_set_decl_location (label_decl, &lp->where);
335 else
336 DECL_ARTIFICIAL (label_decl) = 1;
338 /* Store the label in the label list and return the LABEL_DECL. */
339 lp->backend_decl = label_decl;
340 return label_decl;
345 /* Convert a gfc_symbol to an identifier of the same name. */
347 static tree
348 gfc_sym_identifier (gfc_symbol * sym)
350 if (sym->attr.is_main_program && strcmp (sym->name, "main") == 0)
351 return (get_identifier ("MAIN__"));
352 else
353 return (get_identifier (sym->name));
357 /* Construct mangled name from symbol name. */
359 static tree
360 gfc_sym_mangled_identifier (gfc_symbol * sym)
362 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
364 /* Prevent the mangling of identifiers that have an assigned
365 binding label (mainly those that are bind(c)). */
366 if (sym->attr.is_bind_c == 1 && sym->binding_label)
367 return get_identifier (sym->binding_label);
369 if (!sym->fn_result_spec)
371 if (sym->module == NULL)
372 return gfc_sym_identifier (sym);
373 else
375 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
376 return get_identifier (name);
379 else
381 /* This is an entity that is actually local to a module procedure
382 that appears in the result specification expression. Since
383 sym->module will be a zero length string, we use ns->proc_name
384 instead. */
385 if (sym->ns->proc_name && sym->ns->proc_name->module)
387 snprintf (name, sizeof name, "__%s_MOD__%s_PROC_%s",
388 sym->ns->proc_name->module,
389 sym->ns->proc_name->name,
390 sym->name);
391 return get_identifier (name);
393 else
395 snprintf (name, sizeof name, "__%s_PROC_%s",
396 sym->ns->proc_name->name, sym->name);
397 return get_identifier (name);
403 /* Construct mangled function name from symbol name. */
405 static tree
406 gfc_sym_mangled_function_id (gfc_symbol * sym)
408 int has_underscore;
409 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
411 /* It may be possible to simply use the binding label if it's
412 provided, and remove the other checks. Then we could use it
413 for other things if we wished. */
414 if ((sym->attr.is_bind_c == 1 || sym->attr.is_iso_c == 1) &&
415 sym->binding_label)
416 /* use the binding label rather than the mangled name */
417 return get_identifier (sym->binding_label);
419 if ((sym->module == NULL || sym->attr.proc == PROC_EXTERNAL
420 || (sym->module != NULL && (sym->attr.external
421 || sym->attr.if_source == IFSRC_IFBODY)))
422 && !sym->attr.module_procedure)
424 /* Main program is mangled into MAIN__. */
425 if (sym->attr.is_main_program)
426 return get_identifier ("MAIN__");
428 /* Intrinsic procedures are never mangled. */
429 if (sym->attr.proc == PROC_INTRINSIC)
430 return get_identifier (sym->name);
432 if (flag_underscoring)
434 has_underscore = strchr (sym->name, '_') != 0;
435 if (flag_second_underscore && has_underscore)
436 snprintf (name, sizeof name, "%s__", sym->name);
437 else
438 snprintf (name, sizeof name, "%s_", sym->name);
439 return get_identifier (name);
441 else
442 return get_identifier (sym->name);
444 else
446 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
447 return get_identifier (name);
452 void
453 gfc_set_decl_assembler_name (tree decl, tree name)
455 tree target_mangled = targetm.mangle_decl_assembler_name (decl, name);
456 SET_DECL_ASSEMBLER_NAME (decl, target_mangled);
460 /* Returns true if a variable of specified size should go on the stack. */
463 gfc_can_put_var_on_stack (tree size)
465 unsigned HOST_WIDE_INT low;
467 if (!INTEGER_CST_P (size))
468 return 0;
470 if (flag_max_stack_var_size < 0)
471 return 1;
473 if (!tree_fits_uhwi_p (size))
474 return 0;
476 low = TREE_INT_CST_LOW (size);
477 if (low > (unsigned HOST_WIDE_INT) flag_max_stack_var_size)
478 return 0;
480 /* TODO: Set a per-function stack size limit. */
482 return 1;
486 /* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
487 an expression involving its corresponding pointer. There are
488 2 cases; one for variable size arrays, and one for everything else,
489 because variable-sized arrays require one fewer level of
490 indirection. */
492 static void
493 gfc_finish_cray_pointee (tree decl, gfc_symbol *sym)
495 tree ptr_decl = gfc_get_symbol_decl (sym->cp_pointer);
496 tree value;
498 /* Parameters need to be dereferenced. */
499 if (sym->cp_pointer->attr.dummy)
500 ptr_decl = build_fold_indirect_ref_loc (input_location,
501 ptr_decl);
503 /* Check to see if we're dealing with a variable-sized array. */
504 if (sym->attr.dimension
505 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
507 /* These decls will be dereferenced later, so we don't dereference
508 them here. */
509 value = convert (TREE_TYPE (decl), ptr_decl);
511 else
513 ptr_decl = convert (build_pointer_type (TREE_TYPE (decl)),
514 ptr_decl);
515 value = build_fold_indirect_ref_loc (input_location,
516 ptr_decl);
519 SET_DECL_VALUE_EXPR (decl, value);
520 DECL_HAS_VALUE_EXPR_P (decl) = 1;
521 GFC_DECL_CRAY_POINTEE (decl) = 1;
525 /* Finish processing of a declaration without an initial value. */
527 static void
528 gfc_finish_decl (tree decl)
530 gcc_assert (TREE_CODE (decl) == PARM_DECL
531 || DECL_INITIAL (decl) == NULL_TREE);
533 if (!VAR_P (decl))
534 return;
536 if (DECL_SIZE (decl) == NULL_TREE
537 && TYPE_SIZE (TREE_TYPE (decl)) != NULL_TREE)
538 layout_decl (decl, 0);
540 /* A few consistency checks. */
541 /* A static variable with an incomplete type is an error if it is
542 initialized. Also if it is not file scope. Otherwise, let it
543 through, but if it is not `extern' then it may cause an error
544 message later. */
545 /* An automatic variable with an incomplete type is an error. */
547 /* We should know the storage size. */
548 gcc_assert (DECL_SIZE (decl) != NULL_TREE
549 || (TREE_STATIC (decl)
550 ? (!DECL_INITIAL (decl) || !DECL_CONTEXT (decl))
551 : DECL_EXTERNAL (decl)));
553 /* The storage size should be constant. */
554 gcc_assert ((!DECL_EXTERNAL (decl) && !TREE_STATIC (decl))
555 || !DECL_SIZE (decl)
556 || TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST);
560 /* Handle setting of GFC_DECL_SCALAR* on DECL. */
562 void
563 gfc_finish_decl_attrs (tree decl, symbol_attribute *attr)
565 if (!attr->dimension && !attr->codimension)
567 /* Handle scalar allocatable variables. */
568 if (attr->allocatable)
570 gfc_allocate_lang_decl (decl);
571 GFC_DECL_SCALAR_ALLOCATABLE (decl) = 1;
573 /* Handle scalar pointer variables. */
574 if (attr->pointer)
576 gfc_allocate_lang_decl (decl);
577 GFC_DECL_SCALAR_POINTER (decl) = 1;
583 /* Apply symbol attributes to a variable, and add it to the function scope. */
585 static void
586 gfc_finish_var_decl (tree decl, gfc_symbol * sym)
588 tree new_type;
590 /* Set DECL_VALUE_EXPR for Cray Pointees. */
591 if (sym->attr.cray_pointee)
592 gfc_finish_cray_pointee (decl, sym);
594 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
595 This is the equivalent of the TARGET variables.
596 We also need to set this if the variable is passed by reference in a
597 CALL statement. */
598 if (sym->attr.target)
599 TREE_ADDRESSABLE (decl) = 1;
601 /* If it wasn't used we wouldn't be getting it. */
602 TREE_USED (decl) = 1;
604 if (sym->attr.flavor == FL_PARAMETER
605 && (sym->attr.dimension || sym->ts.type == BT_DERIVED))
606 TREE_READONLY (decl) = 1;
608 /* Chain this decl to the pending declarations. Don't do pushdecl()
609 because this would add them to the current scope rather than the
610 function scope. */
611 if (current_function_decl != NULL_TREE)
613 if (sym->ns->proc_name
614 && (sym->ns->proc_name->backend_decl == current_function_decl
615 || sym->result == sym))
616 gfc_add_decl_to_function (decl);
617 else if (sym->ns->proc_name
618 && sym->ns->proc_name->attr.flavor == FL_LABEL)
619 /* This is a BLOCK construct. */
620 add_decl_as_local (decl);
621 else
622 gfc_add_decl_to_parent_function (decl);
625 if (sym->attr.cray_pointee)
626 return;
628 if(sym->attr.is_bind_c == 1 && sym->binding_label)
630 /* We need to put variables that are bind(c) into the common
631 segment of the object file, because this is what C would do.
632 gfortran would typically put them in either the BSS or
633 initialized data segments, and only mark them as common if
634 they were part of common blocks. However, if they are not put
635 into common space, then C cannot initialize global Fortran
636 variables that it interoperates with and the draft says that
637 either Fortran or C should be able to initialize it (but not
638 both, of course.) (J3/04-007, section 15.3). */
639 TREE_PUBLIC(decl) = 1;
640 DECL_COMMON(decl) = 1;
641 if (sym->attr.access == ACCESS_PRIVATE && !sym->attr.public_used)
643 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
644 DECL_VISIBILITY_SPECIFIED (decl) = true;
648 /* If a variable is USE associated, it's always external. */
649 if (sym->attr.use_assoc || sym->attr.used_in_submodule)
651 DECL_EXTERNAL (decl) = 1;
652 TREE_PUBLIC (decl) = 1;
654 else if (sym->fn_result_spec && !sym->ns->proc_name->module)
657 if (sym->ns->proc_name->attr.if_source != IFSRC_DECL)
658 DECL_EXTERNAL (decl) = 1;
659 else
660 TREE_STATIC (decl) = 1;
662 TREE_PUBLIC (decl) = 1;
664 else if (sym->module && !sym->attr.result && !sym->attr.dummy)
666 /* TODO: Don't set sym->module for result or dummy variables. */
667 gcc_assert (current_function_decl == NULL_TREE || sym->result == sym);
669 TREE_PUBLIC (decl) = 1;
670 TREE_STATIC (decl) = 1;
671 if (sym->attr.access == ACCESS_PRIVATE && !sym->attr.public_used)
673 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
674 DECL_VISIBILITY_SPECIFIED (decl) = true;
678 /* Derived types are a bit peculiar because of the possibility of
679 a default initializer; this must be applied each time the variable
680 comes into scope it therefore need not be static. These variables
681 are SAVE_NONE but have an initializer. Otherwise explicitly
682 initialized variables are SAVE_IMPLICIT and explicitly saved are
683 SAVE_EXPLICIT. */
684 if (!sym->attr.use_assoc
685 && (sym->attr.save != SAVE_NONE || sym->attr.data
686 || (sym->value && sym->ns->proc_name->attr.is_main_program)
687 || (flag_coarray == GFC_FCOARRAY_LIB
688 && sym->attr.codimension && !sym->attr.allocatable)))
689 TREE_STATIC (decl) = 1;
691 /* If derived-type variables with DTIO procedures are not made static
692 some bits of code referencing them get optimized away.
693 TODO Understand why this is so and fix it. */
694 if (!sym->attr.use_assoc
695 && ((sym->ts.type == BT_DERIVED
696 && sym->ts.u.derived->attr.has_dtio_procs)
697 || (sym->ts.type == BT_CLASS
698 && CLASS_DATA (sym)->ts.u.derived->attr.has_dtio_procs)))
699 TREE_STATIC (decl) = 1;
701 /* Treat asynchronous variables the same as volatile, for now. */
702 if (sym->attr.volatile_ || sym->attr.asynchronous)
704 TREE_THIS_VOLATILE (decl) = 1;
705 TREE_SIDE_EFFECTS (decl) = 1;
706 new_type = build_qualified_type (TREE_TYPE (decl), TYPE_QUAL_VOLATILE);
707 TREE_TYPE (decl) = new_type;
710 /* Keep variables larger than max-stack-var-size off stack. */
711 if (!(sym->ns->proc_name && sym->ns->proc_name->attr.recursive)
712 && !sym->attr.automatic
713 && INTEGER_CST_P (DECL_SIZE_UNIT (decl))
714 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
715 /* Put variable length auto array pointers always into stack. */
716 && (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
717 || sym->attr.dimension == 0
718 || sym->as->type != AS_EXPLICIT
719 || sym->attr.pointer
720 || sym->attr.allocatable)
721 && !DECL_ARTIFICIAL (decl))
723 TREE_STATIC (decl) = 1;
725 /* Because the size of this variable isn't known until now, we may have
726 greedily added an initializer to this variable (in build_init_assign)
727 even though the max-stack-var-size indicates the variable should be
728 static. Therefore we rip out the automatic initializer here and
729 replace it with a static one. */
730 gfc_symtree *st = gfc_find_symtree (sym->ns->sym_root, sym->name);
731 gfc_code *prev = NULL;
732 gfc_code *code = sym->ns->code;
733 while (code && code->op == EXEC_INIT_ASSIGN)
735 /* Look for an initializer meant for this symbol. */
736 if (code->expr1->symtree == st)
738 if (prev)
739 prev->next = code->next;
740 else
741 sym->ns->code = code->next;
743 break;
746 prev = code;
747 code = code->next;
749 if (code && code->op == EXEC_INIT_ASSIGN)
751 /* Keep the init expression for a static initializer. */
752 sym->value = code->expr2;
753 /* Cleanup the defunct code object, without freeing the init expr. */
754 code->expr2 = NULL;
755 gfc_free_statement (code);
756 free (code);
760 /* Handle threadprivate variables. */
761 if (sym->attr.threadprivate
762 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
763 set_decl_tls_model (decl, decl_default_tls_model (decl));
765 gfc_finish_decl_attrs (decl, &sym->attr);
769 /* Allocate the lang-specific part of a decl. */
771 void
772 gfc_allocate_lang_decl (tree decl)
774 if (DECL_LANG_SPECIFIC (decl) == NULL)
775 DECL_LANG_SPECIFIC (decl) = ggc_cleared_alloc<struct lang_decl> ();
778 /* Remember a symbol to generate initialization/cleanup code at function
779 entry/exit. */
781 static void
782 gfc_defer_symbol_init (gfc_symbol * sym)
784 gfc_symbol *p;
785 gfc_symbol *last;
786 gfc_symbol *head;
788 /* Don't add a symbol twice. */
789 if (sym->tlink)
790 return;
792 last = head = sym->ns->proc_name;
793 p = last->tlink;
795 /* Make sure that setup code for dummy variables which are used in the
796 setup of other variables is generated first. */
797 if (sym->attr.dummy)
799 /* Find the first dummy arg seen after us, or the first non-dummy arg.
800 This is a circular list, so don't go past the head. */
801 while (p != head
802 && (!p->attr.dummy || p->dummy_order > sym->dummy_order))
804 last = p;
805 p = p->tlink;
808 /* Insert in between last and p. */
809 last->tlink = sym;
810 sym->tlink = p;
814 /* Used in gfc_get_symbol_decl and gfc_get_derived_type to obtain the
815 backend_decl for a module symbol, if it all ready exists. If the
816 module gsymbol does not exist, it is created. If the symbol does
817 not exist, it is added to the gsymbol namespace. Returns true if
818 an existing backend_decl is found. */
820 bool
821 gfc_get_module_backend_decl (gfc_symbol *sym)
823 gfc_gsymbol *gsym;
824 gfc_symbol *s;
825 gfc_symtree *st;
827 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->module);
829 if (!gsym || (gsym->ns && gsym->type == GSYM_MODULE))
831 st = NULL;
832 s = NULL;
834 /* Check for a symbol with the same name. */
835 if (gsym)
836 gfc_find_symbol (sym->name, gsym->ns, 0, &s);
838 if (!s)
840 if (!gsym)
842 gsym = gfc_get_gsymbol (sym->module);
843 gsym->type = GSYM_MODULE;
844 gsym->ns = gfc_get_namespace (NULL, 0);
847 st = gfc_new_symtree (&gsym->ns->sym_root, sym->name);
848 st->n.sym = sym;
849 sym->refs++;
851 else if (gfc_fl_struct (sym->attr.flavor))
853 if (s && s->attr.flavor == FL_PROCEDURE)
855 gfc_interface *intr;
856 gcc_assert (s->attr.generic);
857 for (intr = s->generic; intr; intr = intr->next)
858 if (gfc_fl_struct (intr->sym->attr.flavor))
860 s = intr->sym;
861 break;
865 /* Normally we can assume that s is a derived-type symbol since it
866 shares a name with the derived-type sym. However if sym is a
867 STRUCTURE, it may in fact share a name with any other basic type
868 variable. If s is in fact of derived type then we can continue
869 looking for a duplicate type declaration. */
870 if (sym->attr.flavor == FL_STRUCT && s->ts.type == BT_DERIVED)
872 s = s->ts.u.derived;
875 if (gfc_fl_struct (s->attr.flavor) && !s->backend_decl)
877 if (s->attr.flavor == FL_UNION)
878 s->backend_decl = gfc_get_union_type (s);
879 else
880 s->backend_decl = gfc_get_derived_type (s);
882 gfc_copy_dt_decls_ifequal (s, sym, true);
883 return true;
885 else if (s->backend_decl)
887 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
888 gfc_copy_dt_decls_ifequal (s->ts.u.derived, sym->ts.u.derived,
889 true);
890 else if (sym->ts.type == BT_CHARACTER)
891 sym->ts.u.cl->backend_decl = s->ts.u.cl->backend_decl;
892 sym->backend_decl = s->backend_decl;
893 return true;
896 return false;
900 /* Create an array index type variable with function scope. */
902 static tree
903 create_index_var (const char * pfx, int nest)
905 tree decl;
907 decl = gfc_create_var_np (gfc_array_index_type, pfx);
908 if (nest)
909 gfc_add_decl_to_parent_function (decl);
910 else
911 gfc_add_decl_to_function (decl);
912 return decl;
916 /* Create variables to hold all the non-constant bits of info for a
917 descriptorless array. Remember these in the lang-specific part of the
918 type. */
920 static void
921 gfc_build_qualified_array (tree decl, gfc_symbol * sym)
923 tree type;
924 int dim;
925 int nest;
926 gfc_namespace* procns;
927 symbol_attribute *array_attr;
928 gfc_array_spec *as;
929 bool is_classarray = IS_CLASS_ARRAY (sym);
931 type = TREE_TYPE (decl);
932 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
933 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
935 /* We just use the descriptor, if there is one. */
936 if (GFC_DESCRIPTOR_TYPE_P (type))
937 return;
939 gcc_assert (GFC_ARRAY_TYPE_P (type));
940 procns = gfc_find_proc_namespace (sym->ns);
941 nest = (procns->proc_name->backend_decl != current_function_decl)
942 && !sym->attr.contained;
944 if (array_attr->codimension && flag_coarray == GFC_FCOARRAY_LIB
945 && as->type != AS_ASSUMED_SHAPE
946 && GFC_TYPE_ARRAY_CAF_TOKEN (type) == NULL_TREE)
948 tree token;
949 tree token_type = build_qualified_type (pvoid_type_node,
950 TYPE_QUAL_RESTRICT);
952 if (sym->module && (sym->attr.use_assoc
953 || sym->ns->proc_name->attr.flavor == FL_MODULE))
955 tree token_name
956 = get_identifier (gfc_get_string (GFC_PREFIX ("caf_token%s"),
957 IDENTIFIER_POINTER (gfc_sym_mangled_identifier (sym))));
958 token = build_decl (DECL_SOURCE_LOCATION (decl), VAR_DECL, token_name,
959 token_type);
960 if (sym->attr.use_assoc)
961 DECL_EXTERNAL (token) = 1;
962 else
963 TREE_STATIC (token) = 1;
965 TREE_PUBLIC (token) = 1;
967 if (sym->attr.access == ACCESS_PRIVATE && !sym->attr.public_used)
969 DECL_VISIBILITY (token) = VISIBILITY_HIDDEN;
970 DECL_VISIBILITY_SPECIFIED (token) = true;
973 else
975 token = gfc_create_var_np (token_type, "caf_token");
976 TREE_STATIC (token) = 1;
979 GFC_TYPE_ARRAY_CAF_TOKEN (type) = token;
980 DECL_ARTIFICIAL (token) = 1;
981 DECL_NONALIASED (token) = 1;
983 if (sym->module && !sym->attr.use_assoc)
985 pushdecl (token);
986 DECL_CONTEXT (token) = sym->ns->proc_name->backend_decl;
987 gfc_module_add_decl (cur_module, token);
989 else if (sym->attr.host_assoc
990 && TREE_CODE (DECL_CONTEXT (current_function_decl))
991 != TRANSLATION_UNIT_DECL)
992 gfc_add_decl_to_parent_function (token);
993 else
994 gfc_add_decl_to_function (token);
997 for (dim = 0; dim < GFC_TYPE_ARRAY_RANK (type); dim++)
999 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
1001 GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
1002 TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type, dim)) = 1;
1004 /* Don't try to use the unknown bound for assumed shape arrays. */
1005 if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
1006 && (as->type != AS_ASSUMED_SIZE
1007 || dim < GFC_TYPE_ARRAY_RANK (type) - 1))
1009 GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
1010 TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type, dim)) = 1;
1013 if (GFC_TYPE_ARRAY_STRIDE (type, dim) == NULL_TREE)
1015 GFC_TYPE_ARRAY_STRIDE (type, dim) = create_index_var ("stride", nest);
1016 TREE_NO_WARNING (GFC_TYPE_ARRAY_STRIDE (type, dim)) = 1;
1019 for (dim = GFC_TYPE_ARRAY_RANK (type);
1020 dim < GFC_TYPE_ARRAY_RANK (type) + GFC_TYPE_ARRAY_CORANK (type); dim++)
1022 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
1024 GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
1025 TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type, dim)) = 1;
1027 /* Don't try to use the unknown ubound for the last coarray dimension. */
1028 if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
1029 && dim < GFC_TYPE_ARRAY_RANK (type) + GFC_TYPE_ARRAY_CORANK (type) - 1)
1031 GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
1032 TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type, dim)) = 1;
1035 if (GFC_TYPE_ARRAY_OFFSET (type) == NULL_TREE)
1037 GFC_TYPE_ARRAY_OFFSET (type) = gfc_create_var_np (gfc_array_index_type,
1038 "offset");
1039 TREE_NO_WARNING (GFC_TYPE_ARRAY_OFFSET (type)) = 1;
1041 if (nest)
1042 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type));
1043 else
1044 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type));
1047 if (GFC_TYPE_ARRAY_SIZE (type) == NULL_TREE
1048 && as->type != AS_ASSUMED_SIZE)
1050 GFC_TYPE_ARRAY_SIZE (type) = create_index_var ("size", nest);
1051 TREE_NO_WARNING (GFC_TYPE_ARRAY_SIZE (type)) = 1;
1054 if (POINTER_TYPE_P (type))
1056 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type)));
1057 gcc_assert (TYPE_LANG_SPECIFIC (type)
1058 == TYPE_LANG_SPECIFIC (TREE_TYPE (type)));
1059 type = TREE_TYPE (type);
1062 if (! COMPLETE_TYPE_P (type) && GFC_TYPE_ARRAY_SIZE (type))
1064 tree size, range;
1066 size = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
1067 GFC_TYPE_ARRAY_SIZE (type), gfc_index_one_node);
1068 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1069 size);
1070 TYPE_DOMAIN (type) = range;
1071 layout_type (type);
1074 if (TYPE_NAME (type) != NULL_TREE && as->rank > 0
1075 && GFC_TYPE_ARRAY_UBOUND (type, as->rank - 1) != NULL_TREE
1076 && VAR_P (GFC_TYPE_ARRAY_UBOUND (type, as->rank - 1)))
1078 tree gtype = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1080 for (dim = 0; dim < as->rank - 1; dim++)
1082 gcc_assert (TREE_CODE (gtype) == ARRAY_TYPE);
1083 gtype = TREE_TYPE (gtype);
1085 gcc_assert (TREE_CODE (gtype) == ARRAY_TYPE);
1086 if (TYPE_MAX_VALUE (TYPE_DOMAIN (gtype)) == NULL)
1087 TYPE_NAME (type) = NULL_TREE;
1090 if (TYPE_NAME (type) == NULL_TREE)
1092 tree gtype = TREE_TYPE (type), rtype, type_decl;
1094 for (dim = as->rank - 1; dim >= 0; dim--)
1096 tree lbound, ubound;
1097 lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
1098 ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
1099 rtype = build_range_type (gfc_array_index_type, lbound, ubound);
1100 gtype = build_array_type (gtype, rtype);
1101 /* Ensure the bound variables aren't optimized out at -O0.
1102 For -O1 and above they often will be optimized out, but
1103 can be tracked by VTA. Also set DECL_NAMELESS, so that
1104 the artificial lbound.N or ubound.N DECL_NAME doesn't
1105 end up in debug info. */
1106 if (lbound
1107 && VAR_P (lbound)
1108 && DECL_ARTIFICIAL (lbound)
1109 && DECL_IGNORED_P (lbound))
1111 if (DECL_NAME (lbound)
1112 && strstr (IDENTIFIER_POINTER (DECL_NAME (lbound)),
1113 "lbound") != 0)
1114 DECL_NAMELESS (lbound) = 1;
1115 DECL_IGNORED_P (lbound) = 0;
1117 if (ubound
1118 && VAR_P (ubound)
1119 && DECL_ARTIFICIAL (ubound)
1120 && DECL_IGNORED_P (ubound))
1122 if (DECL_NAME (ubound)
1123 && strstr (IDENTIFIER_POINTER (DECL_NAME (ubound)),
1124 "ubound") != 0)
1125 DECL_NAMELESS (ubound) = 1;
1126 DECL_IGNORED_P (ubound) = 0;
1129 TYPE_NAME (type) = type_decl = build_decl (input_location,
1130 TYPE_DECL, NULL, gtype);
1131 DECL_ORIGINAL_TYPE (type_decl) = gtype;
1136 /* For some dummy arguments we don't use the actual argument directly.
1137 Instead we create a local decl and use that. This allows us to perform
1138 initialization, and construct full type information. */
1140 static tree
1141 gfc_build_dummy_array_decl (gfc_symbol * sym, tree dummy)
1143 tree decl;
1144 tree type;
1145 gfc_array_spec *as;
1146 symbol_attribute *array_attr;
1147 char *name;
1148 gfc_packed packed;
1149 int n;
1150 bool known_size;
1151 bool is_classarray = IS_CLASS_ARRAY (sym);
1153 /* Use the array as and attr. */
1154 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
1155 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
1157 /* The dummy is returned for pointer, allocatable or assumed rank arrays.
1158 For class arrays the information if sym is an allocatable or pointer
1159 object needs to be checked explicitly (IS_CLASS_ARRAY can be false for
1160 too many reasons to be of use here). */
1161 if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
1162 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
1163 || array_attr->allocatable
1164 || (as && as->type == AS_ASSUMED_RANK))
1165 return dummy;
1167 /* Add to list of variables if not a fake result variable.
1168 These symbols are set on the symbol only, not on the class component. */
1169 if (sym->attr.result || sym->attr.dummy)
1170 gfc_defer_symbol_init (sym);
1172 /* For a class array the array descriptor is in the _data component, while
1173 for a regular array the TREE_TYPE of the dummy is a pointer to the
1174 descriptor. */
1175 type = TREE_TYPE (is_classarray ? gfc_class_data_get (dummy)
1176 : TREE_TYPE (dummy));
1177 /* type now is the array descriptor w/o any indirection. */
1178 gcc_assert (TREE_CODE (dummy) == PARM_DECL
1179 && POINTER_TYPE_P (TREE_TYPE (dummy)));
1181 /* Do we know the element size? */
1182 known_size = sym->ts.type != BT_CHARACTER
1183 || INTEGER_CST_P (sym->ts.u.cl->backend_decl);
1185 if (known_size && !GFC_DESCRIPTOR_TYPE_P (type))
1187 /* For descriptorless arrays with known element size the actual
1188 argument is sufficient. */
1189 gfc_build_qualified_array (dummy, sym);
1190 return dummy;
1193 if (GFC_DESCRIPTOR_TYPE_P (type))
1195 /* Create a descriptorless array pointer. */
1196 packed = PACKED_NO;
1198 /* Even when -frepack-arrays is used, symbols with TARGET attribute
1199 are not repacked. */
1200 if (!flag_repack_arrays || sym->attr.target)
1202 if (as->type == AS_ASSUMED_SIZE)
1203 packed = PACKED_FULL;
1205 else
1207 if (as->type == AS_EXPLICIT)
1209 packed = PACKED_FULL;
1210 for (n = 0; n < as->rank; n++)
1212 if (!(as->upper[n]
1213 && as->lower[n]
1214 && as->upper[n]->expr_type == EXPR_CONSTANT
1215 && as->lower[n]->expr_type == EXPR_CONSTANT))
1217 packed = PACKED_PARTIAL;
1218 break;
1222 else
1223 packed = PACKED_PARTIAL;
1226 /* For classarrays the element type is required, but
1227 gfc_typenode_for_spec () returns the array descriptor. */
1228 type = is_classarray ? gfc_get_element_type (type)
1229 : gfc_typenode_for_spec (&sym->ts);
1230 type = gfc_get_nodesc_array_type (type, as, packed,
1231 !sym->attr.target);
1233 else
1235 /* We now have an expression for the element size, so create a fully
1236 qualified type. Reset sym->backend decl or this will just return the
1237 old type. */
1238 DECL_ARTIFICIAL (sym->backend_decl) = 1;
1239 sym->backend_decl = NULL_TREE;
1240 type = gfc_sym_type (sym);
1241 packed = PACKED_FULL;
1244 ASM_FORMAT_PRIVATE_NAME (name, IDENTIFIER_POINTER (DECL_NAME (dummy)), 0);
1245 decl = build_decl (input_location,
1246 VAR_DECL, get_identifier (name), type);
1248 DECL_ARTIFICIAL (decl) = 1;
1249 DECL_NAMELESS (decl) = 1;
1250 TREE_PUBLIC (decl) = 0;
1251 TREE_STATIC (decl) = 0;
1252 DECL_EXTERNAL (decl) = 0;
1254 /* Avoid uninitialized warnings for optional dummy arguments. */
1255 if (sym->attr.optional)
1256 TREE_NO_WARNING (decl) = 1;
1258 /* We should never get deferred shape arrays here. We used to because of
1259 frontend bugs. */
1260 gcc_assert (as->type != AS_DEFERRED);
1262 if (packed == PACKED_PARTIAL)
1263 GFC_DECL_PARTIAL_PACKED_ARRAY (decl) = 1;
1264 else if (packed == PACKED_FULL)
1265 GFC_DECL_PACKED_ARRAY (decl) = 1;
1267 gfc_build_qualified_array (decl, sym);
1269 if (DECL_LANG_SPECIFIC (dummy))
1270 DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (dummy);
1271 else
1272 gfc_allocate_lang_decl (decl);
1274 GFC_DECL_SAVED_DESCRIPTOR (decl) = dummy;
1276 if (sym->ns->proc_name->backend_decl == current_function_decl
1277 || sym->attr.contained)
1278 gfc_add_decl_to_function (decl);
1279 else
1280 gfc_add_decl_to_parent_function (decl);
1282 return decl;
1285 /* Return a constant or a variable to use as a string length. Does not
1286 add the decl to the current scope. */
1288 static tree
1289 gfc_create_string_length (gfc_symbol * sym)
1291 gcc_assert (sym->ts.u.cl);
1292 gfc_conv_const_charlen (sym->ts.u.cl);
1294 if (sym->ts.u.cl->backend_decl == NULL_TREE)
1296 tree length;
1297 const char *name;
1299 /* The string length variable shall be in static memory if it is either
1300 explicitly SAVED, a module variable or with -fno-automatic. Only
1301 relevant is "len=:" - otherwise, it is either a constant length or
1302 it is an automatic variable. */
1303 bool static_length = sym->attr.save
1304 || sym->ns->proc_name->attr.flavor == FL_MODULE
1305 || (flag_max_stack_var_size == 0
1306 && sym->ts.deferred && !sym->attr.dummy
1307 && !sym->attr.result && !sym->attr.function);
1309 /* Also prefix the mangled name. We need to call GFC_PREFIX for static
1310 variables as some systems do not support the "." in the assembler name.
1311 For nonstatic variables, the "." does not appear in assembler. */
1312 if (static_length)
1314 if (sym->module)
1315 name = gfc_get_string (GFC_PREFIX ("%s_MOD_%s"), sym->module,
1316 sym->name);
1317 else
1318 name = gfc_get_string (GFC_PREFIX ("%s"), sym->name);
1320 else if (sym->module)
1321 name = gfc_get_string (".__%s_MOD_%s", sym->module, sym->name);
1322 else
1323 name = gfc_get_string (".%s", sym->name);
1325 length = build_decl (input_location,
1326 VAR_DECL, get_identifier (name),
1327 gfc_charlen_type_node);
1328 DECL_ARTIFICIAL (length) = 1;
1329 TREE_USED (length) = 1;
1330 if (sym->ns->proc_name->tlink != NULL)
1331 gfc_defer_symbol_init (sym);
1333 sym->ts.u.cl->backend_decl = length;
1335 if (static_length)
1336 TREE_STATIC (length) = 1;
1338 if (sym->ns->proc_name->attr.flavor == FL_MODULE
1339 && (sym->attr.access != ACCESS_PRIVATE || sym->attr.public_used))
1340 TREE_PUBLIC (length) = 1;
1343 gcc_assert (sym->ts.u.cl->backend_decl != NULL_TREE);
1344 return sym->ts.u.cl->backend_decl;
1347 /* If a variable is assigned a label, we add another two auxiliary
1348 variables. */
1350 static void
1351 gfc_add_assign_aux_vars (gfc_symbol * sym)
1353 tree addr;
1354 tree length;
1355 tree decl;
1357 gcc_assert (sym->backend_decl);
1359 decl = sym->backend_decl;
1360 gfc_allocate_lang_decl (decl);
1361 GFC_DECL_ASSIGN (decl) = 1;
1362 length = build_decl (input_location,
1363 VAR_DECL, create_tmp_var_name (sym->name),
1364 gfc_charlen_type_node);
1365 addr = build_decl (input_location,
1366 VAR_DECL, create_tmp_var_name (sym->name),
1367 pvoid_type_node);
1368 gfc_finish_var_decl (length, sym);
1369 gfc_finish_var_decl (addr, sym);
1370 /* STRING_LENGTH is also used as flag. Less than -1 means that
1371 ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the
1372 target label's address. Otherwise, value is the length of a format string
1373 and ASSIGN_ADDR is its address. */
1374 if (TREE_STATIC (length))
1375 DECL_INITIAL (length) = build_int_cst (gfc_charlen_type_node, -2);
1376 else
1377 gfc_defer_symbol_init (sym);
1379 GFC_DECL_STRING_LEN (decl) = length;
1380 GFC_DECL_ASSIGN_ADDR (decl) = addr;
1384 static tree
1385 add_attributes_to_decl (symbol_attribute sym_attr, tree list)
1387 unsigned id;
1388 tree attr;
1390 for (id = 0; id < EXT_ATTR_NUM; id++)
1391 if (sym_attr.ext_attr & (1 << id))
1393 attr = build_tree_list (
1394 get_identifier (ext_attr_list[id].middle_end_name),
1395 NULL_TREE);
1396 list = chainon (list, attr);
1399 if (sym_attr.omp_declare_target_link)
1400 list = tree_cons (get_identifier ("omp declare target link"),
1401 NULL_TREE, list);
1402 else if (sym_attr.omp_declare_target)
1403 list = tree_cons (get_identifier ("omp declare target"),
1404 NULL_TREE, list);
1406 if (sym_attr.oacc_function)
1408 tree dims = NULL_TREE;
1409 int ix;
1410 int level = sym_attr.oacc_function - 1;
1412 for (ix = GOMP_DIM_MAX; ix--;)
1413 dims = tree_cons (build_int_cst (boolean_type_node, ix >= level),
1414 integer_zero_node, dims);
1416 list = tree_cons (get_identifier ("oacc function"),
1417 dims, list);
1420 return list;
1424 static void build_function_decl (gfc_symbol * sym, bool global);
1427 /* Return the decl for a gfc_symbol, create it if it doesn't already
1428 exist. */
1430 tree
1431 gfc_get_symbol_decl (gfc_symbol * sym)
1433 tree decl;
1434 tree length = NULL_TREE;
1435 tree attributes;
1436 int byref;
1437 bool intrinsic_array_parameter = false;
1438 bool fun_or_res;
1440 gcc_assert (sym->attr.referenced
1441 || sym->attr.flavor == FL_PROCEDURE
1442 || sym->attr.use_assoc
1443 || sym->attr.used_in_submodule
1444 || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY
1445 || (sym->module && sym->attr.if_source != IFSRC_DECL
1446 && sym->backend_decl));
1448 if (sym->ns && sym->ns->proc_name && sym->ns->proc_name->attr.function)
1449 byref = gfc_return_by_reference (sym->ns->proc_name);
1450 else
1451 byref = 0;
1453 /* Make sure that the vtab for the declared type is completed. */
1454 if (sym->ts.type == BT_CLASS)
1456 gfc_component *c = CLASS_DATA (sym);
1457 if (!c->ts.u.derived->backend_decl)
1459 gfc_find_derived_vtab (c->ts.u.derived);
1460 gfc_get_derived_type (sym->ts.u.derived);
1464 /* PDT parameterized array components and string_lengths must have the
1465 'len' parameters substituted for the expressions appearing in the
1466 declaration of the entity and memory allocated/deallocated. */
1467 if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
1468 && sym->param_list != NULL
1469 && !(sym->attr.host_assoc || sym->attr.use_assoc || sym->attr.dummy))
1470 gfc_defer_symbol_init (sym);
1472 /* Dummy PDT 'len' parameters should be checked when they are explicit. */
1473 if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
1474 && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
1475 && sym->param_list != NULL
1476 && sym->attr.dummy)
1477 gfc_defer_symbol_init (sym);
1479 /* All deferred character length procedures need to retain the backend
1480 decl, which is a pointer to the character length in the caller's
1481 namespace and to declare a local character length. */
1482 if (!byref && sym->attr.function
1483 && sym->ts.type == BT_CHARACTER
1484 && sym->ts.deferred
1485 && sym->ts.u.cl->passed_length == NULL
1486 && sym->ts.u.cl->backend_decl
1487 && TREE_CODE (sym->ts.u.cl->backend_decl) == PARM_DECL)
1489 sym->ts.u.cl->passed_length = sym->ts.u.cl->backend_decl;
1490 gcc_assert (POINTER_TYPE_P (TREE_TYPE (sym->ts.u.cl->passed_length)));
1491 sym->ts.u.cl->backend_decl = build_fold_indirect_ref (sym->ts.u.cl->backend_decl);
1494 fun_or_res = byref && (sym->attr.result
1495 || (sym->attr.function && sym->ts.deferred));
1496 if ((sym->attr.dummy && ! sym->attr.function) || fun_or_res)
1498 /* Return via extra parameter. */
1499 if (sym->attr.result && byref
1500 && !sym->backend_decl)
1502 sym->backend_decl =
1503 DECL_ARGUMENTS (sym->ns->proc_name->backend_decl);
1504 /* For entry master function skip over the __entry
1505 argument. */
1506 if (sym->ns->proc_name->attr.entry_master)
1507 sym->backend_decl = DECL_CHAIN (sym->backend_decl);
1510 /* Dummy variables should already have been created. */
1511 gcc_assert (sym->backend_decl);
1513 /* However, the string length of deferred arrays must be set. */
1514 if (sym->ts.type == BT_CHARACTER
1515 && sym->ts.deferred
1516 && sym->attr.dimension
1517 && sym->attr.allocatable)
1518 gfc_defer_symbol_init (sym);
1520 if (sym->attr.pointer && sym->attr.dimension && sym->ts.type != BT_CLASS)
1521 GFC_DECL_PTR_ARRAY_P (sym->backend_decl) = 1;
1523 /* Create a character length variable. */
1524 if (sym->ts.type == BT_CHARACTER)
1526 /* For a deferred dummy, make a new string length variable. */
1527 if (sym->ts.deferred
1529 (sym->ts.u.cl->passed_length == sym->ts.u.cl->backend_decl))
1530 sym->ts.u.cl->backend_decl = NULL_TREE;
1532 if (sym->ts.deferred && byref)
1534 /* The string length of a deferred char array is stored in the
1535 parameter at sym->ts.u.cl->backend_decl as a reference and
1536 marked as a result. Exempt this variable from generating a
1537 temporary for it. */
1538 if (sym->attr.result)
1540 /* We need to insert a indirect ref for param decls. */
1541 if (sym->ts.u.cl->backend_decl
1542 && TREE_CODE (sym->ts.u.cl->backend_decl) == PARM_DECL)
1544 sym->ts.u.cl->passed_length = sym->ts.u.cl->backend_decl;
1545 sym->ts.u.cl->backend_decl =
1546 build_fold_indirect_ref (sym->ts.u.cl->backend_decl);
1549 /* For all other parameters make sure, that they are copied so
1550 that the value and any modifications are local to the routine
1551 by generating a temporary variable. */
1552 else if (sym->attr.function
1553 && sym->ts.u.cl->passed_length == NULL
1554 && sym->ts.u.cl->backend_decl)
1556 sym->ts.u.cl->passed_length = sym->ts.u.cl->backend_decl;
1557 if (POINTER_TYPE_P (TREE_TYPE (sym->ts.u.cl->passed_length)))
1558 sym->ts.u.cl->backend_decl
1559 = build_fold_indirect_ref (sym->ts.u.cl->backend_decl);
1560 else
1561 sym->ts.u.cl->backend_decl = NULL_TREE;
1565 if (sym->ts.u.cl->backend_decl == NULL_TREE)
1566 length = gfc_create_string_length (sym);
1567 else
1568 length = sym->ts.u.cl->backend_decl;
1569 if (VAR_P (length) && DECL_FILE_SCOPE_P (length))
1571 /* Add the string length to the same context as the symbol. */
1572 if (DECL_CONTEXT (sym->backend_decl) == current_function_decl)
1573 gfc_add_decl_to_function (length);
1574 else
1575 gfc_add_decl_to_parent_function (length);
1577 gcc_assert (DECL_CONTEXT (sym->backend_decl) ==
1578 DECL_CONTEXT (length));
1580 gfc_defer_symbol_init (sym);
1584 /* Use a copy of the descriptor for dummy arrays. */
1585 if ((sym->attr.dimension || sym->attr.codimension)
1586 && !TREE_USED (sym->backend_decl))
1588 decl = gfc_build_dummy_array_decl (sym, sym->backend_decl);
1589 /* Prevent the dummy from being detected as unused if it is copied. */
1590 if (sym->backend_decl != NULL && decl != sym->backend_decl)
1591 DECL_ARTIFICIAL (sym->backend_decl) = 1;
1592 sym->backend_decl = decl;
1595 /* Returning the descriptor for dummy class arrays is hazardous, because
1596 some caller is expecting an expression to apply the component refs to.
1597 Therefore the descriptor is only created and stored in
1598 sym->backend_decl's GFC_DECL_SAVED_DESCRIPTOR. The caller is then
1599 responsible to extract it from there, when the descriptor is
1600 desired. */
1601 if (IS_CLASS_ARRAY (sym)
1602 && (!DECL_LANG_SPECIFIC (sym->backend_decl)
1603 || !GFC_DECL_SAVED_DESCRIPTOR (sym->backend_decl)))
1605 decl = gfc_build_dummy_array_decl (sym, sym->backend_decl);
1606 /* Prevent the dummy from being detected as unused if it is copied. */
1607 if (sym->backend_decl != NULL && decl != sym->backend_decl)
1608 DECL_ARTIFICIAL (sym->backend_decl) = 1;
1609 sym->backend_decl = decl;
1612 TREE_USED (sym->backend_decl) = 1;
1613 if (sym->attr.assign && GFC_DECL_ASSIGN (sym->backend_decl) == 0)
1615 gfc_add_assign_aux_vars (sym);
1618 if (sym->ts.type == BT_CLASS && sym->backend_decl)
1619 GFC_DECL_CLASS(sym->backend_decl) = 1;
1621 return sym->backend_decl;
1624 if (sym->backend_decl)
1625 return sym->backend_decl;
1627 /* Special case for array-valued named constants from intrinsic
1628 procedures; those are inlined. */
1629 if (sym->attr.use_assoc && sym->attr.flavor == FL_PARAMETER
1630 && (sym->from_intmod == INTMOD_ISO_FORTRAN_ENV
1631 || sym->from_intmod == INTMOD_ISO_C_BINDING))
1632 intrinsic_array_parameter = true;
1634 /* If use associated compilation, use the module
1635 declaration. */
1636 if ((sym->attr.flavor == FL_VARIABLE
1637 || sym->attr.flavor == FL_PARAMETER)
1638 && (sym->attr.use_assoc || sym->attr.used_in_submodule)
1639 && !intrinsic_array_parameter
1640 && sym->module
1641 && gfc_get_module_backend_decl (sym))
1643 if (sym->ts.type == BT_CLASS && sym->backend_decl)
1644 GFC_DECL_CLASS(sym->backend_decl) = 1;
1645 return sym->backend_decl;
1648 if (sym->attr.flavor == FL_PROCEDURE)
1650 /* Catch functions. Only used for actual parameters,
1651 procedure pointers and procptr initialization targets. */
1652 if (sym->attr.use_assoc
1653 || sym->attr.used_in_submodule
1654 || sym->attr.intrinsic
1655 || sym->attr.if_source != IFSRC_DECL)
1657 decl = gfc_get_extern_function_decl (sym);
1658 gfc_set_decl_location (decl, &sym->declared_at);
1660 else
1662 if (!sym->backend_decl)
1663 build_function_decl (sym, false);
1664 decl = sym->backend_decl;
1666 return decl;
1669 if (sym->attr.intrinsic)
1670 gfc_internal_error ("intrinsic variable which isn't a procedure");
1672 /* Create string length decl first so that they can be used in the
1673 type declaration. For associate names, the target character
1674 length is used. Set 'length' to a constant so that if the
1675 string length is a variable, it is not finished a second time. */
1676 if (sym->ts.type == BT_CHARACTER)
1678 if (sym->attr.associate_var
1679 && sym->ts.deferred
1680 && sym->assoc && sym->assoc->target
1681 && ((sym->assoc->target->expr_type == EXPR_VARIABLE
1682 && sym->assoc->target->symtree->n.sym->ts.type != BT_CHARACTER)
1683 || sym->assoc->target->expr_type != EXPR_VARIABLE))
1684 sym->ts.u.cl->backend_decl = NULL_TREE;
1686 if (sym->attr.associate_var
1687 && sym->ts.u.cl->backend_decl
1688 && (VAR_P (sym->ts.u.cl->backend_decl)
1689 || TREE_CODE (sym->ts.u.cl->backend_decl) == PARM_DECL))
1690 length = gfc_index_zero_node;
1691 else
1692 length = gfc_create_string_length (sym);
1695 /* Create the decl for the variable. */
1696 decl = build_decl (sym->declared_at.lb->location,
1697 VAR_DECL, gfc_sym_identifier (sym), gfc_sym_type (sym));
1699 /* Add attributes to variables. Functions are handled elsewhere. */
1700 attributes = add_attributes_to_decl (sym->attr, NULL_TREE);
1701 decl_attributes (&decl, attributes, 0);
1703 /* Symbols from modules should have their assembler names mangled.
1704 This is done here rather than in gfc_finish_var_decl because it
1705 is different for string length variables. */
1706 if (sym->module || sym->fn_result_spec)
1708 gfc_set_decl_assembler_name (decl, gfc_sym_mangled_identifier (sym));
1709 if (sym->attr.use_assoc && !intrinsic_array_parameter)
1710 DECL_IGNORED_P (decl) = 1;
1713 if (sym->attr.select_type_temporary)
1715 DECL_ARTIFICIAL (decl) = 1;
1716 DECL_IGNORED_P (decl) = 1;
1719 if (sym->attr.dimension || sym->attr.codimension)
1721 /* Create variables to hold the non-constant bits of array info. */
1722 gfc_build_qualified_array (decl, sym);
1724 if (sym->attr.contiguous
1725 || ((sym->attr.allocatable || !sym->attr.dummy) && !sym->attr.pointer))
1726 GFC_DECL_PACKED_ARRAY (decl) = 1;
1729 /* Remember this variable for allocation/cleanup. */
1730 if (sym->attr.dimension || sym->attr.allocatable || sym->attr.codimension
1731 || (sym->ts.type == BT_CLASS &&
1732 (CLASS_DATA (sym)->attr.dimension
1733 || CLASS_DATA (sym)->attr.allocatable))
1734 || (sym->ts.type == BT_DERIVED
1735 && (sym->ts.u.derived->attr.alloc_comp
1736 || (!sym->attr.pointer && !sym->attr.artificial && !sym->attr.save
1737 && !sym->ns->proc_name->attr.is_main_program
1738 && gfc_is_finalizable (sym->ts.u.derived, NULL))))
1739 /* This applies a derived type default initializer. */
1740 || (sym->ts.type == BT_DERIVED
1741 && sym->attr.save == SAVE_NONE
1742 && !sym->attr.data
1743 && !sym->attr.allocatable
1744 && (sym->value && !sym->ns->proc_name->attr.is_main_program)
1745 && !(sym->attr.use_assoc && !intrinsic_array_parameter)))
1746 gfc_defer_symbol_init (sym);
1748 if (sym->ts.type == BT_CHARACTER
1749 && sym->attr.allocatable
1750 && !sym->attr.dimension
1751 && sym->ts.u.cl && sym->ts.u.cl->length
1752 && sym->ts.u.cl->length->expr_type == EXPR_VARIABLE)
1753 gfc_defer_symbol_init (sym);
1755 /* Associate names can use the hidden string length variable
1756 of their associated target. */
1757 if (sym->ts.type == BT_CHARACTER
1758 && TREE_CODE (length) != INTEGER_CST
1759 && TREE_CODE (sym->ts.u.cl->backend_decl) != INDIRECT_REF)
1761 length = fold_convert (gfc_charlen_type_node, length);
1762 gfc_finish_var_decl (length, sym);
1763 if (!sym->attr.associate_var
1764 && TREE_CODE (length) == VAR_DECL
1765 && sym->value && sym->value->expr_type != EXPR_NULL
1766 && sym->value->ts.u.cl->length)
1768 gfc_expr *len = sym->value->ts.u.cl->length;
1769 DECL_INITIAL (length) = gfc_conv_initializer (len, &len->ts,
1770 TREE_TYPE (length),
1771 false, false, false);
1772 DECL_INITIAL (length) = fold_convert (gfc_charlen_type_node,
1773 DECL_INITIAL (length));
1775 else
1776 gcc_assert (!sym->value || sym->value->expr_type == EXPR_NULL);
1779 gfc_finish_var_decl (decl, sym);
1781 if (sym->ts.type == BT_CHARACTER)
1782 /* Character variables need special handling. */
1783 gfc_allocate_lang_decl (decl);
1785 if (sym->assoc && sym->attr.subref_array_pointer)
1786 sym->attr.pointer = 1;
1788 if (sym->attr.pointer && sym->attr.dimension
1789 && !sym->ts.deferred
1790 && !(sym->attr.select_type_temporary
1791 && !sym->attr.subref_array_pointer))
1792 GFC_DECL_PTR_ARRAY_P (decl) = 1;
1794 if (sym->ts.type == BT_CLASS)
1795 GFC_DECL_CLASS(decl) = 1;
1797 sym->backend_decl = decl;
1799 if (sym->attr.assign)
1800 gfc_add_assign_aux_vars (sym);
1802 if (intrinsic_array_parameter)
1804 TREE_STATIC (decl) = 1;
1805 DECL_EXTERNAL (decl) = 0;
1808 if (TREE_STATIC (decl)
1809 && !(sym->attr.use_assoc && !intrinsic_array_parameter)
1810 && (sym->attr.save || sym->ns->proc_name->attr.is_main_program
1811 || !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
1812 || sym->attr.data || sym->ns->proc_name->attr.flavor == FL_MODULE)
1813 && (flag_coarray != GFC_FCOARRAY_LIB
1814 || !sym->attr.codimension || sym->attr.allocatable)
1815 && !(sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.pdt_type)
1816 && !(sym->ts.type == BT_CLASS
1817 && CLASS_DATA (sym)->ts.u.derived->attr.pdt_type))
1819 /* Add static initializer. For procedures, it is only needed if
1820 SAVE is specified otherwise they need to be reinitialized
1821 every time the procedure is entered. The TREE_STATIC is
1822 in this case due to -fmax-stack-var-size=. */
1824 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
1825 TREE_TYPE (decl), sym->attr.dimension
1826 || (sym->attr.codimension
1827 && sym->attr.allocatable),
1828 sym->attr.pointer || sym->attr.allocatable
1829 || sym->ts.type == BT_CLASS,
1830 sym->attr.proc_pointer);
1833 if (!TREE_STATIC (decl)
1834 && POINTER_TYPE_P (TREE_TYPE (decl))
1835 && !sym->attr.pointer
1836 && !sym->attr.allocatable
1837 && !sym->attr.proc_pointer
1838 && !sym->attr.select_type_temporary)
1839 DECL_BY_REFERENCE (decl) = 1;
1841 if (sym->attr.associate_var)
1842 GFC_DECL_ASSOCIATE_VAR_P (decl) = 1;
1844 if (sym->attr.vtab
1845 || (sym->name[0] == '_' && gfc_str_startswith (sym->name, "__def_init")))
1846 TREE_READONLY (decl) = 1;
1848 return decl;
1852 /* Substitute a temporary variable in place of the real one. */
1854 void
1855 gfc_shadow_sym (gfc_symbol * sym, tree decl, gfc_saved_var * save)
1857 save->attr = sym->attr;
1858 save->decl = sym->backend_decl;
1860 gfc_clear_attr (&sym->attr);
1861 sym->attr.referenced = 1;
1862 sym->attr.flavor = FL_VARIABLE;
1864 sym->backend_decl = decl;
1868 /* Restore the original variable. */
1870 void
1871 gfc_restore_sym (gfc_symbol * sym, gfc_saved_var * save)
1873 sym->attr = save->attr;
1874 sym->backend_decl = save->decl;
1878 /* Declare a procedure pointer. */
1880 static tree
1881 get_proc_pointer_decl (gfc_symbol *sym)
1883 tree decl;
1884 tree attributes;
1886 decl = sym->backend_decl;
1887 if (decl)
1888 return decl;
1890 decl = build_decl (input_location,
1891 VAR_DECL, get_identifier (sym->name),
1892 build_pointer_type (gfc_get_function_type (sym)));
1894 if (sym->module)
1896 /* Apply name mangling. */
1897 gfc_set_decl_assembler_name (decl, gfc_sym_mangled_identifier (sym));
1898 if (sym->attr.use_assoc)
1899 DECL_IGNORED_P (decl) = 1;
1902 if ((sym->ns->proc_name
1903 && sym->ns->proc_name->backend_decl == current_function_decl)
1904 || sym->attr.contained)
1905 gfc_add_decl_to_function (decl);
1906 else if (sym->ns->proc_name->attr.flavor != FL_MODULE)
1907 gfc_add_decl_to_parent_function (decl);
1909 sym->backend_decl = decl;
1911 /* If a variable is USE associated, it's always external. */
1912 if (sym->attr.use_assoc)
1914 DECL_EXTERNAL (decl) = 1;
1915 TREE_PUBLIC (decl) = 1;
1917 else if (sym->module && sym->ns->proc_name->attr.flavor == FL_MODULE)
1919 /* This is the declaration of a module variable. */
1920 TREE_PUBLIC (decl) = 1;
1921 if (sym->attr.access == ACCESS_PRIVATE && !sym->attr.public_used)
1923 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
1924 DECL_VISIBILITY_SPECIFIED (decl) = true;
1926 TREE_STATIC (decl) = 1;
1929 if (!sym->attr.use_assoc
1930 && (sym->attr.save != SAVE_NONE || sym->attr.data
1931 || (sym->value && sym->ns->proc_name->attr.is_main_program)))
1932 TREE_STATIC (decl) = 1;
1934 if (TREE_STATIC (decl) && sym->value)
1936 /* Add static initializer. */
1937 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
1938 TREE_TYPE (decl),
1939 sym->attr.dimension,
1940 false, true);
1943 /* Handle threadprivate procedure pointers. */
1944 if (sym->attr.threadprivate
1945 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1946 set_decl_tls_model (decl, decl_default_tls_model (decl));
1948 attributes = add_attributes_to_decl (sym->attr, NULL_TREE);
1949 decl_attributes (&decl, attributes, 0);
1951 return decl;
1955 /* Get a basic decl for an external function. */
1957 tree
1958 gfc_get_extern_function_decl (gfc_symbol * sym)
1960 tree type;
1961 tree fndecl;
1962 tree attributes;
1963 gfc_expr e;
1964 gfc_intrinsic_sym *isym;
1965 gfc_expr argexpr;
1966 char s[GFC_MAX_SYMBOL_LEN + 23]; /* "_gfortran_f2c_specific" and '\0'. */
1967 tree name;
1968 tree mangled_name;
1969 gfc_gsymbol *gsym;
1971 if (sym->backend_decl)
1972 return sym->backend_decl;
1974 /* We should never be creating external decls for alternate entry points.
1975 The procedure may be an alternate entry point, but we don't want/need
1976 to know that. */
1977 gcc_assert (!(sym->attr.entry || sym->attr.entry_master));
1979 if (sym->attr.proc_pointer)
1980 return get_proc_pointer_decl (sym);
1982 /* See if this is an external procedure from the same file. If so,
1983 return the backend_decl. */
1984 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label
1985 ? sym->binding_label : sym->name);
1987 if (gsym && !gsym->defined)
1988 gsym = NULL;
1990 /* This can happen because of C binding. */
1991 if (gsym && gsym->ns && gsym->ns->proc_name
1992 && gsym->ns->proc_name->attr.flavor == FL_MODULE)
1993 goto module_sym;
1995 if ((!sym->attr.use_assoc || sym->attr.if_source != IFSRC_DECL)
1996 && !sym->backend_decl
1997 && gsym && gsym->ns
1998 && ((gsym->type == GSYM_SUBROUTINE) || (gsym->type == GSYM_FUNCTION))
1999 && (gsym->ns->proc_name->backend_decl || !sym->attr.intrinsic))
2001 if (!gsym->ns->proc_name->backend_decl)
2003 /* By construction, the external function cannot be
2004 a contained procedure. */
2005 locus old_loc;
2007 gfc_save_backend_locus (&old_loc);
2008 push_cfun (NULL);
2010 gfc_create_function_decl (gsym->ns, true);
2012 pop_cfun ();
2013 gfc_restore_backend_locus (&old_loc);
2016 /* If the namespace has entries, the proc_name is the
2017 entry master. Find the entry and use its backend_decl.
2018 otherwise, use the proc_name backend_decl. */
2019 if (gsym->ns->entries)
2021 gfc_entry_list *entry = gsym->ns->entries;
2023 for (; entry; entry = entry->next)
2025 if (strcmp (gsym->name, entry->sym->name) == 0)
2027 sym->backend_decl = entry->sym->backend_decl;
2028 break;
2032 else
2033 sym->backend_decl = gsym->ns->proc_name->backend_decl;
2035 if (sym->backend_decl)
2037 /* Avoid problems of double deallocation of the backend declaration
2038 later in gfc_trans_use_stmts; cf. PR 45087. */
2039 if (sym->attr.if_source != IFSRC_DECL && sym->attr.use_assoc)
2040 sym->attr.use_assoc = 0;
2042 return sym->backend_decl;
2046 /* See if this is a module procedure from the same file. If so,
2047 return the backend_decl. */
2048 if (sym->module)
2049 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->module);
2051 module_sym:
2052 if (gsym && gsym->ns
2053 && (gsym->type == GSYM_MODULE
2054 || (gsym->ns->proc_name && gsym->ns->proc_name->attr.flavor == FL_MODULE)))
2056 gfc_symbol *s;
2058 s = NULL;
2059 if (gsym->type == GSYM_MODULE)
2060 gfc_find_symbol (sym->name, gsym->ns, 0, &s);
2061 else
2062 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &s);
2064 if (s && s->backend_decl)
2066 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
2067 gfc_copy_dt_decls_ifequal (s->ts.u.derived, sym->ts.u.derived,
2068 true);
2069 else if (sym->ts.type == BT_CHARACTER)
2070 sym->ts.u.cl->backend_decl = s->ts.u.cl->backend_decl;
2071 sym->backend_decl = s->backend_decl;
2072 return sym->backend_decl;
2076 if (sym->attr.intrinsic)
2078 /* Call the resolution function to get the actual name. This is
2079 a nasty hack which relies on the resolution functions only looking
2080 at the first argument. We pass NULL for the second argument
2081 otherwise things like AINT get confused. */
2082 isym = gfc_find_function (sym->name);
2083 gcc_assert (isym->resolve.f0 != NULL);
2085 memset (&e, 0, sizeof (e));
2086 e.expr_type = EXPR_FUNCTION;
2088 memset (&argexpr, 0, sizeof (argexpr));
2089 gcc_assert (isym->formal);
2090 argexpr.ts = isym->formal->ts;
2092 if (isym->formal->next == NULL)
2093 isym->resolve.f1 (&e, &argexpr);
2094 else
2096 if (isym->formal->next->next == NULL)
2097 isym->resolve.f2 (&e, &argexpr, NULL);
2098 else
2100 if (isym->formal->next->next->next == NULL)
2101 isym->resolve.f3 (&e, &argexpr, NULL, NULL);
2102 else
2104 /* All specific intrinsics take less than 5 arguments. */
2105 gcc_assert (isym->formal->next->next->next->next == NULL);
2106 isym->resolve.f4 (&e, &argexpr, NULL, NULL, NULL);
2111 if (flag_f2c
2112 && ((e.ts.type == BT_REAL && e.ts.kind == gfc_default_real_kind)
2113 || e.ts.type == BT_COMPLEX))
2115 /* Specific which needs a different implementation if f2c
2116 calling conventions are used. */
2117 sprintf (s, "_gfortran_f2c_specific%s", e.value.function.name);
2119 else
2120 sprintf (s, "_gfortran_specific%s", e.value.function.name);
2122 name = get_identifier (s);
2123 mangled_name = name;
2125 else
2127 name = gfc_sym_identifier (sym);
2128 mangled_name = gfc_sym_mangled_function_id (sym);
2131 type = gfc_get_function_type (sym);
2132 fndecl = build_decl (input_location,
2133 FUNCTION_DECL, name, type);
2135 /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
2136 TREE_PUBLIC specifies whether a function is globally addressable (i.e.
2137 the opposite of declaring a function as static in C). */
2138 DECL_EXTERNAL (fndecl) = 1;
2139 TREE_PUBLIC (fndecl) = 1;
2141 attributes = add_attributes_to_decl (sym->attr, NULL_TREE);
2142 decl_attributes (&fndecl, attributes, 0);
2144 gfc_set_decl_assembler_name (fndecl, mangled_name);
2146 /* Set the context of this decl. */
2147 if (0 && sym->ns && sym->ns->proc_name)
2149 /* TODO: Add external decls to the appropriate scope. */
2150 DECL_CONTEXT (fndecl) = sym->ns->proc_name->backend_decl;
2152 else
2154 /* Global declaration, e.g. intrinsic subroutine. */
2155 DECL_CONTEXT (fndecl) = NULL_TREE;
2158 /* Set attributes for PURE functions. A call to PURE function in the
2159 Fortran 95 sense is both pure and without side effects in the C
2160 sense. */
2161 if (sym->attr.pure || sym->attr.implicit_pure)
2163 if (sym->attr.function && !gfc_return_by_reference (sym))
2164 DECL_PURE_P (fndecl) = 1;
2165 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
2166 parameters and don't use alternate returns (is this
2167 allowed?). In that case, calls to them are meaningless, and
2168 can be optimized away. See also in build_function_decl(). */
2169 TREE_SIDE_EFFECTS (fndecl) = 0;
2172 /* Mark non-returning functions. */
2173 if (sym->attr.noreturn)
2174 TREE_THIS_VOLATILE(fndecl) = 1;
2176 sym->backend_decl = fndecl;
2178 if (DECL_CONTEXT (fndecl) == NULL_TREE)
2179 pushdecl_top_level (fndecl);
2181 if (sym->formal_ns
2182 && sym->formal_ns->proc_name == sym
2183 && sym->formal_ns->omp_declare_simd)
2184 gfc_trans_omp_declare_simd (sym->formal_ns);
2186 return fndecl;
2190 /* Create a declaration for a procedure. For external functions (in the C
2191 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
2192 a master function with alternate entry points. */
2194 static void
2195 build_function_decl (gfc_symbol * sym, bool global)
2197 tree fndecl, type, attributes;
2198 symbol_attribute attr;
2199 tree result_decl;
2200 gfc_formal_arglist *f;
2202 bool module_procedure = sym->attr.module_procedure
2203 && sym->ns
2204 && sym->ns->proc_name
2205 && sym->ns->proc_name->attr.flavor == FL_MODULE;
2207 gcc_assert (!sym->attr.external || module_procedure);
2209 if (sym->backend_decl)
2210 return;
2212 /* Set the line and filename. sym->declared_at seems to point to the
2213 last statement for subroutines, but it'll do for now. */
2214 gfc_set_backend_locus (&sym->declared_at);
2216 /* Allow only one nesting level. Allow public declarations. */
2217 gcc_assert (current_function_decl == NULL_TREE
2218 || DECL_FILE_SCOPE_P (current_function_decl)
2219 || (TREE_CODE (DECL_CONTEXT (current_function_decl))
2220 == NAMESPACE_DECL));
2222 type = gfc_get_function_type (sym);
2223 fndecl = build_decl (input_location,
2224 FUNCTION_DECL, gfc_sym_identifier (sym), type);
2226 attr = sym->attr;
2228 /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
2229 TREE_PUBLIC specifies whether a function is globally addressable (i.e.
2230 the opposite of declaring a function as static in C). */
2231 DECL_EXTERNAL (fndecl) = 0;
2233 if (sym->attr.access == ACCESS_UNKNOWN && sym->module
2234 && (sym->ns->default_access == ACCESS_PRIVATE
2235 || (sym->ns->default_access == ACCESS_UNKNOWN
2236 && flag_module_private)))
2237 sym->attr.access = ACCESS_PRIVATE;
2239 if (!current_function_decl
2240 && !sym->attr.entry_master && !sym->attr.is_main_program
2241 && (sym->attr.access != ACCESS_PRIVATE || sym->binding_label
2242 || sym->attr.public_used))
2243 TREE_PUBLIC (fndecl) = 1;
2245 if (sym->attr.referenced || sym->attr.entry_master)
2246 TREE_USED (fndecl) = 1;
2248 attributes = add_attributes_to_decl (attr, NULL_TREE);
2249 decl_attributes (&fndecl, attributes, 0);
2251 /* Figure out the return type of the declared function, and build a
2252 RESULT_DECL for it. If this is a subroutine with alternate
2253 returns, build a RESULT_DECL for it. */
2254 result_decl = NULL_TREE;
2255 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
2256 if (attr.function)
2258 if (gfc_return_by_reference (sym))
2259 type = void_type_node;
2260 else
2262 if (sym->result != sym)
2263 result_decl = gfc_sym_identifier (sym->result);
2265 type = TREE_TYPE (TREE_TYPE (fndecl));
2268 else
2270 /* Look for alternate return placeholders. */
2271 int has_alternate_returns = 0;
2272 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2274 if (f->sym == NULL)
2276 has_alternate_returns = 1;
2277 break;
2281 if (has_alternate_returns)
2282 type = integer_type_node;
2283 else
2284 type = void_type_node;
2287 result_decl = build_decl (input_location,
2288 RESULT_DECL, result_decl, type);
2289 DECL_ARTIFICIAL (result_decl) = 1;
2290 DECL_IGNORED_P (result_decl) = 1;
2291 DECL_CONTEXT (result_decl) = fndecl;
2292 DECL_RESULT (fndecl) = result_decl;
2294 /* Don't call layout_decl for a RESULT_DECL.
2295 layout_decl (result_decl, 0); */
2297 /* TREE_STATIC means the function body is defined here. */
2298 TREE_STATIC (fndecl) = 1;
2300 /* Set attributes for PURE functions. A call to a PURE function in the
2301 Fortran 95 sense is both pure and without side effects in the C
2302 sense. */
2303 if (attr.pure || attr.implicit_pure)
2305 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
2306 including an alternate return. In that case it can also be
2307 marked as PURE. See also in gfc_get_extern_function_decl(). */
2308 if (attr.function && !gfc_return_by_reference (sym))
2309 DECL_PURE_P (fndecl) = 1;
2310 TREE_SIDE_EFFECTS (fndecl) = 0;
2314 /* Layout the function declaration and put it in the binding level
2315 of the current function. */
2317 if (global)
2318 pushdecl_top_level (fndecl);
2319 else
2320 pushdecl (fndecl);
2322 /* Perform name mangling if this is a top level or module procedure. */
2323 if (current_function_decl == NULL_TREE)
2324 gfc_set_decl_assembler_name (fndecl, gfc_sym_mangled_function_id (sym));
2326 sym->backend_decl = fndecl;
2330 /* Create the DECL_ARGUMENTS for a procedure. */
2332 static void
2333 create_function_arglist (gfc_symbol * sym)
2335 tree fndecl;
2336 gfc_formal_arglist *f;
2337 tree typelist, hidden_typelist;
2338 tree arglist, hidden_arglist;
2339 tree type;
2340 tree parm;
2342 fndecl = sym->backend_decl;
2344 /* Build formal argument list. Make sure that their TREE_CONTEXT is
2345 the new FUNCTION_DECL node. */
2346 arglist = NULL_TREE;
2347 hidden_arglist = NULL_TREE;
2348 typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2350 if (sym->attr.entry_master)
2352 type = TREE_VALUE (typelist);
2353 parm = build_decl (input_location,
2354 PARM_DECL, get_identifier ("__entry"), type);
2356 DECL_CONTEXT (parm) = fndecl;
2357 DECL_ARG_TYPE (parm) = type;
2358 TREE_READONLY (parm) = 1;
2359 gfc_finish_decl (parm);
2360 DECL_ARTIFICIAL (parm) = 1;
2362 arglist = chainon (arglist, parm);
2363 typelist = TREE_CHAIN (typelist);
2366 if (gfc_return_by_reference (sym))
2368 tree type = TREE_VALUE (typelist), length = NULL;
2370 if (sym->ts.type == BT_CHARACTER)
2372 /* Length of character result. */
2373 tree len_type = TREE_VALUE (TREE_CHAIN (typelist));
2375 length = build_decl (input_location,
2376 PARM_DECL,
2377 get_identifier (".__result"),
2378 len_type);
2379 if (POINTER_TYPE_P (len_type))
2381 sym->ts.u.cl->passed_length = length;
2382 TREE_USED (length) = 1;
2384 else if (!sym->ts.u.cl->length)
2386 sym->ts.u.cl->backend_decl = length;
2387 TREE_USED (length) = 1;
2389 gcc_assert (TREE_CODE (length) == PARM_DECL);
2390 DECL_CONTEXT (length) = fndecl;
2391 DECL_ARG_TYPE (length) = len_type;
2392 TREE_READONLY (length) = 1;
2393 DECL_ARTIFICIAL (length) = 1;
2394 gfc_finish_decl (length);
2395 if (sym->ts.u.cl->backend_decl == NULL
2396 || sym->ts.u.cl->backend_decl == length)
2398 gfc_symbol *arg;
2399 tree backend_decl;
2401 if (sym->ts.u.cl->backend_decl == NULL)
2403 tree len = build_decl (input_location,
2404 VAR_DECL,
2405 get_identifier ("..__result"),
2406 gfc_charlen_type_node);
2407 DECL_ARTIFICIAL (len) = 1;
2408 TREE_USED (len) = 1;
2409 sym->ts.u.cl->backend_decl = len;
2412 /* Make sure PARM_DECL type doesn't point to incomplete type. */
2413 arg = sym->result ? sym->result : sym;
2414 backend_decl = arg->backend_decl;
2415 /* Temporary clear it, so that gfc_sym_type creates complete
2416 type. */
2417 arg->backend_decl = NULL;
2418 type = gfc_sym_type (arg);
2419 arg->backend_decl = backend_decl;
2420 type = build_reference_type (type);
2424 parm = build_decl (input_location,
2425 PARM_DECL, get_identifier ("__result"), type);
2427 DECL_CONTEXT (parm) = fndecl;
2428 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
2429 TREE_READONLY (parm) = 1;
2430 DECL_ARTIFICIAL (parm) = 1;
2431 gfc_finish_decl (parm);
2433 arglist = chainon (arglist, parm);
2434 typelist = TREE_CHAIN (typelist);
2436 if (sym->ts.type == BT_CHARACTER)
2438 gfc_allocate_lang_decl (parm);
2439 arglist = chainon (arglist, length);
2440 typelist = TREE_CHAIN (typelist);
2444 hidden_typelist = typelist;
2445 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2446 if (f->sym != NULL) /* Ignore alternate returns. */
2447 hidden_typelist = TREE_CHAIN (hidden_typelist);
2449 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2451 char name[GFC_MAX_SYMBOL_LEN + 2];
2453 /* Ignore alternate returns. */
2454 if (f->sym == NULL)
2455 continue;
2457 type = TREE_VALUE (typelist);
2459 if (f->sym->ts.type == BT_CHARACTER
2460 && (!sym->attr.is_bind_c || sym->attr.entry_master))
2462 tree len_type = TREE_VALUE (hidden_typelist);
2463 tree length = NULL_TREE;
2464 if (!f->sym->ts.deferred)
2465 gcc_assert (len_type == gfc_charlen_type_node);
2466 else
2467 gcc_assert (POINTER_TYPE_P (len_type));
2469 strcpy (&name[1], f->sym->name);
2470 name[0] = '_';
2471 length = build_decl (input_location,
2472 PARM_DECL, get_identifier (name), len_type);
2474 hidden_arglist = chainon (hidden_arglist, length);
2475 DECL_CONTEXT (length) = fndecl;
2476 DECL_ARTIFICIAL (length) = 1;
2477 DECL_ARG_TYPE (length) = len_type;
2478 TREE_READONLY (length) = 1;
2479 gfc_finish_decl (length);
2481 /* Remember the passed value. */
2482 if (!f->sym->ts.u.cl || f->sym->ts.u.cl->passed_length)
2484 /* This can happen if the same type is used for multiple
2485 arguments. We need to copy cl as otherwise
2486 cl->passed_length gets overwritten. */
2487 f->sym->ts.u.cl = gfc_new_charlen (f->sym->ns, f->sym->ts.u.cl);
2489 f->sym->ts.u.cl->passed_length = length;
2491 /* Use the passed value for assumed length variables. */
2492 if (!f->sym->ts.u.cl->length)
2494 TREE_USED (length) = 1;
2495 gcc_assert (!f->sym->ts.u.cl->backend_decl);
2496 f->sym->ts.u.cl->backend_decl = length;
2499 hidden_typelist = TREE_CHAIN (hidden_typelist);
2501 if (f->sym->ts.u.cl->backend_decl == NULL
2502 || f->sym->ts.u.cl->backend_decl == length)
2504 if (POINTER_TYPE_P (len_type))
2505 f->sym->ts.u.cl->backend_decl =
2506 build_fold_indirect_ref_loc (input_location, length);
2507 else if (f->sym->ts.u.cl->backend_decl == NULL)
2508 gfc_create_string_length (f->sym);
2510 /* Make sure PARM_DECL type doesn't point to incomplete type. */
2511 if (f->sym->attr.flavor == FL_PROCEDURE)
2512 type = build_pointer_type (gfc_get_function_type (f->sym));
2513 else
2514 type = gfc_sym_type (f->sym);
2517 /* For noncharacter scalar intrinsic types, VALUE passes the value,
2518 hence, the optional status cannot be transferred via a NULL pointer.
2519 Thus, we will use a hidden argument in that case. */
2520 else if (f->sym->attr.optional && f->sym->attr.value
2521 && !f->sym->attr.dimension && f->sym->ts.type != BT_CLASS
2522 && !gfc_bt_struct (f->sym->ts.type))
2524 tree tmp;
2525 strcpy (&name[1], f->sym->name);
2526 name[0] = '_';
2527 tmp = build_decl (input_location,
2528 PARM_DECL, get_identifier (name),
2529 boolean_type_node);
2531 hidden_arglist = chainon (hidden_arglist, tmp);
2532 DECL_CONTEXT (tmp) = fndecl;
2533 DECL_ARTIFICIAL (tmp) = 1;
2534 DECL_ARG_TYPE (tmp) = boolean_type_node;
2535 TREE_READONLY (tmp) = 1;
2536 gfc_finish_decl (tmp);
2539 /* For non-constant length array arguments, make sure they use
2540 a different type node from TYPE_ARG_TYPES type. */
2541 if (f->sym->attr.dimension
2542 && type == TREE_VALUE (typelist)
2543 && TREE_CODE (type) == POINTER_TYPE
2544 && GFC_ARRAY_TYPE_P (type)
2545 && f->sym->as->type != AS_ASSUMED_SIZE
2546 && ! COMPLETE_TYPE_P (TREE_TYPE (type)))
2548 if (f->sym->attr.flavor == FL_PROCEDURE)
2549 type = build_pointer_type (gfc_get_function_type (f->sym));
2550 else
2551 type = gfc_sym_type (f->sym);
2554 if (f->sym->attr.proc_pointer)
2555 type = build_pointer_type (type);
2557 if (f->sym->attr.volatile_)
2558 type = build_qualified_type (type, TYPE_QUAL_VOLATILE);
2560 /* Build the argument declaration. */
2561 parm = build_decl (input_location,
2562 PARM_DECL, gfc_sym_identifier (f->sym), type);
2564 if (f->sym->attr.volatile_)
2566 TREE_THIS_VOLATILE (parm) = 1;
2567 TREE_SIDE_EFFECTS (parm) = 1;
2570 /* Fill in arg stuff. */
2571 DECL_CONTEXT (parm) = fndecl;
2572 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
2573 /* All implementation args except for VALUE are read-only. */
2574 if (!f->sym->attr.value)
2575 TREE_READONLY (parm) = 1;
2576 if (POINTER_TYPE_P (type)
2577 && (!f->sym->attr.proc_pointer
2578 && f->sym->attr.flavor != FL_PROCEDURE))
2579 DECL_BY_REFERENCE (parm) = 1;
2581 gfc_finish_decl (parm);
2582 gfc_finish_decl_attrs (parm, &f->sym->attr);
2584 f->sym->backend_decl = parm;
2586 /* Coarrays which are descriptorless or assumed-shape pass with
2587 -fcoarray=lib the token and the offset as hidden arguments. */
2588 if (flag_coarray == GFC_FCOARRAY_LIB
2589 && ((f->sym->ts.type != BT_CLASS && f->sym->attr.codimension
2590 && !f->sym->attr.allocatable)
2591 || (f->sym->ts.type == BT_CLASS
2592 && CLASS_DATA (f->sym)->attr.codimension
2593 && !CLASS_DATA (f->sym)->attr.allocatable)))
2595 tree caf_type;
2596 tree token;
2597 tree offset;
2599 gcc_assert (f->sym->backend_decl != NULL_TREE
2600 && !sym->attr.is_bind_c);
2601 caf_type = f->sym->ts.type == BT_CLASS
2602 ? TREE_TYPE (CLASS_DATA (f->sym)->backend_decl)
2603 : TREE_TYPE (f->sym->backend_decl);
2605 token = build_decl (input_location, PARM_DECL,
2606 create_tmp_var_name ("caf_token"),
2607 build_qualified_type (pvoid_type_node,
2608 TYPE_QUAL_RESTRICT));
2609 if ((f->sym->ts.type != BT_CLASS
2610 && f->sym->as->type != AS_DEFERRED)
2611 || (f->sym->ts.type == BT_CLASS
2612 && CLASS_DATA (f->sym)->as->type != AS_DEFERRED))
2614 gcc_assert (DECL_LANG_SPECIFIC (f->sym->backend_decl) == NULL
2615 || GFC_DECL_TOKEN (f->sym->backend_decl) == NULL_TREE);
2616 if (DECL_LANG_SPECIFIC (f->sym->backend_decl) == NULL)
2617 gfc_allocate_lang_decl (f->sym->backend_decl);
2618 GFC_DECL_TOKEN (f->sym->backend_decl) = token;
2620 else
2622 gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) == NULL_TREE);
2623 GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) = token;
2626 DECL_CONTEXT (token) = fndecl;
2627 DECL_ARTIFICIAL (token) = 1;
2628 DECL_ARG_TYPE (token) = TREE_VALUE (typelist);
2629 TREE_READONLY (token) = 1;
2630 hidden_arglist = chainon (hidden_arglist, token);
2631 gfc_finish_decl (token);
2633 offset = build_decl (input_location, PARM_DECL,
2634 create_tmp_var_name ("caf_offset"),
2635 gfc_array_index_type);
2637 if ((f->sym->ts.type != BT_CLASS
2638 && f->sym->as->type != AS_DEFERRED)
2639 || (f->sym->ts.type == BT_CLASS
2640 && CLASS_DATA (f->sym)->as->type != AS_DEFERRED))
2642 gcc_assert (GFC_DECL_CAF_OFFSET (f->sym->backend_decl)
2643 == NULL_TREE);
2644 GFC_DECL_CAF_OFFSET (f->sym->backend_decl) = offset;
2646 else
2648 gcc_assert (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) == NULL_TREE);
2649 GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) = offset;
2651 DECL_CONTEXT (offset) = fndecl;
2652 DECL_ARTIFICIAL (offset) = 1;
2653 DECL_ARG_TYPE (offset) = TREE_VALUE (typelist);
2654 TREE_READONLY (offset) = 1;
2655 hidden_arglist = chainon (hidden_arglist, offset);
2656 gfc_finish_decl (offset);
2659 arglist = chainon (arglist, parm);
2660 typelist = TREE_CHAIN (typelist);
2663 /* Add the hidden string length parameters, unless the procedure
2664 is bind(C). */
2665 if (!sym->attr.is_bind_c)
2666 arglist = chainon (arglist, hidden_arglist);
2668 gcc_assert (hidden_typelist == NULL_TREE
2669 || TREE_VALUE (hidden_typelist) == void_type_node);
2670 DECL_ARGUMENTS (fndecl) = arglist;
2673 /* Do the setup necessary before generating the body of a function. */
2675 static void
2676 trans_function_start (gfc_symbol * sym)
2678 tree fndecl;
2680 fndecl = sym->backend_decl;
2682 /* Let GCC know the current scope is this function. */
2683 current_function_decl = fndecl;
2685 /* Let the world know what we're about to do. */
2686 announce_function (fndecl);
2688 if (DECL_FILE_SCOPE_P (fndecl))
2690 /* Create RTL for function declaration. */
2691 rest_of_decl_compilation (fndecl, 1, 0);
2694 /* Create RTL for function definition. */
2695 make_decl_rtl (fndecl);
2697 allocate_struct_function (fndecl, false);
2699 /* function.c requires a push at the start of the function. */
2700 pushlevel ();
2703 /* Create thunks for alternate entry points. */
2705 static void
2706 build_entry_thunks (gfc_namespace * ns, bool global)
2708 gfc_formal_arglist *formal;
2709 gfc_formal_arglist *thunk_formal;
2710 gfc_entry_list *el;
2711 gfc_symbol *thunk_sym;
2712 stmtblock_t body;
2713 tree thunk_fndecl;
2714 tree tmp;
2715 locus old_loc;
2717 /* This should always be a toplevel function. */
2718 gcc_assert (current_function_decl == NULL_TREE);
2720 gfc_save_backend_locus (&old_loc);
2721 for (el = ns->entries; el; el = el->next)
2723 vec<tree, va_gc> *args = NULL;
2724 vec<tree, va_gc> *string_args = NULL;
2726 thunk_sym = el->sym;
2728 build_function_decl (thunk_sym, global);
2729 create_function_arglist (thunk_sym);
2731 trans_function_start (thunk_sym);
2733 thunk_fndecl = thunk_sym->backend_decl;
2735 gfc_init_block (&body);
2737 /* Pass extra parameter identifying this entry point. */
2738 tmp = build_int_cst (gfc_array_index_type, el->id);
2739 vec_safe_push (args, tmp);
2741 if (thunk_sym->attr.function)
2743 if (gfc_return_by_reference (ns->proc_name))
2745 tree ref = DECL_ARGUMENTS (current_function_decl);
2746 vec_safe_push (args, ref);
2747 if (ns->proc_name->ts.type == BT_CHARACTER)
2748 vec_safe_push (args, DECL_CHAIN (ref));
2752 for (formal = gfc_sym_get_dummy_args (ns->proc_name); formal;
2753 formal = formal->next)
2755 /* Ignore alternate returns. */
2756 if (formal->sym == NULL)
2757 continue;
2759 /* We don't have a clever way of identifying arguments, so resort to
2760 a brute-force search. */
2761 for (thunk_formal = gfc_sym_get_dummy_args (thunk_sym);
2762 thunk_formal;
2763 thunk_formal = thunk_formal->next)
2765 if (thunk_formal->sym == formal->sym)
2766 break;
2769 if (thunk_formal)
2771 /* Pass the argument. */
2772 DECL_ARTIFICIAL (thunk_formal->sym->backend_decl) = 1;
2773 vec_safe_push (args, thunk_formal->sym->backend_decl);
2774 if (formal->sym->ts.type == BT_CHARACTER)
2776 tmp = thunk_formal->sym->ts.u.cl->backend_decl;
2777 vec_safe_push (string_args, tmp);
2780 else
2782 /* Pass NULL for a missing argument. */
2783 vec_safe_push (args, null_pointer_node);
2784 if (formal->sym->ts.type == BT_CHARACTER)
2786 tmp = build_int_cst (gfc_charlen_type_node, 0);
2787 vec_safe_push (string_args, tmp);
2792 /* Call the master function. */
2793 vec_safe_splice (args, string_args);
2794 tmp = ns->proc_name->backend_decl;
2795 tmp = build_call_expr_loc_vec (input_location, tmp, args);
2796 if (ns->proc_name->attr.mixed_entry_master)
2798 tree union_decl, field;
2799 tree master_type = TREE_TYPE (ns->proc_name->backend_decl);
2801 union_decl = build_decl (input_location,
2802 VAR_DECL, get_identifier ("__result"),
2803 TREE_TYPE (master_type));
2804 DECL_ARTIFICIAL (union_decl) = 1;
2805 DECL_EXTERNAL (union_decl) = 0;
2806 TREE_PUBLIC (union_decl) = 0;
2807 TREE_USED (union_decl) = 1;
2808 layout_decl (union_decl, 0);
2809 pushdecl (union_decl);
2811 DECL_CONTEXT (union_decl) = current_function_decl;
2812 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
2813 TREE_TYPE (union_decl), union_decl, tmp);
2814 gfc_add_expr_to_block (&body, tmp);
2816 for (field = TYPE_FIELDS (TREE_TYPE (union_decl));
2817 field; field = DECL_CHAIN (field))
2818 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
2819 thunk_sym->result->name) == 0)
2820 break;
2821 gcc_assert (field != NULL_TREE);
2822 tmp = fold_build3_loc (input_location, COMPONENT_REF,
2823 TREE_TYPE (field), union_decl, field,
2824 NULL_TREE);
2825 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
2826 TREE_TYPE (DECL_RESULT (current_function_decl)),
2827 DECL_RESULT (current_function_decl), tmp);
2828 tmp = build1_v (RETURN_EXPR, tmp);
2830 else if (TREE_TYPE (DECL_RESULT (current_function_decl))
2831 != void_type_node)
2833 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
2834 TREE_TYPE (DECL_RESULT (current_function_decl)),
2835 DECL_RESULT (current_function_decl), tmp);
2836 tmp = build1_v (RETURN_EXPR, tmp);
2838 gfc_add_expr_to_block (&body, tmp);
2840 /* Finish off this function and send it for code generation. */
2841 DECL_SAVED_TREE (thunk_fndecl) = gfc_finish_block (&body);
2842 tmp = getdecls ();
2843 poplevel (1, 1);
2844 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl)) = thunk_fndecl;
2845 DECL_SAVED_TREE (thunk_fndecl)
2846 = build3_v (BIND_EXPR, tmp, DECL_SAVED_TREE (thunk_fndecl),
2847 DECL_INITIAL (thunk_fndecl));
2849 /* Output the GENERIC tree. */
2850 dump_function (TDI_original, thunk_fndecl);
2852 /* Store the end of the function, so that we get good line number
2853 info for the epilogue. */
2854 cfun->function_end_locus = input_location;
2856 /* We're leaving the context of this function, so zap cfun.
2857 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
2858 tree_rest_of_compilation. */
2859 set_cfun (NULL);
2861 current_function_decl = NULL_TREE;
2863 cgraph_node::finalize_function (thunk_fndecl, true);
2865 /* We share the symbols in the formal argument list with other entry
2866 points and the master function. Clear them so that they are
2867 recreated for each function. */
2868 for (formal = gfc_sym_get_dummy_args (thunk_sym); formal;
2869 formal = formal->next)
2870 if (formal->sym != NULL) /* Ignore alternate returns. */
2872 formal->sym->backend_decl = NULL_TREE;
2873 if (formal->sym->ts.type == BT_CHARACTER)
2874 formal->sym->ts.u.cl->backend_decl = NULL_TREE;
2877 if (thunk_sym->attr.function)
2879 if (thunk_sym->ts.type == BT_CHARACTER)
2880 thunk_sym->ts.u.cl->backend_decl = NULL_TREE;
2881 if (thunk_sym->result->ts.type == BT_CHARACTER)
2882 thunk_sym->result->ts.u.cl->backend_decl = NULL_TREE;
2886 gfc_restore_backend_locus (&old_loc);
2890 /* Create a decl for a function, and create any thunks for alternate entry
2891 points. If global is true, generate the function in the global binding
2892 level, otherwise in the current binding level (which can be global). */
2894 void
2895 gfc_create_function_decl (gfc_namespace * ns, bool global)
2897 /* Create a declaration for the master function. */
2898 build_function_decl (ns->proc_name, global);
2900 /* Compile the entry thunks. */
2901 if (ns->entries)
2902 build_entry_thunks (ns, global);
2904 /* Now create the read argument list. */
2905 create_function_arglist (ns->proc_name);
2907 if (ns->omp_declare_simd)
2908 gfc_trans_omp_declare_simd (ns);
2911 /* Return the decl used to hold the function return value. If
2912 parent_flag is set, the context is the parent_scope. */
2914 tree
2915 gfc_get_fake_result_decl (gfc_symbol * sym, int parent_flag)
2917 tree decl;
2918 tree length;
2919 tree this_fake_result_decl;
2920 tree this_function_decl;
2922 char name[GFC_MAX_SYMBOL_LEN + 10];
2924 if (parent_flag)
2926 this_fake_result_decl = parent_fake_result_decl;
2927 this_function_decl = DECL_CONTEXT (current_function_decl);
2929 else
2931 this_fake_result_decl = current_fake_result_decl;
2932 this_function_decl = current_function_decl;
2935 if (sym
2936 && sym->ns->proc_name->backend_decl == this_function_decl
2937 && sym->ns->proc_name->attr.entry_master
2938 && sym != sym->ns->proc_name)
2940 tree t = NULL, var;
2941 if (this_fake_result_decl != NULL)
2942 for (t = TREE_CHAIN (this_fake_result_decl); t; t = TREE_CHAIN (t))
2943 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t)), sym->name) == 0)
2944 break;
2945 if (t)
2946 return TREE_VALUE (t);
2947 decl = gfc_get_fake_result_decl (sym->ns->proc_name, parent_flag);
2949 if (parent_flag)
2950 this_fake_result_decl = parent_fake_result_decl;
2951 else
2952 this_fake_result_decl = current_fake_result_decl;
2954 if (decl && sym->ns->proc_name->attr.mixed_entry_master)
2956 tree field;
2958 for (field = TYPE_FIELDS (TREE_TYPE (decl));
2959 field; field = DECL_CHAIN (field))
2960 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
2961 sym->name) == 0)
2962 break;
2964 gcc_assert (field != NULL_TREE);
2965 decl = fold_build3_loc (input_location, COMPONENT_REF,
2966 TREE_TYPE (field), decl, field, NULL_TREE);
2969 var = create_tmp_var_raw (TREE_TYPE (decl), sym->name);
2970 if (parent_flag)
2971 gfc_add_decl_to_parent_function (var);
2972 else
2973 gfc_add_decl_to_function (var);
2975 SET_DECL_VALUE_EXPR (var, decl);
2976 DECL_HAS_VALUE_EXPR_P (var) = 1;
2977 GFC_DECL_RESULT (var) = 1;
2979 TREE_CHAIN (this_fake_result_decl)
2980 = tree_cons (get_identifier (sym->name), var,
2981 TREE_CHAIN (this_fake_result_decl));
2982 return var;
2985 if (this_fake_result_decl != NULL_TREE)
2986 return TREE_VALUE (this_fake_result_decl);
2988 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
2989 sym is NULL. */
2990 if (!sym)
2991 return NULL_TREE;
2993 if (sym->ts.type == BT_CHARACTER)
2995 if (sym->ts.u.cl->backend_decl == NULL_TREE)
2996 length = gfc_create_string_length (sym);
2997 else
2998 length = sym->ts.u.cl->backend_decl;
2999 if (VAR_P (length) && DECL_CONTEXT (length) == NULL_TREE)
3000 gfc_add_decl_to_function (length);
3003 if (gfc_return_by_reference (sym))
3005 decl = DECL_ARGUMENTS (this_function_decl);
3007 if (sym->ns->proc_name->backend_decl == this_function_decl
3008 && sym->ns->proc_name->attr.entry_master)
3009 decl = DECL_CHAIN (decl);
3011 TREE_USED (decl) = 1;
3012 if (sym->as)
3013 decl = gfc_build_dummy_array_decl (sym, decl);
3015 else
3017 sprintf (name, "__result_%.20s",
3018 IDENTIFIER_POINTER (DECL_NAME (this_function_decl)));
3020 if (!sym->attr.mixed_entry_master && sym->attr.function)
3021 decl = build_decl (DECL_SOURCE_LOCATION (this_function_decl),
3022 VAR_DECL, get_identifier (name),
3023 gfc_sym_type (sym));
3024 else
3025 decl = build_decl (DECL_SOURCE_LOCATION (this_function_decl),
3026 VAR_DECL, get_identifier (name),
3027 TREE_TYPE (TREE_TYPE (this_function_decl)));
3028 DECL_ARTIFICIAL (decl) = 1;
3029 DECL_EXTERNAL (decl) = 0;
3030 TREE_PUBLIC (decl) = 0;
3031 TREE_USED (decl) = 1;
3032 GFC_DECL_RESULT (decl) = 1;
3033 TREE_ADDRESSABLE (decl) = 1;
3035 layout_decl (decl, 0);
3036 gfc_finish_decl_attrs (decl, &sym->attr);
3038 if (parent_flag)
3039 gfc_add_decl_to_parent_function (decl);
3040 else
3041 gfc_add_decl_to_function (decl);
3044 if (parent_flag)
3045 parent_fake_result_decl = build_tree_list (NULL, decl);
3046 else
3047 current_fake_result_decl = build_tree_list (NULL, decl);
3049 return decl;
3053 /* Builds a function decl. The remaining parameters are the types of the
3054 function arguments. Negative nargs indicates a varargs function. */
3056 static tree
3057 build_library_function_decl_1 (tree name, const char *spec,
3058 tree rettype, int nargs, va_list p)
3060 vec<tree, va_gc> *arglist;
3061 tree fntype;
3062 tree fndecl;
3063 int n;
3065 /* Library functions must be declared with global scope. */
3066 gcc_assert (current_function_decl == NULL_TREE);
3068 /* Create a list of the argument types. */
3069 vec_alloc (arglist, abs (nargs));
3070 for (n = abs (nargs); n > 0; n--)
3072 tree argtype = va_arg (p, tree);
3073 arglist->quick_push (argtype);
3076 /* Build the function type and decl. */
3077 if (nargs >= 0)
3078 fntype = build_function_type_vec (rettype, arglist);
3079 else
3080 fntype = build_varargs_function_type_vec (rettype, arglist);
3081 if (spec)
3083 tree attr_args = build_tree_list (NULL_TREE,
3084 build_string (strlen (spec), spec));
3085 tree attrs = tree_cons (get_identifier ("fn spec"),
3086 attr_args, TYPE_ATTRIBUTES (fntype));
3087 fntype = build_type_attribute_variant (fntype, attrs);
3089 fndecl = build_decl (input_location,
3090 FUNCTION_DECL, name, fntype);
3092 /* Mark this decl as external. */
3093 DECL_EXTERNAL (fndecl) = 1;
3094 TREE_PUBLIC (fndecl) = 1;
3096 pushdecl (fndecl);
3098 rest_of_decl_compilation (fndecl, 1, 0);
3100 return fndecl;
3103 /* Builds a function decl. The remaining parameters are the types of the
3104 function arguments. Negative nargs indicates a varargs function. */
3106 tree
3107 gfc_build_library_function_decl (tree name, tree rettype, int nargs, ...)
3109 tree ret;
3110 va_list args;
3111 va_start (args, nargs);
3112 ret = build_library_function_decl_1 (name, NULL, rettype, nargs, args);
3113 va_end (args);
3114 return ret;
3117 /* Builds a function decl. The remaining parameters are the types of the
3118 function arguments. Negative nargs indicates a varargs function.
3119 The SPEC parameter specifies the function argument and return type
3120 specification according to the fnspec function type attribute. */
3122 tree
3123 gfc_build_library_function_decl_with_spec (tree name, const char *spec,
3124 tree rettype, int nargs, ...)
3126 tree ret;
3127 va_list args;
3128 va_start (args, nargs);
3129 ret = build_library_function_decl_1 (name, spec, rettype, nargs, args);
3130 va_end (args);
3131 return ret;
3134 static void
3135 gfc_build_intrinsic_function_decls (void)
3137 tree gfc_int4_type_node = gfc_get_int_type (4);
3138 tree gfc_pint4_type_node = build_pointer_type (gfc_int4_type_node);
3139 tree gfc_int8_type_node = gfc_get_int_type (8);
3140 tree gfc_pint8_type_node = build_pointer_type (gfc_int8_type_node);
3141 tree gfc_int16_type_node = gfc_get_int_type (16);
3142 tree gfc_logical4_type_node = gfc_get_logical_type (4);
3143 tree pchar1_type_node = gfc_get_pchar_type (1);
3144 tree pchar4_type_node = gfc_get_pchar_type (4);
3146 /* String functions. */
3147 gfor_fndecl_compare_string = gfc_build_library_function_decl_with_spec (
3148 get_identifier (PREFIX("compare_string")), "..R.R",
3149 integer_type_node, 4, gfc_charlen_type_node, pchar1_type_node,
3150 gfc_charlen_type_node, pchar1_type_node);
3151 DECL_PURE_P (gfor_fndecl_compare_string) = 1;
3152 TREE_NOTHROW (gfor_fndecl_compare_string) = 1;
3154 gfor_fndecl_concat_string = gfc_build_library_function_decl_with_spec (
3155 get_identifier (PREFIX("concat_string")), "..W.R.R",
3156 void_type_node, 6, gfc_charlen_type_node, pchar1_type_node,
3157 gfc_charlen_type_node, pchar1_type_node,
3158 gfc_charlen_type_node, pchar1_type_node);
3159 TREE_NOTHROW (gfor_fndecl_concat_string) = 1;
3161 gfor_fndecl_string_len_trim = gfc_build_library_function_decl_with_spec (
3162 get_identifier (PREFIX("string_len_trim")), "..R",
3163 gfc_charlen_type_node, 2, gfc_charlen_type_node, pchar1_type_node);
3164 DECL_PURE_P (gfor_fndecl_string_len_trim) = 1;
3165 TREE_NOTHROW (gfor_fndecl_string_len_trim) = 1;
3167 gfor_fndecl_string_index = gfc_build_library_function_decl_with_spec (
3168 get_identifier (PREFIX("string_index")), "..R.R.",
3169 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar1_type_node,
3170 gfc_charlen_type_node, pchar1_type_node, gfc_logical4_type_node);
3171 DECL_PURE_P (gfor_fndecl_string_index) = 1;
3172 TREE_NOTHROW (gfor_fndecl_string_index) = 1;
3174 gfor_fndecl_string_scan = gfc_build_library_function_decl_with_spec (
3175 get_identifier (PREFIX("string_scan")), "..R.R.",
3176 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar1_type_node,
3177 gfc_charlen_type_node, pchar1_type_node, gfc_logical4_type_node);
3178 DECL_PURE_P (gfor_fndecl_string_scan) = 1;
3179 TREE_NOTHROW (gfor_fndecl_string_scan) = 1;
3181 gfor_fndecl_string_verify = gfc_build_library_function_decl_with_spec (
3182 get_identifier (PREFIX("string_verify")), "..R.R.",
3183 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar1_type_node,
3184 gfc_charlen_type_node, pchar1_type_node, gfc_logical4_type_node);
3185 DECL_PURE_P (gfor_fndecl_string_verify) = 1;
3186 TREE_NOTHROW (gfor_fndecl_string_verify) = 1;
3188 gfor_fndecl_string_trim = gfc_build_library_function_decl_with_spec (
3189 get_identifier (PREFIX("string_trim")), ".Ww.R",
3190 void_type_node, 4, build_pointer_type (gfc_charlen_type_node),
3191 build_pointer_type (pchar1_type_node), gfc_charlen_type_node,
3192 pchar1_type_node);
3194 gfor_fndecl_string_minmax = gfc_build_library_function_decl_with_spec (
3195 get_identifier (PREFIX("string_minmax")), ".Ww.R",
3196 void_type_node, -4, build_pointer_type (gfc_charlen_type_node),
3197 build_pointer_type (pchar1_type_node), integer_type_node,
3198 integer_type_node);
3200 gfor_fndecl_adjustl = gfc_build_library_function_decl_with_spec (
3201 get_identifier (PREFIX("adjustl")), ".W.R",
3202 void_type_node, 3, pchar1_type_node, gfc_charlen_type_node,
3203 pchar1_type_node);
3204 TREE_NOTHROW (gfor_fndecl_adjustl) = 1;
3206 gfor_fndecl_adjustr = gfc_build_library_function_decl_with_spec (
3207 get_identifier (PREFIX("adjustr")), ".W.R",
3208 void_type_node, 3, pchar1_type_node, gfc_charlen_type_node,
3209 pchar1_type_node);
3210 TREE_NOTHROW (gfor_fndecl_adjustr) = 1;
3212 gfor_fndecl_select_string = gfc_build_library_function_decl_with_spec (
3213 get_identifier (PREFIX("select_string")), ".R.R.",
3214 integer_type_node, 4, pvoid_type_node, integer_type_node,
3215 pchar1_type_node, gfc_charlen_type_node);
3216 DECL_PURE_P (gfor_fndecl_select_string) = 1;
3217 TREE_NOTHROW (gfor_fndecl_select_string) = 1;
3219 gfor_fndecl_compare_string_char4 = gfc_build_library_function_decl_with_spec (
3220 get_identifier (PREFIX("compare_string_char4")), "..R.R",
3221 integer_type_node, 4, gfc_charlen_type_node, pchar4_type_node,
3222 gfc_charlen_type_node, pchar4_type_node);
3223 DECL_PURE_P (gfor_fndecl_compare_string_char4) = 1;
3224 TREE_NOTHROW (gfor_fndecl_compare_string_char4) = 1;
3226 gfor_fndecl_concat_string_char4 = gfc_build_library_function_decl_with_spec (
3227 get_identifier (PREFIX("concat_string_char4")), "..W.R.R",
3228 void_type_node, 6, gfc_charlen_type_node, pchar4_type_node,
3229 gfc_charlen_type_node, pchar4_type_node, gfc_charlen_type_node,
3230 pchar4_type_node);
3231 TREE_NOTHROW (gfor_fndecl_concat_string_char4) = 1;
3233 gfor_fndecl_string_len_trim_char4 = gfc_build_library_function_decl_with_spec (
3234 get_identifier (PREFIX("string_len_trim_char4")), "..R",
3235 gfc_charlen_type_node, 2, gfc_charlen_type_node, pchar4_type_node);
3236 DECL_PURE_P (gfor_fndecl_string_len_trim_char4) = 1;
3237 TREE_NOTHROW (gfor_fndecl_string_len_trim_char4) = 1;
3239 gfor_fndecl_string_index_char4 = gfc_build_library_function_decl_with_spec (
3240 get_identifier (PREFIX("string_index_char4")), "..R.R.",
3241 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar4_type_node,
3242 gfc_charlen_type_node, pchar4_type_node, gfc_logical4_type_node);
3243 DECL_PURE_P (gfor_fndecl_string_index_char4) = 1;
3244 TREE_NOTHROW (gfor_fndecl_string_index_char4) = 1;
3246 gfor_fndecl_string_scan_char4 = gfc_build_library_function_decl_with_spec (
3247 get_identifier (PREFIX("string_scan_char4")), "..R.R.",
3248 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar4_type_node,
3249 gfc_charlen_type_node, pchar4_type_node, gfc_logical4_type_node);
3250 DECL_PURE_P (gfor_fndecl_string_scan_char4) = 1;
3251 TREE_NOTHROW (gfor_fndecl_string_scan_char4) = 1;
3253 gfor_fndecl_string_verify_char4 = gfc_build_library_function_decl_with_spec (
3254 get_identifier (PREFIX("string_verify_char4")), "..R.R.",
3255 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar4_type_node,
3256 gfc_charlen_type_node, pchar4_type_node, gfc_logical4_type_node);
3257 DECL_PURE_P (gfor_fndecl_string_verify_char4) = 1;
3258 TREE_NOTHROW (gfor_fndecl_string_verify_char4) = 1;
3260 gfor_fndecl_string_trim_char4 = gfc_build_library_function_decl_with_spec (
3261 get_identifier (PREFIX("string_trim_char4")), ".Ww.R",
3262 void_type_node, 4, build_pointer_type (gfc_charlen_type_node),
3263 build_pointer_type (pchar4_type_node), gfc_charlen_type_node,
3264 pchar4_type_node);
3266 gfor_fndecl_string_minmax_char4 = gfc_build_library_function_decl_with_spec (
3267 get_identifier (PREFIX("string_minmax_char4")), ".Ww.R",
3268 void_type_node, -4, build_pointer_type (gfc_charlen_type_node),
3269 build_pointer_type (pchar4_type_node), integer_type_node,
3270 integer_type_node);
3272 gfor_fndecl_adjustl_char4 = gfc_build_library_function_decl_with_spec (
3273 get_identifier (PREFIX("adjustl_char4")), ".W.R",
3274 void_type_node, 3, pchar4_type_node, gfc_charlen_type_node,
3275 pchar4_type_node);
3276 TREE_NOTHROW (gfor_fndecl_adjustl_char4) = 1;
3278 gfor_fndecl_adjustr_char4 = gfc_build_library_function_decl_with_spec (
3279 get_identifier (PREFIX("adjustr_char4")), ".W.R",
3280 void_type_node, 3, pchar4_type_node, gfc_charlen_type_node,
3281 pchar4_type_node);
3282 TREE_NOTHROW (gfor_fndecl_adjustr_char4) = 1;
3284 gfor_fndecl_select_string_char4 = gfc_build_library_function_decl_with_spec (
3285 get_identifier (PREFIX("select_string_char4")), ".R.R.",
3286 integer_type_node, 4, pvoid_type_node, integer_type_node,
3287 pvoid_type_node, gfc_charlen_type_node);
3288 DECL_PURE_P (gfor_fndecl_select_string_char4) = 1;
3289 TREE_NOTHROW (gfor_fndecl_select_string_char4) = 1;
3292 /* Conversion between character kinds. */
3294 gfor_fndecl_convert_char1_to_char4 = gfc_build_library_function_decl_with_spec (
3295 get_identifier (PREFIX("convert_char1_to_char4")), ".w.R",
3296 void_type_node, 3, build_pointer_type (pchar4_type_node),
3297 gfc_charlen_type_node, pchar1_type_node);
3299 gfor_fndecl_convert_char4_to_char1 = gfc_build_library_function_decl_with_spec (
3300 get_identifier (PREFIX("convert_char4_to_char1")), ".w.R",
3301 void_type_node, 3, build_pointer_type (pchar1_type_node),
3302 gfc_charlen_type_node, pchar4_type_node);
3304 /* Misc. functions. */
3306 gfor_fndecl_ttynam = gfc_build_library_function_decl_with_spec (
3307 get_identifier (PREFIX("ttynam")), ".W",
3308 void_type_node, 3, pchar_type_node, gfc_charlen_type_node,
3309 integer_type_node);
3311 gfor_fndecl_fdate = gfc_build_library_function_decl_with_spec (
3312 get_identifier (PREFIX("fdate")), ".W",
3313 void_type_node, 2, pchar_type_node, gfc_charlen_type_node);
3315 gfor_fndecl_ctime = gfc_build_library_function_decl_with_spec (
3316 get_identifier (PREFIX("ctime")), ".W",
3317 void_type_node, 3, pchar_type_node, gfc_charlen_type_node,
3318 gfc_int8_type_node);
3320 gfor_fndecl_random_init = gfc_build_library_function_decl (
3321 get_identifier (PREFIX("random_init")),
3322 void_type_node, 3, gfc_logical4_type_node, gfc_logical4_type_node,
3323 gfc_int4_type_node);
3325 gfor_fndecl_sc_kind = gfc_build_library_function_decl_with_spec (
3326 get_identifier (PREFIX("selected_char_kind")), "..R",
3327 gfc_int4_type_node, 2, gfc_charlen_type_node, pchar_type_node);
3328 DECL_PURE_P (gfor_fndecl_sc_kind) = 1;
3329 TREE_NOTHROW (gfor_fndecl_sc_kind) = 1;
3331 gfor_fndecl_si_kind = gfc_build_library_function_decl_with_spec (
3332 get_identifier (PREFIX("selected_int_kind")), ".R",
3333 gfc_int4_type_node, 1, pvoid_type_node);
3334 DECL_PURE_P (gfor_fndecl_si_kind) = 1;
3335 TREE_NOTHROW (gfor_fndecl_si_kind) = 1;
3337 gfor_fndecl_sr_kind = gfc_build_library_function_decl_with_spec (
3338 get_identifier (PREFIX("selected_real_kind2008")), ".RR",
3339 gfc_int4_type_node, 3, pvoid_type_node, pvoid_type_node,
3340 pvoid_type_node);
3341 DECL_PURE_P (gfor_fndecl_sr_kind) = 1;
3342 TREE_NOTHROW (gfor_fndecl_sr_kind) = 1;
3344 gfor_fndecl_system_clock4 = gfc_build_library_function_decl (
3345 get_identifier (PREFIX("system_clock_4")),
3346 void_type_node, 3, gfc_pint4_type_node, gfc_pint4_type_node,
3347 gfc_pint4_type_node);
3349 gfor_fndecl_system_clock8 = gfc_build_library_function_decl (
3350 get_identifier (PREFIX("system_clock_8")),
3351 void_type_node, 3, gfc_pint8_type_node, gfc_pint8_type_node,
3352 gfc_pint8_type_node);
3354 /* Power functions. */
3356 tree ctype, rtype, itype, jtype;
3357 int rkind, ikind, jkind;
3358 #define NIKINDS 3
3359 #define NRKINDS 4
3360 static int ikinds[NIKINDS] = {4, 8, 16};
3361 static int rkinds[NRKINDS] = {4, 8, 10, 16};
3362 char name[PREFIX_LEN + 12]; /* _gfortran_pow_?n_?n */
3364 for (ikind=0; ikind < NIKINDS; ikind++)
3366 itype = gfc_get_int_type (ikinds[ikind]);
3368 for (jkind=0; jkind < NIKINDS; jkind++)
3370 jtype = gfc_get_int_type (ikinds[jkind]);
3371 if (itype && jtype)
3373 sprintf (name, PREFIX("pow_i%d_i%d"), ikinds[ikind],
3374 ikinds[jkind]);
3375 gfor_fndecl_math_powi[jkind][ikind].integer =
3376 gfc_build_library_function_decl (get_identifier (name),
3377 jtype, 2, jtype, itype);
3378 TREE_READONLY (gfor_fndecl_math_powi[jkind][ikind].integer) = 1;
3379 TREE_NOTHROW (gfor_fndecl_math_powi[jkind][ikind].integer) = 1;
3383 for (rkind = 0; rkind < NRKINDS; rkind ++)
3385 rtype = gfc_get_real_type (rkinds[rkind]);
3386 if (rtype && itype)
3388 sprintf (name, PREFIX("pow_r%d_i%d"), rkinds[rkind],
3389 ikinds[ikind]);
3390 gfor_fndecl_math_powi[rkind][ikind].real =
3391 gfc_build_library_function_decl (get_identifier (name),
3392 rtype, 2, rtype, itype);
3393 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].real) = 1;
3394 TREE_NOTHROW (gfor_fndecl_math_powi[rkind][ikind].real) = 1;
3397 ctype = gfc_get_complex_type (rkinds[rkind]);
3398 if (ctype && itype)
3400 sprintf (name, PREFIX("pow_c%d_i%d"), rkinds[rkind],
3401 ikinds[ikind]);
3402 gfor_fndecl_math_powi[rkind][ikind].cmplx =
3403 gfc_build_library_function_decl (get_identifier (name),
3404 ctype, 2,ctype, itype);
3405 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1;
3406 TREE_NOTHROW (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1;
3410 #undef NIKINDS
3411 #undef NRKINDS
3414 gfor_fndecl_math_ishftc4 = gfc_build_library_function_decl (
3415 get_identifier (PREFIX("ishftc4")),
3416 gfc_int4_type_node, 3, gfc_int4_type_node, gfc_int4_type_node,
3417 gfc_int4_type_node);
3418 TREE_READONLY (gfor_fndecl_math_ishftc4) = 1;
3419 TREE_NOTHROW (gfor_fndecl_math_ishftc4) = 1;
3421 gfor_fndecl_math_ishftc8 = gfc_build_library_function_decl (
3422 get_identifier (PREFIX("ishftc8")),
3423 gfc_int8_type_node, 3, gfc_int8_type_node, gfc_int4_type_node,
3424 gfc_int4_type_node);
3425 TREE_READONLY (gfor_fndecl_math_ishftc8) = 1;
3426 TREE_NOTHROW (gfor_fndecl_math_ishftc8) = 1;
3428 if (gfc_int16_type_node)
3430 gfor_fndecl_math_ishftc16 = gfc_build_library_function_decl (
3431 get_identifier (PREFIX("ishftc16")),
3432 gfc_int16_type_node, 3, gfc_int16_type_node, gfc_int4_type_node,
3433 gfc_int4_type_node);
3434 TREE_READONLY (gfor_fndecl_math_ishftc16) = 1;
3435 TREE_NOTHROW (gfor_fndecl_math_ishftc16) = 1;
3438 /* BLAS functions. */
3440 tree pint = build_pointer_type (integer_type_node);
3441 tree ps = build_pointer_type (gfc_get_real_type (gfc_default_real_kind));
3442 tree pd = build_pointer_type (gfc_get_real_type (gfc_default_double_kind));
3443 tree pc = build_pointer_type (gfc_get_complex_type (gfc_default_real_kind));
3444 tree pz = build_pointer_type
3445 (gfc_get_complex_type (gfc_default_double_kind));
3447 gfor_fndecl_sgemm = gfc_build_library_function_decl
3448 (get_identifier
3449 (flag_underscoring ? "sgemm_" : "sgemm"),
3450 void_type_node, 15, pchar_type_node,
3451 pchar_type_node, pint, pint, pint, ps, ps, pint,
3452 ps, pint, ps, ps, pint, integer_type_node,
3453 integer_type_node);
3454 gfor_fndecl_dgemm = gfc_build_library_function_decl
3455 (get_identifier
3456 (flag_underscoring ? "dgemm_" : "dgemm"),
3457 void_type_node, 15, pchar_type_node,
3458 pchar_type_node, pint, pint, pint, pd, pd, pint,
3459 pd, pint, pd, pd, pint, integer_type_node,
3460 integer_type_node);
3461 gfor_fndecl_cgemm = gfc_build_library_function_decl
3462 (get_identifier
3463 (flag_underscoring ? "cgemm_" : "cgemm"),
3464 void_type_node, 15, pchar_type_node,
3465 pchar_type_node, pint, pint, pint, pc, pc, pint,
3466 pc, pint, pc, pc, pint, integer_type_node,
3467 integer_type_node);
3468 gfor_fndecl_zgemm = gfc_build_library_function_decl
3469 (get_identifier
3470 (flag_underscoring ? "zgemm_" : "zgemm"),
3471 void_type_node, 15, pchar_type_node,
3472 pchar_type_node, pint, pint, pint, pz, pz, pint,
3473 pz, pint, pz, pz, pint, integer_type_node,
3474 integer_type_node);
3477 /* Other functions. */
3478 gfor_fndecl_size0 = gfc_build_library_function_decl_with_spec (
3479 get_identifier (PREFIX("size0")), ".R",
3480 gfc_array_index_type, 1, pvoid_type_node);
3481 DECL_PURE_P (gfor_fndecl_size0) = 1;
3482 TREE_NOTHROW (gfor_fndecl_size0) = 1;
3484 gfor_fndecl_size1 = gfc_build_library_function_decl_with_spec (
3485 get_identifier (PREFIX("size1")), ".R",
3486 gfc_array_index_type, 2, pvoid_type_node, gfc_array_index_type);
3487 DECL_PURE_P (gfor_fndecl_size1) = 1;
3488 TREE_NOTHROW (gfor_fndecl_size1) = 1;
3490 gfor_fndecl_iargc = gfc_build_library_function_decl (
3491 get_identifier (PREFIX ("iargc")), gfc_int4_type_node, 0);
3492 TREE_NOTHROW (gfor_fndecl_iargc) = 1;
3494 gfor_fndecl_kill_sub = gfc_build_library_function_decl (
3495 get_identifier (PREFIX ("kill_sub")), void_type_node,
3496 3, gfc_int4_type_node, gfc_int4_type_node, gfc_pint4_type_node);
3498 gfor_fndecl_kill = gfc_build_library_function_decl (
3499 get_identifier (PREFIX ("kill")), gfc_int4_type_node,
3500 2, gfc_int4_type_node, gfc_int4_type_node);
3504 /* Make prototypes for runtime library functions. */
3506 void
3507 gfc_build_builtin_function_decls (void)
3509 tree gfc_int8_type_node = gfc_get_int_type (8);
3511 gfor_fndecl_stop_numeric = gfc_build_library_function_decl (
3512 get_identifier (PREFIX("stop_numeric")),
3513 void_type_node, 2, integer_type_node, boolean_type_node);
3514 /* STOP doesn't return. */
3515 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric) = 1;
3517 gfor_fndecl_stop_string = gfc_build_library_function_decl_with_spec (
3518 get_identifier (PREFIX("stop_string")), ".R.",
3519 void_type_node, 3, pchar_type_node, size_type_node,
3520 boolean_type_node);
3521 /* STOP doesn't return. */
3522 TREE_THIS_VOLATILE (gfor_fndecl_stop_string) = 1;
3524 gfor_fndecl_error_stop_numeric = gfc_build_library_function_decl (
3525 get_identifier (PREFIX("error_stop_numeric")),
3526 void_type_node, 2, integer_type_node, boolean_type_node);
3527 /* ERROR STOP doesn't return. */
3528 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_numeric) = 1;
3530 gfor_fndecl_error_stop_string = gfc_build_library_function_decl_with_spec (
3531 get_identifier (PREFIX("error_stop_string")), ".R.",
3532 void_type_node, 3, pchar_type_node, size_type_node,
3533 boolean_type_node);
3534 /* ERROR STOP doesn't return. */
3535 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_string) = 1;
3537 gfor_fndecl_pause_numeric = gfc_build_library_function_decl (
3538 get_identifier (PREFIX("pause_numeric")),
3539 void_type_node, 1, gfc_int8_type_node);
3541 gfor_fndecl_pause_string = gfc_build_library_function_decl_with_spec (
3542 get_identifier (PREFIX("pause_string")), ".R.",
3543 void_type_node, 2, pchar_type_node, size_type_node);
3545 gfor_fndecl_runtime_error = gfc_build_library_function_decl_with_spec (
3546 get_identifier (PREFIX("runtime_error")), ".R",
3547 void_type_node, -1, pchar_type_node);
3548 /* The runtime_error function does not return. */
3549 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error) = 1;
3551 gfor_fndecl_runtime_error_at = gfc_build_library_function_decl_with_spec (
3552 get_identifier (PREFIX("runtime_error_at")), ".RR",
3553 void_type_node, -2, pchar_type_node, pchar_type_node);
3554 /* The runtime_error_at function does not return. */
3555 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at) = 1;
3557 gfor_fndecl_runtime_warning_at = gfc_build_library_function_decl_with_spec (
3558 get_identifier (PREFIX("runtime_warning_at")), ".RR",
3559 void_type_node, -2, pchar_type_node, pchar_type_node);
3561 gfor_fndecl_generate_error = gfc_build_library_function_decl_with_spec (
3562 get_identifier (PREFIX("generate_error")), ".R.R",
3563 void_type_node, 3, pvoid_type_node, integer_type_node,
3564 pchar_type_node);
3566 gfor_fndecl_os_error = gfc_build_library_function_decl_with_spec (
3567 get_identifier (PREFIX("os_error")), ".R",
3568 void_type_node, 1, pchar_type_node);
3569 /* The runtime_error function does not return. */
3570 TREE_THIS_VOLATILE (gfor_fndecl_os_error) = 1;
3572 gfor_fndecl_set_args = gfc_build_library_function_decl (
3573 get_identifier (PREFIX("set_args")),
3574 void_type_node, 2, integer_type_node,
3575 build_pointer_type (pchar_type_node));
3577 gfor_fndecl_set_fpe = gfc_build_library_function_decl (
3578 get_identifier (PREFIX("set_fpe")),
3579 void_type_node, 1, integer_type_node);
3581 gfor_fndecl_ieee_procedure_entry = gfc_build_library_function_decl (
3582 get_identifier (PREFIX("ieee_procedure_entry")),
3583 void_type_node, 1, pvoid_type_node);
3585 gfor_fndecl_ieee_procedure_exit = gfc_build_library_function_decl (
3586 get_identifier (PREFIX("ieee_procedure_exit")),
3587 void_type_node, 1, pvoid_type_node);
3589 /* Keep the array dimension in sync with the call, later in this file. */
3590 gfor_fndecl_set_options = gfc_build_library_function_decl_with_spec (
3591 get_identifier (PREFIX("set_options")), "..R",
3592 void_type_node, 2, integer_type_node,
3593 build_pointer_type (integer_type_node));
3595 gfor_fndecl_set_convert = gfc_build_library_function_decl (
3596 get_identifier (PREFIX("set_convert")),
3597 void_type_node, 1, integer_type_node);
3599 gfor_fndecl_set_record_marker = gfc_build_library_function_decl (
3600 get_identifier (PREFIX("set_record_marker")),
3601 void_type_node, 1, integer_type_node);
3603 gfor_fndecl_set_max_subrecord_length = gfc_build_library_function_decl (
3604 get_identifier (PREFIX("set_max_subrecord_length")),
3605 void_type_node, 1, integer_type_node);
3607 gfor_fndecl_in_pack = gfc_build_library_function_decl_with_spec (
3608 get_identifier (PREFIX("internal_pack")), ".r",
3609 pvoid_type_node, 1, pvoid_type_node);
3611 gfor_fndecl_in_unpack = gfc_build_library_function_decl_with_spec (
3612 get_identifier (PREFIX("internal_unpack")), ".wR",
3613 void_type_node, 2, pvoid_type_node, pvoid_type_node);
3615 gfor_fndecl_associated = gfc_build_library_function_decl_with_spec (
3616 get_identifier (PREFIX("associated")), ".RR",
3617 integer_type_node, 2, ppvoid_type_node, ppvoid_type_node);
3618 DECL_PURE_P (gfor_fndecl_associated) = 1;
3619 TREE_NOTHROW (gfor_fndecl_associated) = 1;
3621 /* Coarray library calls. */
3622 if (flag_coarray == GFC_FCOARRAY_LIB)
3624 tree pint_type, pppchar_type;
3626 pint_type = build_pointer_type (integer_type_node);
3627 pppchar_type
3628 = build_pointer_type (build_pointer_type (pchar_type_node));
3630 gfor_fndecl_caf_init = gfc_build_library_function_decl (
3631 get_identifier (PREFIX("caf_init")), void_type_node,
3632 2, pint_type, pppchar_type);
3634 gfor_fndecl_caf_finalize = gfc_build_library_function_decl (
3635 get_identifier (PREFIX("caf_finalize")), void_type_node, 0);
3637 gfor_fndecl_caf_this_image = gfc_build_library_function_decl (
3638 get_identifier (PREFIX("caf_this_image")), integer_type_node,
3639 1, integer_type_node);
3641 gfor_fndecl_caf_num_images = gfc_build_library_function_decl (
3642 get_identifier (PREFIX("caf_num_images")), integer_type_node,
3643 2, integer_type_node, integer_type_node);
3645 gfor_fndecl_caf_register = gfc_build_library_function_decl_with_spec (
3646 get_identifier (PREFIX("caf_register")), "RRWWWWR", void_type_node, 7,
3647 size_type_node, integer_type_node, ppvoid_type_node, pvoid_type_node,
3648 pint_type, pchar_type_node, size_type_node);
3650 gfor_fndecl_caf_deregister = gfc_build_library_function_decl_with_spec (
3651 get_identifier (PREFIX("caf_deregister")), "WRWWR", void_type_node, 5,
3652 ppvoid_type_node, integer_type_node, pint_type, pchar_type_node,
3653 size_type_node);
3655 gfor_fndecl_caf_get = gfc_build_library_function_decl_with_spec (
3656 get_identifier (PREFIX("caf_get")), ".R.RRWRRRW", void_type_node, 10,
3657 pvoid_type_node, size_type_node, integer_type_node, pvoid_type_node,
3658 pvoid_type_node, pvoid_type_node, integer_type_node, integer_type_node,
3659 boolean_type_node, pint_type);
3661 gfor_fndecl_caf_send = gfc_build_library_function_decl_with_spec (
3662 get_identifier (PREFIX("caf_send")), ".R.RRRRRRWR", void_type_node, 11,
3663 pvoid_type_node, size_type_node, integer_type_node, pvoid_type_node,
3664 pvoid_type_node, pvoid_type_node, integer_type_node, integer_type_node,
3665 boolean_type_node, pint_type, pvoid_type_node);
3667 gfor_fndecl_caf_sendget = gfc_build_library_function_decl_with_spec (
3668 get_identifier (PREFIX("caf_sendget")), ".R.RRRR.RRRRRR",
3669 void_type_node, 14, pvoid_type_node, size_type_node, integer_type_node,
3670 pvoid_type_node, pvoid_type_node, pvoid_type_node, size_type_node,
3671 integer_type_node, pvoid_type_node, pvoid_type_node, integer_type_node,
3672 integer_type_node, boolean_type_node, integer_type_node);
3674 gfor_fndecl_caf_get_by_ref = gfc_build_library_function_decl_with_spec (
3675 get_identifier (PREFIX("caf_get_by_ref")), ".RWRRRRRWR", void_type_node,
3676 10, pvoid_type_node, integer_type_node, pvoid_type_node,
3677 pvoid_type_node, integer_type_node, integer_type_node,
3678 boolean_type_node, boolean_type_node, pint_type, integer_type_node);
3680 gfor_fndecl_caf_send_by_ref = gfc_build_library_function_decl_with_spec (
3681 get_identifier (PREFIX("caf_send_by_ref")), ".RRRRRRRWR",
3682 void_type_node, 10, pvoid_type_node, integer_type_node, pvoid_type_node,
3683 pvoid_type_node, integer_type_node, integer_type_node,
3684 boolean_type_node, boolean_type_node, pint_type, integer_type_node);
3686 gfor_fndecl_caf_sendget_by_ref
3687 = gfc_build_library_function_decl_with_spec (
3688 get_identifier (PREFIX("caf_sendget_by_ref")), ".RR.RRRRRWWRR",
3689 void_type_node, 13, pvoid_type_node, integer_type_node,
3690 pvoid_type_node, pvoid_type_node, integer_type_node,
3691 pvoid_type_node, integer_type_node, integer_type_node,
3692 boolean_type_node, pint_type, pint_type, integer_type_node,
3693 integer_type_node);
3695 gfor_fndecl_caf_sync_all = gfc_build_library_function_decl_with_spec (
3696 get_identifier (PREFIX("caf_sync_all")), ".WW", void_type_node,
3697 3, pint_type, pchar_type_node, size_type_node);
3699 gfor_fndecl_caf_sync_memory = gfc_build_library_function_decl_with_spec (
3700 get_identifier (PREFIX("caf_sync_memory")), ".WW", void_type_node,
3701 3, pint_type, pchar_type_node, size_type_node);
3703 gfor_fndecl_caf_sync_images = gfc_build_library_function_decl_with_spec (
3704 get_identifier (PREFIX("caf_sync_images")), ".RRWW", void_type_node,
3705 5, integer_type_node, pint_type, pint_type,
3706 pchar_type_node, size_type_node);
3708 gfor_fndecl_caf_error_stop = gfc_build_library_function_decl (
3709 get_identifier (PREFIX("caf_error_stop")),
3710 void_type_node, 1, integer_type_node);
3711 /* CAF's ERROR STOP doesn't return. */
3712 TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop) = 1;
3714 gfor_fndecl_caf_error_stop_str = gfc_build_library_function_decl_with_spec (
3715 get_identifier (PREFIX("caf_error_stop_str")), ".R.",
3716 void_type_node, 2, pchar_type_node, size_type_node);
3717 /* CAF's ERROR STOP doesn't return. */
3718 TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop_str) = 1;
3720 gfor_fndecl_caf_stop_numeric = gfc_build_library_function_decl_with_spec (
3721 get_identifier (PREFIX("caf_stop_numeric")), ".R.",
3722 void_type_node, 1, integer_type_node);
3723 /* CAF's STOP doesn't return. */
3724 TREE_THIS_VOLATILE (gfor_fndecl_caf_stop_numeric) = 1;
3726 gfor_fndecl_caf_stop_str = gfc_build_library_function_decl_with_spec (
3727 get_identifier (PREFIX("caf_stop_str")), ".R.",
3728 void_type_node, 2, pchar_type_node, size_type_node);
3729 /* CAF's STOP doesn't return. */
3730 TREE_THIS_VOLATILE (gfor_fndecl_caf_stop_str) = 1;
3732 gfor_fndecl_caf_atomic_def = gfc_build_library_function_decl_with_spec (
3733 get_identifier (PREFIX("caf_atomic_define")), "R..RW",
3734 void_type_node, 7, pvoid_type_node, size_type_node, integer_type_node,
3735 pvoid_type_node, pint_type, integer_type_node, integer_type_node);
3737 gfor_fndecl_caf_atomic_ref = gfc_build_library_function_decl_with_spec (
3738 get_identifier (PREFIX("caf_atomic_ref")), "R..WW",
3739 void_type_node, 7, pvoid_type_node, size_type_node, integer_type_node,
3740 pvoid_type_node, pint_type, integer_type_node, integer_type_node);
3742 gfor_fndecl_caf_atomic_cas = gfc_build_library_function_decl_with_spec (
3743 get_identifier (PREFIX("caf_atomic_cas")), "R..WRRW",
3744 void_type_node, 9, pvoid_type_node, size_type_node, integer_type_node,
3745 pvoid_type_node, pvoid_type_node, pvoid_type_node, pint_type,
3746 integer_type_node, integer_type_node);
3748 gfor_fndecl_caf_atomic_op = gfc_build_library_function_decl_with_spec (
3749 get_identifier (PREFIX("caf_atomic_op")), ".R..RWW",
3750 void_type_node, 9, integer_type_node, pvoid_type_node, size_type_node,
3751 integer_type_node, pvoid_type_node, pvoid_type_node, pint_type,
3752 integer_type_node, integer_type_node);
3754 gfor_fndecl_caf_lock = gfc_build_library_function_decl_with_spec (
3755 get_identifier (PREFIX("caf_lock")), "R..WWW",
3756 void_type_node, 7, pvoid_type_node, size_type_node, integer_type_node,
3757 pint_type, pint_type, pchar_type_node, size_type_node);
3759 gfor_fndecl_caf_unlock = gfc_build_library_function_decl_with_spec (
3760 get_identifier (PREFIX("caf_unlock")), "R..WW",
3761 void_type_node, 6, pvoid_type_node, size_type_node, integer_type_node,
3762 pint_type, pchar_type_node, size_type_node);
3764 gfor_fndecl_caf_event_post = gfc_build_library_function_decl_with_spec (
3765 get_identifier (PREFIX("caf_event_post")), "R..WW",
3766 void_type_node, 6, pvoid_type_node, size_type_node, integer_type_node,
3767 pint_type, pchar_type_node, size_type_node);
3769 gfor_fndecl_caf_event_wait = gfc_build_library_function_decl_with_spec (
3770 get_identifier (PREFIX("caf_event_wait")), "R..WW",
3771 void_type_node, 6, pvoid_type_node, size_type_node, integer_type_node,
3772 pint_type, pchar_type_node, size_type_node);
3774 gfor_fndecl_caf_event_query = gfc_build_library_function_decl_with_spec (
3775 get_identifier (PREFIX("caf_event_query")), "R..WW",
3776 void_type_node, 5, pvoid_type_node, size_type_node, integer_type_node,
3777 pint_type, pint_type);
3779 gfor_fndecl_caf_fail_image = gfc_build_library_function_decl (
3780 get_identifier (PREFIX("caf_fail_image")), void_type_node, 0);
3781 /* CAF's FAIL doesn't return. */
3782 TREE_THIS_VOLATILE (gfor_fndecl_caf_fail_image) = 1;
3784 gfor_fndecl_caf_failed_images
3785 = gfc_build_library_function_decl_with_spec (
3786 get_identifier (PREFIX("caf_failed_images")), "WRR",
3787 void_type_node, 3, pvoid_type_node, ppvoid_type_node,
3788 integer_type_node);
3790 gfor_fndecl_caf_form_team
3791 = gfc_build_library_function_decl_with_spec (
3792 get_identifier (PREFIX("caf_form_team")), "RWR",
3793 void_type_node, 3, integer_type_node, ppvoid_type_node,
3794 integer_type_node);
3796 gfor_fndecl_caf_change_team
3797 = gfc_build_library_function_decl_with_spec (
3798 get_identifier (PREFIX("caf_change_team")), "RR",
3799 void_type_node, 2, ppvoid_type_node,
3800 integer_type_node);
3802 gfor_fndecl_caf_end_team
3803 = gfc_build_library_function_decl (
3804 get_identifier (PREFIX("caf_end_team")), void_type_node, 0);
3806 gfor_fndecl_caf_get_team
3807 = gfc_build_library_function_decl_with_spec (
3808 get_identifier (PREFIX("caf_get_team")), "R",
3809 void_type_node, 1, integer_type_node);
3811 gfor_fndecl_caf_sync_team
3812 = gfc_build_library_function_decl_with_spec (
3813 get_identifier (PREFIX("caf_sync_team")), "RR",
3814 void_type_node, 2, ppvoid_type_node,
3815 integer_type_node);
3817 gfor_fndecl_caf_team_number
3818 = gfc_build_library_function_decl_with_spec (
3819 get_identifier (PREFIX("caf_team_number")), "R",
3820 integer_type_node, 1, integer_type_node);
3822 gfor_fndecl_caf_image_status
3823 = gfc_build_library_function_decl_with_spec (
3824 get_identifier (PREFIX("caf_image_status")), "RR",
3825 integer_type_node, 2, integer_type_node, ppvoid_type_node);
3827 gfor_fndecl_caf_stopped_images
3828 = gfc_build_library_function_decl_with_spec (
3829 get_identifier (PREFIX("caf_stopped_images")), "WRR",
3830 void_type_node, 3, pvoid_type_node, ppvoid_type_node,
3831 integer_type_node);
3833 gfor_fndecl_co_broadcast = gfc_build_library_function_decl_with_spec (
3834 get_identifier (PREFIX("caf_co_broadcast")), "W.WW",
3835 void_type_node, 5, pvoid_type_node, integer_type_node,
3836 pint_type, pchar_type_node, size_type_node);
3838 gfor_fndecl_co_max = gfc_build_library_function_decl_with_spec (
3839 get_identifier (PREFIX("caf_co_max")), "W.WW",
3840 void_type_node, 6, pvoid_type_node, integer_type_node,
3841 pint_type, pchar_type_node, integer_type_node, size_type_node);
3843 gfor_fndecl_co_min = gfc_build_library_function_decl_with_spec (
3844 get_identifier (PREFIX("caf_co_min")), "W.WW",
3845 void_type_node, 6, pvoid_type_node, integer_type_node,
3846 pint_type, pchar_type_node, integer_type_node, size_type_node);
3848 gfor_fndecl_co_reduce = gfc_build_library_function_decl_with_spec (
3849 get_identifier (PREFIX("caf_co_reduce")), "W.R.WW",
3850 void_type_node, 8, pvoid_type_node,
3851 build_pointer_type (build_varargs_function_type_list (void_type_node,
3852 NULL_TREE)),
3853 integer_type_node, integer_type_node, pint_type, pchar_type_node,
3854 integer_type_node, size_type_node);
3856 gfor_fndecl_co_sum = gfc_build_library_function_decl_with_spec (
3857 get_identifier (PREFIX("caf_co_sum")), "W.WW",
3858 void_type_node, 5, pvoid_type_node, integer_type_node,
3859 pint_type, pchar_type_node, size_type_node);
3861 gfor_fndecl_caf_is_present = gfc_build_library_function_decl_with_spec (
3862 get_identifier (PREFIX("caf_is_present")), "RRR",
3863 integer_type_node, 3, pvoid_type_node, integer_type_node,
3864 pvoid_type_node);
3867 gfc_build_intrinsic_function_decls ();
3868 gfc_build_intrinsic_lib_fndecls ();
3869 gfc_build_io_library_fndecls ();
3873 /* Evaluate the length of dummy character variables. */
3875 static void
3876 gfc_trans_dummy_character (gfc_symbol *sym, gfc_charlen *cl,
3877 gfc_wrapped_block *block)
3879 stmtblock_t init;
3881 gfc_finish_decl (cl->backend_decl);
3883 gfc_start_block (&init);
3885 /* Evaluate the string length expression. */
3886 gfc_conv_string_length (cl, NULL, &init);
3888 gfc_trans_vla_type_sizes (sym, &init);
3890 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
3894 /* Allocate and cleanup an automatic character variable. */
3896 static void
3897 gfc_trans_auto_character_variable (gfc_symbol * sym, gfc_wrapped_block * block)
3899 stmtblock_t init;
3900 tree decl;
3901 tree tmp;
3903 gcc_assert (sym->backend_decl);
3904 gcc_assert (sym->ts.u.cl && sym->ts.u.cl->length);
3906 gfc_init_block (&init);
3908 /* Evaluate the string length expression. */
3909 gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
3911 gfc_trans_vla_type_sizes (sym, &init);
3913 decl = sym->backend_decl;
3915 /* Emit a DECL_EXPR for this variable, which will cause the
3916 gimplifier to allocate storage, and all that good stuff. */
3917 tmp = fold_build1_loc (input_location, DECL_EXPR, TREE_TYPE (decl), decl);
3918 gfc_add_expr_to_block (&init, tmp);
3920 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
3923 /* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
3925 static void
3926 gfc_trans_assign_aux_var (gfc_symbol * sym, gfc_wrapped_block * block)
3928 stmtblock_t init;
3930 gcc_assert (sym->backend_decl);
3931 gfc_start_block (&init);
3933 /* Set the initial value to length. See the comments in
3934 function gfc_add_assign_aux_vars in this file. */
3935 gfc_add_modify (&init, GFC_DECL_STRING_LEN (sym->backend_decl),
3936 build_int_cst (gfc_charlen_type_node, -2));
3938 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
3941 static void
3942 gfc_trans_vla_one_sizepos (tree *tp, stmtblock_t *body)
3944 tree t = *tp, var, val;
3946 if (t == NULL || t == error_mark_node)
3947 return;
3948 if (TREE_CONSTANT (t) || DECL_P (t))
3949 return;
3951 if (TREE_CODE (t) == SAVE_EXPR)
3953 if (SAVE_EXPR_RESOLVED_P (t))
3955 *tp = TREE_OPERAND (t, 0);
3956 return;
3958 val = TREE_OPERAND (t, 0);
3960 else
3961 val = t;
3963 var = gfc_create_var_np (TREE_TYPE (t), NULL);
3964 gfc_add_decl_to_function (var);
3965 gfc_add_modify (body, var, unshare_expr (val));
3966 if (TREE_CODE (t) == SAVE_EXPR)
3967 TREE_OPERAND (t, 0) = var;
3968 *tp = var;
3971 static void
3972 gfc_trans_vla_type_sizes_1 (tree type, stmtblock_t *body)
3974 tree t;
3976 if (type == NULL || type == error_mark_node)
3977 return;
3979 type = TYPE_MAIN_VARIANT (type);
3981 if (TREE_CODE (type) == INTEGER_TYPE)
3983 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type), body);
3984 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type), body);
3986 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
3988 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
3989 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
3992 else if (TREE_CODE (type) == ARRAY_TYPE)
3994 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type), body);
3995 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type), body);
3996 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type), body);
3997 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type), body);
3999 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
4001 TYPE_SIZE (t) = TYPE_SIZE (type);
4002 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
4007 /* Make sure all type sizes and array domains are either constant,
4008 or variable or parameter decls. This is a simplified variant
4009 of gimplify_type_sizes, but we can't use it here, as none of the
4010 variables in the expressions have been gimplified yet.
4011 As type sizes and domains for various variable length arrays
4012 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
4013 time, without this routine gimplify_type_sizes in the middle-end
4014 could result in the type sizes being gimplified earlier than where
4015 those variables are initialized. */
4017 void
4018 gfc_trans_vla_type_sizes (gfc_symbol *sym, stmtblock_t *body)
4020 tree type = TREE_TYPE (sym->backend_decl);
4022 if (TREE_CODE (type) == FUNCTION_TYPE
4023 && (sym->attr.function || sym->attr.result || sym->attr.entry))
4025 if (! current_fake_result_decl)
4026 return;
4028 type = TREE_TYPE (TREE_VALUE (current_fake_result_decl));
4031 while (POINTER_TYPE_P (type))
4032 type = TREE_TYPE (type);
4034 if (GFC_DESCRIPTOR_TYPE_P (type))
4036 tree etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
4038 while (POINTER_TYPE_P (etype))
4039 etype = TREE_TYPE (etype);
4041 gfc_trans_vla_type_sizes_1 (etype, body);
4044 gfc_trans_vla_type_sizes_1 (type, body);
4048 /* Initialize a derived type by building an lvalue from the symbol
4049 and using trans_assignment to do the work. Set dealloc to false
4050 if no deallocation prior the assignment is needed. */
4051 void
4052 gfc_init_default_dt (gfc_symbol * sym, stmtblock_t * block, bool dealloc)
4054 gfc_expr *e;
4055 tree tmp;
4056 tree present;
4058 gcc_assert (block);
4060 /* Initialization of PDTs is done elsewhere. */
4061 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.pdt_type)
4062 return;
4064 gcc_assert (!sym->attr.allocatable);
4065 gfc_set_sym_referenced (sym);
4066 e = gfc_lval_expr_from_sym (sym);
4067 tmp = gfc_trans_assignment (e, sym->value, false, dealloc);
4068 if (sym->attr.dummy && (sym->attr.optional
4069 || sym->ns->proc_name->attr.entry_master))
4071 present = gfc_conv_expr_present (sym);
4072 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
4073 tmp, build_empty_stmt (input_location));
4075 gfc_add_expr_to_block (block, tmp);
4076 gfc_free_expr (e);
4080 /* Initialize INTENT(OUT) derived type dummies. As well as giving
4081 them their default initializer, if they do not have allocatable
4082 components, they have their allocatable components deallocated. */
4084 static void
4085 init_intent_out_dt (gfc_symbol * proc_sym, gfc_wrapped_block * block)
4087 stmtblock_t init;
4088 gfc_formal_arglist *f;
4089 tree tmp;
4090 tree present;
4092 gfc_init_block (&init);
4093 for (f = gfc_sym_get_dummy_args (proc_sym); f; f = f->next)
4094 if (f->sym && f->sym->attr.intent == INTENT_OUT
4095 && !f->sym->attr.pointer
4096 && f->sym->ts.type == BT_DERIVED)
4098 tmp = NULL_TREE;
4100 /* Note: Allocatables are excluded as they are already handled
4101 by the caller. */
4102 if (!f->sym->attr.allocatable
4103 && gfc_is_finalizable (f->sym->ts.u.derived, NULL))
4105 stmtblock_t block;
4106 gfc_expr *e;
4108 gfc_init_block (&block);
4109 f->sym->attr.referenced = 1;
4110 e = gfc_lval_expr_from_sym (f->sym);
4111 gfc_add_finalizer_call (&block, e);
4112 gfc_free_expr (e);
4113 tmp = gfc_finish_block (&block);
4116 if (tmp == NULL_TREE && !f->sym->attr.allocatable
4117 && f->sym->ts.u.derived->attr.alloc_comp && !f->sym->value)
4118 tmp = gfc_deallocate_alloc_comp (f->sym->ts.u.derived,
4119 f->sym->backend_decl,
4120 f->sym->as ? f->sym->as->rank : 0);
4122 if (tmp != NULL_TREE && (f->sym->attr.optional
4123 || f->sym->ns->proc_name->attr.entry_master))
4125 present = gfc_conv_expr_present (f->sym);
4126 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
4127 present, tmp, build_empty_stmt (input_location));
4130 if (tmp != NULL_TREE)
4131 gfc_add_expr_to_block (&init, tmp);
4132 else if (f->sym->value && !f->sym->attr.allocatable)
4133 gfc_init_default_dt (f->sym, &init, true);
4135 else if (f->sym && f->sym->attr.intent == INTENT_OUT
4136 && f->sym->ts.type == BT_CLASS
4137 && !CLASS_DATA (f->sym)->attr.class_pointer
4138 && !CLASS_DATA (f->sym)->attr.allocatable)
4140 stmtblock_t block;
4141 gfc_expr *e;
4143 gfc_init_block (&block);
4144 f->sym->attr.referenced = 1;
4145 e = gfc_lval_expr_from_sym (f->sym);
4146 gfc_add_finalizer_call (&block, e);
4147 gfc_free_expr (e);
4148 tmp = gfc_finish_block (&block);
4150 if (f->sym->attr.optional || f->sym->ns->proc_name->attr.entry_master)
4152 present = gfc_conv_expr_present (f->sym);
4153 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
4154 present, tmp,
4155 build_empty_stmt (input_location));
4158 gfc_add_expr_to_block (&init, tmp);
4161 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
4165 /* Helper function to manage deferred string lengths. */
4167 static tree
4168 gfc_null_and_pass_deferred_len (gfc_symbol *sym, stmtblock_t *init,
4169 locus *loc)
4171 tree tmp;
4173 /* Character length passed by reference. */
4174 tmp = sym->ts.u.cl->passed_length;
4175 tmp = build_fold_indirect_ref_loc (input_location, tmp);
4176 tmp = fold_convert (gfc_charlen_type_node, tmp);
4178 if (!sym->attr.dummy || sym->attr.intent == INTENT_OUT)
4179 /* Zero the string length when entering the scope. */
4180 gfc_add_modify (init, sym->ts.u.cl->backend_decl,
4181 build_int_cst (gfc_charlen_type_node, 0));
4182 else
4184 tree tmp2;
4186 tmp2 = fold_build2_loc (input_location, MODIFY_EXPR,
4187 gfc_charlen_type_node,
4188 sym->ts.u.cl->backend_decl, tmp);
4189 if (sym->attr.optional)
4191 tree present = gfc_conv_expr_present (sym);
4192 tmp2 = build3_loc (input_location, COND_EXPR,
4193 void_type_node, present, tmp2,
4194 build_empty_stmt (input_location));
4196 gfc_add_expr_to_block (init, tmp2);
4199 gfc_restore_backend_locus (loc);
4201 /* Pass the final character length back. */
4202 if (sym->attr.intent != INTENT_IN)
4204 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
4205 gfc_charlen_type_node, tmp,
4206 sym->ts.u.cl->backend_decl);
4207 if (sym->attr.optional)
4209 tree present = gfc_conv_expr_present (sym);
4210 tmp = build3_loc (input_location, COND_EXPR,
4211 void_type_node, present, tmp,
4212 build_empty_stmt (input_location));
4215 else
4216 tmp = NULL_TREE;
4218 return tmp;
4222 /* Get the result expression for a procedure. */
4224 static tree
4225 get_proc_result (gfc_symbol* sym)
4227 if (sym->attr.subroutine || sym == sym->result)
4229 if (current_fake_result_decl != NULL)
4230 return TREE_VALUE (current_fake_result_decl);
4232 return NULL_TREE;
4235 return sym->result->backend_decl;
4239 /* Generate function entry and exit code, and add it to the function body.
4240 This includes:
4241 Allocation and initialization of array variables.
4242 Allocation of character string variables.
4243 Initialization and possibly repacking of dummy arrays.
4244 Initialization of ASSIGN statement auxiliary variable.
4245 Initialization of ASSOCIATE names.
4246 Automatic deallocation. */
4248 void
4249 gfc_trans_deferred_vars (gfc_symbol * proc_sym, gfc_wrapped_block * block)
4251 locus loc;
4252 gfc_symbol *sym;
4253 gfc_formal_arglist *f;
4254 stmtblock_t tmpblock;
4255 bool seen_trans_deferred_array = false;
4256 bool is_pdt_type = false;
4257 tree tmp = NULL;
4258 gfc_expr *e;
4259 gfc_se se;
4260 stmtblock_t init;
4262 /* Deal with implicit return variables. Explicit return variables will
4263 already have been added. */
4264 if (gfc_return_by_reference (proc_sym) && proc_sym->result == proc_sym)
4266 if (!current_fake_result_decl)
4268 gfc_entry_list *el = NULL;
4269 if (proc_sym->attr.entry_master)
4271 for (el = proc_sym->ns->entries; el; el = el->next)
4272 if (el->sym != el->sym->result)
4273 break;
4275 /* TODO: move to the appropriate place in resolve.c. */
4276 if (warn_return_type > 0 && el == NULL)
4277 gfc_warning (OPT_Wreturn_type,
4278 "Return value of function %qs at %L not set",
4279 proc_sym->name, &proc_sym->declared_at);
4281 else if (proc_sym->as)
4283 tree result = TREE_VALUE (current_fake_result_decl);
4284 gfc_save_backend_locus (&loc);
4285 gfc_set_backend_locus (&proc_sym->declared_at);
4286 gfc_trans_dummy_array_bias (proc_sym, result, block);
4288 /* An automatic character length, pointer array result. */
4289 if (proc_sym->ts.type == BT_CHARACTER
4290 && VAR_P (proc_sym->ts.u.cl->backend_decl))
4292 tmp = NULL;
4293 if (proc_sym->ts.deferred)
4295 gfc_start_block (&init);
4296 tmp = gfc_null_and_pass_deferred_len (proc_sym, &init, &loc);
4297 gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
4299 else
4300 gfc_trans_dummy_character (proc_sym, proc_sym->ts.u.cl, block);
4303 else if (proc_sym->ts.type == BT_CHARACTER)
4305 if (proc_sym->ts.deferred)
4307 tmp = NULL;
4308 gfc_save_backend_locus (&loc);
4309 gfc_set_backend_locus (&proc_sym->declared_at);
4310 gfc_start_block (&init);
4311 /* Zero the string length on entry. */
4312 gfc_add_modify (&init, proc_sym->ts.u.cl->backend_decl,
4313 build_int_cst (gfc_charlen_type_node, 0));
4314 /* Null the pointer. */
4315 e = gfc_lval_expr_from_sym (proc_sym);
4316 gfc_init_se (&se, NULL);
4317 se.want_pointer = 1;
4318 gfc_conv_expr (&se, e);
4319 gfc_free_expr (e);
4320 tmp = se.expr;
4321 gfc_add_modify (&init, tmp,
4322 fold_convert (TREE_TYPE (se.expr),
4323 null_pointer_node));
4324 gfc_restore_backend_locus (&loc);
4326 /* Pass back the string length on exit. */
4327 tmp = proc_sym->ts.u.cl->backend_decl;
4328 if (TREE_CODE (tmp) != INDIRECT_REF
4329 && proc_sym->ts.u.cl->passed_length)
4331 tmp = proc_sym->ts.u.cl->passed_length;
4332 tmp = build_fold_indirect_ref_loc (input_location, tmp);
4333 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
4334 TREE_TYPE (tmp), tmp,
4335 fold_convert
4336 (TREE_TYPE (tmp),
4337 proc_sym->ts.u.cl->backend_decl));
4339 else
4340 tmp = NULL_TREE;
4342 gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
4344 else if (VAR_P (proc_sym->ts.u.cl->backend_decl))
4345 gfc_trans_dummy_character (proc_sym, proc_sym->ts.u.cl, block);
4347 else
4348 gcc_assert (flag_f2c && proc_sym->ts.type == BT_COMPLEX);
4350 else if (proc_sym == proc_sym->result && IS_CLASS_ARRAY (proc_sym))
4352 /* Nullify explicit return class arrays on entry. */
4353 tree type;
4354 tmp = get_proc_result (proc_sym);
4355 if (tmp && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
4357 gfc_start_block (&init);
4358 tmp = gfc_class_data_get (tmp);
4359 type = TREE_TYPE (gfc_conv_descriptor_data_get (tmp));
4360 gfc_conv_descriptor_data_set (&init, tmp, build_int_cst (type, 0));
4361 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
4366 /* Initialize the INTENT(OUT) derived type dummy arguments. This
4367 should be done here so that the offsets and lbounds of arrays
4368 are available. */
4369 gfc_save_backend_locus (&loc);
4370 gfc_set_backend_locus (&proc_sym->declared_at);
4371 init_intent_out_dt (proc_sym, block);
4372 gfc_restore_backend_locus (&loc);
4374 for (sym = proc_sym->tlink; sym != proc_sym; sym = sym->tlink)
4376 bool alloc_comp_or_fini = (sym->ts.type == BT_DERIVED)
4377 && (sym->ts.u.derived->attr.alloc_comp
4378 || gfc_is_finalizable (sym->ts.u.derived,
4379 NULL));
4380 if (sym->assoc)
4381 continue;
4383 if (sym->ts.type == BT_DERIVED
4384 && sym->ts.u.derived
4385 && sym->ts.u.derived->attr.pdt_type)
4387 is_pdt_type = true;
4388 gfc_init_block (&tmpblock);
4389 if (!(sym->attr.dummy
4390 || sym->attr.pointer
4391 || sym->attr.allocatable))
4393 tmp = gfc_allocate_pdt_comp (sym->ts.u.derived,
4394 sym->backend_decl,
4395 sym->as ? sym->as->rank : 0,
4396 sym->param_list);
4397 gfc_add_expr_to_block (&tmpblock, tmp);
4398 if (!sym->attr.result)
4399 tmp = gfc_deallocate_pdt_comp (sym->ts.u.derived,
4400 sym->backend_decl,
4401 sym->as ? sym->as->rank : 0);
4402 else
4403 tmp = NULL_TREE;
4404 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), tmp);
4406 else if (sym->attr.dummy)
4408 tmp = gfc_check_pdt_dummy (sym->ts.u.derived,
4409 sym->backend_decl,
4410 sym->as ? sym->as->rank : 0,
4411 sym->param_list);
4412 gfc_add_expr_to_block (&tmpblock, tmp);
4413 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), NULL);
4416 else if (sym->ts.type == BT_CLASS
4417 && CLASS_DATA (sym)->ts.u.derived
4418 && CLASS_DATA (sym)->ts.u.derived->attr.pdt_type)
4420 gfc_component *data = CLASS_DATA (sym);
4421 is_pdt_type = true;
4422 gfc_init_block (&tmpblock);
4423 if (!(sym->attr.dummy
4424 || CLASS_DATA (sym)->attr.pointer
4425 || CLASS_DATA (sym)->attr.allocatable))
4427 tmp = gfc_class_data_get (sym->backend_decl);
4428 tmp = gfc_allocate_pdt_comp (data->ts.u.derived, tmp,
4429 data->as ? data->as->rank : 0,
4430 sym->param_list);
4431 gfc_add_expr_to_block (&tmpblock, tmp);
4432 tmp = gfc_class_data_get (sym->backend_decl);
4433 if (!sym->attr.result)
4434 tmp = gfc_deallocate_pdt_comp (data->ts.u.derived, tmp,
4435 data->as ? data->as->rank : 0);
4436 else
4437 tmp = NULL_TREE;
4438 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), tmp);
4440 else if (sym->attr.dummy)
4442 tmp = gfc_class_data_get (sym->backend_decl);
4443 tmp = gfc_check_pdt_dummy (data->ts.u.derived, tmp,
4444 data->as ? data->as->rank : 0,
4445 sym->param_list);
4446 gfc_add_expr_to_block (&tmpblock, tmp);
4447 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), NULL);
4451 if (sym->attr.pointer && sym->attr.dimension
4452 && sym->attr.save == SAVE_NONE
4453 && !sym->attr.use_assoc
4454 && !sym->attr.host_assoc
4455 && !sym->attr.dummy
4456 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (sym->backend_decl)))
4458 gfc_init_block (&tmpblock);
4459 gfc_conv_descriptor_span_set (&tmpblock, sym->backend_decl,
4460 build_int_cst (gfc_array_index_type, 0));
4461 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock),
4462 NULL_TREE);
4465 if (sym->ts.type == BT_CLASS
4466 && (sym->attr.save || flag_max_stack_var_size == 0)
4467 && CLASS_DATA (sym)->attr.allocatable)
4469 tree vptr;
4471 if (UNLIMITED_POLY (sym))
4472 vptr = null_pointer_node;
4473 else
4475 gfc_symbol *vsym;
4476 vsym = gfc_find_derived_vtab (sym->ts.u.derived);
4477 vptr = gfc_get_symbol_decl (vsym);
4478 vptr = gfc_build_addr_expr (NULL, vptr);
4481 if (CLASS_DATA (sym)->attr.dimension
4482 || (CLASS_DATA (sym)->attr.codimension
4483 && flag_coarray != GFC_FCOARRAY_LIB))
4485 tmp = gfc_class_data_get (sym->backend_decl);
4486 tmp = gfc_build_null_descriptor (TREE_TYPE (tmp));
4488 else
4489 tmp = null_pointer_node;
4491 DECL_INITIAL (sym->backend_decl)
4492 = gfc_class_set_static_fields (sym->backend_decl, vptr, tmp);
4493 TREE_CONSTANT (DECL_INITIAL (sym->backend_decl)) = 1;
4495 else if ((sym->attr.dimension || sym->attr.codimension
4496 || (IS_CLASS_ARRAY (sym) && !CLASS_DATA (sym)->attr.allocatable)))
4498 bool is_classarray = IS_CLASS_ARRAY (sym);
4499 symbol_attribute *array_attr;
4500 gfc_array_spec *as;
4501 array_type type_of_array;
4503 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
4504 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
4505 /* Assumed-size Cray pointees need to be treated as AS_EXPLICIT. */
4506 type_of_array = as->type;
4507 if (type_of_array == AS_ASSUMED_SIZE && as->cp_was_assumed)
4508 type_of_array = AS_EXPLICIT;
4509 switch (type_of_array)
4511 case AS_EXPLICIT:
4512 if (sym->attr.dummy || sym->attr.result)
4513 gfc_trans_dummy_array_bias (sym, sym->backend_decl, block);
4514 /* Allocatable and pointer arrays need to processed
4515 explicitly. */
4516 else if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
4517 || (sym->ts.type == BT_CLASS
4518 && CLASS_DATA (sym)->attr.class_pointer)
4519 || array_attr->allocatable)
4521 if (TREE_STATIC (sym->backend_decl))
4523 gfc_save_backend_locus (&loc);
4524 gfc_set_backend_locus (&sym->declared_at);
4525 gfc_trans_static_array_pointer (sym);
4526 gfc_restore_backend_locus (&loc);
4528 else
4530 seen_trans_deferred_array = true;
4531 gfc_trans_deferred_array (sym, block);
4534 else if (sym->attr.codimension
4535 && TREE_STATIC (sym->backend_decl))
4537 gfc_init_block (&tmpblock);
4538 gfc_trans_array_cobounds (TREE_TYPE (sym->backend_decl),
4539 &tmpblock, sym);
4540 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock),
4541 NULL_TREE);
4542 continue;
4544 else
4546 gfc_save_backend_locus (&loc);
4547 gfc_set_backend_locus (&sym->declared_at);
4549 if (alloc_comp_or_fini)
4551 seen_trans_deferred_array = true;
4552 gfc_trans_deferred_array (sym, block);
4554 else if (sym->ts.type == BT_DERIVED
4555 && sym->value
4556 && !sym->attr.data
4557 && sym->attr.save == SAVE_NONE)
4559 gfc_start_block (&tmpblock);
4560 gfc_init_default_dt (sym, &tmpblock, false);
4561 gfc_add_init_cleanup (block,
4562 gfc_finish_block (&tmpblock),
4563 NULL_TREE);
4566 gfc_trans_auto_array_allocation (sym->backend_decl,
4567 sym, block);
4568 gfc_restore_backend_locus (&loc);
4570 break;
4572 case AS_ASSUMED_SIZE:
4573 /* Must be a dummy parameter. */
4574 gcc_assert (sym->attr.dummy || as->cp_was_assumed);
4576 /* We should always pass assumed size arrays the g77 way. */
4577 if (sym->attr.dummy)
4578 gfc_trans_g77_array (sym, block);
4579 break;
4581 case AS_ASSUMED_SHAPE:
4582 /* Must be a dummy parameter. */
4583 gcc_assert (sym->attr.dummy);
4585 gfc_trans_dummy_array_bias (sym, sym->backend_decl, block);
4586 break;
4588 case AS_ASSUMED_RANK:
4589 case AS_DEFERRED:
4590 seen_trans_deferred_array = true;
4591 gfc_trans_deferred_array (sym, block);
4592 if (sym->ts.type == BT_CHARACTER && sym->ts.deferred
4593 && sym->attr.result)
4595 gfc_start_block (&init);
4596 gfc_save_backend_locus (&loc);
4597 gfc_set_backend_locus (&sym->declared_at);
4598 tmp = gfc_null_and_pass_deferred_len (sym, &init, &loc);
4599 gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
4601 break;
4603 default:
4604 gcc_unreachable ();
4606 if (alloc_comp_or_fini && !seen_trans_deferred_array)
4607 gfc_trans_deferred_array (sym, block);
4609 else if ((!sym->attr.dummy || sym->ts.deferred)
4610 && (sym->ts.type == BT_CLASS
4611 && CLASS_DATA (sym)->attr.class_pointer))
4612 continue;
4613 else if ((!sym->attr.dummy || sym->ts.deferred)
4614 && (sym->attr.allocatable
4615 || (sym->attr.pointer && sym->attr.result)
4616 || (sym->ts.type == BT_CLASS
4617 && CLASS_DATA (sym)->attr.allocatable)))
4619 if (!sym->attr.save && flag_max_stack_var_size != 0)
4621 tree descriptor = NULL_TREE;
4623 gfc_save_backend_locus (&loc);
4624 gfc_set_backend_locus (&sym->declared_at);
4625 gfc_start_block (&init);
4627 if (sym->ts.type == BT_CHARACTER
4628 && sym->attr.allocatable
4629 && !sym->attr.dimension
4630 && sym->ts.u.cl && sym->ts.u.cl->length
4631 && sym->ts.u.cl->length->expr_type == EXPR_VARIABLE)
4632 gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
4634 if (!sym->attr.pointer)
4636 /* Nullify and automatic deallocation of allocatable
4637 scalars. */
4638 e = gfc_lval_expr_from_sym (sym);
4639 if (sym->ts.type == BT_CLASS)
4640 gfc_add_data_component (e);
4642 gfc_init_se (&se, NULL);
4643 if (sym->ts.type != BT_CLASS
4644 || sym->ts.u.derived->attr.dimension
4645 || sym->ts.u.derived->attr.codimension)
4647 se.want_pointer = 1;
4648 gfc_conv_expr (&se, e);
4650 else if (sym->ts.type == BT_CLASS
4651 && !CLASS_DATA (sym)->attr.dimension
4652 && !CLASS_DATA (sym)->attr.codimension)
4654 se.want_pointer = 1;
4655 gfc_conv_expr (&se, e);
4657 else
4659 se.descriptor_only = 1;
4660 gfc_conv_expr (&se, e);
4661 descriptor = se.expr;
4662 se.expr = gfc_conv_descriptor_data_addr (se.expr);
4663 se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
4665 gfc_free_expr (e);
4667 if (!sym->attr.dummy || sym->attr.intent == INTENT_OUT)
4669 /* Nullify when entering the scope. */
4670 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
4671 TREE_TYPE (se.expr), se.expr,
4672 fold_convert (TREE_TYPE (se.expr),
4673 null_pointer_node));
4674 if (sym->attr.optional)
4676 tree present = gfc_conv_expr_present (sym);
4677 tmp = build3_loc (input_location, COND_EXPR,
4678 void_type_node, present, tmp,
4679 build_empty_stmt (input_location));
4681 gfc_add_expr_to_block (&init, tmp);
4685 if ((sym->attr.dummy || sym->attr.result)
4686 && sym->ts.type == BT_CHARACTER
4687 && sym->ts.deferred
4688 && sym->ts.u.cl->passed_length)
4689 tmp = gfc_null_and_pass_deferred_len (sym, &init, &loc);
4690 else
4692 gfc_restore_backend_locus (&loc);
4693 tmp = NULL_TREE;
4696 /* Deallocate when leaving the scope. Nullifying is not
4697 needed. */
4698 if (!sym->attr.result && !sym->attr.dummy && !sym->attr.pointer
4699 && !sym->ns->proc_name->attr.is_main_program)
4701 if (sym->ts.type == BT_CLASS
4702 && CLASS_DATA (sym)->attr.codimension)
4703 tmp = gfc_deallocate_with_status (descriptor, NULL_TREE,
4704 NULL_TREE, NULL_TREE,
4705 NULL_TREE, true, NULL,
4706 GFC_CAF_COARRAY_ANALYZE);
4707 else
4709 gfc_expr *expr = gfc_lval_expr_from_sym (sym);
4710 tmp = gfc_deallocate_scalar_with_status (se.expr,
4711 NULL_TREE,
4712 NULL_TREE,
4713 true, expr,
4714 sym->ts);
4715 gfc_free_expr (expr);
4719 if (sym->ts.type == BT_CLASS)
4721 /* Initialize _vptr to declared type. */
4722 gfc_symbol *vtab;
4723 tree rhs;
4725 gfc_save_backend_locus (&loc);
4726 gfc_set_backend_locus (&sym->declared_at);
4727 e = gfc_lval_expr_from_sym (sym);
4728 gfc_add_vptr_component (e);
4729 gfc_init_se (&se, NULL);
4730 se.want_pointer = 1;
4731 gfc_conv_expr (&se, e);
4732 gfc_free_expr (e);
4733 if (UNLIMITED_POLY (sym))
4734 rhs = build_int_cst (TREE_TYPE (se.expr), 0);
4735 else
4737 vtab = gfc_find_derived_vtab (sym->ts.u.derived);
4738 rhs = gfc_build_addr_expr (TREE_TYPE (se.expr),
4739 gfc_get_symbol_decl (vtab));
4741 gfc_add_modify (&init, se.expr, rhs);
4742 gfc_restore_backend_locus (&loc);
4745 gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
4748 else if (sym->ts.type == BT_CHARACTER && sym->ts.deferred)
4750 tree tmp = NULL;
4751 stmtblock_t init;
4753 /* If we get to here, all that should be left are pointers. */
4754 gcc_assert (sym->attr.pointer);
4756 if (sym->attr.dummy)
4758 gfc_start_block (&init);
4759 gfc_save_backend_locus (&loc);
4760 gfc_set_backend_locus (&sym->declared_at);
4761 tmp = gfc_null_and_pass_deferred_len (sym, &init, &loc);
4762 gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
4765 else if (sym->ts.deferred)
4766 gfc_fatal_error ("Deferred type parameter not yet supported");
4767 else if (alloc_comp_or_fini)
4768 gfc_trans_deferred_array (sym, block);
4769 else if (sym->ts.type == BT_CHARACTER)
4771 gfc_save_backend_locus (&loc);
4772 gfc_set_backend_locus (&sym->declared_at);
4773 if (sym->attr.dummy || sym->attr.result)
4774 gfc_trans_dummy_character (sym, sym->ts.u.cl, block);
4775 else
4776 gfc_trans_auto_character_variable (sym, block);
4777 gfc_restore_backend_locus (&loc);
4779 else if (sym->attr.assign)
4781 gfc_save_backend_locus (&loc);
4782 gfc_set_backend_locus (&sym->declared_at);
4783 gfc_trans_assign_aux_var (sym, block);
4784 gfc_restore_backend_locus (&loc);
4786 else if (sym->ts.type == BT_DERIVED
4787 && sym->value
4788 && !sym->attr.data
4789 && sym->attr.save == SAVE_NONE)
4791 gfc_start_block (&tmpblock);
4792 gfc_init_default_dt (sym, &tmpblock, false);
4793 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock),
4794 NULL_TREE);
4796 else if (!(UNLIMITED_POLY(sym)) && !is_pdt_type)
4797 gcc_unreachable ();
4800 gfc_init_block (&tmpblock);
4802 for (f = gfc_sym_get_dummy_args (proc_sym); f; f = f->next)
4804 if (f->sym && f->sym->tlink == NULL && f->sym->ts.type == BT_CHARACTER)
4806 gcc_assert (f->sym->ts.u.cl->backend_decl != NULL);
4807 if (TREE_CODE (f->sym->ts.u.cl->backend_decl) == PARM_DECL)
4808 gfc_trans_vla_type_sizes (f->sym, &tmpblock);
4812 if (gfc_return_by_reference (proc_sym) && proc_sym->ts.type == BT_CHARACTER
4813 && current_fake_result_decl != NULL)
4815 gcc_assert (proc_sym->ts.u.cl->backend_decl != NULL);
4816 if (TREE_CODE (proc_sym->ts.u.cl->backend_decl) == PARM_DECL)
4817 gfc_trans_vla_type_sizes (proc_sym, &tmpblock);
4820 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), NULL_TREE);
4824 struct module_hasher : ggc_ptr_hash<module_htab_entry>
4826 typedef const char *compare_type;
4828 static hashval_t hash (module_htab_entry *s)
4830 return htab_hash_string (s->name);
4833 static bool
4834 equal (module_htab_entry *a, const char *b)
4836 return !strcmp (a->name, b);
4840 static GTY (()) hash_table<module_hasher> *module_htab;
4842 /* Hash and equality functions for module_htab's decls. */
4844 hashval_t
4845 module_decl_hasher::hash (tree t)
4847 const_tree n = DECL_NAME (t);
4848 if (n == NULL_TREE)
4849 n = TYPE_NAME (TREE_TYPE (t));
4850 return htab_hash_string (IDENTIFIER_POINTER (n));
4853 bool
4854 module_decl_hasher::equal (tree t1, const char *x2)
4856 const_tree n1 = DECL_NAME (t1);
4857 if (n1 == NULL_TREE)
4858 n1 = TYPE_NAME (TREE_TYPE (t1));
4859 return strcmp (IDENTIFIER_POINTER (n1), x2) == 0;
4862 struct module_htab_entry *
4863 gfc_find_module (const char *name)
4865 if (! module_htab)
4866 module_htab = hash_table<module_hasher>::create_ggc (10);
4868 module_htab_entry **slot
4869 = module_htab->find_slot_with_hash (name, htab_hash_string (name), INSERT);
4870 if (*slot == NULL)
4872 module_htab_entry *entry = ggc_cleared_alloc<module_htab_entry> ();
4874 entry->name = gfc_get_string ("%s", name);
4875 entry->decls = hash_table<module_decl_hasher>::create_ggc (10);
4876 *slot = entry;
4878 return *slot;
4881 void
4882 gfc_module_add_decl (struct module_htab_entry *entry, tree decl)
4884 const char *name;
4886 if (DECL_NAME (decl))
4887 name = IDENTIFIER_POINTER (DECL_NAME (decl));
4888 else
4890 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
4891 name = IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl)));
4893 tree *slot
4894 = entry->decls->find_slot_with_hash (name, htab_hash_string (name),
4895 INSERT);
4896 if (*slot == NULL)
4897 *slot = decl;
4901 /* Generate debugging symbols for namelists. This function must come after
4902 generate_local_decl to ensure that the variables in the namelist are
4903 already declared. */
4905 static tree
4906 generate_namelist_decl (gfc_symbol * sym)
4908 gfc_namelist *nml;
4909 tree decl;
4910 vec<constructor_elt, va_gc> *nml_decls = NULL;
4912 gcc_assert (sym->attr.flavor == FL_NAMELIST);
4913 for (nml = sym->namelist; nml; nml = nml->next)
4915 if (nml->sym->backend_decl == NULL_TREE)
4917 nml->sym->attr.referenced = 1;
4918 nml->sym->backend_decl = gfc_get_symbol_decl (nml->sym);
4920 DECL_IGNORED_P (nml->sym->backend_decl) = 0;
4921 CONSTRUCTOR_APPEND_ELT (nml_decls, NULL_TREE, nml->sym->backend_decl);
4924 decl = make_node (NAMELIST_DECL);
4925 TREE_TYPE (decl) = void_type_node;
4926 NAMELIST_DECL_ASSOCIATED_DECL (decl) = build_constructor (NULL_TREE, nml_decls);
4927 DECL_NAME (decl) = get_identifier (sym->name);
4928 return decl;
4932 /* Output an initialized decl for a module variable. */
4934 static void
4935 gfc_create_module_variable (gfc_symbol * sym)
4937 tree decl;
4939 /* Module functions with alternate entries are dealt with later and
4940 would get caught by the next condition. */
4941 if (sym->attr.entry)
4942 return;
4944 /* Make sure we convert the types of the derived types from iso_c_binding
4945 into (void *). */
4946 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
4947 && sym->ts.type == BT_DERIVED)
4948 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
4950 if (gfc_fl_struct (sym->attr.flavor)
4951 && sym->backend_decl
4952 && TREE_CODE (sym->backend_decl) == RECORD_TYPE)
4954 decl = sym->backend_decl;
4955 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
4957 if (!sym->attr.use_assoc && !sym->attr.used_in_submodule)
4959 gcc_assert (TYPE_CONTEXT (decl) == NULL_TREE
4960 || TYPE_CONTEXT (decl) == sym->ns->proc_name->backend_decl);
4961 gcc_assert (DECL_CONTEXT (TYPE_STUB_DECL (decl)) == NULL_TREE
4962 || DECL_CONTEXT (TYPE_STUB_DECL (decl))
4963 == sym->ns->proc_name->backend_decl);
4965 TYPE_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
4966 DECL_CONTEXT (TYPE_STUB_DECL (decl)) = sym->ns->proc_name->backend_decl;
4967 gfc_module_add_decl (cur_module, TYPE_STUB_DECL (decl));
4970 /* Only output variables, procedure pointers and array valued,
4971 or derived type, parameters. */
4972 if (sym->attr.flavor != FL_VARIABLE
4973 && !(sym->attr.flavor == FL_PARAMETER
4974 && (sym->attr.dimension || sym->ts.type == BT_DERIVED))
4975 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer))
4976 return;
4978 if ((sym->attr.in_common || sym->attr.in_equivalence) && sym->backend_decl)
4980 decl = sym->backend_decl;
4981 gcc_assert (DECL_FILE_SCOPE_P (decl));
4982 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
4983 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
4984 gfc_module_add_decl (cur_module, decl);
4987 /* Don't generate variables from other modules. Variables from
4988 COMMONs and Cray pointees will already have been generated. */
4989 if (sym->attr.use_assoc || sym->attr.used_in_submodule
4990 || sym->attr.in_common || sym->attr.cray_pointee)
4991 return;
4993 /* Equivalenced variables arrive here after creation. */
4994 if (sym->backend_decl
4995 && (sym->equiv_built || sym->attr.in_equivalence))
4996 return;
4998 if (sym->backend_decl && !sym->attr.vtab && !sym->attr.target)
4999 gfc_internal_error ("backend decl for module variable %qs already exists",
5000 sym->name);
5002 if (sym->module && !sym->attr.result && !sym->attr.dummy
5003 && (sym->attr.access == ACCESS_UNKNOWN
5004 && (sym->ns->default_access == ACCESS_PRIVATE
5005 || (sym->ns->default_access == ACCESS_UNKNOWN
5006 && flag_module_private))))
5007 sym->attr.access = ACCESS_PRIVATE;
5009 if (warn_unused_variable && !sym->attr.referenced
5010 && sym->attr.access == ACCESS_PRIVATE)
5011 gfc_warning (OPT_Wunused_value,
5012 "Unused PRIVATE module variable %qs declared at %L",
5013 sym->name, &sym->declared_at);
5015 /* We always want module variables to be created. */
5016 sym->attr.referenced = 1;
5017 /* Create the decl. */
5018 decl = gfc_get_symbol_decl (sym);
5020 /* Create the variable. */
5021 pushdecl (decl);
5022 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE
5023 || (sym->ns->parent->proc_name->attr.flavor == FL_MODULE
5024 && sym->fn_result_spec));
5025 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
5026 rest_of_decl_compilation (decl, 1, 0);
5027 gfc_module_add_decl (cur_module, decl);
5029 /* Also add length of strings. */
5030 if (sym->ts.type == BT_CHARACTER)
5032 tree length;
5034 length = sym->ts.u.cl->backend_decl;
5035 gcc_assert (length || sym->attr.proc_pointer);
5036 if (length && !INTEGER_CST_P (length))
5038 pushdecl (length);
5039 rest_of_decl_compilation (length, 1, 0);
5043 if (sym->attr.codimension && !sym->attr.dummy && !sym->attr.allocatable
5044 && sym->attr.referenced && !sym->attr.use_assoc)
5045 has_coarray_vars = true;
5048 /* Emit debug information for USE statements. */
5050 static void
5051 gfc_trans_use_stmts (gfc_namespace * ns)
5053 gfc_use_list *use_stmt;
5054 for (use_stmt = ns->use_stmts; use_stmt; use_stmt = use_stmt->next)
5056 struct module_htab_entry *entry
5057 = gfc_find_module (use_stmt->module_name);
5058 gfc_use_rename *rent;
5060 if (entry->namespace_decl == NULL)
5062 entry->namespace_decl
5063 = build_decl (input_location,
5064 NAMESPACE_DECL,
5065 get_identifier (use_stmt->module_name),
5066 void_type_node);
5067 DECL_EXTERNAL (entry->namespace_decl) = 1;
5069 gfc_set_backend_locus (&use_stmt->where);
5070 if (!use_stmt->only_flag)
5071 (*debug_hooks->imported_module_or_decl) (entry->namespace_decl,
5072 NULL_TREE,
5073 ns->proc_name->backend_decl,
5074 false, false);
5075 for (rent = use_stmt->rename; rent; rent = rent->next)
5077 tree decl, local_name;
5079 if (rent->op != INTRINSIC_NONE)
5080 continue;
5082 hashval_t hash = htab_hash_string (rent->use_name);
5083 tree *slot = entry->decls->find_slot_with_hash (rent->use_name, hash,
5084 INSERT);
5085 if (*slot == NULL)
5087 gfc_symtree *st;
5089 st = gfc_find_symtree (ns->sym_root,
5090 rent->local_name[0]
5091 ? rent->local_name : rent->use_name);
5093 /* The following can happen if a derived type is renamed. */
5094 if (!st)
5096 char *name;
5097 name = xstrdup (rent->local_name[0]
5098 ? rent->local_name : rent->use_name);
5099 name[0] = (char) TOUPPER ((unsigned char) name[0]);
5100 st = gfc_find_symtree (ns->sym_root, name);
5101 free (name);
5102 gcc_assert (st);
5105 /* Sometimes, generic interfaces wind up being over-ruled by a
5106 local symbol (see PR41062). */
5107 if (!st->n.sym->attr.use_assoc)
5108 continue;
5110 if (st->n.sym->backend_decl
5111 && DECL_P (st->n.sym->backend_decl)
5112 && st->n.sym->module
5113 && strcmp (st->n.sym->module, use_stmt->module_name) == 0)
5115 gcc_assert (DECL_EXTERNAL (entry->namespace_decl)
5116 || !VAR_P (st->n.sym->backend_decl));
5117 decl = copy_node (st->n.sym->backend_decl);
5118 DECL_CONTEXT (decl) = entry->namespace_decl;
5119 DECL_EXTERNAL (decl) = 1;
5120 DECL_IGNORED_P (decl) = 0;
5121 DECL_INITIAL (decl) = NULL_TREE;
5123 else if (st->n.sym->attr.flavor == FL_NAMELIST
5124 && st->n.sym->attr.use_only
5125 && st->n.sym->module
5126 && strcmp (st->n.sym->module, use_stmt->module_name)
5127 == 0)
5129 decl = generate_namelist_decl (st->n.sym);
5130 DECL_CONTEXT (decl) = entry->namespace_decl;
5131 DECL_EXTERNAL (decl) = 1;
5132 DECL_IGNORED_P (decl) = 0;
5133 DECL_INITIAL (decl) = NULL_TREE;
5135 else
5137 *slot = error_mark_node;
5138 entry->decls->clear_slot (slot);
5139 continue;
5141 *slot = decl;
5143 decl = (tree) *slot;
5144 if (rent->local_name[0])
5145 local_name = get_identifier (rent->local_name);
5146 else
5147 local_name = NULL_TREE;
5148 gfc_set_backend_locus (&rent->where);
5149 (*debug_hooks->imported_module_or_decl) (decl, local_name,
5150 ns->proc_name->backend_decl,
5151 !use_stmt->only_flag,
5152 false);
5158 /* Return true if expr is a constant initializer that gfc_conv_initializer
5159 will handle. */
5161 static bool
5162 check_constant_initializer (gfc_expr *expr, gfc_typespec *ts, bool array,
5163 bool pointer)
5165 gfc_constructor *c;
5166 gfc_component *cm;
5168 if (pointer)
5169 return true;
5170 else if (array)
5172 if (expr->expr_type == EXPR_CONSTANT || expr->expr_type == EXPR_NULL)
5173 return true;
5174 else if (expr->expr_type == EXPR_STRUCTURE)
5175 return check_constant_initializer (expr, ts, false, false);
5176 else if (expr->expr_type != EXPR_ARRAY)
5177 return false;
5178 for (c = gfc_constructor_first (expr->value.constructor);
5179 c; c = gfc_constructor_next (c))
5181 if (c->iterator)
5182 return false;
5183 if (c->expr->expr_type == EXPR_STRUCTURE)
5185 if (!check_constant_initializer (c->expr, ts, false, false))
5186 return false;
5188 else if (c->expr->expr_type != EXPR_CONSTANT)
5189 return false;
5191 return true;
5193 else switch (ts->type)
5195 case_bt_struct:
5196 if (expr->expr_type != EXPR_STRUCTURE)
5197 return false;
5198 cm = expr->ts.u.derived->components;
5199 for (c = gfc_constructor_first (expr->value.constructor);
5200 c; c = gfc_constructor_next (c), cm = cm->next)
5202 if (!c->expr || cm->attr.allocatable)
5203 continue;
5204 if (!check_constant_initializer (c->expr, &cm->ts,
5205 cm->attr.dimension,
5206 cm->attr.pointer))
5207 return false;
5209 return true;
5210 default:
5211 return expr->expr_type == EXPR_CONSTANT;
5215 /* Emit debug info for parameters and unreferenced variables with
5216 initializers. */
5218 static void
5219 gfc_emit_parameter_debug_info (gfc_symbol *sym)
5221 tree decl;
5223 if (sym->attr.flavor != FL_PARAMETER
5224 && (sym->attr.flavor != FL_VARIABLE || sym->attr.referenced))
5225 return;
5227 if (sym->backend_decl != NULL
5228 || sym->value == NULL
5229 || sym->attr.use_assoc
5230 || sym->attr.dummy
5231 || sym->attr.result
5232 || sym->attr.function
5233 || sym->attr.intrinsic
5234 || sym->attr.pointer
5235 || sym->attr.allocatable
5236 || sym->attr.cray_pointee
5237 || sym->attr.threadprivate
5238 || sym->attr.is_bind_c
5239 || sym->attr.subref_array_pointer
5240 || sym->attr.assign)
5241 return;
5243 if (sym->ts.type == BT_CHARACTER)
5245 gfc_conv_const_charlen (sym->ts.u.cl);
5246 if (sym->ts.u.cl->backend_decl == NULL
5247 || TREE_CODE (sym->ts.u.cl->backend_decl) != INTEGER_CST)
5248 return;
5250 else if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.alloc_comp)
5251 return;
5253 if (sym->as)
5255 int n;
5257 if (sym->as->type != AS_EXPLICIT)
5258 return;
5259 for (n = 0; n < sym->as->rank; n++)
5260 if (sym->as->lower[n]->expr_type != EXPR_CONSTANT
5261 || sym->as->upper[n] == NULL
5262 || sym->as->upper[n]->expr_type != EXPR_CONSTANT)
5263 return;
5266 if (!check_constant_initializer (sym->value, &sym->ts,
5267 sym->attr.dimension, false))
5268 return;
5270 if (flag_coarray == GFC_FCOARRAY_LIB && sym->attr.codimension)
5271 return;
5273 /* Create the decl for the variable or constant. */
5274 decl = build_decl (input_location,
5275 sym->attr.flavor == FL_PARAMETER ? CONST_DECL : VAR_DECL,
5276 gfc_sym_identifier (sym), gfc_sym_type (sym));
5277 if (sym->attr.flavor == FL_PARAMETER)
5278 TREE_READONLY (decl) = 1;
5279 gfc_set_decl_location (decl, &sym->declared_at);
5280 if (sym->attr.dimension)
5281 GFC_DECL_PACKED_ARRAY (decl) = 1;
5282 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
5283 TREE_STATIC (decl) = 1;
5284 TREE_USED (decl) = 1;
5285 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
5286 TREE_PUBLIC (decl) = 1;
5287 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
5288 TREE_TYPE (decl),
5289 sym->attr.dimension,
5290 false, false);
5291 debug_hooks->early_global_decl (decl);
5295 static void
5296 generate_coarray_sym_init (gfc_symbol *sym)
5298 tree tmp, size, decl, token, desc;
5299 bool is_lock_type, is_event_type;
5300 int reg_type;
5301 gfc_se se;
5302 symbol_attribute attr;
5304 if (sym->attr.dummy || sym->attr.allocatable || !sym->attr.codimension
5305 || sym->attr.use_assoc || !sym->attr.referenced
5306 || sym->attr.select_type_temporary)
5307 return;
5309 decl = sym->backend_decl;
5310 TREE_USED(decl) = 1;
5311 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
5313 is_lock_type = sym->ts.type == BT_DERIVED
5314 && sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
5315 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE;
5317 is_event_type = sym->ts.type == BT_DERIVED
5318 && sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
5319 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE;
5321 /* FIXME: Workaround for PR middle-end/49106, cf. also PR middle-end/49108
5322 to make sure the variable is not optimized away. */
5323 DECL_PRESERVE_P (DECL_CONTEXT (decl)) = 1;
5325 /* For lock types, we pass the array size as only the library knows the
5326 size of the variable. */
5327 if (is_lock_type || is_event_type)
5328 size = gfc_index_one_node;
5329 else
5330 size = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (decl)));
5332 /* Ensure that we do not have size=0 for zero-sized arrays. */
5333 size = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
5334 fold_convert (size_type_node, size),
5335 build_int_cst (size_type_node, 1));
5337 if (GFC_TYPE_ARRAY_RANK (TREE_TYPE (decl)))
5339 tmp = GFC_TYPE_ARRAY_SIZE (TREE_TYPE (decl));
5340 size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
5341 fold_convert (size_type_node, tmp), size);
5344 gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (decl)) != NULL_TREE);
5345 token = gfc_build_addr_expr (ppvoid_type_node,
5346 GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE(decl)));
5347 if (is_lock_type)
5348 reg_type = sym->attr.artificial ? GFC_CAF_CRITICAL : GFC_CAF_LOCK_STATIC;
5349 else if (is_event_type)
5350 reg_type = GFC_CAF_EVENT_STATIC;
5351 else
5352 reg_type = GFC_CAF_COARRAY_STATIC;
5354 /* Compile the symbol attribute. */
5355 if (sym->ts.type == BT_CLASS)
5357 attr = CLASS_DATA (sym)->attr;
5358 /* The pointer attribute is always set on classes, overwrite it with the
5359 class_pointer attribute, which denotes the pointer for classes. */
5360 attr.pointer = attr.class_pointer;
5362 else
5363 attr = sym->attr;
5364 gfc_init_se (&se, NULL);
5365 desc = gfc_conv_scalar_to_descriptor (&se, decl, attr);
5366 gfc_add_block_to_block (&caf_init_block, &se.pre);
5368 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_register, 7, size,
5369 build_int_cst (integer_type_node, reg_type),
5370 token, gfc_build_addr_expr (pvoid_type_node, desc),
5371 null_pointer_node, /* stat. */
5372 null_pointer_node, /* errgmsg. */
5373 build_zero_cst (size_type_node)); /* errmsg_len. */
5374 gfc_add_expr_to_block (&caf_init_block, tmp);
5375 gfc_add_modify (&caf_init_block, decl, fold_convert (TREE_TYPE (decl),
5376 gfc_conv_descriptor_data_get (desc)));
5378 /* Handle "static" initializer. */
5379 if (sym->value)
5381 sym->attr.pointer = 1;
5382 tmp = gfc_trans_assignment (gfc_lval_expr_from_sym (sym), sym->value,
5383 true, false);
5384 sym->attr.pointer = 0;
5385 gfc_add_expr_to_block (&caf_init_block, tmp);
5387 else if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.pointer_comp)
5389 tmp = gfc_nullify_alloc_comp (sym->ts.u.derived, decl, sym->as
5390 ? sym->as->rank : 0,
5391 GFC_STRUCTURE_CAF_MODE_IN_COARRAY);
5392 gfc_add_expr_to_block (&caf_init_block, tmp);
5397 /* Generate constructor function to initialize static, nonallocatable
5398 coarrays. */
5400 static void
5401 generate_coarray_init (gfc_namespace * ns __attribute((unused)))
5403 tree fndecl, tmp, decl, save_fn_decl;
5405 save_fn_decl = current_function_decl;
5406 push_function_context ();
5408 tmp = build_function_type_list (void_type_node, NULL_TREE);
5409 fndecl = build_decl (input_location, FUNCTION_DECL,
5410 create_tmp_var_name ("_caf_init"), tmp);
5412 DECL_STATIC_CONSTRUCTOR (fndecl) = 1;
5413 SET_DECL_INIT_PRIORITY (fndecl, DEFAULT_INIT_PRIORITY);
5415 decl = build_decl (input_location, RESULT_DECL, NULL_TREE, void_type_node);
5416 DECL_ARTIFICIAL (decl) = 1;
5417 DECL_IGNORED_P (decl) = 1;
5418 DECL_CONTEXT (decl) = fndecl;
5419 DECL_RESULT (fndecl) = decl;
5421 pushdecl (fndecl);
5422 current_function_decl = fndecl;
5423 announce_function (fndecl);
5425 rest_of_decl_compilation (fndecl, 0, 0);
5426 make_decl_rtl (fndecl);
5427 allocate_struct_function (fndecl, false);
5429 pushlevel ();
5430 gfc_init_block (&caf_init_block);
5432 gfc_traverse_ns (ns, generate_coarray_sym_init);
5434 DECL_SAVED_TREE (fndecl) = gfc_finish_block (&caf_init_block);
5435 decl = getdecls ();
5437 poplevel (1, 1);
5438 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
5440 DECL_SAVED_TREE (fndecl)
5441 = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (fndecl),
5442 DECL_INITIAL (fndecl));
5443 dump_function (TDI_original, fndecl);
5445 cfun->function_end_locus = input_location;
5446 set_cfun (NULL);
5448 if (decl_function_context (fndecl))
5449 (void) cgraph_node::create (fndecl);
5450 else
5451 cgraph_node::finalize_function (fndecl, true);
5453 pop_function_context ();
5454 current_function_decl = save_fn_decl;
5458 static void
5459 create_module_nml_decl (gfc_symbol *sym)
5461 if (sym->attr.flavor == FL_NAMELIST)
5463 tree decl = generate_namelist_decl (sym);
5464 pushdecl (decl);
5465 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
5466 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
5467 rest_of_decl_compilation (decl, 1, 0);
5468 gfc_module_add_decl (cur_module, decl);
5473 /* Generate all the required code for module variables. */
5475 void
5476 gfc_generate_module_vars (gfc_namespace * ns)
5478 module_namespace = ns;
5479 cur_module = gfc_find_module (ns->proc_name->name);
5481 /* Check if the frontend left the namespace in a reasonable state. */
5482 gcc_assert (ns->proc_name && !ns->proc_name->tlink);
5484 /* Generate COMMON blocks. */
5485 gfc_trans_common (ns);
5487 has_coarray_vars = false;
5489 /* Create decls for all the module variables. */
5490 gfc_traverse_ns (ns, gfc_create_module_variable);
5491 gfc_traverse_ns (ns, create_module_nml_decl);
5493 if (flag_coarray == GFC_FCOARRAY_LIB && has_coarray_vars)
5494 generate_coarray_init (ns);
5496 cur_module = NULL;
5498 gfc_trans_use_stmts (ns);
5499 gfc_traverse_ns (ns, gfc_emit_parameter_debug_info);
5503 static void
5504 gfc_generate_contained_functions (gfc_namespace * parent)
5506 gfc_namespace *ns;
5508 /* We create all the prototypes before generating any code. */
5509 for (ns = parent->contained; ns; ns = ns->sibling)
5511 /* Skip namespaces from used modules. */
5512 if (ns->parent != parent)
5513 continue;
5515 gfc_create_function_decl (ns, false);
5518 for (ns = parent->contained; ns; ns = ns->sibling)
5520 /* Skip namespaces from used modules. */
5521 if (ns->parent != parent)
5522 continue;
5524 gfc_generate_function_code (ns);
5529 /* Drill down through expressions for the array specification bounds and
5530 character length calling generate_local_decl for all those variables
5531 that have not already been declared. */
5533 static void
5534 generate_local_decl (gfc_symbol *);
5536 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
5538 static bool
5539 expr_decls (gfc_expr *e, gfc_symbol *sym,
5540 int *f ATTRIBUTE_UNUSED)
5542 if (e->expr_type != EXPR_VARIABLE
5543 || sym == e->symtree->n.sym
5544 || e->symtree->n.sym->mark
5545 || e->symtree->n.sym->ns != sym->ns)
5546 return false;
5548 generate_local_decl (e->symtree->n.sym);
5549 return false;
5552 static void
5553 generate_expr_decls (gfc_symbol *sym, gfc_expr *e)
5555 gfc_traverse_expr (e, sym, expr_decls, 0);
5559 /* Check for dependencies in the character length and array spec. */
5561 static void
5562 generate_dependency_declarations (gfc_symbol *sym)
5564 int i;
5566 if (sym->ts.type == BT_CHARACTER
5567 && sym->ts.u.cl
5568 && sym->ts.u.cl->length
5569 && sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
5570 generate_expr_decls (sym, sym->ts.u.cl->length);
5572 if (sym->as && sym->as->rank)
5574 for (i = 0; i < sym->as->rank; i++)
5576 generate_expr_decls (sym, sym->as->lower[i]);
5577 generate_expr_decls (sym, sym->as->upper[i]);
5583 /* Generate decls for all local variables. We do this to ensure correct
5584 handling of expressions which only appear in the specification of
5585 other functions. */
5587 static void
5588 generate_local_decl (gfc_symbol * sym)
5590 if (sym->attr.flavor == FL_VARIABLE)
5592 if (sym->attr.codimension && !sym->attr.dummy && !sym->attr.allocatable
5593 && sym->attr.referenced && !sym->attr.use_assoc)
5594 has_coarray_vars = true;
5596 if (!sym->attr.dummy && !sym->ns->proc_name->attr.entry_master)
5597 generate_dependency_declarations (sym);
5599 if (sym->attr.referenced)
5600 gfc_get_symbol_decl (sym);
5602 /* Warnings for unused dummy arguments. */
5603 else if (sym->attr.dummy && !sym->attr.in_namelist)
5605 /* INTENT(out) dummy arguments are likely meant to be set. */
5606 if (warn_unused_dummy_argument && sym->attr.intent == INTENT_OUT)
5608 if (sym->ts.type != BT_DERIVED)
5609 gfc_warning (OPT_Wunused_dummy_argument,
5610 "Dummy argument %qs at %L was declared "
5611 "INTENT(OUT) but was not set", sym->name,
5612 &sym->declared_at);
5613 else if (!gfc_has_default_initializer (sym->ts.u.derived)
5614 && !sym->ts.u.derived->attr.zero_comp)
5615 gfc_warning (OPT_Wunused_dummy_argument,
5616 "Derived-type dummy argument %qs at %L was "
5617 "declared INTENT(OUT) but was not set and "
5618 "does not have a default initializer",
5619 sym->name, &sym->declared_at);
5620 if (sym->backend_decl != NULL_TREE)
5621 TREE_NO_WARNING(sym->backend_decl) = 1;
5623 else if (warn_unused_dummy_argument)
5625 gfc_warning (OPT_Wunused_dummy_argument,
5626 "Unused dummy argument %qs at %L", sym->name,
5627 &sym->declared_at);
5628 if (sym->backend_decl != NULL_TREE)
5629 TREE_NO_WARNING(sym->backend_decl) = 1;
5633 /* Warn for unused variables, but not if they're inside a common
5634 block or a namelist. */
5635 else if (warn_unused_variable
5636 && !(sym->attr.in_common || sym->mark || sym->attr.in_namelist))
5638 if (sym->attr.use_only)
5640 gfc_warning (OPT_Wunused_variable,
5641 "Unused module variable %qs which has been "
5642 "explicitly imported at %L", sym->name,
5643 &sym->declared_at);
5644 if (sym->backend_decl != NULL_TREE)
5645 TREE_NO_WARNING(sym->backend_decl) = 1;
5647 else if (!sym->attr.use_assoc)
5649 /* Corner case: the symbol may be an entry point. At this point,
5650 it may appear to be an unused variable. Suppress warning. */
5651 bool enter = false;
5652 gfc_entry_list *el;
5654 for (el = sym->ns->entries; el; el=el->next)
5655 if (strcmp(sym->name, el->sym->name) == 0)
5656 enter = true;
5658 if (!enter)
5659 gfc_warning (OPT_Wunused_variable,
5660 "Unused variable %qs declared at %L",
5661 sym->name, &sym->declared_at);
5662 if (sym->backend_decl != NULL_TREE)
5663 TREE_NO_WARNING(sym->backend_decl) = 1;
5667 /* For variable length CHARACTER parameters, the PARM_DECL already
5668 references the length variable, so force gfc_get_symbol_decl
5669 even when not referenced. If optimize > 0, it will be optimized
5670 away anyway. But do this only after emitting -Wunused-parameter
5671 warning if requested. */
5672 if (sym->attr.dummy && !sym->attr.referenced
5673 && sym->ts.type == BT_CHARACTER
5674 && sym->ts.u.cl->backend_decl != NULL
5675 && VAR_P (sym->ts.u.cl->backend_decl))
5677 sym->attr.referenced = 1;
5678 gfc_get_symbol_decl (sym);
5681 /* INTENT(out) dummy arguments and result variables with allocatable
5682 components are reset by default and need to be set referenced to
5683 generate the code for nullification and automatic lengths. */
5684 if (!sym->attr.referenced
5685 && sym->ts.type == BT_DERIVED
5686 && sym->ts.u.derived->attr.alloc_comp
5687 && !sym->attr.pointer
5688 && ((sym->attr.dummy && sym->attr.intent == INTENT_OUT)
5690 (sym->attr.result && sym != sym->result)))
5692 sym->attr.referenced = 1;
5693 gfc_get_symbol_decl (sym);
5696 /* Check for dependencies in the array specification and string
5697 length, adding the necessary declarations to the function. We
5698 mark the symbol now, as well as in traverse_ns, to prevent
5699 getting stuck in a circular dependency. */
5700 sym->mark = 1;
5702 else if (sym->attr.flavor == FL_PARAMETER)
5704 if (warn_unused_parameter
5705 && !sym->attr.referenced)
5707 if (!sym->attr.use_assoc)
5708 gfc_warning (OPT_Wunused_parameter,
5709 "Unused parameter %qs declared at %L", sym->name,
5710 &sym->declared_at);
5711 else if (sym->attr.use_only)
5712 gfc_warning (OPT_Wunused_parameter,
5713 "Unused parameter %qs which has been explicitly "
5714 "imported at %L", sym->name, &sym->declared_at);
5717 if (sym->ns
5718 && sym->ns->parent
5719 && sym->ns->parent->code
5720 && sym->ns->parent->code->op == EXEC_BLOCK)
5722 if (sym->attr.referenced)
5723 gfc_get_symbol_decl (sym);
5724 sym->mark = 1;
5727 else if (sym->attr.flavor == FL_PROCEDURE)
5729 /* TODO: move to the appropriate place in resolve.c. */
5730 if (warn_return_type > 0
5731 && sym->attr.function
5732 && sym->result
5733 && sym != sym->result
5734 && !sym->result->attr.referenced
5735 && !sym->attr.use_assoc
5736 && sym->attr.if_source != IFSRC_IFBODY)
5738 gfc_warning (OPT_Wreturn_type,
5739 "Return value %qs of function %qs declared at "
5740 "%L not set", sym->result->name, sym->name,
5741 &sym->result->declared_at);
5743 /* Prevents "Unused variable" warning for RESULT variables. */
5744 sym->result->mark = 1;
5748 if (sym->attr.dummy == 1)
5750 /* Modify the tree type for scalar character dummy arguments of bind(c)
5751 procedures if they are passed by value. The tree type for them will
5752 be promoted to INTEGER_TYPE for the middle end, which appears to be
5753 what C would do with characters passed by-value. The value attribute
5754 implies the dummy is a scalar. */
5755 if (sym->attr.value == 1 && sym->backend_decl != NULL
5756 && sym->ts.type == BT_CHARACTER && sym->ts.is_c_interop
5757 && sym->ns->proc_name != NULL && sym->ns->proc_name->attr.is_bind_c)
5758 gfc_conv_scalar_char_value (sym, NULL, NULL);
5760 /* Unused procedure passed as dummy argument. */
5761 if (sym->attr.flavor == FL_PROCEDURE)
5763 if (!sym->attr.referenced)
5765 if (warn_unused_dummy_argument)
5766 gfc_warning (OPT_Wunused_dummy_argument,
5767 "Unused dummy argument %qs at %L", sym->name,
5768 &sym->declared_at);
5771 /* Silence bogus "unused parameter" warnings from the
5772 middle end. */
5773 if (sym->backend_decl != NULL_TREE)
5774 TREE_NO_WARNING (sym->backend_decl) = 1;
5778 /* Make sure we convert the types of the derived types from iso_c_binding
5779 into (void *). */
5780 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
5781 && sym->ts.type == BT_DERIVED)
5782 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
5786 static void
5787 generate_local_nml_decl (gfc_symbol * sym)
5789 if (sym->attr.flavor == FL_NAMELIST && !sym->attr.use_assoc)
5791 tree decl = generate_namelist_decl (sym);
5792 pushdecl (decl);
5797 static void
5798 generate_local_vars (gfc_namespace * ns)
5800 gfc_traverse_ns (ns, generate_local_decl);
5801 gfc_traverse_ns (ns, generate_local_nml_decl);
5805 /* Generate a switch statement to jump to the correct entry point. Also
5806 creates the label decls for the entry points. */
5808 static tree
5809 gfc_trans_entry_master_switch (gfc_entry_list * el)
5811 stmtblock_t block;
5812 tree label;
5813 tree tmp;
5814 tree val;
5816 gfc_init_block (&block);
5817 for (; el; el = el->next)
5819 /* Add the case label. */
5820 label = gfc_build_label_decl (NULL_TREE);
5821 val = build_int_cst (gfc_array_index_type, el->id);
5822 tmp = build_case_label (val, NULL_TREE, label);
5823 gfc_add_expr_to_block (&block, tmp);
5825 /* And jump to the actual entry point. */
5826 label = gfc_build_label_decl (NULL_TREE);
5827 tmp = build1_v (GOTO_EXPR, label);
5828 gfc_add_expr_to_block (&block, tmp);
5830 /* Save the label decl. */
5831 el->label = label;
5833 tmp = gfc_finish_block (&block);
5834 /* The first argument selects the entry point. */
5835 val = DECL_ARGUMENTS (current_function_decl);
5836 tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE, val, tmp);
5837 return tmp;
5841 /* Add code to string lengths of actual arguments passed to a function against
5842 the expected lengths of the dummy arguments. */
5844 static void
5845 add_argument_checking (stmtblock_t *block, gfc_symbol *sym)
5847 gfc_formal_arglist *formal;
5849 for (formal = gfc_sym_get_dummy_args (sym); formal; formal = formal->next)
5850 if (formal->sym && formal->sym->ts.type == BT_CHARACTER
5851 && !formal->sym->ts.deferred)
5853 enum tree_code comparison;
5854 tree cond;
5855 tree argname;
5856 gfc_symbol *fsym;
5857 gfc_charlen *cl;
5858 const char *message;
5860 fsym = formal->sym;
5861 cl = fsym->ts.u.cl;
5863 gcc_assert (cl);
5864 gcc_assert (cl->passed_length != NULL_TREE);
5865 gcc_assert (cl->backend_decl != NULL_TREE);
5867 /* For POINTER, ALLOCATABLE and assumed-shape dummy arguments, the
5868 string lengths must match exactly. Otherwise, it is only required
5869 that the actual string length is *at least* the expected one.
5870 Sequence association allows for a mismatch of the string length
5871 if the actual argument is (part of) an array, but only if the
5872 dummy argument is an array. (See "Sequence association" in
5873 Section 12.4.1.4 for F95 and 12.4.1.5 for F2003.) */
5874 if (fsym->attr.pointer || fsym->attr.allocatable
5875 || (fsym->as && (fsym->as->type == AS_ASSUMED_SHAPE
5876 || fsym->as->type == AS_ASSUMED_RANK)))
5878 comparison = NE_EXPR;
5879 message = _("Actual string length does not match the declared one"
5880 " for dummy argument '%s' (%ld/%ld)");
5882 else if (fsym->as && fsym->as->rank != 0)
5883 continue;
5884 else
5886 comparison = LT_EXPR;
5887 message = _("Actual string length is shorter than the declared one"
5888 " for dummy argument '%s' (%ld/%ld)");
5891 /* Build the condition. For optional arguments, an actual length
5892 of 0 is also acceptable if the associated string is NULL, which
5893 means the argument was not passed. */
5894 cond = fold_build2_loc (input_location, comparison, logical_type_node,
5895 cl->passed_length, cl->backend_decl);
5896 if (fsym->attr.optional)
5898 tree not_absent;
5899 tree not_0length;
5900 tree absent_failed;
5902 not_0length = fold_build2_loc (input_location, NE_EXPR,
5903 logical_type_node,
5904 cl->passed_length,
5905 build_zero_cst
5906 (TREE_TYPE (cl->passed_length)));
5907 /* The symbol needs to be referenced for gfc_get_symbol_decl. */
5908 fsym->attr.referenced = 1;
5909 not_absent = gfc_conv_expr_present (fsym);
5911 absent_failed = fold_build2_loc (input_location, TRUTH_OR_EXPR,
5912 logical_type_node, not_0length,
5913 not_absent);
5915 cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
5916 logical_type_node, cond, absent_failed);
5919 /* Build the runtime check. */
5920 argname = gfc_build_cstring_const (fsym->name);
5921 argname = gfc_build_addr_expr (pchar_type_node, argname);
5922 gfc_trans_runtime_check (true, false, cond, block, &fsym->declared_at,
5923 message, argname,
5924 fold_convert (long_integer_type_node,
5925 cl->passed_length),
5926 fold_convert (long_integer_type_node,
5927 cl->backend_decl));
5932 static void
5933 create_main_function (tree fndecl)
5935 tree old_context;
5936 tree ftn_main;
5937 tree tmp, decl, result_decl, argc, argv, typelist, arglist;
5938 stmtblock_t body;
5940 old_context = current_function_decl;
5942 if (old_context)
5944 push_function_context ();
5945 saved_parent_function_decls = saved_function_decls;
5946 saved_function_decls = NULL_TREE;
5949 /* main() function must be declared with global scope. */
5950 gcc_assert (current_function_decl == NULL_TREE);
5952 /* Declare the function. */
5953 tmp = build_function_type_list (integer_type_node, integer_type_node,
5954 build_pointer_type (pchar_type_node),
5955 NULL_TREE);
5956 main_identifier_node = get_identifier ("main");
5957 ftn_main = build_decl (input_location, FUNCTION_DECL,
5958 main_identifier_node, tmp);
5959 DECL_EXTERNAL (ftn_main) = 0;
5960 TREE_PUBLIC (ftn_main) = 1;
5961 TREE_STATIC (ftn_main) = 1;
5962 DECL_ATTRIBUTES (ftn_main)
5963 = tree_cons (get_identifier("externally_visible"), NULL_TREE, NULL_TREE);
5965 /* Setup the result declaration (for "return 0"). */
5966 result_decl = build_decl (input_location,
5967 RESULT_DECL, NULL_TREE, integer_type_node);
5968 DECL_ARTIFICIAL (result_decl) = 1;
5969 DECL_IGNORED_P (result_decl) = 1;
5970 DECL_CONTEXT (result_decl) = ftn_main;
5971 DECL_RESULT (ftn_main) = result_decl;
5973 pushdecl (ftn_main);
5975 /* Get the arguments. */
5977 arglist = NULL_TREE;
5978 typelist = TYPE_ARG_TYPES (TREE_TYPE (ftn_main));
5980 tmp = TREE_VALUE (typelist);
5981 argc = build_decl (input_location, PARM_DECL, get_identifier ("argc"), tmp);
5982 DECL_CONTEXT (argc) = ftn_main;
5983 DECL_ARG_TYPE (argc) = TREE_VALUE (typelist);
5984 TREE_READONLY (argc) = 1;
5985 gfc_finish_decl (argc);
5986 arglist = chainon (arglist, argc);
5988 typelist = TREE_CHAIN (typelist);
5989 tmp = TREE_VALUE (typelist);
5990 argv = build_decl (input_location, PARM_DECL, get_identifier ("argv"), tmp);
5991 DECL_CONTEXT (argv) = ftn_main;
5992 DECL_ARG_TYPE (argv) = TREE_VALUE (typelist);
5993 TREE_READONLY (argv) = 1;
5994 DECL_BY_REFERENCE (argv) = 1;
5995 gfc_finish_decl (argv);
5996 arglist = chainon (arglist, argv);
5998 DECL_ARGUMENTS (ftn_main) = arglist;
5999 current_function_decl = ftn_main;
6000 announce_function (ftn_main);
6002 rest_of_decl_compilation (ftn_main, 1, 0);
6003 make_decl_rtl (ftn_main);
6004 allocate_struct_function (ftn_main, false);
6005 pushlevel ();
6007 gfc_init_block (&body);
6009 /* Call some libgfortran initialization routines, call then MAIN__(). */
6011 /* Call _gfortran_caf_init (*argc, ***argv). */
6012 if (flag_coarray == GFC_FCOARRAY_LIB)
6014 tree pint_type, pppchar_type;
6015 pint_type = build_pointer_type (integer_type_node);
6016 pppchar_type
6017 = build_pointer_type (build_pointer_type (pchar_type_node));
6019 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_init, 2,
6020 gfc_build_addr_expr (pint_type, argc),
6021 gfc_build_addr_expr (pppchar_type, argv));
6022 gfc_add_expr_to_block (&body, tmp);
6025 /* Call _gfortran_set_args (argc, argv). */
6026 TREE_USED (argc) = 1;
6027 TREE_USED (argv) = 1;
6028 tmp = build_call_expr_loc (input_location,
6029 gfor_fndecl_set_args, 2, argc, argv);
6030 gfc_add_expr_to_block (&body, tmp);
6032 /* Add a call to set_options to set up the runtime library Fortran
6033 language standard parameters. */
6035 tree array_type, array, var;
6036 vec<constructor_elt, va_gc> *v = NULL;
6037 static const int noptions = 7;
6039 /* Passing a new option to the library requires three modifications:
6040 + add it to the tree_cons list below
6041 + change the noptions variable above
6042 + modify the library (runtime/compile_options.c)! */
6044 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6045 build_int_cst (integer_type_node,
6046 gfc_option.warn_std));
6047 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6048 build_int_cst (integer_type_node,
6049 gfc_option.allow_std));
6050 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6051 build_int_cst (integer_type_node, pedantic));
6052 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6053 build_int_cst (integer_type_node, flag_backtrace));
6054 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6055 build_int_cst (integer_type_node, flag_sign_zero));
6056 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6057 build_int_cst (integer_type_node,
6058 (gfc_option.rtcheck
6059 & GFC_RTCHECK_BOUNDS)));
6060 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6061 build_int_cst (integer_type_node,
6062 gfc_option.fpe_summary));
6064 array_type = build_array_type_nelts (integer_type_node, noptions);
6065 array = build_constructor (array_type, v);
6066 TREE_CONSTANT (array) = 1;
6067 TREE_STATIC (array) = 1;
6069 /* Create a static variable to hold the jump table. */
6070 var = build_decl (input_location, VAR_DECL,
6071 create_tmp_var_name ("options"), array_type);
6072 DECL_ARTIFICIAL (var) = 1;
6073 DECL_IGNORED_P (var) = 1;
6074 TREE_CONSTANT (var) = 1;
6075 TREE_STATIC (var) = 1;
6076 TREE_READONLY (var) = 1;
6077 DECL_INITIAL (var) = array;
6078 pushdecl (var);
6079 var = gfc_build_addr_expr (build_pointer_type (integer_type_node), var);
6081 tmp = build_call_expr_loc (input_location,
6082 gfor_fndecl_set_options, 2,
6083 build_int_cst (integer_type_node, noptions), var);
6084 gfc_add_expr_to_block (&body, tmp);
6087 /* If -ffpe-trap option was provided, add a call to set_fpe so that
6088 the library will raise a FPE when needed. */
6089 if (gfc_option.fpe != 0)
6091 tmp = build_call_expr_loc (input_location,
6092 gfor_fndecl_set_fpe, 1,
6093 build_int_cst (integer_type_node,
6094 gfc_option.fpe));
6095 gfc_add_expr_to_block (&body, tmp);
6098 /* If this is the main program and an -fconvert option was provided,
6099 add a call to set_convert. */
6101 if (flag_convert != GFC_FLAG_CONVERT_NATIVE)
6103 tmp = build_call_expr_loc (input_location,
6104 gfor_fndecl_set_convert, 1,
6105 build_int_cst (integer_type_node, flag_convert));
6106 gfc_add_expr_to_block (&body, tmp);
6109 /* If this is the main program and an -frecord-marker option was provided,
6110 add a call to set_record_marker. */
6112 if (flag_record_marker != 0)
6114 tmp = build_call_expr_loc (input_location,
6115 gfor_fndecl_set_record_marker, 1,
6116 build_int_cst (integer_type_node,
6117 flag_record_marker));
6118 gfc_add_expr_to_block (&body, tmp);
6121 if (flag_max_subrecord_length != 0)
6123 tmp = build_call_expr_loc (input_location,
6124 gfor_fndecl_set_max_subrecord_length, 1,
6125 build_int_cst (integer_type_node,
6126 flag_max_subrecord_length));
6127 gfc_add_expr_to_block (&body, tmp);
6130 /* Call MAIN__(). */
6131 tmp = build_call_expr_loc (input_location,
6132 fndecl, 0);
6133 gfc_add_expr_to_block (&body, tmp);
6135 /* Mark MAIN__ as used. */
6136 TREE_USED (fndecl) = 1;
6138 /* Coarray: Call _gfortran_caf_finalize(void). */
6139 if (flag_coarray == GFC_FCOARRAY_LIB)
6141 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_finalize, 0);
6142 gfc_add_expr_to_block (&body, tmp);
6145 /* "return 0". */
6146 tmp = fold_build2_loc (input_location, MODIFY_EXPR, integer_type_node,
6147 DECL_RESULT (ftn_main),
6148 build_int_cst (integer_type_node, 0));
6149 tmp = build1_v (RETURN_EXPR, tmp);
6150 gfc_add_expr_to_block (&body, tmp);
6153 DECL_SAVED_TREE (ftn_main) = gfc_finish_block (&body);
6154 decl = getdecls ();
6156 /* Finish off this function and send it for code generation. */
6157 poplevel (1, 1);
6158 BLOCK_SUPERCONTEXT (DECL_INITIAL (ftn_main)) = ftn_main;
6160 DECL_SAVED_TREE (ftn_main)
6161 = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (ftn_main),
6162 DECL_INITIAL (ftn_main));
6164 /* Output the GENERIC tree. */
6165 dump_function (TDI_original, ftn_main);
6167 cgraph_node::finalize_function (ftn_main, true);
6169 if (old_context)
6171 pop_function_context ();
6172 saved_function_decls = saved_parent_function_decls;
6174 current_function_decl = old_context;
6178 /* Generate an appropriate return-statement for a procedure. */
6180 tree
6181 gfc_generate_return (void)
6183 gfc_symbol* sym;
6184 tree result;
6185 tree fndecl;
6187 sym = current_procedure_symbol;
6188 fndecl = sym->backend_decl;
6190 if (TREE_TYPE (DECL_RESULT (fndecl)) == void_type_node)
6191 result = NULL_TREE;
6192 else
6194 result = get_proc_result (sym);
6196 /* Set the return value to the dummy result variable. The
6197 types may be different for scalar default REAL functions
6198 with -ff2c, therefore we have to convert. */
6199 if (result != NULL_TREE)
6201 result = convert (TREE_TYPE (DECL_RESULT (fndecl)), result);
6202 result = fold_build2_loc (input_location, MODIFY_EXPR,
6203 TREE_TYPE (result), DECL_RESULT (fndecl),
6204 result);
6208 return build1_v (RETURN_EXPR, result);
6212 static void
6213 is_from_ieee_module (gfc_symbol *sym)
6215 if (sym->from_intmod == INTMOD_IEEE_FEATURES
6216 || sym->from_intmod == INTMOD_IEEE_EXCEPTIONS
6217 || sym->from_intmod == INTMOD_IEEE_ARITHMETIC)
6218 seen_ieee_symbol = 1;
6222 static int
6223 is_ieee_module_used (gfc_namespace *ns)
6225 seen_ieee_symbol = 0;
6226 gfc_traverse_ns (ns, is_from_ieee_module);
6227 return seen_ieee_symbol;
6231 static gfc_omp_clauses *module_oacc_clauses;
6234 static void
6235 add_clause (gfc_symbol *sym, gfc_omp_map_op map_op)
6237 gfc_omp_namelist *n;
6239 n = gfc_get_omp_namelist ();
6240 n->sym = sym;
6241 n->u.map_op = map_op;
6243 if (!module_oacc_clauses)
6244 module_oacc_clauses = gfc_get_omp_clauses ();
6246 if (module_oacc_clauses->lists[OMP_LIST_MAP])
6247 n->next = module_oacc_clauses->lists[OMP_LIST_MAP];
6249 module_oacc_clauses->lists[OMP_LIST_MAP] = n;
6253 static void
6254 find_module_oacc_declare_clauses (gfc_symbol *sym)
6256 if (sym->attr.use_assoc)
6258 gfc_omp_map_op map_op;
6260 if (sym->attr.oacc_declare_create)
6261 map_op = OMP_MAP_FORCE_ALLOC;
6263 if (sym->attr.oacc_declare_copyin)
6264 map_op = OMP_MAP_FORCE_TO;
6266 if (sym->attr.oacc_declare_deviceptr)
6267 map_op = OMP_MAP_FORCE_DEVICEPTR;
6269 if (sym->attr.oacc_declare_device_resident)
6270 map_op = OMP_MAP_DEVICE_RESIDENT;
6272 if (sym->attr.oacc_declare_create
6273 || sym->attr.oacc_declare_copyin
6274 || sym->attr.oacc_declare_deviceptr
6275 || sym->attr.oacc_declare_device_resident)
6277 sym->attr.referenced = 1;
6278 add_clause (sym, map_op);
6284 void
6285 finish_oacc_declare (gfc_namespace *ns, gfc_symbol *sym, bool block)
6287 gfc_code *code;
6288 gfc_oacc_declare *oc;
6289 locus where = gfc_current_locus;
6290 gfc_omp_clauses *omp_clauses = NULL;
6291 gfc_omp_namelist *n, *p;
6293 gfc_traverse_ns (ns, find_module_oacc_declare_clauses);
6295 if (module_oacc_clauses && sym->attr.flavor == FL_PROGRAM)
6297 gfc_oacc_declare *new_oc;
6299 new_oc = gfc_get_oacc_declare ();
6300 new_oc->next = ns->oacc_declare;
6301 new_oc->clauses = module_oacc_clauses;
6303 ns->oacc_declare = new_oc;
6304 module_oacc_clauses = NULL;
6307 if (!ns->oacc_declare)
6308 return;
6310 for (oc = ns->oacc_declare; oc; oc = oc->next)
6312 if (oc->module_var)
6313 continue;
6315 if (block)
6316 gfc_error ("Sorry, !$ACC DECLARE at %L is not allowed "
6317 "in BLOCK construct", &oc->loc);
6320 if (oc->clauses && oc->clauses->lists[OMP_LIST_MAP])
6322 if (omp_clauses == NULL)
6324 omp_clauses = oc->clauses;
6325 continue;
6328 for (n = oc->clauses->lists[OMP_LIST_MAP]; n; p = n, n = n->next)
6331 gcc_assert (p->next == NULL);
6333 p->next = omp_clauses->lists[OMP_LIST_MAP];
6334 omp_clauses = oc->clauses;
6338 if (!omp_clauses)
6339 return;
6341 for (n = omp_clauses->lists[OMP_LIST_MAP]; n; n = n->next)
6343 switch (n->u.map_op)
6345 case OMP_MAP_DEVICE_RESIDENT:
6346 n->u.map_op = OMP_MAP_FORCE_ALLOC;
6347 break;
6349 default:
6350 break;
6354 code = XCNEW (gfc_code);
6355 code->op = EXEC_OACC_DECLARE;
6356 code->loc = where;
6358 code->ext.oacc_declare = gfc_get_oacc_declare ();
6359 code->ext.oacc_declare->clauses = omp_clauses;
6361 code->block = XCNEW (gfc_code);
6362 code->block->op = EXEC_OACC_DECLARE;
6363 code->block->loc = where;
6365 if (ns->code)
6366 code->block->next = ns->code;
6368 ns->code = code;
6370 return;
6374 /* Generate code for a function. */
6376 void
6377 gfc_generate_function_code (gfc_namespace * ns)
6379 tree fndecl;
6380 tree old_context;
6381 tree decl;
6382 tree tmp;
6383 tree fpstate = NULL_TREE;
6384 stmtblock_t init, cleanup;
6385 stmtblock_t body;
6386 gfc_wrapped_block try_block;
6387 tree recurcheckvar = NULL_TREE;
6388 gfc_symbol *sym;
6389 gfc_symbol *previous_procedure_symbol;
6390 int rank, ieee;
6391 bool is_recursive;
6393 sym = ns->proc_name;
6394 previous_procedure_symbol = current_procedure_symbol;
6395 current_procedure_symbol = sym;
6397 /* Initialize sym->tlink so that gfc_trans_deferred_vars does not get
6398 lost or worse. */
6399 sym->tlink = sym;
6401 /* Create the declaration for functions with global scope. */
6402 if (!sym->backend_decl)
6403 gfc_create_function_decl (ns, false);
6405 fndecl = sym->backend_decl;
6406 old_context = current_function_decl;
6408 if (old_context)
6410 push_function_context ();
6411 saved_parent_function_decls = saved_function_decls;
6412 saved_function_decls = NULL_TREE;
6415 trans_function_start (sym);
6417 gfc_init_block (&init);
6419 if (ns->entries && ns->proc_name->ts.type == BT_CHARACTER)
6421 /* Copy length backend_decls to all entry point result
6422 symbols. */
6423 gfc_entry_list *el;
6424 tree backend_decl;
6426 gfc_conv_const_charlen (ns->proc_name->ts.u.cl);
6427 backend_decl = ns->proc_name->result->ts.u.cl->backend_decl;
6428 for (el = ns->entries; el; el = el->next)
6429 el->sym->result->ts.u.cl->backend_decl = backend_decl;
6432 /* Translate COMMON blocks. */
6433 gfc_trans_common (ns);
6435 /* Null the parent fake result declaration if this namespace is
6436 a module function or an external procedures. */
6437 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
6438 || ns->parent == NULL)
6439 parent_fake_result_decl = NULL_TREE;
6441 gfc_generate_contained_functions (ns);
6443 has_coarray_vars = false;
6444 generate_local_vars (ns);
6446 if (flag_coarray == GFC_FCOARRAY_LIB && has_coarray_vars)
6447 generate_coarray_init (ns);
6449 /* Keep the parent fake result declaration in module functions
6450 or external procedures. */
6451 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
6452 || ns->parent == NULL)
6453 current_fake_result_decl = parent_fake_result_decl;
6454 else
6455 current_fake_result_decl = NULL_TREE;
6457 is_recursive = sym->attr.recursive
6458 || (sym->attr.entry_master
6459 && sym->ns->entries->sym->attr.recursive);
6460 if ((gfc_option.rtcheck & GFC_RTCHECK_RECURSION)
6461 && !is_recursive && !flag_recursive)
6463 char * msg;
6465 msg = xasprintf ("Recursive call to nonrecursive procedure '%s'",
6466 sym->name);
6467 recurcheckvar = gfc_create_var (logical_type_node, "is_recursive");
6468 TREE_STATIC (recurcheckvar) = 1;
6469 DECL_INITIAL (recurcheckvar) = logical_false_node;
6470 gfc_add_expr_to_block (&init, recurcheckvar);
6471 gfc_trans_runtime_check (true, false, recurcheckvar, &init,
6472 &sym->declared_at, msg);
6473 gfc_add_modify (&init, recurcheckvar, logical_true_node);
6474 free (msg);
6477 /* Check if an IEEE module is used in the procedure. If so, save
6478 the floating point state. */
6479 ieee = is_ieee_module_used (ns);
6480 if (ieee)
6481 fpstate = gfc_save_fp_state (&init);
6483 /* Now generate the code for the body of this function. */
6484 gfc_init_block (&body);
6486 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
6487 && sym->attr.subroutine)
6489 tree alternate_return;
6490 alternate_return = gfc_get_fake_result_decl (sym, 0);
6491 gfc_add_modify (&body, alternate_return, integer_zero_node);
6494 if (ns->entries)
6496 /* Jump to the correct entry point. */
6497 tmp = gfc_trans_entry_master_switch (ns->entries);
6498 gfc_add_expr_to_block (&body, tmp);
6501 /* If bounds-checking is enabled, generate code to check passed in actual
6502 arguments against the expected dummy argument attributes (e.g. string
6503 lengths). */
6504 if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !sym->attr.is_bind_c)
6505 add_argument_checking (&body, sym);
6507 finish_oacc_declare (ns, sym, false);
6509 tmp = gfc_trans_code (ns->code);
6510 gfc_add_expr_to_block (&body, tmp);
6512 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
6513 || (sym->result && sym->result != sym
6514 && sym->result->ts.type == BT_DERIVED
6515 && sym->result->ts.u.derived->attr.alloc_comp))
6517 bool artificial_result_decl = false;
6518 tree result = get_proc_result (sym);
6519 gfc_symbol *rsym = sym == sym->result ? sym : sym->result;
6521 /* Make sure that a function returning an object with
6522 alloc/pointer_components always has a result, where at least
6523 the allocatable/pointer components are set to zero. */
6524 if (result == NULL_TREE && sym->attr.function
6525 && ((sym->result->ts.type == BT_DERIVED
6526 && (sym->attr.allocatable
6527 || sym->attr.pointer
6528 || sym->result->ts.u.derived->attr.alloc_comp
6529 || sym->result->ts.u.derived->attr.pointer_comp))
6530 || (sym->result->ts.type == BT_CLASS
6531 && (CLASS_DATA (sym)->attr.allocatable
6532 || CLASS_DATA (sym)->attr.class_pointer
6533 || CLASS_DATA (sym->result)->attr.alloc_comp
6534 || CLASS_DATA (sym->result)->attr.pointer_comp))))
6536 artificial_result_decl = true;
6537 result = gfc_get_fake_result_decl (sym, 0);
6540 if (result != NULL_TREE && sym->attr.function && !sym->attr.pointer)
6542 if (sym->attr.allocatable && sym->attr.dimension == 0
6543 && sym->result == sym)
6544 gfc_add_modify (&init, result, fold_convert (TREE_TYPE (result),
6545 null_pointer_node));
6546 else if (sym->ts.type == BT_CLASS
6547 && CLASS_DATA (sym)->attr.allocatable
6548 && CLASS_DATA (sym)->attr.dimension == 0
6549 && sym->result == sym)
6551 tmp = CLASS_DATA (sym)->backend_decl;
6552 tmp = fold_build3_loc (input_location, COMPONENT_REF,
6553 TREE_TYPE (tmp), result, tmp, NULL_TREE);
6554 gfc_add_modify (&init, tmp, fold_convert (TREE_TYPE (tmp),
6555 null_pointer_node));
6557 else if (sym->ts.type == BT_DERIVED
6558 && !sym->attr.allocatable)
6560 gfc_expr *init_exp;
6561 /* Arrays are not initialized using the default initializer of
6562 their elements. Therefore only check if a default
6563 initializer is available when the result is scalar. */
6564 init_exp = rsym->as ? NULL
6565 : gfc_generate_initializer (&rsym->ts, true);
6566 if (init_exp)
6568 tmp = gfc_trans_structure_assign (result, init_exp, 0);
6569 gfc_free_expr (init_exp);
6570 gfc_add_expr_to_block (&init, tmp);
6572 else if (rsym->ts.u.derived->attr.alloc_comp)
6574 rank = rsym->as ? rsym->as->rank : 0;
6575 tmp = gfc_nullify_alloc_comp (rsym->ts.u.derived, result,
6576 rank);
6577 gfc_prepend_expr_to_block (&body, tmp);
6582 if (result == NULL_TREE || artificial_result_decl)
6584 /* TODO: move to the appropriate place in resolve.c. */
6585 if (warn_return_type > 0 && sym == sym->result)
6586 gfc_warning (OPT_Wreturn_type,
6587 "Return value of function %qs at %L not set",
6588 sym->name, &sym->declared_at);
6589 if (warn_return_type > 0)
6590 TREE_NO_WARNING(sym->backend_decl) = 1;
6592 if (result != NULL_TREE)
6593 gfc_add_expr_to_block (&body, gfc_generate_return ());
6596 gfc_init_block (&cleanup);
6598 /* Reset recursion-check variable. */
6599 if ((gfc_option.rtcheck & GFC_RTCHECK_RECURSION)
6600 && !is_recursive && !flag_openmp && recurcheckvar != NULL_TREE)
6602 gfc_add_modify (&cleanup, recurcheckvar, logical_false_node);
6603 recurcheckvar = NULL;
6606 /* If IEEE modules are loaded, restore the floating-point state. */
6607 if (ieee)
6608 gfc_restore_fp_state (&cleanup, fpstate);
6610 /* Finish the function body and add init and cleanup code. */
6611 tmp = gfc_finish_block (&body);
6612 gfc_start_wrapped_block (&try_block, tmp);
6613 /* Add code to create and cleanup arrays. */
6614 gfc_trans_deferred_vars (sym, &try_block);
6615 gfc_add_init_cleanup (&try_block, gfc_finish_block (&init),
6616 gfc_finish_block (&cleanup));
6618 /* Add all the decls we created during processing. */
6619 decl = nreverse (saved_function_decls);
6620 while (decl)
6622 tree next;
6624 next = DECL_CHAIN (decl);
6625 DECL_CHAIN (decl) = NULL_TREE;
6626 pushdecl (decl);
6627 decl = next;
6629 saved_function_decls = NULL_TREE;
6631 DECL_SAVED_TREE (fndecl) = gfc_finish_wrapped_block (&try_block);
6632 decl = getdecls ();
6634 /* Finish off this function and send it for code generation. */
6635 poplevel (1, 1);
6636 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6638 DECL_SAVED_TREE (fndecl)
6639 = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (fndecl),
6640 DECL_INITIAL (fndecl));
6642 /* Output the GENERIC tree. */
6643 dump_function (TDI_original, fndecl);
6645 /* Store the end of the function, so that we get good line number
6646 info for the epilogue. */
6647 cfun->function_end_locus = input_location;
6649 /* We're leaving the context of this function, so zap cfun.
6650 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6651 tree_rest_of_compilation. */
6652 set_cfun (NULL);
6654 if (old_context)
6656 pop_function_context ();
6657 saved_function_decls = saved_parent_function_decls;
6659 current_function_decl = old_context;
6661 if (decl_function_context (fndecl))
6663 /* Register this function with cgraph just far enough to get it
6664 added to our parent's nested function list.
6665 If there are static coarrays in this function, the nested _caf_init
6666 function has already called cgraph_create_node, which also created
6667 the cgraph node for this function. */
6668 if (!has_coarray_vars || flag_coarray != GFC_FCOARRAY_LIB)
6669 (void) cgraph_node::get_create (fndecl);
6671 else
6672 cgraph_node::finalize_function (fndecl, true);
6674 gfc_trans_use_stmts (ns);
6675 gfc_traverse_ns (ns, gfc_emit_parameter_debug_info);
6677 if (sym->attr.is_main_program)
6678 create_main_function (fndecl);
6680 current_procedure_symbol = previous_procedure_symbol;
6684 void
6685 gfc_generate_constructors (void)
6687 gcc_assert (gfc_static_ctors == NULL_TREE);
6688 #if 0
6689 tree fnname;
6690 tree type;
6691 tree fndecl;
6692 tree decl;
6693 tree tmp;
6695 if (gfc_static_ctors == NULL_TREE)
6696 return;
6698 fnname = get_file_function_name ("I");
6699 type = build_function_type_list (void_type_node, NULL_TREE);
6701 fndecl = build_decl (input_location,
6702 FUNCTION_DECL, fnname, type);
6703 TREE_PUBLIC (fndecl) = 1;
6705 decl = build_decl (input_location,
6706 RESULT_DECL, NULL_TREE, void_type_node);
6707 DECL_ARTIFICIAL (decl) = 1;
6708 DECL_IGNORED_P (decl) = 1;
6709 DECL_CONTEXT (decl) = fndecl;
6710 DECL_RESULT (fndecl) = decl;
6712 pushdecl (fndecl);
6714 current_function_decl = fndecl;
6716 rest_of_decl_compilation (fndecl, 1, 0);
6718 make_decl_rtl (fndecl);
6720 allocate_struct_function (fndecl, false);
6722 pushlevel ();
6724 for (; gfc_static_ctors; gfc_static_ctors = TREE_CHAIN (gfc_static_ctors))
6726 tmp = build_call_expr_loc (input_location,
6727 TREE_VALUE (gfc_static_ctors), 0);
6728 DECL_SAVED_TREE (fndecl) = build_stmt (input_location, EXPR_STMT, tmp);
6731 decl = getdecls ();
6732 poplevel (1, 1);
6734 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6735 DECL_SAVED_TREE (fndecl)
6736 = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (fndecl),
6737 DECL_INITIAL (fndecl));
6739 free_after_parsing (cfun);
6740 free_after_compilation (cfun);
6742 tree_rest_of_compilation (fndecl);
6744 current_function_decl = NULL_TREE;
6745 #endif
6748 /* Translates a BLOCK DATA program unit. This means emitting the
6749 commons contained therein plus their initializations. We also emit
6750 a globally visible symbol to make sure that each BLOCK DATA program
6751 unit remains unique. */
6753 void
6754 gfc_generate_block_data (gfc_namespace * ns)
6756 tree decl;
6757 tree id;
6759 /* Tell the backend the source location of the block data. */
6760 if (ns->proc_name)
6761 gfc_set_backend_locus (&ns->proc_name->declared_at);
6762 else
6763 gfc_set_backend_locus (&gfc_current_locus);
6765 /* Process the DATA statements. */
6766 gfc_trans_common (ns);
6768 /* Create a global symbol with the mane of the block data. This is to
6769 generate linker errors if the same name is used twice. It is never
6770 really used. */
6771 if (ns->proc_name)
6772 id = gfc_sym_mangled_function_id (ns->proc_name);
6773 else
6774 id = get_identifier ("__BLOCK_DATA__");
6776 decl = build_decl (input_location,
6777 VAR_DECL, id, gfc_array_index_type);
6778 TREE_PUBLIC (decl) = 1;
6779 TREE_STATIC (decl) = 1;
6780 DECL_IGNORED_P (decl) = 1;
6782 pushdecl (decl);
6783 rest_of_decl_compilation (decl, 1, 0);
6787 /* Process the local variables of a BLOCK construct. */
6789 void
6790 gfc_process_block_locals (gfc_namespace* ns)
6792 tree decl;
6794 saved_local_decls = NULL_TREE;
6795 has_coarray_vars = false;
6797 generate_local_vars (ns);
6799 if (flag_coarray == GFC_FCOARRAY_LIB && has_coarray_vars)
6800 generate_coarray_init (ns);
6802 decl = nreverse (saved_local_decls);
6803 while (decl)
6805 tree next;
6807 next = DECL_CHAIN (decl);
6808 DECL_CHAIN (decl) = NULL_TREE;
6809 pushdecl (decl);
6810 decl = next;
6812 saved_local_decls = NULL_TREE;
6816 #include "gt-fortran-trans-decl.h"