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