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