1 /* Backend function setup
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Paul Brook
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* trans-decl.c -- Handling of backend function and variable decls, etc */
26 #include "coretypes.h"
29 #include "tree-dump.h"
30 #include "gimple.h" /* For create_tmp_var_raw. */
32 #include "diagnostic-core.h" /* For internal_error. */
33 #include "toplev.h" /* For announce_function. */
34 #include "output.h" /* For decl_default_tls_model. */
41 #include "pointer-set.h"
42 #include "constructor.h"
44 #include "trans-types.h"
45 #include "trans-array.h"
46 #include "trans-const.h"
47 /* Only for gfc_trans_code. Shouldn't need to include this. */
48 #include "trans-stmt.h"
50 #define MAX_LABEL_VALUE 99999
53 /* Holds the result of the function if no result variable specified. */
55 static GTY(()) tree current_fake_result_decl
;
56 static GTY(()) tree parent_fake_result_decl
;
59 /* Holds the variable DECLs for the current function. */
61 static GTY(()) tree saved_function_decls
;
62 static GTY(()) tree saved_parent_function_decls
;
64 static struct pointer_set_t
*nonlocal_dummy_decl_pset
;
65 static GTY(()) tree nonlocal_dummy_decls
;
67 /* Holds the variable DECLs that are locals. */
69 static GTY(()) tree saved_local_decls
;
71 /* The namespace of the module we're currently generating. Only used while
72 outputting decls for module variables. Do not rely on this being set. */
74 static gfc_namespace
*module_namespace
;
76 /* The currently processed procedure symbol. */
77 static gfc_symbol
* current_procedure_symbol
= NULL
;
80 /* List of static constructor functions. */
82 tree gfc_static_ctors
;
85 /* Function declarations for builtin library functions. */
87 tree gfor_fndecl_pause_numeric
;
88 tree gfor_fndecl_pause_string
;
89 tree gfor_fndecl_stop_numeric
;
90 tree gfor_fndecl_stop_string
;
91 tree gfor_fndecl_error_stop_numeric
;
92 tree gfor_fndecl_error_stop_string
;
93 tree gfor_fndecl_runtime_error
;
94 tree gfor_fndecl_runtime_error_at
;
95 tree gfor_fndecl_runtime_warning_at
;
96 tree gfor_fndecl_os_error
;
97 tree gfor_fndecl_generate_error
;
98 tree gfor_fndecl_set_args
;
99 tree gfor_fndecl_set_fpe
;
100 tree gfor_fndecl_set_options
;
101 tree gfor_fndecl_set_convert
;
102 tree gfor_fndecl_set_record_marker
;
103 tree gfor_fndecl_set_max_subrecord_length
;
104 tree gfor_fndecl_ctime
;
105 tree gfor_fndecl_fdate
;
106 tree gfor_fndecl_ttynam
;
107 tree gfor_fndecl_in_pack
;
108 tree gfor_fndecl_in_unpack
;
109 tree gfor_fndecl_associated
;
112 /* Math functions. Many other math functions are handled in
113 trans-intrinsic.c. */
115 gfc_powdecl_list gfor_fndecl_math_powi
[4][3];
116 tree gfor_fndecl_math_ishftc4
;
117 tree gfor_fndecl_math_ishftc8
;
118 tree gfor_fndecl_math_ishftc16
;
121 /* String functions. */
123 tree gfor_fndecl_compare_string
;
124 tree gfor_fndecl_concat_string
;
125 tree gfor_fndecl_string_len_trim
;
126 tree gfor_fndecl_string_index
;
127 tree gfor_fndecl_string_scan
;
128 tree gfor_fndecl_string_verify
;
129 tree gfor_fndecl_string_trim
;
130 tree gfor_fndecl_string_minmax
;
131 tree gfor_fndecl_adjustl
;
132 tree gfor_fndecl_adjustr
;
133 tree gfor_fndecl_select_string
;
134 tree gfor_fndecl_compare_string_char4
;
135 tree gfor_fndecl_concat_string_char4
;
136 tree gfor_fndecl_string_len_trim_char4
;
137 tree gfor_fndecl_string_index_char4
;
138 tree gfor_fndecl_string_scan_char4
;
139 tree gfor_fndecl_string_verify_char4
;
140 tree gfor_fndecl_string_trim_char4
;
141 tree gfor_fndecl_string_minmax_char4
;
142 tree gfor_fndecl_adjustl_char4
;
143 tree gfor_fndecl_adjustr_char4
;
144 tree gfor_fndecl_select_string_char4
;
147 /* Conversion between character kinds. */
148 tree gfor_fndecl_convert_char1_to_char4
;
149 tree gfor_fndecl_convert_char4_to_char1
;
152 /* Other misc. runtime library functions. */
154 tree gfor_fndecl_size0
;
155 tree gfor_fndecl_size1
;
156 tree gfor_fndecl_iargc
;
157 tree gfor_fndecl_clz128
;
158 tree gfor_fndecl_ctz128
;
160 /* Intrinsic functions implemented in Fortran. */
161 tree gfor_fndecl_sc_kind
;
162 tree gfor_fndecl_si_kind
;
163 tree gfor_fndecl_sr_kind
;
165 /* BLAS gemm functions. */
166 tree gfor_fndecl_sgemm
;
167 tree gfor_fndecl_dgemm
;
168 tree gfor_fndecl_cgemm
;
169 tree gfor_fndecl_zgemm
;
173 gfc_add_decl_to_parent_function (tree decl
)
176 DECL_CONTEXT (decl
) = DECL_CONTEXT (current_function_decl
);
177 DECL_NONLOCAL (decl
) = 1;
178 DECL_CHAIN (decl
) = saved_parent_function_decls
;
179 saved_parent_function_decls
= decl
;
183 gfc_add_decl_to_function (tree decl
)
186 TREE_USED (decl
) = 1;
187 DECL_CONTEXT (decl
) = current_function_decl
;
188 DECL_CHAIN (decl
) = saved_function_decls
;
189 saved_function_decls
= decl
;
193 add_decl_as_local (tree decl
)
196 TREE_USED (decl
) = 1;
197 DECL_CONTEXT (decl
) = current_function_decl
;
198 DECL_CHAIN (decl
) = saved_local_decls
;
199 saved_local_decls
= decl
;
203 /* Build a backend label declaration. Set TREE_USED for named labels.
204 The context of the label is always the current_function_decl. All
205 labels are marked artificial. */
208 gfc_build_label_decl (tree label_id
)
210 /* 2^32 temporaries should be enough. */
211 static unsigned int tmp_num
= 1;
215 if (label_id
== NULL_TREE
)
217 /* Build an internal label name. */
218 ASM_FORMAT_PRIVATE_NAME (label_name
, "L", tmp_num
++);
219 label_id
= get_identifier (label_name
);
224 /* Build the LABEL_DECL node. Labels have no type. */
225 label_decl
= build_decl (input_location
,
226 LABEL_DECL
, label_id
, void_type_node
);
227 DECL_CONTEXT (label_decl
) = current_function_decl
;
228 DECL_MODE (label_decl
) = VOIDmode
;
230 /* We always define the label as used, even if the original source
231 file never references the label. We don't want all kinds of
232 spurious warnings for old-style Fortran code with too many
234 TREE_USED (label_decl
) = 1;
236 DECL_ARTIFICIAL (label_decl
) = 1;
241 /* Set the backend source location of a decl. */
244 gfc_set_decl_location (tree decl
, locus
* loc
)
246 DECL_SOURCE_LOCATION (decl
) = loc
->lb
->location
;
250 /* Return the backend label declaration for a given label structure,
251 or create it if it doesn't exist yet. */
254 gfc_get_label_decl (gfc_st_label
* lp
)
256 if (lp
->backend_decl
)
257 return lp
->backend_decl
;
260 char label_name
[GFC_MAX_SYMBOL_LEN
+ 1];
263 /* Validate the label declaration from the front end. */
264 gcc_assert (lp
!= NULL
&& lp
->value
<= MAX_LABEL_VALUE
);
266 /* Build a mangled name for the label. */
267 sprintf (label_name
, "__label_%.6d", lp
->value
);
269 /* Build the LABEL_DECL node. */
270 label_decl
= gfc_build_label_decl (get_identifier (label_name
));
272 /* Tell the debugger where the label came from. */
273 if (lp
->value
<= MAX_LABEL_VALUE
) /* An internal label. */
274 gfc_set_decl_location (label_decl
, &lp
->where
);
276 DECL_ARTIFICIAL (label_decl
) = 1;
278 /* Store the label in the label list and return the LABEL_DECL. */
279 lp
->backend_decl
= label_decl
;
285 /* Convert a gfc_symbol to an identifier of the same name. */
288 gfc_sym_identifier (gfc_symbol
* sym
)
290 if (sym
->attr
.is_main_program
&& strcmp (sym
->name
, "main") == 0)
291 return (get_identifier ("MAIN__"));
293 return (get_identifier (sym
->name
));
297 /* Construct mangled name from symbol name. */
300 gfc_sym_mangled_identifier (gfc_symbol
* sym
)
302 char name
[GFC_MAX_MANGLED_SYMBOL_LEN
+ 1];
304 /* Prevent the mangling of identifiers that have an assigned
305 binding label (mainly those that are bind(c)). */
306 if (sym
->attr
.is_bind_c
== 1
307 && sym
->binding_label
[0] != '\0')
308 return get_identifier(sym
->binding_label
);
310 if (sym
->module
== NULL
)
311 return gfc_sym_identifier (sym
);
314 snprintf (name
, sizeof name
, "__%s_MOD_%s", sym
->module
, sym
->name
);
315 return get_identifier (name
);
320 /* Construct mangled function name from symbol name. */
323 gfc_sym_mangled_function_id (gfc_symbol
* sym
)
326 char name
[GFC_MAX_MANGLED_SYMBOL_LEN
+ 1];
328 /* It may be possible to simply use the binding label if it's
329 provided, and remove the other checks. Then we could use it
330 for other things if we wished. */
331 if ((sym
->attr
.is_bind_c
== 1 || sym
->attr
.is_iso_c
== 1) &&
332 sym
->binding_label
[0] != '\0')
333 /* use the binding label rather than the mangled name */
334 return get_identifier (sym
->binding_label
);
336 if (sym
->module
== NULL
|| sym
->attr
.proc
== PROC_EXTERNAL
337 || (sym
->module
!= NULL
&& (sym
->attr
.external
338 || sym
->attr
.if_source
== IFSRC_IFBODY
)))
340 /* Main program is mangled into MAIN__. */
341 if (sym
->attr
.is_main_program
)
342 return get_identifier ("MAIN__");
344 /* Intrinsic procedures are never mangled. */
345 if (sym
->attr
.proc
== PROC_INTRINSIC
)
346 return get_identifier (sym
->name
);
348 if (gfc_option
.flag_underscoring
)
350 has_underscore
= strchr (sym
->name
, '_') != 0;
351 if (gfc_option
.flag_second_underscore
&& has_underscore
)
352 snprintf (name
, sizeof name
, "%s__", sym
->name
);
354 snprintf (name
, sizeof name
, "%s_", sym
->name
);
355 return get_identifier (name
);
358 return get_identifier (sym
->name
);
362 snprintf (name
, sizeof name
, "__%s_MOD_%s", sym
->module
, sym
->name
);
363 return get_identifier (name
);
369 gfc_set_decl_assembler_name (tree decl
, tree name
)
371 tree target_mangled
= targetm
.mangle_decl_assembler_name (decl
, name
);
372 SET_DECL_ASSEMBLER_NAME (decl
, target_mangled
);
376 /* Returns true if a variable of specified size should go on the stack. */
379 gfc_can_put_var_on_stack (tree size
)
381 unsigned HOST_WIDE_INT low
;
383 if (!INTEGER_CST_P (size
))
386 if (gfc_option
.flag_max_stack_var_size
< 0)
389 if (TREE_INT_CST_HIGH (size
) != 0)
392 low
= TREE_INT_CST_LOW (size
);
393 if (low
> (unsigned HOST_WIDE_INT
) gfc_option
.flag_max_stack_var_size
)
396 /* TODO: Set a per-function stack size limit. */
402 /* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
403 an expression involving its corresponding pointer. There are
404 2 cases; one for variable size arrays, and one for everything else,
405 because variable-sized arrays require one fewer level of
409 gfc_finish_cray_pointee (tree decl
, gfc_symbol
*sym
)
411 tree ptr_decl
= gfc_get_symbol_decl (sym
->cp_pointer
);
414 /* Parameters need to be dereferenced. */
415 if (sym
->cp_pointer
->attr
.dummy
)
416 ptr_decl
= build_fold_indirect_ref_loc (input_location
,
419 /* Check to see if we're dealing with a variable-sized array. */
420 if (sym
->attr
.dimension
421 && TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
)
423 /* These decls will be dereferenced later, so we don't dereference
425 value
= convert (TREE_TYPE (decl
), ptr_decl
);
429 ptr_decl
= convert (build_pointer_type (TREE_TYPE (decl
)),
431 value
= build_fold_indirect_ref_loc (input_location
,
435 SET_DECL_VALUE_EXPR (decl
, value
);
436 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
437 GFC_DECL_CRAY_POINTEE (decl
) = 1;
438 /* This is a fake variable just for debugging purposes. */
439 TREE_ASM_WRITTEN (decl
) = 1;
443 /* Finish processing of a declaration without an initial value. */
446 gfc_finish_decl (tree decl
)
448 gcc_assert (TREE_CODE (decl
) == PARM_DECL
449 || DECL_INITIAL (decl
) == NULL_TREE
);
451 if (TREE_CODE (decl
) != VAR_DECL
)
454 if (DECL_SIZE (decl
) == NULL_TREE
455 && TYPE_SIZE (TREE_TYPE (decl
)) != NULL_TREE
)
456 layout_decl (decl
, 0);
458 /* A few consistency checks. */
459 /* A static variable with an incomplete type is an error if it is
460 initialized. Also if it is not file scope. Otherwise, let it
461 through, but if it is not `extern' then it may cause an error
463 /* An automatic variable with an incomplete type is an error. */
465 /* We should know the storage size. */
466 gcc_assert (DECL_SIZE (decl
) != NULL_TREE
467 || (TREE_STATIC (decl
)
468 ? (!DECL_INITIAL (decl
) || !DECL_CONTEXT (decl
))
469 : DECL_EXTERNAL (decl
)));
471 /* The storage size should be constant. */
472 gcc_assert ((!DECL_EXTERNAL (decl
) && !TREE_STATIC (decl
))
474 || TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
);
478 /* Apply symbol attributes to a variable, and add it to the function scope. */
481 gfc_finish_var_decl (tree decl
, gfc_symbol
* sym
)
484 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
485 This is the equivalent of the TARGET variables.
486 We also need to set this if the variable is passed by reference in a
489 /* Set DECL_VALUE_EXPR for Cray Pointees. */
490 if (sym
->attr
.cray_pointee
)
491 gfc_finish_cray_pointee (decl
, sym
);
493 if (sym
->attr
.target
)
494 TREE_ADDRESSABLE (decl
) = 1;
495 /* If it wasn't used we wouldn't be getting it. */
496 TREE_USED (decl
) = 1;
498 /* Chain this decl to the pending declarations. Don't do pushdecl()
499 because this would add them to the current scope rather than the
501 if (current_function_decl
!= NULL_TREE
)
503 if (sym
->ns
->proc_name
->backend_decl
== current_function_decl
504 || sym
->result
== sym
)
505 gfc_add_decl_to_function (decl
);
506 else if (sym
->ns
->proc_name
->attr
.flavor
== FL_LABEL
)
507 /* This is a BLOCK construct. */
508 add_decl_as_local (decl
);
510 gfc_add_decl_to_parent_function (decl
);
513 if (sym
->attr
.cray_pointee
)
516 if(sym
->attr
.is_bind_c
== 1)
518 /* We need to put variables that are bind(c) into the common
519 segment of the object file, because this is what C would do.
520 gfortran would typically put them in either the BSS or
521 initialized data segments, and only mark them as common if
522 they were part of common blocks. However, if they are not put
523 into common space, then C cannot initialize global Fortran
524 variables that it interoperates with and the draft says that
525 either Fortran or C should be able to initialize it (but not
526 both, of course.) (J3/04-007, section 15.3). */
527 TREE_PUBLIC(decl
) = 1;
528 DECL_COMMON(decl
) = 1;
531 /* If a variable is USE associated, it's always external. */
532 if (sym
->attr
.use_assoc
)
534 DECL_EXTERNAL (decl
) = 1;
535 TREE_PUBLIC (decl
) = 1;
537 else if (sym
->module
&& !sym
->attr
.result
&& !sym
->attr
.dummy
)
539 /* TODO: Don't set sym->module for result or dummy variables. */
540 gcc_assert (current_function_decl
== NULL_TREE
|| sym
->result
== sym
);
541 /* This is the declaration of a module variable. */
542 TREE_PUBLIC (decl
) = 1;
543 TREE_STATIC (decl
) = 1;
546 /* Derived types are a bit peculiar because of the possibility of
547 a default initializer; this must be applied each time the variable
548 comes into scope it therefore need not be static. These variables
549 are SAVE_NONE but have an initializer. Otherwise explicitly
550 initialized variables are SAVE_IMPLICIT and explicitly saved are
552 if (!sym
->attr
.use_assoc
553 && (sym
->attr
.save
!= SAVE_NONE
|| sym
->attr
.data
554 || (sym
->value
&& sym
->ns
->proc_name
->attr
.is_main_program
)))
555 TREE_STATIC (decl
) = 1;
557 if (sym
->attr
.volatile_
)
559 TREE_THIS_VOLATILE (decl
) = 1;
560 new_type
= build_qualified_type (TREE_TYPE (decl
), TYPE_QUAL_VOLATILE
);
561 TREE_TYPE (decl
) = new_type
;
564 /* Keep variables larger than max-stack-var-size off stack. */
565 if (!sym
->ns
->proc_name
->attr
.recursive
566 && INTEGER_CST_P (DECL_SIZE_UNIT (decl
))
567 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl
))
568 /* Put variable length auto array pointers always into stack. */
569 && (TREE_CODE (TREE_TYPE (decl
)) != POINTER_TYPE
570 || sym
->attr
.dimension
== 0
571 || sym
->as
->type
!= AS_EXPLICIT
573 || sym
->attr
.allocatable
)
574 && !DECL_ARTIFICIAL (decl
))
575 TREE_STATIC (decl
) = 1;
577 /* Handle threadprivate variables. */
578 if (sym
->attr
.threadprivate
579 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
580 DECL_TLS_MODEL (decl
) = decl_default_tls_model (decl
);
582 if (!sym
->attr
.target
583 && !sym
->attr
.pointer
584 && !sym
->attr
.cray_pointee
585 && !sym
->attr
.proc_pointer
)
586 DECL_RESTRICTED_P (decl
) = 1;
590 /* Allocate the lang-specific part of a decl. */
593 gfc_allocate_lang_decl (tree decl
)
595 DECL_LANG_SPECIFIC (decl
) = ggc_alloc_cleared_lang_decl(sizeof
599 /* Remember a symbol to generate initialization/cleanup code at function
603 gfc_defer_symbol_init (gfc_symbol
* sym
)
609 /* Don't add a symbol twice. */
613 last
= head
= sym
->ns
->proc_name
;
616 /* Make sure that setup code for dummy variables which are used in the
617 setup of other variables is generated first. */
620 /* Find the first dummy arg seen after us, or the first non-dummy arg.
621 This is a circular list, so don't go past the head. */
623 && (!p
->attr
.dummy
|| p
->dummy_order
> sym
->dummy_order
))
629 /* Insert in between last and p. */
635 /* Create an array index type variable with function scope. */
638 create_index_var (const char * pfx
, int nest
)
642 decl
= gfc_create_var_np (gfc_array_index_type
, pfx
);
644 gfc_add_decl_to_parent_function (decl
);
646 gfc_add_decl_to_function (decl
);
651 /* Create variables to hold all the non-constant bits of info for a
652 descriptorless array. Remember these in the lang-specific part of the
656 gfc_build_qualified_array (tree decl
, gfc_symbol
* sym
)
662 type
= TREE_TYPE (decl
);
664 /* We just use the descriptor, if there is one. */
665 if (GFC_DESCRIPTOR_TYPE_P (type
))
668 gcc_assert (GFC_ARRAY_TYPE_P (type
));
669 nest
= (sym
->ns
->proc_name
->backend_decl
!= current_function_decl
)
670 && !sym
->attr
.contained
;
672 for (dim
= 0; dim
< GFC_TYPE_ARRAY_RANK (type
); dim
++)
674 if (GFC_TYPE_ARRAY_LBOUND (type
, dim
) == NULL_TREE
)
676 GFC_TYPE_ARRAY_LBOUND (type
, dim
) = create_index_var ("lbound", nest
);
677 TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type
, dim
)) = 1;
679 /* Don't try to use the unknown bound for assumed shape arrays. */
680 if (GFC_TYPE_ARRAY_UBOUND (type
, dim
) == NULL_TREE
681 && (sym
->as
->type
!= AS_ASSUMED_SIZE
682 || dim
< GFC_TYPE_ARRAY_RANK (type
) - 1))
684 GFC_TYPE_ARRAY_UBOUND (type
, dim
) = create_index_var ("ubound", nest
);
685 TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type
, dim
)) = 1;
688 if (GFC_TYPE_ARRAY_STRIDE (type
, dim
) == NULL_TREE
)
690 GFC_TYPE_ARRAY_STRIDE (type
, dim
) = create_index_var ("stride", nest
);
691 TREE_NO_WARNING (GFC_TYPE_ARRAY_STRIDE (type
, dim
)) = 1;
694 if (GFC_TYPE_ARRAY_OFFSET (type
) == NULL_TREE
)
696 GFC_TYPE_ARRAY_OFFSET (type
) = gfc_create_var_np (gfc_array_index_type
,
698 TREE_NO_WARNING (GFC_TYPE_ARRAY_OFFSET (type
)) = 1;
701 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type
));
703 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type
));
706 if (GFC_TYPE_ARRAY_SIZE (type
) == NULL_TREE
707 && sym
->as
->type
!= AS_ASSUMED_SIZE
)
709 GFC_TYPE_ARRAY_SIZE (type
) = create_index_var ("size", nest
);
710 TREE_NO_WARNING (GFC_TYPE_ARRAY_SIZE (type
)) = 1;
713 if (POINTER_TYPE_P (type
))
715 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type
)));
716 gcc_assert (TYPE_LANG_SPECIFIC (type
)
717 == TYPE_LANG_SPECIFIC (TREE_TYPE (type
)));
718 type
= TREE_TYPE (type
);
721 if (! COMPLETE_TYPE_P (type
) && GFC_TYPE_ARRAY_SIZE (type
))
725 size
= fold_build2 (MINUS_EXPR
, gfc_array_index_type
,
726 GFC_TYPE_ARRAY_SIZE (type
), gfc_index_one_node
);
727 range
= build_range_type (gfc_array_index_type
, gfc_index_zero_node
,
729 TYPE_DOMAIN (type
) = range
;
733 if (TYPE_NAME (type
) != NULL_TREE
734 && GFC_TYPE_ARRAY_UBOUND (type
, sym
->as
->rank
- 1) != NULL_TREE
735 && TREE_CODE (GFC_TYPE_ARRAY_UBOUND (type
, sym
->as
->rank
- 1)) == VAR_DECL
)
737 tree gtype
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
739 for (dim
= 0; dim
< sym
->as
->rank
- 1; dim
++)
741 gcc_assert (TREE_CODE (gtype
) == ARRAY_TYPE
);
742 gtype
= TREE_TYPE (gtype
);
744 gcc_assert (TREE_CODE (gtype
) == ARRAY_TYPE
);
745 if (TYPE_MAX_VALUE (TYPE_DOMAIN (gtype
)) == NULL
)
746 TYPE_NAME (type
) = NULL_TREE
;
749 if (TYPE_NAME (type
) == NULL_TREE
)
751 tree gtype
= TREE_TYPE (type
), rtype
, type_decl
;
753 for (dim
= sym
->as
->rank
- 1; dim
>= 0; dim
--)
756 lbound
= GFC_TYPE_ARRAY_LBOUND (type
, dim
);
757 ubound
= GFC_TYPE_ARRAY_UBOUND (type
, dim
);
758 rtype
= build_range_type (gfc_array_index_type
, lbound
, ubound
);
759 gtype
= build_array_type (gtype
, rtype
);
760 /* Ensure the bound variables aren't optimized out at -O0.
761 For -O1 and above they often will be optimized out, but
762 can be tracked by VTA. Also set DECL_NAMELESS, so that
763 the artificial lbound.N or ubound.N DECL_NAME doesn't
764 end up in debug info. */
765 if (lbound
&& TREE_CODE (lbound
) == VAR_DECL
766 && DECL_ARTIFICIAL (lbound
) && DECL_IGNORED_P (lbound
))
768 if (DECL_NAME (lbound
)
769 && strstr (IDENTIFIER_POINTER (DECL_NAME (lbound
)),
771 DECL_NAMELESS (lbound
) = 1;
772 DECL_IGNORED_P (lbound
) = 0;
774 if (ubound
&& TREE_CODE (ubound
) == VAR_DECL
775 && DECL_ARTIFICIAL (ubound
) && DECL_IGNORED_P (ubound
))
777 if (DECL_NAME (ubound
)
778 && strstr (IDENTIFIER_POINTER (DECL_NAME (ubound
)),
780 DECL_NAMELESS (ubound
) = 1;
781 DECL_IGNORED_P (ubound
) = 0;
784 TYPE_NAME (type
) = type_decl
= build_decl (input_location
,
785 TYPE_DECL
, NULL
, gtype
);
786 DECL_ORIGINAL_TYPE (type_decl
) = gtype
;
791 /* For some dummy arguments we don't use the actual argument directly.
792 Instead we create a local decl and use that. This allows us to perform
793 initialization, and construct full type information. */
796 gfc_build_dummy_array_decl (gfc_symbol
* sym
, tree dummy
)
806 if (sym
->attr
.pointer
|| sym
->attr
.allocatable
)
809 /* Add to list of variables if not a fake result variable. */
810 if (sym
->attr
.result
|| sym
->attr
.dummy
)
811 gfc_defer_symbol_init (sym
);
813 type
= TREE_TYPE (dummy
);
814 gcc_assert (TREE_CODE (dummy
) == PARM_DECL
815 && POINTER_TYPE_P (type
));
817 /* Do we know the element size? */
818 known_size
= sym
->ts
.type
!= BT_CHARACTER
819 || INTEGER_CST_P (sym
->ts
.u
.cl
->backend_decl
);
821 if (known_size
&& !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type
)))
823 /* For descriptorless arrays with known element size the actual
824 argument is sufficient. */
825 gcc_assert (GFC_ARRAY_TYPE_P (type
));
826 gfc_build_qualified_array (dummy
, sym
);
830 type
= TREE_TYPE (type
);
831 if (GFC_DESCRIPTOR_TYPE_P (type
))
833 /* Create a descriptorless array pointer. */
837 /* Even when -frepack-arrays is used, symbols with TARGET attribute
839 if (!gfc_option
.flag_repack_arrays
|| sym
->attr
.target
)
841 if (as
->type
== AS_ASSUMED_SIZE
)
842 packed
= PACKED_FULL
;
846 if (as
->type
== AS_EXPLICIT
)
848 packed
= PACKED_FULL
;
849 for (n
= 0; n
< as
->rank
; n
++)
853 && as
->upper
[n
]->expr_type
== EXPR_CONSTANT
854 && as
->lower
[n
]->expr_type
== EXPR_CONSTANT
))
855 packed
= PACKED_PARTIAL
;
859 packed
= PACKED_PARTIAL
;
862 type
= gfc_typenode_for_spec (&sym
->ts
);
863 type
= gfc_get_nodesc_array_type (type
, sym
->as
, packed
,
868 /* We now have an expression for the element size, so create a fully
869 qualified type. Reset sym->backend decl or this will just return the
871 DECL_ARTIFICIAL (sym
->backend_decl
) = 1;
872 sym
->backend_decl
= NULL_TREE
;
873 type
= gfc_sym_type (sym
);
874 packed
= PACKED_FULL
;
877 ASM_FORMAT_PRIVATE_NAME (name
, IDENTIFIER_POINTER (DECL_NAME (dummy
)), 0);
878 decl
= build_decl (input_location
,
879 VAR_DECL
, get_identifier (name
), type
);
881 DECL_ARTIFICIAL (decl
) = 1;
882 DECL_NAMELESS (decl
) = 1;
883 TREE_PUBLIC (decl
) = 0;
884 TREE_STATIC (decl
) = 0;
885 DECL_EXTERNAL (decl
) = 0;
887 /* We should never get deferred shape arrays here. We used to because of
889 gcc_assert (sym
->as
->type
!= AS_DEFERRED
);
891 if (packed
== PACKED_PARTIAL
)
892 GFC_DECL_PARTIAL_PACKED_ARRAY (decl
) = 1;
893 else if (packed
== PACKED_FULL
)
894 GFC_DECL_PACKED_ARRAY (decl
) = 1;
896 gfc_build_qualified_array (decl
, sym
);
898 if (DECL_LANG_SPECIFIC (dummy
))
899 DECL_LANG_SPECIFIC (decl
) = DECL_LANG_SPECIFIC (dummy
);
901 gfc_allocate_lang_decl (decl
);
903 GFC_DECL_SAVED_DESCRIPTOR (decl
) = dummy
;
905 if (sym
->ns
->proc_name
->backend_decl
== current_function_decl
906 || sym
->attr
.contained
)
907 gfc_add_decl_to_function (decl
);
909 gfc_add_decl_to_parent_function (decl
);
914 /* For symbol SYM with GFC_DECL_SAVED_DESCRIPTOR used in contained
915 function add a VAR_DECL to the current function with DECL_VALUE_EXPR
916 pointing to the artificial variable for debug info purposes. */
919 gfc_nonlocal_dummy_array_decl (gfc_symbol
*sym
)
923 if (! nonlocal_dummy_decl_pset
)
924 nonlocal_dummy_decl_pset
= pointer_set_create ();
926 if (pointer_set_insert (nonlocal_dummy_decl_pset
, sym
->backend_decl
))
929 dummy
= GFC_DECL_SAVED_DESCRIPTOR (sym
->backend_decl
);
930 decl
= build_decl (input_location
, VAR_DECL
, DECL_NAME (dummy
),
931 TREE_TYPE (sym
->backend_decl
));
932 DECL_ARTIFICIAL (decl
) = 0;
933 TREE_USED (decl
) = 1;
934 TREE_PUBLIC (decl
) = 0;
935 TREE_STATIC (decl
) = 0;
936 DECL_EXTERNAL (decl
) = 0;
937 if (DECL_BY_REFERENCE (dummy
))
938 DECL_BY_REFERENCE (decl
) = 1;
939 DECL_LANG_SPECIFIC (decl
) = DECL_LANG_SPECIFIC (sym
->backend_decl
);
940 SET_DECL_VALUE_EXPR (decl
, sym
->backend_decl
);
941 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
942 DECL_CONTEXT (decl
) = DECL_CONTEXT (sym
->backend_decl
);
943 DECL_CHAIN (decl
) = nonlocal_dummy_decls
;
944 nonlocal_dummy_decls
= decl
;
947 /* Return a constant or a variable to use as a string length. Does not
948 add the decl to the current scope. */
951 gfc_create_string_length (gfc_symbol
* sym
)
953 gcc_assert (sym
->ts
.u
.cl
);
954 gfc_conv_const_charlen (sym
->ts
.u
.cl
);
956 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
959 char name
[GFC_MAX_MANGLED_SYMBOL_LEN
+ 2];
961 /* Also prefix the mangled name. */
962 strcpy (&name
[1], sym
->name
);
964 length
= build_decl (input_location
,
965 VAR_DECL
, get_identifier (name
),
966 gfc_charlen_type_node
);
967 DECL_ARTIFICIAL (length
) = 1;
968 TREE_USED (length
) = 1;
969 if (sym
->ns
->proc_name
->tlink
!= NULL
)
970 gfc_defer_symbol_init (sym
);
972 sym
->ts
.u
.cl
->backend_decl
= length
;
975 gcc_assert (sym
->ts
.u
.cl
->backend_decl
!= NULL_TREE
);
976 return sym
->ts
.u
.cl
->backend_decl
;
979 /* If a variable is assigned a label, we add another two auxiliary
983 gfc_add_assign_aux_vars (gfc_symbol
* sym
)
989 gcc_assert (sym
->backend_decl
);
991 decl
= sym
->backend_decl
;
992 gfc_allocate_lang_decl (decl
);
993 GFC_DECL_ASSIGN (decl
) = 1;
994 length
= build_decl (input_location
,
995 VAR_DECL
, create_tmp_var_name (sym
->name
),
996 gfc_charlen_type_node
);
997 addr
= build_decl (input_location
,
998 VAR_DECL
, create_tmp_var_name (sym
->name
),
1000 gfc_finish_var_decl (length
, sym
);
1001 gfc_finish_var_decl (addr
, sym
);
1002 /* STRING_LENGTH is also used as flag. Less than -1 means that
1003 ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the
1004 target label's address. Otherwise, value is the length of a format string
1005 and ASSIGN_ADDR is its address. */
1006 if (TREE_STATIC (length
))
1007 DECL_INITIAL (length
) = build_int_cst (NULL_TREE
, -2);
1009 gfc_defer_symbol_init (sym
);
1011 GFC_DECL_STRING_LEN (decl
) = length
;
1012 GFC_DECL_ASSIGN_ADDR (decl
) = addr
;
1017 add_attributes_to_decl (symbol_attribute sym_attr
, tree list
)
1022 for (id
= 0; id
< EXT_ATTR_NUM
; id
++)
1023 if (sym_attr
.ext_attr
& (1 << id
))
1025 attr
= build_tree_list (
1026 get_identifier (ext_attr_list
[id
].middle_end_name
),
1028 list
= chainon (list
, attr
);
1035 /* Return the decl for a gfc_symbol, create it if it doesn't already
1039 gfc_get_symbol_decl (gfc_symbol
* sym
)
1042 tree length
= NULL_TREE
;
1046 gcc_assert (sym
->attr
.referenced
1047 || sym
->attr
.use_assoc
1048 || sym
->ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
);
1050 if (sym
->ns
&& sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.function
)
1051 byref
= gfc_return_by_reference (sym
->ns
->proc_name
);
1055 /* Make sure that the vtab for the declared type is completed. */
1056 if (sym
->ts
.type
== BT_CLASS
)
1058 gfc_component
*c
= CLASS_DATA (sym
);
1059 if (!c
->ts
.u
.derived
->backend_decl
)
1060 gfc_find_derived_vtab (c
->ts
.u
.derived
);
1063 if ((sym
->attr
.dummy
&& ! sym
->attr
.function
) || (sym
->attr
.result
&& byref
))
1065 /* Return via extra parameter. */
1066 if (sym
->attr
.result
&& byref
1067 && !sym
->backend_decl
)
1070 DECL_ARGUMENTS (sym
->ns
->proc_name
->backend_decl
);
1071 /* For entry master function skip over the __entry
1073 if (sym
->ns
->proc_name
->attr
.entry_master
)
1074 sym
->backend_decl
= DECL_CHAIN (sym
->backend_decl
);
1077 /* Dummy variables should already have been created. */
1078 gcc_assert (sym
->backend_decl
);
1080 /* Create a character length variable. */
1081 if (sym
->ts
.type
== BT_CHARACTER
)
1083 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
1084 length
= gfc_create_string_length (sym
);
1086 length
= sym
->ts
.u
.cl
->backend_decl
;
1087 if (TREE_CODE (length
) == VAR_DECL
1088 && DECL_CONTEXT (length
) == NULL_TREE
)
1090 /* Add the string length to the same context as the symbol. */
1091 if (DECL_CONTEXT (sym
->backend_decl
) == current_function_decl
)
1092 gfc_add_decl_to_function (length
);
1094 gfc_add_decl_to_parent_function (length
);
1096 gcc_assert (DECL_CONTEXT (sym
->backend_decl
) ==
1097 DECL_CONTEXT (length
));
1099 gfc_defer_symbol_init (sym
);
1103 /* Use a copy of the descriptor for dummy arrays. */
1104 if (sym
->attr
.dimension
&& !TREE_USED (sym
->backend_decl
))
1106 decl
= gfc_build_dummy_array_decl (sym
, sym
->backend_decl
);
1107 /* Prevent the dummy from being detected as unused if it is copied. */
1108 if (sym
->backend_decl
!= NULL
&& decl
!= sym
->backend_decl
)
1109 DECL_ARTIFICIAL (sym
->backend_decl
) = 1;
1110 sym
->backend_decl
= decl
;
1113 TREE_USED (sym
->backend_decl
) = 1;
1114 if (sym
->attr
.assign
&& GFC_DECL_ASSIGN (sym
->backend_decl
) == 0)
1116 gfc_add_assign_aux_vars (sym
);
1119 if (sym
->attr
.dimension
1120 && DECL_LANG_SPECIFIC (sym
->backend_decl
)
1121 && GFC_DECL_SAVED_DESCRIPTOR (sym
->backend_decl
)
1122 && DECL_CONTEXT (sym
->backend_decl
) != current_function_decl
)
1123 gfc_nonlocal_dummy_array_decl (sym
);
1125 return sym
->backend_decl
;
1128 if (sym
->backend_decl
)
1129 return sym
->backend_decl
;
1131 /* If use associated and whole file compilation, use the module
1133 if (gfc_option
.flag_whole_file
1134 && sym
->attr
.flavor
== FL_VARIABLE
1135 && sym
->attr
.use_assoc
1140 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->module
);
1141 if (gsym
&& gsym
->ns
&& gsym
->type
== GSYM_MODULE
)
1145 gfc_find_symbol (sym
->name
, gsym
->ns
, 0, &s
);
1146 if (s
&& s
->backend_decl
)
1148 if (sym
->ts
.type
== BT_DERIVED
)
1149 gfc_copy_dt_decls_ifequal (s
->ts
.u
.derived
, sym
->ts
.u
.derived
,
1151 if (sym
->ts
.type
== BT_CHARACTER
)
1152 sym
->ts
.u
.cl
->backend_decl
= s
->ts
.u
.cl
->backend_decl
;
1153 sym
->backend_decl
= s
->backend_decl
;
1154 return sym
->backend_decl
;
1159 /* Catch function declarations. Only used for actual parameters and
1160 procedure pointers. */
1161 if (sym
->attr
.flavor
== FL_PROCEDURE
)
1163 decl
= gfc_get_extern_function_decl (sym
);
1164 gfc_set_decl_location (decl
, &sym
->declared_at
);
1168 if (sym
->attr
.intrinsic
)
1169 internal_error ("intrinsic variable which isn't a procedure");
1171 /* Create string length decl first so that they can be used in the
1172 type declaration. */
1173 if (sym
->ts
.type
== BT_CHARACTER
)
1174 length
= gfc_create_string_length (sym
);
1176 /* Create the decl for the variable. */
1177 decl
= build_decl (sym
->declared_at
.lb
->location
,
1178 VAR_DECL
, gfc_sym_identifier (sym
), gfc_sym_type (sym
));
1180 /* Add attributes to variables. Functions are handled elsewhere. */
1181 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
1182 decl_attributes (&decl
, attributes
, 0);
1184 /* Symbols from modules should have their assembler names mangled.
1185 This is done here rather than in gfc_finish_var_decl because it
1186 is different for string length variables. */
1189 gfc_set_decl_assembler_name (decl
, gfc_sym_mangled_identifier (sym
));
1190 if (sym
->attr
.use_assoc
)
1191 DECL_IGNORED_P (decl
) = 1;
1194 if (sym
->attr
.dimension
)
1196 /* Create variables to hold the non-constant bits of array info. */
1197 gfc_build_qualified_array (decl
, sym
);
1199 if (sym
->attr
.contiguous
1200 || ((sym
->attr
.allocatable
|| !sym
->attr
.dummy
) && !sym
->attr
.pointer
))
1201 GFC_DECL_PACKED_ARRAY (decl
) = 1;
1204 /* Remember this variable for allocation/cleanup. */
1205 if (sym
->attr
.dimension
|| sym
->attr
.allocatable
1206 || (sym
->ts
.type
== BT_CLASS
&&
1207 (CLASS_DATA (sym
)->attr
.dimension
1208 || CLASS_DATA (sym
)->attr
.allocatable
))
1209 || (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.alloc_comp
)
1210 /* This applies a derived type default initializer. */
1211 || (sym
->ts
.type
== BT_DERIVED
1212 && sym
->attr
.save
== SAVE_NONE
1214 && !sym
->attr
.allocatable
1215 && (sym
->value
&& !sym
->ns
->proc_name
->attr
.is_main_program
)
1216 && !sym
->attr
.use_assoc
))
1217 gfc_defer_symbol_init (sym
);
1219 gfc_finish_var_decl (decl
, sym
);
1221 if (sym
->ts
.type
== BT_CHARACTER
)
1223 /* Character variables need special handling. */
1224 gfc_allocate_lang_decl (decl
);
1226 if (TREE_CODE (length
) != INTEGER_CST
)
1228 char name
[GFC_MAX_MANGLED_SYMBOL_LEN
+ 2];
1232 /* Also prefix the mangled name for symbols from modules. */
1233 strcpy (&name
[1], sym
->name
);
1236 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (length
)));
1237 gfc_set_decl_assembler_name (decl
, get_identifier (name
));
1239 gfc_finish_var_decl (length
, sym
);
1240 gcc_assert (!sym
->value
);
1243 else if (sym
->attr
.subref_array_pointer
)
1245 /* We need the span for these beasts. */
1246 gfc_allocate_lang_decl (decl
);
1249 if (sym
->attr
.subref_array_pointer
)
1252 GFC_DECL_SUBREF_ARRAY_P (decl
) = 1;
1253 span
= build_decl (input_location
,
1254 VAR_DECL
, create_tmp_var_name ("span"),
1255 gfc_array_index_type
);
1256 gfc_finish_var_decl (span
, sym
);
1257 TREE_STATIC (span
) = TREE_STATIC (decl
);
1258 DECL_ARTIFICIAL (span
) = 1;
1259 DECL_INITIAL (span
) = build_int_cst (gfc_array_index_type
, 0);
1261 GFC_DECL_SPAN (decl
) = span
;
1262 GFC_TYPE_ARRAY_SPAN (TREE_TYPE (decl
)) = span
;
1265 sym
->backend_decl
= decl
;
1267 if (sym
->attr
.assign
)
1268 gfc_add_assign_aux_vars (sym
);
1270 if (TREE_STATIC (decl
) && !sym
->attr
.use_assoc
1271 && (sym
->attr
.save
|| sym
->ns
->proc_name
->attr
.is_main_program
1272 || gfc_option
.flag_max_stack_var_size
== 0
1273 || sym
->attr
.data
|| sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
))
1275 /* Add static initializer. For procedures, it is only needed if
1276 SAVE is specified otherwise they need to be reinitialized
1277 every time the procedure is entered. The TREE_STATIC is
1278 in this case due to -fmax-stack-var-size=. */
1279 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
1280 TREE_TYPE (decl
), sym
->attr
.dimension
,
1281 sym
->attr
.pointer
|| sym
->attr
.allocatable
);
1284 if (!TREE_STATIC (decl
)
1285 && POINTER_TYPE_P (TREE_TYPE (decl
))
1286 && !sym
->attr
.pointer
1287 && !sym
->attr
.allocatable
1288 && !sym
->attr
.proc_pointer
)
1289 DECL_BY_REFERENCE (decl
) = 1;
1295 /* Substitute a temporary variable in place of the real one. */
1298 gfc_shadow_sym (gfc_symbol
* sym
, tree decl
, gfc_saved_var
* save
)
1300 save
->attr
= sym
->attr
;
1301 save
->decl
= sym
->backend_decl
;
1303 gfc_clear_attr (&sym
->attr
);
1304 sym
->attr
.referenced
= 1;
1305 sym
->attr
.flavor
= FL_VARIABLE
;
1307 sym
->backend_decl
= decl
;
1311 /* Restore the original variable. */
1314 gfc_restore_sym (gfc_symbol
* sym
, gfc_saved_var
* save
)
1316 sym
->attr
= save
->attr
;
1317 sym
->backend_decl
= save
->decl
;
1321 /* Declare a procedure pointer. */
1324 get_proc_pointer_decl (gfc_symbol
*sym
)
1329 decl
= sym
->backend_decl
;
1333 decl
= build_decl (input_location
,
1334 VAR_DECL
, get_identifier (sym
->name
),
1335 build_pointer_type (gfc_get_function_type (sym
)));
1337 if ((sym
->ns
->proc_name
1338 && sym
->ns
->proc_name
->backend_decl
== current_function_decl
)
1339 || sym
->attr
.contained
)
1340 gfc_add_decl_to_function (decl
);
1341 else if (sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
1342 gfc_add_decl_to_parent_function (decl
);
1344 sym
->backend_decl
= decl
;
1346 /* If a variable is USE associated, it's always external. */
1347 if (sym
->attr
.use_assoc
)
1349 DECL_EXTERNAL (decl
) = 1;
1350 TREE_PUBLIC (decl
) = 1;
1352 else if (sym
->module
&& sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
1354 /* This is the declaration of a module variable. */
1355 TREE_PUBLIC (decl
) = 1;
1356 TREE_STATIC (decl
) = 1;
1359 if (!sym
->attr
.use_assoc
1360 && (sym
->attr
.save
!= SAVE_NONE
|| sym
->attr
.data
1361 || (sym
->value
&& sym
->ns
->proc_name
->attr
.is_main_program
)))
1362 TREE_STATIC (decl
) = 1;
1364 if (TREE_STATIC (decl
) && sym
->value
)
1366 /* Add static initializer. */
1367 DECL_INITIAL (decl
) = gfc_conv_initializer (sym
->value
, &sym
->ts
,
1369 sym
->attr
.proc_pointer
? false : sym
->attr
.dimension
,
1370 sym
->attr
.proc_pointer
);
1373 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
1374 decl_attributes (&decl
, attributes
, 0);
1380 /* Get a basic decl for an external function. */
1383 gfc_get_extern_function_decl (gfc_symbol
* sym
)
1389 gfc_intrinsic_sym
*isym
;
1391 char s
[GFC_MAX_SYMBOL_LEN
+ 23]; /* "_gfortran_f2c_specific" and '\0'. */
1396 if (sym
->backend_decl
)
1397 return sym
->backend_decl
;
1399 /* We should never be creating external decls for alternate entry points.
1400 The procedure may be an alternate entry point, but we don't want/need
1402 gcc_assert (!(sym
->attr
.entry
|| sym
->attr
.entry_master
));
1404 if (sym
->attr
.proc_pointer
)
1405 return get_proc_pointer_decl (sym
);
1407 /* See if this is an external procedure from the same file. If so,
1408 return the backend_decl. */
1409 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->name
);
1411 if (gfc_option
.flag_whole_file
1412 && !sym
->attr
.use_assoc
1413 && !sym
->backend_decl
1415 && ((gsym
->type
== GSYM_SUBROUTINE
) || (gsym
->type
== GSYM_FUNCTION
))
1416 && (gsym
->ns
->proc_name
->backend_decl
|| !sym
->attr
.intrinsic
))
1418 if (!gsym
->ns
->proc_name
->backend_decl
)
1420 /* By construction, the external function cannot be
1421 a contained procedure. */
1423 tree save_fn_decl
= current_function_decl
;
1425 current_function_decl
= NULL_TREE
;
1426 gfc_get_backend_locus (&old_loc
);
1429 gfc_create_function_decl (gsym
->ns
, true);
1432 gfc_set_backend_locus (&old_loc
);
1433 current_function_decl
= save_fn_decl
;
1436 /* If the namespace has entries, the proc_name is the
1437 entry master. Find the entry and use its backend_decl.
1438 otherwise, use the proc_name backend_decl. */
1439 if (gsym
->ns
->entries
)
1441 gfc_entry_list
*entry
= gsym
->ns
->entries
;
1443 for (; entry
; entry
= entry
->next
)
1445 if (strcmp (gsym
->name
, entry
->sym
->name
) == 0)
1447 sym
->backend_decl
= entry
->sym
->backend_decl
;
1454 sym
->backend_decl
= gsym
->ns
->proc_name
->backend_decl
;
1457 if (sym
->backend_decl
)
1458 return sym
->backend_decl
;
1461 /* See if this is a module procedure from the same file. If so,
1462 return the backend_decl. */
1464 gsym
= gfc_find_gsymbol (gfc_gsym_root
, sym
->module
);
1466 if (gfc_option
.flag_whole_file
1468 && gsym
->type
== GSYM_MODULE
)
1473 gfc_find_symbol (sym
->name
, gsym
->ns
, 0, &s
);
1474 if (s
&& s
->backend_decl
)
1476 sym
->backend_decl
= s
->backend_decl
;
1477 return sym
->backend_decl
;
1481 if (sym
->attr
.intrinsic
)
1483 /* Call the resolution function to get the actual name. This is
1484 a nasty hack which relies on the resolution functions only looking
1485 at the first argument. We pass NULL for the second argument
1486 otherwise things like AINT get confused. */
1487 isym
= gfc_find_function (sym
->name
);
1488 gcc_assert (isym
->resolve
.f0
!= NULL
);
1490 memset (&e
, 0, sizeof (e
));
1491 e
.expr_type
= EXPR_FUNCTION
;
1493 memset (&argexpr
, 0, sizeof (argexpr
));
1494 gcc_assert (isym
->formal
);
1495 argexpr
.ts
= isym
->formal
->ts
;
1497 if (isym
->formal
->next
== NULL
)
1498 isym
->resolve
.f1 (&e
, &argexpr
);
1501 if (isym
->formal
->next
->next
== NULL
)
1502 isym
->resolve
.f2 (&e
, &argexpr
, NULL
);
1505 if (isym
->formal
->next
->next
->next
== NULL
)
1506 isym
->resolve
.f3 (&e
, &argexpr
, NULL
, NULL
);
1509 /* All specific intrinsics take less than 5 arguments. */
1510 gcc_assert (isym
->formal
->next
->next
->next
->next
== NULL
);
1511 isym
->resolve
.f4 (&e
, &argexpr
, NULL
, NULL
, NULL
);
1516 if (gfc_option
.flag_f2c
1517 && ((e
.ts
.type
== BT_REAL
&& e
.ts
.kind
== gfc_default_real_kind
)
1518 || e
.ts
.type
== BT_COMPLEX
))
1520 /* Specific which needs a different implementation if f2c
1521 calling conventions are used. */
1522 sprintf (s
, "_gfortran_f2c_specific%s", e
.value
.function
.name
);
1525 sprintf (s
, "_gfortran_specific%s", e
.value
.function
.name
);
1527 name
= get_identifier (s
);
1528 mangled_name
= name
;
1532 name
= gfc_sym_identifier (sym
);
1533 mangled_name
= gfc_sym_mangled_function_id (sym
);
1536 type
= gfc_get_function_type (sym
);
1537 fndecl
= build_decl (input_location
,
1538 FUNCTION_DECL
, name
, type
);
1540 attributes
= add_attributes_to_decl (sym
->attr
, NULL_TREE
);
1541 decl_attributes (&fndecl
, attributes
, 0);
1543 gfc_set_decl_assembler_name (fndecl
, mangled_name
);
1545 /* Set the context of this decl. */
1546 if (0 && sym
->ns
&& sym
->ns
->proc_name
)
1548 /* TODO: Add external decls to the appropriate scope. */
1549 DECL_CONTEXT (fndecl
) = sym
->ns
->proc_name
->backend_decl
;
1553 /* Global declaration, e.g. intrinsic subroutine. */
1554 DECL_CONTEXT (fndecl
) = NULL_TREE
;
1557 DECL_EXTERNAL (fndecl
) = 1;
1559 /* This specifies if a function is globally addressable, i.e. it is
1560 the opposite of declaring static in C. */
1561 TREE_PUBLIC (fndecl
) = 1;
1563 /* Set attributes for PURE functions. A call to PURE function in the
1564 Fortran 95 sense is both pure and without side effects in the C
1566 if (sym
->attr
.pure
|| sym
->attr
.elemental
)
1568 if (sym
->attr
.function
&& !gfc_return_by_reference (sym
))
1569 DECL_PURE_P (fndecl
) = 1;
1570 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
1571 parameters and don't use alternate returns (is this
1572 allowed?). In that case, calls to them are meaningless, and
1573 can be optimized away. See also in build_function_decl(). */
1574 TREE_SIDE_EFFECTS (fndecl
) = 0;
1577 /* Mark non-returning functions. */
1578 if (sym
->attr
.noreturn
)
1579 TREE_THIS_VOLATILE(fndecl
) = 1;
1581 sym
->backend_decl
= fndecl
;
1583 if (DECL_CONTEXT (fndecl
) == NULL_TREE
)
1584 pushdecl_top_level (fndecl
);
1590 /* Create a declaration for a procedure. For external functions (in the C
1591 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
1592 a master function with alternate entry points. */
1595 build_function_decl (gfc_symbol
* sym
, bool global
)
1597 tree fndecl
, type
, attributes
;
1598 symbol_attribute attr
;
1600 gfc_formal_arglist
*f
;
1602 gcc_assert (!sym
->backend_decl
);
1603 gcc_assert (!sym
->attr
.external
);
1605 /* Set the line and filename. sym->declared_at seems to point to the
1606 last statement for subroutines, but it'll do for now. */
1607 gfc_set_backend_locus (&sym
->declared_at
);
1609 /* Allow only one nesting level. Allow public declarations. */
1610 gcc_assert (current_function_decl
== NULL_TREE
1611 || DECL_CONTEXT (current_function_decl
) == NULL_TREE
1612 || TREE_CODE (DECL_CONTEXT (current_function_decl
))
1615 type
= gfc_get_function_type (sym
);
1616 fndecl
= build_decl (input_location
,
1617 FUNCTION_DECL
, gfc_sym_identifier (sym
), type
);
1621 attributes
= add_attributes_to_decl (attr
, NULL_TREE
);
1622 decl_attributes (&fndecl
, attributes
, 0);
1624 /* Perform name mangling if this is a top level or module procedure. */
1625 if (current_function_decl
== NULL_TREE
)
1626 gfc_set_decl_assembler_name (fndecl
, gfc_sym_mangled_function_id (sym
));
1628 /* Figure out the return type of the declared function, and build a
1629 RESULT_DECL for it. If this is a subroutine with alternate
1630 returns, build a RESULT_DECL for it. */
1631 result_decl
= NULL_TREE
;
1632 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
1635 if (gfc_return_by_reference (sym
))
1636 type
= void_type_node
;
1639 if (sym
->result
!= sym
)
1640 result_decl
= gfc_sym_identifier (sym
->result
);
1642 type
= TREE_TYPE (TREE_TYPE (fndecl
));
1647 /* Look for alternate return placeholders. */
1648 int has_alternate_returns
= 0;
1649 for (f
= sym
->formal
; f
; f
= f
->next
)
1653 has_alternate_returns
= 1;
1658 if (has_alternate_returns
)
1659 type
= integer_type_node
;
1661 type
= void_type_node
;
1664 result_decl
= build_decl (input_location
,
1665 RESULT_DECL
, result_decl
, type
);
1666 DECL_ARTIFICIAL (result_decl
) = 1;
1667 DECL_IGNORED_P (result_decl
) = 1;
1668 DECL_CONTEXT (result_decl
) = fndecl
;
1669 DECL_RESULT (fndecl
) = result_decl
;
1671 /* Don't call layout_decl for a RESULT_DECL.
1672 layout_decl (result_decl, 0); */
1674 /* Set up all attributes for the function. */
1675 DECL_CONTEXT (fndecl
) = current_function_decl
;
1676 DECL_EXTERNAL (fndecl
) = 0;
1678 /* This specifies if a function is globally visible, i.e. it is
1679 the opposite of declaring static in C. */
1680 if (DECL_CONTEXT (fndecl
) == NULL_TREE
1681 && !sym
->attr
.entry_master
&& !sym
->attr
.is_main_program
)
1682 TREE_PUBLIC (fndecl
) = 1;
1684 /* TREE_STATIC means the function body is defined here. */
1685 TREE_STATIC (fndecl
) = 1;
1687 /* Set attributes for PURE functions. A call to a PURE function in the
1688 Fortran 95 sense is both pure and without side effects in the C
1690 if (attr
.pure
|| attr
.elemental
)
1692 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
1693 including an alternate return. In that case it can also be
1694 marked as PURE. See also in gfc_get_extern_function_decl(). */
1695 if (attr
.function
&& !gfc_return_by_reference (sym
))
1696 DECL_PURE_P (fndecl
) = 1;
1697 TREE_SIDE_EFFECTS (fndecl
) = 0;
1701 /* Layout the function declaration and put it in the binding level
1702 of the current function. */
1705 pushdecl_top_level (fndecl
);
1709 sym
->backend_decl
= fndecl
;
1713 /* Create the DECL_ARGUMENTS for a procedure. */
1716 create_function_arglist (gfc_symbol
* sym
)
1719 gfc_formal_arglist
*f
;
1720 tree typelist
, hidden_typelist
;
1721 tree arglist
, hidden_arglist
;
1725 fndecl
= sym
->backend_decl
;
1727 /* Build formal argument list. Make sure that their TREE_CONTEXT is
1728 the new FUNCTION_DECL node. */
1729 arglist
= NULL_TREE
;
1730 hidden_arglist
= NULL_TREE
;
1731 typelist
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
1733 if (sym
->attr
.entry_master
)
1735 type
= TREE_VALUE (typelist
);
1736 parm
= build_decl (input_location
,
1737 PARM_DECL
, get_identifier ("__entry"), type
);
1739 DECL_CONTEXT (parm
) = fndecl
;
1740 DECL_ARG_TYPE (parm
) = type
;
1741 TREE_READONLY (parm
) = 1;
1742 gfc_finish_decl (parm
);
1743 DECL_ARTIFICIAL (parm
) = 1;
1745 arglist
= chainon (arglist
, parm
);
1746 typelist
= TREE_CHAIN (typelist
);
1749 if (gfc_return_by_reference (sym
))
1751 tree type
= TREE_VALUE (typelist
), length
= NULL
;
1753 if (sym
->ts
.type
== BT_CHARACTER
)
1755 /* Length of character result. */
1756 tree len_type
= TREE_VALUE (TREE_CHAIN (typelist
));
1757 gcc_assert (len_type
== gfc_charlen_type_node
);
1759 length
= build_decl (input_location
,
1761 get_identifier (".__result"),
1763 if (!sym
->ts
.u
.cl
->length
)
1765 sym
->ts
.u
.cl
->backend_decl
= length
;
1766 TREE_USED (length
) = 1;
1768 gcc_assert (TREE_CODE (length
) == PARM_DECL
);
1769 DECL_CONTEXT (length
) = fndecl
;
1770 DECL_ARG_TYPE (length
) = len_type
;
1771 TREE_READONLY (length
) = 1;
1772 DECL_ARTIFICIAL (length
) = 1;
1773 gfc_finish_decl (length
);
1774 if (sym
->ts
.u
.cl
->backend_decl
== NULL
1775 || sym
->ts
.u
.cl
->backend_decl
== length
)
1780 if (sym
->ts
.u
.cl
->backend_decl
== NULL
)
1782 tree len
= build_decl (input_location
,
1784 get_identifier ("..__result"),
1785 gfc_charlen_type_node
);
1786 DECL_ARTIFICIAL (len
) = 1;
1787 TREE_USED (len
) = 1;
1788 sym
->ts
.u
.cl
->backend_decl
= len
;
1791 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1792 arg
= sym
->result
? sym
->result
: sym
;
1793 backend_decl
= arg
->backend_decl
;
1794 /* Temporary clear it, so that gfc_sym_type creates complete
1796 arg
->backend_decl
= NULL
;
1797 type
= gfc_sym_type (arg
);
1798 arg
->backend_decl
= backend_decl
;
1799 type
= build_reference_type (type
);
1803 parm
= build_decl (input_location
,
1804 PARM_DECL
, get_identifier ("__result"), type
);
1806 DECL_CONTEXT (parm
) = fndecl
;
1807 DECL_ARG_TYPE (parm
) = TREE_VALUE (typelist
);
1808 TREE_READONLY (parm
) = 1;
1809 DECL_ARTIFICIAL (parm
) = 1;
1810 gfc_finish_decl (parm
);
1812 arglist
= chainon (arglist
, parm
);
1813 typelist
= TREE_CHAIN (typelist
);
1815 if (sym
->ts
.type
== BT_CHARACTER
)
1817 gfc_allocate_lang_decl (parm
);
1818 arglist
= chainon (arglist
, length
);
1819 typelist
= TREE_CHAIN (typelist
);
1823 hidden_typelist
= typelist
;
1824 for (f
= sym
->formal
; f
; f
= f
->next
)
1825 if (f
->sym
!= NULL
) /* Ignore alternate returns. */
1826 hidden_typelist
= TREE_CHAIN (hidden_typelist
);
1828 for (f
= sym
->formal
; f
; f
= f
->next
)
1830 char name
[GFC_MAX_SYMBOL_LEN
+ 2];
1832 /* Ignore alternate returns. */
1836 type
= TREE_VALUE (typelist
);
1838 if (f
->sym
->ts
.type
== BT_CHARACTER
1839 && (!sym
->attr
.is_bind_c
|| sym
->attr
.entry_master
))
1841 tree len_type
= TREE_VALUE (hidden_typelist
);
1842 tree length
= NULL_TREE
;
1843 gcc_assert (len_type
== gfc_charlen_type_node
);
1845 strcpy (&name
[1], f
->sym
->name
);
1847 length
= build_decl (input_location
,
1848 PARM_DECL
, get_identifier (name
), len_type
);
1850 hidden_arglist
= chainon (hidden_arglist
, length
);
1851 DECL_CONTEXT (length
) = fndecl
;
1852 DECL_ARTIFICIAL (length
) = 1;
1853 DECL_ARG_TYPE (length
) = len_type
;
1854 TREE_READONLY (length
) = 1;
1855 gfc_finish_decl (length
);
1857 /* Remember the passed value. */
1858 if (f
->sym
->ts
.u
.cl
->passed_length
!= NULL
)
1860 /* This can happen if the same type is used for multiple
1861 arguments. We need to copy cl as otherwise
1862 cl->passed_length gets overwritten. */
1863 f
->sym
->ts
.u
.cl
= gfc_new_charlen (f
->sym
->ns
, f
->sym
->ts
.u
.cl
);
1865 f
->sym
->ts
.u
.cl
->passed_length
= length
;
1867 /* Use the passed value for assumed length variables. */
1868 if (!f
->sym
->ts
.u
.cl
->length
)
1870 TREE_USED (length
) = 1;
1871 gcc_assert (!f
->sym
->ts
.u
.cl
->backend_decl
);
1872 f
->sym
->ts
.u
.cl
->backend_decl
= length
;
1875 hidden_typelist
= TREE_CHAIN (hidden_typelist
);
1877 if (f
->sym
->ts
.u
.cl
->backend_decl
== NULL
1878 || f
->sym
->ts
.u
.cl
->backend_decl
== length
)
1880 if (f
->sym
->ts
.u
.cl
->backend_decl
== NULL
)
1881 gfc_create_string_length (f
->sym
);
1883 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1884 if (f
->sym
->attr
.flavor
== FL_PROCEDURE
)
1885 type
= build_pointer_type (gfc_get_function_type (f
->sym
));
1887 type
= gfc_sym_type (f
->sym
);
1891 /* For non-constant length array arguments, make sure they use
1892 a different type node from TYPE_ARG_TYPES type. */
1893 if (f
->sym
->attr
.dimension
1894 && type
== TREE_VALUE (typelist
)
1895 && TREE_CODE (type
) == POINTER_TYPE
1896 && GFC_ARRAY_TYPE_P (type
)
1897 && f
->sym
->as
->type
!= AS_ASSUMED_SIZE
1898 && ! COMPLETE_TYPE_P (TREE_TYPE (type
)))
1900 if (f
->sym
->attr
.flavor
== FL_PROCEDURE
)
1901 type
= build_pointer_type (gfc_get_function_type (f
->sym
));
1903 type
= gfc_sym_type (f
->sym
);
1906 if (f
->sym
->attr
.proc_pointer
)
1907 type
= build_pointer_type (type
);
1909 /* Build the argument declaration. */
1910 parm
= build_decl (input_location
,
1911 PARM_DECL
, gfc_sym_identifier (f
->sym
), type
);
1913 /* Fill in arg stuff. */
1914 DECL_CONTEXT (parm
) = fndecl
;
1915 DECL_ARG_TYPE (parm
) = TREE_VALUE (typelist
);
1916 /* All implementation args are read-only. */
1917 TREE_READONLY (parm
) = 1;
1918 if (POINTER_TYPE_P (type
)
1919 && (!f
->sym
->attr
.proc_pointer
1920 && f
->sym
->attr
.flavor
!= FL_PROCEDURE
))
1921 DECL_BY_REFERENCE (parm
) = 1;
1923 gfc_finish_decl (parm
);
1925 f
->sym
->backend_decl
= parm
;
1927 arglist
= chainon (arglist
, parm
);
1928 typelist
= TREE_CHAIN (typelist
);
1931 /* Add the hidden string length parameters, unless the procedure
1933 if (!sym
->attr
.is_bind_c
)
1934 arglist
= chainon (arglist
, hidden_arglist
);
1936 gcc_assert (hidden_typelist
== NULL_TREE
1937 || TREE_VALUE (hidden_typelist
) == void_type_node
);
1938 DECL_ARGUMENTS (fndecl
) = arglist
;
1941 /* Do the setup necessary before generating the body of a function. */
1944 trans_function_start (gfc_symbol
* sym
)
1948 fndecl
= sym
->backend_decl
;
1950 /* Let GCC know the current scope is this function. */
1951 current_function_decl
= fndecl
;
1953 /* Let the world know what we're about to do. */
1954 announce_function (fndecl
);
1956 if (DECL_CONTEXT (fndecl
) == NULL_TREE
)
1958 /* Create RTL for function declaration. */
1959 rest_of_decl_compilation (fndecl
, 1, 0);
1962 /* Create RTL for function definition. */
1963 make_decl_rtl (fndecl
);
1965 init_function_start (fndecl
);
1967 /* Even though we're inside a function body, we still don't want to
1968 call expand_expr to calculate the size of a variable-sized array.
1969 We haven't necessarily assigned RTL to all variables yet, so it's
1970 not safe to try to expand expressions involving them. */
1971 cfun
->dont_save_pending_sizes_p
= 1;
1973 /* function.c requires a push at the start of the function. */
1977 /* Create thunks for alternate entry points. */
1980 build_entry_thunks (gfc_namespace
* ns
, bool global
)
1982 gfc_formal_arglist
*formal
;
1983 gfc_formal_arglist
*thunk_formal
;
1985 gfc_symbol
*thunk_sym
;
1991 /* This should always be a toplevel function. */
1992 gcc_assert (current_function_decl
== NULL_TREE
);
1994 gfc_get_backend_locus (&old_loc
);
1995 for (el
= ns
->entries
; el
; el
= el
->next
)
1997 VEC(tree
,gc
) *args
= NULL
;
1998 VEC(tree
,gc
) *string_args
= NULL
;
2000 thunk_sym
= el
->sym
;
2002 build_function_decl (thunk_sym
, global
);
2003 create_function_arglist (thunk_sym
);
2005 trans_function_start (thunk_sym
);
2007 thunk_fndecl
= thunk_sym
->backend_decl
;
2009 gfc_init_block (&body
);
2011 /* Pass extra parameter identifying this entry point. */
2012 tmp
= build_int_cst (gfc_array_index_type
, el
->id
);
2013 VEC_safe_push (tree
, gc
, args
, tmp
);
2015 if (thunk_sym
->attr
.function
)
2017 if (gfc_return_by_reference (ns
->proc_name
))
2019 tree ref
= DECL_ARGUMENTS (current_function_decl
);
2020 VEC_safe_push (tree
, gc
, args
, ref
);
2021 if (ns
->proc_name
->ts
.type
== BT_CHARACTER
)
2022 VEC_safe_push (tree
, gc
, args
, DECL_CHAIN (ref
));
2026 for (formal
= ns
->proc_name
->formal
; formal
; formal
= formal
->next
)
2028 /* Ignore alternate returns. */
2029 if (formal
->sym
== NULL
)
2032 /* We don't have a clever way of identifying arguments, so resort to
2033 a brute-force search. */
2034 for (thunk_formal
= thunk_sym
->formal
;
2036 thunk_formal
= thunk_formal
->next
)
2038 if (thunk_formal
->sym
== formal
->sym
)
2044 /* Pass the argument. */
2045 DECL_ARTIFICIAL (thunk_formal
->sym
->backend_decl
) = 1;
2046 VEC_safe_push (tree
, gc
, args
, thunk_formal
->sym
->backend_decl
);
2047 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
2049 tmp
= thunk_formal
->sym
->ts
.u
.cl
->backend_decl
;
2050 VEC_safe_push (tree
, gc
, string_args
, tmp
);
2055 /* Pass NULL for a missing argument. */
2056 VEC_safe_push (tree
, gc
, args
, null_pointer_node
);
2057 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
2059 tmp
= build_int_cst (gfc_charlen_type_node
, 0);
2060 VEC_safe_push (tree
, gc
, string_args
, tmp
);
2065 /* Call the master function. */
2066 VEC_safe_splice (tree
, gc
, args
, string_args
);
2067 tmp
= ns
->proc_name
->backend_decl
;
2068 tmp
= build_call_expr_loc_vec (input_location
, tmp
, args
);
2069 if (ns
->proc_name
->attr
.mixed_entry_master
)
2071 tree union_decl
, field
;
2072 tree master_type
= TREE_TYPE (ns
->proc_name
->backend_decl
);
2074 union_decl
= build_decl (input_location
,
2075 VAR_DECL
, get_identifier ("__result"),
2076 TREE_TYPE (master_type
));
2077 DECL_ARTIFICIAL (union_decl
) = 1;
2078 DECL_EXTERNAL (union_decl
) = 0;
2079 TREE_PUBLIC (union_decl
) = 0;
2080 TREE_USED (union_decl
) = 1;
2081 layout_decl (union_decl
, 0);
2082 pushdecl (union_decl
);
2084 DECL_CONTEXT (union_decl
) = current_function_decl
;
2085 tmp
= fold_build2 (MODIFY_EXPR
, TREE_TYPE (union_decl
),
2087 gfc_add_expr_to_block (&body
, tmp
);
2089 for (field
= TYPE_FIELDS (TREE_TYPE (union_decl
));
2090 field
; field
= DECL_CHAIN (field
))
2091 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field
)),
2092 thunk_sym
->result
->name
) == 0)
2094 gcc_assert (field
!= NULL_TREE
);
2095 tmp
= fold_build3 (COMPONENT_REF
, TREE_TYPE (field
),
2096 union_decl
, field
, NULL_TREE
);
2097 tmp
= fold_build2 (MODIFY_EXPR
,
2098 TREE_TYPE (DECL_RESULT (current_function_decl
)),
2099 DECL_RESULT (current_function_decl
), tmp
);
2100 tmp
= build1_v (RETURN_EXPR
, tmp
);
2102 else if (TREE_TYPE (DECL_RESULT (current_function_decl
))
2105 tmp
= fold_build2 (MODIFY_EXPR
,
2106 TREE_TYPE (DECL_RESULT (current_function_decl
)),
2107 DECL_RESULT (current_function_decl
), tmp
);
2108 tmp
= build1_v (RETURN_EXPR
, tmp
);
2110 gfc_add_expr_to_block (&body
, tmp
);
2112 /* Finish off this function and send it for code generation. */
2113 DECL_SAVED_TREE (thunk_fndecl
) = gfc_finish_block (&body
);
2116 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl
)) = thunk_fndecl
;
2117 DECL_SAVED_TREE (thunk_fndecl
)
2118 = build3_v (BIND_EXPR
, tmp
, DECL_SAVED_TREE (thunk_fndecl
),
2119 DECL_INITIAL (thunk_fndecl
));
2121 /* Output the GENERIC tree. */
2122 dump_function (TDI_original
, thunk_fndecl
);
2124 /* Store the end of the function, so that we get good line number
2125 info for the epilogue. */
2126 cfun
->function_end_locus
= input_location
;
2128 /* We're leaving the context of this function, so zap cfun.
2129 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
2130 tree_rest_of_compilation. */
2133 current_function_decl
= NULL_TREE
;
2135 cgraph_finalize_function (thunk_fndecl
, true);
2137 /* We share the symbols in the formal argument list with other entry
2138 points and the master function. Clear them so that they are
2139 recreated for each function. */
2140 for (formal
= thunk_sym
->formal
; formal
; formal
= formal
->next
)
2141 if (formal
->sym
!= NULL
) /* Ignore alternate returns. */
2143 formal
->sym
->backend_decl
= NULL_TREE
;
2144 if (formal
->sym
->ts
.type
== BT_CHARACTER
)
2145 formal
->sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
2148 if (thunk_sym
->attr
.function
)
2150 if (thunk_sym
->ts
.type
== BT_CHARACTER
)
2151 thunk_sym
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
2152 if (thunk_sym
->result
->ts
.type
== BT_CHARACTER
)
2153 thunk_sym
->result
->ts
.u
.cl
->backend_decl
= NULL_TREE
;
2157 gfc_set_backend_locus (&old_loc
);
2161 /* Create a decl for a function, and create any thunks for alternate entry
2162 points. If global is true, generate the function in the global binding
2163 level, otherwise in the current binding level (which can be global). */
2166 gfc_create_function_decl (gfc_namespace
* ns
, bool global
)
2168 /* Create a declaration for the master function. */
2169 build_function_decl (ns
->proc_name
, global
);
2171 /* Compile the entry thunks. */
2173 build_entry_thunks (ns
, global
);
2175 /* Now create the read argument list. */
2176 create_function_arglist (ns
->proc_name
);
2179 /* Return the decl used to hold the function return value. If
2180 parent_flag is set, the context is the parent_scope. */
2183 gfc_get_fake_result_decl (gfc_symbol
* sym
, int parent_flag
)
2187 tree this_fake_result_decl
;
2188 tree this_function_decl
;
2190 char name
[GFC_MAX_SYMBOL_LEN
+ 10];
2194 this_fake_result_decl
= parent_fake_result_decl
;
2195 this_function_decl
= DECL_CONTEXT (current_function_decl
);
2199 this_fake_result_decl
= current_fake_result_decl
;
2200 this_function_decl
= current_function_decl
;
2204 && sym
->ns
->proc_name
->backend_decl
== this_function_decl
2205 && sym
->ns
->proc_name
->attr
.entry_master
2206 && sym
!= sym
->ns
->proc_name
)
2209 if (this_fake_result_decl
!= NULL
)
2210 for (t
= TREE_CHAIN (this_fake_result_decl
); t
; t
= TREE_CHAIN (t
))
2211 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t
)), sym
->name
) == 0)
2214 return TREE_VALUE (t
);
2215 decl
= gfc_get_fake_result_decl (sym
->ns
->proc_name
, parent_flag
);
2218 this_fake_result_decl
= parent_fake_result_decl
;
2220 this_fake_result_decl
= current_fake_result_decl
;
2222 if (decl
&& sym
->ns
->proc_name
->attr
.mixed_entry_master
)
2226 for (field
= TYPE_FIELDS (TREE_TYPE (decl
));
2227 field
; field
= DECL_CHAIN (field
))
2228 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field
)),
2232 gcc_assert (field
!= NULL_TREE
);
2233 decl
= fold_build3 (COMPONENT_REF
, TREE_TYPE (field
),
2234 decl
, field
, NULL_TREE
);
2237 var
= create_tmp_var_raw (TREE_TYPE (decl
), sym
->name
);
2239 gfc_add_decl_to_parent_function (var
);
2241 gfc_add_decl_to_function (var
);
2243 SET_DECL_VALUE_EXPR (var
, decl
);
2244 DECL_HAS_VALUE_EXPR_P (var
) = 1;
2245 GFC_DECL_RESULT (var
) = 1;
2247 TREE_CHAIN (this_fake_result_decl
)
2248 = tree_cons (get_identifier (sym
->name
), var
,
2249 TREE_CHAIN (this_fake_result_decl
));
2253 if (this_fake_result_decl
!= NULL_TREE
)
2254 return TREE_VALUE (this_fake_result_decl
);
2256 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
2261 if (sym
->ts
.type
== BT_CHARACTER
)
2263 if (sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
2264 length
= gfc_create_string_length (sym
);
2266 length
= sym
->ts
.u
.cl
->backend_decl
;
2267 if (TREE_CODE (length
) == VAR_DECL
2268 && DECL_CONTEXT (length
) == NULL_TREE
)
2269 gfc_add_decl_to_function (length
);
2272 if (gfc_return_by_reference (sym
))
2274 decl
= DECL_ARGUMENTS (this_function_decl
);
2276 if (sym
->ns
->proc_name
->backend_decl
== this_function_decl
2277 && sym
->ns
->proc_name
->attr
.entry_master
)
2278 decl
= DECL_CHAIN (decl
);
2280 TREE_USED (decl
) = 1;
2282 decl
= gfc_build_dummy_array_decl (sym
, decl
);
2286 sprintf (name
, "__result_%.20s",
2287 IDENTIFIER_POINTER (DECL_NAME (this_function_decl
)));
2289 if (!sym
->attr
.mixed_entry_master
&& sym
->attr
.function
)
2290 decl
= build_decl (DECL_SOURCE_LOCATION (this_function_decl
),
2291 VAR_DECL
, get_identifier (name
),
2292 gfc_sym_type (sym
));
2294 decl
= build_decl (DECL_SOURCE_LOCATION (this_function_decl
),
2295 VAR_DECL
, get_identifier (name
),
2296 TREE_TYPE (TREE_TYPE (this_function_decl
)));
2297 DECL_ARTIFICIAL (decl
) = 1;
2298 DECL_EXTERNAL (decl
) = 0;
2299 TREE_PUBLIC (decl
) = 0;
2300 TREE_USED (decl
) = 1;
2301 GFC_DECL_RESULT (decl
) = 1;
2302 TREE_ADDRESSABLE (decl
) = 1;
2304 layout_decl (decl
, 0);
2307 gfc_add_decl_to_parent_function (decl
);
2309 gfc_add_decl_to_function (decl
);
2313 parent_fake_result_decl
= build_tree_list (NULL
, decl
);
2315 current_fake_result_decl
= build_tree_list (NULL
, decl
);
2321 /* Builds a function decl. The remaining parameters are the types of the
2322 function arguments. Negative nargs indicates a varargs function. */
2325 build_library_function_decl_1 (tree name
, const char *spec
,
2326 tree rettype
, int nargs
, va_list p
)
2334 /* Library functions must be declared with global scope. */
2335 gcc_assert (current_function_decl
== NULL_TREE
);
2337 /* Create a list of the argument types. */
2338 for (arglist
= NULL_TREE
, n
= abs (nargs
); n
> 0; n
--)
2340 argtype
= va_arg (p
, tree
);
2341 arglist
= gfc_chainon_list (arglist
, argtype
);
2346 /* Terminate the list. */
2347 arglist
= gfc_chainon_list (arglist
, void_type_node
);
2350 /* Build the function type and decl. */
2351 fntype
= build_function_type (rettype
, arglist
);
2354 tree attr_args
= build_tree_list (NULL_TREE
,
2355 build_string (strlen (spec
), spec
));
2356 tree attrs
= tree_cons (get_identifier ("fn spec"),
2357 attr_args
, TYPE_ATTRIBUTES (fntype
));
2358 fntype
= build_type_attribute_variant (fntype
, attrs
);
2360 fndecl
= build_decl (input_location
,
2361 FUNCTION_DECL
, name
, fntype
);
2363 /* Mark this decl as external. */
2364 DECL_EXTERNAL (fndecl
) = 1;
2365 TREE_PUBLIC (fndecl
) = 1;
2369 rest_of_decl_compilation (fndecl
, 1, 0);
2374 /* Builds a function decl. The remaining parameters are the types of the
2375 function arguments. Negative nargs indicates a varargs function. */
2378 gfc_build_library_function_decl (tree name
, tree rettype
, int nargs
, ...)
2382 va_start (args
, nargs
);
2383 ret
= build_library_function_decl_1 (name
, NULL
, rettype
, nargs
, args
);
2388 /* Builds a function decl. The remaining parameters are the types of the
2389 function arguments. Negative nargs indicates a varargs function.
2390 The SPEC parameter specifies the function argument and return type
2391 specification according to the fnspec function type attribute. */
2394 gfc_build_library_function_decl_with_spec (tree name
, const char *spec
,
2395 tree rettype
, int nargs
, ...)
2399 va_start (args
, nargs
);
2400 ret
= build_library_function_decl_1 (name
, spec
, rettype
, nargs
, args
);
2406 gfc_build_intrinsic_function_decls (void)
2408 tree gfc_int4_type_node
= gfc_get_int_type (4);
2409 tree gfc_int8_type_node
= gfc_get_int_type (8);
2410 tree gfc_int16_type_node
= gfc_get_int_type (16);
2411 tree gfc_logical4_type_node
= gfc_get_logical_type (4);
2412 tree pchar1_type_node
= gfc_get_pchar_type (1);
2413 tree pchar4_type_node
= gfc_get_pchar_type (4);
2415 /* String functions. */
2416 gfor_fndecl_compare_string
= gfc_build_library_function_decl_with_spec (
2417 get_identifier (PREFIX("compare_string")), "..R.R",
2418 integer_type_node
, 4, gfc_charlen_type_node
, pchar1_type_node
,
2419 gfc_charlen_type_node
, pchar1_type_node
);
2420 DECL_PURE_P (gfor_fndecl_compare_string
) = 1;
2422 gfor_fndecl_concat_string
= gfc_build_library_function_decl_with_spec (
2423 get_identifier (PREFIX("concat_string")), "..W.R.R",
2424 void_type_node
, 6, gfc_charlen_type_node
, pchar1_type_node
,
2425 gfc_charlen_type_node
, pchar1_type_node
,
2426 gfc_charlen_type_node
, pchar1_type_node
);
2428 gfor_fndecl_string_len_trim
= gfc_build_library_function_decl_with_spec (
2429 get_identifier (PREFIX("string_len_trim")), "..R",
2430 gfc_charlen_type_node
, 2, gfc_charlen_type_node
, pchar1_type_node
);
2431 DECL_PURE_P (gfor_fndecl_string_len_trim
) = 1;
2433 gfor_fndecl_string_index
= gfc_build_library_function_decl_with_spec (
2434 get_identifier (PREFIX("string_index")), "..R.R.",
2435 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
2436 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
2437 DECL_PURE_P (gfor_fndecl_string_index
) = 1;
2439 gfor_fndecl_string_scan
= gfc_build_library_function_decl_with_spec (
2440 get_identifier (PREFIX("string_scan")), "..R.R.",
2441 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
2442 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
2443 DECL_PURE_P (gfor_fndecl_string_scan
) = 1;
2445 gfor_fndecl_string_verify
= gfc_build_library_function_decl_with_spec (
2446 get_identifier (PREFIX("string_verify")), "..R.R.",
2447 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar1_type_node
,
2448 gfc_charlen_type_node
, pchar1_type_node
, gfc_logical4_type_node
);
2449 DECL_PURE_P (gfor_fndecl_string_verify
) = 1;
2451 gfor_fndecl_string_trim
= gfc_build_library_function_decl_with_spec (
2452 get_identifier (PREFIX("string_trim")), ".Ww.R",
2453 void_type_node
, 4, build_pointer_type (gfc_charlen_type_node
),
2454 build_pointer_type (pchar1_type_node
), gfc_charlen_type_node
,
2457 gfor_fndecl_string_minmax
= gfc_build_library_function_decl_with_spec (
2458 get_identifier (PREFIX("string_minmax")), ".Ww.R",
2459 void_type_node
, -4, build_pointer_type (gfc_charlen_type_node
),
2460 build_pointer_type (pchar1_type_node
), integer_type_node
,
2463 gfor_fndecl_adjustl
= gfc_build_library_function_decl_with_spec (
2464 get_identifier (PREFIX("adjustl")), ".W.R",
2465 void_type_node
, 3, pchar1_type_node
, gfc_charlen_type_node
,
2468 gfor_fndecl_adjustr
= gfc_build_library_function_decl_with_spec (
2469 get_identifier (PREFIX("adjustr")), ".W.R",
2470 void_type_node
, 3, pchar1_type_node
, gfc_charlen_type_node
,
2473 gfor_fndecl_select_string
= gfc_build_library_function_decl_with_spec (
2474 get_identifier (PREFIX("select_string")), ".R.R.",
2475 integer_type_node
, 4, pvoid_type_node
, integer_type_node
,
2476 pchar1_type_node
, gfc_charlen_type_node
);
2477 DECL_PURE_P (gfor_fndecl_select_string
) = 1;
2479 gfor_fndecl_compare_string_char4
= gfc_build_library_function_decl_with_spec (
2480 get_identifier (PREFIX("compare_string_char4")), "..R.R",
2481 integer_type_node
, 4, gfc_charlen_type_node
, pchar4_type_node
,
2482 gfc_charlen_type_node
, pchar4_type_node
);
2483 DECL_PURE_P (gfor_fndecl_compare_string_char4
) = 1;
2485 gfor_fndecl_concat_string_char4
= gfc_build_library_function_decl_with_spec (
2486 get_identifier (PREFIX("concat_string_char4")), "..W.R.R",
2487 void_type_node
, 6, gfc_charlen_type_node
, pchar4_type_node
,
2488 gfc_charlen_type_node
, pchar4_type_node
, gfc_charlen_type_node
,
2491 gfor_fndecl_string_len_trim_char4
= gfc_build_library_function_decl_with_spec (
2492 get_identifier (PREFIX("string_len_trim_char4")), "..R",
2493 gfc_charlen_type_node
, 2, gfc_charlen_type_node
, pchar4_type_node
);
2494 DECL_PURE_P (gfor_fndecl_string_len_trim_char4
) = 1;
2496 gfor_fndecl_string_index_char4
= gfc_build_library_function_decl_with_spec (
2497 get_identifier (PREFIX("string_index_char4")), "..R.R.",
2498 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
2499 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
2500 DECL_PURE_P (gfor_fndecl_string_index_char4
) = 1;
2502 gfor_fndecl_string_scan_char4
= gfc_build_library_function_decl_with_spec (
2503 get_identifier (PREFIX("string_scan_char4")), "..R.R.",
2504 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
2505 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
2506 DECL_PURE_P (gfor_fndecl_string_scan_char4
) = 1;
2508 gfor_fndecl_string_verify_char4
= gfc_build_library_function_decl_with_spec (
2509 get_identifier (PREFIX("string_verify_char4")), "..R.R.",
2510 gfc_charlen_type_node
, 5, gfc_charlen_type_node
, pchar4_type_node
,
2511 gfc_charlen_type_node
, pchar4_type_node
, gfc_logical4_type_node
);
2512 DECL_PURE_P (gfor_fndecl_string_verify_char4
) = 1;
2514 gfor_fndecl_string_trim_char4
= gfc_build_library_function_decl_with_spec (
2515 get_identifier (PREFIX("string_trim_char4")), ".Ww.R",
2516 void_type_node
, 4, build_pointer_type (gfc_charlen_type_node
),
2517 build_pointer_type (pchar4_type_node
), gfc_charlen_type_node
,
2520 gfor_fndecl_string_minmax_char4
= gfc_build_library_function_decl_with_spec (
2521 get_identifier (PREFIX("string_minmax_char4")), ".Ww.R",
2522 void_type_node
, -4, build_pointer_type (gfc_charlen_type_node
),
2523 build_pointer_type (pchar4_type_node
), integer_type_node
,
2526 gfor_fndecl_adjustl_char4
= gfc_build_library_function_decl_with_spec (
2527 get_identifier (PREFIX("adjustl_char4")), ".W.R",
2528 void_type_node
, 3, pchar4_type_node
, gfc_charlen_type_node
,
2531 gfor_fndecl_adjustr_char4
= gfc_build_library_function_decl_with_spec (
2532 get_identifier (PREFIX("adjustr_char4")), ".W.R",
2533 void_type_node
, 3, pchar4_type_node
, gfc_charlen_type_node
,
2536 gfor_fndecl_select_string_char4
= gfc_build_library_function_decl_with_spec (
2537 get_identifier (PREFIX("select_string_char4")), ".R.R.",
2538 integer_type_node
, 4, pvoid_type_node
, integer_type_node
,
2539 pvoid_type_node
, gfc_charlen_type_node
);
2540 DECL_PURE_P (gfor_fndecl_select_string_char4
) = 1;
2543 /* Conversion between character kinds. */
2545 gfor_fndecl_convert_char1_to_char4
= gfc_build_library_function_decl_with_spec (
2546 get_identifier (PREFIX("convert_char1_to_char4")), ".w.R",
2547 void_type_node
, 3, build_pointer_type (pchar4_type_node
),
2548 gfc_charlen_type_node
, pchar1_type_node
);
2550 gfor_fndecl_convert_char4_to_char1
= gfc_build_library_function_decl_with_spec (
2551 get_identifier (PREFIX("convert_char4_to_char1")), ".w.R",
2552 void_type_node
, 3, build_pointer_type (pchar1_type_node
),
2553 gfc_charlen_type_node
, pchar4_type_node
);
2555 /* Misc. functions. */
2557 gfor_fndecl_ttynam
= gfc_build_library_function_decl_with_spec (
2558 get_identifier (PREFIX("ttynam")), ".W",
2559 void_type_node
, 3, pchar_type_node
, gfc_charlen_type_node
,
2562 gfor_fndecl_fdate
= gfc_build_library_function_decl_with_spec (
2563 get_identifier (PREFIX("fdate")), ".W",
2564 void_type_node
, 2, pchar_type_node
, gfc_charlen_type_node
);
2566 gfor_fndecl_ctime
= gfc_build_library_function_decl_with_spec (
2567 get_identifier (PREFIX("ctime")), ".W",
2568 void_type_node
, 3, pchar_type_node
, gfc_charlen_type_node
,
2569 gfc_int8_type_node
);
2571 gfor_fndecl_sc_kind
= gfc_build_library_function_decl_with_spec (
2572 get_identifier (PREFIX("selected_char_kind")), "..R",
2573 gfc_int4_type_node
, 2, gfc_charlen_type_node
, pchar_type_node
);
2574 DECL_PURE_P (gfor_fndecl_sc_kind
) = 1;
2576 gfor_fndecl_si_kind
= gfc_build_library_function_decl_with_spec (
2577 get_identifier (PREFIX("selected_int_kind")), ".R",
2578 gfc_int4_type_node
, 1, pvoid_type_node
);
2579 DECL_PURE_P (gfor_fndecl_si_kind
) = 1;
2581 gfor_fndecl_sr_kind
= gfc_build_library_function_decl_with_spec (
2582 get_identifier (PREFIX("selected_real_kind2008")), ".RR",
2583 gfc_int4_type_node
, 3, pvoid_type_node
, pvoid_type_node
,
2585 DECL_PURE_P (gfor_fndecl_sr_kind
) = 1;
2587 /* Power functions. */
2589 tree ctype
, rtype
, itype
, jtype
;
2590 int rkind
, ikind
, jkind
;
2593 static int ikinds
[NIKINDS
] = {4, 8, 16};
2594 static int rkinds
[NRKINDS
] = {4, 8, 10, 16};
2595 char name
[PREFIX_LEN
+ 12]; /* _gfortran_pow_?n_?n */
2597 for (ikind
=0; ikind
< NIKINDS
; ikind
++)
2599 itype
= gfc_get_int_type (ikinds
[ikind
]);
2601 for (jkind
=0; jkind
< NIKINDS
; jkind
++)
2603 jtype
= gfc_get_int_type (ikinds
[jkind
]);
2606 sprintf(name
, PREFIX("pow_i%d_i%d"), ikinds
[ikind
],
2608 gfor_fndecl_math_powi
[jkind
][ikind
].integer
=
2609 gfc_build_library_function_decl (get_identifier (name
),
2610 jtype
, 2, jtype
, itype
);
2611 TREE_READONLY (gfor_fndecl_math_powi
[jkind
][ikind
].integer
) = 1;
2615 for (rkind
= 0; rkind
< NRKINDS
; rkind
++)
2617 rtype
= gfc_get_real_type (rkinds
[rkind
]);
2620 sprintf(name
, PREFIX("pow_r%d_i%d"), rkinds
[rkind
],
2622 gfor_fndecl_math_powi
[rkind
][ikind
].real
=
2623 gfc_build_library_function_decl (get_identifier (name
),
2624 rtype
, 2, rtype
, itype
);
2625 TREE_READONLY (gfor_fndecl_math_powi
[rkind
][ikind
].real
) = 1;
2628 ctype
= gfc_get_complex_type (rkinds
[rkind
]);
2631 sprintf(name
, PREFIX("pow_c%d_i%d"), rkinds
[rkind
],
2633 gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
=
2634 gfc_build_library_function_decl (get_identifier (name
),
2635 ctype
, 2,ctype
, itype
);
2636 TREE_READONLY (gfor_fndecl_math_powi
[rkind
][ikind
].cmplx
) = 1;
2644 gfor_fndecl_math_ishftc4
= gfc_build_library_function_decl (
2645 get_identifier (PREFIX("ishftc4")),
2646 gfc_int4_type_node
, 3, gfc_int4_type_node
, gfc_int4_type_node
,
2647 gfc_int4_type_node
);
2649 gfor_fndecl_math_ishftc8
= gfc_build_library_function_decl (
2650 get_identifier (PREFIX("ishftc8")),
2651 gfc_int8_type_node
, 3, gfc_int8_type_node
, gfc_int4_type_node
,
2652 gfc_int4_type_node
);
2654 if (gfc_int16_type_node
)
2655 gfor_fndecl_math_ishftc16
= gfc_build_library_function_decl (
2656 get_identifier (PREFIX("ishftc16")),
2657 gfc_int16_type_node
, 3, gfc_int16_type_node
, gfc_int4_type_node
,
2658 gfc_int4_type_node
);
2660 /* BLAS functions. */
2662 tree pint
= build_pointer_type (integer_type_node
);
2663 tree ps
= build_pointer_type (gfc_get_real_type (gfc_default_real_kind
));
2664 tree pd
= build_pointer_type (gfc_get_real_type (gfc_default_double_kind
));
2665 tree pc
= build_pointer_type (gfc_get_complex_type (gfc_default_real_kind
));
2666 tree pz
= build_pointer_type
2667 (gfc_get_complex_type (gfc_default_double_kind
));
2669 gfor_fndecl_sgemm
= gfc_build_library_function_decl
2671 (gfc_option
.flag_underscoring
? "sgemm_"
2673 void_type_node
, 15, pchar_type_node
,
2674 pchar_type_node
, pint
, pint
, pint
, ps
, ps
, pint
,
2675 ps
, pint
, ps
, ps
, pint
, integer_type_node
,
2677 gfor_fndecl_dgemm
= gfc_build_library_function_decl
2679 (gfc_option
.flag_underscoring
? "dgemm_"
2681 void_type_node
, 15, pchar_type_node
,
2682 pchar_type_node
, pint
, pint
, pint
, pd
, pd
, pint
,
2683 pd
, pint
, pd
, pd
, pint
, integer_type_node
,
2685 gfor_fndecl_cgemm
= gfc_build_library_function_decl
2687 (gfc_option
.flag_underscoring
? "cgemm_"
2689 void_type_node
, 15, pchar_type_node
,
2690 pchar_type_node
, pint
, pint
, pint
, pc
, pc
, pint
,
2691 pc
, pint
, pc
, pc
, pint
, integer_type_node
,
2693 gfor_fndecl_zgemm
= gfc_build_library_function_decl
2695 (gfc_option
.flag_underscoring
? "zgemm_"
2697 void_type_node
, 15, pchar_type_node
,
2698 pchar_type_node
, pint
, pint
, pint
, pz
, pz
, pint
,
2699 pz
, pint
, pz
, pz
, pint
, integer_type_node
,
2703 /* Other functions. */
2704 gfor_fndecl_size0
= gfc_build_library_function_decl_with_spec (
2705 get_identifier (PREFIX("size0")), ".R",
2706 gfc_array_index_type
, 1, pvoid_type_node
);
2707 DECL_PURE_P (gfor_fndecl_size0
) = 1;
2709 gfor_fndecl_size1
= gfc_build_library_function_decl_with_spec (
2710 get_identifier (PREFIX("size1")), ".R",
2711 gfc_array_index_type
, 2, pvoid_type_node
, gfc_array_index_type
);
2712 DECL_PURE_P (gfor_fndecl_size1
) = 1;
2714 gfor_fndecl_iargc
= gfc_build_library_function_decl (
2715 get_identifier (PREFIX ("iargc")), gfc_int4_type_node
, 0);
2717 if (gfc_type_for_size (128, true))
2719 tree uint128
= gfc_type_for_size (128, true);
2721 gfor_fndecl_clz128
= gfc_build_library_function_decl (
2722 get_identifier (PREFIX ("clz128")), integer_type_node
, 1, uint128
);
2723 TREE_READONLY (gfor_fndecl_clz128
) = 1;
2725 gfor_fndecl_ctz128
= gfc_build_library_function_decl (
2726 get_identifier (PREFIX ("ctz128")), integer_type_node
, 1, uint128
);
2727 TREE_READONLY (gfor_fndecl_ctz128
) = 1;
2732 /* Make prototypes for runtime library functions. */
2735 gfc_build_builtin_function_decls (void)
2737 tree gfc_int4_type_node
= gfc_get_int_type (4);
2739 gfor_fndecl_stop_numeric
= gfc_build_library_function_decl (
2740 get_identifier (PREFIX("stop_numeric")),
2741 void_type_node
, 1, gfc_int4_type_node
);
2742 /* STOP doesn't return. */
2743 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric
) = 1;
2745 gfor_fndecl_stop_string
= gfc_build_library_function_decl_with_spec (
2746 get_identifier (PREFIX("stop_string")), ".R.",
2747 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
2748 /* STOP doesn't return. */
2749 TREE_THIS_VOLATILE (gfor_fndecl_stop_string
) = 1;
2751 gfor_fndecl_error_stop_numeric
= gfc_build_library_function_decl (
2752 get_identifier (PREFIX("error_stop_numeric")),
2753 void_type_node
, 1, gfc_int4_type_node
);
2754 /* ERROR STOP doesn't return. */
2755 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_numeric
) = 1;
2757 gfor_fndecl_error_stop_string
= gfc_build_library_function_decl_with_spec (
2758 get_identifier (PREFIX("error_stop_string")), ".R.",
2759 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
2760 /* ERROR STOP doesn't return. */
2761 TREE_THIS_VOLATILE (gfor_fndecl_error_stop_string
) = 1;
2763 gfor_fndecl_pause_numeric
= gfc_build_library_function_decl (
2764 get_identifier (PREFIX("pause_numeric")),
2765 void_type_node
, 1, gfc_int4_type_node
);
2767 gfor_fndecl_pause_string
= gfc_build_library_function_decl_with_spec (
2768 get_identifier (PREFIX("pause_string")), ".R.",
2769 void_type_node
, 2, pchar_type_node
, gfc_int4_type_node
);
2771 gfor_fndecl_runtime_error
= gfc_build_library_function_decl_with_spec (
2772 get_identifier (PREFIX("runtime_error")), ".R",
2773 void_type_node
, -1, pchar_type_node
);
2774 /* The runtime_error function does not return. */
2775 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error
) = 1;
2777 gfor_fndecl_runtime_error_at
= gfc_build_library_function_decl_with_spec (
2778 get_identifier (PREFIX("runtime_error_at")), ".RR",
2779 void_type_node
, -2, pchar_type_node
, pchar_type_node
);
2780 /* The runtime_error_at function does not return. */
2781 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at
) = 1;
2783 gfor_fndecl_runtime_warning_at
= gfc_build_library_function_decl_with_spec (
2784 get_identifier (PREFIX("runtime_warning_at")), ".RR",
2785 void_type_node
, -2, pchar_type_node
, pchar_type_node
);
2787 gfor_fndecl_generate_error
= gfc_build_library_function_decl_with_spec (
2788 get_identifier (PREFIX("generate_error")), ".R.R",
2789 void_type_node
, 3, pvoid_type_node
, integer_type_node
,
2792 gfor_fndecl_os_error
= gfc_build_library_function_decl_with_spec (
2793 get_identifier (PREFIX("os_error")), ".R",
2794 void_type_node
, 1, pchar_type_node
);
2795 /* The runtime_error function does not return. */
2796 TREE_THIS_VOLATILE (gfor_fndecl_os_error
) = 1;
2798 gfor_fndecl_set_args
= gfc_build_library_function_decl (
2799 get_identifier (PREFIX("set_args")),
2800 void_type_node
, 2, integer_type_node
,
2801 build_pointer_type (pchar_type_node
));
2803 gfor_fndecl_set_fpe
= gfc_build_library_function_decl (
2804 get_identifier (PREFIX("set_fpe")),
2805 void_type_node
, 1, integer_type_node
);
2807 /* Keep the array dimension in sync with the call, later in this file. */
2808 gfor_fndecl_set_options
= gfc_build_library_function_decl_with_spec (
2809 get_identifier (PREFIX("set_options")), "..R",
2810 void_type_node
, 2, integer_type_node
,
2811 build_pointer_type (integer_type_node
));
2813 gfor_fndecl_set_convert
= gfc_build_library_function_decl (
2814 get_identifier (PREFIX("set_convert")),
2815 void_type_node
, 1, integer_type_node
);
2817 gfor_fndecl_set_record_marker
= gfc_build_library_function_decl (
2818 get_identifier (PREFIX("set_record_marker")),
2819 void_type_node
, 1, integer_type_node
);
2821 gfor_fndecl_set_max_subrecord_length
= gfc_build_library_function_decl (
2822 get_identifier (PREFIX("set_max_subrecord_length")),
2823 void_type_node
, 1, integer_type_node
);
2825 gfor_fndecl_in_pack
= gfc_build_library_function_decl_with_spec (
2826 get_identifier (PREFIX("internal_pack")), ".r",
2827 pvoid_type_node
, 1, pvoid_type_node
);
2829 gfor_fndecl_in_unpack
= gfc_build_library_function_decl_with_spec (
2830 get_identifier (PREFIX("internal_unpack")), ".wR",
2831 void_type_node
, 2, pvoid_type_node
, pvoid_type_node
);
2833 gfor_fndecl_associated
= gfc_build_library_function_decl_with_spec (
2834 get_identifier (PREFIX("associated")), ".RR",
2835 integer_type_node
, 2, ppvoid_type_node
, ppvoid_type_node
);
2836 DECL_PURE_P (gfor_fndecl_associated
) = 1;
2838 gfc_build_intrinsic_function_decls ();
2839 gfc_build_intrinsic_lib_fndecls ();
2840 gfc_build_io_library_fndecls ();
2844 /* Evaluate the length of dummy character variables. */
2847 gfc_trans_dummy_character (gfc_symbol
*sym
, gfc_charlen
*cl
,
2848 gfc_wrapped_block
*block
)
2852 gfc_finish_decl (cl
->backend_decl
);
2854 gfc_start_block (&init
);
2856 /* Evaluate the string length expression. */
2857 gfc_conv_string_length (cl
, NULL
, &init
);
2859 gfc_trans_vla_type_sizes (sym
, &init
);
2861 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
2865 /* Allocate and cleanup an automatic character variable. */
2868 gfc_trans_auto_character_variable (gfc_symbol
* sym
, gfc_wrapped_block
* block
)
2874 gcc_assert (sym
->backend_decl
);
2875 gcc_assert (sym
->ts
.u
.cl
&& sym
->ts
.u
.cl
->length
);
2877 gfc_start_block (&init
);
2879 /* Evaluate the string length expression. */
2880 gfc_conv_string_length (sym
->ts
.u
.cl
, NULL
, &init
);
2882 gfc_trans_vla_type_sizes (sym
, &init
);
2884 decl
= sym
->backend_decl
;
2886 /* Emit a DECL_EXPR for this variable, which will cause the
2887 gimplifier to allocate storage, and all that good stuff. */
2888 tmp
= fold_build1 (DECL_EXPR
, TREE_TYPE (decl
), decl
);
2889 gfc_add_expr_to_block (&init
, tmp
);
2891 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
2894 /* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
2897 gfc_trans_assign_aux_var (gfc_symbol
* sym
, gfc_wrapped_block
* block
)
2901 gcc_assert (sym
->backend_decl
);
2902 gfc_start_block (&init
);
2904 /* Set the initial value to length. See the comments in
2905 function gfc_add_assign_aux_vars in this file. */
2906 gfc_add_modify (&init
, GFC_DECL_STRING_LEN (sym
->backend_decl
),
2907 build_int_cst (NULL_TREE
, -2));
2909 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
2913 gfc_trans_vla_one_sizepos (tree
*tp
, stmtblock_t
*body
)
2915 tree t
= *tp
, var
, val
;
2917 if (t
== NULL
|| t
== error_mark_node
)
2919 if (TREE_CONSTANT (t
) || DECL_P (t
))
2922 if (TREE_CODE (t
) == SAVE_EXPR
)
2924 if (SAVE_EXPR_RESOLVED_P (t
))
2926 *tp
= TREE_OPERAND (t
, 0);
2929 val
= TREE_OPERAND (t
, 0);
2934 var
= gfc_create_var_np (TREE_TYPE (t
), NULL
);
2935 gfc_add_decl_to_function (var
);
2936 gfc_add_modify (body
, var
, val
);
2937 if (TREE_CODE (t
) == SAVE_EXPR
)
2938 TREE_OPERAND (t
, 0) = var
;
2943 gfc_trans_vla_type_sizes_1 (tree type
, stmtblock_t
*body
)
2947 if (type
== NULL
|| type
== error_mark_node
)
2950 type
= TYPE_MAIN_VARIANT (type
);
2952 if (TREE_CODE (type
) == INTEGER_TYPE
)
2954 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type
), body
);
2955 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type
), body
);
2957 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
2959 TYPE_MIN_VALUE (t
) = TYPE_MIN_VALUE (type
);
2960 TYPE_MAX_VALUE (t
) = TYPE_MAX_VALUE (type
);
2963 else if (TREE_CODE (type
) == ARRAY_TYPE
)
2965 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type
), body
);
2966 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type
), body
);
2967 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type
), body
);
2968 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type
), body
);
2970 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
2972 TYPE_SIZE (t
) = TYPE_SIZE (type
);
2973 TYPE_SIZE_UNIT (t
) = TYPE_SIZE_UNIT (type
);
2978 /* Make sure all type sizes and array domains are either constant,
2979 or variable or parameter decls. This is a simplified variant
2980 of gimplify_type_sizes, but we can't use it here, as none of the
2981 variables in the expressions have been gimplified yet.
2982 As type sizes and domains for various variable length arrays
2983 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
2984 time, without this routine gimplify_type_sizes in the middle-end
2985 could result in the type sizes being gimplified earlier than where
2986 those variables are initialized. */
2989 gfc_trans_vla_type_sizes (gfc_symbol
*sym
, stmtblock_t
*body
)
2991 tree type
= TREE_TYPE (sym
->backend_decl
);
2993 if (TREE_CODE (type
) == FUNCTION_TYPE
2994 && (sym
->attr
.function
|| sym
->attr
.result
|| sym
->attr
.entry
))
2996 if (! current_fake_result_decl
)
2999 type
= TREE_TYPE (TREE_VALUE (current_fake_result_decl
));
3002 while (POINTER_TYPE_P (type
))
3003 type
= TREE_TYPE (type
);
3005 if (GFC_DESCRIPTOR_TYPE_P (type
))
3007 tree etype
= GFC_TYPE_ARRAY_DATAPTR_TYPE (type
);
3009 while (POINTER_TYPE_P (etype
))
3010 etype
= TREE_TYPE (etype
);
3012 gfc_trans_vla_type_sizes_1 (etype
, body
);
3015 gfc_trans_vla_type_sizes_1 (type
, body
);
3019 /* Initialize a derived type by building an lvalue from the symbol
3020 and using trans_assignment to do the work. Set dealloc to false
3021 if no deallocation prior the assignment is needed. */
3023 gfc_init_default_dt (gfc_symbol
* sym
, stmtblock_t
* block
, bool dealloc
)
3031 gcc_assert (!sym
->attr
.allocatable
);
3032 gfc_set_sym_referenced (sym
);
3033 e
= gfc_lval_expr_from_sym (sym
);
3034 tmp
= gfc_trans_assignment (e
, sym
->value
, false, dealloc
);
3035 if (sym
->attr
.dummy
&& (sym
->attr
.optional
3036 || sym
->ns
->proc_name
->attr
.entry_master
))
3038 present
= gfc_conv_expr_present (sym
);
3039 tmp
= build3 (COND_EXPR
, TREE_TYPE (tmp
), present
,
3040 tmp
, build_empty_stmt (input_location
));
3042 gfc_add_expr_to_block (block
, tmp
);
3047 /* Initialize INTENT(OUT) derived type dummies. As well as giving
3048 them their default initializer, if they do not have allocatable
3049 components, they have their allocatable components deallocated. */
3052 init_intent_out_dt (gfc_symbol
* proc_sym
, gfc_wrapped_block
* block
)
3055 gfc_formal_arglist
*f
;
3059 gfc_init_block (&init
);
3060 for (f
= proc_sym
->formal
; f
; f
= f
->next
)
3061 if (f
->sym
&& f
->sym
->attr
.intent
== INTENT_OUT
3062 && !f
->sym
->attr
.pointer
3063 && f
->sym
->ts
.type
== BT_DERIVED
)
3065 if (f
->sym
->ts
.u
.derived
->attr
.alloc_comp
&& !f
->sym
->value
)
3067 tmp
= gfc_deallocate_alloc_comp (f
->sym
->ts
.u
.derived
,
3068 f
->sym
->backend_decl
,
3069 f
->sym
->as
? f
->sym
->as
->rank
: 0);
3071 if (f
->sym
->attr
.optional
3072 || f
->sym
->ns
->proc_name
->attr
.entry_master
)
3074 present
= gfc_conv_expr_present (f
->sym
);
3075 tmp
= build3 (COND_EXPR
, TREE_TYPE (tmp
), present
,
3076 tmp
, build_empty_stmt (input_location
));
3079 gfc_add_expr_to_block (&init
, tmp
);
3081 else if (f
->sym
->value
)
3082 gfc_init_default_dt (f
->sym
, &init
, true);
3085 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), NULL_TREE
);
3089 /* Generate function entry and exit code, and add it to the function body.
3091 Allocation and initialization of array variables.
3092 Allocation of character string variables.
3093 Initialization and possibly repacking of dummy arrays.
3094 Initialization of ASSIGN statement auxiliary variable.
3095 Automatic deallocation. */
3098 gfc_trans_deferred_vars (gfc_symbol
* proc_sym
, gfc_wrapped_block
* block
)
3102 gfc_formal_arglist
*f
;
3103 stmtblock_t tmpblock
;
3104 bool seen_trans_deferred_array
= false;
3106 /* Deal with implicit return variables. Explicit return variables will
3107 already have been added. */
3108 if (gfc_return_by_reference (proc_sym
) && proc_sym
->result
== proc_sym
)
3110 if (!current_fake_result_decl
)
3112 gfc_entry_list
*el
= NULL
;
3113 if (proc_sym
->attr
.entry_master
)
3115 for (el
= proc_sym
->ns
->entries
; el
; el
= el
->next
)
3116 if (el
->sym
!= el
->sym
->result
)
3119 /* TODO: move to the appropriate place in resolve.c. */
3120 if (warn_return_type
&& el
== NULL
)
3121 gfc_warning ("Return value of function '%s' at %L not set",
3122 proc_sym
->name
, &proc_sym
->declared_at
);
3124 else if (proc_sym
->as
)
3126 tree result
= TREE_VALUE (current_fake_result_decl
);
3127 gfc_trans_dummy_array_bias (proc_sym
, result
, block
);
3129 /* An automatic character length, pointer array result. */
3130 if (proc_sym
->ts
.type
== BT_CHARACTER
3131 && TREE_CODE (proc_sym
->ts
.u
.cl
->backend_decl
) == VAR_DECL
)
3132 gfc_trans_dummy_character (proc_sym
, proc_sym
->ts
.u
.cl
, block
);
3134 else if (proc_sym
->ts
.type
== BT_CHARACTER
)
3136 if (TREE_CODE (proc_sym
->ts
.u
.cl
->backend_decl
) == VAR_DECL
)
3137 gfc_trans_dummy_character (proc_sym
, proc_sym
->ts
.u
.cl
, block
);
3140 gcc_assert (gfc_option
.flag_f2c
3141 && proc_sym
->ts
.type
== BT_COMPLEX
);
3144 /* Initialize the INTENT(OUT) derived type dummy arguments. This
3145 should be done here so that the offsets and lbounds of arrays
3147 init_intent_out_dt (proc_sym
, block
);
3149 for (sym
= proc_sym
->tlink
; sym
!= proc_sym
; sym
= sym
->tlink
)
3151 bool sym_has_alloc_comp
= (sym
->ts
.type
== BT_DERIVED
)
3152 && sym
->ts
.u
.derived
->attr
.alloc_comp
;
3153 if (sym
->attr
.dimension
)
3155 switch (sym
->as
->type
)
3158 if (sym
->attr
.dummy
|| sym
->attr
.result
)
3159 gfc_trans_dummy_array_bias (sym
, sym
->backend_decl
, block
);
3160 else if (sym
->attr
.pointer
|| sym
->attr
.allocatable
)
3162 if (TREE_STATIC (sym
->backend_decl
))
3163 gfc_trans_static_array_pointer (sym
);
3166 seen_trans_deferred_array
= true;
3167 gfc_trans_deferred_array (sym
, block
);
3172 if (sym_has_alloc_comp
)
3174 seen_trans_deferred_array
= true;
3175 gfc_trans_deferred_array (sym
, block
);
3177 else if (sym
->ts
.type
== BT_DERIVED
3180 && sym
->attr
.save
== SAVE_NONE
)
3182 gfc_start_block (&tmpblock
);
3183 gfc_init_default_dt (sym
, &tmpblock
, false);
3184 gfc_add_init_cleanup (block
,
3185 gfc_finish_block (&tmpblock
),
3189 gfc_get_backend_locus (&loc
);
3190 gfc_set_backend_locus (&sym
->declared_at
);
3191 gfc_trans_auto_array_allocation (sym
->backend_decl
,
3193 gfc_set_backend_locus (&loc
);
3197 case AS_ASSUMED_SIZE
:
3198 /* Must be a dummy parameter. */
3199 gcc_assert (sym
->attr
.dummy
|| sym
->as
->cp_was_assumed
);
3201 /* We should always pass assumed size arrays the g77 way. */
3202 if (sym
->attr
.dummy
)
3203 gfc_trans_g77_array (sym
, block
);
3206 case AS_ASSUMED_SHAPE
:
3207 /* Must be a dummy parameter. */
3208 gcc_assert (sym
->attr
.dummy
);
3210 gfc_trans_dummy_array_bias (sym
, sym
->backend_decl
, block
);
3214 seen_trans_deferred_array
= true;
3215 gfc_trans_deferred_array (sym
, block
);
3221 if (sym_has_alloc_comp
&& !seen_trans_deferred_array
)
3222 gfc_trans_deferred_array (sym
, block
);
3224 else if (sym
->attr
.allocatable
3225 || (sym
->ts
.type
== BT_CLASS
3226 && CLASS_DATA (sym
)->attr
.allocatable
))
3228 if (!sym
->attr
.save
)
3230 /* Nullify and automatic deallocation of allocatable
3237 e
= gfc_lval_expr_from_sym (sym
);
3238 if (sym
->ts
.type
== BT_CLASS
)
3239 gfc_add_component_ref (e
, "$data");
3241 gfc_init_se (&se
, NULL
);
3242 se
.want_pointer
= 1;
3243 gfc_conv_expr (&se
, e
);
3246 /* Nullify when entering the scope. */
3247 gfc_start_block (&init
);
3248 gfc_add_modify (&init
, se
.expr
,
3249 fold_convert (TREE_TYPE (se
.expr
),
3250 null_pointer_node
));
3252 /* Deallocate when leaving the scope. Nullifying is not
3255 if (!sym
->attr
.result
)
3256 tmp
= gfc_deallocate_with_status (se
.expr
, NULL_TREE
,
3258 gfc_add_init_cleanup (block
, gfc_finish_block (&init
), tmp
);
3261 else if (sym_has_alloc_comp
)
3262 gfc_trans_deferred_array (sym
, block
);
3263 else if (sym
->ts
.type
== BT_CHARACTER
)
3265 gfc_get_backend_locus (&loc
);
3266 gfc_set_backend_locus (&sym
->declared_at
);
3267 if (sym
->attr
.dummy
|| sym
->attr
.result
)
3268 gfc_trans_dummy_character (sym
, sym
->ts
.u
.cl
, block
);
3270 gfc_trans_auto_character_variable (sym
, block
);
3271 gfc_set_backend_locus (&loc
);
3273 else if (sym
->attr
.assign
)
3275 gfc_get_backend_locus (&loc
);
3276 gfc_set_backend_locus (&sym
->declared_at
);
3277 gfc_trans_assign_aux_var (sym
, block
);
3278 gfc_set_backend_locus (&loc
);
3280 else if (sym
->ts
.type
== BT_DERIVED
3283 && sym
->attr
.save
== SAVE_NONE
)
3285 gfc_start_block (&tmpblock
);
3286 gfc_init_default_dt (sym
, &tmpblock
, false);
3287 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
),
3294 gfc_init_block (&tmpblock
);
3296 for (f
= proc_sym
->formal
; f
; f
= f
->next
)
3298 if (f
->sym
&& f
->sym
->tlink
== NULL
&& f
->sym
->ts
.type
== BT_CHARACTER
)
3300 gcc_assert (f
->sym
->ts
.u
.cl
->backend_decl
!= NULL
);
3301 if (TREE_CODE (f
->sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
3302 gfc_trans_vla_type_sizes (f
->sym
, &tmpblock
);
3306 if (gfc_return_by_reference (proc_sym
) && proc_sym
->ts
.type
== BT_CHARACTER
3307 && current_fake_result_decl
!= NULL
)
3309 gcc_assert (proc_sym
->ts
.u
.cl
->backend_decl
!= NULL
);
3310 if (TREE_CODE (proc_sym
->ts
.u
.cl
->backend_decl
) == PARM_DECL
)
3311 gfc_trans_vla_type_sizes (proc_sym
, &tmpblock
);
3314 gfc_add_init_cleanup (block
, gfc_finish_block (&tmpblock
), NULL_TREE
);
3317 static GTY ((param_is (struct module_htab_entry
))) htab_t module_htab
;
3319 /* Hash and equality functions for module_htab. */
3322 module_htab_do_hash (const void *x
)
3324 return htab_hash_string (((const struct module_htab_entry
*)x
)->name
);
3328 module_htab_eq (const void *x1
, const void *x2
)
3330 return strcmp ((((const struct module_htab_entry
*)x1
)->name
),
3331 (const char *)x2
) == 0;
3334 /* Hash and equality functions for module_htab's decls. */
3337 module_htab_decls_hash (const void *x
)
3339 const_tree t
= (const_tree
) x
;
3340 const_tree n
= DECL_NAME (t
);
3342 n
= TYPE_NAME (TREE_TYPE (t
));
3343 return htab_hash_string (IDENTIFIER_POINTER (n
));
3347 module_htab_decls_eq (const void *x1
, const void *x2
)
3349 const_tree t1
= (const_tree
) x1
;
3350 const_tree n1
= DECL_NAME (t1
);
3351 if (n1
== NULL_TREE
)
3352 n1
= TYPE_NAME (TREE_TYPE (t1
));
3353 return strcmp (IDENTIFIER_POINTER (n1
), (const char *) x2
) == 0;
3356 struct module_htab_entry
*
3357 gfc_find_module (const char *name
)
3362 module_htab
= htab_create_ggc (10, module_htab_do_hash
,
3363 module_htab_eq
, NULL
);
3365 slot
= htab_find_slot_with_hash (module_htab
, name
,
3366 htab_hash_string (name
), INSERT
);
3369 struct module_htab_entry
*entry
= ggc_alloc_cleared_module_htab_entry ();
3371 entry
->name
= gfc_get_string (name
);
3372 entry
->decls
= htab_create_ggc (10, module_htab_decls_hash
,
3373 module_htab_decls_eq
, NULL
);
3374 *slot
= (void *) entry
;
3376 return (struct module_htab_entry
*) *slot
;
3380 gfc_module_add_decl (struct module_htab_entry
*entry
, tree decl
)
3385 if (DECL_NAME (decl
))
3386 name
= IDENTIFIER_POINTER (DECL_NAME (decl
));
3389 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
3390 name
= IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl
)));
3392 slot
= htab_find_slot_with_hash (entry
->decls
, name
,
3393 htab_hash_string (name
), INSERT
);
3395 *slot
= (void *) decl
;
3398 static struct module_htab_entry
*cur_module
;
3400 /* Output an initialized decl for a module variable. */
3403 gfc_create_module_variable (gfc_symbol
* sym
)
3407 /* Module functions with alternate entries are dealt with later and
3408 would get caught by the next condition. */
3409 if (sym
->attr
.entry
)
3412 /* Make sure we convert the types of the derived types from iso_c_binding
3414 if (sym
->attr
.flavor
!= FL_PROCEDURE
&& sym
->attr
.is_iso_c
3415 && sym
->ts
.type
== BT_DERIVED
)
3416 sym
->backend_decl
= gfc_typenode_for_spec (&(sym
->ts
));
3418 if (sym
->attr
.flavor
== FL_DERIVED
3419 && sym
->backend_decl
3420 && TREE_CODE (sym
->backend_decl
) == RECORD_TYPE
)
3422 decl
= sym
->backend_decl
;
3423 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
3425 /* -fwhole-file mixes up the contexts so these asserts are unnecessary. */
3426 if (!(gfc_option
.flag_whole_file
&& sym
->attr
.use_assoc
))
3428 gcc_assert (TYPE_CONTEXT (decl
) == NULL_TREE
3429 || TYPE_CONTEXT (decl
) == sym
->ns
->proc_name
->backend_decl
);
3430 gcc_assert (DECL_CONTEXT (TYPE_STUB_DECL (decl
)) == NULL_TREE
3431 || DECL_CONTEXT (TYPE_STUB_DECL (decl
))
3432 == sym
->ns
->proc_name
->backend_decl
);
3434 TYPE_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
3435 DECL_CONTEXT (TYPE_STUB_DECL (decl
)) = sym
->ns
->proc_name
->backend_decl
;
3436 gfc_module_add_decl (cur_module
, TYPE_STUB_DECL (decl
));
3439 /* Only output variables, procedure pointers and array valued,
3440 or derived type, parameters. */
3441 if (sym
->attr
.flavor
!= FL_VARIABLE
3442 && !(sym
->attr
.flavor
== FL_PARAMETER
3443 && (sym
->attr
.dimension
|| sym
->ts
.type
== BT_DERIVED
))
3444 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.proc_pointer
))
3447 if ((sym
->attr
.in_common
|| sym
->attr
.in_equivalence
) && sym
->backend_decl
)
3449 decl
= sym
->backend_decl
;
3450 gcc_assert (DECL_CONTEXT (decl
) == NULL_TREE
);
3451 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
3452 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
3453 gfc_module_add_decl (cur_module
, decl
);
3456 /* Don't generate variables from other modules. Variables from
3457 COMMONs will already have been generated. */
3458 if (sym
->attr
.use_assoc
|| sym
->attr
.in_common
)
3461 /* Equivalenced variables arrive here after creation. */
3462 if (sym
->backend_decl
3463 && (sym
->equiv_built
|| sym
->attr
.in_equivalence
))
3466 if (sym
->backend_decl
&& !sym
->attr
.vtab
)
3467 internal_error ("backend decl for module variable %s already exists",
3470 /* We always want module variables to be created. */
3471 sym
->attr
.referenced
= 1;
3472 /* Create the decl. */
3473 decl
= gfc_get_symbol_decl (sym
);
3475 /* Create the variable. */
3477 gcc_assert (DECL_CONTEXT (decl
) == NULL_TREE
);
3478 gcc_assert (sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
);
3479 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
3480 rest_of_decl_compilation (decl
, 1, 0);
3481 gfc_module_add_decl (cur_module
, decl
);
3483 /* Also add length of strings. */
3484 if (sym
->ts
.type
== BT_CHARACTER
)
3488 length
= sym
->ts
.u
.cl
->backend_decl
;
3489 gcc_assert (length
|| sym
->attr
.proc_pointer
);
3490 if (length
&& !INTEGER_CST_P (length
))
3493 rest_of_decl_compilation (length
, 1, 0);
3498 /* Emit debug information for USE statements. */
3501 gfc_trans_use_stmts (gfc_namespace
* ns
)
3503 gfc_use_list
*use_stmt
;
3504 for (use_stmt
= ns
->use_stmts
; use_stmt
; use_stmt
= use_stmt
->next
)
3506 struct module_htab_entry
*entry
3507 = gfc_find_module (use_stmt
->module_name
);
3508 gfc_use_rename
*rent
;
3510 if (entry
->namespace_decl
== NULL
)
3512 entry
->namespace_decl
3513 = build_decl (input_location
,
3515 get_identifier (use_stmt
->module_name
),
3517 DECL_EXTERNAL (entry
->namespace_decl
) = 1;
3519 gfc_set_backend_locus (&use_stmt
->where
);
3520 if (!use_stmt
->only_flag
)
3521 (*debug_hooks
->imported_module_or_decl
) (entry
->namespace_decl
,
3523 ns
->proc_name
->backend_decl
,
3525 for (rent
= use_stmt
->rename
; rent
; rent
= rent
->next
)
3527 tree decl
, local_name
;
3530 if (rent
->op
!= INTRINSIC_NONE
)
3533 slot
= htab_find_slot_with_hash (entry
->decls
, rent
->use_name
,
3534 htab_hash_string (rent
->use_name
),
3540 st
= gfc_find_symtree (ns
->sym_root
,
3542 ? rent
->local_name
: rent
->use_name
);
3545 /* Sometimes, generic interfaces wind up being over-ruled by a
3546 local symbol (see PR41062). */
3547 if (!st
->n
.sym
->attr
.use_assoc
)
3550 if (st
->n
.sym
->backend_decl
3551 && DECL_P (st
->n
.sym
->backend_decl
)
3552 && st
->n
.sym
->module
3553 && strcmp (st
->n
.sym
->module
, use_stmt
->module_name
) == 0)
3555 gcc_assert (DECL_EXTERNAL (entry
->namespace_decl
)
3556 || (TREE_CODE (st
->n
.sym
->backend_decl
)
3558 decl
= copy_node (st
->n
.sym
->backend_decl
);
3559 DECL_CONTEXT (decl
) = entry
->namespace_decl
;
3560 DECL_EXTERNAL (decl
) = 1;
3561 DECL_IGNORED_P (decl
) = 0;
3562 DECL_INITIAL (decl
) = NULL_TREE
;
3566 *slot
= error_mark_node
;
3567 htab_clear_slot (entry
->decls
, slot
);
3572 decl
= (tree
) *slot
;
3573 if (rent
->local_name
[0])
3574 local_name
= get_identifier (rent
->local_name
);
3576 local_name
= NULL_TREE
;
3577 gfc_set_backend_locus (&rent
->where
);
3578 (*debug_hooks
->imported_module_or_decl
) (decl
, local_name
,
3579 ns
->proc_name
->backend_decl
,
3580 !use_stmt
->only_flag
);
3586 /* Return true if expr is a constant initializer that gfc_conv_initializer
3590 check_constant_initializer (gfc_expr
*expr
, gfc_typespec
*ts
, bool array
,
3600 if (expr
->expr_type
== EXPR_CONSTANT
|| expr
->expr_type
== EXPR_NULL
)
3602 else if (expr
->expr_type
== EXPR_STRUCTURE
)
3603 return check_constant_initializer (expr
, ts
, false, false);
3604 else if (expr
->expr_type
!= EXPR_ARRAY
)
3606 for (c
= gfc_constructor_first (expr
->value
.constructor
);
3607 c
; c
= gfc_constructor_next (c
))
3611 if (c
->expr
->expr_type
== EXPR_STRUCTURE
)
3613 if (!check_constant_initializer (c
->expr
, ts
, false, false))
3616 else if (c
->expr
->expr_type
!= EXPR_CONSTANT
)
3621 else switch (ts
->type
)
3624 if (expr
->expr_type
!= EXPR_STRUCTURE
)
3626 cm
= expr
->ts
.u
.derived
->components
;
3627 for (c
= gfc_constructor_first (expr
->value
.constructor
);
3628 c
; c
= gfc_constructor_next (c
), cm
= cm
->next
)
3630 if (!c
->expr
|| cm
->attr
.allocatable
)
3632 if (!check_constant_initializer (c
->expr
, &cm
->ts
,
3639 return expr
->expr_type
== EXPR_CONSTANT
;
3643 /* Emit debug info for parameters and unreferenced variables with
3647 gfc_emit_parameter_debug_info (gfc_symbol
*sym
)
3651 if (sym
->attr
.flavor
!= FL_PARAMETER
3652 && (sym
->attr
.flavor
!= FL_VARIABLE
|| sym
->attr
.referenced
))
3655 if (sym
->backend_decl
!= NULL
3656 || sym
->value
== NULL
3657 || sym
->attr
.use_assoc
3660 || sym
->attr
.function
3661 || sym
->attr
.intrinsic
3662 || sym
->attr
.pointer
3663 || sym
->attr
.allocatable
3664 || sym
->attr
.cray_pointee
3665 || sym
->attr
.threadprivate
3666 || sym
->attr
.is_bind_c
3667 || sym
->attr
.subref_array_pointer
3668 || sym
->attr
.assign
)
3671 if (sym
->ts
.type
== BT_CHARACTER
)
3673 gfc_conv_const_charlen (sym
->ts
.u
.cl
);
3674 if (sym
->ts
.u
.cl
->backend_decl
== NULL
3675 || TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) != INTEGER_CST
)
3678 else if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
->attr
.alloc_comp
)
3685 if (sym
->as
->type
!= AS_EXPLICIT
)
3687 for (n
= 0; n
< sym
->as
->rank
; n
++)
3688 if (sym
->as
->lower
[n
]->expr_type
!= EXPR_CONSTANT
3689 || sym
->as
->upper
[n
] == NULL
3690 || sym
->as
->upper
[n
]->expr_type
!= EXPR_CONSTANT
)
3694 if (!check_constant_initializer (sym
->value
, &sym
->ts
,
3695 sym
->attr
.dimension
, false))
3698 /* Create the decl for the variable or constant. */
3699 decl
= build_decl (input_location
,
3700 sym
->attr
.flavor
== FL_PARAMETER
? CONST_DECL
: VAR_DECL
,
3701 gfc_sym_identifier (sym
), gfc_sym_type (sym
));
3702 if (sym
->attr
.flavor
== FL_PARAMETER
)
3703 TREE_READONLY (decl
) = 1;
3704 gfc_set_decl_location (decl
, &sym
->declared_at
);
3705 if (sym
->attr
.dimension
)
3706 GFC_DECL_PACKED_ARRAY (decl
) = 1;
3707 DECL_CONTEXT (decl
) = sym
->ns
->proc_name
->backend_decl
;
3708 TREE_STATIC (decl
) = 1;
3709 TREE_USED (decl
) = 1;
3710 if (DECL_CONTEXT (decl
) && TREE_CODE (DECL_CONTEXT (decl
)) == NAMESPACE_DECL
)
3711 TREE_PUBLIC (decl
) = 1;
3713 = gfc_conv_initializer (sym
->value
, &sym
->ts
, TREE_TYPE (decl
),
3714 sym
->attr
.dimension
, 0);
3715 debug_hooks
->global_decl (decl
);
3718 /* Generate all the required code for module variables. */
3721 gfc_generate_module_vars (gfc_namespace
* ns
)
3723 module_namespace
= ns
;
3724 cur_module
= gfc_find_module (ns
->proc_name
->name
);
3726 /* Check if the frontend left the namespace in a reasonable state. */
3727 gcc_assert (ns
->proc_name
&& !ns
->proc_name
->tlink
);
3729 /* Generate COMMON blocks. */
3730 gfc_trans_common (ns
);
3732 /* Create decls for all the module variables. */
3733 gfc_traverse_ns (ns
, gfc_create_module_variable
);
3737 gfc_trans_use_stmts (ns
);
3738 gfc_traverse_ns (ns
, gfc_emit_parameter_debug_info
);
3743 gfc_generate_contained_functions (gfc_namespace
* parent
)
3747 /* We create all the prototypes before generating any code. */
3748 for (ns
= parent
->contained
; ns
; ns
= ns
->sibling
)
3750 /* Skip namespaces from used modules. */
3751 if (ns
->parent
!= parent
)
3754 gfc_create_function_decl (ns
, false);
3757 for (ns
= parent
->contained
; ns
; ns
= ns
->sibling
)
3759 /* Skip namespaces from used modules. */
3760 if (ns
->parent
!= parent
)
3763 gfc_generate_function_code (ns
);
3768 /* Drill down through expressions for the array specification bounds and
3769 character length calling generate_local_decl for all those variables
3770 that have not already been declared. */
3773 generate_local_decl (gfc_symbol
*);
3775 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
3778 expr_decls (gfc_expr
*e
, gfc_symbol
*sym
,
3779 int *f ATTRIBUTE_UNUSED
)
3781 if (e
->expr_type
!= EXPR_VARIABLE
3782 || sym
== e
->symtree
->n
.sym
3783 || e
->symtree
->n
.sym
->mark
3784 || e
->symtree
->n
.sym
->ns
!= sym
->ns
)
3787 generate_local_decl (e
->symtree
->n
.sym
);
3792 generate_expr_decls (gfc_symbol
*sym
, gfc_expr
*e
)
3794 gfc_traverse_expr (e
, sym
, expr_decls
, 0);
3798 /* Check for dependencies in the character length and array spec. */
3801 generate_dependency_declarations (gfc_symbol
*sym
)
3805 if (sym
->ts
.type
== BT_CHARACTER
3807 && sym
->ts
.u
.cl
->length
3808 && sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
)
3809 generate_expr_decls (sym
, sym
->ts
.u
.cl
->length
);
3811 if (sym
->as
&& sym
->as
->rank
)
3813 for (i
= 0; i
< sym
->as
->rank
; i
++)
3815 generate_expr_decls (sym
, sym
->as
->lower
[i
]);
3816 generate_expr_decls (sym
, sym
->as
->upper
[i
]);
3822 /* Generate decls for all local variables. We do this to ensure correct
3823 handling of expressions which only appear in the specification of
3827 generate_local_decl (gfc_symbol
* sym
)
3829 if (sym
->attr
.flavor
== FL_VARIABLE
)
3831 if (!sym
->attr
.dummy
&& !sym
->ns
->proc_name
->attr
.entry_master
)
3832 generate_dependency_declarations (sym
);
3834 if (sym
->attr
.referenced
)
3835 gfc_get_symbol_decl (sym
);
3837 /* Warnings for unused dummy arguments. */
3838 else if (sym
->attr
.dummy
)
3840 /* INTENT(out) dummy arguments are likely meant to be set. */
3841 if (gfc_option
.warn_unused_dummy_argument
3842 && sym
->attr
.intent
== INTENT_OUT
)
3844 if (sym
->ts
.type
!= BT_DERIVED
)
3845 gfc_warning ("Dummy argument '%s' at %L was declared "
3846 "INTENT(OUT) but was not set", sym
->name
,
3848 else if (!gfc_has_default_initializer (sym
->ts
.u
.derived
))
3849 gfc_warning ("Derived-type dummy argument '%s' at %L was "
3850 "declared INTENT(OUT) but was not set and "
3851 "does not have a default initializer",
3852 sym
->name
, &sym
->declared_at
);
3854 else if (gfc_option
.warn_unused_dummy_argument
)
3855 gfc_warning ("Unused dummy argument '%s' at %L", sym
->name
,
3859 /* Warn for unused variables, but not if they're inside a common
3860 block or are use-associated. */
3861 else if (warn_unused_variable
3862 && !(sym
->attr
.in_common
|| sym
->attr
.use_assoc
|| sym
->mark
))
3863 gfc_warning ("Unused variable '%s' declared at %L", sym
->name
,
3866 /* For variable length CHARACTER parameters, the PARM_DECL already
3867 references the length variable, so force gfc_get_symbol_decl
3868 even when not referenced. If optimize > 0, it will be optimized
3869 away anyway. But do this only after emitting -Wunused-parameter
3870 warning if requested. */
3871 if (sym
->attr
.dummy
&& !sym
->attr
.referenced
3872 && sym
->ts
.type
== BT_CHARACTER
3873 && sym
->ts
.u
.cl
->backend_decl
!= NULL
3874 && TREE_CODE (sym
->ts
.u
.cl
->backend_decl
) == VAR_DECL
)
3876 sym
->attr
.referenced
= 1;
3877 gfc_get_symbol_decl (sym
);
3880 /* INTENT(out) dummy arguments and result variables with allocatable
3881 components are reset by default and need to be set referenced to
3882 generate the code for nullification and automatic lengths. */
3883 if (!sym
->attr
.referenced
3884 && sym
->ts
.type
== BT_DERIVED
3885 && sym
->ts
.u
.derived
->attr
.alloc_comp
3886 && !sym
->attr
.pointer
3887 && ((sym
->attr
.dummy
&& sym
->attr
.intent
== INTENT_OUT
)
3889 (sym
->attr
.result
&& sym
!= sym
->result
)))
3891 sym
->attr
.referenced
= 1;
3892 gfc_get_symbol_decl (sym
);
3895 /* Check for dependencies in the array specification and string
3896 length, adding the necessary declarations to the function. We
3897 mark the symbol now, as well as in traverse_ns, to prevent
3898 getting stuck in a circular dependency. */
3901 /* We do not want the middle-end to warn about unused parameters
3902 as this was already done above. */
3903 if (sym
->attr
.dummy
&& sym
->backend_decl
!= NULL_TREE
)
3904 TREE_NO_WARNING(sym
->backend_decl
) = 1;
3906 else if (sym
->attr
.flavor
== FL_PARAMETER
)
3908 if (warn_unused_parameter
3909 && !sym
->attr
.referenced
3910 && !sym
->attr
.use_assoc
)
3911 gfc_warning ("Unused parameter '%s' declared at %L", sym
->name
,
3914 else if (sym
->attr
.flavor
== FL_PROCEDURE
)
3916 /* TODO: move to the appropriate place in resolve.c. */
3917 if (warn_return_type
3918 && sym
->attr
.function
3920 && sym
!= sym
->result
3921 && !sym
->result
->attr
.referenced
3922 && !sym
->attr
.use_assoc
3923 && sym
->attr
.if_source
!= IFSRC_IFBODY
)
3925 gfc_warning ("Return value '%s' of function '%s' declared at "
3926 "%L not set", sym
->result
->name
, sym
->name
,
3927 &sym
->result
->declared_at
);
3929 /* Prevents "Unused variable" warning for RESULT variables. */
3930 sym
->result
->mark
= 1;
3934 if (sym
->attr
.dummy
== 1)
3936 /* Modify the tree type for scalar character dummy arguments of bind(c)
3937 procedures if they are passed by value. The tree type for them will
3938 be promoted to INTEGER_TYPE for the middle end, which appears to be
3939 what C would do with characters passed by-value. The value attribute
3940 implies the dummy is a scalar. */
3941 if (sym
->attr
.value
== 1 && sym
->backend_decl
!= NULL
3942 && sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.is_c_interop
3943 && sym
->ns
->proc_name
!= NULL
&& sym
->ns
->proc_name
->attr
.is_bind_c
)
3944 gfc_conv_scalar_char_value (sym
, NULL
, NULL
);
3947 /* Make sure we convert the types of the derived types from iso_c_binding
3949 if (sym
->attr
.flavor
!= FL_PROCEDURE
&& sym
->attr
.is_iso_c
3950 && sym
->ts
.type
== BT_DERIVED
)
3951 sym
->backend_decl
= gfc_typenode_for_spec (&(sym
->ts
));
3955 generate_local_vars (gfc_namespace
* ns
)
3957 gfc_traverse_ns (ns
, generate_local_decl
);
3961 /* Generate a switch statement to jump to the correct entry point. Also
3962 creates the label decls for the entry points. */
3965 gfc_trans_entry_master_switch (gfc_entry_list
* el
)
3972 gfc_init_block (&block
);
3973 for (; el
; el
= el
->next
)
3975 /* Add the case label. */
3976 label
= gfc_build_label_decl (NULL_TREE
);
3977 val
= build_int_cst (gfc_array_index_type
, el
->id
);
3978 tmp
= build3_v (CASE_LABEL_EXPR
, val
, NULL_TREE
, label
);
3979 gfc_add_expr_to_block (&block
, tmp
);
3981 /* And jump to the actual entry point. */
3982 label
= gfc_build_label_decl (NULL_TREE
);
3983 tmp
= build1_v (GOTO_EXPR
, label
);
3984 gfc_add_expr_to_block (&block
, tmp
);
3986 /* Save the label decl. */
3989 tmp
= gfc_finish_block (&block
);
3990 /* The first argument selects the entry point. */
3991 val
= DECL_ARGUMENTS (current_function_decl
);
3992 tmp
= build3_v (SWITCH_EXPR
, val
, tmp
, NULL_TREE
);
3997 /* Add code to string lengths of actual arguments passed to a function against
3998 the expected lengths of the dummy arguments. */
4001 add_argument_checking (stmtblock_t
*block
, gfc_symbol
*sym
)
4003 gfc_formal_arglist
*formal
;
4005 for (formal
= sym
->formal
; formal
; formal
= formal
->next
)
4006 if (formal
->sym
&& formal
->sym
->ts
.type
== BT_CHARACTER
)
4008 enum tree_code comparison
;
4013 const char *message
;
4019 gcc_assert (cl
->passed_length
!= NULL_TREE
);
4020 gcc_assert (cl
->backend_decl
!= NULL_TREE
);
4022 /* For POINTER, ALLOCATABLE and assumed-shape dummy arguments, the
4023 string lengths must match exactly. Otherwise, it is only required
4024 that the actual string length is *at least* the expected one.
4025 Sequence association allows for a mismatch of the string length
4026 if the actual argument is (part of) an array, but only if the
4027 dummy argument is an array. (See "Sequence association" in
4028 Section 12.4.1.4 for F95 and 12.4.1.5 for F2003.) */
4029 if (fsym
->attr
.pointer
|| fsym
->attr
.allocatable
4030 || (fsym
->as
&& fsym
->as
->type
== AS_ASSUMED_SHAPE
))
4032 comparison
= NE_EXPR
;
4033 message
= _("Actual string length does not match the declared one"
4034 " for dummy argument '%s' (%ld/%ld)");
4036 else if (fsym
->as
&& fsym
->as
->rank
!= 0)
4040 comparison
= LT_EXPR
;
4041 message
= _("Actual string length is shorter than the declared one"
4042 " for dummy argument '%s' (%ld/%ld)");
4045 /* Build the condition. For optional arguments, an actual length
4046 of 0 is also acceptable if the associated string is NULL, which
4047 means the argument was not passed. */
4048 cond
= fold_build2 (comparison
, boolean_type_node
,
4049 cl
->passed_length
, cl
->backend_decl
);
4050 if (fsym
->attr
.optional
)
4056 not_0length
= fold_build2 (NE_EXPR
, boolean_type_node
,
4058 fold_convert (gfc_charlen_type_node
,
4059 integer_zero_node
));
4060 /* The symbol needs to be referenced for gfc_get_symbol_decl. */
4061 fsym
->attr
.referenced
= 1;
4062 not_absent
= gfc_conv_expr_present (fsym
);
4064 absent_failed
= fold_build2 (TRUTH_OR_EXPR
, boolean_type_node
,
4065 not_0length
, not_absent
);
4067 cond
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
,
4068 cond
, absent_failed
);
4071 /* Build the runtime check. */
4072 argname
= gfc_build_cstring_const (fsym
->name
);
4073 argname
= gfc_build_addr_expr (pchar_type_node
, argname
);
4074 gfc_trans_runtime_check (true, false, cond
, block
, &fsym
->declared_at
,
4076 fold_convert (long_integer_type_node
,
4078 fold_convert (long_integer_type_node
,
4085 create_main_function (tree fndecl
)
4089 tree tmp
, decl
, result_decl
, argc
, argv
, typelist
, arglist
;
4092 old_context
= current_function_decl
;
4096 push_function_context ();
4097 saved_parent_function_decls
= saved_function_decls
;
4098 saved_function_decls
= NULL_TREE
;
4101 /* main() function must be declared with global scope. */
4102 gcc_assert (current_function_decl
== NULL_TREE
);
4104 /* Declare the function. */
4105 tmp
= build_function_type_list (integer_type_node
, integer_type_node
,
4106 build_pointer_type (pchar_type_node
),
4108 main_identifier_node
= get_identifier ("main");
4109 ftn_main
= build_decl (input_location
, FUNCTION_DECL
,
4110 main_identifier_node
, tmp
);
4111 DECL_EXTERNAL (ftn_main
) = 0;
4112 TREE_PUBLIC (ftn_main
) = 1;
4113 TREE_STATIC (ftn_main
) = 1;
4114 DECL_ATTRIBUTES (ftn_main
)
4115 = tree_cons (get_identifier("externally_visible"), NULL_TREE
, NULL_TREE
);
4117 /* Setup the result declaration (for "return 0"). */
4118 result_decl
= build_decl (input_location
,
4119 RESULT_DECL
, NULL_TREE
, integer_type_node
);
4120 DECL_ARTIFICIAL (result_decl
) = 1;
4121 DECL_IGNORED_P (result_decl
) = 1;
4122 DECL_CONTEXT (result_decl
) = ftn_main
;
4123 DECL_RESULT (ftn_main
) = result_decl
;
4125 pushdecl (ftn_main
);
4127 /* Get the arguments. */
4129 arglist
= NULL_TREE
;
4130 typelist
= TYPE_ARG_TYPES (TREE_TYPE (ftn_main
));
4132 tmp
= TREE_VALUE (typelist
);
4133 argc
= build_decl (input_location
, PARM_DECL
, get_identifier ("argc"), tmp
);
4134 DECL_CONTEXT (argc
) = ftn_main
;
4135 DECL_ARG_TYPE (argc
) = TREE_VALUE (typelist
);
4136 TREE_READONLY (argc
) = 1;
4137 gfc_finish_decl (argc
);
4138 arglist
= chainon (arglist
, argc
);
4140 typelist
= TREE_CHAIN (typelist
);
4141 tmp
= TREE_VALUE (typelist
);
4142 argv
= build_decl (input_location
, PARM_DECL
, get_identifier ("argv"), tmp
);
4143 DECL_CONTEXT (argv
) = ftn_main
;
4144 DECL_ARG_TYPE (argv
) = TREE_VALUE (typelist
);
4145 TREE_READONLY (argv
) = 1;
4146 DECL_BY_REFERENCE (argv
) = 1;
4147 gfc_finish_decl (argv
);
4148 arglist
= chainon (arglist
, argv
);
4150 DECL_ARGUMENTS (ftn_main
) = arglist
;
4151 current_function_decl
= ftn_main
;
4152 announce_function (ftn_main
);
4154 rest_of_decl_compilation (ftn_main
, 1, 0);
4155 make_decl_rtl (ftn_main
);
4156 init_function_start (ftn_main
);
4159 gfc_init_block (&body
);
4161 /* Call some libgfortran initialization routines, call then MAIN__(). */
4163 /* Call _gfortran_set_args (argc, argv). */
4164 TREE_USED (argc
) = 1;
4165 TREE_USED (argv
) = 1;
4166 tmp
= build_call_expr_loc (input_location
,
4167 gfor_fndecl_set_args
, 2, argc
, argv
);
4168 gfc_add_expr_to_block (&body
, tmp
);
4170 /* Add a call to set_options to set up the runtime library Fortran
4171 language standard parameters. */
4173 tree array_type
, array
, var
;
4174 VEC(constructor_elt
,gc
) *v
= NULL
;
4176 /* Passing a new option to the library requires four modifications:
4177 + add it to the tree_cons list below
4178 + change the array size in the call to build_array_type
4179 + change the first argument to the library call
4180 gfor_fndecl_set_options
4181 + modify the library (runtime/compile_options.c)! */
4183 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
4184 build_int_cst (integer_type_node
,
4185 gfc_option
.warn_std
));
4186 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
4187 build_int_cst (integer_type_node
,
4188 gfc_option
.allow_std
));
4189 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
4190 build_int_cst (integer_type_node
, pedantic
));
4191 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
4192 build_int_cst (integer_type_node
,
4193 gfc_option
.flag_dump_core
));
4194 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
4195 build_int_cst (integer_type_node
,
4196 gfc_option
.flag_backtrace
));
4197 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
4198 build_int_cst (integer_type_node
,
4199 gfc_option
.flag_sign_zero
));
4200 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
4201 build_int_cst (integer_type_node
,
4203 & GFC_RTCHECK_BOUNDS
)));
4204 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
4205 build_int_cst (integer_type_node
,
4206 gfc_option
.flag_range_check
));
4208 array_type
= build_array_type (integer_type_node
,
4209 build_index_type (build_int_cst (NULL_TREE
, 7)));
4210 array
= build_constructor (array_type
, v
);
4211 TREE_CONSTANT (array
) = 1;
4212 TREE_STATIC (array
) = 1;
4214 /* Create a static variable to hold the jump table. */
4215 var
= gfc_create_var (array_type
, "options");
4216 TREE_CONSTANT (var
) = 1;
4217 TREE_STATIC (var
) = 1;
4218 TREE_READONLY (var
) = 1;
4219 DECL_INITIAL (var
) = array
;
4220 var
= gfc_build_addr_expr (build_pointer_type (integer_type_node
), var
);
4222 tmp
= build_call_expr_loc (input_location
,
4223 gfor_fndecl_set_options
, 2,
4224 build_int_cst (integer_type_node
, 8), var
);
4225 gfc_add_expr_to_block (&body
, tmp
);
4228 /* If -ffpe-trap option was provided, add a call to set_fpe so that
4229 the library will raise a FPE when needed. */
4230 if (gfc_option
.fpe
!= 0)
4232 tmp
= build_call_expr_loc (input_location
,
4233 gfor_fndecl_set_fpe
, 1,
4234 build_int_cst (integer_type_node
,
4236 gfc_add_expr_to_block (&body
, tmp
);
4239 /* If this is the main program and an -fconvert option was provided,
4240 add a call to set_convert. */
4242 if (gfc_option
.convert
!= GFC_CONVERT_NATIVE
)
4244 tmp
= build_call_expr_loc (input_location
,
4245 gfor_fndecl_set_convert
, 1,
4246 build_int_cst (integer_type_node
,
4247 gfc_option
.convert
));
4248 gfc_add_expr_to_block (&body
, tmp
);
4251 /* If this is the main program and an -frecord-marker option was provided,
4252 add a call to set_record_marker. */
4254 if (gfc_option
.record_marker
!= 0)
4256 tmp
= build_call_expr_loc (input_location
,
4257 gfor_fndecl_set_record_marker
, 1,
4258 build_int_cst (integer_type_node
,
4259 gfc_option
.record_marker
));
4260 gfc_add_expr_to_block (&body
, tmp
);
4263 if (gfc_option
.max_subrecord_length
!= 0)
4265 tmp
= build_call_expr_loc (input_location
,
4266 gfor_fndecl_set_max_subrecord_length
, 1,
4267 build_int_cst (integer_type_node
,
4268 gfc_option
.max_subrecord_length
));
4269 gfc_add_expr_to_block (&body
, tmp
);
4272 /* Call MAIN__(). */
4273 tmp
= build_call_expr_loc (input_location
,
4275 gfc_add_expr_to_block (&body
, tmp
);
4277 /* Mark MAIN__ as used. */
4278 TREE_USED (fndecl
) = 1;
4281 tmp
= fold_build2 (MODIFY_EXPR
, integer_type_node
, DECL_RESULT (ftn_main
),
4282 build_int_cst (integer_type_node
, 0));
4283 tmp
= build1_v (RETURN_EXPR
, tmp
);
4284 gfc_add_expr_to_block (&body
, tmp
);
4287 DECL_SAVED_TREE (ftn_main
) = gfc_finish_block (&body
);
4290 /* Finish off this function and send it for code generation. */
4292 BLOCK_SUPERCONTEXT (DECL_INITIAL (ftn_main
)) = ftn_main
;
4294 DECL_SAVED_TREE (ftn_main
)
4295 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (ftn_main
),
4296 DECL_INITIAL (ftn_main
));
4298 /* Output the GENERIC tree. */
4299 dump_function (TDI_original
, ftn_main
);
4301 cgraph_finalize_function (ftn_main
, true);
4305 pop_function_context ();
4306 saved_function_decls
= saved_parent_function_decls
;
4308 current_function_decl
= old_context
;
4312 /* Get the result expression for a procedure. */
4315 get_proc_result (gfc_symbol
* sym
)
4317 if (sym
->attr
.subroutine
|| sym
== sym
->result
)
4319 if (current_fake_result_decl
!= NULL
)
4320 return TREE_VALUE (current_fake_result_decl
);
4325 return sym
->result
->backend_decl
;
4329 /* Generate an appropriate return-statement for a procedure. */
4332 gfc_generate_return (void)
4338 sym
= current_procedure_symbol
;
4339 fndecl
= sym
->backend_decl
;
4341 if (TREE_TYPE (DECL_RESULT (fndecl
)) == void_type_node
)
4345 result
= get_proc_result (sym
);
4347 /* Set the return value to the dummy result variable. The
4348 types may be different for scalar default REAL functions
4349 with -ff2c, therefore we have to convert. */
4350 if (result
!= NULL_TREE
)
4352 result
= convert (TREE_TYPE (DECL_RESULT (fndecl
)), result
);
4353 result
= fold_build2 (MODIFY_EXPR
, TREE_TYPE (result
),
4354 DECL_RESULT (fndecl
), result
);
4358 return build1_v (RETURN_EXPR
, result
);
4362 /* Generate code for a function. */
4365 gfc_generate_function_code (gfc_namespace
* ns
)
4371 stmtblock_t init
, cleanup
;
4373 gfc_wrapped_block try_block
;
4374 tree recurcheckvar
= NULL_TREE
;
4376 gfc_symbol
*previous_procedure_symbol
;
4380 sym
= ns
->proc_name
;
4381 previous_procedure_symbol
= current_procedure_symbol
;
4382 current_procedure_symbol
= sym
;
4384 /* Check that the frontend isn't still using this. */
4385 gcc_assert (sym
->tlink
== NULL
);
4388 /* Create the declaration for functions with global scope. */
4389 if (!sym
->backend_decl
)
4390 gfc_create_function_decl (ns
, false);
4392 fndecl
= sym
->backend_decl
;
4393 old_context
= current_function_decl
;
4397 push_function_context ();
4398 saved_parent_function_decls
= saved_function_decls
;
4399 saved_function_decls
= NULL_TREE
;
4402 trans_function_start (sym
);
4404 gfc_init_block (&init
);
4406 if (ns
->entries
&& ns
->proc_name
->ts
.type
== BT_CHARACTER
)
4408 /* Copy length backend_decls to all entry point result
4413 gfc_conv_const_charlen (ns
->proc_name
->ts
.u
.cl
);
4414 backend_decl
= ns
->proc_name
->result
->ts
.u
.cl
->backend_decl
;
4415 for (el
= ns
->entries
; el
; el
= el
->next
)
4416 el
->sym
->result
->ts
.u
.cl
->backend_decl
= backend_decl
;
4419 /* Translate COMMON blocks. */
4420 gfc_trans_common (ns
);
4422 /* Null the parent fake result declaration if this namespace is
4423 a module function or an external procedures. */
4424 if ((ns
->parent
&& ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
4425 || ns
->parent
== NULL
)
4426 parent_fake_result_decl
= NULL_TREE
;
4428 gfc_generate_contained_functions (ns
);
4430 nonlocal_dummy_decls
= NULL
;
4431 nonlocal_dummy_decl_pset
= NULL
;
4433 generate_local_vars (ns
);
4435 /* Keep the parent fake result declaration in module functions
4436 or external procedures. */
4437 if ((ns
->parent
&& ns
->parent
->proc_name
->attr
.flavor
== FL_MODULE
)
4438 || ns
->parent
== NULL
)
4439 current_fake_result_decl
= parent_fake_result_decl
;
4441 current_fake_result_decl
= NULL_TREE
;
4443 is_recursive
= sym
->attr
.recursive
4444 || (sym
->attr
.entry_master
4445 && sym
->ns
->entries
->sym
->attr
.recursive
);
4446 if ((gfc_option
.rtcheck
& GFC_RTCHECK_RECURSION
)
4448 && !gfc_option
.flag_recursive
)
4452 asprintf (&msg
, "Recursive call to nonrecursive procedure '%s'",
4454 recurcheckvar
= gfc_create_var (boolean_type_node
, "is_recursive");
4455 TREE_STATIC (recurcheckvar
) = 1;
4456 DECL_INITIAL (recurcheckvar
) = boolean_false_node
;
4457 gfc_add_expr_to_block (&init
, recurcheckvar
);
4458 gfc_trans_runtime_check (true, false, recurcheckvar
, &init
,
4459 &sym
->declared_at
, msg
);
4460 gfc_add_modify (&init
, recurcheckvar
, boolean_true_node
);
4464 /* Now generate the code for the body of this function. */
4465 gfc_init_block (&body
);
4467 if (TREE_TYPE (DECL_RESULT (fndecl
)) != void_type_node
4468 && sym
->attr
.subroutine
)
4470 tree alternate_return
;
4471 alternate_return
= gfc_get_fake_result_decl (sym
, 0);
4472 gfc_add_modify (&body
, alternate_return
, integer_zero_node
);
4477 /* Jump to the correct entry point. */
4478 tmp
= gfc_trans_entry_master_switch (ns
->entries
);
4479 gfc_add_expr_to_block (&body
, tmp
);
4482 /* If bounds-checking is enabled, generate code to check passed in actual
4483 arguments against the expected dummy argument attributes (e.g. string
4485 if ((gfc_option
.rtcheck
& GFC_RTCHECK_BOUNDS
) && !sym
->attr
.is_bind_c
)
4486 add_argument_checking (&body
, sym
);
4488 tmp
= gfc_trans_code (ns
->code
);
4489 gfc_add_expr_to_block (&body
, tmp
);
4491 if (TREE_TYPE (DECL_RESULT (fndecl
)) != void_type_node
)
4493 tree result
= get_proc_result (sym
);
4495 if (result
!= NULL_TREE
4496 && sym
->attr
.function
4497 && !sym
->attr
.pointer
)
4499 if (sym
->ts
.type
== BT_DERIVED
4500 && sym
->ts
.u
.derived
->attr
.alloc_comp
)
4502 rank
= sym
->as
? sym
->as
->rank
: 0;
4503 tmp
= gfc_nullify_alloc_comp (sym
->ts
.u
.derived
, result
, rank
);
4504 gfc_add_expr_to_block (&init
, tmp
);
4506 else if (sym
->attr
.allocatable
&& sym
->attr
.dimension
== 0)
4507 gfc_add_modify (&init
, result
, fold_convert (TREE_TYPE (result
),
4508 null_pointer_node
));
4511 if (result
== NULL_TREE
)
4513 /* TODO: move to the appropriate place in resolve.c. */
4514 if (warn_return_type
&& !sym
->attr
.referenced
&& sym
== sym
->result
)
4515 gfc_warning ("Return value of function '%s' at %L not set",
4516 sym
->name
, &sym
->declared_at
);
4518 TREE_NO_WARNING(sym
->backend_decl
) = 1;
4521 gfc_add_expr_to_block (&body
, gfc_generate_return ());
4524 gfc_init_block (&cleanup
);
4526 /* Reset recursion-check variable. */
4527 if ((gfc_option
.rtcheck
& GFC_RTCHECK_RECURSION
)
4529 && !gfc_option
.flag_openmp
4530 && recurcheckvar
!= NULL_TREE
)
4532 gfc_add_modify (&cleanup
, recurcheckvar
, boolean_false_node
);
4533 recurcheckvar
= NULL
;
4536 /* Finish the function body and add init and cleanup code. */
4537 tmp
= gfc_finish_block (&body
);
4538 gfc_start_wrapped_block (&try_block
, tmp
);
4539 /* Add code to create and cleanup arrays. */
4540 gfc_trans_deferred_vars (sym
, &try_block
);
4541 gfc_add_init_cleanup (&try_block
, gfc_finish_block (&init
),
4542 gfc_finish_block (&cleanup
));
4544 /* Add all the decls we created during processing. */
4545 decl
= saved_function_decls
;
4550 next
= DECL_CHAIN (decl
);
4551 DECL_CHAIN (decl
) = NULL_TREE
;
4555 saved_function_decls
= NULL_TREE
;
4557 DECL_SAVED_TREE (fndecl
) = gfc_finish_wrapped_block (&try_block
);
4560 /* Finish off this function and send it for code generation. */
4562 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
4564 DECL_SAVED_TREE (fndecl
)
4565 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (fndecl
),
4566 DECL_INITIAL (fndecl
));
4568 if (nonlocal_dummy_decls
)
4570 BLOCK_VARS (DECL_INITIAL (fndecl
))
4571 = chainon (BLOCK_VARS (DECL_INITIAL (fndecl
)), nonlocal_dummy_decls
);
4572 pointer_set_destroy (nonlocal_dummy_decl_pset
);
4573 nonlocal_dummy_decls
= NULL
;
4574 nonlocal_dummy_decl_pset
= NULL
;
4577 /* Output the GENERIC tree. */
4578 dump_function (TDI_original
, fndecl
);
4580 /* Store the end of the function, so that we get good line number
4581 info for the epilogue. */
4582 cfun
->function_end_locus
= input_location
;
4584 /* We're leaving the context of this function, so zap cfun.
4585 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
4586 tree_rest_of_compilation. */
4591 pop_function_context ();
4592 saved_function_decls
= saved_parent_function_decls
;
4594 current_function_decl
= old_context
;
4596 if (decl_function_context (fndecl
))
4597 /* Register this function with cgraph just far enough to get it
4598 added to our parent's nested function list. */
4599 (void) cgraph_node (fndecl
);
4601 cgraph_finalize_function (fndecl
, true);
4603 gfc_trans_use_stmts (ns
);
4604 gfc_traverse_ns (ns
, gfc_emit_parameter_debug_info
);
4606 if (sym
->attr
.is_main_program
)
4607 create_main_function (fndecl
);
4609 current_procedure_symbol
= previous_procedure_symbol
;
4614 gfc_generate_constructors (void)
4616 gcc_assert (gfc_static_ctors
== NULL_TREE
);
4624 if (gfc_static_ctors
== NULL_TREE
)
4627 fnname
= get_file_function_name ("I");
4628 type
= build_function_type_list (void_type_node
, NULL_TREE
);
4630 fndecl
= build_decl (input_location
,
4631 FUNCTION_DECL
, fnname
, type
);
4632 TREE_PUBLIC (fndecl
) = 1;
4634 decl
= build_decl (input_location
,
4635 RESULT_DECL
, NULL_TREE
, void_type_node
);
4636 DECL_ARTIFICIAL (decl
) = 1;
4637 DECL_IGNORED_P (decl
) = 1;
4638 DECL_CONTEXT (decl
) = fndecl
;
4639 DECL_RESULT (fndecl
) = decl
;
4643 current_function_decl
= fndecl
;
4645 rest_of_decl_compilation (fndecl
, 1, 0);
4647 make_decl_rtl (fndecl
);
4649 init_function_start (fndecl
);
4653 for (; gfc_static_ctors
; gfc_static_ctors
= TREE_CHAIN (gfc_static_ctors
))
4655 tmp
= build_call_expr_loc (input_location
,
4656 TREE_VALUE (gfc_static_ctors
), 0);
4657 DECL_SAVED_TREE (fndecl
) = build_stmt (input_location
, EXPR_STMT
, tmp
);
4663 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
4664 DECL_SAVED_TREE (fndecl
)
4665 = build3_v (BIND_EXPR
, decl
, DECL_SAVED_TREE (fndecl
),
4666 DECL_INITIAL (fndecl
));
4668 free_after_parsing (cfun
);
4669 free_after_compilation (cfun
);
4671 tree_rest_of_compilation (fndecl
);
4673 current_function_decl
= NULL_TREE
;
4677 /* Translates a BLOCK DATA program unit. This means emitting the
4678 commons contained therein plus their initializations. We also emit
4679 a globally visible symbol to make sure that each BLOCK DATA program
4680 unit remains unique. */
4683 gfc_generate_block_data (gfc_namespace
* ns
)
4688 /* Tell the backend the source location of the block data. */
4690 gfc_set_backend_locus (&ns
->proc_name
->declared_at
);
4692 gfc_set_backend_locus (&gfc_current_locus
);
4694 /* Process the DATA statements. */
4695 gfc_trans_common (ns
);
4697 /* Create a global symbol with the mane of the block data. This is to
4698 generate linker errors if the same name is used twice. It is never
4701 id
= gfc_sym_mangled_function_id (ns
->proc_name
);
4703 id
= get_identifier ("__BLOCK_DATA__");
4705 decl
= build_decl (input_location
,
4706 VAR_DECL
, id
, gfc_array_index_type
);
4707 TREE_PUBLIC (decl
) = 1;
4708 TREE_STATIC (decl
) = 1;
4709 DECL_IGNORED_P (decl
) = 1;
4712 rest_of_decl_compilation (decl
, 1, 0);
4716 /* Process the local variables of a BLOCK construct. */
4719 gfc_process_block_locals (gfc_namespace
* ns
)
4723 gcc_assert (saved_local_decls
== NULL_TREE
);
4724 generate_local_vars (ns
);
4726 decl
= saved_local_decls
;
4731 next
= DECL_CHAIN (decl
);
4732 DECL_CHAIN (decl
) = NULL_TREE
;
4736 saved_local_decls
= NULL_TREE
;
4740 #include "gt-fortran-trans-decl.h"