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