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