]> gcc.gnu.org Git - gcc.git/blame - gcc/fortran/trans-decl.c
re PR fortran/36186 (Wrong handling of BOZ in CMPLX)
[gcc.git] / gcc / fortran / trans-decl.c
CommitLineData
6de9cd9a 1/* Backend function setup
44855d8c 2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software
710a179f 3 Foundation, Inc.
6de9cd9a
DN
4 Contributed by Paul Brook
5
9fc4d79b 6This file is part of GCC.
6de9cd9a 7
9fc4d79b
TS
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
d234d788 10Software Foundation; either version 3, or (at your option) any later
9fc4d79b 11version.
6de9cd9a 12
9fc4d79b
TS
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
6de9cd9a
DN
17
18You should have received a copy of the GNU General Public License
d234d788
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
6de9cd9a
DN
21
22/* trans-decl.c -- Handling of backend function and variable decls, etc */
23
24#include "config.h"
25#include "system.h"
26#include "coretypes.h"
27#include "tree.h"
28#include "tree-dump.h"
eadf906f 29#include "tree-gimple.h"
6de9cd9a
DN
30#include "ggc.h"
31#include "toplev.h"
32#include "tm.h"
25f2dfd3 33#include "rtl.h"
6de9cd9a
DN
34#include "target.h"
35#include "function.h"
6de9cd9a
DN
36#include "flags.h"
37#include "cgraph.h"
6de9cd9a
DN
38#include "gfortran.h"
39#include "trans.h"
40#include "trans-types.h"
41#include "trans-array.h"
42#include "trans-const.h"
43/* Only for gfc_trans_code. Shouldn't need to include this. */
44#include "trans-stmt.h"
45
46#define MAX_LABEL_VALUE 99999
47
48
49/* Holds the result of the function if no result variable specified. */
50
51static GTY(()) tree current_fake_result_decl;
5f20c93a 52static GTY(()) tree parent_fake_result_decl;
6de9cd9a
DN
53
54static GTY(()) tree current_function_return_label;
55
56
57/* Holds the variable DECLs for the current function. */
58
417ab240
JJ
59static GTY(()) tree saved_function_decls;
60static GTY(()) tree saved_parent_function_decls;
6de9cd9a
DN
61
62
63/* The namespace of the module we're currently generating. Only used while
64 outputting decls for module variables. Do not rely on this being set. */
65
66static gfc_namespace *module_namespace;
67
68
69/* List of static constructor functions. */
70
71tree gfc_static_ctors;
72
73
74/* Function declarations for builtin library functions. */
75
6de9cd9a
DN
76tree gfor_fndecl_pause_numeric;
77tree gfor_fndecl_pause_string;
78tree gfor_fndecl_stop_numeric;
79tree gfor_fndecl_stop_string;
80tree gfor_fndecl_select_string;
81tree gfor_fndecl_runtime_error;
f96d606f 82tree gfor_fndecl_runtime_error_at;
1529b8d9 83tree gfor_fndecl_os_error;
f96d606f 84tree gfor_fndecl_generate_error;
944b8b35 85tree gfor_fndecl_set_fpe;
68d2e027 86tree gfor_fndecl_set_options;
eaa90d25 87tree gfor_fndecl_set_convert;
d67ab5ee 88tree gfor_fndecl_set_record_marker;
07b3bbf2 89tree gfor_fndecl_set_max_subrecord_length;
35059811
FXC
90tree gfor_fndecl_ctime;
91tree gfor_fndecl_fdate;
25fc05eb 92tree gfor_fndecl_ttynam;
6de9cd9a
DN
93tree gfor_fndecl_in_pack;
94tree gfor_fndecl_in_unpack;
95tree gfor_fndecl_associated;
96
97
98/* Math functions. Many other math functions are handled in
99 trans-intrinsic.c. */
100
644cb69f 101gfc_powdecl_list gfor_fndecl_math_powi[4][3];
6de9cd9a
DN
102tree gfor_fndecl_math_ishftc4;
103tree gfor_fndecl_math_ishftc8;
644cb69f 104tree gfor_fndecl_math_ishftc16;
6de9cd9a
DN
105
106
107/* String functions. */
108
6de9cd9a
DN
109tree gfor_fndecl_compare_string;
110tree gfor_fndecl_concat_string;
111tree gfor_fndecl_string_len_trim;
112tree gfor_fndecl_string_index;
113tree gfor_fndecl_string_scan;
114tree gfor_fndecl_string_verify;
115tree gfor_fndecl_string_trim;
2263c775 116tree gfor_fndecl_string_minmax;
6de9cd9a
DN
117tree gfor_fndecl_adjustl;
118tree gfor_fndecl_adjustr;
119
120
121/* Other misc. runtime library functions. */
122
123tree gfor_fndecl_size0;
124tree gfor_fndecl_size1;
b41b2534 125tree gfor_fndecl_iargc;
6de9cd9a 126
a39fafac
FXC
127/* Intrinsic functions implemented in Fortran. */
128tree gfor_fndecl_sc_kind;
6de9cd9a
DN
129tree gfor_fndecl_si_kind;
130tree gfor_fndecl_sr_kind;
131
5a0aad31
FXC
132/* BLAS gemm functions. */
133tree gfor_fndecl_sgemm;
134tree gfor_fndecl_dgemm;
135tree gfor_fndecl_cgemm;
136tree gfor_fndecl_zgemm;
137
6de9cd9a
DN
138
139static void
140gfc_add_decl_to_parent_function (tree decl)
141{
6e45f57b 142 gcc_assert (decl);
6de9cd9a
DN
143 DECL_CONTEXT (decl) = DECL_CONTEXT (current_function_decl);
144 DECL_NONLOCAL (decl) = 1;
145 TREE_CHAIN (decl) = saved_parent_function_decls;
146 saved_parent_function_decls = decl;
147}
148
149void
150gfc_add_decl_to_function (tree decl)
151{
6e45f57b 152 gcc_assert (decl);
6de9cd9a
DN
153 TREE_USED (decl) = 1;
154 DECL_CONTEXT (decl) = current_function_decl;
155 TREE_CHAIN (decl) = saved_function_decls;
156 saved_function_decls = decl;
157}
158
159
c006df4e
SB
160/* Build a backend label declaration. Set TREE_USED for named labels.
161 The context of the label is always the current_function_decl. All
162 labels are marked artificial. */
6de9cd9a
DN
163
164tree
165gfc_build_label_decl (tree label_id)
166{
167 /* 2^32 temporaries should be enough. */
168 static unsigned int tmp_num = 1;
169 tree label_decl;
170 char *label_name;
171
172 if (label_id == NULL_TREE)
173 {
174 /* Build an internal label name. */
175 ASM_FORMAT_PRIVATE_NAME (label_name, "L", tmp_num++);
176 label_id = get_identifier (label_name);
177 }
178 else
179 label_name = NULL;
180
181 /* Build the LABEL_DECL node. Labels have no type. */
182 label_decl = build_decl (LABEL_DECL, label_id, void_type_node);
183 DECL_CONTEXT (label_decl) = current_function_decl;
184 DECL_MODE (label_decl) = VOIDmode;
185
c006df4e
SB
186 /* We always define the label as used, even if the original source
187 file never references the label. We don't want all kinds of
188 spurious warnings for old-style Fortran code with too many
189 labels. */
190 TREE_USED (label_decl) = 1;
6de9cd9a 191
c006df4e 192 DECL_ARTIFICIAL (label_decl) = 1;
6de9cd9a
DN
193 return label_decl;
194}
195
196
197/* Returns the return label for the current function. */
198
199tree
200gfc_get_return_label (void)
201{
202 char name[GFC_MAX_SYMBOL_LEN + 10];
203
204 if (current_function_return_label)
205 return current_function_return_label;
206
207 sprintf (name, "__return_%s",
208 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
209
210 current_function_return_label =
211 gfc_build_label_decl (get_identifier (name));
212
213 DECL_ARTIFICIAL (current_function_return_label) = 1;
214
215 return current_function_return_label;
216}
217
218
c8cc8542
PB
219/* Set the backend source location of a decl. */
220
221void
222gfc_set_decl_location (tree decl, locus * loc)
223{
c8cc8542 224 DECL_SOURCE_LOCATION (decl) = loc->lb->location;
c8cc8542
PB
225}
226
227
6de9cd9a
DN
228/* Return the backend label declaration for a given label structure,
229 or create it if it doesn't exist yet. */
230
231tree
232gfc_get_label_decl (gfc_st_label * lp)
233{
6de9cd9a
DN
234 if (lp->backend_decl)
235 return lp->backend_decl;
236 else
237 {
238 char label_name[GFC_MAX_SYMBOL_LEN + 1];
239 tree label_decl;
240
241 /* Validate the label declaration from the front end. */
6e45f57b 242 gcc_assert (lp != NULL && lp->value <= MAX_LABEL_VALUE);
6de9cd9a
DN
243
244 /* Build a mangled name for the label. */
245 sprintf (label_name, "__label_%.6d", lp->value);
246
247 /* Build the LABEL_DECL node. */
248 label_decl = gfc_build_label_decl (get_identifier (label_name));
249
250 /* Tell the debugger where the label came from. */
f8d0aee5 251 if (lp->value <= MAX_LABEL_VALUE) /* An internal label. */
c8cc8542 252 gfc_set_decl_location (label_decl, &lp->where);
6de9cd9a
DN
253 else
254 DECL_ARTIFICIAL (label_decl) = 1;
255
256 /* Store the label in the label list and return the LABEL_DECL. */
257 lp->backend_decl = label_decl;
258 return label_decl;
259 }
260}
261
262
263/* Convert a gfc_symbol to an identifier of the same name. */
264
265static tree
266gfc_sym_identifier (gfc_symbol * sym)
267{
268 return (get_identifier (sym->name));
269}
270
271
272/* Construct mangled name from symbol name. */
273
274static tree
275gfc_sym_mangled_identifier (gfc_symbol * sym)
276{
277 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
278
a8b3b0b6
CR
279 /* Prevent the mangling of identifiers that have an assigned
280 binding label (mainly those that are bind(c)). */
281 if (sym->attr.is_bind_c == 1
282 && sym->binding_label[0] != '\0')
283 return get_identifier(sym->binding_label);
284
cb9e4f55 285 if (sym->module == NULL)
6de9cd9a
DN
286 return gfc_sym_identifier (sym);
287 else
288 {
9998ef84 289 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
6de9cd9a
DN
290 return get_identifier (name);
291 }
292}
293
294
295/* Construct mangled function name from symbol name. */
296
297static tree
298gfc_sym_mangled_function_id (gfc_symbol * sym)
299{
300 int has_underscore;
301 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
302
a8b3b0b6
CR
303 /* It may be possible to simply use the binding label if it's
304 provided, and remove the other checks. Then we could use it
305 for other things if we wished. */
306 if ((sym->attr.is_bind_c == 1 || sym->attr.is_iso_c == 1) &&
307 sym->binding_label[0] != '\0')
308 /* use the binding label rather than the mangled name */
309 return get_identifier (sym->binding_label);
310
cb9e4f55 311 if (sym->module == NULL || sym->attr.proc == PROC_EXTERNAL
c89686a8
PT
312 || (sym->module != NULL && (sym->attr.external
313 || sym->attr.if_source == IFSRC_IFBODY)))
6de9cd9a 314 {
ecf24057
FXC
315 /* Main program is mangled into MAIN__. */
316 if (sym->attr.is_main_program)
317 return get_identifier ("MAIN__");
318
319 /* Intrinsic procedures are never mangled. */
320 if (sym->attr.proc == PROC_INTRINSIC)
6de9cd9a
DN
321 return get_identifier (sym->name);
322
323 if (gfc_option.flag_underscoring)
324 {
325 has_underscore = strchr (sym->name, '_') != 0;
326 if (gfc_option.flag_second_underscore && has_underscore)
327 snprintf (name, sizeof name, "%s__", sym->name);
328 else
329 snprintf (name, sizeof name, "%s_", sym->name);
330 return get_identifier (name);
331 }
332 else
333 return get_identifier (sym->name);
334 }
335 else
336 {
9998ef84 337 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
6de9cd9a
DN
338 return get_identifier (name);
339 }
340}
341
342
bae88af6
TS
343/* Returns true if a variable of specified size should go on the stack. */
344
345int
346gfc_can_put_var_on_stack (tree size)
347{
348 unsigned HOST_WIDE_INT low;
349
350 if (!INTEGER_CST_P (size))
351 return 0;
352
353 if (gfc_option.flag_max_stack_var_size < 0)
354 return 1;
355
356 if (TREE_INT_CST_HIGH (size) != 0)
357 return 0;
358
359 low = TREE_INT_CST_LOW (size);
360 if (low > (unsigned HOST_WIDE_INT) gfc_option.flag_max_stack_var_size)
361 return 0;
362
363/* TODO: Set a per-function stack size limit. */
364
365 return 1;
366}
367
368
b122dc6a
JJ
369/* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
370 an expression involving its corresponding pointer. There are
371 2 cases; one for variable size arrays, and one for everything else,
372 because variable-sized arrays require one fewer level of
373 indirection. */
374
375static void
376gfc_finish_cray_pointee (tree decl, gfc_symbol *sym)
377{
378 tree ptr_decl = gfc_get_symbol_decl (sym->cp_pointer);
379 tree value;
380
381 /* Parameters need to be dereferenced. */
382 if (sym->cp_pointer->attr.dummy)
38611275 383 ptr_decl = build_fold_indirect_ref (ptr_decl);
b122dc6a
JJ
384
385 /* Check to see if we're dealing with a variable-sized array. */
386 if (sym->attr.dimension
387 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
388 {
831d7813 389 /* These decls will be dereferenced later, so we don't dereference
b122dc6a
JJ
390 them here. */
391 value = convert (TREE_TYPE (decl), ptr_decl);
392 }
393 else
394 {
395 ptr_decl = convert (build_pointer_type (TREE_TYPE (decl)),
396 ptr_decl);
38611275 397 value = build_fold_indirect_ref (ptr_decl);
b122dc6a
JJ
398 }
399
400 SET_DECL_VALUE_EXPR (decl, value);
401 DECL_HAS_VALUE_EXPR_P (decl) = 1;
6c7a4dfd 402 GFC_DECL_CRAY_POINTEE (decl) = 1;
b122dc6a
JJ
403 /* This is a fake variable just for debugging purposes. */
404 TREE_ASM_WRITTEN (decl) = 1;
405}
406
407
faf28b3a 408/* Finish processing of a declaration without an initial value. */
6de9cd9a
DN
409
410static void
faf28b3a 411gfc_finish_decl (tree decl)
6de9cd9a 412{
faf28b3a
TS
413 gcc_assert (TREE_CODE (decl) == PARM_DECL
414 || DECL_INITIAL (decl) == NULL_TREE);
6de9cd9a 415
faf28b3a
TS
416 if (TREE_CODE (decl) != VAR_DECL)
417 return;
6de9cd9a 418
faf28b3a
TS
419 if (DECL_SIZE (decl) == NULL_TREE
420 && TYPE_SIZE (TREE_TYPE (decl)) != NULL_TREE)
421 layout_decl (decl, 0);
422
423 /* A few consistency checks. */
424 /* A static variable with an incomplete type is an error if it is
425 initialized. Also if it is not file scope. Otherwise, let it
426 through, but if it is not `extern' then it may cause an error
427 message later. */
428 /* An automatic variable with an incomplete type is an error. */
429
430 /* We should know the storage size. */
431 gcc_assert (DECL_SIZE (decl) != NULL_TREE
432 || (TREE_STATIC (decl)
433 ? (!DECL_INITIAL (decl) || !DECL_CONTEXT (decl))
434 : DECL_EXTERNAL (decl)));
435
436 /* The storage size should be constant. */
437 gcc_assert ((!DECL_EXTERNAL (decl) && !TREE_STATIC (decl))
438 || !DECL_SIZE (decl)
439 || TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST);
6de9cd9a
DN
440}
441
442
443/* Apply symbol attributes to a variable, and add it to the function scope. */
444
445static void
446gfc_finish_var_decl (tree decl, gfc_symbol * sym)
447{
77bb16aa 448 tree new;
f8d0aee5 449 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
6de9cd9a
DN
450 This is the equivalent of the TARGET variables.
451 We also need to set this if the variable is passed by reference in a
452 CALL statement. */
83d890b9 453
b122dc6a 454 /* Set DECL_VALUE_EXPR for Cray Pointees. */
83d890b9 455 if (sym->attr.cray_pointee)
b122dc6a 456 gfc_finish_cray_pointee (decl, sym);
83d890b9 457
6de9cd9a
DN
458 if (sym->attr.target)
459 TREE_ADDRESSABLE (decl) = 1;
460 /* If it wasn't used we wouldn't be getting it. */
461 TREE_USED (decl) = 1;
462
463 /* Chain this decl to the pending declarations. Don't do pushdecl()
464 because this would add them to the current scope rather than the
465 function scope. */
466 if (current_function_decl != NULL_TREE)
467 {
d48734ef
EE
468 if (sym->ns->proc_name->backend_decl == current_function_decl
469 || sym->result == sym)
6de9cd9a
DN
470 gfc_add_decl_to_function (decl);
471 else
472 gfc_add_decl_to_parent_function (decl);
473 }
474
b122dc6a
JJ
475 if (sym->attr.cray_pointee)
476 return;
477
a8b3b0b6
CR
478 if(sym->attr.is_bind_c == 1)
479 {
480 /* We need to put variables that are bind(c) into the common
481 segment of the object file, because this is what C would do.
482 gfortran would typically put them in either the BSS or
483 initialized data segments, and only mark them as common if
484 they were part of common blocks. However, if they are not put
485 into common space, then C cannot initialize global fortran
486 variables that it interoperates with and the draft says that
487 either Fortran or C should be able to initialize it (but not
488 both, of course.) (J3/04-007, section 15.3). */
489 TREE_PUBLIC(decl) = 1;
490 DECL_COMMON(decl) = 1;
491 }
492
6de9cd9a
DN
493 /* If a variable is USE associated, it's always external. */
494 if (sym->attr.use_assoc)
495 {
496 DECL_EXTERNAL (decl) = 1;
497 TREE_PUBLIC (decl) = 1;
498 }
cb9e4f55 499 else if (sym->module && !sym->attr.result && !sym->attr.dummy)
6de9cd9a 500 {
adf3ed3f 501 /* TODO: Don't set sym->module for result or dummy variables. */
d48734ef 502 gcc_assert (current_function_decl == NULL_TREE || sym->result == sym);
6de9cd9a
DN
503 /* This is the declaration of a module variable. */
504 TREE_PUBLIC (decl) = 1;
505 TREE_STATIC (decl) = 1;
506 }
507
b7b184a8
PT
508 /* Derived types are a bit peculiar because of the possibility of
509 a default initializer; this must be applied each time the variable
510 comes into scope it therefore need not be static. These variables
511 are SAVE_NONE but have an initializer. Otherwise explicitly
512 intitialized variables are SAVE_IMPLICIT and explicitly saved are
513 SAVE_EXPLICIT. */
514 if (!sym->attr.use_assoc
515 && (sym->attr.save != SAVE_NONE || sym->attr.data
516 || (sym->value && sym->ns->proc_name->attr.is_main_program)))
6de9cd9a 517 TREE_STATIC (decl) = 1;
775e6c3a
TB
518
519 if (sym->attr.volatile_)
520 {
775e6c3a
TB
521 TREE_THIS_VOLATILE (decl) = 1;
522 new = build_qualified_type (TREE_TYPE (decl), TYPE_QUAL_VOLATILE);
523 TREE_TYPE (decl) = new;
524 }
525
6de9cd9a
DN
526 /* Keep variables larger than max-stack-var-size off stack. */
527 if (!sym->ns->proc_name->attr.recursive
528 && INTEGER_CST_P (DECL_SIZE_UNIT (decl))
9eccb94d
JJ
529 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
530 /* Put variable length auto array pointers always into stack. */
531 && (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
532 || sym->attr.dimension == 0
533 || sym->as->type != AS_EXPLICIT
534 || sym->attr.pointer
535 || sym->attr.allocatable)
536 && !DECL_ARTIFICIAL (decl))
6de9cd9a 537 TREE_STATIC (decl) = 1;
6c7a4dfd
JJ
538
539 /* Handle threadprivate variables. */
8893239d 540 if (sym->attr.threadprivate
6c7a4dfd
JJ
541 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
542 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
6de9cd9a
DN
543}
544
545
546/* Allocate the lang-specific part of a decl. */
547
548void
549gfc_allocate_lang_decl (tree decl)
550{
551 DECL_LANG_SPECIFIC (decl) = (struct lang_decl *)
552 ggc_alloc_cleared (sizeof (struct lang_decl));
553}
554
555/* Remember a symbol to generate initialization/cleanup code at function
556 entry/exit. */
557
558static void
559gfc_defer_symbol_init (gfc_symbol * sym)
560{
561 gfc_symbol *p;
562 gfc_symbol *last;
563 gfc_symbol *head;
564
565 /* Don't add a symbol twice. */
566 if (sym->tlink)
567 return;
568
569 last = head = sym->ns->proc_name;
570 p = last->tlink;
571
572 /* Make sure that setup code for dummy variables which are used in the
573 setup of other variables is generated first. */
574 if (sym->attr.dummy)
575 {
576 /* Find the first dummy arg seen after us, or the first non-dummy arg.
577 This is a circular list, so don't go past the head. */
578 while (p != head
579 && (!p->attr.dummy || p->dummy_order > sym->dummy_order))
580 {
581 last = p;
582 p = p->tlink;
583 }
584 }
585 /* Insert in between last and p. */
586 last->tlink = sym;
587 sym->tlink = p;
588}
589
590
591/* Create an array index type variable with function scope. */
592
593static tree
594create_index_var (const char * pfx, int nest)
595{
596 tree decl;
597
598 decl = gfc_create_var_np (gfc_array_index_type, pfx);
599 if (nest)
600 gfc_add_decl_to_parent_function (decl);
601 else
602 gfc_add_decl_to_function (decl);
603 return decl;
604}
605
606
607/* Create variables to hold all the non-constant bits of info for a
608 descriptorless array. Remember these in the lang-specific part of the
609 type. */
610
611static void
612gfc_build_qualified_array (tree decl, gfc_symbol * sym)
613{
614 tree type;
615 int dim;
616 int nest;
617
618 type = TREE_TYPE (decl);
619
620 /* We just use the descriptor, if there is one. */
621 if (GFC_DESCRIPTOR_TYPE_P (type))
622 return;
623
6e45f57b 624 gcc_assert (GFC_ARRAY_TYPE_P (type));
6de9cd9a
DN
625 nest = (sym->ns->proc_name->backend_decl != current_function_decl)
626 && !sym->attr.contained;
627
628 for (dim = 0; dim < GFC_TYPE_ARRAY_RANK (type); dim++)
629 {
630 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
01306727
FXC
631 {
632 GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
633 TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type, dim)) = 1;
634 }
aa9c57ec 635 /* Don't try to use the unknown bound for assumed shape arrays. */
6de9cd9a
DN
636 if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
637 && (sym->as->type != AS_ASSUMED_SIZE
638 || dim < GFC_TYPE_ARRAY_RANK (type) - 1))
01306727
FXC
639 {
640 GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
641 TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type, dim)) = 1;
642 }
6de9cd9a
DN
643
644 if (GFC_TYPE_ARRAY_STRIDE (type, dim) == NULL_TREE)
01306727
FXC
645 {
646 GFC_TYPE_ARRAY_STRIDE (type, dim) = create_index_var ("stride", nest);
647 TREE_NO_WARNING (GFC_TYPE_ARRAY_STRIDE (type, dim)) = 1;
648 }
6de9cd9a
DN
649 }
650 if (GFC_TYPE_ARRAY_OFFSET (type) == NULL_TREE)
651 {
652 GFC_TYPE_ARRAY_OFFSET (type) = gfc_create_var_np (gfc_array_index_type,
653 "offset");
01306727
FXC
654 TREE_NO_WARNING (GFC_TYPE_ARRAY_OFFSET (type)) = 1;
655
6de9cd9a
DN
656 if (nest)
657 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type));
658 else
659 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type));
660 }
417ab240
JJ
661
662 if (GFC_TYPE_ARRAY_SIZE (type) == NULL_TREE
663 && sym->as->type != AS_ASSUMED_SIZE)
01306727
FXC
664 {
665 GFC_TYPE_ARRAY_SIZE (type) = create_index_var ("size", nest);
666 TREE_NO_WARNING (GFC_TYPE_ARRAY_SIZE (type)) = 1;
667 }
417ab240
JJ
668
669 if (POINTER_TYPE_P (type))
670 {
671 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type)));
672 gcc_assert (TYPE_LANG_SPECIFIC (type)
673 == TYPE_LANG_SPECIFIC (TREE_TYPE (type)));
674 type = TREE_TYPE (type);
675 }
676
677 if (! COMPLETE_TYPE_P (type) && GFC_TYPE_ARRAY_SIZE (type))
678 {
679 tree size, range;
680
44855d8c
TS
681 size = fold_build2 (MINUS_EXPR, gfc_array_index_type,
682 GFC_TYPE_ARRAY_SIZE (type), gfc_index_one_node);
417ab240
JJ
683 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
684 size);
685 TYPE_DOMAIN (type) = range;
686 layout_type (type);
687 }
6de9cd9a
DN
688}
689
690
691/* For some dummy arguments we don't use the actual argument directly.
bae88af6 692 Instead we create a local decl and use that. This allows us to perform
6de9cd9a
DN
693 initialization, and construct full type information. */
694
695static tree
696gfc_build_dummy_array_decl (gfc_symbol * sym, tree dummy)
697{
698 tree decl;
699 tree type;
700 gfc_array_spec *as;
701 char *name;
dcfef7d4 702 gfc_packed packed;
6de9cd9a
DN
703 int n;
704 bool known_size;
705
706 if (sym->attr.pointer || sym->attr.allocatable)
707 return dummy;
708
709 /* Add to list of variables if not a fake result variable. */
710 if (sym->attr.result || sym->attr.dummy)
711 gfc_defer_symbol_init (sym);
712
713 type = TREE_TYPE (dummy);
6e45f57b 714 gcc_assert (TREE_CODE (dummy) == PARM_DECL
6de9cd9a
DN
715 && POINTER_TYPE_P (type));
716
f8d0aee5 717 /* Do we know the element size? */
6de9cd9a
DN
718 known_size = sym->ts.type != BT_CHARACTER
719 || INTEGER_CST_P (sym->ts.cl->backend_decl);
720
721 if (known_size && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type)))
722 {
723 /* For descriptorless arrays with known element size the actual
724 argument is sufficient. */
6e45f57b 725 gcc_assert (GFC_ARRAY_TYPE_P (type));
6de9cd9a
DN
726 gfc_build_qualified_array (dummy, sym);
727 return dummy;
728 }
729
730 type = TREE_TYPE (type);
731 if (GFC_DESCRIPTOR_TYPE_P (type))
732 {
fa951694 733 /* Create a descriptorless array pointer. */
6de9cd9a 734 as = sym->as;
dcfef7d4 735 packed = PACKED_NO;
6de9cd9a
DN
736 if (!gfc_option.flag_repack_arrays)
737 {
738 if (as->type == AS_ASSUMED_SIZE)
dcfef7d4 739 packed = PACKED_FULL;
6de9cd9a
DN
740 }
741 else
742 {
743 if (as->type == AS_EXPLICIT)
744 {
dcfef7d4 745 packed = PACKED_FULL;
6de9cd9a
DN
746 for (n = 0; n < as->rank; n++)
747 {
748 if (!(as->upper[n]
749 && as->lower[n]
750 && as->upper[n]->expr_type == EXPR_CONSTANT
751 && as->lower[n]->expr_type == EXPR_CONSTANT))
dcfef7d4 752 packed = PACKED_PARTIAL;
6de9cd9a
DN
753 }
754 }
755 else
dcfef7d4 756 packed = PACKED_PARTIAL;
6de9cd9a
DN
757 }
758
759 type = gfc_typenode_for_spec (&sym->ts);
760 type = gfc_get_nodesc_array_type (type, sym->as, packed);
761 }
762 else
763 {
764 /* We now have an expression for the element size, so create a fully
765 qualified type. Reset sym->backend decl or this will just return the
766 old type. */
3e978d30 767 DECL_ARTIFICIAL (sym->backend_decl) = 1;
6de9cd9a
DN
768 sym->backend_decl = NULL_TREE;
769 type = gfc_sym_type (sym);
dcfef7d4 770 packed = PACKED_FULL;
6de9cd9a
DN
771 }
772
773 ASM_FORMAT_PRIVATE_NAME (name, IDENTIFIER_POINTER (DECL_NAME (dummy)), 0);
774 decl = build_decl (VAR_DECL, get_identifier (name), type);
775
776 DECL_ARTIFICIAL (decl) = 1;
777 TREE_PUBLIC (decl) = 0;
778 TREE_STATIC (decl) = 0;
779 DECL_EXTERNAL (decl) = 0;
780
781 /* We should never get deferred shape arrays here. We used to because of
782 frontend bugs. */
6e45f57b 783 gcc_assert (sym->as->type != AS_DEFERRED);
6de9cd9a 784
dcfef7d4
TS
785 if (packed == PACKED_PARTIAL)
786 GFC_DECL_PARTIAL_PACKED_ARRAY (decl) = 1;
787 else if (packed == PACKED_FULL)
788 GFC_DECL_PACKED_ARRAY (decl) = 1;
6de9cd9a
DN
789
790 gfc_build_qualified_array (decl, sym);
791
792 if (DECL_LANG_SPECIFIC (dummy))
793 DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (dummy);
794 else
795 gfc_allocate_lang_decl (decl);
796
797 GFC_DECL_SAVED_DESCRIPTOR (decl) = dummy;
798
799 if (sym->ns->proc_name->backend_decl == current_function_decl
800 || sym->attr.contained)
801 gfc_add_decl_to_function (decl);
802 else
803 gfc_add_decl_to_parent_function (decl);
804
805 return decl;
806}
807
808
809/* Return a constant or a variable to use as a string length. Does not
810 add the decl to the current scope. */
811
812static tree
813gfc_create_string_length (gfc_symbol * sym)
814{
815 tree length;
816
6e45f57b 817 gcc_assert (sym->ts.cl);
6de9cd9a
DN
818 gfc_conv_const_charlen (sym->ts.cl);
819
820 if (sym->ts.cl->backend_decl == NULL_TREE)
821 {
822 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
823
824 /* Also prefix the mangled name. */
825 strcpy (&name[1], sym->name);
826 name[0] = '.';
827 length = build_decl (VAR_DECL, get_identifier (name),
d7177ab2 828 gfc_charlen_type_node);
6de9cd9a
DN
829 DECL_ARTIFICIAL (length) = 1;
830 TREE_USED (length) = 1;
417ab240
JJ
831 if (sym->ns->proc_name->tlink != NULL)
832 gfc_defer_symbol_init (sym);
6de9cd9a
DN
833 sym->ts.cl->backend_decl = length;
834 }
835
836 return sym->ts.cl->backend_decl;
837}
838
910450c1
FW
839/* If a variable is assigned a label, we add another two auxiliary
840 variables. */
841
842static void
843gfc_add_assign_aux_vars (gfc_symbol * sym)
844{
845 tree addr;
846 tree length;
847 tree decl;
848
849 gcc_assert (sym->backend_decl);
850
851 decl = sym->backend_decl;
852 gfc_allocate_lang_decl (decl);
853 GFC_DECL_ASSIGN (decl) = 1;
854 length = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
855 gfc_charlen_type_node);
856 addr = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
857 pvoid_type_node);
858 gfc_finish_var_decl (length, sym);
859 gfc_finish_var_decl (addr, sym);
860 /* STRING_LENGTH is also used as flag. Less than -1 means that
861 ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the
862 target label's address. Otherwise, value is the length of a format string
863 and ASSIGN_ADDR is its address. */
864 if (TREE_STATIC (length))
865 DECL_INITIAL (length) = build_int_cst (NULL_TREE, -2);
866 else
867 gfc_defer_symbol_init (sym);
868
869 GFC_DECL_STRING_LEN (decl) = length;
870 GFC_DECL_ASSIGN_ADDR (decl) = addr;
871}
6de9cd9a
DN
872
873/* Return the decl for a gfc_symbol, create it if it doesn't already
874 exist. */
875
876tree
877gfc_get_symbol_decl (gfc_symbol * sym)
878{
879 tree decl;
880 tree length = NULL_TREE;
6de9cd9a
DN
881 int byref;
882
61321991 883 gcc_assert (sym->attr.referenced
c60a84b8
PT
884 || sym->attr.use_assoc
885 || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY);
6de9cd9a
DN
886
887 if (sym->ns && sym->ns->proc_name->attr.function)
888 byref = gfc_return_by_reference (sym->ns->proc_name);
889 else
890 byref = 0;
891
892 if ((sym->attr.dummy && ! sym->attr.function) || (sym->attr.result && byref))
893 {
894 /* Return via extra parameter. */
895 if (sym->attr.result && byref
896 && !sym->backend_decl)
897 {
898 sym->backend_decl =
899 DECL_ARGUMENTS (sym->ns->proc_name->backend_decl);
d198b59a
JJ
900 /* For entry master function skip over the __entry
901 argument. */
902 if (sym->ns->proc_name->attr.entry_master)
903 sym->backend_decl = TREE_CHAIN (sym->backend_decl);
6de9cd9a
DN
904 }
905
906 /* Dummy variables should already have been created. */
6e45f57b 907 gcc_assert (sym->backend_decl);
6de9cd9a
DN
908
909 /* Create a character length variable. */
910 if (sym->ts.type == BT_CHARACTER)
911 {
912 if (sym->ts.cl->backend_decl == NULL_TREE)
417ab240
JJ
913 length = gfc_create_string_length (sym);
914 else
915 length = sym->ts.cl->backend_decl;
916 if (TREE_CODE (length) == VAR_DECL
917 && DECL_CONTEXT (length) == NULL_TREE)
6de9cd9a 918 {
3e978d30
PT
919 /* Add the string length to the same context as the symbol. */
920 if (DECL_CONTEXT (sym->backend_decl) == current_function_decl)
921 gfc_add_decl_to_function (length);
922 else
923 gfc_add_decl_to_parent_function (length);
924
925 gcc_assert (DECL_CONTEXT (sym->backend_decl) ==
926 DECL_CONTEXT (length));
927
417ab240 928 gfc_defer_symbol_init (sym);
a41baa64 929 }
6de9cd9a
DN
930 }
931
932 /* Use a copy of the descriptor for dummy arrays. */
933 if (sym->attr.dimension && !TREE_USED (sym->backend_decl))
934 {
3e978d30
PT
935 decl = gfc_build_dummy_array_decl (sym, sym->backend_decl);
936 /* Prevent the dummy from being detected as unused if it is copied. */
937 if (sym->backend_decl != NULL && decl != sym->backend_decl)
938 DECL_ARTIFICIAL (sym->backend_decl) = 1;
939 sym->backend_decl = decl;
6de9cd9a
DN
940 }
941
942 TREE_USED (sym->backend_decl) = 1;
910450c1
FW
943 if (sym->attr.assign && GFC_DECL_ASSIGN (sym->backend_decl) == 0)
944 {
945 gfc_add_assign_aux_vars (sym);
946 }
6de9cd9a
DN
947 return sym->backend_decl;
948 }
949
950 if (sym->backend_decl)
951 return sym->backend_decl;
952
6de9cd9a
DN
953 /* Catch function declarations. Only used for actual parameters. */
954 if (sym->attr.flavor == FL_PROCEDURE)
955 {
956 decl = gfc_get_extern_function_decl (sym);
957 return decl;
958 }
959
960 if (sym->attr.intrinsic)
961 internal_error ("intrinsic variable which isn't a procedure");
962
963 /* Create string length decl first so that they can be used in the
964 type declaration. */
965 if (sym->ts.type == BT_CHARACTER)
966 length = gfc_create_string_length (sym);
967
968 /* Create the decl for the variable. */
969 decl = build_decl (VAR_DECL, gfc_sym_identifier (sym), gfc_sym_type (sym));
970
c8cc8542
PB
971 gfc_set_decl_location (decl, &sym->declared_at);
972
f8d0aee5 973 /* Symbols from modules should have their assembler names mangled.
6de9cd9a
DN
974 This is done here rather than in gfc_finish_var_decl because it
975 is different for string length variables. */
cb9e4f55 976 if (sym->module)
6de9cd9a
DN
977 SET_DECL_ASSEMBLER_NAME (decl, gfc_sym_mangled_identifier (sym));
978
979 if (sym->attr.dimension)
980 {
981 /* Create variables to hold the non-constant bits of array info. */
982 gfc_build_qualified_array (decl, sym);
983
984 /* Remember this variable for allocation/cleanup. */
985 gfc_defer_symbol_init (sym);
986
987 if ((sym->attr.allocatable || !sym->attr.dummy) && !sym->attr.pointer)
988 GFC_DECL_PACKED_ARRAY (decl) = 1;
989 }
990
5046aff5
PT
991 if (sym->ts.type == BT_DERIVED && sym->ts.derived->attr.alloc_comp)
992 gfc_defer_symbol_init (sym);
b7b184a8
PT
993 /* This applies a derived type default initializer. */
994 else if (sym->ts.type == BT_DERIVED
995 && sym->attr.save == SAVE_NONE
996 && !sym->attr.data
997 && !sym->attr.allocatable
998 && (sym->value && !sym->ns->proc_name->attr.is_main_program)
999 && !sym->attr.use_assoc)
1000 gfc_defer_symbol_init (sym);
5046aff5 1001
6de9cd9a
DN
1002 gfc_finish_var_decl (decl, sym);
1003
597073ac 1004 if (sym->ts.type == BT_CHARACTER)
6de9cd9a 1005 {
6de9cd9a
DN
1006 /* Character variables need special handling. */
1007 gfc_allocate_lang_decl (decl);
1008
597073ac 1009 if (TREE_CODE (length) != INTEGER_CST)
6de9cd9a
DN
1010 {
1011 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
1012
cb9e4f55 1013 if (sym->module)
6de9cd9a
DN
1014 {
1015 /* Also prefix the mangled name for symbols from modules. */
1016 strcpy (&name[1], sym->name);
1017 name[0] = '.';
1018 strcpy (&name[1],
1019 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (length)));
1020 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (name));
1021 }
1022 gfc_finish_var_decl (length, sym);
6e45f57b 1023 gcc_assert (!sym->value);
6de9cd9a 1024 }
6de9cd9a 1025 }
1d6b7f39
PT
1026 else if (sym->attr.subref_array_pointer)
1027 {
1028 /* We need the span for these beasts. */
1029 gfc_allocate_lang_decl (decl);
1030 }
1031
1032 if (sym->attr.subref_array_pointer)
1033 {
1034 tree span;
1035 GFC_DECL_SUBREF_ARRAY_P (decl) = 1;
1036 span = build_decl (VAR_DECL, create_tmp_var_name ("span"),
1037 gfc_array_index_type);
1038 gfc_finish_var_decl (span, sym);
1039 TREE_STATIC (span) = 1;
1040 DECL_INITIAL (span) = build_int_cst (NULL_TREE, 0);
1041
1042 GFC_DECL_SPAN (decl) = span;
1043 }
1044
6de9cd9a
DN
1045 sym->backend_decl = decl;
1046
910450c1 1047 if (sym->attr.assign)
6c0e51c4 1048 gfc_add_assign_aux_vars (sym);
910450c1 1049
597073ac
PB
1050 if (TREE_STATIC (decl) && !sym->attr.use_assoc)
1051 {
1052 /* Add static initializer. */
1053 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
1054 TREE_TYPE (decl), sym->attr.dimension,
1055 sym->attr.pointer || sym->attr.allocatable);
1056 }
1057
6de9cd9a
DN
1058 return decl;
1059}
1060
1061
7b5b57b7
PB
1062/* Substitute a temporary variable in place of the real one. */
1063
1064void
1065gfc_shadow_sym (gfc_symbol * sym, tree decl, gfc_saved_var * save)
1066{
1067 save->attr = sym->attr;
1068 save->decl = sym->backend_decl;
1069
1070 gfc_clear_attr (&sym->attr);
1071 sym->attr.referenced = 1;
1072 sym->attr.flavor = FL_VARIABLE;
1073
1074 sym->backend_decl = decl;
1075}
1076
1077
1078/* Restore the original variable. */
1079
1080void
1081gfc_restore_sym (gfc_symbol * sym, gfc_saved_var * save)
1082{
1083 sym->attr = save->attr;
1084 sym->backend_decl = save->decl;
1085}
1086
1087
6de9cd9a
DN
1088/* Get a basic decl for an external function. */
1089
1090tree
1091gfc_get_extern_function_decl (gfc_symbol * sym)
1092{
1093 tree type;
1094 tree fndecl;
1095 gfc_expr e;
1096 gfc_intrinsic_sym *isym;
1097 gfc_expr argexpr;
e6472bce 1098 char s[GFC_MAX_SYMBOL_LEN + 23]; /* "_gfortran_f2c_specific" and '\0'. */
6de9cd9a
DN
1099 tree name;
1100 tree mangled_name;
1101
1102 if (sym->backend_decl)
1103 return sym->backend_decl;
1104
3d79abbd
PB
1105 /* We should never be creating external decls for alternate entry points.
1106 The procedure may be an alternate entry point, but we don't want/need
1107 to know that. */
6e45f57b 1108 gcc_assert (!(sym->attr.entry || sym->attr.entry_master));
3d79abbd 1109
6de9cd9a
DN
1110 if (sym->attr.intrinsic)
1111 {
1112 /* Call the resolution function to get the actual name. This is
1113 a nasty hack which relies on the resolution functions only looking
1114 at the first argument. We pass NULL for the second argument
1115 otherwise things like AINT get confused. */
1116 isym = gfc_find_function (sym->name);
6e45f57b 1117 gcc_assert (isym->resolve.f0 != NULL);
6de9cd9a
DN
1118
1119 memset (&e, 0, sizeof (e));
1120 e.expr_type = EXPR_FUNCTION;
1121
1122 memset (&argexpr, 0, sizeof (argexpr));
6e45f57b 1123 gcc_assert (isym->formal);
6de9cd9a
DN
1124 argexpr.ts = isym->formal->ts;
1125
1126 if (isym->formal->next == NULL)
1127 isym->resolve.f1 (&e, &argexpr);
1128 else
1129 {
0e7e7e6e
FXC
1130 if (isym->formal->next->next == NULL)
1131 isym->resolve.f2 (&e, &argexpr, NULL);
1132 else
1133 {
5cda5098
FXC
1134 if (isym->formal->next->next->next == NULL)
1135 isym->resolve.f3 (&e, &argexpr, NULL, NULL);
1136 else
1137 {
1138 /* All specific intrinsics take less than 5 arguments. */
1139 gcc_assert (isym->formal->next->next->next->next == NULL);
1140 isym->resolve.f4 (&e, &argexpr, NULL, NULL, NULL);
1141 }
0e7e7e6e 1142 }
6de9cd9a 1143 }
973ff4c0
TS
1144
1145 if (gfc_option.flag_f2c
1146 && ((e.ts.type == BT_REAL && e.ts.kind == gfc_default_real_kind)
1147 || e.ts.type == BT_COMPLEX))
1148 {
1149 /* Specific which needs a different implementation if f2c
1150 calling conventions are used. */
e6472bce 1151 sprintf (s, "_gfortran_f2c_specific%s", e.value.function.name);
973ff4c0
TS
1152 }
1153 else
e6472bce 1154 sprintf (s, "_gfortran_specific%s", e.value.function.name);
973ff4c0 1155
6de9cd9a
DN
1156 name = get_identifier (s);
1157 mangled_name = name;
1158 }
1159 else
1160 {
1161 name = gfc_sym_identifier (sym);
1162 mangled_name = gfc_sym_mangled_function_id (sym);
1163 }
1164
1165 type = gfc_get_function_type (sym);
1166 fndecl = build_decl (FUNCTION_DECL, name, type);
1167
1168 SET_DECL_ASSEMBLER_NAME (fndecl, mangled_name);
1169 /* If the return type is a pointer, avoid alias issues by setting
1170 DECL_IS_MALLOC to nonzero. This means that the function should be
1171 treated as if it were a malloc, meaning it returns a pointer that
1172 is not an alias. */
1173 if (POINTER_TYPE_P (type))
1174 DECL_IS_MALLOC (fndecl) = 1;
1175
1176 /* Set the context of this decl. */
1177 if (0 && sym->ns && sym->ns->proc_name)
1178 {
1179 /* TODO: Add external decls to the appropriate scope. */
1180 DECL_CONTEXT (fndecl) = sym->ns->proc_name->backend_decl;
1181 }
1182 else
1183 {
f8d0aee5 1184 /* Global declaration, e.g. intrinsic subroutine. */
6de9cd9a
DN
1185 DECL_CONTEXT (fndecl) = NULL_TREE;
1186 }
1187
1188 DECL_EXTERNAL (fndecl) = 1;
1189
f8d0aee5 1190 /* This specifies if a function is globally addressable, i.e. it is
6de9cd9a
DN
1191 the opposite of declaring static in C. */
1192 TREE_PUBLIC (fndecl) = 1;
1193
1194 /* Set attributes for PURE functions. A call to PURE function in the
1195 Fortran 95 sense is both pure and without side effects in the C
1196 sense. */
1197 if (sym->attr.pure || sym->attr.elemental)
1198 {
cf013e9f 1199 if (sym->attr.function && !gfc_return_by_reference (sym))
becfd6e5 1200 DECL_PURE_P (fndecl) = 1;
b7e6a6b3
TS
1201 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
1202 parameters and don't use alternate returns (is this
1203 allowed?). In that case, calls to them are meaningless, and
3d79abbd 1204 can be optimized away. See also in build_function_decl(). */
b7e6a6b3 1205 TREE_SIDE_EFFECTS (fndecl) = 0;
6de9cd9a
DN
1206 }
1207
fe58e076
TK
1208 /* Mark non-returning functions. */
1209 if (sym->attr.noreturn)
1210 TREE_THIS_VOLATILE(fndecl) = 1;
1211
6de9cd9a
DN
1212 sym->backend_decl = fndecl;
1213
1214 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1215 pushdecl_top_level (fndecl);
1216
1217 return fndecl;
1218}
1219
1220
1221/* Create a declaration for a procedure. For external functions (in the C
3d79abbd
PB
1222 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
1223 a master function with alternate entry points. */
6de9cd9a 1224
3d79abbd
PB
1225static void
1226build_function_decl (gfc_symbol * sym)
6de9cd9a 1227{
3d79abbd 1228 tree fndecl, type;
6de9cd9a 1229 symbol_attribute attr;
3d79abbd 1230 tree result_decl;
6de9cd9a
DN
1231 gfc_formal_arglist *f;
1232
6e45f57b
PB
1233 gcc_assert (!sym->backend_decl);
1234 gcc_assert (!sym->attr.external);
6de9cd9a 1235
c8cc8542
PB
1236 /* Set the line and filename. sym->declared_at seems to point to the
1237 last statement for subroutines, but it'll do for now. */
1238 gfc_set_backend_locus (&sym->declared_at);
1239
6de9cd9a 1240 /* Allow only one nesting level. Allow public declarations. */
6e45f57b 1241 gcc_assert (current_function_decl == NULL_TREE
6de9cd9a
DN
1242 || DECL_CONTEXT (current_function_decl) == NULL_TREE);
1243
1244 type = gfc_get_function_type (sym);
1245 fndecl = build_decl (FUNCTION_DECL, gfc_sym_identifier (sym), type);
1246
1247 /* Perform name mangling if this is a top level or module procedure. */
1248 if (current_function_decl == NULL_TREE)
1249 SET_DECL_ASSEMBLER_NAME (fndecl, gfc_sym_mangled_function_id (sym));
1250
1251 /* Figure out the return type of the declared function, and build a
f8d0aee5 1252 RESULT_DECL for it. If this is a subroutine with alternate
6de9cd9a
DN
1253 returns, build a RESULT_DECL for it. */
1254 attr = sym->attr;
1255
1256 result_decl = NULL_TREE;
1257 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
1258 if (attr.function)
1259 {
1260 if (gfc_return_by_reference (sym))
1261 type = void_type_node;
1262 else
1263 {
1264 if (sym->result != sym)
1265 result_decl = gfc_sym_identifier (sym->result);
1266
1267 type = TREE_TYPE (TREE_TYPE (fndecl));
1268 }
1269 }
1270 else
1271 {
1272 /* Look for alternate return placeholders. */
1273 int has_alternate_returns = 0;
1274 for (f = sym->formal; f; f = f->next)
1275 {
1276 if (f->sym == NULL)
1277 {
1278 has_alternate_returns = 1;
1279 break;
1280 }
1281 }
1282
1283 if (has_alternate_returns)
1284 type = integer_type_node;
1285 else
1286 type = void_type_node;
1287 }
1288
1289 result_decl = build_decl (RESULT_DECL, result_decl, type);
b785f485
RH
1290 DECL_ARTIFICIAL (result_decl) = 1;
1291 DECL_IGNORED_P (result_decl) = 1;
6de9cd9a
DN
1292 DECL_CONTEXT (result_decl) = fndecl;
1293 DECL_RESULT (fndecl) = result_decl;
1294
1295 /* Don't call layout_decl for a RESULT_DECL.
f8d0aee5 1296 layout_decl (result_decl, 0); */
6de9cd9a
DN
1297
1298 /* If the return type is a pointer, avoid alias issues by setting
1299 DECL_IS_MALLOC to nonzero. This means that the function should be
1300 treated as if it were a malloc, meaning it returns a pointer that
1301 is not an alias. */
1302 if (POINTER_TYPE_P (type))
1303 DECL_IS_MALLOC (fndecl) = 1;
1304
1305 /* Set up all attributes for the function. */
1306 DECL_CONTEXT (fndecl) = current_function_decl;
1307 DECL_EXTERNAL (fndecl) = 0;
1308
ae51017b 1309 /* This specifies if a function is globally visible, i.e. it is
472ca416 1310 the opposite of declaring static in C. */
3d79abbd
PB
1311 if (DECL_CONTEXT (fndecl) == NULL_TREE
1312 && !sym->attr.entry_master)
6de9cd9a
DN
1313 TREE_PUBLIC (fndecl) = 1;
1314
1315 /* TREE_STATIC means the function body is defined here. */
1d754240 1316 TREE_STATIC (fndecl) = 1;
6de9cd9a 1317
f8d0aee5 1318 /* Set attributes for PURE functions. A call to a PURE function in the
6de9cd9a
DN
1319 Fortran 95 sense is both pure and without side effects in the C
1320 sense. */
1321 if (attr.pure || attr.elemental)
1322 {
b7e6a6b3 1323 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
86bf520d 1324 including an alternate return. In that case it can also be
1f2959f0 1325 marked as PURE. See also in gfc_get_extern_function_decl(). */
a01db3bf 1326 if (attr.function && !gfc_return_by_reference (sym))
becfd6e5 1327 DECL_PURE_P (fndecl) = 1;
6de9cd9a
DN
1328 TREE_SIDE_EFFECTS (fndecl) = 0;
1329 }
1330
ecf24057 1331 /* For -fwhole-program to work well, the main program needs to have the
f7a13f71
FXC
1332 "externally_visible" attribute. */
1333 if (attr.is_main_program)
1334 DECL_ATTRIBUTES (fndecl)
1335 = tree_cons (get_identifier("externally_visible"), NULL_TREE, NULL_TREE);
1336
6de9cd9a
DN
1337 /* Layout the function declaration and put it in the binding level
1338 of the current function. */
1d754240 1339 pushdecl (fndecl);
3d79abbd
PB
1340
1341 sym->backend_decl = fndecl;
1342}
1343
1344
1345/* Create the DECL_ARGUMENTS for a procedure. */
1346
1347static void
1348create_function_arglist (gfc_symbol * sym)
1349{
1350 tree fndecl;
1351 gfc_formal_arglist *f;
417ab240
JJ
1352 tree typelist, hidden_typelist;
1353 tree arglist, hidden_arglist;
3d79abbd
PB
1354 tree type;
1355 tree parm;
1356
1357 fndecl = sym->backend_decl;
1358
1d754240
PB
1359 /* Build formal argument list. Make sure that their TREE_CONTEXT is
1360 the new FUNCTION_DECL node. */
1d754240 1361 arglist = NULL_TREE;
417ab240 1362 hidden_arglist = NULL_TREE;
1d754240 1363 typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3d79abbd
PB
1364
1365 if (sym->attr.entry_master)
1366 {
1367 type = TREE_VALUE (typelist);
1368 parm = build_decl (PARM_DECL, get_identifier ("__entry"), type);
1369
1370 DECL_CONTEXT (parm) = fndecl;
1371 DECL_ARG_TYPE (parm) = type;
1372 TREE_READONLY (parm) = 1;
faf28b3a 1373 gfc_finish_decl (parm);
3e978d30 1374 DECL_ARTIFICIAL (parm) = 1;
3d79abbd
PB
1375
1376 arglist = chainon (arglist, parm);
1377 typelist = TREE_CHAIN (typelist);
1378 }
1379
1d754240 1380 if (gfc_return_by_reference (sym))
6de9cd9a 1381 {
417ab240 1382 tree type = TREE_VALUE (typelist), length = NULL;
6de9cd9a 1383
1d754240
PB
1384 if (sym->ts.type == BT_CHARACTER)
1385 {
1d754240 1386 /* Length of character result. */
417ab240
JJ
1387 tree len_type = TREE_VALUE (TREE_CHAIN (typelist));
1388 gcc_assert (len_type == gfc_charlen_type_node);
6de9cd9a 1389
1d754240
PB
1390 length = build_decl (PARM_DECL,
1391 get_identifier (".__result"),
417ab240 1392 len_type);
1d754240
PB
1393 if (!sym->ts.cl->length)
1394 {
1395 sym->ts.cl->backend_decl = length;
1396 TREE_USED (length) = 1;
6de9cd9a 1397 }
6e45f57b 1398 gcc_assert (TREE_CODE (length) == PARM_DECL);
1d754240 1399 DECL_CONTEXT (length) = fndecl;
417ab240 1400 DECL_ARG_TYPE (length) = len_type;
1d754240 1401 TREE_READONLY (length) = 1;
ca0e9281 1402 DECL_ARTIFICIAL (length) = 1;
faf28b3a 1403 gfc_finish_decl (length);
417ab240
JJ
1404 if (sym->ts.cl->backend_decl == NULL
1405 || sym->ts.cl->backend_decl == length)
1406 {
1407 gfc_symbol *arg;
1408 tree backend_decl;
6de9cd9a 1409
417ab240
JJ
1410 if (sym->ts.cl->backend_decl == NULL)
1411 {
1412 tree len = build_decl (VAR_DECL,
1413 get_identifier ("..__result"),
1414 gfc_charlen_type_node);
1415 DECL_ARTIFICIAL (len) = 1;
1416 TREE_USED (len) = 1;
1417 sym->ts.cl->backend_decl = len;
1418 }
6de9cd9a 1419
417ab240
JJ
1420 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1421 arg = sym->result ? sym->result : sym;
1422 backend_decl = arg->backend_decl;
1423 /* Temporary clear it, so that gfc_sym_type creates complete
1424 type. */
1425 arg->backend_decl = NULL;
1426 type = gfc_sym_type (arg);
1427 arg->backend_decl = backend_decl;
1428 type = build_reference_type (type);
1429 }
1430 }
6de9cd9a 1431
417ab240 1432 parm = build_decl (PARM_DECL, get_identifier ("__result"), type);
6de9cd9a 1433
417ab240
JJ
1434 DECL_CONTEXT (parm) = fndecl;
1435 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
1436 TREE_READONLY (parm) = 1;
1437 DECL_ARTIFICIAL (parm) = 1;
faf28b3a 1438 gfc_finish_decl (parm);
6de9cd9a 1439
417ab240
JJ
1440 arglist = chainon (arglist, parm);
1441 typelist = TREE_CHAIN (typelist);
6de9cd9a 1442
417ab240
JJ
1443 if (sym->ts.type == BT_CHARACTER)
1444 {
1445 gfc_allocate_lang_decl (parm);
1446 arglist = chainon (arglist, length);
1d754240
PB
1447 typelist = TREE_CHAIN (typelist);
1448 }
1449 }
6de9cd9a 1450
417ab240
JJ
1451 hidden_typelist = typelist;
1452 for (f = sym->formal; f; f = f->next)
1453 if (f->sym != NULL) /* Ignore alternate returns. */
1454 hidden_typelist = TREE_CHAIN (hidden_typelist);
1455
1d754240
PB
1456 for (f = sym->formal; f; f = f->next)
1457 {
1458 char name[GFC_MAX_SYMBOL_LEN + 2];
417ab240 1459
1d754240
PB
1460 /* Ignore alternate returns. */
1461 if (f->sym == NULL)
1462 continue;
6de9cd9a 1463
1d754240 1464 type = TREE_VALUE (typelist);
6de9cd9a 1465
417ab240
JJ
1466 if (f->sym->ts.type == BT_CHARACTER)
1467 {
1468 tree len_type = TREE_VALUE (hidden_typelist);
1469 tree length = NULL_TREE;
1470 gcc_assert (len_type == gfc_charlen_type_node);
1471
1472 strcpy (&name[1], f->sym->name);
1473 name[0] = '_';
1474 length = build_decl (PARM_DECL, get_identifier (name), len_type);
6de9cd9a 1475
417ab240
JJ
1476 hidden_arglist = chainon (hidden_arglist, length);
1477 DECL_CONTEXT (length) = fndecl;
1478 DECL_ARTIFICIAL (length) = 1;
1479 DECL_ARG_TYPE (length) = len_type;
1480 TREE_READONLY (length) = 1;
faf28b3a 1481 gfc_finish_decl (length);
6de9cd9a 1482
417ab240 1483 /* TODO: Check string lengths when -fbounds-check. */
6de9cd9a 1484
417ab240
JJ
1485 /* Use the passed value for assumed length variables. */
1486 if (!f->sym->ts.cl->length)
6de9cd9a 1487 {
417ab240 1488 TREE_USED (length) = 1;
9b21a380
JJ
1489 gcc_assert (!f->sym->ts.cl->backend_decl);
1490 f->sym->ts.cl->backend_decl = length;
417ab240
JJ
1491 }
1492
1493 hidden_typelist = TREE_CHAIN (hidden_typelist);
1494
1495 if (f->sym->ts.cl->backend_decl == NULL
1496 || f->sym->ts.cl->backend_decl == length)
1497 {
1498 if (f->sym->ts.cl->backend_decl == NULL)
1499 gfc_create_string_length (f->sym);
1500
1501 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1502 if (f->sym->attr.flavor == FL_PROCEDURE)
1503 type = build_pointer_type (gfc_get_function_type (f->sym));
1504 else
1505 type = gfc_sym_type (f->sym);
6de9cd9a 1506 }
6de9cd9a
DN
1507 }
1508
417ab240
JJ
1509 /* For non-constant length array arguments, make sure they use
1510 a different type node from TYPE_ARG_TYPES type. */
1511 if (f->sym->attr.dimension
1512 && type == TREE_VALUE (typelist)
1513 && TREE_CODE (type) == POINTER_TYPE
1514 && GFC_ARRAY_TYPE_P (type)
1515 && f->sym->as->type != AS_ASSUMED_SIZE
1516 && ! COMPLETE_TYPE_P (TREE_TYPE (type)))
1517 {
1518 if (f->sym->attr.flavor == FL_PROCEDURE)
1519 type = build_pointer_type (gfc_get_function_type (f->sym));
1520 else
1521 type = gfc_sym_type (f->sym);
1522 }
1523
1524 /* Build a the argument declaration. */
1525 parm = build_decl (PARM_DECL, gfc_sym_identifier (f->sym), type);
1526
1527 /* Fill in arg stuff. */
1528 DECL_CONTEXT (parm) = fndecl;
1529 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
1530 /* All implementation args are read-only. */
1531 TREE_READONLY (parm) = 1;
1532
faf28b3a 1533 gfc_finish_decl (parm);
417ab240
JJ
1534
1535 f->sym->backend_decl = parm;
1536
1537 arglist = chainon (arglist, parm);
1d754240 1538 typelist = TREE_CHAIN (typelist);
6de9cd9a 1539 }
1d754240 1540
7861a5ce
TB
1541 /* Add the hidden string length parameters, unless the procedure
1542 is bind(C). */
1543 if (!sym->attr.is_bind_c)
1544 arglist = chainon (arglist, hidden_arglist);
417ab240 1545
884d2e6b
SE
1546 gcc_assert (hidden_typelist == NULL_TREE
1547 || TREE_VALUE (hidden_typelist) == void_type_node);
1d754240 1548 DECL_ARGUMENTS (fndecl) = arglist;
3d79abbd 1549}
1d754240 1550
3d79abbd
PB
1551/* Convert FNDECL's code to GIMPLE and handle any nested functions. */
1552
1553static void
1554gfc_gimplify_function (tree fndecl)
1555{
1556 struct cgraph_node *cgn;
1557
1558 gimplify_function_tree (fndecl);
1559 dump_function (TDI_generic, fndecl);
1560
6c7a4dfd
JJ
1561 /* Generate errors for structured block violations. */
1562 /* ??? Could be done as part of resolve_labels. */
1563 if (flag_openmp)
1564 diagnose_omp_structured_block_errors (fndecl);
1565
3d79abbd
PB
1566 /* Convert all nested functions to GIMPLE now. We do things in this order
1567 so that items like VLA sizes are expanded properly in the context of the
1568 correct function. */
1569 cgn = cgraph_node (fndecl);
1570 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1571 gfc_gimplify_function (cgn->decl);
1572}
1573
1574
1575/* Do the setup necessary before generating the body of a function. */
1576
1577static void
1578trans_function_start (gfc_symbol * sym)
1579{
1580 tree fndecl;
1581
1582 fndecl = sym->backend_decl;
1583
f8d0aee5 1584 /* Let GCC know the current scope is this function. */
3d79abbd
PB
1585 current_function_decl = fndecl;
1586
f8d0aee5 1587 /* Let the world know what we're about to do. */
3d79abbd
PB
1588 announce_function (fndecl);
1589
1590 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1591 {
f8d0aee5 1592 /* Create RTL for function declaration. */
3d79abbd
PB
1593 rest_of_decl_compilation (fndecl, 1, 0);
1594 }
1595
f8d0aee5 1596 /* Create RTL for function definition. */
3d79abbd
PB
1597 make_decl_rtl (fndecl);
1598
3d79abbd
PB
1599 init_function_start (fndecl);
1600
1601 /* Even though we're inside a function body, we still don't want to
1602 call expand_expr to calculate the size of a variable-sized array.
1603 We haven't necessarily assigned RTL to all variables yet, so it's
1604 not safe to try to expand expressions involving them. */
e3b5732b 1605 cfun->dont_save_pending_sizes_p = 1;
3d79abbd 1606
f8d0aee5 1607 /* function.c requires a push at the start of the function. */
3d79abbd
PB
1608 pushlevel (0);
1609}
1610
1611/* Create thunks for alternate entry points. */
1612
1613static void
1614build_entry_thunks (gfc_namespace * ns)
1615{
1616 gfc_formal_arglist *formal;
1617 gfc_formal_arglist *thunk_formal;
1618 gfc_entry_list *el;
1619 gfc_symbol *thunk_sym;
1620 stmtblock_t body;
1621 tree thunk_fndecl;
1622 tree args;
1623 tree string_args;
1624 tree tmp;
c8cc8542 1625 locus old_loc;
3d79abbd
PB
1626
1627 /* This should always be a toplevel function. */
6e45f57b 1628 gcc_assert (current_function_decl == NULL_TREE);
3d79abbd 1629
c8cc8542 1630 gfc_get_backend_locus (&old_loc);
3d79abbd
PB
1631 for (el = ns->entries; el; el = el->next)
1632 {
1633 thunk_sym = el->sym;
1634
1635 build_function_decl (thunk_sym);
1636 create_function_arglist (thunk_sym);
1637
1638 trans_function_start (thunk_sym);
1639
1640 thunk_fndecl = thunk_sym->backend_decl;
1641
1642 gfc_start_block (&body);
1643
f8d0aee5 1644 /* Pass extra parameter identifying this entry point. */
7d60be94 1645 tmp = build_int_cst (gfc_array_index_type, el->id);
3d79abbd
PB
1646 args = tree_cons (NULL_TREE, tmp, NULL_TREE);
1647 string_args = NULL_TREE;
1648
d198b59a
JJ
1649 if (thunk_sym->attr.function)
1650 {
1651 if (gfc_return_by_reference (ns->proc_name))
1652 {
1653 tree ref = DECL_ARGUMENTS (current_function_decl);
1654 args = tree_cons (NULL_TREE, ref, args);
1655 if (ns->proc_name->ts.type == BT_CHARACTER)
1656 args = tree_cons (NULL_TREE, TREE_CHAIN (ref),
1657 args);
1658 }
1659 }
1660
3d79abbd
PB
1661 for (formal = ns->proc_name->formal; formal; formal = formal->next)
1662 {
d198b59a
JJ
1663 /* Ignore alternate returns. */
1664 if (formal->sym == NULL)
1665 continue;
1666
3d79abbd
PB
1667 /* We don't have a clever way of identifying arguments, so resort to
1668 a brute-force search. */
1669 for (thunk_formal = thunk_sym->formal;
1670 thunk_formal;
1671 thunk_formal = thunk_formal->next)
1672 {
1673 if (thunk_formal->sym == formal->sym)
1674 break;
1675 }
1676
1677 if (thunk_formal)
1678 {
1679 /* Pass the argument. */
3e978d30 1680 DECL_ARTIFICIAL (thunk_formal->sym->backend_decl) = 1;
3d79abbd
PB
1681 args = tree_cons (NULL_TREE, thunk_formal->sym->backend_decl,
1682 args);
1683 if (formal->sym->ts.type == BT_CHARACTER)
1684 {
1685 tmp = thunk_formal->sym->ts.cl->backend_decl;
1686 string_args = tree_cons (NULL_TREE, tmp, string_args);
1687 }
1688 }
1689 else
1690 {
1691 /* Pass NULL for a missing argument. */
1692 args = tree_cons (NULL_TREE, null_pointer_node, args);
1693 if (formal->sym->ts.type == BT_CHARACTER)
1694 {
c3238e32 1695 tmp = build_int_cst (gfc_charlen_type_node, 0);
3d79abbd
PB
1696 string_args = tree_cons (NULL_TREE, tmp, string_args);
1697 }
1698 }
1699 }
1700
1701 /* Call the master function. */
1702 args = nreverse (args);
1703 args = chainon (args, nreverse (string_args));
1704 tmp = ns->proc_name->backend_decl;
3380b802 1705 tmp = build_function_call_expr (tmp, args);
d198b59a
JJ
1706 if (ns->proc_name->attr.mixed_entry_master)
1707 {
1708 tree union_decl, field;
1709 tree master_type = TREE_TYPE (ns->proc_name->backend_decl);
1710
1711 union_decl = build_decl (VAR_DECL, get_identifier ("__result"),
1712 TREE_TYPE (master_type));
1713 DECL_ARTIFICIAL (union_decl) = 1;
1714 DECL_EXTERNAL (union_decl) = 0;
1715 TREE_PUBLIC (union_decl) = 0;
1716 TREE_USED (union_decl) = 1;
1717 layout_decl (union_decl, 0);
1718 pushdecl (union_decl);
1719
1720 DECL_CONTEXT (union_decl) = current_function_decl;
44855d8c
TS
1721 tmp = fold_build2 (MODIFY_EXPR, TREE_TYPE (union_decl),
1722 union_decl, tmp);
d198b59a
JJ
1723 gfc_add_expr_to_block (&body, tmp);
1724
1725 for (field = TYPE_FIELDS (TREE_TYPE (union_decl));
1726 field; field = TREE_CHAIN (field))
1727 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
1728 thunk_sym->result->name) == 0)
1729 break;
1730 gcc_assert (field != NULL_TREE);
44855d8c
TS
1731 tmp = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
1732 union_decl, field, NULL_TREE);
1733 tmp = fold_build2 (MODIFY_EXPR,
1734 TREE_TYPE (DECL_RESULT (current_function_decl)),
1735 DECL_RESULT (current_function_decl), tmp);
d198b59a
JJ
1736 tmp = build1_v (RETURN_EXPR, tmp);
1737 }
1738 else if (TREE_TYPE (DECL_RESULT (current_function_decl))
1739 != void_type_node)
1740 {
44855d8c
TS
1741 tmp = fold_build2 (MODIFY_EXPR,
1742 TREE_TYPE (DECL_RESULT (current_function_decl)),
1743 DECL_RESULT (current_function_decl), tmp);
d198b59a
JJ
1744 tmp = build1_v (RETURN_EXPR, tmp);
1745 }
3d79abbd
PB
1746 gfc_add_expr_to_block (&body, tmp);
1747
1748 /* Finish off this function and send it for code generation. */
1749 DECL_SAVED_TREE (thunk_fndecl) = gfc_finish_block (&body);
1750 poplevel (1, 0, 1);
1751 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl)) = thunk_fndecl;
1752
1753 /* Output the GENERIC tree. */
1754 dump_function (TDI_original, thunk_fndecl);
1755
1756 /* Store the end of the function, so that we get good line number
1757 info for the epilogue. */
1758 cfun->function_end_locus = input_location;
1759
1760 /* We're leaving the context of this function, so zap cfun.
1761 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
1762 tree_rest_of_compilation. */
db2960f4 1763 set_cfun (NULL);
3d79abbd
PB
1764
1765 current_function_decl = NULL_TREE;
1766
1767 gfc_gimplify_function (thunk_fndecl);
8f235343 1768 cgraph_finalize_function (thunk_fndecl, false);
3d79abbd
PB
1769
1770 /* We share the symbols in the formal argument list with other entry
1771 points and the master function. Clear them so that they are
1772 recreated for each function. */
1773 for (formal = thunk_sym->formal; formal; formal = formal->next)
d198b59a
JJ
1774 if (formal->sym != NULL) /* Ignore alternate returns. */
1775 {
1776 formal->sym->backend_decl = NULL_TREE;
1777 if (formal->sym->ts.type == BT_CHARACTER)
1778 formal->sym->ts.cl->backend_decl = NULL_TREE;
1779 }
1780
1781 if (thunk_sym->attr.function)
3d79abbd 1782 {
d198b59a
JJ
1783 if (thunk_sym->ts.type == BT_CHARACTER)
1784 thunk_sym->ts.cl->backend_decl = NULL_TREE;
1785 if (thunk_sym->result->ts.type == BT_CHARACTER)
1786 thunk_sym->result->ts.cl->backend_decl = NULL_TREE;
3d79abbd
PB
1787 }
1788 }
c8cc8542
PB
1789
1790 gfc_set_backend_locus (&old_loc);
3d79abbd
PB
1791}
1792
1793
1794/* Create a decl for a function, and create any thunks for alternate entry
1795 points. */
1796
1797void
1798gfc_create_function_decl (gfc_namespace * ns)
1799{
1800 /* Create a declaration for the master function. */
1801 build_function_decl (ns->proc_name);
1802
f8d0aee5 1803 /* Compile the entry thunks. */
3d79abbd
PB
1804 if (ns->entries)
1805 build_entry_thunks (ns);
1806
1807 /* Now create the read argument list. */
1808 create_function_arglist (ns->proc_name);
1809}
1810
5f20c93a 1811/* Return the decl used to hold the function return value. If
da4c6ed8 1812 parent_flag is set, the context is the parent_scope. */
6de9cd9a
DN
1813
1814tree
5f20c93a 1815gfc_get_fake_result_decl (gfc_symbol * sym, int parent_flag)
6de9cd9a 1816{
5f20c93a
PT
1817 tree decl;
1818 tree length;
1819 tree this_fake_result_decl;
1820 tree this_function_decl;
6de9cd9a
DN
1821
1822 char name[GFC_MAX_SYMBOL_LEN + 10];
1823
5f20c93a
PT
1824 if (parent_flag)
1825 {
1826 this_fake_result_decl = parent_fake_result_decl;
1827 this_function_decl = DECL_CONTEXT (current_function_decl);
1828 }
1829 else
1830 {
1831 this_fake_result_decl = current_fake_result_decl;
1832 this_function_decl = current_function_decl;
1833 }
1834
d198b59a 1835 if (sym
5f20c93a 1836 && sym->ns->proc_name->backend_decl == this_function_decl
417ab240 1837 && sym->ns->proc_name->attr.entry_master
d198b59a
JJ
1838 && sym != sym->ns->proc_name)
1839 {
417ab240 1840 tree t = NULL, var;
5f20c93a
PT
1841 if (this_fake_result_decl != NULL)
1842 for (t = TREE_CHAIN (this_fake_result_decl); t; t = TREE_CHAIN (t))
417ab240
JJ
1843 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t)), sym->name) == 0)
1844 break;
1845 if (t)
1846 return TREE_VALUE (t);
5f20c93a
PT
1847 decl = gfc_get_fake_result_decl (sym->ns->proc_name, parent_flag);
1848
1849 if (parent_flag)
1850 this_fake_result_decl = parent_fake_result_decl;
1851 else
1852 this_fake_result_decl = current_fake_result_decl;
1853
417ab240 1854 if (decl && sym->ns->proc_name->attr.mixed_entry_master)
d198b59a
JJ
1855 {
1856 tree field;
1857
1858 for (field = TYPE_FIELDS (TREE_TYPE (decl));
1859 field; field = TREE_CHAIN (field))
1860 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
1861 sym->name) == 0)
1862 break;
1863
1864 gcc_assert (field != NULL_TREE);
44855d8c
TS
1865 decl = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
1866 decl, field, NULL_TREE);
d198b59a 1867 }
5f20c93a
PT
1868
1869 var = create_tmp_var_raw (TREE_TYPE (decl), sym->name);
1870 if (parent_flag)
1871 gfc_add_decl_to_parent_function (var);
1872 else
1873 gfc_add_decl_to_function (var);
1874
417ab240
JJ
1875 SET_DECL_VALUE_EXPR (var, decl);
1876 DECL_HAS_VALUE_EXPR_P (var) = 1;
4b8ae4db 1877 GFC_DECL_RESULT (var) = 1;
5f20c93a
PT
1878
1879 TREE_CHAIN (this_fake_result_decl)
1880 = tree_cons (get_identifier (sym->name), var,
1881 TREE_CHAIN (this_fake_result_decl));
417ab240 1882 return var;
d198b59a
JJ
1883 }
1884
5f20c93a
PT
1885 if (this_fake_result_decl != NULL_TREE)
1886 return TREE_VALUE (this_fake_result_decl);
6de9cd9a
DN
1887
1888 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
1889 sym is NULL. */
1890 if (!sym)
1891 return NULL_TREE;
1892
417ab240 1893 if (sym->ts.type == BT_CHARACTER)
6de9cd9a 1894 {
417ab240
JJ
1895 if (sym->ts.cl->backend_decl == NULL_TREE)
1896 length = gfc_create_string_length (sym);
1897 else
1898 length = sym->ts.cl->backend_decl;
1899 if (TREE_CODE (length) == VAR_DECL
1900 && DECL_CONTEXT (length) == NULL_TREE)
a7d6b765 1901 gfc_add_decl_to_function (length);
6de9cd9a
DN
1902 }
1903
1904 if (gfc_return_by_reference (sym))
1905 {
5f20c93a 1906 decl = DECL_ARGUMENTS (this_function_decl);
d198b59a 1907
5f20c93a 1908 if (sym->ns->proc_name->backend_decl == this_function_decl
d198b59a
JJ
1909 && sym->ns->proc_name->attr.entry_master)
1910 decl = TREE_CHAIN (decl);
6de9cd9a
DN
1911
1912 TREE_USED (decl) = 1;
1913 if (sym->as)
1914 decl = gfc_build_dummy_array_decl (sym, decl);
1915 }
1916 else
1917 {
1918 sprintf (name, "__result_%.20s",
5f20c93a 1919 IDENTIFIER_POINTER (DECL_NAME (this_function_decl)));
6de9cd9a 1920
da4c6ed8
TS
1921 if (!sym->attr.mixed_entry_master && sym->attr.function)
1922 decl = build_decl (VAR_DECL, get_identifier (name),
1923 gfc_sym_type (sym));
1924 else
1925 decl = build_decl (VAR_DECL, get_identifier (name),
1926 TREE_TYPE (TREE_TYPE (this_function_decl)));
6de9cd9a
DN
1927 DECL_ARTIFICIAL (decl) = 1;
1928 DECL_EXTERNAL (decl) = 0;
1929 TREE_PUBLIC (decl) = 0;
1930 TREE_USED (decl) = 1;
6c7a4dfd 1931 GFC_DECL_RESULT (decl) = 1;
c55cebda 1932 TREE_ADDRESSABLE (decl) = 1;
6de9cd9a
DN
1933
1934 layout_decl (decl, 0);
1935
5f20c93a
PT
1936 if (parent_flag)
1937 gfc_add_decl_to_parent_function (decl);
1938 else
1939 gfc_add_decl_to_function (decl);
6de9cd9a
DN
1940 }
1941
5f20c93a
PT
1942 if (parent_flag)
1943 parent_fake_result_decl = build_tree_list (NULL, decl);
1944 else
1945 current_fake_result_decl = build_tree_list (NULL, decl);
6de9cd9a
DN
1946
1947 return decl;
1948}
1949
1950
1951/* Builds a function decl. The remaining parameters are the types of the
1952 function arguments. Negative nargs indicates a varargs function. */
1953
1954tree
1955gfc_build_library_function_decl (tree name, tree rettype, int nargs, ...)
1956{
1957 tree arglist;
1958 tree argtype;
1959 tree fntype;
1960 tree fndecl;
1961 va_list p;
1962 int n;
1963
1964 /* Library functions must be declared with global scope. */
6e45f57b 1965 gcc_assert (current_function_decl == NULL_TREE);
6de9cd9a
DN
1966
1967 va_start (p, nargs);
1968
1969
1970 /* Create a list of the argument types. */
1971 for (arglist = NULL_TREE, n = abs (nargs); n > 0; n--)
1972 {
1973 argtype = va_arg (p, tree);
1974 arglist = gfc_chainon_list (arglist, argtype);
1975 }
1976
1977 if (nargs >= 0)
1978 {
1979 /* Terminate the list. */
1980 arglist = gfc_chainon_list (arglist, void_type_node);
1981 }
1982
1983 /* Build the function type and decl. */
1984 fntype = build_function_type (rettype, arglist);
1985 fndecl = build_decl (FUNCTION_DECL, name, fntype);
1986
1987 /* Mark this decl as external. */
1988 DECL_EXTERNAL (fndecl) = 1;
1989 TREE_PUBLIC (fndecl) = 1;
1990
1991 va_end (p);
1992
1993 pushdecl (fndecl);
1994
0e6df31e 1995 rest_of_decl_compilation (fndecl, 1, 0);
6de9cd9a
DN
1996
1997 return fndecl;
1998}
1999
2000static void
2001gfc_build_intrinsic_function_decls (void)
2002{
e2cad04b
RH
2003 tree gfc_int4_type_node = gfc_get_int_type (4);
2004 tree gfc_int8_type_node = gfc_get_int_type (8);
644cb69f 2005 tree gfc_int16_type_node = gfc_get_int_type (16);
e2cad04b 2006 tree gfc_logical4_type_node = gfc_get_logical_type (4);
e2cad04b 2007
6de9cd9a 2008 /* String functions. */
6de9cd9a
DN
2009 gfor_fndecl_compare_string =
2010 gfc_build_library_function_decl (get_identifier (PREFIX("compare_string")),
c9ff1de3 2011 integer_type_node, 4,
d7177ab2
TS
2012 gfc_charlen_type_node, pchar_type_node,
2013 gfc_charlen_type_node, pchar_type_node);
6de9cd9a
DN
2014
2015 gfor_fndecl_concat_string =
2016 gfc_build_library_function_decl (get_identifier (PREFIX("concat_string")),
2017 void_type_node,
2018 6,
d7177ab2
TS
2019 gfc_charlen_type_node, pchar_type_node,
2020 gfc_charlen_type_node, pchar_type_node,
2021 gfc_charlen_type_node, pchar_type_node);
6de9cd9a
DN
2022
2023 gfor_fndecl_string_len_trim =
2024 gfc_build_library_function_decl (get_identifier (PREFIX("string_len_trim")),
2025 gfc_int4_type_node,
d7177ab2 2026 2, gfc_charlen_type_node,
6de9cd9a
DN
2027 pchar_type_node);
2028
2029 gfor_fndecl_string_index =
2030 gfc_build_library_function_decl (get_identifier (PREFIX("string_index")),
2031 gfc_int4_type_node,
d7177ab2
TS
2032 5, gfc_charlen_type_node, pchar_type_node,
2033 gfc_charlen_type_node, pchar_type_node,
6de9cd9a
DN
2034 gfc_logical4_type_node);
2035
2036 gfor_fndecl_string_scan =
2037 gfc_build_library_function_decl (get_identifier (PREFIX("string_scan")),
2038 gfc_int4_type_node,
d7177ab2
TS
2039 5, gfc_charlen_type_node, pchar_type_node,
2040 gfc_charlen_type_node, pchar_type_node,
6de9cd9a
DN
2041 gfc_logical4_type_node);
2042
2043 gfor_fndecl_string_verify =
2044 gfc_build_library_function_decl (get_identifier (PREFIX("string_verify")),
2045 gfc_int4_type_node,
d7177ab2
TS
2046 5, gfc_charlen_type_node, pchar_type_node,
2047 gfc_charlen_type_node, pchar_type_node,
6de9cd9a
DN
2048 gfc_logical4_type_node);
2049
7389bce6 2050 gfor_fndecl_string_trim =
6de9cd9a
DN
2051 gfc_build_library_function_decl (get_identifier (PREFIX("string_trim")),
2052 void_type_node,
2053 4,
d7177ab2 2054 build_pointer_type (gfc_charlen_type_node),
6de9cd9a 2055 ppvoid_type_node,
d7177ab2 2056 gfc_charlen_type_node,
6de9cd9a
DN
2057 pchar_type_node);
2058
2263c775
FXC
2059 gfor_fndecl_string_minmax =
2060 gfc_build_library_function_decl (get_identifier (PREFIX("string_minmax")),
2061 void_type_node, -4,
2062 build_pointer_type (gfc_charlen_type_node),
2063 ppvoid_type_node, integer_type_node,
2064 integer_type_node);
2065
25fc05eb
FXC
2066 gfor_fndecl_ttynam =
2067 gfc_build_library_function_decl (get_identifier (PREFIX("ttynam")),
2068 void_type_node,
2069 3,
2070 pchar_type_node,
2071 gfc_charlen_type_node,
dd52ecb0 2072 integer_type_node);
25fc05eb 2073
35059811
FXC
2074 gfor_fndecl_fdate =
2075 gfc_build_library_function_decl (get_identifier (PREFIX("fdate")),
2076 void_type_node,
2077 2,
2078 pchar_type_node,
2079 gfc_charlen_type_node);
2080
2081 gfor_fndecl_ctime =
2082 gfc_build_library_function_decl (get_identifier (PREFIX("ctime")),
2083 void_type_node,
2084 3,
2085 pchar_type_node,
2086 gfc_charlen_type_node,
2087 gfc_int8_type_node);
2088
6de9cd9a
DN
2089 gfor_fndecl_adjustl =
2090 gfc_build_library_function_decl (get_identifier (PREFIX("adjustl")),
2091 void_type_node,
2092 3,
2093 pchar_type_node,
d7177ab2 2094 gfc_charlen_type_node, pchar_type_node);
6de9cd9a
DN
2095
2096 gfor_fndecl_adjustr =
2097 gfc_build_library_function_decl (get_identifier (PREFIX("adjustr")),
2098 void_type_node,
2099 3,
2100 pchar_type_node,
d7177ab2 2101 gfc_charlen_type_node, pchar_type_node);
6de9cd9a 2102
a39fafac
FXC
2103 gfor_fndecl_sc_kind =
2104 gfc_build_library_function_decl (get_identifier
2105 (PREFIX("selected_char_kind")),
2106 gfc_int4_type_node, 2,
2107 gfc_charlen_type_node, pchar_type_node);
2108
6de9cd9a 2109 gfor_fndecl_si_kind =
e6472bce
FXC
2110 gfc_build_library_function_decl (get_identifier
2111 (PREFIX("selected_int_kind")),
a39fafac 2112 gfc_int4_type_node, 1, pvoid_type_node);
6de9cd9a
DN
2113
2114 gfor_fndecl_sr_kind =
7389bce6 2115 gfc_build_library_function_decl (get_identifier
e6472bce 2116 (PREFIX("selected_real_kind")),
a39fafac
FXC
2117 gfc_int4_type_node, 2,
2118 pvoid_type_node, pvoid_type_node);
6de9cd9a 2119
6de9cd9a 2120 /* Power functions. */
5b200ac2 2121 {
644cb69f
FXC
2122 tree ctype, rtype, itype, jtype;
2123 int rkind, ikind, jkind;
2124#define NIKINDS 3
2125#define NRKINDS 4
2126 static int ikinds[NIKINDS] = {4, 8, 16};
2127 static int rkinds[NRKINDS] = {4, 8, 10, 16};
2128 char name[PREFIX_LEN + 12]; /* _gfortran_pow_?n_?n */
2129
2130 for (ikind=0; ikind < NIKINDS; ikind++)
5b200ac2 2131 {
644cb69f
FXC
2132 itype = gfc_get_int_type (ikinds[ikind]);
2133
2134 for (jkind=0; jkind < NIKINDS; jkind++)
2135 {
2136 jtype = gfc_get_int_type (ikinds[jkind]);
2137 if (itype && jtype)
2138 {
2139 sprintf(name, PREFIX("pow_i%d_i%d"), ikinds[ikind],
2140 ikinds[jkind]);
2141 gfor_fndecl_math_powi[jkind][ikind].integer =
2142 gfc_build_library_function_decl (get_identifier (name),
2143 jtype, 2, jtype, itype);
67fdae36 2144 TREE_READONLY (gfor_fndecl_math_powi[jkind][ikind].integer) = 1;
644cb69f
FXC
2145 }
2146 }
2147
2148 for (rkind = 0; rkind < NRKINDS; rkind ++)
5b200ac2 2149 {
644cb69f
FXC
2150 rtype = gfc_get_real_type (rkinds[rkind]);
2151 if (rtype && itype)
2152 {
2153 sprintf(name, PREFIX("pow_r%d_i%d"), rkinds[rkind],
2154 ikinds[ikind]);
2155 gfor_fndecl_math_powi[rkind][ikind].real =
2156 gfc_build_library_function_decl (get_identifier (name),
2157 rtype, 2, rtype, itype);
67fdae36 2158 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].real) = 1;
644cb69f
FXC
2159 }
2160
2161 ctype = gfc_get_complex_type (rkinds[rkind]);
2162 if (ctype && itype)
2163 {
2164 sprintf(name, PREFIX("pow_c%d_i%d"), rkinds[rkind],
2165 ikinds[ikind]);
2166 gfor_fndecl_math_powi[rkind][ikind].cmplx =
2167 gfc_build_library_function_decl (get_identifier (name),
2168 ctype, 2,ctype, itype);
67fdae36 2169 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1;
644cb69f 2170 }
5b200ac2
FW
2171 }
2172 }
644cb69f
FXC
2173#undef NIKINDS
2174#undef NRKINDS
5b200ac2
FW
2175 }
2176
6de9cd9a
DN
2177 gfor_fndecl_math_ishftc4 =
2178 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc4")),
2179 gfc_int4_type_node,
2180 3, gfc_int4_type_node,
2181 gfc_int4_type_node, gfc_int4_type_node);
2182 gfor_fndecl_math_ishftc8 =
2183 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc8")),
2184 gfc_int8_type_node,
2185 3, gfc_int8_type_node,
644cb69f
FXC
2186 gfc_int4_type_node, gfc_int4_type_node);
2187 if (gfc_int16_type_node)
2188 gfor_fndecl_math_ishftc16 =
2189 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc16")),
2190 gfc_int16_type_node, 3,
2191 gfc_int16_type_node,
2192 gfc_int4_type_node,
2193 gfc_int4_type_node);
2194
5a0aad31
FXC
2195 /* BLAS functions. */
2196 {
dd52ecb0 2197 tree pint = build_pointer_type (integer_type_node);
5a0aad31
FXC
2198 tree ps = build_pointer_type (gfc_get_real_type (gfc_default_real_kind));
2199 tree pd = build_pointer_type (gfc_get_real_type (gfc_default_double_kind));
2200 tree pc = build_pointer_type (gfc_get_complex_type (gfc_default_real_kind));
2201 tree pz = build_pointer_type
2202 (gfc_get_complex_type (gfc_default_double_kind));
2203
2204 gfor_fndecl_sgemm = gfc_build_library_function_decl
2205 (get_identifier
2206 (gfc_option.flag_underscoring ? "sgemm_"
2207 : "sgemm"),
2208 void_type_node, 15, pchar_type_node,
2209 pchar_type_node, pint, pint, pint, ps, ps, pint,
dd52ecb0
JB
2210 ps, pint, ps, ps, pint, integer_type_node,
2211 integer_type_node);
5a0aad31
FXC
2212 gfor_fndecl_dgemm = gfc_build_library_function_decl
2213 (get_identifier
2214 (gfc_option.flag_underscoring ? "dgemm_"
2215 : "dgemm"),
2216 void_type_node, 15, pchar_type_node,
2217 pchar_type_node, pint, pint, pint, pd, pd, pint,
dd52ecb0
JB
2218 pd, pint, pd, pd, pint, integer_type_node,
2219 integer_type_node);
5a0aad31
FXC
2220 gfor_fndecl_cgemm = gfc_build_library_function_decl
2221 (get_identifier
2222 (gfc_option.flag_underscoring ? "cgemm_"
2223 : "cgemm"),
2224 void_type_node, 15, pchar_type_node,
2225 pchar_type_node, pint, pint, pint, pc, pc, pint,
dd52ecb0
JB
2226 pc, pint, pc, pc, pint, integer_type_node,
2227 integer_type_node);
5a0aad31
FXC
2228 gfor_fndecl_zgemm = gfc_build_library_function_decl
2229 (get_identifier
2230 (gfc_option.flag_underscoring ? "zgemm_"
2231 : "zgemm"),
2232 void_type_node, 15, pchar_type_node,
2233 pchar_type_node, pint, pint, pint, pz, pz, pint,
dd52ecb0
JB
2234 pz, pint, pz, pz, pint, integer_type_node,
2235 integer_type_node);
5a0aad31
FXC
2236 }
2237
6de9cd9a
DN
2238 /* Other functions. */
2239 gfor_fndecl_size0 =
2240 gfc_build_library_function_decl (get_identifier (PREFIX("size0")),
2241 gfc_array_index_type,
2242 1, pvoid_type_node);
2243 gfor_fndecl_size1 =
2244 gfc_build_library_function_decl (get_identifier (PREFIX("size1")),
2245 gfc_array_index_type,
2246 2, pvoid_type_node,
2247 gfc_array_index_type);
b41b2534
JB
2248
2249 gfor_fndecl_iargc =
2250 gfc_build_library_function_decl (get_identifier (PREFIX ("iargc")),
2251 gfc_int4_type_node,
2252 0);
6de9cd9a
DN
2253}
2254
2255
2256/* Make prototypes for runtime library functions. */
2257
2258void
2259gfc_build_builtin_function_decls (void)
2260{
e2cad04b 2261 tree gfc_int4_type_node = gfc_get_int_type (4);
6de9cd9a
DN
2262
2263 gfor_fndecl_stop_numeric =
2264 gfc_build_library_function_decl (get_identifier (PREFIX("stop_numeric")),
2265 void_type_node, 1, gfc_int4_type_node);
eed61baa
TK
2266 /* Stop doesn't return. */
2267 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric) = 1;
2268
6de9cd9a
DN
2269 gfor_fndecl_stop_string =
2270 gfc_build_library_function_decl (get_identifier (PREFIX("stop_string")),
2271 void_type_node, 2, pchar_type_node,
2272 gfc_int4_type_node);
eed61baa
TK
2273 /* Stop doesn't return. */
2274 TREE_THIS_VOLATILE (gfor_fndecl_stop_string) = 1;
6de9cd9a
DN
2275
2276 gfor_fndecl_pause_numeric =
2277 gfc_build_library_function_decl (get_identifier (PREFIX("pause_numeric")),
2278 void_type_node, 1, gfc_int4_type_node);
2279
2280 gfor_fndecl_pause_string =
2281 gfc_build_library_function_decl (get_identifier (PREFIX("pause_string")),
2282 void_type_node, 2, pchar_type_node,
2283 gfc_int4_type_node);
2284
2285 gfor_fndecl_select_string =
2286 gfc_build_library_function_decl (get_identifier (PREFIX("select_string")),
7e4b97c7
TB
2287 integer_type_node, 4, pvoid_type_node,
2288 integer_type_node, pchar_type_node,
2289 integer_type_node);
6de9cd9a
DN
2290
2291 gfor_fndecl_runtime_error =
2292 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error")),
c8fe94c7 2293 void_type_node, -1, pchar_type_node);
16275f18
SB
2294 /* The runtime_error function does not return. */
2295 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error) = 1;
6de9cd9a 2296
f96d606f
JD
2297 gfor_fndecl_runtime_error_at =
2298 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error_at")),
c8fe94c7 2299 void_type_node, -2, pchar_type_node,
f96d606f
JD
2300 pchar_type_node);
2301 /* The runtime_error_at function does not return. */
2302 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at) = 1;
2303
2304 gfor_fndecl_generate_error =
2305 gfc_build_library_function_decl (get_identifier (PREFIX("generate_error")),
2306 void_type_node, 3, pvoid_type_node,
dd52ecb0 2307 integer_type_node, pchar_type_node);
f96d606f 2308
1529b8d9
FXC
2309 gfor_fndecl_os_error =
2310 gfc_build_library_function_decl (get_identifier (PREFIX("os_error")),
2311 void_type_node, 1, pchar_type_node);
2312 /* The runtime_error function does not return. */
2313 TREE_THIS_VOLATILE (gfor_fndecl_os_error) = 1;
2314
944b8b35
FXC
2315 gfor_fndecl_set_fpe =
2316 gfc_build_library_function_decl (get_identifier (PREFIX("set_fpe")),
dd52ecb0 2317 void_type_node, 1, integer_type_node);
944b8b35 2318
68d2e027
JD
2319 /* Keep the array dimension in sync with the call, later in this file. */
2320 gfor_fndecl_set_options =
2321 gfc_build_library_function_decl (get_identifier (PREFIX("set_options")),
dd52ecb0 2322 void_type_node, 2, integer_type_node,
68d2e027 2323 pvoid_type_node);
8b67b708 2324
eaa90d25
TK
2325 gfor_fndecl_set_convert =
2326 gfc_build_library_function_decl (get_identifier (PREFIX("set_convert")),
dd52ecb0 2327 void_type_node, 1, integer_type_node);
eaa90d25 2328
d67ab5ee
TK
2329 gfor_fndecl_set_record_marker =
2330 gfc_build_library_function_decl (get_identifier (PREFIX("set_record_marker")),
dd52ecb0 2331 void_type_node, 1, integer_type_node);
d67ab5ee 2332
07b3bbf2
TK
2333 gfor_fndecl_set_max_subrecord_length =
2334 gfc_build_library_function_decl (get_identifier (PREFIX("set_max_subrecord_length")),
dd52ecb0 2335 void_type_node, 1, integer_type_node);
07b3bbf2 2336
6de9cd9a
DN
2337 gfor_fndecl_in_pack = gfc_build_library_function_decl (
2338 get_identifier (PREFIX("internal_pack")),
2339 pvoid_type_node, 1, pvoid_type_node);
2340
2341 gfor_fndecl_in_unpack = gfc_build_library_function_decl (
2342 get_identifier (PREFIX("internal_unpack")),
7e4b97c7 2343 void_type_node, 2, pvoid_type_node, pvoid_type_node);
6de9cd9a
DN
2344
2345 gfor_fndecl_associated =
2346 gfc_build_library_function_decl (
2347 get_identifier (PREFIX("associated")),
8a09ef91 2348 integer_type_node, 2, ppvoid_type_node,
6de9cd9a
DN
2349 ppvoid_type_node);
2350
2351 gfc_build_intrinsic_function_decls ();
2352 gfc_build_intrinsic_lib_fndecls ();
2353 gfc_build_io_library_fndecls ();
2354}
2355
2356
1f2959f0 2357/* Evaluate the length of dummy character variables. */
6de9cd9a
DN
2358
2359static tree
417ab240 2360gfc_trans_dummy_character (gfc_symbol *sym, gfc_charlen *cl, tree fnbody)
6de9cd9a
DN
2361{
2362 stmtblock_t body;
2363
faf28b3a 2364 gfc_finish_decl (cl->backend_decl);
6de9cd9a
DN
2365
2366 gfc_start_block (&body);
2367
2368 /* Evaluate the string length expression. */
07368af0 2369 gfc_conv_string_length (cl, &body);
417ab240
JJ
2370
2371 gfc_trans_vla_type_sizes (sym, &body);
2372
6de9cd9a
DN
2373 gfc_add_expr_to_block (&body, fnbody);
2374 return gfc_finish_block (&body);
2375}
2376
2377
2378/* Allocate and cleanup an automatic character variable. */
2379
2380static tree
2381gfc_trans_auto_character_variable (gfc_symbol * sym, tree fnbody)
2382{
2383 stmtblock_t body;
2384 tree decl;
6de9cd9a
DN
2385 tree tmp;
2386
6e45f57b
PB
2387 gcc_assert (sym->backend_decl);
2388 gcc_assert (sym->ts.cl && sym->ts.cl->length);
6de9cd9a
DN
2389
2390 gfc_start_block (&body);
2391
2392 /* Evaluate the string length expression. */
07368af0 2393 gfc_conv_string_length (sym->ts.cl, &body);
6de9cd9a 2394
417ab240
JJ
2395 gfc_trans_vla_type_sizes (sym, &body);
2396
6de9cd9a
DN
2397 decl = sym->backend_decl;
2398
1a186ec5 2399 /* Emit a DECL_EXPR for this variable, which will cause the
4ab2db93 2400 gimplifier to allocate storage, and all that good stuff. */
44855d8c 2401 tmp = fold_build1 (DECL_EXPR, TREE_TYPE (decl), decl);
6de9cd9a 2402 gfc_add_expr_to_block (&body, tmp);
1a186ec5 2403
6de9cd9a
DN
2404 gfc_add_expr_to_block (&body, fnbody);
2405 return gfc_finish_block (&body);
2406}
2407
910450c1
FW
2408/* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
2409
2410static tree
2411gfc_trans_assign_aux_var (gfc_symbol * sym, tree fnbody)
2412{
2413 stmtblock_t body;
2414
2415 gcc_assert (sym->backend_decl);
2416 gfc_start_block (&body);
2417
2418 /* Set the initial value to length. See the comments in
2419 function gfc_add_assign_aux_vars in this file. */
2420 gfc_add_modify_expr (&body, GFC_DECL_STRING_LEN (sym->backend_decl),
2421 build_int_cst (NULL_TREE, -2));
2422
2423 gfc_add_expr_to_block (&body, fnbody);
2424 return gfc_finish_block (&body);
2425}
2426
417ab240
JJ
2427static void
2428gfc_trans_vla_one_sizepos (tree *tp, stmtblock_t *body)
2429{
2430 tree t = *tp, var, val;
2431
2432 if (t == NULL || t == error_mark_node)
2433 return;
2434 if (TREE_CONSTANT (t) || DECL_P (t))
2435 return;
2436
2437 if (TREE_CODE (t) == SAVE_EXPR)
2438 {
2439 if (SAVE_EXPR_RESOLVED_P (t))
2440 {
2441 *tp = TREE_OPERAND (t, 0);
2442 return;
2443 }
2444 val = TREE_OPERAND (t, 0);
2445 }
2446 else
2447 val = t;
2448
2449 var = gfc_create_var_np (TREE_TYPE (t), NULL);
2450 gfc_add_decl_to_function (var);
2451 gfc_add_modify_expr (body, var, val);
2452 if (TREE_CODE (t) == SAVE_EXPR)
2453 TREE_OPERAND (t, 0) = var;
2454 *tp = var;
2455}
2456
2457static void
2458gfc_trans_vla_type_sizes_1 (tree type, stmtblock_t *body)
2459{
2460 tree t;
2461
2462 if (type == NULL || type == error_mark_node)
2463 return;
2464
2465 type = TYPE_MAIN_VARIANT (type);
2466
2467 if (TREE_CODE (type) == INTEGER_TYPE)
2468 {
2469 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type), body);
2470 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type), body);
2471
2472 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2473 {
2474 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
2475 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
2476 }
2477 }
2478 else if (TREE_CODE (type) == ARRAY_TYPE)
2479 {
2480 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type), body);
2481 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type), body);
2482 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type), body);
2483 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type), body);
2484
2485 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2486 {
2487 TYPE_SIZE (t) = TYPE_SIZE (type);
2488 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
2489 }
2490 }
2491}
2492
2493/* Make sure all type sizes and array domains are either constant,
2494 or variable or parameter decls. This is a simplified variant
2495 of gimplify_type_sizes, but we can't use it here, as none of the
2496 variables in the expressions have been gimplified yet.
2497 As type sizes and domains for various variable length arrays
2498 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
2499 time, without this routine gimplify_type_sizes in the middle-end
2500 could result in the type sizes being gimplified earlier than where
2501 those variables are initialized. */
2502
2503void
2504gfc_trans_vla_type_sizes (gfc_symbol *sym, stmtblock_t *body)
2505{
2506 tree type = TREE_TYPE (sym->backend_decl);
2507
2508 if (TREE_CODE (type) == FUNCTION_TYPE
2509 && (sym->attr.function || sym->attr.result || sym->attr.entry))
2510 {
2511 if (! current_fake_result_decl)
2512 return;
2513
2514 type = TREE_TYPE (TREE_VALUE (current_fake_result_decl));
2515 }
2516
2517 while (POINTER_TYPE_P (type))
2518 type = TREE_TYPE (type);
2519
2520 if (GFC_DESCRIPTOR_TYPE_P (type))
2521 {
2522 tree etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
2523
2524 while (POINTER_TYPE_P (etype))
2525 etype = TREE_TYPE (etype);
2526
2527 gfc_trans_vla_type_sizes_1 (etype, body);
2528 }
2529
2530 gfc_trans_vla_type_sizes_1 (type, body);
2531}
2532
6de9cd9a 2533
b7b184a8
PT
2534/* Initialize a derived type by building an lvalue from the symbol
2535 and using trans_assignment to do the work. */
f40eccb0
PT
2536tree
2537gfc_init_default_dt (gfc_symbol * sym, tree body)
d3837072
PT
2538{
2539 stmtblock_t fnblock;
b7b184a8 2540 gfc_expr *e;
d3837072
PT
2541 tree tmp;
2542 tree present;
2543
2544 gfc_init_block (&fnblock);
b7b184a8
PT
2545 gcc_assert (!sym->attr.allocatable);
2546 gfc_set_sym_referenced (sym);
2547 e = gfc_lval_expr_from_sym (sym);
2548 tmp = gfc_trans_assignment (e, sym->value, false);
2549 if (sym->attr.dummy)
d3837072 2550 {
b7b184a8
PT
2551 present = gfc_conv_expr_present (sym);
2552 tmp = build3 (COND_EXPR, TREE_TYPE (tmp), present,
2553 tmp, build_empty_stmt ());
d3837072 2554 }
b7b184a8
PT
2555 gfc_add_expr_to_block (&fnblock, tmp);
2556 gfc_free_expr (e);
f40eccb0
PT
2557 if (body)
2558 gfc_add_expr_to_block (&fnblock, body);
d3837072
PT
2559 return gfc_finish_block (&fnblock);
2560}
2561
2562
b7b184a8
PT
2563/* Initialize INTENT(OUT) derived type dummies. */
2564static tree
2565init_intent_out_dt (gfc_symbol * proc_sym, tree body)
2566{
2567 stmtblock_t fnblock;
2568 gfc_formal_arglist *f;
2569
2570 gfc_init_block (&fnblock);
2571 for (f = proc_sym->formal; f; f = f->next)
2572 if (f->sym && f->sym->attr.intent == INTENT_OUT
2573 && f->sym->ts.type == BT_DERIVED
2574 && !f->sym->ts.derived->attr.alloc_comp
2575 && f->sym->value)
f40eccb0 2576 body = gfc_init_default_dt (f->sym, body);
b7b184a8
PT
2577
2578 gfc_add_expr_to_block (&fnblock, body);
2579 return gfc_finish_block (&fnblock);
2580}
2581
d3837072 2582
6de9cd9a
DN
2583/* Generate function entry and exit code, and add it to the function body.
2584 This includes:
f8d0aee5 2585 Allocation and initialization of array variables.
6de9cd9a 2586 Allocation of character string variables.
910450c1
FW
2587 Initialization and possibly repacking of dummy arrays.
2588 Initialization of ASSIGN statement auxiliary variable. */
6de9cd9a
DN
2589
2590static tree
2591gfc_trans_deferred_vars (gfc_symbol * proc_sym, tree fnbody)
2592{
2593 locus loc;
2594 gfc_symbol *sym;
417ab240
JJ
2595 gfc_formal_arglist *f;
2596 stmtblock_t body;
7114edca 2597 bool seen_trans_deferred_array = false;
6de9cd9a
DN
2598
2599 /* Deal with implicit return variables. Explicit return variables will
2600 already have been added. */
2601 if (gfc_return_by_reference (proc_sym) && proc_sym->result == proc_sym)
2602 {
2603 if (!current_fake_result_decl)
2604 {
d198b59a
JJ
2605 gfc_entry_list *el = NULL;
2606 if (proc_sym->attr.entry_master)
2607 {
2608 for (el = proc_sym->ns->entries; el; el = el->next)
2609 if (el->sym != el->sym->result)
2610 break;
2611 }
766d0c8c
DF
2612 /* TODO: move to the appropriate place in resolve.c. */
2613 if (warn_return_type && el == NULL)
2614 gfc_warning ("Return value of function '%s' at %L not set",
2615 proc_sym->name, &proc_sym->declared_at);
6de9cd9a 2616 }
d198b59a 2617 else if (proc_sym->as)
6de9cd9a 2618 {
417ab240
JJ
2619 tree result = TREE_VALUE (current_fake_result_decl);
2620 fnbody = gfc_trans_dummy_array_bias (proc_sym, result, fnbody);
f5f701ad
PT
2621
2622 /* An automatic character length, pointer array result. */
2623 if (proc_sym->ts.type == BT_CHARACTER
2624 && TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL)
2625 fnbody = gfc_trans_dummy_character (proc_sym, proc_sym->ts.cl,
2626 fnbody);
6de9cd9a
DN
2627 }
2628 else if (proc_sym->ts.type == BT_CHARACTER)
2629 {
2630 if (TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL)
417ab240
JJ
2631 fnbody = gfc_trans_dummy_character (proc_sym, proc_sym->ts.cl,
2632 fnbody);
6de9cd9a
DN
2633 }
2634 else
973ff4c0
TS
2635 gcc_assert (gfc_option.flag_f2c
2636 && proc_sym->ts.type == BT_COMPLEX);
6de9cd9a
DN
2637 }
2638
d3837072
PT
2639 /* Initialize the INTENT(OUT) derived type dummy arguments. This
2640 should be done here so that the offsets and lbounds of arrays
2641 are available. */
2642 fnbody = init_intent_out_dt (proc_sym, fnbody);
2643
6de9cd9a
DN
2644 for (sym = proc_sym->tlink; sym != proc_sym; sym = sym->tlink)
2645 {
5046aff5
PT
2646 bool sym_has_alloc_comp = (sym->ts.type == BT_DERIVED)
2647 && sym->ts.derived->attr.alloc_comp;
6de9cd9a
DN
2648 if (sym->attr.dimension)
2649 {
2650 switch (sym->as->type)
2651 {
2652 case AS_EXPLICIT:
2653 if (sym->attr.dummy || sym->attr.result)
2654 fnbody =
2655 gfc_trans_dummy_array_bias (sym, sym->backend_decl, fnbody);
2656 else if (sym->attr.pointer || sym->attr.allocatable)
2657 {
2658 if (TREE_STATIC (sym->backend_decl))
2659 gfc_trans_static_array_pointer (sym);
2660 else
7114edca
PT
2661 {
2662 seen_trans_deferred_array = true;
2663 fnbody = gfc_trans_deferred_array (sym, fnbody);
2664 }
6de9cd9a
DN
2665 }
2666 else
2667 {
7114edca
PT
2668 if (sym_has_alloc_comp)
2669 {
2670 seen_trans_deferred_array = true;
2671 fnbody = gfc_trans_deferred_array (sym, fnbody);
2672 }
b7b184a8
PT
2673 else if (sym->ts.type == BT_DERIVED
2674 && sym->value
2675 && !sym->attr.data
2676 && sym->attr.save == SAVE_NONE)
f40eccb0 2677 fnbody = gfc_init_default_dt (sym, fnbody);
7114edca 2678
6de9cd9a
DN
2679 gfc_get_backend_locus (&loc);
2680 gfc_set_backend_locus (&sym->declared_at);
2681 fnbody = gfc_trans_auto_array_allocation (sym->backend_decl,
2682 sym, fnbody);
2683 gfc_set_backend_locus (&loc);
2684 }
2685 break;
2686
2687 case AS_ASSUMED_SIZE:
2688 /* Must be a dummy parameter. */
6e45f57b 2689 gcc_assert (sym->attr.dummy);
6de9cd9a
DN
2690
2691 /* We should always pass assumed size arrays the g77 way. */
6de9cd9a
DN
2692 fnbody = gfc_trans_g77_array (sym, fnbody);
2693 break;
2694
2695 case AS_ASSUMED_SHAPE:
2696 /* Must be a dummy parameter. */
6e45f57b 2697 gcc_assert (sym->attr.dummy);
6de9cd9a
DN
2698
2699 fnbody = gfc_trans_dummy_array_bias (sym, sym->backend_decl,
2700 fnbody);
2701 break;
2702
2703 case AS_DEFERRED:
7114edca
PT
2704 seen_trans_deferred_array = true;
2705 fnbody = gfc_trans_deferred_array (sym, fnbody);
6de9cd9a
DN
2706 break;
2707
2708 default:
6e45f57b 2709 gcc_unreachable ();
6de9cd9a 2710 }
7114edca 2711 if (sym_has_alloc_comp && !seen_trans_deferred_array)
5046aff5 2712 fnbody = gfc_trans_deferred_array (sym, fnbody);
6de9cd9a 2713 }
5046aff5
PT
2714 else if (sym_has_alloc_comp)
2715 fnbody = gfc_trans_deferred_array (sym, fnbody);
6de9cd9a
DN
2716 else if (sym->ts.type == BT_CHARACTER)
2717 {
2718 gfc_get_backend_locus (&loc);
2719 gfc_set_backend_locus (&sym->declared_at);
2720 if (sym->attr.dummy || sym->attr.result)
417ab240 2721 fnbody = gfc_trans_dummy_character (sym, sym->ts.cl, fnbody);
6de9cd9a
DN
2722 else
2723 fnbody = gfc_trans_auto_character_variable (sym, fnbody);
2724 gfc_set_backend_locus (&loc);
2725 }
910450c1
FW
2726 else if (sym->attr.assign)
2727 {
2728 gfc_get_backend_locus (&loc);
2729 gfc_set_backend_locus (&sym->declared_at);
2730 fnbody = gfc_trans_assign_aux_var (sym, fnbody);
2731 gfc_set_backend_locus (&loc);
2732 }
b7b184a8
PT
2733 else if (sym->ts.type == BT_DERIVED
2734 && sym->value
2735 && !sym->attr.data
2736 && sym->attr.save == SAVE_NONE)
f40eccb0 2737 fnbody = gfc_init_default_dt (sym, fnbody);
6de9cd9a 2738 else
6e45f57b 2739 gcc_unreachable ();
6de9cd9a
DN
2740 }
2741
417ab240
JJ
2742 gfc_init_block (&body);
2743
2744 for (f = proc_sym->formal; f; f = f->next)
08113c73
PT
2745 {
2746 if (f->sym && f->sym->tlink == NULL && f->sym->ts.type == BT_CHARACTER)
2747 {
2748 gcc_assert (f->sym->ts.cl->backend_decl != NULL);
2749 if (TREE_CODE (f->sym->ts.cl->backend_decl) == PARM_DECL)
2750 gfc_trans_vla_type_sizes (f->sym, &body);
2751 }
08113c73 2752 }
417ab240
JJ
2753
2754 if (gfc_return_by_reference (proc_sym) && proc_sym->ts.type == BT_CHARACTER
2755 && current_fake_result_decl != NULL)
2756 {
2757 gcc_assert (proc_sym->ts.cl->backend_decl != NULL);
2758 if (TREE_CODE (proc_sym->ts.cl->backend_decl) == PARM_DECL)
2759 gfc_trans_vla_type_sizes (proc_sym, &body);
2760 }
2761
2762 gfc_add_expr_to_block (&body, fnbody);
2763 return gfc_finish_block (&body);
6de9cd9a
DN
2764}
2765
2766
2767/* Output an initialized decl for a module variable. */
2768
2769static void
2770gfc_create_module_variable (gfc_symbol * sym)
2771{
2772 tree decl;
6de9cd9a 2773
1a492601
PT
2774 /* Module functions with alternate entries are dealt with later and
2775 would get caught by the next condition. */
2776 if (sym->attr.entry)
2777 return;
2778
a8b3b0b6
CR
2779 /* Make sure we convert the types of the derived types from iso_c_binding
2780 into (void *). */
2781 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
2782 && sym->ts.type == BT_DERIVED)
2783 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
2784
fdc55763
PT
2785 /* Only output variables and array valued, or derived type,
2786 parameters. */
6de9cd9a 2787 if (sym->attr.flavor != FL_VARIABLE
fdc55763
PT
2788 && !(sym->attr.flavor == FL_PARAMETER
2789 && (sym->attr.dimension || sym->ts.type == BT_DERIVED)))
6de9cd9a
DN
2790 return;
2791
9cbf8b41
TS
2792 /* Don't generate variables from other modules. Variables from
2793 COMMONs will already have been generated. */
2794 if (sym->attr.use_assoc || sym->attr.in_common)
6de9cd9a
DN
2795 return;
2796
30aabb86 2797 /* Equivalenced variables arrive here after creation. */
b95605fb
PT
2798 if (sym->backend_decl
2799 && (sym->equiv_built || sym->attr.in_equivalence))
30aabb86
PT
2800 return;
2801
6de9cd9a
DN
2802 if (sym->backend_decl)
2803 internal_error ("backend decl for module variable %s already exists",
2804 sym->name);
2805
2806 /* We always want module variables to be created. */
2807 sym->attr.referenced = 1;
2808 /* Create the decl. */
2809 decl = gfc_get_symbol_decl (sym);
2810
6de9cd9a
DN
2811 /* Create the variable. */
2812 pushdecl (decl);
0e6df31e 2813 rest_of_decl_compilation (decl, 1, 0);
6de9cd9a
DN
2814
2815 /* Also add length of strings. */
2816 if (sym->ts.type == BT_CHARACTER)
2817 {
2818 tree length;
2819
2820 length = sym->ts.cl->backend_decl;
2821 if (!INTEGER_CST_P (length))
2822 {
2823 pushdecl (length);
0e6df31e 2824 rest_of_decl_compilation (length, 1, 0);
6de9cd9a
DN
2825 }
2826 }
2827}
2828
2829
2830/* Generate all the required code for module variables. */
2831
2832void
2833gfc_generate_module_vars (gfc_namespace * ns)
2834{
2835 module_namespace = ns;
2836
472ca416 2837 /* Check if the frontend left the namespace in a reasonable state. */
6e45f57b 2838 gcc_assert (ns->proc_name && !ns->proc_name->tlink);
6de9cd9a 2839
9cbf8b41
TS
2840 /* Generate COMMON blocks. */
2841 gfc_trans_common (ns);
2842
472ca416 2843 /* Create decls for all the module variables. */
6de9cd9a
DN
2844 gfc_traverse_ns (ns, gfc_create_module_variable);
2845}
2846
2847static void
2848gfc_generate_contained_functions (gfc_namespace * parent)
2849{
2850 gfc_namespace *ns;
2851
2852 /* We create all the prototypes before generating any code. */
2853 for (ns = parent->contained; ns; ns = ns->sibling)
2854 {
2855 /* Skip namespaces from used modules. */
2856 if (ns->parent != parent)
2857 continue;
2858
3d79abbd 2859 gfc_create_function_decl (ns);
6de9cd9a
DN
2860 }
2861
2862 for (ns = parent->contained; ns; ns = ns->sibling)
2863 {
2864 /* Skip namespaces from used modules. */
2865 if (ns->parent != parent)
2866 continue;
2867
2868 gfc_generate_function_code (ns);
2869 }
2870}
2871
2872
3e978d30
PT
2873/* Drill down through expressions for the array specification bounds and
2874 character length calling generate_local_decl for all those variables
2875 that have not already been declared. */
2876
2877static void
2878generate_local_decl (gfc_symbol *);
2879
908a2235 2880/* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
3e978d30 2881
908a2235
PT
2882static bool
2883expr_decls (gfc_expr *e, gfc_symbol *sym,
2884 int *f ATTRIBUTE_UNUSED)
2885{
2886 if (e->expr_type != EXPR_VARIABLE
2887 || sym == e->symtree->n.sym
3e978d30
PT
2888 || e->symtree->n.sym->mark
2889 || e->symtree->n.sym->ns != sym->ns)
908a2235 2890 return false;
3e978d30 2891
908a2235
PT
2892 generate_local_decl (e->symtree->n.sym);
2893 return false;
2894}
3e978d30 2895
908a2235
PT
2896static void
2897generate_expr_decls (gfc_symbol *sym, gfc_expr *e)
2898{
2899 gfc_traverse_expr (e, sym, expr_decls, 0);
3e978d30
PT
2900}
2901
2902
66e4ab31 2903/* Check for dependencies in the character length and array spec. */
3e978d30
PT
2904
2905static void
2906generate_dependency_declarations (gfc_symbol *sym)
2907{
2908 int i;
2909
2910 if (sym->ts.type == BT_CHARACTER
10650fbb
TB
2911 && sym->ts.cl
2912 && sym->ts.cl->length
2913 && sym->ts.cl->length->expr_type != EXPR_CONSTANT)
3e978d30
PT
2914 generate_expr_decls (sym, sym->ts.cl->length);
2915
2916 if (sym->as && sym->as->rank)
2917 {
2918 for (i = 0; i < sym->as->rank; i++)
2919 {
2920 generate_expr_decls (sym, sym->as->lower[i]);
2921 generate_expr_decls (sym, sym->as->upper[i]);
2922 }
2923 }
2924}
2925
2926
6de9cd9a
DN
2927/* Generate decls for all local variables. We do this to ensure correct
2928 handling of expressions which only appear in the specification of
2929 other functions. */
2930
2931static void
2932generate_local_decl (gfc_symbol * sym)
2933{
2934 if (sym->attr.flavor == FL_VARIABLE)
2935 {
3e978d30
PT
2936 /* Check for dependencies in the array specification and string
2937 length, adding the necessary declarations to the function. We
2938 mark the symbol now, as well as in traverse_ns, to prevent
2939 getting stuck in a circular dependency. */
2940 sym->mark = 1;
2941 if (!sym->attr.dummy && !sym->ns->proc_name->attr.entry_master)
2942 generate_dependency_declarations (sym);
2943
6de9cd9a
DN
2944 if (sym->attr.referenced)
2945 gfc_get_symbol_decl (sym);
534fd534
DF
2946 /* INTENT(out) dummy arguments are likely meant to be set. */
2947 else if (warn_unused_variable
2948 && sym->attr.dummy
2949 && sym->attr.intent == INTENT_OUT)
d92693b4 2950 gfc_warning ("Dummy argument '%s' at %L was declared INTENT(OUT) but was not set",
534fd534
DF
2951 sym->name, &sym->declared_at);
2952 /* Specific warning for unused dummy arguments. */
2953 else if (warn_unused_variable && sym->attr.dummy)
d92693b4 2954 gfc_warning ("Unused dummy argument '%s' at %L", sym->name,
80f2bb6e 2955 &sym->declared_at);
f8d0aee5 2956 /* Warn for unused variables, but not if they're inside a common
19e4c59d 2957 block or are use-associated. */
ce738b86 2958 else if (warn_unused_variable
766d0c8c 2959 && !(sym->attr.in_common || sym->attr.use_assoc || sym->mark))
d92693b4 2960 gfc_warning ("Unused variable '%s' declared at %L", sym->name,
80f2bb6e 2961 &sym->declared_at);
417ab240
JJ
2962 /* For variable length CHARACTER parameters, the PARM_DECL already
2963 references the length variable, so force gfc_get_symbol_decl
2964 even when not referenced. If optimize > 0, it will be optimized
2965 away anyway. But do this only after emitting -Wunused-parameter
2966 warning if requested. */
2967 if (sym->attr.dummy && ! sym->attr.referenced
2968 && sym->ts.type == BT_CHARACTER
2969 && sym->ts.cl->backend_decl != NULL
2970 && TREE_CODE (sym->ts.cl->backend_decl) == VAR_DECL)
2971 {
2972 sym->attr.referenced = 1;
2973 gfc_get_symbol_decl (sym);
2974 }
534fd534
DF
2975
2976 /* We do not want the middle-end to warn about unused parameters
2977 as this was already done above. */
2978 if (sym->attr.dummy && sym->backend_decl != NULL_TREE)
2979 TREE_NO_WARNING(sym->backend_decl) = 1;
6de9cd9a 2980 }
33c0c5e9
DF
2981 else if (sym->attr.flavor == FL_PARAMETER)
2982 {
d92693b4 2983 if (warn_unused_parameter
33c0c5e9
DF
2984 && !sym->attr.referenced
2985 && !sym->attr.use_assoc)
d92693b4 2986 gfc_warning ("Unused parameter '%s' declared at %L", sym->name,
33c0c5e9
DF
2987 &sym->declared_at);
2988 }
766d0c8c
DF
2989 else if (sym->attr.flavor == FL_PROCEDURE)
2990 {
2991 /* TODO: move to the appropriate place in resolve.c. */
2992 if (warn_return_type
2993 && sym->attr.function
2994 && sym->result
2995 && sym != sym->result
2996 && !sym->result->attr.referenced
2997 && !sym->attr.use_assoc
2998 && sym->attr.if_source != IFSRC_IFBODY)
2999 {
3000 gfc_warning ("Return value '%s' of function '%s' declared at "
3001 "%L not set", sym->result->name, sym->name,
3002 &sym->result->declared_at);
3003
3004 /* Prevents "Unused variable" warning for RESULT variables. */
3005 sym->mark = sym->result->mark = 1;
3006 }
3007 }
a8b3b0b6 3008
8b16d231
CR
3009 if (sym->attr.dummy == 1)
3010 {
3011 /* Modify the tree type for scalar character dummy arguments of bind(c)
3012 procedures if they are passed by value. The tree type for them will
3013 be promoted to INTEGER_TYPE for the middle end, which appears to be
3014 what C would do with characters passed by-value. The value attribute
3015 implies the dummy is a scalar. */
3016 if (sym->attr.value == 1 && sym->backend_decl != NULL
3017 && sym->ts.type == BT_CHARACTER && sym->ts.is_c_interop
3018 && sym->ns->proc_name != NULL && sym->ns->proc_name->attr.is_bind_c)
e032c2a1 3019 gfc_conv_scalar_char_value (sym, NULL, NULL);
8b16d231
CR
3020 }
3021
a8b3b0b6
CR
3022 /* Make sure we convert the types of the derived types from iso_c_binding
3023 into (void *). */
3024 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
3025 && sym->ts.type == BT_DERIVED)
3026 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
6de9cd9a
DN
3027}
3028
3029static void
3030generate_local_vars (gfc_namespace * ns)
3031{
3032 gfc_traverse_ns (ns, generate_local_decl);
3033}
3034
3035
3d79abbd
PB
3036/* Generate a switch statement to jump to the correct entry point. Also
3037 creates the label decls for the entry points. */
6de9cd9a 3038
3d79abbd
PB
3039static tree
3040gfc_trans_entry_master_switch (gfc_entry_list * el)
6de9cd9a 3041{
3d79abbd
PB
3042 stmtblock_t block;
3043 tree label;
3044 tree tmp;
3045 tree val;
6de9cd9a 3046
3d79abbd
PB
3047 gfc_init_block (&block);
3048 for (; el; el = el->next)
3049 {
3050 /* Add the case label. */
c006df4e 3051 label = gfc_build_label_decl (NULL_TREE);
7d60be94 3052 val = build_int_cst (gfc_array_index_type, el->id);
923ab88c 3053 tmp = build3_v (CASE_LABEL_EXPR, val, NULL_TREE, label);
3d79abbd 3054 gfc_add_expr_to_block (&block, tmp);
7389bce6 3055
3d79abbd
PB
3056 /* And jump to the actual entry point. */
3057 label = gfc_build_label_decl (NULL_TREE);
3d79abbd
PB
3058 tmp = build1_v (GOTO_EXPR, label);
3059 gfc_add_expr_to_block (&block, tmp);
3060
3061 /* Save the label decl. */
3062 el->label = label;
3063 }
3064 tmp = gfc_finish_block (&block);
3065 /* The first argument selects the entry point. */
3066 val = DECL_ARGUMENTS (current_function_decl);
923ab88c 3067 tmp = build3_v (SWITCH_EXPR, val, tmp, NULL_TREE);
3d79abbd 3068 return tmp;
6de9cd9a
DN
3069}
3070
44de5aeb 3071
6de9cd9a
DN
3072/* Generate code for a function. */
3073
3074void
3075gfc_generate_function_code (gfc_namespace * ns)
3076{
3077 tree fndecl;
3078 tree old_context;
3079 tree decl;
3080 tree tmp;
5046aff5 3081 tree tmp2;
6de9cd9a
DN
3082 stmtblock_t block;
3083 stmtblock_t body;
3084 tree result;
3085 gfc_symbol *sym;
5046aff5 3086 int rank;
6de9cd9a
DN
3087
3088 sym = ns->proc_name;
3d79abbd 3089
6de9cd9a 3090 /* Check that the frontend isn't still using this. */
6e45f57b 3091 gcc_assert (sym->tlink == NULL);
6de9cd9a
DN
3092 sym->tlink = sym;
3093
3094 /* Create the declaration for functions with global scope. */
3095 if (!sym->backend_decl)
3d79abbd 3096 gfc_create_function_decl (ns);
6de9cd9a
DN
3097
3098 fndecl = sym->backend_decl;
3099 old_context = current_function_decl;
3100
3101 if (old_context)
3102 {
3103 push_function_context ();
3104 saved_parent_function_decls = saved_function_decls;
3105 saved_function_decls = NULL_TREE;
3106 }
3107
3d79abbd 3108 trans_function_start (sym);
6de9cd9a 3109
6de9cd9a
DN
3110 gfc_start_block (&block);
3111
d198b59a
JJ
3112 if (ns->entries && ns->proc_name->ts.type == BT_CHARACTER)
3113 {
3114 /* Copy length backend_decls to all entry point result
3115 symbols. */
3116 gfc_entry_list *el;
3117 tree backend_decl;
3118
3119 gfc_conv_const_charlen (ns->proc_name->ts.cl);
3120 backend_decl = ns->proc_name->result->ts.cl->backend_decl;
3121 for (el = ns->entries; el; el = el->next)
3122 el->sym->result->ts.cl->backend_decl = backend_decl;
3123 }
3124
6de9cd9a
DN
3125 /* Translate COMMON blocks. */
3126 gfc_trans_common (ns);
3127
5f20c93a
PT
3128 /* Null the parent fake result declaration if this namespace is
3129 a module function or an external procedures. */
3130 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
3131 || ns->parent == NULL)
3132 parent_fake_result_decl = NULL_TREE;
3133
30aabb86
PT
3134 gfc_generate_contained_functions (ns);
3135
6de9cd9a 3136 generate_local_vars (ns);
7389bce6 3137
5f20c93a
PT
3138 /* Keep the parent fake result declaration in module functions
3139 or external procedures. */
3140 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
3141 || ns->parent == NULL)
3142 current_fake_result_decl = parent_fake_result_decl;
3143 else
3144 current_fake_result_decl = NULL_TREE;
3145
6de9cd9a
DN
3146 current_function_return_label = NULL;
3147
3148 /* Now generate the code for the body of this function. */
3149 gfc_init_block (&body);
3150
68d2e027 3151 /* If this is the main program, add a call to set_options to set up the
5f8f5313 3152 runtime library Fortran language standard parameters. */
5f8f5313 3153 if (sym->attr.is_main_program)
8b67b708 3154 {
68d2e027
JD
3155 tree array_type, array, var;
3156
3157 /* Passing a new option to the library requires four modifications:
3158 + add it to the tree_cons list below
3159 + change the array size in the call to build_array_type
3160 + change the first argument to the library call
3161 gfor_fndecl_set_options
3162 + modify the library (runtime/compile_options.c)! */
3163 array = tree_cons (NULL_TREE,
dd52ecb0 3164 build_int_cst (integer_type_node,
68d2e027
JD
3165 gfc_option.warn_std), NULL_TREE);
3166 array = tree_cons (NULL_TREE,
dd52ecb0 3167 build_int_cst (integer_type_node,
68d2e027
JD
3168 gfc_option.allow_std), array);
3169 array = tree_cons (NULL_TREE,
dd52ecb0 3170 build_int_cst (integer_type_node, pedantic), array);
68d2e027 3171 array = tree_cons (NULL_TREE,
dd52ecb0 3172 build_int_cst (integer_type_node,
68d2e027
JD
3173 gfc_option.flag_dump_core), array);
3174 array = tree_cons (NULL_TREE,
dd52ecb0 3175 build_int_cst (integer_type_node,
68d2e027
JD
3176 gfc_option.flag_backtrace), array);
3177 array = tree_cons (NULL_TREE,
dd52ecb0 3178 build_int_cst (integer_type_node,
68d2e027
JD
3179 gfc_option.flag_sign_zero), array);
3180
18fe404f 3181 array = tree_cons (NULL_TREE,
dd52ecb0 3182 build_int_cst (integer_type_node,
18fe404f
TK
3183 flag_bounds_check), array);
3184
dd52ecb0 3185 array_type = build_array_type (integer_type_node,
68d2e027 3186 build_index_type (build_int_cst (NULL_TREE,
18fe404f 3187 6)));
68d2e027
JD
3188 array = build_constructor_from_list (array_type, nreverse (array));
3189 TREE_CONSTANT (array) = 1;
68d2e027
JD
3190 TREE_STATIC (array) = 1;
3191
3192 /* Create a static variable to hold the jump table. */
3193 var = gfc_create_var (array_type, "options");
3194 TREE_CONSTANT (var) = 1;
68d2e027
JD
3195 TREE_STATIC (var) = 1;
3196 TREE_READONLY (var) = 1;
3197 DECL_INITIAL (var) = array;
3198 var = gfc_build_addr_expr (pvoid_type_node, var);
3199
3200 tmp = build_call_expr (gfor_fndecl_set_options, 2,
dd52ecb0 3201 build_int_cst (integer_type_node, 7), var);
8b67b708
FXC
3202 gfc_add_expr_to_block (&body, tmp);
3203 }
3204
944b8b35
FXC
3205 /* If this is the main program and a -ffpe-trap option was provided,
3206 add a call to set_fpe so that the library will raise a FPE when
3207 needed. */
3208 if (sym->attr.is_main_program && gfc_option.fpe != 0)
3209 {
5039610b 3210 tmp = build_call_expr (gfor_fndecl_set_fpe, 1,
dd52ecb0 3211 build_int_cst (integer_type_node,
5039610b 3212 gfc_option.fpe));
944b8b35
FXC
3213 gfc_add_expr_to_block (&body, tmp);
3214 }
3215
eaa90d25
TK
3216 /* If this is the main program and an -fconvert option was provided,
3217 add a call to set_convert. */
3218
d74b97cc 3219 if (sym->attr.is_main_program && gfc_option.convert != GFC_CONVERT_NATIVE)
eaa90d25 3220 {
5039610b 3221 tmp = build_call_expr (gfor_fndecl_set_convert, 1,
dd52ecb0 3222 build_int_cst (integer_type_node,
5039610b 3223 gfc_option.convert));
eaa90d25
TK
3224 gfc_add_expr_to_block (&body, tmp);
3225 }
3226
d67ab5ee
TK
3227 /* If this is the main program and an -frecord-marker option was provided,
3228 add a call to set_record_marker. */
3229
3230 if (sym->attr.is_main_program && gfc_option.record_marker != 0)
3231 {
5039610b 3232 tmp = build_call_expr (gfor_fndecl_set_record_marker, 1,
dd52ecb0 3233 build_int_cst (integer_type_node,
5039610b 3234 gfc_option.record_marker));
d67ab5ee 3235 gfc_add_expr_to_block (&body, tmp);
d67ab5ee 3236 }
eaa90d25 3237
07b3bbf2
TK
3238 if (sym->attr.is_main_program && gfc_option.max_subrecord_length != 0)
3239 {
5039610b
SL
3240 tmp = build_call_expr (gfor_fndecl_set_max_subrecord_length,
3241 1,
dd52ecb0 3242 build_int_cst (integer_type_node,
5039610b 3243 gfc_option.max_subrecord_length));
07b3bbf2
TK
3244 gfc_add_expr_to_block (&body, tmp);
3245 }
3246
6de9cd9a
DN
3247 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
3248 && sym->attr.subroutine)
3249 {
3250 tree alternate_return;
5f20c93a 3251 alternate_return = gfc_get_fake_result_decl (sym, 0);
6de9cd9a
DN
3252 gfc_add_modify_expr (&body, alternate_return, integer_zero_node);
3253 }
3254
3d79abbd
PB
3255 if (ns->entries)
3256 {
3257 /* Jump to the correct entry point. */
3258 tmp = gfc_trans_entry_master_switch (ns->entries);
3259 gfc_add_expr_to_block (&body, tmp);
3260 }
3261
6de9cd9a
DN
3262 tmp = gfc_trans_code (ns->code);
3263 gfc_add_expr_to_block (&body, tmp);
3264
3265 /* Add a return label if needed. */
3266 if (current_function_return_label)
3267 {
3268 tmp = build1_v (LABEL_EXPR, current_function_return_label);
3269 gfc_add_expr_to_block (&body, tmp);
3270 }
3271
3272 tmp = gfc_finish_block (&body);
3273 /* Add code to create and cleanup arrays. */
3274 tmp = gfc_trans_deferred_vars (sym, tmp);
6de9cd9a
DN
3275
3276 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node)
3277 {
19e4c59d 3278 if (sym->attr.subroutine || sym == sym->result)
6de9cd9a 3279 {
417ab240
JJ
3280 if (current_fake_result_decl != NULL)
3281 result = TREE_VALUE (current_fake_result_decl);
3282 else
3283 result = NULL_TREE;
6de9cd9a
DN
3284 current_fake_result_decl = NULL_TREE;
3285 }
3286 else
3287 result = sym->result->backend_decl;
3288
5046aff5
PT
3289 if (result != NULL_TREE && sym->attr.function
3290 && sym->ts.type == BT_DERIVED
73865312
TB
3291 && sym->ts.derived->attr.alloc_comp
3292 && !sym->attr.pointer)
5046aff5
PT
3293 {
3294 rank = sym->as ? sym->as->rank : 0;
3295 tmp2 = gfc_nullify_alloc_comp (sym->ts.derived, result, rank);
3296 gfc_add_expr_to_block (&block, tmp2);
3297 }
3298
766d0c8c
DF
3299 gfc_add_expr_to_block (&block, tmp);
3300
3301 if (result == NULL_TREE)
3302 {
3303 /* TODO: move to the appropriate place in resolve.c. */
3304 if (warn_return_type && !sym->attr.referenced && sym == sym->result)
3305 gfc_warning ("Return value of function '%s' at %L not set",
3306 sym->name, &sym->declared_at);
5046aff5 3307
766d0c8c
DF
3308 TREE_NO_WARNING(sym->backend_decl) = 1;
3309 }
6de9cd9a
DN
3310 else
3311 {
da4c6ed8
TS
3312 /* Set the return value to the dummy result variable. The
3313 types may be different for scalar default REAL functions
3314 with -ff2c, therefore we have to convert. */
3315 tmp = convert (TREE_TYPE (DECL_RESULT (fndecl)), result);
44855d8c
TS
3316 tmp = fold_build2 (MODIFY_EXPR, TREE_TYPE (tmp),
3317 DECL_RESULT (fndecl), tmp);
923ab88c 3318 tmp = build1_v (RETURN_EXPR, tmp);
6de9cd9a
DN
3319 gfc_add_expr_to_block (&block, tmp);
3320 }
3321 }
5046aff5
PT
3322 else
3323 gfc_add_expr_to_block (&block, tmp);
3324
6de9cd9a
DN
3325
3326 /* Add all the decls we created during processing. */
3327 decl = saved_function_decls;
3328 while (decl)
3329 {
3330 tree next;
3331
3332 next = TREE_CHAIN (decl);
3333 TREE_CHAIN (decl) = NULL_TREE;
3334 pushdecl (decl);
3335 decl = next;
3336 }
3337 saved_function_decls = NULL_TREE;
3338
3339 DECL_SAVED_TREE (fndecl) = gfc_finish_block (&block);
3340
3341 /* Finish off this function and send it for code generation. */
3342 poplevel (1, 0, 1);
3343 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
3344
3345 /* Output the GENERIC tree. */
3346 dump_function (TDI_original, fndecl);
3347
3348 /* Store the end of the function, so that we get good line number
3349 info for the epilogue. */
3350 cfun->function_end_locus = input_location;
3351
3352 /* We're leaving the context of this function, so zap cfun.
3353 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
3354 tree_rest_of_compilation. */
db2960f4 3355 set_cfun (NULL);
6de9cd9a
DN
3356
3357 if (old_context)
3358 {
3359 pop_function_context ();
3360 saved_function_decls = saved_parent_function_decls;
3361 }
3362 current_function_decl = old_context;
3363
3364 if (decl_function_context (fndecl))
44de5aeb
RK
3365 /* Register this function with cgraph just far enough to get it
3366 added to our parent's nested function list. */
3367 (void) cgraph_node (fndecl);
6de9cd9a
DN
3368 else
3369 {
44de5aeb 3370 gfc_gimplify_function (fndecl);
8f235343 3371 cgraph_finalize_function (fndecl, false);
6de9cd9a
DN
3372 }
3373}
3374
6de9cd9a
DN
3375void
3376gfc_generate_constructors (void)
3377{
6e45f57b 3378 gcc_assert (gfc_static_ctors == NULL_TREE);
6de9cd9a
DN
3379#if 0
3380 tree fnname;
3381 tree type;
3382 tree fndecl;
3383 tree decl;
3384 tree tmp;
3385
3386 if (gfc_static_ctors == NULL_TREE)
3387 return;
3388
5880f14f 3389 fnname = get_file_function_name ("I");
6de9cd9a
DN
3390 type = build_function_type (void_type_node,
3391 gfc_chainon_list (NULL_TREE, void_type_node));
3392
3393 fndecl = build_decl (FUNCTION_DECL, fnname, type);
3394 TREE_PUBLIC (fndecl) = 1;
3395
3396 decl = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
b785f485
RH
3397 DECL_ARTIFICIAL (decl) = 1;
3398 DECL_IGNORED_P (decl) = 1;
6de9cd9a
DN
3399 DECL_CONTEXT (decl) = fndecl;
3400 DECL_RESULT (fndecl) = decl;
3401
3402 pushdecl (fndecl);
3403
3404 current_function_decl = fndecl;
3405
0e6df31e 3406 rest_of_decl_compilation (fndecl, 1, 0);
6de9cd9a 3407
0e6df31e 3408 make_decl_rtl (fndecl);
6de9cd9a 3409
c8cc8542 3410 init_function_start (fndecl);
6de9cd9a 3411
6de9cd9a
DN
3412 pushlevel (0);
3413
3414 for (; gfc_static_ctors; gfc_static_ctors = TREE_CHAIN (gfc_static_ctors))
3415 {
5039610b 3416 tmp = build_call_expr (TREE_VALUE (gfc_static_ctors), 0);
6de9cd9a
DN
3417 DECL_SAVED_TREE (fndecl) = build_stmt (EXPR_STMT, tmp);
3418 }
3419
3420 poplevel (1, 0, 1);
3421
3422 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
3423
3424 free_after_parsing (cfun);
3425 free_after_compilation (cfun);
3426
0f0377f6 3427 tree_rest_of_compilation (fndecl);
6de9cd9a
DN
3428
3429 current_function_decl = NULL_TREE;
3430#endif
3431}
3432
0de4325e
TS
3433/* Translates a BLOCK DATA program unit. This means emitting the
3434 commons contained therein plus their initializations. We also emit
3435 a globally visible symbol to make sure that each BLOCK DATA program
3436 unit remains unique. */
3437
3438void
3439gfc_generate_block_data (gfc_namespace * ns)
3440{
3441 tree decl;
3442 tree id;
3443
c8cc8542
PB
3444 /* Tell the backend the source location of the block data. */
3445 if (ns->proc_name)
3446 gfc_set_backend_locus (&ns->proc_name->declared_at);
3447 else
3448 gfc_set_backend_locus (&gfc_current_locus);
3449
3450 /* Process the DATA statements. */
0de4325e
TS
3451 gfc_trans_common (ns);
3452
c8cc8542
PB
3453 /* Create a global symbol with the mane of the block data. This is to
3454 generate linker errors if the same name is used twice. It is never
3455 really used. */
0de4325e
TS
3456 if (ns->proc_name)
3457 id = gfc_sym_mangled_function_id (ns->proc_name);
3458 else
3459 id = get_identifier ("__BLOCK_DATA__");
3460
3461 decl = build_decl (VAR_DECL, id, gfc_array_index_type);
3462 TREE_PUBLIC (decl) = 1;
3463 TREE_STATIC (decl) = 1;
3464
3465 pushdecl (decl);
3466 rest_of_decl_compilation (decl, 1, 0);
3467}
3468
83d890b9 3469
6de9cd9a 3470#include "gt-fortran-trans-decl.h"
This page took 1.631377 seconds and 5 git commands to generate.