Debian Codegraph @

gcc/ada/gcc-interface/utils.cc

raw
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * U T I L S *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2025, 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 "target.h"
30 #include "function.h"
31 #include "tree.h"
32 #include "stringpool.h"
33 #include "cgraph.h"
34 #include "diagnostic.h"
35 #include "alias.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
38 #include "attribs.h"
39 #include "varasm.h"
40 #include "toplev.h"
41 #include "opts.h"
42 #include "ipa-strub.h"
43 #include "output.h"
44 #include "debug.h"
45 #include "convert.h"
46 #include "common/common-target.h"
47 #include "langhooks.h"
48 #include "tree-dump.h"
49 #include "tree-inline.h"
50
51 #include "ada.h"
52 #include "types.h"
53 #include "atree.h"
54 #include "nlists.h"
55 #include "snames.h"
56 #include "uintp.h"
57 #include "fe.h"
58 #include "sinfo.h"
59 #include "einfo.h"
60 #include "ada-tree.h"
61 #include "gigi.h"
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 /* True if floating-point arithmetics may use wider intermediate results. */
77 bool fp_arith_may_widen = true;
78
79 /* Tree nodes for the various types and decls we create. */
80 tree gnat_std_decls[(int) ADT_LAST];
81
82 /* Functions to call for each of the possible raise reasons. */
83 tree gnat_raise_decls[(int) LAST_REASON_CODE + 1];
84
85 /* Likewise, but with extra info for each of the possible raise reasons. */
86 tree gnat_raise_decls_ext[(int) LAST_REASON_CODE + 1];
87
88 /* Forward declarations for handlers of attributes. */
89 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
90 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
91 static tree handle_expected_throw_attribute (tree *, tree, tree, int, bool *);
92 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
93 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
94 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
95 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
96 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
97 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
98 static tree handle_no_stack_protector_attribute (tree *, tree, tree, int, bool *);
99 static tree handle_strub_attribute (tree *, tree, tree, int, bool *);
100 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
101 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
102 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
103 static tree handle_noipa_attribute (tree *, tree, tree, int, bool *);
104 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
105 static tree handle_always_inline_attribute (tree *, tree, tree, int, bool *);
106 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
107 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
108 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
109 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
110 static tree handle_uninitialized_attribute (tree *, tree, tree, int, bool *);
111 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
112 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
113 static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
114 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
115 static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
116 static tree handle_vector_size_attribute (tree *, tree, tree, int, bool *);
117 static tree handle_vector_type_attribute (tree *, tree, tree, int, bool *);
118 static tree handle_zero_call_used_regs_attribute (tree *, tree, tree, int,
119 bool *);
120
121 static const struct attribute_spec::exclusions attr_cold_hot_exclusions[] =
122 {
123 { "cold", true, true, true },
124 { "hot" , true, true, true },
125 { NULL , false, false, false }
126 };
127
128 static const struct attribute_spec::exclusions attr_stack_protect_exclusions[] =
129 {
130 { "stack_protect", true, false, false },
131 { "no_stack_protector", true, false, false },
132 { NULL, false, false, false },
133 };
134
135 static const struct attribute_spec::exclusions attr_always_inline_exclusions[] =
136 {
137 { "noinline", true, true, true },
138 { "target_clones", true, true, true },
139 { NULL, false, false, false },
140 };
141
142 static const struct attribute_spec::exclusions attr_noinline_exclusions[] =
143 {
144 { "always_inline", true, true, true },
145 { NULL, false, false, false },
146 };
147
148 static const struct attribute_spec::exclusions attr_target_exclusions[] =
149 {
150 { "target_clones", TARGET_HAS_FMV_TARGET_ATTRIBUTE,
151 TARGET_HAS_FMV_TARGET_ATTRIBUTE, TARGET_HAS_FMV_TARGET_ATTRIBUTE },
152 { NULL, false, false, false },
153 };
154
155 static const struct attribute_spec::exclusions attr_target_clones_exclusions[] =
156 {
157 { "always_inline", true, true, true },
158 { "target", TARGET_HAS_FMV_TARGET_ATTRIBUTE, TARGET_HAS_FMV_TARGET_ATTRIBUTE,
159 TARGET_HAS_FMV_TARGET_ATTRIBUTE },
160 { NULL, false, false, false },
161 };
162
163 /* Fake handler for attributes we don't properly support, typically because
164 they'd require dragging a lot of the common-c front-end circuitry. */
165 static tree fake_attribute_handler (tree *, tree, tree, int, bool *);
166
167 /* Table of machine-independent internal attributes for Ada. We support
168 this minimal set of attributes to accommodate the needs of builtins. */
169 static const attribute_spec gnat_internal_attributes[] =
170 {
171 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
172 affects_type_identity, handler, exclude } */
173 { "const", 0, 0, true, false, false, false,
174 handle_const_attribute, NULL },
175 { "nothrow", 0, 0, true, false, false, false,
176 handle_nothrow_attribute, NULL },
177 { "expected_throw", 0, 0, true, false, false, false,
178 handle_expected_throw_attribute, NULL },
179 { "pure", 0, 0, true, false, false, false,
180 handle_pure_attribute, NULL },
181 { "no vops", 0, 0, true, false, false, false,
182 handle_novops_attribute, NULL },
183 { "nonnull", 0, -1, false, true, true, false,
184 handle_nonnull_attribute, NULL },
185 { "sentinel", 0, 1, false, true, true, false,
186 handle_sentinel_attribute, NULL },
187 { "noreturn", 0, 0, true, false, false, false,
188 handle_noreturn_attribute, NULL },
189 { "stack_protect",0, 0, true, false, false, false,
190 handle_stack_protect_attribute,
191 attr_stack_protect_exclusions },
192 { "no_stack_protector",0, 0, true, false, false, false,
193 handle_no_stack_protector_attribute,
194 attr_stack_protect_exclusions },
195 { "strub", 0, 1, false, true, false, true,
196 handle_strub_attribute, NULL },
197 { "noinline", 0, 0, true, false, false, false,
198 handle_noinline_attribute, attr_noinline_exclusions },
199 { "noclone", 0, 0, true, false, false, false,
200 handle_noclone_attribute, NULL },
201 { "no_icf", 0, 0, true, false, false, false,
202 handle_noicf_attribute, NULL },
203 { "noipa", 0, 0, true, false, false, false,
204 handle_noipa_attribute, NULL },
205 { "leaf", 0, 0, true, false, false, false,
206 handle_leaf_attribute, NULL },
207 { "always_inline",0, 0, true, false, false, false,
208 handle_always_inline_attribute, attr_always_inline_exclusions },
209 { "malloc", 0, 0, true, false, false, false,
210 handle_malloc_attribute, NULL },
211 { "type generic", 0, 0, false, true, true, false,
212 handle_type_generic_attribute, NULL },
213
214 { "flatten", 0, 0, true, false, false, false,
215 handle_flatten_attribute, NULL },
216 { "used", 0, 0, true, false, false, false,
217 handle_used_attribute, NULL },
218 { "uninitialized",0, 0, true, false, false, false,
219 handle_uninitialized_attribute, NULL },
220 { "cold", 0, 0, true, false, false, false,
221 handle_cold_attribute, attr_cold_hot_exclusions },
222 { "hot", 0, 0, true, false, false, false,
223 handle_hot_attribute, attr_cold_hot_exclusions },
224 { "simd", 0, 1, true, false, false, false,
225 handle_simd_attribute, NULL },
226 { "target", 1, -1, true, false, false, false,
227 handle_target_attribute, attr_target_exclusions },
228 { "target_clones",1, -1, true, false, false, false,
229 handle_target_clones_attribute, attr_target_clones_exclusions },
230
231 { "vector_size", 1, 1, false, true, false, false,
232 handle_vector_size_attribute, NULL },
233 { "vector_type", 0, 0, false, true, false, false,
234 handle_vector_type_attribute, NULL },
235 { "may_alias", 0, 0, false, true, false, false,
236 NULL, NULL },
237
238 { "zero_call_used_regs", 1, 1, true, false, false, false,
239 handle_zero_call_used_regs_attribute, NULL },
240
241 /* ??? format and format_arg are heavy and not supported, which actually
242 prevents support for stdio builtins, which we however declare as part
243 of the common builtins.def contents. */
244 { "format", 3, 3, false, true, true, false,
245 fake_attribute_handler, NULL },
246 { "format_arg", 1, 1, false, true, true, false,
247 fake_attribute_handler, NULL },
248
249 /* This is handled entirely in the front end. */
250 { "hardbool", 0, 0, false, true, false, true,
251 fake_attribute_handler, NULL },
252 };
253
254 const scoped_attribute_specs gnat_internal_attribute_table =
255 {
256 "gnu", { gnat_internal_attributes }
257 };
258
259 /* Associates a GNAT tree node to a GCC tree node. It is used in
260 `save_gnu_tree', `get_gnu_tree' and `present_gnu_tree'. See documentation
261 of `save_gnu_tree' for more info. */
262 static GTY((length ("max_gnat_nodes"))) tree *associate_gnat_to_gnu;
263
264 #define GET_GNU_TREE(GNAT_ENTITY) \
265 associate_gnat_to_gnu[(GNAT_ENTITY) - First_Node_Id]
266
267 #define SET_GNU_TREE(GNAT_ENTITY,VAL) \
268 associate_gnat_to_gnu[(GNAT_ENTITY) - First_Node_Id] = (VAL)
269
270 #define PRESENT_GNU_TREE(GNAT_ENTITY) \
271 (associate_gnat_to_gnu[(GNAT_ENTITY) - First_Node_Id] != NULL_TREE)
272
273 /* Associates a GNAT entity to a GCC tree node used as a dummy, if any. */
274 static GTY((length ("max_gnat_nodes"))) tree *dummy_node_table;
275
276 #define GET_DUMMY_NODE(GNAT_ENTITY) \
277 dummy_node_table[(GNAT_ENTITY) - First_Node_Id]
278
279 #define SET_DUMMY_NODE(GNAT_ENTITY,VAL) \
280 dummy_node_table[(GNAT_ENTITY) - First_Node_Id] = (VAL)
281
282 #define PRESENT_DUMMY_NODE(GNAT_ENTITY) \
283 (dummy_node_table[(GNAT_ENTITY) - First_Node_Id] != NULL_TREE)
284
285 /* This variable keeps a table for types for each precision so that we only
286 allocate each of them once. Signed and unsigned types are kept separate.
287
288 Note that these types are only used when fold-const requests something
289 special. Perhaps we should NOT share these types; we'll see how it
290 goes later. */
291 static GTY(()) tree signed_and_unsigned_types[2 * MAX_BITS_PER_WORD + 1][2];
292
293 /* Likewise for float types, but record these by mode. */
294 static GTY(()) tree float_types[NUM_MACHINE_MODES];
295
296 /* For each binding contour we allocate a binding_level structure to indicate
297 the binding depth. */
298
299 struct GTY((chain_next ("%h.chain"))) gnat_binding_level {
300 /* The binding level containing this one (the enclosing binding level). */
301 struct gnat_binding_level *chain;
302 /* The BLOCK node for this level. */
303 tree block;
304 };
305
306 /* The binding level currently in effect. */
307 static GTY(()) struct gnat_binding_level *current_binding_level;
308
309 /* A chain of gnat_binding_level structures awaiting reuse. */
310 static GTY((deletable)) struct gnat_binding_level *free_binding_level;
311
312 /* The context to be used for global declarations. */
313 static GTY(()) tree global_context;
314
315 /* An array of global declarations. */
316 static GTY(()) vec<tree, va_gc> *global_decls;
317
318 /* An array of builtin function declarations. */
319 static GTY(()) vec<tree, va_gc> *builtin_decls;
320
321 /* A chain of unused BLOCK nodes. */
322 static GTY((deletable)) tree free_block_chain;
323
324 /* A hash table of packable types. It is modelled on the generic type
325 hash table in tree.cc, which must thus be used as a reference. */
326
327 struct GTY((for_user)) packable_type_hash
328 {
329 hashval_t hash;
330 tree type;
331 };
332
333 struct packable_type_hasher : ggc_cache_ptr_hash<packable_type_hash>
334 {
335 static inline hashval_t hash (packable_type_hash *t) { return t->hash; }
336 static bool equal (packable_type_hash *a, packable_type_hash *b);
337
338 static int
339 keep_cache_entry (packable_type_hash *&t)
340 {
341 return ggc_marked_p (t->type);
342 }
343 };
344
345 static GTY ((cache)) hash_table<packable_type_hasher> *packable_type_hash_table;
346
347 /* A hash table of padded types. It is modelled on the generic type
348 hash table in tree.cc, which must thus be used as a reference. */
349
350 struct GTY((for_user)) pad_type_hash
351 {
352 hashval_t hash;
353 tree type;
354 };
355
356 struct pad_type_hasher : ggc_cache_ptr_hash<pad_type_hash>
357 {
358 static inline hashval_t hash (pad_type_hash *t) { return t->hash; }
359 static bool equal (pad_type_hash *a, pad_type_hash *b);
360
361 static int
362 keep_cache_entry (pad_type_hash *&t)
363 {
364 return ggc_marked_p (t->type);
365 }
366 };
367
368 static GTY ((cache)) hash_table<pad_type_hasher> *pad_type_hash_table;
369
370 struct GTY((for_user)) sized_type_hash
371 {
372 hashval_t hash;
373 tree type;
374 };
375
376 struct sized_type_hasher : ggc_cache_ptr_hash<sized_type_hash>
377 {
378 static inline hashval_t hash (sized_type_hash *t) { return t->hash; }
379 static bool equal (sized_type_hash *a, sized_type_hash *b);
380
381 static int
382 keep_cache_entry (sized_type_hash *&t)
383 {
384 return ggc_marked_p (t->type);
385 }
386 };
387
388 static GTY ((cache)) hash_table<sized_type_hasher> *sized_type_hash_table;
389
390 static tree merge_sizes (tree, tree, tree, bool, bool);
391 static tree fold_bit_position (const_tree);
392 static tree compute_related_constant (tree, tree);
393 static tree split_plus (tree, tree *);
394 static tree float_type_for_precision (int, machine_mode);
395 static tree convert_to_fat_pointer (tree, tree);
396 static unsigned int scale_by_factor_of (tree, unsigned int);
397
398 /* Linked list used as a queue to defer the initialization of the DECL_CONTEXT
399 of ..._DECL nodes and of the TYPE_CONTEXT of ..._TYPE nodes. */
400 struct deferred_decl_context_node
401 {
402 /* The ..._DECL node to work on. */
403 tree decl;
404
405 /* The corresponding entity's Scope. */
406 Entity_Id gnat_scope;
407
408 /* The value of force_global when DECL was pushed. */
409 int force_global;
410
411 /* The list of ..._TYPE nodes to propagate the context to. */
412 vec<tree> types;
413
414 /* The next queue item. */
415 struct deferred_decl_context_node *next;
416 };
417
418 static struct deferred_decl_context_node *deferred_decl_context_queue = NULL;
419
420 /* Defer the initialization of DECL's DECL_CONTEXT attribute, scheduling to
421 feed it with the elaboration of GNAT_SCOPE. */
422 static struct deferred_decl_context_node *
423 add_deferred_decl_context (tree decl, Entity_Id gnat_scope, int force_global);
424
425 /* Defer the initialization of TYPE's TYPE_CONTEXT attribute, scheduling to
426 feed it with the DECL_CONTEXT computed as part of N as soon as it is
427 computed. */
428 static void add_deferred_type_context (struct deferred_decl_context_node *n,
429 tree type);
430
431 /* Initialize data structures of the utils.cc module. */
432
433 void
434 init_gnat_utils (void)
435 {
436 /* Initialize the association of GNAT nodes to GCC trees. */
437 associate_gnat_to_gnu = ggc_cleared_vec_alloc<tree> (max_gnat_nodes);
438
439 /* Initialize the association of GNAT nodes to GCC trees as dummies. */
440 dummy_node_table = ggc_cleared_vec_alloc<tree> (max_gnat_nodes);
441
442 /* Initialize the hash table of packable types. */
443 packable_type_hash_table = hash_table<packable_type_hasher>::create_ggc (512);
444
445 /* Initialize the hash table of padded types. */
446 pad_type_hash_table = hash_table<pad_type_hasher>::create_ggc (512);
447
448 /* Initialize the hash table of sized types. */
449 sized_type_hash_table = hash_table<sized_type_hasher>::create_ggc (512);
450 }
451
452 /* Destroy data structures of the utils.cc module. */
453
454 void
455 destroy_gnat_utils (void)
456 {
457 /* Destroy the association of GNAT nodes to GCC trees. */
458 ggc_free (associate_gnat_to_gnu);
459 associate_gnat_to_gnu = NULL;
460
461 /* Destroy the association of GNAT nodes to GCC trees as dummies. */
462 ggc_free (dummy_node_table);
463 dummy_node_table = NULL;
464
465 /* Destroy the hash table of packable types. */
466 packable_type_hash_table->empty ();
467 packable_type_hash_table = NULL;
468
469 /* Destroy the hash table of padded types. */
470 pad_type_hash_table->empty ();
471 pad_type_hash_table = NULL;
472
473 /* Destroy the hash table of sized types. */
474 sized_type_hash_table->empty ();
475 sized_type_hash_table = NULL;
476 }
477
478 /* GNAT_ENTITY is a GNAT tree node for an entity. Associate GNU_DECL, a GCC
479 tree node, with GNAT_ENTITY. If GNU_DECL is not a ..._DECL node, abort.
480 If NO_CHECK is true, the latter check is suppressed.
481
482 If GNU_DECL is zero, reset a previous association. */
483
484 void
485 save_gnu_tree (Entity_Id gnat_entity, tree gnu_decl, bool no_check)
486 {
487 /* Check that GNAT_ENTITY is not already defined and that it is being set
488 to something which is a decl. If that is not the case, this usually
489 means GNAT_ENTITY is defined twice, but occasionally is due to some
490 Gigi problem. */
491 gcc_assert (!(gnu_decl
492 && (PRESENT_GNU_TREE (gnat_entity)
493 || (!no_check && !DECL_P (gnu_decl)))));
494
495 SET_GNU_TREE (gnat_entity, gnu_decl);
496 }
497
498 /* GNAT_ENTITY is a GNAT tree node for an entity. Return the GCC tree node
499 that was associated with it. If there is no such tree node, abort.
500
501 In some cases, such as delayed elaboration or expressions that need to
502 be elaborated only once, GNAT_ENTITY is really not an entity. */
503
504 tree
505 get_gnu_tree (Entity_Id gnat_entity)
506 {
507 gcc_assert (PRESENT_GNU_TREE (gnat_entity));
508 return GET_GNU_TREE (gnat_entity);
509 }
510
511 /* Return nonzero if a GCC tree has been associated with GNAT_ENTITY. */
512
513 bool
514 present_gnu_tree (Entity_Id gnat_entity)
515 {
516 return PRESENT_GNU_TREE (gnat_entity);
517 }
518
519 /* Make a dummy type corresponding to GNAT_TYPE. */
520
521 tree
522 make_dummy_type (Entity_Id gnat_type)
523 {
524 Entity_Id gnat_equiv = Gigi_Equivalent_Type (Underlying_Type (gnat_type));
525 tree gnu_type, debug_type;
526
527 /* If there was no equivalent type (can only happen when just annotating
528 types) or underlying type, go back to the original type. */
529 if (No (gnat_equiv))
530 gnat_equiv = gnat_type;
531
532 /* If there is already a dummy type, use that one. Else make one. */
533 if (PRESENT_DUMMY_NODE (gnat_equiv))
534 return GET_DUMMY_NODE (gnat_equiv);
535
536 /* If this is a record, make a RECORD_TYPE or UNION_TYPE; else make
537 an ENUMERAL_TYPE. */
538 gnu_type = make_node (Is_Record_Type (gnat_equiv)
539 ? tree_code_for_record_type (gnat_equiv)
540 : ENUMERAL_TYPE);
541 TYPE_NAME (gnu_type) = get_entity_name (gnat_type);
542 TYPE_DUMMY_P (gnu_type) = 1;
543 TYPE_STUB_DECL (gnu_type)
544 = create_type_stub_decl (TYPE_NAME (gnu_type), gnu_type);
545 if (Is_By_Reference_Type (gnat_equiv))
546 TYPE_BY_REFERENCE_P (gnu_type) = 1;
547 if (Has_Discriminants (gnat_equiv))
548 decl_attributes (&gnu_type,
549 tree_cons (get_identifier ("may_alias"), NULL_TREE,
550 NULL_TREE),
551 ATTR_FLAG_TYPE_IN_PLACE);
552
553 SET_DUMMY_NODE (gnat_equiv, gnu_type);
554
555 /* Create a debug type so that debuggers only see an unspecified type. */
556 if (Needs_Debug_Info (gnat_type))
557 {
558 debug_type = make_node (LANG_TYPE);
559 TYPE_NAME (debug_type) = TYPE_NAME (gnu_type);
560 TYPE_ARTIFICIAL (debug_type) = TYPE_ARTIFICIAL (gnu_type);
561 SET_TYPE_DEBUG_TYPE (gnu_type, debug_type);
562 }
563
564 return gnu_type;
565 }
566
567 /* Return the dummy type that was made for GNAT_TYPE, if any. */
568
569 tree
570 get_dummy_type (Entity_Id gnat_type)
571 {
572 return GET_DUMMY_NODE (gnat_type);
573 }
574
575 /* Build dummy fat and thin pointer types whose designated type is specified
576 by GNAT_DESIG_TYPE/GNU_DESIG_TYPE and attach them to the latter. */
577
578 void
579 build_dummy_unc_pointer_types (Entity_Id gnat_desig_type, tree gnu_desig_type)
580 {
581 tree gnu_template_type, gnu_ptr_template, gnu_array_type, gnu_ptr_array;
582 tree gnu_fat_type, fields, gnu_object_type;
583
584 gnu_template_type = make_node (RECORD_TYPE);
585 TYPE_NAME (gnu_template_type) = create_concat_name (gnat_desig_type, "XUB");
586 TYPE_DUMMY_P (gnu_template_type) = 1;
587 gnu_ptr_template = build_pointer_type (gnu_template_type);
588
589 gnu_array_type = make_node (ENUMERAL_TYPE);
590 TYPE_NAME (gnu_array_type) = create_concat_name (gnat_desig_type, "XUA");
591 TYPE_DUMMY_P (gnu_array_type) = 1;
592 gnu_ptr_array = build_pointer_type (gnu_array_type);
593
594 gnu_fat_type = make_node (RECORD_TYPE);
595 /* Build a stub DECL to trigger the special processing for fat pointer types
596 in gnat_pushdecl. */
597 TYPE_NAME (gnu_fat_type)
598 = create_type_stub_decl (create_concat_name (gnat_desig_type, "XUP"),
599 gnu_fat_type);
600 fields = create_field_decl (get_identifier ("P_ARRAY"), gnu_ptr_array,
601 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 1);
602 DECL_CHAIN (fields)
603 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
604 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 1);
605 finish_fat_pointer_type (gnu_fat_type, fields);
606 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_desig_type);
607 /* Suppress debug info until after the type is completed. */
608 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (gnu_fat_type)) = 1;
609
610 gnu_object_type = make_node (RECORD_TYPE);
611 TYPE_NAME (gnu_object_type) = create_concat_name (gnat_desig_type, "XUT");
612 TYPE_DUMMY_P (gnu_object_type) = 1;
613
614 TYPE_POINTER_TO (gnu_desig_type) = gnu_fat_type;
615 TYPE_REFERENCE_TO (gnu_desig_type) = gnu_fat_type;
616 TYPE_OBJECT_RECORD_TYPE (gnu_desig_type) = gnu_object_type;
617 }
618
619 /* Return true if we are in the global binding level. */
620
621 bool
622 global_bindings_p (void)
623 {
624 return force_global || !current_function_decl;
625 }
626
627 /* Enter a new binding level. */
628
629 void
630 gnat_pushlevel (void)
631 {
632 struct gnat_binding_level *newlevel = NULL;
633
634 /* Reuse a struct for this binding level, if there is one. */
635 if (free_binding_level)
636 {
637 newlevel = free_binding_level;
638 free_binding_level = free_binding_level->chain;
639 }
640 else
641 newlevel = ggc_alloc<gnat_binding_level> ();
642
643 /* Use a free BLOCK, if any; otherwise, allocate one. */
644 if (free_block_chain)
645 {
646 newlevel->block = free_block_chain;
647 free_block_chain = BLOCK_CHAIN (free_block_chain);
648 BLOCK_CHAIN (newlevel->block) = NULL_TREE;
649 }
650 else
651 newlevel->block = make_node (BLOCK);
652
653 /* Point the BLOCK we just made to its parent. */
654 if (current_binding_level)
655 BLOCK_SUPERCONTEXT (newlevel->block) = current_binding_level->block;
656
657 BLOCK_VARS (newlevel->block) = NULL_TREE;
658 BLOCK_SUBBLOCKS (newlevel->block) = NULL_TREE;
659 TREE_USED (newlevel->block) = 1;
660
661 /* Add this level to the front of the chain (stack) of active levels. */
662 newlevel->chain = current_binding_level;
663 current_binding_level = newlevel;
664 }
665
666 /* Set SUPERCONTEXT of the BLOCK for the current binding level to FNDECL
667 and point FNDECL to this BLOCK. */
668
669 void
670 set_current_block_context (tree fndecl)
671 {
672 BLOCK_SUPERCONTEXT (current_binding_level->block) = fndecl;
673 DECL_INITIAL (fndecl) = current_binding_level->block;
674 set_block_for_group (current_binding_level->block);
675 }
676
677 /* Exit a binding level. Set any BLOCK into the current code group. */
678
679 void
680 gnat_poplevel (void)
681 {
682 struct gnat_binding_level *level = current_binding_level;
683 tree block = level->block;
684
685 BLOCK_VARS (block) = nreverse (BLOCK_VARS (block));
686 BLOCK_SUBBLOCKS (block) = blocks_nreverse (BLOCK_SUBBLOCKS (block));
687
688 /* If this is a function-level BLOCK don't do anything. Otherwise, if there
689 are no variables free the block and merge its subblocks into those of its
690 parent block. Otherwise, add it to the list of its parent. */
691 if (TREE_CODE (BLOCK_SUPERCONTEXT (block)) == FUNCTION_DECL)
692 ;
693 else if (!BLOCK_VARS (block))
694 {
695 BLOCK_SUBBLOCKS (level->chain->block)
696 = block_chainon (BLOCK_SUBBLOCKS (block),
697 BLOCK_SUBBLOCKS (level->chain->block));
698 BLOCK_CHAIN (block) = free_block_chain;
699 free_block_chain = block;
700 }
701 else
702 {
703 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (level->chain->block);
704 BLOCK_SUBBLOCKS (level->chain->block) = block;
705 TREE_USED (block) = 1;
706 set_block_for_group (block);
707 }
708
709 /* Free this binding structure. */
710 current_binding_level = level->chain;
711 level->chain = free_binding_level;
712 free_binding_level = level;
713 }
714
715 /* Exit a binding level and discard the associated BLOCK. */
716
717 void
718 gnat_zaplevel (void)
719 {
720 struct gnat_binding_level *level = current_binding_level;
721 tree block = level->block;
722
723 BLOCK_CHAIN (block) = free_block_chain;
724 free_block_chain = block;
725
726 /* Free this binding structure. */
727 current_binding_level = level->chain;
728 level->chain = free_binding_level;
729 free_binding_level = level;
730 }
731
732 /* Set the context of TYPE and its parallel types (if any) to CONTEXT. */
733
734 static void
735 gnat_set_type_context (tree type, tree context)
736 {
737 tree decl = TYPE_STUB_DECL (type);
738
739 TYPE_CONTEXT (type) = context;
740
741 while (decl && DECL_PARALLEL_TYPE (decl))
742 {
743 tree parallel_type = DECL_PARALLEL_TYPE (decl);
744
745 /* Give a context to the parallel types and their stub decl, if any.
746 Some parallel types seems to be present in multiple parallel type
747 chains, so don't mess with their context if they already have one. */
748 if (!TYPE_CONTEXT (parallel_type))
749 {
750 if (TYPE_STUB_DECL (parallel_type))
751 DECL_CONTEXT (TYPE_STUB_DECL (parallel_type)) = context;
752 TYPE_CONTEXT (parallel_type) = context;
753 }
754
755 decl = TYPE_STUB_DECL (DECL_PARALLEL_TYPE (decl));
756 }
757 }
758
759 /* Return the innermost scope, starting at GNAT_NODE, we are be interested in
760 the debug info, or Empty if there is no such scope. If not NULL, set
761 IS_SUBPROGRAM to whether the returned entity is a subprogram. */
762
763 Entity_Id
764 get_debug_scope (Node_Id gnat_node, bool *is_subprogram)
765 {
766 Entity_Id gnat_entity;
767
768 if (is_subprogram)
769 *is_subprogram = false;
770
771 if (Nkind (gnat_node) == N_Defining_Identifier
772 || Nkind (gnat_node) == N_Defining_Operator_Symbol)
773 gnat_entity = Scope (gnat_node);
774 else
775 return Empty;
776
777 while (Present (gnat_entity))
778 {
779 switch (Ekind (gnat_entity))
780 {
781 case E_Function:
782 case E_Procedure:
783 if (Present (Protected_Body_Subprogram (gnat_entity)))
784 gnat_entity = Protected_Body_Subprogram (gnat_entity);
785
786 /* If the scope is a subprogram, then just rely on
787 current_function_decl, so that we don't have to defer
788 anything. This is needed because other places rely on the
789 validity of the DECL_CONTEXT attribute of FUNCTION_DECL nodes. */
790 if (is_subprogram)
791 *is_subprogram = true;
792 return gnat_entity;
793
794 case E_Record_Type:
795 case E_Record_Subtype:
796 return gnat_entity;
797
798 default:
799 /* By default, we are not interested in this particular scope: go to
800 the outer one. */
801 break;
802 }
803
804 gnat_entity = Scope (gnat_entity);
805 }
806
807 return Empty;
808 }
809
810 /* If N is NULL, set TYPE's context to CONTEXT. Defer this to the processing
811 of N otherwise. */
812
813 static void
814 defer_or_set_type_context (tree type, tree context,
815 struct deferred_decl_context_node *n)
816 {
817 if (n)
818 add_deferred_type_context (n, type);
819 else
820 gnat_set_type_context (type, context);
821 }
822
823 /* Return global_context, but create it first if need be. */
824
825 static tree
826 get_global_context (void)
827 {
828 if (!global_context)
829 {
830 global_context
831 = build_translation_unit_decl (get_identifier (main_input_filename));
832 debug_hooks->register_main_translation_unit (global_context);
833 }
834
835 return global_context;
836 }
837
838 /* Record DECL as belonging to the current lexical scope and use GNAT_NODE
839 for location information and flag propagation. */
840
841 void
842 gnat_pushdecl (tree decl, Node_Id gnat_node)
843 {
844 tree context = NULL_TREE;
845 struct deferred_decl_context_node *deferred_decl_context = NULL;
846
847 /* If explicitly asked to make DECL global or if it's an imported nested
848 object, short-circuit the regular Scope-based context computation. */
849 if (!((TREE_PUBLIC (decl) && DECL_EXTERNAL (decl)) || force_global == 1))
850 {
851 /* Rely on the GNAT scope, or fallback to the current_function_decl if
852 the GNAT scope reached the global scope, if it reached a subprogram
853 or the declaration is a subprogram or a variable (for them we skip
854 intermediate context types because the subprogram body elaboration
855 machinery and the inliner both expect a subprogram context).
856
857 Falling back to current_function_decl is necessary for implicit
858 subprograms created by gigi, such as the elaboration subprograms. */
859 bool context_is_subprogram = false;
860 const Entity_Id gnat_scope
861 = get_debug_scope (gnat_node, &context_is_subprogram);
862
863 if (Present (gnat_scope)
864 && !context_is_subprogram
865 && TREE_CODE (decl) != FUNCTION_DECL
866 && TREE_CODE (decl) != VAR_DECL)
867 /* Always assume the scope has not been elaborated, thus defer the
868 context propagation to the time its elaboration will be
869 available. */
870 deferred_decl_context
871 = add_deferred_decl_context (decl, gnat_scope, force_global);
872
873 /* External declarations (when force_global > 0) may not be in a
874 local context. */
875 else if (current_function_decl && force_global == 0)
876 context = current_function_decl;
877 }
878
879 /* If either we are forced to be in global mode or if both the GNAT scope and
880 the current_function_decl did not help in determining the context, use the
881 global scope. */
882 if (!deferred_decl_context && !context)
883 context = get_global_context ();
884
885 /* Mark functions really nested in another function, that is to say defined
886 there as opposed to imported from elsewhere, as initially needing a static
887 chain for the sake of uniformity (lower_nested_functions will recompute it
888 exacly later) and as private to the translation unit (the static chain may
889 be clobbered by calling conventions used across translation units). */
890 if (TREE_CODE (decl) == FUNCTION_DECL
891 && !DECL_EXTERNAL (decl)
892 && context
893 && (TREE_CODE (context) == FUNCTION_DECL
894 || decl_function_context (context)))
895 {
896 DECL_STATIC_CHAIN (decl) = 1;
897 TREE_PUBLIC (decl) = 0;
898 }
899
900 if (!deferred_decl_context)
901 DECL_CONTEXT (decl) = context;
902
903 /* Disable warnings for compiler-generated entities or explicit request. */
904 if (No (gnat_node)
905 || !Comes_From_Source (gnat_node)
906 || Warnings_Off (gnat_node))
907 suppress_warning (decl);
908
909 /* Set the location of DECL and emit a declaration for it. */
910 if (Present (gnat_node) && !renaming_from_instantiation_p (gnat_node))
911 Sloc_to_locus (Sloc (gnat_node), &DECL_SOURCE_LOCATION (decl));
912
913 add_decl_expr (decl, gnat_node);
914
915 /* Put the declaration on the list. The list of declarations is in reverse
916 order. The list will be reversed later. Put global declarations in the
917 globals list and local ones in the current block. But skip TYPE_DECLs
918 for UNCONSTRAINED_ARRAY_TYPE in both cases, as they will cause trouble
919 with the debugger and aren't needed anyway. */
920 if (!(TREE_CODE (decl) == TYPE_DECL
921 && TREE_CODE (TREE_TYPE (decl)) == UNCONSTRAINED_ARRAY_TYPE))
922 {
923 /* External declarations must go to the binding level they belong to.
924 This will make corresponding imported entities are available in the
925 debugger at the proper time. */
926 if (DECL_EXTERNAL (decl)
927 && TREE_CODE (decl) == FUNCTION_DECL
928 && fndecl_built_in_p (decl))
929 vec_safe_push (builtin_decls, decl);
930 else if (global_bindings_p ())
931 vec_safe_push (global_decls, decl);
932 else
933 {
934 DECL_CHAIN (decl) = BLOCK_VARS (current_binding_level->block);
935 BLOCK_VARS (current_binding_level->block) = decl;
936 }
937 }
938
939 /* Pointer types aren't named types in the C sense so we need to generate a
940 typedef in DWARF for them. Also do that for fat pointer types because,
941 even though they are named types in the C sense, they are still the XUP
942 types created for the base array type at this point. */
943 #define TYPE_IS_POINTER_P(NODE) \
944 (TREE_CODE (NODE) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (NODE))
945
946 /* For the declaration of a type, set its name either if it isn't already
947 set or if the previous type name was not derived from a source name.
948 We'd rather have the type named with a real name and all the pointer
949 types to the same object have the same node, except when the names are
950 both derived from source names. */
951 if (TREE_CODE (decl) == TYPE_DECL && DECL_NAME (decl))
952 {
953 tree t = TREE_TYPE (decl);
954
955 /* For pointer types, make sure the typedef is generated and preserved
956 in DWARF, unless the type is artificial. */
957 if (!(TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
958 && (!TYPE_IS_POINTER_P (t) || DECL_ARTIFICIAL (decl)))
959 ;
960 /* For pointer types, create the DECL_ORIGINAL_TYPE that will generate
961 the typedef in DWARF. */
962 else if (TYPE_IS_POINTER_P (t) && !DECL_ARTIFICIAL (decl))
963 {
964 tree tt = build_variant_type_copy (t);
965 TYPE_NAME (tt) = decl;
966 defer_or_set_type_context (tt,
967 DECL_CONTEXT (decl),
968 deferred_decl_context);
969 TREE_TYPE (decl) = tt;
970 if (TYPE_NAME (t)
971 && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
972 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
973 DECL_ORIGINAL_TYPE (decl) = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
974 else
975 DECL_ORIGINAL_TYPE (decl) = t;
976 /* Remark the canonical fat pointer type as artificial. */
977 if (TYPE_IS_FAT_POINTER_P (t))
978 TYPE_ARTIFICIAL (t) = 1;
979 t = NULL_TREE;
980 }
981 else if (TYPE_NAME (t)
982 && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
983 && DECL_ARTIFICIAL (TYPE_NAME (t)) && !DECL_ARTIFICIAL (decl))
984 ;
985 else
986 t = NULL_TREE;
987
988 /* Propagate the name to all the variants, this is needed for the type
989 qualifiers machinery to work properly (see check_qualified_type).
990 Also propagate the context to them. Note that it will be propagated
991 to all parallel types too thanks to gnat_set_type_context. */
992 if (t)
993 for (t = TYPE_MAIN_VARIANT (t); t; t = TYPE_NEXT_VARIANT (t))
994 /* Skip it for pointer types to preserve the typedef. */
995 if (!(TYPE_IS_POINTER_P (t)
996 && TYPE_NAME (t)
997 && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL))
998 {
999 TYPE_NAME (t) = decl;
1000 defer_or_set_type_context (t,
1001 DECL_CONTEXT (decl),
1002 deferred_decl_context);
1003 }
1004 }
1005
1006 #undef TYPE_IS_POINTER_P
1007 }
1008
1009 /* Create a record type that contains a SIZE bytes long field of TYPE with a
1010 starting bit position so that it is aligned to ALIGN bits, and leaving at
1011 least ROOM bytes free before the field. BASE_ALIGN is the alignment the
1012 record is guaranteed to get. GNAT_NODE is used for the position of the
1013 associated TYPE_DECL. */
1014
1015 tree
1016 make_aligning_type (tree type, unsigned int align, tree size,
1017 unsigned int base_align, int room, Node_Id gnat_node)
1018 {
1019 /* We will be crafting a record type with one field at a position set to be
1020 the next multiple of ALIGN past record'address + room bytes. We use a
1021 record placeholder to express record'address. */
1022 tree record_type = make_node (RECORD_TYPE);
1023 tree record = build0 (PLACEHOLDER_EXPR, record_type);
1024
1025 tree record_addr_st
1026 = convert (sizetype, build_unary_op (ADDR_EXPR, NULL_TREE, record));
1027
1028 /* The diagram below summarizes the shape of what we manipulate:
1029
1030 <--------- pos ---------->
1031 { +------------+-------------+-----------------+
1032 record =>{ |############| ... | field (type) |
1033 { +------------+-------------+-----------------+
1034 |<-- room -->|<- voffset ->|<---- size ----->|
1035 o o
1036 | |
1037 record_addr vblock_addr
1038
1039 Every length is in sizetype bytes there, except "pos" which has to be
1040 set as a bit position in the GCC tree for the record. */
1041 tree room_st = size_int (room);
1042 tree vblock_addr_st = size_binop (PLUS_EXPR, record_addr_st, room_st);
1043 tree voffset_st, pos, field;
1044
1045 tree name = TYPE_IDENTIFIER (type);
1046
1047 name = concat_name (name, "ALIGN");
1048 TYPE_NAME (record_type) = name;
1049
1050 /* Compute VOFFSET and then POS. The next byte position multiple of some
1051 alignment after some address is obtained by "and"ing the alignment minus
1052 1 with the two's complement of the address. */
1053 voffset_st = size_binop (BIT_AND_EXPR,
1054 fold_build1 (NEGATE_EXPR, sizetype, vblock_addr_st),
1055 size_int ((align / BITS_PER_UNIT) - 1));
1056
1057 /* POS = (ROOM + VOFFSET) * BIT_PER_UNIT, in bitsizetype. */
1058 pos = size_binop (MULT_EXPR,
1059 convert (bitsizetype,
1060 size_binop (PLUS_EXPR, room_st, voffset_st)),
1061 bitsize_unit_node);
1062
1063 /* Craft the GCC record representation. We exceptionally do everything
1064 manually here because 1) our generic circuitry is not quite ready to
1065 handle the complex position/size expressions we are setting up, 2) we
1066 have a strong simplifying factor at hand: we know the maximum possible
1067 value of voffset, and 3) we have to set/reset at least the sizes in
1068 accordance with this maximum value anyway, as we need them to convey
1069 what should be "alloc"ated for this type.
1070
1071 Use -1 as the 'addressable' indication for the field to prevent the
1072 creation of a bitfield. We don't need one, it would have damaging
1073 consequences on the alignment computation, and create_field_decl would
1074 make one without this special argument, for instance because of the
1075 complex position expression. */
1076 field = create_field_decl (get_identifier ("F"), type, record_type, size,
1077 pos, 1, -1);
1078 TYPE_FIELDS (record_type) = field;
1079
1080 SET_TYPE_ALIGN (record_type, base_align);
1081 TYPE_USER_ALIGN (record_type) = 1;
1082
1083 TYPE_SIZE (record_type)
1084 = size_binop (PLUS_EXPR,
1085 size_binop (MULT_EXPR, convert (bitsizetype, size),
1086 bitsize_unit_node),
1087 bitsize_int (align + room * BITS_PER_UNIT));
1088 TYPE_SIZE_UNIT (record_type)
1089 = size_binop (PLUS_EXPR, size,
1090 size_int (room + align / BITS_PER_UNIT));
1091
1092 SET_TYPE_MODE (record_type, BLKmode);
1093 relate_alias_sets (record_type, type, ALIAS_SET_COPY);
1094
1095 /* Declare it now since it will never be declared otherwise. This is
1096 necessary to ensure that its subtrees are properly marked. */
1097 create_type_decl (name, record_type, true, false, gnat_node);
1098
1099 return record_type;
1100 }
1101
1102 /* Return true iff the packable types are equivalent. */
1103
1104 bool
1105 packable_type_hasher::equal (packable_type_hash *t1, packable_type_hash *t2)
1106 {
1107 tree type1, type2;
1108
1109 if (t1->hash != t2->hash)
1110 return 0;
1111
1112 type1 = t1->type;
1113 type2 = t2->type;
1114
1115 /* We consider that packable types are equivalent if they have the same name,
1116 size, alignment, RM size and storage order. Taking the mode into account
1117 is redundant since it is determined by the others. */
1118 return
1119 TYPE_NAME (type1) == TYPE_NAME (type2)
1120 && TYPE_SIZE (type1) == TYPE_SIZE (type2)
1121 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2)
1122 && TYPE_ADA_SIZE (type1) == TYPE_ADA_SIZE (type2)
1123 && TYPE_REVERSE_STORAGE_ORDER (type1) == TYPE_REVERSE_STORAGE_ORDER (type2);
1124 }
1125
1126 /* Compute the hash value for the packable TYPE. */
1127
1128 static hashval_t
1129 hash_packable_type (tree type)
1130 {
1131 hashval_t hashcode;
1132
1133 hashcode = iterative_hash_expr (TYPE_NAME (type), 0);
1134 hashcode = iterative_hash_expr (TYPE_SIZE (type), hashcode);
1135 hashcode = iterative_hash_hashval_t (TYPE_ALIGN (type), hashcode);
1136 hashcode = iterative_hash_expr (TYPE_ADA_SIZE (type), hashcode);
1137 hashcode
1138 = iterative_hash_hashval_t (TYPE_REVERSE_STORAGE_ORDER (type), hashcode);
1139
1140 return hashcode;
1141 }
1142
1143 /* Look up the packable TYPE in the hash table and return its canonical version
1144 if it exists; otherwise, insert it into the hash table. */
1145
1146 static tree
1147 canonicalize_packable_type (tree type)
1148 {
1149 const hashval_t hashcode = hash_packable_type (type);
1150 struct packable_type_hash in, *h, **slot;
1151
1152 in.hash = hashcode;
1153 in.type = type;
1154 slot = packable_type_hash_table->find_slot_with_hash (&in, hashcode, INSERT);
1155 h = *slot;
1156 if (!h)
1157 {
1158 h = ggc_alloc<packable_type_hash> ();
1159 h->hash = hashcode;
1160 h->type = type;
1161 *slot = h;
1162 }
1163
1164 return h->type;
1165 }
1166
1167 /* TYPE is an ARRAY_TYPE that is being used as the type of a field in a packed
1168 record. See if we can rewrite it as a type that has non-BLKmode, which we
1169 can pack tighter in the packed record. If so, return the new type; if not,
1170 return the original type. */
1171
1172 static tree
1173 make_packable_array_type (tree type)
1174 {
1175 const unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE (type));
1176 unsigned HOST_WIDE_INT new_size;
1177 unsigned int new_align;
1178
1179 /* No point in doing anything if the size is either zero or too large for an
1180 integral mode, or if the type already has non-BLKmode. */
1181 if (size == 0 || size > MAX_FIXED_MODE_SIZE || TYPE_MODE (type) != BLKmode)
1182 return type;
1183
1184 /* Punt if the component type is an aggregate type for now. */
1185 if (AGGREGATE_TYPE_P (TREE_TYPE (type)))
1186 return type;
1187
1188 tree new_type = copy_type (type);
1189
1190 new_size = ceil_pow2 (size);
1191 new_align = MIN (new_size, BIGGEST_ALIGNMENT);
1192 SET_TYPE_ALIGN (new_type, new_align);
1193
1194 TYPE_SIZE (new_type) = bitsize_int (new_size);
1195 TYPE_SIZE_UNIT (new_type) = size_int (new_size / BITS_PER_UNIT);
1196
1197 SET_TYPE_MODE (new_type, mode_for_size (new_size, MODE_INT, 1).else_blk ());
1198
1199 return new_type;
1200 }
1201
1202 /* TYPE is a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE that is being used
1203 as the type of a field in a packed record if IN_RECORD is true, or as
1204 the component type of a packed array if IN_RECORD is false. See if we
1205 can rewrite it either as a type that has non-BLKmode, which we can pack
1206 tighter in the packed record case, or as a smaller type with at most
1207 MAX_ALIGN alignment if the value is non-zero. If so, return the new
1208 type; if not, return the original type. */
1209
1210 tree
1211 make_packable_type (tree type, bool in_record, unsigned int max_align)
1212 {
1213 const unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE (type));
1214 const unsigned int align = TYPE_ALIGN (type);
1215 unsigned HOST_WIDE_INT new_size;
1216 unsigned int new_align;
1217
1218 /* No point in doing anything if the size is zero. */
1219 if (size == 0)
1220 return type;
1221
1222 tree new_type = make_node (TREE_CODE (type));
1223
1224 /* Copy the name and flags from the old type to that of the new.
1225 Note that we rely on the pointer equality created here for
1226 TYPE_NAME to look through conversions in various places. */
1227 TYPE_NAME (new_type) = TYPE_NAME (type);
1228 TYPE_JUSTIFIED_MODULAR_P (new_type) = TYPE_JUSTIFIED_MODULAR_P (type);
1229 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
1230 TYPE_REVERSE_STORAGE_ORDER (new_type) = TYPE_REVERSE_STORAGE_ORDER (type);
1231 if (TREE_CODE (type) == RECORD_TYPE)
1232 TYPE_PADDING_P (new_type) = TYPE_PADDING_P (type);
1233
1234 /* If we are in a record and have a small size, set the alignment to
1235 try for an integral mode. Otherwise set it to try for a smaller
1236 type with BLKmode. */
1237 if (in_record && size <= MAX_FIXED_MODE_SIZE)
1238 {
1239 new_size = ceil_pow2 (size);
1240 new_align = MIN (new_size, BIGGEST_ALIGNMENT);
1241 SET_TYPE_ALIGN (new_type, new_align);
1242 /* build_aligned_type needs to be able to adjust back the alignment. */
1243 TYPE_PACKED (new_type) = 0;
1244 }
1245 else
1246 {
1247 tree ada_size = TYPE_ADA_SIZE (type);
1248
1249 /* Do not try to shrink the size if the RM size is not constant. */
1250 if (TYPE_CONTAINS_TEMPLATE_P (type) || !tree_fits_uhwi_p (ada_size))
1251 return type;
1252
1253 /* Round the RM size up to a unit boundary to get the minimal size
1254 for a BLKmode record. Give up if it's already the size and we
1255 don't need to lower the alignment. */
1256 new_size = tree_to_uhwi (ada_size);
1257 new_size = (new_size + BITS_PER_UNIT - 1) & -BITS_PER_UNIT;
1258 if (new_size == size && (max_align == 0 || align <= max_align))
1259 return type;
1260
1261 new_align = MIN (new_size & -new_size, BIGGEST_ALIGNMENT);
1262 if (max_align > 0 && new_align > max_align)
1263 new_align = max_align;
1264 SET_TYPE_ALIGN (new_type, MIN (align, new_align));
1265 TYPE_PACKED (new_type) = 1;
1266 }
1267
1268 TYPE_USER_ALIGN (new_type) = 1;
1269
1270 /* Now copy the fields, keeping the position and size as we don't want
1271 to change the layout by propagating the packedness downwards. */
1272 tree new_field_list = NULL_TREE;
1273 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1274 {
1275 tree new_field_type = TREE_TYPE (field);
1276 tree new_field, new_field_size;
1277
1278 if (AGGREGATE_TYPE_P (new_field_type)
1279 && tree_fits_uhwi_p (TYPE_SIZE (new_field_type)))
1280 {
1281 if (RECORD_OR_UNION_TYPE_P (new_field_type)
1282 && !TYPE_FAT_POINTER_P (new_field_type))
1283 new_field_type
1284 = make_packable_type (new_field_type, true, max_align);
1285 else if (in_record
1286 && max_align > 0
1287 && max_align < BITS_PER_UNIT
1288 && TREE_CODE (new_field_type) == ARRAY_TYPE)
1289 new_field_type = make_packable_array_type (new_field_type);
1290 }
1291
1292 /* However, for the last field in a not already packed record type
1293 that is of an aggregate type, we need to use the RM size in the
1294 packable version of the record type, see finish_record_type. */
1295 if (!DECL_CHAIN (field)
1296 && !TYPE_PACKED (type)
1297 && RECORD_OR_UNION_TYPE_P (new_field_type)
1298 && !TYPE_FAT_POINTER_P (new_field_type)
1299 && !TYPE_CONTAINS_TEMPLATE_P (new_field_type)
1300 && TYPE_ADA_SIZE (new_field_type))
1301 new_field_size = TYPE_ADA_SIZE (new_field_type);
1302 else
1303 {
1304 new_field_size = DECL_SIZE (field);
1305
1306 /* Make sure not to use too small a type for the size. */
1307 if (TYPE_MODE (new_field_type) == BLKmode)
1308 new_field_type = TREE_TYPE (field);
1309 }
1310
1311 /* This is a layout with full representation, alignment and size clauses
1312 so we simply pass 0 as PACKED like gnat_to_gnu_field in this case. */
1313 new_field
1314 = create_field_decl (DECL_NAME (field), new_field_type, new_type,
1315 new_field_size, bit_position (field), 0,
1316 !DECL_NONADDRESSABLE_P (field));
1317
1318 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (field);
1319 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
1320 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
1321 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (field);
1322
1323 DECL_CHAIN (new_field) = new_field_list;
1324 new_field_list = new_field;
1325 }
1326
1327 /* If this is a padding record, we never want to make the size smaller
1328 than what was specified. For QUAL_UNION_TYPE, also copy the size. */
1329 if (TYPE_IS_PADDING_P (type) || TREE_CODE (type) == QUAL_UNION_TYPE)
1330 {
1331 TYPE_SIZE (new_type) = TYPE_SIZE (type);
1332 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
1333 new_size = size;
1334 }
1335 else
1336 {
1337 TYPE_SIZE (new_type) = bitsize_int (new_size);
1338 TYPE_SIZE_UNIT (new_type) = size_int (new_size / BITS_PER_UNIT);
1339 }
1340
1341 if (!TYPE_CONTAINS_TEMPLATE_P (type))
1342 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (type));
1343
1344 finish_record_type (new_type, nreverse (new_field_list), 2, false);
1345 relate_alias_sets (new_type, type, ALIAS_SET_COPY);
1346 if (gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
1347 SET_TYPE_DEBUG_TYPE (new_type, TYPE_DEBUG_TYPE (type));
1348 else if (TYPE_STUB_DECL (type))
1349 SET_DECL_PARALLEL_TYPE (TYPE_STUB_DECL (new_type),
1350 DECL_PARALLEL_TYPE (TYPE_STUB_DECL (type)));
1351
1352 /* Try harder to get a packable type if necessary, for example in case
1353 the record itself contains a BLKmode field. */
1354 if (in_record && TYPE_MODE (new_type) == BLKmode)
1355 SET_TYPE_MODE (new_type,
1356 mode_for_size_tree (TYPE_SIZE (new_type),
1357 MODE_INT, 1).else_blk ());
1358
1359 /* If neither mode nor size nor alignment shrunk, return the old type. */
1360 if (TYPE_MODE (new_type) == BLKmode && new_size >= size && max_align == 0)
1361 return type;
1362
1363 /* If the packable type is named, we canonicalize it by means of the hash
1364 table. This is consistent with the language semantics and ensures that
1365 gigi and the middle-end have a common view of these packable types. */
1366 return
1367 TYPE_NAME (new_type) ? canonicalize_packable_type (new_type) : new_type;
1368 }
1369
1370 /* Return true if TYPE has an unsigned representation. This needs to be used
1371 when the representation of types whose precision is not equal to their size
1372 is manipulated based on the RM size. */
1373
1374 static inline bool
1375 type_unsigned_for_rm (tree type)
1376 {
1377 /* This is the common case. */
1378 if (TYPE_UNSIGNED (type))
1379 return true;
1380
1381 /* See the E_Signed_Integer_Subtype case of gnat_to_gnu_entity. */
1382 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
1383 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
1384 return true;
1385
1386 return false;
1387 }
1388
1389 /* Return true iff the sized types are equivalent. */
1390
1391 bool
1392 sized_type_hasher::equal (sized_type_hash *t1, sized_type_hash *t2)
1393 {
1394 tree type1, type2;
1395
1396 if (t1->hash != t2->hash)
1397 return false;
1398
1399 type1 = t1->type;
1400 type2 = t2->type;
1401
1402 /* We consider sized types equivalent if they have the same name,
1403 size, alignment, RM size, and biasing. The range is not expected
1404 to vary across different-sized versions of the same base
1405 type. */
1406 bool res
1407 = (TYPE_NAME (type1) == TYPE_NAME (type2)
1408 && TYPE_SIZE (type1) == TYPE_SIZE (type2)
1409 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2)
1410 && TYPE_RM_SIZE (type1) == TYPE_RM_SIZE (type2)
1411 && (TYPE_BIASED_REPRESENTATION_P (type1)
1412 == TYPE_BIASED_REPRESENTATION_P (type2)));
1413
1414 gcc_assert (!res
1415 || (TYPE_RM_MIN_VALUE (type1) == TYPE_RM_MIN_VALUE (type2)
1416 && TYPE_RM_MAX_VALUE (type1) == TYPE_RM_MAX_VALUE (type2)));
1417
1418 return res;
1419 }
1420
1421 /* Compute the hash value for the sized TYPE. */
1422
1423 static hashval_t
1424 hash_sized_type (tree type)
1425 {
1426 hashval_t hashcode;
1427
1428 hashcode = iterative_hash_expr (TYPE_NAME (type), 0);
1429 hashcode = iterative_hash_expr (TYPE_SIZE (type), hashcode);
1430 hashcode = iterative_hash_hashval_t (TYPE_ALIGN (type), hashcode);
1431 hashcode = iterative_hash_expr (TYPE_RM_SIZE (type), hashcode);
1432 hashcode
1433 = iterative_hash_hashval_t (TYPE_BIASED_REPRESENTATION_P (type), hashcode);
1434
1435 return hashcode;
1436 }
1437
1438 /* Look up the sized TYPE in the hash table and return its canonical version
1439 if it exists; otherwise, insert it into the hash table. */
1440
1441 static tree
1442 canonicalize_sized_type (tree type)
1443 {
1444 const hashval_t hashcode = hash_sized_type (type);
1445 struct sized_type_hash in, *h, **slot;
1446
1447 in.hash = hashcode;
1448 in.type = type;
1449 slot = sized_type_hash_table->find_slot_with_hash (&in, hashcode, INSERT);
1450 h = *slot;
1451 if (!h)
1452 {
1453 h = ggc_alloc<sized_type_hash> ();
1454 h->hash = hashcode;
1455 h->type = type;
1456 *slot = h;
1457 }
1458
1459 return h->type;
1460 }
1461
1462 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
1463 If TYPE is the best type, return it. Otherwise, make a new type. We
1464 only support new integral and pointer types. FOR_BIASED is true if
1465 we are making a biased type. */
1466
1467 tree
1468 make_type_from_size (tree type, tree size_tree, bool for_biased)
1469 {
1470 unsigned HOST_WIDE_INT size;
1471 bool biased_p;
1472 tree new_type;
1473
1474 /* If size indicates an error, just return TYPE to avoid propagating
1475 the error. Likewise if it's too large to represent. */
1476 if (!size_tree || !tree_fits_uhwi_p (size_tree))
1477 return type;
1478
1479 size = tree_to_uhwi (size_tree);
1480
1481 switch (TREE_CODE (type))
1482 {
1483 case BOOLEAN_TYPE:
1484 /* Do not mess with boolean types that have foreign convention. */
1485 if (TYPE_PRECISION (type) == 1 && TYPE_SIZE (type) == size_tree)
1486 break;
1487
1488 /* ... fall through ... */
1489
1490 case INTEGER_TYPE:
1491 case ENUMERAL_TYPE:
1492 biased_p = (TREE_CODE (type) == INTEGER_TYPE
1493 && TYPE_BIASED_REPRESENTATION_P (type));
1494
1495 /* FOR_BIASED initially refers to the entity's representation,
1496 not to its type's. The type we're to return must take both
1497 into account. */
1498 for_biased |= biased_p;
1499
1500 /* Integer types with precision 0 are forbidden. */
1501 if (size == 0)
1502 size = 1;
1503
1504 /* Only do something if the type is not a bit-packed array type and does
1505 not already have the proper size and the size is not too large. */
1506 if (BIT_PACKED_ARRAY_TYPE_P (type)
1507 || (TYPE_PRECISION (type) == size && biased_p == for_biased)
1508 || size > (Enable_128bit_Types ? 128 : LONG_LONG_TYPE_SIZE))
1509 break;
1510
1511 /* The type should be an unsigned type if the original type is unsigned
1512 or if the lower bound is constant and non-negative or if the type is
1513 biased, see E_Signed_Integer_Subtype case of gnat_to_gnu_entity. */
1514 if (type_unsigned_for_rm (type) || for_biased)
1515 new_type = make_unsigned_type (size);
1516 else
1517 new_type = make_signed_type (size);
1518 TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
1519 SET_TYPE_RM_MIN_VALUE (new_type, TYPE_MIN_VALUE (type));
1520 SET_TYPE_RM_MAX_VALUE (new_type, TYPE_MAX_VALUE (type));
1521 /* Copy the name to show that it's essentially the same type and
1522 not a subrange type. */
1523 TYPE_NAME (new_type) = TYPE_NAME (type);
1524 TYPE_BIASED_REPRESENTATION_P (new_type) = for_biased;
1525 SET_TYPE_RM_SIZE (new_type, bitsize_int (size));
1526
1527 return (TYPE_NAME (new_type)
1528 ? canonicalize_sized_type (new_type)
1529 : new_type);
1530
1531 case RECORD_TYPE:
1532 /* Do something if this is a fat pointer, in which case we
1533 may need to return the thin pointer. */
1534 if (TYPE_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
1535 {
1536 scalar_int_mode p_mode;
1537 if (!int_mode_for_size (size, 0).exists (&p_mode)
1538 || !targetm.valid_pointer_mode (p_mode))
1539 p_mode = ptr_mode;
1540 return
1541 build_pointer_type_for_mode
1542 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)),
1543 p_mode, 0);
1544 }
1545 break;
1546
1547 case POINTER_TYPE:
1548 /* Only do something if this is a thin pointer, in which case we
1549 may need to return the fat pointer. */
1550 if (TYPE_IS_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
1551 return
1552 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
1553 break;
1554
1555 default:
1556 break;
1557 }
1558
1559 return type;
1560 }
1561
1562 /* Return true iff the padded types are equivalent. */
1563
1564 bool
1565 pad_type_hasher::equal (pad_type_hash *t1, pad_type_hash *t2)
1566 {
1567 tree type1, type2;
1568
1569 if (t1->hash != t2->hash)
1570 return 0;
1571
1572 type1 = t1->type;
1573 type2 = t2->type;
1574
1575 /* We consider that padded types are equivalent if they pad the same type
1576 and have the same size, alignment, RM size and storage order. Taking the
1577 mode into account is redundant since it is determined by the others. */
1578 return
1579 TREE_TYPE (TYPE_FIELDS (type1)) == TREE_TYPE (TYPE_FIELDS (type2))
1580 && TYPE_SIZE (type1) == TYPE_SIZE (type2)
1581 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2)
1582 && TYPE_ADA_SIZE (type1) == TYPE_ADA_SIZE (type2)
1583 && TYPE_REVERSE_STORAGE_ORDER (type1) == TYPE_REVERSE_STORAGE_ORDER (type2);
1584 }
1585
1586 /* Compute the hash value for the padded TYPE. */
1587
1588 static hashval_t
1589 hash_pad_type (tree type)
1590 {
1591 hashval_t hashcode;
1592
1593 hashcode
1594 = iterative_hash_object (TYPE_HASH (TREE_TYPE (TYPE_FIELDS (type))), 0);
1595 hashcode = iterative_hash_expr (TYPE_SIZE (type), hashcode);
1596 hashcode = iterative_hash_hashval_t (TYPE_ALIGN (type), hashcode);
1597 hashcode = iterative_hash_expr (TYPE_ADA_SIZE (type), hashcode);
1598 hashcode
1599 = iterative_hash_hashval_t (TYPE_REVERSE_STORAGE_ORDER (type), hashcode);
1600
1601 return hashcode;
1602 }
1603
1604 /* Look up the padded TYPE in the hash table and return its canonical version
1605 if it exists; otherwise, insert it into the hash table. */
1606
1607 static tree
1608 canonicalize_pad_type (tree type)
1609 {
1610 const hashval_t hashcode = hash_pad_type (type);
1611 struct pad_type_hash in, *h, **slot;
1612
1613 in.hash = hashcode;
1614 in.type = type;
1615 slot = pad_type_hash_table->find_slot_with_hash (&in, hashcode, INSERT);
1616 h = *slot;
1617 if (!h)
1618 {
1619 h = ggc_alloc<pad_type_hash> ();
1620 h->hash = hashcode;
1621 h->type = type;
1622 *slot = h;
1623 }
1624
1625 return h->type;
1626 }
1627
1628 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
1629 if needed. We have already verified that SIZE and ALIGN are large enough.
1630 GNAT_ENTITY is used to name the resulting record and to issue a warning.
1631 IS_COMPONENT_TYPE is true if this is being done for the component type of
1632 an array. DEFINITION is true if this type is being defined. SET_RM_SIZE
1633 is true if the RM size of the resulting type is to be set to SIZE too; in
1634 this case, the padded type is canonicalized before being returned.
1635
1636 Note that, if TYPE is an array, then we pad it even if it has already got
1637 an alignment of ALIGN, provided that it's larger than the alignment of the
1638 element type. This ensures that the size of the type is a multiple of its
1639 alignment as required by the GCC type system, and alleviates the oddity of
1640 the larger alignment, which is used to implement alignment clauses present
1641 on unconstrained array types. */
1642
1643 tree
1644 maybe_pad_type (tree type, tree size, unsigned int align,
1645 Entity_Id gnat_entity, bool is_component_type,
1646 bool definition, bool set_rm_size)
1647 {
1648 tree orig_size = TYPE_SIZE (type);
1649 unsigned int orig_align
1650 = TREE_CODE (type) == ARRAY_TYPE
1651 ? TYPE_ALIGN (TREE_TYPE (type))
1652 : TYPE_ALIGN (type);
1653 tree record, field;
1654
1655 /* If TYPE is a padded type, see if it agrees with any size and alignment
1656 we were given. If so, return the original type. Otherwise, strip
1657 off the padding, since we will either be returning the inner type
1658 or repadding it. If no size or alignment is specified, use that of
1659 the original padded type. */
1660 if (TYPE_IS_PADDING_P (type))
1661 {
1662 if ((!size
1663 || operand_equal_p (round_up (size, orig_align), orig_size, 0))
1664 && (align == 0 || align == orig_align))
1665 return type;
1666
1667 if (!size)
1668 size = orig_size;
1669 if (align == 0)
1670 align = orig_align;
1671
1672 type = TREE_TYPE (TYPE_FIELDS (type));
1673 orig_size = TYPE_SIZE (type);
1674 orig_align
1675 = TREE_CODE (type) == ARRAY_TYPE
1676 ? TYPE_ALIGN (TREE_TYPE (type))
1677 : TYPE_ALIGN (type);
1678 }
1679
1680 /* If the size is either not being changed or is being made smaller (which
1681 is not done here and is only valid for bitfields anyway), show the size
1682 isn't changing. Likewise, clear the alignment if it isn't being
1683 changed. Then return if we aren't doing anything. */
1684 if (size
1685 && (operand_equal_p (size, orig_size, 0)
1686 || (TREE_CODE (orig_size) == INTEGER_CST
1687 && tree_int_cst_lt (size, orig_size))))
1688 size = NULL_TREE;
1689
1690 if (align == orig_align)
1691 align = 0;
1692
1693 if (align == 0 && !size)
1694 return type;
1695
1696 /* We used to modify the record in place in some cases, but that could
1697 generate incorrect debugging information. So make a new record
1698 type and name. */
1699 record = make_node (RECORD_TYPE);
1700 TYPE_PADDING_P (record) = 1;
1701
1702 if (Present (gnat_entity))
1703 TYPE_NAME (record) = create_concat_name (gnat_entity, "PAD");
1704
1705 SET_TYPE_ALIGN (record, align ? align : orig_align);
1706 TYPE_SIZE (record) = size ? size : orig_size;
1707 TYPE_SIZE_UNIT (record)
1708 = convert (sizetype,
1709 size_binop (EXACT_DIV_EXPR, TYPE_SIZE (record),
1710 bitsize_unit_node));
1711
1712 /* If we are changing the alignment and the input type is a record with
1713 BLKmode and a small constant size, try to make a form that has an
1714 integral mode. This might allow the padding record to also have an
1715 integral mode, which will be much more efficient. There is no point
1716 in doing so if a size is specified unless it is also a small constant
1717 size and it is incorrect to do so if we cannot guarantee that the mode
1718 will be naturally aligned since the field must always be addressable.
1719
1720 ??? This might not always be a win when done for a stand-alone object:
1721 since the nominal and the effective type of the object will now have
1722 different modes, a VIEW_CONVERT_EXPR will be required for converting
1723 between them and it might be hard to overcome afterwards, including
1724 at the RTL level when the stand-alone object is accessed as a whole. */
1725 if (align > 0
1726 && RECORD_OR_UNION_TYPE_P (type)
1727 && !TYPE_IS_FAT_POINTER_P (type)
1728 && TYPE_MODE (type) == BLKmode
1729 && !TYPE_BY_REFERENCE_P (type)
1730 && TREE_CODE (orig_size) == INTEGER_CST
1731 && !TREE_OVERFLOW (orig_size)
1732 && compare_tree_int (orig_size, MAX_FIXED_MODE_SIZE) <= 0
1733 && (!size
1734 || (TREE_CODE (size) == INTEGER_CST
1735 && compare_tree_int (size, MAX_FIXED_MODE_SIZE) <= 0)))
1736 {
1737 tree packable_type = make_packable_type (type, true, align);
1738 if (TYPE_MODE (packable_type) != BLKmode
1739 && compare_tree_int (TYPE_SIZE (packable_type), align) <= 0)
1740 type = packable_type;
1741 }
1742
1743 /* Now create the field with the original size. */
1744 field = create_field_decl (get_identifier ("F"), type, record, orig_size,
1745 bitsize_zero_node, 0, 1);
1746 DECL_INTERNAL_P (field) = 1;
1747
1748 /* We will output additional debug info manually below. */
1749 finish_record_type (record, field, 1, false);
1750
1751 /* Set the RM size if requested. */
1752 if (set_rm_size)
1753 {
1754 SET_TYPE_ADA_SIZE (record, size ? size : orig_size);
1755
1756 /* If the padded type is complete and has constant size, we canonicalize
1757 it by means of the hash table. This is consistent with the language
1758 semantics and ensures that gigi and the middle-end have a common view
1759 of these padded types. */
1760 if (TREE_CONSTANT (TYPE_SIZE (record)))
1761 {
1762 tree canonical = canonicalize_pad_type (record);
1763 if (canonical != record)
1764 {
1765 record = canonical;
1766 goto built;
1767 }
1768 }
1769 }
1770
1771 /* Make the inner type the debug type of the padded type. */
1772 if (gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
1773 SET_TYPE_DEBUG_TYPE (record, maybe_debug_type (type));
1774
1775 /* Unless debugging information isn't being written for the input type,
1776 write a record that shows what we are a subtype of and also make a
1777 variable that indicates our size, if still variable. */
1778 if (TREE_CODE (orig_size) != INTEGER_CST
1779 && TYPE_NAME (record)
1780 && TYPE_NAME (type)
1781 && !(TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1782 && DECL_IGNORED_P (TYPE_NAME (type))))
1783 {
1784 tree name = TYPE_IDENTIFIER (record);
1785 tree size_unit = TYPE_SIZE_UNIT (record);
1786
1787 /* A variable that holds the size is required even with no encoding since
1788 it will be referenced by debugging information attributes. At global
1789 level, we need a single variable across all translation units. */
1790 if (size
1791 && TREE_CODE (size) != INTEGER_CST
1792 && (definition || global_bindings_p ()))
1793 {
1794 /* Whether or not gnat_entity comes from source, this XVZ variable is
1795 is a compilation artifact. */
1796 size_unit
1797 = create_var_decl (concat_name (name, "XVZ"), NULL_TREE, sizetype,
1798 size_unit, true, global_bindings_p (),
1799 !definition && global_bindings_p (), false,
1800 false, true, true, NULL, gnat_entity, false);
1801 TYPE_SIZE_UNIT (record) = size_unit;
1802 }
1803
1804 /* There is no need to show what we are a subtype of when outputting as
1805 few encodings as possible: regular debugging infomation makes this
1806 redundant. */
1807 if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL)
1808 {
1809 tree marker = make_node (RECORD_TYPE);
1810 tree orig_name = TYPE_IDENTIFIER (type);
1811
1812 TYPE_NAME (marker) = concat_name (name, "XVS");
1813 finish_record_type (marker,
1814 create_field_decl (orig_name,
1815 build_reference_type (type),
1816 marker, NULL_TREE, NULL_TREE,
1817 0, 0),
1818 0, true);
1819 TYPE_SIZE_UNIT (marker) = size_unit;
1820
1821 add_parallel_type (record, marker);
1822 }
1823 }
1824
1825 built:
1826 /* If a simple size was explicitly given, maybe issue a warning. */
1827 if (!size
1828 || TREE_CODE (size) == COND_EXPR
1829 || TREE_CODE (size) == MAX_EXPR
1830 || No (gnat_entity))
1831 return record;
1832
1833 /* But don't do it if we are just annotating types and the type is tagged or
1834 concurrent, since these types aren't fully laid out in this mode. */
1835 if (type_annotate_only)
1836 {
1837 Entity_Id gnat_type
1838 = is_component_type
1839 ? Component_Type (gnat_entity) : Etype (gnat_entity);
1840
1841 if (Is_Tagged_Type (gnat_type) || Is_Concurrent_Type (gnat_type))
1842 return record;
1843 }
1844
1845 /* Take the original size as the maximum size of the input if there was an
1846 unconstrained record involved and round it up to the specified alignment,
1847 if one was specified, but only for aggregate types. */
1848 if (CONTAINS_PLACEHOLDER_P (orig_size))
1849 orig_size = max_size (orig_size, true);
1850
1851 if (align && AGGREGATE_TYPE_P (type))
1852 orig_size = round_up (orig_size, align);
1853
1854 if (!operand_equal_p (size, orig_size, 0)
1855 && !(TREE_CODE (size) == INTEGER_CST
1856 && TREE_CODE (orig_size) == INTEGER_CST
1857 && (TREE_OVERFLOW (size)
1858 || TREE_OVERFLOW (orig_size)
1859 || tree_int_cst_lt (size, orig_size))))
1860 {
1861 Node_Id gnat_error_node;
1862
1863 /* For a packed array, post the message on the original array type. */
1864 if (Is_Packed_Array_Impl_Type (gnat_entity))
1865 gnat_entity = Original_Array_Type (gnat_entity);
1866
1867 if ((Ekind (gnat_entity) == E_Component
1868 || Ekind (gnat_entity) == E_Discriminant)
1869 && Present (Component_Clause (gnat_entity)))
1870 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
1871 else if (Has_Size_Clause (gnat_entity))
1872 gnat_error_node = Expression (Size_Clause (gnat_entity));
1873 else if (Has_Object_Size_Clause (gnat_entity))
1874 gnat_error_node = Expression (Object_Size_Clause (gnat_entity));
1875 else
1876 gnat_error_node = Empty;
1877
1878 /* Generate message only for entities that come from source, since
1879 if we have an entity created by expansion, the message will be
1880 generated for some other corresponding source entity. */
1881 if (Comes_From_Source (gnat_entity))
1882 {
1883 if (is_component_type)
1884 post_error_ne_tree ("component of& padded{ by ^ bits}??",
1885 gnat_entity, gnat_entity,
1886 size_diffop (size, orig_size));
1887 else if (Present (gnat_error_node))
1888 post_error_ne_tree ("{^ }bits of & unused??",
1889 gnat_error_node, gnat_entity,
1890 size_diffop (size, orig_size));
1891 }
1892 }
1893
1894 return record;
1895 }
1896
1897 /* Return true if padded TYPE was built with an RM size. */
1898
1899 bool
1900 pad_type_has_rm_size (tree type)
1901 {
1902 /* This is required for the lookup. */
1903 if (!TREE_CONSTANT (TYPE_SIZE (type)))
1904 return false;
1905
1906 const hashval_t hashcode = hash_pad_type (type);
1907 struct pad_type_hash in, *h;
1908
1909 in.hash = hashcode;
1910 in.type = type;
1911 h = pad_type_hash_table->find_with_hash (&in, hashcode);
1912
1913 /* The types built with an RM size are the canonicalized ones. */
1914 return h && h->type == type;
1915 }
1916
1917 /* Return a copy of the padded TYPE but with reverse storage order. */
1918
1919 tree
1920 set_reverse_storage_order_on_pad_type (tree type)
1921 {
1922 if (flag_checking)
1923 {
1924 /* If the inner type is not scalar then the function does nothing. */
1925 tree inner_type = TREE_TYPE (TYPE_FIELDS (type));
1926 gcc_assert (!AGGREGATE_TYPE_P (inner_type)
1927 && !VECTOR_TYPE_P (inner_type));
1928 }
1929
1930 /* This is required for the canonicalization. */
1931 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
1932
1933 tree field = copy_node (TYPE_FIELDS (type));
1934 type = copy_type (type);
1935 DECL_CONTEXT (field) = type;
1936 TYPE_FIELDS (type) = field;
1937 TYPE_REVERSE_STORAGE_ORDER (type) = 1;
1938 return canonicalize_pad_type (type);
1939 }
1940
1941 /* Relate the alias sets of NEW_TYPE and OLD_TYPE according to OP.
1942 If this is a multi-dimensional array type, do this recursively.
1943
1944 OP may be
1945 - ALIAS_SET_COPY: the new set is made a copy of the old one.
1946 - ALIAS_SET_SUPERSET: the new set is made a superset of the old one.
1947 - ALIAS_SET_SUBSET: the new set is made a subset of the old one. */
1948
1949 void
1950 relate_alias_sets (tree new_type, tree old_type, enum alias_set_op op)
1951 {
1952 /* Remove any padding from GNU_OLD_TYPE. It doesn't matter in the case
1953 of a one-dimensional array, since the padding has the same alias set
1954 as the field type, but if it's a multi-dimensional array, we need to
1955 see the inner types. */
1956 while (TREE_CODE (old_type) == RECORD_TYPE
1957 && (TYPE_JUSTIFIED_MODULAR_P (old_type)
1958 || TYPE_PADDING_P (old_type)))
1959 old_type = TREE_TYPE (TYPE_FIELDS (old_type));
1960
1961 /* Unconstrained array types are deemed incomplete and would thus be given
1962 alias set 0. Retrieve the underlying array type. */
1963 if (TREE_CODE (old_type) == UNCONSTRAINED_ARRAY_TYPE)
1964 old_type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (old_type))));
1965 if (TREE_CODE (new_type) == UNCONSTRAINED_ARRAY_TYPE)
1966 new_type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (new_type))));
1967
1968 if (TREE_CODE (new_type) == ARRAY_TYPE
1969 && TREE_CODE (TREE_TYPE (new_type)) == ARRAY_TYPE
1970 && TYPE_MULTI_ARRAY_P (TREE_TYPE (new_type)))
1971 relate_alias_sets (TREE_TYPE (new_type), TREE_TYPE (old_type), op);
1972
1973 switch (op)
1974 {
1975 case ALIAS_SET_COPY:
1976 /* The alias set shouldn't be copied between array types with different
1977 aliasing settings because this can break the aliasing relationship
1978 between the array type and its element type. */
1979 if (flag_checking || flag_strict_aliasing)
1980 gcc_assert (!(TREE_CODE (new_type) == ARRAY_TYPE
1981 && TREE_CODE (old_type) == ARRAY_TYPE
1982 && TYPE_NONALIASED_COMPONENT (new_type)
1983 != TYPE_NONALIASED_COMPONENT (old_type)));
1984
1985 /* The alias set is a property of the TYPE_CANONICAL if it exists. */
1986 if (TYPE_STRUCTURAL_EQUALITY_P (new_type))
1987 TYPE_ALIAS_SET (new_type) = get_alias_set (old_type);
1988 else
1989 TYPE_ALIAS_SET (TYPE_CANONICAL (new_type)) = get_alias_set (old_type);
1990 break;
1991
1992 case ALIAS_SET_SUBSET:
1993 case ALIAS_SET_SUPERSET:
1994 {
1995 alias_set_type old_set = get_alias_set (old_type);
1996 alias_set_type new_set = get_alias_set (new_type);
1997
1998 /* Do nothing if the alias sets conflict. This ensures that we
1999 never call record_alias_subset several times for the same pair
2000 or at all for alias set 0. */
2001 if (!alias_sets_conflict_p (old_set, new_set))
2002 {
2003 if (op == ALIAS_SET_SUBSET)
2004 record_alias_subset (old_set, new_set);
2005 else
2006 record_alias_subset (new_set, old_set);
2007 }
2008 }
2009 break;
2010
2011 default:
2012 gcc_unreachable ();
2013 }
2014
2015 record_component_aliases (new_type);
2016 }
2017
2018 /* Record TYPE as a builtin type for Ada. NAME is the name of the type.
2019 ARTIFICIAL_P is true if the type was generated by the compiler. */
2020
2021 void
2022 record_builtin_type (const char *name, tree type, bool artificial_p)
2023 {
2024 tree type_decl = build_decl (input_location,
2025 TYPE_DECL, get_identifier (name), type);
2026 DECL_ARTIFICIAL (type_decl) = artificial_p;
2027 DECL_NAMELESS (type_decl) = (artificial_p
2028 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL);
2029 TYPE_ARTIFICIAL (type) = artificial_p;
2030 gnat_pushdecl (type_decl, Empty);
2031
2032 if (debug_hooks->type_decl)
2033 debug_hooks->type_decl (type_decl, false);
2034 }
2035
2036 /* Finish constructing the character type CHAR_TYPE.
2037
2038 In Ada character types are enumeration types and, as a consequence, are
2039 represented in the front-end by integral types holding the positions of
2040 the enumeration values as defined by the language, which means that the
2041 integral types are unsigned.
2042
2043 Unfortunately the signedness of 'char' in C is implementation-defined
2044 and GCC even has the option -f[un]signed-char to toggle it at run time.
2045 Since GNAT's philosophy is to be compatible with C by default, to wit
2046 Interfaces.C.char is defined as a mere copy of Character, we may need
2047 to declare character types as signed types in GENERIC and generate the
2048 necessary adjustments to make them behave as unsigned types.
2049
2050 The overall strategy is as follows: if 'char' is unsigned, do nothing;
2051 if 'char' is signed, translate character types of CHAR_TYPE_SIZE and
2052 character subtypes with RM_Size = Esize = CHAR_TYPE_SIZE into signed
2053 types. The idea is to ensure that the bit pattern contained in the
2054 Esize'd objects is not changed, even though the numerical value will
2055 be interpreted differently depending on the signedness. */
2056
2057 void
2058 finish_character_type (tree char_type)
2059 {
2060 if (TYPE_UNSIGNED (char_type))
2061 return;
2062
2063 /* Make a copy of a generic unsigned version since we'll modify it. */
2064 tree unsigned_char_type
2065 = (char_type == char_type_node
2066 ? unsigned_char_type_node
2067 : copy_type (gnat_unsigned_type_for (char_type)));
2068
2069 /* Create an unsigned version of the type and set it as debug type. */
2070 TYPE_NAME (unsigned_char_type) = TYPE_NAME (char_type);
2071 TYPE_STRING_FLAG (unsigned_char_type) = TYPE_STRING_FLAG (char_type);
2072 TYPE_ARTIFICIAL (unsigned_char_type) = TYPE_ARTIFICIAL (char_type);
2073 SET_TYPE_DEBUG_TYPE (char_type, unsigned_char_type);
2074
2075 /* If this is a subtype, make the debug type a subtype of the debug type
2076 of the base type and convert literal RM bounds to unsigned. */
2077 if (TREE_TYPE (char_type))
2078 {
2079 tree base_unsigned_char_type = TYPE_DEBUG_TYPE (TREE_TYPE (char_type));
2080 tree min_value = TYPE_RM_MIN_VALUE (char_type);
2081 tree max_value = TYPE_RM_MAX_VALUE (char_type);
2082
2083 if (TREE_CODE (min_value) == INTEGER_CST)
2084 min_value = fold_convert (base_unsigned_char_type, min_value);
2085 if (TREE_CODE (max_value) == INTEGER_CST)
2086 max_value = fold_convert (base_unsigned_char_type, max_value);
2087
2088 TREE_TYPE (unsigned_char_type) = base_unsigned_char_type;
2089 SET_TYPE_RM_MIN_VALUE (unsigned_char_type, min_value);
2090 SET_TYPE_RM_MAX_VALUE (unsigned_char_type, max_value);
2091 }
2092
2093 /* Adjust the RM bounds of the original type to unsigned; that's especially
2094 important for types since they are implicit in this case. */
2095 SET_TYPE_RM_MIN_VALUE (char_type, TYPE_MIN_VALUE (unsigned_char_type));
2096 SET_TYPE_RM_MAX_VALUE (char_type, TYPE_MAX_VALUE (unsigned_char_type));
2097 }
2098
2099 /* Given a record type RECORD_TYPE and a list of FIELD_DECL nodes FIELD_LIST,
2100 finish constructing the record type as a fat pointer type. */
2101
2102 void
2103 finish_fat_pointer_type (tree record_type, tree field_list)
2104 {
2105 /* Make sure we can put it into a register. */
2106 if (STRICT_ALIGNMENT)
2107 SET_TYPE_ALIGN (record_type, MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE));
2108
2109 /* Show what it really is. */
2110 TYPE_FAT_POINTER_P (record_type) = 1;
2111
2112 /* Do not emit debug info for it since the types of its fields may still be
2113 incomplete at this point. */
2114 finish_record_type (record_type, field_list, 0, false);
2115
2116 /* Force type_contains_placeholder_p to return true on it. Although the
2117 PLACEHOLDER_EXPRs are referenced only indirectly, this isn't a pointer
2118 type but the representation of the unconstrained array. */
2119 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (record_type) = 2;
2120 }
2121
2122 /* Clear DECL_BIT_FIELD flag and associated markers on FIELD, which is a field
2123 of aggregate type TYPE. */
2124
2125 static void
2126 clear_decl_bit_field (tree field, tree type)
2127 {
2128 DECL_BIT_FIELD (field) = 0;
2129 DECL_BIT_FIELD_TYPE (field) = NULL_TREE;
2130
2131 /* DECL_BIT_FIELD_REPRESENTATIVE is not defined for QUAL_UNION_TYPE since
2132 it uses the same slot as DECL_QUALIFIER. */
2133 if (TREE_CODE (type) != QUAL_UNION_TYPE)
2134 DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
2135 }
2136
2137 /* Given a record type RECORD_TYPE and a list of FIELD_DECL nodes FIELD_LIST,
2138 finish constructing the record or union type. If REP_LEVEL is zero, this
2139 record has no representation clause and so will be entirely laid out here.
2140 If REP_LEVEL is one, this record has a representation clause and has been
2141 laid out already; only set the sizes and alignment. If REP_LEVEL is two,
2142 this record is derived from a parent record and thus inherits its layout;
2143 only make a pass on the fields to finalize them. DEBUG_INFO_P is true if
2144 additional debug info needs to be output for this type. */
2145
2146 void
2147 finish_record_type (tree record_type, tree field_list, int rep_level,
2148 bool debug_info_p)
2149 {
2150 const enum tree_code orig_code = TREE_CODE (record_type);
2151 const bool had_size = TYPE_SIZE (record_type) != NULL_TREE;
2152 const bool had_align = TYPE_ALIGN (record_type) > 0;
2153 /* For all-repped records with a size specified, lay the QUAL_UNION_TYPE
2154 out just like a UNION_TYPE, since the size will be fixed. */
2155 const enum tree_code code
2156 = (orig_code == QUAL_UNION_TYPE && rep_level > 0 && had_size
2157 ? UNION_TYPE : orig_code);
2158 tree name = TYPE_IDENTIFIER (record_type);
2159 tree ada_size = bitsize_zero_node;
2160 tree size = bitsize_zero_node;
2161 tree field;
2162
2163 TYPE_FIELDS (record_type) = field_list;
2164
2165 /* Always attach the TYPE_STUB_DECL for a record type. It is required to
2166 generate debug info and have a parallel type. */
2167 TYPE_STUB_DECL (record_type) = create_type_stub_decl (name, record_type);
2168
2169 /* Globally initialize the record first. If this is a rep'ed record,
2170 that just means some initializations; otherwise, layout the record. */
2171 if (rep_level > 0)
2172 {
2173 if (TYPE_ALIGN (record_type) < BITS_PER_UNIT)
2174 SET_TYPE_ALIGN (record_type, BITS_PER_UNIT);
2175
2176 if (!had_size)
2177 TYPE_SIZE (record_type) = bitsize_zero_node;
2178 }
2179 else
2180 {
2181 /* Ensure there isn't a size already set. There can be in an error
2182 case where there is a rep clause but all fields have errors and
2183 no longer have a position. */
2184 TYPE_SIZE (record_type) = NULL_TREE;
2185
2186 /* Ensure we use the traditional GCC layout for bitfields when we need
2187 to pack the record type or have a representation clause. The other
2188 possible layout (Microsoft C compiler), if available, would prevent
2189 efficient packing in almost all cases. */
2190 #ifdef TARGET_MS_BITFIELD_LAYOUT
2191 if (TARGET_MS_BITFIELD_LAYOUT && TYPE_PACKED (record_type))
2192 decl_attributes (&record_type,
2193 tree_cons (get_identifier ("gcc_struct"),
2194 NULL_TREE, NULL_TREE),
2195 ATTR_FLAG_TYPE_IN_PLACE);
2196 #endif
2197
2198 layout_type (record_type);
2199 }
2200
2201 /* At this point, the position and size of each field is known. It was
2202 either set before entry by a rep clause, or by laying out the type above.
2203
2204 We now run a pass over the fields (in reverse order for QUAL_UNION_TYPEs)
2205 to compute the Ada size; the GCC size and alignment (for rep'ed records
2206 that are not padding types); and the mode (for rep'ed records). We also
2207 clear the DECL_BIT_FIELD indication for the cases we know have not been
2208 handled yet, and adjust DECL_NONADDRESSABLE_P accordingly. */
2209
2210 if (code == QUAL_UNION_TYPE)
2211 field_list = nreverse (field_list);
2212
2213 for (field = field_list; field; field = DECL_CHAIN (field))
2214 {
2215 tree type = TREE_TYPE (field);
2216 tree pos = bit_position (field);
2217 tree this_size = DECL_SIZE (field);
2218 tree this_ada_size;
2219
2220 if (RECORD_OR_UNION_TYPE_P (type)
2221 && !TYPE_FAT_POINTER_P (type)
2222 && !TYPE_CONTAINS_TEMPLATE_P (type)
2223 && TYPE_ADA_SIZE (type))
2224 this_ada_size = TYPE_ADA_SIZE (type);
2225 else
2226 this_ada_size = this_size;
2227
2228 const bool variant_part = (TREE_CODE (type) == QUAL_UNION_TYPE);
2229
2230 /* Clear DECL_BIT_FIELD for the cases layout_decl does not handle. */
2231 if (DECL_BIT_FIELD (field)
2232 && operand_equal_p (this_size, TYPE_SIZE (type), 0))
2233 {
2234 const unsigned int align = default_field_alignment (field, type);
2235
2236 /* In the general case, type alignment is required. */
2237 if (value_factor_p (pos, align))
2238 {
2239 /* The enclosing record type must be sufficiently aligned.
2240 Otherwise, if no alignment was specified for it and it
2241 has been laid out already, bump its alignment to the
2242 desired one if this is compatible with its size and
2243 maximum alignment, if any. */
2244 if (TYPE_ALIGN (record_type) >= align)
2245 {
2246 SET_DECL_ALIGN (field, MAX (DECL_ALIGN (field), align));
2247 clear_decl_bit_field (field, record_type);
2248 }
2249 else if (!had_align
2250 && rep_level == 0
2251 && value_factor_p (TYPE_SIZE (record_type), align)
2252 && (!TYPE_MAX_ALIGN (record_type)
2253 || TYPE_MAX_ALIGN (record_type) >= align))
2254 {
2255 SET_TYPE_ALIGN (record_type, align);
2256 SET_DECL_ALIGN (field, MAX (DECL_ALIGN (field), align));
2257 clear_decl_bit_field (field, record_type);
2258 }
2259 }
2260
2261 /* In the non-strict alignment case, only byte alignment is. */
2262 if (!STRICT_ALIGNMENT
2263 && DECL_BIT_FIELD (field)
2264 && value_factor_p (pos, BITS_PER_UNIT))
2265 clear_decl_bit_field (field, record_type);
2266 }
2267
2268 /* Clear DECL_BIT_FIELD_TYPE for a variant part at offset 0, it's simply
2269 not supported by the DECL_BIT_FIELD_REPRESENTATIVE machinery because
2270 the variant part is always the last field in the list. */
2271 if (variant_part && integer_zerop (pos))
2272 DECL_BIT_FIELD_TYPE (field) = NULL_TREE;
2273
2274 /* If we still have DECL_BIT_FIELD set at this point, we know that the
2275 field is technically not addressable. Except that it can actually
2276 be addressed if it is BLKmode and happens to be properly aligned. */
2277 if (DECL_BIT_FIELD (field)
2278 && !(DECL_MODE (field) == BLKmode
2279 && value_factor_p (pos, BITS_PER_UNIT)))
2280 DECL_NONADDRESSABLE_P (field) = 1;
2281
2282 /* A type must be as aligned as its most aligned field that is not
2283 a bit-field. But this is already enforced by layout_type. */
2284 if (rep_level > 0 && !DECL_BIT_FIELD (field))
2285 SET_TYPE_ALIGN (record_type,
2286 MAX (TYPE_ALIGN (record_type), DECL_ALIGN (field)));
2287
2288 switch (code)
2289 {
2290 case UNION_TYPE:
2291 ada_size = size_binop (MAX_EXPR, ada_size, this_ada_size);
2292 size = size_binop (MAX_EXPR, size, this_size);
2293 break;
2294
2295 case QUAL_UNION_TYPE:
2296 ada_size
2297 = fold_build3 (COND_EXPR, bitsizetype, DECL_QUALIFIER (field),
2298 this_ada_size, ada_size);
2299 size = fold_build3 (COND_EXPR, bitsizetype, DECL_QUALIFIER (field),
2300 this_size, size);
2301 break;
2302
2303 case RECORD_TYPE:
2304 /* Since we know here that all fields are sorted in order of
2305 increasing bit position, the size of the record is one
2306 higher than the ending bit of the last field processed
2307 unless we have a rep clause, because we might be processing
2308 the REP part of a record with a variant part for which the
2309 variant part has a rep clause but not the fixed part, in
2310 which case this REP part may contain overlapping fields
2311 and thus needs to be treated like a union tyoe above, so
2312 use a MAX in that case. Also, if this field is a variant
2313 part, we need to take into account the previous size in
2314 the case of empty variants. */
2315 ada_size
2316 = merge_sizes (ada_size, pos, this_ada_size, rep_level > 0,
2317 variant_part);
2318 size
2319 = merge_sizes (size, pos, this_size, rep_level > 0, variant_part);
2320 break;
2321
2322 default:
2323 gcc_unreachable ();
2324 }
2325 }
2326
2327 if (code == QUAL_UNION_TYPE)
2328 nreverse (field_list);
2329
2330 /* We need to set the regular sizes if REP_LEVEL is one. */
2331 if (rep_level == 1)
2332 {
2333 /* We round TYPE_SIZE and TYPE_SIZE_UNIT up to TYPE_ALIGN separately
2334 to avoid having very large masking constants in TYPE_SIZE_UNIT. */
2335 const unsigned int align = TYPE_ALIGN (record_type);
2336
2337 /* If this is a padding record, we never want to make the size smaller
2338 than what was specified in it, if any. */
2339 if (TYPE_IS_PADDING_P (record_type) && had_size)
2340 size = round_up (TYPE_SIZE (record_type), BITS_PER_UNIT);
2341 else
2342 size = round_up (size, BITS_PER_UNIT);
2343
2344 TYPE_SIZE (record_type) = variable_size (round_up (size, align));
2345
2346 tree size_unit
2347 = convert (sizetype,
2348 size_binop (EXACT_DIV_EXPR, size, bitsize_unit_node));
2349 TYPE_SIZE_UNIT (record_type)
2350 = variable_size (round_up (size_unit, align / BITS_PER_UNIT));
2351 }
2352
2353 /* We need to set the Ada size if REP_LEVEL is zero or one. */
2354 if (rep_level < 2)
2355 {
2356 /* Now set any of the values we've just computed that apply. */
2357 if (!TYPE_FAT_POINTER_P (record_type)
2358 && !TYPE_CONTAINS_TEMPLATE_P (record_type))
2359 SET_TYPE_ADA_SIZE (record_type, ada_size);
2360 }
2361
2362 /* We need to set the mode if REP_LEVEL is one or two. */
2363 if (rep_level > 0)
2364 {
2365 compute_record_mode (record_type);
2366 finish_bitfield_layout (record_type);
2367 }
2368
2369 /* Reset the TYPE_MAX_ALIGN field since it's private to gigi. */
2370 TYPE_MAX_ALIGN (record_type) = 0;
2371
2372 if (debug_info_p)
2373 rest_of_record_type_compilation (record_type);
2374 }
2375
2376 /* Append PARALLEL_TYPE on the chain of parallel types of TYPE. If
2377 PARRALEL_TYPE has no context and its computation is not deferred yet, also
2378 propagate TYPE's context to PARALLEL_TYPE's or defer its propagation to the
2379 moment TYPE will get a context. */
2380
2381 void
2382 add_parallel_type (tree type, tree parallel_type)
2383 {
2384 tree decl = TYPE_STUB_DECL (type);
2385
2386 while (DECL_PARALLEL_TYPE (decl))
2387 decl = TYPE_STUB_DECL (DECL_PARALLEL_TYPE (decl));
2388
2389 SET_DECL_PARALLEL_TYPE (decl, parallel_type);
2390
2391 /* If PARALLEL_TYPE already has a context, we are done. */
2392 if (TYPE_CONTEXT (parallel_type))
2393 return;
2394
2395 /* Otherwise, try to get one from TYPE's context. If so, simply propagate
2396 it to PARALLEL_TYPE. */
2397 if (TYPE_CONTEXT (type))
2398 gnat_set_type_context (parallel_type, TYPE_CONTEXT (type));
2399
2400 /* Otherwise TYPE has not context yet. We know it will have one thanks to
2401 gnat_pushdecl and then its context will be propagated to PARALLEL_TYPE,
2402 so we have nothing to do in this case. */
2403 }
2404
2405 /* Return true if TYPE has a parallel type. */
2406
2407 static bool
2408 has_parallel_type (tree type)
2409 {
2410 tree decl = TYPE_STUB_DECL (type);
2411
2412 return DECL_PARALLEL_TYPE (decl) != NULL_TREE;
2413 }
2414
2415 /* Wrap up compilation of RECORD_TYPE, i.e. output additional debug info
2416 associated with it. It need not be invoked directly in most cases as
2417 finish_record_type takes care of doing so. */
2418
2419 void
2420 rest_of_record_type_compilation (tree record_type)
2421 {
2422 bool var_size = false;
2423 tree field;
2424
2425 /* If this is a padded type, the bulk of the debug info has already been
2426 generated for the field's type. */
2427 if (TYPE_IS_PADDING_P (record_type))
2428 return;
2429
2430 /* If the type already has a parallel type (XVS type), then we're done. */
2431 if (has_parallel_type (record_type))
2432 return;
2433
2434 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
2435 {
2436 /* We need to make an XVE/XVU record if any field has variable size,
2437 whether or not the record does. For example, if we have a union,
2438 it may be that all fields, rounded up to the alignment, have the
2439 same size, in which case we'll use that size. But the debug
2440 output routines (except Dwarf2) won't be able to output the fields,
2441 so we need to make the special record. */
2442 if (TREE_CODE (DECL_SIZE (field)) != INTEGER_CST
2443 /* If a field has a non-constant qualifier, the record will have
2444 variable size too. */
2445 || (TREE_CODE (record_type) == QUAL_UNION_TYPE
2446 && TREE_CODE (DECL_QUALIFIER (field)) != INTEGER_CST))
2447 {
2448 var_size = true;
2449 break;
2450 }
2451 }
2452
2453 /* If this record type is of variable size, make a parallel record type that
2454 will tell the debugger how the former is laid out (see exp_dbug.ads). */
2455 if (var_size && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL)
2456 {
2457 tree new_record_type
2458 = make_node (TREE_CODE (record_type) == QUAL_UNION_TYPE
2459 ? UNION_TYPE : TREE_CODE (record_type));
2460 tree orig_name = TYPE_IDENTIFIER (record_type), new_name;
2461 tree last_pos = bitsize_zero_node;
2462
2463 new_name
2464 = concat_name (orig_name, TREE_CODE (record_type) == QUAL_UNION_TYPE
2465 ? "XVU" : "XVE");
2466 TYPE_NAME (new_record_type) = new_name;
2467 SET_TYPE_ALIGN (new_record_type, BIGGEST_ALIGNMENT);
2468 TYPE_STUB_DECL (new_record_type)
2469 = create_type_stub_decl (new_name, new_record_type);
2470 DECL_IGNORED_P (TYPE_STUB_DECL (new_record_type))
2471 = DECL_IGNORED_P (TYPE_STUB_DECL (record_type));
2472 gnat_pushdecl (TYPE_STUB_DECL (new_record_type), Empty);
2473 TYPE_SIZE (new_record_type) = size_int (TYPE_ALIGN (record_type));
2474 TYPE_SIZE_UNIT (new_record_type)
2475 = size_int (TYPE_ALIGN (record_type) / BITS_PER_UNIT);
2476
2477 /* Now scan all the fields, replacing each field with a new field
2478 corresponding to the new encoding. */
2479 for (tree old_field = TYPE_FIELDS (record_type);
2480 old_field;
2481 old_field = DECL_CHAIN (old_field))
2482 {
2483 tree field_type = TREE_TYPE (old_field);
2484 tree field_name = DECL_NAME (old_field);
2485 tree curpos = fold_bit_position (old_field);
2486 tree pos, new_field;
2487 bool var = false;
2488 unsigned int align = 0;
2489
2490 /* See how the position was modified from the last position.
2491
2492 There are two basic cases we support: a value was added
2493 to the last position or the last position was rounded to
2494 a boundary and they something was added. Check for the
2495 first case first. If not, see if there is any evidence
2496 of rounding. If so, round the last position and retry.
2497
2498 If this is a union, the position can be taken as zero. */
2499 if (TREE_CODE (new_record_type) == UNION_TYPE)
2500 pos = bitsize_zero_node;
2501 else
2502 pos = compute_related_constant (curpos, last_pos);
2503
2504 if (pos)
2505 ;
2506 else if (TREE_CODE (curpos) == MULT_EXPR
2507 && tree_fits_uhwi_p (TREE_OPERAND (curpos, 1)))
2508 {
2509 tree offset = TREE_OPERAND (curpos, 0);
2510 align = tree_to_uhwi (TREE_OPERAND (curpos, 1));
2511 align = scale_by_factor_of (offset, align);
2512 last_pos = round_up (last_pos, align);
2513 pos = compute_related_constant (curpos, last_pos);
2514 }
2515 else if (TREE_CODE (curpos) == PLUS_EXPR
2516 && tree_fits_uhwi_p (TREE_OPERAND (curpos, 1))
2517 && TREE_CODE (TREE_OPERAND (curpos, 0)) == MULT_EXPR
2518 && tree_fits_uhwi_p
2519 (TREE_OPERAND (TREE_OPERAND (curpos, 0), 1)))
2520 {
2521 tree offset = TREE_OPERAND (TREE_OPERAND (curpos, 0), 0);
2522 unsigned HOST_WIDE_INT addend
2523 = tree_to_uhwi (TREE_OPERAND (curpos, 1));
2524 align
2525 = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (curpos, 0), 1));
2526 align = scale_by_factor_of (offset, align);
2527 align = MIN (align, addend & -addend);
2528 last_pos = round_up (last_pos, align);
2529 pos = compute_related_constant (curpos, last_pos);
2530 }
2531 else
2532 {
2533 align = DECL_ALIGN (old_field);
2534 last_pos = round_up (last_pos, align);
2535 pos = compute_related_constant (curpos, last_pos);
2536 }
2537
2538 /* See if this type is variable-sized and make a pointer type
2539 and indicate the indirection if so. Beware that the debug
2540 back-end may adjust the position computed above according
2541 to the alignment of the field type, i.e. the pointer type
2542 in this case, if we don't preventively counter that. */
2543 if (TREE_CODE (DECL_SIZE (old_field)) != INTEGER_CST)
2544 {
2545 field_type = copy_type (build_pointer_type (field_type));
2546 SET_TYPE_ALIGN (field_type, BITS_PER_UNIT);
2547 var = true;
2548
2549 /* ??? Kludge to work around a bug in Workbench's debugger. */
2550 if (align == 0)
2551 {
2552 align = DECL_ALIGN (old_field);
2553 last_pos = round_up (last_pos, align);
2554 pos = compute_related_constant (curpos, last_pos);
2555 }
2556 }
2557
2558 /* If we can't compute a position, set it to zero.
2559
2560 ??? We really should abort here, but it's too much work
2561 to get this correct for all cases. */
2562 if (!pos)
2563 pos = bitsize_zero_node;
2564
2565 /* Make a new field name, if necessary. */
2566 if (var || align != 0)
2567 {
2568 char suffix[16];
2569
2570 if (align != 0)
2571 sprintf (suffix, "XV%c%u", var ? 'L' : 'A',
2572 align / BITS_PER_UNIT);
2573 else
2574 strcpy (suffix, "XVL");
2575
2576 field_name = concat_name (field_name, suffix);
2577 }
2578
2579 new_field
2580 = create_field_decl (field_name, field_type, new_record_type,
2581 DECL_SIZE (old_field), pos, 0, 0);
2582 /* The specified position is not the actual position of the field
2583 but the gap with the previous field, so the computation of the
2584 bit-field status may be incorrect. We adjust it manually to
2585 avoid generating useless attributes for the field in DWARF. */
2586 if (DECL_SIZE (old_field) == TYPE_SIZE (field_type)
2587 && value_factor_p (pos, BITS_PER_UNIT))
2588 clear_decl_bit_field (new_field, new_record_type);
2589 DECL_CHAIN (new_field) = TYPE_FIELDS (new_record_type);
2590 TYPE_FIELDS (new_record_type) = new_field;
2591
2592 /* If old_field is a QUAL_UNION_TYPE, take its size as being
2593 zero. The only time it's not the last field of the record
2594 is when there are other components at fixed positions after
2595 it (meaning there was a rep clause for every field) and we
2596 want to be able to encode them. */
2597 last_pos = size_binop (PLUS_EXPR, curpos,
2598 (TREE_CODE (TREE_TYPE (old_field))
2599 == QUAL_UNION_TYPE)
2600 ? bitsize_zero_node
2601 : DECL_SIZE (old_field));
2602 }
2603
2604 TYPE_FIELDS (new_record_type) = nreverse (TYPE_FIELDS (new_record_type));
2605
2606 add_parallel_type (record_type, new_record_type);
2607 }
2608 }
2609
2610 /* Utility function of above to merge LAST_SIZE, the previous size of a record
2611 with FIRST_BIT and SIZE that describe a field. If MAX is true, we take the
2612 MAX of the end position of this field with LAST_SIZE. In all other cases,
2613 we use FIRST_BIT plus SIZE. SPECIAL is true if it's for a QUAL_UNION_TYPE,
2614 in which case we must look for COND_EXPRs and replace a value of zero with
2615 the old size. Return an expression for the size. */
2616
2617 static tree
2618 merge_sizes (tree last_size, tree first_bit, tree size, bool max, bool special)
2619 {
2620 tree type = TREE_TYPE (last_size);
2621 tree new_size;
2622
2623 if (!special || TREE_CODE (size) != COND_EXPR)
2624 {
2625 new_size = size_binop (PLUS_EXPR, first_bit, size);
2626 if (max)
2627 new_size = size_binop (MAX_EXPR, last_size, new_size);
2628 }
2629
2630 else
2631 new_size = fold_build3 (COND_EXPR, type, TREE_OPERAND (size, 0),
2632 integer_zerop (TREE_OPERAND (size, 1))
2633 ? last_size : merge_sizes (last_size, first_bit,
2634 TREE_OPERAND (size, 1),
2635 max, special),
2636 integer_zerop (TREE_OPERAND (size, 2))
2637 ? last_size : merge_sizes (last_size, first_bit,
2638 TREE_OPERAND (size, 2),
2639 max, special));
2640
2641 /* We don't need any NON_VALUE_EXPRs and they can confuse us (especially
2642 when fed through SUBSTITUTE_IN_EXPR) into thinking that a constant
2643 size is not constant. */
2644 while (TREE_CODE (new_size) == NON_LVALUE_EXPR)
2645 new_size = TREE_OPERAND (new_size, 0);
2646
2647 return new_size;
2648 }
2649
2650 /* Convert the size expression EXPR to TYPE and fold the result. */
2651
2652 static tree
2653 fold_convert_size (tree type, tree expr)
2654 {
2655 /* We assume that size expressions do not wrap around. */
2656 if (TREE_CODE (expr) == MULT_EXPR || TREE_CODE (expr) == PLUS_EXPR)
2657 return size_binop (TREE_CODE (expr),
2658 fold_convert_size (type, TREE_OPERAND (expr, 0)),
2659 fold_convert_size (type, TREE_OPERAND (expr, 1)));
2660
2661 return fold_convert (type, expr);
2662 }
2663
2664 /* Return the bit position of FIELD, in bits from the start of the record,
2665 and fold it as much as possible. This is a tree of type bitsizetype. */
2666
2667 static tree
2668 fold_bit_position (const_tree field)
2669 {
2670 tree offset = fold_convert_size (bitsizetype, DECL_FIELD_OFFSET (field));
2671 return size_binop (PLUS_EXPR, DECL_FIELD_BIT_OFFSET (field),
2672 size_binop (MULT_EXPR, offset, bitsize_unit_node));
2673 }
2674
2675 /* Utility function of above to see if OP0 and OP1, both of SIZETYPE, are
2676 related by the addition of a constant. Return that constant if so. */
2677
2678 static tree
2679 compute_related_constant (tree op0, tree op1)
2680 {
2681 tree factor, op0_var, op1_var, op0_cst, op1_cst, result;
2682
2683 if (TREE_CODE (op0) == MULT_EXPR
2684 && TREE_CODE (op1) == MULT_EXPR
2685 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST
2686 && TREE_OPERAND (op1, 1) == TREE_OPERAND (op0, 1))
2687 {
2688 factor = TREE_OPERAND (op0, 1);
2689 op0 = TREE_OPERAND (op0, 0);
2690 op1 = TREE_OPERAND (op1, 0);
2691 }
2692 else
2693 factor = NULL_TREE;
2694
2695 op0_cst = split_plus (op0, &op0_var);
2696 op1_cst = split_plus (op1, &op1_var);
2697 result = size_binop (MINUS_EXPR, op0_cst, op1_cst);
2698
2699 if (operand_equal_p (op0_var, op1_var, 0))
2700 return factor ? size_binop (MULT_EXPR, factor, result) : result;
2701
2702 return NULL_TREE;
2703 }
2704
2705 /* Utility function of above to split a tree OP which may be a sum, into a
2706 constant part, which is returned, and a variable part, which is stored
2707 in *PVAR. *PVAR may be bitsize_zero_node. All operations must be of
2708 bitsizetype. */
2709
2710 static tree
2711 split_plus (tree in, tree *pvar)
2712 {
2713 /* Strip conversions in order to ease the tree traversal and maximize the
2714 potential for constant or plus/minus discovery. We need to be careful
2715 to always return and set *pvar to bitsizetype trees, but it's worth
2716 the effort. */
2717 in = remove_conversions (in, false);
2718
2719 *pvar = convert (bitsizetype, in);
2720
2721 if (TREE_CODE (in) == INTEGER_CST)
2722 {
2723 *pvar = bitsize_zero_node;
2724 return convert (bitsizetype, in);
2725 }
2726 else if (TREE_CODE (in) == PLUS_EXPR || TREE_CODE (in) == MINUS_EXPR)
2727 {
2728 tree lhs_var, rhs_var;
2729 tree lhs_con = split_plus (TREE_OPERAND (in, 0), &lhs_var);
2730 tree rhs_con = split_plus (TREE_OPERAND (in, 1), &rhs_var);
2731
2732 if (lhs_var == TREE_OPERAND (in, 0)
2733 && rhs_var == TREE_OPERAND (in, 1))
2734 return bitsize_zero_node;
2735
2736 *pvar = size_binop (TREE_CODE (in), lhs_var, rhs_var);
2737 return size_binop (TREE_CODE (in), lhs_con, rhs_con);
2738 }
2739 else
2740 return bitsize_zero_node;
2741 }
2742
2743 /* Return a copy of TYPE but safe to modify in any way. */
2744
2745 tree
2746 copy_type (tree type)
2747 {
2748 tree new_type = copy_node (type);
2749
2750 /* Unshare the language-specific data. */
2751 if (TYPE_LANG_SPECIFIC (type))
2752 {
2753 TYPE_LANG_SPECIFIC (new_type) = NULL;
2754 SET_TYPE_LANG_SPECIFIC (new_type, GET_TYPE_LANG_SPECIFIC (type));
2755 }
2756
2757 /* And the contents of the language-specific slot if needed. */
2758 if ((INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type))
2759 && TYPE_RM_VALUES (type))
2760 {
2761 TYPE_RM_VALUES (new_type) = NULL_TREE;
2762 SET_TYPE_RM_SIZE (new_type, TYPE_RM_SIZE (type));
2763 SET_TYPE_RM_MIN_VALUE (new_type, TYPE_RM_MIN_VALUE (type));
2764 SET_TYPE_RM_MAX_VALUE (new_type, TYPE_RM_MAX_VALUE (type));
2765 }
2766
2767 /* copy_node clears this field instead of copying it, because it is
2768 aliased with TREE_CHAIN. */
2769 TYPE_STUB_DECL (new_type) = TYPE_STUB_DECL (type);
2770
2771 TYPE_POINTER_TO (new_type) = NULL_TREE;
2772 TYPE_REFERENCE_TO (new_type) = NULL_TREE;
2773 TYPE_MAIN_VARIANT (new_type) = new_type;
2774 TYPE_NEXT_VARIANT (new_type) = NULL_TREE;
2775 TYPE_CANONICAL (new_type) = new_type;
2776
2777 return new_type;
2778 }
2779
2780 /* Return a subtype of sizetype with range MIN to MAX and whose
2781 TYPE_INDEX_TYPE is INDEX. GNAT_NODE is used for the position
2782 of the associated TYPE_DECL. */
2783
2784 tree
2785 create_index_type (tree min, tree max, tree index, Node_Id gnat_node)
2786 {
2787 /* First build a type for the desired range. */
2788 tree type = build_nonshared_range_type (sizetype, min, max);
2789
2790 /* Then set the index type. */
2791 SET_TYPE_INDEX_TYPE (type, index);
2792 create_type_decl (NULL_TREE, type, true, false, gnat_node);
2793
2794 return type;
2795 }
2796
2797 /* Return a subtype of TYPE with range MIN to MAX. If TYPE is NULL,
2798 sizetype is used. */
2799
2800 tree
2801 create_range_type (tree type, tree min, tree max)
2802 {
2803 tree range_type;
2804
2805 if (!type)
2806 type = sizetype;
2807
2808 /* First build a type with the base range. */
2809 range_type = build_nonshared_range_type (type, TYPE_MIN_VALUE (type),
2810 TYPE_MAX_VALUE (type));
2811
2812 /* Then set the actual range. */
2813 SET_TYPE_RM_MIN_VALUE (range_type, min);
2814 SET_TYPE_RM_MAX_VALUE (range_type, max);
2815
2816 return range_type;
2817 }
2818
2819 /* Return an extra subtype of TYPE with range MIN to MAX. */
2820
2821 tree
2822 create_extra_subtype (tree type, tree min, tree max)
2823 {
2824 const bool uns = TYPE_UNSIGNED (type);
2825 const unsigned prec = TYPE_PRECISION (type);
2826 tree subtype = uns ? make_unsigned_type (prec) : make_signed_type (prec);
2827
2828 TREE_TYPE (subtype) = type;
2829 TYPE_EXTRA_SUBTYPE_P (subtype) = 1;
2830
2831 SET_TYPE_RM_MIN_VALUE (subtype, min);
2832 SET_TYPE_RM_MAX_VALUE (subtype, max);
2833
2834 return subtype;
2835 }
2836
2837 /* Return a TYPE_DECL node suitable for the TYPE_STUB_DECL field of TYPE.
2838 NAME gives the name of the type to be used in the declaration. */
2839
2840 tree
2841 create_type_stub_decl (tree name, tree type)
2842 {
2843 tree type_decl = build_decl (input_location, TYPE_DECL, name, type);
2844 DECL_ARTIFICIAL (type_decl) = 1;
2845 DECL_NAMELESS (type_decl) = gnat_encodings != DWARF_GNAT_ENCODINGS_ALL;
2846 TYPE_ARTIFICIAL (type) = 1;
2847 return type_decl;
2848 }
2849
2850 /* Return a TYPE_DECL node for TYPE. NAME gives the name of the type to be
2851 used in the declaration. ARTIFICIAL_P is true if the declaration was
2852 generated by the compiler. DEBUG_INFO_P is true if we need to write
2853 debug information about this type. GNAT_NODE is used for the position
2854 of the decl. Normally, an artificial type might be marked as
2855 nameless. However, if CAN_BE_NAMELESS is false, this marking is
2856 disabled and the name will always be attached for the type. */
2857
2858 tree
2859 create_type_decl (tree name, tree type, bool artificial_p, bool debug_info_p,
2860 Node_Id gnat_node, bool can_be_nameless)
2861 {
2862 enum tree_code code = TREE_CODE (type);
2863 bool is_named
2864 = TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL;
2865 tree type_decl;
2866
2867 /* Only the builtin TYPE_STUB_DECL should be used for dummy types. */
2868 gcc_assert (!TYPE_IS_DUMMY_P (type));
2869
2870 /* If the type hasn't been named yet, we're naming it; preserve an existing
2871 TYPE_STUB_DECL that has been attached to it for some purpose. */
2872 if (!is_named && TYPE_STUB_DECL (type))
2873 {
2874 type_decl = TYPE_STUB_DECL (type);
2875 DECL_NAME (type_decl) = name;
2876 }
2877 else
2878 type_decl = build_decl (input_location, TYPE_DECL, name, type);
2879
2880 DECL_ARTIFICIAL (type_decl) = artificial_p;
2881 TYPE_ARTIFICIAL (type) = artificial_p;
2882 DECL_NAMELESS (type_decl) = (artificial_p
2883 && can_be_nameless
2884 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL);
2885
2886 /* Add this decl to the current binding level. */
2887 gnat_pushdecl (type_decl, gnat_node);
2888
2889 /* If we're naming the type, equate the TYPE_STUB_DECL to the name. This
2890 causes the name to be also viewed as a "tag" by the debug back-end, with
2891 the advantage that no DW_TAG_typedef is emitted for artificial "tagged"
2892 types in DWARF.
2893
2894 Note that if "type" is used as a DECL_ORIGINAL_TYPE, it may be referenced
2895 from multiple contexts, and "type_decl" references a copy of it: in such a
2896 case, do not mess TYPE_STUB_DECL: we do not want to re-use the TYPE_DECL
2897 with the mechanism above. */
2898 if (!is_named && type != DECL_ORIGINAL_TYPE (type_decl))
2899 TYPE_STUB_DECL (type) = type_decl;
2900
2901 /* Do not generate debug info for UNCONSTRAINED_ARRAY_TYPE that the
2902 back-end doesn't support, and for others if we don't need to. */
2903 if (code == UNCONSTRAINED_ARRAY_TYPE || !debug_info_p)
2904 DECL_IGNORED_P (type_decl) = 1;
2905
2906 return type_decl;
2907 }
2908
2909 /* Return a VAR_DECL or CONST_DECL node.
2910
2911 NAME gives the name of the variable. ASM_NAME is its assembler name
2912 (if provided). TYPE is its data type (a GCC ..._TYPE node). INIT is
2913 the GCC tree for an optional initial expression; NULL_TREE if none.
2914
2915 CONST_FLAG is true if this variable is constant, in which case we might
2916 return a CONST_DECL node unless CONST_DECL_ALLOWED_P is false.
2917
2918 PUBLIC_FLAG is true if this is for a reference to a public entity or for a
2919 definition to be made visible outside of the current compilation unit, for
2920 instance variable definitions in a package specification.
2921
2922 EXTERN_FLAG is true when processing an external variable declaration (as
2923 opposed to a definition: no storage is to be allocated for the variable).
2924
2925 STATIC_FLAG is only relevant when not at top level and indicates whether
2926 to always allocate storage to the variable.
2927
2928 VOLATILE_FLAG is true if this variable is declared as volatile.
2929
2930 ARTIFICIAL_P is true if the variable was generated by the compiler.
2931
2932 DEBUG_INFO_P is true if we need to write debug information for it.
2933
2934 ATTR_LIST is the list of attributes to be attached to the variable.
2935
2936 GNAT_NODE is used for the position of the decl. */
2937
2938 tree
2939 create_var_decl (tree name, tree asm_name, tree type, tree init,
2940 bool const_flag, bool public_flag, bool extern_flag,
2941 bool static_flag, bool volatile_flag, bool artificial_p,
2942 bool debug_info_p, struct attrib *attr_list,
2943 Node_Id gnat_node, bool const_decl_allowed_p)
2944 {
2945 /* Whether the object has static storage duration, either explicitly or by
2946 virtue of being declared at the global level. */
2947 const bool static_storage = static_flag || global_bindings_p ();
2948
2949 /* Whether the initializer is constant: for an external object or an object
2950 with static storage duration, we check that the initializer is a valid
2951 constant expression for initializing a static variable; otherwise, we
2952 only check that it is constant. */
2953 const bool init_const
2954 = (init
2955 && gnat_types_compatible_p (type, TREE_TYPE (init))
2956 && (extern_flag || static_storage
2957 ? initializer_constant_valid_p (init, TREE_TYPE (init))
2958 != NULL_TREE
2959 : TREE_CONSTANT (init)));
2960
2961 /* Whether we will make TREE_CONSTANT the DECL we produce here, in which
2962 case the initializer may be used in lieu of the DECL node (as done in
2963 Identifier_to_gnu). This is useful to prevent the need of elaboration
2964 code when an identifier for which such a DECL is made is in turn used
2965 as an initializer. We used to rely on CONST_DECL vs VAR_DECL for this,
2966 but extra constraints apply to this choice (see below) and they are not
2967 relevant to the distinction we wish to make. */
2968 const bool constant_p = const_flag && init_const;
2969
2970 /* The actual DECL node. CONST_DECL was initially intended for enumerals
2971 and may be used for scalars in general but not for aggregates. */
2972 tree var_decl
2973 = build_decl (input_location,
2974 (constant_p
2975 && const_decl_allowed_p
2976 && !AGGREGATE_TYPE_P (type) ? CONST_DECL : VAR_DECL),
2977 name, type);
2978
2979 /* Detect constants created by the front-end to hold 'reference to function
2980 calls for stabilization purposes. This is needed for renaming. */
2981 if (const_flag && init && POINTER_TYPE_P (type))
2982 {
2983 tree inner = init;
2984 if (TREE_CODE (inner) == COMPOUND_EXPR)
2985 inner = TREE_OPERAND (inner, 1);
2986 inner = remove_conversions (inner, true);
2987 if (TREE_CODE (inner) == ADDR_EXPR
2988 && ((TREE_CODE (TREE_OPERAND (inner, 0)) == CALL_EXPR
2989 && !call_is_atomic_load (TREE_OPERAND (inner, 0)))
2990 || (VAR_P (TREE_OPERAND (inner, 0))
2991 && DECL_RETURN_VALUE_P (TREE_OPERAND (inner, 0)))))
2992 DECL_RETURN_VALUE_P (var_decl) = 1;
2993 }
2994
2995 /* If this is external, throw away any initializations (they will be done
2996 elsewhere) unless this is a constant for which we would like to remain
2997 able to get the initializer. If we are defining a global here, leave a
2998 constant initialization and save any variable elaborations for the
2999 elaboration routine. If we are just annotating types, throw away the
3000 initialization if it isn't a constant. */
3001 if ((extern_flag && !constant_p)
3002 || (type_annotate_only && init && !TREE_CONSTANT (init)))
3003 init = NULL_TREE;
3004
3005 /* At the global level, a non-constant initializer generates elaboration
3006 statements. Check that such statements are allowed, that is to say,
3007 not violating a No_Elaboration_Code restriction. */
3008 if (init && !init_const && global_bindings_p ())
3009 Check_Elaboration_Code_Allowed (gnat_node);
3010
3011 /* Attach the initializer, if any. */
3012 DECL_INITIAL (var_decl) = init;
3013
3014 /* Directly set some flags. */
3015 DECL_ARTIFICIAL (var_decl) = artificial_p;
3016 DECL_EXTERNAL (var_decl) = extern_flag;
3017
3018 TREE_CONSTANT (var_decl) = constant_p;
3019 TREE_READONLY (var_decl) = const_flag;
3020
3021 /* The object is public if it is external or if it is declared public
3022 and has static storage duration. */
3023 TREE_PUBLIC (var_decl) = extern_flag || (public_flag && static_storage);
3024
3025 /* We need to allocate static storage for an object with static storage
3026 duration if it isn't external. */
3027 TREE_STATIC (var_decl) = !extern_flag && static_storage;
3028
3029 TREE_SIDE_EFFECTS (var_decl)
3030 = TREE_THIS_VOLATILE (var_decl)
3031 = TYPE_VOLATILE (type) | volatile_flag;
3032
3033 if (TREE_SIDE_EFFECTS (var_decl))
3034 TREE_ADDRESSABLE (var_decl) = 1;
3035
3036 /* Ada doesn't feature Fortran-like COMMON variables so we shouldn't
3037 try to fiddle with DECL_COMMON. However, on platforms that don't
3038 support global BSS sections, uninitialized global variables would
3039 go in DATA instead, thus increasing the size of the executable. */
3040 if (!flag_no_common
3041 && VAR_P (var_decl)
3042 && TREE_PUBLIC (var_decl)
3043 && !have_global_bss_p ())
3044 DECL_COMMON (var_decl) = 1;
3045
3046 /* Do not emit debug info if not requested, or for an external constant whose
3047 initializer is not absolute because this would require a global relocation
3048 in a read-only section which runs afoul of the PE-COFF run-time relocation
3049 mechanism. */
3050 if (!debug_info_p
3051 || (extern_flag
3052 && constant_p
3053 && init
3054 && initializer_constant_valid_p (init, TREE_TYPE (init))
3055 != null_pointer_node))
3056 DECL_IGNORED_P (var_decl) = 1;
3057
3058 /* ??? Some attributes cannot be applied to CONST_DECLs. */
3059 if (VAR_P (var_decl))
3060 process_attributes (&var_decl, &attr_list, true, gnat_node);
3061
3062 /* Add this decl to the current binding level. */
3063 gnat_pushdecl (var_decl, gnat_node);
3064
3065 if (VAR_P (var_decl) && asm_name)
3066 {
3067 /* Let the target mangle the name if this isn't a verbatim asm. */
3068 if (*IDENTIFIER_POINTER (asm_name) != '*')
3069 asm_name = targetm.mangle_decl_assembler_name (var_decl, asm_name);
3070
3071 SET_DECL_ASSEMBLER_NAME (var_decl, asm_name);
3072 }
3073
3074 return var_decl;
3075 }
3076
3077 /* Return true if TYPE, an aggregate type, contains (or is) an array. */
3078
3079 static bool
3080 aggregate_type_contains_array_p (tree type)
3081 {
3082 switch (TREE_CODE (type))
3083 {
3084 case RECORD_TYPE:
3085 case UNION_TYPE:
3086 case QUAL_UNION_TYPE:
3087 {
3088 tree field;
3089 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3090 if (AGGREGATE_TYPE_P (TREE_TYPE (field))
3091 && aggregate_type_contains_array_p (TREE_TYPE (field)))
3092 return true;
3093 return false;
3094 }
3095
3096 case ARRAY_TYPE:
3097 return true;
3098
3099 default:
3100 gcc_unreachable ();
3101 }
3102 }
3103
3104 /* Return true if TYPE is a type with variable size or a padding type with a
3105 field of variable size or a record that has a field with such a type. */
3106
3107 bool
3108 type_has_variable_size (tree type)
3109 {
3110 tree field;
3111
3112 if (!TREE_CONSTANT (TYPE_SIZE (type)))
3113 return true;
3114
3115 if (TYPE_IS_PADDING_P (type)
3116 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
3117 return true;
3118
3119 if (!RECORD_OR_UNION_TYPE_P (type))
3120 return false;
3121
3122 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3123 if (type_has_variable_size (TREE_TYPE (field)))
3124 return true;
3125
3126 return false;
3127 }
3128
3129 /* Return a FIELD_DECL node. NAME is the field's name, TYPE is its type and
3130 RECORD_TYPE is the type of the enclosing record. If SIZE is nonzero, it
3131 is the specified size of the field. If POS is nonzero, it is the bit
3132 position. PACKED is 1 if the enclosing record is packed, -1 if it has
3133 Component_Alignment of Storage_Unit. If ADDRESSABLE is nonzero, it
3134 means we are allowed to take the address of the field; if it is negative,
3135 we should not make a bitfield, which is used by make_aligning_type. */
3136
3137 tree
3138 create_field_decl (tree name, tree type, tree record_type, tree size, tree pos,
3139 int packed, int addressable)
3140 {
3141 tree field_decl = build_decl (input_location, FIELD_DECL, name, type);
3142
3143 DECL_CONTEXT (field_decl) = record_type;
3144 TREE_READONLY (field_decl) = TYPE_READONLY (type);
3145
3146 /* If a size is specified, use it. Otherwise, if the record type is packed
3147 compute a size to use, which may differ from the object's natural size.
3148 We always set a size in this case to trigger the checks for bitfield
3149 creation below, which is typically required when no position has been
3150 specified. */
3151 if (size)
3152 size = convert (bitsizetype, size);
3153 else if (packed == 1)
3154 {
3155 size = rm_size (type);
3156 if (TYPE_MODE (type) == BLKmode)
3157 size = round_up (size, BITS_PER_UNIT);
3158 }
3159
3160 /* If we may, according to ADDRESSABLE, then make a bitfield when the size
3161 is specified for two reasons: first, when it differs from the natural
3162 size; second, when the alignment is insufficient.
3163
3164 We never make a bitfield if the type of the field has a nonconstant size,
3165 because no such entity requiring bitfield operations should reach here.
3166
3167 We do *preventively* make a bitfield when there might be the need for it
3168 but we don't have all the necessary information to decide, as is the case
3169 of a field in a packed record.
3170
3171 We also don't look at STRICT_ALIGNMENT here, and rely on later processing
3172 in layout_decl or finish_record_type to clear the bit_field indication if
3173 it is in fact not needed. */
3174 if (addressable >= 0
3175 && size
3176 && TREE_CODE (size) == INTEGER_CST
3177 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3178 && (packed
3179 || !tree_int_cst_equal (size, TYPE_SIZE (type))
3180 || (pos && !value_factor_p (pos, TYPE_ALIGN (type)))
3181 || (TYPE_ALIGN (record_type)
3182 && TYPE_ALIGN (record_type) < TYPE_ALIGN (type))))
3183 {
3184 DECL_BIT_FIELD (field_decl) = 1;
3185 DECL_SIZE (field_decl) = size;
3186 if (!packed && !pos)
3187 {
3188 if (TYPE_ALIGN (record_type)
3189 && TYPE_ALIGN (record_type) < TYPE_ALIGN (type))
3190 SET_DECL_ALIGN (field_decl, TYPE_ALIGN (record_type));
3191 else
3192 SET_DECL_ALIGN (field_decl, TYPE_ALIGN (type));
3193 }
3194 }
3195
3196 DECL_PACKED (field_decl) = pos ? DECL_BIT_FIELD (field_decl) : packed;
3197
3198 /* If FIELD_TYPE has BLKmode, we must ensure this is aligned to at least
3199 a byte boundary since GCC cannot handle less aligned BLKmode bitfields.
3200 Likewise if it has a variable size and no specified position because
3201 variable-sized objects need to be aligned to at least a byte boundary.
3202 Likewise for an aggregate without specified position that contains an
3203 array because, in this case, slices of variable length of this array
3204 must be handled by GCC and have variable size. */
3205 if (packed && (TYPE_MODE (type) == BLKmode
3206 || (!pos && type_has_variable_size (type))
3207 || (!pos
3208 && AGGREGATE_TYPE_P (type)
3209 && aggregate_type_contains_array_p (type))))
3210 SET_DECL_ALIGN (field_decl, BITS_PER_UNIT);
3211
3212 /* Bump the alignment if need be, either for bitfield/packing purposes or
3213 to satisfy the type requirements if no such considerations apply. When
3214 we get the alignment from the type, indicate if this is from an explicit
3215 user request, which prevents stor-layout from lowering it later on. */
3216 else
3217 {
3218 const unsigned int field_align
3219 = DECL_BIT_FIELD (field_decl)
3220 ? 1
3221 : packed
3222 ? BITS_PER_UNIT
3223 : 0;
3224
3225 if (field_align > DECL_ALIGN (field_decl))
3226 SET_DECL_ALIGN (field_decl, field_align);
3227 else if (!field_align && TYPE_ALIGN (type) > DECL_ALIGN (field_decl))
3228 {
3229 SET_DECL_ALIGN (field_decl, TYPE_ALIGN (type));
3230 DECL_USER_ALIGN (field_decl) = TYPE_USER_ALIGN (type);
3231 }
3232 }
3233
3234 if (pos)
3235 {
3236 /* We need to pass in the alignment the DECL is known to have.
3237 This is the lowest-order bit set in POS, but no more than
3238 the alignment of the record, if one is specified. Note
3239 that an alignment of 0 is taken as infinite. */
3240 unsigned int known_align;
3241
3242 if (tree_fits_uhwi_p (pos))
3243 known_align = tree_to_uhwi (pos) & -tree_to_uhwi (pos);
3244 else
3245 known_align = BITS_PER_UNIT;
3246
3247 if (TYPE_ALIGN (record_type)
3248 && (known_align == 0 || known_align > TYPE_ALIGN (record_type)))
3249 known_align = TYPE_ALIGN (record_type);
3250
3251 layout_decl (field_decl, known_align);
3252 SET_DECL_OFFSET_ALIGN (field_decl,
3253 tree_fits_uhwi_p (pos)
3254 ? BIGGEST_ALIGNMENT : BITS_PER_UNIT);
3255 pos_from_bit (&DECL_FIELD_OFFSET (field_decl),
3256 &DECL_FIELD_BIT_OFFSET (field_decl),
3257 DECL_OFFSET_ALIGN (field_decl), pos);
3258 }
3259
3260 /* In addition to what our caller says, claim the field is addressable if we
3261 know that its type is not suitable.
3262
3263 The field may also be "technically" nonaddressable, meaning that even if
3264 we attempt to take the field's address we will actually get the address
3265 of a copy. This is the case for true bitfields, but the DECL_BIT_FIELD
3266 value we have at this point is not accurate enough, so we don't account
3267 for this here and let finish_record_type decide. */
3268 if (!addressable && !type_for_nonaliased_component_p (type))
3269 addressable = 1;
3270
3271 /* Note that there is a trade-off in making a field nonaddressable because
3272 this will cause type-based alias analysis to use the same alias set for
3273 accesses to the field as for accesses to the whole record: while doing
3274 so will make it more likely to disambiguate accesses to other objects
3275 and accesses to the field, it will make it less likely to disambiguate
3276 accesses to the other fields of the record and accesses to the field.
3277 If the record is fully static, then the trade-off is irrelevant since
3278 the fields of the record can always be disambiguated by their offsets
3279 but, if the record is dynamic, then it can become problematic. */
3280 DECL_NONADDRESSABLE_P (field_decl) = !addressable;
3281
3282 return field_decl;
3283 }
3284
3285 /* Return a PARM_DECL node with NAME and TYPE. */
3286
3287 tree
3288 create_param_decl (tree name, tree type)
3289 {
3290 tree param_decl = build_decl (input_location, PARM_DECL, name, type);
3291
3292 /* Honor TARGET_PROMOTE_PROTOTYPES like the C compiler, as not doing so
3293 can lead to various ABI violations. */
3294 if (targetm.calls.promote_prototypes (NULL_TREE)
3295 && INTEGRAL_TYPE_P (type)
3296 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3297 {
3298 /* We have to be careful about biased types here. Make a subtype
3299 of integer_type_node with the proper biasing. */
3300 if (TREE_CODE (type) == INTEGER_TYPE
3301 && TYPE_BIASED_REPRESENTATION_P (type))
3302 {
3303 tree subtype
3304 = make_unsigned_type (TYPE_PRECISION (integer_type_node));
3305 TREE_TYPE (subtype) = integer_type_node;
3306 TYPE_BIASED_REPRESENTATION_P (subtype) = 1;
3307 SET_TYPE_RM_MIN_VALUE (subtype, TYPE_MIN_VALUE (type));
3308 SET_TYPE_RM_MAX_VALUE (subtype, TYPE_MAX_VALUE (type));
3309 type = subtype;
3310 }
3311 else
3312 type = integer_type_node;
3313 }
3314
3315 DECL_ARG_TYPE (param_decl) = type;
3316 return param_decl;
3317 }
3318
3319 /* Process the attributes in ATTR_LIST for NODE, which is either a DECL or
3320 a TYPE. If IN_PLACE is true, the tree pointed to by NODE should not be
3321 changed. GNAT_NODE is used for the position of error messages. */
3322
3323 void
3324 process_attributes (tree *node, struct attrib **attr_list, bool in_place,
3325 Node_Id gnat_node)
3326 {
3327 struct attrib *attr;
3328
3329 for (attr = *attr_list; attr; attr = attr->next)
3330 switch (attr->type)
3331 {
3332 case ATTR_MACHINE_ATTRIBUTE:
3333 Sloc_to_locus (Sloc (gnat_node), &input_location);
3334 decl_attributes (node, tree_cons (attr->name, attr->args, NULL_TREE),
3335 in_place ? ATTR_FLAG_TYPE_IN_PLACE : 0);
3336 break;
3337
3338 case ATTR_LINK_ALIAS:
3339 if (!DECL_EXTERNAL (*node))
3340 {
3341 TREE_STATIC (*node) = 1;
3342 assemble_alias (*node, attr->name);
3343 }
3344 break;
3345
3346 case ATTR_WEAK_EXTERNAL:
3347 if (SUPPORTS_WEAK)
3348 declare_weak (*node);
3349 else
3350 post_error ("?weak declarations not supported on this target",
3351 attr->error_point);
3352 break;
3353
3354 case ATTR_LINK_SECTION:
3355 if (targetm_common.have_named_sections)
3356 {
3357 set_decl_section_name (*node, IDENTIFIER_POINTER (attr->name));
3358 DECL_COMMON (*node) = 0;
3359 }
3360 else
3361 post_error ("?section attributes are not supported for this target",
3362 attr->error_point);
3363 break;
3364
3365 case ATTR_LINK_CONSTRUCTOR:
3366 DECL_STATIC_CONSTRUCTOR (*node) = 1;
3367 TREE_USED (*node) = 1;
3368 break;
3369
3370 case ATTR_LINK_DESTRUCTOR:
3371 DECL_STATIC_DESTRUCTOR (*node) = 1;
3372 TREE_USED (*node) = 1;
3373 break;
3374
3375 case ATTR_THREAD_LOCAL_STORAGE:
3376 set_decl_tls_model (*node, decl_default_tls_model (*node));
3377 DECL_COMMON (*node) = 0;
3378 break;
3379 }
3380
3381 *attr_list = NULL;
3382 }
3383
3384 /* Return true if VALUE is a known to be a multiple of FACTOR, which must be
3385 a power of 2. */
3386
3387 bool
3388 value_factor_p (tree value, unsigned HOST_WIDE_INT factor)
3389 {
3390 gcc_checking_assert (pow2p_hwi (factor));
3391
3392 if (tree_fits_uhwi_p (value))
3393 return (tree_to_uhwi (value) & (factor - 1)) == 0;
3394
3395 if (TREE_CODE (value) == MULT_EXPR)
3396 return (value_factor_p (TREE_OPERAND (value, 0), factor)
3397 || value_factor_p (TREE_OPERAND (value, 1), factor));
3398
3399 return false;
3400 }
3401
3402 /* Defer the initialization of DECL's DECL_CONTEXT attribute, scheduling to
3403 feed it with the elaboration of GNAT_SCOPE. */
3404
3405 static struct deferred_decl_context_node *
3406 add_deferred_decl_context (tree decl, Entity_Id gnat_scope, int force_global)
3407 {
3408 struct deferred_decl_context_node *new_node;
3409
3410 new_node
3411 = (struct deferred_decl_context_node * ) xmalloc (sizeof (*new_node));
3412 new_node->decl = decl;
3413 new_node->gnat_scope = gnat_scope;
3414 new_node->force_global = force_global;
3415 new_node->types.create (1);
3416 new_node->next = deferred_decl_context_queue;
3417 deferred_decl_context_queue = new_node;
3418 return new_node;
3419 }
3420
3421 /* Defer the initialization of TYPE's TYPE_CONTEXT attribute, scheduling to
3422 feed it with the DECL_CONTEXT computed as part of N as soon as it is
3423 computed. */
3424
3425 static void
3426 add_deferred_type_context (struct deferred_decl_context_node *n, tree type)
3427 {
3428 n->types.safe_push (type);
3429 }
3430
3431 /* Get the GENERIC node corresponding to GNAT_SCOPE, if available. Return
3432 NULL_TREE if it is not available. */
3433
3434 static tree
3435 compute_deferred_decl_context (Entity_Id gnat_scope)
3436 {
3437 tree context;
3438
3439 if (present_gnu_tree (gnat_scope))
3440 context = get_gnu_tree (gnat_scope);
3441 else
3442 return NULL_TREE;
3443
3444 if (TREE_CODE (context) == TYPE_DECL)
3445 {
3446 tree context_type = TREE_TYPE (context);
3447
3448 /* Skip dummy types: only the final ones can appear in the context
3449 chain. */
3450 if (TYPE_DUMMY_P (context_type))
3451 return NULL_TREE;
3452
3453 /* ..._TYPE nodes are more useful than TYPE_DECL nodes in the context
3454 chain. */
3455 else
3456 context = context_type;
3457 }
3458
3459 return context;
3460 }
3461
3462 /* Try to process all deferred nodes in the queue. Keep in the queue the ones
3463 that cannot be processed yet, remove the other ones. If FORCE is true,
3464 force the processing for all nodes, use the global context when nodes don't
3465 have a GNU translation. */
3466
3467 void
3468 process_deferred_decl_context (bool force)
3469 {
3470 struct deferred_decl_context_node **it = &deferred_decl_context_queue;
3471 struct deferred_decl_context_node *node;
3472
3473 while (*it)
3474 {
3475 bool processed = false;
3476 tree context = NULL_TREE;
3477 Entity_Id gnat_scope;
3478
3479 node = *it;
3480
3481 /* If FORCE, get the innermost elaborated scope. Otherwise, just try to
3482 get the first scope. */
3483 gnat_scope = node->gnat_scope;
3484 while (Present (gnat_scope))
3485 {
3486 context = compute_deferred_decl_context (gnat_scope);
3487 if (!force || context)
3488 break;
3489 gnat_scope = get_debug_scope (gnat_scope, NULL);
3490 }
3491
3492 /* Imported declarations must not be in a local context (i.e. not inside
3493 a function). */
3494 if (context && node->force_global > 0)
3495 {
3496 tree ctx = context;
3497
3498 while (ctx)
3499 {
3500 gcc_assert (TREE_CODE (ctx) != FUNCTION_DECL);
3501 ctx = DECL_P (ctx) ? DECL_CONTEXT (ctx) : TYPE_CONTEXT (ctx);
3502 }
3503 }
3504
3505 /* If FORCE, we want to get rid of all nodes in the queue: in case there
3506 was no elaborated scope, use the global context. */
3507 if (force && !context)
3508 context = get_global_context ();
3509
3510 if (context)
3511 {
3512 tree t;
3513 int i;
3514
3515 DECL_CONTEXT (node->decl) = context;
3516
3517 /* Propagate it to the TYPE_CONTEXT attributes of the requested
3518 ..._TYPE nodes. */
3519 FOR_EACH_VEC_ELT (node->types, i, t)
3520 {
3521 gnat_set_type_context (t, context);
3522 }
3523 processed = true;
3524 }
3525
3526 /* If this node has been successfuly processed, remove it from the
3527 queue. Then move to the next node. */
3528 if (processed)
3529 {
3530 *it = node->next;
3531 node->types.release ();
3532 free (node);
3533 }
3534 else
3535 it = &node->next;
3536 }
3537 }
3538
3539 /* Return VALUE scaled by the biggest power-of-2 factor of EXPR. */
3540
3541 static unsigned int
3542 scale_by_factor_of (tree expr, unsigned int value)
3543 {
3544 unsigned HOST_WIDE_INT addend = 0;
3545 unsigned HOST_WIDE_INT factor = 1;
3546
3547 /* Peel conversions around EXPR and try to extract bodies from function
3548 calls: it is possible to get the scale factor from size functions. */
3549 expr = remove_conversions (expr, true);
3550 if (TREE_CODE (expr) == CALL_EXPR)
3551 expr = maybe_inline_call_in_expr (expr);
3552
3553 /* Sometimes we get PLUS_EXPR (BIT_AND_EXPR (..., X), Y), where Y is a
3554 multiple of the scale factor we are looking for. */
3555 if (TREE_CODE (expr) == PLUS_EXPR
3556 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
3557 && tree_fits_uhwi_p (TREE_OPERAND (expr, 1)))
3558 {
3559 addend = TREE_INT_CST_LOW (TREE_OPERAND (expr, 1));
3560 expr = TREE_OPERAND (expr, 0);
3561 }
3562
3563 /* An expression which is a bitwise AND with a mask has a power-of-2 factor
3564 corresponding to the number of trailing zeros of the mask. */
3565 if (TREE_CODE (expr) == BIT_AND_EXPR
3566 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
3567 {
3568 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (TREE_OPERAND (expr, 1));
3569 unsigned int i = 0;
3570
3571 while ((mask & 1) == 0 && i < HOST_BITS_PER_WIDE_INT)
3572 {
3573 mask >>= 1;
3574 factor *= 2;
3575 i++;
3576 }
3577 }
3578
3579 /* If the addend is not a multiple of the factor we found, give up. In
3580 theory we could find a smaller common factor but it's useless for our
3581 needs. This situation arises when dealing with a field F1 with no
3582 alignment requirement but that is following a field F2 with such
3583 requirements. As long as we have F2's offset, we don't need alignment
3584 information to compute F1's. */
3585 if (addend % factor != 0)
3586 factor = 1;
3587
3588 return factor * value;
3589 }
3590
3591 /* Return a LABEL_DECL with NAME. GNAT_NODE is used for the position of
3592 the decl. */
3593
3594 tree
3595 create_label_decl (tree name, Node_Id gnat_node)
3596 {
3597 tree label_decl
3598 = build_decl (input_location, LABEL_DECL, name, void_type_node);
3599
3600 SET_DECL_MODE (label_decl, VOIDmode);
3601
3602 /* Add this decl to the current binding level. */
3603 gnat_pushdecl (label_decl, gnat_node);
3604
3605 return label_decl;
3606 }
3607
3608 /* Return a FUNCTION_DECL node. NAME is the name of the subprogram, ASM_NAME
3609 its assembler name, TYPE its type (a FUNCTION_TYPE or METHOD_TYPE node),
3610 PARAM_DECL_LIST the list of its parameters (a list of PARM_DECL nodes
3611 chained through the DECL_CHAIN field).
3612
3613 INLINE_STATUS describes the inline flags to be set on the FUNCTION_DECL.
3614
3615 PUBLIC_FLAG is true if this is for a reference to a public entity or for a
3616 definition to be made visible outside of the current compilation unit.
3617
3618 EXTERN_FLAG is true when processing an external subprogram declaration.
3619
3620 ARTIFICIAL_P is true if the subprogram was generated by the compiler.
3621
3622 DEBUG_INFO_P is true if we need to write debug information for it.
3623
3624 DEFINITION is true if the subprogram is to be considered as a definition.
3625
3626 ATTR_LIST is the list of attributes to be attached to the subprogram.
3627
3628 GNAT_NODE is used for the position of the decl. */
3629
3630 tree
3631 create_subprog_decl (tree name, tree asm_name, tree type, tree param_decl_list,
3632 enum inline_status_t inline_status, bool public_flag,
3633 bool extern_flag, bool artificial_p, bool debug_info_p,
3634 bool definition, struct attrib *attr_list,
3635 Node_Id gnat_node)
3636 {
3637 tree subprog_decl = build_decl (input_location, FUNCTION_DECL, name, type);
3638 DECL_ARGUMENTS (subprog_decl) = param_decl_list;
3639
3640 DECL_ARTIFICIAL (subprog_decl) = artificial_p;
3641 DECL_EXTERNAL (subprog_decl) = extern_flag;
3642 DECL_FUNCTION_IS_DEF (subprog_decl) = definition;
3643 DECL_IGNORED_P (subprog_decl) = !debug_info_p;
3644 TREE_PUBLIC (subprog_decl) = public_flag;
3645
3646 switch (inline_status)
3647 {
3648 case is_suppressed:
3649 DECL_UNINLINABLE (subprog_decl) = 1;
3650 break;
3651
3652 case is_default:
3653 break;
3654
3655 case is_required:
3656 if (Back_End_Inlining)
3657 {
3658 decl_attributes (&subprog_decl,
3659 tree_cons (get_identifier ("always_inline"),
3660 NULL_TREE, NULL_TREE),
3661 ATTR_FLAG_TYPE_IN_PLACE);
3662
3663 /* Inline_Always guarantees that every direct call is inlined and
3664 that there is no indirect reference to the subprogram, so the
3665 instance in the original package (as well as its clones in the
3666 client packages created for inter-unit inlining) can be made
3667 private, which causes the out-of-line body to be eliminated. */
3668 TREE_PUBLIC (subprog_decl) = 0;
3669 }
3670
3671 /* ... fall through ... */
3672
3673 case is_prescribed:
3674 DECL_DISREGARD_INLINE_LIMITS (subprog_decl) = 1;
3675
3676 /* ... fall through ... */
3677
3678 case is_requested:
3679 DECL_DECLARED_INLINE_P (subprog_decl) = 1;
3680 if (!Debug_Generated_Code)
3681 DECL_NO_INLINE_WARNING_P (subprog_decl) = artificial_p;
3682 break;
3683
3684 default:
3685 gcc_unreachable ();
3686 }
3687
3688 process_attributes (&subprog_decl, &attr_list, true, gnat_node);
3689
3690 /* Once everything is processed, finish the subprogram declaration. */
3691 finish_subprog_decl (subprog_decl, asm_name, type);
3692
3693 /* Add this decl to the current binding level. */
3694 gnat_pushdecl (subprog_decl, gnat_node);
3695
3696 /* Output the assembler code and/or RTL for the declaration. */
3697 rest_of_decl_compilation (subprog_decl, global_bindings_p (), 0);
3698
3699 return subprog_decl;
3700 }
3701
3702 /* Given a subprogram declaration DECL, its assembler name and its type,
3703 finish constructing the subprogram declaration from ASM_NAME and TYPE. */
3704
3705 void
3706 finish_subprog_decl (tree decl, tree asm_name, tree type)
3707 {
3708 /* DECL_ARGUMENTS is set by the caller, but not its context. */
3709 for (tree param_decl = DECL_ARGUMENTS (decl);
3710 param_decl;
3711 param_decl = DECL_CHAIN (param_decl))
3712 DECL_CONTEXT (param_decl) = decl;
3713
3714 tree result_decl
3715 = build_decl (DECL_SOURCE_LOCATION (decl), RESULT_DECL, NULL_TREE,
3716 TREE_TYPE (type));
3717
3718 DECL_ARTIFICIAL (result_decl) = 1;
3719 DECL_IGNORED_P (result_decl) = 1;
3720 DECL_CONTEXT (result_decl) = decl;
3721 DECL_BY_REFERENCE (result_decl) = TREE_ADDRESSABLE (type);
3722 DECL_RESULT (decl) = result_decl;
3723
3724 /* Propagate the "pure" property. */
3725 DECL_PURE_P (decl) = TYPE_RESTRICT (type);
3726
3727 /* Propagate the "noreturn" property. */
3728 TREE_THIS_VOLATILE (decl) = TYPE_VOLATILE (type);
3729
3730 if (asm_name)
3731 {
3732 /* Let the target mangle the name if this isn't a verbatim asm. */
3733 if (*IDENTIFIER_POINTER (asm_name) != '*')
3734 asm_name = targetm.mangle_decl_assembler_name (decl, asm_name);
3735
3736 SET_DECL_ASSEMBLER_NAME (decl, asm_name);
3737
3738 /* The expand_main_function circuitry expects "main_identifier_node" to
3739 designate the DECL_NAME of the 'main' entry point, in turn expected
3740 to be declared as the "main" function literally by default. Ada
3741 program entry points are typically declared with a different name
3742 within the binder generated file, exported as 'main' to satisfy the
3743 system expectations. Force main_identifier_node in this case. */
3744 if (asm_name == main_identifier_node)
3745 DECL_NAME (decl) = main_identifier_node;
3746 }
3747 }
3748
3749 /* Set up the framework for generating code for SUBPROG_DECL, a subprogram
3750 body. This routine needs to be invoked before processing the declarations
3751 appearing in the subprogram. */
3752
3753 void
3754 begin_subprog_body (tree subprog_decl)
3755 {
3756 announce_function (subprog_decl);
3757
3758 /* This function is being defined. */
3759 TREE_STATIC (subprog_decl) = 1;
3760
3761 /* The failure of this assertion will likely come from a wrong context for
3762 the subprogram body, e.g. another procedure for a procedure declared at
3763 library level. */
3764 gcc_assert (current_function_decl == decl_function_context (subprog_decl));
3765
3766 current_function_decl = subprog_decl;
3767
3768 /* Enter a new binding level and show that all the parameters belong to
3769 this function. */
3770 gnat_pushlevel ();
3771 }
3772
3773 /* Finish translating the current subprogram and set its BODY. */
3774
3775 void
3776 end_subprog_body (tree body)
3777 {
3778 tree fndecl = current_function_decl;
3779
3780 /* Attach the BLOCK for this level to the function and pop the level. */
3781 BLOCK_SUPERCONTEXT (current_binding_level->block) = fndecl;
3782 DECL_INITIAL (fndecl) = current_binding_level->block;
3783 gnat_poplevel ();
3784
3785 /* The body should be a BIND_EXPR whose BLOCK is the top-level one. */
3786 if (TREE_CODE (body) == BIND_EXPR)
3787 {
3788 BLOCK_SUPERCONTEXT (BIND_EXPR_BLOCK (body)) = fndecl;
3789 DECL_INITIAL (fndecl) = BIND_EXPR_BLOCK (body);
3790 }
3791
3792 DECL_SAVED_TREE (fndecl) = body;
3793
3794 current_function_decl = decl_function_context (fndecl);
3795 }
3796
3797 /* Wrap up compilation of SUBPROG_DECL, a subprogram body. */
3798
3799 void
3800 rest_of_subprog_body_compilation (tree subprog_decl)
3801 {
3802 /* We cannot track the location of errors past this point. */
3803 Current_Error_Node = Empty;
3804
3805 /* If we're only annotating types, don't actually compile this function. */
3806 if (type_annotate_only)
3807 return;
3808
3809 /* Dump functions before gimplification. */
3810 dump_function (TDI_original, subprog_decl);
3811
3812 if (!decl_function_context (subprog_decl))
3813 cgraph_node::finalize_function (subprog_decl, false);
3814 else
3815 /* Register this function with cgraph just far enough to get it
3816 added to our parent's nested function list. */
3817 (void) cgraph_node::get_create (subprog_decl);
3818 }
3819
3820 tree
3821 gnat_builtin_function (tree decl)
3822 {
3823 gnat_pushdecl (decl, Empty);
3824 return decl;
3825 }
3826
3827 /* Return an integer type with the number of bits of precision given by
3828 PRECISION. UNSIGNEDP is nonzero if the type is unsigned; otherwise
3829 it is a signed type. */
3830
3831 tree
3832 gnat_type_for_size (unsigned precision, int unsignedp)
3833 {
3834 tree t;
3835 char type_name[20];
3836
3837 if (precision <= 2 * MAX_BITS_PER_WORD
3838 && signed_and_unsigned_types[precision][unsignedp])
3839 return signed_and_unsigned_types[precision][unsignedp];
3840
3841 if (unsignedp)
3842 t = make_unsigned_type (precision);
3843 else
3844 t = make_signed_type (precision);
3845 TYPE_ARTIFICIAL (t) = 1;
3846
3847 if (precision <= 2 * MAX_BITS_PER_WORD)
3848 signed_and_unsigned_types[precision][unsignedp] = t;
3849
3850 if (!TYPE_NAME (t))
3851 {
3852 sprintf (type_name, "%sSIGNED_%u", unsignedp ? "UN" : "", precision);
3853 TYPE_NAME (t) = get_identifier (type_name);
3854 }
3855
3856 return t;
3857 }
3858
3859 /* Likewise for floating-point types. */
3860
3861 static tree
3862 float_type_for_precision (int precision, machine_mode mode)
3863 {
3864 tree t;
3865 char type_name[20];
3866
3867 if (float_types[(int) mode])
3868 return float_types[(int) mode];
3869
3870 float_types[(int) mode] = t = make_node (REAL_TYPE);
3871 TYPE_PRECISION (t) = precision;
3872 layout_type (t);
3873
3874 gcc_assert (TYPE_MODE (t) == mode);
3875 if (!TYPE_NAME (t))
3876 {
3877 sprintf (type_name, "FLOAT_%d", precision);
3878 TYPE_NAME (t) = get_identifier (type_name);
3879 }
3880
3881 return t;
3882 }
3883
3884 /* Return a data type that has machine mode MODE. UNSIGNEDP selects
3885 an unsigned type; otherwise a signed type is returned. */
3886
3887 tree
3888 gnat_type_for_mode (machine_mode mode, int unsignedp)
3889 {
3890 if (mode == BLKmode)
3891 return NULL_TREE;
3892
3893 if (mode == VOIDmode)
3894 return void_type_node;
3895
3896 if (COMPLEX_MODE_P (mode))
3897 return NULL_TREE;
3898
3899 scalar_float_mode float_mode;
3900 if (is_a <scalar_float_mode> (mode, &float_mode))
3901 return float_type_for_precision (GET_MODE_PRECISION (float_mode),
3902 float_mode);
3903
3904 scalar_int_mode int_mode;
3905 if (is_a <scalar_int_mode> (mode, &int_mode))
3906 return gnat_type_for_size (GET_MODE_BITSIZE (int_mode), unsignedp);
3907
3908 if (VECTOR_MODE_P (mode))
3909 {
3910 machine_mode inner_mode = GET_MODE_INNER (mode);
3911 tree inner_type = gnat_type_for_mode (inner_mode, unsignedp);
3912 if (inner_type)
3913 return build_vector_type_for_mode (inner_type, mode);
3914 }
3915
3916 return NULL_TREE;
3917 }
3918
3919 /* Return the signed or unsigned version of TYPE_NODE, a scalar type, the
3920 signedness being specified by UNSIGNEDP. */
3921
3922 tree
3923 gnat_signed_or_unsigned_type_for (int unsignedp, tree type_node)
3924 {
3925 if (type_node == char_type_node)
3926 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3927
3928 tree type = gnat_type_for_size (TYPE_PRECISION (type_node), unsignedp);
3929
3930 if (TREE_CODE (type_node) == INTEGER_TYPE && TYPE_MODULAR_P (type_node))
3931 {
3932 type = copy_type (type);
3933 TREE_TYPE (type) = type_node;
3934 }
3935 else if (TREE_TYPE (type_node)
3936 && TREE_CODE (TREE_TYPE (type_node)) == INTEGER_TYPE
3937 && TYPE_MODULAR_P (TREE_TYPE (type_node)))
3938 {
3939 type = copy_type (type);
3940 TREE_TYPE (type) = TREE_TYPE (type_node);
3941 }
3942
3943 return type;
3944 }
3945
3946 /* Return 1 if the types T1 and T2 are compatible, i.e. if they can be
3947 transparently converted to each other. */
3948
3949 int
3950 gnat_types_compatible_p (tree t1, tree t2)
3951 {
3952 enum tree_code code;
3953
3954 /* This is the default criterion. */
3955 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
3956 return 1;
3957
3958 /* We only check structural equivalence here. */
3959 if ((code = TREE_CODE (t1)) != TREE_CODE (t2))
3960 return 0;
3961
3962 /* Vector types are also compatible if they have the same number of subparts
3963 and the same form of (scalar) element type. */
3964 if (code == VECTOR_TYPE
3965 && known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
3966 && TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2))
3967 && TYPE_PRECISION (TREE_TYPE (t1)) == TYPE_PRECISION (TREE_TYPE (t2)))
3968 return 1;
3969
3970 /* Array types are also compatible if they are constrained and have the same
3971 domain(s), the same component type and the same scalar storage order. */
3972 if (code == ARRAY_TYPE
3973 && (TYPE_DOMAIN (t1) == TYPE_DOMAIN (t2)
3974 || (TYPE_DOMAIN (t1)
3975 && TYPE_DOMAIN (t2)
3976 && tree_int_cst_equal (TYPE_MIN_VALUE (TYPE_DOMAIN (t1)),
3977 TYPE_MIN_VALUE (TYPE_DOMAIN (t2)))
3978 && tree_int_cst_equal (TYPE_MAX_VALUE (TYPE_DOMAIN (t1)),
3979 TYPE_MAX_VALUE (TYPE_DOMAIN (t2)))))
3980 && (TREE_TYPE (t1) == TREE_TYPE (t2)
3981 || (TREE_CODE (TREE_TYPE (t1)) == ARRAY_TYPE
3982 && gnat_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))))
3983 && TYPE_REVERSE_STORAGE_ORDER (t1) == TYPE_REVERSE_STORAGE_ORDER (t2))
3984 return 1;
3985
3986 return 0;
3987 }
3988
3989 /* Return true if EXPR is a useless type conversion. */
3990
3991 bool
3992 gnat_useless_type_conversion (tree expr)
3993 {
3994 if (CONVERT_EXPR_P (expr)
3995 || TREE_CODE (expr) == VIEW_CONVERT_EXPR
3996 || TREE_CODE (expr) == NON_LVALUE_EXPR)
3997 return gnat_types_compatible_p (TREE_TYPE (expr),
3998 TREE_TYPE (TREE_OPERAND (expr, 0)));
3999
4000 return false;
4001 }
4002
4003 /* Return true if T, a {FUNCTION,METHOD}_TYPE, has the specified flags. */
4004
4005 bool
4006 fntype_same_flags_p (const_tree t, tree cico_list, bool return_by_direct_ref_p,
4007 bool return_by_invisi_ref_p)
4008 {
4009 return TYPE_CI_CO_LIST (t) == cico_list
4010 && TYPE_RETURN_BY_DIRECT_REF_P (t) == return_by_direct_ref_p
4011 && TREE_ADDRESSABLE (t) == return_by_invisi_ref_p;
4012 }
4013
4014 /* Try to compute the maximum (if MAX_P) or minimum (if !MAX_P) value for the
4015 expression EXP, for very simple expressions. Substitute variable references
4016 with their respective type's min/max values. Return the computed value if
4017 any, or EXP if no value can be computed. */
4018
4019 tree
4020 max_value (tree exp, bool max_p)
4021 {
4022 enum tree_code code = TREE_CODE (exp);
4023 tree type = TREE_TYPE (exp);
4024 tree op0, op1, op2;
4025
4026 switch (TREE_CODE_CLASS (code))
4027 {
4028 case tcc_declaration:
4029 if (VAR_P (exp))
4030 return fold_convert (type,
4031 max_p
4032 ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type));
4033 break;
4034
4035 case tcc_vl_exp:
4036 if (code == CALL_EXPR)
4037 {
4038 tree t;
4039
4040 t = maybe_inline_call_in_expr (exp);
4041 if (t)
4042 return max_value (t, max_p);
4043 }
4044 break;
4045
4046 case tcc_comparison:
4047 return build_int_cst (type, max_p ? 1 : 0);
4048
4049 case tcc_unary:
4050 op0 = TREE_OPERAND (exp, 0);
4051
4052 if (code == NON_LVALUE_EXPR)
4053 return max_value (op0, max_p);
4054
4055 if (code == NEGATE_EXPR)
4056 return max_value (op0, !max_p);
4057
4058 if (code == NOP_EXPR)
4059 return fold_convert (type, max_value (op0, max_p));
4060
4061 break;
4062
4063 case tcc_binary:
4064 op0 = TREE_OPERAND (exp, 0);
4065 op1 = TREE_OPERAND (exp, 1);
4066
4067 switch (code) {
4068 case PLUS_EXPR:
4069 case MULT_EXPR:
4070 return fold_build2 (code, type, max_value(op0, max_p),
4071 max_value (op1, max_p));
4072 case MINUS_EXPR:
4073 case TRUNC_DIV_EXPR:
4074 return fold_build2 (code, type, max_value(op0, max_p),
4075 max_value (op1, !max_p));
4076 default:
4077 break;
4078 }
4079 break;
4080
4081 case tcc_expression:
4082 if (code == COND_EXPR)
4083 {
4084 op0 = TREE_OPERAND (exp, 0);
4085 op1 = TREE_OPERAND (exp, 1);
4086 op2 = TREE_OPERAND (exp, 2);
4087
4088 if (!op1 || !op2)
4089 break;
4090
4091 op1 = max_value (op1, max_p);
4092 op2 = max_value (op2, max_p);
4093
4094 if (op1 == TREE_OPERAND (exp, 1) && op2 == TREE_OPERAND (exp, 2))
4095 break;
4096
4097 return fold_build2 (max_p ? MAX_EXPR : MIN_EXPR, type, op1, op2);
4098 }
4099 break;
4100
4101 default:
4102 break;
4103 }
4104 return exp;
4105 }
4106
4107
4108 /* EXP is an expression for the size of an object. If this size contains
4109 discriminant references, replace them with the maximum (if MAX_P) or
4110 minimum (if !MAX_P) possible value of the discriminant.
4111
4112 Note that the expression may have already been gimplified,in which case
4113 COND_EXPRs have VOID_TYPE and no operands, and this must be handled. */
4114
4115 tree
4116 max_size (tree exp, bool max_p)
4117 {
4118 enum tree_code code = TREE_CODE (exp);
4119 tree type = TREE_TYPE (exp);
4120 tree op0, op1, op2;
4121
4122 switch (TREE_CODE_CLASS (code))
4123 {
4124 case tcc_declaration:
4125 case tcc_constant:
4126 return exp;
4127
4128 case tcc_exceptional:
4129 gcc_assert (code == SSA_NAME);
4130 return exp;
4131
4132 case tcc_vl_exp:
4133 if (code == CALL_EXPR)
4134 {
4135 tree t, *argarray;
4136 int n, i;
4137
4138 t = maybe_inline_call_in_expr (exp);
4139 if (t)
4140 return max_size (t, max_p);
4141
4142 n = call_expr_nargs (exp);
4143 gcc_assert (n > 0);
4144 argarray = XALLOCAVEC (tree, n);
4145 /* This is used to remove possible placeholder in call args. */
4146 for (i = 0; i < n; i++)
4147 argarray[i] = max_size (CALL_EXPR_ARG (exp, i), max_p);
4148 return build_call_array (type, CALL_EXPR_FN (exp), n, argarray);
4149 }
4150 break;
4151
4152 case tcc_reference:
4153 /* If this contains a PLACEHOLDER_EXPR, it is the thing we want to
4154 modify. Otherwise, we treat it like a variable. */
4155 if (CONTAINS_PLACEHOLDER_P (exp))
4156 {
4157 tree base_type = get_base_type (TREE_TYPE (TREE_OPERAND (exp, 1)));
4158 tree val
4159 = fold_convert (base_type,
4160 max_p
4161 ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type));
4162
4163 /* Walk down the extra subtypes to get more restrictive bounds. */
4164 while (TYPE_IS_EXTRA_SUBTYPE_P (type))
4165 {
4166 type = TREE_TYPE (type);
4167 if (max_p)
4168 val = fold_build2 (MIN_EXPR, base_type, val,
4169 fold_convert (base_type,
4170 TYPE_MAX_VALUE (type)));
4171 else
4172 val = fold_build2 (MAX_EXPR, base_type, val,
4173 fold_convert (base_type,
4174 TYPE_MIN_VALUE (type)));
4175 }
4176
4177 return fold_convert (type, max_size (val, max_p));
4178 }
4179
4180 return exp;
4181
4182 case tcc_comparison:
4183 return build_int_cst (type, max_p ? 1 : 0);
4184
4185 case tcc_unary:
4186 op0 = TREE_OPERAND (exp, 0);
4187
4188 if (code == NON_LVALUE_EXPR)
4189 return max_size (op0, max_p);
4190
4191 if (VOID_TYPE_P (TREE_TYPE (op0)))
4192 return max_p ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type);
4193
4194 op0 = max_size (op0, code == NEGATE_EXPR ? !max_p : max_p);
4195
4196 if (op0 == TREE_OPERAND (exp, 0))
4197 return exp;
4198
4199 return fold_build1 (code, type, op0);
4200
4201 case tcc_binary:
4202 op0 = TREE_OPERAND (exp, 0);
4203 op1 = TREE_OPERAND (exp, 1);
4204
4205 /* If we have a multiply-add with a "negative" value in an unsigned
4206 type, do a multiply-subtract with the negated value, in order to
4207 avoid creating a spurious overflow below. */
4208 if (code == PLUS_EXPR
4209 && TREE_CODE (op0) == MULT_EXPR
4210 && TYPE_UNSIGNED (type)
4211 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST
4212 && !TREE_OVERFLOW (TREE_OPERAND (op0, 1))
4213 && tree_int_cst_sign_bit (TREE_OPERAND (op0, 1)))
4214 {
4215 tree tmp = op1;
4216 op1 = build2 (MULT_EXPR, type, TREE_OPERAND (op0, 0),
4217 fold_build1 (NEGATE_EXPR, type,
4218 TREE_OPERAND (op0, 1)));
4219 op0 = tmp;
4220 code = MINUS_EXPR;
4221 }
4222
4223 op0 = max_size (op0, max_p);
4224 op1 = max_size (op1, code == MINUS_EXPR ? !max_p : max_p);
4225
4226 if ((code == MINUS_EXPR || code == PLUS_EXPR))
4227 {
4228 /* If the op0 has overflowed and the op1 is a variable,
4229 propagate the overflow by returning the op0. */
4230 if (TREE_CODE (op0) == INTEGER_CST
4231 && TREE_OVERFLOW (op0)
4232 && TREE_CODE (op1) != INTEGER_CST)
4233 return op0;
4234
4235 /* If we have a "negative" value in an unsigned type, do the
4236 opposite operation on the negated value, in order to avoid
4237 creating a spurious overflow below. */
4238 if (TYPE_UNSIGNED (type)
4239 && TREE_CODE (op1) == INTEGER_CST
4240 && !TREE_OVERFLOW (op1)
4241 && tree_int_cst_sign_bit (op1))
4242 {
4243 op1 = fold_build1 (NEGATE_EXPR, type, op1);
4244 code = (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR);
4245 }
4246 }
4247
4248 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
4249 return exp;
4250
4251 /* We need to detect overflows so we call size_binop here. */
4252 return size_binop (code, op0, op1);
4253
4254 case tcc_expression:
4255 switch (TREE_CODE_LENGTH (code))
4256 {
4257 case 1:
4258 if (code == SAVE_EXPR)
4259 return exp;
4260
4261 op0 = max_size (TREE_OPERAND (exp, 0),
4262 code == TRUTH_NOT_EXPR ? !max_p : max_p);
4263
4264 if (op0 == TREE_OPERAND (exp, 0))
4265 return exp;
4266
4267 return fold_build1 (code, type, op0);
4268
4269 case 2:
4270 if (code == COMPOUND_EXPR)
4271 return max_size (TREE_OPERAND (exp, 1), max_p);
4272
4273 op0 = max_size (TREE_OPERAND (exp, 0), max_p);
4274 op1 = max_size (TREE_OPERAND (exp, 1), max_p);
4275
4276 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
4277 return exp;
4278
4279 return fold_build2 (code, type, op0, op1);
4280
4281 case 3:
4282 if (code == COND_EXPR)
4283 {
4284 op0 = TREE_OPERAND (exp, 0);
4285 op1 = TREE_OPERAND (exp, 1);
4286 op2 = TREE_OPERAND (exp, 2);
4287
4288 if (!op1 || !op2)
4289 return exp;
4290
4291 op1 = max_size (op1, max_p);
4292 op2 = max_size (op2, max_p);
4293
4294 /* If we have the MAX of a "negative" value in an unsigned type
4295 and zero for a length expression, just return zero. */
4296 if (max_p
4297 && TREE_CODE (op0) == LE_EXPR
4298 && TYPE_UNSIGNED (type)
4299 && TREE_CODE (op1) == INTEGER_CST
4300 && !TREE_OVERFLOW (op1)
4301 && tree_int_cst_sign_bit (op1)
4302 && integer_zerop (op2))
4303 return op2;
4304
4305 return fold_build2 (max_p ? MAX_EXPR : MIN_EXPR, type, op1, op2);
4306 }
4307 break;
4308
4309 default:
4310 break;
4311 }
4312
4313 /* Other tree classes cannot happen. */
4314 default:
4315 break;
4316 }
4317
4318 gcc_unreachable ();
4319 }
4320
4321 /* Build a template of type TEMPLATE_TYPE from the array bounds of ARRAY_TYPE.
4322 EXPR is an expression that we can use to locate any PLACEHOLDER_EXPRs.
4323 Return a constructor for the template. */
4324
4325 tree
4326 build_template (tree template_type, tree array_type, tree expr)
4327 {
4328 vec<constructor_elt, va_gc> *template_elts = NULL;
4329 tree bound_list = NULL_TREE;
4330 tree field;
4331
4332 while (TREE_CODE (array_type) == RECORD_TYPE
4333 && (TYPE_PADDING_P (array_type)
4334 || TYPE_JUSTIFIED_MODULAR_P (array_type)))
4335 array_type = TREE_TYPE (TYPE_FIELDS (array_type));
4336
4337 if (TREE_CODE (array_type) == ARRAY_TYPE
4338 || (TREE_CODE (array_type) == INTEGER_TYPE
4339 && TYPE_HAS_ACTUAL_BOUNDS_P (array_type)))
4340 bound_list = TYPE_ACTUAL_BOUNDS (array_type);
4341
4342 /* First make the list for a CONSTRUCTOR for the template. Go down
4343 the field list of the template instead of the type chain because
4344 this array might be an Ada array of array and we can't tell where
4345 the nested array stop being the underlying object. */
4346 for (field = TYPE_FIELDS (template_type);
4347 field;
4348 field = DECL_CHAIN (DECL_CHAIN (field)))
4349 {
4350 tree bounds, min, max;
4351
4352 /* If we have a bound list, get the bounds from there. Likewise
4353 for an ARRAY_TYPE. Otherwise, if expr is a PARM_DECL with
4354 DECL_BY_COMPONENT_PTR_P, use the bounds of the field in the
4355 template, but this will only give us a maximum range. */
4356 if (bound_list)
4357 {
4358 bounds = TREE_VALUE (bound_list);
4359 bound_list = TREE_CHAIN (bound_list);
4360 }
4361 else if (TREE_CODE (array_type) == ARRAY_TYPE)
4362 {
4363 bounds = TYPE_INDEX_TYPE (TYPE_DOMAIN (array_type));
4364 array_type = TREE_TYPE (array_type);
4365 }
4366 else if (expr && TREE_CODE (expr) == PARM_DECL
4367 && DECL_BY_COMPONENT_PTR_P (expr))
4368 bounds = TREE_TYPE (field);
4369 else
4370 gcc_unreachable ();
4371
4372 min = convert (TREE_TYPE (field), TYPE_MIN_VALUE (bounds));
4373 max = convert (TREE_TYPE (DECL_CHAIN (field)), TYPE_MAX_VALUE (bounds));
4374
4375 /* If either MIN or MAX involve a PLACEHOLDER_EXPR, we must
4376 substitute it from OBJECT. */
4377 min = SUBSTITUTE_PLACEHOLDER_IN_EXPR (min, expr);
4378 max = SUBSTITUTE_PLACEHOLDER_IN_EXPR (max, expr);
4379
4380 CONSTRUCTOR_APPEND_ELT (template_elts, field, min);
4381 CONSTRUCTOR_APPEND_ELT (template_elts, DECL_CHAIN (field), max);
4382 }
4383
4384 return gnat_build_constructor (template_type, template_elts);
4385 }
4386
4387 /* Return true if TYPE is suitable for the element type of a vector. */
4388
4389 static bool
4390 type_for_vector_element_p (tree type)
4391 {
4392 machine_mode mode;
4393
4394 if (!INTEGRAL_TYPE_P (type)
4395 && !SCALAR_FLOAT_TYPE_P (type)
4396 && !FIXED_POINT_TYPE_P (type))
4397 return false;
4398
4399 mode = TYPE_MODE (type);
4400 if (GET_MODE_CLASS (mode) != MODE_INT
4401 && !SCALAR_FLOAT_MODE_P (mode)
4402 && !ALL_SCALAR_FIXED_POINT_MODE_P (mode))
4403 return false;
4404
4405 return true;
4406 }
4407
4408 /* Return a vector type given the SIZE and the INNER_TYPE, or NULL_TREE if
4409 this is not possible. If ATTRIBUTE is non-zero, we are processing the
4410 attribute declaration and want to issue error messages on failure. */
4411
4412 static tree
4413 build_vector_type_for_size (tree inner_type, tree size, tree attribute)
4414 {
4415 unsigned HOST_WIDE_INT size_int, inner_size_int;
4416 int nunits;
4417
4418 /* Silently punt on variable sizes. We can't make vector types for them,
4419 need to ignore them on front-end generated subtypes of unconstrained
4420 base types, and this attribute is for binding implementors, not end
4421 users, so we should never get there from legitimate explicit uses. */
4422 if (!tree_fits_uhwi_p (size))
4423 return NULL_TREE;
4424 size_int = tree_to_uhwi (size);
4425
4426 if (!type_for_vector_element_p (inner_type))
4427 {
4428 if (attribute)
4429 error ("invalid element type for attribute %qs",
4430 IDENTIFIER_POINTER (attribute));
4431 return NULL_TREE;
4432 }
4433 inner_size_int = tree_to_uhwi (TYPE_SIZE_UNIT (inner_type));
4434
4435 if (size_int % inner_size_int)
4436 {
4437 if (attribute)
4438 error ("vector size not an integral multiple of component size");
4439 return NULL_TREE;
4440 }
4441
4442 if (size_int == 0)
4443 {
4444 if (attribute)
4445 error ("zero vector size");
4446 return NULL_TREE;
4447 }
4448
4449 nunits = size_int / inner_size_int;
4450 if (nunits & (nunits - 1))
4451 {
4452 if (attribute)
4453 error ("number of components of vector not a power of two");
4454 return NULL_TREE;
4455 }
4456
4457 return build_vector_type (inner_type, nunits);
4458 }
4459
4460 /* Return a vector type whose representative array type is ARRAY_TYPE, or
4461 NULL_TREE if this is not possible. If ATTRIBUTE is non-zero, we are
4462 processing the attribute and want to issue error messages on failure. */
4463
4464 static tree
4465 build_vector_type_for_array (tree array_type, tree attribute)
4466 {
4467 tree vector_type = build_vector_type_for_size (TREE_TYPE (array_type),
4468 TYPE_SIZE_UNIT (array_type),
4469 attribute);
4470 if (!vector_type)
4471 return NULL_TREE;
4472
4473 TYPE_REPRESENTATIVE_ARRAY (vector_type) = array_type;
4474 return vector_type;
4475 }
4476
4477 /* Build a type to be used to represent an aliased object whose nominal type
4478 is an unconstrained array. This consists of a RECORD_TYPE containing a
4479 field of TEMPLATE_TYPE and a field of OBJECT_TYPE, which is an ARRAY_TYPE.
4480 If ARRAY_TYPE is that of an unconstrained array, this is used to represent
4481 an arbitrary unconstrained object. Use NAME as the name of the
4482 record. ARTIFICIAL_P is true if the type was generated by the
4483 compiler, or false if the type came from source. DEBUG_INFO_P is
4484 true if we need to write debug information for the type. */
4485
4486 tree
4487 build_unc_object_type (tree template_type, tree object_type, tree name,
4488 bool artificial_p, bool debug_info_p)
4489 {
4490 tree type = make_node (RECORD_TYPE);
4491 tree template_field
4492 = create_field_decl (get_identifier ("BOUNDS"), template_type, type,
4493 NULL_TREE, NULL_TREE, 0, 1);
4494 tree array_field
4495 = create_field_decl (get_identifier ("ARRAY"), object_type, type,
4496 NULL_TREE, NULL_TREE, 0, 1);
4497
4498 TYPE_NAME (type) = name;
4499 TYPE_CONTAINS_TEMPLATE_P (type) = 1;
4500 DECL_CHAIN (template_field) = array_field;
4501 finish_record_type (type, template_field, 0, true);
4502
4503 /* Declare it now since it will never be declared otherwise. This is
4504 necessary to ensure that its subtrees are properly marked. */
4505 create_type_decl (name, type, artificial_p, debug_info_p, Empty);
4506
4507 return type;
4508 }
4509
4510 /* Same, taking a thin or fat pointer type instead of a template type. */
4511
4512 tree
4513 build_unc_object_type_from_ptr (tree thin_fat_ptr_type, tree object_type,
4514 tree name, bool debug_info_p)
4515 {
4516 tree template_type;
4517
4518 gcc_assert (TYPE_IS_FAT_OR_THIN_POINTER_P (thin_fat_ptr_type));
4519
4520 template_type
4521 = (TYPE_IS_FAT_POINTER_P (thin_fat_ptr_type)
4522 ? TREE_TYPE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (thin_fat_ptr_type))))
4523 : TREE_TYPE (TYPE_FIELDS (TREE_TYPE (thin_fat_ptr_type))));
4524
4525 return
4526 build_unc_object_type (template_type, object_type, name, true,
4527 debug_info_p);
4528 }
4529
4530 /* Update anything previously pointing to OLD_TYPE to point to NEW_TYPE.
4531 In the normal case this is just two adjustments, but we have more to
4532 do if NEW_TYPE is an UNCONSTRAINED_ARRAY_TYPE. */
4533
4534 void
4535 update_pointer_to (tree old_type, tree new_type)
4536 {
4537 tree ptr = TYPE_POINTER_TO (old_type);
4538 tree ref = TYPE_REFERENCE_TO (old_type);
4539 tree t;
4540
4541 /* If this is the main variant, process all the other variants first. */
4542 if (TYPE_MAIN_VARIANT (old_type) == old_type)
4543 for (t = TYPE_NEXT_VARIANT (old_type); t; t = TYPE_NEXT_VARIANT (t))
4544 update_pointer_to (t, new_type);
4545
4546 /* If no pointers and no references, we are done. */
4547 if (!ptr && !ref)
4548 return;
4549
4550 /* Merge the old type qualifiers in the new type.
4551
4552 Each old variant has qualifiers for specific reasons, and the new
4553 designated type as well. Each set of qualifiers represents useful
4554 information grabbed at some point, and merging the two simply unifies
4555 these inputs into the final type description.
4556
4557 Consider for instance a volatile type frozen after an access to constant
4558 type designating it; after the designated type's freeze, we get here with
4559 a volatile NEW_TYPE and a dummy OLD_TYPE with a readonly variant, created
4560 when the access type was processed. We will make a volatile and readonly
4561 designated type, because that's what it really is.
4562
4563 We might also get here for a non-dummy OLD_TYPE variant with different
4564 qualifiers than those of NEW_TYPE, for instance in some cases of pointers
4565 to private record type elaboration (see the comments around the call to
4566 this routine in gnat_to_gnu_entity <E_Access_Type>). We have to merge
4567 the qualifiers in those cases too, to avoid accidentally discarding the
4568 initial set, and will often end up with OLD_TYPE == NEW_TYPE then. */
4569 new_type
4570 = build_qualified_type (new_type,
4571 TYPE_QUALS (old_type) | TYPE_QUALS (new_type));
4572
4573 /* If old type and new type are identical, there is nothing to do. */
4574 if (old_type == new_type)
4575 return;
4576
4577 /* Otherwise, first handle the simple case. */
4578 if (TREE_CODE (new_type) != UNCONSTRAINED_ARRAY_TYPE)
4579 {
4580 tree new_ptr, new_ref;
4581
4582 /* If pointer or reference already points to new type, nothing to do.
4583 This can happen as update_pointer_to can be invoked multiple times
4584 on the same couple of types because of the type variants. */
4585 if ((ptr && TREE_TYPE (ptr) == new_type)
4586 || (ref && TREE_TYPE (ref) == new_type))
4587 return;
4588
4589 /* Chain PTR and its variants at the end. */
4590 new_ptr = TYPE_POINTER_TO (new_type);
4591 if (new_ptr)
4592 {
4593 while (TYPE_NEXT_PTR_TO (new_ptr))
4594 new_ptr = TYPE_NEXT_PTR_TO (new_ptr);
4595 TYPE_NEXT_PTR_TO (new_ptr) = ptr;
4596 }
4597 else if (ptr)
4598 {
4599 TYPE_POINTER_TO (new_type) = ptr;
4600
4601 /* If there is no pointer pointing to NEW_TYPE yet, re-compute the
4602 TYPE_CANONICAL of the old pointer but pointing to NEW_TYPE, like
4603 build_pointer_type would have done for such a pointer, because we
4604 will propagate it in the adjustment loop below. But make sure to
4605 preserve an alias set already present on the old pointer. */
4606 if (TYPE_STRUCTURAL_EQUALITY_P (new_type))
4607 SET_TYPE_STRUCTURAL_EQUALITY (ptr);
4608 else if (TYPE_CANONICAL (new_type) != new_type
4609 || (TYPE_REF_CAN_ALIAS_ALL (ptr)
4610 && !lookup_attribute ("may_alias",
4611 TYPE_ATTRIBUTES (new_type))))
4612 {
4613 const alias_set_type set
4614 = TYPE_STRUCTURAL_EQUALITY_P (ptr)
4615 ? TYPE_ALIAS_SET (ptr)
4616 : TYPE_ALIAS_SET (TYPE_CANONICAL (ptr));
4617 TYPE_CANONICAL (ptr)
4618 = build_pointer_type (TYPE_CANONICAL (new_type));
4619 TYPE_ALIAS_SET (TYPE_CANONICAL (ptr)) = set;
4620 }
4621 }
4622
4623 /* Now adjust them. */
4624 for (; ptr; ptr = TYPE_NEXT_PTR_TO (ptr))
4625 for (t = TYPE_MAIN_VARIANT (ptr); t; t = TYPE_NEXT_VARIANT (t))
4626 {
4627 TREE_TYPE (t) = new_type;
4628 if (TYPE_NULL_BOUNDS (t))
4629 TREE_TYPE (TREE_OPERAND (TYPE_NULL_BOUNDS (t), 0)) = new_type;
4630 TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_POINTER_TO (new_type));
4631 }
4632
4633 /* Chain REF and its variants at the end. */
4634 new_ref = TYPE_REFERENCE_TO (new_type);
4635 if (new_ref)
4636 {
4637 while (TYPE_NEXT_REF_TO (new_ref))
4638 new_ref = TYPE_NEXT_REF_TO (new_ref);
4639 TYPE_NEXT_REF_TO (new_ref) = ref;
4640 }
4641 else if (ref)
4642 {
4643 TYPE_REFERENCE_TO (new_type) = ref;
4644
4645 /* If there is no reference pointing to NEW_TYPE yet, re-compute the
4646 TYPE_CANONICAL of the old reference but pointing to NEW_TYPE, like
4647 build_reference_type would have done for such a reference, because
4648 we will propagate it in the adjustment loop below. But make sure
4649 to preserve an alias set already present on the old reference. */
4650 if (TYPE_STRUCTURAL_EQUALITY_P (new_type))
4651 SET_TYPE_STRUCTURAL_EQUALITY (ref);
4652 else if (TYPE_CANONICAL (new_type) != new_type
4653 || (TYPE_REF_CAN_ALIAS_ALL (ref)
4654 && !lookup_attribute ("may_alias",
4655 TYPE_ATTRIBUTES (new_type))))
4656 {
4657 const alias_set_type set
4658 = TYPE_STRUCTURAL_EQUALITY_P (ref)
4659 ? TYPE_ALIAS_SET (ref)
4660 : TYPE_ALIAS_SET (TYPE_CANONICAL (ref));
4661 TYPE_CANONICAL (ref)
4662 = build_reference_type (TYPE_CANONICAL (new_type));
4663 TYPE_ALIAS_SET (TYPE_CANONICAL (ref)) = set;
4664 }
4665 }
4666
4667 /* Now adjust them. */
4668 for (; ref; ref = TYPE_NEXT_REF_TO (ref))
4669 for (t = TYPE_MAIN_VARIANT (ref); t; t = TYPE_NEXT_VARIANT (t))
4670 {
4671 TREE_TYPE (t) = new_type;
4672 TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_REFERENCE_TO (new_type));
4673 }
4674
4675 TYPE_POINTER_TO (old_type) = NULL_TREE;
4676 TYPE_REFERENCE_TO (old_type) = NULL_TREE;
4677 }
4678
4679 /* Now deal with the unconstrained array case. In this case the pointer
4680 is actually a record where both fields are pointers to dummy nodes.
4681 Turn them into pointers to the correct types using update_pointer_to.
4682 Likewise for the pointer to the object record (thin pointer). */
4683 else
4684 {
4685 tree new_ptr = TYPE_POINTER_TO (new_type);
4686
4687 gcc_assert (TYPE_IS_FAT_POINTER_P (ptr));
4688
4689 /* If PTR already points to NEW_TYPE, nothing to do. This can happen
4690 since update_pointer_to can be invoked multiple times on the same
4691 couple of types because of the type variants. */
4692 if (TYPE_UNCONSTRAINED_ARRAY (ptr) == new_type)
4693 return;
4694
4695 update_pointer_to
4696 (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (ptr))),
4697 TREE_TYPE (TREE_TYPE (TYPE_FIELDS (new_ptr))));
4698
4699 update_pointer_to
4700 (TREE_TYPE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (ptr)))),
4701 TREE_TYPE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (new_ptr)))));
4702
4703 update_pointer_to (TYPE_OBJECT_RECORD_TYPE (old_type),
4704 TYPE_OBJECT_RECORD_TYPE (new_type));
4705
4706 TYPE_POINTER_TO (old_type) = NULL_TREE;
4707 TYPE_REFERENCE_TO (old_type) = NULL_TREE;
4708 }
4709 }
4710
4711 /* Convert EXPR, a pointer to a constrained array, into a pointer to an
4712 unconstrained one. This involves making or finding a template. */
4713
4714 static tree
4715 convert_to_fat_pointer (tree type, tree expr)
4716 {
4717 tree template_type = TREE_TYPE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type))));
4718 tree p_array_type = TREE_TYPE (TYPE_FIELDS (type));
4719 tree etype = TREE_TYPE (expr);
4720 tree template_addr;
4721 vec<constructor_elt, va_gc> *v;
4722 vec_alloc (v, 2);
4723
4724 /* If EXPR is null, make a fat pointer that contains a null pointer to the
4725 array (compare_fat_pointers ensures that this is the full discriminant)
4726 and a valid pointer to the bounds. This latter property is necessary
4727 since the compiler can hoist the load of the bounds done through it. */
4728 if (integer_zerop (expr))
4729 {
4730 tree ptr_template_type = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type)));
4731 tree null_bounds, t;
4732
4733 if (TYPE_NULL_BOUNDS (ptr_template_type))
4734 null_bounds = TYPE_NULL_BOUNDS (ptr_template_type);
4735 else
4736 {
4737 /* The template type can still be dummy at this point so we build an
4738 empty constructor. The middle-end will fill it in with zeros. */
4739 t = build_constructor (template_type, NULL);
4740 TREE_CONSTANT (t) = TREE_STATIC (t) = 1;
4741 null_bounds = build_unary_op (ADDR_EXPR, NULL_TREE, t);
4742 SET_TYPE_NULL_BOUNDS (ptr_template_type, null_bounds);
4743 }
4744
4745 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type),
4746 fold_convert (p_array_type, null_pointer_node));
4747 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (TYPE_FIELDS (type)), null_bounds);
4748 t = build_constructor (type, v);
4749 /* Do not set TREE_CONSTANT so as to force T to static memory. */
4750 TREE_CONSTANT (t) = 0;
4751 TREE_STATIC (t) = 1;
4752
4753 return t;
4754 }
4755
4756 /* If EXPR is a thin pointer, make template and data from the record. */
4757 if (TYPE_IS_THIN_POINTER_P (etype))
4758 {
4759 tree field = TYPE_FIELDS (TREE_TYPE (etype));
4760
4761 expr = gnat_protect_expr (expr);
4762
4763 /* If we have a TYPE_UNCONSTRAINED_ARRAY attached to the RECORD_TYPE,
4764 the thin pointer value has been shifted so we shift it back to get
4765 the template address. */
4766 if (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (etype)))
4767 {
4768 template_addr
4769 = build_binary_op (POINTER_PLUS_EXPR, etype, expr,
4770 fold_build1 (NEGATE_EXPR, sizetype,
4771 byte_position
4772 (DECL_CHAIN (field))));
4773 template_addr
4774 = fold_convert (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type))),
4775 template_addr);
4776 }
4777
4778 /* Otherwise we explicitly take the address of the fields. */
4779 else
4780 {
4781 expr = build_unary_op (INDIRECT_REF, NULL_TREE, expr);
4782 template_addr
4783 = build_unary_op (ADDR_EXPR, NULL_TREE,
4784 build_component_ref (expr, field, false));
4785 expr = build_unary_op (ADDR_EXPR, NULL_TREE,
4786 build_component_ref (expr, DECL_CHAIN (field),
4787 false));
4788 }
4789 }
4790
4791 /* Otherwise, build the constructor for the template. */
4792 else
4793 template_addr
4794 = build_unary_op (ADDR_EXPR, NULL_TREE,
4795 build_template (template_type, TREE_TYPE (etype),
4796 expr));
4797
4798 /* The final result is a constructor for the fat pointer.
4799
4800 If EXPR is an argument of a foreign convention subprogram, the type it
4801 points to is directly the component type. In this case, the expression
4802 type may not match the corresponding FIELD_DECL type at this point, so we
4803 call "convert" here to fix that up if necessary. This type consistency is
4804 required, for instance because it ensures that possible later folding of
4805 COMPONENT_REFs against this constructor always yields something of the
4806 same type as the initial reference.
4807
4808 Note that the call to "build_template" above is still fine because it
4809 will only refer to the provided TEMPLATE_TYPE in this case. */
4810 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type), convert (p_array_type, expr));
4811 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (TYPE_FIELDS (type)), template_addr);
4812 return gnat_build_constructor (type, v);
4813 }
4814
4815 /* Create an expression whose value is that of EXPR,
4816 converted to type TYPE. The TREE_TYPE of the value
4817 is always TYPE. This function implements all reasonable
4818 conversions; callers should filter out those that are
4819 not permitted by the language being compiled. */
4820
4821 tree
4822 convert (tree type, tree expr)
4823 {
4824 tree etype = TREE_TYPE (expr);
4825 enum tree_code ecode = TREE_CODE (etype);
4826 enum tree_code code = TREE_CODE (type);
4827
4828 /* If the expression is already of the right type, we are done. */
4829 if (etype == type)
4830 return expr;
4831
4832 /* If both input and output have padding and are of variable size, do this
4833 as an unchecked conversion. Likewise if one is a mere variant of the
4834 other, so we avoid a pointless unpad/repad sequence. */
4835 else if (code == RECORD_TYPE && ecode == RECORD_TYPE
4836 && TYPE_PADDING_P (type) && TYPE_PADDING_P (etype)
4837 && (!TREE_CONSTANT (TYPE_SIZE (type))
4838 || !TREE_CONSTANT (TYPE_SIZE (etype))
4839 || TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (etype)
4840 || TYPE_NAME (TREE_TYPE (TYPE_FIELDS (type)))
4841 == TYPE_NAME (TREE_TYPE (TYPE_FIELDS (etype)))))
4842 ;
4843
4844 /* If the output type has padding, convert to the inner type and make a
4845 constructor to build the record, unless a variable size is involved. */
4846 else if (code == RECORD_TYPE && TYPE_PADDING_P (type))
4847 {
4848 /* If we previously converted from another type and our type is
4849 of variable size, remove the conversion to avoid the need for
4850 variable-sized temporaries. Likewise for a conversion between
4851 original and packable version. */
4852 if (TREE_CODE (expr) == VIEW_CONVERT_EXPR
4853 && (!TREE_CONSTANT (TYPE_SIZE (type))
4854 || (ecode == RECORD_TYPE
4855 && TYPE_NAME (etype)
4856 == TYPE_NAME (TREE_TYPE (TREE_OPERAND (expr, 0))))))
4857 expr = TREE_OPERAND (expr, 0);
4858
4859 /* If we are just removing the padding from expr, convert the original
4860 object if we have variable size in order to avoid the need for some
4861 variable-sized temporaries. Likewise if the padding is a variant
4862 of the other, so we avoid a pointless unpad/repad sequence. */
4863 if (TREE_CODE (expr) == COMPONENT_REF
4864 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (expr, 0)))
4865 && (!TREE_CONSTANT (TYPE_SIZE (type))
4866 || TYPE_MAIN_VARIANT (type)
4867 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (expr, 0)))
4868 || (ecode == RECORD_TYPE
4869 && TYPE_NAME (etype)
4870 == TYPE_NAME (TREE_TYPE (TYPE_FIELDS (type))))))
4871 return convert (type, TREE_OPERAND (expr, 0));
4872
4873 /* If the inner type is of self-referential size and the expression type
4874 is a record, do this as an unchecked conversion unless both types are
4875 essentially the same. */
4876 if (ecode == RECORD_TYPE
4877 && CONTAINS_PLACEHOLDER_P (DECL_SIZE (TYPE_FIELDS (type)))
4878 && TYPE_MAIN_VARIANT (etype)
4879 != TYPE_MAIN_VARIANT (TREE_TYPE (TYPE_FIELDS (type))))
4880 return unchecked_convert (type, expr, false);
4881
4882 /* If we are converting between array types with variable size, do the
4883 final conversion as an unchecked conversion, again to avoid the need
4884 for some variable-sized temporaries. If valid, this conversion is
4885 very likely purely technical and without real effects. */
4886 if (ecode == ARRAY_TYPE
4887 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (type))) == ARRAY_TYPE
4888 && !TREE_CONSTANT (TYPE_SIZE (etype))
4889 && !TREE_CONSTANT (TYPE_SIZE (type)))
4890 return unchecked_convert (type,
4891 convert (TREE_TYPE (TYPE_FIELDS (type)),
4892 expr),
4893 false);
4894
4895 tree t = convert (TREE_TYPE (TYPE_FIELDS (type)), expr);
4896
4897 /* If converting to the inner type has already created a CONSTRUCTOR with
4898 the right size, then reuse it instead of creating another one. This
4899 can happen for the padding type built to overalign local variables. */
4900 if (TREE_CODE (t) == VIEW_CONVERT_EXPR
4901 && TREE_CODE (TREE_OPERAND (t, 0)) == CONSTRUCTOR
4902 && TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (TREE_OPERAND (t, 0))))
4903 && tree_int_cst_equal (TYPE_SIZE (type),
4904 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (t, 0)))))
4905 return build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (t, 0));
4906
4907 vec<constructor_elt, va_gc> *v;
4908 vec_alloc (v, 1);
4909 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type), t);
4910 return gnat_build_constructor (type, v);
4911 }
4912
4913 /* If the input type has padding, remove it and convert to the output type.
4914 The conditions ordering is arranged to ensure that the output type is not
4915 a padding type here, as it is not clear whether the conversion would
4916 always be correct if this was to happen. */
4917 else if (ecode == RECORD_TYPE && TYPE_PADDING_P (etype))
4918 {
4919 tree unpadded;
4920
4921 /* If we have just converted to this padded type, just get the
4922 inner expression. */
4923 if (TREE_CODE (expr) == CONSTRUCTOR)
4924 unpadded = CONSTRUCTOR_ELT (expr, 0)->value;
4925
4926 /* Otherwise, build an explicit component reference. */
4927 else
4928 unpadded = build_component_ref (expr, TYPE_FIELDS (etype), false);
4929
4930 return convert (type, unpadded);
4931 }
4932
4933 /* If the input is a biased type, convert first to the base type and add
4934 the bias. Note that the bias must go through a full conversion to the
4935 base type, lest it is itself a biased value; this happens for subtypes
4936 of biased types. */
4937 if (ecode == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (etype))
4938 return convert (type, fold_build2 (PLUS_EXPR, TREE_TYPE (etype),
4939 fold_convert (TREE_TYPE (etype), expr),
4940 convert (TREE_TYPE (etype),
4941 TYPE_MIN_VALUE (etype))));
4942
4943 /* If the input is a justified modular type, we need to extract the actual
4944 object before converting it to an other type with the exceptions of an
4945 [unconstrained] array or a mere type variant. It is useful to avoid
4946 the extraction and conversion in these cases because it could end up
4947 replacing a VAR_DECL by a constructor and we might be about the take
4948 the address of the result. */
4949 if (ecode == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (etype)
4950 && code != ARRAY_TYPE
4951 && code != UNCONSTRAINED_ARRAY_TYPE
4952 && TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (etype))
4953 return
4954 convert (type, build_component_ref (expr, TYPE_FIELDS (etype), false));
4955
4956 /* If converting to a type that contains a template, convert to the data
4957 type and then build the template. */
4958 if (code == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (type))
4959 {
4960 tree obj_type = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type)));
4961 vec<constructor_elt, va_gc> *v;
4962 vec_alloc (v, 2);
4963
4964 /* If the source already has a template, get a reference to the
4965 associated array only, as we are going to rebuild a template
4966 for the target type anyway. */
4967 expr = maybe_unconstrained_array (expr);
4968
4969 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type),
4970 build_template (TREE_TYPE (TYPE_FIELDS (type)),
4971 obj_type, NULL_TREE));
4972 if (expr)
4973 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (TYPE_FIELDS (type)),
4974 convert (obj_type, expr));
4975 return gnat_build_constructor (type, v);
4976 }
4977
4978 /* There are some cases of expressions that we process specially. */
4979 switch (TREE_CODE (expr))
4980 {
4981 case ERROR_MARK:
4982 return expr;
4983
4984 case NULL_EXPR:
4985 /* Just set its type here. For TRANSFORM_EXPR, we will do the actual
4986 conversion in gnat_expand_expr. NULL_EXPR does not represent
4987 and actual value, so no conversion is needed. */
4988 expr = copy_node (expr);
4989 TREE_TYPE (expr) = type;
4990 return expr;
4991
4992 case STRING_CST:
4993 /* If we are converting a STRING_CST to another constrained array type,
4994 just make a new one in the proper type. */
4995 if (code == ecode
4996 && !(TREE_CONSTANT (TYPE_SIZE (etype))
4997 && !TREE_CONSTANT (TYPE_SIZE (type))))
4998 {
4999 expr = copy_node (expr);
5000 TREE_TYPE (expr) = type;
5001 return expr;
5002 }
5003 break;
5004
5005 case VECTOR_CST:
5006 /* If we are converting a VECTOR_CST to a mere type variant, just make
5007 a new one in the proper type. */
5008 if (code == ecode && gnat_types_compatible_p (type, etype))
5009 {
5010 expr = copy_node (expr);
5011 TREE_TYPE (expr) = type;
5012 return expr;
5013 }
5014 break;
5015
5016 case CONSTRUCTOR:
5017 /* If we are converting a CONSTRUCTOR to a mere type variant, or to
5018 another padding type around the same type, just make a new one in
5019 the proper type. */
5020 if (code == ecode
5021 && (gnat_types_compatible_p (type, etype)
5022 || (code == RECORD_TYPE
5023 && TYPE_PADDING_P (type) && TYPE_PADDING_P (etype)
5024 && TREE_TYPE (TYPE_FIELDS (type))
5025 == TREE_TYPE (TYPE_FIELDS (etype)))))
5026 {
5027 expr = copy_node (expr);
5028 TREE_TYPE (expr) = type;
5029 CONSTRUCTOR_ELTS (expr) = vec_safe_copy (CONSTRUCTOR_ELTS (expr));
5030 return expr;
5031 }
5032
5033 /* Likewise for a conversion between original and packable version, or
5034 conversion between types of the same size and with the same list of
5035 fields, but we have to work harder to preserve type consistency. */
5036 if (code == ecode
5037 && code == RECORD_TYPE
5038 && (TYPE_NAME (type) == TYPE_NAME (etype)
5039 || tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (etype))))
5040
5041 {
5042 vec<constructor_elt, va_gc> *e = CONSTRUCTOR_ELTS (expr);
5043 unsigned HOST_WIDE_INT len = vec_safe_length (e);
5044 vec<constructor_elt, va_gc> *v;
5045 vec_alloc (v, len);
5046 tree efield = TYPE_FIELDS (etype), field = TYPE_FIELDS (type);
5047 unsigned HOST_WIDE_INT idx;
5048 tree index, value;
5049
5050 /* Whether we need to clear TREE_CONSTANT et al. on the output
5051 constructor when we convert in place. */
5052 bool clear_constant = false;
5053
5054 FOR_EACH_CONSTRUCTOR_ELT(e, idx, index, value)
5055 {
5056 /* Skip the missing fields in the CONSTRUCTOR. */
5057 while (efield && field && !SAME_FIELD_P (efield, index))
5058 {
5059 efield = DECL_CHAIN (efield);
5060 field = DECL_CHAIN (field);
5061 }
5062 /* The field must be the same. */
5063 if (!(efield && field && SAME_FIELD_P (efield, field)))
5064 break;
5065 constructor_elt elt
5066 = {field, convert (TREE_TYPE (field), value)};
5067 v->quick_push (elt);
5068
5069 /* If packing has made this field a bitfield and the input
5070 value couldn't be emitted statically any more, we need to
5071 clear TREE_CONSTANT on our output. */
5072 if (!clear_constant
5073 && TREE_CONSTANT (expr)
5074 && !CONSTRUCTOR_BITFIELD_P (efield)
5075 && CONSTRUCTOR_BITFIELD_P (field)
5076 && !initializer_constant_valid_for_bitfield_p (value))
5077 clear_constant = true;
5078
5079 efield = DECL_CHAIN (efield);
5080 field = DECL_CHAIN (field);
5081 }
5082
5083 /* If we have been able to match and convert all the input fields
5084 to their output type, convert in place now. We'll fallback to a
5085 view conversion downstream otherwise. */
5086 if (idx == len)
5087 {
5088 expr = copy_node (expr);
5089 TREE_TYPE (expr) = type;
5090 CONSTRUCTOR_ELTS (expr) = v;
5091 if (clear_constant)
5092 TREE_CONSTANT (expr) = TREE_STATIC (expr) = 0;
5093 return expr;
5094 }
5095 }
5096
5097 /* Likewise for a conversion between array type and vector type with a
5098 compatible representative array. */
5099 else if (code == VECTOR_TYPE
5100 && ecode == ARRAY_TYPE
5101 && gnat_types_compatible_p (TYPE_REPRESENTATIVE_ARRAY (type),
5102 etype))
5103 {
5104 vec<constructor_elt, va_gc> *e = CONSTRUCTOR_ELTS (expr);
5105 unsigned HOST_WIDE_INT len = vec_safe_length (e);
5106 vec<constructor_elt, va_gc> *v;
5107 unsigned HOST_WIDE_INT ix;
5108 tree value;
5109
5110 /* Build a VECTOR_CST from a *constant* array constructor. */
5111 if (TREE_CONSTANT (expr))
5112 {
5113 bool constant_p = true;
5114
5115 /* Iterate through elements and check if all constructor
5116 elements are *_CSTs. */
5117 FOR_EACH_CONSTRUCTOR_VALUE (e, ix, value)
5118 if (!CONSTANT_CLASS_P (value))
5119 {
5120 constant_p = false;
5121 break;
5122 }
5123
5124 if (constant_p)
5125 return build_vector_from_ctor (type,
5126 CONSTRUCTOR_ELTS (expr));
5127 }
5128
5129 /* Otherwise, build a regular vector constructor. */
5130 vec_alloc (v, len);
5131 FOR_EACH_CONSTRUCTOR_VALUE (e, ix, value)
5132 {
5133 constructor_elt elt = {NULL_TREE, value};
5134 v->quick_push (elt);
5135 }
5136 expr = copy_node (expr);
5137 TREE_TYPE (expr) = type;
5138 CONSTRUCTOR_ELTS (expr) = v;
5139 return expr;
5140 }
5141 break;
5142
5143 case UNCONSTRAINED_ARRAY_REF:
5144 /* First retrieve the underlying array. */
5145 expr = maybe_unconstrained_array (expr);
5146 etype = TREE_TYPE (expr);
5147 ecode = TREE_CODE (etype);
5148 break;
5149
5150 case VIEW_CONVERT_EXPR:
5151 {
5152 /* GCC 4.x is very sensitive to type consistency overall, and view
5153 conversions thus are very frequent. Even though just "convert"ing
5154 the inner operand to the output type is fine in most cases, it
5155 might expose unexpected input/output type mismatches in special
5156 circumstances so we avoid such recursive calls when we can. */
5157 tree op0 = TREE_OPERAND (expr, 0);
5158
5159 /* If we are converting back to the original type, we can just
5160 lift the input conversion. This is a common occurrence with
5161 switches back-and-forth amongst type variants. */
5162 if (type == TREE_TYPE (op0))
5163 return op0;
5164
5165 /* Otherwise, if we're converting between two aggregate or vector
5166 types, we might be allowed to substitute the VIEW_CONVERT_EXPR
5167 target type in place or to just convert the inner expression. */
5168 if ((AGGREGATE_TYPE_P (type) && AGGREGATE_TYPE_P (etype))
5169 || (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (etype)))
5170 {
5171 /* If we are converting between mere variants, we can just
5172 substitute the VIEW_CONVERT_EXPR in place. */
5173 if (gnat_types_compatible_p (type, etype))
5174 return build1 (VIEW_CONVERT_EXPR, type, op0);
5175
5176 /* Otherwise, we may just bypass the input view conversion unless
5177 one of the types is a fat pointer, which is handled by
5178 specialized code below which relies on exact type matching. */
5179 else if (!TYPE_IS_FAT_POINTER_P (type)
5180 && !TYPE_IS_FAT_POINTER_P (etype))
5181 return convert (type, op0);
5182 }
5183
5184 break;
5185 }
5186
5187 default:
5188 break;
5189 }
5190
5191 /* Check for converting to a pointer to an unconstrained array. */
5192 if (TYPE_IS_FAT_POINTER_P (type) && !TYPE_IS_FAT_POINTER_P (etype))
5193 return convert_to_fat_pointer (type, expr);
5194
5195 /* If we are converting between two aggregate or vector types that are mere
5196 variants, just make a VIEW_CONVERT_EXPR. Likewise when we are converting
5197 to a vector type from its representative array type. */
5198 else if ((code == ecode
5199 && (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
5200 && gnat_types_compatible_p (type, etype))
5201 || (code == VECTOR_TYPE
5202 && ecode == ARRAY_TYPE
5203 && gnat_types_compatible_p (TYPE_REPRESENTATIVE_ARRAY (type),
5204 etype)))
5205 return build1 (VIEW_CONVERT_EXPR, type, expr);
5206
5207 /* If we are converting between tagged types, try to upcast properly.
5208 But don't do it if we are just annotating types since tagged types
5209 aren't fully laid out in this mode. */
5210 else if (ecode == RECORD_TYPE && code == RECORD_TYPE
5211 && TYPE_ALIGN_OK (etype) && TYPE_ALIGN_OK (type)
5212 && !type_annotate_only)
5213 {
5214 tree child_etype = etype;
5215 do {
5216 tree field = TYPE_FIELDS (child_etype);
5217 if (DECL_NAME (field) == parent_name_id && TREE_TYPE (field) == type)
5218 return build_component_ref (expr, field, false);
5219 child_etype = TREE_TYPE (field);
5220 } while (TREE_CODE (child_etype) == RECORD_TYPE);
5221 }
5222
5223 /* If we are converting from a smaller form of record type back to it, just
5224 make a VIEW_CONVERT_EXPR. But first pad the expression to have the same
5225 size on both sides. */
5226 else if (ecode == RECORD_TYPE && code == RECORD_TYPE
5227 && smaller_form_type_p (etype, type))
5228 {
5229 expr = convert (maybe_pad_type (etype, TYPE_SIZE (type), 0, Empty,
5230 false, false, true),
5231 expr);
5232 return build1 (VIEW_CONVERT_EXPR, type, expr);
5233 }
5234
5235 /* In all other cases of related types, make a NOP_EXPR. */
5236 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (etype))
5237 return fold_convert (type, expr);
5238
5239 switch (code)
5240 {
5241 case VOID_TYPE:
5242 return fold_build1 (CONVERT_EXPR, type, expr);
5243
5244 case INTEGER_TYPE:
5245 if (TYPE_HAS_ACTUAL_BOUNDS_P (type)
5246 && (ecode == ARRAY_TYPE || ecode == UNCONSTRAINED_ARRAY_TYPE
5247 || (ecode == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (etype))))
5248 return unchecked_convert (type, expr, false);
5249
5250 /* If the output is a biased type, convert first to the base type and
5251 subtract the bias. Note that the bias itself must go through a full
5252 conversion to the base type, lest it is a biased value; this happens
5253 for subtypes of biased types. */
5254 if (TYPE_BIASED_REPRESENTATION_P (type))
5255 return fold_convert (type,
5256 fold_build2 (MINUS_EXPR, TREE_TYPE (type),
5257 convert (TREE_TYPE (type), expr),
5258 convert (TREE_TYPE (type),
5259 TYPE_MIN_VALUE (type))));
5260
5261 /* If we are converting an additive expression to an integer type
5262 with lower precision, be wary of the optimization that can be
5263 applied by convert_to_integer. There are 2 problematic cases:
5264 - if the first operand was originally of a biased type,
5265 because we could be recursively called to convert it
5266 to an intermediate type and thus rematerialize the
5267 additive operator endlessly,
5268 - if the expression contains a placeholder, because an
5269 intermediate conversion that changes the sign could
5270 be inserted and thus introduce an artificial overflow
5271 at compile time when the placeholder is substituted. */
5272 if (ecode == INTEGER_TYPE
5273 && TYPE_PRECISION (type) < TYPE_PRECISION (etype)
5274 && (TREE_CODE (expr) == PLUS_EXPR || TREE_CODE (expr) == MINUS_EXPR))
5275 {
5276 tree op0 = get_unwidened (TREE_OPERAND (expr, 0), type);
5277
5278 if ((TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5279 && TYPE_BIASED_REPRESENTATION_P (TREE_TYPE (op0)))
5280 || CONTAINS_PLACEHOLDER_P (expr))
5281 return fold_convert (type, expr);
5282 }
5283
5284 /* ... fall through ... */
5285
5286 case ENUMERAL_TYPE:
5287 return fold (convert_to_integer (type, expr));
5288
5289 case BOOLEAN_TYPE:
5290 /* Do not use convert_to_integer with boolean types. */
5291 return fold_convert_loc (EXPR_LOCATION (expr), type, expr);
5292
5293 case POINTER_TYPE:
5294 case REFERENCE_TYPE:
5295 /* If converting between two thin pointers, adjust if needed to account
5296 for differing offsets from the base pointer, depending on whether
5297 there is a TYPE_UNCONSTRAINED_ARRAY attached to the record type. */
5298 if (TYPE_IS_THIN_POINTER_P (etype) && TYPE_IS_THIN_POINTER_P (type))
5299 {
5300 tree etype_pos
5301 = TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (etype))
5302 ? byte_position (DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (etype))))
5303 : size_zero_node;
5304 tree type_pos
5305 = TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type))
5306 ? byte_position (DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (type))))
5307 : size_zero_node;
5308 tree byte_diff = size_diffop (type_pos, etype_pos);
5309
5310 expr = fold_convert (type, expr);
5311 if (integer_zerop (byte_diff))
5312 return expr;
5313
5314 return build_binary_op (POINTER_PLUS_EXPR, type, expr,
5315 fold_convert (sizetype, byte_diff));
5316 }
5317
5318 /* If converting from a thin pointer with zero offset from the base to
5319 a pointer to the array, add the offset of the array field. */
5320 if (TYPE_IS_THIN_POINTER_P (etype)
5321 && !TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (etype)))
5322 {
5323 tree arr_field = DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (etype)));
5324
5325 if (TREE_TYPE (type) == TREE_TYPE (arr_field))
5326 {
5327 expr = fold_convert (type, expr);
5328 return build_binary_op (POINTER_PLUS_EXPR, type, expr,
5329 byte_position (arr_field));
5330 }
5331 }
5332
5333 /* If converting fat pointer to normal or thin pointer, get the pointer
5334 to the array and then convert it. */
5335 if (TYPE_IS_FAT_POINTER_P (etype))
5336 expr = build_component_ref (expr, TYPE_FIELDS (etype), false);
5337
5338 return fold (convert_to_pointer (type, expr));
5339
5340 case REAL_TYPE:
5341 return fold (convert_to_real (type, expr));
5342
5343 case RECORD_TYPE:
5344 /* Do a normal conversion between scalar and justified modular type. */
5345 if (TYPE_JUSTIFIED_MODULAR_P (type) && !AGGREGATE_TYPE_P (etype))
5346 {
5347 vec<constructor_elt, va_gc> *v;
5348 vec_alloc (v, 1);
5349
5350 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type),
5351 convert (TREE_TYPE (TYPE_FIELDS (type)),
5352 expr));
5353 return gnat_build_constructor (type, v);
5354 }
5355
5356 /* In these cases, assume the front-end has validated the conversion.
5357 If the conversion is valid, it will be a bit-wise conversion, so
5358 it can be viewed as an unchecked conversion. */
5359 return unchecked_convert (type, expr, false);
5360
5361 case ARRAY_TYPE:
5362 /* Do a normal conversion between unconstrained and constrained array
5363 type, assuming the latter is a constrained version of the former. */
5364 if (TREE_CODE (expr) == INDIRECT_REF
5365 && ecode == ARRAY_TYPE
5366 && TREE_TYPE (etype) == TREE_TYPE (type))
5367 {
5368 tree ptr_type = build_pointer_type (type);
5369 tree t = build_unary_op (INDIRECT_REF, NULL_TREE,
5370 fold_convert (ptr_type,
5371 TREE_OPERAND (expr, 0)));
5372 TREE_READONLY (t) = TREE_READONLY (expr);
5373 TREE_THIS_NOTRAP (t) = TREE_THIS_NOTRAP (expr);
5374 return t;
5375 }
5376
5377 /* In these cases, assume the front-end has validated the conversion.
5378 If the conversion is valid, it will be a bit-wise conversion, so
5379 it can be viewed as an unchecked conversion. */
5380 return unchecked_convert (type, expr, false);
5381
5382 case UNION_TYPE:
5383 /* This is a either a conversion between a tagged type and some
5384 subtype, which we have to mark as a UNION_TYPE because of
5385 overlapping fields or a conversion of an Unchecked_Union. */
5386 return unchecked_convert (type, expr, false);
5387
5388 case UNCONSTRAINED_ARRAY_TYPE:
5389 /* If the input is a VECTOR_TYPE, convert to the representative
5390 array type first. */
5391 if (ecode == VECTOR_TYPE)
5392 {
5393 expr = convert (TYPE_REPRESENTATIVE_ARRAY (etype), expr);
5394 etype = TREE_TYPE (expr);
5395 ecode = TREE_CODE (etype);
5396 }
5397
5398 /* If EXPR is a constrained array, take its address, convert it to a
5399 fat pointer, and then dereference it. Likewise if EXPR is a
5400 record containing both a template and a constrained array.
5401 Note that a record representing a justified modular type
5402 always represents a packed constrained array. */
5403 if (ecode == ARRAY_TYPE
5404 || (ecode == INTEGER_TYPE && TYPE_HAS_ACTUAL_BOUNDS_P (etype))
5405 || (ecode == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (etype))
5406 || (ecode == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (etype)))
5407 return
5408 build_unary_op
5409 (INDIRECT_REF, NULL_TREE,
5410 convert_to_fat_pointer (TREE_TYPE (type),
5411 build_unary_op (ADDR_EXPR,
5412 NULL_TREE, expr)));
5413
5414 /* Do something very similar for converting one unconstrained
5415 array to another. */
5416 else if (ecode == UNCONSTRAINED_ARRAY_TYPE)
5417 return
5418 build_unary_op (INDIRECT_REF, NULL_TREE,
5419 convert (TREE_TYPE (type),
5420 build_unary_op (ADDR_EXPR,
5421 NULL_TREE, expr)));
5422 else
5423 gcc_unreachable ();
5424
5425 case COMPLEX_TYPE:
5426 return fold (convert_to_complex (type, expr));
5427
5428 default:
5429 gcc_unreachable ();
5430 }
5431 }
5432
5433 /* Create an expression whose value is that of EXPR converted to the common
5434 index type, which is sizetype. EXPR is supposed to be in the base type
5435 of the GNAT index type. Calling it is equivalent to doing
5436
5437 convert (sizetype, expr)
5438
5439 but we try to distribute the type conversion with the knowledge that EXPR
5440 cannot overflow in its type. This is a best-effort approach and we fall
5441 back to the above expression as soon as difficulties are encountered.
5442
5443 This is necessary to overcome issues that arise when the GNAT base index
5444 type and the GCC common index type (sizetype) don't have the same size,
5445 which is quite frequent on 64-bit architectures. In this case, and if
5446 the GNAT base index type is signed but the iteration type of the loop has
5447 been forced to unsigned, the loop scalar evolution engine cannot compute
5448 a simple evolution for the general induction variables associated with the
5449 array indices, because it will preserve the wrap-around semantics in the
5450 unsigned type of their "inner" part. As a result, many loop optimizations
5451 are blocked.
5452
5453 The solution is to use a special (basic) induction variable that is at
5454 least as large as sizetype, and to express the aforementioned general
5455 induction variables in terms of this induction variable, eliminating
5456 the problematic intermediate truncation to the GNAT base index type.
5457 This is possible as long as the original expression doesn't overflow
5458 and if the middle-end hasn't introduced artificial overflows in the
5459 course of the various simplification it can make to the expression. */
5460
5461 tree
5462 convert_to_index_type (tree expr)
5463 {
5464 enum tree_code code = TREE_CODE (expr);
5465 tree type = TREE_TYPE (expr);
5466
5467 /* If the type is unsigned, overflow is allowed so we cannot be sure that
5468 EXPR doesn't overflow. Keep it simple if optimization is disabled. */
5469 if (TYPE_UNSIGNED (type) || !optimize || optimize_debug)
5470 return convert (sizetype, expr);
5471
5472 switch (code)
5473 {
5474 case VAR_DECL:
5475 /* The main effect of the function: replace a loop parameter with its
5476 associated special induction variable. */
5477 if (DECL_LOOP_PARM_P (expr) && DECL_INDUCTION_VAR (expr))
5478 expr = DECL_INDUCTION_VAR (expr);
5479 break;
5480
5481 CASE_CONVERT:
5482 {
5483 tree otype = TREE_TYPE (TREE_OPERAND (expr, 0));
5484 /* Bail out as soon as we suspect some sort of type frobbing. */
5485 if (TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5486 || TYPE_UNSIGNED (type) != TYPE_UNSIGNED (otype))
5487 break;
5488 }
5489
5490 /* ... fall through ... */
5491
5492 case NON_LVALUE_EXPR:
5493 return fold_build1 (code, sizetype,
5494 convert_to_index_type (TREE_OPERAND (expr, 0)));
5495
5496 case PLUS_EXPR:
5497 case MINUS_EXPR:
5498 case MULT_EXPR:
5499 return fold_build2 (code, sizetype,
5500 convert_to_index_type (TREE_OPERAND (expr, 0)),
5501 convert_to_index_type (TREE_OPERAND (expr, 1)));
5502
5503 case COMPOUND_EXPR:
5504 return fold_build2 (code, sizetype, TREE_OPERAND (expr, 0),
5505 convert_to_index_type (TREE_OPERAND (expr, 1)));
5506
5507 case COND_EXPR:
5508 return fold_build3 (code, sizetype, TREE_OPERAND (expr, 0),
5509 convert_to_index_type (TREE_OPERAND (expr, 1)),
5510 convert_to_index_type (TREE_OPERAND (expr, 2)));
5511
5512 default:
5513 break;
5514 }
5515
5516 return convert (sizetype, expr);
5517 }
5518
5519 /* Remove all conversions that are done in EXP. This includes converting
5520 from a padded type or to a justified modular type. If TRUE_ADDRESS
5521 is true, always return the address of the containing object even if
5522 the address is not bit-aligned. */
5523
5524 tree
5525 remove_conversions (tree exp, bool true_address)
5526 {
5527 switch (TREE_CODE (exp))
5528 {
5529 case CONSTRUCTOR:
5530 if (true_address
5531 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
5532 && TYPE_JUSTIFIED_MODULAR_P (TREE_TYPE (exp)))
5533 return
5534 remove_conversions (CONSTRUCTOR_ELT (exp, 0)->value, true);
5535 break;
5536
5537 case COMPONENT_REF:
5538 if (TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
5539 return remove_conversions (TREE_OPERAND (exp, 0), true_address);
5540 break;
5541
5542 CASE_CONVERT:
5543 case VIEW_CONVERT_EXPR:
5544 case NON_LVALUE_EXPR:
5545 return remove_conversions (TREE_OPERAND (exp, 0), true_address);
5546
5547 default:
5548 break;
5549 }
5550
5551 return exp;
5552 }
5553
5554 /* If EXP's type is an UNCONSTRAINED_ARRAY_TYPE, return an expression that
5555 refers to the underlying array. If it has TYPE_CONTAINS_TEMPLATE_P,
5556 likewise return an expression pointing to the underlying array. */
5557
5558 tree
5559 maybe_unconstrained_array (tree exp)
5560 {
5561 enum tree_code code = TREE_CODE (exp);
5562 tree type = TREE_TYPE (exp);
5563
5564 switch (TREE_CODE (type))
5565 {
5566 case UNCONSTRAINED_ARRAY_TYPE:
5567 if (code == UNCONSTRAINED_ARRAY_REF)
5568 {
5569 const bool read_only = TREE_READONLY (exp);
5570 const bool no_trap = TREE_THIS_NOTRAP (exp);
5571
5572 exp = TREE_OPERAND (exp, 0);
5573 type = TREE_TYPE (exp);
5574
5575 if (TREE_CODE (exp) == COND_EXPR)
5576 {
5577 tree op1
5578 = build_unary_op (INDIRECT_REF, NULL_TREE,
5579 build_component_ref (TREE_OPERAND (exp, 1),
5580 TYPE_FIELDS (type),
5581 false));
5582 tree op2
5583 = build_unary_op (INDIRECT_REF, NULL_TREE,
5584 build_component_ref (TREE_OPERAND (exp, 2),
5585 TYPE_FIELDS (type),
5586 false));
5587
5588 exp = build3 (COND_EXPR,
5589 TREE_TYPE (TREE_TYPE (TYPE_FIELDS (type))),
5590 TREE_OPERAND (exp, 0), op1, op2);
5591 }
5592 else
5593 {
5594 exp = build_unary_op (INDIRECT_REF, NULL_TREE,
5595 build_component_ref (exp,
5596 TYPE_FIELDS (type),
5597 false));
5598 TREE_READONLY (exp) = read_only;
5599 TREE_THIS_NOTRAP (exp) = no_trap;
5600 }
5601 }
5602
5603 else if (code == LOAD_EXPR)
5604 {
5605 const Entity_Id gnat_smo = tree_to_shwi (TREE_OPERAND (exp, 1));
5606 tree t = maybe_unconstrained_array (TREE_OPERAND (exp, 0));
5607 exp = build_storage_model_load (gnat_smo, t);
5608 }
5609
5610 else if (code == NULL_EXPR)
5611 exp = build1 (NULL_EXPR,
5612 TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (type)))),
5613 TREE_OPERAND (exp, 0));
5614 break;
5615
5616 case RECORD_TYPE:
5617 /* If this is a padded type and it contains a template, convert to the
5618 unpadded type first. */
5619 if (TYPE_PADDING_P (type)
5620 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (type))) == RECORD_TYPE
5621 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (TYPE_FIELDS (type))))
5622 {
5623 exp = convert (TREE_TYPE (TYPE_FIELDS (type)), exp);
5624 code = TREE_CODE (exp);
5625 type = TREE_TYPE (exp);
5626 }
5627
5628 if (TYPE_CONTAINS_TEMPLATE_P (type))
5629 {
5630 /* If the array initializer is a box, return NULL_TREE. */
5631 if (code == CONSTRUCTOR && CONSTRUCTOR_NELTS (exp) < 2)
5632 return NULL_TREE;
5633
5634 exp = build_component_ref (exp, DECL_CHAIN (TYPE_FIELDS (type)),
5635 false);
5636
5637 /* If the array is padded, remove the padding. */
5638 exp = maybe_padded_object (exp);
5639 }
5640 break;
5641
5642 default:
5643 break;
5644 }
5645
5646 return exp;
5647 }
5648
5649 /* Return true if EXPR is an expression that can be folded as an operand
5650 of a VIEW_CONVERT_EXPR. See ada-tree.h for a complete rationale. */
5651
5652 static bool
5653 can_fold_for_view_convert_p (tree expr)
5654 {
5655 tree t1, t2;
5656
5657 /* The folder will fold NOP_EXPRs between integral types with the same
5658 precision (in the middle-end's sense). We cannot allow it if the
5659 types don't have the same precision in the Ada sense as well. */
5660 if (TREE_CODE (expr) != NOP_EXPR)
5661 return true;
5662
5663 t1 = TREE_TYPE (expr);
5664 t2 = TREE_TYPE (TREE_OPERAND (expr, 0));
5665
5666 /* Defer to the folder for non-integral conversions. */
5667 if (!(INTEGRAL_TYPE_P (t1) && INTEGRAL_TYPE_P (t2)))
5668 return true;
5669
5670 /* Only fold conversions that preserve both precisions. */
5671 if (TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5672 && operand_equal_p (rm_size (t1), rm_size (t2), 0))
5673 return true;
5674
5675 return false;
5676 }
5677
5678 /* Return an expression that does an unchecked conversion of EXPR to TYPE.
5679 If NOTRUNC_P is true, truncation operations should be suppressed.
5680
5681 Special care is required with (source or target) integral types whose
5682 precision is not equal to their size, to make sure we fetch or assign
5683 the value bits whose location might depend on the endianness, e.g.
5684
5685 Rmsize : constant := 8;
5686 subtype Int is Integer range 0 .. 2 ** Rmsize - 1;
5687
5688 type Bit_Array is array (1 .. Rmsize) of Boolean;
5689 pragma Pack (Bit_Array);
5690
5691 function To_Bit_Array is new Unchecked_Conversion (Int, Bit_Array);
5692
5693 Value : Int := 2#1000_0001#;
5694 Vbits : Bit_Array := To_Bit_Array (Value);
5695
5696 we expect the 8 bits at Vbits'Address to always contain Value, while
5697 their original location depends on the endianness, at Value'Address
5698 on a little-endian architecture but not on a big-endian one.
5699
5700 One pitfall is that we cannot use TYPE_UNSIGNED directly to decide how
5701 the bits between the precision and the size are filled, because of the
5702 trick used in the E_Signed_Integer_Subtype case of gnat_to_gnu_entity.
5703 So we use the special predicate type_unsigned_for_rm above. */
5704
5705 tree
5706 unchecked_convert (tree type, tree expr, bool notrunc_p)
5707 {
5708 tree etype = TREE_TYPE (expr);
5709 enum tree_code ecode = TREE_CODE (etype);
5710 enum tree_code code = TREE_CODE (type);
5711 const bool ebiased
5712 = (ecode == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (etype));
5713 const bool biased
5714 = (code == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (type));
5715 const bool ereverse
5716 = (AGGREGATE_TYPE_P (etype) && TYPE_REVERSE_STORAGE_ORDER (etype));
5717 const bool reverse
5718 = (AGGREGATE_TYPE_P (type) && TYPE_REVERSE_STORAGE_ORDER (type));
5719 tree tem;
5720 int c = 0;
5721
5722 /* If the expression is already of the right type, we are done. */
5723 if (etype == type)
5724 return expr;
5725
5726 /* If both types are integral or regular pointer, then just do a normal
5727 conversion. Likewise for a conversion to an unconstrained array. */
5728 if (((INTEGRAL_TYPE_P (type)
5729 || (POINTER_TYPE_P (type) && !TYPE_IS_THIN_POINTER_P (type))
5730 || (code == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (type)))
5731 && (INTEGRAL_TYPE_P (etype)
5732 || (POINTER_TYPE_P (etype) && !TYPE_IS_THIN_POINTER_P (etype))
5733 || (ecode == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (etype))))
5734 || code == UNCONSTRAINED_ARRAY_TYPE)
5735 {
5736 if (ebiased)
5737 {
5738 tree ntype = copy_type (etype);
5739 TYPE_BIASED_REPRESENTATION_P (ntype) = 0;
5740 TYPE_MAIN_VARIANT (ntype) = ntype;
5741 expr = build1 (NOP_EXPR, ntype, expr);
5742 }
5743
5744 if (biased)
5745 {
5746 tree rtype = copy_type (type);
5747 TYPE_BIASED_REPRESENTATION_P (rtype) = 0;
5748 TYPE_MAIN_VARIANT (rtype) = rtype;
5749 expr = convert (rtype, expr);
5750 expr = build1 (NOP_EXPR, type, expr);
5751 }
5752 else
5753 expr = convert (type, expr);
5754 }
5755
5756 /* If we are converting to an integral type whose precision is not equal
5757 to its size, first unchecked convert to a record type that contains a
5758 field of the given precision. Then extract the result from the field.
5759
5760 There is a subtlety if the source type is an aggregate type with reverse
5761 storage order because its representation is not contiguous in the native
5762 storage order, i.e. a direct unchecked conversion to an integral type
5763 with N bits of precision cannot read the first N bits of the aggregate
5764 type. To overcome it, we do an unchecked conversion to an integral type
5765 with reverse storage order and return the resulting value. This also
5766 ensures that the result of the unchecked conversion doesn't depend on
5767 the endianness of the target machine, but only on the storage order of
5768 the aggregate type.
5769
5770 Finally, for the sake of consistency, we do the unchecked conversion
5771 to an integral type with reverse storage order as soon as the source
5772 type is an aggregate type with reverse storage order, even if there
5773 are no considerations of precision or size involved. Ultimately, we
5774 further extend this processing to any scalar type. */
5775 else if ((INTEGRAL_TYPE_P (type)
5776 && TYPE_RM_SIZE (type)
5777 && ((c = tree_int_cst_compare (TYPE_RM_SIZE (type),
5778 TYPE_SIZE (type))) < 0
5779 || ereverse))
5780 || (SCALAR_FLOAT_TYPE_P (type) && ereverse))
5781 {
5782 tree rec_type = make_node (RECORD_TYPE);
5783 tree field_type, field;
5784
5785 TYPE_REVERSE_STORAGE_ORDER (rec_type) = ereverse;
5786
5787 if (c < 0)
5788 {
5789 const unsigned HOST_WIDE_INT prec
5790 = TREE_INT_CST_LOW (TYPE_RM_SIZE (type));
5791 if (type_unsigned_for_rm (type))
5792 field_type = make_unsigned_type (prec);
5793 else
5794 field_type = make_signed_type (prec);
5795 SET_TYPE_RM_SIZE (field_type, TYPE_RM_SIZE (type));
5796 }
5797 else
5798 field_type = type;
5799
5800 field = create_field_decl (get_identifier ("OBJ"), field_type, rec_type,
5801 NULL_TREE, bitsize_zero_node, c < 0, 0);
5802
5803 finish_record_type (rec_type, field, 1, false);
5804
5805 expr = unchecked_convert (rec_type, expr, notrunc_p);
5806 expr = build_component_ref (expr, field, false);
5807 expr = fold_build1 (NOP_EXPR, type, expr);
5808 }
5809
5810 /* Similarly if we are converting from an integral type whose precision is
5811 not equal to its size, first copy into a field of the given precision
5812 and unchecked convert the record type.
5813
5814 The same considerations as above apply if the target type is an aggregate
5815 type with reverse storage order and we also proceed similarly. */
5816 else if ((INTEGRAL_TYPE_P (etype)
5817 && TYPE_RM_SIZE (etype)
5818 && ((c = tree_int_cst_compare (TYPE_RM_SIZE (etype),
5819 TYPE_SIZE (etype))) < 0
5820 || reverse))
5821 || (SCALAR_FLOAT_TYPE_P (etype) && reverse))
5822 {
5823 tree rec_type = make_node (RECORD_TYPE);
5824 vec<constructor_elt, va_gc> *v;
5825 vec_alloc (v, 1);
5826 tree field_type, field;
5827
5828 TYPE_REVERSE_STORAGE_ORDER (rec_type) = reverse;
5829
5830 if (c < 0)
5831 {
5832 const unsigned HOST_WIDE_INT prec
5833 = TREE_INT_CST_LOW (TYPE_RM_SIZE (etype));
5834 if (type_unsigned_for_rm (etype))
5835 field_type = make_unsigned_type (prec);
5836 else
5837 field_type = make_signed_type (prec);
5838 SET_TYPE_RM_SIZE (field_type, TYPE_RM_SIZE (etype));
5839 }
5840 else
5841 field_type = etype;
5842
5843 field = create_field_decl (get_identifier ("OBJ"), field_type, rec_type,
5844 NULL_TREE, bitsize_zero_node, c < 0, 0);
5845
5846 finish_record_type (rec_type, field, 1, false);
5847
5848 expr = fold_build1 (NOP_EXPR, field_type, expr);
5849 CONSTRUCTOR_APPEND_ELT (v, field, expr);
5850 expr = gnat_build_constructor (rec_type, v);
5851 expr = unchecked_convert (type, expr, notrunc_p);
5852 }
5853
5854 /* If we are converting between fixed-size types with different sizes, we
5855 need to pad to have the same size on both sides.
5856
5857 ??? We cannot do it unconditionally because unchecked conversions are
5858 used liberally by the front-end to implement interface thunks:
5859
5860 type ada__tags__addr_ptr is access system.address;
5861 S191s : constant ada__tags__addr_ptr := ada__tags__addr_ptr!(S190s);
5862 return p___size__4 (p__object!(S191s.all));
5863
5864 so we need to skip dereferences. */
5865 else if (!INDIRECT_REF_P (expr)
5866 && TREE_CODE (expr) != STRING_CST
5867 && !(AGGREGATE_TYPE_P (etype) && AGGREGATE_TYPE_P (type))
5868 && ecode != UNCONSTRAINED_ARRAY_TYPE
5869 && TREE_CONSTANT (TYPE_SIZE (etype))
5870 && TREE_CONSTANT (TYPE_SIZE (type))
5871 && (c = tree_int_cst_compare (TYPE_SIZE (etype), TYPE_SIZE (type))))
5872 {
5873 if (c < 0)
5874 {
5875 expr = convert (maybe_pad_type (etype, TYPE_SIZE (type), 0, Empty,
5876 false, false, true),
5877 expr);
5878 expr = unchecked_convert (type, expr, notrunc_p);
5879 }
5880 else
5881 {
5882 tree rec_type = maybe_pad_type (type, TYPE_SIZE (etype), 0, Empty,
5883 false, false, true);
5884 expr = unchecked_convert (rec_type, expr, notrunc_p);
5885 expr = build3 (COMPONENT_REF, type, expr, TYPE_FIELDS (rec_type),
5886 NULL_TREE);
5887 }
5888 }
5889
5890 /* Likewise if we are converting from a fixed-size type to a type with self-
5891 referential size. We use the max size to do the padding in this case. */
5892 else if (!INDIRECT_REF_P (expr)
5893 && TREE_CODE (expr) != STRING_CST
5894 && !(AGGREGATE_TYPE_P (etype) && AGGREGATE_TYPE_P (type))
5895 && ecode != UNCONSTRAINED_ARRAY_TYPE
5896 && TREE_CONSTANT (TYPE_SIZE (etype))
5897 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type)))
5898 {
5899 tree new_size = max_size (TYPE_SIZE (type), true);
5900 c = tree_int_cst_compare (TYPE_SIZE (etype), new_size);
5901 if (c < 0)
5902 {
5903 expr = convert (maybe_pad_type (etype, new_size, 0, Empty,
5904 false, false, true),
5905 expr);
5906 expr = unchecked_convert (type, expr, notrunc_p);
5907 }
5908 else
5909 {
5910 tree rec_type = maybe_pad_type (type, TYPE_SIZE (etype), 0, Empty,
5911 false, false, true);
5912 expr = unchecked_convert (rec_type, expr, notrunc_p);
5913 expr = build3 (COMPONENT_REF, type, expr, TYPE_FIELDS (rec_type),
5914 NULL_TREE);
5915 }
5916 }
5917
5918 /* We have a special case when we are converting between two unconstrained
5919 array types. In that case, take the address, convert the fat pointer
5920 types, and dereference. */
5921 else if (ecode == code && code == UNCONSTRAINED_ARRAY_TYPE)
5922 expr = build_unary_op (INDIRECT_REF, NULL_TREE,
5923 build1 (VIEW_CONVERT_EXPR, TREE_TYPE (type),
5924 build_unary_op (ADDR_EXPR, NULL_TREE,
5925 expr)));
5926
5927 /* Another special case is when we are converting to a vector type from its
5928 representative array type; this a regular conversion. */
5929 else if (code == VECTOR_TYPE
5930 && ecode == ARRAY_TYPE
5931 && gnat_types_compatible_p (TYPE_REPRESENTATIVE_ARRAY (type),
5932 etype))
5933 expr = convert (type, expr);
5934
5935 /* And, if the array type is not the representative, we try to build an
5936 intermediate vector type of which the array type is the representative
5937 and to do the unchecked conversion between the vector types, in order
5938 to enable further simplifications in the middle-end. */
5939 else if (code == VECTOR_TYPE
5940 && ecode == ARRAY_TYPE
5941 && (tem = build_vector_type_for_array (etype, NULL_TREE)))
5942 {
5943 expr = convert (tem, expr);
5944 return unchecked_convert (type, expr, notrunc_p);
5945 }
5946
5947 /* If we are converting a CONSTRUCTOR to a more aligned aggregate type, bump
5948 the alignment of the CONSTRUCTOR to speed up the copy operation. But do
5949 not do it for a conversion between original and packable version to avoid
5950 an infinite recursion. */
5951 else if (TREE_CODE (expr) == CONSTRUCTOR
5952 && AGGREGATE_TYPE_P (type)
5953 && TYPE_NAME (type) != TYPE_NAME (etype)
5954 && TYPE_ALIGN (etype) < TYPE_ALIGN (type))
5955 {
5956 expr = convert (maybe_pad_type (etype, NULL_TREE, TYPE_ALIGN (type),
5957 Empty, false, false, true),
5958 expr);
5959 return unchecked_convert (type, expr, notrunc_p);
5960 }
5961
5962 /* If we are converting a CONSTRUCTOR to a larger aggregate type, bump the
5963 size of the CONSTRUCTOR to make sure there are enough allocated bytes.
5964 But do not do it for a conversion between original and packable version
5965 to avoid an infinite recursion. */
5966 else if (TREE_CODE (expr) == CONSTRUCTOR
5967 && AGGREGATE_TYPE_P (type)
5968 && TYPE_NAME (type) != TYPE_NAME (etype)
5969 && TREE_CONSTANT (TYPE_SIZE (type))
5970 && (!TREE_CONSTANT (TYPE_SIZE (etype))
5971 || tree_int_cst_lt (TYPE_SIZE (etype), TYPE_SIZE (type))))
5972 {
5973 expr = convert (maybe_pad_type (etype, TYPE_SIZE (type), 0,
5974 Empty, false, false, true),
5975 expr);
5976 return unchecked_convert (type, expr, notrunc_p);
5977 }
5978
5979 /* If we are converting a string constant to a pointer to character, make
5980 sure that the string is not folded into an integer constant. */
5981 else if (TREE_CODE (expr) == STRING_CST
5982 && POINTER_TYPE_P (type)
5983 && TYPE_STRING_FLAG (TREE_TYPE (type)))
5984 expr = build1 (VIEW_CONVERT_EXPR, type, expr);
5985
5986 /* Otherwise, just build a VIEW_CONVERT_EXPR of the expression. */
5987 else
5988 {
5989 expr = maybe_unconstrained_array (expr);
5990 etype = TREE_TYPE (expr);
5991 ecode = TREE_CODE (etype);
5992 if (can_fold_for_view_convert_p (expr))
5993 expr = fold_build1 (VIEW_CONVERT_EXPR, type, expr);
5994 else
5995 expr = build1 (VIEW_CONVERT_EXPR, type, expr);
5996 }
5997
5998 /* If the result is a non-biased integral type whose precision is not equal
5999 to its size, sign- or zero-extend the result. But we need not do this
6000 if the input is also an integral type and both are unsigned or both are
6001 signed and have the same precision. */
6002 tree type_rm_size;
6003 if (!notrunc_p
6004 && !biased
6005 && INTEGRAL_TYPE_P (type)
6006 && (type_rm_size = TYPE_RM_SIZE (type))
6007 && tree_int_cst_compare (type_rm_size, TYPE_SIZE (type)) < 0
6008 && !(INTEGRAL_TYPE_P (etype)
6009 && type_unsigned_for_rm (type) == type_unsigned_for_rm (etype)
6010 && (type_unsigned_for_rm (type)
6011 || tree_int_cst_compare (type_rm_size,
6012 TYPE_RM_SIZE (etype)
6013 ? TYPE_RM_SIZE (etype)
6014 : TYPE_SIZE (etype)) == 0)))
6015 {
6016 if (integer_zerop (type_rm_size))
6017 expr = build_int_cst (type, 0);
6018 else
6019 {
6020 tree base_type
6021 = gnat_type_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
6022 type_unsigned_for_rm (type));
6023 tree shift_expr
6024 = convert (base_type,
6025 size_binop (MINUS_EXPR,
6026 TYPE_SIZE (type), type_rm_size));
6027 expr
6028 = convert (type,
6029 build_binary_op (RSHIFT_EXPR, base_type,
6030 build_binary_op (LSHIFT_EXPR, base_type,
6031 convert (base_type,
6032 expr),
6033 shift_expr),
6034 shift_expr));
6035 }
6036 }
6037
6038 /* An unchecked conversion should never raise Constraint_Error. The code
6039 below assumes that GCC's conversion routines overflow the same way that
6040 the underlying hardware does. This is probably true. In the rare case
6041 when it is false, we can rely on the fact that such conversions are
6042 erroneous anyway. */
6043 if (TREE_CODE (expr) == INTEGER_CST)
6044 TREE_OVERFLOW (expr) = 0;
6045
6046 /* If the sizes of the types differ and this is an VIEW_CONVERT_EXPR,
6047 show no longer constant. */
6048 if (TREE_CODE (expr) == VIEW_CONVERT_EXPR
6049 && !operand_equal_p (TYPE_SIZE_UNIT (type), TYPE_SIZE_UNIT (etype),
6050 OEP_ONLY_CONST))
6051 TREE_CONSTANT (expr) = 0;
6052
6053 return expr;
6054 }
6055
6056 /* Return the appropriate GCC tree code for the specified GNAT_TYPE,
6057 the latter being a record type as predicated by Is_Record_Type. */
6058
6059 enum tree_code
6060 tree_code_for_record_type (Entity_Id gnat_type)
6061 {
6062 Node_Id component_list, component;
6063
6064 /* Return UNION_TYPE if it's an Unchecked_Union whose non-discriminant
6065 fields are all in the variant part. Otherwise, return RECORD_TYPE. */
6066 if (!Is_Unchecked_Union (gnat_type))
6067 return RECORD_TYPE;
6068
6069 gnat_type = Implementation_Base_Type (gnat_type);
6070 component_list
6071 = Component_List (Type_Definition (Declaration_Node (gnat_type)));
6072
6073 for (component = First_Non_Pragma (Component_Items (component_list));
6074 Present (component);
6075 component = Next_Non_Pragma (component))
6076 if (Ekind (Defining_Entity (component)) == E_Component)
6077 return RECORD_TYPE;
6078
6079 return UNION_TYPE;
6080 }
6081
6082 /* Return true if GNAT_TYPE is a "double" floating-point type, i.e. whose
6083 size is equal to 64 bits, or an array of such a type. Set ALIGN_CLAUSE
6084 according to the presence of an alignment clause on the type or, if it
6085 is an array, on the component type. */
6086
6087 bool
6088 is_double_float_or_array (Entity_Id gnat_type, bool *align_clause)
6089 {
6090 gnat_type = Underlying_Type (gnat_type);
6091
6092 *align_clause = Present (Alignment_Clause (gnat_type));
6093
6094 if (Is_Array_Type (gnat_type))
6095 {
6096 gnat_type = Underlying_Type (Component_Type (gnat_type));
6097 if (Present (Alignment_Clause (gnat_type)))
6098 *align_clause = true;
6099 }
6100
6101 if (!Is_Floating_Point_Type (gnat_type))
6102 return false;
6103
6104 if (UI_To_Int (Esize (gnat_type)) != 64)
6105 return false;
6106
6107 return true;
6108 }
6109
6110 /* Return true if GNAT_TYPE is a "double" or larger scalar type, i.e. whose
6111 size is greater or equal to 64 bits, or an array of such a type. Set
6112 ALIGN_CLAUSE according to the presence of an alignment clause on the
6113 type or, if it is an array, on the component type. */
6114
6115 bool
6116 is_double_scalar_or_array (Entity_Id gnat_type, bool *align_clause)
6117 {
6118 gnat_type = Underlying_Type (gnat_type);
6119
6120 *align_clause = Present (Alignment_Clause (gnat_type));
6121
6122 if (Is_Array_Type (gnat_type))
6123 {
6124 gnat_type = Underlying_Type (Component_Type (gnat_type));
6125 if (Present (Alignment_Clause (gnat_type)))
6126 *align_clause = true;
6127 }
6128
6129 if (!Is_Scalar_Type (gnat_type))
6130 return false;
6131
6132 if (UI_To_Int (Esize (gnat_type)) < 64)
6133 return false;
6134
6135 return true;
6136 }
6137
6138 /* Return true if GNU_TYPE is suitable as the type of a non-aliased
6139 component of an aggregate type. */
6140
6141 bool
6142 type_for_nonaliased_component_p (tree gnu_type)
6143 {
6144 /* If the type is passed by reference, we may have pointers to the
6145 component so it cannot be made non-aliased. */
6146 if (must_pass_by_ref (gnu_type) || default_pass_by_ref (gnu_type))
6147 return false;
6148
6149 /* We used to say that any component of aggregate type is aliased
6150 because the front-end may take 'Reference of it. The front-end
6151 has been enhanced in the meantime so as to use a renaming instead
6152 in most cases, but the back-end can probably take the address of
6153 such a component too so we go for the conservative stance.
6154
6155 For instance, we might need the address of any array type, even
6156 if normally passed by copy, to construct a fat pointer if the
6157 component is used as an actual for an unconstrained formal.
6158
6159 Likewise for record types: even if a specific record subtype is
6160 passed by copy, the parent type might be passed by ref (e.g. if
6161 it's of variable size) and we might take the address of a child
6162 component to pass to a parent formal. We have no way to check
6163 for such conditions here. */
6164 if (AGGREGATE_TYPE_P (gnu_type))
6165 return false;
6166
6167 return true;
6168 }
6169
6170 /* Return true if TYPE is a smaller form of ORIG_TYPE. */
6171
6172 bool
6173 smaller_form_type_p (tree type, tree orig_type)
6174 {
6175 tree size, osize;
6176
6177 /* We're not interested in variants here. */
6178 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig_type))
6179 return false;
6180
6181 /* Like a variant, a packable version keeps the original TYPE_NAME. */
6182 if (TYPE_NAME (type) != TYPE_NAME (orig_type))
6183 return false;
6184
6185 size = TYPE_SIZE (type);
6186 osize = TYPE_SIZE (orig_type);
6187
6188 if (!(TREE_CODE (size) == INTEGER_CST && TREE_CODE (osize) == INTEGER_CST))
6189 return false;
6190
6191 return tree_int_cst_lt (size, osize) != 0;
6192 }
6193
6194 /* Return whether EXPR, which is the renamed object in an object renaming
6195 declaration, can be materialized as a reference (with a REFERENCE_TYPE).
6196 This should be synchronized with Exp_Dbug.Debug_Renaming_Declaration. */
6197
6198 bool
6199 can_materialize_object_renaming_p (Node_Id expr)
6200 {
6201 while (true)
6202 {
6203 expr = Original_Node (expr);
6204
6205 switch (Nkind (expr))
6206 {
6207 case N_Identifier:
6208 case N_Expanded_Name:
6209 if (!Present (Renamed_Object (Entity (expr))))
6210 return true;
6211 expr = Renamed_Object (Entity (expr));
6212 break;
6213
6214 case N_Selected_Component:
6215 {
6216 if (Is_Packed (Underlying_Type (Etype (Prefix (expr)))))
6217 return false;
6218
6219 const Uint bitpos
6220 = Normalized_First_Bit (Entity (Selector_Name (expr)));
6221 if (bitpos != UI_No_Uint && bitpos != Uint_0)
6222 return false;
6223
6224 expr = Prefix (expr);
6225 break;
6226 }
6227
6228 case N_Indexed_Component:
6229 case N_Slice:
6230 {
6231 const Entity_Id t = Underlying_Type (Etype (Prefix (expr)));
6232
6233 if (Is_Array_Type (t) && Present (Packed_Array_Impl_Type (t)))
6234 return false;
6235
6236 expr = Prefix (expr);
6237 break;
6238 }
6239
6240 case N_Explicit_Dereference:
6241 expr = Prefix (expr);
6242 break;
6243
6244 default:
6245 return true;
6246 };
6247 }
6248 }
6249
6250 /* Perform final processing on global declarations. */
6251
6252 static GTY (()) tree dummy_global;
6253
6254 void
6255 gnat_write_global_declarations (void)
6256 {
6257 unsigned int i;
6258 tree iter;
6259
6260 /* If we have declared types as used at the global level, insert them in
6261 the global hash table. We use a dummy variable for this purpose, but
6262 we need to build it unconditionally to avoid -fcompare-debug issues. */
6263 if (first_global_object_name)
6264 {
6265 struct varpool_node *node;
6266 char *label;
6267
6268 ASM_FORMAT_PRIVATE_NAME (label, first_global_object_name, ULONG_MAX);
6269 dummy_global
6270 = build_decl (BUILTINS_LOCATION, VAR_DECL, get_identifier (label),
6271 void_type_node);
6272 DECL_HARD_REGISTER (dummy_global) = 1;
6273 TREE_STATIC (dummy_global) = 1;
6274 node = varpool_node::get_create (dummy_global);
6275 node->definition = 1;
6276 node->force_output = 1;
6277
6278 if (types_used_by_cur_var_decl)
6279 while (!types_used_by_cur_var_decl->is_empty ())
6280 {
6281 tree t = types_used_by_cur_var_decl->pop ();
6282 types_used_by_var_decl_insert (t, dummy_global);
6283 }
6284 }
6285
6286 /* First output the integral global variables, so that they can be referenced
6287 as bounds by the global dynamic types. Skip external variables, unless we
6288 really need to emit debug info for them:, e.g. imported variables. */
6289 FOR_EACH_VEC_SAFE_ELT (global_decls, i, iter)
6290 if (TREE_CODE (iter) == VAR_DECL
6291 && INTEGRAL_TYPE_P (TREE_TYPE (iter))
6292 && (!DECL_EXTERNAL (iter) || !DECL_IGNORED_P (iter)))
6293 rest_of_decl_compilation (iter, true, 0);
6294
6295 /* Now output debug information for the global type declarations. This
6296 ensures that global types whose compilation hasn't been finalized yet,
6297 for example pointers to Taft amendment types, have their compilation
6298 finalized in the right context. */
6299 FOR_EACH_VEC_SAFE_ELT (global_decls, i, iter)
6300 if (TREE_CODE (iter) == TYPE_DECL && !DECL_IGNORED_P (iter))
6301 debug_hooks->type_decl (iter, false);
6302
6303 /* Then output the other global variables. We need to do that after the
6304 information for global types is emitted so that they are finalized. */
6305 FOR_EACH_VEC_SAFE_ELT (global_decls, i, iter)
6306 if (TREE_CODE (iter) == VAR_DECL
6307 && !INTEGRAL_TYPE_P (TREE_TYPE (iter))
6308 && (!DECL_EXTERNAL (iter) || !DECL_IGNORED_P (iter)))
6309 rest_of_decl_compilation (iter, true, 0);
6310
6311 /* Output debug information for the global constants. */
6312 FOR_EACH_VEC_SAFE_ELT (global_decls, i, iter)
6313 if (TREE_CODE (iter) == CONST_DECL && !DECL_IGNORED_P (iter))
6314 debug_hooks->early_global_decl (iter);
6315
6316 /* Output it for the imported functions. */
6317 FOR_EACH_VEC_SAFE_ELT (global_decls, i, iter)
6318 if (TREE_CODE (iter) == FUNCTION_DECL
6319 && DECL_EXTERNAL (iter)
6320 && DECL_INITIAL (iter) == NULL
6321 && !DECL_IGNORED_P (iter)
6322 && DECL_FUNCTION_IS_DEF (iter))
6323 debug_hooks->early_global_decl (iter);
6324
6325 /* Output it for the imported modules/declarations. In GNAT, these are only
6326 materializing subprogram. */
6327 FOR_EACH_VEC_SAFE_ELT (global_decls, i, iter)
6328 if (TREE_CODE (iter) == IMPORTED_DECL && !DECL_IGNORED_P (iter))
6329 debug_hooks->imported_module_or_decl (iter, DECL_NAME (iter),
6330 DECL_CONTEXT (iter), false, false);
6331 }
6332
6333 /* ************************************************************************
6334 * * GCC builtins support *
6335 * ************************************************************************ */
6336
6337 /* The general scheme is fairly simple:
6338
6339 For each builtin function/type to be declared, gnat_install_builtins calls
6340 internal facilities which eventually get to gnat_pushdecl, which in turn
6341 tracks the so declared builtin function decls in the 'builtin_decls' global
6342 datastructure. When an Intrinsic subprogram declaration is processed, we
6343 search this global datastructure to retrieve the associated BUILT_IN DECL
6344 node. */
6345
6346 /* Search the chain of currently available builtin declarations for a node
6347 corresponding to function NAME (an IDENTIFIER_NODE). Return the first node
6348 found, if any, or NULL_TREE otherwise. */
6349 tree
6350 builtin_decl_for (tree name)
6351 {
6352 unsigned i;
6353 tree decl;
6354
6355 FOR_EACH_VEC_SAFE_ELT (builtin_decls, i, decl)
6356 if (DECL_NAME (decl) == name)
6357 return decl;
6358
6359 return NULL_TREE;
6360 }
6361
6362 /* The code below eventually exposes gnat_install_builtins, which declares
6363 the builtin types and functions we might need, either internally or as
6364 user accessible facilities.
6365
6366 ??? This is a first implementation shot, still in rough shape. It is
6367 heavily inspired from the "C" family implementation, with chunks copied
6368 verbatim from there.
6369
6370 Two obvious improvement candidates are:
6371 o Use a more efficient name/decl mapping scheme
6372 o Devise a middle-end infrastructure to avoid having to copy
6373 pieces between front-ends. */
6374
6375 /* ----------------------------------------------------------------------- *
6376 * BUILTIN ELEMENTARY TYPES *
6377 * ----------------------------------------------------------------------- */
6378
6379 /* Standard data types to be used in builtin argument declarations. */
6380
6381 enum c_tree_index
6382 {
6383 CTI_SIGNED_SIZE_TYPE, /* For format checking only. */
6384 CTI_STRING_TYPE,
6385 CTI_CONST_STRING_TYPE,
6386
6387 CTI_MAX
6388 };
6389
6390 static tree c_global_trees[CTI_MAX];
6391
6392 #define signed_size_type_node c_global_trees[CTI_SIGNED_SIZE_TYPE]
6393 #define string_type_node c_global_trees[CTI_STRING_TYPE]
6394 #define const_string_type_node c_global_trees[CTI_CONST_STRING_TYPE]
6395
6396 /* ??? In addition some attribute handlers, we currently don't support a
6397 (small) number of builtin-types, which in turns inhibits support for a
6398 number of builtin functions. */
6399 #define wint_type_node void_type_node
6400 #define intmax_type_node void_type_node
6401 #define uintmax_type_node void_type_node
6402
6403 /* Used to help initialize the builtin-types.def table. When a type of
6404 the correct size doesn't exist, use error_mark_node instead of NULL.
6405 The later results in segfaults even when a decl using the type doesn't
6406 get invoked. */
6407
6408 static tree
6409 builtin_type_for_size (int size, bool unsignedp)
6410 {
6411 tree type = gnat_type_for_size (size, unsignedp);
6412 return type ? type : error_mark_node;
6413 }
6414
6415 /* Build/push the elementary type decls that builtin functions/types
6416 will need. */
6417
6418 static void
6419 install_builtin_elementary_types (void)
6420 {
6421 signed_size_type_node = gnat_signed_type_for (size_type_node);
6422 pid_type_node = integer_type_node;
6423
6424 string_type_node = build_pointer_type (char_type_node);
6425 const_string_type_node
6426 = build_pointer_type (build_qualified_type
6427 (char_type_node, TYPE_QUAL_CONST));
6428 }
6429
6430 /* ----------------------------------------------------------------------- *
6431 * BUILTIN FUNCTION TYPES *
6432 * ----------------------------------------------------------------------- */
6433
6434 /* Now, builtin function types per se. */
6435
6436 enum c_builtin_type
6437 {
6438 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
6439 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
6440 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
6441 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
6442 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
6443 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
6444 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
6445 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
6446 ARG6) NAME,
6447 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
6448 ARG6, ARG7) NAME,
6449 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
6450 ARG6, ARG7, ARG8) NAME,
6451 #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
6452 ARG6, ARG7, ARG8, ARG9) NAME,
6453 #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
6454 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
6455 #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
6456 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
6457 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
6458 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
6459 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
6460 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
6461 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
6462 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
6463 NAME,
6464 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
6465 ARG6) NAME,
6466 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
6467 ARG6, ARG7) NAME,
6468 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
6469 #include "builtin-types.def"
6470 #include "ada-builtin-types.def"
6471 #undef DEF_PRIMITIVE_TYPE
6472 #undef DEF_FUNCTION_TYPE_0
6473 #undef DEF_FUNCTION_TYPE_1
6474 #undef DEF_FUNCTION_TYPE_2
6475 #undef DEF_FUNCTION_TYPE_3
6476 #undef DEF_FUNCTION_TYPE_4
6477 #undef DEF_FUNCTION_TYPE_5
6478 #undef DEF_FUNCTION_TYPE_6
6479 #undef DEF_FUNCTION_TYPE_7
6480 #undef DEF_FUNCTION_TYPE_8
6481 #undef DEF_FUNCTION_TYPE_9
6482 #undef DEF_FUNCTION_TYPE_10
6483 #undef DEF_FUNCTION_TYPE_11
6484 #undef DEF_FUNCTION_TYPE_VAR_0
6485 #undef DEF_FUNCTION_TYPE_VAR_1
6486 #undef DEF_FUNCTION_TYPE_VAR_2
6487 #undef DEF_FUNCTION_TYPE_VAR_3
6488 #undef DEF_FUNCTION_TYPE_VAR_4
6489 #undef DEF_FUNCTION_TYPE_VAR_5
6490 #undef DEF_FUNCTION_TYPE_VAR_6
6491 #undef DEF_FUNCTION_TYPE_VAR_7
6492 #undef DEF_POINTER_TYPE
6493 BT_LAST
6494 };
6495
6496 typedef enum c_builtin_type builtin_type;
6497
6498 /* A temporary array used in communication with def_fn_type. */
6499 static GTY(()) tree builtin_types[(int) BT_LAST + 1];
6500
6501 /* A helper function for install_builtin_types. Build function type
6502 for DEF with return type RET and N arguments. If VAR is true, then the
6503 function should be variadic after those N arguments.
6504
6505 Takes special care not to ICE if any of the types involved are
6506 error_mark_node, which indicates that said type is not in fact available
6507 (see builtin_type_for_size). In which case the function type as a whole
6508 should be error_mark_node. */
6509
6510 static void
6511 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
6512 {
6513 tree t;
6514 tree *args = XALLOCAVEC (tree, n);
6515 va_list list;
6516 int i;
6517
6518 va_start (list, n);
6519 for (i = 0; i < n; ++i)
6520 {
6521 builtin_type a = (builtin_type) va_arg (list, int);
6522 t = builtin_types[a];
6523 if (t == error_mark_node)
6524 goto egress;
6525 args[i] = t;
6526 }
6527
6528 t = builtin_types[ret];
6529 if (t == error_mark_node)
6530 goto egress;
6531 if (var)
6532 t = build_varargs_function_type_array (t, n, args);
6533 else
6534 t = build_function_type_array (t, n, args);
6535
6536 egress:
6537 builtin_types[def] = t;
6538 va_end (list);
6539 }
6540
6541 /* Build the builtin function types and install them in the builtin_types
6542 array for later use in builtin function decls. */
6543
6544 static void
6545 install_builtin_function_types (void)
6546 {
6547 tree va_list_ref_type_node;
6548 tree va_list_arg_type_node;
6549
6550 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
6551 {
6552 va_list_arg_type_node = va_list_ref_type_node =
6553 build_pointer_type (TREE_TYPE (va_list_type_node));
6554 }
6555 else
6556 {
6557 va_list_arg_type_node = va_list_type_node;
6558 va_list_ref_type_node = build_reference_type (va_list_type_node);
6559 }
6560
6561 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
6562 builtin_types[ENUM] = VALUE;
6563 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
6564 def_fn_type (ENUM, RETURN, 0, 0);
6565 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
6566 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
6567 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
6568 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
6569 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
6570 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
6571 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
6572 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
6573 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
6574 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
6575 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
6576 ARG6) \
6577 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
6578 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
6579 ARG6, ARG7) \
6580 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
6581 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
6582 ARG6, ARG7, ARG8) \
6583 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
6584 ARG7, ARG8);
6585 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
6586 ARG6, ARG7, ARG8, ARG9) \
6587 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
6588 ARG7, ARG8, ARG9);
6589 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5,\
6590 ARG6, ARG7, ARG8, ARG9, ARG10) \
6591 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
6592 ARG7, ARG8, ARG9, ARG10);
6593 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5,\
6594 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
6595 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
6596 ARG7, ARG8, ARG9, ARG10, ARG11);
6597 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
6598 def_fn_type (ENUM, RETURN, 1, 0);
6599 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
6600 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
6601 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
6602 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
6603 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
6604 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
6605 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
6606 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
6607 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
6608 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
6609 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
6610 ARG6) \
6611 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
6612 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
6613 ARG6, ARG7) \
6614 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
6615 #define DEF_POINTER_TYPE(ENUM, TYPE) \
6616 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
6617
6618 #include "builtin-types.def"
6619 #include "ada-builtin-types.def"
6620
6621 #undef DEF_PRIMITIVE_TYPE
6622 #undef DEF_FUNCTION_TYPE_0
6623 #undef DEF_FUNCTION_TYPE_1
6624 #undef DEF_FUNCTION_TYPE_2
6625 #undef DEF_FUNCTION_TYPE_3
6626 #undef DEF_FUNCTION_TYPE_4
6627 #undef DEF_FUNCTION_TYPE_5
6628 #undef DEF_FUNCTION_TYPE_6
6629 #undef DEF_FUNCTION_TYPE_7
6630 #undef DEF_FUNCTION_TYPE_8
6631 #undef DEF_FUNCTION_TYPE_9
6632 #undef DEF_FUNCTION_TYPE_10
6633 #undef DEF_FUNCTION_TYPE_11
6634 #undef DEF_FUNCTION_TYPE_VAR_0
6635 #undef DEF_FUNCTION_TYPE_VAR_1
6636 #undef DEF_FUNCTION_TYPE_VAR_2
6637 #undef DEF_FUNCTION_TYPE_VAR_3
6638 #undef DEF_FUNCTION_TYPE_VAR_4
6639 #undef DEF_FUNCTION_TYPE_VAR_5
6640 #undef DEF_FUNCTION_TYPE_VAR_6
6641 #undef DEF_FUNCTION_TYPE_VAR_7
6642 #undef DEF_POINTER_TYPE
6643 builtin_types[(int) BT_LAST] = NULL_TREE;
6644 }
6645
6646 /* ----------------------------------------------------------------------- *
6647 * BUILTIN ATTRIBUTES *
6648 * ----------------------------------------------------------------------- */
6649
6650 enum built_in_attribute
6651 {
6652 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
6653 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
6654 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
6655 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
6656 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
6657 #include "builtin-attrs.def"
6658 #undef DEF_ATTR_NULL_TREE
6659 #undef DEF_ATTR_INT
6660 #undef DEF_ATTR_STRING
6661 #undef DEF_ATTR_IDENT
6662 #undef DEF_ATTR_TREE_LIST
6663 ATTR_LAST
6664 };
6665
6666 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
6667
6668 static void
6669 install_builtin_attributes (void)
6670 {
6671 /* Fill in the built_in_attributes array. */
6672 #define DEF_ATTR_NULL_TREE(ENUM) \
6673 built_in_attributes[(int) ENUM] = NULL_TREE;
6674 #define DEF_ATTR_INT(ENUM, VALUE) \
6675 built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE);
6676 #define DEF_ATTR_STRING(ENUM, VALUE) \
6677 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6678 #define DEF_ATTR_IDENT(ENUM, STRING) \
6679 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6680 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6681 built_in_attributes[(int) ENUM] \
6682 = tree_cons (built_in_attributes[(int) PURPOSE], \
6683 built_in_attributes[(int) VALUE], \
6684 built_in_attributes[(int) CHAIN]);
6685 #include "builtin-attrs.def"
6686 #undef DEF_ATTR_NULL_TREE
6687 #undef DEF_ATTR_INT
6688 #undef DEF_ATTR_STRING
6689 #undef DEF_ATTR_IDENT
6690 #undef DEF_ATTR_TREE_LIST
6691 }
6692
6693 /* Handle a "const" attribute; arguments as in
6694 struct attribute_spec.handler. */
6695
6696 static tree
6697 handle_const_attribute (tree *node, tree ARG_UNUSED (name),
6698 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
6699 bool *no_add_attrs)
6700 {
6701 if (TREE_CODE (*node) == FUNCTION_DECL)
6702 TREE_READONLY (*node) = 1;
6703 else
6704 *no_add_attrs = true;
6705
6706 return NULL_TREE;
6707 }
6708
6709 /* Handle a "nothrow" attribute; arguments as in
6710 struct attribute_spec.handler. */
6711
6712 static tree
6713 handle_nothrow_attribute (tree *node, tree ARG_UNUSED (name),
6714 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
6715 bool *no_add_attrs)
6716 {
6717 if (TREE_CODE (*node) == FUNCTION_DECL)
6718 TREE_NOTHROW (*node) = 1;
6719 else
6720 *no_add_attrs = true;
6721
6722 return NULL_TREE;
6723 }
6724
6725 /* Handle a "expected_throw" attribute; arguments as in
6726 struct attribute_spec.handler. */
6727
6728 static tree
6729 handle_expected_throw_attribute (tree *node, tree ARG_UNUSED (name),
6730 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
6731 bool *no_add_attrs)
6732 {
6733 if (TREE_CODE (*node) == FUNCTION_DECL)
6734 /* No flag to set here. */;
6735 else
6736 *no_add_attrs = true;
6737
6738 return NULL_TREE;
6739 }
6740
6741 /* Handle a "pure" attribute; arguments as in
6742 struct attribute_spec.handler. */
6743
6744 static tree
6745 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6746 int ARG_UNUSED (flags), bool *no_add_attrs)
6747 {
6748 if (TREE_CODE (*node) == FUNCTION_DECL)
6749 DECL_PURE_P (*node) = 1;
6750 /* TODO: support types. */
6751 else
6752 {
6753 warning (OPT_Wattributes, "%qs attribute ignored",
6754 IDENTIFIER_POINTER (name));
6755 *no_add_attrs = true;
6756 }
6757
6758 return NULL_TREE;
6759 }
6760
6761 /* Handle a "no vops" attribute; arguments as in
6762 struct attribute_spec.handler. */
6763
6764 static tree
6765 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
6766 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
6767 bool *ARG_UNUSED (no_add_attrs))
6768 {
6769 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
6770 DECL_IS_NOVOPS (*node) = 1;
6771 return NULL_TREE;
6772 }
6773
6774 /* Helper for nonnull attribute handling; fetch the operand number
6775 from the attribute argument list. */
6776
6777 static bool
6778 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
6779 {
6780 /* Verify the arg number is a constant. */
6781 if (!tree_fits_uhwi_p (arg_num_expr))
6782 return false;
6783
6784 *valp = TREE_INT_CST_LOW (arg_num_expr);
6785 return true;
6786 }
6787
6788 /* Handle the "nonnull" attribute. */
6789 static tree
6790 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
6791 tree args, int ARG_UNUSED (flags),
6792 bool *no_add_attrs)
6793 {
6794 tree type = *node;
6795 unsigned HOST_WIDE_INT attr_arg_num;
6796
6797 /* If no arguments are specified, all pointer arguments should be
6798 non-null. Verify a full prototype is given so that the arguments
6799 will have the correct types when we actually check them later.
6800 Avoid diagnosing type-generic built-ins since those have no
6801 prototype. */
6802 if (!args)
6803 {
6804 if (!prototype_p (type)
6805 && (!TYPE_ATTRIBUTES (type)
6806 || !lookup_attribute ("type generic", TYPE_ATTRIBUTES (type))))
6807 {
6808 error ("%qs attribute without arguments on a non-prototype",
6809 "nonnull");
6810 *no_add_attrs = true;
6811 }
6812 return NULL_TREE;
6813 }
6814
6815 /* Argument list specified. Verify that each argument number references
6816 a pointer argument. */
6817 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
6818 {
6819 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
6820
6821 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
6822 {
6823 error ("%qs argument has invalid operand number (argument %lu)",
6824 "nonnull", (unsigned long) attr_arg_num);
6825 *no_add_attrs = true;
6826 return NULL_TREE;
6827 }
6828
6829 if (prototype_p (type))
6830 {
6831 function_args_iterator iter;
6832 tree argument;
6833
6834 function_args_iter_init (&iter, type);
6835 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
6836 {
6837 argument = function_args_iter_cond (&iter);
6838 if (!argument || ck_num == arg_num)
6839 break;
6840 }
6841
6842 if (!argument
6843 || TREE_CODE (argument) == VOID_TYPE)
6844 {
6845 error ("%qs argument with out-of-range operand number "
6846 "(argument %lu, operand %lu)", "nonnull",
6847 (unsigned long) attr_arg_num, (unsigned long) arg_num);
6848 *no_add_attrs = true;
6849 return NULL_TREE;
6850 }
6851
6852 if (TREE_CODE (argument) != POINTER_TYPE)
6853 {
6854 error ("%qs argument references non-pointer operand "
6855 "(argument %lu, operand %lu)", "nonnull",
6856 (unsigned long) attr_arg_num, (unsigned long) arg_num);
6857 *no_add_attrs = true;
6858 return NULL_TREE;
6859 }
6860 }
6861 }
6862
6863 return NULL_TREE;
6864 }
6865
6866 /* Handle a "sentinel" attribute. */
6867
6868 static tree
6869 handle_sentinel_attribute (tree *node, tree name, tree args,
6870 int ARG_UNUSED (flags), bool *no_add_attrs)
6871 {
6872 if (!prototype_p (*node))
6873 {
6874 warning (OPT_Wattributes,
6875 "%qs attribute requires prototypes with named arguments",
6876 IDENTIFIER_POINTER (name));
6877 *no_add_attrs = true;
6878 }
6879 else
6880 {
6881 if (!stdarg_p (*node))
6882 {
6883 warning (OPT_Wattributes,
6884 "%qs attribute only applies to variadic functions",
6885 IDENTIFIER_POINTER (name));
6886 *no_add_attrs = true;
6887 }
6888 }
6889
6890 if (args)
6891 {
6892 tree position = TREE_VALUE (args);
6893
6894 if (TREE_CODE (position) != INTEGER_CST)
6895 {
6896 warning (0, "requested position is not an integer constant");
6897 *no_add_attrs = true;
6898 }
6899 else
6900 {
6901 if (tree_int_cst_lt (position, integer_zero_node))
6902 {
6903 warning (0, "requested position is less than zero");
6904 *no_add_attrs = true;
6905 }
6906 }
6907 }
6908
6909 return NULL_TREE;
6910 }
6911
6912 /* Handle a "noreturn" attribute; arguments as in
6913 struct attribute_spec.handler. */
6914
6915 static tree
6916 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6917 int ARG_UNUSED (flags), bool *no_add_attrs)
6918 {
6919 tree type = TREE_TYPE (*node);
6920
6921 /* See FIXME comment in c_common_attribute_table. */
6922 if (TREE_CODE (*node) == FUNCTION_DECL)
6923 TREE_THIS_VOLATILE (*node) = 1;
6924 else if (TREE_CODE (type) == POINTER_TYPE
6925 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6926 TREE_TYPE (*node)
6927 = build_pointer_type
6928 (change_qualified_type (TREE_TYPE (type), TYPE_QUAL_VOLATILE));
6929 else
6930 {
6931 warning (OPT_Wattributes, "%qs attribute ignored",
6932 IDENTIFIER_POINTER (name));
6933 *no_add_attrs = true;
6934 }
6935
6936 return NULL_TREE;
6937 }
6938
6939 /* Handle a "stack_protect" attribute; arguments as in
6940 struct attribute_spec.handler. */
6941
6942 static tree
6943 handle_stack_protect_attribute (tree *node, tree name, tree, int,
6944 bool *no_add_attrs)
6945 {
6946 if (TREE_CODE (*node) != FUNCTION_DECL)
6947 {
6948 warning (OPT_Wattributes, "%qE attribute ignored", name);
6949 *no_add_attrs = true;
6950 }
6951
6952 return NULL_TREE;
6953 }
6954
6955 /* Handle a "no_stack_protector" attribute; arguments as in
6956 struct attribute_spec.handler. */
6957
6958 static tree
6959 handle_no_stack_protector_attribute (tree *node, tree name, tree, int,
6960 bool *no_add_attrs)
6961 {
6962 if (TREE_CODE (*node) != FUNCTION_DECL)
6963 {
6964 warning (OPT_Wattributes, "%qE attribute ignored", name);
6965 *no_add_attrs = true;
6966 }
6967
6968 return NULL_TREE;
6969 }
6970
6971 /* Handle a "strub" attribute; arguments as in
6972 struct attribute_spec.handler. */
6973
6974 static tree
6975 handle_strub_attribute (tree *node, tree name,
6976 tree args,
6977 int ARG_UNUSED (flags), bool *no_add_attrs)
6978 {
6979 bool enable = true;
6980
6981 if (args && FUNCTION_POINTER_TYPE_P (*node))
6982 *node = TREE_TYPE (*node);
6983
6984 if (args && FUNC_OR_METHOD_TYPE_P (*node))
6985 {
6986 switch (strub_validate_fn_attr_parm (TREE_VALUE (args)))
6987 {
6988 case 1:
6989 case 2:
6990 enable = true;
6991 break;
6992
6993 case 0:
6994 warning (OPT_Wattributes,
6995 "%qE attribute ignored because of argument %qE",
6996 name, TREE_VALUE (args));
6997 *no_add_attrs = true;
6998 enable = false;
6999 break;
7000
7001 case -1:
7002 case -2:
7003 enable = false;
7004 break;
7005
7006 default:
7007 gcc_unreachable ();
7008 }
7009
7010 args = TREE_CHAIN (args);
7011 }
7012
7013 if (args)
7014 {
7015 warning (OPT_Wattributes,
7016 "ignoring attribute %qE because of excess arguments"
7017 " starting at %qE",
7018 name, TREE_VALUE (args));
7019 *no_add_attrs = true;
7020 enable = false;
7021 }
7022
7023 /* Warn about unmet expectations that the strub attribute works like a
7024 qualifier. ??? Could/should we extend it to the element/field types
7025 here? */
7026 if (TREE_CODE (*node) == ARRAY_TYPE
7027 || VECTOR_TYPE_P (*node)
7028 || TREE_CODE (*node) == COMPLEX_TYPE)
7029 warning (OPT_Wattributes,
7030 "attribute %qE does not apply to elements"
7031 " of non-scalar type %qT",
7032 name, *node);
7033 else if (RECORD_OR_UNION_TYPE_P (*node))
7034 warning (OPT_Wattributes,
7035 "attribute %qE does not apply to fields"
7036 " of aggregate type %qT",
7037 name, *node);
7038
7039 /* If we see a strub-enabling attribute, and we're at the default setting,
7040 implicitly or explicitly, note that the attribute was seen, so that we can
7041 reduce the compile-time overhead to nearly zero when the strub feature is
7042 not used. */
7043 if (enable && flag_strub < -2)
7044 flag_strub += 2;
7045
7046 return NULL_TREE;
7047 }
7048
7049 /* Handle a "noinline" attribute; arguments as in
7050 struct attribute_spec.handler. */
7051
7052 static tree
7053 handle_noinline_attribute (tree *node, tree name,
7054 tree ARG_UNUSED (args),
7055 int ARG_UNUSED (flags), bool *no_add_attrs)
7056 {
7057 if (TREE_CODE (*node) == FUNCTION_DECL)
7058 DECL_UNINLINABLE (*node) = 1;
7059 else
7060 {
7061 warning (OPT_Wattributes, "%qE attribute ignored", name);
7062 *no_add_attrs = true;
7063 }
7064
7065 return NULL_TREE;
7066 }
7067
7068 /* Handle a "noclone" attribute; arguments as in
7069 struct attribute_spec.handler. */
7070
7071 static tree
7072 handle_noclone_attribute (tree *node, tree name,
7073 tree ARG_UNUSED (args),
7074 int ARG_UNUSED (flags), bool *no_add_attrs)
7075 {
7076 if (TREE_CODE (*node) != FUNCTION_DECL)
7077 {
7078 warning (OPT_Wattributes, "%qE attribute ignored", name);
7079 *no_add_attrs = true;
7080 }
7081
7082 return NULL_TREE;
7083 }
7084
7085 /* Handle a "no_icf" attribute; arguments as in
7086 struct attribute_spec.handler. */
7087
7088 static tree
7089 handle_noicf_attribute (tree *node, tree name,
7090 tree ARG_UNUSED (args),
7091 int ARG_UNUSED (flags), bool *no_add_attrs)
7092 {
7093 if (TREE_CODE (*node) != FUNCTION_DECL)
7094 {
7095 warning (OPT_Wattributes, "%qE attribute ignored", name);
7096 *no_add_attrs = true;
7097 }
7098
7099 return NULL_TREE;
7100 }
7101
7102 /* Handle a "noipa" attribute; arguments as in
7103 struct attribute_spec.handler. */
7104
7105 static tree
7106 handle_noipa_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
7107 {
7108 if (TREE_CODE (*node) != FUNCTION_DECL)
7109 {
7110 warning (OPT_Wattributes, "%qE attribute ignored", name);
7111 *no_add_attrs = true;
7112 }
7113
7114 return NULL_TREE;
7115 }
7116
7117 /* Handle a "leaf" attribute; arguments as in
7118 struct attribute_spec.handler. */
7119
7120 static tree
7121 handle_leaf_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7122 int ARG_UNUSED (flags), bool *no_add_attrs)
7123 {
7124 if (TREE_CODE (*node) != FUNCTION_DECL)
7125 {
7126 warning (OPT_Wattributes, "%qE attribute ignored", name);
7127 *no_add_attrs = true;
7128 }
7129 if (!TREE_PUBLIC (*node))
7130 {
7131 warning (OPT_Wattributes, "%qE attribute has no effect", name);
7132 *no_add_attrs = true;
7133 }
7134
7135 return NULL_TREE;
7136 }
7137
7138 /* Handle a "always_inline" attribute; arguments as in
7139 struct attribute_spec.handler. */
7140
7141 static tree
7142 handle_always_inline_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7143 int ARG_UNUSED (flags), bool *no_add_attrs)
7144 {
7145 if (TREE_CODE (*node) == FUNCTION_DECL)
7146 {
7147 /* Set the attribute and mark it for disregarding inline limits. */
7148 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
7149 }
7150 else
7151 {
7152 warning (OPT_Wattributes, "%qE attribute ignored", name);
7153 *no_add_attrs = true;
7154 }
7155
7156 return NULL_TREE;
7157 }
7158
7159 /* Handle a "malloc" attribute; arguments as in
7160 struct attribute_spec.handler. */
7161
7162 static tree
7163 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7164 int ARG_UNUSED (flags), bool *no_add_attrs)
7165 {
7166 if (TREE_CODE (*node) == FUNCTION_DECL
7167 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
7168 DECL_IS_MALLOC (*node) = 1;
7169 else
7170 {
7171 warning (OPT_Wattributes, "%qs attribute ignored",
7172 IDENTIFIER_POINTER (name));
7173 *no_add_attrs = true;
7174 }
7175
7176 return NULL_TREE;
7177 }
7178
7179 /* Fake handler for attributes we don't properly support. */
7180
7181 tree
7182 fake_attribute_handler (tree * ARG_UNUSED (node),
7183 tree ARG_UNUSED (name),
7184 tree ARG_UNUSED (args),
7185 int ARG_UNUSED (flags),
7186 bool * ARG_UNUSED (no_add_attrs))
7187 {
7188 return NULL_TREE;
7189 }
7190
7191 /* Handle a "type_generic" attribute. */
7192
7193 static tree
7194 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
7195 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
7196 bool * ARG_UNUSED (no_add_attrs))
7197 {
7198 /* Ensure we have a function type. */
7199 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
7200
7201 /* Ensure we have a variadic function. */
7202 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
7203
7204 return NULL_TREE;
7205 }
7206
7207 /* Handle a "flatten" attribute; arguments as in
7208 struct attribute_spec.handler. */
7209
7210 static tree
7211 handle_flatten_attribute (tree *node, tree name,
7212 tree args ATTRIBUTE_UNUSED,
7213 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
7214 {
7215 if (TREE_CODE (*node) == FUNCTION_DECL)
7216 /* Do nothing else, just set the attribute. We'll get at
7217 it later with lookup_attribute. */
7218 ;
7219 else
7220 {
7221 warning (OPT_Wattributes, "%qE attribute ignored", name);
7222 *no_add_attrs = true;
7223 }
7224
7225 return NULL_TREE;
7226 }
7227
7228 /* Handle a "used" attribute; arguments as in
7229 struct attribute_spec.handler. */
7230
7231 static tree
7232 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7233 int ARG_UNUSED (flags), bool *no_add_attrs)
7234 {
7235 tree node = *pnode;
7236
7237 if (TREE_CODE (node) == FUNCTION_DECL
7238 || (VAR_P (node) && TREE_STATIC (node))
7239 || (TREE_CODE (node) == TYPE_DECL))
7240 {
7241 TREE_USED (node) = 1;
7242 DECL_PRESERVE_P (node) = 1;
7243 if (VAR_P (node))
7244 DECL_READ_P (node) = 1;
7245 }
7246 else
7247 {
7248 warning (OPT_Wattributes, "%qE attribute ignored", name);
7249 *no_add_attrs = true;
7250 }
7251
7252 return NULL_TREE;
7253 }
7254
7255 /* Handle an "uninitialized" attribute; arguments as in
7256 struct attribute_spec.handler. */
7257
7258 static tree
7259 handle_uninitialized_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7260 int ARG_UNUSED (flags), bool *no_add_attrs)
7261 {
7262 tree decl = *node;
7263 if (!VAR_P (decl))
7264 {
7265 warning (OPT_Wattributes, "%qE attribute ignored because %qD "
7266 "is not a variable", name, decl);
7267 *no_add_attrs = true;
7268 }
7269 else if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
7270 {
7271 warning (OPT_Wattributes, "%qE attribute ignored because %qD "
7272 "is not a local variable", name, decl);
7273 *no_add_attrs = true;
7274 }
7275
7276 return NULL_TREE;
7277 }
7278
7279 /* Handle a "cold" and attribute; arguments as in
7280 struct attribute_spec.handler. */
7281
7282 static tree
7283 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7284 int ARG_UNUSED (flags), bool *no_add_attrs)
7285 {
7286 if (TREE_CODE (*node) == FUNCTION_DECL
7287 || TREE_CODE (*node) == LABEL_DECL)
7288 {
7289 /* Attribute cold processing is done later with lookup_attribute. */
7290 }
7291 else
7292 {
7293 warning (OPT_Wattributes, "%qE attribute ignored", name);
7294 *no_add_attrs = true;
7295 }
7296
7297 return NULL_TREE;
7298 }
7299
7300 /* Handle a "hot" and attribute; arguments as in
7301 struct attribute_spec.handler. */
7302
7303 static tree
7304 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7305 int ARG_UNUSED (flags), bool *no_add_attrs)
7306 {
7307 if (TREE_CODE (*node) == FUNCTION_DECL
7308 || TREE_CODE (*node) == LABEL_DECL)
7309 {
7310 /* Attribute hot processing is done later with lookup_attribute. */
7311 }
7312 else
7313 {
7314 warning (OPT_Wattributes, "%qE attribute ignored", name);
7315 *no_add_attrs = true;
7316 }
7317
7318 return NULL_TREE;
7319 }
7320
7321 /* Handle a "simd" attribute. */
7322
7323 static tree
7324 handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
7325 {
7326 if (TREE_CODE (*node) == FUNCTION_DECL)
7327 {
7328 tree t = get_identifier ("omp declare simd");
7329 tree attr = NULL_TREE;
7330 if (args)
7331 {
7332 tree id = TREE_VALUE (args);
7333
7334 if (TREE_CODE (id) != STRING_CST)
7335 {
7336 error ("attribute %qE argument not a string", name);
7337 *no_add_attrs = true;
7338 return NULL_TREE;
7339 }
7340
7341 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
7342 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
7343 OMP_CLAUSE_NOTINBRANCH);
7344 else if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
7345 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
7346 OMP_CLAUSE_INBRANCH);
7347 else
7348 {
7349 error ("only %<inbranch%> and %<notinbranch%> flags are "
7350 "allowed for %<__simd__%> attribute");
7351 *no_add_attrs = true;
7352 return NULL_TREE;
7353 }
7354 }
7355
7356 DECL_ATTRIBUTES (*node)
7357 = tree_cons (t, build_tree_list (NULL_TREE, attr),
7358 DECL_ATTRIBUTES (*node));
7359 }
7360 else
7361 {
7362 warning (OPT_Wattributes, "%qE attribute ignored", name);
7363 *no_add_attrs = true;
7364 }
7365
7366 return NULL_TREE;
7367 }
7368
7369 /* Handle a "target" attribute. */
7370
7371 static tree
7372 handle_target_attribute (tree *node, tree name, tree args, int flags,
7373 bool *no_add_attrs)
7374 {
7375 /* Ensure we have a function type. */
7376 if (TREE_CODE (*node) != FUNCTION_DECL)
7377 {
7378 warning (OPT_Wattributes, "%qE attribute ignored", name);
7379 *no_add_attrs = true;
7380 }
7381 else if (!targetm.target_option.valid_attribute_p (*node, name, args, flags))
7382 *no_add_attrs = true;
7383
7384 /* Check that there's no empty string in values of the attribute. */
7385 for (tree t = args; t != NULL_TREE; t = TREE_CHAIN (t))
7386 {
7387 tree value = TREE_VALUE (t);
7388 if (TREE_CODE (value) == STRING_CST
7389 && TREE_STRING_LENGTH (value) == 1
7390 && TREE_STRING_POINTER (value)[0] == '\0')
7391 {
7392 warning (OPT_Wattributes, "empty string in attribute %<target%>");
7393 *no_add_attrs = true;
7394 }
7395 }
7396
7397 return NULL_TREE;
7398 }
7399
7400 /* Handle a "target_clones" attribute. */
7401
7402 static tree
7403 handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7404 int ARG_UNUSED (flags), bool *no_add_attrs)
7405 {
7406 /* Ensure we have a function type. */
7407 if (TREE_CODE (*node) == FUNCTION_DECL)
7408 /* Do not inline functions with multiple clone targets. */
7409 DECL_UNINLINABLE (*node) = 1;
7410 else
7411 {
7412 warning (OPT_Wattributes, "%qE attribute ignored", name);
7413 *no_add_attrs = true;
7414 }
7415 return NULL_TREE;
7416 }
7417
7418 /* Handle a "vector_size" attribute; arguments as in
7419 struct attribute_spec.handler. */
7420
7421 static tree
7422 handle_vector_size_attribute (tree *node, tree name, tree args,
7423 int ARG_UNUSED (flags), bool *no_add_attrs)
7424 {
7425 tree type = *node;
7426 tree vector_type;
7427
7428 *no_add_attrs = true;
7429
7430 /* We need to provide for vector pointers, vector arrays, and
7431 functions returning vectors. For example:
7432
7433 __attribute__((vector_size(16))) short *foo;
7434
7435 In this case, the mode is SI, but the type being modified is
7436 HI, so we need to look further. */
7437 while (POINTER_TYPE_P (type)
7438 || TREE_CODE (type) == FUNCTION_TYPE
7439 || TREE_CODE (type) == ARRAY_TYPE)
7440 type = TREE_TYPE (type);
7441
7442 vector_type = build_vector_type_for_size (type, TREE_VALUE (args), name);
7443 if (!vector_type)
7444 return NULL_TREE;
7445
7446 /* Build back pointers if needed. */
7447 *node = reconstruct_complex_type (*node, vector_type);
7448
7449 return NULL_TREE;
7450 }
7451
7452 /* Handle a "vector_type" attribute; arguments as in
7453 struct attribute_spec.handler. */
7454
7455 static tree
7456 handle_vector_type_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7457 int ARG_UNUSED (flags), bool *no_add_attrs)
7458 {
7459 tree type = *node;
7460 tree vector_type;
7461
7462 *no_add_attrs = true;
7463
7464 if (TREE_CODE (type) != ARRAY_TYPE)
7465 {
7466 error ("attribute %qs applies to array types only",
7467 IDENTIFIER_POINTER (name));
7468 return NULL_TREE;
7469 }
7470
7471 vector_type = build_vector_type_for_array (type, name);
7472 if (!vector_type)
7473 return NULL_TREE;
7474
7475 TYPE_REPRESENTATIVE_ARRAY (vector_type) = type;
7476 *node = vector_type;
7477
7478 return NULL_TREE;
7479 }
7480
7481 /* Handle a "zero_call_used_regs" attribute; arguments as in
7482 struct attribute_spec.handler. */
7483
7484 static tree
7485 handle_zero_call_used_regs_attribute (tree *node, tree name, tree args,
7486 int ARG_UNUSED (flags),
7487 bool *no_add_attrs)
7488 {
7489 tree decl = *node;
7490 tree id = TREE_VALUE (args);
7491
7492 if (TREE_CODE (decl) != FUNCTION_DECL)
7493 {
7494 error_at (DECL_SOURCE_LOCATION (decl),
7495 "%qE attribute applies only to functions", name);
7496 *no_add_attrs = true;
7497 return NULL_TREE;
7498 }
7499
7500 /* pragma Machine_Attribute turns string arguments into identifiers.
7501 Reverse it. */
7502 if (TREE_CODE (id) == IDENTIFIER_NODE)
7503 id = TREE_VALUE (args) = build_string
7504 (IDENTIFIER_LENGTH (id), IDENTIFIER_POINTER (id));
7505
7506 if (TREE_CODE (id) != STRING_CST)
7507 {
7508 error_at (DECL_SOURCE_LOCATION (decl),
7509 "%qE argument not a string", name);
7510 *no_add_attrs = true;
7511 return NULL_TREE;
7512 }
7513
7514 bool found = false;
7515 for (unsigned int i = 0; zero_call_used_regs_opts[i].name != NULL; ++i)
7516 if (strcmp (TREE_STRING_POINTER (id),
7517 zero_call_used_regs_opts[i].name) == 0)
7518 {
7519 found = true;
7520 break;
7521 }
7522
7523 if (!found)
7524 {
7525 error_at (DECL_SOURCE_LOCATION (decl),
7526 "unrecognized %qE attribute argument %qs",
7527 name, TREE_STRING_POINTER (id));
7528 *no_add_attrs = true;
7529 }
7530
7531 return NULL_TREE;
7532 }
7533
7534 /* ----------------------------------------------------------------------- *
7535 * BUILTIN FUNCTIONS *
7536 * ----------------------------------------------------------------------- */
7537
7538 /* Worker for DEF_BUILTIN. Possibly define a builtin function with one or two
7539 names. Does not declare a non-__builtin_ function if flag_no_builtin, or
7540 if nonansi_p and flag_no_nonansi_builtin. */
7541
7542 static void
7543 def_builtin_1 (enum built_in_function fncode,
7544 const char *name,
7545 enum built_in_class fnclass,
7546 tree fntype, tree libtype,
7547 bool both_p, bool fallback_p,
7548 bool nonansi_p ATTRIBUTE_UNUSED,
7549 tree fnattrs, bool implicit_p)
7550 {
7551 tree decl;
7552 const char *libname;
7553
7554 /* Preserve an already installed decl. It most likely was setup in advance
7555 (e.g. as part of the internal builtins) for specific reasons. */
7556 if (builtin_decl_explicit (fncode))
7557 return;
7558
7559 if (fntype == error_mark_node)
7560 return;
7561
7562 gcc_assert ((!both_p && !fallback_p)
7563 || startswith (name, "__builtin_"));
7564
7565 libname = name + strlen ("__builtin_");
7566 decl = add_builtin_function (name, fntype, fncode, fnclass,
7567 (fallback_p ? libname : NULL),
7568 fnattrs);
7569 if (both_p)
7570 /* ??? This is normally further controlled by command-line options
7571 like -fno-builtin, but we don't have them for Ada. */
7572 add_builtin_function (libname, libtype, fncode, fnclass,
7573 NULL, fnattrs);
7574
7575 set_builtin_decl (fncode, decl, implicit_p);
7576 }
7577
7578 static int flag_isoc94 = 0;
7579 static int flag_isoc99 = 0;
7580 static int flag_isoc11 = 0;
7581 static int flag_isoc23 = 0;
7582 static int flag_isoc2y = 0;
7583
7584 /* Install what the common builtins.def offers plus our local additions.
7585
7586 Note that ada-builtins.def is included first so that locally redefined
7587 built-in functions take precedence over the commonly defined ones. */
7588
7589 static void
7590 install_builtin_functions (void)
7591 {
7592 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
7593 NONANSI_P, ATTRS, IMPLICIT, COND) \
7594 if (NAME && COND) \
7595 def_builtin_1 (ENUM, NAME, CLASS, \
7596 builtin_types[(int) TYPE], \
7597 builtin_types[(int) LIBTYPE], \
7598 BOTH_P, FALLBACK_P, NONANSI_P, \
7599 built_in_attributes[(int) ATTRS], IMPLICIT);
7600 #define DEF_ADA_BUILTIN(ENUM, NAME, TYPE, ATTRS) \
7601 DEF_BUILTIN (ENUM, "__builtin_" NAME, BUILT_IN_FRONTEND, TYPE, BT_LAST, \
7602 false, false, false, ATTRS, true, true)
7603 #include "ada-builtins.def"
7604 #include "builtins.def"
7605 }
7606
7607 /* ----------------------------------------------------------------------- *
7608 * BUILTIN FUNCTIONS *
7609 * ----------------------------------------------------------------------- */
7610
7611 /* Install the builtin functions we might need. */
7612
7613 void
7614 gnat_install_builtins (void)
7615 {
7616 install_builtin_elementary_types ();
7617 install_builtin_function_types ();
7618 install_builtin_attributes ();
7619
7620 /* Install builtins used by generic middle-end pieces first. Some of these
7621 know about internal specificities and control attributes accordingly, for
7622 instance __builtin_alloca vs no-throw and -fstack-check. We will ignore
7623 the generic definition from builtins.def. */
7624 build_common_builtin_nodes ();
7625
7626 /* Now, install the target specific builtins, such as the AltiVec family on
7627 ppc, and the common set as exposed by builtins.def. */
7628 targetm.init_builtins ();
7629 install_builtin_functions ();
7630 }
7631
7632 #include "gt-ada-utils.h"
7633 #include "gtype-ada.h"