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