Debian Codegraph @

gcc/fortran/trans-decl.cc

raw
1 /* Backend function setup
2 Copyright (C) 2002-2025 Free Software Foundation, Inc.
3 Contributed by Paul Brook
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21 /* trans-decl.cc -- Handling of backend function and variable decls, etc */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "target.h"
27 #include "function.h"
28 #include "tree.h"
29 #include "gfortran.h"
30 #include "gimple-expr.h" /* For create_tmp_var_raw. */
31 #include "trans.h"
32 #include "stringpool.h"
33 #include "cgraph.h"
34 #include "fold-const.h"
35 #include "stor-layout.h"
36 #include "varasm.h"
37 #include "attribs.h"
38 #include "dumpfile.h"
39 #include "toplev.h" /* For announce_function. */
40 #include "debug.h"
41 #include "constructor.h"
42 #include "trans-types.h"
43 #include "trans-array.h"
44 #include "trans-const.h"
45 /* Only for gfc_trans_code. Shouldn't need to include this. */
46 #include "trans-stmt.h"
47 #include "gomp-constants.h"
48 #include "gimplify.h"
49 #include "context.h"
50 #include "omp-general.h"
51 #include "omp-offload.h"
52 #include "attr-fnspec.h"
53 #include "tree-iterator.h"
54 #include "dependency.h"
55
56 #define MAX_LABEL_VALUE 99999
57
58
59 /* Holds the result of the function if no result variable specified. */
60
61 static GTY(()) tree current_fake_result_decl;
62 static GTY(()) tree parent_fake_result_decl;
63
64
65 /* Holds the variable DECLs for the current function. */
66
67 static GTY(()) tree saved_function_decls;
68 static GTY(()) tree saved_parent_function_decls;
69
70 /* Holds the variable DECLs that are locals. */
71
72 static GTY(()) tree saved_local_decls;
73
74 /* The namespace of the module we're currently generating. Only used while
75 outputting decls for module variables. Do not rely on this being set. */
76
77 static gfc_namespace *module_namespace;
78
79 /* The currently processed procedure symbol. */
80 static gfc_symbol* current_procedure_symbol = NULL;
81
82 /* The currently processed module. */
83 static struct module_htab_entry *cur_module;
84
85 /* With -fcoarray=lib: For generating the registering call
86 of static coarrays. */
87 static bool has_coarray_vars_or_accessors;
88 static stmtblock_t caf_init_block;
89
90
91 /* List of static constructor functions. */
92
93 tree gfc_static_ctors;
94
95
96 /* Whether we've seen a symbol from an IEEE module in the namespace. */
97 static int seen_ieee_symbol;
98
99 /* Function declarations for builtin library functions. */
100
101 tree gfor_fndecl_pause_numeric;
102 tree gfor_fndecl_pause_string;
103 tree gfor_fndecl_stop_numeric;
104 tree gfor_fndecl_stop_string;
105 tree gfor_fndecl_error_stop_numeric;
106 tree gfor_fndecl_error_stop_string;
107 tree gfor_fndecl_runtime_error;
108 tree gfor_fndecl_runtime_error_at;
109 tree gfor_fndecl_runtime_warning_at;
110 tree gfor_fndecl_os_error_at;
111 tree gfor_fndecl_generate_error;
112 tree gfor_fndecl_set_args;
113 tree gfor_fndecl_set_fpe;
114 tree gfor_fndecl_set_options;
115 tree gfor_fndecl_set_convert;
116 tree gfor_fndecl_set_record_marker;
117 tree gfor_fndecl_set_max_subrecord_length;
118 tree gfor_fndecl_ctime;
119 tree gfor_fndecl_fdate;
120 tree gfor_fndecl_ttynam;
121 tree gfor_fndecl_in_pack;
122 tree gfor_fndecl_in_unpack;
123 tree gfor_fndecl_in_pack_class;
124 tree gfor_fndecl_in_unpack_class;
125 tree gfor_fndecl_associated;
126 tree gfor_fndecl_system_clock4;
127 tree gfor_fndecl_system_clock8;
128 tree gfor_fndecl_ieee_procedure_entry;
129 tree gfor_fndecl_ieee_procedure_exit;
130
131 /* Coarray run-time library function decls. */
132 tree gfor_fndecl_caf_init;
133 tree gfor_fndecl_caf_finalize;
134 tree gfor_fndecl_caf_this_image;
135 tree gfor_fndecl_caf_num_images;
136 tree gfor_fndecl_caf_register;
137 tree gfor_fndecl_caf_deregister;
138 tree gfor_fndecl_caf_register_accessor;
139 tree gfor_fndecl_caf_register_accessors_finish;
140 tree gfor_fndecl_caf_get_remote_function_index;
141 tree gfor_fndecl_caf_get_from_remote;
142 tree gfor_fndecl_caf_send_to_remote;
143 tree gfor_fndecl_caf_transfer_between_remotes;
144 tree gfor_fndecl_caf_sync_all;
145 tree gfor_fndecl_caf_sync_memory;
146 tree gfor_fndecl_caf_sync_images;
147 tree gfor_fndecl_caf_stop_str;
148 tree gfor_fndecl_caf_stop_numeric;
149 tree gfor_fndecl_caf_error_stop;
150 tree gfor_fndecl_caf_error_stop_str;
151 tree gfor_fndecl_caf_atomic_def;
152 tree gfor_fndecl_caf_atomic_ref;
153 tree gfor_fndecl_caf_atomic_cas;
154 tree gfor_fndecl_caf_atomic_op;
155 tree gfor_fndecl_caf_lock;
156 tree gfor_fndecl_caf_unlock;
157 tree gfor_fndecl_caf_event_post;
158 tree gfor_fndecl_caf_event_wait;
159 tree gfor_fndecl_caf_event_query;
160 tree gfor_fndecl_caf_fail_image;
161 tree gfor_fndecl_caf_failed_images;
162 tree gfor_fndecl_caf_image_status;
163 tree gfor_fndecl_caf_stopped_images;
164 tree gfor_fndecl_caf_form_team;
165 tree gfor_fndecl_caf_change_team;
166 tree gfor_fndecl_caf_end_team;
167 tree gfor_fndecl_caf_sync_team;
168 tree gfor_fndecl_caf_get_team;
169 tree gfor_fndecl_caf_team_number;
170 tree gfor_fndecl_co_broadcast;
171 tree gfor_fndecl_co_max;
172 tree gfor_fndecl_co_min;
173 tree gfor_fndecl_co_reduce;
174 tree gfor_fndecl_co_sum;
175 tree gfor_fndecl_caf_is_present_on_remote;
176 tree gfor_fndecl_caf_random_init;
177
178
179 /* Math functions. Many other math functions are handled in
180 trans-intrinsic.cc. */
181
182 gfc_powdecl_list gfor_fndecl_math_powi[4][3];
183 tree gfor_fndecl_unsigned_pow_list[5][5];
184
185 tree gfor_fndecl_math_ishftc4;
186 tree gfor_fndecl_math_ishftc8;
187 tree gfor_fndecl_math_ishftc16;
188
189
190 /* String functions. */
191
192 tree gfor_fndecl_compare_string;
193 tree gfor_fndecl_concat_string;
194 tree gfor_fndecl_string_len_trim;
195 tree gfor_fndecl_string_index;
196 tree gfor_fndecl_string_scan;
197 tree gfor_fndecl_string_verify;
198 tree gfor_fndecl_string_trim;
199 tree gfor_fndecl_string_minmax;
200 tree gfor_fndecl_adjustl;
201 tree gfor_fndecl_adjustr;
202 tree gfor_fndecl_select_string;
203 tree gfor_fndecl_compare_string_char4;
204 tree gfor_fndecl_concat_string_char4;
205 tree gfor_fndecl_string_len_trim_char4;
206 tree gfor_fndecl_string_index_char4;
207 tree gfor_fndecl_string_scan_char4;
208 tree gfor_fndecl_string_verify_char4;
209 tree gfor_fndecl_string_trim_char4;
210 tree gfor_fndecl_string_minmax_char4;
211 tree gfor_fndecl_adjustl_char4;
212 tree gfor_fndecl_adjustr_char4;
213 tree gfor_fndecl_select_string_char4;
214
215
216 /* Conversion between character kinds. */
217 tree gfor_fndecl_convert_char1_to_char4;
218 tree gfor_fndecl_convert_char4_to_char1;
219
220
221 /* Other misc. runtime library functions. */
222 tree gfor_fndecl_iargc;
223 tree gfor_fndecl_kill;
224 tree gfor_fndecl_kill_sub;
225 tree gfor_fndecl_is_contiguous0;
226
227
228 /* Intrinsic functions implemented in Fortran. */
229 tree gfor_fndecl_sc_kind;
230 tree gfor_fndecl_si_kind;
231 tree gfor_fndecl_sl_kind;
232 tree gfor_fndecl_sr_kind;
233
234 /* BLAS gemm functions. */
235 tree gfor_fndecl_sgemm;
236 tree gfor_fndecl_dgemm;
237 tree gfor_fndecl_cgemm;
238 tree gfor_fndecl_zgemm;
239
240 /* RANDOM_INIT function. */
241 tree gfor_fndecl_random_init; /* libgfortran, 1 image only. */
242
243 static void
244 gfc_add_decl_to_parent_function (tree decl)
245 {
246 gcc_assert (decl);
247 DECL_CONTEXT (decl) = DECL_CONTEXT (current_function_decl);
248 DECL_NONLOCAL (decl) = 1;
249 DECL_CHAIN (decl) = saved_parent_function_decls;
250 saved_parent_function_decls = decl;
251 }
252
253 void
254 gfc_add_decl_to_function (tree decl)
255 {
256 gcc_assert (decl);
257 TREE_USED (decl) = 1;
258 DECL_CONTEXT (decl) = current_function_decl;
259 DECL_CHAIN (decl) = saved_function_decls;
260 saved_function_decls = decl;
261 }
262
263 static void
264 add_decl_as_local (tree decl)
265 {
266 gcc_assert (decl);
267 TREE_USED (decl) = 1;
268 DECL_CONTEXT (decl) = current_function_decl;
269 DECL_CHAIN (decl) = saved_local_decls;
270 saved_local_decls = decl;
271 }
272
273
274 /* Build a backend label declaration. Set TREE_USED for named labels.
275 The context of the label is always the current_function_decl. All
276 labels are marked artificial. */
277
278 tree
279 gfc_build_label_decl (tree label_id)
280 {
281 /* 2^32 temporaries should be enough. */
282 static unsigned int tmp_num = 1;
283 tree label_decl;
284 char *label_name;
285
286 if (label_id == NULL_TREE)
287 {
288 /* Build an internal label name. */
289 ASM_FORMAT_PRIVATE_NAME (label_name, "L", tmp_num++);
290 label_id = get_identifier (label_name);
291 }
292 else
293 label_name = NULL;
294
295 /* Build the LABEL_DECL node. Labels have no type. */
296 label_decl = build_decl (input_location,
297 LABEL_DECL, label_id, void_type_node);
298 DECL_CONTEXT (label_decl) = current_function_decl;
299 SET_DECL_MODE (label_decl, VOIDmode);
300
301 /* We always define the label as used, even if the original source
302 file never references the label. We don't want all kinds of
303 spurious warnings for old-style Fortran code with too many
304 labels. */
305 TREE_USED (label_decl) = 1;
306
307 DECL_ARTIFICIAL (label_decl) = 1;
308 return label_decl;
309 }
310
311
312 /* Set the backend source location of a decl. */
313
314 void
315 gfc_set_decl_location (tree decl, locus * loc)
316 {
317 DECL_SOURCE_LOCATION (decl) = gfc_get_location (loc);
318 }
319
320
321 /* Return the backend label declaration for a given label structure,
322 or create it if it doesn't exist yet. */
323
324 tree
325 gfc_get_label_decl (gfc_st_label * lp)
326 {
327 if (lp->backend_decl)
328 return lp->backend_decl;
329 else
330 {
331 char label_name[GFC_MAX_SYMBOL_LEN + 1];
332 tree label_decl;
333
334 /* Validate the label declaration from the front end. */
335 gcc_assert (lp != NULL && lp->value <= MAX_LABEL_VALUE);
336
337 /* Build a mangled name for the label. */
338 if (lp->omp_region)
339 sprintf (label_name, "__label_%d_%.6d", lp->omp_region, lp->value);
340 else
341 sprintf (label_name, "__label_%.6d", lp->value);
342
343 /* Build the LABEL_DECL node. */
344 label_decl = gfc_build_label_decl (get_identifier (label_name));
345
346 /* Tell the debugger where the label came from. */
347 if (lp->value <= MAX_LABEL_VALUE) /* An internal label. */
348 gfc_set_decl_location (label_decl, &lp->where);
349 else
350 DECL_ARTIFICIAL (label_decl) = 1;
351
352 /* Store the label in the label list and return the LABEL_DECL. */
353 lp->backend_decl = label_decl;
354 return label_decl;
355 }
356 }
357
358 /* Return the name of an identifier. */
359
360 static const char *
361 sym_identifier (gfc_symbol *sym)
362 {
363 if (sym->attr.is_main_program && strcmp (sym->name, "main") == 0)
364 return "MAIN__";
365 else
366 return sym->name;
367 }
368
369 /* Convert a gfc_symbol to an identifier of the same name. */
370
371 static tree
372 gfc_sym_identifier (gfc_symbol * sym)
373 {
374 return get_identifier (sym_identifier (sym));
375 }
376
377 /* Construct mangled name from symbol name. */
378
379 static const char *
380 mangled_identifier (gfc_symbol *sym)
381 {
382 gfc_symbol *proc = sym->ns->proc_name;
383 static char name[3*GFC_MAX_MANGLED_SYMBOL_LEN + 14];
384 /* Prevent the mangling of identifiers that have an assigned
385 binding label (mainly those that are bind(c)). */
386
387 if (sym->attr.is_bind_c == 1 && sym->binding_label)
388 return sym->binding_label;
389
390 if (!sym->fn_result_spec
391 || (sym->module && !(proc && proc->attr.flavor == FL_PROCEDURE)))
392 {
393 if (sym->module == NULL)
394 return sym_identifier (sym);
395 else
396 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
397 }
398 else
399 {
400 /* This is an entity that is actually local to a module procedure
401 that appears in the result specification expression. Since
402 sym->module will be a zero length string, we use ns->proc_name
403 to provide the module name instead. */
404 if (proc && proc->module)
405 snprintf (name, sizeof name, "__%s_MOD__%s_PROC_%s",
406 proc->module, proc->name, sym->name);
407 else
408 snprintf (name, sizeof name, "__%s_PROC_%s",
409 proc->name, sym->name);
410 }
411
412 return name;
413 }
414
415 /* Get mangled identifier, adding the symbol to the global table if
416 it is not yet already there. */
417
418 static tree
419 gfc_sym_mangled_identifier (gfc_symbol * sym)
420 {
421 tree result;
422 gfc_gsymbol *gsym;
423 const char *name;
424
425 name = mangled_identifier (sym);
426 result = get_identifier (name);
427
428 gsym = gfc_find_gsymbol (gfc_gsym_root, name);
429 if (gsym == NULL)
430 {
431 gsym = gfc_get_gsymbol (name, false);
432 gsym->ns = sym->ns;
433 gsym->sym_name = sym->name;
434 }
435
436 return result;
437 }
438
439 /* Construct mangled function name from symbol name. */
440
441 static tree
442 gfc_sym_mangled_function_id (gfc_symbol * sym)
443 {
444 int has_underscore;
445 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
446
447 /* It may be possible to simply use the binding label if it's
448 provided, and remove the other checks. Then we could use it
449 for other things if we wished. */
450 if ((sym->attr.is_bind_c == 1 || sym->attr.is_iso_c == 1) &&
451 sym->binding_label)
452 /* use the binding label rather than the mangled name */
453 return get_identifier (sym->binding_label);
454
455 if ((sym->module == NULL || sym->attr.proc == PROC_EXTERNAL
456 || (sym->module != NULL && (sym->attr.external
457 || sym->attr.if_source == IFSRC_IFBODY)))
458 && !sym->attr.module_procedure)
459 {
460 /* Main program is mangled into MAIN__. */
461 if (sym->attr.is_main_program)
462 return get_identifier ("MAIN__");
463
464 /* Intrinsic procedures are never mangled. */
465 if (sym->attr.proc == PROC_INTRINSIC)
466 return get_identifier (sym->name);
467
468 if (flag_underscoring)
469 {
470 has_underscore = strchr (sym->name, '_') != 0;
471 if (flag_second_underscore && has_underscore)
472 snprintf (name, sizeof name, "%s__", sym->name);
473 else
474 snprintf (name, sizeof name, "%s_", sym->name);
475 return get_identifier (name);
476 }
477 else
478 return get_identifier (sym->name);
479 }
480 else
481 {
482 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
483 return get_identifier (name);
484 }
485 }
486
487
488 void
489 gfc_set_decl_assembler_name (tree decl, tree name)
490 {
491 tree target_mangled = targetm.mangle_decl_assembler_name (decl, name);
492 SET_DECL_ASSEMBLER_NAME (decl, target_mangled);
493 }
494
495
496 /* Returns true if a variable of specified size should go on the stack. */
497
498 bool
499 gfc_can_put_var_on_stack (tree size)
500 {
501 unsigned HOST_WIDE_INT low;
502
503 if (!INTEGER_CST_P (size))
504 return 0;
505
506 if (flag_max_stack_var_size < 0)
507 return 1;
508
509 if (!tree_fits_uhwi_p (size))
510 return 0;
511
512 low = TREE_INT_CST_LOW (size);
513 if (low > (unsigned HOST_WIDE_INT) flag_max_stack_var_size)
514 return 0;
515
516 /* TODO: Set a per-function stack size limit. */
517
518 return 1;
519 }
520
521
522 /* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
523 an expression involving its corresponding pointer. There are
524 2 cases; one for variable size arrays, and one for everything else,
525 because variable-sized arrays require one fewer level of
526 indirection. */
527
528 static void
529 gfc_finish_cray_pointee (tree decl, gfc_symbol *sym)
530 {
531 tree ptr_decl = gfc_get_symbol_decl (sym->cp_pointer);
532 tree value;
533
534 /* Parameters need to be dereferenced. */
535 if (sym->cp_pointer->attr.dummy)
536 ptr_decl = build_fold_indirect_ref_loc (input_location,
537 ptr_decl);
538
539 /* Check to see if we're dealing with a variable-sized array. */
540 if (sym->attr.dimension
541 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
542 {
543 /* These decls will be dereferenced later, so we don't dereference
544 them here. */
545 value = convert (TREE_TYPE (decl), ptr_decl);
546 }
547 else
548 {
549 ptr_decl = convert (build_pointer_type (TREE_TYPE (decl)),
550 ptr_decl);
551 value = build_fold_indirect_ref_loc (input_location,
552 ptr_decl);
553 }
554
555 SET_DECL_VALUE_EXPR (decl, value);
556 DECL_HAS_VALUE_EXPR_P (decl) = 1;
557 GFC_DECL_CRAY_POINTEE (decl) = 1;
558 }
559
560
561 /* Finish processing of a declaration without an initial value. */
562
563 static void
564 gfc_finish_decl (tree decl)
565 {
566 gcc_assert (TREE_CODE (decl) == PARM_DECL
567 || DECL_INITIAL (decl) == NULL_TREE);
568
569 if (!VAR_P (decl))
570 return;
571
572 if (DECL_SIZE (decl) == NULL_TREE
573 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
574 layout_decl (decl, 0);
575
576 /* A few consistency checks. */
577 /* A static variable with an incomplete type is an error if it is
578 initialized. Also if it is not file scope. Otherwise, let it
579 through, but if it is not `extern' then it may cause an error
580 message later. */
581 /* An automatic variable with an incomplete type is an error. */
582
583 /* We should know the storage size. */
584 gcc_assert (DECL_SIZE (decl) != NULL_TREE
585 || (TREE_STATIC (decl)
586 ? (!DECL_INITIAL (decl) || !DECL_CONTEXT (decl))
587 : DECL_EXTERNAL (decl)));
588
589 /* The storage size should be constant. */
590 gcc_assert ((!DECL_EXTERNAL (decl) && !TREE_STATIC (decl))
591 || !DECL_SIZE (decl)
592 || TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST);
593 }
594
595
596 /* Handle setting of GFC_DECL_SCALAR* on DECL. */
597
598 void
599 gfc_finish_decl_attrs (tree decl, symbol_attribute *attr)
600 {
601 if (!attr->dimension && !attr->codimension)
602 {
603 /* Handle scalar allocatable variables. */
604 if (attr->allocatable)
605 {
606 gfc_allocate_lang_decl (decl);
607 GFC_DECL_SCALAR_ALLOCATABLE (decl) = 1;
608 }
609 /* Handle scalar pointer variables. */
610 if (attr->pointer)
611 {
612 gfc_allocate_lang_decl (decl);
613 GFC_DECL_SCALAR_POINTER (decl) = 1;
614 }
615 if (attr->target)
616 {
617 gfc_allocate_lang_decl (decl);
618 GFC_DECL_SCALAR_TARGET (decl) = 1;
619 }
620 }
621 }
622
623
624 /* Apply symbol attributes to a variable, and add it to the function scope. */
625
626 static void
627 gfc_finish_var_decl (tree decl, gfc_symbol * sym)
628 {
629 tree new_type;
630
631 /* Set DECL_VALUE_EXPR for Cray Pointees. */
632 if (sym->attr.cray_pointee)
633 gfc_finish_cray_pointee (decl, sym);
634
635 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
636 This is the equivalent of the TARGET variables.
637 We also need to set this if the variable is passed by reference in a
638 CALL statement. */
639 if (sym->attr.target)
640 TREE_ADDRESSABLE (decl) = 1;
641
642 /* If it wasn't used we wouldn't be getting it. */
643 TREE_USED (decl) = 1;
644
645 if (sym->attr.flavor == FL_PARAMETER
646 && (sym->attr.dimension || sym->ts.type == BT_DERIVED))
647 TREE_READONLY (decl) = 1;
648
649 /* Chain this decl to the pending declarations. Don't do pushdecl()
650 because this would add them to the current scope rather than the
651 function scope. */
652 if (current_function_decl != NULL_TREE)
653 {
654 if (sym->ns->proc_name
655 && (sym->ns->proc_name->backend_decl == current_function_decl
656 || sym->result == sym))
657 gfc_add_decl_to_function (decl);
658 else if (sym->ns->omp_affinity_iterators)
659 {
660 /* Iterator variables are block-local; other variables in the
661 iterator namespace (e.g. implicitly typed host-associated
662 ones used in locator expressions) belong in the enclosing
663 function. */
664 gfc_symbol *iter;
665 for (iter = sym->ns->omp_affinity_iterators; iter;
666 iter = iter->tlink)
667 if (iter == sym)
668 break;
669 if (iter)
670 add_decl_as_local (decl);
671 else
672 gfc_add_decl_to_function (decl);
673 }
674 else if (sym->ns->proc_name
675 && sym->ns->proc_name->attr.flavor == FL_LABEL)
676 /* This is a BLOCK construct. */
677 add_decl_as_local (decl);
678 else
679 gfc_add_decl_to_parent_function (decl);
680 }
681
682 if (sym->attr.cray_pointee)
683 return;
684
685 if(sym->attr.is_bind_c == 1 && sym->binding_label)
686 {
687 /* We need to put variables that are bind(c) into the common
688 segment of the object file, because this is what C would do.
689 gfortran would typically put them in either the BSS or
690 initialized data segments, and only mark them as common if
691 they were part of common blocks. However, if they are not put
692 into common space, then C cannot initialize global Fortran
693 variables that it interoperates with and the draft says that
694 either Fortran or C should be able to initialize it (but not
695 both, of course.) (J3/04-007, section 15.3). */
696 TREE_PUBLIC(decl) = 1;
697 DECL_COMMON(decl) = 1;
698 if (sym->attr.access == ACCESS_PRIVATE && !sym->attr.public_used)
699 {
700 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
701 DECL_VISIBILITY_SPECIFIED (decl) = true;
702 }
703 }
704
705 /* If a variable is USE associated, it's always external. */
706 if (sym->attr.use_assoc || sym->attr.used_in_submodule)
707 {
708 DECL_EXTERNAL (decl) = 1;
709 TREE_PUBLIC (decl) = 1;
710 }
711 else if (sym->fn_result_spec && !sym->ns->proc_name->module)
712 {
713
714 if (sym->ns->proc_name->attr.if_source != IFSRC_DECL)
715 DECL_EXTERNAL (decl) = 1;
716 else
717 TREE_STATIC (decl) = 1;
718
719 TREE_PUBLIC (decl) = 1;
720 }
721 else if (sym->module && !sym->attr.result && !sym->attr.dummy)
722 {
723 /* TODO: Don't set sym->module for result or dummy variables. */
724 gcc_assert (current_function_decl == NULL_TREE || sym->result == sym);
725
726 TREE_PUBLIC (decl) = 1;
727 TREE_STATIC (decl) = 1;
728 if (sym->attr.access == ACCESS_PRIVATE && !sym->attr.public_used)
729 {
730 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
731 DECL_VISIBILITY_SPECIFIED (decl) = true;
732 }
733 }
734
735 /* Derived types are a bit peculiar because of the possibility of
736 a default initializer; this must be applied each time the variable
737 comes into scope it therefore need not be static. These variables
738 are SAVE_NONE but have an initializer. Otherwise explicitly
739 initialized variables are SAVE_IMPLICIT and explicitly saved are
740 SAVE_EXPLICIT. */
741 if (!sym->attr.use_assoc
742 && (sym->attr.save != SAVE_NONE || sym->attr.data
743 || (sym->value && sym->ns->proc_name->attr.is_main_program)
744 || (flag_coarray == GFC_FCOARRAY_LIB
745 && sym->attr.codimension && !sym->attr.allocatable)))
746 TREE_STATIC (decl) = 1;
747
748 /* Treat asynchronous variables the same as volatile, for now. */
749 if (sym->attr.volatile_ || sym->attr.asynchronous)
750 {
751 TREE_THIS_VOLATILE (decl) = 1;
752 TREE_SIDE_EFFECTS (decl) = 1;
753 new_type = build_qualified_type (TREE_TYPE (decl), TYPE_QUAL_VOLATILE);
754 TREE_TYPE (decl) = new_type;
755 }
756
757 /* Keep variables larger than max-stack-var-size off stack. */
758 if (!(sym->ns->proc_name && sym->ns->proc_name->attr.recursive)
759 && !sym->attr.automatic
760 && !sym->attr.associate_var
761 && sym->attr.save != SAVE_EXPLICIT
762 && sym->attr.save != SAVE_IMPLICIT
763 && INTEGER_CST_P (DECL_SIZE_UNIT (decl))
764 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
765 /* Put variable length auto array pointers always into stack. */
766 && (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
767 || sym->attr.dimension == 0
768 || sym->as->type != AS_EXPLICIT
769 || sym->attr.pointer
770 || sym->attr.allocatable)
771 && !DECL_ARTIFICIAL (decl))
772 {
773 if (flag_max_stack_var_size > 0
774 && !(sym->ns->proc_name
775 && sym->ns->proc_name->attr.is_main_program))
776 gfc_warning (OPT_Wsurprising,
777 "Array %qs at %L is larger than limit set by "
778 "%<-fmax-stack-var-size=%>, moved from stack to static "
779 "storage. This makes the procedure unsafe when called "
780 "recursively, or concurrently from multiple threads. "
781 "Consider increasing the %<-fmax-stack-var-size=%> "
782 "limit (or use %<-frecursive%>, which implies "
783 "unlimited %<-fmax-stack-var-size%>) - or change the "
784 "code to use an ALLOCATABLE array. If the variable is "
785 "never accessed concurrently, this warning can be "
786 "ignored, and the variable could also be declared with "
787 "the SAVE attribute.",
788 sym->name, &sym->declared_at);
789
790 TREE_STATIC (decl) = 1;
791
792 /* Because the size of this variable isn't known until now, we may have
793 greedily added an initializer to this variable (in build_init_assign)
794 even though the max-stack-var-size indicates the variable should be
795 static. Therefore we rip out the automatic initializer here and
796 replace it with a static one. */
797 gfc_symtree *st = gfc_find_symtree (sym->ns->sym_root, sym->name);
798 gfc_code *prev = NULL;
799 gfc_code *code = sym->ns->code;
800 while (code && code->op == EXEC_INIT_ASSIGN)
801 {
802 /* Look for an initializer meant for this symbol. */
803 if (code->expr1->symtree == st)
804 {
805 if (prev)
806 prev->next = code->next;
807 else
808 sym->ns->code = code->next;
809
810 break;
811 }
812
813 prev = code;
814 code = code->next;
815 }
816 if (code && code->op == EXEC_INIT_ASSIGN)
817 {
818 /* Keep the init expression for a static initializer. */
819 sym->value = code->expr2;
820 /* Cleanup the defunct code object, without freeing the init expr. */
821 code->expr2 = NULL;
822 gfc_free_statement (code);
823 free (code);
824 }
825 }
826
827 /* Handle threadprivate variables. */
828 if (sym->attr.threadprivate
829 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
830 set_decl_tls_model (decl, decl_default_tls_model (decl));
831
832 if (sym->attr.omp_allocate && TREE_STATIC (decl))
833 {
834 struct gfc_omp_namelist *n;
835 for (n = sym->ns->omp_allocate; n; n = n->next)
836 if (n->sym == sym)
837 break;
838 tree alloc = gfc_conv_constant_to_tree (n->u2.allocator);
839 tree align = (n->u.align ? gfc_conv_constant_to_tree (n->u.align)
840 : NULL_TREE);
841 DECL_ATTRIBUTES (decl)
842 = tree_cons (get_identifier ("omp allocate"),
843 build_tree_list (alloc, align), DECL_ATTRIBUTES (decl));
844 }
845
846 /* Mark weak variables. */
847 if (sym->attr.ext_attr & (1 << EXT_ATTR_WEAK))
848 declare_weak (decl);
849
850 gfc_finish_decl_attrs (decl, &sym->attr);
851 }
852
853
854 /* Allocate the lang-specific part of a decl. */
855
856 void
857 gfc_allocate_lang_decl (tree decl)
858 {
859 if (DECL_LANG_SPECIFIC (decl) == NULL)
860 DECL_LANG_SPECIFIC (decl) = ggc_cleared_alloc<struct lang_decl> ();
861 }
862
863
864 /* Determine order of two symbol declarations. */
865
866 static bool
867 decl_order (gfc_symbol *sym1, gfc_symbol *sym2)
868 {
869 if (sym1->decl_order > sym2->decl_order)
870 return true;
871 else
872 return false;
873 }
874
875
876 /* Remember a symbol to generate initialization/cleanup code at function
877 entry/exit. */
878
879 static void
880 gfc_defer_symbol_init (gfc_symbol * sym)
881 {
882 gfc_symbol *p;
883 gfc_symbol *last;
884 gfc_symbol *head;
885
886 /* Don't add a symbol twice. */
887 if (sym->tlink)
888 return;
889
890 last = head = sym->ns->proc_name;
891 p = last->tlink;
892
893 gfc_function_dependency (sym, head);
894
895 /* Make sure that setup code for dummy variables which are used in the
896 setup of other variables is generated first. */
897 if (sym->attr.dummy)
898 {
899 /* Find the first dummy arg seen after us, or the first non-dummy arg.
900 This is a circular list, so don't go past the head. */
901 while (p != head
902 && (!p->attr.dummy || decl_order (p, sym)))
903 {
904 last = p;
905 p = p->tlink;
906 }
907 }
908 else if (sym->fn_result_dep)
909 {
910 /* In the case of non-dummy symbols with dependencies on an old-fashioned
911 function result (ie. proc_name = proc_name->result), make sure that the
912 order in the tlink chain is such that the code appears in declaration
913 order. This ensures that mutual dependencies between these symbols are
914 respected. */
915 while (p != head
916 && (!p->attr.result || decl_order (sym, p)))
917 {
918 last = p;
919 p = p->tlink;
920 }
921 }
922 /* Insert in between last and p. */
923 last->tlink = sym;
924 sym->tlink = p;
925 }
926
927
928 /* Used in gfc_get_symbol_decl and gfc_get_derived_type to obtain the
929 backend_decl for a module symbol, if it all ready exists. If the
930 module gsymbol does not exist, it is created. If the symbol does
931 not exist, it is added to the gsymbol namespace. Returns true if
932 an existing backend_decl is found. */
933
934 bool
935 gfc_get_module_backend_decl (gfc_symbol *sym)
936 {
937 gfc_gsymbol *gsym;
938 gfc_symbol *s;
939 gfc_symtree *st;
940
941 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->module);
942
943 if (!gsym || (gsym->ns && gsym->type == GSYM_MODULE))
944 {
945 st = NULL;
946 s = NULL;
947
948 /* Check for a symbol with the same name. */
949 if (gsym)
950 gfc_find_symbol (sym->name, gsym->ns, 0, &s);
951
952 if (!s)
953 {
954 if (!gsym)
955 {
956 gsym = gfc_get_gsymbol (sym->module, false);
957 gsym->type = GSYM_MODULE;
958 gsym->ns = gfc_get_namespace (NULL, 0);
959 }
960
961 st = gfc_new_symtree (&gsym->ns->sym_root, sym->name);
962 st->n.sym = sym;
963 sym->refs++;
964 }
965 else if (gfc_fl_struct (sym->attr.flavor))
966 {
967 if (s && s->attr.flavor == FL_PROCEDURE)
968 {
969 gfc_interface *intr;
970 gcc_assert (s->attr.generic);
971 for (intr = s->generic; intr; intr = intr->next)
972 if (gfc_fl_struct (intr->sym->attr.flavor))
973 {
974 s = intr->sym;
975 break;
976 }
977 }
978
979 /* Normally we can assume that s is a derived-type symbol since it
980 shares a name with the derived-type sym. However if sym is a
981 STRUCTURE, it may in fact share a name with any other basic type
982 variable. If s is in fact of derived type then we can continue
983 looking for a duplicate type declaration. */
984 if (sym->attr.flavor == FL_STRUCT && s->ts.type == BT_DERIVED)
985 {
986 s = s->ts.u.derived;
987 }
988
989 if (gfc_fl_struct (s->attr.flavor) && !s->backend_decl)
990 {
991 if (s->attr.flavor == FL_UNION)
992 s->backend_decl = gfc_get_union_type (s);
993 else
994 s->backend_decl = gfc_get_derived_type (s);
995 }
996 gfc_copy_dt_decls_ifequal (s, sym, true);
997 return true;
998 }
999 else if (s->backend_decl)
1000 {
1001 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
1002 gfc_copy_dt_decls_ifequal (s->ts.u.derived, sym->ts.u.derived,
1003 true);
1004 else if (sym->ts.type == BT_CHARACTER)
1005 sym->ts.u.cl->backend_decl = s->ts.u.cl->backend_decl;
1006 sym->backend_decl = s->backend_decl;
1007 return true;
1008 }
1009 }
1010 return false;
1011 }
1012
1013
1014 /* Create an array index type variable with function scope. */
1015
1016 static tree
1017 create_index_var (const char * pfx, int nest)
1018 {
1019 tree decl;
1020
1021 decl = gfc_create_var_np (gfc_array_index_type, pfx);
1022 if (nest)
1023 gfc_add_decl_to_parent_function (decl);
1024 else
1025 gfc_add_decl_to_function (decl);
1026 return decl;
1027 }
1028
1029
1030 /* Create variables to hold all the non-constant bits of info for a
1031 descriptorless array. Remember these in the lang-specific part of the
1032 type. */
1033
1034 static void
1035 gfc_build_qualified_array (tree decl, gfc_symbol * sym)
1036 {
1037 tree type;
1038 int dim;
1039 int nest;
1040 gfc_namespace* procns;
1041 symbol_attribute *array_attr;
1042 gfc_array_spec *as;
1043 bool is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
1044
1045 type = TREE_TYPE (decl);
1046 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
1047 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
1048
1049 /* We just use the descriptor, if there is one. */
1050 if (GFC_DESCRIPTOR_TYPE_P (type))
1051 return;
1052
1053 gcc_assert (GFC_ARRAY_TYPE_P (type));
1054 procns = gfc_find_proc_namespace (sym->ns);
1055 nest = (procns->proc_name->backend_decl != current_function_decl)
1056 && !sym->attr.contained;
1057
1058 if (array_attr->codimension && flag_coarray == GFC_FCOARRAY_LIB
1059 && as->type != AS_ASSUMED_SHAPE
1060 && GFC_TYPE_ARRAY_CAF_TOKEN (type) == NULL_TREE)
1061 {
1062 tree token;
1063 tree token_type = build_qualified_type (pvoid_type_node,
1064 TYPE_QUAL_RESTRICT);
1065
1066 if (sym->module && (sym->attr.use_assoc
1067 || sym->ns->proc_name->attr.flavor == FL_MODULE))
1068 {
1069 tree token_name
1070 = get_identifier (gfc_get_string (GFC_PREFIX ("caf_token%s"),
1071 IDENTIFIER_POINTER (gfc_sym_mangled_identifier (sym))));
1072 token = build_decl (DECL_SOURCE_LOCATION (decl), VAR_DECL, token_name,
1073 token_type);
1074 if (sym->attr.use_assoc
1075 || (sym->attr.host_assoc && sym->attr.used_in_submodule))
1076 DECL_EXTERNAL (token) = 1;
1077 else
1078 TREE_STATIC (token) = 1;
1079
1080 TREE_PUBLIC (token) = 1;
1081
1082 if (sym->attr.access == ACCESS_PRIVATE && !sym->attr.public_used)
1083 {
1084 DECL_VISIBILITY (token) = VISIBILITY_HIDDEN;
1085 DECL_VISIBILITY_SPECIFIED (token) = true;
1086 }
1087 }
1088 else
1089 {
1090 token = gfc_create_var_np (token_type, "caf_token");
1091 TREE_STATIC (token) = 1;
1092 }
1093
1094 GFC_TYPE_ARRAY_CAF_TOKEN (type) = token;
1095 DECL_ARTIFICIAL (token) = 1;
1096 DECL_NONALIASED (token) = 1;
1097
1098 if (sym->module && !sym->attr.use_assoc)
1099 {
1100 module_htab_entry *mod
1101 = cur_module ? cur_module : gfc_find_module (sym->module);
1102 pushdecl (token);
1103 DECL_CONTEXT (token) = sym->ns->proc_name->backend_decl;
1104 gfc_module_add_decl (mod, token);
1105 }
1106 else if (sym->attr.host_assoc
1107 && TREE_CODE (DECL_CONTEXT (current_function_decl))
1108 != TRANSLATION_UNIT_DECL)
1109 gfc_add_decl_to_parent_function (token);
1110 else
1111 gfc_add_decl_to_function (token);
1112 }
1113
1114 for (dim = 0; dim < GFC_TYPE_ARRAY_RANK (type); dim++)
1115 {
1116 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
1117 {
1118 GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
1119 suppress_warning (GFC_TYPE_ARRAY_LBOUND (type, dim));
1120 }
1121 /* Don't try to use the unknown bound for assumed shape arrays. */
1122 if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
1123 && (as->type != AS_ASSUMED_SIZE
1124 || dim < GFC_TYPE_ARRAY_RANK (type) - 1))
1125 {
1126 GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
1127 suppress_warning (GFC_TYPE_ARRAY_UBOUND (type, dim));
1128 }
1129
1130 if (GFC_TYPE_ARRAY_STRIDE (type, dim) == NULL_TREE)
1131 {
1132 GFC_TYPE_ARRAY_STRIDE (type, dim) = create_index_var ("stride", nest);
1133 suppress_warning (GFC_TYPE_ARRAY_STRIDE (type, dim));
1134 }
1135 }
1136 for (dim = GFC_TYPE_ARRAY_RANK (type);
1137 dim < GFC_TYPE_ARRAY_RANK (type) + GFC_TYPE_ARRAY_CORANK (type); dim++)
1138 {
1139 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
1140 {
1141 GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
1142 suppress_warning (GFC_TYPE_ARRAY_LBOUND (type, dim));
1143 }
1144 /* Don't try to use the unknown ubound for the last coarray dimension. */
1145 if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
1146 && dim < GFC_TYPE_ARRAY_RANK (type) + GFC_TYPE_ARRAY_CORANK (type) - 1)
1147 {
1148 GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
1149 suppress_warning (GFC_TYPE_ARRAY_UBOUND (type, dim));
1150 }
1151 }
1152 if (GFC_TYPE_ARRAY_OFFSET (type) == NULL_TREE)
1153 {
1154 GFC_TYPE_ARRAY_OFFSET (type) = gfc_create_var_np (gfc_array_index_type,
1155 "offset");
1156 suppress_warning (GFC_TYPE_ARRAY_OFFSET (type));
1157
1158 if (nest)
1159 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type));
1160 else
1161 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type));
1162 }
1163
1164 if (GFC_TYPE_ARRAY_SIZE (type) == NULL_TREE && as->rank != 0
1165 && as->type != AS_ASSUMED_SIZE)
1166 {
1167 GFC_TYPE_ARRAY_SIZE (type) = create_index_var ("size", nest);
1168 suppress_warning (GFC_TYPE_ARRAY_SIZE (type));
1169 }
1170
1171 if (POINTER_TYPE_P (type))
1172 {
1173 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type)));
1174 gcc_assert (TYPE_LANG_SPECIFIC (type)
1175 == TYPE_LANG_SPECIFIC (TREE_TYPE (type)));
1176 type = TREE_TYPE (type);
1177 }
1178
1179 if (! COMPLETE_TYPE_P (type) && GFC_TYPE_ARRAY_SIZE (type))
1180 {
1181 tree size, range;
1182
1183 size = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
1184 GFC_TYPE_ARRAY_SIZE (type), gfc_index_one_node);
1185 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1186 size);
1187 TYPE_DOMAIN (type) = range;
1188 layout_type (type);
1189 }
1190
1191 if (TYPE_NAME (type) != NULL_TREE && as->rank > 0
1192 && GFC_TYPE_ARRAY_UBOUND (type, as->rank - 1) != NULL_TREE
1193 && VAR_P (GFC_TYPE_ARRAY_UBOUND (type, as->rank - 1)))
1194 {
1195 tree gtype = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1196
1197 for (dim = 0; dim < as->rank - 1; dim++)
1198 {
1199 gcc_assert (TREE_CODE (gtype) == ARRAY_TYPE);
1200 gtype = TREE_TYPE (gtype);
1201 }
1202 gcc_assert (TREE_CODE (gtype) == ARRAY_TYPE);
1203 if (TYPE_MAX_VALUE (TYPE_DOMAIN (gtype)) == NULL)
1204 TYPE_NAME (type) = NULL_TREE;
1205 }
1206
1207 if (TYPE_NAME (type) == NULL_TREE)
1208 {
1209 tree gtype = TREE_TYPE (type), rtype, type_decl;
1210
1211 for (dim = as->rank - 1; dim >= 0; dim--)
1212 {
1213 tree lbound, ubound;
1214 lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
1215 ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
1216 rtype = build_range_type (gfc_array_index_type, lbound, ubound);
1217 gtype = build_array_type (gtype, rtype);
1218 /* Ensure the bound variables aren't optimized out at -O0.
1219 For -O1 and above they often will be optimized out, but
1220 can be tracked by VTA. Also set DECL_NAMELESS, so that
1221 the artificial lbound.N or ubound.N DECL_NAME doesn't
1222 end up in debug info. */
1223 if (lbound
1224 && VAR_P (lbound)
1225 && DECL_ARTIFICIAL (lbound)
1226 && DECL_IGNORED_P (lbound))
1227 {
1228 if (DECL_NAME (lbound)
1229 && strstr (IDENTIFIER_POINTER (DECL_NAME (lbound)),
1230 "lbound") != 0)
1231 DECL_NAMELESS (lbound) = 1;
1232 DECL_IGNORED_P (lbound) = 0;
1233 }
1234 if (ubound
1235 && VAR_P (ubound)
1236 && DECL_ARTIFICIAL (ubound)
1237 && DECL_IGNORED_P (ubound))
1238 {
1239 if (DECL_NAME (ubound)
1240 && strstr (IDENTIFIER_POINTER (DECL_NAME (ubound)),
1241 "ubound") != 0)
1242 DECL_NAMELESS (ubound) = 1;
1243 DECL_IGNORED_P (ubound) = 0;
1244 }
1245 }
1246 TYPE_NAME (type) = type_decl = build_decl (input_location,
1247 TYPE_DECL, NULL, gtype);
1248 DECL_ORIGINAL_TYPE (type_decl) = gtype;
1249 }
1250 }
1251
1252
1253 /* For some dummy arguments we don't use the actual argument directly.
1254 Instead we create a local decl and use that. This allows us to perform
1255 initialization, and construct full type information. */
1256
1257 static tree
1258 gfc_build_dummy_array_decl (gfc_symbol * sym, tree dummy)
1259 {
1260 tree decl;
1261 tree type;
1262 gfc_array_spec *as;
1263 symbol_attribute *array_attr;
1264 char *name;
1265 gfc_packed packed;
1266 int n;
1267 bool known_size;
1268 bool is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
1269
1270 /* Use the array as and attr. */
1271 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
1272 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
1273
1274 /* The dummy is returned for pointer, allocatable or assumed rank arrays.
1275 For class arrays the information if sym is an allocatable or pointer
1276 object needs to be checked explicitly (IS_CLASS_ARRAY can be false for
1277 too many reasons to be of use here). */
1278 if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
1279 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
1280 || array_attr->allocatable
1281 || (as && as->type == AS_ASSUMED_RANK))
1282 return dummy;
1283
1284 /* Add to list of variables if not a fake result variable.
1285 These symbols are set on the symbol only, not on the class component. */
1286 if (sym->attr.result || sym->attr.dummy)
1287 gfc_defer_symbol_init (sym);
1288
1289 /* For a class array the array descriptor is in the _data component, while
1290 for a regular array the TREE_TYPE of the dummy is a pointer to the
1291 descriptor. */
1292 type = TREE_TYPE (is_classarray ? gfc_class_data_get (dummy)
1293 : TREE_TYPE (dummy));
1294 /* type now is the array descriptor w/o any indirection. */
1295 gcc_assert (TREE_CODE (dummy) == PARM_DECL
1296 && POINTER_TYPE_P (TREE_TYPE (dummy)));
1297
1298 /* Do we know the element size? */
1299 known_size = sym->ts.type != BT_CHARACTER
1300 || INTEGER_CST_P (sym->ts.u.cl->backend_decl);
1301
1302 if (known_size && !GFC_DESCRIPTOR_TYPE_P (type))
1303 {
1304 /* For descriptorless arrays with known element size the actual
1305 argument is sufficient. */
1306 gfc_build_qualified_array (dummy, sym);
1307 return dummy;
1308 }
1309
1310 if (GFC_DESCRIPTOR_TYPE_P (type))
1311 {
1312 /* Create a descriptorless array pointer. */
1313 packed = PACKED_NO;
1314
1315 /* Even when -frepack-arrays is used, symbols with TARGET attribute
1316 are not repacked. */
1317 if (!flag_repack_arrays || sym->attr.target)
1318 {
1319 if (as->type == AS_ASSUMED_SIZE)
1320 packed = PACKED_FULL;
1321 }
1322 else
1323 {
1324 if (as->type == AS_EXPLICIT)
1325 {
1326 packed = PACKED_FULL;
1327 for (n = 0; n < as->rank; n++)
1328 {
1329 if (!(as->upper[n]
1330 && as->lower[n]
1331 && as->upper[n]->expr_type == EXPR_CONSTANT
1332 && as->lower[n]->expr_type == EXPR_CONSTANT))
1333 {
1334 packed = PACKED_PARTIAL;
1335 break;
1336 }
1337 }
1338 }
1339 else
1340 packed = PACKED_PARTIAL;
1341 }
1342
1343 /* For classarrays the element type is required, but
1344 gfc_typenode_for_spec () returns the array descriptor. */
1345 type = is_classarray ? gfc_get_element_type (type)
1346 : gfc_typenode_for_spec (&sym->ts);
1347 type = gfc_get_nodesc_array_type (type, as, packed,
1348 !sym->attr.target);
1349 }
1350 else
1351 {
1352 /* We now have an expression for the element size, so create a fully
1353 qualified type. Reset sym->backend decl or this will just return the
1354 old type. */
1355 DECL_ARTIFICIAL (sym->backend_decl) = 1;
1356 sym->backend_decl = NULL_TREE;
1357 type = gfc_sym_type (sym);
1358 packed = PACKED_FULL;
1359 }
1360
1361 ASM_FORMAT_PRIVATE_NAME (name, IDENTIFIER_POINTER (DECL_NAME (dummy)), 0);
1362 decl = build_decl (input_location,
1363 VAR_DECL, get_identifier (name), type);
1364
1365 DECL_ARTIFICIAL (decl) = 1;
1366 DECL_NAMELESS (decl) = 1;
1367 TREE_PUBLIC (decl) = 0;
1368 TREE_STATIC (decl) = 0;
1369 DECL_EXTERNAL (decl) = 0;
1370
1371 /* Avoid uninitialized warnings for optional dummy arguments. */
1372 if ((sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.optional)
1373 || sym->attr.optional)
1374 suppress_warning (decl);
1375
1376 /* We should never get deferred shape arrays here. We used to because of
1377 frontend bugs. */
1378 gcc_assert (as->type != AS_DEFERRED);
1379
1380 if (packed == PACKED_PARTIAL)
1381 GFC_DECL_PARTIAL_PACKED_ARRAY (decl) = 1;
1382 else if (packed == PACKED_FULL)
1383 GFC_DECL_PACKED_ARRAY (decl) = 1;
1384
1385 gfc_build_qualified_array (decl, sym);
1386
1387 if (DECL_LANG_SPECIFIC (dummy))
1388 DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (dummy);
1389 else
1390 gfc_allocate_lang_decl (decl);
1391
1392 GFC_DECL_SAVED_DESCRIPTOR (decl) = dummy;
1393
1394 if (sym->ns->proc_name->backend_decl == current_function_decl
1395 || sym->attr.contained)
1396 gfc_add_decl_to_function (decl);
1397 else
1398 gfc_add_decl_to_parent_function (decl);
1399
1400 return decl;
1401 }
1402
1403 /* Return a constant or a variable to use as a string length. Does not
1404 add the decl to the current scope. */
1405
1406 static tree
1407 gfc_create_string_length (gfc_symbol * sym)
1408 {
1409 gcc_assert (sym->ts.u.cl);
1410 gfc_conv_const_charlen (sym->ts.u.cl);
1411
1412 if (sym->ts.u.cl->backend_decl == NULL_TREE)
1413 {
1414 tree length;
1415 const char *name;
1416
1417 /* The string length variable shall be in static memory if it is either
1418 explicitly SAVED, a module variable or with -fno-automatic. Only
1419 relevant is "len=:" - otherwise, it is either a constant length or
1420 it is an automatic variable. */
1421 bool static_length = sym->attr.save
1422 || sym->ns->proc_name->attr.flavor == FL_MODULE
1423 || (flag_max_stack_var_size == 0
1424 && sym->ts.deferred && !sym->attr.dummy
1425 && !sym->attr.result && !sym->attr.function);
1426
1427 /* Also prefix the mangled name. We need to call GFC_PREFIX for static
1428 variables as some systems do not support the "." in the assembler name.
1429 For nonstatic variables, the "." does not appear in assembler. */
1430 if (static_length)
1431 {
1432 if (sym->module)
1433 name = gfc_get_string (GFC_PREFIX ("%s_MOD_%s"), sym->module,
1434 sym->name);
1435 else
1436 name = gfc_get_string (GFC_PREFIX ("%s"), sym->name);
1437 }
1438 else if (sym->module)
1439 name = gfc_get_string (".__%s_MOD_%s", sym->module, sym->name);
1440 else
1441 name = gfc_get_string (".%s", sym->name);
1442
1443 length = build_decl (input_location,
1444 VAR_DECL, get_identifier (name),
1445 gfc_charlen_type_node);
1446 DECL_ARTIFICIAL (length) = 1;
1447 TREE_USED (length) = 1;
1448 if (sym->ns->proc_name->tlink != NULL)
1449 gfc_defer_symbol_init (sym);
1450
1451 sym->ts.u.cl->backend_decl = length;
1452
1453 if (static_length)
1454 TREE_STATIC (length) = 1;
1455
1456 if (sym->ns->proc_name->attr.flavor == FL_MODULE
1457 && (sym->attr.access != ACCESS_PRIVATE || sym->attr.public_used))
1458 TREE_PUBLIC (length) = 1;
1459 }
1460
1461 gcc_assert (sym->ts.u.cl->backend_decl != NULL_TREE);
1462 return sym->ts.u.cl->backend_decl;
1463 }
1464
1465 /* If a variable is assigned a label, we add another two auxiliary
1466 variables. */
1467
1468 static void
1469 gfc_add_assign_aux_vars (gfc_symbol * sym)
1470 {
1471 tree addr;
1472 tree length;
1473 tree decl;
1474
1475 gcc_assert (sym->backend_decl);
1476
1477 decl = sym->backend_decl;
1478 gfc_allocate_lang_decl (decl);
1479 GFC_DECL_ASSIGN (decl) = 1;
1480 length = build_decl (input_location,
1481 VAR_DECL, create_tmp_var_name (sym->name),
1482 gfc_charlen_type_node);
1483 addr = build_decl (input_location,
1484 VAR_DECL, create_tmp_var_name (sym->name),
1485 pvoid_type_node);
1486 gfc_finish_var_decl (length, sym);
1487 gfc_finish_var_decl (addr, sym);
1488 /* STRING_LENGTH is also used as flag. Less than -1 means that
1489 ASSIGN_ADDR cannot be used. Equal -1 means that ASSIGN_ADDR is the
1490 target label's address. Otherwise, value is the length of a format string
1491 and ASSIGN_ADDR is its address. */
1492 if (TREE_STATIC (length))
1493 DECL_INITIAL (length) = build_int_cst (gfc_charlen_type_node, -2);
1494 else
1495 gfc_defer_symbol_init (sym);
1496
1497 GFC_DECL_STRING_LEN (decl) = length;
1498 GFC_DECL_ASSIGN_ADDR (decl) = addr;
1499 }
1500
1501
1502 static void
1503 add_attributes_to_decl (tree *decl_p, const gfc_symbol *sym)
1504 {
1505 unsigned id;
1506 tree list = NULL_TREE;
1507 symbol_attribute sym_attr = sym->attr;
1508
1509 for (id = 0; id < EXT_ATTR_NUM; id++)
1510 if (sym_attr.ext_attr & (1 << id) && ext_attr_list[id].middle_end_name)
1511 {
1512 tree ident = get_identifier (ext_attr_list[id].middle_end_name);
1513 list = tree_cons (ident, NULL_TREE, list);
1514 }
1515
1516 tree clauses = NULL_TREE;
1517
1518 if (sym_attr.oacc_routine_lop != OACC_ROUTINE_LOP_NONE)
1519 {
1520 omp_clause_code code;
1521 switch (sym_attr.oacc_routine_lop)
1522 {
1523 case OACC_ROUTINE_LOP_GANG:
1524 code = OMP_CLAUSE_GANG;
1525 break;
1526 case OACC_ROUTINE_LOP_WORKER:
1527 code = OMP_CLAUSE_WORKER;
1528 break;
1529 case OACC_ROUTINE_LOP_VECTOR:
1530 code = OMP_CLAUSE_VECTOR;
1531 break;
1532 case OACC_ROUTINE_LOP_SEQ:
1533 code = OMP_CLAUSE_SEQ;
1534 break;
1535 case OACC_ROUTINE_LOP_NONE:
1536 case OACC_ROUTINE_LOP_ERROR:
1537 default:
1538 gcc_unreachable ();
1539 }
1540 tree c = build_omp_clause (UNKNOWN_LOCATION, code);
1541 OMP_CLAUSE_CHAIN (c) = clauses;
1542 clauses = c;
1543
1544 tree dims = oacc_build_routine_dims (clauses);
1545 list = oacc_replace_fn_attrib_attr (list, dims);
1546 }
1547
1548 if (sym_attr.oacc_routine_nohost)
1549 {
1550 tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_NOHOST);
1551 OMP_CLAUSE_CHAIN (c) = clauses;
1552 clauses = c;
1553 }
1554
1555 if (sym_attr.omp_device_type != OMP_DEVICE_TYPE_UNSET)
1556 {
1557 tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_DEVICE_TYPE);
1558 switch (sym_attr.omp_device_type)
1559 {
1560 case OMP_DEVICE_TYPE_HOST:
1561 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = OMP_CLAUSE_DEVICE_TYPE_HOST;
1562 break;
1563 case OMP_DEVICE_TYPE_NOHOST:
1564 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
1565 break;
1566 case OMP_DEVICE_TYPE_ANY:
1567 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = OMP_CLAUSE_DEVICE_TYPE_ANY;
1568 break;
1569 default:
1570 gcc_unreachable ();
1571 }
1572 OMP_CLAUSE_CHAIN (c) = clauses;
1573 clauses = c;
1574 }
1575
1576 bool has_declare = true;
1577 if (sym_attr.omp_declare_target_link
1578 || sym_attr.oacc_declare_link)
1579 list = tree_cons (get_identifier ("omp declare target link"),
1580 clauses, list);
1581 else if (sym_attr.omp_declare_target
1582 || sym_attr.oacc_declare_create
1583 || sym_attr.oacc_declare_copyin
1584 || sym_attr.oacc_declare_deviceptr
1585 || sym_attr.oacc_declare_device_resident)
1586 list = tree_cons (get_identifier ("omp declare target"),
1587 clauses, list);
1588 else
1589 has_declare = false;
1590
1591 if (sym_attr.omp_declare_target_indirect)
1592 list = tree_cons (get_identifier ("omp declare target indirect"),
1593 clauses, list);
1594
1595 decl_attributes (decl_p, list, 0);
1596
1597 if (has_declare
1598 && VAR_P (*decl_p)
1599 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1600 {
1601 has_declare = false;
1602 for (gfc_namespace* ns = sym->ns->contained; ns; ns = ns->sibling)
1603 if (ns->proc_name->attr.omp_declare_target)
1604 {
1605 has_declare = true;
1606 break;
1607 }
1608 }
1609
1610 if (has_declare && VAR_P (*decl_p) && has_declare)
1611 {
1612 /* Add to offload_vars; get_create does so for omp_declare_target,
1613 omp_declare_target_link requires manual work. */
1614 gcc_assert (symtab_node::get (*decl_p) == 0);
1615 symtab_node *node = symtab_node::get_create (*decl_p);
1616 if (node != NULL && sym_attr.omp_declare_target_link)
1617 {
1618 node->offloadable = 1;
1619 if (ENABLE_OFFLOADING)
1620 {
1621 g->have_offload = true;
1622 if (is_a <varpool_node *> (node))
1623 vec_safe_push (offload_vars, *decl_p);
1624 }
1625 }
1626 }
1627 }
1628
1629
1630 static void build_function_decl (gfc_symbol * sym, bool global);
1631
1632
1633 /* Return the decl for a gfc_symbol, create it if it doesn't already
1634 exist. */
1635
1636 tree
1637 gfc_get_symbol_decl (gfc_symbol * sym)
1638 {
1639 tree decl;
1640 tree length = NULL_TREE;
1641 int byref;
1642 bool intrinsic_array_parameter = false;
1643 bool fun_or_res;
1644
1645 gcc_assert (sym->attr.referenced
1646 || sym->attr.flavor == FL_PROCEDURE
1647 || sym->attr.use_assoc
1648 || sym->attr.used_in_submodule
1649 || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY
1650 || (sym->module && sym->attr.if_source != IFSRC_DECL
1651 && sym->backend_decl));
1652
1653 if (sym->attr.dummy && sym->ns->proc_name->attr.is_bind_c
1654 && is_CFI_desc (sym, NULL))
1655 {
1656 gcc_assert (sym->backend_decl && (sym->ts.type != BT_CHARACTER
1657 || sym->ts.u.cl->backend_decl));
1658 return sym->backend_decl;
1659 }
1660
1661 if (sym->ns && sym->ns->proc_name && sym->ns->proc_name->attr.function)
1662 byref = gfc_return_by_reference (sym->ns->proc_name);
1663 else
1664 byref = 0;
1665
1666 /* Make sure that the vtab for the declared type is completed. */
1667 if (sym->ts.type == BT_CLASS)
1668 {
1669 gfc_component *c = CLASS_DATA (sym);
1670 if (!c->ts.u.derived->backend_decl)
1671 {
1672 gfc_find_derived_vtab (c->ts.u.derived);
1673 gfc_get_derived_type (sym->ts.u.derived);
1674 }
1675 }
1676
1677 /* PDT parameterized array components and string_lengths must have the
1678 'len' parameters substituted for the expressions appearing in the
1679 declaration of the entity and memory allocated/deallocated. */
1680 if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
1681 && sym->param_list != NULL
1682 && gfc_current_ns == sym->ns
1683 && !(sym->attr.use_assoc || sym->attr.dummy))
1684 gfc_defer_symbol_init (sym);
1685
1686 /* Dummy PDT 'len' parameters should be checked when they are explicit. */
1687 if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
1688 && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
1689 && sym->param_list != NULL
1690 && sym->attr.dummy)
1691 gfc_defer_symbol_init (sym);
1692
1693 /* All deferred character length procedures need to retain the backend
1694 decl, which is a pointer to the character length in the caller's
1695 namespace and to declare a local character length. */
1696 if (!byref && sym->attr.function
1697 && sym->ts.type == BT_CHARACTER
1698 && sym->ts.deferred
1699 && sym->ts.u.cl->passed_length == NULL
1700 && sym->ts.u.cl->backend_decl
1701 && TREE_CODE (sym->ts.u.cl->backend_decl) == PARM_DECL)
1702 {
1703 sym->ts.u.cl->passed_length = sym->ts.u.cl->backend_decl;
1704 gcc_assert (POINTER_TYPE_P (TREE_TYPE (sym->ts.u.cl->passed_length)));
1705 sym->ts.u.cl->backend_decl = build_fold_indirect_ref (sym->ts.u.cl->backend_decl);
1706 }
1707
1708 fun_or_res = byref && (sym->attr.result
1709 || (sym->attr.function && sym->ts.deferred));
1710 if ((sym->attr.dummy && ! sym->attr.function) || fun_or_res)
1711 {
1712 /* Return via extra parameter. */
1713 if (sym->attr.result && byref
1714 && !sym->backend_decl)
1715 {
1716 sym->backend_decl =
1717 DECL_ARGUMENTS (sym->ns->proc_name->backend_decl);
1718 /* For entry master function skip over the __entry
1719 argument. */
1720 if (sym->ns->proc_name->attr.entry_master)
1721 sym->backend_decl = DECL_CHAIN (sym->backend_decl);
1722 }
1723
1724 /* Automatic array indices in module procedures need the backend_decl
1725 to be extracted from the procedure formal arglist. */
1726 if (sym->attr.dummy && !sym->backend_decl)
1727 {
1728 gfc_formal_arglist *f;
1729 for (f = sym->ns->proc_name->formal; f; f = f->next)
1730 {
1731 gfc_symbol *fsym = f->sym;
1732 if (strcmp (sym->name, fsym->name))
1733 continue;
1734 sym->backend_decl = fsym->backend_decl;
1735 break;
1736 }
1737 }
1738
1739 /* Dummy variables should already have been created. */
1740 gcc_assert (sym->backend_decl);
1741
1742 /* However, the string length of deferred arrays must be set. */
1743 if (sym->ts.type == BT_CHARACTER
1744 && sym->ts.deferred
1745 && sym->attr.dimension
1746 && sym->attr.allocatable)
1747 gfc_defer_symbol_init (sym);
1748
1749 if (sym->attr.pointer && sym->attr.dimension && sym->ts.type != BT_CLASS)
1750 GFC_DECL_PTR_ARRAY_P (sym->backend_decl) = 1;
1751
1752 /* Create a character length variable. */
1753 if (sym->ts.type == BT_CHARACTER)
1754 {
1755 /* For a deferred dummy, make a new string length variable. */
1756 if (sym->ts.deferred
1757 &&
1758 (sym->ts.u.cl->passed_length == sym->ts.u.cl->backend_decl))
1759 sym->ts.u.cl->backend_decl = NULL_TREE;
1760
1761 if (sym->ts.deferred && byref)
1762 {
1763 /* The string length of a deferred char array is stored in the
1764 parameter at sym->ts.u.cl->backend_decl as a reference and
1765 marked as a result. Exempt this variable from generating a
1766 temporary for it. */
1767 if (sym->attr.result)
1768 {
1769 /* We need to insert a indirect ref for param decls. */
1770 if (sym->ts.u.cl->backend_decl
1771 && TREE_CODE (sym->ts.u.cl->backend_decl) == PARM_DECL)
1772 {
1773 sym->ts.u.cl->passed_length = sym->ts.u.cl->backend_decl;
1774 sym->ts.u.cl->backend_decl =
1775 build_fold_indirect_ref (sym->ts.u.cl->backend_decl);
1776 }
1777 }
1778 /* For all other parameters make sure, that they are copied so
1779 that the value and any modifications are local to the routine
1780 by generating a temporary variable. */
1781 else if (sym->attr.function
1782 && sym->ts.u.cl->passed_length == NULL
1783 && sym->ts.u.cl->backend_decl)
1784 {
1785 sym->ts.u.cl->passed_length = sym->ts.u.cl->backend_decl;
1786 if (POINTER_TYPE_P (TREE_TYPE (sym->ts.u.cl->passed_length)))
1787 sym->ts.u.cl->backend_decl
1788 = build_fold_indirect_ref (sym->ts.u.cl->backend_decl);
1789 else
1790 sym->ts.u.cl->backend_decl = NULL_TREE;
1791 }
1792 }
1793
1794 if (sym->ts.u.cl->backend_decl == NULL_TREE)
1795 length = gfc_create_string_length (sym);
1796 else
1797 length = sym->ts.u.cl->backend_decl;
1798 if (VAR_P (length) && DECL_FILE_SCOPE_P (length))
1799 {
1800 /* Add the string length to the same context as the symbol. */
1801 if (DECL_CONTEXT (length) == NULL_TREE)
1802 {
1803 if (sym->backend_decl == current_function_decl
1804 || (DECL_CONTEXT (sym->backend_decl)
1805 == current_function_decl))
1806 gfc_add_decl_to_function (length);
1807 else
1808 gfc_add_decl_to_parent_function (length);
1809 }
1810
1811 gcc_assert (sym->backend_decl == current_function_decl
1812 ? DECL_CONTEXT (length) == current_function_decl
1813 : (DECL_CONTEXT (sym->backend_decl)
1814 == DECL_CONTEXT (length)));
1815
1816 gfc_defer_symbol_init (sym);
1817 }
1818 }
1819
1820 /* Use a copy of the descriptor for dummy arrays. */
1821 if ((sym->attr.dimension || sym->attr.codimension)
1822 && !TREE_USED (sym->backend_decl))
1823 {
1824 decl = gfc_build_dummy_array_decl (sym, sym->backend_decl);
1825 /* Prevent the dummy from being detected as unused if it is copied. */
1826 if (sym->backend_decl != NULL && decl != sym->backend_decl)
1827 DECL_ARTIFICIAL (sym->backend_decl) = 1;
1828 sym->backend_decl = decl;
1829 }
1830
1831 /* Returning the descriptor for dummy class arrays is hazardous, because
1832 some caller is expecting an expression to apply the component refs to.
1833 Therefore the descriptor is only created and stored in
1834 sym->backend_decl's GFC_DECL_SAVED_DESCRIPTOR. The caller is then
1835 responsible to extract it from there, when the descriptor is
1836 desired. */
1837 if (IS_CLASS_COARRAY_OR_ARRAY (sym)
1838 && (!DECL_LANG_SPECIFIC (sym->backend_decl)
1839 || !GFC_DECL_SAVED_DESCRIPTOR (sym->backend_decl)))
1840 {
1841 decl = gfc_build_dummy_array_decl (sym, sym->backend_decl);
1842 /* Prevent the dummy from being detected as unused if it is copied. */
1843 if (sym->backend_decl != NULL && decl != sym->backend_decl)
1844 DECL_ARTIFICIAL (sym->backend_decl) = 1;
1845 sym->backend_decl = decl;
1846 }
1847
1848 TREE_USED (sym->backend_decl) = 1;
1849 if (sym->attr.assign && GFC_DECL_ASSIGN (sym->backend_decl) == 0)
1850 gfc_add_assign_aux_vars (sym);
1851
1852 if (sym->ts.type == BT_CLASS && sym->backend_decl
1853 && !IS_CLASS_COARRAY_OR_ARRAY (sym))
1854 GFC_DECL_CLASS (sym->backend_decl) = 1;
1855
1856 return sym->backend_decl;
1857 }
1858
1859 if (sym->result == sym && sym->attr.assign
1860 && GFC_DECL_ASSIGN (sym->backend_decl) == 0)
1861 gfc_add_assign_aux_vars (sym);
1862
1863 if (sym->backend_decl)
1864 return sym->backend_decl;
1865
1866 /* Special case for array-valued named constants from intrinsic
1867 procedures; those are inlined. */
1868 if (sym->attr.use_assoc && sym->attr.flavor == FL_PARAMETER
1869 && (sym->from_intmod == INTMOD_ISO_FORTRAN_ENV
1870 || sym->from_intmod == INTMOD_ISO_C_BINDING))
1871 intrinsic_array_parameter = true;
1872
1873 /* If use associated compilation, use the module
1874 declaration. */
1875 if ((sym->attr.flavor == FL_VARIABLE
1876 || sym->attr.flavor == FL_PARAMETER)
1877 && (sym->attr.use_assoc || sym->attr.used_in_submodule)
1878 && !intrinsic_array_parameter
1879 && sym->module
1880 && gfc_get_module_backend_decl (sym))
1881 {
1882 if (sym->ts.type == BT_CLASS && sym->backend_decl)
1883 GFC_DECL_CLASS(sym->backend_decl) = 1;
1884 return sym->backend_decl;
1885 }
1886
1887 if (sym->attr.flavor == FL_PROCEDURE)
1888 {
1889 /* Catch functions. Only used for actual parameters,
1890 procedure pointers and procptr initialization targets. */
1891 if (sym->attr.use_assoc
1892 || sym->attr.used_in_submodule
1893 || sym->attr.intrinsic
1894 || sym->attr.if_source != IFSRC_DECL)
1895 {
1896 decl = gfc_get_extern_function_decl (sym);
1897 }
1898 else
1899 {
1900 if (!sym->backend_decl)
1901 build_function_decl (sym, false);
1902 decl = sym->backend_decl;
1903 }
1904 return decl;
1905 }
1906
1907 if (sym->ts.type == BT_UNKNOWN)
1908 gfc_fatal_error ("%s at %L has no default type", sym->name,
1909 &sym->declared_at);
1910
1911 if (sym->attr.intrinsic)
1912 gfc_internal_error ("intrinsic variable which isn't a procedure");
1913
1914 /* Create string length decl first so that they can be used in the
1915 type declaration. For associate names, the target character
1916 length is used. Set 'length' to a constant so that if the
1917 string length is a variable, it is not finished a second time. */
1918 if (sym->ts.type == BT_CHARACTER)
1919 {
1920 if (sym->attr.associate_var
1921 && sym->ts.deferred
1922 && sym->assoc && sym->assoc->target
1923 && ((sym->assoc->target->expr_type == EXPR_VARIABLE
1924 && sym->assoc->target->symtree->n.sym->ts.type != BT_CHARACTER)
1925 || sym->assoc->target->expr_type != EXPR_VARIABLE))
1926 sym->ts.u.cl->backend_decl = NULL_TREE;
1927
1928 if (sym->attr.associate_var
1929 && sym->ts.u.cl->backend_decl
1930 && (VAR_P (sym->ts.u.cl->backend_decl)
1931 || TREE_CODE (sym->ts.u.cl->backend_decl) == PARM_DECL))
1932 length = gfc_index_zero_node;
1933 else
1934 length = gfc_create_string_length (sym);
1935 }
1936
1937 /* Create the decl for the variable. */
1938 decl = build_decl (gfc_get_location (&sym->declared_at),
1939 VAR_DECL, gfc_sym_identifier (sym), gfc_sym_type (sym));
1940
1941 /* Symbols from modules should have their assembler names mangled.
1942 This is done here rather than in gfc_finish_var_decl because it
1943 is different for string length variables. */
1944 if (sym->module || sym->fn_result_spec)
1945 {
1946 gfc_set_decl_assembler_name (decl, gfc_sym_mangled_identifier (sym));
1947 if (sym->attr.use_assoc && !intrinsic_array_parameter)
1948 DECL_IGNORED_P (decl) = 1;
1949 }
1950
1951 if (sym->attr.select_type_temporary)
1952 {
1953 DECL_ARTIFICIAL (decl) = 1;
1954 DECL_IGNORED_P (decl) = 1;
1955 }
1956
1957 if (sym->attr.dimension || sym->attr.codimension)
1958 {
1959 /* Create variables to hold the non-constant bits of array info. */
1960 gfc_build_qualified_array (decl, sym);
1961
1962 if (sym->attr.contiguous
1963 || ((sym->attr.allocatable || !sym->attr.dummy) && !sym->attr.pointer))
1964 GFC_DECL_PACKED_ARRAY (decl) = 1;
1965 }
1966
1967 /* Remember this variable for allocation/cleanup. */
1968 if (sym->attr.dimension || sym->attr.allocatable || sym->attr.codimension
1969 || (sym->ts.type == BT_CLASS &&
1970 (CLASS_DATA (sym)->attr.dimension
1971 || CLASS_DATA (sym)->attr.allocatable))
1972 || (sym->ts.type == BT_DERIVED
1973 && (sym->ts.u.derived->attr.alloc_comp
1974 || (!sym->attr.pointer && !sym->attr.artificial && !sym->attr.save
1975 && !sym->ns->proc_name->attr.is_main_program
1976 && gfc_is_finalizable (sym->ts.u.derived, NULL))))
1977 /* This applies a derived type default initializer. */
1978 || (sym->ts.type == BT_DERIVED
1979 && sym->attr.save == SAVE_NONE
1980 && !sym->attr.data
1981 && !sym->attr.allocatable
1982 && (sym->value && !sym->ns->proc_name->attr.is_main_program)
1983 && !(sym->attr.use_assoc && !intrinsic_array_parameter)))
1984 gfc_defer_symbol_init (sym);
1985
1986 /* Set the vptr of unlimited polymorphic pointer variables so that
1987 they do not cause segfaults in select type, when the selector
1988 is an intrinsic type. Arrays are captured above. */
1989 if (sym->ts.type == BT_CLASS && UNLIMITED_POLY (sym)
1990 && CLASS_DATA (sym)->attr.class_pointer
1991 && !CLASS_DATA (sym)->attr.dimension && !sym->attr.dummy
1992 && sym->attr.flavor == FL_VARIABLE && !sym->assoc)
1993 gfc_defer_symbol_init (sym);
1994
1995 if (sym->ts.type == BT_CHARACTER
1996 && sym->attr.allocatable
1997 && !sym->attr.dimension
1998 && sym->ts.u.cl && sym->ts.u.cl->length
1999 && sym->ts.u.cl->length->expr_type == EXPR_VARIABLE)
2000 gfc_defer_symbol_init (sym);
2001
2002 /* Associate names can use the hidden string length variable
2003 of their associated target. */
2004 if (sym->ts.type == BT_CHARACTER
2005 && TREE_CODE (length) != INTEGER_CST
2006 && TREE_CODE (sym->ts.u.cl->backend_decl) != INDIRECT_REF)
2007 {
2008 length = fold_convert (gfc_charlen_type_node, length);
2009 gfc_finish_var_decl (length, sym);
2010 if (!sym->attr.associate_var
2011 && VAR_P (length)
2012 && sym->value && sym->value->expr_type != EXPR_NULL
2013 && sym->value->ts.u.cl->length)
2014 {
2015 gfc_expr *len = sym->value->ts.u.cl->length;
2016 DECL_INITIAL (length) = gfc_conv_initializer (len, &len->ts,
2017 TREE_TYPE (length),
2018 false, false, false);
2019 DECL_INITIAL (length) = fold_convert (gfc_charlen_type_node,
2020 DECL_INITIAL (length));
2021 }
2022 else
2023 gcc_assert (!sym->value || sym->value->expr_type == EXPR_NULL);
2024 }
2025
2026 gfc_finish_var_decl (decl, sym);
2027
2028 if (sym->ts.type == BT_CHARACTER)
2029 /* Character variables need special handling. */
2030 gfc_allocate_lang_decl (decl);
2031
2032 if (sym->assoc && sym->attr.subref_array_pointer)
2033 sym->attr.pointer = 1;
2034
2035 if (sym->attr.pointer && sym->attr.dimension
2036 && !sym->ts.deferred
2037 && !(sym->attr.select_type_temporary
2038 && !sym->attr.subref_array_pointer))
2039 GFC_DECL_PTR_ARRAY_P (decl) = 1;
2040
2041 if (sym->ts.type == BT_CLASS)
2042 GFC_DECL_CLASS(decl) = 1;
2043
2044 sym->backend_decl = decl;
2045
2046 if (sym->attr.assign)
2047 gfc_add_assign_aux_vars (sym);
2048
2049 if (intrinsic_array_parameter)
2050 {
2051 TREE_STATIC (decl) = 1;
2052 DECL_EXTERNAL (decl) = 0;
2053 }
2054
2055 if (TREE_STATIC (decl)
2056 && !(sym->attr.use_assoc && !intrinsic_array_parameter)
2057 && (sym->attr.save || sym->ns->proc_name->attr.is_main_program
2058 || !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
2059 || sym->attr.data || sym->ns->proc_name->attr.flavor == FL_MODULE)
2060 && (flag_coarray != GFC_FCOARRAY_LIB
2061 || !sym->attr.codimension || sym->attr.allocatable)
2062 && !(sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.pdt_type)
2063 && !(sym->ts.type == BT_CLASS
2064 && CLASS_DATA (sym)->ts.u.derived->attr.pdt_type))
2065 {
2066 /* Add static initializer. For procedures, it is only needed if
2067 SAVE is specified otherwise they need to be reinitialized
2068 every time the procedure is entered. The TREE_STATIC is
2069 in this case due to -fmax-stack-var-size=. */
2070
2071 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
2072 TREE_TYPE (decl), sym->attr.dimension
2073 || (sym->attr.codimension
2074 && sym->attr.allocatable),
2075 sym->attr.pointer || sym->attr.allocatable
2076 || sym->ts.type == BT_CLASS,
2077 sym->attr.proc_pointer);
2078 }
2079
2080 if (!TREE_STATIC (decl)
2081 && POINTER_TYPE_P (TREE_TYPE (decl))
2082 && !sym->attr.pointer
2083 && !sym->attr.allocatable
2084 && !sym->attr.proc_pointer
2085 && !sym->attr.select_type_temporary)
2086 DECL_BY_REFERENCE (decl) = 1;
2087
2088 if (sym->attr.associate_var)
2089 GFC_DECL_ASSOCIATE_VAR_P (decl) = 1;
2090
2091 /* We only longer mark __def_init as read-only if it actually has an
2092 initializer, it does not needlessly take up space in the
2093 read-only section and can go into the BSS instead, see PR 84487.
2094 Marking this as artificial means that OpenMP will treat this as
2095 predetermined shared. */
2096
2097 bool def_init = startswith (sym->name, "__def_init");
2098
2099 if (sym->attr.vtab || def_init)
2100 {
2101 DECL_ARTIFICIAL (decl) = 1;
2102 if (def_init && sym->value)
2103 TREE_READONLY (decl) = 1;
2104 }
2105
2106 /* Add attributes to variables. Functions are handled elsewhere. */
2107 add_attributes_to_decl (&decl, sym);
2108
2109 if (sym->ts.deferred && VAR_P (length))
2110 decl_attributes (&length, DECL_ATTRIBUTES (decl), 0);
2111
2112 return decl;
2113 }
2114
2115
2116 /* Substitute a temporary variable in place of the real one. */
2117
2118 void
2119 gfc_shadow_sym (gfc_symbol * sym, tree decl, gfc_saved_var * save)
2120 {
2121 save->attr = sym->attr;
2122 save->decl = sym->backend_decl;
2123
2124 gfc_clear_attr (&sym->attr);
2125 sym->attr.referenced = 1;
2126 sym->attr.flavor = FL_VARIABLE;
2127
2128 sym->backend_decl = decl;
2129 }
2130
2131
2132 /* Restore the original variable. */
2133
2134 void
2135 gfc_restore_sym (gfc_symbol * sym, gfc_saved_var * save)
2136 {
2137 sym->attr = save->attr;
2138 sym->backend_decl = save->decl;
2139 }
2140
2141
2142 /* Declare a procedure pointer. */
2143
2144 static tree
2145 get_proc_pointer_decl (gfc_symbol *sym)
2146 {
2147 tree decl;
2148
2149 if (sym->module || sym->fn_result_spec)
2150 {
2151 const char *name;
2152 gfc_gsymbol *gsym;
2153
2154 name = mangled_identifier (sym);
2155 gsym = gfc_find_gsymbol (gfc_gsym_root, name);
2156 if (gsym != NULL)
2157 {
2158 gfc_symbol *s;
2159 gfc_find_symbol (sym->name, gsym->ns, 0, &s);
2160 if (s && s->backend_decl)
2161 return s->backend_decl;
2162 }
2163 }
2164
2165 decl = sym->backend_decl;
2166 if (decl)
2167 return decl;
2168
2169 decl = build_decl (input_location,
2170 VAR_DECL, get_identifier (sym->name),
2171 build_pointer_type (gfc_get_function_type (sym)));
2172
2173 if (sym->module)
2174 {
2175 /* Apply name mangling. */
2176 gfc_set_decl_assembler_name (decl, gfc_sym_mangled_identifier (sym));
2177 if (sym->attr.use_assoc)
2178 DECL_IGNORED_P (decl) = 1;
2179 }
2180
2181 if ((sym->ns->proc_name
2182 && sym->ns->proc_name->backend_decl == current_function_decl)
2183 || sym->attr.contained)
2184 gfc_add_decl_to_function (decl);
2185 else if (sym->ns->proc_name->attr.flavor != FL_MODULE)
2186 gfc_add_decl_to_parent_function (decl);
2187
2188 sym->backend_decl = decl;
2189
2190 /* If a variable is USE associated, it's always external. */
2191 if (sym->attr.use_assoc)
2192 {
2193 DECL_EXTERNAL (decl) = 1;
2194 TREE_PUBLIC (decl) = 1;
2195 }
2196 else if (sym->module && sym->ns->proc_name->attr.flavor == FL_MODULE)
2197 {
2198 /* This is the declaration of a module variable. */
2199 TREE_PUBLIC (decl) = 1;
2200 if (sym->attr.access == ACCESS_PRIVATE && !sym->attr.public_used)
2201 {
2202 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2203 DECL_VISIBILITY_SPECIFIED (decl) = true;
2204 }
2205 TREE_STATIC (decl) = 1;
2206 }
2207
2208 if (!sym->attr.use_assoc
2209 && (sym->attr.save != SAVE_NONE || sym->attr.data
2210 || (sym->value && sym->ns->proc_name->attr.is_main_program)))
2211 TREE_STATIC (decl) = 1;
2212
2213 if (TREE_STATIC (decl) && sym->value)
2214 {
2215 /* Add static initializer. */
2216 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
2217 TREE_TYPE (decl),
2218 sym->attr.dimension,
2219 false, true);
2220 }
2221
2222 /* Handle threadprivate procedure pointers. */
2223 if (sym->attr.threadprivate
2224 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2225 set_decl_tls_model (decl, decl_default_tls_model (decl));
2226
2227 add_attributes_to_decl (&decl, sym);
2228
2229 return decl;
2230 }
2231
2232 static void
2233 create_function_arglist (gfc_symbol *sym);
2234
2235 /* Get a basic decl for an external function. */
2236
2237 tree
2238 gfc_get_extern_function_decl (gfc_symbol * sym, gfc_actual_arglist *actual_args,
2239 const char *fnspec)
2240 {
2241 tree type;
2242 tree fndecl;
2243 gfc_expr e;
2244 gfc_intrinsic_sym *isym;
2245 gfc_expr argexpr;
2246 char s[GFC_MAX_SYMBOL_LEN + 23]; /* "_gfortran_f2c_specific" and '\0'. */
2247 tree name;
2248 tree mangled_name;
2249 gfc_gsymbol *gsym;
2250
2251 if (sym->backend_decl)
2252 return sym->backend_decl;
2253
2254 /* We should never be creating external decls for alternate entry points.
2255 The procedure may be an alternate entry point, but we don't want/need
2256 to know that. */
2257 gcc_assert (!(sym->attr.entry || sym->attr.entry_master));
2258
2259 if (sym->attr.proc_pointer)
2260 return get_proc_pointer_decl (sym);
2261
2262 /* See if this is an external procedure from the same file. If so,
2263 return the backend_decl. If we are looking at a BIND(C)
2264 procedure and the symbol is not BIND(C), or vice versa, we
2265 haven't found the right procedure. */
2266
2267 if (sym->binding_label)
2268 {
2269 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
2270 if (gsym && !gsym->bind_c)
2271 gsym = NULL;
2272 }
2273 else if (sym->module == NULL)
2274 {
2275 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->name);
2276 if (gsym && gsym->bind_c)
2277 gsym = NULL;
2278 }
2279 else
2280 {
2281 /* Procedure from a different module. */
2282 gsym = NULL;
2283 }
2284
2285 if (gsym && !gsym->defined)
2286 gsym = NULL;
2287
2288 /* This can happen because of C binding. */
2289 if (gsym && gsym->ns && gsym->ns->proc_name
2290 && gsym->ns->proc_name->attr.flavor == FL_MODULE)
2291 goto module_sym;
2292
2293 if ((!sym->attr.use_assoc || sym->attr.if_source != IFSRC_DECL)
2294 && !sym->backend_decl
2295 && gsym && gsym->ns
2296 && ((gsym->type == GSYM_SUBROUTINE) || (gsym->type == GSYM_FUNCTION))
2297 && (gsym->ns->proc_name->backend_decl || !sym->attr.intrinsic))
2298 {
2299 if (!gsym->ns->proc_name->backend_decl)
2300 {
2301 /* By construction, the external function cannot be
2302 a contained procedure. */
2303 location_t old_loc = input_location;
2304 push_cfun (NULL);
2305
2306 gfc_create_function_decl (gsym->ns, true);
2307
2308 pop_cfun ();
2309 input_location = old_loc;
2310 }
2311
2312 /* If the namespace has entries, the proc_name is the
2313 entry master. Find the entry and use its backend_decl.
2314 otherwise, use the proc_name backend_decl. */
2315 if (gsym->ns->entries)
2316 {
2317 gfc_entry_list *entry = gsym->ns->entries;
2318
2319 for (; entry; entry = entry->next)
2320 {
2321 if (strcmp (gsym->name, entry->sym->name) == 0)
2322 {
2323 sym->backend_decl = entry->sym->backend_decl;
2324 break;
2325 }
2326 }
2327 }
2328 else
2329 sym->backend_decl = gsym->ns->proc_name->backend_decl;
2330
2331 if (sym->backend_decl)
2332 {
2333 /* Avoid problems of double deallocation of the backend declaration
2334 later in gfc_trans_use_stmts; cf. PR 45087. */
2335 if (sym->attr.if_source != IFSRC_DECL && sym->attr.use_assoc)
2336 sym->attr.use_assoc = 0;
2337
2338 return sym->backend_decl;
2339 }
2340 }
2341
2342 /* See if this is a module procedure from the same file. If so,
2343 return the backend_decl. */
2344 if (sym->module)
2345 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->module);
2346
2347 module_sym:
2348 if (gsym && gsym->ns
2349 && (gsym->type == GSYM_MODULE
2350 || (gsym->ns->proc_name && gsym->ns->proc_name->attr.flavor == FL_MODULE)))
2351 {
2352 gfc_symbol *s;
2353
2354 s = NULL;
2355 if (gsym->type == GSYM_MODULE)
2356 gfc_find_symbol (sym->name, gsym->ns, 0, &s);
2357 else
2358 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &s);
2359
2360 if (s && s->backend_decl)
2361 {
2362 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
2363 gfc_copy_dt_decls_ifequal (s->ts.u.derived, sym->ts.u.derived,
2364 true);
2365 else if (sym->ts.type == BT_CHARACTER)
2366 sym->ts.u.cl->backend_decl = s->ts.u.cl->backend_decl;
2367 sym->backend_decl = s->backend_decl;
2368 return sym->backend_decl;
2369 }
2370 }
2371
2372 if (sym->attr.intrinsic)
2373 {
2374 /* Call the resolution function to get the actual name. This is
2375 a nasty hack which relies on the resolution functions only looking
2376 at the first argument. We pass NULL for the second argument
2377 otherwise things like AINT get confused. */
2378 isym = gfc_find_function (sym->name);
2379 gcc_assert (isym->resolve.f0 != NULL);
2380
2381 memset (&e, 0, sizeof (e));
2382 e.expr_type = EXPR_FUNCTION;
2383
2384 memset (&argexpr, 0, sizeof (argexpr));
2385 gcc_assert (isym->formal);
2386 argexpr.ts = isym->formal->ts;
2387
2388 if (isym->formal->next == NULL)
2389 isym->resolve.f1 (&e, &argexpr);
2390 else
2391 {
2392 if (isym->formal->next->next == NULL)
2393 isym->resolve.f2 (&e, &argexpr, NULL);
2394 else
2395 {
2396 if (isym->formal->next->next->next == NULL)
2397 isym->resolve.f3 (&e, &argexpr, NULL, NULL);
2398 else
2399 {
2400 /* All specific intrinsics take less than 5 arguments. */
2401 gcc_assert (isym->formal->next->next->next->next == NULL);
2402 isym->resolve.f4 (&e, &argexpr, NULL, NULL, NULL);
2403 }
2404 }
2405 }
2406
2407 if (flag_f2c
2408 && ((e.ts.type == BT_REAL && e.ts.kind == gfc_default_real_kind)
2409 || e.ts.type == BT_COMPLEX))
2410 {
2411 /* Specific which needs a different implementation if f2c
2412 calling conventions are used. */
2413 sprintf (s, "_gfortran_f2c_specific%s", e.value.function.name);
2414 }
2415 else
2416 sprintf (s, "_gfortran_specific%s", e.value.function.name);
2417
2418 name = get_identifier (s);
2419 mangled_name = name;
2420 }
2421 else
2422 {
2423 name = gfc_sym_identifier (sym);
2424 mangled_name = gfc_sym_mangled_function_id (sym);
2425 }
2426
2427 type = gfc_get_function_type (sym, actual_args, fnspec);
2428
2429 fndecl = build_decl (gfc_get_location (&sym->declared_at),
2430 FUNCTION_DECL, name, type);
2431
2432 /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
2433 TREE_PUBLIC specifies whether a function is globally addressable (i.e.
2434 the opposite of declaring a function as static in C). */
2435 DECL_EXTERNAL (fndecl) = 1;
2436 TREE_PUBLIC (fndecl) = 1;
2437
2438 add_attributes_to_decl (&fndecl, sym);
2439
2440 gfc_set_decl_assembler_name (fndecl, mangled_name);
2441
2442 /* Set the context of this decl. */
2443 if (0 && sym->ns && sym->ns->proc_name)
2444 {
2445 /* TODO: Add external decls to the appropriate scope. */
2446 DECL_CONTEXT (fndecl) = sym->ns->proc_name->backend_decl;
2447 }
2448 else
2449 {
2450 /* Global declaration, e.g. intrinsic subroutine. */
2451 DECL_CONTEXT (fndecl) = NULL_TREE;
2452 }
2453
2454 /* Set attributes for PURE functions. A call to PURE function in the
2455 Fortran 95 sense is both pure and without side effects in the C
2456 sense. */
2457 if (sym->attr.pure || sym->attr.implicit_pure)
2458 {
2459 if (sym->attr.function && !gfc_return_by_reference (sym))
2460 DECL_PURE_P (fndecl) = 1;
2461 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
2462 parameters and don't use alternate returns (is this
2463 allowed?). In that case, calls to them are meaningless, and
2464 can be optimized away. See also in build_function_decl(). */
2465 TREE_SIDE_EFFECTS (fndecl) = 0;
2466 }
2467
2468 /* Mark non-returning functions. */
2469 if (sym->attr.noreturn || sym->attr.ext_attr & (1 << EXT_ATTR_NORETURN))
2470 TREE_THIS_VOLATILE(fndecl) = 1;
2471
2472 sym->backend_decl = fndecl;
2473
2474 if (DECL_CONTEXT (fndecl) == NULL_TREE)
2475 pushdecl_top_level (fndecl);
2476
2477 if (sym->formal_ns
2478 && sym->formal_ns->proc_name == sym)
2479 {
2480 if (sym->formal_ns->omp_declare_simd)
2481 gfc_trans_omp_declare_simd (sym->formal_ns);
2482 if (flag_openmp)
2483 {
2484 // We need DECL_ARGUMENTS to put attributes on, in case some arguments
2485 // need adjustment
2486 create_function_arglist (sym->formal_ns->proc_name);
2487 gfc_trans_omp_declare_variant (sym->formal_ns, sym->ns);
2488 }
2489 }
2490
2491 return fndecl;
2492 }
2493
2494
2495 /* Create a declaration for a procedure. For external functions (in the C
2496 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
2497 a master function with alternate entry points. */
2498
2499 static void
2500 build_function_decl (gfc_symbol * sym, bool global)
2501 {
2502 tree fndecl, type;
2503 symbol_attribute attr;
2504 tree result_decl;
2505 gfc_formal_arglist *f;
2506
2507 bool module_procedure = sym->attr.module_procedure
2508 && sym->ns
2509 && sym->ns->proc_name
2510 && sym->ns->proc_name->attr.flavor == FL_MODULE;
2511
2512 gcc_assert (!sym->attr.external || module_procedure);
2513
2514 if (sym->backend_decl)
2515 return;
2516
2517 /* Set the line and filename. sym->declared_at seems to point to the
2518 last statement for subroutines, but it'll do for now. */
2519 input_location = gfc_get_location (&sym->declared_at);
2520
2521 /* Allow only one nesting level. Allow public declarations. */
2522 gcc_assert (current_function_decl == NULL_TREE
2523 || DECL_FILE_SCOPE_P (current_function_decl)
2524 || (TREE_CODE (DECL_CONTEXT (current_function_decl))
2525 == FUNCTION_DECL)
2526 || (TREE_CODE (DECL_CONTEXT (current_function_decl))
2527 == NAMESPACE_DECL));
2528
2529 type = gfc_get_function_type (sym);
2530 fndecl = build_decl (input_location,
2531 FUNCTION_DECL, gfc_sym_identifier (sym), type);
2532
2533 attr = sym->attr;
2534
2535 /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
2536 TREE_PUBLIC specifies whether a function is globally addressable (i.e.
2537 the opposite of declaring a function as static in C). */
2538 DECL_EXTERNAL (fndecl) = 0;
2539
2540 if (sym->attr.access == ACCESS_UNKNOWN && sym->module
2541 && (sym->ns->default_access == ACCESS_PRIVATE
2542 || (sym->ns->default_access == ACCESS_UNKNOWN
2543 && flag_module_private)))
2544 sym->attr.access = ACCESS_PRIVATE;
2545
2546 if (!current_function_decl
2547 && !sym->attr.entry_master && !sym->attr.is_main_program
2548 && (sym->attr.access != ACCESS_PRIVATE || sym->binding_label
2549 || sym->attr.public_used))
2550 TREE_PUBLIC (fndecl) = 1;
2551
2552 if (sym->attr.referenced || sym->attr.entry_master)
2553 TREE_USED (fndecl) = 1;
2554
2555 add_attributes_to_decl (&fndecl, sym);
2556
2557 /* Figure out the return type of the declared function, and build a
2558 RESULT_DECL for it. If this is a subroutine with alternate
2559 returns, build a RESULT_DECL for it. */
2560 result_decl = NULL_TREE;
2561 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
2562 if (sym->attr.function)
2563 {
2564 if (gfc_return_by_reference (sym))
2565 type = void_type_node;
2566 else
2567 {
2568 if (sym->result != sym)
2569 result_decl = gfc_sym_identifier (sym->result);
2570
2571 type = TREE_TYPE (TREE_TYPE (fndecl));
2572 }
2573 }
2574 else
2575 {
2576 /* Look for alternate return placeholders. */
2577 int has_alternate_returns = 0;
2578 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2579 {
2580 if (f->sym == NULL)
2581 {
2582 has_alternate_returns = 1;
2583 break;
2584 }
2585 }
2586
2587 if (has_alternate_returns)
2588 type = integer_type_node;
2589 else
2590 type = void_type_node;
2591 }
2592
2593 result_decl = build_decl (input_location,
2594 RESULT_DECL, result_decl, type);
2595 DECL_ARTIFICIAL (result_decl) = 1;
2596 DECL_IGNORED_P (result_decl) = 1;
2597 DECL_CONTEXT (result_decl) = fndecl;
2598 DECL_RESULT (fndecl) = result_decl;
2599
2600 /* Don't call layout_decl for a RESULT_DECL.
2601 layout_decl (result_decl, 0); */
2602
2603 /* TREE_STATIC means the function body is defined here. */
2604 TREE_STATIC (fndecl) = 1;
2605
2606 /* Set attributes for PURE functions. A call to a PURE function in the
2607 Fortran 95 sense is both pure and without side effects in the C
2608 sense. */
2609 if (sym->attr.pure || sym->attr.implicit_pure)
2610 {
2611 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
2612 including an alternate return. In that case it can also be
2613 marked as PURE. See also in gfc_get_extern_function_decl(). */
2614 if (attr.function && !gfc_return_by_reference (sym))
2615 DECL_PURE_P (fndecl) = 1;
2616 TREE_SIDE_EFFECTS (fndecl) = 0;
2617 }
2618
2619 /* Mark noinline functions. */
2620 if (attr.ext_attr & (1 << EXT_ATTR_NOINLINE))
2621 DECL_UNINLINABLE (fndecl) = 1;
2622
2623 /* Mark noreturn functions. */
2624 if (attr.ext_attr & (1 << EXT_ATTR_NORETURN))
2625 TREE_THIS_VOLATILE (fndecl) = 1;
2626
2627 /* Mark weak functions. */
2628 if (attr.ext_attr & (1 << EXT_ATTR_WEAK))
2629 declare_weak (fndecl);
2630
2631 /* Layout the function declaration and put it in the binding level
2632 of the current function. */
2633
2634 if (global)
2635 pushdecl_top_level (fndecl);
2636 else
2637 pushdecl (fndecl);
2638
2639 /* Perform name mangling if this is a top level or module procedure. */
2640 if (current_function_decl == NULL_TREE)
2641 gfc_set_decl_assembler_name (fndecl, gfc_sym_mangled_function_id (sym));
2642
2643 sym->backend_decl = fndecl;
2644 }
2645
2646
2647 /* Create the DECL_ARGUMENTS for a procedure.
2648 NOTE: The arguments added here must match the argument type created by
2649 gfc_get_function_type (). */
2650
2651 static void
2652 create_function_arglist (gfc_symbol * sym)
2653 {
2654 tree fndecl;
2655 gfc_formal_arglist *f;
2656 tree typelist, hidden_typelist, optval_typelist;
2657 tree arglist, hidden_arglist, optval_arglist;
2658 tree type;
2659 tree parm;
2660
2661 fndecl = sym->backend_decl;
2662
2663 /* Build formal argument list. Make sure that their TREE_CONTEXT is
2664 the new FUNCTION_DECL node. */
2665 arglist = NULL_TREE;
2666 hidden_arglist = NULL_TREE;
2667 optval_arglist = NULL_TREE;
2668 typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2669
2670 if (sym->attr.entry_master)
2671 {
2672 type = TREE_VALUE (typelist);
2673 parm = build_decl (input_location,
2674 PARM_DECL, get_identifier ("__entry"), type);
2675
2676 DECL_CONTEXT (parm) = fndecl;
2677 DECL_ARG_TYPE (parm) = type;
2678 TREE_READONLY (parm) = 1;
2679 gfc_finish_decl (parm);
2680 DECL_ARTIFICIAL (parm) = 1;
2681
2682 arglist = chainon (arglist, parm);
2683 typelist = TREE_CHAIN (typelist);
2684 }
2685
2686 if (gfc_return_by_reference (sym))
2687 {
2688 tree type = TREE_VALUE (typelist), length = NULL;
2689
2690 if (sym->ts.type == BT_CHARACTER)
2691 {
2692 /* Length of character result. */
2693 tree len_type = TREE_VALUE (TREE_CHAIN (typelist));
2694
2695 length = build_decl (input_location,
2696 PARM_DECL,
2697 get_identifier (".__result"),
2698 len_type);
2699 if (POINTER_TYPE_P (len_type))
2700 {
2701 sym->ts.u.cl->passed_length = length;
2702 TREE_USED (length) = 1;
2703 }
2704 else if (!sym->ts.u.cl->length)
2705 {
2706 sym->ts.u.cl->backend_decl = length;
2707 TREE_USED (length) = 1;
2708 }
2709 gcc_assert (TREE_CODE (length) == PARM_DECL);
2710 DECL_CONTEXT (length) = fndecl;
2711 DECL_ARG_TYPE (length) = len_type;
2712 TREE_READONLY (length) = 1;
2713 DECL_ARTIFICIAL (length) = 1;
2714 gfc_finish_decl (length);
2715 if (sym->ts.u.cl->backend_decl == NULL
2716 || sym->ts.u.cl->backend_decl == length)
2717 {
2718 gfc_symbol *arg;
2719 tree backend_decl;
2720
2721 if (sym->ts.u.cl->backend_decl == NULL)
2722 {
2723 tree len = build_decl (input_location,
2724 VAR_DECL,
2725 get_identifier ("..__result"),
2726 gfc_charlen_type_node);
2727 DECL_ARTIFICIAL (len) = 1;
2728 TREE_USED (len) = 1;
2729 sym->ts.u.cl->backend_decl = len;
2730 }
2731
2732 /* Make sure PARM_DECL type doesn't point to incomplete type. */
2733 arg = sym->result ? sym->result : sym;
2734 backend_decl = arg->backend_decl;
2735 /* Temporary clear it, so that gfc_sym_type creates complete
2736 type. */
2737 arg->backend_decl = NULL;
2738 type = gfc_sym_type (arg);
2739 arg->backend_decl = backend_decl;
2740 type = build_reference_type (type);
2741 }
2742 }
2743
2744 parm = build_decl (input_location,
2745 PARM_DECL, get_identifier ("__result"), type);
2746
2747 DECL_CONTEXT (parm) = fndecl;
2748 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
2749 TREE_READONLY (parm) = 1;
2750 DECL_ARTIFICIAL (parm) = 1;
2751 gfc_finish_decl (parm);
2752
2753 arglist = chainon (arglist, parm);
2754 typelist = TREE_CHAIN (typelist);
2755
2756 if (sym->ts.type == BT_CHARACTER)
2757 {
2758 gfc_allocate_lang_decl (parm);
2759 arglist = chainon (arglist, length);
2760 typelist = TREE_CHAIN (typelist);
2761 }
2762 }
2763
2764 hidden_typelist = typelist;
2765 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2766 if (f->sym != NULL) /* Ignore alternate returns. */
2767 hidden_typelist = TREE_CHAIN (hidden_typelist);
2768
2769 /* Advance hidden_typelist over optional+value argument presence flags. */
2770 optval_typelist = hidden_typelist;
2771 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2772 if (f->sym != NULL
2773 && f->sym->attr.optional && f->sym->attr.value
2774 && !f->sym->attr.dimension && f->sym->ts.type != BT_CLASS)
2775 hidden_typelist = TREE_CHAIN (hidden_typelist);
2776
2777 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2778 {
2779 char name[GFC_MAX_SYMBOL_LEN + 2];
2780
2781 /* Ignore alternate returns. */
2782 if (f->sym == NULL)
2783 continue;
2784
2785 type = TREE_VALUE (typelist);
2786
2787 if (f->sym->ts.type == BT_CHARACTER
2788 && (!sym->attr.is_bind_c || sym->attr.entry_master))
2789 {
2790 tree len_type = TREE_VALUE (hidden_typelist);
2791 tree length = NULL_TREE;
2792 if (!f->sym->ts.deferred)
2793 gcc_assert (len_type == gfc_charlen_type_node);
2794 else
2795 gcc_assert (POINTER_TYPE_P (len_type));
2796
2797 strcpy (&name[1], f->sym->name);
2798 name[0] = '_';
2799 length = build_decl (input_location,
2800 PARM_DECL, get_identifier (name), len_type);
2801
2802 hidden_arglist = chainon (hidden_arglist, length);
2803 DECL_CONTEXT (length) = fndecl;
2804 DECL_ARTIFICIAL (length) = 1;
2805 DECL_ARG_TYPE (length) = len_type;
2806 TREE_READONLY (length) = 1;
2807 gfc_finish_decl (length);
2808
2809 /* Marking the length DECL_HIDDEN_STRING_LENGTH will lead
2810 to tail calls being disabled. Only do that if we
2811 potentially have broken callers. */
2812 if (flag_tail_call_workaround
2813 && f->sym->ts.u.cl
2814 && f->sym->ts.u.cl->length
2815 && f->sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
2816 && (flag_tail_call_workaround == 2
2817 || f->sym->ns->implicit_interface_calls))
2818 DECL_HIDDEN_STRING_LENGTH (length) = 1;
2819
2820 /* Remember the passed value. */
2821 if (!f->sym->ts.u.cl || f->sym->ts.u.cl->passed_length)
2822 {
2823 /* This can happen if the same type is used for multiple
2824 arguments. We need to copy cl as otherwise
2825 cl->passed_length gets overwritten. */
2826 f->sym->ts.u.cl = gfc_new_charlen (f->sym->ns, f->sym->ts.u.cl);
2827 }
2828 f->sym->ts.u.cl->passed_length = length;
2829
2830 /* Use the passed value for assumed length variables. */
2831 if (!f->sym->ts.u.cl->length)
2832 {
2833 TREE_USED (length) = 1;
2834 gcc_assert (!f->sym->ts.u.cl->backend_decl);
2835 f->sym->ts.u.cl->backend_decl = length;
2836 }
2837
2838 hidden_typelist = TREE_CHAIN (hidden_typelist);
2839
2840 if (f->sym->ts.u.cl->backend_decl == NULL
2841 || f->sym->ts.u.cl->backend_decl == length)
2842 {
2843 if (POINTER_TYPE_P (len_type))
2844 f->sym->ts.u.cl->backend_decl
2845 = build_fold_indirect_ref_loc (input_location, length);
2846 else if (f->sym->ts.u.cl->backend_decl == NULL)
2847 gfc_create_string_length (f->sym);
2848
2849 /* Make sure PARM_DECL type doesn't point to incomplete type. */
2850 if (f->sym->attr.flavor == FL_PROCEDURE)
2851 type = build_pointer_type (gfc_get_function_type (f->sym));
2852 else
2853 type = gfc_sym_type (f->sym);
2854 }
2855 }
2856 /* For scalar intrinsic types or derived types, VALUE passes the value,
2857 hence, the optional status cannot be transferred via a NULL pointer.
2858 Thus, we will use a hidden argument in that case. */
2859 if (f->sym->attr.optional && f->sym->attr.value
2860 && !f->sym->attr.dimension && f->sym->ts.type != BT_CLASS)
2861 {
2862 tree tmp;
2863 strcpy (&name[1], f->sym->name);
2864 name[0] = '.';
2865 tmp = build_decl (input_location,
2866 PARM_DECL, get_identifier (name),
2867 boolean_type_node);
2868
2869 optval_arglist = chainon (optval_arglist, tmp);
2870 DECL_CONTEXT (tmp) = fndecl;
2871 DECL_ARTIFICIAL (tmp) = 1;
2872 DECL_ARG_TYPE (tmp) = boolean_type_node;
2873 TREE_READONLY (tmp) = 1;
2874 gfc_finish_decl (tmp);
2875
2876 /* The presence flag must be boolean. */
2877 gcc_assert (TREE_VALUE (optval_typelist) == boolean_type_node);
2878 optval_typelist = TREE_CHAIN (optval_typelist);
2879 }
2880
2881 /* For non-constant length array arguments, make sure they use
2882 a different type node from TYPE_ARG_TYPES type. */
2883 if (f->sym->attr.dimension
2884 && type == TREE_VALUE (typelist)
2885 && TREE_CODE (type) == POINTER_TYPE
2886 && GFC_ARRAY_TYPE_P (type)
2887 && f->sym->as->type != AS_ASSUMED_SIZE
2888 && ! COMPLETE_TYPE_P (TREE_TYPE (type)))
2889 {
2890 if (f->sym->attr.flavor == FL_PROCEDURE)
2891 type = build_pointer_type (gfc_get_function_type (f->sym));
2892 else
2893 type = gfc_sym_type (f->sym);
2894 }
2895
2896 if (f->sym->attr.proc_pointer)
2897 type = build_pointer_type (type);
2898
2899 if (f->sym->attr.volatile_)
2900 type = build_qualified_type (type, TYPE_QUAL_VOLATILE);
2901
2902 /* Build the argument declaration. For C descriptors, we use a
2903 '_'-prefixed name for the parm_decl and inside the proc the
2904 sym->name. */
2905 tree parm_name;
2906 if (sym->attr.is_bind_c && is_CFI_desc (f->sym, NULL))
2907 {
2908 strcpy (&name[1], f->sym->name);
2909 name[0] = '_';
2910 parm_name = get_identifier (name);
2911 }
2912 else
2913 parm_name = gfc_sym_identifier (f->sym);
2914 parm = build_decl (input_location, PARM_DECL, parm_name, type);
2915
2916 if (f->sym->attr.volatile_)
2917 {
2918 TREE_THIS_VOLATILE (parm) = 1;
2919 TREE_SIDE_EFFECTS (parm) = 1;
2920 }
2921
2922 /* Fill in arg stuff. */
2923 DECL_CONTEXT (parm) = fndecl;
2924 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
2925 /* All implementation args except for VALUE are read-only. */
2926 if (!f->sym->attr.value)
2927 TREE_READONLY (parm) = 1;
2928 if (POINTER_TYPE_P (type)
2929 && (!f->sym->attr.proc_pointer
2930 && f->sym->attr.flavor != FL_PROCEDURE))
2931 DECL_BY_REFERENCE (parm) = 1;
2932 if (f->sym->attr.optional)
2933 {
2934 gfc_allocate_lang_decl (parm);
2935 GFC_DECL_OPTIONAL_ARGUMENT (parm) = 1;
2936 }
2937
2938 gfc_finish_decl (parm);
2939 gfc_finish_decl_attrs (parm, &f->sym->attr);
2940
2941 f->sym->backend_decl = parm;
2942
2943 /* Coarrays which are descriptorless or assumed-shape pass with
2944 -fcoarray=lib the token and the offset as hidden arguments. */
2945 if (flag_coarray == GFC_FCOARRAY_LIB
2946 && ((f->sym->ts.type != BT_CLASS && f->sym->attr.codimension
2947 && !f->sym->attr.allocatable)
2948 || (f->sym->ts.type == BT_CLASS
2949 && CLASS_DATA (f->sym)->attr.codimension
2950 && !CLASS_DATA (f->sym)->attr.allocatable)))
2951 {
2952 tree caf_type;
2953 tree token;
2954 tree offset;
2955
2956 gcc_assert (f->sym->backend_decl != NULL_TREE
2957 && !sym->attr.is_bind_c);
2958 caf_type = f->sym->ts.type == BT_CLASS
2959 ? TREE_TYPE (CLASS_DATA (f->sym)->backend_decl)
2960 : TREE_TYPE (f->sym->backend_decl);
2961
2962 token = build_decl (input_location, PARM_DECL,
2963 create_tmp_var_name ("caf_token"),
2964 build_qualified_type (pvoid_type_node,
2965 TYPE_QUAL_RESTRICT));
2966 if ((f->sym->ts.type != BT_CLASS
2967 && f->sym->as->type != AS_DEFERRED)
2968 || (f->sym->ts.type == BT_CLASS
2969 && CLASS_DATA (f->sym)->as->type != AS_DEFERRED))
2970 {
2971 gcc_assert (DECL_LANG_SPECIFIC (f->sym->backend_decl) == NULL
2972 || GFC_DECL_TOKEN (f->sym->backend_decl) == NULL_TREE);
2973 if (DECL_LANG_SPECIFIC (f->sym->backend_decl) == NULL)
2974 gfc_allocate_lang_decl (f->sym->backend_decl);
2975 GFC_DECL_TOKEN (f->sym->backend_decl) = token;
2976 }
2977 else
2978 {
2979 gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) == NULL_TREE);
2980 GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) = token;
2981 }
2982
2983 DECL_CONTEXT (token) = fndecl;
2984 DECL_ARTIFICIAL (token) = 1;
2985 DECL_ARG_TYPE (token) = TREE_VALUE (typelist);
2986 TREE_READONLY (token) = 1;
2987 hidden_arglist = chainon (hidden_arglist, token);
2988 hidden_typelist = TREE_CHAIN (hidden_typelist);
2989 gfc_finish_decl (token);
2990
2991 offset = build_decl (input_location, PARM_DECL,
2992 create_tmp_var_name ("caf_offset"),
2993 gfc_array_index_type);
2994
2995 if ((f->sym->ts.type != BT_CLASS
2996 && f->sym->as->type != AS_DEFERRED)
2997 || (f->sym->ts.type == BT_CLASS
2998 && CLASS_DATA (f->sym)->as->type != AS_DEFERRED))
2999 {
3000 gcc_assert (GFC_DECL_CAF_OFFSET (f->sym->backend_decl)
3001 == NULL_TREE);
3002 GFC_DECL_CAF_OFFSET (f->sym->backend_decl) = offset;
3003 }
3004 else
3005 {
3006 gcc_assert (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) == NULL_TREE);
3007 GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) = offset;
3008 }
3009 DECL_CONTEXT (offset) = fndecl;
3010 DECL_ARTIFICIAL (offset) = 1;
3011 DECL_ARG_TYPE (offset) = TREE_VALUE (typelist);
3012 TREE_READONLY (offset) = 1;
3013 hidden_arglist = chainon (hidden_arglist, offset);
3014 hidden_typelist = TREE_CHAIN (hidden_typelist);
3015 gfc_finish_decl (offset);
3016 }
3017
3018 arglist = chainon (arglist, parm);
3019 typelist = TREE_CHAIN (typelist);
3020 }
3021
3022 /* Add hidden present status for optional+value arguments. */
3023 arglist = chainon (arglist, optval_arglist);
3024
3025 /* Add the hidden string length parameters, unless the procedure
3026 is bind(C). */
3027 if (!sym->attr.is_bind_c)
3028 arglist = chainon (arglist, hidden_arglist);
3029
3030 gcc_assert (hidden_typelist == NULL_TREE
3031 || TREE_VALUE (hidden_typelist) == void_type_node);
3032 DECL_ARGUMENTS (fndecl) = arglist;
3033 }
3034
3035 /* Do the setup necessary before generating the body of a function. */
3036
3037 static void
3038 trans_function_start (gfc_symbol * sym)
3039 {
3040 tree fndecl;
3041
3042 fndecl = sym->backend_decl;
3043
3044 /* Let GCC know the current scope is this function. */
3045 current_function_decl = fndecl;
3046
3047 /* Let the world know what we're about to do. */
3048 announce_function (fndecl);
3049
3050 if (DECL_FILE_SCOPE_P (fndecl))
3051 {
3052 /* Create RTL for function declaration. */
3053 rest_of_decl_compilation (fndecl, 1, 0);
3054 }
3055
3056 /* Create RTL for function definition. */
3057 make_decl_rtl (fndecl);
3058
3059 allocate_struct_function (fndecl, false);
3060
3061 /* function.cc requires a push at the start of the function. */
3062 pushlevel ();
3063 }
3064
3065 /* Create thunks for alternate entry points. */
3066
3067 static void
3068 build_entry_thunks (gfc_namespace * ns, bool global)
3069 {
3070 gfc_formal_arglist *formal;
3071 gfc_formal_arglist *thunk_formal;
3072 gfc_entry_list *el;
3073 gfc_symbol *thunk_sym;
3074 stmtblock_t body;
3075 tree thunk_fndecl;
3076 tree tmp;
3077 location_t old_loc;
3078
3079 /* This should always be a toplevel function. */
3080 gcc_assert (current_function_decl == NULL_TREE);
3081
3082 old_loc = input_location;
3083 for (el = ns->entries; el; el = el->next)
3084 {
3085 vec<tree, va_gc> *args = NULL;
3086 vec<tree, va_gc> *string_args = NULL;
3087
3088 thunk_sym = el->sym;
3089
3090 build_function_decl (thunk_sym, global);
3091 create_function_arglist (thunk_sym);
3092
3093 trans_function_start (thunk_sym);
3094
3095 thunk_fndecl = thunk_sym->backend_decl;
3096
3097 gfc_init_block (&body);
3098
3099 /* Pass extra parameter identifying this entry point. */
3100 tmp = build_int_cst (gfc_array_index_type, el->id);
3101 vec_safe_push (args, tmp);
3102
3103 /* When the master returns by reference, pass the result reference
3104 and (for CHARACTER) the string length to the master call. If the
3105 thunk itself also returns by reference these are forwarded from
3106 its own argument list; otherwise (bind(c) CHARACTER entry) we
3107 create local temporaries and load the value after the call. */
3108 tree result_ref = NULL_TREE;
3109 if (thunk_sym->attr.function
3110 && gfc_return_by_reference (ns->proc_name))
3111 {
3112 if (gfc_return_by_reference (thunk_sym))
3113 {
3114 tree ref = DECL_ARGUMENTS (current_function_decl);
3115 vec_safe_push (args, ref);
3116 if (ns->proc_name->ts.type == BT_CHARACTER)
3117 vec_safe_push (args, DECL_CHAIN (ref));
3118 }
3119 else
3120 {
3121 /* The thunk is bind(c) and returns CHARACTER by value, but
3122 the master returns by reference. Create a local buffer
3123 and length to pass to the master call. */
3124 tree chartype = gfc_get_char_type (thunk_sym->ts.kind);
3125 tree len;
3126
3127 if (thunk_sym->ts.u.cl && thunk_sym->ts.u.cl->length)
3128 {
3129 gfc_se se;
3130 gfc_init_se (&se, NULL);
3131 gfc_conv_expr (&se, thunk_sym->ts.u.cl->length);
3132 gfc_add_block_to_block (&body, &se.pre);
3133 len = se.expr;
3134 gfc_add_block_to_block (&body, &se.post);
3135 }
3136 else
3137 len = build_int_cst (gfc_charlen_type_node, 1);
3138
3139 result_ref = build_decl (input_location, VAR_DECL,
3140 get_identifier ("__entry_result"),
3141 build_array_type (chartype,
3142 build_range_type (gfc_array_index_type,
3143 gfc_index_one_node,
3144 fold_convert (gfc_array_index_type,
3145 len))));
3146 DECL_ARTIFICIAL (result_ref) = 1;
3147 TREE_USED (result_ref) = 1;
3148 DECL_CONTEXT (result_ref) = current_function_decl;
3149 layout_decl (result_ref, 0);
3150 pushdecl (result_ref);
3151
3152 vec_safe_push (args,
3153 build_fold_addr_expr_loc (input_location,
3154 result_ref));
3155 vec_safe_push (args, len);
3156 }
3157 }
3158
3159 for (formal = gfc_sym_get_dummy_args (ns->proc_name); formal;
3160 formal = formal->next)
3161 {
3162 /* Ignore alternate returns. */
3163 if (formal->sym == NULL)
3164 continue;
3165
3166 /* We don't have a clever way of identifying arguments, so resort to
3167 a brute-force search. */
3168 for (thunk_formal = gfc_sym_get_dummy_args (thunk_sym);
3169 thunk_formal;
3170 thunk_formal = thunk_formal->next)
3171 {
3172 if (thunk_formal->sym == formal->sym)
3173 break;
3174 }
3175
3176 if (thunk_formal)
3177 {
3178 /* Pass the argument. */
3179 DECL_ARTIFICIAL (thunk_formal->sym->backend_decl) = 1;
3180 vec_safe_push (args, thunk_formal->sym->backend_decl);
3181 if (formal->sym->ts.type == BT_CHARACTER)
3182 {
3183 tmp = thunk_formal->sym->ts.u.cl->backend_decl;
3184 vec_safe_push (string_args, tmp);
3185 }
3186 }
3187 else
3188 {
3189 /* Pass NULL for a missing argument. */
3190 vec_safe_push (args, null_pointer_node);
3191 if (formal->sym->ts.type == BT_CHARACTER)
3192 {
3193 tmp = build_int_cst (gfc_charlen_type_node, 0);
3194 vec_safe_push (string_args, tmp);
3195 }
3196 }
3197 }
3198
3199 /* Call the master function. */
3200 vec_safe_splice (args, string_args);
3201 tmp = ns->proc_name->backend_decl;
3202 tmp = build_call_expr_loc_vec (input_location, tmp, args);
3203 if (result_ref != NULL_TREE)
3204 {
3205 /* The master returns by reference (void) but the bind(c) thunk
3206 returns CHARACTER by value. Execute the master call, then
3207 load the first character from the local buffer. */
3208 gfc_add_expr_to_block (&body, tmp);
3209 tmp = build4_loc (input_location, ARRAY_REF,
3210 TREE_TYPE (TREE_TYPE (result_ref)),
3211 result_ref, gfc_index_one_node,
3212 NULL_TREE, NULL_TREE);
3213 tmp = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)),
3214 tmp);
3215 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
3216 TREE_TYPE (DECL_RESULT (current_function_decl)),
3217 DECL_RESULT (current_function_decl), tmp);
3218 tmp = build1_v (RETURN_EXPR, tmp);
3219 }
3220 else if (ns->proc_name->attr.mixed_entry_master)
3221 {
3222 tree union_decl, field;
3223 tree master_type = TREE_TYPE (ns->proc_name->backend_decl);
3224
3225 union_decl = build_decl (input_location,
3226 VAR_DECL, get_identifier ("__result"),
3227 TREE_TYPE (master_type));
3228 DECL_ARTIFICIAL (union_decl) = 1;
3229 DECL_EXTERNAL (union_decl) = 0;
3230 TREE_PUBLIC (union_decl) = 0;
3231 TREE_USED (union_decl) = 1;
3232 layout_decl (union_decl, 0);
3233 pushdecl (union_decl);
3234
3235 DECL_CONTEXT (union_decl) = current_function_decl;
3236 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
3237 TREE_TYPE (union_decl), union_decl, tmp);
3238 gfc_add_expr_to_block (&body, tmp);
3239
3240 for (field = TYPE_FIELDS (TREE_TYPE (union_decl));
3241 field; field = DECL_CHAIN (field))
3242 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
3243 thunk_sym->result->name) == 0)
3244 break;
3245 gcc_assert (field != NULL_TREE);
3246 tmp = fold_build3_loc (input_location, COMPONENT_REF,
3247 TREE_TYPE (field), union_decl, field,
3248 NULL_TREE);
3249 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
3250 TREE_TYPE (DECL_RESULT (current_function_decl)),
3251 DECL_RESULT (current_function_decl), tmp);
3252 tmp = build1_v (RETURN_EXPR, tmp);
3253 }
3254 else if (TREE_TYPE (DECL_RESULT (current_function_decl))
3255 != void_type_node)
3256 {
3257 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
3258 TREE_TYPE (DECL_RESULT (current_function_decl)),
3259 DECL_RESULT (current_function_decl), tmp);
3260 tmp = build1_v (RETURN_EXPR, tmp);
3261 }
3262 gfc_add_expr_to_block (&body, tmp);
3263
3264 /* Finish off this function and send it for code generation. */
3265 DECL_SAVED_TREE (thunk_fndecl) = gfc_finish_block (&body);
3266 tmp = getdecls ();
3267 poplevel (1, 1);
3268 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl)) = thunk_fndecl;
3269 DECL_SAVED_TREE (thunk_fndecl)
3270 = fold_build3_loc (DECL_SOURCE_LOCATION (thunk_fndecl), BIND_EXPR,
3271 void_type_node, tmp, DECL_SAVED_TREE (thunk_fndecl),
3272 DECL_INITIAL (thunk_fndecl));
3273
3274 /* Output the GENERIC tree. */
3275 dump_function (TDI_original, thunk_fndecl);
3276
3277 /* Store the end of the function, so that we get good line number
3278 info for the epilogue. */
3279 cfun->function_end_locus = input_location;
3280
3281 /* We're leaving the context of this function, so zap cfun.
3282 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
3283 tree_rest_of_compilation. */
3284 set_cfun (NULL);
3285
3286 current_function_decl = NULL_TREE;
3287
3288 cgraph_node::finalize_function (thunk_fndecl, true);
3289
3290 /* We share the symbols in the formal argument list with other entry
3291 points and the master function. Clear them so that they are
3292 recreated for each function. */
3293 for (formal = gfc_sym_get_dummy_args (thunk_sym); formal;
3294 formal = formal->next)
3295 if (formal->sym != NULL) /* Ignore alternate returns. */
3296 {
3297 formal->sym->backend_decl = NULL_TREE;
3298 if (formal->sym->ts.type == BT_CHARACTER)
3299 formal->sym->ts.u.cl->backend_decl = NULL_TREE;
3300 }
3301
3302 if (thunk_sym->attr.function)
3303 {
3304 if (thunk_sym->ts.type == BT_CHARACTER)
3305 thunk_sym->ts.u.cl->backend_decl = NULL_TREE;
3306 if (thunk_sym->result->ts.type == BT_CHARACTER)
3307 thunk_sym->result->ts.u.cl->backend_decl = NULL_TREE;
3308 }
3309 }
3310
3311 input_location = old_loc;
3312 }
3313
3314
3315 /* Create a decl for a function, and create any thunks for alternate entry
3316 points. If global is true, generate the function in the global binding
3317 level, otherwise in the current binding level (which can be global). */
3318
3319 void
3320 gfc_create_function_decl (gfc_namespace * ns, bool global)
3321 {
3322 /* Create a declaration for the master function. */
3323 build_function_decl (ns->proc_name, global);
3324
3325 /* Compile the entry thunks. */
3326 if (ns->entries)
3327 build_entry_thunks (ns, global);
3328
3329 /* Now create the read argument list. */
3330 create_function_arglist (ns->proc_name);
3331
3332 if (ns->omp_declare_simd)
3333 gfc_trans_omp_declare_simd (ns);
3334
3335 /* Handle 'declare variant' directives. The applicable directives might
3336 be declared in a parent namespace, so this needs to be called even if
3337 there are no local directives. */
3338 if (flag_openmp)
3339 gfc_trans_omp_declare_variant (ns, NULL);
3340 }
3341
3342 /* Return the decl used to hold the function return value. If
3343 parent_flag is set, the context is the parent_scope. */
3344
3345 tree
3346 gfc_get_fake_result_decl (gfc_symbol * sym, int parent_flag)
3347 {
3348 tree decl;
3349 tree length;
3350 tree this_fake_result_decl;
3351 tree this_function_decl;
3352
3353 char name[GFC_MAX_SYMBOL_LEN + 10];
3354
3355 if (parent_flag)
3356 {
3357 this_fake_result_decl = parent_fake_result_decl;
3358 this_function_decl = DECL_CONTEXT (current_function_decl);
3359 }
3360 else
3361 {
3362 this_fake_result_decl = current_fake_result_decl;
3363 this_function_decl = current_function_decl;
3364 }
3365
3366 if (sym
3367 && sym->ns->proc_name->backend_decl == this_function_decl
3368 && sym->ns->proc_name->attr.entry_master
3369 && sym != sym->ns->proc_name)
3370 {
3371 tree t = NULL, var;
3372 if (this_fake_result_decl != NULL)
3373 for (t = TREE_CHAIN (this_fake_result_decl); t; t = TREE_CHAIN (t))
3374 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t)), sym->name) == 0)
3375 break;
3376 if (t)
3377 return TREE_VALUE (t);
3378 decl = gfc_get_fake_result_decl (sym->ns->proc_name, parent_flag);
3379
3380 if (parent_flag)
3381 this_fake_result_decl = parent_fake_result_decl;
3382 else
3383 this_fake_result_decl = current_fake_result_decl;
3384
3385 if (decl && sym->ns->proc_name->attr.mixed_entry_master)
3386 {
3387 tree field;
3388
3389 for (field = TYPE_FIELDS (TREE_TYPE (decl));
3390 field; field = DECL_CHAIN (field))
3391 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
3392 sym->name) == 0)
3393 break;
3394
3395 gcc_assert (field != NULL_TREE);
3396 decl = fold_build3_loc (input_location, COMPONENT_REF,
3397 TREE_TYPE (field), decl, field, NULL_TREE);
3398 }
3399
3400 var = create_tmp_var_raw (TREE_TYPE (decl), sym->name);
3401 if (parent_flag)
3402 gfc_add_decl_to_parent_function (var);
3403 else
3404 gfc_add_decl_to_function (var);
3405
3406 SET_DECL_VALUE_EXPR (var, decl);
3407 DECL_HAS_VALUE_EXPR_P (var) = 1;
3408 GFC_DECL_RESULT (var) = 1;
3409
3410 TREE_CHAIN (this_fake_result_decl)
3411 = tree_cons (get_identifier (sym->name), var,
3412 TREE_CHAIN (this_fake_result_decl));
3413 return var;
3414 }
3415
3416 if (this_fake_result_decl != NULL_TREE)
3417 return TREE_VALUE (this_fake_result_decl);
3418
3419 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
3420 sym is NULL. */
3421 if (!sym)
3422 return NULL_TREE;
3423
3424 if (sym->ts.type == BT_CHARACTER)
3425 {
3426 if (sym->ts.u.cl->backend_decl == NULL_TREE)
3427 length = gfc_create_string_length (sym);
3428 else
3429 length = sym->ts.u.cl->backend_decl;
3430 if (VAR_P (length) && DECL_CONTEXT (length) == NULL_TREE)
3431 gfc_add_decl_to_function (length);
3432 }
3433
3434 if (gfc_return_by_reference (sym))
3435 {
3436 decl = DECL_ARGUMENTS (this_function_decl);
3437
3438 if (sym->ns->proc_name->backend_decl == this_function_decl
3439 && sym->ns->proc_name->attr.entry_master)
3440 decl = DECL_CHAIN (decl);
3441
3442 TREE_USED (decl) = 1;
3443 if (sym->as)
3444 decl = gfc_build_dummy_array_decl (sym, decl);
3445 }
3446 else
3447 {
3448 sprintf (name, "__result_%.20s",
3449 IDENTIFIER_POINTER (DECL_NAME (this_function_decl)));
3450
3451 if (!sym->attr.mixed_entry_master && sym->attr.function)
3452 decl = build_decl (DECL_SOURCE_LOCATION (this_function_decl),
3453 VAR_DECL, get_identifier (name),
3454 gfc_sym_type (sym));
3455 else
3456 decl = build_decl (DECL_SOURCE_LOCATION (this_function_decl),
3457 VAR_DECL, get_identifier (name),
3458 TREE_TYPE (TREE_TYPE (this_function_decl)));
3459 DECL_ARTIFICIAL (decl) = 1;
3460 DECL_EXTERNAL (decl) = 0;
3461 TREE_PUBLIC (decl) = 0;
3462 TREE_USED (decl) = 1;
3463 GFC_DECL_RESULT (decl) = 1;
3464 TREE_ADDRESSABLE (decl) = 1;
3465
3466 layout_decl (decl, 0);
3467 gfc_finish_decl_attrs (decl, &sym->attr);
3468
3469 if (parent_flag)
3470 gfc_add_decl_to_parent_function (decl);
3471 else
3472 gfc_add_decl_to_function (decl);
3473 }
3474
3475 if (parent_flag)
3476 parent_fake_result_decl = build_tree_list (NULL, decl);
3477 else
3478 current_fake_result_decl = build_tree_list (NULL, decl);
3479
3480 if (sym->attr.assign)
3481 DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (sym->backend_decl);
3482
3483 return decl;
3484 }
3485
3486
3487 /* Builds a function decl. The remaining parameters are the types of the
3488 function arguments. Negative nargs indicates a varargs function. */
3489
3490 static tree
3491 build_library_function_decl_1 (tree name, const char *spec,
3492 tree rettype, int nargs, va_list p)
3493 {
3494 vec<tree, va_gc> *arglist;
3495 tree fntype;
3496 tree fndecl;
3497 int n;
3498
3499 /* Library functions must be declared with global scope. */
3500 gcc_assert (current_function_decl == NULL_TREE);
3501
3502 /* Create a list of the argument types. */
3503 vec_alloc (arglist, abs (nargs));
3504 for (n = abs (nargs); n > 0; n--)
3505 {
3506 tree argtype = va_arg (p, tree);
3507 arglist->quick_push (argtype);
3508 }
3509
3510 /* Build the function type and decl. */
3511 if (nargs >= 0)
3512 fntype = build_function_type_vec (rettype, arglist);
3513 else
3514 fntype = build_varargs_function_type_vec (rettype, arglist);
3515 if (spec)
3516 {
3517 tree attr_args = build_tree_list (NULL_TREE,
3518 build_string (strlen (spec), spec));
3519 tree attrs = tree_cons (get_identifier ("fn spec"),
3520 attr_args, TYPE_ATTRIBUTES (fntype));
3521 fntype = build_type_attribute_variant (fntype, attrs);
3522 }
3523 fndecl = build_decl (input_location,
3524 FUNCTION_DECL, name, fntype);
3525
3526 /* Mark this decl as external. */
3527 DECL_EXTERNAL (fndecl) = 1;
3528 TREE_PUBLIC (fndecl) = 1;
3529
3530 pushdecl (fndecl);
3531
3532 rest_of_decl_compilation (fndecl, 1, 0);
3533
3534 return fndecl;
3535 }
3536
3537 /* Builds a function decl. The remaining parameters are the types of the
3538 function arguments. Negative nargs indicates a varargs function. */
3539
3540 tree
3541 gfc_build_library_function_decl (tree name, tree rettype, int nargs, ...)
3542 {
3543 tree ret;
3544 va_list args;
3545 va_start (args, nargs);
3546 ret = build_library_function_decl_1 (name, NULL, rettype, nargs, args);
3547 va_end (args);
3548 return ret;
3549 }
3550
3551 /* Builds a function decl. The remaining parameters are the types of the
3552 function arguments. Negative nargs indicates a varargs function.
3553 The SPEC parameter specifies the function argument and return type
3554 specification according to the fnspec function type attribute. */
3555
3556 tree
3557 gfc_build_library_function_decl_with_spec (tree name, const char *spec,
3558 tree rettype, int nargs, ...)
3559 {
3560 tree ret;
3561 va_list args;
3562 va_start (args, nargs);
3563 if (flag_checking)
3564 {
3565 attr_fnspec fnspec (spec, strlen (spec));
3566 fnspec.verify ();
3567 }
3568 ret = build_library_function_decl_1 (name, spec, rettype, nargs, args);
3569 va_end (args);
3570 return ret;
3571 }
3572
3573 static void
3574 gfc_build_intrinsic_function_decls (void)
3575 {
3576 tree gfc_int4_type_node = gfc_get_int_type (4);
3577 tree gfc_pint4_type_node = build_pointer_type (gfc_int4_type_node);
3578 tree gfc_int8_type_node = gfc_get_int_type (8);
3579 tree gfc_pint8_type_node = build_pointer_type (gfc_int8_type_node);
3580 tree gfc_int16_type_node = gfc_get_int_type (16);
3581 tree gfc_logical4_type_node = gfc_get_logical_type (4);
3582 tree pchar1_type_node = gfc_get_pchar_type (1);
3583 tree pchar4_type_node = gfc_get_pchar_type (4);
3584
3585 /* String functions. */
3586 gfor_fndecl_compare_string = gfc_build_library_function_decl_with_spec (
3587 get_identifier (PREFIX("compare_string")), ". . R . R ",
3588 integer_type_node, 4, gfc_charlen_type_node, pchar1_type_node,
3589 gfc_charlen_type_node, pchar1_type_node);
3590 DECL_PURE_P (gfor_fndecl_compare_string) = 1;
3591 TREE_NOTHROW (gfor_fndecl_compare_string) = 1;
3592
3593 gfor_fndecl_concat_string = gfc_build_library_function_decl_with_spec (
3594 get_identifier (PREFIX("concat_string")), ". . W . R . R ",
3595 void_type_node, 6, gfc_charlen_type_node, pchar1_type_node,
3596 gfc_charlen_type_node, pchar1_type_node,
3597 gfc_charlen_type_node, pchar1_type_node);
3598 TREE_NOTHROW (gfor_fndecl_concat_string) = 1;
3599
3600 gfor_fndecl_string_len_trim = gfc_build_library_function_decl_with_spec (
3601 get_identifier (PREFIX("string_len_trim")), ". . R ",
3602 gfc_charlen_type_node, 2, gfc_charlen_type_node, pchar1_type_node);
3603 DECL_PURE_P (gfor_fndecl_string_len_trim) = 1;
3604 TREE_NOTHROW (gfor_fndecl_string_len_trim) = 1;
3605
3606 gfor_fndecl_string_index = gfc_build_library_function_decl_with_spec (
3607 get_identifier (PREFIX("string_index")), ". . R . R . ",
3608 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar1_type_node,
3609 gfc_charlen_type_node, pchar1_type_node, gfc_logical4_type_node);
3610 DECL_PURE_P (gfor_fndecl_string_index) = 1;
3611 TREE_NOTHROW (gfor_fndecl_string_index) = 1;
3612
3613 gfor_fndecl_string_scan = gfc_build_library_function_decl_with_spec (
3614 get_identifier (PREFIX("string_scan")), ". . R . R . ",
3615 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar1_type_node,
3616 gfc_charlen_type_node, pchar1_type_node, gfc_logical4_type_node);
3617 DECL_PURE_P (gfor_fndecl_string_scan) = 1;
3618 TREE_NOTHROW (gfor_fndecl_string_scan) = 1;
3619
3620 gfor_fndecl_string_verify = gfc_build_library_function_decl_with_spec (
3621 get_identifier (PREFIX("string_verify")), ". . R . R . ",
3622 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar1_type_node,
3623 gfc_charlen_type_node, pchar1_type_node, gfc_logical4_type_node);
3624 DECL_PURE_P (gfor_fndecl_string_verify) = 1;
3625 TREE_NOTHROW (gfor_fndecl_string_verify) = 1;
3626
3627 gfor_fndecl_string_trim = gfc_build_library_function_decl_with_spec (
3628 get_identifier (PREFIX("string_trim")), ". W w . R ",
3629 void_type_node, 4, build_pointer_type (gfc_charlen_type_node),
3630 build_pointer_type (pchar1_type_node), gfc_charlen_type_node,
3631 pchar1_type_node);
3632
3633 gfor_fndecl_string_minmax = gfc_build_library_function_decl_with_spec (
3634 get_identifier (PREFIX("string_minmax")), ". W w . R ",
3635 void_type_node, -4, build_pointer_type (gfc_charlen_type_node),
3636 build_pointer_type (pchar1_type_node), integer_type_node,
3637 integer_type_node);
3638
3639 gfor_fndecl_adjustl = gfc_build_library_function_decl_with_spec (
3640 get_identifier (PREFIX("adjustl")), ". W . R ",
3641 void_type_node, 3, pchar1_type_node, gfc_charlen_type_node,
3642 pchar1_type_node);
3643 TREE_NOTHROW (gfor_fndecl_adjustl) = 1;
3644
3645 gfor_fndecl_adjustr = gfc_build_library_function_decl_with_spec (
3646 get_identifier (PREFIX("adjustr")), ". W . R ",
3647 void_type_node, 3, pchar1_type_node, gfc_charlen_type_node,
3648 pchar1_type_node);
3649 TREE_NOTHROW (gfor_fndecl_adjustr) = 1;
3650
3651 gfor_fndecl_select_string = gfc_build_library_function_decl_with_spec (
3652 get_identifier (PREFIX("select_string")), ". R . R . ",
3653 integer_type_node, 4, pvoid_type_node, integer_type_node,
3654 pchar1_type_node, gfc_charlen_type_node);
3655 DECL_PURE_P (gfor_fndecl_select_string) = 1;
3656 TREE_NOTHROW (gfor_fndecl_select_string) = 1;
3657
3658 gfor_fndecl_compare_string_char4 = gfc_build_library_function_decl_with_spec (
3659 get_identifier (PREFIX("compare_string_char4")), ". . R . R ",
3660 integer_type_node, 4, gfc_charlen_type_node, pchar4_type_node,
3661 gfc_charlen_type_node, pchar4_type_node);
3662 DECL_PURE_P (gfor_fndecl_compare_string_char4) = 1;
3663 TREE_NOTHROW (gfor_fndecl_compare_string_char4) = 1;
3664
3665 gfor_fndecl_concat_string_char4 = gfc_build_library_function_decl_with_spec (
3666 get_identifier (PREFIX("concat_string_char4")), ". . W . R . R ",
3667 void_type_node, 6, gfc_charlen_type_node, pchar4_type_node,
3668 gfc_charlen_type_node, pchar4_type_node, gfc_charlen_type_node,
3669 pchar4_type_node);
3670 TREE_NOTHROW (gfor_fndecl_concat_string_char4) = 1;
3671
3672 gfor_fndecl_string_len_trim_char4 = gfc_build_library_function_decl_with_spec (
3673 get_identifier (PREFIX("string_len_trim_char4")), ". . R ",
3674 gfc_charlen_type_node, 2, gfc_charlen_type_node, pchar4_type_node);
3675 DECL_PURE_P (gfor_fndecl_string_len_trim_char4) = 1;
3676 TREE_NOTHROW (gfor_fndecl_string_len_trim_char4) = 1;
3677
3678 gfor_fndecl_string_index_char4 = gfc_build_library_function_decl_with_spec (
3679 get_identifier (PREFIX("string_index_char4")), ". . R . R . ",
3680 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar4_type_node,
3681 gfc_charlen_type_node, pchar4_type_node, gfc_logical4_type_node);
3682 DECL_PURE_P (gfor_fndecl_string_index_char4) = 1;
3683 TREE_NOTHROW (gfor_fndecl_string_index_char4) = 1;
3684
3685 gfor_fndecl_string_scan_char4 = gfc_build_library_function_decl_with_spec (
3686 get_identifier (PREFIX("string_scan_char4")), ". . R . R . ",
3687 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar4_type_node,
3688 gfc_charlen_type_node, pchar4_type_node, gfc_logical4_type_node);
3689 DECL_PURE_P (gfor_fndecl_string_scan_char4) = 1;
3690 TREE_NOTHROW (gfor_fndecl_string_scan_char4) = 1;
3691
3692 gfor_fndecl_string_verify_char4 = gfc_build_library_function_decl_with_spec (
3693 get_identifier (PREFIX("string_verify_char4")), ". . R . R . ",
3694 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar4_type_node,
3695 gfc_charlen_type_node, pchar4_type_node, gfc_logical4_type_node);
3696 DECL_PURE_P (gfor_fndecl_string_verify_char4) = 1;
3697 TREE_NOTHROW (gfor_fndecl_string_verify_char4) = 1;
3698
3699 gfor_fndecl_string_trim_char4 = gfc_build_library_function_decl_with_spec (
3700 get_identifier (PREFIX("string_trim_char4")), ". W w . R ",
3701 void_type_node, 4, build_pointer_type (gfc_charlen_type_node),
3702 build_pointer_type (pchar4_type_node), gfc_charlen_type_node,
3703 pchar4_type_node);
3704
3705 gfor_fndecl_string_minmax_char4 = gfc_build_library_function_decl_with_spec (
3706 get_identifier (PREFIX("string_minmax_char4")), ". W w . R ",
3707 void_type_node, -4, build_pointer_type (gfc_charlen_type_node),
3708 build_pointer_type (pchar4_type_node), integer_type_node,
3709 integer_type_node);
3710
3711 gfor_fndecl_adjustl_char4 = gfc_build_library_function_decl_with_spec (
3712 get_identifier (PREFIX("adjustl_char4")), ". W . R ",
3713 void_type_node, 3, pchar4_type_node, gfc_charlen_type_node,
3714 pchar4_type_node);
3715 TREE_NOTHROW (gfor_fndecl_adjustl_char4) = 1;
3716
3717 gfor_fndecl_adjustr_char4 = gfc_build_library_function_decl_with_spec (
3718 get_identifier (PREFIX("adjustr_char4")), ". W . R ",
3719 void_type_node, 3, pchar4_type_node, gfc_charlen_type_node,
3720 pchar4_type_node);
3721 TREE_NOTHROW (gfor_fndecl_adjustr_char4) = 1;
3722
3723 gfor_fndecl_select_string_char4 = gfc_build_library_function_decl_with_spec (
3724 get_identifier (PREFIX("select_string_char4")), ". R . R . ",
3725 integer_type_node, 4, pvoid_type_node, integer_type_node,
3726 pvoid_type_node, gfc_charlen_type_node);
3727 DECL_PURE_P (gfor_fndecl_select_string_char4) = 1;
3728 TREE_NOTHROW (gfor_fndecl_select_string_char4) = 1;
3729
3730
3731 /* Conversion between character kinds. */
3732
3733 gfor_fndecl_convert_char1_to_char4 = gfc_build_library_function_decl_with_spec (
3734 get_identifier (PREFIX("convert_char1_to_char4")), ". w . R ",
3735 void_type_node, 3, build_pointer_type (pchar4_type_node),
3736 gfc_charlen_type_node, pchar1_type_node);
3737
3738 gfor_fndecl_convert_char4_to_char1 = gfc_build_library_function_decl_with_spec (
3739 get_identifier (PREFIX("convert_char4_to_char1")), ". w . R ",
3740 void_type_node, 3, build_pointer_type (pchar1_type_node),
3741 gfc_charlen_type_node, pchar4_type_node);
3742
3743 /* Misc. functions. */
3744
3745 gfor_fndecl_ttynam = gfc_build_library_function_decl_with_spec (
3746 get_identifier (PREFIX("ttynam")), ". W . . ",
3747 void_type_node, 3, pchar_type_node, gfc_charlen_type_node,
3748 integer_type_node);
3749
3750 gfor_fndecl_fdate = gfc_build_library_function_decl_with_spec (
3751 get_identifier (PREFIX("fdate")), ". W . ",
3752 void_type_node, 2, pchar_type_node, gfc_charlen_type_node);
3753
3754 gfor_fndecl_ctime = gfc_build_library_function_decl_with_spec (
3755 get_identifier (PREFIX("ctime")), ". W . . ",
3756 void_type_node, 3, pchar_type_node, gfc_charlen_type_node,
3757 gfc_int8_type_node);
3758
3759 gfor_fndecl_random_init = gfc_build_library_function_decl (
3760 get_identifier (PREFIX("random_init")),
3761 void_type_node, 3, gfc_logical4_type_node, gfc_logical4_type_node,
3762 gfc_int4_type_node);
3763
3764 // gfor_fndecl_caf_rand_init is defined in the lib-coarray section below.
3765
3766 gfor_fndecl_sc_kind = gfc_build_library_function_decl_with_spec (
3767 get_identifier (PREFIX("selected_char_kind")), ". . R ",
3768 gfc_int4_type_node, 2, gfc_charlen_type_node, pchar_type_node);
3769 DECL_PURE_P (gfor_fndecl_sc_kind) = 1;
3770 TREE_NOTHROW (gfor_fndecl_sc_kind) = 1;
3771
3772 gfor_fndecl_si_kind = gfc_build_library_function_decl_with_spec (
3773 get_identifier (PREFIX("selected_int_kind")), ". R ",
3774 gfc_int4_type_node, 1, pvoid_type_node);
3775 DECL_PURE_P (gfor_fndecl_si_kind) = 1;
3776 TREE_NOTHROW (gfor_fndecl_si_kind) = 1;
3777
3778 gfor_fndecl_sl_kind = gfc_build_library_function_decl_with_spec (
3779 get_identifier (PREFIX("selected_logical_kind")), ". R ",
3780 gfc_int4_type_node, 1, pvoid_type_node);
3781 DECL_PURE_P (gfor_fndecl_sl_kind) = 1;
3782 TREE_NOTHROW (gfor_fndecl_sl_kind) = 1;
3783
3784 gfor_fndecl_sr_kind = gfc_build_library_function_decl_with_spec (
3785 get_identifier (PREFIX("selected_real_kind2008")), ". R R ",
3786 gfc_int4_type_node, 3, pvoid_type_node, pvoid_type_node,
3787 pvoid_type_node);
3788 DECL_PURE_P (gfor_fndecl_sr_kind) = 1;
3789 TREE_NOTHROW (gfor_fndecl_sr_kind) = 1;
3790
3791 gfor_fndecl_system_clock4 = gfc_build_library_function_decl (
3792 get_identifier (PREFIX("system_clock_4")),
3793 void_type_node, 3, gfc_pint4_type_node, gfc_pint4_type_node,
3794 gfc_pint4_type_node);
3795
3796 gfor_fndecl_system_clock8 = gfc_build_library_function_decl (
3797 get_identifier (PREFIX("system_clock_8")),
3798 void_type_node, 3, gfc_pint8_type_node, gfc_pint8_type_node,
3799 gfc_pint8_type_node);
3800
3801 /* Power functions. */
3802 {
3803 tree ctype, rtype, itype, jtype;
3804 int rkind, ikind, jkind;
3805 #define NIKINDS 3
3806 #define NRKINDS 4
3807 #define NUKINDS 5
3808 static const int ikinds[NIKINDS] = {4, 8, 16};
3809 static const int rkinds[NRKINDS] = {4, 8, 10, 16};
3810 static const int ukinds[NUKINDS] = {1, 2, 4, 8, 16};
3811 char name[PREFIX_LEN + 12]; /* _gfortran_pow_?n_?n */
3812
3813 for (ikind=0; ikind < NIKINDS; ikind++)
3814 {
3815 itype = gfc_get_int_type (ikinds[ikind]);
3816
3817 for (jkind=0; jkind < NIKINDS; jkind++)
3818 {
3819 jtype = gfc_get_int_type (ikinds[jkind]);
3820 if (itype && jtype)
3821 {
3822 sprintf (name, PREFIX("pow_i%d_i%d"), ikinds[ikind],
3823 ikinds[jkind]);
3824 gfor_fndecl_math_powi[jkind][ikind].integer =
3825 gfc_build_library_function_decl (get_identifier (name),
3826 jtype, 2, jtype, itype);
3827 TREE_READONLY (gfor_fndecl_math_powi[jkind][ikind].integer) = 1;
3828 TREE_NOTHROW (gfor_fndecl_math_powi[jkind][ikind].integer) = 1;
3829 }
3830 }
3831
3832 for (rkind = 0; rkind < NRKINDS; rkind ++)
3833 {
3834 rtype = gfc_get_real_type (rkinds[rkind]);
3835 if (rtype && itype)
3836 {
3837 sprintf (name, PREFIX("pow_r%d_i%d"),
3838 gfc_type_abi_kind (BT_REAL, rkinds[rkind]),
3839 ikinds[ikind]);
3840 gfor_fndecl_math_powi[rkind][ikind].real =
3841 gfc_build_library_function_decl (get_identifier (name),
3842 rtype, 2, rtype, itype);
3843 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].real) = 1;
3844 TREE_NOTHROW (gfor_fndecl_math_powi[rkind][ikind].real) = 1;
3845 }
3846
3847 ctype = gfc_get_complex_type (rkinds[rkind]);
3848 if (ctype && itype)
3849 {
3850 sprintf (name, PREFIX("pow_c%d_i%d"),
3851 gfc_type_abi_kind (BT_REAL, rkinds[rkind]),
3852 ikinds[ikind]);
3853 gfor_fndecl_math_powi[rkind][ikind].cmplx =
3854 gfc_build_library_function_decl (get_identifier (name),
3855 ctype, 2,ctype, itype);
3856 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1;
3857 TREE_NOTHROW (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1;
3858 }
3859 }
3860 /* For unsigned types, we have every power for every type. */
3861 for (int base = 0; base < NUKINDS; base++)
3862 {
3863 tree base_type = gfc_get_unsigned_type (ukinds[base]);
3864 for (int expon = 0; expon < NUKINDS; expon++)
3865 {
3866 tree expon_type = gfc_get_unsigned_type (ukinds[base]);
3867 if (base_type && expon_type)
3868 {
3869 sprintf (name, PREFIX("pow_m%d_m%d"), ukinds[base],
3870 ukinds[expon]);
3871 gfor_fndecl_unsigned_pow_list [base][expon] =
3872 gfc_build_library_function_decl (get_identifier (name),
3873 base_type, 2, base_type, expon_type);
3874 TREE_READONLY (gfor_fndecl_unsigned_pow_list[base][expon]) = 1;
3875 TREE_NOTHROW (gfor_fndecl_unsigned_pow_list[base][expon]) = 1;
3876 }
3877 }
3878 }
3879 }
3880 #undef NIKINDS
3881 #undef NRKINDS
3882 #undef NUKINDS
3883 }
3884
3885 gfor_fndecl_math_ishftc4 = gfc_build_library_function_decl (
3886 get_identifier (PREFIX("ishftc4")),
3887 gfc_int4_type_node, 3, gfc_int4_type_node, gfc_int4_type_node,
3888 gfc_int4_type_node);
3889 TREE_READONLY (gfor_fndecl_math_ishftc4) = 1;
3890 TREE_NOTHROW (gfor_fndecl_math_ishftc4) = 1;
3891
3892 gfor_fndecl_math_ishftc8 = gfc_build_library_function_decl (
3893 get_identifier (PREFIX("ishftc8")),
3894 gfc_int8_type_node, 3, gfc_int8_type_node, gfc_int4_type_node,
3895 gfc_int4_type_node);
3896 TREE_READONLY (gfor_fndecl_math_ishftc8) = 1;
3897 TREE_NOTHROW (gfor_fndecl_math_ishftc8) = 1;
3898
3899 if (gfc_int16_type_node)
3900 {
3901 gfor_fndecl_math_ishftc16 = gfc_build_library_function_decl (
3902 get_identifier (PREFIX("ishftc16")),
3903 gfc_int16_type_node, 3, gfc_int16_type_node, gfc_int4_type_node,
3904 gfc_int4_type_node);
3905 TREE_READONLY (gfor_fndecl_math_ishftc16) = 1;
3906 TREE_NOTHROW (gfor_fndecl_math_ishftc16) = 1;
3907 }
3908
3909 /* BLAS functions. */
3910 {
3911 tree pint = build_pointer_type (integer_type_node);
3912 tree ps = build_pointer_type (gfc_get_real_type (gfc_default_real_kind));
3913 tree pd = build_pointer_type (gfc_get_real_type (gfc_default_double_kind));
3914 tree pc = build_pointer_type (gfc_get_complex_type (gfc_default_real_kind));
3915 tree pz = build_pointer_type
3916 (gfc_get_complex_type (gfc_default_double_kind));
3917
3918 gfor_fndecl_sgemm = gfc_build_library_function_decl
3919 (get_identifier
3920 (flag_underscoring ? "sgemm_" : "sgemm"),
3921 void_type_node, 15, pchar_type_node,
3922 pchar_type_node, pint, pint, pint, ps, ps, pint,
3923 ps, pint, ps, ps, pint, integer_type_node,
3924 integer_type_node);
3925 gfor_fndecl_dgemm = gfc_build_library_function_decl
3926 (get_identifier
3927 (flag_underscoring ? "dgemm_" : "dgemm"),
3928 void_type_node, 15, pchar_type_node,
3929 pchar_type_node, pint, pint, pint, pd, pd, pint,
3930 pd, pint, pd, pd, pint, integer_type_node,
3931 integer_type_node);
3932 gfor_fndecl_cgemm = gfc_build_library_function_decl
3933 (get_identifier
3934 (flag_underscoring ? "cgemm_" : "cgemm"),
3935 void_type_node, 15, pchar_type_node,
3936 pchar_type_node, pint, pint, pint, pc, pc, pint,
3937 pc, pint, pc, pc, pint, integer_type_node,
3938 integer_type_node);
3939 gfor_fndecl_zgemm = gfc_build_library_function_decl
3940 (get_identifier
3941 (flag_underscoring ? "zgemm_" : "zgemm"),
3942 void_type_node, 15, pchar_type_node,
3943 pchar_type_node, pint, pint, pint, pz, pz, pint,
3944 pz, pint, pz, pz, pint, integer_type_node,
3945 integer_type_node);
3946 }
3947
3948 /* Other functions. */
3949 gfor_fndecl_iargc = gfc_build_library_function_decl (
3950 get_identifier (PREFIX ("iargc")), gfc_int4_type_node, 0);
3951 TREE_NOTHROW (gfor_fndecl_iargc) = 1;
3952
3953 gfor_fndecl_kill_sub = gfc_build_library_function_decl (
3954 get_identifier (PREFIX ("kill_sub")), void_type_node,
3955 3, gfc_int4_type_node, gfc_int4_type_node, gfc_pint4_type_node);
3956
3957 gfor_fndecl_kill = gfc_build_library_function_decl (
3958 get_identifier (PREFIX ("kill")), gfc_int4_type_node,
3959 2, gfc_int4_type_node, gfc_int4_type_node);
3960
3961 gfor_fndecl_is_contiguous0 = gfc_build_library_function_decl_with_spec (
3962 get_identifier (PREFIX("is_contiguous0")), ". R ",
3963 gfc_int4_type_node, 1, pvoid_type_node);
3964 DECL_PURE_P (gfor_fndecl_is_contiguous0) = 1;
3965 TREE_NOTHROW (gfor_fndecl_is_contiguous0) = 1;
3966 }
3967
3968
3969 /* Make prototypes for runtime library functions. */
3970
3971 void
3972 gfc_build_builtin_function_decls (void)
3973 {
3974 tree gfc_int8_type_node = gfc_get_int_type (8);
3975
3976 gfor_fndecl_stop_numeric = gfc_build_library_function_decl (
3977 get_identifier (PREFIX("stop_numeric")),
3978 void_type_node, 2, integer_type_node, boolean_type_node);
3979 /* STOP doesn't return. */
3980 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric) = 1;
3981
3982 gfor_fndecl_stop_string = gfc_build_library_function_decl_with_spec (
3983 get_identifier (PREFIX("stop_string")), ". R . . ",
3984 void_type_node, 3, pchar_type_node, size_type_node,
3985 boolean_type_node);
3986 /* STOP doesn't return. */
3987 TREE_THIS_VOLATILE (gfor_fndecl_stop_string) = 1;
3988
3989 gfor_fndecl_error_stop_numeric = gfc_build_library_function_decl (
3990 get_identifier (PREFIX("error_stop_numeric")),
3991 void_type_node, 2, integer_type_node, boolean_type_node);
3992 /* ERROR STOP doesn't return. */
3993 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_numeric) = 1;
3994
3995 gfor_fndecl_error_stop_string = gfc_build_library_function_decl_with_spec (
3996 get_identifier (PREFIX("error_stop_string")), ". R . . ",
3997 void_type_node, 3, pchar_type_node, size_type_node,
3998 boolean_type_node);
3999 /* ERROR STOP doesn't return. */
4000 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_string) = 1;
4001
4002 gfor_fndecl_pause_numeric = gfc_build_library_function_decl (
4003 get_identifier (PREFIX("pause_numeric")),
4004 void_type_node, 1, gfc_int8_type_node);
4005
4006 gfor_fndecl_pause_string = gfc_build_library_function_decl_with_spec (
4007 get_identifier (PREFIX("pause_string")), ". R . ",
4008 void_type_node, 2, pchar_type_node, size_type_node);
4009
4010 gfor_fndecl_runtime_error = gfc_build_library_function_decl_with_spec (
4011 get_identifier (PREFIX("runtime_error")), ". R ",
4012 void_type_node, -1, pchar_type_node);
4013 /* The runtime_error function does not return. */
4014 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error) = 1;
4015
4016 gfor_fndecl_runtime_error_at = gfc_build_library_function_decl_with_spec (
4017 get_identifier (PREFIX("runtime_error_at")), ". R R ",
4018 void_type_node, -2, pchar_type_node, pchar_type_node);
4019 /* The runtime_error_at function does not return. */
4020 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at) = 1;
4021
4022 gfor_fndecl_runtime_warning_at = gfc_build_library_function_decl_with_spec (
4023 get_identifier (PREFIX("runtime_warning_at")), ". R R ",
4024 void_type_node, -2, pchar_type_node, pchar_type_node);
4025
4026 gfor_fndecl_generate_error = gfc_build_library_function_decl_with_spec (
4027 get_identifier (PREFIX("generate_error")), ". W . R ",
4028 void_type_node, 3, pvoid_type_node, integer_type_node,
4029 pchar_type_node);
4030
4031 gfor_fndecl_os_error_at = gfc_build_library_function_decl_with_spec (
4032 get_identifier (PREFIX("os_error_at")), ". R R ",
4033 void_type_node, -2, pchar_type_node, pchar_type_node);
4034 /* The os_error_at function does not return. */
4035 TREE_THIS_VOLATILE (gfor_fndecl_os_error_at) = 1;
4036
4037 gfor_fndecl_set_args = gfc_build_library_function_decl (
4038 get_identifier (PREFIX("set_args")),
4039 void_type_node, 2, integer_type_node,
4040 build_pointer_type (pchar_type_node));
4041
4042 gfor_fndecl_set_fpe = gfc_build_library_function_decl (
4043 get_identifier (PREFIX("set_fpe")),
4044 void_type_node, 1, integer_type_node);
4045
4046 gfor_fndecl_ieee_procedure_entry = gfc_build_library_function_decl (
4047 get_identifier (PREFIX("ieee_procedure_entry")),
4048 void_type_node, 1, pvoid_type_node);
4049
4050 gfor_fndecl_ieee_procedure_exit = gfc_build_library_function_decl (
4051 get_identifier (PREFIX("ieee_procedure_exit")),
4052 void_type_node, 1, pvoid_type_node);
4053
4054 /* Keep the array dimension in sync with the call, later in this file. */
4055 gfor_fndecl_set_options = gfc_build_library_function_decl_with_spec (
4056 get_identifier (PREFIX("set_options")), ". . R ",
4057 void_type_node, 2, integer_type_node,
4058 build_pointer_type (integer_type_node));
4059
4060 gfor_fndecl_set_convert = gfc_build_library_function_decl (
4061 get_identifier (PREFIX("set_convert")),
4062 void_type_node, 1, integer_type_node);
4063
4064 gfor_fndecl_set_record_marker = gfc_build_library_function_decl (
4065 get_identifier (PREFIX("set_record_marker")),
4066 void_type_node, 1, integer_type_node);
4067
4068 gfor_fndecl_set_max_subrecord_length = gfc_build_library_function_decl (
4069 get_identifier (PREFIX("set_max_subrecord_length")),
4070 void_type_node, 1, integer_type_node);
4071
4072 gfor_fndecl_in_pack = gfc_build_library_function_decl_with_spec (
4073 get_identifier (PREFIX("internal_pack")), ". r ",
4074 pvoid_type_node, 1, pvoid_type_node);
4075
4076 gfor_fndecl_in_unpack = gfc_build_library_function_decl_with_spec (
4077 get_identifier (PREFIX("internal_unpack")), ". w R ",
4078 void_type_node, 2, pvoid_type_node, pvoid_type_node);
4079
4080 gfor_fndecl_in_pack_class = gfc_build_library_function_decl_with_spec (
4081 get_identifier (PREFIX ("internal_pack_class")), ". w R r r ",
4082 void_type_node, 4, pvoid_type_node, pvoid_type_node, size_type_node,
4083 integer_type_node);
4084
4085 gfor_fndecl_in_unpack_class = gfc_build_library_function_decl_with_spec (
4086 get_identifier (PREFIX ("internal_unpack_class")), ". w R r r ",
4087 void_type_node, 4, pvoid_type_node, pvoid_type_node, size_type_node,
4088 integer_type_node);
4089
4090 gfor_fndecl_associated = gfc_build_library_function_decl_with_spec (
4091 get_identifier (PREFIX ("associated")), ". R R ", integer_type_node, 2,
4092 ppvoid_type_node, ppvoid_type_node);
4093 DECL_PURE_P (gfor_fndecl_associated) = 1;
4094 TREE_NOTHROW (gfor_fndecl_associated) = 1;
4095
4096 /* Coarray library calls. */
4097 if (flag_coarray == GFC_FCOARRAY_LIB)
4098 {
4099 tree pint_type, pppchar_type, psize_type;
4100
4101 pint_type = build_pointer_type (integer_type_node);
4102 pppchar_type
4103 = build_pointer_type (build_pointer_type (pchar_type_node));
4104 psize_type = build_pointer_type (size_type_node);
4105
4106 gfor_fndecl_caf_init = gfc_build_library_function_decl_with_spec (
4107 get_identifier (PREFIX("caf_init")), ". W W ",
4108 void_type_node, 2, pint_type, pppchar_type);
4109
4110 gfor_fndecl_caf_finalize = gfc_build_library_function_decl (
4111 get_identifier (PREFIX("caf_finalize")), void_type_node, 0);
4112
4113 gfor_fndecl_caf_this_image = gfc_build_library_function_decl (
4114 get_identifier (PREFIX("caf_this_image")), integer_type_node,
4115 1, integer_type_node);
4116
4117 gfor_fndecl_caf_num_images = gfc_build_library_function_decl (
4118 get_identifier (PREFIX("caf_num_images")), integer_type_node,
4119 2, integer_type_node, integer_type_node);
4120
4121 gfor_fndecl_caf_register = gfc_build_library_function_decl_with_spec (
4122 get_identifier (PREFIX("caf_register")), ". . . W w w w . ",
4123 void_type_node, 7,
4124 size_type_node, integer_type_node, ppvoid_type_node, pvoid_type_node,
4125 pint_type, pchar_type_node, size_type_node);
4126
4127 gfor_fndecl_caf_deregister = gfc_build_library_function_decl_with_spec (
4128 get_identifier (PREFIX("caf_deregister")), ". W . w w . ",
4129 void_type_node, 5,
4130 ppvoid_type_node, integer_type_node, pint_type, pchar_type_node,
4131 size_type_node);
4132
4133 gfor_fndecl_caf_register_accessor
4134 = gfc_build_library_function_decl_with_spec (
4135 get_identifier (PREFIX ("caf_register_accessor")), ". r r ",
4136 void_type_node, 2, integer_type_node, pvoid_type_node);
4137
4138 gfor_fndecl_caf_register_accessors_finish
4139 = gfc_build_library_function_decl_with_spec (
4140 get_identifier (PREFIX ("caf_register_accessors_finish")), ". ",
4141 void_type_node, 0);
4142
4143 gfor_fndecl_caf_get_remote_function_index
4144 = gfc_build_library_function_decl_with_spec (
4145 get_identifier (PREFIX ("caf_get_remote_function_index")), ". r ",
4146 integer_type_node, 1, integer_type_node);
4147
4148 gfor_fndecl_caf_get_from_remote
4149 = gfc_build_library_function_decl_with_spec (
4150 get_identifier (PREFIX ("caf_get_from_remote")),
4151 ". r r r r r w w w r r w r w r r ", void_type_node, 15,
4152 pvoid_type_node, pvoid_type_node, psize_type, integer_type_node,
4153 size_type_node, ppvoid_type_node, psize_type, pvoid_type_node,
4154 boolean_type_node, integer_type_node, pvoid_type_node, size_type_node,
4155 pint_type, pvoid_type_node, pint_type);
4156
4157 gfor_fndecl_caf_send_to_remote
4158 = gfc_build_library_function_decl_with_spec (
4159 get_identifier (PREFIX ("caf_send_to_remote")),
4160 ". r r r r r r r r r w r w r r ", void_type_node, 14, pvoid_type_node,
4161 pvoid_type_node, psize_type, integer_type_node, size_type_node,
4162 ppvoid_type_node, psize_type, pvoid_type_node, integer_type_node,
4163 pvoid_type_node, size_type_node, pint_type, pvoid_type_node,
4164 pint_type);
4165
4166 gfor_fndecl_caf_transfer_between_remotes
4167 = gfc_build_library_function_decl_with_spec (
4168 get_identifier (PREFIX ("caf_transfer_between_remotes")),
4169 ". r r r r r r r r r r r r r r r r w w r r ", void_type_node, 20,
4170 pvoid_type_node, pvoid_type_node, psize_type, integer_type_node,
4171 integer_type_node, pvoid_type_node, size_type_node, pvoid_type_node,
4172 pvoid_type_node, psize_type, integer_type_node, integer_type_node,
4173 pvoid_type_node, size_type_node, size_type_node, boolean_type_node,
4174 pint_type, pint_type, pvoid_type_node, pint_type);
4175
4176 gfor_fndecl_caf_sync_all = gfc_build_library_function_decl_with_spec (
4177 get_identifier (PREFIX ("caf_sync_all")), ". w w . ", void_type_node, 3,
4178 pint_type, pchar_type_node, size_type_node);
4179
4180 gfor_fndecl_caf_sync_memory = gfc_build_library_function_decl_with_spec (
4181 get_identifier (PREFIX("caf_sync_memory")), ". w w . ", void_type_node,
4182 3, pint_type, pchar_type_node, size_type_node);
4183
4184 gfor_fndecl_caf_sync_images = gfc_build_library_function_decl_with_spec (
4185 get_identifier (PREFIX("caf_sync_images")), ". . r w w . ", void_type_node,
4186 5, integer_type_node, pint_type, pint_type,
4187 pchar_type_node, size_type_node);
4188
4189 gfor_fndecl_caf_error_stop = gfc_build_library_function_decl (
4190 get_identifier (PREFIX("caf_error_stop")),
4191 void_type_node, 1, integer_type_node);
4192 /* CAF's ERROR STOP doesn't return. */
4193 TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop) = 1;
4194
4195 gfor_fndecl_caf_error_stop_str = gfc_build_library_function_decl_with_spec (
4196 get_identifier (PREFIX("caf_error_stop_str")), ". r . ",
4197 void_type_node, 2, pchar_type_node, size_type_node);
4198 /* CAF's ERROR STOP doesn't return. */
4199 TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop_str) = 1;
4200
4201 gfor_fndecl_caf_stop_numeric = gfc_build_library_function_decl (
4202 get_identifier (PREFIX("caf_stop_numeric")),
4203 void_type_node, 1, integer_type_node);
4204 /* CAF's STOP doesn't return. */
4205 TREE_THIS_VOLATILE (gfor_fndecl_caf_stop_numeric) = 1;
4206
4207 gfor_fndecl_caf_stop_str = gfc_build_library_function_decl_with_spec (
4208 get_identifier (PREFIX("caf_stop_str")), ". r . ",
4209 void_type_node, 2, pchar_type_node, size_type_node);
4210 /* CAF's STOP doesn't return. */
4211 TREE_THIS_VOLATILE (gfor_fndecl_caf_stop_str) = 1;
4212
4213 gfor_fndecl_caf_atomic_def = gfc_build_library_function_decl_with_spec (
4214 get_identifier (PREFIX("caf_atomic_define")), ". r . . w w . . ",
4215 void_type_node, 7, pvoid_type_node, size_type_node, integer_type_node,
4216 pvoid_type_node, pint_type, integer_type_node, integer_type_node);
4217
4218 gfor_fndecl_caf_atomic_ref = gfc_build_library_function_decl_with_spec (
4219 get_identifier (PREFIX("caf_atomic_ref")), ". r . . w w . . ",
4220 void_type_node, 7, pvoid_type_node, size_type_node, integer_type_node,
4221 pvoid_type_node, pint_type, integer_type_node, integer_type_node);
4222
4223 gfor_fndecl_caf_atomic_cas = gfc_build_library_function_decl_with_spec (
4224 get_identifier (PREFIX("caf_atomic_cas")), ". r . . w r r w . . ",
4225 void_type_node, 9, pvoid_type_node, size_type_node, integer_type_node,
4226 pvoid_type_node, pvoid_type_node, pvoid_type_node, pint_type,
4227 integer_type_node, integer_type_node);
4228
4229 gfor_fndecl_caf_atomic_op = gfc_build_library_function_decl_with_spec (
4230 get_identifier (PREFIX("caf_atomic_op")), ". . r . . r w w . . ",
4231 void_type_node, 9, integer_type_node, pvoid_type_node, size_type_node,
4232 integer_type_node, pvoid_type_node, pvoid_type_node, pint_type,
4233 integer_type_node, integer_type_node);
4234
4235 gfor_fndecl_caf_lock = gfc_build_library_function_decl_with_spec (
4236 get_identifier (PREFIX("caf_lock")), ". r . . w w w . ",
4237 void_type_node, 7, pvoid_type_node, size_type_node, integer_type_node,
4238 pint_type, pint_type, pchar_type_node, size_type_node);
4239
4240 gfor_fndecl_caf_unlock = gfc_build_library_function_decl_with_spec (
4241 get_identifier (PREFIX("caf_unlock")), ". r . . w w . ",
4242 void_type_node, 6, pvoid_type_node, size_type_node, integer_type_node,
4243 pint_type, pchar_type_node, size_type_node);
4244
4245 gfor_fndecl_caf_event_post = gfc_build_library_function_decl_with_spec (
4246 get_identifier (PREFIX("caf_event_post")), ". r . . w w . ",
4247 void_type_node, 6, pvoid_type_node, size_type_node, integer_type_node,
4248 pint_type, pchar_type_node, size_type_node);
4249
4250 gfor_fndecl_caf_event_wait = gfc_build_library_function_decl_with_spec (
4251 get_identifier (PREFIX("caf_event_wait")), ". r . . w w . ",
4252 void_type_node, 6, pvoid_type_node, size_type_node, integer_type_node,
4253 pint_type, pchar_type_node, size_type_node);
4254
4255 gfor_fndecl_caf_event_query = gfc_build_library_function_decl_with_spec (
4256 get_identifier (PREFIX("caf_event_query")), ". r . . w w ",
4257 void_type_node, 5, pvoid_type_node, size_type_node, integer_type_node,
4258 pint_type, pint_type);
4259
4260 gfor_fndecl_caf_fail_image = gfc_build_library_function_decl (
4261 get_identifier (PREFIX("caf_fail_image")), void_type_node, 0);
4262 /* CAF's FAIL doesn't return. */
4263 TREE_THIS_VOLATILE (gfor_fndecl_caf_fail_image) = 1;
4264
4265 gfor_fndecl_caf_failed_images
4266 = gfc_build_library_function_decl_with_spec (
4267 get_identifier (PREFIX("caf_failed_images")), ". w . r ",
4268 void_type_node, 3, pvoid_type_node, ppvoid_type_node,
4269 integer_type_node);
4270
4271 gfor_fndecl_caf_form_team
4272 = gfc_build_library_function_decl_with_spec (
4273 get_identifier (PREFIX("caf_form_team")), ". . W . ",
4274 void_type_node, 3, integer_type_node, ppvoid_type_node,
4275 integer_type_node);
4276
4277 gfor_fndecl_caf_change_team
4278 = gfc_build_library_function_decl_with_spec (
4279 get_identifier (PREFIX("caf_change_team")), ". w . ",
4280 void_type_node, 2, ppvoid_type_node,
4281 integer_type_node);
4282
4283 gfor_fndecl_caf_end_team
4284 = gfc_build_library_function_decl (
4285 get_identifier (PREFIX("caf_end_team")), void_type_node, 0);
4286
4287 gfor_fndecl_caf_get_team
4288 = gfc_build_library_function_decl (
4289 get_identifier (PREFIX("caf_get_team")),
4290 void_type_node, 1, integer_type_node);
4291
4292 gfor_fndecl_caf_sync_team
4293 = gfc_build_library_function_decl_with_spec (
4294 get_identifier (PREFIX("caf_sync_team")), ". r . ",
4295 void_type_node, 2, ppvoid_type_node,
4296 integer_type_node);
4297
4298 gfor_fndecl_caf_team_number
4299 = gfc_build_library_function_decl_with_spec (
4300 get_identifier (PREFIX("caf_team_number")), ". r ",
4301 integer_type_node, 1, integer_type_node);
4302
4303 gfor_fndecl_caf_image_status
4304 = gfc_build_library_function_decl_with_spec (
4305 get_identifier (PREFIX("caf_image_status")), ". . r ",
4306 integer_type_node, 2, integer_type_node, ppvoid_type_node);
4307
4308 gfor_fndecl_caf_stopped_images
4309 = gfc_build_library_function_decl_with_spec (
4310 get_identifier (PREFIX("caf_stopped_images")), ". w r r ",
4311 void_type_node, 3, pvoid_type_node, ppvoid_type_node,
4312 integer_type_node);
4313
4314 gfor_fndecl_co_broadcast = gfc_build_library_function_decl_with_spec (
4315 get_identifier (PREFIX("caf_co_broadcast")), ". w . w w . ",
4316 void_type_node, 5, pvoid_type_node, integer_type_node,
4317 pint_type, pchar_type_node, size_type_node);
4318
4319 gfor_fndecl_co_max = gfc_build_library_function_decl_with_spec (
4320 get_identifier (PREFIX("caf_co_max")), ". w . w w . . ",
4321 void_type_node, 6, pvoid_type_node, integer_type_node,
4322 pint_type, pchar_type_node, integer_type_node, size_type_node);
4323
4324 gfor_fndecl_co_min = gfc_build_library_function_decl_with_spec (
4325 get_identifier (PREFIX("caf_co_min")), ". w . w w . . ",
4326 void_type_node, 6, pvoid_type_node, integer_type_node,
4327 pint_type, pchar_type_node, integer_type_node, size_type_node);
4328
4329 gfor_fndecl_co_reduce = gfc_build_library_function_decl_with_spec (
4330 get_identifier (PREFIX("caf_co_reduce")), ". w r . . w w . . ",
4331 void_type_node, 8, pvoid_type_node,
4332 build_pointer_type (build_varargs_function_type_list (void_type_node,
4333 NULL_TREE)),
4334 integer_type_node, integer_type_node, pint_type, pchar_type_node,
4335 integer_type_node, size_type_node);
4336
4337 gfor_fndecl_co_sum = gfc_build_library_function_decl_with_spec (
4338 get_identifier (PREFIX("caf_co_sum")), ". w . w w . ",
4339 void_type_node, 5, pvoid_type_node, integer_type_node,
4340 pint_type, pchar_type_node, size_type_node);
4341
4342 gfor_fndecl_caf_is_present_on_remote
4343 = gfc_build_library_function_decl_with_spec (
4344 get_identifier (PREFIX ("caf_is_present_on_remote")), ". r r r r r ",
4345 integer_type_node, 5, pvoid_type_node, integer_type_node,
4346 integer_type_node, pvoid_type_node, size_type_node);
4347
4348 gfor_fndecl_caf_random_init = gfc_build_library_function_decl (
4349 get_identifier (PREFIX("caf_random_init")),
4350 void_type_node, 2, logical_type_node, logical_type_node);
4351 }
4352
4353 gfc_build_intrinsic_function_decls ();
4354 gfc_build_intrinsic_lib_fndecls ();
4355 gfc_build_io_library_fndecls ();
4356 }
4357
4358
4359 /* Evaluate the length of dummy character variables. */
4360
4361 static void
4362 gfc_trans_dummy_character (gfc_symbol *sym, gfc_charlen *cl,
4363 gfc_wrapped_block *block)
4364 {
4365 stmtblock_t init;
4366
4367 gfc_finish_decl (cl->backend_decl);
4368
4369 gfc_start_block (&init);
4370
4371 /* Evaluate the string length expression. */
4372 gfc_conv_string_length (cl, NULL, &init);
4373
4374 gfc_trans_vla_type_sizes (sym, &init);
4375
4376 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
4377 }
4378
4379
4380 /* Allocate and cleanup an automatic character variable. */
4381
4382 static void
4383 gfc_trans_auto_character_variable (gfc_symbol * sym, gfc_wrapped_block * block)
4384 {
4385 stmtblock_t init;
4386 tree decl;
4387 tree tmp;
4388 bool back;
4389
4390 gcc_assert (sym->backend_decl);
4391 gcc_assert (sym->ts.u.cl && sym->ts.u.cl->length);
4392
4393 gfc_init_block (&init);
4394
4395 /* In the case of non-dummy symbols with dependencies on an old-fashioned
4396 function result (ie. proc_name = proc_name->result), gfc_add_init_cleanup
4397 must be called with the last, optional argument false so that the process
4398 ing of the character length occurs after the processing of the result. */
4399 back = sym->fn_result_dep;
4400
4401 /* Evaluate the string length expression. */
4402 gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
4403
4404 gfc_trans_vla_type_sizes (sym, &init);
4405
4406 decl = sym->backend_decl;
4407
4408 /* Emit a DECL_EXPR for this variable, which will cause the
4409 gimplifier to allocate storage, and all that good stuff. */
4410 tmp = fold_build1_loc (input_location, DECL_EXPR, TREE_TYPE (decl), decl);
4411 gfc_add_expr_to_block (&init, tmp);
4412
4413 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE, back);
4414 }
4415
4416 /* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
4417
4418 static void
4419 gfc_trans_assign_aux_var (gfc_symbol * sym, gfc_wrapped_block * block)
4420 {
4421 stmtblock_t init;
4422
4423 gcc_assert (sym->backend_decl);
4424 gfc_start_block (&init);
4425
4426 /* Set the initial value to length. See the comments in
4427 function gfc_add_assign_aux_vars in this file. */
4428 gfc_add_modify (&init, GFC_DECL_STRING_LEN (sym->backend_decl),
4429 build_int_cst (gfc_charlen_type_node, -2));
4430
4431 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
4432 }
4433
4434 static void
4435 gfc_trans_vla_one_sizepos (tree *tp, stmtblock_t *body)
4436 {
4437 tree t = *tp, var, val;
4438
4439 if (t == NULL || t == error_mark_node)
4440 return;
4441 if (TREE_CONSTANT (t) || DECL_P (t))
4442 return;
4443
4444 if (TREE_CODE (t) == SAVE_EXPR)
4445 {
4446 if (SAVE_EXPR_RESOLVED_P (t))
4447 {
4448 *tp = TREE_OPERAND (t, 0);
4449 return;
4450 }
4451 val = TREE_OPERAND (t, 0);
4452 }
4453 else
4454 val = t;
4455
4456 var = gfc_create_var_np (TREE_TYPE (t), NULL);
4457 gfc_add_decl_to_function (var);
4458 gfc_add_modify (body, var, unshare_expr (val));
4459 if (TREE_CODE (t) == SAVE_EXPR)
4460 TREE_OPERAND (t, 0) = var;
4461 *tp = var;
4462 }
4463
4464 static void
4465 gfc_trans_vla_type_sizes_1 (tree type, stmtblock_t *body)
4466 {
4467 tree t;
4468
4469 if (type == NULL || type == error_mark_node)
4470 return;
4471
4472 type = TYPE_MAIN_VARIANT (type);
4473
4474 if (TREE_CODE (type) == INTEGER_TYPE)
4475 {
4476 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type), body);
4477 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type), body);
4478
4479 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
4480 {
4481 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
4482 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
4483 }
4484 }
4485 else if (TREE_CODE (type) == ARRAY_TYPE)
4486 {
4487 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type), body);
4488 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type), body);
4489 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type), body);
4490 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type), body);
4491
4492 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
4493 {
4494 TYPE_SIZE (t) = TYPE_SIZE (type);
4495 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
4496 }
4497 }
4498 }
4499
4500 /* Make sure all type sizes and array domains are either constant,
4501 or variable or parameter decls. This is a simplified variant
4502 of gimplify_type_sizes, but we can't use it here, as none of the
4503 variables in the expressions have been gimplified yet.
4504 As type sizes and domains for various variable length arrays
4505 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
4506 time, without this routine gimplify_type_sizes in the middle-end
4507 could result in the type sizes being gimplified earlier than where
4508 those variables are initialized. */
4509
4510 void
4511 gfc_trans_vla_type_sizes (gfc_symbol *sym, stmtblock_t *body)
4512 {
4513 tree type = TREE_TYPE (sym->backend_decl);
4514
4515 if (TREE_CODE (type) == FUNCTION_TYPE
4516 && (sym->attr.function || sym->attr.result || sym->attr.entry))
4517 {
4518 if (! current_fake_result_decl)
4519 return;
4520
4521 type = TREE_TYPE (TREE_VALUE (current_fake_result_decl));
4522 }
4523
4524 while (POINTER_TYPE_P (type))
4525 type = TREE_TYPE (type);
4526
4527 if (GFC_DESCRIPTOR_TYPE_P (type))
4528 {
4529 tree etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
4530
4531 while (POINTER_TYPE_P (etype))
4532 etype = TREE_TYPE (etype);
4533
4534 gfc_trans_vla_type_sizes_1 (etype, body);
4535 }
4536
4537 gfc_trans_vla_type_sizes_1 (type, body);
4538 }
4539
4540
4541 /* Initialize a derived type by building an lvalue from the symbol
4542 and using trans_assignment to do the work. Set dealloc to false
4543 if no deallocation prior the assignment is needed. */
4544 void
4545 gfc_init_default_dt (gfc_symbol * sym, stmtblock_t * block, bool dealloc)
4546 {
4547 gfc_expr *e;
4548 tree tmp;
4549 tree present;
4550
4551 gcc_assert (block);
4552
4553 /* Initialization of PDTs is done elsewhere. */
4554 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.pdt_type)
4555 return;
4556
4557 gcc_assert (!sym->attr.allocatable);
4558 gfc_set_sym_referenced (sym);
4559 e = gfc_lval_expr_from_sym (sym);
4560 tmp = gfc_trans_assignment (e, sym->value, false, dealloc);
4561 if (sym->attr.dummy && (sym->attr.optional
4562 || sym->ns->proc_name->attr.entry_master))
4563 {
4564 present = gfc_conv_expr_present (sym);
4565 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
4566 tmp, build_empty_stmt (input_location));
4567 }
4568 gfc_add_expr_to_block (block, tmp);
4569 gfc_free_expr (e);
4570 }
4571
4572
4573 /* Initialize INTENT(OUT) derived type dummies. As well as giving
4574 them their default initializer, if they have allocatable
4575 components, they have their allocatable components deallocated. */
4576
4577 static void
4578 init_intent_out_dt (gfc_symbol * proc_sym, gfc_wrapped_block * block)
4579 {
4580 stmtblock_t init;
4581 gfc_formal_arglist *f;
4582 tree tmp;
4583 tree present;
4584 gfc_symbol *s;
4585
4586 gfc_init_block (&init);
4587 for (f = gfc_sym_get_dummy_args (proc_sym); f; f = f->next)
4588 if (f->sym && f->sym->attr.intent == INTENT_OUT
4589 && !f->sym->attr.pointer
4590 && f->sym->ts.type == BT_DERIVED)
4591 {
4592 s = f->sym;
4593 tmp = NULL_TREE;
4594
4595 /* Note: Allocatables are excluded as they are already handled
4596 by the caller. */
4597 if (!f->sym->attr.allocatable
4598 && gfc_is_finalizable (s->ts.u.derived, NULL))
4599 {
4600 stmtblock_t block;
4601 gfc_expr *e;
4602
4603 gfc_init_block (&block);
4604 s->attr.referenced = 1;
4605 e = gfc_lval_expr_from_sym (s);
4606 gfc_add_finalizer_call (&block, e);
4607 gfc_free_expr (e);
4608 tmp = gfc_finish_block (&block);
4609 }
4610
4611 /* Note: Allocatables are excluded as they are already handled
4612 by the caller. */
4613 if (tmp == NULL_TREE && !s->attr.allocatable
4614 && s->ts.u.derived->attr.alloc_comp)
4615 tmp = gfc_deallocate_alloc_comp (s->ts.u.derived,
4616 s->backend_decl,
4617 s->as ? s->as->rank : 0);
4618
4619 if (tmp != NULL_TREE && (s->attr.optional
4620 || s->ns->proc_name->attr.entry_master))
4621 {
4622 present = gfc_conv_expr_present (s);
4623 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
4624 present, tmp, build_empty_stmt (input_location));
4625 }
4626
4627 gfc_add_expr_to_block (&init, tmp);
4628 if (s->value && !s->attr.allocatable)
4629 gfc_init_default_dt (s, &init, false);
4630 }
4631 else if (f->sym && f->sym->attr.intent == INTENT_OUT
4632 && f->sym->ts.type == BT_CLASS
4633 && !CLASS_DATA (f->sym)->attr.class_pointer
4634 && !CLASS_DATA (f->sym)->attr.allocatable)
4635 {
4636 stmtblock_t block;
4637 gfc_expr *e;
4638
4639 gfc_init_block (&block);
4640 f->sym->attr.referenced = 1;
4641 e = gfc_lval_expr_from_sym (f->sym);
4642 gfc_add_finalizer_call (&block, e);
4643 gfc_free_expr (e);
4644 tmp = gfc_finish_block (&block);
4645
4646 if (f->sym->attr.optional || f->sym->ns->proc_name->attr.entry_master)
4647 {
4648 present = gfc_conv_expr_present (f->sym);
4649 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
4650 present, tmp,
4651 build_empty_stmt (input_location));
4652 }
4653 gfc_add_expr_to_block (&init, tmp);
4654 }
4655 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
4656 }
4657
4658
4659 /* Helper function to manage deferred string lengths. */
4660
4661 static tree
4662 gfc_null_and_pass_deferred_len (gfc_symbol *sym, stmtblock_t *init,
4663 location_t loc)
4664 {
4665 tree tmp;
4666
4667 /* Character length passed by reference. */
4668 tmp = sym->ts.u.cl->passed_length;
4669 tmp = build_fold_indirect_ref_loc (input_location, tmp);
4670 tmp = fold_convert (gfc_charlen_type_node, tmp);
4671
4672 if (!sym->attr.dummy || sym->attr.intent == INTENT_OUT)
4673 /* Zero the string length when entering the scope. */
4674 gfc_add_modify (init, sym->ts.u.cl->backend_decl,
4675 build_int_cst (gfc_charlen_type_node, 0));
4676 else
4677 {
4678 tree tmp2;
4679
4680 tmp2 = fold_build2_loc (input_location, MODIFY_EXPR,
4681 gfc_charlen_type_node,
4682 sym->ts.u.cl->backend_decl, tmp);
4683 if (sym->attr.optional)
4684 {
4685 tree present = gfc_conv_expr_present (sym);
4686 tmp2 = build3_loc (input_location, COND_EXPR,
4687 void_type_node, present, tmp2,
4688 build_empty_stmt (input_location));
4689 }
4690 gfc_add_expr_to_block (init, tmp2);
4691 }
4692
4693 input_location = loc;
4694
4695 /* Pass the final character length back. */
4696 if (sym->attr.intent != INTENT_IN)
4697 {
4698 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
4699 gfc_charlen_type_node, tmp,
4700 sym->ts.u.cl->backend_decl);
4701 if (sym->attr.optional)
4702 {
4703 tree present = gfc_conv_expr_present (sym);
4704 tmp = build3_loc (input_location, COND_EXPR,
4705 void_type_node, present, tmp,
4706 build_empty_stmt (input_location));
4707 }
4708 }
4709 else
4710 tmp = NULL_TREE;
4711
4712 return tmp;
4713 }
4714
4715
4716 /* Get the result expression for a procedure. */
4717
4718 static tree
4719 get_proc_result (gfc_symbol* sym)
4720 {
4721 if (sym->attr.subroutine || sym == sym->result)
4722 {
4723 if (current_fake_result_decl != NULL)
4724 return TREE_VALUE (current_fake_result_decl);
4725
4726 return NULL_TREE;
4727 }
4728
4729 return sym->result->backend_decl;
4730 }
4731
4732
4733 /* Generate function entry and exit code, and add it to the function body.
4734 This includes:
4735 Allocation and initialization of array variables.
4736 Allocation of character string variables.
4737 Initialization and possibly repacking of dummy arrays.
4738 Initialization of ASSIGN statement auxiliary variable.
4739 Initialization of ASSOCIATE names.
4740 Automatic deallocation. */
4741
4742 void
4743 gfc_trans_deferred_vars (gfc_symbol * proc_sym, gfc_wrapped_block * block)
4744 {
4745 location_t loc;
4746 gfc_symbol *sym;
4747 gfc_formal_arglist *f;
4748 stmtblock_t tmpblock;
4749 bool seen_trans_deferred_array = false;
4750 bool is_pdt_type = false;
4751 tree tmp = NULL;
4752 gfc_expr *e;
4753 gfc_se se;
4754 stmtblock_t init;
4755
4756 /* Deal with implicit return variables. Explicit return variables will
4757 already have been added. */
4758 if (gfc_return_by_reference (proc_sym) && proc_sym->result == proc_sym)
4759 {
4760 if (!current_fake_result_decl)
4761 {
4762 gfc_entry_list *el = NULL;
4763 if (proc_sym->attr.entry_master)
4764 {
4765 for (el = proc_sym->ns->entries; el; el = el->next)
4766 if (el->sym != el->sym->result)
4767 break;
4768 }
4769 /* TODO: move to the appropriate place in resolve.cc. */
4770 if (warn_return_type > 0 && el == NULL)
4771 gfc_warning (OPT_Wreturn_type,
4772 "Return value of function %qs at %L not set",
4773 proc_sym->name, &proc_sym->declared_at);
4774 }
4775 else if (proc_sym->as)
4776 {
4777 tree result = TREE_VALUE (current_fake_result_decl);
4778 loc = input_location;
4779 input_location = gfc_get_location (&proc_sym->declared_at);
4780 gfc_trans_dummy_array_bias (proc_sym, result, block);
4781
4782 /* An automatic character length, pointer array result. */
4783 if (proc_sym->ts.type == BT_CHARACTER
4784 && VAR_P (proc_sym->ts.u.cl->backend_decl))
4785 {
4786 tmp = NULL;
4787 if (proc_sym->ts.deferred)
4788 {
4789 gfc_start_block (&init);
4790 tmp = gfc_null_and_pass_deferred_len (proc_sym, &init, loc);
4791 gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
4792 }
4793 else
4794 gfc_trans_dummy_character (proc_sym, proc_sym->ts.u.cl, block);
4795 }
4796 }
4797 else if (proc_sym->ts.type == BT_CHARACTER)
4798 {
4799 if (proc_sym->ts.deferred)
4800 {
4801 tmp = NULL;
4802 loc = input_location;
4803 input_location = gfc_get_location (&proc_sym->declared_at);
4804 gfc_start_block (&init);
4805 /* Zero the string length on entry. */
4806 gfc_add_modify (&init, proc_sym->ts.u.cl->backend_decl,
4807 build_int_cst (gfc_charlen_type_node, 0));
4808 /* Null the pointer. */
4809 e = gfc_lval_expr_from_sym (proc_sym);
4810 gfc_init_se (&se, NULL);
4811 se.want_pointer = 1;
4812 gfc_conv_expr (&se, e);
4813 gfc_free_expr (e);
4814 tmp = se.expr;
4815 gfc_add_modify (&init, tmp,
4816 fold_convert (TREE_TYPE (se.expr),
4817 null_pointer_node));
4818 input_location = loc;
4819
4820 /* Pass back the string length on exit. */
4821 tmp = proc_sym->ts.u.cl->backend_decl;
4822 if (TREE_CODE (tmp) != INDIRECT_REF
4823 && proc_sym->ts.u.cl->passed_length)
4824 {
4825 tmp = proc_sym->ts.u.cl->passed_length;
4826 tmp = build_fold_indirect_ref_loc (input_location, tmp);
4827 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
4828 TREE_TYPE (tmp), tmp,
4829 fold_convert
4830 (TREE_TYPE (tmp),
4831 proc_sym->ts.u.cl->backend_decl));
4832 }
4833 else
4834 tmp = NULL_TREE;
4835
4836 gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
4837 }
4838 else if (VAR_P (proc_sym->ts.u.cl->backend_decl))
4839 gfc_trans_dummy_character (proc_sym, proc_sym->ts.u.cl, block);
4840 }
4841 else
4842 gcc_assert (flag_f2c && proc_sym->ts.type == BT_COMPLEX);
4843 }
4844 else if (proc_sym == proc_sym->result && IS_CLASS_ARRAY (proc_sym))
4845 {
4846 /* Nullify explicit return class arrays on entry. */
4847 tree type;
4848 tmp = get_proc_result (proc_sym);
4849 if (tmp && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
4850 {
4851 gfc_start_block (&init);
4852 tmp = gfc_class_data_get (tmp);
4853 type = TREE_TYPE (gfc_conv_descriptor_data_get (tmp));
4854 gfc_conv_descriptor_data_set (&init, tmp, build_int_cst (type, 0));
4855 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
4856 }
4857 }
4858
4859
4860 /* Initialize the INTENT(OUT) derived type dummy arguments. This
4861 should be done here so that the offsets and lbounds of arrays
4862 are available. */
4863 loc = input_location;
4864 input_location = gfc_get_location (&proc_sym->declared_at);
4865 init_intent_out_dt (proc_sym, block);
4866 input_location = loc;
4867
4868 /* For some reasons, internal procedures point to the parent's
4869 namespace. Top-level procedure and variables inside BLOCK are fine. */
4870 gfc_namespace *omp_ns = proc_sym->ns;
4871 if (proc_sym->ns->proc_name != proc_sym)
4872 for (omp_ns = proc_sym->ns->contained; omp_ns;
4873 omp_ns = omp_ns->sibling)
4874 if (omp_ns->proc_name == proc_sym)
4875 break;
4876
4877 /* Add 'omp allocate' attribute for gfc_trans_auto_array_allocation and
4878 unset attr.omp_allocate for 'omp allocate allocator(omp_default_mem_alloc),
4879 which has the normal codepath except for an invalid-use check in the ME.
4880 The main processing happens later in this function. */
4881 for (struct gfc_omp_namelist *n = omp_ns ? omp_ns->omp_allocate : NULL;
4882 n; n = n->next)
4883 if (!TREE_STATIC (n->sym->backend_decl))
4884 {
4885 /* Add empty entries - described and to be filled below. */
4886 tree tmp = build_tree_list (NULL_TREE, NULL_TREE);
4887 TREE_CHAIN (tmp) = build_tree_list (NULL_TREE, NULL_TREE);
4888 DECL_ATTRIBUTES (n->sym->backend_decl)
4889 = tree_cons (get_identifier ("omp allocate"), tmp,
4890 DECL_ATTRIBUTES (n->sym->backend_decl));
4891 if (n->u.align == NULL
4892 && n->u2.allocator != NULL
4893 && n->u2.allocator->expr_type == EXPR_CONSTANT
4894 && mpz_cmp_si (n->u2.allocator->value.integer, 1) == 0)
4895 n->sym->attr.omp_allocate = 0;
4896 }
4897
4898 for (sym = proc_sym->tlink; sym != proc_sym; sym = sym->tlink)
4899 {
4900 bool alloc_comp_or_fini = (sym->ts.type == BT_DERIVED)
4901 && (sym->ts.u.derived->attr.alloc_comp
4902 || gfc_is_finalizable (sym->ts.u.derived,
4903 NULL));
4904 if (sym->assoc)
4905 continue;
4906
4907 /* Set the vptr of unlimited polymorphic pointer variables so that
4908 they do not cause segfaults in select type, when the selector
4909 is an intrinsic type. */
4910 if (sym->ts.type == BT_CLASS && UNLIMITED_POLY (sym)
4911 && sym->attr.flavor == FL_VARIABLE && !sym->assoc
4912 && !sym->attr.dummy && CLASS_DATA (sym)->attr.class_pointer)
4913 {
4914 gfc_symbol *vtab;
4915 gfc_init_block (&tmpblock);
4916 vtab = gfc_find_vtab (&sym->ts);
4917 if (!vtab->backend_decl)
4918 {
4919 if (!vtab->attr.referenced)
4920 gfc_set_sym_referenced (vtab);
4921 gfc_get_symbol_decl (vtab);
4922 }
4923 tmp = gfc_class_vptr_get (sym->backend_decl);
4924 gfc_add_modify (&tmpblock, tmp,
4925 gfc_build_addr_expr (TREE_TYPE (tmp),
4926 vtab->backend_decl));
4927 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), NULL);
4928 }
4929
4930 if (sym->ts.type == BT_DERIVED
4931 && sym->ts.u.derived
4932 && sym->ts.u.derived->attr.pdt_type)
4933 {
4934 is_pdt_type = true;
4935 gfc_init_block (&tmpblock);
4936 if (!(sym->attr.dummy
4937 || sym->attr.pointer
4938 || sym->attr.allocatable))
4939 {
4940 tmp = gfc_allocate_pdt_comp (sym->ts.u.derived,
4941 sym->backend_decl,
4942 sym->as ? sym->as->rank : 0,
4943 sym->param_list);
4944 gfc_add_expr_to_block (&tmpblock, tmp);
4945 if (!sym->attr.result)
4946 tmp = gfc_deallocate_pdt_comp (sym->ts.u.derived,
4947 sym->backend_decl,
4948 sym->as ? sym->as->rank : 0);
4949 else
4950 tmp = NULL_TREE;
4951 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), tmp);
4952 }
4953 else if (sym->attr.dummy)
4954 {
4955 tmp = gfc_check_pdt_dummy (sym->ts.u.derived,
4956 sym->backend_decl,
4957 sym->as ? sym->as->rank : 0,
4958 sym->param_list);
4959 gfc_add_expr_to_block (&tmpblock, tmp);
4960 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), NULL);
4961 }
4962 }
4963 else if (sym->ts.type == BT_CLASS
4964 && CLASS_DATA (sym)->ts.u.derived
4965 && CLASS_DATA (sym)->ts.u.derived->attr.pdt_type)
4966 {
4967 gfc_component *data = CLASS_DATA (sym);
4968 is_pdt_type = true;
4969 gfc_init_block (&tmpblock);
4970 if (!(sym->attr.dummy
4971 || CLASS_DATA (sym)->attr.pointer
4972 || CLASS_DATA (sym)->attr.allocatable))
4973 {
4974 tmp = gfc_class_data_get (sym->backend_decl);
4975 tmp = gfc_allocate_pdt_comp (data->ts.u.derived, tmp,
4976 data->as ? data->as->rank : 0,
4977 sym->param_list);
4978 gfc_add_expr_to_block (&tmpblock, tmp);
4979 tmp = gfc_class_data_get (sym->backend_decl);
4980 if (!sym->attr.result)
4981 tmp = gfc_deallocate_pdt_comp (data->ts.u.derived, tmp,
4982 data->as ? data->as->rank : 0);
4983 else
4984 tmp = NULL_TREE;
4985 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), tmp);
4986 }
4987 else if (sym->attr.dummy)
4988 {
4989 tmp = gfc_class_data_get (sym->backend_decl);
4990 tmp = gfc_check_pdt_dummy (data->ts.u.derived, tmp,
4991 data->as ? data->as->rank : 0,
4992 sym->param_list);
4993 gfc_add_expr_to_block (&tmpblock, tmp);
4994 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), NULL);
4995 }
4996 }
4997
4998 if (sym->attr.pointer && sym->attr.dimension
4999 && sym->attr.save == SAVE_NONE
5000 && !sym->attr.use_assoc
5001 && !sym->attr.host_assoc
5002 && !sym->attr.dummy
5003 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (sym->backend_decl)))
5004 {
5005 gfc_init_block (&tmpblock);
5006 gfc_conv_descriptor_span_set (&tmpblock, sym->backend_decl,
5007 build_int_cst (gfc_array_index_type, 0));
5008 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock),
5009 NULL_TREE);
5010 }
5011
5012 if (sym->ts.type == BT_CLASS
5013 && (sym->attr.save || flag_max_stack_var_size == 0)
5014 && CLASS_DATA (sym)->attr.allocatable)
5015 {
5016 tree vptr;
5017
5018 if (UNLIMITED_POLY (sym))
5019 vptr = null_pointer_node;
5020 else
5021 {
5022 gfc_symbol *vsym;
5023 vsym = gfc_find_derived_vtab (sym->ts.u.derived);
5024 vptr = gfc_get_symbol_decl (vsym);
5025 vptr = gfc_build_addr_expr (NULL, vptr);
5026 }
5027
5028 if (CLASS_DATA (sym)->attr.dimension
5029 || (CLASS_DATA (sym)->attr.codimension
5030 && flag_coarray != GFC_FCOARRAY_LIB))
5031 {
5032 tmp = gfc_class_data_get (sym->backend_decl);
5033 tmp = gfc_build_null_descriptor (TREE_TYPE (tmp));
5034 }
5035 else
5036 tmp = null_pointer_node;
5037
5038 DECL_INITIAL (sym->backend_decl)
5039 = gfc_class_set_static_fields (sym->backend_decl, vptr, tmp);
5040 TREE_CONSTANT (DECL_INITIAL (sym->backend_decl)) = 1;
5041 }
5042 else if ((sym->attr.dimension || sym->attr.codimension
5043 || (IS_CLASS_COARRAY_OR_ARRAY (sym)
5044 && !CLASS_DATA (sym)->attr.allocatable)))
5045 {
5046 bool is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
5047 symbol_attribute *array_attr;
5048 gfc_array_spec *as;
5049 array_type type_of_array;
5050
5051 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
5052 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
5053 /* Assumed-size Cray pointees need to be treated as AS_EXPLICIT. */
5054 type_of_array = as->type;
5055 if (type_of_array == AS_ASSUMED_SIZE && as->cp_was_assumed)
5056 type_of_array = AS_EXPLICIT;
5057 switch (type_of_array)
5058 {
5059 case AS_EXPLICIT:
5060 if (sym->attr.dummy || sym->attr.result)
5061 gfc_trans_dummy_array_bias (sym, sym->backend_decl, block);
5062 /* Allocatable and pointer arrays need to processed
5063 explicitly. */
5064 else if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
5065 || (sym->ts.type == BT_CLASS
5066 && CLASS_DATA (sym)->attr.class_pointer)
5067 || array_attr->allocatable)
5068 {
5069 if (TREE_STATIC (sym->backend_decl))
5070 {
5071 loc = input_location;
5072 input_location = gfc_get_location (&sym->declared_at);
5073 gfc_trans_static_array_pointer (sym);
5074 input_location = loc;
5075 }
5076 else
5077 {
5078 seen_trans_deferred_array = true;
5079 gfc_trans_deferred_array (sym, block);
5080 }
5081 }
5082 else if (sym->attr.codimension
5083 && TREE_STATIC (sym->backend_decl))
5084 {
5085 gfc_init_block (&tmpblock);
5086 gfc_trans_array_cobounds (TREE_TYPE (sym->backend_decl),
5087 &tmpblock, sym);
5088 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock),
5089 NULL_TREE);
5090 continue;
5091 }
5092 else if (sym->attr.codimension && !sym->attr.dimension)
5093 {
5094 /* Scalar coarrays do not need array allocation. */
5095 continue;
5096 }
5097 else
5098 {
5099 loc = input_location;
5100 input_location = gfc_get_location (&sym->declared_at);
5101
5102 if (alloc_comp_or_fini)
5103 {
5104 seen_trans_deferred_array = true;
5105 gfc_trans_deferred_array (sym, block);
5106 }
5107 else if (sym->ts.type == BT_DERIVED
5108 && sym->value
5109 && !sym->attr.data
5110 && sym->attr.save == SAVE_NONE)
5111 {
5112 gfc_start_block (&tmpblock);
5113 gfc_init_default_dt (sym, &tmpblock, false);
5114 gfc_add_init_cleanup (block,
5115 gfc_finish_block (&tmpblock),
5116 NULL_TREE);
5117 }
5118
5119 gfc_trans_auto_array_allocation (sym->backend_decl,
5120 sym, block);
5121 input_location = loc;
5122 }
5123 break;
5124
5125 case AS_ASSUMED_SIZE:
5126 /* Must be a dummy parameter. */
5127 gcc_assert (sym->attr.dummy || as->cp_was_assumed);
5128
5129 /* We should always pass assumed size arrays the g77 way. */
5130 if (sym->attr.dummy)
5131 gfc_trans_g77_array (sym, block);
5132 break;
5133
5134 case AS_ASSUMED_SHAPE:
5135 /* Must be a dummy parameter. */
5136 gcc_assert (sym->attr.dummy);
5137
5138 gfc_trans_dummy_array_bias (sym, sym->backend_decl, block);
5139 break;
5140
5141 case AS_ASSUMED_RANK:
5142 case AS_DEFERRED:
5143 seen_trans_deferred_array = true;
5144 gfc_trans_deferred_array (sym, block);
5145 if (sym->ts.type == BT_CHARACTER && sym->ts.deferred
5146 && sym->attr.result)
5147 {
5148 gfc_start_block (&init);
5149 loc = input_location;
5150 input_location = gfc_get_location (&sym->declared_at);
5151 tmp = gfc_null_and_pass_deferred_len (sym, &init, loc);
5152 gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
5153 }
5154 break;
5155
5156 default:
5157 gcc_unreachable ();
5158 }
5159 if (alloc_comp_or_fini && !seen_trans_deferred_array)
5160 gfc_trans_deferred_array (sym, block);
5161 }
5162 else if ((!sym->attr.dummy || sym->ts.deferred)
5163 && (sym->ts.type == BT_CLASS
5164 && CLASS_DATA (sym)->attr.class_pointer))
5165 gfc_trans_class_array (sym, block);
5166 else if ((!sym->attr.dummy || sym->ts.deferred)
5167 && (sym->attr.allocatable
5168 || (sym->attr.pointer && sym->attr.result)
5169 || (sym->ts.type == BT_CLASS
5170 && CLASS_DATA (sym)->attr.allocatable)))
5171 {
5172 if (!sym->attr.save && flag_max_stack_var_size != 0)
5173 {
5174 tree descriptor = NULL_TREE;
5175
5176 loc = input_location;
5177 input_location = gfc_get_location (&sym->declared_at);
5178 gfc_start_block (&init);
5179
5180 if (sym->ts.type == BT_CHARACTER
5181 && sym->attr.allocatable
5182 && !sym->attr.dimension
5183 && sym->ts.u.cl && sym->ts.u.cl->length
5184 && sym->ts.u.cl->length->expr_type == EXPR_VARIABLE)
5185 gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
5186
5187 if (!sym->attr.pointer)
5188 {
5189 /* Nullify and automatic deallocation of allocatable
5190 scalars. */
5191 e = gfc_lval_expr_from_sym (sym);
5192 if (sym->ts.type == BT_CLASS)
5193 gfc_add_data_component (e);
5194
5195 gfc_init_se (&se, NULL);
5196 if (sym->ts.type != BT_CLASS
5197 || sym->ts.u.derived->attr.dimension
5198 || sym->ts.u.derived->attr.codimension)
5199 {
5200 se.want_pointer = 1;
5201 gfc_conv_expr (&se, e);
5202 }
5203 else if (sym->ts.type == BT_CLASS
5204 && !CLASS_DATA (sym)->attr.dimension
5205 && !CLASS_DATA (sym)->attr.codimension)
5206 {
5207 se.want_pointer = 1;
5208 gfc_conv_expr (&se, e);
5209 }
5210 else
5211 {
5212 se.descriptor_only = 1;
5213 gfc_conv_expr (&se, e);
5214 descriptor = se.expr;
5215 se.expr = gfc_conv_descriptor_data_addr (se.expr);
5216 se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
5217 }
5218 gfc_free_expr (e);
5219
5220 if (!sym->attr.dummy || sym->attr.intent == INTENT_OUT)
5221 {
5222 /* Nullify when entering the scope. */
5223 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
5224 TREE_TYPE (se.expr), se.expr,
5225 fold_convert (TREE_TYPE (se.expr),
5226 null_pointer_node));
5227 if (sym->attr.optional)
5228 {
5229 tree present = gfc_conv_expr_present (sym);
5230 tmp = build3_loc (input_location, COND_EXPR,
5231 void_type_node, present, tmp,
5232 build_empty_stmt (input_location));
5233 }
5234 gfc_add_expr_to_block (&init, tmp);
5235 }
5236 }
5237
5238 if ((sym->attr.dummy || sym->attr.result)
5239 && sym->ts.type == BT_CHARACTER
5240 && sym->ts.deferred
5241 && sym->ts.u.cl->passed_length)
5242 tmp = gfc_null_and_pass_deferred_len (sym, &init, loc);
5243 else
5244 {
5245 input_location = loc;
5246 tmp = NULL_TREE;
5247 }
5248
5249 /* Initialize descriptor's TKR information. */
5250 if (sym->ts.type == BT_CLASS)
5251 gfc_trans_class_array (sym, block);
5252
5253 /* Deallocate when leaving the scope. Nullifying is not
5254 needed. */
5255 if (!sym->attr.result && !sym->attr.dummy && !sym->attr.pointer
5256 && !sym->ns->proc_name->attr.is_main_program)
5257 {
5258 if (sym->ts.type == BT_CLASS
5259 && CLASS_DATA (sym)->attr.codimension)
5260 tmp = gfc_deallocate_with_status (descriptor, NULL_TREE,
5261 NULL_TREE, NULL_TREE,
5262 NULL_TREE, true, NULL,
5263 GFC_CAF_COARRAY_ANALYZE);
5264 else
5265 {
5266 gfc_expr *expr = gfc_lval_expr_from_sym (sym);
5267 tmp = gfc_deallocate_scalar_with_status (se.expr,
5268 NULL_TREE,
5269 NULL_TREE,
5270 true, expr,
5271 sym->ts);
5272 gfc_free_expr (expr);
5273 }
5274 }
5275
5276 if (sym->ts.type == BT_CLASS)
5277 {
5278 /* Initialize _vptr to declared type. */
5279 loc = input_location;
5280 input_location = gfc_get_location (&sym->declared_at);
5281
5282 e = gfc_lval_expr_from_sym (sym);
5283 gfc_reset_vptr (&init, e);
5284 gfc_free_expr (e);
5285 input_location = loc;
5286 }
5287
5288 gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
5289 }
5290 }
5291 else if (sym->ts.type == BT_CHARACTER && sym->ts.deferred)
5292 {
5293 tree tmp = NULL;
5294 stmtblock_t init;
5295
5296 /* If we get to here, all that should be left are pointers. */
5297 gcc_assert (sym->attr.pointer);
5298
5299 if (sym->attr.dummy)
5300 {
5301 gfc_start_block (&init);
5302 loc = input_location;
5303 input_location = gfc_get_location (&sym->declared_at);
5304 tmp = gfc_null_and_pass_deferred_len (sym, &init, loc);
5305 gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
5306 }
5307 }
5308 else if (sym->ts.deferred)
5309 gfc_fatal_error ("Deferred type parameter not yet supported");
5310 else if (alloc_comp_or_fini)
5311 gfc_trans_deferred_array (sym, block);
5312 else if (sym->ts.type == BT_CHARACTER)
5313 {
5314 loc = input_location;
5315 input_location = gfc_get_location (&sym->declared_at);
5316 if (sym->attr.dummy || sym->attr.result)
5317 gfc_trans_dummy_character (sym, sym->ts.u.cl, block);
5318 else
5319 gfc_trans_auto_character_variable (sym, block);
5320 input_location = loc;
5321 }
5322 else if (sym->attr.assign)
5323 {
5324 loc = input_location;
5325 input_location = gfc_get_location (&sym->declared_at);
5326 gfc_trans_assign_aux_var (sym, block);
5327 input_location = loc;
5328 }
5329 else if (sym->ts.type == BT_DERIVED
5330 && sym->value
5331 && !sym->attr.data
5332 && sym->attr.save == SAVE_NONE)
5333 {
5334 gfc_start_block (&tmpblock);
5335 gfc_init_default_dt (sym, &tmpblock, false);
5336 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock),
5337 NULL_TREE);
5338 }
5339 else if (!(UNLIMITED_POLY(sym)) && !is_pdt_type)
5340 gcc_unreachable ();
5341 }
5342
5343 /* Handle 'omp allocate'. This has to be after the block above as
5344 gfc_add_init_cleanup (..., init, ...) puts 'init' of later calls
5345 before earlier calls. The code is a bit more complex as gfortran does
5346 not really work with bind expressions / BIND_EXPR_VARS properly, i.e.
5347 gimplify_bind_expr needs some help for placing the GOMP_alloc. Thus,
5348 we pass on the location of the allocate-assignment expression and,
5349 if the size is not constant, the size variable if Fortran computes this
5350 differently. We also might add an expression location after which the
5351 code has to be added, e.g. for character len expressions, which affect
5352 the UNIT_SIZE. */
5353 gfc_expr *last_allocator = NULL;
5354 if (omp_ns && omp_ns->omp_allocate)
5355 {
5356 if (!block->init || TREE_CODE (block->init) != STATEMENT_LIST)
5357 {
5358 tree tmp = build1_v (LABEL_EXPR, gfc_build_label_decl (NULL_TREE));
5359 append_to_statement_list (tmp, &block->init);
5360 }
5361 if (!block->cleanup || TREE_CODE (block->cleanup) != STATEMENT_LIST)
5362 {
5363 tree tmp = build1_v (LABEL_EXPR, gfc_build_label_decl (NULL_TREE));
5364 append_to_statement_list (tmp, &block->cleanup);
5365 }
5366 }
5367 tree init_stmtlist = block->init;
5368 tree cleanup_stmtlist = block->cleanup;
5369 se.expr = NULL_TREE;
5370 for (struct gfc_omp_namelist *n = omp_ns ? omp_ns->omp_allocate : NULL;
5371 n; n = n->next)
5372 {
5373 tree align = (n->u.align ? gfc_conv_constant_to_tree (n->u.align) : NULL_TREE);
5374 if (last_allocator != n->u2.allocator)
5375 {
5376 location_t loc = input_location;
5377 gfc_init_se (&se, NULL);
5378 if (n->u2.allocator)
5379 {
5380 input_location = gfc_get_location (&n->u2.allocator->where);
5381 gfc_conv_expr (&se, n->u2.allocator);
5382 }
5383 /* We need to evalulate non-constants - also to find the location
5384 after which the GOMP_alloc has to be added to - also as BLOCK
5385 does not yield a new BIND_EXPR_BODY. */
5386 if (n->u2.allocator
5387 && (!(CONSTANT_CLASS_P (se.expr) && DECL_P (se.expr))
5388 || se.pre.head || se.post.head))
5389 {
5390 stmtblock_t tmpblock;
5391 gfc_init_block (&tmpblock);
5392 se.expr = gfc_evaluate_now (se.expr, &tmpblock);
5393 /* First post then pre because the new code is inserted
5394 at the top. */
5395 gfc_add_init_cleanup (block, gfc_finish_block (&se.post), NULL);
5396 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock),
5397 NULL);
5398 gfc_add_init_cleanup (block, gfc_finish_block (&se.pre), NULL);
5399 }
5400 last_allocator = n->u2.allocator;
5401 input_location = loc;
5402 }
5403 if (TREE_STATIC (n->sym->backend_decl))
5404 continue;
5405 /* 'omp allocate( {purpose: allocator, value: align},
5406 {purpose: init-stmtlist, value: cleanup-stmtlist},
5407 {purpose: size-var, value: last-size-expr}}
5408 where init-stmt/cleanup-stmt is the STATEMENT list to find the
5409 try-final block; last-size-expr is to find the location after
5410 which to add the code and 'size-var' is for the proper size, cf.
5411 gfc_trans_auto_array_allocation - either or both of the latter
5412 can be NULL. */
5413 tree tmp = lookup_attribute ("omp allocate",
5414 DECL_ATTRIBUTES (n->sym->backend_decl));
5415 tmp = TREE_VALUE (tmp);
5416 TREE_PURPOSE (tmp) = se.expr;
5417 TREE_VALUE (tmp) = align;
5418 TREE_PURPOSE (TREE_CHAIN (tmp)) = init_stmtlist;
5419 TREE_VALUE (TREE_CHAIN (tmp)) = cleanup_stmtlist;
5420 }
5421
5422 gfc_init_block (&tmpblock);
5423
5424 for (f = gfc_sym_get_dummy_args (proc_sym); f; f = f->next)
5425 {
5426 if (f->sym && f->sym->tlink == NULL && f->sym->ts.type == BT_CHARACTER
5427 && f->sym->ts.u.cl->backend_decl)
5428 {
5429 if (TREE_CODE (f->sym->ts.u.cl->backend_decl) == PARM_DECL)
5430 gfc_trans_vla_type_sizes (f->sym, &tmpblock);
5431 }
5432 }
5433
5434 if (gfc_return_by_reference (proc_sym) && proc_sym->ts.type == BT_CHARACTER
5435 && current_fake_result_decl != NULL)
5436 {
5437 gcc_assert (proc_sym->ts.u.cl->backend_decl != NULL);
5438 if (TREE_CODE (proc_sym->ts.u.cl->backend_decl) == PARM_DECL)
5439 gfc_trans_vla_type_sizes (proc_sym, &tmpblock);
5440 }
5441
5442 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), NULL_TREE);
5443 }
5444
5445
5446 struct module_hasher : ggc_ptr_hash<module_htab_entry>
5447 {
5448 typedef const char *compare_type;
5449
5450 static hashval_t hash (module_htab_entry *s)
5451 {
5452 return htab_hash_string (s->name);
5453 }
5454
5455 static bool
5456 equal (module_htab_entry *a, const char *b)
5457 {
5458 return !strcmp (a->name, b);
5459 }
5460 };
5461
5462 static GTY (()) hash_table<module_hasher> *module_htab;
5463
5464 /* Hash and equality functions for module_htab's decls. */
5465
5466 hashval_t
5467 module_decl_hasher::hash (tree t)
5468 {
5469 const_tree n = DECL_NAME (t);
5470 if (n == NULL_TREE)
5471 n = TYPE_NAME (TREE_TYPE (t));
5472 return htab_hash_string (IDENTIFIER_POINTER (n));
5473 }
5474
5475 bool
5476 module_decl_hasher::equal (tree t1, const char *x2)
5477 {
5478 const_tree n1 = DECL_NAME (t1);
5479 if (n1 == NULL_TREE)
5480 n1 = TYPE_NAME (TREE_TYPE (t1));
5481 return strcmp (IDENTIFIER_POINTER (n1), x2) == 0;
5482 }
5483
5484 struct module_htab_entry *
5485 gfc_find_module (const char *name)
5486 {
5487 if (! module_htab)
5488 module_htab = hash_table<module_hasher>::create_ggc (10);
5489
5490 module_htab_entry **slot
5491 = module_htab->find_slot_with_hash (name, htab_hash_string (name), INSERT);
5492 if (*slot == NULL)
5493 {
5494 module_htab_entry *entry = ggc_cleared_alloc<module_htab_entry> ();
5495
5496 entry->name = gfc_get_string ("%s", name);
5497 entry->decls = hash_table<module_decl_hasher>::create_ggc (10);
5498 *slot = entry;
5499 }
5500 return *slot;
5501 }
5502
5503 void
5504 gfc_module_add_decl (struct module_htab_entry *entry, tree decl)
5505 {
5506 const char *name;
5507
5508 if (DECL_NAME (decl))
5509 name = IDENTIFIER_POINTER (DECL_NAME (decl));
5510 else
5511 {
5512 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
5513 name = IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl)));
5514 }
5515 tree *slot
5516 = entry->decls->find_slot_with_hash (name, htab_hash_string (name),
5517 INSERT);
5518 if (*slot == NULL)
5519 *slot = decl;
5520 }
5521
5522
5523 /* Generate debugging symbols for namelists. This function must come after
5524 generate_local_decl to ensure that the variables in the namelist are
5525 already declared. */
5526
5527 static tree
5528 generate_namelist_decl (gfc_symbol * sym)
5529 {
5530 gfc_namelist *nml;
5531 tree decl;
5532 vec<constructor_elt, va_gc> *nml_decls = NULL;
5533
5534 gcc_assert (sym->attr.flavor == FL_NAMELIST);
5535 for (nml = sym->namelist; nml; nml = nml->next)
5536 {
5537 if (nml->sym->backend_decl == NULL_TREE)
5538 {
5539 nml->sym->attr.referenced = 1;
5540 nml->sym->backend_decl = gfc_get_symbol_decl (nml->sym);
5541 }
5542 DECL_IGNORED_P (nml->sym->backend_decl) = 0;
5543 CONSTRUCTOR_APPEND_ELT (nml_decls, NULL_TREE, nml->sym->backend_decl);
5544 }
5545
5546 decl = make_node (NAMELIST_DECL);
5547 TREE_TYPE (decl) = void_type_node;
5548 NAMELIST_DECL_ASSOCIATED_DECL (decl) = build_constructor (NULL_TREE, nml_decls);
5549 DECL_NAME (decl) = get_identifier (sym->name);
5550 return decl;
5551 }
5552
5553
5554 /* Output an initialized decl for a module variable. */
5555
5556 static void
5557 gfc_create_module_variable (gfc_symbol * sym)
5558 {
5559 tree decl;
5560
5561 /* Module functions with alternate entries are dealt with later and
5562 would get caught by the next condition. */
5563 if (sym->attr.entry)
5564 return;
5565
5566 /* Make sure we convert the types of the derived types from iso_c_binding
5567 into (void *). */
5568 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
5569 && sym->ts.type == BT_DERIVED)
5570 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
5571
5572 if (gfc_fl_struct (sym->attr.flavor)
5573 && sym->backend_decl
5574 && TREE_CODE (sym->backend_decl) == RECORD_TYPE)
5575 {
5576 decl = sym->backend_decl;
5577 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
5578
5579 if (!sym->attr.use_assoc && !sym->attr.used_in_submodule)
5580 {
5581 gcc_assert (TYPE_CONTEXT (decl) == NULL_TREE
5582 || TYPE_CONTEXT (decl) == sym->ns->proc_name->backend_decl);
5583 gcc_assert (DECL_CONTEXT (TYPE_STUB_DECL (decl)) == NULL_TREE
5584 || DECL_CONTEXT (TYPE_STUB_DECL (decl))
5585 == sym->ns->proc_name->backend_decl);
5586 }
5587 TYPE_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
5588 DECL_CONTEXT (TYPE_STUB_DECL (decl)) = sym->ns->proc_name->backend_decl;
5589 gfc_module_add_decl (cur_module, TYPE_STUB_DECL (decl));
5590 }
5591
5592 /* Only output variables, procedure pointers and array valued,
5593 or derived type, parameters. */
5594 if (sym->attr.flavor != FL_VARIABLE
5595 && !(sym->attr.flavor == FL_PARAMETER
5596 && (sym->attr.dimension || sym->ts.type == BT_DERIVED))
5597 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer))
5598 return;
5599
5600 if ((sym->attr.in_common || sym->attr.in_equivalence) && sym->backend_decl)
5601 {
5602 decl = sym->backend_decl;
5603 gcc_assert (DECL_FILE_SCOPE_P (decl));
5604 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
5605 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
5606 gfc_module_add_decl (cur_module, decl);
5607 }
5608
5609 /* Don't generate variables from other modules. Variables from
5610 COMMONs and Cray pointees will already have been generated. */
5611 if (sym->attr.use_assoc || sym->attr.used_in_submodule
5612 || sym->attr.in_common || sym->attr.cray_pointee)
5613 return;
5614
5615 /* Equivalenced variables arrive here after creation. */
5616 if (sym->backend_decl
5617 && (sym->equiv_built || sym->attr.in_equivalence))
5618 return;
5619
5620 if (sym->backend_decl && !sym->attr.vtab && !sym->attr.target)
5621 gfc_internal_error ("backend decl for module variable %qs already exists",
5622 sym->name);
5623
5624 if (sym->module && !sym->attr.result && !sym->attr.dummy
5625 && (sym->attr.access == ACCESS_UNKNOWN
5626 && (sym->ns->default_access == ACCESS_PRIVATE
5627 || (sym->ns->default_access == ACCESS_UNKNOWN
5628 && flag_module_private))))
5629 sym->attr.access = ACCESS_PRIVATE;
5630
5631 if (warn_unused_variable && !sym->attr.referenced
5632 && sym->attr.access == ACCESS_PRIVATE)
5633 gfc_warning (OPT_Wunused_value,
5634 "Unused PRIVATE module variable %qs declared at %L",
5635 sym->name, &sym->declared_at);
5636
5637 /* We always want module variables to be created. */
5638 sym->attr.referenced = 1;
5639 /* Create the decl. */
5640 decl = gfc_get_symbol_decl (sym);
5641
5642 /* Create the variable. */
5643 pushdecl (decl);
5644 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE
5645 || ((sym->ns->parent->proc_name->attr.flavor == FL_MODULE
5646 || sym->ns->parent->proc_name->attr.flavor == FL_PROCEDURE)
5647 && sym->fn_result_spec));
5648 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
5649 rest_of_decl_compilation (decl, 1, 0);
5650 gfc_module_add_decl (cur_module, decl);
5651
5652 /* Also add length of strings. */
5653 if (sym->ts.type == BT_CHARACTER)
5654 {
5655 tree length;
5656
5657 length = sym->ts.u.cl->backend_decl;
5658 gcc_assert (length || sym->attr.proc_pointer);
5659 if (length && !INTEGER_CST_P (length))
5660 {
5661 pushdecl (length);
5662 rest_of_decl_compilation (length, 1, 0);
5663 }
5664 }
5665
5666 if (sym->attr.codimension && !sym->attr.dummy && !sym->attr.allocatable
5667 && sym->attr.referenced && !sym->attr.use_assoc)
5668 has_coarray_vars_or_accessors = true;
5669 }
5670
5671 /* Emit debug information for USE statements. */
5672
5673 static void
5674 gfc_trans_use_stmts (gfc_namespace * ns)
5675 {
5676 gfc_use_list *use_stmt;
5677 for (use_stmt = ns->use_stmts; use_stmt; use_stmt = use_stmt->next)
5678 {
5679 struct module_htab_entry *entry
5680 = gfc_find_module (use_stmt->module_name);
5681 gfc_use_rename *rent;
5682
5683 if (entry->namespace_decl == NULL)
5684 {
5685 entry->namespace_decl
5686 = build_decl (input_location,
5687 NAMESPACE_DECL,
5688 get_identifier (use_stmt->module_name),
5689 void_type_node);
5690 DECL_EXTERNAL (entry->namespace_decl) = 1;
5691 }
5692 input_location = gfc_get_location (&use_stmt->where);
5693 if (!use_stmt->only_flag)
5694 (*debug_hooks->imported_module_or_decl) (entry->namespace_decl,
5695 NULL_TREE,
5696 ns->proc_name->backend_decl,
5697 false, false);
5698 for (rent = use_stmt->rename; rent; rent = rent->next)
5699 {
5700 tree decl, local_name;
5701
5702 if (rent->op != INTRINSIC_NONE)
5703 continue;
5704
5705 hashval_t hash = htab_hash_string (rent->use_name);
5706 tree *slot = entry->decls->find_slot_with_hash (rent->use_name, hash,
5707 INSERT);
5708 if (*slot == NULL)
5709 {
5710 gfc_symtree *st;
5711
5712 st = gfc_find_symtree (ns->sym_root,
5713 rent->local_name[0]
5714 ? rent->local_name : rent->use_name);
5715
5716 /* The following can happen if a derived type is renamed. */
5717 if (!st)
5718 {
5719 char *name;
5720 name = xstrdup (rent->local_name[0]
5721 ? rent->local_name : rent->use_name);
5722 name[0] = (char) TOUPPER ((unsigned char) name[0]);
5723 st = gfc_find_symtree (ns->sym_root, name);
5724 free (name);
5725 gcc_assert (st);
5726 }
5727
5728 /* Sometimes, generic interfaces wind up being over-ruled by a
5729 local symbol (see PR41062). */
5730 if (!st->n.sym->attr.use_assoc)
5731 {
5732 *slot = error_mark_node;
5733 entry->decls->clear_slot (slot);
5734 continue;
5735 }
5736
5737 if (st->n.sym->backend_decl
5738 && DECL_P (st->n.sym->backend_decl)
5739 && st->n.sym->module
5740 && strcmp (st->n.sym->module, use_stmt->module_name) == 0)
5741 {
5742 gcc_assert (DECL_EXTERNAL (entry->namespace_decl)
5743 || !VAR_P (st->n.sym->backend_decl));
5744 decl = copy_node (st->n.sym->backend_decl);
5745 DECL_CONTEXT (decl) = entry->namespace_decl;
5746 DECL_EXTERNAL (decl) = 1;
5747 DECL_IGNORED_P (decl) = 0;
5748 DECL_INITIAL (decl) = NULL_TREE;
5749 }
5750 else if (st->n.sym->attr.flavor == FL_NAMELIST
5751 && st->n.sym->attr.use_only
5752 && st->n.sym->module
5753 && strcmp (st->n.sym->module, use_stmt->module_name)
5754 == 0)
5755 {
5756 decl = generate_namelist_decl (st->n.sym);
5757 DECL_CONTEXT (decl) = entry->namespace_decl;
5758 DECL_EXTERNAL (decl) = 1;
5759 DECL_IGNORED_P (decl) = 0;
5760 DECL_INITIAL (decl) = NULL_TREE;
5761 }
5762 else
5763 {
5764 *slot = error_mark_node;
5765 entry->decls->clear_slot (slot);
5766 continue;
5767 }
5768 *slot = decl;
5769 }
5770 decl = (tree) *slot;
5771 if (rent->local_name[0])
5772 local_name = get_identifier (rent->local_name);
5773 else
5774 local_name = NULL_TREE;
5775 input_location = gfc_get_location (&rent->where);
5776 (*debug_hooks->imported_module_or_decl) (decl, local_name,
5777 ns->proc_name->backend_decl,
5778 !use_stmt->only_flag,
5779 false);
5780 }
5781 }
5782 }
5783
5784
5785 /* Return true if expr is a constant initializer that gfc_conv_initializer
5786 will handle. */
5787
5788 static bool
5789 check_constant_initializer (gfc_expr *expr, gfc_typespec *ts, bool array,
5790 bool pointer)
5791 {
5792 gfc_constructor *c;
5793 gfc_component *cm;
5794
5795 if (pointer)
5796 return true;
5797 else if (array)
5798 {
5799 if (expr->expr_type == EXPR_CONSTANT || expr->expr_type == EXPR_NULL)
5800 return true;
5801 else if (expr->expr_type == EXPR_STRUCTURE)
5802 return check_constant_initializer (expr, ts, false, false);
5803 else if (expr->expr_type != EXPR_ARRAY)
5804 return false;
5805 for (c = gfc_constructor_first (expr->value.constructor);
5806 c; c = gfc_constructor_next (c))
5807 {
5808 if (c->iterator)
5809 return false;
5810 if (c->expr->expr_type == EXPR_STRUCTURE)
5811 {
5812 if (!check_constant_initializer (c->expr, ts, false, false))
5813 return false;
5814 }
5815 else if (c->expr->expr_type != EXPR_CONSTANT)
5816 return false;
5817 }
5818 return true;
5819 }
5820 else switch (ts->type)
5821 {
5822 case_bt_struct:
5823 if (expr->expr_type != EXPR_STRUCTURE)
5824 return false;
5825 cm = expr->ts.u.derived->components;
5826 for (c = gfc_constructor_first (expr->value.constructor);
5827 c; c = gfc_constructor_next (c), cm = cm->next)
5828 {
5829 if (!c->expr || cm->attr.allocatable)
5830 continue;
5831 if (!check_constant_initializer (c->expr, &cm->ts,
5832 cm->attr.dimension,
5833 cm->attr.pointer))
5834 return false;
5835 }
5836 return true;
5837 default:
5838 return expr->expr_type == EXPR_CONSTANT;
5839 }
5840 }
5841
5842 /* Emit debug info for parameters and unreferenced variables with
5843 initializers. */
5844
5845 static void
5846 gfc_emit_parameter_debug_info (gfc_symbol *sym)
5847 {
5848 tree decl;
5849
5850 if (sym->attr.flavor != FL_PARAMETER
5851 && (sym->attr.flavor != FL_VARIABLE || sym->attr.referenced))
5852 return;
5853
5854 if (sym->backend_decl != NULL
5855 || sym->value == NULL
5856 || sym->attr.use_assoc
5857 || sym->attr.dummy
5858 || sym->attr.result
5859 || sym->attr.function
5860 || sym->attr.intrinsic
5861 || sym->attr.pointer
5862 || sym->attr.allocatable
5863 || sym->attr.cray_pointee
5864 || sym->attr.threadprivate
5865 || sym->attr.is_bind_c
5866 || sym->attr.subref_array_pointer
5867 || sym->attr.assign)
5868 return;
5869
5870 if (sym->ts.type == BT_CHARACTER)
5871 {
5872 gfc_conv_const_charlen (sym->ts.u.cl);
5873 if (sym->ts.u.cl->backend_decl == NULL
5874 || TREE_CODE (sym->ts.u.cl->backend_decl) != INTEGER_CST)
5875 return;
5876 }
5877 else if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.alloc_comp)
5878 return;
5879
5880 if (sym->as)
5881 {
5882 int n;
5883
5884 if (sym->as->type != AS_EXPLICIT)
5885 return;
5886 for (n = 0; n < sym->as->rank; n++)
5887 if (sym->as->lower[n]->expr_type != EXPR_CONSTANT
5888 || sym->as->upper[n] == NULL
5889 || sym->as->upper[n]->expr_type != EXPR_CONSTANT)
5890 return;
5891 }
5892
5893 if (!check_constant_initializer (sym->value, &sym->ts,
5894 sym->attr.dimension, false))
5895 return;
5896
5897 if (flag_coarray == GFC_FCOARRAY_LIB && sym->attr.codimension)
5898 return;
5899
5900 /* Create the decl for the variable or constant. */
5901 decl = build_decl (input_location,
5902 sym->attr.flavor == FL_PARAMETER ? CONST_DECL : VAR_DECL,
5903 gfc_sym_identifier (sym), gfc_sym_type (sym));
5904 if (sym->attr.flavor == FL_PARAMETER)
5905 TREE_READONLY (decl) = 1;
5906 gfc_set_decl_location (decl, &sym->declared_at);
5907 if (sym->attr.dimension)
5908 GFC_DECL_PACKED_ARRAY (decl) = 1;
5909 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
5910 TREE_STATIC (decl) = 1;
5911 TREE_USED (decl) = 1;
5912 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
5913 TREE_PUBLIC (decl) = 1;
5914 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
5915 TREE_TYPE (decl),
5916 sym->attr.dimension,
5917 false, false);
5918 debug_hooks->early_global_decl (decl);
5919 }
5920
5921
5922 static void
5923 generate_coarray_sym_init (gfc_symbol *sym)
5924 {
5925 tree tmp, size, decl, token, desc;
5926 bool is_lock_type, is_event_type;
5927 int reg_type;
5928 gfc_se se;
5929 symbol_attribute attr;
5930
5931 if (sym->attr.dummy || sym->attr.allocatable || !sym->attr.codimension
5932 || sym->attr.use_assoc || !sym->attr.referenced
5933 || sym->attr.associate_var
5934 || sym->attr.select_type_temporary)
5935 return;
5936
5937 decl = sym->backend_decl;
5938 TREE_USED(decl) = 1;
5939 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
5940
5941 is_lock_type = sym->ts.type == BT_DERIVED
5942 && sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
5943 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE;
5944
5945 is_event_type = sym->ts.type == BT_DERIVED
5946 && sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
5947 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE;
5948
5949 /* FIXME: Workaround for PR middle-end/49106, cf. also PR middle-end/49108
5950 to make sure the variable is not optimized away. */
5951 DECL_PRESERVE_P (DECL_CONTEXT (decl)) = 1;
5952
5953 /* For lock types, we pass the array size as only the library knows the
5954 size of the variable. */
5955 if (is_lock_type || is_event_type)
5956 size = gfc_index_one_node;
5957 else
5958 size = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (decl)));
5959
5960 /* Ensure that we do not have size=0 for zero-sized arrays. */
5961 size = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
5962 fold_convert (size_type_node, size),
5963 build_int_cst (size_type_node, 1));
5964
5965 if (GFC_TYPE_ARRAY_RANK (TREE_TYPE (decl)))
5966 {
5967 tmp = GFC_TYPE_ARRAY_SIZE (TREE_TYPE (decl));
5968 size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
5969 fold_convert (size_type_node, tmp), size);
5970 }
5971
5972 gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (decl)) != NULL_TREE);
5973 token = gfc_build_addr_expr (ppvoid_type_node,
5974 GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE(decl)));
5975 if (is_lock_type)
5976 reg_type = sym->attr.artificial ? GFC_CAF_CRITICAL : GFC_CAF_LOCK_STATIC;
5977 else if (is_event_type)
5978 reg_type = GFC_CAF_EVENT_STATIC;
5979 else
5980 reg_type = GFC_CAF_COARRAY_STATIC;
5981
5982 /* Compile the symbol attribute. */
5983 if (sym->ts.type == BT_CLASS)
5984 {
5985 attr = CLASS_DATA (sym)->attr;
5986 /* The pointer attribute is always set on classes, overwrite it with the
5987 class_pointer attribute, which denotes the pointer for classes. */
5988 attr.pointer = attr.class_pointer;
5989 }
5990 else
5991 attr = sym->attr;
5992 gfc_init_se (&se, NULL);
5993 desc = gfc_conv_scalar_to_descriptor (&se, decl, attr);
5994 gfc_add_block_to_block (&caf_init_block, &se.pre);
5995
5996 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_register, 7, size,
5997 build_int_cst (integer_type_node, reg_type),
5998 token, gfc_build_addr_expr (pvoid_type_node, desc),
5999 null_pointer_node, /* stat. */
6000 null_pointer_node, /* errgmsg. */
6001 build_zero_cst (size_type_node)); /* errmsg_len. */
6002 gfc_add_expr_to_block (&caf_init_block, tmp);
6003 gfc_add_modify (&caf_init_block, decl, fold_convert (TREE_TYPE (decl),
6004 gfc_conv_descriptor_data_get (desc)));
6005
6006 /* Handle "static" initializer. */
6007 if (sym->value)
6008 {
6009 if (sym->value->expr_type == EXPR_ARRAY)
6010 {
6011 gfc_constructor *c, *cnext;
6012
6013 /* Test if the array has more than one element. */
6014 c = gfc_constructor_first (sym->value->value.constructor);
6015 gcc_assert (c); /* Empty constructor should not happen here. */
6016 cnext = gfc_constructor_next (c);
6017
6018 if (cnext)
6019 {
6020 /* An EXPR_ARRAY with a rank > 1 here has to come from a
6021 DATA statement. Set its rank here as not to confuse
6022 the following steps. */
6023 sym->value->rank = 1;
6024 }
6025 else
6026 {
6027 /* There is only a single value in the constructor, use
6028 it directly for the assignment. */
6029 gfc_expr *new_expr;
6030 new_expr = gfc_copy_expr (c->expr);
6031 gfc_free_expr (sym->value);
6032 sym->value = new_expr;
6033 }
6034 }
6035
6036 sym->attr.pointer = 1;
6037 tmp = gfc_trans_assignment (gfc_lval_expr_from_sym (sym), sym->value,
6038 true, false);
6039 sym->attr.pointer = 0;
6040 gfc_add_expr_to_block (&caf_init_block, tmp);
6041 }
6042 else if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.pointer_comp)
6043 {
6044 tmp = gfc_nullify_alloc_comp (sym->ts.u.derived, decl, sym->as
6045 ? sym->as->rank : 0,
6046 GFC_STRUCTURE_CAF_MODE_IN_COARRAY);
6047 gfc_add_expr_to_block (&caf_init_block, tmp);
6048 }
6049 }
6050
6051 struct caf_accessor
6052 {
6053 struct caf_accessor *next;
6054 gfc_expr *hash, *fdecl;
6055 };
6056
6057 static struct caf_accessor *caf_accessor_head = NULL;
6058
6059 void
6060 gfc_add_caf_accessor (gfc_expr *h, gfc_expr *f)
6061 {
6062 struct caf_accessor *n = XCNEW (struct caf_accessor);
6063 n->next = caf_accessor_head;
6064 n->hash = h;
6065 n->fdecl = f;
6066 caf_accessor_head = n;
6067 }
6068
6069 void
6070 create_caf_accessor_register (stmtblock_t *block)
6071 {
6072 gfc_se se;
6073 tree hash, fdecl;
6074 gfc_init_se (&se, NULL);
6075 for (struct caf_accessor *curr = caf_accessor_head; curr;)
6076 {
6077 gfc_conv_expr (&se, curr->hash);
6078 hash = se.expr;
6079 gfc_conv_expr (&se, curr->fdecl);
6080 fdecl = se.expr;
6081 TREE_USED (fdecl) = 1;
6082 TREE_STATIC (fdecl) = 1;
6083 gcc_assert (FUNCTION_POINTER_TYPE_P (TREE_TYPE (fdecl)));
6084 gfc_add_expr_to_block (
6085 block, build_call_expr (gfor_fndecl_caf_register_accessor, 2, hash,
6086 /*gfc_build_addr_expr (NULL_TREE,*/ fdecl));
6087 curr = curr->next;
6088 free (caf_accessor_head);
6089 caf_accessor_head = curr;
6090 }
6091 gfc_add_expr_to_block (
6092 block, build_call_expr (gfor_fndecl_caf_register_accessors_finish, 0));
6093 }
6094
6095 /* Generate constructor function to initialize static, nonallocatable
6096 coarrays. */
6097
6098 static void
6099 generate_coarray_init (gfc_namespace *ns)
6100 {
6101 tree fndecl, tmp, decl, save_fn_decl;
6102
6103 save_fn_decl = current_function_decl;
6104 push_function_context ();
6105
6106 tmp = build_function_type_list (void_type_node, NULL_TREE);
6107 fndecl = build_decl (input_location, FUNCTION_DECL,
6108 create_tmp_var_name ("_caf_init"), tmp);
6109
6110 DECL_STATIC_CONSTRUCTOR (fndecl) = 1;
6111 SET_DECL_INIT_PRIORITY (fndecl, DEFAULT_INIT_PRIORITY);
6112
6113 decl = build_decl (input_location, RESULT_DECL, NULL_TREE, void_type_node);
6114 DECL_ARTIFICIAL (decl) = 1;
6115 DECL_IGNORED_P (decl) = 1;
6116 DECL_CONTEXT (decl) = fndecl;
6117 DECL_RESULT (fndecl) = decl;
6118
6119 pushdecl (fndecl);
6120 current_function_decl = fndecl;
6121 announce_function (fndecl);
6122
6123 rest_of_decl_compilation (fndecl, 0, 0);
6124 make_decl_rtl (fndecl);
6125 allocate_struct_function (fndecl, false);
6126
6127 pushlevel ();
6128 gfc_init_block (&caf_init_block);
6129
6130 create_caf_accessor_register (&caf_init_block);
6131
6132 gfc_traverse_ns (ns, generate_coarray_sym_init);
6133
6134 DECL_SAVED_TREE (fndecl) = gfc_finish_block (&caf_init_block);
6135 decl = getdecls ();
6136
6137 poplevel (1, 1);
6138 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6139
6140 DECL_SAVED_TREE (fndecl)
6141 = fold_build3_loc (DECL_SOURCE_LOCATION (fndecl), BIND_EXPR, void_type_node,
6142 decl, DECL_SAVED_TREE (fndecl), DECL_INITIAL (fndecl));
6143 dump_function (TDI_original, fndecl);
6144
6145 cfun->function_end_locus = input_location;
6146 set_cfun (NULL);
6147
6148 if (decl_function_context (fndecl))
6149 (void) cgraph_node::create (fndecl);
6150 else
6151 cgraph_node::finalize_function (fndecl, true);
6152
6153 pop_function_context ();
6154 current_function_decl = save_fn_decl;
6155 }
6156
6157
6158 static void
6159 create_module_nml_decl (gfc_symbol *sym)
6160 {
6161 if (sym->attr.flavor == FL_NAMELIST)
6162 {
6163 tree decl = generate_namelist_decl (sym);
6164 pushdecl (decl);
6165 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
6166 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
6167 rest_of_decl_compilation (decl, 1, 0);
6168 gfc_module_add_decl (cur_module, decl);
6169 }
6170 }
6171
6172 static void
6173 gfc_handle_omp_declare_variant (gfc_symbol * sym)
6174 {
6175 if (sym->attr.external
6176 && sym->formal_ns
6177 && sym->formal_ns->omp_declare_variant)
6178 {
6179 gfc_namespace *ns = gfc_current_ns;
6180 gfc_current_ns = sym->ns;
6181 gfc_get_symbol_decl (sym);
6182 gfc_current_ns = ns;
6183 }
6184 }
6185
6186 /* Generate all the required code for module variables. */
6187
6188 void
6189 gfc_generate_module_vars (gfc_namespace * ns)
6190 {
6191 module_namespace = ns;
6192 cur_module = gfc_find_module (ns->proc_name->name);
6193
6194 /* Check if the frontend left the namespace in a reasonable state. */
6195 gcc_assert (ns->proc_name && !ns->proc_name->tlink);
6196
6197 /* Generate COMMON blocks. */
6198 gfc_trans_common (ns);
6199
6200 has_coarray_vars_or_accessors = caf_accessor_head != NULL;
6201
6202 /* Create decls for all the module variables. */
6203 gfc_traverse_ns (ns, gfc_create_module_variable);
6204 gfc_traverse_ns (ns, create_module_nml_decl);
6205
6206 if (flag_coarray == GFC_FCOARRAY_LIB && has_coarray_vars_or_accessors)
6207 generate_coarray_init (ns);
6208
6209 /* For OpenMP, ensure that declare variant in INTERFACE is is processed
6210 especially as some late diagnostic is only done on tree level. */
6211 if (flag_openmp)
6212 gfc_traverse_ns (ns, gfc_handle_omp_declare_variant);
6213
6214 cur_module = NULL;
6215
6216 gfc_trans_use_stmts (ns);
6217 gfc_traverse_ns (ns, gfc_emit_parameter_debug_info);
6218 }
6219
6220
6221 static void
6222 gfc_generate_contained_functions (gfc_namespace * parent)
6223 {
6224 gfc_namespace *ns;
6225
6226 /* We create all the prototypes before generating any code. */
6227 for (ns = parent->contained; ns; ns = ns->sibling)
6228 {
6229 /* Skip namespaces from used modules. */
6230 if (ns->parent != parent)
6231 continue;
6232
6233 gfc_create_function_decl (ns, false);
6234 }
6235
6236 for (ns = parent->contained; ns; ns = ns->sibling)
6237 {
6238 /* Skip namespaces from used modules. */
6239 if (ns->parent != parent)
6240 continue;
6241
6242 gfc_generate_function_code (ns);
6243 }
6244 }
6245
6246
6247 /* Drill down through expressions for the array specification bounds and
6248 character length calling generate_local_decl for all those variables
6249 that have not already been declared. */
6250
6251 static void
6252 generate_local_decl (gfc_symbol *);
6253
6254 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
6255
6256 static bool
6257 expr_decls (gfc_expr *e, gfc_symbol *sym,
6258 int *f ATTRIBUTE_UNUSED)
6259 {
6260 if (e->expr_type != EXPR_VARIABLE
6261 || sym == e->symtree->n.sym
6262 || e->symtree->n.sym->mark
6263 || e->symtree->n.sym->ns != sym->ns)
6264 return false;
6265
6266 generate_local_decl (e->symtree->n.sym);
6267 return false;
6268 }
6269
6270 static void
6271 generate_expr_decls (gfc_symbol *sym, gfc_expr *e)
6272 {
6273 gfc_traverse_expr (e, sym, expr_decls, 0);
6274 }
6275
6276
6277 /* Check for dependencies in the character length and array spec. */
6278
6279 static void
6280 generate_dependency_declarations (gfc_symbol *sym)
6281 {
6282 int i;
6283
6284 if (sym->ts.type == BT_CHARACTER
6285 && sym->ts.u.cl
6286 && sym->ts.u.cl->length
6287 && sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
6288 generate_expr_decls (sym, sym->ts.u.cl->length);
6289
6290 if (sym->as && sym->as->rank)
6291 {
6292 for (i = 0; i < sym->as->rank; i++)
6293 {
6294 generate_expr_decls (sym, sym->as->lower[i]);
6295 generate_expr_decls (sym, sym->as->upper[i]);
6296 }
6297 }
6298 }
6299
6300
6301 /* Generate decls for all local variables. We do this to ensure correct
6302 handling of expressions which only appear in the specification of
6303 other functions. */
6304
6305 static void
6306 generate_local_decl (gfc_symbol * sym)
6307 {
6308 if (sym->attr.flavor == FL_VARIABLE)
6309 {
6310 if (sym->attr.codimension && !sym->attr.dummy && !sym->attr.allocatable
6311 && sym->attr.referenced && !sym->attr.use_assoc)
6312 has_coarray_vars_or_accessors = true;
6313
6314 if (!sym->attr.dummy && !sym->ns->proc_name->attr.entry_master)
6315 generate_dependency_declarations (sym);
6316
6317 if (sym->attr.ext_attr & (1 << EXT_ATTR_WEAK))
6318 {
6319 if (sym->attr.dummy)
6320 gfc_error ("Symbol %qs at %L has the WEAK attribute but is a "
6321 "dummy argument", sym->name, &sym->declared_at);
6322 else
6323 gfc_error ("Symbol %qs at %L has the WEAK attribute but is a "
6324 "local variable", sym->name, &sym->declared_at);
6325 }
6326
6327 if (sym->attr.referenced)
6328 gfc_get_symbol_decl (sym);
6329
6330 /* Warnings for unused dummy arguments. */
6331 else if (sym->attr.dummy && !sym->attr.in_namelist)
6332 {
6333 /* INTENT(out) dummy arguments are likely meant to be set. */
6334 if (warn_unused_dummy_argument && sym->attr.intent == INTENT_OUT)
6335 {
6336 if (sym->ts.type != BT_DERIVED)
6337 gfc_warning (OPT_Wunused_dummy_argument,
6338 "Dummy argument %qs at %L was declared "
6339 "INTENT(OUT) but was not set", sym->name,
6340 &sym->declared_at);
6341 else if (!gfc_has_default_initializer (sym->ts.u.derived)
6342 && !sym->ts.u.derived->attr.zero_comp)
6343 gfc_warning (OPT_Wunused_dummy_argument,
6344 "Derived-type dummy argument %qs at %L was "
6345 "declared INTENT(OUT) but was not set and "
6346 "does not have a default initializer",
6347 sym->name, &sym->declared_at);
6348 if (sym->backend_decl != NULL_TREE)
6349 suppress_warning (sym->backend_decl);
6350 }
6351 else if (warn_unused_dummy_argument)
6352 {
6353 if (!sym->attr.artificial)
6354 gfc_warning (OPT_Wunused_dummy_argument,
6355 "Unused dummy argument %qs at %L", sym->name,
6356 &sym->declared_at);
6357
6358 if (sym->backend_decl != NULL_TREE)
6359 suppress_warning (sym->backend_decl);
6360 }
6361 }
6362
6363 /* Warn for unused variables, but not if they're inside a common
6364 block or a namelist. */
6365 else if (warn_unused_variable
6366 && !(sym->attr.in_common || sym->mark || sym->attr.in_namelist))
6367 {
6368 if (sym->attr.use_only)
6369 {
6370 gfc_warning (OPT_Wunused_variable,
6371 "Unused module variable %qs which has been "
6372 "explicitly imported at %L", sym->name,
6373 &sym->declared_at);
6374 if (sym->backend_decl != NULL_TREE)
6375 suppress_warning (sym->backend_decl);
6376 }
6377 else if (!sym->attr.use_assoc)
6378 {
6379 /* Corner case: the symbol may be an entry point. At this point,
6380 it may appear to be an unused variable. Suppress warning. */
6381 bool enter = false;
6382 gfc_entry_list *el;
6383
6384 for (el = sym->ns->entries; el; el=el->next)
6385 if (strcmp(sym->name, el->sym->name) == 0)
6386 enter = true;
6387
6388 if (!enter)
6389 gfc_warning (OPT_Wunused_variable,
6390 "Unused variable %qs declared at %L",
6391 sym->name, &sym->declared_at);
6392 if (sym->backend_decl != NULL_TREE)
6393 suppress_warning (sym->backend_decl);
6394 }
6395 }
6396
6397 /* For variable length CHARACTER parameters, the PARM_DECL already
6398 references the length variable, so force gfc_get_symbol_decl
6399 even when not referenced. If optimize > 0, it will be optimized
6400 away anyway. But do this only after emitting -Wunused-parameter
6401 warning if requested. */
6402 if (sym->attr.dummy && !sym->attr.referenced
6403 && sym->ts.type == BT_CHARACTER
6404 && sym->ts.u.cl->backend_decl != NULL
6405 && VAR_P (sym->ts.u.cl->backend_decl))
6406 {
6407 sym->attr.referenced = 1;
6408 gfc_get_symbol_decl (sym);
6409 }
6410
6411 /* INTENT(out) dummy arguments and result variables with allocatable
6412 components are reset by default and need to be set referenced to
6413 generate the code for nullification and automatic lengths. */
6414 if (!sym->attr.referenced
6415 && sym->ts.type == BT_DERIVED
6416 && sym->ts.u.derived->attr.alloc_comp
6417 && !sym->attr.pointer
6418 && ((sym->attr.dummy && sym->attr.intent == INTENT_OUT)
6419 ||
6420 (sym->attr.result && sym != sym->result)))
6421 {
6422 sym->attr.referenced = 1;
6423 gfc_get_symbol_decl (sym);
6424 }
6425
6426 /* Check for dependencies in the array specification and string
6427 length, adding the necessary declarations to the function. We
6428 mark the symbol now, as well as in traverse_ns, to prevent
6429 getting stuck in a circular dependency. */
6430 sym->mark = 1;
6431 }
6432 else if (sym->attr.flavor == FL_PARAMETER)
6433 {
6434 if (warn_unused_parameter
6435 && !sym->attr.referenced)
6436 {
6437 if (!sym->attr.use_assoc)
6438 gfc_warning (OPT_Wunused_parameter,
6439 "Unused parameter %qs declared at %L", sym->name,
6440 &sym->declared_at);
6441 else if (sym->attr.use_only)
6442 gfc_warning (OPT_Wunused_parameter,
6443 "Unused parameter %qs which has been explicitly "
6444 "imported at %L", sym->name, &sym->declared_at);
6445 }
6446
6447 if (sym->ns && sym->ns->construct_entities)
6448 {
6449 /* Construction of the intrinsic modules within a BLOCK
6450 construct, where ONLY and RENAMED entities are included,
6451 seems to be bogus. This is a workaround that can be removed
6452 if someone ever takes on the task to creating full-fledge
6453 modules. See PR 69455. */
6454 if (sym->attr.referenced
6455 && sym->from_intmod != INTMOD_ISO_C_BINDING
6456 && sym->from_intmod != INTMOD_ISO_FORTRAN_ENV)
6457 gfc_get_symbol_decl (sym);
6458 sym->mark = 1;
6459 }
6460 }
6461 else if (sym->attr.flavor == FL_PROCEDURE)
6462 {
6463 /* TODO: move to the appropriate place in resolve.cc. */
6464 if (warn_return_type > 0
6465 && sym->attr.function
6466 && sym->result
6467 && sym != sym->result
6468 && !sym->result->attr.referenced
6469 && !sym->attr.use_assoc
6470 && sym->attr.if_source != IFSRC_IFBODY)
6471 {
6472 gfc_warning (OPT_Wreturn_type,
6473 "Return value %qs of function %qs declared at "
6474 "%L not set", sym->result->name, sym->name,
6475 &sym->result->declared_at);
6476
6477 /* Prevents "Unused variable" warning for RESULT variables. */
6478 sym->result->mark = 1;
6479 }
6480 }
6481
6482 if (sym->attr.dummy == 1)
6483 {
6484 /* The tree type for scalar character dummy arguments of BIND(C)
6485 procedures, if they are passed by value, should be unsigned char.
6486 The value attribute implies the dummy is a scalar. */
6487 if (sym->attr.value == 1 && sym->backend_decl != NULL
6488 && sym->ts.type == BT_CHARACTER && sym->ts.is_c_interop
6489 && sym->ns->proc_name != NULL && sym->ns->proc_name->attr.is_bind_c)
6490 {
6491 /* We used to modify the tree here. Now it is done earlier in
6492 the front-end, so we only check it here to avoid regressions. */
6493 gcc_assert (TREE_CODE (TREE_TYPE (sym->backend_decl)) == INTEGER_TYPE);
6494 gcc_assert (TYPE_UNSIGNED (TREE_TYPE (sym->backend_decl)) == 1);
6495 gcc_assert (TYPE_PRECISION (TREE_TYPE (sym->backend_decl)) == CHAR_TYPE_SIZE);
6496 gcc_assert (DECL_BY_REFERENCE (sym->backend_decl) == 0);
6497 }
6498
6499 /* Unused procedure passed as dummy argument. */
6500 if (sym->attr.flavor == FL_PROCEDURE)
6501 {
6502 if (!sym->attr.referenced && !sym->attr.artificial)
6503 {
6504 if (warn_unused_dummy_argument)
6505 gfc_warning (OPT_Wunused_dummy_argument,
6506 "Unused dummy argument %qs at %L", sym->name,
6507 &sym->declared_at);
6508 }
6509
6510 /* Silence bogus "unused parameter" warnings from the
6511 middle end. */
6512 if (sym->backend_decl != NULL_TREE)
6513 suppress_warning (sym->backend_decl);
6514 }
6515 }
6516
6517 /* Make sure we convert the types of the derived types from iso_c_binding
6518 into (void *). */
6519 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
6520 && sym->ts.type == BT_DERIVED)
6521 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
6522 }
6523
6524
6525 static void
6526 generate_local_nml_decl (gfc_symbol * sym)
6527 {
6528 if (sym->attr.flavor == FL_NAMELIST && !sym->attr.use_assoc)
6529 {
6530 tree decl = generate_namelist_decl (sym);
6531 pushdecl (decl);
6532 }
6533 }
6534
6535
6536 static void
6537 generate_local_vars (gfc_namespace * ns)
6538 {
6539 gfc_traverse_ns (ns, generate_local_decl);
6540 gfc_traverse_ns (ns, generate_local_nml_decl);
6541 }
6542
6543
6544 /* Generate a switch statement to jump to the correct entry point. Also
6545 creates the label decls for the entry points. */
6546
6547 static tree
6548 gfc_trans_entry_master_switch (gfc_entry_list * el)
6549 {
6550 stmtblock_t block;
6551 tree label;
6552 tree tmp;
6553 tree val;
6554
6555 gfc_init_block (&block);
6556 for (; el; el = el->next)
6557 {
6558 /* Add the case label. */
6559 label = gfc_build_label_decl (NULL_TREE);
6560 val = build_int_cst (gfc_array_index_type, el->id);
6561 tmp = build_case_label (val, NULL_TREE, label);
6562 gfc_add_expr_to_block (&block, tmp);
6563
6564 /* And jump to the actual entry point. */
6565 label = gfc_build_label_decl (NULL_TREE);
6566 tmp = build1_v (GOTO_EXPR, label);
6567 gfc_add_expr_to_block (&block, tmp);
6568
6569 /* Save the label decl. */
6570 el->label = label;
6571 }
6572 tmp = gfc_finish_block (&block);
6573 /* The first argument selects the entry point. */
6574 val = DECL_ARGUMENTS (current_function_decl);
6575 tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE, val, tmp);
6576 return tmp;
6577 }
6578
6579
6580 /* Add code to string lengths of actual arguments passed to a function against
6581 the expected lengths of the dummy arguments. */
6582
6583 static void
6584 add_argument_checking (stmtblock_t *block, gfc_symbol *sym)
6585 {
6586 gfc_formal_arglist *formal;
6587
6588 for (formal = gfc_sym_get_dummy_args (sym); formal; formal = formal->next)
6589 if (formal->sym && formal->sym->ts.type == BT_CHARACTER
6590 && !formal->sym->ts.deferred)
6591 {
6592 enum tree_code comparison;
6593 tree cond;
6594 tree argname;
6595 gfc_symbol *fsym;
6596 gfc_charlen *cl;
6597 const char *message;
6598
6599 fsym = formal->sym;
6600 cl = fsym->ts.u.cl;
6601
6602 gcc_assert (cl);
6603 gcc_assert (cl->passed_length != NULL_TREE);
6604 gcc_assert (cl->backend_decl != NULL_TREE);
6605
6606 /* For POINTER, ALLOCATABLE and assumed-shape dummy arguments, the
6607 string lengths must match exactly. Otherwise, it is only required
6608 that the actual string length is *at least* the expected one.
6609 Sequence association allows for a mismatch of the string length
6610 if the actual argument is (part of) an array, but only if the
6611 dummy argument is an array. (See "Sequence association" in
6612 Section 12.4.1.4 for F95 and 12.4.1.5 for F2003.) */
6613 if (fsym->attr.pointer || fsym->attr.allocatable
6614 || (fsym->as && (fsym->as->type == AS_ASSUMED_SHAPE
6615 || fsym->as->type == AS_ASSUMED_RANK)))
6616 {
6617 comparison = NE_EXPR;
6618 message = _("Actual string length does not match the declared one"
6619 " for dummy argument '%s' (%ld/%ld)");
6620 }
6621 else if ((fsym->as && fsym->as->rank != 0) || fsym->attr.artificial)
6622 continue;
6623 else
6624 {
6625 comparison = LT_EXPR;
6626 message = _("Actual string length is shorter than the declared one"
6627 " for dummy argument '%s' (%ld/%ld)");
6628 }
6629
6630 /* Build the condition. For optional arguments, an actual length
6631 of 0 is also acceptable if the associated string is NULL, which
6632 means the argument was not passed. */
6633 cond = fold_build2_loc (input_location, comparison, logical_type_node,
6634 cl->passed_length, cl->backend_decl);
6635 if (fsym->attr.optional)
6636 {
6637 tree not_absent;
6638 tree not_0length;
6639 tree absent_failed;
6640
6641 not_0length = fold_build2_loc (input_location, NE_EXPR,
6642 logical_type_node,
6643 cl->passed_length,
6644 build_zero_cst
6645 (TREE_TYPE (cl->passed_length)));
6646 /* The symbol needs to be referenced for gfc_get_symbol_decl. */
6647 fsym->attr.referenced = 1;
6648 not_absent = gfc_conv_expr_present (fsym);
6649
6650 absent_failed = fold_build2_loc (input_location, TRUTH_OR_EXPR,
6651 logical_type_node, not_0length,
6652 not_absent);
6653
6654 cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
6655 logical_type_node, cond, absent_failed);
6656 }
6657
6658 /* Build the runtime check. */
6659 argname = gfc_build_cstring_const (fsym->name);
6660 argname = gfc_build_addr_expr (pchar_type_node, argname);
6661 gfc_trans_runtime_check (true, false, cond, block, &fsym->declared_at,
6662 message, argname,
6663 fold_convert (long_integer_type_node,
6664 cl->passed_length),
6665 fold_convert (long_integer_type_node,
6666 cl->backend_decl));
6667 }
6668 }
6669
6670
6671 static void
6672 create_main_function (tree fndecl)
6673 {
6674 tree old_context;
6675 tree ftn_main;
6676 tree tmp, decl, result_decl, argc, argv, typelist, arglist;
6677 stmtblock_t body;
6678
6679 old_context = current_function_decl;
6680
6681 if (old_context)
6682 {
6683 push_function_context ();
6684 saved_parent_function_decls = saved_function_decls;
6685 saved_function_decls = NULL_TREE;
6686 }
6687
6688 /* main() function must be declared with global scope. */
6689 gcc_assert (current_function_decl == NULL_TREE);
6690
6691 /* Declare the function. */
6692 tmp = build_function_type_list (integer_type_node, integer_type_node,
6693 build_pointer_type (pchar_type_node),
6694 NULL_TREE);
6695 main_identifier_node = get_identifier ("main");
6696 ftn_main = build_decl (input_location, FUNCTION_DECL,
6697 main_identifier_node, tmp);
6698 DECL_EXTERNAL (ftn_main) = 0;
6699 TREE_PUBLIC (ftn_main) = 1;
6700 TREE_STATIC (ftn_main) = 1;
6701 DECL_ATTRIBUTES (ftn_main)
6702 = tree_cons (get_identifier("externally_visible"), NULL_TREE, NULL_TREE);
6703
6704 /* Setup the result declaration (for "return 0"). */
6705 result_decl = build_decl (input_location,
6706 RESULT_DECL, NULL_TREE, integer_type_node);
6707 DECL_ARTIFICIAL (result_decl) = 1;
6708 DECL_IGNORED_P (result_decl) = 1;
6709 DECL_CONTEXT (result_decl) = ftn_main;
6710 DECL_RESULT (ftn_main) = result_decl;
6711
6712 pushdecl (ftn_main);
6713
6714 /* Get the arguments. */
6715
6716 arglist = NULL_TREE;
6717 typelist = TYPE_ARG_TYPES (TREE_TYPE (ftn_main));
6718
6719 tmp = TREE_VALUE (typelist);
6720 argc = build_decl (input_location, PARM_DECL, get_identifier ("argc"), tmp);
6721 DECL_CONTEXT (argc) = ftn_main;
6722 DECL_ARG_TYPE (argc) = TREE_VALUE (typelist);
6723 TREE_READONLY (argc) = 1;
6724 gfc_finish_decl (argc);
6725 arglist = chainon (arglist, argc);
6726
6727 typelist = TREE_CHAIN (typelist);
6728 tmp = TREE_VALUE (typelist);
6729 argv = build_decl (input_location, PARM_DECL, get_identifier ("argv"), tmp);
6730 DECL_CONTEXT (argv) = ftn_main;
6731 DECL_ARG_TYPE (argv) = TREE_VALUE (typelist);
6732 TREE_READONLY (argv) = 1;
6733 DECL_BY_REFERENCE (argv) = 1;
6734 gfc_finish_decl (argv);
6735 arglist = chainon (arglist, argv);
6736
6737 DECL_ARGUMENTS (ftn_main) = arglist;
6738 current_function_decl = ftn_main;
6739 announce_function (ftn_main);
6740
6741 rest_of_decl_compilation (ftn_main, 1, 0);
6742 make_decl_rtl (ftn_main);
6743 allocate_struct_function (ftn_main, false);
6744 pushlevel ();
6745
6746 gfc_init_block (&body);
6747
6748 /* Call some libgfortran initialization routines, call then MAIN__(). */
6749
6750 /* Call _gfortran_caf_init (*argc, ***argv). */
6751 if (flag_coarray == GFC_FCOARRAY_LIB)
6752 {
6753 tree pint_type, pppchar_type;
6754 pint_type = build_pointer_type (integer_type_node);
6755 pppchar_type
6756 = build_pointer_type (build_pointer_type (pchar_type_node));
6757
6758 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_init, 2,
6759 gfc_build_addr_expr (pint_type, argc),
6760 gfc_build_addr_expr (pppchar_type, argv));
6761 gfc_add_expr_to_block (&body, tmp);
6762 }
6763
6764 /* Call _gfortran_set_args (argc, argv). */
6765 TREE_USED (argc) = 1;
6766 TREE_USED (argv) = 1;
6767 tmp = build_call_expr_loc (input_location,
6768 gfor_fndecl_set_args, 2, argc, argv);
6769 gfc_add_expr_to_block (&body, tmp);
6770
6771 /* Add a call to set_options to set up the runtime library Fortran
6772 language standard parameters. */
6773 {
6774 tree array_type, array, var;
6775 vec<constructor_elt, va_gc> *v = NULL;
6776 static const int noptions = 7;
6777
6778 /* Passing a new option to the library requires three modifications:
6779 + add it to the tree_cons list below
6780 + change the noptions variable above
6781 + modify the library (runtime/compile_options.c)! */
6782
6783 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6784 build_int_cst (integer_type_node,
6785 gfc_option.warn_std));
6786 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6787 build_int_cst (integer_type_node,
6788 gfc_option.allow_std));
6789 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6790 build_int_cst (integer_type_node, pedantic));
6791 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6792 build_int_cst (integer_type_node, flag_backtrace));
6793 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6794 build_int_cst (integer_type_node, flag_sign_zero));
6795 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6796 build_int_cst (integer_type_node,
6797 (gfc_option.rtcheck
6798 & GFC_RTCHECK_BOUNDS)));
6799 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6800 build_int_cst (integer_type_node,
6801 gfc_option.fpe_summary));
6802
6803 array_type = build_array_type_nelts (integer_type_node, noptions);
6804 array = build_constructor (array_type, v);
6805 TREE_CONSTANT (array) = 1;
6806 TREE_STATIC (array) = 1;
6807
6808 /* Create a static variable to hold the jump table. */
6809 var = build_decl (input_location, VAR_DECL,
6810 create_tmp_var_name ("options"), array_type);
6811 DECL_ARTIFICIAL (var) = 1;
6812 DECL_IGNORED_P (var) = 1;
6813 TREE_CONSTANT (var) = 1;
6814 TREE_STATIC (var) = 1;
6815 TREE_READONLY (var) = 1;
6816 DECL_INITIAL (var) = array;
6817 pushdecl (var);
6818 var = gfc_build_addr_expr (build_pointer_type (integer_type_node), var);
6819
6820 tmp = build_call_expr_loc (input_location,
6821 gfor_fndecl_set_options, 2,
6822 build_int_cst (integer_type_node, noptions), var);
6823 gfc_add_expr_to_block (&body, tmp);
6824 }
6825
6826 /* If -ffpe-trap option was provided, add a call to set_fpe so that
6827 the library will raise a FPE when needed. */
6828 if (gfc_option.fpe != 0)
6829 {
6830 tmp = build_call_expr_loc (input_location,
6831 gfor_fndecl_set_fpe, 1,
6832 build_int_cst (integer_type_node,
6833 gfc_option.fpe));
6834 gfc_add_expr_to_block (&body, tmp);
6835 }
6836
6837 /* If this is the main program and an -fconvert option was provided,
6838 add a call to set_convert. */
6839
6840 if (flag_convert != GFC_FLAG_CONVERT_NATIVE)
6841 {
6842 tmp = build_call_expr_loc (input_location,
6843 gfor_fndecl_set_convert, 1,
6844 build_int_cst (integer_type_node, flag_convert));
6845 gfc_add_expr_to_block (&body, tmp);
6846 }
6847
6848 /* If this is the main program and an -frecord-marker option was provided,
6849 add a call to set_record_marker. */
6850
6851 if (flag_record_marker != 0)
6852 {
6853 tmp = build_call_expr_loc (input_location,
6854 gfor_fndecl_set_record_marker, 1,
6855 build_int_cst (integer_type_node,
6856 flag_record_marker));
6857 gfc_add_expr_to_block (&body, tmp);
6858 }
6859
6860 if (flag_max_subrecord_length != 0)
6861 {
6862 tmp = build_call_expr_loc (input_location,
6863 gfor_fndecl_set_max_subrecord_length, 1,
6864 build_int_cst (integer_type_node,
6865 flag_max_subrecord_length));
6866 gfc_add_expr_to_block (&body, tmp);
6867 }
6868
6869 /* Call MAIN__(). */
6870 tmp = build_call_expr_loc (input_location,
6871 fndecl, 0);
6872 gfc_add_expr_to_block (&body, tmp);
6873
6874 /* Mark MAIN__ as used. */
6875 TREE_USED (fndecl) = 1;
6876
6877 /* Coarray: Call _gfortran_caf_finalize(void). */
6878 if (flag_coarray == GFC_FCOARRAY_LIB)
6879 {
6880 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_finalize, 0);
6881 gfc_add_expr_to_block (&body, tmp);
6882 }
6883
6884 /* "return 0". */
6885 tmp = fold_build2_loc (input_location, MODIFY_EXPR, integer_type_node,
6886 DECL_RESULT (ftn_main),
6887 integer_zero_node);
6888 tmp = build1_v (RETURN_EXPR, tmp);
6889 gfc_add_expr_to_block (&body, tmp);
6890
6891
6892 DECL_SAVED_TREE (ftn_main) = gfc_finish_block (&body);
6893 decl = getdecls ();
6894
6895 /* Finish off this function and send it for code generation. */
6896 poplevel (1, 1);
6897 BLOCK_SUPERCONTEXT (DECL_INITIAL (ftn_main)) = ftn_main;
6898
6899 DECL_SAVED_TREE (ftn_main)
6900 = fold_build3_loc (DECL_SOURCE_LOCATION (ftn_main), BIND_EXPR,
6901 void_type_node, decl, DECL_SAVED_TREE (ftn_main),
6902 DECL_INITIAL (ftn_main));
6903
6904 /* Output the GENERIC tree. */
6905 dump_function (TDI_original, ftn_main);
6906
6907 cgraph_node::finalize_function (ftn_main, true);
6908
6909 if (old_context)
6910 {
6911 pop_function_context ();
6912 saved_function_decls = saved_parent_function_decls;
6913 }
6914 current_function_decl = old_context;
6915 }
6916
6917
6918 /* Generate an appropriate return-statement for a procedure. */
6919
6920 tree
6921 gfc_generate_return (void)
6922 {
6923 gfc_symbol* sym;
6924 tree result;
6925 tree fndecl;
6926
6927 sym = current_procedure_symbol;
6928 fndecl = sym->backend_decl;
6929
6930 if (TREE_TYPE (DECL_RESULT (fndecl)) == void_type_node)
6931 result = NULL_TREE;
6932 else
6933 {
6934 result = get_proc_result (sym);
6935
6936 /* Set the return value to the dummy result variable. The
6937 types may be different for scalar default REAL functions
6938 with -ff2c, therefore we have to convert. */
6939 if (result != NULL_TREE)
6940 {
6941 result = convert (TREE_TYPE (DECL_RESULT (fndecl)), result);
6942 result = fold_build2_loc (input_location, MODIFY_EXPR,
6943 TREE_TYPE (result), DECL_RESULT (fndecl),
6944 result);
6945 }
6946 else
6947 {
6948 /* If the function does not have a result variable, result is
6949 NULL_TREE, and a 'return' is generated without a variable.
6950 The following generates a 'return __result_XXX' where XXX is
6951 the function name. */
6952 if (sym == sym->result && sym->attr.function && !flag_f2c)
6953 {
6954 result = gfc_get_fake_result_decl (sym, 0);
6955 result = fold_build2_loc (input_location, MODIFY_EXPR,
6956 TREE_TYPE (result),
6957 DECL_RESULT (fndecl), result);
6958 }
6959 }
6960 }
6961
6962 return build1_v (RETURN_EXPR, result);
6963 }
6964
6965
6966 static void
6967 is_from_ieee_module (gfc_symbol *sym)
6968 {
6969 if (sym->from_intmod == INTMOD_IEEE_FEATURES
6970 || sym->from_intmod == INTMOD_IEEE_EXCEPTIONS
6971 || sym->from_intmod == INTMOD_IEEE_ARITHMETIC)
6972 seen_ieee_symbol = 1;
6973 }
6974
6975
6976 static int
6977 is_ieee_module_used (gfc_namespace *ns)
6978 {
6979 seen_ieee_symbol = 0;
6980 gfc_traverse_ns (ns, is_from_ieee_module);
6981 return seen_ieee_symbol;
6982 }
6983
6984
6985 static gfc_omp_clauses *module_oacc_clauses;
6986
6987
6988 static void
6989 add_clause (gfc_symbol *sym, gfc_omp_map_op map_op)
6990 {
6991 gfc_omp_namelist *n;
6992
6993 n = gfc_get_omp_namelist ();
6994 n->sym = sym;
6995 n->where = sym->declared_at;
6996 n->u.map.op = map_op;
6997
6998 if (!module_oacc_clauses)
6999 module_oacc_clauses = gfc_get_omp_clauses ();
7000
7001 if (module_oacc_clauses->lists[OMP_LIST_MAP])
7002 n->next = module_oacc_clauses->lists[OMP_LIST_MAP];
7003
7004 module_oacc_clauses->lists[OMP_LIST_MAP] = n;
7005 }
7006
7007
7008 static void
7009 find_module_oacc_declare_clauses (gfc_symbol *sym)
7010 {
7011 if (sym->attr.use_assoc)
7012 {
7013 gfc_omp_map_op map_op;
7014
7015 if (sym->attr.oacc_declare_create)
7016 map_op = OMP_MAP_FORCE_ALLOC;
7017
7018 if (sym->attr.oacc_declare_copyin)
7019 map_op = OMP_MAP_FORCE_TO;
7020
7021 if (sym->attr.oacc_declare_deviceptr)
7022 map_op = OMP_MAP_FORCE_DEVICEPTR;
7023
7024 if (sym->attr.oacc_declare_device_resident)
7025 map_op = OMP_MAP_DEVICE_RESIDENT;
7026
7027 if (sym->attr.oacc_declare_create
7028 || sym->attr.oacc_declare_copyin
7029 || sym->attr.oacc_declare_deviceptr
7030 || sym->attr.oacc_declare_device_resident)
7031 {
7032 sym->attr.referenced = 1;
7033 add_clause (sym, map_op);
7034 }
7035 }
7036 }
7037
7038
7039 void
7040 finish_oacc_declare (gfc_namespace *ns, gfc_symbol *sym, bool block)
7041 {
7042 gfc_code *code;
7043 gfc_oacc_declare *oc;
7044 locus where;
7045 gfc_omp_clauses *omp_clauses = NULL;
7046 gfc_omp_namelist *n, *p;
7047 module_oacc_clauses = NULL;
7048
7049 gfc_locus_from_location (&where, input_location);
7050 gfc_traverse_ns (ns, find_module_oacc_declare_clauses);
7051
7052 if (module_oacc_clauses && sym->attr.flavor == FL_PROGRAM)
7053 {
7054 gfc_oacc_declare *new_oc;
7055
7056 new_oc = gfc_get_oacc_declare ();
7057 new_oc->next = ns->oacc_declare;
7058 new_oc->clauses = module_oacc_clauses;
7059
7060 ns->oacc_declare = new_oc;
7061 }
7062
7063 if (!ns->oacc_declare)
7064 return;
7065
7066 for (oc = ns->oacc_declare; oc; oc = oc->next)
7067 {
7068 if (oc->module_var)
7069 continue;
7070
7071 if (block)
7072 gfc_error ("Sorry, !$ACC DECLARE at %L is not allowed "
7073 "in BLOCK construct", &oc->loc);
7074
7075
7076 if (oc->clauses && oc->clauses->lists[OMP_LIST_MAP])
7077 {
7078 if (omp_clauses == NULL)
7079 {
7080 omp_clauses = oc->clauses;
7081 continue;
7082 }
7083
7084 for (n = oc->clauses->lists[OMP_LIST_MAP]; n; p = n, n = n->next)
7085 ;
7086
7087 gcc_assert (p->next == NULL);
7088
7089 p->next = omp_clauses->lists[OMP_LIST_MAP];
7090 omp_clauses = oc->clauses;
7091 }
7092 }
7093
7094 if (!omp_clauses)
7095 return;
7096
7097 for (n = omp_clauses->lists[OMP_LIST_MAP]; n; n = n->next)
7098 {
7099 switch (n->u.map.op)
7100 {
7101 case OMP_MAP_DEVICE_RESIDENT:
7102 n->u.map.op = OMP_MAP_FORCE_ALLOC;
7103 break;
7104
7105 default:
7106 break;
7107 }
7108 }
7109
7110 code = XCNEW (gfc_code);
7111 code->op = EXEC_OACC_DECLARE;
7112 code->loc = where;
7113
7114 code->ext.oacc_declare = gfc_get_oacc_declare ();
7115 code->ext.oacc_declare->clauses = omp_clauses;
7116
7117 code->block = XCNEW (gfc_code);
7118 code->block->op = EXEC_OACC_DECLARE;
7119 code->block->loc = where;
7120
7121 if (ns->code)
7122 code->block->next = ns->code;
7123
7124 ns->code = code;
7125
7126 return;
7127 }
7128
7129 static void
7130 gfc_conv_cfi_to_gfc (stmtblock_t *init, stmtblock_t *finally,
7131 tree cfi_desc, tree gfc_desc, gfc_symbol *sym)
7132 {
7133 stmtblock_t block;
7134 gfc_init_block (&block);
7135 tree cfi = build_fold_indirect_ref_loc (input_location, cfi_desc);
7136 tree idx, etype, tmp, tmp2, size_var = NULL_TREE, rank = NULL_TREE;
7137 bool do_copy_inout = false;
7138
7139 /* When allocatable + intent out, free the cfi descriptor. */
7140 if (sym->attr.allocatable && sym->attr.intent == INTENT_OUT)
7141 {
7142 tmp = gfc_get_cfi_desc_base_addr (cfi);
7143 tree call = builtin_decl_explicit (BUILT_IN_FREE);
7144 call = build_call_expr_loc (input_location, call, 1, tmp);
7145 gfc_add_expr_to_block (&block, fold_convert (void_type_node, call));
7146 gfc_add_modify (&block, tmp,
7147 fold_convert (TREE_TYPE (tmp), null_pointer_node));
7148 }
7149
7150 /* -fcheck=bound: Do version, rank, attribute, type and is-NULL checks. */
7151 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
7152 {
7153 char *msg;
7154 tree tmp3;
7155 msg = xasprintf ("Unexpected version %%d (expected %d) in CFI descriptor "
7156 "passed to dummy argument %s", CFI_VERSION, sym->name);
7157 tmp2 = gfc_get_cfi_desc_version (cfi);
7158 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, tmp2,
7159 build_int_cst (TREE_TYPE (tmp2), CFI_VERSION));
7160 gfc_trans_runtime_check (true, false, tmp, &block, &sym->declared_at,
7161 msg, tmp2);
7162 free (msg);
7163
7164 /* Rank check; however, for character(len=*), assumed/explicit-size arrays
7165 are permitted to differ in rank according to the Fortran rules. */
7166 if (sym->as && sym->as->type != AS_ASSUMED_SIZE
7167 && sym->as->type != AS_EXPLICIT)
7168 {
7169 if (sym->as->rank != -1)
7170 msg = xasprintf ("Invalid rank %%d (expected %d) in CFI descriptor "
7171 "passed to dummy argument %s", sym->as->rank,
7172 sym->name);
7173 else
7174 msg = xasprintf ("Invalid rank %%d (expected 0..%d) in CFI "
7175 "descriptor passed to dummy argument %s",
7176 CFI_MAX_RANK, sym->name);
7177
7178 tmp3 = tmp2 = tmp = gfc_get_cfi_desc_rank (cfi);
7179 if (sym->as->rank != -1)
7180 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7181 tmp, build_int_cst (signed_char_type_node,
7182 sym->as->rank));
7183 else
7184 {
7185 tmp = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
7186 tmp, build_zero_cst (TREE_TYPE (tmp)));
7187 tmp2 = fold_build2_loc (input_location, GT_EXPR,
7188 boolean_type_node, tmp2,
7189 build_int_cst (TREE_TYPE (tmp2),
7190 CFI_MAX_RANK));
7191 tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR,
7192 boolean_type_node, tmp, tmp2);
7193 }
7194 gfc_trans_runtime_check (true, false, tmp, &block, &sym->declared_at,
7195 msg, tmp3);
7196 free (msg);
7197 }
7198
7199 tmp3 = tmp = gfc_get_cfi_desc_attribute (cfi);
7200 if (sym->attr.allocatable || sym->attr.pointer)
7201 {
7202 int attr = (sym->attr.pointer ? CFI_attribute_pointer
7203 : CFI_attribute_allocatable);
7204 msg = xasprintf ("Invalid attribute %%d (expected %d) in CFI "
7205 "descriptor passed to dummy argument %s with %s "
7206 "attribute", attr, sym->name,
7207 sym->attr.pointer ? "pointer" : "allocatable");
7208 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7209 tmp, build_int_cst (TREE_TYPE (tmp), attr));
7210 }
7211 else
7212 {
7213 int amin = MIN (CFI_attribute_pointer,
7214 MIN (CFI_attribute_allocatable, CFI_attribute_other));
7215 int amax = MAX (CFI_attribute_pointer,
7216 MAX (CFI_attribute_allocatable, CFI_attribute_other));
7217 msg = xasprintf ("Invalid attribute %%d (expected %d..%d) in CFI "
7218 "descriptor passed to nonallocatable, nonpointer "
7219 "dummy argument %s", amin, amax, sym->name);
7220 tmp2 = tmp;
7221 tmp = fold_build2_loc (input_location, LT_EXPR, boolean_type_node, tmp,
7222 build_int_cst (TREE_TYPE (tmp), amin));
7223 tmp2 = fold_build2_loc (input_location, GT_EXPR, boolean_type_node, tmp2,
7224 build_int_cst (TREE_TYPE (tmp2), amax));
7225 tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR,
7226 boolean_type_node, tmp, tmp2);
7227 gfc_trans_runtime_check (true, false, tmp, &block, &sym->declared_at,
7228 msg, tmp3);
7229 free (msg);
7230 msg = xasprintf ("Invalid unallocatated/unassociated CFI "
7231 "descriptor passed to nonallocatable, nonpointer "
7232 "dummy argument %s", sym->name);
7233 tmp3 = tmp = gfc_get_cfi_desc_base_addr (cfi),
7234 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
7235 tmp, null_pointer_node);
7236 }
7237 gfc_trans_runtime_check (true, false, tmp, &block, &sym->declared_at,
7238 msg, tmp3);
7239 free (msg);
7240
7241 if (sym->ts.type != BT_ASSUMED)
7242 {
7243 int type = CFI_type_other;
7244 if (sym->ts.f90_type == BT_VOID)
7245 {
7246 type = (sym->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
7247 ? CFI_type_cfunptr : CFI_type_cptr);
7248 }
7249 else
7250 switch (sym->ts.type)
7251 {
7252 case BT_INTEGER:
7253 case BT_LOGICAL:
7254 case BT_REAL:
7255 case BT_COMPLEX:
7256 type = CFI_type_from_type_kind (sym->ts.type, sym->ts.kind);
7257 break;
7258 case BT_CHARACTER:
7259 type = CFI_type_from_type_kind (CFI_type_Character,
7260 sym->ts.kind);
7261 break;
7262 case BT_DERIVED:
7263 type = CFI_type_struct;
7264 break;
7265 case BT_VOID:
7266 type = (sym->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
7267 ? CFI_type_cfunptr : CFI_type_cptr);
7268 break;
7269
7270 case BT_UNSIGNED:
7271 gfc_internal_error ("Unsigned not yet implemented");
7272
7273 case BT_ASSUMED:
7274 case BT_CLASS:
7275 case BT_PROCEDURE:
7276 case BT_HOLLERITH:
7277 case BT_UNION:
7278 case BT_BOZ:
7279 case BT_UNKNOWN:
7280 gcc_unreachable ();
7281 }
7282 msg = xasprintf ("Unexpected type %%d (expected %d) in CFI descriptor"
7283 " passed to dummy argument %s", type, sym->name);
7284 tmp2 = tmp = gfc_get_cfi_desc_type (cfi);
7285 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7286 tmp, build_int_cst (TREE_TYPE (tmp), type));
7287 gfc_trans_runtime_check (true, false, tmp, &block, &sym->declared_at,
7288 msg, tmp2);
7289 free (msg);
7290 }
7291 }
7292
7293 if (!sym->attr.referenced)
7294 goto done;
7295
7296 /* Set string length for len=* and len=:, otherwise, it is already set. */
7297 if (sym->ts.type == BT_CHARACTER && !sym->ts.u.cl->length)
7298 {
7299 tmp = fold_convert (gfc_array_index_type,
7300 gfc_get_cfi_desc_elem_len (cfi));
7301 if (sym->ts.kind != 1)
7302 tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
7303 gfc_array_index_type, tmp,
7304 build_int_cst (gfc_charlen_type_node,
7305 sym->ts.kind));
7306 gfc_add_modify (&block, sym->ts.u.cl->backend_decl, tmp);
7307 }
7308
7309 if (sym->ts.type == BT_CHARACTER
7310 && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
7311 {
7312 gfc_conv_string_length (sym->ts.u.cl, NULL, &block);
7313 gfc_trans_vla_type_sizes (sym, &block);
7314 }
7315
7316 /* gfc->data = cfi->base_addr - or for scalars: gfc = cfi->base_addr.
7317 assumed-size/explicit-size arrays end up here for character(len=*)
7318 only. */
7319 if (!sym->attr.dimension || !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
7320 {
7321 tmp = gfc_get_cfi_desc_base_addr (cfi);
7322 gfc_add_modify (&block, gfc_desc,
7323 fold_convert (TREE_TYPE (gfc_desc), tmp));
7324 if (!sym->attr.dimension)
7325 goto done;
7326 }
7327
7328 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
7329 {
7330 /* gfc->dtype = ... (from declaration, not from cfi). */
7331 etype = gfc_get_element_type (TREE_TYPE (gfc_desc));
7332 gfc_add_modify (&block, gfc_conv_descriptor_dtype (gfc_desc),
7333 gfc_get_dtype_rank_type (sym->as->rank, etype));
7334 /* gfc->data = cfi->base_addr. */
7335 gfc_conv_descriptor_data_set (&block, gfc_desc,
7336 gfc_get_cfi_desc_base_addr (cfi));
7337 }
7338
7339 if (sym->ts.type == BT_ASSUMED)
7340 {
7341 /* For type(*), take elem_len + dtype.type from the actual argument. */
7342 gfc_add_modify (&block, gfc_conv_descriptor_elem_len (gfc_desc),
7343 gfc_get_cfi_desc_elem_len (cfi));
7344 tree cond;
7345 tree ctype = gfc_get_cfi_desc_type (cfi);
7346 ctype = fold_build2_loc (input_location, BIT_AND_EXPR, TREE_TYPE (ctype),
7347 ctype, build_int_cst (TREE_TYPE (ctype),
7348 CFI_type_mask));
7349 tree type = gfc_conv_descriptor_type (gfc_desc);
7350
7351 /* if (CFI_type_cptr) BT_VOID else BT_UNKNOWN */
7352 /* Note: BT_VOID is could also be CFI_type_funcptr, but assume c_ptr. */
7353 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
7354 build_int_cst (TREE_TYPE (ctype), CFI_type_cptr));
7355 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, type,
7356 build_int_cst (TREE_TYPE (type), BT_VOID));
7357 tmp2 = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
7358 type,
7359 build_int_cst (TREE_TYPE (type), BT_UNKNOWN));
7360 tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
7361 tmp, tmp2);
7362 /* if (CFI_type_struct) BT_DERIVED else < tmp2 > */
7363 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
7364 build_int_cst (TREE_TYPE (ctype),
7365 CFI_type_struct));
7366 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, type,
7367 build_int_cst (TREE_TYPE (type), BT_DERIVED));
7368 tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
7369 tmp, tmp2);
7370 /* if (CFI_type_Character) BT_CHARACTER else < tmp2 > */
7371 /* Note: this is kind=1, CFI_type_ucs4_char is handled in the 'else if'
7372 before (see below, as generated bottom up). */
7373 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
7374 build_int_cst (TREE_TYPE (ctype),
7375 CFI_type_Character));
7376 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, type,
7377 build_int_cst (TREE_TYPE (type), BT_CHARACTER));
7378 tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
7379 tmp, tmp2);
7380 /* if (CFI_type_ucs4_char) BT_CHARACTER else < tmp2 > */
7381 /* Note: gfc->elem_len = cfi->elem_len/4. */
7382 /* However, assuming that CFI_type_ucs4_char cannot be recovered, leave
7383 gfc->elem_len == cfi->elem_len, which helps with operations which use
7384 sizeof() in Fortran and cfi->elem_len in C. */
7385 tmp = gfc_get_cfi_desc_type (cfi);
7386 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, tmp,
7387 build_int_cst (TREE_TYPE (tmp),
7388 CFI_type_ucs4_char));
7389 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, type,
7390 build_int_cst (TREE_TYPE (type), BT_CHARACTER));
7391 tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
7392 tmp, tmp2);
7393 /* if (CFI_type_Complex) BT_COMPLEX + cfi->elem_len/2 else < tmp2 > */
7394 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
7395 build_int_cst (TREE_TYPE (ctype),
7396 CFI_type_Complex));
7397 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, type,
7398 build_int_cst (TREE_TYPE (type), BT_COMPLEX));
7399 tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
7400 tmp, tmp2);
7401 /* if (CFI_type_Integer || CFI_type_Logical || CFI_type_Real)
7402 ctype else <tmp2> */
7403 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
7404 build_int_cst (TREE_TYPE (ctype),
7405 CFI_type_Integer));
7406 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
7407 build_int_cst (TREE_TYPE (ctype),
7408 CFI_type_Logical));
7409 cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
7410 cond, tmp);
7411 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
7412 build_int_cst (TREE_TYPE (ctype),
7413 CFI_type_Real));
7414 cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
7415 cond, tmp);
7416 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
7417 type, fold_convert (TREE_TYPE (type), ctype));
7418 tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
7419 tmp, tmp2);
7420 gfc_add_expr_to_block (&block, tmp2);
7421 }
7422
7423 if (sym->as->rank < 0)
7424 {
7425 /* Set gfc->dtype.rank, if assumed-rank. */
7426 rank = gfc_get_cfi_desc_rank (cfi);
7427 gfc_add_modify (&block, gfc_conv_descriptor_rank (gfc_desc), rank);
7428 }
7429 else if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
7430 /* In that case, the CFI rank and the declared rank can differ. */
7431 rank = gfc_get_cfi_desc_rank (cfi);
7432 else
7433 rank = build_int_cst (signed_char_type_node, sym->as->rank);
7434
7435 /* With bind(C), the standard requires that both Fortran callers and callees
7436 handle noncontiguous arrays passed to an dummy with 'contiguous' attribute
7437 and with character(len=*) + assumed-size/explicit-size arrays.
7438 cf. Fortran 2018, 18.3.6, paragraph 5 (and for the caller: para. 6). */
7439 if ((sym->ts.type == BT_CHARACTER && !sym->ts.u.cl->length
7440 && (sym->as->type == AS_ASSUMED_SIZE || sym->as->type == AS_EXPLICIT))
7441 || sym->attr.contiguous)
7442 {
7443 do_copy_inout = true;
7444 gcc_assert (!sym->attr.pointer);
7445 stmtblock_t block2;
7446 tree data;
7447 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
7448 data = gfc_conv_descriptor_data_get (gfc_desc);
7449 else if (!POINTER_TYPE_P (TREE_TYPE (gfc_desc)))
7450 data = gfc_build_addr_expr (NULL, gfc_desc);
7451 else
7452 data = gfc_desc;
7453
7454 /* Is copy-in/out needed? */
7455 /* do_copyin = rank != 0 && !assumed-size */
7456 tree cond_var = gfc_create_var (boolean_type_node, "do_copyin");
7457 tree cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7458 rank, build_zero_cst (TREE_TYPE (rank)));
7459 /* dim[rank-1].extent != -1 -> assumed size*/
7460 tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (rank),
7461 rank, build_int_cst (TREE_TYPE (rank), 1));
7462 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7463 gfc_get_cfi_dim_extent (cfi, tmp),
7464 build_int_cst (gfc_array_index_type, -1));
7465 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
7466 boolean_type_node, cond, tmp);
7467 gfc_add_modify (&block, cond_var, cond);
7468 /* if (do_copyin) do_copyin = ... || ... || ... */
7469 gfc_init_block (&block2);
7470 /* dim[0].sm != elem_len */
7471 tmp = fold_convert (gfc_array_index_type,
7472 gfc_get_cfi_desc_elem_len (cfi));
7473 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7474 gfc_get_cfi_dim_sm (cfi, gfc_index_zero_node),
7475 tmp);
7476 gfc_add_modify (&block2, cond_var, cond);
7477
7478 /* for (i = 1; i < rank; ++i)
7479 cond &&= dim[i].sm != (dv->dim[i - 1].sm * dv->dim[i - 1].extent) */
7480 idx = gfc_create_var (TREE_TYPE (rank), "idx");
7481 stmtblock_t loop_body;
7482 gfc_init_block (&loop_body);
7483 tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (idx),
7484 idx, build_int_cst (TREE_TYPE (idx), 1));
7485 tree tmp2 = gfc_get_cfi_dim_sm (cfi, tmp);
7486 tmp = gfc_get_cfi_dim_extent (cfi, tmp);
7487 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
7488 tmp2, tmp);
7489 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7490 gfc_get_cfi_dim_sm (cfi, idx), tmp);
7491 cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
7492 cond_var, cond);
7493 gfc_add_modify (&loop_body, cond_var, cond);
7494 gfc_simple_for_loop (&block2, idx, build_int_cst (TREE_TYPE (idx), 1),
7495 rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
7496 gfc_finish_block (&loop_body));
7497 tmp = build3_v (COND_EXPR, cond_var, gfc_finish_block (&block2),
7498 build_empty_stmt (input_location));
7499 gfc_add_expr_to_block (&block, tmp);
7500
7501 /* Copy-in body. */
7502 gfc_init_block (&block2);
7503 /* size = dim[0].extent; for (i = 1; i < rank; ++i) size *= dim[i].extent */
7504 size_var = gfc_create_var (size_type_node, "size");
7505 tmp = fold_convert (size_type_node,
7506 gfc_get_cfi_dim_extent (cfi, gfc_index_zero_node));
7507 gfc_add_modify (&block2, size_var, tmp);
7508
7509 gfc_init_block (&loop_body);
7510 tmp = fold_convert (size_type_node,
7511 gfc_get_cfi_dim_extent (cfi, idx));
7512 tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
7513 size_var, fold_convert (size_type_node, tmp));
7514 gfc_add_modify (&loop_body, size_var, tmp);
7515 gfc_simple_for_loop (&block2, idx, build_int_cst (TREE_TYPE (idx), 1),
7516 rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
7517 gfc_finish_block (&loop_body));
7518 /* data = malloc (size * elem_len) */
7519 tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
7520 size_var, gfc_get_cfi_desc_elem_len (cfi));
7521 tree call = builtin_decl_explicit (BUILT_IN_MALLOC);
7522 call = build_call_expr_loc (input_location, call, 1, tmp);
7523 gfc_add_modify (&block2, data, fold_convert (TREE_TYPE (data), call));
7524
7525 /* Copy the data:
7526 for (idx = 0; idx < size; ++idx)
7527 {
7528 shift = 0;
7529 tmpidx = idx
7530 for (dim = 0; dim < rank; ++dim)
7531 {
7532 shift += (tmpidx % extent[d]) * sm[d]
7533 tmpidx = tmpidx / extend[d]
7534 }
7535 memcpy (lhs + idx*elem_len, rhs + shift, elem_len)
7536 } .*/
7537 idx = gfc_create_var (size_type_node, "arrayidx");
7538 gfc_init_block (&loop_body);
7539 tree shift = gfc_create_var (size_type_node, "shift");
7540 tree tmpidx = gfc_create_var (size_type_node, "tmpidx");
7541 gfc_add_modify (&loop_body, shift, build_zero_cst (TREE_TYPE (shift)));
7542 gfc_add_modify (&loop_body, tmpidx, idx);
7543 stmtblock_t inner_loop;
7544 gfc_init_block (&inner_loop);
7545 tree dim = gfc_create_var (TREE_TYPE (rank), "dim");
7546 /* shift += (tmpidx % extent[d]) * sm[d] */
7547 tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR,
7548 size_type_node, tmpidx,
7549 fold_convert (size_type_node,
7550 gfc_get_cfi_dim_extent (cfi, dim)));
7551 tmp = fold_build2_loc (input_location, MULT_EXPR,
7552 size_type_node, tmp,
7553 fold_convert (size_type_node,
7554 gfc_get_cfi_dim_sm (cfi, dim)));
7555 gfc_add_modify (&inner_loop, shift,
7556 fold_build2_loc (input_location, PLUS_EXPR,
7557 size_type_node, shift, tmp));
7558 /* tmpidx = tmpidx / extend[d] */
7559 tmp = fold_convert (size_type_node, gfc_get_cfi_dim_extent (cfi, dim));
7560 gfc_add_modify (&inner_loop, tmpidx,
7561 fold_build2_loc (input_location, TRUNC_DIV_EXPR,
7562 size_type_node, tmpidx, tmp));
7563 gfc_simple_for_loop (&loop_body, dim, build_zero_cst (TREE_TYPE (rank)),
7564 rank, LT_EXPR, build_int_cst (TREE_TYPE (dim), 1),
7565 gfc_finish_block (&inner_loop));
7566 /* Assign. */
7567 tmp = fold_convert (pchar_type_node, gfc_get_cfi_desc_base_addr (cfi));
7568 tmp = fold_build2 (POINTER_PLUS_EXPR, pchar_type_node, tmp, shift);
7569 tree lhs;
7570 /* memcpy (lhs + idx*elem_len, rhs + shift, elem_len) */
7571 tree elem_len;
7572 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
7573 elem_len = gfc_conv_descriptor_elem_len (gfc_desc);
7574 else
7575 elem_len = gfc_get_cfi_desc_elem_len (cfi);
7576 lhs = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
7577 elem_len, idx);
7578 lhs = fold_build2_loc (input_location, POINTER_PLUS_EXPR, pchar_type_node,
7579 fold_convert (pchar_type_node, data), lhs);
7580 tmp = fold_convert (pvoid_type_node, tmp);
7581 lhs = fold_convert (pvoid_type_node, lhs);
7582 call = builtin_decl_explicit (BUILT_IN_MEMCPY);
7583 call = build_call_expr_loc (input_location, call, 3, lhs, tmp, elem_len);
7584 gfc_add_expr_to_block (&loop_body, fold_convert (void_type_node, call));
7585 gfc_simple_for_loop (&block2, idx, build_zero_cst (TREE_TYPE (idx)),
7586 size_var, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
7587 gfc_finish_block (&loop_body));
7588 /* if (cond) { block2 } */
7589 tmp = build3_v (COND_EXPR, cond_var, gfc_finish_block (&block2),
7590 build_empty_stmt (input_location));
7591 gfc_add_expr_to_block (&block, tmp);
7592 }
7593
7594 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
7595 {
7596 tree offset, type;
7597 type = TREE_TYPE (gfc_desc);
7598 gfc_trans_array_bounds (type, sym, &offset, &block);
7599 if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
7600 gfc_add_modify (&block, GFC_TYPE_ARRAY_OFFSET (type), offset);
7601 goto done;
7602 }
7603
7604 /* If cfi->data != NULL. */
7605 stmtblock_t block2;
7606 gfc_init_block (&block2);
7607
7608 /* if do_copy_inout: gfc->dspan = gfc->dtype.elem_len
7609 We use gfc instead of cfi on the RHS as this might be a constant. */
7610 tmp = fold_convert (gfc_array_index_type,
7611 gfc_conv_descriptor_elem_len (gfc_desc));
7612 if (!do_copy_inout)
7613 {
7614 /* gfc->dspan = ((cfi->dim[0].sm % gfc->elem_len)
7615 ? cfi->dim[0].sm : gfc->elem_len). */
7616 tree cond;
7617 tree tmp2 = gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]);
7618 cond = fold_build2_loc (input_location, TRUNC_MOD_EXPR,
7619 gfc_array_index_type, tmp2, tmp);
7620 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7621 cond, gfc_index_zero_node);
7622 tmp = build3_loc (input_location, COND_EXPR, gfc_array_index_type, cond,
7623 tmp2, tmp);
7624 }
7625 gfc_conv_descriptor_span_set (&block2, gfc_desc, tmp);
7626
7627 /* Calculate offset + set lbound, ubound and stride. */
7628 gfc_conv_descriptor_offset_set (&block2, gfc_desc, gfc_index_zero_node);
7629 if (sym->as->rank > 0 && !sym->attr.pointer && !sym->attr.allocatable)
7630 for (int i = 0; i < sym->as->rank; ++i)
7631 {
7632 gfc_se se;
7633 gfc_init_se (&se, NULL );
7634 if (sym->as->lower[i])
7635 {
7636 gfc_conv_expr (&se, sym->as->lower[i]);
7637 tmp = se.expr;
7638 }
7639 else
7640 tmp = gfc_index_one_node;
7641 gfc_add_block_to_block (&block2, &se.pre);
7642 gfc_conv_descriptor_lbound_set (&block2, gfc_desc, gfc_rank_cst[i],
7643 tmp);
7644 gfc_add_block_to_block (&block2, &se.post);
7645 }
7646
7647 /* Loop: for (i = 0; i < rank; ++i). */
7648 idx = gfc_create_var (TREE_TYPE (rank), "idx");
7649
7650 /* Loop body. */
7651 stmtblock_t loop_body;
7652 gfc_init_block (&loop_body);
7653 /* gfc->dim[i].lbound = ... */
7654 if (sym->attr.pointer || sym->attr.allocatable)
7655 {
7656 tmp = gfc_get_cfi_dim_lbound (cfi, idx);
7657 gfc_conv_descriptor_lbound_set (&loop_body, gfc_desc, idx, tmp);
7658 }
7659 else if (sym->as->rank < 0)
7660 gfc_conv_descriptor_lbound_set (&loop_body, gfc_desc, idx,
7661 gfc_index_one_node);
7662
7663 /* gfc->dim[i].ubound = gfc->dim[i].lbound + cfi->dim[i].extent - 1. */
7664 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
7665 gfc_conv_descriptor_lbound_get (gfc_desc, idx),
7666 gfc_index_one_node);
7667 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
7668 gfc_get_cfi_dim_extent (cfi, idx), tmp);
7669 gfc_conv_descriptor_ubound_set (&loop_body, gfc_desc, idx, tmp);
7670
7671 if (do_copy_inout)
7672 {
7673 /* gfc->dim[i].stride
7674 = idx == 0 ? 1 : gfc->dim[i-1].stride * cfi->dim[i-1].extent */
7675 tree cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
7676 idx, build_zero_cst (TREE_TYPE (idx)));
7677 tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (idx),
7678 idx, build_int_cst (TREE_TYPE (idx), 1));
7679 tree tmp2 = gfc_get_cfi_dim_extent (cfi, tmp);
7680 tmp = gfc_conv_descriptor_stride_get (gfc_desc, tmp);
7681 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp2),
7682 tmp2, tmp);
7683 tmp = build3_loc (input_location, COND_EXPR, gfc_array_index_type, cond,
7684 gfc_index_one_node, tmp);
7685 }
7686 else
7687 {
7688 /* gfc->dim[i].stride = cfi->dim[i].sm / cfi>elem_len */
7689 tmp = gfc_get_cfi_dim_sm (cfi, idx);
7690 tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
7691 gfc_array_index_type, tmp,
7692 fold_convert (gfc_array_index_type,
7693 gfc_get_cfi_desc_elem_len (cfi)));
7694 }
7695 gfc_conv_descriptor_stride_set (&loop_body, gfc_desc, idx, tmp);
7696 /* gfc->offset -= gfc->dim[i].stride * gfc->dim[i].lbound. */
7697 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
7698 gfc_conv_descriptor_stride_get (gfc_desc, idx),
7699 gfc_conv_descriptor_lbound_get (gfc_desc, idx));
7700 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
7701 gfc_conv_descriptor_offset_get (gfc_desc), tmp);
7702 gfc_conv_descriptor_offset_set (&loop_body, gfc_desc, tmp);
7703
7704 /* Generate loop. */
7705 gfc_simple_for_loop (&block2, idx, build_zero_cst (TREE_TYPE (idx)),
7706 rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
7707 gfc_finish_block (&loop_body));
7708 if (sym->attr.allocatable || sym->attr.pointer)
7709 {
7710 tmp = gfc_get_cfi_desc_base_addr (cfi),
7711 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7712 tmp, null_pointer_node);
7713 tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
7714 build_empty_stmt (input_location));
7715 gfc_add_expr_to_block (&block, tmp);
7716 }
7717 else
7718 gfc_add_block_to_block (&block, &block2);
7719
7720 done:
7721 /* If optional arg: 'if (arg) { block } else { local_arg = NULL; }'. */
7722 if (sym->attr.optional)
7723 {
7724 tree present = fold_build2_loc (input_location, NE_EXPR,
7725 boolean_type_node, cfi_desc,
7726 null_pointer_node);
7727 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
7728 sym->backend_decl,
7729 fold_convert (TREE_TYPE (sym->backend_decl),
7730 null_pointer_node));
7731 tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block), tmp);
7732 gfc_add_expr_to_block (init, tmp);
7733 }
7734 else
7735 gfc_add_block_to_block (init, &block);
7736
7737 if (!sym->attr.referenced)
7738 return;
7739
7740 /* If pointer not changed, nothing to be done (except copy out) */
7741 if (!do_copy_inout && ((!sym->attr.pointer && !sym->attr.allocatable)
7742 || sym->attr.intent == INTENT_IN))
7743 return;
7744
7745 gfc_init_block (&block);
7746
7747 /* For bind(C), Fortran does not permit mixing 'pointer' with 'contiguous' (or
7748 len=*). Thus, when copy out is needed, the bounds ofthe descriptor remain
7749 unchanged. */
7750 if (do_copy_inout)
7751 {
7752 tree data, call;
7753 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
7754 data = gfc_conv_descriptor_data_get (gfc_desc);
7755 else if (!POINTER_TYPE_P (TREE_TYPE (gfc_desc)))
7756 data = gfc_build_addr_expr (NULL, gfc_desc);
7757 else
7758 data = gfc_desc;
7759 gfc_init_block (&block2);
7760 if (sym->attr.intent != INTENT_IN)
7761 {
7762 /* First, create the inner copy-out loop.
7763 for (idx = 0; idx < size; ++idx)
7764 {
7765 shift = 0;
7766 tmpidx = idx
7767 for (dim = 0; dim < rank; ++dim)
7768 {
7769 shift += (tmpidx % extent[d]) * sm[d]
7770 tmpidx = tmpidx / extend[d]
7771 }
7772 memcpy (lhs + shift, rhs + idx*elem_len, elem_len)
7773 } .*/
7774 stmtblock_t loop_body;
7775 idx = gfc_create_var (size_type_node, "arrayidx");
7776 gfc_init_block (&loop_body);
7777 tree shift = gfc_create_var (size_type_node, "shift");
7778 tree tmpidx = gfc_create_var (size_type_node, "tmpidx");
7779 gfc_add_modify (&loop_body, shift,
7780 build_zero_cst (TREE_TYPE (shift)));
7781 gfc_add_modify (&loop_body, tmpidx, idx);
7782 stmtblock_t inner_loop;
7783 gfc_init_block (&inner_loop);
7784 tree dim = gfc_create_var (TREE_TYPE (rank), "dim");
7785 /* shift += (tmpidx % extent[d]) * sm[d] */
7786 tmp = fold_convert (size_type_node,
7787 gfc_get_cfi_dim_extent (cfi, dim));
7788 tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR,
7789 size_type_node, tmpidx, tmp);
7790 tmp = fold_build2_loc (input_location, MULT_EXPR,
7791 size_type_node, tmp,
7792 fold_convert (size_type_node,
7793 gfc_get_cfi_dim_sm (cfi, dim)));
7794 gfc_add_modify (&inner_loop, shift,
7795 fold_build2_loc (input_location, PLUS_EXPR,
7796 size_type_node, shift, tmp));
7797 /* tmpidx = tmpidx / extend[d] */
7798 tmp = fold_convert (size_type_node,
7799 gfc_get_cfi_dim_extent (cfi, dim));
7800 gfc_add_modify (&inner_loop, tmpidx,
7801 fold_build2_loc (input_location, TRUNC_DIV_EXPR,
7802 size_type_node, tmpidx, tmp));
7803 gfc_simple_for_loop (&loop_body, dim,
7804 build_zero_cst (TREE_TYPE (rank)), rank, LT_EXPR,
7805 build_int_cst (TREE_TYPE (dim), 1),
7806 gfc_finish_block (&inner_loop));
7807 /* Assign. */
7808 tree rhs;
7809 tmp = fold_convert (pchar_type_node,
7810 gfc_get_cfi_desc_base_addr (cfi));
7811 tmp = fold_build2 (POINTER_PLUS_EXPR, pchar_type_node, tmp, shift);
7812 /* memcpy (lhs + shift, rhs + idx*elem_len, elem_len) */
7813 tree elem_len;
7814 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
7815 elem_len = gfc_conv_descriptor_elem_len (gfc_desc);
7816 else
7817 elem_len = gfc_get_cfi_desc_elem_len (cfi);
7818 rhs = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
7819 elem_len, idx);
7820 rhs = fold_build2_loc (input_location, POINTER_PLUS_EXPR,
7821 pchar_type_node,
7822 fold_convert (pchar_type_node, data), rhs);
7823 tmp = fold_convert (pvoid_type_node, tmp);
7824 rhs = fold_convert (pvoid_type_node, rhs);
7825 call = builtin_decl_explicit (BUILT_IN_MEMCPY);
7826 call = build_call_expr_loc (input_location, call, 3, tmp, rhs,
7827 elem_len);
7828 gfc_add_expr_to_block (&loop_body,
7829 fold_convert (void_type_node, call));
7830 gfc_simple_for_loop (&block2, idx, build_zero_cst (TREE_TYPE (idx)),
7831 size_var, LT_EXPR,
7832 build_int_cst (TREE_TYPE (idx), 1),
7833 gfc_finish_block (&loop_body));
7834 }
7835 call = builtin_decl_explicit (BUILT_IN_FREE);
7836 call = build_call_expr_loc (input_location, call, 1, data);
7837 gfc_add_expr_to_block (&block2, call);
7838
7839 /* if (cfi->base_addr != gfc->data) { copy out; free(var) }; return */
7840 tree tmp2 = gfc_get_cfi_desc_base_addr (cfi);
7841 tmp2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7842 tmp2, fold_convert (TREE_TYPE (tmp2), data));
7843 tmp = build3_v (COND_EXPR, tmp2, gfc_finish_block (&block2),
7844 build_empty_stmt (input_location));
7845 gfc_add_expr_to_block (&block, tmp);
7846 goto done_finally;
7847 }
7848
7849 /* Update pointer + array data data on exit. */
7850 tmp = gfc_get_cfi_desc_base_addr (cfi);
7851 tmp2 = (!sym->attr.dimension
7852 ? gfc_desc : gfc_conv_descriptor_data_get (gfc_desc));
7853 gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), tmp2));
7854
7855 /* Set string length for len=:, only. */
7856 if (sym->ts.type == BT_CHARACTER && !sym->ts.u.cl->length)
7857 {
7858 tmp2 = gfc_get_cfi_desc_elem_len (cfi);
7859 tmp = fold_convert (TREE_TYPE (tmp2), sym->ts.u.cl->backend_decl);
7860 if (sym->ts.kind != 1)
7861 tmp = fold_build2_loc (input_location, MULT_EXPR,
7862 TREE_TYPE (tmp2), tmp,
7863 build_int_cst (TREE_TYPE (tmp2), sym->ts.kind));
7864 gfc_add_modify (&block, tmp2, tmp);
7865 }
7866
7867 if (!sym->attr.dimension)
7868 goto done_finally;
7869
7870 gfc_init_block (&block2);
7871
7872 /* Loop: for (i = 0; i < rank; ++i). */
7873 idx = gfc_create_var (TREE_TYPE (rank), "idx");
7874
7875 /* Loop body. */
7876 gfc_init_block (&loop_body);
7877 /* cfi->dim[i].lower_bound = gfc->dim[i].lbound */
7878 gfc_add_modify (&loop_body, gfc_get_cfi_dim_lbound (cfi, idx),
7879 gfc_conv_descriptor_lbound_get (gfc_desc, idx));
7880 /* cfi->dim[i].extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1. */
7881 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
7882 gfc_conv_descriptor_ubound_get (gfc_desc, idx),
7883 gfc_conv_descriptor_lbound_get (gfc_desc, idx));
7884 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type, tmp,
7885 gfc_index_one_node);
7886 gfc_add_modify (&loop_body, gfc_get_cfi_dim_extent (cfi, idx), tmp);
7887 /* d->dim[n].sm = gfc->dim[i].stride * gfc->span); */
7888 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
7889 gfc_conv_descriptor_stride_get (gfc_desc, idx),
7890 gfc_conv_descriptor_span_get (gfc_desc));
7891 gfc_add_modify (&loop_body, gfc_get_cfi_dim_sm (cfi, idx), tmp);
7892
7893 /* Generate loop. */
7894 gfc_simple_for_loop (&block2, idx, build_zero_cst (TREE_TYPE (idx)),
7895 rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
7896 gfc_finish_block (&loop_body));
7897 /* if (gfc->data != NULL) { block2 }. */
7898 tmp = gfc_get_cfi_desc_base_addr (cfi),
7899 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7900 tmp, null_pointer_node);
7901 tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
7902 build_empty_stmt (input_location));
7903 gfc_add_expr_to_block (&block, tmp);
7904
7905 done_finally:
7906 /* If optional arg: 'if (arg) { block } else { local_arg = NULL; }'. */
7907 if (sym->attr.optional)
7908 {
7909 tree present = fold_build2_loc (input_location, NE_EXPR,
7910 boolean_type_node, cfi_desc,
7911 null_pointer_node);
7912 tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block),
7913 build_empty_stmt (input_location));
7914 gfc_add_expr_to_block (finally, tmp);
7915 }
7916 else
7917 gfc_add_block_to_block (finally, &block);
7918 }
7919
7920 /* Generate code for a function. */
7921
7922 void
7923 gfc_generate_function_code (gfc_namespace * ns)
7924 {
7925 tree fndecl;
7926 tree old_context;
7927 tree decl;
7928 tree tmp;
7929 tree fpstate = NULL_TREE;
7930 stmtblock_t init, cleanup, outer_block;
7931 stmtblock_t body;
7932 gfc_wrapped_block try_block;
7933 tree recurcheckvar = NULL_TREE;
7934 gfc_symbol *sym;
7935 gfc_symbol *previous_procedure_symbol;
7936 int rank, ieee;
7937 bool is_recursive;
7938
7939 sym = ns->proc_name;
7940 previous_procedure_symbol = current_procedure_symbol;
7941 current_procedure_symbol = sym;
7942
7943 /* Initialize sym->tlink so that gfc_trans_deferred_vars does not get
7944 lost or worse. */
7945 sym->tlink = sym;
7946
7947 /* Create the declaration for functions with global scope. */
7948 if (!sym->backend_decl)
7949 gfc_create_function_decl (ns, false);
7950
7951 fndecl = sym->backend_decl;
7952 old_context = current_function_decl;
7953
7954 if (old_context)
7955 {
7956 push_function_context ();
7957 saved_parent_function_decls = saved_function_decls;
7958 saved_function_decls = NULL_TREE;
7959 }
7960
7961 trans_function_start (sym);
7962 gfc_current_locus = sym->declared_at;
7963
7964 gfc_init_block (&init);
7965 gfc_init_block (&cleanup);
7966 gfc_init_block (&outer_block);
7967
7968 if (ns->entries && ns->proc_name->ts.type == BT_CHARACTER)
7969 {
7970 /* Copy length backend_decls to all entry point result
7971 symbols. */
7972 gfc_entry_list *el;
7973 tree backend_decl;
7974
7975 gfc_conv_const_charlen (ns->proc_name->ts.u.cl);
7976 backend_decl = ns->proc_name->result->ts.u.cl->backend_decl;
7977 for (el = ns->entries; el; el = el->next)
7978 el->sym->result->ts.u.cl->backend_decl = backend_decl;
7979 }
7980
7981 /* Translate COMMON blocks. */
7982 gfc_trans_common (ns);
7983
7984 /* Null the parent fake result declaration if this namespace is
7985 a module function or an external procedures. */
7986 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
7987 || ns->parent == NULL)
7988 parent_fake_result_decl = NULL_TREE;
7989
7990 /* For BIND(C):
7991 - deallocate intent-out allocatable dummy arguments.
7992 - Create GFC variable which will later be populated by convert_CFI_desc */
7993 if (sym->attr.is_bind_c)
7994 for (gfc_formal_arglist *formal = gfc_sym_get_dummy_args (sym);
7995 formal; formal = formal->next)
7996 {
7997 gfc_symbol *fsym = formal->sym;
7998 if (!is_CFI_desc (fsym, NULL))
7999 continue;
8000 if (!fsym->attr.referenced)
8001 {
8002 gfc_conv_cfi_to_gfc (&init, &cleanup, fsym->backend_decl,
8003 NULL_TREE, fsym);
8004 continue;
8005 }
8006 /* Let's now create a local GFI descriptor. Afterwards:
8007 desc is the local descriptor,
8008 desc_p is a pointer to it
8009 and stored in sym->backend_decl
8010 GFC_DECL_SAVED_DESCRIPTOR (desc_p) contains the CFI descriptor
8011 -> PARM_DECL and before sym->backend_decl.
8012 For scalars, decl == decl_p is a pointer variable. */
8013 tree desc_p, desc;
8014 location_t loc = gfc_get_location (&sym->declared_at);
8015 if (fsym->ts.type == BT_CHARACTER && !fsym->ts.u.cl->length)
8016 fsym->ts.u.cl->backend_decl = gfc_create_var (gfc_array_index_type,
8017 fsym->name);
8018 else if (fsym->ts.type == BT_CHARACTER && !fsym->ts.u.cl->backend_decl)
8019 {
8020 gfc_se se;
8021 gfc_init_se (&se, NULL );
8022 gfc_conv_expr (&se, fsym->ts.u.cl->length);
8023 gfc_add_block_to_block (&init, &se.pre);
8024 fsym->ts.u.cl->backend_decl = se.expr;
8025 gcc_assert(se.post.head == NULL_TREE);
8026 }
8027 /* Nullify, otherwise gfc_sym_type will return the CFI type. */
8028 tree tmp = fsym->backend_decl;
8029 fsym->backend_decl = NULL;
8030 tree type = gfc_sym_type (fsym);
8031 gcc_assert (POINTER_TYPE_P (type));
8032 if (POINTER_TYPE_P (TREE_TYPE (type)))
8033 /* For instance, allocatable scalars. */
8034 type = TREE_TYPE (type);
8035 if (TREE_CODE (type) == REFERENCE_TYPE)
8036 type = build_pointer_type (TREE_TYPE (type));
8037 desc_p = build_decl (loc, VAR_DECL, get_identifier (fsym->name), type);
8038 if (!fsym->attr.dimension)
8039 desc = desc_p;
8040 else if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (desc_p))))
8041 {
8042 /* Character(len=*) explicit-size/assumed-size array. */
8043 desc = desc_p;
8044 gfc_build_qualified_array (desc, fsym);
8045 }
8046 else
8047 {
8048 tree size = size_in_bytes (TREE_TYPE (TREE_TYPE (desc_p)));
8049 tree call = builtin_decl_explicit (BUILT_IN_ALLOCA);
8050 call = build_call_expr_loc (input_location, call, 1, size);
8051 gfc_add_modify (&outer_block, desc_p,
8052 fold_convert (TREE_TYPE(desc_p), call));
8053 desc = build_fold_indirect_ref_loc (input_location, desc_p);
8054 }
8055 pushdecl (desc_p);
8056 if (fsym->attr.optional)
8057 {
8058 gfc_allocate_lang_decl (desc_p);
8059 GFC_DECL_OPTIONAL_ARGUMENT (desc_p) = 1;
8060 }
8061 fsym->backend_decl = desc_p;
8062 gfc_conv_cfi_to_gfc (&init, &cleanup, tmp, desc, fsym);
8063 }
8064
8065 /* For OpenMP, ensure that declare variant in INTERFACE is is processed
8066 especially as some late diagnostic is only done on tree level. */
8067 if (flag_openmp)
8068 gfc_traverse_ns (ns, gfc_handle_omp_declare_variant);
8069
8070 gfc_generate_contained_functions (ns);
8071
8072 has_coarray_vars_or_accessors = caf_accessor_head != NULL;
8073 generate_local_vars (ns);
8074
8075 if (flag_coarray == GFC_FCOARRAY_LIB && has_coarray_vars_or_accessors)
8076 generate_coarray_init (ns);
8077
8078 /* Keep the parent fake result declaration in module functions
8079 or external procedures. */
8080 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
8081 || ns->parent == NULL)
8082 current_fake_result_decl = parent_fake_result_decl;
8083 else
8084 current_fake_result_decl = NULL_TREE;
8085
8086 is_recursive = sym->attr.recursive
8087 || (sym->attr.entry_master
8088 && sym->ns->entries->sym->attr.recursive);
8089 if ((gfc_option.rtcheck & GFC_RTCHECK_RECURSION)
8090 && !is_recursive && !flag_recursive && !sym->attr.artificial)
8091 {
8092 char * msg;
8093
8094 msg = xasprintf ("Recursive call to nonrecursive procedure '%s'",
8095 sym->name);
8096 recurcheckvar = gfc_create_var (logical_type_node, "is_recursive");
8097 TREE_STATIC (recurcheckvar) = 1;
8098 DECL_INITIAL (recurcheckvar) = logical_false_node;
8099 gfc_add_expr_to_block (&init, recurcheckvar);
8100 gfc_trans_runtime_check (true, false, recurcheckvar, &init,
8101 &sym->declared_at, msg);
8102 gfc_add_modify (&init, recurcheckvar, logical_true_node);
8103 free (msg);
8104 }
8105
8106 /* Check if an IEEE module is used in the procedure. If so, save
8107 the floating point state. */
8108 ieee = is_ieee_module_used (ns);
8109 if (ieee)
8110 fpstate = gfc_save_fp_state (&init);
8111
8112 /* Now generate the code for the body of this function. */
8113 gfc_init_block (&body);
8114
8115 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
8116 && sym->attr.subroutine)
8117 {
8118 tree alternate_return;
8119 alternate_return = gfc_get_fake_result_decl (sym, 0);
8120 gfc_add_modify (&body, alternate_return, integer_zero_node);
8121 }
8122
8123 if (ns->entries)
8124 {
8125 /* Jump to the correct entry point. */
8126 tmp = gfc_trans_entry_master_switch (ns->entries);
8127 gfc_add_expr_to_block (&body, tmp);
8128 }
8129
8130 /* If bounds-checking is enabled, generate code to check passed in actual
8131 arguments against the expected dummy argument attributes (e.g. string
8132 lengths). */
8133 if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !sym->attr.is_bind_c)
8134 add_argument_checking (&body, sym);
8135
8136 finish_oacc_declare (ns, sym, false);
8137
8138 tmp = gfc_trans_code (ns->code);
8139 gfc_add_expr_to_block (&body, tmp);
8140
8141 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
8142 || (sym->result && sym->result != sym
8143 && sym->result->ts.type == BT_DERIVED
8144 && sym->result->ts.u.derived->attr.alloc_comp))
8145 {
8146 bool artificial_result_decl = false;
8147 tree result = get_proc_result (sym);
8148 gfc_symbol *rsym = sym == sym->result ? sym : sym->result;
8149
8150 /* Make sure that a function returning an object with
8151 alloc/pointer_components always has a result, where at least
8152 the allocatable/pointer components are set to zero. */
8153 if (result == NULL_TREE && sym->attr.function
8154 && ((sym->result->ts.type == BT_DERIVED
8155 && (sym->attr.allocatable
8156 || sym->attr.pointer
8157 || sym->result->ts.u.derived->attr.alloc_comp
8158 || sym->result->ts.u.derived->attr.pointer_comp))
8159 || (sym->result->ts.type == BT_CLASS
8160 && (CLASS_DATA (sym->result)->attr.allocatable
8161 || CLASS_DATA (sym->result)->attr.class_pointer
8162 || CLASS_DATA (sym->result)->attr.alloc_comp
8163 || CLASS_DATA (sym->result)->attr.pointer_comp))))
8164 {
8165 artificial_result_decl = true;
8166 result = gfc_get_fake_result_decl (sym->result, 0);
8167 }
8168
8169 if (result != NULL_TREE && sym->attr.function && !sym->attr.pointer)
8170 {
8171 if (sym->attr.allocatable && sym->attr.dimension == 0
8172 && sym->result == sym)
8173 gfc_add_modify (&init, result, fold_convert (TREE_TYPE (result),
8174 null_pointer_node));
8175 else if (sym->ts.type == BT_CLASS
8176 && CLASS_DATA (sym)->attr.allocatable
8177 && CLASS_DATA (sym)->attr.dimension == 0
8178 && sym->result == sym)
8179 {
8180 tmp = gfc_class_data_get (result);
8181 gfc_add_modify (&init, tmp,
8182 fold_convert (TREE_TYPE (tmp),
8183 null_pointer_node));
8184 gfc_reset_vptr (&init, nullptr, result,
8185 sym->result->ts.u.derived);
8186 }
8187 else if (sym->ts.type == BT_DERIVED
8188 && !sym->attr.allocatable)
8189 {
8190 gfc_expr *init_exp;
8191 /* Arrays are not initialized using the default initializer of
8192 their elements. Therefore only check if a default
8193 initializer is available when the result is scalar. */
8194 init_exp = rsym->as ? NULL
8195 : gfc_generate_initializer (&rsym->ts, true);
8196 if (init_exp)
8197 {
8198 tmp = gfc_trans_structure_assign (result, init_exp, 0);
8199 gfc_free_expr (init_exp);
8200 gfc_add_expr_to_block (&init, tmp);
8201 }
8202 else if (rsym->ts.u.derived->attr.alloc_comp)
8203 {
8204 rank = rsym->as ? rsym->as->rank : 0;
8205 tmp = gfc_nullify_alloc_comp (rsym->ts.u.derived, result,
8206 rank);
8207 gfc_prepend_expr_to_block (&body, tmp);
8208 }
8209 }
8210 }
8211
8212 if (result == NULL_TREE || artificial_result_decl)
8213 {
8214 /* TODO: move to the appropriate place in resolve.cc. */
8215 if (warn_return_type > 0 && sym == sym->result)
8216 gfc_warning (OPT_Wreturn_type,
8217 "Return value of function %qs at %L not set",
8218 sym->name, &sym->declared_at);
8219 if (warn_return_type > 0)
8220 suppress_warning (sym->backend_decl);
8221 }
8222 if (result != NULL_TREE)
8223 gfc_add_expr_to_block (&body, gfc_generate_return ());
8224 }
8225
8226 /* Reset recursion-check variable. */
8227 if (recurcheckvar != NULL_TREE)
8228 {
8229 gfc_add_modify (&cleanup, recurcheckvar, logical_false_node);
8230 recurcheckvar = NULL;
8231 }
8232
8233 /* If IEEE modules are loaded, restore the floating-point state. */
8234 if (ieee)
8235 gfc_restore_fp_state (&cleanup, fpstate);
8236
8237 /* Finish the function body and add init and cleanup code. */
8238 tmp = gfc_finish_block (&body);
8239 /* Add code to create and cleanup arrays. */
8240 gfc_start_wrapped_block (&try_block, tmp);
8241 gfc_trans_deferred_vars (sym, &try_block);
8242 gfc_add_init_cleanup (&try_block, gfc_finish_block (&init),
8243 gfc_finish_block (&cleanup));
8244
8245 /* Add all the decls we created during processing. */
8246 decl = nreverse (saved_function_decls);
8247 while (decl)
8248 {
8249 tree next;
8250
8251 next = DECL_CHAIN (decl);
8252 DECL_CHAIN (decl) = NULL_TREE;
8253 pushdecl (decl);
8254 decl = next;
8255 }
8256 saved_function_decls = NULL_TREE;
8257
8258 gfc_add_expr_to_block (&outer_block, gfc_finish_wrapped_block (&try_block));
8259 DECL_SAVED_TREE (fndecl) = gfc_finish_block (&outer_block);
8260 decl = getdecls ();
8261
8262 /* Finish off this function and send it for code generation. */
8263 poplevel (1, 1);
8264 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
8265
8266 DECL_SAVED_TREE (fndecl)
8267 = fold_build3_loc (DECL_SOURCE_LOCATION (fndecl), BIND_EXPR, void_type_node,
8268 decl, DECL_SAVED_TREE (fndecl), DECL_INITIAL (fndecl));
8269
8270 /* Output the GENERIC tree. */
8271 dump_function (TDI_original, fndecl);
8272
8273 /* Store the end of the function, so that we get good line number
8274 info for the epilogue. */
8275 cfun->function_end_locus = input_location;
8276
8277 /* We're leaving the context of this function, so zap cfun.
8278 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
8279 tree_rest_of_compilation. */
8280 set_cfun (NULL);
8281
8282 if (old_context)
8283 {
8284 pop_function_context ();
8285 saved_function_decls = saved_parent_function_decls;
8286 }
8287 current_function_decl = old_context;
8288
8289 if (decl_function_context (fndecl))
8290 {
8291 /* Register this function with cgraph just far enough to get it
8292 added to our parent's nested function list.
8293 If there are static coarrays in this function, the nested _caf_init
8294 function has already called cgraph_create_node, which also created
8295 the cgraph node for this function. */
8296 if (!has_coarray_vars_or_accessors || flag_coarray != GFC_FCOARRAY_LIB)
8297 (void) cgraph_node::get_create (fndecl);
8298 }
8299 else
8300 cgraph_node::finalize_function (fndecl, true);
8301
8302 gfc_trans_use_stmts (ns);
8303 gfc_traverse_ns (ns, gfc_emit_parameter_debug_info);
8304
8305 if (sym->attr.is_main_program)
8306 create_main_function (fndecl);
8307
8308 current_procedure_symbol = previous_procedure_symbol;
8309 }
8310
8311
8312 void
8313 gfc_generate_constructors (void)
8314 {
8315 gcc_assert (gfc_static_ctors == NULL_TREE);
8316 #if 0
8317 tree fnname;
8318 tree type;
8319 tree fndecl;
8320 tree decl;
8321 tree tmp;
8322
8323 if (gfc_static_ctors == NULL_TREE)
8324 return;
8325
8326 fnname = get_file_function_name ("I");
8327 type = build_function_type_list (void_type_node, NULL_TREE);
8328
8329 fndecl = build_decl (input_location,
8330 FUNCTION_DECL, fnname, type);
8331 TREE_PUBLIC (fndecl) = 1;
8332
8333 decl = build_decl (input_location,
8334 RESULT_DECL, NULL_TREE, void_type_node);
8335 DECL_ARTIFICIAL (decl) = 1;
8336 DECL_IGNORED_P (decl) = 1;
8337 DECL_CONTEXT (decl) = fndecl;
8338 DECL_RESULT (fndecl) = decl;
8339
8340 pushdecl (fndecl);
8341
8342 current_function_decl = fndecl;
8343
8344 rest_of_decl_compilation (fndecl, 1, 0);
8345
8346 make_decl_rtl (fndecl);
8347
8348 allocate_struct_function (fndecl, false);
8349
8350 pushlevel ();
8351
8352 for (; gfc_static_ctors; gfc_static_ctors = TREE_CHAIN (gfc_static_ctors))
8353 {
8354 tmp = build_call_expr_loc (input_location,
8355 TREE_VALUE (gfc_static_ctors), 0);
8356 DECL_SAVED_TREE (fndecl) = build_stmt (input_location, EXPR_STMT, tmp);
8357 }
8358
8359 decl = getdecls ();
8360 poplevel (1, 1);
8361
8362 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
8363 DECL_SAVED_TREE (fndecl)
8364 = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (fndecl),
8365 DECL_INITIAL (fndecl));
8366
8367 free_after_parsing (cfun);
8368 free_after_compilation (cfun);
8369
8370 tree_rest_of_compilation (fndecl);
8371
8372 current_function_decl = NULL_TREE;
8373 #endif
8374 }
8375
8376
8377 /* Helper function for checking of variables declared in a BLOCK DATA program
8378 unit. */
8379
8380 static void
8381 check_block_data_decls (gfc_symbol * sym)
8382 {
8383 if (warn_unused_variable
8384 && sym->attr.flavor == FL_VARIABLE
8385 && !sym->attr.in_common
8386 && !sym->attr.artificial)
8387 {
8388 gfc_warning (OPT_Wunused_variable,
8389 "Symbol %qs at %L is declared in a BLOCK DATA "
8390 "program unit but is not in a COMMON block",
8391 sym->name, &sym->declared_at);
8392 }
8393 }
8394
8395
8396 /* Translates a BLOCK DATA program unit. This means emitting the
8397 commons contained therein plus their initializations. We also emit
8398 a globally visible symbol to make sure that each BLOCK DATA program
8399 unit remains unique. */
8400
8401 void
8402 gfc_generate_block_data (gfc_namespace * ns)
8403 {
8404 tree decl;
8405 tree id;
8406
8407 /* Tell the backend the source location of the block data. */
8408 if (ns->proc_name)
8409 input_location = gfc_get_location (&ns->proc_name->declared_at);
8410 else
8411 input_location = gfc_get_location (&gfc_current_locus);
8412
8413 /* Process the DATA statements. */
8414 gfc_trans_common (ns);
8415
8416 /* Check for variables declared in BLOCK DATA but not used in COMMON. */
8417 gfc_traverse_ns (ns, check_block_data_decls);
8418
8419 /* Create a global symbol with the mane of the block data. This is to
8420 generate linker errors if the same name is used twice. It is never
8421 really used. */
8422 if (ns->proc_name)
8423 id = gfc_sym_mangled_function_id (ns->proc_name);
8424 else
8425 id = get_identifier ("__BLOCK_DATA__");
8426
8427 decl = build_decl (input_location,
8428 VAR_DECL, id, gfc_array_index_type);
8429 TREE_PUBLIC (decl) = 1;
8430 TREE_STATIC (decl) = 1;
8431 DECL_IGNORED_P (decl) = 1;
8432
8433 pushdecl (decl);
8434 rest_of_decl_compilation (decl, 1, 0);
8435 }
8436
8437 void
8438 gfc_start_saved_local_decls ()
8439 {
8440 gcc_checking_assert (current_function_decl != NULL_TREE);
8441 saved_local_decls = NULL_TREE;
8442 }
8443
8444 void
8445 gfc_stop_saved_local_decls ()
8446 {
8447 tree decl = nreverse (saved_local_decls);
8448 while (decl)
8449 {
8450 tree next;
8451
8452 next = DECL_CHAIN (decl);
8453 DECL_CHAIN (decl) = NULL_TREE;
8454 pushdecl (decl);
8455 decl = next;
8456 }
8457 saved_local_decls = NULL_TREE;
8458 }
8459
8460 /* Process the local variables of a BLOCK construct. */
8461
8462 void
8463 gfc_process_block_locals (gfc_namespace* ns)
8464 {
8465 gfc_start_saved_local_decls ();
8466 has_coarray_vars_or_accessors = caf_accessor_head != NULL;
8467
8468 generate_local_vars (ns);
8469
8470 if (flag_coarray == GFC_FCOARRAY_LIB && has_coarray_vars_or_accessors)
8471 generate_coarray_init (ns);
8472 gfc_stop_saved_local_decls ();
8473 }
8474
8475
8476 #include "gt-fortran-trans-decl.h"