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