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