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