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