* gcc-interface/decl.c (gnat_to_gnu_entity) <E_Array_Type>: Do not make
[official-gcc.git] / gcc / ada / gcc-interface / utils.c
blob6472a0cda177da002d284ddde62b0ff1a0924207
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * U T I L S *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2011, Free Software Foundation, Inc. *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License along with GCC; see the file COPYING3. If not see *
19 * <http://www.gnu.org/licenses/>. *
20 * *
21 * GNAT was originally developed by the GNAT team at New York University. *
22 * Extensive contributions were provided by Ada Core Technologies Inc. *
23 * *
24 ****************************************************************************/
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "flags.h"
32 #include "toplev.h"
33 #include "diagnostic-core.h"
34 #include "output.h"
35 #include "ggc.h"
36 #include "debug.h"
37 #include "convert.h"
38 #include "target.h"
39 #include "langhooks.h"
40 #include "cgraph.h"
41 #include "tree-dump.h"
42 #include "tree-inline.h"
43 #include "tree-iterator.h"
45 #include "ada.h"
46 #include "types.h"
47 #include "atree.h"
48 #include "elists.h"
49 #include "namet.h"
50 #include "nlists.h"
51 #include "stringt.h"
52 #include "uintp.h"
53 #include "fe.h"
54 #include "sinfo.h"
55 #include "einfo.h"
56 #include "ada-tree.h"
57 #include "gigi.h"
59 #ifndef MAX_BITS_PER_WORD
60 #define MAX_BITS_PER_WORD BITS_PER_WORD
61 #endif
63 /* If nonzero, pretend we are allocating at global level. */
64 int force_global;
66 /* The default alignment of "double" floating-point types, i.e. floating
67 point types whose size is equal to 64 bits, or 0 if this alignment is
68 not specifically capped. */
69 int double_float_alignment;
71 /* The default alignment of "double" or larger scalar types, i.e. scalar
72 types whose size is greater or equal to 64 bits, or 0 if this alignment
73 is not specifically capped. */
74 int double_scalar_alignment;
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 /* Likewise, but with extra info for each of the possible raise reasons. */
83 tree gnat_raise_decls_ext[(int) LAST_REASON_CODE + 1];
85 /* Forward declarations for handlers of attributes. */
86 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
87 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
88 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
89 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
90 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
91 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
92 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
93 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
94 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
95 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
96 static tree handle_vector_size_attribute (tree *, tree, tree, int, bool *);
97 static tree handle_vector_type_attribute (tree *, tree, tree, int, bool *);
99 /* Fake handler for attributes we don't properly support, typically because
100 they'd require dragging a lot of the common-c front-end circuitry. */
101 static tree fake_attribute_handler (tree *, tree, tree, int, bool *);
103 /* Table of machine-independent internal attributes for Ada. We support
104 this minimal set of attributes to accommodate the needs of builtins. */
105 const struct attribute_spec gnat_internal_attribute_table[] =
107 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
108 affects_type_identity } */
109 { "const", 0, 0, true, false, false, handle_const_attribute,
110 false },
111 { "nothrow", 0, 0, true, false, false, handle_nothrow_attribute,
112 false },
113 { "pure", 0, 0, true, false, false, handle_pure_attribute,
114 false },
115 { "no vops", 0, 0, true, false, false, handle_novops_attribute,
116 false },
117 { "nonnull", 0, -1, false, true, true, handle_nonnull_attribute,
118 false },
119 { "sentinel", 0, 1, false, true, true, handle_sentinel_attribute,
120 false },
121 { "noreturn", 0, 0, true, false, false, handle_noreturn_attribute,
122 false },
123 { "leaf", 0, 0, true, false, false, handle_leaf_attribute,
124 false },
125 { "malloc", 0, 0, true, false, false, handle_malloc_attribute,
126 false },
127 { "type generic", 0, 0, false, true, true, handle_type_generic_attribute,
128 false },
130 { "vector_size", 1, 1, false, true, false, handle_vector_size_attribute,
131 false },
132 { "vector_type", 0, 0, false, true, false, handle_vector_type_attribute,
133 false },
134 { "may_alias", 0, 0, false, true, false, NULL, false },
136 /* ??? format and format_arg are heavy and not supported, which actually
137 prevents support for stdio builtins, which we however declare as part
138 of the common builtins.def contents. */
139 { "format", 3, 3, false, true, true, fake_attribute_handler, false },
140 { "format_arg", 1, 1, false, true, true, fake_attribute_handler, false },
142 { NULL, 0, 0, false, false, false, NULL, false }
145 /* Associates a GNAT tree node to a GCC tree node. It is used in
146 `save_gnu_tree', `get_gnu_tree' and `present_gnu_tree'. See documentation
147 of `save_gnu_tree' for more info. */
148 static GTY((length ("max_gnat_nodes"))) tree *associate_gnat_to_gnu;
150 #define GET_GNU_TREE(GNAT_ENTITY) \
151 associate_gnat_to_gnu[(GNAT_ENTITY) - First_Node_Id]
153 #define SET_GNU_TREE(GNAT_ENTITY,VAL) \
154 associate_gnat_to_gnu[(GNAT_ENTITY) - First_Node_Id] = (VAL)
156 #define PRESENT_GNU_TREE(GNAT_ENTITY) \
157 (associate_gnat_to_gnu[(GNAT_ENTITY) - First_Node_Id] != NULL_TREE)
159 /* Associates a GNAT entity to a GCC tree node used as a dummy, if any. */
160 static GTY((length ("max_gnat_nodes"))) tree *dummy_node_table;
162 #define GET_DUMMY_NODE(GNAT_ENTITY) \
163 dummy_node_table[(GNAT_ENTITY) - First_Node_Id]
165 #define SET_DUMMY_NODE(GNAT_ENTITY,VAL) \
166 dummy_node_table[(GNAT_ENTITY) - First_Node_Id] = (VAL)
168 #define PRESENT_DUMMY_NODE(GNAT_ENTITY) \
169 (dummy_node_table[(GNAT_ENTITY) - First_Node_Id] != NULL_TREE)
171 /* This variable keeps a table for types for each precision so that we only
172 allocate each of them once. Signed and unsigned types are kept separate.
174 Note that these types are only used when fold-const requests something
175 special. Perhaps we should NOT share these types; we'll see how it
176 goes later. */
177 static GTY(()) tree signed_and_unsigned_types[2 * MAX_BITS_PER_WORD + 1][2];
179 /* Likewise for float types, but record these by mode. */
180 static GTY(()) tree float_types[NUM_MACHINE_MODES];
182 /* For each binding contour we allocate a binding_level structure to indicate
183 the binding depth. */
185 struct GTY((chain_next ("%h.chain"))) gnat_binding_level {
186 /* The binding level containing this one (the enclosing binding level). */
187 struct gnat_binding_level *chain;
188 /* The BLOCK node for this level. */
189 tree block;
190 /* If nonzero, the setjmp buffer that needs to be updated for any
191 variable-sized definition within this context. */
192 tree jmpbuf_decl;
195 /* The binding level currently in effect. */
196 static GTY(()) struct gnat_binding_level *current_binding_level;
198 /* A chain of gnat_binding_level structures awaiting reuse. */
199 static GTY((deletable)) struct gnat_binding_level *free_binding_level;
201 /* An array of global declarations. */
202 static GTY(()) VEC(tree,gc) *global_decls;
204 /* An array of builtin function declarations. */
205 static GTY(()) VEC(tree,gc) *builtin_decls;
207 /* An array of global renaming pointers. */
208 static GTY(()) VEC(tree,gc) *global_renaming_pointers;
210 /* A chain of unused BLOCK nodes. */
211 static GTY((deletable)) tree free_block_chain;
213 static tree merge_sizes (tree, tree, tree, bool, bool);
214 static tree compute_related_constant (tree, tree);
215 static tree split_plus (tree, tree *);
216 static tree float_type_for_precision (int, enum machine_mode);
217 static tree convert_to_fat_pointer (tree, tree);
218 static tree convert_to_thin_pointer (tree, tree);
219 static bool potential_alignment_gap (tree, tree, tree);
220 static void process_attributes (tree, struct attrib *);
222 /* Initialize the association of GNAT nodes to GCC trees. */
224 void
225 init_gnat_to_gnu (void)
227 associate_gnat_to_gnu = ggc_alloc_cleared_vec_tree (max_gnat_nodes);
230 /* GNAT_ENTITY is a GNAT tree node for an entity. Associate GNU_DECL, a GCC
231 tree node, with GNAT_ENTITY. If GNU_DECL is not a ..._DECL node, abort.
232 If NO_CHECK is true, the latter check is suppressed.
234 If GNU_DECL is zero, reset a previous association. */
236 void
237 save_gnu_tree (Entity_Id gnat_entity, tree gnu_decl, bool no_check)
239 /* Check that GNAT_ENTITY is not already defined and that it is being set
240 to something which is a decl. If that is not the case, this usually
241 means GNAT_ENTITY is defined twice, but occasionally is due to some
242 Gigi problem. */
243 gcc_assert (!(gnu_decl
244 && (PRESENT_GNU_TREE (gnat_entity)
245 || (!no_check && !DECL_P (gnu_decl)))));
247 SET_GNU_TREE (gnat_entity, gnu_decl);
250 /* GNAT_ENTITY is a GNAT tree node for an entity. Return the GCC tree node
251 that was associated with it. If there is no such tree node, abort.
253 In some cases, such as delayed elaboration or expressions that need to
254 be elaborated only once, GNAT_ENTITY is really not an entity. */
256 tree
257 get_gnu_tree (Entity_Id gnat_entity)
259 gcc_assert (PRESENT_GNU_TREE (gnat_entity));
260 return GET_GNU_TREE (gnat_entity);
263 /* Return nonzero if a GCC tree has been associated with GNAT_ENTITY. */
265 bool
266 present_gnu_tree (Entity_Id gnat_entity)
268 return PRESENT_GNU_TREE (gnat_entity);
271 /* Initialize the association of GNAT nodes to GCC trees as dummies. */
273 void
274 init_dummy_type (void)
276 dummy_node_table = ggc_alloc_cleared_vec_tree (max_gnat_nodes);
279 /* Make a dummy type corresponding to GNAT_TYPE. */
281 tree
282 make_dummy_type (Entity_Id gnat_type)
284 Entity_Id gnat_underlying = Gigi_Equivalent_Type (gnat_type);
285 tree gnu_type;
287 /* If there is an equivalent type, get its underlying type. */
288 if (Present (gnat_underlying))
289 gnat_underlying = Underlying_Type (gnat_underlying);
291 /* If there was no equivalent type (can only happen when just annotating
292 types) or underlying type, go back to the original type. */
293 if (No (gnat_underlying))
294 gnat_underlying = gnat_type;
296 /* If it there already a dummy type, use that one. Else make one. */
297 if (PRESENT_DUMMY_NODE (gnat_underlying))
298 return GET_DUMMY_NODE (gnat_underlying);
300 /* If this is a record, make a RECORD_TYPE or UNION_TYPE; else make
301 an ENUMERAL_TYPE. */
302 gnu_type = make_node (Is_Record_Type (gnat_underlying)
303 ? tree_code_for_record_type (gnat_underlying)
304 : ENUMERAL_TYPE);
305 TYPE_NAME (gnu_type) = get_entity_name (gnat_type);
306 TYPE_DUMMY_P (gnu_type) = 1;
307 TYPE_STUB_DECL (gnu_type)
308 = create_type_stub_decl (TYPE_NAME (gnu_type), gnu_type);
309 if (Is_By_Reference_Type (gnat_type))
310 TREE_ADDRESSABLE (gnu_type) = 1;
312 SET_DUMMY_NODE (gnat_underlying, gnu_type);
314 return gnu_type;
317 /* Return the dummy type that was made for GNAT_TYPE, if any. */
319 tree
320 get_dummy_type (Entity_Id gnat_type)
322 return GET_DUMMY_NODE (gnat_type);
325 /* Build dummy fat and thin pointer types whose designated type is specified
326 by GNAT_DESIG_TYPE/GNU_DESIG_TYPE and attach them to the latter. */
328 void
329 build_dummy_unc_pointer_types (Entity_Id gnat_desig_type, tree gnu_desig_type)
331 tree gnu_template_type, gnu_ptr_template, gnu_array_type, gnu_ptr_array;
332 tree gnu_fat_type, fields, gnu_object_type;
334 gnu_template_type = make_node (RECORD_TYPE);
335 TYPE_NAME (gnu_template_type) = create_concat_name (gnat_desig_type, "XUB");
336 TYPE_DUMMY_P (gnu_template_type) = 1;
337 gnu_ptr_template = build_pointer_type (gnu_template_type);
339 gnu_array_type = make_node (ENUMERAL_TYPE);
340 TYPE_NAME (gnu_array_type) = create_concat_name (gnat_desig_type, "XUA");
341 TYPE_DUMMY_P (gnu_array_type) = 1;
342 gnu_ptr_array = build_pointer_type (gnu_array_type);
344 gnu_fat_type = make_node (RECORD_TYPE);
345 /* Build a stub DECL to trigger the special processing for fat pointer types
346 in gnat_pushdecl. */
347 TYPE_NAME (gnu_fat_type)
348 = create_type_stub_decl (create_concat_name (gnat_desig_type, "XUP"),
349 gnu_fat_type);
350 fields = create_field_decl (get_identifier ("P_ARRAY"), gnu_ptr_array,
351 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
352 DECL_CHAIN (fields)
353 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
354 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
355 finish_fat_pointer_type (gnu_fat_type, fields);
356 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_desig_type);
357 /* Suppress debug info until after the type is completed. */
358 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (gnu_fat_type)) = 1;
360 gnu_object_type = make_node (RECORD_TYPE);
361 TYPE_NAME (gnu_object_type) = create_concat_name (gnat_desig_type, "XUT");
362 TYPE_DUMMY_P (gnu_object_type) = 1;
364 TYPE_POINTER_TO (gnu_desig_type) = gnu_fat_type;
365 TYPE_OBJECT_RECORD_TYPE (gnu_desig_type) = gnu_object_type;
368 /* Return nonzero if we are currently in the global binding level. */
371 global_bindings_p (void)
373 return ((force_global || !current_function_decl) ? -1 : 0);
376 /* Enter a new binding level. */
378 void
379 gnat_pushlevel (void)
381 struct gnat_binding_level *newlevel = NULL;
383 /* Reuse a struct for this binding level, if there is one. */
384 if (free_binding_level)
386 newlevel = free_binding_level;
387 free_binding_level = free_binding_level->chain;
389 else
390 newlevel = ggc_alloc_gnat_binding_level ();
392 /* Use a free BLOCK, if any; otherwise, allocate one. */
393 if (free_block_chain)
395 newlevel->block = free_block_chain;
396 free_block_chain = BLOCK_CHAIN (free_block_chain);
397 BLOCK_CHAIN (newlevel->block) = NULL_TREE;
399 else
400 newlevel->block = make_node (BLOCK);
402 /* Point the BLOCK we just made to its parent. */
403 if (current_binding_level)
404 BLOCK_SUPERCONTEXT (newlevel->block) = current_binding_level->block;
406 BLOCK_VARS (newlevel->block) = NULL_TREE;
407 BLOCK_SUBBLOCKS (newlevel->block) = NULL_TREE;
408 TREE_USED (newlevel->block) = 1;
410 /* Add this level to the front of the chain (stack) of active levels. */
411 newlevel->chain = current_binding_level;
412 newlevel->jmpbuf_decl = NULL_TREE;
413 current_binding_level = newlevel;
416 /* Set SUPERCONTEXT of the BLOCK for the current binding level to FNDECL
417 and point FNDECL to this BLOCK. */
419 void
420 set_current_block_context (tree fndecl)
422 BLOCK_SUPERCONTEXT (current_binding_level->block) = fndecl;
423 DECL_INITIAL (fndecl) = current_binding_level->block;
424 set_block_for_group (current_binding_level->block);
427 /* Set the jmpbuf_decl for the current binding level to DECL. */
429 void
430 set_block_jmpbuf_decl (tree decl)
432 current_binding_level->jmpbuf_decl = decl;
435 /* Get the jmpbuf_decl, if any, for the current binding level. */
437 tree
438 get_block_jmpbuf_decl (void)
440 return current_binding_level->jmpbuf_decl;
443 /* Exit a binding level. Set any BLOCK into the current code group. */
445 void
446 gnat_poplevel (void)
448 struct gnat_binding_level *level = current_binding_level;
449 tree block = level->block;
451 BLOCK_VARS (block) = nreverse (BLOCK_VARS (block));
452 BLOCK_SUBBLOCKS (block) = blocks_nreverse (BLOCK_SUBBLOCKS (block));
454 /* If this is a function-level BLOCK don't do anything. Otherwise, if there
455 are no variables free the block and merge its subblocks into those of its
456 parent block. Otherwise, add it to the list of its parent. */
457 if (TREE_CODE (BLOCK_SUPERCONTEXT (block)) == FUNCTION_DECL)
459 else if (BLOCK_VARS (block) == NULL_TREE)
461 BLOCK_SUBBLOCKS (level->chain->block)
462 = chainon (BLOCK_SUBBLOCKS (block),
463 BLOCK_SUBBLOCKS (level->chain->block));
464 BLOCK_CHAIN (block) = free_block_chain;
465 free_block_chain = block;
467 else
469 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (level->chain->block);
470 BLOCK_SUBBLOCKS (level->chain->block) = block;
471 TREE_USED (block) = 1;
472 set_block_for_group (block);
475 /* Free this binding structure. */
476 current_binding_level = level->chain;
477 level->chain = free_binding_level;
478 free_binding_level = level;
481 /* Exit a binding level and discard the associated BLOCK. */
483 void
484 gnat_zaplevel (void)
486 struct gnat_binding_level *level = current_binding_level;
487 tree block = level->block;
489 BLOCK_CHAIN (block) = free_block_chain;
490 free_block_chain = block;
492 /* Free this binding structure. */
493 current_binding_level = level->chain;
494 level->chain = free_binding_level;
495 free_binding_level = level;
498 /* Records a ..._DECL node DECL as belonging to the current lexical scope
499 and uses GNAT_NODE for location information and propagating flags. */
501 void
502 gnat_pushdecl (tree decl, Node_Id gnat_node)
504 /* If this decl is public external or at toplevel, there is no context. */
505 if ((TREE_PUBLIC (decl) && DECL_EXTERNAL (decl)) || global_bindings_p ())
506 DECL_CONTEXT (decl) = 0;
507 else
509 DECL_CONTEXT (decl) = current_function_decl;
511 /* Functions imported in another function are not really nested.
512 For really nested functions mark them initially as needing
513 a static chain for uses of that flag before unnesting;
514 lower_nested_functions will then recompute it. */
515 if (TREE_CODE (decl) == FUNCTION_DECL && !TREE_PUBLIC (decl))
516 DECL_STATIC_CHAIN (decl) = 1;
519 TREE_NO_WARNING (decl) = (gnat_node == Empty || Warnings_Off (gnat_node));
521 /* Set the location of DECL and emit a declaration for it. */
522 if (Present (gnat_node))
523 Sloc_to_locus (Sloc (gnat_node), &DECL_SOURCE_LOCATION (decl));
524 add_decl_expr (decl, gnat_node);
526 /* Put the declaration on the list. The list of declarations is in reverse
527 order. The list will be reversed later. Put global declarations in the
528 globals list and local ones in the current block. But skip TYPE_DECLs
529 for UNCONSTRAINED_ARRAY_TYPE in both cases, as they will cause trouble
530 with the debugger and aren't needed anyway. */
531 if (!(TREE_CODE (decl) == TYPE_DECL
532 && TREE_CODE (TREE_TYPE (decl)) == UNCONSTRAINED_ARRAY_TYPE))
534 if (global_bindings_p ())
536 VEC_safe_push (tree, gc, global_decls, decl);
538 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
539 VEC_safe_push (tree, gc, builtin_decls, decl);
541 else if (!DECL_EXTERNAL (decl))
543 DECL_CHAIN (decl) = BLOCK_VARS (current_binding_level->block);
544 BLOCK_VARS (current_binding_level->block) = decl;
548 /* For the declaration of a type, set its name if it either is not already
549 set or if the previous type name was not derived from a source name.
550 We'd rather have the type named with a real name and all the pointer
551 types to the same object have the same POINTER_TYPE node. Code in the
552 equivalent function of c-decl.c makes a copy of the type node here, but
553 that may cause us trouble with incomplete types. We make an exception
554 for fat pointer types because the compiler automatically builds them
555 for unconstrained array types and the debugger uses them to represent
556 both these and pointers to these. */
557 if (TREE_CODE (decl) == TYPE_DECL && DECL_NAME (decl))
559 tree t = TREE_TYPE (decl);
561 if (!(TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL))
563 /* Array types aren't tagged types in the C sense so we force the
564 type to be associated with its typedef in the DWARF back-end,
565 in order to make sure that the latter is always preserved. */
566 if (!DECL_ARTIFICIAL (decl) && TREE_CODE (t) == ARRAY_TYPE)
568 tree tt = build_distinct_type_copy (t);
569 TYPE_NAME (tt) = DECL_NAME (decl);
570 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (t);
571 DECL_ORIGINAL_TYPE (decl) = tt;
574 else if (TYPE_IS_FAT_POINTER_P (t))
576 /* We need a variant for the placeholder machinery to work. */
577 tree tt = build_variant_type_copy (t);
578 TYPE_NAME (tt) = decl;
579 TREE_USED (tt) = TREE_USED (t);
580 TREE_TYPE (decl) = tt;
581 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
582 DECL_ORIGINAL_TYPE (decl) = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
583 else
584 DECL_ORIGINAL_TYPE (decl) = t;
585 DECL_ARTIFICIAL (decl) = 0;
586 t = NULL_TREE;
588 else if (DECL_ARTIFICIAL (TYPE_NAME (t)) && !DECL_ARTIFICIAL (decl))
590 else
591 t = NULL_TREE;
593 /* Propagate the name to all the anonymous variants. This is needed
594 for the type qualifiers machinery to work properly. */
595 if (t)
596 for (t = TYPE_MAIN_VARIANT (t); t; t = TYPE_NEXT_VARIANT (t))
597 if (!(TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL))
598 TYPE_NAME (t) = decl;
602 /* Record TYPE as a builtin type for Ada. NAME is the name of the type.
603 ARTIFICIAL_P is true if it's a type that was generated by the compiler. */
605 void
606 record_builtin_type (const char *name, tree type, bool artificial_p)
608 tree type_decl = build_decl (input_location,
609 TYPE_DECL, get_identifier (name), type);
610 DECL_ARTIFICIAL (type_decl) = artificial_p;
611 gnat_pushdecl (type_decl, Empty);
613 if (debug_hooks->type_decl)
614 debug_hooks->type_decl (type_decl, false);
617 /* Given a record type RECORD_TYPE and a list of FIELD_DECL nodes FIELD_LIST,
618 finish constructing the record type as a fat pointer type. */
620 void
621 finish_fat_pointer_type (tree record_type, tree field_list)
623 /* Make sure we can put it into a register. */
624 TYPE_ALIGN (record_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
626 /* Show what it really is. */
627 TYPE_FAT_POINTER_P (record_type) = 1;
629 /* Do not emit debug info for it since the types of its fields may still be
630 incomplete at this point. */
631 finish_record_type (record_type, field_list, 0, false);
633 /* Force type_contains_placeholder_p to return true on it. Although the
634 PLACEHOLDER_EXPRs are referenced only indirectly, this isn't a pointer
635 type but the representation of the unconstrained array. */
636 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (record_type) = 2;
639 /* Given a record type RECORD_TYPE and a list of FIELD_DECL nodes FIELD_LIST,
640 finish constructing the record or union type. If REP_LEVEL is zero, this
641 record has no representation clause and so will be entirely laid out here.
642 If REP_LEVEL is one, this record has a representation clause and has been
643 laid out already; only set the sizes and alignment. If REP_LEVEL is two,
644 this record is derived from a parent record and thus inherits its layout;
645 only make a pass on the fields to finalize them. DEBUG_INFO_P is true if
646 we need to write debug information about this type. */
648 void
649 finish_record_type (tree record_type, tree field_list, int rep_level,
650 bool debug_info_p)
652 enum tree_code code = TREE_CODE (record_type);
653 tree name = TYPE_NAME (record_type);
654 tree ada_size = bitsize_zero_node;
655 tree size = bitsize_zero_node;
656 bool had_size = TYPE_SIZE (record_type) != 0;
657 bool had_size_unit = TYPE_SIZE_UNIT (record_type) != 0;
658 bool had_align = TYPE_ALIGN (record_type) != 0;
659 tree field;
661 TYPE_FIELDS (record_type) = field_list;
663 /* Always attach the TYPE_STUB_DECL for a record type. It is required to
664 generate debug info and have a parallel type. */
665 if (name && TREE_CODE (name) == TYPE_DECL)
666 name = DECL_NAME (name);
667 TYPE_STUB_DECL (record_type) = create_type_stub_decl (name, record_type);
669 /* Globally initialize the record first. If this is a rep'ed record,
670 that just means some initializations; otherwise, layout the record. */
671 if (rep_level > 0)
673 TYPE_ALIGN (record_type) = MAX (BITS_PER_UNIT, TYPE_ALIGN (record_type));
675 if (!had_size_unit)
676 TYPE_SIZE_UNIT (record_type) = size_zero_node;
678 if (!had_size)
679 TYPE_SIZE (record_type) = bitsize_zero_node;
681 /* For all-repped records with a size specified, lay the QUAL_UNION_TYPE
682 out just like a UNION_TYPE, since the size will be fixed. */
683 else if (code == QUAL_UNION_TYPE)
684 code = UNION_TYPE;
686 else
688 /* Ensure there isn't a size already set. There can be in an error
689 case where there is a rep clause but all fields have errors and
690 no longer have a position. */
691 TYPE_SIZE (record_type) = 0;
692 layout_type (record_type);
695 /* At this point, the position and size of each field is known. It was
696 either set before entry by a rep clause, or by laying out the type above.
698 We now run a pass over the fields (in reverse order for QUAL_UNION_TYPEs)
699 to compute the Ada size; the GCC size and alignment (for rep'ed records
700 that are not padding types); and the mode (for rep'ed records). We also
701 clear the DECL_BIT_FIELD indication for the cases we know have not been
702 handled yet, and adjust DECL_NONADDRESSABLE_P accordingly. */
704 if (code == QUAL_UNION_TYPE)
705 field_list = nreverse (field_list);
707 for (field = field_list; field; field = DECL_CHAIN (field))
709 tree type = TREE_TYPE (field);
710 tree pos = bit_position (field);
711 tree this_size = DECL_SIZE (field);
712 tree this_ada_size;
714 if ((TREE_CODE (type) == RECORD_TYPE
715 || TREE_CODE (type) == UNION_TYPE
716 || TREE_CODE (type) == QUAL_UNION_TYPE)
717 && !TYPE_FAT_POINTER_P (type)
718 && !TYPE_CONTAINS_TEMPLATE_P (type)
719 && TYPE_ADA_SIZE (type))
720 this_ada_size = TYPE_ADA_SIZE (type);
721 else
722 this_ada_size = this_size;
724 /* Clear DECL_BIT_FIELD for the cases layout_decl does not handle. */
725 if (DECL_BIT_FIELD (field)
726 && operand_equal_p (this_size, TYPE_SIZE (type), 0))
728 unsigned int align = TYPE_ALIGN (type);
730 /* In the general case, type alignment is required. */
731 if (value_factor_p (pos, align))
733 /* The enclosing record type must be sufficiently aligned.
734 Otherwise, if no alignment was specified for it and it
735 has been laid out already, bump its alignment to the
736 desired one if this is compatible with its size. */
737 if (TYPE_ALIGN (record_type) >= align)
739 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), align);
740 DECL_BIT_FIELD (field) = 0;
742 else if (!had_align
743 && rep_level == 0
744 && value_factor_p (TYPE_SIZE (record_type), align))
746 TYPE_ALIGN (record_type) = align;
747 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), align);
748 DECL_BIT_FIELD (field) = 0;
752 /* In the non-strict alignment case, only byte alignment is. */
753 if (!STRICT_ALIGNMENT
754 && DECL_BIT_FIELD (field)
755 && value_factor_p (pos, BITS_PER_UNIT))
756 DECL_BIT_FIELD (field) = 0;
759 /* If we still have DECL_BIT_FIELD set at this point, we know that the
760 field is technically not addressable. Except that it can actually
761 be addressed if it is BLKmode and happens to be properly aligned. */
762 if (DECL_BIT_FIELD (field)
763 && !(DECL_MODE (field) == BLKmode
764 && value_factor_p (pos, BITS_PER_UNIT)))
765 DECL_NONADDRESSABLE_P (field) = 1;
767 /* A type must be as aligned as its most aligned field that is not
768 a bit-field. But this is already enforced by layout_type. */
769 if (rep_level > 0 && !DECL_BIT_FIELD (field))
770 TYPE_ALIGN (record_type)
771 = MAX (TYPE_ALIGN (record_type), DECL_ALIGN (field));
773 switch (code)
775 case UNION_TYPE:
776 ada_size = size_binop (MAX_EXPR, ada_size, this_ada_size);
777 size = size_binop (MAX_EXPR, size, this_size);
778 break;
780 case QUAL_UNION_TYPE:
781 ada_size
782 = fold_build3 (COND_EXPR, bitsizetype, DECL_QUALIFIER (field),
783 this_ada_size, ada_size);
784 size = fold_build3 (COND_EXPR, bitsizetype, DECL_QUALIFIER (field),
785 this_size, size);
786 break;
788 case RECORD_TYPE:
789 /* Since we know here that all fields are sorted in order of
790 increasing bit position, the size of the record is one
791 higher than the ending bit of the last field processed
792 unless we have a rep clause, since in that case we might
793 have a field outside a QUAL_UNION_TYPE that has a higher ending
794 position. So use a MAX in that case. Also, if this field is a
795 QUAL_UNION_TYPE, we need to take into account the previous size in
796 the case of empty variants. */
797 ada_size
798 = merge_sizes (ada_size, pos, this_ada_size,
799 TREE_CODE (type) == QUAL_UNION_TYPE, rep_level > 0);
800 size
801 = merge_sizes (size, pos, this_size,
802 TREE_CODE (type) == QUAL_UNION_TYPE, rep_level > 0);
803 break;
805 default:
806 gcc_unreachable ();
810 if (code == QUAL_UNION_TYPE)
811 nreverse (field_list);
813 if (rep_level < 2)
815 /* If this is a padding record, we never want to make the size smaller
816 than what was specified in it, if any. */
817 if (TYPE_IS_PADDING_P (record_type) && TYPE_SIZE (record_type))
818 size = TYPE_SIZE (record_type);
820 /* Now set any of the values we've just computed that apply. */
821 if (!TYPE_FAT_POINTER_P (record_type)
822 && !TYPE_CONTAINS_TEMPLATE_P (record_type))
823 SET_TYPE_ADA_SIZE (record_type, ada_size);
825 if (rep_level > 0)
827 tree size_unit = had_size_unit
828 ? TYPE_SIZE_UNIT (record_type)
829 : convert (sizetype,
830 size_binop (CEIL_DIV_EXPR, size,
831 bitsize_unit_node));
832 unsigned int align = TYPE_ALIGN (record_type);
834 TYPE_SIZE (record_type) = variable_size (round_up (size, align));
835 TYPE_SIZE_UNIT (record_type)
836 = variable_size (round_up (size_unit, align / BITS_PER_UNIT));
838 compute_record_mode (record_type);
842 if (debug_info_p)
843 rest_of_record_type_compilation (record_type);
846 /* Wrap up compilation of RECORD_TYPE, i.e. output all the debug information
847 associated with it. It need not be invoked directly in most cases since
848 finish_record_type takes care of doing so, but this can be necessary if
849 a parallel type is to be attached to the record type. */
851 void
852 rest_of_record_type_compilation (tree record_type)
854 tree field_list = TYPE_FIELDS (record_type);
855 tree field;
856 enum tree_code code = TREE_CODE (record_type);
857 bool var_size = false;
859 for (field = field_list; field; field = DECL_CHAIN (field))
861 /* We need to make an XVE/XVU record if any field has variable size,
862 whether or not the record does. For example, if we have a union,
863 it may be that all fields, rounded up to the alignment, have the
864 same size, in which case we'll use that size. But the debug
865 output routines (except Dwarf2) won't be able to output the fields,
866 so we need to make the special record. */
867 if (TREE_CODE (DECL_SIZE (field)) != INTEGER_CST
868 /* If a field has a non-constant qualifier, the record will have
869 variable size too. */
870 || (code == QUAL_UNION_TYPE
871 && TREE_CODE (DECL_QUALIFIER (field)) != INTEGER_CST))
873 var_size = true;
874 break;
878 /* If this record is of variable size, rename it so that the
879 debugger knows it is and make a new, parallel, record
880 that tells the debugger how the record is laid out. See
881 exp_dbug.ads. But don't do this for records that are padding
882 since they confuse GDB. */
883 if (var_size && !TYPE_IS_PADDING_P (record_type))
885 tree new_record_type
886 = make_node (TREE_CODE (record_type) == QUAL_UNION_TYPE
887 ? UNION_TYPE : TREE_CODE (record_type));
888 tree orig_name = TYPE_NAME (record_type), new_name;
889 tree last_pos = bitsize_zero_node;
890 tree old_field, prev_old_field = NULL_TREE;
892 if (TREE_CODE (orig_name) == TYPE_DECL)
893 orig_name = DECL_NAME (orig_name);
895 new_name
896 = concat_name (orig_name, TREE_CODE (record_type) == QUAL_UNION_TYPE
897 ? "XVU" : "XVE");
898 TYPE_NAME (new_record_type) = new_name;
899 TYPE_ALIGN (new_record_type) = BIGGEST_ALIGNMENT;
900 TYPE_STUB_DECL (new_record_type)
901 = create_type_stub_decl (new_name, new_record_type);
902 DECL_IGNORED_P (TYPE_STUB_DECL (new_record_type))
903 = DECL_IGNORED_P (TYPE_STUB_DECL (record_type));
904 TYPE_SIZE (new_record_type) = size_int (TYPE_ALIGN (record_type));
905 TYPE_SIZE_UNIT (new_record_type)
906 = size_int (TYPE_ALIGN (record_type) / BITS_PER_UNIT);
908 add_parallel_type (TYPE_STUB_DECL (record_type), new_record_type);
910 /* Now scan all the fields, replacing each field with a new
911 field corresponding to the new encoding. */
912 for (old_field = TYPE_FIELDS (record_type); old_field;
913 old_field = DECL_CHAIN (old_field))
915 tree field_type = TREE_TYPE (old_field);
916 tree field_name = DECL_NAME (old_field);
917 tree new_field;
918 tree curpos = bit_position (old_field);
919 bool var = false;
920 unsigned int align = 0;
921 tree pos;
923 /* See how the position was modified from the last position.
925 There are two basic cases we support: a value was added
926 to the last position or the last position was rounded to
927 a boundary and they something was added. Check for the
928 first case first. If not, see if there is any evidence
929 of rounding. If so, round the last position and try
930 again.
932 If this is a union, the position can be taken as zero. */
934 /* Some computations depend on the shape of the position expression,
935 so strip conversions to make sure it's exposed. */
936 curpos = remove_conversions (curpos, true);
938 if (TREE_CODE (new_record_type) == UNION_TYPE)
939 pos = bitsize_zero_node, align = 0;
940 else
941 pos = compute_related_constant (curpos, last_pos);
943 if (!pos && TREE_CODE (curpos) == MULT_EXPR
944 && host_integerp (TREE_OPERAND (curpos, 1), 1))
946 tree offset = TREE_OPERAND (curpos, 0);
947 align = tree_low_cst (TREE_OPERAND (curpos, 1), 1);
949 /* An offset which is a bitwise AND with a negative power of 2
950 means an alignment corresponding to this power of 2. Note
951 that, as sizetype is sign-extended but nonetheless unsigned,
952 we don't directly use tree_int_cst_sgn. */
953 offset = remove_conversions (offset, true);
954 if (TREE_CODE (offset) == BIT_AND_EXPR
955 && host_integerp (TREE_OPERAND (offset, 1), 0)
956 && TREE_INT_CST_HIGH (TREE_OPERAND (offset, 1)) < 0)
958 unsigned int pow
959 = - tree_low_cst (TREE_OPERAND (offset, 1), 0);
960 if (exact_log2 (pow) > 0)
961 align *= pow;
964 pos = compute_related_constant (curpos,
965 round_up (last_pos, align));
967 else if (!pos && TREE_CODE (curpos) == PLUS_EXPR
968 && TREE_CODE (TREE_OPERAND (curpos, 1)) == INTEGER_CST
969 && TREE_CODE (TREE_OPERAND (curpos, 0)) == MULT_EXPR
970 && host_integerp (TREE_OPERAND
971 (TREE_OPERAND (curpos, 0), 1),
974 align
975 = tree_low_cst
976 (TREE_OPERAND (TREE_OPERAND (curpos, 0), 1), 1);
977 pos = compute_related_constant (curpos,
978 round_up (last_pos, align));
980 else if (potential_alignment_gap (prev_old_field, old_field,
981 pos))
983 align = TYPE_ALIGN (field_type);
984 pos = compute_related_constant (curpos,
985 round_up (last_pos, align));
988 /* If we can't compute a position, set it to zero.
990 ??? We really should abort here, but it's too much work
991 to get this correct for all cases. */
993 if (!pos)
994 pos = bitsize_zero_node;
996 /* See if this type is variable-sized and make a pointer type
997 and indicate the indirection if so. Beware that the debug
998 back-end may adjust the position computed above according
999 to the alignment of the field type, i.e. the pointer type
1000 in this case, if we don't preventively counter that. */
1001 if (TREE_CODE (DECL_SIZE (old_field)) != INTEGER_CST)
1003 field_type = build_pointer_type (field_type);
1004 if (align != 0 && TYPE_ALIGN (field_type) > align)
1006 field_type = copy_node (field_type);
1007 TYPE_ALIGN (field_type) = align;
1009 var = true;
1012 /* Make a new field name, if necessary. */
1013 if (var || align != 0)
1015 char suffix[16];
1017 if (align != 0)
1018 sprintf (suffix, "XV%c%u", var ? 'L' : 'A',
1019 align / BITS_PER_UNIT);
1020 else
1021 strcpy (suffix, "XVL");
1023 field_name = concat_name (field_name, suffix);
1026 new_field
1027 = create_field_decl (field_name, field_type, new_record_type,
1028 DECL_SIZE (old_field), pos, 0, 0);
1029 DECL_CHAIN (new_field) = TYPE_FIELDS (new_record_type);
1030 TYPE_FIELDS (new_record_type) = new_field;
1032 /* If old_field is a QUAL_UNION_TYPE, take its size as being
1033 zero. The only time it's not the last field of the record
1034 is when there are other components at fixed positions after
1035 it (meaning there was a rep clause for every field) and we
1036 want to be able to encode them. */
1037 last_pos = size_binop (PLUS_EXPR, bit_position (old_field),
1038 (TREE_CODE (TREE_TYPE (old_field))
1039 == QUAL_UNION_TYPE)
1040 ? bitsize_zero_node
1041 : DECL_SIZE (old_field));
1042 prev_old_field = old_field;
1045 TYPE_FIELDS (new_record_type)
1046 = nreverse (TYPE_FIELDS (new_record_type));
1048 rest_of_type_decl_compilation (TYPE_STUB_DECL (new_record_type));
1051 rest_of_type_decl_compilation (TYPE_STUB_DECL (record_type));
1054 /* Append PARALLEL_TYPE on the chain of parallel types for decl. */
1056 void
1057 add_parallel_type (tree decl, tree parallel_type)
1059 tree d = decl;
1061 while (DECL_PARALLEL_TYPE (d))
1062 d = TYPE_STUB_DECL (DECL_PARALLEL_TYPE (d));
1064 SET_DECL_PARALLEL_TYPE (d, parallel_type);
1067 /* Utility function of above to merge LAST_SIZE, the previous size of a record
1068 with FIRST_BIT and SIZE that describe a field. SPECIAL is true if this
1069 represents a QUAL_UNION_TYPE in which case we must look for COND_EXPRs and
1070 replace a value of zero with the old size. If HAS_REP is true, we take the
1071 MAX of the end position of this field with LAST_SIZE. In all other cases,
1072 we use FIRST_BIT plus SIZE. Return an expression for the size. */
1074 static tree
1075 merge_sizes (tree last_size, tree first_bit, tree size, bool special,
1076 bool has_rep)
1078 tree type = TREE_TYPE (last_size);
1079 tree new_size;
1081 if (!special || TREE_CODE (size) != COND_EXPR)
1083 new_size = size_binop (PLUS_EXPR, first_bit, size);
1084 if (has_rep)
1085 new_size = size_binop (MAX_EXPR, last_size, new_size);
1088 else
1089 new_size = fold_build3 (COND_EXPR, type, TREE_OPERAND (size, 0),
1090 integer_zerop (TREE_OPERAND (size, 1))
1091 ? last_size : merge_sizes (last_size, first_bit,
1092 TREE_OPERAND (size, 1),
1093 1, has_rep),
1094 integer_zerop (TREE_OPERAND (size, 2))
1095 ? last_size : merge_sizes (last_size, first_bit,
1096 TREE_OPERAND (size, 2),
1097 1, has_rep));
1099 /* We don't need any NON_VALUE_EXPRs and they can confuse us (especially
1100 when fed through substitute_in_expr) into thinking that a constant
1101 size is not constant. */
1102 while (TREE_CODE (new_size) == NON_LVALUE_EXPR)
1103 new_size = TREE_OPERAND (new_size, 0);
1105 return new_size;
1108 /* Utility function of above to see if OP0 and OP1, both of SIZETYPE, are
1109 related by the addition of a constant. Return that constant if so. */
1111 static tree
1112 compute_related_constant (tree op0, tree op1)
1114 tree op0_var, op1_var;
1115 tree op0_con = split_plus (op0, &op0_var);
1116 tree op1_con = split_plus (op1, &op1_var);
1117 tree result = size_binop (MINUS_EXPR, op0_con, op1_con);
1119 if (operand_equal_p (op0_var, op1_var, 0))
1120 return result;
1121 else if (operand_equal_p (op0, size_binop (PLUS_EXPR, op1_var, result), 0))
1122 return result;
1123 else
1124 return 0;
1127 /* Utility function of above to split a tree OP which may be a sum, into a
1128 constant part, which is returned, and a variable part, which is stored
1129 in *PVAR. *PVAR may be bitsize_zero_node. All operations must be of
1130 bitsizetype. */
1132 static tree
1133 split_plus (tree in, tree *pvar)
1135 /* Strip NOPS in order to ease the tree traversal and maximize the
1136 potential for constant or plus/minus discovery. We need to be careful
1137 to always return and set *pvar to bitsizetype trees, but it's worth
1138 the effort. */
1139 STRIP_NOPS (in);
1141 *pvar = convert (bitsizetype, in);
1143 if (TREE_CODE (in) == INTEGER_CST)
1145 *pvar = bitsize_zero_node;
1146 return convert (bitsizetype, in);
1148 else if (TREE_CODE (in) == PLUS_EXPR || TREE_CODE (in) == MINUS_EXPR)
1150 tree lhs_var, rhs_var;
1151 tree lhs_con = split_plus (TREE_OPERAND (in, 0), &lhs_var);
1152 tree rhs_con = split_plus (TREE_OPERAND (in, 1), &rhs_var);
1154 if (lhs_var == TREE_OPERAND (in, 0)
1155 && rhs_var == TREE_OPERAND (in, 1))
1156 return bitsize_zero_node;
1158 *pvar = size_binop (TREE_CODE (in), lhs_var, rhs_var);
1159 return size_binop (TREE_CODE (in), lhs_con, rhs_con);
1161 else
1162 return bitsize_zero_node;
1165 /* Return a FUNCTION_TYPE node. RETURN_TYPE is the type returned by the
1166 subprogram. If it is VOID_TYPE, then we are dealing with a procedure,
1167 otherwise we are dealing with a function. PARAM_DECL_LIST is a list of
1168 PARM_DECL nodes that are the subprogram parameters. CICO_LIST is the
1169 copy-in/copy-out list to be stored into the TYPE_CICO_LIST field.
1170 RETURN_UNCONSTRAINED_P is true if the function returns an unconstrained
1171 object. RETURN_BY_DIRECT_REF_P is true if the function returns by direct
1172 reference. RETURN_BY_INVISI_REF_P is true if the function returns by
1173 invisible reference. */
1175 tree
1176 create_subprog_type (tree return_type, tree param_decl_list, tree cico_list,
1177 bool return_unconstrained_p, bool return_by_direct_ref_p,
1178 bool return_by_invisi_ref_p)
1180 /* A chain of TREE_LIST nodes whose TREE_VALUEs are the data type nodes of
1181 the subprogram formal parameters. This list is generated by traversing
1182 the input list of PARM_DECL nodes. */
1183 tree param_type_list = NULL_TREE;
1184 tree t, type;
1186 for (t = param_decl_list; t; t = DECL_CHAIN (t))
1187 param_type_list = tree_cons (NULL_TREE, TREE_TYPE (t), param_type_list);
1189 /* The list of the function parameter types has to be terminated by the void
1190 type to signal to the back-end that we are not dealing with a variable
1191 parameter subprogram, but that it has a fixed number of parameters. */
1192 param_type_list = tree_cons (NULL_TREE, void_type_node, param_type_list);
1194 /* The list of argument types has been created in reverse so reverse it. */
1195 param_type_list = nreverse (param_type_list);
1197 type = build_function_type (return_type, param_type_list);
1199 /* TYPE may have been shared since GCC hashes types. If it has a different
1200 CICO_LIST, make a copy. Likewise for the various flags. */
1201 if (!fntype_same_flags_p (type, cico_list, return_unconstrained_p,
1202 return_by_direct_ref_p, return_by_invisi_ref_p))
1204 type = copy_type (type);
1205 TYPE_CI_CO_LIST (type) = cico_list;
1206 TYPE_RETURN_UNCONSTRAINED_P (type) = return_unconstrained_p;
1207 TYPE_RETURN_BY_DIRECT_REF_P (type) = return_by_direct_ref_p;
1208 TREE_ADDRESSABLE (type) = return_by_invisi_ref_p;
1211 return type;
1214 /* Return a copy of TYPE but safe to modify in any way. */
1216 tree
1217 copy_type (tree type)
1219 tree new_type = copy_node (type);
1221 /* Unshare the language-specific data. */
1222 if (TYPE_LANG_SPECIFIC (type))
1224 TYPE_LANG_SPECIFIC (new_type) = NULL;
1225 SET_TYPE_LANG_SPECIFIC (new_type, GET_TYPE_LANG_SPECIFIC (type));
1228 /* And the contents of the language-specific slot if needed. */
1229 if ((INTEGRAL_TYPE_P (type) || TREE_CODE (type) == REAL_TYPE)
1230 && TYPE_RM_VALUES (type))
1232 TYPE_RM_VALUES (new_type) = NULL_TREE;
1233 SET_TYPE_RM_SIZE (new_type, TYPE_RM_SIZE (type));
1234 SET_TYPE_RM_MIN_VALUE (new_type, TYPE_RM_MIN_VALUE (type));
1235 SET_TYPE_RM_MAX_VALUE (new_type, TYPE_RM_MAX_VALUE (type));
1238 /* copy_node clears this field instead of copying it, because it is
1239 aliased with TREE_CHAIN. */
1240 TYPE_STUB_DECL (new_type) = TYPE_STUB_DECL (type);
1242 TYPE_POINTER_TO (new_type) = 0;
1243 TYPE_REFERENCE_TO (new_type) = 0;
1244 TYPE_MAIN_VARIANT (new_type) = new_type;
1245 TYPE_NEXT_VARIANT (new_type) = 0;
1247 return new_type;
1250 /* Return a subtype of sizetype with range MIN to MAX and whose
1251 TYPE_INDEX_TYPE is INDEX. GNAT_NODE is used for the position
1252 of the associated TYPE_DECL. */
1254 tree
1255 create_index_type (tree min, tree max, tree index, Node_Id gnat_node)
1257 /* First build a type for the desired range. */
1258 tree type = build_nonshared_range_type (sizetype, min, max);
1260 /* Then set the index type. */
1261 SET_TYPE_INDEX_TYPE (type, index);
1262 create_type_decl (NULL_TREE, type, NULL, true, false, gnat_node);
1264 return type;
1267 /* Return a subtype of TYPE with range MIN to MAX. If TYPE is NULL,
1268 sizetype is used. */
1270 tree
1271 create_range_type (tree type, tree min, tree max)
1273 tree range_type;
1275 if (type == NULL_TREE)
1276 type = sizetype;
1278 /* First build a type with the base range. */
1279 range_type = build_nonshared_range_type (type, TYPE_MIN_VALUE (type),
1280 TYPE_MAX_VALUE (type));
1282 /* Then set the actual range. */
1283 SET_TYPE_RM_MIN_VALUE (range_type, convert (type, min));
1284 SET_TYPE_RM_MAX_VALUE (range_type, convert (type, max));
1286 return range_type;
1289 /* Return a TYPE_DECL node suitable for the TYPE_STUB_DECL field of a type.
1290 TYPE_NAME gives the name of the type and TYPE is a ..._TYPE node giving
1291 its data type. */
1293 tree
1294 create_type_stub_decl (tree type_name, tree type)
1296 /* Using a named TYPE_DECL ensures that a type name marker is emitted in
1297 STABS while setting DECL_ARTIFICIAL ensures that no DW_TAG_typedef is
1298 emitted in DWARF. */
1299 tree type_decl = build_decl (input_location,
1300 TYPE_DECL, type_name, type);
1301 DECL_ARTIFICIAL (type_decl) = 1;
1302 return type_decl;
1305 /* Return a TYPE_DECL node. TYPE_NAME gives the name of the type and TYPE
1306 is a ..._TYPE node giving its data type. ARTIFICIAL_P is true if this
1307 is a declaration that was generated by the compiler. DEBUG_INFO_P is
1308 true if we need to write debug information about this type. GNAT_NODE
1309 is used for the position of the decl. */
1311 tree
1312 create_type_decl (tree type_name, tree type, struct attrib *attr_list,
1313 bool artificial_p, bool debug_info_p, Node_Id gnat_node)
1315 enum tree_code code = TREE_CODE (type);
1316 bool named = TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL;
1317 tree type_decl;
1319 /* Only the builtin TYPE_STUB_DECL should be used for dummy types. */
1320 gcc_assert (!TYPE_IS_DUMMY_P (type));
1322 /* If the type hasn't been named yet, we're naming it; preserve an existing
1323 TYPE_STUB_DECL that has been attached to it for some purpose. */
1324 if (!named && TYPE_STUB_DECL (type))
1326 type_decl = TYPE_STUB_DECL (type);
1327 DECL_NAME (type_decl) = type_name;
1329 else
1330 type_decl = build_decl (input_location,
1331 TYPE_DECL, type_name, type);
1333 DECL_ARTIFICIAL (type_decl) = artificial_p;
1335 /* Add this decl to the current binding level. */
1336 gnat_pushdecl (type_decl, gnat_node);
1338 process_attributes (type_decl, attr_list);
1340 /* If we're naming the type, equate the TYPE_STUB_DECL to the name.
1341 This causes the name to be also viewed as a "tag" by the debug
1342 back-end, with the advantage that no DW_TAG_typedef is emitted
1343 for artificial "tagged" types in DWARF. */
1344 if (!named)
1345 TYPE_STUB_DECL (type) = type_decl;
1347 /* Pass the type declaration to the debug back-end unless this is an
1348 UNCONSTRAINED_ARRAY_TYPE that the back-end does not support, or a
1349 type for which debugging information was not requested, or else an
1350 ENUMERAL_TYPE or RECORD_TYPE (except for fat pointers) which are
1351 handled separately. And do not pass dummy types either. */
1352 if (code == UNCONSTRAINED_ARRAY_TYPE || !debug_info_p)
1353 DECL_IGNORED_P (type_decl) = 1;
1354 else if (code != ENUMERAL_TYPE
1355 && (code != RECORD_TYPE || TYPE_FAT_POINTER_P (type))
1356 && !((code == POINTER_TYPE || code == REFERENCE_TYPE)
1357 && TYPE_IS_DUMMY_P (TREE_TYPE (type)))
1358 && !(code == RECORD_TYPE
1359 && TYPE_IS_DUMMY_P
1360 (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (type))))))
1361 rest_of_type_decl_compilation (type_decl);
1363 return type_decl;
1366 /* Return a VAR_DECL or CONST_DECL node.
1368 VAR_NAME gives the name of the variable. ASM_NAME is its assembler name
1369 (if provided). TYPE is its data type (a GCC ..._TYPE node). VAR_INIT is
1370 the GCC tree for an optional initial expression; NULL_TREE if none.
1372 CONST_FLAG is true if this variable is constant, in which case we might
1373 return a CONST_DECL node unless CONST_DECL_ALLOWED_P is false.
1375 PUBLIC_FLAG is true if this is for a reference to a public entity or for a
1376 definition to be made visible outside of the current compilation unit, for
1377 instance variable definitions in a package specification.
1379 EXTERN_FLAG is true when processing an external variable declaration (as
1380 opposed to a definition: no storage is to be allocated for the variable).
1382 STATIC_FLAG is only relevant when not at top level. In that case
1383 it indicates whether to always allocate storage to the variable.
1385 GNAT_NODE is used for the position of the decl. */
1387 tree
1388 create_var_decl_1 (tree var_name, tree asm_name, tree type, tree var_init,
1389 bool const_flag, bool public_flag, bool extern_flag,
1390 bool static_flag, bool const_decl_allowed_p,
1391 struct attrib *attr_list, Node_Id gnat_node)
1393 bool init_const
1394 = (var_init != 0
1395 && gnat_types_compatible_p (type, TREE_TYPE (var_init))
1396 && (global_bindings_p () || static_flag
1397 ? initializer_constant_valid_p (var_init, TREE_TYPE (var_init)) != 0
1398 : TREE_CONSTANT (var_init)));
1400 /* Whether we will make TREE_CONSTANT the DECL we produce here, in which
1401 case the initializer may be used in-lieu of the DECL node (as done in
1402 Identifier_to_gnu). This is useful to prevent the need of elaboration
1403 code when an identifier for which such a decl is made is in turn used as
1404 an initializer. We used to rely on CONST vs VAR_DECL for this purpose,
1405 but extra constraints apply to this choice (see below) and are not
1406 relevant to the distinction we wish to make. */
1407 bool constant_p = const_flag && init_const;
1409 /* The actual DECL node. CONST_DECL was initially intended for enumerals
1410 and may be used for scalars in general but not for aggregates. */
1411 tree var_decl
1412 = build_decl (input_location,
1413 (constant_p && const_decl_allowed_p
1414 && !AGGREGATE_TYPE_P (type)) ? CONST_DECL : VAR_DECL,
1415 var_name, type);
1417 /* If this is external, throw away any initializations (they will be done
1418 elsewhere) unless this is a constant for which we would like to remain
1419 able to get the initializer. If we are defining a global here, leave a
1420 constant initialization and save any variable elaborations for the
1421 elaboration routine. If we are just annotating types, throw away the
1422 initialization if it isn't a constant. */
1423 if ((extern_flag && !constant_p)
1424 || (type_annotate_only && var_init && !TREE_CONSTANT (var_init)))
1425 var_init = NULL_TREE;
1427 /* At the global level, an initializer requiring code to be generated
1428 produces elaboration statements. Check that such statements are allowed,
1429 that is, not violating a No_Elaboration_Code restriction. */
1430 if (global_bindings_p () && var_init != 0 && !init_const)
1431 Check_Elaboration_Code_Allowed (gnat_node);
1433 DECL_INITIAL (var_decl) = var_init;
1434 TREE_READONLY (var_decl) = const_flag;
1435 DECL_EXTERNAL (var_decl) = extern_flag;
1436 TREE_PUBLIC (var_decl) = public_flag || extern_flag;
1437 TREE_CONSTANT (var_decl) = constant_p;
1438 TREE_THIS_VOLATILE (var_decl) = TREE_SIDE_EFFECTS (var_decl)
1439 = TYPE_VOLATILE (type);
1441 /* Ada doesn't feature Fortran-like COMMON variables so we shouldn't
1442 try to fiddle with DECL_COMMON. However, on platforms that don't
1443 support global BSS sections, uninitialized global variables would
1444 go in DATA instead, thus increasing the size of the executable. */
1445 if (!flag_no_common
1446 && TREE_CODE (var_decl) == VAR_DECL
1447 && TREE_PUBLIC (var_decl)
1448 && !have_global_bss_p ())
1449 DECL_COMMON (var_decl) = 1;
1451 /* At the global binding level, we need to allocate static storage for the
1452 variable if it isn't external. Otherwise, we allocate automatic storage
1453 unless requested not to. */
1454 TREE_STATIC (var_decl)
1455 = !extern_flag && (static_flag || global_bindings_p ());
1457 /* For an external constant whose initializer is not absolute, do not emit
1458 debug info. In DWARF this would mean a global relocation in a read-only
1459 section which runs afoul of the PE-COFF run-time relocation mechanism. */
1460 if (extern_flag
1461 && constant_p
1462 && initializer_constant_valid_p (var_init, TREE_TYPE (var_init))
1463 != null_pointer_node)
1464 DECL_IGNORED_P (var_decl) = 1;
1466 /* Add this decl to the current binding level. */
1467 gnat_pushdecl (var_decl, gnat_node);
1469 if (TREE_SIDE_EFFECTS (var_decl))
1470 TREE_ADDRESSABLE (var_decl) = 1;
1472 if (TREE_CODE (var_decl) == VAR_DECL)
1474 if (asm_name)
1475 SET_DECL_ASSEMBLER_NAME (var_decl, asm_name);
1476 process_attributes (var_decl, attr_list);
1477 if (global_bindings_p ())
1478 rest_of_decl_compilation (var_decl, true, 0);
1480 else
1481 expand_decl (var_decl);
1483 return var_decl;
1486 /* Return true if TYPE, an aggregate type, contains (or is) an array. */
1488 static bool
1489 aggregate_type_contains_array_p (tree type)
1491 switch (TREE_CODE (type))
1493 case RECORD_TYPE:
1494 case UNION_TYPE:
1495 case QUAL_UNION_TYPE:
1497 tree field;
1498 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1499 if (AGGREGATE_TYPE_P (TREE_TYPE (field))
1500 && aggregate_type_contains_array_p (TREE_TYPE (field)))
1501 return true;
1502 return false;
1505 case ARRAY_TYPE:
1506 return true;
1508 default:
1509 gcc_unreachable ();
1513 /* Return a FIELD_DECL node. FIELD_NAME is the field's name, FIELD_TYPE is
1514 its type and RECORD_TYPE is the type of the enclosing record. If SIZE is
1515 nonzero, it is the specified size of the field. If POS is nonzero, it is
1516 the bit position. PACKED is 1 if the enclosing record is packed, -1 if it
1517 has Component_Alignment of Storage_Unit. If ADDRESSABLE is nonzero, it
1518 means we are allowed to take the address of the field; if it is negative,
1519 we should not make a bitfield, which is used by make_aligning_type. */
1521 tree
1522 create_field_decl (tree field_name, tree field_type, tree record_type,
1523 tree size, tree pos, int packed, int addressable)
1525 tree field_decl = build_decl (input_location,
1526 FIELD_DECL, field_name, field_type);
1528 DECL_CONTEXT (field_decl) = record_type;
1529 TREE_READONLY (field_decl) = TYPE_READONLY (field_type);
1531 /* If FIELD_TYPE is BLKmode, we must ensure this is aligned to at least a
1532 byte boundary since GCC cannot handle less-aligned BLKmode bitfields.
1533 Likewise for an aggregate without specified position that contains an
1534 array, because in this case slices of variable length of this array
1535 must be handled by GCC and variable-sized objects need to be aligned
1536 to at least a byte boundary. */
1537 if (packed && (TYPE_MODE (field_type) == BLKmode
1538 || (!pos
1539 && AGGREGATE_TYPE_P (field_type)
1540 && aggregate_type_contains_array_p (field_type))))
1541 DECL_ALIGN (field_decl) = BITS_PER_UNIT;
1543 /* If a size is specified, use it. Otherwise, if the record type is packed
1544 compute a size to use, which may differ from the object's natural size.
1545 We always set a size in this case to trigger the checks for bitfield
1546 creation below, which is typically required when no position has been
1547 specified. */
1548 if (size)
1549 size = convert (bitsizetype, size);
1550 else if (packed == 1)
1552 size = rm_size (field_type);
1553 if (TYPE_MODE (field_type) == BLKmode)
1554 size = round_up (size, BITS_PER_UNIT);
1557 /* If we may, according to ADDRESSABLE, make a bitfield if a size is
1558 specified for two reasons: first if the size differs from the natural
1559 size. Second, if the alignment is insufficient. There are a number of
1560 ways the latter can be true.
1562 We never make a bitfield if the type of the field has a nonconstant size,
1563 because no such entity requiring bitfield operations should reach here.
1565 We do *preventively* make a bitfield when there might be the need for it
1566 but we don't have all the necessary information to decide, as is the case
1567 of a field with no specified position in a packed record.
1569 We also don't look at STRICT_ALIGNMENT here, and rely on later processing
1570 in layout_decl or finish_record_type to clear the bit_field indication if
1571 it is in fact not needed. */
1572 if (addressable >= 0
1573 && size
1574 && TREE_CODE (size) == INTEGER_CST
1575 && TREE_CODE (TYPE_SIZE (field_type)) == INTEGER_CST
1576 && (!tree_int_cst_equal (size, TYPE_SIZE (field_type))
1577 || (pos && !value_factor_p (pos, TYPE_ALIGN (field_type)))
1578 || packed
1579 || (TYPE_ALIGN (record_type) != 0
1580 && TYPE_ALIGN (record_type) < TYPE_ALIGN (field_type))))
1582 DECL_BIT_FIELD (field_decl) = 1;
1583 DECL_SIZE (field_decl) = size;
1584 if (!packed && !pos)
1586 if (TYPE_ALIGN (record_type) != 0
1587 && TYPE_ALIGN (record_type) < TYPE_ALIGN (field_type))
1588 DECL_ALIGN (field_decl) = TYPE_ALIGN (record_type);
1589 else
1590 DECL_ALIGN (field_decl) = TYPE_ALIGN (field_type);
1594 DECL_PACKED (field_decl) = pos ? DECL_BIT_FIELD (field_decl) : packed;
1596 /* Bump the alignment if need be, either for bitfield/packing purposes or
1597 to satisfy the type requirements if no such consideration applies. When
1598 we get the alignment from the type, indicate if this is from an explicit
1599 user request, which prevents stor-layout from lowering it later on. */
1601 unsigned int bit_align
1602 = (DECL_BIT_FIELD (field_decl) ? 1
1603 : packed && TYPE_MODE (field_type) != BLKmode ? BITS_PER_UNIT : 0);
1605 if (bit_align > DECL_ALIGN (field_decl))
1606 DECL_ALIGN (field_decl) = bit_align;
1607 else if (!bit_align && TYPE_ALIGN (field_type) > DECL_ALIGN (field_decl))
1609 DECL_ALIGN (field_decl) = TYPE_ALIGN (field_type);
1610 DECL_USER_ALIGN (field_decl) = TYPE_USER_ALIGN (field_type);
1614 if (pos)
1616 /* We need to pass in the alignment the DECL is known to have.
1617 This is the lowest-order bit set in POS, but no more than
1618 the alignment of the record, if one is specified. Note
1619 that an alignment of 0 is taken as infinite. */
1620 unsigned int known_align;
1622 if (host_integerp (pos, 1))
1623 known_align = tree_low_cst (pos, 1) & - tree_low_cst (pos, 1);
1624 else
1625 known_align = BITS_PER_UNIT;
1627 if (TYPE_ALIGN (record_type)
1628 && (known_align == 0 || known_align > TYPE_ALIGN (record_type)))
1629 known_align = TYPE_ALIGN (record_type);
1631 layout_decl (field_decl, known_align);
1632 SET_DECL_OFFSET_ALIGN (field_decl,
1633 host_integerp (pos, 1) ? BIGGEST_ALIGNMENT
1634 : BITS_PER_UNIT);
1635 pos_from_bit (&DECL_FIELD_OFFSET (field_decl),
1636 &DECL_FIELD_BIT_OFFSET (field_decl),
1637 DECL_OFFSET_ALIGN (field_decl), pos);
1640 /* In addition to what our caller says, claim the field is addressable if we
1641 know that its type is not suitable.
1643 The field may also be "technically" nonaddressable, meaning that even if
1644 we attempt to take the field's address we will actually get the address
1645 of a copy. This is the case for true bitfields, but the DECL_BIT_FIELD
1646 value we have at this point is not accurate enough, so we don't account
1647 for this here and let finish_record_type decide. */
1648 if (!addressable && !type_for_nonaliased_component_p (field_type))
1649 addressable = 1;
1651 DECL_NONADDRESSABLE_P (field_decl) = !addressable;
1653 return field_decl;
1656 /* Return a PARM_DECL node. PARAM_NAME is the name of the parameter and
1657 PARAM_TYPE is its type. READONLY is true if the parameter is readonly
1658 (either an In parameter or an address of a pass-by-ref parameter). */
1660 tree
1661 create_param_decl (tree param_name, tree param_type, bool readonly)
1663 tree param_decl = build_decl (input_location,
1664 PARM_DECL, param_name, param_type);
1666 /* Honor TARGET_PROMOTE_PROTOTYPES like the C compiler, as not doing so
1667 can lead to various ABI violations. */
1668 if (targetm.calls.promote_prototypes (NULL_TREE)
1669 && INTEGRAL_TYPE_P (param_type)
1670 && TYPE_PRECISION (param_type) < TYPE_PRECISION (integer_type_node))
1672 /* We have to be careful about biased types here. Make a subtype
1673 of integer_type_node with the proper biasing. */
1674 if (TREE_CODE (param_type) == INTEGER_TYPE
1675 && TYPE_BIASED_REPRESENTATION_P (param_type))
1677 tree subtype
1678 = make_unsigned_type (TYPE_PRECISION (integer_type_node));
1679 TREE_TYPE (subtype) = integer_type_node;
1680 TYPE_BIASED_REPRESENTATION_P (subtype) = 1;
1681 SET_TYPE_RM_MIN_VALUE (subtype, TYPE_MIN_VALUE (param_type));
1682 SET_TYPE_RM_MAX_VALUE (subtype, TYPE_MAX_VALUE (param_type));
1683 param_type = subtype;
1685 else
1686 param_type = integer_type_node;
1689 DECL_ARG_TYPE (param_decl) = param_type;
1690 TREE_READONLY (param_decl) = readonly;
1691 return param_decl;
1694 /* Given a DECL and ATTR_LIST, process the listed attributes. */
1696 static void
1697 process_attributes (tree decl, struct attrib *attr_list)
1699 for (; attr_list; attr_list = attr_list->next)
1700 switch (attr_list->type)
1702 case ATTR_MACHINE_ATTRIBUTE:
1703 input_location = DECL_SOURCE_LOCATION (decl);
1704 decl_attributes (&decl, tree_cons (attr_list->name, attr_list->args,
1705 NULL_TREE),
1706 ATTR_FLAG_TYPE_IN_PLACE);
1707 break;
1709 case ATTR_LINK_ALIAS:
1710 if (! DECL_EXTERNAL (decl))
1712 TREE_STATIC (decl) = 1;
1713 assemble_alias (decl, attr_list->name);
1715 break;
1717 case ATTR_WEAK_EXTERNAL:
1718 if (SUPPORTS_WEAK)
1719 declare_weak (decl);
1720 else
1721 post_error ("?weak declarations not supported on this target",
1722 attr_list->error_point);
1723 break;
1725 case ATTR_LINK_SECTION:
1726 if (targetm.have_named_sections)
1728 DECL_SECTION_NAME (decl)
1729 = build_string (IDENTIFIER_LENGTH (attr_list->name),
1730 IDENTIFIER_POINTER (attr_list->name));
1731 DECL_COMMON (decl) = 0;
1733 else
1734 post_error ("?section attributes are not supported for this target",
1735 attr_list->error_point);
1736 break;
1738 case ATTR_LINK_CONSTRUCTOR:
1739 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1740 TREE_USED (decl) = 1;
1741 break;
1743 case ATTR_LINK_DESTRUCTOR:
1744 DECL_STATIC_DESTRUCTOR (decl) = 1;
1745 TREE_USED (decl) = 1;
1746 break;
1748 case ATTR_THREAD_LOCAL_STORAGE:
1749 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
1750 DECL_COMMON (decl) = 0;
1751 break;
1755 /* Record DECL as a global renaming pointer. */
1757 void
1758 record_global_renaming_pointer (tree decl)
1760 gcc_assert (DECL_RENAMED_OBJECT (decl));
1761 VEC_safe_push (tree, gc, global_renaming_pointers, decl);
1764 /* Invalidate the global renaming pointers. */
1766 void
1767 invalidate_global_renaming_pointers (void)
1769 unsigned int i;
1770 tree iter;
1772 FOR_EACH_VEC_ELT (tree, global_renaming_pointers, i, iter)
1773 SET_DECL_RENAMED_OBJECT (iter, NULL_TREE);
1775 VEC_free (tree, gc, global_renaming_pointers);
1778 /* Return true if VALUE is a known to be a multiple of FACTOR, which must be
1779 a power of 2. */
1781 bool
1782 value_factor_p (tree value, HOST_WIDE_INT factor)
1784 if (host_integerp (value, 1))
1785 return tree_low_cst (value, 1) % factor == 0;
1787 if (TREE_CODE (value) == MULT_EXPR)
1788 return (value_factor_p (TREE_OPERAND (value, 0), factor)
1789 || value_factor_p (TREE_OPERAND (value, 1), factor));
1791 return false;
1794 /* Given 2 consecutive field decls PREV_FIELD and CURR_FIELD, return true
1795 unless we can prove these 2 fields are laid out in such a way that no gap
1796 exist between the end of PREV_FIELD and the beginning of CURR_FIELD. OFFSET
1797 is the distance in bits between the end of PREV_FIELD and the starting
1798 position of CURR_FIELD. It is ignored if null. */
1800 static bool
1801 potential_alignment_gap (tree prev_field, tree curr_field, tree offset)
1803 /* If this is the first field of the record, there cannot be any gap */
1804 if (!prev_field)
1805 return false;
1807 /* If the previous field is a union type, then return False: The only
1808 time when such a field is not the last field of the record is when
1809 there are other components at fixed positions after it (meaning there
1810 was a rep clause for every field), in which case we don't want the
1811 alignment constraint to override them. */
1812 if (TREE_CODE (TREE_TYPE (prev_field)) == QUAL_UNION_TYPE)
1813 return false;
1815 /* If the distance between the end of prev_field and the beginning of
1816 curr_field is constant, then there is a gap if the value of this
1817 constant is not null. */
1818 if (offset && host_integerp (offset, 1))
1819 return !integer_zerop (offset);
1821 /* If the size and position of the previous field are constant,
1822 then check the sum of this size and position. There will be a gap
1823 iff it is not multiple of the current field alignment. */
1824 if (host_integerp (DECL_SIZE (prev_field), 1)
1825 && host_integerp (bit_position (prev_field), 1))
1826 return ((tree_low_cst (bit_position (prev_field), 1)
1827 + tree_low_cst (DECL_SIZE (prev_field), 1))
1828 % DECL_ALIGN (curr_field) != 0);
1830 /* If both the position and size of the previous field are multiples
1831 of the current field alignment, there cannot be any gap. */
1832 if (value_factor_p (bit_position (prev_field), DECL_ALIGN (curr_field))
1833 && value_factor_p (DECL_SIZE (prev_field), DECL_ALIGN (curr_field)))
1834 return false;
1836 /* Fallback, return that there may be a potential gap */
1837 return true;
1840 /* Returns a LABEL_DECL node for LABEL_NAME. */
1842 tree
1843 create_label_decl (tree label_name)
1845 tree label_decl = build_decl (input_location,
1846 LABEL_DECL, label_name, void_type_node);
1848 DECL_CONTEXT (label_decl) = current_function_decl;
1849 DECL_MODE (label_decl) = VOIDmode;
1850 DECL_SOURCE_LOCATION (label_decl) = input_location;
1852 return label_decl;
1855 /* Returns a FUNCTION_DECL node. SUBPROG_NAME is the name of the subprogram,
1856 ASM_NAME is its assembler name, SUBPROG_TYPE is its type (a FUNCTION_TYPE
1857 node), PARAM_DECL_LIST is the list of the subprogram arguments (a list of
1858 PARM_DECL nodes chained through the TREE_CHAIN field).
1860 INLINE_FLAG, PUBLIC_FLAG, EXTERN_FLAG, and ATTR_LIST are used to set the
1861 appropriate fields in the FUNCTION_DECL. GNAT_NODE gives the location. */
1863 tree
1864 create_subprog_decl (tree subprog_name, tree asm_name,
1865 tree subprog_type, tree param_decl_list, bool inline_flag,
1866 bool public_flag, bool extern_flag,
1867 struct attrib *attr_list, Node_Id gnat_node)
1869 tree subprog_decl = build_decl (input_location, FUNCTION_DECL, subprog_name,
1870 subprog_type);
1871 tree result_decl = build_decl (input_location, RESULT_DECL, NULL_TREE,
1872 TREE_TYPE (subprog_type));
1874 /* If this is a non-inline function nested inside an inlined external
1875 function, we cannot honor both requests without cloning the nested
1876 function in the current unit since it is private to the other unit.
1877 We could inline the nested function as well but it's probably better
1878 to err on the side of too little inlining. */
1879 if (!inline_flag
1880 && !public_flag
1881 && current_function_decl
1882 && DECL_DECLARED_INLINE_P (current_function_decl)
1883 && DECL_EXTERNAL (current_function_decl))
1884 DECL_DECLARED_INLINE_P (current_function_decl) = 0;
1886 DECL_EXTERNAL (subprog_decl) = extern_flag;
1887 TREE_PUBLIC (subprog_decl) = public_flag;
1888 TREE_READONLY (subprog_decl) = TYPE_READONLY (subprog_type);
1889 TREE_THIS_VOLATILE (subprog_decl) = TYPE_VOLATILE (subprog_type);
1890 TREE_SIDE_EFFECTS (subprog_decl) = TYPE_VOLATILE (subprog_type);
1891 DECL_DECLARED_INLINE_P (subprog_decl) = inline_flag;
1892 DECL_ARGUMENTS (subprog_decl) = param_decl_list;
1894 DECL_ARTIFICIAL (result_decl) = 1;
1895 DECL_IGNORED_P (result_decl) = 1;
1896 DECL_BY_REFERENCE (result_decl) = TREE_ADDRESSABLE (subprog_type);
1897 DECL_RESULT (subprog_decl) = result_decl;
1899 if (asm_name)
1901 SET_DECL_ASSEMBLER_NAME (subprog_decl, asm_name);
1903 /* The expand_main_function circuitry expects "main_identifier_node" to
1904 designate the DECL_NAME of the 'main' entry point, in turn expected
1905 to be declared as the "main" function literally by default. Ada
1906 program entry points are typically declared with a different name
1907 within the binder generated file, exported as 'main' to satisfy the
1908 system expectations. Force main_identifier_node in this case. */
1909 if (asm_name == main_identifier_node)
1910 DECL_NAME (subprog_decl) = main_identifier_node;
1913 /* Add this decl to the current binding level. */
1914 gnat_pushdecl (subprog_decl, gnat_node);
1916 process_attributes (subprog_decl, attr_list);
1918 /* Output the assembler code and/or RTL for the declaration. */
1919 rest_of_decl_compilation (subprog_decl, global_bindings_p (), 0);
1921 return subprog_decl;
1924 /* Set up the framework for generating code for SUBPROG_DECL, a subprogram
1925 body. This routine needs to be invoked before processing the declarations
1926 appearing in the subprogram. */
1928 void
1929 begin_subprog_body (tree subprog_decl)
1931 tree param_decl;
1933 announce_function (subprog_decl);
1935 /* This function is being defined. */
1936 TREE_STATIC (subprog_decl) = 1;
1938 current_function_decl = subprog_decl;
1940 /* Enter a new binding level and show that all the parameters belong to
1941 this function. */
1942 gnat_pushlevel ();
1944 for (param_decl = DECL_ARGUMENTS (subprog_decl); param_decl;
1945 param_decl = DECL_CHAIN (param_decl))
1946 DECL_CONTEXT (param_decl) = subprog_decl;
1948 make_decl_rtl (subprog_decl);
1950 /* We handle pending sizes via the elaboration of types, so we don't need to
1951 save them. This causes them to be marked as part of the outer function
1952 and then discarded. */
1953 get_pending_sizes ();
1956 /* Finish the definition of the current subprogram BODY and finalize it. */
1958 void
1959 end_subprog_body (tree body)
1961 tree fndecl = current_function_decl;
1963 /* Attach the BLOCK for this level to the function and pop the level. */
1964 BLOCK_SUPERCONTEXT (current_binding_level->block) = fndecl;
1965 DECL_INITIAL (fndecl) = current_binding_level->block;
1966 gnat_poplevel ();
1968 /* We handle pending sizes via the elaboration of types, so we don't
1969 need to save them. */
1970 get_pending_sizes ();
1972 /* Mark the RESULT_DECL as being in this subprogram. */
1973 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
1975 /* The body should be a BIND_EXPR whose BLOCK is the top-level one. */
1976 if (TREE_CODE (body) == BIND_EXPR)
1978 BLOCK_SUPERCONTEXT (BIND_EXPR_BLOCK (body)) = fndecl;
1979 DECL_INITIAL (fndecl) = BIND_EXPR_BLOCK (body);
1982 DECL_SAVED_TREE (fndecl) = body;
1984 current_function_decl = DECL_CONTEXT (fndecl);
1986 /* We cannot track the location of errors past this point. */
1987 error_gnat_node = Empty;
1989 /* If we're only annotating types, don't actually compile this function. */
1990 if (type_annotate_only)
1991 return;
1993 /* Dump functions before gimplification. */
1994 dump_function (TDI_original, fndecl);
1996 /* ??? This special handling of nested functions is probably obsolete. */
1997 if (!DECL_CONTEXT (fndecl))
1998 cgraph_finalize_function (fndecl, false);
1999 else
2000 /* Register this function with cgraph just far enough to get it
2001 added to our parent's nested function list. */
2002 (void) cgraph_node (fndecl);
2005 tree
2006 gnat_builtin_function (tree decl)
2008 gnat_pushdecl (decl, Empty);
2009 return decl;
2012 /* Return an integer type with the number of bits of precision given by
2013 PRECISION. UNSIGNEDP is nonzero if the type is unsigned; otherwise
2014 it is a signed type. */
2016 tree
2017 gnat_type_for_size (unsigned precision, int unsignedp)
2019 tree t;
2020 char type_name[20];
2022 if (precision <= 2 * MAX_BITS_PER_WORD
2023 && signed_and_unsigned_types[precision][unsignedp])
2024 return signed_and_unsigned_types[precision][unsignedp];
2026 if (unsignedp)
2027 t = make_unsigned_type (precision);
2028 else
2029 t = make_signed_type (precision);
2031 if (precision <= 2 * MAX_BITS_PER_WORD)
2032 signed_and_unsigned_types[precision][unsignedp] = t;
2034 if (!TYPE_NAME (t))
2036 sprintf (type_name, "%sSIGNED_%d", unsignedp ? "UN" : "", precision);
2037 TYPE_NAME (t) = get_identifier (type_name);
2040 return t;
2043 /* Likewise for floating-point types. */
2045 static tree
2046 float_type_for_precision (int precision, enum machine_mode mode)
2048 tree t;
2049 char type_name[20];
2051 if (float_types[(int) mode])
2052 return float_types[(int) mode];
2054 float_types[(int) mode] = t = make_node (REAL_TYPE);
2055 TYPE_PRECISION (t) = precision;
2056 layout_type (t);
2058 gcc_assert (TYPE_MODE (t) == mode);
2059 if (!TYPE_NAME (t))
2061 sprintf (type_name, "FLOAT_%d", precision);
2062 TYPE_NAME (t) = get_identifier (type_name);
2065 return t;
2068 /* Return a data type that has machine mode MODE. UNSIGNEDP selects
2069 an unsigned type; otherwise a signed type is returned. */
2071 tree
2072 gnat_type_for_mode (enum machine_mode mode, int unsignedp)
2074 if (mode == BLKmode)
2075 return NULL_TREE;
2077 if (mode == VOIDmode)
2078 return void_type_node;
2080 if (COMPLEX_MODE_P (mode))
2081 return NULL_TREE;
2083 if (SCALAR_FLOAT_MODE_P (mode))
2084 return float_type_for_precision (GET_MODE_PRECISION (mode), mode);
2086 if (SCALAR_INT_MODE_P (mode))
2087 return gnat_type_for_size (GET_MODE_BITSIZE (mode), unsignedp);
2089 if (VECTOR_MODE_P (mode))
2091 enum machine_mode inner_mode = GET_MODE_INNER (mode);
2092 tree inner_type = gnat_type_for_mode (inner_mode, unsignedp);
2093 if (inner_type)
2094 return build_vector_type_for_mode (inner_type, mode);
2097 return NULL_TREE;
2100 /* Return the unsigned version of a TYPE_NODE, a scalar type. */
2102 tree
2103 gnat_unsigned_type (tree type_node)
2105 tree type = gnat_type_for_size (TYPE_PRECISION (type_node), 1);
2107 if (TREE_CODE (type_node) == INTEGER_TYPE && TYPE_MODULAR_P (type_node))
2109 type = copy_node (type);
2110 TREE_TYPE (type) = type_node;
2112 else if (TREE_TYPE (type_node)
2113 && TREE_CODE (TREE_TYPE (type_node)) == INTEGER_TYPE
2114 && TYPE_MODULAR_P (TREE_TYPE (type_node)))
2116 type = copy_node (type);
2117 TREE_TYPE (type) = TREE_TYPE (type_node);
2120 return type;
2123 /* Return the signed version of a TYPE_NODE, a scalar type. */
2125 tree
2126 gnat_signed_type (tree type_node)
2128 tree type = gnat_type_for_size (TYPE_PRECISION (type_node), 0);
2130 if (TREE_CODE (type_node) == INTEGER_TYPE && TYPE_MODULAR_P (type_node))
2132 type = copy_node (type);
2133 TREE_TYPE (type) = type_node;
2135 else if (TREE_TYPE (type_node)
2136 && TREE_CODE (TREE_TYPE (type_node)) == INTEGER_TYPE
2137 && TYPE_MODULAR_P (TREE_TYPE (type_node)))
2139 type = copy_node (type);
2140 TREE_TYPE (type) = TREE_TYPE (type_node);
2143 return type;
2146 /* Return 1 if the types T1 and T2 are compatible, i.e. if they can be
2147 transparently converted to each other. */
2150 gnat_types_compatible_p (tree t1, tree t2)
2152 enum tree_code code;
2154 /* This is the default criterion. */
2155 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
2156 return 1;
2158 /* We only check structural equivalence here. */
2159 if ((code = TREE_CODE (t1)) != TREE_CODE (t2))
2160 return 0;
2162 /* Vector types are also compatible if they have the same number of subparts
2163 and the same form of (scalar) element type. */
2164 if (code == VECTOR_TYPE
2165 && TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2166 && TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2))
2167 && TYPE_PRECISION (TREE_TYPE (t1)) == TYPE_PRECISION (TREE_TYPE (t2)))
2168 return 1;
2170 /* Array types are also compatible if they are constrained and have the same
2171 domain(s) and the same component type. */
2172 if (code == ARRAY_TYPE
2173 && (TYPE_DOMAIN (t1) == TYPE_DOMAIN (t2)
2174 || (TYPE_DOMAIN (t1)
2175 && TYPE_DOMAIN (t2)
2176 && tree_int_cst_equal (TYPE_MIN_VALUE (TYPE_DOMAIN (t1)),
2177 TYPE_MIN_VALUE (TYPE_DOMAIN (t2)))
2178 && tree_int_cst_equal (TYPE_MAX_VALUE (TYPE_DOMAIN (t1)),
2179 TYPE_MAX_VALUE (TYPE_DOMAIN (t2)))))
2180 && (TREE_TYPE (t1) == TREE_TYPE (t2)
2181 || (TREE_CODE (TREE_TYPE (t1)) == ARRAY_TYPE
2182 && gnat_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))))
2183 return 1;
2185 /* Padding record types are also compatible if they pad the same
2186 type and have the same constant size. */
2187 if (code == RECORD_TYPE
2188 && TYPE_PADDING_P (t1) && TYPE_PADDING_P (t2)
2189 && TREE_TYPE (TYPE_FIELDS (t1)) == TREE_TYPE (TYPE_FIELDS (t2))
2190 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2191 return 1;
2193 return 0;
2196 /* Return true if T, a FUNCTION_TYPE, has the specified list of flags. */
2198 bool
2199 fntype_same_flags_p (const_tree t, tree cico_list, bool return_unconstrained_p,
2200 bool return_by_direct_ref_p, bool return_by_invisi_ref_p)
2202 return TYPE_CI_CO_LIST (t) == cico_list
2203 && TYPE_RETURN_UNCONSTRAINED_P (t) == return_unconstrained_p
2204 && TYPE_RETURN_BY_DIRECT_REF_P (t) == return_by_direct_ref_p
2205 && TREE_ADDRESSABLE (t) == return_by_invisi_ref_p;
2208 /* EXP is an expression for the size of an object. If this size contains
2209 discriminant references, replace them with the maximum (if MAX_P) or
2210 minimum (if !MAX_P) possible value of the discriminant. */
2212 tree
2213 max_size (tree exp, bool max_p)
2215 enum tree_code code = TREE_CODE (exp);
2216 tree type = TREE_TYPE (exp);
2218 switch (TREE_CODE_CLASS (code))
2220 case tcc_declaration:
2221 case tcc_constant:
2222 return exp;
2224 case tcc_vl_exp:
2225 if (code == CALL_EXPR)
2227 tree t, *argarray;
2228 int n, i;
2230 t = maybe_inline_call_in_expr (exp);
2231 if (t)
2232 return max_size (t, max_p);
2234 n = call_expr_nargs (exp);
2235 gcc_assert (n > 0);
2236 argarray = XALLOCAVEC (tree, n);
2237 for (i = 0; i < n; i++)
2238 argarray[i] = max_size (CALL_EXPR_ARG (exp, i), max_p);
2239 return build_call_array (type, CALL_EXPR_FN (exp), n, argarray);
2241 break;
2243 case tcc_reference:
2244 /* If this contains a PLACEHOLDER_EXPR, it is the thing we want to
2245 modify. Otherwise, we treat it like a variable. */
2246 if (!CONTAINS_PLACEHOLDER_P (exp))
2247 return exp;
2249 type = TREE_TYPE (TREE_OPERAND (exp, 1));
2250 return
2251 max_size (max_p ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type), true);
2253 case tcc_comparison:
2254 return max_p ? size_one_node : size_zero_node;
2256 case tcc_unary:
2257 case tcc_binary:
2258 case tcc_expression:
2259 switch (TREE_CODE_LENGTH (code))
2261 case 1:
2262 if (code == NON_LVALUE_EXPR)
2263 return max_size (TREE_OPERAND (exp, 0), max_p);
2264 else
2265 return
2266 fold_build1 (code, type,
2267 max_size (TREE_OPERAND (exp, 0),
2268 code == NEGATE_EXPR ? !max_p : max_p));
2270 case 2:
2271 if (code == COMPOUND_EXPR)
2272 return max_size (TREE_OPERAND (exp, 1), max_p);
2275 tree lhs = max_size (TREE_OPERAND (exp, 0), max_p);
2276 tree rhs = max_size (TREE_OPERAND (exp, 1),
2277 code == MINUS_EXPR ? !max_p : max_p);
2279 /* Special-case wanting the maximum value of a MIN_EXPR.
2280 In that case, if one side overflows, return the other.
2281 sizetype is signed, but we know sizes are non-negative.
2282 Likewise, handle a MINUS_EXPR or PLUS_EXPR with the LHS
2283 overflowing and the RHS a variable. */
2284 if (max_p
2285 && code == MIN_EXPR
2286 && TREE_CODE (rhs) == INTEGER_CST
2287 && TREE_OVERFLOW (rhs))
2288 return lhs;
2289 else if (max_p
2290 && code == MIN_EXPR
2291 && TREE_CODE (lhs) == INTEGER_CST
2292 && TREE_OVERFLOW (lhs))
2293 return rhs;
2294 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
2295 && TREE_CODE (lhs) == INTEGER_CST
2296 && TREE_OVERFLOW (lhs)
2297 && !TREE_CONSTANT (rhs))
2298 return lhs;
2299 else
2300 return fold_build2 (code, type, lhs, rhs);
2303 case 3:
2304 if (code == SAVE_EXPR)
2305 return exp;
2306 else if (code == COND_EXPR)
2307 return fold_build2 (max_p ? MAX_EXPR : MIN_EXPR, type,
2308 max_size (TREE_OPERAND (exp, 1), max_p),
2309 max_size (TREE_OPERAND (exp, 2), max_p));
2312 /* Other tree classes cannot happen. */
2313 default:
2314 break;
2317 gcc_unreachable ();
2320 /* Build a template of type TEMPLATE_TYPE from the array bounds of ARRAY_TYPE.
2321 EXPR is an expression that we can use to locate any PLACEHOLDER_EXPRs.
2322 Return a constructor for the template. */
2324 tree
2325 build_template (tree template_type, tree array_type, tree expr)
2327 VEC(constructor_elt,gc) *template_elts = NULL;
2328 tree bound_list = NULL_TREE;
2329 tree field;
2331 while (TREE_CODE (array_type) == RECORD_TYPE
2332 && (TYPE_PADDING_P (array_type)
2333 || TYPE_JUSTIFIED_MODULAR_P (array_type)))
2334 array_type = TREE_TYPE (TYPE_FIELDS (array_type));
2336 if (TREE_CODE (array_type) == ARRAY_TYPE
2337 || (TREE_CODE (array_type) == INTEGER_TYPE
2338 && TYPE_HAS_ACTUAL_BOUNDS_P (array_type)))
2339 bound_list = TYPE_ACTUAL_BOUNDS (array_type);
2341 /* First make the list for a CONSTRUCTOR for the template. Go down the
2342 field list of the template instead of the type chain because this
2343 array might be an Ada array of arrays and we can't tell where the
2344 nested arrays stop being the underlying object. */
2346 for (field = TYPE_FIELDS (template_type); field;
2347 (bound_list
2348 ? (bound_list = TREE_CHAIN (bound_list))
2349 : (array_type = TREE_TYPE (array_type))),
2350 field = DECL_CHAIN (DECL_CHAIN (field)))
2352 tree bounds, min, max;
2354 /* If we have a bound list, get the bounds from there. Likewise
2355 for an ARRAY_TYPE. Otherwise, if expr is a PARM_DECL with
2356 DECL_BY_COMPONENT_PTR_P, use the bounds of the field in the template.
2357 This will give us a maximum range. */
2358 if (bound_list)
2359 bounds = TREE_VALUE (bound_list);
2360 else if (TREE_CODE (array_type) == ARRAY_TYPE)
2361 bounds = TYPE_INDEX_TYPE (TYPE_DOMAIN (array_type));
2362 else if (expr && TREE_CODE (expr) == PARM_DECL
2363 && DECL_BY_COMPONENT_PTR_P (expr))
2364 bounds = TREE_TYPE (field);
2365 else
2366 gcc_unreachable ();
2368 min = convert (TREE_TYPE (field), TYPE_MIN_VALUE (bounds));
2369 max = convert (TREE_TYPE (DECL_CHAIN (field)), TYPE_MAX_VALUE (bounds));
2371 /* If either MIN or MAX involve a PLACEHOLDER_EXPR, we must
2372 substitute it from OBJECT. */
2373 min = SUBSTITUTE_PLACEHOLDER_IN_EXPR (min, expr);
2374 max = SUBSTITUTE_PLACEHOLDER_IN_EXPR (max, expr);
2376 CONSTRUCTOR_APPEND_ELT (template_elts, field, min);
2377 CONSTRUCTOR_APPEND_ELT (template_elts, DECL_CHAIN (field), max);
2380 return gnat_build_constructor (template_type, template_elts);
2383 /* Helper routine to make a descriptor field. FIELD_LIST is the list of decls
2384 being built; the new decl is chained on to the front of the list. */
2386 static tree
2387 make_descriptor_field (const char *name, tree type, tree rec_type,
2388 tree initial, tree field_list)
2390 tree field
2391 = create_field_decl (get_identifier (name), type, rec_type, NULL_TREE,
2392 NULL_TREE, 0, 0);
2394 DECL_INITIAL (field) = initial;
2395 DECL_CHAIN (field) = field_list;
2396 return field;
2399 /* Build a 32-bit VMS descriptor from a Mechanism_Type, which must specify a
2400 descriptor type, and the GCC type of an object. Each FIELD_DECL in the
2401 type contains in its DECL_INITIAL the expression to use when a constructor
2402 is made for the type. GNAT_ENTITY is an entity used to print out an error
2403 message if the mechanism cannot be applied to an object of that type and
2404 also for the name. */
2406 tree
2407 build_vms_descriptor32 (tree type, Mechanism_Type mech, Entity_Id gnat_entity)
2409 tree record_type = make_node (RECORD_TYPE);
2410 tree pointer32_type, pointer64_type;
2411 tree field_list = NULL_TREE;
2412 int klass, ndim, i, dtype = 0;
2413 tree inner_type, tem;
2414 tree *idx_arr;
2416 /* If TYPE is an unconstrained array, use the underlying array type. */
2417 if (TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
2418 type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (type))));
2420 /* If this is an array, compute the number of dimensions in the array,
2421 get the index types, and point to the inner type. */
2422 if (TREE_CODE (type) != ARRAY_TYPE)
2423 ndim = 0;
2424 else
2425 for (ndim = 1, inner_type = type;
2426 TREE_CODE (TREE_TYPE (inner_type)) == ARRAY_TYPE
2427 && TYPE_MULTI_ARRAY_P (TREE_TYPE (inner_type));
2428 ndim++, inner_type = TREE_TYPE (inner_type))
2431 idx_arr = XALLOCAVEC (tree, ndim);
2433 if (mech != By_Descriptor_NCA && mech != By_Short_Descriptor_NCA
2434 && TREE_CODE (type) == ARRAY_TYPE && TYPE_CONVENTION_FORTRAN_P (type))
2435 for (i = ndim - 1, inner_type = type;
2436 i >= 0;
2437 i--, inner_type = TREE_TYPE (inner_type))
2438 idx_arr[i] = TYPE_DOMAIN (inner_type);
2439 else
2440 for (i = 0, inner_type = type;
2441 i < ndim;
2442 i++, inner_type = TREE_TYPE (inner_type))
2443 idx_arr[i] = TYPE_DOMAIN (inner_type);
2445 /* Now get the DTYPE value. */
2446 switch (TREE_CODE (type))
2448 case INTEGER_TYPE:
2449 case ENUMERAL_TYPE:
2450 case BOOLEAN_TYPE:
2451 if (TYPE_VAX_FLOATING_POINT_P (type))
2452 switch (tree_low_cst (TYPE_DIGITS_VALUE (type), 1))
2454 case 6:
2455 dtype = 10;
2456 break;
2457 case 9:
2458 dtype = 11;
2459 break;
2460 case 15:
2461 dtype = 27;
2462 break;
2464 else
2465 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
2467 case 8:
2468 dtype = TYPE_UNSIGNED (type) ? 2 : 6;
2469 break;
2470 case 16:
2471 dtype = TYPE_UNSIGNED (type) ? 3 : 7;
2472 break;
2473 case 32:
2474 dtype = TYPE_UNSIGNED (type) ? 4 : 8;
2475 break;
2476 case 64:
2477 dtype = TYPE_UNSIGNED (type) ? 5 : 9;
2478 break;
2479 case 128:
2480 dtype = TYPE_UNSIGNED (type) ? 25 : 26;
2481 break;
2483 break;
2485 case REAL_TYPE:
2486 dtype = GET_MODE_BITSIZE (TYPE_MODE (type)) == 32 ? 52 : 53;
2487 break;
2489 case COMPLEX_TYPE:
2490 if (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
2491 && TYPE_VAX_FLOATING_POINT_P (type))
2492 switch (tree_low_cst (TYPE_DIGITS_VALUE (type), 1))
2494 case 6:
2495 dtype = 12;
2496 break;
2497 case 9:
2498 dtype = 13;
2499 break;
2500 case 15:
2501 dtype = 29;
2503 else
2504 dtype = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) == 32 ? 54: 55;
2505 break;
2507 case ARRAY_TYPE:
2508 dtype = 14;
2509 break;
2511 default:
2512 break;
2515 /* Get the CLASS value. */
2516 switch (mech)
2518 case By_Descriptor_A:
2519 case By_Short_Descriptor_A:
2520 klass = 4;
2521 break;
2522 case By_Descriptor_NCA:
2523 case By_Short_Descriptor_NCA:
2524 klass = 10;
2525 break;
2526 case By_Descriptor_SB:
2527 case By_Short_Descriptor_SB:
2528 klass = 15;
2529 break;
2530 case By_Descriptor:
2531 case By_Short_Descriptor:
2532 case By_Descriptor_S:
2533 case By_Short_Descriptor_S:
2534 default:
2535 klass = 1;
2536 break;
2539 /* Make the type for a descriptor for VMS. The first four fields are the
2540 same for all types. */
2541 field_list
2542 = make_descriptor_field ("LENGTH", gnat_type_for_size (16, 1), record_type,
2543 size_in_bytes ((mech == By_Descriptor_A
2544 || mech == By_Short_Descriptor_A)
2545 ? inner_type : type),
2546 field_list);
2547 field_list
2548 = make_descriptor_field ("DTYPE", gnat_type_for_size (8, 1), record_type,
2549 size_int (dtype), field_list);
2550 field_list
2551 = make_descriptor_field ("CLASS", gnat_type_for_size (8, 1), record_type,
2552 size_int (klass), field_list);
2554 pointer32_type = build_pointer_type_for_mode (type, SImode, false);
2555 pointer64_type = build_pointer_type_for_mode (type, DImode, false);
2557 /* Ensure that only 32-bit pointers are passed in 32-bit descriptors. Note
2558 that we cannot build a template call to the CE routine as it would get a
2559 wrong source location; instead we use a second placeholder for it. */
2560 tem = build_unary_op (ADDR_EXPR, pointer64_type,
2561 build0 (PLACEHOLDER_EXPR, type));
2562 tem = build3 (COND_EXPR, pointer32_type,
2563 build_binary_op (GE_EXPR, boolean_type_node, tem,
2564 build_int_cstu (pointer64_type, 0x80000000)),
2565 build0 (PLACEHOLDER_EXPR, void_type_node),
2566 convert (pointer32_type, tem));
2568 field_list
2569 = make_descriptor_field ("POINTER", pointer32_type, record_type, tem,
2570 field_list);
2572 switch (mech)
2574 case By_Descriptor:
2575 case By_Short_Descriptor:
2576 case By_Descriptor_S:
2577 case By_Short_Descriptor_S:
2578 break;
2580 case By_Descriptor_SB:
2581 case By_Short_Descriptor_SB:
2582 field_list
2583 = make_descriptor_field ("SB_L1", gnat_type_for_size (32, 1),
2584 record_type,
2585 (TREE_CODE (type) == ARRAY_TYPE
2586 ? TYPE_MIN_VALUE (TYPE_DOMAIN (type))
2587 : size_zero_node),
2588 field_list);
2589 field_list
2590 = make_descriptor_field ("SB_U1", gnat_type_for_size (32, 1),
2591 record_type,
2592 (TREE_CODE (type) == ARRAY_TYPE
2593 ? TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2594 : size_zero_node),
2595 field_list);
2596 break;
2598 case By_Descriptor_A:
2599 case By_Short_Descriptor_A:
2600 case By_Descriptor_NCA:
2601 case By_Short_Descriptor_NCA:
2602 field_list
2603 = make_descriptor_field ("SCALE", gnat_type_for_size (8, 1),
2604 record_type, size_zero_node, field_list);
2606 field_list
2607 = make_descriptor_field ("DIGITS", gnat_type_for_size (8, 1),
2608 record_type, size_zero_node, field_list);
2610 field_list
2611 = make_descriptor_field ("AFLAGS", gnat_type_for_size (8, 1),
2612 record_type,
2613 size_int ((mech == By_Descriptor_NCA
2614 || mech == By_Short_Descriptor_NCA)
2616 /* Set FL_COLUMN, FL_COEFF, and
2617 FL_BOUNDS. */
2618 : (TREE_CODE (type) == ARRAY_TYPE
2619 && TYPE_CONVENTION_FORTRAN_P
2620 (type)
2621 ? 224 : 192)),
2622 field_list);
2624 field_list
2625 = make_descriptor_field ("DIMCT", gnat_type_for_size (8, 1),
2626 record_type, size_int (ndim), field_list);
2628 field_list
2629 = make_descriptor_field ("ARSIZE", gnat_type_for_size (32, 1),
2630 record_type, size_in_bytes (type),
2631 field_list);
2633 /* Now build a pointer to the 0,0,0... element. */
2634 tem = build0 (PLACEHOLDER_EXPR, type);
2635 for (i = 0, inner_type = type; i < ndim;
2636 i++, inner_type = TREE_TYPE (inner_type))
2637 tem = build4 (ARRAY_REF, TREE_TYPE (inner_type), tem,
2638 convert (TYPE_DOMAIN (inner_type), size_zero_node),
2639 NULL_TREE, NULL_TREE);
2641 field_list
2642 = make_descriptor_field ("A0", pointer32_type, record_type,
2643 build1 (ADDR_EXPR, pointer32_type, tem),
2644 field_list);
2646 /* Next come the addressing coefficients. */
2647 tem = size_one_node;
2648 for (i = 0; i < ndim; i++)
2650 char fname[3];
2651 tree idx_length
2652 = size_binop (MULT_EXPR, tem,
2653 size_binop (PLUS_EXPR,
2654 size_binop (MINUS_EXPR,
2655 TYPE_MAX_VALUE (idx_arr[i]),
2656 TYPE_MIN_VALUE (idx_arr[i])),
2657 size_int (1)));
2659 fname[0] = ((mech == By_Descriptor_NCA ||
2660 mech == By_Short_Descriptor_NCA) ? 'S' : 'M');
2661 fname[1] = '0' + i, fname[2] = 0;
2662 field_list
2663 = make_descriptor_field (fname, gnat_type_for_size (32, 1),
2664 record_type, idx_length, field_list);
2666 if (mech == By_Descriptor_NCA || mech == By_Short_Descriptor_NCA)
2667 tem = idx_length;
2670 /* Finally here are the bounds. */
2671 for (i = 0; i < ndim; i++)
2673 char fname[3];
2675 fname[0] = 'L', fname[1] = '0' + i, fname[2] = 0;
2676 field_list
2677 = make_descriptor_field (fname, gnat_type_for_size (32, 1),
2678 record_type, TYPE_MIN_VALUE (idx_arr[i]),
2679 field_list);
2681 fname[0] = 'U';
2682 field_list
2683 = make_descriptor_field (fname, gnat_type_for_size (32, 1),
2684 record_type, TYPE_MAX_VALUE (idx_arr[i]),
2685 field_list);
2687 break;
2689 default:
2690 post_error ("unsupported descriptor type for &", gnat_entity);
2693 TYPE_NAME (record_type) = create_concat_name (gnat_entity, "DESC");
2694 finish_record_type (record_type, nreverse (field_list), 0, false);
2695 return record_type;
2698 /* Build a 64-bit VMS descriptor from a Mechanism_Type, which must specify a
2699 descriptor type, and the GCC type of an object. Each FIELD_DECL in the
2700 type contains in its DECL_INITIAL the expression to use when a constructor
2701 is made for the type. GNAT_ENTITY is an entity used to print out an error
2702 message if the mechanism cannot be applied to an object of that type and
2703 also for the name. */
2705 tree
2706 build_vms_descriptor (tree type, Mechanism_Type mech, Entity_Id gnat_entity)
2708 tree record_type = make_node (RECORD_TYPE);
2709 tree pointer64_type;
2710 tree field_list = NULL_TREE;
2711 int klass, ndim, i, dtype = 0;
2712 tree inner_type, tem;
2713 tree *idx_arr;
2715 /* If TYPE is an unconstrained array, use the underlying array type. */
2716 if (TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
2717 type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (type))));
2719 /* If this is an array, compute the number of dimensions in the array,
2720 get the index types, and point to the inner type. */
2721 if (TREE_CODE (type) != ARRAY_TYPE)
2722 ndim = 0;
2723 else
2724 for (ndim = 1, inner_type = type;
2725 TREE_CODE (TREE_TYPE (inner_type)) == ARRAY_TYPE
2726 && TYPE_MULTI_ARRAY_P (TREE_TYPE (inner_type));
2727 ndim++, inner_type = TREE_TYPE (inner_type))
2730 idx_arr = XALLOCAVEC (tree, ndim);
2732 if (mech != By_Descriptor_NCA
2733 && TREE_CODE (type) == ARRAY_TYPE && TYPE_CONVENTION_FORTRAN_P (type))
2734 for (i = ndim - 1, inner_type = type;
2735 i >= 0;
2736 i--, inner_type = TREE_TYPE (inner_type))
2737 idx_arr[i] = TYPE_DOMAIN (inner_type);
2738 else
2739 for (i = 0, inner_type = type;
2740 i < ndim;
2741 i++, inner_type = TREE_TYPE (inner_type))
2742 idx_arr[i] = TYPE_DOMAIN (inner_type);
2744 /* Now get the DTYPE value. */
2745 switch (TREE_CODE (type))
2747 case INTEGER_TYPE:
2748 case ENUMERAL_TYPE:
2749 case BOOLEAN_TYPE:
2750 if (TYPE_VAX_FLOATING_POINT_P (type))
2751 switch (tree_low_cst (TYPE_DIGITS_VALUE (type), 1))
2753 case 6:
2754 dtype = 10;
2755 break;
2756 case 9:
2757 dtype = 11;
2758 break;
2759 case 15:
2760 dtype = 27;
2761 break;
2763 else
2764 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
2766 case 8:
2767 dtype = TYPE_UNSIGNED (type) ? 2 : 6;
2768 break;
2769 case 16:
2770 dtype = TYPE_UNSIGNED (type) ? 3 : 7;
2771 break;
2772 case 32:
2773 dtype = TYPE_UNSIGNED (type) ? 4 : 8;
2774 break;
2775 case 64:
2776 dtype = TYPE_UNSIGNED (type) ? 5 : 9;
2777 break;
2778 case 128:
2779 dtype = TYPE_UNSIGNED (type) ? 25 : 26;
2780 break;
2782 break;
2784 case REAL_TYPE:
2785 dtype = GET_MODE_BITSIZE (TYPE_MODE (type)) == 32 ? 52 : 53;
2786 break;
2788 case COMPLEX_TYPE:
2789 if (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
2790 && TYPE_VAX_FLOATING_POINT_P (type))
2791 switch (tree_low_cst (TYPE_DIGITS_VALUE (type), 1))
2793 case 6:
2794 dtype = 12;
2795 break;
2796 case 9:
2797 dtype = 13;
2798 break;
2799 case 15:
2800 dtype = 29;
2802 else
2803 dtype = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) == 32 ? 54: 55;
2804 break;
2806 case ARRAY_TYPE:
2807 dtype = 14;
2808 break;
2810 default:
2811 break;
2814 /* Get the CLASS value. */
2815 switch (mech)
2817 case By_Descriptor_A:
2818 klass = 4;
2819 break;
2820 case By_Descriptor_NCA:
2821 klass = 10;
2822 break;
2823 case By_Descriptor_SB:
2824 klass = 15;
2825 break;
2826 case By_Descriptor:
2827 case By_Descriptor_S:
2828 default:
2829 klass = 1;
2830 break;
2833 /* Make the type for a 64-bit descriptor for VMS. The first six fields
2834 are the same for all types. */
2835 field_list
2836 = make_descriptor_field ("MBO", gnat_type_for_size (16, 1),
2837 record_type, size_int (1), field_list);
2838 field_list
2839 = make_descriptor_field ("DTYPE", gnat_type_for_size (8, 1),
2840 record_type, size_int (dtype), field_list);
2841 field_list
2842 = make_descriptor_field ("CLASS", gnat_type_for_size (8, 1),
2843 record_type, size_int (klass), field_list);
2844 field_list
2845 = make_descriptor_field ("MBMO", gnat_type_for_size (32, 1),
2846 record_type, ssize_int (-1), field_list);
2847 field_list
2848 = make_descriptor_field ("LENGTH", gnat_type_for_size (64, 1),
2849 record_type,
2850 size_in_bytes (mech == By_Descriptor_A
2851 ? inner_type : type),
2852 field_list);
2854 pointer64_type = build_pointer_type_for_mode (type, DImode, false);
2856 field_list
2857 = make_descriptor_field ("POINTER", pointer64_type, record_type,
2858 build_unary_op (ADDR_EXPR, pointer64_type,
2859 build0 (PLACEHOLDER_EXPR, type)),
2860 field_list);
2862 switch (mech)
2864 case By_Descriptor:
2865 case By_Descriptor_S:
2866 break;
2868 case By_Descriptor_SB:
2869 field_list
2870 = make_descriptor_field ("SB_L1", gnat_type_for_size (64, 1),
2871 record_type,
2872 (TREE_CODE (type) == ARRAY_TYPE
2873 ? TYPE_MIN_VALUE (TYPE_DOMAIN (type))
2874 : size_zero_node),
2875 field_list);
2876 field_list
2877 = make_descriptor_field ("SB_U1", gnat_type_for_size (64, 1),
2878 record_type,
2879 (TREE_CODE (type) == ARRAY_TYPE
2880 ? TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2881 : size_zero_node),
2882 field_list);
2883 break;
2885 case By_Descriptor_A:
2886 case By_Descriptor_NCA:
2887 field_list
2888 = make_descriptor_field ("SCALE", gnat_type_for_size (8, 1),
2889 record_type, size_zero_node, field_list);
2891 field_list
2892 = make_descriptor_field ("DIGITS", gnat_type_for_size (8, 1),
2893 record_type, size_zero_node, field_list);
2895 dtype = (mech == By_Descriptor_NCA
2897 /* Set FL_COLUMN, FL_COEFF, and
2898 FL_BOUNDS. */
2899 : (TREE_CODE (type) == ARRAY_TYPE
2900 && TYPE_CONVENTION_FORTRAN_P (type)
2901 ? 224 : 192));
2902 field_list
2903 = make_descriptor_field ("AFLAGS", gnat_type_for_size (8, 1),
2904 record_type, size_int (dtype),
2905 field_list);
2907 field_list
2908 = make_descriptor_field ("DIMCT", gnat_type_for_size (8, 1),
2909 record_type, size_int (ndim), field_list);
2911 field_list
2912 = make_descriptor_field ("MBZ", gnat_type_for_size (32, 1),
2913 record_type, size_int (0), field_list);
2914 field_list
2915 = make_descriptor_field ("ARSIZE", gnat_type_for_size (64, 1),
2916 record_type, size_in_bytes (type),
2917 field_list);
2919 /* Now build a pointer to the 0,0,0... element. */
2920 tem = build0 (PLACEHOLDER_EXPR, type);
2921 for (i = 0, inner_type = type; i < ndim;
2922 i++, inner_type = TREE_TYPE (inner_type))
2923 tem = build4 (ARRAY_REF, TREE_TYPE (inner_type), tem,
2924 convert (TYPE_DOMAIN (inner_type), size_zero_node),
2925 NULL_TREE, NULL_TREE);
2927 field_list
2928 = make_descriptor_field ("A0", pointer64_type, record_type,
2929 build1 (ADDR_EXPR, pointer64_type, tem),
2930 field_list);
2932 /* Next come the addressing coefficients. */
2933 tem = size_one_node;
2934 for (i = 0; i < ndim; i++)
2936 char fname[3];
2937 tree idx_length
2938 = size_binop (MULT_EXPR, tem,
2939 size_binop (PLUS_EXPR,
2940 size_binop (MINUS_EXPR,
2941 TYPE_MAX_VALUE (idx_arr[i]),
2942 TYPE_MIN_VALUE (idx_arr[i])),
2943 size_int (1)));
2945 fname[0] = (mech == By_Descriptor_NCA ? 'S' : 'M');
2946 fname[1] = '0' + i, fname[2] = 0;
2947 field_list
2948 = make_descriptor_field (fname, gnat_type_for_size (64, 1),
2949 record_type, idx_length, field_list);
2951 if (mech == By_Descriptor_NCA)
2952 tem = idx_length;
2955 /* Finally here are the bounds. */
2956 for (i = 0; i < ndim; i++)
2958 char fname[3];
2960 fname[0] = 'L', fname[1] = '0' + i, fname[2] = 0;
2961 field_list
2962 = make_descriptor_field (fname, gnat_type_for_size (64, 1),
2963 record_type,
2964 TYPE_MIN_VALUE (idx_arr[i]), field_list);
2966 fname[0] = 'U';
2967 field_list
2968 = make_descriptor_field (fname, gnat_type_for_size (64, 1),
2969 record_type,
2970 TYPE_MAX_VALUE (idx_arr[i]), field_list);
2972 break;
2974 default:
2975 post_error ("unsupported descriptor type for &", gnat_entity);
2978 TYPE_NAME (record_type) = create_concat_name (gnat_entity, "DESC64");
2979 finish_record_type (record_type, nreverse (field_list), 0, false);
2980 return record_type;
2983 /* Fill in a VMS descriptor of GNU_TYPE for GNU_EXPR and return the result.
2984 GNAT_ACTUAL is the actual parameter for which the descriptor is built. */
2986 tree
2987 fill_vms_descriptor (tree gnu_type, tree gnu_expr, Node_Id gnat_actual)
2989 VEC(constructor_elt,gc) *v = NULL;
2990 tree field;
2992 gnu_expr = maybe_unconstrained_array (gnu_expr);
2993 gnu_expr = gnat_protect_expr (gnu_expr);
2994 gnat_mark_addressable (gnu_expr);
2996 /* We may need to substitute both GNU_EXPR and a CALL_EXPR to the raise CE
2997 routine in case we have a 32-bit descriptor. */
2998 gnu_expr = build2 (COMPOUND_EXPR, void_type_node,
2999 build_call_raise (CE_Range_Check_Failed, gnat_actual,
3000 N_Raise_Constraint_Error),
3001 gnu_expr);
3003 for (field = TYPE_FIELDS (gnu_type); field; field = DECL_CHAIN (field))
3005 tree value
3006 = convert (TREE_TYPE (field),
3007 SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_INITIAL (field),
3008 gnu_expr));
3009 CONSTRUCTOR_APPEND_ELT (v, field, value);
3012 return gnat_build_constructor (gnu_type, v);
3015 /* Convert GNU_EXPR, a pointer to a 64bit VMS descriptor, to GNU_TYPE, a
3016 regular pointer or fat pointer type. GNAT_SUBPROG is the subprogram to
3017 which the VMS descriptor is passed. */
3019 static tree
3020 convert_vms_descriptor64 (tree gnu_type, tree gnu_expr, Entity_Id gnat_subprog)
3022 tree desc_type = TREE_TYPE (TREE_TYPE (gnu_expr));
3023 tree desc = build1 (INDIRECT_REF, desc_type, gnu_expr);
3024 /* The CLASS field is the 3rd field in the descriptor. */
3025 tree klass = DECL_CHAIN (DECL_CHAIN (TYPE_FIELDS (desc_type)));
3026 /* The POINTER field is the 6th field in the descriptor. */
3027 tree pointer = DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (klass)));
3029 /* Retrieve the value of the POINTER field. */
3030 tree gnu_expr64
3031 = build3 (COMPONENT_REF, TREE_TYPE (pointer), desc, pointer, NULL_TREE);
3033 if (POINTER_TYPE_P (gnu_type))
3034 return convert (gnu_type, gnu_expr64);
3036 else if (TYPE_IS_FAT_POINTER_P (gnu_type))
3038 tree p_array_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
3039 tree p_bounds_type = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)));
3040 tree template_type = TREE_TYPE (p_bounds_type);
3041 tree min_field = TYPE_FIELDS (template_type);
3042 tree max_field = TREE_CHAIN (TYPE_FIELDS (template_type));
3043 tree template_tree, template_addr, aflags, dimct, t, u;
3044 /* See the head comment of build_vms_descriptor. */
3045 int iklass = TREE_INT_CST_LOW (DECL_INITIAL (klass));
3046 tree lfield, ufield;
3047 VEC(constructor_elt,gc) *v;
3049 /* Convert POINTER to the pointer-to-array type. */
3050 gnu_expr64 = convert (p_array_type, gnu_expr64);
3052 switch (iklass)
3054 case 1: /* Class S */
3055 case 15: /* Class SB */
3056 /* Build {1, LENGTH} template; LENGTH64 is the 5th field. */
3057 v = VEC_alloc (constructor_elt, gc, 2);
3058 t = DECL_CHAIN (DECL_CHAIN (klass));
3059 t = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
3060 CONSTRUCTOR_APPEND_ELT (v, min_field,
3061 convert (TREE_TYPE (min_field),
3062 integer_one_node));
3063 CONSTRUCTOR_APPEND_ELT (v, max_field,
3064 convert (TREE_TYPE (max_field), t));
3065 template_tree = gnat_build_constructor (template_type, v);
3066 template_addr = build_unary_op (ADDR_EXPR, NULL_TREE, template_tree);
3068 /* For class S, we are done. */
3069 if (iklass == 1)
3070 break;
3072 /* Test that we really have a SB descriptor, like DEC Ada. */
3073 t = build3 (COMPONENT_REF, TREE_TYPE (klass), desc, klass, NULL);
3074 u = convert (TREE_TYPE (klass), DECL_INITIAL (klass));
3075 u = build_binary_op (EQ_EXPR, boolean_type_node, t, u);
3076 /* If so, there is already a template in the descriptor and
3077 it is located right after the POINTER field. The fields are
3078 64bits so they must be repacked. */
3079 t = TREE_CHAIN (pointer);
3080 lfield = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
3081 lfield = convert (TREE_TYPE (TYPE_FIELDS (template_type)), lfield);
3083 t = TREE_CHAIN (t);
3084 ufield = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
3085 ufield = convert
3086 (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (template_type))), ufield);
3088 /* Build the template in the form of a constructor. */
3089 v = VEC_alloc (constructor_elt, gc, 2);
3090 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (template_type), lfield);
3091 CONSTRUCTOR_APPEND_ELT (v, TREE_CHAIN (TYPE_FIELDS (template_type)),
3092 ufield);
3093 template_tree = gnat_build_constructor (template_type, v);
3095 /* Otherwise use the {1, LENGTH} template we build above. */
3096 template_addr = build3 (COND_EXPR, p_bounds_type, u,
3097 build_unary_op (ADDR_EXPR, p_bounds_type,
3098 template_tree),
3099 template_addr);
3100 break;
3102 case 4: /* Class A */
3103 /* The AFLAGS field is the 3rd field after the pointer in the
3104 descriptor. */
3105 t = DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (pointer)));
3106 aflags = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
3107 /* The DIMCT field is the next field in the descriptor after
3108 aflags. */
3109 t = TREE_CHAIN (t);
3110 dimct = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
3111 /* Raise CONSTRAINT_ERROR if either more than 1 dimension
3112 or FL_COEFF or FL_BOUNDS not set. */
3113 u = build_int_cst (TREE_TYPE (aflags), 192);
3114 u = build_binary_op (TRUTH_OR_EXPR, boolean_type_node,
3115 build_binary_op (NE_EXPR, boolean_type_node,
3116 dimct,
3117 convert (TREE_TYPE (dimct),
3118 size_one_node)),
3119 build_binary_op (NE_EXPR, boolean_type_node,
3120 build2 (BIT_AND_EXPR,
3121 TREE_TYPE (aflags),
3122 aflags, u),
3123 u));
3124 /* There is already a template in the descriptor and it is located
3125 in block 3. The fields are 64bits so they must be repacked. */
3126 t = DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (DECL_CHAIN
3127 (t)))));
3128 lfield = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
3129 lfield = convert (TREE_TYPE (TYPE_FIELDS (template_type)), lfield);
3131 t = TREE_CHAIN (t);
3132 ufield = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
3133 ufield = convert
3134 (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (template_type))), ufield);
3136 /* Build the template in the form of a constructor. */
3137 v = VEC_alloc (constructor_elt, gc, 2);
3138 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (template_type), lfield);
3139 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (TYPE_FIELDS (template_type)),
3140 ufield);
3141 template_tree = gnat_build_constructor (template_type, v);
3142 template_tree = build3 (COND_EXPR, template_type, u,
3143 build_call_raise (CE_Length_Check_Failed, Empty,
3144 N_Raise_Constraint_Error),
3145 template_tree);
3146 template_addr
3147 = build_unary_op (ADDR_EXPR, p_bounds_type, template_tree);
3148 break;
3150 case 10: /* Class NCA */
3151 default:
3152 post_error ("unsupported descriptor type for &", gnat_subprog);
3153 template_addr = integer_zero_node;
3154 break;
3157 /* Build the fat pointer in the form of a constructor. */
3158 v = VEC_alloc (constructor_elt, gc, 2);
3159 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (gnu_type), gnu_expr64);
3160 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (TYPE_FIELDS (gnu_type)),
3161 template_addr);
3162 return gnat_build_constructor (gnu_type, v);
3165 else
3166 gcc_unreachable ();
3169 /* Convert GNU_EXPR, a pointer to a 32bit VMS descriptor, to GNU_TYPE, a
3170 regular pointer or fat pointer type. GNAT_SUBPROG is the subprogram to
3171 which the VMS descriptor is passed. */
3173 static tree
3174 convert_vms_descriptor32 (tree gnu_type, tree gnu_expr, Entity_Id gnat_subprog)
3176 tree desc_type = TREE_TYPE (TREE_TYPE (gnu_expr));
3177 tree desc = build1 (INDIRECT_REF, desc_type, gnu_expr);
3178 /* The CLASS field is the 3rd field in the descriptor. */
3179 tree klass = DECL_CHAIN (DECL_CHAIN (TYPE_FIELDS (desc_type)));
3180 /* The POINTER field is the 4th field in the descriptor. */
3181 tree pointer = DECL_CHAIN (klass);
3183 /* Retrieve the value of the POINTER field. */
3184 tree gnu_expr32
3185 = build3 (COMPONENT_REF, TREE_TYPE (pointer), desc, pointer, NULL_TREE);
3187 if (POINTER_TYPE_P (gnu_type))
3188 return convert (gnu_type, gnu_expr32);
3190 else if (TYPE_IS_FAT_POINTER_P (gnu_type))
3192 tree p_array_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
3193 tree p_bounds_type = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)));
3194 tree template_type = TREE_TYPE (p_bounds_type);
3195 tree min_field = TYPE_FIELDS (template_type);
3196 tree max_field = TREE_CHAIN (TYPE_FIELDS (template_type));
3197 tree template_tree, template_addr, aflags, dimct, t, u;
3198 /* See the head comment of build_vms_descriptor. */
3199 int iklass = TREE_INT_CST_LOW (DECL_INITIAL (klass));
3200 VEC(constructor_elt,gc) *v;
3202 /* Convert POINTER to the pointer-to-array type. */
3203 gnu_expr32 = convert (p_array_type, gnu_expr32);
3205 switch (iklass)
3207 case 1: /* Class S */
3208 case 15: /* Class SB */
3209 /* Build {1, LENGTH} template; LENGTH is the 1st field. */
3210 v = VEC_alloc (constructor_elt, gc, 2);
3211 t = TYPE_FIELDS (desc_type);
3212 t = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
3213 CONSTRUCTOR_APPEND_ELT (v, min_field,
3214 convert (TREE_TYPE (min_field),
3215 integer_one_node));
3216 CONSTRUCTOR_APPEND_ELT (v, max_field,
3217 convert (TREE_TYPE (max_field), t));
3218 template_tree = gnat_build_constructor (template_type, v);
3219 template_addr = build_unary_op (ADDR_EXPR, NULL_TREE, template_tree);
3221 /* For class S, we are done. */
3222 if (iklass == 1)
3223 break;
3225 /* Test that we really have a SB descriptor, like DEC Ada. */
3226 t = build3 (COMPONENT_REF, TREE_TYPE (klass), desc, klass, NULL);
3227 u = convert (TREE_TYPE (klass), DECL_INITIAL (klass));
3228 u = build_binary_op (EQ_EXPR, boolean_type_node, t, u);
3229 /* If so, there is already a template in the descriptor and
3230 it is located right after the POINTER field. */
3231 t = TREE_CHAIN (pointer);
3232 template_tree
3233 = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
3234 /* Otherwise use the {1, LENGTH} template we build above. */
3235 template_addr = build3 (COND_EXPR, p_bounds_type, u,
3236 build_unary_op (ADDR_EXPR, p_bounds_type,
3237 template_tree),
3238 template_addr);
3239 break;
3241 case 4: /* Class A */
3242 /* The AFLAGS field is the 7th field in the descriptor. */
3243 t = DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (pointer)));
3244 aflags = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
3245 /* The DIMCT field is the 8th field in the descriptor. */
3246 t = TREE_CHAIN (t);
3247 dimct = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
3248 /* Raise CONSTRAINT_ERROR if either more than 1 dimension
3249 or FL_COEFF or FL_BOUNDS not set. */
3250 u = build_int_cst (TREE_TYPE (aflags), 192);
3251 u = build_binary_op (TRUTH_OR_EXPR, boolean_type_node,
3252 build_binary_op (NE_EXPR, boolean_type_node,
3253 dimct,
3254 convert (TREE_TYPE (dimct),
3255 size_one_node)),
3256 build_binary_op (NE_EXPR, boolean_type_node,
3257 build2 (BIT_AND_EXPR,
3258 TREE_TYPE (aflags),
3259 aflags, u),
3260 u));
3261 /* There is already a template in the descriptor and it is
3262 located at the start of block 3 (12th field). */
3263 t = DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (t))));
3264 template_tree
3265 = build3 (COMPONENT_REF, TREE_TYPE (t), desc, t, NULL_TREE);
3266 template_tree = build3 (COND_EXPR, TREE_TYPE (t), u,
3267 build_call_raise (CE_Length_Check_Failed, Empty,
3268 N_Raise_Constraint_Error),
3269 template_tree);
3270 template_addr
3271 = build_unary_op (ADDR_EXPR, p_bounds_type, template_tree);
3272 break;
3274 case 10: /* Class NCA */
3275 default:
3276 post_error ("unsupported descriptor type for &", gnat_subprog);
3277 template_addr = integer_zero_node;
3278 break;
3281 /* Build the fat pointer in the form of a constructor. */
3282 v = VEC_alloc (constructor_elt, gc, 2);
3283 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (gnu_type), gnu_expr32);
3284 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (TYPE_FIELDS (gnu_type)),
3285 template_addr);
3287 return gnat_build_constructor (gnu_type, v);
3290 else
3291 gcc_unreachable ();
3294 /* Convert GNU_EXPR, a pointer to a VMS descriptor, to GNU_TYPE, a regular
3295 pointer or fat pointer type. GNU_EXPR_ALT_TYPE is the alternate (32-bit)
3296 pointer type of GNU_EXPR. BY_REF is true if the result is to be used by
3297 reference. GNAT_SUBPROG is the subprogram to which the VMS descriptor is
3298 passed. */
3300 static tree
3301 convert_vms_descriptor (tree gnu_type, tree gnu_expr, tree gnu_expr_alt_type,
3302 bool by_ref, Entity_Id gnat_subprog)
3304 tree desc_type = TREE_TYPE (TREE_TYPE (gnu_expr));
3305 tree desc = build1 (INDIRECT_REF, desc_type, gnu_expr);
3306 tree mbo = TYPE_FIELDS (desc_type);
3307 const char *mbostr = IDENTIFIER_POINTER (DECL_NAME (mbo));
3308 tree mbmo = DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (mbo)));
3309 tree real_type, is64bit, gnu_expr32, gnu_expr64;
3311 if (by_ref)
3312 real_type = TREE_TYPE (gnu_type);
3313 else
3314 real_type = gnu_type;
3316 /* If the field name is not MBO, it must be 32-bit and no alternate.
3317 Otherwise primary must be 64-bit and alternate 32-bit. */
3318 if (strcmp (mbostr, "MBO") != 0)
3320 tree ret = convert_vms_descriptor32 (real_type, gnu_expr, gnat_subprog);
3321 if (by_ref)
3322 ret = build_unary_op (ADDR_EXPR, gnu_type, ret);
3323 return ret;
3326 /* Build the test for 64-bit descriptor. */
3327 mbo = build3 (COMPONENT_REF, TREE_TYPE (mbo), desc, mbo, NULL_TREE);
3328 mbmo = build3 (COMPONENT_REF, TREE_TYPE (mbmo), desc, mbmo, NULL_TREE);
3329 is64bit
3330 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
3331 build_binary_op (EQ_EXPR, boolean_type_node,
3332 convert (integer_type_node, mbo),
3333 integer_one_node),
3334 build_binary_op (EQ_EXPR, boolean_type_node,
3335 convert (integer_type_node, mbmo),
3336 integer_minus_one_node));
3338 /* Build the 2 possible end results. */
3339 gnu_expr64 = convert_vms_descriptor64 (real_type, gnu_expr, gnat_subprog);
3340 if (by_ref)
3341 gnu_expr64 = build_unary_op (ADDR_EXPR, gnu_type, gnu_expr64);
3342 gnu_expr = fold_convert (gnu_expr_alt_type, gnu_expr);
3343 gnu_expr32 = convert_vms_descriptor32 (real_type, gnu_expr, gnat_subprog);
3344 if (by_ref)
3345 gnu_expr32 = build_unary_op (ADDR_EXPR, gnu_type, gnu_expr32);
3347 return build3 (COND_EXPR, gnu_type, is64bit, gnu_expr64, gnu_expr32);
3350 /* Build a stub for the subprogram specified by the GCC tree GNU_SUBPROG
3351 and the GNAT node GNAT_SUBPROG. */
3353 void
3354 build_function_stub (tree gnu_subprog, Entity_Id gnat_subprog)
3356 tree gnu_subprog_type, gnu_subprog_addr, gnu_subprog_call;
3357 tree gnu_subprog_param, gnu_stub_param, gnu_param;
3358 tree gnu_stub_decl = DECL_FUNCTION_STUB (gnu_subprog);
3359 VEC(tree,gc) *gnu_param_vec = NULL;
3361 gnu_subprog_type = TREE_TYPE (gnu_subprog);
3363 /* Initialize the information structure for the function. */
3364 allocate_struct_function (gnu_stub_decl, false);
3365 set_cfun (NULL);
3367 begin_subprog_body (gnu_stub_decl);
3369 start_stmt_group ();
3370 gnat_pushlevel ();
3372 /* Loop over the parameters of the stub and translate any of them
3373 passed by descriptor into a by reference one. */
3374 for (gnu_stub_param = DECL_ARGUMENTS (gnu_stub_decl),
3375 gnu_subprog_param = DECL_ARGUMENTS (gnu_subprog);
3376 gnu_stub_param;
3377 gnu_stub_param = TREE_CHAIN (gnu_stub_param),
3378 gnu_subprog_param = TREE_CHAIN (gnu_subprog_param))
3380 if (DECL_BY_DESCRIPTOR_P (gnu_stub_param))
3382 gcc_assert (DECL_BY_REF_P (gnu_subprog_param));
3383 gnu_param
3384 = convert_vms_descriptor (TREE_TYPE (gnu_subprog_param),
3385 gnu_stub_param,
3386 DECL_PARM_ALT_TYPE (gnu_stub_param),
3387 DECL_BY_DOUBLE_REF_P (gnu_subprog_param),
3388 gnat_subprog);
3390 else
3391 gnu_param = gnu_stub_param;
3393 VEC_safe_push (tree, gc, gnu_param_vec, gnu_param);
3396 /* Invoke the internal subprogram. */
3397 gnu_subprog_addr = build1 (ADDR_EXPR, build_pointer_type (gnu_subprog_type),
3398 gnu_subprog);
3399 gnu_subprog_call = build_call_vec (TREE_TYPE (gnu_subprog_type),
3400 gnu_subprog_addr, gnu_param_vec);
3402 /* Propagate the return value, if any. */
3403 if (VOID_TYPE_P (TREE_TYPE (gnu_subprog_type)))
3404 add_stmt (gnu_subprog_call);
3405 else
3406 add_stmt (build_return_expr (DECL_RESULT (gnu_stub_decl),
3407 gnu_subprog_call));
3409 gnat_poplevel ();
3410 end_subprog_body (end_stmt_group ());
3413 /* Build a type to be used to represent an aliased object whose nominal type
3414 is an unconstrained array. This consists of a RECORD_TYPE containing a
3415 field of TEMPLATE_TYPE and a field of OBJECT_TYPE, which is an ARRAY_TYPE.
3416 If ARRAY_TYPE is that of an unconstrained array, this is used to represent
3417 an arbitrary unconstrained object. Use NAME as the name of the record.
3418 DEBUG_INFO_P is true if we need to write debug information for the type. */
3420 tree
3421 build_unc_object_type (tree template_type, tree object_type, tree name,
3422 bool debug_info_p)
3424 tree type = make_node (RECORD_TYPE);
3425 tree template_field
3426 = create_field_decl (get_identifier ("BOUNDS"), template_type, type,
3427 NULL_TREE, NULL_TREE, 0, 1);
3428 tree array_field
3429 = create_field_decl (get_identifier ("ARRAY"), object_type, type,
3430 NULL_TREE, NULL_TREE, 0, 1);
3432 TYPE_NAME (type) = name;
3433 TYPE_CONTAINS_TEMPLATE_P (type) = 1;
3434 DECL_CHAIN (template_field) = array_field;
3435 finish_record_type (type, template_field, 0, true);
3437 /* Declare it now since it will never be declared otherwise. This is
3438 necessary to ensure that its subtrees are properly marked. */
3439 create_type_decl (name, type, NULL, true, debug_info_p, Empty);
3441 return type;
3444 /* Same, taking a thin or fat pointer type instead of a template type. */
3446 tree
3447 build_unc_object_type_from_ptr (tree thin_fat_ptr_type, tree object_type,
3448 tree name, bool debug_info_p)
3450 tree template_type;
3452 gcc_assert (TYPE_IS_FAT_OR_THIN_POINTER_P (thin_fat_ptr_type));
3454 template_type
3455 = (TYPE_IS_FAT_POINTER_P (thin_fat_ptr_type)
3456 ? TREE_TYPE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (thin_fat_ptr_type))))
3457 : TREE_TYPE (TYPE_FIELDS (TREE_TYPE (thin_fat_ptr_type))));
3459 return
3460 build_unc_object_type (template_type, object_type, name, debug_info_p);
3463 /* Shift the component offsets within an unconstrained object TYPE to make it
3464 suitable for use as a designated type for thin pointers. */
3466 void
3467 shift_unc_components_for_thin_pointers (tree type)
3469 /* Thin pointer values designate the ARRAY data of an unconstrained object,
3470 allocated past the BOUNDS template. The designated type is adjusted to
3471 have ARRAY at position zero and the template at a negative offset, so
3472 that COMPONENT_REFs on (*thin_ptr) designate the proper location. */
3474 tree bounds_field = TYPE_FIELDS (type);
3475 tree array_field = DECL_CHAIN (TYPE_FIELDS (type));
3477 DECL_FIELD_OFFSET (bounds_field)
3478 = size_binop (MINUS_EXPR, size_zero_node, byte_position (array_field));
3480 DECL_FIELD_OFFSET (array_field) = size_zero_node;
3481 DECL_FIELD_BIT_OFFSET (array_field) = bitsize_zero_node;
3484 /* Update anything previously pointing to OLD_TYPE to point to NEW_TYPE.
3485 In the normal case this is just two adjustments, but we have more to
3486 do if NEW_TYPE is an UNCONSTRAINED_ARRAY_TYPE. */
3488 void
3489 update_pointer_to (tree old_type, tree new_type)
3491 tree ptr = TYPE_POINTER_TO (old_type);
3492 tree ref = TYPE_REFERENCE_TO (old_type);
3493 tree t;
3495 /* If this is the main variant, process all the other variants first. */
3496 if (TYPE_MAIN_VARIANT (old_type) == old_type)
3497 for (t = TYPE_NEXT_VARIANT (old_type); t; t = TYPE_NEXT_VARIANT (t))
3498 update_pointer_to (t, new_type);
3500 /* If no pointers and no references, we are done. */
3501 if (!ptr && !ref)
3502 return;
3504 /* Merge the old type qualifiers in the new type.
3506 Each old variant has qualifiers for specific reasons, and the new
3507 designated type as well. Each set of qualifiers represents useful
3508 information grabbed at some point, and merging the two simply unifies
3509 these inputs into the final type description.
3511 Consider for instance a volatile type frozen after an access to constant
3512 type designating it; after the designated type's freeze, we get here with
3513 a volatile NEW_TYPE and a dummy OLD_TYPE with a readonly variant, created
3514 when the access type was processed. We will make a volatile and readonly
3515 designated type, because that's what it really is.
3517 We might also get here for a non-dummy OLD_TYPE variant with different
3518 qualifiers than those of NEW_TYPE, for instance in some cases of pointers
3519 to private record type elaboration (see the comments around the call to
3520 this routine in gnat_to_gnu_entity <E_Access_Type>). We have to merge
3521 the qualifiers in those cases too, to avoid accidentally discarding the
3522 initial set, and will often end up with OLD_TYPE == NEW_TYPE then. */
3523 new_type
3524 = build_qualified_type (new_type,
3525 TYPE_QUALS (old_type) | TYPE_QUALS (new_type));
3527 /* If old type and new type are identical, there is nothing to do. */
3528 if (old_type == new_type)
3529 return;
3531 /* Otherwise, first handle the simple case. */
3532 if (TREE_CODE (new_type) != UNCONSTRAINED_ARRAY_TYPE)
3534 tree new_ptr, new_ref;
3536 /* If pointer or reference already points to new type, nothing to do.
3537 This can happen as update_pointer_to can be invoked multiple times
3538 on the same couple of types because of the type variants. */
3539 if ((ptr && TREE_TYPE (ptr) == new_type)
3540 || (ref && TREE_TYPE (ref) == new_type))
3541 return;
3543 /* Chain PTR and its variants at the end. */
3544 new_ptr = TYPE_POINTER_TO (new_type);
3545 if (new_ptr)
3547 while (TYPE_NEXT_PTR_TO (new_ptr))
3548 new_ptr = TYPE_NEXT_PTR_TO (new_ptr);
3549 TYPE_NEXT_PTR_TO (new_ptr) = ptr;
3551 else
3552 TYPE_POINTER_TO (new_type) = ptr;
3554 /* Now adjust them. */
3555 for (; ptr; ptr = TYPE_NEXT_PTR_TO (ptr))
3556 for (t = TYPE_MAIN_VARIANT (ptr); t; t = TYPE_NEXT_VARIANT (t))
3557 TREE_TYPE (t) = new_type;
3558 TYPE_POINTER_TO (old_type) = NULL_TREE;
3560 /* Chain REF and its variants at the end. */
3561 new_ref = TYPE_REFERENCE_TO (new_type);
3562 if (new_ref)
3564 while (TYPE_NEXT_REF_TO (new_ref))
3565 new_ref = TYPE_NEXT_REF_TO (new_ref);
3566 TYPE_NEXT_REF_TO (new_ref) = ref;
3568 else
3569 TYPE_REFERENCE_TO (new_type) = ref;
3571 /* Now adjust them. */
3572 for (; ref; ref = TYPE_NEXT_REF_TO (ref))
3573 for (t = TYPE_MAIN_VARIANT (ref); t; t = TYPE_NEXT_VARIANT (t))
3574 TREE_TYPE (t) = new_type;
3575 TYPE_REFERENCE_TO (old_type) = NULL_TREE;
3578 /* Now deal with the unconstrained array case. In this case the pointer
3579 is actually a record where both fields are pointers to dummy nodes.
3580 Turn them into pointers to the correct types using update_pointer_to.
3581 Likewise for the pointer to the object record (thin pointer). */
3582 else
3584 tree new_ptr = TYPE_POINTER_TO (new_type);
3586 gcc_assert (TYPE_IS_FAT_POINTER_P (ptr));
3588 /* If PTR already points to NEW_TYPE, nothing to do. This can happen
3589 since update_pointer_to can be invoked multiple times on the same
3590 couple of types because of the type variants. */
3591 if (TYPE_UNCONSTRAINED_ARRAY (ptr) == new_type)
3592 return;
3594 update_pointer_to
3595 (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (ptr))),
3596 TREE_TYPE (TREE_TYPE (TYPE_FIELDS (new_ptr))));
3598 update_pointer_to
3599 (TREE_TYPE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (ptr)))),
3600 TREE_TYPE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (new_ptr)))));
3602 update_pointer_to (TYPE_OBJECT_RECORD_TYPE (old_type),
3603 TYPE_OBJECT_RECORD_TYPE (new_type));
3605 TYPE_POINTER_TO (old_type) = NULL_TREE;
3609 /* Convert EXPR, a pointer to a constrained array, into a pointer to an
3610 unconstrained one. This involves making or finding a template. */
3612 static tree
3613 convert_to_fat_pointer (tree type, tree expr)
3615 tree template_type = TREE_TYPE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type))));
3616 tree p_array_type = TREE_TYPE (TYPE_FIELDS (type));
3617 tree etype = TREE_TYPE (expr);
3618 tree template_tree;
3619 VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, 2);
3621 /* If EXPR is null, make a fat pointer that contains null pointers to the
3622 template and array. */
3623 if (integer_zerop (expr))
3625 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type),
3626 convert (p_array_type, expr));
3627 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (TYPE_FIELDS (type)),
3628 convert (build_pointer_type (template_type),
3629 expr));
3630 return gnat_build_constructor (type, v);
3633 /* If EXPR is a thin pointer, make template and data from the record.. */
3634 else if (TYPE_IS_THIN_POINTER_P (etype))
3636 tree fields = TYPE_FIELDS (TREE_TYPE (etype));
3638 expr = gnat_protect_expr (expr);
3639 if (TREE_CODE (expr) == ADDR_EXPR)
3640 expr = TREE_OPERAND (expr, 0);
3641 else
3642 expr = build1 (INDIRECT_REF, TREE_TYPE (etype), expr);
3644 template_tree = build_component_ref (expr, NULL_TREE, fields, false);
3645 expr = build_unary_op (ADDR_EXPR, NULL_TREE,
3646 build_component_ref (expr, NULL_TREE,
3647 DECL_CHAIN (fields), false));
3650 /* Otherwise, build the constructor for the template. */
3651 else
3652 template_tree = build_template (template_type, TREE_TYPE (etype), expr);
3654 /* The final result is a constructor for the fat pointer.
3656 If EXPR is an argument of a foreign convention subprogram, the type it
3657 points to is directly the component type. In this case, the expression
3658 type may not match the corresponding FIELD_DECL type at this point, so we
3659 call "convert" here to fix that up if necessary. This type consistency is
3660 required, for instance because it ensures that possible later folding of
3661 COMPONENT_REFs against this constructor always yields something of the
3662 same type as the initial reference.
3664 Note that the call to "build_template" above is still fine because it
3665 will only refer to the provided TEMPLATE_TYPE in this case. */
3666 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type),
3667 convert (p_array_type, expr));
3668 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (TYPE_FIELDS (type)),
3669 build_unary_op (ADDR_EXPR, NULL_TREE,
3670 template_tree));
3671 return gnat_build_constructor (type, v);
3674 /* Convert to a thin pointer type, TYPE. The only thing we know how to convert
3675 is something that is a fat pointer, so convert to it first if it EXPR
3676 is not already a fat pointer. */
3678 static tree
3679 convert_to_thin_pointer (tree type, tree expr)
3681 if (!TYPE_IS_FAT_POINTER_P (TREE_TYPE (expr)))
3682 expr
3683 = convert_to_fat_pointer
3684 (TREE_TYPE (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type))), expr);
3686 /* We get the pointer to the data and use a NOP_EXPR to make it the
3687 proper GCC type. */
3688 expr = build_component_ref (expr, NULL_TREE, TYPE_FIELDS (TREE_TYPE (expr)),
3689 false);
3690 expr = build1 (NOP_EXPR, type, expr);
3692 return expr;
3695 /* Create an expression whose value is that of EXPR,
3696 converted to type TYPE. The TREE_TYPE of the value
3697 is always TYPE. This function implements all reasonable
3698 conversions; callers should filter out those that are
3699 not permitted by the language being compiled. */
3701 tree
3702 convert (tree type, tree expr)
3704 tree etype = TREE_TYPE (expr);
3705 enum tree_code ecode = TREE_CODE (etype);
3706 enum tree_code code = TREE_CODE (type);
3708 /* If the expression is already of the right type, we are done. */
3709 if (etype == type)
3710 return expr;
3712 /* If both input and output have padding and are of variable size, do this
3713 as an unchecked conversion. Likewise if one is a mere variant of the
3714 other, so we avoid a pointless unpad/repad sequence. */
3715 else if (code == RECORD_TYPE && ecode == RECORD_TYPE
3716 && TYPE_PADDING_P (type) && TYPE_PADDING_P (etype)
3717 && (!TREE_CONSTANT (TYPE_SIZE (type))
3718 || !TREE_CONSTANT (TYPE_SIZE (etype))
3719 || gnat_types_compatible_p (type, etype)
3720 || TYPE_NAME (TREE_TYPE (TYPE_FIELDS (type)))
3721 == TYPE_NAME (TREE_TYPE (TYPE_FIELDS (etype)))))
3724 /* If the output type has padding, convert to the inner type and make a
3725 constructor to build the record, unless a variable size is involved. */
3726 else if (code == RECORD_TYPE && TYPE_PADDING_P (type))
3728 VEC(constructor_elt,gc) *v;
3730 /* If we previously converted from another type and our type is
3731 of variable size, remove the conversion to avoid the need for
3732 variable-sized temporaries. Likewise for a conversion between
3733 original and packable version. */
3734 if (TREE_CODE (expr) == VIEW_CONVERT_EXPR
3735 && (!TREE_CONSTANT (TYPE_SIZE (type))
3736 || (ecode == RECORD_TYPE
3737 && TYPE_NAME (etype)
3738 == TYPE_NAME (TREE_TYPE (TREE_OPERAND (expr, 0))))))
3739 expr = TREE_OPERAND (expr, 0);
3741 /* If we are just removing the padding from expr, convert the original
3742 object if we have variable size in order to avoid the need for some
3743 variable-sized temporaries. Likewise if the padding is a variant
3744 of the other, so we avoid a pointless unpad/repad sequence. */
3745 if (TREE_CODE (expr) == COMPONENT_REF
3746 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (expr, 0)))
3747 && (!TREE_CONSTANT (TYPE_SIZE (type))
3748 || gnat_types_compatible_p (type,
3749 TREE_TYPE (TREE_OPERAND (expr, 0)))
3750 || (ecode == RECORD_TYPE
3751 && TYPE_NAME (etype)
3752 == TYPE_NAME (TREE_TYPE (TYPE_FIELDS (type))))))
3753 return convert (type, TREE_OPERAND (expr, 0));
3755 /* If the inner type is of self-referential size and the expression type
3756 is a record, do this as an unchecked conversion. But first pad the
3757 expression if possible to have the same size on both sides. */
3758 if (ecode == RECORD_TYPE
3759 && CONTAINS_PLACEHOLDER_P (DECL_SIZE (TYPE_FIELDS (type))))
3761 if (TREE_CODE (TYPE_SIZE (etype)) == INTEGER_CST)
3762 expr = convert (maybe_pad_type (etype, TYPE_SIZE (type), 0, Empty,
3763 false, false, false, true),
3764 expr);
3765 return unchecked_convert (type, expr, false);
3768 /* If we are converting between array types with variable size, do the
3769 final conversion as an unchecked conversion, again to avoid the need
3770 for some variable-sized temporaries. If valid, this conversion is
3771 very likely purely technical and without real effects. */
3772 if (ecode == ARRAY_TYPE
3773 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (type))) == ARRAY_TYPE
3774 && !TREE_CONSTANT (TYPE_SIZE (etype))
3775 && !TREE_CONSTANT (TYPE_SIZE (type)))
3776 return unchecked_convert (type,
3777 convert (TREE_TYPE (TYPE_FIELDS (type)),
3778 expr),
3779 false);
3781 v = VEC_alloc (constructor_elt, gc, 1);
3782 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type),
3783 convert (TREE_TYPE (TYPE_FIELDS (type)), expr));
3784 return gnat_build_constructor (type, v);
3787 /* If the input type has padding, remove it and convert to the output type.
3788 The conditions ordering is arranged to ensure that the output type is not
3789 a padding type here, as it is not clear whether the conversion would
3790 always be correct if this was to happen. */
3791 else if (ecode == RECORD_TYPE && TYPE_PADDING_P (etype))
3793 tree unpadded;
3795 /* If we have just converted to this padded type, just get the
3796 inner expression. */
3797 if (TREE_CODE (expr) == CONSTRUCTOR
3798 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (expr))
3799 && VEC_index (constructor_elt, CONSTRUCTOR_ELTS (expr), 0)->index
3800 == TYPE_FIELDS (etype))
3801 unpadded
3802 = VEC_index (constructor_elt, CONSTRUCTOR_ELTS (expr), 0)->value;
3804 /* Otherwise, build an explicit component reference. */
3805 else
3806 unpadded
3807 = build_component_ref (expr, NULL_TREE, TYPE_FIELDS (etype), false);
3809 return convert (type, unpadded);
3812 /* If the input is a biased type, adjust first. */
3813 if (ecode == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (etype))
3814 return convert (type, fold_build2 (PLUS_EXPR, TREE_TYPE (etype),
3815 fold_convert (TREE_TYPE (etype),
3816 expr),
3817 TYPE_MIN_VALUE (etype)));
3819 /* If the input is a justified modular type, we need to extract the actual
3820 object before converting it to any other type with the exceptions of an
3821 unconstrained array or of a mere type variant. It is useful to avoid the
3822 extraction and conversion in the type variant case because it could end
3823 up replacing a VAR_DECL expr by a constructor and we might be about the
3824 take the address of the result. */
3825 if (ecode == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (etype)
3826 && code != UNCONSTRAINED_ARRAY_TYPE
3827 && TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (etype))
3828 return convert (type, build_component_ref (expr, NULL_TREE,
3829 TYPE_FIELDS (etype), false));
3831 /* If converting to a type that contains a template, convert to the data
3832 type and then build the template. */
3833 if (code == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (type))
3835 tree obj_type = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type)));
3836 VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, 2);
3838 /* If the source already has a template, get a reference to the
3839 associated array only, as we are going to rebuild a template
3840 for the target type anyway. */
3841 expr = maybe_unconstrained_array (expr);
3843 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type),
3844 build_template (TREE_TYPE (TYPE_FIELDS (type)),
3845 obj_type, NULL_TREE));
3846 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (TYPE_FIELDS (type)),
3847 convert (obj_type, expr));
3848 return gnat_build_constructor (type, v);
3851 /* There are some special cases of expressions that we process
3852 specially. */
3853 switch (TREE_CODE (expr))
3855 case ERROR_MARK:
3856 return expr;
3858 case NULL_EXPR:
3859 /* Just set its type here. For TRANSFORM_EXPR, we will do the actual
3860 conversion in gnat_expand_expr. NULL_EXPR does not represent
3861 and actual value, so no conversion is needed. */
3862 expr = copy_node (expr);
3863 TREE_TYPE (expr) = type;
3864 return expr;
3866 case STRING_CST:
3867 /* If we are converting a STRING_CST to another constrained array type,
3868 just make a new one in the proper type. */
3869 if (code == ecode && AGGREGATE_TYPE_P (etype)
3870 && !(TREE_CODE (TYPE_SIZE (etype)) == INTEGER_CST
3871 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
3873 expr = copy_node (expr);
3874 TREE_TYPE (expr) = type;
3875 return expr;
3877 break;
3879 case VECTOR_CST:
3880 /* If we are converting a VECTOR_CST to a mere variant type, just make
3881 a new one in the proper type. */
3882 if (code == ecode && gnat_types_compatible_p (type, etype))
3884 expr = copy_node (expr);
3885 TREE_TYPE (expr) = type;
3886 return expr;
3889 case CONSTRUCTOR:
3890 /* If we are converting a CONSTRUCTOR to a mere variant type, just make
3891 a new one in the proper type. */
3892 if (code == ecode && gnat_types_compatible_p (type, etype))
3894 expr = copy_node (expr);
3895 TREE_TYPE (expr) = type;
3896 return expr;
3899 /* Likewise for a conversion between original and packable version, or
3900 conversion between types of the same size and with the same list of
3901 fields, but we have to work harder to preserve type consistency. */
3902 if (code == ecode
3903 && code == RECORD_TYPE
3904 && (TYPE_NAME (type) == TYPE_NAME (etype)
3905 || tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (etype))))
3908 VEC(constructor_elt,gc) *e = CONSTRUCTOR_ELTS (expr);
3909 unsigned HOST_WIDE_INT len = VEC_length (constructor_elt, e);
3910 VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, len);
3911 tree efield = TYPE_FIELDS (etype), field = TYPE_FIELDS (type);
3912 unsigned HOST_WIDE_INT idx;
3913 tree index, value;
3915 /* Whether we need to clear TREE_CONSTANT et al. on the output
3916 constructor when we convert in place. */
3917 bool clear_constant = false;
3919 FOR_EACH_CONSTRUCTOR_ELT(e, idx, index, value)
3921 constructor_elt *elt;
3922 /* We expect only simple constructors. */
3923 if (!SAME_FIELD_P (index, efield))
3924 break;
3925 /* The field must be the same. */
3926 if (!SAME_FIELD_P (efield, field))
3927 break;
3928 elt = VEC_quick_push (constructor_elt, v, NULL);
3929 elt->index = field;
3930 elt->value = convert (TREE_TYPE (field), value);
3932 /* If packing has made this field a bitfield and the input
3933 value couldn't be emitted statically any more, we need to
3934 clear TREE_CONSTANT on our output. */
3935 if (!clear_constant
3936 && TREE_CONSTANT (expr)
3937 && !CONSTRUCTOR_BITFIELD_P (efield)
3938 && CONSTRUCTOR_BITFIELD_P (field)
3939 && !initializer_constant_valid_for_bitfield_p (value))
3940 clear_constant = true;
3942 efield = DECL_CHAIN (efield);
3943 field = DECL_CHAIN (field);
3946 /* If we have been able to match and convert all the input fields
3947 to their output type, convert in place now. We'll fallback to a
3948 view conversion downstream otherwise. */
3949 if (idx == len)
3951 expr = copy_node (expr);
3952 TREE_TYPE (expr) = type;
3953 CONSTRUCTOR_ELTS (expr) = v;
3954 if (clear_constant)
3955 TREE_CONSTANT (expr) = TREE_STATIC (expr) = 0;
3956 return expr;
3960 /* Likewise for a conversion between array type and vector type with a
3961 compatible representative array. */
3962 else if (code == VECTOR_TYPE
3963 && ecode == ARRAY_TYPE
3964 && gnat_types_compatible_p (TYPE_REPRESENTATIVE_ARRAY (type),
3965 etype))
3967 VEC(constructor_elt,gc) *e = CONSTRUCTOR_ELTS (expr);
3968 unsigned HOST_WIDE_INT len = VEC_length (constructor_elt, e);
3969 VEC(constructor_elt,gc) *v;
3970 unsigned HOST_WIDE_INT ix;
3971 tree value;
3973 /* Build a VECTOR_CST from a *constant* array constructor. */
3974 if (TREE_CONSTANT (expr))
3976 bool constant_p = true;
3978 /* Iterate through elements and check if all constructor
3979 elements are *_CSTs. */
3980 FOR_EACH_CONSTRUCTOR_VALUE (e, ix, value)
3981 if (!CONSTANT_CLASS_P (value))
3983 constant_p = false;
3984 break;
3987 if (constant_p)
3988 return build_vector_from_ctor (type,
3989 CONSTRUCTOR_ELTS (expr));
3992 /* Otherwise, build a regular vector constructor. */
3993 v = VEC_alloc (constructor_elt, gc, len);
3994 FOR_EACH_CONSTRUCTOR_VALUE (e, ix, value)
3996 constructor_elt *elt = VEC_quick_push (constructor_elt, v, NULL);
3997 elt->index = NULL_TREE;
3998 elt->value = value;
4000 expr = copy_node (expr);
4001 TREE_TYPE (expr) = type;
4002 CONSTRUCTOR_ELTS (expr) = v;
4003 return expr;
4005 break;
4007 case UNCONSTRAINED_ARRAY_REF:
4008 /* Convert this to the type of the inner array by getting the address of
4009 the array from the template. */
4010 expr = TREE_OPERAND (expr, 0);
4011 expr = build_unary_op (INDIRECT_REF, NULL_TREE,
4012 build_component_ref (expr, NULL_TREE,
4013 TYPE_FIELDS
4014 (TREE_TYPE (expr)),
4015 false));
4016 etype = TREE_TYPE (expr);
4017 ecode = TREE_CODE (etype);
4018 break;
4020 case VIEW_CONVERT_EXPR:
4022 /* GCC 4.x is very sensitive to type consistency overall, and view
4023 conversions thus are very frequent. Even though just "convert"ing
4024 the inner operand to the output type is fine in most cases, it
4025 might expose unexpected input/output type mismatches in special
4026 circumstances so we avoid such recursive calls when we can. */
4027 tree op0 = TREE_OPERAND (expr, 0);
4029 /* If we are converting back to the original type, we can just
4030 lift the input conversion. This is a common occurrence with
4031 switches back-and-forth amongst type variants. */
4032 if (type == TREE_TYPE (op0))
4033 return op0;
4035 /* Otherwise, if we're converting between two aggregate or vector
4036 types, we might be allowed to substitute the VIEW_CONVERT_EXPR
4037 target type in place or to just convert the inner expression. */
4038 if ((AGGREGATE_TYPE_P (type) && AGGREGATE_TYPE_P (etype))
4039 || (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (etype)))
4041 /* If we are converting between mere variants, we can just
4042 substitute the VIEW_CONVERT_EXPR in place. */
4043 if (gnat_types_compatible_p (type, etype))
4044 return build1 (VIEW_CONVERT_EXPR, type, op0);
4046 /* Otherwise, we may just bypass the input view conversion unless
4047 one of the types is a fat pointer, which is handled by
4048 specialized code below which relies on exact type matching. */
4049 else if (!TYPE_IS_FAT_POINTER_P (type)
4050 && !TYPE_IS_FAT_POINTER_P (etype))
4051 return convert (type, op0);
4054 break;
4056 default:
4057 break;
4060 /* Check for converting to a pointer to an unconstrained array. */
4061 if (TYPE_IS_FAT_POINTER_P (type) && !TYPE_IS_FAT_POINTER_P (etype))
4062 return convert_to_fat_pointer (type, expr);
4064 /* If we are converting between two aggregate or vector types that are mere
4065 variants, just make a VIEW_CONVERT_EXPR. Likewise when we are converting
4066 to a vector type from its representative array type. */
4067 else if ((code == ecode
4068 && (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
4069 && gnat_types_compatible_p (type, etype))
4070 || (code == VECTOR_TYPE
4071 && ecode == ARRAY_TYPE
4072 && gnat_types_compatible_p (TYPE_REPRESENTATIVE_ARRAY (type),
4073 etype)))
4074 return build1 (VIEW_CONVERT_EXPR, type, expr);
4076 /* If we are converting between tagged types, try to upcast properly. */
4077 else if (ecode == RECORD_TYPE && code == RECORD_TYPE
4078 && TYPE_ALIGN_OK (etype) && TYPE_ALIGN_OK (type))
4080 tree child_etype = etype;
4081 do {
4082 tree field = TYPE_FIELDS (child_etype);
4083 if (DECL_NAME (field) == parent_name_id && TREE_TYPE (field) == type)
4084 return build_component_ref (expr, NULL_TREE, field, false);
4085 child_etype = TREE_TYPE (field);
4086 } while (TREE_CODE (child_etype) == RECORD_TYPE);
4089 /* If we are converting from a smaller form of record type back to it, just
4090 make a VIEW_CONVERT_EXPR. But first pad the expression to have the same
4091 size on both sides. */
4092 else if (ecode == RECORD_TYPE && code == RECORD_TYPE
4093 && smaller_form_type_p (etype, type))
4095 expr = convert (maybe_pad_type (etype, TYPE_SIZE (type), 0, Empty,
4096 false, false, false, true),
4097 expr);
4098 return build1 (VIEW_CONVERT_EXPR, type, expr);
4101 /* In all other cases of related types, make a NOP_EXPR. */
4102 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (etype))
4103 return fold_convert (type, expr);
4105 switch (code)
4107 case VOID_TYPE:
4108 return fold_build1 (CONVERT_EXPR, type, expr);
4110 case INTEGER_TYPE:
4111 if (TYPE_HAS_ACTUAL_BOUNDS_P (type)
4112 && (ecode == ARRAY_TYPE || ecode == UNCONSTRAINED_ARRAY_TYPE
4113 || (ecode == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (etype))))
4114 return unchecked_convert (type, expr, false);
4115 else if (TYPE_BIASED_REPRESENTATION_P (type))
4116 return fold_convert (type,
4117 fold_build2 (MINUS_EXPR, TREE_TYPE (type),
4118 convert (TREE_TYPE (type), expr),
4119 TYPE_MIN_VALUE (type)));
4121 /* ... fall through ... */
4123 case ENUMERAL_TYPE:
4124 case BOOLEAN_TYPE:
4125 /* If we are converting an additive expression to an integer type
4126 with lower precision, be wary of the optimization that can be
4127 applied by convert_to_integer. There are 2 problematic cases:
4128 - if the first operand was originally of a biased type,
4129 because we could be recursively called to convert it
4130 to an intermediate type and thus rematerialize the
4131 additive operator endlessly,
4132 - if the expression contains a placeholder, because an
4133 intermediate conversion that changes the sign could
4134 be inserted and thus introduce an artificial overflow
4135 at compile time when the placeholder is substituted. */
4136 if (code == INTEGER_TYPE
4137 && ecode == INTEGER_TYPE
4138 && TYPE_PRECISION (type) < TYPE_PRECISION (etype)
4139 && (TREE_CODE (expr) == PLUS_EXPR || TREE_CODE (expr) == MINUS_EXPR))
4141 tree op0 = get_unwidened (TREE_OPERAND (expr, 0), type);
4143 if ((TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
4144 && TYPE_BIASED_REPRESENTATION_P (TREE_TYPE (op0)))
4145 || CONTAINS_PLACEHOLDER_P (expr))
4146 return build1 (NOP_EXPR, type, expr);
4149 return fold (convert_to_integer (type, expr));
4151 case POINTER_TYPE:
4152 case REFERENCE_TYPE:
4153 /* If converting between two pointers to records denoting
4154 both a template and type, adjust if needed to account
4155 for any differing offsets, since one might be negative. */
4156 if (TYPE_IS_THIN_POINTER_P (etype) && TYPE_IS_THIN_POINTER_P (type))
4158 tree bit_diff
4159 = size_diffop (bit_position (TYPE_FIELDS (TREE_TYPE (etype))),
4160 bit_position (TYPE_FIELDS (TREE_TYPE (type))));
4161 tree byte_diff
4162 = size_binop (CEIL_DIV_EXPR, bit_diff, sbitsize_unit_node);
4163 expr = build1 (NOP_EXPR, type, expr);
4164 TREE_CONSTANT (expr) = TREE_CONSTANT (TREE_OPERAND (expr, 0));
4165 if (integer_zerop (byte_diff))
4166 return expr;
4168 return build_binary_op (POINTER_PLUS_EXPR, type, expr,
4169 fold (convert (sizetype, byte_diff)));
4172 /* If converting to a thin pointer, handle specially. */
4173 if (TYPE_IS_THIN_POINTER_P (type)
4174 && TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)))
4175 return convert_to_thin_pointer (type, expr);
4177 /* If converting fat pointer to normal pointer, get the pointer to the
4178 array and then convert it. */
4179 else if (TYPE_IS_FAT_POINTER_P (etype))
4180 expr
4181 = build_component_ref (expr, NULL_TREE, TYPE_FIELDS (etype), false);
4183 return fold (convert_to_pointer (type, expr));
4185 case REAL_TYPE:
4186 return fold (convert_to_real (type, expr));
4188 case RECORD_TYPE:
4189 if (TYPE_JUSTIFIED_MODULAR_P (type) && !AGGREGATE_TYPE_P (etype))
4191 VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, 1);
4193 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type),
4194 convert (TREE_TYPE (TYPE_FIELDS (type)),
4195 expr));
4196 return gnat_build_constructor (type, v);
4199 /* ... fall through ... */
4201 case ARRAY_TYPE:
4202 /* In these cases, assume the front-end has validated the conversion.
4203 If the conversion is valid, it will be a bit-wise conversion, so
4204 it can be viewed as an unchecked conversion. */
4205 return unchecked_convert (type, expr, false);
4207 case UNION_TYPE:
4208 /* This is a either a conversion between a tagged type and some
4209 subtype, which we have to mark as a UNION_TYPE because of
4210 overlapping fields or a conversion of an Unchecked_Union. */
4211 return unchecked_convert (type, expr, false);
4213 case UNCONSTRAINED_ARRAY_TYPE:
4214 /* If the input is a VECTOR_TYPE, convert to the representative
4215 array type first. */
4216 if (ecode == VECTOR_TYPE)
4218 expr = convert (TYPE_REPRESENTATIVE_ARRAY (etype), expr);
4219 etype = TREE_TYPE (expr);
4220 ecode = TREE_CODE (etype);
4223 /* If EXPR is a constrained array, take its address, convert it to a
4224 fat pointer, and then dereference it. Likewise if EXPR is a
4225 record containing both a template and a constrained array.
4226 Note that a record representing a justified modular type
4227 always represents a packed constrained array. */
4228 if (ecode == ARRAY_TYPE
4229 || (ecode == INTEGER_TYPE && TYPE_HAS_ACTUAL_BOUNDS_P (etype))
4230 || (ecode == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (etype))
4231 || (ecode == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (etype)))
4232 return
4233 build_unary_op
4234 (INDIRECT_REF, NULL_TREE,
4235 convert_to_fat_pointer (TREE_TYPE (type),
4236 build_unary_op (ADDR_EXPR,
4237 NULL_TREE, expr)));
4239 /* Do something very similar for converting one unconstrained
4240 array to another. */
4241 else if (ecode == UNCONSTRAINED_ARRAY_TYPE)
4242 return
4243 build_unary_op (INDIRECT_REF, NULL_TREE,
4244 convert (TREE_TYPE (type),
4245 build_unary_op (ADDR_EXPR,
4246 NULL_TREE, expr)));
4247 else
4248 gcc_unreachable ();
4250 case COMPLEX_TYPE:
4251 return fold (convert_to_complex (type, expr));
4253 default:
4254 gcc_unreachable ();
4258 /* Remove all conversions that are done in EXP. This includes converting
4259 from a padded type or to a justified modular type. If TRUE_ADDRESS
4260 is true, always return the address of the containing object even if
4261 the address is not bit-aligned. */
4263 tree
4264 remove_conversions (tree exp, bool true_address)
4266 switch (TREE_CODE (exp))
4268 case CONSTRUCTOR:
4269 if (true_address
4270 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
4271 && TYPE_JUSTIFIED_MODULAR_P (TREE_TYPE (exp)))
4272 return
4273 remove_conversions (VEC_index (constructor_elt,
4274 CONSTRUCTOR_ELTS (exp), 0)->value,
4275 true);
4276 break;
4278 case COMPONENT_REF:
4279 if (TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
4280 return remove_conversions (TREE_OPERAND (exp, 0), true_address);
4281 break;
4283 case VIEW_CONVERT_EXPR: case NON_LVALUE_EXPR:
4284 CASE_CONVERT:
4285 return remove_conversions (TREE_OPERAND (exp, 0), true_address);
4287 default:
4288 break;
4291 return exp;
4294 /* If EXP's type is an UNCONSTRAINED_ARRAY_TYPE, return an expression that
4295 refers to the underlying array. If it has TYPE_CONTAINS_TEMPLATE_P,
4296 likewise return an expression pointing to the underlying array. */
4298 tree
4299 maybe_unconstrained_array (tree exp)
4301 enum tree_code code = TREE_CODE (exp);
4302 tree new_exp;
4304 switch (TREE_CODE (TREE_TYPE (exp)))
4306 case UNCONSTRAINED_ARRAY_TYPE:
4307 if (code == UNCONSTRAINED_ARRAY_REF)
4309 new_exp = TREE_OPERAND (exp, 0);
4310 new_exp
4311 = build_unary_op (INDIRECT_REF, NULL_TREE,
4312 build_component_ref (new_exp, NULL_TREE,
4313 TYPE_FIELDS
4314 (TREE_TYPE (new_exp)),
4315 false));
4316 TREE_READONLY (new_exp) = TREE_READONLY (exp);
4317 return new_exp;
4320 else if (code == NULL_EXPR)
4321 return build1 (NULL_EXPR,
4322 TREE_TYPE (TREE_TYPE (TYPE_FIELDS
4323 (TREE_TYPE (TREE_TYPE (exp))))),
4324 TREE_OPERAND (exp, 0));
4326 case RECORD_TYPE:
4327 /* If this is a padded type, convert to the unpadded type and see if
4328 it contains a template. */
4329 if (TYPE_PADDING_P (TREE_TYPE (exp)))
4331 new_exp = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (exp))), exp);
4332 if (TREE_CODE (TREE_TYPE (new_exp)) == RECORD_TYPE
4333 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (new_exp)))
4334 return
4335 build_component_ref (new_exp, NULL_TREE,
4336 DECL_CHAIN
4337 (TYPE_FIELDS (TREE_TYPE (new_exp))),
4338 false);
4340 else if (TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (exp)))
4341 return
4342 build_component_ref (exp, NULL_TREE,
4343 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (exp))),
4344 false);
4345 break;
4347 default:
4348 break;
4351 return exp;
4354 /* If EXP's type is a VECTOR_TYPE, return EXP converted to the associated
4355 TYPE_REPRESENTATIVE_ARRAY. */
4357 tree
4358 maybe_vector_array (tree exp)
4360 tree etype = TREE_TYPE (exp);
4362 if (VECTOR_TYPE_P (etype))
4363 exp = convert (TYPE_REPRESENTATIVE_ARRAY (etype), exp);
4365 return exp;
4368 /* Return true if EXPR is an expression that can be folded as an operand
4369 of a VIEW_CONVERT_EXPR. See ada-tree.h for a complete rationale. */
4371 static bool
4372 can_fold_for_view_convert_p (tree expr)
4374 tree t1, t2;
4376 /* The folder will fold NOP_EXPRs between integral types with the same
4377 precision (in the middle-end's sense). We cannot allow it if the
4378 types don't have the same precision in the Ada sense as well. */
4379 if (TREE_CODE (expr) != NOP_EXPR)
4380 return true;
4382 t1 = TREE_TYPE (expr);
4383 t2 = TREE_TYPE (TREE_OPERAND (expr, 0));
4385 /* Defer to the folder for non-integral conversions. */
4386 if (!(INTEGRAL_TYPE_P (t1) && INTEGRAL_TYPE_P (t2)))
4387 return true;
4389 /* Only fold conversions that preserve both precisions. */
4390 if (TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
4391 && operand_equal_p (rm_size (t1), rm_size (t2), 0))
4392 return true;
4394 return false;
4397 /* Return an expression that does an unchecked conversion of EXPR to TYPE.
4398 If NOTRUNC_P is true, truncation operations should be suppressed.
4400 Special care is required with (source or target) integral types whose
4401 precision is not equal to their size, to make sure we fetch or assign
4402 the value bits whose location might depend on the endianness, e.g.
4404 Rmsize : constant := 8;
4405 subtype Int is Integer range 0 .. 2 ** Rmsize - 1;
4407 type Bit_Array is array (1 .. Rmsize) of Boolean;
4408 pragma Pack (Bit_Array);
4410 function To_Bit_Array is new Unchecked_Conversion (Int, Bit_Array);
4412 Value : Int := 2#1000_0001#;
4413 Vbits : Bit_Array := To_Bit_Array (Value);
4415 we expect the 8 bits at Vbits'Address to always contain Value, while
4416 their original location depends on the endianness, at Value'Address
4417 on a little-endian architecture but not on a big-endian one. */
4419 tree
4420 unchecked_convert (tree type, tree expr, bool notrunc_p)
4422 tree etype = TREE_TYPE (expr);
4423 enum tree_code ecode = TREE_CODE (etype);
4424 enum tree_code code = TREE_CODE (type);
4425 int c;
4427 /* If the expression is already of the right type, we are done. */
4428 if (etype == type)
4429 return expr;
4431 /* If both types types are integral just do a normal conversion.
4432 Likewise for a conversion to an unconstrained array. */
4433 if ((((INTEGRAL_TYPE_P (type)
4434 && !(code == INTEGER_TYPE && TYPE_VAX_FLOATING_POINT_P (type)))
4435 || (POINTER_TYPE_P (type) && ! TYPE_IS_THIN_POINTER_P (type))
4436 || (code == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (type)))
4437 && ((INTEGRAL_TYPE_P (etype)
4438 && !(ecode == INTEGER_TYPE && TYPE_VAX_FLOATING_POINT_P (etype)))
4439 || (POINTER_TYPE_P (etype) && !TYPE_IS_THIN_POINTER_P (etype))
4440 || (ecode == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (etype))))
4441 || code == UNCONSTRAINED_ARRAY_TYPE)
4443 if (ecode == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (etype))
4445 tree ntype = copy_type (etype);
4446 TYPE_BIASED_REPRESENTATION_P (ntype) = 0;
4447 TYPE_MAIN_VARIANT (ntype) = ntype;
4448 expr = build1 (NOP_EXPR, ntype, expr);
4451 if (code == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (type))
4453 tree rtype = copy_type (type);
4454 TYPE_BIASED_REPRESENTATION_P (rtype) = 0;
4455 TYPE_MAIN_VARIANT (rtype) = rtype;
4456 expr = convert (rtype, expr);
4457 expr = build1 (NOP_EXPR, type, expr);
4459 else
4460 expr = convert (type, expr);
4463 /* If we are converting to an integral type whose precision is not equal
4464 to its size, first unchecked convert to a record that contains an
4465 object of the output type. Then extract the field. */
4466 else if (INTEGRAL_TYPE_P (type)
4467 && TYPE_RM_SIZE (type)
4468 && 0 != compare_tree_int (TYPE_RM_SIZE (type),
4469 GET_MODE_BITSIZE (TYPE_MODE (type))))
4471 tree rec_type = make_node (RECORD_TYPE);
4472 tree field = create_field_decl (get_identifier ("OBJ"), type, rec_type,
4473 NULL_TREE, NULL_TREE, 1, 0);
4475 TYPE_FIELDS (rec_type) = field;
4476 layout_type (rec_type);
4478 expr = unchecked_convert (rec_type, expr, notrunc_p);
4479 expr = build_component_ref (expr, NULL_TREE, field, false);
4482 /* Similarly if we are converting from an integral type whose precision
4483 is not equal to its size. */
4484 else if (INTEGRAL_TYPE_P (etype)
4485 && TYPE_RM_SIZE (etype)
4486 && 0 != compare_tree_int (TYPE_RM_SIZE (etype),
4487 GET_MODE_BITSIZE (TYPE_MODE (etype))))
4489 tree rec_type = make_node (RECORD_TYPE);
4490 tree field = create_field_decl (get_identifier ("OBJ"), etype, rec_type,
4491 NULL_TREE, NULL_TREE, 1, 0);
4492 VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, 1);
4494 TYPE_FIELDS (rec_type) = field;
4495 layout_type (rec_type);
4497 CONSTRUCTOR_APPEND_ELT (v, field, expr);
4498 expr = gnat_build_constructor (rec_type, v);
4499 expr = unchecked_convert (type, expr, notrunc_p);
4502 /* If we are converting from a scalar type to a type with a different size,
4503 we need to pad to have the same size on both sides.
4505 ??? We cannot do it unconditionally because unchecked conversions are
4506 used liberally by the front-end to implement polymorphism, e.g. in:
4508 S191s : constant ada__tags__addr_ptr := ada__tags__addr_ptr!(S190s);
4509 return p___size__4 (p__object!(S191s.all));
4511 so we skip all expressions that are references. */
4512 else if (!REFERENCE_CLASS_P (expr)
4513 && !AGGREGATE_TYPE_P (etype)
4514 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4515 && (c = tree_int_cst_compare (TYPE_SIZE (etype), TYPE_SIZE (type))))
4517 if (c < 0)
4519 expr = convert (maybe_pad_type (etype, TYPE_SIZE (type), 0, Empty,
4520 false, false, false, true),
4521 expr);
4522 expr = unchecked_convert (type, expr, notrunc_p);
4524 else
4526 tree rec_type = maybe_pad_type (type, TYPE_SIZE (etype), 0, Empty,
4527 false, false, false, true);
4528 expr = unchecked_convert (rec_type, expr, notrunc_p);
4529 expr = build_component_ref (expr, NULL_TREE, TYPE_FIELDS (rec_type),
4530 false);
4534 /* We have a special case when we are converting between two unconstrained
4535 array types. In that case, take the address, convert the fat pointer
4536 types, and dereference. */
4537 else if (ecode == code && code == UNCONSTRAINED_ARRAY_TYPE)
4538 expr = build_unary_op (INDIRECT_REF, NULL_TREE,
4539 build1 (VIEW_CONVERT_EXPR, TREE_TYPE (type),
4540 build_unary_op (ADDR_EXPR, NULL_TREE,
4541 expr)));
4543 /* Another special case is when we are converting to a vector type from its
4544 representative array type; this a regular conversion. */
4545 else if (code == VECTOR_TYPE
4546 && ecode == ARRAY_TYPE
4547 && gnat_types_compatible_p (TYPE_REPRESENTATIVE_ARRAY (type),
4548 etype))
4549 expr = convert (type, expr);
4551 else
4553 expr = maybe_unconstrained_array (expr);
4554 etype = TREE_TYPE (expr);
4555 ecode = TREE_CODE (etype);
4556 if (can_fold_for_view_convert_p (expr))
4557 expr = fold_build1 (VIEW_CONVERT_EXPR, type, expr);
4558 else
4559 expr = build1 (VIEW_CONVERT_EXPR, type, expr);
4562 /* If the result is an integral type whose precision is not equal to its
4563 size, sign- or zero-extend the result. We need not do this if the input
4564 is an integral type of the same precision and signedness or if the output
4565 is a biased type or if both the input and output are unsigned. */
4566 if (!notrunc_p
4567 && INTEGRAL_TYPE_P (type) && TYPE_RM_SIZE (type)
4568 && !(code == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (type))
4569 && 0 != compare_tree_int (TYPE_RM_SIZE (type),
4570 GET_MODE_BITSIZE (TYPE_MODE (type)))
4571 && !(INTEGRAL_TYPE_P (etype)
4572 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (etype)
4573 && operand_equal_p (TYPE_RM_SIZE (type),
4574 (TYPE_RM_SIZE (etype) != 0
4575 ? TYPE_RM_SIZE (etype) : TYPE_SIZE (etype)),
4577 && !(TYPE_UNSIGNED (type) && TYPE_UNSIGNED (etype)))
4579 tree base_type
4580 = gnat_type_for_mode (TYPE_MODE (type), TYPE_UNSIGNED (type));
4581 tree shift_expr
4582 = convert (base_type,
4583 size_binop (MINUS_EXPR,
4584 bitsize_int
4585 (GET_MODE_BITSIZE (TYPE_MODE (type))),
4586 TYPE_RM_SIZE (type)));
4587 expr
4588 = convert (type,
4589 build_binary_op (RSHIFT_EXPR, base_type,
4590 build_binary_op (LSHIFT_EXPR, base_type,
4591 convert (base_type, expr),
4592 shift_expr),
4593 shift_expr));
4596 /* An unchecked conversion should never raise Constraint_Error. The code
4597 below assumes that GCC's conversion routines overflow the same way that
4598 the underlying hardware does. This is probably true. In the rare case
4599 when it is false, we can rely on the fact that such conversions are
4600 erroneous anyway. */
4601 if (TREE_CODE (expr) == INTEGER_CST)
4602 TREE_OVERFLOW (expr) = 0;
4604 /* If the sizes of the types differ and this is an VIEW_CONVERT_EXPR,
4605 show no longer constant. */
4606 if (TREE_CODE (expr) == VIEW_CONVERT_EXPR
4607 && !operand_equal_p (TYPE_SIZE_UNIT (type), TYPE_SIZE_UNIT (etype),
4608 OEP_ONLY_CONST))
4609 TREE_CONSTANT (expr) = 0;
4611 return expr;
4614 /* Return the appropriate GCC tree code for the specified GNAT_TYPE,
4615 the latter being a record type as predicated by Is_Record_Type. */
4617 enum tree_code
4618 tree_code_for_record_type (Entity_Id gnat_type)
4620 Node_Id component_list
4621 = Component_List (Type_Definition
4622 (Declaration_Node
4623 (Implementation_Base_Type (gnat_type))));
4624 Node_Id component;
4626 /* Make this a UNION_TYPE unless it's either not an Unchecked_Union or
4627 we have a non-discriminant field outside a variant. In either case,
4628 it's a RECORD_TYPE. */
4630 if (!Is_Unchecked_Union (gnat_type))
4631 return RECORD_TYPE;
4633 for (component = First_Non_Pragma (Component_Items (component_list));
4634 Present (component);
4635 component = Next_Non_Pragma (component))
4636 if (Ekind (Defining_Entity (component)) == E_Component)
4637 return RECORD_TYPE;
4639 return UNION_TYPE;
4642 /* Return true if GNAT_TYPE is a "double" floating-point type, i.e. whose
4643 size is equal to 64 bits, or an array of such a type. Set ALIGN_CLAUSE
4644 according to the presence of an alignment clause on the type or, if it
4645 is an array, on the component type. */
4647 bool
4648 is_double_float_or_array (Entity_Id gnat_type, bool *align_clause)
4650 gnat_type = Underlying_Type (gnat_type);
4652 *align_clause = Present (Alignment_Clause (gnat_type));
4654 if (Is_Array_Type (gnat_type))
4656 gnat_type = Underlying_Type (Component_Type (gnat_type));
4657 if (Present (Alignment_Clause (gnat_type)))
4658 *align_clause = true;
4661 if (!Is_Floating_Point_Type (gnat_type))
4662 return false;
4664 if (UI_To_Int (Esize (gnat_type)) != 64)
4665 return false;
4667 return true;
4670 /* Return true if GNAT_TYPE is a "double" or larger scalar type, i.e. whose
4671 size is greater or equal to 64 bits, or an array of such a type. Set
4672 ALIGN_CLAUSE according to the presence of an alignment clause on the
4673 type or, if it is an array, on the component type. */
4675 bool
4676 is_double_scalar_or_array (Entity_Id gnat_type, bool *align_clause)
4678 gnat_type = Underlying_Type (gnat_type);
4680 *align_clause = Present (Alignment_Clause (gnat_type));
4682 if (Is_Array_Type (gnat_type))
4684 gnat_type = Underlying_Type (Component_Type (gnat_type));
4685 if (Present (Alignment_Clause (gnat_type)))
4686 *align_clause = true;
4689 if (!Is_Scalar_Type (gnat_type))
4690 return false;
4692 if (UI_To_Int (Esize (gnat_type)) < 64)
4693 return false;
4695 return true;
4698 /* Return true if GNU_TYPE is suitable as the type of a non-aliased
4699 component of an aggregate type. */
4701 bool
4702 type_for_nonaliased_component_p (tree gnu_type)
4704 /* If the type is passed by reference, we may have pointers to the
4705 component so it cannot be made non-aliased. */
4706 if (must_pass_by_ref (gnu_type) || default_pass_by_ref (gnu_type))
4707 return false;
4709 /* We used to say that any component of aggregate type is aliased
4710 because the front-end may take 'Reference of it. The front-end
4711 has been enhanced in the meantime so as to use a renaming instead
4712 in most cases, but the back-end can probably take the address of
4713 such a component too so we go for the conservative stance.
4715 For instance, we might need the address of any array type, even
4716 if normally passed by copy, to construct a fat pointer if the
4717 component is used as an actual for an unconstrained formal.
4719 Likewise for record types: even if a specific record subtype is
4720 passed by copy, the parent type might be passed by ref (e.g. if
4721 it's of variable size) and we might take the address of a child
4722 component to pass to a parent formal. We have no way to check
4723 for such conditions here. */
4724 if (AGGREGATE_TYPE_P (gnu_type))
4725 return false;
4727 return true;
4730 /* Return true if TYPE is a smaller form of ORIG_TYPE. */
4732 bool
4733 smaller_form_type_p (tree type, tree orig_type)
4735 tree size, osize;
4737 /* We're not interested in variants here. */
4738 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig_type))
4739 return false;
4741 /* Like a variant, a packable version keeps the original TYPE_NAME. */
4742 if (TYPE_NAME (type) != TYPE_NAME (orig_type))
4743 return false;
4745 size = TYPE_SIZE (type);
4746 osize = TYPE_SIZE (orig_type);
4748 if (!(TREE_CODE (size) == INTEGER_CST && TREE_CODE (osize) == INTEGER_CST))
4749 return false;
4751 return tree_int_cst_lt (size, osize) != 0;
4754 /* Perform final processing on global variables. */
4756 static GTY (()) tree dummy_global;
4758 void
4759 gnat_write_global_declarations (void)
4761 /* If we have declared types as used at the global level, insert them in
4762 the global hash table. We use a dummy variable for this purpose. */
4763 if (!VEC_empty (tree, types_used_by_cur_var_decl))
4765 dummy_global
4766 = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, void_type_node);
4767 TREE_STATIC (dummy_global) = 1;
4768 TREE_ASM_WRITTEN (dummy_global) = 1;
4769 varpool_mark_needed_node (varpool_node (dummy_global));
4771 while (!VEC_empty (tree, types_used_by_cur_var_decl))
4773 tree t = VEC_pop (tree, types_used_by_cur_var_decl);
4774 types_used_by_var_decl_insert (t, dummy_global);
4778 /* Proceed to optimize and emit assembly.
4779 FIXME: shouldn't be the front end's responsibility to call this. */
4780 cgraph_finalize_compilation_unit ();
4782 /* Emit debug info for all global declarations. */
4783 emit_debug_global_declarations (VEC_address (tree, global_decls),
4784 VEC_length (tree, global_decls));
4787 /* ************************************************************************
4788 * * GCC builtins support *
4789 * ************************************************************************ */
4791 /* The general scheme is fairly simple:
4793 For each builtin function/type to be declared, gnat_install_builtins calls
4794 internal facilities which eventually get to gnat_push_decl, which in turn
4795 tracks the so declared builtin function decls in the 'builtin_decls' global
4796 datastructure. When an Intrinsic subprogram declaration is processed, we
4797 search this global datastructure to retrieve the associated BUILT_IN DECL
4798 node. */
4800 /* Search the chain of currently available builtin declarations for a node
4801 corresponding to function NAME (an IDENTIFIER_NODE). Return the first node
4802 found, if any, or NULL_TREE otherwise. */
4803 tree
4804 builtin_decl_for (tree name)
4806 unsigned i;
4807 tree decl;
4809 FOR_EACH_VEC_ELT (tree, builtin_decls, i, decl)
4810 if (DECL_NAME (decl) == name)
4811 return decl;
4813 return NULL_TREE;
4816 /* The code below eventually exposes gnat_install_builtins, which declares
4817 the builtin types and functions we might need, either internally or as
4818 user accessible facilities.
4820 ??? This is a first implementation shot, still in rough shape. It is
4821 heavily inspired from the "C" family implementation, with chunks copied
4822 verbatim from there.
4824 Two obvious TODO candidates are
4825 o Use a more efficient name/decl mapping scheme
4826 o Devise a middle-end infrastructure to avoid having to copy
4827 pieces between front-ends. */
4829 /* ----------------------------------------------------------------------- *
4830 * BUILTIN ELEMENTARY TYPES *
4831 * ----------------------------------------------------------------------- */
4833 /* Standard data types to be used in builtin argument declarations. */
4835 enum c_tree_index
4837 CTI_SIGNED_SIZE_TYPE, /* For format checking only. */
4838 CTI_STRING_TYPE,
4839 CTI_CONST_STRING_TYPE,
4841 CTI_MAX
4844 static tree c_global_trees[CTI_MAX];
4846 #define signed_size_type_node c_global_trees[CTI_SIGNED_SIZE_TYPE]
4847 #define string_type_node c_global_trees[CTI_STRING_TYPE]
4848 #define const_string_type_node c_global_trees[CTI_CONST_STRING_TYPE]
4850 /* ??? In addition some attribute handlers, we currently don't support a
4851 (small) number of builtin-types, which in turns inhibits support for a
4852 number of builtin functions. */
4853 #define wint_type_node void_type_node
4854 #define intmax_type_node void_type_node
4855 #define uintmax_type_node void_type_node
4857 /* Build the void_list_node (void_type_node having been created). */
4859 static tree
4860 build_void_list_node (void)
4862 tree t = build_tree_list (NULL_TREE, void_type_node);
4863 return t;
4866 /* Used to help initialize the builtin-types.def table. When a type of
4867 the correct size doesn't exist, use error_mark_node instead of NULL.
4868 The later results in segfaults even when a decl using the type doesn't
4869 get invoked. */
4871 static tree
4872 builtin_type_for_size (int size, bool unsignedp)
4874 tree type = gnat_type_for_size (size, unsignedp);
4875 return type ? type : error_mark_node;
4878 /* Build/push the elementary type decls that builtin functions/types
4879 will need. */
4881 static void
4882 install_builtin_elementary_types (void)
4884 signed_size_type_node = gnat_signed_type (size_type_node);
4885 pid_type_node = integer_type_node;
4886 void_list_node = build_void_list_node ();
4888 string_type_node = build_pointer_type (char_type_node);
4889 const_string_type_node
4890 = build_pointer_type (build_qualified_type
4891 (char_type_node, TYPE_QUAL_CONST));
4894 /* ----------------------------------------------------------------------- *
4895 * BUILTIN FUNCTION TYPES *
4896 * ----------------------------------------------------------------------- */
4898 /* Now, builtin function types per se. */
4900 enum c_builtin_type
4902 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
4903 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
4904 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
4905 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
4906 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4907 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4908 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
4909 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
4910 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
4911 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
4912 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
4913 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
4914 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4915 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4916 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
4917 NAME,
4918 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
4919 #include "builtin-types.def"
4920 #undef DEF_PRIMITIVE_TYPE
4921 #undef DEF_FUNCTION_TYPE_0
4922 #undef DEF_FUNCTION_TYPE_1
4923 #undef DEF_FUNCTION_TYPE_2
4924 #undef DEF_FUNCTION_TYPE_3
4925 #undef DEF_FUNCTION_TYPE_4
4926 #undef DEF_FUNCTION_TYPE_5
4927 #undef DEF_FUNCTION_TYPE_6
4928 #undef DEF_FUNCTION_TYPE_7
4929 #undef DEF_FUNCTION_TYPE_VAR_0
4930 #undef DEF_FUNCTION_TYPE_VAR_1
4931 #undef DEF_FUNCTION_TYPE_VAR_2
4932 #undef DEF_FUNCTION_TYPE_VAR_3
4933 #undef DEF_FUNCTION_TYPE_VAR_4
4934 #undef DEF_FUNCTION_TYPE_VAR_5
4935 #undef DEF_POINTER_TYPE
4936 BT_LAST
4939 typedef enum c_builtin_type builtin_type;
4941 /* A temporary array used in communication with def_fn_type. */
4942 static GTY(()) tree builtin_types[(int) BT_LAST + 1];
4944 /* A helper function for install_builtin_types. Build function type
4945 for DEF with return type RET and N arguments. If VAR is true, then the
4946 function should be variadic after those N arguments.
4948 Takes special care not to ICE if any of the types involved are
4949 error_mark_node, which indicates that said type is not in fact available
4950 (see builtin_type_for_size). In which case the function type as a whole
4951 should be error_mark_node. */
4953 static void
4954 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
4956 tree args = NULL, t;
4957 va_list list;
4958 int i;
4960 va_start (list, n);
4961 for (i = 0; i < n; ++i)
4963 builtin_type a = (builtin_type) va_arg (list, int);
4964 t = builtin_types[a];
4965 if (t == error_mark_node)
4966 goto egress;
4967 args = tree_cons (NULL_TREE, t, args);
4969 va_end (list);
4971 args = nreverse (args);
4972 if (!var)
4973 args = chainon (args, void_list_node);
4975 t = builtin_types[ret];
4976 if (t == error_mark_node)
4977 goto egress;
4978 t = build_function_type (t, args);
4980 egress:
4981 builtin_types[def] = t;
4982 va_end (list);
4985 /* Build the builtin function types and install them in the builtin_types
4986 array for later use in builtin function decls. */
4988 static void
4989 install_builtin_function_types (void)
4991 tree va_list_ref_type_node;
4992 tree va_list_arg_type_node;
4994 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4996 va_list_arg_type_node = va_list_ref_type_node =
4997 build_pointer_type (TREE_TYPE (va_list_type_node));
4999 else
5001 va_list_arg_type_node = va_list_type_node;
5002 va_list_ref_type_node = build_reference_type (va_list_type_node);
5005 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5006 builtin_types[ENUM] = VALUE;
5007 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5008 def_fn_type (ENUM, RETURN, 0, 0);
5009 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5010 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5011 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5012 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5013 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5014 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5015 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5016 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5017 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5018 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5019 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5020 ARG6) \
5021 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5022 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5023 ARG6, ARG7) \
5024 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5025 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5026 def_fn_type (ENUM, RETURN, 1, 0);
5027 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5028 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5029 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5030 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5031 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5032 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5033 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5034 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5035 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5036 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5037 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5038 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5040 #include "builtin-types.def"
5042 #undef DEF_PRIMITIVE_TYPE
5043 #undef DEF_FUNCTION_TYPE_1
5044 #undef DEF_FUNCTION_TYPE_2
5045 #undef DEF_FUNCTION_TYPE_3
5046 #undef DEF_FUNCTION_TYPE_4
5047 #undef DEF_FUNCTION_TYPE_5
5048 #undef DEF_FUNCTION_TYPE_6
5049 #undef DEF_FUNCTION_TYPE_VAR_0
5050 #undef DEF_FUNCTION_TYPE_VAR_1
5051 #undef DEF_FUNCTION_TYPE_VAR_2
5052 #undef DEF_FUNCTION_TYPE_VAR_3
5053 #undef DEF_FUNCTION_TYPE_VAR_4
5054 #undef DEF_FUNCTION_TYPE_VAR_5
5055 #undef DEF_POINTER_TYPE
5056 builtin_types[(int) BT_LAST] = NULL_TREE;
5059 /* ----------------------------------------------------------------------- *
5060 * BUILTIN ATTRIBUTES *
5061 * ----------------------------------------------------------------------- */
5063 enum built_in_attribute
5065 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5066 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5067 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5068 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5069 #include "builtin-attrs.def"
5070 #undef DEF_ATTR_NULL_TREE
5071 #undef DEF_ATTR_INT
5072 #undef DEF_ATTR_IDENT
5073 #undef DEF_ATTR_TREE_LIST
5074 ATTR_LAST
5077 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5079 static void
5080 install_builtin_attributes (void)
5082 /* Fill in the built_in_attributes array. */
5083 #define DEF_ATTR_NULL_TREE(ENUM) \
5084 built_in_attributes[(int) ENUM] = NULL_TREE;
5085 #define DEF_ATTR_INT(ENUM, VALUE) \
5086 built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE);
5087 #define DEF_ATTR_IDENT(ENUM, STRING) \
5088 built_in_attributes[(int) ENUM] = get_identifier (STRING);
5089 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5090 built_in_attributes[(int) ENUM] \
5091 = tree_cons (built_in_attributes[(int) PURPOSE], \
5092 built_in_attributes[(int) VALUE], \
5093 built_in_attributes[(int) CHAIN]);
5094 #include "builtin-attrs.def"
5095 #undef DEF_ATTR_NULL_TREE
5096 #undef DEF_ATTR_INT
5097 #undef DEF_ATTR_IDENT
5098 #undef DEF_ATTR_TREE_LIST
5101 /* Handle a "const" attribute; arguments as in
5102 struct attribute_spec.handler. */
5104 static tree
5105 handle_const_attribute (tree *node, tree ARG_UNUSED (name),
5106 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
5107 bool *no_add_attrs)
5109 if (TREE_CODE (*node) == FUNCTION_DECL)
5110 TREE_READONLY (*node) = 1;
5111 else
5112 *no_add_attrs = true;
5114 return NULL_TREE;
5117 /* Handle a "nothrow" attribute; arguments as in
5118 struct attribute_spec.handler. */
5120 static tree
5121 handle_nothrow_attribute (tree *node, tree ARG_UNUSED (name),
5122 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
5123 bool *no_add_attrs)
5125 if (TREE_CODE (*node) == FUNCTION_DECL)
5126 TREE_NOTHROW (*node) = 1;
5127 else
5128 *no_add_attrs = true;
5130 return NULL_TREE;
5133 /* Handle a "pure" attribute; arguments as in
5134 struct attribute_spec.handler. */
5136 static tree
5137 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5138 int ARG_UNUSED (flags), bool *no_add_attrs)
5140 if (TREE_CODE (*node) == FUNCTION_DECL)
5141 DECL_PURE_P (*node) = 1;
5142 /* ??? TODO: Support types. */
5143 else
5145 warning (OPT_Wattributes, "%qs attribute ignored",
5146 IDENTIFIER_POINTER (name));
5147 *no_add_attrs = true;
5150 return NULL_TREE;
5153 /* Handle a "no vops" attribute; arguments as in
5154 struct attribute_spec.handler. */
5156 static tree
5157 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
5158 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
5159 bool *ARG_UNUSED (no_add_attrs))
5161 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
5162 DECL_IS_NOVOPS (*node) = 1;
5163 return NULL_TREE;
5166 /* Helper for nonnull attribute handling; fetch the operand number
5167 from the attribute argument list. */
5169 static bool
5170 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
5172 /* Verify the arg number is a constant. */
5173 if (TREE_CODE (arg_num_expr) != INTEGER_CST
5174 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
5175 return false;
5177 *valp = TREE_INT_CST_LOW (arg_num_expr);
5178 return true;
5181 /* Handle the "nonnull" attribute. */
5182 static tree
5183 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
5184 tree args, int ARG_UNUSED (flags),
5185 bool *no_add_attrs)
5187 tree type = *node;
5188 unsigned HOST_WIDE_INT attr_arg_num;
5190 /* If no arguments are specified, all pointer arguments should be
5191 non-null. Verify a full prototype is given so that the arguments
5192 will have the correct types when we actually check them later. */
5193 if (!args)
5195 if (!prototype_p (type))
5197 error ("nonnull attribute without arguments on a non-prototype");
5198 *no_add_attrs = true;
5200 return NULL_TREE;
5203 /* Argument list specified. Verify that each argument number references
5204 a pointer argument. */
5205 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
5207 tree argument;
5208 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
5210 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
5212 error ("nonnull argument has invalid operand number (argument %lu)",
5213 (unsigned long) attr_arg_num);
5214 *no_add_attrs = true;
5215 return NULL_TREE;
5218 argument = TYPE_ARG_TYPES (type);
5219 if (argument)
5221 for (ck_num = 1; ; ck_num++)
5223 if (!argument || ck_num == arg_num)
5224 break;
5225 argument = TREE_CHAIN (argument);
5228 if (!argument
5229 || TREE_CODE (TREE_VALUE (argument)) == VOID_TYPE)
5231 error ("nonnull argument with out-of-range operand number "
5232 "(argument %lu, operand %lu)",
5233 (unsigned long) attr_arg_num, (unsigned long) arg_num);
5234 *no_add_attrs = true;
5235 return NULL_TREE;
5238 if (TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE)
5240 error ("nonnull argument references non-pointer operand "
5241 "(argument %lu, operand %lu)",
5242 (unsigned long) attr_arg_num, (unsigned long) arg_num);
5243 *no_add_attrs = true;
5244 return NULL_TREE;
5249 return NULL_TREE;
5252 /* Handle a "sentinel" attribute. */
5254 static tree
5255 handle_sentinel_attribute (tree *node, tree name, tree args,
5256 int ARG_UNUSED (flags), bool *no_add_attrs)
5258 tree params = TYPE_ARG_TYPES (*node);
5260 if (!prototype_p (*node))
5262 warning (OPT_Wattributes,
5263 "%qs attribute requires prototypes with named arguments",
5264 IDENTIFIER_POINTER (name));
5265 *no_add_attrs = true;
5267 else
5269 while (TREE_CHAIN (params))
5270 params = TREE_CHAIN (params);
5272 if (VOID_TYPE_P (TREE_VALUE (params)))
5274 warning (OPT_Wattributes,
5275 "%qs attribute only applies to variadic functions",
5276 IDENTIFIER_POINTER (name));
5277 *no_add_attrs = true;
5281 if (args)
5283 tree position = TREE_VALUE (args);
5285 if (TREE_CODE (position) != INTEGER_CST)
5287 warning (0, "requested position is not an integer constant");
5288 *no_add_attrs = true;
5290 else
5292 if (tree_int_cst_lt (position, integer_zero_node))
5294 warning (0, "requested position is less than zero");
5295 *no_add_attrs = true;
5300 return NULL_TREE;
5303 /* Handle a "noreturn" attribute; arguments as in
5304 struct attribute_spec.handler. */
5306 static tree
5307 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5308 int ARG_UNUSED (flags), bool *no_add_attrs)
5310 tree type = TREE_TYPE (*node);
5312 /* See FIXME comment in c_common_attribute_table. */
5313 if (TREE_CODE (*node) == FUNCTION_DECL)
5314 TREE_THIS_VOLATILE (*node) = 1;
5315 else if (TREE_CODE (type) == POINTER_TYPE
5316 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5317 TREE_TYPE (*node)
5318 = build_pointer_type
5319 (build_type_variant (TREE_TYPE (type),
5320 TYPE_READONLY (TREE_TYPE (type)), 1));
5321 else
5323 warning (OPT_Wattributes, "%qs attribute ignored",
5324 IDENTIFIER_POINTER (name));
5325 *no_add_attrs = true;
5328 return NULL_TREE;
5331 /* Handle a "leaf" attribute; arguments as in
5332 struct attribute_spec.handler. */
5334 static tree
5335 handle_leaf_attribute (tree *node, tree name,
5336 tree ARG_UNUSED (args),
5337 int ARG_UNUSED (flags), bool *no_add_attrs)
5339 if (TREE_CODE (*node) != FUNCTION_DECL)
5341 warning (OPT_Wattributes, "%qE attribute ignored", name);
5342 *no_add_attrs = true;
5344 if (!TREE_PUBLIC (*node))
5346 warning (OPT_Wattributes, "%qE attribute has no effect", name);
5347 *no_add_attrs = true;
5350 return NULL_TREE;
5353 /* Handle a "malloc" attribute; arguments as in
5354 struct attribute_spec.handler. */
5356 static tree
5357 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5358 int ARG_UNUSED (flags), bool *no_add_attrs)
5360 if (TREE_CODE (*node) == FUNCTION_DECL
5361 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
5362 DECL_IS_MALLOC (*node) = 1;
5363 else
5365 warning (OPT_Wattributes, "%qs attribute ignored",
5366 IDENTIFIER_POINTER (name));
5367 *no_add_attrs = true;
5370 return NULL_TREE;
5373 /* Fake handler for attributes we don't properly support. */
5375 tree
5376 fake_attribute_handler (tree * ARG_UNUSED (node),
5377 tree ARG_UNUSED (name),
5378 tree ARG_UNUSED (args),
5379 int ARG_UNUSED (flags),
5380 bool * ARG_UNUSED (no_add_attrs))
5382 return NULL_TREE;
5385 /* Handle a "type_generic" attribute. */
5387 static tree
5388 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
5389 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
5390 bool * ARG_UNUSED (no_add_attrs))
5392 tree params;
5394 /* Ensure we have a function type. */
5395 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
5397 params = TYPE_ARG_TYPES (*node);
5398 while (params && ! VOID_TYPE_P (TREE_VALUE (params)))
5399 params = TREE_CHAIN (params);
5401 /* Ensure we have a variadic function. */
5402 gcc_assert (!params);
5404 return NULL_TREE;
5407 /* Handle a "vector_size" attribute; arguments as in
5408 struct attribute_spec.handler. */
5410 static tree
5411 handle_vector_size_attribute (tree *node, tree name, tree args,
5412 int ARG_UNUSED (flags),
5413 bool *no_add_attrs)
5415 unsigned HOST_WIDE_INT vecsize, nunits;
5416 enum machine_mode orig_mode;
5417 tree type = *node, new_type, size;
5419 *no_add_attrs = true;
5421 size = TREE_VALUE (args);
5423 if (!host_integerp (size, 1))
5425 warning (OPT_Wattributes, "%qs attribute ignored",
5426 IDENTIFIER_POINTER (name));
5427 return NULL_TREE;
5430 /* Get the vector size (in bytes). */
5431 vecsize = tree_low_cst (size, 1);
5433 /* We need to provide for vector pointers, vector arrays, and
5434 functions returning vectors. For example:
5436 __attribute__((vector_size(16))) short *foo;
5438 In this case, the mode is SI, but the type being modified is
5439 HI, so we need to look further. */
5441 while (POINTER_TYPE_P (type)
5442 || TREE_CODE (type) == FUNCTION_TYPE
5443 || TREE_CODE (type) == ARRAY_TYPE)
5444 type = TREE_TYPE (type);
5446 /* Get the mode of the type being modified. */
5447 orig_mode = TYPE_MODE (type);
5449 if ((!INTEGRAL_TYPE_P (type)
5450 && !SCALAR_FLOAT_TYPE_P (type)
5451 && !FIXED_POINT_TYPE_P (type))
5452 || (!SCALAR_FLOAT_MODE_P (orig_mode)
5453 && GET_MODE_CLASS (orig_mode) != MODE_INT
5454 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
5455 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
5456 || TREE_CODE (type) == BOOLEAN_TYPE)
5458 error ("invalid vector type for attribute %qs",
5459 IDENTIFIER_POINTER (name));
5460 return NULL_TREE;
5463 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
5465 error ("vector size not an integral multiple of component size");
5466 return NULL;
5469 if (vecsize == 0)
5471 error ("zero vector size");
5472 return NULL;
5475 /* Calculate how many units fit in the vector. */
5476 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
5477 if (nunits & (nunits - 1))
5479 error ("number of components of the vector not a power of two");
5480 return NULL_TREE;
5483 new_type = build_vector_type (type, nunits);
5485 /* Build back pointers if needed. */
5486 *node = reconstruct_complex_type (*node, new_type);
5488 return NULL_TREE;
5491 /* Handle a "vector_type" attribute; arguments as in
5492 struct attribute_spec.handler. */
5494 static tree
5495 handle_vector_type_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5496 int ARG_UNUSED (flags),
5497 bool *no_add_attrs)
5499 /* Vector representative type and size. */
5500 tree rep_type = *node;
5501 tree rep_size = TYPE_SIZE_UNIT (rep_type);
5502 tree rep_name;
5504 /* Vector size in bytes and number of units. */
5505 unsigned HOST_WIDE_INT vec_bytes, vec_units;
5507 /* Vector element type and mode. */
5508 tree elem_type;
5509 enum machine_mode elem_mode;
5511 *no_add_attrs = true;
5513 /* Get the representative array type, possibly nested within a
5514 padding record e.g. for alignment purposes. */
5516 if (TYPE_IS_PADDING_P (rep_type))
5517 rep_type = TREE_TYPE (TYPE_FIELDS (rep_type));
5519 if (TREE_CODE (rep_type) != ARRAY_TYPE)
5521 error ("attribute %qs applies to array types only",
5522 IDENTIFIER_POINTER (name));
5523 return NULL_TREE;
5526 /* Silently punt on variable sizes. We can't make vector types for them,
5527 need to ignore them on front-end generated subtypes of unconstrained
5528 bases, and this attribute is for binding implementors, not end-users, so
5529 we should never get there from legitimate explicit uses. */
5531 if (!host_integerp (rep_size, 1))
5532 return NULL_TREE;
5534 /* Get the element type/mode and check this is something we know
5535 how to make vectors of. */
5537 elem_type = TREE_TYPE (rep_type);
5538 elem_mode = TYPE_MODE (elem_type);
5540 if ((!INTEGRAL_TYPE_P (elem_type)
5541 && !SCALAR_FLOAT_TYPE_P (elem_type)
5542 && !FIXED_POINT_TYPE_P (elem_type))
5543 || (!SCALAR_FLOAT_MODE_P (elem_mode)
5544 && GET_MODE_CLASS (elem_mode) != MODE_INT
5545 && !ALL_SCALAR_FIXED_POINT_MODE_P (elem_mode))
5546 || !host_integerp (TYPE_SIZE_UNIT (elem_type), 1))
5548 error ("invalid element type for attribute %qs",
5549 IDENTIFIER_POINTER (name));
5550 return NULL_TREE;
5553 /* Sanity check the vector size and element type consistency. */
5555 vec_bytes = tree_low_cst (rep_size, 1);
5557 if (vec_bytes % tree_low_cst (TYPE_SIZE_UNIT (elem_type), 1))
5559 error ("vector size not an integral multiple of component size");
5560 return NULL;
5563 if (vec_bytes == 0)
5565 error ("zero vector size");
5566 return NULL;
5569 vec_units = vec_bytes / tree_low_cst (TYPE_SIZE_UNIT (elem_type), 1);
5570 if (vec_units & (vec_units - 1))
5572 error ("number of components of the vector not a power of two");
5573 return NULL_TREE;
5576 /* Build the vector type and replace. */
5578 *node = build_vector_type (elem_type, vec_units);
5579 rep_name = TYPE_NAME (rep_type);
5580 if (TREE_CODE (rep_name) == TYPE_DECL)
5581 rep_name = DECL_NAME (rep_name);
5582 TYPE_NAME (*node) = rep_name;
5583 TYPE_REPRESENTATIVE_ARRAY (*node) = rep_type;
5585 return NULL_TREE;
5588 /* ----------------------------------------------------------------------- *
5589 * BUILTIN FUNCTIONS *
5590 * ----------------------------------------------------------------------- */
5592 /* Worker for DEF_BUILTIN. Possibly define a builtin function with one or two
5593 names. Does not declare a non-__builtin_ function if flag_no_builtin, or
5594 if nonansi_p and flag_no_nonansi_builtin. */
5596 static void
5597 def_builtin_1 (enum built_in_function fncode,
5598 const char *name,
5599 enum built_in_class fnclass,
5600 tree fntype, tree libtype,
5601 bool both_p, bool fallback_p,
5602 bool nonansi_p ATTRIBUTE_UNUSED,
5603 tree fnattrs, bool implicit_p)
5605 tree decl;
5606 const char *libname;
5608 /* Preserve an already installed decl. It most likely was setup in advance
5609 (e.g. as part of the internal builtins) for specific reasons. */
5610 if (built_in_decls[(int) fncode] != NULL_TREE)
5611 return;
5613 gcc_assert ((!both_p && !fallback_p)
5614 || !strncmp (name, "__builtin_",
5615 strlen ("__builtin_")));
5617 libname = name + strlen ("__builtin_");
5618 decl = add_builtin_function (name, fntype, fncode, fnclass,
5619 (fallback_p ? libname : NULL),
5620 fnattrs);
5621 if (both_p)
5622 /* ??? This is normally further controlled by command-line options
5623 like -fno-builtin, but we don't have them for Ada. */
5624 add_builtin_function (libname, libtype, fncode, fnclass,
5625 NULL, fnattrs);
5627 built_in_decls[(int) fncode] = decl;
5628 if (implicit_p)
5629 implicit_built_in_decls[(int) fncode] = decl;
5632 static int flag_isoc94 = 0;
5633 static int flag_isoc99 = 0;
5635 /* Install what the common builtins.def offers. */
5637 static void
5638 install_builtin_functions (void)
5640 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5641 NONANSI_P, ATTRS, IMPLICIT, COND) \
5642 if (NAME && COND) \
5643 def_builtin_1 (ENUM, NAME, CLASS, \
5644 builtin_types[(int) TYPE], \
5645 builtin_types[(int) LIBTYPE], \
5646 BOTH_P, FALLBACK_P, NONANSI_P, \
5647 built_in_attributes[(int) ATTRS], IMPLICIT);
5648 #include "builtins.def"
5649 #undef DEF_BUILTIN
5652 /* ----------------------------------------------------------------------- *
5653 * BUILTIN FUNCTIONS *
5654 * ----------------------------------------------------------------------- */
5656 /* Install the builtin functions we might need. */
5658 void
5659 gnat_install_builtins (void)
5661 install_builtin_elementary_types ();
5662 install_builtin_function_types ();
5663 install_builtin_attributes ();
5665 /* Install builtins used by generic middle-end pieces first. Some of these
5666 know about internal specificities and control attributes accordingly, for
5667 instance __builtin_alloca vs no-throw and -fstack-check. We will ignore
5668 the generic definition from builtins.def. */
5669 build_common_builtin_nodes ();
5671 /* Now, install the target specific builtins, such as the AltiVec family on
5672 ppc, and the common set as exposed by builtins.def. */
5673 targetm.init_builtins ();
5674 install_builtin_functions ();
5677 #include "gt-ada-utils.h"
5678 #include "gtype-ada.h"