2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ada / utils.c
blob8dd445fd8cd735afd79158023454d6fd2a80d58b
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 /* We have attribute handlers using C specific format specifiers in warning
27 messages. Make sure they are properly recognized. */
28 #define GCC_DIAG_STYLE __gcc_cdiag__
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "tree.h"
35 #include "flags.h"
36 #include "defaults.h"
37 #include "toplev.h"
38 #include "output.h"
39 #include "ggc.h"
40 #include "debug.h"
41 #include "convert.h"
42 #include "target.h"
43 #include "function.h"
44 #include "cgraph.h"
45 #include "tree-inline.h"
46 #include "tree-gimple.h"
47 #include "tree-dump.h"
48 #include "pointer-set.h"
49 #include "langhooks.h"
51 #include "ada.h"
52 #include "types.h"
53 #include "atree.h"
54 #include "elists.h"
55 #include "namet.h"
56 #include "nlists.h"
57 #include "stringt.h"
58 #include "uintp.h"
59 #include "fe.h"
60 #include "sinfo.h"
61 #include "einfo.h"
62 #include "ada-tree.h"
63 #include "gigi.h"
65 #ifndef MAX_FIXED_MODE_SIZE
66 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
67 #endif
69 #ifndef MAX_BITS_PER_WORD
70 #define MAX_BITS_PER_WORD BITS_PER_WORD
71 #endif
73 /* If nonzero, pretend we are allocating at global level. */
74 int force_global;
76 /* Tree nodes for the various types and decls we create. */
77 tree gnat_std_decls[(int) ADT_LAST];
79 /* Functions to call for each of the possible raise reasons. */
80 tree gnat_raise_decls[(int) LAST_REASON_CODE + 1];
82 /* Forward declarations for handlers of attributes. */
83 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
84 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
85 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
86 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
87 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
88 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
89 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
90 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
91 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
93 /* Fake handler for attributes we don't properly support, typically because
94 they'd require dragging a lot of the common-c front-end circuitry. */
95 static tree fake_attribute_handler (tree *, tree, tree, int, bool *);
97 /* Table of machine-independent internal attributes for Ada. We support
98 this minimal set ot attributes to accomodate the needs of builtins. */
99 const struct attribute_spec gnat_internal_attribute_table[] =
101 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
102 { "const", 0, 0, true, false, false, handle_const_attribute },
103 { "nothrow", 0, 0, true, false, false, handle_nothrow_attribute },
104 { "pure", 0, 0, true, false, false, handle_pure_attribute },
105 { "no vops", 0, 0, true, false, false, handle_novops_attribute },
106 { "nonnull", 0, -1, false, true, true, handle_nonnull_attribute },
107 { "sentinel", 0, 1, false, true, true, handle_sentinel_attribute },
108 { "noreturn", 0, 0, true, false, false, handle_noreturn_attribute },
109 { "malloc", 0, 0, true, false, false, handle_malloc_attribute },
110 { "type generic", 0, 0, false, true, true, handle_type_generic_attribute },
112 /* ??? format and format_arg are heavy and not supported, which actually
113 prevents support for stdio builtins, which we however declare as part
114 of the common builtins.def contents. */
115 { "format", 3, 3, false, true, true, fake_attribute_handler },
116 { "format_arg", 1, 1, false, true, true, fake_attribute_handler },
118 { NULL, 0, 0, false, false, false, NULL }
121 /* Associates a GNAT tree node to a GCC tree node. It is used in
122 `save_gnu_tree', `get_gnu_tree' and `present_gnu_tree'. See documentation
123 of `save_gnu_tree' for more info. */
124 static GTY((length ("max_gnat_nodes"))) tree *associate_gnat_to_gnu;
126 #define GET_GNU_TREE(GNAT_ENTITY) \
127 associate_gnat_to_gnu[(GNAT_ENTITY) - First_Node_Id]
129 #define SET_GNU_TREE(GNAT_ENTITY,VAL) \
130 associate_gnat_to_gnu[(GNAT_ENTITY) - First_Node_Id] = (VAL)
132 #define PRESENT_GNU_TREE(GNAT_ENTITY) \
133 (associate_gnat_to_gnu[(GNAT_ENTITY) - First_Node_Id] != NULL_TREE)
135 /* Associates a GNAT entity to a GCC tree node used as a dummy, if any. */
136 static GTY((length ("max_gnat_nodes"))) tree *dummy_node_table;
138 #define GET_DUMMY_NODE(GNAT_ENTITY) \
139 dummy_node_table[(GNAT_ENTITY) - First_Node_Id]
141 #define SET_DUMMY_NODE(GNAT_ENTITY,VAL) \
142 dummy_node_table[(GNAT_ENTITY) - First_Node_Id] = (VAL)
144 #define PRESENT_DUMMY_NODE(GNAT_ENTITY) \
145 (dummy_node_table[(GNAT_ENTITY) - First_Node_Id] != NULL_TREE)
147 /* This variable keeps a table for types for each precision so that we only
148 allocate each of them once. Signed and unsigned types are kept separate.
150 Note that these types are only used when fold-const requests something
151 special. Perhaps we should NOT share these types; we'll see how it
152 goes later. */
153 static GTY(()) tree signed_and_unsigned_types[2 * MAX_BITS_PER_WORD + 1][2];
155 /* Likewise for float types, but record these by mode. */
156 static GTY(()) tree float_types[NUM_MACHINE_MODES];
158 /* For each binding contour we allocate a binding_level structure to indicate
159 the binding depth. */
161 struct gnat_binding_level GTY((chain_next ("%h.chain")))
163 /* The binding level containing this one (the enclosing binding level). */
164 struct gnat_binding_level *chain;
165 /* The BLOCK node for this level. */
166 tree block;
167 /* If nonzero, the setjmp buffer that needs to be updated for any
168 variable-sized definition within this context. */
169 tree jmpbuf_decl;
172 /* The binding level currently in effect. */
173 static GTY(()) struct gnat_binding_level *current_binding_level;
175 /* A chain of gnat_binding_level structures awaiting reuse. */
176 static GTY((deletable)) struct gnat_binding_level *free_binding_level;
178 /* An array of global declarations. */
179 static GTY(()) VEC(tree,gc) *global_decls;
181 /* An array of builtin function declarations. */
182 static GTY(()) VEC(tree,gc) *builtin_decls;
184 /* An array of global renaming pointers. */
185 static GTY(()) VEC(tree,gc) *global_renaming_pointers;
187 /* A chain of unused BLOCK nodes. */
188 static GTY((deletable)) tree free_block_chain;
190 static void gnat_install_builtins (void);
191 static tree merge_sizes (tree, tree, tree, bool, bool);
192 static tree compute_related_constant (tree, tree);
193 static tree split_plus (tree, tree *);
194 static void gnat_gimplify_function (tree);
195 static tree float_type_for_precision (int, enum machine_mode);
196 static tree convert_to_fat_pointer (tree, tree);
197 static tree convert_to_thin_pointer (tree, tree);
198 static tree make_descriptor_field (const char *,tree, tree, tree);
199 static bool potential_alignment_gap (tree, tree, tree);
201 /* Initialize the association of GNAT nodes to GCC trees. */
203 void
204 init_gnat_to_gnu (void)
206 associate_gnat_to_gnu
207 = (tree *) ggc_alloc_cleared (max_gnat_nodes * sizeof (tree));
210 /* GNAT_ENTITY is a GNAT tree node for an entity. GNU_DECL is the GCC tree
211 which is to be associated with GNAT_ENTITY. Such GCC tree node is always
212 a ..._DECL node. If NO_CHECK is nonzero, the latter check is suppressed.
214 If GNU_DECL is zero, a previous association is to be reset. */
216 void
217 save_gnu_tree (Entity_Id gnat_entity, tree gnu_decl, bool no_check)
219 /* Check that GNAT_ENTITY is not already defined and that it is being set
220 to something which is a decl. Raise gigi 401 if not. Usually, this
221 means GNAT_ENTITY is defined twice, but occasionally is due to some
222 Gigi problem. */
223 gcc_assert (!(gnu_decl
224 && (PRESENT_GNU_TREE (gnat_entity)
225 || (!no_check && !DECL_P (gnu_decl)))));
227 SET_GNU_TREE (gnat_entity, gnu_decl);
230 /* GNAT_ENTITY is a GNAT tree node for a defining identifier.
231 Return the ..._DECL node that was associated with it. If there is no tree
232 node associated with GNAT_ENTITY, abort.
234 In some cases, such as delayed elaboration or expressions that need to
235 be elaborated only once, GNAT_ENTITY is really not an entity. */
237 tree
238 get_gnu_tree (Entity_Id gnat_entity)
240 gcc_assert (PRESENT_GNU_TREE (gnat_entity));
241 return GET_GNU_TREE (gnat_entity);
244 /* Return nonzero if a GCC tree has been associated with GNAT_ENTITY. */
246 bool
247 present_gnu_tree (Entity_Id gnat_entity)
249 return PRESENT_GNU_TREE (gnat_entity);
252 /* Initialize the association of GNAT nodes to GCC trees as dummies. */
254 void
255 init_dummy_type (void)
257 dummy_node_table
258 = (tree *) ggc_alloc_cleared (max_gnat_nodes * sizeof (tree));
261 /* Make a dummy type corresponding to GNAT_TYPE. */
263 tree
264 make_dummy_type (Entity_Id gnat_type)
266 Entity_Id gnat_underlying = Gigi_Equivalent_Type (gnat_type);
267 tree gnu_type;
269 /* If there is an equivalent type, get its underlying type. */
270 if (Present (gnat_underlying))
271 gnat_underlying = Underlying_Type (gnat_underlying);
273 /* If there was no equivalent type (can only happen when just annotating
274 types) or underlying type, go back to the original type. */
275 if (No (gnat_underlying))
276 gnat_underlying = gnat_type;
278 /* If it there already a dummy type, use that one. Else make one. */
279 if (PRESENT_DUMMY_NODE (gnat_underlying))
280 return GET_DUMMY_NODE (gnat_underlying);
282 /* If this is a record, make a RECORD_TYPE or UNION_TYPE; else make
283 an ENUMERAL_TYPE. */
284 gnu_type = make_node (Is_Record_Type (gnat_underlying)
285 ? tree_code_for_record_type (gnat_underlying)
286 : ENUMERAL_TYPE);
287 TYPE_NAME (gnu_type) = get_entity_name (gnat_type);
288 TYPE_DUMMY_P (gnu_type) = 1;
289 if (AGGREGATE_TYPE_P (gnu_type))
291 TYPE_STUB_DECL (gnu_type) = build_decl (TYPE_DECL, NULL_TREE, gnu_type);
292 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_type);
295 SET_DUMMY_NODE (gnat_underlying, gnu_type);
297 return gnu_type;
300 /* Return nonzero if we are currently in the global binding level. */
303 global_bindings_p (void)
305 return ((force_global || !current_function_decl) ? -1 : 0);
308 /* Enter a new binding level. */
310 void
311 gnat_pushlevel ()
313 struct gnat_binding_level *newlevel = NULL;
315 /* Reuse a struct for this binding level, if there is one. */
316 if (free_binding_level)
318 newlevel = free_binding_level;
319 free_binding_level = free_binding_level->chain;
321 else
322 newlevel
323 = (struct gnat_binding_level *)
324 ggc_alloc (sizeof (struct gnat_binding_level));
326 /* Use a free BLOCK, if any; otherwise, allocate one. */
327 if (free_block_chain)
329 newlevel->block = free_block_chain;
330 free_block_chain = BLOCK_CHAIN (free_block_chain);
331 BLOCK_CHAIN (newlevel->block) = NULL_TREE;
333 else
334 newlevel->block = make_node (BLOCK);
336 /* Point the BLOCK we just made to its parent. */
337 if (current_binding_level)
338 BLOCK_SUPERCONTEXT (newlevel->block) = current_binding_level->block;
340 BLOCK_VARS (newlevel->block) = BLOCK_SUBBLOCKS (newlevel->block) = NULL_TREE;
341 TREE_USED (newlevel->block) = 1;
343 /* Add this level to the front of the chain (stack) of levels that are
344 active. */
345 newlevel->chain = current_binding_level;
346 newlevel->jmpbuf_decl = NULL_TREE;
347 current_binding_level = newlevel;
350 /* Set SUPERCONTEXT of the BLOCK for the current binding level to FNDECL
351 and point FNDECL to this BLOCK. */
353 void
354 set_current_block_context (tree fndecl)
356 BLOCK_SUPERCONTEXT (current_binding_level->block) = fndecl;
357 DECL_INITIAL (fndecl) = current_binding_level->block;
360 /* Set the jmpbuf_decl for the current binding level to DECL. */
362 void
363 set_block_jmpbuf_decl (tree decl)
365 current_binding_level->jmpbuf_decl = decl;
368 /* Get the jmpbuf_decl, if any, for the current binding level. */
370 tree
371 get_block_jmpbuf_decl ()
373 return current_binding_level->jmpbuf_decl;
376 /* Exit a binding level. Set any BLOCK into the current code group. */
378 void
379 gnat_poplevel ()
381 struct gnat_binding_level *level = current_binding_level;
382 tree block = level->block;
384 BLOCK_VARS (block) = nreverse (BLOCK_VARS (block));
385 BLOCK_SUBBLOCKS (block) = nreverse (BLOCK_SUBBLOCKS (block));
387 /* If this is a function-level BLOCK don't do anything. Otherwise, if there
388 are no variables free the block and merge its subblocks into those of its
389 parent block. Otherwise, add it to the list of its parent. */
390 if (TREE_CODE (BLOCK_SUPERCONTEXT (block)) == FUNCTION_DECL)
392 else if (BLOCK_VARS (block) == NULL_TREE)
394 BLOCK_SUBBLOCKS (level->chain->block)
395 = chainon (BLOCK_SUBBLOCKS (block),
396 BLOCK_SUBBLOCKS (level->chain->block));
397 BLOCK_CHAIN (block) = free_block_chain;
398 free_block_chain = block;
400 else
402 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (level->chain->block);
403 BLOCK_SUBBLOCKS (level->chain->block) = block;
404 TREE_USED (block) = 1;
405 set_block_for_group (block);
408 /* Free this binding structure. */
409 current_binding_level = level->chain;
410 level->chain = free_binding_level;
411 free_binding_level = level;
415 /* Records a ..._DECL node DECL as belonging to the current lexical scope
416 and uses GNAT_NODE for location information and propagating flags. */
418 void
419 gnat_pushdecl (tree decl, Node_Id gnat_node)
421 /* If at top level, there is no context. But PARM_DECLs always go in the
422 level of its function. */
423 if (global_bindings_p () && TREE_CODE (decl) != PARM_DECL)
424 DECL_CONTEXT (decl) = 0;
425 else
427 DECL_CONTEXT (decl) = current_function_decl;
429 /* Functions imported in another function are not really nested. */
430 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_PUBLIC (decl))
431 DECL_NO_STATIC_CHAIN (decl) = 1;
434 TREE_NO_WARNING (decl) = (gnat_node == Empty || Warnings_Off (gnat_node));
436 /* Set the location of DECL and emit a declaration for it. */
437 if (Present (gnat_node))
438 Sloc_to_locus (Sloc (gnat_node), &DECL_SOURCE_LOCATION (decl));
439 add_decl_expr (decl, gnat_node);
441 /* Put the declaration on the list. The list of declarations is in reverse
442 order. The list will be reversed later. Put global variables in the
443 globals list and builtin functions in a dedicated list to speed up
444 further lookups. Don't put TYPE_DECLs for UNCONSTRAINED_ARRAY_TYPE into
445 the list, as they will cause trouble with the debugger and aren't needed
446 anyway. */
447 if (TREE_CODE (decl) != TYPE_DECL
448 || TREE_CODE (TREE_TYPE (decl)) != UNCONSTRAINED_ARRAY_TYPE)
450 if (global_bindings_p ())
452 VEC_safe_push (tree, gc, global_decls, decl);
454 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
455 VEC_safe_push (tree, gc, builtin_decls, decl);
457 else
459 TREE_CHAIN (decl) = BLOCK_VARS (current_binding_level->block);
460 BLOCK_VARS (current_binding_level->block) = decl;
464 /* For the declaration of a type, set its name if it either is not already
465 set, was set to an IDENTIFIER_NODE, indicating an internal name,
466 or if the previous type name was not derived from a source name.
467 We'd rather have the type named with a real name and all the pointer
468 types to the same object have the same POINTER_TYPE node. Code in the
469 equivalent function of c-decl.c makes a copy of the type node here, but
470 that may cause us trouble with incomplete types. We make an exception
471 for fat pointer types because the compiler automatically builds them
472 for unconstrained array types and the debugger uses them to represent
473 both these and pointers to these. */
474 if (TREE_CODE (decl) == TYPE_DECL && DECL_NAME (decl))
476 tree t = TREE_TYPE (decl);
478 if (!TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) == IDENTIFIER_NODE)
480 else if (TYPE_FAT_POINTER_P (t))
482 tree tt = build_variant_type_copy (t);
483 TYPE_NAME (tt) = decl;
484 TREE_USED (tt) = TREE_USED (t);
485 TREE_TYPE (decl) = tt;
486 DECL_ORIGINAL_TYPE (decl) = t;
487 t = NULL_TREE;
489 else if (DECL_ARTIFICIAL (TYPE_NAME (t)) && !DECL_ARTIFICIAL (decl))
491 else
492 t = NULL_TREE;
494 /* Propagate the name to all the variants. This is needed for
495 the type qualifiers machinery to work properly. */
496 if (t)
497 for (t = TYPE_MAIN_VARIANT (t); t; t = TYPE_NEXT_VARIANT (t))
498 TYPE_NAME (t) = decl;
502 /* Do little here. Set up the standard declarations later after the
503 front end has been run. */
505 void
506 gnat_init_decl_processing (void)
508 /* Make the binding_level structure for global names. */
509 current_function_decl = 0;
510 current_binding_level = 0;
511 free_binding_level = 0;
512 gnat_pushlevel ();
514 build_common_tree_nodes (true, true);
516 /* In Ada, we use a signed type for SIZETYPE. Use the signed type
517 corresponding to the size of Pmode. In most cases when ptr_mode and
518 Pmode differ, C will use the width of ptr_mode as sizetype. But we get
519 far better code using the width of Pmode. Make this here since we need
520 this before we can expand the GNAT types. */
521 size_type_node = gnat_type_for_size (GET_MODE_BITSIZE (Pmode), 0);
522 set_sizetype (size_type_node);
523 build_common_tree_nodes_2 (0);
525 ptr_void_type_node = build_pointer_type (void_type_node);
528 /* Create the predefined scalar types such as `integer_type_node' needed
529 in the gcc back-end and initialize the global binding level. */
531 void
532 init_gigi_decls (tree long_long_float_type, tree exception_type)
534 tree endlink, decl;
535 unsigned int i;
537 /* Set the types that GCC and Gigi use from the front end. We would like
538 to do this for char_type_node, but it needs to correspond to the C
539 char type. */
540 if (TREE_CODE (TREE_TYPE (long_long_float_type)) == INTEGER_TYPE)
542 /* In this case, the builtin floating point types are VAX float,
543 so make up a type for use. */
544 longest_float_type_node = make_node (REAL_TYPE);
545 TYPE_PRECISION (longest_float_type_node) = LONG_DOUBLE_TYPE_SIZE;
546 layout_type (longest_float_type_node);
547 create_type_decl (get_identifier ("longest float type"),
548 longest_float_type_node, NULL, false, true, Empty);
550 else
551 longest_float_type_node = TREE_TYPE (long_long_float_type);
553 except_type_node = TREE_TYPE (exception_type);
555 unsigned_type_node = gnat_type_for_size (INT_TYPE_SIZE, 1);
556 create_type_decl (get_identifier ("unsigned int"), unsigned_type_node,
557 NULL, false, true, Empty);
559 void_type_decl_node = create_type_decl (get_identifier ("void"),
560 void_type_node, NULL, false, true,
561 Empty);
563 void_ftype = build_function_type (void_type_node, NULL_TREE);
564 ptr_void_ftype = build_pointer_type (void_ftype);
566 /* Build the special descriptor type and its null node if needed. */
567 if (TARGET_VTABLE_USES_DESCRIPTORS)
569 tree field_list = NULL_TREE, null_list = NULL_TREE;
570 int j;
572 fdesc_type_node = make_node (RECORD_TYPE);
574 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; j++)
576 tree field = create_field_decl (NULL_TREE, ptr_void_ftype,
577 fdesc_type_node, 0, 0, 0, 1);
578 TREE_CHAIN (field) = field_list;
579 field_list = field;
580 null_list = tree_cons (field, null_pointer_node, null_list);
583 finish_record_type (fdesc_type_node, nreverse (field_list), 0, false);
584 null_fdesc_node = gnat_build_constructor (fdesc_type_node, null_list);
587 /* Now declare runtime functions. */
588 endlink = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
590 /* malloc is a function declaration tree for a function to allocate
591 memory. */
592 malloc_decl = create_subprog_decl (get_identifier ("__gnat_malloc"),
593 NULL_TREE,
594 build_function_type (ptr_void_type_node,
595 tree_cons (NULL_TREE,
596 sizetype,
597 endlink)),
598 NULL_TREE, false, true, true, NULL,
599 Empty);
600 DECL_IS_MALLOC (malloc_decl) = 1;
602 /* malloc32 is a function declaration tree for a function to allocate
603 32bit memory on a 64bit system. Needed only on 64bit VMS. */
604 malloc32_decl = create_subprog_decl (get_identifier ("__gnat_malloc32"),
605 NULL_TREE,
606 build_function_type (ptr_void_type_node,
607 tree_cons (NULL_TREE,
608 sizetype,
609 endlink)),
610 NULL_TREE, false, true, true, NULL,
611 Empty);
612 DECL_IS_MALLOC (malloc32_decl) = 1;
614 /* free is a function declaration tree for a function to free memory. */
615 free_decl
616 = create_subprog_decl (get_identifier ("__gnat_free"), NULL_TREE,
617 build_function_type (void_type_node,
618 tree_cons (NULL_TREE,
619 ptr_void_type_node,
620 endlink)),
621 NULL_TREE, false, true, true, NULL, Empty);
623 /* Make the types and functions used for exception processing. */
624 jmpbuf_type
625 = build_array_type (gnat_type_for_mode (Pmode, 0),
626 build_index_type (build_int_cst (NULL_TREE, 5)));
627 create_type_decl (get_identifier ("JMPBUF_T"), jmpbuf_type, NULL,
628 true, true, Empty);
629 jmpbuf_ptr_type = build_pointer_type (jmpbuf_type);
631 /* Functions to get and set the jumpbuf pointer for the current thread. */
632 get_jmpbuf_decl
633 = create_subprog_decl
634 (get_identifier ("system__soft_links__get_jmpbuf_address_soft"),
635 NULL_TREE, build_function_type (jmpbuf_ptr_type, NULL_TREE),
636 NULL_TREE, false, true, true, NULL, Empty);
637 /* Avoid creating superfluous edges to __builtin_setjmp receivers. */
638 DECL_PURE_P (get_jmpbuf_decl) = 1;
640 set_jmpbuf_decl
641 = create_subprog_decl
642 (get_identifier ("system__soft_links__set_jmpbuf_address_soft"),
643 NULL_TREE,
644 build_function_type (void_type_node,
645 tree_cons (NULL_TREE, jmpbuf_ptr_type, endlink)),
646 NULL_TREE, false, true, true, NULL, Empty);
648 /* Function to get the current exception. */
649 get_excptr_decl
650 = create_subprog_decl
651 (get_identifier ("system__soft_links__get_gnat_exception"),
652 NULL_TREE,
653 build_function_type (build_pointer_type (except_type_node), NULL_TREE),
654 NULL_TREE, false, true, true, NULL, Empty);
655 /* Avoid creating superfluous edges to __builtin_setjmp receivers. */
656 DECL_PURE_P (get_excptr_decl) = 1;
658 /* Functions that raise exceptions. */
659 raise_nodefer_decl
660 = create_subprog_decl
661 (get_identifier ("__gnat_raise_nodefer_with_msg"), NULL_TREE,
662 build_function_type (void_type_node,
663 tree_cons (NULL_TREE,
664 build_pointer_type (except_type_node),
665 endlink)),
666 NULL_TREE, false, true, true, NULL, Empty);
668 /* Dummy objects to materialize "others" and "all others" in the exception
669 tables. These are exported by a-exexpr.adb, so see this unit for the
670 types to use. */
672 others_decl
673 = create_var_decl (get_identifier ("OTHERS"),
674 get_identifier ("__gnat_others_value"),
675 integer_type_node, 0, 1, 0, 1, 1, 0, Empty);
677 all_others_decl
678 = create_var_decl (get_identifier ("ALL_OTHERS"),
679 get_identifier ("__gnat_all_others_value"),
680 integer_type_node, 0, 1, 0, 1, 1, 0, Empty);
682 /* Hooks to call when entering/leaving an exception handler. */
683 begin_handler_decl
684 = create_subprog_decl (get_identifier ("__gnat_begin_handler"), NULL_TREE,
685 build_function_type (void_type_node,
686 tree_cons (NULL_TREE,
687 ptr_void_type_node,
688 endlink)),
689 NULL_TREE, false, true, true, NULL, Empty);
691 end_handler_decl
692 = create_subprog_decl (get_identifier ("__gnat_end_handler"), NULL_TREE,
693 build_function_type (void_type_node,
694 tree_cons (NULL_TREE,
695 ptr_void_type_node,
696 endlink)),
697 NULL_TREE, false, true, true, NULL, Empty);
699 /* If in no exception handlers mode, all raise statements are redirected to
700 __gnat_last_chance_handler. No need to redefine raise_nodefer_decl, since
701 this procedure will never be called in this mode. */
702 if (No_Exception_Handlers_Set ())
704 decl
705 = create_subprog_decl
706 (get_identifier ("__gnat_last_chance_handler"), NULL_TREE,
707 build_function_type (void_type_node,
708 tree_cons (NULL_TREE,
709 build_pointer_type (char_type_node),
710 tree_cons (NULL_TREE,
711 integer_type_node,
712 endlink))),
713 NULL_TREE, false, true, true, NULL, Empty);
715 for (i = 0; i < ARRAY_SIZE (gnat_raise_decls); i++)
716 gnat_raise_decls[i] = decl;
718 else
719 /* Otherwise, make one decl for each exception reason. */
720 for (i = 0; i < ARRAY_SIZE (gnat_raise_decls); i++)
722 char name[17];
724 sprintf (name, "__gnat_rcheck_%.2d", i);
725 gnat_raise_decls[i]
726 = create_subprog_decl
727 (get_identifier (name), NULL_TREE,
728 build_function_type (void_type_node,
729 tree_cons (NULL_TREE,
730 build_pointer_type
731 (char_type_node),
732 tree_cons (NULL_TREE,
733 integer_type_node,
734 endlink))),
735 NULL_TREE, false, true, true, NULL, Empty);
738 /* Indicate that these never return. */
739 TREE_THIS_VOLATILE (raise_nodefer_decl) = 1;
740 TREE_SIDE_EFFECTS (raise_nodefer_decl) = 1;
741 TREE_TYPE (raise_nodefer_decl)
742 = build_qualified_type (TREE_TYPE (raise_nodefer_decl),
743 TYPE_QUAL_VOLATILE);
745 for (i = 0; i < ARRAY_SIZE (gnat_raise_decls); i++)
747 TREE_THIS_VOLATILE (gnat_raise_decls[i]) = 1;
748 TREE_SIDE_EFFECTS (gnat_raise_decls[i]) = 1;
749 TREE_TYPE (gnat_raise_decls[i])
750 = build_qualified_type (TREE_TYPE (gnat_raise_decls[i]),
751 TYPE_QUAL_VOLATILE);
754 /* setjmp returns an integer and has one operand, which is a pointer to
755 a jmpbuf. */
756 setjmp_decl
757 = create_subprog_decl
758 (get_identifier ("__builtin_setjmp"), NULL_TREE,
759 build_function_type (integer_type_node,
760 tree_cons (NULL_TREE, jmpbuf_ptr_type, endlink)),
761 NULL_TREE, false, true, true, NULL, Empty);
763 DECL_BUILT_IN_CLASS (setjmp_decl) = BUILT_IN_NORMAL;
764 DECL_FUNCTION_CODE (setjmp_decl) = BUILT_IN_SETJMP;
766 /* update_setjmp_buf updates a setjmp buffer from the current stack pointer
767 address. */
768 update_setjmp_buf_decl
769 = create_subprog_decl
770 (get_identifier ("__builtin_update_setjmp_buf"), NULL_TREE,
771 build_function_type (void_type_node,
772 tree_cons (NULL_TREE, jmpbuf_ptr_type, endlink)),
773 NULL_TREE, false, true, true, NULL, Empty);
775 DECL_BUILT_IN_CLASS (update_setjmp_buf_decl) = BUILT_IN_NORMAL;
776 DECL_FUNCTION_CODE (update_setjmp_buf_decl) = BUILT_IN_UPDATE_SETJMP_BUF;
778 main_identifier_node = get_identifier ("main");
780 /* Install the builtins we might need, either internally or as
781 user available facilities for Intrinsic imports. */
782 gnat_install_builtins ();
785 /* Given a record type RECORD_TYPE and a chain of FIELD_DECL nodes FIELDLIST,
786 finish constructing the record or union type. If REP_LEVEL is zero, this
787 record has no representation clause and so will be entirely laid out here.
788 If REP_LEVEL is one, this record has a representation clause and has been
789 laid out already; only set the sizes and alignment. If REP_LEVEL is two,
790 this record is derived from a parent record and thus inherits its layout;
791 only make a pass on the fields to finalize them. If DO_NOT_FINALIZE is
792 true, the record type is expected to be modified afterwards so it will
793 not be sent to the back-end for finalization. */
795 void
796 finish_record_type (tree record_type, tree fieldlist, int rep_level,
797 bool do_not_finalize)
799 enum tree_code code = TREE_CODE (record_type);
800 tree name = TYPE_NAME (record_type);
801 tree ada_size = bitsize_zero_node;
802 tree size = bitsize_zero_node;
803 bool had_size = TYPE_SIZE (record_type) != 0;
804 bool had_size_unit = TYPE_SIZE_UNIT (record_type) != 0;
805 bool had_align = TYPE_ALIGN (record_type) != 0;
806 tree field;
808 if (name && TREE_CODE (name) == TYPE_DECL)
809 name = DECL_NAME (name);
811 TYPE_FIELDS (record_type) = fieldlist;
812 TYPE_STUB_DECL (record_type) = build_decl (TYPE_DECL, name, record_type);
814 /* We don't need both the typedef name and the record name output in
815 the debugging information, since they are the same. */
816 DECL_ARTIFICIAL (TYPE_STUB_DECL (record_type)) = 1;
818 /* Globally initialize the record first. If this is a rep'ed record,
819 that just means some initializations; otherwise, layout the record. */
820 if (rep_level > 0)
822 TYPE_ALIGN (record_type) = MAX (BITS_PER_UNIT, TYPE_ALIGN (record_type));
823 TYPE_MODE (record_type) = BLKmode;
825 if (!had_size_unit)
826 TYPE_SIZE_UNIT (record_type) = size_zero_node;
827 if (!had_size)
828 TYPE_SIZE (record_type) = bitsize_zero_node;
830 /* For all-repped records with a size specified, lay the QUAL_UNION_TYPE
831 out just like a UNION_TYPE, since the size will be fixed. */
832 else if (code == QUAL_UNION_TYPE)
833 code = UNION_TYPE;
835 else
837 /* Ensure there isn't a size already set. There can be in an error
838 case where there is a rep clause but all fields have errors and
839 no longer have a position. */
840 TYPE_SIZE (record_type) = 0;
841 layout_type (record_type);
844 /* At this point, the position and size of each field is known. It was
845 either set before entry by a rep clause, or by laying out the type above.
847 We now run a pass over the fields (in reverse order for QUAL_UNION_TYPEs)
848 to compute the Ada size; the GCC size and alignment (for rep'ed records
849 that are not padding types); and the mode (for rep'ed records). We also
850 clear the DECL_BIT_FIELD indication for the cases we know have not been
851 handled yet, and adjust DECL_NONADDRESSABLE_P accordingly. */
853 if (code == QUAL_UNION_TYPE)
854 fieldlist = nreverse (fieldlist);
856 for (field = fieldlist; field; field = TREE_CHAIN (field))
858 tree type = TREE_TYPE (field);
859 tree pos = bit_position (field);
860 tree this_size = DECL_SIZE (field);
861 tree this_ada_size;
863 if ((TREE_CODE (type) == RECORD_TYPE
864 || TREE_CODE (type) == UNION_TYPE
865 || TREE_CODE (type) == QUAL_UNION_TYPE)
866 && !TYPE_IS_FAT_POINTER_P (type)
867 && !TYPE_CONTAINS_TEMPLATE_P (type)
868 && TYPE_ADA_SIZE (type))
869 this_ada_size = TYPE_ADA_SIZE (type);
870 else
871 this_ada_size = this_size;
873 /* Clear DECL_BIT_FIELD for the cases layout_decl does not handle. */
874 if (DECL_BIT_FIELD (field)
875 && operand_equal_p (this_size, TYPE_SIZE (type), 0))
877 unsigned int align = TYPE_ALIGN (type);
879 /* In the general case, type alignment is required. */
880 if (value_factor_p (pos, align))
882 /* The enclosing record type must be sufficiently aligned.
883 Otherwise, if no alignment was specified for it and it
884 has been laid out already, bump its alignment to the
885 desired one if this is compatible with its size. */
886 if (TYPE_ALIGN (record_type) >= align)
888 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), align);
889 DECL_BIT_FIELD (field) = 0;
891 else if (!had_align
892 && rep_level == 0
893 && value_factor_p (TYPE_SIZE (record_type), align))
895 TYPE_ALIGN (record_type) = align;
896 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), align);
897 DECL_BIT_FIELD (field) = 0;
901 /* In the non-strict alignment case, only byte alignment is. */
902 if (!STRICT_ALIGNMENT
903 && DECL_BIT_FIELD (field)
904 && value_factor_p (pos, BITS_PER_UNIT))
905 DECL_BIT_FIELD (field) = 0;
908 /* If we still have DECL_BIT_FIELD set at this point, we know the field
909 is technically not addressable. Except that it can actually be
910 addressed if the field is BLKmode and happens to be properly
911 aligned. */
912 DECL_NONADDRESSABLE_P (field)
913 |= DECL_BIT_FIELD (field) && DECL_MODE (field) != BLKmode;
915 /* A type must be as aligned as its most aligned field that is not
916 a bit-field. But this is already enforced by layout_type. */
917 if (rep_level > 0 && !DECL_BIT_FIELD (field))
918 TYPE_ALIGN (record_type)
919 = MAX (TYPE_ALIGN (record_type), DECL_ALIGN (field));
921 switch (code)
923 case UNION_TYPE:
924 ada_size = size_binop (MAX_EXPR, ada_size, this_ada_size);
925 size = size_binop (MAX_EXPR, size, this_size);
926 break;
928 case QUAL_UNION_TYPE:
929 ada_size
930 = fold_build3 (COND_EXPR, bitsizetype, DECL_QUALIFIER (field),
931 this_ada_size, ada_size);
932 size = fold_build3 (COND_EXPR, bitsizetype, DECL_QUALIFIER (field),
933 this_size, size);
934 break;
936 case RECORD_TYPE:
937 /* Since we know here that all fields are sorted in order of
938 increasing bit position, the size of the record is one
939 higher than the ending bit of the last field processed
940 unless we have a rep clause, since in that case we might
941 have a field outside a QUAL_UNION_TYPE that has a higher ending
942 position. So use a MAX in that case. Also, if this field is a
943 QUAL_UNION_TYPE, we need to take into account the previous size in
944 the case of empty variants. */
945 ada_size
946 = merge_sizes (ada_size, pos, this_ada_size,
947 TREE_CODE (type) == QUAL_UNION_TYPE, rep_level > 0);
948 size
949 = merge_sizes (size, pos, this_size,
950 TREE_CODE (type) == QUAL_UNION_TYPE, rep_level > 0);
951 break;
953 default:
954 gcc_unreachable ();
958 if (code == QUAL_UNION_TYPE)
959 nreverse (fieldlist);
961 if (rep_level < 2)
963 /* If this is a padding record, we never want to make the size smaller
964 than what was specified in it, if any. */
965 if (TREE_CODE (record_type) == RECORD_TYPE
966 && TYPE_IS_PADDING_P (record_type) && TYPE_SIZE (record_type))
967 size = TYPE_SIZE (record_type);
969 /* Now set any of the values we've just computed that apply. */
970 if (!TYPE_IS_FAT_POINTER_P (record_type)
971 && !TYPE_CONTAINS_TEMPLATE_P (record_type))
972 SET_TYPE_ADA_SIZE (record_type, ada_size);
974 if (rep_level > 0)
976 tree size_unit = had_size_unit
977 ? TYPE_SIZE_UNIT (record_type)
978 : convert (sizetype,
979 size_binop (CEIL_DIV_EXPR, size,
980 bitsize_unit_node));
981 unsigned int align = TYPE_ALIGN (record_type);
983 TYPE_SIZE (record_type) = variable_size (round_up (size, align));
984 TYPE_SIZE_UNIT (record_type)
985 = variable_size (round_up (size_unit, align / BITS_PER_UNIT));
987 compute_record_mode (record_type);
991 if (!do_not_finalize)
992 rest_of_record_type_compilation (record_type);
995 /* Wrap up compilation of RECORD_TYPE, i.e. most notably output all
996 the debug information associated with it. It need not be invoked
997 directly in most cases since finish_record_type takes care of doing
998 so, unless explicitly requested not to through DO_NOT_FINALIZE. */
1000 void
1001 rest_of_record_type_compilation (tree record_type)
1003 tree fieldlist = TYPE_FIELDS (record_type);
1004 tree field;
1005 enum tree_code code = TREE_CODE (record_type);
1006 bool var_size = false;
1008 for (field = fieldlist; field; field = TREE_CHAIN (field))
1010 /* We need to make an XVE/XVU record if any field has variable size,
1011 whether or not the record does. For example, if we have a union,
1012 it may be that all fields, rounded up to the alignment, have the
1013 same size, in which case we'll use that size. But the debug
1014 output routines (except Dwarf2) won't be able to output the fields,
1015 so we need to make the special record. */
1016 if (TREE_CODE (DECL_SIZE (field)) != INTEGER_CST
1017 /* If a field has a non-constant qualifier, the record will have
1018 variable size too. */
1019 || (code == QUAL_UNION_TYPE
1020 && TREE_CODE (DECL_QUALIFIER (field)) != INTEGER_CST))
1022 var_size = true;
1023 break;
1027 /* If this record is of variable size, rename it so that the
1028 debugger knows it is and make a new, parallel, record
1029 that tells the debugger how the record is laid out. See
1030 exp_dbug.ads. But don't do this for records that are padding
1031 since they confuse GDB. */
1032 if (var_size
1033 && !(TREE_CODE (record_type) == RECORD_TYPE
1034 && TYPE_IS_PADDING_P (record_type)))
1036 tree new_record_type
1037 = make_node (TREE_CODE (record_type) == QUAL_UNION_TYPE
1038 ? UNION_TYPE : TREE_CODE (record_type));
1039 tree orig_name = TYPE_NAME (record_type);
1040 tree orig_id
1041 = (TREE_CODE (orig_name) == TYPE_DECL ? DECL_NAME (orig_name)
1042 : orig_name);
1043 tree new_id
1044 = concat_id_with_name (orig_id,
1045 TREE_CODE (record_type) == QUAL_UNION_TYPE
1046 ? "XVU" : "XVE");
1047 tree last_pos = bitsize_zero_node;
1048 tree old_field;
1049 tree prev_old_field = 0;
1051 TYPE_NAME (new_record_type) = new_id;
1052 TYPE_ALIGN (new_record_type) = BIGGEST_ALIGNMENT;
1053 TYPE_STUB_DECL (new_record_type)
1054 = build_decl (TYPE_DECL, new_id, new_record_type);
1055 DECL_ARTIFICIAL (TYPE_STUB_DECL (new_record_type)) = 1;
1056 DECL_IGNORED_P (TYPE_STUB_DECL (new_record_type))
1057 = DECL_IGNORED_P (TYPE_STUB_DECL (record_type));
1058 TYPE_SIZE (new_record_type) = size_int (TYPE_ALIGN (record_type));
1059 TYPE_SIZE_UNIT (new_record_type)
1060 = size_int (TYPE_ALIGN (record_type) / BITS_PER_UNIT);
1062 /* Now scan all the fields, replacing each field with a new
1063 field corresponding to the new encoding. */
1064 for (old_field = TYPE_FIELDS (record_type); old_field;
1065 old_field = TREE_CHAIN (old_field))
1067 tree field_type = TREE_TYPE (old_field);
1068 tree field_name = DECL_NAME (old_field);
1069 tree new_field;
1070 tree curpos = bit_position (old_field);
1071 bool var = false;
1072 unsigned int align = 0;
1073 tree pos;
1075 /* See how the position was modified from the last position.
1077 There are two basic cases we support: a value was added
1078 to the last position or the last position was rounded to
1079 a boundary and they something was added. Check for the
1080 first case first. If not, see if there is any evidence
1081 of rounding. If so, round the last position and try
1082 again.
1084 If this is a union, the position can be taken as zero. */
1086 if (TREE_CODE (new_record_type) == UNION_TYPE)
1087 pos = bitsize_zero_node, align = 0;
1088 else
1089 pos = compute_related_constant (curpos, last_pos);
1091 if (!pos && TREE_CODE (curpos) == MULT_EXPR
1092 && host_integerp (TREE_OPERAND (curpos, 1), 1))
1094 tree offset = TREE_OPERAND (curpos, 0);
1095 align = tree_low_cst (TREE_OPERAND (curpos, 1), 1);
1097 /* Strip off any conversions. */
1098 while (TREE_CODE (offset) == NON_LVALUE_EXPR
1099 || CONVERT_EXPR_P (offset))
1100 offset = TREE_OPERAND (offset, 0);
1102 /* An offset which is a bitwise AND with a negative power of 2
1103 means an alignment corresponding to this power of 2. */
1104 if (TREE_CODE (offset) == BIT_AND_EXPR
1105 && host_integerp (TREE_OPERAND (offset, 1), 0)
1106 && tree_int_cst_sgn (TREE_OPERAND (offset, 1)) < 0)
1108 unsigned int pow
1109 = - tree_low_cst (TREE_OPERAND (offset, 1), 0);
1110 if (exact_log2 (pow) > 0)
1111 align *= pow;
1114 pos = compute_related_constant (curpos,
1115 round_up (last_pos, align));
1117 else if (!pos && TREE_CODE (curpos) == PLUS_EXPR
1118 && TREE_CODE (TREE_OPERAND (curpos, 1)) == INTEGER_CST
1119 && TREE_CODE (TREE_OPERAND (curpos, 0)) == MULT_EXPR
1120 && host_integerp (TREE_OPERAND
1121 (TREE_OPERAND (curpos, 0), 1),
1124 align
1125 = tree_low_cst
1126 (TREE_OPERAND (TREE_OPERAND (curpos, 0), 1), 1);
1127 pos = compute_related_constant (curpos,
1128 round_up (last_pos, align));
1130 else if (potential_alignment_gap (prev_old_field, old_field,
1131 pos))
1133 align = TYPE_ALIGN (field_type);
1134 pos = compute_related_constant (curpos,
1135 round_up (last_pos, align));
1138 /* If we can't compute a position, set it to zero.
1140 ??? We really should abort here, but it's too much work
1141 to get this correct for all cases. */
1143 if (!pos)
1144 pos = bitsize_zero_node;
1146 /* See if this type is variable-sized and make a pointer type
1147 and indicate the indirection if so. Beware that the debug
1148 back-end may adjust the position computed above according
1149 to the alignment of the field type, i.e. the pointer type
1150 in this case, if we don't preventively counter that. */
1151 if (TREE_CODE (DECL_SIZE (old_field)) != INTEGER_CST)
1153 field_type = build_pointer_type (field_type);
1154 if (align != 0 && TYPE_ALIGN (field_type) > align)
1156 field_type = copy_node (field_type);
1157 TYPE_ALIGN (field_type) = align;
1159 var = true;
1162 /* Make a new field name, if necessary. */
1163 if (var || align != 0)
1165 char suffix[16];
1167 if (align != 0)
1168 sprintf (suffix, "XV%c%u", var ? 'L' : 'A',
1169 align / BITS_PER_UNIT);
1170 else
1171 strcpy (suffix, "XVL");
1173 field_name = concat_id_with_name (field_name, suffix);
1176 new_field = create_field_decl (field_name, field_type,
1177 new_record_type, 0,
1178 DECL_SIZE (old_field), pos, 0);
1179 TREE_CHAIN (new_field) = TYPE_FIELDS (new_record_type);
1180 TYPE_FIELDS (new_record_type) = new_field;
1182 /* If old_field is a QUAL_UNION_TYPE, take its size as being
1183 zero. The only time it's not the last field of the record
1184 is when there are other components at fixed positions after
1185 it (meaning there was a rep clause for every field) and we
1186 want to be able to encode them. */
1187 last_pos = size_binop (PLUS_EXPR, bit_position (old_field),
1188 (TREE_CODE (TREE_TYPE (old_field))
1189 == QUAL_UNION_TYPE)
1190 ? bitsize_zero_node
1191 : DECL_SIZE (old_field));
1192 prev_old_field = old_field;
1195 TYPE_FIELDS (new_record_type)
1196 = nreverse (TYPE_FIELDS (new_record_type));
1198 rest_of_type_decl_compilation (TYPE_STUB_DECL (new_record_type));
1201 rest_of_type_decl_compilation (TYPE_STUB_DECL (record_type));
1204 /* Utility function of above to merge LAST_SIZE, the previous size of a record
1205 with FIRST_BIT and SIZE that describe a field. SPECIAL is nonzero
1206 if this represents a QUAL_UNION_TYPE in which case we must look for
1207 COND_EXPRs and replace a value of zero with the old size. If HAS_REP
1208 is nonzero, we must take the MAX of the end position of this field
1209 with LAST_SIZE. In all other cases, we use FIRST_BIT plus SIZE.
1211 We return an expression for the size. */
1213 static tree
1214 merge_sizes (tree last_size, tree first_bit, tree size, bool special,
1215 bool has_rep)
1217 tree type = TREE_TYPE (last_size);
1218 tree new;
1220 if (!special || TREE_CODE (size) != COND_EXPR)
1222 new = size_binop (PLUS_EXPR, first_bit, size);
1223 if (has_rep)
1224 new = size_binop (MAX_EXPR, last_size, new);
1227 else
1228 new = fold_build3 (COND_EXPR, type, TREE_OPERAND (size, 0),
1229 integer_zerop (TREE_OPERAND (size, 1))
1230 ? last_size : merge_sizes (last_size, first_bit,
1231 TREE_OPERAND (size, 1),
1232 1, has_rep),
1233 integer_zerop (TREE_OPERAND (size, 2))
1234 ? last_size : merge_sizes (last_size, first_bit,
1235 TREE_OPERAND (size, 2),
1236 1, has_rep));
1238 /* We don't need any NON_VALUE_EXPRs and they can confuse us (especially
1239 when fed through substitute_in_expr) into thinking that a constant
1240 size is not constant. */
1241 while (TREE_CODE (new) == NON_LVALUE_EXPR)
1242 new = TREE_OPERAND (new, 0);
1244 return new;
1247 /* Utility function of above to see if OP0 and OP1, both of SIZETYPE, are
1248 related by the addition of a constant. Return that constant if so. */
1250 static tree
1251 compute_related_constant (tree op0, tree op1)
1253 tree op0_var, op1_var;
1254 tree op0_con = split_plus (op0, &op0_var);
1255 tree op1_con = split_plus (op1, &op1_var);
1256 tree result = size_binop (MINUS_EXPR, op0_con, op1_con);
1258 if (operand_equal_p (op0_var, op1_var, 0))
1259 return result;
1260 else if (operand_equal_p (op0, size_binop (PLUS_EXPR, op1_var, result), 0))
1261 return result;
1262 else
1263 return 0;
1266 /* Utility function of above to split a tree OP which may be a sum, into a
1267 constant part, which is returned, and a variable part, which is stored
1268 in *PVAR. *PVAR may be bitsize_zero_node. All operations must be of
1269 bitsizetype. */
1271 static tree
1272 split_plus (tree in, tree *pvar)
1274 /* Strip NOPS in order to ease the tree traversal and maximize the
1275 potential for constant or plus/minus discovery. We need to be careful
1276 to always return and set *pvar to bitsizetype trees, but it's worth
1277 the effort. */
1278 STRIP_NOPS (in);
1280 *pvar = convert (bitsizetype, in);
1282 if (TREE_CODE (in) == INTEGER_CST)
1284 *pvar = bitsize_zero_node;
1285 return convert (bitsizetype, in);
1287 else if (TREE_CODE (in) == PLUS_EXPR || TREE_CODE (in) == MINUS_EXPR)
1289 tree lhs_var, rhs_var;
1290 tree lhs_con = split_plus (TREE_OPERAND (in, 0), &lhs_var);
1291 tree rhs_con = split_plus (TREE_OPERAND (in, 1), &rhs_var);
1293 if (lhs_var == TREE_OPERAND (in, 0)
1294 && rhs_var == TREE_OPERAND (in, 1))
1295 return bitsize_zero_node;
1297 *pvar = size_binop (TREE_CODE (in), lhs_var, rhs_var);
1298 return size_binop (TREE_CODE (in), lhs_con, rhs_con);
1300 else
1301 return bitsize_zero_node;
1304 /* Return a FUNCTION_TYPE node. RETURN_TYPE is the type returned by the
1305 subprogram. If it is void_type_node, then we are dealing with a procedure,
1306 otherwise we are dealing with a function. PARAM_DECL_LIST is a list of
1307 PARM_DECL nodes that are the subprogram arguments. CICO_LIST is the
1308 copy-in/copy-out list to be stored into TYPE_CICO_LIST.
1309 RETURNS_UNCONSTRAINED is true if the function returns an unconstrained
1310 object. RETURNS_BY_REF is true if the function returns by reference.
1311 RETURNS_BY_TARGET_PTR is true if the function is to be passed (as its
1312 first parameter) the address of the place to copy its result. */
1314 tree
1315 create_subprog_type (tree return_type, tree param_decl_list, tree cico_list,
1316 bool returns_unconstrained, bool returns_by_ref,
1317 bool returns_by_target_ptr)
1319 /* A chain of TREE_LIST nodes whose TREE_VALUEs are the data type nodes of
1320 the subprogram formal parameters. This list is generated by traversing the
1321 input list of PARM_DECL nodes. */
1322 tree param_type_list = NULL;
1323 tree param_decl;
1324 tree type;
1326 for (param_decl = param_decl_list; param_decl;
1327 param_decl = TREE_CHAIN (param_decl))
1328 param_type_list = tree_cons (NULL_TREE, TREE_TYPE (param_decl),
1329 param_type_list);
1331 /* The list of the function parameter types has to be terminated by the void
1332 type to signal to the back-end that we are not dealing with a variable
1333 parameter subprogram, but that the subprogram has a fixed number of
1334 parameters. */
1335 param_type_list = tree_cons (NULL_TREE, void_type_node, param_type_list);
1337 /* The list of argument types has been created in reverse
1338 so nreverse it. */
1339 param_type_list = nreverse (param_type_list);
1341 type = build_function_type (return_type, param_type_list);
1343 /* TYPE may have been shared since GCC hashes types. If it has a CICO_LIST
1344 or the new type should, make a copy of TYPE. Likewise for
1345 RETURNS_UNCONSTRAINED and RETURNS_BY_REF. */
1346 if (TYPE_CI_CO_LIST (type) || cico_list
1347 || TYPE_RETURNS_UNCONSTRAINED_P (type) != returns_unconstrained
1348 || TYPE_RETURNS_BY_REF_P (type) != returns_by_ref
1349 || TYPE_RETURNS_BY_TARGET_PTR_P (type) != returns_by_target_ptr)
1350 type = copy_type (type);
1352 TYPE_CI_CO_LIST (type) = cico_list;
1353 TYPE_RETURNS_UNCONSTRAINED_P (type) = returns_unconstrained;
1354 TYPE_RETURNS_BY_REF_P (type) = returns_by_ref;
1355 TYPE_RETURNS_BY_TARGET_PTR_P (type) = returns_by_target_ptr;
1356 return type;
1359 /* Return a copy of TYPE but safe to modify in any way. */
1361 tree
1362 copy_type (tree type)
1364 tree new = copy_node (type);
1366 /* copy_node clears this field instead of copying it, because it is
1367 aliased with TREE_CHAIN. */
1368 TYPE_STUB_DECL (new) = TYPE_STUB_DECL (type);
1370 TYPE_POINTER_TO (new) = 0;
1371 TYPE_REFERENCE_TO (new) = 0;
1372 TYPE_MAIN_VARIANT (new) = new;
1373 TYPE_NEXT_VARIANT (new) = 0;
1375 return new;
1378 /* Return an INTEGER_TYPE of SIZETYPE with range MIN to MAX and whose
1379 TYPE_INDEX_TYPE is INDEX. GNAT_NODE is used for the position of
1380 the decl. */
1382 tree
1383 create_index_type (tree min, tree max, tree index, Node_Id gnat_node)
1385 /* First build a type for the desired range. */
1386 tree type = build_index_2_type (min, max);
1388 /* If this type has the TYPE_INDEX_TYPE we want, return it. Otherwise, if it
1389 doesn't have TYPE_INDEX_TYPE set, set it to INDEX. If TYPE_INDEX_TYPE
1390 is set, but not to INDEX, make a copy of this type with the requested
1391 index type. Note that we have no way of sharing these types, but that's
1392 only a small hole. */
1393 if (TYPE_INDEX_TYPE (type) == index)
1394 return type;
1395 else if (TYPE_INDEX_TYPE (type))
1396 type = copy_type (type);
1398 SET_TYPE_INDEX_TYPE (type, index);
1399 create_type_decl (NULL_TREE, type, NULL, true, false, gnat_node);
1400 return type;
1403 /* Return a TYPE_DECL node. TYPE_NAME gives the name of the type (a character
1404 string) and TYPE is a ..._TYPE node giving its data type.
1405 ARTIFICIAL_P is true if this is a declaration that was generated
1406 by the compiler. DEBUG_INFO_P is true if we need to write debugging
1407 information about this type. GNAT_NODE is used for the position of
1408 the decl. */
1410 tree
1411 create_type_decl (tree type_name, tree type, struct attrib *attr_list,
1412 bool artificial_p, bool debug_info_p, Node_Id gnat_node)
1414 tree type_decl = build_decl (TYPE_DECL, type_name, type);
1415 enum tree_code code = TREE_CODE (type);
1417 DECL_ARTIFICIAL (type_decl) = artificial_p;
1419 if (!TYPE_IS_DUMMY_P (type))
1420 gnat_pushdecl (type_decl, gnat_node);
1422 process_attributes (type_decl, attr_list);
1424 /* Pass type declaration information to the debugger unless this is an
1425 UNCONSTRAINED_ARRAY_TYPE, which the debugger does not support,
1426 and ENUMERAL_TYPE or RECORD_TYPE which is handled separately, or
1427 type for which debugging information was not requested. */
1428 if (code == UNCONSTRAINED_ARRAY_TYPE || !debug_info_p)
1429 DECL_IGNORED_P (type_decl) = 1;
1430 else if (code != ENUMERAL_TYPE
1431 && (code != RECORD_TYPE || TYPE_IS_FAT_POINTER_P (type))
1432 && !((code == POINTER_TYPE || code == REFERENCE_TYPE)
1433 && TYPE_IS_DUMMY_P (TREE_TYPE (type))))
1434 rest_of_type_decl_compilation (type_decl);
1436 return type_decl;
1439 /* Return a VAR_DECL or CONST_DECL node.
1441 VAR_NAME gives the name of the variable. ASM_NAME is its assembler name
1442 (if provided). TYPE is its data type (a GCC ..._TYPE node). VAR_INIT is
1443 the GCC tree for an optional initial expression; NULL_TREE if none.
1445 CONST_FLAG is true if this variable is constant, in which case we might
1446 return a CONST_DECL node unless CONST_DECL_ALLOWED_P is false.
1448 PUBLIC_FLAG is true if this definition is to be made visible outside of
1449 the current compilation unit. This flag should be set when processing the
1450 variable definitions in a package specification.
1452 EXTERN_FLAG is nonzero when processing an external variable declaration (as
1453 opposed to a definition: no storage is to be allocated for the variable).
1455 STATIC_FLAG is only relevant when not at top level. In that case
1456 it indicates whether to always allocate storage to the variable.
1458 GNAT_NODE is used for the position of the decl. */
1460 tree
1461 create_var_decl_1 (tree var_name, tree asm_name, tree type, tree var_init,
1462 bool const_flag, bool public_flag, bool extern_flag,
1463 bool static_flag, bool const_decl_allowed_p,
1464 struct attrib *attr_list, Node_Id gnat_node)
1466 bool init_const
1467 = (var_init != 0
1468 && gnat_types_compatible_p (type, TREE_TYPE (var_init))
1469 && (global_bindings_p () || static_flag
1470 ? initializer_constant_valid_p (var_init, TREE_TYPE (var_init)) != 0
1471 : TREE_CONSTANT (var_init)));
1473 /* Whether we will make TREE_CONSTANT the DECL we produce here, in which
1474 case the initializer may be used in-lieu of the DECL node (as done in
1475 Identifier_to_gnu). This is useful to prevent the need of elaboration
1476 code when an identifier for which such a decl is made is in turn used as
1477 an initializer. We used to rely on CONST vs VAR_DECL for this purpose,
1478 but extra constraints apply to this choice (see below) and are not
1479 relevant to the distinction we wish to make. */
1480 bool constant_p = const_flag && init_const;
1482 /* The actual DECL node. CONST_DECL was initially intended for enumerals
1483 and may be used for scalars in general but not for aggregates. */
1484 tree var_decl
1485 = build_decl ((constant_p && const_decl_allowed_p
1486 && !AGGREGATE_TYPE_P (type)) ? CONST_DECL : VAR_DECL,
1487 var_name, type);
1489 /* If this is external, throw away any initializations (they will be done
1490 elsewhere) unless this is a constant for which we would like to remain
1491 able to get the initializer. If we are defining a global here, leave a
1492 constant initialization and save any variable elaborations for the
1493 elaboration routine. If we are just annotating types, throw away the
1494 initialization if it isn't a constant. */
1495 if ((extern_flag && !constant_p)
1496 || (type_annotate_only && var_init && !TREE_CONSTANT (var_init)))
1497 var_init = NULL_TREE;
1499 /* At the global level, an initializer requiring code to be generated
1500 produces elaboration statements. Check that such statements are allowed,
1501 that is, not violating a No_Elaboration_Code restriction. */
1502 if (global_bindings_p () && var_init != 0 && ! init_const)
1503 Check_Elaboration_Code_Allowed (gnat_node);
1505 /* Ada doesn't feature Fortran-like COMMON variables so we shouldn't
1506 try to fiddle with DECL_COMMON. However, on platforms that don't
1507 support global BSS sections, uninitialized global variables would
1508 go in DATA instead, thus increasing the size of the executable. */
1509 if (!flag_no_common
1510 && TREE_CODE (var_decl) == VAR_DECL
1511 && !have_global_bss_p ())
1512 DECL_COMMON (var_decl) = 1;
1513 DECL_INITIAL (var_decl) = var_init;
1514 TREE_READONLY (var_decl) = const_flag;
1515 DECL_EXTERNAL (var_decl) = extern_flag;
1516 TREE_PUBLIC (var_decl) = public_flag || extern_flag;
1517 TREE_CONSTANT (var_decl) = constant_p;
1518 TREE_THIS_VOLATILE (var_decl) = TREE_SIDE_EFFECTS (var_decl)
1519 = TYPE_VOLATILE (type);
1521 /* If it's public and not external, always allocate storage for it.
1522 At the global binding level we need to allocate static storage for the
1523 variable if and only if it's not external. If we are not at the top level
1524 we allocate automatic storage unless requested not to. */
1525 TREE_STATIC (var_decl)
1526 = public_flag || (global_bindings_p () ? !extern_flag : static_flag);
1528 if (asm_name && VAR_OR_FUNCTION_DECL_P (var_decl))
1529 SET_DECL_ASSEMBLER_NAME (var_decl, asm_name);
1531 process_attributes (var_decl, attr_list);
1533 /* Add this decl to the current binding level. */
1534 gnat_pushdecl (var_decl, gnat_node);
1536 if (TREE_SIDE_EFFECTS (var_decl))
1537 TREE_ADDRESSABLE (var_decl) = 1;
1539 if (TREE_CODE (var_decl) != CONST_DECL)
1541 if (global_bindings_p ())
1542 rest_of_decl_compilation (var_decl, true, 0);
1544 else
1545 expand_decl (var_decl);
1547 return var_decl;
1550 /* Return true if TYPE, an aggregate type, contains (or is) an array. */
1552 static bool
1553 aggregate_type_contains_array_p (tree type)
1555 switch (TREE_CODE (type))
1557 case RECORD_TYPE:
1558 case UNION_TYPE:
1559 case QUAL_UNION_TYPE:
1561 tree field;
1562 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1563 if (AGGREGATE_TYPE_P (TREE_TYPE (field))
1564 && aggregate_type_contains_array_p (TREE_TYPE (field)))
1565 return true;
1566 return false;
1569 case ARRAY_TYPE:
1570 return true;
1572 default:
1573 gcc_unreachable ();
1577 /* Returns a FIELD_DECL node. FIELD_NAME the field name, FIELD_TYPE is its
1578 type, and RECORD_TYPE is the type of the parent. PACKED is nonzero if
1579 this field is in a record type with a "pragma pack". If SIZE is nonzero
1580 it is the specified size for this field. If POS is nonzero, it is the bit
1581 position. If ADDRESSABLE is nonzero, it means we are allowed to take
1582 the address of this field for aliasing purposes. If it is negative, we
1583 should not make a bitfield, which is used by make_aligning_type. */
1585 tree
1586 create_field_decl (tree field_name, tree field_type, tree record_type,
1587 int packed, tree size, tree pos, int addressable)
1589 tree field_decl = build_decl (FIELD_DECL, field_name, field_type);
1591 DECL_CONTEXT (field_decl) = record_type;
1592 TREE_READONLY (field_decl) = TYPE_READONLY (field_type);
1594 /* If FIELD_TYPE is BLKmode, we must ensure this is aligned to at least a
1595 byte boundary since GCC cannot handle less-aligned BLKmode bitfields.
1596 Likewise for an aggregate without specified position that contains an
1597 array, because in this case slices of variable length of this array
1598 must be handled by GCC and variable-sized objects need to be aligned
1599 to at least a byte boundary. */
1600 if (packed && (TYPE_MODE (field_type) == BLKmode
1601 || (!pos
1602 && AGGREGATE_TYPE_P (field_type)
1603 && aggregate_type_contains_array_p (field_type))))
1604 DECL_ALIGN (field_decl) = BITS_PER_UNIT;
1606 /* If a size is specified, use it. Otherwise, if the record type is packed
1607 compute a size to use, which may differ from the object's natural size.
1608 We always set a size in this case to trigger the checks for bitfield
1609 creation below, which is typically required when no position has been
1610 specified. */
1611 if (size)
1612 size = convert (bitsizetype, size);
1613 else if (packed == 1)
1615 size = rm_size (field_type);
1617 /* For a constant size larger than MAX_FIXED_MODE_SIZE, round up to
1618 byte. */
1619 if (TREE_CODE (size) == INTEGER_CST
1620 && compare_tree_int (size, MAX_FIXED_MODE_SIZE) > 0)
1621 size = round_up (size, BITS_PER_UNIT);
1624 /* If we may, according to ADDRESSABLE, make a bitfield if a size is
1625 specified for two reasons: first if the size differs from the natural
1626 size. Second, if the alignment is insufficient. There are a number of
1627 ways the latter can be true.
1629 We never make a bitfield if the type of the field has a nonconstant size,
1630 because no such entity requiring bitfield operations should reach here.
1632 We do *preventively* make a bitfield when there might be the need for it
1633 but we don't have all the necessary information to decide, as is the case
1634 of a field with no specified position in a packed record.
1636 We also don't look at STRICT_ALIGNMENT here, and rely on later processing
1637 in layout_decl or finish_record_type to clear the bit_field indication if
1638 it is in fact not needed. */
1639 if (addressable >= 0
1640 && size
1641 && TREE_CODE (size) == INTEGER_CST
1642 && TREE_CODE (TYPE_SIZE (field_type)) == INTEGER_CST
1643 && (!tree_int_cst_equal (size, TYPE_SIZE (field_type))
1644 || (pos && !value_factor_p (pos, TYPE_ALIGN (field_type)))
1645 || packed
1646 || (TYPE_ALIGN (record_type) != 0
1647 && TYPE_ALIGN (record_type) < TYPE_ALIGN (field_type))))
1649 DECL_BIT_FIELD (field_decl) = 1;
1650 DECL_SIZE (field_decl) = size;
1651 if (!packed && !pos)
1652 DECL_ALIGN (field_decl)
1653 = (TYPE_ALIGN (record_type) != 0
1654 ? MIN (TYPE_ALIGN (record_type), TYPE_ALIGN (field_type))
1655 : TYPE_ALIGN (field_type));
1658 DECL_PACKED (field_decl) = pos ? DECL_BIT_FIELD (field_decl) : packed;
1660 /* Bump the alignment if need be, either for bitfield/packing purposes or
1661 to satisfy the type requirements if no such consideration applies. When
1662 we get the alignment from the type, indicate if this is from an explicit
1663 user request, which prevents stor-layout from lowering it later on. */
1665 int bit_align
1666 = (DECL_BIT_FIELD (field_decl) ? 1
1667 : packed && TYPE_MODE (field_type) != BLKmode ? BITS_PER_UNIT : 0);
1669 if (bit_align > DECL_ALIGN (field_decl))
1670 DECL_ALIGN (field_decl) = bit_align;
1671 else if (!bit_align && TYPE_ALIGN (field_type) > DECL_ALIGN (field_decl))
1673 DECL_ALIGN (field_decl) = TYPE_ALIGN (field_type);
1674 DECL_USER_ALIGN (field_decl) = TYPE_USER_ALIGN (field_type);
1678 if (pos)
1680 /* We need to pass in the alignment the DECL is known to have.
1681 This is the lowest-order bit set in POS, but no more than
1682 the alignment of the record, if one is specified. Note
1683 that an alignment of 0 is taken as infinite. */
1684 unsigned int known_align;
1686 if (host_integerp (pos, 1))
1687 known_align = tree_low_cst (pos, 1) & - tree_low_cst (pos, 1);
1688 else
1689 known_align = BITS_PER_UNIT;
1691 if (TYPE_ALIGN (record_type)
1692 && (known_align == 0 || known_align > TYPE_ALIGN (record_type)))
1693 known_align = TYPE_ALIGN (record_type);
1695 layout_decl (field_decl, known_align);
1696 SET_DECL_OFFSET_ALIGN (field_decl,
1697 host_integerp (pos, 1) ? BIGGEST_ALIGNMENT
1698 : BITS_PER_UNIT);
1699 pos_from_bit (&DECL_FIELD_OFFSET (field_decl),
1700 &DECL_FIELD_BIT_OFFSET (field_decl),
1701 DECL_OFFSET_ALIGN (field_decl), pos);
1703 DECL_HAS_REP_P (field_decl) = 1;
1706 /* In addition to what our caller says, claim the field is addressable if we
1707 know that its type is not suitable.
1709 The field may also be "technically" nonaddressable, meaning that even if
1710 we attempt to take the field's address we will actually get the address
1711 of a copy. This is the case for true bitfields, but the DECL_BIT_FIELD
1712 value we have at this point is not accurate enough, so we don't account
1713 for this here and let finish_record_type decide. */
1714 if (!type_for_nonaliased_component_p (field_type))
1715 addressable = 1;
1717 DECL_NONADDRESSABLE_P (field_decl) = !addressable;
1719 return field_decl;
1722 /* Returns a PARM_DECL node. PARAM_NAME is the name of the parameter,
1723 PARAM_TYPE is its type. READONLY is true if the parameter is
1724 readonly (either an In parameter or an address of a pass-by-ref
1725 parameter). */
1727 tree
1728 create_param_decl (tree param_name, tree param_type, bool readonly)
1730 tree param_decl = build_decl (PARM_DECL, param_name, param_type);
1732 /* Honor targetm.calls.promote_prototypes(), as not doing so can
1733 lead to various ABI violations. */
1734 if (targetm.calls.promote_prototypes (param_type)
1735 && (TREE_CODE (param_type) == INTEGER_TYPE
1736 || TREE_CODE (param_type) == ENUMERAL_TYPE)
1737 && TYPE_PRECISION (param_type) < TYPE_PRECISION (integer_type_node))
1739 /* We have to be careful about biased types here. Make a subtype
1740 of integer_type_node with the proper biasing. */
1741 if (TREE_CODE (param_type) == INTEGER_TYPE
1742 && TYPE_BIASED_REPRESENTATION_P (param_type))
1744 param_type
1745 = copy_type (build_range_type (integer_type_node,
1746 TYPE_MIN_VALUE (param_type),
1747 TYPE_MAX_VALUE (param_type)));
1749 TYPE_BIASED_REPRESENTATION_P (param_type) = 1;
1751 else
1752 param_type = integer_type_node;
1755 DECL_ARG_TYPE (param_decl) = param_type;
1756 TREE_READONLY (param_decl) = readonly;
1757 return param_decl;
1760 /* Given a DECL and ATTR_LIST, process the listed attributes. */
1762 void
1763 process_attributes (tree decl, struct attrib *attr_list)
1765 for (; attr_list; attr_list = attr_list->next)
1766 switch (attr_list->type)
1768 case ATTR_MACHINE_ATTRIBUTE:
1769 decl_attributes (&decl, tree_cons (attr_list->name, attr_list->args,
1770 NULL_TREE),
1771 ATTR_FLAG_TYPE_IN_PLACE);
1772 break;
1774 case ATTR_LINK_ALIAS:
1775 if (! DECL_EXTERNAL (decl))
1777 TREE_STATIC (decl) = 1;
1778 assemble_alias (decl, attr_list->name);
1780 break;
1782 case ATTR_WEAK_EXTERNAL:
1783 if (SUPPORTS_WEAK)
1784 declare_weak (decl);
1785 else
1786 post_error ("?weak declarations not supported on this target",
1787 attr_list->error_point);
1788 break;
1790 case ATTR_LINK_SECTION:
1791 if (targetm.have_named_sections)
1793 DECL_SECTION_NAME (decl)
1794 = build_string (IDENTIFIER_LENGTH (attr_list->name),
1795 IDENTIFIER_POINTER (attr_list->name));
1796 DECL_COMMON (decl) = 0;
1798 else
1799 post_error ("?section attributes are not supported for this target",
1800 attr_list->error_point);
1801 break;
1803 case ATTR_LINK_CONSTRUCTOR:
1804 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1805 TREE_USED (decl) = 1;
1806 break;
1808 case ATTR_LINK_DESTRUCTOR:
1809 DECL_STATIC_DESTRUCTOR (decl) = 1;
1810 TREE_USED (decl) = 1;
1811 break;
1815 /* Record a global renaming pointer. */
1817 void
1818 record_global_renaming_pointer (tree decl)
1820 gcc_assert (DECL_RENAMED_OBJECT (decl));
1821 VEC_safe_push (tree, gc, global_renaming_pointers, decl);
1824 /* Invalidate the global renaming pointers. */
1826 void
1827 invalidate_global_renaming_pointers (void)
1829 unsigned int i;
1830 tree iter;
1832 for (i = 0; VEC_iterate(tree, global_renaming_pointers, i, iter); i++)
1833 SET_DECL_RENAMED_OBJECT (iter, NULL_TREE);
1835 VEC_free (tree, gc, global_renaming_pointers);
1838 /* Return true if VALUE is a known to be a multiple of FACTOR, which must be
1839 a power of 2. */
1841 bool
1842 value_factor_p (tree value, HOST_WIDE_INT factor)
1844 if (host_integerp (value, 1))
1845 return tree_low_cst (value, 1) % factor == 0;
1847 if (TREE_CODE (value) == MULT_EXPR)
1848 return (value_factor_p (TREE_OPERAND (value, 0), factor)
1849 || value_factor_p (TREE_OPERAND (value, 1), factor));
1851 return false;
1854 /* Given 2 consecutive field decls PREV_FIELD and CURR_FIELD, return true
1855 unless we can prove these 2 fields are laid out in such a way that no gap
1856 exist between the end of PREV_FIELD and the beginning of CURR_FIELD. OFFSET
1857 is the distance in bits between the end of PREV_FIELD and the starting
1858 position of CURR_FIELD. It is ignored if null. */
1860 static bool
1861 potential_alignment_gap (tree prev_field, tree curr_field, tree offset)
1863 /* If this is the first field of the record, there cannot be any gap */
1864 if (!prev_field)
1865 return false;
1867 /* If the previous field is a union type, then return False: The only
1868 time when such a field is not the last field of the record is when
1869 there are other components at fixed positions after it (meaning there
1870 was a rep clause for every field), in which case we don't want the
1871 alignment constraint to override them. */
1872 if (TREE_CODE (TREE_TYPE (prev_field)) == QUAL_UNION_TYPE)
1873 return false;
1875 /* If the distance between the end of prev_field and the beginning of
1876 curr_field is constant, then there is a gap if the value of this
1877 constant is not null. */
1878 if (offset && host_integerp (offset, 1))
1879 return !integer_zerop (offset);
1881 /* If the size and position of the previous field are constant,
1882 then check the sum of this size and position. There will be a gap
1883 iff it is not multiple of the current field alignment. */
1884 if (host_integerp (DECL_SIZE (prev_field), 1)
1885 && host_integerp (bit_position (prev_field), 1))
1886 return ((tree_low_cst (bit_position (prev_field), 1)
1887 + tree_low_cst (DECL_SIZE (prev_field), 1))
1888 % DECL_ALIGN (curr_field) != 0);
1890 /* If both the position and size of the previous field are multiples
1891 of the current field alignment, there cannot be any gap. */
1892 if (value_factor_p (bit_position (prev_field), DECL_ALIGN (curr_field))
1893 && value_factor_p (DECL_SIZE (prev_field), DECL_ALIGN (curr_field)))
1894 return false;
1896 /* Fallback, return that there may be a potential gap */
1897 return true;
1900 /* Returns a LABEL_DECL node for LABEL_NAME. */
1902 tree
1903 create_label_decl (tree label_name)
1905 tree label_decl = build_decl (LABEL_DECL, label_name, void_type_node);
1907 DECL_CONTEXT (label_decl) = current_function_decl;
1908 DECL_MODE (label_decl) = VOIDmode;
1909 DECL_SOURCE_LOCATION (label_decl) = input_location;
1911 return label_decl;
1914 /* Returns a FUNCTION_DECL node. SUBPROG_NAME is the name of the subprogram,
1915 ASM_NAME is its assembler name, SUBPROG_TYPE is its type (a FUNCTION_TYPE
1916 node), PARAM_DECL_LIST is the list of the subprogram arguments (a list of
1917 PARM_DECL nodes chained through the TREE_CHAIN field).
1919 INLINE_FLAG, PUBLIC_FLAG, EXTERN_FLAG, and ATTR_LIST are used to set the
1920 appropriate fields in the FUNCTION_DECL. GNAT_NODE gives the location. */
1922 tree
1923 create_subprog_decl (tree subprog_name, tree asm_name,
1924 tree subprog_type, tree param_decl_list, bool inline_flag,
1925 bool public_flag, bool extern_flag,
1926 struct attrib *attr_list, Node_Id gnat_node)
1928 tree return_type = TREE_TYPE (subprog_type);
1929 tree subprog_decl = build_decl (FUNCTION_DECL, subprog_name, subprog_type);
1931 /* If this is a function nested inside an inlined external function, it
1932 means we aren't going to compile the outer function unless it is
1933 actually inlined, so do the same for us. */
1934 if (current_function_decl && DECL_INLINE (current_function_decl)
1935 && DECL_EXTERNAL (current_function_decl))
1936 extern_flag = true;
1938 DECL_EXTERNAL (subprog_decl) = extern_flag;
1939 TREE_PUBLIC (subprog_decl) = public_flag;
1940 TREE_STATIC (subprog_decl) = 1;
1941 TREE_READONLY (subprog_decl) = TYPE_READONLY (subprog_type);
1942 TREE_THIS_VOLATILE (subprog_decl) = TYPE_VOLATILE (subprog_type);
1943 TREE_SIDE_EFFECTS (subprog_decl) = TYPE_VOLATILE (subprog_type);
1944 DECL_ARGUMENTS (subprog_decl) = param_decl_list;
1945 DECL_RESULT (subprog_decl) = build_decl (RESULT_DECL, 0, return_type);
1946 DECL_ARTIFICIAL (DECL_RESULT (subprog_decl)) = 1;
1947 DECL_IGNORED_P (DECL_RESULT (subprog_decl)) = 1;
1949 /* TREE_ADDRESSABLE is set on the result type to request the use of the
1950 target by-reference return mechanism. This is not supported all the
1951 way down to RTL expansion with GCC 4, which ICEs on temporary creation
1952 attempts with such a type and expects DECL_BY_REFERENCE to be set on
1953 the RESULT_DECL instead - see gnat_genericize for more details. */
1954 if (TREE_ADDRESSABLE (TREE_TYPE (DECL_RESULT (subprog_decl))))
1956 tree result_decl = DECL_RESULT (subprog_decl);
1958 TREE_ADDRESSABLE (TREE_TYPE (result_decl)) = 0;
1959 DECL_BY_REFERENCE (result_decl) = 1;
1962 if (inline_flag)
1963 DECL_DECLARED_INLINE_P (subprog_decl) = 1;
1965 if (asm_name)
1966 SET_DECL_ASSEMBLER_NAME (subprog_decl, asm_name);
1968 process_attributes (subprog_decl, attr_list);
1970 /* Add this decl to the current binding level. */
1971 gnat_pushdecl (subprog_decl, gnat_node);
1973 /* Output the assembler code and/or RTL for the declaration. */
1974 rest_of_decl_compilation (subprog_decl, global_bindings_p (), 0);
1976 return subprog_decl;
1979 /* Set up the framework for generating code for SUBPROG_DECL, a subprogram
1980 body. This routine needs to be invoked before processing the declarations
1981 appearing in the subprogram. */
1983 void
1984 begin_subprog_body (tree subprog_decl)
1986 tree param_decl;
1988 current_function_decl = subprog_decl;
1989 announce_function (subprog_decl);
1991 /* Enter a new binding level and show that all the parameters belong to
1992 this function. */
1993 gnat_pushlevel ();
1994 for (param_decl = DECL_ARGUMENTS (subprog_decl); param_decl;
1995 param_decl = TREE_CHAIN (param_decl))
1996 DECL_CONTEXT (param_decl) = subprog_decl;
1998 make_decl_rtl (subprog_decl);
2000 /* We handle pending sizes via the elaboration of types, so we don't need to
2001 save them. This causes them to be marked as part of the outer function
2002 and then discarded. */
2003 get_pending_sizes ();
2007 /* Helper for the genericization callback. Return a dereference of VAL
2008 if it is of a reference type. */
2010 static tree
2011 convert_from_reference (tree val)
2013 tree value_type, ref;
2015 if (TREE_CODE (TREE_TYPE (val)) != REFERENCE_TYPE)
2016 return val;
2018 value_type = TREE_TYPE (TREE_TYPE (val));
2019 ref = build1 (INDIRECT_REF, value_type, val);
2021 /* See if what we reference is CONST or VOLATILE, which requires
2022 looking into array types to get to the component type. */
2024 while (TREE_CODE (value_type) == ARRAY_TYPE)
2025 value_type = TREE_TYPE (value_type);
2027 TREE_READONLY (ref)
2028 = (TYPE_QUALS (value_type) & TYPE_QUAL_CONST);
2029 TREE_THIS_VOLATILE (ref)
2030 = (TYPE_QUALS (value_type) & TYPE_QUAL_VOLATILE);
2032 TREE_SIDE_EFFECTS (ref)
2033 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
2035 return ref;
2038 /* Helper for the genericization callback. Returns true if T denotes
2039 a RESULT_DECL with DECL_BY_REFERENCE set. */
2041 static inline bool
2042 is_byref_result (tree t)
2044 return (TREE_CODE (t) == RESULT_DECL && DECL_BY_REFERENCE (t));
2048 /* Tree walking callback for gnat_genericize. Currently ...
2050 o Adjust references to the function's DECL_RESULT if it is marked
2051 DECL_BY_REFERENCE and so has had its type turned into a reference
2052 type at the end of the function compilation. */
2054 static tree
2055 gnat_genericize_r (tree *stmt_p, int *walk_subtrees, void *data)
2057 /* This implementation is modeled after what the C++ front-end is
2058 doing, basis of the downstream passes behavior. */
2060 tree stmt = *stmt_p;
2061 struct pointer_set_t *p_set = (struct pointer_set_t*) data;
2063 /* If we have a direct mention of the result decl, dereference. */
2064 if (is_byref_result (stmt))
2066 *stmt_p = convert_from_reference (stmt);
2067 *walk_subtrees = 0;
2068 return NULL;
2071 /* Otherwise, no need to walk the same tree twice. */
2072 if (pointer_set_contains (p_set, stmt))
2074 *walk_subtrees = 0;
2075 return NULL_TREE;
2078 /* If we are taking the address of what now is a reference, just get the
2079 reference value. */
2080 if (TREE_CODE (stmt) == ADDR_EXPR
2081 && is_byref_result (TREE_OPERAND (stmt, 0)))
2083 *stmt_p = convert (TREE_TYPE (stmt), TREE_OPERAND (stmt, 0));
2084 *walk_subtrees = 0;
2087 /* Don't dereference an by-reference RESULT_DECL inside a RETURN_EXPR. */
2088 else if (TREE_CODE (stmt) == RETURN_EXPR
2089 && TREE_OPERAND (stmt, 0)
2090 && is_byref_result (TREE_OPERAND (stmt, 0)))
2091 *walk_subtrees = 0;
2093 /* Don't look inside trees that cannot embed references of interest. */
2094 else if (IS_TYPE_OR_DECL_P (stmt))
2095 *walk_subtrees = 0;
2097 pointer_set_insert (p_set, *stmt_p);
2099 return NULL;
2102 /* Perform lowering of Ada trees to GENERIC. In particular:
2104 o Turn a DECL_BY_REFERENCE RESULT_DECL into a real by-reference decl
2105 and adjust all the references to this decl accordingly. */
2107 static void
2108 gnat_genericize (tree fndecl)
2110 /* Prior to GCC 4, an explicit By_Reference result mechanism for a function
2111 was handled by simply setting TREE_ADDRESSABLE on the result type.
2112 Everything required to actually pass by invisible ref using the target
2113 mechanism (e.g. extra parameter) was handled at RTL expansion time.
2115 This doesn't work with GCC 4 any more for several reasons. First, the
2116 gimplification process might need the creation of temporaries of this
2117 type, and the gimplifier ICEs on such attempts. Second, the middle-end
2118 now relies on a different attribute for such cases (DECL_BY_REFERENCE on
2119 RESULT/PARM_DECLs), and expects the user invisible by-reference-ness to
2120 be explicitly accounted for by the front-end in the function body.
2122 We achieve the complete transformation in two steps:
2124 1/ create_subprog_decl performs early attribute tweaks: it clears
2125 TREE_ADDRESSABLE from the result type and sets DECL_BY_REFERENCE on
2126 the result decl. The former ensures that the bit isn't set in the GCC
2127 tree saved for the function, so prevents ICEs on temporary creation.
2128 The latter we use here to trigger the rest of the processing.
2130 2/ This function performs the type transformation on the result decl
2131 and adjusts all the references to this decl from the function body
2132 accordingly.
2134 Clearing TREE_ADDRESSABLE from the type differs from the C++ front-end
2135 strategy, which escapes the gimplifier temporary creation issues by
2136 creating it's own temporaries using TARGET_EXPR nodes. Our way relies
2137 on simple specific support code in aggregate_value_p to look at the
2138 target function result decl explicitly. */
2140 struct pointer_set_t *p_set;
2141 tree decl_result = DECL_RESULT (fndecl);
2143 if (!DECL_BY_REFERENCE (decl_result))
2144 return;
2146 /* Make the DECL_RESULT explicitly by-reference and adjust all the
2147 occurrences in the function body using the common tree-walking facility.
2148 We want to see every occurrence of the result decl to adjust the
2149 referencing tree, so need to use our own pointer set to control which
2150 trees should be visited again or not. */
2152 p_set = pointer_set_create ();
2154 TREE_TYPE (decl_result) = build_reference_type (TREE_TYPE (decl_result));
2155 TREE_ADDRESSABLE (decl_result) = 0;
2156 relayout_decl (decl_result);
2158 walk_tree (&DECL_SAVED_TREE (fndecl), gnat_genericize_r, p_set, NULL);
2160 pointer_set_destroy (p_set);
2163 /* Finish the definition of the current subprogram and compile it all the way
2164 to assembler language output. BODY is the tree corresponding to
2165 the subprogram. */
2167 void
2168 end_subprog_body (tree body)
2170 tree fndecl = current_function_decl;
2172 /* Mark the BLOCK for this level as being for this function and pop the
2173 level. Since the vars in it are the parameters, clear them. */
2174 BLOCK_VARS (current_binding_level->block) = 0;
2175 BLOCK_SUPERCONTEXT (current_binding_level->block) = fndecl;
2176 DECL_INITIAL (fndecl) = current_binding_level->block;
2177 gnat_poplevel ();
2179 /* Deal with inline. If declared inline or we should default to inline,
2180 set the flag in the decl. */
2181 DECL_INLINE (fndecl)
2182 = DECL_DECLARED_INLINE_P (fndecl) || flag_inline_trees == 2;
2184 /* We handle pending sizes via the elaboration of types, so we don't
2185 need to save them. */
2186 get_pending_sizes ();
2188 /* Mark the RESULT_DECL as being in this subprogram. */
2189 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
2191 DECL_SAVED_TREE (fndecl) = body;
2193 current_function_decl = DECL_CONTEXT (fndecl);
2194 set_cfun (NULL);
2196 /* We cannot track the location of errors past this point. */
2197 error_gnat_node = Empty;
2199 /* If we're only annotating types, don't actually compile this function. */
2200 if (type_annotate_only)
2201 return;
2203 /* Perform the required pre-gimplification transformations on the tree. */
2204 gnat_genericize (fndecl);
2206 /* We do different things for nested and non-nested functions.
2207 ??? This should be in cgraph. */
2208 if (!DECL_CONTEXT (fndecl))
2210 gnat_gimplify_function (fndecl);
2211 cgraph_finalize_function (fndecl, false);
2213 else
2214 /* Register this function with cgraph just far enough to get it
2215 added to our parent's nested function list. */
2216 (void) cgraph_node (fndecl);
2219 /* Convert FNDECL's code to GIMPLE and handle any nested functions. */
2221 static void
2222 gnat_gimplify_function (tree fndecl)
2224 struct cgraph_node *cgn;
2226 dump_function (TDI_original, fndecl);
2227 gimplify_function_tree (fndecl);
2228 dump_function (TDI_generic, fndecl);
2230 /* Convert all nested functions to GIMPLE now. We do things in this order
2231 so that items like VLA sizes are expanded properly in the context of the
2232 correct function. */
2233 cgn = cgraph_node (fndecl);
2234 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
2235 gnat_gimplify_function (cgn->decl);
2239 tree
2240 gnat_builtin_function (tree decl)
2242 gnat_pushdecl (decl, Empty);
2243 return decl;
2246 /* Return an integer type with the number of bits of precision given by
2247 PRECISION. UNSIGNEDP is nonzero if the type is unsigned; otherwise
2248 it is a signed type. */
2250 tree
2251 gnat_type_for_size (unsigned precision, int unsignedp)
2253 tree t;
2254 char type_name[20];
2256 if (precision <= 2 * MAX_BITS_PER_WORD
2257 && signed_and_unsigned_types[precision][unsignedp])
2258 return signed_and_unsigned_types[precision][unsignedp];
2260 if (unsignedp)
2261 t = make_unsigned_type (precision);
2262 else
2263 t = make_signed_type (precision);
2265 if (precision <= 2 * MAX_BITS_PER_WORD)
2266 signed_and_unsigned_types[precision][unsignedp] = t;
2268 if (!TYPE_NAME (t))
2270 sprintf (type_name, "%sSIGNED_%d", unsignedp ? "UN" : "", precision);
2271 TYPE_NAME (t) = get_identifier (type_name);
2274 return t;
2277 /* Likewise for floating-point types. */
2279 static tree
2280 float_type_for_precision (int precision, enum machine_mode mode)
2282 tree t;
2283 char type_name[20];
2285 if (float_types[(int) mode])
2286 return float_types[(int) mode];
2288 float_types[(int) mode] = t = make_node (REAL_TYPE);
2289 TYPE_PRECISION (t) = precision;
2290 layout_type (t);
2292 gcc_assert (TYPE_MODE (t) == mode);
2293 if (!TYPE_NAME (t))
2295 sprintf (type_name, "FLOAT_%d", precision);
2296 TYPE_NAME (t) = get_identifier (type_name);
2299 return t;
2302 /* Return a data type that has machine mode MODE. UNSIGNEDP selects
2303 an unsigned type; otherwise a signed type is returned. */
2305 tree
2306 gnat_type_for_mode (enum machine_mode mode, int unsignedp)
2308 if (mode == BLKmode)
2309 return NULL_TREE;
2310 else if (mode == VOIDmode)
2311 return void_type_node;
2312 else if (COMPLEX_MODE_P (mode))
2313 return NULL_TREE;
2314 else if (SCALAR_FLOAT_MODE_P (mode))
2315 return float_type_for_precision (GET_MODE_PRECISION (mode), mode);
2316 else if (SCALAR_INT_MODE_P (mode))
2317 return gnat_type_for_size (GET_MODE_BITSIZE (mode), unsignedp);
2318 else
2319 return NULL_TREE;
2322 /* Return the unsigned version of a TYPE_NODE, a scalar type. */
2324 tree
2325 gnat_unsigned_type (tree type_node)
2327 tree type = gnat_type_for_size (TYPE_PRECISION (type_node), 1);
2329 if (TREE_CODE (type_node) == INTEGER_TYPE && TYPE_MODULAR_P (type_node))
2331 type = copy_node (type);
2332 TREE_TYPE (type) = type_node;
2334 else if (TREE_TYPE (type_node)
2335 && TREE_CODE (TREE_TYPE (type_node)) == INTEGER_TYPE
2336 && TYPE_MODULAR_P (TREE_TYPE (type_node)))
2338 type = copy_node (type);
2339 TREE_TYPE (type) = TREE_TYPE (type_node);
2342 return type;
2345 /* Return the signed version of a TYPE_NODE, a scalar type. */
2347 tree
2348 gnat_signed_type (tree type_node)
2350 tree type = gnat_type_for_size (TYPE_PRECISION (type_node), 0);
2352 if (TREE_CODE (type_node) == INTEGER_TYPE && TYPE_MODULAR_P (type_node))
2354 type = copy_node (type);
2355 TREE_TYPE (type) = type_node;
2357 else if (TREE_TYPE (type_node)
2358 && TREE_CODE (TREE_TYPE (type_node)) == INTEGER_TYPE
2359 && TYPE_MODULAR_P (TREE_TYPE (type_node)))
2361 type = copy_node (type);
2362 TREE_TYPE (type) = TREE_TYPE (type_node);
2365 return type;
2368 /* Return 1 if the types T1 and T2 are compatible, i.e. if they can be
2369 transparently converted to each other. */
2372 gnat_types_compatible_p (tree t1, tree t2)
2374 enum tree_code code;
2376 /* This is the default criterion. */
2377 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
2378 return 1;
2380 /* We only check structural equivalence here. */
2381 if ((code = TREE_CODE (t1)) != TREE_CODE (t2))
2382 return 0;
2384 /* Array types are also compatible if they are constrained and have
2385 the same component type and the same domain. */
2386 if (code == ARRAY_TYPE
2387 && TREE_TYPE (t1) == TREE_TYPE (t2)
2388 && tree_int_cst_equal (TYPE_MIN_VALUE (TYPE_DOMAIN (t1)),
2389 TYPE_MIN_VALUE (TYPE_DOMAIN (t2)))
2390 && tree_int_cst_equal (TYPE_MAX_VALUE (TYPE_DOMAIN (t1)),
2391 TYPE_MAX_VALUE (TYPE_DOMAIN (t2))))
2392 return 1;
2394 /* Padding record types are also compatible if they pad the same
2395 type and have the same constant size. */
2396 if (code == RECORD_TYPE
2397 && TYPE_IS_PADDING_P (t1) && TYPE_IS_PADDING_P (t2)
2398 && TREE_TYPE (TYPE_FIELDS (t1)) == TREE_TYPE (TYPE_FIELDS (t2))
2399 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2400 return 1;
2402 return 0;
2405 /* EXP is an expression for the size of an object. If this size contains
2406 discriminant references, replace them with the maximum (if MAX_P) or
2407 minimum (if !MAX_P) possible value of the discriminant. */
2409 tree
2410 max_size (tree exp, bool max_p)
2412 enum tree_code code = TREE_CODE (exp);
2413 tree type = TREE_TYPE (exp);
2415 switch (TREE_CODE_CLASS (code))
2417 case tcc_declaration:
2418 case tcc_constant:
2419 return exp;
2421 case tcc_vl_exp:
2422 if (code == CALL_EXPR)
2424 tree *argarray;
2425 int i, n = call_expr_nargs (exp);
2426 gcc_assert (n > 0);
2428 argarray = (tree *) alloca (n * sizeof (tree));
2429 for (i = 0; i < n; i++)
2430 argarray[i] = max_size (CALL_EXPR_ARG (exp, i), max_p);
2431 return build_call_array (type, CALL_EXPR_FN (exp), n, argarray);
2433 break;
2435 case tcc_reference:
2436 /* If this contains a PLACEHOLDER_EXPR, it is the thing we want to
2437 modify. Otherwise, we treat it like a variable. */
2438 if (!CONTAINS_PLACEHOLDER_P (exp))
2439 return exp;
2441 type = TREE_TYPE (TREE_OPERAND (exp, 1));
2442 return
2443 max_size (max_p ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type), true);
2445 case tcc_comparison:
2446 return max_p ? size_one_node : size_zero_node;
2448 case tcc_unary:
2449 case tcc_binary:
2450 case tcc_expression:
2451 switch (TREE_CODE_LENGTH (code))
2453 case 1:
2454 if (code == NON_LVALUE_EXPR)
2455 return max_size (TREE_OPERAND (exp, 0), max_p);
2456 else
2457 return
2458 fold_build1 (code, type,
2459 max_size (TREE_OPERAND (exp, 0),
2460 code == NEGATE_EXPR ? !max_p : max_p));
2462 case 2:
2463 if (code == COMPOUND_EXPR)
2464 return max_size (TREE_OPERAND (exp, 1), max_p);
2466 /* Calculate "(A ? B : C) - D" as "A ? B - D : C - D" which
2467 may provide a tighter bound on max_size. */
2468 if (code == MINUS_EXPR
2469 && TREE_CODE (TREE_OPERAND (exp, 0)) == COND_EXPR)
2471 tree lhs = fold_build2 (MINUS_EXPR, type,
2472 TREE_OPERAND (TREE_OPERAND (exp, 0), 1),
2473 TREE_OPERAND (exp, 1));
2474 tree rhs = fold_build2 (MINUS_EXPR, type,
2475 TREE_OPERAND (TREE_OPERAND (exp, 0), 2),
2476 TREE_OPERAND (exp, 1));
2477 return fold_build2 (max_p ? MAX_EXPR : MIN_EXPR, type,
2478 max_size (lhs, max_p),
2479 max_size (rhs, max_p));
2483 tree lhs = max_size (TREE_OPERAND (exp, 0), max_p);
2484 tree rhs = max_size (TREE_OPERAND (exp, 1),
2485 code == MINUS_EXPR ? !max_p : max_p);
2487 /* Special-case wanting the maximum value of a MIN_EXPR.
2488 In that case, if one side overflows, return the other.
2489 sizetype is signed, but we know sizes are non-negative.
2490 Likewise, handle a MINUS_EXPR or PLUS_EXPR with the LHS
2491 overflowing or the maximum possible value and the RHS
2492 a variable. */
2493 if (max_p
2494 && code == MIN_EXPR
2495 && TREE_CODE (rhs) == INTEGER_CST
2496 && TREE_OVERFLOW (rhs))
2497 return lhs;
2498 else if (max_p
2499 && code == MIN_EXPR
2500 && TREE_CODE (lhs) == INTEGER_CST
2501 && TREE_OVERFLOW (lhs))
2502 return rhs;
2503 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
2504 && ((TREE_CODE (lhs) == INTEGER_CST
2505 && TREE_OVERFLOW (lhs))
2506 || operand_equal_p (lhs, TYPE_MAX_VALUE (type), 0))
2507 && !TREE_CONSTANT (rhs))
2508 return lhs;
2509 else
2510 return fold_build2 (code, type, lhs, rhs);
2513 case 3:
2514 if (code == SAVE_EXPR)
2515 return exp;
2516 else if (code == COND_EXPR)
2517 return fold_build2 (max_p ? MAX_EXPR : MIN_EXPR, type,
2518 max_size (TREE_OPERAND (exp, 1), max_p),
2519 max_size (TREE_OPERAND (exp, 2), max_p));
2522 /* Other tree classes cannot happen. */
2523 default:
2524 break;
2527 gcc_unreachable ();
2530 /* Build a template of type TEMPLATE_TYPE from the array bounds of ARRAY_TYPE.
2531 EXPR is an expression that we can use to locate any PLACEHOLDER_EXPRs.
2532 Return a constructor for the template. */
2534 tree
2535 build_template (tree template_type, tree array_type, tree expr)
2537 tree template_elts = NULL_TREE;
2538 tree bound_list = NULL_TREE;
2539 tree field;
2541 while (TREE_CODE (array_type) == RECORD_TYPE
2542 && (TYPE_IS_PADDING_P (array_type)
2543 || TYPE_JUSTIFIED_MODULAR_P (array_type)))
2544 array_type = TREE_TYPE (TYPE_FIELDS (array_type));
2546 if (TREE_CODE (array_type) == ARRAY_TYPE
2547 || (TREE_CODE (array_type) == INTEGER_TYPE
2548 && TYPE_HAS_ACTUAL_BOUNDS_P (array_type)))
2549 bound_list = TYPE_ACTUAL_BOUNDS (array_type);
2551 /* First make the list for a CONSTRUCTOR for the template. Go down the
2552 field list of the template instead of the type chain because this
2553 array might be an Ada array of arrays and we can't tell where the
2554 nested arrays stop being the underlying object. */
2556 for (field = TYPE_FIELDS (template_type); field;
2557 (bound_list
2558 ? (bound_list = TREE_CHAIN (bound_list))
2559 : (array_type = TREE_TYPE (array_type))),
2560 field = TREE_CHAIN (TREE_CHAIN (field)))
2562 tree bounds, min, max;
2564 /* If we have a bound list, get the bounds from there. Likewise
2565 for an ARRAY_TYPE. Otherwise, if expr is a PARM_DECL with
2566 DECL_BY_COMPONENT_PTR_P, use the bounds of the field in the template.
2567 This will give us a maximum range. */
2568 if (bound_list)
2569 bounds = TREE_VALUE (bound_list);
2570 else if (TREE_CODE (array_type) == ARRAY_TYPE)
2571 bounds = TYPE_INDEX_TYPE (TYPE_DOMAIN (array_type));
2572 else if (expr && TREE_CODE (expr) == PARM_DECL
2573 && DECL_BY_COMPONENT_PTR_P (expr))
2574 bounds = TREE_TYPE (field);
2575 else
2576 gcc_unreachable ();
2578 min = convert (TREE_TYPE (field), TYPE_MIN_VALUE (bounds));
2579 max = convert (TREE_TYPE (TREE_CHAIN (field)), TYPE_MAX_VALUE (bounds));
2581 /* If either MIN or MAX involve a PLACEHOLDER_EXPR, we must
2582 substitute it from OBJECT. */
2583 min = SUBSTITUTE_PLACEHOLDER_IN_EXPR (min, expr);
2584 max = SUBSTITUTE_PLACEHOLDER_IN_EXPR (max, expr);
2586 template_elts = tree_cons (TREE_CHAIN (field), max,
2587 tree_cons (field, min, template_elts));
2590 return gnat_build_constructor (template_type, nreverse (template_elts));
2593 /* Build a VMS descriptor from a Mechanism_Type, which must specify
2594 a descriptor type, and the GCC type of an object. Each FIELD_DECL
2595 in the type contains in its DECL_INITIAL the expression to use when
2596 a constructor is made for the type. GNAT_ENTITY is an entity used
2597 to print out an error message if the mechanism cannot be applied to
2598 an object of that type and also for the name. */
2600 tree
2601 build_vms_descriptor (tree type, Mechanism_Type mech, Entity_Id gnat_entity)
2603 tree record_type = make_node (RECORD_TYPE);
2604 tree pointer32_type;
2605 tree field_list = 0;
2606 int class;
2607 int dtype = 0;
2608 tree inner_type;
2609 int ndim;
2610 int i;
2611 tree *idx_arr;
2612 tree tem;
2614 /* If TYPE is an unconstrained array, use the underlying array type. */
2615 if (TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
2616 type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (type))));
2618 /* If this is an array, compute the number of dimensions in the array,
2619 get the index types, and point to the inner type. */
2620 if (TREE_CODE (type) != ARRAY_TYPE)
2621 ndim = 0;
2622 else
2623 for (ndim = 1, inner_type = type;
2624 TREE_CODE (TREE_TYPE (inner_type)) == ARRAY_TYPE
2625 && TYPE_MULTI_ARRAY_P (TREE_TYPE (inner_type));
2626 ndim++, inner_type = TREE_TYPE (inner_type))
2629 idx_arr = (tree *) alloca (ndim * sizeof (tree));
2631 if (mech != By_Descriptor_NCA
2632 && TREE_CODE (type) == ARRAY_TYPE && TYPE_CONVENTION_FORTRAN_P (type))
2633 for (i = ndim - 1, inner_type = type;
2634 i >= 0;
2635 i--, inner_type = TREE_TYPE (inner_type))
2636 idx_arr[i] = TYPE_DOMAIN (inner_type);
2637 else
2638 for (i = 0, inner_type = type;
2639 i < ndim;
2640 i++, inner_type = TREE_TYPE (inner_type))
2641 idx_arr[i] = TYPE_DOMAIN (inner_type);
2643 /* Now get the DTYPE value. */
2644 switch (TREE_CODE (type))
2646 case INTEGER_TYPE:
2647 case ENUMERAL_TYPE:
2648 if (TYPE_VAX_FLOATING_POINT_P (type))
2649 switch (tree_low_cst (TYPE_DIGITS_VALUE (type), 1))
2651 case 6:
2652 dtype = 10;
2653 break;
2654 case 9:
2655 dtype = 11;
2656 break;
2657 case 15:
2658 dtype = 27;
2659 break;
2661 else
2662 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
2664 case 8:
2665 dtype = TYPE_UNSIGNED (type) ? 2 : 6;
2666 break;
2667 case 16:
2668 dtype = TYPE_UNSIGNED (type) ? 3 : 7;
2669 break;
2670 case 32:
2671 dtype = TYPE_UNSIGNED (type) ? 4 : 8;
2672 break;
2673 case 64:
2674 dtype = TYPE_UNSIGNED (type) ? 5 : 9;
2675 break;
2676 case 128:
2677 dtype = TYPE_UNSIGNED (type) ? 25 : 26;
2678 break;
2680 break;
2682 case REAL_TYPE:
2683 dtype = GET_MODE_BITSIZE (TYPE_MODE (type)) == 32 ? 52 : 53;
2684 break;
2686 case COMPLEX_TYPE:
2687 if (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
2688 && TYPE_VAX_FLOATING_POINT_P (type))
2689 switch (tree_low_cst (TYPE_DIGITS_VALUE (type), 1))
2691 case 6:
2692 dtype = 12;
2693 break;
2694 case 9:
2695 dtype = 13;
2696 break;
2697 case 15:
2698 dtype = 29;
2700 else
2701 dtype = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) == 32 ? 54: 55;
2702 break;
2704 case ARRAY_TYPE:
2705 dtype = 14;
2706 break;
2708 default:
2709 break;
2712 /* Get the CLASS value. */
2713 switch (mech)
2715 case By_Descriptor_A:
2716 class = 4;
2717 break;
2718 case By_Descriptor_NCA:
2719 class = 10;
2720 break;
2721 case By_Descriptor_SB:
2722 class = 15;
2723 break;
2724 case By_Descriptor:
2725 case By_Descriptor_S:
2726 default:
2727 class = 1;
2728 break;
2731 /* Make the type for a descriptor for VMS. The first four fields
2732 are the same for all types. */
2734 field_list
2735 = chainon (field_list,
2736 make_descriptor_field
2737 ("LENGTH", gnat_type_for_size (16, 1), record_type,
2738 size_in_bytes (mech == By_Descriptor_A ? inner_type : type)));
2740 field_list = chainon (field_list,
2741 make_descriptor_field ("DTYPE",
2742 gnat_type_for_size (8, 1),
2743 record_type, size_int (dtype)));
2744 field_list = chainon (field_list,
2745 make_descriptor_field ("CLASS",
2746 gnat_type_for_size (8, 1),
2747 record_type, size_int (class)));
2749 /* Of course this will crash at run-time if the address space is not
2750 within the low 32 bits, but there is nothing else we can do. */
2751 pointer32_type = build_pointer_type_for_mode (type, SImode, false);
2753 field_list
2754 = chainon (field_list,
2755 make_descriptor_field
2756 ("POINTER", pointer32_type, record_type,
2757 build_unary_op (ADDR_EXPR,
2758 pointer32_type,
2759 build0 (PLACEHOLDER_EXPR, type))));
2761 switch (mech)
2763 case By_Descriptor:
2764 case By_Descriptor_S:
2765 break;
2767 case By_Descriptor_SB:
2768 field_list
2769 = chainon (field_list,
2770 make_descriptor_field
2771 ("SB_L1", gnat_type_for_size (32, 1), record_type,
2772 TREE_CODE (type) == ARRAY_TYPE
2773 ? TYPE_MIN_VALUE (TYPE_DOMAIN (type)) : size_zero_node));
2774 field_list
2775 = chainon (field_list,
2776 make_descriptor_field
2777 ("SB_U1", gnat_type_for_size (32, 1), record_type,
2778 TREE_CODE (type) == ARRAY_TYPE
2779 ? TYPE_MAX_VALUE (TYPE_DOMAIN (type)) : size_zero_node));
2780 break;
2782 case By_Descriptor_A:
2783 case By_Descriptor_NCA:
2784 field_list = chainon (field_list,
2785 make_descriptor_field ("SCALE",
2786 gnat_type_for_size (8, 1),
2787 record_type,
2788 size_zero_node));
2790 field_list = chainon (field_list,
2791 make_descriptor_field ("DIGITS",
2792 gnat_type_for_size (8, 1),
2793 record_type,
2794 size_zero_node));
2796 field_list
2797 = chainon (field_list,
2798 make_descriptor_field
2799 ("AFLAGS", gnat_type_for_size (8, 1), record_type,
2800 size_int (mech == By_Descriptor_NCA
2802 /* Set FL_COLUMN, FL_COEFF, and FL_BOUNDS. */
2803 : (TREE_CODE (type) == ARRAY_TYPE
2804 && TYPE_CONVENTION_FORTRAN_P (type)
2805 ? 224 : 192))));
2807 field_list = chainon (field_list,
2808 make_descriptor_field ("DIMCT",
2809 gnat_type_for_size (8, 1),
2810 record_type,
2811 size_int (ndim)));
2813 field_list = chainon (field_list,
2814 make_descriptor_field ("ARSIZE",
2815 gnat_type_for_size (32, 1),
2816 record_type,
2817 size_in_bytes (type)));
2819 /* Now build a pointer to the 0,0,0... element. */
2820 tem = build0 (PLACEHOLDER_EXPR, type);
2821 for (i = 0, inner_type = type; i < ndim;
2822 i++, inner_type = TREE_TYPE (inner_type))
2823 tem = build4 (ARRAY_REF, TREE_TYPE (inner_type), tem,
2824 convert (TYPE_DOMAIN (inner_type), size_zero_node),
2825 NULL_TREE, NULL_TREE);
2827 field_list
2828 = chainon (field_list,
2829 make_descriptor_field
2830 ("A0",
2831 build_pointer_type_for_mode (inner_type, SImode, false),
2832 record_type,
2833 build1 (ADDR_EXPR,
2834 build_pointer_type_for_mode (inner_type, SImode,
2835 false),
2836 tem)));
2838 /* Next come the addressing coefficients. */
2839 tem = size_one_node;
2840 for (i = 0; i < ndim; i++)
2842 char fname[3];
2843 tree idx_length
2844 = size_binop (MULT_EXPR, tem,
2845 size_binop (PLUS_EXPR,
2846 size_binop (MINUS_EXPR,
2847 TYPE_MAX_VALUE (idx_arr[i]),
2848 TYPE_MIN_VALUE (idx_arr[i])),
2849 size_int (1)));
2851 fname[0] = (mech == By_Descriptor_NCA ? 'S' : 'M');
2852 fname[1] = '0' + i, fname[2] = 0;
2853 field_list
2854 = chainon (field_list,
2855 make_descriptor_field (fname,
2856 gnat_type_for_size (32, 1),
2857 record_type, idx_length));
2859 if (mech == By_Descriptor_NCA)
2860 tem = idx_length;
2863 /* Finally here are the bounds. */
2864 for (i = 0; i < ndim; i++)
2866 char fname[3];
2868 fname[0] = 'L', fname[1] = '0' + i, fname[2] = 0;
2869 field_list
2870 = chainon (field_list,
2871 make_descriptor_field
2872 (fname, gnat_type_for_size (32, 1), record_type,
2873 TYPE_MIN_VALUE (idx_arr[i])));
2875 fname[0] = 'U';
2876 field_list
2877 = chainon (field_list,
2878 make_descriptor_field
2879 (fname, gnat_type_for_size (32, 1), record_type,
2880 TYPE_MAX_VALUE (idx_arr[i])));
2882 break;
2884 default:
2885 post_error ("unsupported descriptor type for &", gnat_entity);
2888 finish_record_type (record_type, field_list, 0, true);
2889 create_type_decl (create_concat_name (gnat_entity, "DESC"), record_type,
2890 NULL, true, false, gnat_entity);
2892 return record_type;
2895 /* Utility routine for above code to make a field. */
2897 static tree
2898 make_descriptor_field (const char *name, tree type,
2899 tree rec_type, tree initial)
2901 tree field
2902 = create_field_decl (get_identifier (name), type, rec_type, 0, 0, 0, 0);
2904 DECL_INITIAL (field) = initial;
2905 return field;
2908 /* Convert GNU_EXPR, a pointer to a VMS descriptor, to GNU_TYPE, a regular
2909 pointer or fat pointer type. GNAT_SUBPROG is the subprogram to which
2910 the VMS descriptor is passed. */
2912 static tree
2913 convert_vms_descriptor (tree gnu_type, tree gnu_expr, Entity_Id gnat_subprog)
2915 tree desc_type = TREE_TYPE (TREE_TYPE (gnu_expr));
2916 tree desc = build1 (INDIRECT_REF, desc_type, gnu_expr);
2917 /* The CLASS field is the 3rd field in the descriptor. */
2918 tree class = TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (desc_type)));
2919 /* The POINTER field is the 4th field in the descriptor. */
2920 tree pointer = TREE_CHAIN (class);
2922 /* Retrieve the value of the POINTER field. */
2923 gnu_expr
2924 = build3 (COMPONENT_REF, TREE_TYPE (pointer), desc, pointer, NULL_TREE);
2926 if (POINTER_TYPE_P (gnu_type))
2927 return convert (gnu_type, gnu_expr);
2929 else if (TYPE_FAT_POINTER_P (gnu_type))
2931 tree p_array_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
2932 tree p_bounds_type = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)));
2933 tree template_type = TREE_TYPE (p_bounds_type);
2934 tree min_field = TYPE_FIELDS (template_type);
2935 tree max_field = TREE_CHAIN (TYPE_FIELDS (template_type));
2936 tree template, template_addr, aflags, dimct, t, u;
2937 /* See the head comment of build_vms_descriptor. */
2938 int iclass = TREE_INT_CST_LOW (DECL_INITIAL (class));
2940 /* Convert POINTER to the type of the P_ARRAY field. */
2941 gnu_expr = convert (p_array_type, gnu_expr);
2943 switch (iclass)
2945 case 1: /* Class S */
2946 case 15: /* Class SB */
2947 /* Build {1, LENGTH} template; LENGTH is the 1st field. */
2948 t = TYPE_FIELDS (desc_type);
2949 t = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
2950 t = tree_cons (min_field,
2951 convert (TREE_TYPE (min_field), integer_one_node),
2952 tree_cons (max_field,
2953 convert (TREE_TYPE (max_field), t),
2954 NULL_TREE));
2955 template = gnat_build_constructor (template_type, t);
2956 template_addr = build_unary_op (ADDR_EXPR, NULL_TREE, template);
2958 /* For class S, we are done. */
2959 if (iclass == 1)
2960 break;
2962 /* Test that we really have a SB descriptor, like DEC Ada. */
2963 t = build3 (COMPONENT_REF, TREE_TYPE (class), desc, class, NULL);
2964 u = convert (TREE_TYPE (class), DECL_INITIAL (class));
2965 u = build_binary_op (EQ_EXPR, integer_type_node, t, u);
2966 /* If so, there is already a template in the descriptor and
2967 it is located right after the POINTER field. */
2968 t = TREE_CHAIN (pointer);
2969 template = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
2970 /* Otherwise use the {1, LENGTH} template we build above. */
2971 template_addr = build3 (COND_EXPR, p_bounds_type, u,
2972 build_unary_op (ADDR_EXPR, p_bounds_type,
2973 template),
2974 template_addr);
2975 break;
2977 case 4: /* Class A */
2978 /* The AFLAGS field is the 7th field in the descriptor. */
2979 t = TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (pointer)));
2980 aflags = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
2981 /* The DIMCT field is the 8th field in the descriptor. */
2982 t = TREE_CHAIN (t);
2983 dimct = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
2984 /* Raise CONSTRAINT_ERROR if either more than 1 dimension
2985 or FL_COEFF or FL_BOUNDS not set. */
2986 u = build_int_cst (TREE_TYPE (aflags), 192);
2987 u = build_binary_op (TRUTH_OR_EXPR, integer_type_node,
2988 build_binary_op (NE_EXPR, integer_type_node,
2989 dimct,
2990 convert (TREE_TYPE (dimct),
2991 size_one_node)),
2992 build_binary_op (NE_EXPR, integer_type_node,
2993 build2 (BIT_AND_EXPR,
2994 TREE_TYPE (aflags),
2995 aflags, u),
2996 u));
2997 add_stmt (build3 (COND_EXPR, void_type_node, u,
2998 build_call_raise (CE_Length_Check_Failed, Empty,
2999 N_Raise_Constraint_Error),
3000 NULL_TREE));
3001 /* There is already a template in the descriptor and it is
3002 located at the start of block 3 (12th field). */
3003 t = TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (t))));
3004 template = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
3005 template_addr = build_unary_op (ADDR_EXPR, p_bounds_type, template);
3006 break;
3008 case 10: /* Class NCA */
3009 default:
3010 post_error ("unsupported descriptor type for &", gnat_subprog);
3011 template_addr = integer_zero_node;
3012 break;
3015 /* Build the fat pointer in the form of a constructor. */
3016 t = tree_cons (TYPE_FIELDS (gnu_type), gnu_expr,
3017 tree_cons (TREE_CHAIN (TYPE_FIELDS (gnu_type)),
3018 template_addr, NULL_TREE));
3019 return gnat_build_constructor (gnu_type, t);
3022 else
3023 gcc_unreachable ();
3026 /* Build a stub for the subprogram specified by the GCC tree GNU_SUBPROG
3027 and the GNAT node GNAT_SUBPROG. */
3029 void
3030 build_function_stub (tree gnu_subprog, Entity_Id gnat_subprog)
3032 tree gnu_subprog_type, gnu_subprog_addr, gnu_subprog_call;
3033 tree gnu_stub_param, gnu_param_list, gnu_arg_types, gnu_param;
3034 tree gnu_stub_decl = DECL_FUNCTION_STUB (gnu_subprog);
3035 tree gnu_body;
3037 gnu_subprog_type = TREE_TYPE (gnu_subprog);
3038 gnu_param_list = NULL_TREE;
3040 begin_subprog_body (gnu_stub_decl);
3041 gnat_pushlevel ();
3043 start_stmt_group ();
3045 /* Loop over the parameters of the stub and translate any of them
3046 passed by descriptor into a by reference one. */
3047 for (gnu_stub_param = DECL_ARGUMENTS (gnu_stub_decl),
3048 gnu_arg_types = TYPE_ARG_TYPES (gnu_subprog_type);
3049 gnu_stub_param;
3050 gnu_stub_param = TREE_CHAIN (gnu_stub_param),
3051 gnu_arg_types = TREE_CHAIN (gnu_arg_types))
3053 if (DECL_BY_DESCRIPTOR_P (gnu_stub_param))
3054 gnu_param = convert_vms_descriptor (TREE_VALUE (gnu_arg_types),
3055 gnu_stub_param, gnat_subprog);
3056 else
3057 gnu_param = gnu_stub_param;
3059 gnu_param_list = tree_cons (NULL_TREE, gnu_param, gnu_param_list);
3062 gnu_body = end_stmt_group ();
3064 /* Invoke the internal subprogram. */
3065 gnu_subprog_addr = build1 (ADDR_EXPR, build_pointer_type (gnu_subprog_type),
3066 gnu_subprog);
3067 gnu_subprog_call = build_call_list (TREE_TYPE (gnu_subprog_type),
3068 gnu_subprog_addr,
3069 nreverse (gnu_param_list));
3071 /* Propagate the return value, if any. */
3072 if (VOID_TYPE_P (TREE_TYPE (gnu_subprog_type)))
3073 append_to_statement_list (gnu_subprog_call, &gnu_body);
3074 else
3075 append_to_statement_list (build_return_expr (DECL_RESULT (gnu_stub_decl),
3076 gnu_subprog_call),
3077 &gnu_body);
3079 gnat_poplevel ();
3081 allocate_struct_function (gnu_stub_decl, false);
3082 end_subprog_body (gnu_body);
3085 /* Build a type to be used to represent an aliased object whose nominal
3086 type is an unconstrained array. This consists of a RECORD_TYPE containing
3087 a field of TEMPLATE_TYPE and a field of OBJECT_TYPE, which is an
3088 ARRAY_TYPE. If ARRAY_TYPE is that of the unconstrained array, this
3089 is used to represent an arbitrary unconstrained object. Use NAME
3090 as the name of the record. */
3092 tree
3093 build_unc_object_type (tree template_type, tree object_type, tree name)
3095 tree type = make_node (RECORD_TYPE);
3096 tree template_field = create_field_decl (get_identifier ("BOUNDS"),
3097 template_type, type, 0, 0, 0, 1);
3098 tree array_field = create_field_decl (get_identifier ("ARRAY"), object_type,
3099 type, 0, 0, 0, 1);
3101 TYPE_NAME (type) = name;
3102 TYPE_CONTAINS_TEMPLATE_P (type) = 1;
3103 finish_record_type (type,
3104 chainon (chainon (NULL_TREE, template_field),
3105 array_field),
3106 0, false);
3108 return type;
3111 /* Same, taking a thin or fat pointer type instead of a template type. */
3113 tree
3114 build_unc_object_type_from_ptr (tree thin_fat_ptr_type, tree object_type,
3115 tree name)
3117 tree template_type;
3119 gcc_assert (TYPE_FAT_OR_THIN_POINTER_P (thin_fat_ptr_type));
3121 template_type
3122 = (TYPE_FAT_POINTER_P (thin_fat_ptr_type)
3123 ? TREE_TYPE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (thin_fat_ptr_type))))
3124 : TREE_TYPE (TYPE_FIELDS (TREE_TYPE (thin_fat_ptr_type))));
3125 return build_unc_object_type (template_type, object_type, name);
3128 /* Shift the component offsets within an unconstrained object TYPE to make it
3129 suitable for use as a designated type for thin pointers. */
3131 void
3132 shift_unc_components_for_thin_pointers (tree type)
3134 /* Thin pointer values designate the ARRAY data of an unconstrained object,
3135 allocated past the BOUNDS template. The designated type is adjusted to
3136 have ARRAY at position zero and the template at a negative offset, so
3137 that COMPONENT_REFs on (*thin_ptr) designate the proper location. */
3139 tree bounds_field = TYPE_FIELDS (type);
3140 tree array_field = TREE_CHAIN (TYPE_FIELDS (type));
3142 DECL_FIELD_OFFSET (bounds_field)
3143 = size_binop (MINUS_EXPR, size_zero_node, byte_position (array_field));
3145 DECL_FIELD_OFFSET (array_field) = size_zero_node;
3146 DECL_FIELD_BIT_OFFSET (array_field) = bitsize_zero_node;
3149 /* Update anything previously pointing to OLD_TYPE to point to NEW_TYPE. In
3150 the normal case this is just two adjustments, but we have more to do
3151 if NEW is an UNCONSTRAINED_ARRAY_TYPE. */
3153 void
3154 update_pointer_to (tree old_type, tree new_type)
3156 tree ptr = TYPE_POINTER_TO (old_type);
3157 tree ref = TYPE_REFERENCE_TO (old_type);
3158 tree ptr1, ref1;
3159 tree type;
3161 /* If this is the main variant, process all the other variants first. */
3162 if (TYPE_MAIN_VARIANT (old_type) == old_type)
3163 for (type = TYPE_NEXT_VARIANT (old_type); type;
3164 type = TYPE_NEXT_VARIANT (type))
3165 update_pointer_to (type, new_type);
3167 /* If no pointer or reference, we are done. */
3168 if (!ptr && !ref)
3169 return;
3171 /* Merge the old type qualifiers in the new type.
3173 Each old variant has qualifiers for specific reasons, and the new
3174 designated type as well. Each set of qualifiers represents useful
3175 information grabbed at some point, and merging the two simply unifies
3176 these inputs into the final type description.
3178 Consider for instance a volatile type frozen after an access to constant
3179 type designating it. After the designated type freeze, we get here with a
3180 volatile new_type and a dummy old_type with a readonly variant, created
3181 when the access type was processed. We shall make a volatile and readonly
3182 designated type, because that's what it really is.
3184 We might also get here for a non-dummy old_type variant with different
3185 qualifiers than the new_type ones, for instance in some cases of pointers
3186 to private record type elaboration (see the comments around the call to
3187 this routine from gnat_to_gnu_entity/E_Access_Type). We have to merge the
3188 qualifiers in those cases too, to avoid accidentally discarding the
3189 initial set, and will often end up with old_type == new_type then. */
3190 new_type = build_qualified_type (new_type,
3191 TYPE_QUALS (old_type)
3192 | TYPE_QUALS (new_type));
3194 /* If the new type and the old one are identical, there is nothing to
3195 update. */
3196 if (old_type == new_type)
3197 return;
3199 /* Otherwise, first handle the simple case. */
3200 if (TREE_CODE (new_type) != UNCONSTRAINED_ARRAY_TYPE)
3202 TYPE_POINTER_TO (new_type) = ptr;
3203 TYPE_REFERENCE_TO (new_type) = ref;
3205 for (; ptr; ptr = TYPE_NEXT_PTR_TO (ptr))
3206 for (ptr1 = TYPE_MAIN_VARIANT (ptr); ptr1;
3207 ptr1 = TYPE_NEXT_VARIANT (ptr1))
3208 TREE_TYPE (ptr1) = new_type;
3210 for (; ref; ref = TYPE_NEXT_REF_TO (ref))
3211 for (ref1 = TYPE_MAIN_VARIANT (ref); ref1;
3212 ref1 = TYPE_NEXT_VARIANT (ref1))
3213 TREE_TYPE (ref1) = new_type;
3216 /* Now deal with the unconstrained array case. In this case the "pointer"
3217 is actually a RECORD_TYPE where both fields are pointers to dummy nodes.
3218 Turn them into pointers to the correct types using update_pointer_to. */
3219 else if (TREE_CODE (ptr) != RECORD_TYPE || !TYPE_IS_FAT_POINTER_P (ptr))
3220 gcc_unreachable ();
3222 else
3224 tree new_obj_rec = TYPE_OBJECT_RECORD_TYPE (new_type);
3225 tree array_field = TYPE_FIELDS (ptr);
3226 tree bounds_field = TREE_CHAIN (TYPE_FIELDS (ptr));
3227 tree new_ptr = TYPE_POINTER_TO (new_type);
3228 tree new_ref;
3229 tree var;
3231 /* Make pointers to the dummy template point to the real template. */
3232 update_pointer_to
3233 (TREE_TYPE (TREE_TYPE (bounds_field)),
3234 TREE_TYPE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (new_ptr)))));
3236 /* The references to the template bounds present in the array type
3237 are made through a PLACEHOLDER_EXPR of type new_ptr. Since we
3238 are updating ptr to make it a full replacement for new_ptr as
3239 pointer to new_type, we must rework the PLACEHOLDER_EXPR so as
3240 to make it of type ptr. */
3241 new_ref = build3 (COMPONENT_REF, TREE_TYPE (bounds_field),
3242 build0 (PLACEHOLDER_EXPR, ptr),
3243 bounds_field, NULL_TREE);
3245 /* Create the new array for the new PLACEHOLDER_EXPR and make
3246 pointers to the dummy array point to it.
3248 ??? This is now the only use of substitute_in_type,
3249 which is a very "heavy" routine to do this, so it
3250 should be replaced at some point. */
3251 update_pointer_to
3252 (TREE_TYPE (TREE_TYPE (array_field)),
3253 substitute_in_type (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (new_ptr))),
3254 TREE_CHAIN (TYPE_FIELDS (new_ptr)), new_ref));
3256 /* Make ptr the pointer to new_type. */
3257 TYPE_POINTER_TO (new_type) = TYPE_REFERENCE_TO (new_type)
3258 = TREE_TYPE (new_type) = ptr;
3260 for (var = TYPE_MAIN_VARIANT (ptr); var; var = TYPE_NEXT_VARIANT (var))
3261 SET_TYPE_UNCONSTRAINED_ARRAY (var, new_type);
3263 /* Now handle updating the allocation record, what the thin pointer
3264 points to. Update all pointers from the old record into the new
3265 one, update the type of the array field, and recompute the size. */
3266 update_pointer_to (TYPE_OBJECT_RECORD_TYPE (old_type), new_obj_rec);
3268 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (new_obj_rec)))
3269 = TREE_TYPE (TREE_TYPE (array_field));
3271 /* The size recomputation needs to account for alignment constraints, so
3272 we let layout_type work it out. This will reset the field offsets to
3273 what they would be in a regular record, so we shift them back to what
3274 we want them to be for a thin pointer designated type afterwards. */
3275 DECL_SIZE (TYPE_FIELDS (new_obj_rec)) = 0;
3276 DECL_SIZE (TREE_CHAIN (TYPE_FIELDS (new_obj_rec))) = 0;
3277 TYPE_SIZE (new_obj_rec) = 0;
3278 layout_type (new_obj_rec);
3280 shift_unc_components_for_thin_pointers (new_obj_rec);
3282 /* We are done, at last. */
3283 rest_of_record_type_compilation (ptr);
3287 /* Convert a pointer to a constrained array into a pointer to a fat
3288 pointer. This involves making or finding a template. */
3290 static tree
3291 convert_to_fat_pointer (tree type, tree expr)
3293 tree template_type = TREE_TYPE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (type))));
3294 tree template, template_addr;
3295 tree etype = TREE_TYPE (expr);
3297 /* If EXPR is a constant of zero, we make a fat pointer that has a null
3298 pointer to the template and array. */
3299 if (integer_zerop (expr))
3300 return
3301 gnat_build_constructor
3302 (type,
3303 tree_cons (TYPE_FIELDS (type),
3304 convert (TREE_TYPE (TYPE_FIELDS (type)), expr),
3305 tree_cons (TREE_CHAIN (TYPE_FIELDS (type)),
3306 convert (build_pointer_type (template_type),
3307 expr),
3308 NULL_TREE)));
3310 /* If EXPR is a thin pointer, make the template and data from the record. */
3312 else if (TYPE_THIN_POINTER_P (etype))
3314 tree fields = TYPE_FIELDS (TREE_TYPE (etype));
3316 expr = save_expr (expr);
3317 if (TREE_CODE (expr) == ADDR_EXPR)
3318 expr = TREE_OPERAND (expr, 0);
3319 else
3320 expr = build1 (INDIRECT_REF, TREE_TYPE (etype), expr);
3322 template = build_component_ref (expr, NULL_TREE, fields, false);
3323 expr = build_unary_op (ADDR_EXPR, NULL_TREE,
3324 build_component_ref (expr, NULL_TREE,
3325 TREE_CHAIN (fields), false));
3327 else
3328 /* Otherwise, build the constructor for the template. */
3329 template = build_template (template_type, TREE_TYPE (etype), expr);
3331 template_addr = build_unary_op (ADDR_EXPR, NULL_TREE, template);
3333 /* The result is a CONSTRUCTOR for the fat pointer.
3335 If expr is an argument of a foreign convention subprogram, the type it
3336 points to is directly the component type. In this case, the expression
3337 type may not match the corresponding FIELD_DECL type at this point, so we
3338 call "convert" here to fix that up if necessary. This type consistency is
3339 required, for instance because it ensures that possible later folding of
3340 component_refs against this constructor always yields something of the
3341 same type as the initial reference.
3343 Note that the call to "build_template" above is still fine, because it
3344 will only refer to the provided template_type in this case. */
3345 return
3346 gnat_build_constructor
3347 (type, tree_cons (TYPE_FIELDS (type),
3348 convert (TREE_TYPE (TYPE_FIELDS (type)), expr),
3349 tree_cons (TREE_CHAIN (TYPE_FIELDS (type)),
3350 template_addr, NULL_TREE)));
3353 /* Convert to a thin pointer type, TYPE. The only thing we know how to convert
3354 is something that is a fat pointer, so convert to it first if it EXPR
3355 is not already a fat pointer. */
3357 static tree
3358 convert_to_thin_pointer (tree type, tree expr)
3360 if (!TYPE_FAT_POINTER_P (TREE_TYPE (expr)))
3361 expr
3362 = convert_to_fat_pointer
3363 (TREE_TYPE (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type))), expr);
3365 /* We get the pointer to the data and use a NOP_EXPR to make it the
3366 proper GCC type. */
3367 expr = build_component_ref (expr, NULL_TREE, TYPE_FIELDS (TREE_TYPE (expr)),
3368 false);
3369 expr = build1 (NOP_EXPR, type, expr);
3371 return expr;
3374 /* Create an expression whose value is that of EXPR,
3375 converted to type TYPE. The TREE_TYPE of the value
3376 is always TYPE. This function implements all reasonable
3377 conversions; callers should filter out those that are
3378 not permitted by the language being compiled. */
3380 tree
3381 convert (tree type, tree expr)
3383 enum tree_code code = TREE_CODE (type);
3384 tree etype = TREE_TYPE (expr);
3385 enum tree_code ecode = TREE_CODE (etype);
3387 /* If EXPR is already the right type, we are done. */
3388 if (type == etype)
3389 return expr;
3391 /* If both input and output have padding and are of variable size, do this
3392 as an unchecked conversion. Likewise if one is a mere variant of the
3393 other, so we avoid a pointless unpad/repad sequence. */
3394 else if (code == RECORD_TYPE && ecode == RECORD_TYPE
3395 && TYPE_IS_PADDING_P (type) && TYPE_IS_PADDING_P (etype)
3396 && (!TREE_CONSTANT (TYPE_SIZE (type))
3397 || !TREE_CONSTANT (TYPE_SIZE (etype))
3398 || gnat_types_compatible_p (type, etype)
3399 || TYPE_NAME (TREE_TYPE (TYPE_FIELDS (type)))
3400 == TYPE_NAME (TREE_TYPE (TYPE_FIELDS (etype)))))
3403 /* If the output type has padding, convert to the inner type and
3404 make a constructor to build the record. */
3405 else if (code == RECORD_TYPE && TYPE_IS_PADDING_P (type))
3407 /* If we previously converted from another type and our type is
3408 of variable size, remove the conversion to avoid the need for
3409 variable-size temporaries. Likewise for a conversion between
3410 original and packable version. */
3411 if (TREE_CODE (expr) == VIEW_CONVERT_EXPR
3412 && (!TREE_CONSTANT (TYPE_SIZE (type))
3413 || (ecode == RECORD_TYPE
3414 && TYPE_NAME (etype)
3415 == TYPE_NAME (TREE_TYPE (TREE_OPERAND (expr, 0))))))
3416 expr = TREE_OPERAND (expr, 0);
3418 /* If we are just removing the padding from expr, convert the original
3419 object if we have variable size in order to avoid the need for some
3420 variable-size temporaries. Likewise if the padding is a mere variant
3421 of the other, so we avoid a pointless unpad/repad sequence. */
3422 if (TREE_CODE (expr) == COMPONENT_REF
3423 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE
3424 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (expr, 0)))
3425 && (!TREE_CONSTANT (TYPE_SIZE (type))
3426 || gnat_types_compatible_p (type,
3427 TREE_TYPE (TREE_OPERAND (expr, 0)))
3428 || (ecode == RECORD_TYPE
3429 && TYPE_NAME (etype)
3430 == TYPE_NAME (TREE_TYPE (TYPE_FIELDS (type))))))
3431 return convert (type, TREE_OPERAND (expr, 0));
3433 /* If the result type is a padded type with a self-referentially-sized
3434 field and the expression type is a record, do this as an
3435 unchecked conversion. */
3436 else if (TREE_CODE (etype) == RECORD_TYPE
3437 && CONTAINS_PLACEHOLDER_P (DECL_SIZE (TYPE_FIELDS (type))))
3438 return unchecked_convert (type, expr, false);
3440 else
3441 return
3442 gnat_build_constructor (type,
3443 tree_cons (TYPE_FIELDS (type),
3444 convert (TREE_TYPE
3445 (TYPE_FIELDS (type)),
3446 expr),
3447 NULL_TREE));
3450 /* If the input type has padding, remove it and convert to the output type.
3451 The conditions ordering is arranged to ensure that the output type is not
3452 a padding type here, as it is not clear whether the conversion would
3453 always be correct if this was to happen. */
3454 else if (ecode == RECORD_TYPE && TYPE_IS_PADDING_P (etype))
3456 tree unpadded;
3458 /* If we have just converted to this padded type, just get the
3459 inner expression. */
3460 if (TREE_CODE (expr) == CONSTRUCTOR
3461 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (expr))
3462 && VEC_index (constructor_elt, CONSTRUCTOR_ELTS (expr), 0)->index
3463 == TYPE_FIELDS (etype))
3464 unpadded
3465 = VEC_index (constructor_elt, CONSTRUCTOR_ELTS (expr), 0)->value;
3467 /* Otherwise, build an explicit component reference. */
3468 else
3469 unpadded
3470 = build_component_ref (expr, NULL_TREE, TYPE_FIELDS (etype), false);
3472 return convert (type, unpadded);
3475 /* If the input is a biased type, adjust first. */
3476 if (ecode == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (etype))
3477 return convert (type, fold_build2 (PLUS_EXPR, TREE_TYPE (etype),
3478 fold_convert (TREE_TYPE (etype),
3479 expr),
3480 TYPE_MIN_VALUE (etype)));
3482 /* If the input is a justified modular type, we need to extract the actual
3483 object before converting it to any other type with the exceptions of an
3484 unconstrained array or of a mere type variant. It is useful to avoid the
3485 extraction and conversion in the type variant case because it could end
3486 up replacing a VAR_DECL expr by a constructor and we might be about the
3487 take the address of the result. */
3488 if (ecode == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (etype)
3489 && code != UNCONSTRAINED_ARRAY_TYPE
3490 && TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (etype))
3491 return convert (type, build_component_ref (expr, NULL_TREE,
3492 TYPE_FIELDS (etype), false));
3494 /* If converting to a type that contains a template, convert to the data
3495 type and then build the template. */
3496 if (code == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (type))
3498 tree obj_type = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (type)));
3500 /* If the source already has a template, get a reference to the
3501 associated array only, as we are going to rebuild a template
3502 for the target type anyway. */
3503 expr = maybe_unconstrained_array (expr);
3505 return
3506 gnat_build_constructor
3507 (type,
3508 tree_cons (TYPE_FIELDS (type),
3509 build_template (TREE_TYPE (TYPE_FIELDS (type)),
3510 obj_type, NULL_TREE),
3511 tree_cons (TREE_CHAIN (TYPE_FIELDS (type)),
3512 convert (obj_type, expr), NULL_TREE)));
3515 /* There are some special cases of expressions that we process
3516 specially. */
3517 switch (TREE_CODE (expr))
3519 case ERROR_MARK:
3520 return expr;
3522 case NULL_EXPR:
3523 /* Just set its type here. For TRANSFORM_EXPR, we will do the actual
3524 conversion in gnat_expand_expr. NULL_EXPR does not represent
3525 and actual value, so no conversion is needed. */
3526 expr = copy_node (expr);
3527 TREE_TYPE (expr) = type;
3528 return expr;
3530 case STRING_CST:
3531 /* If we are converting a STRING_CST to another constrained array type,
3532 just make a new one in the proper type. */
3533 if (code == ecode && AGGREGATE_TYPE_P (etype)
3534 && !(TREE_CODE (TYPE_SIZE (etype)) == INTEGER_CST
3535 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
3537 expr = copy_node (expr);
3538 TREE_TYPE (expr) = type;
3539 return expr;
3541 break;
3543 case CONSTRUCTOR:
3544 /* If we are converting a CONSTRUCTOR to a mere variant type, just make
3545 a new one in the proper type. Likewise for a conversion between
3546 original and packable version. */
3547 if (code == ecode
3548 && (gnat_types_compatible_p (type, etype)
3549 || (code == RECORD_TYPE
3550 && TYPE_NAME (type) == TYPE_NAME (etype))))
3552 expr = copy_node (expr);
3553 TREE_TYPE (expr) = type;
3554 return expr;
3556 break;
3558 case UNCONSTRAINED_ARRAY_REF:
3559 /* Convert this to the type of the inner array by getting the address of
3560 the array from the template. */
3561 expr = build_unary_op (INDIRECT_REF, NULL_TREE,
3562 build_component_ref (TREE_OPERAND (expr, 0),
3563 get_identifier ("P_ARRAY"),
3564 NULL_TREE, false));
3565 etype = TREE_TYPE (expr);
3566 ecode = TREE_CODE (etype);
3567 break;
3569 case VIEW_CONVERT_EXPR:
3571 /* GCC 4.x is very sensitive to type consistency overall, and view
3572 conversions thus are very frequent. Even though just "convert"ing
3573 the inner operand to the output type is fine in most cases, it
3574 might expose unexpected input/output type mismatches in special
3575 circumstances so we avoid such recursive calls when we can. */
3576 tree op0 = TREE_OPERAND (expr, 0);
3578 /* If we are converting back to the original type, we can just
3579 lift the input conversion. This is a common occurrence with
3580 switches back-and-forth amongst type variants. */
3581 if (type == TREE_TYPE (op0))
3582 return op0;
3584 /* Otherwise, if we're converting between two aggregate types, we
3585 might be allowed to substitute the VIEW_CONVERT_EXPR target type
3586 in place or to just convert the inner expression. */
3587 if (AGGREGATE_TYPE_P (type) && AGGREGATE_TYPE_P (etype))
3589 /* If we are converting between mere variants, we can just
3590 substitute the VIEW_CONVERT_EXPR in place. */
3591 if (gnat_types_compatible_p (type, etype))
3592 return build1 (VIEW_CONVERT_EXPR, type, op0);
3594 /* Otherwise, we may just bypass the input view conversion unless
3595 one of the types is a fat pointer, which is handled by
3596 specialized code below which relies on exact type matching. */
3597 else if (!TYPE_FAT_POINTER_P (type) && !TYPE_FAT_POINTER_P (etype))
3598 return convert (type, op0);
3601 break;
3603 case INDIRECT_REF:
3604 /* If both types are record types, just convert the pointer and
3605 make a new INDIRECT_REF.
3607 ??? Disable this for now since it causes problems with the
3608 code in build_binary_op for MODIFY_EXPR which wants to
3609 strip off conversions. But that code really is a mess and
3610 we need to do this a much better way some time. */
3611 if (0
3612 && (TREE_CODE (type) == RECORD_TYPE
3613 || TREE_CODE (type) == UNION_TYPE)
3614 && (TREE_CODE (etype) == RECORD_TYPE
3615 || TREE_CODE (etype) == UNION_TYPE)
3616 && !TYPE_FAT_POINTER_P (type) && !TYPE_FAT_POINTER_P (etype))
3617 return build_unary_op (INDIRECT_REF, NULL_TREE,
3618 convert (build_pointer_type (type),
3619 TREE_OPERAND (expr, 0)));
3620 break;
3622 default:
3623 break;
3626 /* Check for converting to a pointer to an unconstrained array. */
3627 if (TYPE_FAT_POINTER_P (type) && !TYPE_FAT_POINTER_P (etype))
3628 return convert_to_fat_pointer (type, expr);
3630 /* If we are converting between two aggregate types that are mere
3631 variants, just make a VIEW_CONVERT_EXPR. */
3632 else if (code == ecode
3633 && AGGREGATE_TYPE_P (type)
3634 && gnat_types_compatible_p (type, etype))
3635 return build1 (VIEW_CONVERT_EXPR, type, expr);
3637 /* In all other cases of related types, make a NOP_EXPR. */
3638 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (etype)
3639 || (code == INTEGER_CST && ecode == INTEGER_CST
3640 && (type == TREE_TYPE (etype) || etype == TREE_TYPE (type))))
3641 return fold_convert (type, expr);
3643 switch (code)
3645 case VOID_TYPE:
3646 return fold_build1 (CONVERT_EXPR, type, expr);
3648 case BOOLEAN_TYPE:
3649 return fold_convert (type, gnat_truthvalue_conversion (expr));
3651 case INTEGER_TYPE:
3652 if (TYPE_HAS_ACTUAL_BOUNDS_P (type)
3653 && (ecode == ARRAY_TYPE || ecode == UNCONSTRAINED_ARRAY_TYPE
3654 || (ecode == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (etype))))
3655 return unchecked_convert (type, expr, false);
3656 else if (TYPE_BIASED_REPRESENTATION_P (type))
3657 return fold_convert (type,
3658 fold_build2 (MINUS_EXPR, TREE_TYPE (type),
3659 convert (TREE_TYPE (type), expr),
3660 TYPE_MIN_VALUE (type)));
3662 /* ... fall through ... */
3664 case ENUMERAL_TYPE:
3665 /* If we are converting an additive expression to an integer type
3666 with lower precision, be wary of the optimization that can be
3667 applied by convert_to_integer. There are 2 problematic cases:
3668 - if the first operand was originally of a biased type,
3669 because we could be recursively called to convert it
3670 to an intermediate type and thus rematerialize the
3671 additive operator endlessly,
3672 - if the expression contains a placeholder, because an
3673 intermediate conversion that changes the sign could
3674 be inserted and thus introduce an artificial overflow
3675 at compile time when the placeholder is substituted. */
3676 if (code == INTEGER_TYPE
3677 && ecode == INTEGER_TYPE
3678 && TYPE_PRECISION (type) < TYPE_PRECISION (etype)
3679 && (TREE_CODE (expr) == PLUS_EXPR || TREE_CODE (expr) == MINUS_EXPR))
3681 tree op0 = get_unwidened (TREE_OPERAND (expr, 0), type);
3683 if ((TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
3684 && TYPE_BIASED_REPRESENTATION_P (TREE_TYPE (op0)))
3685 || CONTAINS_PLACEHOLDER_P (expr))
3686 return build1 (NOP_EXPR, type, expr);
3689 return fold (convert_to_integer (type, expr));
3691 case POINTER_TYPE:
3692 case REFERENCE_TYPE:
3693 /* If converting between two pointers to records denoting
3694 both a template and type, adjust if needed to account
3695 for any differing offsets, since one might be negative. */
3696 if (TYPE_THIN_POINTER_P (etype) && TYPE_THIN_POINTER_P (type))
3698 tree bit_diff
3699 = size_diffop (bit_position (TYPE_FIELDS (TREE_TYPE (etype))),
3700 bit_position (TYPE_FIELDS (TREE_TYPE (type))));
3701 tree byte_diff = size_binop (CEIL_DIV_EXPR, bit_diff,
3702 sbitsize_int (BITS_PER_UNIT));
3704 expr = build1 (NOP_EXPR, type, expr);
3705 TREE_CONSTANT (expr) = TREE_CONSTANT (TREE_OPERAND (expr, 0));
3706 if (integer_zerop (byte_diff))
3707 return expr;
3709 return build_binary_op (POINTER_PLUS_EXPR, type, expr,
3710 fold (convert (sizetype, byte_diff)));
3713 /* If converting to a thin pointer, handle specially. */
3714 if (TYPE_THIN_POINTER_P (type)
3715 && TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)))
3716 return convert_to_thin_pointer (type, expr);
3718 /* If converting fat pointer to normal pointer, get the pointer to the
3719 array and then convert it. */
3720 else if (TYPE_FAT_POINTER_P (etype))
3721 expr = build_component_ref (expr, get_identifier ("P_ARRAY"),
3722 NULL_TREE, false);
3724 return fold (convert_to_pointer (type, expr));
3726 case REAL_TYPE:
3727 return fold (convert_to_real (type, expr));
3729 case RECORD_TYPE:
3730 if (TYPE_JUSTIFIED_MODULAR_P (type) && !AGGREGATE_TYPE_P (etype))
3731 return
3732 gnat_build_constructor
3733 (type, tree_cons (TYPE_FIELDS (type),
3734 convert (TREE_TYPE (TYPE_FIELDS (type)), expr),
3735 NULL_TREE));
3737 /* ... fall through ... */
3739 case ARRAY_TYPE:
3740 /* In these cases, assume the front-end has validated the conversion.
3741 If the conversion is valid, it will be a bit-wise conversion, so
3742 it can be viewed as an unchecked conversion. */
3743 return unchecked_convert (type, expr, false);
3745 case UNION_TYPE:
3746 /* This is a either a conversion between a tagged type and some
3747 subtype, which we have to mark as a UNION_TYPE because of
3748 overlapping fields or a conversion of an Unchecked_Union. */
3749 return unchecked_convert (type, expr, false);
3751 case UNCONSTRAINED_ARRAY_TYPE:
3752 /* If EXPR is a constrained array, take its address, convert it to a
3753 fat pointer, and then dereference it. Likewise if EXPR is a
3754 record containing both a template and a constrained array.
3755 Note that a record representing a justified modular type
3756 always represents a packed constrained array. */
3757 if (ecode == ARRAY_TYPE
3758 || (ecode == INTEGER_TYPE && TYPE_HAS_ACTUAL_BOUNDS_P (etype))
3759 || (ecode == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (etype))
3760 || (ecode == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (etype)))
3761 return
3762 build_unary_op
3763 (INDIRECT_REF, NULL_TREE,
3764 convert_to_fat_pointer (TREE_TYPE (type),
3765 build_unary_op (ADDR_EXPR,
3766 NULL_TREE, expr)));
3768 /* Do something very similar for converting one unconstrained
3769 array to another. */
3770 else if (ecode == UNCONSTRAINED_ARRAY_TYPE)
3771 return
3772 build_unary_op (INDIRECT_REF, NULL_TREE,
3773 convert (TREE_TYPE (type),
3774 build_unary_op (ADDR_EXPR,
3775 NULL_TREE, expr)));
3776 else
3777 gcc_unreachable ();
3779 case COMPLEX_TYPE:
3780 return fold (convert_to_complex (type, expr));
3782 default:
3783 gcc_unreachable ();
3787 /* Remove all conversions that are done in EXP. This includes converting
3788 from a padded type or to a justified modular type. If TRUE_ADDRESS
3789 is true, always return the address of the containing object even if
3790 the address is not bit-aligned. */
3792 tree
3793 remove_conversions (tree exp, bool true_address)
3795 switch (TREE_CODE (exp))
3797 case CONSTRUCTOR:
3798 if (true_address
3799 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
3800 && TYPE_JUSTIFIED_MODULAR_P (TREE_TYPE (exp)))
3801 return
3802 remove_conversions (VEC_index (constructor_elt,
3803 CONSTRUCTOR_ELTS (exp), 0)->value,
3804 true);
3805 break;
3807 case COMPONENT_REF:
3808 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == RECORD_TYPE
3809 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
3810 return remove_conversions (TREE_OPERAND (exp, 0), true_address);
3811 break;
3813 case VIEW_CONVERT_EXPR: case NON_LVALUE_EXPR:
3814 CASE_CONVERT:
3815 return remove_conversions (TREE_OPERAND (exp, 0), true_address);
3817 default:
3818 break;
3821 return exp;
3824 /* If EXP's type is an UNCONSTRAINED_ARRAY_TYPE, return an expression that
3825 refers to the underlying array. If its type has TYPE_CONTAINS_TEMPLATE_P,
3826 likewise return an expression pointing to the underlying array. */
3828 tree
3829 maybe_unconstrained_array (tree exp)
3831 enum tree_code code = TREE_CODE (exp);
3832 tree new;
3834 switch (TREE_CODE (TREE_TYPE (exp)))
3836 case UNCONSTRAINED_ARRAY_TYPE:
3837 if (code == UNCONSTRAINED_ARRAY_REF)
3840 = build_unary_op (INDIRECT_REF, NULL_TREE,
3841 build_component_ref (TREE_OPERAND (exp, 0),
3842 get_identifier ("P_ARRAY"),
3843 NULL_TREE, false));
3844 TREE_READONLY (new) = TREE_STATIC (new) = TREE_READONLY (exp);
3845 return new;
3848 else if (code == NULL_EXPR)
3849 return build1 (NULL_EXPR,
3850 TREE_TYPE (TREE_TYPE (TYPE_FIELDS
3851 (TREE_TYPE (TREE_TYPE (exp))))),
3852 TREE_OPERAND (exp, 0));
3854 case RECORD_TYPE:
3855 /* If this is a padded type, convert to the unpadded type and see if
3856 it contains a template. */
3857 if (TYPE_IS_PADDING_P (TREE_TYPE (exp)))
3859 new = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (exp))), exp);
3860 if (TREE_CODE (TREE_TYPE (new)) == RECORD_TYPE
3861 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (new)))
3862 return
3863 build_component_ref (new, NULL_TREE,
3864 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new))),
3867 else if (TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (exp)))
3868 return
3869 build_component_ref (exp, NULL_TREE,
3870 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (exp))), 0);
3871 break;
3873 default:
3874 break;
3877 return exp;
3880 /* Return an expression that does an unchecked conversion of EXPR to TYPE.
3881 If NOTRUNC_P is true, truncation operations should be suppressed. */
3883 tree
3884 unchecked_convert (tree type, tree expr, bool notrunc_p)
3886 tree etype = TREE_TYPE (expr);
3888 /* If the expression is already the right type, we are done. */
3889 if (etype == type)
3890 return expr;
3892 /* If both types types are integral just do a normal conversion.
3893 Likewise for a conversion to an unconstrained array. */
3894 if ((((INTEGRAL_TYPE_P (type)
3895 && !(TREE_CODE (type) == INTEGER_TYPE
3896 && TYPE_VAX_FLOATING_POINT_P (type)))
3897 || (POINTER_TYPE_P (type) && ! TYPE_THIN_POINTER_P (type))
3898 || (TREE_CODE (type) == RECORD_TYPE
3899 && TYPE_JUSTIFIED_MODULAR_P (type)))
3900 && ((INTEGRAL_TYPE_P (etype)
3901 && !(TREE_CODE (etype) == INTEGER_TYPE
3902 && TYPE_VAX_FLOATING_POINT_P (etype)))
3903 || (POINTER_TYPE_P (etype) && !TYPE_THIN_POINTER_P (etype))
3904 || (TREE_CODE (etype) == RECORD_TYPE
3905 && TYPE_JUSTIFIED_MODULAR_P (etype))))
3906 || TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
3908 tree rtype = type;
3909 bool final_unchecked = false;
3911 if (TREE_CODE (etype) == INTEGER_TYPE
3912 && TYPE_BIASED_REPRESENTATION_P (etype))
3914 tree ntype = copy_type (etype);
3916 TYPE_BIASED_REPRESENTATION_P (ntype) = 0;
3917 TYPE_MAIN_VARIANT (ntype) = ntype;
3918 expr = build1 (NOP_EXPR, ntype, expr);
3921 if (TREE_CODE (type) == INTEGER_TYPE
3922 && TYPE_BIASED_REPRESENTATION_P (type))
3924 rtype = copy_type (type);
3925 TYPE_BIASED_REPRESENTATION_P (rtype) = 0;
3926 TYPE_MAIN_VARIANT (rtype) = rtype;
3929 /* We have another special case: if we are unchecked converting subtype
3930 into a base type, we need to ensure that VRP doesn't propagate range
3931 information since this conversion may be done precisely to validate
3932 that the object is within the range it is supposed to have. */
3933 else if (TREE_CODE (expr) != INTEGER_CST
3934 && TREE_CODE (type) == INTEGER_TYPE && !TREE_TYPE (type)
3935 && ((TREE_CODE (etype) == INTEGER_TYPE && TREE_TYPE (etype))
3936 || TREE_CODE (etype) == ENUMERAL_TYPE
3937 || TREE_CODE (etype) == BOOLEAN_TYPE))
3939 /* The optimization barrier is a VIEW_CONVERT_EXPR node; moreover,
3940 in order not to be deemed an useless type conversion, it must
3941 be from subtype to base type.
3943 ??? This may raise addressability and/or aliasing issues because
3944 VIEW_CONVERT_EXPR gets gimplified as an lvalue, thus causing the
3945 address of its operand to be taken if it is deemed addressable
3946 and not already in GIMPLE form. */
3947 rtype = gnat_type_for_mode (TYPE_MODE (type), TYPE_UNSIGNED (type));
3948 rtype = copy_type (rtype);
3949 TYPE_MAIN_VARIANT (rtype) = rtype;
3950 TREE_TYPE (rtype) = type;
3951 final_unchecked = true;
3954 expr = convert (rtype, expr);
3955 if (type != rtype)
3956 expr = fold_build1 (final_unchecked ? VIEW_CONVERT_EXPR : NOP_EXPR,
3957 type, expr);
3960 /* If we are converting TO an integral type whose precision is not the
3961 same as its size, first unchecked convert to a record that contains
3962 an object of the output type. Then extract the field. */
3963 else if (INTEGRAL_TYPE_P (type) && TYPE_RM_SIZE (type)
3964 && 0 != compare_tree_int (TYPE_RM_SIZE (type),
3965 GET_MODE_BITSIZE (TYPE_MODE (type))))
3967 tree rec_type = make_node (RECORD_TYPE);
3968 tree field = create_field_decl (get_identifier ("OBJ"), type,
3969 rec_type, 1, 0, 0, 0);
3971 TYPE_FIELDS (rec_type) = field;
3972 layout_type (rec_type);
3974 expr = unchecked_convert (rec_type, expr, notrunc_p);
3975 expr = build_component_ref (expr, NULL_TREE, field, 0);
3978 /* Similarly for integral input type whose precision is not equal to its
3979 size. */
3980 else if (INTEGRAL_TYPE_P (etype) && TYPE_RM_SIZE (etype)
3981 && 0 != compare_tree_int (TYPE_RM_SIZE (etype),
3982 GET_MODE_BITSIZE (TYPE_MODE (etype))))
3984 tree rec_type = make_node (RECORD_TYPE);
3985 tree field
3986 = create_field_decl (get_identifier ("OBJ"), etype, rec_type,
3987 1, 0, 0, 0);
3989 TYPE_FIELDS (rec_type) = field;
3990 layout_type (rec_type);
3992 expr = gnat_build_constructor (rec_type, build_tree_list (field, expr));
3993 expr = unchecked_convert (type, expr, notrunc_p);
3996 /* We have a special case when we are converting between two
3997 unconstrained array types. In that case, take the address,
3998 convert the fat pointer types, and dereference. */
3999 else if (TREE_CODE (etype) == UNCONSTRAINED_ARRAY_TYPE
4000 && TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
4001 expr = build_unary_op (INDIRECT_REF, NULL_TREE,
4002 build1 (VIEW_CONVERT_EXPR, TREE_TYPE (type),
4003 build_unary_op (ADDR_EXPR, NULL_TREE,
4004 expr)));
4005 else
4007 expr = maybe_unconstrained_array (expr);
4008 etype = TREE_TYPE (expr);
4009 expr = fold_build1 (VIEW_CONVERT_EXPR, type, expr);
4012 /* If the result is an integral type whose size is not equal to
4013 the size of the underlying machine type, sign- or zero-extend
4014 the result. We need not do this in the case where the input is
4015 an integral type of the same precision and signedness or if the output
4016 is a biased type or if both the input and output are unsigned. */
4017 if (!notrunc_p
4018 && INTEGRAL_TYPE_P (type) && TYPE_RM_SIZE (type)
4019 && !(TREE_CODE (type) == INTEGER_TYPE
4020 && TYPE_BIASED_REPRESENTATION_P (type))
4021 && 0 != compare_tree_int (TYPE_RM_SIZE (type),
4022 GET_MODE_BITSIZE (TYPE_MODE (type)))
4023 && !(INTEGRAL_TYPE_P (etype)
4024 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (etype)
4025 && operand_equal_p (TYPE_RM_SIZE (type),
4026 (TYPE_RM_SIZE (etype) != 0
4027 ? TYPE_RM_SIZE (etype) : TYPE_SIZE (etype)),
4029 && !(TYPE_UNSIGNED (type) && TYPE_UNSIGNED (etype)))
4031 tree base_type = gnat_type_for_mode (TYPE_MODE (type),
4032 TYPE_UNSIGNED (type));
4033 tree shift_expr
4034 = convert (base_type,
4035 size_binop (MINUS_EXPR,
4036 bitsize_int
4037 (GET_MODE_BITSIZE (TYPE_MODE (type))),
4038 TYPE_RM_SIZE (type)));
4039 expr
4040 = convert (type,
4041 build_binary_op (RSHIFT_EXPR, base_type,
4042 build_binary_op (LSHIFT_EXPR, base_type,
4043 convert (base_type, expr),
4044 shift_expr),
4045 shift_expr));
4048 /* An unchecked conversion should never raise Constraint_Error. The code
4049 below assumes that GCC's conversion routines overflow the same way that
4050 the underlying hardware does. This is probably true. In the rare case
4051 when it is false, we can rely on the fact that such conversions are
4052 erroneous anyway. */
4053 if (TREE_CODE (expr) == INTEGER_CST)
4054 TREE_OVERFLOW (expr) = 0;
4056 /* If the sizes of the types differ and this is an VIEW_CONVERT_EXPR,
4057 show no longer constant. */
4058 if (TREE_CODE (expr) == VIEW_CONVERT_EXPR
4059 && !operand_equal_p (TYPE_SIZE_UNIT (type), TYPE_SIZE_UNIT (etype),
4060 OEP_ONLY_CONST))
4061 TREE_CONSTANT (expr) = 0;
4063 return expr;
4066 /* Return the appropriate GCC tree code for the specified GNAT type,
4067 the latter being a record type as predicated by Is_Record_Type. */
4069 enum tree_code
4070 tree_code_for_record_type (Entity_Id gnat_type)
4072 Node_Id component_list
4073 = Component_List (Type_Definition
4074 (Declaration_Node
4075 (Implementation_Base_Type (gnat_type))));
4076 Node_Id component;
4078 /* Make this a UNION_TYPE unless it's either not an Unchecked_Union or
4079 we have a non-discriminant field outside a variant. In either case,
4080 it's a RECORD_TYPE. */
4082 if (!Is_Unchecked_Union (gnat_type))
4083 return RECORD_TYPE;
4085 for (component = First_Non_Pragma (Component_Items (component_list));
4086 Present (component);
4087 component = Next_Non_Pragma (component))
4088 if (Ekind (Defining_Entity (component)) == E_Component)
4089 return RECORD_TYPE;
4091 return UNION_TYPE;
4094 /* Return true if GNU_TYPE is suitable as the type of a non-aliased
4095 component of an aggregate type. */
4097 bool
4098 type_for_nonaliased_component_p (tree gnu_type)
4100 /* If the type is passed by reference, we may have pointers to the
4101 component so it cannot be made non-aliased. */
4102 if (must_pass_by_ref (gnu_type) || default_pass_by_ref (gnu_type))
4103 return false;
4105 /* We used to say that any component of aggregate type is aliased
4106 because the front-end may take 'Reference of it. The front-end
4107 has been enhanced in the meantime so as to use a renaming instead
4108 in most cases, but the back-end can probably take the address of
4109 such a component too so we go for the conservative stance.
4111 For instance, we might need the address of any array type, even
4112 if normally passed by copy, to construct a fat pointer if the
4113 component is used as an actual for an unconstrained formal.
4115 Likewise for record types: even if a specific record subtype is
4116 passed by copy, the parent type might be passed by ref (e.g. if
4117 it's of variable size) and we might take the address of a child
4118 component to pass to a parent formal. We have no way to check
4119 for such conditions here. */
4120 if (AGGREGATE_TYPE_P (gnu_type))
4121 return false;
4123 return true;
4126 /* Perform final processing on global variables. */
4128 void
4129 gnat_write_global_declarations (void)
4131 /* Proceed to optimize and emit assembly.
4132 FIXME: shouldn't be the front end's responsibility to call this. */
4133 cgraph_optimize ();
4135 /* Emit debug info for all global declarations. */
4136 emit_debug_global_declarations (VEC_address (tree, global_decls),
4137 VEC_length (tree, global_decls));
4140 /* ************************************************************************
4141 * * GCC builtins support *
4142 * ************************************************************************ */
4144 /* The general scheme is fairly simple:
4146 For each builtin function/type to be declared, gnat_install_builtins calls
4147 internal facilities which eventually get to gnat_push_decl, which in turn
4148 tracks the so declared builtin function decls in the 'builtin_decls' global
4149 datastructure. When an Intrinsic subprogram declaration is processed, we
4150 search this global datastructure to retrieve the associated BUILT_IN DECL
4151 node. */
4153 /* Search the chain of currently available builtin declarations for a node
4154 corresponding to function NAME (an IDENTIFIER_NODE). Return the first node
4155 found, if any, or NULL_TREE otherwise. */
4156 tree
4157 builtin_decl_for (tree name)
4159 unsigned i;
4160 tree decl;
4162 for (i = 0; VEC_iterate(tree, builtin_decls, i, decl); i++)
4163 if (DECL_NAME (decl) == name)
4164 return decl;
4166 return NULL_TREE;
4169 /* The code below eventually exposes gnat_install_builtins, which declares
4170 the builtin types and functions we might need, either internally or as
4171 user accessible facilities.
4173 ??? This is a first implementation shot, still in rough shape. It is
4174 heavily inspired from the "C" family implementation, with chunks copied
4175 verbatim from there.
4177 Two obvious TODO candidates are
4178 o Use a more efficient name/decl mapping scheme
4179 o Devise a middle-end infrastructure to avoid having to copy
4180 pieces between front-ends. */
4182 /* ----------------------------------------------------------------------- *
4183 * BUILTIN ELEMENTARY TYPES *
4184 * ----------------------------------------------------------------------- */
4186 /* Standard data types to be used in builtin argument declarations. */
4188 enum c_tree_index
4190 CTI_SIGNED_SIZE_TYPE, /* For format checking only. */
4191 CTI_STRING_TYPE,
4192 CTI_CONST_STRING_TYPE,
4194 CTI_MAX
4197 static tree c_global_trees[CTI_MAX];
4199 #define signed_size_type_node c_global_trees[CTI_SIGNED_SIZE_TYPE]
4200 #define string_type_node c_global_trees[CTI_STRING_TYPE]
4201 #define const_string_type_node c_global_trees[CTI_CONST_STRING_TYPE]
4203 /* ??? In addition some attribute handlers, we currently don't support a
4204 (small) number of builtin-types, which in turns inhibits support for a
4205 number of builtin functions. */
4206 #define wint_type_node void_type_node
4207 #define intmax_type_node void_type_node
4208 #define uintmax_type_node void_type_node
4210 /* Build the void_list_node (void_type_node having been created). */
4212 static tree
4213 build_void_list_node (void)
4215 tree t = build_tree_list (NULL_TREE, void_type_node);
4216 return t;
4219 /* Used to help initialize the builtin-types.def table. When a type of
4220 the correct size doesn't exist, use error_mark_node instead of NULL.
4221 The later results in segfaults even when a decl using the type doesn't
4222 get invoked. */
4224 static tree
4225 builtin_type_for_size (int size, bool unsignedp)
4227 tree type = lang_hooks.types.type_for_size (size, unsignedp);
4228 return type ? type : error_mark_node;
4231 /* Build/push the elementary type decls that builtin functions/types
4232 will need. */
4234 static void
4235 install_builtin_elementary_types (void)
4237 signed_size_type_node = size_type_node;
4238 pid_type_node = integer_type_node;
4239 void_list_node = build_void_list_node ();
4241 string_type_node = build_pointer_type (char_type_node);
4242 const_string_type_node
4243 = build_pointer_type (build_qualified_type
4244 (char_type_node, TYPE_QUAL_CONST));
4247 /* ----------------------------------------------------------------------- *
4248 * BUILTIN FUNCTION TYPES *
4249 * ----------------------------------------------------------------------- */
4251 /* Now, builtin function types per se. */
4253 enum c_builtin_type
4255 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
4256 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
4257 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
4258 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
4259 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4260 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4261 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
4262 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
4263 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
4264 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
4265 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
4266 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
4267 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4268 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4269 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
4270 NAME,
4271 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
4272 #include "builtin-types.def"
4273 #undef DEF_PRIMITIVE_TYPE
4274 #undef DEF_FUNCTION_TYPE_0
4275 #undef DEF_FUNCTION_TYPE_1
4276 #undef DEF_FUNCTION_TYPE_2
4277 #undef DEF_FUNCTION_TYPE_3
4278 #undef DEF_FUNCTION_TYPE_4
4279 #undef DEF_FUNCTION_TYPE_5
4280 #undef DEF_FUNCTION_TYPE_6
4281 #undef DEF_FUNCTION_TYPE_7
4282 #undef DEF_FUNCTION_TYPE_VAR_0
4283 #undef DEF_FUNCTION_TYPE_VAR_1
4284 #undef DEF_FUNCTION_TYPE_VAR_2
4285 #undef DEF_FUNCTION_TYPE_VAR_3
4286 #undef DEF_FUNCTION_TYPE_VAR_4
4287 #undef DEF_FUNCTION_TYPE_VAR_5
4288 #undef DEF_POINTER_TYPE
4289 BT_LAST
4292 typedef enum c_builtin_type builtin_type;
4294 /* A temporary array used in communication with def_fn_type. */
4295 static GTY(()) tree builtin_types[(int) BT_LAST + 1];
4297 /* A helper function for install_builtin_types. Build function type
4298 for DEF with return type RET and N arguments. If VAR is true, then the
4299 function should be variadic after those N arguments.
4301 Takes special care not to ICE if any of the types involved are
4302 error_mark_node, which indicates that said type is not in fact available
4303 (see builtin_type_for_size). In which case the function type as a whole
4304 should be error_mark_node. */
4306 static void
4307 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
4309 tree args = NULL, t;
4310 va_list list;
4311 int i;
4313 va_start (list, n);
4314 for (i = 0; i < n; ++i)
4316 builtin_type a = va_arg (list, builtin_type);
4317 t = builtin_types[a];
4318 if (t == error_mark_node)
4319 goto egress;
4320 args = tree_cons (NULL_TREE, t, args);
4322 va_end (list);
4324 args = nreverse (args);
4325 if (!var)
4326 args = chainon (args, void_list_node);
4328 t = builtin_types[ret];
4329 if (t == error_mark_node)
4330 goto egress;
4331 t = build_function_type (t, args);
4333 egress:
4334 builtin_types[def] = t;
4337 /* Build the builtin function types and install them in the builtin_types
4338 array for later use in builtin function decls. */
4340 static void
4341 install_builtin_function_types (void)
4343 tree va_list_ref_type_node;
4344 tree va_list_arg_type_node;
4346 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4348 va_list_arg_type_node = va_list_ref_type_node =
4349 build_pointer_type (TREE_TYPE (va_list_type_node));
4351 else
4353 va_list_arg_type_node = va_list_type_node;
4354 va_list_ref_type_node = build_reference_type (va_list_type_node);
4357 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
4358 builtin_types[ENUM] = VALUE;
4359 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
4360 def_fn_type (ENUM, RETURN, 0, 0);
4361 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
4362 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
4363 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
4364 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
4365 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4366 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
4367 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4368 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
4369 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4370 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4371 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4372 ARG6) \
4373 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
4374 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4375 ARG6, ARG7) \
4376 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4377 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
4378 def_fn_type (ENUM, RETURN, 1, 0);
4379 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
4380 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
4381 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
4382 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
4383 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4384 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
4385 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4386 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
4387 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4388 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4389 #define DEF_POINTER_TYPE(ENUM, TYPE) \
4390 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
4392 #include "builtin-types.def"
4394 #undef DEF_PRIMITIVE_TYPE
4395 #undef DEF_FUNCTION_TYPE_1
4396 #undef DEF_FUNCTION_TYPE_2
4397 #undef DEF_FUNCTION_TYPE_3
4398 #undef DEF_FUNCTION_TYPE_4
4399 #undef DEF_FUNCTION_TYPE_5
4400 #undef DEF_FUNCTION_TYPE_6
4401 #undef DEF_FUNCTION_TYPE_VAR_0
4402 #undef DEF_FUNCTION_TYPE_VAR_1
4403 #undef DEF_FUNCTION_TYPE_VAR_2
4404 #undef DEF_FUNCTION_TYPE_VAR_3
4405 #undef DEF_FUNCTION_TYPE_VAR_4
4406 #undef DEF_FUNCTION_TYPE_VAR_5
4407 #undef DEF_POINTER_TYPE
4408 builtin_types[(int) BT_LAST] = NULL_TREE;
4411 /* ----------------------------------------------------------------------- *
4412 * BUILTIN ATTRIBUTES *
4413 * ----------------------------------------------------------------------- */
4415 enum built_in_attribute
4417 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4418 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4419 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4420 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4421 #include "builtin-attrs.def"
4422 #undef DEF_ATTR_NULL_TREE
4423 #undef DEF_ATTR_INT
4424 #undef DEF_ATTR_IDENT
4425 #undef DEF_ATTR_TREE_LIST
4426 ATTR_LAST
4429 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4431 static void
4432 install_builtin_attributes (void)
4434 /* Fill in the built_in_attributes array. */
4435 #define DEF_ATTR_NULL_TREE(ENUM) \
4436 built_in_attributes[(int) ENUM] = NULL_TREE;
4437 #define DEF_ATTR_INT(ENUM, VALUE) \
4438 built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE);
4439 #define DEF_ATTR_IDENT(ENUM, STRING) \
4440 built_in_attributes[(int) ENUM] = get_identifier (STRING);
4441 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
4442 built_in_attributes[(int) ENUM] \
4443 = tree_cons (built_in_attributes[(int) PURPOSE], \
4444 built_in_attributes[(int) VALUE], \
4445 built_in_attributes[(int) CHAIN]);
4446 #include "builtin-attrs.def"
4447 #undef DEF_ATTR_NULL_TREE
4448 #undef DEF_ATTR_INT
4449 #undef DEF_ATTR_IDENT
4450 #undef DEF_ATTR_TREE_LIST
4453 /* Handle a "const" attribute; arguments as in
4454 struct attribute_spec.handler. */
4456 static tree
4457 handle_const_attribute (tree *node, tree ARG_UNUSED (name),
4458 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
4459 bool *no_add_attrs)
4461 if (TREE_CODE (*node) == FUNCTION_DECL)
4462 TREE_READONLY (*node) = 1;
4463 else
4464 *no_add_attrs = true;
4466 return NULL_TREE;
4469 /* Handle a "nothrow" attribute; arguments as in
4470 struct attribute_spec.handler. */
4472 static tree
4473 handle_nothrow_attribute (tree *node, tree ARG_UNUSED (name),
4474 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
4475 bool *no_add_attrs)
4477 if (TREE_CODE (*node) == FUNCTION_DECL)
4478 TREE_NOTHROW (*node) = 1;
4479 else
4480 *no_add_attrs = true;
4482 return NULL_TREE;
4485 /* Handle a "pure" attribute; arguments as in
4486 struct attribute_spec.handler. */
4488 static tree
4489 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
4490 int ARG_UNUSED (flags), bool *no_add_attrs)
4492 if (TREE_CODE (*node) == FUNCTION_DECL)
4493 DECL_PURE_P (*node) = 1;
4494 /* ??? TODO: Support types. */
4495 else
4497 warning (OPT_Wattributes, "%qE attribute ignored", name);
4498 *no_add_attrs = true;
4501 return NULL_TREE;
4504 /* Handle a "no vops" attribute; arguments as in
4505 struct attribute_spec.handler. */
4507 static tree
4508 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
4509 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
4510 bool *ARG_UNUSED (no_add_attrs))
4512 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
4513 DECL_IS_NOVOPS (*node) = 1;
4514 return NULL_TREE;
4517 /* Helper for nonnull attribute handling; fetch the operand number
4518 from the attribute argument list. */
4520 static bool
4521 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
4523 /* Verify the arg number is a constant. */
4524 if (TREE_CODE (arg_num_expr) != INTEGER_CST
4525 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
4526 return false;
4528 *valp = TREE_INT_CST_LOW (arg_num_expr);
4529 return true;
4532 /* Handle the "nonnull" attribute. */
4533 static tree
4534 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
4535 tree args, int ARG_UNUSED (flags),
4536 bool *no_add_attrs)
4538 tree type = *node;
4539 unsigned HOST_WIDE_INT attr_arg_num;
4541 /* If no arguments are specified, all pointer arguments should be
4542 non-null. Verify a full prototype is given so that the arguments
4543 will have the correct types when we actually check them later. */
4544 if (!args)
4546 if (!TYPE_ARG_TYPES (type))
4548 error ("nonnull attribute without arguments on a non-prototype");
4549 *no_add_attrs = true;
4551 return NULL_TREE;
4554 /* Argument list specified. Verify that each argument number references
4555 a pointer argument. */
4556 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
4558 tree argument;
4559 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
4561 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
4563 error ("nonnull argument has invalid operand number (argument %lu)",
4564 (unsigned long) attr_arg_num);
4565 *no_add_attrs = true;
4566 return NULL_TREE;
4569 argument = TYPE_ARG_TYPES (type);
4570 if (argument)
4572 for (ck_num = 1; ; ck_num++)
4574 if (!argument || ck_num == arg_num)
4575 break;
4576 argument = TREE_CHAIN (argument);
4579 if (!argument
4580 || TREE_CODE (TREE_VALUE (argument)) == VOID_TYPE)
4582 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
4583 (unsigned long) attr_arg_num, (unsigned long) arg_num);
4584 *no_add_attrs = true;
4585 return NULL_TREE;
4588 if (TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE)
4590 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
4591 (unsigned long) attr_arg_num, (unsigned long) arg_num);
4592 *no_add_attrs = true;
4593 return NULL_TREE;
4598 return NULL_TREE;
4601 /* Handle a "sentinel" attribute. */
4603 static tree
4604 handle_sentinel_attribute (tree *node, tree name, tree args,
4605 int ARG_UNUSED (flags), bool *no_add_attrs)
4607 tree params = TYPE_ARG_TYPES (*node);
4609 if (!params)
4611 warning (OPT_Wattributes,
4612 "%qE attribute requires prototypes with named arguments", name);
4613 *no_add_attrs = true;
4615 else
4617 while (TREE_CHAIN (params))
4618 params = TREE_CHAIN (params);
4620 if (VOID_TYPE_P (TREE_VALUE (params)))
4622 warning (OPT_Wattributes,
4623 "%qE attribute only applies to variadic functions", name);
4624 *no_add_attrs = true;
4628 if (args)
4630 tree position = TREE_VALUE (args);
4632 if (TREE_CODE (position) != INTEGER_CST)
4634 warning (0, "requested position is not an integer constant");
4635 *no_add_attrs = true;
4637 else
4639 if (tree_int_cst_lt (position, integer_zero_node))
4641 warning (0, "requested position is less than zero");
4642 *no_add_attrs = true;
4647 return NULL_TREE;
4650 /* Handle a "noreturn" attribute; arguments as in
4651 struct attribute_spec.handler. */
4653 static tree
4654 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
4655 int ARG_UNUSED (flags), bool *no_add_attrs)
4657 tree type = TREE_TYPE (*node);
4659 /* See FIXME comment in c_common_attribute_table. */
4660 if (TREE_CODE (*node) == FUNCTION_DECL)
4661 TREE_THIS_VOLATILE (*node) = 1;
4662 else if (TREE_CODE (type) == POINTER_TYPE
4663 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
4664 TREE_TYPE (*node)
4665 = build_pointer_type
4666 (build_type_variant (TREE_TYPE (type),
4667 TYPE_READONLY (TREE_TYPE (type)), 1));
4668 else
4670 warning (OPT_Wattributes, "%qE attribute ignored", name);
4671 *no_add_attrs = true;
4674 return NULL_TREE;
4677 /* Handle a "malloc" attribute; arguments as in
4678 struct attribute_spec.handler. */
4680 static tree
4681 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
4682 int ARG_UNUSED (flags), bool *no_add_attrs)
4684 if (TREE_CODE (*node) == FUNCTION_DECL
4685 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4686 DECL_IS_MALLOC (*node) = 1;
4687 else
4689 warning (OPT_Wattributes, "%qE attribute ignored", name);
4690 *no_add_attrs = true;
4693 return NULL_TREE;
4696 /* Fake handler for attributes we don't properly support. */
4698 tree
4699 fake_attribute_handler (tree * ARG_UNUSED (node),
4700 tree ARG_UNUSED (name),
4701 tree ARG_UNUSED (args),
4702 int ARG_UNUSED (flags),
4703 bool * ARG_UNUSED (no_add_attrs))
4705 return NULL_TREE;
4708 /* Handle a "type_generic" attribute. */
4710 static tree
4711 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
4712 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
4713 bool * ARG_UNUSED (no_add_attrs))
4715 tree params;
4717 /* Ensure we have a function type. */
4718 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
4720 params = TYPE_ARG_TYPES (*node);
4721 while (params && ! VOID_TYPE_P (TREE_VALUE (params)))
4722 params = TREE_CHAIN (params);
4724 /* Ensure we have a variadic function. */
4725 gcc_assert (!params);
4727 return NULL_TREE;
4730 /* ----------------------------------------------------------------------- *
4731 * BUILTIN FUNCTIONS *
4732 * ----------------------------------------------------------------------- */
4734 /* Worker for DEF_BUILTIN. Possibly define a builtin function with one or two
4735 names. Does not declare a non-__builtin_ function if flag_no_builtin, or
4736 if nonansi_p and flag_no_nonansi_builtin. */
4738 static void
4739 def_builtin_1 (enum built_in_function fncode,
4740 const char *name,
4741 enum built_in_class fnclass,
4742 tree fntype, tree libtype,
4743 bool both_p, bool fallback_p,
4744 bool nonansi_p ATTRIBUTE_UNUSED,
4745 tree fnattrs, bool implicit_p)
4747 tree decl;
4748 const char *libname;
4750 /* Preserve an already installed decl. It most likely was setup in advance
4751 (e.g. as part of the internal builtins) for specific reasons. */
4752 if (built_in_decls[(int) fncode] != NULL_TREE)
4753 return;
4755 gcc_assert ((!both_p && !fallback_p)
4756 || !strncmp (name, "__builtin_",
4757 strlen ("__builtin_")));
4759 libname = name + strlen ("__builtin_");
4760 decl = add_builtin_function (name, fntype, fncode, fnclass,
4761 (fallback_p ? libname : NULL),
4762 fnattrs);
4763 if (both_p)
4764 /* ??? This is normally further controlled by command-line options
4765 like -fno-builtin, but we don't have them for Ada. */
4766 add_builtin_function (libname, libtype, fncode, fnclass,
4767 NULL, fnattrs);
4769 built_in_decls[(int) fncode] = decl;
4770 if (implicit_p)
4771 implicit_built_in_decls[(int) fncode] = decl;
4774 static int flag_isoc94 = 0;
4775 static int flag_isoc99 = 0;
4777 /* Install what the common builtins.def offers. */
4779 static void
4780 install_builtin_functions (void)
4782 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
4783 NONANSI_P, ATTRS, IMPLICIT, COND) \
4784 if (NAME && COND) \
4785 def_builtin_1 (ENUM, NAME, CLASS, \
4786 builtin_types[(int) TYPE], \
4787 builtin_types[(int) LIBTYPE], \
4788 BOTH_P, FALLBACK_P, NONANSI_P, \
4789 built_in_attributes[(int) ATTRS], IMPLICIT);
4790 #include "builtins.def"
4791 #undef DEF_BUILTIN
4794 /* ----------------------------------------------------------------------- *
4795 * BUILTIN FUNCTIONS *
4796 * ----------------------------------------------------------------------- */
4798 /* Install the builtin functions we might need. */
4800 void
4801 gnat_install_builtins (void)
4803 install_builtin_elementary_types ();
4804 install_builtin_function_types ();
4805 install_builtin_attributes ();
4807 /* Install builtins used by generic middle-end pieces first. Some of these
4808 know about internal specificities and control attributes accordingly, for
4809 instance __builtin_alloca vs no-throw and -fstack-check. We will ignore
4810 the generic definition from builtins.def. */
4811 build_common_builtin_nodes ();
4813 /* Now, install the target specific builtins, such as the AltiVec family on
4814 ppc, and the common set as exposed by builtins.def. */
4815 targetm.init_builtins ();
4816 install_builtin_functions ();
4819 #include "gt-ada-utils.h"
4820 #include "gtype-ada.h"