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