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