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