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