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