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