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