1 /* Backend function setup
2 Copyright (C) 2002-2015 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"
31 #include "double-int.h"
38 #include "fold-const.h"
39 #include "stringpool.h"
40 #include "stor-layout.h"
43 #include "tree-dump.h"
44 #include "gimple-expr.h" /* For create_tmp_var_raw. */
46 #include "diagnostic-core.h" /* For internal_error. */
47 #include "toplev.h" /* For announce_function. */
49 #include "hard-reg-set.h"
55 #include "plugin-api.h"
59 #include "constructor.h"
61 #include "trans-types.h"
62 #include "trans-array.h"
63 #include "trans-const.h"
64 /* Only for gfc_trans_code. Shouldn't need to include this. */
65 #include "trans-stmt.h"
67 #define MAX_LABEL_VALUE 99999
70 /* Holds the result of the function if no result variable specified. */
72 static GTY(()) tree current_fake_result_decl
;
73 static GTY(()) tree parent_fake_result_decl
;
76 /* Holds the variable DECLs for the current function. */
78 static GTY(()) tree saved_function_decls
;
79 static GTY(()) tree saved_parent_function_decls
;
81 static hash_set
<tree
> *nonlocal_dummy_decl_pset
;
82 static GTY(()) tree nonlocal_dummy_decls
;
84 /* Holds the variable DECLs that are locals. */
86 static GTY(()) tree saved_local_decls
;
88 /* The namespace of the module we're currently generating. Only used while
89 outputting decls for module variables. Do not rely on this being set. */
91 static gfc_namespace
*module_namespace
;
93 /* The currently processed procedure symbol. */
94 static gfc_symbol
* current_procedure_symbol
= NULL
;
96 /* The currently processed module. */
97 static struct module_htab_entry
*cur_module
;
99 /* With -fcoarray=lib: For generating the registering call
100 of static coarrays. */
101 static bool has_coarray_vars
;
102 static stmtblock_t caf_init_block
;
105 /* List of static constructor functions. */
107 tree gfc_static_ctors
;
110 /* Whether we've seen a symbol from an IEEE module in the namespace. */
111 static int seen_ieee_symbol
;
113 /* Function declarations for builtin library functions. */
115 tree gfor_fndecl_pause_numeric
;
116 tree gfor_fndecl_pause_string
;
117 tree gfor_fndecl_stop_numeric
;
118 tree gfor_fndecl_stop_numeric_f08
;
119 tree gfor_fndecl_stop_string
;
120 tree gfor_fndecl_error_stop_numeric
;
121 tree gfor_fndecl_error_stop_string
;
122 tree gfor_fndecl_runtime_error
;
123 tree gfor_fndecl_runtime_error_at
;
124 tree gfor_fndecl_runtime_warning_at
;
125 tree gfor_fndecl_os_error
;
126 tree gfor_fndecl_generate_error
;
127 tree gfor_fndecl_set_args
;
128 tree gfor_fndecl_set_fpe
;
129 tree gfor_fndecl_set_options
;
130 tree gfor_fndecl_set_convert
;
131 tree gfor_fndecl_set_record_marker
;
132 tree gfor_fndecl_set_max_subrecord_length
;
133 tree gfor_fndecl_ctime
;
134 tree gfor_fndecl_fdate
;
135 tree gfor_fndecl_ttynam
;
136 tree gfor_fndecl_in_pack
;
137 tree gfor_fndecl_in_unpack
;
138 tree gfor_fndecl_associated
;
139 tree gfor_fndecl_system_clock4
;
140 tree gfor_fndecl_system_clock8
;
141 tree gfor_fndecl_ieee_procedure_entry
;
142 tree gfor_fndecl_ieee_procedure_exit
;
145 /* Coarray run-time library function decls. */
146 tree gfor_fndecl_caf_init
;
147 tree gfor_fndecl_caf_finalize
;
148 tree gfor_fndecl_caf_this_image
;
149 tree gfor_fndecl_caf_num_images
;
150 tree gfor_fndecl_caf_register
;
151 tree gfor_fndecl_caf_deregister
;
152 tree gfor_fndecl_caf_get
;
153 tree gfor_fndecl_caf_send
;
154 tree gfor_fndecl_caf_sendget
;
155 tree gfor_fndecl_caf_sync_all
;
156 tree gfor_fndecl_caf_sync_images
;
157 tree gfor_fndecl_caf_error_stop
;
158 tree gfor_fndecl_caf_error_stop_str
;
159 tree gfor_fndecl_caf_atomic_def
;
160 tree gfor_fndecl_caf_atomic_ref
;
161 tree gfor_fndecl_caf_atomic_cas
;
162 tree gfor_fndecl_caf_atomic_op
;
163 tree gfor_fndecl_caf_lock
;
164 tree gfor_fndecl_caf_unlock
;
165 tree gfor_fndecl_co_broadcast
;
166 tree gfor_fndecl_co_max
;
167 tree gfor_fndecl_co_min
;
168 tree gfor_fndecl_co_reduce
;
169 tree gfor_fndecl_co_sum
;
172 /* Math functions. Many other math functions are handled in
173 trans-intrinsic.c. */
175 gfc_powdecl_list gfor_fndecl_math_powi
[4][3];
176 tree gfor_fndecl_math_ishftc4
;
177 tree gfor_fndecl_math_ishftc8
;
178 tree gfor_fndecl_math_ishftc16
;
181 /* String functions. */
183 tree gfor_fndecl_compare_string
;
184 tree gfor_fndecl_concat_string
;
185 tree gfor_fndecl_string_len_trim
;
186 tree gfor_fndecl_string_index
;
187 tree gfor_fndecl_string_scan
;
188 tree gfor_fndecl_string_verify
;
189 tree gfor_fndecl_string_trim
;
190 tree gfor_fndecl_string_minmax
;
191 tree gfor_fndecl_adjustl
;
192 tree gfor_fndecl_adjustr
;
193 tree gfor_fndecl_select_string
;
194 tree gfor_fndecl_compare_string_char4
;
195 tree gfor_fndecl_concat_string_char4
;
196 tree gfor_fndecl_string_len_trim_char4
;
197 tree gfor_fndecl_string_index_char4
;
198 tree gfor_fndecl_string_scan_char4
;
199 tree gfor_fndecl_string_verify_char4
;
200 tree gfor_fndecl_string_trim_char4
;
201 tree gfor_fndecl_string_minmax_char4
;
202 tree gfor_fndecl_adjustl_char4
;
203 tree gfor_fndecl_adjustr_char4
;
204 tree gfor_fndecl_select_string_char4
;
207 /* Conversion between character kinds. */
208 tree gfor_fndecl_convert_char1_to_char4
;
209 tree gfor_fndecl_convert_char4_to_char1
;
212 /* Other misc. runtime library functions. */
213 tree gfor_fndecl_size0
;
214 tree gfor_fndecl_size1
;
215 tree gfor_fndecl_iargc
;
217 /* Intrinsic functions implemented in Fortran. */
218 tree gfor_fndecl_sc_kind
;
219 tree gfor_fndecl_si_kind
;
220 tree gfor_fndecl_sr_kind
;
222 /* BLAS gemm functions. */
223 tree gfor_fndecl_sgemm
;
224 tree gfor_fndecl_dgemm
;
225 tree gfor_fndecl_cgemm
;
226 tree gfor_fndecl_zgemm
;
230 gfc_add_decl_to_parent_function (tree decl
)
233 DECL_CONTEXT (decl
) = DECL_CONTEXT (current_function_decl
);
234 DECL_NONLOCAL (decl
) = 1;
235 DECL_CHAIN (decl
) = saved_parent_function_decls
;
236 saved_parent_function_decls
= decl
;
240 gfc_add_decl_to_function (tree decl
)
243 TREE_USED (decl
) = 1;
244 DECL_CONTEXT (decl
) = current_function_decl
;
245 DECL_CHAIN (decl
) = saved_function_decls
;
246 saved_function_decls
= decl
;
250 add_decl_as_local (tree decl
)
253 TREE_USED (decl
) = 1;
254 DECL_CONTEXT (decl
) = current_function_decl
;
255 DECL_CHAIN (decl
) = saved_local_decls
;
256 saved_local_decls
= decl
;
260 /* Build a backend label declaration. Set TREE_USED for named labels.
261 The context of the label is always the current_function_decl. All
262 labels are marked artificial. */
265 gfc_build_label_decl (tree label_id
)
267 /* 2^32 temporaries should be enough. */
268 static unsigned int tmp_num
= 1;
272 if (label_id
== NULL_TREE
)
274 /* Build an internal label name. */
275 ASM_FORMAT_PRIVATE_NAME (label_name
, "L", tmp_num
++);
276 label_id
= get_identifier (label_name
);
281 /* Build the LABEL_DECL node. Labels have no type. */
282 label_decl
= build_decl (input_location
,
283 LABEL_DECL
, label_id
, void_type_node
);
284 DECL_CONTEXT (label_decl
) = current_function_decl
;
285 DECL_MODE (label_decl
) = VOIDmode
;
287 /* We always define the label as used, even if the original source
288 file never references the label. We don't want all kinds of
289 spurious warnings for old-style Fortran code with too many
291 TREE_USED (label_decl
) = 1;
293 DECL_ARTIFICIAL (label_decl
) = 1;
298 /* Set the backend source location of a decl. */
301 gfc_set_decl_location (tree decl
, locus
* loc
)
303 DECL_SOURCE_LOCATION (decl
) = loc
->lb
->location
;
307 /* Return the backend label declaration for a given label structure,
308 or create it if it doesn't exist yet. */
311 gfc_get_label_decl (gfc_st_label
* lp
)
313 if (lp
->backend_decl
)
314 return lp
->backend_decl
;
317 char label_name
[GFC_MAX_SYMBOL_LEN
+ 1];
320 /* Validate the label declaration from the front end. */
321 gcc_assert (lp
!= NULL
&& lp
->value
<= MAX_LABEL_VALUE
);
323 /* Build a mangled name for the label. */
324 sprintf (label_name
, "__label_%.6d", lp
->value
);
326 /* Build the LABEL_DECL node. */
327 label_decl
= gfc_build_label_decl (get_identifier (label_name
));
329 /* Tell the debugger where the label came from. */
330 if (lp
->value
<= MAX_LABEL_VALUE
) /* An internal label. */
331 gfc_set_decl_location (label_decl
, &lp
->where
);
333 DECL_ARTIFICIAL (label_decl
) = 1;
335 /* Store the label in the label list and return the LABEL_DECL. */
336 lp
->backend_decl
= label_decl
;
342 /* Convert a gfc_symbol to an identifier of the same name. */
345 gfc_sym_identifier (gfc_symbol
* sym
)
347 if (sym
->attr
.is_main_program
&& strcmp (sym
->name
, "main") == 0)
348 return (get_identifier ("MAIN__"));
350 return (get_identifier (sym
->name
));
354 /* Construct mangled name from symbol name. */
357 gfc_sym_mangled_identifier (gfc_symbol
* sym
)
359 char name
[GFC_MAX_MANGLED_SYMBOL_LEN
+ 1];
361 /* Prevent the mangling of identifiers that have an assigned
362 binding label (mainly those that are bind(c)). */
363 if (sym
->attr
.is_bind_c
== 1 && sym
->binding_label
)
364 return get_identifier (sym
->binding_label
);
366 if (sym
->module
== NULL
)
367 return gfc_sym_identifier (sym
);
370 snprintf (name
, sizeof name
, "__%s_MOD_%s", sym
->module
, sym
->name
);
371 return get_identifier (name
);
376 /* Construct mangled function name from symbol name. */
379 gfc_sym_mangled_function_id (gfc_symbol
* sym
)
382 char name
[GFC_MAX_MANGLED_SYMBOL_LEN
+ 1];
384 /* It may be possible to simply use the binding label if it's
385 provided, and remove the other checks. Then we could use it
386 for other things if we wished. */
387 if ((sym
->attr
.is_bind_c
== 1 || sym
->attr
.is_iso_c
== 1) &&
389 /* use the binding label rather than the mangled name */
390 return get_identifier (sym
->binding_label
);
392 if (sym
->module
== NULL
|| sym
->attr
.proc
== PROC_EXTERNAL
393 || (sym
->module
!= NULL
&& (sym
->attr
.external
394 || sym
->attr
.if_source
== IFSRC_IFBODY
)))
396 /* Main program is mangled into MAIN__. */
397 if (sym
->attr
.is_main_program
)
398 return get_identifier ("MAIN__");
400 /* Intrinsic procedures are never mangled. */
401 if (sym
->attr
.proc
== PROC_INTRINSIC
)
402 return get_identifier (sym
->name
);
404 if (flag_underscoring
)
406 has_underscore
= strchr (sym
->name
, '_') != 0;
407 if (flag_second_underscore
&& has_underscore
)
408 snprintf (name
, sizeof name
, "%s__", sym
->name
);
410 snprintf (name
, sizeof name
, "%s_", sym
->name
);
411 return get_identifier (name
);
414 return get_identifier (sym
->name
);
418 snprintf (name
, sizeof name
, "__%s_MOD_%s", sym
->module
, sym
->name
);
419 return get_identifier (name
);
425 gfc_set_decl_assembler_name (tree decl
, tree name
)
427 tree target_mangled
= targetm
.mangle_decl_assembler_name (decl
, name
);
428 SET_DECL_ASSEMBLER_NAME (decl
, target_mangled
);
432 /* Returns true if a variable of specified size should go on the stack. */
435 gfc_can_put_var_on_stack (tree size
)
437 unsigned HOST_WIDE_INT low
;
439 if (!INTEGER_CST_P (size
))
442 if (flag_max_stack_var_size
< 0)
445 if (!tree_fits_uhwi_p (size
))
448 low
= TREE_INT_CST_LOW (size
);
449 if (low
> (unsigned HOST_WIDE_INT
) flag_max_stack_var_size
)
452 /* TODO: Set a per-function stack size limit. */
458 /* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
459 an expression involving its corresponding pointer. There are
460 2 cases; one for variable size arrays, and one for everything else,
461 because variable-sized arrays require one fewer level of
465 gfc_finish_cray_pointee (tree decl
, gfc_symbol
*sym
)
467 tree ptr_decl
= gfc_get_symbol_decl (sym
->cp_pointer
);
470 /* Parameters need to be dereferenced. */
471 if (sym
->cp_pointer
->attr
.dummy
)
472 ptr_decl
= build_fold_indirect_ref_loc (input_location
,
475 /* Check to see if we're dealing with a variable-sized array. */
476 if (sym
->attr
.dimension
477 && TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
)
479 /* These decls will be dereferenced later, so we don't dereference
481 value
= convert (TREE_TYPE (decl
), ptr_decl
);
485 ptr_decl
= convert (build_pointer_type (TREE_TYPE (decl
)),
487 value
= build_fold_indirect_ref_loc (input_location
,
491 SET_DECL_VALUE_EXPR (decl
, value
);
492 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
493 GFC_DECL_CRAY_POINTEE (decl
) = 1;
497 /* Finish processing of a declaration without an initial value. */
500 gfc_finish_decl (tree decl
)
502 gcc_assert (TREE_CODE (decl
) == PARM_DECL
503 || DECL_INITIAL (decl
) == NULL_TREE
);
505 if (TREE_CODE (decl
) != VAR_DECL
)
508 if (DECL_SIZE (decl
) == NULL_TREE
509 && TYPE_SIZE (TREE_TYPE (decl
)) != NULL_TREE
)
510 layout_decl (decl
, 0);
512 /* A few consistency checks. */
513 /* A static variable with an incomplete type is an error if it is
514 initialized. Also if it is not file scope. Otherwise, let it
515 through, but if it is not `extern' then it may cause an error
517 /* An automatic variable with an incomplete type is an error. */
519 /* We should know the storage size. */
520 gcc_assert (DECL_SIZE (decl
) != NULL_TREE
521 || (TREE_STATIC (decl
)
522 ? (!DECL_INITIAL (decl
) || !DECL_CONTEXT (decl
))
523 : DECL_EXTERNAL (decl
)));
525 /* The storage size should be constant. */
526 gcc_assert ((!DECL_EXTERNAL (decl
) && !TREE_STATIC (decl
))
528 || TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
);
532 /* Handle setting of GFC_DECL_SCALAR* on DECL. */
535 gfc_finish_decl_attrs (tree decl
, symbol_attribute
*attr
)
537 if (!attr
->dimension
&& !attr
->codimension
)
539 /* Handle scalar allocatable variables. */
540 if (attr
->allocatable
)
542 gfc_allocate_lang_decl (decl
);
543 GFC_DECL_SCALAR_ALLOCATABLE (decl
) = 1;
545 /* Handle scalar pointer variables. */
548 gfc_allocate_lang_decl (decl
);
549 GFC_DECL_SCALAR_POINTER (decl
) = 1;
555 /* Apply symbol attributes to a variable, and add it to the function scope. */
558 gfc_finish_var_decl (tree decl
, gfc_symbol
* sym
)
561 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
562 This is the equivalent of the TARGET variables.
563 We also need to set this if the variable is passed by reference in a
566 /* Set DECL_VALUE_EXPR for Cray Pointees. */
567 if (sym
->attr
.cray_pointee
)
568 gfc_finish_cray_pointee (decl
, sym
);
570 if (sym
->attr
.target
)
571 TREE_ADDRESSABLE (decl
) = 1;
572 /* If it wasn't used we wouldn't be getting it. */
573 TREE_USED (decl
) = 1;
575 if (sym
->attr
.flavor
== FL_PARAMETER
576 && (sym
->attr
.dimension
|| sym
->ts
.type
== BT_DERIVED
))
577 TREE_READONLY (decl
) = 1;
579 /* Chain this decl to the pending declarations. Don't do pushdecl()
580 because this would add them to the current scope rather than the
582 if (current_function_decl
!= NULL_TREE
)
584 if (sym
->ns
->proc_name
->backend_decl
== current_function_decl
585 || sym
->result
== sym
)
586 gfc_add_decl_to_function (decl
);
587 else if (sym
->ns
->proc_name
->attr
.flavor
== FL_LABEL
)
588 /* This is a BLOCK construct. */
589 add_decl_as_local (decl
);
591 gfc_add_decl_to_parent_function (decl
);
594 if (sym
->attr
.cray_pointee
)
597 if(sym
->attr
.is_bind_c
== 1 && sym
->binding_label
)
599 /* We need to put variables that are bind(c) into the common
600 segment of the object file, because this is what C would do.
601 gfortran would typically put them in either the BSS or
602 initialized data segments, and only mark them as common if
603 they were part of common blocks. However, if they are not put
604 into common space, then C cannot initialize global Fortran
605 variables that it interoperates with and the draft says that
606 either Fortran or C should be able to initialize it (but not
607 both, of course.) (J3/04-007, section 15.3). */
608 TREE_PUBLIC(decl
) = 1;
609 DECL_COMMON(decl
) = 1;
612 /* If a variable is USE associated, it's always external. */
613 if (sym
->attr
.use_assoc
)
615 DECL_EXTERNAL (decl
) = 1;
616 TREE_PUBLIC (decl
) = 1;
618 else if (sym
->module
&& !sym
->attr
.result
&& !sym
->attr
.dummy
)
620 /* TODO: Don't set sym->module for result or dummy variables. */
621 gcc_assert (current_function_decl
== NULL_TREE
|| sym
->result
== sym
);
623 if (sym
->attr
.access
!= ACCESS_PRIVATE
|| sym
->attr
.public_used
)
624 TREE_PUBLIC (decl
) = 1;
625 TREE_STATIC (decl
) = 1;
628 /* Derived types are a bit peculiar because of the possibility of
629 a default initializer; this must be applied each time the variable
630 comes into scope it therefore need not be static. These variables
631 are SAVE_NONE but have an initializer. Otherwise explicitly
632 initialized variables are SAVE_IMPLICIT and explicitly saved are
634 if (!sym
->attr
.use_assoc
635 && (sym
->attr
.save
!= SAVE_NONE
|| sym
->attr
.data
636 || (sym
->value
&& sym
->ns
->proc_name
->attr
.is_main_program
)
637 || (flag_coarray
== GFC_FCOARRAY_LIB
638 && sym
->attr
.codimension
&& !sym
->attr
.allocatable
)))
639 TREE_STATIC (decl
) = 1;
641 if (sym
->attr
.volatile_
)
643 TREE_THIS_VOLATILE (decl
) = 1;
644 TREE_SIDE_EFFECTS (decl
) = 1;
645 new_type
= build_qualified_type (TREE_TYPE (decl
), TYPE_QUAL_VOLATILE
);
646 TREE_TYPE (decl
) = new_type
;
649 /* Keep variables larger than max-stack-var-size off stack. */
650 if (!sym
->ns
->proc_name
->attr
.recursive
651 && INTEGER_CST_P (DECL_SIZE_UNIT (decl
))
652 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl
))
653 /* Put variable length auto array pointers always into stack. */
654 && (TREE_CODE (TREE_TYPE (decl
)) != POINTER_TYPE
655 || sym
->attr
.dimension
== 0
656 || sym
->as
->type
!= AS_EXPLICIT
658 || sym
->attr
.allocatable
)
659 && !DECL_ARTIFICIAL (decl
))
660 TREE_STATIC (decl
) = 1;
662 /* Handle threadprivate variables. */
663 if (sym
->attr
.threadprivate
664 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
665 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
667 gfc_finish_decl_attrs (decl
, &sym
->attr
);
671 /* Allocate the lang-specific part of a decl. */
674 gfc_allocate_lang_decl (tree decl
)
676 if (DECL_LANG_SPECIFIC (decl
) == NULL
)
677 DECL_LANG_SPECIFIC (decl
) = ggc_cleared_alloc
<struct lang_decl
> ();
680 /* Remember a symbol to generate initialization/cleanup code at function
684 gfc_defer_symbol_init (gfc_symbol
* sym
)
690 /* Don't add a symbol twice. */
694 last
= head
= sym
->ns
->proc_name
;
697 /* Make sure that setup code for dummy variables which are used in the
698 setup of other variables is generated first. */
701 /* Find the first dummy arg seen after us, or the first non-dummy arg.
702 This is a circular list, so don't go past the head. */
704 && (!p
->attr
.dummy
|| p
->dummy_order
> sym
->dummy_order
))
710 /* Insert in between last and p. */
716 /* Used in gfc_get_symbol_decl and gfc_get_derived_type to obtain the
717 backend_decl for a module symbol, if it all ready exists. If the
718 module gsymbol does not exist, it is created. If the symbol does
719 not exist, it is added to the gsymbol namespace. Returns true if
720 an existing backend_decl is found. */
723 gfc_get_module_backend_decl (gfc_symbol
*sym
)
729 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->module
);
731 if (!gsym
|| (gsym
->ns
&& gsym
->type
== GSYM_MODULE
))
737 gfc_find_symbol (sym
->name
, gsym
->ns
, 0, &s
);
743 gsym
= gfc_get_gsymbol (sym
->module
);
744 gsym
->type
= GSYM_MODULE
;
745 gsym
->ns
= gfc_get_namespace (NULL
, 0);
748 st
= gfc_new_symtree (&gsym
->ns
->sym_root
, sym
->name
);
752 else if (sym
->attr
.flavor
== FL_DERIVED
)
754 if (s
&& s
->attr
.flavor
== FL_PROCEDURE
)
757 gcc_assert (s
->attr
.generic
);
758 for (intr
= s
->generic
; intr
; intr
= intr
->next
)
759 if (intr
->sym
->attr
.flavor
== FL_DERIVED
)
766 if (!s
->backend_decl
)
767 s
->backend_decl
= gfc_get_derived_type (s
);
768 gfc_copy_dt_decls_ifequal (s
, sym
, true);
771 else if (s
->backend_decl
)
773 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
774 gfc_copy_dt_decls_ifequal (s
->ts
.u
.derived
, sym
->ts
.u
.derived
,
776 else if (sym
->ts
.type
== BT_CHARACTER
)
777 sym
->ts
.u
.cl
->backend_decl
= s
->ts
.u
.cl
->backend_decl
;
778 sym
->backend_decl
= s
->backend_decl
;
786 /* Create an array index type variable with function scope. */
789 create_index_var (const char * pfx
, int nest
)
793 decl
= gfc_create_var_np (gfc_array_index_type
, pfx
);
795 gfc_add_decl_to_parent_function (decl
);
797 gfc_add_decl_to_function (decl
);
802 /* Create variables to hold all the non-constant bits of info for a
803 descriptorless array. Remember these in the lang-specific part of the
807 gfc_build_qualified_array (tree decl
, gfc_symbol
* sym
)
812 gfc_namespace
* procns
;
814 type
= TREE_TYPE (decl
);
816 /* We just use the descriptor, if there is one. */
817 if (GFC_DESCRIPTOR_TYPE_P (type
))
820 gcc_assert (GFC_ARRAY_TYPE_P (type
));
821 procns
= gfc_find_proc_namespace (sym
->ns
);
822 nest
= (procns
->proc_name
->backend_decl
!= current_function_decl
)
823 && !sym
->attr
.contained
;
825 if (sym
->attr
.codimension
&& flag_coarray
== GFC_FCOARRAY_LIB
826 && sym
->as
->type
!= AS_ASSUMED_SHAPE
827 && GFC_TYPE_ARRAY_CAF_TOKEN (type
) == NULL_TREE
)
830 tree token_type
= build_qualified_type (pvoid_type_node
,
833 if (sym
->module
&& (sym
->attr
.use_assoc
834 || sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
))
837 = get_identifier (gfc_get_string (GFC_PREFIX ("caf_token%s"),
838 IDENTIFIER_POINTER (gfc_sym_mangled_identifier (sym
))));
839 token
= build_decl (DECL_SOURCE_LOCATION (decl
), VAR_DECL
, token_name
,
841 if (sym
->attr
.use_assoc
)
842 DECL_EXTERNAL (token
) = 1;
844 TREE_STATIC (token
) = 1;
846 if (sym
->attr
.use_assoc
|| sym
->attr
.access
!= ACCESS_PRIVATE
||
847 sym
->attr
.public_used
)
848 TREE_PUBLIC (token
) = 1;
852 token
= gfc_create_var_np (token_type
, "caf_token");
853 TREE_STATIC (token
) = 1;
856 GFC_TYPE_ARRAY_CAF_TOKEN (type
) = token
;
857 DECL_ARTIFICIAL (token
) = 1;
858 DECL_NONALIASED (token
) = 1;
860 if (sym
->module
&& !sym
->attr
.use_assoc
)
863 DECL_CONTEXT (token
) = sym
->ns
->proc_name
->backend_decl
;
864 gfc_module_add_decl (cur_module
, token
);
867 gfc_add_decl_to_function (token
);
870 for (dim
= 0; dim
< GFC_TYPE_ARRAY_RANK (type
); dim
++)
872 if (GFC_TYPE_ARRAY_LBOUND (type
, dim
) == NULL_TREE
)
874 GFC_TYPE_ARRAY_LBOUND (type
, dim
) = create_index_var ("lbound", nest
);
875 TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type
, dim
)) = 1;
877 /* Don't try to use the unknown bound for assumed shape arrays. */
878 if (GFC_TYPE_ARRAY_UBOUND (type
, dim
) == NULL_TREE
879 && (sym
->as
->type
!= AS_ASSUMED_SIZE
880 || dim
< GFC_TYPE_ARRAY_RANK (type
) - 1))
882 GFC_TYPE_ARRAY_UBOUND (type
, dim
) = create_index_var ("ubound", nest
);
883 TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type
, dim
)) = 1;
886 if (GFC_TYPE_ARRAY_STRIDE (type
, dim
) == NULL_TREE
)
888 GFC_TYPE_ARRAY_STRIDE (type
, dim
) = create_index_var ("stride", nest
);
889 TREE_NO_WARNING (GFC_TYPE_ARRAY_STRIDE (type
, dim
)) = 1;
892 for (dim
= GFC_TYPE_ARRAY_RANK (type
);
893 dim
< GFC_TYPE_ARRAY_RANK (type
) + GFC_TYPE_ARRAY_CORANK (type
); dim
++)
895 if (GFC_TYPE_ARRAY_LBOUND (type
, dim
) == NULL_TREE
)
897 GFC_TYPE_ARRAY_LBOUND (type
, dim
) = create_index_var ("lbound", nest
);
898 TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type
, dim
)) = 1;
900 /* Don't try to use the unknown ubound for the last coarray dimension. */
901 if (GFC_TYPE_ARRAY_UBOUND (type
, dim
) == NULL_TREE
902 && dim
< GFC_TYPE_ARRAY_RANK (type
) + GFC_TYPE_ARRAY_CORANK (type
) - 1)
904 GFC_TYPE_ARRAY_UBOUND (type
, dim
) = create_index_var ("ubound", nest
);
905 TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type
, dim
)) = 1;
908 if (GFC_TYPE_ARRAY_OFFSET (type
) == NULL_TREE
)
910 GFC_TYPE_ARRAY_OFFSET (type
) = gfc_create_var_np (gfc_array_index_type
,
912 TREE_NO_WARNING (GFC_TYPE_ARRAY_OFFSET (type
)) = 1;
915 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type
));
917 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type
));
920 if (GFC_TYPE_ARRAY_SIZE (type
) == NULL_TREE
921 && sym
->as
->type
!= AS_ASSUMED_SIZE
)
923 GFC_TYPE_ARRAY_SIZE (type
) = create_index_var ("size", nest
);
924 TREE_NO_WARNING (GFC_TYPE_ARRAY_SIZE (type
)) = 1;
927 if (POINTER_TYPE_P (type
))
929 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type
)));
930 gcc_assert (TYPE_LANG_SPECIFIC (type
)
931 == TYPE_LANG_SPECIFIC (TREE_TYPE (type
)));
932 type
= TREE_TYPE (type
);
935 if (! COMPLETE_TYPE_P (type
) && GFC_TYPE_ARRAY_SIZE (type
))
939 size
= fold_build2_loc (input_location
, MINUS_EXPR
, gfc_array_index_type
,
940 GFC_TYPE_ARRAY_SIZE (type
), gfc_index_one_node
);
941 range
= build_range_type (gfc_array_index_type
, gfc_index_zero_node
,
943 TYPE_DOMAIN (type
) = range
;
947 if (TYPE_NAME (type
) != NULL_TREE
948 && GFC_TYPE_ARRAY_UBOUND (type
, sym
->as
->rank
- 1) != NULL_TREE
949 && TREE_CODE (GFC_TYPE_ARRAY_UBOUND (type
, sym
->as
->rank
- 1)) == VAR_DECL
)
951 tree gtype
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
953 for (dim
= 0; dim
< sym
->as
->rank
- 1; dim
++)
955 gcc_assert (TREE_CODE (gtype
) == ARRAY_TYPE
);
956 gtype
= TREE_TYPE (gtype
);
958 gcc_assert (TREE_CODE (gtype
) == ARRAY_TYPE
);
959 if (TYPE_MAX_VALUE (TYPE_DOMAIN (gtype
)) == NULL
)
960 TYPE_NAME (type
) = NULL_TREE
;
963 if (TYPE_NAME (type
) == NULL_TREE
)
965 tree gtype
= TREE_TYPE (type
), rtype
, type_decl
;
967 for (dim
= sym
->as
->rank
- 1; dim
>= 0; dim
--)
970 lbound
= GFC_TYPE_ARRAY_LBOUND (type
, dim
);
971 ubound
= GFC_TYPE_ARRAY_UBOUND (type
, dim
);
972 rtype
= build_range_type (gfc_array_index_type
, lbound
, ubound
);
973 gtype
= build_array_type (gtype
, rtype
);
974 /* Ensure the bound variables aren't optimized out at -O0.
975 For -O1 and above they often will be optimized out, but
976 can be tracked by VTA. Also set DECL_NAMELESS, so that
977 the artificial lbound.N or ubound.N DECL_NAME doesn't
978 end up in debug info. */
979 if (lbound
&& TREE_CODE (lbound
) == VAR_DECL
980 && DECL_ARTIFICIAL (lbound
) && DECL_IGNORED_P (lbound
))
982 if (DECL_NAME (lbound
)
983 && strstr (IDENTIFIER_POINTER (DECL_NAME (lbound
)),
985 DECL_NAMELESS (lbound
) = 1;
986 DECL_IGNORED_P (lbound
) = 0;
988 if (ubound
&& TREE_CODE (ubound
) == VAR_DECL
989 && DECL_ARTIFICIAL (ubound
) && DECL_IGNORED_P (ubound
))
991 if (DECL_NAME (ubound
)
992 && strstr (IDENTIFIER_POINTER (DECL_NAME (ubound
)),
994 DECL_NAMELESS (ubound
) = 1;
995 DECL_IGNORED_P (ubound
) = 0;
998 TYPE_NAME (type
) = type_decl
= build_decl (input_location
,
999 TYPE_DECL
, NULL
, gtype
);
1000 DECL_ORIGINAL_TYPE (type_decl
) = gtype
;
1005 /* For some dummy arguments we don't use the actual argument directly.
1006 Instead we create a local decl and use that. This allows us to perform
1007 initialization, and construct full type information. */
1010 gfc_build_dummy_array_decl (gfc_symbol
* sym
, tree dummy
)
1020 if (sym
->attr
.pointer
|| sym
->attr
.allocatable
1021 || (sym
->as
&& sym
->as
->type
== AS_ASSUMED_RANK
))
1024 /* Add to list of variables if not a fake result variable. */
1025 if (sym
->attr
.result
|| sym
->attr
.dummy
)
1026 gfc_defer_symbol_init (sym
);
1028 type
= TREE_TYPE (dummy
);
1029 gcc_assert (TREE_CODE (dummy
) == PARM_DECL
1030 && POINTER_TYPE_P (type
));
1032 /* Do we know the element size? */
1033 known_size
= sym
->ts
.type
!= BT_CHARACTER
1034 || INTEGER_CST_P (sym
->ts
.u
.cl
->backend_decl
);
1036 if (known_size
&& !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type
)))
1038 /* For descriptorless arrays with known element size the actual
1039 argument is sufficient. */
1040 gcc_assert (GFC_ARRAY_TYPE_P (type
));
1041 gfc_build_qualified_array (dummy
, sym
);
1045 type
= TREE_TYPE (type
);
1046 if (GFC_DESCRIPTOR_TYPE_P (type
))
1048 /* Create a descriptorless array pointer. */
1052 /* Even when -frepack-arrays is used, symbols with TARGET attribute
1053 are not repacked. */
1054 if (!flag_repack_arrays
|| sym
->attr
.target
)
1056 if (as
->type
== AS_ASSUMED_SIZE
)
1057 packed
= PACKED_FULL
;
1061 if (as
->type
== AS_EXPLICIT
)
1063 packed
= PACKED_FULL
;
1064 for (n
= 0; n
< as
->rank
; n
++)
1068 && as
->upper
[n
]->expr_type
== EXPR_CONSTANT
1069 && as
->lower
[n
]->expr_type
== EXPR_CONSTANT
))
1071 packed
= PACKED_PARTIAL
;
1077 packed
= PACKED_PARTIAL
;
1080 type
= gfc_typenode_for_spec (&sym
->ts
);
1081 type
= gfc_get_nodesc_array_type (type
, sym
->as
, packed
,
1086 /* We now have an expression for the element size, so create a fully
1087 qualified type. Reset sym->backend decl or this will just return the
1089 DECL_ARTIFICIAL (sym
->backend_decl
) = 1;
1090 sym
->backend_decl
= NULL_TREE
;
1091 type
= gfc_sym_type (sym
);
1092 packed
= PACKED_FULL
;
1095 ASM_FORMAT_PRIVATE_NAME (name
, IDENTIFIER_POINTER (DECL_NAME (dummy
)), 0);
1096 decl
= build_decl (input_location
,
1097 VAR_DECL
, get_identifier (name
), type
);
1099 DECL_ARTIFICIAL (decl
) = 1;
1100 DECL_NAMELESS (decl
) = 1;
1101 TREE_PUBLIC (decl
) = 0;
1102 TREE_STATIC (decl
) = 0;
1103 DECL_EXTERNAL (decl
) = 0;
1105 /* Avoid uninitialized warnings for optional dummy arguments. */
1106 if (sym
->attr
.optional
)
1107 TREE_NO_WARNING (decl
) = 1;
1109 /* We should never get deferred shape arrays here. We used to because of
1111 gcc_assert (sym
->as
->type
!= AS_DEFERRED
);
1113 if (packed
== PACKED_PARTIAL
)
1114 GFC_DECL_PARTIAL_PACKED_ARRAY (decl
) = 1;
1115 else if (packed
== PACKED_FULL
)
1116 GFC_DECL_PACKED_ARRAY (decl
) = 1;
1118 gfc_build_qualified_array (decl
, sym
);
1120 if (DECL_LANG_SPECIFIC (dummy
))
1121 DECL_LANG_SPECIFIC (decl
) = DECL_LANG_SPECIFIC (dummy
);
1123 gfc_allocate_lang_decl (decl
);
1125 GFC_DECL_SAVED_DESCRIPTOR (decl
) = dummy
;
1127 if (sym
->ns
->proc_name
->backend_decl
== current_function_decl
1128 || sym
->attr
.contained
)
1129 gfc_add_decl_to_function (decl
);
1131 gfc_add_decl_to_parent_function (decl
);
1136 /* For symbol SYM with GFC_DECL_SAVED_DESCRIPTOR used in contained
1137 function add a VAR_DECL to the current function with DECL_VALUE_EXPR
1138 pointing to the artificial variable for debug info purposes. */
1141 gfc_nonlocal_dummy_array_decl (gfc_symbol
*sym
)
1145 if (! nonlocal_dummy_decl_pset
)
1146 nonlocal_dummy_decl_pset
= new hash_set
<tree
>;
1148 if (nonlocal_dummy_decl_pset
->add (sym
->backend_decl
))
1151 dummy
= GFC_DECL_SAVED_DESCRIPTOR (sym
->backend_decl
);
1152 decl
= build_decl (input_location
, VAR_DECL
, DECL_NAME (dummy
),
1153 TREE_TYPE (sym
->backend_decl
));
1154 DECL_ARTIFICIAL (decl
) = 0;
1155 TREE_USED (decl
) = 1;
1156 TREE_PUBLIC (decl
) = 0;
1157 TREE_STATIC (decl
) = 0;
1158 DECL_EXTERNAL (decl
) = 0;
1159 if (DECL_BY_REFERENCE (dummy
))
1160 DECL_BY_REFERENCE (decl
) = 1;
1161 DECL_LANG_SPECIFIC (decl
) = DECL_LANG_SPECIFIC (sym
->backend_decl
);
1162 SET_DECL_VALUE_EXPR (decl
, sym
->backend_decl
);
1163 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
1164 DECL_CONTEXT (decl
) = DECL_CONTEXT (sym
->backend_decl
);
1165 DECL_CHAIN (decl
) = nonlocal_dummy_decls
;
1166 nonlocal_dummy_decls
= decl
;
1169 /* Return a constant or a variable to use as a string length. Does not
1170 add the decl to the current scope. */
1173 gfc_create_string_length (gfc_symbol
* sym
)
1175 gcc_assert (sym
->ts
.u
.cl
);
1176 gfc_conv_const_charlen (sym
->ts
.u
.cl
);
1178 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
1183 /* The string length variable shall be in static memory if it is either
1184 explicitly SAVED, a module variable or with -fno-automatic. Only
1185 relevant is "len=:" - otherwise, it is either a constant length or
1186 it is an automatic variable. */
1187 bool static_length
= sym
->attr
.save
1188 || sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
1189 || (flag_max_stack_var_size
== 0
1190 && sym
->ts
.deferred
&& !sym
->attr
.dummy
1191 && !sym
->attr
.result
&& !sym
->attr
.function
);
1193 /* Also prefix the mangled name. We need to call GFC_PREFIX for static
1194 variables as some systems do not support the "." in the assembler name.
1195 For nonstatic variables, the "." does not appear in assembler. */
1199 name
= gfc_get_string (GFC_PREFIX ("%s_MOD_%s"), sym
->module
,
1202 name
= gfc_get_string (GFC_PREFIX ("%s"), sym
->name
);
1204 else if (sym
->module
)
1205 name
= gfc_get_string (".__%s_MOD_%s", sym
->module
, sym
->name
);
1207 name
= gfc_get_string (".%s", sym
->name
);
1209 length
= build_decl (input_location
,
1210 VAR_DECL
, get_identifier (name
),
1211 gfc_charlen_type_node
);
1212 DECL_ARTIFICIAL (length
) = 1;
1213 TREE_USED (length
) = 1;
1214 if (sym
->ns
->proc_name
->tlink
!= NULL
)
1215 gfc_defer_symbol_init (sym
);
1217 sym
->ts
.u
.cl
->backend_decl
= length
;
1220 TREE_STATIC (length
) = 1;
1222 if (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
1223 && (sym
->attr
.access
!= ACCESS_PRIVATE
|| sym
->attr
.public_used
))
1224 TREE_PUBLIC (length
) = 1;
1227 gcc_assert (sym
->ts
.u
.cl
->backend_decl
!= NULL_TREE
);
1228 return sym
->ts
.u
.cl
->backend_decl
;
1231 /* If a variable is assigned a label, we add another two auxiliary
1235 gfc_add_assign_aux_vars (gfc_symbol
* sym
)
1241 gcc_assert (sym
->backend_decl
);
1243 decl
= sym
->backend_decl
;
1244 gfc_allocate_lang_decl (decl
);
1245 GFC_DECL_ASSIGN (decl
) = 1;
1246 length
= build_decl (input_location
,
1247 VAR_DECL
, create_tmp_var_name (sym
->name
),
1248 gfc_charlen_type_node
);
1249 addr
= build_decl (input_location
,
1250 VAR_DECL
, create_tmp_var_name (sym
->name
),
1252 gfc_finish_var_decl (length
, sym
);
1253 gfc_finish_var_decl (addr
, sym
);
1254 /* STRING_LENGTH is also used as flag. Less than -1 means that
1255 ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the
1256 target label's address. Otherwise, value is the length of a format string
1257 and ASSIGN_ADDR is its address. */
1258 if (TREE_STATIC (length
))
1259 DECL_INITIAL (length
) = build_int_cst (gfc_charlen_type_node
, -2);
1261 gfc_defer_symbol_init (sym
);
1263 GFC_DECL_STRING_LEN (decl
) = length
;
1264 GFC_DECL_ASSIGN_ADDR (decl
) = addr
;
1269 add_attributes_to_decl (symbol_attribute sym_attr
, tree list
)
1274 for (id
= 0; id
< EXT_ATTR_NUM
; id
++)
1275 if (sym_attr
.ext_attr
& (1 << id
))
1277 attr
= build_tree_list (
1278 get_identifier (ext_attr_list
[id
].middle_end_name
),
1280 list
= chainon (list
, attr
);
1283 if (sym_attr
.omp_declare_target
)
1284 list
= tree_cons (get_identifier ("omp declare target"),
1291 static void build_function_decl (gfc_symbol
* sym
, bool global
);
1294 /* Return the decl for a gfc_symbol, create it if it doesn't already
1298 gfc_get_symbol_decl (gfc_symbol
* sym
)
1301 tree length
= NULL_TREE
;
1304 bool intrinsic_array_parameter
= false;
1307 gcc_assert (sym
->attr
.referenced
1308 || sym
->attr
.flavor
== FL_PROCEDURE
1309 || sym
->attr
.use_assoc
1310 || sym
->ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
1311 || (sym
->module
&& sym
->attr
.if_source
!= IFSRC_DECL
1312 && sym
->backend_decl
));
1314 if (sym
->ns
&& sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.function
)
1315 byref
= gfc_return_by_reference (sym
->ns
->proc_name
);
1319 /* Make sure that the vtab for the declared type is completed. */
1320 if (sym
->ts
.type
== BT_CLASS
)
1322 gfc_component
*c
= CLASS_DATA (sym
);
1323 if (!c
->ts
.u
.derived
->backend_decl
)
1325 gfc_find_derived_vtab (c
->ts
.u
.derived
);
1326 gfc_get_derived_type (sym
->ts
.u
.derived
);
1330 /* All deferred character length procedures need to retain the backend
1331 decl, which is a pointer to the character length in the caller's
1332 namespace and to declare a local character length. */
1333 if (!byref
&& sym
->attr
.function
1334 && sym
->ts
.type
== BT_CHARACTER
1336 && sym
->ts
.u
.cl
->passed_length
== NULL
1337 && sym
->ts
.u
.cl
->backend_decl
1338 && TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
1340 sym
->ts
.u
.cl
->passed_length
= sym
->ts
.u
.cl
->backend_decl
;
1341 sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
1342 length
= gfc_create_string_length (sym
);
1345 fun_or_res
= byref
&& (sym
->attr
.result
1346 || (sym
->attr
.function
&& sym
->ts
.deferred
));
1347 if ((sym
->attr
.dummy
&& ! sym
->attr
.function
) || fun_or_res
)
1349 /* Return via extra parameter. */
1350 if (sym
->attr
.result
&& byref
1351 && !sym
->backend_decl
)
1354 DECL_ARGUMENTS (sym
->ns
->proc_name
->backend_decl
);
1355 /* For entry master function skip over the __entry
1357 if (sym
->ns
->proc_name
->attr
.entry_master
)
1358 sym
->backend_decl
= DECL_CHAIN (sym
->backend_decl
);
1361 /* Dummy variables should already have been created. */
1362 gcc_assert (sym
->backend_decl
);
1364 /* Create a character length variable. */
1365 if (sym
->ts
.type
== BT_CHARACTER
)
1367 /* For a deferred dummy, make a new string length variable. */
1368 if (sym
->ts
.deferred
1370 (sym
->ts
.u
.cl
->passed_length
== sym
->ts
.u
.cl
->backend_decl
))
1371 sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
1373 if (sym
->ts
.deferred
&& fun_or_res
1374 && sym
->ts
.u
.cl
->passed_length
== NULL
1375 && sym
->ts
.u
.cl
->backend_decl
)
1377 sym
->ts
.u
.cl
->passed_length
= sym
->ts
.u
.cl
->backend_decl
;
1378 sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
1381 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
1382 length
= gfc_create_string_length (sym
);
1384 length
= sym
->ts
.u
.cl
->backend_decl
;
1385 if (TREE_CODE (length
) == VAR_DECL
1386 && DECL_FILE_SCOPE_P (length
))
1388 /* Add the string length to the same context as the symbol. */
1389 if (DECL_CONTEXT (sym
->backend_decl
) == current_function_decl
)
1390 gfc_add_decl_to_function (length
);
1392 gfc_add_decl_to_parent_function (length
);
1394 gcc_assert (DECL_CONTEXT (sym
->backend_decl
) ==
1395 DECL_CONTEXT (length
));
1397 gfc_defer_symbol_init (sym
);
1401 /* Use a copy of the descriptor for dummy arrays. */
1402 if ((sym
->attr
.dimension
|| sym
->attr
.codimension
)
1403 && !TREE_USED (sym
->backend_decl
))
1405 decl
= gfc_build_dummy_array_decl (sym
, sym
->backend_decl
);
1406 /* Prevent the dummy from being detected as unused if it is copied. */
1407 if (sym
->backend_decl
!= NULL
&& decl
!= sym
->backend_decl
)
1408 DECL_ARTIFICIAL (sym
->backend_decl
) = 1;
1409 sym
->backend_decl
= decl
;
1412 TREE_USED (sym
->backend_decl
) = 1;
1413 if (sym
->attr
.assign
&& GFC_DECL_ASSIGN (sym
->backend_decl
) == 0)
1415 gfc_add_assign_aux_vars (sym
);
1418 if (sym
->attr
.dimension
1419 && DECL_LANG_SPECIFIC (sym
->backend_decl
)
1420 && GFC_DECL_SAVED_DESCRIPTOR (sym
->backend_decl
)
1421 && DECL_CONTEXT (sym
->backend_decl
) != current_function_decl
)
1422 gfc_nonlocal_dummy_array_decl (sym
);
1424 if (sym
->ts
.type
== BT_CLASS
&& sym
->backend_decl
)
1425 GFC_DECL_CLASS(sym
->backend_decl
) = 1;
1427 if (sym
->ts
.type
== BT_CLASS
&& sym
->backend_decl
)
1428 GFC_DECL_CLASS(sym
->backend_decl
) = 1;
1429 return sym
->backend_decl
;
1432 if (sym
->backend_decl
)
1433 return sym
->backend_decl
;
1435 /* Special case for array-valued named constants from intrinsic
1436 procedures; those are inlined. */
1437 if (sym
->attr
.use_assoc
&& sym
->attr
.flavor
== FL_PARAMETER
1438 && (sym
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
1439 || sym
->from_intmod
== INTMOD_ISO_C_BINDING
))
1440 intrinsic_array_parameter
= true;
1442 /* If use associated compilation, use the module
1444 if ((sym
->attr
.flavor
== FL_VARIABLE
1445 || sym
->attr
.flavor
== FL_PARAMETER
)
1446 && sym
->attr
.use_assoc
1447 && !intrinsic_array_parameter
1449 && gfc_get_module_backend_decl (sym
))
1451 if (sym
->ts
.type
== BT_CLASS
&& sym
->backend_decl
)
1452 GFC_DECL_CLASS(sym
->backend_decl
) = 1;
1453 return sym
->backend_decl
;
1456 if (sym
->attr
.flavor
== FL_PROCEDURE
)
1458 /* Catch functions. Only used for actual parameters,
1459 procedure pointers and procptr initialization targets. */
1460 if (sym
->attr
.use_assoc
|| sym
->attr
.intrinsic
1461 || sym
->attr
.if_source
!= IFSRC_DECL
)
1463 decl
= gfc_get_extern_function_decl (sym
);
1464 gfc_set_decl_location (decl
, &sym
->declared_at
);
1468 if (!sym
->backend_decl
)
1469 build_function_decl (sym
, false);
1470 decl
= sym
->backend_decl
;
1475 if (sym
->attr
.intrinsic
)
1476 gfc_internal_error ("intrinsic variable which isn't a procedure");
1478 /* Create string length decl first so that they can be used in the
1479 type declaration. */
1480 if (sym
->ts
.type
== BT_CHARACTER
)
1481 length
= gfc_create_string_length (sym
);
1483 /* Create the decl for the variable. */
1484 decl
= build_decl (sym
->declared_at
.lb
->location
,
1485 VAR_DECL
, gfc_sym_identifier (sym
), gfc_sym_type (sym
));
1487 /* Add attributes to variables. Functions are handled elsewhere. */
1488 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
1489 decl_attributes (&decl
, attributes
, 0);
1491 /* Symbols from modules should have their assembler names mangled.
1492 This is done here rather than in gfc_finish_var_decl because it
1493 is different for string length variables. */
1496 gfc_set_decl_assembler_name (decl
, gfc_sym_mangled_identifier (sym
));
1497 if (sym
->attr
.use_assoc
&& !intrinsic_array_parameter
)
1498 DECL_IGNORED_P (decl
) = 1;
1501 if (sym
->attr
.select_type_temporary
)
1503 DECL_ARTIFICIAL (decl
) = 1;
1504 DECL_IGNORED_P (decl
) = 1;
1507 if (sym
->attr
.dimension
|| sym
->attr
.codimension
)
1509 /* Create variables to hold the non-constant bits of array info. */
1510 gfc_build_qualified_array (decl
, sym
);
1512 if (sym
->attr
.contiguous
1513 || ((sym
->attr
.allocatable
|| !sym
->attr
.dummy
) && !sym
->attr
.pointer
))
1514 GFC_DECL_PACKED_ARRAY (decl
) = 1;
1517 /* Remember this variable for allocation/cleanup. */
1518 if (sym
->attr
.dimension
|| sym
->attr
.allocatable
|| sym
->attr
.codimension
1519 || (sym
->ts
.type
== BT_CLASS
&&
1520 (CLASS_DATA (sym
)->attr
.dimension
1521 || CLASS_DATA (sym
)->attr
.allocatable
))
1522 || (sym
->ts
.type
== BT_DERIVED
1523 && (sym
->ts
.u
.derived
->attr
.alloc_comp
1524 || (!sym
->attr
.pointer
&& !sym
->attr
.artificial
&& !sym
->attr
.save
1525 && !sym
->ns
->proc_name
->attr
.is_main_program
1526 && gfc_is_finalizable (sym
->ts
.u
.derived
, NULL
))))
1527 /* This applies a derived type default initializer. */
1528 || (sym
->ts
.type
== BT_DERIVED
1529 && sym
->attr
.save
== SAVE_NONE
1531 && !sym
->attr
.allocatable
1532 && (sym
->value
&& !sym
->ns
->proc_name
->attr
.is_main_program
)
1533 && !(sym
->attr
.use_assoc
&& !intrinsic_array_parameter
)))
1534 gfc_defer_symbol_init (sym
);
1536 gfc_finish_var_decl (decl
, sym
);
1538 if (sym
->ts
.type
== BT_CHARACTER
)
1540 /* Character variables need special handling. */
1541 gfc_allocate_lang_decl (decl
);
1543 if (TREE_CODE (length
) != INTEGER_CST
)
1545 gfc_finish_var_decl (length
, sym
);
1546 gcc_assert (!sym
->value
);
1549 else if (sym
->attr
.subref_array_pointer
)
1551 /* We need the span for these beasts. */
1552 gfc_allocate_lang_decl (decl
);
1555 if (sym
->attr
.subref_array_pointer
)
1558 GFC_DECL_SUBREF_ARRAY_P (decl
) = 1;
1559 span
= build_decl (input_location
,
1560 VAR_DECL
, create_tmp_var_name ("span"),
1561 gfc_array_index_type
);
1562 gfc_finish_var_decl (span
, sym
);
1563 TREE_STATIC (span
) = TREE_STATIC (decl
);
1564 DECL_ARTIFICIAL (span
) = 1;
1566 GFC_DECL_SPAN (decl
) = span
;
1567 GFC_TYPE_ARRAY_SPAN (TREE_TYPE (decl
)) = span
;
1570 if (sym
->ts
.type
== BT_CLASS
)
1571 GFC_DECL_CLASS(decl
) = 1;
1573 sym
->backend_decl
= decl
;
1575 if (sym
->attr
.assign
)
1576 gfc_add_assign_aux_vars (sym
);
1578 if (intrinsic_array_parameter
)
1580 TREE_STATIC (decl
) = 1;
1581 DECL_EXTERNAL (decl
) = 0;
1584 if (TREE_STATIC (decl
)
1585 && !(sym
->attr
.use_assoc
&& !intrinsic_array_parameter
)
1586 && (sym
->attr
.save
|| sym
->ns
->proc_name
->attr
.is_main_program
1587 || flag_max_stack_var_size
== 0
1588 || sym
->attr
.data
|| sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
1589 && (flag_coarray
!= GFC_FCOARRAY_LIB
1590 || !sym
->attr
.codimension
|| sym
->attr
.allocatable
))
1592 /* Add static initializer. For procedures, it is only needed if
1593 SAVE is specified otherwise they need to be reinitialized
1594 every time the procedure is entered. The TREE_STATIC is
1595 in this case due to -fmax-stack-var-size=. */
1597 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
1598 TREE_TYPE (decl
), sym
->attr
.dimension
1599 || (sym
->attr
.codimension
1600 && sym
->attr
.allocatable
),
1601 sym
->attr
.pointer
|| sym
->attr
.allocatable
1602 || sym
->ts
.type
== BT_CLASS
,
1603 sym
->attr
.proc_pointer
);
1606 if (!TREE_STATIC (decl
)
1607 && POINTER_TYPE_P (TREE_TYPE (decl
))
1608 && !sym
->attr
.pointer
1609 && !sym
->attr
.allocatable
1610 && !sym
->attr
.proc_pointer
1611 && !sym
->attr
.select_type_temporary
)
1612 DECL_BY_REFERENCE (decl
) = 1;
1614 if (sym
->attr
.associate_var
)
1615 GFC_DECL_ASSOCIATE_VAR_P (decl
) = 1;
1618 || (sym
->name
[0] == '_' && strncmp ("__def_init", sym
->name
, 10) == 0))
1619 TREE_READONLY (decl
) = 1;
1625 /* Substitute a temporary variable in place of the real one. */
1628 gfc_shadow_sym (gfc_symbol
* sym
, tree decl
, gfc_saved_var
* save
)
1630 save
->attr
= sym
->attr
;
1631 save
->decl
= sym
->backend_decl
;
1633 gfc_clear_attr (&sym
->attr
);
1634 sym
->attr
.referenced
= 1;
1635 sym
->attr
.flavor
= FL_VARIABLE
;
1637 sym
->backend_decl
= decl
;
1641 /* Restore the original variable. */
1644 gfc_restore_sym (gfc_symbol
* sym
, gfc_saved_var
* save
)
1646 sym
->attr
= save
->attr
;
1647 sym
->backend_decl
= save
->decl
;
1651 /* Declare a procedure pointer. */
1654 get_proc_pointer_decl (gfc_symbol
*sym
)
1659 decl
= sym
->backend_decl
;
1663 decl
= build_decl (input_location
,
1664 VAR_DECL
, get_identifier (sym
->name
),
1665 build_pointer_type (gfc_get_function_type (sym
)));
1669 /* Apply name mangling. */
1670 gfc_set_decl_assembler_name (decl
, gfc_sym_mangled_identifier (sym
));
1671 if (sym
->attr
.use_assoc
)
1672 DECL_IGNORED_P (decl
) = 1;
1675 if ((sym
->ns
->proc_name
1676 && sym
->ns
->proc_name
->backend_decl
== current_function_decl
)
1677 || sym
->attr
.contained
)
1678 gfc_add_decl_to_function (decl
);
1679 else if (sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
1680 gfc_add_decl_to_parent_function (decl
);
1682 sym
->backend_decl
= decl
;
1684 /* If a variable is USE associated, it's always external. */
1685 if (sym
->attr
.use_assoc
)
1687 DECL_EXTERNAL (decl
) = 1;
1688 TREE_PUBLIC (decl
) = 1;
1690 else if (sym
->module
&& sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
1692 /* This is the declaration of a module variable. */
1693 if (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
1694 && (sym
->attr
.access
!= ACCESS_PRIVATE
|| sym
->attr
.public_used
))
1695 TREE_PUBLIC (decl
) = 1;
1696 TREE_STATIC (decl
) = 1;
1699 if (!sym
->attr
.use_assoc
1700 && (sym
->attr
.save
!= SAVE_NONE
|| sym
->attr
.data
1701 || (sym
->value
&& sym
->ns
->proc_name
->attr
.is_main_program
)))
1702 TREE_STATIC (decl
) = 1;
1704 if (TREE_STATIC (decl
) && sym
->value
)
1706 /* Add static initializer. */
1707 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
1709 sym
->attr
.dimension
,
1713 /* Handle threadprivate procedure pointers. */
1714 if (sym
->attr
.threadprivate
1715 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1716 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
1718 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
1719 decl_attributes (&decl
, attributes
, 0);
1725 /* Get a basic decl for an external function. */
1728 gfc_get_extern_function_decl (gfc_symbol
* sym
)
1734 gfc_intrinsic_sym
*isym
;
1736 char s
[GFC_MAX_SYMBOL_LEN
+ 23]; /* "_gfortran_f2c_specific" and '\0'. */
1741 if (sym
->backend_decl
)
1742 return sym
->backend_decl
;
1744 /* We should never be creating external decls for alternate entry points.
1745 The procedure may be an alternate entry point, but we don't want/need
1747 gcc_assert (!(sym
->attr
.entry
|| sym
->attr
.entry_master
));
1749 if (sym
->attr
.proc_pointer
)
1750 return get_proc_pointer_decl (sym
);
1752 /* See if this is an external procedure from the same file. If so,
1753 return the backend_decl. */
1754 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->binding_label
1755 ? sym
->binding_label
: sym
->name
);
1757 if (gsym
&& !gsym
->defined
)
1760 /* This can happen because of C binding. */
1761 if (gsym
&& gsym
->ns
&& gsym
->ns
->proc_name
1762 && gsym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
1765 if ((!sym
->attr
.use_assoc
|| sym
->attr
.if_source
!= IFSRC_DECL
)
1766 && !sym
->backend_decl
1768 && ((gsym
->type
== GSYM_SUBROUTINE
) || (gsym
->type
== GSYM_FUNCTION
))
1769 && (gsym
->ns
->proc_name
->backend_decl
|| !sym
->attr
.intrinsic
))
1771 if (!gsym
->ns
->proc_name
->backend_decl
)
1773 /* By construction, the external function cannot be
1774 a contained procedure. */
1777 gfc_save_backend_locus (&old_loc
);
1780 gfc_create_function_decl (gsym
->ns
, true);
1783 gfc_restore_backend_locus (&old_loc
);
1786 /* If the namespace has entries, the proc_name is the
1787 entry master. Find the entry and use its backend_decl.
1788 otherwise, use the proc_name backend_decl. */
1789 if (gsym
->ns
->entries
)
1791 gfc_entry_list
*entry
= gsym
->ns
->entries
;
1793 for (; entry
; entry
= entry
->next
)
1795 if (strcmp (gsym
->name
, entry
->sym
->name
) == 0)
1797 sym
->backend_decl
= entry
->sym
->backend_decl
;
1803 sym
->backend_decl
= gsym
->ns
->proc_name
->backend_decl
;
1805 if (sym
->backend_decl
)
1807 /* Avoid problems of double deallocation of the backend declaration
1808 later in gfc_trans_use_stmts; cf. PR 45087. */
1809 if (sym
->attr
.if_source
!= IFSRC_DECL
&& sym
->attr
.use_assoc
)
1810 sym
->attr
.use_assoc
= 0;
1812 return sym
->backend_decl
;
1816 /* See if this is a module procedure from the same file. If so,
1817 return the backend_decl. */
1819 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->module
);
1822 if (gsym
&& gsym
->ns
1823 && (gsym
->type
== GSYM_MODULE
1824 || (gsym
->ns
->proc_name
&& gsym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)))
1829 if (gsym
->type
== GSYM_MODULE
)
1830 gfc_find_symbol (sym
->name
, gsym
->ns
, 0, &s
);
1832 gfc_find_symbol (gsym
->sym_name
, gsym
->ns
, 0, &s
);
1834 if (s
&& s
->backend_decl
)
1836 if (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
)
1837 gfc_copy_dt_decls_ifequal (s
->ts
.u
.derived
, sym
->ts
.u
.derived
,
1839 else if (sym
->ts
.type
== BT_CHARACTER
)
1840 sym
->ts
.u
.cl
->backend_decl
= s
->ts
.u
.cl
->backend_decl
;
1841 sym
->backend_decl
= s
->backend_decl
;
1842 return sym
->backend_decl
;
1846 if (sym
->attr
.intrinsic
)
1848 /* Call the resolution function to get the actual name. This is
1849 a nasty hack which relies on the resolution functions only looking
1850 at the first argument. We pass NULL for the second argument
1851 otherwise things like AINT get confused. */
1852 isym
= gfc_find_function (sym
->name
);
1853 gcc_assert (isym
->resolve
.f0
!= NULL
);
1855 memset (&e
, 0, sizeof (e
));
1856 e
.expr_type
= EXPR_FUNCTION
;
1858 memset (&argexpr
, 0, sizeof (argexpr
));
1859 gcc_assert (isym
->formal
);
1860 argexpr
.ts
= isym
->formal
->ts
;
1862 if (isym
->formal
->next
== NULL
)
1863 isym
->resolve
.f1 (&e
, &argexpr
);
1866 if (isym
->formal
->next
->next
== NULL
)
1867 isym
->resolve
.f2 (&e
, &argexpr
, NULL
);
1870 if (isym
->formal
->next
->next
->next
== NULL
)
1871 isym
->resolve
.f3 (&e
, &argexpr
, NULL
, NULL
);
1874 /* All specific intrinsics take less than 5 arguments. */
1875 gcc_assert (isym
->formal
->next
->next
->next
->next
== NULL
);
1876 isym
->resolve
.f4 (&e
, &argexpr
, NULL
, NULL
, NULL
);
1882 && ((e
.ts
.type
== BT_REAL
&& e
.ts
.kind
== gfc_default_real_kind
)
1883 || e
.ts
.type
== BT_COMPLEX
))
1885 /* Specific which needs a different implementation if f2c
1886 calling conventions are used. */
1887 sprintf (s
, "_gfortran_f2c_specific%s", e
.value
.function
.name
);
1890 sprintf (s
, "_gfortran_specific%s", e
.value
.function
.name
);
1892 name
= get_identifier (s
);
1893 mangled_name
= name
;
1897 name
= gfc_sym_identifier (sym
);
1898 mangled_name
= gfc_sym_mangled_function_id (sym
);
1901 type
= gfc_get_function_type (sym
);
1902 fndecl
= build_decl (input_location
,
1903 FUNCTION_DECL
, name
, type
);
1905 /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
1906 TREE_PUBLIC specifies whether a function is globally addressable (i.e.
1907 the opposite of declaring a function as static in C). */
1908 DECL_EXTERNAL (fndecl
) = 1;
1909 TREE_PUBLIC (fndecl
) = 1;
1911 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
1912 decl_attributes (&fndecl
, attributes
, 0);
1914 gfc_set_decl_assembler_name (fndecl
, mangled_name
);
1916 /* Set the context of this decl. */
1917 if (0 && sym
->ns
&& sym
->ns
->proc_name
)
1919 /* TODO: Add external decls to the appropriate scope. */
1920 DECL_CONTEXT (fndecl
) = sym
->ns
->proc_name
->backend_decl
;
1924 /* Global declaration, e.g. intrinsic subroutine. */
1925 DECL_CONTEXT (fndecl
) = NULL_TREE
;
1928 /* Set attributes for PURE functions. A call to PURE function in the
1929 Fortran 95 sense is both pure and without side effects in the C
1931 if (sym
->attr
.pure
|| sym
->attr
.implicit_pure
)
1933 if (sym
->attr
.function
&& !gfc_return_by_reference (sym
))
1934 DECL_PURE_P (fndecl
) = 1;
1935 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
1936 parameters and don't use alternate returns (is this
1937 allowed?). In that case, calls to them are meaningless, and
1938 can be optimized away. See also in build_function_decl(). */
1939 TREE_SIDE_EFFECTS (fndecl
) = 0;
1942 /* Mark non-returning functions. */
1943 if (sym
->attr
.noreturn
)
1944 TREE_THIS_VOLATILE(fndecl
) = 1;
1946 sym
->backend_decl
= fndecl
;
1948 if (DECL_CONTEXT (fndecl
) == NULL_TREE
)
1949 pushdecl_top_level (fndecl
);
1952 && sym
->formal_ns
->proc_name
== sym
1953 && sym
->formal_ns
->omp_declare_simd
)
1954 gfc_trans_omp_declare_simd (sym
->formal_ns
);
1960 /* Create a declaration for a procedure. For external functions (in the C
1961 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
1962 a master function with alternate entry points. */
1965 build_function_decl (gfc_symbol
* sym
, bool global
)
1967 tree fndecl
, type
, attributes
;
1968 symbol_attribute attr
;
1970 gfc_formal_arglist
*f
;
1972 gcc_assert (!sym
->attr
.external
);
1974 if (sym
->backend_decl
)
1977 /* Set the line and filename. sym->declared_at seems to point to the
1978 last statement for subroutines, but it'll do for now. */
1979 gfc_set_backend_locus (&sym
->declared_at
);
1981 /* Allow only one nesting level. Allow public declarations. */
1982 gcc_assert (current_function_decl
== NULL_TREE
1983 || DECL_FILE_SCOPE_P (current_function_decl
)
1984 || (TREE_CODE (DECL_CONTEXT (current_function_decl
))
1985 == NAMESPACE_DECL
));
1987 type
= gfc_get_function_type (sym
);
1988 fndecl
= build_decl (input_location
,
1989 FUNCTION_DECL
, gfc_sym_identifier (sym
), type
);
1993 /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
1994 TREE_PUBLIC specifies whether a function is globally addressable (i.e.
1995 the opposite of declaring a function as static in C). */
1996 DECL_EXTERNAL (fndecl
) = 0;
1998 if (sym
->attr
.access
== ACCESS_UNKNOWN
&& sym
->module
1999 && (sym
->ns
->default_access
== ACCESS_PRIVATE
2000 || (sym
->ns
->default_access
== ACCESS_UNKNOWN
2001 && flag_module_private
)))
2002 sym
->attr
.access
= ACCESS_PRIVATE
;
2004 if (!current_function_decl
2005 && !sym
->attr
.entry_master
&& !sym
->attr
.is_main_program
2006 && (sym
->attr
.access
!= ACCESS_PRIVATE
|| sym
->binding_label
2007 || sym
->attr
.public_used
))
2008 TREE_PUBLIC (fndecl
) = 1;
2010 if (sym
->attr
.referenced
|| sym
->attr
.entry_master
)
2011 TREE_USED (fndecl
) = 1;
2013 attributes
= add_attributes_to_decl (attr
, NULL_TREE
);
2014 decl_attributes (&fndecl
, attributes
, 0);
2016 /* Figure out the return type of the declared function, and build a
2017 RESULT_DECL for it. If this is a subroutine with alternate
2018 returns, build a RESULT_DECL for it. */
2019 result_decl
= NULL_TREE
;
2020 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
2023 if (gfc_return_by_reference (sym
))
2024 type
= void_type_node
;
2027 if (sym
->result
!= sym
)
2028 result_decl
= gfc_sym_identifier (sym
->result
);
2030 type
= TREE_TYPE (TREE_TYPE (fndecl
));
2035 /* Look for alternate return placeholders. */
2036 int has_alternate_returns
= 0;
2037 for (f
= gfc_sym_get_dummy_args (sym
); f
; f
= f
->next
)
2041 has_alternate_returns
= 1;
2046 if (has_alternate_returns
)
2047 type
= integer_type_node
;
2049 type
= void_type_node
;
2052 result_decl
= build_decl (input_location
,
2053 RESULT_DECL
, result_decl
, type
);
2054 DECL_ARTIFICIAL (result_decl
) = 1;
2055 DECL_IGNORED_P (result_decl
) = 1;
2056 DECL_CONTEXT (result_decl
) = fndecl
;
2057 DECL_RESULT (fndecl
) = result_decl
;
2059 /* Don't call layout_decl for a RESULT_DECL.
2060 layout_decl (result_decl, 0); */
2062 /* TREE_STATIC means the function body is defined here. */
2063 TREE_STATIC (fndecl
) = 1;
2065 /* Set attributes for PURE functions. A call to a PURE function in the
2066 Fortran 95 sense is both pure and without side effects in the C
2068 if (attr
.pure
|| attr
.implicit_pure
)
2070 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
2071 including an alternate return. In that case it can also be
2072 marked as PURE. See also in gfc_get_extern_function_decl(). */
2073 if (attr
.function
&& !gfc_return_by_reference (sym
))
2074 DECL_PURE_P (fndecl
) = 1;
2075 TREE_SIDE_EFFECTS (fndecl
) = 0;
2079 /* Layout the function declaration and put it in the binding level
2080 of the current function. */
2083 pushdecl_top_level (fndecl
);
2087 /* Perform name mangling if this is a top level or module procedure. */
2088 if (current_function_decl
== NULL_TREE
)
2089 gfc_set_decl_assembler_name (fndecl
, gfc_sym_mangled_function_id (sym
));
2091 sym
->backend_decl
= fndecl
;
2095 /* Create the DECL_ARGUMENTS for a procedure. */
2098 create_function_arglist (gfc_symbol
* sym
)
2101 gfc_formal_arglist
*f
;
2102 tree typelist
, hidden_typelist
;
2103 tree arglist
, hidden_arglist
;
2107 fndecl
= sym
->backend_decl
;
2109 /* Build formal argument list. Make sure that their TREE_CONTEXT is
2110 the new FUNCTION_DECL node. */
2111 arglist
= NULL_TREE
;
2112 hidden_arglist
= NULL_TREE
;
2113 typelist
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
2115 if (sym
->attr
.entry_master
)
2117 type
= TREE_VALUE (typelist
);
2118 parm
= build_decl (input_location
,
2119 PARM_DECL
, get_identifier ("__entry"), type
);
2121 DECL_CONTEXT (parm
) = fndecl
;
2122 DECL_ARG_TYPE (parm
) = type
;
2123 TREE_READONLY (parm
) = 1;
2124 gfc_finish_decl (parm
);
2125 DECL_ARTIFICIAL (parm
) = 1;
2127 arglist
= chainon (arglist
, parm
);
2128 typelist
= TREE_CHAIN (typelist
);
2131 if (gfc_return_by_reference (sym
))
2133 tree type
= TREE_VALUE (typelist
), length
= NULL
;
2135 if (sym
->ts
.type
== BT_CHARACTER
)
2137 /* Length of character result. */
2138 tree len_type
= TREE_VALUE (TREE_CHAIN (typelist
));
2140 length
= build_decl (input_location
,
2142 get_identifier (".__result"),
2144 if (!sym
->ts
.u
.cl
->length
)
2146 sym
->ts
.u
.cl
->backend_decl
= length
;
2147 TREE_USED (length
) = 1;
2149 gcc_assert (TREE_CODE (length
) == PARM_DECL
);
2150 DECL_CONTEXT (length
) = fndecl
;
2151 DECL_ARG_TYPE (length
) = len_type
;
2152 TREE_READONLY (length
) = 1;
2153 DECL_ARTIFICIAL (length
) = 1;
2154 gfc_finish_decl (length
);
2155 if (sym
->ts
.u
.cl
->backend_decl
== NULL
2156 || sym
->ts
.u
.cl
->backend_decl
== length
)
2161 if (sym
->ts
.u
.cl
->backend_decl
== NULL
)
2163 tree len
= build_decl (input_location
,
2165 get_identifier ("..__result"),
2166 gfc_charlen_type_node
);
2167 DECL_ARTIFICIAL (len
) = 1;
2168 TREE_USED (len
) = 1;
2169 sym
->ts
.u
.cl
->backend_decl
= len
;
2172 /* Make sure PARM_DECL type doesn't point to incomplete type. */
2173 arg
= sym
->result
? sym
->result
: sym
;
2174 backend_decl
= arg
->backend_decl
;
2175 /* Temporary clear it, so that gfc_sym_type creates complete
2177 arg
->backend_decl
= NULL
;
2178 type
= gfc_sym_type (arg
);
2179 arg
->backend_decl
= backend_decl
;
2180 type
= build_reference_type (type
);
2184 parm
= build_decl (input_location
,
2185 PARM_DECL
, get_identifier ("__result"), type
);
2187 DECL_CONTEXT (parm
) = fndecl
;
2188 DECL_ARG_TYPE (parm
) = TREE_VALUE (typelist
);
2189 TREE_READONLY (parm
) = 1;
2190 DECL_ARTIFICIAL (parm
) = 1;
2191 gfc_finish_decl (parm
);
2193 arglist
= chainon (arglist
, parm
);
2194 typelist
= TREE_CHAIN (typelist
);
2196 if (sym
->ts
.type
== BT_CHARACTER
)
2198 gfc_allocate_lang_decl (parm
);
2199 arglist
= chainon (arglist
, length
);
2200 typelist
= TREE_CHAIN (typelist
);
2204 hidden_typelist
= typelist
;
2205 for (f
= gfc_sym_get_dummy_args (sym
); f
; f
= f
->next
)
2206 if (f
->sym
!= NULL
) /* Ignore alternate returns. */
2207 hidden_typelist
= TREE_CHAIN (hidden_typelist
);
2209 for (f
= gfc_sym_get_dummy_args (sym
); f
; f
= f
->next
)
2211 char name
[GFC_MAX_SYMBOL_LEN
+ 2];
2213 /* Ignore alternate returns. */
2217 type
= TREE_VALUE (typelist
);
2219 if (f
->sym
->ts
.type
== BT_CHARACTER
2220 && (!sym
->attr
.is_bind_c
|| sym
->attr
.entry_master
))
2222 tree len_type
= TREE_VALUE (hidden_typelist
);
2223 tree length
= NULL_TREE
;
2224 if (!f
->sym
->ts
.deferred
)
2225 gcc_assert (len_type
== gfc_charlen_type_node
);
2227 gcc_assert (POINTER_TYPE_P (len_type
));
2229 strcpy (&name
[1], f
->sym
->name
);
2231 length
= build_decl (input_location
,
2232 PARM_DECL
, get_identifier (name
), len_type
);
2234 hidden_arglist
= chainon (hidden_arglist
, length
);
2235 DECL_CONTEXT (length
) = fndecl
;
2236 DECL_ARTIFICIAL (length
) = 1;
2237 DECL_ARG_TYPE (length
) = len_type
;
2238 TREE_READONLY (length
) = 1;
2239 gfc_finish_decl (length
);
2241 /* Remember the passed value. */
2242 if (!f
->sym
->ts
.u
.cl
|| f
->sym
->ts
.u
.cl
->passed_length
)
2244 /* This can happen if the same type is used for multiple
2245 arguments. We need to copy cl as otherwise
2246 cl->passed_length gets overwritten. */
2247 f
->sym
->ts
.u
.cl
= gfc_new_charlen (f
->sym
->ns
, f
->sym
->ts
.u
.cl
);
2249 f
->sym
->ts
.u
.cl
->passed_length
= length
;
2251 /* Use the passed value for assumed length variables. */
2252 if (!f
->sym
->ts
.u
.cl
->length
)
2254 TREE_USED (length
) = 1;
2255 gcc_assert (!f
->sym
->ts
.u
.cl
->backend_decl
);
2256 f
->sym
->ts
.u
.cl
->backend_decl
= length
;
2259 hidden_typelist
= TREE_CHAIN (hidden_typelist
);
2261 if (f
->sym
->ts
.u
.cl
->backend_decl
== NULL
2262 || f
->sym
->ts
.u
.cl
->backend_decl
== length
)
2264 if (f
->sym
->ts
.u
.cl
->backend_decl
== NULL
)
2265 gfc_create_string_length (f
->sym
);
2267 /* Make sure PARM_DECL type doesn't point to incomplete type. */
2268 if (f
->sym
->attr
.flavor
== FL_PROCEDURE
)
2269 type
= build_pointer_type (gfc_get_function_type (f
->sym
));
2271 type
= gfc_sym_type (f
->sym
);
2274 /* For noncharacter scalar intrinsic types, VALUE passes the value,
2275 hence, the optional status cannot be transferred via a NULL pointer.
2276 Thus, we will use a hidden argument in that case. */
2277 else if (f
->sym
->attr
.optional
&& f
->sym
->attr
.value
2278 && !f
->sym
->attr
.dimension
&& f
->sym
->ts
.type
!= BT_CLASS
2279 && f
->sym
->ts
.type
!= BT_DERIVED
)
2282 strcpy (&name
[1], f
->sym
->name
);
2284 tmp
= build_decl (input_location
,
2285 PARM_DECL
, get_identifier (name
),
2288 hidden_arglist
= chainon (hidden_arglist
, tmp
);
2289 DECL_CONTEXT (tmp
) = fndecl
;
2290 DECL_ARTIFICIAL (tmp
) = 1;
2291 DECL_ARG_TYPE (tmp
) = boolean_type_node
;
2292 TREE_READONLY (tmp
) = 1;
2293 gfc_finish_decl (tmp
);
2296 /* For non-constant length array arguments, make sure they use
2297 a different type node from TYPE_ARG_TYPES type. */
2298 if (f
->sym
->attr
.dimension
2299 && type
== TREE_VALUE (typelist
)
2300 && TREE_CODE (type
) == POINTER_TYPE
2301 && GFC_ARRAY_TYPE_P (type
)
2302 && f
->sym
->as
->type
!= AS_ASSUMED_SIZE
2303 && ! COMPLETE_TYPE_P (TREE_TYPE (type
)))
2305 if (f
->sym
->attr
.flavor
== FL_PROCEDURE
)
2306 type
= build_pointer_type (gfc_get_function_type (f
->sym
));
2308 type
= gfc_sym_type (f
->sym
);
2311 if (f
->sym
->attr
.proc_pointer
)
2312 type
= build_pointer_type (type
);
2314 if (f
->sym
->attr
.volatile_
)
2315 type
= build_qualified_type (type
, TYPE_QUAL_VOLATILE
);
2317 /* Build the argument declaration. */
2318 parm
= build_decl (input_location
,
2319 PARM_DECL
, gfc_sym_identifier (f
->sym
), type
);
2321 if (f
->sym
->attr
.volatile_
)
2323 TREE_THIS_VOLATILE (parm
) = 1;
2324 TREE_SIDE_EFFECTS (parm
) = 1;
2327 /* Fill in arg stuff. */
2328 DECL_CONTEXT (parm
) = fndecl
;
2329 DECL_ARG_TYPE (parm
) = TREE_VALUE (typelist
);
2330 /* All implementation args except for VALUE are read-only. */
2331 if (!f
->sym
->attr
.value
)
2332 TREE_READONLY (parm
) = 1;
2333 if (POINTER_TYPE_P (type
)
2334 && (!f
->sym
->attr
.proc_pointer
2335 && f
->sym
->attr
.flavor
!= FL_PROCEDURE
))
2336 DECL_BY_REFERENCE (parm
) = 1;
2338 gfc_finish_decl (parm
);
2339 gfc_finish_decl_attrs (parm
, &f
->sym
->attr
);
2341 f
->sym
->backend_decl
= parm
;
2343 /* Coarrays which are descriptorless or assumed-shape pass with
2344 -fcoarray=lib the token and the offset as hidden arguments. */
2345 if (flag_coarray
== GFC_FCOARRAY_LIB
2346 && ((f
->sym
->ts
.type
!= BT_CLASS
&& f
->sym
->attr
.codimension
2347 && !f
->sym
->attr
.allocatable
)
2348 || (f
->sym
->ts
.type
== BT_CLASS
2349 && CLASS_DATA (f
->sym
)->attr
.codimension
2350 && !CLASS_DATA (f
->sym
)->attr
.allocatable
)))
2356 gcc_assert (f
->sym
->backend_decl
!= NULL_TREE
2357 && !sym
->attr
.is_bind_c
);
2358 caf_type
= f
->sym
->ts
.type
== BT_CLASS
2359 ? TREE_TYPE (CLASS_DATA (f
->sym
)->backend_decl
)
2360 : TREE_TYPE (f
->sym
->backend_decl
);
2362 token
= build_decl (input_location
, PARM_DECL
,
2363 create_tmp_var_name ("caf_token"),
2364 build_qualified_type (pvoid_type_node
,
2365 TYPE_QUAL_RESTRICT
));
2366 if ((f
->sym
->ts
.type
!= BT_CLASS
2367 && f
->sym
->as
->type
!= AS_DEFERRED
)
2368 || (f
->sym
->ts
.type
== BT_CLASS
2369 && CLASS_DATA (f
->sym
)->as
->type
!= AS_DEFERRED
))
2371 gcc_assert (DECL_LANG_SPECIFIC (f
->sym
->backend_decl
) == NULL
2372 || GFC_DECL_TOKEN (f
->sym
->backend_decl
) == NULL_TREE
);
2373 if (DECL_LANG_SPECIFIC (f
->sym
->backend_decl
) == NULL
)
2374 gfc_allocate_lang_decl (f
->sym
->backend_decl
);
2375 GFC_DECL_TOKEN (f
->sym
->backend_decl
) = token
;
2379 gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
) == NULL_TREE
);
2380 GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
) = token
;
2383 DECL_CONTEXT (token
) = fndecl
;
2384 DECL_ARTIFICIAL (token
) = 1;
2385 DECL_ARG_TYPE (token
) = TREE_VALUE (typelist
);
2386 TREE_READONLY (token
) = 1;
2387 hidden_arglist
= chainon (hidden_arglist
, token
);
2388 gfc_finish_decl (token
);
2390 offset
= build_decl (input_location
, PARM_DECL
,
2391 create_tmp_var_name ("caf_offset"),
2392 gfc_array_index_type
);
2394 if ((f
->sym
->ts
.type
!= BT_CLASS
2395 && f
->sym
->as
->type
!= AS_DEFERRED
)
2396 || (f
->sym
->ts
.type
== BT_CLASS
2397 && CLASS_DATA (f
->sym
)->as
->type
!= AS_DEFERRED
))
2399 gcc_assert (GFC_DECL_CAF_OFFSET (f
->sym
->backend_decl
)
2401 GFC_DECL_CAF_OFFSET (f
->sym
->backend_decl
) = offset
;
2405 gcc_assert (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type
) == NULL_TREE
);
2406 GFC_TYPE_ARRAY_CAF_OFFSET (caf_type
) = offset
;
2408 DECL_CONTEXT (offset
) = fndecl
;
2409 DECL_ARTIFICIAL (offset
) = 1;
2410 DECL_ARG_TYPE (offset
) = TREE_VALUE (typelist
);
2411 TREE_READONLY (offset
) = 1;
2412 hidden_arglist
= chainon (hidden_arglist
, offset
);
2413 gfc_finish_decl (offset
);
2416 arglist
= chainon (arglist
, parm
);
2417 typelist
= TREE_CHAIN (typelist
);
2420 /* Add the hidden string length parameters, unless the procedure
2422 if (!sym
->attr
.is_bind_c
)
2423 arglist
= chainon (arglist
, hidden_arglist
);
2425 gcc_assert (hidden_typelist
== NULL_TREE
2426 || TREE_VALUE (hidden_typelist
) == void_type_node
);
2427 DECL_ARGUMENTS (fndecl
) = arglist
;
2430 /* Do the setup necessary before generating the body of a function. */
2433 trans_function_start (gfc_symbol
* sym
)
2437 fndecl
= sym
->backend_decl
;
2439 /* Let GCC know the current scope is this function. */
2440 current_function_decl
= fndecl
;
2442 /* Let the world know what we're about to do. */
2443 announce_function (fndecl
);
2445 if (DECL_FILE_SCOPE_P (fndecl
))
2447 /* Create RTL for function declaration. */
2448 rest_of_decl_compilation (fndecl
, 1, 0);
2451 /* Create RTL for function definition. */
2452 make_decl_rtl (fndecl
);
2454 allocate_struct_function (fndecl
, false);
2456 /* function.c requires a push at the start of the function. */
2460 /* Create thunks for alternate entry points. */
2463 build_entry_thunks (gfc_namespace
* ns
, bool global
)
2465 gfc_formal_arglist
*formal
;
2466 gfc_formal_arglist
*thunk_formal
;
2468 gfc_symbol
*thunk_sym
;
2474 /* This should always be a toplevel function. */
2475 gcc_assert (current_function_decl
== NULL_TREE
);
2477 gfc_save_backend_locus (&old_loc
);
2478 for (el
= ns
->entries
; el
; el
= el
->next
)
2480 vec
<tree
, va_gc
> *args
= NULL
;
2481 vec
<tree
, va_gc
> *string_args
= NULL
;
2483 thunk_sym
= el
->sym
;
2485 build_function_decl (thunk_sym
, global
);
2486 create_function_arglist (thunk_sym
);
2488 trans_function_start (thunk_sym
);
2490 thunk_fndecl
= thunk_sym
->backend_decl
;
2492 gfc_init_block (&body
);
2494 /* Pass extra parameter identifying this entry point. */
2495 tmp
= build_int_cst (gfc_array_index_type
, el
->id
);
2496 vec_safe_push (args
, tmp
);
2498 if (thunk_sym
->attr
.function
)
2500 if (gfc_return_by_reference (ns
->proc_name
))
2502 tree ref
= DECL_ARGUMENTS (current_function_decl
);
2503 vec_safe_push (args
, ref
);
2504 if (ns
->proc_name
->ts
.type
== BT_CHARACTER
)
2505 vec_safe_push (args
, DECL_CHAIN (ref
));
2509 for (formal
= gfc_sym_get_dummy_args (ns
->proc_name
); formal
;
2510 formal
= formal
->next
)
2512 /* Ignore alternate returns. */
2513 if (formal
->sym
== NULL
)
2516 /* We don't have a clever way of identifying arguments, so resort to
2517 a brute-force search. */
2518 for (thunk_formal
= gfc_sym_get_dummy_args (thunk_sym
);
2520 thunk_formal
= thunk_formal
->next
)
2522 if (thunk_formal
->sym
== formal
->sym
)
2528 /* Pass the argument. */
2529 DECL_ARTIFICIAL (thunk_formal
->sym
->backend_decl
) = 1;
2530 vec_safe_push (args
, thunk_formal
->sym
->backend_decl
);
2531 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
2533 tmp
= thunk_formal
->sym
->ts
.u
.cl
->backend_decl
;
2534 vec_safe_push (string_args
, tmp
);
2539 /* Pass NULL for a missing argument. */
2540 vec_safe_push (args
, null_pointer_node
);
2541 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
2543 tmp
= build_int_cst (gfc_charlen_type_node
, 0);
2544 vec_safe_push (string_args
, tmp
);
2549 /* Call the master function. */
2550 vec_safe_splice (args
, string_args
);
2551 tmp
= ns
->proc_name
->backend_decl
;
2552 tmp
= build_call_expr_loc_vec (input_location
, tmp
, args
);
2553 if (ns
->proc_name
->attr
.mixed_entry_master
)
2555 tree union_decl
, field
;
2556 tree master_type
= TREE_TYPE (ns
->proc_name
->backend_decl
);
2558 union_decl
= build_decl (input_location
,
2559 VAR_DECL
, get_identifier ("__result"),
2560 TREE_TYPE (master_type
));
2561 DECL_ARTIFICIAL (union_decl
) = 1;
2562 DECL_EXTERNAL (union_decl
) = 0;
2563 TREE_PUBLIC (union_decl
) = 0;
2564 TREE_USED (union_decl
) = 1;
2565 layout_decl (union_decl
, 0);
2566 pushdecl (union_decl
);
2568 DECL_CONTEXT (union_decl
) = current_function_decl
;
2569 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
2570 TREE_TYPE (union_decl
), union_decl
, tmp
);
2571 gfc_add_expr_to_block (&body
, tmp
);
2573 for (field
= TYPE_FIELDS (TREE_TYPE (union_decl
));
2574 field
; field
= DECL_CHAIN (field
))
2575 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field
)),
2576 thunk_sym
->result
->name
) == 0)
2578 gcc_assert (field
!= NULL_TREE
);
2579 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
,
2580 TREE_TYPE (field
), union_decl
, field
,
2582 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
2583 TREE_TYPE (DECL_RESULT (current_function_decl
)),
2584 DECL_RESULT (current_function_decl
), tmp
);
2585 tmp
= build1_v (RETURN_EXPR
, tmp
);
2587 else if (TREE_TYPE (DECL_RESULT (current_function_decl
))
2590 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
2591 TREE_TYPE (DECL_RESULT (current_function_decl
)),
2592 DECL_RESULT (current_function_decl
), tmp
);
2593 tmp
= build1_v (RETURN_EXPR
, tmp
);
2595 gfc_add_expr_to_block (&body
, tmp
);
2597 /* Finish off this function and send it for code generation. */
2598 DECL_SAVED_TREE (thunk_fndecl
) = gfc_finish_block (&body
);
2601 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl
)) = thunk_fndecl
;
2602 DECL_SAVED_TREE (thunk_fndecl
)
2603 = build3_v (BIND_EXPR
, tmp
, DECL_SAVED_TREE (thunk_fndecl
),
2604 DECL_INITIAL (thunk_fndecl
));
2606 /* Output the GENERIC tree. */
2607 dump_function (TDI_original
, thunk_fndecl
);
2609 /* Store the end of the function, so that we get good line number
2610 info for the epilogue. */
2611 cfun
->function_end_locus
= input_location
;
2613 /* We're leaving the context of this function, so zap cfun.
2614 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
2615 tree_rest_of_compilation. */
2618 current_function_decl
= NULL_TREE
;
2620 cgraph_node::finalize_function (thunk_fndecl
, true);
2622 /* We share the symbols in the formal argument list with other entry
2623 points and the master function. Clear them so that they are
2624 recreated for each function. */
2625 for (formal
= gfc_sym_get_dummy_args (thunk_sym
); formal
;
2626 formal
= formal
->next
)
2627 if (formal
->sym
!= NULL
) /* Ignore alternate returns. */
2629 formal
->sym
->backend_decl
= NULL_TREE
;
2630 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
2631 formal
->sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
2634 if (thunk_sym
->attr
.function
)
2636 if (thunk_sym
->ts
.type
== BT_CHARACTER
)
2637 thunk_sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
2638 if (thunk_sym
->result
->ts
.type
== BT_CHARACTER
)
2639 thunk_sym
->result
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
2643 gfc_restore_backend_locus (&old_loc
);
2647 /* Create a decl for a function, and create any thunks for alternate entry
2648 points. If global is true, generate the function in the global binding
2649 level, otherwise in the current binding level (which can be global). */
2652 gfc_create_function_decl (gfc_namespace
* ns
, bool global
)
2654 /* Create a declaration for the master function. */
2655 build_function_decl (ns
->proc_name
, global
);
2657 /* Compile the entry thunks. */
2659 build_entry_thunks (ns
, global
);
2661 /* Now create the read argument list. */
2662 create_function_arglist (ns
->proc_name
);
2664 if (ns
->omp_declare_simd
)
2665 gfc_trans_omp_declare_simd (ns
);
2668 /* Return the decl used to hold the function return value. If
2669 parent_flag is set, the context is the parent_scope. */
2672 gfc_get_fake_result_decl (gfc_symbol
* sym
, int parent_flag
)
2676 tree this_fake_result_decl
;
2677 tree this_function_decl
;
2679 char name
[GFC_MAX_SYMBOL_LEN
+ 10];
2683 this_fake_result_decl
= parent_fake_result_decl
;
2684 this_function_decl
= DECL_CONTEXT (current_function_decl
);
2688 this_fake_result_decl
= current_fake_result_decl
;
2689 this_function_decl
= current_function_decl
;
2693 && sym
->ns
->proc_name
->backend_decl
== this_function_decl
2694 && sym
->ns
->proc_name
->attr
.entry_master
2695 && sym
!= sym
->ns
->proc_name
)
2698 if (this_fake_result_decl
!= NULL
)
2699 for (t
= TREE_CHAIN (this_fake_result_decl
); t
; t
= TREE_CHAIN (t
))
2700 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t
)), sym
->name
) == 0)
2703 return TREE_VALUE (t
);
2704 decl
= gfc_get_fake_result_decl (sym
->ns
->proc_name
, parent_flag
);
2707 this_fake_result_decl
= parent_fake_result_decl
;
2709 this_fake_result_decl
= current_fake_result_decl
;
2711 if (decl
&& sym
->ns
->proc_name
->attr
.mixed_entry_master
)
2715 for (field
= TYPE_FIELDS (TREE_TYPE (decl
));
2716 field
; field
= DECL_CHAIN (field
))
2717 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field
)),
2721 gcc_assert (field
!= NULL_TREE
);
2722 decl
= fold_build3_loc (input_location
, COMPONENT_REF
,
2723 TREE_TYPE (field
), decl
, field
, NULL_TREE
);
2726 var
= create_tmp_var_raw (TREE_TYPE (decl
), sym
->name
);
2728 gfc_add_decl_to_parent_function (var
);
2730 gfc_add_decl_to_function (var
);
2732 SET_DECL_VALUE_EXPR (var
, decl
);
2733 DECL_HAS_VALUE_EXPR_P (var
) = 1;
2734 GFC_DECL_RESULT (var
) = 1;
2736 TREE_CHAIN (this_fake_result_decl
)
2737 = tree_cons (get_identifier (sym
->name
), var
,
2738 TREE_CHAIN (this_fake_result_decl
));
2742 if (this_fake_result_decl
!= NULL_TREE
)
2743 return TREE_VALUE (this_fake_result_decl
);
2745 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
2750 if (sym
->ts
.type
== BT_CHARACTER
)
2752 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
2753 length
= gfc_create_string_length (sym
);
2755 length
= sym
->ts
.u
.cl
->backend_decl
;
2756 if (TREE_CODE (length
) == VAR_DECL
2757 && DECL_CONTEXT (length
) == NULL_TREE
)
2758 gfc_add_decl_to_function (length
);
2761 if (gfc_return_by_reference (sym
))
2763 decl
= DECL_ARGUMENTS (this_function_decl
);
2765 if (sym
->ns
->proc_name
->backend_decl
== this_function_decl
2766 && sym
->ns
->proc_name
->attr
.entry_master
)
2767 decl
= DECL_CHAIN (decl
);
2769 TREE_USED (decl
) = 1;
2771 decl
= gfc_build_dummy_array_decl (sym
, decl
);
2775 sprintf (name
, "__result_%.20s",
2776 IDENTIFIER_POINTER (DECL_NAME (this_function_decl
)));
2778 if (!sym
->attr
.mixed_entry_master
&& sym
->attr
.function
)
2779 decl
= build_decl (DECL_SOURCE_LOCATION (this_function_decl
),
2780 VAR_DECL
, get_identifier (name
),
2781 gfc_sym_type (sym
));
2783 decl
= build_decl (DECL_SOURCE_LOCATION (this_function_decl
),
2784 VAR_DECL
, get_identifier (name
),
2785 TREE_TYPE (TREE_TYPE (this_function_decl
)));
2786 DECL_ARTIFICIAL (decl
) = 1;
2787 DECL_EXTERNAL (decl
) = 0;
2788 TREE_PUBLIC (decl
) = 0;
2789 TREE_USED (decl
) = 1;
2790 GFC_DECL_RESULT (decl
) = 1;
2791 TREE_ADDRESSABLE (decl
) = 1;
2793 layout_decl (decl
, 0);
2794 gfc_finish_decl_attrs (decl
, &sym
->attr
);
2797 gfc_add_decl_to_parent_function (decl
);
2799 gfc_add_decl_to_function (decl
);
2803 parent_fake_result_decl
= build_tree_list (NULL
, decl
);
2805 current_fake_result_decl
= build_tree_list (NULL
, decl
);
2811 /* Builds a function decl. The remaining parameters are the types of the
2812 function arguments. Negative nargs indicates a varargs function. */
2815 build_library_function_decl_1 (tree name
, const char *spec
,
2816 tree rettype
, int nargs
, va_list p
)
2818 vec
<tree
, va_gc
> *arglist
;
2823 /* Library functions must be declared with global scope. */
2824 gcc_assert (current_function_decl
== NULL_TREE
);
2826 /* Create a list of the argument types. */
2827 vec_alloc (arglist
, abs (nargs
));
2828 for (n
= abs (nargs
); n
> 0; n
--)
2830 tree argtype
= va_arg (p
, tree
);
2831 arglist
->quick_push (argtype
);
2834 /* Build the function type and decl. */
2836 fntype
= build_function_type_vec (rettype
, arglist
);
2838 fntype
= build_varargs_function_type_vec (rettype
, arglist
);
2841 tree attr_args
= build_tree_list (NULL_TREE
,
2842 build_string (strlen (spec
), spec
));
2843 tree attrs
= tree_cons (get_identifier ("fn spec"),
2844 attr_args
, TYPE_ATTRIBUTES (fntype
));
2845 fntype
= build_type_attribute_variant (fntype
, attrs
);
2847 fndecl
= build_decl (input_location
,
2848 FUNCTION_DECL
, name
, fntype
);
2850 /* Mark this decl as external. */
2851 DECL_EXTERNAL (fndecl
) = 1;
2852 TREE_PUBLIC (fndecl
) = 1;
2856 rest_of_decl_compilation (fndecl
, 1, 0);
2861 /* Builds a function decl. The remaining parameters are the types of the
2862 function arguments. Negative nargs indicates a varargs function. */
2865 gfc_build_library_function_decl (tree name
, tree rettype
, int nargs
, ...)
2869 va_start (args
, nargs
);
2870 ret
= build_library_function_decl_1 (name
, NULL
, rettype
, nargs
, args
);
2875 /* Builds a function decl. The remaining parameters are the types of the
2876 function arguments. Negative nargs indicates a varargs function.
2877 The SPEC parameter specifies the function argument and return type
2878 specification according to the fnspec function type attribute. */
2881 gfc_build_library_function_decl_with_spec (tree name
, const char *spec
,
2882 tree rettype
, int nargs
, ...)
2886 va_start (args
, nargs
);
2887 ret
= build_library_function_decl_1 (name
, spec
, rettype
, nargs
, args
);
2893 gfc_build_intrinsic_function_decls (void)
2895 tree gfc_int4_type_node
= gfc_get_int_type (4);
2896 tree gfc_pint4_type_node
= build_pointer_type (gfc_int4_type_node
);
2897 tree gfc_int8_type_node
= gfc_get_int_type (8);
2898 tree gfc_pint8_type_node
= build_pointer_type (gfc_int8_type_node
);
2899 tree gfc_int16_type_node
= gfc_get_int_type (16);
2900 tree gfc_logical4_type_node
= gfc_get_logical_type (4);
2901 tree pchar1_type_node
= gfc_get_pchar_type (1);
2902 tree pchar4_type_node
= gfc_get_pchar_type (4);
2904 /* String functions. */
2905 gfor_fndecl_compare_string
= gfc_build_library_function_decl_with_spec (
2906 get_identifier (PREFIX("compare_string")), "..R.R",
2907 integer_type_node
, 4, gfc_charlen_type_node
, pchar1_type_node
,
2908 gfc_charlen_type_node
, pchar1_type_node
);
2909 DECL_PURE_P (gfor_fndecl_compare_string
) = 1;
2910 TREE_NOTHROW (gfor_fndecl_compare_string
) = 1;
2912 gfor_fndecl_concat_string
= gfc_build_library_function_decl_with_spec (
2913 get_identifier (PREFIX("concat_string")), "..W.R.R",
2914 void_type_node
, 6, gfc_charlen_type_node
, pchar1_type_node
,
2915 gfc_charlen_type_node
, pchar1_type_node
,
2916 gfc_charlen_type_node
, pchar1_type_node
);
2917 TREE_NOTHROW (gfor_fndecl_concat_string
) = 1;
2919 gfor_fndecl_string_len_trim
= gfc_build_library_function_decl_with_spec (
2920 get_identifier (PREFIX("string_len_trim")), "..R",
2921 gfc_charlen_type_node
, 2, gfc_charlen_type_node
, pchar1_type_node
);
2922 DECL_PURE_P (gfor_fndecl_string_len_trim
) = 1;
2923 TREE_NOTHROW (gfor_fndecl_string_len_trim
) = 1;
2925 gfor_fndecl_string_index
= gfc_build_library_function_decl_with_spec (
2926 get_identifier (PREFIX("string_index")), "..R.R.",
2927 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
2928 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
2929 DECL_PURE_P (gfor_fndecl_string_index
) = 1;
2930 TREE_NOTHROW (gfor_fndecl_string_index
) = 1;
2932 gfor_fndecl_string_scan
= gfc_build_library_function_decl_with_spec (
2933 get_identifier (PREFIX("string_scan")), "..R.R.",
2934 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
2935 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
2936 DECL_PURE_P (gfor_fndecl_string_scan
) = 1;
2937 TREE_NOTHROW (gfor_fndecl_string_scan
) = 1;
2939 gfor_fndecl_string_verify
= gfc_build_library_function_decl_with_spec (
2940 get_identifier (PREFIX("string_verify")), "..R.R.",
2941 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
2942 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
2943 DECL_PURE_P (gfor_fndecl_string_verify
) = 1;
2944 TREE_NOTHROW (gfor_fndecl_string_verify
) = 1;
2946 gfor_fndecl_string_trim
= gfc_build_library_function_decl_with_spec (
2947 get_identifier (PREFIX("string_trim")), ".Ww.R",
2948 void_type_node
, 4, build_pointer_type (gfc_charlen_type_node
),
2949 build_pointer_type (pchar1_type_node
), gfc_charlen_type_node
,
2952 gfor_fndecl_string_minmax
= gfc_build_library_function_decl_with_spec (
2953 get_identifier (PREFIX("string_minmax")), ".Ww.R",
2954 void_type_node
, -4, build_pointer_type (gfc_charlen_type_node
),
2955 build_pointer_type (pchar1_type_node
), integer_type_node
,
2958 gfor_fndecl_adjustl
= gfc_build_library_function_decl_with_spec (
2959 get_identifier (PREFIX("adjustl")), ".W.R",
2960 void_type_node
, 3, pchar1_type_node
, gfc_charlen_type_node
,
2962 TREE_NOTHROW (gfor_fndecl_adjustl
) = 1;
2964 gfor_fndecl_adjustr
= gfc_build_library_function_decl_with_spec (
2965 get_identifier (PREFIX("adjustr")), ".W.R",
2966 void_type_node
, 3, pchar1_type_node
, gfc_charlen_type_node
,
2968 TREE_NOTHROW (gfor_fndecl_adjustr
) = 1;
2970 gfor_fndecl_select_string
= gfc_build_library_function_decl_with_spec (
2971 get_identifier (PREFIX("select_string")), ".R.R.",
2972 integer_type_node
, 4, pvoid_type_node
, integer_type_node
,
2973 pchar1_type_node
, gfc_charlen_type_node
);
2974 DECL_PURE_P (gfor_fndecl_select_string
) = 1;
2975 TREE_NOTHROW (gfor_fndecl_select_string
) = 1;
2977 gfor_fndecl_compare_string_char4
= gfc_build_library_function_decl_with_spec (
2978 get_identifier (PREFIX("compare_string_char4")), "..R.R",
2979 integer_type_node
, 4, gfc_charlen_type_node
, pchar4_type_node
,
2980 gfc_charlen_type_node
, pchar4_type_node
);
2981 DECL_PURE_P (gfor_fndecl_compare_string_char4
) = 1;
2982 TREE_NOTHROW (gfor_fndecl_compare_string_char4
) = 1;
2984 gfor_fndecl_concat_string_char4
= gfc_build_library_function_decl_with_spec (
2985 get_identifier (PREFIX("concat_string_char4")), "..W.R.R",
2986 void_type_node
, 6, gfc_charlen_type_node
, pchar4_type_node
,
2987 gfc_charlen_type_node
, pchar4_type_node
, gfc_charlen_type_node
,
2989 TREE_NOTHROW (gfor_fndecl_concat_string_char4
) = 1;
2991 gfor_fndecl_string_len_trim_char4
= gfc_build_library_function_decl_with_spec (
2992 get_identifier (PREFIX("string_len_trim_char4")), "..R",
2993 gfc_charlen_type_node
, 2, gfc_charlen_type_node
, pchar4_type_node
);
2994 DECL_PURE_P (gfor_fndecl_string_len_trim_char4
) = 1;
2995 TREE_NOTHROW (gfor_fndecl_string_len_trim_char4
) = 1;
2997 gfor_fndecl_string_index_char4
= gfc_build_library_function_decl_with_spec (
2998 get_identifier (PREFIX("string_index_char4")), "..R.R.",
2999 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
3000 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
3001 DECL_PURE_P (gfor_fndecl_string_index_char4
) = 1;
3002 TREE_NOTHROW (gfor_fndecl_string_index_char4
) = 1;
3004 gfor_fndecl_string_scan_char4
= gfc_build_library_function_decl_with_spec (
3005 get_identifier (PREFIX("string_scan_char4")), "..R.R.",
3006 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
3007 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
3008 DECL_PURE_P (gfor_fndecl_string_scan_char4
) = 1;
3009 TREE_NOTHROW (gfor_fndecl_string_scan_char4
) = 1;
3011 gfor_fndecl_string_verify_char4
= gfc_build_library_function_decl_with_spec (
3012 get_identifier (PREFIX("string_verify_char4")), "..R.R.",
3013 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
3014 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
3015 DECL_PURE_P (gfor_fndecl_string_verify_char4
) = 1;
3016 TREE_NOTHROW (gfor_fndecl_string_verify_char4
) = 1;
3018 gfor_fndecl_string_trim_char4
= gfc_build_library_function_decl_with_spec (
3019 get_identifier (PREFIX("string_trim_char4")), ".Ww.R",
3020 void_type_node
, 4, build_pointer_type (gfc_charlen_type_node
),
3021 build_pointer_type (pchar4_type_node
), gfc_charlen_type_node
,
3024 gfor_fndecl_string_minmax_char4
= gfc_build_library_function_decl_with_spec (
3025 get_identifier (PREFIX("string_minmax_char4")), ".Ww.R",
3026 void_type_node
, -4, build_pointer_type (gfc_charlen_type_node
),
3027 build_pointer_type (pchar4_type_node
), integer_type_node
,
3030 gfor_fndecl_adjustl_char4
= gfc_build_library_function_decl_with_spec (
3031 get_identifier (PREFIX("adjustl_char4")), ".W.R",
3032 void_type_node
, 3, pchar4_type_node
, gfc_charlen_type_node
,
3034 TREE_NOTHROW (gfor_fndecl_adjustl_char4
) = 1;
3036 gfor_fndecl_adjustr_char4
= gfc_build_library_function_decl_with_spec (
3037 get_identifier (PREFIX("adjustr_char4")), ".W.R",
3038 void_type_node
, 3, pchar4_type_node
, gfc_charlen_type_node
,
3040 TREE_NOTHROW (gfor_fndecl_adjustr_char4
) = 1;
3042 gfor_fndecl_select_string_char4
= gfc_build_library_function_decl_with_spec (
3043 get_identifier (PREFIX("select_string_char4")), ".R.R.",
3044 integer_type_node
, 4, pvoid_type_node
, integer_type_node
,
3045 pvoid_type_node
, gfc_charlen_type_node
);
3046 DECL_PURE_P (gfor_fndecl_select_string_char4
) = 1;
3047 TREE_NOTHROW (gfor_fndecl_select_string_char4
) = 1;
3050 /* Conversion between character kinds. */
3052 gfor_fndecl_convert_char1_to_char4
= gfc_build_library_function_decl_with_spec (
3053 get_identifier (PREFIX("convert_char1_to_char4")), ".w.R",
3054 void_type_node
, 3, build_pointer_type (pchar4_type_node
),
3055 gfc_charlen_type_node
, pchar1_type_node
);
3057 gfor_fndecl_convert_char4_to_char1
= gfc_build_library_function_decl_with_spec (
3058 get_identifier (PREFIX("convert_char4_to_char1")), ".w.R",
3059 void_type_node
, 3, build_pointer_type (pchar1_type_node
),
3060 gfc_charlen_type_node
, pchar4_type_node
);
3062 /* Misc. functions. */
3064 gfor_fndecl_ttynam
= gfc_build_library_function_decl_with_spec (
3065 get_identifier (PREFIX("ttynam")), ".W",
3066 void_type_node
, 3, pchar_type_node
, gfc_charlen_type_node
,
3069 gfor_fndecl_fdate
= gfc_build_library_function_decl_with_spec (
3070 get_identifier (PREFIX("fdate")), ".W",
3071 void_type_node
, 2, pchar_type_node
, gfc_charlen_type_node
);
3073 gfor_fndecl_ctime
= gfc_build_library_function_decl_with_spec (
3074 get_identifier (PREFIX("ctime")), ".W",
3075 void_type_node
, 3, pchar_type_node
, gfc_charlen_type_node
,
3076 gfc_int8_type_node
);
3078 gfor_fndecl_sc_kind
= gfc_build_library_function_decl_with_spec (
3079 get_identifier (PREFIX("selected_char_kind")), "..R",
3080 gfc_int4_type_node
, 2, gfc_charlen_type_node
, pchar_type_node
);
3081 DECL_PURE_P (gfor_fndecl_sc_kind
) = 1;
3082 TREE_NOTHROW (gfor_fndecl_sc_kind
) = 1;
3084 gfor_fndecl_si_kind
= gfc_build_library_function_decl_with_spec (
3085 get_identifier (PREFIX("selected_int_kind")), ".R",
3086 gfc_int4_type_node
, 1, pvoid_type_node
);
3087 DECL_PURE_P (gfor_fndecl_si_kind
) = 1;
3088 TREE_NOTHROW (gfor_fndecl_si_kind
) = 1;
3090 gfor_fndecl_sr_kind
= gfc_build_library_function_decl_with_spec (
3091 get_identifier (PREFIX("selected_real_kind2008")), ".RR",
3092 gfc_int4_type_node
, 3, pvoid_type_node
, pvoid_type_node
,
3094 DECL_PURE_P (gfor_fndecl_sr_kind
) = 1;
3095 TREE_NOTHROW (gfor_fndecl_sr_kind
) = 1;
3097 gfor_fndecl_system_clock4
= gfc_build_library_function_decl (
3098 get_identifier (PREFIX("system_clock_4")),
3099 void_type_node
, 3, gfc_pint4_type_node
, gfc_pint4_type_node
,
3100 gfc_pint4_type_node
);
3102 gfor_fndecl_system_clock8
= gfc_build_library_function_decl (
3103 get_identifier (PREFIX("system_clock_8")),
3104 void_type_node
, 3, gfc_pint8_type_node
, gfc_pint8_type_node
,
3105 gfc_pint8_type_node
);
3107 /* Power functions. */
3109 tree ctype
, rtype
, itype
, jtype
;
3110 int rkind
, ikind
, jkind
;
3113 static int ikinds
[NIKINDS
] = {4, 8, 16};
3114 static int rkinds
[NRKINDS
] = {4, 8, 10, 16};
3115 char name
[PREFIX_LEN
+ 12]; /* _gfortran_pow_?n_?n */
3117 for (ikind
=0; ikind
< NIKINDS
; ikind
++)
3119 itype
= gfc_get_int_type (ikinds
[ikind
]);
3121 for (jkind
=0; jkind
< NIKINDS
; jkind
++)
3123 jtype
= gfc_get_int_type (ikinds
[jkind
]);
3126 sprintf(name
, PREFIX("pow_i%d_i%d"), ikinds
[ikind
],
3128 gfor_fndecl_math_powi
[jkind
][ikind
].integer
=
3129 gfc_build_library_function_decl (get_identifier (name
),
3130 jtype
, 2, jtype
, itype
);
3131 TREE_READONLY (gfor_fndecl_math_powi
[jkind
][ikind
].integer
) = 1;
3132 TREE_NOTHROW (gfor_fndecl_math_powi
[jkind
][ikind
].integer
) = 1;
3136 for (rkind
= 0; rkind
< NRKINDS
; rkind
++)
3138 rtype
= gfc_get_real_type (rkinds
[rkind
]);
3141 sprintf(name
, PREFIX("pow_r%d_i%d"), rkinds
[rkind
],
3143 gfor_fndecl_math_powi
[rkind
][ikind
].real
=
3144 gfc_build_library_function_decl (get_identifier (name
),
3145 rtype
, 2, rtype
, itype
);
3146 TREE_READONLY (gfor_fndecl_math_powi
[rkind
][ikind
].real
) = 1;
3147 TREE_NOTHROW (gfor_fndecl_math_powi
[rkind
][ikind
].real
) = 1;
3150 ctype
= gfc_get_complex_type (rkinds
[rkind
]);
3153 sprintf(name
, PREFIX("pow_c%d_i%d"), rkinds
[rkind
],
3155 gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
=
3156 gfc_build_library_function_decl (get_identifier (name
),
3157 ctype
, 2,ctype
, itype
);
3158 TREE_READONLY (gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
) = 1;
3159 TREE_NOTHROW (gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
) = 1;
3167 gfor_fndecl_math_ishftc4
= gfc_build_library_function_decl (
3168 get_identifier (PREFIX("ishftc4")),
3169 gfc_int4_type_node
, 3, gfc_int4_type_node
, gfc_int4_type_node
,
3170 gfc_int4_type_node
);
3171 TREE_READONLY (gfor_fndecl_math_ishftc4
) = 1;
3172 TREE_NOTHROW (gfor_fndecl_math_ishftc4
) = 1;
3174 gfor_fndecl_math_ishftc8
= gfc_build_library_function_decl (
3175 get_identifier (PREFIX("ishftc8")),
3176 gfc_int8_type_node
, 3, gfc_int8_type_node
, gfc_int4_type_node
,
3177 gfc_int4_type_node
);
3178 TREE_READONLY (gfor_fndecl_math_ishftc8
) = 1;
3179 TREE_NOTHROW (gfor_fndecl_math_ishftc8
) = 1;
3181 if (gfc_int16_type_node
)
3183 gfor_fndecl_math_ishftc16
= gfc_build_library_function_decl (
3184 get_identifier (PREFIX("ishftc16")),
3185 gfc_int16_type_node
, 3, gfc_int16_type_node
, gfc_int4_type_node
,
3186 gfc_int4_type_node
);
3187 TREE_READONLY (gfor_fndecl_math_ishftc16
) = 1;
3188 TREE_NOTHROW (gfor_fndecl_math_ishftc16
) = 1;
3191 /* BLAS functions. */
3193 tree pint
= build_pointer_type (integer_type_node
);
3194 tree ps
= build_pointer_type (gfc_get_real_type (gfc_default_real_kind
));
3195 tree pd
= build_pointer_type (gfc_get_real_type (gfc_default_double_kind
));
3196 tree pc
= build_pointer_type (gfc_get_complex_type (gfc_default_real_kind
));
3197 tree pz
= build_pointer_type
3198 (gfc_get_complex_type (gfc_default_double_kind
));
3200 gfor_fndecl_sgemm
= gfc_build_library_function_decl
3202 (flag_underscoring
? "sgemm_" : "sgemm"),
3203 void_type_node
, 15, pchar_type_node
,
3204 pchar_type_node
, pint
, pint
, pint
, ps
, ps
, pint
,
3205 ps
, pint
, ps
, ps
, pint
, integer_type_node
,
3207 gfor_fndecl_dgemm
= gfc_build_library_function_decl
3209 (flag_underscoring
? "dgemm_" : "dgemm"),
3210 void_type_node
, 15, pchar_type_node
,
3211 pchar_type_node
, pint
, pint
, pint
, pd
, pd
, pint
,
3212 pd
, pint
, pd
, pd
, pint
, integer_type_node
,
3214 gfor_fndecl_cgemm
= gfc_build_library_function_decl
3216 (flag_underscoring
? "cgemm_" : "cgemm"),
3217 void_type_node
, 15, pchar_type_node
,
3218 pchar_type_node
, pint
, pint
, pint
, pc
, pc
, pint
,
3219 pc
, pint
, pc
, pc
, pint
, integer_type_node
,
3221 gfor_fndecl_zgemm
= gfc_build_library_function_decl
3223 (flag_underscoring
? "zgemm_" : "zgemm"),
3224 void_type_node
, 15, pchar_type_node
,
3225 pchar_type_node
, pint
, pint
, pint
, pz
, pz
, pint
,
3226 pz
, pint
, pz
, pz
, pint
, integer_type_node
,
3230 /* Other functions. */
3231 gfor_fndecl_size0
= gfc_build_library_function_decl_with_spec (
3232 get_identifier (PREFIX("size0")), ".R",
3233 gfc_array_index_type
, 1, pvoid_type_node
);
3234 DECL_PURE_P (gfor_fndecl_size0
) = 1;
3235 TREE_NOTHROW (gfor_fndecl_size0
) = 1;
3237 gfor_fndecl_size1
= gfc_build_library_function_decl_with_spec (
3238 get_identifier (PREFIX("size1")), ".R",
3239 gfc_array_index_type
, 2, pvoid_type_node
, gfc_array_index_type
);
3240 DECL_PURE_P (gfor_fndecl_size1
) = 1;
3241 TREE_NOTHROW (gfor_fndecl_size1
) = 1;
3243 gfor_fndecl_iargc
= gfc_build_library_function_decl (
3244 get_identifier (PREFIX ("iargc")), gfc_int4_type_node
, 0);
3245 TREE_NOTHROW (gfor_fndecl_iargc
) = 1;
3249 /* Make prototypes for runtime library functions. */
3252 gfc_build_builtin_function_decls (void)
3254 tree gfc_int4_type_node
= gfc_get_int_type (4);
3256 gfor_fndecl_stop_numeric
= gfc_build_library_function_decl (
3257 get_identifier (PREFIX("stop_numeric")),
3258 void_type_node
, 1, gfc_int4_type_node
);
3259 /* STOP doesn't return. */
3260 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric
) = 1;
3262 gfor_fndecl_stop_numeric_f08
= gfc_build_library_function_decl (
3263 get_identifier (PREFIX("stop_numeric_f08")),
3264 void_type_node
, 1, gfc_int4_type_node
);
3265 /* STOP doesn't return. */
3266 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric_f08
) = 1;
3268 gfor_fndecl_stop_string
= gfc_build_library_function_decl_with_spec (
3269 get_identifier (PREFIX("stop_string")), ".R.",
3270 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
3271 /* STOP doesn't return. */
3272 TREE_THIS_VOLATILE (gfor_fndecl_stop_string
) = 1;
3274 gfor_fndecl_error_stop_numeric
= gfc_build_library_function_decl (
3275 get_identifier (PREFIX("error_stop_numeric")),
3276 void_type_node
, 1, gfc_int4_type_node
);
3277 /* ERROR STOP doesn't return. */
3278 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_numeric
) = 1;
3280 gfor_fndecl_error_stop_string
= gfc_build_library_function_decl_with_spec (
3281 get_identifier (PREFIX("error_stop_string")), ".R.",
3282 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
3283 /* ERROR STOP doesn't return. */
3284 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_string
) = 1;
3286 gfor_fndecl_pause_numeric
= gfc_build_library_function_decl (
3287 get_identifier (PREFIX("pause_numeric")),
3288 void_type_node
, 1, gfc_int4_type_node
);
3290 gfor_fndecl_pause_string
= gfc_build_library_function_decl_with_spec (
3291 get_identifier (PREFIX("pause_string")), ".R.",
3292 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
3294 gfor_fndecl_runtime_error
= gfc_build_library_function_decl_with_spec (
3295 get_identifier (PREFIX("runtime_error")), ".R",
3296 void_type_node
, -1, pchar_type_node
);
3297 /* The runtime_error function does not return. */
3298 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error
) = 1;
3300 gfor_fndecl_runtime_error_at
= gfc_build_library_function_decl_with_spec (
3301 get_identifier (PREFIX("runtime_error_at")), ".RR",
3302 void_type_node
, -2, pchar_type_node
, pchar_type_node
);
3303 /* The runtime_error_at function does not return. */
3304 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at
) = 1;
3306 gfor_fndecl_runtime_warning_at
= gfc_build_library_function_decl_with_spec (
3307 get_identifier (PREFIX("runtime_warning_at")), ".RR",
3308 void_type_node
, -2, pchar_type_node
, pchar_type_node
);
3310 gfor_fndecl_generate_error
= gfc_build_library_function_decl_with_spec (
3311 get_identifier (PREFIX("generate_error")), ".R.R",
3312 void_type_node
, 3, pvoid_type_node
, integer_type_node
,
3315 gfor_fndecl_os_error
= gfc_build_library_function_decl_with_spec (
3316 get_identifier (PREFIX("os_error")), ".R",
3317 void_type_node
, 1, pchar_type_node
);
3318 /* The runtime_error function does not return. */
3319 TREE_THIS_VOLATILE (gfor_fndecl_os_error
) = 1;
3321 gfor_fndecl_set_args
= gfc_build_library_function_decl (
3322 get_identifier (PREFIX("set_args")),
3323 void_type_node
, 2, integer_type_node
,
3324 build_pointer_type (pchar_type_node
));
3326 gfor_fndecl_set_fpe
= gfc_build_library_function_decl (
3327 get_identifier (PREFIX("set_fpe")),
3328 void_type_node
, 1, integer_type_node
);
3330 gfor_fndecl_ieee_procedure_entry
= gfc_build_library_function_decl (
3331 get_identifier (PREFIX("ieee_procedure_entry")),
3332 void_type_node
, 1, pvoid_type_node
);
3334 gfor_fndecl_ieee_procedure_exit
= gfc_build_library_function_decl (
3335 get_identifier (PREFIX("ieee_procedure_exit")),
3336 void_type_node
, 1, pvoid_type_node
);
3338 /* Keep the array dimension in sync with the call, later in this file. */
3339 gfor_fndecl_set_options
= gfc_build_library_function_decl_with_spec (
3340 get_identifier (PREFIX("set_options")), "..R",
3341 void_type_node
, 2, integer_type_node
,
3342 build_pointer_type (integer_type_node
));
3344 gfor_fndecl_set_convert
= gfc_build_library_function_decl (
3345 get_identifier (PREFIX("set_convert")),
3346 void_type_node
, 1, integer_type_node
);
3348 gfor_fndecl_set_record_marker
= gfc_build_library_function_decl (
3349 get_identifier (PREFIX("set_record_marker")),
3350 void_type_node
, 1, integer_type_node
);
3352 gfor_fndecl_set_max_subrecord_length
= gfc_build_library_function_decl (
3353 get_identifier (PREFIX("set_max_subrecord_length")),
3354 void_type_node
, 1, integer_type_node
);
3356 gfor_fndecl_in_pack
= gfc_build_library_function_decl_with_spec (
3357 get_identifier (PREFIX("internal_pack")), ".r",
3358 pvoid_type_node
, 1, pvoid_type_node
);
3360 gfor_fndecl_in_unpack
= gfc_build_library_function_decl_with_spec (
3361 get_identifier (PREFIX("internal_unpack")), ".wR",
3362 void_type_node
, 2, pvoid_type_node
, pvoid_type_node
);
3364 gfor_fndecl_associated
= gfc_build_library_function_decl_with_spec (
3365 get_identifier (PREFIX("associated")), ".RR",
3366 integer_type_node
, 2, ppvoid_type_node
, ppvoid_type_node
);
3367 DECL_PURE_P (gfor_fndecl_associated
) = 1;
3368 TREE_NOTHROW (gfor_fndecl_associated
) = 1;
3370 /* Coarray library calls. */
3371 if (flag_coarray
== GFC_FCOARRAY_LIB
)
3373 tree pint_type
, pppchar_type
;
3375 pint_type
= build_pointer_type (integer_type_node
);
3377 = build_pointer_type (build_pointer_type (pchar_type_node
));
3379 gfor_fndecl_caf_init
= gfc_build_library_function_decl (
3380 get_identifier (PREFIX("caf_init")), void_type_node
,
3381 2, pint_type
, pppchar_type
);
3383 gfor_fndecl_caf_finalize
= gfc_build_library_function_decl (
3384 get_identifier (PREFIX("caf_finalize")), void_type_node
, 0);
3386 gfor_fndecl_caf_this_image
= gfc_build_library_function_decl (
3387 get_identifier (PREFIX("caf_this_image")), integer_type_node
,
3388 1, integer_type_node
);
3390 gfor_fndecl_caf_num_images
= gfc_build_library_function_decl (
3391 get_identifier (PREFIX("caf_num_images")), integer_type_node
,
3392 2, integer_type_node
, integer_type_node
);
3394 gfor_fndecl_caf_register
= gfc_build_library_function_decl_with_spec (
3395 get_identifier (PREFIX("caf_register")), "...WWW", pvoid_type_node
, 6,
3396 size_type_node
, integer_type_node
, ppvoid_type_node
, pint_type
,
3397 pchar_type_node
, integer_type_node
);
3399 gfor_fndecl_caf_deregister
= gfc_build_library_function_decl_with_spec (
3400 get_identifier (PREFIX("caf_deregister")), ".WWW", void_type_node
, 4,
3401 ppvoid_type_node
, pint_type
, pchar_type_node
, integer_type_node
);
3403 gfor_fndecl_caf_get
= gfc_build_library_function_decl_with_spec (
3404 get_identifier (PREFIX("caf_get")), ".R.RRRW", void_type_node
, 9,
3405 pvoid_type_node
, size_type_node
, integer_type_node
, pvoid_type_node
,
3406 pvoid_type_node
, pvoid_type_node
, integer_type_node
, integer_type_node
,
3409 gfor_fndecl_caf_send
= gfc_build_library_function_decl_with_spec (
3410 get_identifier (PREFIX("caf_send")), ".R.RRRR", void_type_node
, 9,
3411 pvoid_type_node
, size_type_node
, integer_type_node
, pvoid_type_node
,
3412 pvoid_type_node
, pvoid_type_node
, integer_type_node
, integer_type_node
,
3415 gfor_fndecl_caf_sendget
= gfc_build_library_function_decl_with_spec (
3416 get_identifier (PREFIX("caf_sendget")), ".R.RRRR.RRR", void_type_node
,
3417 13, pvoid_type_node
, size_type_node
, integer_type_node
, pvoid_type_node
,
3418 pvoid_type_node
, pvoid_type_node
, size_type_node
, integer_type_node
,
3419 pvoid_type_node
, pvoid_type_node
, integer_type_node
, integer_type_node
,
3422 gfor_fndecl_caf_sync_all
= gfc_build_library_function_decl_with_spec (
3423 get_identifier (PREFIX("caf_sync_all")), ".WW", void_type_node
,
3424 3, pint_type
, pchar_type_node
, integer_type_node
);
3426 gfor_fndecl_caf_sync_images
= gfc_build_library_function_decl_with_spec (
3427 get_identifier (PREFIX("caf_sync_images")), ".RRWW", void_type_node
,
3428 5, integer_type_node
, pint_type
, pint_type
,
3429 pchar_type_node
, integer_type_node
);
3431 gfor_fndecl_caf_error_stop
= gfc_build_library_function_decl (
3432 get_identifier (PREFIX("caf_error_stop")),
3433 void_type_node
, 1, gfc_int4_type_node
);
3434 /* CAF's ERROR STOP doesn't return. */
3435 TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop
) = 1;
3437 gfor_fndecl_caf_error_stop_str
= gfc_build_library_function_decl_with_spec (
3438 get_identifier (PREFIX("caf_error_stop_str")), ".R.",
3439 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
3440 /* CAF's ERROR STOP doesn't return. */
3441 TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop_str
) = 1;
3443 gfor_fndecl_caf_atomic_def
= gfc_build_library_function_decl_with_spec (
3444 get_identifier (PREFIX("caf_atomic_define")), "R..RW",
3445 void_type_node
, 7, pvoid_type_node
, size_type_node
, integer_type_node
,
3446 pvoid_type_node
, pint_type
, integer_type_node
, integer_type_node
);
3448 gfor_fndecl_caf_atomic_ref
= gfc_build_library_function_decl_with_spec (
3449 get_identifier (PREFIX("caf_atomic_ref")), "R..WW",
3450 void_type_node
, 7, pvoid_type_node
, size_type_node
, integer_type_node
,
3451 pvoid_type_node
, pint_type
, integer_type_node
, integer_type_node
);
3453 gfor_fndecl_caf_atomic_cas
= gfc_build_library_function_decl_with_spec (
3454 get_identifier (PREFIX("caf_atomic_cas")), "R..WRRW",
3455 void_type_node
, 9, pvoid_type_node
, size_type_node
, integer_type_node
,
3456 pvoid_type_node
, pvoid_type_node
, pvoid_type_node
, pint_type
,
3457 integer_type_node
, integer_type_node
);
3459 gfor_fndecl_caf_atomic_op
= gfc_build_library_function_decl_with_spec (
3460 get_identifier (PREFIX("caf_atomic_op")), ".R..RWW",
3461 void_type_node
, 9, integer_type_node
, pvoid_type_node
, size_type_node
,
3462 integer_type_node
, pvoid_type_node
, pvoid_type_node
, pint_type
,
3463 integer_type_node
, integer_type_node
);
3465 gfor_fndecl_caf_lock
= gfc_build_library_function_decl_with_spec (
3466 get_identifier (PREFIX("caf_lock")), "R..WWW",
3467 void_type_node
, 7, pvoid_type_node
, size_type_node
, integer_type_node
,
3468 pint_type
, pint_type
, pchar_type_node
, integer_type_node
);
3470 gfor_fndecl_caf_unlock
= gfc_build_library_function_decl_with_spec (
3471 get_identifier (PREFIX("caf_unlock")), "R..WW",
3472 void_type_node
, 6, pvoid_type_node
, size_type_node
, integer_type_node
,
3473 pint_type
, pchar_type_node
, integer_type_node
);
3475 gfor_fndecl_co_broadcast
= gfc_build_library_function_decl_with_spec (
3476 get_identifier (PREFIX("caf_co_broadcast")), "W.WW",
3477 void_type_node
, 5, pvoid_type_node
, integer_type_node
,
3478 pint_type
, pchar_type_node
, integer_type_node
);
3480 gfor_fndecl_co_max
= gfc_build_library_function_decl_with_spec (
3481 get_identifier (PREFIX("caf_co_max")), "W.WW",
3482 void_type_node
, 6, pvoid_type_node
, integer_type_node
,
3483 pint_type
, pchar_type_node
, integer_type_node
, integer_type_node
);
3485 gfor_fndecl_co_min
= gfc_build_library_function_decl_with_spec (
3486 get_identifier (PREFIX("caf_co_min")), "W.WW",
3487 void_type_node
, 6, pvoid_type_node
, integer_type_node
,
3488 pint_type
, pchar_type_node
, integer_type_node
, integer_type_node
);
3490 gfor_fndecl_co_reduce
= gfc_build_library_function_decl_with_spec (
3491 get_identifier (PREFIX("caf_co_reduce")), "W.R.WW",
3492 void_type_node
, 8, pvoid_type_node
,
3493 build_pointer_type (build_varargs_function_type_list (void_type_node
,
3495 integer_type_node
, integer_type_node
, pint_type
, pchar_type_node
,
3496 integer_type_node
, integer_type_node
);
3498 gfor_fndecl_co_sum
= gfc_build_library_function_decl_with_spec (
3499 get_identifier (PREFIX("caf_co_sum")), "W.WW",
3500 void_type_node
, 5, pvoid_type_node
, integer_type_node
,
3501 pint_type
, pchar_type_node
, integer_type_node
);
3504 gfc_build_intrinsic_function_decls ();
3505 gfc_build_intrinsic_lib_fndecls ();
3506 gfc_build_io_library_fndecls ();
3510 /* Evaluate the length of dummy character variables. */
3513 gfc_trans_dummy_character (gfc_symbol
*sym
, gfc_charlen
*cl
,
3514 gfc_wrapped_block
*block
)
3518 gfc_finish_decl (cl
->backend_decl
);
3520 gfc_start_block (&init
);
3522 /* Evaluate the string length expression. */
3523 gfc_conv_string_length (cl
, NULL
, &init
);
3525 gfc_trans_vla_type_sizes (sym
, &init
);
3527 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
3531 /* Allocate and cleanup an automatic character variable. */
3534 gfc_trans_auto_character_variable (gfc_symbol
* sym
, gfc_wrapped_block
* block
)
3540 gcc_assert (sym
->backend_decl
);
3541 gcc_assert (sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
);
3543 gfc_init_block (&init
);
3545 /* Evaluate the string length expression. */
3546 gfc_conv_string_length (sym
->ts
.u
.cl
, NULL
, &init
);
3548 gfc_trans_vla_type_sizes (sym
, &init
);
3550 decl
= sym
->backend_decl
;
3552 /* Emit a DECL_EXPR for this variable, which will cause the
3553 gimplifier to allocate storage, and all that good stuff. */
3554 tmp
= fold_build1_loc (input_location
, DECL_EXPR
, TREE_TYPE (decl
), decl
);
3555 gfc_add_expr_to_block (&init
, tmp
);
3557 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
3560 /* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
3563 gfc_trans_assign_aux_var (gfc_symbol
* sym
, gfc_wrapped_block
* block
)
3567 gcc_assert (sym
->backend_decl
);
3568 gfc_start_block (&init
);
3570 /* Set the initial value to length. See the comments in
3571 function gfc_add_assign_aux_vars in this file. */
3572 gfc_add_modify (&init
, GFC_DECL_STRING_LEN (sym
->backend_decl
),
3573 build_int_cst (gfc_charlen_type_node
, -2));
3575 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
3579 gfc_trans_vla_one_sizepos (tree
*tp
, stmtblock_t
*body
)
3581 tree t
= *tp
, var
, val
;
3583 if (t
== NULL
|| t
== error_mark_node
)
3585 if (TREE_CONSTANT (t
) || DECL_P (t
))
3588 if (TREE_CODE (t
) == SAVE_EXPR
)
3590 if (SAVE_EXPR_RESOLVED_P (t
))
3592 *tp
= TREE_OPERAND (t
, 0);
3595 val
= TREE_OPERAND (t
, 0);
3600 var
= gfc_create_var_np (TREE_TYPE (t
), NULL
);
3601 gfc_add_decl_to_function (var
);
3602 gfc_add_modify (body
, var
, val
);
3603 if (TREE_CODE (t
) == SAVE_EXPR
)
3604 TREE_OPERAND (t
, 0) = var
;
3609 gfc_trans_vla_type_sizes_1 (tree type
, stmtblock_t
*body
)
3613 if (type
== NULL
|| type
== error_mark_node
)
3616 type
= TYPE_MAIN_VARIANT (type
);
3618 if (TREE_CODE (type
) == INTEGER_TYPE
)
3620 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type
), body
);
3621 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type
), body
);
3623 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
3625 TYPE_MIN_VALUE (t
) = TYPE_MIN_VALUE (type
);
3626 TYPE_MAX_VALUE (t
) = TYPE_MAX_VALUE (type
);
3629 else if (TREE_CODE (type
) == ARRAY_TYPE
)
3631 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type
), body
);
3632 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type
), body
);
3633 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type
), body
);
3634 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type
), body
);
3636 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
3638 TYPE_SIZE (t
) = TYPE_SIZE (type
);
3639 TYPE_SIZE_UNIT (t
) = TYPE_SIZE_UNIT (type
);
3644 /* Make sure all type sizes and array domains are either constant,
3645 or variable or parameter decls. This is a simplified variant
3646 of gimplify_type_sizes, but we can't use it here, as none of the
3647 variables in the expressions have been gimplified yet.
3648 As type sizes and domains for various variable length arrays
3649 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
3650 time, without this routine gimplify_type_sizes in the middle-end
3651 could result in the type sizes being gimplified earlier than where
3652 those variables are initialized. */
3655 gfc_trans_vla_type_sizes (gfc_symbol
*sym
, stmtblock_t
*body
)
3657 tree type
= TREE_TYPE (sym
->backend_decl
);
3659 if (TREE_CODE (type
) == FUNCTION_TYPE
3660 && (sym
->attr
.function
|| sym
->attr
.result
|| sym
->attr
.entry
))
3662 if (! current_fake_result_decl
)
3665 type
= TREE_TYPE (TREE_VALUE (current_fake_result_decl
));
3668 while (POINTER_TYPE_P (type
))
3669 type
= TREE_TYPE (type
);
3671 if (GFC_DESCRIPTOR_TYPE_P (type
))
3673 tree etype
= GFC_TYPE_ARRAY_DATAPTR_TYPE (type
);
3675 while (POINTER_TYPE_P (etype
))
3676 etype
= TREE_TYPE (etype
);
3678 gfc_trans_vla_type_sizes_1 (etype
, body
);
3681 gfc_trans_vla_type_sizes_1 (type
, body
);
3685 /* Initialize a derived type by building an lvalue from the symbol
3686 and using trans_assignment to do the work. Set dealloc to false
3687 if no deallocation prior the assignment is needed. */
3689 gfc_init_default_dt (gfc_symbol
* sym
, stmtblock_t
* block
, bool dealloc
)
3697 gcc_assert (!sym
->attr
.allocatable
);
3698 gfc_set_sym_referenced (sym
);
3699 e
= gfc_lval_expr_from_sym (sym
);
3700 tmp
= gfc_trans_assignment (e
, sym
->value
, false, dealloc
);
3701 if (sym
->attr
.dummy
&& (sym
->attr
.optional
3702 || sym
->ns
->proc_name
->attr
.entry_master
))
3704 present
= gfc_conv_expr_present (sym
);
3705 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
), present
,
3706 tmp
, build_empty_stmt (input_location
));
3708 gfc_add_expr_to_block (block
, tmp
);
3713 /* Initialize INTENT(OUT) derived type dummies. As well as giving
3714 them their default initializer, if they do not have allocatable
3715 components, they have their allocatable components deallocated. */
3718 init_intent_out_dt (gfc_symbol
* proc_sym
, gfc_wrapped_block
* block
)
3721 gfc_formal_arglist
*f
;
3725 gfc_init_block (&init
);
3726 for (f
= gfc_sym_get_dummy_args (proc_sym
); f
; f
= f
->next
)
3727 if (f
->sym
&& f
->sym
->attr
.intent
== INTENT_OUT
3728 && !f
->sym
->attr
.pointer
3729 && f
->sym
->ts
.type
== BT_DERIVED
)
3733 /* Note: Allocatables are excluded as they are already handled
3735 if (!f
->sym
->attr
.allocatable
3736 && gfc_is_finalizable (f
->sym
->ts
.u
.derived
, NULL
))
3741 gfc_init_block (&block
);
3742 f
->sym
->attr
.referenced
= 1;
3743 e
= gfc_lval_expr_from_sym (f
->sym
);
3744 gfc_add_finalizer_call (&block
, e
);
3746 tmp
= gfc_finish_block (&block
);
3749 if (tmp
== NULL_TREE
&& !f
->sym
->attr
.allocatable
3750 && f
->sym
->ts
.u
.derived
->attr
.alloc_comp
&& !f
->sym
->value
)
3751 tmp
= gfc_deallocate_alloc_comp (f
->sym
->ts
.u
.derived
,
3752 f
->sym
->backend_decl
,
3753 f
->sym
->as
? f
->sym
->as
->rank
: 0);
3755 if (tmp
!= NULL_TREE
&& (f
->sym
->attr
.optional
3756 || f
->sym
->ns
->proc_name
->attr
.entry_master
))
3758 present
= gfc_conv_expr_present (f
->sym
);
3759 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
),
3760 present
, tmp
, build_empty_stmt (input_location
));
3763 if (tmp
!= NULL_TREE
)
3764 gfc_add_expr_to_block (&init
, tmp
);
3765 else if (f
->sym
->value
&& !f
->sym
->attr
.allocatable
)
3766 gfc_init_default_dt (f
->sym
, &init
, true);
3768 else if (f
->sym
&& f
->sym
->attr
.intent
== INTENT_OUT
3769 && f
->sym
->ts
.type
== BT_CLASS
3770 && !CLASS_DATA (f
->sym
)->attr
.class_pointer
3771 && !CLASS_DATA (f
->sym
)->attr
.allocatable
)
3776 gfc_init_block (&block
);
3777 f
->sym
->attr
.referenced
= 1;
3778 e
= gfc_lval_expr_from_sym (f
->sym
);
3779 gfc_add_finalizer_call (&block
, e
);
3781 tmp
= gfc_finish_block (&block
);
3783 if (f
->sym
->attr
.optional
|| f
->sym
->ns
->proc_name
->attr
.entry_master
)
3785 present
= gfc_conv_expr_present (f
->sym
);
3786 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
),
3788 build_empty_stmt (input_location
));
3791 gfc_add_expr_to_block (&init
, tmp
);
3794 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
3798 /* Generate function entry and exit code, and add it to the function body.
3800 Allocation and initialization of array variables.
3801 Allocation of character string variables.
3802 Initialization and possibly repacking of dummy arrays.
3803 Initialization of ASSIGN statement auxiliary variable.
3804 Initialization of ASSOCIATE names.
3805 Automatic deallocation. */
3808 gfc_trans_deferred_vars (gfc_symbol
* proc_sym
, gfc_wrapped_block
* block
)
3812 gfc_formal_arglist
*f
;
3813 stmtblock_t tmpblock
;
3814 bool seen_trans_deferred_array
= false;
3820 /* Deal with implicit return variables. Explicit return variables will
3821 already have been added. */
3822 if (gfc_return_by_reference (proc_sym
) && proc_sym
->result
== proc_sym
)
3824 if (!current_fake_result_decl
)
3826 gfc_entry_list
*el
= NULL
;
3827 if (proc_sym
->attr
.entry_master
)
3829 for (el
= proc_sym
->ns
->entries
; el
; el
= el
->next
)
3830 if (el
->sym
!= el
->sym
->result
)
3833 /* TODO: move to the appropriate place in resolve.c. */
3834 if (warn_return_type
&& el
== NULL
)
3835 gfc_warning (OPT_Wreturn_type
,
3836 "Return value of function %qs at %L not set",
3837 proc_sym
->name
, &proc_sym
->declared_at
);
3839 else if (proc_sym
->as
)
3841 tree result
= TREE_VALUE (current_fake_result_decl
);
3842 gfc_trans_dummy_array_bias (proc_sym
, result
, block
);
3844 /* An automatic character length, pointer array result. */
3845 if (proc_sym
->ts
.type
== BT_CHARACTER
3846 && TREE_CODE (proc_sym
->ts
.u
.cl
->backend_decl
) == VAR_DECL
)
3847 gfc_trans_dummy_character (proc_sym
, proc_sym
->ts
.u
.cl
, block
);
3849 else if (proc_sym
->ts
.type
== BT_CHARACTER
)
3851 if (proc_sym
->ts
.deferred
)
3854 gfc_save_backend_locus (&loc
);
3855 gfc_set_backend_locus (&proc_sym
->declared_at
);
3856 gfc_start_block (&init
);
3857 /* Zero the string length on entry. */
3858 gfc_add_modify (&init
, proc_sym
->ts
.u
.cl
->backend_decl
,
3859 build_int_cst (gfc_charlen_type_node
, 0));
3860 /* Null the pointer. */
3861 e
= gfc_lval_expr_from_sym (proc_sym
);
3862 gfc_init_se (&se
, NULL
);
3863 se
.want_pointer
= 1;
3864 gfc_conv_expr (&se
, e
);
3867 gfc_add_modify (&init
, tmp
,
3868 fold_convert (TREE_TYPE (se
.expr
),
3869 null_pointer_node
));
3870 gfc_restore_backend_locus (&loc
);
3872 /* Pass back the string length on exit. */
3873 tmp
= proc_sym
->ts
.u
.cl
->passed_length
;
3874 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
3875 tmp
= fold_convert (gfc_charlen_type_node
, tmp
);
3876 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
3877 gfc_charlen_type_node
, tmp
,
3878 proc_sym
->ts
.u
.cl
->backend_decl
);
3879 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
3881 else if (TREE_CODE (proc_sym
->ts
.u
.cl
->backend_decl
) == VAR_DECL
)
3882 gfc_trans_dummy_character (proc_sym
, proc_sym
->ts
.u
.cl
, block
);
3885 gcc_assert (flag_f2c
&& proc_sym
->ts
.type
== BT_COMPLEX
);
3888 /* Initialize the INTENT(OUT) derived type dummy arguments. This
3889 should be done here so that the offsets and lbounds of arrays
3891 gfc_save_backend_locus (&loc
);
3892 gfc_set_backend_locus (&proc_sym
->declared_at
);
3893 init_intent_out_dt (proc_sym
, block
);
3894 gfc_restore_backend_locus (&loc
);
3896 for (sym
= proc_sym
->tlink
; sym
!= proc_sym
; sym
= sym
->tlink
)
3898 bool alloc_comp_or_fini
= (sym
->ts
.type
== BT_DERIVED
)
3899 && (sym
->ts
.u
.derived
->attr
.alloc_comp
3900 || gfc_is_finalizable (sym
->ts
.u
.derived
,
3905 if (sym
->attr
.subref_array_pointer
3906 && GFC_DECL_SPAN (sym
->backend_decl
)
3907 && !TREE_STATIC (GFC_DECL_SPAN (sym
->backend_decl
)))
3909 gfc_init_block (&tmpblock
);
3910 gfc_add_modify (&tmpblock
, GFC_DECL_SPAN (sym
->backend_decl
),
3911 build_int_cst (gfc_array_index_type
, 0));
3912 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
),
3916 if (sym
->ts
.type
== BT_CLASS
3917 && (sym
->attr
.save
|| flag_max_stack_var_size
== 0)
3918 && CLASS_DATA (sym
)->attr
.allocatable
)
3922 if (UNLIMITED_POLY (sym
))
3923 vptr
= null_pointer_node
;
3927 vsym
= gfc_find_derived_vtab (sym
->ts
.u
.derived
);
3928 vptr
= gfc_get_symbol_decl (vsym
);
3929 vptr
= gfc_build_addr_expr (NULL
, vptr
);
3932 if (CLASS_DATA (sym
)->attr
.dimension
3933 || (CLASS_DATA (sym
)->attr
.codimension
3934 && flag_coarray
!= GFC_FCOARRAY_LIB
))
3936 tmp
= gfc_class_data_get (sym
->backend_decl
);
3937 tmp
= gfc_build_null_descriptor (TREE_TYPE (tmp
));
3940 tmp
= null_pointer_node
;
3942 DECL_INITIAL (sym
->backend_decl
)
3943 = gfc_class_set_static_fields (sym
->backend_decl
, vptr
, tmp
);
3944 TREE_CONSTANT (DECL_INITIAL (sym
->backend_decl
)) = 1;
3946 else if (sym
->attr
.dimension
|| sym
->attr
.codimension
)
3948 /* Assumed-size Cray pointees need to be treated as AS_EXPLICIT. */
3949 array_type tmp
= sym
->as
->type
;
3950 if (tmp
== AS_ASSUMED_SIZE
&& sym
->as
->cp_was_assumed
)
3955 if (sym
->attr
.dummy
|| sym
->attr
.result
)
3956 gfc_trans_dummy_array_bias (sym
, sym
->backend_decl
, block
);
3957 else if (sym
->attr
.pointer
|| sym
->attr
.allocatable
)
3959 if (TREE_STATIC (sym
->backend_decl
))
3961 gfc_save_backend_locus (&loc
);
3962 gfc_set_backend_locus (&sym
->declared_at
);
3963 gfc_trans_static_array_pointer (sym
);
3964 gfc_restore_backend_locus (&loc
);
3968 seen_trans_deferred_array
= true;
3969 gfc_trans_deferred_array (sym
, block
);
3972 else if (sym
->attr
.codimension
&& TREE_STATIC (sym
->backend_decl
))
3974 gfc_init_block (&tmpblock
);
3975 gfc_trans_array_cobounds (TREE_TYPE (sym
->backend_decl
),
3977 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
),
3983 gfc_save_backend_locus (&loc
);
3984 gfc_set_backend_locus (&sym
->declared_at
);
3986 if (alloc_comp_or_fini
)
3988 seen_trans_deferred_array
= true;
3989 gfc_trans_deferred_array (sym
, block
);
3991 else if (sym
->ts
.type
== BT_DERIVED
3994 && sym
->attr
.save
== SAVE_NONE
)
3996 gfc_start_block (&tmpblock
);
3997 gfc_init_default_dt (sym
, &tmpblock
, false);
3998 gfc_add_init_cleanup (block
,
3999 gfc_finish_block (&tmpblock
),
4003 gfc_trans_auto_array_allocation (sym
->backend_decl
,
4005 gfc_restore_backend_locus (&loc
);
4009 case AS_ASSUMED_SIZE
:
4010 /* Must be a dummy parameter. */
4011 gcc_assert (sym
->attr
.dummy
|| sym
->as
->cp_was_assumed
);
4013 /* We should always pass assumed size arrays the g77 way. */
4014 if (sym
->attr
.dummy
)
4015 gfc_trans_g77_array (sym
, block
);
4018 case AS_ASSUMED_SHAPE
:
4019 /* Must be a dummy parameter. */
4020 gcc_assert (sym
->attr
.dummy
);
4022 gfc_trans_dummy_array_bias (sym
, sym
->backend_decl
, block
);
4025 case AS_ASSUMED_RANK
:
4027 seen_trans_deferred_array
= true;
4028 gfc_trans_deferred_array (sym
, block
);
4034 if (alloc_comp_or_fini
&& !seen_trans_deferred_array
)
4035 gfc_trans_deferred_array (sym
, block
);
4037 else if ((!sym
->attr
.dummy
|| sym
->ts
.deferred
)
4038 && (sym
->ts
.type
== BT_CLASS
4039 && CLASS_DATA (sym
)->attr
.class_pointer
))
4041 else if ((!sym
->attr
.dummy
|| sym
->ts
.deferred
)
4042 && (sym
->attr
.allocatable
4043 || (sym
->ts
.type
== BT_CLASS
4044 && CLASS_DATA (sym
)->attr
.allocatable
)))
4046 if (!sym
->attr
.save
&& flag_max_stack_var_size
!= 0)
4048 tree descriptor
= NULL_TREE
;
4050 /* Nullify and automatic deallocation of allocatable
4052 e
= gfc_lval_expr_from_sym (sym
);
4053 if (sym
->ts
.type
== BT_CLASS
)
4054 gfc_add_data_component (e
);
4056 gfc_init_se (&se
, NULL
);
4057 if (sym
->ts
.type
!= BT_CLASS
4058 || sym
->ts
.u
.derived
->attr
.dimension
4059 || sym
->ts
.u
.derived
->attr
.codimension
)
4061 se
.want_pointer
= 1;
4062 gfc_conv_expr (&se
, e
);
4064 else if (sym
->ts
.type
== BT_CLASS
4065 && !CLASS_DATA (sym
)->attr
.dimension
4066 && !CLASS_DATA (sym
)->attr
.codimension
)
4068 se
.want_pointer
= 1;
4069 gfc_conv_expr (&se
, e
);
4073 gfc_conv_expr (&se
, e
);
4074 descriptor
= se
.expr
;
4075 se
.expr
= gfc_conv_descriptor_data_addr (se
.expr
);
4076 se
.expr
= build_fold_indirect_ref_loc (input_location
, se
.expr
);
4080 gfc_save_backend_locus (&loc
);
4081 gfc_set_backend_locus (&sym
->declared_at
);
4082 gfc_start_block (&init
);
4084 if (!sym
->attr
.dummy
|| sym
->attr
.intent
== INTENT_OUT
)
4086 /* Nullify when entering the scope. */
4087 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4088 TREE_TYPE (se
.expr
), se
.expr
,
4089 fold_convert (TREE_TYPE (se
.expr
),
4090 null_pointer_node
));
4091 if (sym
->attr
.optional
)
4093 tree present
= gfc_conv_expr_present (sym
);
4094 tmp
= build3_loc (input_location
, COND_EXPR
,
4095 void_type_node
, present
, tmp
,
4096 build_empty_stmt (input_location
));
4098 gfc_add_expr_to_block (&init
, tmp
);
4101 if ((sym
->attr
.dummy
|| sym
->attr
.result
)
4102 && sym
->ts
.type
== BT_CHARACTER
4103 && sym
->ts
.deferred
)
4105 /* Character length passed by reference. */
4106 tmp
= sym
->ts
.u
.cl
->passed_length
;
4107 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
4108 tmp
= fold_convert (gfc_charlen_type_node
, tmp
);
4110 if (!sym
->attr
.dummy
|| sym
->attr
.intent
== INTENT_OUT
)
4111 /* Zero the string length when entering the scope. */
4112 gfc_add_modify (&init
, sym
->ts
.u
.cl
->backend_decl
,
4113 build_int_cst (gfc_charlen_type_node
, 0));
4118 tmp2
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4119 gfc_charlen_type_node
,
4120 sym
->ts
.u
.cl
->backend_decl
, tmp
);
4121 if (sym
->attr
.optional
)
4123 tree present
= gfc_conv_expr_present (sym
);
4124 tmp2
= build3_loc (input_location
, COND_EXPR
,
4125 void_type_node
, present
, tmp2
,
4126 build_empty_stmt (input_location
));
4128 gfc_add_expr_to_block (&init
, tmp2
);
4131 gfc_restore_backend_locus (&loc
);
4133 /* Pass the final character length back. */
4134 if (sym
->attr
.intent
!= INTENT_IN
)
4136 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4137 gfc_charlen_type_node
, tmp
,
4138 sym
->ts
.u
.cl
->backend_decl
);
4139 if (sym
->attr
.optional
)
4141 tree present
= gfc_conv_expr_present (sym
);
4142 tmp
= build3_loc (input_location
, COND_EXPR
,
4143 void_type_node
, present
, tmp
,
4144 build_empty_stmt (input_location
));
4151 gfc_restore_backend_locus (&loc
);
4153 /* Deallocate when leaving the scope. Nullifying is not
4155 if (!sym
->attr
.result
&& !sym
->attr
.dummy
4156 && !sym
->ns
->proc_name
->attr
.is_main_program
)
4158 if (sym
->ts
.type
== BT_CLASS
4159 && CLASS_DATA (sym
)->attr
.codimension
)
4160 tmp
= gfc_deallocate_with_status (descriptor
, NULL_TREE
,
4161 NULL_TREE
, NULL_TREE
,
4162 NULL_TREE
, true, NULL
,
4166 gfc_expr
*expr
= gfc_lval_expr_from_sym (sym
);
4167 tmp
= gfc_deallocate_scalar_with_status (se
.expr
, NULL_TREE
,
4168 true, expr
, sym
->ts
);
4169 gfc_free_expr (expr
);
4172 if (sym
->ts
.type
== BT_CLASS
)
4174 /* Initialize _vptr to declared type. */
4178 gfc_save_backend_locus (&loc
);
4179 gfc_set_backend_locus (&sym
->declared_at
);
4180 e
= gfc_lval_expr_from_sym (sym
);
4181 gfc_add_vptr_component (e
);
4182 gfc_init_se (&se
, NULL
);
4183 se
.want_pointer
= 1;
4184 gfc_conv_expr (&se
, e
);
4186 if (UNLIMITED_POLY (sym
))
4187 rhs
= build_int_cst (TREE_TYPE (se
.expr
), 0);
4190 vtab
= gfc_find_derived_vtab (sym
->ts
.u
.derived
);
4191 rhs
= gfc_build_addr_expr (TREE_TYPE (se
.expr
),
4192 gfc_get_symbol_decl (vtab
));
4194 gfc_add_modify (&init
, se
.expr
, rhs
);
4195 gfc_restore_backend_locus (&loc
);
4198 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
4201 else if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.deferred
)
4206 /* If we get to here, all that should be left are pointers. */
4207 gcc_assert (sym
->attr
.pointer
);
4209 if (sym
->attr
.dummy
)
4211 gfc_start_block (&init
);
4213 /* Character length passed by reference. */
4214 tmp
= sym
->ts
.u
.cl
->passed_length
;
4215 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
4216 tmp
= fold_convert (gfc_charlen_type_node
, tmp
);
4217 gfc_add_modify (&init
, sym
->ts
.u
.cl
->backend_decl
, tmp
);
4218 /* Pass the final character length back. */
4219 if (sym
->attr
.intent
!= INTENT_IN
)
4220 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4221 gfc_charlen_type_node
, tmp
,
4222 sym
->ts
.u
.cl
->backend_decl
);
4225 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
4228 else if (sym
->ts
.deferred
)
4229 gfc_fatal_error ("Deferred type parameter not yet supported");
4230 else if (alloc_comp_or_fini
)
4231 gfc_trans_deferred_array (sym
, block
);
4232 else if (sym
->ts
.type
== BT_CHARACTER
)
4234 gfc_save_backend_locus (&loc
);
4235 gfc_set_backend_locus (&sym
->declared_at
);
4236 if (sym
->attr
.dummy
|| sym
->attr
.result
)
4237 gfc_trans_dummy_character (sym
, sym
->ts
.u
.cl
, block
);
4239 gfc_trans_auto_character_variable (sym
, block
);
4240 gfc_restore_backend_locus (&loc
);
4242 else if (sym
->attr
.assign
)
4244 gfc_save_backend_locus (&loc
);
4245 gfc_set_backend_locus (&sym
->declared_at
);
4246 gfc_trans_assign_aux_var (sym
, block
);
4247 gfc_restore_backend_locus (&loc
);
4249 else if (sym
->ts
.type
== BT_DERIVED
4252 && sym
->attr
.save
== SAVE_NONE
)
4254 gfc_start_block (&tmpblock
);
4255 gfc_init_default_dt (sym
, &tmpblock
, false);
4256 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
),
4259 else if (!(UNLIMITED_POLY(sym
)))
4263 gfc_init_block (&tmpblock
);
4265 for (f
= gfc_sym_get_dummy_args (proc_sym
); f
; f
= f
->next
)
4267 if (f
->sym
&& f
->sym
->tlink
== NULL
&& f
->sym
->ts
.type
== BT_CHARACTER
)
4269 gcc_assert (f
->sym
->ts
.u
.cl
->backend_decl
!= NULL
);
4270 if (TREE_CODE (f
->sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
4271 gfc_trans_vla_type_sizes (f
->sym
, &tmpblock
);
4275 if (gfc_return_by_reference (proc_sym
) && proc_sym
->ts
.type
== BT_CHARACTER
4276 && current_fake_result_decl
!= NULL
)
4278 gcc_assert (proc_sym
->ts
.u
.cl
->backend_decl
!= NULL
);
4279 if (TREE_CODE (proc_sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
4280 gfc_trans_vla_type_sizes (proc_sym
, &tmpblock
);
4283 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
), NULL_TREE
);
4286 struct module_hasher
: ggc_hasher
<module_htab_entry
*>
4288 typedef const char *compare_type
;
4290 static hashval_t
hash (module_htab_entry
*s
) { return htab_hash_string (s
); }
4292 equal (module_htab_entry
*a
, const char *b
)
4294 return !strcmp (a
->name
, b
);
4298 static GTY (()) hash_table
<module_hasher
> *module_htab
;
4300 /* Hash and equality functions for module_htab's decls. */
4303 module_decl_hasher::hash (tree t
)
4305 const_tree n
= DECL_NAME (t
);
4307 n
= TYPE_NAME (TREE_TYPE (t
));
4308 return htab_hash_string (IDENTIFIER_POINTER (n
));
4312 module_decl_hasher::equal (tree t1
, const char *x2
)
4314 const_tree n1
= DECL_NAME (t1
);
4315 if (n1
== NULL_TREE
)
4316 n1
= TYPE_NAME (TREE_TYPE (t1
));
4317 return strcmp (IDENTIFIER_POINTER (n1
), x2
) == 0;
4320 struct module_htab_entry
*
4321 gfc_find_module (const char *name
)
4324 module_htab
= hash_table
<module_hasher
>::create_ggc (10);
4326 module_htab_entry
**slot
4327 = module_htab
->find_slot_with_hash (name
, htab_hash_string (name
), INSERT
);
4330 module_htab_entry
*entry
= ggc_cleared_alloc
<module_htab_entry
> ();
4332 entry
->name
= gfc_get_string (name
);
4333 entry
->decls
= hash_table
<module_decl_hasher
>::create_ggc (10);
4340 gfc_module_add_decl (struct module_htab_entry
*entry
, tree decl
)
4344 if (DECL_NAME (decl
))
4345 name
= IDENTIFIER_POINTER (DECL_NAME (decl
));
4348 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
4349 name
= IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl
)));
4352 = entry
->decls
->find_slot_with_hash (name
, htab_hash_string (name
),
4359 /* Generate debugging symbols for namelists. This function must come after
4360 generate_local_decl to ensure that the variables in the namelist are
4361 already declared. */
4364 generate_namelist_decl (gfc_symbol
* sym
)
4368 vec
<constructor_elt
, va_gc
> *nml_decls
= NULL
;
4370 gcc_assert (sym
->attr
.flavor
== FL_NAMELIST
);
4371 for (nml
= sym
->namelist
; nml
; nml
= nml
->next
)
4373 if (nml
->sym
->backend_decl
== NULL_TREE
)
4375 nml
->sym
->attr
.referenced
= 1;
4376 nml
->sym
->backend_decl
= gfc_get_symbol_decl (nml
->sym
);
4378 DECL_IGNORED_P (nml
->sym
->backend_decl
) = 0;
4379 CONSTRUCTOR_APPEND_ELT (nml_decls
, NULL_TREE
, nml
->sym
->backend_decl
);
4382 decl
= make_node (NAMELIST_DECL
);
4383 TREE_TYPE (decl
) = void_type_node
;
4384 NAMELIST_DECL_ASSOCIATED_DECL (decl
) = build_constructor (NULL_TREE
, nml_decls
);
4385 DECL_NAME (decl
) = get_identifier (sym
->name
);
4390 /* Output an initialized decl for a module variable. */
4393 gfc_create_module_variable (gfc_symbol
* sym
)
4397 /* Module functions with alternate entries are dealt with later and
4398 would get caught by the next condition. */
4399 if (sym
->attr
.entry
)
4402 /* Make sure we convert the types of the derived types from iso_c_binding
4404 if (sym
->attr
.flavor
!= FL_PROCEDURE
&& sym
->attr
.is_iso_c
4405 && sym
->ts
.type
== BT_DERIVED
)
4406 sym
->backend_decl
= gfc_typenode_for_spec (&(sym
->ts
));
4408 if (sym
->attr
.flavor
== FL_DERIVED
4409 && sym
->backend_decl
4410 && TREE_CODE (sym
->backend_decl
) == RECORD_TYPE
)
4412 decl
= sym
->backend_decl
;
4413 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
4415 if (!sym
->attr
.use_assoc
)
4417 gcc_assert (TYPE_CONTEXT (decl
) == NULL_TREE
4418 || TYPE_CONTEXT (decl
) == sym
->ns
->proc_name
->backend_decl
);
4419 gcc_assert (DECL_CONTEXT (TYPE_STUB_DECL (decl
)) == NULL_TREE
4420 || DECL_CONTEXT (TYPE_STUB_DECL (decl
))
4421 == sym
->ns
->proc_name
->backend_decl
);
4423 TYPE_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
4424 DECL_CONTEXT (TYPE_STUB_DECL (decl
)) = sym
->ns
->proc_name
->backend_decl
;
4425 gfc_module_add_decl (cur_module
, TYPE_STUB_DECL (decl
));
4428 /* Only output variables, procedure pointers and array valued,
4429 or derived type, parameters. */
4430 if (sym
->attr
.flavor
!= FL_VARIABLE
4431 && !(sym
->attr
.flavor
== FL_PARAMETER
4432 && (sym
->attr
.dimension
|| sym
->ts
.type
== BT_DERIVED
))
4433 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.proc_pointer
))
4436 if ((sym
->attr
.in_common
|| sym
->attr
.in_equivalence
) && sym
->backend_decl
)
4438 decl
= sym
->backend_decl
;
4439 gcc_assert (DECL_FILE_SCOPE_P (decl
));
4440 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
4441 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
4442 gfc_module_add_decl (cur_module
, decl
);
4445 /* Don't generate variables from other modules. Variables from
4446 COMMONs and Cray pointees will already have been generated. */
4447 if (sym
->attr
.use_assoc
|| sym
->attr
.in_common
|| sym
->attr
.cray_pointee
)
4450 /* Equivalenced variables arrive here after creation. */
4451 if (sym
->backend_decl
4452 && (sym
->equiv_built
|| sym
->attr
.in_equivalence
))
4455 if (sym
->backend_decl
&& !sym
->attr
.vtab
&& !sym
->attr
.target
)
4456 gfc_internal_error ("backend decl for module variable %qs already exists",
4459 if (sym
->module
&& !sym
->attr
.result
&& !sym
->attr
.dummy
4460 && (sym
->attr
.access
== ACCESS_UNKNOWN
4461 && (sym
->ns
->default_access
== ACCESS_PRIVATE
4462 || (sym
->ns
->default_access
== ACCESS_UNKNOWN
4463 && flag_module_private
))))
4464 sym
->attr
.access
= ACCESS_PRIVATE
;
4466 if (warn_unused_variable
&& !sym
->attr
.referenced
4467 && sym
->attr
.access
== ACCESS_PRIVATE
)
4468 gfc_warning (OPT_Wunused_value
,
4469 "Unused PRIVATE module variable %qs declared at %L",
4470 sym
->name
, &sym
->declared_at
);
4472 /* We always want module variables to be created. */
4473 sym
->attr
.referenced
= 1;
4474 /* Create the decl. */
4475 decl
= gfc_get_symbol_decl (sym
);
4477 /* Create the variable. */
4479 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
4480 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
4481 rest_of_decl_compilation (decl
, 1, 0);
4482 gfc_module_add_decl (cur_module
, decl
);
4484 /* Also add length of strings. */
4485 if (sym
->ts
.type
== BT_CHARACTER
)
4489 length
= sym
->ts
.u
.cl
->backend_decl
;
4490 gcc_assert (length
|| sym
->attr
.proc_pointer
);
4491 if (length
&& !INTEGER_CST_P (length
))
4494 rest_of_decl_compilation (length
, 1, 0);
4498 if (sym
->attr
.codimension
&& !sym
->attr
.dummy
&& !sym
->attr
.allocatable
4499 && sym
->attr
.referenced
&& !sym
->attr
.use_assoc
)
4500 has_coarray_vars
= true;
4503 /* Emit debug information for USE statements. */
4506 gfc_trans_use_stmts (gfc_namespace
* ns
)
4508 gfc_use_list
*use_stmt
;
4509 for (use_stmt
= ns
->use_stmts
; use_stmt
; use_stmt
= use_stmt
->next
)
4511 struct module_htab_entry
*entry
4512 = gfc_find_module (use_stmt
->module_name
);
4513 gfc_use_rename
*rent
;
4515 if (entry
->namespace_decl
== NULL
)
4517 entry
->namespace_decl
4518 = build_decl (input_location
,
4520 get_identifier (use_stmt
->module_name
),
4522 DECL_EXTERNAL (entry
->namespace_decl
) = 1;
4524 gfc_set_backend_locus (&use_stmt
->where
);
4525 if (!use_stmt
->only_flag
)
4526 (*debug_hooks
->imported_module_or_decl
) (entry
->namespace_decl
,
4528 ns
->proc_name
->backend_decl
,
4530 for (rent
= use_stmt
->rename
; rent
; rent
= rent
->next
)
4532 tree decl
, local_name
;
4534 if (rent
->op
!= INTRINSIC_NONE
)
4537 hashval_t hash
= htab_hash_string (rent
->use_name
);
4538 tree
*slot
= entry
->decls
->find_slot_with_hash (rent
->use_name
, hash
,
4544 st
= gfc_find_symtree (ns
->sym_root
,
4546 ? rent
->local_name
: rent
->use_name
);
4548 /* The following can happen if a derived type is renamed. */
4552 name
= xstrdup (rent
->local_name
[0]
4553 ? rent
->local_name
: rent
->use_name
);
4554 name
[0] = (char) TOUPPER ((unsigned char) name
[0]);
4555 st
= gfc_find_symtree (ns
->sym_root
, name
);
4560 /* Sometimes, generic interfaces wind up being over-ruled by a
4561 local symbol (see PR41062). */
4562 if (!st
->n
.sym
->attr
.use_assoc
)
4565 if (st
->n
.sym
->backend_decl
4566 && DECL_P (st
->n
.sym
->backend_decl
)
4567 && st
->n
.sym
->module
4568 && strcmp (st
->n
.sym
->module
, use_stmt
->module_name
) == 0)
4570 gcc_assert (DECL_EXTERNAL (entry
->namespace_decl
)
4571 || (TREE_CODE (st
->n
.sym
->backend_decl
)
4573 decl
= copy_node (st
->n
.sym
->backend_decl
);
4574 DECL_CONTEXT (decl
) = entry
->namespace_decl
;
4575 DECL_EXTERNAL (decl
) = 1;
4576 DECL_IGNORED_P (decl
) = 0;
4577 DECL_INITIAL (decl
) = NULL_TREE
;
4579 else if (st
->n
.sym
->attr
.flavor
== FL_NAMELIST
4580 && st
->n
.sym
->attr
.use_only
4581 && st
->n
.sym
->module
4582 && strcmp (st
->n
.sym
->module
, use_stmt
->module_name
)
4585 decl
= generate_namelist_decl (st
->n
.sym
);
4586 DECL_CONTEXT (decl
) = entry
->namespace_decl
;
4587 DECL_EXTERNAL (decl
) = 1;
4588 DECL_IGNORED_P (decl
) = 0;
4589 DECL_INITIAL (decl
) = NULL_TREE
;
4593 *slot
= error_mark_node
;
4594 entry
->decls
->clear_slot (slot
);
4599 decl
= (tree
) *slot
;
4600 if (rent
->local_name
[0])
4601 local_name
= get_identifier (rent
->local_name
);
4603 local_name
= NULL_TREE
;
4604 gfc_set_backend_locus (&rent
->where
);
4605 (*debug_hooks
->imported_module_or_decl
) (decl
, local_name
,
4606 ns
->proc_name
->backend_decl
,
4607 !use_stmt
->only_flag
);
4613 /* Return true if expr is a constant initializer that gfc_conv_initializer
4617 check_constant_initializer (gfc_expr
*expr
, gfc_typespec
*ts
, bool array
,
4627 if (expr
->expr_type
== EXPR_CONSTANT
|| expr
->expr_type
== EXPR_NULL
)
4629 else if (expr
->expr_type
== EXPR_STRUCTURE
)
4630 return check_constant_initializer (expr
, ts
, false, false);
4631 else if (expr
->expr_type
!= EXPR_ARRAY
)
4633 for (c
= gfc_constructor_first (expr
->value
.constructor
);
4634 c
; c
= gfc_constructor_next (c
))
4638 if (c
->expr
->expr_type
== EXPR_STRUCTURE
)
4640 if (!check_constant_initializer (c
->expr
, ts
, false, false))
4643 else if (c
->expr
->expr_type
!= EXPR_CONSTANT
)
4648 else switch (ts
->type
)
4651 if (expr
->expr_type
!= EXPR_STRUCTURE
)
4653 cm
= expr
->ts
.u
.derived
->components
;
4654 for (c
= gfc_constructor_first (expr
->value
.constructor
);
4655 c
; c
= gfc_constructor_next (c
), cm
= cm
->next
)
4657 if (!c
->expr
|| cm
->attr
.allocatable
)
4659 if (!check_constant_initializer (c
->expr
, &cm
->ts
,
4666 return expr
->expr_type
== EXPR_CONSTANT
;
4670 /* Emit debug info for parameters and unreferenced variables with
4674 gfc_emit_parameter_debug_info (gfc_symbol
*sym
)
4678 if (sym
->attr
.flavor
!= FL_PARAMETER
4679 && (sym
->attr
.flavor
!= FL_VARIABLE
|| sym
->attr
.referenced
))
4682 if (sym
->backend_decl
!= NULL
4683 || sym
->value
== NULL
4684 || sym
->attr
.use_assoc
4687 || sym
->attr
.function
4688 || sym
->attr
.intrinsic
4689 || sym
->attr
.pointer
4690 || sym
->attr
.allocatable
4691 || sym
->attr
.cray_pointee
4692 || sym
->attr
.threadprivate
4693 || sym
->attr
.is_bind_c
4694 || sym
->attr
.subref_array_pointer
4695 || sym
->attr
.assign
)
4698 if (sym
->ts
.type
== BT_CHARACTER
)
4700 gfc_conv_const_charlen (sym
->ts
.u
.cl
);
4701 if (sym
->ts
.u
.cl
->backend_decl
== NULL
4702 || TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) != INTEGER_CST
)
4705 else if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.alloc_comp
)
4712 if (sym
->as
->type
!= AS_EXPLICIT
)
4714 for (n
= 0; n
< sym
->as
->rank
; n
++)
4715 if (sym
->as
->lower
[n
]->expr_type
!= EXPR_CONSTANT
4716 || sym
->as
->upper
[n
] == NULL
4717 || sym
->as
->upper
[n
]->expr_type
!= EXPR_CONSTANT
)
4721 if (!check_constant_initializer (sym
->value
, &sym
->ts
,
4722 sym
->attr
.dimension
, false))
4725 if (flag_coarray
== GFC_FCOARRAY_LIB
&& sym
->attr
.codimension
)
4728 /* Create the decl for the variable or constant. */
4729 decl
= build_decl (input_location
,
4730 sym
->attr
.flavor
== FL_PARAMETER
? CONST_DECL
: VAR_DECL
,
4731 gfc_sym_identifier (sym
), gfc_sym_type (sym
));
4732 if (sym
->attr
.flavor
== FL_PARAMETER
)
4733 TREE_READONLY (decl
) = 1;
4734 gfc_set_decl_location (decl
, &sym
->declared_at
);
4735 if (sym
->attr
.dimension
)
4736 GFC_DECL_PACKED_ARRAY (decl
) = 1;
4737 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
4738 TREE_STATIC (decl
) = 1;
4739 TREE_USED (decl
) = 1;
4740 if (DECL_CONTEXT (decl
) && TREE_CODE (DECL_CONTEXT (decl
)) == NAMESPACE_DECL
)
4741 TREE_PUBLIC (decl
) = 1;
4742 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
4744 sym
->attr
.dimension
,
4746 debug_hooks
->global_decl (decl
);
4751 generate_coarray_sym_init (gfc_symbol
*sym
)
4753 tree tmp
, size
, decl
, token
;
4757 if (sym
->attr
.dummy
|| sym
->attr
.allocatable
|| !sym
->attr
.codimension
4758 || sym
->attr
.use_assoc
|| !sym
->attr
.referenced
4759 || sym
->attr
.select_type_temporary
)
4762 decl
= sym
->backend_decl
;
4763 TREE_USED(decl
) = 1;
4764 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (decl
)));
4766 is_lock_type
= sym
->ts
.type
== BT_DERIVED
4767 && sym
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
4768 && sym
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_LOCK_TYPE
;
4770 /* FIXME: Workaround for PR middle-end/49106, cf. also PR middle-end/49108
4771 to make sure the variable is not optimized away. */
4772 DECL_PRESERVE_P (DECL_CONTEXT (decl
)) = 1;
4774 /* For lock types, we pass the array size as only the library knows the
4775 size of the variable. */
4777 size
= gfc_index_one_node
;
4779 size
= TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (decl
)));
4781 /* Ensure that we do not have size=0 for zero-sized arrays. */
4782 size
= fold_build2_loc (input_location
, MAX_EXPR
, size_type_node
,
4783 fold_convert (size_type_node
, size
),
4784 build_int_cst (size_type_node
, 1));
4786 if (GFC_TYPE_ARRAY_RANK (TREE_TYPE (decl
)))
4788 tmp
= GFC_TYPE_ARRAY_SIZE (TREE_TYPE (decl
));
4789 size
= fold_build2_loc (input_location
, MULT_EXPR
, size_type_node
,
4790 fold_convert (size_type_node
, tmp
), size
);
4793 gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (decl
)) != NULL_TREE
);
4794 token
= gfc_build_addr_expr (ppvoid_type_node
,
4795 GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE(decl
)));
4797 reg_type
= sym
->attr
.artificial
? GFC_CAF_CRITICAL
: GFC_CAF_LOCK_STATIC
;
4799 reg_type
= GFC_CAF_COARRAY_STATIC
;
4800 tmp
= build_call_expr_loc (input_location
, gfor_fndecl_caf_register
, 6, size
,
4801 build_int_cst (integer_type_node
, reg_type
),
4802 token
, null_pointer_node
, /* token, stat. */
4803 null_pointer_node
, /* errgmsg, errmsg_len. */
4804 build_int_cst (integer_type_node
, 0));
4805 gfc_add_modify (&caf_init_block
, decl
, fold_convert (TREE_TYPE (decl
), tmp
));
4807 /* Handle "static" initializer. */
4810 sym
->attr
.pointer
= 1;
4811 tmp
= gfc_trans_assignment (gfc_lval_expr_from_sym (sym
), sym
->value
,
4813 sym
->attr
.pointer
= 0;
4814 gfc_add_expr_to_block (&caf_init_block
, tmp
);
4819 /* Generate constructor function to initialize static, nonallocatable
4823 generate_coarray_init (gfc_namespace
* ns
__attribute((unused
)))
4825 tree fndecl
, tmp
, decl
, save_fn_decl
;
4827 save_fn_decl
= current_function_decl
;
4828 push_function_context ();
4830 tmp
= build_function_type_list (void_type_node
, NULL_TREE
);
4831 fndecl
= build_decl (input_location
, FUNCTION_DECL
,
4832 create_tmp_var_name ("_caf_init"), tmp
);
4834 DECL_STATIC_CONSTRUCTOR (fndecl
) = 1;
4835 SET_DECL_INIT_PRIORITY (fndecl
, DEFAULT_INIT_PRIORITY
);
4837 decl
= build_decl (input_location
, RESULT_DECL
, NULL_TREE
, void_type_node
);
4838 DECL_ARTIFICIAL (decl
) = 1;
4839 DECL_IGNORED_P (decl
) = 1;
4840 DECL_CONTEXT (decl
) = fndecl
;
4841 DECL_RESULT (fndecl
) = decl
;
4844 current_function_decl
= fndecl
;
4845 announce_function (fndecl
);
4847 rest_of_decl_compilation (fndecl
, 0, 0);
4848 make_decl_rtl (fndecl
);
4849 allocate_struct_function (fndecl
, false);
4852 gfc_init_block (&caf_init_block
);
4854 gfc_traverse_ns (ns
, generate_coarray_sym_init
);
4856 DECL_SAVED_TREE (fndecl
) = gfc_finish_block (&caf_init_block
);
4860 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
4862 DECL_SAVED_TREE (fndecl
)
4863 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (fndecl
),
4864 DECL_INITIAL (fndecl
));
4865 dump_function (TDI_original
, fndecl
);
4867 cfun
->function_end_locus
= input_location
;
4870 if (decl_function_context (fndecl
))
4871 (void) cgraph_node::create (fndecl
);
4873 cgraph_node::finalize_function (fndecl
, true);
4875 pop_function_context ();
4876 current_function_decl
= save_fn_decl
;
4881 create_module_nml_decl (gfc_symbol
*sym
)
4883 if (sym
->attr
.flavor
== FL_NAMELIST
)
4885 tree decl
= generate_namelist_decl (sym
);
4887 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
4888 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
4889 rest_of_decl_compilation (decl
, 1, 0);
4890 gfc_module_add_decl (cur_module
, decl
);
4895 /* Generate all the required code for module variables. */
4898 gfc_generate_module_vars (gfc_namespace
* ns
)
4900 module_namespace
= ns
;
4901 cur_module
= gfc_find_module (ns
->proc_name
->name
);
4903 /* Check if the frontend left the namespace in a reasonable state. */
4904 gcc_assert (ns
->proc_name
&& !ns
->proc_name
->tlink
);
4906 /* Generate COMMON blocks. */
4907 gfc_trans_common (ns
);
4909 has_coarray_vars
= false;
4911 /* Create decls for all the module variables. */
4912 gfc_traverse_ns (ns
, gfc_create_module_variable
);
4913 gfc_traverse_ns (ns
, create_module_nml_decl
);
4915 if (flag_coarray
== GFC_FCOARRAY_LIB
&& has_coarray_vars
)
4916 generate_coarray_init (ns
);
4920 gfc_trans_use_stmts (ns
);
4921 gfc_traverse_ns (ns
, gfc_emit_parameter_debug_info
);
4926 gfc_generate_contained_functions (gfc_namespace
* parent
)
4930 /* We create all the prototypes before generating any code. */
4931 for (ns
= parent
->contained
; ns
; ns
= ns
->sibling
)
4933 /* Skip namespaces from used modules. */
4934 if (ns
->parent
!= parent
)
4937 gfc_create_function_decl (ns
, false);
4940 for (ns
= parent
->contained
; ns
; ns
= ns
->sibling
)
4942 /* Skip namespaces from used modules. */
4943 if (ns
->parent
!= parent
)
4946 gfc_generate_function_code (ns
);
4951 /* Drill down through expressions for the array specification bounds and
4952 character length calling generate_local_decl for all those variables
4953 that have not already been declared. */
4956 generate_local_decl (gfc_symbol
*);
4958 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
4961 expr_decls (gfc_expr
*e
, gfc_symbol
*sym
,
4962 int *f ATTRIBUTE_UNUSED
)
4964 if (e
->expr_type
!= EXPR_VARIABLE
4965 || sym
== e
->symtree
->n
.sym
4966 || e
->symtree
->n
.sym
->mark
4967 || e
->symtree
->n
.sym
->ns
!= sym
->ns
)
4970 generate_local_decl (e
->symtree
->n
.sym
);
4975 generate_expr_decls (gfc_symbol
*sym
, gfc_expr
*e
)
4977 gfc_traverse_expr (e
, sym
, expr_decls
, 0);
4981 /* Check for dependencies in the character length and array spec. */
4984 generate_dependency_declarations (gfc_symbol
*sym
)
4988 if (sym
->ts
.type
== BT_CHARACTER
4990 && sym
->ts
.u
.cl
->length
4991 && sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
)
4992 generate_expr_decls (sym
, sym
->ts
.u
.cl
->length
);
4994 if (sym
->as
&& sym
->as
->rank
)
4996 for (i
= 0; i
< sym
->as
->rank
; i
++)
4998 generate_expr_decls (sym
, sym
->as
->lower
[i
]);
4999 generate_expr_decls (sym
, sym
->as
->upper
[i
]);
5005 /* Generate decls for all local variables. We do this to ensure correct
5006 handling of expressions which only appear in the specification of
5010 generate_local_decl (gfc_symbol
* sym
)
5012 if (sym
->attr
.flavor
== FL_VARIABLE
)
5014 if (sym
->attr
.codimension
&& !sym
->attr
.dummy
&& !sym
->attr
.allocatable
5015 && sym
->attr
.referenced
&& !sym
->attr
.use_assoc
)
5016 has_coarray_vars
= true;
5018 if (!sym
->attr
.dummy
&& !sym
->ns
->proc_name
->attr
.entry_master
)
5019 generate_dependency_declarations (sym
);
5021 if (sym
->attr
.referenced
)
5022 gfc_get_symbol_decl (sym
);
5024 /* Warnings for unused dummy arguments. */
5025 else if (sym
->attr
.dummy
&& !sym
->attr
.in_namelist
)
5027 /* INTENT(out) dummy arguments are likely meant to be set. */
5028 if (warn_unused_dummy_argument
&& sym
->attr
.intent
== INTENT_OUT
)
5030 if (sym
->ts
.type
!= BT_DERIVED
)
5031 gfc_warning (OPT_Wunused_dummy_argument
,
5032 "Dummy argument %qs at %L was declared "
5033 "INTENT(OUT) but was not set", sym
->name
,
5035 else if (!gfc_has_default_initializer (sym
->ts
.u
.derived
)
5036 && !sym
->ts
.u
.derived
->attr
.zero_comp
)
5037 gfc_warning (OPT_Wunused_dummy_argument
,
5038 "Derived-type dummy argument %qs at %L was "
5039 "declared INTENT(OUT) but was not set and "
5040 "does not have a default initializer",
5041 sym
->name
, &sym
->declared_at
);
5042 if (sym
->backend_decl
!= NULL_TREE
)
5043 TREE_NO_WARNING(sym
->backend_decl
) = 1;
5045 else if (warn_unused_dummy_argument
)
5047 gfc_warning (OPT_Wunused_dummy_argument
,
5048 "Unused dummy argument %qs at %L", sym
->name
,
5050 if (sym
->backend_decl
!= NULL_TREE
)
5051 TREE_NO_WARNING(sym
->backend_decl
) = 1;
5055 /* Warn for unused variables, but not if they're inside a common
5056 block or a namelist. */
5057 else if (warn_unused_variable
5058 && !(sym
->attr
.in_common
|| sym
->mark
|| sym
->attr
.in_namelist
))
5060 if (sym
->attr
.use_only
)
5062 gfc_warning (OPT_Wunused_variable
,
5063 "Unused module variable %qs which has been "
5064 "explicitly imported at %L", sym
->name
,
5066 if (sym
->backend_decl
!= NULL_TREE
)
5067 TREE_NO_WARNING(sym
->backend_decl
) = 1;
5069 else if (!sym
->attr
.use_assoc
)
5071 gfc_warning (OPT_Wunused_variable
,
5072 "Unused variable %qs declared at %L",
5073 sym
->name
, &sym
->declared_at
);
5074 if (sym
->backend_decl
!= NULL_TREE
)
5075 TREE_NO_WARNING(sym
->backend_decl
) = 1;
5079 /* For variable length CHARACTER parameters, the PARM_DECL already
5080 references the length variable, so force gfc_get_symbol_decl
5081 even when not referenced. If optimize > 0, it will be optimized
5082 away anyway. But do this only after emitting -Wunused-parameter
5083 warning if requested. */
5084 if (sym
->attr
.dummy
&& !sym
->attr
.referenced
5085 && sym
->ts
.type
== BT_CHARACTER
5086 && sym
->ts
.u
.cl
->backend_decl
!= NULL
5087 && TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) == VAR_DECL
)
5089 sym
->attr
.referenced
= 1;
5090 gfc_get_symbol_decl (sym
);
5093 /* INTENT(out) dummy arguments and result variables with allocatable
5094 components are reset by default and need to be set referenced to
5095 generate the code for nullification and automatic lengths. */
5096 if (!sym
->attr
.referenced
5097 && sym
->ts
.type
== BT_DERIVED
5098 && sym
->ts
.u
.derived
->attr
.alloc_comp
5099 && !sym
->attr
.pointer
5100 && ((sym
->attr
.dummy
&& sym
->attr
.intent
== INTENT_OUT
)
5102 (sym
->attr
.result
&& sym
!= sym
->result
)))
5104 sym
->attr
.referenced
= 1;
5105 gfc_get_symbol_decl (sym
);
5108 /* Check for dependencies in the array specification and string
5109 length, adding the necessary declarations to the function. We
5110 mark the symbol now, as well as in traverse_ns, to prevent
5111 getting stuck in a circular dependency. */
5114 else if (sym
->attr
.flavor
== FL_PARAMETER
)
5116 if (warn_unused_parameter
5117 && !sym
->attr
.referenced
)
5119 if (!sym
->attr
.use_assoc
)
5120 gfc_warning (OPT_Wunused_parameter
,
5121 "Unused parameter %qs declared at %L", sym
->name
,
5123 else if (sym
->attr
.use_only
)
5124 gfc_warning (OPT_Wunused_parameter
,
5125 "Unused parameter %qs which has been explicitly "
5126 "imported at %L", sym
->name
, &sym
->declared_at
);
5129 else if (sym
->attr
.flavor
== FL_PROCEDURE
)
5131 /* TODO: move to the appropriate place in resolve.c. */
5132 if (warn_return_type
5133 && sym
->attr
.function
5135 && sym
!= sym
->result
5136 && !sym
->result
->attr
.referenced
5137 && !sym
->attr
.use_assoc
5138 && sym
->attr
.if_source
!= IFSRC_IFBODY
)
5140 gfc_warning (OPT_Wreturn_type
,
5141 "Return value %qs of function %qs declared at "
5142 "%L not set", sym
->result
->name
, sym
->name
,
5143 &sym
->result
->declared_at
);
5145 /* Prevents "Unused variable" warning for RESULT variables. */
5146 sym
->result
->mark
= 1;
5150 if (sym
->attr
.dummy
== 1)
5152 /* Modify the tree type for scalar character dummy arguments of bind(c)
5153 procedures if they are passed by value. The tree type for them will
5154 be promoted to INTEGER_TYPE for the middle end, which appears to be
5155 what C would do with characters passed by-value. The value attribute
5156 implies the dummy is a scalar. */
5157 if (sym
->attr
.value
== 1 && sym
->backend_decl
!= NULL
5158 && sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.is_c_interop
5159 && sym
->ns
->proc_name
!= NULL
&& sym
->ns
->proc_name
->attr
.is_bind_c
)
5160 gfc_conv_scalar_char_value (sym
, NULL
, NULL
);
5162 /* Unused procedure passed as dummy argument. */
5163 if (sym
->attr
.flavor
== FL_PROCEDURE
)
5165 if (!sym
->attr
.referenced
)
5167 if (warn_unused_dummy_argument
)
5168 gfc_warning (OPT_Wunused_dummy_argument
,
5169 "Unused dummy argument %qs at %L", sym
->name
,
5173 /* Silence bogus "unused parameter" warnings from the
5175 if (sym
->backend_decl
!= NULL_TREE
)
5176 TREE_NO_WARNING (sym
->backend_decl
) = 1;
5180 /* Make sure we convert the types of the derived types from iso_c_binding
5182 if (sym
->attr
.flavor
!= FL_PROCEDURE
&& sym
->attr
.is_iso_c
5183 && sym
->ts
.type
== BT_DERIVED
)
5184 sym
->backend_decl
= gfc_typenode_for_spec (&(sym
->ts
));
5189 generate_local_nml_decl (gfc_symbol
* sym
)
5191 if (sym
->attr
.flavor
== FL_NAMELIST
&& !sym
->attr
.use_assoc
)
5193 tree decl
= generate_namelist_decl (sym
);
5200 generate_local_vars (gfc_namespace
* ns
)
5202 gfc_traverse_ns (ns
, generate_local_decl
);
5203 gfc_traverse_ns (ns
, generate_local_nml_decl
);
5207 /* Generate a switch statement to jump to the correct entry point. Also
5208 creates the label decls for the entry points. */
5211 gfc_trans_entry_master_switch (gfc_entry_list
* el
)
5218 gfc_init_block (&block
);
5219 for (; el
; el
= el
->next
)
5221 /* Add the case label. */
5222 label
= gfc_build_label_decl (NULL_TREE
);
5223 val
= build_int_cst (gfc_array_index_type
, el
->id
);
5224 tmp
= build_case_label (val
, NULL_TREE
, label
);
5225 gfc_add_expr_to_block (&block
, tmp
);
5227 /* And jump to the actual entry point. */
5228 label
= gfc_build_label_decl (NULL_TREE
);
5229 tmp
= build1_v (GOTO_EXPR
, label
);
5230 gfc_add_expr_to_block (&block
, tmp
);
5232 /* Save the label decl. */
5235 tmp
= gfc_finish_block (&block
);
5236 /* The first argument selects the entry point. */
5237 val
= DECL_ARGUMENTS (current_function_decl
);
5238 tmp
= fold_build3_loc (input_location
, SWITCH_EXPR
, NULL_TREE
,
5239 val
, tmp
, NULL_TREE
);
5244 /* Add code to string lengths of actual arguments passed to a function against
5245 the expected lengths of the dummy arguments. */
5248 add_argument_checking (stmtblock_t
*block
, gfc_symbol
*sym
)
5250 gfc_formal_arglist
*formal
;
5252 for (formal
= gfc_sym_get_dummy_args (sym
); formal
; formal
= formal
->next
)
5253 if (formal
->sym
&& formal
->sym
->ts
.type
== BT_CHARACTER
5254 && !formal
->sym
->ts
.deferred
)
5256 enum tree_code comparison
;
5261 const char *message
;
5267 gcc_assert (cl
->passed_length
!= NULL_TREE
);
5268 gcc_assert (cl
->backend_decl
!= NULL_TREE
);
5270 /* For POINTER, ALLOCATABLE and assumed-shape dummy arguments, the
5271 string lengths must match exactly. Otherwise, it is only required
5272 that the actual string length is *at least* the expected one.
5273 Sequence association allows for a mismatch of the string length
5274 if the actual argument is (part of) an array, but only if the
5275 dummy argument is an array. (See "Sequence association" in
5276 Section 12.4.1.4 for F95 and 12.4.1.5 for F2003.) */
5277 if (fsym
->attr
.pointer
|| fsym
->attr
.allocatable
5278 || (fsym
->as
&& (fsym
->as
->type
== AS_ASSUMED_SHAPE
5279 || fsym
->as
->type
== AS_ASSUMED_RANK
)))
5281 comparison
= NE_EXPR
;
5282 message
= _("Actual string length does not match the declared one"
5283 " for dummy argument '%s' (%ld/%ld)");
5285 else if (fsym
->as
&& fsym
->as
->rank
!= 0)
5289 comparison
= LT_EXPR
;
5290 message
= _("Actual string length is shorter than the declared one"
5291 " for dummy argument '%s' (%ld/%ld)");
5294 /* Build the condition. For optional arguments, an actual length
5295 of 0 is also acceptable if the associated string is NULL, which
5296 means the argument was not passed. */
5297 cond
= fold_build2_loc (input_location
, comparison
, boolean_type_node
,
5298 cl
->passed_length
, cl
->backend_decl
);
5299 if (fsym
->attr
.optional
)
5305 not_0length
= fold_build2_loc (input_location
, NE_EXPR
,
5308 build_zero_cst (gfc_charlen_type_node
));
5309 /* The symbol needs to be referenced for gfc_get_symbol_decl. */
5310 fsym
->attr
.referenced
= 1;
5311 not_absent
= gfc_conv_expr_present (fsym
);
5313 absent_failed
= fold_build2_loc (input_location
, TRUTH_OR_EXPR
,
5314 boolean_type_node
, not_0length
,
5317 cond
= fold_build2_loc (input_location
, TRUTH_AND_EXPR
,
5318 boolean_type_node
, cond
, absent_failed
);
5321 /* Build the runtime check. */
5322 argname
= gfc_build_cstring_const (fsym
->name
);
5323 argname
= gfc_build_addr_expr (pchar_type_node
, argname
);
5324 gfc_trans_runtime_check (true, false, cond
, block
, &fsym
->declared_at
,
5326 fold_convert (long_integer_type_node
,
5328 fold_convert (long_integer_type_node
,
5335 create_main_function (tree fndecl
)
5339 tree tmp
, decl
, result_decl
, argc
, argv
, typelist
, arglist
;
5342 old_context
= current_function_decl
;
5346 push_function_context ();
5347 saved_parent_function_decls
= saved_function_decls
;
5348 saved_function_decls
= NULL_TREE
;
5351 /* main() function must be declared with global scope. */
5352 gcc_assert (current_function_decl
== NULL_TREE
);
5354 /* Declare the function. */
5355 tmp
= build_function_type_list (integer_type_node
, integer_type_node
,
5356 build_pointer_type (pchar_type_node
),
5358 main_identifier_node
= get_identifier ("main");
5359 ftn_main
= build_decl (input_location
, FUNCTION_DECL
,
5360 main_identifier_node
, tmp
);
5361 DECL_EXTERNAL (ftn_main
) = 0;
5362 TREE_PUBLIC (ftn_main
) = 1;
5363 TREE_STATIC (ftn_main
) = 1;
5364 DECL_ATTRIBUTES (ftn_main
)
5365 = tree_cons (get_identifier("externally_visible"), NULL_TREE
, NULL_TREE
);
5367 /* Setup the result declaration (for "return 0"). */
5368 result_decl
= build_decl (input_location
,
5369 RESULT_DECL
, NULL_TREE
, integer_type_node
);
5370 DECL_ARTIFICIAL (result_decl
) = 1;
5371 DECL_IGNORED_P (result_decl
) = 1;
5372 DECL_CONTEXT (result_decl
) = ftn_main
;
5373 DECL_RESULT (ftn_main
) = result_decl
;
5375 pushdecl (ftn_main
);
5377 /* Get the arguments. */
5379 arglist
= NULL_TREE
;
5380 typelist
= TYPE_ARG_TYPES (TREE_TYPE (ftn_main
));
5382 tmp
= TREE_VALUE (typelist
);
5383 argc
= build_decl (input_location
, PARM_DECL
, get_identifier ("argc"), tmp
);
5384 DECL_CONTEXT (argc
) = ftn_main
;
5385 DECL_ARG_TYPE (argc
) = TREE_VALUE (typelist
);
5386 TREE_READONLY (argc
) = 1;
5387 gfc_finish_decl (argc
);
5388 arglist
= chainon (arglist
, argc
);
5390 typelist
= TREE_CHAIN (typelist
);
5391 tmp
= TREE_VALUE (typelist
);
5392 argv
= build_decl (input_location
, PARM_DECL
, get_identifier ("argv"), tmp
);
5393 DECL_CONTEXT (argv
) = ftn_main
;
5394 DECL_ARG_TYPE (argv
) = TREE_VALUE (typelist
);
5395 TREE_READONLY (argv
) = 1;
5396 DECL_BY_REFERENCE (argv
) = 1;
5397 gfc_finish_decl (argv
);
5398 arglist
= chainon (arglist
, argv
);
5400 DECL_ARGUMENTS (ftn_main
) = arglist
;
5401 current_function_decl
= ftn_main
;
5402 announce_function (ftn_main
);
5404 rest_of_decl_compilation (ftn_main
, 1, 0);
5405 make_decl_rtl (ftn_main
);
5406 allocate_struct_function (ftn_main
, false);
5409 gfc_init_block (&body
);
5411 /* Call some libgfortran initialization routines, call then MAIN__(). */
5413 /* Call _gfortran_caf_init (*argc, ***argv). */
5414 if (flag_coarray
== GFC_FCOARRAY_LIB
)
5416 tree pint_type
, pppchar_type
;
5417 pint_type
= build_pointer_type (integer_type_node
);
5419 = build_pointer_type (build_pointer_type (pchar_type_node
));
5421 tmp
= build_call_expr_loc (input_location
, gfor_fndecl_caf_init
, 2,
5422 gfc_build_addr_expr (pint_type
, argc
),
5423 gfc_build_addr_expr (pppchar_type
, argv
));
5424 gfc_add_expr_to_block (&body
, tmp
);
5427 /* Call _gfortran_set_args (argc, argv). */
5428 TREE_USED (argc
) = 1;
5429 TREE_USED (argv
) = 1;
5430 tmp
= build_call_expr_loc (input_location
,
5431 gfor_fndecl_set_args
, 2, argc
, argv
);
5432 gfc_add_expr_to_block (&body
, tmp
);
5434 /* Add a call to set_options to set up the runtime library Fortran
5435 language standard parameters. */
5437 tree array_type
, array
, var
;
5438 vec
<constructor_elt
, va_gc
> *v
= NULL
;
5440 /* Passing a new option to the library requires four modifications:
5441 + add it to the tree_cons list below
5442 + change the array size in the call to build_array_type
5443 + change the first argument to the library call
5444 gfor_fndecl_set_options
5445 + modify the library (runtime/compile_options.c)! */
5447 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5448 build_int_cst (integer_type_node
,
5449 gfc_option
.warn_std
));
5450 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5451 build_int_cst (integer_type_node
,
5452 gfc_option
.allow_std
));
5453 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5454 build_int_cst (integer_type_node
, pedantic
));
5455 /* TODO: This is the old -fdump-core option, which is unused but
5456 passed due to ABI compatibility; remove when bumping the
5458 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5459 build_int_cst (integer_type_node
,
5461 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5462 build_int_cst (integer_type_node
, flag_backtrace
));
5463 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5464 build_int_cst (integer_type_node
, flag_sign_zero
));
5465 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5466 build_int_cst (integer_type_node
,
5468 & GFC_RTCHECK_BOUNDS
)));
5469 /* TODO: This is the -frange-check option, which no longer affects
5470 library behavior; when bumping the library ABI this slot can be
5471 reused for something else. As it is the last element in the
5472 array, we can instead leave it out altogether. */
5473 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5474 build_int_cst (integer_type_node
, 0));
5475 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5476 build_int_cst (integer_type_node
,
5477 gfc_option
.fpe_summary
));
5479 array_type
= build_array_type (integer_type_node
,
5480 build_index_type (size_int (8)));
5481 array
= build_constructor (array_type
, v
);
5482 TREE_CONSTANT (array
) = 1;
5483 TREE_STATIC (array
) = 1;
5485 /* Create a static variable to hold the jump table. */
5486 var
= build_decl (input_location
, VAR_DECL
,
5487 create_tmp_var_name ("options"),
5489 DECL_ARTIFICIAL (var
) = 1;
5490 DECL_IGNORED_P (var
) = 1;
5491 TREE_CONSTANT (var
) = 1;
5492 TREE_STATIC (var
) = 1;
5493 TREE_READONLY (var
) = 1;
5494 DECL_INITIAL (var
) = array
;
5496 var
= gfc_build_addr_expr (build_pointer_type (integer_type_node
), var
);
5498 tmp
= build_call_expr_loc (input_location
,
5499 gfor_fndecl_set_options
, 2,
5500 build_int_cst (integer_type_node
, 9), var
);
5501 gfc_add_expr_to_block (&body
, tmp
);
5504 /* If -ffpe-trap option was provided, add a call to set_fpe so that
5505 the library will raise a FPE when needed. */
5506 if (gfc_option
.fpe
!= 0)
5508 tmp
= build_call_expr_loc (input_location
,
5509 gfor_fndecl_set_fpe
, 1,
5510 build_int_cst (integer_type_node
,
5512 gfc_add_expr_to_block (&body
, tmp
);
5515 /* If this is the main program and an -fconvert option was provided,
5516 add a call to set_convert. */
5518 if (flag_convert
!= GFC_FLAG_CONVERT_NATIVE
)
5520 tmp
= build_call_expr_loc (input_location
,
5521 gfor_fndecl_set_convert
, 1,
5522 build_int_cst (integer_type_node
, flag_convert
));
5523 gfc_add_expr_to_block (&body
, tmp
);
5526 /* If this is the main program and an -frecord-marker option was provided,
5527 add a call to set_record_marker. */
5529 if (flag_record_marker
!= 0)
5531 tmp
= build_call_expr_loc (input_location
,
5532 gfor_fndecl_set_record_marker
, 1,
5533 build_int_cst (integer_type_node
,
5534 flag_record_marker
));
5535 gfc_add_expr_to_block (&body
, tmp
);
5538 if (flag_max_subrecord_length
!= 0)
5540 tmp
= build_call_expr_loc (input_location
,
5541 gfor_fndecl_set_max_subrecord_length
, 1,
5542 build_int_cst (integer_type_node
,
5543 flag_max_subrecord_length
));
5544 gfc_add_expr_to_block (&body
, tmp
);
5547 /* Call MAIN__(). */
5548 tmp
= build_call_expr_loc (input_location
,
5550 gfc_add_expr_to_block (&body
, tmp
);
5552 /* Mark MAIN__ as used. */
5553 TREE_USED (fndecl
) = 1;
5555 /* Coarray: Call _gfortran_caf_finalize(void). */
5556 if (flag_coarray
== GFC_FCOARRAY_LIB
)
5558 /* Per F2008, 8.5.1 END of the main program implies a
5560 tmp
= builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE
);
5561 tmp
= build_call_expr_loc (input_location
, tmp
, 0);
5562 gfc_add_expr_to_block (&body
, tmp
);
5564 tmp
= build_call_expr_loc (input_location
, gfor_fndecl_caf_finalize
, 0);
5565 gfc_add_expr_to_block (&body
, tmp
);
5569 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
, integer_type_node
,
5570 DECL_RESULT (ftn_main
),
5571 build_int_cst (integer_type_node
, 0));
5572 tmp
= build1_v (RETURN_EXPR
, tmp
);
5573 gfc_add_expr_to_block (&body
, tmp
);
5576 DECL_SAVED_TREE (ftn_main
) = gfc_finish_block (&body
);
5579 /* Finish off this function and send it for code generation. */
5581 BLOCK_SUPERCONTEXT (DECL_INITIAL (ftn_main
)) = ftn_main
;
5583 DECL_SAVED_TREE (ftn_main
)
5584 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (ftn_main
),
5585 DECL_INITIAL (ftn_main
));
5587 /* Output the GENERIC tree. */
5588 dump_function (TDI_original
, ftn_main
);
5590 cgraph_node::finalize_function (ftn_main
, true);
5594 pop_function_context ();
5595 saved_function_decls
= saved_parent_function_decls
;
5597 current_function_decl
= old_context
;
5601 /* Get the result expression for a procedure. */
5604 get_proc_result (gfc_symbol
* sym
)
5606 if (sym
->attr
.subroutine
|| sym
== sym
->result
)
5608 if (current_fake_result_decl
!= NULL
)
5609 return TREE_VALUE (current_fake_result_decl
);
5614 return sym
->result
->backend_decl
;
5618 /* Generate an appropriate return-statement for a procedure. */
5621 gfc_generate_return (void)
5627 sym
= current_procedure_symbol
;
5628 fndecl
= sym
->backend_decl
;
5630 if (TREE_TYPE (DECL_RESULT (fndecl
)) == void_type_node
)
5634 result
= get_proc_result (sym
);
5636 /* Set the return value to the dummy result variable. The
5637 types may be different for scalar default REAL functions
5638 with -ff2c, therefore we have to convert. */
5639 if (result
!= NULL_TREE
)
5641 result
= convert (TREE_TYPE (DECL_RESULT (fndecl
)), result
);
5642 result
= fold_build2_loc (input_location
, MODIFY_EXPR
,
5643 TREE_TYPE (result
), DECL_RESULT (fndecl
),
5648 return build1_v (RETURN_EXPR
, result
);
5653 is_from_ieee_module (gfc_symbol
*sym
)
5655 if (sym
->from_intmod
== INTMOD_IEEE_FEATURES
5656 || sym
->from_intmod
== INTMOD_IEEE_EXCEPTIONS
5657 || sym
->from_intmod
== INTMOD_IEEE_ARITHMETIC
)
5658 seen_ieee_symbol
= 1;
5663 is_ieee_module_used (gfc_namespace
*ns
)
5665 seen_ieee_symbol
= 0;
5666 gfc_traverse_ns (ns
, is_from_ieee_module
);
5667 return seen_ieee_symbol
;
5671 /* Generate code for a function. */
5674 gfc_generate_function_code (gfc_namespace
* ns
)
5680 tree fpstate
= NULL_TREE
;
5681 stmtblock_t init
, cleanup
;
5683 gfc_wrapped_block try_block
;
5684 tree recurcheckvar
= NULL_TREE
;
5686 gfc_symbol
*previous_procedure_symbol
;
5690 sym
= ns
->proc_name
;
5691 previous_procedure_symbol
= current_procedure_symbol
;
5692 current_procedure_symbol
= sym
;
5694 /* Check that the frontend isn't still using this. */
5695 gcc_assert (sym
->tlink
== NULL
);
5698 /* Create the declaration for functions with global scope. */
5699 if (!sym
->backend_decl
)
5700 gfc_create_function_decl (ns
, false);
5702 fndecl
= sym
->backend_decl
;
5703 old_context
= current_function_decl
;
5707 push_function_context ();
5708 saved_parent_function_decls
= saved_function_decls
;
5709 saved_function_decls
= NULL_TREE
;
5712 trans_function_start (sym
);
5714 gfc_init_block (&init
);
5716 if (ns
->entries
&& ns
->proc_name
->ts
.type
== BT_CHARACTER
)
5718 /* Copy length backend_decls to all entry point result
5723 gfc_conv_const_charlen (ns
->proc_name
->ts
.u
.cl
);
5724 backend_decl
= ns
->proc_name
->result
->ts
.u
.cl
->backend_decl
;
5725 for (el
= ns
->entries
; el
; el
= el
->next
)
5726 el
->sym
->result
->ts
.u
.cl
->backend_decl
= backend_decl
;
5729 /* Translate COMMON blocks. */
5730 gfc_trans_common (ns
);
5732 /* Null the parent fake result declaration if this namespace is
5733 a module function or an external procedures. */
5734 if ((ns
->parent
&& ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
5735 || ns
->parent
== NULL
)
5736 parent_fake_result_decl
= NULL_TREE
;
5738 gfc_generate_contained_functions (ns
);
5740 nonlocal_dummy_decls
= NULL
;
5741 nonlocal_dummy_decl_pset
= NULL
;
5743 has_coarray_vars
= false;
5744 generate_local_vars (ns
);
5746 if (flag_coarray
== GFC_FCOARRAY_LIB
&& has_coarray_vars
)
5747 generate_coarray_init (ns
);
5749 /* Keep the parent fake result declaration in module functions
5750 or external procedures. */
5751 if ((ns
->parent
&& ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
5752 || ns
->parent
== NULL
)
5753 current_fake_result_decl
= parent_fake_result_decl
;
5755 current_fake_result_decl
= NULL_TREE
;
5757 is_recursive
= sym
->attr
.recursive
5758 || (sym
->attr
.entry_master
5759 && sym
->ns
->entries
->sym
->attr
.recursive
);
5760 if ((gfc_option
.rtcheck
& GFC_RTCHECK_RECURSION
)
5761 && !is_recursive
&& !flag_recursive
)
5765 msg
= xasprintf ("Recursive call to nonrecursive procedure '%s'",
5767 recurcheckvar
= gfc_create_var (boolean_type_node
, "is_recursive");
5768 TREE_STATIC (recurcheckvar
) = 1;
5769 DECL_INITIAL (recurcheckvar
) = boolean_false_node
;
5770 gfc_add_expr_to_block (&init
, recurcheckvar
);
5771 gfc_trans_runtime_check (true, false, recurcheckvar
, &init
,
5772 &sym
->declared_at
, msg
);
5773 gfc_add_modify (&init
, recurcheckvar
, boolean_true_node
);
5777 /* Check if an IEEE module is used in the procedure. If so, save
5778 the floating point state. */
5779 ieee
= is_ieee_module_used (ns
);
5781 fpstate
= gfc_save_fp_state (&init
);
5783 /* Now generate the code for the body of this function. */
5784 gfc_init_block (&body
);
5786 if (TREE_TYPE (DECL_RESULT (fndecl
)) != void_type_node
5787 && sym
->attr
.subroutine
)
5789 tree alternate_return
;
5790 alternate_return
= gfc_get_fake_result_decl (sym
, 0);
5791 gfc_add_modify (&body
, alternate_return
, integer_zero_node
);
5796 /* Jump to the correct entry point. */
5797 tmp
= gfc_trans_entry_master_switch (ns
->entries
);
5798 gfc_add_expr_to_block (&body
, tmp
);
5801 /* If bounds-checking is enabled, generate code to check passed in actual
5802 arguments against the expected dummy argument attributes (e.g. string
5804 if ((gfc_option
.rtcheck
& GFC_RTCHECK_BOUNDS
) && !sym
->attr
.is_bind_c
)
5805 add_argument_checking (&body
, sym
);
5807 /* Generate !$ACC DECLARE directive. */
5808 if (ns
->oacc_declare_clauses
)
5810 tree tmp
= gfc_trans_oacc_declare (&body
, ns
);
5811 gfc_add_expr_to_block (&body
, tmp
);
5814 tmp
= gfc_trans_code (ns
->code
);
5815 gfc_add_expr_to_block (&body
, tmp
);
5817 if (TREE_TYPE (DECL_RESULT (fndecl
)) != void_type_node
)
5819 tree result
= get_proc_result (sym
);
5821 if (result
!= NULL_TREE
&& sym
->attr
.function
&& !sym
->attr
.pointer
)
5823 if (sym
->attr
.allocatable
&& sym
->attr
.dimension
== 0
5824 && sym
->result
== sym
)
5825 gfc_add_modify (&init
, result
, fold_convert (TREE_TYPE (result
),
5826 null_pointer_node
));
5827 else if (sym
->ts
.type
== BT_CLASS
5828 && CLASS_DATA (sym
)->attr
.allocatable
5829 && CLASS_DATA (sym
)->attr
.dimension
== 0
5830 && sym
->result
== sym
)
5832 tmp
= CLASS_DATA (sym
)->backend_decl
;
5833 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
,
5834 TREE_TYPE (tmp
), result
, tmp
, NULL_TREE
);
5835 gfc_add_modify (&init
, tmp
, fold_convert (TREE_TYPE (tmp
),
5836 null_pointer_node
));
5838 else if (sym
->ts
.type
== BT_DERIVED
5839 && sym
->ts
.u
.derived
->attr
.alloc_comp
5840 && !sym
->attr
.allocatable
)
5842 rank
= sym
->as
? sym
->as
->rank
: 0;
5843 tmp
= gfc_nullify_alloc_comp (sym
->ts
.u
.derived
, result
, rank
);
5844 gfc_add_expr_to_block (&init
, tmp
);
5848 if (result
== NULL_TREE
)
5850 /* TODO: move to the appropriate place in resolve.c. */
5851 if (warn_return_type
&& sym
== sym
->result
)
5852 gfc_warning (OPT_Wreturn_type
,
5853 "Return value of function %qs at %L not set",
5854 sym
->name
, &sym
->declared_at
);
5855 if (warn_return_type
)
5856 TREE_NO_WARNING(sym
->backend_decl
) = 1;
5859 gfc_add_expr_to_block (&body
, gfc_generate_return ());
5862 gfc_init_block (&cleanup
);
5864 /* Reset recursion-check variable. */
5865 if ((gfc_option
.rtcheck
& GFC_RTCHECK_RECURSION
)
5866 && !is_recursive
&& !flag_openmp
&& recurcheckvar
!= NULL_TREE
)
5868 gfc_add_modify (&cleanup
, recurcheckvar
, boolean_false_node
);
5869 recurcheckvar
= NULL
;
5872 /* If IEEE modules are loaded, restore the floating-point state. */
5874 gfc_restore_fp_state (&cleanup
, fpstate
);
5876 /* Finish the function body and add init and cleanup code. */
5877 tmp
= gfc_finish_block (&body
);
5878 gfc_start_wrapped_block (&try_block
, tmp
);
5879 /* Add code to create and cleanup arrays. */
5880 gfc_trans_deferred_vars (sym
, &try_block
);
5881 gfc_add_init_cleanup (&try_block
, gfc_finish_block (&init
),
5882 gfc_finish_block (&cleanup
));
5884 /* Add all the decls we created during processing. */
5885 decl
= saved_function_decls
;
5890 next
= DECL_CHAIN (decl
);
5891 DECL_CHAIN (decl
) = NULL_TREE
;
5895 saved_function_decls
= NULL_TREE
;
5897 DECL_SAVED_TREE (fndecl
) = gfc_finish_wrapped_block (&try_block
);
5900 /* Finish off this function and send it for code generation. */
5902 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
5904 DECL_SAVED_TREE (fndecl
)
5905 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (fndecl
),
5906 DECL_INITIAL (fndecl
));
5908 if (nonlocal_dummy_decls
)
5910 BLOCK_VARS (DECL_INITIAL (fndecl
))
5911 = chainon (BLOCK_VARS (DECL_INITIAL (fndecl
)), nonlocal_dummy_decls
);
5912 delete nonlocal_dummy_decl_pset
;
5913 nonlocal_dummy_decls
= NULL
;
5914 nonlocal_dummy_decl_pset
= NULL
;
5917 /* Output the GENERIC tree. */
5918 dump_function (TDI_original
, fndecl
);
5920 /* Store the end of the function, so that we get good line number
5921 info for the epilogue. */
5922 cfun
->function_end_locus
= input_location
;
5924 /* We're leaving the context of this function, so zap cfun.
5925 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
5926 tree_rest_of_compilation. */
5931 pop_function_context ();
5932 saved_function_decls
= saved_parent_function_decls
;
5934 current_function_decl
= old_context
;
5936 if (decl_function_context (fndecl
))
5938 /* Register this function with cgraph just far enough to get it
5939 added to our parent's nested function list.
5940 If there are static coarrays in this function, the nested _caf_init
5941 function has already called cgraph_create_node, which also created
5942 the cgraph node for this function. */
5943 if (!has_coarray_vars
|| flag_coarray
!= GFC_FCOARRAY_LIB
)
5944 (void) cgraph_node::create (fndecl
);
5947 cgraph_node::finalize_function (fndecl
, true);
5949 gfc_trans_use_stmts (ns
);
5950 gfc_traverse_ns (ns
, gfc_emit_parameter_debug_info
);
5952 if (sym
->attr
.is_main_program
)
5953 create_main_function (fndecl
);
5955 current_procedure_symbol
= previous_procedure_symbol
;
5960 gfc_generate_constructors (void)
5962 gcc_assert (gfc_static_ctors
== NULL_TREE
);
5970 if (gfc_static_ctors
== NULL_TREE
)
5973 fnname
= get_file_function_name ("I");
5974 type
= build_function_type_list (void_type_node
, NULL_TREE
);
5976 fndecl
= build_decl (input_location
,
5977 FUNCTION_DECL
, fnname
, type
);
5978 TREE_PUBLIC (fndecl
) = 1;
5980 decl
= build_decl (input_location
,
5981 RESULT_DECL
, NULL_TREE
, void_type_node
);
5982 DECL_ARTIFICIAL (decl
) = 1;
5983 DECL_IGNORED_P (decl
) = 1;
5984 DECL_CONTEXT (decl
) = fndecl
;
5985 DECL_RESULT (fndecl
) = decl
;
5989 current_function_decl
= fndecl
;
5991 rest_of_decl_compilation (fndecl
, 1, 0);
5993 make_decl_rtl (fndecl
);
5995 allocate_struct_function (fndecl
, false);
5999 for (; gfc_static_ctors
; gfc_static_ctors
= TREE_CHAIN (gfc_static_ctors
))
6001 tmp
= build_call_expr_loc (input_location
,
6002 TREE_VALUE (gfc_static_ctors
), 0);
6003 DECL_SAVED_TREE (fndecl
) = build_stmt (input_location
, EXPR_STMT
, tmp
);
6009 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
6010 DECL_SAVED_TREE (fndecl
)
6011 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (fndecl
),
6012 DECL_INITIAL (fndecl
));
6014 free_after_parsing (cfun
);
6015 free_after_compilation (cfun
);
6017 tree_rest_of_compilation (fndecl
);
6019 current_function_decl
= NULL_TREE
;
6023 /* Translates a BLOCK DATA program unit. This means emitting the
6024 commons contained therein plus their initializations. We also emit
6025 a globally visible symbol to make sure that each BLOCK DATA program
6026 unit remains unique. */
6029 gfc_generate_block_data (gfc_namespace
* ns
)
6034 /* Tell the backend the source location of the block data. */
6036 gfc_set_backend_locus (&ns
->proc_name
->declared_at
);
6038 gfc_set_backend_locus (&gfc_current_locus
);
6040 /* Process the DATA statements. */
6041 gfc_trans_common (ns
);
6043 /* Create a global symbol with the mane of the block data. This is to
6044 generate linker errors if the same name is used twice. It is never
6047 id
= gfc_sym_mangled_function_id (ns
->proc_name
);
6049 id
= get_identifier ("__BLOCK_DATA__");
6051 decl
= build_decl (input_location
,
6052 VAR_DECL
, id
, gfc_array_index_type
);
6053 TREE_PUBLIC (decl
) = 1;
6054 TREE_STATIC (decl
) = 1;
6055 DECL_IGNORED_P (decl
) = 1;
6058 rest_of_decl_compilation (decl
, 1, 0);
6062 /* Process the local variables of a BLOCK construct. */
6065 gfc_process_block_locals (gfc_namespace
* ns
)
6069 gcc_assert (saved_local_decls
== NULL_TREE
);
6070 has_coarray_vars
= false;
6072 generate_local_vars (ns
);
6074 if (flag_coarray
== GFC_FCOARRAY_LIB
&& has_coarray_vars
)
6075 generate_coarray_init (ns
);
6077 decl
= saved_local_decls
;
6082 next
= DECL_CHAIN (decl
);
6083 DECL_CHAIN (decl
) = NULL_TREE
;
6087 saved_local_decls
= NULL_TREE
;
6091 #include "gt-fortran-trans-decl.h"