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