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