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