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