]>
Commit | Line | Data |
---|---|---|
6de9cd9a DN |
1 | /* Backend function setup |
2 | Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. | |
3 | Contributed by Paul Brook | |
4 | ||
9fc4d79b | 5 | This file is part of GCC. |
6de9cd9a | 6 | |
9fc4d79b TS |
7 | GCC is free software; you can redistribute it and/or modify it under |
8 | the terms of the GNU General Public License as published by the Free | |
9 | Software Foundation; either version 2, or (at your option) any later | |
10 | version. | |
6de9cd9a | 11 | |
9fc4d79b TS |
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
6de9cd9a DN |
16 | |
17 | You should have received a copy of the GNU General Public License | |
9fc4d79b TS |
18 | along with GCC; see the file COPYING. If not, write to the Free |
19 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
20 | 02111-1307, USA. */ | |
6de9cd9a DN |
21 | |
22 | /* trans-decl.c -- Handling of backend function and variable decls, etc */ | |
23 | ||
24 | #include "config.h" | |
25 | #include "system.h" | |
26 | #include "coretypes.h" | |
27 | #include "tree.h" | |
28 | #include "tree-dump.h" | |
eadf906f | 29 | #include "tree-gimple.h" |
6de9cd9a DN |
30 | #include "ggc.h" |
31 | #include "toplev.h" | |
32 | #include "tm.h" | |
33 | #include "target.h" | |
34 | #include "function.h" | |
35 | #include "errors.h" | |
36 | #include "flags.h" | |
37 | #include "cgraph.h" | |
38 | #include <assert.h> | |
39 | #include "gfortran.h" | |
40 | #include "trans.h" | |
41 | #include "trans-types.h" | |
42 | #include "trans-array.h" | |
43 | #include "trans-const.h" | |
44 | /* Only for gfc_trans_code. Shouldn't need to include this. */ | |
45 | #include "trans-stmt.h" | |
46 | ||
47 | #define MAX_LABEL_VALUE 99999 | |
48 | ||
49 | ||
50 | /* Holds the result of the function if no result variable specified. */ | |
51 | ||
52 | static GTY(()) tree current_fake_result_decl; | |
53 | ||
54 | static GTY(()) tree current_function_return_label; | |
55 | ||
56 | ||
57 | /* Holds the variable DECLs for the current function. */ | |
58 | ||
59 | static GTY(()) tree saved_function_decls = NULL_TREE; | |
60 | static GTY(()) tree saved_parent_function_decls = NULL_TREE; | |
61 | ||
62 | ||
63 | /* The namespace of the module we're currently generating. Only used while | |
64 | outputting decls for module variables. Do not rely on this being set. */ | |
65 | ||
66 | static gfc_namespace *module_namespace; | |
67 | ||
68 | ||
69 | /* List of static constructor functions. */ | |
70 | ||
71 | tree gfc_static_ctors; | |
72 | ||
73 | ||
74 | /* Function declarations for builtin library functions. */ | |
75 | ||
76 | tree gfor_fndecl_internal_malloc; | |
77 | tree gfor_fndecl_internal_malloc64; | |
78 | tree gfor_fndecl_internal_free; | |
79 | tree gfor_fndecl_allocate; | |
80 | tree gfor_fndecl_allocate64; | |
81 | tree gfor_fndecl_deallocate; | |
82 | tree gfor_fndecl_pause_numeric; | |
83 | tree gfor_fndecl_pause_string; | |
84 | tree gfor_fndecl_stop_numeric; | |
85 | tree gfor_fndecl_stop_string; | |
86 | tree gfor_fndecl_select_string; | |
87 | tree gfor_fndecl_runtime_error; | |
88 | tree gfor_fndecl_in_pack; | |
89 | tree gfor_fndecl_in_unpack; | |
90 | tree gfor_fndecl_associated; | |
91 | ||
92 | ||
93 | /* Math functions. Many other math functions are handled in | |
94 | trans-intrinsic.c. */ | |
95 | ||
5b200ac2 | 96 | gfc_powdecl_list gfor_fndecl_math_powi[3][2]; |
6de9cd9a DN |
97 | tree gfor_fndecl_math_cpowf; |
98 | tree gfor_fndecl_math_cpow; | |
99 | tree gfor_fndecl_math_cabsf; | |
100 | tree gfor_fndecl_math_cabs; | |
101 | tree gfor_fndecl_math_sign4; | |
102 | tree gfor_fndecl_math_sign8; | |
103 | tree gfor_fndecl_math_ishftc4; | |
104 | tree gfor_fndecl_math_ishftc8; | |
105 | tree gfor_fndecl_math_exponent4; | |
106 | tree gfor_fndecl_math_exponent8; | |
107 | ||
108 | ||
109 | /* String functions. */ | |
110 | ||
111 | tree gfor_fndecl_copy_string; | |
112 | tree gfor_fndecl_compare_string; | |
113 | tree gfor_fndecl_concat_string; | |
114 | tree gfor_fndecl_string_len_trim; | |
115 | tree gfor_fndecl_string_index; | |
116 | tree gfor_fndecl_string_scan; | |
117 | tree gfor_fndecl_string_verify; | |
118 | tree gfor_fndecl_string_trim; | |
119 | tree gfor_fndecl_string_repeat; | |
120 | tree gfor_fndecl_adjustl; | |
121 | tree gfor_fndecl_adjustr; | |
122 | ||
123 | ||
124 | /* Other misc. runtime library functions. */ | |
125 | ||
126 | tree gfor_fndecl_size0; | |
127 | tree gfor_fndecl_size1; | |
128 | ||
129 | /* Intrinsic functions implemented in FORTRAN. */ | |
130 | tree gfor_fndecl_si_kind; | |
131 | tree gfor_fndecl_sr_kind; | |
132 | ||
133 | ||
134 | static void | |
135 | gfc_add_decl_to_parent_function (tree decl) | |
136 | { | |
137 | assert (decl); | |
138 | DECL_CONTEXT (decl) = DECL_CONTEXT (current_function_decl); | |
139 | DECL_NONLOCAL (decl) = 1; | |
140 | TREE_CHAIN (decl) = saved_parent_function_decls; | |
141 | saved_parent_function_decls = decl; | |
142 | } | |
143 | ||
144 | void | |
145 | gfc_add_decl_to_function (tree decl) | |
146 | { | |
147 | assert (decl); | |
148 | TREE_USED (decl) = 1; | |
149 | DECL_CONTEXT (decl) = current_function_decl; | |
150 | TREE_CHAIN (decl) = saved_function_decls; | |
151 | saved_function_decls = decl; | |
152 | } | |
153 | ||
154 | ||
155 | /* Build a backend label declaration. | |
156 | Set TREE_USED for named lables. For artificial labels it's up to the | |
157 | caller to mark the label as used. */ | |
158 | ||
159 | tree | |
160 | gfc_build_label_decl (tree label_id) | |
161 | { | |
162 | /* 2^32 temporaries should be enough. */ | |
163 | static unsigned int tmp_num = 1; | |
164 | tree label_decl; | |
165 | char *label_name; | |
166 | ||
167 | if (label_id == NULL_TREE) | |
168 | { | |
169 | /* Build an internal label name. */ | |
170 | ASM_FORMAT_PRIVATE_NAME (label_name, "L", tmp_num++); | |
171 | label_id = get_identifier (label_name); | |
172 | } | |
173 | else | |
174 | label_name = NULL; | |
175 | ||
176 | /* Build the LABEL_DECL node. Labels have no type. */ | |
177 | label_decl = build_decl (LABEL_DECL, label_id, void_type_node); | |
178 | DECL_CONTEXT (label_decl) = current_function_decl; | |
179 | DECL_MODE (label_decl) = VOIDmode; | |
180 | ||
181 | if (label_name) | |
182 | { | |
183 | DECL_ARTIFICIAL (label_decl) = 1; | |
184 | } | |
185 | else | |
186 | { | |
187 | /* We always define the label as used, even if the original source | |
188 | file never references the label. We don't want all kinds of | |
189 | spurious warnings for old-style Fortran code with too many | |
190 | labels. */ | |
191 | TREE_USED (label_decl) = 1; | |
192 | } | |
193 | ||
194 | return label_decl; | |
195 | } | |
196 | ||
197 | ||
198 | /* Returns the return label for the current function. */ | |
199 | ||
200 | tree | |
201 | gfc_get_return_label (void) | |
202 | { | |
203 | char name[GFC_MAX_SYMBOL_LEN + 10]; | |
204 | ||
205 | if (current_function_return_label) | |
206 | return current_function_return_label; | |
207 | ||
208 | sprintf (name, "__return_%s", | |
209 | IDENTIFIER_POINTER (DECL_NAME (current_function_decl))); | |
210 | ||
211 | current_function_return_label = | |
212 | gfc_build_label_decl (get_identifier (name)); | |
213 | ||
214 | DECL_ARTIFICIAL (current_function_return_label) = 1; | |
215 | ||
216 | return current_function_return_label; | |
217 | } | |
218 | ||
219 | ||
220 | /* Return the backend label declaration for a given label structure, | |
221 | or create it if it doesn't exist yet. */ | |
222 | ||
223 | tree | |
224 | gfc_get_label_decl (gfc_st_label * lp) | |
225 | { | |
226 | ||
227 | if (lp->backend_decl) | |
228 | return lp->backend_decl; | |
229 | else | |
230 | { | |
231 | char label_name[GFC_MAX_SYMBOL_LEN + 1]; | |
232 | tree label_decl; | |
233 | ||
234 | /* Validate the label declaration from the front end. */ | |
235 | assert (lp != NULL && lp->value <= MAX_LABEL_VALUE); | |
236 | ||
237 | /* Build a mangled name for the label. */ | |
238 | sprintf (label_name, "__label_%.6d", lp->value); | |
239 | ||
240 | /* Build the LABEL_DECL node. */ | |
241 | label_decl = gfc_build_label_decl (get_identifier (label_name)); | |
242 | ||
243 | /* Tell the debugger where the label came from. */ | |
244 | if (lp->value <= MAX_LABEL_VALUE) /* An internal label */ | |
245 | { | |
d4fa05b9 TS |
246 | DECL_SOURCE_LINE (label_decl) = lp->where.lb->linenum; |
247 | DECL_SOURCE_FILE (label_decl) = lp->where.lb->file->filename; | |
6de9cd9a DN |
248 | } |
249 | else | |
250 | DECL_ARTIFICIAL (label_decl) = 1; | |
251 | ||
252 | /* Store the label in the label list and return the LABEL_DECL. */ | |
253 | lp->backend_decl = label_decl; | |
254 | return label_decl; | |
255 | } | |
256 | } | |
257 | ||
258 | ||
259 | /* Convert a gfc_symbol to an identifier of the same name. */ | |
260 | ||
261 | static tree | |
262 | gfc_sym_identifier (gfc_symbol * sym) | |
263 | { | |
264 | return (get_identifier (sym->name)); | |
265 | } | |
266 | ||
267 | ||
268 | /* Construct mangled name from symbol name. */ | |
269 | ||
270 | static tree | |
271 | gfc_sym_mangled_identifier (gfc_symbol * sym) | |
272 | { | |
273 | char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1]; | |
274 | ||
275 | if (sym->module[0] == 0) | |
276 | return gfc_sym_identifier (sym); | |
277 | else | |
278 | { | |
279 | snprintf (name, sizeof name, "__%s__%s", sym->module, sym->name); | |
280 | return get_identifier (name); | |
281 | } | |
282 | } | |
283 | ||
284 | ||
285 | /* Construct mangled function name from symbol name. */ | |
286 | ||
287 | static tree | |
288 | gfc_sym_mangled_function_id (gfc_symbol * sym) | |
289 | { | |
290 | int has_underscore; | |
291 | char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1]; | |
292 | ||
293 | if (sym->module[0] == 0 || sym->attr.proc == PROC_EXTERNAL | |
294 | || (sym->module[0] != 0 && sym->attr.if_source == IFSRC_IFBODY)) | |
295 | { | |
296 | if (strcmp (sym->name, "MAIN__") == 0 | |
297 | || sym->attr.proc == PROC_INTRINSIC) | |
298 | return get_identifier (sym->name); | |
299 | ||
300 | if (gfc_option.flag_underscoring) | |
301 | { | |
302 | has_underscore = strchr (sym->name, '_') != 0; | |
303 | if (gfc_option.flag_second_underscore && has_underscore) | |
304 | snprintf (name, sizeof name, "%s__", sym->name); | |
305 | else | |
306 | snprintf (name, sizeof name, "%s_", sym->name); | |
307 | return get_identifier (name); | |
308 | } | |
309 | else | |
310 | return get_identifier (sym->name); | |
311 | } | |
312 | else | |
313 | { | |
314 | snprintf (name, sizeof name, "__%s__%s", sym->module, sym->name); | |
315 | return get_identifier (name); | |
316 | } | |
317 | } | |
318 | ||
319 | ||
320 | /* Finish processing of a declaration and install its initial value. */ | |
321 | ||
322 | static void | |
323 | gfc_finish_decl (tree decl, tree init) | |
324 | { | |
325 | if (TREE_CODE (decl) == PARM_DECL) | |
326 | assert (init == NULL_TREE); | |
327 | /* Remember that PARM_DECL doesn't have a DECL_INITIAL field per se | |
328 | -- it overlaps DECL_ARG_TYPE. */ | |
329 | else if (init == NULL_TREE) | |
330 | assert (DECL_INITIAL (decl) == NULL_TREE); | |
331 | else | |
332 | assert (DECL_INITIAL (decl) == error_mark_node); | |
333 | ||
334 | if (init != NULL_TREE) | |
335 | { | |
336 | if (TREE_CODE (decl) != TYPE_DECL) | |
337 | DECL_INITIAL (decl) = init; | |
338 | else | |
339 | { | |
340 | /* typedef foo = bar; store the type of bar as the type of foo. */ | |
341 | TREE_TYPE (decl) = TREE_TYPE (init); | |
342 | DECL_INITIAL (decl) = init = 0; | |
343 | } | |
344 | } | |
345 | ||
346 | if (TREE_CODE (decl) == VAR_DECL) | |
347 | { | |
348 | if (DECL_SIZE (decl) == NULL_TREE | |
349 | && TYPE_SIZE (TREE_TYPE (decl)) != NULL_TREE) | |
350 | layout_decl (decl, 0); | |
351 | ||
352 | /* A static variable with an incomplete type is an error if it is | |
353 | initialized. Also if it is not file scope. Otherwise, let it | |
354 | through, but if it is not `extern' then it may cause an error | |
355 | message later. */ | |
356 | /* An automatic variable with an incomplete type is an error. */ | |
357 | if (DECL_SIZE (decl) == NULL_TREE | |
358 | && (TREE_STATIC (decl) ? (DECL_INITIAL (decl) != 0 | |
359 | || DECL_CONTEXT (decl) != 0) | |
360 | : !DECL_EXTERNAL (decl))) | |
361 | { | |
362 | gfc_fatal_error ("storage size not known"); | |
363 | } | |
364 | ||
365 | if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) | |
366 | && (DECL_SIZE (decl) != 0) | |
367 | && (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)) | |
368 | { | |
369 | gfc_fatal_error ("storage size not constant"); | |
370 | } | |
371 | } | |
372 | ||
373 | } | |
374 | ||
375 | ||
376 | /* Apply symbol attributes to a variable, and add it to the function scope. */ | |
377 | ||
378 | static void | |
379 | gfc_finish_var_decl (tree decl, gfc_symbol * sym) | |
380 | { | |
381 | /* TREE_ADDRESSABLE means the address of this variable is acualy needed. | |
382 | This is the equivalent of the TARGET variables. | |
383 | We also need to set this if the variable is passed by reference in a | |
384 | CALL statement. */ | |
385 | if (sym->attr.target) | |
386 | TREE_ADDRESSABLE (decl) = 1; | |
387 | /* If it wasn't used we wouldn't be getting it. */ | |
388 | TREE_USED (decl) = 1; | |
389 | ||
390 | /* Chain this decl to the pending declarations. Don't do pushdecl() | |
391 | because this would add them to the current scope rather than the | |
392 | function scope. */ | |
393 | if (current_function_decl != NULL_TREE) | |
394 | { | |
395 | if (sym->ns->proc_name->backend_decl == current_function_decl) | |
396 | gfc_add_decl_to_function (decl); | |
397 | else | |
398 | gfc_add_decl_to_parent_function (decl); | |
399 | } | |
400 | ||
401 | /* If a variable is USE associated, it's always external. */ | |
402 | if (sym->attr.use_assoc) | |
403 | { | |
404 | DECL_EXTERNAL (decl) = 1; | |
405 | TREE_PUBLIC (decl) = 1; | |
406 | } | |
407 | else if (sym->module[0] && !sym->attr.result) | |
408 | { | |
409 | /* TODO: Don't set sym->module for result variables. */ | |
410 | assert (current_function_decl == NULL_TREE); | |
411 | /* This is the declaration of a module variable. */ | |
412 | TREE_PUBLIC (decl) = 1; | |
413 | TREE_STATIC (decl) = 1; | |
414 | } | |
415 | ||
416 | if ((sym->attr.save || sym->attr.data || sym->value) | |
417 | && !sym->attr.use_assoc) | |
418 | TREE_STATIC (decl) = 1; | |
419 | ||
420 | /* Keep variables larger than max-stack-var-size off stack. */ | |
421 | if (!sym->ns->proc_name->attr.recursive | |
422 | && INTEGER_CST_P (DECL_SIZE_UNIT (decl)) | |
423 | && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))) | |
424 | TREE_STATIC (decl) = 1; | |
425 | } | |
426 | ||
427 | ||
428 | /* Allocate the lang-specific part of a decl. */ | |
429 | ||
430 | void | |
431 | gfc_allocate_lang_decl (tree decl) | |
432 | { | |
433 | DECL_LANG_SPECIFIC (decl) = (struct lang_decl *) | |
434 | ggc_alloc_cleared (sizeof (struct lang_decl)); | |
435 | } | |
436 | ||
437 | /* Remember a symbol to generate initialization/cleanup code at function | |
438 | entry/exit. */ | |
439 | ||
440 | static void | |
441 | gfc_defer_symbol_init (gfc_symbol * sym) | |
442 | { | |
443 | gfc_symbol *p; | |
444 | gfc_symbol *last; | |
445 | gfc_symbol *head; | |
446 | ||
447 | /* Don't add a symbol twice. */ | |
448 | if (sym->tlink) | |
449 | return; | |
450 | ||
451 | last = head = sym->ns->proc_name; | |
452 | p = last->tlink; | |
453 | ||
454 | /* Make sure that setup code for dummy variables which are used in the | |
455 | setup of other variables is generated first. */ | |
456 | if (sym->attr.dummy) | |
457 | { | |
458 | /* Find the first dummy arg seen after us, or the first non-dummy arg. | |
459 | This is a circular list, so don't go past the head. */ | |
460 | while (p != head | |
461 | && (!p->attr.dummy || p->dummy_order > sym->dummy_order)) | |
462 | { | |
463 | last = p; | |
464 | p = p->tlink; | |
465 | } | |
466 | } | |
467 | /* Insert in between last and p. */ | |
468 | last->tlink = sym; | |
469 | sym->tlink = p; | |
470 | } | |
471 | ||
472 | ||
473 | /* Create an array index type variable with function scope. */ | |
474 | ||
475 | static tree | |
476 | create_index_var (const char * pfx, int nest) | |
477 | { | |
478 | tree decl; | |
479 | ||
480 | decl = gfc_create_var_np (gfc_array_index_type, pfx); | |
481 | if (nest) | |
482 | gfc_add_decl_to_parent_function (decl); | |
483 | else | |
484 | gfc_add_decl_to_function (decl); | |
485 | return decl; | |
486 | } | |
487 | ||
488 | ||
489 | /* Create variables to hold all the non-constant bits of info for a | |
490 | descriptorless array. Remember these in the lang-specific part of the | |
491 | type. */ | |
492 | ||
493 | static void | |
494 | gfc_build_qualified_array (tree decl, gfc_symbol * sym) | |
495 | { | |
496 | tree type; | |
497 | int dim; | |
498 | int nest; | |
499 | ||
500 | type = TREE_TYPE (decl); | |
501 | ||
502 | /* We just use the descriptor, if there is one. */ | |
503 | if (GFC_DESCRIPTOR_TYPE_P (type)) | |
504 | return; | |
505 | ||
506 | assert (GFC_ARRAY_TYPE_P (type)); | |
507 | nest = (sym->ns->proc_name->backend_decl != current_function_decl) | |
508 | && !sym->attr.contained; | |
509 | ||
510 | for (dim = 0; dim < GFC_TYPE_ARRAY_RANK (type); dim++) | |
511 | { | |
512 | if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE) | |
513 | GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest); | |
514 | /* Don't try to use the unkown bound for assumed shape arrays. */ | |
515 | if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE | |
516 | && (sym->as->type != AS_ASSUMED_SIZE | |
517 | || dim < GFC_TYPE_ARRAY_RANK (type) - 1)) | |
518 | GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest); | |
519 | ||
520 | if (GFC_TYPE_ARRAY_STRIDE (type, dim) == NULL_TREE) | |
521 | GFC_TYPE_ARRAY_STRIDE (type, dim) = create_index_var ("stride", nest); | |
522 | } | |
523 | if (GFC_TYPE_ARRAY_OFFSET (type) == NULL_TREE) | |
524 | { | |
525 | GFC_TYPE_ARRAY_OFFSET (type) = gfc_create_var_np (gfc_array_index_type, | |
526 | "offset"); | |
527 | if (nest) | |
528 | gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type)); | |
529 | else | |
530 | gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type)); | |
531 | } | |
532 | } | |
533 | ||
534 | ||
535 | /* For some dummy arguments we don't use the actual argument directly. | |
536 | Instead we create a local decl and use that. This allows us to preform | |
537 | initialization, and construct full type information. */ | |
538 | ||
539 | static tree | |
540 | gfc_build_dummy_array_decl (gfc_symbol * sym, tree dummy) | |
541 | { | |
542 | tree decl; | |
543 | tree type; | |
544 | gfc_array_spec *as; | |
545 | char *name; | |
546 | int packed; | |
547 | int n; | |
548 | bool known_size; | |
549 | ||
550 | if (sym->attr.pointer || sym->attr.allocatable) | |
551 | return dummy; | |
552 | ||
553 | /* Add to list of variables if not a fake result variable. */ | |
554 | if (sym->attr.result || sym->attr.dummy) | |
555 | gfc_defer_symbol_init (sym); | |
556 | ||
557 | type = TREE_TYPE (dummy); | |
558 | assert (TREE_CODE (dummy) == PARM_DECL | |
559 | && POINTER_TYPE_P (type)); | |
560 | ||
561 | /* Do we know the element size. */ | |
562 | known_size = sym->ts.type != BT_CHARACTER | |
563 | || INTEGER_CST_P (sym->ts.cl->backend_decl); | |
564 | ||
565 | if (known_size && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type))) | |
566 | { | |
567 | /* For descriptorless arrays with known element size the actual | |
568 | argument is sufficient. */ | |
569 | assert (GFC_ARRAY_TYPE_P (type)); | |
570 | gfc_build_qualified_array (dummy, sym); | |
571 | return dummy; | |
572 | } | |
573 | ||
574 | type = TREE_TYPE (type); | |
575 | if (GFC_DESCRIPTOR_TYPE_P (type)) | |
576 | { | |
577 | /* Create a decriptorless array pointer. */ | |
578 | as = sym->as; | |
579 | packed = 0; | |
580 | if (!gfc_option.flag_repack_arrays) | |
581 | { | |
582 | if (as->type == AS_ASSUMED_SIZE) | |
583 | packed = 2; | |
584 | } | |
585 | else | |
586 | { | |
587 | if (as->type == AS_EXPLICIT) | |
588 | { | |
589 | packed = 2; | |
590 | for (n = 0; n < as->rank; n++) | |
591 | { | |
592 | if (!(as->upper[n] | |
593 | && as->lower[n] | |
594 | && as->upper[n]->expr_type == EXPR_CONSTANT | |
595 | && as->lower[n]->expr_type == EXPR_CONSTANT)) | |
596 | packed = 1; | |
597 | } | |
598 | } | |
599 | else | |
600 | packed = 1; | |
601 | } | |
602 | ||
603 | type = gfc_typenode_for_spec (&sym->ts); | |
604 | type = gfc_get_nodesc_array_type (type, sym->as, packed); | |
605 | } | |
606 | else | |
607 | { | |
608 | /* We now have an expression for the element size, so create a fully | |
609 | qualified type. Reset sym->backend decl or this will just return the | |
610 | old type. */ | |
611 | sym->backend_decl = NULL_TREE; | |
612 | type = gfc_sym_type (sym); | |
613 | packed = 2; | |
614 | } | |
615 | ||
616 | ASM_FORMAT_PRIVATE_NAME (name, IDENTIFIER_POINTER (DECL_NAME (dummy)), 0); | |
617 | decl = build_decl (VAR_DECL, get_identifier (name), type); | |
618 | ||
619 | DECL_ARTIFICIAL (decl) = 1; | |
620 | TREE_PUBLIC (decl) = 0; | |
621 | TREE_STATIC (decl) = 0; | |
622 | DECL_EXTERNAL (decl) = 0; | |
623 | ||
624 | /* We should never get deferred shape arrays here. We used to because of | |
625 | frontend bugs. */ | |
626 | assert (sym->as->type != AS_DEFERRED); | |
627 | ||
628 | switch (packed) | |
629 | { | |
630 | case 1: | |
631 | GFC_DECL_PARTIAL_PACKED_ARRAY (decl) = 1; | |
632 | break; | |
633 | ||
634 | case 2: | |
635 | GFC_DECL_PACKED_ARRAY (decl) = 1; | |
636 | break; | |
637 | } | |
638 | ||
639 | gfc_build_qualified_array (decl, sym); | |
640 | ||
641 | if (DECL_LANG_SPECIFIC (dummy)) | |
642 | DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (dummy); | |
643 | else | |
644 | gfc_allocate_lang_decl (decl); | |
645 | ||
646 | GFC_DECL_SAVED_DESCRIPTOR (decl) = dummy; | |
647 | ||
648 | if (sym->ns->proc_name->backend_decl == current_function_decl | |
649 | || sym->attr.contained) | |
650 | gfc_add_decl_to_function (decl); | |
651 | else | |
652 | gfc_add_decl_to_parent_function (decl); | |
653 | ||
654 | return decl; | |
655 | } | |
656 | ||
657 | ||
658 | /* Return a constant or a variable to use as a string length. Does not | |
659 | add the decl to the current scope. */ | |
660 | ||
661 | static tree | |
662 | gfc_create_string_length (gfc_symbol * sym) | |
663 | { | |
664 | tree length; | |
665 | ||
666 | assert (sym->ts.cl); | |
667 | gfc_conv_const_charlen (sym->ts.cl); | |
668 | ||
669 | if (sym->ts.cl->backend_decl == NULL_TREE) | |
670 | { | |
671 | char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2]; | |
672 | ||
673 | /* Also prefix the mangled name. */ | |
674 | strcpy (&name[1], sym->name); | |
675 | name[0] = '.'; | |
676 | length = build_decl (VAR_DECL, get_identifier (name), | |
677 | gfc_strlen_type_node); | |
678 | DECL_ARTIFICIAL (length) = 1; | |
679 | TREE_USED (length) = 1; | |
680 | gfc_defer_symbol_init (sym); | |
681 | sym->ts.cl->backend_decl = length; | |
682 | } | |
683 | ||
684 | return sym->ts.cl->backend_decl; | |
685 | } | |
686 | ||
687 | ||
688 | /* Return the decl for a gfc_symbol, create it if it doesn't already | |
689 | exist. */ | |
690 | ||
691 | tree | |
692 | gfc_get_symbol_decl (gfc_symbol * sym) | |
693 | { | |
694 | tree decl; | |
695 | tree length = NULL_TREE; | |
696 | gfc_se se; | |
697 | int byref; | |
698 | ||
699 | assert (sym->attr.referenced); | |
700 | ||
701 | if (sym->ns && sym->ns->proc_name->attr.function) | |
702 | byref = gfc_return_by_reference (sym->ns->proc_name); | |
703 | else | |
704 | byref = 0; | |
705 | ||
706 | if ((sym->attr.dummy && ! sym->attr.function) || (sym->attr.result && byref)) | |
707 | { | |
708 | /* Return via extra parameter. */ | |
709 | if (sym->attr.result && byref | |
710 | && !sym->backend_decl) | |
711 | { | |
712 | sym->backend_decl = | |
713 | DECL_ARGUMENTS (sym->ns->proc_name->backend_decl); | |
714 | } | |
715 | ||
716 | /* Dummy variables should already have been created. */ | |
717 | assert (sym->backend_decl); | |
718 | ||
719 | /* Create a character length variable. */ | |
720 | if (sym->ts.type == BT_CHARACTER) | |
721 | { | |
722 | if (sym->ts.cl->backend_decl == NULL_TREE) | |
723 | { | |
724 | length = gfc_create_string_length (sym); | |
725 | if (TREE_CODE (length) != INTEGER_CST) | |
726 | { | |
727 | gfc_finish_var_decl (length, sym); | |
728 | gfc_defer_symbol_init (sym); | |
729 | } | |
730 | } | |
731 | } | |
732 | ||
733 | /* Use a copy of the descriptor for dummy arrays. */ | |
734 | if (sym->attr.dimension && !TREE_USED (sym->backend_decl)) | |
735 | { | |
736 | sym->backend_decl = | |
737 | gfc_build_dummy_array_decl (sym, sym->backend_decl); | |
738 | } | |
739 | ||
740 | TREE_USED (sym->backend_decl) = 1; | |
741 | return sym->backend_decl; | |
742 | } | |
743 | ||
744 | if (sym->backend_decl) | |
745 | return sym->backend_decl; | |
746 | ||
747 | if (sym->attr.entry) | |
748 | gfc_todo_error ("alternate entry"); | |
749 | ||
750 | /* Catch function declarations. Only used for actual parameters. */ | |
751 | if (sym->attr.flavor == FL_PROCEDURE) | |
752 | { | |
753 | decl = gfc_get_extern_function_decl (sym); | |
754 | return decl; | |
755 | } | |
756 | ||
757 | if (sym->attr.intrinsic) | |
758 | internal_error ("intrinsic variable which isn't a procedure"); | |
759 | ||
760 | /* Create string length decl first so that they can be used in the | |
761 | type declaration. */ | |
762 | if (sym->ts.type == BT_CHARACTER) | |
763 | length = gfc_create_string_length (sym); | |
764 | ||
765 | /* Create the decl for the variable. */ | |
766 | decl = build_decl (VAR_DECL, gfc_sym_identifier (sym), gfc_sym_type (sym)); | |
767 | ||
768 | /* Symbols from modules have its assembler name should be mangled. | |
769 | This is done here rather than in gfc_finish_var_decl because it | |
770 | is different for string length variables. */ | |
771 | if (sym->module[0]) | |
772 | SET_DECL_ASSEMBLER_NAME (decl, gfc_sym_mangled_identifier (sym)); | |
773 | ||
774 | if (sym->attr.dimension) | |
775 | { | |
776 | /* Create variables to hold the non-constant bits of array info. */ | |
777 | gfc_build_qualified_array (decl, sym); | |
778 | ||
779 | /* Remember this variable for allocation/cleanup. */ | |
780 | gfc_defer_symbol_init (sym); | |
781 | ||
782 | if ((sym->attr.allocatable || !sym->attr.dummy) && !sym->attr.pointer) | |
783 | GFC_DECL_PACKED_ARRAY (decl) = 1; | |
784 | } | |
785 | ||
786 | gfc_finish_var_decl (decl, sym); | |
787 | ||
788 | if (sym->attr.assign) | |
789 | { | |
790 | gfc_allocate_lang_decl (decl); | |
791 | GFC_DECL_ASSIGN (decl) = 1; | |
792 | length = gfc_create_var (gfc_strlen_type_node, sym->name); | |
793 | GFC_DECL_STRING_LEN (decl) = length; | |
794 | GFC_DECL_ASSIGN_ADDR (decl) = gfc_create_var (pvoid_type_node, sym->name); | |
795 | /* TODO: Need to check we don't change TREE_STATIC (decl) later. */ | |
796 | TREE_STATIC (length) = TREE_STATIC (decl); | |
797 | /* STRING_LENGTH is also used as flag. Less than -1 means that | |
798 | ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the | |
799 | target label's address. Other value is the length of format string | |
800 | and ASSIGN_ADDR is the address of format string. */ | |
801 | DECL_INITIAL (length) = build_int_2 (-2, -1); | |
802 | } | |
803 | ||
804 | /* TODO: Initialization of pointer variables. */ | |
805 | switch (sym->ts.type) | |
806 | { | |
807 | case BT_CHARACTER: | |
808 | /* Character variables need special handling. */ | |
809 | gfc_allocate_lang_decl (decl); | |
810 | ||
811 | if (TREE_CODE (length) == INTEGER_CST) | |
812 | { | |
813 | /* Static initializer for string scalars. | |
814 | Initialization of string arrays is handled elsewhere. */ | |
815 | if (sym->value && sym->attr.dimension == 0) | |
816 | { | |
817 | assert (TREE_STATIC (decl)); | |
818 | if (sym->attr.pointer) | |
819 | gfc_todo_error ("initialization of character pointers"); | |
820 | DECL_INITIAL (decl) = gfc_conv_string_init (length, sym->value); | |
821 | } | |
822 | } | |
823 | else | |
824 | { | |
825 | char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2]; | |
826 | ||
827 | if (sym->module[0]) | |
828 | { | |
829 | /* Also prefix the mangled name for symbols from modules. */ | |
830 | strcpy (&name[1], sym->name); | |
831 | name[0] = '.'; | |
832 | strcpy (&name[1], | |
833 | IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (length))); | |
834 | SET_DECL_ASSEMBLER_NAME (decl, get_identifier (name)); | |
835 | } | |
836 | gfc_finish_var_decl (length, sym); | |
837 | assert (!sym->value); | |
838 | } | |
839 | break; | |
840 | ||
841 | case BT_DERIVED: | |
842 | if (sym->value && ! (sym->attr.use_assoc || sym->attr.dimension)) | |
843 | { | |
844 | gfc_init_se (&se, NULL); | |
845 | gfc_conv_structure (&se, sym->value, 1); | |
846 | DECL_INITIAL (decl) = se.expr; | |
847 | } | |
848 | break; | |
849 | ||
850 | default: | |
851 | /* Static initializers for SAVEd variables. Arrays have already been | |
852 | remembered. Module variables are initialized when the module is | |
853 | loaded. */ | |
854 | if (sym->value && ! (sym->attr.use_assoc || sym->attr.dimension)) | |
855 | { | |
856 | assert (TREE_STATIC (decl)); | |
857 | gfc_init_se (&se, NULL); | |
858 | gfc_conv_constant (&se, sym->value); | |
859 | DECL_INITIAL (decl) = se.expr; | |
860 | } | |
861 | break; | |
862 | } | |
863 | sym->backend_decl = decl; | |
864 | ||
865 | return decl; | |
866 | } | |
867 | ||
868 | ||
869 | /* Get a basic decl for an external function. */ | |
870 | ||
871 | tree | |
872 | gfc_get_extern_function_decl (gfc_symbol * sym) | |
873 | { | |
874 | tree type; | |
875 | tree fndecl; | |
876 | gfc_expr e; | |
877 | gfc_intrinsic_sym *isym; | |
878 | gfc_expr argexpr; | |
879 | char s[GFC_MAX_SYMBOL_LEN]; | |
880 | tree name; | |
881 | tree mangled_name; | |
882 | ||
883 | if (sym->backend_decl) | |
884 | return sym->backend_decl; | |
885 | ||
886 | if (sym->attr.intrinsic) | |
887 | { | |
888 | /* Call the resolution function to get the actual name. This is | |
889 | a nasty hack which relies on the resolution functions only looking | |
890 | at the first argument. We pass NULL for the second argument | |
891 | otherwise things like AINT get confused. */ | |
892 | isym = gfc_find_function (sym->name); | |
893 | assert (isym->resolve.f0 != NULL); | |
894 | ||
895 | memset (&e, 0, sizeof (e)); | |
896 | e.expr_type = EXPR_FUNCTION; | |
897 | ||
898 | memset (&argexpr, 0, sizeof (argexpr)); | |
899 | assert (isym->formal); | |
900 | argexpr.ts = isym->formal->ts; | |
901 | ||
902 | if (isym->formal->next == NULL) | |
903 | isym->resolve.f1 (&e, &argexpr); | |
904 | else | |
905 | { | |
906 | /* All specific intrinsics take one or two arguments. */ | |
907 | assert (isym->formal->next->next == NULL); | |
908 | isym->resolve.f2 (&e, &argexpr, NULL); | |
909 | } | |
910 | sprintf (s, "specific%s", e.value.function.name); | |
911 | name = get_identifier (s); | |
912 | mangled_name = name; | |
913 | } | |
914 | else | |
915 | { | |
916 | name = gfc_sym_identifier (sym); | |
917 | mangled_name = gfc_sym_mangled_function_id (sym); | |
918 | } | |
919 | ||
920 | type = gfc_get_function_type (sym); | |
921 | fndecl = build_decl (FUNCTION_DECL, name, type); | |
922 | ||
923 | SET_DECL_ASSEMBLER_NAME (fndecl, mangled_name); | |
924 | /* If the return type is a pointer, avoid alias issues by setting | |
925 | DECL_IS_MALLOC to nonzero. This means that the function should be | |
926 | treated as if it were a malloc, meaning it returns a pointer that | |
927 | is not an alias. */ | |
928 | if (POINTER_TYPE_P (type)) | |
929 | DECL_IS_MALLOC (fndecl) = 1; | |
930 | ||
931 | /* Set the context of this decl. */ | |
932 | if (0 && sym->ns && sym->ns->proc_name) | |
933 | { | |
934 | /* TODO: Add external decls to the appropriate scope. */ | |
935 | DECL_CONTEXT (fndecl) = sym->ns->proc_name->backend_decl; | |
936 | } | |
937 | else | |
938 | { | |
939 | /* Global declaration, eg. intrinsic subroutine. */ | |
940 | DECL_CONTEXT (fndecl) = NULL_TREE; | |
941 | } | |
942 | ||
943 | DECL_EXTERNAL (fndecl) = 1; | |
944 | ||
945 | /* This specifies if a function is globaly addressable, ie. it is | |
946 | the opposite of declaring static in C. */ | |
947 | TREE_PUBLIC (fndecl) = 1; | |
948 | ||
949 | /* Set attributes for PURE functions. A call to PURE function in the | |
950 | Fortran 95 sense is both pure and without side effects in the C | |
951 | sense. */ | |
952 | if (sym->attr.pure || sym->attr.elemental) | |
953 | { | |
b7e6a6b3 TS |
954 | if (sym->attr.function) |
955 | DECL_IS_PURE (fndecl) = 1; | |
956 | /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT) | |
957 | parameters and don't use alternate returns (is this | |
958 | allowed?). In that case, calls to them are meaningless, and | |
959 | can be optimized away. See also in gfc_build_function_decl(). */ | |
960 | TREE_SIDE_EFFECTS (fndecl) = 0; | |
6de9cd9a DN |
961 | } |
962 | ||
963 | sym->backend_decl = fndecl; | |
964 | ||
965 | if (DECL_CONTEXT (fndecl) == NULL_TREE) | |
966 | pushdecl_top_level (fndecl); | |
967 | ||
968 | return fndecl; | |
969 | } | |
970 | ||
971 | ||
972 | /* Create a declaration for a procedure. For external functions (in the C | |
973 | sense) use gfc_get_extern_function_decl. */ | |
974 | ||
975 | void | |
976 | gfc_build_function_decl (gfc_symbol * sym) | |
977 | { | |
978 | tree fndecl, type, result_decl, typelist, arglist; | |
979 | tree length; | |
980 | symbol_attribute attr; | |
981 | gfc_formal_arglist *f; | |
982 | ||
983 | assert (!sym->backend_decl); | |
984 | assert (!sym->attr.external); | |
985 | ||
986 | /* Allow only one nesting level. Allow public declarations. */ | |
987 | assert (current_function_decl == NULL_TREE | |
988 | || DECL_CONTEXT (current_function_decl) == NULL_TREE); | |
989 | ||
990 | type = gfc_get_function_type (sym); | |
991 | fndecl = build_decl (FUNCTION_DECL, gfc_sym_identifier (sym), type); | |
992 | ||
993 | /* Perform name mangling if this is a top level or module procedure. */ | |
994 | if (current_function_decl == NULL_TREE) | |
995 | SET_DECL_ASSEMBLER_NAME (fndecl, gfc_sym_mangled_function_id (sym)); | |
996 | ||
997 | /* Figure out the return type of the declared function, and build a | |
998 | RESULT_DECL for it. If this is subroutine with alternate | |
999 | returns, build a RESULT_DECL for it. */ | |
1000 | attr = sym->attr; | |
1001 | ||
1002 | result_decl = NULL_TREE; | |
1003 | /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */ | |
1004 | if (attr.function) | |
1005 | { | |
1006 | if (gfc_return_by_reference (sym)) | |
1007 | type = void_type_node; | |
1008 | else | |
1009 | { | |
1010 | if (sym->result != sym) | |
1011 | result_decl = gfc_sym_identifier (sym->result); | |
1012 | ||
1013 | type = TREE_TYPE (TREE_TYPE (fndecl)); | |
1014 | } | |
1015 | } | |
1016 | else | |
1017 | { | |
1018 | /* Look for alternate return placeholders. */ | |
1019 | int has_alternate_returns = 0; | |
1020 | for (f = sym->formal; f; f = f->next) | |
1021 | { | |
1022 | if (f->sym == NULL) | |
1023 | { | |
1024 | has_alternate_returns = 1; | |
1025 | break; | |
1026 | } | |
1027 | } | |
1028 | ||
1029 | if (has_alternate_returns) | |
1030 | type = integer_type_node; | |
1031 | else | |
1032 | type = void_type_node; | |
1033 | } | |
1034 | ||
1035 | result_decl = build_decl (RESULT_DECL, result_decl, type); | |
1036 | DECL_CONTEXT (result_decl) = fndecl; | |
1037 | DECL_RESULT (fndecl) = result_decl; | |
1038 | ||
1039 | /* Don't call layout_decl for a RESULT_DECL. | |
1040 | layout_decl (result_decl, 0); */ | |
1041 | ||
1042 | /* If the return type is a pointer, avoid alias issues by setting | |
1043 | DECL_IS_MALLOC to nonzero. This means that the function should be | |
1044 | treated as if it were a malloc, meaning it returns a pointer that | |
1045 | is not an alias. */ | |
1046 | if (POINTER_TYPE_P (type)) | |
1047 | DECL_IS_MALLOC (fndecl) = 1; | |
1048 | ||
1049 | /* Set up all attributes for the function. */ | |
1050 | DECL_CONTEXT (fndecl) = current_function_decl; | |
1051 | DECL_EXTERNAL (fndecl) = 0; | |
1052 | ||
1053 | /* This specifies if a function is globaly addressable, ie. it is | |
472ca416 | 1054 | the opposite of declaring static in C. */ |
6de9cd9a DN |
1055 | if (DECL_CONTEXT (fndecl) == NULL_TREE || attr.external) |
1056 | TREE_PUBLIC (fndecl) = 1; | |
1057 | ||
1058 | /* TREE_STATIC means the function body is defined here. */ | |
1059 | if (!attr.external) | |
1060 | TREE_STATIC (fndecl) = 1; | |
1061 | ||
1062 | /* Set attributes for PURE functions. A call to PURE function in the | |
1063 | Fortran 95 sense is both pure and without side effects in the C | |
1064 | sense. */ | |
1065 | if (attr.pure || attr.elemental) | |
1066 | { | |
b7e6a6b3 TS |
1067 | /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments |
1068 | including a alternate return. In that case it can also be | |
1069 | marked as PURE. See also in gfc_get_extern_fucntion_decl(). */ | |
1070 | if (attr.function) | |
1071 | DECL_IS_PURE (fndecl) = 1; | |
6de9cd9a DN |
1072 | TREE_SIDE_EFFECTS (fndecl) = 0; |
1073 | } | |
1074 | ||
1075 | /* Layout the function declaration and put it in the binding level | |
1076 | of the current function. */ | |
1077 | if (!attr.external) | |
1078 | { | |
1079 | tree parm; | |
1080 | ||
1081 | pushdecl (fndecl); | |
1082 | /* Build formal argument list. Make sure that their TREE_CONTEXT is | |
1083 | the new FUNCTION_DECL node. */ | |
1084 | current_function_decl = fndecl; | |
1085 | arglist = NULL_TREE; | |
1086 | typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); | |
1087 | if (gfc_return_by_reference (sym)) | |
1088 | { | |
1089 | type = TREE_VALUE (typelist); | |
1090 | parm = build_decl (PARM_DECL, get_identifier ("__result"), type); | |
1091 | ||
1092 | DECL_CONTEXT (parm) = fndecl; | |
1093 | DECL_ARG_TYPE (parm) = type; | |
1094 | TREE_READONLY (parm) = 1; | |
1095 | gfc_finish_decl (parm, NULL_TREE); | |
1096 | ||
1097 | arglist = chainon (arglist, parm); | |
1098 | typelist = TREE_CHAIN (typelist); | |
1099 | ||
1100 | if (sym->ts.type == BT_CHARACTER) | |
1101 | { | |
1102 | gfc_allocate_lang_decl (parm); | |
1103 | ||
472ca416 | 1104 | /* Length of character result. */ |
6de9cd9a DN |
1105 | type = TREE_VALUE (typelist); |
1106 | assert (type == gfc_strlen_type_node); | |
1107 | ||
1108 | length = build_decl (PARM_DECL, | |
1109 | get_identifier (".__result"), | |
1110 | type); | |
1111 | if (!sym->ts.cl->length) | |
1112 | { | |
1113 | sym->ts.cl->backend_decl = length; | |
1114 | TREE_USED (length) = 1; | |
1115 | } | |
1116 | assert (TREE_CODE (length) == PARM_DECL); | |
1117 | arglist = chainon (arglist, length); | |
1118 | typelist = TREE_CHAIN (typelist); | |
1119 | DECL_CONTEXT (length) = fndecl; | |
1120 | DECL_ARG_TYPE (length) = type; | |
1121 | TREE_READONLY (length) = 1; | |
1122 | gfc_finish_decl (length, NULL_TREE); | |
1123 | } | |
1124 | } | |
1125 | ||
1126 | for (f = sym->formal; f; f = f->next) | |
1127 | { | |
1128 | if (f->sym != NULL) /* ignore alternate returns. */ | |
1129 | { | |
1130 | length = NULL_TREE; | |
1131 | ||
1132 | type = TREE_VALUE (typelist); | |
1133 | ||
1134 | /* Build a the argument declaration. */ | |
1135 | parm = build_decl (PARM_DECL, | |
1136 | gfc_sym_identifier (f->sym), type); | |
1137 | ||
1138 | /* Fill in arg stuff. */ | |
1139 | DECL_CONTEXT (parm) = fndecl; | |
1140 | DECL_ARG_TYPE (parm) = type; | |
1141 | DECL_ARG_TYPE_AS_WRITTEN (parm) = type; | |
1142 | /* All implementation args are read-only. */ | |
1143 | TREE_READONLY (parm) = 1; | |
1144 | ||
1145 | gfc_finish_decl (parm, NULL_TREE); | |
1146 | ||
1147 | f->sym->backend_decl = parm; | |
1148 | ||
1149 | arglist = chainon (arglist, parm); | |
1150 | typelist = TREE_CHAIN (typelist); | |
1151 | } | |
1152 | } | |
1153 | ||
1154 | /* Add the hidden string length parameters. */ | |
1155 | parm = arglist; | |
1156 | for (f = sym->formal; f; f = f->next) | |
1157 | { | |
1158 | char name[GFC_MAX_SYMBOL_LEN + 2]; | |
1159 | /* Ignore alternate returns. */ | |
1160 | if (f->sym == NULL) | |
1161 | continue; | |
1162 | ||
1163 | if (f->sym->ts.type != BT_CHARACTER) | |
1164 | continue; | |
1165 | ||
1166 | parm = f->sym->backend_decl; | |
1167 | type = TREE_VALUE (typelist); | |
1168 | assert (type == gfc_strlen_type_node); | |
1169 | ||
1170 | strcpy (&name[1], f->sym->name); | |
1171 | name[0] = '_'; | |
1172 | length = build_decl (PARM_DECL, get_identifier (name), type); | |
1173 | ||
1174 | arglist = chainon (arglist, length); | |
1175 | DECL_CONTEXT (length) = fndecl; | |
1176 | DECL_ARG_TYPE (length) = type; | |
1177 | TREE_READONLY (length) = 1; | |
1178 | gfc_finish_decl (length, NULL_TREE); | |
1179 | ||
1180 | /* TODO: Check string lengths when -fbounds-check. */ | |
1181 | ||
1182 | /* Use the passed value for assumed length variables. */ | |
1183 | if (!f->sym->ts.cl->length) | |
1184 | { | |
1185 | TREE_USED (length) = 1; | |
1186 | f->sym->ts.cl->backend_decl = length; | |
1187 | } | |
1188 | ||
1189 | parm = TREE_CHAIN (parm); | |
1190 | typelist = TREE_CHAIN (typelist); | |
1191 | } | |
1192 | ||
1193 | assert (TREE_VALUE (typelist) == void_type_node); | |
1194 | DECL_ARGUMENTS (fndecl) = arglist; | |
1195 | ||
1196 | /* Restore the old context. */ | |
1197 | current_function_decl = DECL_CONTEXT (fndecl); | |
1198 | } | |
1199 | sym->backend_decl = fndecl; | |
1200 | } | |
1201 | ||
1202 | ||
1203 | /* Return the decl used to hold the function return value. */ | |
1204 | ||
1205 | tree | |
1206 | gfc_get_fake_result_decl (gfc_symbol * sym) | |
1207 | { | |
1208 | tree decl; | |
1209 | tree length; | |
1210 | ||
1211 | char name[GFC_MAX_SYMBOL_LEN + 10]; | |
1212 | ||
1213 | if (current_fake_result_decl != NULL_TREE) | |
1214 | return current_fake_result_decl; | |
1215 | ||
1216 | /* Only when gfc_get_fake_result_decl is called by gfc_trans_return, | |
1217 | sym is NULL. */ | |
1218 | if (!sym) | |
1219 | return NULL_TREE; | |
1220 | ||
1221 | if (sym->ts.type == BT_CHARACTER | |
1222 | && !sym->ts.cl->backend_decl) | |
1223 | { | |
1224 | length = gfc_create_string_length (sym); | |
1225 | gfc_finish_var_decl (length, sym); | |
1226 | } | |
1227 | ||
1228 | if (gfc_return_by_reference (sym)) | |
1229 | { | |
1230 | decl = DECL_ARGUMENTS (sym->backend_decl); | |
1231 | ||
1232 | TREE_USED (decl) = 1; | |
1233 | if (sym->as) | |
1234 | decl = gfc_build_dummy_array_decl (sym, decl); | |
1235 | } | |
1236 | else | |
1237 | { | |
1238 | sprintf (name, "__result_%.20s", | |
1239 | IDENTIFIER_POINTER (DECL_NAME (current_function_decl))); | |
1240 | ||
1241 | decl = build_decl (VAR_DECL, get_identifier (name), | |
1242 | TREE_TYPE (TREE_TYPE (current_function_decl))); | |
1243 | ||
1244 | DECL_ARTIFICIAL (decl) = 1; | |
1245 | DECL_EXTERNAL (decl) = 0; | |
1246 | TREE_PUBLIC (decl) = 0; | |
1247 | TREE_USED (decl) = 1; | |
1248 | ||
1249 | layout_decl (decl, 0); | |
1250 | ||
1251 | gfc_add_decl_to_function (decl); | |
1252 | } | |
1253 | ||
1254 | current_fake_result_decl = decl; | |
1255 | ||
1256 | return decl; | |
1257 | } | |
1258 | ||
1259 | ||
1260 | /* Builds a function decl. The remaining parameters are the types of the | |
1261 | function arguments. Negative nargs indicates a varargs function. */ | |
1262 | ||
1263 | tree | |
1264 | gfc_build_library_function_decl (tree name, tree rettype, int nargs, ...) | |
1265 | { | |
1266 | tree arglist; | |
1267 | tree argtype; | |
1268 | tree fntype; | |
1269 | tree fndecl; | |
1270 | va_list p; | |
1271 | int n; | |
1272 | ||
1273 | /* Library functions must be declared with global scope. */ | |
1274 | assert (current_function_decl == NULL_TREE); | |
1275 | ||
1276 | va_start (p, nargs); | |
1277 | ||
1278 | ||
1279 | /* Create a list of the argument types. */ | |
1280 | for (arglist = NULL_TREE, n = abs (nargs); n > 0; n--) | |
1281 | { | |
1282 | argtype = va_arg (p, tree); | |
1283 | arglist = gfc_chainon_list (arglist, argtype); | |
1284 | } | |
1285 | ||
1286 | if (nargs >= 0) | |
1287 | { | |
1288 | /* Terminate the list. */ | |
1289 | arglist = gfc_chainon_list (arglist, void_type_node); | |
1290 | } | |
1291 | ||
1292 | /* Build the function type and decl. */ | |
1293 | fntype = build_function_type (rettype, arglist); | |
1294 | fndecl = build_decl (FUNCTION_DECL, name, fntype); | |
1295 | ||
1296 | /* Mark this decl as external. */ | |
1297 | DECL_EXTERNAL (fndecl) = 1; | |
1298 | TREE_PUBLIC (fndecl) = 1; | |
1299 | ||
1300 | va_end (p); | |
1301 | ||
1302 | pushdecl (fndecl); | |
1303 | ||
1304 | rest_of_decl_compilation (fndecl, NULL, 1, 0); | |
1305 | ||
1306 | return fndecl; | |
1307 | } | |
1308 | ||
1309 | static void | |
1310 | gfc_build_intrinsic_function_decls (void) | |
1311 | { | |
1312 | /* String functions. */ | |
1313 | gfor_fndecl_copy_string = | |
1314 | gfc_build_library_function_decl (get_identifier (PREFIX("copy_string")), | |
1315 | void_type_node, | |
1316 | 4, | |
1317 | gfc_strlen_type_node, pchar_type_node, | |
1318 | gfc_strlen_type_node, pchar_type_node); | |
1319 | ||
1320 | gfor_fndecl_compare_string = | |
1321 | gfc_build_library_function_decl (get_identifier (PREFIX("compare_string")), | |
1322 | gfc_int4_type_node, | |
1323 | 4, | |
1324 | gfc_strlen_type_node, pchar_type_node, | |
1325 | gfc_strlen_type_node, pchar_type_node); | |
1326 | ||
1327 | gfor_fndecl_concat_string = | |
1328 | gfc_build_library_function_decl (get_identifier (PREFIX("concat_string")), | |
1329 | void_type_node, | |
1330 | 6, | |
1331 | gfc_strlen_type_node, pchar_type_node, | |
1332 | gfc_strlen_type_node, pchar_type_node, | |
1333 | gfc_strlen_type_node, pchar_type_node); | |
1334 | ||
1335 | gfor_fndecl_string_len_trim = | |
1336 | gfc_build_library_function_decl (get_identifier (PREFIX("string_len_trim")), | |
1337 | gfc_int4_type_node, | |
1338 | 2, gfc_strlen_type_node, | |
1339 | pchar_type_node); | |
1340 | ||
1341 | gfor_fndecl_string_index = | |
1342 | gfc_build_library_function_decl (get_identifier (PREFIX("string_index")), | |
1343 | gfc_int4_type_node, | |
1344 | 5, gfc_strlen_type_node, pchar_type_node, | |
1345 | gfc_strlen_type_node, pchar_type_node, | |
1346 | gfc_logical4_type_node); | |
1347 | ||
1348 | gfor_fndecl_string_scan = | |
1349 | gfc_build_library_function_decl (get_identifier (PREFIX("string_scan")), | |
1350 | gfc_int4_type_node, | |
1351 | 5, gfc_strlen_type_node, pchar_type_node, | |
1352 | gfc_strlen_type_node, pchar_type_node, | |
1353 | gfc_logical4_type_node); | |
1354 | ||
1355 | gfor_fndecl_string_verify = | |
1356 | gfc_build_library_function_decl (get_identifier (PREFIX("string_verify")), | |
1357 | gfc_int4_type_node, | |
1358 | 5, gfc_strlen_type_node, pchar_type_node, | |
1359 | gfc_strlen_type_node, pchar_type_node, | |
1360 | gfc_logical4_type_node); | |
1361 | ||
1362 | gfor_fndecl_string_trim = | |
1363 | gfc_build_library_function_decl (get_identifier (PREFIX("string_trim")), | |
1364 | void_type_node, | |
1365 | 4, | |
1366 | build_pointer_type (gfc_strlen_type_node), | |
1367 | ppvoid_type_node, | |
1368 | gfc_strlen_type_node, | |
1369 | pchar_type_node); | |
1370 | ||
1371 | gfor_fndecl_string_repeat = | |
1372 | gfc_build_library_function_decl (get_identifier (PREFIX("string_repeat")), | |
1373 | void_type_node, | |
1374 | 4, | |
1375 | pchar_type_node, | |
1376 | gfc_strlen_type_node, | |
1377 | pchar_type_node, | |
1378 | gfc_int4_type_node); | |
1379 | ||
1380 | gfor_fndecl_adjustl = | |
1381 | gfc_build_library_function_decl (get_identifier (PREFIX("adjustl")), | |
1382 | void_type_node, | |
1383 | 3, | |
1384 | pchar_type_node, | |
1385 | gfc_strlen_type_node, pchar_type_node); | |
1386 | ||
1387 | gfor_fndecl_adjustr = | |
1388 | gfc_build_library_function_decl (get_identifier (PREFIX("adjustr")), | |
1389 | void_type_node, | |
1390 | 3, | |
1391 | pchar_type_node, | |
1392 | gfc_strlen_type_node, pchar_type_node); | |
1393 | ||
1394 | gfor_fndecl_si_kind = | |
1395 | gfc_build_library_function_decl (get_identifier ("selected_int_kind"), | |
1396 | gfc_int4_type_node, | |
1397 | 1, | |
1398 | pvoid_type_node); | |
1399 | ||
1400 | gfor_fndecl_sr_kind = | |
1401 | gfc_build_library_function_decl (get_identifier ("selected_real_kind"), | |
1402 | gfc_int4_type_node, | |
1403 | 2, pvoid_type_node, | |
1404 | pvoid_type_node); | |
1405 | ||
1406 | ||
1407 | /* Power functions. */ | |
5b200ac2 FW |
1408 | { |
1409 | tree type; | |
1410 | tree itype; | |
1411 | int kind; | |
1412 | int ikind; | |
1413 | static int kinds[2] = {4, 8}; | |
1414 | char name[PREFIX_LEN + 10]; /* _gfortran_pow_?n_?n */ | |
1415 | ||
1416 | for (ikind=0; ikind < 2; ikind++) | |
1417 | { | |
1418 | itype = gfc_get_int_type (kinds[ikind]); | |
1419 | for (kind = 0; kind < 2; kind ++) | |
1420 | { | |
1421 | type = gfc_get_int_type (kinds[kind]); | |
1422 | sprintf(name, PREFIX("pow_i%d_i%d"), kinds[kind], kinds[ikind]); | |
1423 | gfor_fndecl_math_powi[kind][ikind].integer = | |
1424 | gfc_build_library_function_decl (get_identifier (name), | |
1425 | type, 2, type, itype); | |
1426 | ||
1427 | type = gfc_get_real_type (kinds[kind]); | |
1428 | sprintf(name, PREFIX("pow_r%d_i%d"), kinds[kind], kinds[ikind]); | |
1429 | gfor_fndecl_math_powi[kind][ikind].real = | |
1430 | gfc_build_library_function_decl (get_identifier (name), | |
1431 | type, 2, type, itype); | |
1432 | ||
1433 | type = gfc_get_complex_type (kinds[kind]); | |
1434 | sprintf(name, PREFIX("pow_c%d_i%d"), kinds[kind], kinds[ikind]); | |
1435 | gfor_fndecl_math_powi[kind][ikind].cmplx = | |
1436 | gfc_build_library_function_decl (get_identifier (name), | |
1437 | type, 2, type, itype); | |
1438 | } | |
1439 | } | |
1440 | } | |
1441 | ||
6de9cd9a DN |
1442 | gfor_fndecl_math_cpowf = |
1443 | gfc_build_library_function_decl (get_identifier ("cpowf"), | |
1444 | gfc_complex4_type_node, | |
1445 | 1, gfc_complex4_type_node); | |
1446 | gfor_fndecl_math_cpow = | |
1447 | gfc_build_library_function_decl (get_identifier ("cpow"), | |
1448 | gfc_complex8_type_node, | |
1449 | 1, gfc_complex8_type_node); | |
1450 | gfor_fndecl_math_cabsf = | |
1451 | gfc_build_library_function_decl (get_identifier ("cabsf"), | |
1452 | gfc_real4_type_node, | |
1453 | 1, gfc_complex4_type_node); | |
1454 | gfor_fndecl_math_cabs = | |
1455 | gfc_build_library_function_decl (get_identifier ("cabs"), | |
1456 | gfc_real8_type_node, | |
1457 | 1, gfc_complex8_type_node); | |
1458 | gfor_fndecl_math_sign4 = | |
1459 | gfc_build_library_function_decl (get_identifier ("copysignf"), | |
1460 | gfc_real4_type_node, | |
1461 | 1, gfc_real4_type_node); | |
1462 | gfor_fndecl_math_sign8 = | |
1463 | gfc_build_library_function_decl (get_identifier ("copysign"), | |
1464 | gfc_real8_type_node, | |
1465 | 1, gfc_real8_type_node); | |
1466 | gfor_fndecl_math_ishftc4 = | |
1467 | gfc_build_library_function_decl (get_identifier (PREFIX("ishftc4")), | |
1468 | gfc_int4_type_node, | |
1469 | 3, gfc_int4_type_node, | |
1470 | gfc_int4_type_node, gfc_int4_type_node); | |
1471 | gfor_fndecl_math_ishftc8 = | |
1472 | gfc_build_library_function_decl (get_identifier (PREFIX("ishftc8")), | |
1473 | gfc_int8_type_node, | |
1474 | 3, gfc_int8_type_node, | |
1475 | gfc_int8_type_node, gfc_int8_type_node); | |
1476 | gfor_fndecl_math_exponent4 = | |
1477 | gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r4")), | |
1478 | gfc_int4_type_node, | |
1479 | 1, gfc_real4_type_node); | |
1480 | gfor_fndecl_math_exponent8 = | |
1481 | gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r8")), | |
1482 | gfc_int4_type_node, | |
1483 | 1, gfc_real8_type_node); | |
1484 | ||
1485 | /* Other functions. */ | |
1486 | gfor_fndecl_size0 = | |
1487 | gfc_build_library_function_decl (get_identifier (PREFIX("size0")), | |
1488 | gfc_array_index_type, | |
1489 | 1, pvoid_type_node); | |
1490 | gfor_fndecl_size1 = | |
1491 | gfc_build_library_function_decl (get_identifier (PREFIX("size1")), | |
1492 | gfc_array_index_type, | |
1493 | 2, pvoid_type_node, | |
1494 | gfc_array_index_type); | |
1495 | } | |
1496 | ||
1497 | ||
1498 | /* Make prototypes for runtime library functions. */ | |
1499 | ||
1500 | void | |
1501 | gfc_build_builtin_function_decls (void) | |
1502 | { | |
1503 | gfor_fndecl_internal_malloc = | |
1504 | gfc_build_library_function_decl (get_identifier (PREFIX("internal_malloc")), | |
1505 | pvoid_type_node, 1, gfc_int4_type_node); | |
1506 | ||
1507 | gfor_fndecl_internal_malloc64 = | |
1508 | gfc_build_library_function_decl (get_identifier | |
1509 | (PREFIX("internal_malloc64")), | |
1510 | pvoid_type_node, 1, gfc_int8_type_node); | |
1511 | ||
1512 | gfor_fndecl_internal_free = | |
1513 | gfc_build_library_function_decl (get_identifier (PREFIX("internal_free")), | |
1514 | void_type_node, 1, pvoid_type_node); | |
1515 | ||
1516 | gfor_fndecl_allocate = | |
1517 | gfc_build_library_function_decl (get_identifier (PREFIX("allocate")), | |
1518 | void_type_node, 2, ppvoid_type_node, | |
1519 | gfc_int4_type_node); | |
1520 | ||
1521 | gfor_fndecl_allocate64 = | |
1522 | gfc_build_library_function_decl (get_identifier (PREFIX("allocate64")), | |
1523 | void_type_node, 2, ppvoid_type_node, | |
1524 | gfc_int8_type_node); | |
1525 | ||
1526 | gfor_fndecl_deallocate = | |
1527 | gfc_build_library_function_decl (get_identifier (PREFIX("deallocate")), | |
1528 | void_type_node, 1, ppvoid_type_node); | |
1529 | ||
1530 | gfor_fndecl_stop_numeric = | |
1531 | gfc_build_library_function_decl (get_identifier (PREFIX("stop_numeric")), | |
1532 | void_type_node, 1, gfc_int4_type_node); | |
1533 | ||
1534 | gfor_fndecl_stop_string = | |
1535 | gfc_build_library_function_decl (get_identifier (PREFIX("stop_string")), | |
1536 | void_type_node, 2, pchar_type_node, | |
1537 | gfc_int4_type_node); | |
1538 | ||
1539 | gfor_fndecl_pause_numeric = | |
1540 | gfc_build_library_function_decl (get_identifier (PREFIX("pause_numeric")), | |
1541 | void_type_node, 1, gfc_int4_type_node); | |
1542 | ||
1543 | gfor_fndecl_pause_string = | |
1544 | gfc_build_library_function_decl (get_identifier (PREFIX("pause_string")), | |
1545 | void_type_node, 2, pchar_type_node, | |
1546 | gfc_int4_type_node); | |
1547 | ||
1548 | gfor_fndecl_select_string = | |
1549 | gfc_build_library_function_decl (get_identifier (PREFIX("select_string")), | |
1550 | pvoid_type_node, 0); | |
1551 | ||
1552 | gfor_fndecl_runtime_error = | |
1553 | gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error")), | |
1554 | void_type_node, | |
1555 | 3, | |
1556 | pchar_type_node, pchar_type_node, | |
1557 | gfc_int4_type_node); | |
1558 | ||
1559 | gfor_fndecl_in_pack = gfc_build_library_function_decl ( | |
1560 | get_identifier (PREFIX("internal_pack")), | |
1561 | pvoid_type_node, 1, pvoid_type_node); | |
1562 | ||
1563 | gfor_fndecl_in_unpack = gfc_build_library_function_decl ( | |
1564 | get_identifier (PREFIX("internal_unpack")), | |
1565 | pvoid_type_node, 1, pvoid_type_node); | |
1566 | ||
1567 | gfor_fndecl_associated = | |
1568 | gfc_build_library_function_decl ( | |
1569 | get_identifier (PREFIX("associated")), | |
1570 | gfc_logical4_type_node, | |
1571 | 2, | |
1572 | ppvoid_type_node, | |
1573 | ppvoid_type_node); | |
1574 | ||
1575 | gfc_build_intrinsic_function_decls (); | |
1576 | gfc_build_intrinsic_lib_fndecls (); | |
1577 | gfc_build_io_library_fndecls (); | |
1578 | } | |
1579 | ||
1580 | ||
1581 | /* Exaluate the length of dummy character variables. */ | |
1582 | ||
1583 | static tree | |
1584 | gfc_trans_dummy_character (gfc_charlen * cl, tree fnbody) | |
1585 | { | |
1586 | stmtblock_t body; | |
1587 | ||
1588 | gfc_finish_decl (cl->backend_decl, NULL_TREE); | |
1589 | ||
1590 | gfc_start_block (&body); | |
1591 | ||
1592 | /* Evaluate the string length expression. */ | |
1593 | gfc_trans_init_string_length (cl, &body); | |
1594 | ||
1595 | gfc_add_expr_to_block (&body, fnbody); | |
1596 | return gfc_finish_block (&body); | |
1597 | } | |
1598 | ||
1599 | ||
1600 | /* Allocate and cleanup an automatic character variable. */ | |
1601 | ||
1602 | static tree | |
1603 | gfc_trans_auto_character_variable (gfc_symbol * sym, tree fnbody) | |
1604 | { | |
1605 | stmtblock_t body; | |
1606 | tree decl; | |
1607 | tree args; | |
1608 | tree tmp; | |
1609 | ||
1610 | assert (sym->backend_decl); | |
1611 | assert (sym->ts.cl && sym->ts.cl->length); | |
1612 | ||
1613 | gfc_start_block (&body); | |
1614 | ||
1615 | /* Evaluate the string length expression. */ | |
1616 | gfc_trans_init_string_length (sym->ts.cl, &body); | |
1617 | ||
1618 | decl = sym->backend_decl; | |
1619 | ||
1620 | DECL_DEFER_OUTPUT (decl) = 1; | |
1621 | ||
1622 | /* Generate code to allocate the automatic variable. It will be freed | |
1623 | automatically. */ | |
1624 | tmp = gfc_build_addr_expr (NULL, decl); | |
1625 | args = gfc_chainon_list (NULL_TREE, tmp); | |
1626 | args = gfc_chainon_list (args, sym->ts.cl->backend_decl); | |
1627 | tmp = gfc_build_function_call (built_in_decls[BUILT_IN_STACK_ALLOC], args); | |
1628 | gfc_add_expr_to_block (&body, tmp); | |
1629 | gfc_add_expr_to_block (&body, fnbody); | |
1630 | return gfc_finish_block (&body); | |
1631 | } | |
1632 | ||
1633 | ||
1634 | /* Generate function entry and exit code, and add it to the function body. | |
1635 | This includes: | |
1636 | Allocation and initialisation of array variables. | |
1637 | Allocation of character string variables. | |
1638 | Initialization and possibly repacking of dummy arrays. */ | |
1639 | ||
1640 | static tree | |
1641 | gfc_trans_deferred_vars (gfc_symbol * proc_sym, tree fnbody) | |
1642 | { | |
1643 | locus loc; | |
1644 | gfc_symbol *sym; | |
1645 | ||
1646 | /* Deal with implicit return variables. Explicit return variables will | |
1647 | already have been added. */ | |
1648 | if (gfc_return_by_reference (proc_sym) && proc_sym->result == proc_sym) | |
1649 | { | |
1650 | if (!current_fake_result_decl) | |
1651 | { | |
1652 | warning ("Function does not return a value"); | |
1653 | return fnbody; | |
1654 | } | |
1655 | ||
1656 | if (proc_sym->as) | |
1657 | { | |
1658 | fnbody = gfc_trans_dummy_array_bias (proc_sym, | |
1659 | current_fake_result_decl, | |
1660 | fnbody); | |
1661 | } | |
1662 | else if (proc_sym->ts.type == BT_CHARACTER) | |
1663 | { | |
1664 | if (TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL) | |
1665 | fnbody = gfc_trans_dummy_character (proc_sym->ts.cl, fnbody); | |
1666 | } | |
1667 | else | |
1668 | gfc_todo_error ("Deferred non-array return by reference"); | |
1669 | } | |
1670 | ||
1671 | for (sym = proc_sym->tlink; sym != proc_sym; sym = sym->tlink) | |
1672 | { | |
1673 | if (sym->attr.dimension) | |
1674 | { | |
1675 | switch (sym->as->type) | |
1676 | { | |
1677 | case AS_EXPLICIT: | |
1678 | if (sym->attr.dummy || sym->attr.result) | |
1679 | fnbody = | |
1680 | gfc_trans_dummy_array_bias (sym, sym->backend_decl, fnbody); | |
1681 | else if (sym->attr.pointer || sym->attr.allocatable) | |
1682 | { | |
1683 | if (TREE_STATIC (sym->backend_decl)) | |
1684 | gfc_trans_static_array_pointer (sym); | |
1685 | else | |
1686 | fnbody = gfc_trans_deferred_array (sym, fnbody); | |
1687 | } | |
1688 | else | |
1689 | { | |
1690 | gfc_get_backend_locus (&loc); | |
1691 | gfc_set_backend_locus (&sym->declared_at); | |
1692 | fnbody = gfc_trans_auto_array_allocation (sym->backend_decl, | |
1693 | sym, fnbody); | |
1694 | gfc_set_backend_locus (&loc); | |
1695 | } | |
1696 | break; | |
1697 | ||
1698 | case AS_ASSUMED_SIZE: | |
1699 | /* Must be a dummy parameter. */ | |
1700 | assert (sym->attr.dummy); | |
1701 | ||
1702 | /* We should always pass assumed size arrays the g77 way. */ | |
1703 | assert (TREE_CODE (sym->backend_decl) == PARM_DECL); | |
1704 | fnbody = gfc_trans_g77_array (sym, fnbody); | |
1705 | break; | |
1706 | ||
1707 | case AS_ASSUMED_SHAPE: | |
1708 | /* Must be a dummy parameter. */ | |
1709 | assert (sym->attr.dummy); | |
1710 | ||
1711 | fnbody = gfc_trans_dummy_array_bias (sym, sym->backend_decl, | |
1712 | fnbody); | |
1713 | break; | |
1714 | ||
1715 | case AS_DEFERRED: | |
1716 | fnbody = gfc_trans_deferred_array (sym, fnbody); | |
1717 | break; | |
1718 | ||
1719 | default: | |
1720 | abort (); | |
1721 | } | |
1722 | } | |
1723 | else if (sym->ts.type == BT_CHARACTER) | |
1724 | { | |
1725 | gfc_get_backend_locus (&loc); | |
1726 | gfc_set_backend_locus (&sym->declared_at); | |
1727 | if (sym->attr.dummy || sym->attr.result) | |
1728 | fnbody = gfc_trans_dummy_character (sym->ts.cl, fnbody); | |
1729 | else | |
1730 | fnbody = gfc_trans_auto_character_variable (sym, fnbody); | |
1731 | gfc_set_backend_locus (&loc); | |
1732 | } | |
1733 | else | |
1734 | abort (); | |
1735 | } | |
1736 | ||
1737 | return fnbody; | |
1738 | } | |
1739 | ||
1740 | ||
1741 | /* Output an initialized decl for a module variable. */ | |
1742 | ||
1743 | static void | |
1744 | gfc_create_module_variable (gfc_symbol * sym) | |
1745 | { | |
1746 | tree decl; | |
1747 | gfc_se se; | |
1748 | ||
1749 | /* Only output symbols from this module. */ | |
1750 | if (sym->ns != module_namespace) | |
1751 | { | |
1752 | /* I don't think this should ever happen. */ | |
1753 | internal_error ("module symbol %s in wrong namespace", sym->name); | |
1754 | } | |
1755 | ||
1756 | /* Don't ouptut symbols from common blocks. */ | |
1757 | if (sym->attr.common) | |
1758 | return; | |
1759 | ||
1760 | /* Only output variables and array valued parametes. */ | |
1761 | if (sym->attr.flavor != FL_VARIABLE | |
1762 | && (sym->attr.flavor != FL_PARAMETER || sym->attr.dimension == 0)) | |
1763 | return; | |
1764 | ||
1765 | /* Don't generate variables from other modules. */ | |
1766 | if (sym->attr.use_assoc) | |
1767 | return; | |
1768 | ||
1769 | if (sym->backend_decl) | |
1770 | internal_error ("backend decl for module variable %s already exists", | |
1771 | sym->name); | |
1772 | ||
1773 | /* We always want module variables to be created. */ | |
1774 | sym->attr.referenced = 1; | |
1775 | /* Create the decl. */ | |
1776 | decl = gfc_get_symbol_decl (sym); | |
1777 | ||
1778 | /* We want to allocate storage for this variable. */ | |
1779 | TREE_STATIC (decl) = 1; | |
1780 | ||
1781 | if (sym->attr.dimension) | |
1782 | { | |
1783 | assert (sym->attr.pointer || sym->attr.allocatable | |
1784 | || GFC_ARRAY_TYPE_P (TREE_TYPE (sym->backend_decl))); | |
1785 | if (sym->attr.pointer || sym->attr.allocatable) | |
1786 | gfc_trans_static_array_pointer (sym); | |
1787 | else | |
1788 | gfc_trans_auto_array_allocation (sym->backend_decl, sym, NULL_TREE); | |
1789 | } | |
1790 | else if (sym->ts.type == BT_DERIVED) | |
1791 | { | |
1792 | if (sym->value) | |
1793 | gfc_todo_error ("Initialization of derived type module variables"); | |
1794 | } | |
1795 | else | |
1796 | { | |
1797 | if (sym->value) | |
1798 | { | |
1799 | gfc_init_se (&se, NULL); | |
1800 | gfc_conv_constant (&se, sym->value); | |
1801 | DECL_INITIAL (decl) = se.expr; | |
1802 | } | |
1803 | } | |
1804 | ||
1805 | /* Create the variable. */ | |
1806 | pushdecl (decl); | |
1807 | rest_of_decl_compilation (decl, NULL, 1, 0); | |
1808 | ||
1809 | /* Also add length of strings. */ | |
1810 | if (sym->ts.type == BT_CHARACTER) | |
1811 | { | |
1812 | tree length; | |
1813 | ||
1814 | length = sym->ts.cl->backend_decl; | |
1815 | if (!INTEGER_CST_P (length)) | |
1816 | { | |
1817 | pushdecl (length); | |
1818 | rest_of_decl_compilation (length, NULL, 1, 0); | |
1819 | } | |
1820 | } | |
1821 | } | |
1822 | ||
1823 | ||
1824 | /* Generate all the required code for module variables. */ | |
1825 | ||
1826 | void | |
1827 | gfc_generate_module_vars (gfc_namespace * ns) | |
1828 | { | |
1829 | module_namespace = ns; | |
1830 | ||
472ca416 | 1831 | /* Check if the frontend left the namespace in a reasonable state. */ |
6de9cd9a DN |
1832 | assert (ns->proc_name && !ns->proc_name->tlink); |
1833 | ||
472ca416 | 1834 | /* Create decls for all the module variables. */ |
6de9cd9a DN |
1835 | gfc_traverse_ns (ns, gfc_create_module_variable); |
1836 | } | |
1837 | ||
1838 | static void | |
1839 | gfc_generate_contained_functions (gfc_namespace * parent) | |
1840 | { | |
1841 | gfc_namespace *ns; | |
1842 | ||
1843 | /* We create all the prototypes before generating any code. */ | |
1844 | for (ns = parent->contained; ns; ns = ns->sibling) | |
1845 | { | |
1846 | /* Skip namespaces from used modules. */ | |
1847 | if (ns->parent != parent) | |
1848 | continue; | |
1849 | ||
1850 | gfc_build_function_decl (ns->proc_name); | |
1851 | } | |
1852 | ||
1853 | for (ns = parent->contained; ns; ns = ns->sibling) | |
1854 | { | |
1855 | /* Skip namespaces from used modules. */ | |
1856 | if (ns->parent != parent) | |
1857 | continue; | |
1858 | ||
1859 | gfc_generate_function_code (ns); | |
1860 | } | |
1861 | } | |
1862 | ||
1863 | ||
1864 | /* Generate decls for all local variables. We do this to ensure correct | |
1865 | handling of expressions which only appear in the specification of | |
1866 | other functions. */ | |
1867 | ||
1868 | static void | |
1869 | generate_local_decl (gfc_symbol * sym) | |
1870 | { | |
1871 | if (sym->attr.flavor == FL_VARIABLE) | |
1872 | { | |
1873 | /* TODO: The frontend sometimes creates symbols for things which don't | |
1874 | actually exist. E.g. common block names and the names of formal | |
1875 | arguments. The latter are created while attempting to parse | |
1876 | the argument list as a substring reference. | |
1877 | ||
1878 | The proper fix is to avoid adding these symbols in the first place. | |
1879 | For now we hack round it by ignoring anything with an unknown type. | |
1880 | */ | |
1881 | if (sym->ts.type == BT_UNKNOWN) | |
1882 | return; | |
1883 | ||
1884 | if (sym->attr.referenced) | |
1885 | gfc_get_symbol_decl (sym); | |
1886 | else if (sym->attr.dummy) | |
1887 | { | |
1888 | if (warn_unused_parameter) | |
1889 | warning ("unused parameter `%s'", sym->name); | |
1890 | } | |
ce8fc97b | 1891 | /* warn for unused variables, but not if they're inside a common |
472ca416 | 1892 | block. */ |
ce8fc97b | 1893 | else if (warn_unused_variable && !sym->attr.in_common) |
6de9cd9a DN |
1894 | warning ("unused variable `%s'", sym->name); |
1895 | } | |
1896 | } | |
1897 | ||
1898 | static void | |
1899 | generate_local_vars (gfc_namespace * ns) | |
1900 | { | |
1901 | gfc_traverse_ns (ns, generate_local_decl); | |
1902 | } | |
1903 | ||
1904 | ||
1905 | /* Finalize DECL and all nested functions with cgraph. */ | |
1906 | ||
1907 | static void | |
1908 | gfc_finalize (tree decl) | |
1909 | { | |
1910 | struct cgraph_node *cgn; | |
1911 | ||
1912 | cgn = cgraph_node (decl); | |
1913 | for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested) | |
1914 | gfc_finalize (cgn->decl); | |
1915 | ||
1916 | cgraph_finalize_function (decl, false); | |
1917 | } | |
1918 | ||
1919 | /* Generate code for a function. */ | |
1920 | ||
1921 | void | |
1922 | gfc_generate_function_code (gfc_namespace * ns) | |
1923 | { | |
1924 | tree fndecl; | |
1925 | tree old_context; | |
1926 | tree decl; | |
1927 | tree tmp; | |
1928 | stmtblock_t block; | |
1929 | stmtblock_t body; | |
1930 | tree result; | |
1931 | gfc_symbol *sym; | |
1932 | ||
1933 | sym = ns->proc_name; | |
1934 | /* Check that the frontend isn't still using this. */ | |
1935 | assert (sym->tlink == NULL); | |
1936 | ||
1937 | sym->tlink = sym; | |
1938 | ||
1939 | /* Create the declaration for functions with global scope. */ | |
1940 | if (!sym->backend_decl) | |
1941 | gfc_build_function_decl (ns->proc_name); | |
1942 | ||
1943 | fndecl = sym->backend_decl; | |
1944 | old_context = current_function_decl; | |
1945 | ||
1946 | if (old_context) | |
1947 | { | |
1948 | push_function_context (); | |
1949 | saved_parent_function_decls = saved_function_decls; | |
1950 | saved_function_decls = NULL_TREE; | |
1951 | } | |
1952 | ||
1953 | /* let GCC know the current scope is this function */ | |
1954 | current_function_decl = fndecl; | |
1955 | ||
1956 | /* print function name on the console at compile time | |
1957 | (unless this feature was switched of by command line option "-quiet" */ | |
1958 | announce_function (fndecl); | |
1959 | ||
1960 | if (DECL_CONTEXT (fndecl) == NULL_TREE) | |
1961 | { | |
1962 | /* create RTL for function declaration */ | |
1963 | rest_of_decl_compilation (fndecl, NULL, 1, 0); | |
1964 | } | |
1965 | ||
1966 | /* create RTL for function definition */ | |
1967 | make_decl_rtl (fndecl, NULL); | |
1968 | ||
1969 | /* Set the line and filename. sym->decalred_at seems to point to the last | |
1970 | statement for subroutines, but it'll do for now. */ | |
1971 | gfc_set_backend_locus (&sym->declared_at); | |
1972 | ||
1973 | /* line and file should not be 0 */ | |
1974 | init_function_start (fndecl); | |
1975 | ||
1976 | /* We're in function-at-a-time mode. */ | |
1977 | cfun->x_whole_function_mode_p = 1; | |
1978 | ||
1979 | /* Even though we're inside a function body, we still don't want to | |
1980 | call expand_expr to calculate the size of a variable-sized array. | |
1981 | We haven't necessarily assigned RTL to all variables yet, so it's | |
1982 | not safe to try to expand expressions involving them. */ | |
1983 | immediate_size_expand = 0; | |
1984 | cfun->x_dont_save_pending_sizes_p = 1; | |
1985 | ||
1986 | /* Will be created as needed. */ | |
1987 | current_fake_result_decl = NULL_TREE; | |
1988 | ||
1989 | /* function.c requires a push at the start of the function */ | |
1990 | pushlevel (0); | |
1991 | ||
1992 | gfc_start_block (&block); | |
1993 | ||
1994 | gfc_generate_contained_functions (ns); | |
1995 | ||
1996 | /* Translate COMMON blocks. */ | |
1997 | gfc_trans_common (ns); | |
1998 | ||
1999 | generate_local_vars (ns); | |
2000 | ||
2001 | current_function_return_label = NULL; | |
2002 | ||
2003 | /* Now generate the code for the body of this function. */ | |
2004 | gfc_init_block (&body); | |
2005 | ||
2006 | if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node | |
2007 | && sym->attr.subroutine) | |
2008 | { | |
2009 | tree alternate_return; | |
2010 | alternate_return = gfc_get_fake_result_decl (sym); | |
2011 | gfc_add_modify_expr (&body, alternate_return, integer_zero_node); | |
2012 | } | |
2013 | ||
2014 | tmp = gfc_trans_code (ns->code); | |
2015 | gfc_add_expr_to_block (&body, tmp); | |
2016 | ||
2017 | /* Add a return label if needed. */ | |
2018 | if (current_function_return_label) | |
2019 | { | |
2020 | tmp = build1_v (LABEL_EXPR, current_function_return_label); | |
2021 | gfc_add_expr_to_block (&body, tmp); | |
2022 | } | |
2023 | ||
2024 | tmp = gfc_finish_block (&body); | |
2025 | /* Add code to create and cleanup arrays. */ | |
2026 | tmp = gfc_trans_deferred_vars (sym, tmp); | |
2027 | gfc_add_expr_to_block (&block, tmp); | |
2028 | ||
2029 | if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node) | |
2030 | { | |
2031 | if (sym->attr.subroutine ||sym == sym->result) | |
2032 | { | |
2033 | result = current_fake_result_decl; | |
2034 | current_fake_result_decl = NULL_TREE; | |
2035 | } | |
2036 | else | |
2037 | result = sym->result->backend_decl; | |
2038 | ||
2039 | if (result == NULL_TREE) | |
2040 | warning ("Function return value not set"); | |
2041 | else | |
2042 | { | |
2043 | /* Set the return value to the the dummy result variable. */ | |
2044 | tmp = build (MODIFY_EXPR, TREE_TYPE (result), | |
2045 | DECL_RESULT (fndecl), result); | |
2046 | tmp = build_v (RETURN_EXPR, tmp); | |
2047 | gfc_add_expr_to_block (&block, tmp); | |
2048 | } | |
2049 | } | |
2050 | ||
2051 | /* Add all the decls we created during processing. */ | |
2052 | decl = saved_function_decls; | |
2053 | while (decl) | |
2054 | { | |
2055 | tree next; | |
2056 | ||
2057 | next = TREE_CHAIN (decl); | |
2058 | TREE_CHAIN (decl) = NULL_TREE; | |
2059 | pushdecl (decl); | |
2060 | decl = next; | |
2061 | } | |
2062 | saved_function_decls = NULL_TREE; | |
2063 | ||
2064 | DECL_SAVED_TREE (fndecl) = gfc_finish_block (&block); | |
2065 | ||
2066 | /* Finish off this function and send it for code generation. */ | |
2067 | poplevel (1, 0, 1); | |
2068 | BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; | |
2069 | ||
2070 | /* Output the GENERIC tree. */ | |
2071 | dump_function (TDI_original, fndecl); | |
2072 | ||
2073 | /* Store the end of the function, so that we get good line number | |
2074 | info for the epilogue. */ | |
2075 | cfun->function_end_locus = input_location; | |
2076 | ||
2077 | /* We're leaving the context of this function, so zap cfun. | |
2078 | It's still in DECL_STRUCT_FUNCTION, and we'll restore it in | |
2079 | tree_rest_of_compilation. */ | |
2080 | cfun = NULL; | |
2081 | ||
2082 | if (old_context) | |
2083 | { | |
2084 | pop_function_context (); | |
2085 | saved_function_decls = saved_parent_function_decls; | |
2086 | } | |
2087 | current_function_decl = old_context; | |
2088 | ||
2089 | if (decl_function_context (fndecl)) | |
2090 | { | |
2091 | /* Register this function with cgraph just far enough to get it | |
2092 | added to our parent's nested function list. */ | |
2093 | (void) cgraph_node (fndecl); | |
2094 | ||
2095 | /* Lowering nested functions requires gimple input. */ | |
2096 | gimplify_function_tree (fndecl); | |
2097 | } | |
2098 | else | |
2099 | { | |
2100 | if (cgraph_node (fndecl)->nested) | |
2101 | { | |
2102 | gimplify_function_tree (fndecl); | |
2103 | lower_nested_functions (fndecl); | |
2104 | } | |
2105 | gfc_finalize (fndecl); | |
2106 | } | |
2107 | } | |
2108 | ||
2109 | ||
2110 | void | |
2111 | gfc_generate_constructors (void) | |
2112 | { | |
2113 | if (gfc_static_ctors != NULL_TREE) | |
2114 | abort (); | |
2115 | #if 0 | |
2116 | tree fnname; | |
2117 | tree type; | |
2118 | tree fndecl; | |
2119 | tree decl; | |
2120 | tree tmp; | |
2121 | ||
2122 | if (gfc_static_ctors == NULL_TREE) | |
2123 | return; | |
2124 | ||
2125 | fnname = get_file_function_name ('I'); | |
2126 | type = build_function_type (void_type_node, | |
2127 | gfc_chainon_list (NULL_TREE, void_type_node)); | |
2128 | ||
2129 | fndecl = build_decl (FUNCTION_DECL, fnname, type); | |
2130 | TREE_PUBLIC (fndecl) = 1; | |
2131 | ||
2132 | decl = build_decl (RESULT_DECL, NULL_TREE, void_type_node); | |
2133 | DECL_CONTEXT (decl) = fndecl; | |
2134 | DECL_RESULT (fndecl) = decl; | |
2135 | ||
2136 | pushdecl (fndecl); | |
2137 | ||
2138 | current_function_decl = fndecl; | |
2139 | ||
2140 | rest_of_decl_compilation (fndecl, NULL, 1, 0); | |
2141 | ||
2142 | make_decl_rtl (fndecl, NULL); | |
2143 | ||
2144 | init_function_start (fndecl, input_filename, input_line); | |
2145 | ||
2146 | cfun->x_whole_function_mode_p = 1; | |
2147 | ||
2148 | immediate_size_expand = 0; | |
2149 | ||
2150 | pushlevel (0); | |
2151 | ||
2152 | for (; gfc_static_ctors; gfc_static_ctors = TREE_CHAIN (gfc_static_ctors)) | |
2153 | { | |
2154 | tmp = | |
2155 | gfc_build_function_call (TREE_VALUE (gfc_static_ctors), NULL_TREE); | |
2156 | DECL_SAVED_TREE (fndecl) = build_stmt (EXPR_STMT, tmp); | |
2157 | } | |
2158 | ||
2159 | poplevel (1, 0, 1); | |
2160 | ||
2161 | BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; | |
2162 | ||
2163 | free_after_parsing (cfun); | |
2164 | free_after_compilation (cfun); | |
2165 | ||
2166 | tree_rest_of_compilation (fndecl, 0); | |
2167 | ||
2168 | current_function_decl = NULL_TREE; | |
2169 | #endif | |
2170 | } | |
2171 | ||
2172 | #include "gt-fortran-trans-decl.h" |