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