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