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