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