1 /* Backend function setup
2 Copyright (C) 2002-2014 Free Software Foundation, Inc.
3 Contributed by Paul Brook
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* trans-decl.c -- Handling of backend function and variable decls, etc */
25 #include "coretypes.h"
28 #include "stringpool.h"
29 #include "stor-layout.h"
32 #include "tree-dump.h"
33 #include "gimple-expr.h" /* For create_tmp_var_raw. */
35 #include "diagnostic-core.h" /* For internal_error. */
36 #include "toplev.h" /* For announce_function. */
43 #include "pointer-set.h"
44 #include "constructor.h"
46 #include "trans-types.h"
47 #include "trans-array.h"
48 #include "trans-const.h"
49 /* Only for gfc_trans_code. Shouldn't need to include this. */
50 #include "trans-stmt.h"
52 #define MAX_LABEL_VALUE 99999
55 /* Holds the result of the function if no result variable specified. */
57 static GTY(()) tree current_fake_result_decl
;
58 static GTY(()) tree parent_fake_result_decl
;
61 /* Holds the variable DECLs for the current function. */
63 static GTY(()) tree saved_function_decls
;
64 static GTY(()) tree saved_parent_function_decls
;
66 static struct pointer_set_t
*nonlocal_dummy_decl_pset
;
67 static GTY(()) tree nonlocal_dummy_decls
;
69 /* Holds the variable DECLs that are locals. */
71 static GTY(()) tree saved_local_decls
;
73 /* The namespace of the module we're currently generating. Only used while
74 outputting decls for module variables. Do not rely on this being set. */
76 static gfc_namespace
*module_namespace
;
78 /* The currently processed procedure symbol. */
79 static gfc_symbol
* current_procedure_symbol
= NULL
;
82 /* With -fcoarray=lib: For generating the registering call
83 of static coarrays. */
84 static bool has_coarray_vars
;
85 static stmtblock_t caf_init_block
;
88 /* List of static constructor functions. */
90 tree gfc_static_ctors
;
93 /* Function declarations for builtin library functions. */
95 tree gfor_fndecl_pause_numeric
;
96 tree gfor_fndecl_pause_string
;
97 tree gfor_fndecl_stop_numeric
;
98 tree gfor_fndecl_stop_numeric_f08
;
99 tree gfor_fndecl_stop_string
;
100 tree gfor_fndecl_error_stop_numeric
;
101 tree gfor_fndecl_error_stop_string
;
102 tree gfor_fndecl_runtime_error
;
103 tree gfor_fndecl_runtime_error_at
;
104 tree gfor_fndecl_runtime_warning_at
;
105 tree gfor_fndecl_os_error
;
106 tree gfor_fndecl_generate_error
;
107 tree gfor_fndecl_set_args
;
108 tree gfor_fndecl_set_fpe
;
109 tree gfor_fndecl_set_options
;
110 tree gfor_fndecl_set_convert
;
111 tree gfor_fndecl_set_record_marker
;
112 tree gfor_fndecl_set_max_subrecord_length
;
113 tree gfor_fndecl_ctime
;
114 tree gfor_fndecl_fdate
;
115 tree gfor_fndecl_ttynam
;
116 tree gfor_fndecl_in_pack
;
117 tree gfor_fndecl_in_unpack
;
118 tree gfor_fndecl_associated
;
119 tree gfor_fndecl_system_clock4
;
120 tree gfor_fndecl_system_clock8
;
123 /* Coarray run-time library function decls. */
124 tree gfor_fndecl_caf_init
;
125 tree gfor_fndecl_caf_finalize
;
126 tree gfor_fndecl_caf_this_image
;
127 tree gfor_fndecl_caf_num_images
;
128 tree gfor_fndecl_caf_register
;
129 tree gfor_fndecl_caf_deregister
;
130 tree gfor_fndecl_caf_get
;
131 tree gfor_fndecl_caf_send
;
132 tree gfor_fndecl_caf_sendget
;
133 tree gfor_fndecl_caf_critical
;
134 tree gfor_fndecl_caf_end_critical
;
135 tree gfor_fndecl_caf_sync_all
;
136 tree gfor_fndecl_caf_sync_images
;
137 tree gfor_fndecl_caf_error_stop
;
138 tree gfor_fndecl_caf_error_stop_str
;
139 tree gfor_fndecl_co_max
;
140 tree gfor_fndecl_co_min
;
141 tree gfor_fndecl_co_sum
;
144 /* Math functions. Many other math functions are handled in
145 trans-intrinsic.c. */
147 gfc_powdecl_list gfor_fndecl_math_powi
[4][3];
148 tree gfor_fndecl_math_ishftc4
;
149 tree gfor_fndecl_math_ishftc8
;
150 tree gfor_fndecl_math_ishftc16
;
153 /* String functions. */
155 tree gfor_fndecl_compare_string
;
156 tree gfor_fndecl_concat_string
;
157 tree gfor_fndecl_string_len_trim
;
158 tree gfor_fndecl_string_index
;
159 tree gfor_fndecl_string_scan
;
160 tree gfor_fndecl_string_verify
;
161 tree gfor_fndecl_string_trim
;
162 tree gfor_fndecl_string_minmax
;
163 tree gfor_fndecl_adjustl
;
164 tree gfor_fndecl_adjustr
;
165 tree gfor_fndecl_select_string
;
166 tree gfor_fndecl_compare_string_char4
;
167 tree gfor_fndecl_concat_string_char4
;
168 tree gfor_fndecl_string_len_trim_char4
;
169 tree gfor_fndecl_string_index_char4
;
170 tree gfor_fndecl_string_scan_char4
;
171 tree gfor_fndecl_string_verify_char4
;
172 tree gfor_fndecl_string_trim_char4
;
173 tree gfor_fndecl_string_minmax_char4
;
174 tree gfor_fndecl_adjustl_char4
;
175 tree gfor_fndecl_adjustr_char4
;
176 tree gfor_fndecl_select_string_char4
;
179 /* Conversion between character kinds. */
180 tree gfor_fndecl_convert_char1_to_char4
;
181 tree gfor_fndecl_convert_char4_to_char1
;
184 /* Other misc. runtime library functions. */
185 tree gfor_fndecl_size0
;
186 tree gfor_fndecl_size1
;
187 tree gfor_fndecl_iargc
;
189 /* Intrinsic functions implemented in Fortran. */
190 tree gfor_fndecl_sc_kind
;
191 tree gfor_fndecl_si_kind
;
192 tree gfor_fndecl_sr_kind
;
194 /* BLAS gemm functions. */
195 tree gfor_fndecl_sgemm
;
196 tree gfor_fndecl_dgemm
;
197 tree gfor_fndecl_cgemm
;
198 tree gfor_fndecl_zgemm
;
202 gfc_add_decl_to_parent_function (tree decl
)
205 DECL_CONTEXT (decl
) = DECL_CONTEXT (current_function_decl
);
206 DECL_NONLOCAL (decl
) = 1;
207 DECL_CHAIN (decl
) = saved_parent_function_decls
;
208 saved_parent_function_decls
= decl
;
212 gfc_add_decl_to_function (tree decl
)
215 TREE_USED (decl
) = 1;
216 DECL_CONTEXT (decl
) = current_function_decl
;
217 DECL_CHAIN (decl
) = saved_function_decls
;
218 saved_function_decls
= decl
;
222 add_decl_as_local (tree decl
)
225 TREE_USED (decl
) = 1;
226 DECL_CONTEXT (decl
) = current_function_decl
;
227 DECL_CHAIN (decl
) = saved_local_decls
;
228 saved_local_decls
= decl
;
232 /* Build a backend label declaration. Set TREE_USED for named labels.
233 The context of the label is always the current_function_decl. All
234 labels are marked artificial. */
237 gfc_build_label_decl (tree label_id
)
239 /* 2^32 temporaries should be enough. */
240 static unsigned int tmp_num
= 1;
244 if (label_id
== NULL_TREE
)
246 /* Build an internal label name. */
247 ASM_FORMAT_PRIVATE_NAME (label_name
, "L", tmp_num
++);
248 label_id
= get_identifier (label_name
);
253 /* Build the LABEL_DECL node. Labels have no type. */
254 label_decl
= build_decl (input_location
,
255 LABEL_DECL
, label_id
, void_type_node
);
256 DECL_CONTEXT (label_decl
) = current_function_decl
;
257 DECL_MODE (label_decl
) = VOIDmode
;
259 /* We always define the label as used, even if the original source
260 file never references the label. We don't want all kinds of
261 spurious warnings for old-style Fortran code with too many
263 TREE_USED (label_decl
) = 1;
265 DECL_ARTIFICIAL (label_decl
) = 1;
270 /* Set the backend source location of a decl. */
273 gfc_set_decl_location (tree decl
, locus
* loc
)
275 DECL_SOURCE_LOCATION (decl
) = loc
->lb
->location
;
279 /* Return the backend label declaration for a given label structure,
280 or create it if it doesn't exist yet. */
283 gfc_get_label_decl (gfc_st_label
* lp
)
285 if (lp
->backend_decl
)
286 return lp
->backend_decl
;
289 char label_name
[GFC_MAX_SYMBOL_LEN
+ 1];
292 /* Validate the label declaration from the front end. */
293 gcc_assert (lp
!= NULL
&& lp
->value
<= MAX_LABEL_VALUE
);
295 /* Build a mangled name for the label. */
296 sprintf (label_name
, "__label_%.6d", lp
->value
);
298 /* Build the LABEL_DECL node. */
299 label_decl
= gfc_build_label_decl (get_identifier (label_name
));
301 /* Tell the debugger where the label came from. */
302 if (lp
->value
<= MAX_LABEL_VALUE
) /* An internal label. */
303 gfc_set_decl_location (label_decl
, &lp
->where
);
305 DECL_ARTIFICIAL (label_decl
) = 1;
307 /* Store the label in the label list and return the LABEL_DECL. */
308 lp
->backend_decl
= label_decl
;
314 /* Convert a gfc_symbol to an identifier of the same name. */
317 gfc_sym_identifier (gfc_symbol
* sym
)
319 if (sym
->attr
.is_main_program
&& strcmp (sym
->name
, "main") == 0)
320 return (get_identifier ("MAIN__"));
322 return (get_identifier (sym
->name
));
326 /* Construct mangled name from symbol name. */
329 gfc_sym_mangled_identifier (gfc_symbol
* sym
)
331 char name
[GFC_MAX_MANGLED_SYMBOL_LEN
+ 1];
333 /* Prevent the mangling of identifiers that have an assigned
334 binding label (mainly those that are bind(c)). */
335 if (sym
->attr
.is_bind_c
== 1 && sym
->binding_label
)
336 return get_identifier (sym
->binding_label
);
338 if (sym
->module
== NULL
)
339 return gfc_sym_identifier (sym
);
342 snprintf (name
, sizeof name
, "__%s_MOD_%s", sym
->module
, sym
->name
);
343 return get_identifier (name
);
348 /* Construct mangled function name from symbol name. */
351 gfc_sym_mangled_function_id (gfc_symbol
* sym
)
354 char name
[GFC_MAX_MANGLED_SYMBOL_LEN
+ 1];
356 /* It may be possible to simply use the binding label if it's
357 provided, and remove the other checks. Then we could use it
358 for other things if we wished. */
359 if ((sym
->attr
.is_bind_c
== 1 || sym
->attr
.is_iso_c
== 1) &&
361 /* use the binding label rather than the mangled name */
362 return get_identifier (sym
->binding_label
);
364 if (sym
->module
== NULL
|| sym
->attr
.proc
== PROC_EXTERNAL
365 || (sym
->module
!= NULL
&& (sym
->attr
.external
366 || sym
->attr
.if_source
== IFSRC_IFBODY
)))
368 /* Main program is mangled into MAIN__. */
369 if (sym
->attr
.is_main_program
)
370 return get_identifier ("MAIN__");
372 /* Intrinsic procedures are never mangled. */
373 if (sym
->attr
.proc
== PROC_INTRINSIC
)
374 return get_identifier (sym
->name
);
376 if (gfc_option
.flag_underscoring
)
378 has_underscore
= strchr (sym
->name
, '_') != 0;
379 if (gfc_option
.flag_second_underscore
&& has_underscore
)
380 snprintf (name
, sizeof name
, "%s__", sym
->name
);
382 snprintf (name
, sizeof name
, "%s_", sym
->name
);
383 return get_identifier (name
);
386 return get_identifier (sym
->name
);
390 snprintf (name
, sizeof name
, "__%s_MOD_%s", sym
->module
, sym
->name
);
391 return get_identifier (name
);
397 gfc_set_decl_assembler_name (tree decl
, tree name
)
399 tree target_mangled
= targetm
.mangle_decl_assembler_name (decl
, name
);
400 SET_DECL_ASSEMBLER_NAME (decl
, target_mangled
);
404 /* Returns true if a variable of specified size should go on the stack. */
407 gfc_can_put_var_on_stack (tree size
)
409 unsigned HOST_WIDE_INT low
;
411 if (!INTEGER_CST_P (size
))
414 if (gfc_option
.flag_max_stack_var_size
< 0)
417 if (!tree_fits_uhwi_p (size
))
420 low
= TREE_INT_CST_LOW (size
);
421 if (low
> (unsigned HOST_WIDE_INT
) gfc_option
.flag_max_stack_var_size
)
424 /* TODO: Set a per-function stack size limit. */
430 /* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
431 an expression involving its corresponding pointer. There are
432 2 cases; one for variable size arrays, and one for everything else,
433 because variable-sized arrays require one fewer level of
437 gfc_finish_cray_pointee (tree decl
, gfc_symbol
*sym
)
439 tree ptr_decl
= gfc_get_symbol_decl (sym
->cp_pointer
);
442 /* Parameters need to be dereferenced. */
443 if (sym
->cp_pointer
->attr
.dummy
)
444 ptr_decl
= build_fold_indirect_ref_loc (input_location
,
447 /* Check to see if we're dealing with a variable-sized array. */
448 if (sym
->attr
.dimension
449 && TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
)
451 /* These decls will be dereferenced later, so we don't dereference
453 value
= convert (TREE_TYPE (decl
), ptr_decl
);
457 ptr_decl
= convert (build_pointer_type (TREE_TYPE (decl
)),
459 value
= build_fold_indirect_ref_loc (input_location
,
463 SET_DECL_VALUE_EXPR (decl
, value
);
464 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
465 GFC_DECL_CRAY_POINTEE (decl
) = 1;
469 /* Finish processing of a declaration without an initial value. */
472 gfc_finish_decl (tree decl
)
474 gcc_assert (TREE_CODE (decl
) == PARM_DECL
475 || DECL_INITIAL (decl
) == NULL_TREE
);
477 if (TREE_CODE (decl
) != VAR_DECL
)
480 if (DECL_SIZE (decl
) == NULL_TREE
481 && TYPE_SIZE (TREE_TYPE (decl
)) != NULL_TREE
)
482 layout_decl (decl
, 0);
484 /* A few consistency checks. */
485 /* A static variable with an incomplete type is an error if it is
486 initialized. Also if it is not file scope. Otherwise, let it
487 through, but if it is not `extern' then it may cause an error
489 /* An automatic variable with an incomplete type is an error. */
491 /* We should know the storage size. */
492 gcc_assert (DECL_SIZE (decl
) != NULL_TREE
493 || (TREE_STATIC (decl
)
494 ? (!DECL_INITIAL (decl
) || !DECL_CONTEXT (decl
))
495 : DECL_EXTERNAL (decl
)));
497 /* The storage size should be constant. */
498 gcc_assert ((!DECL_EXTERNAL (decl
) && !TREE_STATIC (decl
))
500 || TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
);
504 /* Handle setting of GFC_DECL_SCALAR* on DECL. */
507 gfc_finish_decl_attrs (tree decl
, symbol_attribute
*attr
)
509 if (!attr
->dimension
&& !attr
->codimension
)
511 /* Handle scalar allocatable variables. */
512 if (attr
->allocatable
)
514 gfc_allocate_lang_decl (decl
);
515 GFC_DECL_SCALAR_ALLOCATABLE (decl
) = 1;
517 /* Handle scalar pointer variables. */
520 gfc_allocate_lang_decl (decl
);
521 GFC_DECL_SCALAR_POINTER (decl
) = 1;
527 /* Apply symbol attributes to a variable, and add it to the function scope. */
530 gfc_finish_var_decl (tree decl
, gfc_symbol
* sym
)
533 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
534 This is the equivalent of the TARGET variables.
535 We also need to set this if the variable is passed by reference in a
538 /* Set DECL_VALUE_EXPR for Cray Pointees. */
539 if (sym
->attr
.cray_pointee
)
540 gfc_finish_cray_pointee (decl
, sym
);
542 if (sym
->attr
.target
)
543 TREE_ADDRESSABLE (decl
) = 1;
544 /* If it wasn't used we wouldn't be getting it. */
545 TREE_USED (decl
) = 1;
547 if (sym
->attr
.flavor
== FL_PARAMETER
548 && (sym
->attr
.dimension
|| sym
->ts
.type
== BT_DERIVED
))
549 TREE_READONLY (decl
) = 1;
551 /* Chain this decl to the pending declarations. Don't do pushdecl()
552 because this would add them to the current scope rather than the
554 if (current_function_decl
!= NULL_TREE
)
556 if (sym
->ns
->proc_name
->backend_decl
== current_function_decl
557 || sym
->result
== sym
)
558 gfc_add_decl_to_function (decl
);
559 else if (sym
->ns
->proc_name
->attr
.flavor
== FL_LABEL
)
560 /* This is a BLOCK construct. */
561 add_decl_as_local (decl
);
563 gfc_add_decl_to_parent_function (decl
);
566 if (sym
->attr
.cray_pointee
)
569 if(sym
->attr
.is_bind_c
== 1 && sym
->binding_label
)
571 /* We need to put variables that are bind(c) into the common
572 segment of the object file, because this is what C would do.
573 gfortran would typically put them in either the BSS or
574 initialized data segments, and only mark them as common if
575 they were part of common blocks. However, if they are not put
576 into common space, then C cannot initialize global Fortran
577 variables that it interoperates with and the draft says that
578 either Fortran or C should be able to initialize it (but not
579 both, of course.) (J3/04-007, section 15.3). */
580 TREE_PUBLIC(decl
) = 1;
581 DECL_COMMON(decl
) = 1;
584 /* If a variable is USE associated, it's always external. */
585 if (sym
->attr
.use_assoc
)
587 DECL_EXTERNAL (decl
) = 1;
588 TREE_PUBLIC (decl
) = 1;
590 else if (sym
->module
&& !sym
->attr
.result
&& !sym
->attr
.dummy
)
592 /* TODO: Don't set sym->module for result or dummy variables. */
593 gcc_assert (current_function_decl
== NULL_TREE
|| sym
->result
== sym
);
595 if (sym
->attr
.access
!= ACCESS_PRIVATE
|| sym
->attr
.public_used
)
596 TREE_PUBLIC (decl
) = 1;
597 TREE_STATIC (decl
) = 1;
600 /* Derived types are a bit peculiar because of the possibility of
601 a default initializer; this must be applied each time the variable
602 comes into scope it therefore need not be static. These variables
603 are SAVE_NONE but have an initializer. Otherwise explicitly
604 initialized variables are SAVE_IMPLICIT and explicitly saved are
606 if (!sym
->attr
.use_assoc
607 && (sym
->attr
.save
!= SAVE_NONE
|| sym
->attr
.data
608 || (sym
->value
&& sym
->ns
->proc_name
->attr
.is_main_program
)
609 || (gfc_option
.coarray
== GFC_FCOARRAY_LIB
610 && sym
->attr
.codimension
&& !sym
->attr
.allocatable
)))
611 TREE_STATIC (decl
) = 1;
613 if (sym
->attr
.volatile_
)
615 TREE_THIS_VOLATILE (decl
) = 1;
616 TREE_SIDE_EFFECTS (decl
) = 1;
617 new_type
= build_qualified_type (TREE_TYPE (decl
), TYPE_QUAL_VOLATILE
);
618 TREE_TYPE (decl
) = new_type
;
621 /* Keep variables larger than max-stack-var-size off stack. */
622 if (!sym
->ns
->proc_name
->attr
.recursive
623 && INTEGER_CST_P (DECL_SIZE_UNIT (decl
))
624 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl
))
625 /* Put variable length auto array pointers always into stack. */
626 && (TREE_CODE (TREE_TYPE (decl
)) != POINTER_TYPE
627 || sym
->attr
.dimension
== 0
628 || sym
->as
->type
!= AS_EXPLICIT
630 || sym
->attr
.allocatable
)
631 && !DECL_ARTIFICIAL (decl
))
632 TREE_STATIC (decl
) = 1;
634 /* Handle threadprivate variables. */
635 if (sym
->attr
.threadprivate
636 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
637 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
639 gfc_finish_decl_attrs (decl
, &sym
->attr
);
643 /* Allocate the lang-specific part of a decl. */
646 gfc_allocate_lang_decl (tree decl
)
648 if (DECL_LANG_SPECIFIC (decl
) == NULL
)
649 DECL_LANG_SPECIFIC (decl
) = ggc_cleared_alloc
<struct lang_decl
> ();
652 /* Remember a symbol to generate initialization/cleanup code at function
656 gfc_defer_symbol_init (gfc_symbol
* sym
)
662 /* Don't add a symbol twice. */
666 last
= head
= sym
->ns
->proc_name
;
669 /* Make sure that setup code for dummy variables which are used in the
670 setup of other variables is generated first. */
673 /* Find the first dummy arg seen after us, or the first non-dummy arg.
674 This is a circular list, so don't go past the head. */
676 && (!p
->attr
.dummy
|| p
->dummy_order
> sym
->dummy_order
))
682 /* Insert in between last and p. */
688 /* Used in gfc_get_symbol_decl and gfc_get_derived_type to obtain the
689 backend_decl for a module symbol, if it all ready exists. If the
690 module gsymbol does not exist, it is created. If the symbol does
691 not exist, it is added to the gsymbol namespace. Returns true if
692 an existing backend_decl is found. */
695 gfc_get_module_backend_decl (gfc_symbol
*sym
)
701 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->module
);
703 if (!gsym
|| (gsym
->ns
&& gsym
->type
== GSYM_MODULE
))
709 gfc_find_symbol (sym
->name
, gsym
->ns
, 0, &s
);
715 gsym
= gfc_get_gsymbol (sym
->module
);
716 gsym
->type
= GSYM_MODULE
;
717 gsym
->ns
= gfc_get_namespace (NULL
, 0);
720 st
= gfc_new_symtree (&gsym
->ns
->sym_root
, sym
->name
);
724 else if (sym
->attr
.flavor
== FL_DERIVED
)
726 if (s
&& s
->attr
.flavor
== FL_PROCEDURE
)
729 gcc_assert (s
->attr
.generic
);
730 for (intr
= s
->generic
; intr
; intr
= intr
->next
)
731 if (intr
->sym
->attr
.flavor
== FL_DERIVED
)
738 if (!s
->backend_decl
)
739 s
->backend_decl
= gfc_get_derived_type (s
);
740 gfc_copy_dt_decls_ifequal (s
, sym
, true);
743 else if (s
->backend_decl
)
745 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
746 gfc_copy_dt_decls_ifequal (s
->ts
.u
.derived
, sym
->ts
.u
.derived
,
748 else if (sym
->ts
.type
== BT_CHARACTER
)
749 sym
->ts
.u
.cl
->backend_decl
= s
->ts
.u
.cl
->backend_decl
;
750 sym
->backend_decl
= s
->backend_decl
;
758 /* Create an array index type variable with function scope. */
761 create_index_var (const char * pfx
, int nest
)
765 decl
= gfc_create_var_np (gfc_array_index_type
, pfx
);
767 gfc_add_decl_to_parent_function (decl
);
769 gfc_add_decl_to_function (decl
);
774 /* Create variables to hold all the non-constant bits of info for a
775 descriptorless array. Remember these in the lang-specific part of the
779 gfc_build_qualified_array (tree decl
, gfc_symbol
* sym
)
784 gfc_namespace
* procns
;
786 type
= TREE_TYPE (decl
);
788 /* We just use the descriptor, if there is one. */
789 if (GFC_DESCRIPTOR_TYPE_P (type
))
792 gcc_assert (GFC_ARRAY_TYPE_P (type
));
793 procns
= gfc_find_proc_namespace (sym
->ns
);
794 nest
= (procns
->proc_name
->backend_decl
!= current_function_decl
)
795 && !sym
->attr
.contained
;
797 if (sym
->attr
.codimension
&& gfc_option
.coarray
== GFC_FCOARRAY_LIB
798 && sym
->as
->type
!= AS_ASSUMED_SHAPE
799 && GFC_TYPE_ARRAY_CAF_TOKEN (type
) == NULL_TREE
)
803 token
= gfc_create_var_np (build_qualified_type (pvoid_type_node
,
806 GFC_TYPE_ARRAY_CAF_TOKEN (type
) = token
;
807 DECL_ARTIFICIAL (token
) = 1;
808 TREE_STATIC (token
) = 1;
809 gfc_add_decl_to_function (token
);
812 for (dim
= 0; dim
< GFC_TYPE_ARRAY_RANK (type
); dim
++)
814 if (GFC_TYPE_ARRAY_LBOUND (type
, dim
) == NULL_TREE
)
816 GFC_TYPE_ARRAY_LBOUND (type
, dim
) = create_index_var ("lbound", nest
);
817 TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type
, dim
)) = 1;
819 /* Don't try to use the unknown bound for assumed shape arrays. */
820 if (GFC_TYPE_ARRAY_UBOUND (type
, dim
) == NULL_TREE
821 && (sym
->as
->type
!= AS_ASSUMED_SIZE
822 || dim
< GFC_TYPE_ARRAY_RANK (type
) - 1))
824 GFC_TYPE_ARRAY_UBOUND (type
, dim
) = create_index_var ("ubound", nest
);
825 TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type
, dim
)) = 1;
828 if (GFC_TYPE_ARRAY_STRIDE (type
, dim
) == NULL_TREE
)
830 GFC_TYPE_ARRAY_STRIDE (type
, dim
) = create_index_var ("stride", nest
);
831 TREE_NO_WARNING (GFC_TYPE_ARRAY_STRIDE (type
, dim
)) = 1;
834 for (dim
= GFC_TYPE_ARRAY_RANK (type
);
835 dim
< GFC_TYPE_ARRAY_RANK (type
) + GFC_TYPE_ARRAY_CORANK (type
); dim
++)
837 if (GFC_TYPE_ARRAY_LBOUND (type
, dim
) == NULL_TREE
)
839 GFC_TYPE_ARRAY_LBOUND (type
, dim
) = create_index_var ("lbound", nest
);
840 TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type
, dim
)) = 1;
842 /* Don't try to use the unknown ubound for the last coarray dimension. */
843 if (GFC_TYPE_ARRAY_UBOUND (type
, dim
) == NULL_TREE
844 && dim
< GFC_TYPE_ARRAY_RANK (type
) + GFC_TYPE_ARRAY_CORANK (type
) - 1)
846 GFC_TYPE_ARRAY_UBOUND (type
, dim
) = create_index_var ("ubound", nest
);
847 TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type
, dim
)) = 1;
850 if (GFC_TYPE_ARRAY_OFFSET (type
) == NULL_TREE
)
852 GFC_TYPE_ARRAY_OFFSET (type
) = gfc_create_var_np (gfc_array_index_type
,
854 TREE_NO_WARNING (GFC_TYPE_ARRAY_OFFSET (type
)) = 1;
857 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type
));
859 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type
));
862 if (GFC_TYPE_ARRAY_SIZE (type
) == NULL_TREE
863 && sym
->as
->type
!= AS_ASSUMED_SIZE
)
865 GFC_TYPE_ARRAY_SIZE (type
) = create_index_var ("size", nest
);
866 TREE_NO_WARNING (GFC_TYPE_ARRAY_SIZE (type
)) = 1;
869 if (POINTER_TYPE_P (type
))
871 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type
)));
872 gcc_assert (TYPE_LANG_SPECIFIC (type
)
873 == TYPE_LANG_SPECIFIC (TREE_TYPE (type
)));
874 type
= TREE_TYPE (type
);
877 if (! COMPLETE_TYPE_P (type
) && GFC_TYPE_ARRAY_SIZE (type
))
881 size
= fold_build2_loc (input_location
, MINUS_EXPR
, gfc_array_index_type
,
882 GFC_TYPE_ARRAY_SIZE (type
), gfc_index_one_node
);
883 range
= build_range_type (gfc_array_index_type
, gfc_index_zero_node
,
885 TYPE_DOMAIN (type
) = range
;
889 if (TYPE_NAME (type
) != NULL_TREE
890 && GFC_TYPE_ARRAY_UBOUND (type
, sym
->as
->rank
- 1) != NULL_TREE
891 && TREE_CODE (GFC_TYPE_ARRAY_UBOUND (type
, sym
->as
->rank
- 1)) == VAR_DECL
)
893 tree gtype
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
895 for (dim
= 0; dim
< sym
->as
->rank
- 1; dim
++)
897 gcc_assert (TREE_CODE (gtype
) == ARRAY_TYPE
);
898 gtype
= TREE_TYPE (gtype
);
900 gcc_assert (TREE_CODE (gtype
) == ARRAY_TYPE
);
901 if (TYPE_MAX_VALUE (TYPE_DOMAIN (gtype
)) == NULL
)
902 TYPE_NAME (type
) = NULL_TREE
;
905 if (TYPE_NAME (type
) == NULL_TREE
)
907 tree gtype
= TREE_TYPE (type
), rtype
, type_decl
;
909 for (dim
= sym
->as
->rank
- 1; dim
>= 0; dim
--)
912 lbound
= GFC_TYPE_ARRAY_LBOUND (type
, dim
);
913 ubound
= GFC_TYPE_ARRAY_UBOUND (type
, dim
);
914 rtype
= build_range_type (gfc_array_index_type
, lbound
, ubound
);
915 gtype
= build_array_type (gtype
, rtype
);
916 /* Ensure the bound variables aren't optimized out at -O0.
917 For -O1 and above they often will be optimized out, but
918 can be tracked by VTA. Also set DECL_NAMELESS, so that
919 the artificial lbound.N or ubound.N DECL_NAME doesn't
920 end up in debug info. */
921 if (lbound
&& TREE_CODE (lbound
) == VAR_DECL
922 && DECL_ARTIFICIAL (lbound
) && DECL_IGNORED_P (lbound
))
924 if (DECL_NAME (lbound
)
925 && strstr (IDENTIFIER_POINTER (DECL_NAME (lbound
)),
927 DECL_NAMELESS (lbound
) = 1;
928 DECL_IGNORED_P (lbound
) = 0;
930 if (ubound
&& TREE_CODE (ubound
) == VAR_DECL
931 && DECL_ARTIFICIAL (ubound
) && DECL_IGNORED_P (ubound
))
933 if (DECL_NAME (ubound
)
934 && strstr (IDENTIFIER_POINTER (DECL_NAME (ubound
)),
936 DECL_NAMELESS (ubound
) = 1;
937 DECL_IGNORED_P (ubound
) = 0;
940 TYPE_NAME (type
) = type_decl
= build_decl (input_location
,
941 TYPE_DECL
, NULL
, gtype
);
942 DECL_ORIGINAL_TYPE (type_decl
) = gtype
;
947 /* For some dummy arguments we don't use the actual argument directly.
948 Instead we create a local decl and use that. This allows us to perform
949 initialization, and construct full type information. */
952 gfc_build_dummy_array_decl (gfc_symbol
* sym
, tree dummy
)
962 if (sym
->attr
.pointer
|| sym
->attr
.allocatable
963 || (sym
->as
&& sym
->as
->type
== AS_ASSUMED_RANK
))
966 /* Add to list of variables if not a fake result variable. */
967 if (sym
->attr
.result
|| sym
->attr
.dummy
)
968 gfc_defer_symbol_init (sym
);
970 type
= TREE_TYPE (dummy
);
971 gcc_assert (TREE_CODE (dummy
) == PARM_DECL
972 && POINTER_TYPE_P (type
));
974 /* Do we know the element size? */
975 known_size
= sym
->ts
.type
!= BT_CHARACTER
976 || INTEGER_CST_P (sym
->ts
.u
.cl
->backend_decl
);
978 if (known_size
&& !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type
)))
980 /* For descriptorless arrays with known element size the actual
981 argument is sufficient. */
982 gcc_assert (GFC_ARRAY_TYPE_P (type
));
983 gfc_build_qualified_array (dummy
, sym
);
987 type
= TREE_TYPE (type
);
988 if (GFC_DESCRIPTOR_TYPE_P (type
))
990 /* Create a descriptorless array pointer. */
994 /* Even when -frepack-arrays is used, symbols with TARGET attribute
996 if (!gfc_option
.flag_repack_arrays
|| sym
->attr
.target
)
998 if (as
->type
== AS_ASSUMED_SIZE
)
999 packed
= PACKED_FULL
;
1003 if (as
->type
== AS_EXPLICIT
)
1005 packed
= PACKED_FULL
;
1006 for (n
= 0; n
< as
->rank
; n
++)
1010 && as
->upper
[n
]->expr_type
== EXPR_CONSTANT
1011 && as
->lower
[n
]->expr_type
== EXPR_CONSTANT
))
1013 packed
= PACKED_PARTIAL
;
1019 packed
= PACKED_PARTIAL
;
1022 type
= gfc_typenode_for_spec (&sym
->ts
);
1023 type
= gfc_get_nodesc_array_type (type
, sym
->as
, packed
,
1028 /* We now have an expression for the element size, so create a fully
1029 qualified type. Reset sym->backend decl or this will just return the
1031 DECL_ARTIFICIAL (sym
->backend_decl
) = 1;
1032 sym
->backend_decl
= NULL_TREE
;
1033 type
= gfc_sym_type (sym
);
1034 packed
= PACKED_FULL
;
1037 ASM_FORMAT_PRIVATE_NAME (name
, IDENTIFIER_POINTER (DECL_NAME (dummy
)), 0);
1038 decl
= build_decl (input_location
,
1039 VAR_DECL
, get_identifier (name
), type
);
1041 DECL_ARTIFICIAL (decl
) = 1;
1042 DECL_NAMELESS (decl
) = 1;
1043 TREE_PUBLIC (decl
) = 0;
1044 TREE_STATIC (decl
) = 0;
1045 DECL_EXTERNAL (decl
) = 0;
1047 /* Avoid uninitialized warnings for optional dummy arguments. */
1048 if (sym
->attr
.optional
)
1049 TREE_NO_WARNING (decl
) = 1;
1051 /* We should never get deferred shape arrays here. We used to because of
1053 gcc_assert (sym
->as
->type
!= AS_DEFERRED
);
1055 if (packed
== PACKED_PARTIAL
)
1056 GFC_DECL_PARTIAL_PACKED_ARRAY (decl
) = 1;
1057 else if (packed
== PACKED_FULL
)
1058 GFC_DECL_PACKED_ARRAY (decl
) = 1;
1060 gfc_build_qualified_array (decl
, sym
);
1062 if (DECL_LANG_SPECIFIC (dummy
))
1063 DECL_LANG_SPECIFIC (decl
) = DECL_LANG_SPECIFIC (dummy
);
1065 gfc_allocate_lang_decl (decl
);
1067 GFC_DECL_SAVED_DESCRIPTOR (decl
) = dummy
;
1069 if (sym
->ns
->proc_name
->backend_decl
== current_function_decl
1070 || sym
->attr
.contained
)
1071 gfc_add_decl_to_function (decl
);
1073 gfc_add_decl_to_parent_function (decl
);
1078 /* For symbol SYM with GFC_DECL_SAVED_DESCRIPTOR used in contained
1079 function add a VAR_DECL to the current function with DECL_VALUE_EXPR
1080 pointing to the artificial variable for debug info purposes. */
1083 gfc_nonlocal_dummy_array_decl (gfc_symbol
*sym
)
1087 if (! nonlocal_dummy_decl_pset
)
1088 nonlocal_dummy_decl_pset
= pointer_set_create ();
1090 if (pointer_set_insert (nonlocal_dummy_decl_pset
, sym
->backend_decl
))
1093 dummy
= GFC_DECL_SAVED_DESCRIPTOR (sym
->backend_decl
);
1094 decl
= build_decl (input_location
, VAR_DECL
, DECL_NAME (dummy
),
1095 TREE_TYPE (sym
->backend_decl
));
1096 DECL_ARTIFICIAL (decl
) = 0;
1097 TREE_USED (decl
) = 1;
1098 TREE_PUBLIC (decl
) = 0;
1099 TREE_STATIC (decl
) = 0;
1100 DECL_EXTERNAL (decl
) = 0;
1101 if (DECL_BY_REFERENCE (dummy
))
1102 DECL_BY_REFERENCE (decl
) = 1;
1103 DECL_LANG_SPECIFIC (decl
) = DECL_LANG_SPECIFIC (sym
->backend_decl
);
1104 SET_DECL_VALUE_EXPR (decl
, sym
->backend_decl
);
1105 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
1106 DECL_CONTEXT (decl
) = DECL_CONTEXT (sym
->backend_decl
);
1107 DECL_CHAIN (decl
) = nonlocal_dummy_decls
;
1108 nonlocal_dummy_decls
= decl
;
1111 /* Return a constant or a variable to use as a string length. Does not
1112 add the decl to the current scope. */
1115 gfc_create_string_length (gfc_symbol
* sym
)
1117 gcc_assert (sym
->ts
.u
.cl
);
1118 gfc_conv_const_charlen (sym
->ts
.u
.cl
);
1120 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
1125 /* The string length variable shall be in static memory if it is either
1126 explicitly SAVED, a module variable or with -fno-automatic. Only
1127 relevant is "len=:" - otherwise, it is either a constant length or
1128 it is an automatic variable. */
1129 bool static_length
= sym
->attr
.save
1130 || sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
1131 || (gfc_option
.flag_max_stack_var_size
== 0
1132 && sym
->ts
.deferred
&& !sym
->attr
.dummy
1133 && !sym
->attr
.result
&& !sym
->attr
.function
);
1135 /* Also prefix the mangled name. We need to call GFC_PREFIX for static
1136 variables as some systems do not support the "." in the assembler name.
1137 For nonstatic variables, the "." does not appear in assembler. */
1141 name
= gfc_get_string (GFC_PREFIX ("%s_MOD_%s"), sym
->module
,
1144 name
= gfc_get_string (GFC_PREFIX ("%s"), sym
->name
);
1146 else if (sym
->module
)
1147 name
= gfc_get_string (".__%s_MOD_%s", sym
->module
, sym
->name
);
1149 name
= gfc_get_string (".%s", sym
->name
);
1151 length
= build_decl (input_location
,
1152 VAR_DECL
, get_identifier (name
),
1153 gfc_charlen_type_node
);
1154 DECL_ARTIFICIAL (length
) = 1;
1155 TREE_USED (length
) = 1;
1156 if (sym
->ns
->proc_name
->tlink
!= NULL
)
1157 gfc_defer_symbol_init (sym
);
1159 sym
->ts
.u
.cl
->backend_decl
= length
;
1162 TREE_STATIC (length
) = 1;
1164 if (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
1165 && (sym
->attr
.access
!= ACCESS_PRIVATE
|| sym
->attr
.public_used
))
1166 TREE_PUBLIC (length
) = 1;
1169 gcc_assert (sym
->ts
.u
.cl
->backend_decl
!= NULL_TREE
);
1170 return sym
->ts
.u
.cl
->backend_decl
;
1173 /* If a variable is assigned a label, we add another two auxiliary
1177 gfc_add_assign_aux_vars (gfc_symbol
* sym
)
1183 gcc_assert (sym
->backend_decl
);
1185 decl
= sym
->backend_decl
;
1186 gfc_allocate_lang_decl (decl
);
1187 GFC_DECL_ASSIGN (decl
) = 1;
1188 length
= build_decl (input_location
,
1189 VAR_DECL
, create_tmp_var_name (sym
->name
),
1190 gfc_charlen_type_node
);
1191 addr
= build_decl (input_location
,
1192 VAR_DECL
, create_tmp_var_name (sym
->name
),
1194 gfc_finish_var_decl (length
, sym
);
1195 gfc_finish_var_decl (addr
, sym
);
1196 /* STRING_LENGTH is also used as flag. Less than -1 means that
1197 ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the
1198 target label's address. Otherwise, value is the length of a format string
1199 and ASSIGN_ADDR is its address. */
1200 if (TREE_STATIC (length
))
1201 DECL_INITIAL (length
) = build_int_cst (gfc_charlen_type_node
, -2);
1203 gfc_defer_symbol_init (sym
);
1205 GFC_DECL_STRING_LEN (decl
) = length
;
1206 GFC_DECL_ASSIGN_ADDR (decl
) = addr
;
1211 add_attributes_to_decl (symbol_attribute sym_attr
, tree list
)
1216 for (id
= 0; id
< EXT_ATTR_NUM
; id
++)
1217 if (sym_attr
.ext_attr
& (1 << id
))
1219 attr
= build_tree_list (
1220 get_identifier (ext_attr_list
[id
].middle_end_name
),
1222 list
= chainon (list
, attr
);
1225 if (sym_attr
.omp_declare_target
)
1226 list
= tree_cons (get_identifier ("omp declare target"),
1233 static void build_function_decl (gfc_symbol
* sym
, bool global
);
1236 /* Return the decl for a gfc_symbol, create it if it doesn't already
1240 gfc_get_symbol_decl (gfc_symbol
* sym
)
1243 tree length
= NULL_TREE
;
1246 bool intrinsic_array_parameter
= false;
1249 gcc_assert (sym
->attr
.referenced
1250 || sym
->attr
.flavor
== FL_PROCEDURE
1251 || sym
->attr
.use_assoc
1252 || sym
->ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
1253 || (sym
->module
&& sym
->attr
.if_source
!= IFSRC_DECL
1254 && sym
->backend_decl
));
1256 if (sym
->ns
&& sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.function
)
1257 byref
= gfc_return_by_reference (sym
->ns
->proc_name
);
1261 /* Make sure that the vtab for the declared type is completed. */
1262 if (sym
->ts
.type
== BT_CLASS
)
1264 gfc_component
*c
= CLASS_DATA (sym
);
1265 if (!c
->ts
.u
.derived
->backend_decl
)
1267 gfc_find_derived_vtab (c
->ts
.u
.derived
);
1268 gfc_get_derived_type (sym
->ts
.u
.derived
);
1272 /* All deferred character length procedures need to retain the backend
1273 decl, which is a pointer to the character length in the caller's
1274 namespace and to declare a local character length. */
1275 if (!byref
&& sym
->attr
.function
1276 && sym
->ts
.type
== BT_CHARACTER
1278 && sym
->ts
.u
.cl
->passed_length
== NULL
1279 && sym
->ts
.u
.cl
->backend_decl
1280 && TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
1282 sym
->ts
.u
.cl
->passed_length
= sym
->ts
.u
.cl
->backend_decl
;
1283 sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
1284 length
= gfc_create_string_length (sym
);
1287 fun_or_res
= byref
&& (sym
->attr
.result
1288 || (sym
->attr
.function
&& sym
->ts
.deferred
));
1289 if ((sym
->attr
.dummy
&& ! sym
->attr
.function
) || fun_or_res
)
1291 /* Return via extra parameter. */
1292 if (sym
->attr
.result
&& byref
1293 && !sym
->backend_decl
)
1296 DECL_ARGUMENTS (sym
->ns
->proc_name
->backend_decl
);
1297 /* For entry master function skip over the __entry
1299 if (sym
->ns
->proc_name
->attr
.entry_master
)
1300 sym
->backend_decl
= DECL_CHAIN (sym
->backend_decl
);
1303 /* Dummy variables should already have been created. */
1304 gcc_assert (sym
->backend_decl
);
1306 /* Create a character length variable. */
1307 if (sym
->ts
.type
== BT_CHARACTER
)
1309 /* For a deferred dummy, make a new string length variable. */
1310 if (sym
->ts
.deferred
1312 (sym
->ts
.u
.cl
->passed_length
== sym
->ts
.u
.cl
->backend_decl
))
1313 sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
1315 if (sym
->ts
.deferred
&& fun_or_res
1316 && sym
->ts
.u
.cl
->passed_length
== NULL
1317 && sym
->ts
.u
.cl
->backend_decl
)
1319 sym
->ts
.u
.cl
->passed_length
= sym
->ts
.u
.cl
->backend_decl
;
1320 sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
1323 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
1324 length
= gfc_create_string_length (sym
);
1326 length
= sym
->ts
.u
.cl
->backend_decl
;
1327 if (TREE_CODE (length
) == VAR_DECL
1328 && DECL_FILE_SCOPE_P (length
))
1330 /* Add the string length to the same context as the symbol. */
1331 if (DECL_CONTEXT (sym
->backend_decl
) == current_function_decl
)
1332 gfc_add_decl_to_function (length
);
1334 gfc_add_decl_to_parent_function (length
);
1336 gcc_assert (DECL_CONTEXT (sym
->backend_decl
) ==
1337 DECL_CONTEXT (length
));
1339 gfc_defer_symbol_init (sym
);
1343 /* Use a copy of the descriptor for dummy arrays. */
1344 if ((sym
->attr
.dimension
|| sym
->attr
.codimension
)
1345 && !TREE_USED (sym
->backend_decl
))
1347 decl
= gfc_build_dummy_array_decl (sym
, sym
->backend_decl
);
1348 /* Prevent the dummy from being detected as unused if it is copied. */
1349 if (sym
->backend_decl
!= NULL
&& decl
!= sym
->backend_decl
)
1350 DECL_ARTIFICIAL (sym
->backend_decl
) = 1;
1351 sym
->backend_decl
= decl
;
1354 TREE_USED (sym
->backend_decl
) = 1;
1355 if (sym
->attr
.assign
&& GFC_DECL_ASSIGN (sym
->backend_decl
) == 0)
1357 gfc_add_assign_aux_vars (sym
);
1360 if (sym
->attr
.dimension
1361 && DECL_LANG_SPECIFIC (sym
->backend_decl
)
1362 && GFC_DECL_SAVED_DESCRIPTOR (sym
->backend_decl
)
1363 && DECL_CONTEXT (sym
->backend_decl
) != current_function_decl
)
1364 gfc_nonlocal_dummy_array_decl (sym
);
1366 if (sym
->ts
.type
== BT_CLASS
&& sym
->backend_decl
)
1367 GFC_DECL_CLASS(sym
->backend_decl
) = 1;
1369 if (sym
->ts
.type
== BT_CLASS
&& sym
->backend_decl
)
1370 GFC_DECL_CLASS(sym
->backend_decl
) = 1;
1371 return sym
->backend_decl
;
1374 if (sym
->backend_decl
)
1375 return sym
->backend_decl
;
1377 /* Special case for array-valued named constants from intrinsic
1378 procedures; those are inlined. */
1379 if (sym
->attr
.use_assoc
&& sym
->from_intmod
1380 && sym
->attr
.flavor
== FL_PARAMETER
)
1381 intrinsic_array_parameter
= true;
1383 /* If use associated compilation, use the module
1385 if ((sym
->attr
.flavor
== FL_VARIABLE
1386 || sym
->attr
.flavor
== FL_PARAMETER
)
1387 && sym
->attr
.use_assoc
1388 && !intrinsic_array_parameter
1390 && gfc_get_module_backend_decl (sym
))
1392 if (sym
->ts
.type
== BT_CLASS
&& sym
->backend_decl
)
1393 GFC_DECL_CLASS(sym
->backend_decl
) = 1;
1394 return sym
->backend_decl
;
1397 if (sym
->attr
.flavor
== FL_PROCEDURE
)
1399 /* Catch functions. Only used for actual parameters,
1400 procedure pointers and procptr initialization targets. */
1401 if (sym
->attr
.use_assoc
|| sym
->attr
.intrinsic
1402 || sym
->attr
.if_source
!= IFSRC_DECL
)
1404 decl
= gfc_get_extern_function_decl (sym
);
1405 gfc_set_decl_location (decl
, &sym
->declared_at
);
1409 if (!sym
->backend_decl
)
1410 build_function_decl (sym
, false);
1411 decl
= sym
->backend_decl
;
1416 if (sym
->attr
.intrinsic
)
1417 internal_error ("intrinsic variable which isn't a procedure");
1419 /* Create string length decl first so that they can be used in the
1420 type declaration. */
1421 if (sym
->ts
.type
== BT_CHARACTER
)
1422 length
= gfc_create_string_length (sym
);
1424 /* Create the decl for the variable. */
1425 decl
= build_decl (sym
->declared_at
.lb
->location
,
1426 VAR_DECL
, gfc_sym_identifier (sym
), gfc_sym_type (sym
));
1428 /* Add attributes to variables. Functions are handled elsewhere. */
1429 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
1430 decl_attributes (&decl
, attributes
, 0);
1432 /* Symbols from modules should have their assembler names mangled.
1433 This is done here rather than in gfc_finish_var_decl because it
1434 is different for string length variables. */
1437 gfc_set_decl_assembler_name (decl
, gfc_sym_mangled_identifier (sym
));
1438 if (sym
->attr
.use_assoc
&& !intrinsic_array_parameter
)
1439 DECL_IGNORED_P (decl
) = 1;
1442 if (sym
->attr
.select_type_temporary
)
1444 DECL_ARTIFICIAL (decl
) = 1;
1445 DECL_IGNORED_P (decl
) = 1;
1448 if (sym
->attr
.dimension
|| sym
->attr
.codimension
)
1450 /* Create variables to hold the non-constant bits of array info. */
1451 gfc_build_qualified_array (decl
, sym
);
1453 if (sym
->attr
.contiguous
1454 || ((sym
->attr
.allocatable
|| !sym
->attr
.dummy
) && !sym
->attr
.pointer
))
1455 GFC_DECL_PACKED_ARRAY (decl
) = 1;
1458 /* Remember this variable for allocation/cleanup. */
1459 if (sym
->attr
.dimension
|| sym
->attr
.allocatable
|| sym
->attr
.codimension
1460 || (sym
->ts
.type
== BT_CLASS
&&
1461 (CLASS_DATA (sym
)->attr
.dimension
1462 || CLASS_DATA (sym
)->attr
.allocatable
))
1463 || (sym
->ts
.type
== BT_DERIVED
1464 && (sym
->ts
.u
.derived
->attr
.alloc_comp
1465 || (!sym
->attr
.pointer
&& !sym
->attr
.artificial
&& !sym
->attr
.save
1466 && !sym
->ns
->proc_name
->attr
.is_main_program
1467 && gfc_is_finalizable (sym
->ts
.u
.derived
, NULL
))))
1468 /* This applies a derived type default initializer. */
1469 || (sym
->ts
.type
== BT_DERIVED
1470 && sym
->attr
.save
== SAVE_NONE
1472 && !sym
->attr
.allocatable
1473 && (sym
->value
&& !sym
->ns
->proc_name
->attr
.is_main_program
)
1474 && !(sym
->attr
.use_assoc
&& !intrinsic_array_parameter
)))
1475 gfc_defer_symbol_init (sym
);
1477 gfc_finish_var_decl (decl
, sym
);
1479 if (sym
->ts
.type
== BT_CHARACTER
)
1481 /* Character variables need special handling. */
1482 gfc_allocate_lang_decl (decl
);
1484 if (TREE_CODE (length
) != INTEGER_CST
)
1486 gfc_finish_var_decl (length
, sym
);
1487 gcc_assert (!sym
->value
);
1490 else if (sym
->attr
.subref_array_pointer
)
1492 /* We need the span for these beasts. */
1493 gfc_allocate_lang_decl (decl
);
1496 if (sym
->attr
.subref_array_pointer
)
1499 GFC_DECL_SUBREF_ARRAY_P (decl
) = 1;
1500 span
= build_decl (input_location
,
1501 VAR_DECL
, create_tmp_var_name ("span"),
1502 gfc_array_index_type
);
1503 gfc_finish_var_decl (span
, sym
);
1504 TREE_STATIC (span
) = TREE_STATIC (decl
);
1505 DECL_ARTIFICIAL (span
) = 1;
1507 GFC_DECL_SPAN (decl
) = span
;
1508 GFC_TYPE_ARRAY_SPAN (TREE_TYPE (decl
)) = span
;
1511 if (sym
->ts
.type
== BT_CLASS
)
1512 GFC_DECL_CLASS(decl
) = 1;
1514 sym
->backend_decl
= decl
;
1516 if (sym
->attr
.assign
)
1517 gfc_add_assign_aux_vars (sym
);
1519 if (intrinsic_array_parameter
)
1521 TREE_STATIC (decl
) = 1;
1522 DECL_EXTERNAL (decl
) = 0;
1525 if (TREE_STATIC (decl
)
1526 && !(sym
->attr
.use_assoc
&& !intrinsic_array_parameter
)
1527 && (sym
->attr
.save
|| sym
->ns
->proc_name
->attr
.is_main_program
1528 || gfc_option
.flag_max_stack_var_size
== 0
1529 || sym
->attr
.data
|| sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
1530 && (gfc_option
.coarray
!= GFC_FCOARRAY_LIB
1531 || !sym
->attr
.codimension
|| sym
->attr
.allocatable
))
1533 /* Add static initializer. For procedures, it is only needed if
1534 SAVE is specified otherwise they need to be reinitialized
1535 every time the procedure is entered. The TREE_STATIC is
1536 in this case due to -fmax-stack-var-size=. */
1538 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
1539 TREE_TYPE (decl
), sym
->attr
.dimension
1540 || (sym
->attr
.codimension
1541 && sym
->attr
.allocatable
),
1542 sym
->attr
.pointer
|| sym
->attr
.allocatable
1543 || sym
->ts
.type
== BT_CLASS
,
1544 sym
->attr
.proc_pointer
);
1547 if (!TREE_STATIC (decl
)
1548 && POINTER_TYPE_P (TREE_TYPE (decl
))
1549 && !sym
->attr
.pointer
1550 && !sym
->attr
.allocatable
1551 && !sym
->attr
.proc_pointer
1552 && !sym
->attr
.select_type_temporary
)
1553 DECL_BY_REFERENCE (decl
) = 1;
1555 if (sym
->attr
.associate_var
)
1556 GFC_DECL_ASSOCIATE_VAR_P (decl
) = 1;
1559 || (sym
->name
[0] == '_' && strncmp ("__def_init", sym
->name
, 10) == 0))
1560 TREE_READONLY (decl
) = 1;
1566 /* Substitute a temporary variable in place of the real one. */
1569 gfc_shadow_sym (gfc_symbol
* sym
, tree decl
, gfc_saved_var
* save
)
1571 save
->attr
= sym
->attr
;
1572 save
->decl
= sym
->backend_decl
;
1574 gfc_clear_attr (&sym
->attr
);
1575 sym
->attr
.referenced
= 1;
1576 sym
->attr
.flavor
= FL_VARIABLE
;
1578 sym
->backend_decl
= decl
;
1582 /* Restore the original variable. */
1585 gfc_restore_sym (gfc_symbol
* sym
, gfc_saved_var
* save
)
1587 sym
->attr
= save
->attr
;
1588 sym
->backend_decl
= save
->decl
;
1592 /* Declare a procedure pointer. */
1595 get_proc_pointer_decl (gfc_symbol
*sym
)
1600 decl
= sym
->backend_decl
;
1604 decl
= build_decl (input_location
,
1605 VAR_DECL
, get_identifier (sym
->name
),
1606 build_pointer_type (gfc_get_function_type (sym
)));
1610 /* Apply name mangling. */
1611 gfc_set_decl_assembler_name (decl
, gfc_sym_mangled_identifier (sym
));
1612 if (sym
->attr
.use_assoc
)
1613 DECL_IGNORED_P (decl
) = 1;
1616 if ((sym
->ns
->proc_name
1617 && sym
->ns
->proc_name
->backend_decl
== current_function_decl
)
1618 || sym
->attr
.contained
)
1619 gfc_add_decl_to_function (decl
);
1620 else if (sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
1621 gfc_add_decl_to_parent_function (decl
);
1623 sym
->backend_decl
= decl
;
1625 /* If a variable is USE associated, it's always external. */
1626 if (sym
->attr
.use_assoc
)
1628 DECL_EXTERNAL (decl
) = 1;
1629 TREE_PUBLIC (decl
) = 1;
1631 else if (sym
->module
&& sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
1633 /* This is the declaration of a module variable. */
1634 TREE_PUBLIC (decl
) = 1;
1635 TREE_STATIC (decl
) = 1;
1638 if (!sym
->attr
.use_assoc
1639 && (sym
->attr
.save
!= SAVE_NONE
|| sym
->attr
.data
1640 || (sym
->value
&& sym
->ns
->proc_name
->attr
.is_main_program
)))
1641 TREE_STATIC (decl
) = 1;
1643 if (TREE_STATIC (decl
) && sym
->value
)
1645 /* Add static initializer. */
1646 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
1648 sym
->attr
.dimension
,
1652 /* Handle threadprivate procedure pointers. */
1653 if (sym
->attr
.threadprivate
1654 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1655 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
1657 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
1658 decl_attributes (&decl
, attributes
, 0);
1664 /* Get a basic decl for an external function. */
1667 gfc_get_extern_function_decl (gfc_symbol
* sym
)
1673 gfc_intrinsic_sym
*isym
;
1675 char s
[GFC_MAX_SYMBOL_LEN
+ 23]; /* "_gfortran_f2c_specific" and '\0'. */
1680 if (sym
->backend_decl
)
1681 return sym
->backend_decl
;
1683 /* We should never be creating external decls for alternate entry points.
1684 The procedure may be an alternate entry point, but we don't want/need
1686 gcc_assert (!(sym
->attr
.entry
|| sym
->attr
.entry_master
));
1688 if (sym
->attr
.proc_pointer
)
1689 return get_proc_pointer_decl (sym
);
1691 /* See if this is an external procedure from the same file. If so,
1692 return the backend_decl. */
1693 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->binding_label
1694 ? sym
->binding_label
: sym
->name
);
1696 if (gsym
&& !gsym
->defined
)
1699 /* This can happen because of C binding. */
1700 if (gsym
&& gsym
->ns
&& gsym
->ns
->proc_name
1701 && gsym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
1704 if ((!sym
->attr
.use_assoc
|| sym
->attr
.if_source
!= IFSRC_DECL
)
1705 && !sym
->backend_decl
1707 && ((gsym
->type
== GSYM_SUBROUTINE
) || (gsym
->type
== GSYM_FUNCTION
))
1708 && (gsym
->ns
->proc_name
->backend_decl
|| !sym
->attr
.intrinsic
))
1710 if (!gsym
->ns
->proc_name
->backend_decl
)
1712 /* By construction, the external function cannot be
1713 a contained procedure. */
1716 gfc_save_backend_locus (&old_loc
);
1719 gfc_create_function_decl (gsym
->ns
, true);
1722 gfc_restore_backend_locus (&old_loc
);
1725 /* If the namespace has entries, the proc_name is the
1726 entry master. Find the entry and use its backend_decl.
1727 otherwise, use the proc_name backend_decl. */
1728 if (gsym
->ns
->entries
)
1730 gfc_entry_list
*entry
= gsym
->ns
->entries
;
1732 for (; entry
; entry
= entry
->next
)
1734 if (strcmp (gsym
->name
, entry
->sym
->name
) == 0)
1736 sym
->backend_decl
= entry
->sym
->backend_decl
;
1742 sym
->backend_decl
= gsym
->ns
->proc_name
->backend_decl
;
1744 if (sym
->backend_decl
)
1746 /* Avoid problems of double deallocation of the backend declaration
1747 later in gfc_trans_use_stmts; cf. PR 45087. */
1748 if (sym
->attr
.if_source
!= IFSRC_DECL
&& sym
->attr
.use_assoc
)
1749 sym
->attr
.use_assoc
= 0;
1751 return sym
->backend_decl
;
1755 /* See if this is a module procedure from the same file. If so,
1756 return the backend_decl. */
1758 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->module
);
1761 if (gsym
&& gsym
->ns
1762 && (gsym
->type
== GSYM_MODULE
1763 || (gsym
->ns
->proc_name
&& gsym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)))
1768 if (gsym
->type
== GSYM_MODULE
)
1769 gfc_find_symbol (sym
->name
, gsym
->ns
, 0, &s
);
1771 gfc_find_symbol (gsym
->sym_name
, gsym
->ns
, 0, &s
);
1773 if (s
&& s
->backend_decl
)
1775 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
1776 gfc_copy_dt_decls_ifequal (s
->ts
.u
.derived
, sym
->ts
.u
.derived
,
1778 else if (sym
->ts
.type
== BT_CHARACTER
)
1779 sym
->ts
.u
.cl
->backend_decl
= s
->ts
.u
.cl
->backend_decl
;
1780 sym
->backend_decl
= s
->backend_decl
;
1781 return sym
->backend_decl
;
1785 if (sym
->attr
.intrinsic
)
1787 /* Call the resolution function to get the actual name. This is
1788 a nasty hack which relies on the resolution functions only looking
1789 at the first argument. We pass NULL for the second argument
1790 otherwise things like AINT get confused. */
1791 isym
= gfc_find_function (sym
->name
);
1792 gcc_assert (isym
->resolve
.f0
!= NULL
);
1794 memset (&e
, 0, sizeof (e
));
1795 e
.expr_type
= EXPR_FUNCTION
;
1797 memset (&argexpr
, 0, sizeof (argexpr
));
1798 gcc_assert (isym
->formal
);
1799 argexpr
.ts
= isym
->formal
->ts
;
1801 if (isym
->formal
->next
== NULL
)
1802 isym
->resolve
.f1 (&e
, &argexpr
);
1805 if (isym
->formal
->next
->next
== NULL
)
1806 isym
->resolve
.f2 (&e
, &argexpr
, NULL
);
1809 if (isym
->formal
->next
->next
->next
== NULL
)
1810 isym
->resolve
.f3 (&e
, &argexpr
, NULL
, NULL
);
1813 /* All specific intrinsics take less than 5 arguments. */
1814 gcc_assert (isym
->formal
->next
->next
->next
->next
== NULL
);
1815 isym
->resolve
.f4 (&e
, &argexpr
, NULL
, NULL
, NULL
);
1820 if (gfc_option
.flag_f2c
1821 && ((e
.ts
.type
== BT_REAL
&& e
.ts
.kind
== gfc_default_real_kind
)
1822 || e
.ts
.type
== BT_COMPLEX
))
1824 /* Specific which needs a different implementation if f2c
1825 calling conventions are used. */
1826 sprintf (s
, "_gfortran_f2c_specific%s", e
.value
.function
.name
);
1829 sprintf (s
, "_gfortran_specific%s", e
.value
.function
.name
);
1831 name
= get_identifier (s
);
1832 mangled_name
= name
;
1836 name
= gfc_sym_identifier (sym
);
1837 mangled_name
= gfc_sym_mangled_function_id (sym
);
1840 type
= gfc_get_function_type (sym
);
1841 fndecl
= build_decl (input_location
,
1842 FUNCTION_DECL
, name
, type
);
1844 /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
1845 TREE_PUBLIC specifies whether a function is globally addressable (i.e.
1846 the opposite of declaring a function as static in C). */
1847 DECL_EXTERNAL (fndecl
) = 1;
1848 TREE_PUBLIC (fndecl
) = 1;
1850 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
1851 decl_attributes (&fndecl
, attributes
, 0);
1853 gfc_set_decl_assembler_name (fndecl
, mangled_name
);
1855 /* Set the context of this decl. */
1856 if (0 && sym
->ns
&& sym
->ns
->proc_name
)
1858 /* TODO: Add external decls to the appropriate scope. */
1859 DECL_CONTEXT (fndecl
) = sym
->ns
->proc_name
->backend_decl
;
1863 /* Global declaration, e.g. intrinsic subroutine. */
1864 DECL_CONTEXT (fndecl
) = NULL_TREE
;
1867 /* Set attributes for PURE functions. A call to PURE function in the
1868 Fortran 95 sense is both pure and without side effects in the C
1870 if (sym
->attr
.pure
|| sym
->attr
.implicit_pure
)
1872 if (sym
->attr
.function
&& !gfc_return_by_reference (sym
))
1873 DECL_PURE_P (fndecl
) = 1;
1874 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
1875 parameters and don't use alternate returns (is this
1876 allowed?). In that case, calls to them are meaningless, and
1877 can be optimized away. See also in build_function_decl(). */
1878 TREE_SIDE_EFFECTS (fndecl
) = 0;
1881 /* Mark non-returning functions. */
1882 if (sym
->attr
.noreturn
)
1883 TREE_THIS_VOLATILE(fndecl
) = 1;
1885 sym
->backend_decl
= fndecl
;
1887 if (DECL_CONTEXT (fndecl
) == NULL_TREE
)
1888 pushdecl_top_level (fndecl
);
1891 && sym
->formal_ns
->proc_name
== sym
1892 && sym
->formal_ns
->omp_declare_simd
)
1893 gfc_trans_omp_declare_simd (sym
->formal_ns
);
1899 /* Create a declaration for a procedure. For external functions (in the C
1900 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
1901 a master function with alternate entry points. */
1904 build_function_decl (gfc_symbol
* sym
, bool global
)
1906 tree fndecl
, type
, attributes
;
1907 symbol_attribute attr
;
1909 gfc_formal_arglist
*f
;
1911 gcc_assert (!sym
->attr
.external
);
1913 if (sym
->backend_decl
)
1916 /* Set the line and filename. sym->declared_at seems to point to the
1917 last statement for subroutines, but it'll do for now. */
1918 gfc_set_backend_locus (&sym
->declared_at
);
1920 /* Allow only one nesting level. Allow public declarations. */
1921 gcc_assert (current_function_decl
== NULL_TREE
1922 || DECL_FILE_SCOPE_P (current_function_decl
)
1923 || (TREE_CODE (DECL_CONTEXT (current_function_decl
))
1924 == NAMESPACE_DECL
));
1926 type
= gfc_get_function_type (sym
);
1927 fndecl
= build_decl (input_location
,
1928 FUNCTION_DECL
, gfc_sym_identifier (sym
), type
);
1932 /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
1933 TREE_PUBLIC specifies whether a function is globally addressable (i.e.
1934 the opposite of declaring a function as static in C). */
1935 DECL_EXTERNAL (fndecl
) = 0;
1937 if (sym
->attr
.access
== ACCESS_UNKNOWN
&& sym
->module
1938 && (sym
->ns
->default_access
== ACCESS_PRIVATE
1939 || (sym
->ns
->default_access
== ACCESS_UNKNOWN
1940 && gfc_option
.flag_module_private
)))
1941 sym
->attr
.access
= ACCESS_PRIVATE
;
1943 if (!current_function_decl
1944 && !sym
->attr
.entry_master
&& !sym
->attr
.is_main_program
1945 && (sym
->attr
.access
!= ACCESS_PRIVATE
|| sym
->binding_label
1946 || sym
->attr
.public_used
))
1947 TREE_PUBLIC (fndecl
) = 1;
1949 if (sym
->attr
.referenced
|| sym
->attr
.entry_master
)
1950 TREE_USED (fndecl
) = 1;
1952 attributes
= add_attributes_to_decl (attr
, NULL_TREE
);
1953 decl_attributes (&fndecl
, attributes
, 0);
1955 /* Figure out the return type of the declared function, and build a
1956 RESULT_DECL for it. If this is a subroutine with alternate
1957 returns, build a RESULT_DECL for it. */
1958 result_decl
= NULL_TREE
;
1959 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
1962 if (gfc_return_by_reference (sym
))
1963 type
= void_type_node
;
1966 if (sym
->result
!= sym
)
1967 result_decl
= gfc_sym_identifier (sym
->result
);
1969 type
= TREE_TYPE (TREE_TYPE (fndecl
));
1974 /* Look for alternate return placeholders. */
1975 int has_alternate_returns
= 0;
1976 for (f
= gfc_sym_get_dummy_args (sym
); f
; f
= f
->next
)
1980 has_alternate_returns
= 1;
1985 if (has_alternate_returns
)
1986 type
= integer_type_node
;
1988 type
= void_type_node
;
1991 result_decl
= build_decl (input_location
,
1992 RESULT_DECL
, result_decl
, type
);
1993 DECL_ARTIFICIAL (result_decl
) = 1;
1994 DECL_IGNORED_P (result_decl
) = 1;
1995 DECL_CONTEXT (result_decl
) = fndecl
;
1996 DECL_RESULT (fndecl
) = result_decl
;
1998 /* Don't call layout_decl for a RESULT_DECL.
1999 layout_decl (result_decl, 0); */
2001 /* TREE_STATIC means the function body is defined here. */
2002 TREE_STATIC (fndecl
) = 1;
2004 /* Set attributes for PURE functions. A call to a PURE function in the
2005 Fortran 95 sense is both pure and without side effects in the C
2007 if (attr
.pure
|| attr
.implicit_pure
)
2009 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
2010 including an alternate return. In that case it can also be
2011 marked as PURE. See also in gfc_get_extern_function_decl(). */
2012 if (attr
.function
&& !gfc_return_by_reference (sym
))
2013 DECL_PURE_P (fndecl
) = 1;
2014 TREE_SIDE_EFFECTS (fndecl
) = 0;
2018 /* Layout the function declaration and put it in the binding level
2019 of the current function. */
2022 pushdecl_top_level (fndecl
);
2026 /* Perform name mangling if this is a top level or module procedure. */
2027 if (current_function_decl
== NULL_TREE
)
2028 gfc_set_decl_assembler_name (fndecl
, gfc_sym_mangled_function_id (sym
));
2030 sym
->backend_decl
= fndecl
;
2034 /* Create the DECL_ARGUMENTS for a procedure. */
2037 create_function_arglist (gfc_symbol
* sym
)
2040 gfc_formal_arglist
*f
;
2041 tree typelist
, hidden_typelist
;
2042 tree arglist
, hidden_arglist
;
2046 fndecl
= sym
->backend_decl
;
2048 /* Build formal argument list. Make sure that their TREE_CONTEXT is
2049 the new FUNCTION_DECL node. */
2050 arglist
= NULL_TREE
;
2051 hidden_arglist
= NULL_TREE
;
2052 typelist
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
2054 if (sym
->attr
.entry_master
)
2056 type
= TREE_VALUE (typelist
);
2057 parm
= build_decl (input_location
,
2058 PARM_DECL
, get_identifier ("__entry"), type
);
2060 DECL_CONTEXT (parm
) = fndecl
;
2061 DECL_ARG_TYPE (parm
) = type
;
2062 TREE_READONLY (parm
) = 1;
2063 gfc_finish_decl (parm
);
2064 DECL_ARTIFICIAL (parm
) = 1;
2066 arglist
= chainon (arglist
, parm
);
2067 typelist
= TREE_CHAIN (typelist
);
2070 if (gfc_return_by_reference (sym
))
2072 tree type
= TREE_VALUE (typelist
), length
= NULL
;
2074 if (sym
->ts
.type
== BT_CHARACTER
)
2076 /* Length of character result. */
2077 tree len_type
= TREE_VALUE (TREE_CHAIN (typelist
));
2079 length
= build_decl (input_location
,
2081 get_identifier (".__result"),
2083 if (!sym
->ts
.u
.cl
->length
)
2085 sym
->ts
.u
.cl
->backend_decl
= length
;
2086 TREE_USED (length
) = 1;
2088 gcc_assert (TREE_CODE (length
) == PARM_DECL
);
2089 DECL_CONTEXT (length
) = fndecl
;
2090 DECL_ARG_TYPE (length
) = len_type
;
2091 TREE_READONLY (length
) = 1;
2092 DECL_ARTIFICIAL (length
) = 1;
2093 gfc_finish_decl (length
);
2094 if (sym
->ts
.u
.cl
->backend_decl
== NULL
2095 || sym
->ts
.u
.cl
->backend_decl
== length
)
2100 if (sym
->ts
.u
.cl
->backend_decl
== NULL
)
2102 tree len
= build_decl (input_location
,
2104 get_identifier ("..__result"),
2105 gfc_charlen_type_node
);
2106 DECL_ARTIFICIAL (len
) = 1;
2107 TREE_USED (len
) = 1;
2108 sym
->ts
.u
.cl
->backend_decl
= len
;
2111 /* Make sure PARM_DECL type doesn't point to incomplete type. */
2112 arg
= sym
->result
? sym
->result
: sym
;
2113 backend_decl
= arg
->backend_decl
;
2114 /* Temporary clear it, so that gfc_sym_type creates complete
2116 arg
->backend_decl
= NULL
;
2117 type
= gfc_sym_type (arg
);
2118 arg
->backend_decl
= backend_decl
;
2119 type
= build_reference_type (type
);
2123 parm
= build_decl (input_location
,
2124 PARM_DECL
, get_identifier ("__result"), type
);
2126 DECL_CONTEXT (parm
) = fndecl
;
2127 DECL_ARG_TYPE (parm
) = TREE_VALUE (typelist
);
2128 TREE_READONLY (parm
) = 1;
2129 DECL_ARTIFICIAL (parm
) = 1;
2130 gfc_finish_decl (parm
);
2132 arglist
= chainon (arglist
, parm
);
2133 typelist
= TREE_CHAIN (typelist
);
2135 if (sym
->ts
.type
== BT_CHARACTER
)
2137 gfc_allocate_lang_decl (parm
);
2138 arglist
= chainon (arglist
, length
);
2139 typelist
= TREE_CHAIN (typelist
);
2143 hidden_typelist
= typelist
;
2144 for (f
= gfc_sym_get_dummy_args (sym
); f
; f
= f
->next
)
2145 if (f
->sym
!= NULL
) /* Ignore alternate returns. */
2146 hidden_typelist
= TREE_CHAIN (hidden_typelist
);
2148 for (f
= gfc_sym_get_dummy_args (sym
); f
; f
= f
->next
)
2150 char name
[GFC_MAX_SYMBOL_LEN
+ 2];
2152 /* Ignore alternate returns. */
2156 type
= TREE_VALUE (typelist
);
2158 if (f
->sym
->ts
.type
== BT_CHARACTER
2159 && (!sym
->attr
.is_bind_c
|| sym
->attr
.entry_master
))
2161 tree len_type
= TREE_VALUE (hidden_typelist
);
2162 tree length
= NULL_TREE
;
2163 if (!f
->sym
->ts
.deferred
)
2164 gcc_assert (len_type
== gfc_charlen_type_node
);
2166 gcc_assert (POINTER_TYPE_P (len_type
));
2168 strcpy (&name
[1], f
->sym
->name
);
2170 length
= build_decl (input_location
,
2171 PARM_DECL
, get_identifier (name
), len_type
);
2173 hidden_arglist
= chainon (hidden_arglist
, length
);
2174 DECL_CONTEXT (length
) = fndecl
;
2175 DECL_ARTIFICIAL (length
) = 1;
2176 DECL_ARG_TYPE (length
) = len_type
;
2177 TREE_READONLY (length
) = 1;
2178 gfc_finish_decl (length
);
2180 /* Remember the passed value. */
2181 if (!f
->sym
->ts
.u
.cl
|| f
->sym
->ts
.u
.cl
->passed_length
)
2183 /* This can happen if the same type is used for multiple
2184 arguments. We need to copy cl as otherwise
2185 cl->passed_length gets overwritten. */
2186 f
->sym
->ts
.u
.cl
= gfc_new_charlen (f
->sym
->ns
, f
->sym
->ts
.u
.cl
);
2188 f
->sym
->ts
.u
.cl
->passed_length
= length
;
2190 /* Use the passed value for assumed length variables. */
2191 if (!f
->sym
->ts
.u
.cl
->length
)
2193 TREE_USED (length
) = 1;
2194 gcc_assert (!f
->sym
->ts
.u
.cl
->backend_decl
);
2195 f
->sym
->ts
.u
.cl
->backend_decl
= length
;
2198 hidden_typelist
= TREE_CHAIN (hidden_typelist
);
2200 if (f
->sym
->ts
.u
.cl
->backend_decl
== NULL
2201 || f
->sym
->ts
.u
.cl
->backend_decl
== length
)
2203 if (f
->sym
->ts
.u
.cl
->backend_decl
== NULL
)
2204 gfc_create_string_length (f
->sym
);
2206 /* Make sure PARM_DECL type doesn't point to incomplete type. */
2207 if (f
->sym
->attr
.flavor
== FL_PROCEDURE
)
2208 type
= build_pointer_type (gfc_get_function_type (f
->sym
));
2210 type
= gfc_sym_type (f
->sym
);
2213 /* For noncharacter scalar intrinsic types, VALUE passes the value,
2214 hence, the optional status cannot be transferred via a NULL pointer.
2215 Thus, we will use a hidden argument in that case. */
2216 else if (f
->sym
->attr
.optional
&& f
->sym
->attr
.value
2217 && !f
->sym
->attr
.dimension
&& f
->sym
->ts
.type
!= BT_CLASS
2218 && f
->sym
->ts
.type
!= BT_DERIVED
)
2221 strcpy (&name
[1], f
->sym
->name
);
2223 tmp
= build_decl (input_location
,
2224 PARM_DECL
, get_identifier (name
),
2227 hidden_arglist
= chainon (hidden_arglist
, tmp
);
2228 DECL_CONTEXT (tmp
) = fndecl
;
2229 DECL_ARTIFICIAL (tmp
) = 1;
2230 DECL_ARG_TYPE (tmp
) = boolean_type_node
;
2231 TREE_READONLY (tmp
) = 1;
2232 gfc_finish_decl (tmp
);
2235 /* For non-constant length array arguments, make sure they use
2236 a different type node from TYPE_ARG_TYPES type. */
2237 if (f
->sym
->attr
.dimension
2238 && type
== TREE_VALUE (typelist
)
2239 && TREE_CODE (type
) == POINTER_TYPE
2240 && GFC_ARRAY_TYPE_P (type
)
2241 && f
->sym
->as
->type
!= AS_ASSUMED_SIZE
2242 && ! COMPLETE_TYPE_P (TREE_TYPE (type
)))
2244 if (f
->sym
->attr
.flavor
== FL_PROCEDURE
)
2245 type
= build_pointer_type (gfc_get_function_type (f
->sym
));
2247 type
= gfc_sym_type (f
->sym
);
2250 if (f
->sym
->attr
.proc_pointer
)
2251 type
= build_pointer_type (type
);
2253 if (f
->sym
->attr
.volatile_
)
2254 type
= build_qualified_type (type
, TYPE_QUAL_VOLATILE
);
2256 /* Build the argument declaration. */
2257 parm
= build_decl (input_location
,
2258 PARM_DECL
, gfc_sym_identifier (f
->sym
), type
);
2260 if (f
->sym
->attr
.volatile_
)
2262 TREE_THIS_VOLATILE (parm
) = 1;
2263 TREE_SIDE_EFFECTS (parm
) = 1;
2266 /* Fill in arg stuff. */
2267 DECL_CONTEXT (parm
) = fndecl
;
2268 DECL_ARG_TYPE (parm
) = TREE_VALUE (typelist
);
2269 /* All implementation args are read-only. */
2270 TREE_READONLY (parm
) = 1;
2271 if (POINTER_TYPE_P (type
)
2272 && (!f
->sym
->attr
.proc_pointer
2273 && f
->sym
->attr
.flavor
!= FL_PROCEDURE
))
2274 DECL_BY_REFERENCE (parm
) = 1;
2276 gfc_finish_decl (parm
);
2277 gfc_finish_decl_attrs (parm
, &f
->sym
->attr
);
2279 f
->sym
->backend_decl
= parm
;
2281 /* Coarrays which are descriptorless or assumed-shape pass with
2282 -fcoarray=lib the token and the offset as hidden arguments. */
2283 if (gfc_option
.coarray
== GFC_FCOARRAY_LIB
2284 && ((f
->sym
->ts
.type
!= BT_CLASS
&& f
->sym
->attr
.codimension
2285 && !f
->sym
->attr
.allocatable
)
2286 || (f
->sym
->ts
.type
== BT_CLASS
2287 && CLASS_DATA (f
->sym
)->attr
.codimension
2288 && !CLASS_DATA (f
->sym
)->attr
.allocatable
)))
2294 gcc_assert (f
->sym
->backend_decl
!= NULL_TREE
2295 && !sym
->attr
.is_bind_c
);
2296 caf_type
= f
->sym
->ts
.type
== BT_CLASS
2297 ? TREE_TYPE (CLASS_DATA (f
->sym
)->backend_decl
)
2298 : TREE_TYPE (f
->sym
->backend_decl
);
2300 token
= build_decl (input_location
, PARM_DECL
,
2301 create_tmp_var_name ("caf_token"),
2302 build_qualified_type (pvoid_type_node
,
2303 TYPE_QUAL_RESTRICT
));
2304 if ((f
->sym
->ts
.type
!= BT_CLASS
2305 && f
->sym
->as
->type
!= AS_DEFERRED
)
2306 || (f
->sym
->ts
.type
== BT_CLASS
2307 && CLASS_DATA (f
->sym
)->as
->type
!= AS_DEFERRED
))
2309 gcc_assert (DECL_LANG_SPECIFIC (f
->sym
->backend_decl
) == NULL
2310 || GFC_DECL_TOKEN (f
->sym
->backend_decl
) == NULL_TREE
);
2311 if (DECL_LANG_SPECIFIC (f
->sym
->backend_decl
) == NULL
)
2312 gfc_allocate_lang_decl (f
->sym
->backend_decl
);
2313 GFC_DECL_TOKEN (f
->sym
->backend_decl
) = token
;
2317 gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
) == NULL_TREE
);
2318 GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
) = token
;
2321 DECL_CONTEXT (token
) = fndecl
;
2322 DECL_ARTIFICIAL (token
) = 1;
2323 DECL_ARG_TYPE (token
) = TREE_VALUE (typelist
);
2324 TREE_READONLY (token
) = 1;
2325 hidden_arglist
= chainon (hidden_arglist
, token
);
2326 gfc_finish_decl (token
);
2328 offset
= build_decl (input_location
, PARM_DECL
,
2329 create_tmp_var_name ("caf_offset"),
2330 gfc_array_index_type
);
2332 if ((f
->sym
->ts
.type
!= BT_CLASS
2333 && f
->sym
->as
->type
!= AS_DEFERRED
)
2334 || (f
->sym
->ts
.type
== BT_CLASS
2335 && CLASS_DATA (f
->sym
)->as
->type
!= AS_DEFERRED
))
2337 gcc_assert (GFC_DECL_CAF_OFFSET (f
->sym
->backend_decl
)
2339 GFC_DECL_CAF_OFFSET (f
->sym
->backend_decl
) = offset
;
2343 gcc_assert (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type
) == NULL_TREE
);
2344 GFC_TYPE_ARRAY_CAF_OFFSET (caf_type
) = offset
;
2346 DECL_CONTEXT (offset
) = fndecl
;
2347 DECL_ARTIFICIAL (offset
) = 1;
2348 DECL_ARG_TYPE (offset
) = TREE_VALUE (typelist
);
2349 TREE_READONLY (offset
) = 1;
2350 hidden_arglist
= chainon (hidden_arglist
, offset
);
2351 gfc_finish_decl (offset
);
2354 arglist
= chainon (arglist
, parm
);
2355 typelist
= TREE_CHAIN (typelist
);
2358 /* Add the hidden string length parameters, unless the procedure
2360 if (!sym
->attr
.is_bind_c
)
2361 arglist
= chainon (arglist
, hidden_arglist
);
2363 gcc_assert (hidden_typelist
== NULL_TREE
2364 || TREE_VALUE (hidden_typelist
) == void_type_node
);
2365 DECL_ARGUMENTS (fndecl
) = arglist
;
2368 /* Do the setup necessary before generating the body of a function. */
2371 trans_function_start (gfc_symbol
* sym
)
2375 fndecl
= sym
->backend_decl
;
2377 /* Let GCC know the current scope is this function. */
2378 current_function_decl
= fndecl
;
2380 /* Let the world know what we're about to do. */
2381 announce_function (fndecl
);
2383 if (DECL_FILE_SCOPE_P (fndecl
))
2385 /* Create RTL for function declaration. */
2386 rest_of_decl_compilation (fndecl
, 1, 0);
2389 /* Create RTL for function definition. */
2390 make_decl_rtl (fndecl
);
2392 allocate_struct_function (fndecl
, false);
2394 /* function.c requires a push at the start of the function. */
2398 /* Create thunks for alternate entry points. */
2401 build_entry_thunks (gfc_namespace
* ns
, bool global
)
2403 gfc_formal_arglist
*formal
;
2404 gfc_formal_arglist
*thunk_formal
;
2406 gfc_symbol
*thunk_sym
;
2412 /* This should always be a toplevel function. */
2413 gcc_assert (current_function_decl
== NULL_TREE
);
2415 gfc_save_backend_locus (&old_loc
);
2416 for (el
= ns
->entries
; el
; el
= el
->next
)
2418 vec
<tree
, va_gc
> *args
= NULL
;
2419 vec
<tree
, va_gc
> *string_args
= NULL
;
2421 thunk_sym
= el
->sym
;
2423 build_function_decl (thunk_sym
, global
);
2424 create_function_arglist (thunk_sym
);
2426 trans_function_start (thunk_sym
);
2428 thunk_fndecl
= thunk_sym
->backend_decl
;
2430 gfc_init_block (&body
);
2432 /* Pass extra parameter identifying this entry point. */
2433 tmp
= build_int_cst (gfc_array_index_type
, el
->id
);
2434 vec_safe_push (args
, tmp
);
2436 if (thunk_sym
->attr
.function
)
2438 if (gfc_return_by_reference (ns
->proc_name
))
2440 tree ref
= DECL_ARGUMENTS (current_function_decl
);
2441 vec_safe_push (args
, ref
);
2442 if (ns
->proc_name
->ts
.type
== BT_CHARACTER
)
2443 vec_safe_push (args
, DECL_CHAIN (ref
));
2447 for (formal
= gfc_sym_get_dummy_args (ns
->proc_name
); formal
;
2448 formal
= formal
->next
)
2450 /* Ignore alternate returns. */
2451 if (formal
->sym
== NULL
)
2454 /* We don't have a clever way of identifying arguments, so resort to
2455 a brute-force search. */
2456 for (thunk_formal
= gfc_sym_get_dummy_args (thunk_sym
);
2458 thunk_formal
= thunk_formal
->next
)
2460 if (thunk_formal
->sym
== formal
->sym
)
2466 /* Pass the argument. */
2467 DECL_ARTIFICIAL (thunk_formal
->sym
->backend_decl
) = 1;
2468 vec_safe_push (args
, thunk_formal
->sym
->backend_decl
);
2469 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
2471 tmp
= thunk_formal
->sym
->ts
.u
.cl
->backend_decl
;
2472 vec_safe_push (string_args
, tmp
);
2477 /* Pass NULL for a missing argument. */
2478 vec_safe_push (args
, null_pointer_node
);
2479 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
2481 tmp
= build_int_cst (gfc_charlen_type_node
, 0);
2482 vec_safe_push (string_args
, tmp
);
2487 /* Call the master function. */
2488 vec_safe_splice (args
, string_args
);
2489 tmp
= ns
->proc_name
->backend_decl
;
2490 tmp
= build_call_expr_loc_vec (input_location
, tmp
, args
);
2491 if (ns
->proc_name
->attr
.mixed_entry_master
)
2493 tree union_decl
, field
;
2494 tree master_type
= TREE_TYPE (ns
->proc_name
->backend_decl
);
2496 union_decl
= build_decl (input_location
,
2497 VAR_DECL
, get_identifier ("__result"),
2498 TREE_TYPE (master_type
));
2499 DECL_ARTIFICIAL (union_decl
) = 1;
2500 DECL_EXTERNAL (union_decl
) = 0;
2501 TREE_PUBLIC (union_decl
) = 0;
2502 TREE_USED (union_decl
) = 1;
2503 layout_decl (union_decl
, 0);
2504 pushdecl (union_decl
);
2506 DECL_CONTEXT (union_decl
) = current_function_decl
;
2507 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
2508 TREE_TYPE (union_decl
), union_decl
, tmp
);
2509 gfc_add_expr_to_block (&body
, tmp
);
2511 for (field
= TYPE_FIELDS (TREE_TYPE (union_decl
));
2512 field
; field
= DECL_CHAIN (field
))
2513 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field
)),
2514 thunk_sym
->result
->name
) == 0)
2516 gcc_assert (field
!= NULL_TREE
);
2517 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
,
2518 TREE_TYPE (field
), union_decl
, field
,
2520 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
2521 TREE_TYPE (DECL_RESULT (current_function_decl
)),
2522 DECL_RESULT (current_function_decl
), tmp
);
2523 tmp
= build1_v (RETURN_EXPR
, tmp
);
2525 else if (TREE_TYPE (DECL_RESULT (current_function_decl
))
2528 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
2529 TREE_TYPE (DECL_RESULT (current_function_decl
)),
2530 DECL_RESULT (current_function_decl
), tmp
);
2531 tmp
= build1_v (RETURN_EXPR
, tmp
);
2533 gfc_add_expr_to_block (&body
, tmp
);
2535 /* Finish off this function and send it for code generation. */
2536 DECL_SAVED_TREE (thunk_fndecl
) = gfc_finish_block (&body
);
2539 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl
)) = thunk_fndecl
;
2540 DECL_SAVED_TREE (thunk_fndecl
)
2541 = build3_v (BIND_EXPR
, tmp
, DECL_SAVED_TREE (thunk_fndecl
),
2542 DECL_INITIAL (thunk_fndecl
));
2544 /* Output the GENERIC tree. */
2545 dump_function (TDI_original
, thunk_fndecl
);
2547 /* Store the end of the function, so that we get good line number
2548 info for the epilogue. */
2549 cfun
->function_end_locus
= input_location
;
2551 /* We're leaving the context of this function, so zap cfun.
2552 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
2553 tree_rest_of_compilation. */
2556 current_function_decl
= NULL_TREE
;
2558 cgraph_finalize_function (thunk_fndecl
, true);
2560 /* We share the symbols in the formal argument list with other entry
2561 points and the master function. Clear them so that they are
2562 recreated for each function. */
2563 for (formal
= gfc_sym_get_dummy_args (thunk_sym
); formal
;
2564 formal
= formal
->next
)
2565 if (formal
->sym
!= NULL
) /* Ignore alternate returns. */
2567 formal
->sym
->backend_decl
= NULL_TREE
;
2568 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
2569 formal
->sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
2572 if (thunk_sym
->attr
.function
)
2574 if (thunk_sym
->ts
.type
== BT_CHARACTER
)
2575 thunk_sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
2576 if (thunk_sym
->result
->ts
.type
== BT_CHARACTER
)
2577 thunk_sym
->result
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
2581 gfc_restore_backend_locus (&old_loc
);
2585 /* Create a decl for a function, and create any thunks for alternate entry
2586 points. If global is true, generate the function in the global binding
2587 level, otherwise in the current binding level (which can be global). */
2590 gfc_create_function_decl (gfc_namespace
* ns
, bool global
)
2592 /* Create a declaration for the master function. */
2593 build_function_decl (ns
->proc_name
, global
);
2595 /* Compile the entry thunks. */
2597 build_entry_thunks (ns
, global
);
2599 /* Now create the read argument list. */
2600 create_function_arglist (ns
->proc_name
);
2602 if (ns
->omp_declare_simd
)
2603 gfc_trans_omp_declare_simd (ns
);
2606 /* Return the decl used to hold the function return value. If
2607 parent_flag is set, the context is the parent_scope. */
2610 gfc_get_fake_result_decl (gfc_symbol
* sym
, int parent_flag
)
2614 tree this_fake_result_decl
;
2615 tree this_function_decl
;
2617 char name
[GFC_MAX_SYMBOL_LEN
+ 10];
2621 this_fake_result_decl
= parent_fake_result_decl
;
2622 this_function_decl
= DECL_CONTEXT (current_function_decl
);
2626 this_fake_result_decl
= current_fake_result_decl
;
2627 this_function_decl
= current_function_decl
;
2631 && sym
->ns
->proc_name
->backend_decl
== this_function_decl
2632 && sym
->ns
->proc_name
->attr
.entry_master
2633 && sym
!= sym
->ns
->proc_name
)
2636 if (this_fake_result_decl
!= NULL
)
2637 for (t
= TREE_CHAIN (this_fake_result_decl
); t
; t
= TREE_CHAIN (t
))
2638 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t
)), sym
->name
) == 0)
2641 return TREE_VALUE (t
);
2642 decl
= gfc_get_fake_result_decl (sym
->ns
->proc_name
, parent_flag
);
2645 this_fake_result_decl
= parent_fake_result_decl
;
2647 this_fake_result_decl
= current_fake_result_decl
;
2649 if (decl
&& sym
->ns
->proc_name
->attr
.mixed_entry_master
)
2653 for (field
= TYPE_FIELDS (TREE_TYPE (decl
));
2654 field
; field
= DECL_CHAIN (field
))
2655 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field
)),
2659 gcc_assert (field
!= NULL_TREE
);
2660 decl
= fold_build3_loc (input_location
, COMPONENT_REF
,
2661 TREE_TYPE (field
), decl
, field
, NULL_TREE
);
2664 var
= create_tmp_var_raw (TREE_TYPE (decl
), sym
->name
);
2666 gfc_add_decl_to_parent_function (var
);
2668 gfc_add_decl_to_function (var
);
2670 SET_DECL_VALUE_EXPR (var
, decl
);
2671 DECL_HAS_VALUE_EXPR_P (var
) = 1;
2672 GFC_DECL_RESULT (var
) = 1;
2674 TREE_CHAIN (this_fake_result_decl
)
2675 = tree_cons (get_identifier (sym
->name
), var
,
2676 TREE_CHAIN (this_fake_result_decl
));
2680 if (this_fake_result_decl
!= NULL_TREE
)
2681 return TREE_VALUE (this_fake_result_decl
);
2683 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
2688 if (sym
->ts
.type
== BT_CHARACTER
)
2690 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
2691 length
= gfc_create_string_length (sym
);
2693 length
= sym
->ts
.u
.cl
->backend_decl
;
2694 if (TREE_CODE (length
) == VAR_DECL
2695 && DECL_CONTEXT (length
) == NULL_TREE
)
2696 gfc_add_decl_to_function (length
);
2699 if (gfc_return_by_reference (sym
))
2701 decl
= DECL_ARGUMENTS (this_function_decl
);
2703 if (sym
->ns
->proc_name
->backend_decl
== this_function_decl
2704 && sym
->ns
->proc_name
->attr
.entry_master
)
2705 decl
= DECL_CHAIN (decl
);
2707 TREE_USED (decl
) = 1;
2709 decl
= gfc_build_dummy_array_decl (sym
, decl
);
2713 sprintf (name
, "__result_%.20s",
2714 IDENTIFIER_POINTER (DECL_NAME (this_function_decl
)));
2716 if (!sym
->attr
.mixed_entry_master
&& sym
->attr
.function
)
2717 decl
= build_decl (DECL_SOURCE_LOCATION (this_function_decl
),
2718 VAR_DECL
, get_identifier (name
),
2719 gfc_sym_type (sym
));
2721 decl
= build_decl (DECL_SOURCE_LOCATION (this_function_decl
),
2722 VAR_DECL
, get_identifier (name
),
2723 TREE_TYPE (TREE_TYPE (this_function_decl
)));
2724 DECL_ARTIFICIAL (decl
) = 1;
2725 DECL_EXTERNAL (decl
) = 0;
2726 TREE_PUBLIC (decl
) = 0;
2727 TREE_USED (decl
) = 1;
2728 GFC_DECL_RESULT (decl
) = 1;
2729 TREE_ADDRESSABLE (decl
) = 1;
2731 layout_decl (decl
, 0);
2732 gfc_finish_decl_attrs (decl
, &sym
->attr
);
2735 gfc_add_decl_to_parent_function (decl
);
2737 gfc_add_decl_to_function (decl
);
2741 parent_fake_result_decl
= build_tree_list (NULL
, decl
);
2743 current_fake_result_decl
= build_tree_list (NULL
, decl
);
2749 /* Builds a function decl. The remaining parameters are the types of the
2750 function arguments. Negative nargs indicates a varargs function. */
2753 build_library_function_decl_1 (tree name
, const char *spec
,
2754 tree rettype
, int nargs
, va_list p
)
2756 vec
<tree
, va_gc
> *arglist
;
2761 /* Library functions must be declared with global scope. */
2762 gcc_assert (current_function_decl
== NULL_TREE
);
2764 /* Create a list of the argument types. */
2765 vec_alloc (arglist
, abs (nargs
));
2766 for (n
= abs (nargs
); n
> 0; n
--)
2768 tree argtype
= va_arg (p
, tree
);
2769 arglist
->quick_push (argtype
);
2772 /* Build the function type and decl. */
2774 fntype
= build_function_type_vec (rettype
, arglist
);
2776 fntype
= build_varargs_function_type_vec (rettype
, arglist
);
2779 tree attr_args
= build_tree_list (NULL_TREE
,
2780 build_string (strlen (spec
), spec
));
2781 tree attrs
= tree_cons (get_identifier ("fn spec"),
2782 attr_args
, TYPE_ATTRIBUTES (fntype
));
2783 fntype
= build_type_attribute_variant (fntype
, attrs
);
2785 fndecl
= build_decl (input_location
,
2786 FUNCTION_DECL
, name
, fntype
);
2788 /* Mark this decl as external. */
2789 DECL_EXTERNAL (fndecl
) = 1;
2790 TREE_PUBLIC (fndecl
) = 1;
2794 rest_of_decl_compilation (fndecl
, 1, 0);
2799 /* Builds a function decl. The remaining parameters are the types of the
2800 function arguments. Negative nargs indicates a varargs function. */
2803 gfc_build_library_function_decl (tree name
, tree rettype
, int nargs
, ...)
2807 va_start (args
, nargs
);
2808 ret
= build_library_function_decl_1 (name
, NULL
, rettype
, nargs
, args
);
2813 /* Builds a function decl. The remaining parameters are the types of the
2814 function arguments. Negative nargs indicates a varargs function.
2815 The SPEC parameter specifies the function argument and return type
2816 specification according to the fnspec function type attribute. */
2819 gfc_build_library_function_decl_with_spec (tree name
, const char *spec
,
2820 tree rettype
, int nargs
, ...)
2824 va_start (args
, nargs
);
2825 ret
= build_library_function_decl_1 (name
, spec
, rettype
, nargs
, args
);
2831 gfc_build_intrinsic_function_decls (void)
2833 tree gfc_int4_type_node
= gfc_get_int_type (4);
2834 tree gfc_pint4_type_node
= build_pointer_type (gfc_int4_type_node
);
2835 tree gfc_int8_type_node
= gfc_get_int_type (8);
2836 tree gfc_pint8_type_node
= build_pointer_type (gfc_int8_type_node
);
2837 tree gfc_int16_type_node
= gfc_get_int_type (16);
2838 tree gfc_logical4_type_node
= gfc_get_logical_type (4);
2839 tree pchar1_type_node
= gfc_get_pchar_type (1);
2840 tree pchar4_type_node
= gfc_get_pchar_type (4);
2842 /* String functions. */
2843 gfor_fndecl_compare_string
= gfc_build_library_function_decl_with_spec (
2844 get_identifier (PREFIX("compare_string")), "..R.R",
2845 integer_type_node
, 4, gfc_charlen_type_node
, pchar1_type_node
,
2846 gfc_charlen_type_node
, pchar1_type_node
);
2847 DECL_PURE_P (gfor_fndecl_compare_string
) = 1;
2848 TREE_NOTHROW (gfor_fndecl_compare_string
) = 1;
2850 gfor_fndecl_concat_string
= gfc_build_library_function_decl_with_spec (
2851 get_identifier (PREFIX("concat_string")), "..W.R.R",
2852 void_type_node
, 6, gfc_charlen_type_node
, pchar1_type_node
,
2853 gfc_charlen_type_node
, pchar1_type_node
,
2854 gfc_charlen_type_node
, pchar1_type_node
);
2855 TREE_NOTHROW (gfor_fndecl_concat_string
) = 1;
2857 gfor_fndecl_string_len_trim
= gfc_build_library_function_decl_with_spec (
2858 get_identifier (PREFIX("string_len_trim")), "..R",
2859 gfc_charlen_type_node
, 2, gfc_charlen_type_node
, pchar1_type_node
);
2860 DECL_PURE_P (gfor_fndecl_string_len_trim
) = 1;
2861 TREE_NOTHROW (gfor_fndecl_string_len_trim
) = 1;
2863 gfor_fndecl_string_index
= gfc_build_library_function_decl_with_spec (
2864 get_identifier (PREFIX("string_index")), "..R.R.",
2865 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
2866 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
2867 DECL_PURE_P (gfor_fndecl_string_index
) = 1;
2868 TREE_NOTHROW (gfor_fndecl_string_index
) = 1;
2870 gfor_fndecl_string_scan
= gfc_build_library_function_decl_with_spec (
2871 get_identifier (PREFIX("string_scan")), "..R.R.",
2872 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
2873 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
2874 DECL_PURE_P (gfor_fndecl_string_scan
) = 1;
2875 TREE_NOTHROW (gfor_fndecl_string_scan
) = 1;
2877 gfor_fndecl_string_verify
= gfc_build_library_function_decl_with_spec (
2878 get_identifier (PREFIX("string_verify")), "..R.R.",
2879 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
2880 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
2881 DECL_PURE_P (gfor_fndecl_string_verify
) = 1;
2882 TREE_NOTHROW (gfor_fndecl_string_verify
) = 1;
2884 gfor_fndecl_string_trim
= gfc_build_library_function_decl_with_spec (
2885 get_identifier (PREFIX("string_trim")), ".Ww.R",
2886 void_type_node
, 4, build_pointer_type (gfc_charlen_type_node
),
2887 build_pointer_type (pchar1_type_node
), gfc_charlen_type_node
,
2890 gfor_fndecl_string_minmax
= gfc_build_library_function_decl_with_spec (
2891 get_identifier (PREFIX("string_minmax")), ".Ww.R",
2892 void_type_node
, -4, build_pointer_type (gfc_charlen_type_node
),
2893 build_pointer_type (pchar1_type_node
), integer_type_node
,
2896 gfor_fndecl_adjustl
= gfc_build_library_function_decl_with_spec (
2897 get_identifier (PREFIX("adjustl")), ".W.R",
2898 void_type_node
, 3, pchar1_type_node
, gfc_charlen_type_node
,
2900 TREE_NOTHROW (gfor_fndecl_adjustl
) = 1;
2902 gfor_fndecl_adjustr
= gfc_build_library_function_decl_with_spec (
2903 get_identifier (PREFIX("adjustr")), ".W.R",
2904 void_type_node
, 3, pchar1_type_node
, gfc_charlen_type_node
,
2906 TREE_NOTHROW (gfor_fndecl_adjustr
) = 1;
2908 gfor_fndecl_select_string
= gfc_build_library_function_decl_with_spec (
2909 get_identifier (PREFIX("select_string")), ".R.R.",
2910 integer_type_node
, 4, pvoid_type_node
, integer_type_node
,
2911 pchar1_type_node
, gfc_charlen_type_node
);
2912 DECL_PURE_P (gfor_fndecl_select_string
) = 1;
2913 TREE_NOTHROW (gfor_fndecl_select_string
) = 1;
2915 gfor_fndecl_compare_string_char4
= gfc_build_library_function_decl_with_spec (
2916 get_identifier (PREFIX("compare_string_char4")), "..R.R",
2917 integer_type_node
, 4, gfc_charlen_type_node
, pchar4_type_node
,
2918 gfc_charlen_type_node
, pchar4_type_node
);
2919 DECL_PURE_P (gfor_fndecl_compare_string_char4
) = 1;
2920 TREE_NOTHROW (gfor_fndecl_compare_string_char4
) = 1;
2922 gfor_fndecl_concat_string_char4
= gfc_build_library_function_decl_with_spec (
2923 get_identifier (PREFIX("concat_string_char4")), "..W.R.R",
2924 void_type_node
, 6, gfc_charlen_type_node
, pchar4_type_node
,
2925 gfc_charlen_type_node
, pchar4_type_node
, gfc_charlen_type_node
,
2927 TREE_NOTHROW (gfor_fndecl_concat_string_char4
) = 1;
2929 gfor_fndecl_string_len_trim_char4
= gfc_build_library_function_decl_with_spec (
2930 get_identifier (PREFIX("string_len_trim_char4")), "..R",
2931 gfc_charlen_type_node
, 2, gfc_charlen_type_node
, pchar4_type_node
);
2932 DECL_PURE_P (gfor_fndecl_string_len_trim_char4
) = 1;
2933 TREE_NOTHROW (gfor_fndecl_string_len_trim_char4
) = 1;
2935 gfor_fndecl_string_index_char4
= gfc_build_library_function_decl_with_spec (
2936 get_identifier (PREFIX("string_index_char4")), "..R.R.",
2937 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
2938 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
2939 DECL_PURE_P (gfor_fndecl_string_index_char4
) = 1;
2940 TREE_NOTHROW (gfor_fndecl_string_index_char4
) = 1;
2942 gfor_fndecl_string_scan_char4
= gfc_build_library_function_decl_with_spec (
2943 get_identifier (PREFIX("string_scan_char4")), "..R.R.",
2944 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
2945 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
2946 DECL_PURE_P (gfor_fndecl_string_scan_char4
) = 1;
2947 TREE_NOTHROW (gfor_fndecl_string_scan_char4
) = 1;
2949 gfor_fndecl_string_verify_char4
= gfc_build_library_function_decl_with_spec (
2950 get_identifier (PREFIX("string_verify_char4")), "..R.R.",
2951 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
2952 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
2953 DECL_PURE_P (gfor_fndecl_string_verify_char4
) = 1;
2954 TREE_NOTHROW (gfor_fndecl_string_verify_char4
) = 1;
2956 gfor_fndecl_string_trim_char4
= gfc_build_library_function_decl_with_spec (
2957 get_identifier (PREFIX("string_trim_char4")), ".Ww.R",
2958 void_type_node
, 4, build_pointer_type (gfc_charlen_type_node
),
2959 build_pointer_type (pchar4_type_node
), gfc_charlen_type_node
,
2962 gfor_fndecl_string_minmax_char4
= gfc_build_library_function_decl_with_spec (
2963 get_identifier (PREFIX("string_minmax_char4")), ".Ww.R",
2964 void_type_node
, -4, build_pointer_type (gfc_charlen_type_node
),
2965 build_pointer_type (pchar4_type_node
), integer_type_node
,
2968 gfor_fndecl_adjustl_char4
= gfc_build_library_function_decl_with_spec (
2969 get_identifier (PREFIX("adjustl_char4")), ".W.R",
2970 void_type_node
, 3, pchar4_type_node
, gfc_charlen_type_node
,
2972 TREE_NOTHROW (gfor_fndecl_adjustl_char4
) = 1;
2974 gfor_fndecl_adjustr_char4
= gfc_build_library_function_decl_with_spec (
2975 get_identifier (PREFIX("adjustr_char4")), ".W.R",
2976 void_type_node
, 3, pchar4_type_node
, gfc_charlen_type_node
,
2978 TREE_NOTHROW (gfor_fndecl_adjustr_char4
) = 1;
2980 gfor_fndecl_select_string_char4
= gfc_build_library_function_decl_with_spec (
2981 get_identifier (PREFIX("select_string_char4")), ".R.R.",
2982 integer_type_node
, 4, pvoid_type_node
, integer_type_node
,
2983 pvoid_type_node
, gfc_charlen_type_node
);
2984 DECL_PURE_P (gfor_fndecl_select_string_char4
) = 1;
2985 TREE_NOTHROW (gfor_fndecl_select_string_char4
) = 1;
2988 /* Conversion between character kinds. */
2990 gfor_fndecl_convert_char1_to_char4
= gfc_build_library_function_decl_with_spec (
2991 get_identifier (PREFIX("convert_char1_to_char4")), ".w.R",
2992 void_type_node
, 3, build_pointer_type (pchar4_type_node
),
2993 gfc_charlen_type_node
, pchar1_type_node
);
2995 gfor_fndecl_convert_char4_to_char1
= gfc_build_library_function_decl_with_spec (
2996 get_identifier (PREFIX("convert_char4_to_char1")), ".w.R",
2997 void_type_node
, 3, build_pointer_type (pchar1_type_node
),
2998 gfc_charlen_type_node
, pchar4_type_node
);
3000 /* Misc. functions. */
3002 gfor_fndecl_ttynam
= gfc_build_library_function_decl_with_spec (
3003 get_identifier (PREFIX("ttynam")), ".W",
3004 void_type_node
, 3, pchar_type_node
, gfc_charlen_type_node
,
3007 gfor_fndecl_fdate
= gfc_build_library_function_decl_with_spec (
3008 get_identifier (PREFIX("fdate")), ".W",
3009 void_type_node
, 2, pchar_type_node
, gfc_charlen_type_node
);
3011 gfor_fndecl_ctime
= gfc_build_library_function_decl_with_spec (
3012 get_identifier (PREFIX("ctime")), ".W",
3013 void_type_node
, 3, pchar_type_node
, gfc_charlen_type_node
,
3014 gfc_int8_type_node
);
3016 gfor_fndecl_sc_kind
= gfc_build_library_function_decl_with_spec (
3017 get_identifier (PREFIX("selected_char_kind")), "..R",
3018 gfc_int4_type_node
, 2, gfc_charlen_type_node
, pchar_type_node
);
3019 DECL_PURE_P (gfor_fndecl_sc_kind
) = 1;
3020 TREE_NOTHROW (gfor_fndecl_sc_kind
) = 1;
3022 gfor_fndecl_si_kind
= gfc_build_library_function_decl_with_spec (
3023 get_identifier (PREFIX("selected_int_kind")), ".R",
3024 gfc_int4_type_node
, 1, pvoid_type_node
);
3025 DECL_PURE_P (gfor_fndecl_si_kind
) = 1;
3026 TREE_NOTHROW (gfor_fndecl_si_kind
) = 1;
3028 gfor_fndecl_sr_kind
= gfc_build_library_function_decl_with_spec (
3029 get_identifier (PREFIX("selected_real_kind2008")), ".RR",
3030 gfc_int4_type_node
, 3, pvoid_type_node
, pvoid_type_node
,
3032 DECL_PURE_P (gfor_fndecl_sr_kind
) = 1;
3033 TREE_NOTHROW (gfor_fndecl_sr_kind
) = 1;
3035 gfor_fndecl_system_clock4
= gfc_build_library_function_decl (
3036 get_identifier (PREFIX("system_clock_4")),
3037 void_type_node
, 3, gfc_pint4_type_node
, gfc_pint4_type_node
,
3038 gfc_pint4_type_node
);
3040 gfor_fndecl_system_clock8
= gfc_build_library_function_decl (
3041 get_identifier (PREFIX("system_clock_8")),
3042 void_type_node
, 3, gfc_pint8_type_node
, gfc_pint8_type_node
,
3043 gfc_pint8_type_node
);
3045 /* Power functions. */
3047 tree ctype
, rtype
, itype
, jtype
;
3048 int rkind
, ikind
, jkind
;
3051 static int ikinds
[NIKINDS
] = {4, 8, 16};
3052 static int rkinds
[NRKINDS
] = {4, 8, 10, 16};
3053 char name
[PREFIX_LEN
+ 12]; /* _gfortran_pow_?n_?n */
3055 for (ikind
=0; ikind
< NIKINDS
; ikind
++)
3057 itype
= gfc_get_int_type (ikinds
[ikind
]);
3059 for (jkind
=0; jkind
< NIKINDS
; jkind
++)
3061 jtype
= gfc_get_int_type (ikinds
[jkind
]);
3064 sprintf(name
, PREFIX("pow_i%d_i%d"), ikinds
[ikind
],
3066 gfor_fndecl_math_powi
[jkind
][ikind
].integer
=
3067 gfc_build_library_function_decl (get_identifier (name
),
3068 jtype
, 2, jtype
, itype
);
3069 TREE_READONLY (gfor_fndecl_math_powi
[jkind
][ikind
].integer
) = 1;
3070 TREE_NOTHROW (gfor_fndecl_math_powi
[jkind
][ikind
].integer
) = 1;
3074 for (rkind
= 0; rkind
< NRKINDS
; rkind
++)
3076 rtype
= gfc_get_real_type (rkinds
[rkind
]);
3079 sprintf(name
, PREFIX("pow_r%d_i%d"), rkinds
[rkind
],
3081 gfor_fndecl_math_powi
[rkind
][ikind
].real
=
3082 gfc_build_library_function_decl (get_identifier (name
),
3083 rtype
, 2, rtype
, itype
);
3084 TREE_READONLY (gfor_fndecl_math_powi
[rkind
][ikind
].real
) = 1;
3085 TREE_NOTHROW (gfor_fndecl_math_powi
[rkind
][ikind
].real
) = 1;
3088 ctype
= gfc_get_complex_type (rkinds
[rkind
]);
3091 sprintf(name
, PREFIX("pow_c%d_i%d"), rkinds
[rkind
],
3093 gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
=
3094 gfc_build_library_function_decl (get_identifier (name
),
3095 ctype
, 2,ctype
, itype
);
3096 TREE_READONLY (gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
) = 1;
3097 TREE_NOTHROW (gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
) = 1;
3105 gfor_fndecl_math_ishftc4
= gfc_build_library_function_decl (
3106 get_identifier (PREFIX("ishftc4")),
3107 gfc_int4_type_node
, 3, gfc_int4_type_node
, gfc_int4_type_node
,
3108 gfc_int4_type_node
);
3109 TREE_READONLY (gfor_fndecl_math_ishftc4
) = 1;
3110 TREE_NOTHROW (gfor_fndecl_math_ishftc4
) = 1;
3112 gfor_fndecl_math_ishftc8
= gfc_build_library_function_decl (
3113 get_identifier (PREFIX("ishftc8")),
3114 gfc_int8_type_node
, 3, gfc_int8_type_node
, gfc_int4_type_node
,
3115 gfc_int4_type_node
);
3116 TREE_READONLY (gfor_fndecl_math_ishftc8
) = 1;
3117 TREE_NOTHROW (gfor_fndecl_math_ishftc8
) = 1;
3119 if (gfc_int16_type_node
)
3121 gfor_fndecl_math_ishftc16
= gfc_build_library_function_decl (
3122 get_identifier (PREFIX("ishftc16")),
3123 gfc_int16_type_node
, 3, gfc_int16_type_node
, gfc_int4_type_node
,
3124 gfc_int4_type_node
);
3125 TREE_READONLY (gfor_fndecl_math_ishftc16
) = 1;
3126 TREE_NOTHROW (gfor_fndecl_math_ishftc16
) = 1;
3129 /* BLAS functions. */
3131 tree pint
= build_pointer_type (integer_type_node
);
3132 tree ps
= build_pointer_type (gfc_get_real_type (gfc_default_real_kind
));
3133 tree pd
= build_pointer_type (gfc_get_real_type (gfc_default_double_kind
));
3134 tree pc
= build_pointer_type (gfc_get_complex_type (gfc_default_real_kind
));
3135 tree pz
= build_pointer_type
3136 (gfc_get_complex_type (gfc_default_double_kind
));
3138 gfor_fndecl_sgemm
= gfc_build_library_function_decl
3140 (gfc_option
.flag_underscoring
? "sgemm_"
3142 void_type_node
, 15, pchar_type_node
,
3143 pchar_type_node
, pint
, pint
, pint
, ps
, ps
, pint
,
3144 ps
, pint
, ps
, ps
, pint
, integer_type_node
,
3146 gfor_fndecl_dgemm
= gfc_build_library_function_decl
3148 (gfc_option
.flag_underscoring
? "dgemm_"
3150 void_type_node
, 15, pchar_type_node
,
3151 pchar_type_node
, pint
, pint
, pint
, pd
, pd
, pint
,
3152 pd
, pint
, pd
, pd
, pint
, integer_type_node
,
3154 gfor_fndecl_cgemm
= gfc_build_library_function_decl
3156 (gfc_option
.flag_underscoring
? "cgemm_"
3158 void_type_node
, 15, pchar_type_node
,
3159 pchar_type_node
, pint
, pint
, pint
, pc
, pc
, pint
,
3160 pc
, pint
, pc
, pc
, pint
, integer_type_node
,
3162 gfor_fndecl_zgemm
= gfc_build_library_function_decl
3164 (gfc_option
.flag_underscoring
? "zgemm_"
3166 void_type_node
, 15, pchar_type_node
,
3167 pchar_type_node
, pint
, pint
, pint
, pz
, pz
, pint
,
3168 pz
, pint
, pz
, pz
, pint
, integer_type_node
,
3172 /* Other functions. */
3173 gfor_fndecl_size0
= gfc_build_library_function_decl_with_spec (
3174 get_identifier (PREFIX("size0")), ".R",
3175 gfc_array_index_type
, 1, pvoid_type_node
);
3176 DECL_PURE_P (gfor_fndecl_size0
) = 1;
3177 TREE_NOTHROW (gfor_fndecl_size0
) = 1;
3179 gfor_fndecl_size1
= gfc_build_library_function_decl_with_spec (
3180 get_identifier (PREFIX("size1")), ".R",
3181 gfc_array_index_type
, 2, pvoid_type_node
, gfc_array_index_type
);
3182 DECL_PURE_P (gfor_fndecl_size1
) = 1;
3183 TREE_NOTHROW (gfor_fndecl_size1
) = 1;
3185 gfor_fndecl_iargc
= gfc_build_library_function_decl (
3186 get_identifier (PREFIX ("iargc")), gfc_int4_type_node
, 0);
3187 TREE_NOTHROW (gfor_fndecl_iargc
) = 1;
3191 /* Make prototypes for runtime library functions. */
3194 gfc_build_builtin_function_decls (void)
3196 tree gfc_int4_type_node
= gfc_get_int_type (4);
3198 gfor_fndecl_stop_numeric
= gfc_build_library_function_decl (
3199 get_identifier (PREFIX("stop_numeric")),
3200 void_type_node
, 1, gfc_int4_type_node
);
3201 /* STOP doesn't return. */
3202 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric
) = 1;
3204 gfor_fndecl_stop_numeric_f08
= gfc_build_library_function_decl (
3205 get_identifier (PREFIX("stop_numeric_f08")),
3206 void_type_node
, 1, gfc_int4_type_node
);
3207 /* STOP doesn't return. */
3208 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric_f08
) = 1;
3210 gfor_fndecl_stop_string
= gfc_build_library_function_decl_with_spec (
3211 get_identifier (PREFIX("stop_string")), ".R.",
3212 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
3213 /* STOP doesn't return. */
3214 TREE_THIS_VOLATILE (gfor_fndecl_stop_string
) = 1;
3216 gfor_fndecl_error_stop_numeric
= gfc_build_library_function_decl (
3217 get_identifier (PREFIX("error_stop_numeric")),
3218 void_type_node
, 1, gfc_int4_type_node
);
3219 /* ERROR STOP doesn't return. */
3220 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_numeric
) = 1;
3222 gfor_fndecl_error_stop_string
= gfc_build_library_function_decl_with_spec (
3223 get_identifier (PREFIX("error_stop_string")), ".R.",
3224 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
3225 /* ERROR STOP doesn't return. */
3226 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_string
) = 1;
3228 gfor_fndecl_pause_numeric
= gfc_build_library_function_decl (
3229 get_identifier (PREFIX("pause_numeric")),
3230 void_type_node
, 1, gfc_int4_type_node
);
3232 gfor_fndecl_pause_string
= gfc_build_library_function_decl_with_spec (
3233 get_identifier (PREFIX("pause_string")), ".R.",
3234 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
3236 gfor_fndecl_runtime_error
= gfc_build_library_function_decl_with_spec (
3237 get_identifier (PREFIX("runtime_error")), ".R",
3238 void_type_node
, -1, pchar_type_node
);
3239 /* The runtime_error function does not return. */
3240 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error
) = 1;
3242 gfor_fndecl_runtime_error_at
= gfc_build_library_function_decl_with_spec (
3243 get_identifier (PREFIX("runtime_error_at")), ".RR",
3244 void_type_node
, -2, pchar_type_node
, pchar_type_node
);
3245 /* The runtime_error_at function does not return. */
3246 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at
) = 1;
3248 gfor_fndecl_runtime_warning_at
= gfc_build_library_function_decl_with_spec (
3249 get_identifier (PREFIX("runtime_warning_at")), ".RR",
3250 void_type_node
, -2, pchar_type_node
, pchar_type_node
);
3252 gfor_fndecl_generate_error
= gfc_build_library_function_decl_with_spec (
3253 get_identifier (PREFIX("generate_error")), ".R.R",
3254 void_type_node
, 3, pvoid_type_node
, integer_type_node
,
3257 gfor_fndecl_os_error
= gfc_build_library_function_decl_with_spec (
3258 get_identifier (PREFIX("os_error")), ".R",
3259 void_type_node
, 1, pchar_type_node
);
3260 /* The runtime_error function does not return. */
3261 TREE_THIS_VOLATILE (gfor_fndecl_os_error
) = 1;
3263 gfor_fndecl_set_args
= gfc_build_library_function_decl (
3264 get_identifier (PREFIX("set_args")),
3265 void_type_node
, 2, integer_type_node
,
3266 build_pointer_type (pchar_type_node
));
3268 gfor_fndecl_set_fpe
= gfc_build_library_function_decl (
3269 get_identifier (PREFIX("set_fpe")),
3270 void_type_node
, 1, integer_type_node
);
3272 /* Keep the array dimension in sync with the call, later in this file. */
3273 gfor_fndecl_set_options
= gfc_build_library_function_decl_with_spec (
3274 get_identifier (PREFIX("set_options")), "..R",
3275 void_type_node
, 2, integer_type_node
,
3276 build_pointer_type (integer_type_node
));
3278 gfor_fndecl_set_convert
= gfc_build_library_function_decl (
3279 get_identifier (PREFIX("set_convert")),
3280 void_type_node
, 1, integer_type_node
);
3282 gfor_fndecl_set_record_marker
= gfc_build_library_function_decl (
3283 get_identifier (PREFIX("set_record_marker")),
3284 void_type_node
, 1, integer_type_node
);
3286 gfor_fndecl_set_max_subrecord_length
= gfc_build_library_function_decl (
3287 get_identifier (PREFIX("set_max_subrecord_length")),
3288 void_type_node
, 1, integer_type_node
);
3290 gfor_fndecl_in_pack
= gfc_build_library_function_decl_with_spec (
3291 get_identifier (PREFIX("internal_pack")), ".r",
3292 pvoid_type_node
, 1, pvoid_type_node
);
3294 gfor_fndecl_in_unpack
= gfc_build_library_function_decl_with_spec (
3295 get_identifier (PREFIX("internal_unpack")), ".wR",
3296 void_type_node
, 2, pvoid_type_node
, pvoid_type_node
);
3298 gfor_fndecl_associated
= gfc_build_library_function_decl_with_spec (
3299 get_identifier (PREFIX("associated")), ".RR",
3300 integer_type_node
, 2, ppvoid_type_node
, ppvoid_type_node
);
3301 DECL_PURE_P (gfor_fndecl_associated
) = 1;
3302 TREE_NOTHROW (gfor_fndecl_associated
) = 1;
3304 /* Coarray library calls. */
3305 if (gfc_option
.coarray
== GFC_FCOARRAY_LIB
)
3307 tree pint_type
, pppchar_type
;
3309 pint_type
= build_pointer_type (integer_type_node
);
3311 = build_pointer_type (build_pointer_type (pchar_type_node
));
3313 gfor_fndecl_caf_init
= gfc_build_library_function_decl (
3314 get_identifier (PREFIX("caf_init")), void_type_node
,
3315 2, pint_type
, pppchar_type
);
3317 gfor_fndecl_caf_finalize
= gfc_build_library_function_decl (
3318 get_identifier (PREFIX("caf_finalize")), void_type_node
, 0);
3320 gfor_fndecl_caf_this_image
= gfc_build_library_function_decl (
3321 get_identifier (PREFIX("caf_this_image")), integer_type_node
,
3322 1, integer_type_node
);
3324 gfor_fndecl_caf_num_images
= gfc_build_library_function_decl (
3325 get_identifier (PREFIX("caf_num_images")), integer_type_node
,
3326 2, integer_type_node
, integer_type_node
);
3328 gfor_fndecl_caf_register
= gfc_build_library_function_decl_with_spec (
3329 get_identifier (PREFIX("caf_register")), "...WWW", pvoid_type_node
, 6,
3330 size_type_node
, integer_type_node
, ppvoid_type_node
, pint_type
,
3331 pchar_type_node
, integer_type_node
);
3333 gfor_fndecl_caf_deregister
= gfc_build_library_function_decl_with_spec (
3334 get_identifier (PREFIX("caf_deregister")), ".WWW", void_type_node
, 4,
3335 ppvoid_type_node
, pint_type
, pchar_type_node
, integer_type_node
);
3337 gfor_fndecl_caf_get
= gfc_build_library_function_decl_with_spec (
3338 get_identifier (PREFIX("caf_get")), ".R.RRRW", void_type_node
, 8,
3339 pvoid_type_node
, size_type_node
, integer_type_node
, pvoid_type_node
,
3340 pvoid_type_node
, pvoid_type_node
, integer_type_node
, integer_type_node
);
3342 gfor_fndecl_caf_send
= gfc_build_library_function_decl_with_spec (
3343 get_identifier (PREFIX("caf_send")), ".R.RRRR", void_type_node
, 8,
3344 pvoid_type_node
, size_type_node
, integer_type_node
, pvoid_type_node
,
3345 pvoid_type_node
, pvoid_type_node
, integer_type_node
, integer_type_node
);
3347 gfor_fndecl_caf_sendget
= gfc_build_library_function_decl_with_spec (
3348 get_identifier (PREFIX("caf_sendget")), ".R.RRRR.RRR", void_type_node
,
3349 12, pvoid_type_node
, size_type_node
, integer_type_node
, pvoid_type_node
,
3350 pvoid_type_node
, pvoid_type_node
, size_type_node
, integer_type_node
,
3351 pvoid_type_node
, pvoid_type_node
, integer_type_node
, integer_type_node
);
3353 gfor_fndecl_caf_critical
= gfc_build_library_function_decl (
3354 get_identifier (PREFIX("caf_critical")), void_type_node
, 0);
3356 gfor_fndecl_caf_end_critical
= gfc_build_library_function_decl (
3357 get_identifier (PREFIX("caf_end_critical")), void_type_node
, 0);
3359 gfor_fndecl_caf_sync_all
= gfc_build_library_function_decl_with_spec (
3360 get_identifier (PREFIX("caf_sync_all")), ".WW", void_type_node
,
3361 3, pint_type
, pchar_type_node
, integer_type_node
);
3363 gfor_fndecl_caf_sync_images
= gfc_build_library_function_decl_with_spec (
3364 get_identifier (PREFIX("caf_sync_images")), ".RRWW", void_type_node
,
3365 5, integer_type_node
, pint_type
, pint_type
,
3366 pchar_type_node
, integer_type_node
);
3368 gfor_fndecl_caf_error_stop
= gfc_build_library_function_decl (
3369 get_identifier (PREFIX("caf_error_stop")),
3370 void_type_node
, 1, gfc_int4_type_node
);
3371 /* CAF's ERROR STOP doesn't return. */
3372 TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop
) = 1;
3374 gfor_fndecl_caf_error_stop_str
= gfc_build_library_function_decl_with_spec (
3375 get_identifier (PREFIX("caf_error_stop_str")), ".R.",
3376 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
3377 /* CAF's ERROR STOP doesn't return. */
3378 TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop_str
) = 1;
3380 gfor_fndecl_co_max
= gfc_build_library_function_decl_with_spec (
3381 get_identifier (PREFIX("caf_co_max")), "W.WW",
3382 void_type_node
, 6, pvoid_type_node
, integer_type_node
,
3383 pint_type
, pchar_type_node
, integer_type_node
, integer_type_node
);
3385 gfor_fndecl_co_min
= gfc_build_library_function_decl_with_spec (
3386 get_identifier (PREFIX("caf_co_min")), "W.WW",
3387 void_type_node
, 6, pvoid_type_node
, integer_type_node
,
3388 pint_type
, pchar_type_node
, integer_type_node
, integer_type_node
);
3390 gfor_fndecl_co_sum
= gfc_build_library_function_decl_with_spec (
3391 get_identifier (PREFIX("caf_co_sum")), "W.WW",
3392 void_type_node
, 5, pvoid_type_node
, integer_type_node
,
3393 pint_type
, pchar_type_node
, integer_type_node
);
3396 gfc_build_intrinsic_function_decls ();
3397 gfc_build_intrinsic_lib_fndecls ();
3398 gfc_build_io_library_fndecls ();
3402 /* Evaluate the length of dummy character variables. */
3405 gfc_trans_dummy_character (gfc_symbol
*sym
, gfc_charlen
*cl
,
3406 gfc_wrapped_block
*block
)
3410 gfc_finish_decl (cl
->backend_decl
);
3412 gfc_start_block (&init
);
3414 /* Evaluate the string length expression. */
3415 gfc_conv_string_length (cl
, NULL
, &init
);
3417 gfc_trans_vla_type_sizes (sym
, &init
);
3419 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
3423 /* Allocate and cleanup an automatic character variable. */
3426 gfc_trans_auto_character_variable (gfc_symbol
* sym
, gfc_wrapped_block
* block
)
3432 gcc_assert (sym
->backend_decl
);
3433 gcc_assert (sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
);
3435 gfc_init_block (&init
);
3437 /* Evaluate the string length expression. */
3438 gfc_conv_string_length (sym
->ts
.u
.cl
, NULL
, &init
);
3440 gfc_trans_vla_type_sizes (sym
, &init
);
3442 decl
= sym
->backend_decl
;
3444 /* Emit a DECL_EXPR for this variable, which will cause the
3445 gimplifier to allocate storage, and all that good stuff. */
3446 tmp
= fold_build1_loc (input_location
, DECL_EXPR
, TREE_TYPE (decl
), decl
);
3447 gfc_add_expr_to_block (&init
, tmp
);
3449 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
3452 /* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
3455 gfc_trans_assign_aux_var (gfc_symbol
* sym
, gfc_wrapped_block
* block
)
3459 gcc_assert (sym
->backend_decl
);
3460 gfc_start_block (&init
);
3462 /* Set the initial value to length. See the comments in
3463 function gfc_add_assign_aux_vars in this file. */
3464 gfc_add_modify (&init
, GFC_DECL_STRING_LEN (sym
->backend_decl
),
3465 build_int_cst (gfc_charlen_type_node
, -2));
3467 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
3471 gfc_trans_vla_one_sizepos (tree
*tp
, stmtblock_t
*body
)
3473 tree t
= *tp
, var
, val
;
3475 if (t
== NULL
|| t
== error_mark_node
)
3477 if (TREE_CONSTANT (t
) || DECL_P (t
))
3480 if (TREE_CODE (t
) == SAVE_EXPR
)
3482 if (SAVE_EXPR_RESOLVED_P (t
))
3484 *tp
= TREE_OPERAND (t
, 0);
3487 val
= TREE_OPERAND (t
, 0);
3492 var
= gfc_create_var_np (TREE_TYPE (t
), NULL
);
3493 gfc_add_decl_to_function (var
);
3494 gfc_add_modify (body
, var
, val
);
3495 if (TREE_CODE (t
) == SAVE_EXPR
)
3496 TREE_OPERAND (t
, 0) = var
;
3501 gfc_trans_vla_type_sizes_1 (tree type
, stmtblock_t
*body
)
3505 if (type
== NULL
|| type
== error_mark_node
)
3508 type
= TYPE_MAIN_VARIANT (type
);
3510 if (TREE_CODE (type
) == INTEGER_TYPE
)
3512 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type
), body
);
3513 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type
), body
);
3515 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
3517 TYPE_MIN_VALUE (t
) = TYPE_MIN_VALUE (type
);
3518 TYPE_MAX_VALUE (t
) = TYPE_MAX_VALUE (type
);
3521 else if (TREE_CODE (type
) == ARRAY_TYPE
)
3523 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type
), body
);
3524 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type
), body
);
3525 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type
), body
);
3526 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type
), body
);
3528 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
3530 TYPE_SIZE (t
) = TYPE_SIZE (type
);
3531 TYPE_SIZE_UNIT (t
) = TYPE_SIZE_UNIT (type
);
3536 /* Make sure all type sizes and array domains are either constant,
3537 or variable or parameter decls. This is a simplified variant
3538 of gimplify_type_sizes, but we can't use it here, as none of the
3539 variables in the expressions have been gimplified yet.
3540 As type sizes and domains for various variable length arrays
3541 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
3542 time, without this routine gimplify_type_sizes in the middle-end
3543 could result in the type sizes being gimplified earlier than where
3544 those variables are initialized. */
3547 gfc_trans_vla_type_sizes (gfc_symbol
*sym
, stmtblock_t
*body
)
3549 tree type
= TREE_TYPE (sym
->backend_decl
);
3551 if (TREE_CODE (type
) == FUNCTION_TYPE
3552 && (sym
->attr
.function
|| sym
->attr
.result
|| sym
->attr
.entry
))
3554 if (! current_fake_result_decl
)
3557 type
= TREE_TYPE (TREE_VALUE (current_fake_result_decl
));
3560 while (POINTER_TYPE_P (type
))
3561 type
= TREE_TYPE (type
);
3563 if (GFC_DESCRIPTOR_TYPE_P (type
))
3565 tree etype
= GFC_TYPE_ARRAY_DATAPTR_TYPE (type
);
3567 while (POINTER_TYPE_P (etype
))
3568 etype
= TREE_TYPE (etype
);
3570 gfc_trans_vla_type_sizes_1 (etype
, body
);
3573 gfc_trans_vla_type_sizes_1 (type
, body
);
3577 /* Initialize a derived type by building an lvalue from the symbol
3578 and using trans_assignment to do the work. Set dealloc to false
3579 if no deallocation prior the assignment is needed. */
3581 gfc_init_default_dt (gfc_symbol
* sym
, stmtblock_t
* block
, bool dealloc
)
3589 gcc_assert (!sym
->attr
.allocatable
);
3590 gfc_set_sym_referenced (sym
);
3591 e
= gfc_lval_expr_from_sym (sym
);
3592 tmp
= gfc_trans_assignment (e
, sym
->value
, false, dealloc
);
3593 if (sym
->attr
.dummy
&& (sym
->attr
.optional
3594 || sym
->ns
->proc_name
->attr
.entry_master
))
3596 present
= gfc_conv_expr_present (sym
);
3597 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
), present
,
3598 tmp
, build_empty_stmt (input_location
));
3600 gfc_add_expr_to_block (block
, tmp
);
3605 /* Initialize INTENT(OUT) derived type dummies. As well as giving
3606 them their default initializer, if they do not have allocatable
3607 components, they have their allocatable components deallocated. */
3610 init_intent_out_dt (gfc_symbol
* proc_sym
, gfc_wrapped_block
* block
)
3613 gfc_formal_arglist
*f
;
3617 gfc_init_block (&init
);
3618 for (f
= gfc_sym_get_dummy_args (proc_sym
); f
; f
= f
->next
)
3619 if (f
->sym
&& f
->sym
->attr
.intent
== INTENT_OUT
3620 && !f
->sym
->attr
.pointer
3621 && f
->sym
->ts
.type
== BT_DERIVED
)
3625 /* Note: Allocatables are excluded as they are already handled
3627 if (!f
->sym
->attr
.allocatable
3628 && gfc_is_finalizable (f
->sym
->ts
.u
.derived
, NULL
))
3633 gfc_init_block (&block
);
3634 f
->sym
->attr
.referenced
= 1;
3635 e
= gfc_lval_expr_from_sym (f
->sym
);
3636 gfc_add_finalizer_call (&block
, e
);
3638 tmp
= gfc_finish_block (&block
);
3641 if (tmp
== NULL_TREE
&& !f
->sym
->attr
.allocatable
3642 && f
->sym
->ts
.u
.derived
->attr
.alloc_comp
&& !f
->sym
->value
)
3643 tmp
= gfc_deallocate_alloc_comp (f
->sym
->ts
.u
.derived
,
3644 f
->sym
->backend_decl
,
3645 f
->sym
->as
? f
->sym
->as
->rank
: 0);
3647 if (tmp
!= NULL_TREE
&& (f
->sym
->attr
.optional
3648 || f
->sym
->ns
->proc_name
->attr
.entry_master
))
3650 present
= gfc_conv_expr_present (f
->sym
);
3651 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
),
3652 present
, tmp
, build_empty_stmt (input_location
));
3655 if (tmp
!= NULL_TREE
)
3656 gfc_add_expr_to_block (&init
, tmp
);
3657 else if (f
->sym
->value
&& !f
->sym
->attr
.allocatable
)
3658 gfc_init_default_dt (f
->sym
, &init
, true);
3660 else if (f
->sym
&& f
->sym
->attr
.intent
== INTENT_OUT
3661 && f
->sym
->ts
.type
== BT_CLASS
3662 && !CLASS_DATA (f
->sym
)->attr
.class_pointer
3663 && !CLASS_DATA (f
->sym
)->attr
.allocatable
)
3668 gfc_init_block (&block
);
3669 f
->sym
->attr
.referenced
= 1;
3670 e
= gfc_lval_expr_from_sym (f
->sym
);
3671 gfc_add_finalizer_call (&block
, e
);
3673 tmp
= gfc_finish_block (&block
);
3675 if (f
->sym
->attr
.optional
|| f
->sym
->ns
->proc_name
->attr
.entry_master
)
3677 present
= gfc_conv_expr_present (f
->sym
);
3678 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
),
3680 build_empty_stmt (input_location
));
3683 gfc_add_expr_to_block (&init
, tmp
);
3686 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
3690 /* Generate function entry and exit code, and add it to the function body.
3692 Allocation and initialization of array variables.
3693 Allocation of character string variables.
3694 Initialization and possibly repacking of dummy arrays.
3695 Initialization of ASSIGN statement auxiliary variable.
3696 Initialization of ASSOCIATE names.
3697 Automatic deallocation. */
3700 gfc_trans_deferred_vars (gfc_symbol
* proc_sym
, gfc_wrapped_block
* block
)
3704 gfc_formal_arglist
*f
;
3705 stmtblock_t tmpblock
;
3706 bool seen_trans_deferred_array
= false;
3712 /* Deal with implicit return variables. Explicit return variables will
3713 already have been added. */
3714 if (gfc_return_by_reference (proc_sym
) && proc_sym
->result
== proc_sym
)
3716 if (!current_fake_result_decl
)
3718 gfc_entry_list
*el
= NULL
;
3719 if (proc_sym
->attr
.entry_master
)
3721 for (el
= proc_sym
->ns
->entries
; el
; el
= el
->next
)
3722 if (el
->sym
!= el
->sym
->result
)
3725 /* TODO: move to the appropriate place in resolve.c. */
3726 if (warn_return_type
&& el
== NULL
)
3727 gfc_warning ("Return value of function '%s' at %L not set",
3728 proc_sym
->name
, &proc_sym
->declared_at
);
3730 else if (proc_sym
->as
)
3732 tree result
= TREE_VALUE (current_fake_result_decl
);
3733 gfc_trans_dummy_array_bias (proc_sym
, result
, block
);
3735 /* An automatic character length, pointer array result. */
3736 if (proc_sym
->ts
.type
== BT_CHARACTER
3737 && TREE_CODE (proc_sym
->ts
.u
.cl
->backend_decl
) == VAR_DECL
)
3738 gfc_trans_dummy_character (proc_sym
, proc_sym
->ts
.u
.cl
, block
);
3740 else if (proc_sym
->ts
.type
== BT_CHARACTER
)
3742 if (proc_sym
->ts
.deferred
)
3745 gfc_save_backend_locus (&loc
);
3746 gfc_set_backend_locus (&proc_sym
->declared_at
);
3747 gfc_start_block (&init
);
3748 /* Zero the string length on entry. */
3749 gfc_add_modify (&init
, proc_sym
->ts
.u
.cl
->backend_decl
,
3750 build_int_cst (gfc_charlen_type_node
, 0));
3751 /* Null the pointer. */
3752 e
= gfc_lval_expr_from_sym (proc_sym
);
3753 gfc_init_se (&se
, NULL
);
3754 se
.want_pointer
= 1;
3755 gfc_conv_expr (&se
, e
);
3758 gfc_add_modify (&init
, tmp
,
3759 fold_convert (TREE_TYPE (se
.expr
),
3760 null_pointer_node
));
3761 gfc_restore_backend_locus (&loc
);
3763 /* Pass back the string length on exit. */
3764 tmp
= proc_sym
->ts
.u
.cl
->passed_length
;
3765 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
3766 tmp
= fold_convert (gfc_charlen_type_node
, tmp
);
3767 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
3768 gfc_charlen_type_node
, tmp
,
3769 proc_sym
->ts
.u
.cl
->backend_decl
);
3770 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
3772 else if (TREE_CODE (proc_sym
->ts
.u
.cl
->backend_decl
) == VAR_DECL
)
3773 gfc_trans_dummy_character (proc_sym
, proc_sym
->ts
.u
.cl
, block
);
3776 gcc_assert (gfc_option
.flag_f2c
3777 && proc_sym
->ts
.type
== BT_COMPLEX
);
3780 /* Initialize the INTENT(OUT) derived type dummy arguments. This
3781 should be done here so that the offsets and lbounds of arrays
3783 gfc_save_backend_locus (&loc
);
3784 gfc_set_backend_locus (&proc_sym
->declared_at
);
3785 init_intent_out_dt (proc_sym
, block
);
3786 gfc_restore_backend_locus (&loc
);
3788 for (sym
= proc_sym
->tlink
; sym
!= proc_sym
; sym
= sym
->tlink
)
3790 bool alloc_comp_or_fini
= (sym
->ts
.type
== BT_DERIVED
)
3791 && (sym
->ts
.u
.derived
->attr
.alloc_comp
3792 || gfc_is_finalizable (sym
->ts
.u
.derived
,
3797 if (sym
->attr
.subref_array_pointer
3798 && GFC_DECL_SPAN (sym
->backend_decl
)
3799 && !TREE_STATIC (GFC_DECL_SPAN (sym
->backend_decl
)))
3801 gfc_init_block (&tmpblock
);
3802 gfc_add_modify (&tmpblock
, GFC_DECL_SPAN (sym
->backend_decl
),
3803 build_int_cst (gfc_array_index_type
, 0));
3804 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
),
3808 if (sym
->ts
.type
== BT_CLASS
3809 && (sym
->attr
.save
|| gfc_option
.flag_max_stack_var_size
== 0)
3810 && CLASS_DATA (sym
)->attr
.allocatable
)
3814 if (UNLIMITED_POLY (sym
))
3815 vptr
= null_pointer_node
;
3819 vsym
= gfc_find_derived_vtab (sym
->ts
.u
.derived
);
3820 vptr
= gfc_get_symbol_decl (vsym
);
3821 vptr
= gfc_build_addr_expr (NULL
, vptr
);
3824 if (CLASS_DATA (sym
)->attr
.dimension
3825 || (CLASS_DATA (sym
)->attr
.codimension
3826 && gfc_option
.coarray
!= GFC_FCOARRAY_LIB
))
3828 tmp
= gfc_class_data_get (sym
->backend_decl
);
3829 tmp
= gfc_build_null_descriptor (TREE_TYPE (tmp
));
3832 tmp
= null_pointer_node
;
3834 DECL_INITIAL (sym
->backend_decl
)
3835 = gfc_class_set_static_fields (sym
->backend_decl
, vptr
, tmp
);
3836 TREE_CONSTANT (DECL_INITIAL (sym
->backend_decl
)) = 1;
3838 else if (sym
->attr
.dimension
|| sym
->attr
.codimension
)
3840 /* Assumed-size Cray pointees need to be treated as AS_EXPLICIT. */
3841 array_type tmp
= sym
->as
->type
;
3842 if (tmp
== AS_ASSUMED_SIZE
&& sym
->as
->cp_was_assumed
)
3847 if (sym
->attr
.dummy
|| sym
->attr
.result
)
3848 gfc_trans_dummy_array_bias (sym
, sym
->backend_decl
, block
);
3849 else if (sym
->attr
.pointer
|| sym
->attr
.allocatable
)
3851 if (TREE_STATIC (sym
->backend_decl
))
3853 gfc_save_backend_locus (&loc
);
3854 gfc_set_backend_locus (&sym
->declared_at
);
3855 gfc_trans_static_array_pointer (sym
);
3856 gfc_restore_backend_locus (&loc
);
3860 seen_trans_deferred_array
= true;
3861 gfc_trans_deferred_array (sym
, block
);
3864 else if (sym
->attr
.codimension
&& TREE_STATIC (sym
->backend_decl
))
3866 gfc_init_block (&tmpblock
);
3867 gfc_trans_array_cobounds (TREE_TYPE (sym
->backend_decl
),
3869 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
),
3873 else if (gfc_option
.coarray
!= GFC_FCOARRAY_LIB
)
3875 gfc_save_backend_locus (&loc
);
3876 gfc_set_backend_locus (&sym
->declared_at
);
3878 if (alloc_comp_or_fini
)
3880 seen_trans_deferred_array
= true;
3881 gfc_trans_deferred_array (sym
, block
);
3883 else if (sym
->ts
.type
== BT_DERIVED
3886 && sym
->attr
.save
== SAVE_NONE
)
3888 gfc_start_block (&tmpblock
);
3889 gfc_init_default_dt (sym
, &tmpblock
, false);
3890 gfc_add_init_cleanup (block
,
3891 gfc_finish_block (&tmpblock
),
3895 gfc_trans_auto_array_allocation (sym
->backend_decl
,
3897 gfc_restore_backend_locus (&loc
);
3901 case AS_ASSUMED_SIZE
:
3902 /* Must be a dummy parameter. */
3903 gcc_assert (sym
->attr
.dummy
|| sym
->as
->cp_was_assumed
);
3905 /* We should always pass assumed size arrays the g77 way. */
3906 if (sym
->attr
.dummy
)
3907 gfc_trans_g77_array (sym
, block
);
3910 case AS_ASSUMED_SHAPE
:
3911 /* Must be a dummy parameter. */
3912 gcc_assert (sym
->attr
.dummy
);
3914 gfc_trans_dummy_array_bias (sym
, sym
->backend_decl
, block
);
3917 case AS_ASSUMED_RANK
:
3919 seen_trans_deferred_array
= true;
3920 gfc_trans_deferred_array (sym
, block
);
3926 if (alloc_comp_or_fini
&& !seen_trans_deferred_array
)
3927 gfc_trans_deferred_array (sym
, block
);
3929 else if ((!sym
->attr
.dummy
|| sym
->ts
.deferred
)
3930 && (sym
->ts
.type
== BT_CLASS
3931 && CLASS_DATA (sym
)->attr
.class_pointer
))
3933 else if ((!sym
->attr
.dummy
|| sym
->ts
.deferred
)
3934 && (sym
->attr
.allocatable
3935 || (sym
->ts
.type
== BT_CLASS
3936 && CLASS_DATA (sym
)->attr
.allocatable
)))
3938 if (!sym
->attr
.save
&& gfc_option
.flag_max_stack_var_size
!= 0)
3940 tree descriptor
= NULL_TREE
;
3942 /* Nullify and automatic deallocation of allocatable
3944 e
= gfc_lval_expr_from_sym (sym
);
3945 if (sym
->ts
.type
== BT_CLASS
)
3946 gfc_add_data_component (e
);
3948 gfc_init_se (&se
, NULL
);
3949 if (sym
->ts
.type
!= BT_CLASS
3950 || sym
->ts
.u
.derived
->attr
.dimension
3951 || sym
->ts
.u
.derived
->attr
.codimension
)
3953 se
.want_pointer
= 1;
3954 gfc_conv_expr (&se
, e
);
3956 else if (sym
->ts
.type
== BT_CLASS
3957 && !CLASS_DATA (sym
)->attr
.dimension
3958 && !CLASS_DATA (sym
)->attr
.codimension
)
3960 se
.want_pointer
= 1;
3961 gfc_conv_expr (&se
, e
);
3965 gfc_conv_expr (&se
, e
);
3966 descriptor
= se
.expr
;
3967 se
.expr
= gfc_conv_descriptor_data_addr (se
.expr
);
3968 se
.expr
= build_fold_indirect_ref_loc (input_location
, se
.expr
);
3972 gfc_save_backend_locus (&loc
);
3973 gfc_set_backend_locus (&sym
->declared_at
);
3974 gfc_start_block (&init
);
3976 if (!sym
->attr
.dummy
|| sym
->attr
.intent
== INTENT_OUT
)
3978 /* Nullify when entering the scope. */
3979 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
3980 TREE_TYPE (se
.expr
), se
.expr
,
3981 fold_convert (TREE_TYPE (se
.expr
),
3982 null_pointer_node
));
3983 if (sym
->attr
.optional
)
3985 tree present
= gfc_conv_expr_present (sym
);
3986 tmp
= build3_loc (input_location
, COND_EXPR
,
3987 void_type_node
, present
, tmp
,
3988 build_empty_stmt (input_location
));
3990 gfc_add_expr_to_block (&init
, tmp
);
3993 if ((sym
->attr
.dummy
|| sym
->attr
.result
)
3994 && sym
->ts
.type
== BT_CHARACTER
3995 && sym
->ts
.deferred
)
3997 /* Character length passed by reference. */
3998 tmp
= sym
->ts
.u
.cl
->passed_length
;
3999 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
4000 tmp
= fold_convert (gfc_charlen_type_node
, tmp
);
4002 if (!sym
->attr
.dummy
|| sym
->attr
.intent
== INTENT_OUT
)
4003 /* Zero the string length when entering the scope. */
4004 gfc_add_modify (&init
, sym
->ts
.u
.cl
->backend_decl
,
4005 build_int_cst (gfc_charlen_type_node
, 0));
4010 tmp2
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4011 gfc_charlen_type_node
,
4012 sym
->ts
.u
.cl
->backend_decl
, tmp
);
4013 if (sym
->attr
.optional
)
4015 tree present
= gfc_conv_expr_present (sym
);
4016 tmp2
= build3_loc (input_location
, COND_EXPR
,
4017 void_type_node
, present
, tmp2
,
4018 build_empty_stmt (input_location
));
4020 gfc_add_expr_to_block (&init
, tmp2
);
4023 gfc_restore_backend_locus (&loc
);
4025 /* Pass the final character length back. */
4026 if (sym
->attr
.intent
!= INTENT_IN
)
4028 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4029 gfc_charlen_type_node
, tmp
,
4030 sym
->ts
.u
.cl
->backend_decl
);
4031 if (sym
->attr
.optional
)
4033 tree present
= gfc_conv_expr_present (sym
);
4034 tmp
= build3_loc (input_location
, COND_EXPR
,
4035 void_type_node
, present
, tmp
,
4036 build_empty_stmt (input_location
));
4043 gfc_restore_backend_locus (&loc
);
4045 /* Deallocate when leaving the scope. Nullifying is not
4047 if (!sym
->attr
.result
&& !sym
->attr
.dummy
4048 && !sym
->ns
->proc_name
->attr
.is_main_program
)
4050 if (sym
->ts
.type
== BT_CLASS
4051 && CLASS_DATA (sym
)->attr
.codimension
)
4052 tmp
= gfc_deallocate_with_status (descriptor
, NULL_TREE
,
4053 NULL_TREE
, NULL_TREE
,
4054 NULL_TREE
, true, NULL
,
4058 gfc_expr
*expr
= gfc_lval_expr_from_sym (sym
);
4059 tmp
= gfc_deallocate_scalar_with_status (se
.expr
, NULL_TREE
,
4060 true, expr
, sym
->ts
);
4061 gfc_free_expr (expr
);
4064 if (sym
->ts
.type
== BT_CLASS
)
4066 /* Initialize _vptr to declared type. */
4070 gfc_save_backend_locus (&loc
);
4071 gfc_set_backend_locus (&sym
->declared_at
);
4072 e
= gfc_lval_expr_from_sym (sym
);
4073 gfc_add_vptr_component (e
);
4074 gfc_init_se (&se
, NULL
);
4075 se
.want_pointer
= 1;
4076 gfc_conv_expr (&se
, e
);
4078 if (UNLIMITED_POLY (sym
))
4079 rhs
= build_int_cst (TREE_TYPE (se
.expr
), 0);
4082 vtab
= gfc_find_derived_vtab (sym
->ts
.u
.derived
);
4083 rhs
= gfc_build_addr_expr (TREE_TYPE (se
.expr
),
4084 gfc_get_symbol_decl (vtab
));
4086 gfc_add_modify (&init
, se
.expr
, rhs
);
4087 gfc_restore_backend_locus (&loc
);
4090 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
4093 else if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.deferred
)
4098 /* If we get to here, all that should be left are pointers. */
4099 gcc_assert (sym
->attr
.pointer
);
4101 if (sym
->attr
.dummy
)
4103 gfc_start_block (&init
);
4105 /* Character length passed by reference. */
4106 tmp
= sym
->ts
.u
.cl
->passed_length
;
4107 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
4108 tmp
= fold_convert (gfc_charlen_type_node
, tmp
);
4109 gfc_add_modify (&init
, sym
->ts
.u
.cl
->backend_decl
, tmp
);
4110 /* Pass the final character length back. */
4111 if (sym
->attr
.intent
!= INTENT_IN
)
4112 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4113 gfc_charlen_type_node
, tmp
,
4114 sym
->ts
.u
.cl
->backend_decl
);
4117 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
4120 else if (sym
->ts
.deferred
)
4121 gfc_fatal_error ("Deferred type parameter not yet supported");
4122 else if (alloc_comp_or_fini
)
4123 gfc_trans_deferred_array (sym
, block
);
4124 else if (sym
->ts
.type
== BT_CHARACTER
)
4126 gfc_save_backend_locus (&loc
);
4127 gfc_set_backend_locus (&sym
->declared_at
);
4128 if (sym
->attr
.dummy
|| sym
->attr
.result
)
4129 gfc_trans_dummy_character (sym
, sym
->ts
.u
.cl
, block
);
4131 gfc_trans_auto_character_variable (sym
, block
);
4132 gfc_restore_backend_locus (&loc
);
4134 else if (sym
->attr
.assign
)
4136 gfc_save_backend_locus (&loc
);
4137 gfc_set_backend_locus (&sym
->declared_at
);
4138 gfc_trans_assign_aux_var (sym
, block
);
4139 gfc_restore_backend_locus (&loc
);
4141 else if (sym
->ts
.type
== BT_DERIVED
4144 && sym
->attr
.save
== SAVE_NONE
)
4146 gfc_start_block (&tmpblock
);
4147 gfc_init_default_dt (sym
, &tmpblock
, false);
4148 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
),
4151 else if (!(UNLIMITED_POLY(sym
)))
4155 gfc_init_block (&tmpblock
);
4157 for (f
= gfc_sym_get_dummy_args (proc_sym
); f
; f
= f
->next
)
4159 if (f
->sym
&& f
->sym
->tlink
== NULL
&& f
->sym
->ts
.type
== BT_CHARACTER
)
4161 gcc_assert (f
->sym
->ts
.u
.cl
->backend_decl
!= NULL
);
4162 if (TREE_CODE (f
->sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
4163 gfc_trans_vla_type_sizes (f
->sym
, &tmpblock
);
4167 if (gfc_return_by_reference (proc_sym
) && proc_sym
->ts
.type
== BT_CHARACTER
4168 && current_fake_result_decl
!= NULL
)
4170 gcc_assert (proc_sym
->ts
.u
.cl
->backend_decl
!= NULL
);
4171 if (TREE_CODE (proc_sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
4172 gfc_trans_vla_type_sizes (proc_sym
, &tmpblock
);
4175 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
), NULL_TREE
);
4178 static GTY ((param_is (struct module_htab_entry
))) htab_t module_htab
;
4180 /* Hash and equality functions for module_htab. */
4183 module_htab_do_hash (const void *x
)
4185 return htab_hash_string (((const struct module_htab_entry
*)x
)->name
);
4189 module_htab_eq (const void *x1
, const void *x2
)
4191 return strcmp ((((const struct module_htab_entry
*)x1
)->name
),
4192 (const char *)x2
) == 0;
4195 /* Hash and equality functions for module_htab's decls. */
4198 module_htab_decls_hash (const void *x
)
4200 const_tree t
= (const_tree
) x
;
4201 const_tree n
= DECL_NAME (t
);
4203 n
= TYPE_NAME (TREE_TYPE (t
));
4204 return htab_hash_string (IDENTIFIER_POINTER (n
));
4208 module_htab_decls_eq (const void *x1
, const void *x2
)
4210 const_tree t1
= (const_tree
) x1
;
4211 const_tree n1
= DECL_NAME (t1
);
4212 if (n1
== NULL_TREE
)
4213 n1
= TYPE_NAME (TREE_TYPE (t1
));
4214 return strcmp (IDENTIFIER_POINTER (n1
), (const char *) x2
) == 0;
4217 struct module_htab_entry
*
4218 gfc_find_module (const char *name
)
4223 module_htab
= htab_create_ggc (10, module_htab_do_hash
,
4224 module_htab_eq
, NULL
);
4226 slot
= htab_find_slot_with_hash (module_htab
, name
,
4227 htab_hash_string (name
), INSERT
);
4230 module_htab_entry
*entry
= ggc_cleared_alloc
<module_htab_entry
> ();
4232 entry
->name
= gfc_get_string (name
);
4233 entry
->decls
= htab_create_ggc (10, module_htab_decls_hash
,
4234 module_htab_decls_eq
, NULL
);
4235 *slot
= (void *) entry
;
4237 return (struct module_htab_entry
*) *slot
;
4241 gfc_module_add_decl (struct module_htab_entry
*entry
, tree decl
)
4246 if (DECL_NAME (decl
))
4247 name
= IDENTIFIER_POINTER (DECL_NAME (decl
));
4250 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
4251 name
= IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl
)));
4253 slot
= htab_find_slot_with_hash (entry
->decls
, name
,
4254 htab_hash_string (name
), INSERT
);
4256 *slot
= (void *) decl
;
4259 static struct module_htab_entry
*cur_module
;
4262 /* Generate debugging symbols for namelists. This function must come after
4263 generate_local_decl to ensure that the variables in the namelist are
4264 already declared. */
4267 generate_namelist_decl (gfc_symbol
* sym
)
4271 vec
<constructor_elt
, va_gc
> *nml_decls
= NULL
;
4273 gcc_assert (sym
->attr
.flavor
== FL_NAMELIST
);
4274 for (nml
= sym
->namelist
; nml
; nml
= nml
->next
)
4276 if (nml
->sym
->backend_decl
== NULL_TREE
)
4278 nml
->sym
->attr
.referenced
= 1;
4279 nml
->sym
->backend_decl
= gfc_get_symbol_decl (nml
->sym
);
4281 DECL_IGNORED_P (nml
->sym
->backend_decl
) = 0;
4282 CONSTRUCTOR_APPEND_ELT (nml_decls
, NULL_TREE
, nml
->sym
->backend_decl
);
4285 decl
= make_node (NAMELIST_DECL
);
4286 TREE_TYPE (decl
) = void_type_node
;
4287 NAMELIST_DECL_ASSOCIATED_DECL (decl
) = build_constructor (NULL_TREE
, nml_decls
);
4288 DECL_NAME (decl
) = get_identifier (sym
->name
);
4293 /* Output an initialized decl for a module variable. */
4296 gfc_create_module_variable (gfc_symbol
* sym
)
4300 /* Module functions with alternate entries are dealt with later and
4301 would get caught by the next condition. */
4302 if (sym
->attr
.entry
)
4305 /* Make sure we convert the types of the derived types from iso_c_binding
4307 if (sym
->attr
.flavor
!= FL_PROCEDURE
&& sym
->attr
.is_iso_c
4308 && sym
->ts
.type
== BT_DERIVED
)
4309 sym
->backend_decl
= gfc_typenode_for_spec (&(sym
->ts
));
4311 if (sym
->attr
.flavor
== FL_DERIVED
4312 && sym
->backend_decl
4313 && TREE_CODE (sym
->backend_decl
) == RECORD_TYPE
)
4315 decl
= sym
->backend_decl
;
4316 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
4318 if (!sym
->attr
.use_assoc
)
4320 gcc_assert (TYPE_CONTEXT (decl
) == NULL_TREE
4321 || TYPE_CONTEXT (decl
) == sym
->ns
->proc_name
->backend_decl
);
4322 gcc_assert (DECL_CONTEXT (TYPE_STUB_DECL (decl
)) == NULL_TREE
4323 || DECL_CONTEXT (TYPE_STUB_DECL (decl
))
4324 == sym
->ns
->proc_name
->backend_decl
);
4326 TYPE_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
4327 DECL_CONTEXT (TYPE_STUB_DECL (decl
)) = sym
->ns
->proc_name
->backend_decl
;
4328 gfc_module_add_decl (cur_module
, TYPE_STUB_DECL (decl
));
4331 /* Only output variables, procedure pointers and array valued,
4332 or derived type, parameters. */
4333 if (sym
->attr
.flavor
!= FL_VARIABLE
4334 && !(sym
->attr
.flavor
== FL_PARAMETER
4335 && (sym
->attr
.dimension
|| sym
->ts
.type
== BT_DERIVED
))
4336 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.proc_pointer
))
4339 if ((sym
->attr
.in_common
|| sym
->attr
.in_equivalence
) && sym
->backend_decl
)
4341 decl
= sym
->backend_decl
;
4342 gcc_assert (DECL_FILE_SCOPE_P (decl
));
4343 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
4344 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
4345 gfc_module_add_decl (cur_module
, decl
);
4348 /* Don't generate variables from other modules. Variables from
4349 COMMONs and Cray pointees will already have been generated. */
4350 if (sym
->attr
.use_assoc
|| sym
->attr
.in_common
|| sym
->attr
.cray_pointee
)
4353 /* Equivalenced variables arrive here after creation. */
4354 if (sym
->backend_decl
4355 && (sym
->equiv_built
|| sym
->attr
.in_equivalence
))
4358 if (sym
->backend_decl
&& !sym
->attr
.vtab
&& !sym
->attr
.target
)
4359 internal_error ("backend decl for module variable %s already exists",
4362 if (sym
->module
&& !sym
->attr
.result
&& !sym
->attr
.dummy
4363 && (sym
->attr
.access
== ACCESS_UNKNOWN
4364 && (sym
->ns
->default_access
== ACCESS_PRIVATE
4365 || (sym
->ns
->default_access
== ACCESS_UNKNOWN
4366 && gfc_option
.flag_module_private
))))
4367 sym
->attr
.access
= ACCESS_PRIVATE
;
4369 if (warn_unused_variable
&& !sym
->attr
.referenced
4370 && sym
->attr
.access
== ACCESS_PRIVATE
)
4371 gfc_warning ("Unused PRIVATE module variable '%s' declared at %L",
4372 sym
->name
, &sym
->declared_at
);
4374 /* We always want module variables to be created. */
4375 sym
->attr
.referenced
= 1;
4376 /* Create the decl. */
4377 decl
= gfc_get_symbol_decl (sym
);
4379 /* Create the variable. */
4381 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
4382 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
4383 rest_of_decl_compilation (decl
, 1, 0);
4384 gfc_module_add_decl (cur_module
, decl
);
4386 /* Also add length of strings. */
4387 if (sym
->ts
.type
== BT_CHARACTER
)
4391 length
= sym
->ts
.u
.cl
->backend_decl
;
4392 gcc_assert (length
|| sym
->attr
.proc_pointer
);
4393 if (length
&& !INTEGER_CST_P (length
))
4396 rest_of_decl_compilation (length
, 1, 0);
4400 if (sym
->attr
.codimension
&& !sym
->attr
.dummy
&& !sym
->attr
.allocatable
4401 && sym
->attr
.referenced
&& !sym
->attr
.use_assoc
)
4402 has_coarray_vars
= true;
4405 /* Emit debug information for USE statements. */
4408 gfc_trans_use_stmts (gfc_namespace
* ns
)
4410 gfc_use_list
*use_stmt
;
4411 for (use_stmt
= ns
->use_stmts
; use_stmt
; use_stmt
= use_stmt
->next
)
4413 struct module_htab_entry
*entry
4414 = gfc_find_module (use_stmt
->module_name
);
4415 gfc_use_rename
*rent
;
4417 if (entry
->namespace_decl
== NULL
)
4419 entry
->namespace_decl
4420 = build_decl (input_location
,
4422 get_identifier (use_stmt
->module_name
),
4424 DECL_EXTERNAL (entry
->namespace_decl
) = 1;
4426 gfc_set_backend_locus (&use_stmt
->where
);
4427 if (!use_stmt
->only_flag
)
4428 (*debug_hooks
->imported_module_or_decl
) (entry
->namespace_decl
,
4430 ns
->proc_name
->backend_decl
,
4432 for (rent
= use_stmt
->rename
; rent
; rent
= rent
->next
)
4434 tree decl
, local_name
;
4437 if (rent
->op
!= INTRINSIC_NONE
)
4440 slot
= htab_find_slot_with_hash (entry
->decls
, rent
->use_name
,
4441 htab_hash_string (rent
->use_name
),
4447 st
= gfc_find_symtree (ns
->sym_root
,
4449 ? rent
->local_name
: rent
->use_name
);
4451 /* The following can happen if a derived type is renamed. */
4455 name
= xstrdup (rent
->local_name
[0]
4456 ? rent
->local_name
: rent
->use_name
);
4457 name
[0] = (char) TOUPPER ((unsigned char) name
[0]);
4458 st
= gfc_find_symtree (ns
->sym_root
, name
);
4463 /* Sometimes, generic interfaces wind up being over-ruled by a
4464 local symbol (see PR41062). */
4465 if (!st
->n
.sym
->attr
.use_assoc
)
4468 if (st
->n
.sym
->backend_decl
4469 && DECL_P (st
->n
.sym
->backend_decl
)
4470 && st
->n
.sym
->module
4471 && strcmp (st
->n
.sym
->module
, use_stmt
->module_name
) == 0)
4473 gcc_assert (DECL_EXTERNAL (entry
->namespace_decl
)
4474 || (TREE_CODE (st
->n
.sym
->backend_decl
)
4476 decl
= copy_node (st
->n
.sym
->backend_decl
);
4477 DECL_CONTEXT (decl
) = entry
->namespace_decl
;
4478 DECL_EXTERNAL (decl
) = 1;
4479 DECL_IGNORED_P (decl
) = 0;
4480 DECL_INITIAL (decl
) = NULL_TREE
;
4482 else if (st
->n
.sym
->attr
.flavor
== FL_NAMELIST
4483 && st
->n
.sym
->attr
.use_only
4484 && st
->n
.sym
->module
4485 && strcmp (st
->n
.sym
->module
, use_stmt
->module_name
)
4488 decl
= generate_namelist_decl (st
->n
.sym
);
4489 DECL_CONTEXT (decl
) = entry
->namespace_decl
;
4490 DECL_EXTERNAL (decl
) = 1;
4491 DECL_IGNORED_P (decl
) = 0;
4492 DECL_INITIAL (decl
) = NULL_TREE
;
4496 *slot
= error_mark_node
;
4497 htab_clear_slot (entry
->decls
, slot
);
4502 decl
= (tree
) *slot
;
4503 if (rent
->local_name
[0])
4504 local_name
= get_identifier (rent
->local_name
);
4506 local_name
= NULL_TREE
;
4507 gfc_set_backend_locus (&rent
->where
);
4508 (*debug_hooks
->imported_module_or_decl
) (decl
, local_name
,
4509 ns
->proc_name
->backend_decl
,
4510 !use_stmt
->only_flag
);
4516 /* Return true if expr is a constant initializer that gfc_conv_initializer
4520 check_constant_initializer (gfc_expr
*expr
, gfc_typespec
*ts
, bool array
,
4530 if (expr
->expr_type
== EXPR_CONSTANT
|| expr
->expr_type
== EXPR_NULL
)
4532 else if (expr
->expr_type
== EXPR_STRUCTURE
)
4533 return check_constant_initializer (expr
, ts
, false, false);
4534 else if (expr
->expr_type
!= EXPR_ARRAY
)
4536 for (c
= gfc_constructor_first (expr
->value
.constructor
);
4537 c
; c
= gfc_constructor_next (c
))
4541 if (c
->expr
->expr_type
== EXPR_STRUCTURE
)
4543 if (!check_constant_initializer (c
->expr
, ts
, false, false))
4546 else if (c
->expr
->expr_type
!= EXPR_CONSTANT
)
4551 else switch (ts
->type
)
4554 if (expr
->expr_type
!= EXPR_STRUCTURE
)
4556 cm
= expr
->ts
.u
.derived
->components
;
4557 for (c
= gfc_constructor_first (expr
->value
.constructor
);
4558 c
; c
= gfc_constructor_next (c
), cm
= cm
->next
)
4560 if (!c
->expr
|| cm
->attr
.allocatable
)
4562 if (!check_constant_initializer (c
->expr
, &cm
->ts
,
4569 return expr
->expr_type
== EXPR_CONSTANT
;
4573 /* Emit debug info for parameters and unreferenced variables with
4577 gfc_emit_parameter_debug_info (gfc_symbol
*sym
)
4581 if (sym
->attr
.flavor
!= FL_PARAMETER
4582 && (sym
->attr
.flavor
!= FL_VARIABLE
|| sym
->attr
.referenced
))
4585 if (sym
->backend_decl
!= NULL
4586 || sym
->value
== NULL
4587 || sym
->attr
.use_assoc
4590 || sym
->attr
.function
4591 || sym
->attr
.intrinsic
4592 || sym
->attr
.pointer
4593 || sym
->attr
.allocatable
4594 || sym
->attr
.cray_pointee
4595 || sym
->attr
.threadprivate
4596 || sym
->attr
.is_bind_c
4597 || sym
->attr
.subref_array_pointer
4598 || sym
->attr
.assign
)
4601 if (sym
->ts
.type
== BT_CHARACTER
)
4603 gfc_conv_const_charlen (sym
->ts
.u
.cl
);
4604 if (sym
->ts
.u
.cl
->backend_decl
== NULL
4605 || TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) != INTEGER_CST
)
4608 else if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.alloc_comp
)
4615 if (sym
->as
->type
!= AS_EXPLICIT
)
4617 for (n
= 0; n
< sym
->as
->rank
; n
++)
4618 if (sym
->as
->lower
[n
]->expr_type
!= EXPR_CONSTANT
4619 || sym
->as
->upper
[n
] == NULL
4620 || sym
->as
->upper
[n
]->expr_type
!= EXPR_CONSTANT
)
4624 if (!check_constant_initializer (sym
->value
, &sym
->ts
,
4625 sym
->attr
.dimension
, false))
4628 if (gfc_option
.coarray
== GFC_FCOARRAY_LIB
&& sym
->attr
.codimension
)
4631 /* Create the decl for the variable or constant. */
4632 decl
= build_decl (input_location
,
4633 sym
->attr
.flavor
== FL_PARAMETER
? CONST_DECL
: VAR_DECL
,
4634 gfc_sym_identifier (sym
), gfc_sym_type (sym
));
4635 if (sym
->attr
.flavor
== FL_PARAMETER
)
4636 TREE_READONLY (decl
) = 1;
4637 gfc_set_decl_location (decl
, &sym
->declared_at
);
4638 if (sym
->attr
.dimension
)
4639 GFC_DECL_PACKED_ARRAY (decl
) = 1;
4640 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
4641 TREE_STATIC (decl
) = 1;
4642 TREE_USED (decl
) = 1;
4643 if (DECL_CONTEXT (decl
) && TREE_CODE (DECL_CONTEXT (decl
)) == NAMESPACE_DECL
)
4644 TREE_PUBLIC (decl
) = 1;
4645 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
4647 sym
->attr
.dimension
,
4649 debug_hooks
->global_decl (decl
);
4654 generate_coarray_sym_init (gfc_symbol
*sym
)
4656 tree tmp
, size
, decl
, token
;
4658 if (sym
->attr
.dummy
|| sym
->attr
.allocatable
|| !sym
->attr
.codimension
4659 || sym
->attr
.use_assoc
|| !sym
->attr
.referenced
)
4662 decl
= sym
->backend_decl
;
4663 TREE_USED(decl
) = 1;
4664 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (decl
)));
4666 /* FIXME: Workaround for PR middle-end/49106, cf. also PR middle-end/49108
4667 to make sure the variable is not optimized away. */
4668 DECL_PRESERVE_P (DECL_CONTEXT (decl
)) = 1;
4670 size
= TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (decl
)));
4672 /* Ensure that we do not have size=0 for zero-sized arrays. */
4673 size
= fold_build2_loc (input_location
, MAX_EXPR
, size_type_node
,
4674 fold_convert (size_type_node
, size
),
4675 build_int_cst (size_type_node
, 1));
4677 if (GFC_TYPE_ARRAY_RANK (TREE_TYPE (decl
)))
4679 tmp
= GFC_TYPE_ARRAY_SIZE (TREE_TYPE (decl
));
4680 size
= fold_build2_loc (input_location
, MULT_EXPR
, size_type_node
,
4681 fold_convert (size_type_node
, tmp
), size
);
4684 gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (decl
)) != NULL_TREE
);
4685 token
= gfc_build_addr_expr (ppvoid_type_node
,
4686 GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE(decl
)));
4688 tmp
= build_call_expr_loc (input_location
, gfor_fndecl_caf_register
, 6, size
,
4689 build_int_cst (integer_type_node
,
4690 GFC_CAF_COARRAY_STATIC
), /* type. */
4691 token
, null_pointer_node
, /* token, stat. */
4692 null_pointer_node
, /* errgmsg, errmsg_len. */
4693 build_int_cst (integer_type_node
, 0));
4695 gfc_add_modify (&caf_init_block
, decl
, fold_convert (TREE_TYPE (decl
), tmp
));
4698 /* Handle "static" initializer. */
4701 sym
->attr
.pointer
= 1;
4702 tmp
= gfc_trans_assignment (gfc_lval_expr_from_sym (sym
), sym
->value
,
4704 sym
->attr
.pointer
= 0;
4705 gfc_add_expr_to_block (&caf_init_block
, tmp
);
4710 /* Generate constructor function to initialize static, nonallocatable
4714 generate_coarray_init (gfc_namespace
* ns
__attribute((unused
)))
4716 tree fndecl
, tmp
, decl
, save_fn_decl
;
4718 save_fn_decl
= current_function_decl
;
4719 push_function_context ();
4721 tmp
= build_function_type_list (void_type_node
, NULL_TREE
);
4722 fndecl
= build_decl (input_location
, FUNCTION_DECL
,
4723 create_tmp_var_name ("_caf_init"), tmp
);
4725 DECL_STATIC_CONSTRUCTOR (fndecl
) = 1;
4726 SET_DECL_INIT_PRIORITY (fndecl
, DEFAULT_INIT_PRIORITY
);
4728 decl
= build_decl (input_location
, RESULT_DECL
, NULL_TREE
, void_type_node
);
4729 DECL_ARTIFICIAL (decl
) = 1;
4730 DECL_IGNORED_P (decl
) = 1;
4731 DECL_CONTEXT (decl
) = fndecl
;
4732 DECL_RESULT (fndecl
) = decl
;
4735 current_function_decl
= fndecl
;
4736 announce_function (fndecl
);
4738 rest_of_decl_compilation (fndecl
, 0, 0);
4739 make_decl_rtl (fndecl
);
4740 allocate_struct_function (fndecl
, false);
4743 gfc_init_block (&caf_init_block
);
4745 gfc_traverse_ns (ns
, generate_coarray_sym_init
);
4747 DECL_SAVED_TREE (fndecl
) = gfc_finish_block (&caf_init_block
);
4751 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
4753 DECL_SAVED_TREE (fndecl
)
4754 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (fndecl
),
4755 DECL_INITIAL (fndecl
));
4756 dump_function (TDI_original
, fndecl
);
4758 cfun
->function_end_locus
= input_location
;
4761 if (decl_function_context (fndecl
))
4762 (void) cgraph_create_node (fndecl
);
4764 cgraph_finalize_function (fndecl
, true);
4766 pop_function_context ();
4767 current_function_decl
= save_fn_decl
;
4772 create_module_nml_decl (gfc_symbol
*sym
)
4774 if (sym
->attr
.flavor
== FL_NAMELIST
)
4776 tree decl
= generate_namelist_decl (sym
);
4778 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
4779 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
4780 rest_of_decl_compilation (decl
, 1, 0);
4781 gfc_module_add_decl (cur_module
, decl
);
4786 /* Generate all the required code for module variables. */
4789 gfc_generate_module_vars (gfc_namespace
* ns
)
4791 module_namespace
= ns
;
4792 cur_module
= gfc_find_module (ns
->proc_name
->name
);
4794 /* Check if the frontend left the namespace in a reasonable state. */
4795 gcc_assert (ns
->proc_name
&& !ns
->proc_name
->tlink
);
4797 /* Generate COMMON blocks. */
4798 gfc_trans_common (ns
);
4800 has_coarray_vars
= false;
4802 /* Create decls for all the module variables. */
4803 gfc_traverse_ns (ns
, gfc_create_module_variable
);
4804 gfc_traverse_ns (ns
, create_module_nml_decl
);
4806 if (gfc_option
.coarray
== GFC_FCOARRAY_LIB
&& has_coarray_vars
)
4807 generate_coarray_init (ns
);
4811 gfc_trans_use_stmts (ns
);
4812 gfc_traverse_ns (ns
, gfc_emit_parameter_debug_info
);
4817 gfc_generate_contained_functions (gfc_namespace
* parent
)
4821 /* We create all the prototypes before generating any code. */
4822 for (ns
= parent
->contained
; ns
; ns
= ns
->sibling
)
4824 /* Skip namespaces from used modules. */
4825 if (ns
->parent
!= parent
)
4828 gfc_create_function_decl (ns
, false);
4831 for (ns
= parent
->contained
; ns
; ns
= ns
->sibling
)
4833 /* Skip namespaces from used modules. */
4834 if (ns
->parent
!= parent
)
4837 gfc_generate_function_code (ns
);
4842 /* Drill down through expressions for the array specification bounds and
4843 character length calling generate_local_decl for all those variables
4844 that have not already been declared. */
4847 generate_local_decl (gfc_symbol
*);
4849 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
4852 expr_decls (gfc_expr
*e
, gfc_symbol
*sym
,
4853 int *f ATTRIBUTE_UNUSED
)
4855 if (e
->expr_type
!= EXPR_VARIABLE
4856 || sym
== e
->symtree
->n
.sym
4857 || e
->symtree
->n
.sym
->mark
4858 || e
->symtree
->n
.sym
->ns
!= sym
->ns
)
4861 generate_local_decl (e
->symtree
->n
.sym
);
4866 generate_expr_decls (gfc_symbol
*sym
, gfc_expr
*e
)
4868 gfc_traverse_expr (e
, sym
, expr_decls
, 0);
4872 /* Check for dependencies in the character length and array spec. */
4875 generate_dependency_declarations (gfc_symbol
*sym
)
4879 if (sym
->ts
.type
== BT_CHARACTER
4881 && sym
->ts
.u
.cl
->length
4882 && sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
)
4883 generate_expr_decls (sym
, sym
->ts
.u
.cl
->length
);
4885 if (sym
->as
&& sym
->as
->rank
)
4887 for (i
= 0; i
< sym
->as
->rank
; i
++)
4889 generate_expr_decls (sym
, sym
->as
->lower
[i
]);
4890 generate_expr_decls (sym
, sym
->as
->upper
[i
]);
4896 /* Generate decls for all local variables. We do this to ensure correct
4897 handling of expressions which only appear in the specification of
4901 generate_local_decl (gfc_symbol
* sym
)
4903 if (sym
->attr
.flavor
== FL_VARIABLE
)
4905 if (sym
->attr
.codimension
&& !sym
->attr
.dummy
&& !sym
->attr
.allocatable
4906 && sym
->attr
.referenced
&& !sym
->attr
.use_assoc
)
4907 has_coarray_vars
= true;
4909 if (!sym
->attr
.dummy
&& !sym
->ns
->proc_name
->attr
.entry_master
)
4910 generate_dependency_declarations (sym
);
4912 if (sym
->attr
.referenced
)
4913 gfc_get_symbol_decl (sym
);
4915 /* Warnings for unused dummy arguments. */
4916 else if (sym
->attr
.dummy
&& !sym
->attr
.in_namelist
)
4918 /* INTENT(out) dummy arguments are likely meant to be set. */
4919 if (gfc_option
.warn_unused_dummy_argument
4920 && sym
->attr
.intent
== INTENT_OUT
)
4922 if (sym
->ts
.type
!= BT_DERIVED
)
4923 gfc_warning ("Dummy argument '%s' at %L was declared "
4924 "INTENT(OUT) but was not set", sym
->name
,
4926 else if (!gfc_has_default_initializer (sym
->ts
.u
.derived
)
4927 && !sym
->ts
.u
.derived
->attr
.zero_comp
)
4928 gfc_warning ("Derived-type dummy argument '%s' at %L was "
4929 "declared INTENT(OUT) but was not set and "
4930 "does not have a default initializer",
4931 sym
->name
, &sym
->declared_at
);
4932 if (sym
->backend_decl
!= NULL_TREE
)
4933 TREE_NO_WARNING(sym
->backend_decl
) = 1;
4935 else if (gfc_option
.warn_unused_dummy_argument
)
4937 gfc_warning ("Unused dummy argument '%s' at %L", sym
->name
,
4939 if (sym
->backend_decl
!= NULL_TREE
)
4940 TREE_NO_WARNING(sym
->backend_decl
) = 1;
4944 /* Warn for unused variables, but not if they're inside a common
4945 block or a namelist. */
4946 else if (warn_unused_variable
4947 && !(sym
->attr
.in_common
|| sym
->mark
|| sym
->attr
.in_namelist
))
4949 if (sym
->attr
.use_only
)
4951 gfc_warning ("Unused module variable '%s' which has been "
4952 "explicitly imported at %L", sym
->name
,
4954 if (sym
->backend_decl
!= NULL_TREE
)
4955 TREE_NO_WARNING(sym
->backend_decl
) = 1;
4957 else if (!sym
->attr
.use_assoc
)
4959 gfc_warning ("Unused variable '%s' declared at %L",
4960 sym
->name
, &sym
->declared_at
);
4961 if (sym
->backend_decl
!= NULL_TREE
)
4962 TREE_NO_WARNING(sym
->backend_decl
) = 1;
4966 /* For variable length CHARACTER parameters, the PARM_DECL already
4967 references the length variable, so force gfc_get_symbol_decl
4968 even when not referenced. If optimize > 0, it will be optimized
4969 away anyway. But do this only after emitting -Wunused-parameter
4970 warning if requested. */
4971 if (sym
->attr
.dummy
&& !sym
->attr
.referenced
4972 && sym
->ts
.type
== BT_CHARACTER
4973 && sym
->ts
.u
.cl
->backend_decl
!= NULL
4974 && TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) == VAR_DECL
)
4976 sym
->attr
.referenced
= 1;
4977 gfc_get_symbol_decl (sym
);
4980 /* INTENT(out) dummy arguments and result variables with allocatable
4981 components are reset by default and need to be set referenced to
4982 generate the code for nullification and automatic lengths. */
4983 if (!sym
->attr
.referenced
4984 && sym
->ts
.type
== BT_DERIVED
4985 && sym
->ts
.u
.derived
->attr
.alloc_comp
4986 && !sym
->attr
.pointer
4987 && ((sym
->attr
.dummy
&& sym
->attr
.intent
== INTENT_OUT
)
4989 (sym
->attr
.result
&& sym
!= sym
->result
)))
4991 sym
->attr
.referenced
= 1;
4992 gfc_get_symbol_decl (sym
);
4995 /* Check for dependencies in the array specification and string
4996 length, adding the necessary declarations to the function. We
4997 mark the symbol now, as well as in traverse_ns, to prevent
4998 getting stuck in a circular dependency. */
5001 else if (sym
->attr
.flavor
== FL_PARAMETER
)
5003 if (warn_unused_parameter
5004 && !sym
->attr
.referenced
)
5006 if (!sym
->attr
.use_assoc
)
5007 gfc_warning ("Unused parameter '%s' declared at %L", sym
->name
,
5009 else if (sym
->attr
.use_only
)
5010 gfc_warning ("Unused parameter '%s' which has been explicitly "
5011 "imported at %L", sym
->name
, &sym
->declared_at
);
5014 else if (sym
->attr
.flavor
== FL_PROCEDURE
)
5016 /* TODO: move to the appropriate place in resolve.c. */
5017 if (warn_return_type
5018 && sym
->attr
.function
5020 && sym
!= sym
->result
5021 && !sym
->result
->attr
.referenced
5022 && !sym
->attr
.use_assoc
5023 && sym
->attr
.if_source
!= IFSRC_IFBODY
)
5025 gfc_warning ("Return value '%s' of function '%s' declared at "
5026 "%L not set", sym
->result
->name
, sym
->name
,
5027 &sym
->result
->declared_at
);
5029 /* Prevents "Unused variable" warning for RESULT variables. */
5030 sym
->result
->mark
= 1;
5034 if (sym
->attr
.dummy
== 1)
5036 /* Modify the tree type for scalar character dummy arguments of bind(c)
5037 procedures if they are passed by value. The tree type for them will
5038 be promoted to INTEGER_TYPE for the middle end, which appears to be
5039 what C would do with characters passed by-value. The value attribute
5040 implies the dummy is a scalar. */
5041 if (sym
->attr
.value
== 1 && sym
->backend_decl
!= NULL
5042 && sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.is_c_interop
5043 && sym
->ns
->proc_name
!= NULL
&& sym
->ns
->proc_name
->attr
.is_bind_c
)
5044 gfc_conv_scalar_char_value (sym
, NULL
, NULL
);
5046 /* Unused procedure passed as dummy argument. */
5047 if (sym
->attr
.flavor
== FL_PROCEDURE
)
5049 if (!sym
->attr
.referenced
)
5051 if (gfc_option
.warn_unused_dummy_argument
)
5052 gfc_warning ("Unused dummy argument '%s' at %L", sym
->name
,
5056 /* Silence bogus "unused parameter" warnings from the
5058 if (sym
->backend_decl
!= NULL_TREE
)
5059 TREE_NO_WARNING (sym
->backend_decl
) = 1;
5063 /* Make sure we convert the types of the derived types from iso_c_binding
5065 if (sym
->attr
.flavor
!= FL_PROCEDURE
&& sym
->attr
.is_iso_c
5066 && sym
->ts
.type
== BT_DERIVED
)
5067 sym
->backend_decl
= gfc_typenode_for_spec (&(sym
->ts
));
5072 generate_local_nml_decl (gfc_symbol
* sym
)
5074 if (sym
->attr
.flavor
== FL_NAMELIST
&& !sym
->attr
.use_assoc
)
5076 tree decl
= generate_namelist_decl (sym
);
5083 generate_local_vars (gfc_namespace
* ns
)
5085 gfc_traverse_ns (ns
, generate_local_decl
);
5086 gfc_traverse_ns (ns
, generate_local_nml_decl
);
5090 /* Generate a switch statement to jump to the correct entry point. Also
5091 creates the label decls for the entry points. */
5094 gfc_trans_entry_master_switch (gfc_entry_list
* el
)
5101 gfc_init_block (&block
);
5102 for (; el
; el
= el
->next
)
5104 /* Add the case label. */
5105 label
= gfc_build_label_decl (NULL_TREE
);
5106 val
= build_int_cst (gfc_array_index_type
, el
->id
);
5107 tmp
= build_case_label (val
, NULL_TREE
, label
);
5108 gfc_add_expr_to_block (&block
, tmp
);
5110 /* And jump to the actual entry point. */
5111 label
= gfc_build_label_decl (NULL_TREE
);
5112 tmp
= build1_v (GOTO_EXPR
, label
);
5113 gfc_add_expr_to_block (&block
, tmp
);
5115 /* Save the label decl. */
5118 tmp
= gfc_finish_block (&block
);
5119 /* The first argument selects the entry point. */
5120 val
= DECL_ARGUMENTS (current_function_decl
);
5121 tmp
= fold_build3_loc (input_location
, SWITCH_EXPR
, NULL_TREE
,
5122 val
, tmp
, NULL_TREE
);
5127 /* Add code to string lengths of actual arguments passed to a function against
5128 the expected lengths of the dummy arguments. */
5131 add_argument_checking (stmtblock_t
*block
, gfc_symbol
*sym
)
5133 gfc_formal_arglist
*formal
;
5135 for (formal
= gfc_sym_get_dummy_args (sym
); formal
; formal
= formal
->next
)
5136 if (formal
->sym
&& formal
->sym
->ts
.type
== BT_CHARACTER
5137 && !formal
->sym
->ts
.deferred
)
5139 enum tree_code comparison
;
5144 const char *message
;
5150 gcc_assert (cl
->passed_length
!= NULL_TREE
);
5151 gcc_assert (cl
->backend_decl
!= NULL_TREE
);
5153 /* For POINTER, ALLOCATABLE and assumed-shape dummy arguments, the
5154 string lengths must match exactly. Otherwise, it is only required
5155 that the actual string length is *at least* the expected one.
5156 Sequence association allows for a mismatch of the string length
5157 if the actual argument is (part of) an array, but only if the
5158 dummy argument is an array. (See "Sequence association" in
5159 Section 12.4.1.4 for F95 and 12.4.1.5 for F2003.) */
5160 if (fsym
->attr
.pointer
|| fsym
->attr
.allocatable
5161 || (fsym
->as
&& (fsym
->as
->type
== AS_ASSUMED_SHAPE
5162 || fsym
->as
->type
== AS_ASSUMED_RANK
)))
5164 comparison
= NE_EXPR
;
5165 message
= _("Actual string length does not match the declared one"
5166 " for dummy argument '%s' (%ld/%ld)");
5168 else if (fsym
->as
&& fsym
->as
->rank
!= 0)
5172 comparison
= LT_EXPR
;
5173 message
= _("Actual string length is shorter than the declared one"
5174 " for dummy argument '%s' (%ld/%ld)");
5177 /* Build the condition. For optional arguments, an actual length
5178 of 0 is also acceptable if the associated string is NULL, which
5179 means the argument was not passed. */
5180 cond
= fold_build2_loc (input_location
, comparison
, boolean_type_node
,
5181 cl
->passed_length
, cl
->backend_decl
);
5182 if (fsym
->attr
.optional
)
5188 not_0length
= fold_build2_loc (input_location
, NE_EXPR
,
5191 build_zero_cst (gfc_charlen_type_node
));
5192 /* The symbol needs to be referenced for gfc_get_symbol_decl. */
5193 fsym
->attr
.referenced
= 1;
5194 not_absent
= gfc_conv_expr_present (fsym
);
5196 absent_failed
= fold_build2_loc (input_location
, TRUTH_OR_EXPR
,
5197 boolean_type_node
, not_0length
,
5200 cond
= fold_build2_loc (input_location
, TRUTH_AND_EXPR
,
5201 boolean_type_node
, cond
, absent_failed
);
5204 /* Build the runtime check. */
5205 argname
= gfc_build_cstring_const (fsym
->name
);
5206 argname
= gfc_build_addr_expr (pchar_type_node
, argname
);
5207 gfc_trans_runtime_check (true, false, cond
, block
, &fsym
->declared_at
,
5209 fold_convert (long_integer_type_node
,
5211 fold_convert (long_integer_type_node
,
5218 create_main_function (tree fndecl
)
5222 tree tmp
, decl
, result_decl
, argc
, argv
, typelist
, arglist
;
5225 old_context
= current_function_decl
;
5229 push_function_context ();
5230 saved_parent_function_decls
= saved_function_decls
;
5231 saved_function_decls
= NULL_TREE
;
5234 /* main() function must be declared with global scope. */
5235 gcc_assert (current_function_decl
== NULL_TREE
);
5237 /* Declare the function. */
5238 tmp
= build_function_type_list (integer_type_node
, integer_type_node
,
5239 build_pointer_type (pchar_type_node
),
5241 main_identifier_node
= get_identifier ("main");
5242 ftn_main
= build_decl (input_location
, FUNCTION_DECL
,
5243 main_identifier_node
, tmp
);
5244 DECL_EXTERNAL (ftn_main
) = 0;
5245 TREE_PUBLIC (ftn_main
) = 1;
5246 TREE_STATIC (ftn_main
) = 1;
5247 DECL_ATTRIBUTES (ftn_main
)
5248 = tree_cons (get_identifier("externally_visible"), NULL_TREE
, NULL_TREE
);
5250 /* Setup the result declaration (for "return 0"). */
5251 result_decl
= build_decl (input_location
,
5252 RESULT_DECL
, NULL_TREE
, integer_type_node
);
5253 DECL_ARTIFICIAL (result_decl
) = 1;
5254 DECL_IGNORED_P (result_decl
) = 1;
5255 DECL_CONTEXT (result_decl
) = ftn_main
;
5256 DECL_RESULT (ftn_main
) = result_decl
;
5258 pushdecl (ftn_main
);
5260 /* Get the arguments. */
5262 arglist
= NULL_TREE
;
5263 typelist
= TYPE_ARG_TYPES (TREE_TYPE (ftn_main
));
5265 tmp
= TREE_VALUE (typelist
);
5266 argc
= build_decl (input_location
, PARM_DECL
, get_identifier ("argc"), tmp
);
5267 DECL_CONTEXT (argc
) = ftn_main
;
5268 DECL_ARG_TYPE (argc
) = TREE_VALUE (typelist
);
5269 TREE_READONLY (argc
) = 1;
5270 gfc_finish_decl (argc
);
5271 arglist
= chainon (arglist
, argc
);
5273 typelist
= TREE_CHAIN (typelist
);
5274 tmp
= TREE_VALUE (typelist
);
5275 argv
= build_decl (input_location
, PARM_DECL
, get_identifier ("argv"), tmp
);
5276 DECL_CONTEXT (argv
) = ftn_main
;
5277 DECL_ARG_TYPE (argv
) = TREE_VALUE (typelist
);
5278 TREE_READONLY (argv
) = 1;
5279 DECL_BY_REFERENCE (argv
) = 1;
5280 gfc_finish_decl (argv
);
5281 arglist
= chainon (arglist
, argv
);
5283 DECL_ARGUMENTS (ftn_main
) = arglist
;
5284 current_function_decl
= ftn_main
;
5285 announce_function (ftn_main
);
5287 rest_of_decl_compilation (ftn_main
, 1, 0);
5288 make_decl_rtl (ftn_main
);
5289 allocate_struct_function (ftn_main
, false);
5292 gfc_init_block (&body
);
5294 /* Call some libgfortran initialization routines, call then MAIN__(). */
5296 /* Call _gfortran_caf_init (*argc, ***argv). */
5297 if (gfc_option
.coarray
== GFC_FCOARRAY_LIB
)
5299 tree pint_type
, pppchar_type
;
5300 pint_type
= build_pointer_type (integer_type_node
);
5302 = build_pointer_type (build_pointer_type (pchar_type_node
));
5304 tmp
= build_call_expr_loc (input_location
, gfor_fndecl_caf_init
, 2,
5305 gfc_build_addr_expr (pint_type
, argc
),
5306 gfc_build_addr_expr (pppchar_type
, argv
));
5307 gfc_add_expr_to_block (&body
, tmp
);
5310 /* Call _gfortran_set_args (argc, argv). */
5311 TREE_USED (argc
) = 1;
5312 TREE_USED (argv
) = 1;
5313 tmp
= build_call_expr_loc (input_location
,
5314 gfor_fndecl_set_args
, 2, argc
, argv
);
5315 gfc_add_expr_to_block (&body
, tmp
);
5317 /* Add a call to set_options to set up the runtime library Fortran
5318 language standard parameters. */
5320 tree array_type
, array
, var
;
5321 vec
<constructor_elt
, va_gc
> *v
= NULL
;
5323 /* Passing a new option to the library requires four modifications:
5324 + add it to the tree_cons list below
5325 + change the array size in the call to build_array_type
5326 + change the first argument to the library call
5327 gfor_fndecl_set_options
5328 + modify the library (runtime/compile_options.c)! */
5330 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5331 build_int_cst (integer_type_node
,
5332 gfc_option
.warn_std
));
5333 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5334 build_int_cst (integer_type_node
,
5335 gfc_option
.allow_std
));
5336 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5337 build_int_cst (integer_type_node
, pedantic
));
5338 /* TODO: This is the old -fdump-core option, which is unused but
5339 passed due to ABI compatibility; remove when bumping the
5341 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5342 build_int_cst (integer_type_node
,
5344 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5345 build_int_cst (integer_type_node
,
5346 gfc_option
.flag_backtrace
));
5347 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5348 build_int_cst (integer_type_node
,
5349 gfc_option
.flag_sign_zero
));
5350 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5351 build_int_cst (integer_type_node
,
5353 & GFC_RTCHECK_BOUNDS
)));
5354 /* TODO: This is the -frange-check option, which no longer affects
5355 library behavior; when bumping the library ABI this slot can be
5356 reused for something else. As it is the last element in the
5357 array, we can instead leave it out altogether. */
5358 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5359 build_int_cst (integer_type_node
, 0));
5360 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5361 build_int_cst (integer_type_node
,
5362 gfc_option
.fpe_summary
));
5364 array_type
= build_array_type (integer_type_node
,
5365 build_index_type (size_int (8)));
5366 array
= build_constructor (array_type
, v
);
5367 TREE_CONSTANT (array
) = 1;
5368 TREE_STATIC (array
) = 1;
5370 /* Create a static variable to hold the jump table. */
5371 var
= gfc_create_var (array_type
, "options");
5372 TREE_CONSTANT (var
) = 1;
5373 TREE_STATIC (var
) = 1;
5374 TREE_READONLY (var
) = 1;
5375 DECL_INITIAL (var
) = array
;
5376 var
= gfc_build_addr_expr (build_pointer_type (integer_type_node
), var
);
5378 tmp
= build_call_expr_loc (input_location
,
5379 gfor_fndecl_set_options
, 2,
5380 build_int_cst (integer_type_node
, 9), var
);
5381 gfc_add_expr_to_block (&body
, tmp
);
5384 /* If -ffpe-trap option was provided, add a call to set_fpe so that
5385 the library will raise a FPE when needed. */
5386 if (gfc_option
.fpe
!= 0)
5388 tmp
= build_call_expr_loc (input_location
,
5389 gfor_fndecl_set_fpe
, 1,
5390 build_int_cst (integer_type_node
,
5392 gfc_add_expr_to_block (&body
, tmp
);
5395 /* If this is the main program and an -fconvert option was provided,
5396 add a call to set_convert. */
5398 if (gfc_option
.convert
!= GFC_CONVERT_NATIVE
)
5400 tmp
= build_call_expr_loc (input_location
,
5401 gfor_fndecl_set_convert
, 1,
5402 build_int_cst (integer_type_node
,
5403 gfc_option
.convert
));
5404 gfc_add_expr_to_block (&body
, tmp
);
5407 /* If this is the main program and an -frecord-marker option was provided,
5408 add a call to set_record_marker. */
5410 if (gfc_option
.record_marker
!= 0)
5412 tmp
= build_call_expr_loc (input_location
,
5413 gfor_fndecl_set_record_marker
, 1,
5414 build_int_cst (integer_type_node
,
5415 gfc_option
.record_marker
));
5416 gfc_add_expr_to_block (&body
, tmp
);
5419 if (gfc_option
.max_subrecord_length
!= 0)
5421 tmp
= build_call_expr_loc (input_location
,
5422 gfor_fndecl_set_max_subrecord_length
, 1,
5423 build_int_cst (integer_type_node
,
5424 gfc_option
.max_subrecord_length
));
5425 gfc_add_expr_to_block (&body
, tmp
);
5428 /* Call MAIN__(). */
5429 tmp
= build_call_expr_loc (input_location
,
5431 gfc_add_expr_to_block (&body
, tmp
);
5433 /* Mark MAIN__ as used. */
5434 TREE_USED (fndecl
) = 1;
5436 /* Coarray: Call _gfortran_caf_finalize(void). */
5437 if (gfc_option
.coarray
== GFC_FCOARRAY_LIB
)
5439 /* Per F2008, 8.5.1 END of the main program implies a
5441 tmp
= builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE
);
5442 tmp
= build_call_expr_loc (input_location
, tmp
, 0);
5443 gfc_add_expr_to_block (&body
, tmp
);
5445 tmp
= build_call_expr_loc (input_location
, gfor_fndecl_caf_finalize
, 0);
5446 gfc_add_expr_to_block (&body
, tmp
);
5450 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, integer_type_node
,
5451 DECL_RESULT (ftn_main
),
5452 build_int_cst (integer_type_node
, 0));
5453 tmp
= build1_v (RETURN_EXPR
, tmp
);
5454 gfc_add_expr_to_block (&body
, tmp
);
5457 DECL_SAVED_TREE (ftn_main
) = gfc_finish_block (&body
);
5460 /* Finish off this function and send it for code generation. */
5462 BLOCK_SUPERCONTEXT (DECL_INITIAL (ftn_main
)) = ftn_main
;
5464 DECL_SAVED_TREE (ftn_main
)
5465 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (ftn_main
),
5466 DECL_INITIAL (ftn_main
));
5468 /* Output the GENERIC tree. */
5469 dump_function (TDI_original
, ftn_main
);
5471 cgraph_finalize_function (ftn_main
, true);
5475 pop_function_context ();
5476 saved_function_decls
= saved_parent_function_decls
;
5478 current_function_decl
= old_context
;
5482 /* Get the result expression for a procedure. */
5485 get_proc_result (gfc_symbol
* sym
)
5487 if (sym
->attr
.subroutine
|| sym
== sym
->result
)
5489 if (current_fake_result_decl
!= NULL
)
5490 return TREE_VALUE (current_fake_result_decl
);
5495 return sym
->result
->backend_decl
;
5499 /* Generate an appropriate return-statement for a procedure. */
5502 gfc_generate_return (void)
5508 sym
= current_procedure_symbol
;
5509 fndecl
= sym
->backend_decl
;
5511 if (TREE_TYPE (DECL_RESULT (fndecl
)) == void_type_node
)
5515 result
= get_proc_result (sym
);
5517 /* Set the return value to the dummy result variable. The
5518 types may be different for scalar default REAL functions
5519 with -ff2c, therefore we have to convert. */
5520 if (result
!= NULL_TREE
)
5522 result
= convert (TREE_TYPE (DECL_RESULT (fndecl
)), result
);
5523 result
= fold_build2_loc (input_location
, MODIFY_EXPR
,
5524 TREE_TYPE (result
), DECL_RESULT (fndecl
),
5529 return build1_v (RETURN_EXPR
, result
);
5533 /* Generate code for a function. */
5536 gfc_generate_function_code (gfc_namespace
* ns
)
5542 stmtblock_t init
, cleanup
;
5544 gfc_wrapped_block try_block
;
5545 tree recurcheckvar
= NULL_TREE
;
5547 gfc_symbol
*previous_procedure_symbol
;
5551 sym
= ns
->proc_name
;
5552 previous_procedure_symbol
= current_procedure_symbol
;
5553 current_procedure_symbol
= sym
;
5555 /* Check that the frontend isn't still using this. */
5556 gcc_assert (sym
->tlink
== NULL
);
5559 /* Create the declaration for functions with global scope. */
5560 if (!sym
->backend_decl
)
5561 gfc_create_function_decl (ns
, false);
5563 fndecl
= sym
->backend_decl
;
5564 old_context
= current_function_decl
;
5568 push_function_context ();
5569 saved_parent_function_decls
= saved_function_decls
;
5570 saved_function_decls
= NULL_TREE
;
5573 trans_function_start (sym
);
5575 gfc_init_block (&init
);
5577 if (ns
->entries
&& ns
->proc_name
->ts
.type
== BT_CHARACTER
)
5579 /* Copy length backend_decls to all entry point result
5584 gfc_conv_const_charlen (ns
->proc_name
->ts
.u
.cl
);
5585 backend_decl
= ns
->proc_name
->result
->ts
.u
.cl
->backend_decl
;
5586 for (el
= ns
->entries
; el
; el
= el
->next
)
5587 el
->sym
->result
->ts
.u
.cl
->backend_decl
= backend_decl
;
5590 /* Translate COMMON blocks. */
5591 gfc_trans_common (ns
);
5593 /* Null the parent fake result declaration if this namespace is
5594 a module function or an external procedures. */
5595 if ((ns
->parent
&& ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
5596 || ns
->parent
== NULL
)
5597 parent_fake_result_decl
= NULL_TREE
;
5599 gfc_generate_contained_functions (ns
);
5601 nonlocal_dummy_decls
= NULL
;
5602 nonlocal_dummy_decl_pset
= NULL
;
5604 has_coarray_vars
= false;
5605 generate_local_vars (ns
);
5607 if (gfc_option
.coarray
== GFC_FCOARRAY_LIB
&& has_coarray_vars
)
5608 generate_coarray_init (ns
);
5610 /* Keep the parent fake result declaration in module functions
5611 or external procedures. */
5612 if ((ns
->parent
&& ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
5613 || ns
->parent
== NULL
)
5614 current_fake_result_decl
= parent_fake_result_decl
;
5616 current_fake_result_decl
= NULL_TREE
;
5618 is_recursive
= sym
->attr
.recursive
5619 || (sym
->attr
.entry_master
5620 && sym
->ns
->entries
->sym
->attr
.recursive
);
5621 if ((gfc_option
.rtcheck
& GFC_RTCHECK_RECURSION
)
5623 && !gfc_option
.flag_recursive
)
5627 asprintf (&msg
, "Recursive call to nonrecursive procedure '%s'",
5629 recurcheckvar
= gfc_create_var (boolean_type_node
, "is_recursive");
5630 TREE_STATIC (recurcheckvar
) = 1;
5631 DECL_INITIAL (recurcheckvar
) = boolean_false_node
;
5632 gfc_add_expr_to_block (&init
, recurcheckvar
);
5633 gfc_trans_runtime_check (true, false, recurcheckvar
, &init
,
5634 &sym
->declared_at
, msg
);
5635 gfc_add_modify (&init
, recurcheckvar
, boolean_true_node
);
5639 /* Now generate the code for the body of this function. */
5640 gfc_init_block (&body
);
5642 if (TREE_TYPE (DECL_RESULT (fndecl
)) != void_type_node
5643 && sym
->attr
.subroutine
)
5645 tree alternate_return
;
5646 alternate_return
= gfc_get_fake_result_decl (sym
, 0);
5647 gfc_add_modify (&body
, alternate_return
, integer_zero_node
);
5652 /* Jump to the correct entry point. */
5653 tmp
= gfc_trans_entry_master_switch (ns
->entries
);
5654 gfc_add_expr_to_block (&body
, tmp
);
5657 /* If bounds-checking is enabled, generate code to check passed in actual
5658 arguments against the expected dummy argument attributes (e.g. string
5660 if ((gfc_option
.rtcheck
& GFC_RTCHECK_BOUNDS
) && !sym
->attr
.is_bind_c
)
5661 add_argument_checking (&body
, sym
);
5663 tmp
= gfc_trans_code (ns
->code
);
5664 gfc_add_expr_to_block (&body
, tmp
);
5666 if (TREE_TYPE (DECL_RESULT (fndecl
)) != void_type_node
)
5668 tree result
= get_proc_result (sym
);
5670 if (result
!= NULL_TREE
&& sym
->attr
.function
&& !sym
->attr
.pointer
)
5672 if (sym
->attr
.allocatable
&& sym
->attr
.dimension
== 0
5673 && sym
->result
== sym
)
5674 gfc_add_modify (&init
, result
, fold_convert (TREE_TYPE (result
),
5675 null_pointer_node
));
5676 else if (sym
->ts
.type
== BT_CLASS
5677 && CLASS_DATA (sym
)->attr
.allocatable
5678 && CLASS_DATA (sym
)->attr
.dimension
== 0
5679 && sym
->result
== sym
)
5681 tmp
= CLASS_DATA (sym
)->backend_decl
;
5682 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
,
5683 TREE_TYPE (tmp
), result
, tmp
, NULL_TREE
);
5684 gfc_add_modify (&init
, tmp
, fold_convert (TREE_TYPE (tmp
),
5685 null_pointer_node
));
5687 else if (sym
->ts
.type
== BT_DERIVED
5688 && sym
->ts
.u
.derived
->attr
.alloc_comp
5689 && !sym
->attr
.allocatable
)
5691 rank
= sym
->as
? sym
->as
->rank
: 0;
5692 tmp
= gfc_nullify_alloc_comp (sym
->ts
.u
.derived
, result
, rank
);
5693 gfc_add_expr_to_block (&init
, tmp
);
5697 if (result
== NULL_TREE
)
5699 /* TODO: move to the appropriate place in resolve.c. */
5700 if (warn_return_type
&& sym
== sym
->result
)
5701 gfc_warning ("Return value of function '%s' at %L not set",
5702 sym
->name
, &sym
->declared_at
);
5703 if (warn_return_type
)
5704 TREE_NO_WARNING(sym
->backend_decl
) = 1;
5707 gfc_add_expr_to_block (&body
, gfc_generate_return ());
5710 gfc_init_block (&cleanup
);
5712 /* Reset recursion-check variable. */
5713 if ((gfc_option
.rtcheck
& GFC_RTCHECK_RECURSION
)
5715 && !gfc_option
.gfc_flag_openmp
5716 && recurcheckvar
!= NULL_TREE
)
5718 gfc_add_modify (&cleanup
, recurcheckvar
, boolean_false_node
);
5719 recurcheckvar
= NULL
;
5722 /* Finish the function body and add init and cleanup code. */
5723 tmp
= gfc_finish_block (&body
);
5724 gfc_start_wrapped_block (&try_block
, tmp
);
5725 /* Add code to create and cleanup arrays. */
5726 gfc_trans_deferred_vars (sym
, &try_block
);
5727 gfc_add_init_cleanup (&try_block
, gfc_finish_block (&init
),
5728 gfc_finish_block (&cleanup
));
5730 /* Add all the decls we created during processing. */
5731 decl
= saved_function_decls
;
5736 next
= DECL_CHAIN (decl
);
5737 DECL_CHAIN (decl
) = NULL_TREE
;
5741 saved_function_decls
= NULL_TREE
;
5743 DECL_SAVED_TREE (fndecl
) = gfc_finish_wrapped_block (&try_block
);
5746 /* Finish off this function and send it for code generation. */
5748 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
5750 DECL_SAVED_TREE (fndecl
)
5751 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (fndecl
),
5752 DECL_INITIAL (fndecl
));
5754 if (nonlocal_dummy_decls
)
5756 BLOCK_VARS (DECL_INITIAL (fndecl
))
5757 = chainon (BLOCK_VARS (DECL_INITIAL (fndecl
)), nonlocal_dummy_decls
);
5758 pointer_set_destroy (nonlocal_dummy_decl_pset
);
5759 nonlocal_dummy_decls
= NULL
;
5760 nonlocal_dummy_decl_pset
= NULL
;
5763 /* Output the GENERIC tree. */
5764 dump_function (TDI_original
, fndecl
);
5766 /* Store the end of the function, so that we get good line number
5767 info for the epilogue. */
5768 cfun
->function_end_locus
= input_location
;
5770 /* We're leaving the context of this function, so zap cfun.
5771 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
5772 tree_rest_of_compilation. */
5777 pop_function_context ();
5778 saved_function_decls
= saved_parent_function_decls
;
5780 current_function_decl
= old_context
;
5782 if (decl_function_context (fndecl
))
5784 /* Register this function with cgraph just far enough to get it
5785 added to our parent's nested function list.
5786 If there are static coarrays in this function, the nested _caf_init
5787 function has already called cgraph_create_node, which also created
5788 the cgraph node for this function. */
5789 if (!has_coarray_vars
|| gfc_option
.coarray
!= GFC_FCOARRAY_LIB
)
5790 (void) cgraph_create_node (fndecl
);
5793 cgraph_finalize_function (fndecl
, true);
5795 gfc_trans_use_stmts (ns
);
5796 gfc_traverse_ns (ns
, gfc_emit_parameter_debug_info
);
5798 if (sym
->attr
.is_main_program
)
5799 create_main_function (fndecl
);
5801 current_procedure_symbol
= previous_procedure_symbol
;
5806 gfc_generate_constructors (void)
5808 gcc_assert (gfc_static_ctors
== NULL_TREE
);
5816 if (gfc_static_ctors
== NULL_TREE
)
5819 fnname
= get_file_function_name ("I");
5820 type
= build_function_type_list (void_type_node
, NULL_TREE
);
5822 fndecl
= build_decl (input_location
,
5823 FUNCTION_DECL
, fnname
, type
);
5824 TREE_PUBLIC (fndecl
) = 1;
5826 decl
= build_decl (input_location
,
5827 RESULT_DECL
, NULL_TREE
, void_type_node
);
5828 DECL_ARTIFICIAL (decl
) = 1;
5829 DECL_IGNORED_P (decl
) = 1;
5830 DECL_CONTEXT (decl
) = fndecl
;
5831 DECL_RESULT (fndecl
) = decl
;
5835 current_function_decl
= fndecl
;
5837 rest_of_decl_compilation (fndecl
, 1, 0);
5839 make_decl_rtl (fndecl
);
5841 allocate_struct_function (fndecl
, false);
5845 for (; gfc_static_ctors
; gfc_static_ctors
= TREE_CHAIN (gfc_static_ctors
))
5847 tmp
= build_call_expr_loc (input_location
,
5848 TREE_VALUE (gfc_static_ctors
), 0);
5849 DECL_SAVED_TREE (fndecl
) = build_stmt (input_location
, EXPR_STMT
, tmp
);
5855 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
5856 DECL_SAVED_TREE (fndecl
)
5857 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (fndecl
),
5858 DECL_INITIAL (fndecl
));
5860 free_after_parsing (cfun
);
5861 free_after_compilation (cfun
);
5863 tree_rest_of_compilation (fndecl
);
5865 current_function_decl
= NULL_TREE
;
5869 /* Translates a BLOCK DATA program unit. This means emitting the
5870 commons contained therein plus their initializations. We also emit
5871 a globally visible symbol to make sure that each BLOCK DATA program
5872 unit remains unique. */
5875 gfc_generate_block_data (gfc_namespace
* ns
)
5880 /* Tell the backend the source location of the block data. */
5882 gfc_set_backend_locus (&ns
->proc_name
->declared_at
);
5884 gfc_set_backend_locus (&gfc_current_locus
);
5886 /* Process the DATA statements. */
5887 gfc_trans_common (ns
);
5889 /* Create a global symbol with the mane of the block data. This is to
5890 generate linker errors if the same name is used twice. It is never
5893 id
= gfc_sym_mangled_function_id (ns
->proc_name
);
5895 id
= get_identifier ("__BLOCK_DATA__");
5897 decl
= build_decl (input_location
,
5898 VAR_DECL
, id
, gfc_array_index_type
);
5899 TREE_PUBLIC (decl
) = 1;
5900 TREE_STATIC (decl
) = 1;
5901 DECL_IGNORED_P (decl
) = 1;
5904 rest_of_decl_compilation (decl
, 1, 0);
5908 /* Process the local variables of a BLOCK construct. */
5911 gfc_process_block_locals (gfc_namespace
* ns
)
5915 gcc_assert (saved_local_decls
== NULL_TREE
);
5916 has_coarray_vars
= false;
5918 generate_local_vars (ns
);
5920 if (gfc_option
.coarray
== GFC_FCOARRAY_LIB
&& has_coarray_vars
)
5921 generate_coarray_init (ns
);
5923 decl
= saved_local_decls
;
5928 next
= DECL_CHAIN (decl
);
5929 DECL_CHAIN (decl
) = NULL_TREE
;
5933 saved_local_decls
= NULL_TREE
;
5937 #include "gt-fortran-trans-decl.h"