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