]> gcc.gnu.org Git - gcc.git/blame - gcc/ada/gcc-interface/utils.c
* bitmap.c (bitmap_find_bit): Remove useless test.
[gcc.git] / gcc / ada / gcc-interface / utils.c
CommitLineData
a1ab4c31
AC
1/****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * U T I L S *
6 * *
7 * C Implementation File *
8 * *
396a2ee2 9 * Copyright (C) 1992-2016, Free Software Foundation, Inc. *
a1ab4c31
AC
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 ****************************************************************************/
25
a1ab4c31
AC
26#include "config.h"
27#include "system.h"
28#include "coretypes.h"
2adfab87
AM
29#include "target.h"
30#include "function.h"
a1ab4c31 31#include "tree.h"
d8a2d370 32#include "stringpool.h"
2adfab87
AM
33#include "cgraph.h"
34#include "diagnostic.h"
35#include "alias.h"
36#include "fold-const.h"
d8a2d370
DN
37#include "stor-layout.h"
38#include "attribs.h"
39#include "varasm.h"
a1ab4c31
AC
40#include "toplev.h"
41#include "output.h"
a1ab4c31
AC
42#include "debug.h"
43#include "convert.h"
677f3fa8 44#include "common/common-target.h"
8713b7e4 45#include "langhooks.h"
8713b7e4 46#include "tree-dump.h"
a1ab4c31 47#include "tree-inline.h"
a1ab4c31
AC
48
49#include "ada.h"
50#include "types.h"
51#include "atree.h"
a1ab4c31 52#include "nlists.h"
a1ab4c31
AC
53#include "uintp.h"
54#include "fe.h"
55#include "sinfo.h"
56#include "einfo.h"
57#include "ada-tree.h"
58#include "gigi.h"
59
a1ab4c31
AC
60/* If nonzero, pretend we are allocating at global level. */
61int force_global;
62
caa9d12a
EB
63/* The default alignment of "double" floating-point types, i.e. floating
64 point types whose size is equal to 64 bits, or 0 if this alignment is
65 not specifically capped. */
66int double_float_alignment;
67
68/* The default alignment of "double" or larger scalar types, i.e. scalar
69 types whose size is greater or equal to 64 bits, or 0 if this alignment
70 is not specifically capped. */
71int double_scalar_alignment;
72
24228312
AC
73/* True if floating-point arithmetics may use wider intermediate results. */
74bool fp_arith_may_widen = true;
75
a1ab4c31
AC
76/* Tree nodes for the various types and decls we create. */
77tree gnat_std_decls[(int) ADT_LAST];
78
79/* Functions to call for each of the possible raise reasons. */
80tree gnat_raise_decls[(int) LAST_REASON_CODE + 1];
81
ca8e13e8 82/* Likewise, but with extra info for each of the possible raise reasons. */
437f8c1e
AC
83tree gnat_raise_decls_ext[(int) LAST_REASON_CODE + 1];
84
a1ab4c31
AC
85/* Forward declarations for handlers of attributes. */
86static tree handle_const_attribute (tree *, tree, tree, int, bool *);
87static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
88static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
89static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
90static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
91static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
92static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
0d6e14fd 93static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
f087ea44 94static tree handle_always_inline_attribute (tree *, tree, tree, int, bool *);
a1ab4c31
AC
95static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
96static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
2724e58f 97static tree handle_vector_size_attribute (tree *, tree, tree, int, bool *);
7948ae37 98static tree handle_vector_type_attribute (tree *, tree, tree, int, bool *);
a1ab4c31
AC
99
100/* Fake handler for attributes we don't properly support, typically because
101 they'd require dragging a lot of the common-c front-end circuitry. */
102static tree fake_attribute_handler (tree *, tree, tree, int, bool *);
103
104/* Table of machine-independent internal attributes for Ada. We support
105 this minimal set of attributes to accommodate the needs of builtins. */
106const struct attribute_spec gnat_internal_attribute_table[] =
107{
62d784f7
KT
108 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
109 affects_type_identity } */
110 { "const", 0, 0, true, false, false, handle_const_attribute,
111 false },
112 { "nothrow", 0, 0, true, false, false, handle_nothrow_attribute,
113 false },
114 { "pure", 0, 0, true, false, false, handle_pure_attribute,
115 false },
116 { "no vops", 0, 0, true, false, false, handle_novops_attribute,
117 false },
118 { "nonnull", 0, -1, false, true, true, handle_nonnull_attribute,
119 false },
120 { "sentinel", 0, 1, false, true, true, handle_sentinel_attribute,
121 false },
122 { "noreturn", 0, 0, true, false, false, handle_noreturn_attribute,
123 false },
124 { "leaf", 0, 0, true, false, false, handle_leaf_attribute,
125 false },
f087ea44
AC
126 { "always_inline",0, 0, true, false, false, handle_always_inline_attribute,
127 false },
62d784f7
KT
128 { "malloc", 0, 0, true, false, false, handle_malloc_attribute,
129 false },
130 { "type generic", 0, 0, false, true, true, handle_type_generic_attribute,
131 false },
132
133 { "vector_size", 1, 1, false, true, false, handle_vector_size_attribute,
134 false },
135 { "vector_type", 0, 0, false, true, false, handle_vector_type_attribute,
136 false },
137 { "may_alias", 0, 0, false, true, false, NULL, false },
a1ab4c31
AC
138
139 /* ??? format and format_arg are heavy and not supported, which actually
140 prevents support for stdio builtins, which we however declare as part
141 of the common builtins.def contents. */
62d784f7
KT
142 { "format", 3, 3, false, true, true, fake_attribute_handler, false },
143 { "format_arg", 1, 1, false, true, true, fake_attribute_handler, false },
a1ab4c31 144
62d784f7 145 { NULL, 0, 0, false, false, false, NULL, false }
a1ab4c31
AC
146};
147
148/* Associates a GNAT tree node to a GCC tree node. It is used in
149 `save_gnu_tree', `get_gnu_tree' and `present_gnu_tree'. See documentation
150 of `save_gnu_tree' for more info. */
151static GTY((length ("max_gnat_nodes"))) tree *associate_gnat_to_gnu;
152
153#define GET_GNU_TREE(GNAT_ENTITY) \
154 associate_gnat_to_gnu[(GNAT_ENTITY) - First_Node_Id]
155
156#define SET_GNU_TREE(GNAT_ENTITY,VAL) \
157 associate_gnat_to_gnu[(GNAT_ENTITY) - First_Node_Id] = (VAL)
158
159#define PRESENT_GNU_TREE(GNAT_ENTITY) \
160 (associate_gnat_to_gnu[(GNAT_ENTITY) - First_Node_Id] != NULL_TREE)
161
162/* Associates a GNAT entity to a GCC tree node used as a dummy, if any. */
163static GTY((length ("max_gnat_nodes"))) tree *dummy_node_table;
164
165#define GET_DUMMY_NODE(GNAT_ENTITY) \
166 dummy_node_table[(GNAT_ENTITY) - First_Node_Id]
167
168#define SET_DUMMY_NODE(GNAT_ENTITY,VAL) \
169 dummy_node_table[(GNAT_ENTITY) - First_Node_Id] = (VAL)
170
171#define PRESENT_DUMMY_NODE(GNAT_ENTITY) \
172 (dummy_node_table[(GNAT_ENTITY) - First_Node_Id] != NULL_TREE)
173
174/* This variable keeps a table for types for each precision so that we only
175 allocate each of them once. Signed and unsigned types are kept separate.
176
177 Note that these types are only used when fold-const requests something
178 special. Perhaps we should NOT share these types; we'll see how it
179 goes later. */
180static GTY(()) tree signed_and_unsigned_types[2 * MAX_BITS_PER_WORD + 1][2];
181
182/* Likewise for float types, but record these by mode. */
183static GTY(()) tree float_types[NUM_MACHINE_MODES];
184
185/* For each binding contour we allocate a binding_level structure to indicate
186 the binding depth. */
187
d1b38208 188struct GTY((chain_next ("%h.chain"))) gnat_binding_level {
a1ab4c31
AC
189 /* The binding level containing this one (the enclosing binding level). */
190 struct gnat_binding_level *chain;
191 /* The BLOCK node for this level. */
192 tree block;
193 /* If nonzero, the setjmp buffer that needs to be updated for any
194 variable-sized definition within this context. */
195 tree jmpbuf_decl;
196};
197
198/* The binding level currently in effect. */
199static GTY(()) struct gnat_binding_level *current_binding_level;
200
201/* A chain of gnat_binding_level structures awaiting reuse. */
202static GTY((deletable)) struct gnat_binding_level *free_binding_level;
203
228ee426
EB
204/* The context to be used for global declarations. */
205static GTY(()) tree global_context;
206
a22b794d
EB
207/* An array of global declarations. */
208static GTY(()) vec<tree, va_gc> *global_decls;
a1ab4c31
AC
209
210/* An array of builtin function declarations. */
9771b263 211static GTY(()) vec<tree, va_gc> *builtin_decls;
a1ab4c31 212
a1ab4c31
AC
213/* A chain of unused BLOCK nodes. */
214static GTY((deletable)) tree free_block_chain;
215
842d4ee2
EB
216/* A hash table of padded types. It is modelled on the generic type
217 hash table in tree.c, which must thus be used as a reference. */
d242408f
TS
218
219struct GTY((for_user)) pad_type_hash {
842d4ee2
EB
220 unsigned long hash;
221 tree type;
222};
223
6c907cff 224struct pad_type_hasher : ggc_cache_ptr_hash<pad_type_hash>
d242408f
TS
225{
226 static inline hashval_t hash (pad_type_hash *t) { return t->hash; }
227 static bool equal (pad_type_hash *a, pad_type_hash *b);
08ec2754 228 static int keep_cache_entry (pad_type_hash *&);
d242408f
TS
229};
230
231static GTY ((cache))
232 hash_table<pad_type_hasher> *pad_type_hash_table;
842d4ee2 233
a1ab4c31
AC
234static tree merge_sizes (tree, tree, tree, bool, bool);
235static tree compute_related_constant (tree, tree);
236static tree split_plus (tree, tree *);
ef4bddc2 237static tree float_type_for_precision (int, machine_mode);
a1ab4c31 238static tree convert_to_fat_pointer (tree, tree);
5c475ba9 239static unsigned int scale_by_factor_of (tree, unsigned int);
a1ab4c31 240static bool potential_alignment_gap (tree, tree, tree);
9a30c7c4 241
afc737f0
EB
242/* Linked list used as a queue to defer the initialization of the DECL_CONTEXT
243 of ..._DECL nodes and of the TYPE_CONTEXT of ..._TYPE nodes. */
9a30c7c4
AC
244struct deferred_decl_context_node
245{
afc737f0
EB
246 /* The ..._DECL node to work on. */
247 tree decl;
248
249 /* The corresponding entity's Scope. */
250 Entity_Id gnat_scope;
251
252 /* The value of force_global when DECL was pushed. */
253 int force_global;
254
255 /* The list of ..._TYPE nodes to propagate the context to. */
256 vec<tree> types;
257
258 /* The next queue item. */
259 struct deferred_decl_context_node *next;
9a30c7c4
AC
260};
261
262static struct deferred_decl_context_node *deferred_decl_context_queue = NULL;
263
264/* Defer the initialization of DECL's DECL_CONTEXT attribute, scheduling to
265 feed it with the elaboration of GNAT_SCOPE. */
266static struct deferred_decl_context_node *
267add_deferred_decl_context (tree decl, Entity_Id gnat_scope, int force_global);
268
269/* Defer the initialization of TYPE's TYPE_CONTEXT attribute, scheduling to
270 feed it with the DECL_CONTEXT computed as part of N as soon as it is
271 computed. */
272static void add_deferred_type_context (struct deferred_decl_context_node *n,
273 tree type);
a1ab4c31 274\f
842d4ee2 275/* Initialize data structures of the utils.c module. */
a1ab4c31
AC
276
277void
842d4ee2 278init_gnat_utils (void)
a1ab4c31 279{
842d4ee2 280 /* Initialize the association of GNAT nodes to GCC trees. */
766090c2 281 associate_gnat_to_gnu = ggc_cleared_vec_alloc<tree> (max_gnat_nodes);
842d4ee2
EB
282
283 /* Initialize the association of GNAT nodes to GCC trees as dummies. */
766090c2 284 dummy_node_table = ggc_cleared_vec_alloc<tree> (max_gnat_nodes);
842d4ee2
EB
285
286 /* Initialize the hash table of padded types. */
d242408f 287 pad_type_hash_table = hash_table<pad_type_hasher>::create_ggc (512);
a1ab4c31
AC
288}
289
842d4ee2 290/* Destroy data structures of the utils.c module. */
f04b8d69
EB
291
292void
842d4ee2 293destroy_gnat_utils (void)
f04b8d69 294{
842d4ee2 295 /* Destroy the association of GNAT nodes to GCC trees. */
f04b8d69
EB
296 ggc_free (associate_gnat_to_gnu);
297 associate_gnat_to_gnu = NULL;
f04b8d69 298
842d4ee2
EB
299 /* Destroy the association of GNAT nodes to GCC trees as dummies. */
300 ggc_free (dummy_node_table);
301 dummy_node_table = NULL;
302
303 /* Destroy the hash table of padded types. */
d242408f 304 pad_type_hash_table->empty ();
842d4ee2 305 pad_type_hash_table = NULL;
842d4ee2
EB
306}
307\f
a1d8cc63
EB
308/* GNAT_ENTITY is a GNAT tree node for an entity. Associate GNU_DECL, a GCC
309 tree node, with GNAT_ENTITY. If GNU_DECL is not a ..._DECL node, abort.
310 If NO_CHECK is true, the latter check is suppressed.
a1ab4c31 311
a1d8cc63 312 If GNU_DECL is zero, reset a previous association. */
a1ab4c31
AC
313
314void
315save_gnu_tree (Entity_Id gnat_entity, tree gnu_decl, bool no_check)
316{
317 /* Check that GNAT_ENTITY is not already defined and that it is being set
a1d8cc63 318 to something which is a decl. If that is not the case, this usually
a1ab4c31
AC
319 means GNAT_ENTITY is defined twice, but occasionally is due to some
320 Gigi problem. */
321 gcc_assert (!(gnu_decl
322 && (PRESENT_GNU_TREE (gnat_entity)
323 || (!no_check && !DECL_P (gnu_decl)))));
324
325 SET_GNU_TREE (gnat_entity, gnu_decl);
326}
327
a1d8cc63
EB
328/* GNAT_ENTITY is a GNAT tree node for an entity. Return the GCC tree node
329 that was associated with it. If there is no such tree node, abort.
a1ab4c31
AC
330
331 In some cases, such as delayed elaboration or expressions that need to
332 be elaborated only once, GNAT_ENTITY is really not an entity. */
333
334tree
335get_gnu_tree (Entity_Id gnat_entity)
336{
337 gcc_assert (PRESENT_GNU_TREE (gnat_entity));
338 return GET_GNU_TREE (gnat_entity);
339}
340
341/* Return nonzero if a GCC tree has been associated with GNAT_ENTITY. */
342
343bool
344present_gnu_tree (Entity_Id gnat_entity)
345{
346 return PRESENT_GNU_TREE (gnat_entity);
347}
348\f
a1ab4c31
AC
349/* Make a dummy type corresponding to GNAT_TYPE. */
350
351tree
352make_dummy_type (Entity_Id gnat_type)
353{
bf0b0e5e 354 Entity_Id gnat_equiv = Gigi_Equivalent_Type (Underlying_Type (gnat_type));
a1ab4c31
AC
355 tree gnu_type;
356
a1ab4c31
AC
357 /* If there was no equivalent type (can only happen when just annotating
358 types) or underlying type, go back to the original type. */
bf0b0e5e
AC
359 if (No (gnat_equiv))
360 gnat_equiv = gnat_type;
a1ab4c31
AC
361
362 /* If it there already a dummy type, use that one. Else make one. */
bf0b0e5e
AC
363 if (PRESENT_DUMMY_NODE (gnat_equiv))
364 return GET_DUMMY_NODE (gnat_equiv);
a1ab4c31
AC
365
366 /* If this is a record, make a RECORD_TYPE or UNION_TYPE; else make
367 an ENUMERAL_TYPE. */
bf0b0e5e
AC
368 gnu_type = make_node (Is_Record_Type (gnat_equiv)
369 ? tree_code_for_record_type (gnat_equiv)
a1ab4c31
AC
370 : ENUMERAL_TYPE);
371 TYPE_NAME (gnu_type) = get_entity_name (gnat_type);
372 TYPE_DUMMY_P (gnu_type) = 1;
10069d53
EB
373 TYPE_STUB_DECL (gnu_type)
374 = create_type_stub_decl (TYPE_NAME (gnu_type), gnu_type);
bf0b0e5e 375 if (Is_By_Reference_Type (gnat_equiv))
a0b8b1b7 376 TYPE_BY_REFERENCE_P (gnu_type) = 1;
a1ab4c31 377
bf0b0e5e 378 SET_DUMMY_NODE (gnat_equiv, gnu_type);
a1ab4c31
AC
379
380 return gnu_type;
381}
e3edbd56
EB
382
383/* Return the dummy type that was made for GNAT_TYPE, if any. */
384
385tree
386get_dummy_type (Entity_Id gnat_type)
387{
388 return GET_DUMMY_NODE (gnat_type);
389}
390
391/* Build dummy fat and thin pointer types whose designated type is specified
392 by GNAT_DESIG_TYPE/GNU_DESIG_TYPE and attach them to the latter. */
393
394void
395build_dummy_unc_pointer_types (Entity_Id gnat_desig_type, tree gnu_desig_type)
396{
397 tree gnu_template_type, gnu_ptr_template, gnu_array_type, gnu_ptr_array;
398 tree gnu_fat_type, fields, gnu_object_type;
399
400 gnu_template_type = make_node (RECORD_TYPE);
401 TYPE_NAME (gnu_template_type) = create_concat_name (gnat_desig_type, "XUB");
402 TYPE_DUMMY_P (gnu_template_type) = 1;
403 gnu_ptr_template = build_pointer_type (gnu_template_type);
404
405 gnu_array_type = make_node (ENUMERAL_TYPE);
406 TYPE_NAME (gnu_array_type) = create_concat_name (gnat_desig_type, "XUA");
407 TYPE_DUMMY_P (gnu_array_type) = 1;
408 gnu_ptr_array = build_pointer_type (gnu_array_type);
409
410 gnu_fat_type = make_node (RECORD_TYPE);
411 /* Build a stub DECL to trigger the special processing for fat pointer types
412 in gnat_pushdecl. */
413 TYPE_NAME (gnu_fat_type)
414 = create_type_stub_decl (create_concat_name (gnat_desig_type, "XUP"),
415 gnu_fat_type);
416 fields = create_field_decl (get_identifier ("P_ARRAY"), gnu_ptr_array,
417 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
418 DECL_CHAIN (fields)
419 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
420 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
421 finish_fat_pointer_type (gnu_fat_type, fields);
422 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_desig_type);
423 /* Suppress debug info until after the type is completed. */
424 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (gnu_fat_type)) = 1;
425
426 gnu_object_type = make_node (RECORD_TYPE);
427 TYPE_NAME (gnu_object_type) = create_concat_name (gnat_desig_type, "XUT");
428 TYPE_DUMMY_P (gnu_object_type) = 1;
429
430 TYPE_POINTER_TO (gnu_desig_type) = gnu_fat_type;
1e55d29a 431 TYPE_REFERENCE_TO (gnu_desig_type) = gnu_fat_type;
e3edbd56
EB
432 TYPE_OBJECT_RECORD_TYPE (gnu_desig_type) = gnu_object_type;
433}
a1ab4c31 434\f
c99c0026 435/* Return true if we are in the global binding level. */
a1ab4c31 436
c99c0026 437bool
a1ab4c31
AC
438global_bindings_p (void)
439{
7c775aca 440 return force_global || !current_function_decl;
a1ab4c31
AC
441}
442
a09d56d8 443/* Enter a new binding level. */
a1ab4c31
AC
444
445void
c6bd4220 446gnat_pushlevel (void)
a1ab4c31
AC
447{
448 struct gnat_binding_level *newlevel = NULL;
449
450 /* Reuse a struct for this binding level, if there is one. */
451 if (free_binding_level)
452 {
453 newlevel = free_binding_level;
454 free_binding_level = free_binding_level->chain;
455 }
456 else
766090c2 457 newlevel = ggc_alloc<gnat_binding_level> ();
a1ab4c31
AC
458
459 /* Use a free BLOCK, if any; otherwise, allocate one. */
460 if (free_block_chain)
461 {
462 newlevel->block = free_block_chain;
463 free_block_chain = BLOCK_CHAIN (free_block_chain);
464 BLOCK_CHAIN (newlevel->block) = NULL_TREE;
465 }
466 else
467 newlevel->block = make_node (BLOCK);
468
469 /* Point the BLOCK we just made to its parent. */
470 if (current_binding_level)
471 BLOCK_SUPERCONTEXT (newlevel->block) = current_binding_level->block;
472
a09d56d8
EB
473 BLOCK_VARS (newlevel->block) = NULL_TREE;
474 BLOCK_SUBBLOCKS (newlevel->block) = NULL_TREE;
a1ab4c31
AC
475 TREE_USED (newlevel->block) = 1;
476
a09d56d8 477 /* Add this level to the front of the chain (stack) of active levels. */
a1ab4c31
AC
478 newlevel->chain = current_binding_level;
479 newlevel->jmpbuf_decl = NULL_TREE;
480 current_binding_level = newlevel;
481}
482
483/* Set SUPERCONTEXT of the BLOCK for the current binding level to FNDECL
484 and point FNDECL to this BLOCK. */
485
486void
487set_current_block_context (tree fndecl)
488{
489 BLOCK_SUPERCONTEXT (current_binding_level->block) = fndecl;
490 DECL_INITIAL (fndecl) = current_binding_level->block;
a09d56d8 491 set_block_for_group (current_binding_level->block);
a1ab4c31
AC
492}
493
494/* Set the jmpbuf_decl for the current binding level to DECL. */
495
496void
497set_block_jmpbuf_decl (tree decl)
498{
499 current_binding_level->jmpbuf_decl = decl;
500}
501
502/* Get the jmpbuf_decl, if any, for the current binding level. */
503
504tree
c6bd4220 505get_block_jmpbuf_decl (void)
a1ab4c31
AC
506{
507 return current_binding_level->jmpbuf_decl;
508}
509
a09d56d8 510/* Exit a binding level. Set any BLOCK into the current code group. */
a1ab4c31
AC
511
512void
c6bd4220 513gnat_poplevel (void)
a1ab4c31
AC
514{
515 struct gnat_binding_level *level = current_binding_level;
516 tree block = level->block;
517
518 BLOCK_VARS (block) = nreverse (BLOCK_VARS (block));
72ac05b0 519 BLOCK_SUBBLOCKS (block) = blocks_nreverse (BLOCK_SUBBLOCKS (block));
a1ab4c31
AC
520
521 /* If this is a function-level BLOCK don't do anything. Otherwise, if there
522 are no variables free the block and merge its subblocks into those of its
a09d56d8 523 parent block. Otherwise, add it to the list of its parent. */
a1ab4c31
AC
524 if (TREE_CODE (BLOCK_SUPERCONTEXT (block)) == FUNCTION_DECL)
525 ;
7c775aca 526 else if (!BLOCK_VARS (block))
a1ab4c31
AC
527 {
528 BLOCK_SUBBLOCKS (level->chain->block)
61e46a7d
NF
529 = block_chainon (BLOCK_SUBBLOCKS (block),
530 BLOCK_SUBBLOCKS (level->chain->block));
a1ab4c31
AC
531 BLOCK_CHAIN (block) = free_block_chain;
532 free_block_chain = block;
533 }
534 else
535 {
536 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (level->chain->block);
537 BLOCK_SUBBLOCKS (level->chain->block) = block;
538 TREE_USED (block) = 1;
539 set_block_for_group (block);
540 }
541
542 /* Free this binding structure. */
543 current_binding_level = level->chain;
544 level->chain = free_binding_level;
545 free_binding_level = level;
546}
547
2231f17f
EB
548/* Exit a binding level and discard the associated BLOCK. */
549
550void
551gnat_zaplevel (void)
552{
553 struct gnat_binding_level *level = current_binding_level;
554 tree block = level->block;
555
556 BLOCK_CHAIN (block) = free_block_chain;
557 free_block_chain = block;
558
559 /* Free this binding structure. */
560 current_binding_level = level->chain;
561 level->chain = free_binding_level;
562 free_binding_level = level;
563}
a1ab4c31 564\f
4708440c
EB
565/* Set the context of TYPE and its parallel types (if any) to CONTEXT. */
566
567static void
568gnat_set_type_context (tree type, tree context)
569{
570 tree decl = TYPE_STUB_DECL (type);
571
572 TYPE_CONTEXT (type) = context;
573
574 while (decl && DECL_PARALLEL_TYPE (decl))
575 {
24d4b3d5
AC
576 tree parallel_type = DECL_PARALLEL_TYPE (decl);
577
578 /* Give a context to the parallel types and their stub decl, if any.
579 Some parallel types seems to be present in multiple parallel type
580 chains, so don't mess with their context if they already have one. */
7c775aca 581 if (!TYPE_CONTEXT (parallel_type))
24d4b3d5 582 {
7c775aca 583 if (TYPE_STUB_DECL (parallel_type))
24d4b3d5
AC
584 DECL_CONTEXT (TYPE_STUB_DECL (parallel_type)) = context;
585 TYPE_CONTEXT (parallel_type) = context;
586 }
587
4708440c
EB
588 decl = TYPE_STUB_DECL (DECL_PARALLEL_TYPE (decl));
589 }
590}
591
9a30c7c4
AC
592/* Return the innermost scope, starting at GNAT_NODE, we are be interested in
593 the debug info, or Empty if there is no such scope. If not NULL, set
594 IS_SUBPROGRAM to whether the returned entity is a subprogram. */
595
1d4b96e0 596Entity_Id
9a30c7c4
AC
597get_debug_scope (Node_Id gnat_node, bool *is_subprogram)
598{
599 Entity_Id gnat_entity;
600
601 if (is_subprogram)
602 *is_subprogram = false;
603
1d4b96e0
AC
604 if (Nkind (gnat_node) == N_Defining_Identifier
605 || Nkind (gnat_node) == N_Defining_Operator_Symbol)
9a30c7c4
AC
606 gnat_entity = Scope (gnat_node);
607 else
608 return Empty;
609
610 while (Present (gnat_entity))
611 {
612 switch (Ekind (gnat_entity))
613 {
614 case E_Function:
615 case E_Procedure:
616 if (Present (Protected_Body_Subprogram (gnat_entity)))
617 gnat_entity = Protected_Body_Subprogram (gnat_entity);
618
619 /* If the scope is a subprogram, then just rely on
620 current_function_decl, so that we don't have to defer
621 anything. This is needed because other places rely on the
622 validity of the DECL_CONTEXT attribute of FUNCTION_DECL nodes. */
623 if (is_subprogram)
624 *is_subprogram = true;
625 return gnat_entity;
626
627 case E_Record_Type:
628 case E_Record_Subtype:
629 return gnat_entity;
630
631 default:
632 /* By default, we are not interested in this particular scope: go to
633 the outer one. */
634 break;
635 }
7c775aca 636
9a30c7c4
AC
637 gnat_entity = Scope (gnat_entity);
638 }
7c775aca 639
9a30c7c4
AC
640 return Empty;
641}
642
7c775aca
EB
643/* If N is NULL, set TYPE's context to CONTEXT. Defer this to the processing
644 of N otherwise. */
9a30c7c4
AC
645
646static void
7c775aca 647defer_or_set_type_context (tree type, tree context,
9a30c7c4
AC
648 struct deferred_decl_context_node *n)
649{
650 if (n)
651 add_deferred_type_context (n, type);
652 else
653 gnat_set_type_context (type, context);
654}
655
7c775aca 656/* Return global_context, but create it first if need be. */
9a30c7c4
AC
657
658static tree
659get_global_context (void)
660{
661 if (!global_context)
881a5e60
PMR
662 {
663 global_context = build_translation_unit_decl (NULL_TREE);
664 debug_hooks->register_main_translation_unit (global_context);
665 }
7c775aca 666
9a30c7c4
AC
667 return global_context;
668}
669
228ee426
EB
670/* Record DECL as belonging to the current lexical scope and use GNAT_NODE
671 for location information and flag propagation. */
a1ab4c31
AC
672
673void
674gnat_pushdecl (tree decl, Node_Id gnat_node)
675{
9a30c7c4
AC
676 tree context = NULL_TREE;
677 struct deferred_decl_context_node *deferred_decl_context = NULL;
678
679 /* If explicitely asked to make DECL global or if it's an imported nested
680 object, short-circuit the regular Scope-based context computation. */
681 if (!((TREE_PUBLIC (decl) && DECL_EXTERNAL (decl)) || force_global == 1))
a1ab4c31 682 {
9a30c7c4
AC
683 /* Rely on the GNAT scope, or fallback to the current_function_decl if
684 the GNAT scope reached the global scope, if it reached a subprogram
685 or the declaration is a subprogram or a variable (for them we skip
686 intermediate context types because the subprogram body elaboration
687 machinery and the inliner both expect a subprogram context).
688
689 Falling back to current_function_decl is necessary for implicit
690 subprograms created by gigi, such as the elaboration subprograms. */
691 bool context_is_subprogram = false;
692 const Entity_Id gnat_scope
693 = get_debug_scope (gnat_node, &context_is_subprogram);
694
695 if (Present (gnat_scope)
696 && !context_is_subprogram
697 && TREE_CODE (decl) != FUNCTION_DECL
698 && TREE_CODE (decl) != VAR_DECL)
699 /* Always assume the scope has not been elaborated, thus defer the
700 context propagation to the time its elaboration will be
701 available. */
702 deferred_decl_context
703 = add_deferred_decl_context (decl, gnat_scope, force_global);
704
705 /* External declarations (when force_global > 0) may not be in a
706 local context. */
7c775aca 707 else if (current_function_decl && force_global == 0)
9a30c7c4 708 context = current_function_decl;
a1ab4c31
AC
709 }
710
9a30c7c4 711 /* If either we are forced to be in global mode or if both the GNAT scope and
7c775aca 712 the current_function_decl did not help in determining the context, use the
9a30c7c4 713 global scope. */
7c775aca 714 if (!deferred_decl_context && !context)
9a30c7c4
AC
715 context = get_global_context ();
716
717 /* Functions imported in another function are not really nested.
718 For really nested functions mark them initially as needing
719 a static chain for uses of that flag before unnesting;
720 lower_nested_functions will then recompute it. */
721 if (TREE_CODE (decl) == FUNCTION_DECL
722 && !TREE_PUBLIC (decl)
7c775aca 723 && context
9a30c7c4 724 && (TREE_CODE (context) == FUNCTION_DECL
7c775aca 725 || decl_function_context (context)))
9a30c7c4
AC
726 DECL_STATIC_CHAIN (decl) = 1;
727
728 if (!deferred_decl_context)
729 DECL_CONTEXT (decl) = context;
730
228ee426 731 TREE_NO_WARNING (decl) = (No (gnat_node) || Warnings_Off (gnat_node));
a1ab4c31
AC
732
733 /* Set the location of DECL and emit a declaration for it. */
e8fa3dcd 734 if (Present (gnat_node) && !renaming_from_generic_instantiation_p (gnat_node))
a1ab4c31 735 Sloc_to_locus (Sloc (gnat_node), &DECL_SOURCE_LOCATION (decl));
228ee426 736
a1ab4c31
AC
737 add_decl_expr (decl, gnat_node);
738
739 /* Put the declaration on the list. The list of declarations is in reverse
2231f17f
EB
740 order. The list will be reversed later. Put global declarations in the
741 globals list and local ones in the current block. But skip TYPE_DECLs
742 for UNCONSTRAINED_ARRAY_TYPE in both cases, as they will cause trouble
743 with the debugger and aren't needed anyway. */
744 if (!(TREE_CODE (decl) == TYPE_DECL
745 && TREE_CODE (TREE_TYPE (decl)) == UNCONSTRAINED_ARRAY_TYPE))
a1ab4c31 746 {
9083aacd 747 if (DECL_EXTERNAL (decl))
a1ab4c31 748 {
a1ab4c31 749 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
9771b263 750 vec_safe_push (builtin_decls, decl);
a1ab4c31 751 }
9083aacd 752 else if (global_bindings_p ())
a22b794d 753 vec_safe_push (global_decls, decl);
9083aacd 754 else
a1ab4c31 755 {
a963da4d
EB
756 DECL_CHAIN (decl) = BLOCK_VARS (current_binding_level->block);
757 BLOCK_VARS (current_binding_level->block) = decl;
a1ab4c31
AC
758 }
759 }
760
aef308d0 761 /* For the declaration of a type, set its name either if it isn't already
10069d53 762 set or if the previous type name was not derived from a source name.
a1ab4c31 763 We'd rather have the type named with a real name and all the pointer
aef308d0
PMR
764 types to the same object have the same node, except when the names are
765 both derived from source names. */
a1ab4c31
AC
766 if (TREE_CODE (decl) == TYPE_DECL && DECL_NAME (decl))
767 {
768 tree t = TREE_TYPE (decl);
769
79714815
EB
770 /* Array and pointer types aren't tagged types in the C sense so we need
771 to generate a typedef in DWARF for them and make sure it is preserved,
772 unless the type is artificial. */
aef308d0 773 if (!(TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
79714815
EB
774 && ((TREE_CODE (t) != ARRAY_TYPE && TREE_CODE (t) != POINTER_TYPE)
775 || DECL_ARTIFICIAL (decl)))
776 ;
777 /* For array and pointer types, create the DECL_ORIGINAL_TYPE that will
778 generate the typedef in DWARF. Also do that for fat pointer types
779 because, even though they are tagged types in the C sense, they are
780 still XUP types attached to the base array type at this point. */
aef308d0 781 else if (!DECL_ARTIFICIAL (decl)
79714815
EB
782 && (TREE_CODE (t) == ARRAY_TYPE
783 || TREE_CODE (t) == POINTER_TYPE
784 || TYPE_IS_FAT_POINTER_P (t)))
a1ab4c31 785 {
aef308d0 786 tree tt;
1e039275
EB
787 /* ??? Copy and original type are not supposed to be variant but we
788 really need a variant for the placeholder machinery to work. */
aef308d0
PMR
789 if (TYPE_IS_FAT_POINTER_P (t))
790 tt = build_variant_type_copy (t);
791 else
1e039275
EB
792 {
793 /* TYPE_NEXT_PTR_TO is a chain of main variants. */
794 tt = build_distinct_type_copy (TYPE_MAIN_VARIANT (t));
79714815
EB
795 if (TREE_CODE (t) == POINTER_TYPE)
796 TYPE_NEXT_PTR_TO (TYPE_MAIN_VARIANT (t)) = tt;
1e039275
EB
797 tt = build_qualified_type (tt, TYPE_QUALS (t));
798 }
a1ab4c31 799 TYPE_NAME (tt) = decl;
9a30c7c4
AC
800 defer_or_set_type_context (tt,
801 DECL_CONTEXT (decl),
802 deferred_decl_context);
a1ab4c31
AC
803 TREE_USED (tt) = TREE_USED (t);
804 TREE_TYPE (decl) = tt;
79714815 805 if (TYPE_NAME (t)
aef308d0
PMR
806 && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
807 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
40c88b94
EB
808 DECL_ORIGINAL_TYPE (decl) = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
809 else
810 DECL_ORIGINAL_TYPE (decl) = t;
79714815
EB
811 /* Array types need to have a name so that they can be related to
812 their GNAT encodings. */
813 if (TREE_CODE (t) == ARRAY_TYPE && !TYPE_NAME (t))
814 TYPE_NAME (t) = DECL_NAME (decl);
e3edbd56 815 t = NULL_TREE;
a1ab4c31 816 }
79714815 817 else if (TYPE_NAME (t)
aef308d0
PMR
818 && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
819 && DECL_ARTIFICIAL (TYPE_NAME (t)) && !DECL_ARTIFICIAL (decl))
a1ab4c31
AC
820 ;
821 else
822 t = NULL_TREE;
823
79714815
EB
824 /* Propagate the name to all the variants, this is needed for the type
825 qualifiers machinery to work properly (see check_qualified_type).
826 Also propagate the context to them. Note that it will be propagated
827 to all parallel types too thanks to gnat_set_type_context. */
a1ab4c31
AC
828 if (t)
829 for (t = TYPE_MAIN_VARIANT (t); t; t = TYPE_NEXT_VARIANT (t))
79714815
EB
830 /* ??? Because of the previous kludge, we can have variants of fat
831 pointer types with different names. */
832 if (!(TYPE_IS_FAT_POINTER_P (t)
833 && TYPE_NAME (t)
834 && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL))
d4d05b52
EB
835 {
836 TYPE_NAME (t) = decl;
9a30c7c4
AC
837 defer_or_set_type_context (t,
838 DECL_CONTEXT (decl),
839 deferred_decl_context);
d4d05b52 840 }
a1ab4c31
AC
841 }
842}
843\f
842d4ee2
EB
844/* Create a record type that contains a SIZE bytes long field of TYPE with a
845 starting bit position so that it is aligned to ALIGN bits, and leaving at
846 least ROOM bytes free before the field. BASE_ALIGN is the alignment the
0746af5e
EB
847 record is guaranteed to get. GNAT_NODE is used for the position of the
848 associated TYPE_DECL. */
842d4ee2
EB
849
850tree
851make_aligning_type (tree type, unsigned int align, tree size,
0746af5e 852 unsigned int base_align, int room, Node_Id gnat_node)
842d4ee2
EB
853{
854 /* We will be crafting a record type with one field at a position set to be
855 the next multiple of ALIGN past record'address + room bytes. We use a
856 record placeholder to express record'address. */
857 tree record_type = make_node (RECORD_TYPE);
858 tree record = build0 (PLACEHOLDER_EXPR, record_type);
859
860 tree record_addr_st
861 = convert (sizetype, build_unary_op (ADDR_EXPR, NULL_TREE, record));
862
863 /* The diagram below summarizes the shape of what we manipulate:
864
865 <--------- pos ---------->
866 { +------------+-------------+-----------------+
867 record =>{ |############| ... | field (type) |
868 { +------------+-------------+-----------------+
869 |<-- room -->|<- voffset ->|<---- size ----->|
870 o o
871 | |
872 record_addr vblock_addr
873
874 Every length is in sizetype bytes there, except "pos" which has to be
875 set as a bit position in the GCC tree for the record. */
876 tree room_st = size_int (room);
877 tree vblock_addr_st = size_binop (PLUS_EXPR, record_addr_st, room_st);
878 tree voffset_st, pos, field;
879
9dba4b55 880 tree name = TYPE_IDENTIFIER (type);
842d4ee2 881
842d4ee2
EB
882 name = concat_name (name, "ALIGN");
883 TYPE_NAME (record_type) = name;
884
885 /* Compute VOFFSET and then POS. The next byte position multiple of some
886 alignment after some address is obtained by "and"ing the alignment minus
887 1 with the two's complement of the address. */
888 voffset_st = size_binop (BIT_AND_EXPR,
889 fold_build1 (NEGATE_EXPR, sizetype, vblock_addr_st),
890 size_int ((align / BITS_PER_UNIT) - 1));
891
892 /* POS = (ROOM + VOFFSET) * BIT_PER_UNIT, in bitsizetype. */
893 pos = size_binop (MULT_EXPR,
894 convert (bitsizetype,
895 size_binop (PLUS_EXPR, room_st, voffset_st)),
896 bitsize_unit_node);
897
898 /* Craft the GCC record representation. We exceptionally do everything
899 manually here because 1) our generic circuitry is not quite ready to
900 handle the complex position/size expressions we are setting up, 2) we
901 have a strong simplifying factor at hand: we know the maximum possible
902 value of voffset, and 3) we have to set/reset at least the sizes in
903 accordance with this maximum value anyway, as we need them to convey
904 what should be "alloc"ated for this type.
905
906 Use -1 as the 'addressable' indication for the field to prevent the
907 creation of a bitfield. We don't need one, it would have damaging
908 consequences on the alignment computation, and create_field_decl would
909 make one without this special argument, for instance because of the
910 complex position expression. */
911 field = create_field_decl (get_identifier ("F"), type, record_type, size,
912 pos, 1, -1);
913 TYPE_FIELDS (record_type) = field;
914
fe37c7af 915 SET_TYPE_ALIGN (record_type, base_align);
842d4ee2
EB
916 TYPE_USER_ALIGN (record_type) = 1;
917
918 TYPE_SIZE (record_type)
919 = size_binop (PLUS_EXPR,
920 size_binop (MULT_EXPR, convert (bitsizetype, size),
921 bitsize_unit_node),
922 bitsize_int (align + room * BITS_PER_UNIT));
923 TYPE_SIZE_UNIT (record_type)
924 = size_binop (PLUS_EXPR, size,
925 size_int (room + align / BITS_PER_UNIT));
926
927 SET_TYPE_MODE (record_type, BLKmode);
928 relate_alias_sets (record_type, type, ALIAS_SET_COPY);
929
930 /* Declare it now since it will never be declared otherwise. This is
931 necessary to ensure that its subtrees are properly marked. */
74746d49 932 create_type_decl (name, record_type, true, false, gnat_node);
842d4ee2
EB
933
934 return record_type;
935}
936
937/* TYPE is a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE that is being used
938 as the field type of a packed record if IN_RECORD is true, or as the
939 component type of a packed array if IN_RECORD is false. See if we can
b3f75672
EB
940 rewrite it either as a type that has non-BLKmode, which we can pack
941 tighter in the packed record case, or as a smaller type with at most
942 MAX_ALIGN alignment if the value is non-zero. If so, return the new
943 type; if not, return the original type. */
842d4ee2
EB
944
945tree
b3f75672 946make_packable_type (tree type, bool in_record, unsigned int max_align)
842d4ee2 947{
ae7e9ddd 948 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE (type));
842d4ee2 949 unsigned HOST_WIDE_INT new_size;
b3f75672
EB
950 unsigned int align = TYPE_ALIGN (type);
951 unsigned int new_align;
842d4ee2
EB
952
953 /* No point in doing anything if the size is zero. */
954 if (size == 0)
955 return type;
956
b3f75672 957 tree new_type = make_node (TREE_CODE (type));
842d4ee2
EB
958
959 /* Copy the name and flags from the old type to that of the new.
960 Note that we rely on the pointer equality created here for
961 TYPE_NAME to look through conversions in various places. */
962 TYPE_NAME (new_type) = TYPE_NAME (type);
963 TYPE_JUSTIFIED_MODULAR_P (new_type) = TYPE_JUSTIFIED_MODULAR_P (type);
964 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
ee45a32d 965 TYPE_REVERSE_STORAGE_ORDER (new_type) = TYPE_REVERSE_STORAGE_ORDER (type);
842d4ee2
EB
966 if (TREE_CODE (type) == RECORD_TYPE)
967 TYPE_PADDING_P (new_type) = TYPE_PADDING_P (type);
968
969 /* If we are in a record and have a small size, set the alignment to
970 try for an integral mode. Otherwise set it to try for a smaller
971 type with BLKmode. */
972 if (in_record && size <= MAX_FIXED_MODE_SIZE)
973 {
b3f75672
EB
974 new_size = ceil_pow2 (size);
975 new_align = MIN (new_size, BIGGEST_ALIGNMENT);
976 SET_TYPE_ALIGN (new_type, new_align);
842d4ee2
EB
977 }
978 else
979 {
842d4ee2
EB
980 /* Do not try to shrink the size if the RM size is not constant. */
981 if (TYPE_CONTAINS_TEMPLATE_P (type)
cc269bb6 982 || !tree_fits_uhwi_p (TYPE_ADA_SIZE (type)))
842d4ee2
EB
983 return type;
984
985 /* Round the RM size up to a unit boundary to get the minimal size
b3f75672
EB
986 for a BLKmode record. Give up if it's already the size and we
987 don't need to lower the alignment. */
eb1ce453 988 new_size = tree_to_uhwi (TYPE_ADA_SIZE (type));
842d4ee2 989 new_size = (new_size + BITS_PER_UNIT - 1) & -BITS_PER_UNIT;
b3f75672 990 if (new_size == size && (max_align == 0 || align <= max_align))
842d4ee2
EB
991 return type;
992
b3f75672
EB
993 new_align = MIN (new_size & -new_size, BIGGEST_ALIGNMENT);
994 if (max_align > 0 && new_align > max_align)
995 new_align = max_align;
996 SET_TYPE_ALIGN (new_type, MIN (align, new_align));
842d4ee2
EB
997 }
998
999 TYPE_USER_ALIGN (new_type) = 1;
1000
1001 /* Now copy the fields, keeping the position and size as we don't want
1002 to change the layout by propagating the packedness downwards. */
b3f75672
EB
1003 tree new_field_list = NULL_TREE;
1004 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
842d4ee2 1005 {
b3f75672 1006 tree new_field_type = TREE_TYPE (field);
842d4ee2
EB
1007 tree new_field, new_size;
1008
1009 if (RECORD_OR_UNION_TYPE_P (new_field_type)
1010 && !TYPE_FAT_POINTER_P (new_field_type)
cc269bb6 1011 && tree_fits_uhwi_p (TYPE_SIZE (new_field_type)))
b3f75672 1012 new_field_type = make_packable_type (new_field_type, true, max_align);
842d4ee2
EB
1013
1014 /* However, for the last field in a not already packed record type
1015 that is of an aggregate type, we need to use the RM size in the
1016 packable version of the record type, see finish_record_type. */
b3f75672 1017 if (!DECL_CHAIN (field)
842d4ee2
EB
1018 && !TYPE_PACKED (type)
1019 && RECORD_OR_UNION_TYPE_P (new_field_type)
1020 && !TYPE_FAT_POINTER_P (new_field_type)
1021 && !TYPE_CONTAINS_TEMPLATE_P (new_field_type)
1022 && TYPE_ADA_SIZE (new_field_type))
1023 new_size = TYPE_ADA_SIZE (new_field_type);
1024 else
b3f75672 1025 new_size = DECL_SIZE (field);
842d4ee2
EB
1026
1027 new_field
b3f75672
EB
1028 = create_field_decl (DECL_NAME (field), new_field_type, new_type,
1029 new_size, bit_position (field),
842d4ee2 1030 TYPE_PACKED (type),
b3f75672 1031 !DECL_NONADDRESSABLE_P (field));
842d4ee2 1032
b3f75672
EB
1033 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (field);
1034 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
842d4ee2 1035 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
b3f75672 1036 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (field);
842d4ee2 1037
b3f75672
EB
1038 DECL_CHAIN (new_field) = new_field_list;
1039 new_field_list = new_field;
842d4ee2
EB
1040 }
1041
b3f75672 1042 finish_record_type (new_type, nreverse (new_field_list), 2, false);
842d4ee2 1043 relate_alias_sets (new_type, type, ALIAS_SET_COPY);
44e9e3ec
EB
1044 if (TYPE_STUB_DECL (type))
1045 SET_DECL_PARALLEL_TYPE (TYPE_STUB_DECL (new_type),
1046 DECL_PARALLEL_TYPE (TYPE_STUB_DECL (type)));
842d4ee2
EB
1047
1048 /* If this is a padding record, we never want to make the size smaller
1049 than what was specified. For QUAL_UNION_TYPE, also copy the size. */
1050 if (TYPE_IS_PADDING_P (type) || TREE_CODE (type) == QUAL_UNION_TYPE)
1051 {
1052 TYPE_SIZE (new_type) = TYPE_SIZE (type);
1053 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
1054 new_size = size;
1055 }
1056 else
1057 {
1058 TYPE_SIZE (new_type) = bitsize_int (new_size);
b3f75672 1059 TYPE_SIZE_UNIT (new_type) = size_int (new_size / BITS_PER_UNIT);
842d4ee2
EB
1060 }
1061
1062 if (!TYPE_CONTAINS_TEMPLATE_P (type))
1063 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (type));
1064
1065 compute_record_mode (new_type);
1066
1067 /* Try harder to get a packable type if necessary, for example
1068 in case the record itself contains a BLKmode field. */
1069 if (in_record && TYPE_MODE (new_type) == BLKmode)
1070 SET_TYPE_MODE (new_type,
1071 mode_for_size_tree (TYPE_SIZE (new_type), MODE_INT, 1));
1072
b3f75672
EB
1073 /* If neither mode nor size nor alignment shrunk, return the old type. */
1074 if (TYPE_MODE (new_type) == BLKmode && new_size >= size && max_align == 0)
842d4ee2
EB
1075 return type;
1076
1077 return new_type;
1078}
1079
1080/* Given a type TYPE, return a new type whose size is appropriate for SIZE.
1081 If TYPE is the best type, return it. Otherwise, make a new type. We
1082 only support new integral and pointer types. FOR_BIASED is true if
1083 we are making a biased type. */
1084
1085tree
1086make_type_from_size (tree type, tree size_tree, bool for_biased)
1087{
1088 unsigned HOST_WIDE_INT size;
1089 bool biased_p;
1090 tree new_type;
1091
1092 /* If size indicates an error, just return TYPE to avoid propagating
1093 the error. Likewise if it's too large to represent. */
cc269bb6 1094 if (!size_tree || !tree_fits_uhwi_p (size_tree))
842d4ee2
EB
1095 return type;
1096
ae7e9ddd 1097 size = tree_to_uhwi (size_tree);
842d4ee2
EB
1098
1099 switch (TREE_CODE (type))
1100 {
1101 case INTEGER_TYPE:
1102 case ENUMERAL_TYPE:
1103 case BOOLEAN_TYPE:
1104 biased_p = (TREE_CODE (type) == INTEGER_TYPE
1105 && TYPE_BIASED_REPRESENTATION_P (type));
1106
1107 /* Integer types with precision 0 are forbidden. */
1108 if (size == 0)
1109 size = 1;
1110
1111 /* Only do something if the type isn't a packed array type and doesn't
1112 already have the proper size and the size isn't too large. */
1113 if (TYPE_IS_PACKED_ARRAY_TYPE_P (type)
1114 || (TYPE_PRECISION (type) == size && biased_p == for_biased)
1115 || size > LONG_LONG_TYPE_SIZE)
1116 break;
1117
1118 biased_p |= for_biased;
1119 if (TYPE_UNSIGNED (type) || biased_p)
1120 new_type = make_unsigned_type (size);
1121 else
1122 new_type = make_signed_type (size);
1123 TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
1eb58520
AC
1124 SET_TYPE_RM_MIN_VALUE (new_type, TYPE_MIN_VALUE (type));
1125 SET_TYPE_RM_MAX_VALUE (new_type, TYPE_MAX_VALUE (type));
842d4ee2
EB
1126 /* Copy the name to show that it's essentially the same type and
1127 not a subrange type. */
1128 TYPE_NAME (new_type) = TYPE_NAME (type);
1129 TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
1130 SET_TYPE_RM_SIZE (new_type, bitsize_int (size));
1131 return new_type;
1132
1133 case RECORD_TYPE:
1134 /* Do something if this is a fat pointer, in which case we
1135 may need to return the thin pointer. */
1136 if (TYPE_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
1137 {
ef4bddc2 1138 machine_mode p_mode = mode_for_size (size, MODE_INT, 0);
842d4ee2
EB
1139 if (!targetm.valid_pointer_mode (p_mode))
1140 p_mode = ptr_mode;
1141 return
1142 build_pointer_type_for_mode
1143 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)),
1144 p_mode, 0);
1145 }
1146 break;
1147
1148 case POINTER_TYPE:
1149 /* Only do something if this is a thin pointer, in which case we
1150 may need to return the fat pointer. */
1151 if (TYPE_IS_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
1152 return
1153 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
1154 break;
1155
1156 default:
1157 break;
1158 }
1159
1160 return type;
1161}
1162
1163/* See if the data pointed to by the hash table slot is marked. */
1164
08ec2754
RS
1165int
1166pad_type_hasher::keep_cache_entry (pad_type_hash *&t)
842d4ee2 1167{
08ec2754 1168 return ggc_marked_p (t->type);
842d4ee2
EB
1169}
1170
d242408f 1171/* Return true iff the padded types are equivalent. */
842d4ee2 1172
d242408f
TS
1173bool
1174pad_type_hasher::equal (pad_type_hash *t1, pad_type_hash *t2)
842d4ee2 1175{
842d4ee2
EB
1176 tree type1, type2;
1177
1178 if (t1->hash != t2->hash)
1179 return 0;
1180
1181 type1 = t1->type;
1182 type2 = t2->type;
1183
ee45a32d
EB
1184 /* We consider that the padded types are equivalent if they pad the same type
1185 and have the same size, alignment, RM size and storage order. Taking the
1186 mode into account is redundant since it is determined by the others. */
842d4ee2
EB
1187 return
1188 TREE_TYPE (TYPE_FIELDS (type1)) == TREE_TYPE (TYPE_FIELDS (type2))
1189 && TYPE_SIZE (type1) == TYPE_SIZE (type2)
1190 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2)
ee45a32d
EB
1191 && TYPE_ADA_SIZE (type1) == TYPE_ADA_SIZE (type2)
1192 && TYPE_REVERSE_STORAGE_ORDER (type1) == TYPE_REVERSE_STORAGE_ORDER (type2);
842d4ee2
EB
1193}
1194
5cb7516d
EB
1195/* Look up the padded TYPE in the hash table and return its canonical version
1196 if it exists; otherwise, insert it into the hash table. */
1197
1198static tree
1199lookup_and_insert_pad_type (tree type)
1200{
1201 hashval_t hashcode;
1202 struct pad_type_hash in, *h;
5cb7516d
EB
1203
1204 hashcode
1205 = iterative_hash_object (TYPE_HASH (TREE_TYPE (TYPE_FIELDS (type))), 0);
1206 hashcode = iterative_hash_expr (TYPE_SIZE (type), hashcode);
1207 hashcode = iterative_hash_hashval_t (TYPE_ALIGN (type), hashcode);
1208 hashcode = iterative_hash_expr (TYPE_ADA_SIZE (type), hashcode);
1209
1210 in.hash = hashcode;
1211 in.type = type;
d242408f 1212 h = pad_type_hash_table->find_with_hash (&in, hashcode);
5cb7516d
EB
1213 if (h)
1214 return h->type;
1215
1216 h = ggc_alloc<pad_type_hash> ();
1217 h->hash = hashcode;
1218 h->type = type;
d242408f 1219 *pad_type_hash_table->find_slot_with_hash (h, hashcode, INSERT) = h;
5cb7516d
EB
1220 return NULL_TREE;
1221}
1222
842d4ee2 1223/* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
5cb7516d 1224 if needed. We have already verified that SIZE and ALIGN are large enough.
842d4ee2
EB
1225 GNAT_ENTITY is used to name the resulting record and to issue a warning.
1226 IS_COMPONENT_TYPE is true if this is being done for the component type of
1227 an array. IS_USER_TYPE is true if the original type needs to be completed.
1228 DEFINITION is true if this type is being defined. SET_RM_SIZE is true if
afc737f0
EB
1229 the RM size of the resulting type is to be set to SIZE too; in this case,
1230 the padded type is canonicalized before being returned. */
842d4ee2
EB
1231
1232tree
1233maybe_pad_type (tree type, tree size, unsigned int align,
1234 Entity_Id gnat_entity, bool is_component_type,
1235 bool is_user_type, bool definition, bool set_rm_size)
1236{
1237 tree orig_size = TYPE_SIZE (type);
44e9e3ec 1238 unsigned int orig_align = TYPE_ALIGN (type);
842d4ee2
EB
1239 tree record, field;
1240
1241 /* If TYPE is a padded type, see if it agrees with any size and alignment
1242 we were given. If so, return the original type. Otherwise, strip
1243 off the padding, since we will either be returning the inner type
1244 or repadding it. If no size or alignment is specified, use that of
1245 the original padded type. */
1246 if (TYPE_IS_PADDING_P (type))
1247 {
1248 if ((!size
44e9e3ec
EB
1249 || operand_equal_p (round_up (size, orig_align), orig_size, 0))
1250 && (align == 0 || align == orig_align))
842d4ee2
EB
1251 return type;
1252
1253 if (!size)
44e9e3ec 1254 size = orig_size;
842d4ee2 1255 if (align == 0)
44e9e3ec 1256 align = orig_align;
842d4ee2
EB
1257
1258 type = TREE_TYPE (TYPE_FIELDS (type));
1259 orig_size = TYPE_SIZE (type);
44e9e3ec 1260 orig_align = TYPE_ALIGN (type);
842d4ee2
EB
1261 }
1262
1263 /* If the size is either not being changed or is being made smaller (which
1264 is not done here and is only valid for bitfields anyway), show the size
1265 isn't changing. Likewise, clear the alignment if it isn't being
1266 changed. Then return if we aren't doing anything. */
1267 if (size
1268 && (operand_equal_p (size, orig_size, 0)
1269 || (TREE_CODE (orig_size) == INTEGER_CST
1270 && tree_int_cst_lt (size, orig_size))))
1271 size = NULL_TREE;
1272
44e9e3ec 1273 if (align == orig_align)
842d4ee2
EB
1274 align = 0;
1275
1276 if (align == 0 && !size)
1277 return type;
1278
1279 /* If requested, complete the original type and give it a name. */
1280 if (is_user_type)
1281 create_type_decl (get_entity_name (gnat_entity), type,
74746d49 1282 !Comes_From_Source (gnat_entity),
842d4ee2
EB
1283 !(TYPE_NAME (type)
1284 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1285 && DECL_IGNORED_P (TYPE_NAME (type))),
1286 gnat_entity);
1287
1288 /* We used to modify the record in place in some cases, but that could
1289 generate incorrect debugging information. So make a new record
1290 type and name. */
1291 record = make_node (RECORD_TYPE);
1292 TYPE_PADDING_P (record) = 1;
1293
7c775aca 1294 /* ??? Padding types around packed array implementation types will be
2d595887
PMR
1295 considered as root types in the array descriptor language hook (see
1296 gnat_get_array_descr_info). Give them the original packed array type
1297 name so that the one coming from sources appears in the debugging
1298 information. */
7c775aca
EB
1299 if (TYPE_IMPL_PACKED_ARRAY_P (type)
1300 && TYPE_ORIGINAL_PACKED_ARRAY (type)
1301 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1302 TYPE_NAME (record) = TYPE_NAME (TYPE_ORIGINAL_PACKED_ARRAY (type));
2d595887 1303 else if (Present (gnat_entity))
842d4ee2
EB
1304 TYPE_NAME (record) = create_concat_name (gnat_entity, "PAD");
1305
fe37c7af 1306 SET_TYPE_ALIGN (record, align ? align : orig_align);
842d4ee2
EB
1307 TYPE_SIZE (record) = size ? size : orig_size;
1308 TYPE_SIZE_UNIT (record)
1309 = convert (sizetype,
1310 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (record),
1311 bitsize_unit_node));
1312
1313 /* If we are changing the alignment and the input type is a record with
1314 BLKmode and a small constant size, try to make a form that has an
1315 integral mode. This might allow the padding record to also have an
1316 integral mode, which will be much more efficient. There is no point
1317 in doing so if a size is specified unless it is also a small constant
1318 size and it is incorrect to do so if we cannot guarantee that the mode
1319 will be naturally aligned since the field must always be addressable.
1320
1321 ??? This might not always be a win when done for a stand-alone object:
1322 since the nominal and the effective type of the object will now have
1323 different modes, a VIEW_CONVERT_EXPR will be required for converting
1324 between them and it might be hard to overcome afterwards, including
1325 at the RTL level when the stand-alone object is accessed as a whole. */
1326 if (align != 0
1327 && RECORD_OR_UNION_TYPE_P (type)
1328 && TYPE_MODE (type) == BLKmode
1329 && !TYPE_BY_REFERENCE_P (type)
1330 && TREE_CODE (orig_size) == INTEGER_CST
1331 && !TREE_OVERFLOW (orig_size)
1332 && compare_tree_int (orig_size, MAX_FIXED_MODE_SIZE) <= 0
1333 && (!size
1334 || (TREE_CODE (size) == INTEGER_CST
1335 && compare_tree_int (size, MAX_FIXED_MODE_SIZE) <= 0)))
1336 {
1337 tree packable_type = make_packable_type (type, true);
1338 if (TYPE_MODE (packable_type) != BLKmode
1339 && align >= TYPE_ALIGN (packable_type))
1340 type = packable_type;
1341 }
1342
1343 /* Now create the field with the original size. */
1366ba41
EB
1344 field = create_field_decl (get_identifier ("F"), type, record, orig_size,
1345 bitsize_zero_node, 0, 1);
842d4ee2
EB
1346 DECL_INTERNAL_P (field) = 1;
1347
afc737f0 1348 /* We will output additional debug info manually below. */
842d4ee2
EB
1349 finish_record_type (record, field, 1, false);
1350
afc737f0
EB
1351 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1352 SET_TYPE_DEBUG_TYPE (record, type);
1353
842d4ee2
EB
1354 /* Set the RM size if requested. */
1355 if (set_rm_size)
1356 {
5cb7516d
EB
1357 tree canonical_pad_type;
1358
842d4ee2
EB
1359 SET_TYPE_ADA_SIZE (record, size ? size : orig_size);
1360
1361 /* If the padded type is complete and has constant size, we canonicalize
1362 it by means of the hash table. This is consistent with the language
1363 semantics and ensures that gigi and the middle-end have a common view
1364 of these padded types. */
5cb7516d
EB
1365 if (TREE_CONSTANT (TYPE_SIZE (record))
1366 && (canonical_pad_type = lookup_and_insert_pad_type (record)))
842d4ee2 1367 {
5cb7516d
EB
1368 record = canonical_pad_type;
1369 goto built;
842d4ee2
EB
1370 }
1371 }
1372
1373 /* Unless debugging information isn't being written for the input type,
1374 write a record that shows what we are a subtype of and also make a
eb59e428
PMR
1375 variable that indicates our size, if still variable. */
1376 if (TREE_CODE (orig_size) != INTEGER_CST
842d4ee2
EB
1377 && TYPE_NAME (record)
1378 && TYPE_NAME (type)
1379 && !(TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1380 && DECL_IGNORED_P (TYPE_NAME (type))))
1381 {
9dba4b55 1382 tree name = TYPE_IDENTIFIER (record);
c1a569ef
EB
1383 tree size_unit = TYPE_SIZE_UNIT (record);
1384
1385 /* A variable that holds the size is required even with no encoding since
1386 it will be referenced by debugging information attributes. At global
1387 level, we need a single variable across all translation units. */
1388 if (size
1389 && TREE_CODE (size) != INTEGER_CST
1390 && (definition || global_bindings_p ()))
1391 {
eb59e428
PMR
1392 /* Whether or not gnat_entity comes from source, this XVZ variable is
1393 is a compilation artifact. */
c1a569ef
EB
1394 size_unit
1395 = create_var_decl (concat_name (name, "XVZ"), NULL_TREE, sizetype,
1396 size_unit, true, global_bindings_p (),
1397 !definition && global_bindings_p (), false,
2056c5ed 1398 false, true, true, NULL, gnat_entity);
c1a569ef
EB
1399 TYPE_SIZE_UNIT (record) = size_unit;
1400 }
1401
eb59e428
PMR
1402 /* There is no need to show what we are a subtype of when outputting as
1403 few encodings as possible: regular debugging infomation makes this
1404 redundant. */
1405 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
1406 {
1407 tree marker = make_node (RECORD_TYPE);
1408 tree orig_name = TYPE_IDENTIFIER (type);
1409
1410 TYPE_NAME (marker) = concat_name (name, "XVS");
1411 finish_record_type (marker,
1412 create_field_decl (orig_name,
1413 build_reference_type (type),
1414 marker, NULL_TREE, NULL_TREE,
1415 0, 0),
1416 0, true);
1417 TYPE_SIZE_UNIT (marker) = size_unit;
1418
1419 add_parallel_type (record, marker);
1420 }
842d4ee2
EB
1421 }
1422
842d4ee2 1423built:
80746f5d 1424 /* If a simple size was explicitly given, maybe issue a warning. */
842d4ee2
EB
1425 if (!size
1426 || TREE_CODE (size) == COND_EXPR
1427 || TREE_CODE (size) == MAX_EXPR
80746f5d 1428 || No (gnat_entity))
842d4ee2
EB
1429 return record;
1430
80746f5d
EB
1431 /* But don't do it if we are just annotating types and the type is tagged or
1432 concurrent, since these types aren't fully laid out in this mode. */
1433 if (type_annotate_only)
1434 {
1435 Entity_Id gnat_type
1436 = is_component_type
1437 ? Component_Type (gnat_entity) : Etype (gnat_entity);
1438
1439 if (Is_Tagged_Type (gnat_type) || Is_Concurrent_Type (gnat_type))
1440 return record;
1441 }
1442
1443 /* Take the original size as the maximum size of the input if there was an
1444 unconstrained record involved and round it up to the specified alignment,
1445 if one was specified, but only for aggregate types. */
842d4ee2
EB
1446 if (CONTAINS_PLACEHOLDER_P (orig_size))
1447 orig_size = max_size (orig_size, true);
1448
f42dd37f 1449 if (align && AGGREGATE_TYPE_P (type))
842d4ee2
EB
1450 orig_size = round_up (orig_size, align);
1451
1452 if (!operand_equal_p (size, orig_size, 0)
1453 && !(TREE_CODE (size) == INTEGER_CST
1454 && TREE_CODE (orig_size) == INTEGER_CST
1455 && (TREE_OVERFLOW (size)
1456 || TREE_OVERFLOW (orig_size)
1457 || tree_int_cst_lt (size, orig_size))))
1458 {
1459 Node_Id gnat_error_node = Empty;
1460
1a4cb227
AC
1461 /* For a packed array, post the message on the original array type. */
1462 if (Is_Packed_Array_Impl_Type (gnat_entity))
842d4ee2
EB
1463 gnat_entity = Original_Array_Type (gnat_entity);
1464
1465 if ((Ekind (gnat_entity) == E_Component
1466 || Ekind (gnat_entity) == E_Discriminant)
1467 && Present (Component_Clause (gnat_entity)))
1468 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
1469 else if (Present (Size_Clause (gnat_entity)))
1470 gnat_error_node = Expression (Size_Clause (gnat_entity));
1471
1472 /* Generate message only for entities that come from source, since
1473 if we have an entity created by expansion, the message will be
1474 generated for some other corresponding source entity. */
1475 if (Comes_From_Source (gnat_entity))
1476 {
1477 if (Present (gnat_error_node))
1478 post_error_ne_tree ("{^ }bits of & unused?",
1479 gnat_error_node, gnat_entity,
1480 size_diffop (size, orig_size));
1481 else if (is_component_type)
1482 post_error_ne_tree ("component of& padded{ by ^ bits}?",
1483 gnat_entity, gnat_entity,
1484 size_diffop (size, orig_size));
1485 }
1486 }
1487
1488 return record;
1489}
ee45a32d
EB
1490
1491/* Return a copy of the padded TYPE but with reverse storage order. */
1492
1493tree
1494set_reverse_storage_order_on_pad_type (tree type)
1495{
1496 tree field, canonical_pad_type;
1497
4232ebbb
ML
1498 if (flag_checking)
1499 {
1500 /* If the inner type is not scalar then the function does nothing. */
1501 tree inner_type = TREE_TYPE (TYPE_FIELDS (type));
1502 gcc_assert (!AGGREGATE_TYPE_P (inner_type)
1503 && !VECTOR_TYPE_P (inner_type));
1504 }
ee45a32d
EB
1505
1506 /* This is required for the canonicalization. */
1507 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
1508
1509 field = copy_node (TYPE_FIELDS (type));
1510 type = copy_type (type);
1511 DECL_CONTEXT (field) = type;
1512 TYPE_FIELDS (type) = field;
1513 TYPE_REVERSE_STORAGE_ORDER (type) = 1;
1514 canonical_pad_type = lookup_and_insert_pad_type (type);
1515 return canonical_pad_type ? canonical_pad_type : type;
1516}
842d4ee2
EB
1517\f
1518/* Relate the alias sets of GNU_NEW_TYPE and GNU_OLD_TYPE according to OP.
1519 If this is a multi-dimensional array type, do this recursively.
1520
1521 OP may be
1522 - ALIAS_SET_COPY: the new set is made a copy of the old one.
1523 - ALIAS_SET_SUPERSET: the new set is made a superset of the old one.
1524 - ALIAS_SET_SUBSET: the new set is made a subset of the old one. */
1525
1526void
1527relate_alias_sets (tree gnu_new_type, tree gnu_old_type, enum alias_set_op op)
1528{
1529 /* Remove any padding from GNU_OLD_TYPE. It doesn't matter in the case
1530 of a one-dimensional array, since the padding has the same alias set
1531 as the field type, but if it's a multi-dimensional array, we need to
1532 see the inner types. */
1533 while (TREE_CODE (gnu_old_type) == RECORD_TYPE
1534 && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
1535 || TYPE_PADDING_P (gnu_old_type)))
1536 gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
1537
1538 /* Unconstrained array types are deemed incomplete and would thus be given
1539 alias set 0. Retrieve the underlying array type. */
1540 if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
1541 gnu_old_type
1542 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
1543 if (TREE_CODE (gnu_new_type) == UNCONSTRAINED_ARRAY_TYPE)
1544 gnu_new_type
1545 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_new_type))));
1546
1547 if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
1548 && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
1549 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
1550 relate_alias_sets (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type), op);
1551
1552 switch (op)
1553 {
1554 case ALIAS_SET_COPY:
1555 /* The alias set shouldn't be copied between array types with different
1556 aliasing settings because this can break the aliasing relationship
1557 between the array type and its element type. */
9abe8b74 1558 if (flag_checking || flag_strict_aliasing)
842d4ee2
EB
1559 gcc_assert (!(TREE_CODE (gnu_new_type) == ARRAY_TYPE
1560 && TREE_CODE (gnu_old_type) == ARRAY_TYPE
1561 && TYPE_NONALIASED_COMPONENT (gnu_new_type)
1562 != TYPE_NONALIASED_COMPONENT (gnu_old_type)));
1563
1564 TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
1565 break;
1566
1567 case ALIAS_SET_SUBSET:
1568 case ALIAS_SET_SUPERSET:
1569 {
1570 alias_set_type old_set = get_alias_set (gnu_old_type);
1571 alias_set_type new_set = get_alias_set (gnu_new_type);
1572
1573 /* Do nothing if the alias sets conflict. This ensures that we
1574 never call record_alias_subset several times for the same pair
1575 or at all for alias set 0. */
1576 if (!alias_sets_conflict_p (old_set, new_set))
1577 {
1578 if (op == ALIAS_SET_SUBSET)
1579 record_alias_subset (old_set, new_set);
1580 else
1581 record_alias_subset (new_set, old_set);
1582 }
1583 }
1584 break;
1585
1586 default:
1587 gcc_unreachable ();
1588 }
1589
1590 record_component_aliases (gnu_new_type);
1591}
1592\f
1aeb40dd 1593/* Record TYPE as a builtin type for Ada. NAME is the name of the type.
c1a569ef 1594 ARTIFICIAL_P is true if the type was generated by the compiler. */
a1ab4c31
AC
1595
1596void
1aeb40dd 1597record_builtin_type (const char *name, tree type, bool artificial_p)
a1ab4c31 1598{
c172df28
AH
1599 tree type_decl = build_decl (input_location,
1600 TYPE_DECL, get_identifier (name), type);
1aeb40dd 1601 DECL_ARTIFICIAL (type_decl) = artificial_p;
bc712852 1602 TYPE_ARTIFICIAL (type) = artificial_p;
10069d53 1603 gnat_pushdecl (type_decl, Empty);
a1ab4c31 1604
10069d53
EB
1605 if (debug_hooks->type_decl)
1606 debug_hooks->type_decl (type_decl, false);
a1ab4c31
AC
1607}
1608\f
825da0d2
EB
1609/* Finish constructing the character type CHAR_TYPE.
1610
1611 In Ada character types are enumeration types and, as a consequence, are
1612 represented in the front-end by integral types holding the positions of
1613 the enumeration values as defined by the language, which means that the
1614 integral types are unsigned.
1615
1616 Unfortunately the signedness of 'char' in C is implementation-defined
1617 and GCC even has the option -fsigned-char to toggle it at run time.
1618 Since GNAT's philosophy is to be compatible with C by default, to wit
1619 Interfaces.C.char is defined as a mere copy of Character, we may need
1620 to declare character types as signed types in GENERIC and generate the
1621 necessary adjustments to make them behave as unsigned types.
1622
1623 The overall strategy is as follows: if 'char' is unsigned, do nothing;
1624 if 'char' is signed, translate character types of CHAR_TYPE_SIZE and
1625 character subtypes with RM_Size = Esize = CHAR_TYPE_SIZE into signed
1626 types. The idea is to ensure that the bit pattern contained in the
1627 Esize'd objects is not changed, even though the numerical value will
1628 be interpreted differently depending on the signedness.
1629
1630 For character types, the bounds are implicit and, therefore, need to
1631 be adjusted. Morever, the debug info needs the unsigned version. */
1632
1633void
1634finish_character_type (tree char_type)
1635{
1636 if (TYPE_UNSIGNED (char_type))
1637 return;
1638
7005800c
EB
1639 /* Make a copy of a generic unsigned version since we'll modify it. */
1640 tree unsigned_char_type
1641 = (char_type == char_type_node
1642 ? unsigned_char_type_node
1643 : copy_type (gnat_unsigned_type_for (char_type)));
825da0d2
EB
1644
1645 TYPE_NAME (unsigned_char_type) = TYPE_NAME (char_type);
1646 TYPE_STRING_FLAG (unsigned_char_type) = TYPE_STRING_FLAG (char_type);
1647 TYPE_ARTIFICIAL (unsigned_char_type) = TYPE_ARTIFICIAL (char_type);
1648
1649 SET_TYPE_DEBUG_TYPE (char_type, unsigned_char_type);
1650 SET_TYPE_RM_MIN_VALUE (char_type, TYPE_MIN_VALUE (unsigned_char_type));
1651 SET_TYPE_RM_MAX_VALUE (char_type, TYPE_MAX_VALUE (unsigned_char_type));
1652}
1653
e3edbd56
EB
1654/* Given a record type RECORD_TYPE and a list of FIELD_DECL nodes FIELD_LIST,
1655 finish constructing the record type as a fat pointer type. */
1656
1657void
1658finish_fat_pointer_type (tree record_type, tree field_list)
1659{
1660 /* Make sure we can put it into a register. */
5da8c011 1661 if (STRICT_ALIGNMENT)
fe37c7af 1662 SET_TYPE_ALIGN (record_type, MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE));
e3edbd56
EB
1663
1664 /* Show what it really is. */
1665 TYPE_FAT_POINTER_P (record_type) = 1;
1666
1667 /* Do not emit debug info for it since the types of its fields may still be
1668 incomplete at this point. */
1669 finish_record_type (record_type, field_list, 0, false);
1670
1671 /* Force type_contains_placeholder_p to return true on it. Although the
1672 PLACEHOLDER_EXPRs are referenced only indirectly, this isn't a pointer
1673 type but the representation of the unconstrained array. */
1674 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (record_type) = 2;
1675}
1676
032d1b71 1677/* Given a record type RECORD_TYPE and a list of FIELD_DECL nodes FIELD_LIST,
a1ab4c31
AC
1678 finish constructing the record or union type. If REP_LEVEL is zero, this
1679 record has no representation clause and so will be entirely laid out here.
1680 If REP_LEVEL is one, this record has a representation clause and has been
1681 laid out already; only set the sizes and alignment. If REP_LEVEL is two,
1682 this record is derived from a parent record and thus inherits its layout;
032d1b71 1683 only make a pass on the fields to finalize them. DEBUG_INFO_P is true if
afc737f0 1684 additional debug info needs to be output for this type. */
a1ab4c31
AC
1685
1686void
032d1b71
EB
1687finish_record_type (tree record_type, tree field_list, int rep_level,
1688 bool debug_info_p)
a1ab4c31
AC
1689{
1690 enum tree_code code = TREE_CODE (record_type);
9dba4b55 1691 tree name = TYPE_IDENTIFIER (record_type);
a1ab4c31
AC
1692 tree ada_size = bitsize_zero_node;
1693 tree size = bitsize_zero_node;
1694 bool had_size = TYPE_SIZE (record_type) != 0;
1695 bool had_size_unit = TYPE_SIZE_UNIT (record_type) != 0;
1696 bool had_align = TYPE_ALIGN (record_type) != 0;
1697 tree field;
1698
032d1b71 1699 TYPE_FIELDS (record_type) = field_list;
a1ab4c31 1700
10069d53
EB
1701 /* Always attach the TYPE_STUB_DECL for a record type. It is required to
1702 generate debug info and have a parallel type. */
10069d53 1703 TYPE_STUB_DECL (record_type) = create_type_stub_decl (name, record_type);
a1ab4c31
AC
1704
1705 /* Globally initialize the record first. If this is a rep'ed record,
1706 that just means some initializations; otherwise, layout the record. */
1707 if (rep_level > 0)
1708 {
fe37c7af
MM
1709 SET_TYPE_ALIGN (record_type, MAX (BITS_PER_UNIT,
1710 TYPE_ALIGN (record_type)));
a1ab4c31
AC
1711
1712 if (!had_size_unit)
1713 TYPE_SIZE_UNIT (record_type) = size_zero_node;
b1fa9126 1714
a1ab4c31
AC
1715 if (!had_size)
1716 TYPE_SIZE (record_type) = bitsize_zero_node;
1717
1718 /* For all-repped records with a size specified, lay the QUAL_UNION_TYPE
1719 out just like a UNION_TYPE, since the size will be fixed. */
1720 else if (code == QUAL_UNION_TYPE)
1721 code = UNION_TYPE;
1722 }
1723 else
1724 {
1725 /* Ensure there isn't a size already set. There can be in an error
1726 case where there is a rep clause but all fields have errors and
1727 no longer have a position. */
1728 TYPE_SIZE (record_type) = 0;
bb358f1c
EB
1729
1730 /* Ensure we use the traditional GCC layout for bitfields when we need
1731 to pack the record type or have a representation clause. The other
1732 possible layout (Microsoft C compiler), if available, would prevent
1733 efficient packing in almost all cases. */
1734#ifdef TARGET_MS_BITFIELD_LAYOUT
1735 if (TARGET_MS_BITFIELD_LAYOUT && TYPE_PACKED (record_type))
1736 decl_attributes (&record_type,
1737 tree_cons (get_identifier ("gcc_struct"),
1738 NULL_TREE, NULL_TREE),
1739 ATTR_FLAG_TYPE_IN_PLACE);
1740#endif
1741
a1ab4c31
AC
1742 layout_type (record_type);
1743 }
1744
1745 /* At this point, the position and size of each field is known. It was
1746 either set before entry by a rep clause, or by laying out the type above.
1747
1748 We now run a pass over the fields (in reverse order for QUAL_UNION_TYPEs)
1749 to compute the Ada size; the GCC size and alignment (for rep'ed records
1750 that are not padding types); and the mode (for rep'ed records). We also
1751 clear the DECL_BIT_FIELD indication for the cases we know have not been
1752 handled yet, and adjust DECL_NONADDRESSABLE_P accordingly. */
1753
1754 if (code == QUAL_UNION_TYPE)
032d1b71 1755 field_list = nreverse (field_list);
a1ab4c31 1756
910ad8de 1757 for (field = field_list; field; field = DECL_CHAIN (field))
a1ab4c31
AC
1758 {
1759 tree type = TREE_TYPE (field);
1760 tree pos = bit_position (field);
1761 tree this_size = DECL_SIZE (field);
1762 tree this_ada_size;
1763
e1e5852c 1764 if (RECORD_OR_UNION_TYPE_P (type)
315cff15 1765 && !TYPE_FAT_POINTER_P (type)
a1ab4c31
AC
1766 && !TYPE_CONTAINS_TEMPLATE_P (type)
1767 && TYPE_ADA_SIZE (type))
1768 this_ada_size = TYPE_ADA_SIZE (type);
1769 else
1770 this_ada_size = this_size;
1771
1772 /* Clear DECL_BIT_FIELD for the cases layout_decl does not handle. */
1773 if (DECL_BIT_FIELD (field)
1774 && operand_equal_p (this_size, TYPE_SIZE (type), 0))
1775 {
1776 unsigned int align = TYPE_ALIGN (type);
1777
1778 /* In the general case, type alignment is required. */
1779 if (value_factor_p (pos, align))
1780 {
1781 /* The enclosing record type must be sufficiently aligned.
1782 Otherwise, if no alignment was specified for it and it
1783 has been laid out already, bump its alignment to the
14ecca2e
EB
1784 desired one if this is compatible with its size and
1785 maximum alignment, if any. */
a1ab4c31
AC
1786 if (TYPE_ALIGN (record_type) >= align)
1787 {
fe37c7af 1788 SET_DECL_ALIGN (field, MAX (DECL_ALIGN (field), align));
a1ab4c31
AC
1789 DECL_BIT_FIELD (field) = 0;
1790 }
1791 else if (!had_align
1792 && rep_level == 0
14ecca2e
EB
1793 && value_factor_p (TYPE_SIZE (record_type), align)
1794 && (!TYPE_MAX_ALIGN (record_type)
1795 || TYPE_MAX_ALIGN (record_type) >= align))
a1ab4c31 1796 {
fe37c7af
MM
1797 SET_TYPE_ALIGN (record_type, align);
1798 SET_DECL_ALIGN (field, MAX (DECL_ALIGN (field), align));
a1ab4c31
AC
1799 DECL_BIT_FIELD (field) = 0;
1800 }
1801 }
1802
1803 /* In the non-strict alignment case, only byte alignment is. */
1804 if (!STRICT_ALIGNMENT
1805 && DECL_BIT_FIELD (field)
1806 && value_factor_p (pos, BITS_PER_UNIT))
1807 DECL_BIT_FIELD (field) = 0;
1808 }
1809
c1abd261
EB
1810 /* If we still have DECL_BIT_FIELD set at this point, we know that the
1811 field is technically not addressable. Except that it can actually
1812 be addressed if it is BLKmode and happens to be properly aligned. */
1813 if (DECL_BIT_FIELD (field)
1814 && !(DECL_MODE (field) == BLKmode
1815 && value_factor_p (pos, BITS_PER_UNIT)))
1816 DECL_NONADDRESSABLE_P (field) = 1;
a1ab4c31
AC
1817
1818 /* A type must be as aligned as its most aligned field that is not
1819 a bit-field. But this is already enforced by layout_type. */
1820 if (rep_level > 0 && !DECL_BIT_FIELD (field))
fe37c7af
MM
1821 SET_TYPE_ALIGN (record_type,
1822 MAX (TYPE_ALIGN (record_type), DECL_ALIGN (field)));
a1ab4c31
AC
1823
1824 switch (code)
1825 {
1826 case UNION_TYPE:
1827 ada_size = size_binop (MAX_EXPR, ada_size, this_ada_size);
1828 size = size_binop (MAX_EXPR, size, this_size);
1829 break;
1830
1831 case QUAL_UNION_TYPE:
1832 ada_size
1833 = fold_build3 (COND_EXPR, bitsizetype, DECL_QUALIFIER (field),
1834 this_ada_size, ada_size);
1835 size = fold_build3 (COND_EXPR, bitsizetype, DECL_QUALIFIER (field),
1836 this_size, size);
1837 break;
1838
1839 case RECORD_TYPE:
1840 /* Since we know here that all fields are sorted in order of
1841 increasing bit position, the size of the record is one
1842 higher than the ending bit of the last field processed
1843 unless we have a rep clause, since in that case we might
1844 have a field outside a QUAL_UNION_TYPE that has a higher ending
1845 position. So use a MAX in that case. Also, if this field is a
1846 QUAL_UNION_TYPE, we need to take into account the previous size in
1847 the case of empty variants. */
1848 ada_size
1849 = merge_sizes (ada_size, pos, this_ada_size,
1850 TREE_CODE (type) == QUAL_UNION_TYPE, rep_level > 0);
1851 size
1852 = merge_sizes (size, pos, this_size,
1853 TREE_CODE (type) == QUAL_UNION_TYPE, rep_level > 0);
1854 break;
1855
1856 default:
1857 gcc_unreachable ();
1858 }
1859 }
1860
1861 if (code == QUAL_UNION_TYPE)
032d1b71 1862 nreverse (field_list);
a1ab4c31
AC
1863
1864 if (rep_level < 2)
1865 {
1866 /* If this is a padding record, we never want to make the size smaller
1867 than what was specified in it, if any. */
315cff15 1868 if (TYPE_IS_PADDING_P (record_type) && TYPE_SIZE (record_type))
a1ab4c31
AC
1869 size = TYPE_SIZE (record_type);
1870
1871 /* Now set any of the values we've just computed that apply. */
315cff15 1872 if (!TYPE_FAT_POINTER_P (record_type)
a1ab4c31
AC
1873 && !TYPE_CONTAINS_TEMPLATE_P (record_type))
1874 SET_TYPE_ADA_SIZE (record_type, ada_size);
1875
1876 if (rep_level > 0)
1877 {
1878 tree size_unit = had_size_unit
1879 ? TYPE_SIZE_UNIT (record_type)
1880 : convert (sizetype,
1881 size_binop (CEIL_DIV_EXPR, size,
1882 bitsize_unit_node));
1883 unsigned int align = TYPE_ALIGN (record_type);
1884
1885 TYPE_SIZE (record_type) = variable_size (round_up (size, align));
1886 TYPE_SIZE_UNIT (record_type)
1887 = variable_size (round_up (size_unit, align / BITS_PER_UNIT));
1888
1889 compute_record_mode (record_type);
1890 }
1891 }
1892
14ecca2e
EB
1893 /* Reset the TYPE_MAX_ALIGN field since it's private to gigi. */
1894 TYPE_MAX_ALIGN (record_type) = 0;
1895
032d1b71 1896 if (debug_info_p)
a1ab4c31
AC
1897 rest_of_record_type_compilation (record_type);
1898}
1899
24d4b3d5
AC
1900/* Append PARALLEL_TYPE on the chain of parallel types of TYPE. If
1901 PARRALEL_TYPE has no context and its computation is not deferred yet, also
1902 propagate TYPE's context to PARALLEL_TYPE's or defer its propagation to the
1903 moment TYPE will get a context. */
a5695aa2
EB
1904
1905void
1906add_parallel_type (tree type, tree parallel_type)
1907{
1908 tree decl = TYPE_STUB_DECL (type);
1909
1910 while (DECL_PARALLEL_TYPE (decl))
1911 decl = TYPE_STUB_DECL (DECL_PARALLEL_TYPE (decl));
1912
1913 SET_DECL_PARALLEL_TYPE (decl, parallel_type);
24d4b3d5
AC
1914
1915 /* If PARALLEL_TYPE already has a context, we are done. */
7c775aca 1916 if (TYPE_CONTEXT (parallel_type))
24d4b3d5
AC
1917 return;
1918
7c775aca
EB
1919 /* Otherwise, try to get one from TYPE's context. If so, simply propagate
1920 it to PARALLEL_TYPE. */
1921 if (TYPE_CONTEXT (type))
24d4b3d5
AC
1922 gnat_set_type_context (parallel_type, TYPE_CONTEXT (type));
1923
7c775aca
EB
1924 /* Otherwise TYPE has not context yet. We know it will have one thanks to
1925 gnat_pushdecl and then its context will be propagated to PARALLEL_TYPE,
1926 so we have nothing to do in this case. */
a5695aa2
EB
1927}
1928
1929/* Return true if TYPE has a parallel type. */
1930
1931static bool
1932has_parallel_type (tree type)
1933{
1934 tree decl = TYPE_STUB_DECL (type);
1935
1936 return DECL_PARALLEL_TYPE (decl) != NULL_TREE;
1937}
1938
afc737f0
EB
1939/* Wrap up compilation of RECORD_TYPE, i.e. output additional debug info
1940 associated with it. It need not be invoked directly in most cases as
1941 finish_record_type takes care of doing so. */
a1ab4c31
AC
1942
1943void
1944rest_of_record_type_compilation (tree record_type)
1945{
a1ab4c31 1946 bool var_size = false;
fb88e1dd 1947 tree field;
a1ab4c31 1948
fb88e1dd
EB
1949 /* If this is a padded type, the bulk of the debug info has already been
1950 generated for the field's type. */
1951 if (TYPE_IS_PADDING_P (record_type))
1952 return;
1953
a5695aa2
EB
1954 /* If the type already has a parallel type (XVS type), then we're done. */
1955 if (has_parallel_type (record_type))
1956 return;
1957
fb88e1dd 1958 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
a1ab4c31
AC
1959 {
1960 /* We need to make an XVE/XVU record if any field has variable size,
1961 whether or not the record does. For example, if we have a union,
1962 it may be that all fields, rounded up to the alignment, have the
1963 same size, in which case we'll use that size. But the debug
1964 output routines (except Dwarf2) won't be able to output the fields,
1965 so we need to make the special record. */
1966 if (TREE_CODE (DECL_SIZE (field)) != INTEGER_CST
1967 /* If a field has a non-constant qualifier, the record will have
1968 variable size too. */
fb88e1dd 1969 || (TREE_CODE (record_type) == QUAL_UNION_TYPE
a1ab4c31
AC
1970 && TREE_CODE (DECL_QUALIFIER (field)) != INTEGER_CST))
1971 {
1972 var_size = true;
1973 break;
1974 }
1975 }
1976
fb88e1dd
EB
1977 /* If this record type is of variable size, make a parallel record type that
1978 will tell the debugger how the former is laid out (see exp_dbug.ads). */
986ccd21 1979 if (var_size && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
a1ab4c31
AC
1980 {
1981 tree new_record_type
1982 = make_node (TREE_CODE (record_type) == QUAL_UNION_TYPE
1983 ? UNION_TYPE : TREE_CODE (record_type));
9dba4b55 1984 tree orig_name = TYPE_IDENTIFIER (record_type), new_name;
a1ab4c31 1985 tree last_pos = bitsize_zero_node;
0fb2335d 1986 tree old_field, prev_old_field = NULL_TREE;
a1ab4c31 1987
0fb2335d
EB
1988 new_name
1989 = concat_name (orig_name, TREE_CODE (record_type) == QUAL_UNION_TYPE
1990 ? "XVU" : "XVE");
1991 TYPE_NAME (new_record_type) = new_name;
fe37c7af 1992 SET_TYPE_ALIGN (new_record_type, BIGGEST_ALIGNMENT);
a1ab4c31 1993 TYPE_STUB_DECL (new_record_type)
0fb2335d 1994 = create_type_stub_decl (new_name, new_record_type);
a1ab4c31
AC
1995 DECL_IGNORED_P (TYPE_STUB_DECL (new_record_type))
1996 = DECL_IGNORED_P (TYPE_STUB_DECL (record_type));
396a2ee2 1997 gnat_pushdecl (TYPE_STUB_DECL (new_record_type), Empty);
a1ab4c31
AC
1998 TYPE_SIZE (new_record_type) = size_int (TYPE_ALIGN (record_type));
1999 TYPE_SIZE_UNIT (new_record_type)
2000 = size_int (TYPE_ALIGN (record_type) / BITS_PER_UNIT);
2001
5c475ba9
EB
2002 /* Now scan all the fields, replacing each field with a new field
2003 corresponding to the new encoding. */
a1ab4c31 2004 for (old_field = TYPE_FIELDS (record_type); old_field;
910ad8de 2005 old_field = DECL_CHAIN (old_field))
a1ab4c31
AC
2006 {
2007 tree field_type = TREE_TYPE (old_field);
2008 tree field_name = DECL_NAME (old_field);
a1ab4c31 2009 tree curpos = bit_position (old_field);
5c475ba9 2010 tree pos, new_field;
a1ab4c31
AC
2011 bool var = false;
2012 unsigned int align = 0;
a1ab4c31 2013
5c475ba9
EB
2014 /* We're going to do some pattern matching below so remove as many
2015 conversions as possible. */
2016 curpos = remove_conversions (curpos, true);
a1ab4c31 2017
5c475ba9 2018 /* See how the position was modified from the last position.
a1ab4c31 2019
5c475ba9
EB
2020 There are two basic cases we support: a value was added
2021 to the last position or the last position was rounded to
2022 a boundary and they something was added. Check for the
2023 first case first. If not, see if there is any evidence
2024 of rounding. If so, round the last position and retry.
a1ab4c31 2025
5c475ba9 2026 If this is a union, the position can be taken as zero. */
a1ab4c31 2027 if (TREE_CODE (new_record_type) == UNION_TYPE)
5c475ba9 2028 pos = bitsize_zero_node;
a1ab4c31
AC
2029 else
2030 pos = compute_related_constant (curpos, last_pos);
2031
5c475ba9
EB
2032 if (!pos
2033 && TREE_CODE (curpos) == MULT_EXPR
cc269bb6 2034 && tree_fits_uhwi_p (TREE_OPERAND (curpos, 1)))
a1ab4c31
AC
2035 {
2036 tree offset = TREE_OPERAND (curpos, 0);
ae7e9ddd 2037 align = tree_to_uhwi (TREE_OPERAND (curpos, 1));
5c475ba9
EB
2038 align = scale_by_factor_of (offset, align);
2039 last_pos = round_up (last_pos, align);
2040 pos = compute_related_constant (curpos, last_pos);
a1ab4c31 2041 }
5c475ba9
EB
2042 else if (!pos
2043 && TREE_CODE (curpos) == PLUS_EXPR
cc269bb6 2044 && tree_fits_uhwi_p (TREE_OPERAND (curpos, 1))
a1ab4c31 2045 && TREE_CODE (TREE_OPERAND (curpos, 0)) == MULT_EXPR
5a36c51b
RS
2046 && tree_fits_uhwi_p
2047 (TREE_OPERAND (TREE_OPERAND (curpos, 0), 1)))
a1ab4c31 2048 {
5c475ba9
EB
2049 tree offset = TREE_OPERAND (TREE_OPERAND (curpos, 0), 0);
2050 unsigned HOST_WIDE_INT addend
ae7e9ddd 2051 = tree_to_uhwi (TREE_OPERAND (curpos, 1));
a1ab4c31 2052 align
ae7e9ddd 2053 = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (curpos, 0), 1));
5c475ba9
EB
2054 align = scale_by_factor_of (offset, align);
2055 align = MIN (align, addend & -addend);
2056 last_pos = round_up (last_pos, align);
2057 pos = compute_related_constant (curpos, last_pos);
a1ab4c31 2058 }
5c475ba9 2059 else if (potential_alignment_gap (prev_old_field, old_field, pos))
a1ab4c31
AC
2060 {
2061 align = TYPE_ALIGN (field_type);
5c475ba9
EB
2062 last_pos = round_up (last_pos, align);
2063 pos = compute_related_constant (curpos, last_pos);
a1ab4c31
AC
2064 }
2065
2066 /* If we can't compute a position, set it to zero.
2067
5c475ba9
EB
2068 ??? We really should abort here, but it's too much work
2069 to get this correct for all cases. */
a1ab4c31
AC
2070 if (!pos)
2071 pos = bitsize_zero_node;
2072
2073 /* See if this type is variable-sized and make a pointer type
2074 and indicate the indirection if so. Beware that the debug
2075 back-end may adjust the position computed above according
2076 to the alignment of the field type, i.e. the pointer type
2077 in this case, if we don't preventively counter that. */
2078 if (TREE_CODE (DECL_SIZE (old_field)) != INTEGER_CST)
2079 {
2080 field_type = build_pointer_type (field_type);
2081 if (align != 0 && TYPE_ALIGN (field_type) > align)
2082 {
afc737f0 2083 field_type = copy_type (field_type);
fe37c7af 2084 SET_TYPE_ALIGN (field_type, align);
a1ab4c31
AC
2085 }
2086 var = true;
2087 }
2088
2089 /* Make a new field name, if necessary. */
2090 if (var || align != 0)
2091 {
2092 char suffix[16];
2093
2094 if (align != 0)
2095 sprintf (suffix, "XV%c%u", var ? 'L' : 'A',
2096 align / BITS_PER_UNIT);
2097 else
2098 strcpy (suffix, "XVL");
2099
0fb2335d 2100 field_name = concat_name (field_name, suffix);
a1ab4c31
AC
2101 }
2102
da01bfee
EB
2103 new_field
2104 = create_field_decl (field_name, field_type, new_record_type,
2105 DECL_SIZE (old_field), pos, 0, 0);
910ad8de 2106 DECL_CHAIN (new_field) = TYPE_FIELDS (new_record_type);
a1ab4c31
AC
2107 TYPE_FIELDS (new_record_type) = new_field;
2108
2109 /* If old_field is a QUAL_UNION_TYPE, take its size as being
2110 zero. The only time it's not the last field of the record
2111 is when there are other components at fixed positions after
2112 it (meaning there was a rep clause for every field) and we
2113 want to be able to encode them. */
2114 last_pos = size_binop (PLUS_EXPR, bit_position (old_field),
2115 (TREE_CODE (TREE_TYPE (old_field))
2116 == QUAL_UNION_TYPE)
2117 ? bitsize_zero_node
2118 : DECL_SIZE (old_field));
2119 prev_old_field = old_field;
2120 }
2121
fb88e1dd 2122 TYPE_FIELDS (new_record_type) = nreverse (TYPE_FIELDS (new_record_type));
a1ab4c31 2123
a5695aa2 2124 add_parallel_type (record_type, new_record_type);
a1ab4c31 2125 }
a1ab4c31
AC
2126}
2127
a1ab4c31 2128/* Utility function of above to merge LAST_SIZE, the previous size of a record
1e17ef87
EB
2129 with FIRST_BIT and SIZE that describe a field. SPECIAL is true if this
2130 represents a QUAL_UNION_TYPE in which case we must look for COND_EXPRs and
2131 replace a value of zero with the old size. If HAS_REP is true, we take the
2132 MAX of the end position of this field with LAST_SIZE. In all other cases,
2133 we use FIRST_BIT plus SIZE. Return an expression for the size. */
a1ab4c31
AC
2134
2135static tree
2136merge_sizes (tree last_size, tree first_bit, tree size, bool special,
2137 bool has_rep)
2138{
2139 tree type = TREE_TYPE (last_size);
c6bd4220 2140 tree new_size;
a1ab4c31
AC
2141
2142 if (!special || TREE_CODE (size) != COND_EXPR)
2143 {
c6bd4220 2144 new_size = size_binop (PLUS_EXPR, first_bit, size);
a1ab4c31 2145 if (has_rep)
c6bd4220 2146 new_size = size_binop (MAX_EXPR, last_size, new_size);
a1ab4c31
AC
2147 }
2148
2149 else
c6bd4220
EB
2150 new_size = fold_build3 (COND_EXPR, type, TREE_OPERAND (size, 0),
2151 integer_zerop (TREE_OPERAND (size, 1))
2152 ? last_size : merge_sizes (last_size, first_bit,
2153 TREE_OPERAND (size, 1),
2154 1, has_rep),
2155 integer_zerop (TREE_OPERAND (size, 2))
2156 ? last_size : merge_sizes (last_size, first_bit,
2157 TREE_OPERAND (size, 2),
2158 1, has_rep));
a1ab4c31
AC
2159
2160 /* We don't need any NON_VALUE_EXPRs and they can confuse us (especially
2161 when fed through substitute_in_expr) into thinking that a constant
2162 size is not constant. */
c6bd4220
EB
2163 while (TREE_CODE (new_size) == NON_LVALUE_EXPR)
2164 new_size = TREE_OPERAND (new_size, 0);
a1ab4c31 2165
c6bd4220 2166 return new_size;
a1ab4c31
AC
2167}
2168
2169/* Utility function of above to see if OP0 and OP1, both of SIZETYPE, are
2170 related by the addition of a constant. Return that constant if so. */
2171
2172static tree
2173compute_related_constant (tree op0, tree op1)
2174{
2175 tree op0_var, op1_var;
2176 tree op0_con = split_plus (op0, &op0_var);
2177 tree op1_con = split_plus (op1, &op1_var);
2178 tree result = size_binop (MINUS_EXPR, op0_con, op1_con);
2179
2180 if (operand_equal_p (op0_var, op1_var, 0))
2181 return result;
2182 else if (operand_equal_p (op0, size_binop (PLUS_EXPR, op1_var, result), 0))
2183 return result;
2184 else
2185 return 0;
2186}
2187
2188/* Utility function of above to split a tree OP which may be a sum, into a
2189 constant part, which is returned, and a variable part, which is stored
2190 in *PVAR. *PVAR may be bitsize_zero_node. All operations must be of
2191 bitsizetype. */
2192
2193static tree
2194split_plus (tree in, tree *pvar)
2195{
722356ce
EB
2196 /* Strip conversions in order to ease the tree traversal and maximize the
2197 potential for constant or plus/minus discovery. We need to be careful
a1ab4c31
AC
2198 to always return and set *pvar to bitsizetype trees, but it's worth
2199 the effort. */
722356ce 2200 in = remove_conversions (in, false);
a1ab4c31
AC
2201
2202 *pvar = convert (bitsizetype, in);
2203
2204 if (TREE_CODE (in) == INTEGER_CST)
2205 {
2206 *pvar = bitsize_zero_node;
2207 return convert (bitsizetype, in);
2208 }
2209 else if (TREE_CODE (in) == PLUS_EXPR || TREE_CODE (in) == MINUS_EXPR)
2210 {
2211 tree lhs_var, rhs_var;
2212 tree lhs_con = split_plus (TREE_OPERAND (in, 0), &lhs_var);
2213 tree rhs_con = split_plus (TREE_OPERAND (in, 1), &rhs_var);
2214
2215 if (lhs_var == TREE_OPERAND (in, 0)
2216 && rhs_var == TREE_OPERAND (in, 1))
2217 return bitsize_zero_node;
2218
2219 *pvar = size_binop (TREE_CODE (in), lhs_var, rhs_var);
2220 return size_binop (TREE_CODE (in), lhs_con, rhs_con);
2221 }
2222 else
2223 return bitsize_zero_node;
2224}
2225\f
a1ab4c31
AC
2226/* Return a copy of TYPE but safe to modify in any way. */
2227
2228tree
2229copy_type (tree type)
2230{
c6bd4220 2231 tree new_type = copy_node (type);
a1ab4c31 2232
90dcfecb
EB
2233 /* Unshare the language-specific data. */
2234 if (TYPE_LANG_SPECIFIC (type))
2235 {
2236 TYPE_LANG_SPECIFIC (new_type) = NULL;
2237 SET_TYPE_LANG_SPECIFIC (new_type, GET_TYPE_LANG_SPECIFIC (type));
2238 }
2239
2240 /* And the contents of the language-specific slot if needed. */
2241 if ((INTEGRAL_TYPE_P (type) || TREE_CODE (type) == REAL_TYPE)
2242 && TYPE_RM_VALUES (type))
2243 {
2244 TYPE_RM_VALUES (new_type) = NULL_TREE;
2245 SET_TYPE_RM_SIZE (new_type, TYPE_RM_SIZE (type));
2246 SET_TYPE_RM_MIN_VALUE (new_type, TYPE_RM_MIN_VALUE (type));
2247 SET_TYPE_RM_MAX_VALUE (new_type, TYPE_RM_MAX_VALUE (type));
2248 }
2249
a1ab4c31
AC
2250 /* copy_node clears this field instead of copying it, because it is
2251 aliased with TREE_CHAIN. */
c6bd4220 2252 TYPE_STUB_DECL (new_type) = TYPE_STUB_DECL (type);
a1ab4c31 2253
afc737f0
EB
2254 TYPE_POINTER_TO (new_type) = NULL_TREE;
2255 TYPE_REFERENCE_TO (new_type) = NULL_TREE;
c6bd4220 2256 TYPE_MAIN_VARIANT (new_type) = new_type;
afc737f0 2257 TYPE_NEXT_VARIANT (new_type) = NULL_TREE;
4b7bd260 2258 TYPE_CANONICAL (new_type) = new_type;
a1ab4c31 2259
c6bd4220 2260 return new_type;
a1ab4c31
AC
2261}
2262\f
c1abd261
EB
2263/* Return a subtype of sizetype with range MIN to MAX and whose
2264 TYPE_INDEX_TYPE is INDEX. GNAT_NODE is used for the position
2265 of the associated TYPE_DECL. */
a1ab4c31
AC
2266
2267tree
2268create_index_type (tree min, tree max, tree index, Node_Id gnat_node)
2269{
2270 /* First build a type for the desired range. */
523e82a7 2271 tree type = build_nonshared_range_type (sizetype, min, max);
a1ab4c31 2272
523e82a7 2273 /* Then set the index type. */
a1ab4c31 2274 SET_TYPE_INDEX_TYPE (type, index);
74746d49 2275 create_type_decl (NULL_TREE, type, true, false, gnat_node);
c1abd261 2276
a1ab4c31
AC
2277 return type;
2278}
84fb43a1
EB
2279
2280/* Return a subtype of TYPE with range MIN to MAX. If TYPE is NULL,
2281 sizetype is used. */
2282
2283tree
2284create_range_type (tree type, tree min, tree max)
2285{
2286 tree range_type;
2287
7c775aca 2288 if (!type)
84fb43a1
EB
2289 type = sizetype;
2290
2291 /* First build a type with the base range. */
523e82a7
EB
2292 range_type = build_nonshared_range_type (type, TYPE_MIN_VALUE (type),
2293 TYPE_MAX_VALUE (type));
84fb43a1
EB
2294
2295 /* Then set the actual range. */
1eb58520
AC
2296 SET_TYPE_RM_MIN_VALUE (range_type, min);
2297 SET_TYPE_RM_MAX_VALUE (range_type, max);
84fb43a1
EB
2298
2299 return range_type;
2300}
a1ab4c31 2301\f
6249559b
EB
2302/* Return a TYPE_DECL node suitable for the TYPE_STUB_DECL field of TYPE.
2303 NAME gives the name of the type to be used in the declaration. */
10069d53
EB
2304
2305tree
6249559b 2306create_type_stub_decl (tree name, tree type)
10069d53 2307{
6249559b 2308 tree type_decl = build_decl (input_location, TYPE_DECL, name, type);
10069d53 2309 DECL_ARTIFICIAL (type_decl) = 1;
bc712852 2310 TYPE_ARTIFICIAL (type) = 1;
10069d53
EB
2311 return type_decl;
2312}
2313
6249559b
EB
2314/* Return a TYPE_DECL node for TYPE. NAME gives the name of the type to be
2315 used in the declaration. ARTIFICIAL_P is true if the declaration was
2316 generated by the compiler. DEBUG_INFO_P is true if we need to write
2317 debug information about this type. GNAT_NODE is used for the position
2318 of the decl. */
a1ab4c31
AC
2319
2320tree
6249559b
EB
2321create_type_decl (tree name, tree type, bool artificial_p, bool debug_info_p,
2322 Node_Id gnat_node)
a1ab4c31 2323{
a1ab4c31 2324 enum tree_code code = TREE_CODE (type);
6249559b
EB
2325 bool is_named
2326 = TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL;
10069d53 2327 tree type_decl;
a1ab4c31 2328
10069d53
EB
2329 /* Only the builtin TYPE_STUB_DECL should be used for dummy types. */
2330 gcc_assert (!TYPE_IS_DUMMY_P (type));
a1ab4c31 2331
10069d53
EB
2332 /* If the type hasn't been named yet, we're naming it; preserve an existing
2333 TYPE_STUB_DECL that has been attached to it for some purpose. */
6249559b 2334 if (!is_named && TYPE_STUB_DECL (type))
10069d53
EB
2335 {
2336 type_decl = TYPE_STUB_DECL (type);
6249559b 2337 DECL_NAME (type_decl) = name;
10069d53
EB
2338 }
2339 else
6249559b 2340 type_decl = build_decl (input_location, TYPE_DECL, name, type);
a1ab4c31 2341
10069d53 2342 DECL_ARTIFICIAL (type_decl) = artificial_p;
bc712852 2343 TYPE_ARTIFICIAL (type) = artificial_p;
58c8f770
EB
2344
2345 /* Add this decl to the current binding level. */
10069d53 2346 gnat_pushdecl (type_decl, gnat_node);
58c8f770 2347
aef308d0
PMR
2348 /* If we're naming the type, equate the TYPE_STUB_DECL to the name. This
2349 causes the name to be also viewed as a "tag" by the debug back-end, with
2350 the advantage that no DW_TAG_typedef is emitted for artificial "tagged"
2351 types in DWARF.
2352
2353 Note that if "type" is used as a DECL_ORIGINAL_TYPE, it may be referenced
2354 from multiple contexts, and "type_decl" references a copy of it: in such a
2355 case, do not mess TYPE_STUB_DECL: we do not want to re-use the TYPE_DECL
2356 with the mechanism above. */
6249559b 2357 if (!is_named && type != DECL_ORIGINAL_TYPE (type_decl))
10069d53
EB
2358 TYPE_STUB_DECL (type) = type_decl;
2359
50741117
EB
2360 /* Do not generate debug info for UNCONSTRAINED_ARRAY_TYPE that the
2361 back-end doesn't support, and for others if we don't need to. */
a1ab4c31
AC
2362 if (code == UNCONSTRAINED_ARRAY_TYPE || !debug_info_p)
2363 DECL_IGNORED_P (type_decl) = 1;
a1ab4c31
AC
2364
2365 return type_decl;
2366}
10069d53 2367\f
a1ab4c31
AC
2368/* Return a VAR_DECL or CONST_DECL node.
2369
6249559b
EB
2370 NAME gives the name of the variable. ASM_NAME is its assembler name
2371 (if provided). TYPE is its data type (a GCC ..._TYPE node). INIT is
a1ab4c31
AC
2372 the GCC tree for an optional initial expression; NULL_TREE if none.
2373
2374 CONST_FLAG is true if this variable is constant, in which case we might
2375 return a CONST_DECL node unless CONST_DECL_ALLOWED_P is false.
2376
2377 PUBLIC_FLAG is true if this is for a reference to a public entity or for a
2378 definition to be made visible outside of the current compilation unit, for
2379 instance variable definitions in a package specification.
2380
1e17ef87 2381 EXTERN_FLAG is true when processing an external variable declaration (as
a1ab4c31
AC
2382 opposed to a definition: no storage is to be allocated for the variable).
2383
2056c5ed
EB
2384 STATIC_FLAG is only relevant when not at top level and indicates whether
2385 to always allocate storage to the variable.
2386
2387 VOLATILE_FLAG is true if this variable is declared as volatile.
a1ab4c31 2388
c1a569ef
EB
2389 ARTIFICIAL_P is true if the variable was generated by the compiler.
2390
2391 DEBUG_INFO_P is true if we need to write debug information for it.
2392
2ade427a
EB
2393 ATTR_LIST is the list of attributes to be attached to the variable.
2394
a1ab4c31
AC
2395 GNAT_NODE is used for the position of the decl. */
2396
2397tree
6249559b
EB
2398create_var_decl (tree name, tree asm_name, tree type, tree init,
2399 bool const_flag, bool public_flag, bool extern_flag,
2056c5ed
EB
2400 bool static_flag, bool volatile_flag, bool artificial_p,
2401 bool debug_info_p, struct attrib *attr_list,
2402 Node_Id gnat_node, bool const_decl_allowed_p)
a1ab4c31 2403{
5fe48b3d
EB
2404 /* Whether the object has static storage duration, either explicitly or by
2405 virtue of being declared at the global level. */
2406 const bool static_storage = static_flag || global_bindings_p ();
2407
2408 /* Whether the initializer is constant: for an external object or an object
2409 with static storage duration, we check that the initializer is a valid
2410 constant expression for initializing a static variable; otherwise, we
2411 only check that it is constant. */
2412 const bool init_const
6249559b
EB
2413 = (init
2414 && gnat_types_compatible_p (type, TREE_TYPE (init))
5fe48b3d 2415 && (extern_flag || static_storage
6249559b 2416 ? initializer_constant_valid_p (init, TREE_TYPE (init))
5fe48b3d 2417 != NULL_TREE
6249559b 2418 : TREE_CONSTANT (init)));
a1ab4c31
AC
2419
2420 /* Whether we will make TREE_CONSTANT the DECL we produce here, in which
5fe48b3d 2421 case the initializer may be used in lieu of the DECL node (as done in
a1ab4c31 2422 Identifier_to_gnu). This is useful to prevent the need of elaboration
5fe48b3d
EB
2423 code when an identifier for which such a DECL is made is in turn used
2424 as an initializer. We used to rely on CONST_DECL vs VAR_DECL for this,
2425 but extra constraints apply to this choice (see below) and they are not
2426 relevant to the distinction we wish to make. */
2427 const bool constant_p = const_flag && init_const;
a1ab4c31
AC
2428
2429 /* The actual DECL node. CONST_DECL was initially intended for enumerals
2430 and may be used for scalars in general but not for aggregates. */
2431 tree var_decl
c172df28
AH
2432 = build_decl (input_location,
2433 (constant_p && const_decl_allowed_p
a1ab4c31 2434 && !AGGREGATE_TYPE_P (type)) ? CONST_DECL : VAR_DECL,
6249559b 2435 name, type);
a1ab4c31 2436
93e708f9
EB
2437 /* Detect constants created by the front-end to hold 'reference to function
2438 calls for stabilization purposes. This is needed for renaming. */
2439 if (const_flag && init && POINTER_TYPE_P (type))
2440 {
2441 tree inner = init;
2442 if (TREE_CODE (inner) == COMPOUND_EXPR)
2443 inner = TREE_OPERAND (inner, 1);
2444 inner = remove_conversions (inner, true);
2445 if (TREE_CODE (inner) == ADDR_EXPR
2446 && ((TREE_CODE (TREE_OPERAND (inner, 0)) == CALL_EXPR
2447 && !call_is_atomic_load (TREE_OPERAND (inner, 0)))
2448 || (TREE_CODE (TREE_OPERAND (inner, 0)) == VAR_DECL
2449 && DECL_RETURN_VALUE_P (TREE_OPERAND (inner, 0)))))
2450 DECL_RETURN_VALUE_P (var_decl) = 1;
2451 }
2452
a1ab4c31
AC
2453 /* If this is external, throw away any initializations (they will be done
2454 elsewhere) unless this is a constant for which we would like to remain
2455 able to get the initializer. If we are defining a global here, leave a
2456 constant initialization and save any variable elaborations for the
2457 elaboration routine. If we are just annotating types, throw away the
2458 initialization if it isn't a constant. */
2459 if ((extern_flag && !constant_p)
6249559b
EB
2460 || (type_annotate_only && init && !TREE_CONSTANT (init)))
2461 init = NULL_TREE;
a1ab4c31 2462
5fe48b3d
EB
2463 /* At the global level, a non-constant initializer generates elaboration
2464 statements. Check that such statements are allowed, that is to say,
2465 not violating a No_Elaboration_Code restriction. */
6249559b 2466 if (init && !init_const && global_bindings_p ())
a1ab4c31 2467 Check_Elaboration_Code_Allowed (gnat_node);
3b9e8343 2468
c1a569ef 2469 /* Attach the initializer, if any. */
6249559b 2470 DECL_INITIAL (var_decl) = init;
c1a569ef
EB
2471
2472 /* Directly set some flags. */
2473 DECL_ARTIFICIAL (var_decl) = artificial_p;
8b7b0c36 2474 DECL_EXTERNAL (var_decl) = extern_flag;
a1ab4c31 2475
ffe9a0a7
EB
2476 TREE_CONSTANT (var_decl) = constant_p;
2477 TREE_READONLY (var_decl) = const_flag;
2478
2479 /* The object is public if it is external or if it is declared public
2480 and has static storage duration. */
2481 TREE_PUBLIC (var_decl) = extern_flag || (public_flag && static_storage);
2482
2483 /* We need to allocate static storage for an object with static storage
2484 duration if it isn't external. */
2485 TREE_STATIC (var_decl) = !extern_flag && static_storage;
2486
2487 TREE_SIDE_EFFECTS (var_decl)
2488 = TREE_THIS_VOLATILE (var_decl)
2489 = TYPE_VOLATILE (type) | volatile_flag;
2490
2491 if (TREE_SIDE_EFFECTS (var_decl))
2492 TREE_ADDRESSABLE (var_decl) = 1;
2493
a1ab4c31
AC
2494 /* Ada doesn't feature Fortran-like COMMON variables so we shouldn't
2495 try to fiddle with DECL_COMMON. However, on platforms that don't
2496 support global BSS sections, uninitialized global variables would
2497 go in DATA instead, thus increasing the size of the executable. */
2498 if (!flag_no_common
2499 && TREE_CODE (var_decl) == VAR_DECL
3b9e8343 2500 && TREE_PUBLIC (var_decl)
a1ab4c31
AC
2501 && !have_global_bss_p ())
2502 DECL_COMMON (var_decl) = 1;
a1ab4c31 2503
c1a569ef
EB
2504 /* Do not emit debug info for a CONST_DECL if optimization isn't enabled,
2505 since we will create an associated variable. Likewise for an external
2506 constant whose initializer is not absolute, because this would mean a
2507 global relocation in a read-only section which runs afoul of the PE-COFF
2508 run-time relocation mechanism. */
2509 if (!debug_info_p
2510 || (TREE_CODE (var_decl) == CONST_DECL && !optimize)
2511 || (extern_flag
2512 && constant_p
6249559b
EB
2513 && init
2514 && initializer_constant_valid_p (init, TREE_TYPE (init))
c1a569ef 2515 != null_pointer_node))
5225a138
EB
2516 DECL_IGNORED_P (var_decl) = 1;
2517
74746d49
EB
2518 /* ??? Some attributes cannot be applied to CONST_DECLs. */
2519 if (TREE_CODE (var_decl) == VAR_DECL)
2520 process_attributes (&var_decl, &attr_list, true, gnat_node);
2521
2522 /* Add this decl to the current binding level. */
2523 gnat_pushdecl (var_decl, gnat_node);
2524
a22b794d 2525 if (TREE_CODE (var_decl) == VAR_DECL && asm_name)
a1ab4c31 2526 {
a22b794d
EB
2527 /* Let the target mangle the name if this isn't a verbatim asm. */
2528 if (*IDENTIFIER_POINTER (asm_name) != '*')
2529 asm_name = targetm.mangle_decl_assembler_name (var_decl, asm_name);
74746d49 2530
a22b794d 2531 SET_DECL_ASSEMBLER_NAME (var_decl, asm_name);
a1ab4c31 2532 }
a1ab4c31
AC
2533
2534 return var_decl;
2535}
2536\f
2537/* Return true if TYPE, an aggregate type, contains (or is) an array. */
2538
2539static bool
2540aggregate_type_contains_array_p (tree type)
2541{
2542 switch (TREE_CODE (type))
2543 {
2544 case RECORD_TYPE:
2545 case UNION_TYPE:
2546 case QUAL_UNION_TYPE:
2547 {
2548 tree field;
910ad8de 2549 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
a1ab4c31
AC
2550 if (AGGREGATE_TYPE_P (TREE_TYPE (field))
2551 && aggregate_type_contains_array_p (TREE_TYPE (field)))
2552 return true;
2553 return false;
2554 }
2555
2556 case ARRAY_TYPE:
2557 return true;
2558
2559 default:
2560 gcc_unreachable ();
2561 }
2562}
2563
6249559b
EB
2564/* Return a FIELD_DECL node. NAME is the field's name, TYPE is its type and
2565 RECORD_TYPE is the type of the enclosing record. If SIZE is nonzero, it
2566 is the specified size of the field. If POS is nonzero, it is the bit
2567 position. PACKED is 1 if the enclosing record is packed, -1 if it has
2568 Component_Alignment of Storage_Unit. If ADDRESSABLE is nonzero, it
62f9f3ce
EB
2569 means we are allowed to take the address of the field; if it is negative,
2570 we should not make a bitfield, which is used by make_aligning_type. */
a1ab4c31
AC
2571
2572tree
6249559b
EB
2573create_field_decl (tree name, tree type, tree record_type, tree size, tree pos,
2574 int packed, int addressable)
a1ab4c31 2575{
6249559b 2576 tree field_decl = build_decl (input_location, FIELD_DECL, name, type);
a1ab4c31
AC
2577
2578 DECL_CONTEXT (field_decl) = record_type;
6249559b 2579 TREE_READONLY (field_decl) = TYPE_READONLY (type);
a1ab4c31
AC
2580
2581 /* If FIELD_TYPE is BLKmode, we must ensure this is aligned to at least a
2582 byte boundary since GCC cannot handle less-aligned BLKmode bitfields.
2583 Likewise for an aggregate without specified position that contains an
2584 array, because in this case slices of variable length of this array
2585 must be handled by GCC and variable-sized objects need to be aligned
2586 to at least a byte boundary. */
6249559b 2587 if (packed && (TYPE_MODE (type) == BLKmode
a1ab4c31 2588 || (!pos
6249559b
EB
2589 && AGGREGATE_TYPE_P (type)
2590 && aggregate_type_contains_array_p (type))))
fe37c7af 2591 SET_DECL_ALIGN (field_decl, BITS_PER_UNIT);
a1ab4c31
AC
2592
2593 /* If a size is specified, use it. Otherwise, if the record type is packed
2594 compute a size to use, which may differ from the object's natural size.
2595 We always set a size in this case to trigger the checks for bitfield
2596 creation below, which is typically required when no position has been
2597 specified. */
2598 if (size)
2599 size = convert (bitsizetype, size);
2600 else if (packed == 1)
2601 {
6249559b
EB
2602 size = rm_size (type);
2603 if (TYPE_MODE (type) == BLKmode)
62f9f3ce 2604 size = round_up (size, BITS_PER_UNIT);
a1ab4c31
AC
2605 }
2606
2607 /* If we may, according to ADDRESSABLE, make a bitfield if a size is
2608 specified for two reasons: first if the size differs from the natural
2609 size. Second, if the alignment is insufficient. There are a number of
2610 ways the latter can be true.
2611
2612 We never make a bitfield if the type of the field has a nonconstant size,
2613 because no such entity requiring bitfield operations should reach here.
2614
2615 We do *preventively* make a bitfield when there might be the need for it
2616 but we don't have all the necessary information to decide, as is the case
2617 of a field with no specified position in a packed record.
2618
2619 We also don't look at STRICT_ALIGNMENT here, and rely on later processing
2620 in layout_decl or finish_record_type to clear the bit_field indication if
2621 it is in fact not needed. */
2622 if (addressable >= 0
2623 && size
2624 && TREE_CODE (size) == INTEGER_CST
6249559b
EB
2625 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
2626 && (!tree_int_cst_equal (size, TYPE_SIZE (type))
2627 || (pos && !value_factor_p (pos, TYPE_ALIGN (type)))
a1ab4c31
AC
2628 || packed
2629 || (TYPE_ALIGN (record_type) != 0
6249559b 2630 && TYPE_ALIGN (record_type) < TYPE_ALIGN (type))))
a1ab4c31
AC
2631 {
2632 DECL_BIT_FIELD (field_decl) = 1;
2633 DECL_SIZE (field_decl) = size;
2634 if (!packed && !pos)
feec4372
EB
2635 {
2636 if (TYPE_ALIGN (record_type) != 0
6249559b 2637 && TYPE_ALIGN (record_type) < TYPE_ALIGN (type))
fe37c7af 2638 SET_DECL_ALIGN (field_decl, TYPE_ALIGN (record_type));
feec4372 2639 else
fe37c7af 2640 SET_DECL_ALIGN (field_decl, TYPE_ALIGN (type));
feec4372 2641 }
a1ab4c31
AC
2642 }
2643
2644 DECL_PACKED (field_decl) = pos ? DECL_BIT_FIELD (field_decl) : packed;
2645
2646 /* Bump the alignment if need be, either for bitfield/packing purposes or
2647 to satisfy the type requirements if no such consideration applies. When
2648 we get the alignment from the type, indicate if this is from an explicit
2649 user request, which prevents stor-layout from lowering it later on. */
2650 {
d9223014 2651 unsigned int bit_align
a1ab4c31 2652 = (DECL_BIT_FIELD (field_decl) ? 1
6249559b 2653 : packed && TYPE_MODE (type) != BLKmode ? BITS_PER_UNIT : 0);
a1ab4c31
AC
2654
2655 if (bit_align > DECL_ALIGN (field_decl))
fe37c7af 2656 SET_DECL_ALIGN (field_decl, bit_align);
6249559b 2657 else if (!bit_align && TYPE_ALIGN (type) > DECL_ALIGN (field_decl))
a1ab4c31 2658 {
fe37c7af 2659 SET_DECL_ALIGN (field_decl, TYPE_ALIGN (type));
6249559b 2660 DECL_USER_ALIGN (field_decl) = TYPE_USER_ALIGN (type);
a1ab4c31
AC
2661 }
2662 }
2663
2664 if (pos)
2665 {
2666 /* We need to pass in the alignment the DECL is known to have.
2667 This is the lowest-order bit set in POS, but no more than
2668 the alignment of the record, if one is specified. Note
2669 that an alignment of 0 is taken as infinite. */
2670 unsigned int known_align;
2671
cc269bb6 2672 if (tree_fits_uhwi_p (pos))
ae7e9ddd 2673 known_align = tree_to_uhwi (pos) & - tree_to_uhwi (pos);
a1ab4c31
AC
2674 else
2675 known_align = BITS_PER_UNIT;
2676
2677 if (TYPE_ALIGN (record_type)
2678 && (known_align == 0 || known_align > TYPE_ALIGN (record_type)))
2679 known_align = TYPE_ALIGN (record_type);
2680
2681 layout_decl (field_decl, known_align);
2682 SET_DECL_OFFSET_ALIGN (field_decl,
cc269bb6 2683 tree_fits_uhwi_p (pos) ? BIGGEST_ALIGNMENT
a1ab4c31
AC
2684 : BITS_PER_UNIT);
2685 pos_from_bit (&DECL_FIELD_OFFSET (field_decl),
2686 &DECL_FIELD_BIT_OFFSET (field_decl),
2687 DECL_OFFSET_ALIGN (field_decl), pos);
a1ab4c31
AC
2688 }
2689
2690 /* In addition to what our caller says, claim the field is addressable if we
2691 know that its type is not suitable.
2692
2693 The field may also be "technically" nonaddressable, meaning that even if
2694 we attempt to take the field's address we will actually get the address
2695 of a copy. This is the case for true bitfields, but the DECL_BIT_FIELD
2696 value we have at this point is not accurate enough, so we don't account
2697 for this here and let finish_record_type decide. */
6249559b 2698 if (!addressable && !type_for_nonaliased_component_p (type))
a1ab4c31
AC
2699 addressable = 1;
2700
2701 DECL_NONADDRESSABLE_P (field_decl) = !addressable;
2702
2703 return field_decl;
2704}
2705\f
1e55d29a 2706/* Return a PARM_DECL node with NAME and TYPE. */
a1ab4c31
AC
2707
2708tree
1e55d29a 2709create_param_decl (tree name, tree type)
a1ab4c31 2710{
6249559b 2711 tree param_decl = build_decl (input_location, PARM_DECL, name, type);
a1ab4c31 2712
a8e05f92
EB
2713 /* Honor TARGET_PROMOTE_PROTOTYPES like the C compiler, as not doing so
2714 can lead to various ABI violations. */
2715 if (targetm.calls.promote_prototypes (NULL_TREE)
6249559b
EB
2716 && INTEGRAL_TYPE_P (type)
2717 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
a1ab4c31
AC
2718 {
2719 /* We have to be careful about biased types here. Make a subtype
2720 of integer_type_node with the proper biasing. */
6249559b
EB
2721 if (TREE_CODE (type) == INTEGER_TYPE
2722 && TYPE_BIASED_REPRESENTATION_P (type))
a1ab4c31 2723 {
84fb43a1
EB
2724 tree subtype
2725 = make_unsigned_type (TYPE_PRECISION (integer_type_node));
c1abd261
EB
2726 TREE_TYPE (subtype) = integer_type_node;
2727 TYPE_BIASED_REPRESENTATION_P (subtype) = 1;
6249559b
EB
2728 SET_TYPE_RM_MIN_VALUE (subtype, TYPE_MIN_VALUE (type));
2729 SET_TYPE_RM_MAX_VALUE (subtype, TYPE_MAX_VALUE (type));
2730 type = subtype;
a1ab4c31
AC
2731 }
2732 else
6249559b 2733 type = integer_type_node;
a1ab4c31
AC
2734 }
2735
6249559b 2736 DECL_ARG_TYPE (param_decl) = type;
a1ab4c31
AC
2737 return param_decl;
2738}
2739\f
74746d49
EB
2740/* Process the attributes in ATTR_LIST for NODE, which is either a DECL or
2741 a TYPE. If IN_PLACE is true, the tree pointed to by NODE should not be
2742 changed. GNAT_NODE is used for the position of error messages. */
a1ab4c31 2743
74746d49
EB
2744void
2745process_attributes (tree *node, struct attrib **attr_list, bool in_place,
2746 Node_Id gnat_node)
a1ab4c31 2747{
74746d49
EB
2748 struct attrib *attr;
2749
2750 for (attr = *attr_list; attr; attr = attr->next)
2751 switch (attr->type)
a1ab4c31
AC
2752 {
2753 case ATTR_MACHINE_ATTRIBUTE:
74746d49
EB
2754 Sloc_to_locus (Sloc (gnat_node), &input_location);
2755 decl_attributes (node, tree_cons (attr->name, attr->args, NULL_TREE),
2756 in_place ? ATTR_FLAG_TYPE_IN_PLACE : 0);
a1ab4c31
AC
2757 break;
2758
2759 case ATTR_LINK_ALIAS:
74746d49 2760 if (!DECL_EXTERNAL (*node))
a1ab4c31 2761 {
74746d49
EB
2762 TREE_STATIC (*node) = 1;
2763 assemble_alias (*node, attr->name);
a1ab4c31
AC
2764 }
2765 break;
2766
2767 case ATTR_WEAK_EXTERNAL:
2768 if (SUPPORTS_WEAK)
74746d49 2769 declare_weak (*node);
a1ab4c31
AC
2770 else
2771 post_error ("?weak declarations not supported on this target",
74746d49 2772 attr->error_point);
a1ab4c31
AC
2773 break;
2774
2775 case ATTR_LINK_SECTION:
677f3fa8 2776 if (targetm_common.have_named_sections)
a1ab4c31 2777 {
0ab75824 2778 set_decl_section_name (*node, IDENTIFIER_POINTER (attr->name));
74746d49 2779 DECL_COMMON (*node) = 0;
a1ab4c31
AC
2780 }
2781 else
2782 post_error ("?section attributes are not supported for this target",
74746d49 2783 attr->error_point);
a1ab4c31
AC
2784 break;
2785
2786 case ATTR_LINK_CONSTRUCTOR:
74746d49
EB
2787 DECL_STATIC_CONSTRUCTOR (*node) = 1;
2788 TREE_USED (*node) = 1;
a1ab4c31
AC
2789 break;
2790
2791 case ATTR_LINK_DESTRUCTOR:
74746d49
EB
2792 DECL_STATIC_DESTRUCTOR (*node) = 1;
2793 TREE_USED (*node) = 1;
a1ab4c31 2794 break;
40a14772
TG
2795
2796 case ATTR_THREAD_LOCAL_STORAGE:
56363ffd 2797 set_decl_tls_model (*node, decl_default_tls_model (*node));
74746d49 2798 DECL_COMMON (*node) = 0;
40a14772 2799 break;
a1ab4c31 2800 }
74746d49
EB
2801
2802 *attr_list = NULL;
a1ab4c31 2803}
a1ab4c31
AC
2804
2805/* Return true if VALUE is a known to be a multiple of FACTOR, which must be
2806 a power of 2. */
2807
2808bool
2809value_factor_p (tree value, HOST_WIDE_INT factor)
2810{
cc269bb6 2811 if (tree_fits_uhwi_p (value))
ae7e9ddd 2812 return tree_to_uhwi (value) % factor == 0;
a1ab4c31
AC
2813
2814 if (TREE_CODE (value) == MULT_EXPR)
2815 return (value_factor_p (TREE_OPERAND (value, 0), factor)
2816 || value_factor_p (TREE_OPERAND (value, 1), factor));
2817
2818 return false;
2819}
2820
e8fa3dcd
PMR
2821/* Return whether GNAT_NODE is a defining identifier for a renaming that comes
2822 from the parameter association for the instantiation of a generic. We do
2823 not want to emit source location for them: the code generated for their
2824 initialization is likely to disturb debugging. */
2825
2826bool
2827renaming_from_generic_instantiation_p (Node_Id gnat_node)
2828{
2829 if (Nkind (gnat_node) != N_Defining_Identifier
2830 || !IN (Ekind (gnat_node), Object_Kind)
2831 || Comes_From_Source (gnat_node)
2832 || !Present (Renamed_Object (gnat_node)))
2833 return false;
2834
2835 /* Get the object declaration of the renamed object, if any and if the
2836 renamed object is a mere identifier. */
2837 gnat_node = Renamed_Object (gnat_node);
2838 if (Nkind (gnat_node) != N_Identifier)
2839 return false;
2840
2841 gnat_node = Entity (gnat_node);
2842 if (!Present (Parent (gnat_node)))
2843 return false;
2844
2845 gnat_node = Parent (gnat_node);
2846 return
2847 (Present (gnat_node)
2848 && Nkind (gnat_node) == N_Object_Declaration
2849 && Present (Corresponding_Generic_Association (gnat_node)));
2850}
2851
9a30c7c4
AC
2852/* Defer the initialization of DECL's DECL_CONTEXT attribute, scheduling to
2853 feed it with the elaboration of GNAT_SCOPE. */
2854
2855static struct deferred_decl_context_node *
2856add_deferred_decl_context (tree decl, Entity_Id gnat_scope, int force_global)
2857{
2858 struct deferred_decl_context_node *new_node;
2859
2860 new_node
2861 = (struct deferred_decl_context_node * ) xmalloc (sizeof (*new_node));
2862 new_node->decl = decl;
2863 new_node->gnat_scope = gnat_scope;
2864 new_node->force_global = force_global;
2865 new_node->types.create (1);
2866 new_node->next = deferred_decl_context_queue;
2867 deferred_decl_context_queue = new_node;
2868 return new_node;
2869}
2870
2871/* Defer the initialization of TYPE's TYPE_CONTEXT attribute, scheduling to
2872 feed it with the DECL_CONTEXT computed as part of N as soon as it is
2873 computed. */
2874
2875static void
2876add_deferred_type_context (struct deferred_decl_context_node *n, tree type)
2877{
2878 n->types.safe_push (type);
2879}
2880
2881/* Get the GENERIC node corresponding to GNAT_SCOPE, if available. Return
2882 NULL_TREE if it is not available. */
2883
2884static tree
2885compute_deferred_decl_context (Entity_Id gnat_scope)
2886{
2887 tree context;
2888
2889 if (present_gnu_tree (gnat_scope))
2890 context = get_gnu_tree (gnat_scope);
2891 else
2892 return NULL_TREE;
2893
2894 if (TREE_CODE (context) == TYPE_DECL)
2895 {
2896 const tree context_type = TREE_TYPE (context);
2897
2898 /* Skip dummy types: only the final ones can appear in the context
2899 chain. */
2900 if (TYPE_DUMMY_P (context_type))
2901 return NULL_TREE;
2902
2903 /* ..._TYPE nodes are more useful than TYPE_DECL nodes in the context
2904 chain. */
2905 else
2906 context = context_type;
2907 }
2908
2909 return context;
2910}
2911
2912/* Try to process all deferred nodes in the queue. Keep in the queue the ones
2913 that cannot be processed yet, remove the other ones. If FORCE is true,
2914 force the processing for all nodes, use the global context when nodes don't
2915 have a GNU translation. */
2916
2917void
2918process_deferred_decl_context (bool force)
2919{
2920 struct deferred_decl_context_node **it = &deferred_decl_context_queue;
2921 struct deferred_decl_context_node *node;
2922
2923 while (*it != NULL)
2924 {
2925 bool processed = false;
2926 tree context = NULL_TREE;
2927 Entity_Id gnat_scope;
2928
2929 node = *it;
2930
2931 /* If FORCE, get the innermost elaborated scope. Otherwise, just try to
2932 get the first scope. */
2933 gnat_scope = node->gnat_scope;
2934 while (Present (gnat_scope))
2935 {
2936 context = compute_deferred_decl_context (gnat_scope);
7c775aca 2937 if (!force || context)
9a30c7c4
AC
2938 break;
2939 gnat_scope = get_debug_scope (gnat_scope, NULL);
2940 }
2941
2942 /* Imported declarations must not be in a local context (i.e. not inside
2943 a function). */
7c775aca 2944 if (context && node->force_global > 0)
9a30c7c4
AC
2945 {
2946 tree ctx = context;
2947
7c775aca 2948 while (ctx)
9a30c7c4
AC
2949 {
2950 gcc_assert (TREE_CODE (ctx) != FUNCTION_DECL);
7c775aca 2951 ctx = DECL_P (ctx) ? DECL_CONTEXT (ctx) : TYPE_CONTEXT (ctx);
9a30c7c4
AC
2952 }
2953 }
2954
2955 /* If FORCE, we want to get rid of all nodes in the queue: in case there
2956 was no elaborated scope, use the global context. */
7c775aca 2957 if (force && !context)
9a30c7c4
AC
2958 context = get_global_context ();
2959
7c775aca 2960 if (context)
9a30c7c4
AC
2961 {
2962 tree t;
2963 int i;
2964
2965 DECL_CONTEXT (node->decl) = context;
2966
2967 /* Propagate it to the TYPE_CONTEXT attributes of the requested
2968 ..._TYPE nodes. */
2969 FOR_EACH_VEC_ELT (node->types, i, t)
2970 {
24d4b3d5 2971 gnat_set_type_context (t, context);
9a30c7c4
AC
2972 }
2973 processed = true;
2974 }
2975
2976 /* If this node has been successfuly processed, remove it from the
2977 queue. Then move to the next node. */
2978 if (processed)
2979 {
2980 *it = node->next;
2981 node->types.release ();
2982 free (node);
2983 }
2984 else
2985 it = &node->next;
2986 }
2987}
2988
2989
5c475ba9
EB
2990/* Return VALUE scaled by the biggest power-of-2 factor of EXPR. */
2991
2992static unsigned int
2993scale_by_factor_of (tree expr, unsigned int value)
2994{
3b5d86ec
PMR
2995 unsigned HOST_WIDE_INT addend = 0;
2996 unsigned HOST_WIDE_INT factor = 1;
2997
2998 /* Peel conversions around EXPR and try to extract bodies from function
2999 calls: it is possible to get the scale factor from size functions. */
5c475ba9 3000 expr = remove_conversions (expr, true);
3b5d86ec
PMR
3001 if (TREE_CODE (expr) == CALL_EXPR)
3002 expr = maybe_inline_call_in_expr (expr);
3003
3004 /* Sometimes we get PLUS_EXPR (BIT_AND_EXPR (..., X), Y), where Y is a
3005 multiple of the scale factor we are looking for. */
3006 if (TREE_CODE (expr) == PLUS_EXPR
3007 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
3008 && tree_fits_uhwi_p (TREE_OPERAND (expr, 1)))
3009 {
3010 addend = TREE_INT_CST_LOW (TREE_OPERAND (expr, 1));
3011 expr = TREE_OPERAND (expr, 0);
3012 }
5c475ba9
EB
3013
3014 /* An expression which is a bitwise AND with a mask has a power-of-2 factor
3015 corresponding to the number of trailing zeros of the mask. */
3016 if (TREE_CODE (expr) == BIT_AND_EXPR
3017 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
3018 {
3019 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (TREE_OPERAND (expr, 1));
3020 unsigned int i = 0;
3021
3022 while ((mask & 1) == 0 && i < HOST_BITS_PER_WIDE_INT)
3023 {
3024 mask >>= 1;
3b5d86ec 3025 factor *= 2;
5c475ba9
EB
3026 i++;
3027 }
3028 }
3029
3b5d86ec
PMR
3030 /* If the addend is not a multiple of the factor we found, give up. In
3031 theory we could find a smaller common factor but it's useless for our
3032 needs. This situation arises when dealing with a field F1 with no
3033 alignment requirement but that is following a field F2 with such
3034 requirements. As long as we have F2's offset, we don't need alignment
3035 information to compute F1's. */
3036 if (addend % factor != 0)
3037 factor = 1;
3038
3039 return factor * value;
5c475ba9
EB
3040}
3041
7d7fcb08 3042/* Given two consecutive field decls PREV_FIELD and CURR_FIELD, return true
a1ab4c31
AC
3043 unless we can prove these 2 fields are laid out in such a way that no gap
3044 exist between the end of PREV_FIELD and the beginning of CURR_FIELD. OFFSET
3045 is the distance in bits between the end of PREV_FIELD and the starting
3046 position of CURR_FIELD. It is ignored if null. */
3047
3048static bool
3049potential_alignment_gap (tree prev_field, tree curr_field, tree offset)
3050{
3051 /* If this is the first field of the record, there cannot be any gap */
3052 if (!prev_field)
3053 return false;
3054
78df6221 3055 /* If the previous field is a union type, then return false: The only
a1ab4c31
AC
3056 time when such a field is not the last field of the record is when
3057 there are other components at fixed positions after it (meaning there
3058 was a rep clause for every field), in which case we don't want the
3059 alignment constraint to override them. */
3060 if (TREE_CODE (TREE_TYPE (prev_field)) == QUAL_UNION_TYPE)
3061 return false;
3062
3063 /* If the distance between the end of prev_field and the beginning of
3064 curr_field is constant, then there is a gap if the value of this
3065 constant is not null. */
cc269bb6 3066 if (offset && tree_fits_uhwi_p (offset))
a1ab4c31
AC
3067 return !integer_zerop (offset);
3068
3069 /* If the size and position of the previous field are constant,
3070 then check the sum of this size and position. There will be a gap
3071 iff it is not multiple of the current field alignment. */
cc269bb6
RS
3072 if (tree_fits_uhwi_p (DECL_SIZE (prev_field))
3073 && tree_fits_uhwi_p (bit_position (prev_field)))
ae7e9ddd
RS
3074 return ((tree_to_uhwi (bit_position (prev_field))
3075 + tree_to_uhwi (DECL_SIZE (prev_field)))
a1ab4c31
AC
3076 % DECL_ALIGN (curr_field) != 0);
3077
3078 /* If both the position and size of the previous field are multiples
3079 of the current field alignment, there cannot be any gap. */
3080 if (value_factor_p (bit_position (prev_field), DECL_ALIGN (curr_field))
3081 && value_factor_p (DECL_SIZE (prev_field), DECL_ALIGN (curr_field)))
3082 return false;
3083
3084 /* Fallback, return that there may be a potential gap */
3085 return true;
3086}
3087
6249559b
EB
3088/* Return a LABEL_DECL with NAME. GNAT_NODE is used for the position of
3089 the decl. */
a1ab4c31
AC
3090
3091tree
6249559b 3092create_label_decl (tree name, Node_Id gnat_node)
a1ab4c31 3093{
88a94e2b 3094 tree label_decl
6249559b 3095 = build_decl (input_location, LABEL_DECL, name, void_type_node);
a1ab4c31 3096
88a94e2b
EB
3097 DECL_MODE (label_decl) = VOIDmode;
3098
3099 /* Add this decl to the current binding level. */
3100 gnat_pushdecl (label_decl, gnat_node);
a1ab4c31
AC
3101
3102 return label_decl;
3103}
3104\f
6249559b
EB
3105/* Return a FUNCTION_DECL node. NAME is the name of the subprogram, ASM_NAME
3106 its assembler name, TYPE its type (a FUNCTION_TYPE node), PARAM_DECL_LIST
3107 the list of its parameters (a list of PARM_DECL nodes chained through the
3108 DECL_CHAIN field).
a1ab4c31 3109
2ade427a
EB
3110 INLINE_STATUS describes the inline flags to be set on the FUNCTION_DECL.
3111
1e55d29a
EB
3112 PUBLIC_FLAG is true if this is for a reference to a public entity or for a
3113 definition to be made visible outside of the current compilation unit.
3114
3115 EXTERN_FLAG is true when processing an external subprogram declaration.
c1a569ef
EB
3116
3117 ARTIFICIAL_P is true if the subprogram was generated by the compiler.
3118
3119 DEBUG_INFO_P is true if we need to write debug information for it.
3120
2ade427a
EB
3121 ATTR_LIST is the list of attributes to be attached to the subprogram.
3122
c1a569ef 3123 GNAT_NODE is used for the position of the decl. */
a1ab4c31
AC
3124
3125tree
6249559b 3126create_subprog_decl (tree name, tree asm_name, tree type, tree param_decl_list,
1e55d29a
EB
3127 enum inline_status_t inline_status, bool public_flag,
3128 bool extern_flag, bool artificial_p, bool debug_info_p,
6249559b 3129 struct attrib *attr_list, Node_Id gnat_node)
a1ab4c31 3130{
6249559b 3131 tree subprog_decl = build_decl (input_location, FUNCTION_DECL, name, type);
7d7fcb08 3132 DECL_ARGUMENTS (subprog_decl) = param_decl_list;
1e55d29a 3133 finish_subprog_decl (subprog_decl, type);
a1ab4c31 3134
c1a569ef 3135 DECL_ARTIFICIAL (subprog_decl) = artificial_p;
7d7fcb08 3136 DECL_EXTERNAL (subprog_decl) = extern_flag;
1e55d29a
EB
3137 TREE_PUBLIC (subprog_decl) = public_flag;
3138
3139 if (!debug_info_p)
3140 DECL_IGNORED_P (subprog_decl) = 1;
0e24192c
EB
3141
3142 switch (inline_status)
3143 {
3144 case is_suppressed:
3145 DECL_UNINLINABLE (subprog_decl) = 1;
3146 break;
3147
3148 case is_disabled:
3149 break;
3150
f087ea44
AC
3151 case is_required:
3152 if (Back_End_Inlining)
1eb58520
AC
3153 decl_attributes (&subprog_decl,
3154 tree_cons (get_identifier ("always_inline"),
3155 NULL_TREE, NULL_TREE),
3156 ATTR_FLAG_TYPE_IN_PLACE);
3157
f087ea44
AC
3158 /* ... fall through ... */
3159
0e24192c
EB
3160 case is_enabled:
3161 DECL_DECLARED_INLINE_P (subprog_decl) = 1;
c1a569ef 3162 DECL_NO_INLINE_WARNING_P (subprog_decl) = artificial_p;
0e24192c
EB
3163 break;
3164
3165 default:
3166 gcc_unreachable ();
3167 }
7d7fcb08 3168
2b50232a
EB
3169 process_attributes (&subprog_decl, &attr_list, true, gnat_node);
3170
3171 /* Add this decl to the current binding level. */
3172 gnat_pushdecl (subprog_decl, gnat_node);
3173
a1ab4c31
AC
3174 if (asm_name)
3175 {
2b50232a
EB
3176 /* Let the target mangle the name if this isn't a verbatim asm. */
3177 if (*IDENTIFIER_POINTER (asm_name) != '*')
3178 asm_name = targetm.mangle_decl_assembler_name (subprog_decl, asm_name);
3179
a1ab4c31
AC
3180 SET_DECL_ASSEMBLER_NAME (subprog_decl, asm_name);
3181
3182 /* The expand_main_function circuitry expects "main_identifier_node" to
3183 designate the DECL_NAME of the 'main' entry point, in turn expected
3184 to be declared as the "main" function literally by default. Ada
3185 program entry points are typically declared with a different name
3186 within the binder generated file, exported as 'main' to satisfy the
cfbb663c 3187 system expectations. Force main_identifier_node in this case. */
a1ab4c31 3188 if (asm_name == main_identifier_node)
cfbb663c 3189 DECL_NAME (subprog_decl) = main_identifier_node;
a1ab4c31
AC
3190 }
3191
a1ab4c31
AC
3192 /* Output the assembler code and/or RTL for the declaration. */
3193 rest_of_decl_compilation (subprog_decl, global_bindings_p (), 0);
3194
3195 return subprog_decl;
3196}
1e55d29a
EB
3197
3198/* Given a subprogram declaration DECL and its TYPE, finish constructing the
3199 subprogram declaration from TYPE. */
3200
3201void
3202finish_subprog_decl (tree decl, tree type)
3203{
3204 tree result_decl
3205 = build_decl (DECL_SOURCE_LOCATION (decl), RESULT_DECL, NULL_TREE,
3206 TREE_TYPE (type));
3207
3208 DECL_ARTIFICIAL (result_decl) = 1;
3209 DECL_IGNORED_P (result_decl) = 1;
3210 DECL_BY_REFERENCE (result_decl) = TREE_ADDRESSABLE (type);
3211 DECL_RESULT (decl) = result_decl;
3212
3213 TREE_READONLY (decl) = TYPE_READONLY (type);
3214 TREE_SIDE_EFFECTS (decl) = TREE_THIS_VOLATILE (decl) = TYPE_VOLATILE (type);
3215}
a1ab4c31
AC
3216\f
3217/* Set up the framework for generating code for SUBPROG_DECL, a subprogram
3218 body. This routine needs to be invoked before processing the declarations
3219 appearing in the subprogram. */
3220
3221void
3222begin_subprog_body (tree subprog_decl)
3223{
3224 tree param_decl;
3225
a1ab4c31
AC
3226 announce_function (subprog_decl);
3227
0ae44446
JR
3228 /* This function is being defined. */
3229 TREE_STATIC (subprog_decl) = 1;
3230
e2d13a4a
EB
3231 /* The failure of this assertion will likely come from a wrong context for
3232 the subprogram body, e.g. another procedure for a procedure declared at
3233 library level. */
3234 gcc_assert (current_function_decl == decl_function_context (subprog_decl));
3235
58c8f770
EB
3236 current_function_decl = subprog_decl;
3237
a1ab4c31
AC
3238 /* Enter a new binding level and show that all the parameters belong to
3239 this function. */
3240 gnat_pushlevel ();
a09d56d8 3241
a1ab4c31 3242 for (param_decl = DECL_ARGUMENTS (subprog_decl); param_decl;
910ad8de 3243 param_decl = DECL_CHAIN (param_decl))
a1ab4c31
AC
3244 DECL_CONTEXT (param_decl) = subprog_decl;
3245
3246 make_decl_rtl (subprog_decl);
a1ab4c31
AC
3247}
3248
71196d4e 3249/* Finish translating the current subprogram and set its BODY. */
a1ab4c31
AC
3250
3251void
a406865a 3252end_subprog_body (tree body)
a1ab4c31
AC
3253{
3254 tree fndecl = current_function_decl;
3255
bd9c7fb9 3256 /* Attach the BLOCK for this level to the function and pop the level. */
a1ab4c31
AC
3257 BLOCK_SUPERCONTEXT (current_binding_level->block) = fndecl;
3258 DECL_INITIAL (fndecl) = current_binding_level->block;
3259 gnat_poplevel ();
3260
a1ab4c31
AC
3261 /* Mark the RESULT_DECL as being in this subprogram. */
3262 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
3263
a963da4d
EB
3264 /* The body should be a BIND_EXPR whose BLOCK is the top-level one. */
3265 if (TREE_CODE (body) == BIND_EXPR)
3266 {
3267 BLOCK_SUPERCONTEXT (BIND_EXPR_BLOCK (body)) = fndecl;
3268 DECL_INITIAL (fndecl) = BIND_EXPR_BLOCK (body);
3269 }
3270
a1ab4c31
AC
3271 DECL_SAVED_TREE (fndecl) = body;
3272
228ee426 3273 current_function_decl = decl_function_context (fndecl);
71196d4e
EB
3274}
3275
3276/* Wrap up compilation of SUBPROG_DECL, a subprogram body. */
a1ab4c31 3277
71196d4e
EB
3278void
3279rest_of_subprog_body_compilation (tree subprog_decl)
3280{
a1ab4c31
AC
3281 /* We cannot track the location of errors past this point. */
3282 error_gnat_node = Empty;
3283
3284 /* If we're only annotating types, don't actually compile this function. */
3285 if (type_annotate_only)
3286 return;
3287
a406865a 3288 /* Dump functions before gimplification. */
71196d4e 3289 dump_function (TDI_original, subprog_decl);
a406865a 3290
228ee426 3291 if (!decl_function_context (subprog_decl))
3dafb85c 3292 cgraph_node::finalize_function (subprog_decl, false);
a1ab4c31
AC
3293 else
3294 /* Register this function with cgraph just far enough to get it
3295 added to our parent's nested function list. */
037e5573 3296 (void) cgraph_node::get_create (subprog_decl);
a1ab4c31
AC
3297}
3298
a1ab4c31
AC
3299tree
3300gnat_builtin_function (tree decl)
3301{
3302 gnat_pushdecl (decl, Empty);
3303 return decl;
3304}
3305
3306/* Return an integer type with the number of bits of precision given by
3307 PRECISION. UNSIGNEDP is nonzero if the type is unsigned; otherwise
3308 it is a signed type. */
3309
3310tree
3311gnat_type_for_size (unsigned precision, int unsignedp)
3312{
3313 tree t;
3314 char type_name[20];
3315
3316 if (precision <= 2 * MAX_BITS_PER_WORD
3317 && signed_and_unsigned_types[precision][unsignedp])
3318 return signed_and_unsigned_types[precision][unsignedp];
3319
3320 if (unsignedp)
3321 t = make_unsigned_type (precision);
3322 else
3323 t = make_signed_type (precision);
3324
3325 if (precision <= 2 * MAX_BITS_PER_WORD)
3326 signed_and_unsigned_types[precision][unsignedp] = t;
3327
3328 if (!TYPE_NAME (t))
3329 {
c3d79c60 3330 sprintf (type_name, "%sSIGNED_%u", unsignedp ? "UN" : "", precision);
a1ab4c31
AC
3331 TYPE_NAME (t) = get_identifier (type_name);
3332 }
3333
3334 return t;
3335}
3336
3337/* Likewise for floating-point types. */
3338
3339static tree
ef4bddc2 3340float_type_for_precision (int precision, machine_mode mode)
a1ab4c31
AC
3341{
3342 tree t;
3343 char type_name[20];
3344
3345 if (float_types[(int) mode])
3346 return float_types[(int) mode];
3347
3348 float_types[(int) mode] = t = make_node (REAL_TYPE);
3349 TYPE_PRECISION (t) = precision;
3350 layout_type (t);
3351
3352 gcc_assert (TYPE_MODE (t) == mode);
3353 if (!TYPE_NAME (t))
3354 {
3355 sprintf (type_name, "FLOAT_%d", precision);
3356 TYPE_NAME (t) = get_identifier (type_name);
3357 }
3358
3359 return t;
3360}
3361
3362/* Return a data type that has machine mode MODE. UNSIGNEDP selects
3363 an unsigned type; otherwise a signed type is returned. */
3364
3365tree
ef4bddc2 3366gnat_type_for_mode (machine_mode mode, int unsignedp)
a1ab4c31
AC
3367{
3368 if (mode == BLKmode)
3369 return NULL_TREE;
2799d18c
EB
3370
3371 if (mode == VOIDmode)
a1ab4c31 3372 return void_type_node;
2799d18c
EB
3373
3374 if (COMPLEX_MODE_P (mode))
a1ab4c31 3375 return NULL_TREE;
2799d18c
EB
3376
3377 if (SCALAR_FLOAT_MODE_P (mode))
a1ab4c31 3378 return float_type_for_precision (GET_MODE_PRECISION (mode), mode);
2799d18c
EB
3379
3380 if (SCALAR_INT_MODE_P (mode))
a1ab4c31 3381 return gnat_type_for_size (GET_MODE_BITSIZE (mode), unsignedp);
2799d18c
EB
3382
3383 if (VECTOR_MODE_P (mode))
3384 {
ef4bddc2 3385 machine_mode inner_mode = GET_MODE_INNER (mode);
2799d18c
EB
3386 tree inner_type = gnat_type_for_mode (inner_mode, unsignedp);
3387 if (inner_type)
3388 return build_vector_type_for_mode (inner_type, mode);
3389 }
3390
3391 return NULL_TREE;
a1ab4c31
AC
3392}
3393
9a1bdc31
EB
3394/* Return the signed or unsigned version of TYPE_NODE, a scalar type, the
3395 signedness being specified by UNSIGNEDP. */
a1ab4c31
AC
3396
3397tree
9a1bdc31 3398gnat_signed_or_unsigned_type_for (int unsignedp, tree type_node)
a1ab4c31 3399{
825da0d2
EB
3400 if (type_node == char_type_node)
3401 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3402
9a1bdc31 3403 tree type = gnat_type_for_size (TYPE_PRECISION (type_node), unsignedp);
a1ab4c31
AC
3404
3405 if (TREE_CODE (type_node) == INTEGER_TYPE && TYPE_MODULAR_P (type_node))
3406 {
afc737f0 3407 type = copy_type (type);
a1ab4c31
AC
3408 TREE_TYPE (type) = type_node;
3409 }
3410 else if (TREE_TYPE (type_node)
3411 && TREE_CODE (TREE_TYPE (type_node)) == INTEGER_TYPE
3412 && TYPE_MODULAR_P (TREE_TYPE (type_node)))
3413 {
afc737f0 3414 type = copy_type (type);
a1ab4c31
AC
3415 TREE_TYPE (type) = TREE_TYPE (type_node);
3416 }
3417
3418 return type;
3419}
3420
3421/* Return 1 if the types T1 and T2 are compatible, i.e. if they can be
3422 transparently converted to each other. */
3423
3424int
3425gnat_types_compatible_p (tree t1, tree t2)
3426{
3427 enum tree_code code;
3428
3429 /* This is the default criterion. */
3430 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
3431 return 1;
3432
3433 /* We only check structural equivalence here. */
3434 if ((code = TREE_CODE (t1)) != TREE_CODE (t2))
3435 return 0;
3436
7948ae37
OH
3437 /* Vector types are also compatible if they have the same number of subparts
3438 and the same form of (scalar) element type. */
3439 if (code == VECTOR_TYPE
3440 && TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
3441 && TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2))
3442 && TYPE_PRECISION (TREE_TYPE (t1)) == TYPE_PRECISION (TREE_TYPE (t2)))
3443 return 1;
3444
cfa0bd19 3445 /* Array types are also compatible if they are constrained and have the same
ee45a32d 3446 domain(s), the same component type and the same scalar storage order. */
a1ab4c31 3447 if (code == ARRAY_TYPE
0adef32b
JJ
3448 && (TYPE_DOMAIN (t1) == TYPE_DOMAIN (t2)
3449 || (TYPE_DOMAIN (t1)
b4680ca1 3450 && TYPE_DOMAIN (t2)
0adef32b
JJ
3451 && tree_int_cst_equal (TYPE_MIN_VALUE (TYPE_DOMAIN (t1)),
3452 TYPE_MIN_VALUE (TYPE_DOMAIN (t2)))
3453 && tree_int_cst_equal (TYPE_MAX_VALUE (TYPE_DOMAIN (t1)),
cfa0bd19 3454 TYPE_MAX_VALUE (TYPE_DOMAIN (t2)))))
96bba5e6
EB
3455 && (TREE_TYPE (t1) == TREE_TYPE (t2)
3456 || (TREE_CODE (TREE_TYPE (t1)) == ARRAY_TYPE
ee45a32d
EB
3457 && gnat_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))))
3458 && TYPE_REVERSE_STORAGE_ORDER (t1) == TYPE_REVERSE_STORAGE_ORDER (t2))
a1ab4c31
AC
3459 return 1;
3460
a1ab4c31
AC
3461 return 0;
3462}
523e82a7 3463
71196d4e
EB
3464/* Return true if EXPR is a useless type conversion. */
3465
3466bool
3467gnat_useless_type_conversion (tree expr)
3468{
3469 if (CONVERT_EXPR_P (expr)
3470 || TREE_CODE (expr) == VIEW_CONVERT_EXPR
3471 || TREE_CODE (expr) == NON_LVALUE_EXPR)
3472 return gnat_types_compatible_p (TREE_TYPE (expr),
3473 TREE_TYPE (TREE_OPERAND (expr, 0)));
3474
3475 return false;
3476}
3477
523e82a7
EB
3478/* Return true if T, a FUNCTION_TYPE, has the specified list of flags. */
3479
3480bool
3481fntype_same_flags_p (const_tree t, tree cico_list, bool return_unconstrained_p,
3482 bool return_by_direct_ref_p, bool return_by_invisi_ref_p)
3483{
3484 return TYPE_CI_CO_LIST (t) == cico_list
3485 && TYPE_RETURN_UNCONSTRAINED_P (t) == return_unconstrained_p
3486 && TYPE_RETURN_BY_DIRECT_REF_P (t) == return_by_direct_ref_p
3487 && TREE_ADDRESSABLE (t) == return_by_invisi_ref_p;
3488}
a1ab4c31
AC
3489\f
3490/* EXP is an expression for the size of an object. If this size contains
3491 discriminant references, replace them with the maximum (if MAX_P) or
3492 minimum (if !MAX_P) possible value of the discriminant. */
3493
3494tree
3495max_size (tree exp, bool max_p)
3496{
3497 enum tree_code code = TREE_CODE (exp);
3498 tree type = TREE_TYPE (exp);
3499
3500 switch (TREE_CODE_CLASS (code))
3501 {
3502 case tcc_declaration:
3503 case tcc_constant:
3504 return exp;
3505
3506 case tcc_vl_exp:
3507 if (code == CALL_EXPR)
3508 {
f82a627c
EB
3509 tree t, *argarray;
3510 int n, i;
3511
3512 t = maybe_inline_call_in_expr (exp);
3513 if (t)
3514 return max_size (t, max_p);
a1ab4c31 3515
f82a627c
EB
3516 n = call_expr_nargs (exp);
3517 gcc_assert (n > 0);
2bb1fc26 3518 argarray = XALLOCAVEC (tree, n);
a1ab4c31
AC
3519 for (i = 0; i < n; i++)
3520 argarray[i] = max_size (CALL_EXPR_ARG (exp, i), max_p);
3521 return build_call_array (type, CALL_EXPR_FN (exp), n, argarray);
3522 }
3523 break;
3524
3525 case tcc_reference:
3526 /* If this contains a PLACEHOLDER_EXPR, it is the thing we want to
3527 modify. Otherwise, we treat it like a variable. */
1eb58520
AC
3528 if (CONTAINS_PLACEHOLDER_P (exp))
3529 {
3530 tree val_type = TREE_TYPE (TREE_OPERAND (exp, 1));
3531 tree val = (max_p ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type));
3532 return max_size (convert (get_base_type (val_type), val), true);
3533 }
a1ab4c31 3534
1eb58520 3535 return exp;
a1ab4c31
AC
3536
3537 case tcc_comparison:
3538 return max_p ? size_one_node : size_zero_node;
3539
3540 case tcc_unary:
ce3da0d0
EB
3541 if (code == NON_LVALUE_EXPR)
3542 return max_size (TREE_OPERAND (exp, 0), max_p);
6625d7bc 3543
ce3da0d0
EB
3544 return fold_build1 (code, type,
3545 max_size (TREE_OPERAND (exp, 0),
3546 code == NEGATE_EXPR ? !max_p : max_p));
3547
a1ab4c31 3548 case tcc_binary:
ce3da0d0
EB
3549 {
3550 tree lhs = max_size (TREE_OPERAND (exp, 0), max_p);
3551 tree rhs = max_size (TREE_OPERAND (exp, 1),
3552 code == MINUS_EXPR ? !max_p : max_p);
3553
3554 /* Special-case wanting the maximum value of a MIN_EXPR.
3555 In that case, if one side overflows, return the other. */
3556 if (max_p && code == MIN_EXPR)
3557 {
3558 if (TREE_CODE (rhs) == INTEGER_CST && TREE_OVERFLOW (rhs))
3559 return lhs;
3560
3561 if (TREE_CODE (lhs) == INTEGER_CST && TREE_OVERFLOW (lhs))
3562 return rhs;
3563 }
3564
3565 /* Likewise, handle a MINUS_EXPR or PLUS_EXPR with the LHS
3566 overflowing and the RHS a variable. */
3567 if ((code == MINUS_EXPR || code == PLUS_EXPR)
3568 && TREE_CODE (lhs) == INTEGER_CST
3569 && TREE_OVERFLOW (lhs)
396e67d2 3570 && TREE_CODE (rhs) != INTEGER_CST)
ce3da0d0
EB
3571 return lhs;
3572
396e67d2
EB
3573 /* If we are going to subtract a "negative" value in an unsigned type,
3574 do the operation as an addition of the negated value, in order to
3575 avoid creating a spurious overflow below. */
3576 if (code == MINUS_EXPR
3577 && TYPE_UNSIGNED (type)
3578 && TREE_CODE (rhs) == INTEGER_CST
3579 && !TREE_OVERFLOW (rhs)
3580 && tree_int_cst_sign_bit (rhs) != 0)
3581 {
3582 rhs = fold_build1 (NEGATE_EXPR, type, rhs);
3583 code = PLUS_EXPR;
3584 }
3585
3586 /* We need to detect overflows so we call size_binop here. */
ce3da0d0
EB
3587 return size_binop (code, lhs, rhs);
3588 }
3589
a1ab4c31
AC
3590 case tcc_expression:
3591 switch (TREE_CODE_LENGTH (code))
3592 {
3593 case 1:
722356ce
EB
3594 if (code == SAVE_EXPR)
3595 return exp;
ce3da0d0
EB
3596
3597 return fold_build1 (code, type,
3598 max_size (TREE_OPERAND (exp, 0), max_p));
a1ab4c31
AC
3599
3600 case 2:
3601 if (code == COMPOUND_EXPR)
3602 return max_size (TREE_OPERAND (exp, 1), max_p);
3603
ce3da0d0
EB
3604 return fold_build2 (code, type,
3605 max_size (TREE_OPERAND (exp, 0), max_p),
3606 max_size (TREE_OPERAND (exp, 1), max_p));
a1ab4c31
AC
3607
3608 case 3:
722356ce 3609 if (code == COND_EXPR)
a1ab4c31
AC
3610 return fold_build2 (max_p ? MAX_EXPR : MIN_EXPR, type,
3611 max_size (TREE_OPERAND (exp, 1), max_p),
3612 max_size (TREE_OPERAND (exp, 2), max_p));
ce3da0d0
EB
3613
3614 default:
3615 break;
a1ab4c31
AC
3616 }
3617
3618 /* Other tree classes cannot happen. */
3619 default:
3620 break;
3621 }
3622
3623 gcc_unreachable ();
3624}
3625\f
3626/* Build a template of type TEMPLATE_TYPE from the array bounds of ARRAY_TYPE.
3627 EXPR is an expression that we can use to locate any PLACEHOLDER_EXPRs.
3628 Return a constructor for the template. */
3629
3630tree
3631build_template (tree template_type, tree array_type, tree expr)
3632{
9771b263 3633 vec<constructor_elt, va_gc> *template_elts = NULL;
a1ab4c31
AC
3634 tree bound_list = NULL_TREE;
3635 tree field;
3636
3637 while (TREE_CODE (array_type) == RECORD_TYPE
315cff15 3638 && (TYPE_PADDING_P (array_type)
a1ab4c31
AC
3639 || TYPE_JUSTIFIED_MODULAR_P (array_type)))
3640 array_type = TREE_TYPE (TYPE_FIELDS (array_type));
3641
3642 if (TREE_CODE (array_type) == ARRAY_TYPE
3643 || (TREE_CODE (array_type) == INTEGER_TYPE
3644 && TYPE_HAS_ACTUAL_BOUNDS_P (array_type)))
3645 bound_list = TYPE_ACTUAL_BOUNDS (array_type);
3646
3647 /* First make the list for a CONSTRUCTOR for the template. Go down the
3648 field list of the template instead of the type chain because this
3649 array might be an Ada array of arrays and we can't tell where the
3650 nested arrays stop being the underlying object. */
3651
3652 for (field = TYPE_FIELDS (template_type); field;
3653 (bound_list
3654 ? (bound_list = TREE_CHAIN (bound_list))
3655 : (array_type = TREE_TYPE (array_type))),
910ad8de 3656 field = DECL_CHAIN (DECL_CHAIN (field)))
a1ab4c31
AC
3657 {
3658 tree bounds, min, max;
3659
3660 /* If we have a bound list, get the bounds from there. Likewise
3661 for an ARRAY_TYPE. Otherwise, if expr is a PARM_DECL with
3662 DECL_BY_COMPONENT_PTR_P, use the bounds of the field in the template.
3663 This will give us a maximum range. */
3664 if (bound_list)
3665 bounds = TREE_VALUE (bound_list);
3666 else if (TREE_CODE (array_type) == ARRAY_TYPE)
3667 bounds = TYPE_INDEX_TYPE (TYPE_DOMAIN (array_type));
3668 else if (expr && TREE_CODE (expr) == PARM_DECL
3669 && DECL_BY_COMPONENT_PTR_P (expr))
3670 bounds = TREE_TYPE (field);
3671 else
3672 gcc_unreachable ();
3673
3674 min = convert (TREE_TYPE (field), TYPE_MIN_VALUE (bounds));
910ad8de 3675 max = convert (TREE_TYPE (DECL_CHAIN (field)), TYPE_MAX_VALUE (bounds));
a1ab4c31
AC
3676
3677 /* If either MIN or MAX involve a PLACEHOLDER_EXPR, we must
3678 substitute it from OBJECT. */
3679 min = SUBSTITUTE_PLACEHOLDER_IN_EXPR (min, expr);
3680 max = SUBSTITUTE_PLACEHOLDER_IN_EXPR (max, expr);
3681
0e228dd9 3682 CONSTRUCTOR_APPEND_ELT (template_elts, field, min);
910ad8de 3683 CONSTRUCTOR_APPEND_ELT (template_elts, DECL_CHAIN (field), max);
a1ab4c31
AC
3684 }
3685
0e228dd9 3686 return gnat_build_constructor (template_type, template_elts);
a1ab4c31
AC
3687}
3688\f
e63b36bd
EB
3689/* Return true if TYPE is suitable for the element type of a vector. */
3690
3691static bool
3692type_for_vector_element_p (tree type)
3693{
ef4bddc2 3694 machine_mode mode;
e63b36bd
EB
3695
3696 if (!INTEGRAL_TYPE_P (type)
3697 && !SCALAR_FLOAT_TYPE_P (type)
3698 && !FIXED_POINT_TYPE_P (type))
3699 return false;
3700
3701 mode = TYPE_MODE (type);
3702 if (GET_MODE_CLASS (mode) != MODE_INT
3703 && !SCALAR_FLOAT_MODE_P (mode)
3704 && !ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3705 return false;
3706
3707 return true;
3708}
3709
3710/* Return a vector type given the SIZE and the INNER_TYPE, or NULL_TREE if
3711 this is not possible. If ATTRIBUTE is non-zero, we are processing the
3712 attribute declaration and want to issue error messages on failure. */
3713
3714static tree
3715build_vector_type_for_size (tree inner_type, tree size, tree attribute)
3716{
3717 unsigned HOST_WIDE_INT size_int, inner_size_int;
3718 int nunits;
3719
3720 /* Silently punt on variable sizes. We can't make vector types for them,
3721 need to ignore them on front-end generated subtypes of unconstrained
3722 base types, and this attribute is for binding implementors, not end
3723 users, so we should never get there from legitimate explicit uses. */
3724 if (!tree_fits_uhwi_p (size))
3725 return NULL_TREE;
3726 size_int = tree_to_uhwi (size);
3727
3728 if (!type_for_vector_element_p (inner_type))
3729 {
3730 if (attribute)
3731 error ("invalid element type for attribute %qs",
3732 IDENTIFIER_POINTER (attribute));
3733 return NULL_TREE;
3734 }
3735 inner_size_int = tree_to_uhwi (TYPE_SIZE_UNIT (inner_type));
3736
3737 if (size_int % inner_size_int)
3738 {
3739 if (attribute)
3740 error ("vector size not an integral multiple of component size");
3741 return NULL_TREE;
3742 }
3743
3744 if (size_int == 0)
3745 {
3746 if (attribute)
3747 error ("zero vector size");
3748 return NULL_TREE;
3749 }
3750
3751 nunits = size_int / inner_size_int;
3752 if (nunits & (nunits - 1))
3753 {
3754 if (attribute)
3755 error ("number of components of vector not a power of two");
3756 return NULL_TREE;
3757 }
3758
3759 return build_vector_type (inner_type, nunits);
3760}
3761
3762/* Return a vector type whose representative array type is ARRAY_TYPE, or
3763 NULL_TREE if this is not possible. If ATTRIBUTE is non-zero, we are
3764 processing the attribute and want to issue error messages on failure. */
3765
3766static tree
3767build_vector_type_for_array (tree array_type, tree attribute)
3768{
3769 tree vector_type = build_vector_type_for_size (TREE_TYPE (array_type),
3770 TYPE_SIZE_UNIT (array_type),
3771 attribute);
3772 if (!vector_type)
3773 return NULL_TREE;
3774
3775 TYPE_REPRESENTATIVE_ARRAY (vector_type) = array_type;
3776 return vector_type;
3777}
3778\f
928dfa4b
EB
3779/* Build a type to be used to represent an aliased object whose nominal type
3780 is an unconstrained array. This consists of a RECORD_TYPE containing a
3781 field of TEMPLATE_TYPE and a field of OBJECT_TYPE, which is an ARRAY_TYPE.
3782 If ARRAY_TYPE is that of an unconstrained array, this is used to represent
3783 an arbitrary unconstrained object. Use NAME as the name of the record.
3784 DEBUG_INFO_P is true if we need to write debug information for the type. */
a1ab4c31
AC
3785
3786tree
928dfa4b
EB
3787build_unc_object_type (tree template_type, tree object_type, tree name,
3788 bool debug_info_p)
a1ab4c31 3789{
24d4b3d5 3790 tree decl;
a1ab4c31 3791 tree type = make_node (RECORD_TYPE);
da01bfee
EB
3792 tree template_field
3793 = create_field_decl (get_identifier ("BOUNDS"), template_type, type,
3794 NULL_TREE, NULL_TREE, 0, 1);
3795 tree array_field
3796 = create_field_decl (get_identifier ("ARRAY"), object_type, type,
3797 NULL_TREE, NULL_TREE, 0, 1);
a1ab4c31
AC
3798
3799 TYPE_NAME (type) = name;
3800 TYPE_CONTAINS_TEMPLATE_P (type) = 1;
910ad8de 3801 DECL_CHAIN (template_field) = array_field;
928dfa4b
EB
3802 finish_record_type (type, template_field, 0, true);
3803
3804 /* Declare it now since it will never be declared otherwise. This is
3805 necessary to ensure that its subtrees are properly marked. */
24d4b3d5
AC
3806 decl = create_type_decl (name, type, true, debug_info_p, Empty);
3807
3808 /* template_type will not be used elsewhere than here, so to keep the debug
3809 info clean and in order to avoid scoping issues, make decl its
3810 context. */
3811 gnat_set_type_context (template_type, decl);
a1ab4c31
AC
3812
3813 return type;
3814}
3815
3816/* Same, taking a thin or fat pointer type instead of a template type. */
3817
3818tree
3819build_unc_object_type_from_ptr (tree thin_fat_ptr_type, tree object_type,
928dfa4b 3820 tree name, bool debug_info_p)
a1ab4c31
AC
3821{
3822 tree template_type;
3823
315cff15 3824 gcc_assert (TYPE_IS_FAT_OR_THIN_POINTER_P (thin_fat_ptr_type));
a1ab4c31
AC
3825
3826 template_type
315cff15 3827 = (TYPE_IS_FAT_POINTER_P (thin_fat_ptr_type)
910ad8de 3828 ? TREE_TYPE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (thin_fat_ptr_type))))
a1ab4c31 3829 : TREE_TYPE (TYPE_FIELDS (TREE_TYPE (thin_fat_ptr_type))));
928dfa4b
EB
3830
3831 return
3832 build_unc_object_type (template_type, object_type, name, debug_info_p);
a1ab4c31 3833}
a1ab4c31 3834\f
229077b0
EB
3835/* Update anything previously pointing to OLD_TYPE to point to NEW_TYPE.
3836 In the normal case this is just two adjustments, but we have more to
3837 do if NEW_TYPE is an UNCONSTRAINED_ARRAY_TYPE. */
a1ab4c31
AC
3838
3839void
3840update_pointer_to (tree old_type, tree new_type)
3841{
3842 tree ptr = TYPE_POINTER_TO (old_type);
3843 tree ref = TYPE_REFERENCE_TO (old_type);
aeecf17c 3844 tree t;
a1ab4c31
AC
3845
3846 /* If this is the main variant, process all the other variants first. */
3847 if (TYPE_MAIN_VARIANT (old_type) == old_type)
aeecf17c
EB
3848 for (t = TYPE_NEXT_VARIANT (old_type); t; t = TYPE_NEXT_VARIANT (t))
3849 update_pointer_to (t, new_type);
a1ab4c31 3850
229077b0 3851 /* If no pointers and no references, we are done. */
a1ab4c31
AC
3852 if (!ptr && !ref)
3853 return;
3854
3855 /* Merge the old type qualifiers in the new type.
3856
3857 Each old variant has qualifiers for specific reasons, and the new
229077b0 3858 designated type as well. Each set of qualifiers represents useful
a1ab4c31
AC
3859 information grabbed at some point, and merging the two simply unifies
3860 these inputs into the final type description.
3861
3862 Consider for instance a volatile type frozen after an access to constant
229077b0
EB
3863 type designating it; after the designated type's freeze, we get here with
3864 a volatile NEW_TYPE and a dummy OLD_TYPE with a readonly variant, created
3865 when the access type was processed. We will make a volatile and readonly
a1ab4c31
AC
3866 designated type, because that's what it really is.
3867
229077b0
EB
3868 We might also get here for a non-dummy OLD_TYPE variant with different
3869 qualifiers than those of NEW_TYPE, for instance in some cases of pointers
a1ab4c31 3870 to private record type elaboration (see the comments around the call to
229077b0
EB
3871 this routine in gnat_to_gnu_entity <E_Access_Type>). We have to merge
3872 the qualifiers in those cases too, to avoid accidentally discarding the
3873 initial set, and will often end up with OLD_TYPE == NEW_TYPE then. */
3874 new_type
3875 = build_qualified_type (new_type,
3876 TYPE_QUALS (old_type) | TYPE_QUALS (new_type));
3877
3878 /* If old type and new type are identical, there is nothing to do. */
a1ab4c31
AC
3879 if (old_type == new_type)
3880 return;
3881
3882 /* Otherwise, first handle the simple case. */
3883 if (TREE_CODE (new_type) != UNCONSTRAINED_ARRAY_TYPE)
3884 {
aeecf17c
EB
3885 tree new_ptr, new_ref;
3886
3887 /* If pointer or reference already points to new type, nothing to do.
3888 This can happen as update_pointer_to can be invoked multiple times
3889 on the same couple of types because of the type variants. */
3890 if ((ptr && TREE_TYPE (ptr) == new_type)
3891 || (ref && TREE_TYPE (ref) == new_type))
3892 return;
3893
3894 /* Chain PTR and its variants at the end. */
3895 new_ptr = TYPE_POINTER_TO (new_type);
3896 if (new_ptr)
3897 {
3898 while (TYPE_NEXT_PTR_TO (new_ptr))
3899 new_ptr = TYPE_NEXT_PTR_TO (new_ptr);
3900 TYPE_NEXT_PTR_TO (new_ptr) = ptr;
3901 }
3902 else
3903 TYPE_POINTER_TO (new_type) = ptr;
a1ab4c31 3904
aeecf17c 3905 /* Now adjust them. */
a1ab4c31 3906 for (; ptr; ptr = TYPE_NEXT_PTR_TO (ptr))
aeecf17c 3907 for (t = TYPE_MAIN_VARIANT (ptr); t; t = TYPE_NEXT_VARIANT (t))
50179d58
EB
3908 {
3909 TREE_TYPE (t) = new_type;
3910 if (TYPE_NULL_BOUNDS (t))
3911 TREE_TYPE (TREE_OPERAND (TYPE_NULL_BOUNDS (t), 0)) = new_type;
3912 }
de9528f0 3913
aeecf17c
EB
3914 /* Chain REF and its variants at the end. */
3915 new_ref = TYPE_REFERENCE_TO (new_type);
3916 if (new_ref)
3917 {
3918 while (TYPE_NEXT_REF_TO (new_ref))
3919 new_ref = TYPE_NEXT_REF_TO (new_ref);
3920 TYPE_NEXT_REF_TO (new_ref) = ref;
3921 }
3922 else
3923 TYPE_REFERENCE_TO (new_type) = ref;
3924
3925 /* Now adjust them. */
a1ab4c31 3926 for (; ref; ref = TYPE_NEXT_REF_TO (ref))
aeecf17c
EB
3927 for (t = TYPE_MAIN_VARIANT (ref); t; t = TYPE_NEXT_VARIANT (t))
3928 TREE_TYPE (t) = new_type;
de9528f0
EB
3929
3930 TYPE_POINTER_TO (old_type) = NULL_TREE;
3bd6ca3f 3931 TYPE_REFERENCE_TO (old_type) = NULL_TREE;
a1ab4c31
AC
3932 }
3933
aeecf17c
EB
3934 /* Now deal with the unconstrained array case. In this case the pointer
3935 is actually a record where both fields are pointers to dummy nodes.
e3edbd56
EB
3936 Turn them into pointers to the correct types using update_pointer_to.
3937 Likewise for the pointer to the object record (thin pointer). */
a1ab4c31
AC
3938 else
3939 {
e3edbd56 3940 tree new_ptr = TYPE_POINTER_TO (new_type);
aeecf17c
EB
3941
3942 gcc_assert (TYPE_IS_FAT_POINTER_P (ptr));
3943
e3edbd56 3944 /* If PTR already points to NEW_TYPE, nothing to do. This can happen
aeecf17c
EB
3945 since update_pointer_to can be invoked multiple times on the same
3946 couple of types because of the type variants. */
3947 if (TYPE_UNCONSTRAINED_ARRAY (ptr) == new_type)
3948 return;
3949
a1ab4c31 3950 update_pointer_to
e3edbd56
EB
3951 (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (ptr))),
3952 TREE_TYPE (TREE_TYPE (TYPE_FIELDS (new_ptr))));
a1ab4c31 3953
a1ab4c31 3954 update_pointer_to
e3edbd56
EB
3955 (TREE_TYPE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (ptr)))),
3956 TREE_TYPE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (new_ptr)))));
aeecf17c 3957
e3edbd56
EB
3958 update_pointer_to (TYPE_OBJECT_RECORD_TYPE (old_type),
3959 TYPE_OBJECT_RECORD_TYPE (new_type));
a1ab4c31 3960
e3edbd56 3961 TYPE_POINTER_TO (old_type) = NULL_TREE;
1e55d29a 3962 TYPE_REFERENCE_TO (old_type) = NULL_TREE;
a1ab4c31
AC
3963 }
3964}
3965\f
8df2e902
EB
3966/* Convert EXPR, a pointer to a constrained array, into a pointer to an
3967 unconstrained one. This involves making or finding a template. */
a1ab4c31
AC
3968
3969static tree
3970convert_to_fat_pointer (tree type, tree expr)
3971{
910ad8de 3972 tree template_type = TREE_TYPE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type))));
8df2e902 3973 tree p_array_type = TREE_TYPE (TYPE_FIELDS (type));
a1ab4c31 3974 tree etype = TREE_TYPE (expr);
88293f03 3975 tree template_addr;
9771b263
DN
3976 vec<constructor_elt, va_gc> *v;
3977 vec_alloc (v, 2);
a1ab4c31 3978
50179d58
EB
3979 /* If EXPR is null, make a fat pointer that contains a null pointer to the
3980 array (compare_fat_pointers ensures that this is the full discriminant)
3981 and a valid pointer to the bounds. This latter property is necessary
3982 since the compiler can hoist the load of the bounds done through it. */
a1ab4c31 3983 if (integer_zerop (expr))
0e228dd9 3984 {
50179d58
EB
3985 tree ptr_template_type = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type)));
3986 tree null_bounds, t;
3987
3988 if (TYPE_NULL_BOUNDS (ptr_template_type))
3989 null_bounds = TYPE_NULL_BOUNDS (ptr_template_type);
3990 else
3991 {
3992 /* The template type can still be dummy at this point so we build an
3993 empty constructor. The middle-end will fill it in with zeros. */
90b4c164 3994 t = build_constructor (template_type, NULL);
50179d58
EB
3995 TREE_CONSTANT (t) = TREE_STATIC (t) = 1;
3996 null_bounds = build_unary_op (ADDR_EXPR, NULL_TREE, t);
3997 SET_TYPE_NULL_BOUNDS (ptr_template_type, null_bounds);
3998 }
3999
0e228dd9 4000 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type),
50179d58
EB
4001 fold_convert (p_array_type, null_pointer_node));
4002 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (TYPE_FIELDS (type)), null_bounds);
4003 t = build_constructor (type, v);
4004 /* Do not set TREE_CONSTANT so as to force T to static memory. */
4005 TREE_CONSTANT (t) = 0;
4006 TREE_STATIC (t) = 1;
4007
4008 return t;
0e228dd9 4009 }
a1ab4c31 4010
0d7de0e1
EB
4011 /* If EXPR is a thin pointer, make template and data from the record. */
4012 if (TYPE_IS_THIN_POINTER_P (etype))
a1ab4c31 4013 {
0d7de0e1 4014 tree field = TYPE_FIELDS (TREE_TYPE (etype));
a1ab4c31 4015
7d7a1fe8 4016 expr = gnat_protect_expr (expr);
88293f03
EB
4017
4018 /* If we have a TYPE_UNCONSTRAINED_ARRAY attached to the RECORD_TYPE,
4019 the thin pointer value has been shifted so we shift it back to get
4020 the template address. */
4021 if (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (etype)))
2b45154d 4022 {
88293f03
EB
4023 template_addr
4024 = build_binary_op (POINTER_PLUS_EXPR, etype, expr,
4025 fold_build1 (NEGATE_EXPR, sizetype,
4026 byte_position
4027 (DECL_CHAIN (field))));
4028 template_addr
4029 = fold_convert (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type))),
4030 template_addr);
2b45154d 4031 }
a1ab4c31 4032
88293f03
EB
4033 /* Otherwise we explicitly take the address of the fields. */
4034 else
4035 {
4036 expr = build_unary_op (INDIRECT_REF, NULL_TREE, expr);
4037 template_addr
4038 = build_unary_op (ADDR_EXPR, NULL_TREE,
64235766 4039 build_component_ref (expr, field, false));
88293f03 4040 expr = build_unary_op (ADDR_EXPR, NULL_TREE,
64235766 4041 build_component_ref (expr, DECL_CHAIN (field),
88293f03
EB
4042 false));
4043 }
a1ab4c31 4044 }
8df2e902
EB
4045
4046 /* Otherwise, build the constructor for the template. */
a1ab4c31 4047 else
88293f03
EB
4048 template_addr
4049 = build_unary_op (ADDR_EXPR, NULL_TREE,
4050 build_template (template_type, TREE_TYPE (etype),
4051 expr));
a1ab4c31 4052
8df2e902 4053 /* The final result is a constructor for the fat pointer.
a1ab4c31 4054
8df2e902
EB
4055 If EXPR is an argument of a foreign convention subprogram, the type it
4056 points to is directly the component type. In this case, the expression
a1ab4c31 4057 type may not match the corresponding FIELD_DECL type at this point, so we
8df2e902 4058 call "convert" here to fix that up if necessary. This type consistency is
a1ab4c31 4059 required, for instance because it ensures that possible later folding of
8df2e902 4060 COMPONENT_REFs against this constructor always yields something of the
a1ab4c31
AC
4061 same type as the initial reference.
4062
8df2e902
EB
4063 Note that the call to "build_template" above is still fine because it
4064 will only refer to the provided TEMPLATE_TYPE in this case. */
88293f03
EB
4065 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type), convert (p_array_type, expr));
4066 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (TYPE_FIELDS (type)), template_addr);
0e228dd9 4067 return gnat_build_constructor (type, v);
a1ab4c31
AC
4068}
4069\f
a1ab4c31
AC
4070/* Create an expression whose value is that of EXPR,
4071 converted to type TYPE. The TREE_TYPE of the value
4072 is always TYPE. This function implements all reasonable
4073 conversions; callers should filter out those that are
4074 not permitted by the language being compiled. */
4075
4076tree
4077convert (tree type, tree expr)
4078{
a1ab4c31
AC
4079 tree etype = TREE_TYPE (expr);
4080 enum tree_code ecode = TREE_CODE (etype);
c34f3839 4081 enum tree_code code = TREE_CODE (type);
a1ab4c31 4082
c34f3839
EB
4083 /* If the expression is already of the right type, we are done. */
4084 if (etype == type)
a1ab4c31
AC
4085 return expr;
4086
4087 /* If both input and output have padding and are of variable size, do this
4088 as an unchecked conversion. Likewise if one is a mere variant of the
4089 other, so we avoid a pointless unpad/repad sequence. */
4090 else if (code == RECORD_TYPE && ecode == RECORD_TYPE
315cff15 4091 && TYPE_PADDING_P (type) && TYPE_PADDING_P (etype)
a1ab4c31
AC
4092 && (!TREE_CONSTANT (TYPE_SIZE (type))
4093 || !TREE_CONSTANT (TYPE_SIZE (etype))
842d4ee2 4094 || TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (etype)
a1ab4c31
AC
4095 || TYPE_NAME (TREE_TYPE (TYPE_FIELDS (type)))
4096 == TYPE_NAME (TREE_TYPE (TYPE_FIELDS (etype)))))
4097 ;
4098
f88facfe
EB
4099 /* If the output type has padding, convert to the inner type and make a
4100 constructor to build the record, unless a variable size is involved. */
315cff15 4101 else if (code == RECORD_TYPE && TYPE_PADDING_P (type))
a1ab4c31 4102 {
9771b263 4103 vec<constructor_elt, va_gc> *v;
0e228dd9 4104
a1ab4c31
AC
4105 /* If we previously converted from another type and our type is
4106 of variable size, remove the conversion to avoid the need for
f88facfe 4107 variable-sized temporaries. Likewise for a conversion between
a1ab4c31
AC
4108 original and packable version. */
4109 if (TREE_CODE (expr) == VIEW_CONVERT_EXPR
4110 && (!TREE_CONSTANT (TYPE_SIZE (type))
4111 || (ecode == RECORD_TYPE
4112 && TYPE_NAME (etype)
4113 == TYPE_NAME (TREE_TYPE (TREE_OPERAND (expr, 0))))))
4114 expr = TREE_OPERAND (expr, 0);
4115
4116 /* If we are just removing the padding from expr, convert the original
4117 object if we have variable size in order to avoid the need for some
f88facfe 4118 variable-sized temporaries. Likewise if the padding is a variant
a1ab4c31
AC
4119 of the other, so we avoid a pointless unpad/repad sequence. */
4120 if (TREE_CODE (expr) == COMPONENT_REF
a1ab4c31
AC
4121 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (expr, 0)))
4122 && (!TREE_CONSTANT (TYPE_SIZE (type))
842d4ee2
EB
4123 || TYPE_MAIN_VARIANT (type)
4124 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (expr, 0)))
a1ab4c31
AC
4125 || (ecode == RECORD_TYPE
4126 && TYPE_NAME (etype)
4127 == TYPE_NAME (TREE_TYPE (TYPE_FIELDS (type))))))
4128 return convert (type, TREE_OPERAND (expr, 0));
4129
431cfac1
EB
4130 /* If the inner type is of self-referential size and the expression type
4131 is a record, do this as an unchecked conversion. But first pad the
4132 expression if possible to have the same size on both sides. */
c34f3839 4133 if (ecode == RECORD_TYPE
f88facfe 4134 && CONTAINS_PLACEHOLDER_P (DECL_SIZE (TYPE_FIELDS (type))))
431cfac1 4135 {
980a0501 4136 if (TREE_CODE (TYPE_SIZE (etype)) == INTEGER_CST)
431cfac1 4137 expr = convert (maybe_pad_type (etype, TYPE_SIZE (type), 0, Empty,
980a0501
EB
4138 false, false, false, true),
4139 expr);
431cfac1
EB
4140 return unchecked_convert (type, expr, false);
4141 }
a1ab4c31 4142
f88facfe
EB
4143 /* If we are converting between array types with variable size, do the
4144 final conversion as an unchecked conversion, again to avoid the need
4145 for some variable-sized temporaries. If valid, this conversion is
4146 very likely purely technical and without real effects. */
c34f3839 4147 if (ecode == ARRAY_TYPE
f88facfe
EB
4148 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (type))) == ARRAY_TYPE
4149 && !TREE_CONSTANT (TYPE_SIZE (etype))
4150 && !TREE_CONSTANT (TYPE_SIZE (type)))
4151 return unchecked_convert (type,
4152 convert (TREE_TYPE (TYPE_FIELDS (type)),
4153 expr),
4154 false);
4155
9771b263 4156 vec_alloc (v, 1);
0e228dd9
NF
4157 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type),
4158 convert (TREE_TYPE (TYPE_FIELDS (type)), expr));
4159 return gnat_build_constructor (type, v);
a1ab4c31
AC
4160 }
4161
4162 /* If the input type has padding, remove it and convert to the output type.
4163 The conditions ordering is arranged to ensure that the output type is not
4164 a padding type here, as it is not clear whether the conversion would
4165 always be correct if this was to happen. */
315cff15 4166 else if (ecode == RECORD_TYPE && TYPE_PADDING_P (etype))
a1ab4c31
AC
4167 {
4168 tree unpadded;
4169
4170 /* If we have just converted to this padded type, just get the
4171 inner expression. */
2117b9bb
EB
4172 if (TREE_CODE (expr) == CONSTRUCTOR)
4173 unpadded = CONSTRUCTOR_ELT (expr, 0)->value;
a1ab4c31
AC
4174
4175 /* Otherwise, build an explicit component reference. */
4176 else
64235766 4177 unpadded = build_component_ref (expr, TYPE_FIELDS (etype), false);
a1ab4c31
AC
4178
4179 return convert (type, unpadded);
4180 }
4181
4182 /* If the input is a biased type, adjust first. */
4183 if (ecode == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (etype))
4184 return convert (type, fold_build2 (PLUS_EXPR, TREE_TYPE (etype),
1eb58520 4185 fold_convert (TREE_TYPE (etype), expr),
a1ab4c31 4186 fold_convert (TREE_TYPE (etype),
1eb58520 4187 TYPE_MIN_VALUE (etype))));
a1ab4c31
AC
4188
4189 /* If the input is a justified modular type, we need to extract the actual
4190 object before converting it to any other type with the exceptions of an
4191 unconstrained array or of a mere type variant. It is useful to avoid the
4192 extraction and conversion in the type variant case because it could end
4193 up replacing a VAR_DECL expr by a constructor and we might be about the
4194 take the address of the result. */
4195 if (ecode == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (etype)
4196 && code != UNCONSTRAINED_ARRAY_TYPE
4197 && TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (etype))
64235766
EB
4198 return
4199 convert (type, build_component_ref (expr, TYPE_FIELDS (etype), false));
a1ab4c31
AC
4200
4201 /* If converting to a type that contains a template, convert to the data
4202 type and then build the template. */
4203 if (code == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (type))
4204 {
910ad8de 4205 tree obj_type = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type)));
9771b263
DN
4206 vec<constructor_elt, va_gc> *v;
4207 vec_alloc (v, 2);
a1ab4c31
AC
4208
4209 /* If the source already has a template, get a reference to the
4210 associated array only, as we are going to rebuild a template
4211 for the target type anyway. */
4212 expr = maybe_unconstrained_array (expr);
4213
0e228dd9
NF
4214 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type),
4215 build_template (TREE_TYPE (TYPE_FIELDS (type)),
4216 obj_type, NULL_TREE));
73a1a803
EB
4217 if (expr)
4218 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (TYPE_FIELDS (type)),
4219 convert (obj_type, expr));
0e228dd9 4220 return gnat_build_constructor (type, v);
a1ab4c31
AC
4221 }
4222
a1c7d797 4223 /* There are some cases of expressions that we process specially. */
a1ab4c31
AC
4224 switch (TREE_CODE (expr))
4225 {
4226 case ERROR_MARK:
4227 return expr;
4228
4229 case NULL_EXPR:
4230 /* Just set its type here. For TRANSFORM_EXPR, we will do the actual
4231 conversion in gnat_expand_expr. NULL_EXPR does not represent
4232 and actual value, so no conversion is needed. */
4233 expr = copy_node (expr);
4234 TREE_TYPE (expr) = type;
4235 return expr;
4236
4237 case STRING_CST:
4238 /* If we are converting a STRING_CST to another constrained array type,
4239 just make a new one in the proper type. */
4240 if (code == ecode && AGGREGATE_TYPE_P (etype)
4241 && !(TREE_CODE (TYPE_SIZE (etype)) == INTEGER_CST
4242 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
4243 {
4244 expr = copy_node (expr);
4245 TREE_TYPE (expr) = type;
4246 return expr;
4247 }
4248 break;
4249
7948ae37 4250 case VECTOR_CST:
44e9e3ec 4251 /* If we are converting a VECTOR_CST to a mere type variant, just make
7948ae37
OH
4252 a new one in the proper type. */
4253 if (code == ecode && gnat_types_compatible_p (type, etype))
4254 {
4255 expr = copy_node (expr);
4256 TREE_TYPE (expr) = type;
4257 return expr;
4258 }
4259
a1ab4c31 4260 case CONSTRUCTOR:
44e9e3ec
EB
4261 /* If we are converting a CONSTRUCTOR to a mere type variant, or to
4262 another padding type around the same type, just make a new one in
4263 the proper type. */
4264 if (code == ecode
4265 && (gnat_types_compatible_p (type, etype)
4266 || (code == RECORD_TYPE
4267 && TYPE_PADDING_P (type) && TYPE_PADDING_P (etype)
4268 && TREE_TYPE (TYPE_FIELDS (type))
4269 == TREE_TYPE (TYPE_FIELDS (etype)))))
a1ab4c31
AC
4270 {
4271 expr = copy_node (expr);
4272 TREE_TYPE (expr) = type;
9771b263 4273 CONSTRUCTOR_ELTS (expr) = vec_safe_copy (CONSTRUCTOR_ELTS (expr));
a1ab4c31
AC
4274 return expr;
4275 }
4276
cb3d597d
EB
4277 /* Likewise for a conversion between original and packable version, or
4278 conversion between types of the same size and with the same list of
4279 fields, but we have to work harder to preserve type consistency. */
a1ab4c31
AC
4280 if (code == ecode
4281 && code == RECORD_TYPE
cb3d597d
EB
4282 && (TYPE_NAME (type) == TYPE_NAME (etype)
4283 || tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (etype))))
4284
a1ab4c31 4285 {
9771b263
DN
4286 vec<constructor_elt, va_gc> *e = CONSTRUCTOR_ELTS (expr);
4287 unsigned HOST_WIDE_INT len = vec_safe_length (e);
4288 vec<constructor_elt, va_gc> *v;
4289 vec_alloc (v, len);
a1ab4c31
AC
4290 tree efield = TYPE_FIELDS (etype), field = TYPE_FIELDS (type);
4291 unsigned HOST_WIDE_INT idx;
4292 tree index, value;
4293
db868e1e
OH
4294 /* Whether we need to clear TREE_CONSTANT et al. on the output
4295 constructor when we convert in place. */
4296 bool clear_constant = false;
4297
a1ab4c31
AC
4298 FOR_EACH_CONSTRUCTOR_ELT(e, idx, index, value)
4299 {
44e9e3ec
EB
4300 /* Skip the missing fields in the CONSTRUCTOR. */
4301 while (efield && field && !SAME_FIELD_P (efield, index))
4302 {
4303 efield = DECL_CHAIN (efield);
4304 field = DECL_CHAIN (field);
4305 }
cb3d597d 4306 /* The field must be the same. */
44e9e3ec 4307 if (!(efield && field && SAME_FIELD_P (efield, field)))
a1ab4c31 4308 break;
44e9e3ec
EB
4309 constructor_elt elt
4310 = {field, convert (TREE_TYPE (field), value)};
9771b263 4311 v->quick_push (elt);
db868e1e
OH
4312
4313 /* If packing has made this field a bitfield and the input
4314 value couldn't be emitted statically any more, we need to
4315 clear TREE_CONSTANT on our output. */
ced57283
EB
4316 if (!clear_constant
4317 && TREE_CONSTANT (expr)
db868e1e
OH
4318 && !CONSTRUCTOR_BITFIELD_P (efield)
4319 && CONSTRUCTOR_BITFIELD_P (field)
4320 && !initializer_constant_valid_for_bitfield_p (value))
4321 clear_constant = true;
4322
910ad8de
NF
4323 efield = DECL_CHAIN (efield);
4324 field = DECL_CHAIN (field);
a1ab4c31
AC
4325 }
4326
db868e1e
OH
4327 /* If we have been able to match and convert all the input fields
4328 to their output type, convert in place now. We'll fallback to a
4329 view conversion downstream otherwise. */
a1ab4c31
AC
4330 if (idx == len)
4331 {
4332 expr = copy_node (expr);
4333 TREE_TYPE (expr) = type;
4334 CONSTRUCTOR_ELTS (expr) = v;
db868e1e 4335 if (clear_constant)
ced57283 4336 TREE_CONSTANT (expr) = TREE_STATIC (expr) = 0;
a1ab4c31
AC
4337 return expr;
4338 }
4339 }
7948ae37
OH
4340
4341 /* Likewise for a conversion between array type and vector type with a
4342 compatible representative array. */
4343 else if (code == VECTOR_TYPE
4344 && ecode == ARRAY_TYPE
4345 && gnat_types_compatible_p (TYPE_REPRESENTATIVE_ARRAY (type),
4346 etype))
4347 {
9771b263
DN
4348 vec<constructor_elt, va_gc> *e = CONSTRUCTOR_ELTS (expr);
4349 unsigned HOST_WIDE_INT len = vec_safe_length (e);
4350 vec<constructor_elt, va_gc> *v;
7948ae37
OH
4351 unsigned HOST_WIDE_INT ix;
4352 tree value;
4353
4354 /* Build a VECTOR_CST from a *constant* array constructor. */
4355 if (TREE_CONSTANT (expr))
4356 {
4357 bool constant_p = true;
4358
4359 /* Iterate through elements and check if all constructor
4360 elements are *_CSTs. */
4361 FOR_EACH_CONSTRUCTOR_VALUE (e, ix, value)
4362 if (!CONSTANT_CLASS_P (value))
4363 {
4364 constant_p = false;
4365 break;
4366 }
4367
4368 if (constant_p)
4369 return build_vector_from_ctor (type,
4370 CONSTRUCTOR_ELTS (expr));
4371 }
4372
4373 /* Otherwise, build a regular vector constructor. */
9771b263 4374 vec_alloc (v, len);
7948ae37
OH
4375 FOR_EACH_CONSTRUCTOR_VALUE (e, ix, value)
4376 {
f32682ca 4377 constructor_elt elt = {NULL_TREE, value};
9771b263 4378 v->quick_push (elt);
7948ae37
OH
4379 }
4380 expr = copy_node (expr);
4381 TREE_TYPE (expr) = type;
4382 CONSTRUCTOR_ELTS (expr) = v;
4383 return expr;
4384 }
a1ab4c31
AC
4385 break;
4386
4387 case UNCONSTRAINED_ARRAY_REF:
a1c7d797
EB
4388 /* First retrieve the underlying array. */
4389 expr = maybe_unconstrained_array (expr);
4390 etype = TREE_TYPE (expr);
4391 ecode = TREE_CODE (etype);
4392 break;
a1ab4c31
AC
4393
4394 case VIEW_CONVERT_EXPR:
4395 {
4396 /* GCC 4.x is very sensitive to type consistency overall, and view
4397 conversions thus are very frequent. Even though just "convert"ing
4398 the inner operand to the output type is fine in most cases, it
4399 might expose unexpected input/output type mismatches in special
4400 circumstances so we avoid such recursive calls when we can. */
4401 tree op0 = TREE_OPERAND (expr, 0);
4402
4403 /* If we are converting back to the original type, we can just
4404 lift the input conversion. This is a common occurrence with
4405 switches back-and-forth amongst type variants. */
4406 if (type == TREE_TYPE (op0))
4407 return op0;
4408
7948ae37
OH
4409 /* Otherwise, if we're converting between two aggregate or vector
4410 types, we might be allowed to substitute the VIEW_CONVERT_EXPR
4411 target type in place or to just convert the inner expression. */
4412 if ((AGGREGATE_TYPE_P (type) && AGGREGATE_TYPE_P (etype))
4413 || (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (etype)))
a1ab4c31
AC
4414 {
4415 /* If we are converting between mere variants, we can just
4416 substitute the VIEW_CONVERT_EXPR in place. */
4417 if (gnat_types_compatible_p (type, etype))
4418 return build1 (VIEW_CONVERT_EXPR, type, op0);
4419
4420 /* Otherwise, we may just bypass the input view conversion unless
4421 one of the types is a fat pointer, which is handled by
4422 specialized code below which relies on exact type matching. */
315cff15
EB
4423 else if (!TYPE_IS_FAT_POINTER_P (type)
4424 && !TYPE_IS_FAT_POINTER_P (etype))
a1ab4c31
AC
4425 return convert (type, op0);
4426 }
ad1d36ba
EB
4427
4428 break;
a1ab4c31 4429 }
a1ab4c31 4430
a1ab4c31
AC
4431 default:
4432 break;
4433 }
4434
4435 /* Check for converting to a pointer to an unconstrained array. */
315cff15 4436 if (TYPE_IS_FAT_POINTER_P (type) && !TYPE_IS_FAT_POINTER_P (etype))
a1ab4c31
AC
4437 return convert_to_fat_pointer (type, expr);
4438
7948ae37
OH
4439 /* If we are converting between two aggregate or vector types that are mere
4440 variants, just make a VIEW_CONVERT_EXPR. Likewise when we are converting
4441 to a vector type from its representative array type. */
4442 else if ((code == ecode
4443 && (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
4444 && gnat_types_compatible_p (type, etype))
4445 || (code == VECTOR_TYPE
4446 && ecode == ARRAY_TYPE
4447 && gnat_types_compatible_p (TYPE_REPRESENTATIVE_ARRAY (type),
4448 etype)))
a1ab4c31
AC
4449 return build1 (VIEW_CONVERT_EXPR, type, expr);
4450
76af763d
EB
4451 /* If we are converting between tagged types, try to upcast properly. */
4452 else if (ecode == RECORD_TYPE && code == RECORD_TYPE
4453 && TYPE_ALIGN_OK (etype) && TYPE_ALIGN_OK (type))
4454 {
4455 tree child_etype = etype;
4456 do {
4457 tree field = TYPE_FIELDS (child_etype);
4458 if (DECL_NAME (field) == parent_name_id && TREE_TYPE (field) == type)
64235766 4459 return build_component_ref (expr, field, false);
76af763d
EB
4460 child_etype = TREE_TYPE (field);
4461 } while (TREE_CODE (child_etype) == RECORD_TYPE);
4462 }
4463
bb1f7929
EB
4464 /* If we are converting from a smaller form of record type back to it, just
4465 make a VIEW_CONVERT_EXPR. But first pad the expression to have the same
4466 size on both sides. */
4467 else if (ecode == RECORD_TYPE && code == RECORD_TYPE
4468 && smaller_form_type_p (etype, type))
4469 {
4470 expr = convert (maybe_pad_type (etype, TYPE_SIZE (type), 0, Empty,
4471 false, false, false, true),
4472 expr);
4473 return build1 (VIEW_CONVERT_EXPR, type, expr);
4474 }
4475
a1ab4c31 4476 /* In all other cases of related types, make a NOP_EXPR. */
86060344 4477 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (etype))
a1ab4c31
AC
4478 return fold_convert (type, expr);
4479
4480 switch (code)
4481 {
4482 case VOID_TYPE:
4483 return fold_build1 (CONVERT_EXPR, type, expr);
4484
a1ab4c31
AC
4485 case INTEGER_TYPE:
4486 if (TYPE_HAS_ACTUAL_BOUNDS_P (type)
4487 && (ecode == ARRAY_TYPE || ecode == UNCONSTRAINED_ARRAY_TYPE
4488 || (ecode == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (etype))))
4489 return unchecked_convert (type, expr, false);
4490 else if (TYPE_BIASED_REPRESENTATION_P (type))
4491 return fold_convert (type,
4492 fold_build2 (MINUS_EXPR, TREE_TYPE (type),
4493 convert (TREE_TYPE (type), expr),
1eb58520
AC
4494 convert (TREE_TYPE (type),
4495 TYPE_MIN_VALUE (type))));
a1ab4c31
AC
4496
4497 /* ... fall through ... */
4498
4499 case ENUMERAL_TYPE:
01ddebf2 4500 case BOOLEAN_TYPE:
a1ab4c31
AC
4501 /* If we are converting an additive expression to an integer type
4502 with lower precision, be wary of the optimization that can be
4503 applied by convert_to_integer. There are 2 problematic cases:
4504 - if the first operand was originally of a biased type,
4505 because we could be recursively called to convert it
4506 to an intermediate type and thus rematerialize the
4507 additive operator endlessly,
4508 - if the expression contains a placeholder, because an
4509 intermediate conversion that changes the sign could
4510 be inserted and thus introduce an artificial overflow
4511 at compile time when the placeholder is substituted. */
4512 if (code == INTEGER_TYPE
4513 && ecode == INTEGER_TYPE
4514 && TYPE_PRECISION (type) < TYPE_PRECISION (etype)
4515 && (TREE_CODE (expr) == PLUS_EXPR || TREE_CODE (expr) == MINUS_EXPR))
4516 {
4517 tree op0 = get_unwidened (TREE_OPERAND (expr, 0), type);
4518
4519 if ((TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
4520 && TYPE_BIASED_REPRESENTATION_P (TREE_TYPE (op0)))
4521 || CONTAINS_PLACEHOLDER_P (expr))
4522 return build1 (NOP_EXPR, type, expr);
4523 }
4524
4525 return fold (convert_to_integer (type, expr));
4526
4527 case POINTER_TYPE:
4528 case REFERENCE_TYPE:
0d7de0e1 4529 /* If converting between two thin pointers, adjust if needed to account
2b45154d
EB
4530 for differing offsets from the base pointer, depending on whether
4531 there is a TYPE_UNCONSTRAINED_ARRAY attached to the record type. */
315cff15 4532 if (TYPE_IS_THIN_POINTER_P (etype) && TYPE_IS_THIN_POINTER_P (type))
a1ab4c31 4533 {
2b45154d 4534 tree etype_pos
7c775aca 4535 = TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (etype))
2b45154d
EB
4536 ? byte_position (DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (etype))))
4537 : size_zero_node;
4538 tree type_pos
7c775aca 4539 = TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type))
2b45154d
EB
4540 ? byte_position (DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (type))))
4541 : size_zero_node;
4542 tree byte_diff = size_diffop (type_pos, etype_pos);
0d7de0e1 4543
a1ab4c31 4544 expr = build1 (NOP_EXPR, type, expr);
a1ab4c31
AC
4545 if (integer_zerop (byte_diff))
4546 return expr;
4547
4548 return build_binary_op (POINTER_PLUS_EXPR, type, expr,
0d7de0e1 4549 fold_convert (sizetype, byte_diff));
a1ab4c31
AC
4550 }
4551
0d7de0e1
EB
4552 /* If converting fat pointer to normal or thin pointer, get the pointer
4553 to the array and then convert it. */
4554 if (TYPE_IS_FAT_POINTER_P (etype))
64235766 4555 expr = build_component_ref (expr, TYPE_FIELDS (etype), false);
a1ab4c31
AC
4556
4557 return fold (convert_to_pointer (type, expr));
4558
4559 case REAL_TYPE:
4560 return fold (convert_to_real (type, expr));
4561
4562 case RECORD_TYPE:
4563 if (TYPE_JUSTIFIED_MODULAR_P (type) && !AGGREGATE_TYPE_P (etype))
0e228dd9 4564 {
9771b263
DN
4565 vec<constructor_elt, va_gc> *v;
4566 vec_alloc (v, 1);
0e228dd9
NF
4567
4568 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type),
4569 convert (TREE_TYPE (TYPE_FIELDS (type)),
4570 expr));
4571 return gnat_build_constructor (type, v);
4572 }
a1ab4c31
AC
4573
4574 /* ... fall through ... */
4575
4576 case ARRAY_TYPE:
4577 /* In these cases, assume the front-end has validated the conversion.
4578 If the conversion is valid, it will be a bit-wise conversion, so
4579 it can be viewed as an unchecked conversion. */
4580 return unchecked_convert (type, expr, false);
4581
4582 case UNION_TYPE:
4583 /* This is a either a conversion between a tagged type and some
4584 subtype, which we have to mark as a UNION_TYPE because of
4585 overlapping fields or a conversion of an Unchecked_Union. */
4586 return unchecked_convert (type, expr, false);
4587
4588 case UNCONSTRAINED_ARRAY_TYPE:
7948ae37
OH
4589 /* If the input is a VECTOR_TYPE, convert to the representative
4590 array type first. */
4591 if (ecode == VECTOR_TYPE)
4592 {
4593 expr = convert (TYPE_REPRESENTATIVE_ARRAY (etype), expr);
4594 etype = TREE_TYPE (expr);
4595 ecode = TREE_CODE (etype);
4596 }
4597
a1ab4c31
AC
4598 /* If EXPR is a constrained array, take its address, convert it to a
4599 fat pointer, and then dereference it. Likewise if EXPR is a
4600 record containing both a template and a constrained array.
4601 Note that a record representing a justified modular type
4602 always represents a packed constrained array. */
4603 if (ecode == ARRAY_TYPE
4604 || (ecode == INTEGER_TYPE && TYPE_HAS_ACTUAL_BOUNDS_P (etype))
4605 || (ecode == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (etype))
4606 || (ecode == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (etype)))
4607 return
4608 build_unary_op
4609 (INDIRECT_REF, NULL_TREE,
4610 convert_to_fat_pointer (TREE_TYPE (type),
4611 build_unary_op (ADDR_EXPR,
4612 NULL_TREE, expr)));
4613
4614 /* Do something very similar for converting one unconstrained
4615 array to another. */
4616 else if (ecode == UNCONSTRAINED_ARRAY_TYPE)
4617 return
4618 build_unary_op (INDIRECT_REF, NULL_TREE,
4619 convert (TREE_TYPE (type),
4620 build_unary_op (ADDR_EXPR,
4621 NULL_TREE, expr)));
4622 else
4623 gcc_unreachable ();
4624
4625 case COMPLEX_TYPE:
4626 return fold (convert_to_complex (type, expr));
4627
4628 default:
4629 gcc_unreachable ();
4630 }
4631}
15bf7d19
EB
4632
4633/* Create an expression whose value is that of EXPR converted to the common
4634 index type, which is sizetype. EXPR is supposed to be in the base type
4635 of the GNAT index type. Calling it is equivalent to doing
4636
4637 convert (sizetype, expr)
4638
4639 but we try to distribute the type conversion with the knowledge that EXPR
4640 cannot overflow in its type. This is a best-effort approach and we fall
4641 back to the above expression as soon as difficulties are encountered.
4642
4643 This is necessary to overcome issues that arise when the GNAT base index
4644 type and the GCC common index type (sizetype) don't have the same size,
4645 which is quite frequent on 64-bit architectures. In this case, and if
4646 the GNAT base index type is signed but the iteration type of the loop has
4647 been forced to unsigned, the loop scalar evolution engine cannot compute
4648 a simple evolution for the general induction variables associated with the
4649 array indices, because it will preserve the wrap-around semantics in the
4650 unsigned type of their "inner" part. As a result, many loop optimizations
4651 are blocked.
4652
4653 The solution is to use a special (basic) induction variable that is at
4654 least as large as sizetype, and to express the aforementioned general
4655 induction variables in terms of this induction variable, eliminating
4656 the problematic intermediate truncation to the GNAT base index type.
4657 This is possible as long as the original expression doesn't overflow
4658 and if the middle-end hasn't introduced artificial overflows in the
4659 course of the various simplification it can make to the expression. */
4660
4661tree
4662convert_to_index_type (tree expr)
4663{
4664 enum tree_code code = TREE_CODE (expr);
4665 tree type = TREE_TYPE (expr);
4666
4667 /* If the type is unsigned, overflow is allowed so we cannot be sure that
4668 EXPR doesn't overflow. Keep it simple if optimization is disabled. */
4669 if (TYPE_UNSIGNED (type) || !optimize)
4670 return convert (sizetype, expr);
4671
4672 switch (code)
4673 {
4674 case VAR_DECL:
4675 /* The main effect of the function: replace a loop parameter with its
4676 associated special induction variable. */
4677 if (DECL_LOOP_PARM_P (expr) && DECL_INDUCTION_VAR (expr))
4678 expr = DECL_INDUCTION_VAR (expr);
4679 break;
4680
4681 CASE_CONVERT:
4682 {
4683 tree otype = TREE_TYPE (TREE_OPERAND (expr, 0));
4684 /* Bail out as soon as we suspect some sort of type frobbing. */
4685 if (TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4686 || TYPE_UNSIGNED (type) != TYPE_UNSIGNED (otype))
4687 break;
4688 }
4689
4690 /* ... fall through ... */
4691
4692 case NON_LVALUE_EXPR:
4693 return fold_build1 (code, sizetype,
4694 convert_to_index_type (TREE_OPERAND (expr, 0)));
4695
4696 case PLUS_EXPR:
4697 case MINUS_EXPR:
4698 case MULT_EXPR:
4699 return fold_build2 (code, sizetype,
4700 convert_to_index_type (TREE_OPERAND (expr, 0)),
4701 convert_to_index_type (TREE_OPERAND (expr, 1)));
4702
4703 case COMPOUND_EXPR:
4704 return fold_build2 (code, sizetype, TREE_OPERAND (expr, 0),
4705 convert_to_index_type (TREE_OPERAND (expr, 1)));
4706
4707 case COND_EXPR:
4708 return fold_build3 (code, sizetype, TREE_OPERAND (expr, 0),
4709 convert_to_index_type (TREE_OPERAND (expr, 1)),
4710 convert_to_index_type (TREE_OPERAND (expr, 2)));
4711
4712 default:
4713 break;
4714 }
4715
4716 return convert (sizetype, expr);
4717}
a1ab4c31
AC
4718\f
4719/* Remove all conversions that are done in EXP. This includes converting
4720 from a padded type or to a justified modular type. If TRUE_ADDRESS
4721 is true, always return the address of the containing object even if
4722 the address is not bit-aligned. */
4723
4724tree
4725remove_conversions (tree exp, bool true_address)
4726{
4727 switch (TREE_CODE (exp))
4728 {
4729 case CONSTRUCTOR:
4730 if (true_address
4731 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
4732 && TYPE_JUSTIFIED_MODULAR_P (TREE_TYPE (exp)))
4733 return
2117b9bb 4734 remove_conversions (CONSTRUCTOR_ELT (exp, 0)->value, true);
a1ab4c31
AC
4735 break;
4736
4737 case COMPONENT_REF:
315cff15 4738 if (TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
a1ab4c31
AC
4739 return remove_conversions (TREE_OPERAND (exp, 0), true_address);
4740 break;
4741
a1ab4c31 4742 CASE_CONVERT:
722356ce
EB
4743 case VIEW_CONVERT_EXPR:
4744 case NON_LVALUE_EXPR:
a1ab4c31
AC
4745 return remove_conversions (TREE_OPERAND (exp, 0), true_address);
4746
4747 default:
4748 break;
4749 }
4750
4751 return exp;
4752}
4753\f
4754/* If EXP's type is an UNCONSTRAINED_ARRAY_TYPE, return an expression that
86060344 4755 refers to the underlying array. If it has TYPE_CONTAINS_TEMPLATE_P,
a1ab4c31
AC
4756 likewise return an expression pointing to the underlying array. */
4757
4758tree
4759maybe_unconstrained_array (tree exp)
4760{
4761 enum tree_code code = TREE_CODE (exp);
1aa291f7 4762 tree type = TREE_TYPE (exp);
a1ab4c31 4763
1aa291f7 4764 switch (TREE_CODE (type))
a1ab4c31
AC
4765 {
4766 case UNCONSTRAINED_ARRAY_TYPE:
4767 if (code == UNCONSTRAINED_ARRAY_REF)
4768 {
7e169899 4769 const bool read_only = TREE_READONLY (exp);
a1c7d797
EB
4770 const bool no_trap = TREE_THIS_NOTRAP (exp);
4771
7e169899 4772 exp = TREE_OPERAND (exp, 0);
1aa291f7
EB
4773 type = TREE_TYPE (exp);
4774
7e169899
EB
4775 if (TREE_CODE (exp) == COND_EXPR)
4776 {
4777 tree op1
4778 = build_unary_op (INDIRECT_REF, NULL_TREE,
4779 build_component_ref (TREE_OPERAND (exp, 1),
1aa291f7 4780 TYPE_FIELDS (type),
7e169899
EB
4781 false));
4782 tree op2
4783 = build_unary_op (INDIRECT_REF, NULL_TREE,
4784 build_component_ref (TREE_OPERAND (exp, 2),
1aa291f7 4785 TYPE_FIELDS (type),
7e169899
EB
4786 false));
4787
4788 exp = build3 (COND_EXPR,
1aa291f7 4789 TREE_TYPE (TREE_TYPE (TYPE_FIELDS (type))),
7e169899
EB
4790 TREE_OPERAND (exp, 0), op1, op2);
4791 }
4792 else
a1c7d797
EB
4793 {
4794 exp = build_unary_op (INDIRECT_REF, NULL_TREE,
64235766
EB
4795 build_component_ref (exp,
4796 TYPE_FIELDS (type),
a1c7d797
EB
4797 false));
4798 TREE_READONLY (exp) = read_only;
4799 TREE_THIS_NOTRAP (exp) = no_trap;
4800 }
a1ab4c31
AC
4801 }
4802
4803 else if (code == NULL_EXPR)
1aa291f7
EB
4804 exp = build1 (NULL_EXPR,
4805 TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (type)))),
4806 TREE_OPERAND (exp, 0));
4807 break;
a1ab4c31
AC
4808
4809 case RECORD_TYPE:
1aa291f7
EB
4810 /* If this is a padded type and it contains a template, convert to the
4811 unpadded type first. */
4812 if (TYPE_PADDING_P (type)
4813 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (type))) == RECORD_TYPE
4814 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (TYPE_FIELDS (type))))
a1ab4c31 4815 {
1aa291f7 4816 exp = convert (TREE_TYPE (TYPE_FIELDS (type)), exp);
64235766 4817 code = TREE_CODE (exp);
1aa291f7
EB
4818 type = TREE_TYPE (exp);
4819 }
4820
4821 if (TYPE_CONTAINS_TEMPLATE_P (type))
4822 {
64235766
EB
4823 /* If the array initializer is a box, return NULL_TREE. */
4824 if (code == CONSTRUCTOR && CONSTRUCTOR_NELTS (exp) < 2)
4825 return NULL_TREE;
4826
4827 exp = build_component_ref (exp, DECL_CHAIN (TYPE_FIELDS (type)),
4828 false);
4829 type = TREE_TYPE (exp);
1aa291f7
EB
4830
4831 /* If the array type is padded, convert to the unpadded type. */
64235766
EB
4832 if (TYPE_IS_PADDING_P (type))
4833 exp = convert (TREE_TYPE (TYPE_FIELDS (type)), exp);
a1ab4c31 4834 }
a1ab4c31
AC
4835 break;
4836
4837 default:
4838 break;
4839 }
4840
4841 return exp;
4842}
4843\f
afcea859 4844/* Return true if EXPR is an expression that can be folded as an operand
84fb43a1 4845 of a VIEW_CONVERT_EXPR. See ada-tree.h for a complete rationale. */
afcea859
EB
4846
4847static bool
4848can_fold_for_view_convert_p (tree expr)
4849{
4850 tree t1, t2;
4851
4852 /* The folder will fold NOP_EXPRs between integral types with the same
4853 precision (in the middle-end's sense). We cannot allow it if the
4854 types don't have the same precision in the Ada sense as well. */
4855 if (TREE_CODE (expr) != NOP_EXPR)
4856 return true;
4857
4858 t1 = TREE_TYPE (expr);
4859 t2 = TREE_TYPE (TREE_OPERAND (expr, 0));
4860
4861 /* Defer to the folder for non-integral conversions. */
4862 if (!(INTEGRAL_TYPE_P (t1) && INTEGRAL_TYPE_P (t2)))
4863 return true;
4864
4865 /* Only fold conversions that preserve both precisions. */
4866 if (TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
4867 && operand_equal_p (rm_size (t1), rm_size (t2), 0))
4868 return true;
4869
4870 return false;
4871}
4872
a1ab4c31 4873/* Return an expression that does an unchecked conversion of EXPR to TYPE.
afcea859
EB
4874 If NOTRUNC_P is true, truncation operations should be suppressed.
4875
4876 Special care is required with (source or target) integral types whose
4877 precision is not equal to their size, to make sure we fetch or assign
4878 the value bits whose location might depend on the endianness, e.g.
4879
4880 Rmsize : constant := 8;
4881 subtype Int is Integer range 0 .. 2 ** Rmsize - 1;
4882
4883 type Bit_Array is array (1 .. Rmsize) of Boolean;
4884 pragma Pack (Bit_Array);
4885
4886 function To_Bit_Array is new Unchecked_Conversion (Int, Bit_Array);
4887
4888 Value : Int := 2#1000_0001#;
4889 Vbits : Bit_Array := To_Bit_Array (Value);
4890
4891 we expect the 8 bits at Vbits'Address to always contain Value, while
4892 their original location depends on the endianness, at Value'Address
84fb43a1 4893 on a little-endian architecture but not on a big-endian one. */
a1ab4c31
AC
4894
4895tree
4896unchecked_convert (tree type, tree expr, bool notrunc_p)
4897{
4898 tree etype = TREE_TYPE (expr);
c34f3839
EB
4899 enum tree_code ecode = TREE_CODE (etype);
4900 enum tree_code code = TREE_CODE (type);
e63b36bd 4901 tree tem;
980a0501 4902 int c;
a1ab4c31 4903
c34f3839 4904 /* If the expression is already of the right type, we are done. */
a1ab4c31
AC
4905 if (etype == type)
4906 return expr;
4907
026c3cfd 4908 /* If both types are integral just do a normal conversion.
a1ab4c31 4909 Likewise for a conversion to an unconstrained array. */
1eb58520 4910 if (((INTEGRAL_TYPE_P (type)
0d7de0e1 4911 || (POINTER_TYPE_P (type) && !TYPE_IS_THIN_POINTER_P (type))
c34f3839 4912 || (code == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (type)))
1eb58520 4913 && (INTEGRAL_TYPE_P (etype)
315cff15 4914 || (POINTER_TYPE_P (etype) && !TYPE_IS_THIN_POINTER_P (etype))
c34f3839
EB
4915 || (ecode == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (etype))))
4916 || code == UNCONSTRAINED_ARRAY_TYPE)
a1ab4c31 4917 {
c34f3839 4918 if (ecode == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (etype))
a1ab4c31
AC
4919 {
4920 tree ntype = copy_type (etype);
a1ab4c31
AC
4921 TYPE_BIASED_REPRESENTATION_P (ntype) = 0;
4922 TYPE_MAIN_VARIANT (ntype) = ntype;
4923 expr = build1 (NOP_EXPR, ntype, expr);
4924 }
4925
c34f3839 4926 if (code == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (type))
a1ab4c31 4927 {
afcea859 4928 tree rtype = copy_type (type);
a1ab4c31
AC
4929 TYPE_BIASED_REPRESENTATION_P (rtype) = 0;
4930 TYPE_MAIN_VARIANT (rtype) = rtype;
afcea859
EB
4931 expr = convert (rtype, expr);
4932 expr = build1 (NOP_EXPR, type, expr);
a1ab4c31 4933 }
afcea859
EB
4934 else
4935 expr = convert (type, expr);
a1ab4c31
AC
4936 }
4937
afcea859 4938 /* If we are converting to an integral type whose precision is not equal
ee45a32d
EB
4939 to its size, first unchecked convert to a record type that contains a
4940 field of the given precision. Then extract the result from the field.
4941
4942 There is a subtlety if the source type is an aggregate type with reverse
4943 storage order because its representation is not contiguous in the native
4944 storage order, i.e. a direct unchecked conversion to an integral type
4945 with N bits of precision cannot read the first N bits of the aggregate
4946 type. To overcome it, we do an unchecked conversion to an integral type
4947 with reverse storage order and return the resulting value. This also
4948 ensures that the result of the unchecked conversion doesn't depend on
4949 the endianness of the target machine, but only on the storage order of
4950 the aggregate type.
4951
4952 Finally, for the sake of consistency, we do the unchecked conversion
4953 to an integral type with reverse storage order as soon as the source
4954 type is an aggregate type with reverse storage order, even if there
4955 are no considerations of precision or size involved. */
980a0501
EB
4956 else if (INTEGRAL_TYPE_P (type)
4957 && TYPE_RM_SIZE (type)
9a1bdc31
EB
4958 && (tree_int_cst_compare (TYPE_RM_SIZE (type),
4959 TYPE_SIZE (type)) < 0
ee45a32d
EB
4960 || (AGGREGATE_TYPE_P (etype)
4961 && TYPE_REVERSE_STORAGE_ORDER (etype))))
a1ab4c31
AC
4962 {
4963 tree rec_type = make_node (RECORD_TYPE);
416de7d5
EB
4964 unsigned HOST_WIDE_INT prec = TREE_INT_CST_LOW (TYPE_RM_SIZE (type));
4965 tree field_type, field;
4966
ee45a32d
EB
4967 if (AGGREGATE_TYPE_P (etype))
4968 TYPE_REVERSE_STORAGE_ORDER (rec_type)
4969 = TYPE_REVERSE_STORAGE_ORDER (etype);
4970
416de7d5
EB
4971 if (TYPE_UNSIGNED (type))
4972 field_type = make_unsigned_type (prec);
4973 else
4974 field_type = make_signed_type (prec);
4975 SET_TYPE_RM_SIZE (field_type, TYPE_RM_SIZE (type));
4976
4977 field = create_field_decl (get_identifier ("OBJ"), field_type, rec_type,
44e9e3ec 4978 NULL_TREE, bitsize_zero_node, 1, 0);
a1ab4c31 4979
44e9e3ec 4980 finish_record_type (rec_type, field, 1, false);
a1ab4c31
AC
4981
4982 expr = unchecked_convert (rec_type, expr, notrunc_p);
64235766 4983 expr = build_component_ref (expr, field, false);
416de7d5 4984 expr = fold_build1 (NOP_EXPR, type, expr);
a1ab4c31
AC
4985 }
4986
416de7d5
EB
4987 /* Similarly if we are converting from an integral type whose precision is
4988 not equal to its size, first copy into a field of the given precision
ee45a32d
EB
4989 and unchecked convert the record type.
4990
4991 The same considerations as above apply if the target type is an aggregate
4992 type with reverse storage order and we also proceed similarly. */
980a0501
EB
4993 else if (INTEGRAL_TYPE_P (etype)
4994 && TYPE_RM_SIZE (etype)
9a1bdc31
EB
4995 && (tree_int_cst_compare (TYPE_RM_SIZE (etype),
4996 TYPE_SIZE (etype)) < 0
ee45a32d
EB
4997 || (AGGREGATE_TYPE_P (type)
4998 && TYPE_REVERSE_STORAGE_ORDER (type))))
a1ab4c31
AC
4999 {
5000 tree rec_type = make_node (RECORD_TYPE);
416de7d5 5001 unsigned HOST_WIDE_INT prec = TREE_INT_CST_LOW (TYPE_RM_SIZE (etype));
9771b263
DN
5002 vec<constructor_elt, va_gc> *v;
5003 vec_alloc (v, 1);
416de7d5
EB
5004 tree field_type, field;
5005
ee45a32d
EB
5006 if (AGGREGATE_TYPE_P (type))
5007 TYPE_REVERSE_STORAGE_ORDER (rec_type)
5008 = TYPE_REVERSE_STORAGE_ORDER (type);
5009
416de7d5
EB
5010 if (TYPE_UNSIGNED (etype))
5011 field_type = make_unsigned_type (prec);
5012 else
5013 field_type = make_signed_type (prec);
5014 SET_TYPE_RM_SIZE (field_type, TYPE_RM_SIZE (etype));
5015
5016 field = create_field_decl (get_identifier ("OBJ"), field_type, rec_type,
44e9e3ec 5017 NULL_TREE, bitsize_zero_node, 1, 0);
a1ab4c31 5018
44e9e3ec 5019 finish_record_type (rec_type, field, 1, false);
a1ab4c31 5020
416de7d5 5021 expr = fold_build1 (NOP_EXPR, field_type, expr);
0e228dd9
NF
5022 CONSTRUCTOR_APPEND_ELT (v, field, expr);
5023 expr = gnat_build_constructor (rec_type, v);
a1ab4c31
AC
5024 expr = unchecked_convert (type, expr, notrunc_p);
5025 }
5026
980a0501
EB
5027 /* If we are converting from a scalar type to a type with a different size,
5028 we need to pad to have the same size on both sides.
5029
5030 ??? We cannot do it unconditionally because unchecked conversions are
5031 used liberally by the front-end to implement polymorphism, e.g. in:
5032
5033 S191s : constant ada__tags__addr_ptr := ada__tags__addr_ptr!(S190s);
5034 return p___size__4 (p__object!(S191s.all));
5035
5036 so we skip all expressions that are references. */
5037 else if (!REFERENCE_CLASS_P (expr)
5038 && !AGGREGATE_TYPE_P (etype)
5039 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
5040 && (c = tree_int_cst_compare (TYPE_SIZE (etype), TYPE_SIZE (type))))
5041 {
5042 if (c < 0)
5043 {
5044 expr = convert (maybe_pad_type (etype, TYPE_SIZE (type), 0, Empty,
5045 false, false, false, true),
5046 expr);
5047 expr = unchecked_convert (type, expr, notrunc_p);
5048 }
5049 else
5050 {
5051 tree rec_type = maybe_pad_type (type, TYPE_SIZE (etype), 0, Empty,
5052 false, false, false, true);
5053 expr = unchecked_convert (rec_type, expr, notrunc_p);
64235766 5054 expr = build_component_ref (expr, TYPE_FIELDS (rec_type), false);
980a0501
EB
5055 }
5056 }
5057
7948ae37
OH
5058 /* We have a special case when we are converting between two unconstrained
5059 array types. In that case, take the address, convert the fat pointer
5060 types, and dereference. */
c34f3839 5061 else if (ecode == code && code == UNCONSTRAINED_ARRAY_TYPE)
a1ab4c31
AC
5062 expr = build_unary_op (INDIRECT_REF, NULL_TREE,
5063 build1 (VIEW_CONVERT_EXPR, TREE_TYPE (type),
5064 build_unary_op (ADDR_EXPR, NULL_TREE,
5065 expr)));
7948ae37
OH
5066
5067 /* Another special case is when we are converting to a vector type from its
5068 representative array type; this a regular conversion. */
c34f3839
EB
5069 else if (code == VECTOR_TYPE
5070 && ecode == ARRAY_TYPE
7948ae37
OH
5071 && gnat_types_compatible_p (TYPE_REPRESENTATIVE_ARRAY (type),
5072 etype))
5073 expr = convert (type, expr);
5074
e63b36bd
EB
5075 /* And, if the array type is not the representative, we try to build an
5076 intermediate vector type of which the array type is the representative
5077 and to do the unchecked conversion between the vector types, in order
5078 to enable further simplifications in the middle-end. */
5079 else if (code == VECTOR_TYPE
5080 && ecode == ARRAY_TYPE
5081 && (tem = build_vector_type_for_array (etype, NULL_TREE)))
5082 {
5083 expr = convert (tem, expr);
5084 return unchecked_convert (type, expr, notrunc_p);
5085 }
5086
44e9e3ec
EB
5087 /* If we are converting a CONSTRUCTOR to a more aligned RECORD_TYPE, bump
5088 the alignment of the CONSTRUCTOR to speed up the copy operation. */
5089 else if (TREE_CODE (expr) == CONSTRUCTOR
5090 && code == RECORD_TYPE
5091 && TYPE_ALIGN (etype) < TYPE_ALIGN (type))
5092 {
5093 expr = convert (maybe_pad_type (etype, NULL_TREE, TYPE_ALIGN (type),
5094 Empty, false, false, false, true),
5095 expr);
5096 return unchecked_convert (type, expr, notrunc_p);
5097 }
5098
5099 /* Otherwise, just build a VIEW_CONVERT_EXPR of the expression. */
a1ab4c31
AC
5100 else
5101 {
5102 expr = maybe_unconstrained_array (expr);
5103 etype = TREE_TYPE (expr);
c34f3839 5104 ecode = TREE_CODE (etype);
afcea859
EB
5105 if (can_fold_for_view_convert_p (expr))
5106 expr = fold_build1 (VIEW_CONVERT_EXPR, type, expr);
5107 else
5108 expr = build1 (VIEW_CONVERT_EXPR, type, expr);
a1ab4c31
AC
5109 }
5110
afcea859
EB
5111 /* If the result is an integral type whose precision is not equal to its
5112 size, sign- or zero-extend the result. We need not do this if the input
5113 is an integral type of the same precision and signedness or if the output
a1ab4c31
AC
5114 is a biased type or if both the input and output are unsigned. */
5115 if (!notrunc_p
9a1bdc31
EB
5116 && INTEGRAL_TYPE_P (type)
5117 && TYPE_RM_SIZE (type)
5118 && tree_int_cst_compare (TYPE_RM_SIZE (type), TYPE_SIZE (type)) < 0
a1ab4c31
AC
5119 && !(INTEGRAL_TYPE_P (etype)
5120 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (etype)
9a1bdc31
EB
5121 && tree_int_cst_compare (TYPE_RM_SIZE (type),
5122 TYPE_RM_SIZE (etype)
5123 ? TYPE_RM_SIZE (etype)
5124 : TYPE_SIZE (etype)) == 0)
5125 && !(code == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (type))
a1ab4c31
AC
5126 && !(TYPE_UNSIGNED (type) && TYPE_UNSIGNED (etype)))
5127 {
c34f3839 5128 tree base_type
9a1bdc31
EB
5129 = gnat_type_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
5130 TYPE_UNSIGNED (type));
a1ab4c31
AC
5131 tree shift_expr
5132 = convert (base_type,
5133 size_binop (MINUS_EXPR,
9a1bdc31 5134 TYPE_SIZE (type), TYPE_RM_SIZE (type)));
a1ab4c31
AC
5135 expr
5136 = convert (type,
5137 build_binary_op (RSHIFT_EXPR, base_type,
5138 build_binary_op (LSHIFT_EXPR, base_type,
5139 convert (base_type, expr),
5140 shift_expr),
5141 shift_expr));
5142 }
5143
5144 /* An unchecked conversion should never raise Constraint_Error. The code
5145 below assumes that GCC's conversion routines overflow the same way that
5146 the underlying hardware does. This is probably true. In the rare case
5147 when it is false, we can rely on the fact that such conversions are
5148 erroneous anyway. */
5149 if (TREE_CODE (expr) == INTEGER_CST)
5150 TREE_OVERFLOW (expr) = 0;
5151
5152 /* If the sizes of the types differ and this is an VIEW_CONVERT_EXPR,
5153 show no longer constant. */
5154 if (TREE_CODE (expr) == VIEW_CONVERT_EXPR
5155 && !operand_equal_p (TYPE_SIZE_UNIT (type), TYPE_SIZE_UNIT (etype),
5156 OEP_ONLY_CONST))
5157 TREE_CONSTANT (expr) = 0;
5158
5159 return expr;
5160}
5161\f
feec4372 5162/* Return the appropriate GCC tree code for the specified GNAT_TYPE,
a1ab4c31
AC
5163 the latter being a record type as predicated by Is_Record_Type. */
5164
5165enum tree_code
5166tree_code_for_record_type (Entity_Id gnat_type)
5167{
b1a785fb 5168 Node_Id component_list, component;
a1ab4c31 5169
b1a785fb
EB
5170 /* Return UNION_TYPE if it's an Unchecked_Union whose non-discriminant
5171 fields are all in the variant part. Otherwise, return RECORD_TYPE. */
a1ab4c31
AC
5172 if (!Is_Unchecked_Union (gnat_type))
5173 return RECORD_TYPE;
5174
b1a785fb
EB
5175 gnat_type = Implementation_Base_Type (gnat_type);
5176 component_list
5177 = Component_List (Type_Definition (Declaration_Node (gnat_type)));
5178
a1ab4c31
AC
5179 for (component = First_Non_Pragma (Component_Items (component_list));
5180 Present (component);
5181 component = Next_Non_Pragma (component))
5182 if (Ekind (Defining_Entity (component)) == E_Component)
5183 return RECORD_TYPE;
5184
5185 return UNION_TYPE;
5186}
5187
caa9d12a
EB
5188/* Return true if GNAT_TYPE is a "double" floating-point type, i.e. whose
5189 size is equal to 64 bits, or an array of such a type. Set ALIGN_CLAUSE
5190 according to the presence of an alignment clause on the type or, if it
5191 is an array, on the component type. */
5192
5193bool
5194is_double_float_or_array (Entity_Id gnat_type, bool *align_clause)
5195{
5196 gnat_type = Underlying_Type (gnat_type);
5197
5198 *align_clause = Present (Alignment_Clause (gnat_type));
5199
5200 if (Is_Array_Type (gnat_type))
5201 {
5202 gnat_type = Underlying_Type (Component_Type (gnat_type));
5203 if (Present (Alignment_Clause (gnat_type)))
5204 *align_clause = true;
5205 }
5206
5207 if (!Is_Floating_Point_Type (gnat_type))
5208 return false;
5209
5210 if (UI_To_Int (Esize (gnat_type)) != 64)
5211 return false;
5212
5213 return true;
5214}
5215
5216/* Return true if GNAT_TYPE is a "double" or larger scalar type, i.e. whose
5217 size is greater or equal to 64 bits, or an array of such a type. Set
5218 ALIGN_CLAUSE according to the presence of an alignment clause on the
5219 type or, if it is an array, on the component type. */
5220
5221bool
5222is_double_scalar_or_array (Entity_Id gnat_type, bool *align_clause)
5223{
5224 gnat_type = Underlying_Type (gnat_type);
5225
5226 *align_clause = Present (Alignment_Clause (gnat_type));
5227
5228 if (Is_Array_Type (gnat_type))
5229 {
5230 gnat_type = Underlying_Type (Component_Type (gnat_type));
5231 if (Present (Alignment_Clause (gnat_type)))
5232 *align_clause = true;
5233 }
5234
5235 if (!Is_Scalar_Type (gnat_type))
5236 return false;
5237
5238 if (UI_To_Int (Esize (gnat_type)) < 64)
5239 return false;
5240
5241 return true;
5242}
5243
a1ab4c31
AC
5244/* Return true if GNU_TYPE is suitable as the type of a non-aliased
5245 component of an aggregate type. */
5246
5247bool
5248type_for_nonaliased_component_p (tree gnu_type)
5249{
5250 /* If the type is passed by reference, we may have pointers to the
5251 component so it cannot be made non-aliased. */
5252 if (must_pass_by_ref (gnu_type) || default_pass_by_ref (gnu_type))
5253 return false;
5254
5255 /* We used to say that any component of aggregate type is aliased
5256 because the front-end may take 'Reference of it. The front-end
5257 has been enhanced in the meantime so as to use a renaming instead
5258 in most cases, but the back-end can probably take the address of
5259 such a component too so we go for the conservative stance.
5260
5261 For instance, we might need the address of any array type, even
5262 if normally passed by copy, to construct a fat pointer if the
5263 component is used as an actual for an unconstrained formal.
5264
5265 Likewise for record types: even if a specific record subtype is
5266 passed by copy, the parent type might be passed by ref (e.g. if
5267 it's of variable size) and we might take the address of a child
5268 component to pass to a parent formal. We have no way to check
5269 for such conditions here. */
5270 if (AGGREGATE_TYPE_P (gnu_type))
5271 return false;
5272
5273 return true;
5274}
5275
bb1f7929
EB
5276/* Return true if TYPE is a smaller form of ORIG_TYPE. */
5277
5278bool
5279smaller_form_type_p (tree type, tree orig_type)
5280{
5281 tree size, osize;
5282
5283 /* We're not interested in variants here. */
5284 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig_type))
5285 return false;
5286
5287 /* Like a variant, a packable version keeps the original TYPE_NAME. */
5288 if (TYPE_NAME (type) != TYPE_NAME (orig_type))
5289 return false;
5290
5291 size = TYPE_SIZE (type);
5292 osize = TYPE_SIZE (orig_type);
5293
5294 if (!(TREE_CODE (size) == INTEGER_CST && TREE_CODE (osize) == INTEGER_CST))
5295 return false;
5296
5297 return tree_int_cst_lt (size, osize) != 0;
5298}
5299
a22b794d 5300/* Perform final processing on global declarations. */
d7438551 5301
0f50b3db
EB
5302static GTY (()) tree dummy_global;
5303
a1ab4c31 5304void
a22b794d 5305gnat_write_global_declarations (void)
a1ab4c31 5306{
10e4d056
EB
5307 unsigned int i;
5308 tree iter;
5309
65444786 5310 /* If we have declared types as used at the global level, insert them in
755c71fa
EB
5311 the global hash table. We use a dummy variable for this purpose, but
5312 we need to build it unconditionally to avoid -fcompare-debug issues. */
5313 if (first_global_object_name)
65444786 5314 {
35e8bcf4 5315 struct varpool_node *node;
d3c268ab
EB
5316 char *label;
5317
5318 ASM_FORMAT_PRIVATE_NAME (label, first_global_object_name, 0);
65444786 5319 dummy_global
d3c268ab
EB
5320 = build_decl (BUILTINS_LOCATION, VAR_DECL, get_identifier (label),
5321 void_type_node);
dd25fe0a 5322 DECL_HARD_REGISTER (dummy_global) = 1;
65444786 5323 TREE_STATIC (dummy_global) = 1;
037e5573 5324 node = varpool_node::get_create (dummy_global);
dd25fe0a 5325 node->definition = 1;
67348ccc 5326 node->force_output = 1;
65444786 5327
755c71fa
EB
5328 if (types_used_by_cur_var_decl)
5329 while (!types_used_by_cur_var_decl->is_empty ())
5330 {
5331 tree t = types_used_by_cur_var_decl->pop ();
5332 types_used_by_var_decl_insert (t, dummy_global);
5333 }
65444786
EB
5334 }
5335
a22b794d
EB
5336 /* Output debug information for all global type declarations first. This
5337 ensures that global types whose compilation hasn't been finalized yet,
5338 for example pointers to Taft amendment types, have their compilation
5339 finalized in the right context. */
5340 FOR_EACH_VEC_SAFE_ELT (global_decls, i, iter)
5341 if (TREE_CODE (iter) == TYPE_DECL && !DECL_IGNORED_P (iter))
1f0e2688 5342 debug_hooks->type_decl (iter, false);
a22b794d
EB
5343
5344 /* Then output the global variables. We need to do that after the debug
8afaddaa 5345 information for global types is emitted so that they are finalized. */
a22b794d
EB
5346 FOR_EACH_VEC_SAFE_ELT (global_decls, i, iter)
5347 if (TREE_CODE (iter) == VAR_DECL)
5348 rest_of_decl_compilation (iter, true, 0);
caadda8e
PMR
5349
5350 /* Output the imported modules/declarations. In GNAT, these are only
5351 materializing subprogram. */
5352 FOR_EACH_VEC_SAFE_ELT (global_decls, i, iter)
5353 if (TREE_CODE (iter) == IMPORTED_DECL && !DECL_IGNORED_P (iter))
5354 debug_hooks->imported_module_or_decl (iter, DECL_NAME (iter),
5355 DECL_CONTEXT (iter), 0);
a1ab4c31
AC
5356}
5357
5358/* ************************************************************************
5359 * * GCC builtins support *
5360 * ************************************************************************ */
5361
5362/* The general scheme is fairly simple:
5363
5364 For each builtin function/type to be declared, gnat_install_builtins calls
aef308d0 5365 internal facilities which eventually get to gnat_pushdecl, which in turn
a1ab4c31
AC
5366 tracks the so declared builtin function decls in the 'builtin_decls' global
5367 datastructure. When an Intrinsic subprogram declaration is processed, we
5368 search this global datastructure to retrieve the associated BUILT_IN DECL
5369 node. */
5370
5371/* Search the chain of currently available builtin declarations for a node
5372 corresponding to function NAME (an IDENTIFIER_NODE). Return the first node
5373 found, if any, or NULL_TREE otherwise. */
5374tree
5375builtin_decl_for (tree name)
5376{
5377 unsigned i;
5378 tree decl;
5379
9771b263 5380 FOR_EACH_VEC_SAFE_ELT (builtin_decls, i, decl)
a1ab4c31
AC
5381 if (DECL_NAME (decl) == name)
5382 return decl;
5383
5384 return NULL_TREE;
5385}
5386
5387/* The code below eventually exposes gnat_install_builtins, which declares
5388 the builtin types and functions we might need, either internally or as
5389 user accessible facilities.
5390
5391 ??? This is a first implementation shot, still in rough shape. It is
5392 heavily inspired from the "C" family implementation, with chunks copied
5393 verbatim from there.
5394
ba464315 5395 Two obvious improvement candidates are:
a1ab4c31
AC
5396 o Use a more efficient name/decl mapping scheme
5397 o Devise a middle-end infrastructure to avoid having to copy
5398 pieces between front-ends. */
5399
5400/* ----------------------------------------------------------------------- *
5401 * BUILTIN ELEMENTARY TYPES *
5402 * ----------------------------------------------------------------------- */
5403
5404/* Standard data types to be used in builtin argument declarations. */
5405
5406enum c_tree_index
5407{
5408 CTI_SIGNED_SIZE_TYPE, /* For format checking only. */
5409 CTI_STRING_TYPE,
5410 CTI_CONST_STRING_TYPE,
5411
5412 CTI_MAX
5413};
5414
5415static tree c_global_trees[CTI_MAX];
5416
5417#define signed_size_type_node c_global_trees[CTI_SIGNED_SIZE_TYPE]
5418#define string_type_node c_global_trees[CTI_STRING_TYPE]
5419#define const_string_type_node c_global_trees[CTI_CONST_STRING_TYPE]
5420
5421/* ??? In addition some attribute handlers, we currently don't support a
5422 (small) number of builtin-types, which in turns inhibits support for a
5423 number of builtin functions. */
5424#define wint_type_node void_type_node
5425#define intmax_type_node void_type_node
5426#define uintmax_type_node void_type_node
5427
5428/* Build the void_list_node (void_type_node having been created). */
5429
5430static tree
5431build_void_list_node (void)
5432{
5433 tree t = build_tree_list (NULL_TREE, void_type_node);
5434 return t;
5435}
5436
5437/* Used to help initialize the builtin-types.def table. When a type of
5438 the correct size doesn't exist, use error_mark_node instead of NULL.
5439 The later results in segfaults even when a decl using the type doesn't
5440 get invoked. */
5441
5442static tree
5443builtin_type_for_size (int size, bool unsignedp)
5444{
ced57283 5445 tree type = gnat_type_for_size (size, unsignedp);
a1ab4c31
AC
5446 return type ? type : error_mark_node;
5447}
5448
5449/* Build/push the elementary type decls that builtin functions/types
5450 will need. */
5451
5452static void
5453install_builtin_elementary_types (void)
5454{
9a1bdc31 5455 signed_size_type_node = gnat_signed_type_for (size_type_node);
a1ab4c31
AC
5456 pid_type_node = integer_type_node;
5457 void_list_node = build_void_list_node ();
5458
5459 string_type_node = build_pointer_type (char_type_node);
5460 const_string_type_node
5461 = build_pointer_type (build_qualified_type
5462 (char_type_node, TYPE_QUAL_CONST));
5463}
5464
5465/* ----------------------------------------------------------------------- *
5466 * BUILTIN FUNCTION TYPES *
5467 * ----------------------------------------------------------------------- */
5468
5469/* Now, builtin function types per se. */
5470
5471enum c_builtin_type
5472{
5473#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5474#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5475#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5476#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5477#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5478#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5479#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
f6a7cffc
TS
5480#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5481 ARG6) NAME,
5482#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5483 ARG6, ARG7) NAME,
5484#define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5485 ARG6, ARG7, ARG8) NAME,
d9a6bd32
JJ
5486#define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5487 ARG6, ARG7, ARG8, ARG9) NAME,
5488#define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5489 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
5490#define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5491 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
a1ab4c31
AC
5492#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5493#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5494#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5495#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5496#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
f6a7cffc 5497#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
56a9f6bc 5498 NAME,
3e32ee19
NS
5499#define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5500 ARG6) NAME,
56a9f6bc
TS
5501#define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5502 ARG6, ARG7) NAME,
a1ab4c31
AC
5503#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5504#include "builtin-types.def"
5505#undef DEF_PRIMITIVE_TYPE
5506#undef DEF_FUNCTION_TYPE_0
5507#undef DEF_FUNCTION_TYPE_1
5508#undef DEF_FUNCTION_TYPE_2
5509#undef DEF_FUNCTION_TYPE_3
5510#undef DEF_FUNCTION_TYPE_4
5511#undef DEF_FUNCTION_TYPE_5
5512#undef DEF_FUNCTION_TYPE_6
5513#undef DEF_FUNCTION_TYPE_7
acf0174b 5514#undef DEF_FUNCTION_TYPE_8
d9a6bd32
JJ
5515#undef DEF_FUNCTION_TYPE_9
5516#undef DEF_FUNCTION_TYPE_10
5517#undef DEF_FUNCTION_TYPE_11
a1ab4c31
AC
5518#undef DEF_FUNCTION_TYPE_VAR_0
5519#undef DEF_FUNCTION_TYPE_VAR_1
5520#undef DEF_FUNCTION_TYPE_VAR_2
5521#undef DEF_FUNCTION_TYPE_VAR_3
5522#undef DEF_FUNCTION_TYPE_VAR_4
5523#undef DEF_FUNCTION_TYPE_VAR_5
3e32ee19 5524#undef DEF_FUNCTION_TYPE_VAR_6
56a9f6bc 5525#undef DEF_FUNCTION_TYPE_VAR_7
a1ab4c31
AC
5526#undef DEF_POINTER_TYPE
5527 BT_LAST
5528};
5529
5530typedef enum c_builtin_type builtin_type;
5531
5532/* A temporary array used in communication with def_fn_type. */
5533static GTY(()) tree builtin_types[(int) BT_LAST + 1];
5534
5535/* A helper function for install_builtin_types. Build function type
5536 for DEF with return type RET and N arguments. If VAR is true, then the
5537 function should be variadic after those N arguments.
5538
5539 Takes special care not to ICE if any of the types involved are
5540 error_mark_node, which indicates that said type is not in fact available
5541 (see builtin_type_for_size). In which case the function type as a whole
5542 should be error_mark_node. */
5543
5544static void
5545def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5546{
e5b00edf
NF
5547 tree t;
5548 tree *args = XALLOCAVEC (tree, n);
a1ab4c31
AC
5549 va_list list;
5550 int i;
5551
5552 va_start (list, n);
5553 for (i = 0; i < n; ++i)
5554 {
c6bd4220 5555 builtin_type a = (builtin_type) va_arg (list, int);
a1ab4c31
AC
5556 t = builtin_types[a];
5557 if (t == error_mark_node)
5558 goto egress;
e5b00edf 5559 args[i] = t;
a1ab4c31 5560 }
a1ab4c31 5561
a1ab4c31
AC
5562 t = builtin_types[ret];
5563 if (t == error_mark_node)
5564 goto egress;
e5b00edf
NF
5565 if (var)
5566 t = build_varargs_function_type_array (t, n, args);
5567 else
5568 t = build_function_type_array (t, n, args);
a1ab4c31
AC
5569
5570 egress:
5571 builtin_types[def] = t;
0edf1bb2 5572 va_end (list);
a1ab4c31
AC
5573}
5574
5575/* Build the builtin function types and install them in the builtin_types
5576 array for later use in builtin function decls. */
5577
5578static void
5579install_builtin_function_types (void)
5580{
5581 tree va_list_ref_type_node;
5582 tree va_list_arg_type_node;
5583
5584 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5585 {
5586 va_list_arg_type_node = va_list_ref_type_node =
5587 build_pointer_type (TREE_TYPE (va_list_type_node));
5588 }
5589 else
5590 {
5591 va_list_arg_type_node = va_list_type_node;
5592 va_list_ref_type_node = build_reference_type (va_list_type_node);
5593 }
5594
5595#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5596 builtin_types[ENUM] = VALUE;
5597#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5598 def_fn_type (ENUM, RETURN, 0, 0);
5599#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5600 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5601#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5602 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5603#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5604 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5605#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5606 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5607#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5608 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5609#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5610 ARG6) \
5611 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5612#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5613 ARG6, ARG7) \
5614 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
acf0174b
JJ
5615#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5616 ARG6, ARG7, ARG8) \
5617 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5618 ARG7, ARG8);
d9a6bd32
JJ
5619#define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5620 ARG6, ARG7, ARG8, ARG9) \
5621 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5622 ARG7, ARG8, ARG9);
5623#define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5,\
5624 ARG6, ARG7, ARG8, ARG9, ARG10) \
5625 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5626 ARG7, ARG8, ARG9, ARG10);
5627#define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5,\
5628 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5629 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5630 ARG7, ARG8, ARG9, ARG10, ARG11);
a1ab4c31
AC
5631#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5632 def_fn_type (ENUM, RETURN, 1, 0);
5633#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5634 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5635#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5636 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5637#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5638 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5639#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5640 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5641#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5642 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3e32ee19
NS
5643#define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5644 ARG6) \
5645 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
56a9f6bc
TS
5646#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5647 ARG6, ARG7) \
5648 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
a1ab4c31
AC
5649#define DEF_POINTER_TYPE(ENUM, TYPE) \
5650 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5651
5652#include "builtin-types.def"
5653
5654#undef DEF_PRIMITIVE_TYPE
f6a7cffc 5655#undef DEF_FUNCTION_TYPE_0
a1ab4c31
AC
5656#undef DEF_FUNCTION_TYPE_1
5657#undef DEF_FUNCTION_TYPE_2
5658#undef DEF_FUNCTION_TYPE_3
5659#undef DEF_FUNCTION_TYPE_4
5660#undef DEF_FUNCTION_TYPE_5
5661#undef DEF_FUNCTION_TYPE_6
f6a7cffc
TS
5662#undef DEF_FUNCTION_TYPE_7
5663#undef DEF_FUNCTION_TYPE_8
d9a6bd32
JJ
5664#undef DEF_FUNCTION_TYPE_9
5665#undef DEF_FUNCTION_TYPE_10
5666#undef DEF_FUNCTION_TYPE_11
a1ab4c31
AC
5667#undef DEF_FUNCTION_TYPE_VAR_0
5668#undef DEF_FUNCTION_TYPE_VAR_1
5669#undef DEF_FUNCTION_TYPE_VAR_2
5670#undef DEF_FUNCTION_TYPE_VAR_3
5671#undef DEF_FUNCTION_TYPE_VAR_4
5672#undef DEF_FUNCTION_TYPE_VAR_5
3e32ee19 5673#undef DEF_FUNCTION_TYPE_VAR_6
56a9f6bc 5674#undef DEF_FUNCTION_TYPE_VAR_7
a1ab4c31
AC
5675#undef DEF_POINTER_TYPE
5676 builtin_types[(int) BT_LAST] = NULL_TREE;
5677}
5678
5679/* ----------------------------------------------------------------------- *
5680 * BUILTIN ATTRIBUTES *
5681 * ----------------------------------------------------------------------- */
5682
5683enum built_in_attribute
5684{
5685#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5686#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
e384e6b5 5687#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
a1ab4c31
AC
5688#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5689#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5690#include "builtin-attrs.def"
5691#undef DEF_ATTR_NULL_TREE
5692#undef DEF_ATTR_INT
e384e6b5 5693#undef DEF_ATTR_STRING
a1ab4c31
AC
5694#undef DEF_ATTR_IDENT
5695#undef DEF_ATTR_TREE_LIST
5696 ATTR_LAST
5697};
5698
5699static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5700
5701static void
5702install_builtin_attributes (void)
5703{
5704 /* Fill in the built_in_attributes array. */
5705#define DEF_ATTR_NULL_TREE(ENUM) \
5706 built_in_attributes[(int) ENUM] = NULL_TREE;
5707#define DEF_ATTR_INT(ENUM, VALUE) \
5708 built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE);
e384e6b5
BS
5709#define DEF_ATTR_STRING(ENUM, VALUE) \
5710 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
a1ab4c31
AC
5711#define DEF_ATTR_IDENT(ENUM, STRING) \
5712 built_in_attributes[(int) ENUM] = get_identifier (STRING);
5713#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5714 built_in_attributes[(int) ENUM] \
5715 = tree_cons (built_in_attributes[(int) PURPOSE], \
5716 built_in_attributes[(int) VALUE], \
5717 built_in_attributes[(int) CHAIN]);
5718#include "builtin-attrs.def"
5719#undef DEF_ATTR_NULL_TREE
5720#undef DEF_ATTR_INT
e384e6b5 5721#undef DEF_ATTR_STRING
a1ab4c31
AC
5722#undef DEF_ATTR_IDENT
5723#undef DEF_ATTR_TREE_LIST
5724}
5725
5726/* Handle a "const" attribute; arguments as in
5727 struct attribute_spec.handler. */
5728
5729static tree
5730handle_const_attribute (tree *node, tree ARG_UNUSED (name),
5731 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
5732 bool *no_add_attrs)
5733{
5734 if (TREE_CODE (*node) == FUNCTION_DECL)
5735 TREE_READONLY (*node) = 1;
5736 else
5737 *no_add_attrs = true;
5738
5739 return NULL_TREE;
5740}
5741
5742/* Handle a "nothrow" attribute; arguments as in
5743 struct attribute_spec.handler. */
5744
5745static tree
5746handle_nothrow_attribute (tree *node, tree ARG_UNUSED (name),
5747 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
5748 bool *no_add_attrs)
5749{
5750 if (TREE_CODE (*node) == FUNCTION_DECL)
5751 TREE_NOTHROW (*node) = 1;
5752 else
5753 *no_add_attrs = true;
5754
5755 return NULL_TREE;
5756}
5757
5758/* Handle a "pure" attribute; arguments as in
5759 struct attribute_spec.handler. */
5760
5761static tree
5762handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5763 int ARG_UNUSED (flags), bool *no_add_attrs)
5764{
5765 if (TREE_CODE (*node) == FUNCTION_DECL)
5766 DECL_PURE_P (*node) = 1;
ba464315 5767 /* TODO: support types. */
a1ab4c31
AC
5768 else
5769 {
7948ae37
OH
5770 warning (OPT_Wattributes, "%qs attribute ignored",
5771 IDENTIFIER_POINTER (name));
a1ab4c31
AC
5772 *no_add_attrs = true;
5773 }
5774
5775 return NULL_TREE;
5776}
5777
5778/* Handle a "no vops" attribute; arguments as in
5779 struct attribute_spec.handler. */
5780
5781static tree
5782handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
5783 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
5784 bool *ARG_UNUSED (no_add_attrs))
5785{
5786 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
5787 DECL_IS_NOVOPS (*node) = 1;
5788 return NULL_TREE;
5789}
5790
5791/* Helper for nonnull attribute handling; fetch the operand number
5792 from the attribute argument list. */
5793
5794static bool
5795get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
5796{
5797 /* Verify the arg number is a constant. */
807e902e 5798 if (!tree_fits_uhwi_p (arg_num_expr))
a1ab4c31
AC
5799 return false;
5800
5801 *valp = TREE_INT_CST_LOW (arg_num_expr);
5802 return true;
5803}
5804
5805/* Handle the "nonnull" attribute. */
5806static tree
5807handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
5808 tree args, int ARG_UNUSED (flags),
5809 bool *no_add_attrs)
5810{
5811 tree type = *node;
5812 unsigned HOST_WIDE_INT attr_arg_num;
5813
5814 /* If no arguments are specified, all pointer arguments should be
5815 non-null. Verify a full prototype is given so that the arguments
5816 will have the correct types when we actually check them later. */
5817 if (!args)
5818 {
f4da8dce 5819 if (!prototype_p (type))
a1ab4c31
AC
5820 {
5821 error ("nonnull attribute without arguments on a non-prototype");
5822 *no_add_attrs = true;
5823 }
5824 return NULL_TREE;
5825 }
5826
5827 /* Argument list specified. Verify that each argument number references
5828 a pointer argument. */
5829 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
5830 {
a1ab4c31
AC
5831 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
5832
5833 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
5834 {
5835 error ("nonnull argument has invalid operand number (argument %lu)",
5836 (unsigned long) attr_arg_num);
5837 *no_add_attrs = true;
5838 return NULL_TREE;
5839 }
5840
d7d058c5 5841 if (prototype_p (type))
a1ab4c31 5842 {
d7d058c5
NF
5843 function_args_iterator iter;
5844 tree argument;
5845
5846 function_args_iter_init (&iter, type);
5847 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
a1ab4c31 5848 {
d7d058c5 5849 argument = function_args_iter_cond (&iter);
a1ab4c31
AC
5850 if (!argument || ck_num == arg_num)
5851 break;
a1ab4c31
AC
5852 }
5853
5854 if (!argument
d7d058c5 5855 || TREE_CODE (argument) == VOID_TYPE)
a1ab4c31 5856 {
58c8f770
EB
5857 error ("nonnull argument with out-of-range operand number "
5858 "(argument %lu, operand %lu)",
a1ab4c31
AC
5859 (unsigned long) attr_arg_num, (unsigned long) arg_num);
5860 *no_add_attrs = true;
5861 return NULL_TREE;
5862 }
5863
d7d058c5 5864 if (TREE_CODE (argument) != POINTER_TYPE)
a1ab4c31 5865 {
58c8f770
EB
5866 error ("nonnull argument references non-pointer operand "
5867 "(argument %lu, operand %lu)",
a1ab4c31
AC
5868 (unsigned long) attr_arg_num, (unsigned long) arg_num);
5869 *no_add_attrs = true;
5870 return NULL_TREE;
5871 }
5872 }
5873 }
5874
5875 return NULL_TREE;
5876}
5877
5878/* Handle a "sentinel" attribute. */
5879
5880static tree
5881handle_sentinel_attribute (tree *node, tree name, tree args,
5882 int ARG_UNUSED (flags), bool *no_add_attrs)
5883{
f4da8dce 5884 if (!prototype_p (*node))
a1ab4c31
AC
5885 {
5886 warning (OPT_Wattributes,
7948ae37
OH
5887 "%qs attribute requires prototypes with named arguments",
5888 IDENTIFIER_POINTER (name));
a1ab4c31
AC
5889 *no_add_attrs = true;
5890 }
5891 else
5892 {
dcf0c47e 5893 if (!stdarg_p (*node))
a1ab4c31
AC
5894 {
5895 warning (OPT_Wattributes,
7948ae37
OH
5896 "%qs attribute only applies to variadic functions",
5897 IDENTIFIER_POINTER (name));
a1ab4c31
AC
5898 *no_add_attrs = true;
5899 }
5900 }
5901
5902 if (args)
5903 {
5904 tree position = TREE_VALUE (args);
5905
5906 if (TREE_CODE (position) != INTEGER_CST)
5907 {
5908 warning (0, "requested position is not an integer constant");
5909 *no_add_attrs = true;
5910 }
5911 else
5912 {
5913 if (tree_int_cst_lt (position, integer_zero_node))
5914 {
5915 warning (0, "requested position is less than zero");
5916 *no_add_attrs = true;
5917 }
5918 }
5919 }
5920
5921 return NULL_TREE;
5922}
5923
5924/* Handle a "noreturn" attribute; arguments as in
5925 struct attribute_spec.handler. */
5926
5927static tree
5928handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5929 int ARG_UNUSED (flags), bool *no_add_attrs)
5930{
5931 tree type = TREE_TYPE (*node);
5932
5933 /* See FIXME comment in c_common_attribute_table. */
5934 if (TREE_CODE (*node) == FUNCTION_DECL)
5935 TREE_THIS_VOLATILE (*node) = 1;
5936 else if (TREE_CODE (type) == POINTER_TYPE
5937 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5938 TREE_TYPE (*node)
5939 = build_pointer_type
5940 (build_type_variant (TREE_TYPE (type),
5941 TYPE_READONLY (TREE_TYPE (type)), 1));
5942 else
5943 {
7948ae37
OH
5944 warning (OPT_Wattributes, "%qs attribute ignored",
5945 IDENTIFIER_POINTER (name));
a1ab4c31
AC
5946 *no_add_attrs = true;
5947 }
5948
5949 return NULL_TREE;
5950}
5951
0d6e14fd
JH
5952/* Handle a "leaf" attribute; arguments as in
5953 struct attribute_spec.handler. */
5954
5955static tree
f087ea44 5956handle_leaf_attribute (tree *node, tree name, tree ARG_UNUSED (args),
0d6e14fd
JH
5957 int ARG_UNUSED (flags), bool *no_add_attrs)
5958{
5959 if (TREE_CODE (*node) != FUNCTION_DECL)
5960 {
5961 warning (OPT_Wattributes, "%qE attribute ignored", name);
5962 *no_add_attrs = true;
5963 }
5964 if (!TREE_PUBLIC (*node))
5965 {
32a5388a 5966 warning (OPT_Wattributes, "%qE attribute has no effect", name);
0d6e14fd 5967 *no_add_attrs = true;
f087ea44
AC
5968 }
5969
5970 return NULL_TREE;
5971}
5972
5973/* Handle a "always_inline" attribute; arguments as in
5974 struct attribute_spec.handler. */
5975
5976static tree
5977handle_always_inline_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5978 int ARG_UNUSED (flags), bool *no_add_attrs)
5979{
5980 if (TREE_CODE (*node) == FUNCTION_DECL)
5981 {
5982 /* Set the attribute and mark it for disregarding inline limits. */
5983 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
5984 }
5985 else
5986 {
5987 warning (OPT_Wattributes, "%qE attribute ignored", name);
5988 *no_add_attrs = true;
0d6e14fd
JH
5989 }
5990
5991 return NULL_TREE;
5992}
5993
a1ab4c31
AC
5994/* Handle a "malloc" attribute; arguments as in
5995 struct attribute_spec.handler. */
5996
5997static tree
5998handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5999 int ARG_UNUSED (flags), bool *no_add_attrs)
6000{
6001 if (TREE_CODE (*node) == FUNCTION_DECL
6002 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
6003 DECL_IS_MALLOC (*node) = 1;
6004 else
6005 {
7948ae37
OH
6006 warning (OPT_Wattributes, "%qs attribute ignored",
6007 IDENTIFIER_POINTER (name));
a1ab4c31
AC
6008 *no_add_attrs = true;
6009 }
6010
6011 return NULL_TREE;
6012}
6013
6014/* Fake handler for attributes we don't properly support. */
6015
6016tree
6017fake_attribute_handler (tree * ARG_UNUSED (node),
6018 tree ARG_UNUSED (name),
6019 tree ARG_UNUSED (args),
6020 int ARG_UNUSED (flags),
6021 bool * ARG_UNUSED (no_add_attrs))
6022{
6023 return NULL_TREE;
6024}
6025
6026/* Handle a "type_generic" attribute. */
6027
6028static tree
6029handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
6030 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
6031 bool * ARG_UNUSED (no_add_attrs))
6032{
a1ab4c31
AC
6033 /* Ensure we have a function type. */
6034 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
b4680ca1 6035
a1ab4c31 6036 /* Ensure we have a variadic function. */
dcf0c47e 6037 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
a1ab4c31
AC
6038
6039 return NULL_TREE;
6040}
6041
2724e58f
OH
6042/* Handle a "vector_size" attribute; arguments as in
6043 struct attribute_spec.handler. */
6044
6045static tree
6046handle_vector_size_attribute (tree *node, tree name, tree args,
e63b36bd 6047 int ARG_UNUSED (flags), bool *no_add_attrs)
2724e58f 6048{
e63b36bd
EB
6049 tree type = *node;
6050 tree vector_type;
2724e58f
OH
6051
6052 *no_add_attrs = true;
6053
2724e58f
OH
6054 /* We need to provide for vector pointers, vector arrays, and
6055 functions returning vectors. For example:
6056
6057 __attribute__((vector_size(16))) short *foo;
6058
6059 In this case, the mode is SI, but the type being modified is
6060 HI, so we need to look further. */
2724e58f
OH
6061 while (POINTER_TYPE_P (type)
6062 || TREE_CODE (type) == FUNCTION_TYPE
132a5459 6063 || TREE_CODE (type) == ARRAY_TYPE)
2724e58f
OH
6064 type = TREE_TYPE (type);
6065
e63b36bd
EB
6066 vector_type = build_vector_type_for_size (type, TREE_VALUE (args), name);
6067 if (!vector_type)
6068 return NULL_TREE;
2724e58f
OH
6069
6070 /* Build back pointers if needed. */
e63b36bd 6071 *node = reconstruct_complex_type (*node, vector_type);
2724e58f
OH
6072
6073 return NULL_TREE;
6074}
6075
7948ae37
OH
6076/* Handle a "vector_type" attribute; arguments as in
6077 struct attribute_spec.handler. */
6078
6079static tree
6080handle_vector_type_attribute (tree *node, tree name, tree ARG_UNUSED (args),
e63b36bd 6081 int ARG_UNUSED (flags), bool *no_add_attrs)
7948ae37 6082{
e63b36bd
EB
6083 tree type = *node;
6084 tree vector_type;
7948ae37
OH
6085
6086 *no_add_attrs = true;
6087
e63b36bd 6088 if (TREE_CODE (type) != ARRAY_TYPE)
7948ae37
OH
6089 {
6090 error ("attribute %qs applies to array types only",
6091 IDENTIFIER_POINTER (name));
6092 return NULL_TREE;
6093 }
6094
e63b36bd
EB
6095 vector_type = build_vector_type_for_array (type, name);
6096 if (!vector_type)
7948ae37
OH
6097 return NULL_TREE;
6098
e63b36bd
EB
6099 TYPE_REPRESENTATIVE_ARRAY (vector_type) = type;
6100 *node = vector_type;
7948ae37
OH
6101
6102 return NULL_TREE;
6103}
6104
a1ab4c31
AC
6105/* ----------------------------------------------------------------------- *
6106 * BUILTIN FUNCTIONS *
6107 * ----------------------------------------------------------------------- */
6108
6109/* Worker for DEF_BUILTIN. Possibly define a builtin function with one or two
6110 names. Does not declare a non-__builtin_ function if flag_no_builtin, or
6111 if nonansi_p and flag_no_nonansi_builtin. */
6112
6113static void
6114def_builtin_1 (enum built_in_function fncode,
6115 const char *name,
6116 enum built_in_class fnclass,
6117 tree fntype, tree libtype,
6118 bool both_p, bool fallback_p,
6119 bool nonansi_p ATTRIBUTE_UNUSED,
6120 tree fnattrs, bool implicit_p)
6121{
6122 tree decl;
6123 const char *libname;
6124
6125 /* Preserve an already installed decl. It most likely was setup in advance
6126 (e.g. as part of the internal builtins) for specific reasons. */
7c775aca 6127 if (builtin_decl_explicit (fncode))
a1ab4c31
AC
6128 return;
6129
6130 gcc_assert ((!both_p && !fallback_p)
6131 || !strncmp (name, "__builtin_",
6132 strlen ("__builtin_")));
6133
6134 libname = name + strlen ("__builtin_");
6135 decl = add_builtin_function (name, fntype, fncode, fnclass,
6136 (fallback_p ? libname : NULL),
6137 fnattrs);
6138 if (both_p)
6139 /* ??? This is normally further controlled by command-line options
6140 like -fno-builtin, but we don't have them for Ada. */
6141 add_builtin_function (libname, libtype, fncode, fnclass,
6142 NULL, fnattrs);
6143
e79983f4 6144 set_builtin_decl (fncode, decl, implicit_p);
a1ab4c31
AC
6145}
6146
6147static int flag_isoc94 = 0;
6148static int flag_isoc99 = 0;
22869a37 6149static int flag_isoc11 = 0;
a1ab4c31
AC
6150
6151/* Install what the common builtins.def offers. */
6152
6153static void
6154install_builtin_functions (void)
6155{
6156#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
6157 NONANSI_P, ATTRS, IMPLICIT, COND) \
6158 if (NAME && COND) \
6159 def_builtin_1 (ENUM, NAME, CLASS, \
6160 builtin_types[(int) TYPE], \
6161 builtin_types[(int) LIBTYPE], \
6162 BOTH_P, FALLBACK_P, NONANSI_P, \
6163 built_in_attributes[(int) ATTRS], IMPLICIT);
6164#include "builtins.def"
a1ab4c31
AC
6165}
6166
6167/* ----------------------------------------------------------------------- *
6168 * BUILTIN FUNCTIONS *
6169 * ----------------------------------------------------------------------- */
6170
6171/* Install the builtin functions we might need. */
6172
6173void
6174gnat_install_builtins (void)
6175{
6176 install_builtin_elementary_types ();
6177 install_builtin_function_types ();
6178 install_builtin_attributes ();
6179
6180 /* Install builtins used by generic middle-end pieces first. Some of these
6181 know about internal specificities and control attributes accordingly, for
6182 instance __builtin_alloca vs no-throw and -fstack-check. We will ignore
6183 the generic definition from builtins.def. */
384c400a 6184 build_common_builtin_nodes ();
a1ab4c31
AC
6185
6186 /* Now, install the target specific builtins, such as the AltiVec family on
6187 ppc, and the common set as exposed by builtins.def. */
6188 targetm.init_builtins ();
6189 install_builtin_functions ();
6190}
6191
6192#include "gt-ada-utils.h"
6193#include "gtype-ada.h"
This page took 3.291415 seconds and 5 git commands to generate.