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