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