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