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