]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Backend function setup |
8d51f26f | 2 | Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, |
5d81ddd0 | 3 | 2011, 2012 |
66647d44 | 4 | Free Software Foundation, Inc. |
6de9cd9a DN |
5 | Contributed by Paul Brook |
6 | ||
9fc4d79b | 7 | This file is part of GCC. |
6de9cd9a | 8 | |
9fc4d79b TS |
9 | GCC is free software; you can redistribute it and/or modify it under |
10 | the terms of the GNU General Public License as published by the Free | |
d234d788 | 11 | Software Foundation; either version 3, or (at your option) any later |
9fc4d79b | 12 | version. |
6de9cd9a | 13 | |
9fc4d79b TS |
14 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
15 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 | for more details. | |
6de9cd9a DN |
18 | |
19 | You should have received a copy of the GNU General Public License | |
d234d788 NC |
20 | along with GCC; see the file COPYING3. If not see |
21 | <http://www.gnu.org/licenses/>. */ | |
6de9cd9a DN |
22 | |
23 | /* trans-decl.c -- Handling of backend function and variable decls, etc */ | |
24 | ||
25 | #include "config.h" | |
26 | #include "system.h" | |
27 | #include "coretypes.h" | |
d347d97e | 28 | #include "tm.h" |
6de9cd9a DN |
29 | #include "tree.h" |
30 | #include "tree-dump.h" | |
a48ba7e1 | 31 | #include "gimple.h" /* For create_tmp_var_raw. */ |
6de9cd9a | 32 | #include "ggc.h" |
c829d016 TB |
33 | #include "diagnostic-core.h" /* For internal_error. */ |
34 | #include "toplev.h" /* For announce_function. */ | |
6de9cd9a DN |
35 | #include "target.h" |
36 | #include "function.h" | |
6de9cd9a DN |
37 | #include "flags.h" |
38 | #include "cgraph.h" | |
a64f5186 | 39 | #include "debug.h" |
6de9cd9a | 40 | #include "gfortran.h" |
77f2a970 | 41 | #include "pointer-set.h" |
b7e75771 | 42 | #include "constructor.h" |
6de9cd9a DN |
43 | #include "trans.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" | |
49 | ||
50 | #define MAX_LABEL_VALUE 99999 | |
51 | ||
52 | ||
53 | /* Holds the result of the function if no result variable specified. */ | |
54 | ||
55 | static GTY(()) tree current_fake_result_decl; | |
5f20c93a | 56 | static GTY(()) tree parent_fake_result_decl; |
6de9cd9a | 57 | |
6de9cd9a DN |
58 | |
59 | /* Holds the variable DECLs for the current function. */ | |
60 | ||
417ab240 JJ |
61 | static GTY(()) tree saved_function_decls; |
62 | static GTY(()) tree saved_parent_function_decls; | |
6de9cd9a | 63 | |
77f2a970 JJ |
64 | static struct pointer_set_t *nonlocal_dummy_decl_pset; |
65 | static GTY(()) tree nonlocal_dummy_decls; | |
6de9cd9a | 66 | |
9abe5e56 DK |
67 | /* Holds the variable DECLs that are locals. */ |
68 | ||
69 | static GTY(()) tree saved_local_decls; | |
70 | ||
6de9cd9a DN |
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. */ | |
73 | ||
74 | static gfc_namespace *module_namespace; | |
75 | ||
d74d8807 DK |
76 | /* The currently processed procedure symbol. */ |
77 | static gfc_symbol* current_procedure_symbol = NULL; | |
78 | ||
6de9cd9a | 79 | |
b8ff4e88 TB |
80 | /* With -fcoarray=lib: For generating the registering call |
81 | of static coarrays. */ | |
82 | static bool has_coarray_vars; | |
83 | static stmtblock_t caf_init_block; | |
84 | ||
85 | ||
6de9cd9a DN |
86 | /* List of static constructor functions. */ |
87 | ||
88 | tree gfc_static_ctors; | |
89 | ||
90 | ||
91 | /* Function declarations for builtin library functions. */ | |
92 | ||
6de9cd9a DN |
93 | tree gfor_fndecl_pause_numeric; |
94 | tree gfor_fndecl_pause_string; | |
95 | tree gfor_fndecl_stop_numeric; | |
cea59ace | 96 | tree gfor_fndecl_stop_numeric_f08; |
6de9cd9a | 97 | tree gfor_fndecl_stop_string; |
6d1b0f92 | 98 | tree gfor_fndecl_error_stop_numeric; |
d0a4a61c | 99 | tree gfor_fndecl_error_stop_string; |
6de9cd9a | 100 | tree gfor_fndecl_runtime_error; |
f96d606f | 101 | tree gfor_fndecl_runtime_error_at; |
0d52899f | 102 | tree gfor_fndecl_runtime_warning_at; |
1529b8d9 | 103 | tree gfor_fndecl_os_error; |
f96d606f | 104 | tree gfor_fndecl_generate_error; |
092231a8 | 105 | tree gfor_fndecl_set_args; |
944b8b35 | 106 | tree gfor_fndecl_set_fpe; |
68d2e027 | 107 | tree gfor_fndecl_set_options; |
eaa90d25 | 108 | tree gfor_fndecl_set_convert; |
d67ab5ee | 109 | tree gfor_fndecl_set_record_marker; |
07b3bbf2 | 110 | tree gfor_fndecl_set_max_subrecord_length; |
35059811 FXC |
111 | tree gfor_fndecl_ctime; |
112 | tree gfor_fndecl_fdate; | |
25fc05eb | 113 | tree gfor_fndecl_ttynam; |
6de9cd9a DN |
114 | tree gfor_fndecl_in_pack; |
115 | tree gfor_fndecl_in_unpack; | |
116 | tree gfor_fndecl_associated; | |
117 | ||
118 | ||
60386f50 TB |
119 | /* Coarray run-time library function decls. */ |
120 | tree gfor_fndecl_caf_init; | |
121 | tree gfor_fndecl_caf_finalize; | |
b8ff4e88 | 122 | tree gfor_fndecl_caf_register; |
5d81ddd0 | 123 | tree gfor_fndecl_caf_deregister; |
60386f50 TB |
124 | tree gfor_fndecl_caf_critical; |
125 | tree gfor_fndecl_caf_end_critical; | |
126 | tree gfor_fndecl_caf_sync_all; | |
127 | tree gfor_fndecl_caf_sync_images; | |
128 | tree gfor_fndecl_caf_error_stop; | |
129 | tree gfor_fndecl_caf_error_stop_str; | |
130 | ||
131 | /* Coarray global variables for num_images/this_image. */ | |
132 | ||
133 | tree gfort_gvar_caf_num_images; | |
134 | tree gfort_gvar_caf_this_image; | |
135 | ||
136 | ||
6de9cd9a DN |
137 | /* Math functions. Many other math functions are handled in |
138 | trans-intrinsic.c. */ | |
139 | ||
644cb69f | 140 | gfc_powdecl_list gfor_fndecl_math_powi[4][3]; |
6de9cd9a DN |
141 | tree gfor_fndecl_math_ishftc4; |
142 | tree gfor_fndecl_math_ishftc8; | |
644cb69f | 143 | tree gfor_fndecl_math_ishftc16; |
6de9cd9a DN |
144 | |
145 | ||
146 | /* String functions. */ | |
147 | ||
6de9cd9a DN |
148 | tree gfor_fndecl_compare_string; |
149 | tree gfor_fndecl_concat_string; | |
150 | tree gfor_fndecl_string_len_trim; | |
151 | tree gfor_fndecl_string_index; | |
152 | tree gfor_fndecl_string_scan; | |
153 | tree gfor_fndecl_string_verify; | |
154 | tree gfor_fndecl_string_trim; | |
2263c775 | 155 | tree gfor_fndecl_string_minmax; |
6de9cd9a DN |
156 | tree gfor_fndecl_adjustl; |
157 | tree gfor_fndecl_adjustr; | |
d393bbd7 | 158 | tree gfor_fndecl_select_string; |
374929b2 FXC |
159 | tree gfor_fndecl_compare_string_char4; |
160 | tree gfor_fndecl_concat_string_char4; | |
161 | tree gfor_fndecl_string_len_trim_char4; | |
162 | tree gfor_fndecl_string_index_char4; | |
163 | tree gfor_fndecl_string_scan_char4; | |
164 | tree gfor_fndecl_string_verify_char4; | |
165 | tree gfor_fndecl_string_trim_char4; | |
166 | tree gfor_fndecl_string_minmax_char4; | |
167 | tree gfor_fndecl_adjustl_char4; | |
168 | tree gfor_fndecl_adjustr_char4; | |
d393bbd7 FXC |
169 | tree gfor_fndecl_select_string_char4; |
170 | ||
171 | ||
172 | /* Conversion between character kinds. */ | |
173 | tree gfor_fndecl_convert_char1_to_char4; | |
174 | tree gfor_fndecl_convert_char4_to_char1; | |
6de9cd9a DN |
175 | |
176 | ||
177 | /* Other misc. runtime library functions. */ | |
6de9cd9a DN |
178 | tree gfor_fndecl_size0; |
179 | tree gfor_fndecl_size1; | |
b41b2534 | 180 | tree gfor_fndecl_iargc; |
6de9cd9a | 181 | |
a39fafac FXC |
182 | /* Intrinsic functions implemented in Fortran. */ |
183 | tree gfor_fndecl_sc_kind; | |
6de9cd9a DN |
184 | tree gfor_fndecl_si_kind; |
185 | tree gfor_fndecl_sr_kind; | |
186 | ||
5a0aad31 FXC |
187 | /* BLAS gemm functions. */ |
188 | tree gfor_fndecl_sgemm; | |
189 | tree gfor_fndecl_dgemm; | |
190 | tree gfor_fndecl_cgemm; | |
191 | tree gfor_fndecl_zgemm; | |
192 | ||
6de9cd9a DN |
193 | |
194 | static void | |
195 | gfc_add_decl_to_parent_function (tree decl) | |
196 | { | |
6e45f57b | 197 | gcc_assert (decl); |
6de9cd9a DN |
198 | DECL_CONTEXT (decl) = DECL_CONTEXT (current_function_decl); |
199 | DECL_NONLOCAL (decl) = 1; | |
910ad8de | 200 | DECL_CHAIN (decl) = saved_parent_function_decls; |
6de9cd9a DN |
201 | saved_parent_function_decls = decl; |
202 | } | |
203 | ||
204 | void | |
205 | gfc_add_decl_to_function (tree decl) | |
206 | { | |
6e45f57b | 207 | gcc_assert (decl); |
6de9cd9a DN |
208 | TREE_USED (decl) = 1; |
209 | DECL_CONTEXT (decl) = current_function_decl; | |
910ad8de | 210 | DECL_CHAIN (decl) = saved_function_decls; |
6de9cd9a DN |
211 | saved_function_decls = decl; |
212 | } | |
213 | ||
9abe5e56 DK |
214 | static void |
215 | add_decl_as_local (tree decl) | |
216 | { | |
217 | gcc_assert (decl); | |
218 | TREE_USED (decl) = 1; | |
219 | DECL_CONTEXT (decl) = current_function_decl; | |
910ad8de | 220 | DECL_CHAIN (decl) = saved_local_decls; |
9abe5e56 DK |
221 | saved_local_decls = decl; |
222 | } | |
223 | ||
6de9cd9a | 224 | |
c006df4e SB |
225 | /* Build a backend label declaration. Set TREE_USED for named labels. |
226 | The context of the label is always the current_function_decl. All | |
227 | labels are marked artificial. */ | |
6de9cd9a DN |
228 | |
229 | tree | |
230 | gfc_build_label_decl (tree label_id) | |
231 | { | |
232 | /* 2^32 temporaries should be enough. */ | |
233 | static unsigned int tmp_num = 1; | |
234 | tree label_decl; | |
235 | char *label_name; | |
236 | ||
237 | if (label_id == NULL_TREE) | |
238 | { | |
239 | /* Build an internal label name. */ | |
240 | ASM_FORMAT_PRIVATE_NAME (label_name, "L", tmp_num++); | |
241 | label_id = get_identifier (label_name); | |
242 | } | |
243 | else | |
244 | label_name = NULL; | |
245 | ||
246 | /* Build the LABEL_DECL node. Labels have no type. */ | |
c2255bc4 AH |
247 | label_decl = build_decl (input_location, |
248 | LABEL_DECL, label_id, void_type_node); | |
6de9cd9a DN |
249 | DECL_CONTEXT (label_decl) = current_function_decl; |
250 | DECL_MODE (label_decl) = VOIDmode; | |
251 | ||
c006df4e SB |
252 | /* We always define the label as used, even if the original source |
253 | file never references the label. We don't want all kinds of | |
254 | spurious warnings for old-style Fortran code with too many | |
255 | labels. */ | |
256 | TREE_USED (label_decl) = 1; | |
6de9cd9a | 257 | |
c006df4e | 258 | DECL_ARTIFICIAL (label_decl) = 1; |
6de9cd9a DN |
259 | return label_decl; |
260 | } | |
261 | ||
262 | ||
c8cc8542 PB |
263 | /* Set the backend source location of a decl. */ |
264 | ||
265 | void | |
266 | gfc_set_decl_location (tree decl, locus * loc) | |
267 | { | |
c8cc8542 | 268 | DECL_SOURCE_LOCATION (decl) = loc->lb->location; |
c8cc8542 PB |
269 | } |
270 | ||
271 | ||
6de9cd9a DN |
272 | /* Return the backend label declaration for a given label structure, |
273 | or create it if it doesn't exist yet. */ | |
274 | ||
275 | tree | |
276 | gfc_get_label_decl (gfc_st_label * lp) | |
277 | { | |
6de9cd9a DN |
278 | if (lp->backend_decl) |
279 | return lp->backend_decl; | |
280 | else | |
281 | { | |
282 | char label_name[GFC_MAX_SYMBOL_LEN + 1]; | |
283 | tree label_decl; | |
284 | ||
285 | /* Validate the label declaration from the front end. */ | |
6e45f57b | 286 | gcc_assert (lp != NULL && lp->value <= MAX_LABEL_VALUE); |
6de9cd9a DN |
287 | |
288 | /* Build a mangled name for the label. */ | |
289 | sprintf (label_name, "__label_%.6d", lp->value); | |
290 | ||
291 | /* Build the LABEL_DECL node. */ | |
292 | label_decl = gfc_build_label_decl (get_identifier (label_name)); | |
293 | ||
294 | /* Tell the debugger where the label came from. */ | |
f8d0aee5 | 295 | if (lp->value <= MAX_LABEL_VALUE) /* An internal label. */ |
c8cc8542 | 296 | gfc_set_decl_location (label_decl, &lp->where); |
6de9cd9a DN |
297 | else |
298 | DECL_ARTIFICIAL (label_decl) = 1; | |
299 | ||
300 | /* Store the label in the label list and return the LABEL_DECL. */ | |
301 | lp->backend_decl = label_decl; | |
302 | return label_decl; | |
303 | } | |
304 | } | |
305 | ||
306 | ||
307 | /* Convert a gfc_symbol to an identifier of the same name. */ | |
308 | ||
309 | static tree | |
310 | gfc_sym_identifier (gfc_symbol * sym) | |
311 | { | |
a7ad6c2d TB |
312 | if (sym->attr.is_main_program && strcmp (sym->name, "main") == 0) |
313 | return (get_identifier ("MAIN__")); | |
314 | else | |
315 | return (get_identifier (sym->name)); | |
6de9cd9a DN |
316 | } |
317 | ||
318 | ||
319 | /* Construct mangled name from symbol name. */ | |
320 | ||
321 | static tree | |
322 | gfc_sym_mangled_identifier (gfc_symbol * sym) | |
323 | { | |
324 | char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1]; | |
325 | ||
a8b3b0b6 CR |
326 | /* Prevent the mangling of identifiers that have an assigned |
327 | binding label (mainly those that are bind(c)). */ | |
62603fae JB |
328 | if (sym->attr.is_bind_c == 1 && sym->binding_label) |
329 | return get_identifier (sym->binding_label); | |
a8b3b0b6 | 330 | |
cb9e4f55 | 331 | if (sym->module == NULL) |
6de9cd9a DN |
332 | return gfc_sym_identifier (sym); |
333 | else | |
334 | { | |
9998ef84 | 335 | snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name); |
6de9cd9a DN |
336 | return get_identifier (name); |
337 | } | |
338 | } | |
339 | ||
340 | ||
341 | /* Construct mangled function name from symbol name. */ | |
342 | ||
343 | static tree | |
344 | gfc_sym_mangled_function_id (gfc_symbol * sym) | |
345 | { | |
346 | int has_underscore; | |
347 | char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1]; | |
348 | ||
a8b3b0b6 CR |
349 | /* It may be possible to simply use the binding label if it's |
350 | provided, and remove the other checks. Then we could use it | |
351 | for other things if we wished. */ | |
352 | if ((sym->attr.is_bind_c == 1 || sym->attr.is_iso_c == 1) && | |
62603fae | 353 | sym->binding_label) |
a8b3b0b6 CR |
354 | /* use the binding label rather than the mangled name */ |
355 | return get_identifier (sym->binding_label); | |
356 | ||
cb9e4f55 | 357 | if (sym->module == NULL || sym->attr.proc == PROC_EXTERNAL |
c89686a8 PT |
358 | || (sym->module != NULL && (sym->attr.external |
359 | || sym->attr.if_source == IFSRC_IFBODY))) | |
6de9cd9a | 360 | { |
ecf24057 FXC |
361 | /* Main program is mangled into MAIN__. */ |
362 | if (sym->attr.is_main_program) | |
363 | return get_identifier ("MAIN__"); | |
364 | ||
365 | /* Intrinsic procedures are never mangled. */ | |
366 | if (sym->attr.proc == PROC_INTRINSIC) | |
6de9cd9a DN |
367 | return get_identifier (sym->name); |
368 | ||
369 | if (gfc_option.flag_underscoring) | |
370 | { | |
371 | has_underscore = strchr (sym->name, '_') != 0; | |
372 | if (gfc_option.flag_second_underscore && has_underscore) | |
373 | snprintf (name, sizeof name, "%s__", sym->name); | |
374 | else | |
375 | snprintf (name, sizeof name, "%s_", sym->name); | |
376 | return get_identifier (name); | |
377 | } | |
378 | else | |
379 | return get_identifier (sym->name); | |
380 | } | |
381 | else | |
382 | { | |
9998ef84 | 383 | snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name); |
6de9cd9a DN |
384 | return get_identifier (name); |
385 | } | |
386 | } | |
387 | ||
388 | ||
43ce5e52 FXC |
389 | void |
390 | gfc_set_decl_assembler_name (tree decl, tree name) | |
391 | { | |
392 | tree target_mangled = targetm.mangle_decl_assembler_name (decl, name); | |
393 | SET_DECL_ASSEMBLER_NAME (decl, target_mangled); | |
394 | } | |
395 | ||
396 | ||
bae88af6 TS |
397 | /* Returns true if a variable of specified size should go on the stack. */ |
398 | ||
399 | int | |
400 | gfc_can_put_var_on_stack (tree size) | |
401 | { | |
402 | unsigned HOST_WIDE_INT low; | |
403 | ||
404 | if (!INTEGER_CST_P (size)) | |
405 | return 0; | |
406 | ||
407 | if (gfc_option.flag_max_stack_var_size < 0) | |
408 | return 1; | |
409 | ||
410 | if (TREE_INT_CST_HIGH (size) != 0) | |
411 | return 0; | |
412 | ||
413 | low = TREE_INT_CST_LOW (size); | |
414 | if (low > (unsigned HOST_WIDE_INT) gfc_option.flag_max_stack_var_size) | |
415 | return 0; | |
416 | ||
417 | /* TODO: Set a per-function stack size limit. */ | |
418 | ||
419 | return 1; | |
420 | } | |
421 | ||
422 | ||
b122dc6a JJ |
423 | /* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to |
424 | an expression involving its corresponding pointer. There are | |
425 | 2 cases; one for variable size arrays, and one for everything else, | |
426 | because variable-sized arrays require one fewer level of | |
427 | indirection. */ | |
428 | ||
429 | static void | |
430 | gfc_finish_cray_pointee (tree decl, gfc_symbol *sym) | |
431 | { | |
432 | tree ptr_decl = gfc_get_symbol_decl (sym->cp_pointer); | |
433 | tree value; | |
434 | ||
435 | /* Parameters need to be dereferenced. */ | |
436 | if (sym->cp_pointer->attr.dummy) | |
db3927fb AH |
437 | ptr_decl = build_fold_indirect_ref_loc (input_location, |
438 | ptr_decl); | |
b122dc6a JJ |
439 | |
440 | /* Check to see if we're dealing with a variable-sized array. */ | |
441 | if (sym->attr.dimension | |
442 | && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE) | |
443 | { | |
831d7813 | 444 | /* These decls will be dereferenced later, so we don't dereference |
b122dc6a JJ |
445 | them here. */ |
446 | value = convert (TREE_TYPE (decl), ptr_decl); | |
447 | } | |
448 | else | |
449 | { | |
450 | ptr_decl = convert (build_pointer_type (TREE_TYPE (decl)), | |
451 | ptr_decl); | |
db3927fb AH |
452 | value = build_fold_indirect_ref_loc (input_location, |
453 | ptr_decl); | |
b122dc6a JJ |
454 | } |
455 | ||
456 | SET_DECL_VALUE_EXPR (decl, value); | |
457 | DECL_HAS_VALUE_EXPR_P (decl) = 1; | |
6c7a4dfd | 458 | GFC_DECL_CRAY_POINTEE (decl) = 1; |
b122dc6a JJ |
459 | } |
460 | ||
461 | ||
faf28b3a | 462 | /* Finish processing of a declaration without an initial value. */ |
6de9cd9a DN |
463 | |
464 | static void | |
faf28b3a | 465 | gfc_finish_decl (tree decl) |
6de9cd9a | 466 | { |
faf28b3a TS |
467 | gcc_assert (TREE_CODE (decl) == PARM_DECL |
468 | || DECL_INITIAL (decl) == NULL_TREE); | |
6de9cd9a | 469 | |
faf28b3a TS |
470 | if (TREE_CODE (decl) != VAR_DECL) |
471 | return; | |
6de9cd9a | 472 | |
faf28b3a TS |
473 | if (DECL_SIZE (decl) == NULL_TREE |
474 | && TYPE_SIZE (TREE_TYPE (decl)) != NULL_TREE) | |
475 | layout_decl (decl, 0); | |
476 | ||
477 | /* A few consistency checks. */ | |
478 | /* A static variable with an incomplete type is an error if it is | |
479 | initialized. Also if it is not file scope. Otherwise, let it | |
480 | through, but if it is not `extern' then it may cause an error | |
481 | message later. */ | |
482 | /* An automatic variable with an incomplete type is an error. */ | |
483 | ||
484 | /* We should know the storage size. */ | |
485 | gcc_assert (DECL_SIZE (decl) != NULL_TREE | |
486 | || (TREE_STATIC (decl) | |
487 | ? (!DECL_INITIAL (decl) || !DECL_CONTEXT (decl)) | |
488 | : DECL_EXTERNAL (decl))); | |
489 | ||
490 | /* The storage size should be constant. */ | |
491 | gcc_assert ((!DECL_EXTERNAL (decl) && !TREE_STATIC (decl)) | |
492 | || !DECL_SIZE (decl) | |
493 | || TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST); | |
6de9cd9a DN |
494 | } |
495 | ||
496 | ||
497 | /* Apply symbol attributes to a variable, and add it to the function scope. */ | |
498 | ||
499 | static void | |
500 | gfc_finish_var_decl (tree decl, gfc_symbol * sym) | |
501 | { | |
7b901ac4 | 502 | tree new_type; |
f8d0aee5 | 503 | /* TREE_ADDRESSABLE means the address of this variable is actually needed. |
6de9cd9a DN |
504 | This is the equivalent of the TARGET variables. |
505 | We also need to set this if the variable is passed by reference in a | |
506 | CALL statement. */ | |
83d890b9 | 507 | |
b122dc6a | 508 | /* Set DECL_VALUE_EXPR for Cray Pointees. */ |
83d890b9 | 509 | if (sym->attr.cray_pointee) |
b122dc6a | 510 | gfc_finish_cray_pointee (decl, sym); |
83d890b9 | 511 | |
6de9cd9a DN |
512 | if (sym->attr.target) |
513 | TREE_ADDRESSABLE (decl) = 1; | |
514 | /* If it wasn't used we wouldn't be getting it. */ | |
515 | TREE_USED (decl) = 1; | |
516 | ||
1e4b1376 TB |
517 | if (sym->attr.flavor == FL_PARAMETER |
518 | && (sym->attr.dimension || sym->ts.type == BT_DERIVED)) | |
519 | TREE_READONLY (decl) = 1; | |
520 | ||
6de9cd9a DN |
521 | /* Chain this decl to the pending declarations. Don't do pushdecl() |
522 | because this would add them to the current scope rather than the | |
523 | function scope. */ | |
524 | if (current_function_decl != NULL_TREE) | |
525 | { | |
d48734ef | 526 | if (sym->ns->proc_name->backend_decl == current_function_decl |
9abe5e56 | 527 | || sym->result == sym) |
6de9cd9a | 528 | gfc_add_decl_to_function (decl); |
9abe5e56 DK |
529 | else if (sym->ns->proc_name->attr.flavor == FL_LABEL) |
530 | /* This is a BLOCK construct. */ | |
531 | add_decl_as_local (decl); | |
6de9cd9a DN |
532 | else |
533 | gfc_add_decl_to_parent_function (decl); | |
534 | } | |
535 | ||
b122dc6a JJ |
536 | if (sym->attr.cray_pointee) |
537 | return; | |
538 | ||
5af6fa0b | 539 | if(sym->attr.is_bind_c == 1 && sym->binding_label) |
a8b3b0b6 CR |
540 | { |
541 | /* We need to put variables that are bind(c) into the common | |
542 | segment of the object file, because this is what C would do. | |
543 | gfortran would typically put them in either the BSS or | |
544 | initialized data segments, and only mark them as common if | |
545 | they were part of common blocks. However, if they are not put | |
93c71688 | 546 | into common space, then C cannot initialize global Fortran |
a8b3b0b6 CR |
547 | variables that it interoperates with and the draft says that |
548 | either Fortran or C should be able to initialize it (but not | |
549 | both, of course.) (J3/04-007, section 15.3). */ | |
550 | TREE_PUBLIC(decl) = 1; | |
551 | DECL_COMMON(decl) = 1; | |
552 | } | |
553 | ||
6de9cd9a DN |
554 | /* If a variable is USE associated, it's always external. */ |
555 | if (sym->attr.use_assoc) | |
556 | { | |
557 | DECL_EXTERNAL (decl) = 1; | |
558 | TREE_PUBLIC (decl) = 1; | |
559 | } | |
cb9e4f55 | 560 | else if (sym->module && !sym->attr.result && !sym->attr.dummy) |
6de9cd9a | 561 | { |
adf3ed3f | 562 | /* TODO: Don't set sym->module for result or dummy variables. */ |
d48734ef | 563 | gcc_assert (current_function_decl == NULL_TREE || sym->result == sym); |
6de9cd9a | 564 | /* This is the declaration of a module variable. */ |
ed4639a9 | 565 | if (sym->attr.access != ACCESS_PRIVATE || sym->attr.public_used) |
5af6fa0b | 566 | TREE_PUBLIC (decl) = 1; |
6de9cd9a DN |
567 | TREE_STATIC (decl) = 1; |
568 | } | |
569 | ||
b7b184a8 PT |
570 | /* Derived types are a bit peculiar because of the possibility of |
571 | a default initializer; this must be applied each time the variable | |
572 | comes into scope it therefore need not be static. These variables | |
573 | are SAVE_NONE but have an initializer. Otherwise explicitly | |
df2fba9e | 574 | initialized variables are SAVE_IMPLICIT and explicitly saved are |
b7b184a8 PT |
575 | SAVE_EXPLICIT. */ |
576 | if (!sym->attr.use_assoc | |
577 | && (sym->attr.save != SAVE_NONE || sym->attr.data | |
b8ff4e88 TB |
578 | || (sym->value && sym->ns->proc_name->attr.is_main_program) |
579 | || (gfc_option.coarray == GFC_FCOARRAY_LIB | |
580 | && sym->attr.codimension && !sym->attr.allocatable))) | |
6de9cd9a | 581 | TREE_STATIC (decl) = 1; |
775e6c3a TB |
582 | |
583 | if (sym->attr.volatile_) | |
584 | { | |
775e6c3a | 585 | TREE_THIS_VOLATILE (decl) = 1; |
c28d1d9b | 586 | TREE_SIDE_EFFECTS (decl) = 1; |
7b901ac4 KG |
587 | new_type = build_qualified_type (TREE_TYPE (decl), TYPE_QUAL_VOLATILE); |
588 | TREE_TYPE (decl) = new_type; | |
775e6c3a TB |
589 | } |
590 | ||
6de9cd9a DN |
591 | /* Keep variables larger than max-stack-var-size off stack. */ |
592 | if (!sym->ns->proc_name->attr.recursive | |
593 | && INTEGER_CST_P (DECL_SIZE_UNIT (decl)) | |
9eccb94d JJ |
594 | && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl)) |
595 | /* Put variable length auto array pointers always into stack. */ | |
596 | && (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE | |
597 | || sym->attr.dimension == 0 | |
598 | || sym->as->type != AS_EXPLICIT | |
599 | || sym->attr.pointer | |
600 | || sym->attr.allocatable) | |
601 | && !DECL_ARTIFICIAL (decl)) | |
6de9cd9a | 602 | TREE_STATIC (decl) = 1; |
6c7a4dfd JJ |
603 | |
604 | /* Handle threadprivate variables. */ | |
8893239d | 605 | if (sym->attr.threadprivate |
6c7a4dfd JJ |
606 | && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))) |
607 | DECL_TLS_MODEL (decl) = decl_default_tls_model (decl); | |
10174ddf MM |
608 | |
609 | if (!sym->attr.target | |
610 | && !sym->attr.pointer | |
b3aefde2 | 611 | && !sym->attr.cray_pointee |
9b87db3c MM |
612 | && !sym->attr.proc_pointer) |
613 | DECL_RESTRICTED_P (decl) = 1; | |
6de9cd9a DN |
614 | } |
615 | ||
616 | ||
617 | /* Allocate the lang-specific part of a decl. */ | |
618 | ||
619 | void | |
620 | gfc_allocate_lang_decl (tree decl) | |
621 | { | |
a9429e29 LB |
622 | DECL_LANG_SPECIFIC (decl) = ggc_alloc_cleared_lang_decl(sizeof |
623 | (struct lang_decl)); | |
6de9cd9a DN |
624 | } |
625 | ||
626 | /* Remember a symbol to generate initialization/cleanup code at function | |
627 | entry/exit. */ | |
628 | ||
629 | static void | |
630 | gfc_defer_symbol_init (gfc_symbol * sym) | |
631 | { | |
632 | gfc_symbol *p; | |
633 | gfc_symbol *last; | |
634 | gfc_symbol *head; | |
635 | ||
636 | /* Don't add a symbol twice. */ | |
637 | if (sym->tlink) | |
638 | return; | |
639 | ||
640 | last = head = sym->ns->proc_name; | |
641 | p = last->tlink; | |
642 | ||
643 | /* Make sure that setup code for dummy variables which are used in the | |
644 | setup of other variables is generated first. */ | |
645 | if (sym->attr.dummy) | |
646 | { | |
647 | /* Find the first dummy arg seen after us, or the first non-dummy arg. | |
648 | This is a circular list, so don't go past the head. */ | |
649 | while (p != head | |
650 | && (!p->attr.dummy || p->dummy_order > sym->dummy_order)) | |
651 | { | |
652 | last = p; | |
653 | p = p->tlink; | |
654 | } | |
655 | } | |
656 | /* Insert in between last and p. */ | |
657 | last->tlink = sym; | |
658 | sym->tlink = p; | |
659 | } | |
660 | ||
661 | ||
0101807c PT |
662 | /* Used in gfc_get_symbol_decl and gfc_get_derived_type to obtain the |
663 | backend_decl for a module symbol, if it all ready exists. If the | |
664 | module gsymbol does not exist, it is created. If the symbol does | |
665 | not exist, it is added to the gsymbol namespace. Returns true if | |
666 | an existing backend_decl is found. */ | |
667 | ||
668 | bool | |
669 | gfc_get_module_backend_decl (gfc_symbol *sym) | |
670 | { | |
671 | gfc_gsymbol *gsym; | |
672 | gfc_symbol *s; | |
673 | gfc_symtree *st; | |
674 | ||
675 | gsym = gfc_find_gsymbol (gfc_gsym_root, sym->module); | |
676 | ||
677 | if (!gsym || (gsym->ns && gsym->type == GSYM_MODULE)) | |
678 | { | |
679 | st = NULL; | |
680 | s = NULL; | |
681 | ||
682 | if (gsym) | |
683 | gfc_find_symbol (sym->name, gsym->ns, 0, &s); | |
684 | ||
685 | if (!s) | |
686 | { | |
687 | if (!gsym) | |
688 | { | |
689 | gsym = gfc_get_gsymbol (sym->module); | |
690 | gsym->type = GSYM_MODULE; | |
691 | gsym->ns = gfc_get_namespace (NULL, 0); | |
692 | } | |
693 | ||
694 | st = gfc_new_symtree (&gsym->ns->sym_root, sym->name); | |
695 | st->n.sym = sym; | |
696 | sym->refs++; | |
697 | } | |
698 | else if (sym->attr.flavor == FL_DERIVED) | |
699 | { | |
c3f34952 TB |
700 | if (s && s->attr.flavor == FL_PROCEDURE) |
701 | { | |
702 | gfc_interface *intr; | |
703 | gcc_assert (s->attr.generic); | |
704 | for (intr = s->generic; intr; intr = intr->next) | |
705 | if (intr->sym->attr.flavor == FL_DERIVED) | |
706 | { | |
707 | s = intr->sym; | |
708 | break; | |
709 | } | |
710 | } | |
711 | ||
0101807c PT |
712 | if (!s->backend_decl) |
713 | s->backend_decl = gfc_get_derived_type (s); | |
714 | gfc_copy_dt_decls_ifequal (s, sym, true); | |
715 | return true; | |
716 | } | |
717 | else if (s->backend_decl) | |
718 | { | |
f29bda83 | 719 | if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS) |
0101807c PT |
720 | gfc_copy_dt_decls_ifequal (s->ts.u.derived, sym->ts.u.derived, |
721 | true); | |
722 | else if (sym->ts.type == BT_CHARACTER) | |
723 | sym->ts.u.cl->backend_decl = s->ts.u.cl->backend_decl; | |
724 | sym->backend_decl = s->backend_decl; | |
725 | return true; | |
726 | } | |
727 | } | |
728 | return false; | |
729 | } | |
730 | ||
731 | ||
6de9cd9a DN |
732 | /* Create an array index type variable with function scope. */ |
733 | ||
734 | static tree | |
735 | create_index_var (const char * pfx, int nest) | |
736 | { | |
737 | tree decl; | |
738 | ||
739 | decl = gfc_create_var_np (gfc_array_index_type, pfx); | |
740 | if (nest) | |
741 | gfc_add_decl_to_parent_function (decl); | |
742 | else | |
743 | gfc_add_decl_to_function (decl); | |
744 | return decl; | |
745 | } | |
746 | ||
747 | ||
748 | /* Create variables to hold all the non-constant bits of info for a | |
749 | descriptorless array. Remember these in the lang-specific part of the | |
750 | type. */ | |
751 | ||
752 | static void | |
753 | gfc_build_qualified_array (tree decl, gfc_symbol * sym) | |
754 | { | |
755 | tree type; | |
756 | int dim; | |
757 | int nest; | |
52bf62f9 | 758 | gfc_namespace* procns; |
6de9cd9a DN |
759 | |
760 | type = TREE_TYPE (decl); | |
761 | ||
762 | /* We just use the descriptor, if there is one. */ | |
763 | if (GFC_DESCRIPTOR_TYPE_P (type)) | |
764 | return; | |
765 | ||
6e45f57b | 766 | gcc_assert (GFC_ARRAY_TYPE_P (type)); |
52bf62f9 DK |
767 | procns = gfc_find_proc_namespace (sym->ns); |
768 | nest = (procns->proc_name->backend_decl != current_function_decl) | |
6de9cd9a DN |
769 | && !sym->attr.contained; |
770 | ||
b8ff4e88 | 771 | if (sym->attr.codimension && gfc_option.coarray == GFC_FCOARRAY_LIB |
aa13dc3c | 772 | && sym->as->type != AS_ASSUMED_SHAPE |
b8ff4e88 TB |
773 | && GFC_TYPE_ARRAY_CAF_TOKEN (type) == NULL_TREE) |
774 | { | |
775 | tree token; | |
776 | ||
15e2b595 TB |
777 | token = gfc_create_var_np (build_qualified_type (pvoid_type_node, |
778 | TYPE_QUAL_RESTRICT), | |
779 | "caf_token"); | |
b8ff4e88 TB |
780 | GFC_TYPE_ARRAY_CAF_TOKEN (type) = token; |
781 | DECL_ARTIFICIAL (token) = 1; | |
782 | TREE_STATIC (token) = 1; | |
783 | gfc_add_decl_to_function (token); | |
784 | } | |
785 | ||
6de9cd9a DN |
786 | for (dim = 0; dim < GFC_TYPE_ARRAY_RANK (type); dim++) |
787 | { | |
788 | if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE) | |
01306727 FXC |
789 | { |
790 | GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest); | |
791 | TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type, dim)) = 1; | |
792 | } | |
aa9c57ec | 793 | /* Don't try to use the unknown bound for assumed shape arrays. */ |
6de9cd9a DN |
794 | if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE |
795 | && (sym->as->type != AS_ASSUMED_SIZE | |
796 | || dim < GFC_TYPE_ARRAY_RANK (type) - 1)) | |
01306727 FXC |
797 | { |
798 | GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest); | |
799 | TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type, dim)) = 1; | |
800 | } | |
6de9cd9a DN |
801 | |
802 | if (GFC_TYPE_ARRAY_STRIDE (type, dim) == NULL_TREE) | |
01306727 FXC |
803 | { |
804 | GFC_TYPE_ARRAY_STRIDE (type, dim) = create_index_var ("stride", nest); | |
805 | TREE_NO_WARNING (GFC_TYPE_ARRAY_STRIDE (type, dim)) = 1; | |
806 | } | |
6de9cd9a | 807 | } |
a3935ffc TB |
808 | for (dim = GFC_TYPE_ARRAY_RANK (type); |
809 | dim < GFC_TYPE_ARRAY_RANK (type) + GFC_TYPE_ARRAY_CORANK (type); dim++) | |
810 | { | |
811 | if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE) | |
812 | { | |
813 | GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest); | |
814 | TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type, dim)) = 1; | |
815 | } | |
816 | /* Don't try to use the unknown ubound for the last coarray dimension. */ | |
817 | if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE | |
818 | && dim < GFC_TYPE_ARRAY_RANK (type) + GFC_TYPE_ARRAY_CORANK (type) - 1) | |
819 | { | |
820 | GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest); | |
821 | TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type, dim)) = 1; | |
822 | } | |
823 | } | |
6de9cd9a DN |
824 | if (GFC_TYPE_ARRAY_OFFSET (type) == NULL_TREE) |
825 | { | |
826 | GFC_TYPE_ARRAY_OFFSET (type) = gfc_create_var_np (gfc_array_index_type, | |
827 | "offset"); | |
01306727 FXC |
828 | TREE_NO_WARNING (GFC_TYPE_ARRAY_OFFSET (type)) = 1; |
829 | ||
6de9cd9a DN |
830 | if (nest) |
831 | gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type)); | |
832 | else | |
833 | gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type)); | |
834 | } | |
417ab240 JJ |
835 | |
836 | if (GFC_TYPE_ARRAY_SIZE (type) == NULL_TREE | |
837 | && sym->as->type != AS_ASSUMED_SIZE) | |
01306727 FXC |
838 | { |
839 | GFC_TYPE_ARRAY_SIZE (type) = create_index_var ("size", nest); | |
840 | TREE_NO_WARNING (GFC_TYPE_ARRAY_SIZE (type)) = 1; | |
841 | } | |
417ab240 JJ |
842 | |
843 | if (POINTER_TYPE_P (type)) | |
844 | { | |
845 | gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type))); | |
846 | gcc_assert (TYPE_LANG_SPECIFIC (type) | |
847 | == TYPE_LANG_SPECIFIC (TREE_TYPE (type))); | |
848 | type = TREE_TYPE (type); | |
849 | } | |
850 | ||
851 | if (! COMPLETE_TYPE_P (type) && GFC_TYPE_ARRAY_SIZE (type)) | |
852 | { | |
853 | tree size, range; | |
854 | ||
bc98ed60 TB |
855 | size = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type, |
856 | GFC_TYPE_ARRAY_SIZE (type), gfc_index_one_node); | |
417ab240 JJ |
857 | range = build_range_type (gfc_array_index_type, gfc_index_zero_node, |
858 | size); | |
859 | TYPE_DOMAIN (type) = range; | |
860 | layout_type (type); | |
861 | } | |
25c29c56 | 862 | |
25c29c56 JJ |
863 | if (TYPE_NAME (type) != NULL_TREE |
864 | && GFC_TYPE_ARRAY_UBOUND (type, sym->as->rank - 1) != NULL_TREE | |
865 | && TREE_CODE (GFC_TYPE_ARRAY_UBOUND (type, sym->as->rank - 1)) == VAR_DECL) | |
866 | { | |
867 | tree gtype = DECL_ORIGINAL_TYPE (TYPE_NAME (type)); | |
868 | ||
869 | for (dim = 0; dim < sym->as->rank - 1; dim++) | |
870 | { | |
871 | gcc_assert (TREE_CODE (gtype) == ARRAY_TYPE); | |
872 | gtype = TREE_TYPE (gtype); | |
873 | } | |
874 | gcc_assert (TREE_CODE (gtype) == ARRAY_TYPE); | |
875 | if (TYPE_MAX_VALUE (TYPE_DOMAIN (gtype)) == NULL) | |
876 | TYPE_NAME (type) = NULL_TREE; | |
877 | } | |
878 | ||
879 | if (TYPE_NAME (type) == NULL_TREE) | |
880 | { | |
881 | tree gtype = TREE_TYPE (type), rtype, type_decl; | |
882 | ||
883 | for (dim = sym->as->rank - 1; dim >= 0; dim--) | |
884 | { | |
fcd3c5a9 JJ |
885 | tree lbound, ubound; |
886 | lbound = GFC_TYPE_ARRAY_LBOUND (type, dim); | |
887 | ubound = GFC_TYPE_ARRAY_UBOUND (type, dim); | |
888 | rtype = build_range_type (gfc_array_index_type, lbound, ubound); | |
25c29c56 | 889 | gtype = build_array_type (gtype, rtype); |
e429bb49 JJ |
890 | /* Ensure the bound variables aren't optimized out at -O0. |
891 | For -O1 and above they often will be optimized out, but | |
cd3f04c8 JJ |
892 | can be tracked by VTA. Also set DECL_NAMELESS, so that |
893 | the artificial lbound.N or ubound.N DECL_NAME doesn't | |
894 | end up in debug info. */ | |
fcd3c5a9 JJ |
895 | if (lbound && TREE_CODE (lbound) == VAR_DECL |
896 | && DECL_ARTIFICIAL (lbound) && DECL_IGNORED_P (lbound)) | |
897 | { | |
898 | if (DECL_NAME (lbound) | |
899 | && strstr (IDENTIFIER_POINTER (DECL_NAME (lbound)), | |
900 | "lbound") != 0) | |
cd3f04c8 | 901 | DECL_NAMELESS (lbound) = 1; |
fcd3c5a9 JJ |
902 | DECL_IGNORED_P (lbound) = 0; |
903 | } | |
904 | if (ubound && TREE_CODE (ubound) == VAR_DECL | |
905 | && DECL_ARTIFICIAL (ubound) && DECL_IGNORED_P (ubound)) | |
906 | { | |
907 | if (DECL_NAME (ubound) | |
908 | && strstr (IDENTIFIER_POINTER (DECL_NAME (ubound)), | |
909 | "ubound") != 0) | |
cd3f04c8 | 910 | DECL_NAMELESS (ubound) = 1; |
fcd3c5a9 JJ |
911 | DECL_IGNORED_P (ubound) = 0; |
912 | } | |
25c29c56 | 913 | } |
c2255bc4 AH |
914 | TYPE_NAME (type) = type_decl = build_decl (input_location, |
915 | TYPE_DECL, NULL, gtype); | |
25c29c56 JJ |
916 | DECL_ORIGINAL_TYPE (type_decl) = gtype; |
917 | } | |
6de9cd9a DN |
918 | } |
919 | ||
920 | ||
921 | /* For some dummy arguments we don't use the actual argument directly. | |
bae88af6 | 922 | Instead we create a local decl and use that. This allows us to perform |
6de9cd9a DN |
923 | initialization, and construct full type information. */ |
924 | ||
925 | static tree | |
926 | gfc_build_dummy_array_decl (gfc_symbol * sym, tree dummy) | |
927 | { | |
928 | tree decl; | |
929 | tree type; | |
930 | gfc_array_spec *as; | |
931 | char *name; | |
dcfef7d4 | 932 | gfc_packed packed; |
6de9cd9a DN |
933 | int n; |
934 | bool known_size; | |
935 | ||
936 | if (sym->attr.pointer || sym->attr.allocatable) | |
937 | return dummy; | |
938 | ||
939 | /* Add to list of variables if not a fake result variable. */ | |
940 | if (sym->attr.result || sym->attr.dummy) | |
941 | gfc_defer_symbol_init (sym); | |
942 | ||
943 | type = TREE_TYPE (dummy); | |
6e45f57b | 944 | gcc_assert (TREE_CODE (dummy) == PARM_DECL |
6de9cd9a DN |
945 | && POINTER_TYPE_P (type)); |
946 | ||
f8d0aee5 | 947 | /* Do we know the element size? */ |
6de9cd9a | 948 | known_size = sym->ts.type != BT_CHARACTER |
bc21d315 | 949 | || INTEGER_CST_P (sym->ts.u.cl->backend_decl); |
6de9cd9a DN |
950 | |
951 | if (known_size && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type))) | |
952 | { | |
953 | /* For descriptorless arrays with known element size the actual | |
954 | argument is sufficient. */ | |
6e45f57b | 955 | gcc_assert (GFC_ARRAY_TYPE_P (type)); |
6de9cd9a DN |
956 | gfc_build_qualified_array (dummy, sym); |
957 | return dummy; | |
958 | } | |
959 | ||
960 | type = TREE_TYPE (type); | |
961 | if (GFC_DESCRIPTOR_TYPE_P (type)) | |
962 | { | |
fa951694 | 963 | /* Create a descriptorless array pointer. */ |
6de9cd9a | 964 | as = sym->as; |
dcfef7d4 | 965 | packed = PACKED_NO; |
1c3339af FXC |
966 | |
967 | /* Even when -frepack-arrays is used, symbols with TARGET attribute | |
968 | are not repacked. */ | |
969 | if (!gfc_option.flag_repack_arrays || sym->attr.target) | |
6de9cd9a DN |
970 | { |
971 | if (as->type == AS_ASSUMED_SIZE) | |
dcfef7d4 | 972 | packed = PACKED_FULL; |
6de9cd9a DN |
973 | } |
974 | else | |
975 | { | |
976 | if (as->type == AS_EXPLICIT) | |
977 | { | |
dcfef7d4 | 978 | packed = PACKED_FULL; |
6de9cd9a DN |
979 | for (n = 0; n < as->rank; n++) |
980 | { | |
981 | if (!(as->upper[n] | |
982 | && as->lower[n] | |
983 | && as->upper[n]->expr_type == EXPR_CONSTANT | |
984 | && as->lower[n]->expr_type == EXPR_CONSTANT)) | |
dcfef7d4 | 985 | packed = PACKED_PARTIAL; |
6de9cd9a DN |
986 | } |
987 | } | |
988 | else | |
dcfef7d4 | 989 | packed = PACKED_PARTIAL; |
6de9cd9a DN |
990 | } |
991 | ||
992 | type = gfc_typenode_for_spec (&sym->ts); | |
10174ddf MM |
993 | type = gfc_get_nodesc_array_type (type, sym->as, packed, |
994 | !sym->attr.target); | |
6de9cd9a DN |
995 | } |
996 | else | |
997 | { | |
998 | /* We now have an expression for the element size, so create a fully | |
999 | qualified type. Reset sym->backend decl or this will just return the | |
1000 | old type. */ | |
3e978d30 | 1001 | DECL_ARTIFICIAL (sym->backend_decl) = 1; |
6de9cd9a DN |
1002 | sym->backend_decl = NULL_TREE; |
1003 | type = gfc_sym_type (sym); | |
dcfef7d4 | 1004 | packed = PACKED_FULL; |
6de9cd9a DN |
1005 | } |
1006 | ||
1007 | ASM_FORMAT_PRIVATE_NAME (name, IDENTIFIER_POINTER (DECL_NAME (dummy)), 0); | |
c2255bc4 AH |
1008 | decl = build_decl (input_location, |
1009 | VAR_DECL, get_identifier (name), type); | |
6de9cd9a DN |
1010 | |
1011 | DECL_ARTIFICIAL (decl) = 1; | |
cd3f04c8 | 1012 | DECL_NAMELESS (decl) = 1; |
6de9cd9a DN |
1013 | TREE_PUBLIC (decl) = 0; |
1014 | TREE_STATIC (decl) = 0; | |
1015 | DECL_EXTERNAL (decl) = 0; | |
1016 | ||
1017 | /* We should never get deferred shape arrays here. We used to because of | |
1018 | frontend bugs. */ | |
6e45f57b | 1019 | gcc_assert (sym->as->type != AS_DEFERRED); |
6de9cd9a | 1020 | |
dcfef7d4 TS |
1021 | if (packed == PACKED_PARTIAL) |
1022 | GFC_DECL_PARTIAL_PACKED_ARRAY (decl) = 1; | |
1023 | else if (packed == PACKED_FULL) | |
1024 | GFC_DECL_PACKED_ARRAY (decl) = 1; | |
6de9cd9a DN |
1025 | |
1026 | gfc_build_qualified_array (decl, sym); | |
1027 | ||
1028 | if (DECL_LANG_SPECIFIC (dummy)) | |
1029 | DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (dummy); | |
1030 | else | |
1031 | gfc_allocate_lang_decl (decl); | |
1032 | ||
1033 | GFC_DECL_SAVED_DESCRIPTOR (decl) = dummy; | |
1034 | ||
1035 | if (sym->ns->proc_name->backend_decl == current_function_decl | |
1036 | || sym->attr.contained) | |
1037 | gfc_add_decl_to_function (decl); | |
1038 | else | |
1039 | gfc_add_decl_to_parent_function (decl); | |
1040 | ||
1041 | return decl; | |
1042 | } | |
1043 | ||
77f2a970 JJ |
1044 | /* For symbol SYM with GFC_DECL_SAVED_DESCRIPTOR used in contained |
1045 | function add a VAR_DECL to the current function with DECL_VALUE_EXPR | |
1046 | pointing to the artificial variable for debug info purposes. */ | |
1047 | ||
1048 | static void | |
1049 | gfc_nonlocal_dummy_array_decl (gfc_symbol *sym) | |
1050 | { | |
1051 | tree decl, dummy; | |
1052 | ||
1053 | if (! nonlocal_dummy_decl_pset) | |
1054 | nonlocal_dummy_decl_pset = pointer_set_create (); | |
1055 | ||
1056 | if (pointer_set_insert (nonlocal_dummy_decl_pset, sym->backend_decl)) | |
1057 | return; | |
1058 | ||
1059 | dummy = GFC_DECL_SAVED_DESCRIPTOR (sym->backend_decl); | |
c2255bc4 | 1060 | decl = build_decl (input_location, VAR_DECL, DECL_NAME (dummy), |
77f2a970 JJ |
1061 | TREE_TYPE (sym->backend_decl)); |
1062 | DECL_ARTIFICIAL (decl) = 0; | |
1063 | TREE_USED (decl) = 1; | |
1064 | TREE_PUBLIC (decl) = 0; | |
1065 | TREE_STATIC (decl) = 0; | |
1066 | DECL_EXTERNAL (decl) = 0; | |
1067 | if (DECL_BY_REFERENCE (dummy)) | |
1068 | DECL_BY_REFERENCE (decl) = 1; | |
1069 | DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (sym->backend_decl); | |
1070 | SET_DECL_VALUE_EXPR (decl, sym->backend_decl); | |
1071 | DECL_HAS_VALUE_EXPR_P (decl) = 1; | |
1072 | DECL_CONTEXT (decl) = DECL_CONTEXT (sym->backend_decl); | |
910ad8de | 1073 | DECL_CHAIN (decl) = nonlocal_dummy_decls; |
77f2a970 JJ |
1074 | nonlocal_dummy_decls = decl; |
1075 | } | |
6de9cd9a DN |
1076 | |
1077 | /* Return a constant or a variable to use as a string length. Does not | |
1078 | add the decl to the current scope. */ | |
1079 | ||
1080 | static tree | |
1081 | gfc_create_string_length (gfc_symbol * sym) | |
1082 | { | |
bc21d315 JW |
1083 | gcc_assert (sym->ts.u.cl); |
1084 | gfc_conv_const_charlen (sym->ts.u.cl); | |
cadb8f42 | 1085 | |
bc21d315 | 1086 | if (sym->ts.u.cl->backend_decl == NULL_TREE) |
6de9cd9a | 1087 | { |
cadb8f42 | 1088 | tree length; |
6052c299 | 1089 | const char *name; |
6de9cd9a DN |
1090 | |
1091 | /* Also prefix the mangled name. */ | |
6052c299 TB |
1092 | if (sym->module) |
1093 | name = gfc_get_string (".__%s_MOD_%s", sym->module, sym->name); | |
1094 | else | |
1095 | name = gfc_get_string (".%s", sym->name); | |
1096 | ||
c2255bc4 AH |
1097 | length = build_decl (input_location, |
1098 | VAR_DECL, get_identifier (name), | |
d7177ab2 | 1099 | gfc_charlen_type_node); |
6de9cd9a DN |
1100 | DECL_ARTIFICIAL (length) = 1; |
1101 | TREE_USED (length) = 1; | |
417ab240 JJ |
1102 | if (sym->ns->proc_name->tlink != NULL) |
1103 | gfc_defer_symbol_init (sym); | |
cadb8f42 | 1104 | |
bc21d315 | 1105 | sym->ts.u.cl->backend_decl = length; |
6052c299 TB |
1106 | |
1107 | if (sym->attr.save || sym->ns->proc_name->attr.flavor == FL_MODULE) | |
1108 | TREE_STATIC (length) = 1; | |
1109 | ||
1110 | if (sym->ns->proc_name->attr.flavor == FL_MODULE | |
1111 | && (sym->attr.access != ACCESS_PRIVATE || sym->attr.public_used)) | |
1112 | TREE_PUBLIC (length) = 1; | |
6de9cd9a DN |
1113 | } |
1114 | ||
bc21d315 JW |
1115 | gcc_assert (sym->ts.u.cl->backend_decl != NULL_TREE); |
1116 | return sym->ts.u.cl->backend_decl; | |
6de9cd9a DN |
1117 | } |
1118 | ||
910450c1 FW |
1119 | /* If a variable is assigned a label, we add another two auxiliary |
1120 | variables. */ | |
1121 | ||
1122 | static void | |
1123 | gfc_add_assign_aux_vars (gfc_symbol * sym) | |
1124 | { | |
1125 | tree addr; | |
1126 | tree length; | |
1127 | tree decl; | |
1128 | ||
1129 | gcc_assert (sym->backend_decl); | |
1130 | ||
1131 | decl = sym->backend_decl; | |
1132 | gfc_allocate_lang_decl (decl); | |
1133 | GFC_DECL_ASSIGN (decl) = 1; | |
c2255bc4 AH |
1134 | length = build_decl (input_location, |
1135 | VAR_DECL, create_tmp_var_name (sym->name), | |
910450c1 | 1136 | gfc_charlen_type_node); |
c2255bc4 AH |
1137 | addr = build_decl (input_location, |
1138 | VAR_DECL, create_tmp_var_name (sym->name), | |
910450c1 FW |
1139 | pvoid_type_node); |
1140 | gfc_finish_var_decl (length, sym); | |
1141 | gfc_finish_var_decl (addr, sym); | |
1142 | /* STRING_LENGTH is also used as flag. Less than -1 means that | |
1143 | ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the | |
1144 | target label's address. Otherwise, value is the length of a format string | |
1145 | and ASSIGN_ADDR is its address. */ | |
1146 | if (TREE_STATIC (length)) | |
df09d1d5 | 1147 | DECL_INITIAL (length) = build_int_cst (gfc_charlen_type_node, -2); |
910450c1 FW |
1148 | else |
1149 | gfc_defer_symbol_init (sym); | |
1150 | ||
1151 | GFC_DECL_STRING_LEN (decl) = length; | |
1152 | GFC_DECL_ASSIGN_ADDR (decl) = addr; | |
1153 | } | |
6de9cd9a | 1154 | |
08a6b8e0 TB |
1155 | |
1156 | static tree | |
1157 | add_attributes_to_decl (symbol_attribute sym_attr, tree list) | |
1158 | { | |
1159 | unsigned id; | |
1160 | tree attr; | |
1161 | ||
1162 | for (id = 0; id < EXT_ATTR_NUM; id++) | |
1163 | if (sym_attr.ext_attr & (1 << id)) | |
1164 | { | |
1165 | attr = build_tree_list ( | |
1166 | get_identifier (ext_attr_list[id].middle_end_name), | |
1167 | NULL_TREE); | |
1168 | list = chainon (list, attr); | |
1169 | } | |
1170 | ||
1171 | return list; | |
1172 | } | |
1173 | ||
1174 | ||
1d0134b3 JW |
1175 | static void build_function_decl (gfc_symbol * sym, bool global); |
1176 | ||
1177 | ||
6de9cd9a DN |
1178 | /* Return the decl for a gfc_symbol, create it if it doesn't already |
1179 | exist. */ | |
1180 | ||
1181 | tree | |
1182 | gfc_get_symbol_decl (gfc_symbol * sym) | |
1183 | { | |
1184 | tree decl; | |
1185 | tree length = NULL_TREE; | |
08a6b8e0 | 1186 | tree attributes; |
6de9cd9a | 1187 | int byref; |
be1f1ed9 | 1188 | bool intrinsic_array_parameter = false; |
6de9cd9a | 1189 | |
61321991 | 1190 | gcc_assert (sym->attr.referenced |
c60a84b8 | 1191 | || sym->attr.use_assoc |
6a018495 TB |
1192 | || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY |
1193 | || (sym->module && sym->attr.if_source != IFSRC_DECL | |
1194 | && sym->backend_decl)); | |
6de9cd9a | 1195 | |
f64edc8b | 1196 | if (sym->ns && sym->ns->proc_name && sym->ns->proc_name->attr.function) |
6de9cd9a DN |
1197 | byref = gfc_return_by_reference (sym->ns->proc_name); |
1198 | else | |
1199 | byref = 0; | |
1200 | ||
eece1eb9 PT |
1201 | /* Make sure that the vtab for the declared type is completed. */ |
1202 | if (sym->ts.type == BT_CLASS) | |
1203 | { | |
7a08eda1 | 1204 | gfc_component *c = CLASS_DATA (sym); |
eece1eb9 | 1205 | if (!c->ts.u.derived->backend_decl) |
58eba515 JW |
1206 | { |
1207 | gfc_find_derived_vtab (c->ts.u.derived); | |
1208 | gfc_get_derived_type (sym->ts.u.derived); | |
1209 | } | |
eece1eb9 PT |
1210 | } |
1211 | ||
8d51f26f PT |
1212 | /* All deferred character length procedures need to retain the backend |
1213 | decl, which is a pointer to the character length in the caller's | |
1214 | namespace and to declare a local character length. */ | |
1215 | if (!byref && sym->attr.function | |
1216 | && sym->ts.type == BT_CHARACTER | |
1217 | && sym->ts.deferred | |
1218 | && sym->ts.u.cl->passed_length == NULL | |
1219 | && sym->ts.u.cl->backend_decl | |
1220 | && TREE_CODE (sym->ts.u.cl->backend_decl) == PARM_DECL) | |
1221 | { | |
1222 | sym->ts.u.cl->passed_length = sym->ts.u.cl->backend_decl; | |
1223 | sym->ts.u.cl->backend_decl = NULL_TREE; | |
1224 | length = gfc_create_string_length (sym); | |
1225 | } | |
1226 | ||
6de9cd9a DN |
1227 | if ((sym->attr.dummy && ! sym->attr.function) || (sym->attr.result && byref)) |
1228 | { | |
1229 | /* Return via extra parameter. */ | |
1230 | if (sym->attr.result && byref | |
1231 | && !sym->backend_decl) | |
1232 | { | |
1233 | sym->backend_decl = | |
1234 | DECL_ARGUMENTS (sym->ns->proc_name->backend_decl); | |
d198b59a JJ |
1235 | /* For entry master function skip over the __entry |
1236 | argument. */ | |
1237 | if (sym->ns->proc_name->attr.entry_master) | |
910ad8de | 1238 | sym->backend_decl = DECL_CHAIN (sym->backend_decl); |
6de9cd9a DN |
1239 | } |
1240 | ||
1241 | /* Dummy variables should already have been created. */ | |
6e45f57b | 1242 | gcc_assert (sym->backend_decl); |
6de9cd9a DN |
1243 | |
1244 | /* Create a character length variable. */ | |
1245 | if (sym->ts.type == BT_CHARACTER) | |
1246 | { | |
8d51f26f PT |
1247 | /* For a deferred dummy, make a new string length variable. */ |
1248 | if (sym->ts.deferred | |
1249 | && | |
1250 | (sym->ts.u.cl->passed_length == sym->ts.u.cl->backend_decl)) | |
1251 | sym->ts.u.cl->backend_decl = NULL_TREE; | |
1252 | ||
1253 | if (sym->ts.deferred && sym->attr.result | |
1254 | && sym->ts.u.cl->passed_length == NULL | |
1255 | && sym->ts.u.cl->backend_decl) | |
1256 | { | |
1257 | sym->ts.u.cl->passed_length = sym->ts.u.cl->backend_decl; | |
1258 | sym->ts.u.cl->backend_decl = NULL_TREE; | |
1259 | } | |
1260 | ||
bc21d315 | 1261 | if (sym->ts.u.cl->backend_decl == NULL_TREE) |
417ab240 JJ |
1262 | length = gfc_create_string_length (sym); |
1263 | else | |
bc21d315 | 1264 | length = sym->ts.u.cl->backend_decl; |
417ab240 | 1265 | if (TREE_CODE (length) == VAR_DECL |
e5b16755 | 1266 | && DECL_FILE_SCOPE_P (length)) |
6de9cd9a | 1267 | { |
3e978d30 PT |
1268 | /* Add the string length to the same context as the symbol. */ |
1269 | if (DECL_CONTEXT (sym->backend_decl) == current_function_decl) | |
1270 | gfc_add_decl_to_function (length); | |
1271 | else | |
1272 | gfc_add_decl_to_parent_function (length); | |
1273 | ||
1274 | gcc_assert (DECL_CONTEXT (sym->backend_decl) == | |
1275 | DECL_CONTEXT (length)); | |
1276 | ||
417ab240 | 1277 | gfc_defer_symbol_init (sym); |
a41baa64 | 1278 | } |
6de9cd9a DN |
1279 | } |
1280 | ||
1281 | /* Use a copy of the descriptor for dummy arrays. */ | |
4ca9939b TB |
1282 | if ((sym->attr.dimension || sym->attr.codimension) |
1283 | && !TREE_USED (sym->backend_decl)) | |
6de9cd9a | 1284 | { |
3e978d30 PT |
1285 | decl = gfc_build_dummy_array_decl (sym, sym->backend_decl); |
1286 | /* Prevent the dummy from being detected as unused if it is copied. */ | |
1287 | if (sym->backend_decl != NULL && decl != sym->backend_decl) | |
1288 | DECL_ARTIFICIAL (sym->backend_decl) = 1; | |
1289 | sym->backend_decl = decl; | |
6de9cd9a DN |
1290 | } |
1291 | ||
1292 | TREE_USED (sym->backend_decl) = 1; | |
910450c1 FW |
1293 | if (sym->attr.assign && GFC_DECL_ASSIGN (sym->backend_decl) == 0) |
1294 | { | |
1295 | gfc_add_assign_aux_vars (sym); | |
1296 | } | |
77f2a970 JJ |
1297 | |
1298 | if (sym->attr.dimension | |
1299 | && DECL_LANG_SPECIFIC (sym->backend_decl) | |
1300 | && GFC_DECL_SAVED_DESCRIPTOR (sym->backend_decl) | |
1301 | && DECL_CONTEXT (sym->backend_decl) != current_function_decl) | |
1302 | gfc_nonlocal_dummy_array_decl (sym); | |
1303 | ||
c49ea23d PT |
1304 | if (sym->ts.type == BT_CLASS && sym->backend_decl) |
1305 | GFC_DECL_CLASS(sym->backend_decl) = 1; | |
1306 | ||
1307 | if (sym->ts.type == BT_CLASS && sym->backend_decl) | |
1308 | GFC_DECL_CLASS(sym->backend_decl) = 1; | |
1309 | return sym->backend_decl; | |
6de9cd9a DN |
1310 | } |
1311 | ||
1312 | if (sym->backend_decl) | |
1313 | return sym->backend_decl; | |
1314 | ||
4ed5664e TB |
1315 | /* Special case for array-valued named constants from intrinsic |
1316 | procedures; those are inlined. */ | |
1317 | if (sym->attr.use_assoc && sym->from_intmod | |
1318 | && sym->attr.flavor == FL_PARAMETER) | |
1319 | intrinsic_array_parameter = true; | |
1320 | ||
3af8d8cb | 1321 | /* If use associated and whole file compilation, use the module |
43afc047 | 1322 | declaration. */ |
3af8d8cb | 1323 | if (gfc_option.flag_whole_file |
4ed5664e TB |
1324 | && (sym->attr.flavor == FL_VARIABLE |
1325 | || sym->attr.flavor == FL_PARAMETER) | |
0101807c PT |
1326 | && sym->attr.use_assoc |
1327 | && !intrinsic_array_parameter | |
1328 | && sym->module | |
1329 | && gfc_get_module_backend_decl (sym)) | |
c49ea23d PT |
1330 | { |
1331 | if (sym->ts.type == BT_CLASS && sym->backend_decl) | |
1332 | GFC_DECL_CLASS(sym->backend_decl) = 1; | |
1333 | return sym->backend_decl; | |
1334 | } | |
3af8d8cb | 1335 | |
6de9cd9a DN |
1336 | if (sym->attr.flavor == FL_PROCEDURE) |
1337 | { | |
1d0134b3 JW |
1338 | /* Catch function declarations. Only used for actual parameters, |
1339 | procedure pointers and procptr initialization targets. */ | |
1340 | if (sym->attr.external || sym->attr.use_assoc || sym->attr.intrinsic) | |
1341 | { | |
1342 | decl = gfc_get_extern_function_decl (sym); | |
1343 | gfc_set_decl_location (decl, &sym->declared_at); | |
1344 | } | |
1345 | else | |
1346 | { | |
1347 | if (!sym->backend_decl) | |
1348 | build_function_decl (sym, false); | |
1349 | decl = sym->backend_decl; | |
1350 | } | |
6de9cd9a DN |
1351 | return decl; |
1352 | } | |
1353 | ||
1354 | if (sym->attr.intrinsic) | |
1355 | internal_error ("intrinsic variable which isn't a procedure"); | |
1356 | ||
1357 | /* Create string length decl first so that they can be used in the | |
1358 | type declaration. */ | |
1359 | if (sym->ts.type == BT_CHARACTER) | |
1360 | length = gfc_create_string_length (sym); | |
1361 | ||
1362 | /* Create the decl for the variable. */ | |
c2255bc4 AH |
1363 | decl = build_decl (sym->declared_at.lb->location, |
1364 | VAR_DECL, gfc_sym_identifier (sym), gfc_sym_type (sym)); | |
c8cc8542 | 1365 | |
43ce5e52 FXC |
1366 | /* Add attributes to variables. Functions are handled elsewhere. */ |
1367 | attributes = add_attributes_to_decl (sym->attr, NULL_TREE); | |
1368 | decl_attributes (&decl, attributes, 0); | |
1369 | ||
f8d0aee5 | 1370 | /* Symbols from modules should have their assembler names mangled. |
6de9cd9a DN |
1371 | This is done here rather than in gfc_finish_var_decl because it |
1372 | is different for string length variables. */ | |
cb9e4f55 | 1373 | if (sym->module) |
a64f5186 | 1374 | { |
43ce5e52 | 1375 | gfc_set_decl_assembler_name (decl, gfc_sym_mangled_identifier (sym)); |
be1f1ed9 | 1376 | if (sym->attr.use_assoc && !intrinsic_array_parameter) |
a64f5186 JJ |
1377 | DECL_IGNORED_P (decl) = 1; |
1378 | } | |
6de9cd9a | 1379 | |
4ca9939b | 1380 | if (sym->attr.dimension || sym->attr.codimension) |
6de9cd9a DN |
1381 | { |
1382 | /* Create variables to hold the non-constant bits of array info. */ | |
1383 | gfc_build_qualified_array (decl, sym); | |
1384 | ||
fe4e525c TB |
1385 | if (sym->attr.contiguous |
1386 | || ((sym->attr.allocatable || !sym->attr.dummy) && !sym->attr.pointer)) | |
6de9cd9a DN |
1387 | GFC_DECL_PACKED_ARRAY (decl) = 1; |
1388 | } | |
1389 | ||
1517fd57 | 1390 | /* Remember this variable for allocation/cleanup. */ |
9f3761c5 | 1391 | if (sym->attr.dimension || sym->attr.allocatable || sym->attr.codimension |
1517fd57 | 1392 | || (sym->ts.type == BT_CLASS && |
7a08eda1 JW |
1393 | (CLASS_DATA (sym)->attr.dimension |
1394 | || CLASS_DATA (sym)->attr.allocatable)) | |
1517fd57 JW |
1395 | || (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.alloc_comp) |
1396 | /* This applies a derived type default initializer. */ | |
1397 | || (sym->ts.type == BT_DERIVED | |
1398 | && sym->attr.save == SAVE_NONE | |
1399 | && !sym->attr.data | |
1400 | && !sym->attr.allocatable | |
1401 | && (sym->value && !sym->ns->proc_name->attr.is_main_program) | |
be1f1ed9 | 1402 | && !(sym->attr.use_assoc && !intrinsic_array_parameter))) |
b7b184a8 | 1403 | gfc_defer_symbol_init (sym); |
5046aff5 | 1404 | |
6de9cd9a DN |
1405 | gfc_finish_var_decl (decl, sym); |
1406 | ||
597073ac | 1407 | if (sym->ts.type == BT_CHARACTER) |
6de9cd9a | 1408 | { |
6de9cd9a DN |
1409 | /* Character variables need special handling. */ |
1410 | gfc_allocate_lang_decl (decl); | |
1411 | ||
597073ac | 1412 | if (TREE_CODE (length) != INTEGER_CST) |
6de9cd9a | 1413 | { |
6de9cd9a | 1414 | gfc_finish_var_decl (length, sym); |
6e45f57b | 1415 | gcc_assert (!sym->value); |
6de9cd9a | 1416 | } |
6de9cd9a | 1417 | } |
1d6b7f39 PT |
1418 | else if (sym->attr.subref_array_pointer) |
1419 | { | |
1420 | /* We need the span for these beasts. */ | |
1421 | gfc_allocate_lang_decl (decl); | |
1422 | } | |
1423 | ||
1424 | if (sym->attr.subref_array_pointer) | |
1425 | { | |
1426 | tree span; | |
1427 | GFC_DECL_SUBREF_ARRAY_P (decl) = 1; | |
c2255bc4 AH |
1428 | span = build_decl (input_location, |
1429 | VAR_DECL, create_tmp_var_name ("span"), | |
1d6b7f39 PT |
1430 | gfc_array_index_type); |
1431 | gfc_finish_var_decl (span, sym); | |
de870512 JJ |
1432 | TREE_STATIC (span) = TREE_STATIC (decl); |
1433 | DECL_ARTIFICIAL (span) = 1; | |
1d6b7f39 PT |
1434 | |
1435 | GFC_DECL_SPAN (decl) = span; | |
de870512 | 1436 | GFC_TYPE_ARRAY_SPAN (TREE_TYPE (decl)) = span; |
1d6b7f39 PT |
1437 | } |
1438 | ||
c49ea23d PT |
1439 | if (sym->ts.type == BT_CLASS) |
1440 | GFC_DECL_CLASS(decl) = 1; | |
1441 | ||
6de9cd9a DN |
1442 | sym->backend_decl = decl; |
1443 | ||
910450c1 | 1444 | if (sym->attr.assign) |
6c0e51c4 | 1445 | gfc_add_assign_aux_vars (sym); |
910450c1 | 1446 | |
be1f1ed9 TB |
1447 | if (intrinsic_array_parameter) |
1448 | { | |
1449 | TREE_STATIC (decl) = 1; | |
1450 | DECL_EXTERNAL (decl) = 0; | |
1451 | } | |
1452 | ||
1453 | if (TREE_STATIC (decl) | |
1454 | && !(sym->attr.use_assoc && !intrinsic_array_parameter) | |
2b56d6a4 TB |
1455 | && (sym->attr.save || sym->ns->proc_name->attr.is_main_program |
1456 | || gfc_option.flag_max_stack_var_size == 0 | |
b8ff4e88 | 1457 | || sym->attr.data || sym->ns->proc_name->attr.flavor == FL_MODULE) |
badd9e69 TB |
1458 | && (gfc_option.coarray != GFC_FCOARRAY_LIB |
1459 | || !sym->attr.codimension || sym->attr.allocatable)) | |
2b56d6a4 TB |
1460 | { |
1461 | /* Add static initializer. For procedures, it is only needed if | |
1462 | SAVE is specified otherwise they need to be reinitialized | |
1463 | every time the procedure is entered. The TREE_STATIC is | |
1464 | in this case due to -fmax-stack-var-size=. */ | |
597073ac | 1465 | DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts, |
1d0134b3 | 1466 | TREE_TYPE (decl), |
badd9e69 TB |
1467 | sym->attr.dimension |
1468 | || (sym->attr.codimension | |
1469 | && sym->attr.allocatable), | |
1d0134b3 JW |
1470 | sym->attr.pointer |
1471 | || sym->attr.allocatable, | |
1472 | sym->attr.proc_pointer); | |
597073ac PB |
1473 | } |
1474 | ||
77f2a970 JJ |
1475 | if (!TREE_STATIC (decl) |
1476 | && POINTER_TYPE_P (TREE_TYPE (decl)) | |
1477 | && !sym->attr.pointer | |
1478 | && !sym->attr.allocatable | |
1479 | && !sym->attr.proc_pointer) | |
1480 | DECL_BY_REFERENCE (decl) = 1; | |
1481 | ||
cf651ca2 TB |
1482 | if (sym->attr.vtab |
1483 | || (sym->name[0] == '_' && strncmp ("__def_init", sym->name, 10) == 0)) | |
22c30bc0 TB |
1484 | { |
1485 | TREE_READONLY (decl) = 1; | |
1486 | GFC_DECL_PUSH_TOPLEVEL (decl) = 1; | |
1487 | } | |
cf651ca2 | 1488 | |
6de9cd9a DN |
1489 | return decl; |
1490 | } | |
1491 | ||
1492 | ||
7b5b57b7 PB |
1493 | /* Substitute a temporary variable in place of the real one. */ |
1494 | ||
1495 | void | |
1496 | gfc_shadow_sym (gfc_symbol * sym, tree decl, gfc_saved_var * save) | |
1497 | { | |
1498 | save->attr = sym->attr; | |
1499 | save->decl = sym->backend_decl; | |
1500 | ||
1501 | gfc_clear_attr (&sym->attr); | |
1502 | sym->attr.referenced = 1; | |
1503 | sym->attr.flavor = FL_VARIABLE; | |
1504 | ||
1505 | sym->backend_decl = decl; | |
1506 | } | |
1507 | ||
1508 | ||
1509 | /* Restore the original variable. */ | |
1510 | ||
1511 | void | |
1512 | gfc_restore_sym (gfc_symbol * sym, gfc_saved_var * save) | |
1513 | { | |
1514 | sym->attr = save->attr; | |
1515 | sym->backend_decl = save->decl; | |
1516 | } | |
1517 | ||
1518 | ||
8fb74da4 JW |
1519 | /* Declare a procedure pointer. */ |
1520 | ||
1521 | static tree | |
1522 | get_proc_pointer_decl (gfc_symbol *sym) | |
1523 | { | |
1524 | tree decl; | |
08a6b8e0 | 1525 | tree attributes; |
8fb74da4 JW |
1526 | |
1527 | decl = sym->backend_decl; | |
1528 | if (decl) | |
1529 | return decl; | |
1530 | ||
c2255bc4 AH |
1531 | decl = build_decl (input_location, |
1532 | VAR_DECL, get_identifier (sym->name), | |
8fb74da4 JW |
1533 | build_pointer_type (gfc_get_function_type (sym))); |
1534 | ||
06c7153f TB |
1535 | if ((sym->ns->proc_name |
1536 | && sym->ns->proc_name->backend_decl == current_function_decl) | |
8fb74da4 JW |
1537 | || sym->attr.contained) |
1538 | gfc_add_decl_to_function (decl); | |
6e0d2de7 | 1539 | else if (sym->ns->proc_name->attr.flavor != FL_MODULE) |
8fb74da4 JW |
1540 | gfc_add_decl_to_parent_function (decl); |
1541 | ||
1542 | sym->backend_decl = decl; | |
1543 | ||
6e0d2de7 JW |
1544 | /* If a variable is USE associated, it's always external. */ |
1545 | if (sym->attr.use_assoc) | |
1546 | { | |
1547 | DECL_EXTERNAL (decl) = 1; | |
1548 | TREE_PUBLIC (decl) = 1; | |
1549 | } | |
1550 | else if (sym->module && sym->ns->proc_name->attr.flavor == FL_MODULE) | |
1551 | { | |
1552 | /* This is the declaration of a module variable. */ | |
1553 | TREE_PUBLIC (decl) = 1; | |
1554 | TREE_STATIC (decl) = 1; | |
1555 | } | |
1556 | ||
8fb74da4 JW |
1557 | if (!sym->attr.use_assoc |
1558 | && (sym->attr.save != SAVE_NONE || sym->attr.data | |
1559 | || (sym->value && sym->ns->proc_name->attr.is_main_program))) | |
1560 | TREE_STATIC (decl) = 1; | |
1561 | ||
1562 | if (TREE_STATIC (decl) && sym->value) | |
1563 | { | |
1564 | /* Add static initializer. */ | |
1565 | DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts, | |
1d0134b3 JW |
1566 | TREE_TYPE (decl), |
1567 | sym->attr.dimension, | |
1568 | false, true); | |
8fb74da4 JW |
1569 | } |
1570 | ||
a6c975bd JJ |
1571 | /* Handle threadprivate procedure pointers. */ |
1572 | if (sym->attr.threadprivate | |
1573 | && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))) | |
1574 | DECL_TLS_MODEL (decl) = decl_default_tls_model (decl); | |
1575 | ||
08a6b8e0 TB |
1576 | attributes = add_attributes_to_decl (sym->attr, NULL_TREE); |
1577 | decl_attributes (&decl, attributes, 0); | |
1578 | ||
8fb74da4 JW |
1579 | return decl; |
1580 | } | |
1581 | ||
1582 | ||
6de9cd9a DN |
1583 | /* Get a basic decl for an external function. */ |
1584 | ||
1585 | tree | |
1586 | gfc_get_extern_function_decl (gfc_symbol * sym) | |
1587 | { | |
1588 | tree type; | |
1589 | tree fndecl; | |
08a6b8e0 | 1590 | tree attributes; |
6de9cd9a DN |
1591 | gfc_expr e; |
1592 | gfc_intrinsic_sym *isym; | |
1593 | gfc_expr argexpr; | |
e6472bce | 1594 | char s[GFC_MAX_SYMBOL_LEN + 23]; /* "_gfortran_f2c_specific" and '\0'. */ |
6de9cd9a DN |
1595 | tree name; |
1596 | tree mangled_name; | |
71a7778c | 1597 | gfc_gsymbol *gsym; |
6de9cd9a DN |
1598 | |
1599 | if (sym->backend_decl) | |
1600 | return sym->backend_decl; | |
1601 | ||
3d79abbd PB |
1602 | /* We should never be creating external decls for alternate entry points. |
1603 | The procedure may be an alternate entry point, but we don't want/need | |
1604 | to know that. */ | |
6e45f57b | 1605 | gcc_assert (!(sym->attr.entry || sym->attr.entry_master)); |
3d79abbd | 1606 | |
8fb74da4 JW |
1607 | if (sym->attr.proc_pointer) |
1608 | return get_proc_pointer_decl (sym); | |
1609 | ||
71a7778c PT |
1610 | /* See if this is an external procedure from the same file. If so, |
1611 | return the backend_decl. */ | |
1612 | gsym = gfc_find_gsymbol (gfc_gsym_root, sym->name); | |
1613 | ||
1614 | if (gfc_option.flag_whole_file | |
6a018495 | 1615 | && (!sym->attr.use_assoc || sym->attr.if_source != IFSRC_DECL) |
71a7778c PT |
1616 | && !sym->backend_decl |
1617 | && gsym && gsym->ns | |
1618 | && ((gsym->type == GSYM_SUBROUTINE) || (gsym->type == GSYM_FUNCTION)) | |
fb55ca75 | 1619 | && (gsym->ns->proc_name->backend_decl || !sym->attr.intrinsic)) |
71a7778c | 1620 | { |
fb55ca75 TB |
1621 | if (!gsym->ns->proc_name->backend_decl) |
1622 | { | |
1623 | /* By construction, the external function cannot be | |
1624 | a contained procedure. */ | |
1625 | locus old_loc; | |
1626 | tree save_fn_decl = current_function_decl; | |
1627 | ||
1628 | current_function_decl = NULL_TREE; | |
363aab21 | 1629 | gfc_save_backend_locus (&old_loc); |
fb55ca75 TB |
1630 | push_cfun (cfun); |
1631 | ||
1632 | gfc_create_function_decl (gsym->ns, true); | |
1633 | ||
1634 | pop_cfun (); | |
363aab21 | 1635 | gfc_restore_backend_locus (&old_loc); |
fb55ca75 TB |
1636 | current_function_decl = save_fn_decl; |
1637 | } | |
1638 | ||
71a7778c PT |
1639 | /* If the namespace has entries, the proc_name is the |
1640 | entry master. Find the entry and use its backend_decl. | |
1641 | otherwise, use the proc_name backend_decl. */ | |
1642 | if (gsym->ns->entries) | |
1643 | { | |
1644 | gfc_entry_list *entry = gsym->ns->entries; | |
1645 | ||
1646 | for (; entry; entry = entry->next) | |
1647 | { | |
1648 | if (strcmp (gsym->name, entry->sym->name) == 0) | |
1649 | { | |
1650 | sym->backend_decl = entry->sym->backend_decl; | |
1651 | break; | |
1652 | } | |
1653 | } | |
1654 | } | |
1655 | else | |
6a018495 | 1656 | sym->backend_decl = gsym->ns->proc_name->backend_decl; |
71a7778c PT |
1657 | |
1658 | if (sym->backend_decl) | |
6a018495 TB |
1659 | { |
1660 | /* Avoid problems of double deallocation of the backend declaration | |
1661 | later in gfc_trans_use_stmts; cf. PR 45087. */ | |
1662 | if (sym->attr.if_source != IFSRC_DECL && sym->attr.use_assoc) | |
1663 | sym->attr.use_assoc = 0; | |
1664 | ||
1665 | return sym->backend_decl; | |
1666 | } | |
71a7778c PT |
1667 | } |
1668 | ||
3af8d8cb PT |
1669 | /* See if this is a module procedure from the same file. If so, |
1670 | return the backend_decl. */ | |
1671 | if (sym->module) | |
1672 | gsym = gfc_find_gsymbol (gfc_gsym_root, sym->module); | |
1673 | ||
1674 | if (gfc_option.flag_whole_file | |
1675 | && gsym && gsym->ns | |
1676 | && gsym->type == GSYM_MODULE) | |
1677 | { | |
1678 | gfc_symbol *s; | |
1679 | ||
1680 | s = NULL; | |
1681 | gfc_find_symbol (sym->name, gsym->ns, 0, &s); | |
1682 | if (s && s->backend_decl) | |
1683 | { | |
f29bda83 TB |
1684 | if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS) |
1685 | gfc_copy_dt_decls_ifequal (s->ts.u.derived, sym->ts.u.derived, | |
1686 | true); | |
1687 | else if (sym->ts.type == BT_CHARACTER) | |
1688 | sym->ts.u.cl->backend_decl = s->ts.u.cl->backend_decl; | |
3af8d8cb PT |
1689 | sym->backend_decl = s->backend_decl; |
1690 | return sym->backend_decl; | |
1691 | } | |
1692 | } | |
1693 | ||
6de9cd9a DN |
1694 | if (sym->attr.intrinsic) |
1695 | { | |
1696 | /* Call the resolution function to get the actual name. This is | |
1697 | a nasty hack which relies on the resolution functions only looking | |
1698 | at the first argument. We pass NULL for the second argument | |
1699 | otherwise things like AINT get confused. */ | |
1700 | isym = gfc_find_function (sym->name); | |
6e45f57b | 1701 | gcc_assert (isym->resolve.f0 != NULL); |
6de9cd9a DN |
1702 | |
1703 | memset (&e, 0, sizeof (e)); | |
1704 | e.expr_type = EXPR_FUNCTION; | |
1705 | ||
1706 | memset (&argexpr, 0, sizeof (argexpr)); | |
6e45f57b | 1707 | gcc_assert (isym->formal); |
6de9cd9a DN |
1708 | argexpr.ts = isym->formal->ts; |
1709 | ||
1710 | if (isym->formal->next == NULL) | |
1711 | isym->resolve.f1 (&e, &argexpr); | |
1712 | else | |
1713 | { | |
0e7e7e6e FXC |
1714 | if (isym->formal->next->next == NULL) |
1715 | isym->resolve.f2 (&e, &argexpr, NULL); | |
1716 | else | |
1717 | { | |
5cda5098 FXC |
1718 | if (isym->formal->next->next->next == NULL) |
1719 | isym->resolve.f3 (&e, &argexpr, NULL, NULL); | |
1720 | else | |
1721 | { | |
1722 | /* All specific intrinsics take less than 5 arguments. */ | |
1723 | gcc_assert (isym->formal->next->next->next->next == NULL); | |
1724 | isym->resolve.f4 (&e, &argexpr, NULL, NULL, NULL); | |
1725 | } | |
0e7e7e6e | 1726 | } |
6de9cd9a | 1727 | } |
973ff4c0 TS |
1728 | |
1729 | if (gfc_option.flag_f2c | |
1730 | && ((e.ts.type == BT_REAL && e.ts.kind == gfc_default_real_kind) | |
1731 | || e.ts.type == BT_COMPLEX)) | |
1732 | { | |
1733 | /* Specific which needs a different implementation if f2c | |
1734 | calling conventions are used. */ | |
e6472bce | 1735 | sprintf (s, "_gfortran_f2c_specific%s", e.value.function.name); |
973ff4c0 TS |
1736 | } |
1737 | else | |
e6472bce | 1738 | sprintf (s, "_gfortran_specific%s", e.value.function.name); |
973ff4c0 | 1739 | |
6de9cd9a DN |
1740 | name = get_identifier (s); |
1741 | mangled_name = name; | |
1742 | } | |
1743 | else | |
1744 | { | |
1745 | name = gfc_sym_identifier (sym); | |
1746 | mangled_name = gfc_sym_mangled_function_id (sym); | |
1747 | } | |
1748 | ||
1749 | type = gfc_get_function_type (sym); | |
c2255bc4 AH |
1750 | fndecl = build_decl (input_location, |
1751 | FUNCTION_DECL, name, type); | |
6de9cd9a | 1752 | |
384f586a KT |
1753 | /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes; |
1754 | TREE_PUBLIC specifies whether a function is globally addressable (i.e. | |
dd5a833e | 1755 | the opposite of declaring a function as static in C). */ |
384f586a KT |
1756 | DECL_EXTERNAL (fndecl) = 1; |
1757 | TREE_PUBLIC (fndecl) = 1; | |
1758 | ||
43ce5e52 FXC |
1759 | attributes = add_attributes_to_decl (sym->attr, NULL_TREE); |
1760 | decl_attributes (&fndecl, attributes, 0); | |
1761 | ||
1762 | gfc_set_decl_assembler_name (fndecl, mangled_name); | |
6de9cd9a DN |
1763 | |
1764 | /* Set the context of this decl. */ | |
1765 | if (0 && sym->ns && sym->ns->proc_name) | |
1766 | { | |
1767 | /* TODO: Add external decls to the appropriate scope. */ | |
1768 | DECL_CONTEXT (fndecl) = sym->ns->proc_name->backend_decl; | |
1769 | } | |
1770 | else | |
1771 | { | |
f8d0aee5 | 1772 | /* Global declaration, e.g. intrinsic subroutine. */ |
6de9cd9a DN |
1773 | DECL_CONTEXT (fndecl) = NULL_TREE; |
1774 | } | |
1775 | ||
6de9cd9a DN |
1776 | /* Set attributes for PURE functions. A call to PURE function in the |
1777 | Fortran 95 sense is both pure and without side effects in the C | |
1778 | sense. */ | |
1779 | if (sym->attr.pure || sym->attr.elemental) | |
1780 | { | |
cf013e9f | 1781 | if (sym->attr.function && !gfc_return_by_reference (sym)) |
becfd6e5 | 1782 | DECL_PURE_P (fndecl) = 1; |
b7e6a6b3 TS |
1783 | /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT) |
1784 | parameters and don't use alternate returns (is this | |
1785 | allowed?). In that case, calls to them are meaningless, and | |
3d79abbd | 1786 | can be optimized away. See also in build_function_decl(). */ |
b7e6a6b3 | 1787 | TREE_SIDE_EFFECTS (fndecl) = 0; |
6de9cd9a DN |
1788 | } |
1789 | ||
fe58e076 TK |
1790 | /* Mark non-returning functions. */ |
1791 | if (sym->attr.noreturn) | |
1792 | TREE_THIS_VOLATILE(fndecl) = 1; | |
1793 | ||
6de9cd9a DN |
1794 | sym->backend_decl = fndecl; |
1795 | ||
1796 | if (DECL_CONTEXT (fndecl) == NULL_TREE) | |
1797 | pushdecl_top_level (fndecl); | |
1798 | ||
1799 | return fndecl; | |
1800 | } | |
1801 | ||
1802 | ||
1803 | /* Create a declaration for a procedure. For external functions (in the C | |
3d79abbd PB |
1804 | sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is |
1805 | a master function with alternate entry points. */ | |
6de9cd9a | 1806 | |
3d79abbd | 1807 | static void |
fb55ca75 | 1808 | build_function_decl (gfc_symbol * sym, bool global) |
6de9cd9a | 1809 | { |
08a6b8e0 | 1810 | tree fndecl, type, attributes; |
6de9cd9a | 1811 | symbol_attribute attr; |
3d79abbd | 1812 | tree result_decl; |
6de9cd9a DN |
1813 | gfc_formal_arglist *f; |
1814 | ||
6e45f57b | 1815 | gcc_assert (!sym->attr.external); |
6de9cd9a | 1816 | |
1d0134b3 JW |
1817 | if (sym->backend_decl) |
1818 | return; | |
1819 | ||
c8cc8542 PB |
1820 | /* Set the line and filename. sym->declared_at seems to point to the |
1821 | last statement for subroutines, but it'll do for now. */ | |
1822 | gfc_set_backend_locus (&sym->declared_at); | |
1823 | ||
6de9cd9a | 1824 | /* Allow only one nesting level. Allow public declarations. */ |
6e45f57b | 1825 | gcc_assert (current_function_decl == NULL_TREE |
e5b16755 RG |
1826 | || DECL_FILE_SCOPE_P (current_function_decl) |
1827 | || (TREE_CODE (DECL_CONTEXT (current_function_decl)) | |
1828 | == NAMESPACE_DECL)); | |
6de9cd9a DN |
1829 | |
1830 | type = gfc_get_function_type (sym); | |
c2255bc4 AH |
1831 | fndecl = build_decl (input_location, |
1832 | FUNCTION_DECL, gfc_sym_identifier (sym), type); | |
6de9cd9a | 1833 | |
43ce5e52 FXC |
1834 | attr = sym->attr; |
1835 | ||
384f586a KT |
1836 | /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes; |
1837 | TREE_PUBLIC specifies whether a function is globally addressable (i.e. | |
dd5a833e | 1838 | the opposite of declaring a function as static in C). */ |
384f586a KT |
1839 | DECL_EXTERNAL (fndecl) = 0; |
1840 | ||
1841 | if (!current_function_decl | |
5af6fa0b | 1842 | && !sym->attr.entry_master && !sym->attr.is_main_program |
cdd244b8 TB |
1843 | && (sym->attr.access != ACCESS_PRIVATE || sym->binding_label |
1844 | || sym->attr.public_used)) | |
384f586a KT |
1845 | TREE_PUBLIC (fndecl) = 1; |
1846 | ||
43ce5e52 FXC |
1847 | attributes = add_attributes_to_decl (attr, NULL_TREE); |
1848 | decl_attributes (&fndecl, attributes, 0); | |
1849 | ||
6de9cd9a | 1850 | /* Figure out the return type of the declared function, and build a |
f8d0aee5 | 1851 | RESULT_DECL for it. If this is a subroutine with alternate |
6de9cd9a | 1852 | returns, build a RESULT_DECL for it. */ |
6de9cd9a DN |
1853 | result_decl = NULL_TREE; |
1854 | /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */ | |
1855 | if (attr.function) | |
1856 | { | |
1857 | if (gfc_return_by_reference (sym)) | |
1858 | type = void_type_node; | |
1859 | else | |
1860 | { | |
1861 | if (sym->result != sym) | |
1862 | result_decl = gfc_sym_identifier (sym->result); | |
1863 | ||
1864 | type = TREE_TYPE (TREE_TYPE (fndecl)); | |
1865 | } | |
1866 | } | |
1867 | else | |
1868 | { | |
1869 | /* Look for alternate return placeholders. */ | |
1870 | int has_alternate_returns = 0; | |
1871 | for (f = sym->formal; f; f = f->next) | |
1872 | { | |
1873 | if (f->sym == NULL) | |
1874 | { | |
1875 | has_alternate_returns = 1; | |
1876 | break; | |
1877 | } | |
1878 | } | |
1879 | ||
1880 | if (has_alternate_returns) | |
1881 | type = integer_type_node; | |
1882 | else | |
1883 | type = void_type_node; | |
1884 | } | |
1885 | ||
c2255bc4 AH |
1886 | result_decl = build_decl (input_location, |
1887 | RESULT_DECL, result_decl, type); | |
b785f485 RH |
1888 | DECL_ARTIFICIAL (result_decl) = 1; |
1889 | DECL_IGNORED_P (result_decl) = 1; | |
6de9cd9a DN |
1890 | DECL_CONTEXT (result_decl) = fndecl; |
1891 | DECL_RESULT (fndecl) = result_decl; | |
1892 | ||
1893 | /* Don't call layout_decl for a RESULT_DECL. | |
f8d0aee5 | 1894 | layout_decl (result_decl, 0); */ |
6de9cd9a | 1895 | |
6de9cd9a | 1896 | /* TREE_STATIC means the function body is defined here. */ |
1d754240 | 1897 | TREE_STATIC (fndecl) = 1; |
6de9cd9a | 1898 | |
f8d0aee5 | 1899 | /* Set attributes for PURE functions. A call to a PURE function in the |
6de9cd9a DN |
1900 | Fortran 95 sense is both pure and without side effects in the C |
1901 | sense. */ | |
1902 | if (attr.pure || attr.elemental) | |
1903 | { | |
b7e6a6b3 | 1904 | /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments |
86bf520d | 1905 | including an alternate return. In that case it can also be |
1f2959f0 | 1906 | marked as PURE. See also in gfc_get_extern_function_decl(). */ |
a01db3bf | 1907 | if (attr.function && !gfc_return_by_reference (sym)) |
becfd6e5 | 1908 | DECL_PURE_P (fndecl) = 1; |
6de9cd9a DN |
1909 | TREE_SIDE_EFFECTS (fndecl) = 0; |
1910 | } | |
1911 | ||
08a6b8e0 | 1912 | |
6de9cd9a DN |
1913 | /* Layout the function declaration and put it in the binding level |
1914 | of the current function. */ | |
fb55ca75 | 1915 | |
cf651ca2 TB |
1916 | if (global |
1917 | || (sym->name[0] == '_' && strncmp ("__copy", sym->name, 6) == 0)) | |
fb55ca75 TB |
1918 | pushdecl_top_level (fndecl); |
1919 | else | |
1920 | pushdecl (fndecl); | |
3d79abbd | 1921 | |
e5b16755 RG |
1922 | /* Perform name mangling if this is a top level or module procedure. */ |
1923 | if (current_function_decl == NULL_TREE) | |
1924 | gfc_set_decl_assembler_name (fndecl, gfc_sym_mangled_function_id (sym)); | |
1925 | ||
3d79abbd PB |
1926 | sym->backend_decl = fndecl; |
1927 | } | |
1928 | ||
1929 | ||
1930 | /* Create the DECL_ARGUMENTS for a procedure. */ | |
1931 | ||
1932 | static void | |
1933 | create_function_arglist (gfc_symbol * sym) | |
1934 | { | |
1935 | tree fndecl; | |
1936 | gfc_formal_arglist *f; | |
417ab240 JJ |
1937 | tree typelist, hidden_typelist; |
1938 | tree arglist, hidden_arglist; | |
3d79abbd PB |
1939 | tree type; |
1940 | tree parm; | |
1941 | ||
1942 | fndecl = sym->backend_decl; | |
1943 | ||
1d754240 PB |
1944 | /* Build formal argument list. Make sure that their TREE_CONTEXT is |
1945 | the new FUNCTION_DECL node. */ | |
1d754240 | 1946 | arglist = NULL_TREE; |
417ab240 | 1947 | hidden_arglist = NULL_TREE; |
1d754240 | 1948 | typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); |
3d79abbd PB |
1949 | |
1950 | if (sym->attr.entry_master) | |
1951 | { | |
1952 | type = TREE_VALUE (typelist); | |
c2255bc4 AH |
1953 | parm = build_decl (input_location, |
1954 | PARM_DECL, get_identifier ("__entry"), type); | |
3d79abbd PB |
1955 | |
1956 | DECL_CONTEXT (parm) = fndecl; | |
1957 | DECL_ARG_TYPE (parm) = type; | |
1958 | TREE_READONLY (parm) = 1; | |
faf28b3a | 1959 | gfc_finish_decl (parm); |
3e978d30 | 1960 | DECL_ARTIFICIAL (parm) = 1; |
3d79abbd PB |
1961 | |
1962 | arglist = chainon (arglist, parm); | |
1963 | typelist = TREE_CHAIN (typelist); | |
1964 | } | |
1965 | ||
1d754240 | 1966 | if (gfc_return_by_reference (sym)) |
6de9cd9a | 1967 | { |
417ab240 | 1968 | tree type = TREE_VALUE (typelist), length = NULL; |
6de9cd9a | 1969 | |
1d754240 PB |
1970 | if (sym->ts.type == BT_CHARACTER) |
1971 | { | |
1d754240 | 1972 | /* Length of character result. */ |
417ab240 | 1973 | tree len_type = TREE_VALUE (TREE_CHAIN (typelist)); |
6de9cd9a | 1974 | |
c2255bc4 AH |
1975 | length = build_decl (input_location, |
1976 | PARM_DECL, | |
1d754240 | 1977 | get_identifier (".__result"), |
417ab240 | 1978 | len_type); |
bc21d315 | 1979 | if (!sym->ts.u.cl->length) |
1d754240 | 1980 | { |
bc21d315 | 1981 | sym->ts.u.cl->backend_decl = length; |
1d754240 | 1982 | TREE_USED (length) = 1; |
6de9cd9a | 1983 | } |
6e45f57b | 1984 | gcc_assert (TREE_CODE (length) == PARM_DECL); |
1d754240 | 1985 | DECL_CONTEXT (length) = fndecl; |
417ab240 | 1986 | DECL_ARG_TYPE (length) = len_type; |
1d754240 | 1987 | TREE_READONLY (length) = 1; |
ca0e9281 | 1988 | DECL_ARTIFICIAL (length) = 1; |
faf28b3a | 1989 | gfc_finish_decl (length); |
bc21d315 JW |
1990 | if (sym->ts.u.cl->backend_decl == NULL |
1991 | || sym->ts.u.cl->backend_decl == length) | |
417ab240 JJ |
1992 | { |
1993 | gfc_symbol *arg; | |
1994 | tree backend_decl; | |
6de9cd9a | 1995 | |
bc21d315 | 1996 | if (sym->ts.u.cl->backend_decl == NULL) |
417ab240 | 1997 | { |
c2255bc4 AH |
1998 | tree len = build_decl (input_location, |
1999 | VAR_DECL, | |
417ab240 JJ |
2000 | get_identifier ("..__result"), |
2001 | gfc_charlen_type_node); | |
2002 | DECL_ARTIFICIAL (len) = 1; | |
2003 | TREE_USED (len) = 1; | |
bc21d315 | 2004 | sym->ts.u.cl->backend_decl = len; |
417ab240 | 2005 | } |
6de9cd9a | 2006 | |
417ab240 JJ |
2007 | /* Make sure PARM_DECL type doesn't point to incomplete type. */ |
2008 | arg = sym->result ? sym->result : sym; | |
2009 | backend_decl = arg->backend_decl; | |
2010 | /* Temporary clear it, so that gfc_sym_type creates complete | |
2011 | type. */ | |
2012 | arg->backend_decl = NULL; | |
2013 | type = gfc_sym_type (arg); | |
2014 | arg->backend_decl = backend_decl; | |
2015 | type = build_reference_type (type); | |
2016 | } | |
2017 | } | |
6de9cd9a | 2018 | |
c2255bc4 AH |
2019 | parm = build_decl (input_location, |
2020 | PARM_DECL, get_identifier ("__result"), type); | |
6de9cd9a | 2021 | |
417ab240 JJ |
2022 | DECL_CONTEXT (parm) = fndecl; |
2023 | DECL_ARG_TYPE (parm) = TREE_VALUE (typelist); | |
2024 | TREE_READONLY (parm) = 1; | |
2025 | DECL_ARTIFICIAL (parm) = 1; | |
faf28b3a | 2026 | gfc_finish_decl (parm); |
6de9cd9a | 2027 | |
417ab240 JJ |
2028 | arglist = chainon (arglist, parm); |
2029 | typelist = TREE_CHAIN (typelist); | |
6de9cd9a | 2030 | |
417ab240 JJ |
2031 | if (sym->ts.type == BT_CHARACTER) |
2032 | { | |
2033 | gfc_allocate_lang_decl (parm); | |
2034 | arglist = chainon (arglist, length); | |
1d754240 PB |
2035 | typelist = TREE_CHAIN (typelist); |
2036 | } | |
2037 | } | |
6de9cd9a | 2038 | |
417ab240 JJ |
2039 | hidden_typelist = typelist; |
2040 | for (f = sym->formal; f; f = f->next) | |
2041 | if (f->sym != NULL) /* Ignore alternate returns. */ | |
2042 | hidden_typelist = TREE_CHAIN (hidden_typelist); | |
2043 | ||
1d754240 PB |
2044 | for (f = sym->formal; f; f = f->next) |
2045 | { | |
2046 | char name[GFC_MAX_SYMBOL_LEN + 2]; | |
417ab240 | 2047 | |
1d754240 PB |
2048 | /* Ignore alternate returns. */ |
2049 | if (f->sym == NULL) | |
2050 | continue; | |
6de9cd9a | 2051 | |
1d754240 | 2052 | type = TREE_VALUE (typelist); |
6de9cd9a | 2053 | |
33215bb3 TB |
2054 | if (f->sym->ts.type == BT_CHARACTER |
2055 | && (!sym->attr.is_bind_c || sym->attr.entry_master)) | |
417ab240 JJ |
2056 | { |
2057 | tree len_type = TREE_VALUE (hidden_typelist); | |
2058 | tree length = NULL_TREE; | |
8d51f26f PT |
2059 | if (!f->sym->ts.deferred) |
2060 | gcc_assert (len_type == gfc_charlen_type_node); | |
2061 | else | |
2062 | gcc_assert (POINTER_TYPE_P (len_type)); | |
417ab240 JJ |
2063 | |
2064 | strcpy (&name[1], f->sym->name); | |
2065 | name[0] = '_'; | |
c2255bc4 AH |
2066 | length = build_decl (input_location, |
2067 | PARM_DECL, get_identifier (name), len_type); | |
6de9cd9a | 2068 | |
417ab240 JJ |
2069 | hidden_arglist = chainon (hidden_arglist, length); |
2070 | DECL_CONTEXT (length) = fndecl; | |
2071 | DECL_ARTIFICIAL (length) = 1; | |
2072 | DECL_ARG_TYPE (length) = len_type; | |
2073 | TREE_READONLY (length) = 1; | |
faf28b3a | 2074 | gfc_finish_decl (length); |
6de9cd9a | 2075 | |
cadb8f42 | 2076 | /* Remember the passed value. */ |
bc21d315 | 2077 | if (f->sym->ts.u.cl->passed_length != NULL) |
3ba558db TB |
2078 | { |
2079 | /* This can happen if the same type is used for multiple | |
2080 | arguments. We need to copy cl as otherwise | |
2081 | cl->passed_length gets overwritten. */ | |
b76e28c6 | 2082 | f->sym->ts.u.cl = gfc_new_charlen (f->sym->ns, f->sym->ts.u.cl); |
3ba558db | 2083 | } |
bc21d315 | 2084 | f->sym->ts.u.cl->passed_length = length; |
6de9cd9a | 2085 | |
417ab240 | 2086 | /* Use the passed value for assumed length variables. */ |
bc21d315 | 2087 | if (!f->sym->ts.u.cl->length) |
6de9cd9a | 2088 | { |
417ab240 | 2089 | TREE_USED (length) = 1; |
bc21d315 JW |
2090 | gcc_assert (!f->sym->ts.u.cl->backend_decl); |
2091 | f->sym->ts.u.cl->backend_decl = length; | |
417ab240 JJ |
2092 | } |
2093 | ||
2094 | hidden_typelist = TREE_CHAIN (hidden_typelist); | |
2095 | ||
bc21d315 JW |
2096 | if (f->sym->ts.u.cl->backend_decl == NULL |
2097 | || f->sym->ts.u.cl->backend_decl == length) | |
417ab240 | 2098 | { |
bc21d315 | 2099 | if (f->sym->ts.u.cl->backend_decl == NULL) |
417ab240 JJ |
2100 | gfc_create_string_length (f->sym); |
2101 | ||
2102 | /* Make sure PARM_DECL type doesn't point to incomplete type. */ | |
2103 | if (f->sym->attr.flavor == FL_PROCEDURE) | |
2104 | type = build_pointer_type (gfc_get_function_type (f->sym)); | |
2105 | else | |
2106 | type = gfc_sym_type (f->sym); | |
6de9cd9a | 2107 | } |
6de9cd9a DN |
2108 | } |
2109 | ||
417ab240 JJ |
2110 | /* For non-constant length array arguments, make sure they use |
2111 | a different type node from TYPE_ARG_TYPES type. */ | |
2112 | if (f->sym->attr.dimension | |
2113 | && type == TREE_VALUE (typelist) | |
2114 | && TREE_CODE (type) == POINTER_TYPE | |
2115 | && GFC_ARRAY_TYPE_P (type) | |
2116 | && f->sym->as->type != AS_ASSUMED_SIZE | |
2117 | && ! COMPLETE_TYPE_P (TREE_TYPE (type))) | |
2118 | { | |
2119 | if (f->sym->attr.flavor == FL_PROCEDURE) | |
2120 | type = build_pointer_type (gfc_get_function_type (f->sym)); | |
2121 | else | |
2122 | type = gfc_sym_type (f->sym); | |
2123 | } | |
2124 | ||
8fb74da4 JW |
2125 | if (f->sym->attr.proc_pointer) |
2126 | type = build_pointer_type (type); | |
2127 | ||
c28d1d9b TB |
2128 | if (f->sym->attr.volatile_) |
2129 | type = build_qualified_type (type, TYPE_QUAL_VOLATILE); | |
2130 | ||
df2fba9e | 2131 | /* Build the argument declaration. */ |
c2255bc4 AH |
2132 | parm = build_decl (input_location, |
2133 | PARM_DECL, gfc_sym_identifier (f->sym), type); | |
417ab240 | 2134 | |
c28d1d9b TB |
2135 | if (f->sym->attr.volatile_) |
2136 | { | |
2137 | TREE_THIS_VOLATILE (parm) = 1; | |
2138 | TREE_SIDE_EFFECTS (parm) = 1; | |
2139 | } | |
2140 | ||
417ab240 JJ |
2141 | /* Fill in arg stuff. */ |
2142 | DECL_CONTEXT (parm) = fndecl; | |
2143 | DECL_ARG_TYPE (parm) = TREE_VALUE (typelist); | |
2144 | /* All implementation args are read-only. */ | |
2145 | TREE_READONLY (parm) = 1; | |
714495cd JJ |
2146 | if (POINTER_TYPE_P (type) |
2147 | && (!f->sym->attr.proc_pointer | |
2148 | && f->sym->attr.flavor != FL_PROCEDURE)) | |
2149 | DECL_BY_REFERENCE (parm) = 1; | |
417ab240 | 2150 | |
faf28b3a | 2151 | gfc_finish_decl (parm); |
417ab240 JJ |
2152 | |
2153 | f->sym->backend_decl = parm; | |
2154 | ||
aa13dc3c TB |
2155 | /* Coarrays which are descriptorless or assumed-shape pass with |
2156 | -fcoarray=lib the token and the offset as hidden arguments. */ | |
0c53708e TB |
2157 | if (f->sym->attr.codimension |
2158 | && gfc_option.coarray == GFC_FCOARRAY_LIB | |
aa13dc3c | 2159 | && !f->sym->attr.allocatable) |
0c53708e TB |
2160 | { |
2161 | tree caf_type; | |
2162 | tree token; | |
2163 | tree offset; | |
2164 | ||
2165 | gcc_assert (f->sym->backend_decl != NULL_TREE | |
2166 | && !sym->attr.is_bind_c); | |
2167 | caf_type = TREE_TYPE (f->sym->backend_decl); | |
2168 | ||
0c53708e TB |
2169 | token = build_decl (input_location, PARM_DECL, |
2170 | create_tmp_var_name ("caf_token"), | |
2171 | build_qualified_type (pvoid_type_node, | |
2172 | TYPE_QUAL_RESTRICT)); | |
aa13dc3c TB |
2173 | if (f->sym->as->type == AS_ASSUMED_SHAPE) |
2174 | { | |
2175 | gcc_assert (DECL_LANG_SPECIFIC (f->sym->backend_decl) == NULL | |
2176 | || GFC_DECL_TOKEN (f->sym->backend_decl) == NULL_TREE); | |
2177 | if (DECL_LANG_SPECIFIC (f->sym->backend_decl) == NULL) | |
2178 | gfc_allocate_lang_decl (f->sym->backend_decl); | |
2179 | GFC_DECL_TOKEN (f->sym->backend_decl) = token; | |
2180 | } | |
2181 | else | |
2182 | { | |
2183 | gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) == NULL_TREE); | |
2184 | GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) = token; | |
2185 | } | |
2186 | ||
0c53708e TB |
2187 | DECL_CONTEXT (token) = fndecl; |
2188 | DECL_ARTIFICIAL (token) = 1; | |
2189 | DECL_ARG_TYPE (token) = TREE_VALUE (typelist); | |
2190 | TREE_READONLY (token) = 1; | |
2191 | hidden_arglist = chainon (hidden_arglist, token); | |
2192 | gfc_finish_decl (token); | |
2193 | ||
0c53708e TB |
2194 | offset = build_decl (input_location, PARM_DECL, |
2195 | create_tmp_var_name ("caf_offset"), | |
2196 | gfc_array_index_type); | |
2197 | ||
aa13dc3c TB |
2198 | if (f->sym->as->type == AS_ASSUMED_SHAPE) |
2199 | { | |
2200 | gcc_assert (GFC_DECL_CAF_OFFSET (f->sym->backend_decl) | |
2201 | == NULL_TREE); | |
2202 | GFC_DECL_CAF_OFFSET (f->sym->backend_decl) = offset; | |
2203 | } | |
2204 | else | |
2205 | { | |
2206 | gcc_assert (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) == NULL_TREE); | |
2207 | GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) = offset; | |
2208 | } | |
0c53708e TB |
2209 | DECL_CONTEXT (offset) = fndecl; |
2210 | DECL_ARTIFICIAL (offset) = 1; | |
2211 | DECL_ARG_TYPE (offset) = TREE_VALUE (typelist); | |
2212 | TREE_READONLY (offset) = 1; | |
2213 | hidden_arglist = chainon (hidden_arglist, offset); | |
2214 | gfc_finish_decl (offset); | |
2215 | } | |
2216 | ||
417ab240 | 2217 | arglist = chainon (arglist, parm); |
1d754240 | 2218 | typelist = TREE_CHAIN (typelist); |
6de9cd9a | 2219 | } |
1d754240 | 2220 | |
7861a5ce TB |
2221 | /* Add the hidden string length parameters, unless the procedure |
2222 | is bind(C). */ | |
2223 | if (!sym->attr.is_bind_c) | |
2224 | arglist = chainon (arglist, hidden_arglist); | |
417ab240 | 2225 | |
884d2e6b SE |
2226 | gcc_assert (hidden_typelist == NULL_TREE |
2227 | || TREE_VALUE (hidden_typelist) == void_type_node); | |
1d754240 | 2228 | DECL_ARGUMENTS (fndecl) = arglist; |
3d79abbd | 2229 | } |
1d754240 | 2230 | |
3d79abbd PB |
2231 | /* Do the setup necessary before generating the body of a function. */ |
2232 | ||
2233 | static void | |
2234 | trans_function_start (gfc_symbol * sym) | |
2235 | { | |
2236 | tree fndecl; | |
2237 | ||
2238 | fndecl = sym->backend_decl; | |
2239 | ||
f8d0aee5 | 2240 | /* Let GCC know the current scope is this function. */ |
3d79abbd PB |
2241 | current_function_decl = fndecl; |
2242 | ||
f8d0aee5 | 2243 | /* Let the world know what we're about to do. */ |
3d79abbd PB |
2244 | announce_function (fndecl); |
2245 | ||
e5b16755 | 2246 | if (DECL_FILE_SCOPE_P (fndecl)) |
3d79abbd | 2247 | { |
f8d0aee5 | 2248 | /* Create RTL for function declaration. */ |
3d79abbd PB |
2249 | rest_of_decl_compilation (fndecl, 1, 0); |
2250 | } | |
2251 | ||
f8d0aee5 | 2252 | /* Create RTL for function definition. */ |
3d79abbd PB |
2253 | make_decl_rtl (fndecl); |
2254 | ||
3d79abbd PB |
2255 | init_function_start (fndecl); |
2256 | ||
f8d0aee5 | 2257 | /* function.c requires a push at the start of the function. */ |
87a60f68 | 2258 | pushlevel (); |
3d79abbd PB |
2259 | } |
2260 | ||
2261 | /* Create thunks for alternate entry points. */ | |
2262 | ||
2263 | static void | |
fb55ca75 | 2264 | build_entry_thunks (gfc_namespace * ns, bool global) |
3d79abbd PB |
2265 | { |
2266 | gfc_formal_arglist *formal; | |
2267 | gfc_formal_arglist *thunk_formal; | |
2268 | gfc_entry_list *el; | |
2269 | gfc_symbol *thunk_sym; | |
2270 | stmtblock_t body; | |
2271 | tree thunk_fndecl; | |
3d79abbd | 2272 | tree tmp; |
c8cc8542 | 2273 | locus old_loc; |
3d79abbd PB |
2274 | |
2275 | /* This should always be a toplevel function. */ | |
6e45f57b | 2276 | gcc_assert (current_function_decl == NULL_TREE); |
3d79abbd | 2277 | |
363aab21 | 2278 | gfc_save_backend_locus (&old_loc); |
3d79abbd PB |
2279 | for (el = ns->entries; el; el = el->next) |
2280 | { | |
3bb06db4 NF |
2281 | VEC(tree,gc) *args = NULL; |
2282 | VEC(tree,gc) *string_args = NULL; | |
2283 | ||
3d79abbd PB |
2284 | thunk_sym = el->sym; |
2285 | ||
fb55ca75 | 2286 | build_function_decl (thunk_sym, global); |
3d79abbd PB |
2287 | create_function_arglist (thunk_sym); |
2288 | ||
2289 | trans_function_start (thunk_sym); | |
2290 | ||
2291 | thunk_fndecl = thunk_sym->backend_decl; | |
2292 | ||
c7c79a09 | 2293 | gfc_init_block (&body); |
3d79abbd | 2294 | |
f8d0aee5 | 2295 | /* Pass extra parameter identifying this entry point. */ |
7d60be94 | 2296 | tmp = build_int_cst (gfc_array_index_type, el->id); |
3bb06db4 | 2297 | VEC_safe_push (tree, gc, args, tmp); |
3d79abbd | 2298 | |
d198b59a JJ |
2299 | if (thunk_sym->attr.function) |
2300 | { | |
2301 | if (gfc_return_by_reference (ns->proc_name)) | |
2302 | { | |
2303 | tree ref = DECL_ARGUMENTS (current_function_decl); | |
3bb06db4 | 2304 | VEC_safe_push (tree, gc, args, ref); |
d198b59a | 2305 | if (ns->proc_name->ts.type == BT_CHARACTER) |
910ad8de | 2306 | VEC_safe_push (tree, gc, args, DECL_CHAIN (ref)); |
d198b59a JJ |
2307 | } |
2308 | } | |
2309 | ||
3d79abbd PB |
2310 | for (formal = ns->proc_name->formal; formal; formal = formal->next) |
2311 | { | |
d198b59a JJ |
2312 | /* Ignore alternate returns. */ |
2313 | if (formal->sym == NULL) | |
2314 | continue; | |
2315 | ||
3d79abbd PB |
2316 | /* We don't have a clever way of identifying arguments, so resort to |
2317 | a brute-force search. */ | |
2318 | for (thunk_formal = thunk_sym->formal; | |
2319 | thunk_formal; | |
2320 | thunk_formal = thunk_formal->next) | |
2321 | { | |
2322 | if (thunk_formal->sym == formal->sym) | |
2323 | break; | |
2324 | } | |
2325 | ||
2326 | if (thunk_formal) | |
2327 | { | |
2328 | /* Pass the argument. */ | |
3e978d30 | 2329 | DECL_ARTIFICIAL (thunk_formal->sym->backend_decl) = 1; |
3bb06db4 | 2330 | VEC_safe_push (tree, gc, args, thunk_formal->sym->backend_decl); |
3d79abbd PB |
2331 | if (formal->sym->ts.type == BT_CHARACTER) |
2332 | { | |
bc21d315 | 2333 | tmp = thunk_formal->sym->ts.u.cl->backend_decl; |
3bb06db4 | 2334 | VEC_safe_push (tree, gc, string_args, tmp); |
3d79abbd PB |
2335 | } |
2336 | } | |
2337 | else | |
2338 | { | |
2339 | /* Pass NULL for a missing argument. */ | |
3bb06db4 | 2340 | VEC_safe_push (tree, gc, args, null_pointer_node); |
3d79abbd PB |
2341 | if (formal->sym->ts.type == BT_CHARACTER) |
2342 | { | |
c3238e32 | 2343 | tmp = build_int_cst (gfc_charlen_type_node, 0); |
3bb06db4 | 2344 | VEC_safe_push (tree, gc, string_args, tmp); |
3d79abbd PB |
2345 | } |
2346 | } | |
2347 | } | |
2348 | ||
2349 | /* Call the master function. */ | |
3bb06db4 | 2350 | VEC_safe_splice (tree, gc, args, string_args); |
3d79abbd | 2351 | tmp = ns->proc_name->backend_decl; |
3bb06db4 | 2352 | tmp = build_call_expr_loc_vec (input_location, tmp, args); |
d198b59a JJ |
2353 | if (ns->proc_name->attr.mixed_entry_master) |
2354 | { | |
2355 | tree union_decl, field; | |
2356 | tree master_type = TREE_TYPE (ns->proc_name->backend_decl); | |
2357 | ||
c2255bc4 AH |
2358 | union_decl = build_decl (input_location, |
2359 | VAR_DECL, get_identifier ("__result"), | |
d198b59a JJ |
2360 | TREE_TYPE (master_type)); |
2361 | DECL_ARTIFICIAL (union_decl) = 1; | |
2362 | DECL_EXTERNAL (union_decl) = 0; | |
2363 | TREE_PUBLIC (union_decl) = 0; | |
2364 | TREE_USED (union_decl) = 1; | |
2365 | layout_decl (union_decl, 0); | |
2366 | pushdecl (union_decl); | |
2367 | ||
2368 | DECL_CONTEXT (union_decl) = current_function_decl; | |
bc98ed60 TB |
2369 | tmp = fold_build2_loc (input_location, MODIFY_EXPR, |
2370 | TREE_TYPE (union_decl), union_decl, tmp); | |
d198b59a JJ |
2371 | gfc_add_expr_to_block (&body, tmp); |
2372 | ||
2373 | for (field = TYPE_FIELDS (TREE_TYPE (union_decl)); | |
910ad8de | 2374 | field; field = DECL_CHAIN (field)) |
d198b59a JJ |
2375 | if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)), |
2376 | thunk_sym->result->name) == 0) | |
2377 | break; | |
2378 | gcc_assert (field != NULL_TREE); | |
bc98ed60 TB |
2379 | tmp = fold_build3_loc (input_location, COMPONENT_REF, |
2380 | TREE_TYPE (field), union_decl, field, | |
2381 | NULL_TREE); | |
2382 | tmp = fold_build2_loc (input_location, MODIFY_EXPR, | |
44855d8c TS |
2383 | TREE_TYPE (DECL_RESULT (current_function_decl)), |
2384 | DECL_RESULT (current_function_decl), tmp); | |
d198b59a JJ |
2385 | tmp = build1_v (RETURN_EXPR, tmp); |
2386 | } | |
2387 | else if (TREE_TYPE (DECL_RESULT (current_function_decl)) | |
2388 | != void_type_node) | |
2389 | { | |
bc98ed60 | 2390 | tmp = fold_build2_loc (input_location, MODIFY_EXPR, |
44855d8c TS |
2391 | TREE_TYPE (DECL_RESULT (current_function_decl)), |
2392 | DECL_RESULT (current_function_decl), tmp); | |
d198b59a JJ |
2393 | tmp = build1_v (RETURN_EXPR, tmp); |
2394 | } | |
3d79abbd PB |
2395 | gfc_add_expr_to_block (&body, tmp); |
2396 | ||
2397 | /* Finish off this function and send it for code generation. */ | |
2398 | DECL_SAVED_TREE (thunk_fndecl) = gfc_finish_block (&body); | |
c7c79a09 | 2399 | tmp = getdecls (); |
87a60f68 | 2400 | poplevel (1, 1); |
3d79abbd | 2401 | BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl)) = thunk_fndecl; |
c7c79a09 JJ |
2402 | DECL_SAVED_TREE (thunk_fndecl) |
2403 | = build3_v (BIND_EXPR, tmp, DECL_SAVED_TREE (thunk_fndecl), | |
2404 | DECL_INITIAL (thunk_fndecl)); | |
3d79abbd PB |
2405 | |
2406 | /* Output the GENERIC tree. */ | |
2407 | dump_function (TDI_original, thunk_fndecl); | |
2408 | ||
2409 | /* Store the end of the function, so that we get good line number | |
2410 | info for the epilogue. */ | |
2411 | cfun->function_end_locus = input_location; | |
2412 | ||
2413 | /* We're leaving the context of this function, so zap cfun. | |
2414 | It's still in DECL_STRUCT_FUNCTION, and we'll restore it in | |
2415 | tree_rest_of_compilation. */ | |
db2960f4 | 2416 | set_cfun (NULL); |
3d79abbd PB |
2417 | |
2418 | current_function_decl = NULL_TREE; | |
2419 | ||
9618fb3c | 2420 | cgraph_finalize_function (thunk_fndecl, true); |
3d79abbd PB |
2421 | |
2422 | /* We share the symbols in the formal argument list with other entry | |
2423 | points and the master function. Clear them so that they are | |
2424 | recreated for each function. */ | |
2425 | for (formal = thunk_sym->formal; formal; formal = formal->next) | |
d198b59a JJ |
2426 | if (formal->sym != NULL) /* Ignore alternate returns. */ |
2427 | { | |
2428 | formal->sym->backend_decl = NULL_TREE; | |
2429 | if (formal->sym->ts.type == BT_CHARACTER) | |
bc21d315 | 2430 | formal->sym->ts.u.cl->backend_decl = NULL_TREE; |
d198b59a JJ |
2431 | } |
2432 | ||
2433 | if (thunk_sym->attr.function) | |
3d79abbd | 2434 | { |
d198b59a | 2435 | if (thunk_sym->ts.type == BT_CHARACTER) |
bc21d315 | 2436 | thunk_sym->ts.u.cl->backend_decl = NULL_TREE; |
d198b59a | 2437 | if (thunk_sym->result->ts.type == BT_CHARACTER) |
bc21d315 | 2438 | thunk_sym->result->ts.u.cl->backend_decl = NULL_TREE; |
3d79abbd PB |
2439 | } |
2440 | } | |
c8cc8542 | 2441 | |
363aab21 | 2442 | gfc_restore_backend_locus (&old_loc); |
3d79abbd PB |
2443 | } |
2444 | ||
2445 | ||
2446 | /* Create a decl for a function, and create any thunks for alternate entry | |
fb55ca75 TB |
2447 | points. If global is true, generate the function in the global binding |
2448 | level, otherwise in the current binding level (which can be global). */ | |
3d79abbd PB |
2449 | |
2450 | void | |
fb55ca75 | 2451 | gfc_create_function_decl (gfc_namespace * ns, bool global) |
3d79abbd PB |
2452 | { |
2453 | /* Create a declaration for the master function. */ | |
fb55ca75 | 2454 | build_function_decl (ns->proc_name, global); |
3d79abbd | 2455 | |
f8d0aee5 | 2456 | /* Compile the entry thunks. */ |
3d79abbd | 2457 | if (ns->entries) |
fb55ca75 | 2458 | build_entry_thunks (ns, global); |
3d79abbd PB |
2459 | |
2460 | /* Now create the read argument list. */ | |
2461 | create_function_arglist (ns->proc_name); | |
2462 | } | |
2463 | ||
5f20c93a | 2464 | /* Return the decl used to hold the function return value. If |
da4c6ed8 | 2465 | parent_flag is set, the context is the parent_scope. */ |
6de9cd9a DN |
2466 | |
2467 | tree | |
5f20c93a | 2468 | gfc_get_fake_result_decl (gfc_symbol * sym, int parent_flag) |
6de9cd9a | 2469 | { |
5f20c93a PT |
2470 | tree decl; |
2471 | tree length; | |
2472 | tree this_fake_result_decl; | |
2473 | tree this_function_decl; | |
6de9cd9a DN |
2474 | |
2475 | char name[GFC_MAX_SYMBOL_LEN + 10]; | |
2476 | ||
5f20c93a PT |
2477 | if (parent_flag) |
2478 | { | |
2479 | this_fake_result_decl = parent_fake_result_decl; | |
2480 | this_function_decl = DECL_CONTEXT (current_function_decl); | |
2481 | } | |
2482 | else | |
2483 | { | |
2484 | this_fake_result_decl = current_fake_result_decl; | |
2485 | this_function_decl = current_function_decl; | |
2486 | } | |
2487 | ||
d198b59a | 2488 | if (sym |
5f20c93a | 2489 | && sym->ns->proc_name->backend_decl == this_function_decl |
417ab240 | 2490 | && sym->ns->proc_name->attr.entry_master |
d198b59a JJ |
2491 | && sym != sym->ns->proc_name) |
2492 | { | |
417ab240 | 2493 | tree t = NULL, var; |
5f20c93a PT |
2494 | if (this_fake_result_decl != NULL) |
2495 | for (t = TREE_CHAIN (this_fake_result_decl); t; t = TREE_CHAIN (t)) | |
417ab240 JJ |
2496 | if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t)), sym->name) == 0) |
2497 | break; | |
2498 | if (t) | |
2499 | return TREE_VALUE (t); | |
5f20c93a PT |
2500 | decl = gfc_get_fake_result_decl (sym->ns->proc_name, parent_flag); |
2501 | ||
2502 | if (parent_flag) | |
2503 | this_fake_result_decl = parent_fake_result_decl; | |
2504 | else | |
2505 | this_fake_result_decl = current_fake_result_decl; | |
2506 | ||
417ab240 | 2507 | if (decl && sym->ns->proc_name->attr.mixed_entry_master) |
d198b59a JJ |
2508 | { |
2509 | tree field; | |
2510 | ||
2511 | for (field = TYPE_FIELDS (TREE_TYPE (decl)); | |
910ad8de | 2512 | field; field = DECL_CHAIN (field)) |
d198b59a JJ |
2513 | if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)), |
2514 | sym->name) == 0) | |
2515 | break; | |
2516 | ||
2517 | gcc_assert (field != NULL_TREE); | |
bc98ed60 TB |
2518 | decl = fold_build3_loc (input_location, COMPONENT_REF, |
2519 | TREE_TYPE (field), decl, field, NULL_TREE); | |
d198b59a | 2520 | } |
5f20c93a PT |
2521 | |
2522 | var = create_tmp_var_raw (TREE_TYPE (decl), sym->name); | |
2523 | if (parent_flag) | |
2524 | gfc_add_decl_to_parent_function (var); | |
2525 | else | |
2526 | gfc_add_decl_to_function (var); | |
2527 | ||
417ab240 JJ |
2528 | SET_DECL_VALUE_EXPR (var, decl); |
2529 | DECL_HAS_VALUE_EXPR_P (var) = 1; | |
4b8ae4db | 2530 | GFC_DECL_RESULT (var) = 1; |
5f20c93a PT |
2531 | |
2532 | TREE_CHAIN (this_fake_result_decl) | |
2533 | = tree_cons (get_identifier (sym->name), var, | |
2534 | TREE_CHAIN (this_fake_result_decl)); | |
417ab240 | 2535 | return var; |
d198b59a JJ |
2536 | } |
2537 | ||
5f20c93a PT |
2538 | if (this_fake_result_decl != NULL_TREE) |
2539 | return TREE_VALUE (this_fake_result_decl); | |
6de9cd9a DN |
2540 | |
2541 | /* Only when gfc_get_fake_result_decl is called by gfc_trans_return, | |
2542 | sym is NULL. */ | |
2543 | if (!sym) | |
2544 | return NULL_TREE; | |
2545 | ||
417ab240 | 2546 | if (sym->ts.type == BT_CHARACTER) |
6de9cd9a | 2547 | { |
bc21d315 | 2548 | if (sym->ts.u.cl->backend_decl == NULL_TREE) |
417ab240 JJ |
2549 | length = gfc_create_string_length (sym); |
2550 | else | |
bc21d315 | 2551 | length = sym->ts.u.cl->backend_decl; |
417ab240 JJ |
2552 | if (TREE_CODE (length) == VAR_DECL |
2553 | && DECL_CONTEXT (length) == NULL_TREE) | |
a7d6b765 | 2554 | gfc_add_decl_to_function (length); |
6de9cd9a DN |
2555 | } |
2556 | ||
2557 | if (gfc_return_by_reference (sym)) | |
2558 | { | |
5f20c93a | 2559 | decl = DECL_ARGUMENTS (this_function_decl); |
d198b59a | 2560 | |
5f20c93a | 2561 | if (sym->ns->proc_name->backend_decl == this_function_decl |
d198b59a | 2562 | && sym->ns->proc_name->attr.entry_master) |
910ad8de | 2563 | decl = DECL_CHAIN (decl); |
6de9cd9a DN |
2564 | |
2565 | TREE_USED (decl) = 1; | |
2566 | if (sym->as) | |
2567 | decl = gfc_build_dummy_array_decl (sym, decl); | |
2568 | } | |
2569 | else | |
2570 | { | |
2571 | sprintf (name, "__result_%.20s", | |
5f20c93a | 2572 | IDENTIFIER_POINTER (DECL_NAME (this_function_decl))); |
6de9cd9a | 2573 | |
da4c6ed8 | 2574 | if (!sym->attr.mixed_entry_master && sym->attr.function) |
88e09c79 | 2575 | decl = build_decl (DECL_SOURCE_LOCATION (this_function_decl), |
c2255bc4 | 2576 | VAR_DECL, get_identifier (name), |
da4c6ed8 TS |
2577 | gfc_sym_type (sym)); |
2578 | else | |
88e09c79 | 2579 | decl = build_decl (DECL_SOURCE_LOCATION (this_function_decl), |
c2255bc4 | 2580 | VAR_DECL, get_identifier (name), |
da4c6ed8 | 2581 | TREE_TYPE (TREE_TYPE (this_function_decl))); |
6de9cd9a DN |
2582 | DECL_ARTIFICIAL (decl) = 1; |
2583 | DECL_EXTERNAL (decl) = 0; | |
2584 | TREE_PUBLIC (decl) = 0; | |
2585 | TREE_USED (decl) = 1; | |
6c7a4dfd | 2586 | GFC_DECL_RESULT (decl) = 1; |
c55cebda | 2587 | TREE_ADDRESSABLE (decl) = 1; |
6de9cd9a DN |
2588 | |
2589 | layout_decl (decl, 0); | |
2590 | ||
5f20c93a PT |
2591 | if (parent_flag) |
2592 | gfc_add_decl_to_parent_function (decl); | |
2593 | else | |
2594 | gfc_add_decl_to_function (decl); | |
6de9cd9a DN |
2595 | } |
2596 | ||
5f20c93a PT |
2597 | if (parent_flag) |
2598 | parent_fake_result_decl = build_tree_list (NULL, decl); | |
2599 | else | |
2600 | current_fake_result_decl = build_tree_list (NULL, decl); | |
6de9cd9a DN |
2601 | |
2602 | return decl; | |
2603 | } | |
2604 | ||
2605 | ||
2606 | /* Builds a function decl. The remaining parameters are the types of the | |
2607 | function arguments. Negative nargs indicates a varargs function. */ | |
2608 | ||
0b7b376d RG |
2609 | static tree |
2610 | build_library_function_decl_1 (tree name, const char *spec, | |
2611 | tree rettype, int nargs, va_list p) | |
6de9cd9a | 2612 | { |
6c32445b | 2613 | VEC(tree,gc) *arglist; |
6de9cd9a DN |
2614 | tree fntype; |
2615 | tree fndecl; | |
6de9cd9a DN |
2616 | int n; |
2617 | ||
2618 | /* Library functions must be declared with global scope. */ | |
6e45f57b | 2619 | gcc_assert (current_function_decl == NULL_TREE); |
6de9cd9a | 2620 | |
6de9cd9a | 2621 | /* Create a list of the argument types. */ |
6c32445b NF |
2622 | arglist = VEC_alloc (tree, gc, abs (nargs)); |
2623 | for (n = abs (nargs); n > 0; n--) | |
6de9cd9a | 2624 | { |
6c32445b NF |
2625 | tree argtype = va_arg (p, tree); |
2626 | VEC_quick_push (tree, arglist, argtype); | |
6de9cd9a DN |
2627 | } |
2628 | ||
2629 | /* Build the function type and decl. */ | |
6c32445b NF |
2630 | if (nargs >= 0) |
2631 | fntype = build_function_type_vec (rettype, arglist); | |
2632 | else | |
2633 | fntype = build_varargs_function_type_vec (rettype, arglist); | |
0b7b376d RG |
2634 | if (spec) |
2635 | { | |
2636 | tree attr_args = build_tree_list (NULL_TREE, | |
2637 | build_string (strlen (spec), spec)); | |
2638 | tree attrs = tree_cons (get_identifier ("fn spec"), | |
2639 | attr_args, TYPE_ATTRIBUTES (fntype)); | |
2640 | fntype = build_type_attribute_variant (fntype, attrs); | |
2641 | } | |
c2255bc4 AH |
2642 | fndecl = build_decl (input_location, |
2643 | FUNCTION_DECL, name, fntype); | |
6de9cd9a DN |
2644 | |
2645 | /* Mark this decl as external. */ | |
2646 | DECL_EXTERNAL (fndecl) = 1; | |
2647 | TREE_PUBLIC (fndecl) = 1; | |
2648 | ||
6de9cd9a DN |
2649 | pushdecl (fndecl); |
2650 | ||
0e6df31e | 2651 | rest_of_decl_compilation (fndecl, 1, 0); |
6de9cd9a DN |
2652 | |
2653 | return fndecl; | |
2654 | } | |
2655 | ||
0b7b376d RG |
2656 | /* Builds a function decl. The remaining parameters are the types of the |
2657 | function arguments. Negative nargs indicates a varargs function. */ | |
2658 | ||
2659 | tree | |
2660 | gfc_build_library_function_decl (tree name, tree rettype, int nargs, ...) | |
2661 | { | |
2662 | tree ret; | |
2663 | va_list args; | |
2664 | va_start (args, nargs); | |
2665 | ret = build_library_function_decl_1 (name, NULL, rettype, nargs, args); | |
2666 | va_end (args); | |
2667 | return ret; | |
2668 | } | |
2669 | ||
2670 | /* Builds a function decl. The remaining parameters are the types of the | |
2671 | function arguments. Negative nargs indicates a varargs function. | |
2672 | The SPEC parameter specifies the function argument and return type | |
2673 | specification according to the fnspec function type attribute. */ | |
2674 | ||
3f34855a | 2675 | tree |
0b7b376d RG |
2676 | gfc_build_library_function_decl_with_spec (tree name, const char *spec, |
2677 | tree rettype, int nargs, ...) | |
2678 | { | |
2679 | tree ret; | |
2680 | va_list args; | |
2681 | va_start (args, nargs); | |
2682 | ret = build_library_function_decl_1 (name, spec, rettype, nargs, args); | |
2683 | va_end (args); | |
2684 | return ret; | |
2685 | } | |
2686 | ||
6de9cd9a DN |
2687 | static void |
2688 | gfc_build_intrinsic_function_decls (void) | |
2689 | { | |
e2cad04b RH |
2690 | tree gfc_int4_type_node = gfc_get_int_type (4); |
2691 | tree gfc_int8_type_node = gfc_get_int_type (8); | |
644cb69f | 2692 | tree gfc_int16_type_node = gfc_get_int_type (16); |
e2cad04b | 2693 | tree gfc_logical4_type_node = gfc_get_logical_type (4); |
374929b2 FXC |
2694 | tree pchar1_type_node = gfc_get_pchar_type (1); |
2695 | tree pchar4_type_node = gfc_get_pchar_type (4); | |
e2cad04b | 2696 | |
6de9cd9a | 2697 | /* String functions. */ |
6368f166 DF |
2698 | gfor_fndecl_compare_string = gfc_build_library_function_decl_with_spec ( |
2699 | get_identifier (PREFIX("compare_string")), "..R.R", | |
2700 | integer_type_node, 4, gfc_charlen_type_node, pchar1_type_node, | |
2701 | gfc_charlen_type_node, pchar1_type_node); | |
4ca4be7f | 2702 | DECL_PURE_P (gfor_fndecl_compare_string) = 1; |
22b139e1 | 2703 | TREE_NOTHROW (gfor_fndecl_compare_string) = 1; |
6368f166 DF |
2704 | |
2705 | gfor_fndecl_concat_string = gfc_build_library_function_decl_with_spec ( | |
2706 | get_identifier (PREFIX("concat_string")), "..W.R.R", | |
2707 | void_type_node, 6, gfc_charlen_type_node, pchar1_type_node, | |
2708 | gfc_charlen_type_node, pchar1_type_node, | |
2709 | gfc_charlen_type_node, pchar1_type_node); | |
22b139e1 | 2710 | TREE_NOTHROW (gfor_fndecl_concat_string) = 1; |
6368f166 DF |
2711 | |
2712 | gfor_fndecl_string_len_trim = gfc_build_library_function_decl_with_spec ( | |
2713 | get_identifier (PREFIX("string_len_trim")), "..R", | |
2714 | gfc_charlen_type_node, 2, gfc_charlen_type_node, pchar1_type_node); | |
4ca4be7f | 2715 | DECL_PURE_P (gfor_fndecl_string_len_trim) = 1; |
22b139e1 | 2716 | TREE_NOTHROW (gfor_fndecl_string_len_trim) = 1; |
6368f166 DF |
2717 | |
2718 | gfor_fndecl_string_index = gfc_build_library_function_decl_with_spec ( | |
2719 | get_identifier (PREFIX("string_index")), "..R.R.", | |
2720 | gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar1_type_node, | |
2721 | gfc_charlen_type_node, pchar1_type_node, gfc_logical4_type_node); | |
4ca4be7f | 2722 | DECL_PURE_P (gfor_fndecl_string_index) = 1; |
22b139e1 | 2723 | TREE_NOTHROW (gfor_fndecl_string_index) = 1; |
6368f166 DF |
2724 | |
2725 | gfor_fndecl_string_scan = gfc_build_library_function_decl_with_spec ( | |
2726 | get_identifier (PREFIX("string_scan")), "..R.R.", | |
2727 | gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar1_type_node, | |
2728 | gfc_charlen_type_node, pchar1_type_node, gfc_logical4_type_node); | |
4ca4be7f | 2729 | DECL_PURE_P (gfor_fndecl_string_scan) = 1; |
22b139e1 | 2730 | TREE_NOTHROW (gfor_fndecl_string_scan) = 1; |
6368f166 DF |
2731 | |
2732 | gfor_fndecl_string_verify = gfc_build_library_function_decl_with_spec ( | |
2733 | get_identifier (PREFIX("string_verify")), "..R.R.", | |
2734 | gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar1_type_node, | |
2735 | gfc_charlen_type_node, pchar1_type_node, gfc_logical4_type_node); | |
4ca4be7f | 2736 | DECL_PURE_P (gfor_fndecl_string_verify) = 1; |
22b139e1 | 2737 | TREE_NOTHROW (gfor_fndecl_string_verify) = 1; |
6368f166 DF |
2738 | |
2739 | gfor_fndecl_string_trim = gfc_build_library_function_decl_with_spec ( | |
2740 | get_identifier (PREFIX("string_trim")), ".Ww.R", | |
2741 | void_type_node, 4, build_pointer_type (gfc_charlen_type_node), | |
2742 | build_pointer_type (pchar1_type_node), gfc_charlen_type_node, | |
2743 | pchar1_type_node); | |
2744 | ||
2745 | gfor_fndecl_string_minmax = gfc_build_library_function_decl_with_spec ( | |
2746 | get_identifier (PREFIX("string_minmax")), ".Ww.R", | |
2747 | void_type_node, -4, build_pointer_type (gfc_charlen_type_node), | |
2748 | build_pointer_type (pchar1_type_node), integer_type_node, | |
2749 | integer_type_node); | |
2750 | ||
2751 | gfor_fndecl_adjustl = gfc_build_library_function_decl_with_spec ( | |
2752 | get_identifier (PREFIX("adjustl")), ".W.R", | |
2753 | void_type_node, 3, pchar1_type_node, gfc_charlen_type_node, | |
2754 | pchar1_type_node); | |
22b139e1 | 2755 | TREE_NOTHROW (gfor_fndecl_adjustl) = 1; |
6368f166 DF |
2756 | |
2757 | gfor_fndecl_adjustr = gfc_build_library_function_decl_with_spec ( | |
2758 | get_identifier (PREFIX("adjustr")), ".W.R", | |
2759 | void_type_node, 3, pchar1_type_node, gfc_charlen_type_node, | |
2760 | pchar1_type_node); | |
22b139e1 | 2761 | TREE_NOTHROW (gfor_fndecl_adjustr) = 1; |
6368f166 DF |
2762 | |
2763 | gfor_fndecl_select_string = gfc_build_library_function_decl_with_spec ( | |
2764 | get_identifier (PREFIX("select_string")), ".R.R.", | |
2765 | integer_type_node, 4, pvoid_type_node, integer_type_node, | |
2766 | pchar1_type_node, gfc_charlen_type_node); | |
4ca4be7f | 2767 | DECL_PURE_P (gfor_fndecl_select_string) = 1; |
22b139e1 | 2768 | TREE_NOTHROW (gfor_fndecl_select_string) = 1; |
6368f166 DF |
2769 | |
2770 | gfor_fndecl_compare_string_char4 = gfc_build_library_function_decl_with_spec ( | |
2771 | get_identifier (PREFIX("compare_string_char4")), "..R.R", | |
2772 | integer_type_node, 4, gfc_charlen_type_node, pchar4_type_node, | |
2773 | gfc_charlen_type_node, pchar4_type_node); | |
4ca4be7f | 2774 | DECL_PURE_P (gfor_fndecl_compare_string_char4) = 1; |
22b139e1 | 2775 | TREE_NOTHROW (gfor_fndecl_compare_string_char4) = 1; |
6368f166 DF |
2776 | |
2777 | gfor_fndecl_concat_string_char4 = gfc_build_library_function_decl_with_spec ( | |
2778 | get_identifier (PREFIX("concat_string_char4")), "..W.R.R", | |
2779 | void_type_node, 6, gfc_charlen_type_node, pchar4_type_node, | |
2780 | gfc_charlen_type_node, pchar4_type_node, gfc_charlen_type_node, | |
2781 | pchar4_type_node); | |
22b139e1 | 2782 | TREE_NOTHROW (gfor_fndecl_concat_string_char4) = 1; |
6368f166 DF |
2783 | |
2784 | gfor_fndecl_string_len_trim_char4 = gfc_build_library_function_decl_with_spec ( | |
2785 | get_identifier (PREFIX("string_len_trim_char4")), "..R", | |
2786 | gfc_charlen_type_node, 2, gfc_charlen_type_node, pchar4_type_node); | |
4ca4be7f | 2787 | DECL_PURE_P (gfor_fndecl_string_len_trim_char4) = 1; |
22b139e1 | 2788 | TREE_NOTHROW (gfor_fndecl_string_len_trim_char4) = 1; |
6368f166 DF |
2789 | |
2790 | gfor_fndecl_string_index_char4 = gfc_build_library_function_decl_with_spec ( | |
2791 | get_identifier (PREFIX("string_index_char4")), "..R.R.", | |
2792 | gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar4_type_node, | |
2793 | gfc_charlen_type_node, pchar4_type_node, gfc_logical4_type_node); | |
4ca4be7f | 2794 | DECL_PURE_P (gfor_fndecl_string_index_char4) = 1; |
22b139e1 | 2795 | TREE_NOTHROW (gfor_fndecl_string_index_char4) = 1; |
6368f166 DF |
2796 | |
2797 | gfor_fndecl_string_scan_char4 = gfc_build_library_function_decl_with_spec ( | |
2798 | get_identifier (PREFIX("string_scan_char4")), "..R.R.", | |
2799 | gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar4_type_node, | |
2800 | gfc_charlen_type_node, pchar4_type_node, gfc_logical4_type_node); | |
4ca4be7f | 2801 | DECL_PURE_P (gfor_fndecl_string_scan_char4) = 1; |
22b139e1 | 2802 | TREE_NOTHROW (gfor_fndecl_string_scan_char4) = 1; |
6368f166 DF |
2803 | |
2804 | gfor_fndecl_string_verify_char4 = gfc_build_library_function_decl_with_spec ( | |
2805 | get_identifier (PREFIX("string_verify_char4")), "..R.R.", | |
2806 | gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar4_type_node, | |
2807 | gfc_charlen_type_node, pchar4_type_node, gfc_logical4_type_node); | |
4ca4be7f | 2808 | DECL_PURE_P (gfor_fndecl_string_verify_char4) = 1; |
22b139e1 | 2809 | TREE_NOTHROW (gfor_fndecl_string_verify_char4) = 1; |
6368f166 DF |
2810 | |
2811 | gfor_fndecl_string_trim_char4 = gfc_build_library_function_decl_with_spec ( | |
2812 | get_identifier (PREFIX("string_trim_char4")), ".Ww.R", | |
2813 | void_type_node, 4, build_pointer_type (gfc_charlen_type_node), | |
2814 | build_pointer_type (pchar4_type_node), gfc_charlen_type_node, | |
2815 | pchar4_type_node); | |
2816 | ||
2817 | gfor_fndecl_string_minmax_char4 = gfc_build_library_function_decl_with_spec ( | |
2818 | get_identifier (PREFIX("string_minmax_char4")), ".Ww.R", | |
2819 | void_type_node, -4, build_pointer_type (gfc_charlen_type_node), | |
2820 | build_pointer_type (pchar4_type_node), integer_type_node, | |
2821 | integer_type_node); | |
2822 | ||
2823 | gfor_fndecl_adjustl_char4 = gfc_build_library_function_decl_with_spec ( | |
2824 | get_identifier (PREFIX("adjustl_char4")), ".W.R", | |
2825 | void_type_node, 3, pchar4_type_node, gfc_charlen_type_node, | |
2826 | pchar4_type_node); | |
22b139e1 | 2827 | TREE_NOTHROW (gfor_fndecl_adjustl_char4) = 1; |
6368f166 DF |
2828 | |
2829 | gfor_fndecl_adjustr_char4 = gfc_build_library_function_decl_with_spec ( | |
2830 | get_identifier (PREFIX("adjustr_char4")), ".W.R", | |
2831 | void_type_node, 3, pchar4_type_node, gfc_charlen_type_node, | |
2832 | pchar4_type_node); | |
22b139e1 | 2833 | TREE_NOTHROW (gfor_fndecl_adjustr_char4) = 1; |
6368f166 DF |
2834 | |
2835 | gfor_fndecl_select_string_char4 = gfc_build_library_function_decl_with_spec ( | |
2836 | get_identifier (PREFIX("select_string_char4")), ".R.R.", | |
2837 | integer_type_node, 4, pvoid_type_node, integer_type_node, | |
2838 | pvoid_type_node, gfc_charlen_type_node); | |
4ca4be7f | 2839 | DECL_PURE_P (gfor_fndecl_select_string_char4) = 1; |
22b139e1 | 2840 | TREE_NOTHROW (gfor_fndecl_select_string_char4) = 1; |
d393bbd7 FXC |
2841 | |
2842 | ||
2843 | /* Conversion between character kinds. */ | |
2844 | ||
6368f166 DF |
2845 | gfor_fndecl_convert_char1_to_char4 = gfc_build_library_function_decl_with_spec ( |
2846 | get_identifier (PREFIX("convert_char1_to_char4")), ".w.R", | |
2847 | void_type_node, 3, build_pointer_type (pchar4_type_node), | |
2848 | gfc_charlen_type_node, pchar1_type_node); | |
d393bbd7 | 2849 | |
6368f166 DF |
2850 | gfor_fndecl_convert_char4_to_char1 = gfc_build_library_function_decl_with_spec ( |
2851 | get_identifier (PREFIX("convert_char4_to_char1")), ".w.R", | |
2852 | void_type_node, 3, build_pointer_type (pchar1_type_node), | |
2853 | gfc_charlen_type_node, pchar4_type_node); | |
d393bbd7 | 2854 | |
374929b2 | 2855 | /* Misc. functions. */ |
2263c775 | 2856 | |
6368f166 DF |
2857 | gfor_fndecl_ttynam = gfc_build_library_function_decl_with_spec ( |
2858 | get_identifier (PREFIX("ttynam")), ".W", | |
2859 | void_type_node, 3, pchar_type_node, gfc_charlen_type_node, | |
2860 | integer_type_node); | |
2861 | ||
2862 | gfor_fndecl_fdate = gfc_build_library_function_decl_with_spec ( | |
2863 | get_identifier (PREFIX("fdate")), ".W", | |
2864 | void_type_node, 2, pchar_type_node, gfc_charlen_type_node); | |
2865 | ||
2866 | gfor_fndecl_ctime = gfc_build_library_function_decl_with_spec ( | |
2867 | get_identifier (PREFIX("ctime")), ".W", | |
2868 | void_type_node, 3, pchar_type_node, gfc_charlen_type_node, | |
2869 | gfc_int8_type_node); | |
2870 | ||
2871 | gfor_fndecl_sc_kind = gfc_build_library_function_decl_with_spec ( | |
2872 | get_identifier (PREFIX("selected_char_kind")), "..R", | |
2873 | gfc_int4_type_node, 2, gfc_charlen_type_node, pchar_type_node); | |
4ca4be7f | 2874 | DECL_PURE_P (gfor_fndecl_sc_kind) = 1; |
22b139e1 | 2875 | TREE_NOTHROW (gfor_fndecl_sc_kind) = 1; |
6368f166 DF |
2876 | |
2877 | gfor_fndecl_si_kind = gfc_build_library_function_decl_with_spec ( | |
2878 | get_identifier (PREFIX("selected_int_kind")), ".R", | |
2879 | gfc_int4_type_node, 1, pvoid_type_node); | |
4ca4be7f | 2880 | DECL_PURE_P (gfor_fndecl_si_kind) = 1; |
22b139e1 | 2881 | TREE_NOTHROW (gfor_fndecl_si_kind) = 1; |
6368f166 DF |
2882 | |
2883 | gfor_fndecl_sr_kind = gfc_build_library_function_decl_with_spec ( | |
2884 | get_identifier (PREFIX("selected_real_kind2008")), ".RR", | |
2885 | gfc_int4_type_node, 3, pvoid_type_node, pvoid_type_node, | |
2886 | pvoid_type_node); | |
4ca4be7f | 2887 | DECL_PURE_P (gfor_fndecl_sr_kind) = 1; |
22b139e1 | 2888 | TREE_NOTHROW (gfor_fndecl_sr_kind) = 1; |
6de9cd9a | 2889 | |
6de9cd9a | 2890 | /* Power functions. */ |
5b200ac2 | 2891 | { |
644cb69f FXC |
2892 | tree ctype, rtype, itype, jtype; |
2893 | int rkind, ikind, jkind; | |
2894 | #define NIKINDS 3 | |
2895 | #define NRKINDS 4 | |
2896 | static int ikinds[NIKINDS] = {4, 8, 16}; | |
2897 | static int rkinds[NRKINDS] = {4, 8, 10, 16}; | |
2898 | char name[PREFIX_LEN + 12]; /* _gfortran_pow_?n_?n */ | |
2899 | ||
2900 | for (ikind=0; ikind < NIKINDS; ikind++) | |
5b200ac2 | 2901 | { |
644cb69f FXC |
2902 | itype = gfc_get_int_type (ikinds[ikind]); |
2903 | ||
2904 | for (jkind=0; jkind < NIKINDS; jkind++) | |
2905 | { | |
2906 | jtype = gfc_get_int_type (ikinds[jkind]); | |
2907 | if (itype && jtype) | |
2908 | { | |
2909 | sprintf(name, PREFIX("pow_i%d_i%d"), ikinds[ikind], | |
2910 | ikinds[jkind]); | |
2911 | gfor_fndecl_math_powi[jkind][ikind].integer = | |
2912 | gfc_build_library_function_decl (get_identifier (name), | |
2913 | jtype, 2, jtype, itype); | |
67fdae36 | 2914 | TREE_READONLY (gfor_fndecl_math_powi[jkind][ikind].integer) = 1; |
22b139e1 | 2915 | TREE_NOTHROW (gfor_fndecl_math_powi[jkind][ikind].integer) = 1; |
644cb69f FXC |
2916 | } |
2917 | } | |
2918 | ||
2919 | for (rkind = 0; rkind < NRKINDS; rkind ++) | |
5b200ac2 | 2920 | { |
644cb69f FXC |
2921 | rtype = gfc_get_real_type (rkinds[rkind]); |
2922 | if (rtype && itype) | |
2923 | { | |
2924 | sprintf(name, PREFIX("pow_r%d_i%d"), rkinds[rkind], | |
2925 | ikinds[ikind]); | |
2926 | gfor_fndecl_math_powi[rkind][ikind].real = | |
2927 | gfc_build_library_function_decl (get_identifier (name), | |
2928 | rtype, 2, rtype, itype); | |
67fdae36 | 2929 | TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].real) = 1; |
22b139e1 | 2930 | TREE_NOTHROW (gfor_fndecl_math_powi[rkind][ikind].real) = 1; |
644cb69f FXC |
2931 | } |
2932 | ||
2933 | ctype = gfc_get_complex_type (rkinds[rkind]); | |
2934 | if (ctype && itype) | |
2935 | { | |
2936 | sprintf(name, PREFIX("pow_c%d_i%d"), rkinds[rkind], | |
2937 | ikinds[ikind]); | |
2938 | gfor_fndecl_math_powi[rkind][ikind].cmplx = | |
2939 | gfc_build_library_function_decl (get_identifier (name), | |
2940 | ctype, 2,ctype, itype); | |
67fdae36 | 2941 | TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1; |
22b139e1 | 2942 | TREE_NOTHROW (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1; |
644cb69f | 2943 | } |
5b200ac2 FW |
2944 | } |
2945 | } | |
644cb69f FXC |
2946 | #undef NIKINDS |
2947 | #undef NRKINDS | |
5b200ac2 FW |
2948 | } |
2949 | ||
6368f166 DF |
2950 | gfor_fndecl_math_ishftc4 = gfc_build_library_function_decl ( |
2951 | get_identifier (PREFIX("ishftc4")), | |
2952 | gfc_int4_type_node, 3, gfc_int4_type_node, gfc_int4_type_node, | |
2953 | gfc_int4_type_node); | |
22b139e1 JJ |
2954 | TREE_READONLY (gfor_fndecl_math_ishftc4) = 1; |
2955 | TREE_NOTHROW (gfor_fndecl_math_ishftc4) = 1; | |
6368f166 DF |
2956 | |
2957 | gfor_fndecl_math_ishftc8 = gfc_build_library_function_decl ( | |
2958 | get_identifier (PREFIX("ishftc8")), | |
2959 | gfc_int8_type_node, 3, gfc_int8_type_node, gfc_int4_type_node, | |
2960 | gfc_int4_type_node); | |
22b139e1 JJ |
2961 | TREE_READONLY (gfor_fndecl_math_ishftc8) = 1; |
2962 | TREE_NOTHROW (gfor_fndecl_math_ishftc8) = 1; | |
6368f166 | 2963 | |
644cb69f | 2964 | if (gfc_int16_type_node) |
22b139e1 JJ |
2965 | { |
2966 | gfor_fndecl_math_ishftc16 = gfc_build_library_function_decl ( | |
6368f166 DF |
2967 | get_identifier (PREFIX("ishftc16")), |
2968 | gfc_int16_type_node, 3, gfc_int16_type_node, gfc_int4_type_node, | |
2969 | gfc_int4_type_node); | |
22b139e1 JJ |
2970 | TREE_READONLY (gfor_fndecl_math_ishftc16) = 1; |
2971 | TREE_NOTHROW (gfor_fndecl_math_ishftc16) = 1; | |
2972 | } | |
644cb69f | 2973 | |
5a0aad31 FXC |
2974 | /* BLAS functions. */ |
2975 | { | |
dd52ecb0 | 2976 | tree pint = build_pointer_type (integer_type_node); |
5a0aad31 FXC |
2977 | tree ps = build_pointer_type (gfc_get_real_type (gfc_default_real_kind)); |
2978 | tree pd = build_pointer_type (gfc_get_real_type (gfc_default_double_kind)); | |
2979 | tree pc = build_pointer_type (gfc_get_complex_type (gfc_default_real_kind)); | |
2980 | tree pz = build_pointer_type | |
2981 | (gfc_get_complex_type (gfc_default_double_kind)); | |
2982 | ||
2983 | gfor_fndecl_sgemm = gfc_build_library_function_decl | |
2984 | (get_identifier | |
2985 | (gfc_option.flag_underscoring ? "sgemm_" | |
2986 | : "sgemm"), | |
2987 | void_type_node, 15, pchar_type_node, | |
2988 | pchar_type_node, pint, pint, pint, ps, ps, pint, | |
dd52ecb0 JB |
2989 | ps, pint, ps, ps, pint, integer_type_node, |
2990 | integer_type_node); | |
5a0aad31 FXC |
2991 | gfor_fndecl_dgemm = gfc_build_library_function_decl |
2992 | (get_identifier | |
2993 | (gfc_option.flag_underscoring ? "dgemm_" | |
2994 | : "dgemm"), | |
2995 | void_type_node, 15, pchar_type_node, | |
2996 | pchar_type_node, pint, pint, pint, pd, pd, pint, | |
dd52ecb0 JB |
2997 | pd, pint, pd, pd, pint, integer_type_node, |
2998 | integer_type_node); | |
5a0aad31 FXC |
2999 | gfor_fndecl_cgemm = gfc_build_library_function_decl |
3000 | (get_identifier | |
3001 | (gfc_option.flag_underscoring ? "cgemm_" | |
3002 | : "cgemm"), | |
3003 | void_type_node, 15, pchar_type_node, | |
3004 | pchar_type_node, pint, pint, pint, pc, pc, pint, | |
dd52ecb0 JB |
3005 | pc, pint, pc, pc, pint, integer_type_node, |
3006 | integer_type_node); | |
5a0aad31 FXC |
3007 | gfor_fndecl_zgemm = gfc_build_library_function_decl |
3008 | (get_identifier | |
3009 | (gfc_option.flag_underscoring ? "zgemm_" | |
3010 | : "zgemm"), | |
3011 | void_type_node, 15, pchar_type_node, | |
3012 | pchar_type_node, pint, pint, pint, pz, pz, pint, | |
dd52ecb0 JB |
3013 | pz, pint, pz, pz, pint, integer_type_node, |
3014 | integer_type_node); | |
5a0aad31 FXC |
3015 | } |
3016 | ||
6de9cd9a | 3017 | /* Other functions. */ |
6368f166 DF |
3018 | gfor_fndecl_size0 = gfc_build_library_function_decl_with_spec ( |
3019 | get_identifier (PREFIX("size0")), ".R", | |
3020 | gfc_array_index_type, 1, pvoid_type_node); | |
4ca4be7f | 3021 | DECL_PURE_P (gfor_fndecl_size0) = 1; |
22b139e1 | 3022 | TREE_NOTHROW (gfor_fndecl_size0) = 1; |
6368f166 DF |
3023 | |
3024 | gfor_fndecl_size1 = gfc_build_library_function_decl_with_spec ( | |
3025 | get_identifier (PREFIX("size1")), ".R", | |
3026 | gfc_array_index_type, 2, pvoid_type_node, gfc_array_index_type); | |
4ca4be7f | 3027 | DECL_PURE_P (gfor_fndecl_size1) = 1; |
22b139e1 | 3028 | TREE_NOTHROW (gfor_fndecl_size1) = 1; |
6368f166 DF |
3029 | |
3030 | gfor_fndecl_iargc = gfc_build_library_function_decl ( | |
3031 | get_identifier (PREFIX ("iargc")), gfc_int4_type_node, 0); | |
22b139e1 | 3032 | TREE_NOTHROW (gfor_fndecl_iargc) = 1; |
6de9cd9a DN |
3033 | } |
3034 | ||
3035 | ||
3036 | /* Make prototypes for runtime library functions. */ | |
3037 | ||
3038 | void | |
3039 | gfc_build_builtin_function_decls (void) | |
3040 | { | |
e2cad04b | 3041 | tree gfc_int4_type_node = gfc_get_int_type (4); |
6de9cd9a | 3042 | |
6368f166 DF |
3043 | gfor_fndecl_stop_numeric = gfc_build_library_function_decl ( |
3044 | get_identifier (PREFIX("stop_numeric")), | |
3045 | void_type_node, 1, gfc_int4_type_node); | |
6d1b0f92 | 3046 | /* STOP doesn't return. */ |
eed61baa TK |
3047 | TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric) = 1; |
3048 | ||
cea59ace JD |
3049 | gfor_fndecl_stop_numeric_f08 = gfc_build_library_function_decl ( |
3050 | get_identifier (PREFIX("stop_numeric_f08")), | |
3051 | void_type_node, 1, gfc_int4_type_node); | |
3052 | /* STOP doesn't return. */ | |
3053 | TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric_f08) = 1; | |
3054 | ||
6368f166 DF |
3055 | gfor_fndecl_stop_string = gfc_build_library_function_decl_with_spec ( |
3056 | get_identifier (PREFIX("stop_string")), ".R.", | |
3057 | void_type_node, 2, pchar_type_node, gfc_int4_type_node); | |
6d1b0f92 | 3058 | /* STOP doesn't return. */ |
6368f166 | 3059 | TREE_THIS_VOLATILE (gfor_fndecl_stop_string) = 1; |
4ca4be7f | 3060 | |
6368f166 DF |
3061 | gfor_fndecl_error_stop_numeric = gfc_build_library_function_decl ( |
3062 | get_identifier (PREFIX("error_stop_numeric")), | |
3063 | void_type_node, 1, gfc_int4_type_node); | |
6d1b0f92 JD |
3064 | /* ERROR STOP doesn't return. */ |
3065 | TREE_THIS_VOLATILE (gfor_fndecl_error_stop_numeric) = 1; | |
3066 | ||
6368f166 DF |
3067 | gfor_fndecl_error_stop_string = gfc_build_library_function_decl_with_spec ( |
3068 | get_identifier (PREFIX("error_stop_string")), ".R.", | |
3069 | void_type_node, 2, pchar_type_node, gfc_int4_type_node); | |
d0a4a61c TB |
3070 | /* ERROR STOP doesn't return. */ |
3071 | TREE_THIS_VOLATILE (gfor_fndecl_error_stop_string) = 1; | |
3072 | ||
6368f166 DF |
3073 | gfor_fndecl_pause_numeric = gfc_build_library_function_decl ( |
3074 | get_identifier (PREFIX("pause_numeric")), | |
3075 | void_type_node, 1, gfc_int4_type_node); | |
6d1b0f92 | 3076 | |
6368f166 DF |
3077 | gfor_fndecl_pause_string = gfc_build_library_function_decl_with_spec ( |
3078 | get_identifier (PREFIX("pause_string")), ".R.", | |
3079 | void_type_node, 2, pchar_type_node, gfc_int4_type_node); | |
6de9cd9a | 3080 | |
6368f166 DF |
3081 | gfor_fndecl_runtime_error = gfc_build_library_function_decl_with_spec ( |
3082 | get_identifier (PREFIX("runtime_error")), ".R", | |
3083 | void_type_node, -1, pchar_type_node); | |
16275f18 SB |
3084 | /* The runtime_error function does not return. */ |
3085 | TREE_THIS_VOLATILE (gfor_fndecl_runtime_error) = 1; | |
6de9cd9a | 3086 | |
6368f166 DF |
3087 | gfor_fndecl_runtime_error_at = gfc_build_library_function_decl_with_spec ( |
3088 | get_identifier (PREFIX("runtime_error_at")), ".RR", | |
3089 | void_type_node, -2, pchar_type_node, pchar_type_node); | |
f96d606f JD |
3090 | /* The runtime_error_at function does not return. */ |
3091 | TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at) = 1; | |
3092 | ||
6368f166 DF |
3093 | gfor_fndecl_runtime_warning_at = gfc_build_library_function_decl_with_spec ( |
3094 | get_identifier (PREFIX("runtime_warning_at")), ".RR", | |
3095 | void_type_node, -2, pchar_type_node, pchar_type_node); | |
3096 | ||
3097 | gfor_fndecl_generate_error = gfc_build_library_function_decl_with_spec ( | |
3098 | get_identifier (PREFIX("generate_error")), ".R.R", | |
3099 | void_type_node, 3, pvoid_type_node, integer_type_node, | |
3100 | pchar_type_node); | |
3101 | ||
3102 | gfor_fndecl_os_error = gfc_build_library_function_decl_with_spec ( | |
3103 | get_identifier (PREFIX("os_error")), ".R", | |
3104 | void_type_node, 1, pchar_type_node); | |
1529b8d9 FXC |
3105 | /* The runtime_error function does not return. */ |
3106 | TREE_THIS_VOLATILE (gfor_fndecl_os_error) = 1; | |
3107 | ||
6368f166 DF |
3108 | gfor_fndecl_set_args = gfc_build_library_function_decl ( |
3109 | get_identifier (PREFIX("set_args")), | |
3110 | void_type_node, 2, integer_type_node, | |
3111 | build_pointer_type (pchar_type_node)); | |
092231a8 | 3112 | |
6368f166 DF |
3113 | gfor_fndecl_set_fpe = gfc_build_library_function_decl ( |
3114 | get_identifier (PREFIX("set_fpe")), | |
3115 | void_type_node, 1, integer_type_node); | |
944b8b35 | 3116 | |
68d2e027 | 3117 | /* Keep the array dimension in sync with the call, later in this file. */ |
6368f166 DF |
3118 | gfor_fndecl_set_options = gfc_build_library_function_decl_with_spec ( |
3119 | get_identifier (PREFIX("set_options")), "..R", | |
3120 | void_type_node, 2, integer_type_node, | |
3121 | build_pointer_type (integer_type_node)); | |
8b67b708 | 3122 | |
6368f166 DF |
3123 | gfor_fndecl_set_convert = gfc_build_library_function_decl ( |
3124 | get_identifier (PREFIX("set_convert")), | |
3125 | void_type_node, 1, integer_type_node); | |
eaa90d25 | 3126 | |
6368f166 DF |
3127 | gfor_fndecl_set_record_marker = gfc_build_library_function_decl ( |
3128 | get_identifier (PREFIX("set_record_marker")), | |
3129 | void_type_node, 1, integer_type_node); | |
d67ab5ee | 3130 | |
6368f166 DF |
3131 | gfor_fndecl_set_max_subrecord_length = gfc_build_library_function_decl ( |
3132 | get_identifier (PREFIX("set_max_subrecord_length")), | |
3133 | void_type_node, 1, integer_type_node); | |
07b3bbf2 | 3134 | |
0b7b376d | 3135 | gfor_fndecl_in_pack = gfc_build_library_function_decl_with_spec ( |
6368f166 DF |
3136 | get_identifier (PREFIX("internal_pack")), ".r", |
3137 | pvoid_type_node, 1, pvoid_type_node); | |
6de9cd9a | 3138 | |
0b7b376d | 3139 | gfor_fndecl_in_unpack = gfc_build_library_function_decl_with_spec ( |
6368f166 DF |
3140 | get_identifier (PREFIX("internal_unpack")), ".wR", |
3141 | void_type_node, 2, pvoid_type_node, pvoid_type_node); | |
3142 | ||
3143 | gfor_fndecl_associated = gfc_build_library_function_decl_with_spec ( | |
3144 | get_identifier (PREFIX("associated")), ".RR", | |
3145 | integer_type_node, 2, ppvoid_type_node, ppvoid_type_node); | |
4ca4be7f | 3146 | DECL_PURE_P (gfor_fndecl_associated) = 1; |
22b139e1 | 3147 | TREE_NOTHROW (gfor_fndecl_associated) = 1; |
6de9cd9a | 3148 | |
60386f50 TB |
3149 | /* Coarray library calls. */ |
3150 | if (gfc_option.coarray == GFC_FCOARRAY_LIB) | |
3151 | { | |
3152 | tree pint_type, pppchar_type; | |
3153 | ||
3154 | pint_type = build_pointer_type (integer_type_node); | |
3155 | pppchar_type | |
3156 | = build_pointer_type (build_pointer_type (pchar_type_node)); | |
3157 | ||
3158 | gfor_fndecl_caf_init = gfc_build_library_function_decl ( | |
3159 | get_identifier (PREFIX("caf_init")), void_type_node, | |
3160 | 4, pint_type, pppchar_type, pint_type, pint_type); | |
3161 | ||
3162 | gfor_fndecl_caf_finalize = gfc_build_library_function_decl ( | |
3163 | get_identifier (PREFIX("caf_finalize")), void_type_node, 0); | |
3164 | ||
b8ff4e88 TB |
3165 | gfor_fndecl_caf_register = gfc_build_library_function_decl_with_spec ( |
3166 | get_identifier (PREFIX("caf_register")), "...WWW", pvoid_type_node, 6, | |
3167 | size_type_node, integer_type_node, ppvoid_type_node, pint_type, | |
5d81ddd0 TB |
3168 | pchar_type_node, integer_type_node); |
3169 | ||
3170 | gfor_fndecl_caf_deregister = gfc_build_library_function_decl_with_spec ( | |
3171 | get_identifier (PREFIX("caf_deregister")), ".WWW", void_type_node, 4, | |
3172 | ppvoid_type_node, pint_type, pchar_type_node, integer_type_node); | |
b8ff4e88 | 3173 | |
60386f50 TB |
3174 | gfor_fndecl_caf_critical = gfc_build_library_function_decl ( |
3175 | get_identifier (PREFIX("caf_critical")), void_type_node, 0); | |
3176 | ||
3177 | gfor_fndecl_caf_end_critical = gfc_build_library_function_decl ( | |
3178 | get_identifier (PREFIX("caf_end_critical")), void_type_node, 0); | |
3179 | ||
3180 | gfor_fndecl_caf_sync_all = gfc_build_library_function_decl_with_spec ( | |
f5c01f5b DC |
3181 | get_identifier (PREFIX("caf_sync_all")), ".WW", void_type_node, |
3182 | 3, pint_type, build_pointer_type (pchar_type_node), integer_type_node); | |
60386f50 TB |
3183 | |
3184 | gfor_fndecl_caf_sync_images = gfc_build_library_function_decl_with_spec ( | |
f5c01f5b DC |
3185 | get_identifier (PREFIX("caf_sync_images")), ".RRWW", void_type_node, |
3186 | 5, integer_type_node, pint_type, pint_type, | |
3187 | build_pointer_type (pchar_type_node), integer_type_node); | |
60386f50 TB |
3188 | |
3189 | gfor_fndecl_caf_error_stop = gfc_build_library_function_decl ( | |
3190 | get_identifier (PREFIX("caf_error_stop")), | |
3191 | void_type_node, 1, gfc_int4_type_node); | |
3192 | /* CAF's ERROR STOP doesn't return. */ | |
3193 | TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop) = 1; | |
3194 | ||
3195 | gfor_fndecl_caf_error_stop_str = gfc_build_library_function_decl_with_spec ( | |
3196 | get_identifier (PREFIX("caf_error_stop_str")), ".R.", | |
3197 | void_type_node, 2, pchar_type_node, gfc_int4_type_node); | |
3198 | /* CAF's ERROR STOP doesn't return. */ | |
3199 | TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop_str) = 1; | |
3200 | } | |
3201 | ||
6de9cd9a DN |
3202 | gfc_build_intrinsic_function_decls (); |
3203 | gfc_build_intrinsic_lib_fndecls (); | |
3204 | gfc_build_io_library_fndecls (); | |
3205 | } | |
3206 | ||
3207 | ||
1f2959f0 | 3208 | /* Evaluate the length of dummy character variables. */ |
6de9cd9a | 3209 | |
0019d498 DK |
3210 | static void |
3211 | gfc_trans_dummy_character (gfc_symbol *sym, gfc_charlen *cl, | |
3212 | gfc_wrapped_block *block) | |
6de9cd9a | 3213 | { |
0019d498 | 3214 | stmtblock_t init; |
6de9cd9a | 3215 | |
faf28b3a | 3216 | gfc_finish_decl (cl->backend_decl); |
6de9cd9a | 3217 | |
0019d498 | 3218 | gfc_start_block (&init); |
6de9cd9a DN |
3219 | |
3220 | /* Evaluate the string length expression. */ | |
0019d498 | 3221 | gfc_conv_string_length (cl, NULL, &init); |
417ab240 | 3222 | |
0019d498 | 3223 | gfc_trans_vla_type_sizes (sym, &init); |
417ab240 | 3224 | |
0019d498 | 3225 | gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE); |
6de9cd9a DN |
3226 | } |
3227 | ||
3228 | ||
3229 | /* Allocate and cleanup an automatic character variable. */ | |
3230 | ||
0019d498 DK |
3231 | static void |
3232 | gfc_trans_auto_character_variable (gfc_symbol * sym, gfc_wrapped_block * block) | |
6de9cd9a | 3233 | { |
0019d498 | 3234 | stmtblock_t init; |
6de9cd9a | 3235 | tree decl; |
6de9cd9a DN |
3236 | tree tmp; |
3237 | ||
6e45f57b | 3238 | gcc_assert (sym->backend_decl); |
bc21d315 | 3239 | gcc_assert (sym->ts.u.cl && sym->ts.u.cl->length); |
6de9cd9a | 3240 | |
323cea66 | 3241 | gfc_init_block (&init); |
6de9cd9a DN |
3242 | |
3243 | /* Evaluate the string length expression. */ | |
0019d498 | 3244 | gfc_conv_string_length (sym->ts.u.cl, NULL, &init); |
6de9cd9a | 3245 | |
0019d498 | 3246 | gfc_trans_vla_type_sizes (sym, &init); |
417ab240 | 3247 | |
6de9cd9a DN |
3248 | decl = sym->backend_decl; |
3249 | ||
1a186ec5 | 3250 | /* Emit a DECL_EXPR for this variable, which will cause the |
4ab2db93 | 3251 | gimplifier to allocate storage, and all that good stuff. */ |
bc98ed60 | 3252 | tmp = fold_build1_loc (input_location, DECL_EXPR, TREE_TYPE (decl), decl); |
0019d498 | 3253 | gfc_add_expr_to_block (&init, tmp); |
1a186ec5 | 3254 | |
0019d498 | 3255 | gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE); |
6de9cd9a DN |
3256 | } |
3257 | ||
910450c1 FW |
3258 | /* Set the initial value of ASSIGN statement auxiliary variable explicitly. */ |
3259 | ||
0019d498 DK |
3260 | static void |
3261 | gfc_trans_assign_aux_var (gfc_symbol * sym, gfc_wrapped_block * block) | |
910450c1 | 3262 | { |
0019d498 | 3263 | stmtblock_t init; |
910450c1 FW |
3264 | |
3265 | gcc_assert (sym->backend_decl); | |
0019d498 | 3266 | gfc_start_block (&init); |
910450c1 FW |
3267 | |
3268 | /* Set the initial value to length. See the comments in | |
3269 | function gfc_add_assign_aux_vars in this file. */ | |
0019d498 | 3270 | gfc_add_modify (&init, GFC_DECL_STRING_LEN (sym->backend_decl), |
df09d1d5 | 3271 | build_int_cst (gfc_charlen_type_node, -2)); |
910450c1 | 3272 | |
0019d498 | 3273 | gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE); |
910450c1 FW |
3274 | } |
3275 | ||
417ab240 JJ |
3276 | static void |
3277 | gfc_trans_vla_one_sizepos (tree *tp, stmtblock_t *body) | |
3278 | { | |
3279 | tree t = *tp, var, val; | |
3280 | ||
3281 | if (t == NULL || t == error_mark_node) | |
3282 | return; | |
3283 | if (TREE_CONSTANT (t) || DECL_P (t)) | |
3284 | return; | |
3285 | ||
3286 | if (TREE_CODE (t) == SAVE_EXPR) | |
3287 | { | |
3288 | if (SAVE_EXPR_RESOLVED_P (t)) | |
3289 | { | |
3290 | *tp = TREE_OPERAND (t, 0); | |
3291 | return; | |
3292 | } | |
3293 | val = TREE_OPERAND (t, 0); | |
3294 | } | |
3295 | else | |
3296 | val = t; | |
3297 | ||
3298 | var = gfc_create_var_np (TREE_TYPE (t), NULL); | |
3299 | gfc_add_decl_to_function (var); | |
726a989a | 3300 | gfc_add_modify (body, var, val); |
417ab240 JJ |
3301 | if (TREE_CODE (t) == SAVE_EXPR) |
3302 | TREE_OPERAND (t, 0) = var; | |
3303 | *tp = var; | |
3304 | } | |
3305 | ||
3306 | static void | |
3307 | gfc_trans_vla_type_sizes_1 (tree type, stmtblock_t *body) | |
3308 | { | |
3309 | tree t; | |
3310 | ||
3311 | if (type == NULL || type == error_mark_node) | |
3312 | return; | |
3313 | ||
3314 | type = TYPE_MAIN_VARIANT (type); | |
3315 | ||
3316 | if (TREE_CODE (type) == INTEGER_TYPE) | |
3317 | { | |
3318 | gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type), body); | |
3319 | gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type), body); | |
3320 | ||
3321 | for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) | |
3322 | { | |
3323 | TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type); | |
3324 | TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type); | |
3325 | } | |
3326 | } | |
3327 | else if (TREE_CODE (type) == ARRAY_TYPE) | |
3328 | { | |
3329 | gfc_trans_vla_type_sizes_1 (TREE_TYPE (type), body); | |
3330 | gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type), body); | |
3331 | gfc_trans_vla_one_sizepos (&TYPE_SIZE (type), body); | |
3332 | gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type), body); | |
3333 | ||
3334 | for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) | |
3335 | { | |
3336 | TYPE_SIZE (t) = TYPE_SIZE (type); | |
3337 | TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type); | |
3338 | } | |
3339 | } | |
3340 | } | |
3341 | ||
3342 | /* Make sure all type sizes and array domains are either constant, | |
3343 | or variable or parameter decls. This is a simplified variant | |
3344 | of gimplify_type_sizes, but we can't use it here, as none of the | |
3345 | variables in the expressions have been gimplified yet. | |
3346 | As type sizes and domains for various variable length arrays | |
3347 | contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars | |
3348 | time, without this routine gimplify_type_sizes in the middle-end | |
3349 | could result in the type sizes being gimplified earlier than where | |
3350 | those variables are initialized. */ | |
3351 | ||
3352 | void | |
3353 | gfc_trans_vla_type_sizes (gfc_symbol *sym, stmtblock_t *body) | |
3354 | { | |
3355 | tree type = TREE_TYPE (sym->backend_decl); | |
3356 | ||
3357 | if (TREE_CODE (type) == FUNCTION_TYPE | |
3358 | && (sym->attr.function || sym->attr.result || sym->attr.entry)) | |
3359 | { | |
3360 | if (! current_fake_result_decl) | |
3361 | return; | |
3362 | ||
3363 | type = TREE_TYPE (TREE_VALUE (current_fake_result_decl)); | |
3364 | } | |
3365 | ||
3366 | while (POINTER_TYPE_P (type)) | |
3367 | type = TREE_TYPE (type); | |
3368 | ||
3369 | if (GFC_DESCRIPTOR_TYPE_P (type)) | |
3370 | { | |
3371 | tree etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type); | |
3372 | ||
3373 | while (POINTER_TYPE_P (etype)) | |
3374 | etype = TREE_TYPE (etype); | |
3375 | ||
3376 | gfc_trans_vla_type_sizes_1 (etype, body); | |
3377 | } | |
3378 | ||
3379 | gfc_trans_vla_type_sizes_1 (type, body); | |
3380 | } | |
3381 | ||
6de9cd9a | 3382 | |
b7b184a8 | 3383 | /* Initialize a derived type by building an lvalue from the symbol |
2b56d6a4 TB |
3384 | and using trans_assignment to do the work. Set dealloc to false |
3385 | if no deallocation prior the assignment is needed. */ | |
0019d498 DK |
3386 | void |
3387 | gfc_init_default_dt (gfc_symbol * sym, stmtblock_t * block, bool dealloc) | |
d3837072 | 3388 | { |
b7b184a8 | 3389 | gfc_expr *e; |
d3837072 PT |
3390 | tree tmp; |
3391 | tree present; | |
3392 | ||
0019d498 DK |
3393 | gcc_assert (block); |
3394 | ||
b7b184a8 PT |
3395 | gcc_assert (!sym->attr.allocatable); |
3396 | gfc_set_sym_referenced (sym); | |
3397 | e = gfc_lval_expr_from_sym (sym); | |
2b56d6a4 | 3398 | tmp = gfc_trans_assignment (e, sym->value, false, dealloc); |
ba6f7079 TB |
3399 | if (sym->attr.dummy && (sym->attr.optional |
3400 | || sym->ns->proc_name->attr.entry_master)) | |
d3837072 | 3401 | { |
b7b184a8 | 3402 | present = gfc_conv_expr_present (sym); |
5d44e5c8 TB |
3403 | tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present, |
3404 | tmp, build_empty_stmt (input_location)); | |
d3837072 | 3405 | } |
0019d498 | 3406 | gfc_add_expr_to_block (block, tmp); |
b7b184a8 | 3407 | gfc_free_expr (e); |
d3837072 PT |
3408 | } |
3409 | ||
3410 | ||
2c69d527 PT |
3411 | /* Initialize INTENT(OUT) derived type dummies. As well as giving |
3412 | them their default initializer, if they do not have allocatable | |
3413 | components, they have their allocatable components deallocated. */ | |
3414 | ||
0019d498 DK |
3415 | static void |
3416 | init_intent_out_dt (gfc_symbol * proc_sym, gfc_wrapped_block * block) | |
b7b184a8 | 3417 | { |
0019d498 | 3418 | stmtblock_t init; |
b7b184a8 | 3419 | gfc_formal_arglist *f; |
2c69d527 | 3420 | tree tmp; |
8a272531 | 3421 | tree present; |
b7b184a8 | 3422 | |
0019d498 | 3423 | gfc_init_block (&init); |
b7b184a8 PT |
3424 | for (f = proc_sym->formal; f; f = f->next) |
3425 | if (f->sym && f->sym->attr.intent == INTENT_OUT | |
758e12af TB |
3426 | && !f->sym->attr.pointer |
3427 | && f->sym->ts.type == BT_DERIVED) | |
2c69d527 | 3428 | { |
ba6f7079 | 3429 | if (f->sym->ts.u.derived->attr.alloc_comp && !f->sym->value) |
2c69d527 | 3430 | { |
bc21d315 | 3431 | tmp = gfc_deallocate_alloc_comp (f->sym->ts.u.derived, |
2c69d527 PT |
3432 | f->sym->backend_decl, |
3433 | f->sym->as ? f->sym->as->rank : 0); | |
8a272531 | 3434 | |
ba6f7079 TB |
3435 | if (f->sym->attr.optional |
3436 | || f->sym->ns->proc_name->attr.entry_master) | |
3437 | { | |
3438 | present = gfc_conv_expr_present (f->sym); | |
5d44e5c8 TB |
3439 | tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), |
3440 | present, tmp, | |
3441 | build_empty_stmt (input_location)); | |
ba6f7079 | 3442 | } |
8a272531 | 3443 | |
0019d498 | 3444 | gfc_add_expr_to_block (&init, tmp); |
2c69d527 | 3445 | } |
ba6f7079 | 3446 | else if (f->sym->value) |
0019d498 | 3447 | gfc_init_default_dt (f->sym, &init, true); |
2c69d527 | 3448 | } |
c7f17815 JW |
3449 | else if (f->sym && f->sym->attr.intent == INTENT_OUT |
3450 | && f->sym->ts.type == BT_CLASS | |
3451 | && !CLASS_DATA (f->sym)->attr.class_pointer | |
3452 | && CLASS_DATA (f->sym)->ts.u.derived->attr.alloc_comp) | |
3453 | { | |
2be13164 TB |
3454 | tmp = gfc_class_data_get (f->sym->backend_decl); |
3455 | if (CLASS_DATA (f->sym)->as == NULL) | |
3456 | tmp = build_fold_indirect_ref_loc (input_location, tmp); | |
c7f17815 JW |
3457 | tmp = gfc_deallocate_alloc_comp (CLASS_DATA (f->sym)->ts.u.derived, |
3458 | tmp, | |
3459 | CLASS_DATA (f->sym)->as ? | |
3460 | CLASS_DATA (f->sym)->as->rank : 0); | |
3461 | ||
3462 | if (f->sym->attr.optional || f->sym->ns->proc_name->attr.entry_master) | |
3463 | { | |
3464 | present = gfc_conv_expr_present (f->sym); | |
3465 | tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), | |
3466 | present, tmp, | |
3467 | build_empty_stmt (input_location)); | |
3468 | } | |
3469 | ||
3470 | gfc_add_expr_to_block (&init, tmp); | |
3471 | } | |
b7b184a8 | 3472 | |
0019d498 | 3473 | gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE); |
b7b184a8 PT |
3474 | } |
3475 | ||
d3837072 | 3476 | |
6de9cd9a DN |
3477 | /* Generate function entry and exit code, and add it to the function body. |
3478 | This includes: | |
f8d0aee5 | 3479 | Allocation and initialization of array variables. |
6de9cd9a | 3480 | Allocation of character string variables. |
910450c1 | 3481 | Initialization and possibly repacking of dummy arrays. |
1517fd57 | 3482 | Initialization of ASSIGN statement auxiliary variable. |
571d54de | 3483 | Initialization of ASSOCIATE names. |
1517fd57 | 3484 | Automatic deallocation. */ |
6de9cd9a | 3485 | |
d74d8807 DK |
3486 | void |
3487 | gfc_trans_deferred_vars (gfc_symbol * proc_sym, gfc_wrapped_block * block) | |
6de9cd9a DN |
3488 | { |
3489 | locus loc; | |
3490 | gfc_symbol *sym; | |
417ab240 | 3491 | gfc_formal_arglist *f; |
0019d498 | 3492 | stmtblock_t tmpblock; |
7114edca | 3493 | bool seen_trans_deferred_array = false; |
8d51f26f PT |
3494 | tree tmp = NULL; |
3495 | gfc_expr *e; | |
3496 | gfc_se se; | |
3497 | stmtblock_t init; | |
6de9cd9a DN |
3498 | |
3499 | /* Deal with implicit return variables. Explicit return variables will | |
3500 | already have been added. */ | |
3501 | if (gfc_return_by_reference (proc_sym) && proc_sym->result == proc_sym) | |
3502 | { | |
3503 | if (!current_fake_result_decl) | |
3504 | { | |
d198b59a JJ |
3505 | gfc_entry_list *el = NULL; |
3506 | if (proc_sym->attr.entry_master) | |
3507 | { | |
3508 | for (el = proc_sym->ns->entries; el; el = el->next) | |
3509 | if (el->sym != el->sym->result) | |
3510 | break; | |
3511 | } | |
766d0c8c DF |
3512 | /* TODO: move to the appropriate place in resolve.c. */ |
3513 | if (warn_return_type && el == NULL) | |
3514 | gfc_warning ("Return value of function '%s' at %L not set", | |
3515 | proc_sym->name, &proc_sym->declared_at); | |
6de9cd9a | 3516 | } |
d198b59a | 3517 | else if (proc_sym->as) |
6de9cd9a | 3518 | { |
417ab240 | 3519 | tree result = TREE_VALUE (current_fake_result_decl); |
d74d8807 | 3520 | gfc_trans_dummy_array_bias (proc_sym, result, block); |
f5f701ad PT |
3521 | |
3522 | /* An automatic character length, pointer array result. */ | |
3523 | if (proc_sym->ts.type == BT_CHARACTER | |
bc21d315 | 3524 | && TREE_CODE (proc_sym->ts.u.cl->backend_decl) == VAR_DECL) |
d74d8807 | 3525 | gfc_trans_dummy_character (proc_sym, proc_sym->ts.u.cl, block); |
6de9cd9a DN |
3526 | } |
3527 | else if (proc_sym->ts.type == BT_CHARACTER) | |
3528 | { | |
8d51f26f PT |
3529 | if (proc_sym->ts.deferred) |
3530 | { | |
3531 | tmp = NULL; | |
ceccaacf TB |
3532 | gfc_save_backend_locus (&loc); |
3533 | gfc_set_backend_locus (&proc_sym->declared_at); | |
8d51f26f PT |
3534 | gfc_start_block (&init); |
3535 | /* Zero the string length on entry. */ | |
3536 | gfc_add_modify (&init, proc_sym->ts.u.cl->backend_decl, | |
3537 | build_int_cst (gfc_charlen_type_node, 0)); | |
3538 | /* Null the pointer. */ | |
3539 | e = gfc_lval_expr_from_sym (proc_sym); | |
3540 | gfc_init_se (&se, NULL); | |
3541 | se.want_pointer = 1; | |
3542 | gfc_conv_expr (&se, e); | |
3543 | gfc_free_expr (e); | |
3544 | tmp = se.expr; | |
3545 | gfc_add_modify (&init, tmp, | |
3546 | fold_convert (TREE_TYPE (se.expr), | |
3547 | null_pointer_node)); | |
ceccaacf | 3548 | gfc_restore_backend_locus (&loc); |
8d51f26f PT |
3549 | |
3550 | /* Pass back the string length on exit. */ | |
3551 | tmp = proc_sym->ts.u.cl->passed_length; | |
3552 | tmp = build_fold_indirect_ref_loc (input_location, tmp); | |
3553 | tmp = fold_convert (gfc_charlen_type_node, tmp); | |
3554 | tmp = fold_build2_loc (input_location, MODIFY_EXPR, | |
3555 | gfc_charlen_type_node, tmp, | |
3556 | proc_sym->ts.u.cl->backend_decl); | |
3557 | gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp); | |
3558 | } | |
3559 | else if (TREE_CODE (proc_sym->ts.u.cl->backend_decl) == VAR_DECL) | |
d74d8807 | 3560 | gfc_trans_dummy_character (proc_sym, proc_sym->ts.u.cl, block); |
6de9cd9a DN |
3561 | } |
3562 | else | |
973ff4c0 TS |
3563 | gcc_assert (gfc_option.flag_f2c |
3564 | && proc_sym->ts.type == BT_COMPLEX); | |
6de9cd9a DN |
3565 | } |
3566 | ||
d3837072 PT |
3567 | /* Initialize the INTENT(OUT) derived type dummy arguments. This |
3568 | should be done here so that the offsets and lbounds of arrays | |
3569 | are available. */ | |
ceccaacf TB |
3570 | gfc_save_backend_locus (&loc); |
3571 | gfc_set_backend_locus (&proc_sym->declared_at); | |
d74d8807 | 3572 | init_intent_out_dt (proc_sym, block); |
ceccaacf | 3573 | gfc_restore_backend_locus (&loc); |
d3837072 | 3574 | |
6de9cd9a DN |
3575 | for (sym = proc_sym->tlink; sym != proc_sym; sym = sym->tlink) |
3576 | { | |
5046aff5 | 3577 | bool sym_has_alloc_comp = (sym->ts.type == BT_DERIVED) |
bc21d315 | 3578 | && sym->ts.u.derived->attr.alloc_comp; |
571d54de | 3579 | if (sym->assoc) |
6312ef45 JW |
3580 | continue; |
3581 | ||
0c1e1df8 JJ |
3582 | if (sym->attr.subref_array_pointer |
3583 | && GFC_DECL_SPAN (sym->backend_decl) | |
3584 | && !TREE_STATIC (GFC_DECL_SPAN (sym->backend_decl))) | |
3585 | { | |
3586 | gfc_init_block (&tmpblock); | |
3587 | gfc_add_modify (&tmpblock, GFC_DECL_SPAN (sym->backend_decl), | |
3588 | build_int_cst (gfc_array_index_type, 0)); | |
3589 | gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), | |
3590 | NULL_TREE); | |
3591 | } | |
3592 | ||
4ca9939b | 3593 | if (sym->attr.dimension || sym->attr.codimension) |
6de9cd9a | 3594 | { |
cde3a7a9 AL |
3595 | /* Assumed-size Cray pointees need to be treated as AS_EXPLICIT. */ |
3596 | array_type tmp = sym->as->type; | |
3597 | if (tmp == AS_ASSUMED_SIZE && sym->as->cp_was_assumed) | |
3598 | tmp = AS_EXPLICIT; | |
3599 | switch (tmp) | |
6de9cd9a DN |
3600 | { |
3601 | case AS_EXPLICIT: | |
3602 | if (sym->attr.dummy || sym->attr.result) | |
d74d8807 | 3603 | gfc_trans_dummy_array_bias (sym, sym->backend_decl, block); |
6de9cd9a DN |
3604 | else if (sym->attr.pointer || sym->attr.allocatable) |
3605 | { | |
3606 | if (TREE_STATIC (sym->backend_decl)) | |
ceccaacf TB |
3607 | { |
3608 | gfc_save_backend_locus (&loc); | |
3609 | gfc_set_backend_locus (&sym->declared_at); | |
3610 | gfc_trans_static_array_pointer (sym); | |
3611 | gfc_restore_backend_locus (&loc); | |
3612 | } | |
6de9cd9a | 3613 | else |
7114edca PT |
3614 | { |
3615 | seen_trans_deferred_array = true; | |
d74d8807 | 3616 | gfc_trans_deferred_array (sym, block); |
7114edca | 3617 | } |
6de9cd9a | 3618 | } |
9f3761c5 TB |
3619 | else if (sym->attr.codimension && TREE_STATIC (sym->backend_decl)) |
3620 | { | |
3621 | gfc_init_block (&tmpblock); | |
3622 | gfc_trans_array_cobounds (TREE_TYPE (sym->backend_decl), | |
3623 | &tmpblock, sym); | |
3624 | gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), | |
3625 | NULL_TREE); | |
3626 | continue; | |
3627 | } | |
b8ff4e88 | 3628 | else if (gfc_option.coarray != GFC_FCOARRAY_LIB) |
6de9cd9a | 3629 | { |
ceccaacf TB |
3630 | gfc_save_backend_locus (&loc); |
3631 | gfc_set_backend_locus (&sym->declared_at); | |
3632 | ||
7114edca PT |
3633 | if (sym_has_alloc_comp) |
3634 | { | |
3635 | seen_trans_deferred_array = true; | |
d74d8807 | 3636 | gfc_trans_deferred_array (sym, block); |
7114edca | 3637 | } |
b7b184a8 PT |
3638 | else if (sym->ts.type == BT_DERIVED |
3639 | && sym->value | |
3640 | && !sym->attr.data | |
3641 | && sym->attr.save == SAVE_NONE) | |
0019d498 DK |
3642 | { |
3643 | gfc_start_block (&tmpblock); | |
3644 | gfc_init_default_dt (sym, &tmpblock, false); | |
d74d8807 | 3645 | gfc_add_init_cleanup (block, |
0019d498 DK |
3646 | gfc_finish_block (&tmpblock), |
3647 | NULL_TREE); | |
3648 | } | |
7114edca | 3649 | |
0019d498 | 3650 | gfc_trans_auto_array_allocation (sym->backend_decl, |
d74d8807 | 3651 | sym, block); |
363aab21 | 3652 | gfc_restore_backend_locus (&loc); |
6de9cd9a DN |
3653 | } |
3654 | break; | |
3655 | ||
3656 | case AS_ASSUMED_SIZE: | |
3657 | /* Must be a dummy parameter. */ | |
b3aefde2 | 3658 | gcc_assert (sym->attr.dummy || sym->as->cp_was_assumed); |
6de9cd9a DN |
3659 | |
3660 | /* We should always pass assumed size arrays the g77 way. */ | |
b3aefde2 | 3661 | if (sym->attr.dummy) |
d74d8807 | 3662 | gfc_trans_g77_array (sym, block); |
0019d498 | 3663 | break; |
6de9cd9a DN |
3664 | |
3665 | case AS_ASSUMED_SHAPE: | |
3666 | /* Must be a dummy parameter. */ | |
6e45f57b | 3667 | gcc_assert (sym->attr.dummy); |
6de9cd9a | 3668 | |
d74d8807 | 3669 | gfc_trans_dummy_array_bias (sym, sym->backend_decl, block); |
6de9cd9a DN |
3670 | break; |
3671 | ||
3672 | case AS_DEFERRED: | |
7114edca | 3673 | seen_trans_deferred_array = true; |
d74d8807 | 3674 | gfc_trans_deferred_array (sym, block); |
6de9cd9a DN |
3675 | break; |
3676 | ||
3677 | default: | |
6e45f57b | 3678 | gcc_unreachable (); |
6de9cd9a | 3679 | } |
7114edca | 3680 | if (sym_has_alloc_comp && !seen_trans_deferred_array) |
d74d8807 | 3681 | gfc_trans_deferred_array (sym, block); |
6de9cd9a | 3682 | } |
c49ea23d PT |
3683 | else if ((!sym->attr.dummy || sym->ts.deferred) |
3684 | && (sym->ts.type == BT_CLASS | |
102344e2 | 3685 | && CLASS_DATA (sym)->attr.class_pointer)) |
1b26c26b | 3686 | continue; |
8d51f26f | 3687 | else if ((!sym->attr.dummy || sym->ts.deferred) |
25cbe58f TB |
3688 | && (sym->attr.allocatable |
3689 | || (sym->ts.type == BT_CLASS | |
3690 | && CLASS_DATA (sym)->attr.allocatable))) | |
1517fd57 | 3691 | { |
64b33a7e TB |
3692 | if (!sym->attr.save) |
3693 | { | |
5d81ddd0 TB |
3694 | tree descriptor = NULL_TREE; |
3695 | ||
64b33a7e TB |
3696 | /* Nullify and automatic deallocation of allocatable |
3697 | scalars. */ | |
64b33a7e TB |
3698 | e = gfc_lval_expr_from_sym (sym); |
3699 | if (sym->ts.type == BT_CLASS) | |
b04533af | 3700 | gfc_add_data_component (e); |
64b33a7e TB |
3701 | |
3702 | gfc_init_se (&se, NULL); | |
c49ea23d PT |
3703 | if (sym->ts.type != BT_CLASS |
3704 | || sym->ts.u.derived->attr.dimension | |
3705 | || sym->ts.u.derived->attr.codimension) | |
3706 | { | |
3707 | se.want_pointer = 1; | |
3708 | gfc_conv_expr (&se, e); | |
3709 | } | |
3710 | else if (sym->ts.type == BT_CLASS | |
3711 | && !CLASS_DATA (sym)->attr.dimension | |
3712 | && !CLASS_DATA (sym)->attr.codimension) | |
3713 | { | |
3714 | se.want_pointer = 1; | |
3715 | gfc_conv_expr (&se, e); | |
3716 | } | |
3717 | else | |
3718 | { | |
3719 | gfc_conv_expr (&se, e); | |
5d81ddd0 | 3720 | descriptor = se.expr; |
c49ea23d PT |
3721 | se.expr = gfc_conv_descriptor_data_addr (se.expr); |
3722 | se.expr = build_fold_indirect_ref_loc (input_location, se.expr); | |
3723 | } | |
64b33a7e TB |
3724 | gfc_free_expr (e); |
3725 | ||
ceccaacf TB |
3726 | gfc_save_backend_locus (&loc); |
3727 | gfc_set_backend_locus (&sym->declared_at); | |
0019d498 | 3728 | gfc_start_block (&init); |
8d51f26f PT |
3729 | |
3730 | if (!sym->attr.dummy || sym->attr.intent == INTENT_OUT) | |
3731 | { | |
3732 | /* Nullify when entering the scope. */ | |
3733 | gfc_add_modify (&init, se.expr, | |
3734 | fold_convert (TREE_TYPE (se.expr), | |
3735 | null_pointer_node)); | |
3736 | } | |
3737 | ||
3738 | if ((sym->attr.dummy ||sym->attr.result) | |
3739 | && sym->ts.type == BT_CHARACTER | |
3740 | && sym->ts.deferred) | |
3741 | { | |
3742 | /* Character length passed by reference. */ | |
3743 | tmp = sym->ts.u.cl->passed_length; | |
3744 | tmp = build_fold_indirect_ref_loc (input_location, tmp); | |
3745 | tmp = fold_convert (gfc_charlen_type_node, tmp); | |
3746 | ||
3747 | if (!sym->attr.dummy || sym->attr.intent == INTENT_OUT) | |
3748 | /* Zero the string length when entering the scope. */ | |
3749 | gfc_add_modify (&init, sym->ts.u.cl->backend_decl, | |
3750 | build_int_cst (gfc_charlen_type_node, 0)); | |
3751 | else | |
3752 | gfc_add_modify (&init, sym->ts.u.cl->backend_decl, tmp); | |
3753 | ||
ceccaacf TB |
3754 | gfc_restore_backend_locus (&loc); |
3755 | ||
8d51f26f PT |
3756 | /* Pass the final character length back. */ |
3757 | if (sym->attr.intent != INTENT_IN) | |
3758 | tmp = fold_build2_loc (input_location, MODIFY_EXPR, | |
3759 | gfc_charlen_type_node, tmp, | |
3760 | sym->ts.u.cl->backend_decl); | |
3761 | else | |
3762 | tmp = NULL_TREE; | |
3763 | } | |
ceccaacf TB |
3764 | else |
3765 | gfc_restore_backend_locus (&loc); | |
64b33a7e TB |
3766 | |
3767 | /* Deallocate when leaving the scope. Nullifying is not | |
3768 | needed. */ | |
8d51f26f | 3769 | if (!sym->attr.result && !sym->attr.dummy) |
5d81ddd0 TB |
3770 | { |
3771 | if (sym->ts.type == BT_CLASS | |
3772 | && CLASS_DATA (sym)->attr.codimension) | |
3773 | tmp = gfc_deallocate_with_status (descriptor, NULL_TREE, | |
3774 | NULL_TREE, NULL_TREE, | |
3775 | NULL_TREE, true, NULL, | |
3776 | true); | |
3777 | else | |
3778 | tmp = gfc_deallocate_scalar_with_status (se.expr, NULL, | |
3779 | true, NULL, | |
3780 | sym->ts); | |
3781 | } | |
8c077737 JW |
3782 | if (sym->ts.type == BT_CLASS) |
3783 | { | |
3784 | /* Initialize _vptr to declared type. */ | |
3785 | gfc_symbol *vtab = gfc_find_derived_vtab (sym->ts.u.derived); | |
3786 | tree rhs; | |
ceccaacf TB |
3787 | |
3788 | gfc_save_backend_locus (&loc); | |
3789 | gfc_set_backend_locus (&sym->declared_at); | |
8c077737 JW |
3790 | e = gfc_lval_expr_from_sym (sym); |
3791 | gfc_add_vptr_component (e); | |
3792 | gfc_init_se (&se, NULL); | |
3793 | se.want_pointer = 1; | |
3794 | gfc_conv_expr (&se, e); | |
3795 | gfc_free_expr (e); | |
3796 | rhs = gfc_build_addr_expr (TREE_TYPE (se.expr), | |
3797 | gfc_get_symbol_decl (vtab)); | |
3798 | gfc_add_modify (&init, se.expr, rhs); | |
ceccaacf | 3799 | gfc_restore_backend_locus (&loc); |
8c077737 JW |
3800 | } |
3801 | ||
d74d8807 | 3802 | gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp); |
64b33a7e | 3803 | } |
1517fd57 | 3804 | } |
8d51f26f PT |
3805 | else if (sym->ts.type == BT_CHARACTER && sym->ts.deferred) |
3806 | { | |
3807 | tree tmp = NULL; | |
3808 | stmtblock_t init; | |
3809 | ||
3810 | /* If we get to here, all that should be left are pointers. */ | |
3811 | gcc_assert (sym->attr.pointer); | |
3812 | ||
3813 | if (sym->attr.dummy) | |
3814 | { | |
3815 | gfc_start_block (&init); | |
3816 | ||
3817 | /* Character length passed by reference. */ | |
3818 | tmp = sym->ts.u.cl->passed_length; | |
3819 | tmp = build_fold_indirect_ref_loc (input_location, tmp); | |
3820 | tmp = fold_convert (gfc_charlen_type_node, tmp); | |
3821 | gfc_add_modify (&init, sym->ts.u.cl->backend_decl, tmp); | |
3822 | /* Pass the final character length back. */ | |
3823 | if (sym->attr.intent != INTENT_IN) | |
3824 | tmp = fold_build2_loc (input_location, MODIFY_EXPR, | |
3825 | gfc_charlen_type_node, tmp, | |
3826 | sym->ts.u.cl->backend_decl); | |
3827 | else | |
3828 | tmp = NULL_TREE; | |
3829 | gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp); | |
3830 | } | |
3831 | } | |
e69afb29 SK |
3832 | else if (sym->ts.deferred) |
3833 | gfc_fatal_error ("Deferred type parameter not yet supported"); | |
3a28464c | 3834 | else if (sym_has_alloc_comp) |
d74d8807 | 3835 | gfc_trans_deferred_array (sym, block); |
6de9cd9a DN |
3836 | else if (sym->ts.type == BT_CHARACTER) |
3837 | { | |
363aab21 | 3838 | gfc_save_backend_locus (&loc); |
6de9cd9a DN |
3839 | gfc_set_backend_locus (&sym->declared_at); |
3840 | if (sym->attr.dummy || sym->attr.result) | |
d74d8807 | 3841 | gfc_trans_dummy_character (sym, sym->ts.u.cl, block); |
6de9cd9a | 3842 | else |
d74d8807 | 3843 | gfc_trans_auto_character_variable (sym, block); |
363aab21 | 3844 | gfc_restore_backend_locus (&loc); |
6de9cd9a | 3845 | } |
910450c1 FW |
3846 | else if (sym->attr.assign) |
3847 | { | |
363aab21 | 3848 | gfc_save_backend_locus (&loc); |
910450c1 | 3849 | gfc_set_backend_locus (&sym->declared_at); |
d74d8807 | 3850 | gfc_trans_assign_aux_var (sym, block); |
363aab21 | 3851 | gfc_restore_backend_locus (&loc); |
910450c1 | 3852 | } |
b7b184a8 PT |
3853 | else if (sym->ts.type == BT_DERIVED |
3854 | && sym->value | |
3855 | && !sym->attr.data | |
3856 | && sym->attr.save == SAVE_NONE) | |
0019d498 DK |
3857 | { |
3858 | gfc_start_block (&tmpblock); | |
3859 | gfc_init_default_dt (sym, &tmpblock, false); | |
d74d8807 | 3860 | gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), |
0019d498 DK |
3861 | NULL_TREE); |
3862 | } | |
6de9cd9a | 3863 | else |
6e45f57b | 3864 | gcc_unreachable (); |
6de9cd9a DN |
3865 | } |
3866 | ||
0019d498 | 3867 | gfc_init_block (&tmpblock); |
417ab240 JJ |
3868 | |
3869 | for (f = proc_sym->formal; f; f = f->next) | |
08113c73 PT |
3870 | { |
3871 | if (f->sym && f->sym->tlink == NULL && f->sym->ts.type == BT_CHARACTER) | |
3872 | { | |
bc21d315 JW |
3873 | gcc_assert (f->sym->ts.u.cl->backend_decl != NULL); |
3874 | if (TREE_CODE (f->sym->ts.u.cl->backend_decl) == PARM_DECL) | |
0019d498 | 3875 | gfc_trans_vla_type_sizes (f->sym, &tmpblock); |
08113c73 | 3876 | } |
08113c73 | 3877 | } |
417ab240 JJ |
3878 | |
3879 | if (gfc_return_by_reference (proc_sym) && proc_sym->ts.type == BT_CHARACTER | |
3880 | && current_fake_result_decl != NULL) | |
3881 | { | |
bc21d315 JW |
3882 | gcc_assert (proc_sym->ts.u.cl->backend_decl != NULL); |
3883 | if (TREE_CODE (proc_sym->ts.u.cl->backend_decl) == PARM_DECL) | |
0019d498 | 3884 | gfc_trans_vla_type_sizes (proc_sym, &tmpblock); |
417ab240 JJ |
3885 | } |
3886 | ||
d74d8807 | 3887 | gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), NULL_TREE); |
6de9cd9a DN |
3888 | } |
3889 | ||
a64f5186 JJ |
3890 | static GTY ((param_is (struct module_htab_entry))) htab_t module_htab; |
3891 | ||
3892 | /* Hash and equality functions for module_htab. */ | |
3893 | ||
3894 | static hashval_t | |
3895 | module_htab_do_hash (const void *x) | |
3896 | { | |
3897 | return htab_hash_string (((const struct module_htab_entry *)x)->name); | |
3898 | } | |
3899 | ||
3900 | static int | |
3901 | module_htab_eq (const void *x1, const void *x2) | |
3902 | { | |
3903 | return strcmp ((((const struct module_htab_entry *)x1)->name), | |
3904 | (const char *)x2) == 0; | |
3905 | } | |
3906 | ||
3907 | /* Hash and equality functions for module_htab's decls. */ | |
3908 | ||
3909 | static hashval_t | |
3910 | module_htab_decls_hash (const void *x) | |
3911 | { | |
3912 | const_tree t = (const_tree) x; | |
3913 | const_tree n = DECL_NAME (t); | |
3914 | if (n == NULL_TREE) | |
3915 | n = TYPE_NAME (TREE_TYPE (t)); | |
afdda4b4 | 3916 | return htab_hash_string (IDENTIFIER_POINTER (n)); |
a64f5186 JJ |
3917 | } |
3918 | ||
3919 | static int | |
3920 | module_htab_decls_eq (const void *x1, const void *x2) | |
3921 | { | |
3922 | const_tree t1 = (const_tree) x1; | |
3923 | const_tree n1 = DECL_NAME (t1); | |
3924 | if (n1 == NULL_TREE) | |
3925 | n1 = TYPE_NAME (TREE_TYPE (t1)); | |
3926 | return strcmp (IDENTIFIER_POINTER (n1), (const char *) x2) == 0; | |
3927 | } | |
3928 | ||
3929 | struct module_htab_entry * | |
3930 | gfc_find_module (const char *name) | |
3931 | { | |
3932 | void **slot; | |
3933 | ||
3934 | if (! module_htab) | |
3935 | module_htab = htab_create_ggc (10, module_htab_do_hash, | |
3936 | module_htab_eq, NULL); | |
3937 | ||
3938 | slot = htab_find_slot_with_hash (module_htab, name, | |
3939 | htab_hash_string (name), INSERT); | |
3940 | if (*slot == NULL) | |
3941 | { | |
a9429e29 | 3942 | struct module_htab_entry *entry = ggc_alloc_cleared_module_htab_entry (); |
a64f5186 JJ |
3943 | |
3944 | entry->name = gfc_get_string (name); | |
3945 | entry->decls = htab_create_ggc (10, module_htab_decls_hash, | |
3946 | module_htab_decls_eq, NULL); | |
3947 | *slot = (void *) entry; | |
3948 | } | |
3949 | return (struct module_htab_entry *) *slot; | |
3950 | } | |
3951 | ||
3952 | void | |
3953 | gfc_module_add_decl (struct module_htab_entry *entry, tree decl) | |
3954 | { | |
3955 | void **slot; | |
3956 | const char *name; | |
3957 | ||
3958 | if (DECL_NAME (decl)) | |
3959 | name = IDENTIFIER_POINTER (DECL_NAME (decl)); | |
3960 | else | |
3961 | { | |
3962 | gcc_assert (TREE_CODE (decl) == TYPE_DECL); | |
3963 | name = IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))); | |
3964 | } | |
3965 | slot = htab_find_slot_with_hash (entry->decls, name, | |
3966 | htab_hash_string (name), INSERT); | |
3967 | if (*slot == NULL) | |
3968 | *slot = (void *) decl; | |
3969 | } | |
3970 | ||
3971 | static struct module_htab_entry *cur_module; | |
6de9cd9a DN |
3972 | |
3973 | /* Output an initialized decl for a module variable. */ | |
3974 | ||
3975 | static void | |
3976 | gfc_create_module_variable (gfc_symbol * sym) | |
3977 | { | |
3978 | tree decl; | |
6de9cd9a | 3979 | |
1a492601 PT |
3980 | /* Module functions with alternate entries are dealt with later and |
3981 | would get caught by the next condition. */ | |
3982 | if (sym->attr.entry) | |
3983 | return; | |
3984 | ||
a8b3b0b6 CR |
3985 | /* Make sure we convert the types of the derived types from iso_c_binding |
3986 | into (void *). */ | |
3987 | if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c | |
3988 | && sym->ts.type == BT_DERIVED) | |
3989 | sym->backend_decl = gfc_typenode_for_spec (&(sym->ts)); | |
3990 | ||
a64f5186 JJ |
3991 | if (sym->attr.flavor == FL_DERIVED |
3992 | && sym->backend_decl | |
3993 | && TREE_CODE (sym->backend_decl) == RECORD_TYPE) | |
3994 | { | |
3995 | decl = sym->backend_decl; | |
3996 | gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE); | |
b8849663 PT |
3997 | |
3998 | /* -fwhole-file mixes up the contexts so these asserts are unnecessary. */ | |
3999 | if (!(gfc_option.flag_whole_file && sym->attr.use_assoc)) | |
4000 | { | |
4001 | gcc_assert (TYPE_CONTEXT (decl) == NULL_TREE | |
4002 | || TYPE_CONTEXT (decl) == sym->ns->proc_name->backend_decl); | |
4003 | gcc_assert (DECL_CONTEXT (TYPE_STUB_DECL (decl)) == NULL_TREE | |
4004 | || DECL_CONTEXT (TYPE_STUB_DECL (decl)) | |
4005 | == sym->ns->proc_name->backend_decl); | |
4006 | } | |
a64f5186 JJ |
4007 | TYPE_CONTEXT (decl) = sym->ns->proc_name->backend_decl; |
4008 | DECL_CONTEXT (TYPE_STUB_DECL (decl)) = sym->ns->proc_name->backend_decl; | |
4009 | gfc_module_add_decl (cur_module, TYPE_STUB_DECL (decl)); | |
4010 | } | |
4011 | ||
6e0d2de7 JW |
4012 | /* Only output variables, procedure pointers and array valued, |
4013 | or derived type, parameters. */ | |
6de9cd9a | 4014 | if (sym->attr.flavor != FL_VARIABLE |
fdc55763 | 4015 | && !(sym->attr.flavor == FL_PARAMETER |
6e0d2de7 JW |
4016 | && (sym->attr.dimension || sym->ts.type == BT_DERIVED)) |
4017 | && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer)) | |
6de9cd9a DN |
4018 | return; |
4019 | ||
a64f5186 JJ |
4020 | if ((sym->attr.in_common || sym->attr.in_equivalence) && sym->backend_decl) |
4021 | { | |
4022 | decl = sym->backend_decl; | |
e5b16755 | 4023 | gcc_assert (DECL_FILE_SCOPE_P (decl)); |
a64f5186 JJ |
4024 | gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE); |
4025 | DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl; | |
4026 | gfc_module_add_decl (cur_module, decl); | |
4027 | } | |
4028 | ||
9cbf8b41 TS |
4029 | /* Don't generate variables from other modules. Variables from |
4030 | COMMONs will already have been generated. */ | |
4031 | if (sym->attr.use_assoc || sym->attr.in_common) | |
6de9cd9a DN |
4032 | return; |
4033 | ||
30aabb86 | 4034 | /* Equivalenced variables arrive here after creation. */ |
b95605fb | 4035 | if (sym->backend_decl |
a64f5186 JJ |
4036 | && (sym->equiv_built || sym->attr.in_equivalence)) |
4037 | return; | |
30aabb86 | 4038 | |
80f95228 | 4039 | if (sym->backend_decl && !sym->attr.vtab && !sym->attr.target) |
6de9cd9a DN |
4040 | internal_error ("backend decl for module variable %s already exists", |
4041 | sym->name); | |
4042 | ||
4043 | /* We always want module variables to be created. */ | |
4044 | sym->attr.referenced = 1; | |
4045 | /* Create the decl. */ | |
4046 | decl = gfc_get_symbol_decl (sym); | |
4047 | ||
6de9cd9a DN |
4048 | /* Create the variable. */ |
4049 | pushdecl (decl); | |
a64f5186 JJ |
4050 | gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE); |
4051 | DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl; | |
0e6df31e | 4052 | rest_of_decl_compilation (decl, 1, 0); |
a64f5186 | 4053 | gfc_module_add_decl (cur_module, decl); |
6de9cd9a DN |
4054 | |
4055 | /* Also add length of strings. */ | |
4056 | if (sym->ts.type == BT_CHARACTER) | |
4057 | { | |
4058 | tree length; | |
4059 | ||
bc21d315 | 4060 | length = sym->ts.u.cl->backend_decl; |
9c4174d8 PT |
4061 | gcc_assert (length || sym->attr.proc_pointer); |
4062 | if (length && !INTEGER_CST_P (length)) | |
6de9cd9a DN |
4063 | { |
4064 | pushdecl (length); | |
0e6df31e | 4065 | rest_of_decl_compilation (length, 1, 0); |
6de9cd9a DN |
4066 | } |
4067 | } | |
b8ff4e88 TB |
4068 | |
4069 | if (sym->attr.codimension && !sym->attr.dummy && !sym->attr.allocatable | |
4070 | && sym->attr.referenced && !sym->attr.use_assoc) | |
4071 | has_coarray_vars = true; | |
6de9cd9a DN |
4072 | } |
4073 | ||
9268ba9a | 4074 | /* Emit debug information for USE statements. */ |
a64f5186 JJ |
4075 | |
4076 | static void | |
4077 | gfc_trans_use_stmts (gfc_namespace * ns) | |
4078 | { | |
4079 | gfc_use_list *use_stmt; | |
4080 | for (use_stmt = ns->use_stmts; use_stmt; use_stmt = use_stmt->next) | |
4081 | { | |
4082 | struct module_htab_entry *entry | |
4083 | = gfc_find_module (use_stmt->module_name); | |
4084 | gfc_use_rename *rent; | |
4085 | ||
4086 | if (entry->namespace_decl == NULL) | |
4087 | { | |
4088 | entry->namespace_decl | |
c2255bc4 AH |
4089 | = build_decl (input_location, |
4090 | NAMESPACE_DECL, | |
a64f5186 JJ |
4091 | get_identifier (use_stmt->module_name), |
4092 | void_type_node); | |
4093 | DECL_EXTERNAL (entry->namespace_decl) = 1; | |
4094 | } | |
9268ba9a | 4095 | gfc_set_backend_locus (&use_stmt->where); |
a64f5186 JJ |
4096 | if (!use_stmt->only_flag) |
4097 | (*debug_hooks->imported_module_or_decl) (entry->namespace_decl, | |
4098 | NULL_TREE, | |
4099 | ns->proc_name->backend_decl, | |
4100 | false); | |
4101 | for (rent = use_stmt->rename; rent; rent = rent->next) | |
4102 | { | |
4103 | tree decl, local_name; | |
4104 | void **slot; | |
4105 | ||
4106 | if (rent->op != INTRINSIC_NONE) | |
4107 | continue; | |
4108 | ||
4109 | slot = htab_find_slot_with_hash (entry->decls, rent->use_name, | |
4110 | htab_hash_string (rent->use_name), | |
4111 | INSERT); | |
4112 | if (*slot == NULL) | |
4113 | { | |
4114 | gfc_symtree *st; | |
4115 | ||
4116 | st = gfc_find_symtree (ns->sym_root, | |
4117 | rent->local_name[0] | |
4118 | ? rent->local_name : rent->use_name); | |
c3f34952 TB |
4119 | |
4120 | /* The following can happen if a derived type is renamed. */ | |
4121 | if (!st) | |
4122 | { | |
4123 | char *name; | |
4124 | name = xstrdup (rent->local_name[0] | |
4125 | ? rent->local_name : rent->use_name); | |
4126 | name[0] = (char) TOUPPER ((unsigned char) name[0]); | |
4127 | st = gfc_find_symtree (ns->sym_root, name); | |
4128 | free (name); | |
4129 | gcc_assert (st); | |
4130 | } | |
1151ccc9 PT |
4131 | |
4132 | /* Sometimes, generic interfaces wind up being over-ruled by a | |
4133 | local symbol (see PR41062). */ | |
4134 | if (!st->n.sym->attr.use_assoc) | |
4135 | continue; | |
4136 | ||
9268ba9a JJ |
4137 | if (st->n.sym->backend_decl |
4138 | && DECL_P (st->n.sym->backend_decl) | |
4139 | && st->n.sym->module | |
4140 | && strcmp (st->n.sym->module, use_stmt->module_name) == 0) | |
a64f5186 | 4141 | { |
9268ba9a JJ |
4142 | gcc_assert (DECL_EXTERNAL (entry->namespace_decl) |
4143 | || (TREE_CODE (st->n.sym->backend_decl) | |
4144 | != VAR_DECL)); | |
a64f5186 JJ |
4145 | decl = copy_node (st->n.sym->backend_decl); |
4146 | DECL_CONTEXT (decl) = entry->namespace_decl; | |
4147 | DECL_EXTERNAL (decl) = 1; | |
4148 | DECL_IGNORED_P (decl) = 0; | |
4149 | DECL_INITIAL (decl) = NULL_TREE; | |
4150 | } | |
4151 | else | |
4152 | { | |
4153 | *slot = error_mark_node; | |
4154 | htab_clear_slot (entry->decls, slot); | |
4155 | continue; | |
4156 | } | |
4157 | *slot = decl; | |
4158 | } | |
4159 | decl = (tree) *slot; | |
4160 | if (rent->local_name[0]) | |
4161 | local_name = get_identifier (rent->local_name); | |
4162 | else | |
4163 | local_name = NULL_TREE; | |
9268ba9a | 4164 | gfc_set_backend_locus (&rent->where); |
a64f5186 JJ |
4165 | (*debug_hooks->imported_module_or_decl) (decl, local_name, |
4166 | ns->proc_name->backend_decl, | |
4167 | !use_stmt->only_flag); | |
4168 | } | |
4169 | } | |
6de9cd9a DN |
4170 | } |
4171 | ||
9268ba9a | 4172 | |
bd11e37d JJ |
4173 | /* Return true if expr is a constant initializer that gfc_conv_initializer |
4174 | will handle. */ | |
4175 | ||
4176 | static bool | |
4177 | check_constant_initializer (gfc_expr *expr, gfc_typespec *ts, bool array, | |
4178 | bool pointer) | |
4179 | { | |
4180 | gfc_constructor *c; | |
4181 | gfc_component *cm; | |
4182 | ||
4183 | if (pointer) | |
4184 | return true; | |
4185 | else if (array) | |
4186 | { | |
4187 | if (expr->expr_type == EXPR_CONSTANT || expr->expr_type == EXPR_NULL) | |
4188 | return true; | |
4189 | else if (expr->expr_type == EXPR_STRUCTURE) | |
4190 | return check_constant_initializer (expr, ts, false, false); | |
4191 | else if (expr->expr_type != EXPR_ARRAY) | |
4192 | return false; | |
b7e75771 JD |
4193 | for (c = gfc_constructor_first (expr->value.constructor); |
4194 | c; c = gfc_constructor_next (c)) | |
bd11e37d JJ |
4195 | { |
4196 | if (c->iterator) | |
4197 | return false; | |
4198 | if (c->expr->expr_type == EXPR_STRUCTURE) | |
4199 | { | |
4200 | if (!check_constant_initializer (c->expr, ts, false, false)) | |
4201 | return false; | |
4202 | } | |
4203 | else if (c->expr->expr_type != EXPR_CONSTANT) | |
4204 | return false; | |
4205 | } | |
4206 | return true; | |
4207 | } | |
4208 | else switch (ts->type) | |
4209 | { | |
4210 | case BT_DERIVED: | |
4211 | if (expr->expr_type != EXPR_STRUCTURE) | |
4212 | return false; | |
bc21d315 | 4213 | cm = expr->ts.u.derived->components; |
b7e75771 JD |
4214 | for (c = gfc_constructor_first (expr->value.constructor); |
4215 | c; c = gfc_constructor_next (c), cm = cm->next) | |
bd11e37d JJ |
4216 | { |
4217 | if (!c->expr || cm->attr.allocatable) | |
4218 | continue; | |
4219 | if (!check_constant_initializer (c->expr, &cm->ts, | |
4220 | cm->attr.dimension, | |
4221 | cm->attr.pointer)) | |
4222 | return false; | |
4223 | } | |
4224 | return true; | |
4225 | default: | |
4226 | return expr->expr_type == EXPR_CONSTANT; | |
4227 | } | |
4228 | } | |
4229 | ||
4230 | /* Emit debug info for parameters and unreferenced variables with | |
4231 | initializers. */ | |
4232 | ||
4233 | static void | |
4234 | gfc_emit_parameter_debug_info (gfc_symbol *sym) | |
4235 | { | |
4236 | tree decl; | |
4237 | ||
4238 | if (sym->attr.flavor != FL_PARAMETER | |
4239 | && (sym->attr.flavor != FL_VARIABLE || sym->attr.referenced)) | |
4240 | return; | |
4241 | ||
4242 | if (sym->backend_decl != NULL | |
4243 | || sym->value == NULL | |
4244 | || sym->attr.use_assoc | |
4245 | || sym->attr.dummy | |
4246 | || sym->attr.result | |
4247 | || sym->attr.function | |
4248 | || sym->attr.intrinsic | |
4249 | || sym->attr.pointer | |
4250 | || sym->attr.allocatable | |
4251 | || sym->attr.cray_pointee | |
4252 | || sym->attr.threadprivate | |
4253 | || sym->attr.is_bind_c | |
4254 | || sym->attr.subref_array_pointer | |
4255 | || sym->attr.assign) | |
4256 | return; | |
4257 | ||
4258 | if (sym->ts.type == BT_CHARACTER) | |
4259 | { | |
bc21d315 JW |
4260 | gfc_conv_const_charlen (sym->ts.u.cl); |
4261 | if (sym->ts.u.cl->backend_decl == NULL | |
4262 | || TREE_CODE (sym->ts.u.cl->backend_decl) != INTEGER_CST) | |
bd11e37d JJ |
4263 | return; |
4264 | } | |
bc21d315 | 4265 | else if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.alloc_comp) |
bd11e37d JJ |
4266 | return; |
4267 | ||
4268 | if (sym->as) | |
4269 | { | |
4270 | int n; | |
4271 | ||
4272 | if (sym->as->type != AS_EXPLICIT) | |
4273 | return; | |
4274 | for (n = 0; n < sym->as->rank; n++) | |
4275 | if (sym->as->lower[n]->expr_type != EXPR_CONSTANT | |
4276 | || sym->as->upper[n] == NULL | |
4277 | || sym->as->upper[n]->expr_type != EXPR_CONSTANT) | |
4278 | return; | |
4279 | } | |
4280 | ||
4281 | if (!check_constant_initializer (sym->value, &sym->ts, | |
4282 | sym->attr.dimension, false)) | |
4283 | return; | |
4284 | ||
b8ff4e88 TB |
4285 | if (gfc_option.coarray == GFC_FCOARRAY_LIB && sym->attr.codimension) |
4286 | return; | |
4287 | ||
bd11e37d | 4288 | /* Create the decl for the variable or constant. */ |
c2255bc4 AH |
4289 | decl = build_decl (input_location, |
4290 | sym->attr.flavor == FL_PARAMETER ? CONST_DECL : VAR_DECL, | |
bd11e37d JJ |
4291 | gfc_sym_identifier (sym), gfc_sym_type (sym)); |
4292 | if (sym->attr.flavor == FL_PARAMETER) | |
4293 | TREE_READONLY (decl) = 1; | |
4294 | gfc_set_decl_location (decl, &sym->declared_at); | |
4295 | if (sym->attr.dimension) | |
4296 | GFC_DECL_PACKED_ARRAY (decl) = 1; | |
4297 | DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl; | |
4298 | TREE_STATIC (decl) = 1; | |
4299 | TREE_USED (decl) = 1; | |
4300 | if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL) | |
4301 | TREE_PUBLIC (decl) = 1; | |
1d0134b3 JW |
4302 | DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts, |
4303 | TREE_TYPE (decl), | |
4304 | sym->attr.dimension, | |
4305 | false, false); | |
bd11e37d JJ |
4306 | debug_hooks->global_decl (decl); |
4307 | } | |
4308 | ||
b8ff4e88 TB |
4309 | |
4310 | static void | |
4311 | generate_coarray_sym_init (gfc_symbol *sym) | |
4312 | { | |
4313 | tree tmp, size, decl, token; | |
4314 | ||
4315 | if (sym->attr.dummy || sym->attr.allocatable || !sym->attr.codimension | |
4316 | || sym->attr.use_assoc || !sym->attr.referenced) | |
4317 | return; | |
4318 | ||
4319 | decl = sym->backend_decl; | |
4320 | TREE_USED(decl) = 1; | |
4321 | gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (decl))); | |
4322 | ||
4323 | /* FIXME: Workaround for PR middle-end/49106, cf. also PR middle-end/49108 | |
4324 | to make sure the variable is not optimized away. */ | |
4325 | DECL_PRESERVE_P (DECL_CONTEXT (decl)) = 1; | |
4326 | ||
4327 | size = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (decl))); | |
4328 | ||
107a9bc9 TB |
4329 | /* Ensure that we do not have size=0 for zero-sized arrays. */ |
4330 | size = fold_build2_loc (input_location, MAX_EXPR, size_type_node, | |
4331 | fold_convert (size_type_node, size), | |
4332 | build_int_cst (size_type_node, 1)); | |
4333 | ||
b8ff4e88 TB |
4334 | if (GFC_TYPE_ARRAY_RANK (TREE_TYPE (decl))) |
4335 | { | |
4336 | tmp = GFC_TYPE_ARRAY_SIZE (TREE_TYPE (decl)); | |
4337 | size = fold_build2_loc (input_location, MULT_EXPR, size_type_node, | |
107a9bc9 | 4338 | fold_convert (size_type_node, tmp), size); |
b8ff4e88 TB |
4339 | } |
4340 | ||
4341 | gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (decl)) != NULL_TREE); | |
4342 | token = gfc_build_addr_expr (ppvoid_type_node, | |
4343 | GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE(decl))); | |
4344 | ||
4345 | tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_register, 6, size, | |
ea6363a3 | 4346 | build_int_cst (integer_type_node, |
86187d0f | 4347 | GFC_CAF_COARRAY_STATIC), /* type. */ |
b8ff4e88 TB |
4348 | token, null_pointer_node, /* token, stat. */ |
4349 | null_pointer_node, /* errgmsg, errmsg_len. */ | |
4350 | build_int_cst (integer_type_node, 0)); | |
4351 | ||
4352 | gfc_add_modify (&caf_init_block, decl, fold_convert (TREE_TYPE (decl), tmp)); | |
4353 | ||
4354 | ||
4355 | /* Handle "static" initializer. */ | |
4356 | if (sym->value) | |
4357 | { | |
4358 | sym->attr.pointer = 1; | |
4359 | tmp = gfc_trans_assignment (gfc_lval_expr_from_sym (sym), sym->value, | |
4360 | true, false); | |
4361 | sym->attr.pointer = 0; | |
4362 | gfc_add_expr_to_block (&caf_init_block, tmp); | |
4363 | } | |
4364 | } | |
4365 | ||
4366 | ||
4367 | /* Generate constructor function to initialize static, nonallocatable | |
4368 | coarrays. */ | |
4369 | ||
4370 | static void | |
4371 | generate_coarray_init (gfc_namespace * ns __attribute((unused))) | |
4372 | { | |
4373 | tree fndecl, tmp, decl, save_fn_decl; | |
4374 | ||
4375 | save_fn_decl = current_function_decl; | |
4376 | push_function_context (); | |
4377 | ||
4378 | tmp = build_function_type_list (void_type_node, NULL_TREE); | |
4379 | fndecl = build_decl (input_location, FUNCTION_DECL, | |
4380 | create_tmp_var_name ("_caf_init"), tmp); | |
4381 | ||
4382 | DECL_STATIC_CONSTRUCTOR (fndecl) = 1; | |
4383 | SET_DECL_INIT_PRIORITY (fndecl, DEFAULT_INIT_PRIORITY); | |
4384 | ||
4385 | decl = build_decl (input_location, RESULT_DECL, NULL_TREE, void_type_node); | |
4386 | DECL_ARTIFICIAL (decl) = 1; | |
4387 | DECL_IGNORED_P (decl) = 1; | |
4388 | DECL_CONTEXT (decl) = fndecl; | |
4389 | DECL_RESULT (fndecl) = decl; | |
4390 | ||
4391 | pushdecl (fndecl); | |
4392 | current_function_decl = fndecl; | |
4393 | announce_function (fndecl); | |
4394 | ||
4395 | rest_of_decl_compilation (fndecl, 0, 0); | |
4396 | make_decl_rtl (fndecl); | |
4397 | init_function_start (fndecl); | |
4398 | ||
87a60f68 | 4399 | pushlevel (); |
b8ff4e88 TB |
4400 | gfc_init_block (&caf_init_block); |
4401 | ||
4402 | gfc_traverse_ns (ns, generate_coarray_sym_init); | |
4403 | ||
4404 | DECL_SAVED_TREE (fndecl) = gfc_finish_block (&caf_init_block); | |
4405 | decl = getdecls (); | |
4406 | ||
87a60f68 | 4407 | poplevel (1, 1); |
b8ff4e88 TB |
4408 | BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; |
4409 | ||
4410 | DECL_SAVED_TREE (fndecl) | |
4411 | = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (fndecl), | |
4412 | DECL_INITIAL (fndecl)); | |
4413 | dump_function (TDI_original, fndecl); | |
4414 | ||
4415 | cfun->function_end_locus = input_location; | |
4416 | set_cfun (NULL); | |
4417 | ||
4418 | if (decl_function_context (fndecl)) | |
4419 | (void) cgraph_create_node (fndecl); | |
4420 | else | |
4421 | cgraph_finalize_function (fndecl, true); | |
4422 | ||
4423 | pop_function_context (); | |
4424 | current_function_decl = save_fn_decl; | |
4425 | } | |
4426 | ||
4427 | ||
9268ba9a JJ |
4428 | /* Generate all the required code for module variables. */ |
4429 | ||
4430 | void | |
4431 | gfc_generate_module_vars (gfc_namespace * ns) | |
4432 | { | |
4433 | module_namespace = ns; | |
4434 | cur_module = gfc_find_module (ns->proc_name->name); | |
4435 | ||
4436 | /* Check if the frontend left the namespace in a reasonable state. */ | |
4437 | gcc_assert (ns->proc_name && !ns->proc_name->tlink); | |
4438 | ||
4439 | /* Generate COMMON blocks. */ | |
4440 | gfc_trans_common (ns); | |
4441 | ||
b8ff4e88 TB |
4442 | has_coarray_vars = false; |
4443 | ||
9268ba9a JJ |
4444 | /* Create decls for all the module variables. */ |
4445 | gfc_traverse_ns (ns, gfc_create_module_variable); | |
4446 | ||
b8ff4e88 TB |
4447 | if (gfc_option.coarray == GFC_FCOARRAY_LIB && has_coarray_vars) |
4448 | generate_coarray_init (ns); | |
4449 | ||
9268ba9a JJ |
4450 | cur_module = NULL; |
4451 | ||
4452 | gfc_trans_use_stmts (ns); | |
bd11e37d | 4453 | gfc_traverse_ns (ns, gfc_emit_parameter_debug_info); |
9268ba9a JJ |
4454 | } |
4455 | ||
4456 | ||
6de9cd9a DN |
4457 | static void |
4458 | gfc_generate_contained_functions (gfc_namespace * parent) | |
4459 | { | |
4460 | gfc_namespace *ns; | |
4461 | ||
4462 | /* We create all the prototypes before generating any code. */ | |
4463 | for (ns = parent->contained; ns; ns = ns->sibling) | |
4464 | { | |
4465 | /* Skip namespaces from used modules. */ | |
4466 | if (ns->parent != parent) | |
4467 | continue; | |
4468 | ||
fb55ca75 | 4469 | gfc_create_function_decl (ns, false); |
6de9cd9a DN |
4470 | } |
4471 | ||
4472 | for (ns = parent->contained; ns; ns = ns->sibling) | |
4473 | { | |
4474 | /* Skip namespaces from used modules. */ | |
4475 | if (ns->parent != parent) | |
4476 | continue; | |
4477 | ||
4478 | gfc_generate_function_code (ns); | |
4479 | } | |
4480 | } | |
4481 | ||
4482 | ||
3e978d30 PT |
4483 | /* Drill down through expressions for the array specification bounds and |
4484 | character length calling generate_local_decl for all those variables | |
4485 | that have not already been declared. */ | |
4486 | ||
4487 | static void | |
4488 | generate_local_decl (gfc_symbol *); | |
4489 | ||
908a2235 | 4490 | /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */ |
3e978d30 | 4491 | |
908a2235 PT |
4492 | static bool |
4493 | expr_decls (gfc_expr *e, gfc_symbol *sym, | |
4494 | int *f ATTRIBUTE_UNUSED) | |
4495 | { | |
4496 | if (e->expr_type != EXPR_VARIABLE | |
4497 | || sym == e->symtree->n.sym | |
3e978d30 PT |
4498 | || e->symtree->n.sym->mark |
4499 | || e->symtree->n.sym->ns != sym->ns) | |
908a2235 | 4500 | return false; |
3e978d30 | 4501 | |
908a2235 PT |
4502 | generate_local_decl (e->symtree->n.sym); |
4503 | return false; | |
4504 | } | |
3e978d30 | 4505 | |
908a2235 PT |
4506 | static void |
4507 | generate_expr_decls (gfc_symbol *sym, gfc_expr *e) | |
4508 | { | |
4509 | gfc_traverse_expr (e, sym, expr_decls, 0); | |
3e978d30 PT |
4510 | } |
4511 | ||
4512 | ||
66e4ab31 | 4513 | /* Check for dependencies in the character length and array spec. */ |
3e978d30 PT |
4514 | |
4515 | static void | |
4516 | generate_dependency_declarations (gfc_symbol *sym) | |
4517 | { | |
4518 | int i; | |
4519 | ||
4520 | if (sym->ts.type == BT_CHARACTER | |
bc21d315 JW |
4521 | && sym->ts.u.cl |
4522 | && sym->ts.u.cl->length | |
4523 | && sym->ts.u.cl->length->expr_type != EXPR_CONSTANT) | |
4524 | generate_expr_decls (sym, sym->ts.u.cl->length); | |
3e978d30 PT |
4525 | |
4526 | if (sym->as && sym->as->rank) | |
4527 | { | |
4528 | for (i = 0; i < sym->as->rank; i++) | |
4529 | { | |
4530 | generate_expr_decls (sym, sym->as->lower[i]); | |
4531 | generate_expr_decls (sym, sym->as->upper[i]); | |
4532 | } | |
4533 | } | |
4534 | } | |
4535 | ||
4536 | ||
6de9cd9a DN |
4537 | /* Generate decls for all local variables. We do this to ensure correct |
4538 | handling of expressions which only appear in the specification of | |
4539 | other functions. */ | |
4540 | ||
4541 | static void | |
4542 | generate_local_decl (gfc_symbol * sym) | |
4543 | { | |
4544 | if (sym->attr.flavor == FL_VARIABLE) | |
4545 | { | |
b8ff4e88 TB |
4546 | if (sym->attr.codimension && !sym->attr.dummy && !sym->attr.allocatable |
4547 | && sym->attr.referenced && !sym->attr.use_assoc) | |
4548 | has_coarray_vars = true; | |
4549 | ||
3e978d30 | 4550 | if (!sym->attr.dummy && !sym->ns->proc_name->attr.entry_master) |
2c69d527 | 4551 | generate_dependency_declarations (sym); |
3e978d30 | 4552 | |
6de9cd9a | 4553 | if (sym->attr.referenced) |
2c69d527 | 4554 | gfc_get_symbol_decl (sym); |
4ed44ccc DF |
4555 | |
4556 | /* Warnings for unused dummy arguments. */ | |
4557 | else if (sym->attr.dummy) | |
ad6d42e1 | 4558 | { |
4ed44ccc DF |
4559 | /* INTENT(out) dummy arguments are likely meant to be set. */ |
4560 | if (gfc_option.warn_unused_dummy_argument | |
4561 | && sym->attr.intent == INTENT_OUT) | |
4562 | { | |
4563 | if (sym->ts.type != BT_DERIVED) | |
4564 | gfc_warning ("Dummy argument '%s' at %L was declared " | |
4565 | "INTENT(OUT) but was not set", sym->name, | |
4566 | &sym->declared_at); | |
4567 | else if (!gfc_has_default_initializer (sym->ts.u.derived)) | |
4568 | gfc_warning ("Derived-type dummy argument '%s' at %L was " | |
4569 | "declared INTENT(OUT) but was not set and " | |
4570 | "does not have a default initializer", | |
4571 | sym->name, &sym->declared_at); | |
fba5ace0 TB |
4572 | if (sym->backend_decl != NULL_TREE) |
4573 | TREE_NO_WARNING(sym->backend_decl) = 1; | |
4ed44ccc DF |
4574 | } |
4575 | else if (gfc_option.warn_unused_dummy_argument) | |
fba5ace0 TB |
4576 | { |
4577 | gfc_warning ("Unused dummy argument '%s' at %L", sym->name, | |
4ed44ccc | 4578 | &sym->declared_at); |
fba5ace0 TB |
4579 | if (sym->backend_decl != NULL_TREE) |
4580 | TREE_NO_WARNING(sym->backend_decl) = 1; | |
4581 | } | |
ad6d42e1 | 4582 | } |
4ed44ccc | 4583 | |
f8d0aee5 | 4584 | /* Warn for unused variables, but not if they're inside a common |
f585b0d3 | 4585 | block, a namelist, or are use-associated. */ |
ce738b86 | 4586 | else if (warn_unused_variable |
f585b0d3 JD |
4587 | && !(sym->attr.in_common || sym->attr.use_assoc || sym->mark |
4588 | || sym->attr.in_namelist)) | |
fba5ace0 TB |
4589 | { |
4590 | gfc_warning ("Unused variable '%s' declared at %L", sym->name, | |
4591 | &sym->declared_at); | |
4592 | if (sym->backend_decl != NULL_TREE) | |
4593 | TREE_NO_WARNING(sym->backend_decl) = 1; | |
4594 | } | |
dbad8e71 | 4595 | else if (warn_unused_variable && sym->attr.use_only) |
fba5ace0 TB |
4596 | { |
4597 | gfc_warning ("Unused module variable '%s' which has been explicitly " | |
4598 | "imported at %L", sym->name, &sym->declared_at); | |
4599 | if (sym->backend_decl != NULL_TREE) | |
4600 | TREE_NO_WARNING(sym->backend_decl) = 1; | |
4601 | } | |
2c69d527 | 4602 | |
417ab240 JJ |
4603 | /* For variable length CHARACTER parameters, the PARM_DECL already |
4604 | references the length variable, so force gfc_get_symbol_decl | |
4605 | even when not referenced. If optimize > 0, it will be optimized | |
4606 | away anyway. But do this only after emitting -Wunused-parameter | |
4607 | warning if requested. */ | |
2c69d527 PT |
4608 | if (sym->attr.dummy && !sym->attr.referenced |
4609 | && sym->ts.type == BT_CHARACTER | |
bc21d315 JW |
4610 | && sym->ts.u.cl->backend_decl != NULL |
4611 | && TREE_CODE (sym->ts.u.cl->backend_decl) == VAR_DECL) | |
417ab240 JJ |
4612 | { |
4613 | sym->attr.referenced = 1; | |
4614 | gfc_get_symbol_decl (sym); | |
4615 | } | |
534fd534 | 4616 | |
5af2eace PT |
4617 | /* INTENT(out) dummy arguments and result variables with allocatable |
4618 | components are reset by default and need to be set referenced to | |
4619 | generate the code for nullification and automatic lengths. */ | |
4620 | if (!sym->attr.referenced | |
2c69d527 | 4621 | && sym->ts.type == BT_DERIVED |
bc21d315 | 4622 | && sym->ts.u.derived->attr.alloc_comp |
758e12af | 4623 | && !sym->attr.pointer |
5af2eace PT |
4624 | && ((sym->attr.dummy && sym->attr.intent == INTENT_OUT) |
4625 | || | |
4626 | (sym->attr.result && sym != sym->result))) | |
2c69d527 PT |
4627 | { |
4628 | sym->attr.referenced = 1; | |
4629 | gfc_get_symbol_decl (sym); | |
4630 | } | |
4631 | ||
06c7153f TB |
4632 | /* Check for dependencies in the array specification and string |
4633 | length, adding the necessary declarations to the function. We | |
4634 | mark the symbol now, as well as in traverse_ns, to prevent | |
4635 | getting stuck in a circular dependency. */ | |
4636 | sym->mark = 1; | |
6de9cd9a | 4637 | } |
33c0c5e9 DF |
4638 | else if (sym->attr.flavor == FL_PARAMETER) |
4639 | { | |
d92693b4 | 4640 | if (warn_unused_parameter |
dbad8e71 TB |
4641 | && !sym->attr.referenced) |
4642 | { | |
4643 | if (!sym->attr.use_assoc) | |
4644 | gfc_warning ("Unused parameter '%s' declared at %L", sym->name, | |
4645 | &sym->declared_at); | |
4646 | else if (sym->attr.use_only) | |
4647 | gfc_warning ("Unused parameter '%s' which has been explicitly " | |
4648 | "imported at %L", sym->name, &sym->declared_at); | |
4649 | } | |
33c0c5e9 | 4650 | } |
766d0c8c DF |
4651 | else if (sym->attr.flavor == FL_PROCEDURE) |
4652 | { | |
4653 | /* TODO: move to the appropriate place in resolve.c. */ | |
4654 | if (warn_return_type | |
4655 | && sym->attr.function | |
4656 | && sym->result | |
4657 | && sym != sym->result | |
4658 | && !sym->result->attr.referenced | |
4659 | && !sym->attr.use_assoc | |
4660 | && sym->attr.if_source != IFSRC_IFBODY) | |
4661 | { | |
4662 | gfc_warning ("Return value '%s' of function '%s' declared at " | |
4663 | "%L not set", sym->result->name, sym->name, | |
4664 | &sym->result->declared_at); | |
4665 | ||
4666 | /* Prevents "Unused variable" warning for RESULT variables. */ | |
06c7153f | 4667 | sym->result->mark = 1; |
766d0c8c DF |
4668 | } |
4669 | } | |
a8b3b0b6 | 4670 | |
8b16d231 CR |
4671 | if (sym->attr.dummy == 1) |
4672 | { | |
4673 | /* Modify the tree type for scalar character dummy arguments of bind(c) | |
4674 | procedures if they are passed by value. The tree type for them will | |
4675 | be promoted to INTEGER_TYPE for the middle end, which appears to be | |
4676 | what C would do with characters passed by-value. The value attribute | |
4677 | implies the dummy is a scalar. */ | |
4678 | if (sym->attr.value == 1 && sym->backend_decl != NULL | |
4679 | && sym->ts.type == BT_CHARACTER && sym->ts.is_c_interop | |
4680 | && sym->ns->proc_name != NULL && sym->ns->proc_name->attr.is_bind_c) | |
e032c2a1 | 4681 | gfc_conv_scalar_char_value (sym, NULL, NULL); |
d20597cb TK |
4682 | |
4683 | /* Unused procedure passed as dummy argument. */ | |
4684 | if (sym->attr.flavor == FL_PROCEDURE) | |
4685 | { | |
4686 | if (!sym->attr.referenced) | |
4687 | { | |
4688 | if (gfc_option.warn_unused_dummy_argument) | |
4689 | gfc_warning ("Unused dummy argument '%s' at %L", sym->name, | |
4690 | &sym->declared_at); | |
4691 | } | |
4692 | ||
4693 | /* Silence bogus "unused parameter" warnings from the | |
4694 | middle end. */ | |
4695 | if (sym->backend_decl != NULL_TREE) | |
4696 | TREE_NO_WARNING (sym->backend_decl) = 1; | |
4697 | } | |
8b16d231 CR |
4698 | } |
4699 | ||
a8b3b0b6 CR |
4700 | /* Make sure we convert the types of the derived types from iso_c_binding |
4701 | into (void *). */ | |
4702 | if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c | |
4703 | && sym->ts.type == BT_DERIVED) | |
4704 | sym->backend_decl = gfc_typenode_for_spec (&(sym->ts)); | |
6de9cd9a DN |
4705 | } |
4706 | ||
4707 | static void | |
4708 | generate_local_vars (gfc_namespace * ns) | |
4709 | { | |
4710 | gfc_traverse_ns (ns, generate_local_decl); | |
4711 | } | |
4712 | ||
4713 | ||
3d79abbd PB |
4714 | /* Generate a switch statement to jump to the correct entry point. Also |
4715 | creates the label decls for the entry points. */ | |
6de9cd9a | 4716 | |
3d79abbd PB |
4717 | static tree |
4718 | gfc_trans_entry_master_switch (gfc_entry_list * el) | |
6de9cd9a | 4719 | { |
3d79abbd PB |
4720 | stmtblock_t block; |
4721 | tree label; | |
4722 | tree tmp; | |
4723 | tree val; | |
6de9cd9a | 4724 | |
3d79abbd PB |
4725 | gfc_init_block (&block); |
4726 | for (; el; el = el->next) | |
4727 | { | |
4728 | /* Add the case label. */ | |
c006df4e | 4729 | label = gfc_build_label_decl (NULL_TREE); |
7d60be94 | 4730 | val = build_int_cst (gfc_array_index_type, el->id); |
3d528853 | 4731 | tmp = build_case_label (val, NULL_TREE, label); |
3d79abbd | 4732 | gfc_add_expr_to_block (&block, tmp); |
7389bce6 | 4733 | |
3d79abbd PB |
4734 | /* And jump to the actual entry point. */ |
4735 | label = gfc_build_label_decl (NULL_TREE); | |
3d79abbd PB |
4736 | tmp = build1_v (GOTO_EXPR, label); |
4737 | gfc_add_expr_to_block (&block, tmp); | |
4738 | ||
4739 | /* Save the label decl. */ | |
4740 | el->label = label; | |
4741 | } | |
4742 | tmp = gfc_finish_block (&block); | |
4743 | /* The first argument selects the entry point. */ | |
4744 | val = DECL_ARGUMENTS (current_function_decl); | |
0cd2402d SB |
4745 | tmp = fold_build3_loc (input_location, SWITCH_EXPR, NULL_TREE, |
4746 | val, tmp, NULL_TREE); | |
3d79abbd | 4747 | return tmp; |
6de9cd9a DN |
4748 | } |
4749 | ||
44de5aeb | 4750 | |
cadb8f42 DK |
4751 | /* Add code to string lengths of actual arguments passed to a function against |
4752 | the expected lengths of the dummy arguments. */ | |
4753 | ||
4754 | static void | |
4755 | add_argument_checking (stmtblock_t *block, gfc_symbol *sym) | |
4756 | { | |
4757 | gfc_formal_arglist *formal; | |
4758 | ||
4759 | for (formal = sym->formal; formal; formal = formal->next) | |
be94c034 | 4760 | if (formal->sym && formal->sym->ts.type == BT_CHARACTER |
9be1227b | 4761 | && !formal->sym->ts.deferred) |
cadb8f42 DK |
4762 | { |
4763 | enum tree_code comparison; | |
4764 | tree cond; | |
4765 | tree argname; | |
4766 | gfc_symbol *fsym; | |
4767 | gfc_charlen *cl; | |
4768 | const char *message; | |
4769 | ||
4770 | fsym = formal->sym; | |
bc21d315 | 4771 | cl = fsym->ts.u.cl; |
cadb8f42 DK |
4772 | |
4773 | gcc_assert (cl); | |
4774 | gcc_assert (cl->passed_length != NULL_TREE); | |
4775 | gcc_assert (cl->backend_decl != NULL_TREE); | |
4776 | ||
4777 | /* For POINTER, ALLOCATABLE and assumed-shape dummy arguments, the | |
4778 | string lengths must match exactly. Otherwise, it is only required | |
cb7a8961 TB |
4779 | that the actual string length is *at least* the expected one. |
4780 | Sequence association allows for a mismatch of the string length | |
4781 | if the actual argument is (part of) an array, but only if the | |
4782 | dummy argument is an array. (See "Sequence association" in | |
4783 | Section 12.4.1.4 for F95 and 12.4.1.5 for F2003.) */ | |
be94c034 TB |
4784 | if (fsym->attr.pointer || fsym->attr.allocatable |
4785 | || (fsym->as && fsym->as->type == AS_ASSUMED_SHAPE)) | |
cadb8f42 DK |
4786 | { |
4787 | comparison = NE_EXPR; | |
4788 | message = _("Actual string length does not match the declared one" | |
4789 | " for dummy argument '%s' (%ld/%ld)"); | |
4790 | } | |
cb7a8961 TB |
4791 | else if (fsym->as && fsym->as->rank != 0) |
4792 | continue; | |
cadb8f42 DK |
4793 | else |
4794 | { | |
4795 | comparison = LT_EXPR; | |
4796 | message = _("Actual string length is shorter than the declared one" | |
4797 | " for dummy argument '%s' (%ld/%ld)"); | |
4798 | } | |
4799 | ||
4800 | /* Build the condition. For optional arguments, an actual length | |
4801 | of 0 is also acceptable if the associated string is NULL, which | |
4802 | means the argument was not passed. */ | |
bc98ed60 TB |
4803 | cond = fold_build2_loc (input_location, comparison, boolean_type_node, |
4804 | cl->passed_length, cl->backend_decl); | |
cadb8f42 DK |
4805 | if (fsym->attr.optional) |
4806 | { | |
4807 | tree not_absent; | |
4808 | tree not_0length; | |
4809 | tree absent_failed; | |
4810 | ||
bc98ed60 TB |
4811 | not_0length = fold_build2_loc (input_location, NE_EXPR, |
4812 | boolean_type_node, | |
4813 | cl->passed_length, | |
e8160c9a | 4814 | build_zero_cst (gfc_charlen_type_node)); |
702a738b PT |
4815 | /* The symbol needs to be referenced for gfc_get_symbol_decl. */ |
4816 | fsym->attr.referenced = 1; | |
4817 | not_absent = gfc_conv_expr_present (fsym); | |
cadb8f42 | 4818 | |
bc98ed60 TB |
4819 | absent_failed = fold_build2_loc (input_location, TRUTH_OR_EXPR, |
4820 | boolean_type_node, not_0length, | |
4821 | not_absent); | |
cadb8f42 | 4822 | |
bc98ed60 TB |
4823 | cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, |
4824 | boolean_type_node, cond, absent_failed); | |
cadb8f42 DK |
4825 | } |
4826 | ||
4827 | /* Build the runtime check. */ | |
4828 | argname = gfc_build_cstring_const (fsym->name); | |
4829 | argname = gfc_build_addr_expr (pchar_type_node, argname); | |
4830 | gfc_trans_runtime_check (true, false, cond, block, &fsym->declared_at, | |
4831 | message, argname, | |
4832 | fold_convert (long_integer_type_node, | |
4833 | cl->passed_length), | |
4834 | fold_convert (long_integer_type_node, | |
4835 | cl->backend_decl)); | |
4836 | } | |
4837 | } | |
4838 | ||
4839 | ||
6f4d39bb TB |
4840 | /* Generate the _gfortran_caf_this_image and _gfortran_caf_num_images |
4841 | global variables for -fcoarray=lib. They are placed into the translation | |
4842 | unit of the main program. Make sure that in one TU (the one of the main | |
4843 | program), the first call to gfc_init_coarray_decl is done with true. | |
4844 | Otherwise, expect link errors. */ | |
4845 | ||
60386f50 | 4846 | void |
6f4d39bb | 4847 | gfc_init_coarray_decl (bool main_tu) |
60386f50 | 4848 | { |
6f4d39bb | 4849 | tree save_fn_decl; |
60386f50 TB |
4850 | |
4851 | if (gfc_option.coarray != GFC_FCOARRAY_LIB) | |
4852 | return; | |
4853 | ||
4854 | if (gfort_gvar_caf_this_image || gfort_gvar_caf_num_images) | |
4855 | return; | |
4856 | ||
4857 | save_fn_decl = current_function_decl; | |
4858 | current_function_decl = NULL_TREE; | |
4859 | push_cfun (cfun); | |
4860 | ||
6f4d39bb TB |
4861 | gfort_gvar_caf_this_image |
4862 | = build_decl (input_location, VAR_DECL, | |
4863 | get_identifier (PREFIX("caf_this_image")), | |
4864 | integer_type_node); | |
60386f50 TB |
4865 | DECL_ARTIFICIAL (gfort_gvar_caf_this_image) = 1; |
4866 | TREE_USED (gfort_gvar_caf_this_image) = 1; | |
4867 | TREE_PUBLIC (gfort_gvar_caf_this_image) = 1; | |
6f4d39bb TB |
4868 | TREE_READONLY (gfort_gvar_caf_this_image) = 0; |
4869 | ||
4870 | if (main_tu) | |
4871 | TREE_STATIC (gfort_gvar_caf_this_image) = 1; | |
4872 | else | |
4873 | DECL_EXTERNAL (gfort_gvar_caf_this_image) = 1; | |
4874 | ||
4875 | pushdecl_top_level (gfort_gvar_caf_this_image); | |
60386f50 | 4876 | |
6f4d39bb TB |
4877 | gfort_gvar_caf_num_images |
4878 | = build_decl (input_location, VAR_DECL, | |
4879 | get_identifier (PREFIX("caf_num_images")), | |
4880 | integer_type_node); | |
60386f50 TB |
4881 | DECL_ARTIFICIAL (gfort_gvar_caf_num_images) = 1; |
4882 | TREE_USED (gfort_gvar_caf_num_images) = 1; | |
4883 | TREE_PUBLIC (gfort_gvar_caf_num_images) = 1; | |
6f4d39bb TB |
4884 | TREE_READONLY (gfort_gvar_caf_num_images) = 0; |
4885 | ||
4886 | if (main_tu) | |
4887 | TREE_STATIC (gfort_gvar_caf_num_images) = 1; | |
4888 | else | |
4889 | DECL_EXTERNAL (gfort_gvar_caf_num_images) = 1; | |
4890 | ||
4891 | pushdecl_top_level (gfort_gvar_caf_num_images); | |
60386f50 TB |
4892 | |
4893 | pop_cfun (); | |
4894 | current_function_decl = save_fn_decl; | |
4895 | } | |
4896 | ||
4897 | ||
092231a8 TB |
4898 | static void |
4899 | create_main_function (tree fndecl) | |
4900 | { | |
86c3c481 | 4901 | tree old_context; |
092231a8 TB |
4902 | tree ftn_main; |
4903 | tree tmp, decl, result_decl, argc, argv, typelist, arglist; | |
4904 | stmtblock_t body; | |
4905 | ||
86c3c481 TB |
4906 | old_context = current_function_decl; |
4907 | ||
4908 | if (old_context) | |
4909 | { | |
4910 | push_function_context (); | |
4911 | saved_parent_function_decls = saved_function_decls; | |
4912 | saved_function_decls = NULL_TREE; | |
4913 | } | |
4914 | ||
092231a8 TB |
4915 | /* main() function must be declared with global scope. */ |
4916 | gcc_assert (current_function_decl == NULL_TREE); | |
4917 | ||
4918 | /* Declare the function. */ | |
4919 | tmp = build_function_type_list (integer_type_node, integer_type_node, | |
4920 | build_pointer_type (pchar_type_node), | |
4921 | NULL_TREE); | |
a7ad6c2d | 4922 | main_identifier_node = get_identifier ("main"); |
c2255bc4 AH |
4923 | ftn_main = build_decl (input_location, FUNCTION_DECL, |
4924 | main_identifier_node, tmp); | |
092231a8 TB |
4925 | DECL_EXTERNAL (ftn_main) = 0; |
4926 | TREE_PUBLIC (ftn_main) = 1; | |
4927 | TREE_STATIC (ftn_main) = 1; | |
4928 | DECL_ATTRIBUTES (ftn_main) | |
4929 | = tree_cons (get_identifier("externally_visible"), NULL_TREE, NULL_TREE); | |
4930 | ||
4931 | /* Setup the result declaration (for "return 0"). */ | |
c2255bc4 AH |
4932 | result_decl = build_decl (input_location, |
4933 | RESULT_DECL, NULL_TREE, integer_type_node); | |
092231a8 TB |
4934 | DECL_ARTIFICIAL (result_decl) = 1; |
4935 | DECL_IGNORED_P (result_decl) = 1; | |
4936 | DECL_CONTEXT (result_decl) = ftn_main; | |
4937 | DECL_RESULT (ftn_main) = result_decl; | |
4938 | ||
4939 | pushdecl (ftn_main); | |
4940 | ||
4941 | /* Get the arguments. */ | |
4942 | ||
4943 | arglist = NULL_TREE; | |
4944 | typelist = TYPE_ARG_TYPES (TREE_TYPE (ftn_main)); | |
4945 | ||
4946 | tmp = TREE_VALUE (typelist); | |
c2255bc4 | 4947 | argc = build_decl (input_location, PARM_DECL, get_identifier ("argc"), tmp); |
092231a8 TB |
4948 | DECL_CONTEXT (argc) = ftn_main; |
4949 | DECL_ARG_TYPE (argc) = TREE_VALUE (typelist); | |
4950 | TREE_READONLY (argc) = 1; | |
4951 | gfc_finish_decl (argc); | |
4952 | arglist = chainon (arglist, argc); | |
4953 | ||
4954 | typelist = TREE_CHAIN (typelist); | |
4955 | tmp = TREE_VALUE (typelist); | |
c2255bc4 | 4956 | argv = build_decl (input_location, PARM_DECL, get_identifier ("argv"), tmp); |
092231a8 TB |
4957 | DECL_CONTEXT (argv) = ftn_main; |
4958 | DECL_ARG_TYPE (argv) = TREE_VALUE (typelist); | |
4959 | TREE_READONLY (argv) = 1; | |
4960 | DECL_BY_REFERENCE (argv) = 1; | |
4961 | gfc_finish_decl (argv); | |
4962 | arglist = chainon (arglist, argv); | |
4963 | ||
4964 | DECL_ARGUMENTS (ftn_main) = arglist; | |
4965 | current_function_decl = ftn_main; | |
4966 | announce_function (ftn_main); | |
4967 | ||
4968 | rest_of_decl_compilation (ftn_main, 1, 0); | |
4969 | make_decl_rtl (ftn_main); | |
4970 | init_function_start (ftn_main); | |
87a60f68 | 4971 | pushlevel (); |
092231a8 TB |
4972 | |
4973 | gfc_init_block (&body); | |
4974 | ||
4975 | /* Call some libgfortran initialization routines, call then MAIN__(). */ | |
4976 | ||
60386f50 TB |
4977 | /* Call _gfortran_caf_init (*argc, ***argv, *this_image, *num_images). */ |
4978 | if (gfc_option.coarray == GFC_FCOARRAY_LIB) | |
4979 | { | |
4980 | tree pint_type, pppchar_type; | |
4981 | pint_type = build_pointer_type (integer_type_node); | |
4982 | pppchar_type | |
4983 | = build_pointer_type (build_pointer_type (pchar_type_node)); | |
4984 | ||
6f4d39bb | 4985 | gfc_init_coarray_decl (true); |
60386f50 TB |
4986 | tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_init, 4, |
4987 | gfc_build_addr_expr (pint_type, argc), | |
4988 | gfc_build_addr_expr (pppchar_type, argv), | |
4989 | gfc_build_addr_expr (pint_type, gfort_gvar_caf_this_image), | |
4990 | gfc_build_addr_expr (pint_type, gfort_gvar_caf_num_images)); | |
4991 | gfc_add_expr_to_block (&body, tmp); | |
4992 | } | |
4993 | ||
092231a8 | 4994 | /* Call _gfortran_set_args (argc, argv). */ |
86c3c481 TB |
4995 | TREE_USED (argc) = 1; |
4996 | TREE_USED (argv) = 1; | |
db3927fb AH |
4997 | tmp = build_call_expr_loc (input_location, |
4998 | gfor_fndecl_set_args, 2, argc, argv); | |
092231a8 TB |
4999 | gfc_add_expr_to_block (&body, tmp); |
5000 | ||
5001 | /* Add a call to set_options to set up the runtime library Fortran | |
5002 | language standard parameters. */ | |
5003 | { | |
5004 | tree array_type, array, var; | |
8748ad99 | 5005 | VEC(constructor_elt,gc) *v = NULL; |
092231a8 TB |
5006 | |
5007 | /* Passing a new option to the library requires four modifications: | |
5008 | + add it to the tree_cons list below | |
5009 | + change the array size in the call to build_array_type | |
5010 | + change the first argument to the library call | |
5011 | gfor_fndecl_set_options | |
5012 | + modify the library (runtime/compile_options.c)! */ | |
5013 | ||
8748ad99 NF |
5014 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, |
5015 | build_int_cst (integer_type_node, | |
5016 | gfc_option.warn_std)); | |
5017 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, | |
5018 | build_int_cst (integer_type_node, | |
5019 | gfc_option.allow_std)); | |
5020 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, | |
5021 | build_int_cst (integer_type_node, pedantic)); | |
de8bd142 JB |
5022 | /* TODO: This is the old -fdump-core option, which is unused but |
5023 | passed due to ABI compatibility; remove when bumping the | |
5024 | library ABI. */ | |
8748ad99 NF |
5025 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, |
5026 | build_int_cst (integer_type_node, | |
de8bd142 | 5027 | 0)); |
8748ad99 NF |
5028 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, |
5029 | build_int_cst (integer_type_node, | |
5030 | gfc_option.flag_backtrace)); | |
5031 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, | |
5032 | build_int_cst (integer_type_node, | |
5033 | gfc_option.flag_sign_zero)); | |
5034 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, | |
5035 | build_int_cst (integer_type_node, | |
5036 | (gfc_option.rtcheck | |
5037 | & GFC_RTCHECK_BOUNDS))); | |
80b91c0b JB |
5038 | /* TODO: This is the -frange-check option, which no longer affects |
5039 | library behavior; when bumping the library ABI this slot can be | |
5040 | reused for something else. As it is the last element in the | |
5041 | array, we can instead leave it out altogether. | |
8748ad99 NF |
5042 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, |
5043 | build_int_cst (integer_type_node, | |
5044 | gfc_option.flag_range_check)); | |
80b91c0b | 5045 | */ |
092231a8 TB |
5046 | |
5047 | array_type = build_array_type (integer_type_node, | |
80b91c0b | 5048 | build_index_type (size_int (6))); |
8748ad99 | 5049 | array = build_constructor (array_type, v); |
092231a8 TB |
5050 | TREE_CONSTANT (array) = 1; |
5051 | TREE_STATIC (array) = 1; | |
5052 | ||
5053 | /* Create a static variable to hold the jump table. */ | |
5054 | var = gfc_create_var (array_type, "options"); | |
5055 | TREE_CONSTANT (var) = 1; | |
5056 | TREE_STATIC (var) = 1; | |
5057 | TREE_READONLY (var) = 1; | |
5058 | DECL_INITIAL (var) = array; | |
5059 | var = gfc_build_addr_expr (build_pointer_type (integer_type_node), var); | |
5060 | ||
db3927fb AH |
5061 | tmp = build_call_expr_loc (input_location, |
5062 | gfor_fndecl_set_options, 2, | |
80b91c0b | 5063 | build_int_cst (integer_type_node, 7), var); |
092231a8 TB |
5064 | gfc_add_expr_to_block (&body, tmp); |
5065 | } | |
5066 | ||
5067 | /* If -ffpe-trap option was provided, add a call to set_fpe so that | |
5068 | the library will raise a FPE when needed. */ | |
5069 | if (gfc_option.fpe != 0) | |
5070 | { | |
db3927fb AH |
5071 | tmp = build_call_expr_loc (input_location, |
5072 | gfor_fndecl_set_fpe, 1, | |
092231a8 TB |
5073 | build_int_cst (integer_type_node, |
5074 | gfc_option.fpe)); | |
5075 | gfc_add_expr_to_block (&body, tmp); | |
5076 | } | |
5077 | ||
5078 | /* If this is the main program and an -fconvert option was provided, | |
5079 | add a call to set_convert. */ | |
5080 | ||
5081 | if (gfc_option.convert != GFC_CONVERT_NATIVE) | |
5082 | { | |
db3927fb AH |
5083 | tmp = build_call_expr_loc (input_location, |
5084 | gfor_fndecl_set_convert, 1, | |
092231a8 TB |
5085 | build_int_cst (integer_type_node, |
5086 | gfc_option.convert)); | |
5087 | gfc_add_expr_to_block (&body, tmp); | |
5088 | } | |
5089 | ||
5090 | /* If this is the main program and an -frecord-marker option was provided, | |
5091 | add a call to set_record_marker. */ | |
5092 | ||
5093 | if (gfc_option.record_marker != 0) | |
5094 | { | |
db3927fb AH |
5095 | tmp = build_call_expr_loc (input_location, |
5096 | gfor_fndecl_set_record_marker, 1, | |
092231a8 TB |
5097 | build_int_cst (integer_type_node, |
5098 | gfc_option.record_marker)); | |
5099 | gfc_add_expr_to_block (&body, tmp); | |
5100 | } | |
5101 | ||
5102 | if (gfc_option.max_subrecord_length != 0) | |
5103 | { | |
db3927fb AH |
5104 | tmp = build_call_expr_loc (input_location, |
5105 | gfor_fndecl_set_max_subrecord_length, 1, | |
092231a8 TB |
5106 | build_int_cst (integer_type_node, |
5107 | gfc_option.max_subrecord_length)); | |
5108 | gfc_add_expr_to_block (&body, tmp); | |
5109 | } | |
5110 | ||
5111 | /* Call MAIN__(). */ | |
db3927fb AH |
5112 | tmp = build_call_expr_loc (input_location, |
5113 | fndecl, 0); | |
092231a8 TB |
5114 | gfc_add_expr_to_block (&body, tmp); |
5115 | ||
86c3c481 TB |
5116 | /* Mark MAIN__ as used. */ |
5117 | TREE_USED (fndecl) = 1; | |
5118 | ||
60386f50 TB |
5119 | /* Coarray: Call _gfortran_caf_finalize(void). */ |
5120 | if (gfc_option.coarray == GFC_FCOARRAY_LIB) | |
5121 | { | |
5122 | /* Per F2008, 8.5.1 END of the main program implies a | |
5123 | SYNC MEMORY. */ | |
e79983f4 | 5124 | tmp = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE); |
60386f50 TB |
5125 | tmp = build_call_expr_loc (input_location, tmp, 0); |
5126 | gfc_add_expr_to_block (&body, tmp); | |
5127 | ||
5128 | tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_finalize, 0); | |
5129 | gfc_add_expr_to_block (&body, tmp); | |
5130 | } | |
5131 | ||
092231a8 | 5132 | /* "return 0". */ |
bc98ed60 TB |
5133 | tmp = fold_build2_loc (input_location, MODIFY_EXPR, integer_type_node, |
5134 | DECL_RESULT (ftn_main), | |
5135 | build_int_cst (integer_type_node, 0)); | |
092231a8 TB |
5136 | tmp = build1_v (RETURN_EXPR, tmp); |
5137 | gfc_add_expr_to_block (&body, tmp); | |
5138 | ||
5139 | ||
5140 | DECL_SAVED_TREE (ftn_main) = gfc_finish_block (&body); | |
5141 | decl = getdecls (); | |
5142 | ||
5143 | /* Finish off this function and send it for code generation. */ | |
87a60f68 | 5144 | poplevel (1, 1); |
092231a8 TB |
5145 | BLOCK_SUPERCONTEXT (DECL_INITIAL (ftn_main)) = ftn_main; |
5146 | ||
5147 | DECL_SAVED_TREE (ftn_main) | |
5148 | = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (ftn_main), | |
5149 | DECL_INITIAL (ftn_main)); | |
5150 | ||
5151 | /* Output the GENERIC tree. */ | |
5152 | dump_function (TDI_original, ftn_main); | |
5153 | ||
9618fb3c | 5154 | cgraph_finalize_function (ftn_main, true); |
86c3c481 TB |
5155 | |
5156 | if (old_context) | |
5157 | { | |
5158 | pop_function_context (); | |
5159 | saved_function_decls = saved_parent_function_decls; | |
5160 | } | |
5161 | current_function_decl = old_context; | |
092231a8 TB |
5162 | } |
5163 | ||
5164 | ||
d74d8807 DK |
5165 | /* Get the result expression for a procedure. */ |
5166 | ||
5167 | static tree | |
5168 | get_proc_result (gfc_symbol* sym) | |
5169 | { | |
5170 | if (sym->attr.subroutine || sym == sym->result) | |
5171 | { | |
5172 | if (current_fake_result_decl != NULL) | |
5173 | return TREE_VALUE (current_fake_result_decl); | |
5174 | ||
5175 | return NULL_TREE; | |
5176 | } | |
5177 | ||
5178 | return sym->result->backend_decl; | |
5179 | } | |
5180 | ||
5181 | ||
5182 | /* Generate an appropriate return-statement for a procedure. */ | |
5183 | ||
5184 | tree | |
5185 | gfc_generate_return (void) | |
5186 | { | |
5187 | gfc_symbol* sym; | |
5188 | tree result; | |
5189 | tree fndecl; | |
5190 | ||
5191 | sym = current_procedure_symbol; | |
5192 | fndecl = sym->backend_decl; | |
5193 | ||
5194 | if (TREE_TYPE (DECL_RESULT (fndecl)) == void_type_node) | |
5195 | result = NULL_TREE; | |
5196 | else | |
5197 | { | |
5198 | result = get_proc_result (sym); | |
5199 | ||
5200 | /* Set the return value to the dummy result variable. The | |
5201 | types may be different for scalar default REAL functions | |
5202 | with -ff2c, therefore we have to convert. */ | |
5203 | if (result != NULL_TREE) | |
5204 | { | |
5205 | result = convert (TREE_TYPE (DECL_RESULT (fndecl)), result); | |
bc98ed60 TB |
5206 | result = fold_build2_loc (input_location, MODIFY_EXPR, |
5207 | TREE_TYPE (result), DECL_RESULT (fndecl), | |
5208 | result); | |
d74d8807 DK |
5209 | } |
5210 | } | |
5211 | ||
5212 | return build1_v (RETURN_EXPR, result); | |
5213 | } | |
5214 | ||
5215 | ||
6de9cd9a DN |
5216 | /* Generate code for a function. */ |
5217 | ||
5218 | void | |
5219 | gfc_generate_function_code (gfc_namespace * ns) | |
5220 | { | |
5221 | tree fndecl; | |
5222 | tree old_context; | |
5223 | tree decl; | |
5224 | tree tmp; | |
d74d8807 | 5225 | stmtblock_t init, cleanup; |
6de9cd9a | 5226 | stmtblock_t body; |
d74d8807 | 5227 | gfc_wrapped_block try_block; |
702a738b | 5228 | tree recurcheckvar = NULL_TREE; |
6de9cd9a | 5229 | gfc_symbol *sym; |
d74d8807 | 5230 | gfc_symbol *previous_procedure_symbol; |
5046aff5 | 5231 | int rank; |
cf7d2eb0 | 5232 | bool is_recursive; |
6de9cd9a DN |
5233 | |
5234 | sym = ns->proc_name; | |
d74d8807 DK |
5235 | previous_procedure_symbol = current_procedure_symbol; |
5236 | current_procedure_symbol = sym; | |
3d79abbd | 5237 | |
6de9cd9a | 5238 | /* Check that the frontend isn't still using this. */ |
6e45f57b | 5239 | gcc_assert (sym->tlink == NULL); |
6de9cd9a DN |
5240 | sym->tlink = sym; |
5241 | ||
5242 | /* Create the declaration for functions with global scope. */ | |
5243 | if (!sym->backend_decl) | |
fb55ca75 | 5244 | gfc_create_function_decl (ns, false); |
6de9cd9a DN |
5245 | |
5246 | fndecl = sym->backend_decl; | |
5247 | old_context = current_function_decl; | |
5248 | ||
5249 | if (old_context) | |
5250 | { | |
5251 | push_function_context (); | |
5252 | saved_parent_function_decls = saved_function_decls; | |
5253 | saved_function_decls = NULL_TREE; | |
5254 | } | |
5255 | ||
3d79abbd | 5256 | trans_function_start (sym); |
6de9cd9a | 5257 | |
d74d8807 | 5258 | gfc_init_block (&init); |
6de9cd9a | 5259 | |
d198b59a JJ |
5260 | if (ns->entries && ns->proc_name->ts.type == BT_CHARACTER) |
5261 | { | |
5262 | /* Copy length backend_decls to all entry point result | |
5263 | symbols. */ | |
5264 | gfc_entry_list *el; | |
5265 | tree backend_decl; | |
5266 | ||
bc21d315 JW |
5267 | gfc_conv_const_charlen (ns->proc_name->ts.u.cl); |
5268 | backend_decl = ns->proc_name->result->ts.u.cl->backend_decl; | |
d198b59a | 5269 | for (el = ns->entries; el; el = el->next) |
bc21d315 | 5270 | el->sym->result->ts.u.cl->backend_decl = backend_decl; |
d198b59a JJ |
5271 | } |
5272 | ||
6de9cd9a DN |
5273 | /* Translate COMMON blocks. */ |
5274 | gfc_trans_common (ns); | |
5275 | ||
5f20c93a PT |
5276 | /* Null the parent fake result declaration if this namespace is |
5277 | a module function or an external procedures. */ | |
5278 | if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE) | |
5279 | || ns->parent == NULL) | |
5280 | parent_fake_result_decl = NULL_TREE; | |
5281 | ||
30aabb86 PT |
5282 | gfc_generate_contained_functions (ns); |
5283 | ||
77f2a970 JJ |
5284 | nonlocal_dummy_decls = NULL; |
5285 | nonlocal_dummy_decl_pset = NULL; | |
5286 | ||
b8ff4e88 | 5287 | has_coarray_vars = false; |
6de9cd9a | 5288 | generate_local_vars (ns); |
7389bce6 | 5289 | |
b8ff4e88 TB |
5290 | if (gfc_option.coarray == GFC_FCOARRAY_LIB && has_coarray_vars) |
5291 | generate_coarray_init (ns); | |
5292 | ||
5f20c93a PT |
5293 | /* Keep the parent fake result declaration in module functions |
5294 | or external procedures. */ | |
5295 | if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE) | |
5296 | || ns->parent == NULL) | |
5297 | current_fake_result_decl = parent_fake_result_decl; | |
5298 | else | |
5299 | current_fake_result_decl = NULL_TREE; | |
5300 | ||
d74d8807 DK |
5301 | is_recursive = sym->attr.recursive |
5302 | || (sym->attr.entry_master | |
5303 | && sym->ns->entries->sym->attr.recursive); | |
5304 | if ((gfc_option.rtcheck & GFC_RTCHECK_RECURSION) | |
5305 | && !is_recursive | |
5306 | && !gfc_option.flag_recursive) | |
5307 | { | |
5308 | char * msg; | |
5309 | ||
5310 | asprintf (&msg, "Recursive call to nonrecursive procedure '%s'", | |
5311 | sym->name); | |
5312 | recurcheckvar = gfc_create_var (boolean_type_node, "is_recursive"); | |
5313 | TREE_STATIC (recurcheckvar) = 1; | |
5314 | DECL_INITIAL (recurcheckvar) = boolean_false_node; | |
5315 | gfc_add_expr_to_block (&init, recurcheckvar); | |
5316 | gfc_trans_runtime_check (true, false, recurcheckvar, &init, | |
5317 | &sym->declared_at, msg); | |
5318 | gfc_add_modify (&init, recurcheckvar, boolean_true_node); | |
cede9502 | 5319 | free (msg); |
d74d8807 | 5320 | } |
6de9cd9a DN |
5321 | |
5322 | /* Now generate the code for the body of this function. */ | |
5323 | gfc_init_block (&body); | |
5324 | ||
5325 | if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node | |
d74d8807 | 5326 | && sym->attr.subroutine) |
6de9cd9a DN |
5327 | { |
5328 | tree alternate_return; | |
5f20c93a | 5329 | alternate_return = gfc_get_fake_result_decl (sym, 0); |
726a989a | 5330 | gfc_add_modify (&body, alternate_return, integer_zero_node); |
6de9cd9a DN |
5331 | } |
5332 | ||
3d79abbd PB |
5333 | if (ns->entries) |
5334 | { | |
5335 | /* Jump to the correct entry point. */ | |
5336 | tmp = gfc_trans_entry_master_switch (ns->entries); | |
5337 | gfc_add_expr_to_block (&body, tmp); | |
5338 | } | |
5339 | ||
cadb8f42 DK |
5340 | /* If bounds-checking is enabled, generate code to check passed in actual |
5341 | arguments against the expected dummy argument attributes (e.g. string | |
5342 | lengths). */ | |
975d3303 | 5343 | if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !sym->attr.is_bind_c) |
cadb8f42 DK |
5344 | add_argument_checking (&body, sym); |
5345 | ||
6de9cd9a DN |
5346 | tmp = gfc_trans_code (ns->code); |
5347 | gfc_add_expr_to_block (&body, tmp); | |
5348 | ||
6de9cd9a DN |
5349 | if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node) |
5350 | { | |
d74d8807 | 5351 | tree result = get_proc_result (sym); |
6de9cd9a | 5352 | |
7a3eeb85 | 5353 | if (result != NULL_TREE && sym->attr.function && !sym->attr.pointer) |
5046aff5 | 5354 | { |
f18694de TB |
5355 | if (sym->attr.allocatable && sym->attr.dimension == 0 |
5356 | && sym->result == sym) | |
5357 | gfc_add_modify (&init, result, fold_convert (TREE_TYPE (result), | |
5358 | null_pointer_node)); | |
7a3eeb85 JW |
5359 | else if (sym->ts.type == BT_CLASS |
5360 | && CLASS_DATA (sym)->attr.allocatable | |
102344e2 TB |
5361 | && CLASS_DATA (sym)->attr.dimension == 0 |
5362 | && sym->result == sym) | |
7a3eeb85 JW |
5363 | { |
5364 | tmp = CLASS_DATA (sym)->backend_decl; | |
5365 | tmp = fold_build3_loc (input_location, COMPONENT_REF, | |
5366 | TREE_TYPE (tmp), result, tmp, NULL_TREE); | |
5367 | gfc_add_modify (&init, tmp, fold_convert (TREE_TYPE (tmp), | |
5368 | null_pointer_node)); | |
5369 | } | |
f18694de | 5370 | else if (sym->ts.type == BT_DERIVED |
7a3eeb85 JW |
5371 | && sym->ts.u.derived->attr.alloc_comp |
5372 | && !sym->attr.allocatable) | |
5b130807 TB |
5373 | { |
5374 | rank = sym->as ? sym->as->rank : 0; | |
d74d8807 DK |
5375 | tmp = gfc_nullify_alloc_comp (sym->ts.u.derived, result, rank); |
5376 | gfc_add_expr_to_block (&init, tmp); | |
5b130807 | 5377 | } |
958dd42b | 5378 | } |
cf7d2eb0 | 5379 | |
766d0c8c DF |
5380 | if (result == NULL_TREE) |
5381 | { | |
5382 | /* TODO: move to the appropriate place in resolve.c. */ | |
fba5ace0 | 5383 | if (warn_return_type && sym == sym->result) |
766d0c8c DF |
5384 | gfc_warning ("Return value of function '%s' at %L not set", |
5385 | sym->name, &sym->declared_at); | |
fba5ace0 TB |
5386 | if (warn_return_type) |
5387 | TREE_NO_WARNING(sym->backend_decl) = 1; | |
766d0c8c | 5388 | } |
6de9cd9a | 5389 | else |
d74d8807 | 5390 | gfc_add_expr_to_block (&body, gfc_generate_return ()); |
6de9cd9a | 5391 | } |
d74d8807 DK |
5392 | |
5393 | gfc_init_block (&cleanup); | |
5394 | ||
5395 | /* Reset recursion-check variable. */ | |
5396 | if ((gfc_option.rtcheck & GFC_RTCHECK_RECURSION) | |
5397 | && !is_recursive | |
e3339d0f | 5398 | && !gfc_option.gfc_flag_openmp |
d74d8807 | 5399 | && recurcheckvar != NULL_TREE) |
cf7d2eb0 | 5400 | { |
d74d8807 DK |
5401 | gfc_add_modify (&cleanup, recurcheckvar, boolean_false_node); |
5402 | recurcheckvar = NULL; | |
cf7d2eb0 | 5403 | } |
5046aff5 | 5404 | |
d74d8807 DK |
5405 | /* Finish the function body and add init and cleanup code. */ |
5406 | tmp = gfc_finish_block (&body); | |
5407 | gfc_start_wrapped_block (&try_block, tmp); | |
5408 | /* Add code to create and cleanup arrays. */ | |
5409 | gfc_trans_deferred_vars (sym, &try_block); | |
5410 | gfc_add_init_cleanup (&try_block, gfc_finish_block (&init), | |
5411 | gfc_finish_block (&cleanup)); | |
6de9cd9a DN |
5412 | |
5413 | /* Add all the decls we created during processing. */ | |
5414 | decl = saved_function_decls; | |
5415 | while (decl) | |
5416 | { | |
5417 | tree next; | |
5418 | ||
910ad8de NF |
5419 | next = DECL_CHAIN (decl); |
5420 | DECL_CHAIN (decl) = NULL_TREE; | |
cf651ca2 TB |
5421 | if (GFC_DECL_PUSH_TOPLEVEL (decl)) |
5422 | pushdecl_top_level (decl); | |
5423 | else | |
5424 | pushdecl (decl); | |
6de9cd9a DN |
5425 | decl = next; |
5426 | } | |
5427 | saved_function_decls = NULL_TREE; | |
5428 | ||
d74d8807 | 5429 | DECL_SAVED_TREE (fndecl) = gfc_finish_wrapped_block (&try_block); |
c7c79a09 | 5430 | decl = getdecls (); |
6de9cd9a DN |
5431 | |
5432 | /* Finish off this function and send it for code generation. */ | |
87a60f68 | 5433 | poplevel (1, 1); |
6de9cd9a DN |
5434 | BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; |
5435 | ||
c7c79a09 JJ |
5436 | DECL_SAVED_TREE (fndecl) |
5437 | = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (fndecl), | |
5438 | DECL_INITIAL (fndecl)); | |
5439 | ||
77f2a970 JJ |
5440 | if (nonlocal_dummy_decls) |
5441 | { | |
5442 | BLOCK_VARS (DECL_INITIAL (fndecl)) | |
5443 | = chainon (BLOCK_VARS (DECL_INITIAL (fndecl)), nonlocal_dummy_decls); | |
5444 | pointer_set_destroy (nonlocal_dummy_decl_pset); | |
5445 | nonlocal_dummy_decls = NULL; | |
5446 | nonlocal_dummy_decl_pset = NULL; | |
5447 | } | |
5448 | ||
6de9cd9a DN |
5449 | /* Output the GENERIC tree. */ |
5450 | dump_function (TDI_original, fndecl); | |
5451 | ||
5452 | /* Store the end of the function, so that we get good line number | |
5453 | info for the epilogue. */ | |
5454 | cfun->function_end_locus = input_location; | |
5455 | ||
5456 | /* We're leaving the context of this function, so zap cfun. | |
5457 | It's still in DECL_STRUCT_FUNCTION, and we'll restore it in | |
5458 | tree_rest_of_compilation. */ | |
db2960f4 | 5459 | set_cfun (NULL); |
6de9cd9a DN |
5460 | |
5461 | if (old_context) | |
5462 | { | |
5463 | pop_function_context (); | |
5464 | saved_function_decls = saved_parent_function_decls; | |
5465 | } | |
5466 | current_function_decl = old_context; | |
5467 | ||
b8ff4e88 TB |
5468 | if (decl_function_context (fndecl) && !gfc_option.coarray == GFC_FCOARRAY_LIB |
5469 | && has_coarray_vars) | |
44de5aeb | 5470 | /* Register this function with cgraph just far enough to get it |
b8ff4e88 TB |
5471 | added to our parent's nested function list. |
5472 | If there are static coarrays in this function, the nested _caf_init | |
5473 | function has already called cgraph_create_node, which also created | |
5474 | the cgraph node for this function. */ | |
7c589800 | 5475 | (void) cgraph_create_node (fndecl); |
6de9cd9a | 5476 | else |
9618fb3c | 5477 | cgraph_finalize_function (fndecl, true); |
a64f5186 JJ |
5478 | |
5479 | gfc_trans_use_stmts (ns); | |
bd11e37d | 5480 | gfc_traverse_ns (ns, gfc_emit_parameter_debug_info); |
092231a8 TB |
5481 | |
5482 | if (sym->attr.is_main_program) | |
5483 | create_main_function (fndecl); | |
d74d8807 DK |
5484 | |
5485 | current_procedure_symbol = previous_procedure_symbol; | |
6de9cd9a DN |
5486 | } |
5487 | ||
092231a8 | 5488 | |
6de9cd9a DN |
5489 | void |
5490 | gfc_generate_constructors (void) | |
5491 | { | |
6e45f57b | 5492 | gcc_assert (gfc_static_ctors == NULL_TREE); |
6de9cd9a DN |
5493 | #if 0 |
5494 | tree fnname; | |
5495 | tree type; | |
5496 | tree fndecl; | |
5497 | tree decl; | |
5498 | tree tmp; | |
5499 | ||
5500 | if (gfc_static_ctors == NULL_TREE) | |
5501 | return; | |
5502 | ||
5880f14f | 5503 | fnname = get_file_function_name ("I"); |
b64fca63 | 5504 | type = build_function_type_list (void_type_node, NULL_TREE); |
6de9cd9a | 5505 | |
c2255bc4 AH |
5506 | fndecl = build_decl (input_location, |
5507 | FUNCTION_DECL, fnname, type); | |
6de9cd9a DN |
5508 | TREE_PUBLIC (fndecl) = 1; |
5509 | ||
c2255bc4 AH |
5510 | decl = build_decl (input_location, |
5511 | RESULT_DECL, NULL_TREE, void_type_node); | |
b785f485 RH |
5512 | DECL_ARTIFICIAL (decl) = 1; |
5513 | DECL_IGNORED_P (decl) = 1; | |
6de9cd9a DN |
5514 | DECL_CONTEXT (decl) = fndecl; |
5515 | DECL_RESULT (fndecl) = decl; | |
5516 | ||
5517 | pushdecl (fndecl); | |
5518 | ||
5519 | current_function_decl = fndecl; | |
5520 | ||
0e6df31e | 5521 | rest_of_decl_compilation (fndecl, 1, 0); |
6de9cd9a | 5522 | |
0e6df31e | 5523 | make_decl_rtl (fndecl); |
6de9cd9a | 5524 | |
c8cc8542 | 5525 | init_function_start (fndecl); |
6de9cd9a | 5526 | |
87a60f68 | 5527 | pushlevel (); |
6de9cd9a DN |
5528 | |
5529 | for (; gfc_static_ctors; gfc_static_ctors = TREE_CHAIN (gfc_static_ctors)) | |
5530 | { | |
db3927fb AH |
5531 | tmp = build_call_expr_loc (input_location, |
5532 | TREE_VALUE (gfc_static_ctors), 0); | |
c2255bc4 | 5533 | DECL_SAVED_TREE (fndecl) = build_stmt (input_location, EXPR_STMT, tmp); |
6de9cd9a DN |
5534 | } |
5535 | ||
c7c79a09 | 5536 | decl = getdecls (); |
87a60f68 | 5537 | poplevel (1, 1); |
6de9cd9a DN |
5538 | |
5539 | BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; | |
c7c79a09 JJ |
5540 | DECL_SAVED_TREE (fndecl) |
5541 | = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (fndecl), | |
5542 | DECL_INITIAL (fndecl)); | |
6de9cd9a DN |
5543 | |
5544 | free_after_parsing (cfun); | |
5545 | free_after_compilation (cfun); | |
5546 | ||
0f0377f6 | 5547 | tree_rest_of_compilation (fndecl); |
6de9cd9a DN |
5548 | |
5549 | current_function_decl = NULL_TREE; | |
5550 | #endif | |
5551 | } | |
5552 | ||
0de4325e TS |
5553 | /* Translates a BLOCK DATA program unit. This means emitting the |
5554 | commons contained therein plus their initializations. We also emit | |
5555 | a globally visible symbol to make sure that each BLOCK DATA program | |
5556 | unit remains unique. */ | |
5557 | ||
5558 | void | |
5559 | gfc_generate_block_data (gfc_namespace * ns) | |
5560 | { | |
5561 | tree decl; | |
5562 | tree id; | |
5563 | ||
c8cc8542 PB |
5564 | /* Tell the backend the source location of the block data. */ |
5565 | if (ns->proc_name) | |
5566 | gfc_set_backend_locus (&ns->proc_name->declared_at); | |
5567 | else | |
5568 | gfc_set_backend_locus (&gfc_current_locus); | |
5569 | ||
5570 | /* Process the DATA statements. */ | |
0de4325e TS |
5571 | gfc_trans_common (ns); |
5572 | ||
c8cc8542 PB |
5573 | /* Create a global symbol with the mane of the block data. This is to |
5574 | generate linker errors if the same name is used twice. It is never | |
5575 | really used. */ | |
0de4325e TS |
5576 | if (ns->proc_name) |
5577 | id = gfc_sym_mangled_function_id (ns->proc_name); | |
5578 | else | |
5579 | id = get_identifier ("__BLOCK_DATA__"); | |
5580 | ||
c2255bc4 AH |
5581 | decl = build_decl (input_location, |
5582 | VAR_DECL, id, gfc_array_index_type); | |
0de4325e TS |
5583 | TREE_PUBLIC (decl) = 1; |
5584 | TREE_STATIC (decl) = 1; | |
a64f5186 | 5585 | DECL_IGNORED_P (decl) = 1; |
0de4325e TS |
5586 | |
5587 | pushdecl (decl); | |
5588 | rest_of_decl_compilation (decl, 1, 0); | |
5589 | } | |
5590 | ||
83d890b9 | 5591 | |
9abe5e56 DK |
5592 | /* Process the local variables of a BLOCK construct. */ |
5593 | ||
5594 | void | |
6312ef45 | 5595 | gfc_process_block_locals (gfc_namespace* ns) |
9abe5e56 DK |
5596 | { |
5597 | tree decl; | |
5598 | ||
5599 | gcc_assert (saved_local_decls == NULL_TREE); | |
b8ff4e88 TB |
5600 | has_coarray_vars = false; |
5601 | ||
9abe5e56 DK |
5602 | generate_local_vars (ns); |
5603 | ||
b8ff4e88 TB |
5604 | if (gfc_option.coarray == GFC_FCOARRAY_LIB && has_coarray_vars) |
5605 | generate_coarray_init (ns); | |
5606 | ||
9abe5e56 DK |
5607 | decl = saved_local_decls; |
5608 | while (decl) | |
5609 | { | |
5610 | tree next; | |
5611 | ||
910ad8de NF |
5612 | next = DECL_CHAIN (decl); |
5613 | DECL_CHAIN (decl) = NULL_TREE; | |
9abe5e56 DK |
5614 | pushdecl (decl); |
5615 | decl = next; | |
5616 | } | |
5617 | saved_local_decls = NULL_TREE; | |
5618 | } | |
5619 | ||
5620 | ||
6de9cd9a | 5621 | #include "gt-fortran-trans-decl.h" |