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