Update concepts branch to revision 131834
[official-gcc.git] / gcc / ada / utils.c
blobf255d37d6efa0ee9755da13b3118792a61a39dd2
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 of attributes to accommodate 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 add_parallel_type (TYPE_STUB_DECL (record_type), new_record_type);
1064 /* Now scan all the fields, replacing each field with a new
1065 field corresponding to the new encoding. */
1066 for (old_field = TYPE_FIELDS (record_type); old_field;
1067 old_field = TREE_CHAIN (old_field))
1069 tree field_type = TREE_TYPE (old_field);
1070 tree field_name = DECL_NAME (old_field);
1071 tree new_field;
1072 tree curpos = bit_position (old_field);
1073 bool var = false;
1074 unsigned int align = 0;
1075 tree pos;
1077 /* See how the position was modified from the last position.
1079 There are two basic cases we support: a value was added
1080 to the last position or the last position was rounded to
1081 a boundary and they something was added. Check for the
1082 first case first. If not, see if there is any evidence
1083 of rounding. If so, round the last position and try
1084 again.
1086 If this is a union, the position can be taken as zero. */
1088 /* Some computations depend on the shape of the position expression,
1089 so strip conversions to make sure it's exposed. */
1090 curpos = remove_conversions (curpos, true);
1092 if (TREE_CODE (new_record_type) == UNION_TYPE)
1093 pos = bitsize_zero_node, align = 0;
1094 else
1095 pos = compute_related_constant (curpos, last_pos);
1097 if (!pos && TREE_CODE (curpos) == MULT_EXPR
1098 && host_integerp (TREE_OPERAND (curpos, 1), 1))
1100 tree offset = TREE_OPERAND (curpos, 0);
1101 align = tree_low_cst (TREE_OPERAND (curpos, 1), 1);
1103 /* An offset which is a bitwise AND with a negative power of 2
1104 means an alignment corresponding to this power of 2. */
1105 offset = remove_conversions (offset, true);
1106 if (TREE_CODE (offset) == BIT_AND_EXPR
1107 && host_integerp (TREE_OPERAND (offset, 1), 0)
1108 && tree_int_cst_sgn (TREE_OPERAND (offset, 1)) < 0)
1110 unsigned int pow
1111 = - tree_low_cst (TREE_OPERAND (offset, 1), 0);
1112 if (exact_log2 (pow) > 0)
1113 align *= pow;
1116 pos = compute_related_constant (curpos,
1117 round_up (last_pos, align));
1119 else if (!pos && TREE_CODE (curpos) == PLUS_EXPR
1120 && TREE_CODE (TREE_OPERAND (curpos, 1)) == INTEGER_CST
1121 && TREE_CODE (TREE_OPERAND (curpos, 0)) == MULT_EXPR
1122 && host_integerp (TREE_OPERAND
1123 (TREE_OPERAND (curpos, 0), 1),
1126 align
1127 = tree_low_cst
1128 (TREE_OPERAND (TREE_OPERAND (curpos, 0), 1), 1);
1129 pos = compute_related_constant (curpos,
1130 round_up (last_pos, align));
1132 else if (potential_alignment_gap (prev_old_field, old_field,
1133 pos))
1135 align = TYPE_ALIGN (field_type);
1136 pos = compute_related_constant (curpos,
1137 round_up (last_pos, align));
1140 /* If we can't compute a position, set it to zero.
1142 ??? We really should abort here, but it's too much work
1143 to get this correct for all cases. */
1145 if (!pos)
1146 pos = bitsize_zero_node;
1148 /* See if this type is variable-sized and make a pointer type
1149 and indicate the indirection if so. Beware that the debug
1150 back-end may adjust the position computed above according
1151 to the alignment of the field type, i.e. the pointer type
1152 in this case, if we don't preventively counter that. */
1153 if (TREE_CODE (DECL_SIZE (old_field)) != INTEGER_CST)
1155 field_type = build_pointer_type (field_type);
1156 if (align != 0 && TYPE_ALIGN (field_type) > align)
1158 field_type = copy_node (field_type);
1159 TYPE_ALIGN (field_type) = align;
1161 var = true;
1164 /* Make a new field name, if necessary. */
1165 if (var || align != 0)
1167 char suffix[16];
1169 if (align != 0)
1170 sprintf (suffix, "XV%c%u", var ? 'L' : 'A',
1171 align / BITS_PER_UNIT);
1172 else
1173 strcpy (suffix, "XVL");
1175 field_name = concat_id_with_name (field_name, suffix);
1178 new_field = create_field_decl (field_name, field_type,
1179 new_record_type, 0,
1180 DECL_SIZE (old_field), pos, 0);
1181 TREE_CHAIN (new_field) = TYPE_FIELDS (new_record_type);
1182 TYPE_FIELDS (new_record_type) = new_field;
1184 /* If old_field is a QUAL_UNION_TYPE, take its size as being
1185 zero. The only time it's not the last field of the record
1186 is when there are other components at fixed positions after
1187 it (meaning there was a rep clause for every field) and we
1188 want to be able to encode them. */
1189 last_pos = size_binop (PLUS_EXPR, bit_position (old_field),
1190 (TREE_CODE (TREE_TYPE (old_field))
1191 == QUAL_UNION_TYPE)
1192 ? bitsize_zero_node
1193 : DECL_SIZE (old_field));
1194 prev_old_field = old_field;
1197 TYPE_FIELDS (new_record_type)
1198 = nreverse (TYPE_FIELDS (new_record_type));
1200 rest_of_type_decl_compilation (TYPE_STUB_DECL (new_record_type));
1203 rest_of_type_decl_compilation (TYPE_STUB_DECL (record_type));
1206 /* Append PARALLEL_TYPE on the chain of parallel types for decl. */
1208 void
1209 add_parallel_type (tree decl, tree parallel_type)
1211 tree d = decl;
1213 while (DECL_PARALLEL_TYPE (d))
1214 d = TYPE_STUB_DECL (DECL_PARALLEL_TYPE (d));
1216 SET_DECL_PARALLEL_TYPE (d, parallel_type);
1219 /* Return the parallel type associated to a type, if any. */
1221 tree
1222 get_parallel_type (tree type)
1224 if (TYPE_STUB_DECL (type))
1225 return DECL_PARALLEL_TYPE (TYPE_STUB_DECL (type));
1226 else
1227 return NULL_TREE;
1230 /* Utility function of above to merge LAST_SIZE, the previous size of a record
1231 with FIRST_BIT and SIZE that describe a field. SPECIAL is nonzero
1232 if this represents a QUAL_UNION_TYPE in which case we must look for
1233 COND_EXPRs and replace a value of zero with the old size. If HAS_REP
1234 is nonzero, we must take the MAX of the end position of this field
1235 with LAST_SIZE. In all other cases, we use FIRST_BIT plus SIZE.
1237 We return an expression for the size. */
1239 static tree
1240 merge_sizes (tree last_size, tree first_bit, tree size, bool special,
1241 bool has_rep)
1243 tree type = TREE_TYPE (last_size);
1244 tree new;
1246 if (!special || TREE_CODE (size) != COND_EXPR)
1248 new = size_binop (PLUS_EXPR, first_bit, size);
1249 if (has_rep)
1250 new = size_binop (MAX_EXPR, last_size, new);
1253 else
1254 new = fold_build3 (COND_EXPR, type, TREE_OPERAND (size, 0),
1255 integer_zerop (TREE_OPERAND (size, 1))
1256 ? last_size : merge_sizes (last_size, first_bit,
1257 TREE_OPERAND (size, 1),
1258 1, has_rep),
1259 integer_zerop (TREE_OPERAND (size, 2))
1260 ? last_size : merge_sizes (last_size, first_bit,
1261 TREE_OPERAND (size, 2),
1262 1, has_rep));
1264 /* We don't need any NON_VALUE_EXPRs and they can confuse us (especially
1265 when fed through substitute_in_expr) into thinking that a constant
1266 size is not constant. */
1267 while (TREE_CODE (new) == NON_LVALUE_EXPR)
1268 new = TREE_OPERAND (new, 0);
1270 return new;
1273 /* Utility function of above to see if OP0 and OP1, both of SIZETYPE, are
1274 related by the addition of a constant. Return that constant if so. */
1276 static tree
1277 compute_related_constant (tree op0, tree op1)
1279 tree op0_var, op1_var;
1280 tree op0_con = split_plus (op0, &op0_var);
1281 tree op1_con = split_plus (op1, &op1_var);
1282 tree result = size_binop (MINUS_EXPR, op0_con, op1_con);
1284 if (operand_equal_p (op0_var, op1_var, 0))
1285 return result;
1286 else if (operand_equal_p (op0, size_binop (PLUS_EXPR, op1_var, result), 0))
1287 return result;
1288 else
1289 return 0;
1292 /* Utility function of above to split a tree OP which may be a sum, into a
1293 constant part, which is returned, and a variable part, which is stored
1294 in *PVAR. *PVAR may be bitsize_zero_node. All operations must be of
1295 bitsizetype. */
1297 static tree
1298 split_plus (tree in, tree *pvar)
1300 /* Strip NOPS in order to ease the tree traversal and maximize the
1301 potential for constant or plus/minus discovery. We need to be careful
1302 to always return and set *pvar to bitsizetype trees, but it's worth
1303 the effort. */
1304 STRIP_NOPS (in);
1306 *pvar = convert (bitsizetype, in);
1308 if (TREE_CODE (in) == INTEGER_CST)
1310 *pvar = bitsize_zero_node;
1311 return convert (bitsizetype, in);
1313 else if (TREE_CODE (in) == PLUS_EXPR || TREE_CODE (in) == MINUS_EXPR)
1315 tree lhs_var, rhs_var;
1316 tree lhs_con = split_plus (TREE_OPERAND (in, 0), &lhs_var);
1317 tree rhs_con = split_plus (TREE_OPERAND (in, 1), &rhs_var);
1319 if (lhs_var == TREE_OPERAND (in, 0)
1320 && rhs_var == TREE_OPERAND (in, 1))
1321 return bitsize_zero_node;
1323 *pvar = size_binop (TREE_CODE (in), lhs_var, rhs_var);
1324 return size_binop (TREE_CODE (in), lhs_con, rhs_con);
1326 else
1327 return bitsize_zero_node;
1330 /* Return a FUNCTION_TYPE node. RETURN_TYPE is the type returned by the
1331 subprogram. If it is void_type_node, then we are dealing with a procedure,
1332 otherwise we are dealing with a function. PARAM_DECL_LIST is a list of
1333 PARM_DECL nodes that are the subprogram arguments. CICO_LIST is the
1334 copy-in/copy-out list to be stored into TYPE_CICO_LIST.
1335 RETURNS_UNCONSTRAINED is true if the function returns an unconstrained
1336 object. RETURNS_BY_REF is true if the function returns by reference.
1337 RETURNS_BY_TARGET_PTR is true if the function is to be passed (as its
1338 first parameter) the address of the place to copy its result. */
1340 tree
1341 create_subprog_type (tree return_type, tree param_decl_list, tree cico_list,
1342 bool returns_unconstrained, bool returns_by_ref,
1343 bool returns_by_target_ptr)
1345 /* A chain of TREE_LIST nodes whose TREE_VALUEs are the data type nodes of
1346 the subprogram formal parameters. This list is generated by traversing the
1347 input list of PARM_DECL nodes. */
1348 tree param_type_list = NULL;
1349 tree param_decl;
1350 tree type;
1352 for (param_decl = param_decl_list; param_decl;
1353 param_decl = TREE_CHAIN (param_decl))
1354 param_type_list = tree_cons (NULL_TREE, TREE_TYPE (param_decl),
1355 param_type_list);
1357 /* The list of the function parameter types has to be terminated by the void
1358 type to signal to the back-end that we are not dealing with a variable
1359 parameter subprogram, but that the subprogram has a fixed number of
1360 parameters. */
1361 param_type_list = tree_cons (NULL_TREE, void_type_node, param_type_list);
1363 /* The list of argument types has been created in reverse
1364 so nreverse it. */
1365 param_type_list = nreverse (param_type_list);
1367 type = build_function_type (return_type, param_type_list);
1369 /* TYPE may have been shared since GCC hashes types. If it has a CICO_LIST
1370 or the new type should, make a copy of TYPE. Likewise for
1371 RETURNS_UNCONSTRAINED and RETURNS_BY_REF. */
1372 if (TYPE_CI_CO_LIST (type) || cico_list
1373 || TYPE_RETURNS_UNCONSTRAINED_P (type) != returns_unconstrained
1374 || TYPE_RETURNS_BY_REF_P (type) != returns_by_ref
1375 || TYPE_RETURNS_BY_TARGET_PTR_P (type) != returns_by_target_ptr)
1376 type = copy_type (type);
1378 TYPE_CI_CO_LIST (type) = cico_list;
1379 TYPE_RETURNS_UNCONSTRAINED_P (type) = returns_unconstrained;
1380 TYPE_RETURNS_BY_REF_P (type) = returns_by_ref;
1381 TYPE_RETURNS_BY_TARGET_PTR_P (type) = returns_by_target_ptr;
1382 return type;
1385 /* Return a copy of TYPE but safe to modify in any way. */
1387 tree
1388 copy_type (tree type)
1390 tree new = copy_node (type);
1392 /* copy_node clears this field instead of copying it, because it is
1393 aliased with TREE_CHAIN. */
1394 TYPE_STUB_DECL (new) = TYPE_STUB_DECL (type);
1396 TYPE_POINTER_TO (new) = 0;
1397 TYPE_REFERENCE_TO (new) = 0;
1398 TYPE_MAIN_VARIANT (new) = new;
1399 TYPE_NEXT_VARIANT (new) = 0;
1401 return new;
1404 /* Return an INTEGER_TYPE of SIZETYPE with range MIN to MAX and whose
1405 TYPE_INDEX_TYPE is INDEX. GNAT_NODE is used for the position of
1406 the decl. */
1408 tree
1409 create_index_type (tree min, tree max, tree index, Node_Id gnat_node)
1411 /* First build a type for the desired range. */
1412 tree type = build_index_2_type (min, max);
1414 /* If this type has the TYPE_INDEX_TYPE we want, return it. Otherwise, if it
1415 doesn't have TYPE_INDEX_TYPE set, set it to INDEX. If TYPE_INDEX_TYPE
1416 is set, but not to INDEX, make a copy of this type with the requested
1417 index type. Note that we have no way of sharing these types, but that's
1418 only a small hole. */
1419 if (TYPE_INDEX_TYPE (type) == index)
1420 return type;
1421 else if (TYPE_INDEX_TYPE (type))
1422 type = copy_type (type);
1424 SET_TYPE_INDEX_TYPE (type, index);
1425 create_type_decl (NULL_TREE, type, NULL, true, false, gnat_node);
1426 return type;
1429 /* Return a TYPE_DECL node. TYPE_NAME gives the name of the type (a character
1430 string) and TYPE is a ..._TYPE node giving its data type.
1431 ARTIFICIAL_P is true if this is a declaration that was generated
1432 by the compiler. DEBUG_INFO_P is true if we need to write debugging
1433 information about this type. GNAT_NODE is used for the position of
1434 the decl. */
1436 tree
1437 create_type_decl (tree type_name, tree type, struct attrib *attr_list,
1438 bool artificial_p, bool debug_info_p, Node_Id gnat_node)
1440 tree type_decl = build_decl (TYPE_DECL, type_name, type);
1441 enum tree_code code = TREE_CODE (type);
1443 DECL_ARTIFICIAL (type_decl) = artificial_p;
1445 if (!TYPE_IS_DUMMY_P (type))
1446 gnat_pushdecl (type_decl, gnat_node);
1448 process_attributes (type_decl, attr_list);
1450 /* Pass type declaration information to the debugger unless this is an
1451 UNCONSTRAINED_ARRAY_TYPE, which the debugger does not support,
1452 and ENUMERAL_TYPE or RECORD_TYPE which is handled separately, or
1453 type for which debugging information was not requested. */
1454 if (code == UNCONSTRAINED_ARRAY_TYPE || !debug_info_p)
1455 DECL_IGNORED_P (type_decl) = 1;
1456 else if (code != ENUMERAL_TYPE
1457 && (code != RECORD_TYPE || TYPE_IS_FAT_POINTER_P (type))
1458 && !((code == POINTER_TYPE || code == REFERENCE_TYPE)
1459 && TYPE_IS_DUMMY_P (TREE_TYPE (type))))
1460 rest_of_type_decl_compilation (type_decl);
1462 return type_decl;
1465 /* Return a VAR_DECL or CONST_DECL node.
1467 VAR_NAME gives the name of the variable. ASM_NAME is its assembler name
1468 (if provided). TYPE is its data type (a GCC ..._TYPE node). VAR_INIT is
1469 the GCC tree for an optional initial expression; NULL_TREE if none.
1471 CONST_FLAG is true if this variable is constant, in which case we might
1472 return a CONST_DECL node unless CONST_DECL_ALLOWED_P is false.
1474 PUBLIC_FLAG is true if this definition is to be made visible outside of
1475 the current compilation unit. This flag should be set when processing the
1476 variable definitions in a package specification.
1478 EXTERN_FLAG is nonzero when processing an external variable declaration (as
1479 opposed to a definition: no storage is to be allocated for the variable).
1481 STATIC_FLAG is only relevant when not at top level. In that case
1482 it indicates whether to always allocate storage to the variable.
1484 GNAT_NODE is used for the position of the decl. */
1486 tree
1487 create_var_decl_1 (tree var_name, tree asm_name, tree type, tree var_init,
1488 bool const_flag, bool public_flag, bool extern_flag,
1489 bool static_flag, bool const_decl_allowed_p,
1490 struct attrib *attr_list, Node_Id gnat_node)
1492 bool init_const
1493 = (var_init != 0
1494 && gnat_types_compatible_p (type, TREE_TYPE (var_init))
1495 && (global_bindings_p () || static_flag
1496 ? initializer_constant_valid_p (var_init, TREE_TYPE (var_init)) != 0
1497 : TREE_CONSTANT (var_init)));
1499 /* Whether we will make TREE_CONSTANT the DECL we produce here, in which
1500 case the initializer may be used in-lieu of the DECL node (as done in
1501 Identifier_to_gnu). This is useful to prevent the need of elaboration
1502 code when an identifier for which such a decl is made is in turn used as
1503 an initializer. We used to rely on CONST vs VAR_DECL for this purpose,
1504 but extra constraints apply to this choice (see below) and are not
1505 relevant to the distinction we wish to make. */
1506 bool constant_p = const_flag && init_const;
1508 /* The actual DECL node. CONST_DECL was initially intended for enumerals
1509 and may be used for scalars in general but not for aggregates. */
1510 tree var_decl
1511 = build_decl ((constant_p && const_decl_allowed_p
1512 && !AGGREGATE_TYPE_P (type)) ? CONST_DECL : VAR_DECL,
1513 var_name, type);
1515 /* If this is external, throw away any initializations (they will be done
1516 elsewhere) unless this is a constant for which we would like to remain
1517 able to get the initializer. If we are defining a global here, leave a
1518 constant initialization and save any variable elaborations for the
1519 elaboration routine. If we are just annotating types, throw away the
1520 initialization if it isn't a constant. */
1521 if ((extern_flag && !constant_p)
1522 || (type_annotate_only && var_init && !TREE_CONSTANT (var_init)))
1523 var_init = NULL_TREE;
1525 /* At the global level, an initializer requiring code to be generated
1526 produces elaboration statements. Check that such statements are allowed,
1527 that is, not violating a No_Elaboration_Code restriction. */
1528 if (global_bindings_p () && var_init != 0 && ! init_const)
1529 Check_Elaboration_Code_Allowed (gnat_node);
1531 /* Ada doesn't feature Fortran-like COMMON variables so we shouldn't
1532 try to fiddle with DECL_COMMON. However, on platforms that don't
1533 support global BSS sections, uninitialized global variables would
1534 go in DATA instead, thus increasing the size of the executable. */
1535 if (!flag_no_common
1536 && TREE_CODE (var_decl) == VAR_DECL
1537 && !have_global_bss_p ())
1538 DECL_COMMON (var_decl) = 1;
1539 DECL_INITIAL (var_decl) = var_init;
1540 TREE_READONLY (var_decl) = const_flag;
1541 DECL_EXTERNAL (var_decl) = extern_flag;
1542 TREE_PUBLIC (var_decl) = public_flag || extern_flag;
1543 TREE_CONSTANT (var_decl) = constant_p;
1544 TREE_THIS_VOLATILE (var_decl) = TREE_SIDE_EFFECTS (var_decl)
1545 = TYPE_VOLATILE (type);
1547 /* If it's public and not external, always allocate storage for it.
1548 At the global binding level we need to allocate static storage for the
1549 variable if and only if it's not external. If we are not at the top level
1550 we allocate automatic storage unless requested not to. */
1551 TREE_STATIC (var_decl)
1552 = public_flag || (global_bindings_p () ? !extern_flag : static_flag);
1554 if (asm_name && VAR_OR_FUNCTION_DECL_P (var_decl))
1555 SET_DECL_ASSEMBLER_NAME (var_decl, asm_name);
1557 process_attributes (var_decl, attr_list);
1559 /* Add this decl to the current binding level. */
1560 gnat_pushdecl (var_decl, gnat_node);
1562 if (TREE_SIDE_EFFECTS (var_decl))
1563 TREE_ADDRESSABLE (var_decl) = 1;
1565 if (TREE_CODE (var_decl) != CONST_DECL)
1567 if (global_bindings_p ())
1568 rest_of_decl_compilation (var_decl, true, 0);
1570 else
1571 expand_decl (var_decl);
1573 return var_decl;
1576 /* Return true if TYPE, an aggregate type, contains (or is) an array. */
1578 static bool
1579 aggregate_type_contains_array_p (tree type)
1581 switch (TREE_CODE (type))
1583 case RECORD_TYPE:
1584 case UNION_TYPE:
1585 case QUAL_UNION_TYPE:
1587 tree field;
1588 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1589 if (AGGREGATE_TYPE_P (TREE_TYPE (field))
1590 && aggregate_type_contains_array_p (TREE_TYPE (field)))
1591 return true;
1592 return false;
1595 case ARRAY_TYPE:
1596 return true;
1598 default:
1599 gcc_unreachable ();
1603 /* Returns a FIELD_DECL node. FIELD_NAME the field name, FIELD_TYPE is its
1604 type, and RECORD_TYPE is the type of the parent. PACKED is nonzero if
1605 this field is in a record type with a "pragma pack". If SIZE is nonzero
1606 it is the specified size for this field. If POS is nonzero, it is the bit
1607 position. If ADDRESSABLE is nonzero, it means we are allowed to take
1608 the address of this field for aliasing purposes. If it is negative, we
1609 should not make a bitfield, which is used by make_aligning_type. */
1611 tree
1612 create_field_decl (tree field_name, tree field_type, tree record_type,
1613 int packed, tree size, tree pos, int addressable)
1615 tree field_decl = build_decl (FIELD_DECL, field_name, field_type);
1617 DECL_CONTEXT (field_decl) = record_type;
1618 TREE_READONLY (field_decl) = TYPE_READONLY (field_type);
1620 /* If FIELD_TYPE is BLKmode, we must ensure this is aligned to at least a
1621 byte boundary since GCC cannot handle less-aligned BLKmode bitfields.
1622 Likewise for an aggregate without specified position that contains an
1623 array, because in this case slices of variable length of this array
1624 must be handled by GCC and variable-sized objects need to be aligned
1625 to at least a byte boundary. */
1626 if (packed && (TYPE_MODE (field_type) == BLKmode
1627 || (!pos
1628 && AGGREGATE_TYPE_P (field_type)
1629 && aggregate_type_contains_array_p (field_type))))
1630 DECL_ALIGN (field_decl) = BITS_PER_UNIT;
1632 /* If a size is specified, use it. Otherwise, if the record type is packed
1633 compute a size to use, which may differ from the object's natural size.
1634 We always set a size in this case to trigger the checks for bitfield
1635 creation below, which is typically required when no position has been
1636 specified. */
1637 if (size)
1638 size = convert (bitsizetype, size);
1639 else if (packed == 1)
1641 size = rm_size (field_type);
1643 /* For a constant size larger than MAX_FIXED_MODE_SIZE, round up to
1644 byte. */
1645 if (TREE_CODE (size) == INTEGER_CST
1646 && compare_tree_int (size, MAX_FIXED_MODE_SIZE) > 0)
1647 size = round_up (size, BITS_PER_UNIT);
1650 /* If we may, according to ADDRESSABLE, make a bitfield if a size is
1651 specified for two reasons: first if the size differs from the natural
1652 size. Second, if the alignment is insufficient. There are a number of
1653 ways the latter can be true.
1655 We never make a bitfield if the type of the field has a nonconstant size,
1656 because no such entity requiring bitfield operations should reach here.
1658 We do *preventively* make a bitfield when there might be the need for it
1659 but we don't have all the necessary information to decide, as is the case
1660 of a field with no specified position in a packed record.
1662 We also don't look at STRICT_ALIGNMENT here, and rely on later processing
1663 in layout_decl or finish_record_type to clear the bit_field indication if
1664 it is in fact not needed. */
1665 if (addressable >= 0
1666 && size
1667 && TREE_CODE (size) == INTEGER_CST
1668 && TREE_CODE (TYPE_SIZE (field_type)) == INTEGER_CST
1669 && (!tree_int_cst_equal (size, TYPE_SIZE (field_type))
1670 || (pos && !value_factor_p (pos, TYPE_ALIGN (field_type)))
1671 || packed
1672 || (TYPE_ALIGN (record_type) != 0
1673 && TYPE_ALIGN (record_type) < TYPE_ALIGN (field_type))))
1675 DECL_BIT_FIELD (field_decl) = 1;
1676 DECL_SIZE (field_decl) = size;
1677 if (!packed && !pos)
1678 DECL_ALIGN (field_decl)
1679 = (TYPE_ALIGN (record_type) != 0
1680 ? MIN (TYPE_ALIGN (record_type), TYPE_ALIGN (field_type))
1681 : TYPE_ALIGN (field_type));
1684 DECL_PACKED (field_decl) = pos ? DECL_BIT_FIELD (field_decl) : packed;
1686 /* Bump the alignment if need be, either for bitfield/packing purposes or
1687 to satisfy the type requirements if no such consideration applies. When
1688 we get the alignment from the type, indicate if this is from an explicit
1689 user request, which prevents stor-layout from lowering it later on. */
1691 int bit_align
1692 = (DECL_BIT_FIELD (field_decl) ? 1
1693 : packed && TYPE_MODE (field_type) != BLKmode ? BITS_PER_UNIT : 0);
1695 if (bit_align > DECL_ALIGN (field_decl))
1696 DECL_ALIGN (field_decl) = bit_align;
1697 else if (!bit_align && TYPE_ALIGN (field_type) > DECL_ALIGN (field_decl))
1699 DECL_ALIGN (field_decl) = TYPE_ALIGN (field_type);
1700 DECL_USER_ALIGN (field_decl) = TYPE_USER_ALIGN (field_type);
1704 if (pos)
1706 /* We need to pass in the alignment the DECL is known to have.
1707 This is the lowest-order bit set in POS, but no more than
1708 the alignment of the record, if one is specified. Note
1709 that an alignment of 0 is taken as infinite. */
1710 unsigned int known_align;
1712 if (host_integerp (pos, 1))
1713 known_align = tree_low_cst (pos, 1) & - tree_low_cst (pos, 1);
1714 else
1715 known_align = BITS_PER_UNIT;
1717 if (TYPE_ALIGN (record_type)
1718 && (known_align == 0 || known_align > TYPE_ALIGN (record_type)))
1719 known_align = TYPE_ALIGN (record_type);
1721 layout_decl (field_decl, known_align);
1722 SET_DECL_OFFSET_ALIGN (field_decl,
1723 host_integerp (pos, 1) ? BIGGEST_ALIGNMENT
1724 : BITS_PER_UNIT);
1725 pos_from_bit (&DECL_FIELD_OFFSET (field_decl),
1726 &DECL_FIELD_BIT_OFFSET (field_decl),
1727 DECL_OFFSET_ALIGN (field_decl), pos);
1729 DECL_HAS_REP_P (field_decl) = 1;
1732 /* In addition to what our caller says, claim the field is addressable if we
1733 know that its type is not suitable.
1735 The field may also be "technically" nonaddressable, meaning that even if
1736 we attempt to take the field's address we will actually get the address
1737 of a copy. This is the case for true bitfields, but the DECL_BIT_FIELD
1738 value we have at this point is not accurate enough, so we don't account
1739 for this here and let finish_record_type decide. */
1740 if (!type_for_nonaliased_component_p (field_type))
1741 addressable = 1;
1743 DECL_NONADDRESSABLE_P (field_decl) = !addressable;
1745 return field_decl;
1748 /* Returns a PARM_DECL node. PARAM_NAME is the name of the parameter,
1749 PARAM_TYPE is its type. READONLY is true if the parameter is
1750 readonly (either an In parameter or an address of a pass-by-ref
1751 parameter). */
1753 tree
1754 create_param_decl (tree param_name, tree param_type, bool readonly)
1756 tree param_decl = build_decl (PARM_DECL, param_name, param_type);
1758 /* Honor targetm.calls.promote_prototypes(), as not doing so can
1759 lead to various ABI violations. */
1760 if (targetm.calls.promote_prototypes (param_type)
1761 && (TREE_CODE (param_type) == INTEGER_TYPE
1762 || TREE_CODE (param_type) == ENUMERAL_TYPE)
1763 && TYPE_PRECISION (param_type) < TYPE_PRECISION (integer_type_node))
1765 /* We have to be careful about biased types here. Make a subtype
1766 of integer_type_node with the proper biasing. */
1767 if (TREE_CODE (param_type) == INTEGER_TYPE
1768 && TYPE_BIASED_REPRESENTATION_P (param_type))
1770 param_type
1771 = copy_type (build_range_type (integer_type_node,
1772 TYPE_MIN_VALUE (param_type),
1773 TYPE_MAX_VALUE (param_type)));
1775 TYPE_BIASED_REPRESENTATION_P (param_type) = 1;
1777 else
1778 param_type = integer_type_node;
1781 DECL_ARG_TYPE (param_decl) = param_type;
1782 TREE_READONLY (param_decl) = readonly;
1783 return param_decl;
1786 /* Given a DECL and ATTR_LIST, process the listed attributes. */
1788 void
1789 process_attributes (tree decl, struct attrib *attr_list)
1791 for (; attr_list; attr_list = attr_list->next)
1792 switch (attr_list->type)
1794 case ATTR_MACHINE_ATTRIBUTE:
1795 decl_attributes (&decl, tree_cons (attr_list->name, attr_list->args,
1796 NULL_TREE),
1797 ATTR_FLAG_TYPE_IN_PLACE);
1798 break;
1800 case ATTR_LINK_ALIAS:
1801 if (! DECL_EXTERNAL (decl))
1803 TREE_STATIC (decl) = 1;
1804 assemble_alias (decl, attr_list->name);
1806 break;
1808 case ATTR_WEAK_EXTERNAL:
1809 if (SUPPORTS_WEAK)
1810 declare_weak (decl);
1811 else
1812 post_error ("?weak declarations not supported on this target",
1813 attr_list->error_point);
1814 break;
1816 case ATTR_LINK_SECTION:
1817 if (targetm.have_named_sections)
1819 DECL_SECTION_NAME (decl)
1820 = build_string (IDENTIFIER_LENGTH (attr_list->name),
1821 IDENTIFIER_POINTER (attr_list->name));
1822 DECL_COMMON (decl) = 0;
1824 else
1825 post_error ("?section attributes are not supported for this target",
1826 attr_list->error_point);
1827 break;
1829 case ATTR_LINK_CONSTRUCTOR:
1830 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1831 TREE_USED (decl) = 1;
1832 break;
1834 case ATTR_LINK_DESTRUCTOR:
1835 DECL_STATIC_DESTRUCTOR (decl) = 1;
1836 TREE_USED (decl) = 1;
1837 break;
1841 /* Record a global renaming pointer. */
1843 void
1844 record_global_renaming_pointer (tree decl)
1846 gcc_assert (DECL_RENAMED_OBJECT (decl));
1847 VEC_safe_push (tree, gc, global_renaming_pointers, decl);
1850 /* Invalidate the global renaming pointers. */
1852 void
1853 invalidate_global_renaming_pointers (void)
1855 unsigned int i;
1856 tree iter;
1858 for (i = 0; VEC_iterate(tree, global_renaming_pointers, i, iter); i++)
1859 SET_DECL_RENAMED_OBJECT (iter, NULL_TREE);
1861 VEC_free (tree, gc, global_renaming_pointers);
1864 /* Return true if VALUE is a known to be a multiple of FACTOR, which must be
1865 a power of 2. */
1867 bool
1868 value_factor_p (tree value, HOST_WIDE_INT factor)
1870 if (host_integerp (value, 1))
1871 return tree_low_cst (value, 1) % factor == 0;
1873 if (TREE_CODE (value) == MULT_EXPR)
1874 return (value_factor_p (TREE_OPERAND (value, 0), factor)
1875 || value_factor_p (TREE_OPERAND (value, 1), factor));
1877 return false;
1880 /* Given 2 consecutive field decls PREV_FIELD and CURR_FIELD, return true
1881 unless we can prove these 2 fields are laid out in such a way that no gap
1882 exist between the end of PREV_FIELD and the beginning of CURR_FIELD. OFFSET
1883 is the distance in bits between the end of PREV_FIELD and the starting
1884 position of CURR_FIELD. It is ignored if null. */
1886 static bool
1887 potential_alignment_gap (tree prev_field, tree curr_field, tree offset)
1889 /* If this is the first field of the record, there cannot be any gap */
1890 if (!prev_field)
1891 return false;
1893 /* If the previous field is a union type, then return False: The only
1894 time when such a field is not the last field of the record is when
1895 there are other components at fixed positions after it (meaning there
1896 was a rep clause for every field), in which case we don't want the
1897 alignment constraint to override them. */
1898 if (TREE_CODE (TREE_TYPE (prev_field)) == QUAL_UNION_TYPE)
1899 return false;
1901 /* If the distance between the end of prev_field and the beginning of
1902 curr_field is constant, then there is a gap if the value of this
1903 constant is not null. */
1904 if (offset && host_integerp (offset, 1))
1905 return !integer_zerop (offset);
1907 /* If the size and position of the previous field are constant,
1908 then check the sum of this size and position. There will be a gap
1909 iff it is not multiple of the current field alignment. */
1910 if (host_integerp (DECL_SIZE (prev_field), 1)
1911 && host_integerp (bit_position (prev_field), 1))
1912 return ((tree_low_cst (bit_position (prev_field), 1)
1913 + tree_low_cst (DECL_SIZE (prev_field), 1))
1914 % DECL_ALIGN (curr_field) != 0);
1916 /* If both the position and size of the previous field are multiples
1917 of the current field alignment, there cannot be any gap. */
1918 if (value_factor_p (bit_position (prev_field), DECL_ALIGN (curr_field))
1919 && value_factor_p (DECL_SIZE (prev_field), DECL_ALIGN (curr_field)))
1920 return false;
1922 /* Fallback, return that there may be a potential gap */
1923 return true;
1926 /* Returns a LABEL_DECL node for LABEL_NAME. */
1928 tree
1929 create_label_decl (tree label_name)
1931 tree label_decl = build_decl (LABEL_DECL, label_name, void_type_node);
1933 DECL_CONTEXT (label_decl) = current_function_decl;
1934 DECL_MODE (label_decl) = VOIDmode;
1935 DECL_SOURCE_LOCATION (label_decl) = input_location;
1937 return label_decl;
1940 /* Returns a FUNCTION_DECL node. SUBPROG_NAME is the name of the subprogram,
1941 ASM_NAME is its assembler name, SUBPROG_TYPE is its type (a FUNCTION_TYPE
1942 node), PARAM_DECL_LIST is the list of the subprogram arguments (a list of
1943 PARM_DECL nodes chained through the TREE_CHAIN field).
1945 INLINE_FLAG, PUBLIC_FLAG, EXTERN_FLAG, and ATTR_LIST are used to set the
1946 appropriate fields in the FUNCTION_DECL. GNAT_NODE gives the location. */
1948 tree
1949 create_subprog_decl (tree subprog_name, tree asm_name,
1950 tree subprog_type, tree param_decl_list, bool inline_flag,
1951 bool public_flag, bool extern_flag,
1952 struct attrib *attr_list, Node_Id gnat_node)
1954 tree return_type = TREE_TYPE (subprog_type);
1955 tree subprog_decl = build_decl (FUNCTION_DECL, subprog_name, subprog_type);
1957 /* If this is a function nested inside an inlined external function, it
1958 means we aren't going to compile the outer function unless it is
1959 actually inlined, so do the same for us. */
1960 if (current_function_decl && DECL_INLINE (current_function_decl)
1961 && DECL_EXTERNAL (current_function_decl))
1962 extern_flag = true;
1964 DECL_EXTERNAL (subprog_decl) = extern_flag;
1965 TREE_PUBLIC (subprog_decl) = public_flag;
1966 TREE_STATIC (subprog_decl) = 1;
1967 TREE_READONLY (subprog_decl) = TYPE_READONLY (subprog_type);
1968 TREE_THIS_VOLATILE (subprog_decl) = TYPE_VOLATILE (subprog_type);
1969 TREE_SIDE_EFFECTS (subprog_decl) = TYPE_VOLATILE (subprog_type);
1970 DECL_ARGUMENTS (subprog_decl) = param_decl_list;
1971 DECL_RESULT (subprog_decl) = build_decl (RESULT_DECL, 0, return_type);
1972 DECL_ARTIFICIAL (DECL_RESULT (subprog_decl)) = 1;
1973 DECL_IGNORED_P (DECL_RESULT (subprog_decl)) = 1;
1975 /* TREE_ADDRESSABLE is set on the result type to request the use of the
1976 target by-reference return mechanism. This is not supported all the
1977 way down to RTL expansion with GCC 4, which ICEs on temporary creation
1978 attempts with such a type and expects DECL_BY_REFERENCE to be set on
1979 the RESULT_DECL instead - see gnat_genericize for more details. */
1980 if (TREE_ADDRESSABLE (TREE_TYPE (DECL_RESULT (subprog_decl))))
1982 tree result_decl = DECL_RESULT (subprog_decl);
1984 TREE_ADDRESSABLE (TREE_TYPE (result_decl)) = 0;
1985 DECL_BY_REFERENCE (result_decl) = 1;
1988 if (inline_flag)
1989 DECL_DECLARED_INLINE_P (subprog_decl) = 1;
1991 if (asm_name)
1993 SET_DECL_ASSEMBLER_NAME (subprog_decl, asm_name);
1995 /* The expand_main_function circuitry expects "main_identifier_node" to
1996 designate the DECL_NAME of the 'main' entry point, in turn expected
1997 to be declared as the "main" function literally by default. Ada
1998 program entry points are typically declared with a different name
1999 within the binder generated file, exported as 'main' to satisfy the
2000 system expectations. Redirect main_identifier_node in this case. */
2001 if (asm_name == main_identifier_node)
2002 main_identifier_node = DECL_NAME (subprog_decl);
2005 process_attributes (subprog_decl, attr_list);
2007 /* Add this decl to the current binding level. */
2008 gnat_pushdecl (subprog_decl, gnat_node);
2010 /* Output the assembler code and/or RTL for the declaration. */
2011 rest_of_decl_compilation (subprog_decl, global_bindings_p (), 0);
2013 return subprog_decl;
2016 /* Set up the framework for generating code for SUBPROG_DECL, a subprogram
2017 body. This routine needs to be invoked before processing the declarations
2018 appearing in the subprogram. */
2020 void
2021 begin_subprog_body (tree subprog_decl)
2023 tree param_decl;
2025 current_function_decl = subprog_decl;
2026 announce_function (subprog_decl);
2028 /* Enter a new binding level and show that all the parameters belong to
2029 this function. */
2030 gnat_pushlevel ();
2031 for (param_decl = DECL_ARGUMENTS (subprog_decl); param_decl;
2032 param_decl = TREE_CHAIN (param_decl))
2033 DECL_CONTEXT (param_decl) = subprog_decl;
2035 make_decl_rtl (subprog_decl);
2037 /* We handle pending sizes via the elaboration of types, so we don't need to
2038 save them. This causes them to be marked as part of the outer function
2039 and then discarded. */
2040 get_pending_sizes ();
2044 /* Helper for the genericization callback. Return a dereference of VAL
2045 if it is of a reference type. */
2047 static tree
2048 convert_from_reference (tree val)
2050 tree value_type, ref;
2052 if (TREE_CODE (TREE_TYPE (val)) != REFERENCE_TYPE)
2053 return val;
2055 value_type = TREE_TYPE (TREE_TYPE (val));
2056 ref = build1 (INDIRECT_REF, value_type, val);
2058 /* See if what we reference is CONST or VOLATILE, which requires
2059 looking into array types to get to the component type. */
2061 while (TREE_CODE (value_type) == ARRAY_TYPE)
2062 value_type = TREE_TYPE (value_type);
2064 TREE_READONLY (ref)
2065 = (TYPE_QUALS (value_type) & TYPE_QUAL_CONST);
2066 TREE_THIS_VOLATILE (ref)
2067 = (TYPE_QUALS (value_type) & TYPE_QUAL_VOLATILE);
2069 TREE_SIDE_EFFECTS (ref)
2070 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
2072 return ref;
2075 /* Helper for the genericization callback. Returns true if T denotes
2076 a RESULT_DECL with DECL_BY_REFERENCE set. */
2078 static inline bool
2079 is_byref_result (tree t)
2081 return (TREE_CODE (t) == RESULT_DECL && DECL_BY_REFERENCE (t));
2085 /* Tree walking callback for gnat_genericize. Currently ...
2087 o Adjust references to the function's DECL_RESULT if it is marked
2088 DECL_BY_REFERENCE and so has had its type turned into a reference
2089 type at the end of the function compilation. */
2091 static tree
2092 gnat_genericize_r (tree *stmt_p, int *walk_subtrees, void *data)
2094 /* This implementation is modeled after what the C++ front-end is
2095 doing, basis of the downstream passes behavior. */
2097 tree stmt = *stmt_p;
2098 struct pointer_set_t *p_set = (struct pointer_set_t*) data;
2100 /* If we have a direct mention of the result decl, dereference. */
2101 if (is_byref_result (stmt))
2103 *stmt_p = convert_from_reference (stmt);
2104 *walk_subtrees = 0;
2105 return NULL;
2108 /* Otherwise, no need to walk the same tree twice. */
2109 if (pointer_set_contains (p_set, stmt))
2111 *walk_subtrees = 0;
2112 return NULL_TREE;
2115 /* If we are taking the address of what now is a reference, just get the
2116 reference value. */
2117 if (TREE_CODE (stmt) == ADDR_EXPR
2118 && is_byref_result (TREE_OPERAND (stmt, 0)))
2120 *stmt_p = convert (TREE_TYPE (stmt), TREE_OPERAND (stmt, 0));
2121 *walk_subtrees = 0;
2124 /* Don't dereference an by-reference RESULT_DECL inside a RETURN_EXPR. */
2125 else if (TREE_CODE (stmt) == RETURN_EXPR
2126 && TREE_OPERAND (stmt, 0)
2127 && is_byref_result (TREE_OPERAND (stmt, 0)))
2128 *walk_subtrees = 0;
2130 /* Don't look inside trees that cannot embed references of interest. */
2131 else if (IS_TYPE_OR_DECL_P (stmt))
2132 *walk_subtrees = 0;
2134 pointer_set_insert (p_set, *stmt_p);
2136 return NULL;
2139 /* Perform lowering of Ada trees to GENERIC. In particular:
2141 o Turn a DECL_BY_REFERENCE RESULT_DECL into a real by-reference decl
2142 and adjust all the references to this decl accordingly. */
2144 static void
2145 gnat_genericize (tree fndecl)
2147 /* Prior to GCC 4, an explicit By_Reference result mechanism for a function
2148 was handled by simply setting TREE_ADDRESSABLE on the result type.
2149 Everything required to actually pass by invisible ref using the target
2150 mechanism (e.g. extra parameter) was handled at RTL expansion time.
2152 This doesn't work with GCC 4 any more for several reasons. First, the
2153 gimplification process might need the creation of temporaries of this
2154 type, and the gimplifier ICEs on such attempts. Second, the middle-end
2155 now relies on a different attribute for such cases (DECL_BY_REFERENCE on
2156 RESULT/PARM_DECLs), and expects the user invisible by-reference-ness to
2157 be explicitly accounted for by the front-end in the function body.
2159 We achieve the complete transformation in two steps:
2161 1/ create_subprog_decl performs early attribute tweaks: it clears
2162 TREE_ADDRESSABLE from the result type and sets DECL_BY_REFERENCE on
2163 the result decl. The former ensures that the bit isn't set in the GCC
2164 tree saved for the function, so prevents ICEs on temporary creation.
2165 The latter we use here to trigger the rest of the processing.
2167 2/ This function performs the type transformation on the result decl
2168 and adjusts all the references to this decl from the function body
2169 accordingly.
2171 Clearing TREE_ADDRESSABLE from the type differs from the C++ front-end
2172 strategy, which escapes the gimplifier temporary creation issues by
2173 creating it's own temporaries using TARGET_EXPR nodes. Our way relies
2174 on simple specific support code in aggregate_value_p to look at the
2175 target function result decl explicitly. */
2177 struct pointer_set_t *p_set;
2178 tree decl_result = DECL_RESULT (fndecl);
2180 if (!DECL_BY_REFERENCE (decl_result))
2181 return;
2183 /* Make the DECL_RESULT explicitly by-reference and adjust all the
2184 occurrences in the function body using the common tree-walking facility.
2185 We want to see every occurrence of the result decl to adjust the
2186 referencing tree, so need to use our own pointer set to control which
2187 trees should be visited again or not. */
2189 p_set = pointer_set_create ();
2191 TREE_TYPE (decl_result) = build_reference_type (TREE_TYPE (decl_result));
2192 TREE_ADDRESSABLE (decl_result) = 0;
2193 relayout_decl (decl_result);
2195 walk_tree (&DECL_SAVED_TREE (fndecl), gnat_genericize_r, p_set, NULL);
2197 pointer_set_destroy (p_set);
2200 /* Finish the definition of the current subprogram and compile it all the way
2201 to assembler language output. BODY is the tree corresponding to
2202 the subprogram. */
2204 void
2205 end_subprog_body (tree body)
2207 tree fndecl = current_function_decl;
2209 /* Mark the BLOCK for this level as being for this function and pop the
2210 level. Since the vars in it are the parameters, clear them. */
2211 BLOCK_VARS (current_binding_level->block) = 0;
2212 BLOCK_SUPERCONTEXT (current_binding_level->block) = fndecl;
2213 DECL_INITIAL (fndecl) = current_binding_level->block;
2214 gnat_poplevel ();
2216 /* Deal with inline. If declared inline or we should default to inline,
2217 set the flag in the decl. */
2218 DECL_INLINE (fndecl)
2219 = DECL_DECLARED_INLINE_P (fndecl) || flag_inline_trees == 2;
2221 /* We handle pending sizes via the elaboration of types, so we don't
2222 need to save them. */
2223 get_pending_sizes ();
2225 /* Mark the RESULT_DECL as being in this subprogram. */
2226 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
2228 DECL_SAVED_TREE (fndecl) = body;
2230 current_function_decl = DECL_CONTEXT (fndecl);
2231 set_cfun (NULL);
2233 /* We cannot track the location of errors past this point. */
2234 error_gnat_node = Empty;
2236 /* If we're only annotating types, don't actually compile this function. */
2237 if (type_annotate_only)
2238 return;
2240 /* Perform the required pre-gimplification transformations on the tree. */
2241 gnat_genericize (fndecl);
2243 /* We do different things for nested and non-nested functions.
2244 ??? This should be in cgraph. */
2245 if (!DECL_CONTEXT (fndecl))
2247 gnat_gimplify_function (fndecl);
2248 cgraph_finalize_function (fndecl, false);
2250 else
2251 /* Register this function with cgraph just far enough to get it
2252 added to our parent's nested function list. */
2253 (void) cgraph_node (fndecl);
2256 /* Convert FNDECL's code to GIMPLE and handle any nested functions. */
2258 static void
2259 gnat_gimplify_function (tree fndecl)
2261 struct cgraph_node *cgn;
2263 dump_function (TDI_original, fndecl);
2264 gimplify_function_tree (fndecl);
2265 dump_function (TDI_generic, fndecl);
2267 /* Convert all nested functions to GIMPLE now. We do things in this order
2268 so that items like VLA sizes are expanded properly in the context of the
2269 correct function. */
2270 cgn = cgraph_node (fndecl);
2271 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
2272 gnat_gimplify_function (cgn->decl);
2276 tree
2277 gnat_builtin_function (tree decl)
2279 gnat_pushdecl (decl, Empty);
2280 return decl;
2283 /* Return an integer type with the number of bits of precision given by
2284 PRECISION. UNSIGNEDP is nonzero if the type is unsigned; otherwise
2285 it is a signed type. */
2287 tree
2288 gnat_type_for_size (unsigned precision, int unsignedp)
2290 tree t;
2291 char type_name[20];
2293 if (precision <= 2 * MAX_BITS_PER_WORD
2294 && signed_and_unsigned_types[precision][unsignedp])
2295 return signed_and_unsigned_types[precision][unsignedp];
2297 if (unsignedp)
2298 t = make_unsigned_type (precision);
2299 else
2300 t = make_signed_type (precision);
2302 if (precision <= 2 * MAX_BITS_PER_WORD)
2303 signed_and_unsigned_types[precision][unsignedp] = t;
2305 if (!TYPE_NAME (t))
2307 sprintf (type_name, "%sSIGNED_%d", unsignedp ? "UN" : "", precision);
2308 TYPE_NAME (t) = get_identifier (type_name);
2311 return t;
2314 /* Likewise for floating-point types. */
2316 static tree
2317 float_type_for_precision (int precision, enum machine_mode mode)
2319 tree t;
2320 char type_name[20];
2322 if (float_types[(int) mode])
2323 return float_types[(int) mode];
2325 float_types[(int) mode] = t = make_node (REAL_TYPE);
2326 TYPE_PRECISION (t) = precision;
2327 layout_type (t);
2329 gcc_assert (TYPE_MODE (t) == mode);
2330 if (!TYPE_NAME (t))
2332 sprintf (type_name, "FLOAT_%d", precision);
2333 TYPE_NAME (t) = get_identifier (type_name);
2336 return t;
2339 /* Return a data type that has machine mode MODE. UNSIGNEDP selects
2340 an unsigned type; otherwise a signed type is returned. */
2342 tree
2343 gnat_type_for_mode (enum machine_mode mode, int unsignedp)
2345 if (mode == BLKmode)
2346 return NULL_TREE;
2347 else if (mode == VOIDmode)
2348 return void_type_node;
2349 else if (COMPLEX_MODE_P (mode))
2350 return NULL_TREE;
2351 else if (SCALAR_FLOAT_MODE_P (mode))
2352 return float_type_for_precision (GET_MODE_PRECISION (mode), mode);
2353 else if (SCALAR_INT_MODE_P (mode))
2354 return gnat_type_for_size (GET_MODE_BITSIZE (mode), unsignedp);
2355 else
2356 return NULL_TREE;
2359 /* Return the unsigned version of a TYPE_NODE, a scalar type. */
2361 tree
2362 gnat_unsigned_type (tree type_node)
2364 tree type = gnat_type_for_size (TYPE_PRECISION (type_node), 1);
2366 if (TREE_CODE (type_node) == INTEGER_TYPE && TYPE_MODULAR_P (type_node))
2368 type = copy_node (type);
2369 TREE_TYPE (type) = type_node;
2371 else if (TREE_TYPE (type_node)
2372 && TREE_CODE (TREE_TYPE (type_node)) == INTEGER_TYPE
2373 && TYPE_MODULAR_P (TREE_TYPE (type_node)))
2375 type = copy_node (type);
2376 TREE_TYPE (type) = TREE_TYPE (type_node);
2379 return type;
2382 /* Return the signed version of a TYPE_NODE, a scalar type. */
2384 tree
2385 gnat_signed_type (tree type_node)
2387 tree type = gnat_type_for_size (TYPE_PRECISION (type_node), 0);
2389 if (TREE_CODE (type_node) == INTEGER_TYPE && TYPE_MODULAR_P (type_node))
2391 type = copy_node (type);
2392 TREE_TYPE (type) = type_node;
2394 else if (TREE_TYPE (type_node)
2395 && TREE_CODE (TREE_TYPE (type_node)) == INTEGER_TYPE
2396 && TYPE_MODULAR_P (TREE_TYPE (type_node)))
2398 type = copy_node (type);
2399 TREE_TYPE (type) = TREE_TYPE (type_node);
2402 return type;
2405 /* Return 1 if the types T1 and T2 are compatible, i.e. if they can be
2406 transparently converted to each other. */
2409 gnat_types_compatible_p (tree t1, tree t2)
2411 enum tree_code code;
2413 /* This is the default criterion. */
2414 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
2415 return 1;
2417 /* We only check structural equivalence here. */
2418 if ((code = TREE_CODE (t1)) != TREE_CODE (t2))
2419 return 0;
2421 /* Array types are also compatible if they are constrained and have
2422 the same component type and the same domain. */
2423 if (code == ARRAY_TYPE
2424 && TREE_TYPE (t1) == TREE_TYPE (t2)
2425 && tree_int_cst_equal (TYPE_MIN_VALUE (TYPE_DOMAIN (t1)),
2426 TYPE_MIN_VALUE (TYPE_DOMAIN (t2)))
2427 && tree_int_cst_equal (TYPE_MAX_VALUE (TYPE_DOMAIN (t1)),
2428 TYPE_MAX_VALUE (TYPE_DOMAIN (t2))))
2429 return 1;
2431 /* Padding record types are also compatible if they pad the same
2432 type and have the same constant size. */
2433 if (code == RECORD_TYPE
2434 && TYPE_IS_PADDING_P (t1) && TYPE_IS_PADDING_P (t2)
2435 && TREE_TYPE (TYPE_FIELDS (t1)) == TREE_TYPE (TYPE_FIELDS (t2))
2436 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2437 return 1;
2439 return 0;
2442 /* EXP is an expression for the size of an object. If this size contains
2443 discriminant references, replace them with the maximum (if MAX_P) or
2444 minimum (if !MAX_P) possible value of the discriminant. */
2446 tree
2447 max_size (tree exp, bool max_p)
2449 enum tree_code code = TREE_CODE (exp);
2450 tree type = TREE_TYPE (exp);
2452 switch (TREE_CODE_CLASS (code))
2454 case tcc_declaration:
2455 case tcc_constant:
2456 return exp;
2458 case tcc_vl_exp:
2459 if (code == CALL_EXPR)
2461 tree *argarray;
2462 int i, n = call_expr_nargs (exp);
2463 gcc_assert (n > 0);
2465 argarray = (tree *) alloca (n * sizeof (tree));
2466 for (i = 0; i < n; i++)
2467 argarray[i] = max_size (CALL_EXPR_ARG (exp, i), max_p);
2468 return build_call_array (type, CALL_EXPR_FN (exp), n, argarray);
2470 break;
2472 case tcc_reference:
2473 /* If this contains a PLACEHOLDER_EXPR, it is the thing we want to
2474 modify. Otherwise, we treat it like a variable. */
2475 if (!CONTAINS_PLACEHOLDER_P (exp))
2476 return exp;
2478 type = TREE_TYPE (TREE_OPERAND (exp, 1));
2479 return
2480 max_size (max_p ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type), true);
2482 case tcc_comparison:
2483 return max_p ? size_one_node : size_zero_node;
2485 case tcc_unary:
2486 case tcc_binary:
2487 case tcc_expression:
2488 switch (TREE_CODE_LENGTH (code))
2490 case 1:
2491 if (code == NON_LVALUE_EXPR)
2492 return max_size (TREE_OPERAND (exp, 0), max_p);
2493 else
2494 return
2495 fold_build1 (code, type,
2496 max_size (TREE_OPERAND (exp, 0),
2497 code == NEGATE_EXPR ? !max_p : max_p));
2499 case 2:
2500 if (code == COMPOUND_EXPR)
2501 return max_size (TREE_OPERAND (exp, 1), max_p);
2503 /* Calculate "(A ? B : C) - D" as "A ? B - D : C - D" which
2504 may provide a tighter bound on max_size. */
2505 if (code == MINUS_EXPR
2506 && TREE_CODE (TREE_OPERAND (exp, 0)) == COND_EXPR)
2508 tree lhs = fold_build2 (MINUS_EXPR, type,
2509 TREE_OPERAND (TREE_OPERAND (exp, 0), 1),
2510 TREE_OPERAND (exp, 1));
2511 tree rhs = fold_build2 (MINUS_EXPR, type,
2512 TREE_OPERAND (TREE_OPERAND (exp, 0), 2),
2513 TREE_OPERAND (exp, 1));
2514 return fold_build2 (max_p ? MAX_EXPR : MIN_EXPR, type,
2515 max_size (lhs, max_p),
2516 max_size (rhs, max_p));
2520 tree lhs = max_size (TREE_OPERAND (exp, 0), max_p);
2521 tree rhs = max_size (TREE_OPERAND (exp, 1),
2522 code == MINUS_EXPR ? !max_p : max_p);
2524 /* Special-case wanting the maximum value of a MIN_EXPR.
2525 In that case, if one side overflows, return the other.
2526 sizetype is signed, but we know sizes are non-negative.
2527 Likewise, handle a MINUS_EXPR or PLUS_EXPR with the LHS
2528 overflowing or the maximum possible value and the RHS
2529 a variable. */
2530 if (max_p
2531 && code == MIN_EXPR
2532 && TREE_CODE (rhs) == INTEGER_CST
2533 && TREE_OVERFLOW (rhs))
2534 return lhs;
2535 else if (max_p
2536 && code == MIN_EXPR
2537 && TREE_CODE (lhs) == INTEGER_CST
2538 && TREE_OVERFLOW (lhs))
2539 return rhs;
2540 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
2541 && ((TREE_CODE (lhs) == INTEGER_CST
2542 && TREE_OVERFLOW (lhs))
2543 || operand_equal_p (lhs, TYPE_MAX_VALUE (type), 0))
2544 && !TREE_CONSTANT (rhs))
2545 return lhs;
2546 else
2547 return fold_build2 (code, type, lhs, rhs);
2550 case 3:
2551 if (code == SAVE_EXPR)
2552 return exp;
2553 else if (code == COND_EXPR)
2554 return fold_build2 (max_p ? MAX_EXPR : MIN_EXPR, type,
2555 max_size (TREE_OPERAND (exp, 1), max_p),
2556 max_size (TREE_OPERAND (exp, 2), max_p));
2559 /* Other tree classes cannot happen. */
2560 default:
2561 break;
2564 gcc_unreachable ();
2567 /* Build a template of type TEMPLATE_TYPE from the array bounds of ARRAY_TYPE.
2568 EXPR is an expression that we can use to locate any PLACEHOLDER_EXPRs.
2569 Return a constructor for the template. */
2571 tree
2572 build_template (tree template_type, tree array_type, tree expr)
2574 tree template_elts = NULL_TREE;
2575 tree bound_list = NULL_TREE;
2576 tree field;
2578 while (TREE_CODE (array_type) == RECORD_TYPE
2579 && (TYPE_IS_PADDING_P (array_type)
2580 || TYPE_JUSTIFIED_MODULAR_P (array_type)))
2581 array_type = TREE_TYPE (TYPE_FIELDS (array_type));
2583 if (TREE_CODE (array_type) == ARRAY_TYPE
2584 || (TREE_CODE (array_type) == INTEGER_TYPE
2585 && TYPE_HAS_ACTUAL_BOUNDS_P (array_type)))
2586 bound_list = TYPE_ACTUAL_BOUNDS (array_type);
2588 /* First make the list for a CONSTRUCTOR for the template. Go down the
2589 field list of the template instead of the type chain because this
2590 array might be an Ada array of arrays and we can't tell where the
2591 nested arrays stop being the underlying object. */
2593 for (field = TYPE_FIELDS (template_type); field;
2594 (bound_list
2595 ? (bound_list = TREE_CHAIN (bound_list))
2596 : (array_type = TREE_TYPE (array_type))),
2597 field = TREE_CHAIN (TREE_CHAIN (field)))
2599 tree bounds, min, max;
2601 /* If we have a bound list, get the bounds from there. Likewise
2602 for an ARRAY_TYPE. Otherwise, if expr is a PARM_DECL with
2603 DECL_BY_COMPONENT_PTR_P, use the bounds of the field in the template.
2604 This will give us a maximum range. */
2605 if (bound_list)
2606 bounds = TREE_VALUE (bound_list);
2607 else if (TREE_CODE (array_type) == ARRAY_TYPE)
2608 bounds = TYPE_INDEX_TYPE (TYPE_DOMAIN (array_type));
2609 else if (expr && TREE_CODE (expr) == PARM_DECL
2610 && DECL_BY_COMPONENT_PTR_P (expr))
2611 bounds = TREE_TYPE (field);
2612 else
2613 gcc_unreachable ();
2615 min = convert (TREE_TYPE (field), TYPE_MIN_VALUE (bounds));
2616 max = convert (TREE_TYPE (TREE_CHAIN (field)), TYPE_MAX_VALUE (bounds));
2618 /* If either MIN or MAX involve a PLACEHOLDER_EXPR, we must
2619 substitute it from OBJECT. */
2620 min = SUBSTITUTE_PLACEHOLDER_IN_EXPR (min, expr);
2621 max = SUBSTITUTE_PLACEHOLDER_IN_EXPR (max, expr);
2623 template_elts = tree_cons (TREE_CHAIN (field), max,
2624 tree_cons (field, min, template_elts));
2627 return gnat_build_constructor (template_type, nreverse (template_elts));
2630 /* Build a VMS descriptor from a Mechanism_Type, which must specify
2631 a descriptor type, and the GCC type of an object. Each FIELD_DECL
2632 in the type contains in its DECL_INITIAL the expression to use when
2633 a constructor is made for the type. GNAT_ENTITY is an entity used
2634 to print out an error message if the mechanism cannot be applied to
2635 an object of that type and also for the name. */
2637 tree
2638 build_vms_descriptor (tree type, Mechanism_Type mech, Entity_Id gnat_entity)
2640 tree record_type = make_node (RECORD_TYPE);
2641 tree pointer32_type;
2642 tree field_list = 0;
2643 int class;
2644 int dtype = 0;
2645 tree inner_type;
2646 int ndim;
2647 int i;
2648 tree *idx_arr;
2649 tree tem;
2651 /* If TYPE is an unconstrained array, use the underlying array type. */
2652 if (TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
2653 type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (type))));
2655 /* If this is an array, compute the number of dimensions in the array,
2656 get the index types, and point to the inner type. */
2657 if (TREE_CODE (type) != ARRAY_TYPE)
2658 ndim = 0;
2659 else
2660 for (ndim = 1, inner_type = type;
2661 TREE_CODE (TREE_TYPE (inner_type)) == ARRAY_TYPE
2662 && TYPE_MULTI_ARRAY_P (TREE_TYPE (inner_type));
2663 ndim++, inner_type = TREE_TYPE (inner_type))
2666 idx_arr = (tree *) alloca (ndim * sizeof (tree));
2668 if (mech != By_Descriptor_NCA
2669 && TREE_CODE (type) == ARRAY_TYPE && TYPE_CONVENTION_FORTRAN_P (type))
2670 for (i = ndim - 1, inner_type = type;
2671 i >= 0;
2672 i--, inner_type = TREE_TYPE (inner_type))
2673 idx_arr[i] = TYPE_DOMAIN (inner_type);
2674 else
2675 for (i = 0, inner_type = type;
2676 i < ndim;
2677 i++, inner_type = TREE_TYPE (inner_type))
2678 idx_arr[i] = TYPE_DOMAIN (inner_type);
2680 /* Now get the DTYPE value. */
2681 switch (TREE_CODE (type))
2683 case INTEGER_TYPE:
2684 case ENUMERAL_TYPE:
2685 if (TYPE_VAX_FLOATING_POINT_P (type))
2686 switch (tree_low_cst (TYPE_DIGITS_VALUE (type), 1))
2688 case 6:
2689 dtype = 10;
2690 break;
2691 case 9:
2692 dtype = 11;
2693 break;
2694 case 15:
2695 dtype = 27;
2696 break;
2698 else
2699 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
2701 case 8:
2702 dtype = TYPE_UNSIGNED (type) ? 2 : 6;
2703 break;
2704 case 16:
2705 dtype = TYPE_UNSIGNED (type) ? 3 : 7;
2706 break;
2707 case 32:
2708 dtype = TYPE_UNSIGNED (type) ? 4 : 8;
2709 break;
2710 case 64:
2711 dtype = TYPE_UNSIGNED (type) ? 5 : 9;
2712 break;
2713 case 128:
2714 dtype = TYPE_UNSIGNED (type) ? 25 : 26;
2715 break;
2717 break;
2719 case REAL_TYPE:
2720 dtype = GET_MODE_BITSIZE (TYPE_MODE (type)) == 32 ? 52 : 53;
2721 break;
2723 case COMPLEX_TYPE:
2724 if (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
2725 && TYPE_VAX_FLOATING_POINT_P (type))
2726 switch (tree_low_cst (TYPE_DIGITS_VALUE (type), 1))
2728 case 6:
2729 dtype = 12;
2730 break;
2731 case 9:
2732 dtype = 13;
2733 break;
2734 case 15:
2735 dtype = 29;
2737 else
2738 dtype = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) == 32 ? 54: 55;
2739 break;
2741 case ARRAY_TYPE:
2742 dtype = 14;
2743 break;
2745 default:
2746 break;
2749 /* Get the CLASS value. */
2750 switch (mech)
2752 case By_Descriptor_A:
2753 class = 4;
2754 break;
2755 case By_Descriptor_NCA:
2756 class = 10;
2757 break;
2758 case By_Descriptor_SB:
2759 class = 15;
2760 break;
2761 case By_Descriptor:
2762 case By_Descriptor_S:
2763 default:
2764 class = 1;
2765 break;
2768 /* Make the type for a descriptor for VMS. The first four fields
2769 are the same for all types. */
2771 field_list
2772 = chainon (field_list,
2773 make_descriptor_field
2774 ("LENGTH", gnat_type_for_size (16, 1), record_type,
2775 size_in_bytes (mech == By_Descriptor_A ? inner_type : type)));
2777 field_list = chainon (field_list,
2778 make_descriptor_field ("DTYPE",
2779 gnat_type_for_size (8, 1),
2780 record_type, size_int (dtype)));
2781 field_list = chainon (field_list,
2782 make_descriptor_field ("CLASS",
2783 gnat_type_for_size (8, 1),
2784 record_type, size_int (class)));
2786 /* Of course this will crash at run-time if the address space is not
2787 within the low 32 bits, but there is nothing else we can do. */
2788 pointer32_type = build_pointer_type_for_mode (type, SImode, false);
2790 field_list
2791 = chainon (field_list,
2792 make_descriptor_field
2793 ("POINTER", pointer32_type, record_type,
2794 build_unary_op (ADDR_EXPR,
2795 pointer32_type,
2796 build0 (PLACEHOLDER_EXPR, type))));
2798 switch (mech)
2800 case By_Descriptor:
2801 case By_Descriptor_S:
2802 break;
2804 case By_Descriptor_SB:
2805 field_list
2806 = chainon (field_list,
2807 make_descriptor_field
2808 ("SB_L1", gnat_type_for_size (32, 1), record_type,
2809 TREE_CODE (type) == ARRAY_TYPE
2810 ? TYPE_MIN_VALUE (TYPE_DOMAIN (type)) : size_zero_node));
2811 field_list
2812 = chainon (field_list,
2813 make_descriptor_field
2814 ("SB_U1", gnat_type_for_size (32, 1), record_type,
2815 TREE_CODE (type) == ARRAY_TYPE
2816 ? TYPE_MAX_VALUE (TYPE_DOMAIN (type)) : size_zero_node));
2817 break;
2819 case By_Descriptor_A:
2820 case By_Descriptor_NCA:
2821 field_list = chainon (field_list,
2822 make_descriptor_field ("SCALE",
2823 gnat_type_for_size (8, 1),
2824 record_type,
2825 size_zero_node));
2827 field_list = chainon (field_list,
2828 make_descriptor_field ("DIGITS",
2829 gnat_type_for_size (8, 1),
2830 record_type,
2831 size_zero_node));
2833 field_list
2834 = chainon (field_list,
2835 make_descriptor_field
2836 ("AFLAGS", gnat_type_for_size (8, 1), record_type,
2837 size_int (mech == By_Descriptor_NCA
2839 /* Set FL_COLUMN, FL_COEFF, and FL_BOUNDS. */
2840 : (TREE_CODE (type) == ARRAY_TYPE
2841 && TYPE_CONVENTION_FORTRAN_P (type)
2842 ? 224 : 192))));
2844 field_list = chainon (field_list,
2845 make_descriptor_field ("DIMCT",
2846 gnat_type_for_size (8, 1),
2847 record_type,
2848 size_int (ndim)));
2850 field_list = chainon (field_list,
2851 make_descriptor_field ("ARSIZE",
2852 gnat_type_for_size (32, 1),
2853 record_type,
2854 size_in_bytes (type)));
2856 /* Now build a pointer to the 0,0,0... element. */
2857 tem = build0 (PLACEHOLDER_EXPR, type);
2858 for (i = 0, inner_type = type; i < ndim;
2859 i++, inner_type = TREE_TYPE (inner_type))
2860 tem = build4 (ARRAY_REF, TREE_TYPE (inner_type), tem,
2861 convert (TYPE_DOMAIN (inner_type), size_zero_node),
2862 NULL_TREE, NULL_TREE);
2864 field_list
2865 = chainon (field_list,
2866 make_descriptor_field
2867 ("A0",
2868 build_pointer_type_for_mode (inner_type, SImode, false),
2869 record_type,
2870 build1 (ADDR_EXPR,
2871 build_pointer_type_for_mode (inner_type, SImode,
2872 false),
2873 tem)));
2875 /* Next come the addressing coefficients. */
2876 tem = size_one_node;
2877 for (i = 0; i < ndim; i++)
2879 char fname[3];
2880 tree idx_length
2881 = size_binop (MULT_EXPR, tem,
2882 size_binop (PLUS_EXPR,
2883 size_binop (MINUS_EXPR,
2884 TYPE_MAX_VALUE (idx_arr[i]),
2885 TYPE_MIN_VALUE (idx_arr[i])),
2886 size_int (1)));
2888 fname[0] = (mech == By_Descriptor_NCA ? 'S' : 'M');
2889 fname[1] = '0' + i, fname[2] = 0;
2890 field_list
2891 = chainon (field_list,
2892 make_descriptor_field (fname,
2893 gnat_type_for_size (32, 1),
2894 record_type, idx_length));
2896 if (mech == By_Descriptor_NCA)
2897 tem = idx_length;
2900 /* Finally here are the bounds. */
2901 for (i = 0; i < ndim; i++)
2903 char fname[3];
2905 fname[0] = 'L', fname[1] = '0' + i, fname[2] = 0;
2906 field_list
2907 = chainon (field_list,
2908 make_descriptor_field
2909 (fname, gnat_type_for_size (32, 1), record_type,
2910 TYPE_MIN_VALUE (idx_arr[i])));
2912 fname[0] = 'U';
2913 field_list
2914 = chainon (field_list,
2915 make_descriptor_field
2916 (fname, gnat_type_for_size (32, 1), record_type,
2917 TYPE_MAX_VALUE (idx_arr[i])));
2919 break;
2921 default:
2922 post_error ("unsupported descriptor type for &", gnat_entity);
2925 finish_record_type (record_type, field_list, 0, true);
2926 create_type_decl (create_concat_name (gnat_entity, "DESC"), record_type,
2927 NULL, true, false, gnat_entity);
2929 return record_type;
2932 /* Utility routine for above code to make a field. */
2934 static tree
2935 make_descriptor_field (const char *name, tree type,
2936 tree rec_type, tree initial)
2938 tree field
2939 = create_field_decl (get_identifier (name), type, rec_type, 0, 0, 0, 0);
2941 DECL_INITIAL (field) = initial;
2942 return field;
2945 /* Convert GNU_EXPR, a pointer to a VMS descriptor, to GNU_TYPE, a regular
2946 pointer or fat pointer type. GNAT_SUBPROG is the subprogram to which
2947 the VMS descriptor is passed. */
2949 static tree
2950 convert_vms_descriptor (tree gnu_type, tree gnu_expr, Entity_Id gnat_subprog)
2952 tree desc_type = TREE_TYPE (TREE_TYPE (gnu_expr));
2953 tree desc = build1 (INDIRECT_REF, desc_type, gnu_expr);
2954 /* The CLASS field is the 3rd field in the descriptor. */
2955 tree class = TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (desc_type)));
2956 /* The POINTER field is the 4th field in the descriptor. */
2957 tree pointer = TREE_CHAIN (class);
2959 /* Retrieve the value of the POINTER field. */
2960 gnu_expr
2961 = build3 (COMPONENT_REF, TREE_TYPE (pointer), desc, pointer, NULL_TREE);
2963 if (POINTER_TYPE_P (gnu_type))
2964 return convert (gnu_type, gnu_expr);
2966 else if (TYPE_FAT_POINTER_P (gnu_type))
2968 tree p_array_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
2969 tree p_bounds_type = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)));
2970 tree template_type = TREE_TYPE (p_bounds_type);
2971 tree min_field = TYPE_FIELDS (template_type);
2972 tree max_field = TREE_CHAIN (TYPE_FIELDS (template_type));
2973 tree template, template_addr, aflags, dimct, t, u;
2974 /* See the head comment of build_vms_descriptor. */
2975 int iclass = TREE_INT_CST_LOW (DECL_INITIAL (class));
2977 /* Convert POINTER to the type of the P_ARRAY field. */
2978 gnu_expr = convert (p_array_type, gnu_expr);
2980 switch (iclass)
2982 case 1: /* Class S */
2983 case 15: /* Class SB */
2984 /* Build {1, LENGTH} template; LENGTH is the 1st field. */
2985 t = TYPE_FIELDS (desc_type);
2986 t = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
2987 t = tree_cons (min_field,
2988 convert (TREE_TYPE (min_field), integer_one_node),
2989 tree_cons (max_field,
2990 convert (TREE_TYPE (max_field), t),
2991 NULL_TREE));
2992 template = gnat_build_constructor (template_type, t);
2993 template_addr = build_unary_op (ADDR_EXPR, NULL_TREE, template);
2995 /* For class S, we are done. */
2996 if (iclass == 1)
2997 break;
2999 /* Test that we really have a SB descriptor, like DEC Ada. */
3000 t = build3 (COMPONENT_REF, TREE_TYPE (class), desc, class, NULL);
3001 u = convert (TREE_TYPE (class), DECL_INITIAL (class));
3002 u = build_binary_op (EQ_EXPR, integer_type_node, t, u);
3003 /* If so, there is already a template in the descriptor and
3004 it is located right after the POINTER field. */
3005 t = TREE_CHAIN (pointer);
3006 template = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
3007 /* Otherwise use the {1, LENGTH} template we build above. */
3008 template_addr = build3 (COND_EXPR, p_bounds_type, u,
3009 build_unary_op (ADDR_EXPR, p_bounds_type,
3010 template),
3011 template_addr);
3012 break;
3014 case 4: /* Class A */
3015 /* The AFLAGS field is the 7th field in the descriptor. */
3016 t = TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (pointer)));
3017 aflags = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
3018 /* The DIMCT field is the 8th field in the descriptor. */
3019 t = TREE_CHAIN (t);
3020 dimct = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
3021 /* Raise CONSTRAINT_ERROR if either more than 1 dimension
3022 or FL_COEFF or FL_BOUNDS not set. */
3023 u = build_int_cst (TREE_TYPE (aflags), 192);
3024 u = build_binary_op (TRUTH_OR_EXPR, integer_type_node,
3025 build_binary_op (NE_EXPR, integer_type_node,
3026 dimct,
3027 convert (TREE_TYPE (dimct),
3028 size_one_node)),
3029 build_binary_op (NE_EXPR, integer_type_node,
3030 build2 (BIT_AND_EXPR,
3031 TREE_TYPE (aflags),
3032 aflags, u),
3033 u));
3034 add_stmt (build3 (COND_EXPR, void_type_node, u,
3035 build_call_raise (CE_Length_Check_Failed, Empty,
3036 N_Raise_Constraint_Error),
3037 NULL_TREE));
3038 /* There is already a template in the descriptor and it is
3039 located at the start of block 3 (12th field). */
3040 t = TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (t))));
3041 template = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
3042 template_addr = build_unary_op (ADDR_EXPR, p_bounds_type, template);
3043 break;
3045 case 10: /* Class NCA */
3046 default:
3047 post_error ("unsupported descriptor type for &", gnat_subprog);
3048 template_addr = integer_zero_node;
3049 break;
3052 /* Build the fat pointer in the form of a constructor. */
3053 t = tree_cons (TYPE_FIELDS (gnu_type), gnu_expr,
3054 tree_cons (TREE_CHAIN (TYPE_FIELDS (gnu_type)),
3055 template_addr, NULL_TREE));
3056 return gnat_build_constructor (gnu_type, t);
3059 else
3060 gcc_unreachable ();
3063 /* Build a stub for the subprogram specified by the GCC tree GNU_SUBPROG
3064 and the GNAT node GNAT_SUBPROG. */
3066 void
3067 build_function_stub (tree gnu_subprog, Entity_Id gnat_subprog)
3069 tree gnu_subprog_type, gnu_subprog_addr, gnu_subprog_call;
3070 tree gnu_stub_param, gnu_param_list, gnu_arg_types, gnu_param;
3071 tree gnu_stub_decl = DECL_FUNCTION_STUB (gnu_subprog);
3072 tree gnu_body;
3074 gnu_subprog_type = TREE_TYPE (gnu_subprog);
3075 gnu_param_list = NULL_TREE;
3077 begin_subprog_body (gnu_stub_decl);
3078 gnat_pushlevel ();
3080 start_stmt_group ();
3082 /* Loop over the parameters of the stub and translate any of them
3083 passed by descriptor into a by reference one. */
3084 for (gnu_stub_param = DECL_ARGUMENTS (gnu_stub_decl),
3085 gnu_arg_types = TYPE_ARG_TYPES (gnu_subprog_type);
3086 gnu_stub_param;
3087 gnu_stub_param = TREE_CHAIN (gnu_stub_param),
3088 gnu_arg_types = TREE_CHAIN (gnu_arg_types))
3090 if (DECL_BY_DESCRIPTOR_P (gnu_stub_param))
3091 gnu_param = convert_vms_descriptor (TREE_VALUE (gnu_arg_types),
3092 gnu_stub_param, gnat_subprog);
3093 else
3094 gnu_param = gnu_stub_param;
3096 gnu_param_list = tree_cons (NULL_TREE, gnu_param, gnu_param_list);
3099 gnu_body = end_stmt_group ();
3101 /* Invoke the internal subprogram. */
3102 gnu_subprog_addr = build1 (ADDR_EXPR, build_pointer_type (gnu_subprog_type),
3103 gnu_subprog);
3104 gnu_subprog_call = build_call_list (TREE_TYPE (gnu_subprog_type),
3105 gnu_subprog_addr,
3106 nreverse (gnu_param_list));
3108 /* Propagate the return value, if any. */
3109 if (VOID_TYPE_P (TREE_TYPE (gnu_subprog_type)))
3110 append_to_statement_list (gnu_subprog_call, &gnu_body);
3111 else
3112 append_to_statement_list (build_return_expr (DECL_RESULT (gnu_stub_decl),
3113 gnu_subprog_call),
3114 &gnu_body);
3116 gnat_poplevel ();
3118 allocate_struct_function (gnu_stub_decl, false);
3119 end_subprog_body (gnu_body);
3122 /* Build a type to be used to represent an aliased object whose nominal
3123 type is an unconstrained array. This consists of a RECORD_TYPE containing
3124 a field of TEMPLATE_TYPE and a field of OBJECT_TYPE, which is an
3125 ARRAY_TYPE. If ARRAY_TYPE is that of the unconstrained array, this
3126 is used to represent an arbitrary unconstrained object. Use NAME
3127 as the name of the record. */
3129 tree
3130 build_unc_object_type (tree template_type, tree object_type, tree name)
3132 tree type = make_node (RECORD_TYPE);
3133 tree template_field = create_field_decl (get_identifier ("BOUNDS"),
3134 template_type, type, 0, 0, 0, 1);
3135 tree array_field = create_field_decl (get_identifier ("ARRAY"), object_type,
3136 type, 0, 0, 0, 1);
3138 TYPE_NAME (type) = name;
3139 TYPE_CONTAINS_TEMPLATE_P (type) = 1;
3140 finish_record_type (type,
3141 chainon (chainon (NULL_TREE, template_field),
3142 array_field),
3143 0, false);
3145 return type;
3148 /* Same, taking a thin or fat pointer type instead of a template type. */
3150 tree
3151 build_unc_object_type_from_ptr (tree thin_fat_ptr_type, tree object_type,
3152 tree name)
3154 tree template_type;
3156 gcc_assert (TYPE_FAT_OR_THIN_POINTER_P (thin_fat_ptr_type));
3158 template_type
3159 = (TYPE_FAT_POINTER_P (thin_fat_ptr_type)
3160 ? TREE_TYPE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (thin_fat_ptr_type))))
3161 : TREE_TYPE (TYPE_FIELDS (TREE_TYPE (thin_fat_ptr_type))));
3162 return build_unc_object_type (template_type, object_type, name);
3165 /* Shift the component offsets within an unconstrained object TYPE to make it
3166 suitable for use as a designated type for thin pointers. */
3168 void
3169 shift_unc_components_for_thin_pointers (tree type)
3171 /* Thin pointer values designate the ARRAY data of an unconstrained object,
3172 allocated past the BOUNDS template. The designated type is adjusted to
3173 have ARRAY at position zero and the template at a negative offset, so
3174 that COMPONENT_REFs on (*thin_ptr) designate the proper location. */
3176 tree bounds_field = TYPE_FIELDS (type);
3177 tree array_field = TREE_CHAIN (TYPE_FIELDS (type));
3179 DECL_FIELD_OFFSET (bounds_field)
3180 = size_binop (MINUS_EXPR, size_zero_node, byte_position (array_field));
3182 DECL_FIELD_OFFSET (array_field) = size_zero_node;
3183 DECL_FIELD_BIT_OFFSET (array_field) = bitsize_zero_node;
3186 /* Update anything previously pointing to OLD_TYPE to point to NEW_TYPE. In
3187 the normal case this is just two adjustments, but we have more to do
3188 if NEW is an UNCONSTRAINED_ARRAY_TYPE. */
3190 void
3191 update_pointer_to (tree old_type, tree new_type)
3193 tree ptr = TYPE_POINTER_TO (old_type);
3194 tree ref = TYPE_REFERENCE_TO (old_type);
3195 tree ptr1, ref1;
3196 tree type;
3198 /* If this is the main variant, process all the other variants first. */
3199 if (TYPE_MAIN_VARIANT (old_type) == old_type)
3200 for (type = TYPE_NEXT_VARIANT (old_type); type;
3201 type = TYPE_NEXT_VARIANT (type))
3202 update_pointer_to (type, new_type);
3204 /* If no pointer or reference, we are done. */
3205 if (!ptr && !ref)
3206 return;
3208 /* Merge the old type qualifiers in the new type.
3210 Each old variant has qualifiers for specific reasons, and the new
3211 designated type as well. Each set of qualifiers represents useful
3212 information grabbed at some point, and merging the two simply unifies
3213 these inputs into the final type description.
3215 Consider for instance a volatile type frozen after an access to constant
3216 type designating it. After the designated type freeze, we get here with a
3217 volatile new_type and a dummy old_type with a readonly variant, created
3218 when the access type was processed. We shall make a volatile and readonly
3219 designated type, because that's what it really is.
3221 We might also get here for a non-dummy old_type variant with different
3222 qualifiers than the new_type ones, for instance in some cases of pointers
3223 to private record type elaboration (see the comments around the call to
3224 this routine from gnat_to_gnu_entity/E_Access_Type). We have to merge the
3225 qualifiers in those cases too, to avoid accidentally discarding the
3226 initial set, and will often end up with old_type == new_type then. */
3227 new_type = build_qualified_type (new_type,
3228 TYPE_QUALS (old_type)
3229 | TYPE_QUALS (new_type));
3231 /* If the new type and the old one are identical, there is nothing to
3232 update. */
3233 if (old_type == new_type)
3234 return;
3236 /* Otherwise, first handle the simple case. */
3237 if (TREE_CODE (new_type) != UNCONSTRAINED_ARRAY_TYPE)
3239 TYPE_POINTER_TO (new_type) = ptr;
3240 TYPE_REFERENCE_TO (new_type) = ref;
3242 for (; ptr; ptr = TYPE_NEXT_PTR_TO (ptr))
3243 for (ptr1 = TYPE_MAIN_VARIANT (ptr); ptr1;
3244 ptr1 = TYPE_NEXT_VARIANT (ptr1))
3245 TREE_TYPE (ptr1) = new_type;
3247 for (; ref; ref = TYPE_NEXT_REF_TO (ref))
3248 for (ref1 = TYPE_MAIN_VARIANT (ref); ref1;
3249 ref1 = TYPE_NEXT_VARIANT (ref1))
3250 TREE_TYPE (ref1) = new_type;
3253 /* Now deal with the unconstrained array case. In this case the "pointer"
3254 is actually a RECORD_TYPE where both fields are pointers to dummy nodes.
3255 Turn them into pointers to the correct types using update_pointer_to. */
3256 else if (TREE_CODE (ptr) != RECORD_TYPE || !TYPE_IS_FAT_POINTER_P (ptr))
3257 gcc_unreachable ();
3259 else
3261 tree new_obj_rec = TYPE_OBJECT_RECORD_TYPE (new_type);
3262 tree array_field = TYPE_FIELDS (ptr);
3263 tree bounds_field = TREE_CHAIN (TYPE_FIELDS (ptr));
3264 tree new_ptr = TYPE_POINTER_TO (new_type);
3265 tree new_ref;
3266 tree var;
3268 /* Make pointers to the dummy template point to the real template. */
3269 update_pointer_to
3270 (TREE_TYPE (TREE_TYPE (bounds_field)),
3271 TREE_TYPE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (new_ptr)))));
3273 /* The references to the template bounds present in the array type
3274 are made through a PLACEHOLDER_EXPR of type new_ptr. Since we
3275 are updating ptr to make it a full replacement for new_ptr as
3276 pointer to new_type, we must rework the PLACEHOLDER_EXPR so as
3277 to make it of type ptr. */
3278 new_ref = build3 (COMPONENT_REF, TREE_TYPE (bounds_field),
3279 build0 (PLACEHOLDER_EXPR, ptr),
3280 bounds_field, NULL_TREE);
3282 /* Create the new array for the new PLACEHOLDER_EXPR and make
3283 pointers to the dummy array point to it.
3285 ??? This is now the only use of substitute_in_type,
3286 which is a very "heavy" routine to do this, so it
3287 should be replaced at some point. */
3288 update_pointer_to
3289 (TREE_TYPE (TREE_TYPE (array_field)),
3290 substitute_in_type (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (new_ptr))),
3291 TREE_CHAIN (TYPE_FIELDS (new_ptr)), new_ref));
3293 /* Make ptr the pointer to new_type. */
3294 TYPE_POINTER_TO (new_type) = TYPE_REFERENCE_TO (new_type)
3295 = TREE_TYPE (new_type) = ptr;
3297 for (var = TYPE_MAIN_VARIANT (ptr); var; var = TYPE_NEXT_VARIANT (var))
3298 SET_TYPE_UNCONSTRAINED_ARRAY (var, new_type);
3300 /* Now handle updating the allocation record, what the thin pointer
3301 points to. Update all pointers from the old record into the new
3302 one, update the type of the array field, and recompute the size. */
3303 update_pointer_to (TYPE_OBJECT_RECORD_TYPE (old_type), new_obj_rec);
3305 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (new_obj_rec)))
3306 = TREE_TYPE (TREE_TYPE (array_field));
3308 /* The size recomputation needs to account for alignment constraints, so
3309 we let layout_type work it out. This will reset the field offsets to
3310 what they would be in a regular record, so we shift them back to what
3311 we want them to be for a thin pointer designated type afterwards. */
3312 DECL_SIZE (TYPE_FIELDS (new_obj_rec)) = 0;
3313 DECL_SIZE (TREE_CHAIN (TYPE_FIELDS (new_obj_rec))) = 0;
3314 TYPE_SIZE (new_obj_rec) = 0;
3315 layout_type (new_obj_rec);
3317 shift_unc_components_for_thin_pointers (new_obj_rec);
3319 /* We are done, at last. */
3320 rest_of_record_type_compilation (ptr);
3324 /* Convert a pointer to a constrained array into a pointer to a fat
3325 pointer. This involves making or finding a template. */
3327 static tree
3328 convert_to_fat_pointer (tree type, tree expr)
3330 tree template_type = TREE_TYPE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (type))));
3331 tree template, template_addr;
3332 tree etype = TREE_TYPE (expr);
3334 /* If EXPR is a constant of zero, we make a fat pointer that has a null
3335 pointer to the template and array. */
3336 if (integer_zerop (expr))
3337 return
3338 gnat_build_constructor
3339 (type,
3340 tree_cons (TYPE_FIELDS (type),
3341 convert (TREE_TYPE (TYPE_FIELDS (type)), expr),
3342 tree_cons (TREE_CHAIN (TYPE_FIELDS (type)),
3343 convert (build_pointer_type (template_type),
3344 expr),
3345 NULL_TREE)));
3347 /* If EXPR is a thin pointer, make the template and data from the record. */
3349 else if (TYPE_THIN_POINTER_P (etype))
3351 tree fields = TYPE_FIELDS (TREE_TYPE (etype));
3353 expr = save_expr (expr);
3354 if (TREE_CODE (expr) == ADDR_EXPR)
3355 expr = TREE_OPERAND (expr, 0);
3356 else
3357 expr = build1 (INDIRECT_REF, TREE_TYPE (etype), expr);
3359 template = build_component_ref (expr, NULL_TREE, fields, false);
3360 expr = build_unary_op (ADDR_EXPR, NULL_TREE,
3361 build_component_ref (expr, NULL_TREE,
3362 TREE_CHAIN (fields), false));
3364 else
3365 /* Otherwise, build the constructor for the template. */
3366 template = build_template (template_type, TREE_TYPE (etype), expr);
3368 template_addr = build_unary_op (ADDR_EXPR, NULL_TREE, template);
3370 /* The result is a CONSTRUCTOR for the fat pointer.
3372 If expr is an argument of a foreign convention subprogram, the type it
3373 points to is directly the component type. In this case, the expression
3374 type may not match the corresponding FIELD_DECL type at this point, so we
3375 call "convert" here to fix that up if necessary. This type consistency is
3376 required, for instance because it ensures that possible later folding of
3377 component_refs against this constructor always yields something of the
3378 same type as the initial reference.
3380 Note that the call to "build_template" above is still fine, because it
3381 will only refer to the provided template_type in this case. */
3382 return
3383 gnat_build_constructor
3384 (type, tree_cons (TYPE_FIELDS (type),
3385 convert (TREE_TYPE (TYPE_FIELDS (type)), expr),
3386 tree_cons (TREE_CHAIN (TYPE_FIELDS (type)),
3387 template_addr, NULL_TREE)));
3390 /* Convert to a thin pointer type, TYPE. The only thing we know how to convert
3391 is something that is a fat pointer, so convert to it first if it EXPR
3392 is not already a fat pointer. */
3394 static tree
3395 convert_to_thin_pointer (tree type, tree expr)
3397 if (!TYPE_FAT_POINTER_P (TREE_TYPE (expr)))
3398 expr
3399 = convert_to_fat_pointer
3400 (TREE_TYPE (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type))), expr);
3402 /* We get the pointer to the data and use a NOP_EXPR to make it the
3403 proper GCC type. */
3404 expr = build_component_ref (expr, NULL_TREE, TYPE_FIELDS (TREE_TYPE (expr)),
3405 false);
3406 expr = build1 (NOP_EXPR, type, expr);
3408 return expr;
3411 /* Create an expression whose value is that of EXPR,
3412 converted to type TYPE. The TREE_TYPE of the value
3413 is always TYPE. This function implements all reasonable
3414 conversions; callers should filter out those that are
3415 not permitted by the language being compiled. */
3417 tree
3418 convert (tree type, tree expr)
3420 enum tree_code code = TREE_CODE (type);
3421 tree etype = TREE_TYPE (expr);
3422 enum tree_code ecode = TREE_CODE (etype);
3424 /* If EXPR is already the right type, we are done. */
3425 if (type == etype)
3426 return expr;
3428 /* If both input and output have padding and are of variable size, do this
3429 as an unchecked conversion. Likewise if one is a mere variant of the
3430 other, so we avoid a pointless unpad/repad sequence. */
3431 else if (code == RECORD_TYPE && ecode == RECORD_TYPE
3432 && TYPE_IS_PADDING_P (type) && TYPE_IS_PADDING_P (etype)
3433 && (!TREE_CONSTANT (TYPE_SIZE (type))
3434 || !TREE_CONSTANT (TYPE_SIZE (etype))
3435 || gnat_types_compatible_p (type, etype)
3436 || TYPE_NAME (TREE_TYPE (TYPE_FIELDS (type)))
3437 == TYPE_NAME (TREE_TYPE (TYPE_FIELDS (etype)))))
3440 /* If the output type has padding, convert to the inner type and
3441 make a constructor to build the record. */
3442 else if (code == RECORD_TYPE && TYPE_IS_PADDING_P (type))
3444 /* If we previously converted from another type and our type is
3445 of variable size, remove the conversion to avoid the need for
3446 variable-size temporaries. Likewise for a conversion between
3447 original and packable version. */
3448 if (TREE_CODE (expr) == VIEW_CONVERT_EXPR
3449 && (!TREE_CONSTANT (TYPE_SIZE (type))
3450 || (ecode == RECORD_TYPE
3451 && TYPE_NAME (etype)
3452 == TYPE_NAME (TREE_TYPE (TREE_OPERAND (expr, 0))))))
3453 expr = TREE_OPERAND (expr, 0);
3455 /* If we are just removing the padding from expr, convert the original
3456 object if we have variable size in order to avoid the need for some
3457 variable-size temporaries. Likewise if the padding is a mere variant
3458 of the other, so we avoid a pointless unpad/repad sequence. */
3459 if (TREE_CODE (expr) == COMPONENT_REF
3460 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE
3461 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (expr, 0)))
3462 && (!TREE_CONSTANT (TYPE_SIZE (type))
3463 || gnat_types_compatible_p (type,
3464 TREE_TYPE (TREE_OPERAND (expr, 0)))
3465 || (ecode == RECORD_TYPE
3466 && TYPE_NAME (etype)
3467 == TYPE_NAME (TREE_TYPE (TYPE_FIELDS (type))))))
3468 return convert (type, TREE_OPERAND (expr, 0));
3470 /* If the result type is a padded type with a self-referentially-sized
3471 field and the expression type is a record, do this as an
3472 unchecked conversion. */
3473 else if (TREE_CODE (etype) == RECORD_TYPE
3474 && CONTAINS_PLACEHOLDER_P (DECL_SIZE (TYPE_FIELDS (type))))
3475 return unchecked_convert (type, expr, false);
3477 else
3478 return
3479 gnat_build_constructor (type,
3480 tree_cons (TYPE_FIELDS (type),
3481 convert (TREE_TYPE
3482 (TYPE_FIELDS (type)),
3483 expr),
3484 NULL_TREE));
3487 /* If the input type has padding, remove it and convert to the output type.
3488 The conditions ordering is arranged to ensure that the output type is not
3489 a padding type here, as it is not clear whether the conversion would
3490 always be correct if this was to happen. */
3491 else if (ecode == RECORD_TYPE && TYPE_IS_PADDING_P (etype))
3493 tree unpadded;
3495 /* If we have just converted to this padded type, just get the
3496 inner expression. */
3497 if (TREE_CODE (expr) == CONSTRUCTOR
3498 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (expr))
3499 && VEC_index (constructor_elt, CONSTRUCTOR_ELTS (expr), 0)->index
3500 == TYPE_FIELDS (etype))
3501 unpadded
3502 = VEC_index (constructor_elt, CONSTRUCTOR_ELTS (expr), 0)->value;
3504 /* Otherwise, build an explicit component reference. */
3505 else
3506 unpadded
3507 = build_component_ref (expr, NULL_TREE, TYPE_FIELDS (etype), false);
3509 return convert (type, unpadded);
3512 /* If the input is a biased type, adjust first. */
3513 if (ecode == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (etype))
3514 return convert (type, fold_build2 (PLUS_EXPR, TREE_TYPE (etype),
3515 fold_convert (TREE_TYPE (etype),
3516 expr),
3517 TYPE_MIN_VALUE (etype)));
3519 /* If the input is a justified modular type, we need to extract the actual
3520 object before converting it to any other type with the exceptions of an
3521 unconstrained array or of a mere type variant. It is useful to avoid the
3522 extraction and conversion in the type variant case because it could end
3523 up replacing a VAR_DECL expr by a constructor and we might be about the
3524 take the address of the result. */
3525 if (ecode == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (etype)
3526 && code != UNCONSTRAINED_ARRAY_TYPE
3527 && TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (etype))
3528 return convert (type, build_component_ref (expr, NULL_TREE,
3529 TYPE_FIELDS (etype), false));
3531 /* If converting to a type that contains a template, convert to the data
3532 type and then build the template. */
3533 if (code == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (type))
3535 tree obj_type = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (type)));
3537 /* If the source already has a template, get a reference to the
3538 associated array only, as we are going to rebuild a template
3539 for the target type anyway. */
3540 expr = maybe_unconstrained_array (expr);
3542 return
3543 gnat_build_constructor
3544 (type,
3545 tree_cons (TYPE_FIELDS (type),
3546 build_template (TREE_TYPE (TYPE_FIELDS (type)),
3547 obj_type, NULL_TREE),
3548 tree_cons (TREE_CHAIN (TYPE_FIELDS (type)),
3549 convert (obj_type, expr), NULL_TREE)));
3552 /* There are some special cases of expressions that we process
3553 specially. */
3554 switch (TREE_CODE (expr))
3556 case ERROR_MARK:
3557 return expr;
3559 case NULL_EXPR:
3560 /* Just set its type here. For TRANSFORM_EXPR, we will do the actual
3561 conversion in gnat_expand_expr. NULL_EXPR does not represent
3562 and actual value, so no conversion is needed. */
3563 expr = copy_node (expr);
3564 TREE_TYPE (expr) = type;
3565 return expr;
3567 case STRING_CST:
3568 /* If we are converting a STRING_CST to another constrained array type,
3569 just make a new one in the proper type. */
3570 if (code == ecode && AGGREGATE_TYPE_P (etype)
3571 && !(TREE_CODE (TYPE_SIZE (etype)) == INTEGER_CST
3572 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
3574 expr = copy_node (expr);
3575 TREE_TYPE (expr) = type;
3576 return expr;
3578 break;
3580 case CONSTRUCTOR:
3581 /* If we are converting a CONSTRUCTOR to a mere variant type, just make
3582 a new one in the proper type. Likewise for a conversion between
3583 original and packable version. */
3584 if (code == ecode
3585 && (gnat_types_compatible_p (type, etype)
3586 || (code == RECORD_TYPE
3587 && TYPE_NAME (type) == TYPE_NAME (etype))))
3589 expr = copy_node (expr);
3590 TREE_TYPE (expr) = type;
3591 return expr;
3593 break;
3595 case UNCONSTRAINED_ARRAY_REF:
3596 /* Convert this to the type of the inner array by getting the address of
3597 the array from the template. */
3598 expr = build_unary_op (INDIRECT_REF, NULL_TREE,
3599 build_component_ref (TREE_OPERAND (expr, 0),
3600 get_identifier ("P_ARRAY"),
3601 NULL_TREE, false));
3602 etype = TREE_TYPE (expr);
3603 ecode = TREE_CODE (etype);
3604 break;
3606 case VIEW_CONVERT_EXPR:
3608 /* GCC 4.x is very sensitive to type consistency overall, and view
3609 conversions thus are very frequent. Even though just "convert"ing
3610 the inner operand to the output type is fine in most cases, it
3611 might expose unexpected input/output type mismatches in special
3612 circumstances so we avoid such recursive calls when we can. */
3613 tree op0 = TREE_OPERAND (expr, 0);
3615 /* If we are converting back to the original type, we can just
3616 lift the input conversion. This is a common occurrence with
3617 switches back-and-forth amongst type variants. */
3618 if (type == TREE_TYPE (op0))
3619 return op0;
3621 /* Otherwise, if we're converting between two aggregate types, we
3622 might be allowed to substitute the VIEW_CONVERT_EXPR target type
3623 in place or to just convert the inner expression. */
3624 if (AGGREGATE_TYPE_P (type) && AGGREGATE_TYPE_P (etype))
3626 /* If we are converting between mere variants, we can just
3627 substitute the VIEW_CONVERT_EXPR in place. */
3628 if (gnat_types_compatible_p (type, etype))
3629 return build1 (VIEW_CONVERT_EXPR, type, op0);
3631 /* Otherwise, we may just bypass the input view conversion unless
3632 one of the types is a fat pointer, which is handled by
3633 specialized code below which relies on exact type matching. */
3634 else if (!TYPE_FAT_POINTER_P (type) && !TYPE_FAT_POINTER_P (etype))
3635 return convert (type, op0);
3638 break;
3640 case INDIRECT_REF:
3641 /* If both types are record types, just convert the pointer and
3642 make a new INDIRECT_REF.
3644 ??? Disable this for now since it causes problems with the
3645 code in build_binary_op for MODIFY_EXPR which wants to
3646 strip off conversions. But that code really is a mess and
3647 we need to do this a much better way some time. */
3648 if (0
3649 && (TREE_CODE (type) == RECORD_TYPE
3650 || TREE_CODE (type) == UNION_TYPE)
3651 && (TREE_CODE (etype) == RECORD_TYPE
3652 || TREE_CODE (etype) == UNION_TYPE)
3653 && !TYPE_FAT_POINTER_P (type) && !TYPE_FAT_POINTER_P (etype))
3654 return build_unary_op (INDIRECT_REF, NULL_TREE,
3655 convert (build_pointer_type (type),
3656 TREE_OPERAND (expr, 0)));
3657 break;
3659 default:
3660 break;
3663 /* Check for converting to a pointer to an unconstrained array. */
3664 if (TYPE_FAT_POINTER_P (type) && !TYPE_FAT_POINTER_P (etype))
3665 return convert_to_fat_pointer (type, expr);
3667 /* If we are converting between two aggregate types that are mere
3668 variants, just make a VIEW_CONVERT_EXPR. */
3669 else if (code == ecode
3670 && AGGREGATE_TYPE_P (type)
3671 && gnat_types_compatible_p (type, etype))
3672 return build1 (VIEW_CONVERT_EXPR, type, expr);
3674 /* In all other cases of related types, make a NOP_EXPR. */
3675 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (etype)
3676 || (code == INTEGER_CST && ecode == INTEGER_CST
3677 && (type == TREE_TYPE (etype) || etype == TREE_TYPE (type))))
3678 return fold_convert (type, expr);
3680 switch (code)
3682 case VOID_TYPE:
3683 return fold_build1 (CONVERT_EXPR, type, expr);
3685 case BOOLEAN_TYPE:
3686 return fold_convert (type, gnat_truthvalue_conversion (expr));
3688 case INTEGER_TYPE:
3689 if (TYPE_HAS_ACTUAL_BOUNDS_P (type)
3690 && (ecode == ARRAY_TYPE || ecode == UNCONSTRAINED_ARRAY_TYPE
3691 || (ecode == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (etype))))
3692 return unchecked_convert (type, expr, false);
3693 else if (TYPE_BIASED_REPRESENTATION_P (type))
3694 return fold_convert (type,
3695 fold_build2 (MINUS_EXPR, TREE_TYPE (type),
3696 convert (TREE_TYPE (type), expr),
3697 TYPE_MIN_VALUE (type)));
3699 /* ... fall through ... */
3701 case ENUMERAL_TYPE:
3702 /* If we are converting an additive expression to an integer type
3703 with lower precision, be wary of the optimization that can be
3704 applied by convert_to_integer. There are 2 problematic cases:
3705 - if the first operand was originally of a biased type,
3706 because we could be recursively called to convert it
3707 to an intermediate type and thus rematerialize the
3708 additive operator endlessly,
3709 - if the expression contains a placeholder, because an
3710 intermediate conversion that changes the sign could
3711 be inserted and thus introduce an artificial overflow
3712 at compile time when the placeholder is substituted. */
3713 if (code == INTEGER_TYPE
3714 && ecode == INTEGER_TYPE
3715 && TYPE_PRECISION (type) < TYPE_PRECISION (etype)
3716 && (TREE_CODE (expr) == PLUS_EXPR || TREE_CODE (expr) == MINUS_EXPR))
3718 tree op0 = get_unwidened (TREE_OPERAND (expr, 0), type);
3720 if ((TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
3721 && TYPE_BIASED_REPRESENTATION_P (TREE_TYPE (op0)))
3722 || CONTAINS_PLACEHOLDER_P (expr))
3723 return build1 (NOP_EXPR, type, expr);
3726 return fold (convert_to_integer (type, expr));
3728 case POINTER_TYPE:
3729 case REFERENCE_TYPE:
3730 /* If converting between two pointers to records denoting
3731 both a template and type, adjust if needed to account
3732 for any differing offsets, since one might be negative. */
3733 if (TYPE_THIN_POINTER_P (etype) && TYPE_THIN_POINTER_P (type))
3735 tree bit_diff
3736 = size_diffop (bit_position (TYPE_FIELDS (TREE_TYPE (etype))),
3737 bit_position (TYPE_FIELDS (TREE_TYPE (type))));
3738 tree byte_diff = size_binop (CEIL_DIV_EXPR, bit_diff,
3739 sbitsize_int (BITS_PER_UNIT));
3741 expr = build1 (NOP_EXPR, type, expr);
3742 TREE_CONSTANT (expr) = TREE_CONSTANT (TREE_OPERAND (expr, 0));
3743 if (integer_zerop (byte_diff))
3744 return expr;
3746 return build_binary_op (POINTER_PLUS_EXPR, type, expr,
3747 fold (convert (sizetype, byte_diff)));
3750 /* If converting to a thin pointer, handle specially. */
3751 if (TYPE_THIN_POINTER_P (type)
3752 && TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)))
3753 return convert_to_thin_pointer (type, expr);
3755 /* If converting fat pointer to normal pointer, get the pointer to the
3756 array and then convert it. */
3757 else if (TYPE_FAT_POINTER_P (etype))
3758 expr = build_component_ref (expr, get_identifier ("P_ARRAY"),
3759 NULL_TREE, false);
3761 return fold (convert_to_pointer (type, expr));
3763 case REAL_TYPE:
3764 return fold (convert_to_real (type, expr));
3766 case RECORD_TYPE:
3767 if (TYPE_JUSTIFIED_MODULAR_P (type) && !AGGREGATE_TYPE_P (etype))
3768 return
3769 gnat_build_constructor
3770 (type, tree_cons (TYPE_FIELDS (type),
3771 convert (TREE_TYPE (TYPE_FIELDS (type)), expr),
3772 NULL_TREE));
3774 /* ... fall through ... */
3776 case ARRAY_TYPE:
3777 /* In these cases, assume the front-end has validated the conversion.
3778 If the conversion is valid, it will be a bit-wise conversion, so
3779 it can be viewed as an unchecked conversion. */
3780 return unchecked_convert (type, expr, false);
3782 case UNION_TYPE:
3783 /* This is a either a conversion between a tagged type and some
3784 subtype, which we have to mark as a UNION_TYPE because of
3785 overlapping fields or a conversion of an Unchecked_Union. */
3786 return unchecked_convert (type, expr, false);
3788 case UNCONSTRAINED_ARRAY_TYPE:
3789 /* If EXPR is a constrained array, take its address, convert it to a
3790 fat pointer, and then dereference it. Likewise if EXPR is a
3791 record containing both a template and a constrained array.
3792 Note that a record representing a justified modular type
3793 always represents a packed constrained array. */
3794 if (ecode == ARRAY_TYPE
3795 || (ecode == INTEGER_TYPE && TYPE_HAS_ACTUAL_BOUNDS_P (etype))
3796 || (ecode == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (etype))
3797 || (ecode == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (etype)))
3798 return
3799 build_unary_op
3800 (INDIRECT_REF, NULL_TREE,
3801 convert_to_fat_pointer (TREE_TYPE (type),
3802 build_unary_op (ADDR_EXPR,
3803 NULL_TREE, expr)));
3805 /* Do something very similar for converting one unconstrained
3806 array to another. */
3807 else if (ecode == UNCONSTRAINED_ARRAY_TYPE)
3808 return
3809 build_unary_op (INDIRECT_REF, NULL_TREE,
3810 convert (TREE_TYPE (type),
3811 build_unary_op (ADDR_EXPR,
3812 NULL_TREE, expr)));
3813 else
3814 gcc_unreachable ();
3816 case COMPLEX_TYPE:
3817 return fold (convert_to_complex (type, expr));
3819 default:
3820 gcc_unreachable ();
3824 /* Remove all conversions that are done in EXP. This includes converting
3825 from a padded type or to a justified modular type. If TRUE_ADDRESS
3826 is true, always return the address of the containing object even if
3827 the address is not bit-aligned. */
3829 tree
3830 remove_conversions (tree exp, bool true_address)
3832 switch (TREE_CODE (exp))
3834 case CONSTRUCTOR:
3835 if (true_address
3836 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
3837 && TYPE_JUSTIFIED_MODULAR_P (TREE_TYPE (exp)))
3838 return
3839 remove_conversions (VEC_index (constructor_elt,
3840 CONSTRUCTOR_ELTS (exp), 0)->value,
3841 true);
3842 break;
3844 case COMPONENT_REF:
3845 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == RECORD_TYPE
3846 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
3847 return remove_conversions (TREE_OPERAND (exp, 0), true_address);
3848 break;
3850 case VIEW_CONVERT_EXPR: case NON_LVALUE_EXPR:
3851 CASE_CONVERT:
3852 return remove_conversions (TREE_OPERAND (exp, 0), true_address);
3854 default:
3855 break;
3858 return exp;
3861 /* If EXP's type is an UNCONSTRAINED_ARRAY_TYPE, return an expression that
3862 refers to the underlying array. If its type has TYPE_CONTAINS_TEMPLATE_P,
3863 likewise return an expression pointing to the underlying array. */
3865 tree
3866 maybe_unconstrained_array (tree exp)
3868 enum tree_code code = TREE_CODE (exp);
3869 tree new;
3871 switch (TREE_CODE (TREE_TYPE (exp)))
3873 case UNCONSTRAINED_ARRAY_TYPE:
3874 if (code == UNCONSTRAINED_ARRAY_REF)
3877 = build_unary_op (INDIRECT_REF, NULL_TREE,
3878 build_component_ref (TREE_OPERAND (exp, 0),
3879 get_identifier ("P_ARRAY"),
3880 NULL_TREE, false));
3881 TREE_READONLY (new) = TREE_STATIC (new) = TREE_READONLY (exp);
3882 return new;
3885 else if (code == NULL_EXPR)
3886 return build1 (NULL_EXPR,
3887 TREE_TYPE (TREE_TYPE (TYPE_FIELDS
3888 (TREE_TYPE (TREE_TYPE (exp))))),
3889 TREE_OPERAND (exp, 0));
3891 case RECORD_TYPE:
3892 /* If this is a padded type, convert to the unpadded type and see if
3893 it contains a template. */
3894 if (TYPE_IS_PADDING_P (TREE_TYPE (exp)))
3896 new = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (exp))), exp);
3897 if (TREE_CODE (TREE_TYPE (new)) == RECORD_TYPE
3898 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (new)))
3899 return
3900 build_component_ref (new, NULL_TREE,
3901 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new))),
3904 else if (TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (exp)))
3905 return
3906 build_component_ref (exp, NULL_TREE,
3907 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (exp))), 0);
3908 break;
3910 default:
3911 break;
3914 return exp;
3917 /* Return an expression that does an unchecked conversion of EXPR to TYPE.
3918 If NOTRUNC_P is true, truncation operations should be suppressed. */
3920 tree
3921 unchecked_convert (tree type, tree expr, bool notrunc_p)
3923 tree etype = TREE_TYPE (expr);
3925 /* If the expression is already the right type, we are done. */
3926 if (etype == type)
3927 return expr;
3929 /* If both types types are integral just do a normal conversion.
3930 Likewise for a conversion to an unconstrained array. */
3931 if ((((INTEGRAL_TYPE_P (type)
3932 && !(TREE_CODE (type) == INTEGER_TYPE
3933 && TYPE_VAX_FLOATING_POINT_P (type)))
3934 || (POINTER_TYPE_P (type) && ! TYPE_THIN_POINTER_P (type))
3935 || (TREE_CODE (type) == RECORD_TYPE
3936 && TYPE_JUSTIFIED_MODULAR_P (type)))
3937 && ((INTEGRAL_TYPE_P (etype)
3938 && !(TREE_CODE (etype) == INTEGER_TYPE
3939 && TYPE_VAX_FLOATING_POINT_P (etype)))
3940 || (POINTER_TYPE_P (etype) && !TYPE_THIN_POINTER_P (etype))
3941 || (TREE_CODE (etype) == RECORD_TYPE
3942 && TYPE_JUSTIFIED_MODULAR_P (etype))))
3943 || TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
3945 tree rtype = type;
3946 bool final_unchecked = false;
3948 if (TREE_CODE (etype) == INTEGER_TYPE
3949 && TYPE_BIASED_REPRESENTATION_P (etype))
3951 tree ntype = copy_type (etype);
3953 TYPE_BIASED_REPRESENTATION_P (ntype) = 0;
3954 TYPE_MAIN_VARIANT (ntype) = ntype;
3955 expr = build1 (NOP_EXPR, ntype, expr);
3958 if (TREE_CODE (type) == INTEGER_TYPE
3959 && TYPE_BIASED_REPRESENTATION_P (type))
3961 rtype = copy_type (type);
3962 TYPE_BIASED_REPRESENTATION_P (rtype) = 0;
3963 TYPE_MAIN_VARIANT (rtype) = rtype;
3966 /* We have another special case: if we are unchecked converting subtype
3967 into a base type, we need to ensure that VRP doesn't propagate range
3968 information since this conversion may be done precisely to validate
3969 that the object is within the range it is supposed to have. */
3970 else if (TREE_CODE (expr) != INTEGER_CST
3971 && TREE_CODE (type) == INTEGER_TYPE && !TREE_TYPE (type)
3972 && ((TREE_CODE (etype) == INTEGER_TYPE && TREE_TYPE (etype))
3973 || TREE_CODE (etype) == ENUMERAL_TYPE
3974 || TREE_CODE (etype) == BOOLEAN_TYPE))
3976 /* The optimization barrier is a VIEW_CONVERT_EXPR node; moreover,
3977 in order not to be deemed an useless type conversion, it must
3978 be from subtype to base type.
3980 ??? This may raise addressability and/or aliasing issues because
3981 VIEW_CONVERT_EXPR gets gimplified as an lvalue, thus causing the
3982 address of its operand to be taken if it is deemed addressable
3983 and not already in GIMPLE form. */
3984 rtype = gnat_type_for_mode (TYPE_MODE (type), TYPE_UNSIGNED (type));
3985 rtype = copy_type (rtype);
3986 TYPE_MAIN_VARIANT (rtype) = rtype;
3987 TREE_TYPE (rtype) = type;
3988 final_unchecked = true;
3991 expr = convert (rtype, expr);
3992 if (type != rtype)
3993 expr = fold_build1 (final_unchecked ? VIEW_CONVERT_EXPR : NOP_EXPR,
3994 type, expr);
3997 /* If we are converting TO an integral type whose precision is not the
3998 same as its size, first unchecked convert to a record that contains
3999 an object of the output type. Then extract the field. */
4000 else if (INTEGRAL_TYPE_P (type) && TYPE_RM_SIZE (type)
4001 && 0 != compare_tree_int (TYPE_RM_SIZE (type),
4002 GET_MODE_BITSIZE (TYPE_MODE (type))))
4004 tree rec_type = make_node (RECORD_TYPE);
4005 tree field = create_field_decl (get_identifier ("OBJ"), type,
4006 rec_type, 1, 0, 0, 0);
4008 TYPE_FIELDS (rec_type) = field;
4009 layout_type (rec_type);
4011 expr = unchecked_convert (rec_type, expr, notrunc_p);
4012 expr = build_component_ref (expr, NULL_TREE, field, 0);
4015 /* Similarly for integral input type whose precision is not equal to its
4016 size. */
4017 else if (INTEGRAL_TYPE_P (etype) && TYPE_RM_SIZE (etype)
4018 && 0 != compare_tree_int (TYPE_RM_SIZE (etype),
4019 GET_MODE_BITSIZE (TYPE_MODE (etype))))
4021 tree rec_type = make_node (RECORD_TYPE);
4022 tree field
4023 = create_field_decl (get_identifier ("OBJ"), etype, rec_type,
4024 1, 0, 0, 0);
4026 TYPE_FIELDS (rec_type) = field;
4027 layout_type (rec_type);
4029 expr = gnat_build_constructor (rec_type, build_tree_list (field, expr));
4030 expr = unchecked_convert (type, expr, notrunc_p);
4033 /* We have a special case when we are converting between two
4034 unconstrained array types. In that case, take the address,
4035 convert the fat pointer types, and dereference. */
4036 else if (TREE_CODE (etype) == UNCONSTRAINED_ARRAY_TYPE
4037 && TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
4038 expr = build_unary_op (INDIRECT_REF, NULL_TREE,
4039 build1 (VIEW_CONVERT_EXPR, TREE_TYPE (type),
4040 build_unary_op (ADDR_EXPR, NULL_TREE,
4041 expr)));
4042 else
4044 expr = maybe_unconstrained_array (expr);
4045 etype = TREE_TYPE (expr);
4046 expr = fold_build1 (VIEW_CONVERT_EXPR, type, expr);
4049 /* If the result is an integral type whose size is not equal to
4050 the size of the underlying machine type, sign- or zero-extend
4051 the result. We need not do this in the case where the input is
4052 an integral type of the same precision and signedness or if the output
4053 is a biased type or if both the input and output are unsigned. */
4054 if (!notrunc_p
4055 && INTEGRAL_TYPE_P (type) && TYPE_RM_SIZE (type)
4056 && !(TREE_CODE (type) == INTEGER_TYPE
4057 && TYPE_BIASED_REPRESENTATION_P (type))
4058 && 0 != compare_tree_int (TYPE_RM_SIZE (type),
4059 GET_MODE_BITSIZE (TYPE_MODE (type)))
4060 && !(INTEGRAL_TYPE_P (etype)
4061 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (etype)
4062 && operand_equal_p (TYPE_RM_SIZE (type),
4063 (TYPE_RM_SIZE (etype) != 0
4064 ? TYPE_RM_SIZE (etype) : TYPE_SIZE (etype)),
4066 && !(TYPE_UNSIGNED (type) && TYPE_UNSIGNED (etype)))
4068 tree base_type = gnat_type_for_mode (TYPE_MODE (type),
4069 TYPE_UNSIGNED (type));
4070 tree shift_expr
4071 = convert (base_type,
4072 size_binop (MINUS_EXPR,
4073 bitsize_int
4074 (GET_MODE_BITSIZE (TYPE_MODE (type))),
4075 TYPE_RM_SIZE (type)));
4076 expr
4077 = convert (type,
4078 build_binary_op (RSHIFT_EXPR, base_type,
4079 build_binary_op (LSHIFT_EXPR, base_type,
4080 convert (base_type, expr),
4081 shift_expr),
4082 shift_expr));
4085 /* An unchecked conversion should never raise Constraint_Error. The code
4086 below assumes that GCC's conversion routines overflow the same way that
4087 the underlying hardware does. This is probably true. In the rare case
4088 when it is false, we can rely on the fact that such conversions are
4089 erroneous anyway. */
4090 if (TREE_CODE (expr) == INTEGER_CST)
4091 TREE_OVERFLOW (expr) = 0;
4093 /* If the sizes of the types differ and this is an VIEW_CONVERT_EXPR,
4094 show no longer constant. */
4095 if (TREE_CODE (expr) == VIEW_CONVERT_EXPR
4096 && !operand_equal_p (TYPE_SIZE_UNIT (type), TYPE_SIZE_UNIT (etype),
4097 OEP_ONLY_CONST))
4098 TREE_CONSTANT (expr) = 0;
4100 return expr;
4103 /* Return the appropriate GCC tree code for the specified GNAT type,
4104 the latter being a record type as predicated by Is_Record_Type. */
4106 enum tree_code
4107 tree_code_for_record_type (Entity_Id gnat_type)
4109 Node_Id component_list
4110 = Component_List (Type_Definition
4111 (Declaration_Node
4112 (Implementation_Base_Type (gnat_type))));
4113 Node_Id component;
4115 /* Make this a UNION_TYPE unless it's either not an Unchecked_Union or
4116 we have a non-discriminant field outside a variant. In either case,
4117 it's a RECORD_TYPE. */
4119 if (!Is_Unchecked_Union (gnat_type))
4120 return RECORD_TYPE;
4122 for (component = First_Non_Pragma (Component_Items (component_list));
4123 Present (component);
4124 component = Next_Non_Pragma (component))
4125 if (Ekind (Defining_Entity (component)) == E_Component)
4126 return RECORD_TYPE;
4128 return UNION_TYPE;
4131 /* Return true if GNU_TYPE is suitable as the type of a non-aliased
4132 component of an aggregate type. */
4134 bool
4135 type_for_nonaliased_component_p (tree gnu_type)
4137 /* If the type is passed by reference, we may have pointers to the
4138 component so it cannot be made non-aliased. */
4139 if (must_pass_by_ref (gnu_type) || default_pass_by_ref (gnu_type))
4140 return false;
4142 /* We used to say that any component of aggregate type is aliased
4143 because the front-end may take 'Reference of it. The front-end
4144 has been enhanced in the meantime so as to use a renaming instead
4145 in most cases, but the back-end can probably take the address of
4146 such a component too so we go for the conservative stance.
4148 For instance, we might need the address of any array type, even
4149 if normally passed by copy, to construct a fat pointer if the
4150 component is used as an actual for an unconstrained formal.
4152 Likewise for record types: even if a specific record subtype is
4153 passed by copy, the parent type might be passed by ref (e.g. if
4154 it's of variable size) and we might take the address of a child
4155 component to pass to a parent formal. We have no way to check
4156 for such conditions here. */
4157 if (AGGREGATE_TYPE_P (gnu_type))
4158 return false;
4160 return true;
4163 /* Perform final processing on global variables. */
4165 void
4166 gnat_write_global_declarations (void)
4168 /* Proceed to optimize and emit assembly.
4169 FIXME: shouldn't be the front end's responsibility to call this. */
4170 cgraph_optimize ();
4172 /* Emit debug info for all global declarations. */
4173 emit_debug_global_declarations (VEC_address (tree, global_decls),
4174 VEC_length (tree, global_decls));
4177 /* ************************************************************************
4178 * * GCC builtins support *
4179 * ************************************************************************ */
4181 /* The general scheme is fairly simple:
4183 For each builtin function/type to be declared, gnat_install_builtins calls
4184 internal facilities which eventually get to gnat_push_decl, which in turn
4185 tracks the so declared builtin function decls in the 'builtin_decls' global
4186 datastructure. When an Intrinsic subprogram declaration is processed, we
4187 search this global datastructure to retrieve the associated BUILT_IN DECL
4188 node. */
4190 /* Search the chain of currently available builtin declarations for a node
4191 corresponding to function NAME (an IDENTIFIER_NODE). Return the first node
4192 found, if any, or NULL_TREE otherwise. */
4193 tree
4194 builtin_decl_for (tree name)
4196 unsigned i;
4197 tree decl;
4199 for (i = 0; VEC_iterate(tree, builtin_decls, i, decl); i++)
4200 if (DECL_NAME (decl) == name)
4201 return decl;
4203 return NULL_TREE;
4206 /* The code below eventually exposes gnat_install_builtins, which declares
4207 the builtin types and functions we might need, either internally or as
4208 user accessible facilities.
4210 ??? This is a first implementation shot, still in rough shape. It is
4211 heavily inspired from the "C" family implementation, with chunks copied
4212 verbatim from there.
4214 Two obvious TODO candidates are
4215 o Use a more efficient name/decl mapping scheme
4216 o Devise a middle-end infrastructure to avoid having to copy
4217 pieces between front-ends. */
4219 /* ----------------------------------------------------------------------- *
4220 * BUILTIN ELEMENTARY TYPES *
4221 * ----------------------------------------------------------------------- */
4223 /* Standard data types to be used in builtin argument declarations. */
4225 enum c_tree_index
4227 CTI_SIGNED_SIZE_TYPE, /* For format checking only. */
4228 CTI_STRING_TYPE,
4229 CTI_CONST_STRING_TYPE,
4231 CTI_MAX
4234 static tree c_global_trees[CTI_MAX];
4236 #define signed_size_type_node c_global_trees[CTI_SIGNED_SIZE_TYPE]
4237 #define string_type_node c_global_trees[CTI_STRING_TYPE]
4238 #define const_string_type_node c_global_trees[CTI_CONST_STRING_TYPE]
4240 /* ??? In addition some attribute handlers, we currently don't support a
4241 (small) number of builtin-types, which in turns inhibits support for a
4242 number of builtin functions. */
4243 #define wint_type_node void_type_node
4244 #define intmax_type_node void_type_node
4245 #define uintmax_type_node void_type_node
4247 /* Build the void_list_node (void_type_node having been created). */
4249 static tree
4250 build_void_list_node (void)
4252 tree t = build_tree_list (NULL_TREE, void_type_node);
4253 return t;
4256 /* Used to help initialize the builtin-types.def table. When a type of
4257 the correct size doesn't exist, use error_mark_node instead of NULL.
4258 The later results in segfaults even when a decl using the type doesn't
4259 get invoked. */
4261 static tree
4262 builtin_type_for_size (int size, bool unsignedp)
4264 tree type = lang_hooks.types.type_for_size (size, unsignedp);
4265 return type ? type : error_mark_node;
4268 /* Build/push the elementary type decls that builtin functions/types
4269 will need. */
4271 static void
4272 install_builtin_elementary_types (void)
4274 signed_size_type_node = size_type_node;
4275 pid_type_node = integer_type_node;
4276 void_list_node = build_void_list_node ();
4278 string_type_node = build_pointer_type (char_type_node);
4279 const_string_type_node
4280 = build_pointer_type (build_qualified_type
4281 (char_type_node, TYPE_QUAL_CONST));
4284 /* ----------------------------------------------------------------------- *
4285 * BUILTIN FUNCTION TYPES *
4286 * ----------------------------------------------------------------------- */
4288 /* Now, builtin function types per se. */
4290 enum c_builtin_type
4292 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
4293 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
4294 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
4295 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
4296 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4297 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4298 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
4299 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
4300 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
4301 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
4302 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
4303 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
4304 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4305 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4306 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
4307 NAME,
4308 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
4309 #include "builtin-types.def"
4310 #undef DEF_PRIMITIVE_TYPE
4311 #undef DEF_FUNCTION_TYPE_0
4312 #undef DEF_FUNCTION_TYPE_1
4313 #undef DEF_FUNCTION_TYPE_2
4314 #undef DEF_FUNCTION_TYPE_3
4315 #undef DEF_FUNCTION_TYPE_4
4316 #undef DEF_FUNCTION_TYPE_5
4317 #undef DEF_FUNCTION_TYPE_6
4318 #undef DEF_FUNCTION_TYPE_7
4319 #undef DEF_FUNCTION_TYPE_VAR_0
4320 #undef DEF_FUNCTION_TYPE_VAR_1
4321 #undef DEF_FUNCTION_TYPE_VAR_2
4322 #undef DEF_FUNCTION_TYPE_VAR_3
4323 #undef DEF_FUNCTION_TYPE_VAR_4
4324 #undef DEF_FUNCTION_TYPE_VAR_5
4325 #undef DEF_POINTER_TYPE
4326 BT_LAST
4329 typedef enum c_builtin_type builtin_type;
4331 /* A temporary array used in communication with def_fn_type. */
4332 static GTY(()) tree builtin_types[(int) BT_LAST + 1];
4334 /* A helper function for install_builtin_types. Build function type
4335 for DEF with return type RET and N arguments. If VAR is true, then the
4336 function should be variadic after those N arguments.
4338 Takes special care not to ICE if any of the types involved are
4339 error_mark_node, which indicates that said type is not in fact available
4340 (see builtin_type_for_size). In which case the function type as a whole
4341 should be error_mark_node. */
4343 static void
4344 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
4346 tree args = NULL, t;
4347 va_list list;
4348 int i;
4350 va_start (list, n);
4351 for (i = 0; i < n; ++i)
4353 builtin_type a = va_arg (list, builtin_type);
4354 t = builtin_types[a];
4355 if (t == error_mark_node)
4356 goto egress;
4357 args = tree_cons (NULL_TREE, t, args);
4359 va_end (list);
4361 args = nreverse (args);
4362 if (!var)
4363 args = chainon (args, void_list_node);
4365 t = builtin_types[ret];
4366 if (t == error_mark_node)
4367 goto egress;
4368 t = build_function_type (t, args);
4370 egress:
4371 builtin_types[def] = t;
4374 /* Build the builtin function types and install them in the builtin_types
4375 array for later use in builtin function decls. */
4377 static void
4378 install_builtin_function_types (void)
4380 tree va_list_ref_type_node;
4381 tree va_list_arg_type_node;
4383 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4385 va_list_arg_type_node = va_list_ref_type_node =
4386 build_pointer_type (TREE_TYPE (va_list_type_node));
4388 else
4390 va_list_arg_type_node = va_list_type_node;
4391 va_list_ref_type_node = build_reference_type (va_list_type_node);
4394 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
4395 builtin_types[ENUM] = VALUE;
4396 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
4397 def_fn_type (ENUM, RETURN, 0, 0);
4398 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
4399 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
4400 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
4401 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
4402 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4403 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
4404 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4405 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
4406 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4407 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4408 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4409 ARG6) \
4410 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
4411 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4412 ARG6, ARG7) \
4413 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4414 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
4415 def_fn_type (ENUM, RETURN, 1, 0);
4416 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
4417 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
4418 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
4419 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
4420 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4421 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
4422 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4423 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
4424 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4425 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4426 #define DEF_POINTER_TYPE(ENUM, TYPE) \
4427 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
4429 #include "builtin-types.def"
4431 #undef DEF_PRIMITIVE_TYPE
4432 #undef DEF_FUNCTION_TYPE_1
4433 #undef DEF_FUNCTION_TYPE_2
4434 #undef DEF_FUNCTION_TYPE_3
4435 #undef DEF_FUNCTION_TYPE_4
4436 #undef DEF_FUNCTION_TYPE_5
4437 #undef DEF_FUNCTION_TYPE_6
4438 #undef DEF_FUNCTION_TYPE_VAR_0
4439 #undef DEF_FUNCTION_TYPE_VAR_1
4440 #undef DEF_FUNCTION_TYPE_VAR_2
4441 #undef DEF_FUNCTION_TYPE_VAR_3
4442 #undef DEF_FUNCTION_TYPE_VAR_4
4443 #undef DEF_FUNCTION_TYPE_VAR_5
4444 #undef DEF_POINTER_TYPE
4445 builtin_types[(int) BT_LAST] = NULL_TREE;
4448 /* ----------------------------------------------------------------------- *
4449 * BUILTIN ATTRIBUTES *
4450 * ----------------------------------------------------------------------- */
4452 enum built_in_attribute
4454 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4455 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4456 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4457 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4458 #include "builtin-attrs.def"
4459 #undef DEF_ATTR_NULL_TREE
4460 #undef DEF_ATTR_INT
4461 #undef DEF_ATTR_IDENT
4462 #undef DEF_ATTR_TREE_LIST
4463 ATTR_LAST
4466 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4468 static void
4469 install_builtin_attributes (void)
4471 /* Fill in the built_in_attributes array. */
4472 #define DEF_ATTR_NULL_TREE(ENUM) \
4473 built_in_attributes[(int) ENUM] = NULL_TREE;
4474 #define DEF_ATTR_INT(ENUM, VALUE) \
4475 built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE);
4476 #define DEF_ATTR_IDENT(ENUM, STRING) \
4477 built_in_attributes[(int) ENUM] = get_identifier (STRING);
4478 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
4479 built_in_attributes[(int) ENUM] \
4480 = tree_cons (built_in_attributes[(int) PURPOSE], \
4481 built_in_attributes[(int) VALUE], \
4482 built_in_attributes[(int) CHAIN]);
4483 #include "builtin-attrs.def"
4484 #undef DEF_ATTR_NULL_TREE
4485 #undef DEF_ATTR_INT
4486 #undef DEF_ATTR_IDENT
4487 #undef DEF_ATTR_TREE_LIST
4490 /* Handle a "const" attribute; arguments as in
4491 struct attribute_spec.handler. */
4493 static tree
4494 handle_const_attribute (tree *node, tree ARG_UNUSED (name),
4495 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
4496 bool *no_add_attrs)
4498 if (TREE_CODE (*node) == FUNCTION_DECL)
4499 TREE_READONLY (*node) = 1;
4500 else
4501 *no_add_attrs = true;
4503 return NULL_TREE;
4506 /* Handle a "nothrow" attribute; arguments as in
4507 struct attribute_spec.handler. */
4509 static tree
4510 handle_nothrow_attribute (tree *node, tree ARG_UNUSED (name),
4511 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
4512 bool *no_add_attrs)
4514 if (TREE_CODE (*node) == FUNCTION_DECL)
4515 TREE_NOTHROW (*node) = 1;
4516 else
4517 *no_add_attrs = true;
4519 return NULL_TREE;
4522 /* Handle a "pure" attribute; arguments as in
4523 struct attribute_spec.handler. */
4525 static tree
4526 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
4527 int ARG_UNUSED (flags), bool *no_add_attrs)
4529 if (TREE_CODE (*node) == FUNCTION_DECL)
4530 DECL_PURE_P (*node) = 1;
4531 /* ??? TODO: Support types. */
4532 else
4534 warning (OPT_Wattributes, "%qE attribute ignored", name);
4535 *no_add_attrs = true;
4538 return NULL_TREE;
4541 /* Handle a "no vops" attribute; arguments as in
4542 struct attribute_spec.handler. */
4544 static tree
4545 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
4546 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
4547 bool *ARG_UNUSED (no_add_attrs))
4549 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
4550 DECL_IS_NOVOPS (*node) = 1;
4551 return NULL_TREE;
4554 /* Helper for nonnull attribute handling; fetch the operand number
4555 from the attribute argument list. */
4557 static bool
4558 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
4560 /* Verify the arg number is a constant. */
4561 if (TREE_CODE (arg_num_expr) != INTEGER_CST
4562 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
4563 return false;
4565 *valp = TREE_INT_CST_LOW (arg_num_expr);
4566 return true;
4569 /* Handle the "nonnull" attribute. */
4570 static tree
4571 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
4572 tree args, int ARG_UNUSED (flags),
4573 bool *no_add_attrs)
4575 tree type = *node;
4576 unsigned HOST_WIDE_INT attr_arg_num;
4578 /* If no arguments are specified, all pointer arguments should be
4579 non-null. Verify a full prototype is given so that the arguments
4580 will have the correct types when we actually check them later. */
4581 if (!args)
4583 if (!TYPE_ARG_TYPES (type))
4585 error ("nonnull attribute without arguments on a non-prototype");
4586 *no_add_attrs = true;
4588 return NULL_TREE;
4591 /* Argument list specified. Verify that each argument number references
4592 a pointer argument. */
4593 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
4595 tree argument;
4596 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
4598 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
4600 error ("nonnull argument has invalid operand number (argument %lu)",
4601 (unsigned long) attr_arg_num);
4602 *no_add_attrs = true;
4603 return NULL_TREE;
4606 argument = TYPE_ARG_TYPES (type);
4607 if (argument)
4609 for (ck_num = 1; ; ck_num++)
4611 if (!argument || ck_num == arg_num)
4612 break;
4613 argument = TREE_CHAIN (argument);
4616 if (!argument
4617 || TREE_CODE (TREE_VALUE (argument)) == VOID_TYPE)
4619 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
4620 (unsigned long) attr_arg_num, (unsigned long) arg_num);
4621 *no_add_attrs = true;
4622 return NULL_TREE;
4625 if (TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE)
4627 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
4628 (unsigned long) attr_arg_num, (unsigned long) arg_num);
4629 *no_add_attrs = true;
4630 return NULL_TREE;
4635 return NULL_TREE;
4638 /* Handle a "sentinel" attribute. */
4640 static tree
4641 handle_sentinel_attribute (tree *node, tree name, tree args,
4642 int ARG_UNUSED (flags), bool *no_add_attrs)
4644 tree params = TYPE_ARG_TYPES (*node);
4646 if (!params)
4648 warning (OPT_Wattributes,
4649 "%qE attribute requires prototypes with named arguments", name);
4650 *no_add_attrs = true;
4652 else
4654 while (TREE_CHAIN (params))
4655 params = TREE_CHAIN (params);
4657 if (VOID_TYPE_P (TREE_VALUE (params)))
4659 warning (OPT_Wattributes,
4660 "%qE attribute only applies to variadic functions", name);
4661 *no_add_attrs = true;
4665 if (args)
4667 tree position = TREE_VALUE (args);
4669 if (TREE_CODE (position) != INTEGER_CST)
4671 warning (0, "requested position is not an integer constant");
4672 *no_add_attrs = true;
4674 else
4676 if (tree_int_cst_lt (position, integer_zero_node))
4678 warning (0, "requested position is less than zero");
4679 *no_add_attrs = true;
4684 return NULL_TREE;
4687 /* Handle a "noreturn" attribute; arguments as in
4688 struct attribute_spec.handler. */
4690 static tree
4691 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
4692 int ARG_UNUSED (flags), bool *no_add_attrs)
4694 tree type = TREE_TYPE (*node);
4696 /* See FIXME comment in c_common_attribute_table. */
4697 if (TREE_CODE (*node) == FUNCTION_DECL)
4698 TREE_THIS_VOLATILE (*node) = 1;
4699 else if (TREE_CODE (type) == POINTER_TYPE
4700 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
4701 TREE_TYPE (*node)
4702 = build_pointer_type
4703 (build_type_variant (TREE_TYPE (type),
4704 TYPE_READONLY (TREE_TYPE (type)), 1));
4705 else
4707 warning (OPT_Wattributes, "%qE attribute ignored", name);
4708 *no_add_attrs = true;
4711 return NULL_TREE;
4714 /* Handle a "malloc" attribute; arguments as in
4715 struct attribute_spec.handler. */
4717 static tree
4718 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
4719 int ARG_UNUSED (flags), bool *no_add_attrs)
4721 if (TREE_CODE (*node) == FUNCTION_DECL
4722 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4723 DECL_IS_MALLOC (*node) = 1;
4724 else
4726 warning (OPT_Wattributes, "%qE attribute ignored", name);
4727 *no_add_attrs = true;
4730 return NULL_TREE;
4733 /* Fake handler for attributes we don't properly support. */
4735 tree
4736 fake_attribute_handler (tree * ARG_UNUSED (node),
4737 tree ARG_UNUSED (name),
4738 tree ARG_UNUSED (args),
4739 int ARG_UNUSED (flags),
4740 bool * ARG_UNUSED (no_add_attrs))
4742 return NULL_TREE;
4745 /* Handle a "type_generic" attribute. */
4747 static tree
4748 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
4749 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
4750 bool * ARG_UNUSED (no_add_attrs))
4752 tree params;
4754 /* Ensure we have a function type. */
4755 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
4757 params = TYPE_ARG_TYPES (*node);
4758 while (params && ! VOID_TYPE_P (TREE_VALUE (params)))
4759 params = TREE_CHAIN (params);
4761 /* Ensure we have a variadic function. */
4762 gcc_assert (!params);
4764 return NULL_TREE;
4767 /* ----------------------------------------------------------------------- *
4768 * BUILTIN FUNCTIONS *
4769 * ----------------------------------------------------------------------- */
4771 /* Worker for DEF_BUILTIN. Possibly define a builtin function with one or two
4772 names. Does not declare a non-__builtin_ function if flag_no_builtin, or
4773 if nonansi_p and flag_no_nonansi_builtin. */
4775 static void
4776 def_builtin_1 (enum built_in_function fncode,
4777 const char *name,
4778 enum built_in_class fnclass,
4779 tree fntype, tree libtype,
4780 bool both_p, bool fallback_p,
4781 bool nonansi_p ATTRIBUTE_UNUSED,
4782 tree fnattrs, bool implicit_p)
4784 tree decl;
4785 const char *libname;
4787 /* Preserve an already installed decl. It most likely was setup in advance
4788 (e.g. as part of the internal builtins) for specific reasons. */
4789 if (built_in_decls[(int) fncode] != NULL_TREE)
4790 return;
4792 gcc_assert ((!both_p && !fallback_p)
4793 || !strncmp (name, "__builtin_",
4794 strlen ("__builtin_")));
4796 libname = name + strlen ("__builtin_");
4797 decl = add_builtin_function (name, fntype, fncode, fnclass,
4798 (fallback_p ? libname : NULL),
4799 fnattrs);
4800 if (both_p)
4801 /* ??? This is normally further controlled by command-line options
4802 like -fno-builtin, but we don't have them for Ada. */
4803 add_builtin_function (libname, libtype, fncode, fnclass,
4804 NULL, fnattrs);
4806 built_in_decls[(int) fncode] = decl;
4807 if (implicit_p)
4808 implicit_built_in_decls[(int) fncode] = decl;
4811 static int flag_isoc94 = 0;
4812 static int flag_isoc99 = 0;
4814 /* Install what the common builtins.def offers. */
4816 static void
4817 install_builtin_functions (void)
4819 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
4820 NONANSI_P, ATTRS, IMPLICIT, COND) \
4821 if (NAME && COND) \
4822 def_builtin_1 (ENUM, NAME, CLASS, \
4823 builtin_types[(int) TYPE], \
4824 builtin_types[(int) LIBTYPE], \
4825 BOTH_P, FALLBACK_P, NONANSI_P, \
4826 built_in_attributes[(int) ATTRS], IMPLICIT);
4827 #include "builtins.def"
4828 #undef DEF_BUILTIN
4831 /* ----------------------------------------------------------------------- *
4832 * BUILTIN FUNCTIONS *
4833 * ----------------------------------------------------------------------- */
4835 /* Install the builtin functions we might need. */
4837 void
4838 gnat_install_builtins (void)
4840 install_builtin_elementary_types ();
4841 install_builtin_function_types ();
4842 install_builtin_attributes ();
4844 /* Install builtins used by generic middle-end pieces first. Some of these
4845 know about internal specificities and control attributes accordingly, for
4846 instance __builtin_alloca vs no-throw and -fstack-check. We will ignore
4847 the generic definition from builtins.def. */
4848 build_common_builtin_nodes ();
4850 /* Now, install the target specific builtins, such as the AltiVec family on
4851 ppc, and the common set as exposed by builtins.def. */
4852 targetm.init_builtins ();
4853 install_builtin_functions ();
4856 #include "gt-ada-utils.h"
4857 #include "gtype-ada.h"