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