1 /* Backend function setup
2 Copyright (C) 2002-2016 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"
30 #include "gimple-expr.h" /* For create_tmp_var_raw. */
32 #include "stringpool.h"
34 #include "fold-const.h"
35 #include "stor-layout.h"
38 #include "tree-dump.h"
39 #include "toplev.h" /* For announce_function. */
41 #include "constructor.h"
42 #include "trans-types.h"
43 #include "trans-array.h"
44 #include "trans-const.h"
45 /* Only for gfc_trans_code. Shouldn't need to include this. */
46 #include "trans-stmt.h"
47 #include "gomp-constants.h"
50 #define MAX_LABEL_VALUE 99999
53 /* Holds the result of the function if no result variable specified. */
55 static GTY(()) tree current_fake_result_decl
;
56 static GTY(()) tree parent_fake_result_decl
;
59 /* Holds the variable DECLs for the current function. */
61 static GTY(()) tree saved_function_decls
;
62 static GTY(()) tree saved_parent_function_decls
;
64 static hash_set
<tree
> *nonlocal_dummy_decl_pset
;
65 static GTY(()) tree nonlocal_dummy_decls
;
67 /* Holds the variable DECLs that are locals. */
69 static GTY(()) tree saved_local_decls
;
71 /* The namespace of the module we're currently generating. Only used while
72 outputting decls for module variables. Do not rely on this being set. */
74 static gfc_namespace
*module_namespace
;
76 /* The currently processed procedure symbol. */
77 static gfc_symbol
* current_procedure_symbol
= NULL
;
79 /* The currently processed module. */
80 static struct module_htab_entry
*cur_module
;
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 /* Whether we've seen a symbol from an IEEE module in the namespace. */
94 static int seen_ieee_symbol
;
96 /* Function declarations for builtin library functions. */
98 tree gfor_fndecl_pause_numeric
;
99 tree gfor_fndecl_pause_string
;
100 tree gfor_fndecl_stop_numeric
;
101 tree gfor_fndecl_stop_numeric_f08
;
102 tree gfor_fndecl_stop_string
;
103 tree gfor_fndecl_error_stop_numeric
;
104 tree gfor_fndecl_error_stop_string
;
105 tree gfor_fndecl_runtime_error
;
106 tree gfor_fndecl_runtime_error_at
;
107 tree gfor_fndecl_runtime_warning_at
;
108 tree gfor_fndecl_os_error
;
109 tree gfor_fndecl_generate_error
;
110 tree gfor_fndecl_set_args
;
111 tree gfor_fndecl_set_fpe
;
112 tree gfor_fndecl_set_options
;
113 tree gfor_fndecl_set_convert
;
114 tree gfor_fndecl_set_record_marker
;
115 tree gfor_fndecl_set_max_subrecord_length
;
116 tree gfor_fndecl_ctime
;
117 tree gfor_fndecl_fdate
;
118 tree gfor_fndecl_ttynam
;
119 tree gfor_fndecl_in_pack
;
120 tree gfor_fndecl_in_unpack
;
121 tree gfor_fndecl_associated
;
122 tree gfor_fndecl_system_clock4
;
123 tree gfor_fndecl_system_clock8
;
124 tree gfor_fndecl_ieee_procedure_entry
;
125 tree gfor_fndecl_ieee_procedure_exit
;
128 /* Coarray run-time library function decls. */
129 tree gfor_fndecl_caf_init
;
130 tree gfor_fndecl_caf_finalize
;
131 tree gfor_fndecl_caf_this_image
;
132 tree gfor_fndecl_caf_num_images
;
133 tree gfor_fndecl_caf_register
;
134 tree gfor_fndecl_caf_deregister
;
135 tree gfor_fndecl_caf_get
;
136 tree gfor_fndecl_caf_send
;
137 tree gfor_fndecl_caf_sendget
;
138 tree gfor_fndecl_caf_get_by_ref
;
139 tree gfor_fndecl_caf_send_by_ref
;
140 tree gfor_fndecl_caf_sendget_by_ref
;
141 tree gfor_fndecl_caf_sync_all
;
142 tree gfor_fndecl_caf_sync_memory
;
143 tree gfor_fndecl_caf_sync_images
;
144 tree gfor_fndecl_caf_stop_str
;
145 tree gfor_fndecl_caf_stop_numeric
;
146 tree gfor_fndecl_caf_error_stop
;
147 tree gfor_fndecl_caf_error_stop_str
;
148 tree gfor_fndecl_caf_atomic_def
;
149 tree gfor_fndecl_caf_atomic_ref
;
150 tree gfor_fndecl_caf_atomic_cas
;
151 tree gfor_fndecl_caf_atomic_op
;
152 tree gfor_fndecl_caf_lock
;
153 tree gfor_fndecl_caf_unlock
;
154 tree gfor_fndecl_caf_event_post
;
155 tree gfor_fndecl_caf_event_wait
;
156 tree gfor_fndecl_caf_event_query
;
157 tree gfor_fndecl_co_broadcast
;
158 tree gfor_fndecl_co_max
;
159 tree gfor_fndecl_co_min
;
160 tree gfor_fndecl_co_reduce
;
161 tree gfor_fndecl_co_sum
;
164 /* Math functions. Many other math functions are handled in
165 trans-intrinsic.c. */
167 gfc_powdecl_list gfor_fndecl_math_powi
[4][3];
168 tree gfor_fndecl_math_ishftc4
;
169 tree gfor_fndecl_math_ishftc8
;
170 tree gfor_fndecl_math_ishftc16
;
173 /* String functions. */
175 tree gfor_fndecl_compare_string
;
176 tree gfor_fndecl_concat_string
;
177 tree gfor_fndecl_string_len_trim
;
178 tree gfor_fndecl_string_index
;
179 tree gfor_fndecl_string_scan
;
180 tree gfor_fndecl_string_verify
;
181 tree gfor_fndecl_string_trim
;
182 tree gfor_fndecl_string_minmax
;
183 tree gfor_fndecl_adjustl
;
184 tree gfor_fndecl_adjustr
;
185 tree gfor_fndecl_select_string
;
186 tree gfor_fndecl_compare_string_char4
;
187 tree gfor_fndecl_concat_string_char4
;
188 tree gfor_fndecl_string_len_trim_char4
;
189 tree gfor_fndecl_string_index_char4
;
190 tree gfor_fndecl_string_scan_char4
;
191 tree gfor_fndecl_string_verify_char4
;
192 tree gfor_fndecl_string_trim_char4
;
193 tree gfor_fndecl_string_minmax_char4
;
194 tree gfor_fndecl_adjustl_char4
;
195 tree gfor_fndecl_adjustr_char4
;
196 tree gfor_fndecl_select_string_char4
;
199 /* Conversion between character kinds. */
200 tree gfor_fndecl_convert_char1_to_char4
;
201 tree gfor_fndecl_convert_char4_to_char1
;
204 /* Other misc. runtime library functions. */
205 tree gfor_fndecl_size0
;
206 tree gfor_fndecl_size1
;
207 tree gfor_fndecl_iargc
;
209 /* Intrinsic functions implemented in Fortran. */
210 tree gfor_fndecl_sc_kind
;
211 tree gfor_fndecl_si_kind
;
212 tree gfor_fndecl_sr_kind
;
214 /* BLAS gemm functions. */
215 tree gfor_fndecl_sgemm
;
216 tree gfor_fndecl_dgemm
;
217 tree gfor_fndecl_cgemm
;
218 tree gfor_fndecl_zgemm
;
222 gfc_add_decl_to_parent_function (tree decl
)
225 DECL_CONTEXT (decl
) = DECL_CONTEXT (current_function_decl
);
226 DECL_NONLOCAL (decl
) = 1;
227 DECL_CHAIN (decl
) = saved_parent_function_decls
;
228 saved_parent_function_decls
= decl
;
232 gfc_add_decl_to_function (tree decl
)
235 TREE_USED (decl
) = 1;
236 DECL_CONTEXT (decl
) = current_function_decl
;
237 DECL_CHAIN (decl
) = saved_function_decls
;
238 saved_function_decls
= decl
;
242 add_decl_as_local (tree decl
)
245 TREE_USED (decl
) = 1;
246 DECL_CONTEXT (decl
) = current_function_decl
;
247 DECL_CHAIN (decl
) = saved_local_decls
;
248 saved_local_decls
= decl
;
252 /* Build a backend label declaration. Set TREE_USED for named labels.
253 The context of the label is always the current_function_decl. All
254 labels are marked artificial. */
257 gfc_build_label_decl (tree label_id
)
259 /* 2^32 temporaries should be enough. */
260 static unsigned int tmp_num
= 1;
264 if (label_id
== NULL_TREE
)
266 /* Build an internal label name. */
267 ASM_FORMAT_PRIVATE_NAME (label_name
, "L", tmp_num
++);
268 label_id
= get_identifier (label_name
);
273 /* Build the LABEL_DECL node. Labels have no type. */
274 label_decl
= build_decl (input_location
,
275 LABEL_DECL
, label_id
, void_type_node
);
276 DECL_CONTEXT (label_decl
) = current_function_decl
;
277 DECL_MODE (label_decl
) = VOIDmode
;
279 /* We always define the label as used, even if the original source
280 file never references the label. We don't want all kinds of
281 spurious warnings for old-style Fortran code with too many
283 TREE_USED (label_decl
) = 1;
285 DECL_ARTIFICIAL (label_decl
) = 1;
290 /* Set the backend source location of a decl. */
293 gfc_set_decl_location (tree decl
, locus
* loc
)
295 DECL_SOURCE_LOCATION (decl
) = loc
->lb
->location
;
299 /* Return the backend label declaration for a given label structure,
300 or create it if it doesn't exist yet. */
303 gfc_get_label_decl (gfc_st_label
* lp
)
305 if (lp
->backend_decl
)
306 return lp
->backend_decl
;
309 char label_name
[GFC_MAX_SYMBOL_LEN
+ 1];
312 /* Validate the label declaration from the front end. */
313 gcc_assert (lp
!= NULL
&& lp
->value
<= MAX_LABEL_VALUE
);
315 /* Build a mangled name for the label. */
316 sprintf (label_name
, "__label_%.6d", lp
->value
);
318 /* Build the LABEL_DECL node. */
319 label_decl
= gfc_build_label_decl (get_identifier (label_name
));
321 /* Tell the debugger where the label came from. */
322 if (lp
->value
<= MAX_LABEL_VALUE
) /* An internal label. */
323 gfc_set_decl_location (label_decl
, &lp
->where
);
325 DECL_ARTIFICIAL (label_decl
) = 1;
327 /* Store the label in the label list and return the LABEL_DECL. */
328 lp
->backend_decl
= label_decl
;
334 /* Convert a gfc_symbol to an identifier of the same name. */
337 gfc_sym_identifier (gfc_symbol
* sym
)
339 if (sym
->attr
.is_main_program
&& strcmp (sym
->name
, "main") == 0)
340 return (get_identifier ("MAIN__"));
342 return (get_identifier (sym
->name
));
346 /* Construct mangled name from symbol name. */
349 gfc_sym_mangled_identifier (gfc_symbol
* sym
)
351 char name
[GFC_MAX_MANGLED_SYMBOL_LEN
+ 1];
353 /* Prevent the mangling of identifiers that have an assigned
354 binding label (mainly those that are bind(c)). */
355 if (sym
->attr
.is_bind_c
== 1 && sym
->binding_label
)
356 return get_identifier (sym
->binding_label
);
358 if (sym
->module
== NULL
)
359 return gfc_sym_identifier (sym
);
362 snprintf (name
, sizeof name
, "__%s_MOD_%s", sym
->module
, sym
->name
);
363 return get_identifier (name
);
368 /* Construct mangled function name from symbol name. */
371 gfc_sym_mangled_function_id (gfc_symbol
* sym
)
374 char name
[GFC_MAX_MANGLED_SYMBOL_LEN
+ 1];
376 /* It may be possible to simply use the binding label if it's
377 provided, and remove the other checks. Then we could use it
378 for other things if we wished. */
379 if ((sym
->attr
.is_bind_c
== 1 || sym
->attr
.is_iso_c
== 1) &&
381 /* use the binding label rather than the mangled name */
382 return get_identifier (sym
->binding_label
);
384 if ((sym
->module
== NULL
|| sym
->attr
.proc
== PROC_EXTERNAL
385 || (sym
->module
!= NULL
&& (sym
->attr
.external
386 || sym
->attr
.if_source
== IFSRC_IFBODY
)))
387 && !sym
->attr
.module_procedure
)
389 /* Main program is mangled into MAIN__. */
390 if (sym
->attr
.is_main_program
)
391 return get_identifier ("MAIN__");
393 /* Intrinsic procedures are never mangled. */
394 if (sym
->attr
.proc
== PROC_INTRINSIC
)
395 return get_identifier (sym
->name
);
397 if (flag_underscoring
)
399 has_underscore
= strchr (sym
->name
, '_') != 0;
400 if (flag_second_underscore
&& has_underscore
)
401 snprintf (name
, sizeof name
, "%s__", sym
->name
);
403 snprintf (name
, sizeof name
, "%s_", sym
->name
);
404 return get_identifier (name
);
407 return get_identifier (sym
->name
);
411 snprintf (name
, sizeof name
, "__%s_MOD_%s", sym
->module
, sym
->name
);
412 return get_identifier (name
);
418 gfc_set_decl_assembler_name (tree decl
, tree name
)
420 tree target_mangled
= targetm
.mangle_decl_assembler_name (decl
, name
);
421 SET_DECL_ASSEMBLER_NAME (decl
, target_mangled
);
425 /* Returns true if a variable of specified size should go on the stack. */
428 gfc_can_put_var_on_stack (tree size
)
430 unsigned HOST_WIDE_INT low
;
432 if (!INTEGER_CST_P (size
))
435 if (flag_max_stack_var_size
< 0)
438 if (!tree_fits_uhwi_p (size
))
441 low
= TREE_INT_CST_LOW (size
);
442 if (low
> (unsigned HOST_WIDE_INT
) flag_max_stack_var_size
)
445 /* TODO: Set a per-function stack size limit. */
451 /* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
452 an expression involving its corresponding pointer. There are
453 2 cases; one for variable size arrays, and one for everything else,
454 because variable-sized arrays require one fewer level of
458 gfc_finish_cray_pointee (tree decl
, gfc_symbol
*sym
)
460 tree ptr_decl
= gfc_get_symbol_decl (sym
->cp_pointer
);
463 /* Parameters need to be dereferenced. */
464 if (sym
->cp_pointer
->attr
.dummy
)
465 ptr_decl
= build_fold_indirect_ref_loc (input_location
,
468 /* Check to see if we're dealing with a variable-sized array. */
469 if (sym
->attr
.dimension
470 && TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
)
472 /* These decls will be dereferenced later, so we don't dereference
474 value
= convert (TREE_TYPE (decl
), ptr_decl
);
478 ptr_decl
= convert (build_pointer_type (TREE_TYPE (decl
)),
480 value
= build_fold_indirect_ref_loc (input_location
,
484 SET_DECL_VALUE_EXPR (decl
, value
);
485 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
486 GFC_DECL_CRAY_POINTEE (decl
) = 1;
490 /* Finish processing of a declaration without an initial value. */
493 gfc_finish_decl (tree decl
)
495 gcc_assert (TREE_CODE (decl
) == PARM_DECL
496 || DECL_INITIAL (decl
) == NULL_TREE
);
498 if (TREE_CODE (decl
) != VAR_DECL
)
501 if (DECL_SIZE (decl
) == NULL_TREE
502 && TYPE_SIZE (TREE_TYPE (decl
)) != NULL_TREE
)
503 layout_decl (decl
, 0);
505 /* A few consistency checks. */
506 /* A static variable with an incomplete type is an error if it is
507 initialized. Also if it is not file scope. Otherwise, let it
508 through, but if it is not `extern' then it may cause an error
510 /* An automatic variable with an incomplete type is an error. */
512 /* We should know the storage size. */
513 gcc_assert (DECL_SIZE (decl
) != NULL_TREE
514 || (TREE_STATIC (decl
)
515 ? (!DECL_INITIAL (decl
) || !DECL_CONTEXT (decl
))
516 : DECL_EXTERNAL (decl
)));
518 /* The storage size should be constant. */
519 gcc_assert ((!DECL_EXTERNAL (decl
) && !TREE_STATIC (decl
))
521 || TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
);
525 /* Handle setting of GFC_DECL_SCALAR* on DECL. */
528 gfc_finish_decl_attrs (tree decl
, symbol_attribute
*attr
)
530 if (!attr
->dimension
&& !attr
->codimension
)
532 /* Handle scalar allocatable variables. */
533 if (attr
->allocatable
)
535 gfc_allocate_lang_decl (decl
);
536 GFC_DECL_SCALAR_ALLOCATABLE (decl
) = 1;
538 /* Handle scalar pointer variables. */
541 gfc_allocate_lang_decl (decl
);
542 GFC_DECL_SCALAR_POINTER (decl
) = 1;
548 /* Apply symbol attributes to a variable, and add it to the function scope. */
551 gfc_finish_var_decl (tree decl
, gfc_symbol
* sym
)
555 /* Set DECL_VALUE_EXPR for Cray Pointees. */
556 if (sym
->attr
.cray_pointee
)
557 gfc_finish_cray_pointee (decl
, sym
);
559 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
560 This is the equivalent of the TARGET variables.
561 We also need to set this if the variable is passed by reference in a
563 if (sym
->attr
.target
)
564 TREE_ADDRESSABLE (decl
) = 1;
566 /* If it wasn't used we wouldn't be getting it. */
567 TREE_USED (decl
) = 1;
569 if (sym
->attr
.flavor
== FL_PARAMETER
570 && (sym
->attr
.dimension
|| sym
->ts
.type
== BT_DERIVED
))
571 TREE_READONLY (decl
) = 1;
573 /* Chain this decl to the pending declarations. Don't do pushdecl()
574 because this would add them to the current scope rather than the
576 if (current_function_decl
!= NULL_TREE
)
578 if (sym
->ns
->proc_name
->backend_decl
== current_function_decl
579 || sym
->result
== sym
)
580 gfc_add_decl_to_function (decl
);
581 else if (sym
->ns
->proc_name
->attr
.flavor
== FL_LABEL
)
582 /* This is a BLOCK construct. */
583 add_decl_as_local (decl
);
585 gfc_add_decl_to_parent_function (decl
);
588 if (sym
->attr
.cray_pointee
)
591 if(sym
->attr
.is_bind_c
== 1 && sym
->binding_label
)
593 /* We need to put variables that are bind(c) into the common
594 segment of the object file, because this is what C would do.
595 gfortran would typically put them in either the BSS or
596 initialized data segments, and only mark them as common if
597 they were part of common blocks. However, if they are not put
598 into common space, then C cannot initialize global Fortran
599 variables that it interoperates with and the draft says that
600 either Fortran or C should be able to initialize it (but not
601 both, of course.) (J3/04-007, section 15.3). */
602 TREE_PUBLIC(decl
) = 1;
603 DECL_COMMON(decl
) = 1;
604 if (sym
->attr
.access
== ACCESS_PRIVATE
&& !sym
->attr
.public_used
)
606 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
607 DECL_VISIBILITY_SPECIFIED (decl
) = true;
611 /* If a variable is USE associated, it's always external. */
612 if (sym
->attr
.use_assoc
|| sym
->attr
.used_in_submodule
)
614 DECL_EXTERNAL (decl
) = 1;
615 TREE_PUBLIC (decl
) = 1;
617 else if (sym
->module
&& !sym
->attr
.result
&& !sym
->attr
.dummy
)
619 /* TODO: Don't set sym->module for result or dummy variables. */
620 gcc_assert (current_function_decl
== NULL_TREE
|| sym
->result
== sym
);
622 TREE_PUBLIC (decl
) = 1;
623 TREE_STATIC (decl
) = 1;
624 if (sym
->attr
.access
== ACCESS_PRIVATE
&& !sym
->attr
.public_used
)
626 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
627 DECL_VISIBILITY_SPECIFIED (decl
) = true;
631 /* Derived types are a bit peculiar because of the possibility of
632 a default initializer; this must be applied each time the variable
633 comes into scope it therefore need not be static. These variables
634 are SAVE_NONE but have an initializer. Otherwise explicitly
635 initialized variables are SAVE_IMPLICIT and explicitly saved are
637 if (!sym
->attr
.use_assoc
638 && (sym
->attr
.save
!= SAVE_NONE
|| sym
->attr
.data
639 || (sym
->value
&& sym
->ns
->proc_name
->attr
.is_main_program
)
640 || (flag_coarray
== GFC_FCOARRAY_LIB
641 && sym
->attr
.codimension
&& !sym
->attr
.allocatable
)))
642 TREE_STATIC (decl
) = 1;
644 /* If derived-type variables with DTIO procedures are not made static
645 some bits of code referencing them get optimized away.
646 TODO Understand why this is so and fix it. */
647 if (!sym
->attr
.use_assoc
648 && ((sym
->ts
.type
== BT_DERIVED
649 && sym
->ts
.u
.derived
->attr
.has_dtio_procs
)
650 || (sym
->ts
.type
== BT_CLASS
651 && CLASS_DATA (sym
)->ts
.u
.derived
->attr
.has_dtio_procs
)))
652 TREE_STATIC (decl
) = 1;
654 if (sym
->attr
.volatile_
)
656 TREE_THIS_VOLATILE (decl
) = 1;
657 TREE_SIDE_EFFECTS (decl
) = 1;
658 new_type
= build_qualified_type (TREE_TYPE (decl
), TYPE_QUAL_VOLATILE
);
659 TREE_TYPE (decl
) = new_type
;
662 /* Keep variables larger than max-stack-var-size off stack. */
663 if (!sym
->ns
->proc_name
->attr
.recursive
&& !sym
->attr
.automatic
664 && INTEGER_CST_P (DECL_SIZE_UNIT (decl
))
665 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl
))
666 /* Put variable length auto array pointers always into stack. */
667 && (TREE_CODE (TREE_TYPE (decl
)) != POINTER_TYPE
668 || sym
->attr
.dimension
== 0
669 || sym
->as
->type
!= AS_EXPLICIT
671 || sym
->attr
.allocatable
)
672 && !DECL_ARTIFICIAL (decl
))
674 TREE_STATIC (decl
) = 1;
676 /* Because the size of this variable isn't known until now, we may have
677 greedily added an initializer to this variable (in build_init_assign)
678 even though the max-stack-var-size indicates the variable should be
679 static. Therefore we rip out the automatic initializer here and
680 replace it with a static one. */
681 gfc_symtree
*st
= gfc_find_symtree (sym
->ns
->sym_root
, sym
->name
);
682 gfc_code
*prev
= NULL
;
683 gfc_code
*code
= sym
->ns
->code
;
684 while (code
&& code
->op
== EXEC_INIT_ASSIGN
)
686 /* Look for an initializer meant for this symbol. */
687 if (code
->expr1
->symtree
== st
)
690 prev
->next
= code
->next
;
692 sym
->ns
->code
= code
->next
;
700 if (code
&& code
->op
== EXEC_INIT_ASSIGN
)
702 /* Keep the init expression for a static initializer. */
703 sym
->value
= code
->expr2
;
704 /* Cleanup the defunct code object, without freeing the init expr. */
706 gfc_free_statement (code
);
711 /* Handle threadprivate variables. */
712 if (sym
->attr
.threadprivate
713 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
714 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
716 gfc_finish_decl_attrs (decl
, &sym
->attr
);
720 /* Allocate the lang-specific part of a decl. */
723 gfc_allocate_lang_decl (tree decl
)
725 if (DECL_LANG_SPECIFIC (decl
) == NULL
)
726 DECL_LANG_SPECIFIC (decl
) = ggc_cleared_alloc
<struct lang_decl
> ();
729 /* Remember a symbol to generate initialization/cleanup code at function
733 gfc_defer_symbol_init (gfc_symbol
* sym
)
739 /* Don't add a symbol twice. */
743 last
= head
= sym
->ns
->proc_name
;
746 /* Make sure that setup code for dummy variables which are used in the
747 setup of other variables is generated first. */
750 /* Find the first dummy arg seen after us, or the first non-dummy arg.
751 This is a circular list, so don't go past the head. */
753 && (!p
->attr
.dummy
|| p
->dummy_order
> sym
->dummy_order
))
759 /* Insert in between last and p. */
765 /* Used in gfc_get_symbol_decl and gfc_get_derived_type to obtain the
766 backend_decl for a module symbol, if it all ready exists. If the
767 module gsymbol does not exist, it is created. If the symbol does
768 not exist, it is added to the gsymbol namespace. Returns true if
769 an existing backend_decl is found. */
772 gfc_get_module_backend_decl (gfc_symbol
*sym
)
778 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->module
);
780 if (!gsym
|| (gsym
->ns
&& gsym
->type
== GSYM_MODULE
))
785 /* Check for a symbol with the same name. */
787 gfc_find_symbol (sym
->name
, gsym
->ns
, 0, &s
);
793 gsym
= gfc_get_gsymbol (sym
->module
);
794 gsym
->type
= GSYM_MODULE
;
795 gsym
->ns
= gfc_get_namespace (NULL
, 0);
798 st
= gfc_new_symtree (&gsym
->ns
->sym_root
, sym
->name
);
802 else if (gfc_fl_struct (sym
->attr
.flavor
))
804 if (s
&& s
->attr
.flavor
== FL_PROCEDURE
)
807 gcc_assert (s
->attr
.generic
);
808 for (intr
= s
->generic
; intr
; intr
= intr
->next
)
809 if (gfc_fl_struct (intr
->sym
->attr
.flavor
))
816 /* Normally we can assume that s is a derived-type symbol since it
817 shares a name with the derived-type sym. However if sym is a
818 STRUCTURE, it may in fact share a name with any other basic type
819 variable. If s is in fact of derived type then we can continue
820 looking for a duplicate type declaration. */
821 if (sym
->attr
.flavor
== FL_STRUCT
&& s
->ts
.type
== BT_DERIVED
)
826 if (gfc_fl_struct (s
->attr
.flavor
) && !s
->backend_decl
)
828 if (s
->attr
.flavor
== FL_UNION
)
829 s
->backend_decl
= gfc_get_union_type (s
);
831 s
->backend_decl
= gfc_get_derived_type (s
);
833 gfc_copy_dt_decls_ifequal (s
, sym
, true);
836 else if (s
->backend_decl
)
838 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
839 gfc_copy_dt_decls_ifequal (s
->ts
.u
.derived
, sym
->ts
.u
.derived
,
841 else if (sym
->ts
.type
== BT_CHARACTER
)
842 sym
->ts
.u
.cl
->backend_decl
= s
->ts
.u
.cl
->backend_decl
;
843 sym
->backend_decl
= s
->backend_decl
;
851 /* Create an array index type variable with function scope. */
854 create_index_var (const char * pfx
, int nest
)
858 decl
= gfc_create_var_np (gfc_array_index_type
, pfx
);
860 gfc_add_decl_to_parent_function (decl
);
862 gfc_add_decl_to_function (decl
);
867 /* Create variables to hold all the non-constant bits of info for a
868 descriptorless array. Remember these in the lang-specific part of the
872 gfc_build_qualified_array (tree decl
, gfc_symbol
* sym
)
877 gfc_namespace
* procns
;
878 symbol_attribute
*array_attr
;
880 bool is_classarray
= IS_CLASS_ARRAY (sym
);
882 type
= TREE_TYPE (decl
);
883 array_attr
= is_classarray
? &CLASS_DATA (sym
)->attr
: &sym
->attr
;
884 as
= is_classarray
? CLASS_DATA (sym
)->as
: sym
->as
;
886 /* We just use the descriptor, if there is one. */
887 if (GFC_DESCRIPTOR_TYPE_P (type
))
890 gcc_assert (GFC_ARRAY_TYPE_P (type
));
891 procns
= gfc_find_proc_namespace (sym
->ns
);
892 nest
= (procns
->proc_name
->backend_decl
!= current_function_decl
)
893 && !sym
->attr
.contained
;
895 if (array_attr
->codimension
&& flag_coarray
== GFC_FCOARRAY_LIB
896 && as
->type
!= AS_ASSUMED_SHAPE
897 && GFC_TYPE_ARRAY_CAF_TOKEN (type
) == NULL_TREE
)
900 tree token_type
= build_qualified_type (pvoid_type_node
,
903 if (sym
->module
&& (sym
->attr
.use_assoc
904 || sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
))
907 = get_identifier (gfc_get_string (GFC_PREFIX ("caf_token%s"),
908 IDENTIFIER_POINTER (gfc_sym_mangled_identifier (sym
))));
909 token
= build_decl (DECL_SOURCE_LOCATION (decl
), VAR_DECL
, token_name
,
911 if (sym
->attr
.use_assoc
)
912 DECL_EXTERNAL (token
) = 1;
914 TREE_STATIC (token
) = 1;
916 TREE_PUBLIC (token
) = 1;
918 if (sym
->attr
.access
== ACCESS_PRIVATE
&& !sym
->attr
.public_used
)
920 DECL_VISIBILITY (token
) = VISIBILITY_HIDDEN
;
921 DECL_VISIBILITY_SPECIFIED (token
) = true;
926 token
= gfc_create_var_np (token_type
, "caf_token");
927 TREE_STATIC (token
) = 1;
930 GFC_TYPE_ARRAY_CAF_TOKEN (type
) = token
;
931 DECL_ARTIFICIAL (token
) = 1;
932 DECL_NONALIASED (token
) = 1;
934 if (sym
->module
&& !sym
->attr
.use_assoc
)
937 DECL_CONTEXT (token
) = sym
->ns
->proc_name
->backend_decl
;
938 gfc_module_add_decl (cur_module
, token
);
941 gfc_add_decl_to_function (token
);
944 for (dim
= 0; dim
< GFC_TYPE_ARRAY_RANK (type
); dim
++)
946 if (GFC_TYPE_ARRAY_LBOUND (type
, dim
) == NULL_TREE
)
948 GFC_TYPE_ARRAY_LBOUND (type
, dim
) = create_index_var ("lbound", nest
);
949 TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type
, dim
)) = 1;
951 /* Don't try to use the unknown bound for assumed shape arrays. */
952 if (GFC_TYPE_ARRAY_UBOUND (type
, dim
) == NULL_TREE
953 && (as
->type
!= AS_ASSUMED_SIZE
954 || dim
< GFC_TYPE_ARRAY_RANK (type
) - 1))
956 GFC_TYPE_ARRAY_UBOUND (type
, dim
) = create_index_var ("ubound", nest
);
957 TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type
, dim
)) = 1;
960 if (GFC_TYPE_ARRAY_STRIDE (type
, dim
) == NULL_TREE
)
962 GFC_TYPE_ARRAY_STRIDE (type
, dim
) = create_index_var ("stride", nest
);
963 TREE_NO_WARNING (GFC_TYPE_ARRAY_STRIDE (type
, dim
)) = 1;
966 for (dim
= GFC_TYPE_ARRAY_RANK (type
);
967 dim
< GFC_TYPE_ARRAY_RANK (type
) + GFC_TYPE_ARRAY_CORANK (type
); dim
++)
969 if (GFC_TYPE_ARRAY_LBOUND (type
, dim
) == NULL_TREE
)
971 GFC_TYPE_ARRAY_LBOUND (type
, dim
) = create_index_var ("lbound", nest
);
972 TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type
, dim
)) = 1;
974 /* Don't try to use the unknown ubound for the last coarray dimension. */
975 if (GFC_TYPE_ARRAY_UBOUND (type
, dim
) == NULL_TREE
976 && dim
< GFC_TYPE_ARRAY_RANK (type
) + GFC_TYPE_ARRAY_CORANK (type
) - 1)
978 GFC_TYPE_ARRAY_UBOUND (type
, dim
) = create_index_var ("ubound", nest
);
979 TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type
, dim
)) = 1;
982 if (GFC_TYPE_ARRAY_OFFSET (type
) == NULL_TREE
)
984 GFC_TYPE_ARRAY_OFFSET (type
) = gfc_create_var_np (gfc_array_index_type
,
986 TREE_NO_WARNING (GFC_TYPE_ARRAY_OFFSET (type
)) = 1;
989 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type
));
991 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type
));
994 if (GFC_TYPE_ARRAY_SIZE (type
) == NULL_TREE
995 && as
->type
!= AS_ASSUMED_SIZE
)
997 GFC_TYPE_ARRAY_SIZE (type
) = create_index_var ("size", nest
);
998 TREE_NO_WARNING (GFC_TYPE_ARRAY_SIZE (type
)) = 1;
1001 if (POINTER_TYPE_P (type
))
1003 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type
)));
1004 gcc_assert (TYPE_LANG_SPECIFIC (type
)
1005 == TYPE_LANG_SPECIFIC (TREE_TYPE (type
)));
1006 type
= TREE_TYPE (type
);
1009 if (! COMPLETE_TYPE_P (type
) && GFC_TYPE_ARRAY_SIZE (type
))
1013 size
= fold_build2_loc (input_location
, MINUS_EXPR
, gfc_array_index_type
,
1014 GFC_TYPE_ARRAY_SIZE (type
), gfc_index_one_node
);
1015 range
= build_range_type (gfc_array_index_type
, gfc_index_zero_node
,
1017 TYPE_DOMAIN (type
) = range
;
1021 if (TYPE_NAME (type
) != NULL_TREE
1022 && GFC_TYPE_ARRAY_UBOUND (type
, as
->rank
- 1) != NULL_TREE
1023 && TREE_CODE (GFC_TYPE_ARRAY_UBOUND (type
, as
->rank
- 1)) == VAR_DECL
)
1025 tree gtype
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
1027 for (dim
= 0; dim
< as
->rank
- 1; dim
++)
1029 gcc_assert (TREE_CODE (gtype
) == ARRAY_TYPE
);
1030 gtype
= TREE_TYPE (gtype
);
1032 gcc_assert (TREE_CODE (gtype
) == ARRAY_TYPE
);
1033 if (TYPE_MAX_VALUE (TYPE_DOMAIN (gtype
)) == NULL
)
1034 TYPE_NAME (type
) = NULL_TREE
;
1037 if (TYPE_NAME (type
) == NULL_TREE
)
1039 tree gtype
= TREE_TYPE (type
), rtype
, type_decl
;
1041 for (dim
= as
->rank
- 1; dim
>= 0; dim
--)
1043 tree lbound
, ubound
;
1044 lbound
= GFC_TYPE_ARRAY_LBOUND (type
, dim
);
1045 ubound
= GFC_TYPE_ARRAY_UBOUND (type
, dim
);
1046 rtype
= build_range_type (gfc_array_index_type
, lbound
, ubound
);
1047 gtype
= build_array_type (gtype
, rtype
);
1048 /* Ensure the bound variables aren't optimized out at -O0.
1049 For -O1 and above they often will be optimized out, but
1050 can be tracked by VTA. Also set DECL_NAMELESS, so that
1051 the artificial lbound.N or ubound.N DECL_NAME doesn't
1052 end up in debug info. */
1053 if (lbound
&& TREE_CODE (lbound
) == VAR_DECL
1054 && DECL_ARTIFICIAL (lbound
) && DECL_IGNORED_P (lbound
))
1056 if (DECL_NAME (lbound
)
1057 && strstr (IDENTIFIER_POINTER (DECL_NAME (lbound
)),
1059 DECL_NAMELESS (lbound
) = 1;
1060 DECL_IGNORED_P (lbound
) = 0;
1062 if (ubound
&& TREE_CODE (ubound
) == VAR_DECL
1063 && DECL_ARTIFICIAL (ubound
) && DECL_IGNORED_P (ubound
))
1065 if (DECL_NAME (ubound
)
1066 && strstr (IDENTIFIER_POINTER (DECL_NAME (ubound
)),
1068 DECL_NAMELESS (ubound
) = 1;
1069 DECL_IGNORED_P (ubound
) = 0;
1072 TYPE_NAME (type
) = type_decl
= build_decl (input_location
,
1073 TYPE_DECL
, NULL
, gtype
);
1074 DECL_ORIGINAL_TYPE (type_decl
) = gtype
;
1079 /* For some dummy arguments we don't use the actual argument directly.
1080 Instead we create a local decl and use that. This allows us to perform
1081 initialization, and construct full type information. */
1084 gfc_build_dummy_array_decl (gfc_symbol
* sym
, tree dummy
)
1089 symbol_attribute
*array_attr
;
1094 bool is_classarray
= IS_CLASS_ARRAY (sym
);
1096 /* Use the array as and attr. */
1097 as
= is_classarray
? CLASS_DATA (sym
)->as
: sym
->as
;
1098 array_attr
= is_classarray
? &CLASS_DATA (sym
)->attr
: &sym
->attr
;
1100 /* The dummy is returned for pointer, allocatable or assumed rank arrays.
1101 For class arrays the information if sym is an allocatable or pointer
1102 object needs to be checked explicitly (IS_CLASS_ARRAY can be false for
1103 too many reasons to be of use here). */
1104 if ((sym
->ts
.type
!= BT_CLASS
&& sym
->attr
.pointer
)
1105 || (sym
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym
)->attr
.class_pointer
)
1106 || array_attr
->allocatable
1107 || (as
&& as
->type
== AS_ASSUMED_RANK
))
1110 /* Add to list of variables if not a fake result variable.
1111 These symbols are set on the symbol only, not on the class component. */
1112 if (sym
->attr
.result
|| sym
->attr
.dummy
)
1113 gfc_defer_symbol_init (sym
);
1115 /* For a class array the array descriptor is in the _data component, while
1116 for a regular array the TREE_TYPE of the dummy is a pointer to the
1118 type
= TREE_TYPE (is_classarray
? gfc_class_data_get (dummy
)
1119 : TREE_TYPE (dummy
));
1120 /* type now is the array descriptor w/o any indirection. */
1121 gcc_assert (TREE_CODE (dummy
) == PARM_DECL
1122 && POINTER_TYPE_P (TREE_TYPE (dummy
)));
1124 /* Do we know the element size? */
1125 known_size
= sym
->ts
.type
!= BT_CHARACTER
1126 || INTEGER_CST_P (sym
->ts
.u
.cl
->backend_decl
);
1128 if (known_size
&& !GFC_DESCRIPTOR_TYPE_P (type
))
1130 /* For descriptorless arrays with known element size the actual
1131 argument is sufficient. */
1132 gfc_build_qualified_array (dummy
, sym
);
1136 if (GFC_DESCRIPTOR_TYPE_P (type
))
1138 /* Create a descriptorless array pointer. */
1141 /* Even when -frepack-arrays is used, symbols with TARGET attribute
1142 are not repacked. */
1143 if (!flag_repack_arrays
|| sym
->attr
.target
)
1145 if (as
->type
== AS_ASSUMED_SIZE
)
1146 packed
= PACKED_FULL
;
1150 if (as
->type
== AS_EXPLICIT
)
1152 packed
= PACKED_FULL
;
1153 for (n
= 0; n
< as
->rank
; n
++)
1157 && as
->upper
[n
]->expr_type
== EXPR_CONSTANT
1158 && as
->lower
[n
]->expr_type
== EXPR_CONSTANT
))
1160 packed
= PACKED_PARTIAL
;
1166 packed
= PACKED_PARTIAL
;
1169 /* For classarrays the element type is required, but
1170 gfc_typenode_for_spec () returns the array descriptor. */
1171 type
= is_classarray
? gfc_get_element_type (type
)
1172 : gfc_typenode_for_spec (&sym
->ts
);
1173 type
= gfc_get_nodesc_array_type (type
, as
, packed
,
1178 /* We now have an expression for the element size, so create a fully
1179 qualified type. Reset sym->backend decl or this will just return the
1181 DECL_ARTIFICIAL (sym
->backend_decl
) = 1;
1182 sym
->backend_decl
= NULL_TREE
;
1183 type
= gfc_sym_type (sym
);
1184 packed
= PACKED_FULL
;
1187 ASM_FORMAT_PRIVATE_NAME (name
, IDENTIFIER_POINTER (DECL_NAME (dummy
)), 0);
1188 decl
= build_decl (input_location
,
1189 VAR_DECL
, get_identifier (name
), type
);
1191 DECL_ARTIFICIAL (decl
) = 1;
1192 DECL_NAMELESS (decl
) = 1;
1193 TREE_PUBLIC (decl
) = 0;
1194 TREE_STATIC (decl
) = 0;
1195 DECL_EXTERNAL (decl
) = 0;
1197 /* Avoid uninitialized warnings for optional dummy arguments. */
1198 if (sym
->attr
.optional
)
1199 TREE_NO_WARNING (decl
) = 1;
1201 /* We should never get deferred shape arrays here. We used to because of
1203 gcc_assert (as
->type
!= AS_DEFERRED
);
1205 if (packed
== PACKED_PARTIAL
)
1206 GFC_DECL_PARTIAL_PACKED_ARRAY (decl
) = 1;
1207 else if (packed
== PACKED_FULL
)
1208 GFC_DECL_PACKED_ARRAY (decl
) = 1;
1210 gfc_build_qualified_array (decl
, sym
);
1212 if (DECL_LANG_SPECIFIC (dummy
))
1213 DECL_LANG_SPECIFIC (decl
) = DECL_LANG_SPECIFIC (dummy
);
1215 gfc_allocate_lang_decl (decl
);
1217 GFC_DECL_SAVED_DESCRIPTOR (decl
) = dummy
;
1219 if (sym
->ns
->proc_name
->backend_decl
== current_function_decl
1220 || sym
->attr
.contained
)
1221 gfc_add_decl_to_function (decl
);
1223 gfc_add_decl_to_parent_function (decl
);
1228 /* For symbol SYM with GFC_DECL_SAVED_DESCRIPTOR used in contained
1229 function add a VAR_DECL to the current function with DECL_VALUE_EXPR
1230 pointing to the artificial variable for debug info purposes. */
1233 gfc_nonlocal_dummy_array_decl (gfc_symbol
*sym
)
1237 if (! nonlocal_dummy_decl_pset
)
1238 nonlocal_dummy_decl_pset
= new hash_set
<tree
>;
1240 if (nonlocal_dummy_decl_pset
->add (sym
->backend_decl
))
1243 dummy
= GFC_DECL_SAVED_DESCRIPTOR (sym
->backend_decl
);
1244 decl
= build_decl (input_location
, VAR_DECL
, DECL_NAME (dummy
),
1245 TREE_TYPE (sym
->backend_decl
));
1246 DECL_ARTIFICIAL (decl
) = 0;
1247 TREE_USED (decl
) = 1;
1248 TREE_PUBLIC (decl
) = 0;
1249 TREE_STATIC (decl
) = 0;
1250 DECL_EXTERNAL (decl
) = 0;
1251 if (DECL_BY_REFERENCE (dummy
))
1252 DECL_BY_REFERENCE (decl
) = 1;
1253 DECL_LANG_SPECIFIC (decl
) = DECL_LANG_SPECIFIC (sym
->backend_decl
);
1254 SET_DECL_VALUE_EXPR (decl
, sym
->backend_decl
);
1255 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
1256 DECL_CONTEXT (decl
) = DECL_CONTEXT (sym
->backend_decl
);
1257 DECL_CHAIN (decl
) = nonlocal_dummy_decls
;
1258 nonlocal_dummy_decls
= decl
;
1261 /* Return a constant or a variable to use as a string length. Does not
1262 add the decl to the current scope. */
1265 gfc_create_string_length (gfc_symbol
* sym
)
1267 gcc_assert (sym
->ts
.u
.cl
);
1268 gfc_conv_const_charlen (sym
->ts
.u
.cl
);
1270 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
1275 /* The string length variable shall be in static memory if it is either
1276 explicitly SAVED, a module variable or with -fno-automatic. Only
1277 relevant is "len=:" - otherwise, it is either a constant length or
1278 it is an automatic variable. */
1279 bool static_length
= sym
->attr
.save
1280 || sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
1281 || (flag_max_stack_var_size
== 0
1282 && sym
->ts
.deferred
&& !sym
->attr
.dummy
1283 && !sym
->attr
.result
&& !sym
->attr
.function
);
1285 /* Also prefix the mangled name. We need to call GFC_PREFIX for static
1286 variables as some systems do not support the "." in the assembler name.
1287 For nonstatic variables, the "." does not appear in assembler. */
1291 name
= gfc_get_string (GFC_PREFIX ("%s_MOD_%s"), sym
->module
,
1294 name
= gfc_get_string (GFC_PREFIX ("%s"), sym
->name
);
1296 else if (sym
->module
)
1297 name
= gfc_get_string (".__%s_MOD_%s", sym
->module
, sym
->name
);
1299 name
= gfc_get_string (".%s", sym
->name
);
1301 length
= build_decl (input_location
,
1302 VAR_DECL
, get_identifier (name
),
1303 gfc_charlen_type_node
);
1304 DECL_ARTIFICIAL (length
) = 1;
1305 TREE_USED (length
) = 1;
1306 if (sym
->ns
->proc_name
->tlink
!= NULL
)
1307 gfc_defer_symbol_init (sym
);
1309 sym
->ts
.u
.cl
->backend_decl
= length
;
1312 TREE_STATIC (length
) = 1;
1314 if (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
1315 && (sym
->attr
.access
!= ACCESS_PRIVATE
|| sym
->attr
.public_used
))
1316 TREE_PUBLIC (length
) = 1;
1319 gcc_assert (sym
->ts
.u
.cl
->backend_decl
!= NULL_TREE
);
1320 return sym
->ts
.u
.cl
->backend_decl
;
1323 /* If a variable is assigned a label, we add another two auxiliary
1327 gfc_add_assign_aux_vars (gfc_symbol
* sym
)
1333 gcc_assert (sym
->backend_decl
);
1335 decl
= sym
->backend_decl
;
1336 gfc_allocate_lang_decl (decl
);
1337 GFC_DECL_ASSIGN (decl
) = 1;
1338 length
= build_decl (input_location
,
1339 VAR_DECL
, create_tmp_var_name (sym
->name
),
1340 gfc_charlen_type_node
);
1341 addr
= build_decl (input_location
,
1342 VAR_DECL
, create_tmp_var_name (sym
->name
),
1344 gfc_finish_var_decl (length
, sym
);
1345 gfc_finish_var_decl (addr
, sym
);
1346 /* STRING_LENGTH is also used as flag. Less than -1 means that
1347 ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the
1348 target label's address. Otherwise, value is the length of a format string
1349 and ASSIGN_ADDR is its address. */
1350 if (TREE_STATIC (length
))
1351 DECL_INITIAL (length
) = build_int_cst (gfc_charlen_type_node
, -2);
1353 gfc_defer_symbol_init (sym
);
1355 GFC_DECL_STRING_LEN (decl
) = length
;
1356 GFC_DECL_ASSIGN_ADDR (decl
) = addr
;
1361 add_attributes_to_decl (symbol_attribute sym_attr
, tree list
)
1366 for (id
= 0; id
< EXT_ATTR_NUM
; id
++)
1367 if (sym_attr
.ext_attr
& (1 << id
))
1369 attr
= build_tree_list (
1370 get_identifier (ext_attr_list
[id
].middle_end_name
),
1372 list
= chainon (list
, attr
);
1375 if (sym_attr
.omp_declare_target
)
1376 list
= tree_cons (get_identifier ("omp declare target"),
1379 if (sym_attr
.oacc_function
)
1381 tree dims
= NULL_TREE
;
1383 int level
= sym_attr
.oacc_function
- 1;
1385 for (ix
= GOMP_DIM_MAX
; ix
--;)
1386 dims
= tree_cons (build_int_cst (boolean_type_node
, ix
>= level
),
1387 integer_zero_node
, dims
);
1389 list
= tree_cons (get_identifier ("oacc function"),
1397 static void build_function_decl (gfc_symbol
* sym
, bool global
);
1400 /* Return the decl for a gfc_symbol, create it if it doesn't already
1404 gfc_get_symbol_decl (gfc_symbol
* sym
)
1407 tree length
= NULL_TREE
;
1410 bool intrinsic_array_parameter
= false;
1413 gcc_assert (sym
->attr
.referenced
1414 || sym
->attr
.flavor
== FL_PROCEDURE
1415 || sym
->attr
.use_assoc
1416 || sym
->attr
.used_in_submodule
1417 || sym
->ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
1418 || (sym
->module
&& sym
->attr
.if_source
!= IFSRC_DECL
1419 && sym
->backend_decl
));
1421 if (sym
->ns
&& sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.function
)
1422 byref
= gfc_return_by_reference (sym
->ns
->proc_name
);
1426 /* Make sure that the vtab for the declared type is completed. */
1427 if (sym
->ts
.type
== BT_CLASS
)
1429 gfc_component
*c
= CLASS_DATA (sym
);
1430 if (!c
->ts
.u
.derived
->backend_decl
)
1432 gfc_find_derived_vtab (c
->ts
.u
.derived
);
1433 gfc_get_derived_type (sym
->ts
.u
.derived
);
1437 /* All deferred character length procedures need to retain the backend
1438 decl, which is a pointer to the character length in the caller's
1439 namespace and to declare a local character length. */
1440 if (!byref
&& sym
->attr
.function
1441 && sym
->ts
.type
== BT_CHARACTER
1443 && sym
->ts
.u
.cl
->passed_length
== NULL
1444 && sym
->ts
.u
.cl
->backend_decl
1445 && TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
1447 sym
->ts
.u
.cl
->passed_length
= sym
->ts
.u
.cl
->backend_decl
;
1448 gcc_assert (POINTER_TYPE_P (TREE_TYPE (sym
->ts
.u
.cl
->passed_length
)));
1449 sym
->ts
.u
.cl
->backend_decl
= build_fold_indirect_ref (sym
->ts
.u
.cl
->backend_decl
);
1452 fun_or_res
= byref
&& (sym
->attr
.result
1453 || (sym
->attr
.function
&& sym
->ts
.deferred
));
1454 if ((sym
->attr
.dummy
&& ! sym
->attr
.function
) || fun_or_res
)
1456 /* Return via extra parameter. */
1457 if (sym
->attr
.result
&& byref
1458 && !sym
->backend_decl
)
1461 DECL_ARGUMENTS (sym
->ns
->proc_name
->backend_decl
);
1462 /* For entry master function skip over the __entry
1464 if (sym
->ns
->proc_name
->attr
.entry_master
)
1465 sym
->backend_decl
= DECL_CHAIN (sym
->backend_decl
);
1468 /* Dummy variables should already have been created. */
1469 gcc_assert (sym
->backend_decl
);
1471 /* Create a character length variable. */
1472 if (sym
->ts
.type
== BT_CHARACTER
)
1474 /* For a deferred dummy, make a new string length variable. */
1475 if (sym
->ts
.deferred
1477 (sym
->ts
.u
.cl
->passed_length
== sym
->ts
.u
.cl
->backend_decl
))
1478 sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
1480 if (sym
->ts
.deferred
&& byref
)
1482 /* The string length of a deferred char array is stored in the
1483 parameter at sym->ts.u.cl->backend_decl as a reference and
1484 marked as a result. Exempt this variable from generating a
1485 temporary for it. */
1486 if (sym
->attr
.result
)
1488 /* We need to insert a indirect ref for param decls. */
1489 if (sym
->ts
.u
.cl
->backend_decl
1490 && TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
1492 sym
->ts
.u
.cl
->passed_length
= sym
->ts
.u
.cl
->backend_decl
;
1493 sym
->ts
.u
.cl
->backend_decl
=
1494 build_fold_indirect_ref (sym
->ts
.u
.cl
->backend_decl
);
1497 /* For all other parameters make sure, that they are copied so
1498 that the value and any modifications are local to the routine
1499 by generating a temporary variable. */
1500 else if (sym
->attr
.function
1501 && sym
->ts
.u
.cl
->passed_length
== NULL
1502 && sym
->ts
.u
.cl
->backend_decl
)
1504 sym
->ts
.u
.cl
->passed_length
= sym
->ts
.u
.cl
->backend_decl
;
1505 if (POINTER_TYPE_P (TREE_TYPE (sym
->ts
.u
.cl
->passed_length
)))
1506 sym
->ts
.u
.cl
->backend_decl
1507 = build_fold_indirect_ref (sym
->ts
.u
.cl
->backend_decl
);
1509 sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
1513 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
1514 length
= gfc_create_string_length (sym
);
1516 length
= sym
->ts
.u
.cl
->backend_decl
;
1517 if (TREE_CODE (length
) == VAR_DECL
1518 && DECL_FILE_SCOPE_P (length
))
1520 /* Add the string length to the same context as the symbol. */
1521 if (DECL_CONTEXT (sym
->backend_decl
) == current_function_decl
)
1522 gfc_add_decl_to_function (length
);
1524 gfc_add_decl_to_parent_function (length
);
1526 gcc_assert (DECL_CONTEXT (sym
->backend_decl
) ==
1527 DECL_CONTEXT (length
));
1529 gfc_defer_symbol_init (sym
);
1533 /* Use a copy of the descriptor for dummy arrays. */
1534 if ((sym
->attr
.dimension
|| sym
->attr
.codimension
)
1535 && !TREE_USED (sym
->backend_decl
))
1537 decl
= gfc_build_dummy_array_decl (sym
, sym
->backend_decl
);
1538 /* Prevent the dummy from being detected as unused if it is copied. */
1539 if (sym
->backend_decl
!= NULL
&& decl
!= sym
->backend_decl
)
1540 DECL_ARTIFICIAL (sym
->backend_decl
) = 1;
1541 sym
->backend_decl
= decl
;
1544 /* Returning the descriptor for dummy class arrays is hazardous, because
1545 some caller is expecting an expression to apply the component refs to.
1546 Therefore the descriptor is only created and stored in
1547 sym->backend_decl's GFC_DECL_SAVED_DESCRIPTOR. The caller is then
1548 responsible to extract it from there, when the descriptor is
1550 if (IS_CLASS_ARRAY (sym
)
1551 && (!DECL_LANG_SPECIFIC (sym
->backend_decl
)
1552 || !GFC_DECL_SAVED_DESCRIPTOR (sym
->backend_decl
)))
1554 decl
= gfc_build_dummy_array_decl (sym
, sym
->backend_decl
);
1555 /* Prevent the dummy from being detected as unused if it is copied. */
1556 if (sym
->backend_decl
!= NULL
&& decl
!= sym
->backend_decl
)
1557 DECL_ARTIFICIAL (sym
->backend_decl
) = 1;
1558 sym
->backend_decl
= decl
;
1561 TREE_USED (sym
->backend_decl
) = 1;
1562 if (sym
->attr
.assign
&& GFC_DECL_ASSIGN (sym
->backend_decl
) == 0)
1564 gfc_add_assign_aux_vars (sym
);
1567 if ((sym
->attr
.dimension
|| IS_CLASS_ARRAY (sym
))
1568 && DECL_LANG_SPECIFIC (sym
->backend_decl
)
1569 && GFC_DECL_SAVED_DESCRIPTOR (sym
->backend_decl
)
1570 && DECL_CONTEXT (sym
->backend_decl
) != current_function_decl
)
1571 gfc_nonlocal_dummy_array_decl (sym
);
1573 if (sym
->ts
.type
== BT_CLASS
&& sym
->backend_decl
)
1574 GFC_DECL_CLASS(sym
->backend_decl
) = 1;
1576 return sym
->backend_decl
;
1579 if (sym
->backend_decl
)
1580 return sym
->backend_decl
;
1582 /* Special case for array-valued named constants from intrinsic
1583 procedures; those are inlined. */
1584 if (sym
->attr
.use_assoc
&& sym
->attr
.flavor
== FL_PARAMETER
1585 && (sym
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
1586 || sym
->from_intmod
== INTMOD_ISO_C_BINDING
))
1587 intrinsic_array_parameter
= true;
1589 /* If use associated compilation, use the module
1591 if ((sym
->attr
.flavor
== FL_VARIABLE
1592 || sym
->attr
.flavor
== FL_PARAMETER
)
1593 && (sym
->attr
.use_assoc
|| sym
->attr
.used_in_submodule
)
1594 && !intrinsic_array_parameter
1596 && gfc_get_module_backend_decl (sym
))
1598 if (sym
->ts
.type
== BT_CLASS
&& sym
->backend_decl
)
1599 GFC_DECL_CLASS(sym
->backend_decl
) = 1;
1600 return sym
->backend_decl
;
1603 if (sym
->attr
.flavor
== FL_PROCEDURE
)
1605 /* Catch functions. Only used for actual parameters,
1606 procedure pointers and procptr initialization targets. */
1607 if (sym
->attr
.use_assoc
|| sym
->attr
.intrinsic
1608 || sym
->attr
.if_source
!= IFSRC_DECL
)
1610 decl
= gfc_get_extern_function_decl (sym
);
1611 gfc_set_decl_location (decl
, &sym
->declared_at
);
1615 if (!sym
->backend_decl
)
1616 build_function_decl (sym
, false);
1617 decl
= sym
->backend_decl
;
1622 if (sym
->attr
.intrinsic
)
1623 gfc_internal_error ("intrinsic variable which isn't a procedure");
1625 /* Create string length decl first so that they can be used in the
1626 type declaration. For associate names, the target character
1627 length is used. Set 'length' to a constant so that if the
1628 string lenght is a variable, it is not finished a second time. */
1629 if (sym
->ts
.type
== BT_CHARACTER
)
1631 if (sym
->attr
.associate_var
1632 && sym
->ts
.u
.cl
->backend_decl
1633 && TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) == VAR_DECL
)
1634 length
= gfc_index_zero_node
;
1636 length
= gfc_create_string_length (sym
);
1639 /* Create the decl for the variable. */
1640 decl
= build_decl (sym
->declared_at
.lb
->location
,
1641 VAR_DECL
, gfc_sym_identifier (sym
), gfc_sym_type (sym
));
1643 /* Add attributes to variables. Functions are handled elsewhere. */
1644 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
1645 decl_attributes (&decl
, attributes
, 0);
1647 /* Symbols from modules should have their assembler names mangled.
1648 This is done here rather than in gfc_finish_var_decl because it
1649 is different for string length variables. */
1652 gfc_set_decl_assembler_name (decl
, gfc_sym_mangled_identifier (sym
));
1653 if (sym
->attr
.use_assoc
&& !intrinsic_array_parameter
)
1654 DECL_IGNORED_P (decl
) = 1;
1657 if (sym
->attr
.select_type_temporary
)
1659 DECL_ARTIFICIAL (decl
) = 1;
1660 DECL_IGNORED_P (decl
) = 1;
1663 if (sym
->attr
.dimension
|| sym
->attr
.codimension
)
1665 /* Create variables to hold the non-constant bits of array info. */
1666 gfc_build_qualified_array (decl
, sym
);
1668 if (sym
->attr
.contiguous
1669 || ((sym
->attr
.allocatable
|| !sym
->attr
.dummy
) && !sym
->attr
.pointer
))
1670 GFC_DECL_PACKED_ARRAY (decl
) = 1;
1673 /* Remember this variable for allocation/cleanup. */
1674 if (sym
->attr
.dimension
|| sym
->attr
.allocatable
|| sym
->attr
.codimension
1675 || (sym
->ts
.type
== BT_CLASS
&&
1676 (CLASS_DATA (sym
)->attr
.dimension
1677 || CLASS_DATA (sym
)->attr
.allocatable
))
1678 || (sym
->ts
.type
== BT_DERIVED
1679 && (sym
->ts
.u
.derived
->attr
.alloc_comp
1680 || (!sym
->attr
.pointer
&& !sym
->attr
.artificial
&& !sym
->attr
.save
1681 && !sym
->ns
->proc_name
->attr
.is_main_program
1682 && gfc_is_finalizable (sym
->ts
.u
.derived
, NULL
))))
1683 /* This applies a derived type default initializer. */
1684 || (sym
->ts
.type
== BT_DERIVED
1685 && sym
->attr
.save
== SAVE_NONE
1687 && !sym
->attr
.allocatable
1688 && (sym
->value
&& !sym
->ns
->proc_name
->attr
.is_main_program
)
1689 && !(sym
->attr
.use_assoc
&& !intrinsic_array_parameter
)))
1690 gfc_defer_symbol_init (sym
);
1692 /* Associate names can use the hidden string length variable
1693 of their associated target. */
1694 if (sym
->ts
.type
== BT_CHARACTER
1695 && TREE_CODE (length
) != INTEGER_CST
)
1697 gfc_finish_var_decl (length
, sym
);
1698 gcc_assert (!sym
->value
);
1701 gfc_finish_var_decl (decl
, sym
);
1703 if (sym
->ts
.type
== BT_CHARACTER
)
1704 /* Character variables need special handling. */
1705 gfc_allocate_lang_decl (decl
);
1706 else if (sym
->attr
.subref_array_pointer
)
1707 /* We need the span for these beasts. */
1708 gfc_allocate_lang_decl (decl
);
1710 if (sym
->attr
.subref_array_pointer
)
1713 GFC_DECL_SUBREF_ARRAY_P (decl
) = 1;
1714 span
= build_decl (input_location
,
1715 VAR_DECL
, create_tmp_var_name ("span"),
1716 gfc_array_index_type
);
1717 gfc_finish_var_decl (span
, sym
);
1718 TREE_STATIC (span
) = TREE_STATIC (decl
);
1719 DECL_ARTIFICIAL (span
) = 1;
1721 GFC_DECL_SPAN (decl
) = span
;
1722 GFC_TYPE_ARRAY_SPAN (TREE_TYPE (decl
)) = span
;
1725 if (sym
->ts
.type
== BT_CLASS
)
1726 GFC_DECL_CLASS(decl
) = 1;
1728 sym
->backend_decl
= decl
;
1730 if (sym
->attr
.assign
)
1731 gfc_add_assign_aux_vars (sym
);
1733 if (intrinsic_array_parameter
)
1735 TREE_STATIC (decl
) = 1;
1736 DECL_EXTERNAL (decl
) = 0;
1739 if (TREE_STATIC (decl
)
1740 && !(sym
->attr
.use_assoc
&& !intrinsic_array_parameter
)
1741 && (sym
->attr
.save
|| sym
->ns
->proc_name
->attr
.is_main_program
1742 || !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl
))
1743 || sym
->attr
.data
|| sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
1744 && (flag_coarray
!= GFC_FCOARRAY_LIB
1745 || !sym
->attr
.codimension
|| sym
->attr
.allocatable
))
1747 /* Add static initializer. For procedures, it is only needed if
1748 SAVE is specified otherwise they need to be reinitialized
1749 every time the procedure is entered. The TREE_STATIC is
1750 in this case due to -fmax-stack-var-size=. */
1752 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
1753 TREE_TYPE (decl
), sym
->attr
.dimension
1754 || (sym
->attr
.codimension
1755 && sym
->attr
.allocatable
),
1756 sym
->attr
.pointer
|| sym
->attr
.allocatable
1757 || sym
->ts
.type
== BT_CLASS
,
1758 sym
->attr
.proc_pointer
);
1761 if (!TREE_STATIC (decl
)
1762 && POINTER_TYPE_P (TREE_TYPE (decl
))
1763 && !sym
->attr
.pointer
1764 && !sym
->attr
.allocatable
1765 && !sym
->attr
.proc_pointer
1766 && !sym
->attr
.select_type_temporary
)
1767 DECL_BY_REFERENCE (decl
) = 1;
1769 if (sym
->attr
.associate_var
)
1770 GFC_DECL_ASSOCIATE_VAR_P (decl
) = 1;
1773 || (sym
->name
[0] == '_' && strncmp ("__def_init", sym
->name
, 10) == 0))
1774 TREE_READONLY (decl
) = 1;
1780 /* Substitute a temporary variable in place of the real one. */
1783 gfc_shadow_sym (gfc_symbol
* sym
, tree decl
, gfc_saved_var
* save
)
1785 save
->attr
= sym
->attr
;
1786 save
->decl
= sym
->backend_decl
;
1788 gfc_clear_attr (&sym
->attr
);
1789 sym
->attr
.referenced
= 1;
1790 sym
->attr
.flavor
= FL_VARIABLE
;
1792 sym
->backend_decl
= decl
;
1796 /* Restore the original variable. */
1799 gfc_restore_sym (gfc_symbol
* sym
, gfc_saved_var
* save
)
1801 sym
->attr
= save
->attr
;
1802 sym
->backend_decl
= save
->decl
;
1806 /* Declare a procedure pointer. */
1809 get_proc_pointer_decl (gfc_symbol
*sym
)
1814 decl
= sym
->backend_decl
;
1818 decl
= build_decl (input_location
,
1819 VAR_DECL
, get_identifier (sym
->name
),
1820 build_pointer_type (gfc_get_function_type (sym
)));
1824 /* Apply name mangling. */
1825 gfc_set_decl_assembler_name (decl
, gfc_sym_mangled_identifier (sym
));
1826 if (sym
->attr
.use_assoc
)
1827 DECL_IGNORED_P (decl
) = 1;
1830 if ((sym
->ns
->proc_name
1831 && sym
->ns
->proc_name
->backend_decl
== current_function_decl
)
1832 || sym
->attr
.contained
)
1833 gfc_add_decl_to_function (decl
);
1834 else if (sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
1835 gfc_add_decl_to_parent_function (decl
);
1837 sym
->backend_decl
= decl
;
1839 /* If a variable is USE associated, it's always external. */
1840 if (sym
->attr
.use_assoc
)
1842 DECL_EXTERNAL (decl
) = 1;
1843 TREE_PUBLIC (decl
) = 1;
1845 else if (sym
->module
&& sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
1847 /* This is the declaration of a module variable. */
1848 TREE_PUBLIC (decl
) = 1;
1849 if (sym
->attr
.access
== ACCESS_PRIVATE
&& !sym
->attr
.public_used
)
1851 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
1852 DECL_VISIBILITY_SPECIFIED (decl
) = true;
1854 TREE_STATIC (decl
) = 1;
1857 if (!sym
->attr
.use_assoc
1858 && (sym
->attr
.save
!= SAVE_NONE
|| sym
->attr
.data
1859 || (sym
->value
&& sym
->ns
->proc_name
->attr
.is_main_program
)))
1860 TREE_STATIC (decl
) = 1;
1862 if (TREE_STATIC (decl
) && sym
->value
)
1864 /* Add static initializer. */
1865 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
1867 sym
->attr
.dimension
,
1871 /* Handle threadprivate procedure pointers. */
1872 if (sym
->attr
.threadprivate
1873 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1874 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
1876 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
1877 decl_attributes (&decl
, attributes
, 0);
1883 /* Get a basic decl for an external function. */
1886 gfc_get_extern_function_decl (gfc_symbol
* sym
)
1892 gfc_intrinsic_sym
*isym
;
1894 char s
[GFC_MAX_SYMBOL_LEN
+ 23]; /* "_gfortran_f2c_specific" and '\0'. */
1899 if (sym
->backend_decl
)
1900 return sym
->backend_decl
;
1902 /* We should never be creating external decls for alternate entry points.
1903 The procedure may be an alternate entry point, but we don't want/need
1905 gcc_assert (!(sym
->attr
.entry
|| sym
->attr
.entry_master
));
1907 if (sym
->attr
.proc_pointer
)
1908 return get_proc_pointer_decl (sym
);
1910 /* See if this is an external procedure from the same file. If so,
1911 return the backend_decl. */
1912 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->binding_label
1913 ? sym
->binding_label
: sym
->name
);
1915 if (gsym
&& !gsym
->defined
)
1918 /* This can happen because of C binding. */
1919 if (gsym
&& gsym
->ns
&& gsym
->ns
->proc_name
1920 && gsym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
1923 if ((!sym
->attr
.use_assoc
|| sym
->attr
.if_source
!= IFSRC_DECL
)
1924 && !sym
->backend_decl
1926 && ((gsym
->type
== GSYM_SUBROUTINE
) || (gsym
->type
== GSYM_FUNCTION
))
1927 && (gsym
->ns
->proc_name
->backend_decl
|| !sym
->attr
.intrinsic
))
1929 if (!gsym
->ns
->proc_name
->backend_decl
)
1931 /* By construction, the external function cannot be
1932 a contained procedure. */
1935 gfc_save_backend_locus (&old_loc
);
1938 gfc_create_function_decl (gsym
->ns
, true);
1941 gfc_restore_backend_locus (&old_loc
);
1944 /* If the namespace has entries, the proc_name is the
1945 entry master. Find the entry and use its backend_decl.
1946 otherwise, use the proc_name backend_decl. */
1947 if (gsym
->ns
->entries
)
1949 gfc_entry_list
*entry
= gsym
->ns
->entries
;
1951 for (; entry
; entry
= entry
->next
)
1953 if (strcmp (gsym
->name
, entry
->sym
->name
) == 0)
1955 sym
->backend_decl
= entry
->sym
->backend_decl
;
1961 sym
->backend_decl
= gsym
->ns
->proc_name
->backend_decl
;
1963 if (sym
->backend_decl
)
1965 /* Avoid problems of double deallocation of the backend declaration
1966 later in gfc_trans_use_stmts; cf. PR 45087. */
1967 if (sym
->attr
.if_source
!= IFSRC_DECL
&& sym
->attr
.use_assoc
)
1968 sym
->attr
.use_assoc
= 0;
1970 return sym
->backend_decl
;
1974 /* See if this is a module procedure from the same file. If so,
1975 return the backend_decl. */
1977 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->module
);
1980 if (gsym
&& gsym
->ns
1981 && (gsym
->type
== GSYM_MODULE
1982 || (gsym
->ns
->proc_name
&& gsym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)))
1987 if (gsym
->type
== GSYM_MODULE
)
1988 gfc_find_symbol (sym
->name
, gsym
->ns
, 0, &s
);
1990 gfc_find_symbol (gsym
->sym_name
, gsym
->ns
, 0, &s
);
1992 if (s
&& s
->backend_decl
)
1994 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
1995 gfc_copy_dt_decls_ifequal (s
->ts
.u
.derived
, sym
->ts
.u
.derived
,
1997 else if (sym
->ts
.type
== BT_CHARACTER
)
1998 sym
->ts
.u
.cl
->backend_decl
= s
->ts
.u
.cl
->backend_decl
;
1999 sym
->backend_decl
= s
->backend_decl
;
2000 return sym
->backend_decl
;
2004 if (sym
->attr
.intrinsic
)
2006 /* Call the resolution function to get the actual name. This is
2007 a nasty hack which relies on the resolution functions only looking
2008 at the first argument. We pass NULL for the second argument
2009 otherwise things like AINT get confused. */
2010 isym
= gfc_find_function (sym
->name
);
2011 gcc_assert (isym
->resolve
.f0
!= NULL
);
2013 memset (&e
, 0, sizeof (e
));
2014 e
.expr_type
= EXPR_FUNCTION
;
2016 memset (&argexpr
, 0, sizeof (argexpr
));
2017 gcc_assert (isym
->formal
);
2018 argexpr
.ts
= isym
->formal
->ts
;
2020 if (isym
->formal
->next
== NULL
)
2021 isym
->resolve
.f1 (&e
, &argexpr
);
2024 if (isym
->formal
->next
->next
== NULL
)
2025 isym
->resolve
.f2 (&e
, &argexpr
, NULL
);
2028 if (isym
->formal
->next
->next
->next
== NULL
)
2029 isym
->resolve
.f3 (&e
, &argexpr
, NULL
, NULL
);
2032 /* All specific intrinsics take less than 5 arguments. */
2033 gcc_assert (isym
->formal
->next
->next
->next
->next
== NULL
);
2034 isym
->resolve
.f4 (&e
, &argexpr
, NULL
, NULL
, NULL
);
2040 && ((e
.ts
.type
== BT_REAL
&& e
.ts
.kind
== gfc_default_real_kind
)
2041 || e
.ts
.type
== BT_COMPLEX
))
2043 /* Specific which needs a different implementation if f2c
2044 calling conventions are used. */
2045 sprintf (s
, "_gfortran_f2c_specific%s", e
.value
.function
.name
);
2048 sprintf (s
, "_gfortran_specific%s", e
.value
.function
.name
);
2050 name
= get_identifier (s
);
2051 mangled_name
= name
;
2055 name
= gfc_sym_identifier (sym
);
2056 mangled_name
= gfc_sym_mangled_function_id (sym
);
2059 type
= gfc_get_function_type (sym
);
2060 fndecl
= build_decl (input_location
,
2061 FUNCTION_DECL
, name
, type
);
2063 /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
2064 TREE_PUBLIC specifies whether a function is globally addressable (i.e.
2065 the opposite of declaring a function as static in C). */
2066 DECL_EXTERNAL (fndecl
) = 1;
2067 TREE_PUBLIC (fndecl
) = 1;
2069 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
2070 decl_attributes (&fndecl
, attributes
, 0);
2072 gfc_set_decl_assembler_name (fndecl
, mangled_name
);
2074 /* Set the context of this decl. */
2075 if (0 && sym
->ns
&& sym
->ns
->proc_name
)
2077 /* TODO: Add external decls to the appropriate scope. */
2078 DECL_CONTEXT (fndecl
) = sym
->ns
->proc_name
->backend_decl
;
2082 /* Global declaration, e.g. intrinsic subroutine. */
2083 DECL_CONTEXT (fndecl
) = NULL_TREE
;
2086 /* Set attributes for PURE functions. A call to PURE function in the
2087 Fortran 95 sense is both pure and without side effects in the C
2089 if (sym
->attr
.pure
|| sym
->attr
.implicit_pure
)
2091 if (sym
->attr
.function
&& !gfc_return_by_reference (sym
))
2092 DECL_PURE_P (fndecl
) = 1;
2093 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
2094 parameters and don't use alternate returns (is this
2095 allowed?). In that case, calls to them are meaningless, and
2096 can be optimized away. See also in build_function_decl(). */
2097 TREE_SIDE_EFFECTS (fndecl
) = 0;
2100 /* Mark non-returning functions. */
2101 if (sym
->attr
.noreturn
)
2102 TREE_THIS_VOLATILE(fndecl
) = 1;
2104 sym
->backend_decl
= fndecl
;
2106 if (DECL_CONTEXT (fndecl
) == NULL_TREE
)
2107 pushdecl_top_level (fndecl
);
2110 && sym
->formal_ns
->proc_name
== sym
2111 && sym
->formal_ns
->omp_declare_simd
)
2112 gfc_trans_omp_declare_simd (sym
->formal_ns
);
2118 /* Create a declaration for a procedure. For external functions (in the C
2119 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
2120 a master function with alternate entry points. */
2123 build_function_decl (gfc_symbol
* sym
, bool global
)
2125 tree fndecl
, type
, attributes
;
2126 symbol_attribute attr
;
2128 gfc_formal_arglist
*f
;
2130 bool module_procedure
= sym
->attr
.module_procedure
2132 && sym
->ns
->proc_name
2133 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
;
2135 gcc_assert (!sym
->attr
.external
|| module_procedure
);
2137 if (sym
->backend_decl
)
2140 /* Set the line and filename. sym->declared_at seems to point to the
2141 last statement for subroutines, but it'll do for now. */
2142 gfc_set_backend_locus (&sym
->declared_at
);
2144 /* Allow only one nesting level. Allow public declarations. */
2145 gcc_assert (current_function_decl
== NULL_TREE
2146 || DECL_FILE_SCOPE_P (current_function_decl
)
2147 || (TREE_CODE (DECL_CONTEXT (current_function_decl
))
2148 == NAMESPACE_DECL
));
2150 type
= gfc_get_function_type (sym
);
2151 fndecl
= build_decl (input_location
,
2152 FUNCTION_DECL
, gfc_sym_identifier (sym
), type
);
2156 /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
2157 TREE_PUBLIC specifies whether a function is globally addressable (i.e.
2158 the opposite of declaring a function as static in C). */
2159 DECL_EXTERNAL (fndecl
) = 0;
2161 if (sym
->attr
.access
== ACCESS_UNKNOWN
&& sym
->module
2162 && (sym
->ns
->default_access
== ACCESS_PRIVATE
2163 || (sym
->ns
->default_access
== ACCESS_UNKNOWN
2164 && flag_module_private
)))
2165 sym
->attr
.access
= ACCESS_PRIVATE
;
2167 if (!current_function_decl
2168 && !sym
->attr
.entry_master
&& !sym
->attr
.is_main_program
2169 && (sym
->attr
.access
!= ACCESS_PRIVATE
|| sym
->binding_label
2170 || sym
->attr
.public_used
))
2171 TREE_PUBLIC (fndecl
) = 1;
2173 if (sym
->attr
.referenced
|| sym
->attr
.entry_master
)
2174 TREE_USED (fndecl
) = 1;
2176 attributes
= add_attributes_to_decl (attr
, NULL_TREE
);
2177 decl_attributes (&fndecl
, attributes
, 0);
2179 /* Figure out the return type of the declared function, and build a
2180 RESULT_DECL for it. If this is a subroutine with alternate
2181 returns, build a RESULT_DECL for it. */
2182 result_decl
= NULL_TREE
;
2183 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
2186 if (gfc_return_by_reference (sym
))
2187 type
= void_type_node
;
2190 if (sym
->result
!= sym
)
2191 result_decl
= gfc_sym_identifier (sym
->result
);
2193 type
= TREE_TYPE (TREE_TYPE (fndecl
));
2198 /* Look for alternate return placeholders. */
2199 int has_alternate_returns
= 0;
2200 for (f
= gfc_sym_get_dummy_args (sym
); f
; f
= f
->next
)
2204 has_alternate_returns
= 1;
2209 if (has_alternate_returns
)
2210 type
= integer_type_node
;
2212 type
= void_type_node
;
2215 result_decl
= build_decl (input_location
,
2216 RESULT_DECL
, result_decl
, type
);
2217 DECL_ARTIFICIAL (result_decl
) = 1;
2218 DECL_IGNORED_P (result_decl
) = 1;
2219 DECL_CONTEXT (result_decl
) = fndecl
;
2220 DECL_RESULT (fndecl
) = result_decl
;
2222 /* Don't call layout_decl for a RESULT_DECL.
2223 layout_decl (result_decl, 0); */
2225 /* TREE_STATIC means the function body is defined here. */
2226 TREE_STATIC (fndecl
) = 1;
2228 /* Set attributes for PURE functions. A call to a PURE function in the
2229 Fortran 95 sense is both pure and without side effects in the C
2231 if (attr
.pure
|| attr
.implicit_pure
)
2233 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
2234 including an alternate return. In that case it can also be
2235 marked as PURE. See also in gfc_get_extern_function_decl(). */
2236 if (attr
.function
&& !gfc_return_by_reference (sym
))
2237 DECL_PURE_P (fndecl
) = 1;
2238 TREE_SIDE_EFFECTS (fndecl
) = 0;
2242 /* Layout the function declaration and put it in the binding level
2243 of the current function. */
2246 pushdecl_top_level (fndecl
);
2250 /* Perform name mangling if this is a top level or module procedure. */
2251 if (current_function_decl
== NULL_TREE
)
2252 gfc_set_decl_assembler_name (fndecl
, gfc_sym_mangled_function_id (sym
));
2254 sym
->backend_decl
= fndecl
;
2258 /* Create the DECL_ARGUMENTS for a procedure. */
2261 create_function_arglist (gfc_symbol
* sym
)
2264 gfc_formal_arglist
*f
;
2265 tree typelist
, hidden_typelist
;
2266 tree arglist
, hidden_arglist
;
2270 fndecl
= sym
->backend_decl
;
2272 /* Build formal argument list. Make sure that their TREE_CONTEXT is
2273 the new FUNCTION_DECL node. */
2274 arglist
= NULL_TREE
;
2275 hidden_arglist
= NULL_TREE
;
2276 typelist
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
2278 if (sym
->attr
.entry_master
)
2280 type
= TREE_VALUE (typelist
);
2281 parm
= build_decl (input_location
,
2282 PARM_DECL
, get_identifier ("__entry"), type
);
2284 DECL_CONTEXT (parm
) = fndecl
;
2285 DECL_ARG_TYPE (parm
) = type
;
2286 TREE_READONLY (parm
) = 1;
2287 gfc_finish_decl (parm
);
2288 DECL_ARTIFICIAL (parm
) = 1;
2290 arglist
= chainon (arglist
, parm
);
2291 typelist
= TREE_CHAIN (typelist
);
2294 if (gfc_return_by_reference (sym
))
2296 tree type
= TREE_VALUE (typelist
), length
= NULL
;
2298 if (sym
->ts
.type
== BT_CHARACTER
)
2300 /* Length of character result. */
2301 tree len_type
= TREE_VALUE (TREE_CHAIN (typelist
));
2303 length
= build_decl (input_location
,
2305 get_identifier (".__result"),
2307 if (POINTER_TYPE_P (len_type
))
2309 sym
->ts
.u
.cl
->passed_length
= length
;
2310 TREE_USED (length
) = 1;
2312 else if (!sym
->ts
.u
.cl
->length
)
2314 sym
->ts
.u
.cl
->backend_decl
= length
;
2315 TREE_USED (length
) = 1;
2317 gcc_assert (TREE_CODE (length
) == PARM_DECL
);
2318 DECL_CONTEXT (length
) = fndecl
;
2319 DECL_ARG_TYPE (length
) = len_type
;
2320 TREE_READONLY (length
) = 1;
2321 DECL_ARTIFICIAL (length
) = 1;
2322 gfc_finish_decl (length
);
2323 if (sym
->ts
.u
.cl
->backend_decl
== NULL
2324 || sym
->ts
.u
.cl
->backend_decl
== length
)
2329 if (sym
->ts
.u
.cl
->backend_decl
== NULL
)
2331 tree len
= build_decl (input_location
,
2333 get_identifier ("..__result"),
2334 gfc_charlen_type_node
);
2335 DECL_ARTIFICIAL (len
) = 1;
2336 TREE_USED (len
) = 1;
2337 sym
->ts
.u
.cl
->backend_decl
= len
;
2340 /* Make sure PARM_DECL type doesn't point to incomplete type. */
2341 arg
= sym
->result
? sym
->result
: sym
;
2342 backend_decl
= arg
->backend_decl
;
2343 /* Temporary clear it, so that gfc_sym_type creates complete
2345 arg
->backend_decl
= NULL
;
2346 type
= gfc_sym_type (arg
);
2347 arg
->backend_decl
= backend_decl
;
2348 type
= build_reference_type (type
);
2352 parm
= build_decl (input_location
,
2353 PARM_DECL
, get_identifier ("__result"), type
);
2355 DECL_CONTEXT (parm
) = fndecl
;
2356 DECL_ARG_TYPE (parm
) = TREE_VALUE (typelist
);
2357 TREE_READONLY (parm
) = 1;
2358 DECL_ARTIFICIAL (parm
) = 1;
2359 gfc_finish_decl (parm
);
2361 arglist
= chainon (arglist
, parm
);
2362 typelist
= TREE_CHAIN (typelist
);
2364 if (sym
->ts
.type
== BT_CHARACTER
)
2366 gfc_allocate_lang_decl (parm
);
2367 arglist
= chainon (arglist
, length
);
2368 typelist
= TREE_CHAIN (typelist
);
2372 hidden_typelist
= typelist
;
2373 for (f
= gfc_sym_get_dummy_args (sym
); f
; f
= f
->next
)
2374 if (f
->sym
!= NULL
) /* Ignore alternate returns. */
2375 hidden_typelist
= TREE_CHAIN (hidden_typelist
);
2377 for (f
= gfc_sym_get_dummy_args (sym
); f
; f
= f
->next
)
2379 char name
[GFC_MAX_SYMBOL_LEN
+ 2];
2381 /* Ignore alternate returns. */
2385 type
= TREE_VALUE (typelist
);
2387 if (f
->sym
->ts
.type
== BT_CHARACTER
2388 && (!sym
->attr
.is_bind_c
|| sym
->attr
.entry_master
))
2390 tree len_type
= TREE_VALUE (hidden_typelist
);
2391 tree length
= NULL_TREE
;
2392 if (!f
->sym
->ts
.deferred
)
2393 gcc_assert (len_type
== gfc_charlen_type_node
);
2395 gcc_assert (POINTER_TYPE_P (len_type
));
2397 strcpy (&name
[1], f
->sym
->name
);
2399 length
= build_decl (input_location
,
2400 PARM_DECL
, get_identifier (name
), len_type
);
2402 hidden_arglist
= chainon (hidden_arglist
, length
);
2403 DECL_CONTEXT (length
) = fndecl
;
2404 DECL_ARTIFICIAL (length
) = 1;
2405 DECL_ARG_TYPE (length
) = len_type
;
2406 TREE_READONLY (length
) = 1;
2407 gfc_finish_decl (length
);
2409 /* Remember the passed value. */
2410 if (!f
->sym
->ts
.u
.cl
|| f
->sym
->ts
.u
.cl
->passed_length
)
2412 /* This can happen if the same type is used for multiple
2413 arguments. We need to copy cl as otherwise
2414 cl->passed_length gets overwritten. */
2415 f
->sym
->ts
.u
.cl
= gfc_new_charlen (f
->sym
->ns
, f
->sym
->ts
.u
.cl
);
2417 f
->sym
->ts
.u
.cl
->passed_length
= length
;
2419 /* Use the passed value for assumed length variables. */
2420 if (!f
->sym
->ts
.u
.cl
->length
)
2422 TREE_USED (length
) = 1;
2423 gcc_assert (!f
->sym
->ts
.u
.cl
->backend_decl
);
2424 f
->sym
->ts
.u
.cl
->backend_decl
= length
;
2427 hidden_typelist
= TREE_CHAIN (hidden_typelist
);
2429 if (f
->sym
->ts
.u
.cl
->backend_decl
== NULL
2430 || f
->sym
->ts
.u
.cl
->backend_decl
== length
)
2432 if (POINTER_TYPE_P (len_type
))
2433 f
->sym
->ts
.u
.cl
->backend_decl
=
2434 build_fold_indirect_ref_loc (input_location
, length
);
2435 else if (f
->sym
->ts
.u
.cl
->backend_decl
== NULL
)
2436 gfc_create_string_length (f
->sym
);
2438 /* Make sure PARM_DECL type doesn't point to incomplete type. */
2439 if (f
->sym
->attr
.flavor
== FL_PROCEDURE
)
2440 type
= build_pointer_type (gfc_get_function_type (f
->sym
));
2442 type
= gfc_sym_type (f
->sym
);
2445 /* For noncharacter scalar intrinsic types, VALUE passes the value,
2446 hence, the optional status cannot be transferred via a NULL pointer.
2447 Thus, we will use a hidden argument in that case. */
2448 else if (f
->sym
->attr
.optional
&& f
->sym
->attr
.value
2449 && !f
->sym
->attr
.dimension
&& f
->sym
->ts
.type
!= BT_CLASS
2450 && !gfc_bt_struct (f
->sym
->ts
.type
))
2453 strcpy (&name
[1], f
->sym
->name
);
2455 tmp
= build_decl (input_location
,
2456 PARM_DECL
, get_identifier (name
),
2459 hidden_arglist
= chainon (hidden_arglist
, tmp
);
2460 DECL_CONTEXT (tmp
) = fndecl
;
2461 DECL_ARTIFICIAL (tmp
) = 1;
2462 DECL_ARG_TYPE (tmp
) = boolean_type_node
;
2463 TREE_READONLY (tmp
) = 1;
2464 gfc_finish_decl (tmp
);
2467 /* For non-constant length array arguments, make sure they use
2468 a different type node from TYPE_ARG_TYPES type. */
2469 if (f
->sym
->attr
.dimension
2470 && type
== TREE_VALUE (typelist
)
2471 && TREE_CODE (type
) == POINTER_TYPE
2472 && GFC_ARRAY_TYPE_P (type
)
2473 && f
->sym
->as
->type
!= AS_ASSUMED_SIZE
2474 && ! COMPLETE_TYPE_P (TREE_TYPE (type
)))
2476 if (f
->sym
->attr
.flavor
== FL_PROCEDURE
)
2477 type
= build_pointer_type (gfc_get_function_type (f
->sym
));
2479 type
= gfc_sym_type (f
->sym
);
2482 if (f
->sym
->attr
.proc_pointer
)
2483 type
= build_pointer_type (type
);
2485 if (f
->sym
->attr
.volatile_
)
2486 type
= build_qualified_type (type
, TYPE_QUAL_VOLATILE
);
2488 /* Build the argument declaration. */
2489 parm
= build_decl (input_location
,
2490 PARM_DECL
, gfc_sym_identifier (f
->sym
), type
);
2492 if (f
->sym
->attr
.volatile_
)
2494 TREE_THIS_VOLATILE (parm
) = 1;
2495 TREE_SIDE_EFFECTS (parm
) = 1;
2498 /* Fill in arg stuff. */
2499 DECL_CONTEXT (parm
) = fndecl
;
2500 DECL_ARG_TYPE (parm
) = TREE_VALUE (typelist
);
2501 /* All implementation args except for VALUE are read-only. */
2502 if (!f
->sym
->attr
.value
)
2503 TREE_READONLY (parm
) = 1;
2504 if (POINTER_TYPE_P (type
)
2505 && (!f
->sym
->attr
.proc_pointer
2506 && f
->sym
->attr
.flavor
!= FL_PROCEDURE
))
2507 DECL_BY_REFERENCE (parm
) = 1;
2509 gfc_finish_decl (parm
);
2510 gfc_finish_decl_attrs (parm
, &f
->sym
->attr
);
2512 f
->sym
->backend_decl
= parm
;
2514 /* Coarrays which are descriptorless or assumed-shape pass with
2515 -fcoarray=lib the token and the offset as hidden arguments. */
2516 if (flag_coarray
== GFC_FCOARRAY_LIB
2517 && ((f
->sym
->ts
.type
!= BT_CLASS
&& f
->sym
->attr
.codimension
2518 && !f
->sym
->attr
.allocatable
)
2519 || (f
->sym
->ts
.type
== BT_CLASS
2520 && CLASS_DATA (f
->sym
)->attr
.codimension
2521 && !CLASS_DATA (f
->sym
)->attr
.allocatable
)))
2527 gcc_assert (f
->sym
->backend_decl
!= NULL_TREE
2528 && !sym
->attr
.is_bind_c
);
2529 caf_type
= f
->sym
->ts
.type
== BT_CLASS
2530 ? TREE_TYPE (CLASS_DATA (f
->sym
)->backend_decl
)
2531 : TREE_TYPE (f
->sym
->backend_decl
);
2533 token
= build_decl (input_location
, PARM_DECL
,
2534 create_tmp_var_name ("caf_token"),
2535 build_qualified_type (pvoid_type_node
,
2536 TYPE_QUAL_RESTRICT
));
2537 if ((f
->sym
->ts
.type
!= BT_CLASS
2538 && f
->sym
->as
->type
!= AS_DEFERRED
)
2539 || (f
->sym
->ts
.type
== BT_CLASS
2540 && CLASS_DATA (f
->sym
)->as
->type
!= AS_DEFERRED
))
2542 gcc_assert (DECL_LANG_SPECIFIC (f
->sym
->backend_decl
) == NULL
2543 || GFC_DECL_TOKEN (f
->sym
->backend_decl
) == NULL_TREE
);
2544 if (DECL_LANG_SPECIFIC (f
->sym
->backend_decl
) == NULL
)
2545 gfc_allocate_lang_decl (f
->sym
->backend_decl
);
2546 GFC_DECL_TOKEN (f
->sym
->backend_decl
) = token
;
2550 gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
) == NULL_TREE
);
2551 GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
) = token
;
2554 DECL_CONTEXT (token
) = fndecl
;
2555 DECL_ARTIFICIAL (token
) = 1;
2556 DECL_ARG_TYPE (token
) = TREE_VALUE (typelist
);
2557 TREE_READONLY (token
) = 1;
2558 hidden_arglist
= chainon (hidden_arglist
, token
);
2559 gfc_finish_decl (token
);
2561 offset
= build_decl (input_location
, PARM_DECL
,
2562 create_tmp_var_name ("caf_offset"),
2563 gfc_array_index_type
);
2565 if ((f
->sym
->ts
.type
!= BT_CLASS
2566 && f
->sym
->as
->type
!= AS_DEFERRED
)
2567 || (f
->sym
->ts
.type
== BT_CLASS
2568 && CLASS_DATA (f
->sym
)->as
->type
!= AS_DEFERRED
))
2570 gcc_assert (GFC_DECL_CAF_OFFSET (f
->sym
->backend_decl
)
2572 GFC_DECL_CAF_OFFSET (f
->sym
->backend_decl
) = offset
;
2576 gcc_assert (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type
) == NULL_TREE
);
2577 GFC_TYPE_ARRAY_CAF_OFFSET (caf_type
) = offset
;
2579 DECL_CONTEXT (offset
) = fndecl
;
2580 DECL_ARTIFICIAL (offset
) = 1;
2581 DECL_ARG_TYPE (offset
) = TREE_VALUE (typelist
);
2582 TREE_READONLY (offset
) = 1;
2583 hidden_arglist
= chainon (hidden_arglist
, offset
);
2584 gfc_finish_decl (offset
);
2587 arglist
= chainon (arglist
, parm
);
2588 typelist
= TREE_CHAIN (typelist
);
2591 /* Add the hidden string length parameters, unless the procedure
2593 if (!sym
->attr
.is_bind_c
)
2594 arglist
= chainon (arglist
, hidden_arglist
);
2596 gcc_assert (hidden_typelist
== NULL_TREE
2597 || TREE_VALUE (hidden_typelist
) == void_type_node
);
2598 DECL_ARGUMENTS (fndecl
) = arglist
;
2601 /* Do the setup necessary before generating the body of a function. */
2604 trans_function_start (gfc_symbol
* sym
)
2608 fndecl
= sym
->backend_decl
;
2610 /* Let GCC know the current scope is this function. */
2611 current_function_decl
= fndecl
;
2613 /* Let the world know what we're about to do. */
2614 announce_function (fndecl
);
2616 if (DECL_FILE_SCOPE_P (fndecl
))
2618 /* Create RTL for function declaration. */
2619 rest_of_decl_compilation (fndecl
, 1, 0);
2622 /* Create RTL for function definition. */
2623 make_decl_rtl (fndecl
);
2625 allocate_struct_function (fndecl
, false);
2627 /* function.c requires a push at the start of the function. */
2631 /* Create thunks for alternate entry points. */
2634 build_entry_thunks (gfc_namespace
* ns
, bool global
)
2636 gfc_formal_arglist
*formal
;
2637 gfc_formal_arglist
*thunk_formal
;
2639 gfc_symbol
*thunk_sym
;
2645 /* This should always be a toplevel function. */
2646 gcc_assert (current_function_decl
== NULL_TREE
);
2648 gfc_save_backend_locus (&old_loc
);
2649 for (el
= ns
->entries
; el
; el
= el
->next
)
2651 vec
<tree
, va_gc
> *args
= NULL
;
2652 vec
<tree
, va_gc
> *string_args
= NULL
;
2654 thunk_sym
= el
->sym
;
2656 build_function_decl (thunk_sym
, global
);
2657 create_function_arglist (thunk_sym
);
2659 trans_function_start (thunk_sym
);
2661 thunk_fndecl
= thunk_sym
->backend_decl
;
2663 gfc_init_block (&body
);
2665 /* Pass extra parameter identifying this entry point. */
2666 tmp
= build_int_cst (gfc_array_index_type
, el
->id
);
2667 vec_safe_push (args
, tmp
);
2669 if (thunk_sym
->attr
.function
)
2671 if (gfc_return_by_reference (ns
->proc_name
))
2673 tree ref
= DECL_ARGUMENTS (current_function_decl
);
2674 vec_safe_push (args
, ref
);
2675 if (ns
->proc_name
->ts
.type
== BT_CHARACTER
)
2676 vec_safe_push (args
, DECL_CHAIN (ref
));
2680 for (formal
= gfc_sym_get_dummy_args (ns
->proc_name
); formal
;
2681 formal
= formal
->next
)
2683 /* Ignore alternate returns. */
2684 if (formal
->sym
== NULL
)
2687 /* We don't have a clever way of identifying arguments, so resort to
2688 a brute-force search. */
2689 for (thunk_formal
= gfc_sym_get_dummy_args (thunk_sym
);
2691 thunk_formal
= thunk_formal
->next
)
2693 if (thunk_formal
->sym
== formal
->sym
)
2699 /* Pass the argument. */
2700 DECL_ARTIFICIAL (thunk_formal
->sym
->backend_decl
) = 1;
2701 vec_safe_push (args
, thunk_formal
->sym
->backend_decl
);
2702 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
2704 tmp
= thunk_formal
->sym
->ts
.u
.cl
->backend_decl
;
2705 vec_safe_push (string_args
, tmp
);
2710 /* Pass NULL for a missing argument. */
2711 vec_safe_push (args
, null_pointer_node
);
2712 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
2714 tmp
= build_int_cst (gfc_charlen_type_node
, 0);
2715 vec_safe_push (string_args
, tmp
);
2720 /* Call the master function. */
2721 vec_safe_splice (args
, string_args
);
2722 tmp
= ns
->proc_name
->backend_decl
;
2723 tmp
= build_call_expr_loc_vec (input_location
, tmp
, args
);
2724 if (ns
->proc_name
->attr
.mixed_entry_master
)
2726 tree union_decl
, field
;
2727 tree master_type
= TREE_TYPE (ns
->proc_name
->backend_decl
);
2729 union_decl
= build_decl (input_location
,
2730 VAR_DECL
, get_identifier ("__result"),
2731 TREE_TYPE (master_type
));
2732 DECL_ARTIFICIAL (union_decl
) = 1;
2733 DECL_EXTERNAL (union_decl
) = 0;
2734 TREE_PUBLIC (union_decl
) = 0;
2735 TREE_USED (union_decl
) = 1;
2736 layout_decl (union_decl
, 0);
2737 pushdecl (union_decl
);
2739 DECL_CONTEXT (union_decl
) = current_function_decl
;
2740 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
2741 TREE_TYPE (union_decl
), union_decl
, tmp
);
2742 gfc_add_expr_to_block (&body
, tmp
);
2744 for (field
= TYPE_FIELDS (TREE_TYPE (union_decl
));
2745 field
; field
= DECL_CHAIN (field
))
2746 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field
)),
2747 thunk_sym
->result
->name
) == 0)
2749 gcc_assert (field
!= NULL_TREE
);
2750 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
,
2751 TREE_TYPE (field
), union_decl
, field
,
2753 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
2754 TREE_TYPE (DECL_RESULT (current_function_decl
)),
2755 DECL_RESULT (current_function_decl
), tmp
);
2756 tmp
= build1_v (RETURN_EXPR
, tmp
);
2758 else if (TREE_TYPE (DECL_RESULT (current_function_decl
))
2761 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
2762 TREE_TYPE (DECL_RESULT (current_function_decl
)),
2763 DECL_RESULT (current_function_decl
), tmp
);
2764 tmp
= build1_v (RETURN_EXPR
, tmp
);
2766 gfc_add_expr_to_block (&body
, tmp
);
2768 /* Finish off this function and send it for code generation. */
2769 DECL_SAVED_TREE (thunk_fndecl
) = gfc_finish_block (&body
);
2772 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl
)) = thunk_fndecl
;
2773 DECL_SAVED_TREE (thunk_fndecl
)
2774 = build3_v (BIND_EXPR
, tmp
, DECL_SAVED_TREE (thunk_fndecl
),
2775 DECL_INITIAL (thunk_fndecl
));
2777 /* Output the GENERIC tree. */
2778 dump_function (TDI_original
, thunk_fndecl
);
2780 /* Store the end of the function, so that we get good line number
2781 info for the epilogue. */
2782 cfun
->function_end_locus
= input_location
;
2784 /* We're leaving the context of this function, so zap cfun.
2785 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
2786 tree_rest_of_compilation. */
2789 current_function_decl
= NULL_TREE
;
2791 cgraph_node::finalize_function (thunk_fndecl
, true);
2793 /* We share the symbols in the formal argument list with other entry
2794 points and the master function. Clear them so that they are
2795 recreated for each function. */
2796 for (formal
= gfc_sym_get_dummy_args (thunk_sym
); formal
;
2797 formal
= formal
->next
)
2798 if (formal
->sym
!= NULL
) /* Ignore alternate returns. */
2800 formal
->sym
->backend_decl
= NULL_TREE
;
2801 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
2802 formal
->sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
2805 if (thunk_sym
->attr
.function
)
2807 if (thunk_sym
->ts
.type
== BT_CHARACTER
)
2808 thunk_sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
2809 if (thunk_sym
->result
->ts
.type
== BT_CHARACTER
)
2810 thunk_sym
->result
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
2814 gfc_restore_backend_locus (&old_loc
);
2818 /* Create a decl for a function, and create any thunks for alternate entry
2819 points. If global is true, generate the function in the global binding
2820 level, otherwise in the current binding level (which can be global). */
2823 gfc_create_function_decl (gfc_namespace
* ns
, bool global
)
2825 /* Create a declaration for the master function. */
2826 build_function_decl (ns
->proc_name
, global
);
2828 /* Compile the entry thunks. */
2830 build_entry_thunks (ns
, global
);
2832 /* Now create the read argument list. */
2833 create_function_arglist (ns
->proc_name
);
2835 if (ns
->omp_declare_simd
)
2836 gfc_trans_omp_declare_simd (ns
);
2839 /* Return the decl used to hold the function return value. If
2840 parent_flag is set, the context is the parent_scope. */
2843 gfc_get_fake_result_decl (gfc_symbol
* sym
, int parent_flag
)
2847 tree this_fake_result_decl
;
2848 tree this_function_decl
;
2850 char name
[GFC_MAX_SYMBOL_LEN
+ 10];
2854 this_fake_result_decl
= parent_fake_result_decl
;
2855 this_function_decl
= DECL_CONTEXT (current_function_decl
);
2859 this_fake_result_decl
= current_fake_result_decl
;
2860 this_function_decl
= current_function_decl
;
2864 && sym
->ns
->proc_name
->backend_decl
== this_function_decl
2865 && sym
->ns
->proc_name
->attr
.entry_master
2866 && sym
!= sym
->ns
->proc_name
)
2869 if (this_fake_result_decl
!= NULL
)
2870 for (t
= TREE_CHAIN (this_fake_result_decl
); t
; t
= TREE_CHAIN (t
))
2871 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t
)), sym
->name
) == 0)
2874 return TREE_VALUE (t
);
2875 decl
= gfc_get_fake_result_decl (sym
->ns
->proc_name
, parent_flag
);
2878 this_fake_result_decl
= parent_fake_result_decl
;
2880 this_fake_result_decl
= current_fake_result_decl
;
2882 if (decl
&& sym
->ns
->proc_name
->attr
.mixed_entry_master
)
2886 for (field
= TYPE_FIELDS (TREE_TYPE (decl
));
2887 field
; field
= DECL_CHAIN (field
))
2888 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field
)),
2892 gcc_assert (field
!= NULL_TREE
);
2893 decl
= fold_build3_loc (input_location
, COMPONENT_REF
,
2894 TREE_TYPE (field
), decl
, field
, NULL_TREE
);
2897 var
= create_tmp_var_raw (TREE_TYPE (decl
), sym
->name
);
2899 gfc_add_decl_to_parent_function (var
);
2901 gfc_add_decl_to_function (var
);
2903 SET_DECL_VALUE_EXPR (var
, decl
);
2904 DECL_HAS_VALUE_EXPR_P (var
) = 1;
2905 GFC_DECL_RESULT (var
) = 1;
2907 TREE_CHAIN (this_fake_result_decl
)
2908 = tree_cons (get_identifier (sym
->name
), var
,
2909 TREE_CHAIN (this_fake_result_decl
));
2913 if (this_fake_result_decl
!= NULL_TREE
)
2914 return TREE_VALUE (this_fake_result_decl
);
2916 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
2921 if (sym
->ts
.type
== BT_CHARACTER
)
2923 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
2924 length
= gfc_create_string_length (sym
);
2926 length
= sym
->ts
.u
.cl
->backend_decl
;
2927 if (TREE_CODE (length
) == VAR_DECL
2928 && DECL_CONTEXT (length
) == NULL_TREE
)
2929 gfc_add_decl_to_function (length
);
2932 if (gfc_return_by_reference (sym
))
2934 decl
= DECL_ARGUMENTS (this_function_decl
);
2936 if (sym
->ns
->proc_name
->backend_decl
== this_function_decl
2937 && sym
->ns
->proc_name
->attr
.entry_master
)
2938 decl
= DECL_CHAIN (decl
);
2940 TREE_USED (decl
) = 1;
2942 decl
= gfc_build_dummy_array_decl (sym
, decl
);
2946 sprintf (name
, "__result_%.20s",
2947 IDENTIFIER_POINTER (DECL_NAME (this_function_decl
)));
2949 if (!sym
->attr
.mixed_entry_master
&& sym
->attr
.function
)
2950 decl
= build_decl (DECL_SOURCE_LOCATION (this_function_decl
),
2951 VAR_DECL
, get_identifier (name
),
2952 gfc_sym_type (sym
));
2954 decl
= build_decl (DECL_SOURCE_LOCATION (this_function_decl
),
2955 VAR_DECL
, get_identifier (name
),
2956 TREE_TYPE (TREE_TYPE (this_function_decl
)));
2957 DECL_ARTIFICIAL (decl
) = 1;
2958 DECL_EXTERNAL (decl
) = 0;
2959 TREE_PUBLIC (decl
) = 0;
2960 TREE_USED (decl
) = 1;
2961 GFC_DECL_RESULT (decl
) = 1;
2962 TREE_ADDRESSABLE (decl
) = 1;
2964 layout_decl (decl
, 0);
2965 gfc_finish_decl_attrs (decl
, &sym
->attr
);
2968 gfc_add_decl_to_parent_function (decl
);
2970 gfc_add_decl_to_function (decl
);
2974 parent_fake_result_decl
= build_tree_list (NULL
, decl
);
2976 current_fake_result_decl
= build_tree_list (NULL
, decl
);
2982 /* Builds a function decl. The remaining parameters are the types of the
2983 function arguments. Negative nargs indicates a varargs function. */
2986 build_library_function_decl_1 (tree name
, const char *spec
,
2987 tree rettype
, int nargs
, va_list p
)
2989 vec
<tree
, va_gc
> *arglist
;
2994 /* Library functions must be declared with global scope. */
2995 gcc_assert (current_function_decl
== NULL_TREE
);
2997 /* Create a list of the argument types. */
2998 vec_alloc (arglist
, abs (nargs
));
2999 for (n
= abs (nargs
); n
> 0; n
--)
3001 tree argtype
= va_arg (p
, tree
);
3002 arglist
->quick_push (argtype
);
3005 /* Build the function type and decl. */
3007 fntype
= build_function_type_vec (rettype
, arglist
);
3009 fntype
= build_varargs_function_type_vec (rettype
, arglist
);
3012 tree attr_args
= build_tree_list (NULL_TREE
,
3013 build_string (strlen (spec
), spec
));
3014 tree attrs
= tree_cons (get_identifier ("fn spec"),
3015 attr_args
, TYPE_ATTRIBUTES (fntype
));
3016 fntype
= build_type_attribute_variant (fntype
, attrs
);
3018 fndecl
= build_decl (input_location
,
3019 FUNCTION_DECL
, name
, fntype
);
3021 /* Mark this decl as external. */
3022 DECL_EXTERNAL (fndecl
) = 1;
3023 TREE_PUBLIC (fndecl
) = 1;
3027 rest_of_decl_compilation (fndecl
, 1, 0);
3032 /* Builds a function decl. The remaining parameters are the types of the
3033 function arguments. Negative nargs indicates a varargs function. */
3036 gfc_build_library_function_decl (tree name
, tree rettype
, int nargs
, ...)
3040 va_start (args
, nargs
);
3041 ret
= build_library_function_decl_1 (name
, NULL
, rettype
, nargs
, args
);
3046 /* Builds a function decl. The remaining parameters are the types of the
3047 function arguments. Negative nargs indicates a varargs function.
3048 The SPEC parameter specifies the function argument and return type
3049 specification according to the fnspec function type attribute. */
3052 gfc_build_library_function_decl_with_spec (tree name
, const char *spec
,
3053 tree rettype
, int nargs
, ...)
3057 va_start (args
, nargs
);
3058 ret
= build_library_function_decl_1 (name
, spec
, rettype
, nargs
, args
);
3064 gfc_build_intrinsic_function_decls (void)
3066 tree gfc_int4_type_node
= gfc_get_int_type (4);
3067 tree gfc_pint4_type_node
= build_pointer_type (gfc_int4_type_node
);
3068 tree gfc_int8_type_node
= gfc_get_int_type (8);
3069 tree gfc_pint8_type_node
= build_pointer_type (gfc_int8_type_node
);
3070 tree gfc_int16_type_node
= gfc_get_int_type (16);
3071 tree gfc_logical4_type_node
= gfc_get_logical_type (4);
3072 tree pchar1_type_node
= gfc_get_pchar_type (1);
3073 tree pchar4_type_node
= gfc_get_pchar_type (4);
3075 /* String functions. */
3076 gfor_fndecl_compare_string
= gfc_build_library_function_decl_with_spec (
3077 get_identifier (PREFIX("compare_string")), "..R.R",
3078 integer_type_node
, 4, gfc_charlen_type_node
, pchar1_type_node
,
3079 gfc_charlen_type_node
, pchar1_type_node
);
3080 DECL_PURE_P (gfor_fndecl_compare_string
) = 1;
3081 TREE_NOTHROW (gfor_fndecl_compare_string
) = 1;
3083 gfor_fndecl_concat_string
= gfc_build_library_function_decl_with_spec (
3084 get_identifier (PREFIX("concat_string")), "..W.R.R",
3085 void_type_node
, 6, gfc_charlen_type_node
, pchar1_type_node
,
3086 gfc_charlen_type_node
, pchar1_type_node
,
3087 gfc_charlen_type_node
, pchar1_type_node
);
3088 TREE_NOTHROW (gfor_fndecl_concat_string
) = 1;
3090 gfor_fndecl_string_len_trim
= gfc_build_library_function_decl_with_spec (
3091 get_identifier (PREFIX("string_len_trim")), "..R",
3092 gfc_charlen_type_node
, 2, gfc_charlen_type_node
, pchar1_type_node
);
3093 DECL_PURE_P (gfor_fndecl_string_len_trim
) = 1;
3094 TREE_NOTHROW (gfor_fndecl_string_len_trim
) = 1;
3096 gfor_fndecl_string_index
= gfc_build_library_function_decl_with_spec (
3097 get_identifier (PREFIX("string_index")), "..R.R.",
3098 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
3099 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
3100 DECL_PURE_P (gfor_fndecl_string_index
) = 1;
3101 TREE_NOTHROW (gfor_fndecl_string_index
) = 1;
3103 gfor_fndecl_string_scan
= gfc_build_library_function_decl_with_spec (
3104 get_identifier (PREFIX("string_scan")), "..R.R.",
3105 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
3106 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
3107 DECL_PURE_P (gfor_fndecl_string_scan
) = 1;
3108 TREE_NOTHROW (gfor_fndecl_string_scan
) = 1;
3110 gfor_fndecl_string_verify
= gfc_build_library_function_decl_with_spec (
3111 get_identifier (PREFIX("string_verify")), "..R.R.",
3112 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
3113 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
3114 DECL_PURE_P (gfor_fndecl_string_verify
) = 1;
3115 TREE_NOTHROW (gfor_fndecl_string_verify
) = 1;
3117 gfor_fndecl_string_trim
= gfc_build_library_function_decl_with_spec (
3118 get_identifier (PREFIX("string_trim")), ".Ww.R",
3119 void_type_node
, 4, build_pointer_type (gfc_charlen_type_node
),
3120 build_pointer_type (pchar1_type_node
), gfc_charlen_type_node
,
3123 gfor_fndecl_string_minmax
= gfc_build_library_function_decl_with_spec (
3124 get_identifier (PREFIX("string_minmax")), ".Ww.R",
3125 void_type_node
, -4, build_pointer_type (gfc_charlen_type_node
),
3126 build_pointer_type (pchar1_type_node
), integer_type_node
,
3129 gfor_fndecl_adjustl
= gfc_build_library_function_decl_with_spec (
3130 get_identifier (PREFIX("adjustl")), ".W.R",
3131 void_type_node
, 3, pchar1_type_node
, gfc_charlen_type_node
,
3133 TREE_NOTHROW (gfor_fndecl_adjustl
) = 1;
3135 gfor_fndecl_adjustr
= gfc_build_library_function_decl_with_spec (
3136 get_identifier (PREFIX("adjustr")), ".W.R",
3137 void_type_node
, 3, pchar1_type_node
, gfc_charlen_type_node
,
3139 TREE_NOTHROW (gfor_fndecl_adjustr
) = 1;
3141 gfor_fndecl_select_string
= gfc_build_library_function_decl_with_spec (
3142 get_identifier (PREFIX("select_string")), ".R.R.",
3143 integer_type_node
, 4, pvoid_type_node
, integer_type_node
,
3144 pchar1_type_node
, gfc_charlen_type_node
);
3145 DECL_PURE_P (gfor_fndecl_select_string
) = 1;
3146 TREE_NOTHROW (gfor_fndecl_select_string
) = 1;
3148 gfor_fndecl_compare_string_char4
= gfc_build_library_function_decl_with_spec (
3149 get_identifier (PREFIX("compare_string_char4")), "..R.R",
3150 integer_type_node
, 4, gfc_charlen_type_node
, pchar4_type_node
,
3151 gfc_charlen_type_node
, pchar4_type_node
);
3152 DECL_PURE_P (gfor_fndecl_compare_string_char4
) = 1;
3153 TREE_NOTHROW (gfor_fndecl_compare_string_char4
) = 1;
3155 gfor_fndecl_concat_string_char4
= gfc_build_library_function_decl_with_spec (
3156 get_identifier (PREFIX("concat_string_char4")), "..W.R.R",
3157 void_type_node
, 6, gfc_charlen_type_node
, pchar4_type_node
,
3158 gfc_charlen_type_node
, pchar4_type_node
, gfc_charlen_type_node
,
3160 TREE_NOTHROW (gfor_fndecl_concat_string_char4
) = 1;
3162 gfor_fndecl_string_len_trim_char4
= gfc_build_library_function_decl_with_spec (
3163 get_identifier (PREFIX("string_len_trim_char4")), "..R",
3164 gfc_charlen_type_node
, 2, gfc_charlen_type_node
, pchar4_type_node
);
3165 DECL_PURE_P (gfor_fndecl_string_len_trim_char4
) = 1;
3166 TREE_NOTHROW (gfor_fndecl_string_len_trim_char4
) = 1;
3168 gfor_fndecl_string_index_char4
= gfc_build_library_function_decl_with_spec (
3169 get_identifier (PREFIX("string_index_char4")), "..R.R.",
3170 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
3171 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
3172 DECL_PURE_P (gfor_fndecl_string_index_char4
) = 1;
3173 TREE_NOTHROW (gfor_fndecl_string_index_char4
) = 1;
3175 gfor_fndecl_string_scan_char4
= gfc_build_library_function_decl_with_spec (
3176 get_identifier (PREFIX("string_scan_char4")), "..R.R.",
3177 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
3178 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
3179 DECL_PURE_P (gfor_fndecl_string_scan_char4
) = 1;
3180 TREE_NOTHROW (gfor_fndecl_string_scan_char4
) = 1;
3182 gfor_fndecl_string_verify_char4
= gfc_build_library_function_decl_with_spec (
3183 get_identifier (PREFIX("string_verify_char4")), "..R.R.",
3184 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
3185 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
3186 DECL_PURE_P (gfor_fndecl_string_verify_char4
) = 1;
3187 TREE_NOTHROW (gfor_fndecl_string_verify_char4
) = 1;
3189 gfor_fndecl_string_trim_char4
= gfc_build_library_function_decl_with_spec (
3190 get_identifier (PREFIX("string_trim_char4")), ".Ww.R",
3191 void_type_node
, 4, build_pointer_type (gfc_charlen_type_node
),
3192 build_pointer_type (pchar4_type_node
), gfc_charlen_type_node
,
3195 gfor_fndecl_string_minmax_char4
= gfc_build_library_function_decl_with_spec (
3196 get_identifier (PREFIX("string_minmax_char4")), ".Ww.R",
3197 void_type_node
, -4, build_pointer_type (gfc_charlen_type_node
),
3198 build_pointer_type (pchar4_type_node
), integer_type_node
,
3201 gfor_fndecl_adjustl_char4
= gfc_build_library_function_decl_with_spec (
3202 get_identifier (PREFIX("adjustl_char4")), ".W.R",
3203 void_type_node
, 3, pchar4_type_node
, gfc_charlen_type_node
,
3205 TREE_NOTHROW (gfor_fndecl_adjustl_char4
) = 1;
3207 gfor_fndecl_adjustr_char4
= gfc_build_library_function_decl_with_spec (
3208 get_identifier (PREFIX("adjustr_char4")), ".W.R",
3209 void_type_node
, 3, pchar4_type_node
, gfc_charlen_type_node
,
3211 TREE_NOTHROW (gfor_fndecl_adjustr_char4
) = 1;
3213 gfor_fndecl_select_string_char4
= gfc_build_library_function_decl_with_spec (
3214 get_identifier (PREFIX("select_string_char4")), ".R.R.",
3215 integer_type_node
, 4, pvoid_type_node
, integer_type_node
,
3216 pvoid_type_node
, gfc_charlen_type_node
);
3217 DECL_PURE_P (gfor_fndecl_select_string_char4
) = 1;
3218 TREE_NOTHROW (gfor_fndecl_select_string_char4
) = 1;
3221 /* Conversion between character kinds. */
3223 gfor_fndecl_convert_char1_to_char4
= gfc_build_library_function_decl_with_spec (
3224 get_identifier (PREFIX("convert_char1_to_char4")), ".w.R",
3225 void_type_node
, 3, build_pointer_type (pchar4_type_node
),
3226 gfc_charlen_type_node
, pchar1_type_node
);
3228 gfor_fndecl_convert_char4_to_char1
= gfc_build_library_function_decl_with_spec (
3229 get_identifier (PREFIX("convert_char4_to_char1")), ".w.R",
3230 void_type_node
, 3, build_pointer_type (pchar1_type_node
),
3231 gfc_charlen_type_node
, pchar4_type_node
);
3233 /* Misc. functions. */
3235 gfor_fndecl_ttynam
= gfc_build_library_function_decl_with_spec (
3236 get_identifier (PREFIX("ttynam")), ".W",
3237 void_type_node
, 3, pchar_type_node
, gfc_charlen_type_node
,
3240 gfor_fndecl_fdate
= gfc_build_library_function_decl_with_spec (
3241 get_identifier (PREFIX("fdate")), ".W",
3242 void_type_node
, 2, pchar_type_node
, gfc_charlen_type_node
);
3244 gfor_fndecl_ctime
= gfc_build_library_function_decl_with_spec (
3245 get_identifier (PREFIX("ctime")), ".W",
3246 void_type_node
, 3, pchar_type_node
, gfc_charlen_type_node
,
3247 gfc_int8_type_node
);
3249 gfor_fndecl_sc_kind
= gfc_build_library_function_decl_with_spec (
3250 get_identifier (PREFIX("selected_char_kind")), "..R",
3251 gfc_int4_type_node
, 2, gfc_charlen_type_node
, pchar_type_node
);
3252 DECL_PURE_P (gfor_fndecl_sc_kind
) = 1;
3253 TREE_NOTHROW (gfor_fndecl_sc_kind
) = 1;
3255 gfor_fndecl_si_kind
= gfc_build_library_function_decl_with_spec (
3256 get_identifier (PREFIX("selected_int_kind")), ".R",
3257 gfc_int4_type_node
, 1, pvoid_type_node
);
3258 DECL_PURE_P (gfor_fndecl_si_kind
) = 1;
3259 TREE_NOTHROW (gfor_fndecl_si_kind
) = 1;
3261 gfor_fndecl_sr_kind
= gfc_build_library_function_decl_with_spec (
3262 get_identifier (PREFIX("selected_real_kind2008")), ".RR",
3263 gfc_int4_type_node
, 3, pvoid_type_node
, pvoid_type_node
,
3265 DECL_PURE_P (gfor_fndecl_sr_kind
) = 1;
3266 TREE_NOTHROW (gfor_fndecl_sr_kind
) = 1;
3268 gfor_fndecl_system_clock4
= gfc_build_library_function_decl (
3269 get_identifier (PREFIX("system_clock_4")),
3270 void_type_node
, 3, gfc_pint4_type_node
, gfc_pint4_type_node
,
3271 gfc_pint4_type_node
);
3273 gfor_fndecl_system_clock8
= gfc_build_library_function_decl (
3274 get_identifier (PREFIX("system_clock_8")),
3275 void_type_node
, 3, gfc_pint8_type_node
, gfc_pint8_type_node
,
3276 gfc_pint8_type_node
);
3278 /* Power functions. */
3280 tree ctype
, rtype
, itype
, jtype
;
3281 int rkind
, ikind
, jkind
;
3284 static int ikinds
[NIKINDS
] = {4, 8, 16};
3285 static int rkinds
[NRKINDS
] = {4, 8, 10, 16};
3286 char name
[PREFIX_LEN
+ 12]; /* _gfortran_pow_?n_?n */
3288 for (ikind
=0; ikind
< NIKINDS
; ikind
++)
3290 itype
= gfc_get_int_type (ikinds
[ikind
]);
3292 for (jkind
=0; jkind
< NIKINDS
; jkind
++)
3294 jtype
= gfc_get_int_type (ikinds
[jkind
]);
3297 sprintf(name
, PREFIX("pow_i%d_i%d"), ikinds
[ikind
],
3299 gfor_fndecl_math_powi
[jkind
][ikind
].integer
=
3300 gfc_build_library_function_decl (get_identifier (name
),
3301 jtype
, 2, jtype
, itype
);
3302 TREE_READONLY (gfor_fndecl_math_powi
[jkind
][ikind
].integer
) = 1;
3303 TREE_NOTHROW (gfor_fndecl_math_powi
[jkind
][ikind
].integer
) = 1;
3307 for (rkind
= 0; rkind
< NRKINDS
; rkind
++)
3309 rtype
= gfc_get_real_type (rkinds
[rkind
]);
3312 sprintf(name
, PREFIX("pow_r%d_i%d"), rkinds
[rkind
],
3314 gfor_fndecl_math_powi
[rkind
][ikind
].real
=
3315 gfc_build_library_function_decl (get_identifier (name
),
3316 rtype
, 2, rtype
, itype
);
3317 TREE_READONLY (gfor_fndecl_math_powi
[rkind
][ikind
].real
) = 1;
3318 TREE_NOTHROW (gfor_fndecl_math_powi
[rkind
][ikind
].real
) = 1;
3321 ctype
= gfc_get_complex_type (rkinds
[rkind
]);
3324 sprintf(name
, PREFIX("pow_c%d_i%d"), rkinds
[rkind
],
3326 gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
=
3327 gfc_build_library_function_decl (get_identifier (name
),
3328 ctype
, 2,ctype
, itype
);
3329 TREE_READONLY (gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
) = 1;
3330 TREE_NOTHROW (gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
) = 1;
3338 gfor_fndecl_math_ishftc4
= gfc_build_library_function_decl (
3339 get_identifier (PREFIX("ishftc4")),
3340 gfc_int4_type_node
, 3, gfc_int4_type_node
, gfc_int4_type_node
,
3341 gfc_int4_type_node
);
3342 TREE_READONLY (gfor_fndecl_math_ishftc4
) = 1;
3343 TREE_NOTHROW (gfor_fndecl_math_ishftc4
) = 1;
3345 gfor_fndecl_math_ishftc8
= gfc_build_library_function_decl (
3346 get_identifier (PREFIX("ishftc8")),
3347 gfc_int8_type_node
, 3, gfc_int8_type_node
, gfc_int4_type_node
,
3348 gfc_int4_type_node
);
3349 TREE_READONLY (gfor_fndecl_math_ishftc8
) = 1;
3350 TREE_NOTHROW (gfor_fndecl_math_ishftc8
) = 1;
3352 if (gfc_int16_type_node
)
3354 gfor_fndecl_math_ishftc16
= gfc_build_library_function_decl (
3355 get_identifier (PREFIX("ishftc16")),
3356 gfc_int16_type_node
, 3, gfc_int16_type_node
, gfc_int4_type_node
,
3357 gfc_int4_type_node
);
3358 TREE_READONLY (gfor_fndecl_math_ishftc16
) = 1;
3359 TREE_NOTHROW (gfor_fndecl_math_ishftc16
) = 1;
3362 /* BLAS functions. */
3364 tree pint
= build_pointer_type (integer_type_node
);
3365 tree ps
= build_pointer_type (gfc_get_real_type (gfc_default_real_kind
));
3366 tree pd
= build_pointer_type (gfc_get_real_type (gfc_default_double_kind
));
3367 tree pc
= build_pointer_type (gfc_get_complex_type (gfc_default_real_kind
));
3368 tree pz
= build_pointer_type
3369 (gfc_get_complex_type (gfc_default_double_kind
));
3371 gfor_fndecl_sgemm
= gfc_build_library_function_decl
3373 (flag_underscoring
? "sgemm_" : "sgemm"),
3374 void_type_node
, 15, pchar_type_node
,
3375 pchar_type_node
, pint
, pint
, pint
, ps
, ps
, pint
,
3376 ps
, pint
, ps
, ps
, pint
, integer_type_node
,
3378 gfor_fndecl_dgemm
= gfc_build_library_function_decl
3380 (flag_underscoring
? "dgemm_" : "dgemm"),
3381 void_type_node
, 15, pchar_type_node
,
3382 pchar_type_node
, pint
, pint
, pint
, pd
, pd
, pint
,
3383 pd
, pint
, pd
, pd
, pint
, integer_type_node
,
3385 gfor_fndecl_cgemm
= gfc_build_library_function_decl
3387 (flag_underscoring
? "cgemm_" : "cgemm"),
3388 void_type_node
, 15, pchar_type_node
,
3389 pchar_type_node
, pint
, pint
, pint
, pc
, pc
, pint
,
3390 pc
, pint
, pc
, pc
, pint
, integer_type_node
,
3392 gfor_fndecl_zgemm
= gfc_build_library_function_decl
3394 (flag_underscoring
? "zgemm_" : "zgemm"),
3395 void_type_node
, 15, pchar_type_node
,
3396 pchar_type_node
, pint
, pint
, pint
, pz
, pz
, pint
,
3397 pz
, pint
, pz
, pz
, pint
, integer_type_node
,
3401 /* Other functions. */
3402 gfor_fndecl_size0
= gfc_build_library_function_decl_with_spec (
3403 get_identifier (PREFIX("size0")), ".R",
3404 gfc_array_index_type
, 1, pvoid_type_node
);
3405 DECL_PURE_P (gfor_fndecl_size0
) = 1;
3406 TREE_NOTHROW (gfor_fndecl_size0
) = 1;
3408 gfor_fndecl_size1
= gfc_build_library_function_decl_with_spec (
3409 get_identifier (PREFIX("size1")), ".R",
3410 gfc_array_index_type
, 2, pvoid_type_node
, gfc_array_index_type
);
3411 DECL_PURE_P (gfor_fndecl_size1
) = 1;
3412 TREE_NOTHROW (gfor_fndecl_size1
) = 1;
3414 gfor_fndecl_iargc
= gfc_build_library_function_decl (
3415 get_identifier (PREFIX ("iargc")), gfc_int4_type_node
, 0);
3416 TREE_NOTHROW (gfor_fndecl_iargc
) = 1;
3420 /* Make prototypes for runtime library functions. */
3423 gfc_build_builtin_function_decls (void)
3425 tree gfc_int4_type_node
= gfc_get_int_type (4);
3427 gfor_fndecl_stop_numeric
= gfc_build_library_function_decl (
3428 get_identifier (PREFIX("stop_numeric")),
3429 void_type_node
, 1, gfc_int4_type_node
);
3430 /* STOP doesn't return. */
3431 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric
) = 1;
3433 gfor_fndecl_stop_numeric_f08
= gfc_build_library_function_decl (
3434 get_identifier (PREFIX("stop_numeric_f08")),
3435 void_type_node
, 1, gfc_int4_type_node
);
3436 /* STOP doesn't return. */
3437 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric_f08
) = 1;
3439 gfor_fndecl_stop_string
= gfc_build_library_function_decl_with_spec (
3440 get_identifier (PREFIX("stop_string")), ".R.",
3441 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
3442 /* STOP doesn't return. */
3443 TREE_THIS_VOLATILE (gfor_fndecl_stop_string
) = 1;
3445 gfor_fndecl_error_stop_numeric
= gfc_build_library_function_decl (
3446 get_identifier (PREFIX("error_stop_numeric")),
3447 void_type_node
, 1, gfc_int4_type_node
);
3448 /* ERROR STOP doesn't return. */
3449 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_numeric
) = 1;
3451 gfor_fndecl_error_stop_string
= gfc_build_library_function_decl_with_spec (
3452 get_identifier (PREFIX("error_stop_string")), ".R.",
3453 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
3454 /* ERROR STOP doesn't return. */
3455 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_string
) = 1;
3457 gfor_fndecl_pause_numeric
= gfc_build_library_function_decl (
3458 get_identifier (PREFIX("pause_numeric")),
3459 void_type_node
, 1, gfc_int4_type_node
);
3461 gfor_fndecl_pause_string
= gfc_build_library_function_decl_with_spec (
3462 get_identifier (PREFIX("pause_string")), ".R.",
3463 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
3465 gfor_fndecl_runtime_error
= gfc_build_library_function_decl_with_spec (
3466 get_identifier (PREFIX("runtime_error")), ".R",
3467 void_type_node
, -1, pchar_type_node
);
3468 /* The runtime_error function does not return. */
3469 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error
) = 1;
3471 gfor_fndecl_runtime_error_at
= gfc_build_library_function_decl_with_spec (
3472 get_identifier (PREFIX("runtime_error_at")), ".RR",
3473 void_type_node
, -2, pchar_type_node
, pchar_type_node
);
3474 /* The runtime_error_at function does not return. */
3475 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at
) = 1;
3477 gfor_fndecl_runtime_warning_at
= gfc_build_library_function_decl_with_spec (
3478 get_identifier (PREFIX("runtime_warning_at")), ".RR",
3479 void_type_node
, -2, pchar_type_node
, pchar_type_node
);
3481 gfor_fndecl_generate_error
= gfc_build_library_function_decl_with_spec (
3482 get_identifier (PREFIX("generate_error")), ".R.R",
3483 void_type_node
, 3, pvoid_type_node
, integer_type_node
,
3486 gfor_fndecl_os_error
= gfc_build_library_function_decl_with_spec (
3487 get_identifier (PREFIX("os_error")), ".R",
3488 void_type_node
, 1, pchar_type_node
);
3489 /* The runtime_error function does not return. */
3490 TREE_THIS_VOLATILE (gfor_fndecl_os_error
) = 1;
3492 gfor_fndecl_set_args
= gfc_build_library_function_decl (
3493 get_identifier (PREFIX("set_args")),
3494 void_type_node
, 2, integer_type_node
,
3495 build_pointer_type (pchar_type_node
));
3497 gfor_fndecl_set_fpe
= gfc_build_library_function_decl (
3498 get_identifier (PREFIX("set_fpe")),
3499 void_type_node
, 1, integer_type_node
);
3501 gfor_fndecl_ieee_procedure_entry
= gfc_build_library_function_decl (
3502 get_identifier (PREFIX("ieee_procedure_entry")),
3503 void_type_node
, 1, pvoid_type_node
);
3505 gfor_fndecl_ieee_procedure_exit
= gfc_build_library_function_decl (
3506 get_identifier (PREFIX("ieee_procedure_exit")),
3507 void_type_node
, 1, pvoid_type_node
);
3509 /* Keep the array dimension in sync with the call, later in this file. */
3510 gfor_fndecl_set_options
= gfc_build_library_function_decl_with_spec (
3511 get_identifier (PREFIX("set_options")), "..R",
3512 void_type_node
, 2, integer_type_node
,
3513 build_pointer_type (integer_type_node
));
3515 gfor_fndecl_set_convert
= gfc_build_library_function_decl (
3516 get_identifier (PREFIX("set_convert")),
3517 void_type_node
, 1, integer_type_node
);
3519 gfor_fndecl_set_record_marker
= gfc_build_library_function_decl (
3520 get_identifier (PREFIX("set_record_marker")),
3521 void_type_node
, 1, integer_type_node
);
3523 gfor_fndecl_set_max_subrecord_length
= gfc_build_library_function_decl (
3524 get_identifier (PREFIX("set_max_subrecord_length")),
3525 void_type_node
, 1, integer_type_node
);
3527 gfor_fndecl_in_pack
= gfc_build_library_function_decl_with_spec (
3528 get_identifier (PREFIX("internal_pack")), ".r",
3529 pvoid_type_node
, 1, pvoid_type_node
);
3531 gfor_fndecl_in_unpack
= gfc_build_library_function_decl_with_spec (
3532 get_identifier (PREFIX("internal_unpack")), ".wR",
3533 void_type_node
, 2, pvoid_type_node
, pvoid_type_node
);
3535 gfor_fndecl_associated
= gfc_build_library_function_decl_with_spec (
3536 get_identifier (PREFIX("associated")), ".RR",
3537 integer_type_node
, 2, ppvoid_type_node
, ppvoid_type_node
);
3538 DECL_PURE_P (gfor_fndecl_associated
) = 1;
3539 TREE_NOTHROW (gfor_fndecl_associated
) = 1;
3541 /* Coarray library calls. */
3542 if (flag_coarray
== GFC_FCOARRAY_LIB
)
3544 tree pint_type
, pppchar_type
;
3546 pint_type
= build_pointer_type (integer_type_node
);
3548 = build_pointer_type (build_pointer_type (pchar_type_node
));
3550 gfor_fndecl_caf_init
= gfc_build_library_function_decl (
3551 get_identifier (PREFIX("caf_init")), void_type_node
,
3552 2, pint_type
, pppchar_type
);
3554 gfor_fndecl_caf_finalize
= gfc_build_library_function_decl (
3555 get_identifier (PREFIX("caf_finalize")), void_type_node
, 0);
3557 gfor_fndecl_caf_this_image
= gfc_build_library_function_decl (
3558 get_identifier (PREFIX("caf_this_image")), integer_type_node
,
3559 1, integer_type_node
);
3561 gfor_fndecl_caf_num_images
= gfc_build_library_function_decl (
3562 get_identifier (PREFIX("caf_num_images")), integer_type_node
,
3563 2, integer_type_node
, integer_type_node
);
3565 gfor_fndecl_caf_register
= gfc_build_library_function_decl_with_spec (
3566 get_identifier (PREFIX("caf_register")), "RRWWWWR", void_type_node
, 7,
3567 size_type_node
, integer_type_node
, ppvoid_type_node
, pvoid_type_node
,
3568 pint_type
, pchar_type_node
, integer_type_node
);
3570 gfor_fndecl_caf_deregister
= gfc_build_library_function_decl_with_spec (
3571 get_identifier (PREFIX("caf_deregister")), "WWWR", void_type_node
, 4,
3572 ppvoid_type_node
, pint_type
, pchar_type_node
, integer_type_node
);
3574 gfor_fndecl_caf_get
= gfc_build_library_function_decl_with_spec (
3575 get_identifier (PREFIX("caf_get")), ".R.RRWRRRW", void_type_node
, 10,
3576 pvoid_type_node
, size_type_node
, integer_type_node
, pvoid_type_node
,
3577 pvoid_type_node
, pvoid_type_node
, integer_type_node
, integer_type_node
,
3578 boolean_type_node
, pint_type
);
3580 gfor_fndecl_caf_send
= gfc_build_library_function_decl_with_spec (
3581 get_identifier (PREFIX("caf_send")), ".R.RRRRRRW", void_type_node
, 10,
3582 pvoid_type_node
, size_type_node
, integer_type_node
, pvoid_type_node
,
3583 pvoid_type_node
, pvoid_type_node
, integer_type_node
, integer_type_node
,
3584 boolean_type_node
, pint_type
);
3586 gfor_fndecl_caf_sendget
= gfc_build_library_function_decl_with_spec (
3587 get_identifier (PREFIX("caf_sendget")), ".R.RRRR.RRRRRR",
3588 void_type_node
, 14, pvoid_type_node
, size_type_node
, integer_type_node
,
3589 pvoid_type_node
, pvoid_type_node
, pvoid_type_node
, size_type_node
,
3590 integer_type_node
, pvoid_type_node
, pvoid_type_node
, integer_type_node
,
3591 integer_type_node
, boolean_type_node
, integer_type_node
);
3593 gfor_fndecl_caf_get_by_ref
= gfc_build_library_function_decl_with_spec (
3594 get_identifier (PREFIX("caf_get_by_ref")), ".RWRRRRRW", void_type_node
,
3595 9, pvoid_type_node
, integer_type_node
, pvoid_type_node
, pvoid_type_node
,
3596 integer_type_node
, integer_type_node
, boolean_type_node
,
3597 boolean_type_node
, pint_type
);
3599 gfor_fndecl_caf_send_by_ref
= gfc_build_library_function_decl_with_spec (
3600 get_identifier (PREFIX("caf_send_by_ref")), ".RRRRRRRW", void_type_node
,
3601 9, pvoid_type_node
, integer_type_node
, pvoid_type_node
, pvoid_type_node
,
3602 integer_type_node
, integer_type_node
, boolean_type_node
,
3603 boolean_type_node
, pint_type
);
3605 gfor_fndecl_caf_sendget_by_ref
3606 = gfc_build_library_function_decl_with_spec (
3607 get_identifier (PREFIX("caf_sendget_by_ref")), ".RR.RRRRRWW",
3608 void_type_node
, 11, pvoid_type_node
, integer_type_node
,
3609 pvoid_type_node
, pvoid_type_node
, integer_type_node
,
3610 pvoid_type_node
, integer_type_node
, integer_type_node
,
3611 boolean_type_node
, pint_type
, pint_type
);
3613 gfor_fndecl_caf_sync_all
= gfc_build_library_function_decl_with_spec (
3614 get_identifier (PREFIX("caf_sync_all")), ".WW", void_type_node
,
3615 3, pint_type
, pchar_type_node
, integer_type_node
);
3617 gfor_fndecl_caf_sync_memory
= gfc_build_library_function_decl_with_spec (
3618 get_identifier (PREFIX("caf_sync_memory")), ".WW", void_type_node
,
3619 3, pint_type
, pchar_type_node
, integer_type_node
);
3621 gfor_fndecl_caf_sync_images
= gfc_build_library_function_decl_with_spec (
3622 get_identifier (PREFIX("caf_sync_images")), ".RRWW", void_type_node
,
3623 5, integer_type_node
, pint_type
, pint_type
,
3624 pchar_type_node
, integer_type_node
);
3626 gfor_fndecl_caf_error_stop
= gfc_build_library_function_decl (
3627 get_identifier (PREFIX("caf_error_stop")),
3628 void_type_node
, 1, gfc_int4_type_node
);
3629 /* CAF's ERROR STOP doesn't return. */
3630 TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop
) = 1;
3632 gfor_fndecl_caf_error_stop_str
= gfc_build_library_function_decl_with_spec (
3633 get_identifier (PREFIX("caf_error_stop_str")), ".R.",
3634 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
3635 /* CAF's ERROR STOP doesn't return. */
3636 TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop_str
) = 1;
3638 gfor_fndecl_caf_stop_numeric
= gfc_build_library_function_decl_with_spec (
3639 get_identifier (PREFIX("caf_stop_numeric")), ".R.",
3640 void_type_node
, 1, gfc_int4_type_node
);
3641 /* CAF's STOP doesn't return. */
3642 TREE_THIS_VOLATILE (gfor_fndecl_caf_stop_numeric
) = 1;
3644 gfor_fndecl_caf_stop_str
= gfc_build_library_function_decl_with_spec (
3645 get_identifier (PREFIX("caf_stop_str")), ".R.",
3646 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
3647 /* CAF's STOP doesn't return. */
3648 TREE_THIS_VOLATILE (gfor_fndecl_caf_stop_str
) = 1;
3650 gfor_fndecl_caf_atomic_def
= gfc_build_library_function_decl_with_spec (
3651 get_identifier (PREFIX("caf_atomic_define")), "R..RW",
3652 void_type_node
, 7, pvoid_type_node
, size_type_node
, integer_type_node
,
3653 pvoid_type_node
, pint_type
, integer_type_node
, integer_type_node
);
3655 gfor_fndecl_caf_atomic_ref
= gfc_build_library_function_decl_with_spec (
3656 get_identifier (PREFIX("caf_atomic_ref")), "R..WW",
3657 void_type_node
, 7, pvoid_type_node
, size_type_node
, integer_type_node
,
3658 pvoid_type_node
, pint_type
, integer_type_node
, integer_type_node
);
3660 gfor_fndecl_caf_atomic_cas
= gfc_build_library_function_decl_with_spec (
3661 get_identifier (PREFIX("caf_atomic_cas")), "R..WRRW",
3662 void_type_node
, 9, pvoid_type_node
, size_type_node
, integer_type_node
,
3663 pvoid_type_node
, pvoid_type_node
, pvoid_type_node
, pint_type
,
3664 integer_type_node
, integer_type_node
);
3666 gfor_fndecl_caf_atomic_op
= gfc_build_library_function_decl_with_spec (
3667 get_identifier (PREFIX("caf_atomic_op")), ".R..RWW",
3668 void_type_node
, 9, integer_type_node
, pvoid_type_node
, size_type_node
,
3669 integer_type_node
, pvoid_type_node
, pvoid_type_node
, pint_type
,
3670 integer_type_node
, integer_type_node
);
3672 gfor_fndecl_caf_lock
= gfc_build_library_function_decl_with_spec (
3673 get_identifier (PREFIX("caf_lock")), "R..WWW",
3674 void_type_node
, 7, pvoid_type_node
, size_type_node
, integer_type_node
,
3675 pint_type
, pint_type
, pchar_type_node
, integer_type_node
);
3677 gfor_fndecl_caf_unlock
= gfc_build_library_function_decl_with_spec (
3678 get_identifier (PREFIX("caf_unlock")), "R..WW",
3679 void_type_node
, 6, pvoid_type_node
, size_type_node
, integer_type_node
,
3680 pint_type
, pchar_type_node
, integer_type_node
);
3682 gfor_fndecl_caf_event_post
= gfc_build_library_function_decl_with_spec (
3683 get_identifier (PREFIX("caf_event_post")), "R..WW",
3684 void_type_node
, 6, pvoid_type_node
, size_type_node
, integer_type_node
,
3685 pint_type
, pchar_type_node
, integer_type_node
);
3687 gfor_fndecl_caf_event_wait
= gfc_build_library_function_decl_with_spec (
3688 get_identifier (PREFIX("caf_event_wait")), "R..WW",
3689 void_type_node
, 6, pvoid_type_node
, size_type_node
, integer_type_node
,
3690 pint_type
, pchar_type_node
, integer_type_node
);
3692 gfor_fndecl_caf_event_query
= gfc_build_library_function_decl_with_spec (
3693 get_identifier (PREFIX("caf_event_query")), "R..WW",
3694 void_type_node
, 5, pvoid_type_node
, size_type_node
, integer_type_node
,
3695 pint_type
, pint_type
);
3697 gfor_fndecl_co_broadcast
= gfc_build_library_function_decl_with_spec (
3698 get_identifier (PREFIX("caf_co_broadcast")), "W.WW",
3699 void_type_node
, 5, pvoid_type_node
, integer_type_node
,
3700 pint_type
, pchar_type_node
, integer_type_node
);
3702 gfor_fndecl_co_max
= gfc_build_library_function_decl_with_spec (
3703 get_identifier (PREFIX("caf_co_max")), "W.WW",
3704 void_type_node
, 6, pvoid_type_node
, integer_type_node
,
3705 pint_type
, pchar_type_node
, integer_type_node
, integer_type_node
);
3707 gfor_fndecl_co_min
= gfc_build_library_function_decl_with_spec (
3708 get_identifier (PREFIX("caf_co_min")), "W.WW",
3709 void_type_node
, 6, pvoid_type_node
, integer_type_node
,
3710 pint_type
, pchar_type_node
, integer_type_node
, integer_type_node
);
3712 gfor_fndecl_co_reduce
= gfc_build_library_function_decl_with_spec (
3713 get_identifier (PREFIX("caf_co_reduce")), "W.R.WW",
3714 void_type_node
, 8, pvoid_type_node
,
3715 build_pointer_type (build_varargs_function_type_list (void_type_node
,
3717 integer_type_node
, integer_type_node
, pint_type
, pchar_type_node
,
3718 integer_type_node
, integer_type_node
);
3720 gfor_fndecl_co_sum
= gfc_build_library_function_decl_with_spec (
3721 get_identifier (PREFIX("caf_co_sum")), "W.WW",
3722 void_type_node
, 5, pvoid_type_node
, integer_type_node
,
3723 pint_type
, pchar_type_node
, integer_type_node
);
3726 gfc_build_intrinsic_function_decls ();
3727 gfc_build_intrinsic_lib_fndecls ();
3728 gfc_build_io_library_fndecls ();
3732 /* Evaluate the length of dummy character variables. */
3735 gfc_trans_dummy_character (gfc_symbol
*sym
, gfc_charlen
*cl
,
3736 gfc_wrapped_block
*block
)
3740 gfc_finish_decl (cl
->backend_decl
);
3742 gfc_start_block (&init
);
3744 /* Evaluate the string length expression. */
3745 gfc_conv_string_length (cl
, NULL
, &init
);
3747 gfc_trans_vla_type_sizes (sym
, &init
);
3749 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
3753 /* Allocate and cleanup an automatic character variable. */
3756 gfc_trans_auto_character_variable (gfc_symbol
* sym
, gfc_wrapped_block
* block
)
3762 gcc_assert (sym
->backend_decl
);
3763 gcc_assert (sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
);
3765 gfc_init_block (&init
);
3767 /* Evaluate the string length expression. */
3768 gfc_conv_string_length (sym
->ts
.u
.cl
, NULL
, &init
);
3770 gfc_trans_vla_type_sizes (sym
, &init
);
3772 decl
= sym
->backend_decl
;
3774 /* Emit a DECL_EXPR for this variable, which will cause the
3775 gimplifier to allocate storage, and all that good stuff. */
3776 tmp
= fold_build1_loc (input_location
, DECL_EXPR
, TREE_TYPE (decl
), decl
);
3777 gfc_add_expr_to_block (&init
, tmp
);
3779 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
3782 /* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
3785 gfc_trans_assign_aux_var (gfc_symbol
* sym
, gfc_wrapped_block
* block
)
3789 gcc_assert (sym
->backend_decl
);
3790 gfc_start_block (&init
);
3792 /* Set the initial value to length. See the comments in
3793 function gfc_add_assign_aux_vars in this file. */
3794 gfc_add_modify (&init
, GFC_DECL_STRING_LEN (sym
->backend_decl
),
3795 build_int_cst (gfc_charlen_type_node
, -2));
3797 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
3801 gfc_trans_vla_one_sizepos (tree
*tp
, stmtblock_t
*body
)
3803 tree t
= *tp
, var
, val
;
3805 if (t
== NULL
|| t
== error_mark_node
)
3807 if (TREE_CONSTANT (t
) || DECL_P (t
))
3810 if (TREE_CODE (t
) == SAVE_EXPR
)
3812 if (SAVE_EXPR_RESOLVED_P (t
))
3814 *tp
= TREE_OPERAND (t
, 0);
3817 val
= TREE_OPERAND (t
, 0);
3822 var
= gfc_create_var_np (TREE_TYPE (t
), NULL
);
3823 gfc_add_decl_to_function (var
);
3824 gfc_add_modify (body
, var
, unshare_expr (val
));
3825 if (TREE_CODE (t
) == SAVE_EXPR
)
3826 TREE_OPERAND (t
, 0) = var
;
3831 gfc_trans_vla_type_sizes_1 (tree type
, stmtblock_t
*body
)
3835 if (type
== NULL
|| type
== error_mark_node
)
3838 type
= TYPE_MAIN_VARIANT (type
);
3840 if (TREE_CODE (type
) == INTEGER_TYPE
)
3842 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type
), body
);
3843 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type
), body
);
3845 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
3847 TYPE_MIN_VALUE (t
) = TYPE_MIN_VALUE (type
);
3848 TYPE_MAX_VALUE (t
) = TYPE_MAX_VALUE (type
);
3851 else if (TREE_CODE (type
) == ARRAY_TYPE
)
3853 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type
), body
);
3854 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type
), body
);
3855 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type
), body
);
3856 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type
), body
);
3858 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
3860 TYPE_SIZE (t
) = TYPE_SIZE (type
);
3861 TYPE_SIZE_UNIT (t
) = TYPE_SIZE_UNIT (type
);
3866 /* Make sure all type sizes and array domains are either constant,
3867 or variable or parameter decls. This is a simplified variant
3868 of gimplify_type_sizes, but we can't use it here, as none of the
3869 variables in the expressions have been gimplified yet.
3870 As type sizes and domains for various variable length arrays
3871 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
3872 time, without this routine gimplify_type_sizes in the middle-end
3873 could result in the type sizes being gimplified earlier than where
3874 those variables are initialized. */
3877 gfc_trans_vla_type_sizes (gfc_symbol
*sym
, stmtblock_t
*body
)
3879 tree type
= TREE_TYPE (sym
->backend_decl
);
3881 if (TREE_CODE (type
) == FUNCTION_TYPE
3882 && (sym
->attr
.function
|| sym
->attr
.result
|| sym
->attr
.entry
))
3884 if (! current_fake_result_decl
)
3887 type
= TREE_TYPE (TREE_VALUE (current_fake_result_decl
));
3890 while (POINTER_TYPE_P (type
))
3891 type
= TREE_TYPE (type
);
3893 if (GFC_DESCRIPTOR_TYPE_P (type
))
3895 tree etype
= GFC_TYPE_ARRAY_DATAPTR_TYPE (type
);
3897 while (POINTER_TYPE_P (etype
))
3898 etype
= TREE_TYPE (etype
);
3900 gfc_trans_vla_type_sizes_1 (etype
, body
);
3903 gfc_trans_vla_type_sizes_1 (type
, body
);
3907 /* Initialize a derived type by building an lvalue from the symbol
3908 and using trans_assignment to do the work. Set dealloc to false
3909 if no deallocation prior the assignment is needed. */
3911 gfc_init_default_dt (gfc_symbol
* sym
, stmtblock_t
* block
, bool dealloc
)
3919 gcc_assert (!sym
->attr
.allocatable
);
3920 gfc_set_sym_referenced (sym
);
3921 e
= gfc_lval_expr_from_sym (sym
);
3922 tmp
= gfc_trans_assignment (e
, sym
->value
, false, dealloc
);
3923 if (sym
->attr
.dummy
&& (sym
->attr
.optional
3924 || sym
->ns
->proc_name
->attr
.entry_master
))
3926 present
= gfc_conv_expr_present (sym
);
3927 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
), present
,
3928 tmp
, build_empty_stmt (input_location
));
3930 gfc_add_expr_to_block (block
, tmp
);
3935 /* Initialize INTENT(OUT) derived type dummies. As well as giving
3936 them their default initializer, if they do not have allocatable
3937 components, they have their allocatable components deallocated. */
3940 init_intent_out_dt (gfc_symbol
* proc_sym
, gfc_wrapped_block
* block
)
3943 gfc_formal_arglist
*f
;
3947 gfc_init_block (&init
);
3948 for (f
= gfc_sym_get_dummy_args (proc_sym
); f
; f
= f
->next
)
3949 if (f
->sym
&& f
->sym
->attr
.intent
== INTENT_OUT
3950 && !f
->sym
->attr
.pointer
3951 && f
->sym
->ts
.type
== BT_DERIVED
)
3955 /* Note: Allocatables are excluded as they are already handled
3957 if (!f
->sym
->attr
.allocatable
3958 && gfc_is_finalizable (f
->sym
->ts
.u
.derived
, NULL
))
3963 gfc_init_block (&block
);
3964 f
->sym
->attr
.referenced
= 1;
3965 e
= gfc_lval_expr_from_sym (f
->sym
);
3966 gfc_add_finalizer_call (&block
, e
);
3968 tmp
= gfc_finish_block (&block
);
3971 if (tmp
== NULL_TREE
&& !f
->sym
->attr
.allocatable
3972 && f
->sym
->ts
.u
.derived
->attr
.alloc_comp
&& !f
->sym
->value
)
3973 tmp
= gfc_deallocate_alloc_comp (f
->sym
->ts
.u
.derived
,
3974 f
->sym
->backend_decl
,
3975 f
->sym
->as
? f
->sym
->as
->rank
: 0);
3977 if (tmp
!= NULL_TREE
&& (f
->sym
->attr
.optional
3978 || f
->sym
->ns
->proc_name
->attr
.entry_master
))
3980 present
= gfc_conv_expr_present (f
->sym
);
3981 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
),
3982 present
, tmp
, build_empty_stmt (input_location
));
3985 if (tmp
!= NULL_TREE
)
3986 gfc_add_expr_to_block (&init
, tmp
);
3987 else if (f
->sym
->value
&& !f
->sym
->attr
.allocatable
)
3988 gfc_init_default_dt (f
->sym
, &init
, true);
3990 else if (f
->sym
&& f
->sym
->attr
.intent
== INTENT_OUT
3991 && f
->sym
->ts
.type
== BT_CLASS
3992 && !CLASS_DATA (f
->sym
)->attr
.class_pointer
3993 && !CLASS_DATA (f
->sym
)->attr
.allocatable
)
3998 gfc_init_block (&block
);
3999 f
->sym
->attr
.referenced
= 1;
4000 e
= gfc_lval_expr_from_sym (f
->sym
);
4001 gfc_add_finalizer_call (&block
, e
);
4003 tmp
= gfc_finish_block (&block
);
4005 if (f
->sym
->attr
.optional
|| f
->sym
->ns
->proc_name
->attr
.entry_master
)
4007 present
= gfc_conv_expr_present (f
->sym
);
4008 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
),
4010 build_empty_stmt (input_location
));
4013 gfc_add_expr_to_block (&init
, tmp
);
4016 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
4020 /* Helper function to manage deferred string lengths. */
4023 gfc_null_and_pass_deferred_len (gfc_symbol
*sym
, stmtblock_t
*init
,
4028 /* Character length passed by reference. */
4029 tmp
= sym
->ts
.u
.cl
->passed_length
;
4030 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
4031 tmp
= fold_convert (gfc_charlen_type_node
, tmp
);
4033 if (!sym
->attr
.dummy
|| sym
->attr
.intent
== INTENT_OUT
)
4034 /* Zero the string length when entering the scope. */
4035 gfc_add_modify (init
, sym
->ts
.u
.cl
->backend_decl
,
4036 build_int_cst (gfc_charlen_type_node
, 0));
4041 tmp2
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4042 gfc_charlen_type_node
,
4043 sym
->ts
.u
.cl
->backend_decl
, tmp
);
4044 if (sym
->attr
.optional
)
4046 tree present
= gfc_conv_expr_present (sym
);
4047 tmp2
= build3_loc (input_location
, COND_EXPR
,
4048 void_type_node
, present
, tmp2
,
4049 build_empty_stmt (input_location
));
4051 gfc_add_expr_to_block (init
, tmp2
);
4054 gfc_restore_backend_locus (loc
);
4056 /* Pass the final character length back. */
4057 if (sym
->attr
.intent
!= INTENT_IN
)
4059 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4060 gfc_charlen_type_node
, tmp
,
4061 sym
->ts
.u
.cl
->backend_decl
);
4062 if (sym
->attr
.optional
)
4064 tree present
= gfc_conv_expr_present (sym
);
4065 tmp
= build3_loc (input_location
, COND_EXPR
,
4066 void_type_node
, present
, tmp
,
4067 build_empty_stmt (input_location
));
4076 /* Generate function entry and exit code, and add it to the function body.
4078 Allocation and initialization of array variables.
4079 Allocation of character string variables.
4080 Initialization and possibly repacking of dummy arrays.
4081 Initialization of ASSIGN statement auxiliary variable.
4082 Initialization of ASSOCIATE names.
4083 Automatic deallocation. */
4086 gfc_trans_deferred_vars (gfc_symbol
* proc_sym
, gfc_wrapped_block
* block
)
4090 gfc_formal_arglist
*f
;
4091 stmtblock_t tmpblock
;
4092 bool seen_trans_deferred_array
= false;
4098 /* Deal with implicit return variables. Explicit return variables will
4099 already have been added. */
4100 if (gfc_return_by_reference (proc_sym
) && proc_sym
->result
== proc_sym
)
4102 if (!current_fake_result_decl
)
4104 gfc_entry_list
*el
= NULL
;
4105 if (proc_sym
->attr
.entry_master
)
4107 for (el
= proc_sym
->ns
->entries
; el
; el
= el
->next
)
4108 if (el
->sym
!= el
->sym
->result
)
4111 /* TODO: move to the appropriate place in resolve.c. */
4112 if (warn_return_type
&& el
== NULL
)
4113 gfc_warning (OPT_Wreturn_type
,
4114 "Return value of function %qs at %L not set",
4115 proc_sym
->name
, &proc_sym
->declared_at
);
4117 else if (proc_sym
->as
)
4119 tree result
= TREE_VALUE (current_fake_result_decl
);
4120 gfc_save_backend_locus (&loc
);
4121 gfc_set_backend_locus (&proc_sym
->declared_at
);
4122 gfc_trans_dummy_array_bias (proc_sym
, result
, block
);
4124 /* An automatic character length, pointer array result. */
4125 if (proc_sym
->ts
.type
== BT_CHARACTER
4126 && TREE_CODE (proc_sym
->ts
.u
.cl
->backend_decl
) == VAR_DECL
)
4129 if (proc_sym
->ts
.deferred
)
4131 gfc_start_block (&init
);
4132 tmp
= gfc_null_and_pass_deferred_len (proc_sym
, &init
, &loc
);
4133 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
4136 gfc_trans_dummy_character (proc_sym
, proc_sym
->ts
.u
.cl
, block
);
4139 else if (proc_sym
->ts
.type
== BT_CHARACTER
)
4141 if (proc_sym
->ts
.deferred
)
4144 gfc_save_backend_locus (&loc
);
4145 gfc_set_backend_locus (&proc_sym
->declared_at
);
4146 gfc_start_block (&init
);
4147 /* Zero the string length on entry. */
4148 gfc_add_modify (&init
, proc_sym
->ts
.u
.cl
->backend_decl
,
4149 build_int_cst (gfc_charlen_type_node
, 0));
4150 /* Null the pointer. */
4151 e
= gfc_lval_expr_from_sym (proc_sym
);
4152 gfc_init_se (&se
, NULL
);
4153 se
.want_pointer
= 1;
4154 gfc_conv_expr (&se
, e
);
4157 gfc_add_modify (&init
, tmp
,
4158 fold_convert (TREE_TYPE (se
.expr
),
4159 null_pointer_node
));
4160 gfc_restore_backend_locus (&loc
);
4162 /* Pass back the string length on exit. */
4163 tmp
= proc_sym
->ts
.u
.cl
->backend_decl
;
4164 if (TREE_CODE (tmp
) != INDIRECT_REF
4165 && proc_sym
->ts
.u
.cl
->passed_length
)
4167 tmp
= proc_sym
->ts
.u
.cl
->passed_length
;
4168 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
4169 tmp
= fold_convert (gfc_charlen_type_node
, tmp
);
4170 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4171 gfc_charlen_type_node
, tmp
,
4172 proc_sym
->ts
.u
.cl
->backend_decl
);
4177 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
4179 else if (TREE_CODE (proc_sym
->ts
.u
.cl
->backend_decl
) == VAR_DECL
)
4180 gfc_trans_dummy_character (proc_sym
, proc_sym
->ts
.u
.cl
, block
);
4183 gcc_assert (flag_f2c
&& proc_sym
->ts
.type
== BT_COMPLEX
);
4186 /* Initialize the INTENT(OUT) derived type dummy arguments. This
4187 should be done here so that the offsets and lbounds of arrays
4189 gfc_save_backend_locus (&loc
);
4190 gfc_set_backend_locus (&proc_sym
->declared_at
);
4191 init_intent_out_dt (proc_sym
, block
);
4192 gfc_restore_backend_locus (&loc
);
4194 for (sym
= proc_sym
->tlink
; sym
!= proc_sym
; sym
= sym
->tlink
)
4196 bool alloc_comp_or_fini
= (sym
->ts
.type
== BT_DERIVED
)
4197 && (sym
->ts
.u
.derived
->attr
.alloc_comp
4198 || gfc_is_finalizable (sym
->ts
.u
.derived
,
4203 if (sym
->attr
.subref_array_pointer
4204 && GFC_DECL_SPAN (sym
->backend_decl
)
4205 && !TREE_STATIC (GFC_DECL_SPAN (sym
->backend_decl
)))
4207 gfc_init_block (&tmpblock
);
4208 gfc_add_modify (&tmpblock
, GFC_DECL_SPAN (sym
->backend_decl
),
4209 build_int_cst (gfc_array_index_type
, 0));
4210 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
),
4214 if (sym
->ts
.type
== BT_CLASS
4215 && (sym
->attr
.save
|| flag_max_stack_var_size
== 0)
4216 && CLASS_DATA (sym
)->attr
.allocatable
)
4220 if (UNLIMITED_POLY (sym
))
4221 vptr
= null_pointer_node
;
4225 vsym
= gfc_find_derived_vtab (sym
->ts
.u
.derived
);
4226 vptr
= gfc_get_symbol_decl (vsym
);
4227 vptr
= gfc_build_addr_expr (NULL
, vptr
);
4230 if (CLASS_DATA (sym
)->attr
.dimension
4231 || (CLASS_DATA (sym
)->attr
.codimension
4232 && flag_coarray
!= GFC_FCOARRAY_LIB
))
4234 tmp
= gfc_class_data_get (sym
->backend_decl
);
4235 tmp
= gfc_build_null_descriptor (TREE_TYPE (tmp
));
4238 tmp
= null_pointer_node
;
4240 DECL_INITIAL (sym
->backend_decl
)
4241 = gfc_class_set_static_fields (sym
->backend_decl
, vptr
, tmp
);
4242 TREE_CONSTANT (DECL_INITIAL (sym
->backend_decl
)) = 1;
4244 else if ((sym
->attr
.dimension
|| sym
->attr
.codimension
4245 || (IS_CLASS_ARRAY (sym
) && !CLASS_DATA (sym
)->attr
.allocatable
)))
4247 bool is_classarray
= IS_CLASS_ARRAY (sym
);
4248 symbol_attribute
*array_attr
;
4250 array_type type_of_array
;
4252 array_attr
= is_classarray
? &CLASS_DATA (sym
)->attr
: &sym
->attr
;
4253 as
= is_classarray
? CLASS_DATA (sym
)->as
: sym
->as
;
4254 /* Assumed-size Cray pointees need to be treated as AS_EXPLICIT. */
4255 type_of_array
= as
->type
;
4256 if (type_of_array
== AS_ASSUMED_SIZE
&& as
->cp_was_assumed
)
4257 type_of_array
= AS_EXPLICIT
;
4258 switch (type_of_array
)
4261 if (sym
->attr
.dummy
|| sym
->attr
.result
)
4262 gfc_trans_dummy_array_bias (sym
, sym
->backend_decl
, block
);
4263 /* Allocatable and pointer arrays need to processed
4265 else if ((sym
->ts
.type
!= BT_CLASS
&& sym
->attr
.pointer
)
4266 || (sym
->ts
.type
== BT_CLASS
4267 && CLASS_DATA (sym
)->attr
.class_pointer
)
4268 || array_attr
->allocatable
)
4270 if (TREE_STATIC (sym
->backend_decl
))
4272 gfc_save_backend_locus (&loc
);
4273 gfc_set_backend_locus (&sym
->declared_at
);
4274 gfc_trans_static_array_pointer (sym
);
4275 gfc_restore_backend_locus (&loc
);
4279 seen_trans_deferred_array
= true;
4280 gfc_trans_deferred_array (sym
, block
);
4283 else if (sym
->attr
.codimension
4284 && TREE_STATIC (sym
->backend_decl
))
4286 gfc_init_block (&tmpblock
);
4287 gfc_trans_array_cobounds (TREE_TYPE (sym
->backend_decl
),
4289 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
),
4295 gfc_save_backend_locus (&loc
);
4296 gfc_set_backend_locus (&sym
->declared_at
);
4298 if (alloc_comp_or_fini
)
4300 seen_trans_deferred_array
= true;
4301 gfc_trans_deferred_array (sym
, block
);
4303 else if (sym
->ts
.type
== BT_DERIVED
4306 && sym
->attr
.save
== SAVE_NONE
)
4308 gfc_start_block (&tmpblock
);
4309 gfc_init_default_dt (sym
, &tmpblock
, false);
4310 gfc_add_init_cleanup (block
,
4311 gfc_finish_block (&tmpblock
),
4315 gfc_trans_auto_array_allocation (sym
->backend_decl
,
4317 gfc_restore_backend_locus (&loc
);
4321 case AS_ASSUMED_SIZE
:
4322 /* Must be a dummy parameter. */
4323 gcc_assert (sym
->attr
.dummy
|| as
->cp_was_assumed
);
4325 /* We should always pass assumed size arrays the g77 way. */
4326 if (sym
->attr
.dummy
)
4327 gfc_trans_g77_array (sym
, block
);
4330 case AS_ASSUMED_SHAPE
:
4331 /* Must be a dummy parameter. */
4332 gcc_assert (sym
->attr
.dummy
);
4334 gfc_trans_dummy_array_bias (sym
, sym
->backend_decl
, block
);
4337 case AS_ASSUMED_RANK
:
4339 seen_trans_deferred_array
= true;
4340 gfc_trans_deferred_array (sym
, block
);
4341 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.deferred
4342 && sym
->attr
.result
)
4344 gfc_start_block (&init
);
4345 gfc_save_backend_locus (&loc
);
4346 gfc_set_backend_locus (&sym
->declared_at
);
4347 tmp
= gfc_null_and_pass_deferred_len (sym
, &init
, &loc
);
4348 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
4355 if (alloc_comp_or_fini
&& !seen_trans_deferred_array
)
4356 gfc_trans_deferred_array (sym
, block
);
4358 else if ((!sym
->attr
.dummy
|| sym
->ts
.deferred
)
4359 && (sym
->ts
.type
== BT_CLASS
4360 && CLASS_DATA (sym
)->attr
.class_pointer
))
4362 else if ((!sym
->attr
.dummy
|| sym
->ts
.deferred
)
4363 && (sym
->attr
.allocatable
4364 || (sym
->attr
.pointer
&& sym
->attr
.result
)
4365 || (sym
->ts
.type
== BT_CLASS
4366 && CLASS_DATA (sym
)->attr
.allocatable
)))
4368 if (!sym
->attr
.save
&& flag_max_stack_var_size
!= 0)
4370 tree descriptor
= NULL_TREE
;
4372 gfc_save_backend_locus (&loc
);
4373 gfc_set_backend_locus (&sym
->declared_at
);
4374 gfc_start_block (&init
);
4376 if (!sym
->attr
.pointer
)
4378 /* Nullify and automatic deallocation of allocatable
4380 e
= gfc_lval_expr_from_sym (sym
);
4381 if (sym
->ts
.type
== BT_CLASS
)
4382 gfc_add_data_component (e
);
4384 gfc_init_se (&se
, NULL
);
4385 if (sym
->ts
.type
!= BT_CLASS
4386 || sym
->ts
.u
.derived
->attr
.dimension
4387 || sym
->ts
.u
.derived
->attr
.codimension
)
4389 se
.want_pointer
= 1;
4390 gfc_conv_expr (&se
, e
);
4392 else if (sym
->ts
.type
== BT_CLASS
4393 && !CLASS_DATA (sym
)->attr
.dimension
4394 && !CLASS_DATA (sym
)->attr
.codimension
)
4396 se
.want_pointer
= 1;
4397 gfc_conv_expr (&se
, e
);
4401 se
.descriptor_only
= 1;
4402 gfc_conv_expr (&se
, e
);
4403 descriptor
= se
.expr
;
4404 se
.expr
= gfc_conv_descriptor_data_addr (se
.expr
);
4405 se
.expr
= build_fold_indirect_ref_loc (input_location
, se
.expr
);
4409 if (!sym
->attr
.dummy
|| sym
->attr
.intent
== INTENT_OUT
)
4411 /* Nullify when entering the scope. */
4412 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4413 TREE_TYPE (se
.expr
), se
.expr
,
4414 fold_convert (TREE_TYPE (se
.expr
),
4415 null_pointer_node
));
4416 if (sym
->attr
.optional
)
4418 tree present
= gfc_conv_expr_present (sym
);
4419 tmp
= build3_loc (input_location
, COND_EXPR
,
4420 void_type_node
, present
, tmp
,
4421 build_empty_stmt (input_location
));
4423 gfc_add_expr_to_block (&init
, tmp
);
4427 if ((sym
->attr
.dummy
|| sym
->attr
.result
)
4428 && sym
->ts
.type
== BT_CHARACTER
4430 && sym
->ts
.u
.cl
->passed_length
)
4431 tmp
= gfc_null_and_pass_deferred_len (sym
, &init
, &loc
);
4433 gfc_restore_backend_locus (&loc
);
4435 /* Deallocate when leaving the scope. Nullifying is not
4437 if (!sym
->attr
.result
&& !sym
->attr
.dummy
&& !sym
->attr
.pointer
4438 && !sym
->ns
->proc_name
->attr
.is_main_program
)
4440 if (sym
->ts
.type
== BT_CLASS
4441 && CLASS_DATA (sym
)->attr
.codimension
)
4442 tmp
= gfc_deallocate_with_status (descriptor
, NULL_TREE
,
4443 NULL_TREE
, NULL_TREE
,
4444 NULL_TREE
, true, NULL
,
4448 gfc_expr
*expr
= gfc_lval_expr_from_sym (sym
);
4449 tmp
= gfc_deallocate_scalar_with_status (se
.expr
, NULL_TREE
,
4450 true, expr
, sym
->ts
);
4451 gfc_free_expr (expr
);
4455 if (sym
->ts
.type
== BT_CLASS
)
4457 /* Initialize _vptr to declared type. */
4461 gfc_save_backend_locus (&loc
);
4462 gfc_set_backend_locus (&sym
->declared_at
);
4463 e
= gfc_lval_expr_from_sym (sym
);
4464 gfc_add_vptr_component (e
);
4465 gfc_init_se (&se
, NULL
);
4466 se
.want_pointer
= 1;
4467 gfc_conv_expr (&se
, e
);
4469 if (UNLIMITED_POLY (sym
))
4470 rhs
= build_int_cst (TREE_TYPE (se
.expr
), 0);
4473 vtab
= gfc_find_derived_vtab (sym
->ts
.u
.derived
);
4474 rhs
= gfc_build_addr_expr (TREE_TYPE (se
.expr
),
4475 gfc_get_symbol_decl (vtab
));
4477 gfc_add_modify (&init
, se
.expr
, rhs
);
4478 gfc_restore_backend_locus (&loc
);
4481 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
4484 else if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.deferred
)
4489 /* If we get to here, all that should be left are pointers. */
4490 gcc_assert (sym
->attr
.pointer
);
4492 if (sym
->attr
.dummy
)
4494 gfc_start_block (&init
);
4495 gfc_save_backend_locus (&loc
);
4496 gfc_set_backend_locus (&sym
->declared_at
);
4497 tmp
= gfc_null_and_pass_deferred_len (sym
, &init
, &loc
);
4498 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
4501 else if (sym
->ts
.deferred
)
4502 gfc_fatal_error ("Deferred type parameter not yet supported");
4503 else if (alloc_comp_or_fini
)
4504 gfc_trans_deferred_array (sym
, block
);
4505 else if (sym
->ts
.type
== BT_CHARACTER
)
4507 gfc_save_backend_locus (&loc
);
4508 gfc_set_backend_locus (&sym
->declared_at
);
4509 if (sym
->attr
.dummy
|| sym
->attr
.result
)
4510 gfc_trans_dummy_character (sym
, sym
->ts
.u
.cl
, block
);
4512 gfc_trans_auto_character_variable (sym
, block
);
4513 gfc_restore_backend_locus (&loc
);
4515 else if (sym
->attr
.assign
)
4517 gfc_save_backend_locus (&loc
);
4518 gfc_set_backend_locus (&sym
->declared_at
);
4519 gfc_trans_assign_aux_var (sym
, block
);
4520 gfc_restore_backend_locus (&loc
);
4522 else if (sym
->ts
.type
== BT_DERIVED
4525 && sym
->attr
.save
== SAVE_NONE
)
4527 gfc_start_block (&tmpblock
);
4528 gfc_init_default_dt (sym
, &tmpblock
, false);
4529 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
),
4532 else if (!(UNLIMITED_POLY(sym
)))
4536 gfc_init_block (&tmpblock
);
4538 for (f
= gfc_sym_get_dummy_args (proc_sym
); f
; f
= f
->next
)
4540 if (f
->sym
&& f
->sym
->tlink
== NULL
&& f
->sym
->ts
.type
== BT_CHARACTER
)
4542 gcc_assert (f
->sym
->ts
.u
.cl
->backend_decl
!= NULL
);
4543 if (TREE_CODE (f
->sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
4544 gfc_trans_vla_type_sizes (f
->sym
, &tmpblock
);
4548 if (gfc_return_by_reference (proc_sym
) && proc_sym
->ts
.type
== BT_CHARACTER
4549 && current_fake_result_decl
!= NULL
)
4551 gcc_assert (proc_sym
->ts
.u
.cl
->backend_decl
!= NULL
);
4552 if (TREE_CODE (proc_sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
4553 gfc_trans_vla_type_sizes (proc_sym
, &tmpblock
);
4556 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
), NULL_TREE
);
4560 struct module_hasher
: ggc_ptr_hash
<module_htab_entry
>
4562 typedef const char *compare_type
;
4564 static hashval_t
hash (module_htab_entry
*s
) { return htab_hash_string (s
); }
4566 equal (module_htab_entry
*a
, const char *b
)
4568 return !strcmp (a
->name
, b
);
4572 static GTY (()) hash_table
<module_hasher
> *module_htab
;
4574 /* Hash and equality functions for module_htab's decls. */
4577 module_decl_hasher::hash (tree t
)
4579 const_tree n
= DECL_NAME (t
);
4581 n
= TYPE_NAME (TREE_TYPE (t
));
4582 return htab_hash_string (IDENTIFIER_POINTER (n
));
4586 module_decl_hasher::equal (tree t1
, const char *x2
)
4588 const_tree n1
= DECL_NAME (t1
);
4589 if (n1
== NULL_TREE
)
4590 n1
= TYPE_NAME (TREE_TYPE (t1
));
4591 return strcmp (IDENTIFIER_POINTER (n1
), x2
) == 0;
4594 struct module_htab_entry
*
4595 gfc_find_module (const char *name
)
4598 module_htab
= hash_table
<module_hasher
>::create_ggc (10);
4600 module_htab_entry
**slot
4601 = module_htab
->find_slot_with_hash (name
, htab_hash_string (name
), INSERT
);
4604 module_htab_entry
*entry
= ggc_cleared_alloc
<module_htab_entry
> ();
4606 entry
->name
= gfc_get_string (name
);
4607 entry
->decls
= hash_table
<module_decl_hasher
>::create_ggc (10);
4614 gfc_module_add_decl (struct module_htab_entry
*entry
, tree decl
)
4618 if (DECL_NAME (decl
))
4619 name
= IDENTIFIER_POINTER (DECL_NAME (decl
));
4622 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
4623 name
= IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl
)));
4626 = entry
->decls
->find_slot_with_hash (name
, htab_hash_string (name
),
4633 /* Generate debugging symbols for namelists. This function must come after
4634 generate_local_decl to ensure that the variables in the namelist are
4635 already declared. */
4638 generate_namelist_decl (gfc_symbol
* sym
)
4642 vec
<constructor_elt
, va_gc
> *nml_decls
= NULL
;
4644 gcc_assert (sym
->attr
.flavor
== FL_NAMELIST
);
4645 for (nml
= sym
->namelist
; nml
; nml
= nml
->next
)
4647 if (nml
->sym
->backend_decl
== NULL_TREE
)
4649 nml
->sym
->attr
.referenced
= 1;
4650 nml
->sym
->backend_decl
= gfc_get_symbol_decl (nml
->sym
);
4652 DECL_IGNORED_P (nml
->sym
->backend_decl
) = 0;
4653 CONSTRUCTOR_APPEND_ELT (nml_decls
, NULL_TREE
, nml
->sym
->backend_decl
);
4656 decl
= make_node (NAMELIST_DECL
);
4657 TREE_TYPE (decl
) = void_type_node
;
4658 NAMELIST_DECL_ASSOCIATED_DECL (decl
) = build_constructor (NULL_TREE
, nml_decls
);
4659 DECL_NAME (decl
) = get_identifier (sym
->name
);
4664 /* Output an initialized decl for a module variable. */
4667 gfc_create_module_variable (gfc_symbol
* sym
)
4671 /* Module functions with alternate entries are dealt with later and
4672 would get caught by the next condition. */
4673 if (sym
->attr
.entry
)
4676 /* Make sure we convert the types of the derived types from iso_c_binding
4678 if (sym
->attr
.flavor
!= FL_PROCEDURE
&& sym
->attr
.is_iso_c
4679 && sym
->ts
.type
== BT_DERIVED
)
4680 sym
->backend_decl
= gfc_typenode_for_spec (&(sym
->ts
));
4682 if (gfc_fl_struct (sym
->attr
.flavor
)
4683 && sym
->backend_decl
4684 && TREE_CODE (sym
->backend_decl
) == RECORD_TYPE
)
4686 decl
= sym
->backend_decl
;
4687 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
4689 if (!sym
->attr
.use_assoc
&& !sym
->attr
.used_in_submodule
)
4691 gcc_assert (TYPE_CONTEXT (decl
) == NULL_TREE
4692 || TYPE_CONTEXT (decl
) == sym
->ns
->proc_name
->backend_decl
);
4693 gcc_assert (DECL_CONTEXT (TYPE_STUB_DECL (decl
)) == NULL_TREE
4694 || DECL_CONTEXT (TYPE_STUB_DECL (decl
))
4695 == sym
->ns
->proc_name
->backend_decl
);
4697 TYPE_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
4698 DECL_CONTEXT (TYPE_STUB_DECL (decl
)) = sym
->ns
->proc_name
->backend_decl
;
4699 gfc_module_add_decl (cur_module
, TYPE_STUB_DECL (decl
));
4702 /* Only output variables, procedure pointers and array valued,
4703 or derived type, parameters. */
4704 if (sym
->attr
.flavor
!= FL_VARIABLE
4705 && !(sym
->attr
.flavor
== FL_PARAMETER
4706 && (sym
->attr
.dimension
|| sym
->ts
.type
== BT_DERIVED
))
4707 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.proc_pointer
))
4710 if ((sym
->attr
.in_common
|| sym
->attr
.in_equivalence
) && sym
->backend_decl
)
4712 decl
= sym
->backend_decl
;
4713 gcc_assert (DECL_FILE_SCOPE_P (decl
));
4714 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
4715 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
4716 gfc_module_add_decl (cur_module
, decl
);
4719 /* Don't generate variables from other modules. Variables from
4720 COMMONs and Cray pointees will already have been generated. */
4721 if (sym
->attr
.use_assoc
|| sym
->attr
.used_in_submodule
4722 || sym
->attr
.in_common
|| sym
->attr
.cray_pointee
)
4725 /* Equivalenced variables arrive here after creation. */
4726 if (sym
->backend_decl
4727 && (sym
->equiv_built
|| sym
->attr
.in_equivalence
))
4730 if (sym
->backend_decl
&& !sym
->attr
.vtab
&& !sym
->attr
.target
)
4731 gfc_internal_error ("backend decl for module variable %qs already exists",
4734 if (sym
->module
&& !sym
->attr
.result
&& !sym
->attr
.dummy
4735 && (sym
->attr
.access
== ACCESS_UNKNOWN
4736 && (sym
->ns
->default_access
== ACCESS_PRIVATE
4737 || (sym
->ns
->default_access
== ACCESS_UNKNOWN
4738 && flag_module_private
))))
4739 sym
->attr
.access
= ACCESS_PRIVATE
;
4741 if (warn_unused_variable
&& !sym
->attr
.referenced
4742 && sym
->attr
.access
== ACCESS_PRIVATE
)
4743 gfc_warning (OPT_Wunused_value
,
4744 "Unused PRIVATE module variable %qs declared at %L",
4745 sym
->name
, &sym
->declared_at
);
4747 /* We always want module variables to be created. */
4748 sym
->attr
.referenced
= 1;
4749 /* Create the decl. */
4750 decl
= gfc_get_symbol_decl (sym
);
4752 /* Create the variable. */
4754 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
4755 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
4756 rest_of_decl_compilation (decl
, 1, 0);
4757 gfc_module_add_decl (cur_module
, decl
);
4759 /* Also add length of strings. */
4760 if (sym
->ts
.type
== BT_CHARACTER
)
4764 length
= sym
->ts
.u
.cl
->backend_decl
;
4765 gcc_assert (length
|| sym
->attr
.proc_pointer
);
4766 if (length
&& !INTEGER_CST_P (length
))
4769 rest_of_decl_compilation (length
, 1, 0);
4773 if (sym
->attr
.codimension
&& !sym
->attr
.dummy
&& !sym
->attr
.allocatable
4774 && sym
->attr
.referenced
&& !sym
->attr
.use_assoc
)
4775 has_coarray_vars
= true;
4778 /* Emit debug information for USE statements. */
4781 gfc_trans_use_stmts (gfc_namespace
* ns
)
4783 gfc_use_list
*use_stmt
;
4784 for (use_stmt
= ns
->use_stmts
; use_stmt
; use_stmt
= use_stmt
->next
)
4786 struct module_htab_entry
*entry
4787 = gfc_find_module (use_stmt
->module_name
);
4788 gfc_use_rename
*rent
;
4790 if (entry
->namespace_decl
== NULL
)
4792 entry
->namespace_decl
4793 = build_decl (input_location
,
4795 get_identifier (use_stmt
->module_name
),
4797 DECL_EXTERNAL (entry
->namespace_decl
) = 1;
4799 gfc_set_backend_locus (&use_stmt
->where
);
4800 if (!use_stmt
->only_flag
)
4801 (*debug_hooks
->imported_module_or_decl
) (entry
->namespace_decl
,
4803 ns
->proc_name
->backend_decl
,
4805 for (rent
= use_stmt
->rename
; rent
; rent
= rent
->next
)
4807 tree decl
, local_name
;
4809 if (rent
->op
!= INTRINSIC_NONE
)
4812 hashval_t hash
= htab_hash_string (rent
->use_name
);
4813 tree
*slot
= entry
->decls
->find_slot_with_hash (rent
->use_name
, hash
,
4819 st
= gfc_find_symtree (ns
->sym_root
,
4821 ? rent
->local_name
: rent
->use_name
);
4823 /* The following can happen if a derived type is renamed. */
4827 name
= xstrdup (rent
->local_name
[0]
4828 ? rent
->local_name
: rent
->use_name
);
4829 name
[0] = (char) TOUPPER ((unsigned char) name
[0]);
4830 st
= gfc_find_symtree (ns
->sym_root
, name
);
4835 /* Sometimes, generic interfaces wind up being over-ruled by a
4836 local symbol (see PR41062). */
4837 if (!st
->n
.sym
->attr
.use_assoc
)
4840 if (st
->n
.sym
->backend_decl
4841 && DECL_P (st
->n
.sym
->backend_decl
)
4842 && st
->n
.sym
->module
4843 && strcmp (st
->n
.sym
->module
, use_stmt
->module_name
) == 0)
4845 gcc_assert (DECL_EXTERNAL (entry
->namespace_decl
)
4846 || (TREE_CODE (st
->n
.sym
->backend_decl
)
4848 decl
= copy_node (st
->n
.sym
->backend_decl
);
4849 DECL_CONTEXT (decl
) = entry
->namespace_decl
;
4850 DECL_EXTERNAL (decl
) = 1;
4851 DECL_IGNORED_P (decl
) = 0;
4852 DECL_INITIAL (decl
) = NULL_TREE
;
4854 else if (st
->n
.sym
->attr
.flavor
== FL_NAMELIST
4855 && st
->n
.sym
->attr
.use_only
4856 && st
->n
.sym
->module
4857 && strcmp (st
->n
.sym
->module
, use_stmt
->module_name
)
4860 decl
= generate_namelist_decl (st
->n
.sym
);
4861 DECL_CONTEXT (decl
) = entry
->namespace_decl
;
4862 DECL_EXTERNAL (decl
) = 1;
4863 DECL_IGNORED_P (decl
) = 0;
4864 DECL_INITIAL (decl
) = NULL_TREE
;
4868 *slot
= error_mark_node
;
4869 entry
->decls
->clear_slot (slot
);
4874 decl
= (tree
) *slot
;
4875 if (rent
->local_name
[0])
4876 local_name
= get_identifier (rent
->local_name
);
4878 local_name
= NULL_TREE
;
4879 gfc_set_backend_locus (&rent
->where
);
4880 (*debug_hooks
->imported_module_or_decl
) (decl
, local_name
,
4881 ns
->proc_name
->backend_decl
,
4882 !use_stmt
->only_flag
);
4888 /* Return true if expr is a constant initializer that gfc_conv_initializer
4892 check_constant_initializer (gfc_expr
*expr
, gfc_typespec
*ts
, bool array
,
4902 if (expr
->expr_type
== EXPR_CONSTANT
|| expr
->expr_type
== EXPR_NULL
)
4904 else if (expr
->expr_type
== EXPR_STRUCTURE
)
4905 return check_constant_initializer (expr
, ts
, false, false);
4906 else if (expr
->expr_type
!= EXPR_ARRAY
)
4908 for (c
= gfc_constructor_first (expr
->value
.constructor
);
4909 c
; c
= gfc_constructor_next (c
))
4913 if (c
->expr
->expr_type
== EXPR_STRUCTURE
)
4915 if (!check_constant_initializer (c
->expr
, ts
, false, false))
4918 else if (c
->expr
->expr_type
!= EXPR_CONSTANT
)
4923 else switch (ts
->type
)
4926 if (expr
->expr_type
!= EXPR_STRUCTURE
)
4928 cm
= expr
->ts
.u
.derived
->components
;
4929 for (c
= gfc_constructor_first (expr
->value
.constructor
);
4930 c
; c
= gfc_constructor_next (c
), cm
= cm
->next
)
4932 if (!c
->expr
|| cm
->attr
.allocatable
)
4934 if (!check_constant_initializer (c
->expr
, &cm
->ts
,
4941 return expr
->expr_type
== EXPR_CONSTANT
;
4945 /* Emit debug info for parameters and unreferenced variables with
4949 gfc_emit_parameter_debug_info (gfc_symbol
*sym
)
4953 if (sym
->attr
.flavor
!= FL_PARAMETER
4954 && (sym
->attr
.flavor
!= FL_VARIABLE
|| sym
->attr
.referenced
))
4957 if (sym
->backend_decl
!= NULL
4958 || sym
->value
== NULL
4959 || sym
->attr
.use_assoc
4962 || sym
->attr
.function
4963 || sym
->attr
.intrinsic
4964 || sym
->attr
.pointer
4965 || sym
->attr
.allocatable
4966 || sym
->attr
.cray_pointee
4967 || sym
->attr
.threadprivate
4968 || sym
->attr
.is_bind_c
4969 || sym
->attr
.subref_array_pointer
4970 || sym
->attr
.assign
)
4973 if (sym
->ts
.type
== BT_CHARACTER
)
4975 gfc_conv_const_charlen (sym
->ts
.u
.cl
);
4976 if (sym
->ts
.u
.cl
->backend_decl
== NULL
4977 || TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) != INTEGER_CST
)
4980 else if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.alloc_comp
)
4987 if (sym
->as
->type
!= AS_EXPLICIT
)
4989 for (n
= 0; n
< sym
->as
->rank
; n
++)
4990 if (sym
->as
->lower
[n
]->expr_type
!= EXPR_CONSTANT
4991 || sym
->as
->upper
[n
] == NULL
4992 || sym
->as
->upper
[n
]->expr_type
!= EXPR_CONSTANT
)
4996 if (!check_constant_initializer (sym
->value
, &sym
->ts
,
4997 sym
->attr
.dimension
, false))
5000 if (flag_coarray
== GFC_FCOARRAY_LIB
&& sym
->attr
.codimension
)
5003 /* Create the decl for the variable or constant. */
5004 decl
= build_decl (input_location
,
5005 sym
->attr
.flavor
== FL_PARAMETER
? CONST_DECL
: VAR_DECL
,
5006 gfc_sym_identifier (sym
), gfc_sym_type (sym
));
5007 if (sym
->attr
.flavor
== FL_PARAMETER
)
5008 TREE_READONLY (decl
) = 1;
5009 gfc_set_decl_location (decl
, &sym
->declared_at
);
5010 if (sym
->attr
.dimension
)
5011 GFC_DECL_PACKED_ARRAY (decl
) = 1;
5012 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
5013 TREE_STATIC (decl
) = 1;
5014 TREE_USED (decl
) = 1;
5015 if (DECL_CONTEXT (decl
) && TREE_CODE (DECL_CONTEXT (decl
)) == NAMESPACE_DECL
)
5016 TREE_PUBLIC (decl
) = 1;
5017 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
5019 sym
->attr
.dimension
,
5021 debug_hooks
->early_global_decl (decl
);
5026 generate_coarray_sym_init (gfc_symbol
*sym
)
5028 tree tmp
, size
, decl
, token
, desc
;
5029 bool is_lock_type
, is_event_type
;
5032 symbol_attribute attr
;
5034 if (sym
->attr
.dummy
|| sym
->attr
.allocatable
|| !sym
->attr
.codimension
5035 || sym
->attr
.use_assoc
|| !sym
->attr
.referenced
5036 || sym
->attr
.select_type_temporary
)
5039 decl
= sym
->backend_decl
;
5040 TREE_USED(decl
) = 1;
5041 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (decl
)));
5043 is_lock_type
= sym
->ts
.type
== BT_DERIVED
5044 && sym
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
5045 && sym
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_LOCK_TYPE
;
5047 is_event_type
= sym
->ts
.type
== BT_DERIVED
5048 && sym
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
5049 && sym
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_EVENT_TYPE
;
5051 /* FIXME: Workaround for PR middle-end/49106, cf. also PR middle-end/49108
5052 to make sure the variable is not optimized away. */
5053 DECL_PRESERVE_P (DECL_CONTEXT (decl
)) = 1;
5055 /* For lock types, we pass the array size as only the library knows the
5056 size of the variable. */
5057 if (is_lock_type
|| is_event_type
)
5058 size
= gfc_index_one_node
;
5060 size
= TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (decl
)));
5062 /* Ensure that we do not have size=0 for zero-sized arrays. */
5063 size
= fold_build2_loc (input_location
, MAX_EXPR
, size_type_node
,
5064 fold_convert (size_type_node
, size
),
5065 build_int_cst (size_type_node
, 1));
5067 if (GFC_TYPE_ARRAY_RANK (TREE_TYPE (decl
)))
5069 tmp
= GFC_TYPE_ARRAY_SIZE (TREE_TYPE (decl
));
5070 size
= fold_build2_loc (input_location
, MULT_EXPR
, size_type_node
,
5071 fold_convert (size_type_node
, tmp
), size
);
5074 gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (decl
)) != NULL_TREE
);
5075 token
= gfc_build_addr_expr (ppvoid_type_node
,
5076 GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE(decl
)));
5078 reg_type
= sym
->attr
.artificial
? GFC_CAF_CRITICAL
: GFC_CAF_LOCK_STATIC
;
5079 else if (is_event_type
)
5080 reg_type
= GFC_CAF_EVENT_STATIC
;
5082 reg_type
= GFC_CAF_COARRAY_STATIC
;
5084 gfc_init_se (&se
, NULL
);
5085 desc
= gfc_conv_scalar_to_descriptor (&se
, decl
, attr
);
5086 gfc_add_block_to_block (&caf_init_block
, &se
.pre
);
5088 tmp
= build_call_expr_loc (input_location
, gfor_fndecl_caf_register
, 7, size
,
5089 build_int_cst (integer_type_node
, reg_type
),
5090 token
, gfc_build_addr_expr (pvoid_type_node
, desc
),
5091 null_pointer_node
, /* stat. */
5092 null_pointer_node
, /* errgmsg, errmsg_len. */
5093 build_int_cst (integer_type_node
, 0));
5094 gfc_add_expr_to_block (&caf_init_block
, tmp
);
5095 gfc_add_modify (&caf_init_block
, decl
, fold_convert (TREE_TYPE (decl
),
5096 gfc_conv_descriptor_data_get (desc
)));
5098 /* Handle "static" initializer. */
5101 sym
->attr
.pointer
= 1;
5102 tmp
= gfc_trans_assignment (gfc_lval_expr_from_sym (sym
), sym
->value
,
5104 sym
->attr
.pointer
= 0;
5105 gfc_add_expr_to_block (&caf_init_block
, tmp
);
5110 /* Generate constructor function to initialize static, nonallocatable
5114 generate_coarray_init (gfc_namespace
* ns
__attribute((unused
)))
5116 tree fndecl
, tmp
, decl
, save_fn_decl
;
5118 save_fn_decl
= current_function_decl
;
5119 push_function_context ();
5121 tmp
= build_function_type_list (void_type_node
, NULL_TREE
);
5122 fndecl
= build_decl (input_location
, FUNCTION_DECL
,
5123 create_tmp_var_name ("_caf_init"), tmp
);
5125 DECL_STATIC_CONSTRUCTOR (fndecl
) = 1;
5126 SET_DECL_INIT_PRIORITY (fndecl
, DEFAULT_INIT_PRIORITY
);
5128 decl
= build_decl (input_location
, RESULT_DECL
, NULL_TREE
, void_type_node
);
5129 DECL_ARTIFICIAL (decl
) = 1;
5130 DECL_IGNORED_P (decl
) = 1;
5131 DECL_CONTEXT (decl
) = fndecl
;
5132 DECL_RESULT (fndecl
) = decl
;
5135 current_function_decl
= fndecl
;
5136 announce_function (fndecl
);
5138 rest_of_decl_compilation (fndecl
, 0, 0);
5139 make_decl_rtl (fndecl
);
5140 allocate_struct_function (fndecl
, false);
5143 gfc_init_block (&caf_init_block
);
5145 gfc_traverse_ns (ns
, generate_coarray_sym_init
);
5147 DECL_SAVED_TREE (fndecl
) = gfc_finish_block (&caf_init_block
);
5151 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
5153 DECL_SAVED_TREE (fndecl
)
5154 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (fndecl
),
5155 DECL_INITIAL (fndecl
));
5156 dump_function (TDI_original
, fndecl
);
5158 cfun
->function_end_locus
= input_location
;
5161 if (decl_function_context (fndecl
))
5162 (void) cgraph_node::create (fndecl
);
5164 cgraph_node::finalize_function (fndecl
, true);
5166 pop_function_context ();
5167 current_function_decl
= save_fn_decl
;
5172 create_module_nml_decl (gfc_symbol
*sym
)
5174 if (sym
->attr
.flavor
== FL_NAMELIST
)
5176 tree decl
= generate_namelist_decl (sym
);
5178 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
5179 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
5180 rest_of_decl_compilation (decl
, 1, 0);
5181 gfc_module_add_decl (cur_module
, decl
);
5186 /* Generate all the required code for module variables. */
5189 gfc_generate_module_vars (gfc_namespace
* ns
)
5191 module_namespace
= ns
;
5192 cur_module
= gfc_find_module (ns
->proc_name
->name
);
5194 /* Check if the frontend left the namespace in a reasonable state. */
5195 gcc_assert (ns
->proc_name
&& !ns
->proc_name
->tlink
);
5197 /* Generate COMMON blocks. */
5198 gfc_trans_common (ns
);
5200 has_coarray_vars
= false;
5202 /* Create decls for all the module variables. */
5203 gfc_traverse_ns (ns
, gfc_create_module_variable
);
5204 gfc_traverse_ns (ns
, create_module_nml_decl
);
5206 if (flag_coarray
== GFC_FCOARRAY_LIB
&& has_coarray_vars
)
5207 generate_coarray_init (ns
);
5211 gfc_trans_use_stmts (ns
);
5212 gfc_traverse_ns (ns
, gfc_emit_parameter_debug_info
);
5217 gfc_generate_contained_functions (gfc_namespace
* parent
)
5221 /* We create all the prototypes before generating any code. */
5222 for (ns
= parent
->contained
; ns
; ns
= ns
->sibling
)
5224 /* Skip namespaces from used modules. */
5225 if (ns
->parent
!= parent
)
5228 gfc_create_function_decl (ns
, false);
5231 for (ns
= parent
->contained
; ns
; ns
= ns
->sibling
)
5233 /* Skip namespaces from used modules. */
5234 if (ns
->parent
!= parent
)
5237 gfc_generate_function_code (ns
);
5242 /* Drill down through expressions for the array specification bounds and
5243 character length calling generate_local_decl for all those variables
5244 that have not already been declared. */
5247 generate_local_decl (gfc_symbol
*);
5249 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
5252 expr_decls (gfc_expr
*e
, gfc_symbol
*sym
,
5253 int *f ATTRIBUTE_UNUSED
)
5255 if (e
->expr_type
!= EXPR_VARIABLE
5256 || sym
== e
->symtree
->n
.sym
5257 || e
->symtree
->n
.sym
->mark
5258 || e
->symtree
->n
.sym
->ns
!= sym
->ns
)
5261 generate_local_decl (e
->symtree
->n
.sym
);
5266 generate_expr_decls (gfc_symbol
*sym
, gfc_expr
*e
)
5268 gfc_traverse_expr (e
, sym
, expr_decls
, 0);
5272 /* Check for dependencies in the character length and array spec. */
5275 generate_dependency_declarations (gfc_symbol
*sym
)
5279 if (sym
->ts
.type
== BT_CHARACTER
5281 && sym
->ts
.u
.cl
->length
5282 && sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
)
5283 generate_expr_decls (sym
, sym
->ts
.u
.cl
->length
);
5285 if (sym
->as
&& sym
->as
->rank
)
5287 for (i
= 0; i
< sym
->as
->rank
; i
++)
5289 generate_expr_decls (sym
, sym
->as
->lower
[i
]);
5290 generate_expr_decls (sym
, sym
->as
->upper
[i
]);
5296 /* Generate decls for all local variables. We do this to ensure correct
5297 handling of expressions which only appear in the specification of
5301 generate_local_decl (gfc_symbol
* sym
)
5303 if (sym
->attr
.flavor
== FL_VARIABLE
)
5305 if (sym
->attr
.codimension
&& !sym
->attr
.dummy
&& !sym
->attr
.allocatable
5306 && sym
->attr
.referenced
&& !sym
->attr
.use_assoc
)
5307 has_coarray_vars
= true;
5309 if (!sym
->attr
.dummy
&& !sym
->ns
->proc_name
->attr
.entry_master
)
5310 generate_dependency_declarations (sym
);
5312 if (sym
->attr
.referenced
)
5313 gfc_get_symbol_decl (sym
);
5315 /* Warnings for unused dummy arguments. */
5316 else if (sym
->attr
.dummy
&& !sym
->attr
.in_namelist
)
5318 /* INTENT(out) dummy arguments are likely meant to be set. */
5319 if (warn_unused_dummy_argument
&& sym
->attr
.intent
== INTENT_OUT
)
5321 if (sym
->ts
.type
!= BT_DERIVED
)
5322 gfc_warning (OPT_Wunused_dummy_argument
,
5323 "Dummy argument %qs at %L was declared "
5324 "INTENT(OUT) but was not set", sym
->name
,
5326 else if (!gfc_has_default_initializer (sym
->ts
.u
.derived
)
5327 && !sym
->ts
.u
.derived
->attr
.zero_comp
)
5328 gfc_warning (OPT_Wunused_dummy_argument
,
5329 "Derived-type dummy argument %qs at %L was "
5330 "declared INTENT(OUT) but was not set and "
5331 "does not have a default initializer",
5332 sym
->name
, &sym
->declared_at
);
5333 if (sym
->backend_decl
!= NULL_TREE
)
5334 TREE_NO_WARNING(sym
->backend_decl
) = 1;
5336 else if (warn_unused_dummy_argument
)
5338 gfc_warning (OPT_Wunused_dummy_argument
,
5339 "Unused dummy argument %qs at %L", sym
->name
,
5341 if (sym
->backend_decl
!= NULL_TREE
)
5342 TREE_NO_WARNING(sym
->backend_decl
) = 1;
5346 /* Warn for unused variables, but not if they're inside a common
5347 block or a namelist. */
5348 else if (warn_unused_variable
5349 && !(sym
->attr
.in_common
|| sym
->mark
|| sym
->attr
.in_namelist
))
5351 if (sym
->attr
.use_only
)
5353 gfc_warning (OPT_Wunused_variable
,
5354 "Unused module variable %qs which has been "
5355 "explicitly imported at %L", sym
->name
,
5357 if (sym
->backend_decl
!= NULL_TREE
)
5358 TREE_NO_WARNING(sym
->backend_decl
) = 1;
5360 else if (!sym
->attr
.use_assoc
)
5362 /* Corner case: the symbol may be an entry point. At this point,
5363 it may appear to be an unused variable. Suppress warning. */
5367 for (el
= sym
->ns
->entries
; el
; el
=el
->next
)
5368 if (strcmp(sym
->name
, el
->sym
->name
) == 0)
5372 gfc_warning (OPT_Wunused_variable
,
5373 "Unused variable %qs declared at %L",
5374 sym
->name
, &sym
->declared_at
);
5375 if (sym
->backend_decl
!= NULL_TREE
)
5376 TREE_NO_WARNING(sym
->backend_decl
) = 1;
5380 /* For variable length CHARACTER parameters, the PARM_DECL already
5381 references the length variable, so force gfc_get_symbol_decl
5382 even when not referenced. If optimize > 0, it will be optimized
5383 away anyway. But do this only after emitting -Wunused-parameter
5384 warning if requested. */
5385 if (sym
->attr
.dummy
&& !sym
->attr
.referenced
5386 && sym
->ts
.type
== BT_CHARACTER
5387 && sym
->ts
.u
.cl
->backend_decl
!= NULL
5388 && TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) == VAR_DECL
)
5390 sym
->attr
.referenced
= 1;
5391 gfc_get_symbol_decl (sym
);
5394 /* INTENT(out) dummy arguments and result variables with allocatable
5395 components are reset by default and need to be set referenced to
5396 generate the code for nullification and automatic lengths. */
5397 if (!sym
->attr
.referenced
5398 && sym
->ts
.type
== BT_DERIVED
5399 && sym
->ts
.u
.derived
->attr
.alloc_comp
5400 && !sym
->attr
.pointer
5401 && ((sym
->attr
.dummy
&& sym
->attr
.intent
== INTENT_OUT
)
5403 (sym
->attr
.result
&& sym
!= sym
->result
)))
5405 sym
->attr
.referenced
= 1;
5406 gfc_get_symbol_decl (sym
);
5409 /* Check for dependencies in the array specification and string
5410 length, adding the necessary declarations to the function. We
5411 mark the symbol now, as well as in traverse_ns, to prevent
5412 getting stuck in a circular dependency. */
5415 else if (sym
->attr
.flavor
== FL_PARAMETER
)
5417 if (warn_unused_parameter
5418 && !sym
->attr
.referenced
)
5420 if (!sym
->attr
.use_assoc
)
5421 gfc_warning (OPT_Wunused_parameter
,
5422 "Unused parameter %qs declared at %L", sym
->name
,
5424 else if (sym
->attr
.use_only
)
5425 gfc_warning (OPT_Wunused_parameter
,
5426 "Unused parameter %qs which has been explicitly "
5427 "imported at %L", sym
->name
, &sym
->declared_at
);
5432 && sym
->ns
->parent
->code
5433 && sym
->ns
->parent
->code
->op
== EXEC_BLOCK
)
5435 if (sym
->attr
.referenced
)
5436 gfc_get_symbol_decl (sym
);
5440 else if (sym
->attr
.flavor
== FL_PROCEDURE
)
5442 /* TODO: move to the appropriate place in resolve.c. */
5443 if (warn_return_type
5444 && sym
->attr
.function
5446 && sym
!= sym
->result
5447 && !sym
->result
->attr
.referenced
5448 && !sym
->attr
.use_assoc
5449 && sym
->attr
.if_source
!= IFSRC_IFBODY
)
5451 gfc_warning (OPT_Wreturn_type
,
5452 "Return value %qs of function %qs declared at "
5453 "%L not set", sym
->result
->name
, sym
->name
,
5454 &sym
->result
->declared_at
);
5456 /* Prevents "Unused variable" warning for RESULT variables. */
5457 sym
->result
->mark
= 1;
5461 if (sym
->attr
.dummy
== 1)
5463 /* Modify the tree type for scalar character dummy arguments of bind(c)
5464 procedures if they are passed by value. The tree type for them will
5465 be promoted to INTEGER_TYPE for the middle end, which appears to be
5466 what C would do with characters passed by-value. The value attribute
5467 implies the dummy is a scalar. */
5468 if (sym
->attr
.value
== 1 && sym
->backend_decl
!= NULL
5469 && sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.is_c_interop
5470 && sym
->ns
->proc_name
!= NULL
&& sym
->ns
->proc_name
->attr
.is_bind_c
)
5471 gfc_conv_scalar_char_value (sym
, NULL
, NULL
);
5473 /* Unused procedure passed as dummy argument. */
5474 if (sym
->attr
.flavor
== FL_PROCEDURE
)
5476 if (!sym
->attr
.referenced
)
5478 if (warn_unused_dummy_argument
)
5479 gfc_warning (OPT_Wunused_dummy_argument
,
5480 "Unused dummy argument %qs at %L", sym
->name
,
5484 /* Silence bogus "unused parameter" warnings from the
5486 if (sym
->backend_decl
!= NULL_TREE
)
5487 TREE_NO_WARNING (sym
->backend_decl
) = 1;
5491 /* Make sure we convert the types of the derived types from iso_c_binding
5493 if (sym
->attr
.flavor
!= FL_PROCEDURE
&& sym
->attr
.is_iso_c
5494 && sym
->ts
.type
== BT_DERIVED
)
5495 sym
->backend_decl
= gfc_typenode_for_spec (&(sym
->ts
));
5500 generate_local_nml_decl (gfc_symbol
* sym
)
5502 if (sym
->attr
.flavor
== FL_NAMELIST
&& !sym
->attr
.use_assoc
)
5504 tree decl
= generate_namelist_decl (sym
);
5511 generate_local_vars (gfc_namespace
* ns
)
5513 gfc_traverse_ns (ns
, generate_local_decl
);
5514 gfc_traverse_ns (ns
, generate_local_nml_decl
);
5518 /* Generate a switch statement to jump to the correct entry point. Also
5519 creates the label decls for the entry points. */
5522 gfc_trans_entry_master_switch (gfc_entry_list
* el
)
5529 gfc_init_block (&block
);
5530 for (; el
; el
= el
->next
)
5532 /* Add the case label. */
5533 label
= gfc_build_label_decl (NULL_TREE
);
5534 val
= build_int_cst (gfc_array_index_type
, el
->id
);
5535 tmp
= build_case_label (val
, NULL_TREE
, label
);
5536 gfc_add_expr_to_block (&block
, tmp
);
5538 /* And jump to the actual entry point. */
5539 label
= gfc_build_label_decl (NULL_TREE
);
5540 tmp
= build1_v (GOTO_EXPR
, label
);
5541 gfc_add_expr_to_block (&block
, tmp
);
5543 /* Save the label decl. */
5546 tmp
= gfc_finish_block (&block
);
5547 /* The first argument selects the entry point. */
5548 val
= DECL_ARGUMENTS (current_function_decl
);
5549 tmp
= fold_build3_loc (input_location
, SWITCH_EXPR
, NULL_TREE
,
5550 val
, tmp
, NULL_TREE
);
5555 /* Add code to string lengths of actual arguments passed to a function against
5556 the expected lengths of the dummy arguments. */
5559 add_argument_checking (stmtblock_t
*block
, gfc_symbol
*sym
)
5561 gfc_formal_arglist
*formal
;
5563 for (formal
= gfc_sym_get_dummy_args (sym
); formal
; formal
= formal
->next
)
5564 if (formal
->sym
&& formal
->sym
->ts
.type
== BT_CHARACTER
5565 && !formal
->sym
->ts
.deferred
)
5567 enum tree_code comparison
;
5572 const char *message
;
5578 gcc_assert (cl
->passed_length
!= NULL_TREE
);
5579 gcc_assert (cl
->backend_decl
!= NULL_TREE
);
5581 /* For POINTER, ALLOCATABLE and assumed-shape dummy arguments, the
5582 string lengths must match exactly. Otherwise, it is only required
5583 that the actual string length is *at least* the expected one.
5584 Sequence association allows for a mismatch of the string length
5585 if the actual argument is (part of) an array, but only if the
5586 dummy argument is an array. (See "Sequence association" in
5587 Section 12.4.1.4 for F95 and 12.4.1.5 for F2003.) */
5588 if (fsym
->attr
.pointer
|| fsym
->attr
.allocatable
5589 || (fsym
->as
&& (fsym
->as
->type
== AS_ASSUMED_SHAPE
5590 || fsym
->as
->type
== AS_ASSUMED_RANK
)))
5592 comparison
= NE_EXPR
;
5593 message
= _("Actual string length does not match the declared one"
5594 " for dummy argument '%s' (%ld/%ld)");
5596 else if (fsym
->as
&& fsym
->as
->rank
!= 0)
5600 comparison
= LT_EXPR
;
5601 message
= _("Actual string length is shorter than the declared one"
5602 " for dummy argument '%s' (%ld/%ld)");
5605 /* Build the condition. For optional arguments, an actual length
5606 of 0 is also acceptable if the associated string is NULL, which
5607 means the argument was not passed. */
5608 cond
= fold_build2_loc (input_location
, comparison
, boolean_type_node
,
5609 cl
->passed_length
, cl
->backend_decl
);
5610 if (fsym
->attr
.optional
)
5616 not_0length
= fold_build2_loc (input_location
, NE_EXPR
,
5619 build_zero_cst (gfc_charlen_type_node
));
5620 /* The symbol needs to be referenced for gfc_get_symbol_decl. */
5621 fsym
->attr
.referenced
= 1;
5622 not_absent
= gfc_conv_expr_present (fsym
);
5624 absent_failed
= fold_build2_loc (input_location
, TRUTH_OR_EXPR
,
5625 boolean_type_node
, not_0length
,
5628 cond
= fold_build2_loc (input_location
, TRUTH_AND_EXPR
,
5629 boolean_type_node
, cond
, absent_failed
);
5632 /* Build the runtime check. */
5633 argname
= gfc_build_cstring_const (fsym
->name
);
5634 argname
= gfc_build_addr_expr (pchar_type_node
, argname
);
5635 gfc_trans_runtime_check (true, false, cond
, block
, &fsym
->declared_at
,
5637 fold_convert (long_integer_type_node
,
5639 fold_convert (long_integer_type_node
,
5646 create_main_function (tree fndecl
)
5650 tree tmp
, decl
, result_decl
, argc
, argv
, typelist
, arglist
;
5653 old_context
= current_function_decl
;
5657 push_function_context ();
5658 saved_parent_function_decls
= saved_function_decls
;
5659 saved_function_decls
= NULL_TREE
;
5662 /* main() function must be declared with global scope. */
5663 gcc_assert (current_function_decl
== NULL_TREE
);
5665 /* Declare the function. */
5666 tmp
= build_function_type_list (integer_type_node
, integer_type_node
,
5667 build_pointer_type (pchar_type_node
),
5669 main_identifier_node
= get_identifier ("main");
5670 ftn_main
= build_decl (input_location
, FUNCTION_DECL
,
5671 main_identifier_node
, tmp
);
5672 DECL_EXTERNAL (ftn_main
) = 0;
5673 TREE_PUBLIC (ftn_main
) = 1;
5674 TREE_STATIC (ftn_main
) = 1;
5675 DECL_ATTRIBUTES (ftn_main
)
5676 = tree_cons (get_identifier("externally_visible"), NULL_TREE
, NULL_TREE
);
5678 /* Setup the result declaration (for "return 0"). */
5679 result_decl
= build_decl (input_location
,
5680 RESULT_DECL
, NULL_TREE
, integer_type_node
);
5681 DECL_ARTIFICIAL (result_decl
) = 1;
5682 DECL_IGNORED_P (result_decl
) = 1;
5683 DECL_CONTEXT (result_decl
) = ftn_main
;
5684 DECL_RESULT (ftn_main
) = result_decl
;
5686 pushdecl (ftn_main
);
5688 /* Get the arguments. */
5690 arglist
= NULL_TREE
;
5691 typelist
= TYPE_ARG_TYPES (TREE_TYPE (ftn_main
));
5693 tmp
= TREE_VALUE (typelist
);
5694 argc
= build_decl (input_location
, PARM_DECL
, get_identifier ("argc"), tmp
);
5695 DECL_CONTEXT (argc
) = ftn_main
;
5696 DECL_ARG_TYPE (argc
) = TREE_VALUE (typelist
);
5697 TREE_READONLY (argc
) = 1;
5698 gfc_finish_decl (argc
);
5699 arglist
= chainon (arglist
, argc
);
5701 typelist
= TREE_CHAIN (typelist
);
5702 tmp
= TREE_VALUE (typelist
);
5703 argv
= build_decl (input_location
, PARM_DECL
, get_identifier ("argv"), tmp
);
5704 DECL_CONTEXT (argv
) = ftn_main
;
5705 DECL_ARG_TYPE (argv
) = TREE_VALUE (typelist
);
5706 TREE_READONLY (argv
) = 1;
5707 DECL_BY_REFERENCE (argv
) = 1;
5708 gfc_finish_decl (argv
);
5709 arglist
= chainon (arglist
, argv
);
5711 DECL_ARGUMENTS (ftn_main
) = arglist
;
5712 current_function_decl
= ftn_main
;
5713 announce_function (ftn_main
);
5715 rest_of_decl_compilation (ftn_main
, 1, 0);
5716 make_decl_rtl (ftn_main
);
5717 allocate_struct_function (ftn_main
, false);
5720 gfc_init_block (&body
);
5722 /* Call some libgfortran initialization routines, call then MAIN__(). */
5724 /* Call _gfortran_caf_init (*argc, ***argv). */
5725 if (flag_coarray
== GFC_FCOARRAY_LIB
)
5727 tree pint_type
, pppchar_type
;
5728 pint_type
= build_pointer_type (integer_type_node
);
5730 = build_pointer_type (build_pointer_type (pchar_type_node
));
5732 tmp
= build_call_expr_loc (input_location
, gfor_fndecl_caf_init
, 2,
5733 gfc_build_addr_expr (pint_type
, argc
),
5734 gfc_build_addr_expr (pppchar_type
, argv
));
5735 gfc_add_expr_to_block (&body
, tmp
);
5738 /* Call _gfortran_set_args (argc, argv). */
5739 TREE_USED (argc
) = 1;
5740 TREE_USED (argv
) = 1;
5741 tmp
= build_call_expr_loc (input_location
,
5742 gfor_fndecl_set_args
, 2, argc
, argv
);
5743 gfc_add_expr_to_block (&body
, tmp
);
5745 /* Add a call to set_options to set up the runtime library Fortran
5746 language standard parameters. */
5748 tree array_type
, array
, var
;
5749 vec
<constructor_elt
, va_gc
> *v
= NULL
;
5751 /* Passing a new option to the library requires four modifications:
5752 + add it to the tree_cons list below
5753 + change the array size in the call to build_array_type
5754 + change the first argument to the library call
5755 gfor_fndecl_set_options
5756 + modify the library (runtime/compile_options.c)! */
5758 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5759 build_int_cst (integer_type_node
,
5760 gfc_option
.warn_std
));
5761 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5762 build_int_cst (integer_type_node
,
5763 gfc_option
.allow_std
));
5764 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5765 build_int_cst (integer_type_node
, pedantic
));
5766 /* TODO: This is the old -fdump-core option, which is unused but
5767 passed due to ABI compatibility; remove when bumping the
5769 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5770 build_int_cst (integer_type_node
,
5772 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5773 build_int_cst (integer_type_node
, flag_backtrace
));
5774 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5775 build_int_cst (integer_type_node
, flag_sign_zero
));
5776 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5777 build_int_cst (integer_type_node
,
5779 & GFC_RTCHECK_BOUNDS
)));
5780 /* TODO: This is the -frange-check option, which no longer affects
5781 library behavior; when bumping the library ABI this slot can be
5782 reused for something else. As it is the last element in the
5783 array, we can instead leave it out altogether. */
5784 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5785 build_int_cst (integer_type_node
, 0));
5786 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5787 build_int_cst (integer_type_node
,
5788 gfc_option
.fpe_summary
));
5790 array_type
= build_array_type (integer_type_node
,
5791 build_index_type (size_int (8)));
5792 array
= build_constructor (array_type
, v
);
5793 TREE_CONSTANT (array
) = 1;
5794 TREE_STATIC (array
) = 1;
5796 /* Create a static variable to hold the jump table. */
5797 var
= build_decl (input_location
, VAR_DECL
,
5798 create_tmp_var_name ("options"),
5800 DECL_ARTIFICIAL (var
) = 1;
5801 DECL_IGNORED_P (var
) = 1;
5802 TREE_CONSTANT (var
) = 1;
5803 TREE_STATIC (var
) = 1;
5804 TREE_READONLY (var
) = 1;
5805 DECL_INITIAL (var
) = array
;
5807 var
= gfc_build_addr_expr (build_pointer_type (integer_type_node
), var
);
5809 tmp
= build_call_expr_loc (input_location
,
5810 gfor_fndecl_set_options
, 2,
5811 build_int_cst (integer_type_node
, 9), var
);
5812 gfc_add_expr_to_block (&body
, tmp
);
5815 /* If -ffpe-trap option was provided, add a call to set_fpe so that
5816 the library will raise a FPE when needed. */
5817 if (gfc_option
.fpe
!= 0)
5819 tmp
= build_call_expr_loc (input_location
,
5820 gfor_fndecl_set_fpe
, 1,
5821 build_int_cst (integer_type_node
,
5823 gfc_add_expr_to_block (&body
, tmp
);
5826 /* If this is the main program and an -fconvert option was provided,
5827 add a call to set_convert. */
5829 if (flag_convert
!= GFC_FLAG_CONVERT_NATIVE
)
5831 tmp
= build_call_expr_loc (input_location
,
5832 gfor_fndecl_set_convert
, 1,
5833 build_int_cst (integer_type_node
, flag_convert
));
5834 gfc_add_expr_to_block (&body
, tmp
);
5837 /* If this is the main program and an -frecord-marker option was provided,
5838 add a call to set_record_marker. */
5840 if (flag_record_marker
!= 0)
5842 tmp
= build_call_expr_loc (input_location
,
5843 gfor_fndecl_set_record_marker
, 1,
5844 build_int_cst (integer_type_node
,
5845 flag_record_marker
));
5846 gfc_add_expr_to_block (&body
, tmp
);
5849 if (flag_max_subrecord_length
!= 0)
5851 tmp
= build_call_expr_loc (input_location
,
5852 gfor_fndecl_set_max_subrecord_length
, 1,
5853 build_int_cst (integer_type_node
,
5854 flag_max_subrecord_length
));
5855 gfc_add_expr_to_block (&body
, tmp
);
5858 /* Call MAIN__(). */
5859 tmp
= build_call_expr_loc (input_location
,
5861 gfc_add_expr_to_block (&body
, tmp
);
5863 /* Mark MAIN__ as used. */
5864 TREE_USED (fndecl
) = 1;
5866 /* Coarray: Call _gfortran_caf_finalize(void). */
5867 if (flag_coarray
== GFC_FCOARRAY_LIB
)
5869 tmp
= build_call_expr_loc (input_location
, gfor_fndecl_caf_finalize
, 0);
5870 gfc_add_expr_to_block (&body
, tmp
);
5874 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, integer_type_node
,
5875 DECL_RESULT (ftn_main
),
5876 build_int_cst (integer_type_node
, 0));
5877 tmp
= build1_v (RETURN_EXPR
, tmp
);
5878 gfc_add_expr_to_block (&body
, tmp
);
5881 DECL_SAVED_TREE (ftn_main
) = gfc_finish_block (&body
);
5884 /* Finish off this function and send it for code generation. */
5886 BLOCK_SUPERCONTEXT (DECL_INITIAL (ftn_main
)) = ftn_main
;
5888 DECL_SAVED_TREE (ftn_main
)
5889 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (ftn_main
),
5890 DECL_INITIAL (ftn_main
));
5892 /* Output the GENERIC tree. */
5893 dump_function (TDI_original
, ftn_main
);
5895 cgraph_node::finalize_function (ftn_main
, true);
5899 pop_function_context ();
5900 saved_function_decls
= saved_parent_function_decls
;
5902 current_function_decl
= old_context
;
5906 /* Get the result expression for a procedure. */
5909 get_proc_result (gfc_symbol
* sym
)
5911 if (sym
->attr
.subroutine
|| sym
== sym
->result
)
5913 if (current_fake_result_decl
!= NULL
)
5914 return TREE_VALUE (current_fake_result_decl
);
5919 return sym
->result
->backend_decl
;
5923 /* Generate an appropriate return-statement for a procedure. */
5926 gfc_generate_return (void)
5932 sym
= current_procedure_symbol
;
5933 fndecl
= sym
->backend_decl
;
5935 if (TREE_TYPE (DECL_RESULT (fndecl
)) == void_type_node
)
5939 result
= get_proc_result (sym
);
5941 /* Set the return value to the dummy result variable. The
5942 types may be different for scalar default REAL functions
5943 with -ff2c, therefore we have to convert. */
5944 if (result
!= NULL_TREE
)
5946 result
= convert (TREE_TYPE (DECL_RESULT (fndecl
)), result
);
5947 result
= fold_build2_loc (input_location
, MODIFY_EXPR
,
5948 TREE_TYPE (result
), DECL_RESULT (fndecl
),
5953 return build1_v (RETURN_EXPR
, result
);
5958 is_from_ieee_module (gfc_symbol
*sym
)
5960 if (sym
->from_intmod
== INTMOD_IEEE_FEATURES
5961 || sym
->from_intmod
== INTMOD_IEEE_EXCEPTIONS
5962 || sym
->from_intmod
== INTMOD_IEEE_ARITHMETIC
)
5963 seen_ieee_symbol
= 1;
5968 is_ieee_module_used (gfc_namespace
*ns
)
5970 seen_ieee_symbol
= 0;
5971 gfc_traverse_ns (ns
, is_from_ieee_module
);
5972 return seen_ieee_symbol
;
5976 static gfc_omp_clauses
*module_oacc_clauses
;
5980 add_clause (gfc_symbol
*sym
, gfc_omp_map_op map_op
)
5982 gfc_omp_namelist
*n
;
5984 n
= gfc_get_omp_namelist ();
5986 n
->u
.map_op
= map_op
;
5988 if (!module_oacc_clauses
)
5989 module_oacc_clauses
= gfc_get_omp_clauses ();
5991 if (module_oacc_clauses
->lists
[OMP_LIST_MAP
])
5992 n
->next
= module_oacc_clauses
->lists
[OMP_LIST_MAP
];
5994 module_oacc_clauses
->lists
[OMP_LIST_MAP
] = n
;
5999 find_module_oacc_declare_clauses (gfc_symbol
*sym
)
6001 if (sym
->attr
.use_assoc
)
6003 gfc_omp_map_op map_op
;
6005 if (sym
->attr
.oacc_declare_create
)
6006 map_op
= OMP_MAP_FORCE_ALLOC
;
6008 if (sym
->attr
.oacc_declare_copyin
)
6009 map_op
= OMP_MAP_FORCE_TO
;
6011 if (sym
->attr
.oacc_declare_deviceptr
)
6012 map_op
= OMP_MAP_FORCE_DEVICEPTR
;
6014 if (sym
->attr
.oacc_declare_device_resident
)
6015 map_op
= OMP_MAP_DEVICE_RESIDENT
;
6017 if (sym
->attr
.oacc_declare_create
6018 || sym
->attr
.oacc_declare_copyin
6019 || sym
->attr
.oacc_declare_deviceptr
6020 || sym
->attr
.oacc_declare_device_resident
)
6022 sym
->attr
.referenced
= 1;
6023 add_clause (sym
, map_op
);
6030 finish_oacc_declare (gfc_namespace
*ns
, gfc_symbol
*sym
, bool block
)
6033 gfc_oacc_declare
*oc
;
6034 locus where
= gfc_current_locus
;
6035 gfc_omp_clauses
*omp_clauses
= NULL
;
6036 gfc_omp_namelist
*n
, *p
;
6038 gfc_traverse_ns (ns
, find_module_oacc_declare_clauses
);
6040 if (module_oacc_clauses
&& sym
->attr
.flavor
== FL_PROGRAM
)
6042 gfc_oacc_declare
*new_oc
;
6044 new_oc
= gfc_get_oacc_declare ();
6045 new_oc
->next
= ns
->oacc_declare
;
6046 new_oc
->clauses
= module_oacc_clauses
;
6048 ns
->oacc_declare
= new_oc
;
6049 module_oacc_clauses
= NULL
;
6052 if (!ns
->oacc_declare
)
6055 for (oc
= ns
->oacc_declare
; oc
; oc
= oc
->next
)
6061 gfc_error ("Sorry, $!ACC DECLARE at %L is not allowed "
6062 "in BLOCK construct", &oc
->loc
);
6065 if (oc
->clauses
&& oc
->clauses
->lists
[OMP_LIST_MAP
])
6067 if (omp_clauses
== NULL
)
6069 omp_clauses
= oc
->clauses
;
6073 for (n
= oc
->clauses
->lists
[OMP_LIST_MAP
]; n
; p
= n
, n
= n
->next
)
6076 gcc_assert (p
->next
== NULL
);
6078 p
->next
= omp_clauses
->lists
[OMP_LIST_MAP
];
6079 omp_clauses
= oc
->clauses
;
6086 for (n
= omp_clauses
->lists
[OMP_LIST_MAP
]; n
; n
= n
->next
)
6088 switch (n
->u
.map_op
)
6090 case OMP_MAP_DEVICE_RESIDENT
:
6091 n
->u
.map_op
= OMP_MAP_FORCE_ALLOC
;
6099 code
= XCNEW (gfc_code
);
6100 code
->op
= EXEC_OACC_DECLARE
;
6103 code
->ext
.oacc_declare
= gfc_get_oacc_declare ();
6104 code
->ext
.oacc_declare
->clauses
= omp_clauses
;
6106 code
->block
= XCNEW (gfc_code
);
6107 code
->block
->op
= EXEC_OACC_DECLARE
;
6108 code
->block
->loc
= where
;
6111 code
->block
->next
= ns
->code
;
6119 /* Generate code for a function. */
6122 gfc_generate_function_code (gfc_namespace
* ns
)
6128 tree fpstate
= NULL_TREE
;
6129 stmtblock_t init
, cleanup
;
6131 gfc_wrapped_block try_block
;
6132 tree recurcheckvar
= NULL_TREE
;
6134 gfc_symbol
*previous_procedure_symbol
;
6138 sym
= ns
->proc_name
;
6139 previous_procedure_symbol
= current_procedure_symbol
;
6140 current_procedure_symbol
= sym
;
6142 /* Check that the frontend isn't still using this. */
6143 gcc_assert (sym
->tlink
== NULL
);
6146 /* Create the declaration for functions with global scope. */
6147 if (!sym
->backend_decl
)
6148 gfc_create_function_decl (ns
, false);
6150 fndecl
= sym
->backend_decl
;
6151 old_context
= current_function_decl
;
6155 push_function_context ();
6156 saved_parent_function_decls
= saved_function_decls
;
6157 saved_function_decls
= NULL_TREE
;
6160 trans_function_start (sym
);
6162 gfc_init_block (&init
);
6164 if (ns
->entries
&& ns
->proc_name
->ts
.type
== BT_CHARACTER
)
6166 /* Copy length backend_decls to all entry point result
6171 gfc_conv_const_charlen (ns
->proc_name
->ts
.u
.cl
);
6172 backend_decl
= ns
->proc_name
->result
->ts
.u
.cl
->backend_decl
;
6173 for (el
= ns
->entries
; el
; el
= el
->next
)
6174 el
->sym
->result
->ts
.u
.cl
->backend_decl
= backend_decl
;
6177 /* Translate COMMON blocks. */
6178 gfc_trans_common (ns
);
6180 /* Null the parent fake result declaration if this namespace is
6181 a module function or an external procedures. */
6182 if ((ns
->parent
&& ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
6183 || ns
->parent
== NULL
)
6184 parent_fake_result_decl
= NULL_TREE
;
6186 gfc_generate_contained_functions (ns
);
6188 nonlocal_dummy_decls
= NULL
;
6189 nonlocal_dummy_decl_pset
= NULL
;
6191 has_coarray_vars
= false;
6192 generate_local_vars (ns
);
6194 if (flag_coarray
== GFC_FCOARRAY_LIB
&& has_coarray_vars
)
6195 generate_coarray_init (ns
);
6197 /* Keep the parent fake result declaration in module functions
6198 or external procedures. */
6199 if ((ns
->parent
&& ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
6200 || ns
->parent
== NULL
)
6201 current_fake_result_decl
= parent_fake_result_decl
;
6203 current_fake_result_decl
= NULL_TREE
;
6205 is_recursive
= sym
->attr
.recursive
6206 || (sym
->attr
.entry_master
6207 && sym
->ns
->entries
->sym
->attr
.recursive
);
6208 if ((gfc_option
.rtcheck
& GFC_RTCHECK_RECURSION
)
6209 && !is_recursive
&& !flag_recursive
)
6213 msg
= xasprintf ("Recursive call to nonrecursive procedure '%s'",
6215 recurcheckvar
= gfc_create_var (boolean_type_node
, "is_recursive");
6216 TREE_STATIC (recurcheckvar
) = 1;
6217 DECL_INITIAL (recurcheckvar
) = boolean_false_node
;
6218 gfc_add_expr_to_block (&init
, recurcheckvar
);
6219 gfc_trans_runtime_check (true, false, recurcheckvar
, &init
,
6220 &sym
->declared_at
, msg
);
6221 gfc_add_modify (&init
, recurcheckvar
, boolean_true_node
);
6225 /* Check if an IEEE module is used in the procedure. If so, save
6226 the floating point state. */
6227 ieee
= is_ieee_module_used (ns
);
6229 fpstate
= gfc_save_fp_state (&init
);
6231 /* Now generate the code for the body of this function. */
6232 gfc_init_block (&body
);
6234 if (TREE_TYPE (DECL_RESULT (fndecl
)) != void_type_node
6235 && sym
->attr
.subroutine
)
6237 tree alternate_return
;
6238 alternate_return
= gfc_get_fake_result_decl (sym
, 0);
6239 gfc_add_modify (&body
, alternate_return
, integer_zero_node
);
6244 /* Jump to the correct entry point. */
6245 tmp
= gfc_trans_entry_master_switch (ns
->entries
);
6246 gfc_add_expr_to_block (&body
, tmp
);
6249 /* If bounds-checking is enabled, generate code to check passed in actual
6250 arguments against the expected dummy argument attributes (e.g. string
6252 if ((gfc_option
.rtcheck
& GFC_RTCHECK_BOUNDS
) && !sym
->attr
.is_bind_c
)
6253 add_argument_checking (&body
, sym
);
6255 finish_oacc_declare (ns
, sym
, false);
6257 tmp
= gfc_trans_code (ns
->code
);
6258 gfc_add_expr_to_block (&body
, tmp
);
6260 if (TREE_TYPE (DECL_RESULT (fndecl
)) != void_type_node
6261 || (sym
->result
&& sym
->result
!= sym
6262 && sym
->result
->ts
.type
== BT_DERIVED
6263 && sym
->result
->ts
.u
.derived
->attr
.alloc_comp
))
6265 bool artificial_result_decl
= false;
6266 tree result
= get_proc_result (sym
);
6267 gfc_symbol
*rsym
= sym
== sym
->result
? sym
: sym
->result
;
6269 /* Make sure that a function returning an object with
6270 alloc/pointer_components always has a result, where at least
6271 the allocatable/pointer components are set to zero. */
6272 if (result
== NULL_TREE
&& sym
->attr
.function
6273 && ((sym
->result
->ts
.type
== BT_DERIVED
6274 && (sym
->attr
.allocatable
6275 || sym
->attr
.pointer
6276 || sym
->result
->ts
.u
.derived
->attr
.alloc_comp
6277 || sym
->result
->ts
.u
.derived
->attr
.pointer_comp
))
6278 || (sym
->result
->ts
.type
== BT_CLASS
6279 && (CLASS_DATA (sym
)->attr
.allocatable
6280 || CLASS_DATA (sym
)->attr
.class_pointer
6281 || CLASS_DATA (sym
->result
)->attr
.alloc_comp
6282 || CLASS_DATA (sym
->result
)->attr
.pointer_comp
))))
6284 artificial_result_decl
= true;
6285 result
= gfc_get_fake_result_decl (sym
, 0);
6288 if (result
!= NULL_TREE
&& sym
->attr
.function
&& !sym
->attr
.pointer
)
6290 if (sym
->attr
.allocatable
&& sym
->attr
.dimension
== 0
6291 && sym
->result
== sym
)
6292 gfc_add_modify (&init
, result
, fold_convert (TREE_TYPE (result
),
6293 null_pointer_node
));
6294 else if (sym
->ts
.type
== BT_CLASS
6295 && CLASS_DATA (sym
)->attr
.allocatable
6296 && CLASS_DATA (sym
)->attr
.dimension
== 0
6297 && sym
->result
== sym
)
6299 tmp
= CLASS_DATA (sym
)->backend_decl
;
6300 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
,
6301 TREE_TYPE (tmp
), result
, tmp
, NULL_TREE
);
6302 gfc_add_modify (&init
, tmp
, fold_convert (TREE_TYPE (tmp
),
6303 null_pointer_node
));
6305 else if (sym
->ts
.type
== BT_DERIVED
6306 && !sym
->attr
.allocatable
)
6309 /* Arrays are not initialized using the default initializer of
6310 their elements. Therefore only check if a default
6311 initializer is available when the result is scalar. */
6312 init_exp
= rsym
->as
? NULL
6313 : gfc_generate_initializer (&rsym
->ts
, true);
6316 tmp
= gfc_trans_structure_assign (result
, init_exp
, 0);
6317 gfc_free_expr (init_exp
);
6318 gfc_add_expr_to_block (&init
, tmp
);
6320 else if (rsym
->ts
.u
.derived
->attr
.alloc_comp
)
6322 rank
= rsym
->as
? rsym
->as
->rank
: 0;
6323 tmp
= gfc_nullify_alloc_comp (rsym
->ts
.u
.derived
, result
,
6325 gfc_prepend_expr_to_block (&body
, tmp
);
6330 if (result
== NULL_TREE
|| artificial_result_decl
)
6332 /* TODO: move to the appropriate place in resolve.c. */
6333 if (warn_return_type
&& sym
== sym
->result
)
6334 gfc_warning (OPT_Wreturn_type
,
6335 "Return value of function %qs at %L not set",
6336 sym
->name
, &sym
->declared_at
);
6337 if (warn_return_type
)
6338 TREE_NO_WARNING(sym
->backend_decl
) = 1;
6340 if (result
!= NULL_TREE
)
6341 gfc_add_expr_to_block (&body
, gfc_generate_return ());
6344 gfc_init_block (&cleanup
);
6346 /* Reset recursion-check variable. */
6347 if ((gfc_option
.rtcheck
& GFC_RTCHECK_RECURSION
)
6348 && !is_recursive
&& !flag_openmp
&& recurcheckvar
!= NULL_TREE
)
6350 gfc_add_modify (&cleanup
, recurcheckvar
, boolean_false_node
);
6351 recurcheckvar
= NULL
;
6354 /* If IEEE modules are loaded, restore the floating-point state. */
6356 gfc_restore_fp_state (&cleanup
, fpstate
);
6358 /* Finish the function body and add init and cleanup code. */
6359 tmp
= gfc_finish_block (&body
);
6360 gfc_start_wrapped_block (&try_block
, tmp
);
6361 /* Add code to create and cleanup arrays. */
6362 gfc_trans_deferred_vars (sym
, &try_block
);
6363 gfc_add_init_cleanup (&try_block
, gfc_finish_block (&init
),
6364 gfc_finish_block (&cleanup
));
6366 /* Add all the decls we created during processing. */
6367 decl
= nreverse (saved_function_decls
);
6372 next
= DECL_CHAIN (decl
);
6373 DECL_CHAIN (decl
) = NULL_TREE
;
6377 saved_function_decls
= NULL_TREE
;
6379 DECL_SAVED_TREE (fndecl
) = gfc_finish_wrapped_block (&try_block
);
6382 /* Finish off this function and send it for code generation. */
6384 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
6386 DECL_SAVED_TREE (fndecl
)
6387 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (fndecl
),
6388 DECL_INITIAL (fndecl
));
6390 if (nonlocal_dummy_decls
)
6392 BLOCK_VARS (DECL_INITIAL (fndecl
))
6393 = chainon (BLOCK_VARS (DECL_INITIAL (fndecl
)), nonlocal_dummy_decls
);
6394 delete nonlocal_dummy_decl_pset
;
6395 nonlocal_dummy_decls
= NULL
;
6396 nonlocal_dummy_decl_pset
= NULL
;
6399 /* Output the GENERIC tree. */
6400 dump_function (TDI_original
, fndecl
);
6402 /* Store the end of the function, so that we get good line number
6403 info for the epilogue. */
6404 cfun
->function_end_locus
= input_location
;
6406 /* We're leaving the context of this function, so zap cfun.
6407 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6408 tree_rest_of_compilation. */
6413 pop_function_context ();
6414 saved_function_decls
= saved_parent_function_decls
;
6416 current_function_decl
= old_context
;
6418 if (decl_function_context (fndecl
))
6420 /* Register this function with cgraph just far enough to get it
6421 added to our parent's nested function list.
6422 If there are static coarrays in this function, the nested _caf_init
6423 function has already called cgraph_create_node, which also created
6424 the cgraph node for this function. */
6425 if (!has_coarray_vars
|| flag_coarray
!= GFC_FCOARRAY_LIB
)
6426 (void) cgraph_node::get_create (fndecl
);
6429 cgraph_node::finalize_function (fndecl
, true);
6431 gfc_trans_use_stmts (ns
);
6432 gfc_traverse_ns (ns
, gfc_emit_parameter_debug_info
);
6434 if (sym
->attr
.is_main_program
)
6435 create_main_function (fndecl
);
6437 current_procedure_symbol
= previous_procedure_symbol
;
6442 gfc_generate_constructors (void)
6444 gcc_assert (gfc_static_ctors
== NULL_TREE
);
6452 if (gfc_static_ctors
== NULL_TREE
)
6455 fnname
= get_file_function_name ("I");
6456 type
= build_function_type_list (void_type_node
, NULL_TREE
);
6458 fndecl
= build_decl (input_location
,
6459 FUNCTION_DECL
, fnname
, type
);
6460 TREE_PUBLIC (fndecl
) = 1;
6462 decl
= build_decl (input_location
,
6463 RESULT_DECL
, NULL_TREE
, void_type_node
);
6464 DECL_ARTIFICIAL (decl
) = 1;
6465 DECL_IGNORED_P (decl
) = 1;
6466 DECL_CONTEXT (decl
) = fndecl
;
6467 DECL_RESULT (fndecl
) = decl
;
6471 current_function_decl
= fndecl
;
6473 rest_of_decl_compilation (fndecl
, 1, 0);
6475 make_decl_rtl (fndecl
);
6477 allocate_struct_function (fndecl
, false);
6481 for (; gfc_static_ctors
; gfc_static_ctors
= TREE_CHAIN (gfc_static_ctors
))
6483 tmp
= build_call_expr_loc (input_location
,
6484 TREE_VALUE (gfc_static_ctors
), 0);
6485 DECL_SAVED_TREE (fndecl
) = build_stmt (input_location
, EXPR_STMT
, tmp
);
6491 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
6492 DECL_SAVED_TREE (fndecl
)
6493 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (fndecl
),
6494 DECL_INITIAL (fndecl
));
6496 free_after_parsing (cfun
);
6497 free_after_compilation (cfun
);
6499 tree_rest_of_compilation (fndecl
);
6501 current_function_decl
= NULL_TREE
;
6505 /* Translates a BLOCK DATA program unit. This means emitting the
6506 commons contained therein plus their initializations. We also emit
6507 a globally visible symbol to make sure that each BLOCK DATA program
6508 unit remains unique. */
6511 gfc_generate_block_data (gfc_namespace
* ns
)
6516 /* Tell the backend the source location of the block data. */
6518 gfc_set_backend_locus (&ns
->proc_name
->declared_at
);
6520 gfc_set_backend_locus (&gfc_current_locus
);
6522 /* Process the DATA statements. */
6523 gfc_trans_common (ns
);
6525 /* Create a global symbol with the mane of the block data. This is to
6526 generate linker errors if the same name is used twice. It is never
6529 id
= gfc_sym_mangled_function_id (ns
->proc_name
);
6531 id
= get_identifier ("__BLOCK_DATA__");
6533 decl
= build_decl (input_location
,
6534 VAR_DECL
, id
, gfc_array_index_type
);
6535 TREE_PUBLIC (decl
) = 1;
6536 TREE_STATIC (decl
) = 1;
6537 DECL_IGNORED_P (decl
) = 1;
6540 rest_of_decl_compilation (decl
, 1, 0);
6544 /* Process the local variables of a BLOCK construct. */
6547 gfc_process_block_locals (gfc_namespace
* ns
)
6551 gcc_assert (saved_local_decls
== NULL_TREE
);
6552 has_coarray_vars
= false;
6554 generate_local_vars (ns
);
6556 if (flag_coarray
== GFC_FCOARRAY_LIB
&& has_coarray_vars
)
6557 generate_coarray_init (ns
);
6559 decl
= nreverse (saved_local_decls
);
6564 next
= DECL_CHAIN (decl
);
6565 DECL_CHAIN (decl
) = NULL_TREE
;
6569 saved_local_decls
= NULL_TREE
;
6573 #include "gt-fortran-trans-decl.h"