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