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