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