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