]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/trans-decl.c
Daily bump.
[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;
8b9eb27e 1208 bool fun_or_res;
4ee9c684 1209
0b5dc8b5 1210 gcc_assert (sym->attr.referenced
9e45357a 1211 || sym->attr.flavor == FL_PROCEDURE
1212 || sym->attr.use_assoc
1213 || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY
1214 || (sym->module && sym->attr.if_source != IFSRC_DECL
1215 && sym->backend_decl));
4ee9c684 1216
ff70e443 1217 if (sym->ns && sym->ns->proc_name && sym->ns->proc_name->attr.function)
4ee9c684 1218 byref = gfc_return_by_reference (sym->ns->proc_name);
1219 else
1220 byref = 0;
1221
09c509ed 1222 /* Make sure that the vtab for the declared type is completed. */
1223 if (sym->ts.type == BT_CLASS)
1224 {
50b4b37b 1225 gfc_component *c = CLASS_DATA (sym);
09c509ed 1226 if (!c->ts.u.derived->backend_decl)
eab71f19 1227 {
1228 gfc_find_derived_vtab (c->ts.u.derived);
1229 gfc_get_derived_type (sym->ts.u.derived);
1230 }
09c509ed 1231 }
1232
617125a6 1233 /* All deferred character length procedures need to retain the backend
1234 decl, which is a pointer to the character length in the caller's
1235 namespace and to declare a local character length. */
1236 if (!byref && sym->attr.function
1237 && sym->ts.type == BT_CHARACTER
1238 && sym->ts.deferred
1239 && sym->ts.u.cl->passed_length == NULL
1240 && sym->ts.u.cl->backend_decl
1241 && TREE_CODE (sym->ts.u.cl->backend_decl) == PARM_DECL)
1242 {
1243 sym->ts.u.cl->passed_length = sym->ts.u.cl->backend_decl;
1244 sym->ts.u.cl->backend_decl = NULL_TREE;
1245 length = gfc_create_string_length (sym);
1246 }
1247
8b9eb27e 1248 fun_or_res = byref && (sym->attr.result
1249 || (sym->attr.function && sym->ts.deferred));
1250 if ((sym->attr.dummy && ! sym->attr.function) || fun_or_res)
4ee9c684 1251 {
1252 /* Return via extra parameter. */
1253 if (sym->attr.result && byref
1254 && !sym->backend_decl)
1255 {
1256 sym->backend_decl =
1257 DECL_ARGUMENTS (sym->ns->proc_name->backend_decl);
c6871095 1258 /* For entry master function skip over the __entry
1259 argument. */
1260 if (sym->ns->proc_name->attr.entry_master)
1767a056 1261 sym->backend_decl = DECL_CHAIN (sym->backend_decl);
4ee9c684 1262 }
1263
1264 /* Dummy variables should already have been created. */
22d678e8 1265 gcc_assert (sym->backend_decl);
4ee9c684 1266
1267 /* Create a character length variable. */
1268 if (sym->ts.type == BT_CHARACTER)
1269 {
617125a6 1270 /* For a deferred dummy, make a new string length variable. */
1271 if (sym->ts.deferred
1272 &&
1273 (sym->ts.u.cl->passed_length == sym->ts.u.cl->backend_decl))
1274 sym->ts.u.cl->backend_decl = NULL_TREE;
1275
8b9eb27e 1276 if (sym->ts.deferred && fun_or_res
617125a6 1277 && sym->ts.u.cl->passed_length == NULL
1278 && sym->ts.u.cl->backend_decl)
1279 {
1280 sym->ts.u.cl->passed_length = sym->ts.u.cl->backend_decl;
1281 sym->ts.u.cl->backend_decl = NULL_TREE;
1282 }
1283
eeebe20b 1284 if (sym->ts.u.cl->backend_decl == NULL_TREE)
d4163395 1285 length = gfc_create_string_length (sym);
1286 else
eeebe20b 1287 length = sym->ts.u.cl->backend_decl;
d4163395 1288 if (TREE_CODE (length) == VAR_DECL
16a40513 1289 && DECL_FILE_SCOPE_P (length))
4ee9c684 1290 {
d95efb59 1291 /* Add the string length to the same context as the symbol. */
1292 if (DECL_CONTEXT (sym->backend_decl) == current_function_decl)
1293 gfc_add_decl_to_function (length);
1294 else
1295 gfc_add_decl_to_parent_function (length);
1296
1297 gcc_assert (DECL_CONTEXT (sym->backend_decl) ==
1298 DECL_CONTEXT (length));
1299
d4163395 1300 gfc_defer_symbol_init (sym);
e8ff3944 1301 }
4ee9c684 1302 }
1303
1304 /* Use a copy of the descriptor for dummy arrays. */
7a777e43 1305 if ((sym->attr.dimension || sym->attr.codimension)
1306 && !TREE_USED (sym->backend_decl))
4ee9c684 1307 {
d95efb59 1308 decl = gfc_build_dummy_array_decl (sym, sym->backend_decl);
1309 /* Prevent the dummy from being detected as unused if it is copied. */
1310 if (sym->backend_decl != NULL && decl != sym->backend_decl)
1311 DECL_ARTIFICIAL (sym->backend_decl) = 1;
1312 sym->backend_decl = decl;
4ee9c684 1313 }
1314
1315 TREE_USED (sym->backend_decl) = 1;
c8f1568f 1316 if (sym->attr.assign && GFC_DECL_ASSIGN (sym->backend_decl) == 0)
1317 {
1318 gfc_add_assign_aux_vars (sym);
1319 }
9579733e 1320
1321 if (sym->attr.dimension
1322 && DECL_LANG_SPECIFIC (sym->backend_decl)
1323 && GFC_DECL_SAVED_DESCRIPTOR (sym->backend_decl)
1324 && DECL_CONTEXT (sym->backend_decl) != current_function_decl)
1325 gfc_nonlocal_dummy_array_decl (sym);
1326
fd23cc08 1327 if (sym->ts.type == BT_CLASS && sym->backend_decl)
1328 GFC_DECL_CLASS(sym->backend_decl) = 1;
1329
1330 if (sym->ts.type == BT_CLASS && sym->backend_decl)
1331 GFC_DECL_CLASS(sym->backend_decl) = 1;
1332 return sym->backend_decl;
4ee9c684 1333 }
1334
1335 if (sym->backend_decl)
1336 return sym->backend_decl;
1337
a25debd0 1338 /* Special case for array-valued named constants from intrinsic
1339 procedures; those are inlined. */
1340 if (sym->attr.use_assoc && sym->from_intmod
1341 && sym->attr.flavor == FL_PARAMETER)
1342 intrinsic_array_parameter = true;
1343
044bbd71 1344 /* If use associated compilation, use the module
094bca96 1345 declaration. */
044bbd71 1346 if ((sym->attr.flavor == FL_VARIABLE
1347 || sym->attr.flavor == FL_PARAMETER)
1348 && sym->attr.use_assoc
1349 && !intrinsic_array_parameter
1350 && sym->module
1351 && gfc_get_module_backend_decl (sym))
fd23cc08 1352 {
1353 if (sym->ts.type == BT_CLASS && sym->backend_decl)
1354 GFC_DECL_CLASS(sym->backend_decl) = 1;
1355 return sym->backend_decl;
1356 }
7ea64434 1357
4ee9c684 1358 if (sym->attr.flavor == FL_PROCEDURE)
1359 {
802532b9 1360 /* Catch function declarations. Only used for actual parameters,
1361 procedure pointers and procptr initialization targets. */
1362 if (sym->attr.external || sym->attr.use_assoc || sym->attr.intrinsic)
1363 {
1364 decl = gfc_get_extern_function_decl (sym);
1365 gfc_set_decl_location (decl, &sym->declared_at);
1366 }
1367 else
1368 {
1369 if (!sym->backend_decl)
1370 build_function_decl (sym, false);
1371 decl = sym->backend_decl;
1372 }
4ee9c684 1373 return decl;
1374 }
1375
1376 if (sym->attr.intrinsic)
1377 internal_error ("intrinsic variable which isn't a procedure");
1378
1379 /* Create string length decl first so that they can be used in the
1380 type declaration. */
1381 if (sym->ts.type == BT_CHARACTER)
1382 length = gfc_create_string_length (sym);
1383
1384 /* Create the decl for the variable. */
e60a6f7b 1385 decl = build_decl (sym->declared_at.lb->location,
1386 VAR_DECL, gfc_sym_identifier (sym), gfc_sym_type (sym));
b31f705b 1387
1236e28b 1388 /* Add attributes to variables. Functions are handled elsewhere. */
1389 attributes = add_attributes_to_decl (sym->attr, NULL_TREE);
1390 decl_attributes (&decl, attributes, 0);
1391
f888a3fb 1392 /* Symbols from modules should have their assembler names mangled.
4ee9c684 1393 This is done here rather than in gfc_finish_var_decl because it
1394 is different for string length variables. */
4f0fae8e 1395 if (sym->module)
df4d540f 1396 {
1236e28b 1397 gfc_set_decl_assembler_name (decl, gfc_sym_mangled_identifier (sym));
cd2c99b8 1398 if (sym->attr.use_assoc && !intrinsic_array_parameter)
df4d540f 1399 DECL_IGNORED_P (decl) = 1;
1400 }
4ee9c684 1401
bc5663df 1402 if (sym->attr.select_type_temporary)
1403 {
1404 DECL_ARTIFICIAL (decl) = 1;
1405 DECL_IGNORED_P (decl) = 1;
1406 }
1407
7a777e43 1408 if (sym->attr.dimension || sym->attr.codimension)
4ee9c684 1409 {
1410 /* Create variables to hold the non-constant bits of array info. */
1411 gfc_build_qualified_array (decl, sym);
1412
b3c3927c 1413 if (sym->attr.contiguous
1414 || ((sym->attr.allocatable || !sym->attr.dummy) && !sym->attr.pointer))
4ee9c684 1415 GFC_DECL_PACKED_ARRAY (decl) = 1;
1416 }
1417
0a96a7cc 1418 /* Remember this variable for allocation/cleanup. */
7c7db7f6 1419 if (sym->attr.dimension || sym->attr.allocatable || sym->attr.codimension
0a96a7cc 1420 || (sym->ts.type == BT_CLASS &&
50b4b37b 1421 (CLASS_DATA (sym)->attr.dimension
1422 || CLASS_DATA (sym)->attr.allocatable))
0a96a7cc 1423 || (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.alloc_comp)
1424 /* This applies a derived type default initializer. */
1425 || (sym->ts.type == BT_DERIVED
1426 && sym->attr.save == SAVE_NONE
1427 && !sym->attr.data
1428 && !sym->attr.allocatable
1429 && (sym->value && !sym->ns->proc_name->attr.is_main_program)
cd2c99b8 1430 && !(sym->attr.use_assoc && !intrinsic_array_parameter)))
f0d4969f 1431 gfc_defer_symbol_init (sym);
2294b616 1432
4ee9c684 1433 gfc_finish_var_decl (decl, sym);
1434
bda1f152 1435 if (sym->ts.type == BT_CHARACTER)
4ee9c684 1436 {
4ee9c684 1437 /* Character variables need special handling. */
1438 gfc_allocate_lang_decl (decl);
1439
bda1f152 1440 if (TREE_CODE (length) != INTEGER_CST)
4ee9c684 1441 {
4ee9c684 1442 gfc_finish_var_decl (length, sym);
22d678e8 1443 gcc_assert (!sym->value);
4ee9c684 1444 }
4ee9c684 1445 }
1033248c 1446 else if (sym->attr.subref_array_pointer)
1447 {
1448 /* We need the span for these beasts. */
1449 gfc_allocate_lang_decl (decl);
1450 }
1451
1452 if (sym->attr.subref_array_pointer)
1453 {
1454 tree span;
1455 GFC_DECL_SUBREF_ARRAY_P (decl) = 1;
e60a6f7b 1456 span = build_decl (input_location,
1457 VAR_DECL, create_tmp_var_name ("span"),
1033248c 1458 gfc_array_index_type);
1459 gfc_finish_var_decl (span, sym);
6180d82a 1460 TREE_STATIC (span) = TREE_STATIC (decl);
1461 DECL_ARTIFICIAL (span) = 1;
1033248c 1462
1463 GFC_DECL_SPAN (decl) = span;
6180d82a 1464 GFC_TYPE_ARRAY_SPAN (TREE_TYPE (decl)) = span;
1033248c 1465 }
1466
fd23cc08 1467 if (sym->ts.type == BT_CLASS)
1468 GFC_DECL_CLASS(decl) = 1;
1469
4ee9c684 1470 sym->backend_decl = decl;
1471
c8f1568f 1472 if (sym->attr.assign)
21ebda4d 1473 gfc_add_assign_aux_vars (sym);
c8f1568f 1474
cd2c99b8 1475 if (intrinsic_array_parameter)
1476 {
1477 TREE_STATIC (decl) = 1;
1478 DECL_EXTERNAL (decl) = 0;
1479 }
1480
1481 if (TREE_STATIC (decl)
1482 && !(sym->attr.use_assoc && !intrinsic_array_parameter)
a545a8f8 1483 && (sym->attr.save || sym->ns->proc_name->attr.is_main_program
1484 || gfc_option.flag_max_stack_var_size == 0
a961ca30 1485 || sym->attr.data || sym->ns->proc_name->attr.flavor == FL_MODULE)
0d3bb1de 1486 && (gfc_option.coarray != GFC_FCOARRAY_LIB
1487 || !sym->attr.codimension || sym->attr.allocatable))
a545a8f8 1488 {
1489 /* Add static initializer. For procedures, it is only needed if
1490 SAVE is specified otherwise they need to be reinitialized
1491 every time the procedure is entered. The TREE_STATIC is
1492 in this case due to -fmax-stack-var-size=. */
bda1f152 1493 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
802532b9 1494 TREE_TYPE (decl),
0d3bb1de 1495 sym->attr.dimension
1496 || (sym->attr.codimension
1497 && sym->attr.allocatable),
802532b9 1498 sym->attr.pointer
1499 || sym->attr.allocatable,
1500 sym->attr.proc_pointer);
bda1f152 1501 }
1502
9579733e 1503 if (!TREE_STATIC (decl)
1504 && POINTER_TYPE_P (TREE_TYPE (decl))
1505 && !sym->attr.pointer
1506 && !sym->attr.allocatable
bc5663df 1507 && !sym->attr.proc_pointer
1508 && !sym->attr.select_type_temporary)
9579733e 1509 DECL_BY_REFERENCE (decl) = 1;
1510
ebad7c3e 1511 if (sym->attr.vtab
1512 || (sym->name[0] == '_' && strncmp ("__def_init", sym->name, 10) == 0))
4c197fd0 1513 TREE_READONLY (decl) = 1;
ebad7c3e 1514
4ee9c684 1515 return decl;
1516}
1517
1518
dbe60343 1519/* Substitute a temporary variable in place of the real one. */
1520
1521void
1522gfc_shadow_sym (gfc_symbol * sym, tree decl, gfc_saved_var * save)
1523{
1524 save->attr = sym->attr;
1525 save->decl = sym->backend_decl;
1526
1527 gfc_clear_attr (&sym->attr);
1528 sym->attr.referenced = 1;
1529 sym->attr.flavor = FL_VARIABLE;
1530
1531 sym->backend_decl = decl;
1532}
1533
1534
1535/* Restore the original variable. */
1536
1537void
1538gfc_restore_sym (gfc_symbol * sym, gfc_saved_var * save)
1539{
1540 sym->attr = save->attr;
1541 sym->backend_decl = save->decl;
1542}
1543
1544
cad0ddcf 1545/* Declare a procedure pointer. */
1546
1547static tree
1548get_proc_pointer_decl (gfc_symbol *sym)
1549{
1550 tree decl;
36b0a1b0 1551 tree attributes;
cad0ddcf 1552
1553 decl = sym->backend_decl;
1554 if (decl)
1555 return decl;
1556
e60a6f7b 1557 decl = build_decl (input_location,
1558 VAR_DECL, get_identifier (sym->name),
cad0ddcf 1559 build_pointer_type (gfc_get_function_type (sym)));
1560
ea6f8461 1561 if (sym->module)
1562 {
1563 /* Apply name mangling. */
1564 gfc_set_decl_assembler_name (decl, gfc_sym_mangled_identifier (sym));
1565 if (sym->attr.use_assoc)
1566 DECL_IGNORED_P (decl) = 1;
1567 }
a90fe829 1568
e72f979a 1569 if ((sym->ns->proc_name
1570 && sym->ns->proc_name->backend_decl == current_function_decl)
cad0ddcf 1571 || sym->attr.contained)
1572 gfc_add_decl_to_function (decl);
cf269acc 1573 else if (sym->ns->proc_name->attr.flavor != FL_MODULE)
cad0ddcf 1574 gfc_add_decl_to_parent_function (decl);
1575
1576 sym->backend_decl = decl;
1577
cf269acc 1578 /* If a variable is USE associated, it's always external. */
1579 if (sym->attr.use_assoc)
1580 {
1581 DECL_EXTERNAL (decl) = 1;
1582 TREE_PUBLIC (decl) = 1;
1583 }
1584 else if (sym->module && sym->ns->proc_name->attr.flavor == FL_MODULE)
1585 {
1586 /* This is the declaration of a module variable. */
1587 TREE_PUBLIC (decl) = 1;
1588 TREE_STATIC (decl) = 1;
1589 }
1590
cad0ddcf 1591 if (!sym->attr.use_assoc
1592 && (sym->attr.save != SAVE_NONE || sym->attr.data
1593 || (sym->value && sym->ns->proc_name->attr.is_main_program)))
1594 TREE_STATIC (decl) = 1;
1595
1596 if (TREE_STATIC (decl) && sym->value)
1597 {
1598 /* Add static initializer. */
1599 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
802532b9 1600 TREE_TYPE (decl),
1601 sym->attr.dimension,
1602 false, true);
cad0ddcf 1603 }
1604
8fb1768c 1605 /* Handle threadprivate procedure pointers. */
1606 if (sym->attr.threadprivate
1607 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1608 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
1609
36b0a1b0 1610 attributes = add_attributes_to_decl (sym->attr, NULL_TREE);
1611 decl_attributes (&decl, attributes, 0);
1612
cad0ddcf 1613 return decl;
1614}
1615
1616
4ee9c684 1617/* Get a basic decl for an external function. */
1618
1619tree
1620gfc_get_extern_function_decl (gfc_symbol * sym)
1621{
1622 tree type;
1623 tree fndecl;
36b0a1b0 1624 tree attributes;
4ee9c684 1625 gfc_expr e;
1626 gfc_intrinsic_sym *isym;
1627 gfc_expr argexpr;
17000b91 1628 char s[GFC_MAX_SYMBOL_LEN + 23]; /* "_gfortran_f2c_specific" and '\0'. */
4ee9c684 1629 tree name;
1630 tree mangled_name;
83aeedb9 1631 gfc_gsymbol *gsym;
4ee9c684 1632
1633 if (sym->backend_decl)
1634 return sym->backend_decl;
1635
1b716045 1636 /* We should never be creating external decls for alternate entry points.
1637 The procedure may be an alternate entry point, but we don't want/need
1638 to know that. */
22d678e8 1639 gcc_assert (!(sym->attr.entry || sym->attr.entry_master));
1b716045 1640
cad0ddcf 1641 if (sym->attr.proc_pointer)
1642 return get_proc_pointer_decl (sym);
1643
83aeedb9 1644 /* See if this is an external procedure from the same file. If so,
1645 return the backend_decl. */
da5c730d 1646 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label
1647 ? sym->binding_label : sym->name);
83aeedb9 1648
c8b913ab 1649 if (gsym && !gsym->defined)
1650 gsym = NULL;
1651
1652 /* This can happen because of C binding. */
1653 if (gsym && gsym->ns && gsym->ns->proc_name
1654 && gsym->ns->proc_name->attr.flavor == FL_MODULE)
1655 goto module_sym;
1656
044bbd71 1657 if ((!sym->attr.use_assoc || sym->attr.if_source != IFSRC_DECL)
1658 && !sym->backend_decl
1659 && gsym && gsym->ns
1660 && ((gsym->type == GSYM_SUBROUTINE) || (gsym->type == GSYM_FUNCTION))
1661 && (gsym->ns->proc_name->backend_decl || !sym->attr.intrinsic))
83aeedb9 1662 {
d896f9b3 1663 if (!gsym->ns->proc_name->backend_decl)
1664 {
1665 /* By construction, the external function cannot be
1666 a contained procedure. */
1667 locus old_loc;
d896f9b3 1668
4671339c 1669 gfc_save_backend_locus (&old_loc);
9078126c 1670 push_cfun (NULL);
d896f9b3 1671
1672 gfc_create_function_decl (gsym->ns, true);
1673
1674 pop_cfun ();
4671339c 1675 gfc_restore_backend_locus (&old_loc);
d896f9b3 1676 }
1677
83aeedb9 1678 /* If the namespace has entries, the proc_name is the
1679 entry master. Find the entry and use its backend_decl.
1680 otherwise, use the proc_name backend_decl. */
1681 if (gsym->ns->entries)
1682 {
1683 gfc_entry_list *entry = gsym->ns->entries;
1684
1685 for (; entry; entry = entry->next)
1686 {
1687 if (strcmp (gsym->name, entry->sym->name) == 0)
1688 {
1689 sym->backend_decl = entry->sym->backend_decl;
1690 break;
1691 }
1692 }
1693 }
1694 else
40c74b02 1695 sym->backend_decl = gsym->ns->proc_name->backend_decl;
83aeedb9 1696
1697 if (sym->backend_decl)
40c74b02 1698 {
1699 /* Avoid problems of double deallocation of the backend declaration
1700 later in gfc_trans_use_stmts; cf. PR 45087. */
1701 if (sym->attr.if_source != IFSRC_DECL && sym->attr.use_assoc)
1702 sym->attr.use_assoc = 0;
1703
1704 return sym->backend_decl;
1705 }
83aeedb9 1706 }
1707
7ea64434 1708 /* See if this is a module procedure from the same file. If so,
1709 return the backend_decl. */
1710 if (sym->module)
1711 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->module);
1712
c8b913ab 1713module_sym:
1714 if (gsym && gsym->ns
1715 && (gsym->type == GSYM_MODULE
1716 || (gsym->ns->proc_name && gsym->ns->proc_name->attr.flavor == FL_MODULE)))
7ea64434 1717 {
1718 gfc_symbol *s;
1719
1720 s = NULL;
c8b913ab 1721 if (gsym->type == GSYM_MODULE)
1722 gfc_find_symbol (sym->name, gsym->ns, 0, &s);
1723 else
1724 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &s);
1725
7ea64434 1726 if (s && s->backend_decl)
1727 {
85ec2f13 1728 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
1729 gfc_copy_dt_decls_ifequal (s->ts.u.derived, sym->ts.u.derived,
1730 true);
1731 else if (sym->ts.type == BT_CHARACTER)
1732 sym->ts.u.cl->backend_decl = s->ts.u.cl->backend_decl;
7ea64434 1733 sym->backend_decl = s->backend_decl;
1734 return sym->backend_decl;
1735 }
1736 }
1737
4ee9c684 1738 if (sym->attr.intrinsic)
1739 {
1740 /* Call the resolution function to get the actual name. This is
1741 a nasty hack which relies on the resolution functions only looking
1742 at the first argument. We pass NULL for the second argument
1743 otherwise things like AINT get confused. */
1744 isym = gfc_find_function (sym->name);
22d678e8 1745 gcc_assert (isym->resolve.f0 != NULL);
4ee9c684 1746
1747 memset (&e, 0, sizeof (e));
1748 e.expr_type = EXPR_FUNCTION;
1749
1750 memset (&argexpr, 0, sizeof (argexpr));
22d678e8 1751 gcc_assert (isym->formal);
4ee9c684 1752 argexpr.ts = isym->formal->ts;
1753
1754 if (isym->formal->next == NULL)
1755 isym->resolve.f1 (&e, &argexpr);
1756 else
1757 {
37e0271a 1758 if (isym->formal->next->next == NULL)
1759 isym->resolve.f2 (&e, &argexpr, NULL);
1760 else
1761 {
7fe55cc9 1762 if (isym->formal->next->next->next == NULL)
1763 isym->resolve.f3 (&e, &argexpr, NULL, NULL);
1764 else
1765 {
1766 /* All specific intrinsics take less than 5 arguments. */
1767 gcc_assert (isym->formal->next->next->next->next == NULL);
1768 isym->resolve.f4 (&e, &argexpr, NULL, NULL, NULL);
1769 }
37e0271a 1770 }
4ee9c684 1771 }
bdaed7d2 1772
1773 if (gfc_option.flag_f2c
1774 && ((e.ts.type == BT_REAL && e.ts.kind == gfc_default_real_kind)
1775 || e.ts.type == BT_COMPLEX))
1776 {
1777 /* Specific which needs a different implementation if f2c
1778 calling conventions are used. */
17000b91 1779 sprintf (s, "_gfortran_f2c_specific%s", e.value.function.name);
bdaed7d2 1780 }
1781 else
17000b91 1782 sprintf (s, "_gfortran_specific%s", e.value.function.name);
bdaed7d2 1783
4ee9c684 1784 name = get_identifier (s);
1785 mangled_name = name;
1786 }
1787 else
1788 {
1789 name = gfc_sym_identifier (sym);
1790 mangled_name = gfc_sym_mangled_function_id (sym);
1791 }
1792
1793 type = gfc_get_function_type (sym);
e60a6f7b 1794 fndecl = build_decl (input_location,
1795 FUNCTION_DECL, name, type);
4ee9c684 1796
e27454ee 1797 /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
1798 TREE_PUBLIC specifies whether a function is globally addressable (i.e.
851d9296 1799 the opposite of declaring a function as static in C). */
e27454ee 1800 DECL_EXTERNAL (fndecl) = 1;
1801 TREE_PUBLIC (fndecl) = 1;
1802
1236e28b 1803 attributes = add_attributes_to_decl (sym->attr, NULL_TREE);
1804 decl_attributes (&fndecl, attributes, 0);
1805
1806 gfc_set_decl_assembler_name (fndecl, mangled_name);
4ee9c684 1807
1808 /* Set the context of this decl. */
1809 if (0 && sym->ns && sym->ns->proc_name)
1810 {
1811 /* TODO: Add external decls to the appropriate scope. */
1812 DECL_CONTEXT (fndecl) = sym->ns->proc_name->backend_decl;
1813 }
1814 else
1815 {
f888a3fb 1816 /* Global declaration, e.g. intrinsic subroutine. */
4ee9c684 1817 DECL_CONTEXT (fndecl) = NULL_TREE;
1818 }
1819
4ee9c684 1820 /* Set attributes for PURE functions. A call to PURE function in the
1821 Fortran 95 sense is both pure and without side effects in the C
1822 sense. */
bead0399 1823 if (sym->attr.pure || sym->attr.implicit_pure)
4ee9c684 1824 {
4d4b9f0e 1825 if (sym->attr.function && !gfc_return_by_reference (sym))
9c2a0c05 1826 DECL_PURE_P (fndecl) = 1;
be393645 1827 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
1828 parameters and don't use alternate returns (is this
1829 allowed?). In that case, calls to them are meaningless, and
1b716045 1830 can be optimized away. See also in build_function_decl(). */
be393645 1831 TREE_SIDE_EFFECTS (fndecl) = 0;
4ee9c684 1832 }
1833
6e27d773 1834 /* Mark non-returning functions. */
1835 if (sym->attr.noreturn)
1836 TREE_THIS_VOLATILE(fndecl) = 1;
1837
4ee9c684 1838 sym->backend_decl = fndecl;
1839
1840 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1841 pushdecl_top_level (fndecl);
1842
1843 return fndecl;
1844}
1845
1846
1847/* Create a declaration for a procedure. For external functions (in the C
1b716045 1848 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
1849 a master function with alternate entry points. */
4ee9c684 1850
1b716045 1851static void
d896f9b3 1852build_function_decl (gfc_symbol * sym, bool global)
4ee9c684 1853{
36b0a1b0 1854 tree fndecl, type, attributes;
4ee9c684 1855 symbol_attribute attr;
1b716045 1856 tree result_decl;
4ee9c684 1857 gfc_formal_arglist *f;
1858
22d678e8 1859 gcc_assert (!sym->attr.external);
4ee9c684 1860
802532b9 1861 if (sym->backend_decl)
1862 return;
1863
b31f705b 1864 /* Set the line and filename. sym->declared_at seems to point to the
1865 last statement for subroutines, but it'll do for now. */
1866 gfc_set_backend_locus (&sym->declared_at);
1867
4ee9c684 1868 /* Allow only one nesting level. Allow public declarations. */
22d678e8 1869 gcc_assert (current_function_decl == NULL_TREE
16a40513 1870 || DECL_FILE_SCOPE_P (current_function_decl)
1871 || (TREE_CODE (DECL_CONTEXT (current_function_decl))
1872 == NAMESPACE_DECL));
4ee9c684 1873
1874 type = gfc_get_function_type (sym);
e60a6f7b 1875 fndecl = build_decl (input_location,
1876 FUNCTION_DECL, gfc_sym_identifier (sym), type);
4ee9c684 1877
1236e28b 1878 attr = sym->attr;
1879
e27454ee 1880 /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
1881 TREE_PUBLIC specifies whether a function is globally addressable (i.e.
851d9296 1882 the opposite of declaring a function as static in C). */
e27454ee 1883 DECL_EXTERNAL (fndecl) = 0;
1884
7d6b5754 1885 if (sym->attr.access == ACCESS_UNKNOWN && sym->module
1886 && (sym->ns->default_access == ACCESS_PRIVATE
1887 || (sym->ns->default_access == ACCESS_UNKNOWN
1888 && gfc_option.flag_module_private)))
1889 sym->attr.access = ACCESS_PRIVATE;
1890
e27454ee 1891 if (!current_function_decl
c5cb468c 1892 && !sym->attr.entry_master && !sym->attr.is_main_program
b1f74325 1893 && (sym->attr.access != ACCESS_PRIVATE || sym->binding_label
1894 || sym->attr.public_used))
e27454ee 1895 TREE_PUBLIC (fndecl) = 1;
1896
9e45357a 1897 if (sym->attr.referenced || sym->attr.entry_master)
1898 TREE_USED (fndecl) = 1;
1899
1236e28b 1900 attributes = add_attributes_to_decl (attr, NULL_TREE);
1901 decl_attributes (&fndecl, attributes, 0);
1902
4ee9c684 1903 /* Figure out the return type of the declared function, and build a
f888a3fb 1904 RESULT_DECL for it. If this is a subroutine with alternate
4ee9c684 1905 returns, build a RESULT_DECL for it. */
4ee9c684 1906 result_decl = NULL_TREE;
1907 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
1908 if (attr.function)
1909 {
1910 if (gfc_return_by_reference (sym))
1911 type = void_type_node;
1912 else
1913 {
1914 if (sym->result != sym)
1915 result_decl = gfc_sym_identifier (sym->result);
1916
1917 type = TREE_TYPE (TREE_TYPE (fndecl));
1918 }
1919 }
1920 else
1921 {
1922 /* Look for alternate return placeholders. */
1923 int has_alternate_returns = 0;
6777213b 1924 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
4ee9c684 1925 {
1926 if (f->sym == NULL)
1927 {
1928 has_alternate_returns = 1;
1929 break;
1930 }
1931 }
1932
1933 if (has_alternate_returns)
1934 type = integer_type_node;
1935 else
1936 type = void_type_node;
1937 }
1938
e60a6f7b 1939 result_decl = build_decl (input_location,
1940 RESULT_DECL, result_decl, type);
540edea7 1941 DECL_ARTIFICIAL (result_decl) = 1;
1942 DECL_IGNORED_P (result_decl) = 1;
4ee9c684 1943 DECL_CONTEXT (result_decl) = fndecl;
1944 DECL_RESULT (fndecl) = result_decl;
1945
1946 /* Don't call layout_decl for a RESULT_DECL.
f888a3fb 1947 layout_decl (result_decl, 0); */
4ee9c684 1948
4ee9c684 1949 /* TREE_STATIC means the function body is defined here. */
e4b2c26c 1950 TREE_STATIC (fndecl) = 1;
4ee9c684 1951
f888a3fb 1952 /* Set attributes for PURE functions. A call to a PURE function in the
4ee9c684 1953 Fortran 95 sense is both pure and without side effects in the C
1954 sense. */
bead0399 1955 if (attr.pure || attr.implicit_pure)
4ee9c684 1956 {
be393645 1957 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
a0527218 1958 including an alternate return. In that case it can also be
231e961a 1959 marked as PURE. See also in gfc_get_extern_function_decl(). */
4c319962 1960 if (attr.function && !gfc_return_by_reference (sym))
9c2a0c05 1961 DECL_PURE_P (fndecl) = 1;
4ee9c684 1962 TREE_SIDE_EFFECTS (fndecl) = 0;
1963 }
1964
36b0a1b0 1965
4ee9c684 1966 /* Layout the function declaration and put it in the binding level
1967 of the current function. */
d896f9b3 1968
4c197fd0 1969 if (global)
d896f9b3 1970 pushdecl_top_level (fndecl);
1971 else
1972 pushdecl (fndecl);
1b716045 1973
16a40513 1974 /* Perform name mangling if this is a top level or module procedure. */
1975 if (current_function_decl == NULL_TREE)
1976 gfc_set_decl_assembler_name (fndecl, gfc_sym_mangled_function_id (sym));
1977
1b716045 1978 sym->backend_decl = fndecl;
1979}
1980
1981
1982/* Create the DECL_ARGUMENTS for a procedure. */
1983
1984static void
1985create_function_arglist (gfc_symbol * sym)
1986{
1987 tree fndecl;
1988 gfc_formal_arglist *f;
d4163395 1989 tree typelist, hidden_typelist;
1990 tree arglist, hidden_arglist;
1b716045 1991 tree type;
1992 tree parm;
1993
1994 fndecl = sym->backend_decl;
1995
e4b2c26c 1996 /* Build formal argument list. Make sure that their TREE_CONTEXT is
1997 the new FUNCTION_DECL node. */
e4b2c26c 1998 arglist = NULL_TREE;
d4163395 1999 hidden_arglist = NULL_TREE;
e4b2c26c 2000 typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1b716045 2001
2002 if (sym->attr.entry_master)
2003 {
2004 type = TREE_VALUE (typelist);
e60a6f7b 2005 parm = build_decl (input_location,
2006 PARM_DECL, get_identifier ("__entry"), type);
a90fe829 2007
1b716045 2008 DECL_CONTEXT (parm) = fndecl;
2009 DECL_ARG_TYPE (parm) = type;
2010 TREE_READONLY (parm) = 1;
b9c7fce7 2011 gfc_finish_decl (parm);
d95efb59 2012 DECL_ARTIFICIAL (parm) = 1;
1b716045 2013
2014 arglist = chainon (arglist, parm);
2015 typelist = TREE_CHAIN (typelist);
2016 }
2017
e4b2c26c 2018 if (gfc_return_by_reference (sym))
4ee9c684 2019 {
d4163395 2020 tree type = TREE_VALUE (typelist), length = NULL;
4ee9c684 2021
e4b2c26c 2022 if (sym->ts.type == BT_CHARACTER)
2023 {
e4b2c26c 2024 /* Length of character result. */
d4163395 2025 tree len_type = TREE_VALUE (TREE_CHAIN (typelist));
4ee9c684 2026
e60a6f7b 2027 length = build_decl (input_location,
2028 PARM_DECL,
e4b2c26c 2029 get_identifier (".__result"),
d4163395 2030 len_type);
eeebe20b 2031 if (!sym->ts.u.cl->length)
e4b2c26c 2032 {
eeebe20b 2033 sym->ts.u.cl->backend_decl = length;
e4b2c26c 2034 TREE_USED (length) = 1;
4ee9c684 2035 }
22d678e8 2036 gcc_assert (TREE_CODE (length) == PARM_DECL);
e4b2c26c 2037 DECL_CONTEXT (length) = fndecl;
d4163395 2038 DECL_ARG_TYPE (length) = len_type;
e4b2c26c 2039 TREE_READONLY (length) = 1;
b5b40b3f 2040 DECL_ARTIFICIAL (length) = 1;
b9c7fce7 2041 gfc_finish_decl (length);
eeebe20b 2042 if (sym->ts.u.cl->backend_decl == NULL
2043 || sym->ts.u.cl->backend_decl == length)
d4163395 2044 {
2045 gfc_symbol *arg;
2046 tree backend_decl;
4ee9c684 2047
eeebe20b 2048 if (sym->ts.u.cl->backend_decl == NULL)
d4163395 2049 {
e60a6f7b 2050 tree len = build_decl (input_location,
2051 VAR_DECL,
d4163395 2052 get_identifier ("..__result"),
2053 gfc_charlen_type_node);
2054 DECL_ARTIFICIAL (len) = 1;
2055 TREE_USED (len) = 1;
eeebe20b 2056 sym->ts.u.cl->backend_decl = len;
d4163395 2057 }
4ee9c684 2058
d4163395 2059 /* Make sure PARM_DECL type doesn't point to incomplete type. */
2060 arg = sym->result ? sym->result : sym;
2061 backend_decl = arg->backend_decl;
2062 /* Temporary clear it, so that gfc_sym_type creates complete
2063 type. */
2064 arg->backend_decl = NULL;
2065 type = gfc_sym_type (arg);
2066 arg->backend_decl = backend_decl;
2067 type = build_reference_type (type);
2068 }
2069 }
4ee9c684 2070
e60a6f7b 2071 parm = build_decl (input_location,
2072 PARM_DECL, get_identifier ("__result"), type);
4ee9c684 2073
d4163395 2074 DECL_CONTEXT (parm) = fndecl;
2075 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
2076 TREE_READONLY (parm) = 1;
2077 DECL_ARTIFICIAL (parm) = 1;
b9c7fce7 2078 gfc_finish_decl (parm);
4ee9c684 2079
d4163395 2080 arglist = chainon (arglist, parm);
2081 typelist = TREE_CHAIN (typelist);
4ee9c684 2082
d4163395 2083 if (sym->ts.type == BT_CHARACTER)
2084 {
2085 gfc_allocate_lang_decl (parm);
2086 arglist = chainon (arglist, length);
e4b2c26c 2087 typelist = TREE_CHAIN (typelist);
2088 }
2089 }
4ee9c684 2090
d4163395 2091 hidden_typelist = typelist;
6777213b 2092 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
d4163395 2093 if (f->sym != NULL) /* Ignore alternate returns. */
2094 hidden_typelist = TREE_CHAIN (hidden_typelist);
2095
6777213b 2096 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
e4b2c26c 2097 {
2098 char name[GFC_MAX_SYMBOL_LEN + 2];
d4163395 2099
e4b2c26c 2100 /* Ignore alternate returns. */
2101 if (f->sym == NULL)
2102 continue;
4ee9c684 2103
e4b2c26c 2104 type = TREE_VALUE (typelist);
4ee9c684 2105
296db1d1 2106 if (f->sym->ts.type == BT_CHARACTER
2107 && (!sym->attr.is_bind_c || sym->attr.entry_master))
d4163395 2108 {
2109 tree len_type = TREE_VALUE (hidden_typelist);
2110 tree length = NULL_TREE;
617125a6 2111 if (!f->sym->ts.deferred)
2112 gcc_assert (len_type == gfc_charlen_type_node);
2113 else
2114 gcc_assert (POINTER_TYPE_P (len_type));
d4163395 2115
2116 strcpy (&name[1], f->sym->name);
2117 name[0] = '_';
e60a6f7b 2118 length = build_decl (input_location,
2119 PARM_DECL, get_identifier (name), len_type);
4ee9c684 2120
d4163395 2121 hidden_arglist = chainon (hidden_arglist, length);
2122 DECL_CONTEXT (length) = fndecl;
2123 DECL_ARTIFICIAL (length) = 1;
2124 DECL_ARG_TYPE (length) = len_type;
2125 TREE_READONLY (length) = 1;
b9c7fce7 2126 gfc_finish_decl (length);
4ee9c684 2127
a4abf8a0 2128 /* Remember the passed value. */
a90fe829 2129 if (!f->sym->ts.u.cl || f->sym->ts.u.cl->passed_length)
6be7c32c 2130 {
2131 /* This can happen if the same type is used for multiple
2132 arguments. We need to copy cl as otherwise
2133 cl->passed_length gets overwritten. */
d270ce52 2134 f->sym->ts.u.cl = gfc_new_charlen (f->sym->ns, f->sym->ts.u.cl);
6be7c32c 2135 }
eeebe20b 2136 f->sym->ts.u.cl->passed_length = length;
4ee9c684 2137
d4163395 2138 /* Use the passed value for assumed length variables. */
eeebe20b 2139 if (!f->sym->ts.u.cl->length)
4ee9c684 2140 {
d4163395 2141 TREE_USED (length) = 1;
eeebe20b 2142 gcc_assert (!f->sym->ts.u.cl->backend_decl);
2143 f->sym->ts.u.cl->backend_decl = length;
d4163395 2144 }
2145
2146 hidden_typelist = TREE_CHAIN (hidden_typelist);
2147
eeebe20b 2148 if (f->sym->ts.u.cl->backend_decl == NULL
2149 || f->sym->ts.u.cl->backend_decl == length)
d4163395 2150 {
eeebe20b 2151 if (f->sym->ts.u.cl->backend_decl == NULL)
d4163395 2152 gfc_create_string_length (f->sym);
2153
2154 /* Make sure PARM_DECL type doesn't point to incomplete type. */
2155 if (f->sym->attr.flavor == FL_PROCEDURE)
2156 type = build_pointer_type (gfc_get_function_type (f->sym));
2157 else
2158 type = gfc_sym_type (f->sym);
4ee9c684 2159 }
4ee9c684 2160 }
532c2d79 2161 /* For noncharacter scalar intrinsic types, VALUE passes the value,
2162 hence, the optional status cannot be transfered via a NULL pointer.
2163 Thus, we will use a hidden argument in that case. */
2164 else if (f->sym->attr.optional && f->sym->attr.value
68cc384e 2165 && !f->sym->attr.dimension && f->sym->ts.type != BT_CLASS
532c2d79 2166 && f->sym->ts.type != BT_DERIVED)
2167 {
2168 tree tmp;
2169 strcpy (&name[1], f->sym->name);
2170 name[0] = '_';
2171 tmp = build_decl (input_location,
2172 PARM_DECL, get_identifier (name),
2173 boolean_type_node);
2174
2175 hidden_arglist = chainon (hidden_arglist, tmp);
2176 DECL_CONTEXT (tmp) = fndecl;
2177 DECL_ARTIFICIAL (tmp) = 1;
2178 DECL_ARG_TYPE (tmp) = boolean_type_node;
2179 TREE_READONLY (tmp) = 1;
2180 gfc_finish_decl (tmp);
2181 }
4ee9c684 2182
d4163395 2183 /* For non-constant length array arguments, make sure they use
2184 a different type node from TYPE_ARG_TYPES type. */
2185 if (f->sym->attr.dimension
2186 && type == TREE_VALUE (typelist)
2187 && TREE_CODE (type) == POINTER_TYPE
2188 && GFC_ARRAY_TYPE_P (type)
2189 && f->sym->as->type != AS_ASSUMED_SIZE
2190 && ! COMPLETE_TYPE_P (TREE_TYPE (type)))
2191 {
2192 if (f->sym->attr.flavor == FL_PROCEDURE)
2193 type = build_pointer_type (gfc_get_function_type (f->sym));
2194 else
2195 type = gfc_sym_type (f->sym);
2196 }
2197
cad0ddcf 2198 if (f->sym->attr.proc_pointer)
2199 type = build_pointer_type (type);
2200
2364aa60 2201 if (f->sym->attr.volatile_)
2202 type = build_qualified_type (type, TYPE_QUAL_VOLATILE);
2203
69b1505f 2204 /* Build the argument declaration. */
e60a6f7b 2205 parm = build_decl (input_location,
2206 PARM_DECL, gfc_sym_identifier (f->sym), type);
d4163395 2207
2364aa60 2208 if (f->sym->attr.volatile_)
2209 {
2210 TREE_THIS_VOLATILE (parm) = 1;
2211 TREE_SIDE_EFFECTS (parm) = 1;
2212 }
2213
d4163395 2214 /* Fill in arg stuff. */
2215 DECL_CONTEXT (parm) = fndecl;
2216 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
2217 /* All implementation args are read-only. */
2218 TREE_READONLY (parm) = 1;
98923a84 2219 if (POINTER_TYPE_P (type)
2220 && (!f->sym->attr.proc_pointer
2221 && f->sym->attr.flavor != FL_PROCEDURE))
2222 DECL_BY_REFERENCE (parm) = 1;
d4163395 2223
b9c7fce7 2224 gfc_finish_decl (parm);
d4163395 2225
2226 f->sym->backend_decl = parm;
2227
7dce33fe 2228 /* Coarrays which are descriptorless or assumed-shape pass with
2229 -fcoarray=lib the token and the offset as hidden arguments. */
85c94a64 2230 if (f->sym->attr.codimension
2231 && gfc_option.coarray == GFC_FCOARRAY_LIB
7dce33fe 2232 && !f->sym->attr.allocatable)
85c94a64 2233 {
2234 tree caf_type;
2235 tree token;
2236 tree offset;
2237
2238 gcc_assert (f->sym->backend_decl != NULL_TREE
2239 && !sym->attr.is_bind_c);
2240 caf_type = TREE_TYPE (f->sym->backend_decl);
2241
85c94a64 2242 token = build_decl (input_location, PARM_DECL,
2243 create_tmp_var_name ("caf_token"),
2244 build_qualified_type (pvoid_type_node,
2245 TYPE_QUAL_RESTRICT));
7dce33fe 2246 if (f->sym->as->type == AS_ASSUMED_SHAPE)
2247 {
2248 gcc_assert (DECL_LANG_SPECIFIC (f->sym->backend_decl) == NULL
2249 || GFC_DECL_TOKEN (f->sym->backend_decl) == NULL_TREE);
2250 if (DECL_LANG_SPECIFIC (f->sym->backend_decl) == NULL)
2251 gfc_allocate_lang_decl (f->sym->backend_decl);
2252 GFC_DECL_TOKEN (f->sym->backend_decl) = token;
2253 }
2254 else
2255 {
2256 gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) == NULL_TREE);
2257 GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) = token;
2258 }
a90fe829 2259
85c94a64 2260 DECL_CONTEXT (token) = fndecl;
2261 DECL_ARTIFICIAL (token) = 1;
2262 DECL_ARG_TYPE (token) = TREE_VALUE (typelist);
2263 TREE_READONLY (token) = 1;
2264 hidden_arglist = chainon (hidden_arglist, token);
2265 gfc_finish_decl (token);
2266
85c94a64 2267 offset = build_decl (input_location, PARM_DECL,
2268 create_tmp_var_name ("caf_offset"),
2269 gfc_array_index_type);
2270
7dce33fe 2271 if (f->sym->as->type == AS_ASSUMED_SHAPE)
2272 {
2273 gcc_assert (GFC_DECL_CAF_OFFSET (f->sym->backend_decl)
2274 == NULL_TREE);
2275 GFC_DECL_CAF_OFFSET (f->sym->backend_decl) = offset;
2276 }
2277 else
2278 {
2279 gcc_assert (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) == NULL_TREE);
2280 GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) = offset;
2281 }
85c94a64 2282 DECL_CONTEXT (offset) = fndecl;
2283 DECL_ARTIFICIAL (offset) = 1;
2284 DECL_ARG_TYPE (offset) = TREE_VALUE (typelist);
2285 TREE_READONLY (offset) = 1;
2286 hidden_arglist = chainon (hidden_arglist, offset);
2287 gfc_finish_decl (offset);
2288 }
2289
d4163395 2290 arglist = chainon (arglist, parm);
e4b2c26c 2291 typelist = TREE_CHAIN (typelist);
4ee9c684 2292 }
e4b2c26c 2293
465e4a95 2294 /* Add the hidden string length parameters, unless the procedure
2295 is bind(C). */
2296 if (!sym->attr.is_bind_c)
2297 arglist = chainon (arglist, hidden_arglist);
d4163395 2298
ebe27ea2 2299 gcc_assert (hidden_typelist == NULL_TREE
2300 || TREE_VALUE (hidden_typelist) == void_type_node);
e4b2c26c 2301 DECL_ARGUMENTS (fndecl) = arglist;
1b716045 2302}
e4b2c26c 2303
1b716045 2304/* Do the setup necessary before generating the body of a function. */
2305
2306static void
2307trans_function_start (gfc_symbol * sym)
2308{
2309 tree fndecl;
2310
2311 fndecl = sym->backend_decl;
2312
f888a3fb 2313 /* Let GCC know the current scope is this function. */
1b716045 2314 current_function_decl = fndecl;
2315
f888a3fb 2316 /* Let the world know what we're about to do. */
1b716045 2317 announce_function (fndecl);
2318
16a40513 2319 if (DECL_FILE_SCOPE_P (fndecl))
1b716045 2320 {
f888a3fb 2321 /* Create RTL for function declaration. */
1b716045 2322 rest_of_decl_compilation (fndecl, 1, 0);
2323 }
2324
f888a3fb 2325 /* Create RTL for function definition. */
1b716045 2326 make_decl_rtl (fndecl);
2327
00cf115c 2328 allocate_struct_function (fndecl, false);
1b716045 2329
f888a3fb 2330 /* function.c requires a push at the start of the function. */
cde2be84 2331 pushlevel ();
1b716045 2332}
2333
2334/* Create thunks for alternate entry points. */
2335
2336static void
d896f9b3 2337build_entry_thunks (gfc_namespace * ns, bool global)
1b716045 2338{
2339 gfc_formal_arglist *formal;
2340 gfc_formal_arglist *thunk_formal;
2341 gfc_entry_list *el;
2342 gfc_symbol *thunk_sym;
2343 stmtblock_t body;
2344 tree thunk_fndecl;
1b716045 2345 tree tmp;
b31f705b 2346 locus old_loc;
1b716045 2347
2348 /* This should always be a toplevel function. */
22d678e8 2349 gcc_assert (current_function_decl == NULL_TREE);
1b716045 2350
4671339c 2351 gfc_save_backend_locus (&old_loc);
1b716045 2352 for (el = ns->entries; el; el = el->next)
2353 {
f1f41a6c 2354 vec<tree, va_gc> *args = NULL;
2355 vec<tree, va_gc> *string_args = NULL;
414c3a2c 2356
1b716045 2357 thunk_sym = el->sym;
a90fe829 2358
d896f9b3 2359 build_function_decl (thunk_sym, global);
1b716045 2360 create_function_arglist (thunk_sym);
2361
2362 trans_function_start (thunk_sym);
2363
2364 thunk_fndecl = thunk_sym->backend_decl;
2365
e5004242 2366 gfc_init_block (&body);
1b716045 2367
f888a3fb 2368 /* Pass extra parameter identifying this entry point. */
7016c612 2369 tmp = build_int_cst (gfc_array_index_type, el->id);
f1f41a6c 2370 vec_safe_push (args, tmp);
1b716045 2371
c6871095 2372 if (thunk_sym->attr.function)
2373 {
2374 if (gfc_return_by_reference (ns->proc_name))
2375 {
2376 tree ref = DECL_ARGUMENTS (current_function_decl);
f1f41a6c 2377 vec_safe_push (args, ref);
c6871095 2378 if (ns->proc_name->ts.type == BT_CHARACTER)
f1f41a6c 2379 vec_safe_push (args, DECL_CHAIN (ref));
c6871095 2380 }
2381 }
2382
6777213b 2383 for (formal = gfc_sym_get_dummy_args (ns->proc_name); formal;
2384 formal = formal->next)
1b716045 2385 {
c6871095 2386 /* Ignore alternate returns. */
2387 if (formal->sym == NULL)
2388 continue;
2389
1b716045 2390 /* We don't have a clever way of identifying arguments, so resort to
2391 a brute-force search. */
6777213b 2392 for (thunk_formal = gfc_sym_get_dummy_args (thunk_sym);
1b716045 2393 thunk_formal;
2394 thunk_formal = thunk_formal->next)
2395 {
2396 if (thunk_formal->sym == formal->sym)
2397 break;
2398 }
2399
2400 if (thunk_formal)
2401 {
2402 /* Pass the argument. */
d95efb59 2403 DECL_ARTIFICIAL (thunk_formal->sym->backend_decl) = 1;
f1f41a6c 2404 vec_safe_push (args, thunk_formal->sym->backend_decl);
1b716045 2405 if (formal->sym->ts.type == BT_CHARACTER)
2406 {
eeebe20b 2407 tmp = thunk_formal->sym->ts.u.cl->backend_decl;
f1f41a6c 2408 vec_safe_push (string_args, tmp);
1b716045 2409 }
2410 }
2411 else
2412 {
2413 /* Pass NULL for a missing argument. */
f1f41a6c 2414 vec_safe_push (args, null_pointer_node);
1b716045 2415 if (formal->sym->ts.type == BT_CHARACTER)
2416 {
7d3075f6 2417 tmp = build_int_cst (gfc_charlen_type_node, 0);
f1f41a6c 2418 vec_safe_push (string_args, tmp);
1b716045 2419 }
2420 }
2421 }
2422
2423 /* Call the master function. */
f1f41a6c 2424 vec_safe_splice (args, string_args);
1b716045 2425 tmp = ns->proc_name->backend_decl;
414c3a2c 2426 tmp = build_call_expr_loc_vec (input_location, tmp, args);
c6871095 2427 if (ns->proc_name->attr.mixed_entry_master)
2428 {
2429 tree union_decl, field;
2430 tree master_type = TREE_TYPE (ns->proc_name->backend_decl);
2431
e60a6f7b 2432 union_decl = build_decl (input_location,
2433 VAR_DECL, get_identifier ("__result"),
c6871095 2434 TREE_TYPE (master_type));
2435 DECL_ARTIFICIAL (union_decl) = 1;
2436 DECL_EXTERNAL (union_decl) = 0;
2437 TREE_PUBLIC (union_decl) = 0;
2438 TREE_USED (union_decl) = 1;
2439 layout_decl (union_decl, 0);
2440 pushdecl (union_decl);
2441
2442 DECL_CONTEXT (union_decl) = current_function_decl;
fd779e1d 2443 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
2444 TREE_TYPE (union_decl), union_decl, tmp);
c6871095 2445 gfc_add_expr_to_block (&body, tmp);
2446
2447 for (field = TYPE_FIELDS (TREE_TYPE (union_decl));
1767a056 2448 field; field = DECL_CHAIN (field))
c6871095 2449 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
2450 thunk_sym->result->name) == 0)
2451 break;
2452 gcc_assert (field != NULL_TREE);
fd779e1d 2453 tmp = fold_build3_loc (input_location, COMPONENT_REF,
2454 TREE_TYPE (field), union_decl, field,
2455 NULL_TREE);
a90fe829 2456 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
f75d6b8a 2457 TREE_TYPE (DECL_RESULT (current_function_decl)),
2458 DECL_RESULT (current_function_decl), tmp);
c6871095 2459 tmp = build1_v (RETURN_EXPR, tmp);
2460 }
2461 else if (TREE_TYPE (DECL_RESULT (current_function_decl))
2462 != void_type_node)
2463 {
fd779e1d 2464 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
f75d6b8a 2465 TREE_TYPE (DECL_RESULT (current_function_decl)),
2466 DECL_RESULT (current_function_decl), tmp);
c6871095 2467 tmp = build1_v (RETURN_EXPR, tmp);
2468 }
1b716045 2469 gfc_add_expr_to_block (&body, tmp);
2470
2471 /* Finish off this function and send it for code generation. */
2472 DECL_SAVED_TREE (thunk_fndecl) = gfc_finish_block (&body);
e5004242 2473 tmp = getdecls ();
cde2be84 2474 poplevel (1, 1);
1b716045 2475 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl)) = thunk_fndecl;
e5004242 2476 DECL_SAVED_TREE (thunk_fndecl)
2477 = build3_v (BIND_EXPR, tmp, DECL_SAVED_TREE (thunk_fndecl),
2478 DECL_INITIAL (thunk_fndecl));
1b716045 2479
2480 /* Output the GENERIC tree. */
2481 dump_function (TDI_original, thunk_fndecl);
2482
2483 /* Store the end of the function, so that we get good line number
2484 info for the epilogue. */
2485 cfun->function_end_locus = input_location;
2486
2487 /* We're leaving the context of this function, so zap cfun.
2488 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
2489 tree_rest_of_compilation. */
87d4aa85 2490 set_cfun (NULL);
1b716045 2491
2492 current_function_decl = NULL_TREE;
2493
bb982f66 2494 cgraph_finalize_function (thunk_fndecl, true);
1b716045 2495
2496 /* We share the symbols in the formal argument list with other entry
2497 points and the master function. Clear them so that they are
2498 recreated for each function. */
6777213b 2499 for (formal = gfc_sym_get_dummy_args (thunk_sym); formal;
2500 formal = formal->next)
c6871095 2501 if (formal->sym != NULL) /* Ignore alternate returns. */
2502 {
2503 formal->sym->backend_decl = NULL_TREE;
2504 if (formal->sym->ts.type == BT_CHARACTER)
eeebe20b 2505 formal->sym->ts.u.cl->backend_decl = NULL_TREE;
c6871095 2506 }
2507
2508 if (thunk_sym->attr.function)
1b716045 2509 {
c6871095 2510 if (thunk_sym->ts.type == BT_CHARACTER)
eeebe20b 2511 thunk_sym->ts.u.cl->backend_decl = NULL_TREE;
c6871095 2512 if (thunk_sym->result->ts.type == BT_CHARACTER)
eeebe20b 2513 thunk_sym->result->ts.u.cl->backend_decl = NULL_TREE;
1b716045 2514 }
2515 }
b31f705b 2516
4671339c 2517 gfc_restore_backend_locus (&old_loc);
1b716045 2518}
2519
2520
2521/* Create a decl for a function, and create any thunks for alternate entry
d896f9b3 2522 points. If global is true, generate the function in the global binding
2523 level, otherwise in the current binding level (which can be global). */
1b716045 2524
2525void
d896f9b3 2526gfc_create_function_decl (gfc_namespace * ns, bool global)
1b716045 2527{
2528 /* Create a declaration for the master function. */
d896f9b3 2529 build_function_decl (ns->proc_name, global);
1b716045 2530
f888a3fb 2531 /* Compile the entry thunks. */
1b716045 2532 if (ns->entries)
d896f9b3 2533 build_entry_thunks (ns, global);
1b716045 2534
2535 /* Now create the read argument list. */
2536 create_function_arglist (ns->proc_name);
2537}
2538
c750cc52 2539/* Return the decl used to hold the function return value. If
3350e716 2540 parent_flag is set, the context is the parent_scope. */
4ee9c684 2541
2542tree
c750cc52 2543gfc_get_fake_result_decl (gfc_symbol * sym, int parent_flag)
4ee9c684 2544{
c750cc52 2545 tree decl;
2546 tree length;
2547 tree this_fake_result_decl;
2548 tree this_function_decl;
4ee9c684 2549
2550 char name[GFC_MAX_SYMBOL_LEN + 10];
2551
c750cc52 2552 if (parent_flag)
2553 {
2554 this_fake_result_decl = parent_fake_result_decl;
2555 this_function_decl = DECL_CONTEXT (current_function_decl);
2556 }
2557 else
2558 {
2559 this_fake_result_decl = current_fake_result_decl;
2560 this_function_decl = current_function_decl;
2561 }
2562
c6871095 2563 if (sym
c750cc52 2564 && sym->ns->proc_name->backend_decl == this_function_decl
d4163395 2565 && sym->ns->proc_name->attr.entry_master
c6871095 2566 && sym != sym->ns->proc_name)
2567 {
d4163395 2568 tree t = NULL, var;
c750cc52 2569 if (this_fake_result_decl != NULL)
2570 for (t = TREE_CHAIN (this_fake_result_decl); t; t = TREE_CHAIN (t))
d4163395 2571 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t)), sym->name) == 0)
2572 break;
2573 if (t)
2574 return TREE_VALUE (t);
c750cc52 2575 decl = gfc_get_fake_result_decl (sym->ns->proc_name, parent_flag);
2576
2577 if (parent_flag)
2578 this_fake_result_decl = parent_fake_result_decl;
2579 else
2580 this_fake_result_decl = current_fake_result_decl;
2581
d4163395 2582 if (decl && sym->ns->proc_name->attr.mixed_entry_master)
c6871095 2583 {
2584 tree field;
2585
2586 for (field = TYPE_FIELDS (TREE_TYPE (decl));
1767a056 2587 field; field = DECL_CHAIN (field))
c6871095 2588 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
2589 sym->name) == 0)
2590 break;
2591
2592 gcc_assert (field != NULL_TREE);
fd779e1d 2593 decl = fold_build3_loc (input_location, COMPONENT_REF,
2594 TREE_TYPE (field), decl, field, NULL_TREE);
c6871095 2595 }
c750cc52 2596
2597 var = create_tmp_var_raw (TREE_TYPE (decl), sym->name);
2598 if (parent_flag)
2599 gfc_add_decl_to_parent_function (var);
2600 else
2601 gfc_add_decl_to_function (var);
2602
d4163395 2603 SET_DECL_VALUE_EXPR (var, decl);
2604 DECL_HAS_VALUE_EXPR_P (var) = 1;
2cf330c4 2605 GFC_DECL_RESULT (var) = 1;
c750cc52 2606
2607 TREE_CHAIN (this_fake_result_decl)
2608 = tree_cons (get_identifier (sym->name), var,
2609 TREE_CHAIN (this_fake_result_decl));
d4163395 2610 return var;
c6871095 2611 }
2612
c750cc52 2613 if (this_fake_result_decl != NULL_TREE)
2614 return TREE_VALUE (this_fake_result_decl);
4ee9c684 2615
2616 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
2617 sym is NULL. */
2618 if (!sym)
2619 return NULL_TREE;
2620
d4163395 2621 if (sym->ts.type == BT_CHARACTER)
4ee9c684 2622 {
eeebe20b 2623 if (sym->ts.u.cl->backend_decl == NULL_TREE)
d4163395 2624 length = gfc_create_string_length (sym);
2625 else
eeebe20b 2626 length = sym->ts.u.cl->backend_decl;
d4163395 2627 if (TREE_CODE (length) == VAR_DECL
2628 && DECL_CONTEXT (length) == NULL_TREE)
99042714 2629 gfc_add_decl_to_function (length);
4ee9c684 2630 }
2631
2632 if (gfc_return_by_reference (sym))
2633 {
c750cc52 2634 decl = DECL_ARGUMENTS (this_function_decl);
c6871095 2635
c750cc52 2636 if (sym->ns->proc_name->backend_decl == this_function_decl
c6871095 2637 && sym->ns->proc_name->attr.entry_master)
1767a056 2638 decl = DECL_CHAIN (decl);
4ee9c684 2639
2640 TREE_USED (decl) = 1;
2641 if (sym->as)
2642 decl = gfc_build_dummy_array_decl (sym, decl);
2643 }
2644 else
2645 {
2646 sprintf (name, "__result_%.20s",
c750cc52 2647 IDENTIFIER_POINTER (DECL_NAME (this_function_decl)));
4ee9c684 2648
3350e716 2649 if (!sym->attr.mixed_entry_master && sym->attr.function)
1e71b314 2650 decl = build_decl (DECL_SOURCE_LOCATION (this_function_decl),
e60a6f7b 2651 VAR_DECL, get_identifier (name),
3350e716 2652 gfc_sym_type (sym));
2653 else
1e71b314 2654 decl = build_decl (DECL_SOURCE_LOCATION (this_function_decl),
e60a6f7b 2655 VAR_DECL, get_identifier (name),
3350e716 2656 TREE_TYPE (TREE_TYPE (this_function_decl)));
4ee9c684 2657 DECL_ARTIFICIAL (decl) = 1;
2658 DECL_EXTERNAL (decl) = 0;
2659 TREE_PUBLIC (decl) = 0;
2660 TREE_USED (decl) = 1;
764f1175 2661 GFC_DECL_RESULT (decl) = 1;
a379e3a9 2662 TREE_ADDRESSABLE (decl) = 1;
4ee9c684 2663
2664 layout_decl (decl, 0);
2665
c750cc52 2666 if (parent_flag)
2667 gfc_add_decl_to_parent_function (decl);
2668 else
2669 gfc_add_decl_to_function (decl);
4ee9c684 2670 }
2671
c750cc52 2672 if (parent_flag)
2673 parent_fake_result_decl = build_tree_list (NULL, decl);
2674 else
2675 current_fake_result_decl = build_tree_list (NULL, decl);
4ee9c684 2676
2677 return decl;
2678}
2679
2680
2681/* Builds a function decl. The remaining parameters are the types of the
2682 function arguments. Negative nargs indicates a varargs function. */
2683
8ce86007 2684static tree
2685build_library_function_decl_1 (tree name, const char *spec,
2686 tree rettype, int nargs, va_list p)
4ee9c684 2687{
f1f41a6c 2688 vec<tree, va_gc> *arglist;
4ee9c684 2689 tree fntype;
2690 tree fndecl;
4ee9c684 2691 int n;
2692
2693 /* Library functions must be declared with global scope. */
22d678e8 2694 gcc_assert (current_function_decl == NULL_TREE);
4ee9c684 2695
4ee9c684 2696 /* Create a list of the argument types. */
f1f41a6c 2697 vec_alloc (arglist, abs (nargs));
5edc3af9 2698 for (n = abs (nargs); n > 0; n--)
4ee9c684 2699 {
5edc3af9 2700 tree argtype = va_arg (p, tree);
f1f41a6c 2701 arglist->quick_push (argtype);
4ee9c684 2702 }
2703
2704 /* Build the function type and decl. */
5edc3af9 2705 if (nargs >= 0)
2706 fntype = build_function_type_vec (rettype, arglist);
2707 else
2708 fntype = build_varargs_function_type_vec (rettype, arglist);
8ce86007 2709 if (spec)
2710 {
2711 tree attr_args = build_tree_list (NULL_TREE,
2712 build_string (strlen (spec), spec));
2713 tree attrs = tree_cons (get_identifier ("fn spec"),
2714 attr_args, TYPE_ATTRIBUTES (fntype));
2715 fntype = build_type_attribute_variant (fntype, attrs);
2716 }
e60a6f7b 2717 fndecl = build_decl (input_location,
2718 FUNCTION_DECL, name, fntype);
4ee9c684 2719
2720 /* Mark this decl as external. */
2721 DECL_EXTERNAL (fndecl) = 1;
2722 TREE_PUBLIC (fndecl) = 1;
2723
4ee9c684 2724 pushdecl (fndecl);
2725
b2c4af5e 2726 rest_of_decl_compilation (fndecl, 1, 0);
4ee9c684 2727
2728 return fndecl;
2729}
2730
8ce86007 2731/* Builds a function decl. The remaining parameters are the types of the
2732 function arguments. Negative nargs indicates a varargs function. */
2733
2734tree
2735gfc_build_library_function_decl (tree name, tree rettype, int nargs, ...)
2736{
2737 tree ret;
2738 va_list args;
2739 va_start (args, nargs);
2740 ret = build_library_function_decl_1 (name, NULL, rettype, nargs, args);
2741 va_end (args);
2742 return ret;
2743}
2744
2745/* Builds a function decl. The remaining parameters are the types of the
2746 function arguments. Negative nargs indicates a varargs function.
2747 The SPEC parameter specifies the function argument and return type
2748 specification according to the fnspec function type attribute. */
2749
4bf69bc3 2750tree
8ce86007 2751gfc_build_library_function_decl_with_spec (tree name, const char *spec,
2752 tree rettype, int nargs, ...)
2753{
2754 tree ret;
2755 va_list args;
2756 va_start (args, nargs);
2757 ret = build_library_function_decl_1 (name, spec, rettype, nargs, args);
2758 va_end (args);
2759 return ret;
2760}
2761
4ee9c684 2762static void
2763gfc_build_intrinsic_function_decls (void)
2764{
90ba9145 2765 tree gfc_int4_type_node = gfc_get_int_type (4);
2766 tree gfc_int8_type_node = gfc_get_int_type (8);
920e54ef 2767 tree gfc_int16_type_node = gfc_get_int_type (16);
90ba9145 2768 tree gfc_logical4_type_node = gfc_get_logical_type (4);
40b806de 2769 tree pchar1_type_node = gfc_get_pchar_type (1);
2770 tree pchar4_type_node = gfc_get_pchar_type (4);
90ba9145 2771
4ee9c684 2772 /* String functions. */
241ecdc7 2773 gfor_fndecl_compare_string = gfc_build_library_function_decl_with_spec (
2774 get_identifier (PREFIX("compare_string")), "..R.R",
2775 integer_type_node, 4, gfc_charlen_type_node, pchar1_type_node,
2776 gfc_charlen_type_node, pchar1_type_node);
537824d1 2777 DECL_PURE_P (gfor_fndecl_compare_string) = 1;
bc351485 2778 TREE_NOTHROW (gfor_fndecl_compare_string) = 1;
241ecdc7 2779
2780 gfor_fndecl_concat_string = gfc_build_library_function_decl_with_spec (
2781 get_identifier (PREFIX("concat_string")), "..W.R.R",
2782 void_type_node, 6, gfc_charlen_type_node, pchar1_type_node,
2783 gfc_charlen_type_node, pchar1_type_node,
2784 gfc_charlen_type_node, pchar1_type_node);
bc351485 2785 TREE_NOTHROW (gfor_fndecl_concat_string) = 1;
241ecdc7 2786
2787 gfor_fndecl_string_len_trim = gfc_build_library_function_decl_with_spec (
2788 get_identifier (PREFIX("string_len_trim")), "..R",
2789 gfc_charlen_type_node, 2, gfc_charlen_type_node, pchar1_type_node);
537824d1 2790 DECL_PURE_P (gfor_fndecl_string_len_trim) = 1;
bc351485 2791 TREE_NOTHROW (gfor_fndecl_string_len_trim) = 1;
241ecdc7 2792
2793 gfor_fndecl_string_index = gfc_build_library_function_decl_with_spec (
2794 get_identifier (PREFIX("string_index")), "..R.R.",
2795 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar1_type_node,
2796 gfc_charlen_type_node, pchar1_type_node, gfc_logical4_type_node);
537824d1 2797 DECL_PURE_P (gfor_fndecl_string_index) = 1;
bc351485 2798 TREE_NOTHROW (gfor_fndecl_string_index) = 1;
241ecdc7 2799
2800 gfor_fndecl_string_scan = gfc_build_library_function_decl_with_spec (
2801 get_identifier (PREFIX("string_scan")), "..R.R.",
2802 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar1_type_node,
2803 gfc_charlen_type_node, pchar1_type_node, gfc_logical4_type_node);
537824d1 2804 DECL_PURE_P (gfor_fndecl_string_scan) = 1;
bc351485 2805 TREE_NOTHROW (gfor_fndecl_string_scan) = 1;
241ecdc7 2806
2807 gfor_fndecl_string_verify = gfc_build_library_function_decl_with_spec (
2808 get_identifier (PREFIX("string_verify")), "..R.R.",
2809 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar1_type_node,
2810 gfc_charlen_type_node, pchar1_type_node, gfc_logical4_type_node);
537824d1 2811 DECL_PURE_P (gfor_fndecl_string_verify) = 1;
bc351485 2812 TREE_NOTHROW (gfor_fndecl_string_verify) = 1;
241ecdc7 2813
2814 gfor_fndecl_string_trim = gfc_build_library_function_decl_with_spec (
2815 get_identifier (PREFIX("string_trim")), ".Ww.R",
2816 void_type_node, 4, build_pointer_type (gfc_charlen_type_node),
2817 build_pointer_type (pchar1_type_node), gfc_charlen_type_node,
2818 pchar1_type_node);
2819
2820 gfor_fndecl_string_minmax = gfc_build_library_function_decl_with_spec (
2821 get_identifier (PREFIX("string_minmax")), ".Ww.R",
2822 void_type_node, -4, build_pointer_type (gfc_charlen_type_node),
2823 build_pointer_type (pchar1_type_node), integer_type_node,
2824 integer_type_node);
2825
2826 gfor_fndecl_adjustl = gfc_build_library_function_decl_with_spec (
2827 get_identifier (PREFIX("adjustl")), ".W.R",
2828 void_type_node, 3, pchar1_type_node, gfc_charlen_type_node,
2829 pchar1_type_node);
bc351485 2830 TREE_NOTHROW (gfor_fndecl_adjustl) = 1;
241ecdc7 2831
2832 gfor_fndecl_adjustr = gfc_build_library_function_decl_with_spec (
2833 get_identifier (PREFIX("adjustr")), ".W.R",
2834 void_type_node, 3, pchar1_type_node, gfc_charlen_type_node,
2835 pchar1_type_node);
bc351485 2836 TREE_NOTHROW (gfor_fndecl_adjustr) = 1;
241ecdc7 2837
2838 gfor_fndecl_select_string = gfc_build_library_function_decl_with_spec (
2839 get_identifier (PREFIX("select_string")), ".R.R.",
2840 integer_type_node, 4, pvoid_type_node, integer_type_node,
2841 pchar1_type_node, gfc_charlen_type_node);
537824d1 2842 DECL_PURE_P (gfor_fndecl_select_string) = 1;
bc351485 2843 TREE_NOTHROW (gfor_fndecl_select_string) = 1;
241ecdc7 2844
2845 gfor_fndecl_compare_string_char4 = gfc_build_library_function_decl_with_spec (
2846 get_identifier (PREFIX("compare_string_char4")), "..R.R",
2847 integer_type_node, 4, gfc_charlen_type_node, pchar4_type_node,
2848 gfc_charlen_type_node, pchar4_type_node);
537824d1 2849 DECL_PURE_P (gfor_fndecl_compare_string_char4) = 1;
bc351485 2850 TREE_NOTHROW (gfor_fndecl_compare_string_char4) = 1;
241ecdc7 2851
2852 gfor_fndecl_concat_string_char4 = gfc_build_library_function_decl_with_spec (
2853 get_identifier (PREFIX("concat_string_char4")), "..W.R.R",
2854 void_type_node, 6, gfc_charlen_type_node, pchar4_type_node,
2855 gfc_charlen_type_node, pchar4_type_node, gfc_charlen_type_node,
2856 pchar4_type_node);
bc351485 2857 TREE_NOTHROW (gfor_fndecl_concat_string_char4) = 1;
241ecdc7 2858
2859 gfor_fndecl_string_len_trim_char4 = gfc_build_library_function_decl_with_spec (
2860 get_identifier (PREFIX("string_len_trim_char4")), "..R",
2861 gfc_charlen_type_node, 2, gfc_charlen_type_node, pchar4_type_node);
537824d1 2862 DECL_PURE_P (gfor_fndecl_string_len_trim_char4) = 1;
bc351485 2863 TREE_NOTHROW (gfor_fndecl_string_len_trim_char4) = 1;
241ecdc7 2864
2865 gfor_fndecl_string_index_char4 = gfc_build_library_function_decl_with_spec (
2866 get_identifier (PREFIX("string_index_char4")), "..R.R.",
2867 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar4_type_node,
2868 gfc_charlen_type_node, pchar4_type_node, gfc_logical4_type_node);
537824d1 2869 DECL_PURE_P (gfor_fndecl_string_index_char4) = 1;
bc351485 2870 TREE_NOTHROW (gfor_fndecl_string_index_char4) = 1;
241ecdc7 2871
2872 gfor_fndecl_string_scan_char4 = gfc_build_library_function_decl_with_spec (
2873 get_identifier (PREFIX("string_scan_char4")), "..R.R.",
2874 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar4_type_node,
2875 gfc_charlen_type_node, pchar4_type_node, gfc_logical4_type_node);
537824d1 2876 DECL_PURE_P (gfor_fndecl_string_scan_char4) = 1;
bc351485 2877 TREE_NOTHROW (gfor_fndecl_string_scan_char4) = 1;
241ecdc7 2878
2879 gfor_fndecl_string_verify_char4 = gfc_build_library_function_decl_with_spec (
2880 get_identifier (PREFIX("string_verify_char4")), "..R.R.",
2881 gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar4_type_node,
2882 gfc_charlen_type_node, pchar4_type_node, gfc_logical4_type_node);
537824d1 2883 DECL_PURE_P (gfor_fndecl_string_verify_char4) = 1;
bc351485 2884 TREE_NOTHROW (gfor_fndecl_string_verify_char4) = 1;
241ecdc7 2885
2886 gfor_fndecl_string_trim_char4 = gfc_build_library_function_decl_with_spec (
2887 get_identifier (PREFIX("string_trim_char4")), ".Ww.R",
2888 void_type_node, 4, build_pointer_type (gfc_charlen_type_node),
2889 build_pointer_type (pchar4_type_node), gfc_charlen_type_node,
2890 pchar4_type_node);
2891
2892 gfor_fndecl_string_minmax_char4 = gfc_build_library_function_decl_with_spec (
2893 get_identifier (PREFIX("string_minmax_char4")), ".Ww.R",
2894 void_type_node, -4, build_pointer_type (gfc_charlen_type_node),
2895 build_pointer_type (pchar4_type_node), integer_type_node,
2896 integer_type_node);
2897
2898 gfor_fndecl_adjustl_char4 = gfc_build_library_function_decl_with_spec (
2899 get_identifier (PREFIX("adjustl_char4")), ".W.R",
2900 void_type_node, 3, pchar4_type_node, gfc_charlen_type_node,
2901 pchar4_type_node);
bc351485 2902 TREE_NOTHROW (gfor_fndecl_adjustl_char4) = 1;
241ecdc7 2903
2904 gfor_fndecl_adjustr_char4 = gfc_build_library_function_decl_with_spec (
2905 get_identifier (PREFIX("adjustr_char4")), ".W.R",
2906 void_type_node, 3, pchar4_type_node, gfc_charlen_type_node,
2907 pchar4_type_node);
bc351485 2908 TREE_NOTHROW (gfor_fndecl_adjustr_char4) = 1;
241ecdc7 2909
2910 gfor_fndecl_select_string_char4 = gfc_build_library_function_decl_with_spec (
2911 get_identifier (PREFIX("select_string_char4")), ".R.R.",
2912 integer_type_node, 4, pvoid_type_node, integer_type_node,
2913 pvoid_type_node, gfc_charlen_type_node);
537824d1 2914 DECL_PURE_P (gfor_fndecl_select_string_char4) = 1;
bc351485 2915 TREE_NOTHROW (gfor_fndecl_select_string_char4) = 1;
b44437b9 2916
2917
2918 /* Conversion between character kinds. */
2919
241ecdc7 2920 gfor_fndecl_convert_char1_to_char4 = gfc_build_library_function_decl_with_spec (
2921 get_identifier (PREFIX("convert_char1_to_char4")), ".w.R",
2922 void_type_node, 3, build_pointer_type (pchar4_type_node),
2923 gfc_charlen_type_node, pchar1_type_node);
b44437b9 2924
241ecdc7 2925 gfor_fndecl_convert_char4_to_char1 = gfc_build_library_function_decl_with_spec (
2926 get_identifier (PREFIX("convert_char4_to_char1")), ".w.R",
2927 void_type_node, 3, build_pointer_type (pchar1_type_node),
2928 gfc_charlen_type_node, pchar4_type_node);
b44437b9 2929
40b806de 2930 /* Misc. functions. */
5fcc6ec2 2931
241ecdc7 2932 gfor_fndecl_ttynam = gfc_build_library_function_decl_with_spec (
2933 get_identifier (PREFIX("ttynam")), ".W",
2934 void_type_node, 3, pchar_type_node, gfc_charlen_type_node,
2935 integer_type_node);
2936
2937 gfor_fndecl_fdate = gfc_build_library_function_decl_with_spec (
2938 get_identifier (PREFIX("fdate")), ".W",
2939 void_type_node, 2, pchar_type_node, gfc_charlen_type_node);
2940
2941 gfor_fndecl_ctime = gfc_build_library_function_decl_with_spec (
2942 get_identifier (PREFIX("ctime")), ".W",
2943 void_type_node, 3, pchar_type_node, gfc_charlen_type_node,
2944 gfc_int8_type_node);
2945
2946 gfor_fndecl_sc_kind = gfc_build_library_function_decl_with_spec (
2947 get_identifier (PREFIX("selected_char_kind")), "..R",
2948 gfc_int4_type_node, 2, gfc_charlen_type_node, pchar_type_node);
537824d1 2949 DECL_PURE_P (gfor_fndecl_sc_kind) = 1;
bc351485 2950 TREE_NOTHROW (gfor_fndecl_sc_kind) = 1;
241ecdc7 2951
2952 gfor_fndecl_si_kind = gfc_build_library_function_decl_with_spec (
2953 get_identifier (PREFIX("selected_int_kind")), ".R",
2954 gfc_int4_type_node, 1, pvoid_type_node);
537824d1 2955 DECL_PURE_P (gfor_fndecl_si_kind) = 1;
bc351485 2956 TREE_NOTHROW (gfor_fndecl_si_kind) = 1;
241ecdc7 2957
2958 gfor_fndecl_sr_kind = gfc_build_library_function_decl_with_spec (
2959 get_identifier (PREFIX("selected_real_kind2008")), ".RR",
2960 gfc_int4_type_node, 3, pvoid_type_node, pvoid_type_node,
2961 pvoid_type_node);
537824d1 2962 DECL_PURE_P (gfor_fndecl_sr_kind) = 1;
bc351485 2963 TREE_NOTHROW (gfor_fndecl_sr_kind) = 1;
4ee9c684 2964
4ee9c684 2965 /* Power functions. */
76834664 2966 {
920e54ef 2967 tree ctype, rtype, itype, jtype;
2968 int rkind, ikind, jkind;
2969#define NIKINDS 3
2970#define NRKINDS 4
2971 static int ikinds[NIKINDS] = {4, 8, 16};
2972 static int rkinds[NRKINDS] = {4, 8, 10, 16};
2973 char name[PREFIX_LEN + 12]; /* _gfortran_pow_?n_?n */
2974
2975 for (ikind=0; ikind < NIKINDS; ikind++)
76834664 2976 {
920e54ef 2977 itype = gfc_get_int_type (ikinds[ikind]);
2978
2979 for (jkind=0; jkind < NIKINDS; jkind++)
2980 {
2981 jtype = gfc_get_int_type (ikinds[jkind]);
2982 if (itype && jtype)
2983 {
2984 sprintf(name, PREFIX("pow_i%d_i%d"), ikinds[ikind],
2985 ikinds[jkind]);
2986 gfor_fndecl_math_powi[jkind][ikind].integer =
2987 gfc_build_library_function_decl (get_identifier (name),
2988 jtype, 2, jtype, itype);
2177d98b 2989 TREE_READONLY (gfor_fndecl_math_powi[jkind][ikind].integer) = 1;
bc351485 2990 TREE_NOTHROW (gfor_fndecl_math_powi[jkind][ikind].integer) = 1;
920e54ef 2991 }
2992 }
2993
2994 for (rkind = 0; rkind < NRKINDS; rkind ++)
76834664 2995 {
920e54ef 2996 rtype = gfc_get_real_type (rkinds[rkind]);
2997 if (rtype && itype)
2998 {
2999 sprintf(name, PREFIX("pow_r%d_i%d"), rkinds[rkind],
3000 ikinds[ikind]);
3001 gfor_fndecl_math_powi[rkind][ikind].real =
3002 gfc_build_library_function_decl (get_identifier (name),
3003 rtype, 2, rtype, itype);
2177d98b 3004 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].real) = 1;
bc351485 3005 TREE_NOTHROW (gfor_fndecl_math_powi[rkind][ikind].real) = 1;
920e54ef 3006 }
3007
3008 ctype = gfc_get_complex_type (rkinds[rkind]);
3009 if (ctype && itype)
3010 {
3011 sprintf(name, PREFIX("pow_c%d_i%d"), rkinds[rkind],
3012 ikinds[ikind]);
3013 gfor_fndecl_math_powi[rkind][ikind].cmplx =
3014 gfc_build_library_function_decl (get_identifier (name),
3015 ctype, 2,ctype, itype);
2177d98b 3016 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1;
bc351485 3017 TREE_NOTHROW (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1;
920e54ef 3018 }
76834664 3019 }
3020 }
920e54ef 3021#undef NIKINDS
3022#undef NRKINDS
76834664 3023 }
3024
241ecdc7 3025 gfor_fndecl_math_ishftc4 = gfc_build_library_function_decl (
3026 get_identifier (PREFIX("ishftc4")),
3027 gfc_int4_type_node, 3, gfc_int4_type_node, gfc_int4_type_node,
3028 gfc_int4_type_node);
bc351485 3029 TREE_READONLY (gfor_fndecl_math_ishftc4) = 1;
3030 TREE_NOTHROW (gfor_fndecl_math_ishftc4) = 1;
a90fe829 3031
241ecdc7 3032 gfor_fndecl_math_ishftc8 = gfc_build_library_function_decl (
3033 get_identifier (PREFIX("ishftc8")),
3034 gfc_int8_type_node, 3, gfc_int8_type_node, gfc_int4_type_node,
3035 gfc_int4_type_node);
bc351485 3036 TREE_READONLY (gfor_fndecl_math_ishftc8) = 1;
3037 TREE_NOTHROW (gfor_fndecl_math_ishftc8) = 1;
241ecdc7 3038
920e54ef 3039 if (gfc_int16_type_node)
bc351485 3040 {
3041 gfor_fndecl_math_ishftc16 = gfc_build_library_function_decl (
241ecdc7 3042 get_identifier (PREFIX("ishftc16")),
3043 gfc_int16_type_node, 3, gfc_int16_type_node, gfc_int4_type_node,
3044 gfc_int4_type_node);
bc351485 3045 TREE_READONLY (gfor_fndecl_math_ishftc16) = 1;
3046 TREE_NOTHROW (gfor_fndecl_math_ishftc16) = 1;
3047 }
920e54ef 3048
4e8e57b0 3049 /* BLAS functions. */
3050 {
36c921b9 3051 tree pint = build_pointer_type (integer_type_node);
4e8e57b0 3052 tree ps = build_pointer_type (gfc_get_real_type (gfc_default_real_kind));
3053 tree pd = build_pointer_type (gfc_get_real_type (gfc_default_double_kind));
3054 tree pc = build_pointer_type (gfc_get_complex_type (gfc_default_real_kind));
3055 tree pz = build_pointer_type
3056 (gfc_get_complex_type (gfc_default_double_kind));
3057
3058 gfor_fndecl_sgemm = gfc_build_library_function_decl
3059 (get_identifier
3060 (gfc_option.flag_underscoring ? "sgemm_"
3061 : "sgemm"),
3062 void_type_node, 15, pchar_type_node,
3063 pchar_type_node, pint, pint, pint, ps, ps, pint,
36c921b9 3064 ps, pint, ps, ps, pint, integer_type_node,
3065 integer_type_node);
4e8e57b0 3066 gfor_fndecl_dgemm = gfc_build_library_function_decl
3067 (get_identifier
3068 (gfc_option.flag_underscoring ? "dgemm_"
3069 : "dgemm"),
3070 void_type_node, 15, pchar_type_node,
3071 pchar_type_node, pint, pint, pint, pd, pd, pint,
36c921b9 3072 pd, pint, pd, pd, pint, integer_type_node,
3073 integer_type_node);
4e8e57b0 3074 gfor_fndecl_cgemm = gfc_build_library_function_decl
3075 (get_identifier
3076 (gfc_option.flag_underscoring ? "cgemm_"
3077 : "cgemm"),
3078 void_type_node, 15, pchar_type_node,
3079 pchar_type_node, pint, pint, pint, pc, pc, pint,
36c921b9 3080 pc, pint, pc, pc, pint, integer_type_node,
3081 integer_type_node);
4e8e57b0 3082 gfor_fndecl_zgemm = gfc_build_library_function_decl
3083 (get_identifier
3084 (gfc_option.flag_underscoring ? "zgemm_"
3085 : "zgemm"),
3086 void_type_node, 15, pchar_type_node,
3087 pchar_type_node, pint, pint, pint, pz, pz, pint,
36c921b9 3088 pz, pint, pz, pz, pint, integer_type_node,
3089 integer_type_node);
4e8e57b0 3090 }
3091
4ee9c684 3092 /* Other functions. */
241ecdc7 3093 gfor_fndecl_size0 = gfc_build_library_function_decl_with_spec (
3094 get_identifier (PREFIX("size0")), ".R",
3095 gfc_array_index_type, 1, pvoid_type_node);
537824d1 3096 DECL_PURE_P (gfor_fndecl_size0) = 1;
bc351485 3097 TREE_NOTHROW (gfor_fndecl_size0) = 1;
241ecdc7 3098
3099 gfor_fndecl_size1 = gfc_build_library_function_decl_with_spec (
3100 get_identifier (PREFIX("size1")), ".R",
3101 gfc_array_index_type, 2, pvoid_type_node, gfc_array_index_type);
537824d1 3102 DECL_PURE_P (gfor_fndecl_size1) = 1;
bc351485 3103 TREE_NOTHROW (gfor_fndecl_size1) = 1;
241ecdc7 3104
3105 gfor_fndecl_iargc = gfc_build_library_function_decl (
3106 get_identifier (PREFIX ("iargc")), gfc_int4_type_node, 0);
bc351485 3107 TREE_NOTHROW (gfor_fndecl_iargc) = 1;
4ee9c684 3108}
3109
3110
3111/* Make prototypes for runtime library functions. */
3112
3113void
3114gfc_build_builtin_function_decls (void)
3115{
90ba9145 3116 tree gfc_int4_type_node = gfc_get_int_type (4);
4ee9c684 3117
241ecdc7 3118 gfor_fndecl_stop_numeric = gfc_build_library_function_decl (
3119 get_identifier (PREFIX("stop_numeric")),
3120 void_type_node, 1, gfc_int4_type_node);
070cc790 3121 /* STOP doesn't return. */
98ccec97 3122 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric) = 1;
3123
dff2ea5f 3124 gfor_fndecl_stop_numeric_f08 = gfc_build_library_function_decl (
3125 get_identifier (PREFIX("stop_numeric_f08")),
3126 void_type_node, 1, gfc_int4_type_node);
3127 /* STOP doesn't return. */
3128 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric_f08) = 1;
3129
241ecdc7 3130 gfor_fndecl_stop_string = gfc_build_library_function_decl_with_spec (
3131 get_identifier (PREFIX("stop_string")), ".R.",
3132 void_type_node, 2, pchar_type_node, gfc_int4_type_node);
070cc790 3133 /* STOP doesn't return. */
241ecdc7 3134 TREE_THIS_VOLATILE (gfor_fndecl_stop_string) = 1;
537824d1 3135
241ecdc7 3136 gfor_fndecl_error_stop_numeric = gfc_build_library_function_decl (
3137 get_identifier (PREFIX("error_stop_numeric")),
3138 void_type_node, 1, gfc_int4_type_node);
070cc790 3139 /* ERROR STOP doesn't return. */
3140 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_numeric) = 1;
3141
241ecdc7 3142 gfor_fndecl_error_stop_string = gfc_build_library_function_decl_with_spec (
3143 get_identifier (PREFIX("error_stop_string")), ".R.",
3144 void_type_node, 2, pchar_type_node, gfc_int4_type_node);
c6cd3066 3145 /* ERROR STOP doesn't return. */
3146 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_string) = 1;
3147
241ecdc7 3148 gfor_fndecl_pause_numeric = gfc_build_library_function_decl (
3149 get_identifier (PREFIX("pause_numeric")),
3150 void_type_node, 1, gfc_int4_type_node);
070cc790 3151
241ecdc7 3152 gfor_fndecl_pause_string = gfc_build_library_function_decl_with_spec (
3153 get_identifier (PREFIX("pause_string")), ".R.",
3154 void_type_node, 2, pchar_type_node, gfc_int4_type_node);
4ee9c684 3155
241ecdc7 3156 gfor_fndecl_runtime_error = gfc_build_library_function_decl_with_spec (
3157 get_identifier (PREFIX("runtime_error")), ".R",
3158 void_type_node, -1, pchar_type_node);
9c0f3811 3159 /* The runtime_error function does not return. */
3160 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error) = 1;
4ee9c684 3161
241ecdc7 3162 gfor_fndecl_runtime_error_at = gfc_build_library_function_decl_with_spec (
3163 get_identifier (PREFIX("runtime_error_at")), ".RR",
3164 void_type_node, -2, pchar_type_node, pchar_type_node);
50ad5fa2 3165 /* The runtime_error_at function does not return. */
3166 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at) = 1;
a90fe829 3167
241ecdc7 3168 gfor_fndecl_runtime_warning_at = gfc_build_library_function_decl_with_spec (
3169 get_identifier (PREFIX("runtime_warning_at")), ".RR",
3170 void_type_node, -2, pchar_type_node, pchar_type_node);
3171
3172 gfor_fndecl_generate_error = gfc_build_library_function_decl_with_spec (
3173 get_identifier (PREFIX("generate_error")), ".R.R",
3174 void_type_node, 3, pvoid_type_node, integer_type_node,
3175 pchar_type_node);
3176
3177 gfor_fndecl_os_error = gfc_build_library_function_decl_with_spec (
3178 get_identifier (PREFIX("os_error")), ".R",
3179 void_type_node, 1, pchar_type_node);
9915365e 3180 /* The runtime_error function does not return. */
3181 TREE_THIS_VOLATILE (gfor_fndecl_os_error) = 1;
3182
241ecdc7 3183 gfor_fndecl_set_args = gfc_build_library_function_decl (
3184 get_identifier (PREFIX("set_args")),
3185 void_type_node, 2, integer_type_node,
3186 build_pointer_type (pchar_type_node));
7257a5d2 3187
241ecdc7 3188 gfor_fndecl_set_fpe = gfc_build_library_function_decl (
3189 get_identifier (PREFIX("set_fpe")),
3190 void_type_node, 1, integer_type_node);
8c84a5de 3191
56c7c2d7 3192 /* Keep the array dimension in sync with the call, later in this file. */
241ecdc7 3193 gfor_fndecl_set_options = gfc_build_library_function_decl_with_spec (
3194 get_identifier (PREFIX("set_options")), "..R",
3195 void_type_node, 2, integer_type_node,
3196 build_pointer_type (integer_type_node));
64fc3c4c 3197
241ecdc7 3198 gfor_fndecl_set_convert = gfc_build_library_function_decl (
3199 get_identifier (PREFIX("set_convert")),
3200 void_type_node, 1, integer_type_node);
15774a8b 3201
241ecdc7 3202 gfor_fndecl_set_record_marker = gfc_build_library_function_decl (
3203 get_identifier (PREFIX("set_record_marker")),
3204 void_type_node, 1, integer_type_node);
f23886ab 3205
241ecdc7 3206 gfor_fndecl_set_max_subrecord_length = gfc_build_library_function_decl (
3207 get_identifier (PREFIX("set_max_subrecord_length")),
3208 void_type_node, 1, integer_type_node);
bbaaa7b1 3209
8ce86007 3210 gfor_fndecl_in_pack = gfc_build_library_function_decl_with_spec (
241ecdc7 3211 get_identifier (PREFIX("internal_pack")), ".r",
3212 pvoid_type_node, 1, pvoid_type_node);
4ee9c684 3213
8ce86007 3214 gfor_fndecl_in_unpack = gfc_build_library_function_decl_with_spec (
241ecdc7 3215 get_identifier (PREFIX("internal_unpack")), ".wR",
3216 void_type_node, 2, pvoid_type_node, pvoid_type_node);
3217
3218 gfor_fndecl_associated = gfc_build_library_function_decl_with_spec (
3219 get_identifier (PREFIX("associated")), ".RR",
3220 integer_type_node, 2, ppvoid_type_node, ppvoid_type_node);
537824d1 3221 DECL_PURE_P (gfor_fndecl_associated) = 1;
bc351485 3222 TREE_NOTHROW (gfor_fndecl_associated) = 1;
4ee9c684 3223
70b5944a 3224 /* Coarray library calls. */
3225 if (gfc_option.coarray == GFC_FCOARRAY_LIB)
3226 {
3227 tree pint_type, pppchar_type;
3228
3229 pint_type = build_pointer_type (integer_type_node);
3230 pppchar_type
3231 = build_pointer_type (build_pointer_type (pchar_type_node));
3232
3233 gfor_fndecl_caf_init = gfc_build_library_function_decl (
3234 get_identifier (PREFIX("caf_init")), void_type_node,
3235 4, pint_type, pppchar_type, pint_type, pint_type);
3236
3237 gfor_fndecl_caf_finalize = gfc_build_library_function_decl (
3238 get_identifier (PREFIX("caf_finalize")), void_type_node, 0);
3239
a961ca30 3240 gfor_fndecl_caf_register = gfc_build_library_function_decl_with_spec (
3241 get_identifier (PREFIX("caf_register")), "...WWW", pvoid_type_node, 6,
3242 size_type_node, integer_type_node, ppvoid_type_node, pint_type,
d0d776fb 3243 pchar_type_node, integer_type_node);
3244
3245 gfor_fndecl_caf_deregister = gfc_build_library_function_decl_with_spec (
3246 get_identifier (PREFIX("caf_deregister")), ".WWW", void_type_node, 4,
3247 ppvoid_type_node, pint_type, pchar_type_node, integer_type_node);
a961ca30 3248
70b5944a 3249 gfor_fndecl_caf_critical = gfc_build_library_function_decl (
3250 get_identifier (PREFIX("caf_critical")), void_type_node, 0);
3251
3252 gfor_fndecl_caf_end_critical = gfc_build_library_function_decl (
3253 get_identifier (PREFIX("caf_end_critical")), void_type_node, 0);
3254
3255 gfor_fndecl_caf_sync_all = gfc_build_library_function_decl_with_spec (
96b417f0 3256 get_identifier (PREFIX("caf_sync_all")), ".WW", void_type_node,
3257 3, pint_type, build_pointer_type (pchar_type_node), integer_type_node);
70b5944a 3258
3259 gfor_fndecl_caf_sync_images = gfc_build_library_function_decl_with_spec (
96b417f0 3260 get_identifier (PREFIX("caf_sync_images")), ".RRWW", void_type_node,
3261 5, integer_type_node, pint_type, pint_type,
3262 build_pointer_type (pchar_type_node), integer_type_node);
70b5944a 3263
3264 gfor_fndecl_caf_error_stop = gfc_build_library_function_decl (
3265 get_identifier (PREFIX("caf_error_stop")),
3266 void_type_node, 1, gfc_int4_type_node);
3267 /* CAF's ERROR STOP doesn't return. */
3268 TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop) = 1;
3269
3270 gfor_fndecl_caf_error_stop_str = gfc_build_library_function_decl_with_spec (
3271 get_identifier (PREFIX("caf_error_stop_str")), ".R.",
3272 void_type_node, 2, pchar_type_node, gfc_int4_type_node);
3273 /* CAF's ERROR STOP doesn't return. */
3274 TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop_str) = 1;
3275 }
3276
4ee9c684 3277 gfc_build_intrinsic_function_decls ();
3278 gfc_build_intrinsic_lib_fndecls ();
3279 gfc_build_io_library_fndecls ();
3280}
3281
3282
231e961a 3283/* Evaluate the length of dummy character variables. */
4ee9c684 3284
c5faa799 3285static void
3286gfc_trans_dummy_character (gfc_symbol *sym, gfc_charlen *cl,
3287 gfc_wrapped_block *block)
4ee9c684 3288{
c5faa799 3289 stmtblock_t init;
4ee9c684 3290
b9c7fce7 3291 gfc_finish_decl (cl->backend_decl);
4ee9c684 3292
c5faa799 3293 gfc_start_block (&init);
4ee9c684 3294
3295 /* Evaluate the string length expression. */
c5faa799 3296 gfc_conv_string_length (cl, NULL, &init);
d4163395 3297
c5faa799 3298 gfc_trans_vla_type_sizes (sym, &init);
d4163395 3299
c5faa799 3300 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
4ee9c684 3301}
3302
3303
3304/* Allocate and cleanup an automatic character variable. */
3305
c5faa799 3306static void
3307gfc_trans_auto_character_variable (gfc_symbol * sym, gfc_wrapped_block * block)
4ee9c684 3308{
c5faa799 3309 stmtblock_t init;
4ee9c684 3310 tree decl;
4ee9c684 3311 tree tmp;
3312
22d678e8 3313 gcc_assert (sym->backend_decl);
eeebe20b 3314 gcc_assert (sym->ts.u.cl && sym->ts.u.cl->length);
4ee9c684 3315
3714c8b6 3316 gfc_init_block (&init);
4ee9c684 3317
3318 /* Evaluate the string length expression. */
c5faa799 3319 gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
4ee9c684 3320
c5faa799 3321 gfc_trans_vla_type_sizes (sym, &init);
d4163395 3322
4ee9c684 3323 decl = sym->backend_decl;
3324
afcf285e 3325 /* Emit a DECL_EXPR for this variable, which will cause the
4b3a701c 3326 gimplifier to allocate storage, and all that good stuff. */
fd779e1d 3327 tmp = fold_build1_loc (input_location, DECL_EXPR, TREE_TYPE (decl), decl);
c5faa799 3328 gfc_add_expr_to_block (&init, tmp);
afcf285e 3329
c5faa799 3330 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
4ee9c684 3331}
3332
c8f1568f 3333/* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
3334
c5faa799 3335static void
3336gfc_trans_assign_aux_var (gfc_symbol * sym, gfc_wrapped_block * block)
c8f1568f 3337{
c5faa799 3338 stmtblock_t init;
c8f1568f 3339
3340 gcc_assert (sym->backend_decl);
c5faa799 3341 gfc_start_block (&init);
c8f1568f 3342
3343 /* Set the initial value to length. See the comments in
3344 function gfc_add_assign_aux_vars in this file. */
c5faa799 3345 gfc_add_modify (&init, GFC_DECL_STRING_LEN (sym->backend_decl),
35bf1214 3346 build_int_cst (gfc_charlen_type_node, -2));
c8f1568f 3347
c5faa799 3348 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
c8f1568f 3349}
3350
d4163395 3351static void
3352gfc_trans_vla_one_sizepos (tree *tp, stmtblock_t *body)
3353{
3354 tree t = *tp, var, val;
3355
3356 if (t == NULL || t == error_mark_node)
3357 return;
3358 if (TREE_CONSTANT (t) || DECL_P (t))
3359 return;
3360
3361 if (TREE_CODE (t) == SAVE_EXPR)
3362 {
3363 if (SAVE_EXPR_RESOLVED_P (t))
3364 {
3365 *tp = TREE_OPERAND (t, 0);
3366 return;
3367 }
3368 val = TREE_OPERAND (t, 0);
3369 }
3370 else
3371 val = t;
3372
3373 var = gfc_create_var_np (TREE_TYPE (t), NULL);
3374 gfc_add_decl_to_function (var);
75a70cf9 3375 gfc_add_modify (body, var, val);
d4163395 3376 if (TREE_CODE (t) == SAVE_EXPR)
3377 TREE_OPERAND (t, 0) = var;
3378 *tp = var;
3379}
3380
3381static void
3382gfc_trans_vla_type_sizes_1 (tree type, stmtblock_t *body)
3383{
3384 tree t;
3385
3386 if (type == NULL || type == error_mark_node)
3387 return;
3388
3389 type = TYPE_MAIN_VARIANT (type);
3390
3391 if (TREE_CODE (type) == INTEGER_TYPE)
3392 {
3393 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type), body);
3394 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type), body);
3395
3396 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
3397 {
3398 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
3399 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
3400 }
3401 }
3402 else if (TREE_CODE (type) == ARRAY_TYPE)
3403 {
3404 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type), body);
3405 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type), body);
3406 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type), body);
3407 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type), body);
3408
3409 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
3410 {
3411 TYPE_SIZE (t) = TYPE_SIZE (type);
3412 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
3413 }
3414 }
3415}
3416
3417/* Make sure all type sizes and array domains are either constant,
3418 or variable or parameter decls. This is a simplified variant
3419 of gimplify_type_sizes, but we can't use it here, as none of the
3420 variables in the expressions have been gimplified yet.
3421 As type sizes and domains for various variable length arrays
3422 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
3423 time, without this routine gimplify_type_sizes in the middle-end
3424 could result in the type sizes being gimplified earlier than where
3425 those variables are initialized. */
3426
3427void
3428gfc_trans_vla_type_sizes (gfc_symbol *sym, stmtblock_t *body)
3429{
3430 tree type = TREE_TYPE (sym->backend_decl);
3431
3432 if (TREE_CODE (type) == FUNCTION_TYPE
3433 && (sym->attr.function || sym->attr.result || sym->attr.entry))
3434 {
3435 if (! current_fake_result_decl)
3436 return;
3437
3438 type = TREE_TYPE (TREE_VALUE (current_fake_result_decl));
3439 }
3440
3441 while (POINTER_TYPE_P (type))
3442 type = TREE_TYPE (type);
3443
3444 if (GFC_DESCRIPTOR_TYPE_P (type))
3445 {
3446 tree etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
3447
3448 while (POINTER_TYPE_P (etype))
3449 etype = TREE_TYPE (etype);
3450
3451 gfc_trans_vla_type_sizes_1 (etype, body);
3452 }
3453
3454 gfc_trans_vla_type_sizes_1 (type, body);
3455}
3456
4ee9c684 3457
f0d4969f 3458/* Initialize a derived type by building an lvalue from the symbol
a545a8f8 3459 and using trans_assignment to do the work. Set dealloc to false
3460 if no deallocation prior the assignment is needed. */
c5faa799 3461void
3462gfc_init_default_dt (gfc_symbol * sym, stmtblock_t * block, bool dealloc)
87114d2e 3463{
f0d4969f 3464 gfc_expr *e;
87114d2e 3465 tree tmp;
3466 tree present;
3467
c5faa799 3468 gcc_assert (block);
3469
f0d4969f 3470 gcc_assert (!sym->attr.allocatable);
3471 gfc_set_sym_referenced (sym);
3472 e = gfc_lval_expr_from_sym (sym);
a545a8f8 3473 tmp = gfc_trans_assignment (e, sym->value, false, dealloc);
c38054a8 3474 if (sym->attr.dummy && (sym->attr.optional
3475 || sym->ns->proc_name->attr.entry_master))
87114d2e 3476 {
f0d4969f 3477 present = gfc_conv_expr_present (sym);
2be9d8f1 3478 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
3479 tmp, build_empty_stmt (input_location));
87114d2e 3480 }
c5faa799 3481 gfc_add_expr_to_block (block, tmp);
f0d4969f 3482 gfc_free_expr (e);
87114d2e 3483}
3484
3485
8714fc76 3486/* Initialize INTENT(OUT) derived type dummies. As well as giving
3487 them their default initializer, if they do not have allocatable
3488 components, they have their allocatable components deallocated. */
3489
c5faa799 3490static void
3491init_intent_out_dt (gfc_symbol * proc_sym, gfc_wrapped_block * block)
f0d4969f 3492{
c5faa799 3493 stmtblock_t init;
f0d4969f 3494 gfc_formal_arglist *f;
8714fc76 3495 tree tmp;
5907c3ea 3496 tree present;
f0d4969f 3497
c5faa799 3498 gfc_init_block (&init);
6777213b 3499 for (f = gfc_sym_get_dummy_args (proc_sym); f; f = f->next)
f0d4969f 3500 if (f->sym && f->sym->attr.intent == INTENT_OUT
c49db15e 3501 && !f->sym->attr.pointer
3502 && f->sym->ts.type == BT_DERIVED)
8714fc76 3503 {
942ba353 3504 tmp = NULL_TREE;
3505
3506 /* Note: Allocatables are excluded as they are already handled
3507 by the caller. */
3508 if (!f->sym->attr.allocatable
3509 && gfc_is_finalizable (f->sym->ts.u.derived, NULL))
8714fc76 3510 {
942ba353 3511 stmtblock_t block;
3512 gfc_expr *e;
3513
3514 gfc_init_block (&block);
3515 f->sym->attr.referenced = 1;
3516 e = gfc_lval_expr_from_sym (f->sym);
3517 gfc_add_finalizer_call (&block, e);
3518 gfc_free_expr (e);
3519 tmp = gfc_finish_block (&block);
3520 }
5907c3ea 3521
942ba353 3522 if (tmp == NULL_TREE && !f->sym->attr.allocatable
3523 && f->sym->ts.u.derived->attr.alloc_comp && !f->sym->value)
3524 tmp = gfc_deallocate_alloc_comp (f->sym->ts.u.derived,
3525 f->sym->backend_decl,
3526 f->sym->as ? f->sym->as->rank : 0);
5907c3ea 3527
942ba353 3528 if (tmp != NULL_TREE && (f->sym->attr.optional
3529 || f->sym->ns->proc_name->attr.entry_master))
3530 {
3531 present = gfc_conv_expr_present (f->sym);
3532 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
3533 present, tmp, build_empty_stmt (input_location));
8714fc76 3534 }
942ba353 3535
3536 if (tmp != NULL_TREE)
3537 gfc_add_expr_to_block (&init, tmp);
3538 else if (f->sym->value && !f->sym->attr.allocatable)
c5faa799 3539 gfc_init_default_dt (f->sym, &init, true);
8714fc76 3540 }
c56d57d6 3541 else if (f->sym && f->sym->attr.intent == INTENT_OUT
3542 && f->sym->ts.type == BT_CLASS
3543 && !CLASS_DATA (f->sym)->attr.class_pointer
942ba353 3544 && !CLASS_DATA (f->sym)->attr.allocatable)
c56d57d6 3545 {
942ba353 3546 stmtblock_t block;
3547 gfc_expr *e;
3548
3549 gfc_init_block (&block);
3550 f->sym->attr.referenced = 1;
3551 e = gfc_lval_expr_from_sym (f->sym);
3552 gfc_add_finalizer_call (&block, e);
3553 gfc_free_expr (e);
3554 tmp = gfc_finish_block (&block);
c56d57d6 3555
3556 if (f->sym->attr.optional || f->sym->ns->proc_name->attr.entry_master)
3557 {
3558 present = gfc_conv_expr_present (f->sym);
3559 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
3560 present, tmp,
3561 build_empty_stmt (input_location));
3562 }
3563
3564 gfc_add_expr_to_block (&init, tmp);
3565 }
f0d4969f 3566
c5faa799 3567 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
f0d4969f 3568}
3569
87114d2e 3570
4ee9c684 3571/* Generate function entry and exit code, and add it to the function body.
3572 This includes:
f888a3fb 3573 Allocation and initialization of array variables.
4ee9c684 3574 Allocation of character string variables.
c8f1568f 3575 Initialization and possibly repacking of dummy arrays.
0a96a7cc 3576 Initialization of ASSIGN statement auxiliary variable.
8f3f9eab 3577 Initialization of ASSOCIATE names.
0a96a7cc 3578 Automatic deallocation. */
4ee9c684 3579
89ac8ba1 3580void
3581gfc_trans_deferred_vars (gfc_symbol * proc_sym, gfc_wrapped_block * block)
4ee9c684 3582{
3583 locus loc;
3584 gfc_symbol *sym;
d4163395 3585 gfc_formal_arglist *f;
c5faa799 3586 stmtblock_t tmpblock;
25dd7350 3587 bool seen_trans_deferred_array = false;
617125a6 3588 tree tmp = NULL;
3589 gfc_expr *e;
3590 gfc_se se;
3591 stmtblock_t init;
4ee9c684 3592
3593 /* Deal with implicit return variables. Explicit return variables will
3594 already have been added. */
3595 if (gfc_return_by_reference (proc_sym) && proc_sym->result == proc_sym)
3596 {
3597 if (!current_fake_result_decl)
3598 {
c6871095 3599 gfc_entry_list *el = NULL;
3600 if (proc_sym->attr.entry_master)
3601 {
3602 for (el = proc_sym->ns->entries; el; el = el->next)
3603 if (el->sym != el->sym->result)
3604 break;
3605 }
fa7b6574 3606 /* TODO: move to the appropriate place in resolve.c. */
3607 if (warn_return_type && el == NULL)
3608 gfc_warning ("Return value of function '%s' at %L not set",
3609 proc_sym->name, &proc_sym->declared_at);
4ee9c684 3610 }
c6871095 3611 else if (proc_sym->as)
4ee9c684 3612 {
d4163395 3613 tree result = TREE_VALUE (current_fake_result_decl);
89ac8ba1 3614 gfc_trans_dummy_array_bias (proc_sym, result, block);
10b07432 3615
3616 /* An automatic character length, pointer array result. */
3617 if (proc_sym->ts.type == BT_CHARACTER
eeebe20b 3618 && TREE_CODE (proc_sym->ts.u.cl->backend_decl) == VAR_DECL)
89ac8ba1 3619 gfc_trans_dummy_character (proc_sym, proc_sym->ts.u.cl, block);
4ee9c684 3620 }
3621 else if (proc_sym->ts.type == BT_CHARACTER)
3622 {
617125a6 3623 if (proc_sym->ts.deferred)
3624 {
3625 tmp = NULL;
da2c4122 3626 gfc_save_backend_locus (&loc);
3627 gfc_set_backend_locus (&proc_sym->declared_at);
617125a6 3628 gfc_start_block (&init);
3629 /* Zero the string length on entry. */
3630 gfc_add_modify (&init, proc_sym->ts.u.cl->backend_decl,
3631 build_int_cst (gfc_charlen_type_node, 0));
3632 /* Null the pointer. */
3633 e = gfc_lval_expr_from_sym (proc_sym);
3634 gfc_init_se (&se, NULL);
3635 se.want_pointer = 1;
3636 gfc_conv_expr (&se, e);
3637 gfc_free_expr (e);
3638 tmp = se.expr;
3639 gfc_add_modify (&init, tmp,
3640 fold_convert (TREE_TYPE (se.expr),
3641 null_pointer_node));
da2c4122 3642 gfc_restore_backend_locus (&loc);
617125a6 3643
3644 /* Pass back the string length on exit. */
3645 tmp = proc_sym->ts.u.cl->passed_length;
3646 tmp = build_fold_indirect_ref_loc (input_location, tmp);
3647 tmp = fold_convert (gfc_charlen_type_node, tmp);
3648 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
3649 gfc_charlen_type_node, tmp,
3650 proc_sym->ts.u.cl->backend_decl);
3651 gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
3652 }
3653 else if (TREE_CODE (proc_sym->ts.u.cl->backend_decl) == VAR_DECL)
89ac8ba1 3654 gfc_trans_dummy_character (proc_sym, proc_sym->ts.u.cl, block);
4ee9c684 3655 }
3656 else
bdaed7d2 3657 gcc_assert (gfc_option.flag_f2c
3658 && proc_sym->ts.type == BT_COMPLEX);
4ee9c684 3659 }
3660
87114d2e 3661 /* Initialize the INTENT(OUT) derived type dummy arguments. This
3662 should be done here so that the offsets and lbounds of arrays
3663 are available. */
da2c4122 3664 gfc_save_backend_locus (&loc);
3665 gfc_set_backend_locus (&proc_sym->declared_at);
89ac8ba1 3666 init_intent_out_dt (proc_sym, block);
da2c4122 3667 gfc_restore_backend_locus (&loc);
87114d2e 3668
4ee9c684 3669 for (sym = proc_sym->tlink; sym != proc_sym; sym = sym->tlink)
3670 {
2294b616 3671 bool sym_has_alloc_comp = (sym->ts.type == BT_DERIVED)
eeebe20b 3672 && sym->ts.u.derived->attr.alloc_comp;
8f3f9eab 3673 if (sym->assoc)
3c82e013 3674 continue;
3675
a56d63bc 3676 if (sym->attr.subref_array_pointer
3677 && GFC_DECL_SPAN (sym->backend_decl)
3678 && !TREE_STATIC (GFC_DECL_SPAN (sym->backend_decl)))
3679 {
3680 gfc_init_block (&tmpblock);
3681 gfc_add_modify (&tmpblock, GFC_DECL_SPAN (sym->backend_decl),
3682 build_int_cst (gfc_array_index_type, 0));
3683 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock),
3684 NULL_TREE);
3685 }
3686
85ae3cab 3687 if (sym->ts.type == BT_CLASS
3688 && (sym->attr.save || gfc_option.flag_max_stack_var_size == 0)
fb139b21 3689 && CLASS_DATA (sym)->attr.allocatable)
3690 {
3691 tree vptr;
3692
3693 if (UNLIMITED_POLY (sym))
3694 vptr = null_pointer_node;
3695 else
3696 {
3697 gfc_symbol *vsym;
3698 vsym = gfc_find_derived_vtab (sym->ts.u.derived);
3699 vptr = gfc_get_symbol_decl (vsym);
3700 vptr = gfc_build_addr_expr (NULL, vptr);
3701 }
3702
3703 if (CLASS_DATA (sym)->attr.dimension
3704 || (CLASS_DATA (sym)->attr.codimension
3705 && gfc_option.coarray != GFC_FCOARRAY_LIB))
3706 {
3707 tmp = gfc_class_data_get (sym->backend_decl);
3708 tmp = gfc_build_null_descriptor (TREE_TYPE (tmp));
3709 }
3710 else
3711 tmp = null_pointer_node;
3712
3713 DECL_INITIAL (sym->backend_decl)
3714 = gfc_class_set_static_fields (sym->backend_decl, vptr, tmp);
3715 TREE_CONSTANT (DECL_INITIAL (sym->backend_decl)) = 1;
3716 }
3717 else if (sym->attr.dimension || sym->attr.codimension)
4ee9c684 3718 {
f7a6fca4 3719 /* Assumed-size Cray pointees need to be treated as AS_EXPLICIT. */
3720 array_type tmp = sym->as->type;
3721 if (tmp == AS_ASSUMED_SIZE && sym->as->cp_was_assumed)
3722 tmp = AS_EXPLICIT;
3723 switch (tmp)
4ee9c684 3724 {
3725 case AS_EXPLICIT:
3726 if (sym->attr.dummy || sym->attr.result)
89ac8ba1 3727 gfc_trans_dummy_array_bias (sym, sym->backend_decl, block);
4ee9c684 3728 else if (sym->attr.pointer || sym->attr.allocatable)
3729 {
3730 if (TREE_STATIC (sym->backend_decl))
da2c4122 3731 {
3732 gfc_save_backend_locus (&loc);
3733 gfc_set_backend_locus (&sym->declared_at);
3734 gfc_trans_static_array_pointer (sym);
3735 gfc_restore_backend_locus (&loc);
3736 }
4ee9c684 3737 else
25dd7350 3738 {
3739 seen_trans_deferred_array = true;
89ac8ba1 3740 gfc_trans_deferred_array (sym, block);
25dd7350 3741 }
4ee9c684 3742 }
7c7db7f6 3743 else if (sym->attr.codimension && TREE_STATIC (sym->backend_decl))
3744 {
3745 gfc_init_block (&tmpblock);
3746 gfc_trans_array_cobounds (TREE_TYPE (sym->backend_decl),
3747 &tmpblock, sym);
3748 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock),
3749 NULL_TREE);
3750 continue;
3751 }
a961ca30 3752 else if (gfc_option.coarray != GFC_FCOARRAY_LIB)
4ee9c684 3753 {
da2c4122 3754 gfc_save_backend_locus (&loc);
3755 gfc_set_backend_locus (&sym->declared_at);
3756
25dd7350 3757 if (sym_has_alloc_comp)
3758 {
3759 seen_trans_deferred_array = true;
89ac8ba1 3760 gfc_trans_deferred_array (sym, block);
25dd7350 3761 }
f0d4969f 3762 else if (sym->ts.type == BT_DERIVED
3763 && sym->value
3764 && !sym->attr.data
3765 && sym->attr.save == SAVE_NONE)
c5faa799 3766 {
3767 gfc_start_block (&tmpblock);
3768 gfc_init_default_dt (sym, &tmpblock, false);
89ac8ba1 3769 gfc_add_init_cleanup (block,
c5faa799 3770 gfc_finish_block (&tmpblock),
3771 NULL_TREE);
3772 }
25dd7350 3773
c5faa799 3774 gfc_trans_auto_array_allocation (sym->backend_decl,
89ac8ba1 3775 sym, block);
4671339c 3776 gfc_restore_backend_locus (&loc);
4ee9c684 3777 }
3778 break;
3779
3780 case AS_ASSUMED_SIZE:
3781 /* Must be a dummy parameter. */
452695a8 3782 gcc_assert (sym->attr.dummy || sym->as->cp_was_assumed);
4ee9c684 3783
3784 /* We should always pass assumed size arrays the g77 way. */
452695a8 3785 if (sym->attr.dummy)
89ac8ba1 3786 gfc_trans_g77_array (sym, block);
c5faa799 3787 break;
4ee9c684 3788
3789 case AS_ASSUMED_SHAPE:
3790 /* Must be a dummy parameter. */
22d678e8 3791 gcc_assert (sym->attr.dummy);
4ee9c684 3792
89ac8ba1 3793 gfc_trans_dummy_array_bias (sym, sym->backend_decl, block);
4ee9c684 3794 break;
3795
f00f6dd6 3796 case AS_ASSUMED_RANK:
4ee9c684 3797 case AS_DEFERRED:
25dd7350 3798 seen_trans_deferred_array = true;
89ac8ba1 3799 gfc_trans_deferred_array (sym, block);
4ee9c684 3800 break;
3801
3802 default:
22d678e8 3803 gcc_unreachable ();
4ee9c684 3804 }
25dd7350 3805 if (sym_has_alloc_comp && !seen_trans_deferred_array)
89ac8ba1 3806 gfc_trans_deferred_array (sym, block);
4ee9c684 3807 }
fd23cc08 3808 else if ((!sym->attr.dummy || sym->ts.deferred)
3809 && (sym->ts.type == BT_CLASS
3a19c063 3810 && CLASS_DATA (sym)->attr.class_pointer))
2930c007 3811 continue;
617125a6 3812 else if ((!sym->attr.dummy || sym->ts.deferred)
456dd7d6 3813 && (sym->attr.allocatable
3814 || (sym->ts.type == BT_CLASS
3815 && CLASS_DATA (sym)->attr.allocatable)))
0a96a7cc 3816 {
be125be0 3817 if (!sym->attr.save && gfc_option.flag_max_stack_var_size != 0)
908e9973 3818 {
d0d776fb 3819 tree descriptor = NULL_TREE;
3820
908e9973 3821 /* Nullify and automatic deallocation of allocatable
3822 scalars. */
908e9973 3823 e = gfc_lval_expr_from_sym (sym);
3824 if (sym->ts.type == BT_CLASS)
607ae689 3825 gfc_add_data_component (e);
908e9973 3826
3827 gfc_init_se (&se, NULL);
fd23cc08 3828 if (sym->ts.type != BT_CLASS
3829 || sym->ts.u.derived->attr.dimension
3830 || sym->ts.u.derived->attr.codimension)
3831 {
3832 se.want_pointer = 1;
3833 gfc_conv_expr (&se, e);
3834 }
3835 else if (sym->ts.type == BT_CLASS
3836 && !CLASS_DATA (sym)->attr.dimension
3837 && !CLASS_DATA (sym)->attr.codimension)
3838 {
3839 se.want_pointer = 1;
3840 gfc_conv_expr (&se, e);
3841 }
3842 else
3843 {
3844 gfc_conv_expr (&se, e);
d0d776fb 3845 descriptor = se.expr;
fd23cc08 3846 se.expr = gfc_conv_descriptor_data_addr (se.expr);
3847 se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
3848 }
908e9973 3849 gfc_free_expr (e);
3850
da2c4122 3851 gfc_save_backend_locus (&loc);
3852 gfc_set_backend_locus (&sym->declared_at);
c5faa799 3853 gfc_start_block (&init);
617125a6 3854
3855 if (!sym->attr.dummy || sym->attr.intent == INTENT_OUT)
3856 {
3857 /* Nullify when entering the scope. */
3858 gfc_add_modify (&init, se.expr,
3859 fold_convert (TREE_TYPE (se.expr),
3860 null_pointer_node));
3861 }
3862
8b9eb27e 3863 if ((sym->attr.dummy ||sym->attr.result)
617125a6 3864 && sym->ts.type == BT_CHARACTER
3865 && sym->ts.deferred)
3866 {
3867 /* Character length passed by reference. */
3868 tmp = sym->ts.u.cl->passed_length;
3869 tmp = build_fold_indirect_ref_loc (input_location, tmp);
3870 tmp = fold_convert (gfc_charlen_type_node, tmp);
3871
3872 if (!sym->attr.dummy || sym->attr.intent == INTENT_OUT)
3873 /* Zero the string length when entering the scope. */
3874 gfc_add_modify (&init, sym->ts.u.cl->backend_decl,
3875 build_int_cst (gfc_charlen_type_node, 0));
3876 else
3877 gfc_add_modify (&init, sym->ts.u.cl->backend_decl, tmp);
3878
da2c4122 3879 gfc_restore_backend_locus (&loc);
3880
617125a6 3881 /* Pass the final character length back. */
3882 if (sym->attr.intent != INTENT_IN)
3883 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
3884 gfc_charlen_type_node, tmp,
3885 sym->ts.u.cl->backend_decl);
3886 else
3887 tmp = NULL_TREE;
3888 }
da2c4122 3889 else
3890 gfc_restore_backend_locus (&loc);
908e9973 3891
3892 /* Deallocate when leaving the scope. Nullifying is not
3893 needed. */
afb69dbf 3894 if (!sym->attr.result && !sym->attr.dummy
3895 && !sym->ns->proc_name->attr.is_main_program)
d0d776fb 3896 {
3897 if (sym->ts.type == BT_CLASS
3898 && CLASS_DATA (sym)->attr.codimension)
3899 tmp = gfc_deallocate_with_status (descriptor, NULL_TREE,
3900 NULL_TREE, NULL_TREE,
3901 NULL_TREE, true, NULL,
3902 true);
3903 else
c23adf93 3904 {
3905 gfc_expr *expr = gfc_lval_expr_from_sym (sym);
3906 tmp = gfc_deallocate_scalar_with_status (se.expr, NULL_TREE,
3907 true, expr, sym->ts);
3908 gfc_free_expr (expr);
3909 }
d0d776fb 3910 }
afc44c79 3911 if (sym->ts.type == BT_CLASS)
3912 {
3913 /* Initialize _vptr to declared type. */
a90fe829 3914 gfc_symbol *vtab;
afc44c79 3915 tree rhs;
da2c4122 3916
3917 gfc_save_backend_locus (&loc);
3918 gfc_set_backend_locus (&sym->declared_at);
afc44c79 3919 e = gfc_lval_expr_from_sym (sym);
3920 gfc_add_vptr_component (e);
3921 gfc_init_se (&se, NULL);
3922 se.want_pointer = 1;
3923 gfc_conv_expr (&se, e);
3924 gfc_free_expr (e);
a90fe829 3925 if (UNLIMITED_POLY (sym))
3926 rhs = build_int_cst (TREE_TYPE (se.expr), 0);
3927 else
3928 {
3929 vtab = gfc_find_derived_vtab (sym->ts.u.derived);
3930 rhs = gfc_build_addr_expr (TREE_TYPE (se.expr),
3931 gfc_get_symbol_decl (vtab));
3932 }
afc44c79 3933 gfc_add_modify (&init, se.expr, rhs);
da2c4122 3934 gfc_restore_backend_locus (&loc);
afc44c79 3935 }
3936
89ac8ba1 3937 gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
908e9973 3938 }
0a96a7cc 3939 }
617125a6 3940 else if (sym->ts.type == BT_CHARACTER && sym->ts.deferred)
3941 {
3942 tree tmp = NULL;
3943 stmtblock_t init;
3944
3945 /* If we get to here, all that should be left are pointers. */
3946 gcc_assert (sym->attr.pointer);
3947
3948 if (sym->attr.dummy)
3949 {
3950 gfc_start_block (&init);
3951
3952 /* Character length passed by reference. */
3953 tmp = sym->ts.u.cl->passed_length;
3954 tmp = build_fold_indirect_ref_loc (input_location, tmp);
3955 tmp = fold_convert (gfc_charlen_type_node, tmp);
3956 gfc_add_modify (&init, sym->ts.u.cl->backend_decl, tmp);
3957 /* Pass the final character length back. */
3958 if (sym->attr.intent != INTENT_IN)
3959 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
3960 gfc_charlen_type_node, tmp,
3961 sym->ts.u.cl->backend_decl);
3962 else
3963 tmp = NULL_TREE;
3964 gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
3965 }
3966 }
3e715c81 3967 else if (sym->ts.deferred)
3968 gfc_fatal_error ("Deferred type parameter not yet supported");
fabc1fc9 3969 else if (sym_has_alloc_comp)
89ac8ba1 3970 gfc_trans_deferred_array (sym, block);
4ee9c684 3971 else if (sym->ts.type == BT_CHARACTER)
3972 {
4671339c 3973 gfc_save_backend_locus (&loc);
4ee9c684 3974 gfc_set_backend_locus (&sym->declared_at);
3975 if (sym->attr.dummy || sym->attr.result)
89ac8ba1 3976 gfc_trans_dummy_character (sym, sym->ts.u.cl, block);
4ee9c684 3977 else
89ac8ba1 3978 gfc_trans_auto_character_variable (sym, block);
4671339c 3979 gfc_restore_backend_locus (&loc);
4ee9c684 3980 }
c8f1568f 3981 else if (sym->attr.assign)
3982 {
4671339c 3983 gfc_save_backend_locus (&loc);
c8f1568f 3984 gfc_set_backend_locus (&sym->declared_at);
89ac8ba1 3985 gfc_trans_assign_aux_var (sym, block);
4671339c 3986 gfc_restore_backend_locus (&loc);
c8f1568f 3987 }
f0d4969f 3988 else if (sym->ts.type == BT_DERIVED
3989 && sym->value
3990 && !sym->attr.data
3991 && sym->attr.save == SAVE_NONE)
c5faa799 3992 {
3993 gfc_start_block (&tmpblock);
3994 gfc_init_default_dt (sym, &tmpblock, false);
89ac8ba1 3995 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock),
c5faa799 3996 NULL_TREE);
3997 }
a90fe829 3998 else if (!(UNLIMITED_POLY(sym)))
22d678e8 3999 gcc_unreachable ();
4ee9c684 4000 }
4001
c5faa799 4002 gfc_init_block (&tmpblock);
d4163395 4003
6777213b 4004 for (f = gfc_sym_get_dummy_args (proc_sym); f; f = f->next)
1e853e89 4005 {
4006 if (f->sym && f->sym->tlink == NULL && f->sym->ts.type == BT_CHARACTER)
4007 {
eeebe20b 4008 gcc_assert (f->sym->ts.u.cl->backend_decl != NULL);
4009 if (TREE_CODE (f->sym->ts.u.cl->backend_decl) == PARM_DECL)
c5faa799 4010 gfc_trans_vla_type_sizes (f->sym, &tmpblock);
1e853e89 4011 }
1e853e89 4012 }
d4163395 4013
4014 if (gfc_return_by_reference (proc_sym) && proc_sym->ts.type == BT_CHARACTER
4015 && current_fake_result_decl != NULL)
4016 {
eeebe20b 4017 gcc_assert (proc_sym->ts.u.cl->backend_decl != NULL);
4018 if (TREE_CODE (proc_sym->ts.u.cl->backend_decl) == PARM_DECL)
c5faa799 4019 gfc_trans_vla_type_sizes (proc_sym, &tmpblock);
d4163395 4020 }
4021
89ac8ba1 4022 gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), NULL_TREE);
4ee9c684 4023}
4024
df4d540f 4025static GTY ((param_is (struct module_htab_entry))) htab_t module_htab;
4026
4027/* Hash and equality functions for module_htab. */
4028
4029static hashval_t
4030module_htab_do_hash (const void *x)
4031{
4032 return htab_hash_string (((const struct module_htab_entry *)x)->name);
4033}
4034
4035static int
4036module_htab_eq (const void *x1, const void *x2)
4037{
4038 return strcmp ((((const struct module_htab_entry *)x1)->name),
4039 (const char *)x2) == 0;
4040}
4041
4042/* Hash and equality functions for module_htab's decls. */
4043
4044static hashval_t
4045module_htab_decls_hash (const void *x)
4046{
4047 const_tree t = (const_tree) x;
4048 const_tree n = DECL_NAME (t);
4049 if (n == NULL_TREE)
4050 n = TYPE_NAME (TREE_TYPE (t));
8f1e8e0e 4051 return htab_hash_string (IDENTIFIER_POINTER (n));
df4d540f 4052}
4053
4054static int
4055module_htab_decls_eq (const void *x1, const void *x2)
4056{
4057 const_tree t1 = (const_tree) x1;
4058 const_tree n1 = DECL_NAME (t1);
4059 if (n1 == NULL_TREE)
4060 n1 = TYPE_NAME (TREE_TYPE (t1));
4061 return strcmp (IDENTIFIER_POINTER (n1), (const char *) x2) == 0;
4062}
4063
4064struct module_htab_entry *
4065gfc_find_module (const char *name)
4066{
4067 void **slot;
4068
4069 if (! module_htab)
4070 module_htab = htab_create_ggc (10, module_htab_do_hash,
4071 module_htab_eq, NULL);
4072
4073 slot = htab_find_slot_with_hash (module_htab, name,
4074 htab_hash_string (name), INSERT);
4075 if (*slot == NULL)
4076 {
ba72912a 4077 struct module_htab_entry *entry = ggc_alloc_cleared_module_htab_entry ();
df4d540f 4078
4079 entry->name = gfc_get_string (name);
4080 entry->decls = htab_create_ggc (10, module_htab_decls_hash,
4081 module_htab_decls_eq, NULL);
4082 *slot = (void *) entry;
4083 }
4084 return (struct module_htab_entry *) *slot;
4085}
4086
4087void
4088gfc_module_add_decl (struct module_htab_entry *entry, tree decl)
4089{
4090 void **slot;
4091 const char *name;
4092
4093 if (DECL_NAME (decl))
4094 name = IDENTIFIER_POINTER (DECL_NAME (decl));
4095 else
4096 {
4097 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
4098 name = IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl)));
4099 }
4100 slot = htab_find_slot_with_hash (entry->decls, name,
4101 htab_hash_string (name), INSERT);
4102 if (*slot == NULL)
4103 *slot = (void *) decl;
4104}
4105
4106static struct module_htab_entry *cur_module;
4ee9c684 4107
4108/* Output an initialized decl for a module variable. */
4109
4110static void
4111gfc_create_module_variable (gfc_symbol * sym)
4112{
4113 tree decl;
4ee9c684 4114
d77f260f 4115 /* Module functions with alternate entries are dealt with later and
4116 would get caught by the next condition. */
4117 if (sym->attr.entry)
4118 return;
4119
c5d33754 4120 /* Make sure we convert the types of the derived types from iso_c_binding
4121 into (void *). */
4122 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
4123 && sym->ts.type == BT_DERIVED)
4124 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
4125
df4d540f 4126 if (sym->attr.flavor == FL_DERIVED
4127 && sym->backend_decl
4128 && TREE_CODE (sym->backend_decl) == RECORD_TYPE)
4129 {
4130 decl = sym->backend_decl;
4131 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
9f1470cb 4132
044bbd71 4133 if (!sym->attr.use_assoc)
9f1470cb 4134 {
4135 gcc_assert (TYPE_CONTEXT (decl) == NULL_TREE
4136 || TYPE_CONTEXT (decl) == sym->ns->proc_name->backend_decl);
4137 gcc_assert (DECL_CONTEXT (TYPE_STUB_DECL (decl)) == NULL_TREE
4138 || DECL_CONTEXT (TYPE_STUB_DECL (decl))
4139 == sym->ns->proc_name->backend_decl);
4140 }
df4d540f 4141 TYPE_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
4142 DECL_CONTEXT (TYPE_STUB_DECL (decl)) = sym->ns->proc_name->backend_decl;
4143 gfc_module_add_decl (cur_module, TYPE_STUB_DECL (decl));
4144 }
4145
cf269acc 4146 /* Only output variables, procedure pointers and array valued,
4147 or derived type, parameters. */
4ee9c684 4148 if (sym->attr.flavor != FL_VARIABLE
be0f1581 4149 && !(sym->attr.flavor == FL_PARAMETER
cf269acc 4150 && (sym->attr.dimension || sym->ts.type == BT_DERIVED))
4151 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer))
4ee9c684 4152 return;
4153
df4d540f 4154 if ((sym->attr.in_common || sym->attr.in_equivalence) && sym->backend_decl)
4155 {
4156 decl = sym->backend_decl;
16a40513 4157 gcc_assert (DECL_FILE_SCOPE_P (decl));
df4d540f 4158 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
4159 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
4160 gfc_module_add_decl (cur_module, decl);
4161 }
4162
d43a7f7f 4163 /* Don't generate variables from other modules. Variables from
4164 COMMONs will already have been generated. */
4165 if (sym->attr.use_assoc || sym->attr.in_common)
4ee9c684 4166 return;
4167
2b685f8e 4168 /* Equivalenced variables arrive here after creation. */
976d903a 4169 if (sym->backend_decl
df4d540f 4170 && (sym->equiv_built || sym->attr.in_equivalence))
4171 return;
2b685f8e 4172
23d075f4 4173 if (sym->backend_decl && !sym->attr.vtab && !sym->attr.target)
4ee9c684 4174 internal_error ("backend decl for module variable %s already exists",
4175 sym->name);
4176
4177 /* We always want module variables to be created. */
4178 sym->attr.referenced = 1;
4179 /* Create the decl. */
4180 decl = gfc_get_symbol_decl (sym);
4181
4ee9c684 4182 /* Create the variable. */
4183 pushdecl (decl);
df4d540f 4184 gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
4185 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
b2c4af5e 4186 rest_of_decl_compilation (decl, 1, 0);
df4d540f 4187 gfc_module_add_decl (cur_module, decl);
4ee9c684 4188
4189 /* Also add length of strings. */
4190 if (sym->ts.type == BT_CHARACTER)
4191 {
4192 tree length;
4193
eeebe20b 4194 length = sym->ts.u.cl->backend_decl;
cf4b41d8 4195 gcc_assert (length || sym->attr.proc_pointer);
4196 if (length && !INTEGER_CST_P (length))
4ee9c684 4197 {
4198 pushdecl (length);
b2c4af5e 4199 rest_of_decl_compilation (length, 1, 0);
4ee9c684 4200 }
4201 }
a961ca30 4202
4203 if (sym->attr.codimension && !sym->attr.dummy && !sym->attr.allocatable
4204 && sym->attr.referenced && !sym->attr.use_assoc)
4205 has_coarray_vars = true;
4ee9c684 4206}
4207
51d9479b 4208/* Emit debug information for USE statements. */
df4d540f 4209
4210static void
4211gfc_trans_use_stmts (gfc_namespace * ns)
4212{
4213 gfc_use_list *use_stmt;
4214 for (use_stmt = ns->use_stmts; use_stmt; use_stmt = use_stmt->next)
4215 {
4216 struct module_htab_entry *entry
4217 = gfc_find_module (use_stmt->module_name);
4218 gfc_use_rename *rent;
4219
4220 if (entry->namespace_decl == NULL)
4221 {
4222 entry->namespace_decl
e60a6f7b 4223 = build_decl (input_location,
4224 NAMESPACE_DECL,
df4d540f 4225 get_identifier (use_stmt->module_name),
4226 void_type_node);
4227 DECL_EXTERNAL (entry->namespace_decl) = 1;
4228 }
51d9479b 4229 gfc_set_backend_locus (&use_stmt->where);
df4d540f 4230 if (!use_stmt->only_flag)
4231 (*debug_hooks->imported_module_or_decl) (entry->namespace_decl,
4232 NULL_TREE,
4233 ns->proc_name->backend_decl,
4234 false);
4235 for (rent = use_stmt->rename; rent; rent = rent->next)
4236 {
4237 tree decl, local_name;
4238 void **slot;
4239
4240 if (rent->op != INTRINSIC_NONE)
4241 continue;
4242
4243 slot = htab_find_slot_with_hash (entry->decls, rent->use_name,
4244 htab_hash_string (rent->use_name),
4245 INSERT);
4246 if (*slot == NULL)
4247 {
4248 gfc_symtree *st;
4249
4250 st = gfc_find_symtree (ns->sym_root,
4251 rent->local_name[0]
4252 ? rent->local_name : rent->use_name);
c2958b6b 4253
4254 /* The following can happen if a derived type is renamed. */
4255 if (!st)
4256 {
4257 char *name;
4258 name = xstrdup (rent->local_name[0]
4259 ? rent->local_name : rent->use_name);
4260 name[0] = (char) TOUPPER ((unsigned char) name[0]);
4261 st = gfc_find_symtree (ns->sym_root, name);
4262 free (name);
4263 gcc_assert (st);
4264 }
857c96ba 4265
4266 /* Sometimes, generic interfaces wind up being over-ruled by a
4267 local symbol (see PR41062). */
4268 if (!st->n.sym->attr.use_assoc)
4269 continue;
4270
51d9479b 4271 if (st->n.sym->backend_decl
4272 && DECL_P (st->n.sym->backend_decl)
4273 && st->n.sym->module
4274 && strcmp (st->n.sym->module, use_stmt->module_name) == 0)
df4d540f 4275 {
51d9479b 4276 gcc_assert (DECL_EXTERNAL (entry->namespace_decl)
4277 || (TREE_CODE (st->n.sym->backend_decl)
4278 != VAR_DECL));
df4d540f 4279 decl = copy_node (st->n.sym->backend_decl);
4280 DECL_CONTEXT (decl) = entry->namespace_decl;
4281 DECL_EXTERNAL (decl) = 1;
4282 DECL_IGNORED_P (decl) = 0;
4283 DECL_INITIAL (decl) = NULL_TREE;
4284 }
4285 else
4286 {
4287 *slot = error_mark_node;
4288 htab_clear_slot (entry->decls, slot);
4289 continue;
4290 }
4291 *slot = decl;
4292 }
4293 decl = (tree) *slot;
4294 if (rent->local_name[0])
4295 local_name = get_identifier (rent->local_name);
4296 else
4297 local_name = NULL_TREE;
51d9479b 4298 gfc_set_backend_locus (&rent->where);
df4d540f 4299 (*debug_hooks->imported_module_or_decl) (decl, local_name,
4300 ns->proc_name->backend_decl,
4301 !use_stmt->only_flag);
4302 }
4303 }
4ee9c684 4304}
4305
51d9479b 4306
2eb674c9 4307/* Return true if expr is a constant initializer that gfc_conv_initializer
4308 will handle. */
4309
4310static bool
4311check_constant_initializer (gfc_expr *expr, gfc_typespec *ts, bool array,
4312 bool pointer)
4313{
4314 gfc_constructor *c;
4315 gfc_component *cm;
4316
4317 if (pointer)
4318 return true;
4319 else if (array)
4320 {
4321 if (expr->expr_type == EXPR_CONSTANT || expr->expr_type == EXPR_NULL)
4322 return true;
4323 else if (expr->expr_type == EXPR_STRUCTURE)
4324 return check_constant_initializer (expr, ts, false, false);
4325 else if (expr->expr_type != EXPR_ARRAY)
4326 return false;
126387b5 4327 for (c = gfc_constructor_first (expr->value.constructor);
4328 c; c = gfc_constructor_next (c))
2eb674c9 4329 {
4330 if (c->iterator)
4331 return false;
4332 if (c->expr->expr_type == EXPR_STRUCTURE)
4333 {
4334 if (!check_constant_initializer (c->expr, ts, false, false))
4335 return false;
4336 }
4337 else if (c->expr->expr_type != EXPR_CONSTANT)
4338 return false;
4339 }
4340 return true;
4341 }
4342 else switch (ts->type)
4343 {
4344 case BT_DERIVED:
4345 if (expr->expr_type != EXPR_STRUCTURE)
4346 return false;
eeebe20b 4347 cm = expr->ts.u.derived->components;
126387b5 4348 for (c = gfc_constructor_first (expr->value.constructor);
4349 c; c = gfc_constructor_next (c), cm = cm->next)
2eb674c9 4350 {
4351 if (!c->expr || cm->attr.allocatable)
4352 continue;
4353 if (!check_constant_initializer (c->expr, &cm->ts,
4354 cm->attr.dimension,
4355 cm->attr.pointer))
4356 return false;
4357 }
4358 return true;
4359 default:
4360 return expr->expr_type == EXPR_CONSTANT;
4361 }
4362}
4363
4364/* Emit debug info for parameters and unreferenced variables with
4365 initializers. */
4366
4367static void
4368gfc_emit_parameter_debug_info (gfc_symbol *sym)
4369{
4370 tree decl;
4371
4372 if (sym->attr.flavor != FL_PARAMETER
4373 && (sym->attr.flavor != FL_VARIABLE || sym->attr.referenced))
4374 return;
4375
4376 if (sym->backend_decl != NULL
4377 || sym->value == NULL
4378 || sym->attr.use_assoc
4379 || sym->attr.dummy
4380 || sym->attr.result
4381 || sym->attr.function
4382 || sym->attr.intrinsic
4383 || sym->attr.pointer
4384 || sym->attr.allocatable
4385 || sym->attr.cray_pointee
4386 || sym->attr.threadprivate
4387 || sym->attr.is_bind_c
4388 || sym->attr.subref_array_pointer
4389 || sym->attr.assign)
4390 return;
4391
4392 if (sym->ts.type == BT_CHARACTER)
4393 {
eeebe20b 4394 gfc_conv_const_charlen (sym->ts.u.cl);
4395 if (sym->ts.u.cl->backend_decl == NULL
4396 || TREE_CODE (sym->ts.u.cl->backend_decl) != INTEGER_CST)
2eb674c9 4397 return;
4398 }
eeebe20b 4399 else if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.alloc_comp)
2eb674c9 4400 return;
4401
4402 if (sym->as)
4403 {
4404 int n;
4405
4406 if (sym->as->type != AS_EXPLICIT)
4407 return;
4408 for (n = 0; n < sym->as->rank; n++)
4409 if (sym->as->lower[n]->expr_type != EXPR_CONSTANT
4410 || sym->as->upper[n] == NULL
4411 || sym->as->upper[n]->expr_type != EXPR_CONSTANT)
4412 return;
4413 }
4414
4415 if (!check_constant_initializer (sym->value, &sym->ts,
4416 sym->attr.dimension, false))
4417 return;
4418
a961ca30 4419 if (gfc_option.coarray == GFC_FCOARRAY_LIB && sym->attr.codimension)
4420 return;
4421
2eb674c9 4422 /* Create the decl for the variable or constant. */
e60a6f7b 4423 decl = build_decl (input_location,
4424 sym->attr.flavor == FL_PARAMETER ? CONST_DECL : VAR_DECL,
2eb674c9 4425 gfc_sym_identifier (sym), gfc_sym_type (sym));
4426 if (sym->attr.flavor == FL_PARAMETER)
4427 TREE_READONLY (decl) = 1;
4428 gfc_set_decl_location (decl, &sym->declared_at);
4429 if (sym->attr.dimension)
4430 GFC_DECL_PACKED_ARRAY (decl) = 1;
4431 DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
4432 TREE_STATIC (decl) = 1;
4433 TREE_USED (decl) = 1;
4434 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
4435 TREE_PUBLIC (decl) = 1;
802532b9 4436 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
4437 TREE_TYPE (decl),
4438 sym->attr.dimension,
4439 false, false);
2eb674c9 4440 debug_hooks->global_decl (decl);
4441}
4442
a961ca30 4443
4444static void
4445generate_coarray_sym_init (gfc_symbol *sym)
4446{
4447 tree tmp, size, decl, token;
4448
4449 if (sym->attr.dummy || sym->attr.allocatable || !sym->attr.codimension
a90fe829 4450 || sym->attr.use_assoc || !sym->attr.referenced)
a961ca30 4451 return;
4452
4453 decl = sym->backend_decl;
4454 TREE_USED(decl) = 1;
4455 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
4456
4457 /* FIXME: Workaround for PR middle-end/49106, cf. also PR middle-end/49108
4458 to make sure the variable is not optimized away. */
4459 DECL_PRESERVE_P (DECL_CONTEXT (decl)) = 1;
4460
4461 size = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (decl)));
4462
a90fe829 4463 /* Ensure that we do not have size=0 for zero-sized arrays. */
ee4e7a5e 4464 size = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
4465 fold_convert (size_type_node, size),
4466 build_int_cst (size_type_node, 1));
4467
a961ca30 4468 if (GFC_TYPE_ARRAY_RANK (TREE_TYPE (decl)))
4469 {
4470 tmp = GFC_TYPE_ARRAY_SIZE (TREE_TYPE (decl));
4471 size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
ee4e7a5e 4472 fold_convert (size_type_node, tmp), size);
a961ca30 4473 }
4474
4475 gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (decl)) != NULL_TREE);
4476 token = gfc_build_addr_expr (ppvoid_type_node,
4477 GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE(decl)));
4478
4479 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_register, 6, size,
7b186db6 4480 build_int_cst (integer_type_node,
4f5fe475 4481 GFC_CAF_COARRAY_STATIC), /* type. */
a961ca30 4482 token, null_pointer_node, /* token, stat. */
4483 null_pointer_node, /* errgmsg, errmsg_len. */
4484 build_int_cst (integer_type_node, 0));
a90fe829 4485
a961ca30 4486 gfc_add_modify (&caf_init_block, decl, fold_convert (TREE_TYPE (decl), tmp));
4487
4488
4489 /* Handle "static" initializer. */
4490 if (sym->value)
4491 {
4492 sym->attr.pointer = 1;
4493 tmp = gfc_trans_assignment (gfc_lval_expr_from_sym (sym), sym->value,
4494 true, false);
4495 sym->attr.pointer = 0;
4496 gfc_add_expr_to_block (&caf_init_block, tmp);
4497 }
4498}
4499
4500
4501/* Generate constructor function to initialize static, nonallocatable
4502 coarrays. */
4503
4504static void
4505generate_coarray_init (gfc_namespace * ns __attribute((unused)))
4506{
4507 tree fndecl, tmp, decl, save_fn_decl;
4508
4509 save_fn_decl = current_function_decl;
4510 push_function_context ();
4511
4512 tmp = build_function_type_list (void_type_node, NULL_TREE);
4513 fndecl = build_decl (input_location, FUNCTION_DECL,
4514 create_tmp_var_name ("_caf_init"), tmp);
4515
4516 DECL_STATIC_CONSTRUCTOR (fndecl) = 1;
4517 SET_DECL_INIT_PRIORITY (fndecl, DEFAULT_INIT_PRIORITY);
4518
4519 decl = build_decl (input_location, RESULT_DECL, NULL_TREE, void_type_node);
4520 DECL_ARTIFICIAL (decl) = 1;
4521 DECL_IGNORED_P (decl) = 1;
4522 DECL_CONTEXT (decl) = fndecl;
4523 DECL_RESULT (fndecl) = decl;
4524
4525 pushdecl (fndecl);
4526 current_function_decl = fndecl;
4527 announce_function (fndecl);
4528
4529 rest_of_decl_compilation (fndecl, 0, 0);
4530 make_decl_rtl (fndecl);
00cf115c 4531 allocate_struct_function (fndecl, false);
a961ca30 4532
cde2be84 4533 pushlevel ();
a961ca30 4534 gfc_init_block (&caf_init_block);
4535
4536 gfc_traverse_ns (ns, generate_coarray_sym_init);
4537
4538 DECL_SAVED_TREE (fndecl) = gfc_finish_block (&caf_init_block);
4539 decl = getdecls ();
4540
cde2be84 4541 poplevel (1, 1);
a961ca30 4542 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
4543
4544 DECL_SAVED_TREE (fndecl)
4545 = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (fndecl),
4546 DECL_INITIAL (fndecl));
4547 dump_function (TDI_original, fndecl);
4548
4549 cfun->function_end_locus = input_location;
4550 set_cfun (NULL);
4551
4552 if (decl_function_context (fndecl))
4553 (void) cgraph_create_node (fndecl);
4554 else
4555 cgraph_finalize_function (fndecl, true);
4556
4557 pop_function_context ();
4558 current_function_decl = save_fn_decl;
4559}
4560
4561
51d9479b 4562/* Generate all the required code for module variables. */
4563
4564void
4565gfc_generate_module_vars (gfc_namespace * ns)
4566{
4567 module_namespace = ns;
4568 cur_module = gfc_find_module (ns->proc_name->name);
4569
4570 /* Check if the frontend left the namespace in a reasonable state. */
4571 gcc_assert (ns->proc_name && !ns->proc_name->tlink);
4572
4573 /* Generate COMMON blocks. */
4574 gfc_trans_common (ns);
4575
a961ca30 4576 has_coarray_vars = false;
4577
51d9479b 4578 /* Create decls for all the module variables. */
4579 gfc_traverse_ns (ns, gfc_create_module_variable);
4580
a961ca30 4581 if (gfc_option.coarray == GFC_FCOARRAY_LIB && has_coarray_vars)
4582 generate_coarray_init (ns);
4583
51d9479b 4584 cur_module = NULL;
4585
4586 gfc_trans_use_stmts (ns);
2eb674c9 4587 gfc_traverse_ns (ns, gfc_emit_parameter_debug_info);
51d9479b 4588}
4589
4590
4ee9c684 4591static void
4592gfc_generate_contained_functions (gfc_namespace * parent)
4593{
4594 gfc_namespace *ns;
4595
4596 /* We create all the prototypes before generating any code. */
4597 for (ns = parent->contained; ns; ns = ns->sibling)
4598 {
4599 /* Skip namespaces from used modules. */
4600 if (ns->parent != parent)
4601 continue;
4602
d896f9b3 4603 gfc_create_function_decl (ns, false);
4ee9c684 4604 }
4605
4606 for (ns = parent->contained; ns; ns = ns->sibling)
4607 {
4608 /* Skip namespaces from used modules. */
4609 if (ns->parent != parent)
4610 continue;
4611
4612 gfc_generate_function_code (ns);
4613 }
4614}
4615
4616
d95efb59 4617/* Drill down through expressions for the array specification bounds and
4618 character length calling generate_local_decl for all those variables
4619 that have not already been declared. */
4620
4621static void
4622generate_local_decl (gfc_symbol *);
4623
1acb400a 4624/* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
d95efb59 4625
1acb400a 4626static bool
4627expr_decls (gfc_expr *e, gfc_symbol *sym,
4628 int *f ATTRIBUTE_UNUSED)
4629{
4630 if (e->expr_type != EXPR_VARIABLE
4631 || sym == e->symtree->n.sym
d95efb59 4632 || e->symtree->n.sym->mark
4633 || e->symtree->n.sym->ns != sym->ns)
1acb400a 4634 return false;
d95efb59 4635
1acb400a 4636 generate_local_decl (e->symtree->n.sym);
4637 return false;
4638}
d95efb59 4639
1acb400a 4640static void
4641generate_expr_decls (gfc_symbol *sym, gfc_expr *e)
4642{
4643 gfc_traverse_expr (e, sym, expr_decls, 0);
d95efb59 4644}
4645
4646
f6d0e37a 4647/* Check for dependencies in the character length and array spec. */
d95efb59 4648
4649static void
4650generate_dependency_declarations (gfc_symbol *sym)
4651{
4652 int i;
4653
4654 if (sym->ts.type == BT_CHARACTER
eeebe20b 4655 && sym->ts.u.cl
4656 && sym->ts.u.cl->length
4657 && sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
4658 generate_expr_decls (sym, sym->ts.u.cl->length);
d95efb59 4659
4660 if (sym->as && sym->as->rank)
4661 {
4662 for (i = 0; i < sym->as->rank; i++)
4663 {
4664 generate_expr_decls (sym, sym->as->lower[i]);
4665 generate_expr_decls (sym, sym->as->upper[i]);
4666 }
4667 }
4668}
4669
4670
4ee9c684 4671/* Generate decls for all local variables. We do this to ensure correct
4672 handling of expressions which only appear in the specification of
4673 other functions. */
4674
4675static void
4676generate_local_decl (gfc_symbol * sym)
4677{
4678 if (sym->attr.flavor == FL_VARIABLE)
4679 {
a961ca30 4680 if (sym->attr.codimension && !sym->attr.dummy && !sym->attr.allocatable
4681 && sym->attr.referenced && !sym->attr.use_assoc)
4682 has_coarray_vars = true;
4683
d95efb59 4684 if (!sym->attr.dummy && !sym->ns->proc_name->attr.entry_master)
8714fc76 4685 generate_dependency_declarations (sym);
d95efb59 4686
4ee9c684 4687 if (sym->attr.referenced)
8714fc76 4688 gfc_get_symbol_decl (sym);
4acad347 4689
4690 /* Warnings for unused dummy arguments. */
4691 else if (sym->attr.dummy)
7c0ca46e 4692 {
4acad347 4693 /* INTENT(out) dummy arguments are likely meant to be set. */
4694 if (gfc_option.warn_unused_dummy_argument
4695 && sym->attr.intent == INTENT_OUT)
4696 {
4697 if (sym->ts.type != BT_DERIVED)
4698 gfc_warning ("Dummy argument '%s' at %L was declared "
4699 "INTENT(OUT) but was not set", sym->name,
4700 &sym->declared_at);
4701 else if (!gfc_has_default_initializer (sym->ts.u.derived))
4702 gfc_warning ("Derived-type dummy argument '%s' at %L was "
4703 "declared INTENT(OUT) but was not set and "
4704 "does not have a default initializer",
4705 sym->name, &sym->declared_at);
90a4a5a6 4706 if (sym->backend_decl != NULL_TREE)
4707 TREE_NO_WARNING(sym->backend_decl) = 1;
4acad347 4708 }
4709 else if (gfc_option.warn_unused_dummy_argument)
90a4a5a6 4710 {
4711 gfc_warning ("Unused dummy argument '%s' at %L", sym->name,
4acad347 4712 &sym->declared_at);
90a4a5a6 4713 if (sym->backend_decl != NULL_TREE)
4714 TREE_NO_WARNING(sym->backend_decl) = 1;
4715 }
7c0ca46e 4716 }
4acad347 4717
f888a3fb 4718 /* Warn for unused variables, but not if they're inside a common
1dbfac29 4719 block or a namelist. */
36609028 4720 else if (warn_unused_variable
1dbfac29 4721 && !(sym->attr.in_common || sym->mark || sym->attr.in_namelist))
90a4a5a6 4722 {
1dbfac29 4723 if (sym->attr.use_only)
4724 {
4725 gfc_warning ("Unused module variable '%s' which has been "
4726 "explicitly imported at %L", sym->name,
4727 &sym->declared_at);
4728 if (sym->backend_decl != NULL_TREE)
4729 TREE_NO_WARNING(sym->backend_decl) = 1;
4730 }
4731 else if (!sym->attr.use_assoc)
4732 {
4733 gfc_warning ("Unused variable '%s' declared at %L",
4734 sym->name, &sym->declared_at);
4735 if (sym->backend_decl != NULL_TREE)
4736 TREE_NO_WARNING(sym->backend_decl) = 1;
4737 }
90a4a5a6 4738 }
8714fc76 4739
d4163395 4740 /* For variable length CHARACTER parameters, the PARM_DECL already
4741 references the length variable, so force gfc_get_symbol_decl
4742 even when not referenced. If optimize > 0, it will be optimized
4743 away anyway. But do this only after emitting -Wunused-parameter
4744 warning if requested. */
8714fc76 4745 if (sym->attr.dummy && !sym->attr.referenced
4746 && sym->ts.type == BT_CHARACTER
eeebe20b 4747 && sym->ts.u.cl->backend_decl != NULL
4748 && TREE_CODE (sym->ts.u.cl->backend_decl) == VAR_DECL)
d4163395 4749 {
4750 sym->attr.referenced = 1;
4751 gfc_get_symbol_decl (sym);
4752 }
76776e6d 4753
d0163401 4754 /* INTENT(out) dummy arguments and result variables with allocatable
4755 components are reset by default and need to be set referenced to
4756 generate the code for nullification and automatic lengths. */
4757 if (!sym->attr.referenced
8714fc76 4758 && sym->ts.type == BT_DERIVED
eeebe20b 4759 && sym->ts.u.derived->attr.alloc_comp
c49db15e 4760 && !sym->attr.pointer
d0163401 4761 && ((sym->attr.dummy && sym->attr.intent == INTENT_OUT)
4762 ||
4763 (sym->attr.result && sym != sym->result)))
8714fc76 4764 {
4765 sym->attr.referenced = 1;
4766 gfc_get_symbol_decl (sym);
4767 }
4768
e72f979a 4769 /* Check for dependencies in the array specification and string
4770 length, adding the necessary declarations to the function. We
4771 mark the symbol now, as well as in traverse_ns, to prevent
4772 getting stuck in a circular dependency. */
4773 sym->mark = 1;
4ee9c684 4774 }
5dd246c1 4775 else if (sym->attr.flavor == FL_PARAMETER)
4776 {
6ecfe89d 4777 if (warn_unused_parameter
f326eb81 4778 && !sym->attr.referenced)
4779 {
4780 if (!sym->attr.use_assoc)
4781 gfc_warning ("Unused parameter '%s' declared at %L", sym->name,
4782 &sym->declared_at);
4783 else if (sym->attr.use_only)
4784 gfc_warning ("Unused parameter '%s' which has been explicitly "
4785 "imported at %L", sym->name, &sym->declared_at);
4786 }
5dd246c1 4787 }
fa7b6574 4788 else if (sym->attr.flavor == FL_PROCEDURE)
4789 {
4790 /* TODO: move to the appropriate place in resolve.c. */
4791 if (warn_return_type
4792 && sym->attr.function
4793 && sym->result
4794 && sym != sym->result
4795 && !sym->result->attr.referenced
4796 && !sym->attr.use_assoc
4797 && sym->attr.if_source != IFSRC_IFBODY)
4798 {
4799 gfc_warning ("Return value '%s' of function '%s' declared at "
4800 "%L not set", sym->result->name, sym->name,
4801 &sym->result->declared_at);
4802
4803 /* Prevents "Unused variable" warning for RESULT variables. */
e72f979a 4804 sym->result->mark = 1;
fa7b6574 4805 }
4806 }
c5d33754 4807
19ba2ad8 4808 if (sym->attr.dummy == 1)
4809 {
4810 /* Modify the tree type for scalar character dummy arguments of bind(c)
4811 procedures if they are passed by value. The tree type for them will
4812 be promoted to INTEGER_TYPE for the middle end, which appears to be
4813 what C would do with characters passed by-value. The value attribute
4814 implies the dummy is a scalar. */
4815 if (sym->attr.value == 1 && sym->backend_decl != NULL
4816 && sym->ts.type == BT_CHARACTER && sym->ts.is_c_interop
4817 && sym->ns->proc_name != NULL && sym->ns->proc_name->attr.is_bind_c)
4c47c8b7 4818 gfc_conv_scalar_char_value (sym, NULL, NULL);
fc6338c7 4819
4820 /* Unused procedure passed as dummy argument. */
4821 if (sym->attr.flavor == FL_PROCEDURE)
4822 {
4823 if (!sym->attr.referenced)
4824 {
4825 if (gfc_option.warn_unused_dummy_argument)
4826 gfc_warning ("Unused dummy argument '%s' at %L", sym->name,
a90fe829 4827 &sym->declared_at);
fc6338c7 4828 }
4829
4830 /* Silence bogus "unused parameter" warnings from the
4831 middle end. */
4832 if (sym->backend_decl != NULL_TREE)
4833 TREE_NO_WARNING (sym->backend_decl) = 1;
4834 }
19ba2ad8 4835 }
4836
c5d33754 4837 /* Make sure we convert the types of the derived types from iso_c_binding
4838 into (void *). */
4839 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
4840 && sym->ts.type == BT_DERIVED)
4841 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
4ee9c684 4842}
4843
4844static void
4845generate_local_vars (gfc_namespace * ns)
4846{
4847 gfc_traverse_ns (ns, generate_local_decl);
4848}
4849
4850
1b716045 4851/* Generate a switch statement to jump to the correct entry point. Also
4852 creates the label decls for the entry points. */
4ee9c684 4853
1b716045 4854static tree
4855gfc_trans_entry_master_switch (gfc_entry_list * el)
4ee9c684 4856{
1b716045 4857 stmtblock_t block;
4858 tree label;
4859 tree tmp;
4860 tree val;
4ee9c684 4861
1b716045 4862 gfc_init_block (&block);
4863 for (; el; el = el->next)
4864 {
4865 /* Add the case label. */
b797d6d3 4866 label = gfc_build_label_decl (NULL_TREE);
7016c612 4867 val = build_int_cst (gfc_array_index_type, el->id);
b6e3dd65 4868 tmp = build_case_label (val, NULL_TREE, label);
1b716045 4869 gfc_add_expr_to_block (&block, tmp);
5b11d932 4870
1b716045 4871 /* And jump to the actual entry point. */
4872 label = gfc_build_label_decl (NULL_TREE);
1b716045 4873 tmp = build1_v (GOTO_EXPR, label);
4874 gfc_add_expr_to_block (&block, tmp);
4875
4876 /* Save the label decl. */
4877 el->label = label;
4878 }
4879 tmp = gfc_finish_block (&block);
4880 /* The first argument selects the entry point. */
4881 val = DECL_ARGUMENTS (current_function_decl);
bfb10994 4882 tmp = fold_build3_loc (input_location, SWITCH_EXPR, NULL_TREE,
4883 val, tmp, NULL_TREE);
1b716045 4884 return tmp;
4ee9c684 4885}
4886
6374121b 4887
a4abf8a0 4888/* Add code to string lengths of actual arguments passed to a function against
4889 the expected lengths of the dummy arguments. */
4890
4891static void
4892add_argument_checking (stmtblock_t *block, gfc_symbol *sym)
4893{
4894 gfc_formal_arglist *formal;
4895
6777213b 4896 for (formal = gfc_sym_get_dummy_args (sym); formal; formal = formal->next)
517c89e5 4897 if (formal->sym && formal->sym->ts.type == BT_CHARACTER
6c3000f4 4898 && !formal->sym->ts.deferred)
a4abf8a0 4899 {
4900 enum tree_code comparison;
4901 tree cond;
4902 tree argname;
4903 gfc_symbol *fsym;
4904 gfc_charlen *cl;
4905 const char *message;
4906
4907 fsym = formal->sym;
eeebe20b 4908 cl = fsym->ts.u.cl;
a4abf8a0 4909
4910 gcc_assert (cl);
4911 gcc_assert (cl->passed_length != NULL_TREE);
4912 gcc_assert (cl->backend_decl != NULL_TREE);
4913
4914 /* For POINTER, ALLOCATABLE and assumed-shape dummy arguments, the
4915 string lengths must match exactly. Otherwise, it is only required
be4be771 4916 that the actual string length is *at least* the expected one.
4917 Sequence association allows for a mismatch of the string length
4918 if the actual argument is (part of) an array, but only if the
4919 dummy argument is an array. (See "Sequence association" in
4920 Section 12.4.1.4 for F95 and 12.4.1.5 for F2003.) */
517c89e5 4921 if (fsym->attr.pointer || fsym->attr.allocatable
f00f6dd6 4922 || (fsym->as && (fsym->as->type == AS_ASSUMED_SHAPE
4923 || fsym->as->type == AS_ASSUMED_RANK)))
a4abf8a0 4924 {
4925 comparison = NE_EXPR;
4926 message = _("Actual string length does not match the declared one"
4927 " for dummy argument '%s' (%ld/%ld)");
4928 }
be4be771 4929 else if (fsym->as && fsym->as->rank != 0)
4930 continue;
a4abf8a0 4931 else
4932 {
4933 comparison = LT_EXPR;
4934 message = _("Actual string length is shorter than the declared one"
4935 " for dummy argument '%s' (%ld/%ld)");
4936 }
4937
4938 /* Build the condition. For optional arguments, an actual length
4939 of 0 is also acceptable if the associated string is NULL, which
4940 means the argument was not passed. */
fd779e1d 4941 cond = fold_build2_loc (input_location, comparison, boolean_type_node,
4942 cl->passed_length, cl->backend_decl);
a4abf8a0 4943 if (fsym->attr.optional)
4944 {
4945 tree not_absent;
4946 tree not_0length;
4947 tree absent_failed;
4948
fd779e1d 4949 not_0length = fold_build2_loc (input_location, NE_EXPR,
4950 boolean_type_node,
4951 cl->passed_length,
385f3f36 4952 build_zero_cst (gfc_charlen_type_node));
5fa0fdc2 4953 /* The symbol needs to be referenced for gfc_get_symbol_decl. */
4954 fsym->attr.referenced = 1;
4955 not_absent = gfc_conv_expr_present (fsym);
a4abf8a0 4956
fd779e1d 4957 absent_failed = fold_build2_loc (input_location, TRUTH_OR_EXPR,
4958 boolean_type_node, not_0length,
4959 not_absent);
a4abf8a0 4960
fd779e1d 4961 cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
4962 boolean_type_node, cond, absent_failed);
a4abf8a0 4963 }
4964
4965 /* Build the runtime check. */
4966 argname = gfc_build_cstring_const (fsym->name);
4967 argname = gfc_build_addr_expr (pchar_type_node, argname);
4968 gfc_trans_runtime_check (true, false, cond, block, &fsym->declared_at,
4969 message, argname,
4970 fold_convert (long_integer_type_node,
4971 cl->passed_length),
4972 fold_convert (long_integer_type_node,
4973 cl->backend_decl));
4974 }
4975}
4976
4977
642970a3 4978/* Generate the _gfortran_caf_this_image and _gfortran_caf_num_images
4979 global variables for -fcoarray=lib. They are placed into the translation
4980 unit of the main program. Make sure that in one TU (the one of the main
4981 program), the first call to gfc_init_coarray_decl is done with true.
4982 Otherwise, expect link errors. */
4983
70b5944a 4984void
642970a3 4985gfc_init_coarray_decl (bool main_tu)
70b5944a 4986{
70b5944a 4987 if (gfc_option.coarray != GFC_FCOARRAY_LIB)
4988 return;
4989
4990 if (gfort_gvar_caf_this_image || gfort_gvar_caf_num_images)
4991 return;
4992
70b5944a 4993 push_cfun (cfun);
4994
642970a3 4995 gfort_gvar_caf_this_image
4996 = build_decl (input_location, VAR_DECL,
4997 get_identifier (PREFIX("caf_this_image")),
4998 integer_type_node);
70b5944a 4999 DECL_ARTIFICIAL (gfort_gvar_caf_this_image) = 1;
5000 TREE_USED (gfort_gvar_caf_this_image) = 1;
5001 TREE_PUBLIC (gfort_gvar_caf_this_image) = 1;
642970a3 5002 TREE_READONLY (gfort_gvar_caf_this_image) = 0;
5003
5004 if (main_tu)
5005 TREE_STATIC (gfort_gvar_caf_this_image) = 1;
5006 else
5007 DECL_EXTERNAL (gfort_gvar_caf_this_image) = 1;
5008
5009 pushdecl_top_level (gfort_gvar_caf_this_image);
70b5944a 5010
642970a3 5011 gfort_gvar_caf_num_images
5012 = build_decl (input_location, VAR_DECL,
5013 get_identifier (PREFIX("caf_num_images")),
5014 integer_type_node);
70b5944a 5015 DECL_ARTIFICIAL (gfort_gvar_caf_num_images) = 1;
5016 TREE_USED (gfort_gvar_caf_num_images) = 1;
5017 TREE_PUBLIC (gfort_gvar_caf_num_images) = 1;
642970a3 5018 TREE_READONLY (gfort_gvar_caf_num_images) = 0;
5019
5020 if (main_tu)
5021 TREE_STATIC (gfort_gvar_caf_num_images) = 1;
5022 else
5023 DECL_EXTERNAL (gfort_gvar_caf_num_images) = 1;
5024
5025 pushdecl_top_level (gfort_gvar_caf_num_images);
70b5944a 5026
5027 pop_cfun ();
70b5944a 5028}
5029
5030
7257a5d2 5031static void
5032create_main_function (tree fndecl)
5033{
43702da6 5034 tree old_context;
7257a5d2 5035 tree ftn_main;
5036 tree tmp, decl, result_decl, argc, argv, typelist, arglist;
5037 stmtblock_t body;
5038
43702da6 5039 old_context = current_function_decl;
5040
5041 if (old_context)
5042 {
5043 push_function_context ();
5044 saved_parent_function_decls = saved_function_decls;
5045 saved_function_decls = NULL_TREE;
5046 }
5047
7257a5d2 5048 /* main() function must be declared with global scope. */
5049 gcc_assert (current_function_decl == NULL_TREE);
5050
5051 /* Declare the function. */
5052 tmp = build_function_type_list (integer_type_node, integer_type_node,
5053 build_pointer_type (pchar_type_node),
5054 NULL_TREE);
0509d0ee 5055 main_identifier_node = get_identifier ("main");
e60a6f7b 5056 ftn_main = build_decl (input_location, FUNCTION_DECL,
5057 main_identifier_node, tmp);
7257a5d2 5058 DECL_EXTERNAL (ftn_main) = 0;
5059 TREE_PUBLIC (ftn_main) = 1;
5060 TREE_STATIC (ftn_main) = 1;
5061 DECL_ATTRIBUTES (ftn_main)
5062 = tree_cons (get_identifier("externally_visible"), NULL_TREE, NULL_TREE);
5063
5064 /* Setup the result declaration (for "return 0"). */
e60a6f7b 5065 result_decl = build_decl (input_location,
5066 RESULT_DECL, NULL_TREE, integer_type_node);
7257a5d2 5067 DECL_ARTIFICIAL (result_decl) = 1;
5068 DECL_IGNORED_P (result_decl) = 1;
5069 DECL_CONTEXT (result_decl) = ftn_main;
5070 DECL_RESULT (ftn_main) = result_decl;
5071
5072 pushdecl (ftn_main);
5073
5074 /* Get the arguments. */
5075
5076 arglist = NULL_TREE;
5077 typelist = TYPE_ARG_TYPES (TREE_TYPE (ftn_main));
5078
5079 tmp = TREE_VALUE (typelist);
e60a6f7b 5080 argc = build_decl (input_location, PARM_DECL, get_identifier ("argc"), tmp);
7257a5d2 5081 DECL_CONTEXT (argc) = ftn_main;
5082 DECL_ARG_TYPE (argc) = TREE_VALUE (typelist);
5083 TREE_READONLY (argc) = 1;
5084 gfc_finish_decl (argc);
5085 arglist = chainon (arglist, argc);
5086
5087 typelist = TREE_CHAIN (typelist);
5088 tmp = TREE_VALUE (typelist);
e60a6f7b 5089 argv = build_decl (input_location, PARM_DECL, get_identifier ("argv"), tmp);
7257a5d2 5090 DECL_CONTEXT (argv) = ftn_main;
5091 DECL_ARG_TYPE (argv) = TREE_VALUE (typelist);
5092 TREE_READONLY (argv) = 1;
5093 DECL_BY_REFERENCE (argv) = 1;
5094 gfc_finish_decl (argv);
5095 arglist = chainon (arglist, argv);
5096
5097 DECL_ARGUMENTS (ftn_main) = arglist;
5098 current_function_decl = ftn_main;
5099 announce_function (ftn_main);
5100
5101 rest_of_decl_compilation (ftn_main, 1, 0);
5102 make_decl_rtl (ftn_main);
00cf115c 5103 allocate_struct_function (ftn_main, false);
cde2be84 5104 pushlevel ();
7257a5d2 5105
5106 gfc_init_block (&body);
5107
5108 /* Call some libgfortran initialization routines, call then MAIN__(). */
5109
70b5944a 5110 /* Call _gfortran_caf_init (*argc, ***argv, *this_image, *num_images). */
5111 if (gfc_option.coarray == GFC_FCOARRAY_LIB)
5112 {
5113 tree pint_type, pppchar_type;
5114 pint_type = build_pointer_type (integer_type_node);
5115 pppchar_type
5116 = build_pointer_type (build_pointer_type (pchar_type_node));
5117
642970a3 5118 gfc_init_coarray_decl (true);
70b5944a 5119 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_init, 4,
5120 gfc_build_addr_expr (pint_type, argc),
5121 gfc_build_addr_expr (pppchar_type, argv),
5122 gfc_build_addr_expr (pint_type, gfort_gvar_caf_this_image),
5123 gfc_build_addr_expr (pint_type, gfort_gvar_caf_num_images));
5124 gfc_add_expr_to_block (&body, tmp);
5125 }
5126
7257a5d2 5127 /* Call _gfortran_set_args (argc, argv). */
43702da6 5128 TREE_USED (argc) = 1;
5129 TREE_USED (argv) = 1;
389dd41b 5130 tmp = build_call_expr_loc (input_location,
5131 gfor_fndecl_set_args, 2, argc, argv);
7257a5d2 5132 gfc_add_expr_to_block (&body, tmp);
5133
5134 /* Add a call to set_options to set up the runtime library Fortran
5135 language standard parameters. */
5136 {
5137 tree array_type, array, var;
f1f41a6c 5138 vec<constructor_elt, va_gc> *v = NULL;
7257a5d2 5139
5140 /* Passing a new option to the library requires four modifications:
5141 + add it to the tree_cons list below
5142 + change the array size in the call to build_array_type
5143 + change the first argument to the library call
5144 gfor_fndecl_set_options
5145 + modify the library (runtime/compile_options.c)! */
5146
06f13dc1 5147 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
5148 build_int_cst (integer_type_node,
5149 gfc_option.warn_std));
5150 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
5151 build_int_cst (integer_type_node,
5152 gfc_option.allow_std));
5153 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
5154 build_int_cst (integer_type_node, pedantic));
b2130263 5155 /* TODO: This is the old -fdump-core option, which is unused but
5156 passed due to ABI compatibility; remove when bumping the
5157 library ABI. */
06f13dc1 5158 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
5159 build_int_cst (integer_type_node,
b2130263 5160 0));
06f13dc1 5161 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
5162 build_int_cst (integer_type_node,
5163 gfc_option.flag_backtrace));
5164 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
5165 build_int_cst (integer_type_node,
5166 gfc_option.flag_sign_zero));
5167 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
5168 build_int_cst (integer_type_node,
5169 (gfc_option.rtcheck
5170 & GFC_RTCHECK_BOUNDS)));
7f4f15dc 5171 /* TODO: This is the -frange-check option, which no longer affects
5172 library behavior; when bumping the library ABI this slot can be
5173 reused for something else. As it is the last element in the
5174 array, we can instead leave it out altogether.
06f13dc1 5175 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
5176 build_int_cst (integer_type_node,
5177 gfc_option.flag_range_check));
7f4f15dc 5178 */
7257a5d2 5179
5180 array_type = build_array_type (integer_type_node,
7f4f15dc 5181 build_index_type (size_int (6)));
06f13dc1 5182 array = build_constructor (array_type, v);
7257a5d2 5183 TREE_CONSTANT (array) = 1;
5184 TREE_STATIC (array) = 1;
5185
5186 /* Create a static variable to hold the jump table. */
5187 var = gfc_create_var (array_type, "options");
5188 TREE_CONSTANT (var) = 1;
5189 TREE_STATIC (var) = 1;
5190 TREE_READONLY (var) = 1;
5191 DECL_INITIAL (var) = array;
5192 var = gfc_build_addr_expr (build_pointer_type (integer_type_node), var);
5193
389dd41b 5194 tmp = build_call_expr_loc (input_location,
5195 gfor_fndecl_set_options, 2,
7f4f15dc 5196 build_int_cst (integer_type_node, 7), var);
7257a5d2 5197 gfc_add_expr_to_block (&body, tmp);
5198 }
5199
5200 /* If -ffpe-trap option was provided, add a call to set_fpe so that
5201 the library will raise a FPE when needed. */
5202 if (gfc_option.fpe != 0)
5203 {
389dd41b 5204 tmp = build_call_expr_loc (input_location,
5205 gfor_fndecl_set_fpe, 1,
7257a5d2 5206 build_int_cst (integer_type_node,
5207 gfc_option.fpe));
5208 gfc_add_expr_to_block (&body, tmp);
5209 }
5210
5211 /* If this is the main program and an -fconvert option was provided,
5212 add a call to set_convert. */
5213
5214 if (gfc_option.convert != GFC_CONVERT_NATIVE)
5215 {
389dd41b 5216 tmp = build_call_expr_loc (input_location,
5217 gfor_fndecl_set_convert, 1,
7257a5d2 5218 build_int_cst (integer_type_node,
5219 gfc_option.convert));
5220 gfc_add_expr_to_block (&body, tmp);
5221 }
5222
5223 /* If this is the main program and an -frecord-marker option was provided,
5224 add a call to set_record_marker. */
5225
5226 if (gfc_option.record_marker != 0)
5227 {
389dd41b 5228 tmp = build_call_expr_loc (input_location,
5229 gfor_fndecl_set_record_marker, 1,
7257a5d2 5230 build_int_cst (integer_type_node,
5231 gfc_option.record_marker));
5232 gfc_add_expr_to_block (&body, tmp);
5233 }
5234
5235 if (gfc_option.max_subrecord_length != 0)
5236 {
389dd41b 5237 tmp = build_call_expr_loc (input_location,
5238 gfor_fndecl_set_max_subrecord_length, 1,
7257a5d2 5239 build_int_cst (integer_type_node,
5240 gfc_option.max_subrecord_length));
5241 gfc_add_expr_to_block (&body, tmp);
5242 }
5243
5244 /* Call MAIN__(). */
389dd41b 5245 tmp = build_call_expr_loc (input_location,
5246 fndecl, 0);
7257a5d2 5247 gfc_add_expr_to_block (&body, tmp);
5248
43702da6 5249 /* Mark MAIN__ as used. */
5250 TREE_USED (fndecl) = 1;
5251
70b5944a 5252 /* Coarray: Call _gfortran_caf_finalize(void). */
5253 if (gfc_option.coarray == GFC_FCOARRAY_LIB)
a90fe829 5254 {
70b5944a 5255 /* Per F2008, 8.5.1 END of the main program implies a
a90fe829 5256 SYNC MEMORY. */
b9a16870 5257 tmp = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
70b5944a 5258 tmp = build_call_expr_loc (input_location, tmp, 0);
5259 gfc_add_expr_to_block (&body, tmp);
5260
5261 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_finalize, 0);
5262 gfc_add_expr_to_block (&body, tmp);
5263 }
5264
7257a5d2 5265 /* "return 0". */
fd779e1d 5266 tmp = fold_build2_loc (input_location, MODIFY_EXPR, integer_type_node,
5267 DECL_RESULT (ftn_main),
5268 build_int_cst (integer_type_node, 0));
7257a5d2 5269 tmp = build1_v (RETURN_EXPR, tmp);
5270 gfc_add_expr_to_block (&body, tmp);
5271
5272
5273 DECL_SAVED_TREE (ftn_main) = gfc_finish_block (&body);
5274 decl = getdecls ();
5275
5276 /* Finish off this function and send it for code generation. */
cde2be84 5277 poplevel (1, 1);
7257a5d2 5278 BLOCK_SUPERCONTEXT (DECL_INITIAL (ftn_main)) = ftn_main;
5279
5280 DECL_SAVED_TREE (ftn_main)
5281 = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (ftn_main),
5282 DECL_INITIAL (ftn_main));
5283
5284 /* Output the GENERIC tree. */
5285 dump_function (TDI_original, ftn_main);
5286
bb982f66 5287 cgraph_finalize_function (ftn_main, true);
43702da6 5288
5289 if (old_context)
5290 {
5291 pop_function_context ();
5292 saved_function_decls = saved_parent_function_decls;
5293 }
5294 current_function_decl = old_context;
7257a5d2 5295}
5296
5297
89ac8ba1 5298/* Get the result expression for a procedure. */
5299
5300static tree
5301get_proc_result (gfc_symbol* sym)
5302{
5303 if (sym->attr.subroutine || sym == sym->result)
5304 {
5305 if (current_fake_result_decl != NULL)
5306 return TREE_VALUE (current_fake_result_decl);
5307
5308 return NULL_TREE;
5309 }
5310
5311 return sym->result->backend_decl;
5312}
5313
5314
5315/* Generate an appropriate return-statement for a procedure. */
5316
5317tree
5318gfc_generate_return (void)
5319{
5320 gfc_symbol* sym;
5321 tree result;
5322 tree fndecl;
5323
5324 sym = current_procedure_symbol;
5325 fndecl = sym->backend_decl;
5326
5327 if (TREE_TYPE (DECL_RESULT (fndecl)) == void_type_node)
5328 result = NULL_TREE;
5329 else
5330 {
5331 result = get_proc_result (sym);
5332
5333 /* Set the return value to the dummy result variable. The
5334 types may be different for scalar default REAL functions
5335 with -ff2c, therefore we have to convert. */
5336 if (result != NULL_TREE)
5337 {
5338 result = convert (TREE_TYPE (DECL_RESULT (fndecl)), result);
fd779e1d 5339 result = fold_build2_loc (input_location, MODIFY_EXPR,
5340 TREE_TYPE (result), DECL_RESULT (fndecl),
5341 result);
89ac8ba1 5342 }
5343 }
5344
5345 return build1_v (RETURN_EXPR, result);
5346}
5347
5348
4ee9c684 5349/* Generate code for a function. */
5350
5351void
5352gfc_generate_function_code (gfc_namespace * ns)
5353{
5354 tree fndecl;
5355 tree old_context;
5356 tree decl;
5357 tree tmp;
89ac8ba1 5358 stmtblock_t init, cleanup;
4ee9c684 5359 stmtblock_t body;
89ac8ba1 5360 gfc_wrapped_block try_block;
5fa0fdc2 5361 tree recurcheckvar = NULL_TREE;
4ee9c684 5362 gfc_symbol *sym;
89ac8ba1 5363 gfc_symbol *previous_procedure_symbol;
2294b616 5364 int rank;
e50e62f5 5365 bool is_recursive;
4ee9c684 5366
5367 sym = ns->proc_name;
89ac8ba1 5368 previous_procedure_symbol = current_procedure_symbol;
5369 current_procedure_symbol = sym;
1b716045 5370
4ee9c684 5371 /* Check that the frontend isn't still using this. */
22d678e8 5372 gcc_assert (sym->tlink == NULL);
4ee9c684 5373 sym->tlink = sym;
5374
5375 /* Create the declaration for functions with global scope. */
5376 if (!sym->backend_decl)
d896f9b3 5377 gfc_create_function_decl (ns, false);
4ee9c684 5378
5379 fndecl = sym->backend_decl;
5380 old_context = current_function_decl;
5381
5382 if (old_context)
5383 {
5384 push_function_context ();
5385 saved_parent_function_decls = saved_function_decls;
5386 saved_function_decls = NULL_TREE;
5387 }
5388
1b716045 5389 trans_function_start (sym);
4ee9c684 5390
89ac8ba1 5391 gfc_init_block (&init);
4ee9c684 5392
c6871095 5393 if (ns->entries && ns->proc_name->ts.type == BT_CHARACTER)
5394 {
5395 /* Copy length backend_decls to all entry point result
5396 symbols. */
5397 gfc_entry_list *el;
5398 tree backend_decl;
5399
eeebe20b 5400 gfc_conv_const_charlen (ns->proc_name->ts.u.cl);
5401 backend_decl = ns->proc_name->result->ts.u.cl->backend_decl;
c6871095 5402 for (el = ns->entries; el; el = el->next)
eeebe20b 5403 el->sym->result->ts.u.cl->backend_decl = backend_decl;
c6871095 5404 }
5405
4ee9c684 5406 /* Translate COMMON blocks. */
5407 gfc_trans_common (ns);
5408
c750cc52 5409 /* Null the parent fake result declaration if this namespace is
5410 a module function or an external procedures. */
5411 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
5412 || ns->parent == NULL)
5413 parent_fake_result_decl = NULL_TREE;
5414
2b685f8e 5415 gfc_generate_contained_functions (ns);
5416
9579733e 5417 nonlocal_dummy_decls = NULL;
5418 nonlocal_dummy_decl_pset = NULL;
5419
a961ca30 5420 has_coarray_vars = false;
4ee9c684 5421 generate_local_vars (ns);
5b11d932 5422
a961ca30 5423 if (gfc_option.coarray == GFC_FCOARRAY_LIB && has_coarray_vars)
5424 generate_coarray_init (ns);
5425
c750cc52 5426 /* Keep the parent fake result declaration in module functions
5427 or external procedures. */
5428 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
5429 || ns->parent == NULL)
5430 current_fake_result_decl = parent_fake_result_decl;
5431 else
5432 current_fake_result_decl = NULL_TREE;
5433
89ac8ba1 5434 is_recursive = sym->attr.recursive
5435 || (sym->attr.entry_master
5436 && sym->ns->entries->sym->attr.recursive);
5437 if ((gfc_option.rtcheck & GFC_RTCHECK_RECURSION)
5438 && !is_recursive
5439 && !gfc_option.flag_recursive)
5440 {
5441 char * msg;
5442
5443 asprintf (&msg, "Recursive call to nonrecursive procedure '%s'",
5444 sym->name);
5445 recurcheckvar = gfc_create_var (boolean_type_node, "is_recursive");
5446 TREE_STATIC (recurcheckvar) = 1;
5447 DECL_INITIAL (recurcheckvar) = boolean_false_node;
5448 gfc_add_expr_to_block (&init, recurcheckvar);
5449 gfc_trans_runtime_check (true, false, recurcheckvar, &init,
5450 &sym->declared_at, msg);
5451 gfc_add_modify (&init, recurcheckvar, boolean_true_node);
434f0922 5452 free (msg);
89ac8ba1 5453 }
4ee9c684 5454
5455 /* Now generate the code for the body of this function. */
5456 gfc_init_block (&body);
5457
5458 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
89ac8ba1 5459 && sym->attr.subroutine)
4ee9c684 5460 {
5461 tree alternate_return;
c750cc52 5462 alternate_return = gfc_get_fake_result_decl (sym, 0);
75a70cf9 5463 gfc_add_modify (&body, alternate_return, integer_zero_node);
4ee9c684 5464 }
5465
1b716045 5466 if (ns->entries)
5467 {
5468 /* Jump to the correct entry point. */
5469 tmp = gfc_trans_entry_master_switch (ns->entries);
5470 gfc_add_expr_to_block (&body, tmp);
5471 }
5472
a4abf8a0 5473 /* If bounds-checking is enabled, generate code to check passed in actual
5474 arguments against the expected dummy argument attributes (e.g. string
5475 lengths). */
c1630d65 5476 if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !sym->attr.is_bind_c)
a4abf8a0 5477 add_argument_checking (&body, sym);
5478
4ee9c684 5479 tmp = gfc_trans_code (ns->code);
5480 gfc_add_expr_to_block (&body, tmp);
5481
4ee9c684 5482 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node)
5483 {
89ac8ba1 5484 tree result = get_proc_result (sym);
4ee9c684 5485
42766cb3 5486 if (result != NULL_TREE && sym->attr.function && !sym->attr.pointer)
2294b616 5487 {
deb7edfc 5488 if (sym->attr.allocatable && sym->attr.dimension == 0
5489 && sym->result == sym)
5490 gfc_add_modify (&init, result, fold_convert (TREE_TYPE (result),
5491 null_pointer_node));
42766cb3 5492 else if (sym->ts.type == BT_CLASS
5493 && CLASS_DATA (sym)->attr.allocatable
3a19c063 5494 && CLASS_DATA (sym)->attr.dimension == 0
5495 && sym->result == sym)
42766cb3 5496 {
5497 tmp = CLASS_DATA (sym)->backend_decl;
5498 tmp = fold_build3_loc (input_location, COMPONENT_REF,
5499 TREE_TYPE (tmp), result, tmp, NULL_TREE);
5500 gfc_add_modify (&init, tmp, fold_convert (TREE_TYPE (tmp),
5501 null_pointer_node));
5502 }
deb7edfc 5503 else if (sym->ts.type == BT_DERIVED
42766cb3 5504 && sym->ts.u.derived->attr.alloc_comp
5505 && !sym->attr.allocatable)
53169279 5506 {
5507 rank = sym->as ? sym->as->rank : 0;
89ac8ba1 5508 tmp = gfc_nullify_alloc_comp (sym->ts.u.derived, result, rank);
5509 gfc_add_expr_to_block (&init, tmp);
53169279 5510 }
5176859a 5511 }
e50e62f5 5512
fa7b6574 5513 if (result == NULL_TREE)
5514 {
5515 /* TODO: move to the appropriate place in resolve.c. */
90a4a5a6 5516 if (warn_return_type && sym == sym->result)
fa7b6574 5517 gfc_warning ("Return value of function '%s' at %L not set",
5518 sym->name, &sym->declared_at);
90a4a5a6 5519 if (warn_return_type)
5520 TREE_NO_WARNING(sym->backend_decl) = 1;
fa7b6574 5521 }
4ee9c684 5522 else
89ac8ba1 5523 gfc_add_expr_to_block (&body, gfc_generate_return ());
4ee9c684 5524 }
89ac8ba1 5525
5526 gfc_init_block (&cleanup);
5527
5528 /* Reset recursion-check variable. */
5529 if ((gfc_option.rtcheck & GFC_RTCHECK_RECURSION)
5530 && !is_recursive
5ae82d58 5531 && !gfc_option.gfc_flag_openmp
89ac8ba1 5532 && recurcheckvar != NULL_TREE)
e50e62f5 5533 {
89ac8ba1 5534 gfc_add_modify (&cleanup, recurcheckvar, boolean_false_node);
5535 recurcheckvar = NULL;
e50e62f5 5536 }
2294b616 5537
89ac8ba1 5538 /* Finish the function body and add init and cleanup code. */
5539 tmp = gfc_finish_block (&body);
5540 gfc_start_wrapped_block (&try_block, tmp);
5541 /* Add code to create and cleanup arrays. */
5542 gfc_trans_deferred_vars (sym, &try_block);
5543 gfc_add_init_cleanup (&try_block, gfc_finish_block (&init),
5544 gfc_finish_block (&cleanup));
4ee9c684 5545
5546 /* Add all the decls we created during processing. */
5547 decl = saved_function_decls;
5548 while (decl)
5549 {
5550 tree next;
5551
1767a056 5552 next = DECL_CHAIN (decl);
5553 DECL_CHAIN (decl) = NULL_TREE;
4c197fd0 5554 pushdecl (decl);
4ee9c684 5555 decl = next;
5556 }
5557 saved_function_decls = NULL_TREE;
5558
89ac8ba1 5559 DECL_SAVED_TREE (fndecl) = gfc_finish_wrapped_block (&try_block);
e5004242 5560 decl = getdecls ();
4ee9c684 5561
5562 /* Finish off this function and send it for code generation. */
cde2be84 5563 poplevel (1, 1);
4ee9c684 5564 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
5565
e5004242 5566 DECL_SAVED_TREE (fndecl)
5567 = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (fndecl),
5568 DECL_INITIAL (fndecl));
5569
9579733e 5570 if (nonlocal_dummy_decls)
5571 {
5572 BLOCK_VARS (DECL_INITIAL (fndecl))
5573 = chainon (BLOCK_VARS (DECL_INITIAL (fndecl)), nonlocal_dummy_decls);
5574 pointer_set_destroy (nonlocal_dummy_decl_pset);
5575 nonlocal_dummy_decls = NULL;
5576 nonlocal_dummy_decl_pset = NULL;
5577 }
5578
4ee9c684 5579 /* Output the GENERIC tree. */
5580 dump_function (TDI_original, fndecl);
5581
5582 /* Store the end of the function, so that we get good line number
5583 info for the epilogue. */
5584 cfun->function_end_locus = input_location;
5585
5586 /* We're leaving the context of this function, so zap cfun.
5587 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
5588 tree_rest_of_compilation. */
87d4aa85 5589 set_cfun (NULL);
4ee9c684 5590
5591 if (old_context)
5592 {
5593 pop_function_context ();
5594 saved_function_decls = saved_parent_function_decls;
5595 }
5596 current_function_decl = old_context;
5597
8b01dcb7 5598 if (decl_function_context (fndecl) && gfc_option.coarray != GFC_FCOARRAY_LIB
a961ca30 5599 && has_coarray_vars)
6374121b 5600 /* Register this function with cgraph just far enough to get it
a961ca30 5601 added to our parent's nested function list.
5602 If there are static coarrays in this function, the nested _caf_init
5603 function has already called cgraph_create_node, which also created
5604 the cgraph node for this function. */
460beda6 5605 (void) cgraph_create_node (fndecl);
4ee9c684 5606 else
bb982f66 5607 cgraph_finalize_function (fndecl, true);
df4d540f 5608
5609 gfc_trans_use_stmts (ns);
2eb674c9 5610 gfc_traverse_ns (ns, gfc_emit_parameter_debug_info);
7257a5d2 5611
5612 if (sym->attr.is_main_program)
5613 create_main_function (fndecl);
89ac8ba1 5614
5615 current_procedure_symbol = previous_procedure_symbol;
4ee9c684 5616}
5617
7257a5d2 5618
4ee9c684 5619void
5620gfc_generate_constructors (void)
5621{
22d678e8 5622 gcc_assert (gfc_static_ctors == NULL_TREE);
4ee9c684 5623#if 0
5624 tree fnname;
5625 tree type;
5626 tree fndecl;
5627 tree decl;
5628 tree tmp;
5629
5630 if (gfc_static_ctors == NULL_TREE)
5631 return;
5632
db85cc4f 5633 fnname = get_file_function_name ("I");
e1036019 5634 type = build_function_type_list (void_type_node, NULL_TREE);
4ee9c684 5635
e60a6f7b 5636 fndecl = build_decl (input_location,
5637 FUNCTION_DECL, fnname, type);
4ee9c684 5638 TREE_PUBLIC (fndecl) = 1;
5639
e60a6f7b 5640 decl = build_decl (input_location,
5641 RESULT_DECL, NULL_TREE, void_type_node);
540edea7 5642 DECL_ARTIFICIAL (decl) = 1;
5643 DECL_IGNORED_P (decl) = 1;
4ee9c684 5644 DECL_CONTEXT (decl) = fndecl;
5645 DECL_RESULT (fndecl) = decl;
5646
5647 pushdecl (fndecl);
5648
5649 current_function_decl = fndecl;
5650
b2c4af5e 5651 rest_of_decl_compilation (fndecl, 1, 0);
4ee9c684 5652
b2c4af5e 5653 make_decl_rtl (fndecl);
4ee9c684 5654
00cf115c 5655 allocate_struct_function (fndecl, false);
4ee9c684 5656
cde2be84 5657 pushlevel ();
4ee9c684 5658
5659 for (; gfc_static_ctors; gfc_static_ctors = TREE_CHAIN (gfc_static_ctors))
5660 {
389dd41b 5661 tmp = build_call_expr_loc (input_location,
5662 TREE_VALUE (gfc_static_ctors), 0);
e60a6f7b 5663 DECL_SAVED_TREE (fndecl) = build_stmt (input_location, EXPR_STMT, tmp);
4ee9c684 5664 }
5665
e5004242 5666 decl = getdecls ();
cde2be84 5667 poplevel (1, 1);
4ee9c684 5668
5669 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
e5004242 5670 DECL_SAVED_TREE (fndecl)
5671 = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (fndecl),
5672 DECL_INITIAL (fndecl));
4ee9c684 5673
5674 free_after_parsing (cfun);
5675 free_after_compilation (cfun);
5676
6148a911 5677 tree_rest_of_compilation (fndecl);
4ee9c684 5678
5679 current_function_decl = NULL_TREE;
5680#endif
5681}
5682
9ec7c303 5683/* Translates a BLOCK DATA program unit. This means emitting the
5684 commons contained therein plus their initializations. We also emit
5685 a globally visible symbol to make sure that each BLOCK DATA program
5686 unit remains unique. */
5687
5688void
5689gfc_generate_block_data (gfc_namespace * ns)
5690{
5691 tree decl;
5692 tree id;
5693
b31f705b 5694 /* Tell the backend the source location of the block data. */
5695 if (ns->proc_name)
5696 gfc_set_backend_locus (&ns->proc_name->declared_at);
5697 else
5698 gfc_set_backend_locus (&gfc_current_locus);
5699
5700 /* Process the DATA statements. */
9ec7c303 5701 gfc_trans_common (ns);
5702
b31f705b 5703 /* Create a global symbol with the mane of the block data. This is to
5704 generate linker errors if the same name is used twice. It is never
5705 really used. */
9ec7c303 5706 if (ns->proc_name)
5707 id = gfc_sym_mangled_function_id (ns->proc_name);
5708 else
5709 id = get_identifier ("__BLOCK_DATA__");
5710
e60a6f7b 5711 decl = build_decl (input_location,
5712 VAR_DECL, id, gfc_array_index_type);
9ec7c303 5713 TREE_PUBLIC (decl) = 1;
5714 TREE_STATIC (decl) = 1;
df4d540f 5715 DECL_IGNORED_P (decl) = 1;
9ec7c303 5716
5717 pushdecl (decl);
5718 rest_of_decl_compilation (decl, 1, 0);
5719}
5720
b549d2a5 5721
6a7084d7 5722/* Process the local variables of a BLOCK construct. */
5723
5724void
3c82e013 5725gfc_process_block_locals (gfc_namespace* ns)
6a7084d7 5726{
5727 tree decl;
5728
5729 gcc_assert (saved_local_decls == NULL_TREE);
a961ca30 5730 has_coarray_vars = false;
5731
6a7084d7 5732 generate_local_vars (ns);
5733
a961ca30 5734 if (gfc_option.coarray == GFC_FCOARRAY_LIB && has_coarray_vars)
5735 generate_coarray_init (ns);
5736
6a7084d7 5737 decl = saved_local_decls;
5738 while (decl)
5739 {
5740 tree next;
5741
1767a056 5742 next = DECL_CHAIN (decl);
5743 DECL_CHAIN (decl) = NULL_TREE;
6a7084d7 5744 pushdecl (decl);
5745 decl = next;
5746 }
5747 saved_local_decls = NULL_TREE;
5748}
5749
5750
4ee9c684 5751#include "gt-fortran-trans-decl.h"