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