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