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