Daily bump.
[official-gcc.git] / gcc / ada / utils.c
blob58f0b68c4fca6c766c585711061e1e0c672dead3
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * U T I L S *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2008, Free Software Foundation, Inc. *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License along with GCC; see the file COPYING3. If not see *
19 * <http://www.gnu.org/licenses/>. *
20 * *
21 * GNAT was originally developed by the GNAT team at New York University. *
22 * Extensive contributions were provided by Ada Core Technologies Inc. *
23 * *
24 ****************************************************************************/
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "flags.h"
32 #include "defaults.h"
33 #include "toplev.h"
34 #include "output.h"
35 #include "ggc.h"
36 #include "debug.h"
37 #include "convert.h"
38 #include "target.h"
39 #include "function.h"
40 #include "cgraph.h"
41 #include "tree-inline.h"
42 #include "tree-gimple.h"
43 #include "tree-dump.h"
44 #include "pointer-set.h"
46 #include "ada.h"
47 #include "types.h"
48 #include "atree.h"
49 #include "elists.h"
50 #include "namet.h"
51 #include "nlists.h"
52 #include "stringt.h"
53 #include "uintp.h"
54 #include "fe.h"
55 #include "sinfo.h"
56 #include "einfo.h"
57 #include "ada-tree.h"
58 #include "gigi.h"
60 #ifndef MAX_FIXED_MODE_SIZE
61 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
62 #endif
64 #ifndef MAX_BITS_PER_WORD
65 #define MAX_BITS_PER_WORD BITS_PER_WORD
66 #endif
68 /* If nonzero, pretend we are allocating at global level. */
69 int force_global;
71 /* Tree nodes for the various types and decls we create. */
72 tree gnat_std_decls[(int) ADT_LAST];
74 /* Functions to call for each of the possible raise reasons. */
75 tree gnat_raise_decls[(int) LAST_REASON_CODE + 1];
77 /* Forward declarations for handlers of attributes. */
78 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
79 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
81 /* Table of machine-independent internal attributes for Ada. We support
82 this minimal set of attributes to accommodate the Alpha back-end which
83 unconditionally puts them on its builtins. */
84 const struct attribute_spec gnat_internal_attribute_table[] =
86 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
87 { "const", 0, 0, true, false, false, handle_const_attribute },
88 { "nothrow", 0, 0, true, false, false, handle_nothrow_attribute },
89 { NULL, 0, 0, false, false, false, NULL }
92 /* Associates a GNAT tree node to a GCC tree node. It is used in
93 `save_gnu_tree', `get_gnu_tree' and `present_gnu_tree'. See documentation
94 of `save_gnu_tree' for more info. */
95 static GTY((length ("max_gnat_nodes"))) tree *associate_gnat_to_gnu;
97 #define GET_GNU_TREE(GNAT_ENTITY) \
98 associate_gnat_to_gnu[(GNAT_ENTITY) - First_Node_Id]
100 #define SET_GNU_TREE(GNAT_ENTITY,VAL) \
101 associate_gnat_to_gnu[(GNAT_ENTITY) - First_Node_Id] = (VAL)
103 #define PRESENT_GNU_TREE(GNAT_ENTITY) \
104 (associate_gnat_to_gnu[(GNAT_ENTITY) - First_Node_Id] != NULL_TREE)
106 /* Associates a GNAT entity to a GCC tree node used as a dummy, if any. */
107 static GTY((length ("max_gnat_nodes"))) tree *dummy_node_table;
109 #define GET_DUMMY_NODE(GNAT_ENTITY) \
110 dummy_node_table[(GNAT_ENTITY) - First_Node_Id]
112 #define SET_DUMMY_NODE(GNAT_ENTITY,VAL) \
113 dummy_node_table[(GNAT_ENTITY) - First_Node_Id] = (VAL)
115 #define PRESENT_DUMMY_NODE(GNAT_ENTITY) \
116 (dummy_node_table[(GNAT_ENTITY) - First_Node_Id] != NULL_TREE)
118 /* This variable keeps a table for types for each precision so that we only
119 allocate each of them once. Signed and unsigned types are kept separate.
121 Note that these types are only used when fold-const requests something
122 special. Perhaps we should NOT share these types; we'll see how it
123 goes later. */
124 static GTY(()) tree signed_and_unsigned_types[2 * MAX_BITS_PER_WORD + 1][2];
126 /* Likewise for float types, but record these by mode. */
127 static GTY(()) tree float_types[NUM_MACHINE_MODES];
129 /* For each binding contour we allocate a binding_level structure to indicate
130 the binding depth. */
132 struct gnat_binding_level GTY((chain_next ("%h.chain")))
134 /* The binding level containing this one (the enclosing binding level). */
135 struct gnat_binding_level *chain;
136 /* The BLOCK node for this level. */
137 tree block;
138 /* If nonzero, the setjmp buffer that needs to be updated for any
139 variable-sized definition within this context. */
140 tree jmpbuf_decl;
143 /* The binding level currently in effect. */
144 static GTY(()) struct gnat_binding_level *current_binding_level;
146 /* A chain of gnat_binding_level structures awaiting reuse. */
147 static GTY((deletable)) struct gnat_binding_level *free_binding_level;
149 /* An array of global declarations. */
150 static GTY(()) VEC(tree,gc) *global_decls;
152 /* An array of builtin declarations. */
153 static GTY(()) VEC(tree,gc) *builtin_decls;
155 /* An array of global renaming pointers. */
156 static GTY(()) VEC(tree,gc) *global_renaming_pointers;
158 /* A chain of unused BLOCK nodes. */
159 static GTY((deletable)) tree free_block_chain;
161 static void gnat_install_builtins (void);
162 static tree merge_sizes (tree, tree, tree, bool, bool);
163 static tree compute_related_constant (tree, tree);
164 static tree split_plus (tree, tree *);
165 static void gnat_gimplify_function (tree);
166 static tree float_type_for_precision (int, enum machine_mode);
167 static tree convert_to_fat_pointer (tree, tree);
168 static tree convert_to_thin_pointer (tree, tree);
169 static tree make_descriptor_field (const char *,tree, tree, tree);
170 static bool potential_alignment_gap (tree, tree, tree);
172 /* Initialize the association of GNAT nodes to GCC trees. */
174 void
175 init_gnat_to_gnu (void)
177 associate_gnat_to_gnu
178 = (tree *) ggc_alloc_cleared (max_gnat_nodes * sizeof (tree));
181 /* GNAT_ENTITY is a GNAT tree node for an entity. GNU_DECL is the GCC tree
182 which is to be associated with GNAT_ENTITY. Such GCC tree node is always
183 a ..._DECL node. If NO_CHECK is nonzero, the latter check is suppressed.
185 If GNU_DECL is zero, a previous association is to be reset. */
187 void
188 save_gnu_tree (Entity_Id gnat_entity, tree gnu_decl, bool no_check)
190 /* Check that GNAT_ENTITY is not already defined and that it is being set
191 to something which is a decl. Raise gigi 401 if not. Usually, this
192 means GNAT_ENTITY is defined twice, but occasionally is due to some
193 Gigi problem. */
194 gcc_assert (!(gnu_decl
195 && (PRESENT_GNU_TREE (gnat_entity)
196 || (!no_check && !DECL_P (gnu_decl)))));
198 SET_GNU_TREE (gnat_entity, gnu_decl);
201 /* GNAT_ENTITY is a GNAT tree node for a defining identifier.
202 Return the ..._DECL node that was associated with it. If there is no tree
203 node associated with GNAT_ENTITY, abort.
205 In some cases, such as delayed elaboration or expressions that need to
206 be elaborated only once, GNAT_ENTITY is really not an entity. */
208 tree
209 get_gnu_tree (Entity_Id gnat_entity)
211 gcc_assert (PRESENT_GNU_TREE (gnat_entity));
212 return GET_GNU_TREE (gnat_entity);
215 /* Return nonzero if a GCC tree has been associated with GNAT_ENTITY. */
217 bool
218 present_gnu_tree (Entity_Id gnat_entity)
220 return PRESENT_GNU_TREE (gnat_entity);
223 /* Initialize the association of GNAT nodes to GCC trees as dummies. */
225 void
226 init_dummy_type (void)
228 dummy_node_table
229 = (tree *) ggc_alloc_cleared (max_gnat_nodes * sizeof (tree));
232 /* Make a dummy type corresponding to GNAT_TYPE. */
234 tree
235 make_dummy_type (Entity_Id gnat_type)
237 Entity_Id gnat_underlying = Gigi_Equivalent_Type (gnat_type);
238 tree gnu_type;
240 /* If there is an equivalent type, get its underlying type. */
241 if (Present (gnat_underlying))
242 gnat_underlying = Underlying_Type (gnat_underlying);
244 /* If there was no equivalent type (can only happen when just annotating
245 types) or underlying type, go back to the original type. */
246 if (No (gnat_underlying))
247 gnat_underlying = gnat_type;
249 /* If it there already a dummy type, use that one. Else make one. */
250 if (PRESENT_DUMMY_NODE (gnat_underlying))
251 return GET_DUMMY_NODE (gnat_underlying);
253 /* If this is a record, make a RECORD_TYPE or UNION_TYPE; else make
254 an ENUMERAL_TYPE. */
255 gnu_type = make_node (Is_Record_Type (gnat_underlying)
256 ? tree_code_for_record_type (gnat_underlying)
257 : ENUMERAL_TYPE);
258 TYPE_NAME (gnu_type) = get_entity_name (gnat_type);
259 TYPE_DUMMY_P (gnu_type) = 1;
260 if (AGGREGATE_TYPE_P (gnu_type))
262 TYPE_STUB_DECL (gnu_type) = build_decl (TYPE_DECL, NULL_TREE, gnu_type);
263 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_type);
266 SET_DUMMY_NODE (gnat_underlying, gnu_type);
268 return gnu_type;
271 /* Return nonzero if we are currently in the global binding level. */
274 global_bindings_p (void)
276 return ((force_global || !current_function_decl) ? -1 : 0);
279 /* Enter a new binding level. */
281 void
282 gnat_pushlevel ()
284 struct gnat_binding_level *newlevel = NULL;
286 /* Reuse a struct for this binding level, if there is one. */
287 if (free_binding_level)
289 newlevel = free_binding_level;
290 free_binding_level = free_binding_level->chain;
292 else
293 newlevel
294 = (struct gnat_binding_level *)
295 ggc_alloc (sizeof (struct gnat_binding_level));
297 /* Use a free BLOCK, if any; otherwise, allocate one. */
298 if (free_block_chain)
300 newlevel->block = free_block_chain;
301 free_block_chain = BLOCK_CHAIN (free_block_chain);
302 BLOCK_CHAIN (newlevel->block) = NULL_TREE;
304 else
305 newlevel->block = make_node (BLOCK);
307 /* Point the BLOCK we just made to its parent. */
308 if (current_binding_level)
309 BLOCK_SUPERCONTEXT (newlevel->block) = current_binding_level->block;
311 BLOCK_VARS (newlevel->block) = BLOCK_SUBBLOCKS (newlevel->block) = NULL_TREE;
312 TREE_USED (newlevel->block) = 1;
314 /* Add this level to the front of the chain (stack) of levels that are
315 active. */
316 newlevel->chain = current_binding_level;
317 newlevel->jmpbuf_decl = NULL_TREE;
318 current_binding_level = newlevel;
321 /* Set SUPERCONTEXT of the BLOCK for the current binding level to FNDECL
322 and point FNDECL to this BLOCK. */
324 void
325 set_current_block_context (tree fndecl)
327 BLOCK_SUPERCONTEXT (current_binding_level->block) = fndecl;
328 DECL_INITIAL (fndecl) = current_binding_level->block;
331 /* Set the jmpbuf_decl for the current binding level to DECL. */
333 void
334 set_block_jmpbuf_decl (tree decl)
336 current_binding_level->jmpbuf_decl = decl;
339 /* Get the jmpbuf_decl, if any, for the current binding level. */
341 tree
342 get_block_jmpbuf_decl ()
344 return current_binding_level->jmpbuf_decl;
347 /* Exit a binding level. Set any BLOCK into the current code group. */
349 void
350 gnat_poplevel ()
352 struct gnat_binding_level *level = current_binding_level;
353 tree block = level->block;
355 BLOCK_VARS (block) = nreverse (BLOCK_VARS (block));
356 BLOCK_SUBBLOCKS (block) = nreverse (BLOCK_SUBBLOCKS (block));
358 /* If this is a function-level BLOCK don't do anything. Otherwise, if there
359 are no variables free the block and merge its subblocks into those of its
360 parent block. Otherwise, add it to the list of its parent. */
361 if (TREE_CODE (BLOCK_SUPERCONTEXT (block)) == FUNCTION_DECL)
363 else if (BLOCK_VARS (block) == NULL_TREE)
365 BLOCK_SUBBLOCKS (level->chain->block)
366 = chainon (BLOCK_SUBBLOCKS (block),
367 BLOCK_SUBBLOCKS (level->chain->block));
368 BLOCK_CHAIN (block) = free_block_chain;
369 free_block_chain = block;
371 else
373 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (level->chain->block);
374 BLOCK_SUBBLOCKS (level->chain->block) = block;
375 TREE_USED (block) = 1;
376 set_block_for_group (block);
379 /* Free this binding structure. */
380 current_binding_level = level->chain;
381 level->chain = free_binding_level;
382 free_binding_level = level;
385 /* Insert BLOCK at the end of the list of subblocks of the
386 current binding level. This is used when a BIND_EXPR is expanded,
387 to handle the BLOCK node inside the BIND_EXPR. */
389 void
390 insert_block (tree block)
392 TREE_USED (block) = 1;
393 TREE_CHAIN (block) = BLOCK_SUBBLOCKS (current_binding_level->block);
394 BLOCK_SUBBLOCKS (current_binding_level->block) = block;
397 /* Records a ..._DECL node DECL as belonging to the current lexical scope
398 and uses GNAT_NODE for location information and propagating flags. */
400 void
401 gnat_pushdecl (tree decl, Node_Id gnat_node)
403 /* If at top level, there is no context. But PARM_DECLs always go in the
404 level of its function. */
405 if (global_bindings_p () && TREE_CODE (decl) != PARM_DECL)
406 DECL_CONTEXT (decl) = 0;
407 else
409 DECL_CONTEXT (decl) = current_function_decl;
411 /* Functions imported in another function are not really nested. */
412 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_PUBLIC (decl))
413 DECL_NO_STATIC_CHAIN (decl) = 1;
416 TREE_NO_WARNING (decl) = (gnat_node == Empty || Warnings_Off (gnat_node));
418 /* Set the location of DECL and emit a declaration for it. */
419 if (Present (gnat_node))
420 Sloc_to_locus (Sloc (gnat_node), &DECL_SOURCE_LOCATION (decl));
421 add_decl_expr (decl, gnat_node);
423 /* Put the declaration on the list. The list of declarations is in reverse
424 order. The list will be reversed later. Put global variables in the
425 globals list and builtin functions in a dedicated list to speed up
426 further lookups. Don't put TYPE_DECLs for UNCONSTRAINED_ARRAY_TYPE into
427 the list, as they will cause trouble with the debugger and aren't needed
428 anyway. */
429 if (TREE_CODE (decl) != TYPE_DECL
430 || TREE_CODE (TREE_TYPE (decl)) != UNCONSTRAINED_ARRAY_TYPE)
432 if (global_bindings_p ())
434 VEC_safe_push (tree, gc, global_decls, decl);
436 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
437 VEC_safe_push (tree, gc, builtin_decls, decl);
439 else
441 TREE_CHAIN (decl) = BLOCK_VARS (current_binding_level->block);
442 BLOCK_VARS (current_binding_level->block) = decl;
446 /* For the declaration of a type, set its name if it either is not already
447 set, was set to an IDENTIFIER_NODE, indicating an internal name,
448 or if the previous type name was not derived from a source name.
449 We'd rather have the type named with a real name and all the pointer
450 types to the same object have the same POINTER_TYPE node. Code in the
451 equivalent function of c-decl.c makes a copy of the type node here, but
452 that may cause us trouble with incomplete types. We make an exception
453 for fat pointer types because the compiler automatically builds them
454 for unconstrained array types and the debugger uses them to represent
455 both these and pointers to these. */
456 if (TREE_CODE (decl) == TYPE_DECL && DECL_NAME (decl))
458 tree t = TREE_TYPE (decl);
460 if (!TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) == IDENTIFIER_NODE)
461 TYPE_NAME (t) = decl;
462 else if (TYPE_FAT_POINTER_P (t))
464 tree tt = build_variant_type_copy (t);
465 TYPE_NAME (tt) = decl;
466 TREE_USED (tt) = TREE_USED (t);
467 TREE_TYPE (decl) = tt;
468 DECL_ORIGINAL_TYPE (decl) = t;
470 else if (DECL_ARTIFICIAL (TYPE_NAME (t)) && !DECL_ARTIFICIAL (decl))
471 TYPE_NAME (t) = decl;
475 /* Do little here. Set up the standard declarations later after the
476 front end has been run. */
478 void
479 gnat_init_decl_processing (void)
481 /* Make the binding_level structure for global names. */
482 current_function_decl = 0;
483 current_binding_level = 0;
484 free_binding_level = 0;
485 gnat_pushlevel ();
487 build_common_tree_nodes (true, true);
489 /* In Ada, we use a signed type for SIZETYPE. Use the signed type
490 corresponding to the size of Pmode. In most cases when ptr_mode and
491 Pmode differ, C will use the width of ptr_mode as sizetype. But we get
492 far better code using the width of Pmode. Make this here since we need
493 this before we can expand the GNAT types. */
494 size_type_node = gnat_type_for_size (GET_MODE_BITSIZE (Pmode), 0);
495 set_sizetype (size_type_node);
496 build_common_tree_nodes_2 (0);
498 ptr_void_type_node = build_pointer_type (void_type_node);
500 gnat_install_builtins ();
503 /* Install the builtin functions we might need. */
505 static void
506 gnat_install_builtins ()
508 /* Builtins used by generic middle-end optimizers. */
509 build_common_builtin_nodes ();
511 /* Target specific builtins, such as the AltiVec family on ppc. */
512 targetm.init_builtins ();
515 /* Create the predefined scalar types such as `integer_type_node' needed
516 in the gcc back-end and initialize the global binding level. */
518 void
519 init_gigi_decls (tree long_long_float_type, tree exception_type)
521 tree endlink, decl;
522 unsigned int i;
524 /* Set the types that GCC and Gigi use from the front end. We would like
525 to do this for char_type_node, but it needs to correspond to the C
526 char type. */
527 if (TREE_CODE (TREE_TYPE (long_long_float_type)) == INTEGER_TYPE)
529 /* In this case, the builtin floating point types are VAX float,
530 so make up a type for use. */
531 longest_float_type_node = make_node (REAL_TYPE);
532 TYPE_PRECISION (longest_float_type_node) = LONG_DOUBLE_TYPE_SIZE;
533 layout_type (longest_float_type_node);
534 create_type_decl (get_identifier ("longest float type"),
535 longest_float_type_node, NULL, false, true, Empty);
537 else
538 longest_float_type_node = TREE_TYPE (long_long_float_type);
540 except_type_node = TREE_TYPE (exception_type);
542 unsigned_type_node = gnat_type_for_size (INT_TYPE_SIZE, 1);
543 create_type_decl (get_identifier ("unsigned int"), unsigned_type_node,
544 NULL, false, true, Empty);
546 void_type_decl_node = create_type_decl (get_identifier ("void"),
547 void_type_node, NULL, false, true,
548 Empty);
550 void_ftype = build_function_type (void_type_node, NULL_TREE);
551 ptr_void_ftype = build_pointer_type (void_ftype);
553 /* Now declare runtime functions. */
554 endlink = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
556 /* malloc is a function declaration tree for a function to allocate
557 memory. */
558 malloc_decl = create_subprog_decl (get_identifier ("__gnat_malloc"),
559 NULL_TREE,
560 build_function_type (ptr_void_type_node,
561 tree_cons (NULL_TREE,
562 sizetype,
563 endlink)),
564 NULL_TREE, false, true, true, NULL,
565 Empty);
566 DECL_IS_MALLOC (malloc_decl) = 1;
568 /* free is a function declaration tree for a function to free memory. */
569 free_decl
570 = create_subprog_decl (get_identifier ("__gnat_free"), NULL_TREE,
571 build_function_type (void_type_node,
572 tree_cons (NULL_TREE,
573 ptr_void_type_node,
574 endlink)),
575 NULL_TREE, false, true, true, NULL, Empty);
577 /* Make the types and functions used for exception processing. */
578 jmpbuf_type
579 = build_array_type (gnat_type_for_mode (Pmode, 0),
580 build_index_type (build_int_cst (NULL_TREE, 5)));
581 create_type_decl (get_identifier ("JMPBUF_T"), jmpbuf_type, NULL,
582 true, true, Empty);
583 jmpbuf_ptr_type = build_pointer_type (jmpbuf_type);
585 /* Functions to get and set the jumpbuf pointer for the current thread. */
586 get_jmpbuf_decl
587 = create_subprog_decl
588 (get_identifier ("system__soft_links__get_jmpbuf_address_soft"),
589 NULL_TREE, build_function_type (jmpbuf_ptr_type, NULL_TREE),
590 NULL_TREE, false, true, true, NULL, Empty);
591 /* Avoid creating superfluous edges to __builtin_setjmp receivers. */
592 DECL_IS_PURE (get_jmpbuf_decl) = 1;
594 set_jmpbuf_decl
595 = create_subprog_decl
596 (get_identifier ("system__soft_links__set_jmpbuf_address_soft"),
597 NULL_TREE,
598 build_function_type (void_type_node,
599 tree_cons (NULL_TREE, jmpbuf_ptr_type, endlink)),
600 NULL_TREE, false, true, true, NULL, Empty);
602 /* Function to get the current exception. */
603 get_excptr_decl
604 = create_subprog_decl
605 (get_identifier ("system__soft_links__get_gnat_exception"),
606 NULL_TREE,
607 build_function_type (build_pointer_type (except_type_node), NULL_TREE),
608 NULL_TREE, false, true, true, NULL, Empty);
609 /* Avoid creating superfluous edges to __builtin_setjmp receivers. */
610 DECL_IS_PURE (get_excptr_decl) = 1;
612 /* Functions that raise exceptions. */
613 raise_nodefer_decl
614 = create_subprog_decl
615 (get_identifier ("__gnat_raise_nodefer_with_msg"), NULL_TREE,
616 build_function_type (void_type_node,
617 tree_cons (NULL_TREE,
618 build_pointer_type (except_type_node),
619 endlink)),
620 NULL_TREE, false, true, true, NULL, Empty);
622 /* Dummy objects to materialize "others" and "all others" in the exception
623 tables. These are exported by a-exexpr.adb, so see this unit for the
624 types to use. */
626 others_decl
627 = create_var_decl (get_identifier ("OTHERS"),
628 get_identifier ("__gnat_others_value"),
629 integer_type_node, 0, 1, 0, 1, 1, 0, Empty);
631 all_others_decl
632 = create_var_decl (get_identifier ("ALL_OTHERS"),
633 get_identifier ("__gnat_all_others_value"),
634 integer_type_node, 0, 1, 0, 1, 1, 0, Empty);
636 /* Hooks to call when entering/leaving an exception handler. */
637 begin_handler_decl
638 = create_subprog_decl (get_identifier ("__gnat_begin_handler"), NULL_TREE,
639 build_function_type (void_type_node,
640 tree_cons (NULL_TREE,
641 ptr_void_type_node,
642 endlink)),
643 NULL_TREE, false, true, true, NULL, Empty);
645 end_handler_decl
646 = create_subprog_decl (get_identifier ("__gnat_end_handler"), NULL_TREE,
647 build_function_type (void_type_node,
648 tree_cons (NULL_TREE,
649 ptr_void_type_node,
650 endlink)),
651 NULL_TREE, false, true, true, NULL, Empty);
653 /* If in no exception handlers mode, all raise statements are redirected to
654 __gnat_last_chance_handler. No need to redefine raise_nodefer_decl, since
655 this procedure will never be called in this mode. */
656 if (No_Exception_Handlers_Set ())
658 decl
659 = create_subprog_decl
660 (get_identifier ("__gnat_last_chance_handler"), NULL_TREE,
661 build_function_type (void_type_node,
662 tree_cons (NULL_TREE,
663 build_pointer_type (char_type_node),
664 tree_cons (NULL_TREE,
665 integer_type_node,
666 endlink))),
667 NULL_TREE, false, true, true, NULL, Empty);
669 for (i = 0; i < ARRAY_SIZE (gnat_raise_decls); i++)
670 gnat_raise_decls[i] = decl;
672 else
673 /* Otherwise, make one decl for each exception reason. */
674 for (i = 0; i < ARRAY_SIZE (gnat_raise_decls); i++)
676 char name[17];
678 sprintf (name, "__gnat_rcheck_%.2d", i);
679 gnat_raise_decls[i]
680 = create_subprog_decl
681 (get_identifier (name), NULL_TREE,
682 build_function_type (void_type_node,
683 tree_cons (NULL_TREE,
684 build_pointer_type
685 (char_type_node),
686 tree_cons (NULL_TREE,
687 integer_type_node,
688 endlink))),
689 NULL_TREE, false, true, true, NULL, Empty);
692 /* Indicate that these never return. */
693 TREE_THIS_VOLATILE (raise_nodefer_decl) = 1;
694 TREE_SIDE_EFFECTS (raise_nodefer_decl) = 1;
695 TREE_TYPE (raise_nodefer_decl)
696 = build_qualified_type (TREE_TYPE (raise_nodefer_decl),
697 TYPE_QUAL_VOLATILE);
699 for (i = 0; i < ARRAY_SIZE (gnat_raise_decls); i++)
701 TREE_THIS_VOLATILE (gnat_raise_decls[i]) = 1;
702 TREE_SIDE_EFFECTS (gnat_raise_decls[i]) = 1;
703 TREE_TYPE (gnat_raise_decls[i])
704 = build_qualified_type (TREE_TYPE (gnat_raise_decls[i]),
705 TYPE_QUAL_VOLATILE);
708 /* setjmp returns an integer and has one operand, which is a pointer to
709 a jmpbuf. */
710 setjmp_decl
711 = create_subprog_decl
712 (get_identifier ("__builtin_setjmp"), NULL_TREE,
713 build_function_type (integer_type_node,
714 tree_cons (NULL_TREE, jmpbuf_ptr_type, endlink)),
715 NULL_TREE, false, true, true, NULL, Empty);
717 DECL_BUILT_IN_CLASS (setjmp_decl) = BUILT_IN_NORMAL;
718 DECL_FUNCTION_CODE (setjmp_decl) = BUILT_IN_SETJMP;
720 /* update_setjmp_buf updates a setjmp buffer from the current stack pointer
721 address. */
722 update_setjmp_buf_decl
723 = create_subprog_decl
724 (get_identifier ("__builtin_update_setjmp_buf"), NULL_TREE,
725 build_function_type (void_type_node,
726 tree_cons (NULL_TREE, jmpbuf_ptr_type, endlink)),
727 NULL_TREE, false, true, true, NULL, Empty);
729 DECL_BUILT_IN_CLASS (update_setjmp_buf_decl) = BUILT_IN_NORMAL;
730 DECL_FUNCTION_CODE (update_setjmp_buf_decl) = BUILT_IN_UPDATE_SETJMP_BUF;
732 main_identifier_node = get_identifier ("main");
735 /* Given a record type RECORD_TYPE and a chain of FIELD_DECL nodes FIELDLIST,
736 finish constructing the record or union type. If REP_LEVEL is zero, this
737 record has no representation clause and so will be entirely laid out here.
738 If REP_LEVEL is one, this record has a representation clause and has been
739 laid out already; only set the sizes and alignment. If REP_LEVEL is two,
740 this record is derived from a parent record and thus inherits its layout;
741 only make a pass on the fields to finalize them. If DO_NOT_FINALIZE is
742 true, the record type is expected to be modified afterwards so it will
743 not be sent to the back-end for finalization. */
745 void
746 finish_record_type (tree record_type, tree fieldlist, int rep_level,
747 bool do_not_finalize)
749 enum tree_code code = TREE_CODE (record_type);
750 tree name = TYPE_NAME (record_type);
751 tree ada_size = bitsize_zero_node;
752 tree size = bitsize_zero_node;
753 bool had_size = TYPE_SIZE (record_type) != 0;
754 bool had_size_unit = TYPE_SIZE_UNIT (record_type) != 0;
755 bool had_align = TYPE_ALIGN (record_type) != 0;
756 tree field;
758 if (name && TREE_CODE (name) == TYPE_DECL)
759 name = DECL_NAME (name);
761 TYPE_FIELDS (record_type) = fieldlist;
762 TYPE_STUB_DECL (record_type) = build_decl (TYPE_DECL, name, record_type);
764 /* We don't need both the typedef name and the record name output in
765 the debugging information, since they are the same. */
766 DECL_ARTIFICIAL (TYPE_STUB_DECL (record_type)) = 1;
768 /* Globally initialize the record first. If this is a rep'ed record,
769 that just means some initializations; otherwise, layout the record. */
770 if (rep_level > 0)
772 TYPE_ALIGN (record_type) = MAX (BITS_PER_UNIT, TYPE_ALIGN (record_type));
773 TYPE_MODE (record_type) = BLKmode;
775 if (!had_size_unit)
776 TYPE_SIZE_UNIT (record_type) = size_zero_node;
777 if (!had_size)
778 TYPE_SIZE (record_type) = bitsize_zero_node;
780 /* For all-repped records with a size specified, lay the QUAL_UNION_TYPE
781 out just like a UNION_TYPE, since the size will be fixed. */
782 else if (code == QUAL_UNION_TYPE)
783 code = UNION_TYPE;
785 else
787 /* Ensure there isn't a size already set. There can be in an error
788 case where there is a rep clause but all fields have errors and
789 no longer have a position. */
790 TYPE_SIZE (record_type) = 0;
791 layout_type (record_type);
794 /* At this point, the position and size of each field is known. It was
795 either set before entry by a rep clause, or by laying out the type above.
797 We now run a pass over the fields (in reverse order for QUAL_UNION_TYPEs)
798 to compute the Ada size; the GCC size and alignment (for rep'ed records
799 that are not padding types); and the mode (for rep'ed records). We also
800 clear the DECL_BIT_FIELD indication for the cases we know have not been
801 handled yet, and adjust DECL_NONADDRESSABLE_P accordingly. */
803 if (code == QUAL_UNION_TYPE)
804 fieldlist = nreverse (fieldlist);
806 for (field = fieldlist; field; field = TREE_CHAIN (field))
808 tree type = TREE_TYPE (field);
809 tree pos = bit_position (field);
810 tree this_size = DECL_SIZE (field);
811 tree this_ada_size;
813 if ((TREE_CODE (type) == RECORD_TYPE
814 || TREE_CODE (type) == UNION_TYPE
815 || TREE_CODE (type) == QUAL_UNION_TYPE)
816 && !TYPE_IS_FAT_POINTER_P (type)
817 && !TYPE_CONTAINS_TEMPLATE_P (type)
818 && TYPE_ADA_SIZE (type))
819 this_ada_size = TYPE_ADA_SIZE (type);
820 else
821 this_ada_size = this_size;
823 /* Clear DECL_BIT_FIELD for the cases layout_decl does not handle. */
824 if (DECL_BIT_FIELD (field)
825 && operand_equal_p (this_size, TYPE_SIZE (type), 0))
827 unsigned int align = TYPE_ALIGN (type);
829 /* In the general case, type alignment is required. */
830 if (value_factor_p (pos, align))
832 /* The enclosing record type must be sufficiently aligned.
833 Otherwise, if no alignment was specified for it and it
834 has been laid out already, bump its alignment to the
835 desired one if this is compatible with its size. */
836 if (TYPE_ALIGN (record_type) >= align)
838 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), align);
839 DECL_BIT_FIELD (field) = 0;
841 else if (!had_align
842 && rep_level == 0
843 && value_factor_p (TYPE_SIZE (record_type), align))
845 TYPE_ALIGN (record_type) = align;
846 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), align);
847 DECL_BIT_FIELD (field) = 0;
851 /* In the non-strict alignment case, only byte alignment is. */
852 if (!STRICT_ALIGNMENT
853 && DECL_BIT_FIELD (field)
854 && value_factor_p (pos, BITS_PER_UNIT))
855 DECL_BIT_FIELD (field) = 0;
858 /* If we still have DECL_BIT_FIELD set at this point, we know the field
859 is technically not addressable. Except that it can actually be
860 addressed if the field is BLKmode and happens to be properly
861 aligned. */
862 DECL_NONADDRESSABLE_P (field)
863 |= DECL_BIT_FIELD (field) && DECL_MODE (field) != BLKmode;
865 /* A type must be as aligned as its most aligned field that is not
866 a bit-field. But this is already enforced by layout_type. */
867 if (rep_level > 0 && !DECL_BIT_FIELD (field))
868 TYPE_ALIGN (record_type)
869 = MAX (TYPE_ALIGN (record_type), DECL_ALIGN (field));
871 switch (code)
873 case UNION_TYPE:
874 ada_size = size_binop (MAX_EXPR, ada_size, this_ada_size);
875 size = size_binop (MAX_EXPR, size, this_size);
876 break;
878 case QUAL_UNION_TYPE:
879 ada_size
880 = fold_build3 (COND_EXPR, bitsizetype, DECL_QUALIFIER (field),
881 this_ada_size, ada_size);
882 size = fold_build3 (COND_EXPR, bitsizetype, DECL_QUALIFIER (field),
883 this_size, size);
884 break;
886 case RECORD_TYPE:
887 /* Since we know here that all fields are sorted in order of
888 increasing bit position, the size of the record is one
889 higher than the ending bit of the last field processed
890 unless we have a rep clause, since in that case we might
891 have a field outside a QUAL_UNION_TYPE that has a higher ending
892 position. So use a MAX in that case. Also, if this field is a
893 QUAL_UNION_TYPE, we need to take into account the previous size in
894 the case of empty variants. */
895 ada_size
896 = merge_sizes (ada_size, pos, this_ada_size,
897 TREE_CODE (type) == QUAL_UNION_TYPE, rep_level > 0);
898 size
899 = merge_sizes (size, pos, this_size,
900 TREE_CODE (type) == QUAL_UNION_TYPE, rep_level > 0);
901 break;
903 default:
904 gcc_unreachable ();
908 if (code == QUAL_UNION_TYPE)
909 nreverse (fieldlist);
911 if (rep_level < 2)
913 /* If this is a padding record, we never want to make the size smaller
914 than what was specified in it, if any. */
915 if (TREE_CODE (record_type) == RECORD_TYPE
916 && TYPE_IS_PADDING_P (record_type) && TYPE_SIZE (record_type))
917 size = TYPE_SIZE (record_type);
919 /* Now set any of the values we've just computed that apply. */
920 if (!TYPE_IS_FAT_POINTER_P (record_type)
921 && !TYPE_CONTAINS_TEMPLATE_P (record_type))
922 SET_TYPE_ADA_SIZE (record_type, ada_size);
924 if (rep_level > 0)
926 tree size_unit = had_size_unit
927 ? TYPE_SIZE_UNIT (record_type)
928 : convert (sizetype,
929 size_binop (CEIL_DIV_EXPR, size,
930 bitsize_unit_node));
931 unsigned int align = TYPE_ALIGN (record_type);
933 TYPE_SIZE (record_type) = variable_size (round_up (size, align));
934 TYPE_SIZE_UNIT (record_type)
935 = variable_size (round_up (size_unit, align / BITS_PER_UNIT));
937 compute_record_mode (record_type);
941 if (!do_not_finalize)
942 rest_of_record_type_compilation (record_type);
945 /* Wrap up compilation of RECORD_TYPE, i.e. most notably output all
946 the debug information associated with it. It need not be invoked
947 directly in most cases since finish_record_type takes care of doing
948 so, unless explicitly requested not to through DO_NOT_FINALIZE. */
950 void
951 rest_of_record_type_compilation (tree record_type)
953 tree fieldlist = TYPE_FIELDS (record_type);
954 tree field;
955 enum tree_code code = TREE_CODE (record_type);
956 bool var_size = false;
958 for (field = fieldlist; field; field = TREE_CHAIN (field))
960 /* We need to make an XVE/XVU record if any field has variable size,
961 whether or not the record does. For example, if we have a union,
962 it may be that all fields, rounded up to the alignment, have the
963 same size, in which case we'll use that size. But the debug
964 output routines (except Dwarf2) won't be able to output the fields,
965 so we need to make the special record. */
966 if (TREE_CODE (DECL_SIZE (field)) != INTEGER_CST
967 /* If a field has a non-constant qualifier, the record will have
968 variable size too. */
969 || (code == QUAL_UNION_TYPE
970 && TREE_CODE (DECL_QUALIFIER (field)) != INTEGER_CST))
972 var_size = true;
973 break;
977 /* If this record is of variable size, rename it so that the
978 debugger knows it is and make a new, parallel, record
979 that tells the debugger how the record is laid out. See
980 exp_dbug.ads. But don't do this for records that are padding
981 since they confuse GDB. */
982 if (var_size
983 && !(TREE_CODE (record_type) == RECORD_TYPE
984 && TYPE_IS_PADDING_P (record_type)))
986 tree new_record_type
987 = make_node (TREE_CODE (record_type) == QUAL_UNION_TYPE
988 ? UNION_TYPE : TREE_CODE (record_type));
989 tree orig_name = TYPE_NAME (record_type);
990 tree orig_id
991 = (TREE_CODE (orig_name) == TYPE_DECL ? DECL_NAME (orig_name)
992 : orig_name);
993 tree new_id
994 = concat_id_with_name (orig_id,
995 TREE_CODE (record_type) == QUAL_UNION_TYPE
996 ? "XVU" : "XVE");
997 tree last_pos = bitsize_zero_node;
998 tree old_field;
999 tree prev_old_field = 0;
1001 TYPE_NAME (new_record_type) = new_id;
1002 TYPE_ALIGN (new_record_type) = BIGGEST_ALIGNMENT;
1003 TYPE_STUB_DECL (new_record_type)
1004 = build_decl (TYPE_DECL, new_id, new_record_type);
1005 DECL_ARTIFICIAL (TYPE_STUB_DECL (new_record_type)) = 1;
1006 DECL_IGNORED_P (TYPE_STUB_DECL (new_record_type))
1007 = DECL_IGNORED_P (TYPE_STUB_DECL (record_type));
1008 TYPE_SIZE (new_record_type) = size_int (TYPE_ALIGN (record_type));
1009 TYPE_SIZE_UNIT (new_record_type)
1010 = size_int (TYPE_ALIGN (record_type) / BITS_PER_UNIT);
1012 /* Now scan all the fields, replacing each field with a new
1013 field corresponding to the new encoding. */
1014 for (old_field = TYPE_FIELDS (record_type); old_field;
1015 old_field = TREE_CHAIN (old_field))
1017 tree field_type = TREE_TYPE (old_field);
1018 tree field_name = DECL_NAME (old_field);
1019 tree new_field;
1020 tree curpos = bit_position (old_field);
1021 bool var = false;
1022 unsigned int align = 0;
1023 tree pos;
1025 /* See how the position was modified from the last position.
1027 There are two basic cases we support: a value was added
1028 to the last position or the last position was rounded to
1029 a boundary and they something was added. Check for the
1030 first case first. If not, see if there is any evidence
1031 of rounding. If so, round the last position and try
1032 again.
1034 If this is a union, the position can be taken as zero. */
1036 if (TREE_CODE (new_record_type) == UNION_TYPE)
1037 pos = bitsize_zero_node, align = 0;
1038 else
1039 pos = compute_related_constant (curpos, last_pos);
1041 if (!pos && TREE_CODE (curpos) == MULT_EXPR
1042 && host_integerp (TREE_OPERAND (curpos, 1), 1))
1044 tree offset = TREE_OPERAND (curpos, 0);
1045 align = tree_low_cst (TREE_OPERAND (curpos, 1), 1);
1047 /* Strip off any conversions. */
1048 while (TREE_CODE (offset) == NON_LVALUE_EXPR
1049 || TREE_CODE (offset) == NOP_EXPR
1050 || TREE_CODE (offset) == CONVERT_EXPR)
1051 offset = TREE_OPERAND (offset, 0);
1053 /* An offset which is a bitwise AND with a negative power of 2
1054 means an alignment corresponding to this power of 2. */
1055 if (TREE_CODE (offset) == BIT_AND_EXPR
1056 && host_integerp (TREE_OPERAND (offset, 1), 0)
1057 && tree_int_cst_sgn (TREE_OPERAND (offset, 1)) < 0)
1059 unsigned int pow
1060 = - tree_low_cst (TREE_OPERAND (offset, 1), 0);
1061 if (exact_log2 (pow) > 0)
1062 align *= pow;
1065 pos = compute_related_constant (curpos,
1066 round_up (last_pos, align));
1068 else if (!pos && TREE_CODE (curpos) == PLUS_EXPR
1069 && TREE_CODE (TREE_OPERAND (curpos, 1)) == INTEGER_CST
1070 && TREE_CODE (TREE_OPERAND (curpos, 0)) == MULT_EXPR
1071 && host_integerp (TREE_OPERAND
1072 (TREE_OPERAND (curpos, 0), 1),
1075 align
1076 = tree_low_cst
1077 (TREE_OPERAND (TREE_OPERAND (curpos, 0), 1), 1);
1078 pos = compute_related_constant (curpos,
1079 round_up (last_pos, align));
1081 else if (potential_alignment_gap (prev_old_field, old_field,
1082 pos))
1084 align = TYPE_ALIGN (field_type);
1085 pos = compute_related_constant (curpos,
1086 round_up (last_pos, align));
1089 /* If we can't compute a position, set it to zero.
1091 ??? We really should abort here, but it's too much work
1092 to get this correct for all cases. */
1094 if (!pos)
1095 pos = bitsize_zero_node;
1097 /* See if this type is variable-sized and make a pointer type
1098 and indicate the indirection if so. Beware that the debug
1099 back-end may adjust the position computed above according
1100 to the alignment of the field type, i.e. the pointer type
1101 in this case, if we don't preventively counter that. */
1102 if (TREE_CODE (DECL_SIZE (old_field)) != INTEGER_CST)
1104 field_type = build_pointer_type (field_type);
1105 if (align != 0 && TYPE_ALIGN (field_type) > align)
1107 field_type = copy_node (field_type);
1108 TYPE_ALIGN (field_type) = align;
1110 var = true;
1113 /* Make a new field name, if necessary. */
1114 if (var || align != 0)
1116 char suffix[16];
1118 if (align != 0)
1119 sprintf (suffix, "XV%c%u", var ? 'L' : 'A',
1120 align / BITS_PER_UNIT);
1121 else
1122 strcpy (suffix, "XVL");
1124 field_name = concat_id_with_name (field_name, suffix);
1127 new_field = create_field_decl (field_name, field_type,
1128 new_record_type, 0,
1129 DECL_SIZE (old_field), pos, 0);
1130 TREE_CHAIN (new_field) = TYPE_FIELDS (new_record_type);
1131 TYPE_FIELDS (new_record_type) = new_field;
1133 /* If old_field is a QUAL_UNION_TYPE, take its size as being
1134 zero. The only time it's not the last field of the record
1135 is when there are other components at fixed positions after
1136 it (meaning there was a rep clause for every field) and we
1137 want to be able to encode them. */
1138 last_pos = size_binop (PLUS_EXPR, bit_position (old_field),
1139 (TREE_CODE (TREE_TYPE (old_field))
1140 == QUAL_UNION_TYPE)
1141 ? bitsize_zero_node
1142 : DECL_SIZE (old_field));
1143 prev_old_field = old_field;
1146 TYPE_FIELDS (new_record_type)
1147 = nreverse (TYPE_FIELDS (new_record_type));
1149 rest_of_type_decl_compilation (TYPE_STUB_DECL (new_record_type));
1152 rest_of_type_decl_compilation (TYPE_STUB_DECL (record_type));
1155 /* Utility function of above to merge LAST_SIZE, the previous size of a record
1156 with FIRST_BIT and SIZE that describe a field. SPECIAL is nonzero
1157 if this represents a QUAL_UNION_TYPE in which case we must look for
1158 COND_EXPRs and replace a value of zero with the old size. If HAS_REP
1159 is nonzero, we must take the MAX of the end position of this field
1160 with LAST_SIZE. In all other cases, we use FIRST_BIT plus SIZE.
1162 We return an expression for the size. */
1164 static tree
1165 merge_sizes (tree last_size, tree first_bit, tree size, bool special,
1166 bool has_rep)
1168 tree type = TREE_TYPE (last_size);
1169 tree new;
1171 if (!special || TREE_CODE (size) != COND_EXPR)
1173 new = size_binop (PLUS_EXPR, first_bit, size);
1174 if (has_rep)
1175 new = size_binop (MAX_EXPR, last_size, new);
1178 else
1179 new = fold_build3 (COND_EXPR, type, TREE_OPERAND (size, 0),
1180 integer_zerop (TREE_OPERAND (size, 1))
1181 ? last_size : merge_sizes (last_size, first_bit,
1182 TREE_OPERAND (size, 1),
1183 1, has_rep),
1184 integer_zerop (TREE_OPERAND (size, 2))
1185 ? last_size : merge_sizes (last_size, first_bit,
1186 TREE_OPERAND (size, 2),
1187 1, has_rep));
1189 /* We don't need any NON_VALUE_EXPRs and they can confuse us (especially
1190 when fed through substitute_in_expr) into thinking that a constant
1191 size is not constant. */
1192 while (TREE_CODE (new) == NON_LVALUE_EXPR)
1193 new = TREE_OPERAND (new, 0);
1195 return new;
1198 /* Utility function of above to see if OP0 and OP1, both of SIZETYPE, are
1199 related by the addition of a constant. Return that constant if so. */
1201 static tree
1202 compute_related_constant (tree op0, tree op1)
1204 tree op0_var, op1_var;
1205 tree op0_con = split_plus (op0, &op0_var);
1206 tree op1_con = split_plus (op1, &op1_var);
1207 tree result = size_binop (MINUS_EXPR, op0_con, op1_con);
1209 if (operand_equal_p (op0_var, op1_var, 0))
1210 return result;
1211 else if (operand_equal_p (op0, size_binop (PLUS_EXPR, op1_var, result), 0))
1212 return result;
1213 else
1214 return 0;
1217 /* Utility function of above to split a tree OP which may be a sum, into a
1218 constant part, which is returned, and a variable part, which is stored
1219 in *PVAR. *PVAR may be bitsize_zero_node. All operations must be of
1220 bitsizetype. */
1222 static tree
1223 split_plus (tree in, tree *pvar)
1225 /* Strip NOPS in order to ease the tree traversal and maximize the
1226 potential for constant or plus/minus discovery. We need to be careful
1227 to always return and set *pvar to bitsizetype trees, but it's worth
1228 the effort. */
1229 STRIP_NOPS (in);
1231 *pvar = convert (bitsizetype, in);
1233 if (TREE_CODE (in) == INTEGER_CST)
1235 *pvar = bitsize_zero_node;
1236 return convert (bitsizetype, in);
1238 else if (TREE_CODE (in) == PLUS_EXPR || TREE_CODE (in) == MINUS_EXPR)
1240 tree lhs_var, rhs_var;
1241 tree lhs_con = split_plus (TREE_OPERAND (in, 0), &lhs_var);
1242 tree rhs_con = split_plus (TREE_OPERAND (in, 1), &rhs_var);
1244 if (lhs_var == TREE_OPERAND (in, 0)
1245 && rhs_var == TREE_OPERAND (in, 1))
1246 return bitsize_zero_node;
1248 *pvar = size_binop (TREE_CODE (in), lhs_var, rhs_var);
1249 return size_binop (TREE_CODE (in), lhs_con, rhs_con);
1251 else
1252 return bitsize_zero_node;
1255 /* Return a FUNCTION_TYPE node. RETURN_TYPE is the type returned by the
1256 subprogram. If it is void_type_node, then we are dealing with a procedure,
1257 otherwise we are dealing with a function. PARAM_DECL_LIST is a list of
1258 PARM_DECL nodes that are the subprogram arguments. CICO_LIST is the
1259 copy-in/copy-out list to be stored into TYPE_CICO_LIST.
1260 RETURNS_UNCONSTRAINED is nonzero if the function returns an unconstrained
1261 object. RETURNS_BY_REF is nonzero if the function returns by reference.
1262 RETURNS_WITH_DSP is nonzero if the function is to return with a
1263 depressed stack pointer. RETURNS_BY_TARGET_PTR is true if the function
1264 is to be passed (as its first parameter) the address of the place to copy
1265 its result. */
1267 tree
1268 create_subprog_type (tree return_type, tree param_decl_list, tree cico_list,
1269 bool returns_unconstrained, bool returns_by_ref,
1270 bool returns_with_dsp, bool returns_by_target_ptr)
1272 /* A chain of TREE_LIST nodes whose TREE_VALUEs are the data type nodes of
1273 the subprogram formal parameters. This list is generated by traversing the
1274 input list of PARM_DECL nodes. */
1275 tree param_type_list = NULL;
1276 tree param_decl;
1277 tree type;
1279 for (param_decl = param_decl_list; param_decl;
1280 param_decl = TREE_CHAIN (param_decl))
1281 param_type_list = tree_cons (NULL_TREE, TREE_TYPE (param_decl),
1282 param_type_list);
1284 /* The list of the function parameter types has to be terminated by the void
1285 type to signal to the back-end that we are not dealing with a variable
1286 parameter subprogram, but that the subprogram has a fixed number of
1287 parameters. */
1288 param_type_list = tree_cons (NULL_TREE, void_type_node, param_type_list);
1290 /* The list of argument types has been created in reverse
1291 so nreverse it. */
1292 param_type_list = nreverse (param_type_list);
1294 type = build_function_type (return_type, param_type_list);
1296 /* TYPE may have been shared since GCC hashes types. If it has a CICO_LIST
1297 or the new type should, make a copy of TYPE. Likewise for
1298 RETURNS_UNCONSTRAINED and RETURNS_BY_REF. */
1299 if (TYPE_CI_CO_LIST (type) || cico_list
1300 || TYPE_RETURNS_UNCONSTRAINED_P (type) != returns_unconstrained
1301 || TYPE_RETURNS_BY_REF_P (type) != returns_by_ref
1302 || TYPE_RETURNS_BY_TARGET_PTR_P (type) != returns_by_target_ptr)
1303 type = copy_type (type);
1305 TYPE_CI_CO_LIST (type) = cico_list;
1306 TYPE_RETURNS_UNCONSTRAINED_P (type) = returns_unconstrained;
1307 TYPE_RETURNS_STACK_DEPRESSED (type) = returns_with_dsp;
1308 TYPE_RETURNS_BY_REF_P (type) = returns_by_ref;
1309 TYPE_RETURNS_BY_TARGET_PTR_P (type) = returns_by_target_ptr;
1310 return type;
1313 /* Return a copy of TYPE but safe to modify in any way. */
1315 tree
1316 copy_type (tree type)
1318 tree new = copy_node (type);
1320 /* copy_node clears this field instead of copying it, because it is
1321 aliased with TREE_CHAIN. */
1322 TYPE_STUB_DECL (new) = TYPE_STUB_DECL (type);
1324 TYPE_POINTER_TO (new) = 0;
1325 TYPE_REFERENCE_TO (new) = 0;
1326 TYPE_MAIN_VARIANT (new) = new;
1327 TYPE_NEXT_VARIANT (new) = 0;
1329 return new;
1332 /* Return an INTEGER_TYPE of SIZETYPE with range MIN to MAX and whose
1333 TYPE_INDEX_TYPE is INDEX. GNAT_NODE is used for the position of
1334 the decl. */
1336 tree
1337 create_index_type (tree min, tree max, tree index, Node_Id gnat_node)
1339 /* First build a type for the desired range. */
1340 tree type = build_index_2_type (min, max);
1342 /* If this type has the TYPE_INDEX_TYPE we want, return it. Otherwise, if it
1343 doesn't have TYPE_INDEX_TYPE set, set it to INDEX. If TYPE_INDEX_TYPE
1344 is set, but not to INDEX, make a copy of this type with the requested
1345 index type. Note that we have no way of sharing these types, but that's
1346 only a small hole. */
1347 if (TYPE_INDEX_TYPE (type) == index)
1348 return type;
1349 else if (TYPE_INDEX_TYPE (type))
1350 type = copy_type (type);
1352 SET_TYPE_INDEX_TYPE (type, index);
1353 create_type_decl (NULL_TREE, type, NULL, true, false, gnat_node);
1354 return type;
1357 /* Return a TYPE_DECL node. TYPE_NAME gives the name of the type (a character
1358 string) and TYPE is a ..._TYPE node giving its data type.
1359 ARTIFICIAL_P is true if this is a declaration that was generated
1360 by the compiler. DEBUG_INFO_P is true if we need to write debugging
1361 information about this type. GNAT_NODE is used for the position of
1362 the decl. */
1364 tree
1365 create_type_decl (tree type_name, tree type, struct attrib *attr_list,
1366 bool artificial_p, bool debug_info_p, Node_Id gnat_node)
1368 tree type_decl = build_decl (TYPE_DECL, type_name, type);
1369 enum tree_code code = TREE_CODE (type);
1371 DECL_ARTIFICIAL (type_decl) = artificial_p;
1373 if (!TYPE_IS_DUMMY_P (type))
1374 gnat_pushdecl (type_decl, gnat_node);
1376 process_attributes (type_decl, attr_list);
1378 /* Pass type declaration information to the debugger unless this is an
1379 UNCONSTRAINED_ARRAY_TYPE, which the debugger does not support,
1380 and ENUMERAL_TYPE or RECORD_TYPE which is handled separately, or
1381 type for which debugging information was not requested. */
1382 if (code == UNCONSTRAINED_ARRAY_TYPE || !debug_info_p)
1383 DECL_IGNORED_P (type_decl) = 1;
1384 else if (code != ENUMERAL_TYPE
1385 && (code != RECORD_TYPE || TYPE_IS_FAT_POINTER_P (type))
1386 && !((code == POINTER_TYPE || code == REFERENCE_TYPE)
1387 && TYPE_IS_DUMMY_P (TREE_TYPE (type))))
1388 rest_of_type_decl_compilation (type_decl);
1390 return type_decl;
1393 /* Helper for create_var_decl and create_true_var_decl. Returns a GCC VAR_DECL
1394 or CONST_DECL node.
1396 VAR_NAME gives the name of the variable. ASM_NAME is its assembler name
1397 (if provided). TYPE is its data type (a GCC ..._TYPE node). VAR_INIT is
1398 the GCC tree for an optional initial expression; NULL_TREE if none.
1400 CONST_FLAG is true if this variable is constant, in which case we might
1401 return a CONST_DECL node unless CONST_DECL_ALLOWED_FLAG is false.
1403 PUBLIC_FLAG is true if this definition is to be made visible outside of
1404 the current compilation unit. This flag should be set when processing the
1405 variable definitions in a package specification. EXTERN_FLAG is nonzero
1406 when processing an external variable declaration (as opposed to a
1407 definition: no storage is to be allocated for the variable here).
1409 STATIC_FLAG is only relevant when not at top level. In that case
1410 it indicates whether to always allocate storage to the variable.
1412 GNAT_NODE is used for the position of the decl. */
1414 static tree
1415 create_var_decl_1 (tree var_name, tree asm_name, tree type, tree var_init,
1416 bool const_flag, bool const_decl_allowed_flag,
1417 bool public_flag, bool extern_flag, bool static_flag,
1418 struct attrib *attr_list, Node_Id gnat_node)
1420 bool init_const
1421 = (var_init != 0
1422 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (var_init))
1423 && (global_bindings_p () || static_flag
1424 ? initializer_constant_valid_p (var_init, TREE_TYPE (var_init)) != 0
1425 : TREE_CONSTANT (var_init)));
1427 /* Whether we will make TREE_CONSTANT the DECL we produce here, in which
1428 case the initializer may be used in-lieu of the DECL node (as done in
1429 Identifier_to_gnu). This is useful to prevent the need of elaboration
1430 code when an identifier for which such a decl is made is in turn used as
1431 an initializer. We used to rely on CONST vs VAR_DECL for this purpose,
1432 but extra constraints apply to this choice (see below) and are not
1433 relevant to the distinction we wish to make. */
1434 bool constant_p = const_flag && init_const;
1436 /* The actual DECL node. CONST_DECL was initially intended for enumerals
1437 and may be used for scalars in general but not for aggregates. */
1438 tree var_decl
1439 = build_decl ((constant_p && const_decl_allowed_flag
1440 && !AGGREGATE_TYPE_P (type)) ? CONST_DECL : VAR_DECL,
1441 var_name, type);
1443 /* If this is external, throw away any initializations (they will be done
1444 elsewhere) unless this is a a constant for which we would like to remain
1445 able to get the initializer. If we are defining a global here, leave a
1446 constant initialization and save any variable elaborations for the
1447 elaboration routine. If we are just annotating types, throw away the
1448 initialization if it isn't a constant. */
1449 if ((extern_flag && !constant_p)
1450 || (type_annotate_only && var_init && !TREE_CONSTANT (var_init)))
1451 var_init = NULL_TREE;
1453 /* At the global level, an initializer requiring code to be generated
1454 produces elaboration statements. Check that such statements are allowed,
1455 that is, not violating a No_Elaboration_Code restriction. */
1456 if (global_bindings_p () && var_init != 0 && ! init_const)
1457 Check_Elaboration_Code_Allowed (gnat_node);
1459 /* Ada doesn't feature Fortran-like COMMON variables so we shouldn't
1460 try to fiddle with DECL_COMMON. However, on platforms that don't
1461 support global BSS sections, uninitialized global variables would
1462 go in DATA instead, thus increasing the size of the executable. */
1463 if (!flag_no_common
1464 && TREE_CODE (var_decl) == VAR_DECL
1465 && !have_global_bss_p ())
1466 DECL_COMMON (var_decl) = 1;
1467 DECL_INITIAL (var_decl) = var_init;
1468 TREE_READONLY (var_decl) = const_flag;
1469 DECL_EXTERNAL (var_decl) = extern_flag;
1470 TREE_PUBLIC (var_decl) = public_flag || extern_flag;
1471 TREE_CONSTANT (var_decl) = constant_p;
1472 TREE_THIS_VOLATILE (var_decl) = TREE_SIDE_EFFECTS (var_decl)
1473 = TYPE_VOLATILE (type);
1475 /* If it's public and not external, always allocate storage for it.
1476 At the global binding level we need to allocate static storage for the
1477 variable if and only if it's not external. If we are not at the top level
1478 we allocate automatic storage unless requested not to. */
1479 TREE_STATIC (var_decl)
1480 = public_flag || (global_bindings_p () ? !extern_flag : static_flag);
1482 if (asm_name && VAR_OR_FUNCTION_DECL_P (var_decl))
1483 SET_DECL_ASSEMBLER_NAME (var_decl, asm_name);
1485 process_attributes (var_decl, attr_list);
1487 /* Add this decl to the current binding level. */
1488 gnat_pushdecl (var_decl, gnat_node);
1490 if (TREE_SIDE_EFFECTS (var_decl))
1491 TREE_ADDRESSABLE (var_decl) = 1;
1493 if (TREE_CODE (var_decl) != CONST_DECL)
1495 if (global_bindings_p ())
1496 rest_of_decl_compilation (var_decl, true, 0);
1498 else
1499 expand_decl (var_decl);
1501 return var_decl;
1504 /* Wrapper around create_var_decl_1 for cases where we don't care whether
1505 a VAR or a CONST decl node is created. */
1507 tree
1508 create_var_decl (tree var_name, tree asm_name, tree type, tree var_init,
1509 bool const_flag, bool public_flag, bool extern_flag,
1510 bool static_flag, struct attrib *attr_list,
1511 Node_Id gnat_node)
1513 return create_var_decl_1 (var_name, asm_name, type, var_init,
1514 const_flag, true,
1515 public_flag, extern_flag, static_flag,
1516 attr_list, gnat_node);
1519 /* Wrapper around create_var_decl_1 for cases where a VAR_DECL node is
1520 required. The primary intent is for DECL_CONST_CORRESPONDING_VARs, which
1521 must be VAR_DECLs and on which we want TREE_READONLY set to have them
1522 possibly assigned to a readonly data section. */
1524 tree
1525 create_true_var_decl (tree var_name, tree asm_name, tree type, tree var_init,
1526 bool const_flag, bool public_flag, bool extern_flag,
1527 bool static_flag, struct attrib *attr_list,
1528 Node_Id gnat_node)
1530 return create_var_decl_1 (var_name, asm_name, type, var_init,
1531 const_flag, false,
1532 public_flag, extern_flag, static_flag,
1533 attr_list, gnat_node);
1536 /* Return true if TYPE, an aggregate type, contains (or is) an array. */
1538 static bool
1539 aggregate_type_contains_array_p (tree type)
1541 switch (TREE_CODE (type))
1543 case RECORD_TYPE:
1544 case UNION_TYPE:
1545 case QUAL_UNION_TYPE:
1547 tree field;
1548 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1549 if (AGGREGATE_TYPE_P (TREE_TYPE (field))
1550 && aggregate_type_contains_array_p (TREE_TYPE (field)))
1551 return true;
1552 return false;
1555 case ARRAY_TYPE:
1556 return true;
1558 default:
1559 gcc_unreachable ();
1563 /* Returns a FIELD_DECL node. FIELD_NAME the field name, FIELD_TYPE is its
1564 type, and RECORD_TYPE is the type of the parent. PACKED is nonzero if
1565 this field is in a record type with a "pragma pack". If SIZE is nonzero
1566 it is the specified size for this field. If POS is nonzero, it is the bit
1567 position. If ADDRESSABLE is nonzero, it means we are allowed to take
1568 the address of this field for aliasing purposes. If it is negative, we
1569 should not make a bitfield, which is used by make_aligning_type. */
1571 tree
1572 create_field_decl (tree field_name, tree field_type, tree record_type,
1573 int packed, tree size, tree pos, int addressable)
1575 tree field_decl = build_decl (FIELD_DECL, field_name, field_type);
1577 DECL_CONTEXT (field_decl) = record_type;
1578 TREE_READONLY (field_decl) = TYPE_READONLY (field_type);
1580 /* If FIELD_TYPE is BLKmode, we must ensure this is aligned to at least a
1581 byte boundary since GCC cannot handle less-aligned BLKmode bitfields.
1582 Likewise for an aggregate without specified position that contains an
1583 array, because in this case slices of variable length of this array
1584 must be handled by GCC and variable-sized objects need to be aligned
1585 to at least a byte boundary. */
1586 if (packed && (TYPE_MODE (field_type) == BLKmode
1587 || (!pos
1588 && AGGREGATE_TYPE_P (field_type)
1589 && aggregate_type_contains_array_p (field_type))))
1590 DECL_ALIGN (field_decl) = BITS_PER_UNIT;
1592 /* If a size is specified, use it. Otherwise, if the record type is packed
1593 compute a size to use, which may differ from the object's natural size.
1594 We always set a size in this case to trigger the checks for bitfield
1595 creation below, which is typically required when no position has been
1596 specified. */
1597 if (size)
1598 size = convert (bitsizetype, size);
1599 else if (packed == 1)
1601 size = rm_size (field_type);
1603 /* For a constant size larger than MAX_FIXED_MODE_SIZE, round up to
1604 byte. */
1605 if (TREE_CODE (size) == INTEGER_CST
1606 && compare_tree_int (size, MAX_FIXED_MODE_SIZE) > 0)
1607 size = round_up (size, BITS_PER_UNIT);
1610 /* If we may, according to ADDRESSABLE, make a bitfield if a size is
1611 specified for two reasons: first if the size differs from the natural
1612 size. Second, if the alignment is insufficient. There are a number of
1613 ways the latter can be true.
1615 We never make a bitfield if the type of the field has a nonconstant size,
1616 because no such entity requiring bitfield operations should reach here.
1618 We do *preventively* make a bitfield when there might be the need for it
1619 but we don't have all the necessary information to decide, as is the case
1620 of a field with no specified position in a packed record.
1622 We also don't look at STRICT_ALIGNMENT here, and rely on later processing
1623 in layout_decl or finish_record_type to clear the bit_field indication if
1624 it is in fact not needed. */
1625 if (addressable >= 0
1626 && size
1627 && TREE_CODE (size) == INTEGER_CST
1628 && TREE_CODE (TYPE_SIZE (field_type)) == INTEGER_CST
1629 && (!tree_int_cst_equal (size, TYPE_SIZE (field_type))
1630 || (pos && !value_factor_p (pos, TYPE_ALIGN (field_type)))
1631 || packed
1632 || (TYPE_ALIGN (record_type) != 0
1633 && TYPE_ALIGN (record_type) < TYPE_ALIGN (field_type))))
1635 DECL_BIT_FIELD (field_decl) = 1;
1636 DECL_SIZE (field_decl) = size;
1637 if (!packed && !pos)
1638 DECL_ALIGN (field_decl)
1639 = (TYPE_ALIGN (record_type) != 0
1640 ? MIN (TYPE_ALIGN (record_type), TYPE_ALIGN (field_type))
1641 : TYPE_ALIGN (field_type));
1644 DECL_PACKED (field_decl) = pos ? DECL_BIT_FIELD (field_decl) : packed;
1646 /* Bump the alignment if need be, either for bitfield/packing purposes or
1647 to satisfy the type requirements if no such consideration applies. When
1648 we get the alignment from the type, indicate if this is from an explicit
1649 user request, which prevents stor-layout from lowering it later on. */
1651 int bit_align
1652 = (DECL_BIT_FIELD (field_decl) ? 1
1653 : packed && TYPE_MODE (field_type) != BLKmode ? BITS_PER_UNIT : 0);
1655 if (bit_align > DECL_ALIGN (field_decl))
1656 DECL_ALIGN (field_decl) = bit_align;
1657 else if (!bit_align && TYPE_ALIGN (field_type) > DECL_ALIGN (field_decl))
1659 DECL_ALIGN (field_decl) = TYPE_ALIGN (field_type);
1660 DECL_USER_ALIGN (field_decl) = TYPE_USER_ALIGN (field_type);
1664 if (pos)
1666 /* We need to pass in the alignment the DECL is known to have.
1667 This is the lowest-order bit set in POS, but no more than
1668 the alignment of the record, if one is specified. Note
1669 that an alignment of 0 is taken as infinite. */
1670 unsigned int known_align;
1672 if (host_integerp (pos, 1))
1673 known_align = tree_low_cst (pos, 1) & - tree_low_cst (pos, 1);
1674 else
1675 known_align = BITS_PER_UNIT;
1677 if (TYPE_ALIGN (record_type)
1678 && (known_align == 0 || known_align > TYPE_ALIGN (record_type)))
1679 known_align = TYPE_ALIGN (record_type);
1681 layout_decl (field_decl, known_align);
1682 SET_DECL_OFFSET_ALIGN (field_decl,
1683 host_integerp (pos, 1) ? BIGGEST_ALIGNMENT
1684 : BITS_PER_UNIT);
1685 pos_from_bit (&DECL_FIELD_OFFSET (field_decl),
1686 &DECL_FIELD_BIT_OFFSET (field_decl),
1687 DECL_OFFSET_ALIGN (field_decl), pos);
1689 DECL_HAS_REP_P (field_decl) = 1;
1692 /* In addition to what our caller says, claim the field is addressable if we
1693 know that its type is not suitable.
1695 The field may also be "technically" nonaddressable, meaning that even if
1696 we attempt to take the field's address we will actually get the address
1697 of a copy. This is the case for true bitfields, but the DECL_BIT_FIELD
1698 value we have at this point is not accurate enough, so we don't account
1699 for this here and let finish_record_type decide. */
1700 if (!type_for_nonaliased_component_p (field_type))
1701 addressable = 1;
1703 DECL_NONADDRESSABLE_P (field_decl) = !addressable;
1705 return field_decl;
1708 /* Returns a PARM_DECL node. PARAM_NAME is the name of the parameter,
1709 PARAM_TYPE is its type. READONLY is true if the parameter is
1710 readonly (either an In parameter or an address of a pass-by-ref
1711 parameter). */
1713 tree
1714 create_param_decl (tree param_name, tree param_type, bool readonly)
1716 tree param_decl = build_decl (PARM_DECL, param_name, param_type);
1718 /* Honor targetm.calls.promote_prototypes(), as not doing so can
1719 lead to various ABI violations. */
1720 if (targetm.calls.promote_prototypes (param_type)
1721 && (TREE_CODE (param_type) == INTEGER_TYPE
1722 || TREE_CODE (param_type) == ENUMERAL_TYPE)
1723 && TYPE_PRECISION (param_type) < TYPE_PRECISION (integer_type_node))
1725 /* We have to be careful about biased types here. Make a subtype
1726 of integer_type_node with the proper biasing. */
1727 if (TREE_CODE (param_type) == INTEGER_TYPE
1728 && TYPE_BIASED_REPRESENTATION_P (param_type))
1730 param_type
1731 = copy_type (build_range_type (integer_type_node,
1732 TYPE_MIN_VALUE (param_type),
1733 TYPE_MAX_VALUE (param_type)));
1735 TYPE_BIASED_REPRESENTATION_P (param_type) = 1;
1737 else
1738 param_type = integer_type_node;
1741 DECL_ARG_TYPE (param_decl) = param_type;
1742 TREE_READONLY (param_decl) = readonly;
1743 return param_decl;
1746 /* Given a DECL and ATTR_LIST, process the listed attributes. */
1748 void
1749 process_attributes (tree decl, struct attrib *attr_list)
1751 for (; attr_list; attr_list = attr_list->next)
1752 switch (attr_list->type)
1754 case ATTR_MACHINE_ATTRIBUTE:
1755 decl_attributes (&decl, tree_cons (attr_list->name, attr_list->args,
1756 NULL_TREE),
1757 ATTR_FLAG_TYPE_IN_PLACE);
1758 break;
1760 case ATTR_LINK_ALIAS:
1761 if (! DECL_EXTERNAL (decl))
1763 TREE_STATIC (decl) = 1;
1764 assemble_alias (decl, attr_list->name);
1766 break;
1768 case ATTR_WEAK_EXTERNAL:
1769 if (SUPPORTS_WEAK)
1770 declare_weak (decl);
1771 else
1772 post_error ("?weak declarations not supported on this target",
1773 attr_list->error_point);
1774 break;
1776 case ATTR_LINK_SECTION:
1777 if (targetm.have_named_sections)
1779 DECL_SECTION_NAME (decl)
1780 = build_string (IDENTIFIER_LENGTH (attr_list->name),
1781 IDENTIFIER_POINTER (attr_list->name));
1782 DECL_COMMON (decl) = 0;
1784 else
1785 post_error ("?section attributes are not supported for this target",
1786 attr_list->error_point);
1787 break;
1789 case ATTR_LINK_CONSTRUCTOR:
1790 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1791 TREE_USED (decl) = 1;
1792 break;
1794 case ATTR_LINK_DESTRUCTOR:
1795 DECL_STATIC_DESTRUCTOR (decl) = 1;
1796 TREE_USED (decl) = 1;
1797 break;
1801 /* Record a global renaming pointer. */
1803 void
1804 record_global_renaming_pointer (tree decl)
1806 gcc_assert (DECL_RENAMED_OBJECT (decl));
1807 VEC_safe_push (tree, gc, global_renaming_pointers, decl);
1810 /* Invalidate the global renaming pointers. */
1812 void
1813 invalidate_global_renaming_pointers (void)
1815 unsigned int i;
1816 tree iter;
1818 for (i = 0; VEC_iterate(tree, global_renaming_pointers, i, iter); i++)
1819 SET_DECL_RENAMED_OBJECT (iter, NULL_TREE);
1821 VEC_free (tree, gc, global_renaming_pointers);
1824 /* Return true if VALUE is a known to be a multiple of FACTOR, which must be
1825 a power of 2. */
1827 bool
1828 value_factor_p (tree value, HOST_WIDE_INT factor)
1830 if (host_integerp (value, 1))
1831 return tree_low_cst (value, 1) % factor == 0;
1833 if (TREE_CODE (value) == MULT_EXPR)
1834 return (value_factor_p (TREE_OPERAND (value, 0), factor)
1835 || value_factor_p (TREE_OPERAND (value, 1), factor));
1837 return false;
1840 /* Given 2 consecutive field decls PREV_FIELD and CURR_FIELD, return true
1841 unless we can prove these 2 fields are laid out in such a way that no gap
1842 exist between the end of PREV_FIELD and the beginning of CURR_FIELD. OFFSET
1843 is the distance in bits between the end of PREV_FIELD and the starting
1844 position of CURR_FIELD. It is ignored if null. */
1846 static bool
1847 potential_alignment_gap (tree prev_field, tree curr_field, tree offset)
1849 /* If this is the first field of the record, there cannot be any gap */
1850 if (!prev_field)
1851 return false;
1853 /* If the previous field is a union type, then return False: The only
1854 time when such a field is not the last field of the record is when
1855 there are other components at fixed positions after it (meaning there
1856 was a rep clause for every field), in which case we don't want the
1857 alignment constraint to override them. */
1858 if (TREE_CODE (TREE_TYPE (prev_field)) == QUAL_UNION_TYPE)
1859 return false;
1861 /* If the distance between the end of prev_field and the beginning of
1862 curr_field is constant, then there is a gap if the value of this
1863 constant is not null. */
1864 if (offset && host_integerp (offset, 1))
1865 return !integer_zerop (offset);
1867 /* If the size and position of the previous field are constant,
1868 then check the sum of this size and position. There will be a gap
1869 iff it is not multiple of the current field alignment. */
1870 if (host_integerp (DECL_SIZE (prev_field), 1)
1871 && host_integerp (bit_position (prev_field), 1))
1872 return ((tree_low_cst (bit_position (prev_field), 1)
1873 + tree_low_cst (DECL_SIZE (prev_field), 1))
1874 % DECL_ALIGN (curr_field) != 0);
1876 /* If both the position and size of the previous field are multiples
1877 of the current field alignment, there cannot be any gap. */
1878 if (value_factor_p (bit_position (prev_field), DECL_ALIGN (curr_field))
1879 && value_factor_p (DECL_SIZE (prev_field), DECL_ALIGN (curr_field)))
1880 return false;
1882 /* Fallback, return that there may be a potential gap */
1883 return true;
1886 /* Returns a LABEL_DECL node for LABEL_NAME. */
1888 tree
1889 create_label_decl (tree label_name)
1891 tree label_decl = build_decl (LABEL_DECL, label_name, void_type_node);
1893 DECL_CONTEXT (label_decl) = current_function_decl;
1894 DECL_MODE (label_decl) = VOIDmode;
1895 DECL_SOURCE_LOCATION (label_decl) = input_location;
1897 return label_decl;
1900 /* Returns a FUNCTION_DECL node. SUBPROG_NAME is the name of the subprogram,
1901 ASM_NAME is its assembler name, SUBPROG_TYPE is its type (a FUNCTION_TYPE
1902 node), PARAM_DECL_LIST is the list of the subprogram arguments (a list of
1903 PARM_DECL nodes chained through the TREE_CHAIN field).
1905 INLINE_FLAG, PUBLIC_FLAG, EXTERN_FLAG, and ATTR_LIST are used to set the
1906 appropriate fields in the FUNCTION_DECL. GNAT_NODE gives the location. */
1908 tree
1909 create_subprog_decl (tree subprog_name, tree asm_name,
1910 tree subprog_type, tree param_decl_list, bool inline_flag,
1911 bool public_flag, bool extern_flag,
1912 struct attrib *attr_list, Node_Id gnat_node)
1914 tree return_type = TREE_TYPE (subprog_type);
1915 tree subprog_decl = build_decl (FUNCTION_DECL, subprog_name, subprog_type);
1917 /* If this is a function nested inside an inlined external function, it
1918 means we aren't going to compile the outer function unless it is
1919 actually inlined, so do the same for us. */
1920 if (current_function_decl && DECL_INLINE (current_function_decl)
1921 && DECL_EXTERNAL (current_function_decl))
1922 extern_flag = true;
1924 DECL_EXTERNAL (subprog_decl) = extern_flag;
1925 TREE_PUBLIC (subprog_decl) = public_flag;
1926 TREE_STATIC (subprog_decl) = 1;
1927 TREE_READONLY (subprog_decl) = TYPE_READONLY (subprog_type);
1928 TREE_THIS_VOLATILE (subprog_decl) = TYPE_VOLATILE (subprog_type);
1929 TREE_SIDE_EFFECTS (subprog_decl) = TYPE_VOLATILE (subprog_type);
1930 DECL_ARGUMENTS (subprog_decl) = param_decl_list;
1931 DECL_RESULT (subprog_decl) = build_decl (RESULT_DECL, 0, return_type);
1932 DECL_ARTIFICIAL (DECL_RESULT (subprog_decl)) = 1;
1933 DECL_IGNORED_P (DECL_RESULT (subprog_decl)) = 1;
1935 /* TREE_ADDRESSABLE is set on the result type to request the use of the
1936 target by-reference return mechanism. This is not supported all the
1937 way down to RTL expansion with GCC 4, which ICEs on temporary creation
1938 attempts with such a type and expects DECL_BY_REFERENCE to be set on
1939 the RESULT_DECL instead - see gnat_genericize for more details. */
1940 if (TREE_ADDRESSABLE (TREE_TYPE (DECL_RESULT (subprog_decl))))
1942 tree result_decl = DECL_RESULT (subprog_decl);
1944 TREE_ADDRESSABLE (TREE_TYPE (result_decl)) = 0;
1945 DECL_BY_REFERENCE (result_decl) = 1;
1948 if (inline_flag)
1949 DECL_DECLARED_INLINE_P (subprog_decl) = 1;
1951 if (asm_name)
1952 SET_DECL_ASSEMBLER_NAME (subprog_decl, asm_name);
1954 process_attributes (subprog_decl, attr_list);
1956 /* Add this decl to the current binding level. */
1957 gnat_pushdecl (subprog_decl, gnat_node);
1959 /* Output the assembler code and/or RTL for the declaration. */
1960 rest_of_decl_compilation (subprog_decl, global_bindings_p (), 0);
1962 return subprog_decl;
1965 /* Set up the framework for generating code for SUBPROG_DECL, a subprogram
1966 body. This routine needs to be invoked before processing the declarations
1967 appearing in the subprogram. */
1969 void
1970 begin_subprog_body (tree subprog_decl)
1972 tree param_decl;
1974 current_function_decl = subprog_decl;
1975 announce_function (subprog_decl);
1977 /* Enter a new binding level and show that all the parameters belong to
1978 this function. */
1979 gnat_pushlevel ();
1980 for (param_decl = DECL_ARGUMENTS (subprog_decl); param_decl;
1981 param_decl = TREE_CHAIN (param_decl))
1982 DECL_CONTEXT (param_decl) = subprog_decl;
1984 make_decl_rtl (subprog_decl);
1986 /* We handle pending sizes via the elaboration of types, so we don't need to
1987 save them. This causes them to be marked as part of the outer function
1988 and then discarded. */
1989 get_pending_sizes ();
1993 /* Helper for the genericization callback. Return a dereference of VAL
1994 if it is of a reference type. */
1996 static tree
1997 convert_from_reference (tree val)
1999 tree value_type, ref;
2001 if (TREE_CODE (TREE_TYPE (val)) != REFERENCE_TYPE)
2002 return val;
2004 value_type = TREE_TYPE (TREE_TYPE (val));
2005 ref = build1 (INDIRECT_REF, value_type, val);
2007 /* See if what we reference is CONST or VOLATILE, which requires
2008 looking into array types to get to the component type. */
2010 while (TREE_CODE (value_type) == ARRAY_TYPE)
2011 value_type = TREE_TYPE (value_type);
2013 TREE_READONLY (ref)
2014 = (TYPE_QUALS (value_type) & TYPE_QUAL_CONST);
2015 TREE_THIS_VOLATILE (ref)
2016 = (TYPE_QUALS (value_type) & TYPE_QUAL_VOLATILE);
2018 TREE_SIDE_EFFECTS (ref)
2019 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
2021 return ref;
2024 /* Helper for the genericization callback. Returns true if T denotes
2025 a RESULT_DECL with DECL_BY_REFERENCE set. */
2027 static inline bool
2028 is_byref_result (tree t)
2030 return (TREE_CODE (t) == RESULT_DECL && DECL_BY_REFERENCE (t));
2034 /* Tree walking callback for gnat_genericize. Currently ...
2036 o Adjust references to the function's DECL_RESULT if it is marked
2037 DECL_BY_REFERENCE and so has had its type turned into a reference
2038 type at the end of the function compilation. */
2040 static tree
2041 gnat_genericize_r (tree *stmt_p, int *walk_subtrees, void *data)
2043 /* This implementation is modeled after what the C++ front-end is
2044 doing, basis of the downstream passes behavior. */
2046 tree stmt = *stmt_p;
2047 struct pointer_set_t *p_set = (struct pointer_set_t*) data;
2049 /* If we have a direct mention of the result decl, dereference. */
2050 if (is_byref_result (stmt))
2052 *stmt_p = convert_from_reference (stmt);
2053 *walk_subtrees = 0;
2054 return NULL;
2057 /* Otherwise, no need to walk the the same tree twice. */
2058 if (pointer_set_contains (p_set, stmt))
2060 *walk_subtrees = 0;
2061 return NULL_TREE;
2064 /* If we are taking the address of what now is a reference, just get the
2065 reference value. */
2066 if (TREE_CODE (stmt) == ADDR_EXPR
2067 && is_byref_result (TREE_OPERAND (stmt, 0)))
2069 *stmt_p = convert (TREE_TYPE (stmt), TREE_OPERAND (stmt, 0));
2070 *walk_subtrees = 0;
2073 /* Don't dereference an by-reference RESULT_DECL inside a RETURN_EXPR. */
2074 else if (TREE_CODE (stmt) == RETURN_EXPR
2075 && TREE_OPERAND (stmt, 0)
2076 && is_byref_result (TREE_OPERAND (stmt, 0)))
2077 *walk_subtrees = 0;
2079 /* Don't look inside trees that cannot embed references of interest. */
2080 else if (IS_TYPE_OR_DECL_P (stmt))
2081 *walk_subtrees = 0;
2083 pointer_set_insert (p_set, *stmt_p);
2085 return NULL;
2088 /* Perform lowering of Ada trees to GENERIC. In particular:
2090 o Turn a DECL_BY_REFERENCE RESULT_DECL into a real by-reference decl
2091 and adjust all the references to this decl accordingly. */
2093 static void
2094 gnat_genericize (tree fndecl)
2096 /* Prior to GCC 4, an explicit By_Reference result mechanism for a function
2097 was handled by simply setting TREE_ADDRESSABLE on the result type.
2098 Everything required to actually pass by invisible ref using the target
2099 mechanism (e.g. extra parameter) was handled at RTL expansion time.
2101 This doesn't work with GCC 4 any more for several reasons. First, the
2102 gimplification process might need the creation of temporaries of this
2103 type, and the gimplifier ICEs on such attempts. Second, the middle-end
2104 now relies on a different attribute for such cases (DECL_BY_REFERENCE on
2105 RESULT/PARM_DECLs), and expects the user invisible by-reference-ness to
2106 be explicitely accounted for by the front-end in the function body.
2108 We achieve the complete transformation in two steps:
2110 1/ create_subprog_decl performs early attribute tweaks: it clears
2111 TREE_ADDRESSABLE from the result type and sets DECL_BY_REFERENCE on
2112 the result decl. The former ensures that the bit isn't set in the GCC
2113 tree saved for the function, so prevents ICEs on temporary creation.
2114 The latter we use here to trigger the rest of the processing.
2116 2/ This function performs the type transformation on the result decl
2117 and adjusts all the references to this decl from the function body
2118 accordingly.
2120 Clearing TREE_ADDRESSABLE from the type differs from the C++ front-end
2121 strategy, which escapes the gimplifier temporary creation issues by
2122 creating it's own temporaries using TARGET_EXPR nodes. Our way relies
2123 on simple specific support code in aggregate_value_p to look at the
2124 target function result decl explicitely. */
2126 struct pointer_set_t *p_set;
2127 tree decl_result = DECL_RESULT (fndecl);
2129 if (!DECL_BY_REFERENCE (decl_result))
2130 return;
2132 /* Make the DECL_RESULT explicitely by-reference and adjust all the
2133 occurrences in the function body using the common tree-walking facility.
2134 We want to see every occurrence of the result decl to adjust the
2135 referencing tree, so need to use our own pointer set to control which
2136 trees should be visited again or not. */
2138 p_set = pointer_set_create ();
2140 TREE_TYPE (decl_result) = build_reference_type (TREE_TYPE (decl_result));
2141 TREE_ADDRESSABLE (decl_result) = 0;
2142 relayout_decl (decl_result);
2144 walk_tree (&DECL_SAVED_TREE (fndecl), gnat_genericize_r, p_set, NULL);
2146 pointer_set_destroy (p_set);
2149 /* Finish the definition of the current subprogram and compile it all the way
2150 to assembler language output. BODY is the tree corresponding to
2151 the subprogram. */
2153 void
2154 end_subprog_body (tree body)
2156 tree fndecl = current_function_decl;
2158 /* Mark the BLOCK for this level as being for this function and pop the
2159 level. Since the vars in it are the parameters, clear them. */
2160 BLOCK_VARS (current_binding_level->block) = 0;
2161 BLOCK_SUPERCONTEXT (current_binding_level->block) = fndecl;
2162 DECL_INITIAL (fndecl) = current_binding_level->block;
2163 gnat_poplevel ();
2165 /* Deal with inline. If declared inline or we should default to inline,
2166 set the flag in the decl. */
2167 DECL_INLINE (fndecl)
2168 = DECL_DECLARED_INLINE_P (fndecl) || flag_inline_trees == 2;
2170 /* We handle pending sizes via the elaboration of types, so we don't
2171 need to save them. */
2172 get_pending_sizes ();
2174 /* Mark the RESULT_DECL as being in this subprogram. */
2175 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
2177 DECL_SAVED_TREE (fndecl) = body;
2179 current_function_decl = DECL_CONTEXT (fndecl);
2180 set_cfun (NULL);
2182 /* We cannot track the location of errors past this point. */
2183 error_gnat_node = Empty;
2185 /* If we're only annotating types, don't actually compile this function. */
2186 if (type_annotate_only)
2187 return;
2189 /* Perform the required pre-gimplfication transformations on the tree. */
2190 gnat_genericize (fndecl);
2192 /* We do different things for nested and non-nested functions.
2193 ??? This should be in cgraph. */
2194 if (!DECL_CONTEXT (fndecl))
2196 gnat_gimplify_function (fndecl);
2197 cgraph_finalize_function (fndecl, false);
2199 else
2200 /* Register this function with cgraph just far enough to get it
2201 added to our parent's nested function list. */
2202 (void) cgraph_node (fndecl);
2205 /* Convert FNDECL's code to GIMPLE and handle any nested functions. */
2207 static void
2208 gnat_gimplify_function (tree fndecl)
2210 struct cgraph_node *cgn;
2212 dump_function (TDI_original, fndecl);
2213 gimplify_function_tree (fndecl);
2214 dump_function (TDI_generic, fndecl);
2216 /* Convert all nested functions to GIMPLE now. We do things in this order
2217 so that items like VLA sizes are expanded properly in the context of the
2218 correct function. */
2219 cgn = cgraph_node (fndecl);
2220 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
2221 gnat_gimplify_function (cgn->decl);
2225 tree
2226 gnat_builtin_function (tree decl)
2228 gnat_pushdecl (decl, Empty);
2229 return decl;
2232 /* Handle a "const" attribute; arguments as in
2233 struct attribute_spec.handler. */
2235 static tree
2236 handle_const_attribute (tree *node, tree ARG_UNUSED (name),
2237 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
2238 bool *no_add_attrs)
2240 if (TREE_CODE (*node) == FUNCTION_DECL)
2241 TREE_READONLY (*node) = 1;
2242 else
2243 *no_add_attrs = true;
2245 return NULL_TREE;
2248 /* Handle a "nothrow" attribute; arguments as in
2249 struct attribute_spec.handler. */
2251 static tree
2252 handle_nothrow_attribute (tree *node, tree ARG_UNUSED (name),
2253 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
2254 bool *no_add_attrs)
2256 if (TREE_CODE (*node) == FUNCTION_DECL)
2257 TREE_NOTHROW (*node) = 1;
2258 else
2259 *no_add_attrs = true;
2261 return NULL_TREE;
2264 /* Return an integer type with the number of bits of precision given by
2265 PRECISION. UNSIGNEDP is nonzero if the type is unsigned; otherwise
2266 it is a signed type. */
2268 tree
2269 gnat_type_for_size (unsigned precision, int unsignedp)
2271 tree t;
2272 char type_name[20];
2274 if (precision <= 2 * MAX_BITS_PER_WORD
2275 && signed_and_unsigned_types[precision][unsignedp])
2276 return signed_and_unsigned_types[precision][unsignedp];
2278 if (unsignedp)
2279 t = make_unsigned_type (precision);
2280 else
2281 t = make_signed_type (precision);
2283 if (precision <= 2 * MAX_BITS_PER_WORD)
2284 signed_and_unsigned_types[precision][unsignedp] = t;
2286 if (!TYPE_NAME (t))
2288 sprintf (type_name, "%sSIGNED_%d", unsignedp ? "UN" : "", precision);
2289 TYPE_NAME (t) = get_identifier (type_name);
2292 return t;
2295 /* Likewise for floating-point types. */
2297 static tree
2298 float_type_for_precision (int precision, enum machine_mode mode)
2300 tree t;
2301 char type_name[20];
2303 if (float_types[(int) mode])
2304 return float_types[(int) mode];
2306 float_types[(int) mode] = t = make_node (REAL_TYPE);
2307 TYPE_PRECISION (t) = precision;
2308 layout_type (t);
2310 gcc_assert (TYPE_MODE (t) == mode);
2311 if (!TYPE_NAME (t))
2313 sprintf (type_name, "FLOAT_%d", precision);
2314 TYPE_NAME (t) = get_identifier (type_name);
2317 return t;
2320 /* Return a data type that has machine mode MODE. UNSIGNEDP selects
2321 an unsigned type; otherwise a signed type is returned. */
2323 tree
2324 gnat_type_for_mode (enum machine_mode mode, int unsignedp)
2326 if (mode == BLKmode)
2327 return NULL_TREE;
2328 else if (mode == VOIDmode)
2329 return void_type_node;
2330 else if (COMPLEX_MODE_P (mode))
2331 return NULL_TREE;
2332 else if (SCALAR_FLOAT_MODE_P (mode))
2333 return float_type_for_precision (GET_MODE_PRECISION (mode), mode);
2334 else if (SCALAR_INT_MODE_P (mode))
2335 return gnat_type_for_size (GET_MODE_BITSIZE (mode), unsignedp);
2336 else
2337 return NULL_TREE;
2340 /* Return the unsigned version of a TYPE_NODE, a scalar type. */
2342 tree
2343 gnat_unsigned_type (tree type_node)
2345 tree type = gnat_type_for_size (TYPE_PRECISION (type_node), 1);
2347 if (TREE_CODE (type_node) == INTEGER_TYPE && TYPE_MODULAR_P (type_node))
2349 type = copy_node (type);
2350 TREE_TYPE (type) = type_node;
2352 else if (TREE_TYPE (type_node)
2353 && TREE_CODE (TREE_TYPE (type_node)) == INTEGER_TYPE
2354 && TYPE_MODULAR_P (TREE_TYPE (type_node)))
2356 type = copy_node (type);
2357 TREE_TYPE (type) = TREE_TYPE (type_node);
2360 return type;
2363 /* Return the signed version of a TYPE_NODE, a scalar type. */
2365 tree
2366 gnat_signed_type (tree type_node)
2368 tree type = gnat_type_for_size (TYPE_PRECISION (type_node), 0);
2370 if (TREE_CODE (type_node) == INTEGER_TYPE && TYPE_MODULAR_P (type_node))
2372 type = copy_node (type);
2373 TREE_TYPE (type) = type_node;
2375 else if (TREE_TYPE (type_node)
2376 && TREE_CODE (TREE_TYPE (type_node)) == INTEGER_TYPE
2377 && TYPE_MODULAR_P (TREE_TYPE (type_node)))
2379 type = copy_node (type);
2380 TREE_TYPE (type) = TREE_TYPE (type_node);
2383 return type;
2387 /* EXP is an expression for the size of an object. If this size contains
2388 discriminant references, replace them with the maximum (if MAX_P) or
2389 minimum (if !MAX_P) possible value of the discriminant. */
2391 tree
2392 max_size (tree exp, bool max_p)
2394 enum tree_code code = TREE_CODE (exp);
2395 tree type = TREE_TYPE (exp);
2397 switch (TREE_CODE_CLASS (code))
2399 case tcc_declaration:
2400 case tcc_constant:
2401 return exp;
2403 case tcc_vl_exp:
2404 if (code == CALL_EXPR)
2406 tree *argarray;
2407 int i, n = call_expr_nargs (exp);
2408 gcc_assert (n > 0);
2410 argarray = (tree *) alloca (n * sizeof (tree));
2411 for (i = 0; i < n; i++)
2412 argarray[i] = max_size (CALL_EXPR_ARG (exp, i), max_p);
2413 return build_call_array (type, CALL_EXPR_FN (exp), n, argarray);
2415 break;
2417 case tcc_reference:
2418 /* If this contains a PLACEHOLDER_EXPR, it is the thing we want to
2419 modify. Otherwise, we treat it like a variable. */
2420 if (!CONTAINS_PLACEHOLDER_P (exp))
2421 return exp;
2423 type = TREE_TYPE (TREE_OPERAND (exp, 1));
2424 return
2425 max_size (max_p ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type), true);
2427 case tcc_comparison:
2428 return max_p ? size_one_node : size_zero_node;
2430 case tcc_unary:
2431 case tcc_binary:
2432 case tcc_expression:
2433 switch (TREE_CODE_LENGTH (code))
2435 case 1:
2436 if (code == NON_LVALUE_EXPR)
2437 return max_size (TREE_OPERAND (exp, 0), max_p);
2438 else
2439 return
2440 fold_build1 (code, type,
2441 max_size (TREE_OPERAND (exp, 0),
2442 code == NEGATE_EXPR ? !max_p : max_p));
2444 case 2:
2445 if (code == COMPOUND_EXPR)
2446 return max_size (TREE_OPERAND (exp, 1), max_p);
2448 /* Calculate "(A ? B : C) - D" as "A ? B - D : C - D" which
2449 may provide a tighter bound on max_size. */
2450 if (code == MINUS_EXPR
2451 && TREE_CODE (TREE_OPERAND (exp, 0)) == COND_EXPR)
2453 tree lhs = fold_build2 (MINUS_EXPR, type,
2454 TREE_OPERAND (TREE_OPERAND (exp, 0), 1),
2455 TREE_OPERAND (exp, 1));
2456 tree rhs = fold_build2 (MINUS_EXPR, type,
2457 TREE_OPERAND (TREE_OPERAND (exp, 0), 2),
2458 TREE_OPERAND (exp, 1));
2459 return fold_build2 (max_p ? MAX_EXPR : MIN_EXPR, type,
2460 max_size (lhs, max_p),
2461 max_size (rhs, max_p));
2465 tree lhs = max_size (TREE_OPERAND (exp, 0), max_p);
2466 tree rhs = max_size (TREE_OPERAND (exp, 1),
2467 code == MINUS_EXPR ? !max_p : max_p);
2469 /* Special-case wanting the maximum value of a MIN_EXPR.
2470 In that case, if one side overflows, return the other.
2471 sizetype is signed, but we know sizes are non-negative.
2472 Likewise, handle a MINUS_EXPR or PLUS_EXPR with the LHS
2473 overflowing or the maximum possible value and the RHS
2474 a variable. */
2475 if (max_p
2476 && code == MIN_EXPR
2477 && TREE_CODE (rhs) == INTEGER_CST
2478 && TREE_OVERFLOW (rhs))
2479 return lhs;
2480 else if (max_p
2481 && code == MIN_EXPR
2482 && TREE_CODE (lhs) == INTEGER_CST
2483 && TREE_OVERFLOW (lhs))
2484 return rhs;
2485 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
2486 && ((TREE_CODE (lhs) == INTEGER_CST
2487 && TREE_OVERFLOW (lhs))
2488 || operand_equal_p (lhs, TYPE_MAX_VALUE (type), 0))
2489 && !TREE_CONSTANT (rhs))
2490 return lhs;
2491 else
2492 return fold_build2 (code, type, lhs, rhs);
2495 case 3:
2496 if (code == SAVE_EXPR)
2497 return exp;
2498 else if (code == COND_EXPR)
2499 return fold_build2 (max_p ? MAX_EXPR : MIN_EXPR, type,
2500 max_size (TREE_OPERAND (exp, 1), max_p),
2501 max_size (TREE_OPERAND (exp, 2), max_p));
2504 /* Other tree classes cannot happen. */
2505 default:
2506 break;
2509 gcc_unreachable ();
2512 /* Build a template of type TEMPLATE_TYPE from the array bounds of ARRAY_TYPE.
2513 EXPR is an expression that we can use to locate any PLACEHOLDER_EXPRs.
2514 Return a constructor for the template. */
2516 tree
2517 build_template (tree template_type, tree array_type, tree expr)
2519 tree template_elts = NULL_TREE;
2520 tree bound_list = NULL_TREE;
2521 tree field;
2523 while (TREE_CODE (array_type) == RECORD_TYPE
2524 && (TYPE_IS_PADDING_P (array_type)
2525 || TYPE_JUSTIFIED_MODULAR_P (array_type)))
2526 array_type = TREE_TYPE (TYPE_FIELDS (array_type));
2528 if (TREE_CODE (array_type) == ARRAY_TYPE
2529 || (TREE_CODE (array_type) == INTEGER_TYPE
2530 && TYPE_HAS_ACTUAL_BOUNDS_P (array_type)))
2531 bound_list = TYPE_ACTUAL_BOUNDS (array_type);
2533 /* First make the list for a CONSTRUCTOR for the template. Go down the
2534 field list of the template instead of the type chain because this
2535 array might be an Ada array of arrays and we can't tell where the
2536 nested arrays stop being the underlying object. */
2538 for (field = TYPE_FIELDS (template_type); field;
2539 (bound_list
2540 ? (bound_list = TREE_CHAIN (bound_list))
2541 : (array_type = TREE_TYPE (array_type))),
2542 field = TREE_CHAIN (TREE_CHAIN (field)))
2544 tree bounds, min, max;
2546 /* If we have a bound list, get the bounds from there. Likewise
2547 for an ARRAY_TYPE. Otherwise, if expr is a PARM_DECL with
2548 DECL_BY_COMPONENT_PTR_P, use the bounds of the field in the template.
2549 This will give us a maximum range. */
2550 if (bound_list)
2551 bounds = TREE_VALUE (bound_list);
2552 else if (TREE_CODE (array_type) == ARRAY_TYPE)
2553 bounds = TYPE_INDEX_TYPE (TYPE_DOMAIN (array_type));
2554 else if (expr && TREE_CODE (expr) == PARM_DECL
2555 && DECL_BY_COMPONENT_PTR_P (expr))
2556 bounds = TREE_TYPE (field);
2557 else
2558 gcc_unreachable ();
2560 min = convert (TREE_TYPE (field), TYPE_MIN_VALUE (bounds));
2561 max = convert (TREE_TYPE (TREE_CHAIN (field)), TYPE_MAX_VALUE (bounds));
2563 /* If either MIN or MAX involve a PLACEHOLDER_EXPR, we must
2564 substitute it from OBJECT. */
2565 min = SUBSTITUTE_PLACEHOLDER_IN_EXPR (min, expr);
2566 max = SUBSTITUTE_PLACEHOLDER_IN_EXPR (max, expr);
2568 template_elts = tree_cons (TREE_CHAIN (field), max,
2569 tree_cons (field, min, template_elts));
2572 return gnat_build_constructor (template_type, nreverse (template_elts));
2575 /* Build a VMS descriptor from a Mechanism_Type, which must specify
2576 a descriptor type, and the GCC type of an object. Each FIELD_DECL
2577 in the type contains in its DECL_INITIAL the expression to use when
2578 a constructor is made for the type. GNAT_ENTITY is an entity used
2579 to print out an error message if the mechanism cannot be applied to
2580 an object of that type and also for the name. */
2582 tree
2583 build_vms_descriptor (tree type, Mechanism_Type mech, Entity_Id gnat_entity)
2585 tree record_type = make_node (RECORD_TYPE);
2586 tree pointer32_type;
2587 tree field_list = 0;
2588 int class;
2589 int dtype = 0;
2590 tree inner_type;
2591 int ndim;
2592 int i;
2593 tree *idx_arr;
2594 tree tem;
2596 /* If TYPE is an unconstrained array, use the underlying array type. */
2597 if (TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
2598 type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (type))));
2600 /* If this is an array, compute the number of dimensions in the array,
2601 get the index types, and point to the inner type. */
2602 if (TREE_CODE (type) != ARRAY_TYPE)
2603 ndim = 0;
2604 else
2605 for (ndim = 1, inner_type = type;
2606 TREE_CODE (TREE_TYPE (inner_type)) == ARRAY_TYPE
2607 && TYPE_MULTI_ARRAY_P (TREE_TYPE (inner_type));
2608 ndim++, inner_type = TREE_TYPE (inner_type))
2611 idx_arr = (tree *) alloca (ndim * sizeof (tree));
2613 if (mech != By_Descriptor_NCA
2614 && TREE_CODE (type) == ARRAY_TYPE && TYPE_CONVENTION_FORTRAN_P (type))
2615 for (i = ndim - 1, inner_type = type;
2616 i >= 0;
2617 i--, inner_type = TREE_TYPE (inner_type))
2618 idx_arr[i] = TYPE_DOMAIN (inner_type);
2619 else
2620 for (i = 0, inner_type = type;
2621 i < ndim;
2622 i++, inner_type = TREE_TYPE (inner_type))
2623 idx_arr[i] = TYPE_DOMAIN (inner_type);
2625 /* Now get the DTYPE value. */
2626 switch (TREE_CODE (type))
2628 case INTEGER_TYPE:
2629 case ENUMERAL_TYPE:
2630 if (TYPE_VAX_FLOATING_POINT_P (type))
2631 switch (tree_low_cst (TYPE_DIGITS_VALUE (type), 1))
2633 case 6:
2634 dtype = 10;
2635 break;
2636 case 9:
2637 dtype = 11;
2638 break;
2639 case 15:
2640 dtype = 27;
2641 break;
2643 else
2644 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
2646 case 8:
2647 dtype = TYPE_UNSIGNED (type) ? 2 : 6;
2648 break;
2649 case 16:
2650 dtype = TYPE_UNSIGNED (type) ? 3 : 7;
2651 break;
2652 case 32:
2653 dtype = TYPE_UNSIGNED (type) ? 4 : 8;
2654 break;
2655 case 64:
2656 dtype = TYPE_UNSIGNED (type) ? 5 : 9;
2657 break;
2658 case 128:
2659 dtype = TYPE_UNSIGNED (type) ? 25 : 26;
2660 break;
2662 break;
2664 case REAL_TYPE:
2665 dtype = GET_MODE_BITSIZE (TYPE_MODE (type)) == 32 ? 52 : 53;
2666 break;
2668 case COMPLEX_TYPE:
2669 if (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
2670 && TYPE_VAX_FLOATING_POINT_P (type))
2671 switch (tree_low_cst (TYPE_DIGITS_VALUE (type), 1))
2673 case 6:
2674 dtype = 12;
2675 break;
2676 case 9:
2677 dtype = 13;
2678 break;
2679 case 15:
2680 dtype = 29;
2682 else
2683 dtype = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) == 32 ? 54: 55;
2684 break;
2686 case ARRAY_TYPE:
2687 dtype = 14;
2688 break;
2690 default:
2691 break;
2694 /* Get the CLASS value. */
2695 switch (mech)
2697 case By_Descriptor_A:
2698 class = 4;
2699 break;
2700 case By_Descriptor_NCA:
2701 class = 10;
2702 break;
2703 case By_Descriptor_SB:
2704 class = 15;
2705 break;
2706 case By_Descriptor:
2707 case By_Descriptor_S:
2708 default:
2709 class = 1;
2710 break;
2713 /* Make the type for a descriptor for VMS. The first four fields
2714 are the same for all types. */
2716 field_list
2717 = chainon (field_list,
2718 make_descriptor_field
2719 ("LENGTH", gnat_type_for_size (16, 1), record_type,
2720 size_in_bytes (mech == By_Descriptor_A ? inner_type : type)));
2722 field_list = chainon (field_list,
2723 make_descriptor_field ("DTYPE",
2724 gnat_type_for_size (8, 1),
2725 record_type, size_int (dtype)));
2726 field_list = chainon (field_list,
2727 make_descriptor_field ("CLASS",
2728 gnat_type_for_size (8, 1),
2729 record_type, size_int (class)));
2731 /* Of course this will crash at run-time if the address space is not
2732 within the low 32 bits, but there is nothing else we can do. */
2733 pointer32_type = build_pointer_type_for_mode (type, SImode, false);
2735 field_list
2736 = chainon (field_list,
2737 make_descriptor_field
2738 ("POINTER", pointer32_type, record_type,
2739 build_unary_op (ADDR_EXPR,
2740 pointer32_type,
2741 build0 (PLACEHOLDER_EXPR, type))));
2743 switch (mech)
2745 case By_Descriptor:
2746 case By_Descriptor_S:
2747 break;
2749 case By_Descriptor_SB:
2750 field_list
2751 = chainon (field_list,
2752 make_descriptor_field
2753 ("SB_L1", gnat_type_for_size (32, 1), record_type,
2754 TREE_CODE (type) == ARRAY_TYPE
2755 ? TYPE_MIN_VALUE (TYPE_DOMAIN (type)) : size_zero_node));
2756 field_list
2757 = chainon (field_list,
2758 make_descriptor_field
2759 ("SB_U1", gnat_type_for_size (32, 1), record_type,
2760 TREE_CODE (type) == ARRAY_TYPE
2761 ? TYPE_MAX_VALUE (TYPE_DOMAIN (type)) : size_zero_node));
2762 break;
2764 case By_Descriptor_A:
2765 case By_Descriptor_NCA:
2766 field_list = chainon (field_list,
2767 make_descriptor_field ("SCALE",
2768 gnat_type_for_size (8, 1),
2769 record_type,
2770 size_zero_node));
2772 field_list = chainon (field_list,
2773 make_descriptor_field ("DIGITS",
2774 gnat_type_for_size (8, 1),
2775 record_type,
2776 size_zero_node));
2778 field_list
2779 = chainon (field_list,
2780 make_descriptor_field
2781 ("AFLAGS", gnat_type_for_size (8, 1), record_type,
2782 size_int (mech == By_Descriptor_NCA
2784 /* Set FL_COLUMN, FL_COEFF, and FL_BOUNDS. */
2785 : (TREE_CODE (type) == ARRAY_TYPE
2786 && TYPE_CONVENTION_FORTRAN_P (type)
2787 ? 224 : 192))));
2789 field_list = chainon (field_list,
2790 make_descriptor_field ("DIMCT",
2791 gnat_type_for_size (8, 1),
2792 record_type,
2793 size_int (ndim)));
2795 field_list = chainon (field_list,
2796 make_descriptor_field ("ARSIZE",
2797 gnat_type_for_size (32, 1),
2798 record_type,
2799 size_in_bytes (type)));
2801 /* Now build a pointer to the 0,0,0... element. */
2802 tem = build0 (PLACEHOLDER_EXPR, type);
2803 for (i = 0, inner_type = type; i < ndim;
2804 i++, inner_type = TREE_TYPE (inner_type))
2805 tem = build4 (ARRAY_REF, TREE_TYPE (inner_type), tem,
2806 convert (TYPE_DOMAIN (inner_type), size_zero_node),
2807 NULL_TREE, NULL_TREE);
2809 field_list
2810 = chainon (field_list,
2811 make_descriptor_field
2812 ("A0",
2813 build_pointer_type_for_mode (inner_type, SImode, false),
2814 record_type,
2815 build1 (ADDR_EXPR,
2816 build_pointer_type_for_mode (inner_type, SImode,
2817 false),
2818 tem)));
2820 /* Next come the addressing coefficients. */
2821 tem = size_one_node;
2822 for (i = 0; i < ndim; i++)
2824 char fname[3];
2825 tree idx_length
2826 = size_binop (MULT_EXPR, tem,
2827 size_binop (PLUS_EXPR,
2828 size_binop (MINUS_EXPR,
2829 TYPE_MAX_VALUE (idx_arr[i]),
2830 TYPE_MIN_VALUE (idx_arr[i])),
2831 size_int (1)));
2833 fname[0] = (mech == By_Descriptor_NCA ? 'S' : 'M');
2834 fname[1] = '0' + i, fname[2] = 0;
2835 field_list
2836 = chainon (field_list,
2837 make_descriptor_field (fname,
2838 gnat_type_for_size (32, 1),
2839 record_type, idx_length));
2841 if (mech == By_Descriptor_NCA)
2842 tem = idx_length;
2845 /* Finally here are the bounds. */
2846 for (i = 0; i < ndim; i++)
2848 char fname[3];
2850 fname[0] = 'L', fname[1] = '0' + i, fname[2] = 0;
2851 field_list
2852 = chainon (field_list,
2853 make_descriptor_field
2854 (fname, gnat_type_for_size (32, 1), record_type,
2855 TYPE_MIN_VALUE (idx_arr[i])));
2857 fname[0] = 'U';
2858 field_list
2859 = chainon (field_list,
2860 make_descriptor_field
2861 (fname, gnat_type_for_size (32, 1), record_type,
2862 TYPE_MAX_VALUE (idx_arr[i])));
2864 break;
2866 default:
2867 post_error ("unsupported descriptor type for &", gnat_entity);
2870 finish_record_type (record_type, field_list, 0, true);
2871 create_type_decl (create_concat_name (gnat_entity, "DESC"), record_type,
2872 NULL, true, false, gnat_entity);
2874 return record_type;
2877 /* Utility routine for above code to make a field. */
2879 static tree
2880 make_descriptor_field (const char *name, tree type,
2881 tree rec_type, tree initial)
2883 tree field
2884 = create_field_decl (get_identifier (name), type, rec_type, 0, 0, 0, 0);
2886 DECL_INITIAL (field) = initial;
2887 return field;
2890 /* Convert GNU_EXPR, a pointer to a VMS descriptor, to GNU_TYPE, a regular
2891 pointer or fat pointer type. GNAT_SUBPROG is the subprogram to which
2892 the VMS descriptor is passed. */
2894 static tree
2895 convert_vms_descriptor (tree gnu_type, tree gnu_expr, Entity_Id gnat_subprog)
2897 tree desc_type = TREE_TYPE (TREE_TYPE (gnu_expr));
2898 tree desc = build1 (INDIRECT_REF, desc_type, gnu_expr);
2899 /* The CLASS field is the 3rd field in the descriptor. */
2900 tree class = TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (desc_type)));
2901 /* The POINTER field is the 4th field in the descriptor. */
2902 tree pointer = TREE_CHAIN (class);
2904 /* Retrieve the value of the POINTER field. */
2905 gnu_expr
2906 = build3 (COMPONENT_REF, TREE_TYPE (pointer), desc, pointer, NULL_TREE);
2908 if (POINTER_TYPE_P (gnu_type))
2909 return convert (gnu_type, gnu_expr);
2911 else if (TYPE_FAT_POINTER_P (gnu_type))
2913 tree p_array_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
2914 tree p_bounds_type = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)));
2915 tree template_type = TREE_TYPE (p_bounds_type);
2916 tree min_field = TYPE_FIELDS (template_type);
2917 tree max_field = TREE_CHAIN (TYPE_FIELDS (template_type));
2918 tree template, template_addr, aflags, dimct, t, u;
2919 /* See the head comment of build_vms_descriptor. */
2920 int iclass = TREE_INT_CST_LOW (DECL_INITIAL (class));
2922 /* Convert POINTER to the type of the P_ARRAY field. */
2923 gnu_expr = convert (p_array_type, gnu_expr);
2925 switch (iclass)
2927 case 1: /* Class S */
2928 case 15: /* Class SB */
2929 /* Build {1, LENGTH} template; LENGTH is the 1st field. */
2930 t = TYPE_FIELDS (desc_type);
2931 t = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
2932 t = tree_cons (min_field,
2933 convert (TREE_TYPE (min_field), integer_one_node),
2934 tree_cons (max_field,
2935 convert (TREE_TYPE (max_field), t),
2936 NULL_TREE));
2937 template = gnat_build_constructor (template_type, t);
2938 template_addr = build_unary_op (ADDR_EXPR, NULL_TREE, template);
2940 /* For class S, we are done. */
2941 if (iclass == 1)
2942 break;
2944 /* Test that we really have a SB descriptor, like DEC Ada. */
2945 t = build3 (COMPONENT_REF, TREE_TYPE (class), desc, class, NULL);
2946 u = convert (TREE_TYPE (class), DECL_INITIAL (class));
2947 u = build_binary_op (EQ_EXPR, integer_type_node, t, u);
2948 /* If so, there is already a template in the descriptor and
2949 it is located right after the POINTER field. */
2950 t = TREE_CHAIN (pointer);
2951 template = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
2952 /* Otherwise use the {1, LENGTH} template we build above. */
2953 template_addr = build3 (COND_EXPR, p_bounds_type, u,
2954 build_unary_op (ADDR_EXPR, p_bounds_type,
2955 template),
2956 template_addr);
2957 break;
2959 case 4: /* Class A */
2960 /* The AFLAGS field is the 7th field in the descriptor. */
2961 t = TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (pointer)));
2962 aflags = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
2963 /* The DIMCT field is the 8th field in the descriptor. */
2964 t = TREE_CHAIN (t);
2965 dimct = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
2966 /* Raise CONSTRAINT_ERROR if either more than 1 dimension
2967 or FL_COEFF or FL_BOUNDS not set. */
2968 u = build_int_cst (TREE_TYPE (aflags), 192);
2969 u = build_binary_op (TRUTH_OR_EXPR, integer_type_node,
2970 build_binary_op (NE_EXPR, integer_type_node,
2971 dimct,
2972 convert (TREE_TYPE (dimct),
2973 size_one_node)),
2974 build_binary_op (NE_EXPR, integer_type_node,
2975 build2 (BIT_AND_EXPR,
2976 TREE_TYPE (aflags),
2977 aflags, u),
2978 u));
2979 add_stmt (build3 (COND_EXPR, void_type_node, u,
2980 build_call_raise (CE_Length_Check_Failed, Empty,
2981 N_Raise_Constraint_Error),
2982 NULL_TREE));
2983 /* There is already a template in the descriptor and it is
2984 located at the start of block 3 (12th field). */
2985 t = TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (t))));
2986 template = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
2987 template_addr = build_unary_op (ADDR_EXPR, p_bounds_type, template);
2988 break;
2990 case 10: /* Class NCA */
2991 default:
2992 post_error ("unsupported descriptor type for &", gnat_subprog);
2993 template_addr = integer_zero_node;
2994 break;
2997 /* Build the fat pointer in the form of a constructor. */
2998 t = tree_cons (TYPE_FIELDS (gnu_type), gnu_expr,
2999 tree_cons (TREE_CHAIN (TYPE_FIELDS (gnu_type)),
3000 template_addr, NULL_TREE));
3001 return gnat_build_constructor (gnu_type, t);
3004 else
3005 gcc_unreachable ();
3008 /* Build a stub for the subprogram specified by the GCC tree GNU_SUBPROG
3009 and the GNAT node GNAT_SUBPROG. */
3011 void
3012 build_function_stub (tree gnu_subprog, Entity_Id gnat_subprog)
3014 tree gnu_subprog_type, gnu_subprog_addr, gnu_subprog_call;
3015 tree gnu_stub_param, gnu_param_list, gnu_arg_types, gnu_param;
3016 tree gnu_stub_decl = DECL_FUNCTION_STUB (gnu_subprog);
3017 tree gnu_body;
3019 gnu_subprog_type = TREE_TYPE (gnu_subprog);
3020 gnu_param_list = NULL_TREE;
3022 begin_subprog_body (gnu_stub_decl);
3023 gnat_pushlevel ();
3025 start_stmt_group ();
3027 /* Loop over the parameters of the stub and translate any of them
3028 passed by descriptor into a by reference one. */
3029 for (gnu_stub_param = DECL_ARGUMENTS (gnu_stub_decl),
3030 gnu_arg_types = TYPE_ARG_TYPES (gnu_subprog_type);
3031 gnu_stub_param;
3032 gnu_stub_param = TREE_CHAIN (gnu_stub_param),
3033 gnu_arg_types = TREE_CHAIN (gnu_arg_types))
3035 if (DECL_BY_DESCRIPTOR_P (gnu_stub_param))
3036 gnu_param = convert_vms_descriptor (TREE_VALUE (gnu_arg_types),
3037 gnu_stub_param, gnat_subprog);
3038 else
3039 gnu_param = gnu_stub_param;
3041 gnu_param_list = tree_cons (NULL_TREE, gnu_param, gnu_param_list);
3044 gnu_body = end_stmt_group ();
3046 /* Invoke the internal subprogram. */
3047 gnu_subprog_addr = build1 (ADDR_EXPR, build_pointer_type (gnu_subprog_type),
3048 gnu_subprog);
3049 gnu_subprog_call = build_call_list (TREE_TYPE (gnu_subprog_type),
3050 gnu_subprog_addr,
3051 nreverse (gnu_param_list));
3053 /* Propagate the return value, if any. */
3054 if (VOID_TYPE_P (TREE_TYPE (gnu_subprog_type)))
3055 append_to_statement_list (gnu_subprog_call, &gnu_body);
3056 else
3057 append_to_statement_list (build_return_expr (DECL_RESULT (gnu_stub_decl),
3058 gnu_subprog_call),
3059 &gnu_body);
3061 gnat_poplevel ();
3063 allocate_struct_function (gnu_stub_decl, false);
3064 end_subprog_body (gnu_body);
3067 /* Build a type to be used to represent an aliased object whose nominal
3068 type is an unconstrained array. This consists of a RECORD_TYPE containing
3069 a field of TEMPLATE_TYPE and a field of OBJECT_TYPE, which is an
3070 ARRAY_TYPE. If ARRAY_TYPE is that of the unconstrained array, this
3071 is used to represent an arbitrary unconstrained object. Use NAME
3072 as the name of the record. */
3074 tree
3075 build_unc_object_type (tree template_type, tree object_type, tree name)
3077 tree type = make_node (RECORD_TYPE);
3078 tree template_field = create_field_decl (get_identifier ("BOUNDS"),
3079 template_type, type, 0, 0, 0, 1);
3080 tree array_field = create_field_decl (get_identifier ("ARRAY"), object_type,
3081 type, 0, 0, 0, 1);
3083 TYPE_NAME (type) = name;
3084 TYPE_CONTAINS_TEMPLATE_P (type) = 1;
3085 finish_record_type (type,
3086 chainon (chainon (NULL_TREE, template_field),
3087 array_field),
3088 0, false);
3090 return type;
3093 /* Same, taking a thin or fat pointer type instead of a template type. */
3095 tree
3096 build_unc_object_type_from_ptr (tree thin_fat_ptr_type, tree object_type,
3097 tree name)
3099 tree template_type;
3101 gcc_assert (TYPE_FAT_OR_THIN_POINTER_P (thin_fat_ptr_type));
3103 template_type
3104 = (TYPE_FAT_POINTER_P (thin_fat_ptr_type)
3105 ? TREE_TYPE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (thin_fat_ptr_type))))
3106 : TREE_TYPE (TYPE_FIELDS (TREE_TYPE (thin_fat_ptr_type))));
3107 return build_unc_object_type (template_type, object_type, name);
3110 /* Shift the component offsets within an unconstrained object TYPE to make it
3111 suitable for use as a designated type for thin pointers. */
3113 void
3114 shift_unc_components_for_thin_pointers (tree type)
3116 /* Thin pointer values designate the ARRAY data of an unconstrained object,
3117 allocated past the BOUNDS template. The designated type is adjusted to
3118 have ARRAY at position zero and the template at a negative offset, so
3119 that COMPONENT_REFs on (*thin_ptr) designate the proper location. */
3121 tree bounds_field = TYPE_FIELDS (type);
3122 tree array_field = TREE_CHAIN (TYPE_FIELDS (type));
3124 DECL_FIELD_OFFSET (bounds_field)
3125 = size_binop (MINUS_EXPR, size_zero_node, byte_position (array_field));
3127 DECL_FIELD_OFFSET (array_field) = size_zero_node;
3128 DECL_FIELD_BIT_OFFSET (array_field) = bitsize_zero_node;
3131 /* Update anything previously pointing to OLD_TYPE to point to NEW_TYPE. In
3132 the normal case this is just two adjustments, but we have more to do
3133 if NEW is an UNCONSTRAINED_ARRAY_TYPE. */
3135 void
3136 update_pointer_to (tree old_type, tree new_type)
3138 tree ptr = TYPE_POINTER_TO (old_type);
3139 tree ref = TYPE_REFERENCE_TO (old_type);
3140 tree ptr1, ref1;
3141 tree type;
3143 /* If this is the main variant, process all the other variants first. */
3144 if (TYPE_MAIN_VARIANT (old_type) == old_type)
3145 for (type = TYPE_NEXT_VARIANT (old_type); type;
3146 type = TYPE_NEXT_VARIANT (type))
3147 update_pointer_to (type, new_type);
3149 /* If no pointer or reference, we are done. */
3150 if (!ptr && !ref)
3151 return;
3153 /* Merge the old type qualifiers in the new type.
3155 Each old variant has qualifiers for specific reasons, and the new
3156 designated type as well. Each set of qualifiers represents useful
3157 information grabbed at some point, and merging the two simply unifies
3158 these inputs into the final type description.
3160 Consider for instance a volatile type frozen after an access to constant
3161 type designating it. After the designated type freeze, we get here with a
3162 volatile new_type and a dummy old_type with a readonly variant, created
3163 when the access type was processed. We shall make a volatile and readonly
3164 designated type, because that's what it really is.
3166 We might also get here for a non-dummy old_type variant with different
3167 qualifiers than the new_type ones, for instance in some cases of pointers
3168 to private record type elaboration (see the comments around the call to
3169 this routine from gnat_to_gnu_entity/E_Access_Type). We have to merge the
3170 qualifiers in thoses cases too, to avoid accidentally discarding the
3171 initial set, and will often end up with old_type == new_type then. */
3172 new_type = build_qualified_type (new_type,
3173 TYPE_QUALS (old_type)
3174 | TYPE_QUALS (new_type));
3176 /* If the new type and the old one are identical, there is nothing to
3177 update. */
3178 if (old_type == new_type)
3179 return;
3181 /* Otherwise, first handle the simple case. */
3182 if (TREE_CODE (new_type) != UNCONSTRAINED_ARRAY_TYPE)
3184 TYPE_POINTER_TO (new_type) = ptr;
3185 TYPE_REFERENCE_TO (new_type) = ref;
3187 for (; ptr; ptr = TYPE_NEXT_PTR_TO (ptr))
3188 for (ptr1 = TYPE_MAIN_VARIANT (ptr); ptr1;
3189 ptr1 = TYPE_NEXT_VARIANT (ptr1))
3190 TREE_TYPE (ptr1) = new_type;
3192 for (; ref; ref = TYPE_NEXT_REF_TO (ref))
3193 for (ref1 = TYPE_MAIN_VARIANT (ref); ref1;
3194 ref1 = TYPE_NEXT_VARIANT (ref1))
3195 TREE_TYPE (ref1) = new_type;
3198 /* Now deal with the unconstrained array case. In this case the "pointer"
3199 is actually a RECORD_TYPE where both fields are pointers to dummy nodes.
3200 Turn them into pointers to the correct types using update_pointer_to. */
3201 else if (TREE_CODE (ptr) != RECORD_TYPE || !TYPE_IS_FAT_POINTER_P (ptr))
3202 gcc_unreachable ();
3204 else
3206 tree new_obj_rec = TYPE_OBJECT_RECORD_TYPE (new_type);
3207 tree array_field = TYPE_FIELDS (ptr);
3208 tree bounds_field = TREE_CHAIN (TYPE_FIELDS (ptr));
3209 tree new_ptr = TYPE_POINTER_TO (new_type);
3210 tree new_ref;
3211 tree var;
3213 /* Make pointers to the dummy template point to the real template. */
3214 update_pointer_to
3215 (TREE_TYPE (TREE_TYPE (bounds_field)),
3216 TREE_TYPE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (new_ptr)))));
3218 /* The references to the template bounds present in the array type
3219 are made through a PLACEHOLDER_EXPR of type new_ptr. Since we
3220 are updating ptr to make it a full replacement for new_ptr as
3221 pointer to new_type, we must rework the PLACEHOLDER_EXPR so as
3222 to make it of type ptr. */
3223 new_ref = build3 (COMPONENT_REF, TREE_TYPE (bounds_field),
3224 build0 (PLACEHOLDER_EXPR, ptr),
3225 bounds_field, NULL_TREE);
3227 /* Create the new array for the new PLACEHOLDER_EXPR and make
3228 pointers to the dummy array point to it.
3230 ??? This is now the only use of substitute_in_type,
3231 which is a very "heavy" routine to do this, so it
3232 should be replaced at some point. */
3233 update_pointer_to
3234 (TREE_TYPE (TREE_TYPE (array_field)),
3235 substitute_in_type (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (new_ptr))),
3236 TREE_CHAIN (TYPE_FIELDS (new_ptr)), new_ref));
3238 /* Make ptr the pointer to new_type. */
3239 TYPE_POINTER_TO (new_type) = TYPE_REFERENCE_TO (new_type)
3240 = TREE_TYPE (new_type) = ptr;
3242 for (var = TYPE_MAIN_VARIANT (ptr); var; var = TYPE_NEXT_VARIANT (var))
3243 SET_TYPE_UNCONSTRAINED_ARRAY (var, new_type);
3245 /* Now handle updating the allocation record, what the thin pointer
3246 points to. Update all pointers from the old record into the new
3247 one, update the type of the array field, and recompute the size. */
3248 update_pointer_to (TYPE_OBJECT_RECORD_TYPE (old_type), new_obj_rec);
3250 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (new_obj_rec)))
3251 = TREE_TYPE (TREE_TYPE (array_field));
3253 /* The size recomputation needs to account for alignment constraints, so
3254 we let layout_type work it out. This will reset the field offsets to
3255 what they would be in a regular record, so we shift them back to what
3256 we want them to be for a thin pointer designated type afterwards. */
3257 DECL_SIZE (TYPE_FIELDS (new_obj_rec)) = 0;
3258 DECL_SIZE (TREE_CHAIN (TYPE_FIELDS (new_obj_rec))) = 0;
3259 TYPE_SIZE (new_obj_rec) = 0;
3260 layout_type (new_obj_rec);
3262 shift_unc_components_for_thin_pointers (new_obj_rec);
3264 /* We are done, at last. */
3265 rest_of_record_type_compilation (ptr);
3269 /* Convert a pointer to a constrained array into a pointer to a fat
3270 pointer. This involves making or finding a template. */
3272 static tree
3273 convert_to_fat_pointer (tree type, tree expr)
3275 tree template_type = TREE_TYPE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (type))));
3276 tree template, template_addr;
3277 tree etype = TREE_TYPE (expr);
3279 /* If EXPR is a constant of zero, we make a fat pointer that has a null
3280 pointer to the template and array. */
3281 if (integer_zerop (expr))
3282 return
3283 gnat_build_constructor
3284 (type,
3285 tree_cons (TYPE_FIELDS (type),
3286 convert (TREE_TYPE (TYPE_FIELDS (type)), expr),
3287 tree_cons (TREE_CHAIN (TYPE_FIELDS (type)),
3288 convert (build_pointer_type (template_type),
3289 expr),
3290 NULL_TREE)));
3292 /* If EXPR is a thin pointer, make the template and data from the record. */
3294 else if (TYPE_THIN_POINTER_P (etype))
3296 tree fields = TYPE_FIELDS (TREE_TYPE (etype));
3298 expr = save_expr (expr);
3299 if (TREE_CODE (expr) == ADDR_EXPR)
3300 expr = TREE_OPERAND (expr, 0);
3301 else
3302 expr = build1 (INDIRECT_REF, TREE_TYPE (etype), expr);
3304 template = build_component_ref (expr, NULL_TREE, fields, false);
3305 expr = build_unary_op (ADDR_EXPR, NULL_TREE,
3306 build_component_ref (expr, NULL_TREE,
3307 TREE_CHAIN (fields), false));
3309 else
3310 /* Otherwise, build the constructor for the template. */
3311 template = build_template (template_type, TREE_TYPE (etype), expr);
3313 template_addr = build_unary_op (ADDR_EXPR, NULL_TREE, template);
3315 /* The result is a CONSTRUCTOR for the fat pointer.
3317 If expr is an argument of a foreign convention subprogram, the type it
3318 points to is directly the component type. In this case, the expression
3319 type may not match the corresponding FIELD_DECL type at this point, so we
3320 call "convert" here to fix that up if necessary. This type consistency is
3321 required, for instance because it ensures that possible later folding of
3322 component_refs against this constructor always yields something of the
3323 same type as the initial reference.
3325 Note that the call to "build_template" above is still fine, because it
3326 will only refer to the provided template_type in this case. */
3327 return
3328 gnat_build_constructor
3329 (type, tree_cons (TYPE_FIELDS (type),
3330 convert (TREE_TYPE (TYPE_FIELDS (type)), expr),
3331 tree_cons (TREE_CHAIN (TYPE_FIELDS (type)),
3332 template_addr, NULL_TREE)));
3335 /* Convert to a thin pointer type, TYPE. The only thing we know how to convert
3336 is something that is a fat pointer, so convert to it first if it EXPR
3337 is not already a fat pointer. */
3339 static tree
3340 convert_to_thin_pointer (tree type, tree expr)
3342 if (!TYPE_FAT_POINTER_P (TREE_TYPE (expr)))
3343 expr
3344 = convert_to_fat_pointer
3345 (TREE_TYPE (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type))), expr);
3347 /* We get the pointer to the data and use a NOP_EXPR to make it the
3348 proper GCC type. */
3349 expr = build_component_ref (expr, NULL_TREE, TYPE_FIELDS (TREE_TYPE (expr)),
3350 false);
3351 expr = build1 (NOP_EXPR, type, expr);
3353 return expr;
3356 /* Create an expression whose value is that of EXPR,
3357 converted to type TYPE. The TREE_TYPE of the value
3358 is always TYPE. This function implements all reasonable
3359 conversions; callers should filter out those that are
3360 not permitted by the language being compiled. */
3362 tree
3363 convert (tree type, tree expr)
3365 enum tree_code code = TREE_CODE (type);
3366 tree etype = TREE_TYPE (expr);
3367 enum tree_code ecode = TREE_CODE (etype);
3369 /* If EXPR is already the right type, we are done. */
3370 if (type == etype)
3371 return expr;
3373 /* If both input and output have padding and are of variable size, do this
3374 as an unchecked conversion. Likewise if one is a mere variant of the
3375 other, so we avoid a pointless unpad/repad sequence. */
3376 else if (ecode == RECORD_TYPE && code == RECORD_TYPE
3377 && TYPE_IS_PADDING_P (type) && TYPE_IS_PADDING_P (etype)
3378 && (!TREE_CONSTANT (TYPE_SIZE (type))
3379 || !TREE_CONSTANT (TYPE_SIZE (etype))
3380 || TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (etype)))
3383 /* If the output type has padding, make a constructor to build the
3384 record. */
3385 else if (code == RECORD_TYPE && TYPE_IS_PADDING_P (type))
3387 /* If we previously converted from another type and our type is
3388 of variable size, remove the conversion to avoid the need for
3389 variable-size temporaries. */
3390 if (TREE_CODE (expr) == VIEW_CONVERT_EXPR
3391 && !TREE_CONSTANT (TYPE_SIZE (type)))
3392 expr = TREE_OPERAND (expr, 0);
3394 /* If we are just removing the padding from expr, convert the original
3395 object if we have variable size. That will avoid the need
3396 for some variable-size temporaries. */
3397 if (TREE_CODE (expr) == COMPONENT_REF
3398 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE
3399 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (expr, 0)))
3400 && !TREE_CONSTANT (TYPE_SIZE (type)))
3401 return convert (type, TREE_OPERAND (expr, 0));
3403 /* If the result type is a padded type with a self-referentially-sized
3404 field and the expression type is a record, do this as an
3405 unchecked conversion. */
3406 else if (TREE_CODE (etype) == RECORD_TYPE
3407 && CONTAINS_PLACEHOLDER_P (DECL_SIZE (TYPE_FIELDS (type))))
3408 return unchecked_convert (type, expr, false);
3410 else
3411 return
3412 gnat_build_constructor (type,
3413 tree_cons (TYPE_FIELDS (type),
3414 convert (TREE_TYPE
3415 (TYPE_FIELDS (type)),
3416 expr),
3417 NULL_TREE));
3420 /* If the input type has padding, remove it and convert to the output type.
3421 The conditions ordering is arranged to ensure that the output type is not
3422 a padding type here, as it is not clear whether the conversion would
3423 always be correct if this was to happen. */
3424 else if (ecode == RECORD_TYPE && TYPE_IS_PADDING_P (etype))
3426 tree unpadded;
3428 /* If we have just converted to this padded type, just get the
3429 inner expression. */
3430 if (TREE_CODE (expr) == CONSTRUCTOR
3431 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (expr))
3432 && VEC_index (constructor_elt, CONSTRUCTOR_ELTS (expr), 0)->index
3433 == TYPE_FIELDS (etype))
3434 unpadded
3435 = VEC_index (constructor_elt, CONSTRUCTOR_ELTS (expr), 0)->value;
3437 /* Otherwise, build an explicit component reference. */
3438 else
3439 unpadded
3440 = build_component_ref (expr, NULL_TREE, TYPE_FIELDS (etype), false);
3442 return convert (type, unpadded);
3445 /* If the input is a biased type, adjust first. */
3446 if (ecode == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (etype))
3447 return convert (type, fold_build2 (PLUS_EXPR, TREE_TYPE (etype),
3448 fold_convert (TREE_TYPE (etype),
3449 expr),
3450 TYPE_MIN_VALUE (etype)));
3452 /* If the input is a justified modular type, we need to extract the actual
3453 object before converting it to any other type with the exceptions of an
3454 unconstrained array or of a mere type variant. It is useful to avoid the
3455 extraction and conversion in the type variant case because it could end
3456 up replacing a VAR_DECL expr by a constructor and we might be about the
3457 take the address of the result. */
3458 if (ecode == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (etype)
3459 && code != UNCONSTRAINED_ARRAY_TYPE
3460 && TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (etype))
3461 return convert (type, build_component_ref (expr, NULL_TREE,
3462 TYPE_FIELDS (etype), false));
3464 /* If converting to a type that contains a template, convert to the data
3465 type and then build the template. */
3466 if (code == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (type))
3468 tree obj_type = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (type)));
3470 /* If the source already has a template, get a reference to the
3471 associated array only, as we are going to rebuild a template
3472 for the target type anyway. */
3473 expr = maybe_unconstrained_array (expr);
3475 return
3476 gnat_build_constructor
3477 (type,
3478 tree_cons (TYPE_FIELDS (type),
3479 build_template (TREE_TYPE (TYPE_FIELDS (type)),
3480 obj_type, NULL_TREE),
3481 tree_cons (TREE_CHAIN (TYPE_FIELDS (type)),
3482 convert (obj_type, expr), NULL_TREE)));
3485 /* There are some special cases of expressions that we process
3486 specially. */
3487 switch (TREE_CODE (expr))
3489 case ERROR_MARK:
3490 return expr;
3492 case NULL_EXPR:
3493 /* Just set its type here. For TRANSFORM_EXPR, we will do the actual
3494 conversion in gnat_expand_expr. NULL_EXPR does not represent
3495 and actual value, so no conversion is needed. */
3496 expr = copy_node (expr);
3497 TREE_TYPE (expr) = type;
3498 return expr;
3500 case STRING_CST:
3501 /* If we are converting a STRING_CST to another constrained array type,
3502 just make a new one in the proper type. */
3503 if (code == ecode && AGGREGATE_TYPE_P (etype)
3504 && !(TREE_CODE (TYPE_SIZE (etype)) == INTEGER_CST
3505 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
3507 expr = copy_node (expr);
3508 TREE_TYPE (expr) = type;
3509 return expr;
3511 break;
3513 case CONSTRUCTOR:
3514 /* If we are converting a CONSTRUCTOR to another constrained array type
3515 with the same domain, just make a new one in the proper type. */
3516 if (code == ecode && code == ARRAY_TYPE
3517 && TREE_TYPE (type) == TREE_TYPE (etype)
3518 && tree_int_cst_equal (TYPE_MIN_VALUE (TYPE_DOMAIN (type)),
3519 TYPE_MIN_VALUE (TYPE_DOMAIN (etype)))
3520 && tree_int_cst_equal (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
3521 TYPE_MAX_VALUE (TYPE_DOMAIN (etype))))
3523 expr = copy_node (expr);
3524 TREE_TYPE (expr) = type;
3525 return expr;
3527 break;
3529 case UNCONSTRAINED_ARRAY_REF:
3530 /* Convert this to the type of the inner array by getting the address of
3531 the array from the template. */
3532 expr = build_unary_op (INDIRECT_REF, NULL_TREE,
3533 build_component_ref (TREE_OPERAND (expr, 0),
3534 get_identifier ("P_ARRAY"),
3535 NULL_TREE, false));
3536 etype = TREE_TYPE (expr);
3537 ecode = TREE_CODE (etype);
3538 break;
3540 case VIEW_CONVERT_EXPR:
3542 /* GCC 4.x is very sensitive to type consistency overall, and view
3543 conversions thus are very frequent. Even though just "convert"ing
3544 the inner operand to the output type is fine in most cases, it
3545 might expose unexpected input/output type mismatches in special
3546 circumstances so we avoid such recursive calls when we can. */
3548 tree op0 = TREE_OPERAND (expr, 0);
3550 /* If we are converting back to the original type, we can just
3551 lift the input conversion. This is a common occurrence with
3552 switches back-and-forth amongst type variants. */
3553 if (type == TREE_TYPE (op0))
3554 return op0;
3556 /* Otherwise, if we're converting between two aggregate types, we
3557 might be allowed to substitute the VIEW_CONVERT target type in
3558 place or to just convert the inner expression. */
3559 if (AGGREGATE_TYPE_P (type) && AGGREGATE_TYPE_P (etype))
3561 /* If we are converting between type variants, we can just
3562 substitute the VIEW_CONVERT in place. */
3563 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (etype))
3564 return build1 (VIEW_CONVERT_EXPR, type, op0);
3566 /* Otherwise, we may just bypass the input view conversion unless
3567 one of the types is a fat pointer, which is handled by
3568 specialized code below which relies on exact type matching. */
3569 else if (!TYPE_FAT_POINTER_P (type) && !TYPE_FAT_POINTER_P (etype))
3570 return convert (type, op0);
3573 break;
3575 case INDIRECT_REF:
3576 /* If both types are record types, just convert the pointer and
3577 make a new INDIRECT_REF.
3579 ??? Disable this for now since it causes problems with the
3580 code in build_binary_op for MODIFY_EXPR which wants to
3581 strip off conversions. But that code really is a mess and
3582 we need to do this a much better way some time. */
3583 if (0
3584 && (TREE_CODE (type) == RECORD_TYPE
3585 || TREE_CODE (type) == UNION_TYPE)
3586 && (TREE_CODE (etype) == RECORD_TYPE
3587 || TREE_CODE (etype) == UNION_TYPE)
3588 && !TYPE_FAT_POINTER_P (type) && !TYPE_FAT_POINTER_P (etype))
3589 return build_unary_op (INDIRECT_REF, NULL_TREE,
3590 convert (build_pointer_type (type),
3591 TREE_OPERAND (expr, 0)));
3592 break;
3594 default:
3595 break;
3598 /* Check for converting to a pointer to an unconstrained array. */
3599 if (TYPE_FAT_POINTER_P (type) && !TYPE_FAT_POINTER_P (etype))
3600 return convert_to_fat_pointer (type, expr);
3602 /* If we're converting between two aggregate types that have the same main
3603 variant, just make a VIEW_CONVER_EXPR. */
3604 else if (AGGREGATE_TYPE_P (type)
3605 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (etype))
3606 return build1 (VIEW_CONVERT_EXPR, type, expr);
3608 /* In all other cases of related types, make a NOP_EXPR. */
3609 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (etype)
3610 || (code == INTEGER_CST && ecode == INTEGER_CST
3611 && (type == TREE_TYPE (etype) || etype == TREE_TYPE (type))))
3612 return fold_convert (type, expr);
3614 switch (code)
3616 case VOID_TYPE:
3617 return fold_build1 (CONVERT_EXPR, type, expr);
3619 case BOOLEAN_TYPE:
3620 return fold_convert (type, gnat_truthvalue_conversion (expr));
3622 case INTEGER_TYPE:
3623 if (TYPE_HAS_ACTUAL_BOUNDS_P (type)
3624 && (ecode == ARRAY_TYPE || ecode == UNCONSTRAINED_ARRAY_TYPE
3625 || (ecode == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (etype))))
3626 return unchecked_convert (type, expr, false);
3627 else if (TYPE_BIASED_REPRESENTATION_P (type))
3628 return fold_convert (type,
3629 fold_build2 (MINUS_EXPR, TREE_TYPE (type),
3630 convert (TREE_TYPE (type), expr),
3631 TYPE_MIN_VALUE (type)));
3633 /* ... fall through ... */
3635 case ENUMERAL_TYPE:
3636 return fold (convert_to_integer (type, expr));
3638 case POINTER_TYPE:
3639 case REFERENCE_TYPE:
3640 /* If converting between two pointers to records denoting
3641 both a template and type, adjust if needed to account
3642 for any differing offsets, since one might be negative. */
3643 if (TYPE_THIN_POINTER_P (etype) && TYPE_THIN_POINTER_P (type))
3645 tree bit_diff
3646 = size_diffop (bit_position (TYPE_FIELDS (TREE_TYPE (etype))),
3647 bit_position (TYPE_FIELDS (TREE_TYPE (type))));
3648 tree byte_diff = size_binop (CEIL_DIV_EXPR, bit_diff,
3649 sbitsize_int (BITS_PER_UNIT));
3651 expr = build1 (NOP_EXPR, type, expr);
3652 TREE_CONSTANT (expr) = TREE_CONSTANT (TREE_OPERAND (expr, 0));
3653 if (integer_zerop (byte_diff))
3654 return expr;
3656 return build_binary_op (POINTER_PLUS_EXPR, type, expr,
3657 fold (convert (sizetype, byte_diff)));
3660 /* If converting to a thin pointer, handle specially. */
3661 if (TYPE_THIN_POINTER_P (type)
3662 && TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)))
3663 return convert_to_thin_pointer (type, expr);
3665 /* If converting fat pointer to normal pointer, get the pointer to the
3666 array and then convert it. */
3667 else if (TYPE_FAT_POINTER_P (etype))
3668 expr = build_component_ref (expr, get_identifier ("P_ARRAY"),
3669 NULL_TREE, false);
3671 return fold (convert_to_pointer (type, expr));
3673 case REAL_TYPE:
3674 return fold (convert_to_real (type, expr));
3676 case RECORD_TYPE:
3677 if (TYPE_JUSTIFIED_MODULAR_P (type) && !AGGREGATE_TYPE_P (etype))
3678 return
3679 gnat_build_constructor
3680 (type, tree_cons (TYPE_FIELDS (type),
3681 convert (TREE_TYPE (TYPE_FIELDS (type)), expr),
3682 NULL_TREE));
3684 /* ... fall through ... */
3686 case ARRAY_TYPE:
3687 /* In these cases, assume the front-end has validated the conversion.
3688 If the conversion is valid, it will be a bit-wise conversion, so
3689 it can be viewed as an unchecked conversion. */
3690 return unchecked_convert (type, expr, false);
3692 case UNION_TYPE:
3693 /* This is a either a conversion between a tagged type and some
3694 subtype, which we have to mark as a UNION_TYPE because of
3695 overlapping fields or a conversion of an Unchecked_Union. */
3696 return unchecked_convert (type, expr, false);
3698 case UNCONSTRAINED_ARRAY_TYPE:
3699 /* If EXPR is a constrained array, take its address, convert it to a
3700 fat pointer, and then dereference it. Likewise if EXPR is a
3701 record containing both a template and a constrained array.
3702 Note that a record representing a justified modular type
3703 always represents a packed constrained array. */
3704 if (ecode == ARRAY_TYPE
3705 || (ecode == INTEGER_TYPE && TYPE_HAS_ACTUAL_BOUNDS_P (etype))
3706 || (ecode == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (etype))
3707 || (ecode == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (etype)))
3708 return
3709 build_unary_op
3710 (INDIRECT_REF, NULL_TREE,
3711 convert_to_fat_pointer (TREE_TYPE (type),
3712 build_unary_op (ADDR_EXPR,
3713 NULL_TREE, expr)));
3715 /* Do something very similar for converting one unconstrained
3716 array to another. */
3717 else if (ecode == UNCONSTRAINED_ARRAY_TYPE)
3718 return
3719 build_unary_op (INDIRECT_REF, NULL_TREE,
3720 convert (TREE_TYPE (type),
3721 build_unary_op (ADDR_EXPR,
3722 NULL_TREE, expr)));
3723 else
3724 gcc_unreachable ();
3726 case COMPLEX_TYPE:
3727 return fold (convert_to_complex (type, expr));
3729 default:
3730 gcc_unreachable ();
3734 /* Remove all conversions that are done in EXP. This includes converting
3735 from a padded type or to a justified modular type. If TRUE_ADDRESS
3736 is true, always return the address of the containing object even if
3737 the address is not bit-aligned. */
3739 tree
3740 remove_conversions (tree exp, bool true_address)
3742 switch (TREE_CODE (exp))
3744 case CONSTRUCTOR:
3745 if (true_address
3746 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
3747 && TYPE_JUSTIFIED_MODULAR_P (TREE_TYPE (exp)))
3748 return
3749 remove_conversions (VEC_index (constructor_elt,
3750 CONSTRUCTOR_ELTS (exp), 0)->value,
3751 true);
3752 break;
3754 case COMPONENT_REF:
3755 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == RECORD_TYPE
3756 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
3757 return remove_conversions (TREE_OPERAND (exp, 0), true_address);
3758 break;
3760 case VIEW_CONVERT_EXPR: case NON_LVALUE_EXPR:
3761 case NOP_EXPR: case CONVERT_EXPR:
3762 return remove_conversions (TREE_OPERAND (exp, 0), true_address);
3764 default:
3765 break;
3768 return exp;
3771 /* If EXP's type is an UNCONSTRAINED_ARRAY_TYPE, return an expression that
3772 refers to the underlying array. If its type has TYPE_CONTAINS_TEMPLATE_P,
3773 likewise return an expression pointing to the underlying array. */
3775 tree
3776 maybe_unconstrained_array (tree exp)
3778 enum tree_code code = TREE_CODE (exp);
3779 tree new;
3781 switch (TREE_CODE (TREE_TYPE (exp)))
3783 case UNCONSTRAINED_ARRAY_TYPE:
3784 if (code == UNCONSTRAINED_ARRAY_REF)
3787 = build_unary_op (INDIRECT_REF, NULL_TREE,
3788 build_component_ref (TREE_OPERAND (exp, 0),
3789 get_identifier ("P_ARRAY"),
3790 NULL_TREE, false));
3791 TREE_READONLY (new) = TREE_STATIC (new) = TREE_READONLY (exp);
3792 return new;
3795 else if (code == NULL_EXPR)
3796 return build1 (NULL_EXPR,
3797 TREE_TYPE (TREE_TYPE (TYPE_FIELDS
3798 (TREE_TYPE (TREE_TYPE (exp))))),
3799 TREE_OPERAND (exp, 0));
3801 case RECORD_TYPE:
3802 /* If this is a padded type, convert to the unpadded type and see if
3803 it contains a template. */
3804 if (TYPE_IS_PADDING_P (TREE_TYPE (exp)))
3806 new = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (exp))), exp);
3807 if (TREE_CODE (TREE_TYPE (new)) == RECORD_TYPE
3808 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (new)))
3809 return
3810 build_component_ref (new, NULL_TREE,
3811 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new))),
3814 else if (TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (exp)))
3815 return
3816 build_component_ref (exp, NULL_TREE,
3817 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (exp))), 0);
3818 break;
3820 default:
3821 break;
3824 return exp;
3827 /* Return an expression that does an unchecked conversion of EXPR to TYPE.
3828 If NOTRUNC_P is true, truncation operations should be suppressed. */
3830 tree
3831 unchecked_convert (tree type, tree expr, bool notrunc_p)
3833 tree etype = TREE_TYPE (expr);
3835 /* If the expression is already the right type, we are done. */
3836 if (etype == type)
3837 return expr;
3839 /* If both types types are integral just do a normal conversion.
3840 Likewise for a conversion to an unconstrained array. */
3841 if ((((INTEGRAL_TYPE_P (type)
3842 && !(TREE_CODE (type) == INTEGER_TYPE
3843 && TYPE_VAX_FLOATING_POINT_P (type)))
3844 || (POINTER_TYPE_P (type) && ! TYPE_THIN_POINTER_P (type))
3845 || (TREE_CODE (type) == RECORD_TYPE
3846 && TYPE_JUSTIFIED_MODULAR_P (type)))
3847 && ((INTEGRAL_TYPE_P (etype)
3848 && !(TREE_CODE (etype) == INTEGER_TYPE
3849 && TYPE_VAX_FLOATING_POINT_P (etype)))
3850 || (POINTER_TYPE_P (etype) && !TYPE_THIN_POINTER_P (etype))
3851 || (TREE_CODE (etype) == RECORD_TYPE
3852 && TYPE_JUSTIFIED_MODULAR_P (etype))))
3853 || TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
3855 tree rtype = type;
3856 bool final_unchecked = false;
3858 if (TREE_CODE (etype) == INTEGER_TYPE
3859 && TYPE_BIASED_REPRESENTATION_P (etype))
3861 tree ntype = copy_type (etype);
3863 TYPE_BIASED_REPRESENTATION_P (ntype) = 0;
3864 TYPE_MAIN_VARIANT (ntype) = ntype;
3865 expr = build1 (NOP_EXPR, ntype, expr);
3868 if (TREE_CODE (type) == INTEGER_TYPE
3869 && TYPE_BIASED_REPRESENTATION_P (type))
3871 rtype = copy_type (type);
3872 TYPE_BIASED_REPRESENTATION_P (rtype) = 0;
3873 TYPE_MAIN_VARIANT (rtype) = rtype;
3876 /* We have another special case: if we are unchecked converting subtype
3877 into a base type, we need to ensure that VRP doesn't propagate range
3878 information since this conversion may be done precisely to validate
3879 that the object is within the range it is supposed to have. */
3880 else if (TREE_CODE (expr) != INTEGER_CST
3881 && TREE_CODE (type) == INTEGER_TYPE && !TREE_TYPE (type)
3882 && ((TREE_CODE (etype) == INTEGER_TYPE && TREE_TYPE (etype))
3883 || TREE_CODE (etype) == ENUMERAL_TYPE
3884 || TREE_CODE (etype) == BOOLEAN_TYPE))
3886 /* The optimization barrier is a VIEW_CONVERT_EXPR node; moreover,
3887 in order not to be deemed an useless type conversion, it must
3888 be from subtype to base type.
3890 ??? This may raise addressability and/or aliasing issues because
3891 VIEW_CONVERT_EXPR gets gimplified as an lvalue, thus causing the
3892 address of its operand to be taken if it is deemed addressable
3893 and not already in GIMPLE form. */
3894 rtype = gnat_type_for_mode (TYPE_MODE (type), TYPE_UNSIGNED (type));
3895 rtype = copy_type (rtype);
3896 TYPE_MAIN_VARIANT (rtype) = rtype;
3897 TREE_TYPE (rtype) = type;
3898 final_unchecked = true;
3901 expr = convert (rtype, expr);
3902 if (type != rtype)
3903 expr = fold_build1 (final_unchecked ? VIEW_CONVERT_EXPR : NOP_EXPR,
3904 type, expr);
3907 /* If we are converting TO an integral type whose precision is not the
3908 same as its size, first unchecked convert to a record that contains
3909 an object of the output type. Then extract the field. */
3910 else if (INTEGRAL_TYPE_P (type) && TYPE_RM_SIZE (type)
3911 && 0 != compare_tree_int (TYPE_RM_SIZE (type),
3912 GET_MODE_BITSIZE (TYPE_MODE (type))))
3914 tree rec_type = make_node (RECORD_TYPE);
3915 tree field = create_field_decl (get_identifier ("OBJ"), type,
3916 rec_type, 1, 0, 0, 0);
3918 TYPE_FIELDS (rec_type) = field;
3919 layout_type (rec_type);
3921 expr = unchecked_convert (rec_type, expr, notrunc_p);
3922 expr = build_component_ref (expr, NULL_TREE, field, 0);
3925 /* Similarly for integral input type whose precision is not equal to its
3926 size. */
3927 else if (INTEGRAL_TYPE_P (etype) && TYPE_RM_SIZE (etype)
3928 && 0 != compare_tree_int (TYPE_RM_SIZE (etype),
3929 GET_MODE_BITSIZE (TYPE_MODE (etype))))
3931 tree rec_type = make_node (RECORD_TYPE);
3932 tree field
3933 = create_field_decl (get_identifier ("OBJ"), etype, rec_type,
3934 1, 0, 0, 0);
3936 TYPE_FIELDS (rec_type) = field;
3937 layout_type (rec_type);
3939 expr = gnat_build_constructor (rec_type, build_tree_list (field, expr));
3940 expr = unchecked_convert (type, expr, notrunc_p);
3943 /* We have a special case when we are converting between two
3944 unconstrained array types. In that case, take the address,
3945 convert the fat pointer types, and dereference. */
3946 else if (TREE_CODE (etype) == UNCONSTRAINED_ARRAY_TYPE
3947 && TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
3948 expr = build_unary_op (INDIRECT_REF, NULL_TREE,
3949 build1 (VIEW_CONVERT_EXPR, TREE_TYPE (type),
3950 build_unary_op (ADDR_EXPR, NULL_TREE,
3951 expr)));
3952 else
3954 expr = maybe_unconstrained_array (expr);
3955 etype = TREE_TYPE (expr);
3956 expr = fold_build1 (VIEW_CONVERT_EXPR, type, expr);
3959 /* If the result is an integral type whose size is not equal to
3960 the size of the underlying machine type, sign- or zero-extend
3961 the result. We need not do this in the case where the input is
3962 an integral type of the same precision and signedness or if the output
3963 is a biased type or if both the input and output are unsigned. */
3964 if (!notrunc_p
3965 && INTEGRAL_TYPE_P (type) && TYPE_RM_SIZE (type)
3966 && !(TREE_CODE (type) == INTEGER_TYPE
3967 && TYPE_BIASED_REPRESENTATION_P (type))
3968 && 0 != compare_tree_int (TYPE_RM_SIZE (type),
3969 GET_MODE_BITSIZE (TYPE_MODE (type)))
3970 && !(INTEGRAL_TYPE_P (etype)
3971 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (etype)
3972 && operand_equal_p (TYPE_RM_SIZE (type),
3973 (TYPE_RM_SIZE (etype) != 0
3974 ? TYPE_RM_SIZE (etype) : TYPE_SIZE (etype)),
3976 && !(TYPE_UNSIGNED (type) && TYPE_UNSIGNED (etype)))
3978 tree base_type = gnat_type_for_mode (TYPE_MODE (type),
3979 TYPE_UNSIGNED (type));
3980 tree shift_expr
3981 = convert (base_type,
3982 size_binop (MINUS_EXPR,
3983 bitsize_int
3984 (GET_MODE_BITSIZE (TYPE_MODE (type))),
3985 TYPE_RM_SIZE (type)));
3986 expr
3987 = convert (type,
3988 build_binary_op (RSHIFT_EXPR, base_type,
3989 build_binary_op (LSHIFT_EXPR, base_type,
3990 convert (base_type, expr),
3991 shift_expr),
3992 shift_expr));
3995 /* An unchecked conversion should never raise Constraint_Error. The code
3996 below assumes that GCC's conversion routines overflow the same way that
3997 the underlying hardware does. This is probably true. In the rare case
3998 when it is false, we can rely on the fact that such conversions are
3999 erroneous anyway. */
4000 if (TREE_CODE (expr) == INTEGER_CST)
4001 TREE_OVERFLOW (expr) = 0;
4003 /* If the sizes of the types differ and this is an VIEW_CONVERT_EXPR,
4004 show no longer constant. */
4005 if (TREE_CODE (expr) == VIEW_CONVERT_EXPR
4006 && !operand_equal_p (TYPE_SIZE_UNIT (type), TYPE_SIZE_UNIT (etype),
4007 OEP_ONLY_CONST))
4008 TREE_CONSTANT (expr) = 0;
4010 return expr;
4013 /* Search the chain of currently available builtin declarations for a node
4014 corresponding to function NAME (an IDENTIFIER_NODE). Return the first node
4015 found, if any, or NULL_TREE otherwise. */
4016 tree
4017 builtin_decl_for (tree name)
4019 unsigned i;
4020 tree decl;
4022 for (i = 0; VEC_iterate(tree, builtin_decls, i, decl); i++)
4023 if (DECL_NAME (decl) == name)
4024 return decl;
4026 return NULL_TREE;
4029 /* Return the appropriate GCC tree code for the specified GNAT type,
4030 the latter being a record type as predicated by Is_Record_Type. */
4032 enum tree_code
4033 tree_code_for_record_type (Entity_Id gnat_type)
4035 Node_Id component_list
4036 = Component_List (Type_Definition
4037 (Declaration_Node
4038 (Implementation_Base_Type (gnat_type))));
4039 Node_Id component;
4041 /* Make this a UNION_TYPE unless it's either not an Unchecked_Union or
4042 we have a non-discriminant field outside a variant. In either case,
4043 it's a RECORD_TYPE. */
4045 if (!Is_Unchecked_Union (gnat_type))
4046 return RECORD_TYPE;
4048 for (component = First_Non_Pragma (Component_Items (component_list));
4049 Present (component);
4050 component = Next_Non_Pragma (component))
4051 if (Ekind (Defining_Entity (component)) == E_Component)
4052 return RECORD_TYPE;
4054 return UNION_TYPE;
4057 /* Return true if GNU_TYPE is suitable as the type of a non-aliased
4058 component of an aggregate type. */
4060 bool
4061 type_for_nonaliased_component_p (tree gnu_type)
4063 /* If the type is passed by reference, we may have pointers to the
4064 component so it cannot be made non-aliased. */
4065 if (must_pass_by_ref (gnu_type) || default_pass_by_ref (gnu_type))
4066 return false;
4068 /* We used to say that any component of aggregate type is aliased
4069 because the front-end may take 'Reference of it. The front-end
4070 has been enhanced in the meantime so as to use a renaming instead
4071 in most cases, but the back-end can probably take the address of
4072 such a component too so we go for the conservative stance.
4074 For instance, we might need the address of any array type, even
4075 if normally passed by copy, to construct a fat pointer if the
4076 component is used as an actual for an unconstrained formal.
4078 Likewise for record types: even if a specific record subtype is
4079 passed by copy, the parent type might be passed by ref (e.g. if
4080 it's of variable size) and we might take the address of a child
4081 component to pass to a parent formal. We have no way to check
4082 for such conditions here. */
4083 if (AGGREGATE_TYPE_P (gnu_type))
4084 return false;
4086 return true;
4089 /* Perform final processing on global variables. */
4091 void
4092 gnat_write_global_declarations (void)
4094 /* Proceed to optimize and emit assembly.
4095 FIXME: shouldn't be the front end's responsibility to call this. */
4096 cgraph_optimize ();
4098 /* Emit debug info for all global declarations. */
4099 emit_debug_global_declarations (VEC_address (tree, global_decls),
4100 VEC_length (tree, global_decls));
4103 #include "gt-ada-utils.h"
4104 #include "gtype-ada.h"