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