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