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