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